dw_dmac.c 41 KB

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