dw-axi-dmac-platform.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. // SPDX-License-Identifier: GPL-2.0
  2. // (C) 2017-2018 Synopsys, Inc. (www.synopsys.com)
  3. /*
  4. * Synopsys DesignWare AXI DMA Controller driver.
  5. *
  6. * Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/dmapool.h>
  13. #include <linux/err.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/property.h>
  22. #include <linux/types.h>
  23. #include "dw-axi-dmac.h"
  24. #include "../dmaengine.h"
  25. #include "../virt-dma.h"
  26. /*
  27. * The set of bus widths supported by the DMA controller. DW AXI DMAC supports
  28. * master data bus width up to 512 bits (for both AXI master interfaces), but
  29. * it depends on IP block configurarion.
  30. */
  31. #define AXI_DMA_BUSWIDTHS \
  32. (DMA_SLAVE_BUSWIDTH_1_BYTE | \
  33. DMA_SLAVE_BUSWIDTH_2_BYTES | \
  34. DMA_SLAVE_BUSWIDTH_4_BYTES | \
  35. DMA_SLAVE_BUSWIDTH_8_BYTES | \
  36. DMA_SLAVE_BUSWIDTH_16_BYTES | \
  37. DMA_SLAVE_BUSWIDTH_32_BYTES | \
  38. DMA_SLAVE_BUSWIDTH_64_BYTES)
  39. static inline void
  40. axi_dma_iowrite32(struct axi_dma_chip *chip, u32 reg, u32 val)
  41. {
  42. iowrite32(val, chip->regs + reg);
  43. }
  44. static inline u32 axi_dma_ioread32(struct axi_dma_chip *chip, u32 reg)
  45. {
  46. return ioread32(chip->regs + reg);
  47. }
  48. static inline void
  49. axi_chan_iowrite32(struct axi_dma_chan *chan, u32 reg, u32 val)
  50. {
  51. iowrite32(val, chan->chan_regs + reg);
  52. }
  53. static inline u32 axi_chan_ioread32(struct axi_dma_chan *chan, u32 reg)
  54. {
  55. return ioread32(chan->chan_regs + reg);
  56. }
  57. static inline void
  58. axi_chan_iowrite64(struct axi_dma_chan *chan, u32 reg, u64 val)
  59. {
  60. /*
  61. * We split one 64 bit write for two 32 bit write as some HW doesn't
  62. * support 64 bit access.
  63. */
  64. iowrite32(lower_32_bits(val), chan->chan_regs + reg);
  65. iowrite32(upper_32_bits(val), chan->chan_regs + reg + 4);
  66. }
  67. static inline void axi_dma_disable(struct axi_dma_chip *chip)
  68. {
  69. u32 val;
  70. val = axi_dma_ioread32(chip, DMAC_CFG);
  71. val &= ~DMAC_EN_MASK;
  72. axi_dma_iowrite32(chip, DMAC_CFG, val);
  73. }
  74. static inline void axi_dma_enable(struct axi_dma_chip *chip)
  75. {
  76. u32 val;
  77. val = axi_dma_ioread32(chip, DMAC_CFG);
  78. val |= DMAC_EN_MASK;
  79. axi_dma_iowrite32(chip, DMAC_CFG, val);
  80. }
  81. static inline void axi_dma_irq_disable(struct axi_dma_chip *chip)
  82. {
  83. u32 val;
  84. val = axi_dma_ioread32(chip, DMAC_CFG);
  85. val &= ~INT_EN_MASK;
  86. axi_dma_iowrite32(chip, DMAC_CFG, val);
  87. }
  88. static inline void axi_dma_irq_enable(struct axi_dma_chip *chip)
  89. {
  90. u32 val;
  91. val = axi_dma_ioread32(chip, DMAC_CFG);
  92. val |= INT_EN_MASK;
  93. axi_dma_iowrite32(chip, DMAC_CFG, val);
  94. }
  95. static inline void axi_chan_irq_disable(struct axi_dma_chan *chan, u32 irq_mask)
  96. {
  97. u32 val;
  98. if (likely(irq_mask == DWAXIDMAC_IRQ_ALL)) {
  99. axi_chan_iowrite32(chan, CH_INTSTATUS_ENA, DWAXIDMAC_IRQ_NONE);
  100. } else {
  101. val = axi_chan_ioread32(chan, CH_INTSTATUS_ENA);
  102. val &= ~irq_mask;
  103. axi_chan_iowrite32(chan, CH_INTSTATUS_ENA, val);
  104. }
  105. }
  106. static inline void axi_chan_irq_set(struct axi_dma_chan *chan, u32 irq_mask)
  107. {
  108. axi_chan_iowrite32(chan, CH_INTSTATUS_ENA, irq_mask);
  109. }
  110. static inline void axi_chan_irq_sig_set(struct axi_dma_chan *chan, u32 irq_mask)
  111. {
  112. axi_chan_iowrite32(chan, CH_INTSIGNAL_ENA, irq_mask);
  113. }
  114. static inline void axi_chan_irq_clear(struct axi_dma_chan *chan, u32 irq_mask)
  115. {
  116. axi_chan_iowrite32(chan, CH_INTCLEAR, irq_mask);
  117. }
  118. static inline u32 axi_chan_irq_read(struct axi_dma_chan *chan)
  119. {
  120. return axi_chan_ioread32(chan, CH_INTSTATUS);
  121. }
  122. static inline void axi_chan_disable(struct axi_dma_chan *chan)
  123. {
  124. u32 val;
  125. val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
  126. val &= ~(BIT(chan->id) << DMAC_CHAN_EN_SHIFT);
  127. val |= BIT(chan->id) << DMAC_CHAN_EN_WE_SHIFT;
  128. axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
  129. }
  130. static inline void axi_chan_enable(struct axi_dma_chan *chan)
  131. {
  132. u32 val;
  133. val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
  134. val |= BIT(chan->id) << DMAC_CHAN_EN_SHIFT |
  135. BIT(chan->id) << DMAC_CHAN_EN_WE_SHIFT;
  136. axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
  137. }
  138. static inline bool axi_chan_is_hw_enable(struct axi_dma_chan *chan)
  139. {
  140. u32 val;
  141. val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
  142. return !!(val & (BIT(chan->id) << DMAC_CHAN_EN_SHIFT));
  143. }
  144. static void axi_dma_hw_init(struct axi_dma_chip *chip)
  145. {
  146. u32 i;
  147. for (i = 0; i < chip->dw->hdata->nr_channels; i++) {
  148. axi_chan_irq_disable(&chip->dw->chan[i], DWAXIDMAC_IRQ_ALL);
  149. axi_chan_disable(&chip->dw->chan[i]);
  150. }
  151. }
  152. static u32 axi_chan_get_xfer_width(struct axi_dma_chan *chan, dma_addr_t src,
  153. dma_addr_t dst, size_t len)
  154. {
  155. u32 max_width = chan->chip->dw->hdata->m_data_width;
  156. return __ffs(src | dst | len | BIT(max_width));
  157. }
  158. static inline const char *axi_chan_name(struct axi_dma_chan *chan)
  159. {
  160. return dma_chan_name(&chan->vc.chan);
  161. }
  162. static struct axi_dma_desc *axi_desc_get(struct axi_dma_chan *chan)
  163. {
  164. struct dw_axi_dma *dw = chan->chip->dw;
  165. struct axi_dma_desc *desc;
  166. dma_addr_t phys;
  167. desc = dma_pool_zalloc(dw->desc_pool, GFP_NOWAIT, &phys);
  168. if (unlikely(!desc)) {
  169. dev_err(chan2dev(chan), "%s: not enough descriptors available\n",
  170. axi_chan_name(chan));
  171. return NULL;
  172. }
  173. atomic_inc(&chan->descs_allocated);
  174. INIT_LIST_HEAD(&desc->xfer_list);
  175. desc->vd.tx.phys = phys;
  176. desc->chan = chan;
  177. return desc;
  178. }
  179. static void axi_desc_put(struct axi_dma_desc *desc)
  180. {
  181. struct axi_dma_chan *chan = desc->chan;
  182. struct dw_axi_dma *dw = chan->chip->dw;
  183. struct axi_dma_desc *child, *_next;
  184. unsigned int descs_put = 0;
  185. list_for_each_entry_safe(child, _next, &desc->xfer_list, xfer_list) {
  186. list_del(&child->xfer_list);
  187. dma_pool_free(dw->desc_pool, child, child->vd.tx.phys);
  188. descs_put++;
  189. }
  190. dma_pool_free(dw->desc_pool, desc, desc->vd.tx.phys);
  191. descs_put++;
  192. atomic_sub(descs_put, &chan->descs_allocated);
  193. dev_vdbg(chan2dev(chan), "%s: %d descs put, %d still allocated\n",
  194. axi_chan_name(chan), descs_put,
  195. atomic_read(&chan->descs_allocated));
  196. }
  197. static void vchan_desc_put(struct virt_dma_desc *vdesc)
  198. {
  199. axi_desc_put(vd_to_axi_desc(vdesc));
  200. }
  201. static enum dma_status
  202. dma_chan_tx_status(struct dma_chan *dchan, dma_cookie_t cookie,
  203. struct dma_tx_state *txstate)
  204. {
  205. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  206. enum dma_status ret;
  207. ret = dma_cookie_status(dchan, cookie, txstate);
  208. if (chan->is_paused && ret == DMA_IN_PROGRESS)
  209. ret = DMA_PAUSED;
  210. return ret;
  211. }
  212. static void write_desc_llp(struct axi_dma_desc *desc, dma_addr_t adr)
  213. {
  214. desc->lli.llp = cpu_to_le64(adr);
  215. }
  216. static void write_chan_llp(struct axi_dma_chan *chan, dma_addr_t adr)
  217. {
  218. axi_chan_iowrite64(chan, CH_LLP, adr);
  219. }
  220. /* Called in chan locked context */
  221. static void axi_chan_block_xfer_start(struct axi_dma_chan *chan,
  222. struct axi_dma_desc *first)
  223. {
  224. u32 priority = chan->chip->dw->hdata->priority[chan->id];
  225. u32 reg, irq_mask;
  226. u8 lms = 0; /* Select AXI0 master for LLI fetching */
  227. if (unlikely(axi_chan_is_hw_enable(chan))) {
  228. dev_err(chan2dev(chan), "%s is non-idle!\n",
  229. axi_chan_name(chan));
  230. return;
  231. }
  232. axi_dma_enable(chan->chip);
  233. reg = (DWAXIDMAC_MBLK_TYPE_LL << CH_CFG_L_DST_MULTBLK_TYPE_POS |
  234. DWAXIDMAC_MBLK_TYPE_LL << CH_CFG_L_SRC_MULTBLK_TYPE_POS);
  235. axi_chan_iowrite32(chan, CH_CFG_L, reg);
  236. reg = (DWAXIDMAC_TT_FC_MEM_TO_MEM_DMAC << CH_CFG_H_TT_FC_POS |
  237. priority << CH_CFG_H_PRIORITY_POS |
  238. DWAXIDMAC_HS_SEL_HW << CH_CFG_H_HS_SEL_DST_POS |
  239. DWAXIDMAC_HS_SEL_HW << CH_CFG_H_HS_SEL_SRC_POS);
  240. axi_chan_iowrite32(chan, CH_CFG_H, reg);
  241. write_chan_llp(chan, first->vd.tx.phys | lms);
  242. irq_mask = DWAXIDMAC_IRQ_DMA_TRF | DWAXIDMAC_IRQ_ALL_ERR;
  243. axi_chan_irq_sig_set(chan, irq_mask);
  244. /* Generate 'suspend' status but don't generate interrupt */
  245. irq_mask |= DWAXIDMAC_IRQ_SUSPENDED;
  246. axi_chan_irq_set(chan, irq_mask);
  247. axi_chan_enable(chan);
  248. }
  249. static void axi_chan_start_first_queued(struct axi_dma_chan *chan)
  250. {
  251. struct axi_dma_desc *desc;
  252. struct virt_dma_desc *vd;
  253. vd = vchan_next_desc(&chan->vc);
  254. if (!vd)
  255. return;
  256. desc = vd_to_axi_desc(vd);
  257. dev_vdbg(chan2dev(chan), "%s: started %u\n", axi_chan_name(chan),
  258. vd->tx.cookie);
  259. axi_chan_block_xfer_start(chan, desc);
  260. }
  261. static void dma_chan_issue_pending(struct dma_chan *dchan)
  262. {
  263. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  264. unsigned long flags;
  265. spin_lock_irqsave(&chan->vc.lock, flags);
  266. if (vchan_issue_pending(&chan->vc))
  267. axi_chan_start_first_queued(chan);
  268. spin_unlock_irqrestore(&chan->vc.lock, flags);
  269. }
  270. static int dma_chan_alloc_chan_resources(struct dma_chan *dchan)
  271. {
  272. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  273. /* ASSERT: channel is idle */
  274. if (axi_chan_is_hw_enable(chan)) {
  275. dev_err(chan2dev(chan), "%s is non-idle!\n",
  276. axi_chan_name(chan));
  277. return -EBUSY;
  278. }
  279. dev_vdbg(dchan2dev(dchan), "%s: allocating\n", axi_chan_name(chan));
  280. pm_runtime_get(chan->chip->dev);
  281. return 0;
  282. }
  283. static void dma_chan_free_chan_resources(struct dma_chan *dchan)
  284. {
  285. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  286. /* ASSERT: channel is idle */
  287. if (axi_chan_is_hw_enable(chan))
  288. dev_err(dchan2dev(dchan), "%s is non-idle!\n",
  289. axi_chan_name(chan));
  290. axi_chan_disable(chan);
  291. axi_chan_irq_disable(chan, DWAXIDMAC_IRQ_ALL);
  292. vchan_free_chan_resources(&chan->vc);
  293. dev_vdbg(dchan2dev(dchan),
  294. "%s: free resources, descriptor still allocated: %u\n",
  295. axi_chan_name(chan), atomic_read(&chan->descs_allocated));
  296. pm_runtime_put(chan->chip->dev);
  297. }
  298. /*
  299. * If DW_axi_dmac sees CHx_CTL.ShadowReg_Or_LLI_Last bit of the fetched LLI
  300. * as 1, it understands that the current block is the final block in the
  301. * transfer and completes the DMA transfer operation at the end of current
  302. * block transfer.
  303. */
  304. static void set_desc_last(struct axi_dma_desc *desc)
  305. {
  306. u32 val;
  307. val = le32_to_cpu(desc->lli.ctl_hi);
  308. val |= CH_CTL_H_LLI_LAST;
  309. desc->lli.ctl_hi = cpu_to_le32(val);
  310. }
  311. static void write_desc_sar(struct axi_dma_desc *desc, dma_addr_t adr)
  312. {
  313. desc->lli.sar = cpu_to_le64(adr);
  314. }
  315. static void write_desc_dar(struct axi_dma_desc *desc, dma_addr_t adr)
  316. {
  317. desc->lli.dar = cpu_to_le64(adr);
  318. }
  319. static void set_desc_src_master(struct axi_dma_desc *desc)
  320. {
  321. u32 val;
  322. /* Select AXI0 for source master */
  323. val = le32_to_cpu(desc->lli.ctl_lo);
  324. val &= ~CH_CTL_L_SRC_MAST;
  325. desc->lli.ctl_lo = cpu_to_le32(val);
  326. }
  327. static void set_desc_dest_master(struct axi_dma_desc *desc)
  328. {
  329. u32 val;
  330. /* Select AXI1 for source master if available */
  331. val = le32_to_cpu(desc->lli.ctl_lo);
  332. if (desc->chan->chip->dw->hdata->nr_masters > 1)
  333. val |= CH_CTL_L_DST_MAST;
  334. else
  335. val &= ~CH_CTL_L_DST_MAST;
  336. desc->lli.ctl_lo = cpu_to_le32(val);
  337. }
  338. static struct dma_async_tx_descriptor *
  339. dma_chan_prep_dma_memcpy(struct dma_chan *dchan, dma_addr_t dst_adr,
  340. dma_addr_t src_adr, size_t len, unsigned long flags)
  341. {
  342. struct axi_dma_desc *first = NULL, *desc = NULL, *prev = NULL;
  343. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  344. size_t block_ts, max_block_ts, xfer_len;
  345. u32 xfer_width, reg;
  346. u8 lms = 0; /* Select AXI0 master for LLI fetching */
  347. dev_dbg(chan2dev(chan), "%s: memcpy: src: %pad dst: %pad length: %zd flags: %#lx",
  348. axi_chan_name(chan), &src_adr, &dst_adr, len, flags);
  349. max_block_ts = chan->chip->dw->hdata->block_size[chan->id];
  350. while (len) {
  351. xfer_len = len;
  352. /*
  353. * Take care for the alignment.
  354. * Actually source and destination widths can be different, but
  355. * make them same to be simpler.
  356. */
  357. xfer_width = axi_chan_get_xfer_width(chan, src_adr, dst_adr, xfer_len);
  358. /*
  359. * block_ts indicates the total number of data of width
  360. * to be transferred in a DMA block transfer.
  361. * BLOCK_TS register should be set to block_ts - 1
  362. */
  363. block_ts = xfer_len >> xfer_width;
  364. if (block_ts > max_block_ts) {
  365. block_ts = max_block_ts;
  366. xfer_len = max_block_ts << xfer_width;
  367. }
  368. desc = axi_desc_get(chan);
  369. if (unlikely(!desc))
  370. goto err_desc_get;
  371. write_desc_sar(desc, src_adr);
  372. write_desc_dar(desc, dst_adr);
  373. desc->lli.block_ts_lo = cpu_to_le32(block_ts - 1);
  374. reg = CH_CTL_H_LLI_VALID;
  375. if (chan->chip->dw->hdata->restrict_axi_burst_len) {
  376. u32 burst_len = chan->chip->dw->hdata->axi_rw_burst_len;
  377. reg |= (CH_CTL_H_ARLEN_EN |
  378. burst_len << CH_CTL_H_ARLEN_POS |
  379. CH_CTL_H_AWLEN_EN |
  380. burst_len << CH_CTL_H_AWLEN_POS);
  381. }
  382. desc->lli.ctl_hi = cpu_to_le32(reg);
  383. reg = (DWAXIDMAC_BURST_TRANS_LEN_4 << CH_CTL_L_DST_MSIZE_POS |
  384. DWAXIDMAC_BURST_TRANS_LEN_4 << CH_CTL_L_SRC_MSIZE_POS |
  385. xfer_width << CH_CTL_L_DST_WIDTH_POS |
  386. xfer_width << CH_CTL_L_SRC_WIDTH_POS |
  387. DWAXIDMAC_CH_CTL_L_INC << CH_CTL_L_DST_INC_POS |
  388. DWAXIDMAC_CH_CTL_L_INC << CH_CTL_L_SRC_INC_POS);
  389. desc->lli.ctl_lo = cpu_to_le32(reg);
  390. set_desc_src_master(desc);
  391. set_desc_dest_master(desc);
  392. /* Manage transfer list (xfer_list) */
  393. if (!first) {
  394. first = desc;
  395. } else {
  396. list_add_tail(&desc->xfer_list, &first->xfer_list);
  397. write_desc_llp(prev, desc->vd.tx.phys | lms);
  398. }
  399. prev = desc;
  400. /* update the length and addresses for the next loop cycle */
  401. len -= xfer_len;
  402. dst_adr += xfer_len;
  403. src_adr += xfer_len;
  404. }
  405. /* Total len of src/dest sg == 0, so no descriptor were allocated */
  406. if (unlikely(!first))
  407. return NULL;
  408. /* Set end-of-link to the last link descriptor of list */
  409. set_desc_last(desc);
  410. return vchan_tx_prep(&chan->vc, &first->vd, flags);
  411. err_desc_get:
  412. if (first)
  413. axi_desc_put(first);
  414. return NULL;
  415. }
  416. static void axi_chan_dump_lli(struct axi_dma_chan *chan,
  417. struct axi_dma_desc *desc)
  418. {
  419. dev_err(dchan2dev(&chan->vc.chan),
  420. "SAR: 0x%llx DAR: 0x%llx LLP: 0x%llx BTS 0x%x CTL: 0x%x:%08x",
  421. le64_to_cpu(desc->lli.sar),
  422. le64_to_cpu(desc->lli.dar),
  423. le64_to_cpu(desc->lli.llp),
  424. le32_to_cpu(desc->lli.block_ts_lo),
  425. le32_to_cpu(desc->lli.ctl_hi),
  426. le32_to_cpu(desc->lli.ctl_lo));
  427. }
  428. static void axi_chan_list_dump_lli(struct axi_dma_chan *chan,
  429. struct axi_dma_desc *desc_head)
  430. {
  431. struct axi_dma_desc *desc;
  432. axi_chan_dump_lli(chan, desc_head);
  433. list_for_each_entry(desc, &desc_head->xfer_list, xfer_list)
  434. axi_chan_dump_lli(chan, desc);
  435. }
  436. static noinline void axi_chan_handle_err(struct axi_dma_chan *chan, u32 status)
  437. {
  438. struct virt_dma_desc *vd;
  439. unsigned long flags;
  440. spin_lock_irqsave(&chan->vc.lock, flags);
  441. axi_chan_disable(chan);
  442. /* The bad descriptor currently is in the head of vc list */
  443. vd = vchan_next_desc(&chan->vc);
  444. /* Remove the completed descriptor from issued list */
  445. list_del(&vd->node);
  446. /* WARN about bad descriptor */
  447. dev_err(chan2dev(chan),
  448. "Bad descriptor submitted for %s, cookie: %d, irq: 0x%08x\n",
  449. axi_chan_name(chan), vd->tx.cookie, status);
  450. axi_chan_list_dump_lli(chan, vd_to_axi_desc(vd));
  451. vchan_cookie_complete(vd);
  452. /* Try to restart the controller */
  453. axi_chan_start_first_queued(chan);
  454. spin_unlock_irqrestore(&chan->vc.lock, flags);
  455. }
  456. static void axi_chan_block_xfer_complete(struct axi_dma_chan *chan)
  457. {
  458. struct virt_dma_desc *vd;
  459. unsigned long flags;
  460. spin_lock_irqsave(&chan->vc.lock, flags);
  461. if (unlikely(axi_chan_is_hw_enable(chan))) {
  462. dev_err(chan2dev(chan), "BUG: %s caught DWAXIDMAC_IRQ_DMA_TRF, but channel not idle!\n",
  463. axi_chan_name(chan));
  464. axi_chan_disable(chan);
  465. }
  466. /* The completed descriptor currently is in the head of vc list */
  467. vd = vchan_next_desc(&chan->vc);
  468. /* Remove the completed descriptor from issued list before completing */
  469. list_del(&vd->node);
  470. vchan_cookie_complete(vd);
  471. /* Submit queued descriptors after processing the completed ones */
  472. axi_chan_start_first_queued(chan);
  473. spin_unlock_irqrestore(&chan->vc.lock, flags);
  474. }
  475. static irqreturn_t dw_axi_dma_interrupt(int irq, void *dev_id)
  476. {
  477. struct axi_dma_chip *chip = dev_id;
  478. struct dw_axi_dma *dw = chip->dw;
  479. struct axi_dma_chan *chan;
  480. u32 status, i;
  481. /* Disable DMAC inerrupts. We'll enable them after processing chanels */
  482. axi_dma_irq_disable(chip);
  483. /* Poll, clear and process every chanel interrupt status */
  484. for (i = 0; i < dw->hdata->nr_channels; i++) {
  485. chan = &dw->chan[i];
  486. status = axi_chan_irq_read(chan);
  487. axi_chan_irq_clear(chan, status);
  488. dev_vdbg(chip->dev, "%s %u IRQ status: 0x%08x\n",
  489. axi_chan_name(chan), i, status);
  490. if (status & DWAXIDMAC_IRQ_ALL_ERR)
  491. axi_chan_handle_err(chan, status);
  492. else if (status & DWAXIDMAC_IRQ_DMA_TRF)
  493. axi_chan_block_xfer_complete(chan);
  494. }
  495. /* Re-enable interrupts */
  496. axi_dma_irq_enable(chip);
  497. return IRQ_HANDLED;
  498. }
  499. static int dma_chan_terminate_all(struct dma_chan *dchan)
  500. {
  501. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  502. unsigned long flags;
  503. LIST_HEAD(head);
  504. spin_lock_irqsave(&chan->vc.lock, flags);
  505. axi_chan_disable(chan);
  506. vchan_get_all_descriptors(&chan->vc, &head);
  507. /*
  508. * As vchan_dma_desc_free_list can access to desc_allocated list
  509. * we need to call it in vc.lock context.
  510. */
  511. vchan_dma_desc_free_list(&chan->vc, &head);
  512. spin_unlock_irqrestore(&chan->vc.lock, flags);
  513. dev_vdbg(dchan2dev(dchan), "terminated: %s\n", axi_chan_name(chan));
  514. return 0;
  515. }
  516. static int dma_chan_pause(struct dma_chan *dchan)
  517. {
  518. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  519. unsigned long flags;
  520. unsigned int timeout = 20; /* timeout iterations */
  521. u32 val;
  522. spin_lock_irqsave(&chan->vc.lock, flags);
  523. val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
  524. val |= BIT(chan->id) << DMAC_CHAN_SUSP_SHIFT |
  525. BIT(chan->id) << DMAC_CHAN_SUSP_WE_SHIFT;
  526. axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
  527. do {
  528. if (axi_chan_irq_read(chan) & DWAXIDMAC_IRQ_SUSPENDED)
  529. break;
  530. udelay(2);
  531. } while (--timeout);
  532. axi_chan_irq_clear(chan, DWAXIDMAC_IRQ_SUSPENDED);
  533. chan->is_paused = true;
  534. spin_unlock_irqrestore(&chan->vc.lock, flags);
  535. return timeout ? 0 : -EAGAIN;
  536. }
  537. /* Called in chan locked context */
  538. static inline void axi_chan_resume(struct axi_dma_chan *chan)
  539. {
  540. u32 val;
  541. val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
  542. val &= ~(BIT(chan->id) << DMAC_CHAN_SUSP_SHIFT);
  543. val |= (BIT(chan->id) << DMAC_CHAN_SUSP_WE_SHIFT);
  544. axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
  545. chan->is_paused = false;
  546. }
  547. static int dma_chan_resume(struct dma_chan *dchan)
  548. {
  549. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  550. unsigned long flags;
  551. spin_lock_irqsave(&chan->vc.lock, flags);
  552. if (chan->is_paused)
  553. axi_chan_resume(chan);
  554. spin_unlock_irqrestore(&chan->vc.lock, flags);
  555. return 0;
  556. }
  557. static int axi_dma_suspend(struct axi_dma_chip *chip)
  558. {
  559. axi_dma_irq_disable(chip);
  560. axi_dma_disable(chip);
  561. clk_disable_unprepare(chip->core_clk);
  562. clk_disable_unprepare(chip->cfgr_clk);
  563. return 0;
  564. }
  565. static int axi_dma_resume(struct axi_dma_chip *chip)
  566. {
  567. int ret;
  568. ret = clk_prepare_enable(chip->cfgr_clk);
  569. if (ret < 0)
  570. return ret;
  571. ret = clk_prepare_enable(chip->core_clk);
  572. if (ret < 0)
  573. return ret;
  574. axi_dma_enable(chip);
  575. axi_dma_irq_enable(chip);
  576. return 0;
  577. }
  578. static int __maybe_unused axi_dma_runtime_suspend(struct device *dev)
  579. {
  580. struct axi_dma_chip *chip = dev_get_drvdata(dev);
  581. return axi_dma_suspend(chip);
  582. }
  583. static int __maybe_unused axi_dma_runtime_resume(struct device *dev)
  584. {
  585. struct axi_dma_chip *chip = dev_get_drvdata(dev);
  586. return axi_dma_resume(chip);
  587. }
  588. static int parse_device_properties(struct axi_dma_chip *chip)
  589. {
  590. struct device *dev = chip->dev;
  591. u32 tmp, carr[DMAC_MAX_CHANNELS];
  592. int ret;
  593. ret = device_property_read_u32(dev, "dma-channels", &tmp);
  594. if (ret)
  595. return ret;
  596. if (tmp == 0 || tmp > DMAC_MAX_CHANNELS)
  597. return -EINVAL;
  598. chip->dw->hdata->nr_channels = tmp;
  599. ret = device_property_read_u32(dev, "snps,dma-masters", &tmp);
  600. if (ret)
  601. return ret;
  602. if (tmp == 0 || tmp > DMAC_MAX_MASTERS)
  603. return -EINVAL;
  604. chip->dw->hdata->nr_masters = tmp;
  605. ret = device_property_read_u32(dev, "snps,data-width", &tmp);
  606. if (ret)
  607. return ret;
  608. if (tmp > DWAXIDMAC_TRANS_WIDTH_MAX)
  609. return -EINVAL;
  610. chip->dw->hdata->m_data_width = tmp;
  611. ret = device_property_read_u32_array(dev, "snps,block-size", carr,
  612. chip->dw->hdata->nr_channels);
  613. if (ret)
  614. return ret;
  615. for (tmp = 0; tmp < chip->dw->hdata->nr_channels; tmp++) {
  616. if (carr[tmp] == 0 || carr[tmp] > DMAC_MAX_BLK_SIZE)
  617. return -EINVAL;
  618. chip->dw->hdata->block_size[tmp] = carr[tmp];
  619. }
  620. ret = device_property_read_u32_array(dev, "snps,priority", carr,
  621. chip->dw->hdata->nr_channels);
  622. if (ret)
  623. return ret;
  624. /* Priority value must be programmed within [0:nr_channels-1] range */
  625. for (tmp = 0; tmp < chip->dw->hdata->nr_channels; tmp++) {
  626. if (carr[tmp] >= chip->dw->hdata->nr_channels)
  627. return -EINVAL;
  628. chip->dw->hdata->priority[tmp] = carr[tmp];
  629. }
  630. /* axi-max-burst-len is optional property */
  631. ret = device_property_read_u32(dev, "snps,axi-max-burst-len", &tmp);
  632. if (!ret) {
  633. if (tmp > DWAXIDMAC_ARWLEN_MAX + 1)
  634. return -EINVAL;
  635. if (tmp < DWAXIDMAC_ARWLEN_MIN + 1)
  636. return -EINVAL;
  637. chip->dw->hdata->restrict_axi_burst_len = true;
  638. chip->dw->hdata->axi_rw_burst_len = tmp - 1;
  639. }
  640. return 0;
  641. }
  642. static int dw_probe(struct platform_device *pdev)
  643. {
  644. struct axi_dma_chip *chip;
  645. struct resource *mem;
  646. struct dw_axi_dma *dw;
  647. struct dw_axi_dma_hcfg *hdata;
  648. u32 i;
  649. int ret;
  650. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  651. if (!chip)
  652. return -ENOMEM;
  653. dw = devm_kzalloc(&pdev->dev, sizeof(*dw), GFP_KERNEL);
  654. if (!dw)
  655. return -ENOMEM;
  656. hdata = devm_kzalloc(&pdev->dev, sizeof(*hdata), GFP_KERNEL);
  657. if (!hdata)
  658. return -ENOMEM;
  659. chip->dw = dw;
  660. chip->dev = &pdev->dev;
  661. chip->dw->hdata = hdata;
  662. chip->irq = platform_get_irq(pdev, 0);
  663. if (chip->irq < 0)
  664. return chip->irq;
  665. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  666. chip->regs = devm_ioremap_resource(chip->dev, mem);
  667. if (IS_ERR(chip->regs))
  668. return PTR_ERR(chip->regs);
  669. chip->core_clk = devm_clk_get(chip->dev, "core-clk");
  670. if (IS_ERR(chip->core_clk))
  671. return PTR_ERR(chip->core_clk);
  672. chip->cfgr_clk = devm_clk_get(chip->dev, "cfgr-clk");
  673. if (IS_ERR(chip->cfgr_clk))
  674. return PTR_ERR(chip->cfgr_clk);
  675. ret = parse_device_properties(chip);
  676. if (ret)
  677. return ret;
  678. dw->chan = devm_kcalloc(chip->dev, hdata->nr_channels,
  679. sizeof(*dw->chan), GFP_KERNEL);
  680. if (!dw->chan)
  681. return -ENOMEM;
  682. ret = devm_request_irq(chip->dev, chip->irq, dw_axi_dma_interrupt,
  683. IRQF_SHARED, KBUILD_MODNAME, chip);
  684. if (ret)
  685. return ret;
  686. /* Lli address must be aligned to a 64-byte boundary */
  687. dw->desc_pool = dmam_pool_create(KBUILD_MODNAME, chip->dev,
  688. sizeof(struct axi_dma_desc), 64, 0);
  689. if (!dw->desc_pool) {
  690. dev_err(chip->dev, "No memory for descriptors dma pool\n");
  691. return -ENOMEM;
  692. }
  693. INIT_LIST_HEAD(&dw->dma.channels);
  694. for (i = 0; i < hdata->nr_channels; i++) {
  695. struct axi_dma_chan *chan = &dw->chan[i];
  696. chan->chip = chip;
  697. chan->id = i;
  698. chan->chan_regs = chip->regs + COMMON_REG_LEN + i * CHAN_REG_LEN;
  699. atomic_set(&chan->descs_allocated, 0);
  700. chan->vc.desc_free = vchan_desc_put;
  701. vchan_init(&chan->vc, &dw->dma);
  702. }
  703. /* Set capabilities */
  704. dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
  705. /* DMA capabilities */
  706. dw->dma.chancnt = hdata->nr_channels;
  707. dw->dma.src_addr_widths = AXI_DMA_BUSWIDTHS;
  708. dw->dma.dst_addr_widths = AXI_DMA_BUSWIDTHS;
  709. dw->dma.directions = BIT(DMA_MEM_TO_MEM);
  710. dw->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  711. dw->dma.dev = chip->dev;
  712. dw->dma.device_tx_status = dma_chan_tx_status;
  713. dw->dma.device_issue_pending = dma_chan_issue_pending;
  714. dw->dma.device_terminate_all = dma_chan_terminate_all;
  715. dw->dma.device_pause = dma_chan_pause;
  716. dw->dma.device_resume = dma_chan_resume;
  717. dw->dma.device_alloc_chan_resources = dma_chan_alloc_chan_resources;
  718. dw->dma.device_free_chan_resources = dma_chan_free_chan_resources;
  719. dw->dma.device_prep_dma_memcpy = dma_chan_prep_dma_memcpy;
  720. platform_set_drvdata(pdev, chip);
  721. pm_runtime_enable(chip->dev);
  722. /*
  723. * We can't just call pm_runtime_get here instead of
  724. * pm_runtime_get_noresume + axi_dma_resume because we need
  725. * driver to work also without Runtime PM.
  726. */
  727. pm_runtime_get_noresume(chip->dev);
  728. ret = axi_dma_resume(chip);
  729. if (ret < 0)
  730. goto err_pm_disable;
  731. axi_dma_hw_init(chip);
  732. pm_runtime_put(chip->dev);
  733. ret = dma_async_device_register(&dw->dma);
  734. if (ret)
  735. goto err_pm_disable;
  736. dev_info(chip->dev, "DesignWare AXI DMA Controller, %d channels\n",
  737. dw->hdata->nr_channels);
  738. return 0;
  739. err_pm_disable:
  740. pm_runtime_disable(chip->dev);
  741. return ret;
  742. }
  743. static int dw_remove(struct platform_device *pdev)
  744. {
  745. struct axi_dma_chip *chip = platform_get_drvdata(pdev);
  746. struct dw_axi_dma *dw = chip->dw;
  747. struct axi_dma_chan *chan, *_chan;
  748. u32 i;
  749. /* Enable clk before accessing to registers */
  750. clk_prepare_enable(chip->cfgr_clk);
  751. clk_prepare_enable(chip->core_clk);
  752. axi_dma_irq_disable(chip);
  753. for (i = 0; i < dw->hdata->nr_channels; i++) {
  754. axi_chan_disable(&chip->dw->chan[i]);
  755. axi_chan_irq_disable(&chip->dw->chan[i], DWAXIDMAC_IRQ_ALL);
  756. }
  757. axi_dma_disable(chip);
  758. pm_runtime_disable(chip->dev);
  759. axi_dma_suspend(chip);
  760. devm_free_irq(chip->dev, chip->irq, chip);
  761. list_for_each_entry_safe(chan, _chan, &dw->dma.channels,
  762. vc.chan.device_node) {
  763. list_del(&chan->vc.chan.device_node);
  764. tasklet_kill(&chan->vc.task);
  765. }
  766. dma_async_device_unregister(&dw->dma);
  767. return 0;
  768. }
  769. static const struct dev_pm_ops dw_axi_dma_pm_ops = {
  770. SET_RUNTIME_PM_OPS(axi_dma_runtime_suspend, axi_dma_runtime_resume, NULL)
  771. };
  772. static const struct of_device_id dw_dma_of_id_table[] = {
  773. { .compatible = "snps,axi-dma-1.01a" },
  774. {}
  775. };
  776. MODULE_DEVICE_TABLE(of, dw_dma_of_id_table);
  777. static struct platform_driver dw_driver = {
  778. .probe = dw_probe,
  779. .remove = dw_remove,
  780. .driver = {
  781. .name = KBUILD_MODNAME,
  782. .of_match_table = of_match_ptr(dw_dma_of_id_table),
  783. .pm = &dw_axi_dma_pm_ops,
  784. },
  785. };
  786. module_platform_driver(dw_driver);
  787. MODULE_LICENSE("GPL v2");
  788. MODULE_DESCRIPTION("Synopsys DesignWare AXI DMA Controller platform driver");
  789. MODULE_AUTHOR("Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>");