pxa_dma.c 40 KB

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