mxs-dma.c 23 KB

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