ark-axi-dma.c 42 KB

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