core.c 44 KB

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