core.c 37 KB

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