mxs-dma.c 22 KB

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