mxs-dma.c 23 KB

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