pxa_dma.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  1. /*
  2. * Copyright 2015 Robert Jarzmik <robert.jarzmik@free.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/err.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/types.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/slab.h>
  15. #include <linux/dmaengine.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/device.h>
  18. #include <linux/platform_data/mmp_dma.h>
  19. #include <linux/dmapool.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_dma.h>
  22. #include <linux/of.h>
  23. #include <linux/wait.h>
  24. #include <linux/dma/pxa-dma.h>
  25. #include "dmaengine.h"
  26. #include "virt-dma.h"
  27. #define DCSR(n) (0x0000 + ((n) << 2))
  28. #define DALGN(n) 0x00a0
  29. #define DINT 0x00f0
  30. #define DDADR(n) (0x0200 + ((n) << 4))
  31. #define DSADR(n) (0x0204 + ((n) << 4))
  32. #define DTADR(n) (0x0208 + ((n) << 4))
  33. #define DCMD(n) (0x020c + ((n) << 4))
  34. #define PXA_DCSR_RUN BIT(31) /* Run Bit (read / write) */
  35. #define PXA_DCSR_NODESC BIT(30) /* No-Descriptor Fetch (read / write) */
  36. #define PXA_DCSR_STOPIRQEN BIT(29) /* Stop Interrupt Enable (R/W) */
  37. #define PXA_DCSR_REQPEND BIT(8) /* Request Pending (read-only) */
  38. #define PXA_DCSR_STOPSTATE BIT(3) /* Stop State (read-only) */
  39. #define PXA_DCSR_ENDINTR BIT(2) /* End Interrupt (read / write) */
  40. #define PXA_DCSR_STARTINTR BIT(1) /* Start Interrupt (read / write) */
  41. #define PXA_DCSR_BUSERR BIT(0) /* Bus Error Interrupt (read / write) */
  42. #define PXA_DCSR_EORIRQEN BIT(28) /* End of Receive IRQ Enable (R/W) */
  43. #define PXA_DCSR_EORJMPEN BIT(27) /* Jump to next descriptor on EOR */
  44. #define PXA_DCSR_EORSTOPEN BIT(26) /* STOP on an EOR */
  45. #define PXA_DCSR_SETCMPST BIT(25) /* Set Descriptor Compare Status */
  46. #define PXA_DCSR_CLRCMPST BIT(24) /* Clear Descriptor Compare Status */
  47. #define PXA_DCSR_CMPST BIT(10) /* The Descriptor Compare Status */
  48. #define PXA_DCSR_EORINTR BIT(9) /* The end of Receive */
  49. #define DRCMR_MAPVLD BIT(7) /* Map Valid (read / write) */
  50. #define DRCMR_CHLNUM 0x1f /* mask for Channel Number (read / write) */
  51. #define DDADR_DESCADDR 0xfffffff0 /* Address of next descriptor (mask) */
  52. #define DDADR_STOP BIT(0) /* Stop (read / write) */
  53. #define PXA_DCMD_INCSRCADDR BIT(31) /* Source Address Increment Setting. */
  54. #define PXA_DCMD_INCTRGADDR BIT(30) /* Target Address Increment Setting. */
  55. #define PXA_DCMD_FLOWSRC BIT(29) /* Flow Control by the source. */
  56. #define PXA_DCMD_FLOWTRG BIT(28) /* Flow Control by the target. */
  57. #define PXA_DCMD_STARTIRQEN BIT(22) /* Start Interrupt Enable */
  58. #define PXA_DCMD_ENDIRQEN BIT(21) /* End Interrupt Enable */
  59. #define PXA_DCMD_ENDIAN BIT(18) /* Device Endian-ness. */
  60. #define PXA_DCMD_BURST8 (1 << 16) /* 8 byte burst */
  61. #define PXA_DCMD_BURST16 (2 << 16) /* 16 byte burst */
  62. #define PXA_DCMD_BURST32 (3 << 16) /* 32 byte burst */
  63. #define PXA_DCMD_WIDTH1 (1 << 14) /* 1 byte width */
  64. #define PXA_DCMD_WIDTH2 (2 << 14) /* 2 byte width (HalfWord) */
  65. #define PXA_DCMD_WIDTH4 (3 << 14) /* 4 byte width (Word) */
  66. #define PXA_DCMD_LENGTH 0x01fff /* length mask (max = 8K - 1) */
  67. #define PDMA_ALIGNMENT 3
  68. #define PDMA_MAX_DESC_BYTES (PXA_DCMD_LENGTH & ~((1 << PDMA_ALIGNMENT) - 1))
  69. struct pxad_desc_hw {
  70. u32 ddadr; /* Points to the next descriptor + flags */
  71. u32 dsadr; /* DSADR value for the current transfer */
  72. u32 dtadr; /* DTADR value for the current transfer */
  73. u32 dcmd; /* DCMD value for the current transfer */
  74. } __aligned(16);
  75. struct pxad_desc_sw {
  76. struct virt_dma_desc vd; /* Virtual descriptor */
  77. int nb_desc; /* Number of hw. descriptors */
  78. size_t len; /* Number of bytes xfered */
  79. dma_addr_t first; /* First descriptor's addr */
  80. /* At least one descriptor has an src/dst address not multiple of 8 */
  81. bool misaligned;
  82. bool cyclic;
  83. struct dma_pool *desc_pool; /* Channel's used allocator */
  84. struct pxad_desc_hw *hw_desc[]; /* DMA coherent descriptors */
  85. };
  86. struct pxad_phy {
  87. int idx;
  88. void __iomem *base;
  89. struct pxad_chan *vchan;
  90. };
  91. struct pxad_chan {
  92. struct virt_dma_chan vc; /* Virtual channel */
  93. u32 drcmr; /* Requestor of the channel */
  94. enum pxad_chan_prio prio; /* Required priority of phy */
  95. /*
  96. * At least one desc_sw in submitted or issued transfers on this channel
  97. * has one address such as: addr % 8 != 0. This implies the DALGN
  98. * setting on the phy.
  99. */
  100. bool misaligned;
  101. struct dma_slave_config cfg; /* Runtime config */
  102. /* protected by vc->lock */
  103. struct pxad_phy *phy;
  104. struct dma_pool *desc_pool; /* Descriptors pool */
  105. dma_cookie_t bus_error;
  106. wait_queue_head_t wq_state;
  107. };
  108. struct pxad_device {
  109. struct dma_device slave;
  110. int nr_chans;
  111. int nr_requestors;
  112. void __iomem *base;
  113. struct pxad_phy *phys;
  114. spinlock_t phy_lock; /* Phy association */
  115. #ifdef CONFIG_DEBUG_FS
  116. struct dentry *dbgfs_root;
  117. struct dentry *dbgfs_state;
  118. struct dentry **dbgfs_chan;
  119. #endif
  120. };
  121. #define tx_to_pxad_desc(tx) \
  122. container_of(tx, struct pxad_desc_sw, async_tx)
  123. #define to_pxad_chan(dchan) \
  124. container_of(dchan, struct pxad_chan, vc.chan)
  125. #define to_pxad_dev(dmadev) \
  126. container_of(dmadev, struct pxad_device, slave)
  127. #define to_pxad_sw_desc(_vd) \
  128. container_of((_vd), struct pxad_desc_sw, vd)
  129. #define _phy_readl_relaxed(phy, _reg) \
  130. readl_relaxed((phy)->base + _reg((phy)->idx))
  131. #define phy_readl_relaxed(phy, _reg) \
  132. ({ \
  133. u32 _v; \
  134. _v = readl_relaxed((phy)->base + _reg((phy)->idx)); \
  135. dev_vdbg(&phy->vchan->vc.chan.dev->device, \
  136. "%s(): readl(%s): 0x%08x\n", __func__, #_reg, \
  137. _v); \
  138. _v; \
  139. })
  140. #define phy_writel(phy, val, _reg) \
  141. do { \
  142. writel((val), (phy)->base + _reg((phy)->idx)); \
  143. dev_vdbg(&phy->vchan->vc.chan.dev->device, \
  144. "%s(): writel(0x%08x, %s)\n", \
  145. __func__, (u32)(val), #_reg); \
  146. } while (0)
  147. #define phy_writel_relaxed(phy, val, _reg) \
  148. do { \
  149. writel_relaxed((val), (phy)->base + _reg((phy)->idx)); \
  150. dev_vdbg(&phy->vchan->vc.chan.dev->device, \
  151. "%s(): writel_relaxed(0x%08x, %s)\n", \
  152. __func__, (u32)(val), #_reg); \
  153. } while (0)
  154. static unsigned int pxad_drcmr(unsigned int line)
  155. {
  156. if (line < 64)
  157. return 0x100 + line * 4;
  158. return 0x1000 + line * 4;
  159. }
  160. /*
  161. * Debug fs
  162. */
  163. #ifdef CONFIG_DEBUG_FS
  164. #include <linux/debugfs.h>
  165. #include <linux/uaccess.h>
  166. #include <linux/seq_file.h>
  167. static int dbg_show_requester_chan(struct seq_file *s, void *p)
  168. {
  169. struct pxad_phy *phy = s->private;
  170. int i;
  171. u32 drcmr;
  172. seq_printf(s, "DMA channel %d requester :\n", phy->idx);
  173. for (i = 0; i < 70; i++) {
  174. drcmr = readl_relaxed(phy->base + pxad_drcmr(i));
  175. if ((drcmr & DRCMR_CHLNUM) == phy->idx)
  176. seq_printf(s, "\tRequester %d (MAPVLD=%d)\n", i,
  177. !!(drcmr & DRCMR_MAPVLD));
  178. }
  179. return 0;
  180. }
  181. static inline int dbg_burst_from_dcmd(u32 dcmd)
  182. {
  183. int burst = (dcmd >> 16) & 0x3;
  184. return burst ? 4 << burst : 0;
  185. }
  186. static int is_phys_valid(unsigned long addr)
  187. {
  188. return pfn_valid(__phys_to_pfn(addr));
  189. }
  190. #define PXA_DCSR_STR(flag) (dcsr & PXA_DCSR_##flag ? #flag" " : "")
  191. #define PXA_DCMD_STR(flag) (dcmd & PXA_DCMD_##flag ? #flag" " : "")
  192. static int dbg_show_descriptors(struct seq_file *s, void *p)
  193. {
  194. struct pxad_phy *phy = s->private;
  195. int i, max_show = 20, burst, width;
  196. u32 dcmd;
  197. unsigned long phys_desc, ddadr;
  198. struct pxad_desc_hw *desc;
  199. phys_desc = ddadr = _phy_readl_relaxed(phy, DDADR);
  200. seq_printf(s, "DMA channel %d descriptors :\n", phy->idx);
  201. seq_printf(s, "[%03d] First descriptor unknown\n", 0);
  202. for (i = 1; i < max_show && is_phys_valid(phys_desc); i++) {
  203. desc = phys_to_virt(phys_desc);
  204. dcmd = desc->dcmd;
  205. burst = dbg_burst_from_dcmd(dcmd);
  206. width = (1 << ((dcmd >> 14) & 0x3)) >> 1;
  207. seq_printf(s, "[%03d] Desc at %08lx(virt %p)\n",
  208. i, phys_desc, desc);
  209. seq_printf(s, "\tDDADR = %08x\n", desc->ddadr);
  210. seq_printf(s, "\tDSADR = %08x\n", desc->dsadr);
  211. seq_printf(s, "\tDTADR = %08x\n", desc->dtadr);
  212. seq_printf(s, "\tDCMD = %08x (%s%s%s%s%s%s%sburst=%d width=%d len=%d)\n",
  213. dcmd,
  214. PXA_DCMD_STR(INCSRCADDR), PXA_DCMD_STR(INCTRGADDR),
  215. PXA_DCMD_STR(FLOWSRC), PXA_DCMD_STR(FLOWTRG),
  216. PXA_DCMD_STR(STARTIRQEN), PXA_DCMD_STR(ENDIRQEN),
  217. PXA_DCMD_STR(ENDIAN), burst, width,
  218. dcmd & PXA_DCMD_LENGTH);
  219. phys_desc = desc->ddadr;
  220. }
  221. if (i == max_show)
  222. seq_printf(s, "[%03d] Desc at %08lx ... max display reached\n",
  223. i, phys_desc);
  224. else
  225. seq_printf(s, "[%03d] Desc at %08lx is %s\n",
  226. i, phys_desc, phys_desc == DDADR_STOP ?
  227. "DDADR_STOP" : "invalid");
  228. return 0;
  229. }
  230. static int dbg_show_chan_state(struct seq_file *s, void *p)
  231. {
  232. struct pxad_phy *phy = s->private;
  233. u32 dcsr, dcmd;
  234. int burst, width;
  235. static const char * const str_prio[] = {
  236. "high", "normal", "low", "invalid"
  237. };
  238. dcsr = _phy_readl_relaxed(phy, DCSR);
  239. dcmd = _phy_readl_relaxed(phy, DCMD);
  240. burst = dbg_burst_from_dcmd(dcmd);
  241. width = (1 << ((dcmd >> 14) & 0x3)) >> 1;
  242. seq_printf(s, "DMA channel %d\n", phy->idx);
  243. seq_printf(s, "\tPriority : %s\n",
  244. str_prio[(phy->idx & 0xf) / 4]);
  245. seq_printf(s, "\tUnaligned transfer bit: %s\n",
  246. _phy_readl_relaxed(phy, DALGN) & BIT(phy->idx) ?
  247. "yes" : "no");
  248. seq_printf(s, "\tDCSR = %08x (%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
  249. dcsr, PXA_DCSR_STR(RUN), PXA_DCSR_STR(NODESC),
  250. PXA_DCSR_STR(STOPIRQEN), PXA_DCSR_STR(EORIRQEN),
  251. PXA_DCSR_STR(EORJMPEN), PXA_DCSR_STR(EORSTOPEN),
  252. PXA_DCSR_STR(SETCMPST), PXA_DCSR_STR(CLRCMPST),
  253. PXA_DCSR_STR(CMPST), PXA_DCSR_STR(EORINTR),
  254. PXA_DCSR_STR(REQPEND), PXA_DCSR_STR(STOPSTATE),
  255. PXA_DCSR_STR(ENDINTR), PXA_DCSR_STR(STARTINTR),
  256. PXA_DCSR_STR(BUSERR));
  257. seq_printf(s, "\tDCMD = %08x (%s%s%s%s%s%s%sburst=%d width=%d len=%d)\n",
  258. dcmd,
  259. PXA_DCMD_STR(INCSRCADDR), PXA_DCMD_STR(INCTRGADDR),
  260. PXA_DCMD_STR(FLOWSRC), PXA_DCMD_STR(FLOWTRG),
  261. PXA_DCMD_STR(STARTIRQEN), PXA_DCMD_STR(ENDIRQEN),
  262. PXA_DCMD_STR(ENDIAN), burst, width, dcmd & PXA_DCMD_LENGTH);
  263. seq_printf(s, "\tDSADR = %08x\n", _phy_readl_relaxed(phy, DSADR));
  264. seq_printf(s, "\tDTADR = %08x\n", _phy_readl_relaxed(phy, DTADR));
  265. seq_printf(s, "\tDDADR = %08x\n", _phy_readl_relaxed(phy, DDADR));
  266. return 0;
  267. }
  268. static int dbg_show_state(struct seq_file *s, void *p)
  269. {
  270. struct pxad_device *pdev = s->private;
  271. /* basic device status */
  272. seq_puts(s, "DMA engine status\n");
  273. seq_printf(s, "\tChannel number: %d\n", pdev->nr_chans);
  274. return 0;
  275. }
  276. #define DBGFS_FUNC_DECL(name) \
  277. static int dbg_open_##name(struct inode *inode, struct file *file) \
  278. { \
  279. return single_open(file, dbg_show_##name, inode->i_private); \
  280. } \
  281. static const struct file_operations dbg_fops_##name = { \
  282. .open = dbg_open_##name, \
  283. .llseek = seq_lseek, \
  284. .read = seq_read, \
  285. .release = single_release, \
  286. }
  287. DBGFS_FUNC_DECL(state);
  288. DBGFS_FUNC_DECL(chan_state);
  289. DBGFS_FUNC_DECL(descriptors);
  290. DBGFS_FUNC_DECL(requester_chan);
  291. static struct dentry *pxad_dbg_alloc_chan(struct pxad_device *pdev,
  292. int ch, struct dentry *chandir)
  293. {
  294. char chan_name[11];
  295. struct dentry *chan, *chan_state = NULL, *chan_descr = NULL;
  296. struct dentry *chan_reqs = NULL;
  297. void *dt;
  298. scnprintf(chan_name, sizeof(chan_name), "%d", ch);
  299. chan = debugfs_create_dir(chan_name, chandir);
  300. dt = (void *)&pdev->phys[ch];
  301. if (chan)
  302. chan_state = debugfs_create_file("state", 0400, chan, dt,
  303. &dbg_fops_chan_state);
  304. if (chan_state)
  305. chan_descr = debugfs_create_file("descriptors", 0400, chan, dt,
  306. &dbg_fops_descriptors);
  307. if (chan_descr)
  308. chan_reqs = debugfs_create_file("requesters", 0400, chan, dt,
  309. &dbg_fops_requester_chan);
  310. if (!chan_reqs)
  311. goto err_state;
  312. return chan;
  313. err_state:
  314. debugfs_remove_recursive(chan);
  315. return NULL;
  316. }
  317. static void pxad_init_debugfs(struct pxad_device *pdev)
  318. {
  319. int i;
  320. struct dentry *chandir;
  321. pdev->dbgfs_root = debugfs_create_dir(dev_name(pdev->slave.dev), NULL);
  322. if (IS_ERR(pdev->dbgfs_root) || !pdev->dbgfs_root)
  323. goto err_root;
  324. pdev->dbgfs_state = debugfs_create_file("state", 0400, pdev->dbgfs_root,
  325. pdev, &dbg_fops_state);
  326. if (!pdev->dbgfs_state)
  327. goto err_state;
  328. pdev->dbgfs_chan =
  329. kmalloc_array(pdev->nr_chans, sizeof(*pdev->dbgfs_state),
  330. GFP_KERNEL);
  331. if (!pdev->dbgfs_chan)
  332. goto err_alloc;
  333. chandir = debugfs_create_dir("channels", pdev->dbgfs_root);
  334. if (!chandir)
  335. goto err_chandir;
  336. for (i = 0; i < pdev->nr_chans; i++) {
  337. pdev->dbgfs_chan[i] = pxad_dbg_alloc_chan(pdev, i, chandir);
  338. if (!pdev->dbgfs_chan[i])
  339. goto err_chans;
  340. }
  341. return;
  342. err_chans:
  343. err_chandir:
  344. kfree(pdev->dbgfs_chan);
  345. err_alloc:
  346. err_state:
  347. debugfs_remove_recursive(pdev->dbgfs_root);
  348. err_root:
  349. pr_err("pxad: debugfs is not available\n");
  350. }
  351. static void pxad_cleanup_debugfs(struct pxad_device *pdev)
  352. {
  353. debugfs_remove_recursive(pdev->dbgfs_root);
  354. }
  355. #else
  356. static inline void pxad_init_debugfs(struct pxad_device *pdev) {}
  357. static inline void pxad_cleanup_debugfs(struct pxad_device *pdev) {}
  358. #endif
  359. static struct pxad_phy *lookup_phy(struct pxad_chan *pchan)
  360. {
  361. int prio, i;
  362. struct pxad_device *pdev = to_pxad_dev(pchan->vc.chan.device);
  363. struct pxad_phy *phy, *found = NULL;
  364. unsigned long flags;
  365. /*
  366. * dma channel priorities
  367. * ch 0 - 3, 16 - 19 <--> (0)
  368. * ch 4 - 7, 20 - 23 <--> (1)
  369. * ch 8 - 11, 24 - 27 <--> (2)
  370. * ch 12 - 15, 28 - 31 <--> (3)
  371. */
  372. spin_lock_irqsave(&pdev->phy_lock, flags);
  373. for (prio = pchan->prio; prio >= PXAD_PRIO_HIGHEST; prio--) {
  374. for (i = 0; i < pdev->nr_chans; i++) {
  375. if (prio != (i & 0xf) >> 2)
  376. continue;
  377. phy = &pdev->phys[i];
  378. if (!phy->vchan) {
  379. phy->vchan = pchan;
  380. found = phy;
  381. goto out_unlock;
  382. }
  383. }
  384. }
  385. out_unlock:
  386. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  387. dev_dbg(&pchan->vc.chan.dev->device,
  388. "%s(): phy=%p(%d)\n", __func__, found,
  389. found ? found->idx : -1);
  390. return found;
  391. }
  392. static void pxad_free_phy(struct pxad_chan *chan)
  393. {
  394. struct pxad_device *pdev = to_pxad_dev(chan->vc.chan.device);
  395. unsigned long flags;
  396. u32 reg;
  397. dev_dbg(&chan->vc.chan.dev->device,
  398. "%s(): freeing\n", __func__);
  399. if (!chan->phy)
  400. return;
  401. /* clear the channel mapping in DRCMR */
  402. if (chan->drcmr <= pdev->nr_requestors) {
  403. reg = pxad_drcmr(chan->drcmr);
  404. writel_relaxed(0, chan->phy->base + reg);
  405. }
  406. spin_lock_irqsave(&pdev->phy_lock, flags);
  407. chan->phy->vchan = NULL;
  408. chan->phy = NULL;
  409. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  410. }
  411. static bool is_chan_running(struct pxad_chan *chan)
  412. {
  413. u32 dcsr;
  414. struct pxad_phy *phy = chan->phy;
  415. if (!phy)
  416. return false;
  417. dcsr = phy_readl_relaxed(phy, DCSR);
  418. return dcsr & PXA_DCSR_RUN;
  419. }
  420. static bool is_running_chan_misaligned(struct pxad_chan *chan)
  421. {
  422. u32 dalgn;
  423. BUG_ON(!chan->phy);
  424. dalgn = phy_readl_relaxed(chan->phy, DALGN);
  425. return dalgn & (BIT(chan->phy->idx));
  426. }
  427. static void phy_enable(struct pxad_phy *phy, bool misaligned)
  428. {
  429. struct pxad_device *pdev;
  430. u32 reg, dalgn;
  431. if (!phy->vchan)
  432. return;
  433. dev_dbg(&phy->vchan->vc.chan.dev->device,
  434. "%s(); phy=%p(%d) misaligned=%d\n", __func__,
  435. phy, phy->idx, misaligned);
  436. pdev = to_pxad_dev(phy->vchan->vc.chan.device);
  437. if (phy->vchan->drcmr <= pdev->nr_requestors) {
  438. reg = pxad_drcmr(phy->vchan->drcmr);
  439. writel_relaxed(DRCMR_MAPVLD | phy->idx, phy->base + reg);
  440. }
  441. dalgn = phy_readl_relaxed(phy, DALGN);
  442. if (misaligned)
  443. dalgn |= BIT(phy->idx);
  444. else
  445. dalgn &= ~BIT(phy->idx);
  446. phy_writel_relaxed(phy, dalgn, DALGN);
  447. phy_writel(phy, PXA_DCSR_STOPIRQEN | PXA_DCSR_ENDINTR |
  448. PXA_DCSR_BUSERR | PXA_DCSR_RUN, DCSR);
  449. }
  450. static void phy_disable(struct pxad_phy *phy)
  451. {
  452. u32 dcsr;
  453. if (!phy)
  454. return;
  455. dcsr = phy_readl_relaxed(phy, DCSR);
  456. dev_dbg(&phy->vchan->vc.chan.dev->device,
  457. "%s(): phy=%p(%d)\n", __func__, phy, phy->idx);
  458. phy_writel(phy, dcsr & ~PXA_DCSR_RUN & ~PXA_DCSR_STOPIRQEN, DCSR);
  459. }
  460. static void pxad_launch_chan(struct pxad_chan *chan,
  461. struct pxad_desc_sw *desc)
  462. {
  463. dev_dbg(&chan->vc.chan.dev->device,
  464. "%s(): desc=%p\n", __func__, desc);
  465. if (!chan->phy) {
  466. chan->phy = lookup_phy(chan);
  467. if (!chan->phy) {
  468. dev_dbg(&chan->vc.chan.dev->device,
  469. "%s(): no free dma channel\n", __func__);
  470. return;
  471. }
  472. }
  473. chan->bus_error = 0;
  474. /*
  475. * Program the descriptor's address into the DMA controller,
  476. * then start the DMA transaction
  477. */
  478. phy_writel(chan->phy, desc->first, DDADR);
  479. phy_enable(chan->phy, chan->misaligned);
  480. wake_up(&chan->wq_state);
  481. }
  482. static void set_updater_desc(struct pxad_desc_sw *sw_desc,
  483. unsigned long flags)
  484. {
  485. struct pxad_desc_hw *updater =
  486. sw_desc->hw_desc[sw_desc->nb_desc - 1];
  487. dma_addr_t dma = sw_desc->hw_desc[sw_desc->nb_desc - 2]->ddadr;
  488. updater->ddadr = DDADR_STOP;
  489. updater->dsadr = dma;
  490. updater->dtadr = dma + 8;
  491. updater->dcmd = PXA_DCMD_WIDTH4 | PXA_DCMD_BURST32 |
  492. (PXA_DCMD_LENGTH & sizeof(u32));
  493. if (flags & DMA_PREP_INTERRUPT)
  494. updater->dcmd |= PXA_DCMD_ENDIRQEN;
  495. if (sw_desc->cyclic)
  496. sw_desc->hw_desc[sw_desc->nb_desc - 2]->ddadr = sw_desc->first;
  497. }
  498. static bool is_desc_completed(struct virt_dma_desc *vd)
  499. {
  500. struct pxad_desc_sw *sw_desc = to_pxad_sw_desc(vd);
  501. struct pxad_desc_hw *updater =
  502. sw_desc->hw_desc[sw_desc->nb_desc - 1];
  503. return updater->dtadr != (updater->dsadr + 8);
  504. }
  505. static void pxad_desc_chain(struct virt_dma_desc *vd1,
  506. struct virt_dma_desc *vd2)
  507. {
  508. struct pxad_desc_sw *desc1 = to_pxad_sw_desc(vd1);
  509. struct pxad_desc_sw *desc2 = to_pxad_sw_desc(vd2);
  510. dma_addr_t dma_to_chain;
  511. dma_to_chain = desc2->first;
  512. desc1->hw_desc[desc1->nb_desc - 1]->ddadr = dma_to_chain;
  513. }
  514. static bool pxad_try_hotchain(struct virt_dma_chan *vc,
  515. struct virt_dma_desc *vd)
  516. {
  517. struct virt_dma_desc *vd_last_issued = NULL;
  518. struct pxad_chan *chan = to_pxad_chan(&vc->chan);
  519. /*
  520. * Attempt to hot chain the tx if the phy is still running. This is
  521. * considered successful only if either the channel is still running
  522. * after the chaining, or if the chained transfer is completed after
  523. * having been hot chained.
  524. * A change of alignment is not allowed, and forbids hotchaining.
  525. */
  526. if (is_chan_running(chan)) {
  527. BUG_ON(list_empty(&vc->desc_issued));
  528. if (!is_running_chan_misaligned(chan) &&
  529. to_pxad_sw_desc(vd)->misaligned)
  530. return false;
  531. vd_last_issued = list_entry(vc->desc_issued.prev,
  532. struct virt_dma_desc, node);
  533. pxad_desc_chain(vd_last_issued, vd);
  534. if (is_chan_running(chan) || is_desc_completed(vd))
  535. return true;
  536. }
  537. return false;
  538. }
  539. static unsigned int clear_chan_irq(struct pxad_phy *phy)
  540. {
  541. u32 dcsr;
  542. u32 dint = readl(phy->base + DINT);
  543. if (!(dint & BIT(phy->idx)))
  544. return PXA_DCSR_RUN;
  545. /* clear irq */
  546. dcsr = phy_readl_relaxed(phy, DCSR);
  547. phy_writel(phy, dcsr, DCSR);
  548. if ((dcsr & PXA_DCSR_BUSERR) && (phy->vchan))
  549. dev_warn(&phy->vchan->vc.chan.dev->device,
  550. "%s(chan=%p): PXA_DCSR_BUSERR\n",
  551. __func__, &phy->vchan);
  552. return dcsr & ~PXA_DCSR_RUN;
  553. }
  554. static irqreturn_t pxad_chan_handler(int irq, void *dev_id)
  555. {
  556. struct pxad_phy *phy = dev_id;
  557. struct pxad_chan *chan = phy->vchan;
  558. struct virt_dma_desc *vd, *tmp;
  559. unsigned int dcsr;
  560. unsigned long flags;
  561. bool vd_completed;
  562. dma_cookie_t last_started = 0;
  563. BUG_ON(!chan);
  564. dcsr = clear_chan_irq(phy);
  565. if (dcsr & PXA_DCSR_RUN)
  566. return IRQ_NONE;
  567. spin_lock_irqsave(&chan->vc.lock, flags);
  568. list_for_each_entry_safe(vd, tmp, &chan->vc.desc_issued, node) {
  569. vd_completed = is_desc_completed(vd);
  570. dev_dbg(&chan->vc.chan.dev->device,
  571. "%s(): checking txd %p[%x]: completed=%d dcsr=0x%x\n",
  572. __func__, vd, vd->tx.cookie, vd_completed,
  573. dcsr);
  574. last_started = vd->tx.cookie;
  575. if (to_pxad_sw_desc(vd)->cyclic) {
  576. vchan_cyclic_callback(vd);
  577. break;
  578. }
  579. if (vd_completed) {
  580. list_del(&vd->node);
  581. vchan_cookie_complete(vd);
  582. } else {
  583. break;
  584. }
  585. }
  586. if (dcsr & PXA_DCSR_BUSERR) {
  587. chan->bus_error = last_started;
  588. phy_disable(phy);
  589. }
  590. if (!chan->bus_error && dcsr & PXA_DCSR_STOPSTATE) {
  591. dev_dbg(&chan->vc.chan.dev->device,
  592. "%s(): channel stopped, submitted_empty=%d issued_empty=%d",
  593. __func__,
  594. list_empty(&chan->vc.desc_submitted),
  595. list_empty(&chan->vc.desc_issued));
  596. phy_writel_relaxed(phy, dcsr & ~PXA_DCSR_STOPIRQEN, DCSR);
  597. if (list_empty(&chan->vc.desc_issued)) {
  598. chan->misaligned =
  599. !list_empty(&chan->vc.desc_submitted);
  600. } else {
  601. vd = list_first_entry(&chan->vc.desc_issued,
  602. struct virt_dma_desc, node);
  603. pxad_launch_chan(chan, to_pxad_sw_desc(vd));
  604. }
  605. }
  606. spin_unlock_irqrestore(&chan->vc.lock, flags);
  607. wake_up(&chan->wq_state);
  608. return IRQ_HANDLED;
  609. }
  610. static irqreturn_t pxad_int_handler(int irq, void *dev_id)
  611. {
  612. struct pxad_device *pdev = dev_id;
  613. struct pxad_phy *phy;
  614. u32 dint = readl(pdev->base + DINT);
  615. int i, ret = IRQ_NONE;
  616. while (dint) {
  617. i = __ffs(dint);
  618. dint &= (dint - 1);
  619. phy = &pdev->phys[i];
  620. if (pxad_chan_handler(irq, phy) == IRQ_HANDLED)
  621. ret = IRQ_HANDLED;
  622. }
  623. return ret;
  624. }
  625. static int pxad_alloc_chan_resources(struct dma_chan *dchan)
  626. {
  627. struct pxad_chan *chan = to_pxad_chan(dchan);
  628. struct pxad_device *pdev = to_pxad_dev(chan->vc.chan.device);
  629. if (chan->desc_pool)
  630. return 1;
  631. chan->desc_pool = dma_pool_create(dma_chan_name(dchan),
  632. pdev->slave.dev,
  633. sizeof(struct pxad_desc_hw),
  634. __alignof__(struct pxad_desc_hw),
  635. 0);
  636. if (!chan->desc_pool) {
  637. dev_err(&chan->vc.chan.dev->device,
  638. "%s(): unable to allocate descriptor pool\n",
  639. __func__);
  640. return -ENOMEM;
  641. }
  642. return 1;
  643. }
  644. static void pxad_free_chan_resources(struct dma_chan *dchan)
  645. {
  646. struct pxad_chan *chan = to_pxad_chan(dchan);
  647. vchan_free_chan_resources(&chan->vc);
  648. dma_pool_destroy(chan->desc_pool);
  649. chan->desc_pool = NULL;
  650. }
  651. static void pxad_free_desc(struct virt_dma_desc *vd)
  652. {
  653. int i;
  654. dma_addr_t dma;
  655. struct pxad_desc_sw *sw_desc = to_pxad_sw_desc(vd);
  656. BUG_ON(sw_desc->nb_desc == 0);
  657. for (i = sw_desc->nb_desc - 1; i >= 0; i--) {
  658. if (i > 0)
  659. dma = sw_desc->hw_desc[i - 1]->ddadr;
  660. else
  661. dma = sw_desc->first;
  662. dma_pool_free(sw_desc->desc_pool,
  663. sw_desc->hw_desc[i], dma);
  664. }
  665. sw_desc->nb_desc = 0;
  666. kfree(sw_desc);
  667. }
  668. static struct pxad_desc_sw *
  669. pxad_alloc_desc(struct pxad_chan *chan, unsigned int nb_hw_desc)
  670. {
  671. struct pxad_desc_sw *sw_desc;
  672. dma_addr_t dma;
  673. int i;
  674. sw_desc = kzalloc(sizeof(*sw_desc) +
  675. nb_hw_desc * sizeof(struct pxad_desc_hw *),
  676. GFP_NOWAIT);
  677. if (!sw_desc)
  678. return NULL;
  679. sw_desc->desc_pool = chan->desc_pool;
  680. for (i = 0; i < nb_hw_desc; i++) {
  681. sw_desc->hw_desc[i] = dma_pool_alloc(sw_desc->desc_pool,
  682. GFP_NOWAIT, &dma);
  683. if (!sw_desc->hw_desc[i]) {
  684. dev_err(&chan->vc.chan.dev->device,
  685. "%s(): Couldn't allocate the %dth hw_desc from dma_pool %p\n",
  686. __func__, i, sw_desc->desc_pool);
  687. goto err;
  688. }
  689. if (i == 0)
  690. sw_desc->first = dma;
  691. else
  692. sw_desc->hw_desc[i - 1]->ddadr = dma;
  693. sw_desc->nb_desc++;
  694. }
  695. return sw_desc;
  696. err:
  697. pxad_free_desc(&sw_desc->vd);
  698. return NULL;
  699. }
  700. static dma_cookie_t pxad_tx_submit(struct dma_async_tx_descriptor *tx)
  701. {
  702. struct virt_dma_chan *vc = to_virt_chan(tx->chan);
  703. struct pxad_chan *chan = to_pxad_chan(&vc->chan);
  704. struct virt_dma_desc *vd_chained = NULL,
  705. *vd = container_of(tx, struct virt_dma_desc, tx);
  706. dma_cookie_t cookie;
  707. unsigned long flags;
  708. set_updater_desc(to_pxad_sw_desc(vd), tx->flags);
  709. spin_lock_irqsave(&vc->lock, flags);
  710. cookie = dma_cookie_assign(tx);
  711. if (list_empty(&vc->desc_submitted) && pxad_try_hotchain(vc, vd)) {
  712. list_move_tail(&vd->node, &vc->desc_issued);
  713. dev_dbg(&chan->vc.chan.dev->device,
  714. "%s(): txd %p[%x]: submitted (hot linked)\n",
  715. __func__, vd, cookie);
  716. goto out;
  717. }
  718. /*
  719. * Fallback to placing the tx in the submitted queue
  720. */
  721. if (!list_empty(&vc->desc_submitted)) {
  722. vd_chained = list_entry(vc->desc_submitted.prev,
  723. struct virt_dma_desc, node);
  724. /*
  725. * Only chain the descriptors if no new misalignment is
  726. * introduced. If a new misalignment is chained, let the channel
  727. * stop, and be relaunched in misalign mode from the irq
  728. * handler.
  729. */
  730. if (chan->misaligned || !to_pxad_sw_desc(vd)->misaligned)
  731. pxad_desc_chain(vd_chained, vd);
  732. else
  733. vd_chained = NULL;
  734. }
  735. dev_dbg(&chan->vc.chan.dev->device,
  736. "%s(): txd %p[%x]: submitted (%s linked)\n",
  737. __func__, vd, cookie, vd_chained ? "cold" : "not");
  738. list_move_tail(&vd->node, &vc->desc_submitted);
  739. chan->misaligned |= to_pxad_sw_desc(vd)->misaligned;
  740. out:
  741. spin_unlock_irqrestore(&vc->lock, flags);
  742. return cookie;
  743. }
  744. static void pxad_issue_pending(struct dma_chan *dchan)
  745. {
  746. struct pxad_chan *chan = to_pxad_chan(dchan);
  747. struct virt_dma_desc *vd_first;
  748. unsigned long flags;
  749. spin_lock_irqsave(&chan->vc.lock, flags);
  750. if (list_empty(&chan->vc.desc_submitted))
  751. goto out;
  752. vd_first = list_first_entry(&chan->vc.desc_submitted,
  753. struct virt_dma_desc, node);
  754. dev_dbg(&chan->vc.chan.dev->device,
  755. "%s(): txd %p[%x]", __func__, vd_first, vd_first->tx.cookie);
  756. vchan_issue_pending(&chan->vc);
  757. if (!pxad_try_hotchain(&chan->vc, vd_first))
  758. pxad_launch_chan(chan, to_pxad_sw_desc(vd_first));
  759. out:
  760. spin_unlock_irqrestore(&chan->vc.lock, flags);
  761. }
  762. static inline struct dma_async_tx_descriptor *
  763. pxad_tx_prep(struct virt_dma_chan *vc, struct virt_dma_desc *vd,
  764. unsigned long tx_flags)
  765. {
  766. struct dma_async_tx_descriptor *tx;
  767. struct pxad_chan *chan = container_of(vc, struct pxad_chan, vc);
  768. INIT_LIST_HEAD(&vd->node);
  769. tx = vchan_tx_prep(vc, vd, tx_flags);
  770. tx->tx_submit = pxad_tx_submit;
  771. dev_dbg(&chan->vc.chan.dev->device,
  772. "%s(): vc=%p txd=%p[%x] flags=0x%lx\n", __func__,
  773. vc, vd, vd->tx.cookie,
  774. tx_flags);
  775. return tx;
  776. }
  777. static void pxad_get_config(struct pxad_chan *chan,
  778. enum dma_transfer_direction dir,
  779. u32 *dcmd, u32 *dev_src, u32 *dev_dst)
  780. {
  781. u32 maxburst = 0, dev_addr = 0;
  782. enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  783. struct pxad_device *pdev = to_pxad_dev(chan->vc.chan.device);
  784. *dcmd = 0;
  785. if (dir == DMA_DEV_TO_MEM) {
  786. maxburst = chan->cfg.src_maxburst;
  787. width = chan->cfg.src_addr_width;
  788. dev_addr = chan->cfg.src_addr;
  789. *dev_src = dev_addr;
  790. *dcmd |= PXA_DCMD_INCTRGADDR;
  791. if (chan->drcmr <= pdev->nr_requestors)
  792. *dcmd |= PXA_DCMD_FLOWSRC;
  793. }
  794. if (dir == DMA_MEM_TO_DEV) {
  795. maxburst = chan->cfg.dst_maxburst;
  796. width = chan->cfg.dst_addr_width;
  797. dev_addr = chan->cfg.dst_addr;
  798. *dev_dst = dev_addr;
  799. *dcmd |= PXA_DCMD_INCSRCADDR;
  800. if (chan->drcmr <= pdev->nr_requestors)
  801. *dcmd |= PXA_DCMD_FLOWTRG;
  802. }
  803. if (dir == DMA_MEM_TO_MEM)
  804. *dcmd |= PXA_DCMD_BURST32 | PXA_DCMD_INCTRGADDR |
  805. PXA_DCMD_INCSRCADDR;
  806. dev_dbg(&chan->vc.chan.dev->device,
  807. "%s(): dev_addr=0x%x maxburst=%d width=%d dir=%d\n",
  808. __func__, dev_addr, maxburst, width, dir);
  809. if (width == DMA_SLAVE_BUSWIDTH_1_BYTE)
  810. *dcmd |= PXA_DCMD_WIDTH1;
  811. else if (width == DMA_SLAVE_BUSWIDTH_2_BYTES)
  812. *dcmd |= PXA_DCMD_WIDTH2;
  813. else if (width == DMA_SLAVE_BUSWIDTH_4_BYTES)
  814. *dcmd |= PXA_DCMD_WIDTH4;
  815. if (maxburst == 8)
  816. *dcmd |= PXA_DCMD_BURST8;
  817. else if (maxburst == 16)
  818. *dcmd |= PXA_DCMD_BURST16;
  819. else if (maxburst == 32)
  820. *dcmd |= PXA_DCMD_BURST32;
  821. /* FIXME: drivers should be ported over to use the filter
  822. * function. Once that's done, the following two lines can
  823. * be removed.
  824. */
  825. if (chan->cfg.slave_id)
  826. chan->drcmr = chan->cfg.slave_id;
  827. }
  828. static struct dma_async_tx_descriptor *
  829. pxad_prep_memcpy(struct dma_chan *dchan,
  830. dma_addr_t dma_dst, dma_addr_t dma_src,
  831. size_t len, unsigned long flags)
  832. {
  833. struct pxad_chan *chan = to_pxad_chan(dchan);
  834. struct pxad_desc_sw *sw_desc;
  835. struct pxad_desc_hw *hw_desc;
  836. u32 dcmd;
  837. unsigned int i, nb_desc = 0;
  838. size_t copy;
  839. if (!dchan || !len)
  840. return NULL;
  841. dev_dbg(&chan->vc.chan.dev->device,
  842. "%s(): dma_dst=0x%lx dma_src=0x%lx len=%zu flags=%lx\n",
  843. __func__, (unsigned long)dma_dst, (unsigned long)dma_src,
  844. len, flags);
  845. pxad_get_config(chan, DMA_MEM_TO_MEM, &dcmd, NULL, NULL);
  846. nb_desc = DIV_ROUND_UP(len, PDMA_MAX_DESC_BYTES);
  847. sw_desc = pxad_alloc_desc(chan, nb_desc + 1);
  848. if (!sw_desc)
  849. return NULL;
  850. sw_desc->len = len;
  851. if (!IS_ALIGNED(dma_src, 1 << PDMA_ALIGNMENT) ||
  852. !IS_ALIGNED(dma_dst, 1 << PDMA_ALIGNMENT))
  853. sw_desc->misaligned = true;
  854. i = 0;
  855. do {
  856. hw_desc = sw_desc->hw_desc[i++];
  857. copy = min_t(size_t, len, PDMA_MAX_DESC_BYTES);
  858. hw_desc->dcmd = dcmd | (PXA_DCMD_LENGTH & copy);
  859. hw_desc->dsadr = dma_src;
  860. hw_desc->dtadr = dma_dst;
  861. len -= copy;
  862. dma_src += copy;
  863. dma_dst += copy;
  864. } while (len);
  865. set_updater_desc(sw_desc, flags);
  866. return pxad_tx_prep(&chan->vc, &sw_desc->vd, flags);
  867. }
  868. static struct dma_async_tx_descriptor *
  869. pxad_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
  870. unsigned int sg_len, enum dma_transfer_direction dir,
  871. unsigned long flags, void *context)
  872. {
  873. struct pxad_chan *chan = to_pxad_chan(dchan);
  874. struct pxad_desc_sw *sw_desc;
  875. size_t len, avail;
  876. struct scatterlist *sg;
  877. dma_addr_t dma;
  878. u32 dcmd, dsadr = 0, dtadr = 0;
  879. unsigned int nb_desc = 0, i, j = 0;
  880. if ((sgl == NULL) || (sg_len == 0))
  881. return NULL;
  882. pxad_get_config(chan, dir, &dcmd, &dsadr, &dtadr);
  883. dev_dbg(&chan->vc.chan.dev->device,
  884. "%s(): dir=%d flags=%lx\n", __func__, dir, flags);
  885. for_each_sg(sgl, sg, sg_len, i)
  886. nb_desc += DIV_ROUND_UP(sg_dma_len(sg), PDMA_MAX_DESC_BYTES);
  887. sw_desc = pxad_alloc_desc(chan, nb_desc + 1);
  888. if (!sw_desc)
  889. return NULL;
  890. for_each_sg(sgl, sg, sg_len, i) {
  891. dma = sg_dma_address(sg);
  892. avail = sg_dma_len(sg);
  893. sw_desc->len += avail;
  894. do {
  895. len = min_t(size_t, avail, PDMA_MAX_DESC_BYTES);
  896. if (dma & 0x7)
  897. sw_desc->misaligned = true;
  898. sw_desc->hw_desc[j]->dcmd =
  899. dcmd | (PXA_DCMD_LENGTH & len);
  900. sw_desc->hw_desc[j]->dsadr = dsadr ? dsadr : dma;
  901. sw_desc->hw_desc[j++]->dtadr = dtadr ? dtadr : dma;
  902. dma += len;
  903. avail -= len;
  904. } while (avail);
  905. }
  906. set_updater_desc(sw_desc, flags);
  907. return pxad_tx_prep(&chan->vc, &sw_desc->vd, flags);
  908. }
  909. static struct dma_async_tx_descriptor *
  910. pxad_prep_dma_cyclic(struct dma_chan *dchan,
  911. dma_addr_t buf_addr, size_t len, size_t period_len,
  912. enum dma_transfer_direction dir, unsigned long flags)
  913. {
  914. struct pxad_chan *chan = to_pxad_chan(dchan);
  915. struct pxad_desc_sw *sw_desc;
  916. struct pxad_desc_hw **phw_desc;
  917. dma_addr_t dma;
  918. u32 dcmd, dsadr = 0, dtadr = 0;
  919. unsigned int nb_desc = 0;
  920. if (!dchan || !len || !period_len)
  921. return NULL;
  922. if ((dir != DMA_DEV_TO_MEM) && (dir != DMA_MEM_TO_DEV)) {
  923. dev_err(&chan->vc.chan.dev->device,
  924. "Unsupported direction for cyclic DMA\n");
  925. return NULL;
  926. }
  927. /* the buffer length must be a multiple of period_len */
  928. if (len % period_len != 0 || period_len > PDMA_MAX_DESC_BYTES ||
  929. !IS_ALIGNED(period_len, 1 << PDMA_ALIGNMENT))
  930. return NULL;
  931. pxad_get_config(chan, dir, &dcmd, &dsadr, &dtadr);
  932. dcmd |= PXA_DCMD_ENDIRQEN | (PXA_DCMD_LENGTH & period_len);
  933. dev_dbg(&chan->vc.chan.dev->device,
  934. "%s(): buf_addr=0x%lx len=%zu period=%zu dir=%d flags=%lx\n",
  935. __func__, (unsigned long)buf_addr, len, period_len, dir, flags);
  936. nb_desc = DIV_ROUND_UP(period_len, PDMA_MAX_DESC_BYTES);
  937. nb_desc *= DIV_ROUND_UP(len, period_len);
  938. sw_desc = pxad_alloc_desc(chan, nb_desc + 1);
  939. if (!sw_desc)
  940. return NULL;
  941. sw_desc->cyclic = true;
  942. sw_desc->len = len;
  943. phw_desc = sw_desc->hw_desc;
  944. dma = buf_addr;
  945. do {
  946. phw_desc[0]->dsadr = dsadr ? dsadr : dma;
  947. phw_desc[0]->dtadr = dtadr ? dtadr : dma;
  948. phw_desc[0]->dcmd = dcmd;
  949. phw_desc++;
  950. dma += period_len;
  951. len -= period_len;
  952. } while (len);
  953. set_updater_desc(sw_desc, flags);
  954. return pxad_tx_prep(&chan->vc, &sw_desc->vd, flags);
  955. }
  956. static int pxad_config(struct dma_chan *dchan,
  957. struct dma_slave_config *cfg)
  958. {
  959. struct pxad_chan *chan = to_pxad_chan(dchan);
  960. if (!dchan)
  961. return -EINVAL;
  962. chan->cfg = *cfg;
  963. return 0;
  964. }
  965. static int pxad_terminate_all(struct dma_chan *dchan)
  966. {
  967. struct pxad_chan *chan = to_pxad_chan(dchan);
  968. struct pxad_device *pdev = to_pxad_dev(chan->vc.chan.device);
  969. struct virt_dma_desc *vd = NULL;
  970. unsigned long flags;
  971. struct pxad_phy *phy;
  972. LIST_HEAD(head);
  973. dev_dbg(&chan->vc.chan.dev->device,
  974. "%s(): vchan %p: terminate all\n", __func__, &chan->vc);
  975. spin_lock_irqsave(&chan->vc.lock, flags);
  976. vchan_get_all_descriptors(&chan->vc, &head);
  977. list_for_each_entry(vd, &head, node) {
  978. dev_dbg(&chan->vc.chan.dev->device,
  979. "%s(): cancelling txd %p[%x] (completed=%d)", __func__,
  980. vd, vd->tx.cookie, is_desc_completed(vd));
  981. }
  982. phy = chan->phy;
  983. if (phy) {
  984. phy_disable(chan->phy);
  985. pxad_free_phy(chan);
  986. chan->phy = NULL;
  987. spin_lock(&pdev->phy_lock);
  988. phy->vchan = NULL;
  989. spin_unlock(&pdev->phy_lock);
  990. }
  991. spin_unlock_irqrestore(&chan->vc.lock, flags);
  992. vchan_dma_desc_free_list(&chan->vc, &head);
  993. return 0;
  994. }
  995. static unsigned int pxad_residue(struct pxad_chan *chan,
  996. dma_cookie_t cookie)
  997. {
  998. struct virt_dma_desc *vd = NULL;
  999. struct pxad_desc_sw *sw_desc = NULL;
  1000. struct pxad_desc_hw *hw_desc = NULL;
  1001. u32 curr, start, len, end, residue = 0;
  1002. unsigned long flags;
  1003. bool passed = false;
  1004. int i;
  1005. /*
  1006. * If the channel does not have a phy pointer anymore, it has already
  1007. * been completed. Therefore, its residue is 0.
  1008. */
  1009. if (!chan->phy)
  1010. return 0;
  1011. spin_lock_irqsave(&chan->vc.lock, flags);
  1012. vd = vchan_find_desc(&chan->vc, cookie);
  1013. if (!vd)
  1014. goto out;
  1015. sw_desc = to_pxad_sw_desc(vd);
  1016. if (sw_desc->hw_desc[0]->dcmd & PXA_DCMD_INCSRCADDR)
  1017. curr = phy_readl_relaxed(chan->phy, DSADR);
  1018. else
  1019. curr = phy_readl_relaxed(chan->phy, DTADR);
  1020. /*
  1021. * curr has to be actually read before checking descriptor
  1022. * completion, so that a curr inside a status updater
  1023. * descriptor implies the following test returns true, and
  1024. * preventing reordering of curr load and the test.
  1025. */
  1026. rmb();
  1027. if (is_desc_completed(vd))
  1028. goto out;
  1029. for (i = 0; i < sw_desc->nb_desc - 1; i++) {
  1030. hw_desc = sw_desc->hw_desc[i];
  1031. if (sw_desc->hw_desc[0]->dcmd & PXA_DCMD_INCSRCADDR)
  1032. start = hw_desc->dsadr;
  1033. else
  1034. start = hw_desc->dtadr;
  1035. len = hw_desc->dcmd & PXA_DCMD_LENGTH;
  1036. end = start + len;
  1037. /*
  1038. * 'passed' will be latched once we found the descriptor
  1039. * which lies inside the boundaries of the curr
  1040. * pointer. All descriptors that occur in the list
  1041. * _after_ we found that partially handled descriptor
  1042. * are still to be processed and are hence added to the
  1043. * residual bytes counter.
  1044. */
  1045. if (passed) {
  1046. residue += len;
  1047. } else if (curr >= start && curr <= end) {
  1048. residue += end - curr;
  1049. passed = true;
  1050. }
  1051. }
  1052. if (!passed)
  1053. residue = sw_desc->len;
  1054. out:
  1055. spin_unlock_irqrestore(&chan->vc.lock, flags);
  1056. dev_dbg(&chan->vc.chan.dev->device,
  1057. "%s(): txd %p[%x] sw_desc=%p: %d\n",
  1058. __func__, vd, cookie, sw_desc, residue);
  1059. return residue;
  1060. }
  1061. static enum dma_status pxad_tx_status(struct dma_chan *dchan,
  1062. dma_cookie_t cookie,
  1063. struct dma_tx_state *txstate)
  1064. {
  1065. struct pxad_chan *chan = to_pxad_chan(dchan);
  1066. enum dma_status ret;
  1067. if (cookie == chan->bus_error)
  1068. return DMA_ERROR;
  1069. ret = dma_cookie_status(dchan, cookie, txstate);
  1070. if (likely(txstate && (ret != DMA_ERROR)))
  1071. dma_set_residue(txstate, pxad_residue(chan, cookie));
  1072. return ret;
  1073. }
  1074. static void pxad_synchronize(struct dma_chan *dchan)
  1075. {
  1076. struct pxad_chan *chan = to_pxad_chan(dchan);
  1077. wait_event(chan->wq_state, !is_chan_running(chan));
  1078. vchan_synchronize(&chan->vc);
  1079. }
  1080. static void pxad_free_channels(struct dma_device *dmadev)
  1081. {
  1082. struct pxad_chan *c, *cn;
  1083. list_for_each_entry_safe(c, cn, &dmadev->channels,
  1084. vc.chan.device_node) {
  1085. list_del(&c->vc.chan.device_node);
  1086. tasklet_kill(&c->vc.task);
  1087. }
  1088. }
  1089. static int pxad_remove(struct platform_device *op)
  1090. {
  1091. struct pxad_device *pdev = platform_get_drvdata(op);
  1092. pxad_cleanup_debugfs(pdev);
  1093. pxad_free_channels(&pdev->slave);
  1094. dma_async_device_unregister(&pdev->slave);
  1095. return 0;
  1096. }
  1097. static int pxad_init_phys(struct platform_device *op,
  1098. struct pxad_device *pdev,
  1099. unsigned int nb_phy_chans)
  1100. {
  1101. int irq0, irq, nr_irq = 0, i, ret;
  1102. struct pxad_phy *phy;
  1103. irq0 = platform_get_irq(op, 0);
  1104. if (irq0 < 0)
  1105. return irq0;
  1106. pdev->phys = devm_kcalloc(&op->dev, nb_phy_chans,
  1107. sizeof(pdev->phys[0]), GFP_KERNEL);
  1108. if (!pdev->phys)
  1109. return -ENOMEM;
  1110. for (i = 0; i < nb_phy_chans; i++)
  1111. if (platform_get_irq(op, i) > 0)
  1112. nr_irq++;
  1113. for (i = 0; i < nb_phy_chans; i++) {
  1114. phy = &pdev->phys[i];
  1115. phy->base = pdev->base;
  1116. phy->idx = i;
  1117. irq = platform_get_irq(op, i);
  1118. if ((nr_irq > 1) && (irq > 0))
  1119. ret = devm_request_irq(&op->dev, irq,
  1120. pxad_chan_handler,
  1121. IRQF_SHARED, "pxa-dma", phy);
  1122. if ((nr_irq == 1) && (i == 0))
  1123. ret = devm_request_irq(&op->dev, irq0,
  1124. pxad_int_handler,
  1125. IRQF_SHARED, "pxa-dma", pdev);
  1126. if (ret) {
  1127. dev_err(pdev->slave.dev,
  1128. "%s(): can't request irq %d:%d\n", __func__,
  1129. irq, ret);
  1130. return ret;
  1131. }
  1132. }
  1133. return 0;
  1134. }
  1135. static const struct of_device_id pxad_dt_ids[] = {
  1136. { .compatible = "marvell,pdma-1.0", },
  1137. {}
  1138. };
  1139. MODULE_DEVICE_TABLE(of, pxad_dt_ids);
  1140. static struct dma_chan *pxad_dma_xlate(struct of_phandle_args *dma_spec,
  1141. struct of_dma *ofdma)
  1142. {
  1143. struct pxad_device *d = ofdma->of_dma_data;
  1144. struct dma_chan *chan;
  1145. chan = dma_get_any_slave_channel(&d->slave);
  1146. if (!chan)
  1147. return NULL;
  1148. to_pxad_chan(chan)->drcmr = dma_spec->args[0];
  1149. to_pxad_chan(chan)->prio = dma_spec->args[1];
  1150. return chan;
  1151. }
  1152. static int pxad_init_dmadev(struct platform_device *op,
  1153. struct pxad_device *pdev,
  1154. unsigned int nr_phy_chans,
  1155. unsigned int nr_requestors)
  1156. {
  1157. int ret;
  1158. unsigned int i;
  1159. struct pxad_chan *c;
  1160. pdev->nr_chans = nr_phy_chans;
  1161. pdev->nr_requestors = nr_requestors;
  1162. INIT_LIST_HEAD(&pdev->slave.channels);
  1163. pdev->slave.device_alloc_chan_resources = pxad_alloc_chan_resources;
  1164. pdev->slave.device_free_chan_resources = pxad_free_chan_resources;
  1165. pdev->slave.device_tx_status = pxad_tx_status;
  1166. pdev->slave.device_issue_pending = pxad_issue_pending;
  1167. pdev->slave.device_config = pxad_config;
  1168. pdev->slave.device_synchronize = pxad_synchronize;
  1169. pdev->slave.device_terminate_all = pxad_terminate_all;
  1170. if (op->dev.coherent_dma_mask)
  1171. dma_set_mask(&op->dev, op->dev.coherent_dma_mask);
  1172. else
  1173. dma_set_mask(&op->dev, DMA_BIT_MASK(32));
  1174. ret = pxad_init_phys(op, pdev, nr_phy_chans);
  1175. if (ret)
  1176. return ret;
  1177. for (i = 0; i < nr_phy_chans; i++) {
  1178. c = devm_kzalloc(&op->dev, sizeof(*c), GFP_KERNEL);
  1179. if (!c)
  1180. return -ENOMEM;
  1181. c->vc.desc_free = pxad_free_desc;
  1182. vchan_init(&c->vc, &pdev->slave);
  1183. init_waitqueue_head(&c->wq_state);
  1184. }
  1185. return dma_async_device_register(&pdev->slave);
  1186. }
  1187. static int pxad_probe(struct platform_device *op)
  1188. {
  1189. struct pxad_device *pdev;
  1190. const struct of_device_id *of_id;
  1191. struct mmp_dma_platdata *pdata = dev_get_platdata(&op->dev);
  1192. struct resource *iores;
  1193. int ret, dma_channels = 0, nb_requestors = 0;
  1194. const enum dma_slave_buswidth widths =
  1195. DMA_SLAVE_BUSWIDTH_1_BYTE | DMA_SLAVE_BUSWIDTH_2_BYTES |
  1196. DMA_SLAVE_BUSWIDTH_4_BYTES;
  1197. pdev = devm_kzalloc(&op->dev, sizeof(*pdev), GFP_KERNEL);
  1198. if (!pdev)
  1199. return -ENOMEM;
  1200. spin_lock_init(&pdev->phy_lock);
  1201. iores = platform_get_resource(op, IORESOURCE_MEM, 0);
  1202. pdev->base = devm_ioremap_resource(&op->dev, iores);
  1203. if (IS_ERR(pdev->base))
  1204. return PTR_ERR(pdev->base);
  1205. of_id = of_match_device(pxad_dt_ids, &op->dev);
  1206. if (of_id) {
  1207. of_property_read_u32(op->dev.of_node, "#dma-channels",
  1208. &dma_channels);
  1209. ret = of_property_read_u32(op->dev.of_node, "#dma-requests",
  1210. &nb_requestors);
  1211. if (ret) {
  1212. dev_warn(pdev->slave.dev,
  1213. "#dma-requests set to default 32 as missing in OF: %d",
  1214. ret);
  1215. nb_requestors = 32;
  1216. };
  1217. } else if (pdata && pdata->dma_channels) {
  1218. dma_channels = pdata->dma_channels;
  1219. nb_requestors = pdata->nb_requestors;
  1220. } else {
  1221. dma_channels = 32; /* default 32 channel */
  1222. }
  1223. dma_cap_set(DMA_SLAVE, pdev->slave.cap_mask);
  1224. dma_cap_set(DMA_MEMCPY, pdev->slave.cap_mask);
  1225. dma_cap_set(DMA_CYCLIC, pdev->slave.cap_mask);
  1226. dma_cap_set(DMA_PRIVATE, pdev->slave.cap_mask);
  1227. pdev->slave.device_prep_dma_memcpy = pxad_prep_memcpy;
  1228. pdev->slave.device_prep_slave_sg = pxad_prep_slave_sg;
  1229. pdev->slave.device_prep_dma_cyclic = pxad_prep_dma_cyclic;
  1230. pdev->slave.copy_align = PDMA_ALIGNMENT;
  1231. pdev->slave.src_addr_widths = widths;
  1232. pdev->slave.dst_addr_widths = widths;
  1233. pdev->slave.directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM);
  1234. pdev->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  1235. pdev->slave.descriptor_reuse = true;
  1236. pdev->slave.dev = &op->dev;
  1237. ret = pxad_init_dmadev(op, pdev, dma_channels, nb_requestors);
  1238. if (ret) {
  1239. dev_err(pdev->slave.dev, "unable to register\n");
  1240. return ret;
  1241. }
  1242. if (op->dev.of_node) {
  1243. /* Device-tree DMA controller registration */
  1244. ret = of_dma_controller_register(op->dev.of_node,
  1245. pxad_dma_xlate, pdev);
  1246. if (ret < 0) {
  1247. dev_err(pdev->slave.dev,
  1248. "of_dma_controller_register failed\n");
  1249. return ret;
  1250. }
  1251. }
  1252. platform_set_drvdata(op, pdev);
  1253. pxad_init_debugfs(pdev);
  1254. dev_info(pdev->slave.dev, "initialized %d channels on %d requestors\n",
  1255. dma_channels, nb_requestors);
  1256. return 0;
  1257. }
  1258. static const struct platform_device_id pxad_id_table[] = {
  1259. { "pxa-dma", },
  1260. { },
  1261. };
  1262. static struct platform_driver pxad_driver = {
  1263. .driver = {
  1264. .name = "pxa-dma",
  1265. .of_match_table = pxad_dt_ids,
  1266. },
  1267. .id_table = pxad_id_table,
  1268. .probe = pxad_probe,
  1269. .remove = pxad_remove,
  1270. };
  1271. bool pxad_filter_fn(struct dma_chan *chan, void *param)
  1272. {
  1273. struct pxad_chan *c = to_pxad_chan(chan);
  1274. struct pxad_param *p = param;
  1275. if (chan->device->dev->driver != &pxad_driver.driver)
  1276. return false;
  1277. c->drcmr = p->drcmr;
  1278. c->prio = p->prio;
  1279. return true;
  1280. }
  1281. EXPORT_SYMBOL_GPL(pxad_filter_fn);
  1282. module_platform_driver(pxad_driver);
  1283. MODULE_DESCRIPTION("Marvell PXA Peripheral DMA Driver");
  1284. MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
  1285. MODULE_LICENSE("GPL v2");