dw-axi-dmac-platform.c 40 KB

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