dw_dmac.c 38 KB

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