mxs-dma.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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_zalloc_coherent(mxs_dma->dma_device.dev,
  359. CCW_BLOCK_SIZE,
  360. &mxs_chan->ccw_phys, GFP_KERNEL);
  361. if (!mxs_chan->ccw) {
  362. ret = -ENOMEM;
  363. goto err_alloc;
  364. }
  365. if (mxs_chan->chan_irq != NO_IRQ) {
  366. ret = request_irq(mxs_chan->chan_irq, mxs_dma_int_handler,
  367. 0, "mxs-dma", mxs_dma);
  368. if (ret)
  369. goto err_irq;
  370. }
  371. ret = clk_prepare_enable(mxs_dma->clk);
  372. if (ret)
  373. goto err_clk;
  374. mxs_dma_reset_chan(mxs_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(mxs_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: %d > %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_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  557. unsigned long arg)
  558. {
  559. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  560. int ret = 0;
  561. switch (cmd) {
  562. case DMA_TERMINATE_ALL:
  563. mxs_dma_reset_chan(mxs_chan);
  564. mxs_dma_disable_chan(mxs_chan);
  565. break;
  566. case DMA_PAUSE:
  567. mxs_dma_pause_chan(mxs_chan);
  568. break;
  569. case DMA_RESUME:
  570. mxs_dma_resume_chan(mxs_chan);
  571. break;
  572. default:
  573. ret = -ENOSYS;
  574. }
  575. return ret;
  576. }
  577. static enum dma_status mxs_dma_tx_status(struct dma_chan *chan,
  578. dma_cookie_t cookie, struct dma_tx_state *txstate)
  579. {
  580. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  581. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  582. u32 residue = 0;
  583. if (mxs_chan->status == DMA_IN_PROGRESS &&
  584. mxs_chan->flags & MXS_DMA_SG_LOOP) {
  585. struct mxs_dma_ccw *last_ccw;
  586. u32 bar;
  587. last_ccw = &mxs_chan->ccw[mxs_chan->desc_count - 1];
  588. residue = last_ccw->xfer_bytes + last_ccw->bufaddr;
  589. bar = readl(mxs_dma->base +
  590. HW_APBHX_CHn_BAR(mxs_dma, chan->chan_id));
  591. residue -= bar;
  592. }
  593. dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
  594. residue);
  595. return mxs_chan->status;
  596. }
  597. static void mxs_dma_issue_pending(struct dma_chan *chan)
  598. {
  599. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  600. mxs_dma_enable_chan(mxs_chan);
  601. }
  602. static int __init mxs_dma_init(struct mxs_dma_engine *mxs_dma)
  603. {
  604. int ret;
  605. ret = clk_prepare_enable(mxs_dma->clk);
  606. if (ret)
  607. return ret;
  608. ret = stmp_reset_block(mxs_dma->base);
  609. if (ret)
  610. goto err_out;
  611. /* enable apbh burst */
  612. if (dma_is_apbh(mxs_dma)) {
  613. writel(BM_APBH_CTRL0_APB_BURST_EN,
  614. mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
  615. writel(BM_APBH_CTRL0_APB_BURST8_EN,
  616. mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
  617. }
  618. /* enable irq for all the channels */
  619. writel(MXS_DMA_CHANNELS_MASK << MXS_DMA_CHANNELS,
  620. mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_SET);
  621. err_out:
  622. clk_disable_unprepare(mxs_dma->clk);
  623. return ret;
  624. }
  625. struct mxs_dma_filter_param {
  626. struct device_node *of_node;
  627. unsigned int chan_id;
  628. };
  629. static bool mxs_dma_filter_fn(struct dma_chan *chan, void *fn_param)
  630. {
  631. struct mxs_dma_filter_param *param = fn_param;
  632. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  633. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  634. int chan_irq;
  635. if (mxs_dma->dma_device.dev->of_node != param->of_node)
  636. return false;
  637. if (chan->chan_id != param->chan_id)
  638. return false;
  639. chan_irq = platform_get_irq(mxs_dma->pdev, param->chan_id);
  640. if (chan_irq < 0)
  641. return false;
  642. mxs_chan->chan_irq = chan_irq;
  643. return true;
  644. }
  645. static struct dma_chan *mxs_dma_xlate(struct of_phandle_args *dma_spec,
  646. struct of_dma *ofdma)
  647. {
  648. struct mxs_dma_engine *mxs_dma = ofdma->of_dma_data;
  649. dma_cap_mask_t mask = mxs_dma->dma_device.cap_mask;
  650. struct mxs_dma_filter_param param;
  651. if (dma_spec->args_count != 1)
  652. return NULL;
  653. param.of_node = ofdma->of_node;
  654. param.chan_id = dma_spec->args[0];
  655. if (param.chan_id >= mxs_dma->nr_channels)
  656. return NULL;
  657. return dma_request_channel(mask, mxs_dma_filter_fn, &param);
  658. }
  659. static int __init mxs_dma_probe(struct platform_device *pdev)
  660. {
  661. struct device_node *np = pdev->dev.of_node;
  662. const struct platform_device_id *id_entry;
  663. const struct of_device_id *of_id;
  664. const struct mxs_dma_type *dma_type;
  665. struct mxs_dma_engine *mxs_dma;
  666. struct resource *iores;
  667. int ret, i;
  668. mxs_dma = devm_kzalloc(&pdev->dev, sizeof(*mxs_dma), GFP_KERNEL);
  669. if (!mxs_dma)
  670. return -ENOMEM;
  671. ret = of_property_read_u32(np, "dma-channels", &mxs_dma->nr_channels);
  672. if (ret) {
  673. dev_err(&pdev->dev, "failed to read dma-channels\n");
  674. return ret;
  675. }
  676. of_id = of_match_device(mxs_dma_dt_ids, &pdev->dev);
  677. if (of_id)
  678. id_entry = of_id->data;
  679. else
  680. id_entry = platform_get_device_id(pdev);
  681. dma_type = (struct mxs_dma_type *)id_entry->driver_data;
  682. mxs_dma->type = dma_type->type;
  683. mxs_dma->dev_id = dma_type->id;
  684. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  685. mxs_dma->base = devm_ioremap_resource(&pdev->dev, iores);
  686. if (IS_ERR(mxs_dma->base))
  687. return PTR_ERR(mxs_dma->base);
  688. mxs_dma->clk = devm_clk_get(&pdev->dev, NULL);
  689. if (IS_ERR(mxs_dma->clk))
  690. return PTR_ERR(mxs_dma->clk);
  691. dma_cap_set(DMA_SLAVE, mxs_dma->dma_device.cap_mask);
  692. dma_cap_set(DMA_CYCLIC, mxs_dma->dma_device.cap_mask);
  693. INIT_LIST_HEAD(&mxs_dma->dma_device.channels);
  694. /* Initialize channel parameters */
  695. for (i = 0; i < MXS_DMA_CHANNELS; i++) {
  696. struct mxs_dma_chan *mxs_chan = &mxs_dma->mxs_chans[i];
  697. mxs_chan->mxs_dma = mxs_dma;
  698. mxs_chan->chan.device = &mxs_dma->dma_device;
  699. dma_cookie_init(&mxs_chan->chan);
  700. tasklet_init(&mxs_chan->tasklet, mxs_dma_tasklet,
  701. (unsigned long) mxs_chan);
  702. /* Add the channel to mxs_chan list */
  703. list_add_tail(&mxs_chan->chan.device_node,
  704. &mxs_dma->dma_device.channels);
  705. }
  706. ret = mxs_dma_init(mxs_dma);
  707. if (ret)
  708. return ret;
  709. mxs_dma->pdev = pdev;
  710. mxs_dma->dma_device.dev = &pdev->dev;
  711. /* mxs_dma gets 65535 bytes maximum sg size */
  712. mxs_dma->dma_device.dev->dma_parms = &mxs_dma->dma_parms;
  713. dma_set_max_seg_size(mxs_dma->dma_device.dev, MAX_XFER_BYTES);
  714. mxs_dma->dma_device.device_alloc_chan_resources = mxs_dma_alloc_chan_resources;
  715. mxs_dma->dma_device.device_free_chan_resources = mxs_dma_free_chan_resources;
  716. mxs_dma->dma_device.device_tx_status = mxs_dma_tx_status;
  717. mxs_dma->dma_device.device_prep_slave_sg = mxs_dma_prep_slave_sg;
  718. mxs_dma->dma_device.device_prep_dma_cyclic = mxs_dma_prep_dma_cyclic;
  719. mxs_dma->dma_device.device_control = mxs_dma_control;
  720. mxs_dma->dma_device.device_issue_pending = mxs_dma_issue_pending;
  721. ret = dma_async_device_register(&mxs_dma->dma_device);
  722. if (ret) {
  723. dev_err(mxs_dma->dma_device.dev, "unable to register\n");
  724. return ret;
  725. }
  726. ret = of_dma_controller_register(np, mxs_dma_xlate, mxs_dma);
  727. if (ret) {
  728. dev_err(mxs_dma->dma_device.dev,
  729. "failed to register controller\n");
  730. dma_async_device_unregister(&mxs_dma->dma_device);
  731. }
  732. dev_info(mxs_dma->dma_device.dev, "initialized\n");
  733. return 0;
  734. }
  735. static struct platform_driver mxs_dma_driver = {
  736. .driver = {
  737. .name = "mxs-dma",
  738. .of_match_table = mxs_dma_dt_ids,
  739. },
  740. .id_table = mxs_dma_ids,
  741. };
  742. static int __init mxs_dma_module_init(void)
  743. {
  744. return platform_driver_probe(&mxs_dma_driver, mxs_dma_probe);
  745. }
  746. subsys_initcall(mxs_dma_module_init);