dw_dmac.c 28 KB

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