dw_dmac.c 38 KB

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