dw_dmac.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  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;
  755. }
  756. channel_writel(dwc, CFG_LO, cfglo);
  757. channel_writel(dwc, CFG_HI, cfghi);
  758. /*
  759. * NOTE: some controllers may have additional features that we
  760. * need to initialize here, like "scatter-gather" (which
  761. * doesn't mean what you think it means), and status writeback.
  762. */
  763. spin_lock_bh(&dwc->lock);
  764. i = dwc->descs_allocated;
  765. while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
  766. spin_unlock_bh(&dwc->lock);
  767. desc = kzalloc(sizeof(struct dw_desc), GFP_KERNEL);
  768. if (!desc) {
  769. dev_info(chan2dev(chan),
  770. "only allocated %d descriptors\n", i);
  771. spin_lock_bh(&dwc->lock);
  772. break;
  773. }
  774. INIT_LIST_HEAD(&desc->tx_list);
  775. dma_async_tx_descriptor_init(&desc->txd, chan);
  776. desc->txd.tx_submit = dwc_tx_submit;
  777. desc->txd.flags = DMA_CTRL_ACK;
  778. desc->txd.phys = dma_map_single(chan2parent(chan), &desc->lli,
  779. sizeof(desc->lli), DMA_TO_DEVICE);
  780. dwc_desc_put(dwc, desc);
  781. spin_lock_bh(&dwc->lock);
  782. i = ++dwc->descs_allocated;
  783. }
  784. /* Enable interrupts */
  785. channel_set_bit(dw, MASK.XFER, dwc->mask);
  786. channel_set_bit(dw, MASK.BLOCK, dwc->mask);
  787. channel_set_bit(dw, MASK.ERROR, dwc->mask);
  788. spin_unlock_bh(&dwc->lock);
  789. dev_dbg(chan2dev(chan),
  790. "alloc_chan_resources allocated %d descriptors\n", i);
  791. return i;
  792. }
  793. static void dwc_free_chan_resources(struct dma_chan *chan)
  794. {
  795. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  796. struct dw_dma *dw = to_dw_dma(chan->device);
  797. struct dw_desc *desc, *_desc;
  798. LIST_HEAD(list);
  799. dev_dbg(chan2dev(chan), "free_chan_resources (descs allocated=%u)\n",
  800. dwc->descs_allocated);
  801. /* ASSERT: channel is idle */
  802. BUG_ON(!list_empty(&dwc->active_list));
  803. BUG_ON(!list_empty(&dwc->queue));
  804. BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
  805. spin_lock_bh(&dwc->lock);
  806. list_splice_init(&dwc->free_list, &list);
  807. dwc->descs_allocated = 0;
  808. /* Disable interrupts */
  809. channel_clear_bit(dw, MASK.XFER, dwc->mask);
  810. channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
  811. channel_clear_bit(dw, MASK.ERROR, dwc->mask);
  812. spin_unlock_bh(&dwc->lock);
  813. list_for_each_entry_safe(desc, _desc, &list, desc_node) {
  814. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  815. dma_unmap_single(chan2parent(chan), desc->txd.phys,
  816. sizeof(desc->lli), DMA_TO_DEVICE);
  817. kfree(desc);
  818. }
  819. dev_vdbg(chan2dev(chan), "free_chan_resources done\n");
  820. }
  821. /* --------------------- Cyclic DMA API extensions -------------------- */
  822. /**
  823. * dw_dma_cyclic_start - start the cyclic DMA transfer
  824. * @chan: the DMA channel to start
  825. *
  826. * Must be called with soft interrupts disabled. Returns zero on success or
  827. * -errno on failure.
  828. */
  829. int dw_dma_cyclic_start(struct dma_chan *chan)
  830. {
  831. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  832. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  833. if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
  834. dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
  835. return -ENODEV;
  836. }
  837. spin_lock(&dwc->lock);
  838. /* assert channel is idle */
  839. if (dma_readl(dw, CH_EN) & dwc->mask) {
  840. dev_err(chan2dev(&dwc->chan),
  841. "BUG: Attempted to start non-idle channel\n");
  842. dev_err(chan2dev(&dwc->chan),
  843. " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
  844. channel_readl(dwc, SAR),
  845. channel_readl(dwc, DAR),
  846. channel_readl(dwc, LLP),
  847. channel_readl(dwc, CTL_HI),
  848. channel_readl(dwc, CTL_LO));
  849. spin_unlock(&dwc->lock);
  850. return -EBUSY;
  851. }
  852. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  853. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  854. dma_writel(dw, CLEAR.XFER, dwc->mask);
  855. /* setup DMAC channel registers */
  856. channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
  857. channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  858. channel_writel(dwc, CTL_HI, 0);
  859. channel_set_bit(dw, CH_EN, dwc->mask);
  860. spin_unlock(&dwc->lock);
  861. return 0;
  862. }
  863. EXPORT_SYMBOL(dw_dma_cyclic_start);
  864. /**
  865. * dw_dma_cyclic_stop - stop the cyclic DMA transfer
  866. * @chan: the DMA channel to stop
  867. *
  868. * Must be called with soft interrupts disabled.
  869. */
  870. void dw_dma_cyclic_stop(struct dma_chan *chan)
  871. {
  872. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  873. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  874. spin_lock(&dwc->lock);
  875. channel_clear_bit(dw, CH_EN, dwc->mask);
  876. while (dma_readl(dw, CH_EN) & dwc->mask)
  877. cpu_relax();
  878. spin_unlock(&dwc->lock);
  879. }
  880. EXPORT_SYMBOL(dw_dma_cyclic_stop);
  881. /**
  882. * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
  883. * @chan: the DMA channel to prepare
  884. * @buf_addr: physical DMA address where the buffer starts
  885. * @buf_len: total number of bytes for the entire buffer
  886. * @period_len: number of bytes for each period
  887. * @direction: transfer direction, to or from device
  888. *
  889. * Must be called before trying to start the transfer. Returns a valid struct
  890. * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
  891. */
  892. struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
  893. dma_addr_t buf_addr, size_t buf_len, size_t period_len,
  894. enum dma_data_direction direction)
  895. {
  896. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  897. struct dw_cyclic_desc *cdesc;
  898. struct dw_cyclic_desc *retval = NULL;
  899. struct dw_desc *desc;
  900. struct dw_desc *last = NULL;
  901. struct dw_dma_slave *dws = chan->private;
  902. unsigned long was_cyclic;
  903. unsigned int reg_width;
  904. unsigned int periods;
  905. unsigned int i;
  906. spin_lock_bh(&dwc->lock);
  907. if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
  908. spin_unlock_bh(&dwc->lock);
  909. dev_dbg(chan2dev(&dwc->chan),
  910. "queue and/or active list are not empty\n");
  911. return ERR_PTR(-EBUSY);
  912. }
  913. was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  914. spin_unlock_bh(&dwc->lock);
  915. if (was_cyclic) {
  916. dev_dbg(chan2dev(&dwc->chan),
  917. "channel already prepared for cyclic DMA\n");
  918. return ERR_PTR(-EBUSY);
  919. }
  920. retval = ERR_PTR(-EINVAL);
  921. reg_width = dws->reg_width;
  922. periods = buf_len / period_len;
  923. /* Check for too big/unaligned periods and unaligned DMA buffer. */
  924. if (period_len > (DWC_MAX_COUNT << reg_width))
  925. goto out_err;
  926. if (unlikely(period_len & ((1 << reg_width) - 1)))
  927. goto out_err;
  928. if (unlikely(buf_addr & ((1 << reg_width) - 1)))
  929. goto out_err;
  930. if (unlikely(!(direction & (DMA_TO_DEVICE | DMA_FROM_DEVICE))))
  931. goto out_err;
  932. retval = ERR_PTR(-ENOMEM);
  933. if (periods > NR_DESCS_PER_CHANNEL)
  934. goto out_err;
  935. cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
  936. if (!cdesc)
  937. goto out_err;
  938. cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
  939. if (!cdesc->desc)
  940. goto out_err_alloc;
  941. for (i = 0; i < periods; i++) {
  942. desc = dwc_desc_get(dwc);
  943. if (!desc)
  944. goto out_err_desc_get;
  945. switch (direction) {
  946. case DMA_TO_DEVICE:
  947. desc->lli.dar = dws->tx_reg;
  948. desc->lli.sar = buf_addr + (period_len * i);
  949. desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private)
  950. | DWC_CTLL_DST_WIDTH(reg_width)
  951. | DWC_CTLL_SRC_WIDTH(reg_width)
  952. | DWC_CTLL_DST_FIX
  953. | DWC_CTLL_SRC_INC
  954. | DWC_CTLL_FC_M2P
  955. | DWC_CTLL_INT_EN);
  956. break;
  957. case DMA_FROM_DEVICE:
  958. desc->lli.dar = buf_addr + (period_len * i);
  959. desc->lli.sar = dws->rx_reg;
  960. desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private)
  961. | DWC_CTLL_SRC_WIDTH(reg_width)
  962. | DWC_CTLL_DST_WIDTH(reg_width)
  963. | DWC_CTLL_DST_INC
  964. | DWC_CTLL_SRC_FIX
  965. | DWC_CTLL_FC_P2M
  966. | DWC_CTLL_INT_EN);
  967. break;
  968. default:
  969. break;
  970. }
  971. desc->lli.ctlhi = (period_len >> reg_width);
  972. cdesc->desc[i] = desc;
  973. if (last) {
  974. last->lli.llp = desc->txd.phys;
  975. dma_sync_single_for_device(chan2parent(chan),
  976. last->txd.phys, sizeof(last->lli),
  977. DMA_TO_DEVICE);
  978. }
  979. last = desc;
  980. }
  981. /* lets make a cyclic list */
  982. last->lli.llp = cdesc->desc[0]->txd.phys;
  983. dma_sync_single_for_device(chan2parent(chan), last->txd.phys,
  984. sizeof(last->lli), DMA_TO_DEVICE);
  985. dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%08x len %zu "
  986. "period %zu periods %d\n", buf_addr, buf_len,
  987. period_len, periods);
  988. cdesc->periods = periods;
  989. dwc->cdesc = cdesc;
  990. return cdesc;
  991. out_err_desc_get:
  992. while (i--)
  993. dwc_desc_put(dwc, cdesc->desc[i]);
  994. out_err_alloc:
  995. kfree(cdesc);
  996. out_err:
  997. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  998. return (struct dw_cyclic_desc *)retval;
  999. }
  1000. EXPORT_SYMBOL(dw_dma_cyclic_prep);
  1001. /**
  1002. * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
  1003. * @chan: the DMA channel to free
  1004. */
  1005. void dw_dma_cyclic_free(struct dma_chan *chan)
  1006. {
  1007. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1008. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  1009. struct dw_cyclic_desc *cdesc = dwc->cdesc;
  1010. int i;
  1011. dev_dbg(chan2dev(&dwc->chan), "cyclic free\n");
  1012. if (!cdesc)
  1013. return;
  1014. spin_lock_bh(&dwc->lock);
  1015. channel_clear_bit(dw, CH_EN, dwc->mask);
  1016. while (dma_readl(dw, CH_EN) & dwc->mask)
  1017. cpu_relax();
  1018. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  1019. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  1020. dma_writel(dw, CLEAR.XFER, dwc->mask);
  1021. spin_unlock_bh(&dwc->lock);
  1022. for (i = 0; i < cdesc->periods; i++)
  1023. dwc_desc_put(dwc, cdesc->desc[i]);
  1024. kfree(cdesc->desc);
  1025. kfree(cdesc);
  1026. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1027. }
  1028. EXPORT_SYMBOL(dw_dma_cyclic_free);
  1029. /*----------------------------------------------------------------------*/
  1030. static void dw_dma_off(struct dw_dma *dw)
  1031. {
  1032. dma_writel(dw, CFG, 0);
  1033. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  1034. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  1035. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  1036. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  1037. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  1038. while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
  1039. cpu_relax();
  1040. }
  1041. static int __init dw_probe(struct platform_device *pdev)
  1042. {
  1043. struct dw_dma_platform_data *pdata;
  1044. struct resource *io;
  1045. struct dw_dma *dw;
  1046. size_t size;
  1047. int irq;
  1048. int err;
  1049. int i;
  1050. pdata = pdev->dev.platform_data;
  1051. if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
  1052. return -EINVAL;
  1053. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1054. if (!io)
  1055. return -EINVAL;
  1056. irq = platform_get_irq(pdev, 0);
  1057. if (irq < 0)
  1058. return irq;
  1059. size = sizeof(struct dw_dma);
  1060. size += pdata->nr_channels * sizeof(struct dw_dma_chan);
  1061. dw = kzalloc(size, GFP_KERNEL);
  1062. if (!dw)
  1063. return -ENOMEM;
  1064. if (!request_mem_region(io->start, DW_REGLEN, pdev->dev.driver->name)) {
  1065. err = -EBUSY;
  1066. goto err_kfree;
  1067. }
  1068. dw->regs = ioremap(io->start, DW_REGLEN);
  1069. if (!dw->regs) {
  1070. err = -ENOMEM;
  1071. goto err_release_r;
  1072. }
  1073. dw->clk = clk_get(&pdev->dev, "hclk");
  1074. if (IS_ERR(dw->clk)) {
  1075. err = PTR_ERR(dw->clk);
  1076. goto err_clk;
  1077. }
  1078. clk_enable(dw->clk);
  1079. /* force dma off, just in case */
  1080. dw_dma_off(dw);
  1081. err = request_irq(irq, dw_dma_interrupt, 0, "dw_dmac", dw);
  1082. if (err)
  1083. goto err_irq;
  1084. platform_set_drvdata(pdev, dw);
  1085. tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
  1086. dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
  1087. INIT_LIST_HEAD(&dw->dma.channels);
  1088. for (i = 0; i < pdata->nr_channels; i++, dw->dma.chancnt++) {
  1089. struct dw_dma_chan *dwc = &dw->chan[i];
  1090. dwc->chan.device = &dw->dma;
  1091. dwc->chan.cookie = dwc->completed = 1;
  1092. dwc->chan.chan_id = i;
  1093. list_add_tail(&dwc->chan.device_node, &dw->dma.channels);
  1094. dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
  1095. spin_lock_init(&dwc->lock);
  1096. dwc->mask = 1 << i;
  1097. INIT_LIST_HEAD(&dwc->active_list);
  1098. INIT_LIST_HEAD(&dwc->queue);
  1099. INIT_LIST_HEAD(&dwc->free_list);
  1100. channel_clear_bit(dw, CH_EN, dwc->mask);
  1101. }
  1102. /* Clear/disable all interrupts on all channels. */
  1103. dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
  1104. dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
  1105. dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
  1106. dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
  1107. dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
  1108. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  1109. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  1110. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  1111. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  1112. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  1113. dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
  1114. dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
  1115. if (pdata->is_private)
  1116. dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
  1117. dw->dma.dev = &pdev->dev;
  1118. dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
  1119. dw->dma.device_free_chan_resources = dwc_free_chan_resources;
  1120. dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
  1121. dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
  1122. dw->dma.device_control = dwc_control;
  1123. dw->dma.device_tx_status = dwc_tx_status;
  1124. dw->dma.device_issue_pending = dwc_issue_pending;
  1125. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  1126. printk(KERN_INFO "%s: DesignWare DMA Controller, %d channels\n",
  1127. dev_name(&pdev->dev), dw->dma.chancnt);
  1128. dma_async_device_register(&dw->dma);
  1129. return 0;
  1130. err_irq:
  1131. clk_disable(dw->clk);
  1132. clk_put(dw->clk);
  1133. err_clk:
  1134. iounmap(dw->regs);
  1135. dw->regs = NULL;
  1136. err_release_r:
  1137. release_resource(io);
  1138. err_kfree:
  1139. kfree(dw);
  1140. return err;
  1141. }
  1142. static int __exit dw_remove(struct platform_device *pdev)
  1143. {
  1144. struct dw_dma *dw = platform_get_drvdata(pdev);
  1145. struct dw_dma_chan *dwc, *_dwc;
  1146. struct resource *io;
  1147. dw_dma_off(dw);
  1148. dma_async_device_unregister(&dw->dma);
  1149. free_irq(platform_get_irq(pdev, 0), dw);
  1150. tasklet_kill(&dw->tasklet);
  1151. list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
  1152. chan.device_node) {
  1153. list_del(&dwc->chan.device_node);
  1154. channel_clear_bit(dw, CH_EN, dwc->mask);
  1155. }
  1156. clk_disable(dw->clk);
  1157. clk_put(dw->clk);
  1158. iounmap(dw->regs);
  1159. dw->regs = NULL;
  1160. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1161. release_mem_region(io->start, DW_REGLEN);
  1162. kfree(dw);
  1163. return 0;
  1164. }
  1165. static void dw_shutdown(struct platform_device *pdev)
  1166. {
  1167. struct dw_dma *dw = platform_get_drvdata(pdev);
  1168. dw_dma_off(platform_get_drvdata(pdev));
  1169. clk_disable(dw->clk);
  1170. }
  1171. static int dw_suspend_noirq(struct device *dev)
  1172. {
  1173. struct platform_device *pdev = to_platform_device(dev);
  1174. struct dw_dma *dw = platform_get_drvdata(pdev);
  1175. dw_dma_off(platform_get_drvdata(pdev));
  1176. clk_disable(dw->clk);
  1177. return 0;
  1178. }
  1179. static int dw_resume_noirq(struct device *dev)
  1180. {
  1181. struct platform_device *pdev = to_platform_device(dev);
  1182. struct dw_dma *dw = platform_get_drvdata(pdev);
  1183. clk_enable(dw->clk);
  1184. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  1185. return 0;
  1186. }
  1187. static const struct dev_pm_ops dw_dev_pm_ops = {
  1188. .suspend_noirq = dw_suspend_noirq,
  1189. .resume_noirq = dw_resume_noirq,
  1190. };
  1191. static struct platform_driver dw_driver = {
  1192. .remove = __exit_p(dw_remove),
  1193. .shutdown = dw_shutdown,
  1194. .driver = {
  1195. .name = "dw_dmac",
  1196. .pm = &dw_dev_pm_ops,
  1197. },
  1198. };
  1199. static int __init dw_init(void)
  1200. {
  1201. return platform_driver_probe(&dw_driver, dw_probe);
  1202. }
  1203. subsys_initcall(dw_init);
  1204. static void __exit dw_exit(void)
  1205. {
  1206. platform_driver_unregister(&dw_driver);
  1207. }
  1208. module_exit(dw_exit);
  1209. MODULE_LICENSE("GPL v2");
  1210. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
  1211. MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");