mxs-dma.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright 2011 Freescale Semiconductor, Inc. All Rights Reserved.
  4. //
  5. // Refer to drivers/dma/imx-sdma.c
  6. #include <linux/init.h>
  7. #include <linux/types.h>
  8. #include <linux/mm.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/clk.h>
  11. #include <linux/wait.h>
  12. #include <linux/sched.h>
  13. #include <linux/semaphore.h>
  14. #include <linux/device.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/slab.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/dmaengine.h>
  19. #include <linux/delay.h>
  20. #include <linux/module.h>
  21. #include <linux/stmp_device.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_dma.h>
  25. #include <linux/list.h>
  26. #include <linux/dma/mxs-dma.h>
  27. #include <asm/irq.h>
  28. #include "dmaengine.h"
  29. /*
  30. * NOTE: The term "PIO" throughout the mxs-dma implementation means
  31. * PIO mode of mxs apbh-dma and apbx-dma. With this working mode,
  32. * dma can program the controller registers of peripheral devices.
  33. */
  34. #define dma_is_apbh(mxs_dma) ((mxs_dma)->type == MXS_DMA_APBH)
  35. #define apbh_is_old(mxs_dma) ((mxs_dma)->dev_id == IMX23_DMA)
  36. #define HW_APBHX_CTRL0 0x000
  37. #define BM_APBH_CTRL0_APB_BURST8_EN (1 << 29)
  38. #define BM_APBH_CTRL0_APB_BURST_EN (1 << 28)
  39. #define BP_APBH_CTRL0_RESET_CHANNEL 16
  40. #define HW_APBHX_CTRL1 0x010
  41. #define HW_APBHX_CTRL2 0x020
  42. #define HW_APBHX_CHANNEL_CTRL 0x030
  43. #define BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL 16
  44. /*
  45. * The offset of NXTCMDAR register is different per both dma type and version,
  46. * while stride for each channel is all the same 0x70.
  47. */
  48. #define HW_APBHX_CHn_NXTCMDAR(d, n) \
  49. (((dma_is_apbh(d) && apbh_is_old(d)) ? 0x050 : 0x110) + (n) * 0x70)
  50. #define HW_APBHX_CHn_SEMA(d, n) \
  51. (((dma_is_apbh(d) && apbh_is_old(d)) ? 0x080 : 0x140) + (n) * 0x70)
  52. #define HW_APBHX_CHn_BAR(d, n) \
  53. (((dma_is_apbh(d) && apbh_is_old(d)) ? 0x070 : 0x130) + (n) * 0x70)
  54. #define HW_APBX_CHn_DEBUG1(d, n) (0x150 + (n) * 0x70)
  55. /*
  56. * ccw bits definitions
  57. *
  58. * COMMAND: 0..1 (2)
  59. * CHAIN: 2 (1)
  60. * IRQ: 3 (1)
  61. * NAND_LOCK: 4 (1) - not implemented
  62. * NAND_WAIT4READY: 5 (1) - not implemented
  63. * DEC_SEM: 6 (1)
  64. * WAIT4END: 7 (1)
  65. * HALT_ON_TERMINATE: 8 (1)
  66. * TERMINATE_FLUSH: 9 (1)
  67. * RESERVED: 10..11 (2)
  68. * PIO_NUM: 12..15 (4)
  69. */
  70. #define BP_CCW_COMMAND 0
  71. #define BM_CCW_COMMAND (3 << 0)
  72. #define CCW_CHAIN (1 << 2)
  73. #define CCW_IRQ (1 << 3)
  74. #define CCW_WAIT4RDY (1 << 5)
  75. #define CCW_DEC_SEM (1 << 6)
  76. #define CCW_WAIT4END (1 << 7)
  77. #define CCW_HALT_ON_TERM (1 << 8)
  78. #define CCW_TERM_FLUSH (1 << 9)
  79. #define BP_CCW_PIO_NUM 12
  80. #define BM_CCW_PIO_NUM (0xf << 12)
  81. #define BF_CCW(value, field) (((value) << BP_CCW_##field) & BM_CCW_##field)
  82. #define MXS_DMA_CMD_NO_XFER 0
  83. #define MXS_DMA_CMD_WRITE 1
  84. #define MXS_DMA_CMD_READ 2
  85. #define MXS_DMA_CMD_DMA_SENSE 3 /* not implemented */
  86. struct mxs_dma_ccw {
  87. u32 next;
  88. u16 bits;
  89. u16 xfer_bytes;
  90. #define MAX_XFER_BYTES 0xff00
  91. u32 bufaddr;
  92. #define MXS_PIO_WORDS 16
  93. u32 pio_words[MXS_PIO_WORDS];
  94. };
  95. #define CCW_BLOCK_SIZE (4 * PAGE_SIZE)
  96. #define NUM_CCW (int)(CCW_BLOCK_SIZE / sizeof(struct mxs_dma_ccw))
  97. struct mxs_dma_chan {
  98. struct mxs_dma_engine *mxs_dma;
  99. struct dma_chan chan;
  100. struct dma_async_tx_descriptor desc;
  101. struct tasklet_struct tasklet;
  102. unsigned int chan_irq;
  103. struct mxs_dma_ccw *ccw;
  104. dma_addr_t ccw_phys;
  105. int desc_count;
  106. enum dma_status status;
  107. unsigned int flags;
  108. bool reset;
  109. #define MXS_DMA_SG_LOOP (1 << 0)
  110. #define MXS_DMA_USE_SEMAPHORE (1 << 1)
  111. };
  112. #define MXS_DMA_CHANNELS 16
  113. #define MXS_DMA_CHANNELS_MASK 0xffff
  114. enum mxs_dma_devtype {
  115. MXS_DMA_APBH,
  116. MXS_DMA_APBX,
  117. };
  118. enum mxs_dma_id {
  119. IMX23_DMA,
  120. IMX28_DMA,
  121. };
  122. struct mxs_dma_engine {
  123. enum mxs_dma_id dev_id;
  124. enum mxs_dma_devtype type;
  125. void __iomem *base;
  126. struct clk *clk;
  127. struct dma_device dma_device;
  128. struct mxs_dma_chan mxs_chans[MXS_DMA_CHANNELS];
  129. struct platform_device *pdev;
  130. unsigned int nr_channels;
  131. };
  132. struct mxs_dma_type {
  133. enum mxs_dma_id id;
  134. enum mxs_dma_devtype type;
  135. };
  136. static struct mxs_dma_type mxs_dma_types[] = {
  137. {
  138. .id = IMX23_DMA,
  139. .type = MXS_DMA_APBH,
  140. }, {
  141. .id = IMX23_DMA,
  142. .type = MXS_DMA_APBX,
  143. }, {
  144. .id = IMX28_DMA,
  145. .type = MXS_DMA_APBH,
  146. }, {
  147. .id = IMX28_DMA,
  148. .type = MXS_DMA_APBX,
  149. }
  150. };
  151. static const struct of_device_id mxs_dma_dt_ids[] = {
  152. { .compatible = "fsl,imx23-dma-apbh", .data = &mxs_dma_types[0], },
  153. { .compatible = "fsl,imx23-dma-apbx", .data = &mxs_dma_types[1], },
  154. { .compatible = "fsl,imx28-dma-apbh", .data = &mxs_dma_types[2], },
  155. { .compatible = "fsl,imx28-dma-apbx", .data = &mxs_dma_types[3], },
  156. { /* sentinel */ }
  157. };
  158. MODULE_DEVICE_TABLE(of, mxs_dma_dt_ids);
  159. static struct mxs_dma_chan *to_mxs_dma_chan(struct dma_chan *chan)
  160. {
  161. return container_of(chan, struct mxs_dma_chan, chan);
  162. }
  163. static void mxs_dma_reset_chan(struct dma_chan *chan)
  164. {
  165. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  166. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  167. int chan_id = mxs_chan->chan.chan_id;
  168. /*
  169. * mxs dma channel resets can cause a channel stall. To recover from a
  170. * channel stall, we have to reset the whole DMA engine. To avoid this,
  171. * we use cyclic DMA with semaphores, that are enhanced in
  172. * mxs_dma_int_handler. To reset the channel, we can simply stop writing
  173. * into the semaphore counter.
  174. */
  175. if (mxs_chan->flags & MXS_DMA_USE_SEMAPHORE &&
  176. mxs_chan->flags & MXS_DMA_SG_LOOP) {
  177. mxs_chan->reset = true;
  178. } else if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma)) {
  179. writel(1 << (chan_id + BP_APBH_CTRL0_RESET_CHANNEL),
  180. mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
  181. } else {
  182. unsigned long elapsed = 0;
  183. const unsigned long max_wait = 50000; /* 50ms */
  184. void __iomem *reg_dbg1 = mxs_dma->base +
  185. HW_APBX_CHn_DEBUG1(mxs_dma, chan_id);
  186. /*
  187. * On i.MX28 APBX, the DMA channel can stop working if we reset
  188. * the channel while it is in READ_FLUSH (0x08) state.
  189. * We wait here until we leave the state. Then we trigger the
  190. * reset. Waiting a maximum of 50ms, the kernel shouldn't crash
  191. * because of this.
  192. */
  193. while ((readl(reg_dbg1) & 0xf) == 0x8 && elapsed < max_wait) {
  194. udelay(100);
  195. elapsed += 100;
  196. }
  197. if (elapsed >= max_wait)
  198. dev_err(&mxs_chan->mxs_dma->pdev->dev,
  199. "Failed waiting for the DMA channel %d to leave state READ_FLUSH, trying to reset channel in READ_FLUSH state now\n",
  200. chan_id);
  201. writel(1 << (chan_id + BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL),
  202. mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
  203. }
  204. mxs_chan->status = DMA_COMPLETE;
  205. }
  206. static void mxs_dma_enable_chan(struct dma_chan *chan)
  207. {
  208. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  209. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  210. int chan_id = mxs_chan->chan.chan_id;
  211. /* set cmd_addr up */
  212. writel(mxs_chan->ccw_phys,
  213. mxs_dma->base + HW_APBHX_CHn_NXTCMDAR(mxs_dma, chan_id));
  214. /* write 1 to SEMA to kick off the channel */
  215. if (mxs_chan->flags & MXS_DMA_USE_SEMAPHORE &&
  216. mxs_chan->flags & MXS_DMA_SG_LOOP) {
  217. /* A cyclic DMA consists of at least 2 segments, so initialize
  218. * the semaphore with 2 so we have enough time to add 1 to the
  219. * semaphore if we need to */
  220. writel(2, mxs_dma->base + HW_APBHX_CHn_SEMA(mxs_dma, chan_id));
  221. } else {
  222. writel(1, mxs_dma->base + HW_APBHX_CHn_SEMA(mxs_dma, chan_id));
  223. }
  224. mxs_chan->reset = false;
  225. }
  226. static void mxs_dma_disable_chan(struct dma_chan *chan)
  227. {
  228. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  229. mxs_chan->status = DMA_COMPLETE;
  230. }
  231. static int mxs_dma_pause_chan(struct dma_chan *chan)
  232. {
  233. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  234. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  235. int chan_id = mxs_chan->chan.chan_id;
  236. /* freeze the channel */
  237. if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
  238. writel(1 << chan_id,
  239. mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
  240. else
  241. writel(1 << chan_id,
  242. mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
  243. mxs_chan->status = DMA_PAUSED;
  244. return 0;
  245. }
  246. static int mxs_dma_resume_chan(struct dma_chan *chan)
  247. {
  248. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  249. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  250. int chan_id = mxs_chan->chan.chan_id;
  251. /* unfreeze the channel */
  252. if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
  253. writel(1 << chan_id,
  254. mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_CLR);
  255. else
  256. writel(1 << chan_id,
  257. mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_CLR);
  258. mxs_chan->status = DMA_IN_PROGRESS;
  259. return 0;
  260. }
  261. static dma_cookie_t mxs_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  262. {
  263. return dma_cookie_assign(tx);
  264. }
  265. static void mxs_dma_tasklet(struct tasklet_struct *t)
  266. {
  267. struct mxs_dma_chan *mxs_chan = from_tasklet(mxs_chan, t, tasklet);
  268. dmaengine_desc_get_callback_invoke(&mxs_chan->desc, NULL);
  269. }
  270. static int mxs_dma_irq_to_chan(struct mxs_dma_engine *mxs_dma, int irq)
  271. {
  272. int i;
  273. for (i = 0; i != mxs_dma->nr_channels; ++i)
  274. if (mxs_dma->mxs_chans[i].chan_irq == irq)
  275. return i;
  276. return -EINVAL;
  277. }
  278. static irqreturn_t mxs_dma_int_handler(int irq, void *dev_id)
  279. {
  280. struct mxs_dma_engine *mxs_dma = dev_id;
  281. struct mxs_dma_chan *mxs_chan;
  282. u32 completed;
  283. u32 err;
  284. int chan = mxs_dma_irq_to_chan(mxs_dma, irq);
  285. if (chan < 0)
  286. return IRQ_NONE;
  287. /* completion status */
  288. completed = readl(mxs_dma->base + HW_APBHX_CTRL1);
  289. completed = (completed >> chan) & 0x1;
  290. /* Clear interrupt */
  291. writel((1 << chan),
  292. mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_CLR);
  293. /* error status */
  294. err = readl(mxs_dma->base + HW_APBHX_CTRL2);
  295. err &= (1 << (MXS_DMA_CHANNELS + chan)) | (1 << chan);
  296. /*
  297. * error status bit is in the upper 16 bits, error irq bit in the lower
  298. * 16 bits. We transform it into a simpler error code:
  299. * err: 0x00 = no error, 0x01 = TERMINATION, 0x02 = BUS_ERROR
  300. */
  301. err = (err >> (MXS_DMA_CHANNELS + chan)) + (err >> chan);
  302. /* Clear error irq */
  303. writel((1 << chan),
  304. mxs_dma->base + HW_APBHX_CTRL2 + STMP_OFFSET_REG_CLR);
  305. /*
  306. * When both completion and error of termination bits set at the
  307. * same time, we do not take it as an error. IOW, it only becomes
  308. * an error we need to handle here in case of either it's a bus
  309. * error or a termination error with no completion. 0x01 is termination
  310. * error, so we can subtract err & completed to get the real error case.
  311. */
  312. err -= err & completed;
  313. mxs_chan = &mxs_dma->mxs_chans[chan];
  314. if (err) {
  315. dev_dbg(mxs_dma->dma_device.dev,
  316. "%s: error in channel %d\n", __func__,
  317. chan);
  318. mxs_chan->status = DMA_ERROR;
  319. mxs_dma_reset_chan(&mxs_chan->chan);
  320. } else if (mxs_chan->status != DMA_COMPLETE) {
  321. if (mxs_chan->flags & MXS_DMA_SG_LOOP) {
  322. mxs_chan->status = DMA_IN_PROGRESS;
  323. if (mxs_chan->flags & MXS_DMA_USE_SEMAPHORE)
  324. writel(1, mxs_dma->base +
  325. HW_APBHX_CHn_SEMA(mxs_dma, chan));
  326. } else {
  327. mxs_chan->status = DMA_COMPLETE;
  328. }
  329. }
  330. if (mxs_chan->status == DMA_COMPLETE) {
  331. if (mxs_chan->reset)
  332. return IRQ_HANDLED;
  333. dma_cookie_complete(&mxs_chan->desc);
  334. }
  335. /* schedule tasklet on this channel */
  336. tasklet_schedule(&mxs_chan->tasklet);
  337. return IRQ_HANDLED;
  338. }
  339. static int mxs_dma_alloc_chan_resources(struct dma_chan *chan)
  340. {
  341. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  342. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  343. int ret;
  344. mxs_chan->ccw = dma_alloc_coherent(mxs_dma->dma_device.dev,
  345. CCW_BLOCK_SIZE,
  346. &mxs_chan->ccw_phys, GFP_KERNEL);
  347. if (!mxs_chan->ccw) {
  348. ret = -ENOMEM;
  349. goto err_alloc;
  350. }
  351. ret = request_irq(mxs_chan->chan_irq, mxs_dma_int_handler,
  352. 0, "mxs-dma", mxs_dma);
  353. if (ret)
  354. goto err_irq;
  355. ret = clk_prepare_enable(mxs_dma->clk);
  356. if (ret)
  357. goto err_clk;
  358. mxs_dma_reset_chan(chan);
  359. dma_async_tx_descriptor_init(&mxs_chan->desc, chan);
  360. mxs_chan->desc.tx_submit = mxs_dma_tx_submit;
  361. /* the descriptor is ready */
  362. async_tx_ack(&mxs_chan->desc);
  363. return 0;
  364. err_clk:
  365. free_irq(mxs_chan->chan_irq, mxs_dma);
  366. err_irq:
  367. dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE,
  368. mxs_chan->ccw, mxs_chan->ccw_phys);
  369. err_alloc:
  370. return ret;
  371. }
  372. static void mxs_dma_free_chan_resources(struct dma_chan *chan)
  373. {
  374. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  375. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  376. mxs_dma_disable_chan(chan);
  377. free_irq(mxs_chan->chan_irq, mxs_dma);
  378. dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE,
  379. mxs_chan->ccw, mxs_chan->ccw_phys);
  380. clk_disable_unprepare(mxs_dma->clk);
  381. }
  382. /*
  383. * How to use the flags for ->device_prep_slave_sg() :
  384. * [1] If there is only one DMA command in the DMA chain, the code should be:
  385. * ......
  386. * ->device_prep_slave_sg(DMA_CTRL_ACK);
  387. * ......
  388. * [2] If there are two DMA commands in the DMA chain, the code should be
  389. * ......
  390. * ->device_prep_slave_sg(0);
  391. * ......
  392. * ->device_prep_slave_sg(DMA_CTRL_ACK);
  393. * ......
  394. * [3] If there are more than two DMA commands in the DMA chain, the code
  395. * should be:
  396. * ......
  397. * ->device_prep_slave_sg(0); // First
  398. * ......
  399. * ->device_prep_slave_sg(DMA_CTRL_ACK]);
  400. * ......
  401. * ->device_prep_slave_sg(DMA_CTRL_ACK); // Last
  402. * ......
  403. */
  404. static struct dma_async_tx_descriptor *mxs_dma_prep_slave_sg(
  405. struct dma_chan *chan, struct scatterlist *sgl,
  406. unsigned int sg_len, enum dma_transfer_direction direction,
  407. unsigned long flags, void *context)
  408. {
  409. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  410. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  411. struct mxs_dma_ccw *ccw;
  412. struct scatterlist *sg;
  413. u32 i, j;
  414. u32 *pio;
  415. int idx = 0;
  416. if (mxs_chan->status == DMA_IN_PROGRESS)
  417. idx = mxs_chan->desc_count;
  418. if (sg_len + idx > NUM_CCW) {
  419. dev_err(mxs_dma->dma_device.dev,
  420. "maximum number of sg exceeded: %d > %d\n",
  421. sg_len, NUM_CCW);
  422. goto err_out;
  423. }
  424. mxs_chan->status = DMA_IN_PROGRESS;
  425. mxs_chan->flags = 0;
  426. /*
  427. * If the sg is prepared with append flag set, the sg
  428. * will be appended to the last prepared sg.
  429. */
  430. if (idx) {
  431. BUG_ON(idx < 1);
  432. ccw = &mxs_chan->ccw[idx - 1];
  433. ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
  434. ccw->bits |= CCW_CHAIN;
  435. ccw->bits &= ~CCW_IRQ;
  436. ccw->bits &= ~CCW_DEC_SEM;
  437. } else {
  438. idx = 0;
  439. }
  440. if (direction == DMA_TRANS_NONE) {
  441. ccw = &mxs_chan->ccw[idx++];
  442. pio = (u32 *) sgl;
  443. for (j = 0; j < sg_len;)
  444. ccw->pio_words[j++] = *pio++;
  445. ccw->bits = 0;
  446. ccw->bits |= CCW_IRQ;
  447. ccw->bits |= CCW_DEC_SEM;
  448. if (flags & MXS_DMA_CTRL_WAIT4END)
  449. ccw->bits |= CCW_WAIT4END;
  450. ccw->bits |= CCW_HALT_ON_TERM;
  451. ccw->bits |= CCW_TERM_FLUSH;
  452. ccw->bits |= BF_CCW(sg_len, PIO_NUM);
  453. ccw->bits |= BF_CCW(MXS_DMA_CMD_NO_XFER, COMMAND);
  454. if (flags & MXS_DMA_CTRL_WAIT4RDY)
  455. ccw->bits |= CCW_WAIT4RDY;
  456. } else {
  457. for_each_sg(sgl, sg, sg_len, i) {
  458. if (sg_dma_len(sg) > MAX_XFER_BYTES) {
  459. dev_err(mxs_dma->dma_device.dev, "maximum bytes for sg entry exceeded: %d > %d\n",
  460. sg_dma_len(sg), MAX_XFER_BYTES);
  461. goto err_out;
  462. }
  463. ccw = &mxs_chan->ccw[idx++];
  464. ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
  465. ccw->bufaddr = sg->dma_address;
  466. ccw->xfer_bytes = sg_dma_len(sg);
  467. ccw->bits = 0;
  468. ccw->bits |= CCW_CHAIN;
  469. ccw->bits |= CCW_HALT_ON_TERM;
  470. ccw->bits |= CCW_TERM_FLUSH;
  471. ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
  472. MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ,
  473. COMMAND);
  474. if (i + 1 == sg_len) {
  475. ccw->bits &= ~CCW_CHAIN;
  476. ccw->bits |= CCW_IRQ;
  477. ccw->bits |= CCW_DEC_SEM;
  478. if (flags & MXS_DMA_CTRL_WAIT4END)
  479. ccw->bits |= CCW_WAIT4END;
  480. }
  481. }
  482. }
  483. mxs_chan->desc_count = idx;
  484. return &mxs_chan->desc;
  485. err_out:
  486. mxs_chan->status = DMA_ERROR;
  487. return NULL;
  488. }
  489. static struct dma_async_tx_descriptor *mxs_dma_prep_dma_cyclic(
  490. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  491. size_t period_len, enum dma_transfer_direction direction,
  492. unsigned long flags)
  493. {
  494. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  495. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  496. u32 num_periods = buf_len / period_len;
  497. u32 i = 0, buf = 0;
  498. if (mxs_chan->status == DMA_IN_PROGRESS)
  499. return NULL;
  500. mxs_chan->status = DMA_IN_PROGRESS;
  501. mxs_chan->flags |= MXS_DMA_SG_LOOP;
  502. mxs_chan->flags |= MXS_DMA_USE_SEMAPHORE;
  503. if (num_periods > NUM_CCW) {
  504. dev_err(mxs_dma->dma_device.dev,
  505. "maximum number of sg exceeded: %d > %d\n",
  506. num_periods, NUM_CCW);
  507. goto err_out;
  508. }
  509. if (period_len > MAX_XFER_BYTES) {
  510. dev_err(mxs_dma->dma_device.dev,
  511. "maximum period size exceeded: %zu > %d\n",
  512. period_len, MAX_XFER_BYTES);
  513. goto err_out;
  514. }
  515. while (buf < buf_len) {
  516. struct mxs_dma_ccw *ccw = &mxs_chan->ccw[i];
  517. if (i + 1 == num_periods)
  518. ccw->next = mxs_chan->ccw_phys;
  519. else
  520. ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * (i + 1);
  521. ccw->bufaddr = dma_addr;
  522. ccw->xfer_bytes = period_len;
  523. ccw->bits = 0;
  524. ccw->bits |= CCW_CHAIN;
  525. ccw->bits |= CCW_IRQ;
  526. ccw->bits |= CCW_HALT_ON_TERM;
  527. ccw->bits |= CCW_TERM_FLUSH;
  528. ccw->bits |= CCW_DEC_SEM;
  529. ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
  530. MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ, COMMAND);
  531. dma_addr += period_len;
  532. buf += period_len;
  533. i++;
  534. }
  535. mxs_chan->desc_count = i;
  536. return &mxs_chan->desc;
  537. err_out:
  538. mxs_chan->status = DMA_ERROR;
  539. return NULL;
  540. }
  541. static int mxs_dma_terminate_all(struct dma_chan *chan)
  542. {
  543. mxs_dma_reset_chan(chan);
  544. mxs_dma_disable_chan(chan);
  545. return 0;
  546. }
  547. static enum dma_status mxs_dma_tx_status(struct dma_chan *chan,
  548. dma_cookie_t cookie, struct dma_tx_state *txstate)
  549. {
  550. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  551. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  552. u32 residue = 0;
  553. if (mxs_chan->status == DMA_IN_PROGRESS &&
  554. mxs_chan->flags & MXS_DMA_SG_LOOP) {
  555. struct mxs_dma_ccw *last_ccw;
  556. u32 bar;
  557. last_ccw = &mxs_chan->ccw[mxs_chan->desc_count - 1];
  558. residue = last_ccw->xfer_bytes + last_ccw->bufaddr;
  559. bar = readl(mxs_dma->base +
  560. HW_APBHX_CHn_BAR(mxs_dma, chan->chan_id));
  561. residue -= bar;
  562. }
  563. dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
  564. residue);
  565. return mxs_chan->status;
  566. }
  567. static int mxs_dma_init(struct mxs_dma_engine *mxs_dma)
  568. {
  569. int ret;
  570. ret = clk_prepare_enable(mxs_dma->clk);
  571. if (ret)
  572. return ret;
  573. ret = stmp_reset_block(mxs_dma->base);
  574. if (ret)
  575. goto err_out;
  576. /* enable apbh burst */
  577. if (dma_is_apbh(mxs_dma)) {
  578. writel(BM_APBH_CTRL0_APB_BURST_EN,
  579. mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
  580. writel(BM_APBH_CTRL0_APB_BURST8_EN,
  581. mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
  582. }
  583. /* enable irq for all the channels */
  584. writel(MXS_DMA_CHANNELS_MASK << MXS_DMA_CHANNELS,
  585. mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_SET);
  586. err_out:
  587. clk_disable_unprepare(mxs_dma->clk);
  588. return ret;
  589. }
  590. struct mxs_dma_filter_param {
  591. unsigned int chan_id;
  592. };
  593. static bool mxs_dma_filter_fn(struct dma_chan *chan, void *fn_param)
  594. {
  595. struct mxs_dma_filter_param *param = fn_param;
  596. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  597. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  598. int chan_irq;
  599. if (chan->chan_id != param->chan_id)
  600. return false;
  601. chan_irq = platform_get_irq(mxs_dma->pdev, param->chan_id);
  602. if (chan_irq < 0)
  603. return false;
  604. mxs_chan->chan_irq = chan_irq;
  605. return true;
  606. }
  607. static struct dma_chan *mxs_dma_xlate(struct of_phandle_args *dma_spec,
  608. struct of_dma *ofdma)
  609. {
  610. struct mxs_dma_engine *mxs_dma = ofdma->of_dma_data;
  611. dma_cap_mask_t mask = mxs_dma->dma_device.cap_mask;
  612. struct mxs_dma_filter_param param;
  613. if (dma_spec->args_count != 1)
  614. return NULL;
  615. param.chan_id = dma_spec->args[0];
  616. if (param.chan_id >= mxs_dma->nr_channels)
  617. return NULL;
  618. return __dma_request_channel(&mask, mxs_dma_filter_fn, &param,
  619. ofdma->of_node);
  620. }
  621. static int mxs_dma_probe(struct platform_device *pdev)
  622. {
  623. struct device_node *np = pdev->dev.of_node;
  624. const struct mxs_dma_type *dma_type;
  625. struct mxs_dma_engine *mxs_dma;
  626. struct resource *iores;
  627. int ret, i;
  628. mxs_dma = devm_kzalloc(&pdev->dev, sizeof(*mxs_dma), GFP_KERNEL);
  629. if (!mxs_dma)
  630. return -ENOMEM;
  631. ret = of_property_read_u32(np, "dma-channels", &mxs_dma->nr_channels);
  632. if (ret) {
  633. dev_err(&pdev->dev, "failed to read dma-channels\n");
  634. return ret;
  635. }
  636. dma_type = (struct mxs_dma_type *)of_device_get_match_data(&pdev->dev);
  637. mxs_dma->type = dma_type->type;
  638. mxs_dma->dev_id = dma_type->id;
  639. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  640. mxs_dma->base = devm_ioremap_resource(&pdev->dev, iores);
  641. if (IS_ERR(mxs_dma->base))
  642. return PTR_ERR(mxs_dma->base);
  643. mxs_dma->clk = devm_clk_get(&pdev->dev, NULL);
  644. if (IS_ERR(mxs_dma->clk))
  645. return PTR_ERR(mxs_dma->clk);
  646. dma_cap_set(DMA_SLAVE, mxs_dma->dma_device.cap_mask);
  647. dma_cap_set(DMA_CYCLIC, mxs_dma->dma_device.cap_mask);
  648. INIT_LIST_HEAD(&mxs_dma->dma_device.channels);
  649. /* Initialize channel parameters */
  650. for (i = 0; i < MXS_DMA_CHANNELS; i++) {
  651. struct mxs_dma_chan *mxs_chan = &mxs_dma->mxs_chans[i];
  652. mxs_chan->mxs_dma = mxs_dma;
  653. mxs_chan->chan.device = &mxs_dma->dma_device;
  654. dma_cookie_init(&mxs_chan->chan);
  655. tasklet_setup(&mxs_chan->tasklet, mxs_dma_tasklet);
  656. /* Add the channel to mxs_chan list */
  657. list_add_tail(&mxs_chan->chan.device_node,
  658. &mxs_dma->dma_device.channels);
  659. }
  660. ret = mxs_dma_init(mxs_dma);
  661. if (ret)
  662. return ret;
  663. mxs_dma->pdev = pdev;
  664. mxs_dma->dma_device.dev = &pdev->dev;
  665. /* mxs_dma gets 65535 bytes maximum sg size */
  666. dma_set_max_seg_size(mxs_dma->dma_device.dev, MAX_XFER_BYTES);
  667. mxs_dma->dma_device.device_alloc_chan_resources = mxs_dma_alloc_chan_resources;
  668. mxs_dma->dma_device.device_free_chan_resources = mxs_dma_free_chan_resources;
  669. mxs_dma->dma_device.device_tx_status = mxs_dma_tx_status;
  670. mxs_dma->dma_device.device_prep_slave_sg = mxs_dma_prep_slave_sg;
  671. mxs_dma->dma_device.device_prep_dma_cyclic = mxs_dma_prep_dma_cyclic;
  672. mxs_dma->dma_device.device_pause = mxs_dma_pause_chan;
  673. mxs_dma->dma_device.device_resume = mxs_dma_resume_chan;
  674. mxs_dma->dma_device.device_terminate_all = mxs_dma_terminate_all;
  675. mxs_dma->dma_device.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  676. mxs_dma->dma_device.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  677. mxs_dma->dma_device.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  678. mxs_dma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  679. mxs_dma->dma_device.device_issue_pending = mxs_dma_enable_chan;
  680. ret = dmaenginem_async_device_register(&mxs_dma->dma_device);
  681. if (ret) {
  682. dev_err(mxs_dma->dma_device.dev, "unable to register\n");
  683. return ret;
  684. }
  685. ret = of_dma_controller_register(np, mxs_dma_xlate, mxs_dma);
  686. if (ret) {
  687. dev_err(mxs_dma->dma_device.dev,
  688. "failed to register controller\n");
  689. }
  690. dev_info(mxs_dma->dma_device.dev, "initialized\n");
  691. return 0;
  692. }
  693. static struct platform_driver mxs_dma_driver = {
  694. .driver = {
  695. .name = "mxs-dma",
  696. .of_match_table = mxs_dma_dt_ids,
  697. },
  698. .probe = mxs_dma_probe,
  699. };
  700. builtin_platform_driver(mxs_dma_driver);