pxa_dma.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  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.dev = &op->dev;
  1208. ret = pxad_init_dmadev(op, pdev, dma_channels);
  1209. if (ret) {
  1210. dev_err(pdev->slave.dev, "unable to register\n");
  1211. return ret;
  1212. }
  1213. if (op->dev.of_node) {
  1214. /* Device-tree DMA controller registration */
  1215. ret = of_dma_controller_register(op->dev.of_node,
  1216. pxad_dma_xlate, pdev);
  1217. if (ret < 0) {
  1218. dev_err(pdev->slave.dev,
  1219. "of_dma_controller_register failed\n");
  1220. return ret;
  1221. }
  1222. }
  1223. platform_set_drvdata(op, pdev);
  1224. pxad_init_debugfs(pdev);
  1225. dev_info(pdev->slave.dev, "initialized %d channels\n", dma_channels);
  1226. return 0;
  1227. }
  1228. static const struct platform_device_id pxad_id_table[] = {
  1229. { "pxa-dma", },
  1230. { },
  1231. };
  1232. static struct platform_driver pxad_driver = {
  1233. .driver = {
  1234. .name = "pxa-dma",
  1235. .of_match_table = pxad_dt_ids,
  1236. },
  1237. .id_table = pxad_id_table,
  1238. .probe = pxad_probe,
  1239. .remove = pxad_remove,
  1240. };
  1241. bool pxad_filter_fn(struct dma_chan *chan, void *param)
  1242. {
  1243. struct pxad_chan *c = to_pxad_chan(chan);
  1244. struct pxad_param *p = param;
  1245. if (chan->device->dev->driver != &pxad_driver.driver)
  1246. return false;
  1247. c->drcmr = p->drcmr;
  1248. c->prio = p->prio;
  1249. return true;
  1250. }
  1251. EXPORT_SYMBOL_GPL(pxad_filter_fn);
  1252. int pxad_toggle_reserved_channel(int legacy_channel)
  1253. {
  1254. if (legacy_unavailable & (BIT(legacy_channel)))
  1255. return -EBUSY;
  1256. legacy_reserved ^= BIT(legacy_channel);
  1257. return 0;
  1258. }
  1259. EXPORT_SYMBOL_GPL(pxad_toggle_reserved_channel);
  1260. module_platform_driver(pxad_driver);
  1261. MODULE_DESCRIPTION("Marvell PXA Peripheral DMA Driver");
  1262. MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
  1263. MODULE_LICENSE("GPL v2");