pxa_dma.c 41 KB

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