pxa_dma.c 41 KB

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