dw_dmac.c 48 KB

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