dw_dmac.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  1. /*
  2. * Driver for the Synopsys DesignWare DMA Controller (aka DMACA on
  3. * AVR32 systems.)
  4. *
  5. * Copyright (C) 2007-2008 Atmel Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/mm.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include "dw_dmac_regs.h"
  23. /*
  24. * This supports the Synopsys "DesignWare AHB Central DMA Controller",
  25. * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all
  26. * of which use ARM any more). See the "Databook" from Synopsys for
  27. * information beyond what licensees probably provide.
  28. *
  29. * The driver has currently been tested only with the Atmel AT32AP7000,
  30. * which does not support descriptor writeback.
  31. */
  32. #define DWC_DEFAULT_CTLLO(private) ({ \
  33. struct dw_dma_slave *__slave = (private); \
  34. int dms = __slave ? __slave->dst_master : 0; \
  35. int sms = __slave ? __slave->src_master : 1; \
  36. \
  37. (DWC_CTLL_DST_MSIZE(0) \
  38. | DWC_CTLL_SRC_MSIZE(0) \
  39. | DWC_CTLL_LLP_D_EN \
  40. | DWC_CTLL_LLP_S_EN \
  41. | DWC_CTLL_DMS(dms) \
  42. | DWC_CTLL_SMS(sms)); \
  43. })
  44. /*
  45. * This is configuration-dependent and usually a funny size like 4095.
  46. * Let's round it down to the nearest power of two.
  47. *
  48. * Note that this is a transfer count, i.e. if we transfer 32-bit
  49. * words, we can do 8192 bytes per descriptor.
  50. *
  51. * This parameter is also system-specific.
  52. */
  53. #define DWC_MAX_COUNT 2048U
  54. /*
  55. * Number of descriptors to allocate for each channel. This should be
  56. * made configurable somehow; preferably, the clients (at least the
  57. * ones using slave transfers) should be able to give us a hint.
  58. */
  59. #define NR_DESCS_PER_CHANNEL 64
  60. /*----------------------------------------------------------------------*/
  61. /*
  62. * Because we're not relying on writeback from the controller (it may not
  63. * even be configured into the core!) we don't need to use dma_pool. These
  64. * descriptors -- and associated data -- are cacheable. We do need to make
  65. * sure their dcache entries are written back before handing them off to
  66. * the controller, though.
  67. */
  68. static struct device *chan2dev(struct dma_chan *chan)
  69. {
  70. return &chan->dev->device;
  71. }
  72. static struct device *chan2parent(struct dma_chan *chan)
  73. {
  74. return chan->dev->device.parent;
  75. }
  76. static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
  77. {
  78. return list_entry(dwc->active_list.next, struct dw_desc, desc_node);
  79. }
  80. static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
  81. {
  82. struct dw_desc *desc, *_desc;
  83. struct dw_desc *ret = NULL;
  84. unsigned int i = 0;
  85. spin_lock_bh(&dwc->lock);
  86. list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
  87. if (async_tx_test_ack(&desc->txd)) {
  88. list_del(&desc->desc_node);
  89. ret = desc;
  90. break;
  91. }
  92. dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc);
  93. i++;
  94. }
  95. spin_unlock_bh(&dwc->lock);
  96. dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i);
  97. return ret;
  98. }
  99. static void dwc_sync_desc_for_cpu(struct dw_dma_chan *dwc, struct dw_desc *desc)
  100. {
  101. struct dw_desc *child;
  102. list_for_each_entry(child, &desc->tx_list, desc_node)
  103. dma_sync_single_for_cpu(chan2parent(&dwc->chan),
  104. child->txd.phys, sizeof(child->lli),
  105. DMA_TO_DEVICE);
  106. dma_sync_single_for_cpu(chan2parent(&dwc->chan),
  107. desc->txd.phys, sizeof(desc->lli),
  108. DMA_TO_DEVICE);
  109. }
  110. /*
  111. * Move a descriptor, including any children, to the free list.
  112. * `desc' must not be on any lists.
  113. */
  114. static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
  115. {
  116. if (desc) {
  117. struct dw_desc *child;
  118. dwc_sync_desc_for_cpu(dwc, desc);
  119. spin_lock_bh(&dwc->lock);
  120. list_for_each_entry(child, &desc->tx_list, desc_node)
  121. dev_vdbg(chan2dev(&dwc->chan),
  122. "moving child desc %p to freelist\n",
  123. child);
  124. list_splice_init(&desc->tx_list, &dwc->free_list);
  125. dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc);
  126. list_add(&desc->desc_node, &dwc->free_list);
  127. spin_unlock_bh(&dwc->lock);
  128. }
  129. }
  130. /* Called with dwc->lock held and bh disabled */
  131. static dma_cookie_t
  132. dwc_assign_cookie(struct dw_dma_chan *dwc, struct dw_desc *desc)
  133. {
  134. dma_cookie_t cookie = dwc->chan.cookie;
  135. if (++cookie < 0)
  136. cookie = 1;
  137. dwc->chan.cookie = cookie;
  138. desc->txd.cookie = cookie;
  139. return cookie;
  140. }
  141. /*----------------------------------------------------------------------*/
  142. /* Called with dwc->lock held and bh disabled */
  143. static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
  144. {
  145. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  146. /* ASSERT: channel is idle */
  147. if (dma_readl(dw, CH_EN) & dwc->mask) {
  148. dev_err(chan2dev(&dwc->chan),
  149. "BUG: Attempted to start non-idle channel\n");
  150. dev_err(chan2dev(&dwc->chan),
  151. " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
  152. channel_readl(dwc, SAR),
  153. channel_readl(dwc, DAR),
  154. channel_readl(dwc, LLP),
  155. channel_readl(dwc, CTL_HI),
  156. channel_readl(dwc, CTL_LO));
  157. /* The tasklet will hopefully advance the queue... */
  158. return;
  159. }
  160. channel_writel(dwc, LLP, first->txd.phys);
  161. channel_writel(dwc, CTL_LO,
  162. DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  163. channel_writel(dwc, CTL_HI, 0);
  164. channel_set_bit(dw, CH_EN, dwc->mask);
  165. }
  166. /*----------------------------------------------------------------------*/
  167. static void
  168. dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc)
  169. {
  170. dma_async_tx_callback callback;
  171. void *param;
  172. struct dma_async_tx_descriptor *txd = &desc->txd;
  173. dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
  174. dwc->completed = txd->cookie;
  175. callback = txd->callback;
  176. param = txd->callback_param;
  177. dwc_sync_desc_for_cpu(dwc, desc);
  178. list_splice_init(&desc->tx_list, &dwc->free_list);
  179. list_move(&desc->desc_node, &dwc->free_list);
  180. if (!dwc->chan.private) {
  181. struct device *parent = chan2parent(&dwc->chan);
  182. if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  183. if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
  184. dma_unmap_single(parent, desc->lli.dar,
  185. desc->len, DMA_FROM_DEVICE);
  186. else
  187. dma_unmap_page(parent, desc->lli.dar,
  188. desc->len, DMA_FROM_DEVICE);
  189. }
  190. if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  191. if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
  192. dma_unmap_single(parent, desc->lli.sar,
  193. desc->len, DMA_TO_DEVICE);
  194. else
  195. dma_unmap_page(parent, desc->lli.sar,
  196. desc->len, DMA_TO_DEVICE);
  197. }
  198. }
  199. /*
  200. * The API requires that no submissions are done from a
  201. * callback, so we don't need to drop the lock here
  202. */
  203. if (callback)
  204. callback(param);
  205. }
  206. static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
  207. {
  208. struct dw_desc *desc, *_desc;
  209. LIST_HEAD(list);
  210. if (dma_readl(dw, CH_EN) & dwc->mask) {
  211. dev_err(chan2dev(&dwc->chan),
  212. "BUG: XFER bit set, but channel not idle!\n");
  213. /* Try to continue after resetting the channel... */
  214. channel_clear_bit(dw, CH_EN, dwc->mask);
  215. while (dma_readl(dw, CH_EN) & dwc->mask)
  216. cpu_relax();
  217. }
  218. /*
  219. * Submit queued descriptors ASAP, i.e. before we go through
  220. * the completed ones.
  221. */
  222. list_splice_init(&dwc->active_list, &list);
  223. if (!list_empty(&dwc->queue)) {
  224. list_move(dwc->queue.next, &dwc->active_list);
  225. dwc_dostart(dwc, dwc_first_active(dwc));
  226. }
  227. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  228. dwc_descriptor_complete(dwc, desc);
  229. }
  230. static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
  231. {
  232. dma_addr_t llp;
  233. struct dw_desc *desc, *_desc;
  234. struct dw_desc *child;
  235. u32 status_xfer;
  236. /*
  237. * Clear block interrupt flag before scanning so that we don't
  238. * miss any, and read LLP before RAW_XFER to ensure it is
  239. * valid if we decide to scan the list.
  240. */
  241. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  242. llp = channel_readl(dwc, LLP);
  243. status_xfer = dma_readl(dw, RAW.XFER);
  244. if (status_xfer & dwc->mask) {
  245. /* Everything we've submitted is done */
  246. dma_writel(dw, CLEAR.XFER, dwc->mask);
  247. dwc_complete_all(dw, dwc);
  248. return;
  249. }
  250. if (list_empty(&dwc->active_list))
  251. return;
  252. dev_vdbg(chan2dev(&dwc->chan), "scan_descriptors: llp=0x%x\n", llp);
  253. list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
  254. if (desc->lli.llp == llp)
  255. /* This one is currently in progress */
  256. return;
  257. list_for_each_entry(child, &desc->tx_list, desc_node)
  258. if (child->lli.llp == llp)
  259. /* Currently in progress */
  260. return;
  261. /*
  262. * No descriptors so far seem to be in progress, i.e.
  263. * this one must be done.
  264. */
  265. dwc_descriptor_complete(dwc, desc);
  266. }
  267. dev_err(chan2dev(&dwc->chan),
  268. "BUG: All descriptors done, but channel not idle!\n");
  269. /* Try to continue after resetting the channel... */
  270. channel_clear_bit(dw, CH_EN, dwc->mask);
  271. while (dma_readl(dw, CH_EN) & dwc->mask)
  272. cpu_relax();
  273. if (!list_empty(&dwc->queue)) {
  274. list_move(dwc->queue.next, &dwc->active_list);
  275. dwc_dostart(dwc, dwc_first_active(dwc));
  276. }
  277. }
  278. static void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
  279. {
  280. dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
  281. " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
  282. lli->sar, lli->dar, lli->llp,
  283. lli->ctlhi, lli->ctllo);
  284. }
  285. static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
  286. {
  287. struct dw_desc *bad_desc;
  288. struct dw_desc *child;
  289. dwc_scan_descriptors(dw, dwc);
  290. /*
  291. * The descriptor currently at the head of the active list is
  292. * borked. Since we don't have any way to report errors, we'll
  293. * just have to scream loudly and try to carry on.
  294. */
  295. bad_desc = dwc_first_active(dwc);
  296. list_del_init(&bad_desc->desc_node);
  297. list_move(dwc->queue.next, dwc->active_list.prev);
  298. /* Clear the error flag and try to restart the controller */
  299. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  300. if (!list_empty(&dwc->active_list))
  301. dwc_dostart(dwc, dwc_first_active(dwc));
  302. /*
  303. * KERN_CRITICAL may seem harsh, but since this only happens
  304. * when someone submits a bad physical address in a
  305. * descriptor, we should consider ourselves lucky that the
  306. * controller flagged an error instead of scribbling over
  307. * random memory locations.
  308. */
  309. dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
  310. "Bad descriptor submitted for DMA!\n");
  311. dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
  312. " cookie: %d\n", bad_desc->txd.cookie);
  313. dwc_dump_lli(dwc, &bad_desc->lli);
  314. list_for_each_entry(child, &bad_desc->tx_list, desc_node)
  315. dwc_dump_lli(dwc, &child->lli);
  316. /* Pretend the descriptor completed successfully */
  317. dwc_descriptor_complete(dwc, bad_desc);
  318. }
  319. /* --------------------- Cyclic DMA API extensions -------------------- */
  320. inline dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
  321. {
  322. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  323. return channel_readl(dwc, SAR);
  324. }
  325. EXPORT_SYMBOL(dw_dma_get_src_addr);
  326. inline dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
  327. {
  328. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  329. return channel_readl(dwc, DAR);
  330. }
  331. EXPORT_SYMBOL(dw_dma_get_dst_addr);
  332. /* called with dwc->lock held and all DMAC interrupts disabled */
  333. static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
  334. u32 status_block, u32 status_err, u32 status_xfer)
  335. {
  336. if (status_block & dwc->mask) {
  337. void (*callback)(void *param);
  338. void *callback_param;
  339. dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
  340. channel_readl(dwc, LLP));
  341. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  342. callback = dwc->cdesc->period_callback;
  343. callback_param = dwc->cdesc->period_callback_param;
  344. if (callback) {
  345. spin_unlock(&dwc->lock);
  346. callback(callback_param);
  347. spin_lock(&dwc->lock);
  348. }
  349. }
  350. /*
  351. * Error and transfer complete are highly unlikely, and will most
  352. * likely be due to a configuration error by the user.
  353. */
  354. if (unlikely(status_err & dwc->mask) ||
  355. unlikely(status_xfer & dwc->mask)) {
  356. int i;
  357. dev_err(chan2dev(&dwc->chan), "cyclic DMA unexpected %s "
  358. "interrupt, stopping DMA transfer\n",
  359. status_xfer ? "xfer" : "error");
  360. dev_err(chan2dev(&dwc->chan),
  361. " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
  362. channel_readl(dwc, SAR),
  363. channel_readl(dwc, DAR),
  364. channel_readl(dwc, LLP),
  365. channel_readl(dwc, CTL_HI),
  366. channel_readl(dwc, CTL_LO));
  367. channel_clear_bit(dw, CH_EN, dwc->mask);
  368. while (dma_readl(dw, CH_EN) & dwc->mask)
  369. cpu_relax();
  370. /* make sure DMA does not restart by loading a new list */
  371. channel_writel(dwc, LLP, 0);
  372. channel_writel(dwc, CTL_LO, 0);
  373. channel_writel(dwc, CTL_HI, 0);
  374. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  375. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  376. dma_writel(dw, CLEAR.XFER, dwc->mask);
  377. for (i = 0; i < dwc->cdesc->periods; i++)
  378. dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
  379. }
  380. }
  381. /* ------------------------------------------------------------------------- */
  382. static void dw_dma_tasklet(unsigned long data)
  383. {
  384. struct dw_dma *dw = (struct dw_dma *)data;
  385. struct dw_dma_chan *dwc;
  386. u32 status_block;
  387. u32 status_xfer;
  388. u32 status_err;
  389. int i;
  390. status_block = dma_readl(dw, RAW.BLOCK);
  391. status_xfer = dma_readl(dw, RAW.XFER);
  392. status_err = dma_readl(dw, RAW.ERROR);
  393. dev_vdbg(dw->dma.dev, "tasklet: status_block=%x status_err=%x\n",
  394. status_block, status_err);
  395. for (i = 0; i < dw->dma.chancnt; i++) {
  396. dwc = &dw->chan[i];
  397. spin_lock(&dwc->lock);
  398. if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
  399. dwc_handle_cyclic(dw, dwc, status_block, status_err,
  400. status_xfer);
  401. else if (status_err & (1 << i))
  402. dwc_handle_error(dw, dwc);
  403. else if ((status_block | status_xfer) & (1 << i))
  404. dwc_scan_descriptors(dw, dwc);
  405. spin_unlock(&dwc->lock);
  406. }
  407. /*
  408. * Re-enable interrupts. Block Complete interrupts are only
  409. * enabled if the INT_EN bit in the descriptor is set. This
  410. * will trigger a scan before the whole list is done.
  411. */
  412. channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
  413. channel_set_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  414. channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
  415. }
  416. static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
  417. {
  418. struct dw_dma *dw = dev_id;
  419. u32 status;
  420. dev_vdbg(dw->dma.dev, "interrupt: status=0x%x\n",
  421. dma_readl(dw, STATUS_INT));
  422. /*
  423. * Just disable the interrupts. We'll turn them back on in the
  424. * softirq handler.
  425. */
  426. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  427. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  428. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  429. status = dma_readl(dw, STATUS_INT);
  430. if (status) {
  431. dev_err(dw->dma.dev,
  432. "BUG: Unexpected interrupts pending: 0x%x\n",
  433. status);
  434. /* Try to recover */
  435. channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
  436. channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1);
  437. channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
  438. channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
  439. channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
  440. }
  441. tasklet_schedule(&dw->tasklet);
  442. return IRQ_HANDLED;
  443. }
  444. /*----------------------------------------------------------------------*/
  445. static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
  446. {
  447. struct dw_desc *desc = txd_to_dw_desc(tx);
  448. struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
  449. dma_cookie_t cookie;
  450. spin_lock_bh(&dwc->lock);
  451. cookie = dwc_assign_cookie(dwc, desc);
  452. /*
  453. * REVISIT: We should attempt to chain as many descriptors as
  454. * possible, perhaps even appending to those already submitted
  455. * for DMA. But this is hard to do in a race-free manner.
  456. */
  457. if (list_empty(&dwc->active_list)) {
  458. dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
  459. desc->txd.cookie);
  460. list_add_tail(&desc->desc_node, &dwc->active_list);
  461. dwc_dostart(dwc, dwc_first_active(dwc));
  462. } else {
  463. dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
  464. desc->txd.cookie);
  465. list_add_tail(&desc->desc_node, &dwc->queue);
  466. }
  467. spin_unlock_bh(&dwc->lock);
  468. return cookie;
  469. }
  470. static struct dma_async_tx_descriptor *
  471. dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  472. size_t len, unsigned long flags)
  473. {
  474. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  475. struct dw_desc *desc;
  476. struct dw_desc *first;
  477. struct dw_desc *prev;
  478. size_t xfer_count;
  479. size_t offset;
  480. unsigned int src_width;
  481. unsigned int dst_width;
  482. u32 ctllo;
  483. dev_vdbg(chan2dev(chan), "prep_dma_memcpy d0x%x s0x%x l0x%zx f0x%lx\n",
  484. dest, src, len, flags);
  485. if (unlikely(!len)) {
  486. dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
  487. return NULL;
  488. }
  489. /*
  490. * We can be a lot more clever here, but this should take care
  491. * of the most common optimization.
  492. */
  493. if (!((src | dest | len) & 7))
  494. src_width = dst_width = 3;
  495. else if (!((src | dest | len) & 3))
  496. src_width = dst_width = 2;
  497. else if (!((src | dest | len) & 1))
  498. src_width = dst_width = 1;
  499. else
  500. src_width = dst_width = 0;
  501. ctllo = DWC_DEFAULT_CTLLO(chan->private)
  502. | DWC_CTLL_DST_WIDTH(dst_width)
  503. | DWC_CTLL_SRC_WIDTH(src_width)
  504. | DWC_CTLL_DST_INC
  505. | DWC_CTLL_SRC_INC
  506. | DWC_CTLL_FC_M2M;
  507. prev = first = NULL;
  508. for (offset = 0; offset < len; offset += xfer_count << src_width) {
  509. xfer_count = min_t(size_t, (len - offset) >> src_width,
  510. DWC_MAX_COUNT);
  511. desc = dwc_desc_get(dwc);
  512. if (!desc)
  513. goto err_desc_get;
  514. desc->lli.sar = src + offset;
  515. desc->lli.dar = dest + offset;
  516. desc->lli.ctllo = ctllo;
  517. desc->lli.ctlhi = xfer_count;
  518. if (!first) {
  519. first = desc;
  520. } else {
  521. prev->lli.llp = desc->txd.phys;
  522. dma_sync_single_for_device(chan2parent(chan),
  523. prev->txd.phys, sizeof(prev->lli),
  524. DMA_TO_DEVICE);
  525. list_add_tail(&desc->desc_node,
  526. &first->tx_list);
  527. }
  528. prev = desc;
  529. }
  530. if (flags & DMA_PREP_INTERRUPT)
  531. /* Trigger interrupt after last block */
  532. prev->lli.ctllo |= DWC_CTLL_INT_EN;
  533. prev->lli.llp = 0;
  534. dma_sync_single_for_device(chan2parent(chan),
  535. prev->txd.phys, sizeof(prev->lli),
  536. DMA_TO_DEVICE);
  537. first->txd.flags = flags;
  538. first->len = len;
  539. return &first->txd;
  540. err_desc_get:
  541. dwc_desc_put(dwc, first);
  542. return NULL;
  543. }
  544. static struct dma_async_tx_descriptor *
  545. dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  546. unsigned int sg_len, enum dma_data_direction direction,
  547. unsigned long flags)
  548. {
  549. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  550. struct dw_dma_slave *dws = chan->private;
  551. struct dw_desc *prev;
  552. struct dw_desc *first;
  553. u32 ctllo;
  554. dma_addr_t reg;
  555. unsigned int reg_width;
  556. unsigned int mem_width;
  557. unsigned int i;
  558. struct scatterlist *sg;
  559. size_t total_len = 0;
  560. dev_vdbg(chan2dev(chan), "prep_dma_slave\n");
  561. if (unlikely(!dws || !sg_len))
  562. return NULL;
  563. reg_width = dws->reg_width;
  564. prev = first = NULL;
  565. switch (direction) {
  566. case DMA_TO_DEVICE:
  567. ctllo = (DWC_DEFAULT_CTLLO(chan->private)
  568. | DWC_CTLL_DST_WIDTH(reg_width)
  569. | DWC_CTLL_DST_FIX
  570. | DWC_CTLL_SRC_INC
  571. | DWC_CTLL_FC_M2P);
  572. reg = dws->tx_reg;
  573. for_each_sg(sgl, sg, sg_len, i) {
  574. struct dw_desc *desc;
  575. u32 len;
  576. u32 mem;
  577. desc = dwc_desc_get(dwc);
  578. if (!desc) {
  579. dev_err(chan2dev(chan),
  580. "not enough descriptors available\n");
  581. goto err_desc_get;
  582. }
  583. mem = sg_phys(sg);
  584. len = sg_dma_len(sg);
  585. mem_width = 2;
  586. if (unlikely(mem & 3 || len & 3))
  587. mem_width = 0;
  588. desc->lli.sar = mem;
  589. desc->lli.dar = reg;
  590. desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
  591. desc->lli.ctlhi = len >> mem_width;
  592. if (!first) {
  593. first = desc;
  594. } else {
  595. prev->lli.llp = desc->txd.phys;
  596. dma_sync_single_for_device(chan2parent(chan),
  597. prev->txd.phys,
  598. sizeof(prev->lli),
  599. DMA_TO_DEVICE);
  600. list_add_tail(&desc->desc_node,
  601. &first->tx_list);
  602. }
  603. prev = desc;
  604. total_len += len;
  605. }
  606. break;
  607. case DMA_FROM_DEVICE:
  608. ctllo = (DWC_DEFAULT_CTLLO(chan->private)
  609. | DWC_CTLL_SRC_WIDTH(reg_width)
  610. | DWC_CTLL_DST_INC
  611. | DWC_CTLL_SRC_FIX
  612. | DWC_CTLL_FC_P2M);
  613. reg = dws->rx_reg;
  614. for_each_sg(sgl, sg, sg_len, i) {
  615. struct dw_desc *desc;
  616. u32 len;
  617. u32 mem;
  618. desc = dwc_desc_get(dwc);
  619. if (!desc) {
  620. dev_err(chan2dev(chan),
  621. "not enough descriptors available\n");
  622. goto err_desc_get;
  623. }
  624. mem = sg_phys(sg);
  625. len = sg_dma_len(sg);
  626. mem_width = 2;
  627. if (unlikely(mem & 3 || len & 3))
  628. mem_width = 0;
  629. desc->lli.sar = reg;
  630. desc->lli.dar = mem;
  631. desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
  632. desc->lli.ctlhi = len >> reg_width;
  633. if (!first) {
  634. first = desc;
  635. } else {
  636. prev->lli.llp = desc->txd.phys;
  637. dma_sync_single_for_device(chan2parent(chan),
  638. prev->txd.phys,
  639. sizeof(prev->lli),
  640. DMA_TO_DEVICE);
  641. list_add_tail(&desc->desc_node,
  642. &first->tx_list);
  643. }
  644. prev = desc;
  645. total_len += len;
  646. }
  647. break;
  648. default:
  649. return NULL;
  650. }
  651. if (flags & DMA_PREP_INTERRUPT)
  652. /* Trigger interrupt after last block */
  653. prev->lli.ctllo |= DWC_CTLL_INT_EN;
  654. prev->lli.llp = 0;
  655. dma_sync_single_for_device(chan2parent(chan),
  656. prev->txd.phys, sizeof(prev->lli),
  657. DMA_TO_DEVICE);
  658. first->len = total_len;
  659. return &first->txd;
  660. err_desc_get:
  661. dwc_desc_put(dwc, first);
  662. return NULL;
  663. }
  664. static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  665. unsigned long arg)
  666. {
  667. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  668. struct dw_dma *dw = to_dw_dma(chan->device);
  669. struct dw_desc *desc, *_desc;
  670. LIST_HEAD(list);
  671. /* Only supports DMA_TERMINATE_ALL */
  672. if (cmd != DMA_TERMINATE_ALL)
  673. return -ENXIO;
  674. /*
  675. * This is only called when something went wrong elsewhere, so
  676. * we don't really care about the data. Just disable the
  677. * channel. We still have to poll the channel enable bit due
  678. * to AHB/HSB limitations.
  679. */
  680. spin_lock_bh(&dwc->lock);
  681. channel_clear_bit(dw, CH_EN, dwc->mask);
  682. while (dma_readl(dw, CH_EN) & dwc->mask)
  683. cpu_relax();
  684. /* active_list entries will end up before queued entries */
  685. list_splice_init(&dwc->queue, &list);
  686. list_splice_init(&dwc->active_list, &list);
  687. spin_unlock_bh(&dwc->lock);
  688. /* Flush all pending and queued descriptors */
  689. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  690. dwc_descriptor_complete(dwc, desc);
  691. return 0;
  692. }
  693. static enum dma_status
  694. dwc_tx_status(struct dma_chan *chan,
  695. dma_cookie_t cookie,
  696. struct dma_tx_state *txstate)
  697. {
  698. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  699. dma_cookie_t last_used;
  700. dma_cookie_t last_complete;
  701. int ret;
  702. last_complete = dwc->completed;
  703. last_used = chan->cookie;
  704. ret = dma_async_is_complete(cookie, last_complete, last_used);
  705. if (ret != DMA_SUCCESS) {
  706. spin_lock_bh(&dwc->lock);
  707. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  708. spin_unlock_bh(&dwc->lock);
  709. last_complete = dwc->completed;
  710. last_used = chan->cookie;
  711. ret = dma_async_is_complete(cookie, last_complete, last_used);
  712. }
  713. dma_set_tx_state(txstate, last_complete, last_used, 0);
  714. return ret;
  715. }
  716. static void dwc_issue_pending(struct dma_chan *chan)
  717. {
  718. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  719. spin_lock_bh(&dwc->lock);
  720. if (!list_empty(&dwc->queue))
  721. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  722. spin_unlock_bh(&dwc->lock);
  723. }
  724. static int dwc_alloc_chan_resources(struct dma_chan *chan)
  725. {
  726. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  727. struct dw_dma *dw = to_dw_dma(chan->device);
  728. struct dw_desc *desc;
  729. struct dw_dma_slave *dws;
  730. int i;
  731. u32 cfghi;
  732. u32 cfglo;
  733. dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
  734. /* ASSERT: channel is idle */
  735. if (dma_readl(dw, CH_EN) & dwc->mask) {
  736. dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
  737. return -EIO;
  738. }
  739. dwc->completed = chan->cookie = 1;
  740. cfghi = DWC_CFGH_FIFO_MODE;
  741. cfglo = 0;
  742. dws = chan->private;
  743. if (dws) {
  744. /*
  745. * We need controller-specific data to set up slave
  746. * transfers.
  747. */
  748. BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev);
  749. cfghi = dws->cfg_hi;
  750. cfglo = dws->cfg_lo;
  751. }
  752. channel_writel(dwc, CFG_LO, cfglo);
  753. channel_writel(dwc, CFG_HI, cfghi);
  754. /*
  755. * NOTE: some controllers may have additional features that we
  756. * need to initialize here, like "scatter-gather" (which
  757. * doesn't mean what you think it means), and status writeback.
  758. */
  759. spin_lock_bh(&dwc->lock);
  760. i = dwc->descs_allocated;
  761. while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
  762. spin_unlock_bh(&dwc->lock);
  763. desc = kzalloc(sizeof(struct dw_desc), GFP_KERNEL);
  764. if (!desc) {
  765. dev_info(chan2dev(chan),
  766. "only allocated %d descriptors\n", i);
  767. spin_lock_bh(&dwc->lock);
  768. break;
  769. }
  770. INIT_LIST_HEAD(&desc->tx_list);
  771. dma_async_tx_descriptor_init(&desc->txd, chan);
  772. desc->txd.tx_submit = dwc_tx_submit;
  773. desc->txd.flags = DMA_CTRL_ACK;
  774. desc->txd.phys = dma_map_single(chan2parent(chan), &desc->lli,
  775. sizeof(desc->lli), DMA_TO_DEVICE);
  776. dwc_desc_put(dwc, desc);
  777. spin_lock_bh(&dwc->lock);
  778. i = ++dwc->descs_allocated;
  779. }
  780. /* Enable interrupts */
  781. channel_set_bit(dw, MASK.XFER, dwc->mask);
  782. channel_set_bit(dw, MASK.BLOCK, dwc->mask);
  783. channel_set_bit(dw, MASK.ERROR, dwc->mask);
  784. spin_unlock_bh(&dwc->lock);
  785. dev_dbg(chan2dev(chan),
  786. "alloc_chan_resources allocated %d descriptors\n", i);
  787. return i;
  788. }
  789. static void dwc_free_chan_resources(struct dma_chan *chan)
  790. {
  791. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  792. struct dw_dma *dw = to_dw_dma(chan->device);
  793. struct dw_desc *desc, *_desc;
  794. LIST_HEAD(list);
  795. dev_dbg(chan2dev(chan), "free_chan_resources (descs allocated=%u)\n",
  796. dwc->descs_allocated);
  797. /* ASSERT: channel is idle */
  798. BUG_ON(!list_empty(&dwc->active_list));
  799. BUG_ON(!list_empty(&dwc->queue));
  800. BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
  801. spin_lock_bh(&dwc->lock);
  802. list_splice_init(&dwc->free_list, &list);
  803. dwc->descs_allocated = 0;
  804. /* Disable interrupts */
  805. channel_clear_bit(dw, MASK.XFER, dwc->mask);
  806. channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
  807. channel_clear_bit(dw, MASK.ERROR, dwc->mask);
  808. spin_unlock_bh(&dwc->lock);
  809. list_for_each_entry_safe(desc, _desc, &list, desc_node) {
  810. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  811. dma_unmap_single(chan2parent(chan), desc->txd.phys,
  812. sizeof(desc->lli), DMA_TO_DEVICE);
  813. kfree(desc);
  814. }
  815. dev_vdbg(chan2dev(chan), "free_chan_resources done\n");
  816. }
  817. /* --------------------- Cyclic DMA API extensions -------------------- */
  818. /**
  819. * dw_dma_cyclic_start - start the cyclic DMA transfer
  820. * @chan: the DMA channel to start
  821. *
  822. * Must be called with soft interrupts disabled. Returns zero on success or
  823. * -errno on failure.
  824. */
  825. int dw_dma_cyclic_start(struct dma_chan *chan)
  826. {
  827. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  828. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  829. if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
  830. dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
  831. return -ENODEV;
  832. }
  833. spin_lock(&dwc->lock);
  834. /* assert channel is idle */
  835. if (dma_readl(dw, CH_EN) & dwc->mask) {
  836. dev_err(chan2dev(&dwc->chan),
  837. "BUG: Attempted to start non-idle channel\n");
  838. dev_err(chan2dev(&dwc->chan),
  839. " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
  840. channel_readl(dwc, SAR),
  841. channel_readl(dwc, DAR),
  842. channel_readl(dwc, LLP),
  843. channel_readl(dwc, CTL_HI),
  844. channel_readl(dwc, CTL_LO));
  845. spin_unlock(&dwc->lock);
  846. return -EBUSY;
  847. }
  848. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  849. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  850. dma_writel(dw, CLEAR.XFER, dwc->mask);
  851. /* setup DMAC channel registers */
  852. channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
  853. channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  854. channel_writel(dwc, CTL_HI, 0);
  855. channel_set_bit(dw, CH_EN, dwc->mask);
  856. spin_unlock(&dwc->lock);
  857. return 0;
  858. }
  859. EXPORT_SYMBOL(dw_dma_cyclic_start);
  860. /**
  861. * dw_dma_cyclic_stop - stop the cyclic DMA transfer
  862. * @chan: the DMA channel to stop
  863. *
  864. * Must be called with soft interrupts disabled.
  865. */
  866. void dw_dma_cyclic_stop(struct dma_chan *chan)
  867. {
  868. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  869. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  870. spin_lock(&dwc->lock);
  871. channel_clear_bit(dw, CH_EN, dwc->mask);
  872. while (dma_readl(dw, CH_EN) & dwc->mask)
  873. cpu_relax();
  874. spin_unlock(&dwc->lock);
  875. }
  876. EXPORT_SYMBOL(dw_dma_cyclic_stop);
  877. /**
  878. * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
  879. * @chan: the DMA channel to prepare
  880. * @buf_addr: physical DMA address where the buffer starts
  881. * @buf_len: total number of bytes for the entire buffer
  882. * @period_len: number of bytes for each period
  883. * @direction: transfer direction, to or from device
  884. *
  885. * Must be called before trying to start the transfer. Returns a valid struct
  886. * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
  887. */
  888. struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
  889. dma_addr_t buf_addr, size_t buf_len, size_t period_len,
  890. enum dma_data_direction direction)
  891. {
  892. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  893. struct dw_cyclic_desc *cdesc;
  894. struct dw_cyclic_desc *retval = NULL;
  895. struct dw_desc *desc;
  896. struct dw_desc *last = NULL;
  897. struct dw_dma_slave *dws = chan->private;
  898. unsigned long was_cyclic;
  899. unsigned int reg_width;
  900. unsigned int periods;
  901. unsigned int i;
  902. spin_lock_bh(&dwc->lock);
  903. if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
  904. spin_unlock_bh(&dwc->lock);
  905. dev_dbg(chan2dev(&dwc->chan),
  906. "queue and/or active list are not empty\n");
  907. return ERR_PTR(-EBUSY);
  908. }
  909. was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  910. spin_unlock_bh(&dwc->lock);
  911. if (was_cyclic) {
  912. dev_dbg(chan2dev(&dwc->chan),
  913. "channel already prepared for cyclic DMA\n");
  914. return ERR_PTR(-EBUSY);
  915. }
  916. retval = ERR_PTR(-EINVAL);
  917. reg_width = dws->reg_width;
  918. periods = buf_len / period_len;
  919. /* Check for too big/unaligned periods and unaligned DMA buffer. */
  920. if (period_len > (DWC_MAX_COUNT << reg_width))
  921. goto out_err;
  922. if (unlikely(period_len & ((1 << reg_width) - 1)))
  923. goto out_err;
  924. if (unlikely(buf_addr & ((1 << reg_width) - 1)))
  925. goto out_err;
  926. if (unlikely(!(direction & (DMA_TO_DEVICE | DMA_FROM_DEVICE))))
  927. goto out_err;
  928. retval = ERR_PTR(-ENOMEM);
  929. if (periods > NR_DESCS_PER_CHANNEL)
  930. goto out_err;
  931. cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
  932. if (!cdesc)
  933. goto out_err;
  934. cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
  935. if (!cdesc->desc)
  936. goto out_err_alloc;
  937. for (i = 0; i < periods; i++) {
  938. desc = dwc_desc_get(dwc);
  939. if (!desc)
  940. goto out_err_desc_get;
  941. switch (direction) {
  942. case DMA_TO_DEVICE:
  943. desc->lli.dar = dws->tx_reg;
  944. desc->lli.sar = buf_addr + (period_len * i);
  945. desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private)
  946. | DWC_CTLL_DST_WIDTH(reg_width)
  947. | DWC_CTLL_SRC_WIDTH(reg_width)
  948. | DWC_CTLL_DST_FIX
  949. | DWC_CTLL_SRC_INC
  950. | DWC_CTLL_FC_M2P
  951. | DWC_CTLL_INT_EN);
  952. break;
  953. case DMA_FROM_DEVICE:
  954. desc->lli.dar = buf_addr + (period_len * i);
  955. desc->lli.sar = dws->rx_reg;
  956. desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private)
  957. | DWC_CTLL_SRC_WIDTH(reg_width)
  958. | DWC_CTLL_DST_WIDTH(reg_width)
  959. | DWC_CTLL_DST_INC
  960. | DWC_CTLL_SRC_FIX
  961. | DWC_CTLL_FC_P2M
  962. | DWC_CTLL_INT_EN);
  963. break;
  964. default:
  965. break;
  966. }
  967. desc->lli.ctlhi = (period_len >> reg_width);
  968. cdesc->desc[i] = desc;
  969. if (last) {
  970. last->lli.llp = desc->txd.phys;
  971. dma_sync_single_for_device(chan2parent(chan),
  972. last->txd.phys, sizeof(last->lli),
  973. DMA_TO_DEVICE);
  974. }
  975. last = desc;
  976. }
  977. /* lets make a cyclic list */
  978. last->lli.llp = cdesc->desc[0]->txd.phys;
  979. dma_sync_single_for_device(chan2parent(chan), last->txd.phys,
  980. sizeof(last->lli), DMA_TO_DEVICE);
  981. dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%08x len %zu "
  982. "period %zu periods %d\n", buf_addr, buf_len,
  983. period_len, periods);
  984. cdesc->periods = periods;
  985. dwc->cdesc = cdesc;
  986. return cdesc;
  987. out_err_desc_get:
  988. while (i--)
  989. dwc_desc_put(dwc, cdesc->desc[i]);
  990. out_err_alloc:
  991. kfree(cdesc);
  992. out_err:
  993. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  994. return (struct dw_cyclic_desc *)retval;
  995. }
  996. EXPORT_SYMBOL(dw_dma_cyclic_prep);
  997. /**
  998. * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
  999. * @chan: the DMA channel to free
  1000. */
  1001. void dw_dma_cyclic_free(struct dma_chan *chan)
  1002. {
  1003. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1004. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  1005. struct dw_cyclic_desc *cdesc = dwc->cdesc;
  1006. int i;
  1007. dev_dbg(chan2dev(&dwc->chan), "cyclic free\n");
  1008. if (!cdesc)
  1009. return;
  1010. spin_lock_bh(&dwc->lock);
  1011. channel_clear_bit(dw, CH_EN, dwc->mask);
  1012. while (dma_readl(dw, CH_EN) & dwc->mask)
  1013. cpu_relax();
  1014. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  1015. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  1016. dma_writel(dw, CLEAR.XFER, dwc->mask);
  1017. spin_unlock_bh(&dwc->lock);
  1018. for (i = 0; i < cdesc->periods; i++)
  1019. dwc_desc_put(dwc, cdesc->desc[i]);
  1020. kfree(cdesc->desc);
  1021. kfree(cdesc);
  1022. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1023. }
  1024. EXPORT_SYMBOL(dw_dma_cyclic_free);
  1025. /*----------------------------------------------------------------------*/
  1026. static void dw_dma_off(struct dw_dma *dw)
  1027. {
  1028. dma_writel(dw, CFG, 0);
  1029. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  1030. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  1031. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  1032. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  1033. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  1034. while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
  1035. cpu_relax();
  1036. }
  1037. static int __init dw_probe(struct platform_device *pdev)
  1038. {
  1039. struct dw_dma_platform_data *pdata;
  1040. struct resource *io;
  1041. struct dw_dma *dw;
  1042. size_t size;
  1043. int irq;
  1044. int err;
  1045. int i;
  1046. pdata = pdev->dev.platform_data;
  1047. if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
  1048. return -EINVAL;
  1049. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1050. if (!io)
  1051. return -EINVAL;
  1052. irq = platform_get_irq(pdev, 0);
  1053. if (irq < 0)
  1054. return irq;
  1055. size = sizeof(struct dw_dma);
  1056. size += pdata->nr_channels * sizeof(struct dw_dma_chan);
  1057. dw = kzalloc(size, GFP_KERNEL);
  1058. if (!dw)
  1059. return -ENOMEM;
  1060. if (!request_mem_region(io->start, DW_REGLEN, pdev->dev.driver->name)) {
  1061. err = -EBUSY;
  1062. goto err_kfree;
  1063. }
  1064. dw->regs = ioremap(io->start, DW_REGLEN);
  1065. if (!dw->regs) {
  1066. err = -ENOMEM;
  1067. goto err_release_r;
  1068. }
  1069. dw->clk = clk_get(&pdev->dev, "hclk");
  1070. if (IS_ERR(dw->clk)) {
  1071. err = PTR_ERR(dw->clk);
  1072. goto err_clk;
  1073. }
  1074. clk_enable(dw->clk);
  1075. /* force dma off, just in case */
  1076. dw_dma_off(dw);
  1077. err = request_irq(irq, dw_dma_interrupt, 0, "dw_dmac", dw);
  1078. if (err)
  1079. goto err_irq;
  1080. platform_set_drvdata(pdev, dw);
  1081. tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
  1082. dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
  1083. INIT_LIST_HEAD(&dw->dma.channels);
  1084. for (i = 0; i < pdata->nr_channels; i++, dw->dma.chancnt++) {
  1085. struct dw_dma_chan *dwc = &dw->chan[i];
  1086. dwc->chan.device = &dw->dma;
  1087. dwc->chan.cookie = dwc->completed = 1;
  1088. dwc->chan.chan_id = i;
  1089. list_add_tail(&dwc->chan.device_node, &dw->dma.channels);
  1090. dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
  1091. spin_lock_init(&dwc->lock);
  1092. dwc->mask = 1 << i;
  1093. INIT_LIST_HEAD(&dwc->active_list);
  1094. INIT_LIST_HEAD(&dwc->queue);
  1095. INIT_LIST_HEAD(&dwc->free_list);
  1096. channel_clear_bit(dw, CH_EN, dwc->mask);
  1097. }
  1098. /* Clear/disable all interrupts on all channels. */
  1099. dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
  1100. dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
  1101. dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
  1102. dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
  1103. dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
  1104. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  1105. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  1106. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  1107. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  1108. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  1109. dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
  1110. dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
  1111. if (pdata->is_private)
  1112. dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
  1113. dw->dma.dev = &pdev->dev;
  1114. dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
  1115. dw->dma.device_free_chan_resources = dwc_free_chan_resources;
  1116. dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
  1117. dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
  1118. dw->dma.device_control = dwc_control;
  1119. dw->dma.device_tx_status = dwc_tx_status;
  1120. dw->dma.device_issue_pending = dwc_issue_pending;
  1121. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  1122. printk(KERN_INFO "%s: DesignWare DMA Controller, %d channels\n",
  1123. dev_name(&pdev->dev), dw->dma.chancnt);
  1124. dma_async_device_register(&dw->dma);
  1125. return 0;
  1126. err_irq:
  1127. clk_disable(dw->clk);
  1128. clk_put(dw->clk);
  1129. err_clk:
  1130. iounmap(dw->regs);
  1131. dw->regs = NULL;
  1132. err_release_r:
  1133. release_resource(io);
  1134. err_kfree:
  1135. kfree(dw);
  1136. return err;
  1137. }
  1138. static int __exit dw_remove(struct platform_device *pdev)
  1139. {
  1140. struct dw_dma *dw = platform_get_drvdata(pdev);
  1141. struct dw_dma_chan *dwc, *_dwc;
  1142. struct resource *io;
  1143. dw_dma_off(dw);
  1144. dma_async_device_unregister(&dw->dma);
  1145. free_irq(platform_get_irq(pdev, 0), dw);
  1146. tasklet_kill(&dw->tasklet);
  1147. list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
  1148. chan.device_node) {
  1149. list_del(&dwc->chan.device_node);
  1150. channel_clear_bit(dw, CH_EN, dwc->mask);
  1151. }
  1152. clk_disable(dw->clk);
  1153. clk_put(dw->clk);
  1154. iounmap(dw->regs);
  1155. dw->regs = NULL;
  1156. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1157. release_mem_region(io->start, DW_REGLEN);
  1158. kfree(dw);
  1159. return 0;
  1160. }
  1161. static void dw_shutdown(struct platform_device *pdev)
  1162. {
  1163. struct dw_dma *dw = platform_get_drvdata(pdev);
  1164. dw_dma_off(platform_get_drvdata(pdev));
  1165. clk_disable(dw->clk);
  1166. }
  1167. static int dw_suspend_noirq(struct device *dev)
  1168. {
  1169. struct platform_device *pdev = to_platform_device(dev);
  1170. struct dw_dma *dw = platform_get_drvdata(pdev);
  1171. dw_dma_off(platform_get_drvdata(pdev));
  1172. clk_disable(dw->clk);
  1173. return 0;
  1174. }
  1175. static int dw_resume_noirq(struct device *dev)
  1176. {
  1177. struct platform_device *pdev = to_platform_device(dev);
  1178. struct dw_dma *dw = platform_get_drvdata(pdev);
  1179. clk_enable(dw->clk);
  1180. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  1181. return 0;
  1182. }
  1183. static const struct dev_pm_ops dw_dev_pm_ops = {
  1184. .suspend_noirq = dw_suspend_noirq,
  1185. .resume_noirq = dw_resume_noirq,
  1186. };
  1187. static struct platform_driver dw_driver = {
  1188. .remove = __exit_p(dw_remove),
  1189. .shutdown = dw_shutdown,
  1190. .driver = {
  1191. .name = "dw_dmac",
  1192. .pm = &dw_dev_pm_ops,
  1193. },
  1194. };
  1195. static int __init dw_init(void)
  1196. {
  1197. return platform_driver_probe(&dw_driver, dw_probe);
  1198. }
  1199. subsys_initcall(dw_init);
  1200. static void __exit dw_exit(void)
  1201. {
  1202. platform_driver_unregister(&dw_driver);
  1203. }
  1204. module_exit(dw_exit);
  1205. MODULE_LICENSE("GPL v2");
  1206. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
  1207. MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");