dw_dmac.c 37 KB

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