dw_dmac.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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->slave.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->slave.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->slave.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. struct dma_client *client)
  636. {
  637. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  638. struct dw_dma *dw = to_dw_dma(chan->device);
  639. struct dw_desc *desc;
  640. struct dma_slave *slave;
  641. struct dw_dma_slave *dws;
  642. int i;
  643. u32 cfghi;
  644. u32 cfglo;
  645. dev_vdbg(&chan->dev, "alloc_chan_resources\n");
  646. /* Channels doing slave DMA can only handle one client. */
  647. if (dwc->dws || client->slave) {
  648. if (chan->client_count)
  649. return -EBUSY;
  650. }
  651. /* ASSERT: channel is idle */
  652. if (dma_readl(dw, CH_EN) & dwc->mask) {
  653. dev_dbg(&chan->dev, "DMA channel not idle?\n");
  654. return -EIO;
  655. }
  656. dwc->completed = chan->cookie = 1;
  657. cfghi = DWC_CFGH_FIFO_MODE;
  658. cfglo = 0;
  659. slave = client->slave;
  660. if (slave) {
  661. /*
  662. * We need controller-specific data to set up slave
  663. * transfers.
  664. */
  665. BUG_ON(!slave->dma_dev || slave->dma_dev != dw->dma.dev);
  666. dws = container_of(slave, struct dw_dma_slave, slave);
  667. dwc->dws = dws;
  668. cfghi = dws->cfg_hi;
  669. cfglo = dws->cfg_lo;
  670. } else {
  671. dwc->dws = NULL;
  672. }
  673. channel_writel(dwc, CFG_LO, cfglo);
  674. channel_writel(dwc, CFG_HI, cfghi);
  675. /*
  676. * NOTE: some controllers may have additional features that we
  677. * need to initialize here, like "scatter-gather" (which
  678. * doesn't mean what you think it means), and status writeback.
  679. */
  680. spin_lock_bh(&dwc->lock);
  681. i = dwc->descs_allocated;
  682. while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
  683. spin_unlock_bh(&dwc->lock);
  684. desc = kzalloc(sizeof(struct dw_desc), GFP_KERNEL);
  685. if (!desc) {
  686. dev_info(&chan->dev,
  687. "only allocated %d descriptors\n", i);
  688. spin_lock_bh(&dwc->lock);
  689. break;
  690. }
  691. dma_async_tx_descriptor_init(&desc->txd, chan);
  692. desc->txd.tx_submit = dwc_tx_submit;
  693. desc->txd.flags = DMA_CTRL_ACK;
  694. INIT_LIST_HEAD(&desc->txd.tx_list);
  695. desc->txd.phys = dma_map_single(chan->dev.parent, &desc->lli,
  696. sizeof(desc->lli), DMA_TO_DEVICE);
  697. dwc_desc_put(dwc, desc);
  698. spin_lock_bh(&dwc->lock);
  699. i = ++dwc->descs_allocated;
  700. }
  701. /* Enable interrupts */
  702. channel_set_bit(dw, MASK.XFER, dwc->mask);
  703. channel_set_bit(dw, MASK.BLOCK, dwc->mask);
  704. channel_set_bit(dw, MASK.ERROR, dwc->mask);
  705. spin_unlock_bh(&dwc->lock);
  706. dev_dbg(&chan->dev,
  707. "alloc_chan_resources allocated %d descriptors\n", i);
  708. return i;
  709. }
  710. static void dwc_free_chan_resources(struct dma_chan *chan)
  711. {
  712. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  713. struct dw_dma *dw = to_dw_dma(chan->device);
  714. struct dw_desc *desc, *_desc;
  715. LIST_HEAD(list);
  716. dev_dbg(&chan->dev, "free_chan_resources (descs allocated=%u)\n",
  717. dwc->descs_allocated);
  718. /* ASSERT: channel is idle */
  719. BUG_ON(!list_empty(&dwc->active_list));
  720. BUG_ON(!list_empty(&dwc->queue));
  721. BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
  722. spin_lock_bh(&dwc->lock);
  723. list_splice_init(&dwc->free_list, &list);
  724. dwc->descs_allocated = 0;
  725. dwc->dws = NULL;
  726. /* Disable interrupts */
  727. channel_clear_bit(dw, MASK.XFER, dwc->mask);
  728. channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
  729. channel_clear_bit(dw, MASK.ERROR, dwc->mask);
  730. spin_unlock_bh(&dwc->lock);
  731. list_for_each_entry_safe(desc, _desc, &list, desc_node) {
  732. dev_vdbg(&chan->dev, " freeing descriptor %p\n", desc);
  733. dma_unmap_single(chan->dev.parent, desc->txd.phys,
  734. sizeof(desc->lli), DMA_TO_DEVICE);
  735. kfree(desc);
  736. }
  737. dev_vdbg(&chan->dev, "free_chan_resources done\n");
  738. }
  739. /*----------------------------------------------------------------------*/
  740. static void dw_dma_off(struct dw_dma *dw)
  741. {
  742. dma_writel(dw, CFG, 0);
  743. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  744. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  745. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  746. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  747. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  748. while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
  749. cpu_relax();
  750. }
  751. static int __init dw_probe(struct platform_device *pdev)
  752. {
  753. struct dw_dma_platform_data *pdata;
  754. struct resource *io;
  755. struct dw_dma *dw;
  756. size_t size;
  757. int irq;
  758. int err;
  759. int i;
  760. pdata = pdev->dev.platform_data;
  761. if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
  762. return -EINVAL;
  763. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  764. if (!io)
  765. return -EINVAL;
  766. irq = platform_get_irq(pdev, 0);
  767. if (irq < 0)
  768. return irq;
  769. size = sizeof(struct dw_dma);
  770. size += pdata->nr_channels * sizeof(struct dw_dma_chan);
  771. dw = kzalloc(size, GFP_KERNEL);
  772. if (!dw)
  773. return -ENOMEM;
  774. if (!request_mem_region(io->start, DW_REGLEN, pdev->dev.driver->name)) {
  775. err = -EBUSY;
  776. goto err_kfree;
  777. }
  778. memset(dw, 0, sizeof *dw);
  779. dw->regs = ioremap(io->start, DW_REGLEN);
  780. if (!dw->regs) {
  781. err = -ENOMEM;
  782. goto err_release_r;
  783. }
  784. dw->clk = clk_get(&pdev->dev, "hclk");
  785. if (IS_ERR(dw->clk)) {
  786. err = PTR_ERR(dw->clk);
  787. goto err_clk;
  788. }
  789. clk_enable(dw->clk);
  790. /* force dma off, just in case */
  791. dw_dma_off(dw);
  792. err = request_irq(irq, dw_dma_interrupt, 0, "dw_dmac", dw);
  793. if (err)
  794. goto err_irq;
  795. platform_set_drvdata(pdev, dw);
  796. tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
  797. dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
  798. INIT_LIST_HEAD(&dw->dma.channels);
  799. for (i = 0; i < pdata->nr_channels; i++, dw->dma.chancnt++) {
  800. struct dw_dma_chan *dwc = &dw->chan[i];
  801. dwc->chan.device = &dw->dma;
  802. dwc->chan.cookie = dwc->completed = 1;
  803. dwc->chan.chan_id = i;
  804. list_add_tail(&dwc->chan.device_node, &dw->dma.channels);
  805. dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
  806. spin_lock_init(&dwc->lock);
  807. dwc->mask = 1 << i;
  808. INIT_LIST_HEAD(&dwc->active_list);
  809. INIT_LIST_HEAD(&dwc->queue);
  810. INIT_LIST_HEAD(&dwc->free_list);
  811. channel_clear_bit(dw, CH_EN, dwc->mask);
  812. }
  813. /* Clear/disable all interrupts on all channels. */
  814. dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
  815. dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
  816. dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
  817. dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
  818. dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
  819. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  820. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  821. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  822. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  823. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  824. dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
  825. dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
  826. dw->dma.dev = &pdev->dev;
  827. dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
  828. dw->dma.device_free_chan_resources = dwc_free_chan_resources;
  829. dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
  830. dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
  831. dw->dma.device_terminate_all = dwc_terminate_all;
  832. dw->dma.device_is_tx_complete = dwc_is_tx_complete;
  833. dw->dma.device_issue_pending = dwc_issue_pending;
  834. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  835. printk(KERN_INFO "%s: DesignWare DMA Controller, %d channels\n",
  836. pdev->dev.bus_id, dw->dma.chancnt);
  837. dma_async_device_register(&dw->dma);
  838. return 0;
  839. err_irq:
  840. clk_disable(dw->clk);
  841. clk_put(dw->clk);
  842. err_clk:
  843. iounmap(dw->regs);
  844. dw->regs = NULL;
  845. err_release_r:
  846. release_resource(io);
  847. err_kfree:
  848. kfree(dw);
  849. return err;
  850. }
  851. static int __exit dw_remove(struct platform_device *pdev)
  852. {
  853. struct dw_dma *dw = platform_get_drvdata(pdev);
  854. struct dw_dma_chan *dwc, *_dwc;
  855. struct resource *io;
  856. dw_dma_off(dw);
  857. dma_async_device_unregister(&dw->dma);
  858. free_irq(platform_get_irq(pdev, 0), dw);
  859. tasklet_kill(&dw->tasklet);
  860. list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
  861. chan.device_node) {
  862. list_del(&dwc->chan.device_node);
  863. channel_clear_bit(dw, CH_EN, dwc->mask);
  864. }
  865. clk_disable(dw->clk);
  866. clk_put(dw->clk);
  867. iounmap(dw->regs);
  868. dw->regs = NULL;
  869. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  870. release_mem_region(io->start, DW_REGLEN);
  871. kfree(dw);
  872. return 0;
  873. }
  874. static void dw_shutdown(struct platform_device *pdev)
  875. {
  876. struct dw_dma *dw = platform_get_drvdata(pdev);
  877. dw_dma_off(platform_get_drvdata(pdev));
  878. clk_disable(dw->clk);
  879. }
  880. static int dw_suspend_late(struct platform_device *pdev, pm_message_t mesg)
  881. {
  882. struct dw_dma *dw = platform_get_drvdata(pdev);
  883. dw_dma_off(platform_get_drvdata(pdev));
  884. clk_disable(dw->clk);
  885. return 0;
  886. }
  887. static int dw_resume_early(struct platform_device *pdev)
  888. {
  889. struct dw_dma *dw = platform_get_drvdata(pdev);
  890. clk_enable(dw->clk);
  891. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  892. return 0;
  893. }
  894. static struct platform_driver dw_driver = {
  895. .remove = __exit_p(dw_remove),
  896. .shutdown = dw_shutdown,
  897. .suspend_late = dw_suspend_late,
  898. .resume_early = dw_resume_early,
  899. .driver = {
  900. .name = "dw_dmac",
  901. },
  902. };
  903. static int __init dw_init(void)
  904. {
  905. return platform_driver_probe(&dw_driver, dw_probe);
  906. }
  907. module_init(dw_init);
  908. static void __exit dw_exit(void)
  909. {
  910. platform_driver_unregister(&dw_driver);
  911. }
  912. module_exit(dw_exit);
  913. MODULE_LICENSE("GPL v2");
  914. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
  915. MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");