dw_dmac.c 28 KB

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