dw-axi-dmac-platform.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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>");