ark-axi-dma.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  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/iopoll.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/property.h>
  23. #include <linux/types.h>
  24. #include <linux/of_dma.h>
  25. #include "ark-axi-dma.h"
  26. #include "dmaengine.h"
  27. #include "virt-dma.h"
  28. /*
  29. * This supports the Synopsys "DesignWare AHB Central DMA Controller",
  30. * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all
  31. * of which use ARM any more). See the "Databook" from Synopsys for
  32. * information beyond what licensees probably provide.
  33. *
  34. * The driver has been tested with the Atmel AT32AP7000, which does not
  35. * support descriptor writeback.
  36. */
  37. #define DWC_DEFAULT_CTLLO(_chan) ({ \
  38. struct axi_dma_chan *_dwc = dchan_to_axi_dma_chan(_chan); \
  39. struct dma_slave_config *_sconfig = &_dwc->dma_sconfig; \
  40. bool _is_slave = is_slave_direction(_dwc->direction); \
  41. u8 _smsize = _is_slave ? _sconfig->src_maxburst : \
  42. DWAXIDMAC_BURST_TRANS_LEN_16; \
  43. u8 _dmsize = _is_slave ? _sconfig->dst_maxburst : \
  44. DWAXIDMAC_BURST_TRANS_LEN_16; \
  45. u8 _dms = (_dwc->direction == DMA_MEM_TO_DEV) ? \
  46. _dwc->dws.p_master : _dwc->dws.m_master; \
  47. u8 _sms = (_dwc->direction == DMA_DEV_TO_MEM) ? \
  48. _dwc->dws.p_master : _dwc->dws.m_master; \
  49. \
  50. (DWC_CTLL_DST_MSIZE(_dmsize) \
  51. | DWC_CTLL_SRC_MSIZE(_smsize) \
  52. | DWC_CTLL_DMS(_dms) \
  53. | DWC_CTLL_SMS(_sms)); \
  54. })
  55. /*
  56. * The set of bus widths supported by the DMA controller. DW AXI DMAC supports
  57. * master data bus width up to 512 bits (for both AXI master interfaces), but
  58. * it depends on IP block configurarion.
  59. */
  60. #define AXI_DMA_BUSWIDTHS \
  61. (DMA_SLAVE_BUSWIDTH_1_BYTE | \
  62. DMA_SLAVE_BUSWIDTH_2_BYTES | \
  63. DMA_SLAVE_BUSWIDTH_4_BYTES | \
  64. DMA_SLAVE_BUSWIDTH_8_BYTES | \
  65. DMA_SLAVE_BUSWIDTH_16_BYTES | \
  66. DMA_SLAVE_BUSWIDTH_32_BYTES | \
  67. DMA_SLAVE_BUSWIDTH_64_BYTES)
  68. static inline void
  69. axi_dma_iowrite32(struct axi_dma_chip *chip, u32 reg, u32 val)
  70. {
  71. iowrite32(val, chip->regs + reg);
  72. }
  73. static inline u32 axi_dma_ioread32(struct axi_dma_chip *chip, u32 reg)
  74. {
  75. return ioread32(chip->regs + reg);
  76. }
  77. static inline void
  78. axi_chan_iowrite32(struct axi_dma_chan *chan, u32 reg, u32 val)
  79. {
  80. iowrite32(val, chan->chan_regs + reg);
  81. }
  82. static inline u32 axi_chan_ioread32(struct axi_dma_chan *chan, u32 reg)
  83. {
  84. return ioread32(chan->chan_regs + reg);
  85. }
  86. static inline void
  87. axi_chan_iowrite64(struct axi_dma_chan *chan, u32 reg, u64 val)
  88. {
  89. /*
  90. * We split one 64 bit write for two 32 bit write as some HW doesn't
  91. * support 64 bit access.
  92. */
  93. iowrite32(lower_32_bits(val), chan->chan_regs + reg);
  94. iowrite32(upper_32_bits(val), chan->chan_regs + reg + 4);
  95. }
  96. static inline void axi_dma_disable(struct axi_dma_chip *chip)
  97. {
  98. u32 val;
  99. val = axi_dma_ioread32(chip, DMAC_CFG);
  100. val &= ~DMAC_EN_MASK;
  101. axi_dma_iowrite32(chip, DMAC_CFG, val);
  102. }
  103. static inline void axi_dma_enable(struct axi_dma_chip *chip)
  104. {
  105. u32 val;
  106. val = axi_dma_ioread32(chip, DMAC_CFG);
  107. val |= DMAC_EN_MASK;
  108. axi_dma_iowrite32(chip, DMAC_CFG, val);
  109. }
  110. static inline void axi_dma_irq_disable(struct axi_dma_chip *chip)
  111. {
  112. u32 val;
  113. val = axi_dma_ioread32(chip, DMAC_CFG);
  114. val &= ~INT_EN_MASK;
  115. axi_dma_iowrite32(chip, DMAC_CFG, val);
  116. }
  117. static inline void axi_dma_irq_enable(struct axi_dma_chip *chip)
  118. {
  119. u32 val;
  120. val = axi_dma_ioread32(chip, DMAC_CFG);
  121. val |= INT_EN_MASK;
  122. axi_dma_iowrite32(chip, DMAC_CFG, val);
  123. }
  124. static inline void axi_chan_irq_disable(struct axi_dma_chan *chan, u32 irq_mask)
  125. {
  126. u32 val;
  127. if (likely(irq_mask == DWAXIDMAC_IRQ_ALL)) {
  128. axi_chan_iowrite32(chan, CH_INTSTATUS_ENA, DWAXIDMAC_IRQ_NONE);
  129. } else {
  130. val = axi_chan_ioread32(chan, CH_INTSTATUS_ENA);
  131. val &= ~irq_mask;
  132. axi_chan_iowrite32(chan, CH_INTSTATUS_ENA, val);
  133. }
  134. }
  135. static inline void axi_chan_irq_set(struct axi_dma_chan *chan, u32 irq_mask)
  136. {
  137. axi_chan_iowrite32(chan, CH_INTSTATUS_ENA, irq_mask);
  138. }
  139. static inline void axi_chan_irq_sig_set(struct axi_dma_chan *chan, u32 irq_mask)
  140. {
  141. axi_chan_iowrite32(chan, CH_INTSIGNAL_ENA, irq_mask);
  142. }
  143. static inline void axi_chan_irq_clear(struct axi_dma_chan *chan, u32 irq_mask)
  144. {
  145. axi_chan_iowrite32(chan, CH_INTCLEAR, irq_mask);
  146. }
  147. static inline bool axi_chan_is_hw_enable(struct axi_dma_chan *chan)
  148. {
  149. u32 val;
  150. val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
  151. return !!(val & (BIT(chan->id) << DMAC_CHAN_EN_SHIFT));
  152. }
  153. static inline u32 axi_chan_irq_read(struct axi_dma_chan *chan)
  154. {
  155. return axi_chan_ioread32(chan, CH_INTSTATUS);
  156. }
  157. static inline void axi_chan_disable(struct axi_dma_chan *chan)
  158. {
  159. u32 val;
  160. val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
  161. val &= ~(BIT(chan->id) << DMAC_CHAN_EN_SHIFT);
  162. val |= BIT(chan->id) << DMAC_CHAN_EN_WE_SHIFT;
  163. axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
  164. }
  165. static inline void axi_chan_enable(struct axi_dma_chan *chan)
  166. {
  167. u32 val;
  168. val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
  169. val |= BIT(chan->id) << DMAC_CHAN_EN_SHIFT |
  170. BIT(chan->id) << DMAC_CHAN_EN_WE_SHIFT;
  171. axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
  172. }
  173. static void axi_dma_hw_init(struct axi_dma_chip *chip)
  174. {
  175. u32 i;
  176. for (i = 0; i < chip->dw->hdata->nr_channels; i++) {
  177. axi_chan_irq_disable(&chip->dw->chan[i], DWAXIDMAC_IRQ_ALL);
  178. axi_chan_disable(&chip->dw->chan[i]);
  179. }
  180. }
  181. static u32 axi_chan_get_xfer_width(struct axi_dma_chan *chan, dma_addr_t src,
  182. dma_addr_t dst, size_t len)
  183. {
  184. u32 max_width = chan->chip->dw->hdata->m_data_width;
  185. return __ffs(src | dst | len | BIT(max_width));
  186. }
  187. static inline const char *axi_chan_name(struct axi_dma_chan *chan)
  188. {
  189. return dma_chan_name(&chan->vc.chan);
  190. }
  191. static struct axi_dma_desc *axi_desc_get(struct axi_dma_chan *chan)
  192. {
  193. struct dw_axi_dma *dw = chan->chip->dw;
  194. struct axi_dma_desc *desc;
  195. dma_addr_t phys;
  196. desc = dma_pool_zalloc(dw->desc_pool, GFP_NOWAIT, &phys);
  197. if (unlikely(!desc)) {
  198. dev_err(chan2dev(chan), "%s: not enough descriptors available\n",
  199. axi_chan_name(chan));
  200. return NULL;
  201. }
  202. atomic_inc(&chan->descs_allocated);
  203. INIT_LIST_HEAD(&desc->xfer_list);
  204. desc->vd.tx.phys = phys;
  205. desc->chan = chan;
  206. return desc;
  207. }
  208. static void axi_desc_put(struct axi_dma_desc *desc)
  209. {
  210. struct axi_dma_chan *chan = desc->chan;
  211. struct dw_axi_dma *dw = chan->chip->dw;
  212. struct axi_dma_desc *child, *_next;
  213. unsigned int descs_put = 0;
  214. list_for_each_entry_safe(child, _next, &desc->xfer_list, xfer_list) {
  215. list_del(&child->xfer_list);
  216. dma_pool_free(dw->desc_pool, child, child->vd.tx.phys);
  217. descs_put++;
  218. }
  219. dma_pool_free(dw->desc_pool, desc, desc->vd.tx.phys);
  220. descs_put++;
  221. atomic_sub(descs_put, &chan->descs_allocated);
  222. dev_vdbg(chan2dev(chan), "%s: %d descs put, %d still allocated\n",
  223. axi_chan_name(chan), descs_put,
  224. atomic_read(&chan->descs_allocated));
  225. }
  226. static void vchan_desc_put(struct virt_dma_desc *vdesc)
  227. {
  228. axi_desc_put(vd_to_axi_desc(vdesc));
  229. }
  230. /* Returns how many bytes were already received from source */
  231. static inline u32 dma_chan_get_sent(struct axi_dma_chan *chan)
  232. {
  233. u32 block_ts = axi_chan_ioread32(chan, CH_STATUS);
  234. u32 ctllo = axi_chan_ioread32(chan, CH_CTL_L);
  235. //printk("dma_chan_get_sent %d.\n", (block_ts & DMAX_MAX_BLK_MASK) * (1 << (ctllo >> CH_CTL_L_SRC_WIDTH_POS & 7)));
  236. return (block_ts & DMAX_MAX_BLK_MASK) * (1 << (ctllo >> CH_CTL_L_SRC_WIDTH_POS & 7));
  237. }
  238. static u32 dma_chan_get_residue(struct axi_dma_chan *chan, dma_cookie_t cookie)
  239. {
  240. struct axi_dma_desc *first, *desc;
  241. struct virt_dma_desc *vd;
  242. unsigned long flags;
  243. u32 residue = 0;
  244. u32 sentf, sentr;
  245. u32 llplo, llphi;
  246. u64 llp;
  247. spin_lock_irqsave(&chan->vc.lock, flags);
  248. sentf = dma_chan_get_sent(chan);
  249. llplo = axi_chan_ioread32(chan, CH_LLP);
  250. llphi = axi_chan_ioread32(chan, CH_LLP + 4);
  251. llp = ((u64)llphi << 32) | llplo;
  252. sentr = dma_chan_get_sent(chan);
  253. if (sentr < sentf) {
  254. llplo = axi_chan_ioread32(chan, CH_LLP);
  255. llphi = axi_chan_ioread32(chan, CH_LLP + 4);
  256. llp = ((u64)llphi << 32) | llplo;
  257. }
  258. vd = vchan_next_desc(&chan->vc);
  259. if (vd) {
  260. first = vd_to_axi_desc(vd);
  261. residue = first->total_len;
  262. if (llp == DWC_LLP_LOC(first->lli.llp)) {
  263. /* Currently in progress */
  264. residue -= sentr;
  265. } else {
  266. residue -= first->len;
  267. list_for_each_entry(desc, &first->xfer_list, xfer_list) {
  268. if (llp == DWC_LLP_LOC(desc->lli.llp)) {
  269. /* Currently in progress */
  270. residue -= sentr;
  271. break;
  272. } else {
  273. residue -= desc->len;
  274. }
  275. }
  276. }
  277. } else {
  278. residue = 0;
  279. }
  280. //printk("dma_chan_get_residue %d.\n", residue);
  281. spin_unlock_irqrestore(&chan->vc.lock, flags);
  282. return residue;
  283. }
  284. static enum dma_status
  285. dma_chan_tx_status(struct dma_chan *dchan, dma_cookie_t cookie,
  286. struct dma_tx_state *txstate)
  287. {
  288. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  289. enum dma_status ret;
  290. ret = dma_cookie_status(dchan, cookie, txstate);
  291. if (ret == DMA_COMPLETE)
  292. return ret;
  293. dma_set_residue(txstate, dma_chan_get_residue(chan, cookie));
  294. if (chan->is_paused && ret == DMA_IN_PROGRESS)
  295. ret = DMA_PAUSED;
  296. return ret;
  297. }
  298. static void write_desc_llp(struct axi_dma_desc *desc, dma_addr_t adr)
  299. {
  300. desc->lli.llp = cpu_to_le64(adr);
  301. }
  302. static void write_chan_llp(struct axi_dma_chan *chan, dma_addr_t adr)
  303. {
  304. axi_chan_iowrite64(chan, CH_LLP, adr);
  305. }
  306. /* Called in chan locked context */
  307. static void axi_chan_block_xfer_start(struct axi_dma_chan *chan,
  308. struct axi_dma_desc *first)
  309. {
  310. struct dma_slave_config *sconfig = &chan->dma_sconfig;
  311. u32 priority = chan->chip->dw->hdata->priority[chan->id];
  312. u32 reg, irq_mask;
  313. u8 lms = 0; /* Select AXI0 master for LLI fetching */
  314. if (unlikely(axi_chan_is_hw_enable(chan))) {
  315. dev_err(chan2dev(chan), "%s is non-idle!\n",
  316. axi_chan_name(chan));
  317. return;
  318. }
  319. axi_dma_enable(chan->chip);
  320. reg = (chan->dws.dst_id << CH_CFG_L_DST_PER_POS |
  321. chan->dws.src_id << CH_CFG_L_SRC_PER_POS |
  322. DWAXIDMAC_MBLK_TYPE_LL << CH_CFG_L_DST_MULTBLK_TYPE_POS |
  323. DWAXIDMAC_MBLK_TYPE_LL << CH_CFG_L_SRC_MULTBLK_TYPE_POS);
  324. axi_chan_iowrite32(chan, CH_CFG_L, reg);
  325. reg = (DWAXIDMAC_TT_FC_MEM_TO_MEM_DMAC << CH_CFG_H_TT_FC_POS |
  326. priority << CH_CFG_H_PRIORITY_POS |
  327. DWAXIDMAC_HS_SEL_HW << CH_CFG_H_HS_SEL_DST_POS |
  328. DWAXIDMAC_HS_SEL_HW << CH_CFG_H_HS_SEL_SRC_POS);
  329. if (sconfig->direction == DMA_MEM_TO_DEV)
  330. reg |= sconfig->device_fc ? DWC_CFGH_FC(DWAXIDMAC_TT_FC_MEM_TO_PER_DST) :
  331. DWC_CFGH_FC(DWAXIDMAC_TT_FC_MEM_TO_PER_DMAC);
  332. else if (sconfig->direction == DMA_DEV_TO_MEM)
  333. reg |= sconfig->device_fc ? DWC_CFGH_FC(DWAXIDMAC_TT_FC_PER_TO_MEM_SRC) :
  334. DWC_CFGH_FC(DWAXIDMAC_TT_FC_PER_TO_MEM_DMAC);
  335. axi_chan_iowrite32(chan, CH_CFG_H, reg);
  336. write_chan_llp(chan, first->vd.tx.phys | lms);
  337. irq_mask = DWAXIDMAC_IRQ_DMA_TRF | DWAXIDMAC_IRQ_ALL_ERR;
  338. if (chan->cyclic)
  339. irq_mask |= DWAXIDMAC_IRQ_BLOCK_TRF;
  340. axi_chan_irq_sig_set(chan, irq_mask);
  341. /* Generate 'suspend' status but don't generate interrupt */
  342. irq_mask |= DWAXIDMAC_IRQ_SUSPENDED;
  343. axi_chan_irq_set(chan, irq_mask);
  344. axi_chan_enable(chan);
  345. }
  346. static void axi_chan_start_first_queued(struct axi_dma_chan *chan)
  347. {
  348. struct axi_dma_desc *desc;
  349. struct virt_dma_desc *vd;
  350. vd = vchan_next_desc(&chan->vc);
  351. if (!vd)
  352. return;
  353. desc = vd_to_axi_desc(vd);
  354. dev_vdbg(chan2dev(chan), "%s: started %u\n", axi_chan_name(chan),
  355. vd->tx.cookie);
  356. axi_chan_block_xfer_start(chan, desc);
  357. }
  358. static void dma_chan_issue_pending(struct dma_chan *dchan)
  359. {
  360. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  361. unsigned long flags;
  362. spin_lock_irqsave(&chan->vc.lock, flags);
  363. if (vchan_issue_pending(&chan->vc))
  364. axi_chan_start_first_queued(chan);
  365. spin_unlock_irqrestore(&chan->vc.lock, flags);
  366. }
  367. static int dma_chan_alloc_chan_resources(struct dma_chan *dchan)
  368. {
  369. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  370. /* ASSERT: channel is idle */
  371. if (axi_chan_is_hw_enable(chan)) {
  372. dev_err(chan2dev(chan), "%s is non-idle!\n",
  373. axi_chan_name(chan));
  374. return -EBUSY;
  375. }
  376. dev_vdbg(dchan2dev(dchan), "%s: allocating\n", axi_chan_name(chan));
  377. pm_runtime_get(chan->chip->dev);
  378. return 0;
  379. }
  380. static void dma_chan_free_chan_resources(struct dma_chan *dchan)
  381. {
  382. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  383. /* ASSERT: channel is idle */
  384. if (axi_chan_is_hw_enable(chan))
  385. dev_err(dchan2dev(dchan), "%s is non-idle!\n",
  386. axi_chan_name(chan));
  387. axi_chan_disable(chan);
  388. axi_chan_irq_disable(chan, DWAXIDMAC_IRQ_ALL);
  389. vchan_free_chan_resources(&chan->vc);
  390. dev_vdbg(dchan2dev(dchan),
  391. "%s: free resources, descriptor still allocated: %u\n",
  392. axi_chan_name(chan), atomic_read(&chan->descs_allocated));
  393. pm_runtime_put(chan->chip->dev);
  394. }
  395. /*
  396. * If DW_axi_dmac sees CHx_CTL.ShadowReg_Or_LLI_Last bit of the fetched LLI
  397. * as 1, it understands that the current block is the final block in the
  398. * transfer and completes the DMA transfer operation at the end of current
  399. * block transfer.
  400. */
  401. static void set_desc_last(struct axi_dma_desc *desc)
  402. {
  403. u32 val;
  404. write_desc_llp(desc, 0);
  405. val = le32_to_cpu(desc->lli.ctl_hi);
  406. val |= CH_CTL_H_LLI_LAST;
  407. desc->lli.ctl_hi = cpu_to_le32(val);
  408. }
  409. static void write_desc_sar(struct axi_dma_desc *desc, dma_addr_t adr)
  410. {
  411. desc->lli.sar = cpu_to_le64(adr);
  412. }
  413. static void write_desc_dar(struct axi_dma_desc *desc, dma_addr_t adr)
  414. {
  415. desc->lli.dar = cpu_to_le64(adr);
  416. }
  417. static void set_desc_src_master(struct axi_dma_desc *desc)
  418. {
  419. u32 val;
  420. /* Select AXI0 for source master */
  421. val = le32_to_cpu(desc->lli.ctl_lo);
  422. val &= ~CH_CTL_L_SRC_MAST;
  423. desc->lli.ctl_lo = cpu_to_le32(val);
  424. }
  425. static void set_desc_dest_master(struct axi_dma_desc *desc)
  426. {
  427. u32 val;
  428. /* Select AXI1 for source master if available */
  429. val = le32_to_cpu(desc->lli.ctl_lo);
  430. if (desc->chan->chip->dw->hdata->nr_masters > 1)
  431. val |= CH_CTL_L_DST_MAST;
  432. else
  433. val &= ~CH_CTL_L_DST_MAST;
  434. desc->lli.ctl_lo = cpu_to_le32(val);
  435. }
  436. static struct dma_async_tx_descriptor *
  437. dma_chan_prep_dma_memcpy(struct dma_chan *dchan, dma_addr_t dst_adr,
  438. dma_addr_t src_adr, size_t len, unsigned long flags)
  439. {
  440. struct axi_dma_desc *first = NULL, *desc = NULL, *prev = NULL;
  441. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  442. size_t block_ts, max_block_ts, xfer_len;
  443. u32 xfer_width, reg;
  444. u8 lms = 0; /* Select AXI0 master for LLI fetching */
  445. dev_dbg(chan2dev(chan), "%s: memcpy: src: %pad dst: %pad length: %zd flags: %#lx",
  446. axi_chan_name(chan), &src_adr, &dst_adr, len, flags);
  447. max_block_ts = chan->chip->dw->hdata->block_size[chan->id];
  448. while (len) {
  449. xfer_len = len;
  450. /*
  451. * Take care for the alignment.
  452. * Actually source and destination widths can be different, but
  453. * make them same to be simpler.
  454. */
  455. xfer_width = axi_chan_get_xfer_width(chan, src_adr, dst_adr, xfer_len);
  456. /*
  457. * block_ts indicates the total number of data of width
  458. * to be transferred in a DMA block transfer.
  459. * BLOCK_TS register should be set to block_ts - 1
  460. */
  461. block_ts = xfer_len >> xfer_width;
  462. if (block_ts > max_block_ts) {
  463. block_ts = max_block_ts;
  464. xfer_len = max_block_ts << xfer_width;
  465. }
  466. desc = axi_desc_get(chan);
  467. if (unlikely(!desc))
  468. goto err_desc_get;
  469. write_desc_sar(desc, src_adr);
  470. write_desc_dar(desc, dst_adr);
  471. desc->lli.block_ts_lo = cpu_to_le32(block_ts - 1);
  472. reg = CH_CTL_H_LLI_VALID;
  473. if (chan->chip->dw->hdata->restrict_axi_burst_len) {
  474. u32 burst_len = chan->chip->dw->hdata->axi_rw_burst_len;
  475. reg |= (CH_CTL_H_ARLEN_EN |
  476. burst_len << CH_CTL_H_ARLEN_POS |
  477. CH_CTL_H_AWLEN_EN |
  478. burst_len << CH_CTL_H_AWLEN_POS);
  479. }
  480. desc->lli.ctl_hi = cpu_to_le32(reg);
  481. reg = (DWAXIDMAC_BURST_TRANS_LEN_4 << CH_CTL_L_DST_MSIZE_POS |
  482. DWAXIDMAC_BURST_TRANS_LEN_4 << CH_CTL_L_SRC_MSIZE_POS |
  483. xfer_width << CH_CTL_L_DST_WIDTH_POS |
  484. xfer_width << CH_CTL_L_SRC_WIDTH_POS |
  485. DWAXIDMAC_CH_CTL_L_INC << CH_CTL_L_DST_INC_POS |
  486. DWAXIDMAC_CH_CTL_L_INC << CH_CTL_L_SRC_INC_POS);
  487. desc->lli.ctl_lo = cpu_to_le32(reg);
  488. set_desc_src_master(desc);
  489. set_desc_dest_master(desc);
  490. desc->len = xfer_len;
  491. /* Manage transfer list (xfer_list) */
  492. if (!first) {
  493. first = desc;
  494. } else {
  495. list_add_tail(&desc->xfer_list, &first->xfer_list);
  496. write_desc_llp(prev, desc->vd.tx.phys | lms);
  497. }
  498. prev = desc;
  499. /* update the length and addresses for the next loop cycle */
  500. len -= xfer_len;
  501. dst_adr += xfer_len;
  502. src_adr += xfer_len;
  503. }
  504. /* Total len of src/dest sg == 0, so no descriptor were allocated */
  505. if (unlikely(!first))
  506. return NULL;
  507. /* Set end-of-link to the last link descriptor of list */
  508. set_desc_last(desc);
  509. first->total_len = len;
  510. return vchan_tx_prep(&chan->vc, &first->vd, flags);
  511. err_desc_get:
  512. if (first)
  513. axi_desc_put(first);
  514. return NULL;
  515. }
  516. static struct dma_async_tx_descriptor *
  517. dma_chan_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
  518. unsigned int sg_len, enum dma_transfer_direction direction,
  519. unsigned long flags, void *context)
  520. {
  521. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  522. struct dma_slave_config *sconfig = &chan->dma_sconfig;
  523. struct axi_dma_desc *prev;
  524. struct axi_dma_desc *first;
  525. struct axi_dma_desc *desc;
  526. u32 ctllo, ctlhi;
  527. u8 m_master = chan->dws.m_master;
  528. u8 lms = DWC_LLP_LMS(m_master);
  529. dma_addr_t reg;
  530. unsigned int reg_width;
  531. unsigned int i;
  532. struct scatterlist *sg;
  533. size_t total_len = 0;
  534. size_t max_block_ts;
  535. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  536. if (unlikely(!is_slave_direction(direction) || !sg_len))
  537. return NULL;
  538. max_block_ts = chan->chip->dw->hdata->block_size[chan->id];
  539. chan->direction = direction;
  540. prev = first = NULL;
  541. ctlhi = CH_CTL_H_LLI_VALID;
  542. if (chan->chip->dw->hdata->restrict_axi_burst_len) {
  543. u32 burst_len = chan->chip->dw->hdata->axi_rw_burst_len;
  544. ctlhi |= (CH_CTL_H_ARLEN_EN |
  545. burst_len << CH_CTL_H_ARLEN_POS |
  546. CH_CTL_H_AWLEN_EN |
  547. burst_len << CH_CTL_H_AWLEN_POS);
  548. }
  549. switch (direction) {
  550. case DMA_MEM_TO_DEV:
  551. reg_width = __ffs(sconfig->dst_addr_width);
  552. reg = sconfig->dst_addr;
  553. ctllo = (DWC_DEFAULT_CTLLO(dchan)
  554. | DWC_CTLL_DST_WIDTH(reg_width)
  555. | DWC_CTLL_DST_FIX
  556. | DWC_CTLL_SRC_INC);
  557. for_each_sg(sgl, sg, sg_len, i) {
  558. u32 len, dlen, mem;
  559. mem = sg_dma_address(sg);
  560. len = sg_dma_len(sg);
  561. slave_sg_todev_fill_desc:
  562. desc = axi_desc_get(chan);
  563. if (!desc)
  564. goto err_desc_get;
  565. write_desc_sar(desc, mem);
  566. write_desc_dar(desc, reg);
  567. desc->lli.ctl_hi = cpu_to_le32(ctlhi);
  568. desc->lli.ctl_lo = cpu_to_le32(ctllo | DWC_CTLL_SRC_WIDTH(reg_width));
  569. if ((len >> reg_width) > max_block_ts) {
  570. dlen = max_block_ts << reg_width;
  571. mem += dlen;
  572. len -= dlen;
  573. } else {
  574. dlen = len;
  575. len = 0;
  576. }
  577. desc->lli.block_ts_lo = cpu_to_le32((dlen >> reg_width) - 1);
  578. desc->len = dlen;
  579. if (!first) {
  580. first = desc;
  581. } else {
  582. list_add_tail(&desc->xfer_list, &first->xfer_list);
  583. write_desc_llp(prev, desc->vd.tx.phys | lms);
  584. }
  585. prev = desc;
  586. total_len += dlen;
  587. if (len)
  588. goto slave_sg_todev_fill_desc;
  589. }
  590. break;
  591. case DMA_DEV_TO_MEM:
  592. reg_width = __ffs(sconfig->src_addr_width);
  593. reg = sconfig->src_addr;
  594. ctllo = (DWC_DEFAULT_CTLLO(dchan)
  595. | DWC_CTLL_SRC_WIDTH(reg_width)
  596. | DWC_CTLL_DST_INC
  597. | DWC_CTLL_SRC_FIX);
  598. for_each_sg(sgl, sg, sg_len, i) {
  599. u32 len, dlen, mem;
  600. mem = sg_dma_address(sg);
  601. len = sg_dma_len(sg);
  602. slave_sg_fromdev_fill_desc:
  603. desc = axi_desc_get(chan);
  604. if (!desc)
  605. goto err_desc_get;
  606. write_desc_sar(desc, reg);
  607. write_desc_dar(desc, mem);
  608. desc->lli.ctl_hi = cpu_to_le32(ctlhi);
  609. desc->lli.ctl_lo = cpu_to_le32(ctllo | DWC_CTLL_DST_WIDTH(reg_width));
  610. if ((len >> reg_width) > max_block_ts) {
  611. dlen = max_block_ts << reg_width;
  612. mem += dlen;
  613. len -= dlen;
  614. } else {
  615. dlen = len;
  616. len = 0;
  617. }
  618. desc->lli.block_ts_lo = cpu_to_le32((dlen >> reg_width) - 1);
  619. desc->len = dlen;
  620. if (!first) {
  621. first = desc;
  622. } else {
  623. list_add_tail(&desc->xfer_list, &first->xfer_list);
  624. write_desc_llp(prev, desc->vd.tx.phys | lms);
  625. }
  626. prev = desc;
  627. total_len += dlen;
  628. if (len)
  629. goto slave_sg_fromdev_fill_desc;
  630. }
  631. break;
  632. default:
  633. return NULL;
  634. }
  635. /* Total len of src/dest sg == 0, so no descriptor were allocated */
  636. if (unlikely(!first))
  637. return NULL;
  638. /* Set end-of-link to the last link descriptor of list */
  639. set_desc_last(desc);
  640. first->total_len = total_len;
  641. return vchan_tx_prep(&chan->vc, &first->vd, flags);
  642. err_desc_get:
  643. dev_err(chan2dev(chan),
  644. "not enough descriptors available. Direction %d\n", direction);
  645. axi_desc_put(first);
  646. return NULL;
  647. }
  648. static struct dma_async_tx_descriptor *dma_chan_prep_dma_cyclic(
  649. struct dma_chan *dchan, dma_addr_t buf_addr, size_t len,
  650. size_t period_len, enum dma_transfer_direction direction,
  651. unsigned long flags)
  652. {
  653. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  654. struct dma_slave_config *sconfig = &chan->dma_sconfig;
  655. struct axi_dma_desc *prev;
  656. struct axi_dma_desc *first;
  657. struct axi_dma_desc *desc;
  658. u8 m_master = chan->dws.m_master;
  659. u8 lms = DWC_LLP_LMS(m_master);
  660. unsigned int reg_width;
  661. size_t max_block_ts;
  662. u32 ctlhi;
  663. unsigned int periods;
  664. unsigned int i;
  665. chan->direction = direction;
  666. max_block_ts = chan->chip->dw->hdata->block_size[chan->id];
  667. if (direction == DMA_MEM_TO_DEV)
  668. reg_width = __ffs(sconfig->dst_addr_width);
  669. else
  670. reg_width = __ffs(sconfig->src_addr_width);
  671. periods = len / period_len;
  672. /* Check for too big/unaligned periods and unaligned DMA buffer. */
  673. if (period_len > (max_block_ts << reg_width))
  674. goto out_err;
  675. if (unlikely(period_len & ((1 << reg_width) - 1)))
  676. goto out_err;
  677. if (unlikely(buf_addr & ((1 << reg_width) - 1)))
  678. goto out_err;
  679. prev = first = NULL;
  680. ctlhi = CH_CTL_H_LLI_VALID | CH_CTL_H_IOC_BLKTFR_EN;
  681. if (chan->chip->dw->hdata->restrict_axi_burst_len) {
  682. u32 burst_len = chan->chip->dw->hdata->axi_rw_burst_len;
  683. ctlhi |= (CH_CTL_H_ARLEN_EN |
  684. burst_len << CH_CTL_H_ARLEN_POS |
  685. CH_CTL_H_AWLEN_EN |
  686. burst_len << CH_CTL_H_AWLEN_POS);
  687. }
  688. for (i = 0; i < periods; i++) {
  689. desc = axi_desc_get(chan);
  690. if (!desc)
  691. goto out_err_desc_get;
  692. switch (direction) {
  693. case DMA_MEM_TO_DEV:
  694. write_desc_sar(desc, buf_addr + period_len * i);
  695. write_desc_dar(desc, sconfig->dst_addr);
  696. desc->lli.ctl_lo = (DWC_DEFAULT_CTLLO(dchan)
  697. | DWC_CTLL_DST_WIDTH(reg_width)
  698. | DWC_CTLL_SRC_WIDTH(reg_width)
  699. | DWC_CTLL_DST_FIX
  700. | DWC_CTLL_SRC_INC);
  701. desc->lli.ctl_hi = cpu_to_le32(ctlhi);
  702. desc->lli.block_ts_lo = cpu_to_le32((period_len >> reg_width) - 1);
  703. break;
  704. case DMA_DEV_TO_MEM:
  705. write_desc_sar(desc, sconfig->src_addr);
  706. write_desc_dar(desc, buf_addr + period_len * i);
  707. desc->lli.ctl_lo = (DWC_DEFAULT_CTLLO(dchan)
  708. | DWC_CTLL_SRC_WIDTH(reg_width)
  709. | DWC_CTLL_DST_WIDTH(reg_width)
  710. | DWC_CTLL_DST_INC
  711. | DWC_CTLL_SRC_FIX);
  712. desc->lli.ctl_hi = cpu_to_le32(ctlhi);
  713. desc->lli.block_ts_lo = cpu_to_le32((period_len >> reg_width) - 1);
  714. break;
  715. default:
  716. break;
  717. }
  718. desc->len = period_len;
  719. if (!first) {
  720. first = desc;
  721. } else {
  722. list_add_tail(&desc->xfer_list, &first->xfer_list);
  723. write_desc_llp(prev, desc->vd.tx.phys | lms);
  724. }
  725. prev = desc;
  726. }
  727. first->total_len = len;
  728. /* Let's make a cyclic list */
  729. write_desc_llp(prev, first->vd.tx.phys | lms);
  730. dev_dbg(chan2dev(chan),
  731. "cyclic prepared buf %pad len %zu period %zu periods %d\n",
  732. &buf_addr, len, period_len, periods);
  733. chan->cyclic = true;
  734. return vchan_tx_prep(&chan->vc, &first->vd, flags);
  735. out_err_desc_get:
  736. axi_desc_put(first);
  737. out_err:
  738. return NULL;
  739. }
  740. bool dw_axi_dma_filter(struct dma_chan *dchan, void *param)
  741. {
  742. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  743. struct dw_axi_dma_slave *dws = param;
  744. if (dws->dma_dev != dchan->device->dev)
  745. return false;
  746. /* We have to copy data since dws can be temporary storage */
  747. memcpy(&chan->dws, dws, sizeof(struct dw_axi_dma_slave));
  748. return true;
  749. }
  750. EXPORT_SYMBOL_GPL(dw_axi_dma_filter);
  751. static void axi_chan_dump_lli(struct axi_dma_chan *chan,
  752. struct axi_dma_desc *desc)
  753. {
  754. dev_err(dchan2dev(&chan->vc.chan),
  755. "SAR: 0x%llx DAR: 0x%llx LLP: 0x%llx BTS 0x%x CTL: 0x%x:%08x",
  756. le64_to_cpu(desc->lli.sar),
  757. le64_to_cpu(desc->lli.dar),
  758. le64_to_cpu(desc->lli.llp),
  759. le32_to_cpu(desc->lli.block_ts_lo),
  760. le32_to_cpu(desc->lli.ctl_hi),
  761. le32_to_cpu(desc->lli.ctl_lo));
  762. }
  763. static void axi_chan_list_dump_lli(struct axi_dma_chan *chan,
  764. struct axi_dma_desc *desc_head)
  765. {
  766. struct axi_dma_desc *desc;
  767. axi_chan_dump_lli(chan, desc_head);
  768. list_for_each_entry(desc, &desc_head->xfer_list, xfer_list)
  769. axi_chan_dump_lli(chan, desc);
  770. }
  771. static noinline void axi_chan_handle_err(struct axi_dma_chan *chan, u32 status)
  772. {
  773. struct virt_dma_desc *vd;
  774. unsigned long flags;
  775. spin_lock_irqsave(&chan->vc.lock, flags);
  776. axi_chan_disable(chan);
  777. /* The bad descriptor currently is in the head of vc list */
  778. vd = vchan_next_desc(&chan->vc);
  779. /* Remove the completed descriptor from issued list */
  780. list_del(&vd->node);
  781. /* WARN about bad descriptor */
  782. dev_err(chan2dev(chan),
  783. "Bad descriptor submitted for %s, cookie: %d, irq: 0x%08x\n",
  784. axi_chan_name(chan), vd->tx.cookie, status);
  785. axi_chan_list_dump_lli(chan, vd_to_axi_desc(vd));
  786. vchan_cookie_complete(vd);
  787. /* Try to restart the controller */
  788. axi_chan_start_first_queued(chan);
  789. spin_unlock_irqrestore(&chan->vc.lock, flags);
  790. }
  791. static void axi_chan_block_xfer_complete(struct axi_dma_chan *chan)
  792. {
  793. struct virt_dma_desc *vd;
  794. unsigned long flags;
  795. spin_lock_irqsave(&chan->vc.lock, flags);
  796. /* The completed descriptor currently is in the head of vc list */
  797. vd = vchan_next_desc(&chan->vc);
  798. if (chan->cyclic) {
  799. struct axi_dma_desc *first, *desc, *cur_desc;
  800. u32 llplo, llphi;
  801. u64 llp;
  802. first = vd_to_axi_desc(vd);
  803. llplo = axi_chan_ioread32(chan, CH_LLP);
  804. llphi = axi_chan_ioread32(chan, CH_LLP + 4);
  805. llp = ((u64)llphi << 32) | llplo;
  806. //printk("llp=0x%llx, first->lli.llp=0x%llx.\n", llp, first->lli.llp);
  807. if (llp == DWC_LLP_LOC(first->lli.llp)) {
  808. cur_desc = list_prev_entry(first, xfer_list);
  809. cur_desc->lli.ctl_hi |= CH_CTL_H_LLI_VALID;
  810. } else {
  811. list_for_each_entry(desc, &first->xfer_list, xfer_list) {
  812. //printk("llp=0x%llx, desc->lli.llp=0x%llx.\n", llp, desc->lli.llp);
  813. if (llp == DWC_LLP_LOC(desc->lli.llp)) {
  814. cur_desc = list_prev_entry(desc, xfer_list);
  815. cur_desc->lli.ctl_hi |= CH_CTL_H_LLI_VALID;
  816. break;
  817. }
  818. }
  819. }
  820. vchan_cyclic_callback(vd);
  821. } else {
  822. if (unlikely(axi_chan_is_hw_enable(chan))) {
  823. dev_err(chan2dev(chan), "BUG: %s caught DWAXIDMAC_IRQ_DMA_TRF, but channel not idle!\n",
  824. axi_chan_name(chan));
  825. axi_chan_disable(chan);
  826. }
  827. /* Remove the completed descriptor from issued list before completing */
  828. list_del(&vd->node);
  829. vchan_cookie_complete(vd);
  830. /* Submit queued descriptors after processing the completed ones */
  831. axi_chan_start_first_queued(chan);
  832. }
  833. spin_unlock_irqrestore(&chan->vc.lock, flags);
  834. }
  835. static irqreturn_t dw_axi_dma_interrupt(int irq, void *dev_id)
  836. {
  837. struct axi_dma_chip *chip = dev_id;
  838. struct dw_axi_dma *dw = chip->dw;
  839. struct axi_dma_chan *chan;
  840. u32 status, i;
  841. /* Disable DMAC inerrupts. We'll enable them after processing chanels */
  842. axi_dma_irq_disable(chip);
  843. /* Poll, clear and process every chanel interrupt status */
  844. for (i = 0; i < dw->hdata->nr_channels; i++) {
  845. chan = &dw->chan[i];
  846. status = axi_chan_irq_read(chan);
  847. axi_chan_irq_clear(chan, status);
  848. //if (status) printk("ch %d status 0x%x.\n", i, status);
  849. dev_vdbg(chip->dev, "%s %u IRQ status: 0x%08x\n",
  850. axi_chan_name(chan), i, status);
  851. if (status & DWAXIDMAC_IRQ_ALL_ERR)
  852. axi_chan_handle_err(chan, status);
  853. else if (status & (DWAXIDMAC_IRQ_DMA_TRF | DWAXIDMAC_IRQ_BLOCK_TRF))
  854. axi_chan_block_xfer_complete(chan);
  855. }
  856. /* Re-enable interrupts */
  857. axi_dma_irq_enable(chip);
  858. return IRQ_HANDLED;
  859. }
  860. /*
  861. * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
  862. * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
  863. *
  864. * NOTE: burst size 2 is not supported by controller.
  865. *
  866. * This can be done by finding least significant bit set: n & (n - 1)
  867. */
  868. static inline void convert_burst(u32 *maxburst)
  869. {
  870. if (*maxburst > 1)
  871. *maxburst = fls(*maxburst) - 2;
  872. else
  873. *maxburst = 0;
  874. }
  875. static int dma_chan_config(struct dma_chan *dchan, struct dma_slave_config *sconfig)
  876. {
  877. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  878. /* Check if chan will be configured for slave transfers */
  879. if (!is_slave_direction(sconfig->direction))
  880. return -EINVAL;
  881. memcpy(&chan->dma_sconfig, sconfig, sizeof(*sconfig));
  882. chan->direction = sconfig->direction;
  883. if (chan->dma_sconfig.src_maxburst == 0)
  884. chan->dma_sconfig.src_maxburst = chan->dma_sconfig.dst_maxburst;
  885. if (chan->dma_sconfig.dst_maxburst == 0)
  886. chan->dma_sconfig.dst_maxburst = chan->dma_sconfig.src_maxburst;
  887. convert_burst(&chan->dma_sconfig.src_maxburst);
  888. convert_burst(&chan->dma_sconfig.dst_maxburst);
  889. return 0;
  890. }
  891. static int dma_chan_terminate_all(struct dma_chan *dchan)
  892. {
  893. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  894. u32 chan_active = BIT(chan->id) << DMAC_CHAN_EN_SHIFT;
  895. unsigned long flags;
  896. u32 val;
  897. int ret;
  898. LIST_HEAD(head);
  899. axi_chan_disable(chan);
  900. ret = readl_poll_timeout_atomic(chan->chip->regs + DMAC_CHEN, val,
  901. !(val & chan_active), 1000, 10000);
  902. if (ret == -ETIMEDOUT)
  903. dev_warn(dchan2dev(dchan),
  904. "%s failed to stop\n", axi_chan_name(chan));
  905. spin_lock_irqsave(&chan->vc.lock, flags);
  906. vchan_get_all_descriptors(&chan->vc, &head);
  907. chan->cyclic = false;
  908. spin_unlock_irqrestore(&chan->vc.lock, flags);
  909. vchan_dma_desc_free_list(&chan->vc, &head);
  910. dev_vdbg(dchan2dev(dchan), "terminated: %s\n", axi_chan_name(chan));
  911. return 0;
  912. }
  913. static int dma_chan_pause(struct dma_chan *dchan)
  914. {
  915. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  916. unsigned long flags;
  917. unsigned int timeout = 20; /* timeout iterations */
  918. u32 val;
  919. spin_lock_irqsave(&chan->vc.lock, flags);
  920. val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
  921. val |= BIT(chan->id) << DMAC_CHAN_SUSP_SHIFT |
  922. BIT(chan->id) << DMAC_CHAN_SUSP_WE_SHIFT;
  923. axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
  924. do {
  925. if (axi_chan_irq_read(chan) & DWAXIDMAC_IRQ_SUSPENDED)
  926. break;
  927. udelay(2);
  928. } while (--timeout);
  929. axi_chan_irq_clear(chan, DWAXIDMAC_IRQ_SUSPENDED);
  930. chan->is_paused = true;
  931. spin_unlock_irqrestore(&chan->vc.lock, flags);
  932. return timeout ? 0 : -EAGAIN;
  933. }
  934. /* Called in chan locked context */
  935. static inline void axi_chan_resume(struct axi_dma_chan *chan)
  936. {
  937. u32 val;
  938. val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
  939. val &= ~(BIT(chan->id) << DMAC_CHAN_SUSP_SHIFT);
  940. val |= (BIT(chan->id) << DMAC_CHAN_SUSP_WE_SHIFT);
  941. axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
  942. chan->is_paused = false;
  943. }
  944. static int dma_chan_resume(struct dma_chan *dchan)
  945. {
  946. struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
  947. unsigned long flags;
  948. spin_lock_irqsave(&chan->vc.lock, flags);
  949. if (chan->is_paused)
  950. axi_chan_resume(chan);
  951. spin_unlock_irqrestore(&chan->vc.lock, flags);
  952. return 0;
  953. }
  954. static int axi_dma_suspend(struct axi_dma_chip *chip)
  955. {
  956. axi_dma_irq_disable(chip);
  957. axi_dma_disable(chip);
  958. clk_disable_unprepare(chip->core_clk);
  959. clk_disable_unprepare(chip->cfgr_clk);
  960. return 0;
  961. }
  962. static int axi_dma_resume(struct axi_dma_chip *chip)
  963. {
  964. int ret;
  965. ret = clk_prepare_enable(chip->cfgr_clk);
  966. if (ret < 0)
  967. return ret;
  968. ret = clk_prepare_enable(chip->core_clk);
  969. if (ret < 0)
  970. return ret;
  971. axi_dma_enable(chip);
  972. axi_dma_irq_enable(chip);
  973. return 0;
  974. }
  975. static int __maybe_unused axi_dma_runtime_suspend(struct device *dev)
  976. {
  977. struct axi_dma_chip *chip = dev_get_drvdata(dev);
  978. return axi_dma_suspend(chip);
  979. }
  980. static int __maybe_unused axi_dma_runtime_resume(struct device *dev)
  981. {
  982. struct axi_dma_chip *chip = dev_get_drvdata(dev);
  983. return axi_dma_resume(chip);
  984. }
  985. static int parse_device_properties(struct axi_dma_chip *chip)
  986. {
  987. struct device *dev = chip->dev;
  988. u32 tmp, carr[DMAC_MAX_CHANNELS];
  989. int ret;
  990. ret = device_property_read_u32(dev, "dma-channels", &tmp);
  991. if (ret)
  992. return ret;
  993. if (tmp == 0 || tmp > DMAC_MAX_CHANNELS)
  994. return -EINVAL;
  995. chip->dw->hdata->nr_channels = tmp;
  996. ret = device_property_read_u32(dev, "snps,dma-masters", &tmp);
  997. if (ret)
  998. return ret;
  999. if (tmp == 0 || tmp > DMAC_MAX_MASTERS)
  1000. return -EINVAL;
  1001. chip->dw->hdata->nr_masters = tmp;
  1002. ret = device_property_read_u32(dev, "snps,data-width", &tmp);
  1003. if (ret)
  1004. return ret;
  1005. if (tmp > DWAXIDMAC_TRANS_WIDTH_MAX)
  1006. return -EINVAL;
  1007. chip->dw->hdata->m_data_width = tmp;
  1008. ret = device_property_read_u32_array(dev, "snps,block-size", carr,
  1009. chip->dw->hdata->nr_channels);
  1010. if (ret)
  1011. return ret;
  1012. for (tmp = 0; tmp < chip->dw->hdata->nr_channels; tmp++) {
  1013. if (carr[tmp] == 0 || carr[tmp] > DMAC_MAX_BLK_SIZE)
  1014. return -EINVAL;
  1015. chip->dw->hdata->block_size[tmp] = carr[tmp];
  1016. }
  1017. ret = device_property_read_u32_array(dev, "snps,priority", carr,
  1018. chip->dw->hdata->nr_channels);
  1019. if (ret)
  1020. return ret;
  1021. /* Priority value must be programmed within [0:nr_channels-1] range */
  1022. for (tmp = 0; tmp < chip->dw->hdata->nr_channels; tmp++) {
  1023. if (carr[tmp] >= chip->dw->hdata->nr_channels)
  1024. return -EINVAL;
  1025. chip->dw->hdata->priority[tmp] = carr[tmp];
  1026. }
  1027. /* axi-max-burst-len is optional property */
  1028. ret = device_property_read_u32(dev, "snps,axi-max-burst-len", &tmp);
  1029. if (!ret) {
  1030. if (tmp > DWAXIDMAC_ARWLEN_MAX + 1)
  1031. return -EINVAL;
  1032. if (tmp < DWAXIDMAC_ARWLEN_MIN + 1)
  1033. return -EINVAL;
  1034. chip->dw->hdata->restrict_axi_burst_len = true;
  1035. chip->dw->hdata->axi_rw_burst_len = tmp - 1;
  1036. }
  1037. return 0;
  1038. }
  1039. static struct dma_chan *dw_axi_dma_of_xlate(struct of_phandle_args *dma_spec,
  1040. struct of_dma *ofdma)
  1041. {
  1042. struct dw_axi_dma *dw = ofdma->of_dma_data;
  1043. struct dw_axi_dma_slave slave = {
  1044. .dma_dev = dw->dma.dev,
  1045. };
  1046. dma_cap_mask_t cap;
  1047. if (dma_spec->args_count != 3)
  1048. return NULL;
  1049. slave.src_id = dma_spec->args[0];
  1050. slave.dst_id = dma_spec->args[0];
  1051. slave.m_master = dma_spec->args[1];
  1052. slave.p_master = dma_spec->args[2];
  1053. if (WARN_ON(slave.src_id >= DMAC_MAX_NR_REQUESTS ||
  1054. slave.dst_id >= DMAC_MAX_NR_REQUESTS ||
  1055. slave.m_master >= dw->hdata->nr_masters ||
  1056. slave.p_master >= dw->hdata->nr_masters))
  1057. return NULL;
  1058. dma_cap_zero(cap);
  1059. dma_cap_set(DMA_SLAVE, cap);
  1060. /* TODO: there should be a simpler way to do this */
  1061. return dma_request_channel(cap, dw_axi_dma_filter, &slave);
  1062. }
  1063. static int dw_probe(struct platform_device *pdev)
  1064. {
  1065. struct axi_dma_chip *chip;
  1066. struct resource *mem;
  1067. struct dw_axi_dma *dw;
  1068. struct dw_axi_dma_hcfg *hdata;
  1069. u32 i;
  1070. int ret;
  1071. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  1072. if (!chip)
  1073. return -ENOMEM;
  1074. dw = devm_kzalloc(&pdev->dev, sizeof(*dw), GFP_KERNEL);
  1075. if (!dw)
  1076. return -ENOMEM;
  1077. hdata = devm_kzalloc(&pdev->dev, sizeof(*hdata), GFP_KERNEL);
  1078. if (!hdata)
  1079. return -ENOMEM;
  1080. chip->dw = dw;
  1081. chip->dev = &pdev->dev;
  1082. chip->dw->hdata = hdata;
  1083. chip->irq = platform_get_irq(pdev, 0);
  1084. if (chip->irq < 0)
  1085. return chip->irq;
  1086. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1087. chip->regs = devm_ioremap_resource(chip->dev, mem);
  1088. if (IS_ERR(chip->regs))
  1089. return PTR_ERR(chip->regs);
  1090. chip->core_clk = devm_clk_get(chip->dev, "core-clk");
  1091. if (IS_ERR(chip->core_clk))
  1092. return PTR_ERR(chip->core_clk);
  1093. chip->cfgr_clk = devm_clk_get(chip->dev, "cfgr-clk");
  1094. if (IS_ERR(chip->cfgr_clk))
  1095. return PTR_ERR(chip->cfgr_clk);
  1096. ret = parse_device_properties(chip);
  1097. if (ret)
  1098. return ret;
  1099. dw->chan = devm_kcalloc(chip->dev, hdata->nr_channels,
  1100. sizeof(*dw->chan), GFP_KERNEL);
  1101. if (!dw->chan)
  1102. return -ENOMEM;
  1103. ret = devm_request_irq(chip->dev, chip->irq, dw_axi_dma_interrupt,
  1104. IRQF_SHARED, KBUILD_MODNAME, chip);
  1105. if (ret)
  1106. return ret;
  1107. /* Lli address must be aligned to a 64-byte boundary */
  1108. dw->desc_pool = dmam_pool_create(KBUILD_MODNAME, chip->dev,
  1109. sizeof(struct axi_dma_desc), 64, 0);
  1110. if (!dw->desc_pool) {
  1111. dev_err(chip->dev, "No memory for descriptors dma pool\n");
  1112. return -ENOMEM;
  1113. }
  1114. INIT_LIST_HEAD(&dw->dma.channels);
  1115. for (i = 0; i < hdata->nr_channels; i++) {
  1116. struct axi_dma_chan *chan = &dw->chan[i];
  1117. chan->chip = chip;
  1118. chan->id = i;
  1119. chan->chan_regs = chip->regs + COMMON_REG_LEN + i * CHAN_REG_LEN;
  1120. atomic_set(&chan->descs_allocated, 0);
  1121. chan->vc.desc_free = vchan_desc_put;
  1122. vchan_init(&chan->vc, &dw->dma);
  1123. }
  1124. /* Set capabilities */
  1125. dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
  1126. dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
  1127. /* DMA capabilities */
  1128. dw->dma.chancnt = hdata->nr_channels;
  1129. dw->dma.src_addr_widths = AXI_DMA_BUSWIDTHS;
  1130. dw->dma.dst_addr_widths = AXI_DMA_BUSWIDTHS;
  1131. dw->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
  1132. BIT(DMA_MEM_TO_MEM);
  1133. dw->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;//DMA_RESIDUE_GRANULARITY_DESCRIPTOR;//DMA_RESIDUE_GRANULARITY_BURST;
  1134. dw->dma.dev = chip->dev;
  1135. dw->dma.device_config = dma_chan_config;
  1136. dw->dma.device_tx_status = dma_chan_tx_status;
  1137. dw->dma.device_issue_pending = dma_chan_issue_pending;
  1138. dw->dma.device_terminate_all = dma_chan_terminate_all;
  1139. dw->dma.device_pause = dma_chan_pause;
  1140. dw->dma.device_resume = dma_chan_resume;
  1141. dw->dma.device_alloc_chan_resources = dma_chan_alloc_chan_resources;
  1142. dw->dma.device_free_chan_resources = dma_chan_free_chan_resources;
  1143. dw->dma.device_prep_dma_memcpy = dma_chan_prep_dma_memcpy;
  1144. dw->dma.device_prep_slave_sg = dma_chan_prep_slave_sg;
  1145. dw->dma.device_prep_dma_cyclic = dma_chan_prep_dma_cyclic;
  1146. platform_set_drvdata(pdev, chip);
  1147. if (pdev->dev.of_node) {
  1148. ret = of_dma_controller_register(pdev->dev.of_node,
  1149. dw_axi_dma_of_xlate, dw);
  1150. if (ret)
  1151. dev_err(&pdev->dev,
  1152. "could not register of_dma_controller\n");
  1153. }
  1154. pm_runtime_enable(chip->dev);
  1155. /*
  1156. * We can't just call pm_runtime_get here instead of
  1157. * pm_runtime_get_noresume + axi_dma_resume because we need
  1158. * driver to work also without Runtime PM.
  1159. */
  1160. pm_runtime_get_noresume(chip->dev);
  1161. ret = axi_dma_resume(chip);
  1162. if (ret < 0)
  1163. goto err_pm_disable;
  1164. axi_dma_hw_init(chip);
  1165. pm_runtime_put(chip->dev);
  1166. ret = dmaenginem_async_device_register(&dw->dma);
  1167. if (ret)
  1168. goto err_pm_disable;
  1169. dev_info(chip->dev, "DesignWare AXI DMA Controller, %d channels\n",
  1170. dw->hdata->nr_channels);
  1171. return 0;
  1172. err_pm_disable:
  1173. pm_runtime_disable(chip->dev);
  1174. return ret;
  1175. }
  1176. static int dw_remove(struct platform_device *pdev)
  1177. {
  1178. struct axi_dma_chip *chip = platform_get_drvdata(pdev);
  1179. struct dw_axi_dma *dw = chip->dw;
  1180. struct axi_dma_chan *chan, *_chan;
  1181. u32 i;
  1182. /* Enable clk before accessing to registers */
  1183. clk_prepare_enable(chip->cfgr_clk);
  1184. clk_prepare_enable(chip->core_clk);
  1185. axi_dma_irq_disable(chip);
  1186. for (i = 0; i < dw->hdata->nr_channels; i++) {
  1187. axi_chan_disable(&chip->dw->chan[i]);
  1188. axi_chan_irq_disable(&chip->dw->chan[i], DWAXIDMAC_IRQ_ALL);
  1189. }
  1190. axi_dma_disable(chip);
  1191. pm_runtime_disable(chip->dev);
  1192. axi_dma_suspend(chip);
  1193. devm_free_irq(chip->dev, chip->irq, chip);
  1194. list_for_each_entry_safe(chan, _chan, &dw->dma.channels,
  1195. vc.chan.device_node) {
  1196. list_del(&chan->vc.chan.device_node);
  1197. tasklet_kill(&chan->vc.task);
  1198. }
  1199. return 0;
  1200. }
  1201. static const struct dev_pm_ops dw_axi_dma_pm_ops = {
  1202. SET_RUNTIME_PM_OPS(axi_dma_runtime_suspend, axi_dma_runtime_resume, NULL)
  1203. };
  1204. static const struct of_device_id dw_dma_of_id_table[] = {
  1205. { .compatible = "snps,axi-dma-1.01a" },
  1206. {}
  1207. };
  1208. MODULE_DEVICE_TABLE(of, dw_dma_of_id_table);
  1209. static struct platform_driver dw_driver = {
  1210. .probe = dw_probe,
  1211. .remove = dw_remove,
  1212. .driver = {
  1213. .name = KBUILD_MODNAME,
  1214. .of_match_table = of_match_ptr(dw_dma_of_id_table),
  1215. .pm = &dw_axi_dma_pm_ops,
  1216. },
  1217. };
  1218. module_platform_driver(dw_driver);
  1219. MODULE_LICENSE("GPL v2");
  1220. MODULE_DESCRIPTION("Synopsys DesignWare AXI DMA Controller platform driver");
  1221. MODULE_AUTHOR("Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>");