mxs-dma.c 24 KB

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