core.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  1. /*
  2. * Core driver for the Synopsys DesignWare DMA Controller
  3. *
  4. * Copyright (C) 2007-2008 Atmel Corporation
  5. * Copyright (C) 2010-2011 ST Microelectronics
  6. * Copyright (C) 2013 Intel Corporation
  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/bitops.h>
  13. #include <linux/delay.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/dmapool.h>
  17. #include <linux/err.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/mm.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/pm_runtime.h>
  25. #include "../dmaengine.h"
  26. #include "internal.h"
  27. /*
  28. * This supports the Synopsys "DesignWare AHB Central DMA Controller",
  29. * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all
  30. * of which use ARM any more). See the "Databook" from Synopsys for
  31. * information beyond what licensees probably provide.
  32. *
  33. * The driver has been tested with the Atmel AT32AP7000, which does not
  34. * support descriptor writeback.
  35. */
  36. #define DWC_DEFAULT_CTLLO(_chan) ({ \
  37. struct dw_dma_chan *_dwc = to_dw_dma_chan(_chan); \
  38. struct dma_slave_config *_sconfig = &_dwc->dma_sconfig; \
  39. bool _is_slave = is_slave_direction(_dwc->direction); \
  40. u8 _smsize = _is_slave ? _sconfig->src_maxburst : \
  41. DW_DMA_MSIZE_16; \
  42. u8 _dmsize = _is_slave ? _sconfig->dst_maxburst : \
  43. DW_DMA_MSIZE_16; \
  44. u8 _dms = (_dwc->direction == DMA_MEM_TO_DEV) ? \
  45. _dwc->p_master : _dwc->m_master; \
  46. u8 _sms = (_dwc->direction == DMA_DEV_TO_MEM) ? \
  47. _dwc->p_master : _dwc->m_master; \
  48. \
  49. (DWC_CTLL_DST_MSIZE(_dmsize) \
  50. | DWC_CTLL_SRC_MSIZE(_smsize) \
  51. | DWC_CTLL_LLP_D_EN \
  52. | DWC_CTLL_LLP_S_EN \
  53. | DWC_CTLL_DMS(_dms) \
  54. | DWC_CTLL_SMS(_sms)); \
  55. })
  56. /* The set of bus widths supported by the DMA controller */
  57. #define DW_DMA_BUSWIDTHS \
  58. BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
  59. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  60. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  61. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)
  62. /*----------------------------------------------------------------------*/
  63. static struct device *chan2dev(struct dma_chan *chan)
  64. {
  65. return &chan->dev->device;
  66. }
  67. static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
  68. {
  69. return to_dw_desc(dwc->active_list.next);
  70. }
  71. static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
  72. {
  73. struct dw_desc *desc = txd_to_dw_desc(tx);
  74. struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
  75. dma_cookie_t cookie;
  76. unsigned long flags;
  77. spin_lock_irqsave(&dwc->lock, flags);
  78. cookie = dma_cookie_assign(tx);
  79. /*
  80. * REVISIT: We should attempt to chain as many descriptors as
  81. * possible, perhaps even appending to those already submitted
  82. * for DMA. But this is hard to do in a race-free manner.
  83. */
  84. list_add_tail(&desc->desc_node, &dwc->queue);
  85. spin_unlock_irqrestore(&dwc->lock, flags);
  86. dev_vdbg(chan2dev(tx->chan), "%s: queued %u\n",
  87. __func__, desc->txd.cookie);
  88. return cookie;
  89. }
  90. static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
  91. {
  92. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  93. struct dw_desc *desc;
  94. dma_addr_t phys;
  95. desc = dma_pool_zalloc(dw->desc_pool, GFP_ATOMIC, &phys);
  96. if (!desc)
  97. return NULL;
  98. dwc->descs_allocated++;
  99. INIT_LIST_HEAD(&desc->tx_list);
  100. dma_async_tx_descriptor_init(&desc->txd, &dwc->chan);
  101. desc->txd.tx_submit = dwc_tx_submit;
  102. desc->txd.flags = DMA_CTRL_ACK;
  103. desc->txd.phys = phys;
  104. return desc;
  105. }
  106. static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
  107. {
  108. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  109. struct dw_desc *child, *_next;
  110. if (unlikely(!desc))
  111. return;
  112. list_for_each_entry_safe(child, _next, &desc->tx_list, desc_node) {
  113. list_del(&child->desc_node);
  114. dma_pool_free(dw->desc_pool, child, child->txd.phys);
  115. dwc->descs_allocated--;
  116. }
  117. dma_pool_free(dw->desc_pool, desc, desc->txd.phys);
  118. dwc->descs_allocated--;
  119. }
  120. static void dwc_initialize(struct dw_dma_chan *dwc)
  121. {
  122. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  123. u32 cfghi = DWC_CFGH_FIFO_MODE;
  124. u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority);
  125. if (test_bit(DW_DMA_IS_INITIALIZED, &dwc->flags))
  126. return;
  127. cfghi |= DWC_CFGH_DST_PER(dwc->dst_id);
  128. cfghi |= DWC_CFGH_SRC_PER(dwc->src_id);
  129. channel_writel(dwc, CFG_LO, cfglo);
  130. channel_writel(dwc, CFG_HI, cfghi);
  131. /* Enable interrupts */
  132. channel_set_bit(dw, MASK.XFER, dwc->mask);
  133. channel_set_bit(dw, MASK.ERROR, dwc->mask);
  134. set_bit(DW_DMA_IS_INITIALIZED, &dwc->flags);
  135. }
  136. /*----------------------------------------------------------------------*/
  137. static inline void dwc_dump_chan_regs(struct dw_dma_chan *dwc)
  138. {
  139. dev_err(chan2dev(&dwc->chan),
  140. " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
  141. channel_readl(dwc, SAR),
  142. channel_readl(dwc, DAR),
  143. channel_readl(dwc, LLP),
  144. channel_readl(dwc, CTL_HI),
  145. channel_readl(dwc, CTL_LO));
  146. }
  147. static inline void dwc_chan_disable(struct dw_dma *dw, struct dw_dma_chan *dwc)
  148. {
  149. channel_clear_bit(dw, CH_EN, dwc->mask);
  150. while (dma_readl(dw, CH_EN) & dwc->mask)
  151. cpu_relax();
  152. }
  153. /*----------------------------------------------------------------------*/
  154. /* Perform single block transfer */
  155. static inline void dwc_do_single_block(struct dw_dma_chan *dwc,
  156. struct dw_desc *desc)
  157. {
  158. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  159. u32 ctllo;
  160. /*
  161. * Software emulation of LLP mode relies on interrupts to continue
  162. * multi block transfer.
  163. */
  164. ctllo = lli_read(desc, ctllo) | DWC_CTLL_INT_EN;
  165. channel_writel(dwc, SAR, lli_read(desc, sar));
  166. channel_writel(dwc, DAR, lli_read(desc, dar));
  167. channel_writel(dwc, CTL_LO, ctllo);
  168. channel_writel(dwc, CTL_HI, lli_read(desc, ctlhi));
  169. channel_set_bit(dw, CH_EN, dwc->mask);
  170. /* Move pointer to next descriptor */
  171. dwc->tx_node_active = dwc->tx_node_active->next;
  172. }
  173. /* Called with dwc->lock held and bh disabled */
  174. static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
  175. {
  176. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  177. u8 lms = DWC_LLP_LMS(dwc->m_master);
  178. unsigned long was_soft_llp;
  179. /* ASSERT: channel is idle */
  180. if (dma_readl(dw, CH_EN) & dwc->mask) {
  181. dev_err(chan2dev(&dwc->chan),
  182. "%s: BUG: Attempted to start non-idle channel\n",
  183. __func__);
  184. dwc_dump_chan_regs(dwc);
  185. /* The tasklet will hopefully advance the queue... */
  186. return;
  187. }
  188. if (dwc->nollp) {
  189. was_soft_llp = test_and_set_bit(DW_DMA_IS_SOFT_LLP,
  190. &dwc->flags);
  191. if (was_soft_llp) {
  192. dev_err(chan2dev(&dwc->chan),
  193. "BUG: Attempted to start new LLP transfer inside ongoing one\n");
  194. return;
  195. }
  196. dwc_initialize(dwc);
  197. first->residue = first->total_len;
  198. dwc->tx_node_active = &first->tx_list;
  199. /* Submit first block */
  200. dwc_do_single_block(dwc, first);
  201. return;
  202. }
  203. dwc_initialize(dwc);
  204. channel_writel(dwc, LLP, first->txd.phys | lms);
  205. channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  206. channel_writel(dwc, CTL_HI, 0);
  207. channel_set_bit(dw, CH_EN, dwc->mask);
  208. }
  209. static void dwc_dostart_first_queued(struct dw_dma_chan *dwc)
  210. {
  211. struct dw_desc *desc;
  212. if (list_empty(&dwc->queue))
  213. return;
  214. list_move(dwc->queue.next, &dwc->active_list);
  215. desc = dwc_first_active(dwc);
  216. dev_vdbg(chan2dev(&dwc->chan), "%s: started %u\n", __func__, desc->txd.cookie);
  217. dwc_dostart(dwc, desc);
  218. }
  219. /*----------------------------------------------------------------------*/
  220. static void
  221. dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
  222. bool callback_required)
  223. {
  224. dma_async_tx_callback callback = NULL;
  225. void *param = NULL;
  226. struct dma_async_tx_descriptor *txd = &desc->txd;
  227. struct dw_desc *child;
  228. unsigned long flags;
  229. dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
  230. spin_lock_irqsave(&dwc->lock, flags);
  231. dma_cookie_complete(txd);
  232. if (callback_required) {
  233. callback = txd->callback;
  234. param = txd->callback_param;
  235. }
  236. /* async_tx_ack */
  237. list_for_each_entry(child, &desc->tx_list, desc_node)
  238. async_tx_ack(&child->txd);
  239. async_tx_ack(&desc->txd);
  240. dwc_desc_put(dwc, desc);
  241. spin_unlock_irqrestore(&dwc->lock, flags);
  242. if (callback)
  243. callback(param);
  244. }
  245. static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
  246. {
  247. struct dw_desc *desc, *_desc;
  248. LIST_HEAD(list);
  249. unsigned long flags;
  250. spin_lock_irqsave(&dwc->lock, flags);
  251. if (dma_readl(dw, CH_EN) & dwc->mask) {
  252. dev_err(chan2dev(&dwc->chan),
  253. "BUG: XFER bit set, but channel not idle!\n");
  254. /* Try to continue after resetting the channel... */
  255. dwc_chan_disable(dw, dwc);
  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. dwc_dostart_first_queued(dwc);
  263. spin_unlock_irqrestore(&dwc->lock, flags);
  264. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  265. dwc_descriptor_complete(dwc, desc, true);
  266. }
  267. /* Returns how many bytes were already received from source */
  268. static inline u32 dwc_get_sent(struct dw_dma_chan *dwc)
  269. {
  270. u32 ctlhi = channel_readl(dwc, CTL_HI);
  271. u32 ctllo = channel_readl(dwc, CTL_LO);
  272. return (ctlhi & DWC_CTLH_BLOCK_TS_MASK) * (1 << (ctllo >> 4 & 7));
  273. }
  274. static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
  275. {
  276. dma_addr_t llp;
  277. struct dw_desc *desc, *_desc;
  278. struct dw_desc *child;
  279. u32 status_xfer;
  280. unsigned long flags;
  281. spin_lock_irqsave(&dwc->lock, flags);
  282. llp = channel_readl(dwc, LLP);
  283. status_xfer = dma_readl(dw, RAW.XFER);
  284. if (status_xfer & dwc->mask) {
  285. /* Everything we've submitted is done */
  286. dma_writel(dw, CLEAR.XFER, dwc->mask);
  287. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
  288. struct list_head *head, *active = dwc->tx_node_active;
  289. /*
  290. * We are inside first active descriptor.
  291. * Otherwise something is really wrong.
  292. */
  293. desc = dwc_first_active(dwc);
  294. head = &desc->tx_list;
  295. if (active != head) {
  296. /* Update residue to reflect last sent descriptor */
  297. if (active == head->next)
  298. desc->residue -= desc->len;
  299. else
  300. desc->residue -= to_dw_desc(active->prev)->len;
  301. child = to_dw_desc(active);
  302. /* Submit next block */
  303. dwc_do_single_block(dwc, child);
  304. spin_unlock_irqrestore(&dwc->lock, flags);
  305. return;
  306. }
  307. /* We are done here */
  308. clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
  309. }
  310. spin_unlock_irqrestore(&dwc->lock, flags);
  311. dwc_complete_all(dw, dwc);
  312. return;
  313. }
  314. if (list_empty(&dwc->active_list)) {
  315. spin_unlock_irqrestore(&dwc->lock, flags);
  316. return;
  317. }
  318. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
  319. dev_vdbg(chan2dev(&dwc->chan), "%s: soft LLP mode\n", __func__);
  320. spin_unlock_irqrestore(&dwc->lock, flags);
  321. return;
  322. }
  323. dev_vdbg(chan2dev(&dwc->chan), "%s: llp=%pad\n", __func__, &llp);
  324. list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
  325. /* Initial residue value */
  326. desc->residue = desc->total_len;
  327. /* Check first descriptors addr */
  328. if (desc->txd.phys == DWC_LLP_LOC(llp)) {
  329. spin_unlock_irqrestore(&dwc->lock, flags);
  330. return;
  331. }
  332. /* Check first descriptors llp */
  333. if (lli_read(desc, llp) == llp) {
  334. /* This one is currently in progress */
  335. desc->residue -= dwc_get_sent(dwc);
  336. spin_unlock_irqrestore(&dwc->lock, flags);
  337. return;
  338. }
  339. desc->residue -= desc->len;
  340. list_for_each_entry(child, &desc->tx_list, desc_node) {
  341. if (lli_read(child, llp) == llp) {
  342. /* Currently in progress */
  343. desc->residue -= dwc_get_sent(dwc);
  344. spin_unlock_irqrestore(&dwc->lock, flags);
  345. return;
  346. }
  347. desc->residue -= child->len;
  348. }
  349. /*
  350. * No descriptors so far seem to be in progress, i.e.
  351. * this one must be done.
  352. */
  353. spin_unlock_irqrestore(&dwc->lock, flags);
  354. dwc_descriptor_complete(dwc, desc, true);
  355. spin_lock_irqsave(&dwc->lock, flags);
  356. }
  357. dev_err(chan2dev(&dwc->chan),
  358. "BUG: All descriptors done, but channel not idle!\n");
  359. /* Try to continue after resetting the channel... */
  360. dwc_chan_disable(dw, dwc);
  361. dwc_dostart_first_queued(dwc);
  362. spin_unlock_irqrestore(&dwc->lock, flags);
  363. }
  364. static inline void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_desc *desc)
  365. {
  366. dev_crit(chan2dev(&dwc->chan), " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
  367. lli_read(desc, sar),
  368. lli_read(desc, dar),
  369. lli_read(desc, llp),
  370. lli_read(desc, ctlhi),
  371. lli_read(desc, ctllo));
  372. }
  373. static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
  374. {
  375. struct dw_desc *bad_desc;
  376. struct dw_desc *child;
  377. unsigned long flags;
  378. dwc_scan_descriptors(dw, dwc);
  379. spin_lock_irqsave(&dwc->lock, flags);
  380. /*
  381. * The descriptor currently at the head of the active list is
  382. * borked. Since we don't have any way to report errors, we'll
  383. * just have to scream loudly and try to carry on.
  384. */
  385. bad_desc = dwc_first_active(dwc);
  386. list_del_init(&bad_desc->desc_node);
  387. list_move(dwc->queue.next, dwc->active_list.prev);
  388. /* Clear the error flag and try to restart the controller */
  389. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  390. if (!list_empty(&dwc->active_list))
  391. dwc_dostart(dwc, dwc_first_active(dwc));
  392. /*
  393. * WARN may seem harsh, but since this only happens
  394. * when someone submits a bad physical address in a
  395. * descriptor, we should consider ourselves lucky that the
  396. * controller flagged an error instead of scribbling over
  397. * random memory locations.
  398. */
  399. dev_WARN(chan2dev(&dwc->chan), "Bad descriptor submitted for DMA!\n"
  400. " cookie: %d\n", bad_desc->txd.cookie);
  401. dwc_dump_lli(dwc, bad_desc);
  402. list_for_each_entry(child, &bad_desc->tx_list, desc_node)
  403. dwc_dump_lli(dwc, child);
  404. spin_unlock_irqrestore(&dwc->lock, flags);
  405. /* Pretend the descriptor completed successfully */
  406. dwc_descriptor_complete(dwc, bad_desc, true);
  407. }
  408. /* --------------------- Cyclic DMA API extensions -------------------- */
  409. dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
  410. {
  411. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  412. return channel_readl(dwc, SAR);
  413. }
  414. EXPORT_SYMBOL(dw_dma_get_src_addr);
  415. dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
  416. {
  417. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  418. return channel_readl(dwc, DAR);
  419. }
  420. EXPORT_SYMBOL(dw_dma_get_dst_addr);
  421. /* Called with dwc->lock held and all DMAC interrupts disabled */
  422. static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
  423. u32 status_block, u32 status_err, u32 status_xfer)
  424. {
  425. unsigned long flags;
  426. if (status_block & dwc->mask) {
  427. void (*callback)(void *param);
  428. void *callback_param;
  429. dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
  430. channel_readl(dwc, LLP));
  431. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  432. callback = dwc->cdesc->period_callback;
  433. callback_param = dwc->cdesc->period_callback_param;
  434. if (callback)
  435. callback(callback_param);
  436. }
  437. /*
  438. * Error and transfer complete are highly unlikely, and will most
  439. * likely be due to a configuration error by the user.
  440. */
  441. if (unlikely(status_err & dwc->mask) ||
  442. unlikely(status_xfer & dwc->mask)) {
  443. unsigned int i;
  444. dev_err(chan2dev(&dwc->chan),
  445. "cyclic DMA unexpected %s interrupt, stopping DMA transfer\n",
  446. status_xfer ? "xfer" : "error");
  447. spin_lock_irqsave(&dwc->lock, flags);
  448. dwc_dump_chan_regs(dwc);
  449. dwc_chan_disable(dw, dwc);
  450. /* Make sure DMA does not restart by loading a new list */
  451. channel_writel(dwc, LLP, 0);
  452. channel_writel(dwc, CTL_LO, 0);
  453. channel_writel(dwc, CTL_HI, 0);
  454. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  455. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  456. dma_writel(dw, CLEAR.XFER, dwc->mask);
  457. for (i = 0; i < dwc->cdesc->periods; i++)
  458. dwc_dump_lli(dwc, dwc->cdesc->desc[i]);
  459. spin_unlock_irqrestore(&dwc->lock, flags);
  460. }
  461. /* Re-enable interrupts */
  462. channel_set_bit(dw, MASK.BLOCK, dwc->mask);
  463. }
  464. /* ------------------------------------------------------------------------- */
  465. static void dw_dma_tasklet(unsigned long data)
  466. {
  467. struct dw_dma *dw = (struct dw_dma *)data;
  468. struct dw_dma_chan *dwc;
  469. u32 status_block;
  470. u32 status_xfer;
  471. u32 status_err;
  472. unsigned int i;
  473. status_block = dma_readl(dw, RAW.BLOCK);
  474. status_xfer = dma_readl(dw, RAW.XFER);
  475. status_err = dma_readl(dw, RAW.ERROR);
  476. dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err);
  477. for (i = 0; i < dw->dma.chancnt; i++) {
  478. dwc = &dw->chan[i];
  479. if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
  480. dwc_handle_cyclic(dw, dwc, status_block, status_err,
  481. status_xfer);
  482. else if (status_err & (1 << i))
  483. dwc_handle_error(dw, dwc);
  484. else if (status_xfer & (1 << i))
  485. dwc_scan_descriptors(dw, dwc);
  486. }
  487. /* Re-enable interrupts */
  488. channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
  489. channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
  490. }
  491. static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
  492. {
  493. struct dw_dma *dw = dev_id;
  494. u32 status;
  495. /* Check if we have any interrupt from the DMAC which is not in use */
  496. if (!dw->in_use)
  497. return IRQ_NONE;
  498. status = dma_readl(dw, STATUS_INT);
  499. dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__, status);
  500. /* Check if we have any interrupt from the DMAC */
  501. if (!status)
  502. return IRQ_NONE;
  503. /*
  504. * Just disable the interrupts. We'll turn them back on in the
  505. * softirq handler.
  506. */
  507. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  508. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  509. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  510. status = dma_readl(dw, STATUS_INT);
  511. if (status) {
  512. dev_err(dw->dma.dev,
  513. "BUG: Unexpected interrupts pending: 0x%x\n",
  514. status);
  515. /* Try to recover */
  516. channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
  517. channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1);
  518. channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
  519. channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
  520. channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
  521. }
  522. tasklet_schedule(&dw->tasklet);
  523. return IRQ_HANDLED;
  524. }
  525. /*----------------------------------------------------------------------*/
  526. static struct dma_async_tx_descriptor *
  527. dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  528. size_t len, unsigned long flags)
  529. {
  530. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  531. struct dw_dma *dw = to_dw_dma(chan->device);
  532. struct dw_desc *desc;
  533. struct dw_desc *first;
  534. struct dw_desc *prev;
  535. size_t xfer_count;
  536. size_t offset;
  537. u8 m_master = dwc->m_master;
  538. unsigned int src_width;
  539. unsigned int dst_width;
  540. unsigned int data_width = dw->pdata->data_width[m_master];
  541. u32 ctllo;
  542. u8 lms = DWC_LLP_LMS(m_master);
  543. dev_vdbg(chan2dev(chan),
  544. "%s: d%pad s%pad l0x%zx f0x%lx\n", __func__,
  545. &dest, &src, len, flags);
  546. if (unlikely(!len)) {
  547. dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
  548. return NULL;
  549. }
  550. dwc->direction = DMA_MEM_TO_MEM;
  551. src_width = dst_width = __ffs(data_width | src | dest | len);
  552. ctllo = DWC_DEFAULT_CTLLO(chan)
  553. | DWC_CTLL_DST_WIDTH(dst_width)
  554. | DWC_CTLL_SRC_WIDTH(src_width)
  555. | DWC_CTLL_DST_INC
  556. | DWC_CTLL_SRC_INC
  557. | DWC_CTLL_FC_M2M;
  558. prev = first = NULL;
  559. for (offset = 0; offset < len; offset += xfer_count << src_width) {
  560. xfer_count = min_t(size_t, (len - offset) >> src_width,
  561. dwc->block_size);
  562. desc = dwc_desc_get(dwc);
  563. if (!desc)
  564. goto err_desc_get;
  565. lli_write(desc, sar, src + offset);
  566. lli_write(desc, dar, dest + offset);
  567. lli_write(desc, ctllo, ctllo);
  568. lli_write(desc, ctlhi, xfer_count);
  569. desc->len = xfer_count << src_width;
  570. if (!first) {
  571. first = desc;
  572. } else {
  573. lli_write(prev, llp, desc->txd.phys | lms);
  574. list_add_tail(&desc->desc_node, &first->tx_list);
  575. }
  576. prev = desc;
  577. }
  578. if (flags & DMA_PREP_INTERRUPT)
  579. /* Trigger interrupt after last block */
  580. lli_set(prev, ctllo, DWC_CTLL_INT_EN);
  581. prev->lli.llp = 0;
  582. lli_clear(prev, ctllo, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  583. first->txd.flags = flags;
  584. first->total_len = len;
  585. return &first->txd;
  586. err_desc_get:
  587. dwc_desc_put(dwc, first);
  588. return NULL;
  589. }
  590. static struct dma_async_tx_descriptor *
  591. dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  592. unsigned int sg_len, enum dma_transfer_direction direction,
  593. unsigned long flags, void *context)
  594. {
  595. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  596. struct dw_dma *dw = to_dw_dma(chan->device);
  597. struct dma_slave_config *sconfig = &dwc->dma_sconfig;
  598. struct dw_desc *prev;
  599. struct dw_desc *first;
  600. u32 ctllo;
  601. u8 m_master = dwc->m_master;
  602. u8 lms = DWC_LLP_LMS(m_master);
  603. dma_addr_t reg;
  604. unsigned int reg_width;
  605. unsigned int mem_width;
  606. unsigned int data_width = dw->pdata->data_width[m_master];
  607. unsigned int i;
  608. struct scatterlist *sg;
  609. size_t total_len = 0;
  610. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  611. if (unlikely(!is_slave_direction(direction) || !sg_len))
  612. return NULL;
  613. dwc->direction = direction;
  614. prev = first = NULL;
  615. switch (direction) {
  616. case DMA_MEM_TO_DEV:
  617. reg_width = __ffs(sconfig->dst_addr_width);
  618. reg = sconfig->dst_addr;
  619. ctllo = (DWC_DEFAULT_CTLLO(chan)
  620. | DWC_CTLL_DST_WIDTH(reg_width)
  621. | DWC_CTLL_DST_FIX
  622. | DWC_CTLL_SRC_INC);
  623. ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
  624. DWC_CTLL_FC(DW_DMA_FC_D_M2P);
  625. for_each_sg(sgl, sg, sg_len, i) {
  626. struct dw_desc *desc;
  627. u32 len, dlen, mem;
  628. mem = sg_dma_address(sg);
  629. len = sg_dma_len(sg);
  630. mem_width = __ffs(data_width | mem | len);
  631. slave_sg_todev_fill_desc:
  632. desc = dwc_desc_get(dwc);
  633. if (!desc)
  634. goto err_desc_get;
  635. lli_write(desc, sar, mem);
  636. lli_write(desc, dar, reg);
  637. lli_write(desc, ctllo, ctllo | DWC_CTLL_SRC_WIDTH(mem_width));
  638. if ((len >> mem_width) > dwc->block_size) {
  639. dlen = dwc->block_size << mem_width;
  640. mem += dlen;
  641. len -= dlen;
  642. } else {
  643. dlen = len;
  644. len = 0;
  645. }
  646. lli_write(desc, ctlhi, dlen >> mem_width);
  647. desc->len = dlen;
  648. if (!first) {
  649. first = desc;
  650. } else {
  651. lli_write(prev, llp, desc->txd.phys | lms);
  652. list_add_tail(&desc->desc_node, &first->tx_list);
  653. }
  654. prev = desc;
  655. total_len += dlen;
  656. if (len)
  657. goto slave_sg_todev_fill_desc;
  658. }
  659. break;
  660. case DMA_DEV_TO_MEM:
  661. reg_width = __ffs(sconfig->src_addr_width);
  662. reg = sconfig->src_addr;
  663. ctllo = (DWC_DEFAULT_CTLLO(chan)
  664. | DWC_CTLL_SRC_WIDTH(reg_width)
  665. | DWC_CTLL_DST_INC
  666. | DWC_CTLL_SRC_FIX);
  667. ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
  668. DWC_CTLL_FC(DW_DMA_FC_D_P2M);
  669. for_each_sg(sgl, sg, sg_len, i) {
  670. struct dw_desc *desc;
  671. u32 len, dlen, mem;
  672. mem = sg_dma_address(sg);
  673. len = sg_dma_len(sg);
  674. mem_width = __ffs(data_width | mem | len);
  675. slave_sg_fromdev_fill_desc:
  676. desc = dwc_desc_get(dwc);
  677. if (!desc)
  678. goto err_desc_get;
  679. lli_write(desc, sar, reg);
  680. lli_write(desc, dar, mem);
  681. lli_write(desc, ctllo, ctllo | DWC_CTLL_DST_WIDTH(mem_width));
  682. if ((len >> reg_width) > dwc->block_size) {
  683. dlen = dwc->block_size << reg_width;
  684. mem += dlen;
  685. len -= dlen;
  686. } else {
  687. dlen = len;
  688. len = 0;
  689. }
  690. lli_write(desc, ctlhi, dlen >> reg_width);
  691. desc->len = dlen;
  692. if (!first) {
  693. first = desc;
  694. } else {
  695. lli_write(prev, llp, desc->txd.phys | lms);
  696. list_add_tail(&desc->desc_node, &first->tx_list);
  697. }
  698. prev = desc;
  699. total_len += dlen;
  700. if (len)
  701. goto slave_sg_fromdev_fill_desc;
  702. }
  703. break;
  704. default:
  705. return NULL;
  706. }
  707. if (flags & DMA_PREP_INTERRUPT)
  708. /* Trigger interrupt after last block */
  709. lli_set(prev, ctllo, DWC_CTLL_INT_EN);
  710. prev->lli.llp = 0;
  711. lli_clear(prev, ctllo, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  712. first->total_len = total_len;
  713. return &first->txd;
  714. err_desc_get:
  715. dev_err(chan2dev(chan),
  716. "not enough descriptors available. Direction %d\n", direction);
  717. dwc_desc_put(dwc, first);
  718. return NULL;
  719. }
  720. bool dw_dma_filter(struct dma_chan *chan, void *param)
  721. {
  722. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  723. struct dw_dma_slave *dws = param;
  724. if (dws->dma_dev != chan->device->dev)
  725. return false;
  726. /* We have to copy data since dws can be temporary storage */
  727. dwc->src_id = dws->src_id;
  728. dwc->dst_id = dws->dst_id;
  729. dwc->m_master = dws->m_master;
  730. dwc->p_master = dws->p_master;
  731. return true;
  732. }
  733. EXPORT_SYMBOL_GPL(dw_dma_filter);
  734. /*
  735. * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
  736. * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
  737. *
  738. * NOTE: burst size 2 is not supported by controller.
  739. *
  740. * This can be done by finding least significant bit set: n & (n - 1)
  741. */
  742. static inline void convert_burst(u32 *maxburst)
  743. {
  744. if (*maxburst > 1)
  745. *maxburst = fls(*maxburst) - 2;
  746. else
  747. *maxburst = 0;
  748. }
  749. static int dwc_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
  750. {
  751. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  752. /* Check if chan will be configured for slave transfers */
  753. if (!is_slave_direction(sconfig->direction))
  754. return -EINVAL;
  755. memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
  756. dwc->direction = sconfig->direction;
  757. convert_burst(&dwc->dma_sconfig.src_maxburst);
  758. convert_burst(&dwc->dma_sconfig.dst_maxburst);
  759. return 0;
  760. }
  761. static int dwc_pause(struct dma_chan *chan)
  762. {
  763. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  764. unsigned long flags;
  765. unsigned int count = 20; /* timeout iterations */
  766. u32 cfglo;
  767. spin_lock_irqsave(&dwc->lock, flags);
  768. cfglo = channel_readl(dwc, CFG_LO);
  769. channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
  770. while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY) && count--)
  771. udelay(2);
  772. set_bit(DW_DMA_IS_PAUSED, &dwc->flags);
  773. spin_unlock_irqrestore(&dwc->lock, flags);
  774. return 0;
  775. }
  776. static inline void dwc_chan_resume(struct dw_dma_chan *dwc)
  777. {
  778. u32 cfglo = channel_readl(dwc, CFG_LO);
  779. channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
  780. clear_bit(DW_DMA_IS_PAUSED, &dwc->flags);
  781. }
  782. static int dwc_resume(struct dma_chan *chan)
  783. {
  784. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  785. unsigned long flags;
  786. spin_lock_irqsave(&dwc->lock, flags);
  787. if (test_bit(DW_DMA_IS_PAUSED, &dwc->flags))
  788. dwc_chan_resume(dwc);
  789. spin_unlock_irqrestore(&dwc->lock, flags);
  790. return 0;
  791. }
  792. static int dwc_terminate_all(struct dma_chan *chan)
  793. {
  794. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  795. struct dw_dma *dw = to_dw_dma(chan->device);
  796. struct dw_desc *desc, *_desc;
  797. unsigned long flags;
  798. LIST_HEAD(list);
  799. spin_lock_irqsave(&dwc->lock, flags);
  800. clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
  801. dwc_chan_disable(dw, dwc);
  802. dwc_chan_resume(dwc);
  803. /* active_list entries will end up before queued entries */
  804. list_splice_init(&dwc->queue, &list);
  805. list_splice_init(&dwc->active_list, &list);
  806. spin_unlock_irqrestore(&dwc->lock, flags);
  807. /* Flush all pending and queued descriptors */
  808. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  809. dwc_descriptor_complete(dwc, desc, false);
  810. return 0;
  811. }
  812. static struct dw_desc *dwc_find_desc(struct dw_dma_chan *dwc, dma_cookie_t c)
  813. {
  814. struct dw_desc *desc;
  815. list_for_each_entry(desc, &dwc->active_list, desc_node)
  816. if (desc->txd.cookie == c)
  817. return desc;
  818. return NULL;
  819. }
  820. static u32 dwc_get_residue(struct dw_dma_chan *dwc, dma_cookie_t cookie)
  821. {
  822. struct dw_desc *desc;
  823. unsigned long flags;
  824. u32 residue;
  825. spin_lock_irqsave(&dwc->lock, flags);
  826. desc = dwc_find_desc(dwc, cookie);
  827. if (desc) {
  828. if (desc == dwc_first_active(dwc)) {
  829. residue = desc->residue;
  830. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags) && residue)
  831. residue -= dwc_get_sent(dwc);
  832. } else {
  833. residue = desc->total_len;
  834. }
  835. } else {
  836. residue = 0;
  837. }
  838. spin_unlock_irqrestore(&dwc->lock, flags);
  839. return residue;
  840. }
  841. static enum dma_status
  842. dwc_tx_status(struct dma_chan *chan,
  843. dma_cookie_t cookie,
  844. struct dma_tx_state *txstate)
  845. {
  846. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  847. enum dma_status ret;
  848. ret = dma_cookie_status(chan, cookie, txstate);
  849. if (ret == DMA_COMPLETE)
  850. return ret;
  851. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  852. ret = dma_cookie_status(chan, cookie, txstate);
  853. if (ret == DMA_COMPLETE)
  854. return ret;
  855. dma_set_residue(txstate, dwc_get_residue(dwc, cookie));
  856. if (test_bit(DW_DMA_IS_PAUSED, &dwc->flags) && ret == DMA_IN_PROGRESS)
  857. return DMA_PAUSED;
  858. return ret;
  859. }
  860. static void dwc_issue_pending(struct dma_chan *chan)
  861. {
  862. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  863. unsigned long flags;
  864. spin_lock_irqsave(&dwc->lock, flags);
  865. if (list_empty(&dwc->active_list))
  866. dwc_dostart_first_queued(dwc);
  867. spin_unlock_irqrestore(&dwc->lock, flags);
  868. }
  869. /*----------------------------------------------------------------------*/
  870. static void dw_dma_off(struct dw_dma *dw)
  871. {
  872. unsigned int i;
  873. dma_writel(dw, CFG, 0);
  874. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  875. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  876. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  877. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  878. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  879. while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
  880. cpu_relax();
  881. for (i = 0; i < dw->dma.chancnt; i++)
  882. clear_bit(DW_DMA_IS_INITIALIZED, &dw->chan[i].flags);
  883. }
  884. static void dw_dma_on(struct dw_dma *dw)
  885. {
  886. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  887. }
  888. static int dwc_alloc_chan_resources(struct dma_chan *chan)
  889. {
  890. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  891. struct dw_dma *dw = to_dw_dma(chan->device);
  892. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  893. /* ASSERT: channel is idle */
  894. if (dma_readl(dw, CH_EN) & dwc->mask) {
  895. dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
  896. return -EIO;
  897. }
  898. dma_cookie_init(chan);
  899. /*
  900. * NOTE: some controllers may have additional features that we
  901. * need to initialize here, like "scatter-gather" (which
  902. * doesn't mean what you think it means), and status writeback.
  903. */
  904. /*
  905. * We need controller-specific data to set up slave transfers.
  906. */
  907. if (chan->private && !dw_dma_filter(chan, chan->private)) {
  908. dev_warn(chan2dev(chan), "Wrong controller-specific data\n");
  909. return -EINVAL;
  910. }
  911. /* Enable controller here if needed */
  912. if (!dw->in_use)
  913. dw_dma_on(dw);
  914. dw->in_use |= dwc->mask;
  915. return 0;
  916. }
  917. static void dwc_free_chan_resources(struct dma_chan *chan)
  918. {
  919. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  920. struct dw_dma *dw = to_dw_dma(chan->device);
  921. unsigned long flags;
  922. LIST_HEAD(list);
  923. dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__,
  924. dwc->descs_allocated);
  925. /* ASSERT: channel is idle */
  926. BUG_ON(!list_empty(&dwc->active_list));
  927. BUG_ON(!list_empty(&dwc->queue));
  928. BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
  929. spin_lock_irqsave(&dwc->lock, flags);
  930. /* Clear custom channel configuration */
  931. dwc->src_id = 0;
  932. dwc->dst_id = 0;
  933. dwc->m_master = 0;
  934. dwc->p_master = 0;
  935. clear_bit(DW_DMA_IS_INITIALIZED, &dwc->flags);
  936. /* Disable interrupts */
  937. channel_clear_bit(dw, MASK.XFER, dwc->mask);
  938. channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
  939. channel_clear_bit(dw, MASK.ERROR, dwc->mask);
  940. spin_unlock_irqrestore(&dwc->lock, flags);
  941. /* Disable controller in case it was a last user */
  942. dw->in_use &= ~dwc->mask;
  943. if (!dw->in_use)
  944. dw_dma_off(dw);
  945. dev_vdbg(chan2dev(chan), "%s: done\n", __func__);
  946. }
  947. /* --------------------- Cyclic DMA API extensions -------------------- */
  948. /**
  949. * dw_dma_cyclic_start - start the cyclic DMA transfer
  950. * @chan: the DMA channel to start
  951. *
  952. * Must be called with soft interrupts disabled. Returns zero on success or
  953. * -errno on failure.
  954. */
  955. int dw_dma_cyclic_start(struct dma_chan *chan)
  956. {
  957. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  958. struct dw_dma *dw = to_dw_dma(chan->device);
  959. unsigned long flags;
  960. if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
  961. dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
  962. return -ENODEV;
  963. }
  964. spin_lock_irqsave(&dwc->lock, flags);
  965. /* Enable interrupts to perform cyclic transfer */
  966. channel_set_bit(dw, MASK.BLOCK, dwc->mask);
  967. dwc_dostart(dwc, dwc->cdesc->desc[0]);
  968. spin_unlock_irqrestore(&dwc->lock, flags);
  969. return 0;
  970. }
  971. EXPORT_SYMBOL(dw_dma_cyclic_start);
  972. /**
  973. * dw_dma_cyclic_stop - stop the cyclic DMA transfer
  974. * @chan: the DMA channel to stop
  975. *
  976. * Must be called with soft interrupts disabled.
  977. */
  978. void dw_dma_cyclic_stop(struct dma_chan *chan)
  979. {
  980. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  981. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  982. unsigned long flags;
  983. spin_lock_irqsave(&dwc->lock, flags);
  984. dwc_chan_disable(dw, dwc);
  985. spin_unlock_irqrestore(&dwc->lock, flags);
  986. }
  987. EXPORT_SYMBOL(dw_dma_cyclic_stop);
  988. /**
  989. * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
  990. * @chan: the DMA channel to prepare
  991. * @buf_addr: physical DMA address where the buffer starts
  992. * @buf_len: total number of bytes for the entire buffer
  993. * @period_len: number of bytes for each period
  994. * @direction: transfer direction, to or from device
  995. *
  996. * Must be called before trying to start the transfer. Returns a valid struct
  997. * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
  998. */
  999. struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
  1000. dma_addr_t buf_addr, size_t buf_len, size_t period_len,
  1001. enum dma_transfer_direction direction)
  1002. {
  1003. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1004. struct dma_slave_config *sconfig = &dwc->dma_sconfig;
  1005. struct dw_cyclic_desc *cdesc;
  1006. struct dw_cyclic_desc *retval = NULL;
  1007. struct dw_desc *desc;
  1008. struct dw_desc *last = NULL;
  1009. u8 lms = DWC_LLP_LMS(dwc->m_master);
  1010. unsigned long was_cyclic;
  1011. unsigned int reg_width;
  1012. unsigned int periods;
  1013. unsigned int i;
  1014. unsigned long flags;
  1015. spin_lock_irqsave(&dwc->lock, flags);
  1016. if (dwc->nollp) {
  1017. spin_unlock_irqrestore(&dwc->lock, flags);
  1018. dev_dbg(chan2dev(&dwc->chan),
  1019. "channel doesn't support LLP transfers\n");
  1020. return ERR_PTR(-EINVAL);
  1021. }
  1022. if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
  1023. spin_unlock_irqrestore(&dwc->lock, flags);
  1024. dev_dbg(chan2dev(&dwc->chan),
  1025. "queue and/or active list are not empty\n");
  1026. return ERR_PTR(-EBUSY);
  1027. }
  1028. was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1029. spin_unlock_irqrestore(&dwc->lock, flags);
  1030. if (was_cyclic) {
  1031. dev_dbg(chan2dev(&dwc->chan),
  1032. "channel already prepared for cyclic DMA\n");
  1033. return ERR_PTR(-EBUSY);
  1034. }
  1035. retval = ERR_PTR(-EINVAL);
  1036. if (unlikely(!is_slave_direction(direction)))
  1037. goto out_err;
  1038. dwc->direction = direction;
  1039. if (direction == DMA_MEM_TO_DEV)
  1040. reg_width = __ffs(sconfig->dst_addr_width);
  1041. else
  1042. reg_width = __ffs(sconfig->src_addr_width);
  1043. periods = buf_len / period_len;
  1044. /* Check for too big/unaligned periods and unaligned DMA buffer. */
  1045. if (period_len > (dwc->block_size << reg_width))
  1046. goto out_err;
  1047. if (unlikely(period_len & ((1 << reg_width) - 1)))
  1048. goto out_err;
  1049. if (unlikely(buf_addr & ((1 << reg_width) - 1)))
  1050. goto out_err;
  1051. retval = ERR_PTR(-ENOMEM);
  1052. cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
  1053. if (!cdesc)
  1054. goto out_err;
  1055. cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
  1056. if (!cdesc->desc)
  1057. goto out_err_alloc;
  1058. for (i = 0; i < periods; i++) {
  1059. desc = dwc_desc_get(dwc);
  1060. if (!desc)
  1061. goto out_err_desc_get;
  1062. switch (direction) {
  1063. case DMA_MEM_TO_DEV:
  1064. lli_write(desc, dar, sconfig->dst_addr);
  1065. lli_write(desc, sar, buf_addr + period_len * i);
  1066. lli_write(desc, ctllo, (DWC_DEFAULT_CTLLO(chan)
  1067. | DWC_CTLL_DST_WIDTH(reg_width)
  1068. | DWC_CTLL_SRC_WIDTH(reg_width)
  1069. | DWC_CTLL_DST_FIX
  1070. | DWC_CTLL_SRC_INC
  1071. | DWC_CTLL_INT_EN));
  1072. lli_set(desc, ctllo, sconfig->device_fc ?
  1073. DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
  1074. DWC_CTLL_FC(DW_DMA_FC_D_M2P));
  1075. break;
  1076. case DMA_DEV_TO_MEM:
  1077. lli_write(desc, dar, buf_addr + period_len * i);
  1078. lli_write(desc, sar, sconfig->src_addr);
  1079. lli_write(desc, ctllo, (DWC_DEFAULT_CTLLO(chan)
  1080. | DWC_CTLL_SRC_WIDTH(reg_width)
  1081. | DWC_CTLL_DST_WIDTH(reg_width)
  1082. | DWC_CTLL_DST_INC
  1083. | DWC_CTLL_SRC_FIX
  1084. | DWC_CTLL_INT_EN));
  1085. lli_set(desc, ctllo, sconfig->device_fc ?
  1086. DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
  1087. DWC_CTLL_FC(DW_DMA_FC_D_P2M));
  1088. break;
  1089. default:
  1090. break;
  1091. }
  1092. lli_write(desc, ctlhi, period_len >> reg_width);
  1093. cdesc->desc[i] = desc;
  1094. if (last)
  1095. lli_write(last, llp, desc->txd.phys | lms);
  1096. last = desc;
  1097. }
  1098. /* Let's make a cyclic list */
  1099. lli_write(last, llp, cdesc->desc[0]->txd.phys | lms);
  1100. dev_dbg(chan2dev(&dwc->chan),
  1101. "cyclic prepared buf %pad len %zu period %zu periods %d\n",
  1102. &buf_addr, buf_len, period_len, periods);
  1103. cdesc->periods = periods;
  1104. dwc->cdesc = cdesc;
  1105. return cdesc;
  1106. out_err_desc_get:
  1107. while (i--)
  1108. dwc_desc_put(dwc, cdesc->desc[i]);
  1109. out_err_alloc:
  1110. kfree(cdesc);
  1111. out_err:
  1112. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1113. return (struct dw_cyclic_desc *)retval;
  1114. }
  1115. EXPORT_SYMBOL(dw_dma_cyclic_prep);
  1116. /**
  1117. * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
  1118. * @chan: the DMA channel to free
  1119. */
  1120. void dw_dma_cyclic_free(struct dma_chan *chan)
  1121. {
  1122. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1123. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  1124. struct dw_cyclic_desc *cdesc = dwc->cdesc;
  1125. unsigned int i;
  1126. unsigned long flags;
  1127. dev_dbg(chan2dev(&dwc->chan), "%s\n", __func__);
  1128. if (!cdesc)
  1129. return;
  1130. spin_lock_irqsave(&dwc->lock, flags);
  1131. dwc_chan_disable(dw, dwc);
  1132. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  1133. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  1134. dma_writel(dw, CLEAR.XFER, dwc->mask);
  1135. spin_unlock_irqrestore(&dwc->lock, flags);
  1136. for (i = 0; i < cdesc->periods; i++)
  1137. dwc_desc_put(dwc, cdesc->desc[i]);
  1138. kfree(cdesc->desc);
  1139. kfree(cdesc);
  1140. dwc->cdesc = NULL;
  1141. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1142. }
  1143. EXPORT_SYMBOL(dw_dma_cyclic_free);
  1144. /*----------------------------------------------------------------------*/
  1145. int dw_dma_probe(struct dw_dma_chip *chip)
  1146. {
  1147. struct dw_dma_platform_data *pdata;
  1148. struct dw_dma *dw;
  1149. bool autocfg = false;
  1150. unsigned int dw_params;
  1151. unsigned int i;
  1152. int err;
  1153. dw = devm_kzalloc(chip->dev, sizeof(*dw), GFP_KERNEL);
  1154. if (!dw)
  1155. return -ENOMEM;
  1156. dw->pdata = devm_kzalloc(chip->dev, sizeof(*dw->pdata), GFP_KERNEL);
  1157. if (!dw->pdata)
  1158. return -ENOMEM;
  1159. dw->regs = chip->regs;
  1160. chip->dw = dw;
  1161. pm_runtime_get_sync(chip->dev);
  1162. if (!chip->pdata) {
  1163. dw_params = dma_readl(dw, DW_PARAMS);
  1164. dev_dbg(chip->dev, "DW_PARAMS: 0x%08x\n", dw_params);
  1165. autocfg = dw_params >> DW_PARAMS_EN & 1;
  1166. if (!autocfg) {
  1167. err = -EINVAL;
  1168. goto err_pdata;
  1169. }
  1170. /* Reassign the platform data pointer */
  1171. pdata = dw->pdata;
  1172. /* Get hardware configuration parameters */
  1173. pdata->nr_channels = (dw_params >> DW_PARAMS_NR_CHAN & 7) + 1;
  1174. pdata->nr_masters = (dw_params >> DW_PARAMS_NR_MASTER & 3) + 1;
  1175. for (i = 0; i < pdata->nr_masters; i++) {
  1176. pdata->data_width[i] =
  1177. 4 << (dw_params >> DW_PARAMS_DATA_WIDTH(i) & 3);
  1178. }
  1179. pdata->block_size = dma_readl(dw, MAX_BLK_SIZE);
  1180. /* Fill platform data with the default values */
  1181. pdata->is_private = true;
  1182. pdata->is_memcpy = true;
  1183. pdata->chan_allocation_order = CHAN_ALLOCATION_ASCENDING;
  1184. pdata->chan_priority = CHAN_PRIORITY_ASCENDING;
  1185. } else if (chip->pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS) {
  1186. err = -EINVAL;
  1187. goto err_pdata;
  1188. } else {
  1189. memcpy(dw->pdata, chip->pdata, sizeof(*dw->pdata));
  1190. /* Reassign the platform data pointer */
  1191. pdata = dw->pdata;
  1192. }
  1193. dw->chan = devm_kcalloc(chip->dev, pdata->nr_channels, sizeof(*dw->chan),
  1194. GFP_KERNEL);
  1195. if (!dw->chan) {
  1196. err = -ENOMEM;
  1197. goto err_pdata;
  1198. }
  1199. /* Calculate all channel mask before DMA setup */
  1200. dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
  1201. /* Force dma off, just in case */
  1202. dw_dma_off(dw);
  1203. /* Create a pool of consistent memory blocks for hardware descriptors */
  1204. dw->desc_pool = dmam_pool_create("dw_dmac_desc_pool", chip->dev,
  1205. sizeof(struct dw_desc), 4, 0);
  1206. if (!dw->desc_pool) {
  1207. dev_err(chip->dev, "No memory for descriptors dma pool\n");
  1208. err = -ENOMEM;
  1209. goto err_pdata;
  1210. }
  1211. tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
  1212. err = request_irq(chip->irq, dw_dma_interrupt, IRQF_SHARED,
  1213. "dw_dmac", dw);
  1214. if (err)
  1215. goto err_pdata;
  1216. INIT_LIST_HEAD(&dw->dma.channels);
  1217. for (i = 0; i < pdata->nr_channels; i++) {
  1218. struct dw_dma_chan *dwc = &dw->chan[i];
  1219. dwc->chan.device = &dw->dma;
  1220. dma_cookie_init(&dwc->chan);
  1221. if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
  1222. list_add_tail(&dwc->chan.device_node,
  1223. &dw->dma.channels);
  1224. else
  1225. list_add(&dwc->chan.device_node, &dw->dma.channels);
  1226. /* 7 is highest priority & 0 is lowest. */
  1227. if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
  1228. dwc->priority = pdata->nr_channels - i - 1;
  1229. else
  1230. dwc->priority = i;
  1231. dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
  1232. spin_lock_init(&dwc->lock);
  1233. dwc->mask = 1 << i;
  1234. INIT_LIST_HEAD(&dwc->active_list);
  1235. INIT_LIST_HEAD(&dwc->queue);
  1236. channel_clear_bit(dw, CH_EN, dwc->mask);
  1237. dwc->direction = DMA_TRANS_NONE;
  1238. /* Hardware configuration */
  1239. if (autocfg) {
  1240. unsigned int r = DW_DMA_MAX_NR_CHANNELS - i - 1;
  1241. void __iomem *addr = &__dw_regs(dw)->DWC_PARAMS[r];
  1242. unsigned int dwc_params = dma_readl_native(addr);
  1243. dev_dbg(chip->dev, "DWC_PARAMS[%d]: 0x%08x\n", i,
  1244. dwc_params);
  1245. /*
  1246. * Decode maximum block size for given channel. The
  1247. * stored 4 bit value represents blocks from 0x00 for 3
  1248. * up to 0x0a for 4095.
  1249. */
  1250. dwc->block_size =
  1251. (4 << ((pdata->block_size >> 4 * i) & 0xf)) - 1;
  1252. dwc->nollp =
  1253. (dwc_params >> DWC_PARAMS_MBLK_EN & 0x1) == 0;
  1254. } else {
  1255. dwc->block_size = pdata->block_size;
  1256. /* Check if channel supports multi block transfer */
  1257. channel_writel(dwc, LLP, DWC_LLP_LOC(0xffffffff));
  1258. dwc->nollp = DWC_LLP_LOC(channel_readl(dwc, LLP)) == 0;
  1259. channel_writel(dwc, LLP, 0);
  1260. }
  1261. }
  1262. /* Clear all interrupts on all channels. */
  1263. dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
  1264. dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
  1265. dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
  1266. dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
  1267. dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
  1268. /* Set capabilities */
  1269. dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
  1270. if (pdata->is_private)
  1271. dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
  1272. if (pdata->is_memcpy)
  1273. dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
  1274. dw->dma.dev = chip->dev;
  1275. dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
  1276. dw->dma.device_free_chan_resources = dwc_free_chan_resources;
  1277. dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
  1278. dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
  1279. dw->dma.device_config = dwc_config;
  1280. dw->dma.device_pause = dwc_pause;
  1281. dw->dma.device_resume = dwc_resume;
  1282. dw->dma.device_terminate_all = dwc_terminate_all;
  1283. dw->dma.device_tx_status = dwc_tx_status;
  1284. dw->dma.device_issue_pending = dwc_issue_pending;
  1285. /* DMA capabilities */
  1286. dw->dma.src_addr_widths = DW_DMA_BUSWIDTHS;
  1287. dw->dma.dst_addr_widths = DW_DMA_BUSWIDTHS;
  1288. dw->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
  1289. BIT(DMA_MEM_TO_MEM);
  1290. dw->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1291. err = dma_async_device_register(&dw->dma);
  1292. if (err)
  1293. goto err_dma_register;
  1294. dev_info(chip->dev, "DesignWare DMA Controller, %d channels\n",
  1295. pdata->nr_channels);
  1296. pm_runtime_put_sync_suspend(chip->dev);
  1297. return 0;
  1298. err_dma_register:
  1299. free_irq(chip->irq, dw);
  1300. err_pdata:
  1301. pm_runtime_put_sync_suspend(chip->dev);
  1302. return err;
  1303. }
  1304. EXPORT_SYMBOL_GPL(dw_dma_probe);
  1305. int dw_dma_remove(struct dw_dma_chip *chip)
  1306. {
  1307. struct dw_dma *dw = chip->dw;
  1308. struct dw_dma_chan *dwc, *_dwc;
  1309. pm_runtime_get_sync(chip->dev);
  1310. dw_dma_off(dw);
  1311. dma_async_device_unregister(&dw->dma);
  1312. free_irq(chip->irq, dw);
  1313. tasklet_kill(&dw->tasklet);
  1314. list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
  1315. chan.device_node) {
  1316. list_del(&dwc->chan.device_node);
  1317. channel_clear_bit(dw, CH_EN, dwc->mask);
  1318. }
  1319. pm_runtime_put_sync_suspend(chip->dev);
  1320. return 0;
  1321. }
  1322. EXPORT_SYMBOL_GPL(dw_dma_remove);
  1323. int dw_dma_disable(struct dw_dma_chip *chip)
  1324. {
  1325. struct dw_dma *dw = chip->dw;
  1326. dw_dma_off(dw);
  1327. return 0;
  1328. }
  1329. EXPORT_SYMBOL_GPL(dw_dma_disable);
  1330. int dw_dma_enable(struct dw_dma_chip *chip)
  1331. {
  1332. struct dw_dma *dw = chip->dw;
  1333. dw_dma_on(dw);
  1334. return 0;
  1335. }
  1336. EXPORT_SYMBOL_GPL(dw_dma_enable);
  1337. MODULE_LICENSE("GPL v2");
  1338. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller core driver");
  1339. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1340. MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");