pxa_dma.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  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. void __iomem *base;
  109. struct pxad_phy *phys;
  110. spinlock_t phy_lock; /* Phy association */
  111. #ifdef CONFIG_DEBUG_FS
  112. struct dentry *dbgfs_root;
  113. struct dentry *dbgfs_state;
  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. /*
  157. * Debug fs
  158. */
  159. #ifdef CONFIG_DEBUG_FS
  160. #include <linux/debugfs.h>
  161. #include <linux/uaccess.h>
  162. #include <linux/seq_file.h>
  163. static int dbg_show_requester_chan(struct seq_file *s, void *p)
  164. {
  165. int pos = 0;
  166. struct pxad_phy *phy = s->private;
  167. int i;
  168. u32 drcmr;
  169. pos += 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. pos += seq_printf(s, "\tRequester %d (MAPVLD=%d)\n", i,
  174. !!(drcmr & DRCMR_MAPVLD));
  175. }
  176. return pos;
  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. reg = pxad_drcmr(chan->drcmr);
  414. writel_relaxed(0, chan->phy->base + reg);
  415. spin_lock_irqsave(&pdev->phy_lock, flags);
  416. for (i = 0; i < 32; i++)
  417. if (chan->phy == &pdev->phys[i])
  418. legacy_unavailable &= ~BIT(i);
  419. chan->phy->vchan = NULL;
  420. chan->phy = NULL;
  421. spin_unlock_irqrestore(&pdev->phy_lock, flags);
  422. }
  423. static bool is_chan_running(struct pxad_chan *chan)
  424. {
  425. u32 dcsr;
  426. struct pxad_phy *phy = chan->phy;
  427. if (!phy)
  428. return false;
  429. dcsr = phy_readl_relaxed(phy, DCSR);
  430. return dcsr & PXA_DCSR_RUN;
  431. }
  432. static bool is_running_chan_misaligned(struct pxad_chan *chan)
  433. {
  434. u32 dalgn;
  435. BUG_ON(!chan->phy);
  436. dalgn = phy_readl_relaxed(chan->phy, DALGN);
  437. return dalgn & (BIT(chan->phy->idx));
  438. }
  439. static void phy_enable(struct pxad_phy *phy, bool misaligned)
  440. {
  441. u32 reg, dalgn;
  442. if (!phy->vchan)
  443. return;
  444. dev_dbg(&phy->vchan->vc.chan.dev->device,
  445. "%s(); phy=%p(%d) misaligned=%d\n", __func__,
  446. phy, phy->idx, misaligned);
  447. reg = pxad_drcmr(phy->vchan->drcmr);
  448. writel_relaxed(DRCMR_MAPVLD | phy->idx, phy->base + reg);
  449. dalgn = phy_readl_relaxed(phy, DALGN);
  450. if (misaligned)
  451. dalgn |= BIT(phy->idx);
  452. else
  453. dalgn &= ~BIT(phy->idx);
  454. phy_writel_relaxed(phy, dalgn, DALGN);
  455. phy_writel(phy, PXA_DCSR_STOPIRQEN | PXA_DCSR_ENDINTR |
  456. PXA_DCSR_BUSERR | PXA_DCSR_RUN, DCSR);
  457. }
  458. static void phy_disable(struct pxad_phy *phy)
  459. {
  460. u32 dcsr;
  461. if (!phy)
  462. return;
  463. dcsr = phy_readl_relaxed(phy, DCSR);
  464. dev_dbg(&phy->vchan->vc.chan.dev->device,
  465. "%s(): phy=%p(%d)\n", __func__, phy, phy->idx);
  466. phy_writel(phy, dcsr & ~PXA_DCSR_RUN & ~PXA_DCSR_STOPIRQEN, DCSR);
  467. }
  468. static void pxad_launch_chan(struct pxad_chan *chan,
  469. struct pxad_desc_sw *desc)
  470. {
  471. dev_dbg(&chan->vc.chan.dev->device,
  472. "%s(): desc=%p\n", __func__, desc);
  473. if (!chan->phy) {
  474. chan->phy = lookup_phy(chan);
  475. if (!chan->phy) {
  476. dev_dbg(&chan->vc.chan.dev->device,
  477. "%s(): no free dma channel\n", __func__);
  478. return;
  479. }
  480. }
  481. /*
  482. * Program the descriptor's address into the DMA controller,
  483. * then start the DMA transaction
  484. */
  485. phy_writel(chan->phy, desc->first, DDADR);
  486. phy_enable(chan->phy, chan->misaligned);
  487. }
  488. static void set_updater_desc(struct pxad_desc_sw *sw_desc,
  489. unsigned long flags)
  490. {
  491. struct pxad_desc_hw *updater =
  492. sw_desc->hw_desc[sw_desc->nb_desc - 1];
  493. dma_addr_t dma = sw_desc->hw_desc[sw_desc->nb_desc - 2]->ddadr;
  494. updater->ddadr = DDADR_STOP;
  495. updater->dsadr = dma;
  496. updater->dtadr = dma + 8;
  497. updater->dcmd = PXA_DCMD_WIDTH4 | PXA_DCMD_BURST32 |
  498. (PXA_DCMD_LENGTH & sizeof(u32));
  499. if (flags & DMA_PREP_INTERRUPT)
  500. updater->dcmd |= PXA_DCMD_ENDIRQEN;
  501. }
  502. static bool is_desc_completed(struct virt_dma_desc *vd)
  503. {
  504. struct pxad_desc_sw *sw_desc = to_pxad_sw_desc(vd);
  505. struct pxad_desc_hw *updater =
  506. sw_desc->hw_desc[sw_desc->nb_desc - 1];
  507. return updater->dtadr != (updater->dsadr + 8);
  508. }
  509. static void pxad_desc_chain(struct virt_dma_desc *vd1,
  510. struct virt_dma_desc *vd2)
  511. {
  512. struct pxad_desc_sw *desc1 = to_pxad_sw_desc(vd1);
  513. struct pxad_desc_sw *desc2 = to_pxad_sw_desc(vd2);
  514. dma_addr_t dma_to_chain;
  515. dma_to_chain = desc2->first;
  516. desc1->hw_desc[desc1->nb_desc - 1]->ddadr = dma_to_chain;
  517. }
  518. static bool pxad_try_hotchain(struct virt_dma_chan *vc,
  519. struct virt_dma_desc *vd)
  520. {
  521. struct virt_dma_desc *vd_last_issued = NULL;
  522. struct pxad_chan *chan = to_pxad_chan(&vc->chan);
  523. /*
  524. * Attempt to hot chain the tx if the phy is still running. This is
  525. * considered successful only if either the channel is still running
  526. * after the chaining, or if the chained transfer is completed after
  527. * having been hot chained.
  528. * A change of alignment is not allowed, and forbids hotchaining.
  529. */
  530. if (is_chan_running(chan)) {
  531. BUG_ON(list_empty(&vc->desc_issued));
  532. if (!is_running_chan_misaligned(chan) &&
  533. to_pxad_sw_desc(vd)->misaligned)
  534. return false;
  535. vd_last_issued = list_entry(vc->desc_issued.prev,
  536. struct virt_dma_desc, node);
  537. pxad_desc_chain(vd_last_issued, vd);
  538. if (is_chan_running(chan) || is_desc_completed(vd_last_issued))
  539. return true;
  540. }
  541. return false;
  542. }
  543. static unsigned int clear_chan_irq(struct pxad_phy *phy)
  544. {
  545. u32 dcsr;
  546. u32 dint = readl(phy->base + DINT);
  547. if (!(dint & BIT(phy->idx)))
  548. return PXA_DCSR_RUN;
  549. /* clear irq */
  550. dcsr = phy_readl_relaxed(phy, DCSR);
  551. phy_writel(phy, dcsr, DCSR);
  552. if ((dcsr & PXA_DCSR_BUSERR) && (phy->vchan))
  553. dev_warn(&phy->vchan->vc.chan.dev->device,
  554. "%s(chan=%p): PXA_DCSR_BUSERR\n",
  555. __func__, &phy->vchan);
  556. return dcsr & ~PXA_DCSR_RUN;
  557. }
  558. static irqreturn_t pxad_chan_handler(int irq, void *dev_id)
  559. {
  560. struct pxad_phy *phy = dev_id;
  561. struct pxad_chan *chan = phy->vchan;
  562. struct virt_dma_desc *vd, *tmp;
  563. unsigned int dcsr;
  564. unsigned long flags;
  565. BUG_ON(!chan);
  566. dcsr = clear_chan_irq(phy);
  567. if (dcsr & PXA_DCSR_RUN)
  568. return IRQ_NONE;
  569. spin_lock_irqsave(&chan->vc.lock, flags);
  570. list_for_each_entry_safe(vd, tmp, &chan->vc.desc_issued, node) {
  571. dev_dbg(&chan->vc.chan.dev->device,
  572. "%s(): checking txd %p[%x]: completed=%d\n",
  573. __func__, vd, vd->tx.cookie, is_desc_completed(vd));
  574. if (is_desc_completed(vd)) {
  575. list_del(&vd->node);
  576. vchan_cookie_complete(vd);
  577. } else {
  578. break;
  579. }
  580. }
  581. if (dcsr & PXA_DCSR_STOPSTATE) {
  582. dev_dbg(&chan->vc.chan.dev->device,
  583. "%s(): channel stopped, submitted_empty=%d issued_empty=%d",
  584. __func__,
  585. list_empty(&chan->vc.desc_submitted),
  586. list_empty(&chan->vc.desc_issued));
  587. phy_writel_relaxed(phy, dcsr & ~PXA_DCSR_STOPIRQEN, DCSR);
  588. if (list_empty(&chan->vc.desc_issued)) {
  589. chan->misaligned =
  590. !list_empty(&chan->vc.desc_submitted);
  591. } else {
  592. vd = list_first_entry(&chan->vc.desc_issued,
  593. struct virt_dma_desc, node);
  594. pxad_launch_chan(chan, to_pxad_sw_desc(vd));
  595. }
  596. }
  597. spin_unlock_irqrestore(&chan->vc.lock, flags);
  598. return IRQ_HANDLED;
  599. }
  600. static irqreturn_t pxad_int_handler(int irq, void *dev_id)
  601. {
  602. struct pxad_device *pdev = dev_id;
  603. struct pxad_phy *phy;
  604. u32 dint = readl(pdev->base + DINT);
  605. int i, ret = IRQ_NONE;
  606. while (dint) {
  607. i = __ffs(dint);
  608. dint &= (dint - 1);
  609. phy = &pdev->phys[i];
  610. if ((i < 32) && (legacy_reserved & BIT(i)))
  611. continue;
  612. if (pxad_chan_handler(irq, phy) == IRQ_HANDLED)
  613. ret = IRQ_HANDLED;
  614. }
  615. return ret;
  616. }
  617. static int pxad_alloc_chan_resources(struct dma_chan *dchan)
  618. {
  619. struct pxad_chan *chan = to_pxad_chan(dchan);
  620. struct pxad_device *pdev = to_pxad_dev(chan->vc.chan.device);
  621. if (chan->desc_pool)
  622. return 1;
  623. chan->desc_pool = dma_pool_create(dma_chan_name(dchan),
  624. pdev->slave.dev,
  625. sizeof(struct pxad_desc_hw),
  626. __alignof__(struct pxad_desc_hw),
  627. 0);
  628. if (!chan->desc_pool) {
  629. dev_err(&chan->vc.chan.dev->device,
  630. "%s(): unable to allocate descriptor pool\n",
  631. __func__);
  632. return -ENOMEM;
  633. }
  634. return 1;
  635. }
  636. static void pxad_free_chan_resources(struct dma_chan *dchan)
  637. {
  638. struct pxad_chan *chan = to_pxad_chan(dchan);
  639. vchan_free_chan_resources(&chan->vc);
  640. dma_pool_destroy(chan->desc_pool);
  641. chan->desc_pool = NULL;
  642. }
  643. static void pxad_free_desc(struct virt_dma_desc *vd)
  644. {
  645. int i;
  646. dma_addr_t dma;
  647. struct pxad_desc_sw *sw_desc = to_pxad_sw_desc(vd);
  648. BUG_ON(sw_desc->nb_desc == 0);
  649. for (i = sw_desc->nb_desc - 1; i >= 0; i--) {
  650. if (i > 0)
  651. dma = sw_desc->hw_desc[i - 1]->ddadr;
  652. else
  653. dma = sw_desc->first;
  654. dma_pool_free(sw_desc->desc_pool,
  655. sw_desc->hw_desc[i], dma);
  656. }
  657. sw_desc->nb_desc = 0;
  658. kfree(sw_desc);
  659. }
  660. static struct pxad_desc_sw *
  661. pxad_alloc_desc(struct pxad_chan *chan, unsigned int nb_hw_desc)
  662. {
  663. struct pxad_desc_sw *sw_desc;
  664. dma_addr_t dma;
  665. int i;
  666. sw_desc = kzalloc(sizeof(*sw_desc) +
  667. nb_hw_desc * sizeof(struct pxad_desc_hw *),
  668. GFP_NOWAIT);
  669. if (!sw_desc)
  670. return NULL;
  671. sw_desc->desc_pool = chan->desc_pool;
  672. for (i = 0; i < nb_hw_desc; i++) {
  673. sw_desc->hw_desc[i] = dma_pool_alloc(sw_desc->desc_pool,
  674. GFP_NOWAIT, &dma);
  675. if (!sw_desc->hw_desc[i]) {
  676. dev_err(&chan->vc.chan.dev->device,
  677. "%s(): Couldn't allocate the %dth hw_desc from dma_pool %p\n",
  678. __func__, i, sw_desc->desc_pool);
  679. goto err;
  680. }
  681. if (i == 0)
  682. sw_desc->first = dma;
  683. else
  684. sw_desc->hw_desc[i - 1]->ddadr = dma;
  685. sw_desc->nb_desc++;
  686. }
  687. return sw_desc;
  688. err:
  689. pxad_free_desc(&sw_desc->vd);
  690. return NULL;
  691. }
  692. static dma_cookie_t pxad_tx_submit(struct dma_async_tx_descriptor *tx)
  693. {
  694. struct virt_dma_chan *vc = to_virt_chan(tx->chan);
  695. struct pxad_chan *chan = to_pxad_chan(&vc->chan);
  696. struct virt_dma_desc *vd_chained = NULL,
  697. *vd = container_of(tx, struct virt_dma_desc, tx);
  698. dma_cookie_t cookie;
  699. unsigned long flags;
  700. set_updater_desc(to_pxad_sw_desc(vd), tx->flags);
  701. spin_lock_irqsave(&vc->lock, flags);
  702. cookie = dma_cookie_assign(tx);
  703. if (list_empty(&vc->desc_submitted) && pxad_try_hotchain(vc, vd)) {
  704. list_move_tail(&vd->node, &vc->desc_issued);
  705. dev_dbg(&chan->vc.chan.dev->device,
  706. "%s(): txd %p[%x]: submitted (hot linked)\n",
  707. __func__, vd, cookie);
  708. goto out;
  709. }
  710. /*
  711. * Fallback to placing the tx in the submitted queue
  712. */
  713. if (!list_empty(&vc->desc_submitted)) {
  714. vd_chained = list_entry(vc->desc_submitted.prev,
  715. struct virt_dma_desc, node);
  716. /*
  717. * Only chain the descriptors if no new misalignment is
  718. * introduced. If a new misalignment is chained, let the channel
  719. * stop, and be relaunched in misalign mode from the irq
  720. * handler.
  721. */
  722. if (chan->misaligned || !to_pxad_sw_desc(vd)->misaligned)
  723. pxad_desc_chain(vd_chained, vd);
  724. else
  725. vd_chained = NULL;
  726. }
  727. dev_dbg(&chan->vc.chan.dev->device,
  728. "%s(): txd %p[%x]: submitted (%s linked)\n",
  729. __func__, vd, cookie, vd_chained ? "cold" : "not");
  730. list_move_tail(&vd->node, &vc->desc_submitted);
  731. chan->misaligned |= to_pxad_sw_desc(vd)->misaligned;
  732. out:
  733. spin_unlock_irqrestore(&vc->lock, flags);
  734. return cookie;
  735. }
  736. static void pxad_issue_pending(struct dma_chan *dchan)
  737. {
  738. struct pxad_chan *chan = to_pxad_chan(dchan);
  739. struct virt_dma_desc *vd_first;
  740. unsigned long flags;
  741. spin_lock_irqsave(&chan->vc.lock, flags);
  742. if (list_empty(&chan->vc.desc_submitted))
  743. goto out;
  744. vd_first = list_first_entry(&chan->vc.desc_submitted,
  745. struct virt_dma_desc, node);
  746. dev_dbg(&chan->vc.chan.dev->device,
  747. "%s(): txd %p[%x]", __func__, vd_first, vd_first->tx.cookie);
  748. vchan_issue_pending(&chan->vc);
  749. if (!pxad_try_hotchain(&chan->vc, vd_first))
  750. pxad_launch_chan(chan, to_pxad_sw_desc(vd_first));
  751. out:
  752. spin_unlock_irqrestore(&chan->vc.lock, flags);
  753. }
  754. static inline struct dma_async_tx_descriptor *
  755. pxad_tx_prep(struct virt_dma_chan *vc, struct virt_dma_desc *vd,
  756. unsigned long tx_flags)
  757. {
  758. struct dma_async_tx_descriptor *tx;
  759. struct pxad_chan *chan = container_of(vc, struct pxad_chan, vc);
  760. tx = vchan_tx_prep(vc, vd, tx_flags);
  761. tx->tx_submit = pxad_tx_submit;
  762. dev_dbg(&chan->vc.chan.dev->device,
  763. "%s(): vc=%p txd=%p[%x] flags=0x%lx\n", __func__,
  764. vc, vd, vd->tx.cookie,
  765. tx_flags);
  766. return tx;
  767. }
  768. static void pxad_get_config(struct pxad_chan *chan,
  769. enum dma_transfer_direction dir,
  770. u32 *dcmd, u32 *dev_src, u32 *dev_dst)
  771. {
  772. u32 maxburst = 0, dev_addr = 0;
  773. enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  774. *dcmd = 0;
  775. if (chan->cfg.direction == DMA_DEV_TO_MEM) {
  776. maxburst = chan->cfg.src_maxburst;
  777. width = chan->cfg.src_addr_width;
  778. dev_addr = chan->cfg.src_addr;
  779. *dev_src = dev_addr;
  780. *dcmd |= PXA_DCMD_INCTRGADDR | PXA_DCMD_FLOWSRC;
  781. }
  782. if (chan->cfg.direction == DMA_MEM_TO_DEV) {
  783. maxburst = chan->cfg.dst_maxburst;
  784. width = chan->cfg.dst_addr_width;
  785. dev_addr = chan->cfg.dst_addr;
  786. *dev_dst = dev_addr;
  787. *dcmd |= PXA_DCMD_INCSRCADDR | PXA_DCMD_FLOWTRG;
  788. }
  789. if (chan->cfg.direction == DMA_MEM_TO_MEM)
  790. *dcmd |= PXA_DCMD_BURST32 | PXA_DCMD_INCTRGADDR |
  791. PXA_DCMD_INCSRCADDR;
  792. dev_dbg(&chan->vc.chan.dev->device,
  793. "%s(): dev_addr=0x%x maxburst=%d width=%d dir=%d\n",
  794. __func__, dev_addr, maxburst, width, dir);
  795. if (width == DMA_SLAVE_BUSWIDTH_1_BYTE)
  796. *dcmd |= PXA_DCMD_WIDTH1;
  797. else if (width == DMA_SLAVE_BUSWIDTH_2_BYTES)
  798. *dcmd |= PXA_DCMD_WIDTH2;
  799. else if (width == DMA_SLAVE_BUSWIDTH_4_BYTES)
  800. *dcmd |= PXA_DCMD_WIDTH4;
  801. if (maxburst == 8)
  802. *dcmd |= PXA_DCMD_BURST8;
  803. else if (maxburst == 16)
  804. *dcmd |= PXA_DCMD_BURST16;
  805. else if (maxburst == 32)
  806. *dcmd |= PXA_DCMD_BURST32;
  807. /* FIXME: drivers should be ported over to use the filter
  808. * function. Once that's done, the following two lines can
  809. * be removed.
  810. */
  811. if (chan->cfg.slave_id)
  812. chan->drcmr = chan->cfg.slave_id;
  813. }
  814. static struct dma_async_tx_descriptor *
  815. pxad_prep_memcpy(struct dma_chan *dchan,
  816. dma_addr_t dma_dst, dma_addr_t dma_src,
  817. size_t len, unsigned long flags)
  818. {
  819. struct pxad_chan *chan = to_pxad_chan(dchan);
  820. struct pxad_desc_sw *sw_desc;
  821. struct pxad_desc_hw *hw_desc;
  822. u32 dcmd;
  823. unsigned int i, nb_desc = 0;
  824. size_t copy;
  825. if (!dchan || !len)
  826. return NULL;
  827. dev_dbg(&chan->vc.chan.dev->device,
  828. "%s(): dma_dst=0x%lx dma_src=0x%lx len=%zu flags=%lx\n",
  829. __func__, (unsigned long)dma_dst, (unsigned long)dma_src,
  830. len, flags);
  831. pxad_get_config(chan, DMA_MEM_TO_MEM, &dcmd, NULL, NULL);
  832. nb_desc = DIV_ROUND_UP(len, PDMA_MAX_DESC_BYTES);
  833. sw_desc = pxad_alloc_desc(chan, nb_desc + 1);
  834. if (!sw_desc)
  835. return NULL;
  836. sw_desc->len = len;
  837. if (!IS_ALIGNED(dma_src, 1 << PDMA_ALIGNMENT) ||
  838. !IS_ALIGNED(dma_dst, 1 << PDMA_ALIGNMENT))
  839. sw_desc->misaligned = true;
  840. i = 0;
  841. do {
  842. hw_desc = sw_desc->hw_desc[i++];
  843. copy = min_t(size_t, len, PDMA_MAX_DESC_BYTES);
  844. hw_desc->dcmd = dcmd | (PXA_DCMD_LENGTH & copy);
  845. hw_desc->dsadr = dma_src;
  846. hw_desc->dtadr = dma_dst;
  847. len -= copy;
  848. dma_src += copy;
  849. dma_dst += copy;
  850. } while (len);
  851. set_updater_desc(sw_desc, flags);
  852. return pxad_tx_prep(&chan->vc, &sw_desc->vd, flags);
  853. }
  854. static struct dma_async_tx_descriptor *
  855. pxad_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
  856. unsigned int sg_len, enum dma_transfer_direction dir,
  857. unsigned long flags, void *context)
  858. {
  859. struct pxad_chan *chan = to_pxad_chan(dchan);
  860. struct pxad_desc_sw *sw_desc;
  861. size_t len, avail;
  862. struct scatterlist *sg;
  863. dma_addr_t dma;
  864. u32 dcmd, dsadr = 0, dtadr = 0;
  865. unsigned int nb_desc = 0, i, j = 0;
  866. if ((sgl == NULL) || (sg_len == 0))
  867. return NULL;
  868. pxad_get_config(chan, dir, &dcmd, &dsadr, &dtadr);
  869. dev_dbg(&chan->vc.chan.dev->device,
  870. "%s(): dir=%d flags=%lx\n", __func__, dir, flags);
  871. for_each_sg(sgl, sg, sg_len, i)
  872. nb_desc += DIV_ROUND_UP(sg_dma_len(sg), PDMA_MAX_DESC_BYTES);
  873. sw_desc = pxad_alloc_desc(chan, nb_desc + 1);
  874. if (!sw_desc)
  875. return NULL;
  876. for_each_sg(sgl, sg, sg_len, i) {
  877. dma = sg_dma_address(sg);
  878. avail = sg_dma_len(sg);
  879. sw_desc->len += avail;
  880. do {
  881. len = min_t(size_t, avail, PDMA_MAX_DESC_BYTES);
  882. if (dma & 0x7)
  883. sw_desc->misaligned = true;
  884. sw_desc->hw_desc[j]->dcmd =
  885. dcmd | (PXA_DCMD_LENGTH & len);
  886. sw_desc->hw_desc[j]->dsadr = dsadr ? dsadr : dma;
  887. sw_desc->hw_desc[j++]->dtadr = dtadr ? dtadr : dma;
  888. dma += len;
  889. avail -= len;
  890. } while (avail);
  891. }
  892. set_updater_desc(sw_desc, flags);
  893. return pxad_tx_prep(&chan->vc, &sw_desc->vd, flags);
  894. }
  895. static struct dma_async_tx_descriptor *
  896. pxad_prep_dma_cyclic(struct dma_chan *dchan,
  897. dma_addr_t buf_addr, size_t len, size_t period_len,
  898. enum dma_transfer_direction dir, unsigned long flags)
  899. {
  900. struct pxad_chan *chan = to_pxad_chan(dchan);
  901. struct pxad_desc_sw *sw_desc;
  902. struct pxad_desc_hw **phw_desc;
  903. dma_addr_t dma;
  904. u32 dcmd, dsadr = 0, dtadr = 0;
  905. unsigned int nb_desc = 0;
  906. if (!dchan || !len || !period_len)
  907. return NULL;
  908. if ((dir != DMA_DEV_TO_MEM) && (dir != DMA_MEM_TO_DEV)) {
  909. dev_err(&chan->vc.chan.dev->device,
  910. "Unsupported direction for cyclic DMA\n");
  911. return NULL;
  912. }
  913. /* the buffer length must be a multiple of period_len */
  914. if (len % period_len != 0 || period_len > PDMA_MAX_DESC_BYTES ||
  915. !IS_ALIGNED(period_len, 1 << PDMA_ALIGNMENT))
  916. return NULL;
  917. pxad_get_config(chan, dir, &dcmd, &dsadr, &dtadr);
  918. dcmd |= PXA_DCMD_ENDIRQEN | (PXA_DCMD_LENGTH | period_len);
  919. dev_dbg(&chan->vc.chan.dev->device,
  920. "%s(): buf_addr=0x%lx len=%zu period=%zu dir=%d flags=%lx\n",
  921. __func__, (unsigned long)buf_addr, len, period_len, dir, flags);
  922. nb_desc = DIV_ROUND_UP(period_len, PDMA_MAX_DESC_BYTES);
  923. nb_desc *= DIV_ROUND_UP(len, period_len);
  924. sw_desc = pxad_alloc_desc(chan, nb_desc + 1);
  925. if (!sw_desc)
  926. return NULL;
  927. sw_desc->cyclic = true;
  928. sw_desc->len = len;
  929. phw_desc = sw_desc->hw_desc;
  930. dma = buf_addr;
  931. do {
  932. phw_desc[0]->dsadr = dsadr ? dsadr : dma;
  933. phw_desc[0]->dtadr = dtadr ? dtadr : dma;
  934. phw_desc[0]->dcmd = dcmd;
  935. phw_desc++;
  936. dma += period_len;
  937. len -= period_len;
  938. } while (len);
  939. set_updater_desc(sw_desc, flags);
  940. return pxad_tx_prep(&chan->vc, &sw_desc->vd, flags);
  941. }
  942. static int pxad_config(struct dma_chan *dchan,
  943. struct dma_slave_config *cfg)
  944. {
  945. struct pxad_chan *chan = to_pxad_chan(dchan);
  946. if (!dchan)
  947. return -EINVAL;
  948. chan->cfg = *cfg;
  949. return 0;
  950. }
  951. static int pxad_terminate_all(struct dma_chan *dchan)
  952. {
  953. struct pxad_chan *chan = to_pxad_chan(dchan);
  954. struct pxad_device *pdev = to_pxad_dev(chan->vc.chan.device);
  955. struct virt_dma_desc *vd = NULL;
  956. unsigned long flags;
  957. struct pxad_phy *phy;
  958. LIST_HEAD(head);
  959. dev_dbg(&chan->vc.chan.dev->device,
  960. "%s(): vchan %p: terminate all\n", __func__, &chan->vc);
  961. spin_lock_irqsave(&chan->vc.lock, flags);
  962. vchan_get_all_descriptors(&chan->vc, &head);
  963. list_for_each_entry(vd, &head, node) {
  964. dev_dbg(&chan->vc.chan.dev->device,
  965. "%s(): cancelling txd %p[%x] (completed=%d)", __func__,
  966. vd, vd->tx.cookie, is_desc_completed(vd));
  967. }
  968. phy = chan->phy;
  969. if (phy) {
  970. phy_disable(chan->phy);
  971. pxad_free_phy(chan);
  972. chan->phy = NULL;
  973. spin_lock(&pdev->phy_lock);
  974. phy->vchan = NULL;
  975. spin_unlock(&pdev->phy_lock);
  976. }
  977. spin_unlock_irqrestore(&chan->vc.lock, flags);
  978. vchan_dma_desc_free_list(&chan->vc, &head);
  979. return 0;
  980. }
  981. static unsigned int pxad_residue(struct pxad_chan *chan,
  982. dma_cookie_t cookie)
  983. {
  984. struct virt_dma_desc *vd = NULL;
  985. struct pxad_desc_sw *sw_desc = NULL;
  986. struct pxad_desc_hw *hw_desc = NULL;
  987. u32 curr, start, len, end, residue = 0;
  988. unsigned long flags;
  989. bool passed = false;
  990. int i;
  991. /*
  992. * If the channel does not have a phy pointer anymore, it has already
  993. * been completed. Therefore, its residue is 0.
  994. */
  995. if (!chan->phy)
  996. return 0;
  997. spin_lock_irqsave(&chan->vc.lock, flags);
  998. vd = vchan_find_desc(&chan->vc, cookie);
  999. if (!vd)
  1000. goto out;
  1001. sw_desc = to_pxad_sw_desc(vd);
  1002. if (sw_desc->hw_desc[0]->dcmd & PXA_DCMD_INCSRCADDR)
  1003. curr = phy_readl_relaxed(chan->phy, DSADR);
  1004. else
  1005. curr = phy_readl_relaxed(chan->phy, DTADR);
  1006. for (i = 0; i < sw_desc->nb_desc - 1; i++) {
  1007. hw_desc = sw_desc->hw_desc[i];
  1008. if (sw_desc->hw_desc[0]->dcmd & PXA_DCMD_INCSRCADDR)
  1009. start = hw_desc->dsadr;
  1010. else
  1011. start = hw_desc->dtadr;
  1012. len = hw_desc->dcmd & PXA_DCMD_LENGTH;
  1013. end = start + len;
  1014. /*
  1015. * 'passed' will be latched once we found the descriptor
  1016. * which lies inside the boundaries of the curr
  1017. * pointer. All descriptors that occur in the list
  1018. * _after_ we found that partially handled descriptor
  1019. * are still to be processed and are hence added to the
  1020. * residual bytes counter.
  1021. */
  1022. if (passed) {
  1023. residue += len;
  1024. } else if (curr >= start && curr <= end) {
  1025. residue += end - curr;
  1026. passed = true;
  1027. }
  1028. }
  1029. if (!passed)
  1030. residue = sw_desc->len;
  1031. out:
  1032. spin_unlock_irqrestore(&chan->vc.lock, flags);
  1033. dev_dbg(&chan->vc.chan.dev->device,
  1034. "%s(): txd %p[%x] sw_desc=%p: %d\n",
  1035. __func__, vd, cookie, sw_desc, residue);
  1036. return residue;
  1037. }
  1038. static enum dma_status pxad_tx_status(struct dma_chan *dchan,
  1039. dma_cookie_t cookie,
  1040. struct dma_tx_state *txstate)
  1041. {
  1042. struct pxad_chan *chan = to_pxad_chan(dchan);
  1043. enum dma_status ret;
  1044. ret = dma_cookie_status(dchan, cookie, txstate);
  1045. if (likely(txstate && (ret != DMA_ERROR)))
  1046. dma_set_residue(txstate, pxad_residue(chan, cookie));
  1047. return ret;
  1048. }
  1049. static void pxad_free_channels(struct dma_device *dmadev)
  1050. {
  1051. struct pxad_chan *c, *cn;
  1052. list_for_each_entry_safe(c, cn, &dmadev->channels,
  1053. vc.chan.device_node) {
  1054. list_del(&c->vc.chan.device_node);
  1055. tasklet_kill(&c->vc.task);
  1056. }
  1057. }
  1058. static int pxad_remove(struct platform_device *op)
  1059. {
  1060. struct pxad_device *pdev = platform_get_drvdata(op);
  1061. pxad_cleanup_debugfs(pdev);
  1062. pxad_free_channels(&pdev->slave);
  1063. dma_async_device_unregister(&pdev->slave);
  1064. return 0;
  1065. }
  1066. static int pxad_init_phys(struct platform_device *op,
  1067. struct pxad_device *pdev,
  1068. unsigned int nb_phy_chans)
  1069. {
  1070. int irq0, irq, nr_irq = 0, i, ret;
  1071. struct pxad_phy *phy;
  1072. irq0 = platform_get_irq(op, 0);
  1073. if (irq0 < 0)
  1074. return irq0;
  1075. pdev->phys = devm_kcalloc(&op->dev, nb_phy_chans,
  1076. sizeof(pdev->phys[0]), GFP_KERNEL);
  1077. if (!pdev->phys)
  1078. return -ENOMEM;
  1079. for (i = 0; i < nb_phy_chans; i++)
  1080. if (platform_get_irq(op, i) > 0)
  1081. nr_irq++;
  1082. for (i = 0; i < nb_phy_chans; i++) {
  1083. phy = &pdev->phys[i];
  1084. phy->base = pdev->base;
  1085. phy->idx = i;
  1086. irq = platform_get_irq(op, i);
  1087. if ((nr_irq > 1) && (irq > 0))
  1088. ret = devm_request_irq(&op->dev, irq,
  1089. pxad_chan_handler,
  1090. IRQF_SHARED, "pxa-dma", phy);
  1091. if ((nr_irq == 1) && (i == 0))
  1092. ret = devm_request_irq(&op->dev, irq0,
  1093. pxad_int_handler,
  1094. IRQF_SHARED, "pxa-dma", pdev);
  1095. if (ret) {
  1096. dev_err(pdev->slave.dev,
  1097. "%s(): can't request irq %d:%d\n", __func__,
  1098. irq, ret);
  1099. return ret;
  1100. }
  1101. }
  1102. return 0;
  1103. }
  1104. static const struct of_device_id const pxad_dt_ids[] = {
  1105. { .compatible = "marvell,pdma-1.0", },
  1106. {}
  1107. };
  1108. MODULE_DEVICE_TABLE(of, pxad_dt_ids);
  1109. static struct dma_chan *pxad_dma_xlate(struct of_phandle_args *dma_spec,
  1110. struct of_dma *ofdma)
  1111. {
  1112. struct pxad_device *d = ofdma->of_dma_data;
  1113. struct dma_chan *chan;
  1114. chan = dma_get_any_slave_channel(&d->slave);
  1115. if (!chan)
  1116. return NULL;
  1117. to_pxad_chan(chan)->drcmr = dma_spec->args[0];
  1118. to_pxad_chan(chan)->prio = dma_spec->args[1];
  1119. return chan;
  1120. }
  1121. static int pxad_init_dmadev(struct platform_device *op,
  1122. struct pxad_device *pdev,
  1123. unsigned int nr_phy_chans)
  1124. {
  1125. int ret;
  1126. unsigned int i;
  1127. struct pxad_chan *c;
  1128. pdev->nr_chans = nr_phy_chans;
  1129. INIT_LIST_HEAD(&pdev->slave.channels);
  1130. pdev->slave.device_alloc_chan_resources = pxad_alloc_chan_resources;
  1131. pdev->slave.device_free_chan_resources = pxad_free_chan_resources;
  1132. pdev->slave.device_tx_status = pxad_tx_status;
  1133. pdev->slave.device_issue_pending = pxad_issue_pending;
  1134. pdev->slave.device_config = pxad_config;
  1135. pdev->slave.device_terminate_all = pxad_terminate_all;
  1136. if (op->dev.coherent_dma_mask)
  1137. dma_set_mask(&op->dev, op->dev.coherent_dma_mask);
  1138. else
  1139. dma_set_mask(&op->dev, DMA_BIT_MASK(32));
  1140. ret = pxad_init_phys(op, pdev, nr_phy_chans);
  1141. if (ret)
  1142. return ret;
  1143. for (i = 0; i < nr_phy_chans; i++) {
  1144. c = devm_kzalloc(&op->dev, sizeof(*c), GFP_KERNEL);
  1145. if (!c)
  1146. return -ENOMEM;
  1147. c->vc.desc_free = pxad_free_desc;
  1148. vchan_init(&c->vc, &pdev->slave);
  1149. }
  1150. return dma_async_device_register(&pdev->slave);
  1151. }
  1152. static int pxad_probe(struct platform_device *op)
  1153. {
  1154. struct pxad_device *pdev;
  1155. const struct of_device_id *of_id;
  1156. struct mmp_dma_platdata *pdata = dev_get_platdata(&op->dev);
  1157. struct resource *iores;
  1158. int ret, dma_channels = 0;
  1159. const enum dma_slave_buswidth widths =
  1160. DMA_SLAVE_BUSWIDTH_1_BYTE | DMA_SLAVE_BUSWIDTH_2_BYTES |
  1161. DMA_SLAVE_BUSWIDTH_4_BYTES;
  1162. pdev = devm_kzalloc(&op->dev, sizeof(*pdev), GFP_KERNEL);
  1163. if (!pdev)
  1164. return -ENOMEM;
  1165. spin_lock_init(&pdev->phy_lock);
  1166. iores = platform_get_resource(op, IORESOURCE_MEM, 0);
  1167. pdev->base = devm_ioremap_resource(&op->dev, iores);
  1168. if (IS_ERR(pdev->base))
  1169. return PTR_ERR(pdev->base);
  1170. of_id = of_match_device(pxad_dt_ids, &op->dev);
  1171. if (of_id)
  1172. of_property_read_u32(op->dev.of_node, "#dma-channels",
  1173. &dma_channels);
  1174. else if (pdata && pdata->dma_channels)
  1175. dma_channels = pdata->dma_channels;
  1176. else
  1177. dma_channels = 32; /* default 32 channel */
  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.copy_align = PDMA_ALIGNMENT;
  1186. pdev->slave.src_addr_widths = widths;
  1187. pdev->slave.dst_addr_widths = widths;
  1188. pdev->slave.directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM);
  1189. pdev->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  1190. pdev->slave.dev = &op->dev;
  1191. ret = pxad_init_dmadev(op, pdev, dma_channels);
  1192. if (ret) {
  1193. dev_err(pdev->slave.dev, "unable to register\n");
  1194. return ret;
  1195. }
  1196. if (op->dev.of_node) {
  1197. /* Device-tree DMA controller registration */
  1198. ret = of_dma_controller_register(op->dev.of_node,
  1199. pxad_dma_xlate, pdev);
  1200. if (ret < 0) {
  1201. dev_err(pdev->slave.dev,
  1202. "of_dma_controller_register failed\n");
  1203. return ret;
  1204. }
  1205. }
  1206. platform_set_drvdata(op, pdev);
  1207. pxad_init_debugfs(pdev);
  1208. dev_info(pdev->slave.dev, "initialized %d channels\n", dma_channels);
  1209. return 0;
  1210. }
  1211. static const struct platform_device_id pxad_id_table[] = {
  1212. { "pxa-dma", },
  1213. { },
  1214. };
  1215. static struct platform_driver pxad_driver = {
  1216. .driver = {
  1217. .name = "pxa-dma",
  1218. .of_match_table = pxad_dt_ids,
  1219. },
  1220. .id_table = pxad_id_table,
  1221. .probe = pxad_probe,
  1222. .remove = pxad_remove,
  1223. };
  1224. bool pxad_filter_fn(struct dma_chan *chan, void *param)
  1225. {
  1226. struct pxad_chan *c = to_pxad_chan(chan);
  1227. struct pxad_param *p = param;
  1228. if (chan->device->dev->driver != &pxad_driver.driver)
  1229. return false;
  1230. c->drcmr = p->drcmr;
  1231. c->prio = p->prio;
  1232. return true;
  1233. }
  1234. EXPORT_SYMBOL_GPL(pxad_filter_fn);
  1235. int pxad_toggle_reserved_channel(int legacy_channel)
  1236. {
  1237. if (legacy_unavailable & (BIT(legacy_channel)))
  1238. return -EBUSY;
  1239. legacy_reserved ^= BIT(legacy_channel);
  1240. return 0;
  1241. }
  1242. EXPORT_SYMBOL_GPL(pxad_toggle_reserved_channel);
  1243. module_platform_driver(pxad_driver);
  1244. MODULE_DESCRIPTION("Marvell PXA Peripheral DMA Driver");
  1245. MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
  1246. MODULE_LICENSE("GPL v2");