pxa_dma.c 40 KB

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