dw_dmac.c 38 KB

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