pxa_dma.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2015 Robert Jarzmik <robert.jarzmik@free.fr>
  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.h>
  18. #include <linux/of_dma.h>
  19. #include <linux/wait.h>
  20. #include <linux/dma/pxa-dma.h>
  21. #include "dmaengine.h"
  22. #include "virt-dma.h"
  23. #define DCSR(n) (0x0000 + ((n) << 2))
  24. #define DALGN(n) 0x00a0
  25. #define DINT 0x00f0
  26. #define DDADR(n) (0x0200 + ((n) << 4))
  27. #define DSADR(n) (0x0204 + ((n) << 4))
  28. #define DTADR(n) (0x0208 + ((n) << 4))
  29. #define DCMD(n) (0x020c + ((n) << 4))
  30. #define PXA_DCSR_RUN BIT(31) /* Run Bit (read / write) */
  31. #define PXA_DCSR_NODESC BIT(30) /* No-Descriptor Fetch (read / write) */
  32. #define PXA_DCSR_STOPIRQEN BIT(29) /* Stop Interrupt Enable (R/W) */
  33. #define PXA_DCSR_REQPEND BIT(8) /* Request Pending (read-only) */
  34. #define PXA_DCSR_STOPSTATE BIT(3) /* Stop State (read-only) */
  35. #define PXA_DCSR_ENDINTR BIT(2) /* End Interrupt (read / write) */
  36. #define PXA_DCSR_STARTINTR BIT(1) /* Start Interrupt (read / write) */
  37. #define PXA_DCSR_BUSERR BIT(0) /* Bus Error Interrupt (read / write) */
  38. #define PXA_DCSR_EORIRQEN BIT(28) /* End of Receive IRQ Enable (R/W) */
  39. #define PXA_DCSR_EORJMPEN BIT(27) /* Jump to next descriptor on EOR */
  40. #define PXA_DCSR_EORSTOPEN BIT(26) /* STOP on an EOR */
  41. #define PXA_DCSR_SETCMPST BIT(25) /* Set Descriptor Compare Status */
  42. #define PXA_DCSR_CLRCMPST BIT(24) /* Clear Descriptor Compare Status */
  43. #define PXA_DCSR_CMPST BIT(10) /* The Descriptor Compare Status */
  44. #define PXA_DCSR_EORINTR BIT(9) /* The end of Receive */
  45. #define DRCMR_MAPVLD BIT(7) /* Map Valid (read / write) */
  46. #define DRCMR_CHLNUM 0x1f /* mask for Channel Number (read / write) */
  47. #define DDADR_DESCADDR 0xfffffff0 /* Address of next descriptor (mask) */
  48. #define DDADR_STOP BIT(0) /* Stop (read / write) */
  49. #define PXA_DCMD_INCSRCADDR BIT(31) /* Source Address Increment Setting. */
  50. #define PXA_DCMD_INCTRGADDR BIT(30) /* Target Address Increment Setting. */
  51. #define PXA_DCMD_FLOWSRC BIT(29) /* Flow Control by the source. */
  52. #define PXA_DCMD_FLOWTRG BIT(28) /* Flow Control by the target. */
  53. #define PXA_DCMD_STARTIRQEN BIT(22) /* Start Interrupt Enable */
  54. #define PXA_DCMD_ENDIRQEN BIT(21) /* End Interrupt Enable */
  55. #define PXA_DCMD_ENDIAN BIT(18) /* Device Endian-ness. */
  56. #define PXA_DCMD_BURST8 (1 << 16) /* 8 byte burst */
  57. #define PXA_DCMD_BURST16 (2 << 16) /* 16 byte burst */
  58. #define PXA_DCMD_BURST32 (3 << 16) /* 32 byte burst */
  59. #define PXA_DCMD_WIDTH1 (1 << 14) /* 1 byte width */
  60. #define PXA_DCMD_WIDTH2 (2 << 14) /* 2 byte width (HalfWord) */
  61. #define PXA_DCMD_WIDTH4 (3 << 14) /* 4 byte width (Word) */
  62. #define PXA_DCMD_LENGTH 0x01fff /* length mask (max = 8K - 1) */
  63. #define PDMA_ALIGNMENT 3
  64. #define PDMA_MAX_DESC_BYTES (PXA_DCMD_LENGTH & ~((1 << PDMA_ALIGNMENT) - 1))
  65. struct pxad_desc_hw {
  66. u32 ddadr; /* Points to the next descriptor + flags */
  67. u32 dsadr; /* DSADR value for the current transfer */
  68. u32 dtadr; /* DTADR value for the current transfer */
  69. u32 dcmd; /* DCMD value for the current transfer */
  70. } __aligned(16);
  71. struct pxad_desc_sw {
  72. struct virt_dma_desc vd; /* Virtual descriptor */
  73. int nb_desc; /* Number of hw. descriptors */
  74. size_t len; /* Number of bytes xfered */
  75. dma_addr_t first; /* First descriptor's addr */
  76. /* At least one descriptor has an src/dst address not multiple of 8 */
  77. bool misaligned;
  78. bool cyclic;
  79. struct dma_pool *desc_pool; /* Channel's used allocator */
  80. struct pxad_desc_hw *hw_desc[] __counted_by(nb_desc);
  81. /* 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. void *desc;
  629. int i;
  630. sw_desc = kzalloc(struct_size(sw_desc, hw_desc, nb_hw_desc),
  631. GFP_NOWAIT);
  632. if (!sw_desc)
  633. return NULL;
  634. sw_desc->desc_pool = chan->desc_pool;
  635. for (i = 0; i < nb_hw_desc; i++) {
  636. desc = dma_pool_alloc(sw_desc->desc_pool, GFP_NOWAIT, &dma);
  637. if (!desc) {
  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. sw_desc->nb_desc++;
  644. sw_desc->hw_desc[i] = desc;
  645. if (i == 0)
  646. sw_desc->first = dma;
  647. else
  648. sw_desc->hw_desc[i - 1]->ddadr = dma;
  649. }
  650. return sw_desc;
  651. err:
  652. pxad_free_desc(&sw_desc->vd);
  653. return NULL;
  654. }
  655. static dma_cookie_t pxad_tx_submit(struct dma_async_tx_descriptor *tx)
  656. {
  657. struct virt_dma_chan *vc = to_virt_chan(tx->chan);
  658. struct pxad_chan *chan = to_pxad_chan(&vc->chan);
  659. struct virt_dma_desc *vd_chained = NULL,
  660. *vd = container_of(tx, struct virt_dma_desc, tx);
  661. dma_cookie_t cookie;
  662. unsigned long flags;
  663. set_updater_desc(to_pxad_sw_desc(vd), tx->flags);
  664. spin_lock_irqsave(&vc->lock, flags);
  665. cookie = dma_cookie_assign(tx);
  666. if (list_empty(&vc->desc_submitted) && pxad_try_hotchain(vc, vd)) {
  667. list_move_tail(&vd->node, &vc->desc_issued);
  668. dev_dbg(&chan->vc.chan.dev->device,
  669. "%s(): txd %p[%x]: submitted (hot linked)\n",
  670. __func__, vd, cookie);
  671. goto out;
  672. }
  673. /*
  674. * Fallback to placing the tx in the submitted queue
  675. */
  676. if (!list_empty(&vc->desc_submitted)) {
  677. vd_chained = list_entry(vc->desc_submitted.prev,
  678. struct virt_dma_desc, node);
  679. /*
  680. * Only chain the descriptors if no new misalignment is
  681. * introduced. If a new misalignment is chained, let the channel
  682. * stop, and be relaunched in misalign mode from the irq
  683. * handler.
  684. */
  685. if (chan->misaligned || !to_pxad_sw_desc(vd)->misaligned)
  686. pxad_desc_chain(vd_chained, vd);
  687. else
  688. vd_chained = NULL;
  689. }
  690. dev_dbg(&chan->vc.chan.dev->device,
  691. "%s(): txd %p[%x]: submitted (%s linked)\n",
  692. __func__, vd, cookie, vd_chained ? "cold" : "not");
  693. list_move_tail(&vd->node, &vc->desc_submitted);
  694. chan->misaligned |= to_pxad_sw_desc(vd)->misaligned;
  695. out:
  696. spin_unlock_irqrestore(&vc->lock, flags);
  697. return cookie;
  698. }
  699. static void pxad_issue_pending(struct dma_chan *dchan)
  700. {
  701. struct pxad_chan *chan = to_pxad_chan(dchan);
  702. struct virt_dma_desc *vd_first;
  703. unsigned long flags;
  704. spin_lock_irqsave(&chan->vc.lock, flags);
  705. if (list_empty(&chan->vc.desc_submitted))
  706. goto out;
  707. vd_first = list_first_entry(&chan->vc.desc_submitted,
  708. struct virt_dma_desc, node);
  709. dev_dbg(&chan->vc.chan.dev->device,
  710. "%s(): txd %p[%x]", __func__, vd_first, vd_first->tx.cookie);
  711. vchan_issue_pending(&chan->vc);
  712. if (!pxad_try_hotchain(&chan->vc, vd_first))
  713. pxad_launch_chan(chan, to_pxad_sw_desc(vd_first));
  714. out:
  715. spin_unlock_irqrestore(&chan->vc.lock, flags);
  716. }
  717. static inline struct dma_async_tx_descriptor *
  718. pxad_tx_prep(struct virt_dma_chan *vc, struct virt_dma_desc *vd,
  719. unsigned long tx_flags)
  720. {
  721. struct dma_async_tx_descriptor *tx;
  722. struct pxad_chan *chan = container_of(vc, struct pxad_chan, vc);
  723. INIT_LIST_HEAD(&vd->node);
  724. tx = vchan_tx_prep(vc, vd, tx_flags);
  725. tx->tx_submit = pxad_tx_submit;
  726. dev_dbg(&chan->vc.chan.dev->device,
  727. "%s(): vc=%p txd=%p[%x] flags=0x%lx\n", __func__,
  728. vc, vd, vd->tx.cookie,
  729. tx_flags);
  730. return tx;
  731. }
  732. static void pxad_get_config(struct pxad_chan *chan,
  733. enum dma_transfer_direction dir,
  734. u32 *dcmd, u32 *dev_src, u32 *dev_dst)
  735. {
  736. u32 maxburst = 0, dev_addr = 0;
  737. enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  738. struct pxad_device *pdev = to_pxad_dev(chan->vc.chan.device);
  739. *dcmd = 0;
  740. if (dir == DMA_DEV_TO_MEM) {
  741. maxburst = chan->cfg.src_maxburst;
  742. width = chan->cfg.src_addr_width;
  743. dev_addr = chan->cfg.src_addr;
  744. *dev_src = dev_addr;
  745. *dcmd |= PXA_DCMD_INCTRGADDR;
  746. if (chan->drcmr <= pdev->nr_requestors)
  747. *dcmd |= PXA_DCMD_FLOWSRC;
  748. }
  749. if (dir == DMA_MEM_TO_DEV) {
  750. maxburst = chan->cfg.dst_maxburst;
  751. width = chan->cfg.dst_addr_width;
  752. dev_addr = chan->cfg.dst_addr;
  753. *dev_dst = dev_addr;
  754. *dcmd |= PXA_DCMD_INCSRCADDR;
  755. if (chan->drcmr <= pdev->nr_requestors)
  756. *dcmd |= PXA_DCMD_FLOWTRG;
  757. }
  758. if (dir == DMA_MEM_TO_MEM)
  759. *dcmd |= PXA_DCMD_BURST32 | PXA_DCMD_INCTRGADDR |
  760. PXA_DCMD_INCSRCADDR;
  761. dev_dbg(&chan->vc.chan.dev->device,
  762. "%s(): dev_addr=0x%x maxburst=%d width=%d dir=%d\n",
  763. __func__, dev_addr, maxburst, width, dir);
  764. if (width == DMA_SLAVE_BUSWIDTH_1_BYTE)
  765. *dcmd |= PXA_DCMD_WIDTH1;
  766. else if (width == DMA_SLAVE_BUSWIDTH_2_BYTES)
  767. *dcmd |= PXA_DCMD_WIDTH2;
  768. else if (width == DMA_SLAVE_BUSWIDTH_4_BYTES)
  769. *dcmd |= PXA_DCMD_WIDTH4;
  770. if (maxburst == 8)
  771. *dcmd |= PXA_DCMD_BURST8;
  772. else if (maxburst == 16)
  773. *dcmd |= PXA_DCMD_BURST16;
  774. else if (maxburst == 32)
  775. *dcmd |= PXA_DCMD_BURST32;
  776. }
  777. static struct dma_async_tx_descriptor *
  778. pxad_prep_memcpy(struct dma_chan *dchan,
  779. dma_addr_t dma_dst, dma_addr_t dma_src,
  780. size_t len, unsigned long flags)
  781. {
  782. struct pxad_chan *chan = to_pxad_chan(dchan);
  783. struct pxad_desc_sw *sw_desc;
  784. struct pxad_desc_hw *hw_desc;
  785. u32 dcmd;
  786. unsigned int i, nb_desc = 0;
  787. size_t copy;
  788. if (!dchan || !len)
  789. return NULL;
  790. dev_dbg(&chan->vc.chan.dev->device,
  791. "%s(): dma_dst=0x%lx dma_src=0x%lx len=%zu flags=%lx\n",
  792. __func__, (unsigned long)dma_dst, (unsigned long)dma_src,
  793. len, flags);
  794. pxad_get_config(chan, DMA_MEM_TO_MEM, &dcmd, NULL, NULL);
  795. nb_desc = DIV_ROUND_UP(len, PDMA_MAX_DESC_BYTES);
  796. sw_desc = pxad_alloc_desc(chan, nb_desc + 1);
  797. if (!sw_desc)
  798. return NULL;
  799. sw_desc->len = len;
  800. if (!IS_ALIGNED(dma_src, 1 << PDMA_ALIGNMENT) ||
  801. !IS_ALIGNED(dma_dst, 1 << PDMA_ALIGNMENT))
  802. sw_desc->misaligned = true;
  803. i = 0;
  804. do {
  805. hw_desc = sw_desc->hw_desc[i++];
  806. copy = min_t(size_t, len, PDMA_MAX_DESC_BYTES);
  807. hw_desc->dcmd = dcmd | (PXA_DCMD_LENGTH & copy);
  808. hw_desc->dsadr = dma_src;
  809. hw_desc->dtadr = dma_dst;
  810. len -= copy;
  811. dma_src += copy;
  812. dma_dst += copy;
  813. } while (len);
  814. set_updater_desc(sw_desc, flags);
  815. return pxad_tx_prep(&chan->vc, &sw_desc->vd, flags);
  816. }
  817. static struct dma_async_tx_descriptor *
  818. pxad_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
  819. unsigned int sg_len, enum dma_transfer_direction dir,
  820. unsigned long flags, void *context)
  821. {
  822. struct pxad_chan *chan = to_pxad_chan(dchan);
  823. struct pxad_desc_sw *sw_desc;
  824. size_t len, avail;
  825. struct scatterlist *sg;
  826. dma_addr_t dma;
  827. u32 dcmd, dsadr = 0, dtadr = 0;
  828. unsigned int nb_desc = 0, i, j = 0;
  829. if ((sgl == NULL) || (sg_len == 0))
  830. return NULL;
  831. pxad_get_config(chan, dir, &dcmd, &dsadr, &dtadr);
  832. dev_dbg(&chan->vc.chan.dev->device,
  833. "%s(): dir=%d flags=%lx\n", __func__, dir, flags);
  834. for_each_sg(sgl, sg, sg_len, i)
  835. nb_desc += DIV_ROUND_UP(sg_dma_len(sg), PDMA_MAX_DESC_BYTES);
  836. sw_desc = pxad_alloc_desc(chan, nb_desc + 1);
  837. if (!sw_desc)
  838. return NULL;
  839. for_each_sg(sgl, sg, sg_len, i) {
  840. dma = sg_dma_address(sg);
  841. avail = sg_dma_len(sg);
  842. sw_desc->len += avail;
  843. do {
  844. len = min_t(size_t, avail, PDMA_MAX_DESC_BYTES);
  845. if (dma & 0x7)
  846. sw_desc->misaligned = true;
  847. sw_desc->hw_desc[j]->dcmd =
  848. dcmd | (PXA_DCMD_LENGTH & len);
  849. sw_desc->hw_desc[j]->dsadr = dsadr ? dsadr : dma;
  850. sw_desc->hw_desc[j++]->dtadr = dtadr ? dtadr : dma;
  851. dma += len;
  852. avail -= len;
  853. } while (avail);
  854. }
  855. set_updater_desc(sw_desc, flags);
  856. return pxad_tx_prep(&chan->vc, &sw_desc->vd, flags);
  857. }
  858. static struct dma_async_tx_descriptor *
  859. pxad_prep_dma_cyclic(struct dma_chan *dchan,
  860. dma_addr_t buf_addr, size_t len, size_t period_len,
  861. enum dma_transfer_direction dir, unsigned long flags)
  862. {
  863. struct pxad_chan *chan = to_pxad_chan(dchan);
  864. struct pxad_desc_sw *sw_desc;
  865. struct pxad_desc_hw **phw_desc;
  866. dma_addr_t dma;
  867. u32 dcmd, dsadr = 0, dtadr = 0;
  868. unsigned int nb_desc = 0;
  869. if (!dchan || !len || !period_len)
  870. return NULL;
  871. if ((dir != DMA_DEV_TO_MEM) && (dir != DMA_MEM_TO_DEV)) {
  872. dev_err(&chan->vc.chan.dev->device,
  873. "Unsupported direction for cyclic DMA\n");
  874. return NULL;
  875. }
  876. /* the buffer length must be a multiple of period_len */
  877. if (len % period_len != 0 || period_len > PDMA_MAX_DESC_BYTES ||
  878. !IS_ALIGNED(period_len, 1 << PDMA_ALIGNMENT))
  879. return NULL;
  880. pxad_get_config(chan, dir, &dcmd, &dsadr, &dtadr);
  881. dcmd |= PXA_DCMD_ENDIRQEN | (PXA_DCMD_LENGTH & period_len);
  882. dev_dbg(&chan->vc.chan.dev->device,
  883. "%s(): buf_addr=0x%lx len=%zu period=%zu dir=%d flags=%lx\n",
  884. __func__, (unsigned long)buf_addr, len, period_len, dir, flags);
  885. nb_desc = DIV_ROUND_UP(period_len, PDMA_MAX_DESC_BYTES);
  886. nb_desc *= DIV_ROUND_UP(len, period_len);
  887. sw_desc = pxad_alloc_desc(chan, nb_desc + 1);
  888. if (!sw_desc)
  889. return NULL;
  890. sw_desc->cyclic = true;
  891. sw_desc->len = len;
  892. phw_desc = sw_desc->hw_desc;
  893. dma = buf_addr;
  894. do {
  895. phw_desc[0]->dsadr = dsadr ? dsadr : dma;
  896. phw_desc[0]->dtadr = dtadr ? dtadr : dma;
  897. phw_desc[0]->dcmd = dcmd;
  898. phw_desc++;
  899. dma += period_len;
  900. len -= period_len;
  901. } while (len);
  902. set_updater_desc(sw_desc, flags);
  903. return pxad_tx_prep(&chan->vc, &sw_desc->vd, flags);
  904. }
  905. static int pxad_config(struct dma_chan *dchan,
  906. struct dma_slave_config *cfg)
  907. {
  908. struct pxad_chan *chan = to_pxad_chan(dchan);
  909. if (!dchan)
  910. return -EINVAL;
  911. chan->cfg = *cfg;
  912. return 0;
  913. }
  914. static int pxad_terminate_all(struct dma_chan *dchan)
  915. {
  916. struct pxad_chan *chan = to_pxad_chan(dchan);
  917. struct pxad_device *pdev = to_pxad_dev(chan->vc.chan.device);
  918. struct virt_dma_desc *vd = NULL;
  919. unsigned long flags;
  920. struct pxad_phy *phy;
  921. LIST_HEAD(head);
  922. dev_dbg(&chan->vc.chan.dev->device,
  923. "%s(): vchan %p: terminate all\n", __func__, &chan->vc);
  924. spin_lock_irqsave(&chan->vc.lock, flags);
  925. vchan_get_all_descriptors(&chan->vc, &head);
  926. list_for_each_entry(vd, &head, node) {
  927. dev_dbg(&chan->vc.chan.dev->device,
  928. "%s(): cancelling txd %p[%x] (completed=%d)", __func__,
  929. vd, vd->tx.cookie, is_desc_completed(vd));
  930. }
  931. phy = chan->phy;
  932. if (phy) {
  933. phy_disable(chan->phy);
  934. pxad_free_phy(chan);
  935. chan->phy = NULL;
  936. spin_lock(&pdev->phy_lock);
  937. phy->vchan = NULL;
  938. spin_unlock(&pdev->phy_lock);
  939. }
  940. spin_unlock_irqrestore(&chan->vc.lock, flags);
  941. vchan_dma_desc_free_list(&chan->vc, &head);
  942. return 0;
  943. }
  944. static unsigned int pxad_residue(struct pxad_chan *chan,
  945. dma_cookie_t cookie)
  946. {
  947. struct virt_dma_desc *vd = NULL;
  948. struct pxad_desc_sw *sw_desc = NULL;
  949. struct pxad_desc_hw *hw_desc = NULL;
  950. u32 curr, start, len, end, residue = 0;
  951. unsigned long flags;
  952. bool passed = false;
  953. int i;
  954. /*
  955. * If the channel does not have a phy pointer anymore, it has already
  956. * been completed. Therefore, its residue is 0.
  957. */
  958. if (!chan->phy)
  959. return 0;
  960. spin_lock_irqsave(&chan->vc.lock, flags);
  961. vd = vchan_find_desc(&chan->vc, cookie);
  962. if (!vd)
  963. goto out;
  964. sw_desc = to_pxad_sw_desc(vd);
  965. if (sw_desc->hw_desc[0]->dcmd & PXA_DCMD_INCSRCADDR)
  966. curr = phy_readl_relaxed(chan->phy, DSADR);
  967. else
  968. curr = phy_readl_relaxed(chan->phy, DTADR);
  969. /*
  970. * curr has to be actually read before checking descriptor
  971. * completion, so that a curr inside a status updater
  972. * descriptor implies the following test returns true, and
  973. * preventing reordering of curr load and the test.
  974. */
  975. rmb();
  976. if (is_desc_completed(vd))
  977. goto out;
  978. for (i = 0; i < sw_desc->nb_desc - 1; i++) {
  979. hw_desc = sw_desc->hw_desc[i];
  980. if (sw_desc->hw_desc[0]->dcmd & PXA_DCMD_INCSRCADDR)
  981. start = hw_desc->dsadr;
  982. else
  983. start = hw_desc->dtadr;
  984. len = hw_desc->dcmd & PXA_DCMD_LENGTH;
  985. end = start + len;
  986. /*
  987. * 'passed' will be latched once we found the descriptor
  988. * which lies inside the boundaries of the curr
  989. * pointer. All descriptors that occur in the list
  990. * _after_ we found that partially handled descriptor
  991. * are still to be processed and are hence added to the
  992. * residual bytes counter.
  993. */
  994. if (passed) {
  995. residue += len;
  996. } else if (curr >= start && curr <= end) {
  997. residue += end - curr;
  998. passed = true;
  999. }
  1000. }
  1001. if (!passed)
  1002. residue = sw_desc->len;
  1003. out:
  1004. spin_unlock_irqrestore(&chan->vc.lock, flags);
  1005. dev_dbg(&chan->vc.chan.dev->device,
  1006. "%s(): txd %p[%x] sw_desc=%p: %d\n",
  1007. __func__, vd, cookie, sw_desc, residue);
  1008. return residue;
  1009. }
  1010. static enum dma_status pxad_tx_status(struct dma_chan *dchan,
  1011. dma_cookie_t cookie,
  1012. struct dma_tx_state *txstate)
  1013. {
  1014. struct pxad_chan *chan = to_pxad_chan(dchan);
  1015. enum dma_status ret;
  1016. if (cookie == chan->bus_error)
  1017. return DMA_ERROR;
  1018. ret = dma_cookie_status(dchan, cookie, txstate);
  1019. if (likely(txstate && (ret != DMA_ERROR)))
  1020. dma_set_residue(txstate, pxad_residue(chan, cookie));
  1021. return ret;
  1022. }
  1023. static void pxad_synchronize(struct dma_chan *dchan)
  1024. {
  1025. struct pxad_chan *chan = to_pxad_chan(dchan);
  1026. wait_event(chan->wq_state, !is_chan_running(chan));
  1027. vchan_synchronize(&chan->vc);
  1028. }
  1029. static void pxad_free_channels(struct dma_device *dmadev)
  1030. {
  1031. struct pxad_chan *c, *cn;
  1032. list_for_each_entry_safe(c, cn, &dmadev->channels,
  1033. vc.chan.device_node) {
  1034. list_del(&c->vc.chan.device_node);
  1035. tasklet_kill(&c->vc.task);
  1036. }
  1037. }
  1038. static void pxad_remove(struct platform_device *op)
  1039. {
  1040. struct pxad_device *pdev = platform_get_drvdata(op);
  1041. pxad_cleanup_debugfs(pdev);
  1042. pxad_free_channels(&pdev->slave);
  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 dma_slave_map *slave_map = NULL;
  1140. struct mmp_dma_platdata *pdata = dev_get_platdata(&op->dev);
  1141. int ret, dma_channels = 0, nb_requestors = 0, slave_map_cnt = 0;
  1142. const enum dma_slave_buswidth widths =
  1143. DMA_SLAVE_BUSWIDTH_1_BYTE | DMA_SLAVE_BUSWIDTH_2_BYTES |
  1144. DMA_SLAVE_BUSWIDTH_4_BYTES;
  1145. pdev = devm_kzalloc(&op->dev, sizeof(*pdev), GFP_KERNEL);
  1146. if (!pdev)
  1147. return -ENOMEM;
  1148. spin_lock_init(&pdev->phy_lock);
  1149. pdev->base = devm_platform_ioremap_resource(op, 0);
  1150. if (IS_ERR(pdev->base))
  1151. return PTR_ERR(pdev->base);
  1152. if (op->dev.of_node) {
  1153. /* Parse new and deprecated dma-channels properties */
  1154. if (of_property_read_u32(op->dev.of_node, "dma-channels",
  1155. &dma_channels))
  1156. of_property_read_u32(op->dev.of_node, "#dma-channels",
  1157. &dma_channels);
  1158. /* Parse new and deprecated dma-requests properties */
  1159. ret = of_property_read_u32(op->dev.of_node, "dma-requests",
  1160. &nb_requestors);
  1161. if (ret)
  1162. ret = of_property_read_u32(op->dev.of_node, "#dma-requests",
  1163. &nb_requestors);
  1164. if (ret) {
  1165. dev_warn(pdev->slave.dev,
  1166. "#dma-requests set to default 32 as missing in OF: %d",
  1167. ret);
  1168. nb_requestors = 32;
  1169. }
  1170. } else if (pdata && pdata->dma_channels) {
  1171. dma_channels = pdata->dma_channels;
  1172. nb_requestors = pdata->nb_requestors;
  1173. slave_map = pdata->slave_map;
  1174. slave_map_cnt = pdata->slave_map_cnt;
  1175. } else {
  1176. dma_channels = 32; /* default 32 channel */
  1177. }
  1178. dma_cap_set(DMA_SLAVE, pdev->slave.cap_mask);
  1179. dma_cap_set(DMA_MEMCPY, pdev->slave.cap_mask);
  1180. dma_cap_set(DMA_CYCLIC, pdev->slave.cap_mask);
  1181. dma_cap_set(DMA_PRIVATE, pdev->slave.cap_mask);
  1182. pdev->slave.device_prep_dma_memcpy = pxad_prep_memcpy;
  1183. pdev->slave.device_prep_slave_sg = pxad_prep_slave_sg;
  1184. pdev->slave.device_prep_dma_cyclic = pxad_prep_dma_cyclic;
  1185. pdev->slave.filter.map = slave_map;
  1186. pdev->slave.filter.mapcnt = slave_map_cnt;
  1187. pdev->slave.filter.fn = pxad_filter_fn;
  1188. pdev->slave.copy_align = PDMA_ALIGNMENT;
  1189. pdev->slave.src_addr_widths = widths;
  1190. pdev->slave.dst_addr_widths = widths;
  1191. pdev->slave.directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM);
  1192. pdev->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  1193. pdev->slave.descriptor_reuse = true;
  1194. pdev->slave.dev = &op->dev;
  1195. ret = pxad_init_dmadev(op, pdev, dma_channels, nb_requestors);
  1196. if (ret) {
  1197. dev_err(pdev->slave.dev, "unable to register\n");
  1198. return ret;
  1199. }
  1200. if (op->dev.of_node) {
  1201. /* Device-tree DMA controller registration */
  1202. ret = of_dma_controller_register(op->dev.of_node,
  1203. pxad_dma_xlate, pdev);
  1204. if (ret < 0) {
  1205. dev_err(pdev->slave.dev,
  1206. "of_dma_controller_register failed\n");
  1207. return ret;
  1208. }
  1209. }
  1210. platform_set_drvdata(op, pdev);
  1211. pxad_init_debugfs(pdev);
  1212. dev_info(pdev->slave.dev, "initialized %d channels on %d requestors\n",
  1213. dma_channels, nb_requestors);
  1214. return 0;
  1215. }
  1216. static const struct platform_device_id pxad_id_table[] = {
  1217. { "pxa-dma", },
  1218. { },
  1219. };
  1220. static struct platform_driver pxad_driver = {
  1221. .driver = {
  1222. .name = "pxa-dma",
  1223. .of_match_table = pxad_dt_ids,
  1224. },
  1225. .id_table = pxad_id_table,
  1226. .probe = pxad_probe,
  1227. .remove_new = pxad_remove,
  1228. };
  1229. static bool pxad_filter_fn(struct dma_chan *chan, void *param)
  1230. {
  1231. struct pxad_chan *c = to_pxad_chan(chan);
  1232. struct pxad_param *p = param;
  1233. if (chan->device->dev->driver != &pxad_driver.driver)
  1234. return false;
  1235. c->drcmr = p->drcmr;
  1236. c->prio = p->prio;
  1237. return true;
  1238. }
  1239. module_platform_driver(pxad_driver);
  1240. MODULE_DESCRIPTION("Marvell PXA Peripheral DMA Driver");
  1241. MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
  1242. MODULE_LICENSE("GPL v2");