dw_dmac.c 37 KB

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