dw_dmac.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  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. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/clk.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/of.h>
  22. #include <linux/of_dma.h>
  23. #include <linux/mm.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <linux/acpi.h>
  28. #include <linux/acpi_dma.h>
  29. #include "dw_dmac_regs.h"
  30. #include "dmaengine.h"
  31. /*
  32. * This supports the Synopsys "DesignWare AHB Central DMA Controller",
  33. * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all
  34. * of which use ARM any more). See the "Databook" from Synopsys for
  35. * information beyond what licensees probably provide.
  36. *
  37. * The driver has currently been tested only with the Atmel AT32AP7000,
  38. * which does not support descriptor writeback.
  39. */
  40. static inline unsigned int dwc_get_dms(struct dw_dma_slave *slave)
  41. {
  42. return slave ? slave->dst_master : 0;
  43. }
  44. static inline unsigned int dwc_get_sms(struct dw_dma_slave *slave)
  45. {
  46. return slave ? slave->src_master : 1;
  47. }
  48. static inline void dwc_set_masters(struct dw_dma_chan *dwc)
  49. {
  50. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  51. struct dw_dma_slave *dws = dwc->chan.private;
  52. unsigned char mmax = dw->nr_masters - 1;
  53. if (dwc->request_line == ~0) {
  54. dwc->src_master = min_t(unsigned char, mmax, dwc_get_sms(dws));
  55. dwc->dst_master = min_t(unsigned char, mmax, dwc_get_dms(dws));
  56. }
  57. }
  58. #define DWC_DEFAULT_CTLLO(_chan) ({ \
  59. struct dw_dma_chan *_dwc = to_dw_dma_chan(_chan); \
  60. struct dma_slave_config *_sconfig = &_dwc->dma_sconfig; \
  61. bool _is_slave = is_slave_direction(_dwc->direction); \
  62. u8 _smsize = _is_slave ? _sconfig->src_maxburst : \
  63. DW_DMA_MSIZE_16; \
  64. u8 _dmsize = _is_slave ? _sconfig->dst_maxburst : \
  65. DW_DMA_MSIZE_16; \
  66. \
  67. (DWC_CTLL_DST_MSIZE(_dmsize) \
  68. | DWC_CTLL_SRC_MSIZE(_smsize) \
  69. | DWC_CTLL_LLP_D_EN \
  70. | DWC_CTLL_LLP_S_EN \
  71. | DWC_CTLL_DMS(_dwc->dst_master) \
  72. | DWC_CTLL_SMS(_dwc->src_master)); \
  73. })
  74. /*
  75. * Number of descriptors to allocate for each channel. This should be
  76. * made configurable somehow; preferably, the clients (at least the
  77. * ones using slave transfers) should be able to give us a hint.
  78. */
  79. #define NR_DESCS_PER_CHANNEL 64
  80. /*----------------------------------------------------------------------*/
  81. static struct device *chan2dev(struct dma_chan *chan)
  82. {
  83. return &chan->dev->device;
  84. }
  85. static struct device *chan2parent(struct dma_chan *chan)
  86. {
  87. return chan->dev->device.parent;
  88. }
  89. static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
  90. {
  91. return to_dw_desc(dwc->active_list.next);
  92. }
  93. static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
  94. {
  95. struct dw_desc *desc, *_desc;
  96. struct dw_desc *ret = NULL;
  97. unsigned int i = 0;
  98. unsigned long flags;
  99. spin_lock_irqsave(&dwc->lock, flags);
  100. list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
  101. i++;
  102. if (async_tx_test_ack(&desc->txd)) {
  103. list_del(&desc->desc_node);
  104. ret = desc;
  105. break;
  106. }
  107. dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc);
  108. }
  109. spin_unlock_irqrestore(&dwc->lock, flags);
  110. dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i);
  111. return ret;
  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. spin_lock_irqsave(&dwc->lock, flags);
  123. list_for_each_entry(child, &desc->tx_list, desc_node)
  124. dev_vdbg(chan2dev(&dwc->chan),
  125. "moving child desc %p to freelist\n",
  126. child);
  127. list_splice_init(&desc->tx_list, &dwc->free_list);
  128. dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc);
  129. list_add(&desc->desc_node, &dwc->free_list);
  130. spin_unlock_irqrestore(&dwc->lock, flags);
  131. }
  132. }
  133. static void dwc_initialize(struct dw_dma_chan *dwc)
  134. {
  135. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  136. struct dw_dma_slave *dws = dwc->chan.private;
  137. u32 cfghi = DWC_CFGH_FIFO_MODE;
  138. u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority);
  139. if (dwc->initialized == true)
  140. return;
  141. if (dws) {
  142. /*
  143. * We need controller-specific data to set up slave
  144. * transfers.
  145. */
  146. BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev);
  147. cfghi = dws->cfg_hi;
  148. cfglo |= dws->cfg_lo & ~DWC_CFGL_CH_PRIOR_MASK;
  149. } else {
  150. if (dwc->direction == DMA_MEM_TO_DEV)
  151. cfghi = DWC_CFGH_DST_PER(dwc->request_line);
  152. else if (dwc->direction == DMA_DEV_TO_MEM)
  153. cfghi = DWC_CFGH_SRC_PER(dwc->request_line);
  154. }
  155. channel_writel(dwc, CFG_LO, cfglo);
  156. channel_writel(dwc, CFG_HI, cfghi);
  157. /* Enable interrupts */
  158. channel_set_bit(dw, MASK.XFER, dwc->mask);
  159. channel_set_bit(dw, MASK.ERROR, dwc->mask);
  160. dwc->initialized = true;
  161. }
  162. /*----------------------------------------------------------------------*/
  163. static inline unsigned int dwc_fast_fls(unsigned long long v)
  164. {
  165. /*
  166. * We can be a lot more clever here, but this should take care
  167. * of the most common optimization.
  168. */
  169. if (!(v & 7))
  170. return 3;
  171. else if (!(v & 3))
  172. return 2;
  173. else if (!(v & 1))
  174. return 1;
  175. return 0;
  176. }
  177. static inline void dwc_dump_chan_regs(struct dw_dma_chan *dwc)
  178. {
  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. }
  187. static inline void dwc_chan_disable(struct dw_dma *dw, struct dw_dma_chan *dwc)
  188. {
  189. channel_clear_bit(dw, CH_EN, dwc->mask);
  190. while (dma_readl(dw, CH_EN) & dwc->mask)
  191. cpu_relax();
  192. }
  193. /*----------------------------------------------------------------------*/
  194. /* Perform single block transfer */
  195. static inline void dwc_do_single_block(struct dw_dma_chan *dwc,
  196. struct dw_desc *desc)
  197. {
  198. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  199. u32 ctllo;
  200. /* Software emulation of LLP mode relies on interrupts to continue
  201. * multi block transfer. */
  202. ctllo = desc->lli.ctllo | DWC_CTLL_INT_EN;
  203. channel_writel(dwc, SAR, desc->lli.sar);
  204. channel_writel(dwc, DAR, desc->lli.dar);
  205. channel_writel(dwc, CTL_LO, ctllo);
  206. channel_writel(dwc, CTL_HI, desc->lli.ctlhi);
  207. channel_set_bit(dw, CH_EN, dwc->mask);
  208. /* Move pointer to next descriptor */
  209. dwc->tx_node_active = dwc->tx_node_active->next;
  210. }
  211. /* Called with dwc->lock held and bh disabled */
  212. static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
  213. {
  214. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  215. unsigned long was_soft_llp;
  216. /* ASSERT: channel is idle */
  217. if (dma_readl(dw, CH_EN) & dwc->mask) {
  218. dev_err(chan2dev(&dwc->chan),
  219. "BUG: Attempted to start non-idle channel\n");
  220. dwc_dump_chan_regs(dwc);
  221. /* The tasklet will hopefully advance the queue... */
  222. return;
  223. }
  224. if (dwc->nollp) {
  225. was_soft_llp = test_and_set_bit(DW_DMA_IS_SOFT_LLP,
  226. &dwc->flags);
  227. if (was_soft_llp) {
  228. dev_err(chan2dev(&dwc->chan),
  229. "BUG: Attempted to start new LLP transfer "
  230. "inside ongoing one\n");
  231. return;
  232. }
  233. dwc_initialize(dwc);
  234. dwc->residue = first->total_len;
  235. dwc->tx_node_active = &first->tx_list;
  236. /* Submit first block */
  237. dwc_do_single_block(dwc, first);
  238. return;
  239. }
  240. dwc_initialize(dwc);
  241. channel_writel(dwc, LLP, first->txd.phys);
  242. channel_writel(dwc, CTL_LO,
  243. DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  244. channel_writel(dwc, CTL_HI, 0);
  245. channel_set_bit(dw, CH_EN, dwc->mask);
  246. }
  247. /*----------------------------------------------------------------------*/
  248. static void
  249. dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
  250. bool callback_required)
  251. {
  252. dma_async_tx_callback callback = NULL;
  253. void *param = NULL;
  254. struct dma_async_tx_descriptor *txd = &desc->txd;
  255. struct dw_desc *child;
  256. unsigned long flags;
  257. dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
  258. spin_lock_irqsave(&dwc->lock, flags);
  259. dma_cookie_complete(txd);
  260. if (callback_required) {
  261. callback = txd->callback;
  262. param = txd->callback_param;
  263. }
  264. /* async_tx_ack */
  265. list_for_each_entry(child, &desc->tx_list, desc_node)
  266. async_tx_ack(&child->txd);
  267. async_tx_ack(&desc->txd);
  268. list_splice_init(&desc->tx_list, &dwc->free_list);
  269. list_move(&desc->desc_node, &dwc->free_list);
  270. if (!is_slave_direction(dwc->direction)) {
  271. struct device *parent = chan2parent(&dwc->chan);
  272. if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  273. if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
  274. dma_unmap_single(parent, desc->lli.dar,
  275. desc->total_len, DMA_FROM_DEVICE);
  276. else
  277. dma_unmap_page(parent, desc->lli.dar,
  278. desc->total_len, DMA_FROM_DEVICE);
  279. }
  280. if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  281. if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
  282. dma_unmap_single(parent, desc->lli.sar,
  283. desc->total_len, DMA_TO_DEVICE);
  284. else
  285. dma_unmap_page(parent, desc->lli.sar,
  286. desc->total_len, DMA_TO_DEVICE);
  287. }
  288. }
  289. spin_unlock_irqrestore(&dwc->lock, flags);
  290. if (callback)
  291. callback(param);
  292. }
  293. static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
  294. {
  295. struct dw_desc *desc, *_desc;
  296. LIST_HEAD(list);
  297. unsigned long flags;
  298. spin_lock_irqsave(&dwc->lock, flags);
  299. if (dma_readl(dw, CH_EN) & dwc->mask) {
  300. dev_err(chan2dev(&dwc->chan),
  301. "BUG: XFER bit set, but channel not idle!\n");
  302. /* Try to continue after resetting the channel... */
  303. dwc_chan_disable(dw, dwc);
  304. }
  305. /*
  306. * Submit queued descriptors ASAP, i.e. before we go through
  307. * the completed ones.
  308. */
  309. list_splice_init(&dwc->active_list, &list);
  310. if (!list_empty(&dwc->queue)) {
  311. list_move(dwc->queue.next, &dwc->active_list);
  312. dwc_dostart(dwc, dwc_first_active(dwc));
  313. }
  314. spin_unlock_irqrestore(&dwc->lock, flags);
  315. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  316. dwc_descriptor_complete(dwc, desc, true);
  317. }
  318. /* Returns how many bytes were already received from source */
  319. static inline u32 dwc_get_sent(struct dw_dma_chan *dwc)
  320. {
  321. u32 ctlhi = channel_readl(dwc, CTL_HI);
  322. u32 ctllo = channel_readl(dwc, CTL_LO);
  323. return (ctlhi & DWC_CTLH_BLOCK_TS_MASK) * (1 << (ctllo >> 4 & 7));
  324. }
  325. static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
  326. {
  327. dma_addr_t llp;
  328. struct dw_desc *desc, *_desc;
  329. struct dw_desc *child;
  330. u32 status_xfer;
  331. unsigned long flags;
  332. spin_lock_irqsave(&dwc->lock, flags);
  333. llp = channel_readl(dwc, LLP);
  334. status_xfer = dma_readl(dw, RAW.XFER);
  335. if (status_xfer & dwc->mask) {
  336. /* Everything we've submitted is done */
  337. dma_writel(dw, CLEAR.XFER, dwc->mask);
  338. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
  339. struct list_head *head, *active = dwc->tx_node_active;
  340. /*
  341. * We are inside first active descriptor.
  342. * Otherwise something is really wrong.
  343. */
  344. desc = dwc_first_active(dwc);
  345. head = &desc->tx_list;
  346. if (active != head) {
  347. /* Update desc to reflect last sent one */
  348. if (active != head->next)
  349. desc = to_dw_desc(active->prev);
  350. dwc->residue -= desc->len;
  351. child = to_dw_desc(active);
  352. /* Submit next block */
  353. dwc_do_single_block(dwc, child);
  354. spin_unlock_irqrestore(&dwc->lock, flags);
  355. return;
  356. }
  357. /* We are done here */
  358. clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
  359. }
  360. dwc->residue = 0;
  361. spin_unlock_irqrestore(&dwc->lock, flags);
  362. dwc_complete_all(dw, dwc);
  363. return;
  364. }
  365. if (list_empty(&dwc->active_list)) {
  366. dwc->residue = 0;
  367. spin_unlock_irqrestore(&dwc->lock, flags);
  368. return;
  369. }
  370. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
  371. dev_vdbg(chan2dev(&dwc->chan), "%s: soft LLP mode\n", __func__);
  372. spin_unlock_irqrestore(&dwc->lock, flags);
  373. return;
  374. }
  375. dev_vdbg(chan2dev(&dwc->chan), "%s: llp=0x%llx\n", __func__,
  376. (unsigned long long)llp);
  377. list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
  378. /* Initial residue value */
  379. dwc->residue = desc->total_len;
  380. /* Check first descriptors addr */
  381. if (desc->txd.phys == llp) {
  382. spin_unlock_irqrestore(&dwc->lock, flags);
  383. return;
  384. }
  385. /* Check first descriptors llp */
  386. if (desc->lli.llp == llp) {
  387. /* This one is currently in progress */
  388. dwc->residue -= dwc_get_sent(dwc);
  389. spin_unlock_irqrestore(&dwc->lock, flags);
  390. return;
  391. }
  392. dwc->residue -= desc->len;
  393. list_for_each_entry(child, &desc->tx_list, desc_node) {
  394. if (child->lli.llp == llp) {
  395. /* Currently in progress */
  396. dwc->residue -= dwc_get_sent(dwc);
  397. spin_unlock_irqrestore(&dwc->lock, flags);
  398. return;
  399. }
  400. dwc->residue -= child->len;
  401. }
  402. /*
  403. * No descriptors so far seem to be in progress, i.e.
  404. * this one must be done.
  405. */
  406. spin_unlock_irqrestore(&dwc->lock, flags);
  407. dwc_descriptor_complete(dwc, desc, true);
  408. spin_lock_irqsave(&dwc->lock, flags);
  409. }
  410. dev_err(chan2dev(&dwc->chan),
  411. "BUG: All descriptors done, but channel not idle!\n");
  412. /* Try to continue after resetting the channel... */
  413. dwc_chan_disable(dw, dwc);
  414. if (!list_empty(&dwc->queue)) {
  415. list_move(dwc->queue.next, &dwc->active_list);
  416. dwc_dostart(dwc, dwc_first_active(dwc));
  417. }
  418. spin_unlock_irqrestore(&dwc->lock, flags);
  419. }
  420. static inline void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
  421. {
  422. dev_crit(chan2dev(&dwc->chan), " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
  423. lli->sar, lli->dar, lli->llp, lli->ctlhi, lli->ctllo);
  424. }
  425. static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
  426. {
  427. struct dw_desc *bad_desc;
  428. struct dw_desc *child;
  429. unsigned long flags;
  430. dwc_scan_descriptors(dw, dwc);
  431. spin_lock_irqsave(&dwc->lock, flags);
  432. /*
  433. * The descriptor currently at the head of the active list is
  434. * borked. Since we don't have any way to report errors, we'll
  435. * just have to scream loudly and try to carry on.
  436. */
  437. bad_desc = dwc_first_active(dwc);
  438. list_del_init(&bad_desc->desc_node);
  439. list_move(dwc->queue.next, dwc->active_list.prev);
  440. /* Clear the error flag and try to restart the controller */
  441. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  442. if (!list_empty(&dwc->active_list))
  443. dwc_dostart(dwc, dwc_first_active(dwc));
  444. /*
  445. * WARN may seem harsh, but since this only happens
  446. * when someone submits a bad physical address in a
  447. * descriptor, we should consider ourselves lucky that the
  448. * controller flagged an error instead of scribbling over
  449. * random memory locations.
  450. */
  451. dev_WARN(chan2dev(&dwc->chan), "Bad descriptor submitted for DMA!\n"
  452. " cookie: %d\n", bad_desc->txd.cookie);
  453. dwc_dump_lli(dwc, &bad_desc->lli);
  454. list_for_each_entry(child, &bad_desc->tx_list, desc_node)
  455. dwc_dump_lli(dwc, &child->lli);
  456. spin_unlock_irqrestore(&dwc->lock, flags);
  457. /* Pretend the descriptor completed successfully */
  458. dwc_descriptor_complete(dwc, bad_desc, true);
  459. }
  460. /* --------------------- Cyclic DMA API extensions -------------------- */
  461. inline dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
  462. {
  463. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  464. return channel_readl(dwc, SAR);
  465. }
  466. EXPORT_SYMBOL(dw_dma_get_src_addr);
  467. inline dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
  468. {
  469. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  470. return channel_readl(dwc, DAR);
  471. }
  472. EXPORT_SYMBOL(dw_dma_get_dst_addr);
  473. /* Called with dwc->lock held and all DMAC interrupts disabled */
  474. static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
  475. u32 status_err, u32 status_xfer)
  476. {
  477. unsigned long flags;
  478. if (dwc->mask) {
  479. void (*callback)(void *param);
  480. void *callback_param;
  481. dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
  482. channel_readl(dwc, LLP));
  483. callback = dwc->cdesc->period_callback;
  484. callback_param = dwc->cdesc->period_callback_param;
  485. if (callback)
  486. callback(callback_param);
  487. }
  488. /*
  489. * Error and transfer complete are highly unlikely, and will most
  490. * likely be due to a configuration error by the user.
  491. */
  492. if (unlikely(status_err & dwc->mask) ||
  493. unlikely(status_xfer & dwc->mask)) {
  494. int i;
  495. dev_err(chan2dev(&dwc->chan), "cyclic DMA unexpected %s "
  496. "interrupt, stopping DMA transfer\n",
  497. status_xfer ? "xfer" : "error");
  498. spin_lock_irqsave(&dwc->lock, flags);
  499. dwc_dump_chan_regs(dwc);
  500. dwc_chan_disable(dw, dwc);
  501. /* Make sure DMA does not restart by loading a new list */
  502. channel_writel(dwc, LLP, 0);
  503. channel_writel(dwc, CTL_LO, 0);
  504. channel_writel(dwc, CTL_HI, 0);
  505. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  506. dma_writel(dw, CLEAR.XFER, dwc->mask);
  507. for (i = 0; i < dwc->cdesc->periods; i++)
  508. dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
  509. spin_unlock_irqrestore(&dwc->lock, flags);
  510. }
  511. }
  512. /* ------------------------------------------------------------------------- */
  513. static void dw_dma_tasklet(unsigned long data)
  514. {
  515. struct dw_dma *dw = (struct dw_dma *)data;
  516. struct dw_dma_chan *dwc;
  517. u32 status_xfer;
  518. u32 status_err;
  519. int i;
  520. status_xfer = dma_readl(dw, RAW.XFER);
  521. status_err = dma_readl(dw, RAW.ERROR);
  522. dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err);
  523. for (i = 0; i < dw->dma.chancnt; i++) {
  524. dwc = &dw->chan[i];
  525. if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
  526. dwc_handle_cyclic(dw, dwc, status_err, status_xfer);
  527. else if (status_err & (1 << i))
  528. dwc_handle_error(dw, dwc);
  529. else if (status_xfer & (1 << i))
  530. dwc_scan_descriptors(dw, dwc);
  531. }
  532. /*
  533. * Re-enable interrupts.
  534. */
  535. channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
  536. channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
  537. }
  538. static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
  539. {
  540. struct dw_dma *dw = dev_id;
  541. u32 status;
  542. dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__,
  543. dma_readl(dw, STATUS_INT));
  544. /*
  545. * Just disable the interrupts. We'll turn them back on in the
  546. * softirq handler.
  547. */
  548. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  549. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  550. status = dma_readl(dw, STATUS_INT);
  551. if (status) {
  552. dev_err(dw->dma.dev,
  553. "BUG: Unexpected interrupts pending: 0x%x\n",
  554. status);
  555. /* Try to recover */
  556. channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
  557. channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
  558. channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
  559. channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
  560. }
  561. tasklet_schedule(&dw->tasklet);
  562. return IRQ_HANDLED;
  563. }
  564. /*----------------------------------------------------------------------*/
  565. static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
  566. {
  567. struct dw_desc *desc = txd_to_dw_desc(tx);
  568. struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
  569. dma_cookie_t cookie;
  570. unsigned long flags;
  571. spin_lock_irqsave(&dwc->lock, flags);
  572. cookie = dma_cookie_assign(tx);
  573. /*
  574. * REVISIT: We should attempt to chain as many descriptors as
  575. * possible, perhaps even appending to those already submitted
  576. * for DMA. But this is hard to do in a race-free manner.
  577. */
  578. if (list_empty(&dwc->active_list)) {
  579. dev_vdbg(chan2dev(tx->chan), "%s: started %u\n", __func__,
  580. desc->txd.cookie);
  581. list_add_tail(&desc->desc_node, &dwc->active_list);
  582. dwc_dostart(dwc, dwc_first_active(dwc));
  583. } else {
  584. dev_vdbg(chan2dev(tx->chan), "%s: queued %u\n", __func__,
  585. desc->txd.cookie);
  586. list_add_tail(&desc->desc_node, &dwc->queue);
  587. }
  588. spin_unlock_irqrestore(&dwc->lock, flags);
  589. return cookie;
  590. }
  591. static struct dma_async_tx_descriptor *
  592. dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  593. size_t len, unsigned long flags)
  594. {
  595. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  596. struct dw_dma *dw = to_dw_dma(chan->device);
  597. struct dw_desc *desc;
  598. struct dw_desc *first;
  599. struct dw_desc *prev;
  600. size_t xfer_count;
  601. size_t offset;
  602. unsigned int src_width;
  603. unsigned int dst_width;
  604. unsigned int data_width;
  605. u32 ctllo;
  606. dev_vdbg(chan2dev(chan),
  607. "%s: d0x%llx s0x%llx l0x%zx f0x%lx\n", __func__,
  608. (unsigned long long)dest, (unsigned long long)src,
  609. len, flags);
  610. if (unlikely(!len)) {
  611. dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
  612. return NULL;
  613. }
  614. dwc->direction = DMA_MEM_TO_MEM;
  615. data_width = min_t(unsigned int, dw->data_width[dwc->src_master],
  616. dw->data_width[dwc->dst_master]);
  617. src_width = dst_width = min_t(unsigned int, data_width,
  618. dwc_fast_fls(src | dest | len));
  619. ctllo = DWC_DEFAULT_CTLLO(chan)
  620. | DWC_CTLL_DST_WIDTH(dst_width)
  621. | DWC_CTLL_SRC_WIDTH(src_width)
  622. | DWC_CTLL_DST_INC
  623. | DWC_CTLL_SRC_INC
  624. | DWC_CTLL_FC_M2M;
  625. prev = first = NULL;
  626. for (offset = 0; offset < len; offset += xfer_count << src_width) {
  627. xfer_count = min_t(size_t, (len - offset) >> src_width,
  628. dwc->block_size);
  629. desc = dwc_desc_get(dwc);
  630. if (!desc)
  631. goto err_desc_get;
  632. desc->lli.sar = src + offset;
  633. desc->lli.dar = dest + offset;
  634. desc->lli.ctllo = ctllo;
  635. desc->lli.ctlhi = xfer_count;
  636. desc->len = xfer_count << src_width;
  637. if (!first) {
  638. first = desc;
  639. } else {
  640. prev->lli.llp = desc->txd.phys;
  641. list_add_tail(&desc->desc_node,
  642. &first->tx_list);
  643. }
  644. prev = desc;
  645. }
  646. if (flags & DMA_PREP_INTERRUPT)
  647. /* Trigger interrupt after last block */
  648. prev->lli.ctllo |= DWC_CTLL_INT_EN;
  649. prev->lli.llp = 0;
  650. first->txd.flags = flags;
  651. first->total_len = len;
  652. return &first->txd;
  653. err_desc_get:
  654. dwc_desc_put(dwc, first);
  655. return NULL;
  656. }
  657. static struct dma_async_tx_descriptor *
  658. dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  659. unsigned int sg_len, enum dma_transfer_direction direction,
  660. unsigned long flags, void *context)
  661. {
  662. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  663. struct dw_dma *dw = to_dw_dma(chan->device);
  664. struct dma_slave_config *sconfig = &dwc->dma_sconfig;
  665. struct dw_desc *prev;
  666. struct dw_desc *first;
  667. u32 ctllo;
  668. dma_addr_t reg;
  669. unsigned int reg_width;
  670. unsigned int mem_width;
  671. unsigned int data_width;
  672. unsigned int i;
  673. struct scatterlist *sg;
  674. size_t total_len = 0;
  675. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  676. if (unlikely(!is_slave_direction(direction) || !sg_len))
  677. return NULL;
  678. dwc->direction = direction;
  679. prev = first = NULL;
  680. switch (direction) {
  681. case DMA_MEM_TO_DEV:
  682. reg_width = __fls(sconfig->dst_addr_width);
  683. reg = sconfig->dst_addr;
  684. ctllo = (DWC_DEFAULT_CTLLO(chan)
  685. | DWC_CTLL_DST_WIDTH(reg_width)
  686. | DWC_CTLL_DST_FIX
  687. | DWC_CTLL_SRC_INC);
  688. ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
  689. DWC_CTLL_FC(DW_DMA_FC_D_M2P);
  690. data_width = dw->data_width[dwc->src_master];
  691. for_each_sg(sgl, sg, sg_len, i) {
  692. struct dw_desc *desc;
  693. u32 len, dlen, mem;
  694. mem = sg_dma_address(sg);
  695. len = sg_dma_len(sg);
  696. mem_width = min_t(unsigned int,
  697. data_width, dwc_fast_fls(mem | len));
  698. slave_sg_todev_fill_desc:
  699. desc = dwc_desc_get(dwc);
  700. if (!desc) {
  701. dev_err(chan2dev(chan),
  702. "not enough descriptors available\n");
  703. goto err_desc_get;
  704. }
  705. desc->lli.sar = mem;
  706. desc->lli.dar = reg;
  707. desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
  708. if ((len >> mem_width) > dwc->block_size) {
  709. dlen = dwc->block_size << mem_width;
  710. mem += dlen;
  711. len -= dlen;
  712. } else {
  713. dlen = len;
  714. len = 0;
  715. }
  716. desc->lli.ctlhi = dlen >> mem_width;
  717. desc->len = dlen;
  718. if (!first) {
  719. first = desc;
  720. } else {
  721. prev->lli.llp = desc->txd.phys;
  722. list_add_tail(&desc->desc_node,
  723. &first->tx_list);
  724. }
  725. prev = desc;
  726. total_len += dlen;
  727. if (len)
  728. goto slave_sg_todev_fill_desc;
  729. }
  730. break;
  731. case DMA_DEV_TO_MEM:
  732. reg_width = __fls(sconfig->src_addr_width);
  733. reg = sconfig->src_addr;
  734. ctllo = (DWC_DEFAULT_CTLLO(chan)
  735. | DWC_CTLL_SRC_WIDTH(reg_width)
  736. | DWC_CTLL_DST_INC
  737. | DWC_CTLL_SRC_FIX);
  738. ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
  739. DWC_CTLL_FC(DW_DMA_FC_D_P2M);
  740. data_width = dw->data_width[dwc->dst_master];
  741. for_each_sg(sgl, sg, sg_len, i) {
  742. struct dw_desc *desc;
  743. u32 len, dlen, mem;
  744. mem = sg_dma_address(sg);
  745. len = sg_dma_len(sg);
  746. mem_width = min_t(unsigned int,
  747. data_width, dwc_fast_fls(mem | len));
  748. slave_sg_fromdev_fill_desc:
  749. desc = dwc_desc_get(dwc);
  750. if (!desc) {
  751. dev_err(chan2dev(chan),
  752. "not enough descriptors available\n");
  753. goto err_desc_get;
  754. }
  755. desc->lli.sar = reg;
  756. desc->lli.dar = mem;
  757. desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
  758. if ((len >> reg_width) > dwc->block_size) {
  759. dlen = dwc->block_size << reg_width;
  760. mem += dlen;
  761. len -= dlen;
  762. } else {
  763. dlen = len;
  764. len = 0;
  765. }
  766. desc->lli.ctlhi = dlen >> reg_width;
  767. desc->len = dlen;
  768. if (!first) {
  769. first = desc;
  770. } else {
  771. prev->lli.llp = desc->txd.phys;
  772. list_add_tail(&desc->desc_node,
  773. &first->tx_list);
  774. }
  775. prev = desc;
  776. total_len += dlen;
  777. if (len)
  778. goto slave_sg_fromdev_fill_desc;
  779. }
  780. break;
  781. default:
  782. return NULL;
  783. }
  784. if (flags & DMA_PREP_INTERRUPT)
  785. /* Trigger interrupt after last block */
  786. prev->lli.ctllo |= DWC_CTLL_INT_EN;
  787. prev->lli.llp = 0;
  788. first->total_len = total_len;
  789. return &first->txd;
  790. err_desc_get:
  791. dwc_desc_put(dwc, first);
  792. return NULL;
  793. }
  794. /*
  795. * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
  796. * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
  797. *
  798. * NOTE: burst size 2 is not supported by controller.
  799. *
  800. * This can be done by finding least significant bit set: n & (n - 1)
  801. */
  802. static inline void convert_burst(u32 *maxburst)
  803. {
  804. if (*maxburst > 1)
  805. *maxburst = fls(*maxburst) - 2;
  806. else
  807. *maxburst = 0;
  808. }
  809. static int
  810. set_runtime_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
  811. {
  812. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  813. /* Check if chan will be configured for slave transfers */
  814. if (!is_slave_direction(sconfig->direction))
  815. return -EINVAL;
  816. memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
  817. dwc->direction = sconfig->direction;
  818. /* Take the request line from slave_id member */
  819. if (dwc->request_line == ~0)
  820. dwc->request_line = sconfig->slave_id;
  821. convert_burst(&dwc->dma_sconfig.src_maxburst);
  822. convert_burst(&dwc->dma_sconfig.dst_maxburst);
  823. return 0;
  824. }
  825. static inline void dwc_chan_pause(struct dw_dma_chan *dwc)
  826. {
  827. u32 cfglo = channel_readl(dwc, CFG_LO);
  828. unsigned int count = 20; /* timeout iterations */
  829. channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
  830. while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY) && count--)
  831. udelay(2);
  832. dwc->paused = true;
  833. }
  834. static inline void dwc_chan_resume(struct dw_dma_chan *dwc)
  835. {
  836. u32 cfglo = channel_readl(dwc, CFG_LO);
  837. channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
  838. dwc->paused = false;
  839. }
  840. static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  841. unsigned long arg)
  842. {
  843. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  844. struct dw_dma *dw = to_dw_dma(chan->device);
  845. struct dw_desc *desc, *_desc;
  846. unsigned long flags;
  847. LIST_HEAD(list);
  848. if (cmd == DMA_PAUSE) {
  849. spin_lock_irqsave(&dwc->lock, flags);
  850. dwc_chan_pause(dwc);
  851. spin_unlock_irqrestore(&dwc->lock, flags);
  852. } else if (cmd == DMA_RESUME) {
  853. if (!dwc->paused)
  854. return 0;
  855. spin_lock_irqsave(&dwc->lock, flags);
  856. dwc_chan_resume(dwc);
  857. spin_unlock_irqrestore(&dwc->lock, flags);
  858. } else if (cmd == DMA_TERMINATE_ALL) {
  859. spin_lock_irqsave(&dwc->lock, flags);
  860. clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
  861. dwc_chan_disable(dw, dwc);
  862. dwc_chan_resume(dwc);
  863. /* active_list entries will end up before queued entries */
  864. list_splice_init(&dwc->queue, &list);
  865. list_splice_init(&dwc->active_list, &list);
  866. spin_unlock_irqrestore(&dwc->lock, flags);
  867. /* Flush all pending and queued descriptors */
  868. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  869. dwc_descriptor_complete(dwc, desc, false);
  870. } else if (cmd == DMA_SLAVE_CONFIG) {
  871. return set_runtime_config(chan, (struct dma_slave_config *)arg);
  872. } else {
  873. return -ENXIO;
  874. }
  875. return 0;
  876. }
  877. static inline u32 dwc_get_residue(struct dw_dma_chan *dwc)
  878. {
  879. unsigned long flags;
  880. u32 residue;
  881. spin_lock_irqsave(&dwc->lock, flags);
  882. residue = dwc->residue;
  883. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags) && residue)
  884. residue -= dwc_get_sent(dwc);
  885. spin_unlock_irqrestore(&dwc->lock, flags);
  886. return residue;
  887. }
  888. static enum dma_status
  889. dwc_tx_status(struct dma_chan *chan,
  890. dma_cookie_t cookie,
  891. struct dma_tx_state *txstate)
  892. {
  893. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  894. enum dma_status ret;
  895. ret = dma_cookie_status(chan, cookie, txstate);
  896. if (ret != DMA_SUCCESS) {
  897. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  898. ret = dma_cookie_status(chan, cookie, txstate);
  899. }
  900. if (ret != DMA_SUCCESS)
  901. dma_set_residue(txstate, dwc_get_residue(dwc));
  902. if (dwc->paused)
  903. return DMA_PAUSED;
  904. return ret;
  905. }
  906. static void dwc_issue_pending(struct dma_chan *chan)
  907. {
  908. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  909. if (!list_empty(&dwc->queue))
  910. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  911. }
  912. static int dwc_alloc_chan_resources(struct dma_chan *chan)
  913. {
  914. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  915. struct dw_dma *dw = to_dw_dma(chan->device);
  916. struct dw_desc *desc;
  917. int i;
  918. unsigned long flags;
  919. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  920. /* ASSERT: channel is idle */
  921. if (dma_readl(dw, CH_EN) & dwc->mask) {
  922. dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
  923. return -EIO;
  924. }
  925. dma_cookie_init(chan);
  926. /*
  927. * NOTE: some controllers may have additional features that we
  928. * need to initialize here, like "scatter-gather" (which
  929. * doesn't mean what you think it means), and status writeback.
  930. */
  931. dwc_set_masters(dwc);
  932. spin_lock_irqsave(&dwc->lock, flags);
  933. i = dwc->descs_allocated;
  934. while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
  935. dma_addr_t phys;
  936. spin_unlock_irqrestore(&dwc->lock, flags);
  937. desc = dma_pool_alloc(dw->desc_pool, GFP_ATOMIC, &phys);
  938. if (!desc)
  939. goto err_desc_alloc;
  940. memset(desc, 0, sizeof(struct dw_desc));
  941. INIT_LIST_HEAD(&desc->tx_list);
  942. dma_async_tx_descriptor_init(&desc->txd, chan);
  943. desc->txd.tx_submit = dwc_tx_submit;
  944. desc->txd.flags = DMA_CTRL_ACK;
  945. desc->txd.phys = phys;
  946. dwc_desc_put(dwc, desc);
  947. spin_lock_irqsave(&dwc->lock, flags);
  948. i = ++dwc->descs_allocated;
  949. }
  950. spin_unlock_irqrestore(&dwc->lock, flags);
  951. dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
  952. return i;
  953. err_desc_alloc:
  954. dev_info(chan2dev(chan), "only allocated %d descriptors\n", i);
  955. return i;
  956. }
  957. static void dwc_free_chan_resources(struct dma_chan *chan)
  958. {
  959. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  960. struct dw_dma *dw = to_dw_dma(chan->device);
  961. struct dw_desc *desc, *_desc;
  962. unsigned long flags;
  963. LIST_HEAD(list);
  964. dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__,
  965. dwc->descs_allocated);
  966. /* ASSERT: channel is idle */
  967. BUG_ON(!list_empty(&dwc->active_list));
  968. BUG_ON(!list_empty(&dwc->queue));
  969. BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
  970. spin_lock_irqsave(&dwc->lock, flags);
  971. list_splice_init(&dwc->free_list, &list);
  972. dwc->descs_allocated = 0;
  973. dwc->initialized = false;
  974. dwc->request_line = ~0;
  975. /* Disable interrupts */
  976. channel_clear_bit(dw, MASK.XFER, dwc->mask);
  977. channel_clear_bit(dw, MASK.ERROR, dwc->mask);
  978. spin_unlock_irqrestore(&dwc->lock, flags);
  979. list_for_each_entry_safe(desc, _desc, &list, desc_node) {
  980. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  981. dma_pool_free(dw->desc_pool, desc, desc->txd.phys);
  982. }
  983. dev_vdbg(chan2dev(chan), "%s: done\n", __func__);
  984. }
  985. /*----------------------------------------------------------------------*/
  986. struct dw_dma_of_filter_args {
  987. struct dw_dma *dw;
  988. unsigned int req;
  989. unsigned int src;
  990. unsigned int dst;
  991. };
  992. static bool dw_dma_of_filter(struct dma_chan *chan, void *param)
  993. {
  994. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  995. struct dw_dma_of_filter_args *fargs = param;
  996. /* Ensure the device matches our channel */
  997. if (chan->device != &fargs->dw->dma)
  998. return false;
  999. dwc->request_line = fargs->req;
  1000. dwc->src_master = fargs->src;
  1001. dwc->dst_master = fargs->dst;
  1002. return true;
  1003. }
  1004. static struct dma_chan *dw_dma_of_xlate(struct of_phandle_args *dma_spec,
  1005. struct of_dma *ofdma)
  1006. {
  1007. struct dw_dma *dw = ofdma->of_dma_data;
  1008. struct dw_dma_of_filter_args fargs = {
  1009. .dw = dw,
  1010. };
  1011. dma_cap_mask_t cap;
  1012. if (dma_spec->args_count != 3)
  1013. return NULL;
  1014. fargs.req = dma_spec->args[0];
  1015. fargs.src = dma_spec->args[1];
  1016. fargs.dst = dma_spec->args[2];
  1017. if (WARN_ON(fargs.req >= DW_DMA_MAX_NR_REQUESTS ||
  1018. fargs.src >= dw->nr_masters ||
  1019. fargs.dst >= dw->nr_masters))
  1020. return NULL;
  1021. dma_cap_zero(cap);
  1022. dma_cap_set(DMA_SLAVE, cap);
  1023. /* TODO: there should be a simpler way to do this */
  1024. return dma_request_channel(cap, dw_dma_of_filter, &fargs);
  1025. }
  1026. #ifdef CONFIG_ACPI
  1027. static bool dw_dma_acpi_filter(struct dma_chan *chan, void *param)
  1028. {
  1029. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1030. struct acpi_dma_spec *dma_spec = param;
  1031. if (chan->device->dev != dma_spec->dev ||
  1032. chan->chan_id != dma_spec->chan_id)
  1033. return false;
  1034. dwc->request_line = dma_spec->slave_id;
  1035. dwc->src_master = dwc_get_sms(NULL);
  1036. dwc->dst_master = dwc_get_dms(NULL);
  1037. return true;
  1038. }
  1039. static void dw_dma_acpi_controller_register(struct dw_dma *dw)
  1040. {
  1041. struct device *dev = dw->dma.dev;
  1042. struct acpi_dma_filter_info *info;
  1043. int ret;
  1044. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  1045. if (!info)
  1046. return;
  1047. dma_cap_zero(info->dma_cap);
  1048. dma_cap_set(DMA_SLAVE, info->dma_cap);
  1049. info->filter_fn = dw_dma_acpi_filter;
  1050. ret = devm_acpi_dma_controller_register(dev, acpi_dma_simple_xlate,
  1051. info);
  1052. if (ret)
  1053. dev_err(dev, "could not register acpi_dma_controller\n");
  1054. }
  1055. #else /* !CONFIG_ACPI */
  1056. static inline void dw_dma_acpi_controller_register(struct dw_dma *dw) {}
  1057. #endif /* !CONFIG_ACPI */
  1058. /* --------------------- Cyclic DMA API extensions -------------------- */
  1059. /**
  1060. * dw_dma_cyclic_start - start the cyclic DMA transfer
  1061. * @chan: the DMA channel to start
  1062. *
  1063. * Must be called with soft interrupts disabled. Returns zero on success or
  1064. * -errno on failure.
  1065. */
  1066. int dw_dma_cyclic_start(struct dma_chan *chan)
  1067. {
  1068. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1069. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  1070. unsigned long flags;
  1071. if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
  1072. dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
  1073. return -ENODEV;
  1074. }
  1075. spin_lock_irqsave(&dwc->lock, flags);
  1076. /* Assert channel is idle */
  1077. if (dma_readl(dw, CH_EN) & dwc->mask) {
  1078. dev_err(chan2dev(&dwc->chan),
  1079. "BUG: Attempted to start non-idle channel\n");
  1080. dwc_dump_chan_regs(dwc);
  1081. spin_unlock_irqrestore(&dwc->lock, flags);
  1082. return -EBUSY;
  1083. }
  1084. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  1085. dma_writel(dw, CLEAR.XFER, dwc->mask);
  1086. /* Setup DMAC channel registers */
  1087. channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
  1088. channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  1089. channel_writel(dwc, CTL_HI, 0);
  1090. channel_set_bit(dw, CH_EN, dwc->mask);
  1091. spin_unlock_irqrestore(&dwc->lock, flags);
  1092. return 0;
  1093. }
  1094. EXPORT_SYMBOL(dw_dma_cyclic_start);
  1095. /**
  1096. * dw_dma_cyclic_stop - stop the cyclic DMA transfer
  1097. * @chan: the DMA channel to stop
  1098. *
  1099. * Must be called with soft interrupts disabled.
  1100. */
  1101. void dw_dma_cyclic_stop(struct dma_chan *chan)
  1102. {
  1103. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1104. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  1105. unsigned long flags;
  1106. spin_lock_irqsave(&dwc->lock, flags);
  1107. dwc_chan_disable(dw, dwc);
  1108. spin_unlock_irqrestore(&dwc->lock, flags);
  1109. }
  1110. EXPORT_SYMBOL(dw_dma_cyclic_stop);
  1111. /**
  1112. * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
  1113. * @chan: the DMA channel to prepare
  1114. * @buf_addr: physical DMA address where the buffer starts
  1115. * @buf_len: total number of bytes for the entire buffer
  1116. * @period_len: number of bytes for each period
  1117. * @direction: transfer direction, to or from device
  1118. *
  1119. * Must be called before trying to start the transfer. Returns a valid struct
  1120. * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
  1121. */
  1122. struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
  1123. dma_addr_t buf_addr, size_t buf_len, size_t period_len,
  1124. enum dma_transfer_direction direction)
  1125. {
  1126. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1127. struct dma_slave_config *sconfig = &dwc->dma_sconfig;
  1128. struct dw_cyclic_desc *cdesc;
  1129. struct dw_cyclic_desc *retval = NULL;
  1130. struct dw_desc *desc;
  1131. struct dw_desc *last = NULL;
  1132. unsigned long was_cyclic;
  1133. unsigned int reg_width;
  1134. unsigned int periods;
  1135. unsigned int i;
  1136. unsigned long flags;
  1137. spin_lock_irqsave(&dwc->lock, flags);
  1138. if (dwc->nollp) {
  1139. spin_unlock_irqrestore(&dwc->lock, flags);
  1140. dev_dbg(chan2dev(&dwc->chan),
  1141. "channel doesn't support LLP transfers\n");
  1142. return ERR_PTR(-EINVAL);
  1143. }
  1144. if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
  1145. spin_unlock_irqrestore(&dwc->lock, flags);
  1146. dev_dbg(chan2dev(&dwc->chan),
  1147. "queue and/or active list are not empty\n");
  1148. return ERR_PTR(-EBUSY);
  1149. }
  1150. was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1151. spin_unlock_irqrestore(&dwc->lock, flags);
  1152. if (was_cyclic) {
  1153. dev_dbg(chan2dev(&dwc->chan),
  1154. "channel already prepared for cyclic DMA\n");
  1155. return ERR_PTR(-EBUSY);
  1156. }
  1157. retval = ERR_PTR(-EINVAL);
  1158. if (unlikely(!is_slave_direction(direction)))
  1159. goto out_err;
  1160. dwc->direction = direction;
  1161. if (direction == DMA_MEM_TO_DEV)
  1162. reg_width = __ffs(sconfig->dst_addr_width);
  1163. else
  1164. reg_width = __ffs(sconfig->src_addr_width);
  1165. periods = buf_len / period_len;
  1166. /* Check for too big/unaligned periods and unaligned DMA buffer. */
  1167. if (period_len > (dwc->block_size << reg_width))
  1168. goto out_err;
  1169. if (unlikely(period_len & ((1 << reg_width) - 1)))
  1170. goto out_err;
  1171. if (unlikely(buf_addr & ((1 << reg_width) - 1)))
  1172. goto out_err;
  1173. retval = ERR_PTR(-ENOMEM);
  1174. if (periods > NR_DESCS_PER_CHANNEL)
  1175. goto out_err;
  1176. cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
  1177. if (!cdesc)
  1178. goto out_err;
  1179. cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
  1180. if (!cdesc->desc)
  1181. goto out_err_alloc;
  1182. for (i = 0; i < periods; i++) {
  1183. desc = dwc_desc_get(dwc);
  1184. if (!desc)
  1185. goto out_err_desc_get;
  1186. switch (direction) {
  1187. case DMA_MEM_TO_DEV:
  1188. desc->lli.dar = sconfig->dst_addr;
  1189. desc->lli.sar = buf_addr + (period_len * i);
  1190. desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
  1191. | DWC_CTLL_DST_WIDTH(reg_width)
  1192. | DWC_CTLL_SRC_WIDTH(reg_width)
  1193. | DWC_CTLL_DST_FIX
  1194. | DWC_CTLL_SRC_INC
  1195. | DWC_CTLL_INT_EN);
  1196. desc->lli.ctllo |= sconfig->device_fc ?
  1197. DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
  1198. DWC_CTLL_FC(DW_DMA_FC_D_M2P);
  1199. break;
  1200. case DMA_DEV_TO_MEM:
  1201. desc->lli.dar = buf_addr + (period_len * i);
  1202. desc->lli.sar = sconfig->src_addr;
  1203. desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
  1204. | DWC_CTLL_SRC_WIDTH(reg_width)
  1205. | DWC_CTLL_DST_WIDTH(reg_width)
  1206. | DWC_CTLL_DST_INC
  1207. | DWC_CTLL_SRC_FIX
  1208. | DWC_CTLL_INT_EN);
  1209. desc->lli.ctllo |= sconfig->device_fc ?
  1210. DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
  1211. DWC_CTLL_FC(DW_DMA_FC_D_P2M);
  1212. break;
  1213. default:
  1214. break;
  1215. }
  1216. desc->lli.ctlhi = (period_len >> reg_width);
  1217. cdesc->desc[i] = desc;
  1218. if (last)
  1219. last->lli.llp = desc->txd.phys;
  1220. last = desc;
  1221. }
  1222. /* Let's make a cyclic list */
  1223. last->lli.llp = cdesc->desc[0]->txd.phys;
  1224. dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%llx len %zu "
  1225. "period %zu periods %d\n", (unsigned long long)buf_addr,
  1226. buf_len, period_len, periods);
  1227. cdesc->periods = periods;
  1228. dwc->cdesc = cdesc;
  1229. return cdesc;
  1230. out_err_desc_get:
  1231. while (i--)
  1232. dwc_desc_put(dwc, cdesc->desc[i]);
  1233. out_err_alloc:
  1234. kfree(cdesc);
  1235. out_err:
  1236. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1237. return (struct dw_cyclic_desc *)retval;
  1238. }
  1239. EXPORT_SYMBOL(dw_dma_cyclic_prep);
  1240. /**
  1241. * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
  1242. * @chan: the DMA channel to free
  1243. */
  1244. void dw_dma_cyclic_free(struct dma_chan *chan)
  1245. {
  1246. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1247. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  1248. struct dw_cyclic_desc *cdesc = dwc->cdesc;
  1249. int i;
  1250. unsigned long flags;
  1251. dev_dbg(chan2dev(&dwc->chan), "%s\n", __func__);
  1252. if (!cdesc)
  1253. return;
  1254. spin_lock_irqsave(&dwc->lock, flags);
  1255. dwc_chan_disable(dw, dwc);
  1256. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  1257. dma_writel(dw, CLEAR.XFER, dwc->mask);
  1258. spin_unlock_irqrestore(&dwc->lock, flags);
  1259. for (i = 0; i < cdesc->periods; i++)
  1260. dwc_desc_put(dwc, cdesc->desc[i]);
  1261. kfree(cdesc->desc);
  1262. kfree(cdesc);
  1263. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1264. }
  1265. EXPORT_SYMBOL(dw_dma_cyclic_free);
  1266. /*----------------------------------------------------------------------*/
  1267. static void dw_dma_off(struct dw_dma *dw)
  1268. {
  1269. int i;
  1270. dma_writel(dw, CFG, 0);
  1271. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  1272. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  1273. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  1274. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  1275. while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
  1276. cpu_relax();
  1277. for (i = 0; i < dw->dma.chancnt; i++)
  1278. dw->chan[i].initialized = false;
  1279. }
  1280. #ifdef CONFIG_OF
  1281. static struct dw_dma_platform_data *
  1282. dw_dma_parse_dt(struct platform_device *pdev)
  1283. {
  1284. struct device_node *np = pdev->dev.of_node;
  1285. struct dw_dma_platform_data *pdata;
  1286. u32 tmp, arr[4];
  1287. if (!np) {
  1288. dev_err(&pdev->dev, "Missing DT data\n");
  1289. return NULL;
  1290. }
  1291. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1292. if (!pdata)
  1293. return NULL;
  1294. if (of_property_read_u32(np, "dma-channels", &pdata->nr_channels))
  1295. return NULL;
  1296. if (of_property_read_bool(np, "is_private"))
  1297. pdata->is_private = true;
  1298. if (!of_property_read_u32(np, "chan_allocation_order", &tmp))
  1299. pdata->chan_allocation_order = (unsigned char)tmp;
  1300. if (!of_property_read_u32(np, "chan_priority", &tmp))
  1301. pdata->chan_priority = tmp;
  1302. if (!of_property_read_u32(np, "block_size", &tmp))
  1303. pdata->block_size = tmp;
  1304. if (!of_property_read_u32(np, "dma-masters", &tmp)) {
  1305. if (tmp > 4)
  1306. return NULL;
  1307. pdata->nr_masters = tmp;
  1308. }
  1309. if (!of_property_read_u32_array(np, "data_width", arr,
  1310. pdata->nr_masters))
  1311. for (tmp = 0; tmp < pdata->nr_masters; tmp++)
  1312. pdata->data_width[tmp] = arr[tmp];
  1313. return pdata;
  1314. }
  1315. #else
  1316. static inline struct dw_dma_platform_data *
  1317. dw_dma_parse_dt(struct platform_device *pdev)
  1318. {
  1319. return NULL;
  1320. }
  1321. #endif
  1322. static int dw_probe(struct platform_device *pdev)
  1323. {
  1324. struct dw_dma_platform_data *pdata;
  1325. struct resource *io;
  1326. struct dw_dma *dw;
  1327. size_t size;
  1328. void __iomem *regs;
  1329. bool autocfg;
  1330. unsigned int dw_params;
  1331. unsigned int nr_channels;
  1332. unsigned int max_blk_size = 0;
  1333. int irq;
  1334. int err;
  1335. int i;
  1336. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1337. if (!io)
  1338. return -EINVAL;
  1339. irq = platform_get_irq(pdev, 0);
  1340. if (irq < 0)
  1341. return irq;
  1342. regs = devm_ioremap_resource(&pdev->dev, io);
  1343. if (IS_ERR(regs))
  1344. return PTR_ERR(regs);
  1345. /* Apply default dma_mask if needed */
  1346. if (!pdev->dev.dma_mask) {
  1347. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  1348. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  1349. }
  1350. dw_params = dma_read_byaddr(regs, DW_PARAMS);
  1351. autocfg = dw_params >> DW_PARAMS_EN & 0x1;
  1352. dev_dbg(&pdev->dev, "DW_PARAMS: 0x%08x\n", dw_params);
  1353. pdata = dev_get_platdata(&pdev->dev);
  1354. if (!pdata)
  1355. pdata = dw_dma_parse_dt(pdev);
  1356. if (!pdata && autocfg) {
  1357. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1358. if (!pdata)
  1359. return -ENOMEM;
  1360. /* Fill platform data with the default values */
  1361. pdata->is_private = true;
  1362. pdata->chan_allocation_order = CHAN_ALLOCATION_ASCENDING;
  1363. pdata->chan_priority = CHAN_PRIORITY_ASCENDING;
  1364. } else if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
  1365. return -EINVAL;
  1366. if (autocfg)
  1367. nr_channels = (dw_params >> DW_PARAMS_NR_CHAN & 0x7) + 1;
  1368. else
  1369. nr_channels = pdata->nr_channels;
  1370. size = sizeof(struct dw_dma) + nr_channels * sizeof(struct dw_dma_chan);
  1371. dw = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  1372. if (!dw)
  1373. return -ENOMEM;
  1374. dw->clk = devm_clk_get(&pdev->dev, "hclk");
  1375. if (IS_ERR(dw->clk))
  1376. return PTR_ERR(dw->clk);
  1377. clk_prepare_enable(dw->clk);
  1378. dw->regs = regs;
  1379. /* Get hardware configuration parameters */
  1380. if (autocfg) {
  1381. max_blk_size = dma_readl(dw, MAX_BLK_SIZE);
  1382. dw->nr_masters = (dw_params >> DW_PARAMS_NR_MASTER & 3) + 1;
  1383. for (i = 0; i < dw->nr_masters; i++) {
  1384. dw->data_width[i] =
  1385. (dw_params >> DW_PARAMS_DATA_WIDTH(i) & 3) + 2;
  1386. }
  1387. } else {
  1388. dw->nr_masters = pdata->nr_masters;
  1389. memcpy(dw->data_width, pdata->data_width, 4);
  1390. }
  1391. /* Calculate all channel mask before DMA setup */
  1392. dw->all_chan_mask = (1 << nr_channels) - 1;
  1393. /* Force dma off, just in case */
  1394. dw_dma_off(dw);
  1395. /* Disable BLOCK interrupts as well */
  1396. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  1397. err = devm_request_irq(&pdev->dev, irq, dw_dma_interrupt, 0,
  1398. "dw_dmac", dw);
  1399. if (err)
  1400. return err;
  1401. platform_set_drvdata(pdev, dw);
  1402. /* Create a pool of consistent memory blocks for hardware descriptors */
  1403. dw->desc_pool = dmam_pool_create("dw_dmac_desc_pool", &pdev->dev,
  1404. sizeof(struct dw_desc), 4, 0);
  1405. if (!dw->desc_pool) {
  1406. dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
  1407. return -ENOMEM;
  1408. }
  1409. tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
  1410. INIT_LIST_HEAD(&dw->dma.channels);
  1411. for (i = 0; i < nr_channels; i++) {
  1412. struct dw_dma_chan *dwc = &dw->chan[i];
  1413. int r = nr_channels - i - 1;
  1414. dwc->chan.device = &dw->dma;
  1415. dma_cookie_init(&dwc->chan);
  1416. if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
  1417. list_add_tail(&dwc->chan.device_node,
  1418. &dw->dma.channels);
  1419. else
  1420. list_add(&dwc->chan.device_node, &dw->dma.channels);
  1421. /* 7 is highest priority & 0 is lowest. */
  1422. if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
  1423. dwc->priority = r;
  1424. else
  1425. dwc->priority = i;
  1426. dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
  1427. spin_lock_init(&dwc->lock);
  1428. dwc->mask = 1 << i;
  1429. INIT_LIST_HEAD(&dwc->active_list);
  1430. INIT_LIST_HEAD(&dwc->queue);
  1431. INIT_LIST_HEAD(&dwc->free_list);
  1432. channel_clear_bit(dw, CH_EN, dwc->mask);
  1433. dwc->direction = DMA_TRANS_NONE;
  1434. dwc->request_line = ~0;
  1435. /* Hardware configuration */
  1436. if (autocfg) {
  1437. unsigned int dwc_params;
  1438. dwc_params = dma_read_byaddr(regs + r * sizeof(u32),
  1439. DWC_PARAMS);
  1440. dev_dbg(&pdev->dev, "DWC_PARAMS[%d]: 0x%08x\n", i,
  1441. dwc_params);
  1442. /* Decode maximum block size for given channel. The
  1443. * stored 4 bit value represents blocks from 0x00 for 3
  1444. * up to 0x0a for 4095. */
  1445. dwc->block_size =
  1446. (4 << ((max_blk_size >> 4 * i) & 0xf)) - 1;
  1447. dwc->nollp =
  1448. (dwc_params >> DWC_PARAMS_MBLK_EN & 0x1) == 0;
  1449. } else {
  1450. dwc->block_size = pdata->block_size;
  1451. /* Check if channel supports multi block transfer */
  1452. channel_writel(dwc, LLP, 0xfffffffc);
  1453. dwc->nollp =
  1454. (channel_readl(dwc, LLP) & 0xfffffffc) == 0;
  1455. channel_writel(dwc, LLP, 0);
  1456. }
  1457. }
  1458. /* Clear all interrupts on all channels. */
  1459. dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
  1460. dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
  1461. dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
  1462. dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
  1463. dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
  1464. dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
  1465. dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
  1466. if (pdata->is_private)
  1467. dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
  1468. dw->dma.dev = &pdev->dev;
  1469. dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
  1470. dw->dma.device_free_chan_resources = dwc_free_chan_resources;
  1471. dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
  1472. dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
  1473. dw->dma.device_control = dwc_control;
  1474. dw->dma.device_tx_status = dwc_tx_status;
  1475. dw->dma.device_issue_pending = dwc_issue_pending;
  1476. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  1477. dev_info(&pdev->dev, "DesignWare DMA Controller, %d channels\n",
  1478. nr_channels);
  1479. dma_async_device_register(&dw->dma);
  1480. if (pdev->dev.of_node) {
  1481. err = of_dma_controller_register(pdev->dev.of_node,
  1482. dw_dma_of_xlate, dw);
  1483. if (err)
  1484. dev_err(&pdev->dev,
  1485. "could not register of_dma_controller\n");
  1486. }
  1487. if (ACPI_HANDLE(&pdev->dev))
  1488. dw_dma_acpi_controller_register(dw);
  1489. return 0;
  1490. }
  1491. static int dw_remove(struct platform_device *pdev)
  1492. {
  1493. struct dw_dma *dw = platform_get_drvdata(pdev);
  1494. struct dw_dma_chan *dwc, *_dwc;
  1495. if (pdev->dev.of_node)
  1496. of_dma_controller_free(pdev->dev.of_node);
  1497. dw_dma_off(dw);
  1498. dma_async_device_unregister(&dw->dma);
  1499. tasklet_kill(&dw->tasklet);
  1500. list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
  1501. chan.device_node) {
  1502. list_del(&dwc->chan.device_node);
  1503. channel_clear_bit(dw, CH_EN, dwc->mask);
  1504. }
  1505. return 0;
  1506. }
  1507. static void dw_shutdown(struct platform_device *pdev)
  1508. {
  1509. struct dw_dma *dw = platform_get_drvdata(pdev);
  1510. dw_dma_off(dw);
  1511. clk_disable_unprepare(dw->clk);
  1512. }
  1513. static int dw_suspend_noirq(struct device *dev)
  1514. {
  1515. struct platform_device *pdev = to_platform_device(dev);
  1516. struct dw_dma *dw = platform_get_drvdata(pdev);
  1517. dw_dma_off(dw);
  1518. clk_disable_unprepare(dw->clk);
  1519. return 0;
  1520. }
  1521. static int dw_resume_noirq(struct device *dev)
  1522. {
  1523. struct platform_device *pdev = to_platform_device(dev);
  1524. struct dw_dma *dw = platform_get_drvdata(pdev);
  1525. clk_prepare_enable(dw->clk);
  1526. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  1527. return 0;
  1528. }
  1529. static const struct dev_pm_ops dw_dev_pm_ops = {
  1530. .suspend_noirq = dw_suspend_noirq,
  1531. .resume_noirq = dw_resume_noirq,
  1532. .freeze_noirq = dw_suspend_noirq,
  1533. .thaw_noirq = dw_resume_noirq,
  1534. .restore_noirq = dw_resume_noirq,
  1535. .poweroff_noirq = dw_suspend_noirq,
  1536. };
  1537. #ifdef CONFIG_OF
  1538. static const struct of_device_id dw_dma_of_id_table[] = {
  1539. { .compatible = "snps,dma-spear1340" },
  1540. {}
  1541. };
  1542. MODULE_DEVICE_TABLE(of, dw_dma_of_id_table);
  1543. #endif
  1544. #ifdef CONFIG_ACPI
  1545. static const struct acpi_device_id dw_dma_acpi_id_table[] = {
  1546. { "INTL9C60", 0 },
  1547. { }
  1548. };
  1549. #endif
  1550. static struct platform_driver dw_driver = {
  1551. .probe = dw_probe,
  1552. .remove = dw_remove,
  1553. .shutdown = dw_shutdown,
  1554. .driver = {
  1555. .name = "dw_dmac",
  1556. .pm = &dw_dev_pm_ops,
  1557. .of_match_table = of_match_ptr(dw_dma_of_id_table),
  1558. .acpi_match_table = ACPI_PTR(dw_dma_acpi_id_table),
  1559. },
  1560. };
  1561. static int __init dw_init(void)
  1562. {
  1563. return platform_driver_register(&dw_driver);
  1564. }
  1565. subsys_initcall(dw_init);
  1566. static void __exit dw_exit(void)
  1567. {
  1568. platform_driver_unregister(&dw_driver);
  1569. }
  1570. module_exit(dw_exit);
  1571. MODULE_LICENSE("GPL v2");
  1572. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
  1573. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1574. MODULE_AUTHOR("Viresh Kumar <viresh.linux@gmail.com>");