pxa_dma.c 41 KB

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