dw_dmac.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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. /* NOTE: DMS+SMS is system-specific. We should get this information
  33. * from the platform code somehow.
  34. */
  35. #define DWC_DEFAULT_CTLLO (DWC_CTLL_DST_MSIZE(0) \
  36. | DWC_CTLL_SRC_MSIZE(0) \
  37. | DWC_CTLL_DMS(0) \
  38. | DWC_CTLL_SMS(1) \
  39. | DWC_CTLL_LLP_D_EN \
  40. | DWC_CTLL_LLP_S_EN)
  41. /*
  42. * This is configuration-dependent and usually a funny size like 4095.
  43. * Let's round it down to the nearest power of two.
  44. *
  45. * Note that this is a transfer count, i.e. if we transfer 32-bit
  46. * words, we can do 8192 bytes per descriptor.
  47. *
  48. * This parameter is also system-specific.
  49. */
  50. #define DWC_MAX_COUNT 2048U
  51. /*
  52. * Number of descriptors to allocate for each channel. This should be
  53. * made configurable somehow; preferably, the clients (at least the
  54. * ones using slave transfers) should be able to give us a hint.
  55. */
  56. #define NR_DESCS_PER_CHANNEL 64
  57. /*----------------------------------------------------------------------*/
  58. /*
  59. * Because we're not relying on writeback from the controller (it may not
  60. * even be configured into the core!) we don't need to use dma_pool. These
  61. * descriptors -- and associated data -- are cacheable. We do need to make
  62. * sure their dcache entries are written back before handing them off to
  63. * the controller, though.
  64. */
  65. static struct device *chan2dev(struct dma_chan *chan)
  66. {
  67. return &chan->dev->device;
  68. }
  69. static struct device *chan2parent(struct dma_chan *chan)
  70. {
  71. return chan->dev->device.parent;
  72. }
  73. static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
  74. {
  75. return list_entry(dwc->active_list.next, struct dw_desc, desc_node);
  76. }
  77. static struct dw_desc *dwc_first_queued(struct dw_dma_chan *dwc)
  78. {
  79. return list_entry(dwc->queue.next, struct dw_desc, desc_node);
  80. }
  81. static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
  82. {
  83. struct dw_desc *desc, *_desc;
  84. struct dw_desc *ret = NULL;
  85. unsigned int i = 0;
  86. spin_lock_bh(&dwc->lock);
  87. list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
  88. if (async_tx_test_ack(&desc->txd)) {
  89. list_del(&desc->desc_node);
  90. ret = desc;
  91. break;
  92. }
  93. dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc);
  94. i++;
  95. }
  96. spin_unlock_bh(&dwc->lock);
  97. dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i);
  98. return ret;
  99. }
  100. static void dwc_sync_desc_for_cpu(struct dw_dma_chan *dwc, struct dw_desc *desc)
  101. {
  102. struct dw_desc *child;
  103. list_for_each_entry(child, &desc->txd.tx_list, desc_node)
  104. dma_sync_single_for_cpu(chan2parent(&dwc->chan),
  105. child->txd.phys, sizeof(child->lli),
  106. DMA_TO_DEVICE);
  107. dma_sync_single_for_cpu(chan2parent(&dwc->chan),
  108. desc->txd.phys, sizeof(desc->lli),
  109. DMA_TO_DEVICE);
  110. }
  111. /*
  112. * Move a descriptor, including any children, to the free list.
  113. * `desc' must not be on any lists.
  114. */
  115. static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
  116. {
  117. if (desc) {
  118. struct dw_desc *child;
  119. dwc_sync_desc_for_cpu(dwc, desc);
  120. spin_lock_bh(&dwc->lock);
  121. list_for_each_entry(child, &desc->txd.tx_list, desc_node)
  122. dev_vdbg(chan2dev(&dwc->chan),
  123. "moving child desc %p to freelist\n",
  124. child);
  125. list_splice_init(&desc->txd.tx_list, &dwc->free_list);
  126. dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc);
  127. list_add(&desc->desc_node, &dwc->free_list);
  128. spin_unlock_bh(&dwc->lock);
  129. }
  130. }
  131. /* Called with dwc->lock held and bh disabled */
  132. static dma_cookie_t
  133. dwc_assign_cookie(struct dw_dma_chan *dwc, struct dw_desc *desc)
  134. {
  135. dma_cookie_t cookie = dwc->chan.cookie;
  136. if (++cookie < 0)
  137. cookie = 1;
  138. dwc->chan.cookie = cookie;
  139. desc->txd.cookie = cookie;
  140. return cookie;
  141. }
  142. /*----------------------------------------------------------------------*/
  143. /* Called with dwc->lock held and bh disabled */
  144. static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
  145. {
  146. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  147. /* ASSERT: channel is idle */
  148. if (dma_readl(dw, CH_EN) & dwc->mask) {
  149. dev_err(chan2dev(&dwc->chan),
  150. "BUG: Attempted to start non-idle channel\n");
  151. dev_err(chan2dev(&dwc->chan),
  152. " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
  153. channel_readl(dwc, SAR),
  154. channel_readl(dwc, DAR),
  155. channel_readl(dwc, LLP),
  156. channel_readl(dwc, CTL_HI),
  157. channel_readl(dwc, CTL_LO));
  158. /* The tasklet will hopefully advance the queue... */
  159. return;
  160. }
  161. channel_writel(dwc, LLP, first->txd.phys);
  162. channel_writel(dwc, CTL_LO,
  163. DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  164. channel_writel(dwc, CTL_HI, 0);
  165. channel_set_bit(dw, CH_EN, dwc->mask);
  166. }
  167. /*----------------------------------------------------------------------*/
  168. static void
  169. dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc)
  170. {
  171. dma_async_tx_callback callback;
  172. void *param;
  173. struct dma_async_tx_descriptor *txd = &desc->txd;
  174. dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
  175. dwc->completed = txd->cookie;
  176. callback = txd->callback;
  177. param = txd->callback_param;
  178. dwc_sync_desc_for_cpu(dwc, desc);
  179. list_splice_init(&txd->tx_list, &dwc->free_list);
  180. list_move(&desc->desc_node, &dwc->free_list);
  181. /*
  182. * We use dma_unmap_page() regardless of how the buffers were
  183. * mapped before they were submitted...
  184. */
  185. if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP))
  186. dma_unmap_page(chan2parent(&dwc->chan), desc->lli.dar,
  187. desc->len, DMA_FROM_DEVICE);
  188. if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP))
  189. dma_unmap_page(chan2parent(&dwc->chan), desc->lli.sar,
  190. desc->len, DMA_TO_DEVICE);
  191. /*
  192. * The API requires that no submissions are done from a
  193. * callback, so we don't need to drop the lock here
  194. */
  195. if (callback)
  196. callback(param);
  197. }
  198. static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
  199. {
  200. struct dw_desc *desc, *_desc;
  201. LIST_HEAD(list);
  202. if (dma_readl(dw, CH_EN) & dwc->mask) {
  203. dev_err(chan2dev(&dwc->chan),
  204. "BUG: XFER bit set, but channel not idle!\n");
  205. /* Try to continue after resetting the channel... */
  206. channel_clear_bit(dw, CH_EN, dwc->mask);
  207. while (dma_readl(dw, CH_EN) & dwc->mask)
  208. cpu_relax();
  209. }
  210. /*
  211. * Submit queued descriptors ASAP, i.e. before we go through
  212. * the completed ones.
  213. */
  214. if (!list_empty(&dwc->queue))
  215. dwc_dostart(dwc, dwc_first_queued(dwc));
  216. list_splice_init(&dwc->active_list, &list);
  217. list_splice_init(&dwc->queue, &dwc->active_list);
  218. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  219. dwc_descriptor_complete(dwc, desc);
  220. }
  221. static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
  222. {
  223. dma_addr_t llp;
  224. struct dw_desc *desc, *_desc;
  225. struct dw_desc *child;
  226. u32 status_xfer;
  227. /*
  228. * Clear block interrupt flag before scanning so that we don't
  229. * miss any, and read LLP before RAW_XFER to ensure it is
  230. * valid if we decide to scan the list.
  231. */
  232. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  233. llp = channel_readl(dwc, LLP);
  234. status_xfer = dma_readl(dw, RAW.XFER);
  235. if (status_xfer & dwc->mask) {
  236. /* Everything we've submitted is done */
  237. dma_writel(dw, CLEAR.XFER, dwc->mask);
  238. dwc_complete_all(dw, dwc);
  239. return;
  240. }
  241. dev_vdbg(chan2dev(&dwc->chan), "scan_descriptors: llp=0x%x\n", llp);
  242. list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
  243. if (desc->lli.llp == llp)
  244. /* This one is currently in progress */
  245. return;
  246. list_for_each_entry(child, &desc->txd.tx_list, desc_node)
  247. if (child->lli.llp == llp)
  248. /* Currently in progress */
  249. return;
  250. /*
  251. * No descriptors so far seem to be in progress, i.e.
  252. * this one must be done.
  253. */
  254. dwc_descriptor_complete(dwc, desc);
  255. }
  256. dev_err(chan2dev(&dwc->chan),
  257. "BUG: All descriptors done, but channel not idle!\n");
  258. /* Try to continue after resetting the channel... */
  259. channel_clear_bit(dw, CH_EN, dwc->mask);
  260. while (dma_readl(dw, CH_EN) & dwc->mask)
  261. cpu_relax();
  262. if (!list_empty(&dwc->queue)) {
  263. dwc_dostart(dwc, dwc_first_queued(dwc));
  264. list_splice_init(&dwc->queue, &dwc->active_list);
  265. }
  266. }
  267. static void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
  268. {
  269. dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
  270. " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
  271. lli->sar, lli->dar, lli->llp,
  272. lli->ctlhi, lli->ctllo);
  273. }
  274. static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
  275. {
  276. struct dw_desc *bad_desc;
  277. struct dw_desc *child;
  278. dwc_scan_descriptors(dw, dwc);
  279. /*
  280. * The descriptor currently at the head of the active list is
  281. * borked. Since we don't have any way to report errors, we'll
  282. * just have to scream loudly and try to carry on.
  283. */
  284. bad_desc = dwc_first_active(dwc);
  285. list_del_init(&bad_desc->desc_node);
  286. list_splice_init(&dwc->queue, dwc->active_list.prev);
  287. /* Clear the error flag and try to restart the controller */
  288. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  289. if (!list_empty(&dwc->active_list))
  290. dwc_dostart(dwc, dwc_first_active(dwc));
  291. /*
  292. * KERN_CRITICAL may seem harsh, but since this only happens
  293. * when someone submits a bad physical address in a
  294. * descriptor, we should consider ourselves lucky that the
  295. * controller flagged an error instead of scribbling over
  296. * random memory locations.
  297. */
  298. dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
  299. "Bad descriptor submitted for DMA!\n");
  300. dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
  301. " cookie: %d\n", bad_desc->txd.cookie);
  302. dwc_dump_lli(dwc, &bad_desc->lli);
  303. list_for_each_entry(child, &bad_desc->txd.tx_list, desc_node)
  304. dwc_dump_lli(dwc, &child->lli);
  305. /* Pretend the descriptor completed successfully */
  306. dwc_descriptor_complete(dwc, bad_desc);
  307. }
  308. static void dw_dma_tasklet(unsigned long data)
  309. {
  310. struct dw_dma *dw = (struct dw_dma *)data;
  311. struct dw_dma_chan *dwc;
  312. u32 status_block;
  313. u32 status_xfer;
  314. u32 status_err;
  315. int i;
  316. status_block = dma_readl(dw, RAW.BLOCK);
  317. status_xfer = dma_readl(dw, RAW.XFER);
  318. status_err = dma_readl(dw, RAW.ERROR);
  319. dev_vdbg(dw->dma.dev, "tasklet: status_block=%x status_err=%x\n",
  320. status_block, status_err);
  321. for (i = 0; i < dw->dma.chancnt; i++) {
  322. dwc = &dw->chan[i];
  323. spin_lock(&dwc->lock);
  324. if (status_err & (1 << i))
  325. dwc_handle_error(dw, dwc);
  326. else if ((status_block | status_xfer) & (1 << i))
  327. dwc_scan_descriptors(dw, dwc);
  328. spin_unlock(&dwc->lock);
  329. }
  330. /*
  331. * Re-enable interrupts. Block Complete interrupts are only
  332. * enabled if the INT_EN bit in the descriptor is set. This
  333. * will trigger a scan before the whole list is done.
  334. */
  335. channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
  336. channel_set_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  337. channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
  338. }
  339. static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
  340. {
  341. struct dw_dma *dw = dev_id;
  342. u32 status;
  343. dev_vdbg(dw->dma.dev, "interrupt: status=0x%x\n",
  344. dma_readl(dw, STATUS_INT));
  345. /*
  346. * Just disable the interrupts. We'll turn them back on in the
  347. * softirq handler.
  348. */
  349. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  350. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  351. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  352. status = dma_readl(dw, STATUS_INT);
  353. if (status) {
  354. dev_err(dw->dma.dev,
  355. "BUG: Unexpected interrupts pending: 0x%x\n",
  356. status);
  357. /* Try to recover */
  358. channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
  359. channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1);
  360. channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
  361. channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
  362. channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
  363. }
  364. tasklet_schedule(&dw->tasklet);
  365. return IRQ_HANDLED;
  366. }
  367. /*----------------------------------------------------------------------*/
  368. static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
  369. {
  370. struct dw_desc *desc = txd_to_dw_desc(tx);
  371. struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
  372. dma_cookie_t cookie;
  373. spin_lock_bh(&dwc->lock);
  374. cookie = dwc_assign_cookie(dwc, desc);
  375. /*
  376. * REVISIT: We should attempt to chain as many descriptors as
  377. * possible, perhaps even appending to those already submitted
  378. * for DMA. But this is hard to do in a race-free manner.
  379. */
  380. if (list_empty(&dwc->active_list)) {
  381. dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
  382. desc->txd.cookie);
  383. dwc_dostart(dwc, desc);
  384. list_add_tail(&desc->desc_node, &dwc->active_list);
  385. } else {
  386. dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
  387. desc->txd.cookie);
  388. list_add_tail(&desc->desc_node, &dwc->queue);
  389. }
  390. spin_unlock_bh(&dwc->lock);
  391. return cookie;
  392. }
  393. static struct dma_async_tx_descriptor *
  394. dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  395. size_t len, unsigned long flags)
  396. {
  397. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  398. struct dw_desc *desc;
  399. struct dw_desc *first;
  400. struct dw_desc *prev;
  401. size_t xfer_count;
  402. size_t offset;
  403. unsigned int src_width;
  404. unsigned int dst_width;
  405. u32 ctllo;
  406. dev_vdbg(chan2dev(chan), "prep_dma_memcpy d0x%x s0x%x l0x%zx f0x%lx\n",
  407. dest, src, len, flags);
  408. if (unlikely(!len)) {
  409. dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
  410. return NULL;
  411. }
  412. /*
  413. * We can be a lot more clever here, but this should take care
  414. * of the most common optimization.
  415. */
  416. if (!((src | dest | len) & 3))
  417. src_width = dst_width = 2;
  418. else if (!((src | dest | len) & 1))
  419. src_width = dst_width = 1;
  420. else
  421. src_width = dst_width = 0;
  422. ctllo = DWC_DEFAULT_CTLLO
  423. | DWC_CTLL_DST_WIDTH(dst_width)
  424. | DWC_CTLL_SRC_WIDTH(src_width)
  425. | DWC_CTLL_DST_INC
  426. | DWC_CTLL_SRC_INC
  427. | DWC_CTLL_FC_M2M;
  428. prev = first = NULL;
  429. for (offset = 0; offset < len; offset += xfer_count << src_width) {
  430. xfer_count = min_t(size_t, (len - offset) >> src_width,
  431. DWC_MAX_COUNT);
  432. desc = dwc_desc_get(dwc);
  433. if (!desc)
  434. goto err_desc_get;
  435. desc->lli.sar = src + offset;
  436. desc->lli.dar = dest + offset;
  437. desc->lli.ctllo = ctllo;
  438. desc->lli.ctlhi = xfer_count;
  439. if (!first) {
  440. first = desc;
  441. } else {
  442. prev->lli.llp = desc->txd.phys;
  443. dma_sync_single_for_device(chan2parent(chan),
  444. prev->txd.phys, sizeof(prev->lli),
  445. DMA_TO_DEVICE);
  446. list_add_tail(&desc->desc_node,
  447. &first->txd.tx_list);
  448. }
  449. prev = desc;
  450. }
  451. if (flags & DMA_PREP_INTERRUPT)
  452. /* Trigger interrupt after last block */
  453. prev->lli.ctllo |= DWC_CTLL_INT_EN;
  454. prev->lli.llp = 0;
  455. dma_sync_single_for_device(chan2parent(chan),
  456. prev->txd.phys, sizeof(prev->lli),
  457. DMA_TO_DEVICE);
  458. first->txd.flags = flags;
  459. first->len = len;
  460. return &first->txd;
  461. err_desc_get:
  462. dwc_desc_put(dwc, first);
  463. return NULL;
  464. }
  465. static struct dma_async_tx_descriptor *
  466. dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  467. unsigned int sg_len, enum dma_data_direction direction,
  468. unsigned long flags)
  469. {
  470. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  471. struct dw_dma_slave *dws = dwc->dws;
  472. struct dw_desc *prev;
  473. struct dw_desc *first;
  474. u32 ctllo;
  475. dma_addr_t reg;
  476. unsigned int reg_width;
  477. unsigned int mem_width;
  478. unsigned int i;
  479. struct scatterlist *sg;
  480. size_t total_len = 0;
  481. dev_vdbg(chan2dev(chan), "prep_dma_slave\n");
  482. if (unlikely(!dws || !sg_len))
  483. return NULL;
  484. reg_width = dws->reg_width;
  485. prev = first = NULL;
  486. sg_len = dma_map_sg(chan2parent(chan), sgl, sg_len, direction);
  487. switch (direction) {
  488. case DMA_TO_DEVICE:
  489. ctllo = (DWC_DEFAULT_CTLLO
  490. | DWC_CTLL_DST_WIDTH(reg_width)
  491. | DWC_CTLL_DST_FIX
  492. | DWC_CTLL_SRC_INC
  493. | DWC_CTLL_FC_M2P);
  494. reg = dws->tx_reg;
  495. for_each_sg(sgl, sg, sg_len, i) {
  496. struct dw_desc *desc;
  497. u32 len;
  498. u32 mem;
  499. desc = dwc_desc_get(dwc);
  500. if (!desc) {
  501. dev_err(chan2dev(chan),
  502. "not enough descriptors available\n");
  503. goto err_desc_get;
  504. }
  505. mem = sg_phys(sg);
  506. len = sg_dma_len(sg);
  507. mem_width = 2;
  508. if (unlikely(mem & 3 || len & 3))
  509. mem_width = 0;
  510. desc->lli.sar = mem;
  511. desc->lli.dar = reg;
  512. desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
  513. desc->lli.ctlhi = len >> mem_width;
  514. if (!first) {
  515. first = desc;
  516. } else {
  517. prev->lli.llp = desc->txd.phys;
  518. dma_sync_single_for_device(chan2parent(chan),
  519. prev->txd.phys,
  520. sizeof(prev->lli),
  521. DMA_TO_DEVICE);
  522. list_add_tail(&desc->desc_node,
  523. &first->txd.tx_list);
  524. }
  525. prev = desc;
  526. total_len += len;
  527. }
  528. break;
  529. case DMA_FROM_DEVICE:
  530. ctllo = (DWC_DEFAULT_CTLLO
  531. | DWC_CTLL_SRC_WIDTH(reg_width)
  532. | DWC_CTLL_DST_INC
  533. | DWC_CTLL_SRC_FIX
  534. | DWC_CTLL_FC_P2M);
  535. reg = dws->rx_reg;
  536. for_each_sg(sgl, sg, sg_len, i) {
  537. struct dw_desc *desc;
  538. u32 len;
  539. u32 mem;
  540. desc = dwc_desc_get(dwc);
  541. if (!desc) {
  542. dev_err(chan2dev(chan),
  543. "not enough descriptors available\n");
  544. goto err_desc_get;
  545. }
  546. mem = sg_phys(sg);
  547. len = sg_dma_len(sg);
  548. mem_width = 2;
  549. if (unlikely(mem & 3 || len & 3))
  550. mem_width = 0;
  551. desc->lli.sar = reg;
  552. desc->lli.dar = mem;
  553. desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
  554. desc->lli.ctlhi = len >> reg_width;
  555. if (!first) {
  556. first = desc;
  557. } else {
  558. prev->lli.llp = desc->txd.phys;
  559. dma_sync_single_for_device(chan2parent(chan),
  560. prev->txd.phys,
  561. sizeof(prev->lli),
  562. DMA_TO_DEVICE);
  563. list_add_tail(&desc->desc_node,
  564. &first->txd.tx_list);
  565. }
  566. prev = desc;
  567. total_len += len;
  568. }
  569. break;
  570. default:
  571. return NULL;
  572. }
  573. if (flags & DMA_PREP_INTERRUPT)
  574. /* Trigger interrupt after last block */
  575. prev->lli.ctllo |= DWC_CTLL_INT_EN;
  576. prev->lli.llp = 0;
  577. dma_sync_single_for_device(chan2parent(chan),
  578. prev->txd.phys, sizeof(prev->lli),
  579. DMA_TO_DEVICE);
  580. first->len = total_len;
  581. return &first->txd;
  582. err_desc_get:
  583. dwc_desc_put(dwc, first);
  584. return NULL;
  585. }
  586. static void dwc_terminate_all(struct dma_chan *chan)
  587. {
  588. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  589. struct dw_dma *dw = to_dw_dma(chan->device);
  590. struct dw_desc *desc, *_desc;
  591. LIST_HEAD(list);
  592. /*
  593. * This is only called when something went wrong elsewhere, so
  594. * we don't really care about the data. Just disable the
  595. * channel. We still have to poll the channel enable bit due
  596. * to AHB/HSB limitations.
  597. */
  598. spin_lock_bh(&dwc->lock);
  599. channel_clear_bit(dw, CH_EN, dwc->mask);
  600. while (dma_readl(dw, CH_EN) & dwc->mask)
  601. cpu_relax();
  602. /* active_list entries will end up before queued entries */
  603. list_splice_init(&dwc->queue, &list);
  604. list_splice_init(&dwc->active_list, &list);
  605. spin_unlock_bh(&dwc->lock);
  606. /* Flush all pending and queued descriptors */
  607. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  608. dwc_descriptor_complete(dwc, desc);
  609. }
  610. static enum dma_status
  611. dwc_is_tx_complete(struct dma_chan *chan,
  612. dma_cookie_t cookie,
  613. dma_cookie_t *done, dma_cookie_t *used)
  614. {
  615. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  616. dma_cookie_t last_used;
  617. dma_cookie_t last_complete;
  618. int ret;
  619. last_complete = dwc->completed;
  620. last_used = chan->cookie;
  621. ret = dma_async_is_complete(cookie, last_complete, last_used);
  622. if (ret != DMA_SUCCESS) {
  623. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  624. last_complete = dwc->completed;
  625. last_used = chan->cookie;
  626. ret = dma_async_is_complete(cookie, last_complete, last_used);
  627. }
  628. if (done)
  629. *done = last_complete;
  630. if (used)
  631. *used = last_used;
  632. return ret;
  633. }
  634. static void dwc_issue_pending(struct dma_chan *chan)
  635. {
  636. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  637. spin_lock_bh(&dwc->lock);
  638. if (!list_empty(&dwc->queue))
  639. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  640. spin_unlock_bh(&dwc->lock);
  641. }
  642. static int dwc_alloc_chan_resources(struct dma_chan *chan)
  643. {
  644. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  645. struct dw_dma *dw = to_dw_dma(chan->device);
  646. struct dw_desc *desc;
  647. struct dw_dma_slave *dws;
  648. int i;
  649. u32 cfghi;
  650. u32 cfglo;
  651. dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
  652. /* ASSERT: channel is idle */
  653. if (dma_readl(dw, CH_EN) & dwc->mask) {
  654. dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
  655. return -EIO;
  656. }
  657. dwc->completed = chan->cookie = 1;
  658. cfghi = DWC_CFGH_FIFO_MODE;
  659. cfglo = 0;
  660. dws = dwc->dws;
  661. if (dws) {
  662. /*
  663. * We need controller-specific data to set up slave
  664. * transfers.
  665. */
  666. BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev);
  667. cfghi = dws->cfg_hi;
  668. cfglo = dws->cfg_lo;
  669. }
  670. channel_writel(dwc, CFG_LO, cfglo);
  671. channel_writel(dwc, CFG_HI, cfghi);
  672. /*
  673. * NOTE: some controllers may have additional features that we
  674. * need to initialize here, like "scatter-gather" (which
  675. * doesn't mean what you think it means), and status writeback.
  676. */
  677. spin_lock_bh(&dwc->lock);
  678. i = dwc->descs_allocated;
  679. while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
  680. spin_unlock_bh(&dwc->lock);
  681. desc = kzalloc(sizeof(struct dw_desc), GFP_KERNEL);
  682. if (!desc) {
  683. dev_info(chan2dev(chan),
  684. "only allocated %d descriptors\n", i);
  685. spin_lock_bh(&dwc->lock);
  686. break;
  687. }
  688. dma_async_tx_descriptor_init(&desc->txd, chan);
  689. desc->txd.tx_submit = dwc_tx_submit;
  690. desc->txd.flags = DMA_CTRL_ACK;
  691. INIT_LIST_HEAD(&desc->txd.tx_list);
  692. desc->txd.phys = dma_map_single(chan2parent(chan), &desc->lli,
  693. sizeof(desc->lli), DMA_TO_DEVICE);
  694. dwc_desc_put(dwc, desc);
  695. spin_lock_bh(&dwc->lock);
  696. i = ++dwc->descs_allocated;
  697. }
  698. /* Enable interrupts */
  699. channel_set_bit(dw, MASK.XFER, dwc->mask);
  700. channel_set_bit(dw, MASK.BLOCK, dwc->mask);
  701. channel_set_bit(dw, MASK.ERROR, dwc->mask);
  702. spin_unlock_bh(&dwc->lock);
  703. dev_dbg(chan2dev(chan),
  704. "alloc_chan_resources allocated %d descriptors\n", i);
  705. return i;
  706. }
  707. static void dwc_free_chan_resources(struct dma_chan *chan)
  708. {
  709. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  710. struct dw_dma *dw = to_dw_dma(chan->device);
  711. struct dw_desc *desc, *_desc;
  712. LIST_HEAD(list);
  713. dev_dbg(chan2dev(chan), "free_chan_resources (descs allocated=%u)\n",
  714. dwc->descs_allocated);
  715. /* ASSERT: channel is idle */
  716. BUG_ON(!list_empty(&dwc->active_list));
  717. BUG_ON(!list_empty(&dwc->queue));
  718. BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
  719. spin_lock_bh(&dwc->lock);
  720. list_splice_init(&dwc->free_list, &list);
  721. dwc->descs_allocated = 0;
  722. dwc->dws = NULL;
  723. /* Disable interrupts */
  724. channel_clear_bit(dw, MASK.XFER, dwc->mask);
  725. channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
  726. channel_clear_bit(dw, MASK.ERROR, dwc->mask);
  727. spin_unlock_bh(&dwc->lock);
  728. list_for_each_entry_safe(desc, _desc, &list, desc_node) {
  729. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  730. dma_unmap_single(chan2parent(chan), desc->txd.phys,
  731. sizeof(desc->lli), DMA_TO_DEVICE);
  732. kfree(desc);
  733. }
  734. dev_vdbg(chan2dev(chan), "free_chan_resources done\n");
  735. }
  736. /*----------------------------------------------------------------------*/
  737. static void dw_dma_off(struct dw_dma *dw)
  738. {
  739. dma_writel(dw, CFG, 0);
  740. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  741. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  742. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  743. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  744. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  745. while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
  746. cpu_relax();
  747. }
  748. static int __init dw_probe(struct platform_device *pdev)
  749. {
  750. struct dw_dma_platform_data *pdata;
  751. struct resource *io;
  752. struct dw_dma *dw;
  753. size_t size;
  754. int irq;
  755. int err;
  756. int i;
  757. pdata = pdev->dev.platform_data;
  758. if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
  759. return -EINVAL;
  760. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  761. if (!io)
  762. return -EINVAL;
  763. irq = platform_get_irq(pdev, 0);
  764. if (irq < 0)
  765. return irq;
  766. size = sizeof(struct dw_dma);
  767. size += pdata->nr_channels * sizeof(struct dw_dma_chan);
  768. dw = kzalloc(size, GFP_KERNEL);
  769. if (!dw)
  770. return -ENOMEM;
  771. if (!request_mem_region(io->start, DW_REGLEN, pdev->dev.driver->name)) {
  772. err = -EBUSY;
  773. goto err_kfree;
  774. }
  775. memset(dw, 0, sizeof *dw);
  776. dw->regs = ioremap(io->start, DW_REGLEN);
  777. if (!dw->regs) {
  778. err = -ENOMEM;
  779. goto err_release_r;
  780. }
  781. dw->clk = clk_get(&pdev->dev, "hclk");
  782. if (IS_ERR(dw->clk)) {
  783. err = PTR_ERR(dw->clk);
  784. goto err_clk;
  785. }
  786. clk_enable(dw->clk);
  787. /* force dma off, just in case */
  788. dw_dma_off(dw);
  789. err = request_irq(irq, dw_dma_interrupt, 0, "dw_dmac", dw);
  790. if (err)
  791. goto err_irq;
  792. platform_set_drvdata(pdev, dw);
  793. tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
  794. dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
  795. INIT_LIST_HEAD(&dw->dma.channels);
  796. for (i = 0; i < pdata->nr_channels; i++, dw->dma.chancnt++) {
  797. struct dw_dma_chan *dwc = &dw->chan[i];
  798. dwc->chan.device = &dw->dma;
  799. dwc->chan.cookie = dwc->completed = 1;
  800. dwc->chan.chan_id = i;
  801. list_add_tail(&dwc->chan.device_node, &dw->dma.channels);
  802. dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
  803. spin_lock_init(&dwc->lock);
  804. dwc->mask = 1 << i;
  805. INIT_LIST_HEAD(&dwc->active_list);
  806. INIT_LIST_HEAD(&dwc->queue);
  807. INIT_LIST_HEAD(&dwc->free_list);
  808. channel_clear_bit(dw, CH_EN, dwc->mask);
  809. }
  810. /* Clear/disable all interrupts on all channels. */
  811. dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
  812. dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
  813. dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
  814. dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
  815. dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
  816. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  817. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  818. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  819. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  820. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  821. dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
  822. dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
  823. dw->dma.dev = &pdev->dev;
  824. dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
  825. dw->dma.device_free_chan_resources = dwc_free_chan_resources;
  826. dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
  827. dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
  828. dw->dma.device_terminate_all = dwc_terminate_all;
  829. dw->dma.device_is_tx_complete = dwc_is_tx_complete;
  830. dw->dma.device_issue_pending = dwc_issue_pending;
  831. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  832. printk(KERN_INFO "%s: DesignWare DMA Controller, %d channels\n",
  833. pdev->dev.bus_id, dw->dma.chancnt);
  834. dma_async_device_register(&dw->dma);
  835. return 0;
  836. err_irq:
  837. clk_disable(dw->clk);
  838. clk_put(dw->clk);
  839. err_clk:
  840. iounmap(dw->regs);
  841. dw->regs = NULL;
  842. err_release_r:
  843. release_resource(io);
  844. err_kfree:
  845. kfree(dw);
  846. return err;
  847. }
  848. static int __exit dw_remove(struct platform_device *pdev)
  849. {
  850. struct dw_dma *dw = platform_get_drvdata(pdev);
  851. struct dw_dma_chan *dwc, *_dwc;
  852. struct resource *io;
  853. dw_dma_off(dw);
  854. dma_async_device_unregister(&dw->dma);
  855. free_irq(platform_get_irq(pdev, 0), dw);
  856. tasklet_kill(&dw->tasklet);
  857. list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
  858. chan.device_node) {
  859. list_del(&dwc->chan.device_node);
  860. channel_clear_bit(dw, CH_EN, dwc->mask);
  861. }
  862. clk_disable(dw->clk);
  863. clk_put(dw->clk);
  864. iounmap(dw->regs);
  865. dw->regs = NULL;
  866. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  867. release_mem_region(io->start, DW_REGLEN);
  868. kfree(dw);
  869. return 0;
  870. }
  871. static void dw_shutdown(struct platform_device *pdev)
  872. {
  873. struct dw_dma *dw = platform_get_drvdata(pdev);
  874. dw_dma_off(platform_get_drvdata(pdev));
  875. clk_disable(dw->clk);
  876. }
  877. static int dw_suspend_late(struct platform_device *pdev, pm_message_t mesg)
  878. {
  879. struct dw_dma *dw = platform_get_drvdata(pdev);
  880. dw_dma_off(platform_get_drvdata(pdev));
  881. clk_disable(dw->clk);
  882. return 0;
  883. }
  884. static int dw_resume_early(struct platform_device *pdev)
  885. {
  886. struct dw_dma *dw = platform_get_drvdata(pdev);
  887. clk_enable(dw->clk);
  888. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  889. return 0;
  890. }
  891. static struct platform_driver dw_driver = {
  892. .remove = __exit_p(dw_remove),
  893. .shutdown = dw_shutdown,
  894. .suspend_late = dw_suspend_late,
  895. .resume_early = dw_resume_early,
  896. .driver = {
  897. .name = "dw_dmac",
  898. },
  899. };
  900. static int __init dw_init(void)
  901. {
  902. return platform_driver_probe(&dw_driver, dw_probe);
  903. }
  904. module_init(dw_init);
  905. static void __exit dw_exit(void)
  906. {
  907. platform_driver_unregister(&dw_driver);
  908. }
  909. module_exit(dw_exit);
  910. MODULE_LICENSE("GPL v2");
  911. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
  912. MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");