mxs-dma.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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/fsl/mxs-dma.h>
  26. #include <linux/stmp_device.h>
  27. #include <linux/of.h>
  28. #include <linux/of_device.h>
  29. #include <asm/irq.h>
  30. #include <mach/mxs.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. /*
  56. * ccw bits definitions
  57. *
  58. * COMMAND: 0..1 (2)
  59. * CHAIN: 2 (1)
  60. * IRQ: 3 (1)
  61. * NAND_LOCK: 4 (1) - not implemented
  62. * NAND_WAIT4READY: 5 (1) - not implemented
  63. * DEC_SEM: 6 (1)
  64. * WAIT4END: 7 (1)
  65. * HALT_ON_TERMINATE: 8 (1)
  66. * TERMINATE_FLUSH: 9 (1)
  67. * RESERVED: 10..11 (2)
  68. * PIO_NUM: 12..15 (4)
  69. */
  70. #define BP_CCW_COMMAND 0
  71. #define BM_CCW_COMMAND (3 << 0)
  72. #define CCW_CHAIN (1 << 2)
  73. #define CCW_IRQ (1 << 3)
  74. #define CCW_DEC_SEM (1 << 6)
  75. #define CCW_WAIT4END (1 << 7)
  76. #define CCW_HALT_ON_TERM (1 << 8)
  77. #define CCW_TERM_FLUSH (1 << 9)
  78. #define BP_CCW_PIO_NUM 12
  79. #define BM_CCW_PIO_NUM (0xf << 12)
  80. #define BF_CCW(value, field) (((value) << BP_CCW_##field) & BM_CCW_##field)
  81. #define MXS_DMA_CMD_NO_XFER 0
  82. #define MXS_DMA_CMD_WRITE 1
  83. #define MXS_DMA_CMD_READ 2
  84. #define MXS_DMA_CMD_DMA_SENSE 3 /* not implemented */
  85. struct mxs_dma_ccw {
  86. u32 next;
  87. u16 bits;
  88. u16 xfer_bytes;
  89. #define MAX_XFER_BYTES 0xff00
  90. u32 bufaddr;
  91. #define MXS_PIO_WORDS 16
  92. u32 pio_words[MXS_PIO_WORDS];
  93. };
  94. #define NUM_CCW (int)(PAGE_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. 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. #define MXS_DMA_SG_LOOP (1 << 0)
  107. };
  108. #define MXS_DMA_CHANNELS 16
  109. #define MXS_DMA_CHANNELS_MASK 0xffff
  110. enum mxs_dma_devtype {
  111. MXS_DMA_APBH,
  112. MXS_DMA_APBX,
  113. };
  114. enum mxs_dma_id {
  115. IMX23_DMA,
  116. IMX28_DMA,
  117. };
  118. struct mxs_dma_engine {
  119. enum mxs_dma_id dev_id;
  120. enum mxs_dma_devtype type;
  121. void __iomem *base;
  122. struct clk *clk;
  123. struct dma_device dma_device;
  124. struct device_dma_parameters dma_parms;
  125. struct mxs_dma_chan mxs_chans[MXS_DMA_CHANNELS];
  126. };
  127. struct mxs_dma_type {
  128. enum mxs_dma_id id;
  129. enum mxs_dma_devtype type;
  130. };
  131. static struct mxs_dma_type mxs_dma_types[] = {
  132. {
  133. .id = IMX23_DMA,
  134. .type = MXS_DMA_APBH,
  135. }, {
  136. .id = IMX23_DMA,
  137. .type = MXS_DMA_APBX,
  138. }, {
  139. .id = IMX28_DMA,
  140. .type = MXS_DMA_APBH,
  141. }, {
  142. .id = IMX28_DMA,
  143. .type = MXS_DMA_APBX,
  144. }
  145. };
  146. static struct platform_device_id mxs_dma_ids[] = {
  147. {
  148. .name = "imx23-dma-apbh",
  149. .driver_data = (kernel_ulong_t) &mxs_dma_types[0],
  150. }, {
  151. .name = "imx23-dma-apbx",
  152. .driver_data = (kernel_ulong_t) &mxs_dma_types[1],
  153. }, {
  154. .name = "imx28-dma-apbh",
  155. .driver_data = (kernel_ulong_t) &mxs_dma_types[2],
  156. }, {
  157. .name = "imx28-dma-apbx",
  158. .driver_data = (kernel_ulong_t) &mxs_dma_types[3],
  159. }, {
  160. /* end of list */
  161. }
  162. };
  163. static const struct of_device_id mxs_dma_dt_ids[] = {
  164. { .compatible = "fsl,imx23-dma-apbh", .data = &mxs_dma_ids[0], },
  165. { .compatible = "fsl,imx23-dma-apbx", .data = &mxs_dma_ids[1], },
  166. { .compatible = "fsl,imx28-dma-apbh", .data = &mxs_dma_ids[2], },
  167. { .compatible = "fsl,imx28-dma-apbx", .data = &mxs_dma_ids[3], },
  168. { /* sentinel */ }
  169. };
  170. MODULE_DEVICE_TABLE(of, mxs_dma_dt_ids);
  171. static struct mxs_dma_chan *to_mxs_dma_chan(struct dma_chan *chan)
  172. {
  173. return container_of(chan, struct mxs_dma_chan, chan);
  174. }
  175. int mxs_dma_is_apbh(struct dma_chan *chan)
  176. {
  177. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  178. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  179. return dma_is_apbh(mxs_dma);
  180. }
  181. int mxs_dma_is_apbx(struct dma_chan *chan)
  182. {
  183. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  184. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  185. return !dma_is_apbh(mxs_dma);
  186. }
  187. static void mxs_dma_reset_chan(struct mxs_dma_chan *mxs_chan)
  188. {
  189. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  190. int chan_id = mxs_chan->chan.chan_id;
  191. if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
  192. writel(1 << (chan_id + BP_APBH_CTRL0_RESET_CHANNEL),
  193. mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
  194. else
  195. writel(1 << (chan_id + BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL),
  196. mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
  197. }
  198. static void mxs_dma_enable_chan(struct mxs_dma_chan *mxs_chan)
  199. {
  200. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  201. int chan_id = mxs_chan->chan.chan_id;
  202. /* set cmd_addr up */
  203. writel(mxs_chan->ccw_phys,
  204. mxs_dma->base + HW_APBHX_CHn_NXTCMDAR(mxs_dma, chan_id));
  205. /* write 1 to SEMA to kick off the channel */
  206. writel(1, mxs_dma->base + HW_APBHX_CHn_SEMA(mxs_dma, chan_id));
  207. }
  208. static void mxs_dma_disable_chan(struct mxs_dma_chan *mxs_chan)
  209. {
  210. mxs_chan->status = DMA_SUCCESS;
  211. }
  212. static void mxs_dma_pause_chan(struct mxs_dma_chan *mxs_chan)
  213. {
  214. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  215. int chan_id = mxs_chan->chan.chan_id;
  216. /* freeze the channel */
  217. if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
  218. writel(1 << chan_id,
  219. mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
  220. else
  221. writel(1 << chan_id,
  222. mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
  223. mxs_chan->status = DMA_PAUSED;
  224. }
  225. static void mxs_dma_resume_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. /* unfreeze the channel */
  230. if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
  231. writel(1 << chan_id,
  232. mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_CLR);
  233. else
  234. writel(1 << chan_id,
  235. mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_CLR);
  236. mxs_chan->status = DMA_IN_PROGRESS;
  237. }
  238. static dma_cookie_t mxs_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  239. {
  240. return dma_cookie_assign(tx);
  241. }
  242. static void mxs_dma_tasklet(unsigned long data)
  243. {
  244. struct mxs_dma_chan *mxs_chan = (struct mxs_dma_chan *) data;
  245. if (mxs_chan->desc.callback)
  246. mxs_chan->desc.callback(mxs_chan->desc.callback_param);
  247. }
  248. static irqreturn_t mxs_dma_int_handler(int irq, void *dev_id)
  249. {
  250. struct mxs_dma_engine *mxs_dma = dev_id;
  251. u32 stat1, stat2;
  252. /* completion status */
  253. stat1 = readl(mxs_dma->base + HW_APBHX_CTRL1);
  254. stat1 &= MXS_DMA_CHANNELS_MASK;
  255. writel(stat1, mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_CLR);
  256. /* error status */
  257. stat2 = readl(mxs_dma->base + HW_APBHX_CTRL2);
  258. writel(stat2, mxs_dma->base + HW_APBHX_CTRL2 + STMP_OFFSET_REG_CLR);
  259. /*
  260. * When both completion and error of termination bits set at the
  261. * same time, we do not take it as an error. IOW, it only becomes
  262. * an error we need to handle here in case of either it's (1) a bus
  263. * error or (2) a termination error with no completion.
  264. */
  265. stat2 = ((stat2 >> MXS_DMA_CHANNELS) & stat2) | /* (1) */
  266. (~(stat2 >> MXS_DMA_CHANNELS) & stat2 & ~stat1); /* (2) */
  267. /* combine error and completion status for checking */
  268. stat1 = (stat2 << MXS_DMA_CHANNELS) | stat1;
  269. while (stat1) {
  270. int channel = fls(stat1) - 1;
  271. struct mxs_dma_chan *mxs_chan =
  272. &mxs_dma->mxs_chans[channel % MXS_DMA_CHANNELS];
  273. if (channel >= MXS_DMA_CHANNELS) {
  274. dev_dbg(mxs_dma->dma_device.dev,
  275. "%s: error in channel %d\n", __func__,
  276. channel - MXS_DMA_CHANNELS);
  277. mxs_chan->status = DMA_ERROR;
  278. mxs_dma_reset_chan(mxs_chan);
  279. } else {
  280. if (mxs_chan->flags & MXS_DMA_SG_LOOP)
  281. mxs_chan->status = DMA_IN_PROGRESS;
  282. else
  283. mxs_chan->status = DMA_SUCCESS;
  284. }
  285. stat1 &= ~(1 << channel);
  286. if (mxs_chan->status == DMA_SUCCESS)
  287. dma_cookie_complete(&mxs_chan->desc);
  288. /* schedule tasklet on this channel */
  289. tasklet_schedule(&mxs_chan->tasklet);
  290. }
  291. return IRQ_HANDLED;
  292. }
  293. static int mxs_dma_alloc_chan_resources(struct dma_chan *chan)
  294. {
  295. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  296. struct mxs_dma_data *data = chan->private;
  297. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  298. int ret;
  299. if (!data)
  300. return -EINVAL;
  301. mxs_chan->chan_irq = data->chan_irq;
  302. mxs_chan->ccw = dma_alloc_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
  303. &mxs_chan->ccw_phys, GFP_KERNEL);
  304. if (!mxs_chan->ccw) {
  305. ret = -ENOMEM;
  306. goto err_alloc;
  307. }
  308. memset(mxs_chan->ccw, 0, PAGE_SIZE);
  309. if (mxs_chan->chan_irq != NO_IRQ) {
  310. ret = request_irq(mxs_chan->chan_irq, mxs_dma_int_handler,
  311. 0, "mxs-dma", mxs_dma);
  312. if (ret)
  313. goto err_irq;
  314. }
  315. ret = clk_prepare_enable(mxs_dma->clk);
  316. if (ret)
  317. goto err_clk;
  318. mxs_dma_reset_chan(mxs_chan);
  319. dma_async_tx_descriptor_init(&mxs_chan->desc, chan);
  320. mxs_chan->desc.tx_submit = mxs_dma_tx_submit;
  321. /* the descriptor is ready */
  322. async_tx_ack(&mxs_chan->desc);
  323. return 0;
  324. err_clk:
  325. free_irq(mxs_chan->chan_irq, mxs_dma);
  326. err_irq:
  327. dma_free_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
  328. mxs_chan->ccw, mxs_chan->ccw_phys);
  329. err_alloc:
  330. return ret;
  331. }
  332. static void mxs_dma_free_chan_resources(struct dma_chan *chan)
  333. {
  334. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  335. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  336. mxs_dma_disable_chan(mxs_chan);
  337. free_irq(mxs_chan->chan_irq, mxs_dma);
  338. dma_free_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
  339. mxs_chan->ccw, mxs_chan->ccw_phys);
  340. clk_disable_unprepare(mxs_dma->clk);
  341. }
  342. /*
  343. * How to use the flags for ->device_prep_slave_sg() :
  344. * [1] If there is only one DMA command in the DMA chain, the code should be:
  345. * ......
  346. * ->device_prep_slave_sg(DMA_CTRL_ACK);
  347. * ......
  348. * [2] If there are two DMA commands in the DMA chain, the code should be
  349. * ......
  350. * ->device_prep_slave_sg(0);
  351. * ......
  352. * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  353. * ......
  354. * [3] If there are more than two DMA commands in the DMA chain, the code
  355. * should be:
  356. * ......
  357. * ->device_prep_slave_sg(0); // First
  358. * ......
  359. * ->device_prep_slave_sg(DMA_PREP_INTERRUPT [| DMA_CTRL_ACK]);
  360. * ......
  361. * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK); // Last
  362. * ......
  363. */
  364. static struct dma_async_tx_descriptor *mxs_dma_prep_slave_sg(
  365. struct dma_chan *chan, struct scatterlist *sgl,
  366. unsigned int sg_len, enum dma_transfer_direction direction,
  367. unsigned long flags, void *context)
  368. {
  369. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  370. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  371. struct mxs_dma_ccw *ccw;
  372. struct scatterlist *sg;
  373. int i, j;
  374. u32 *pio;
  375. bool append = flags & DMA_PREP_INTERRUPT;
  376. int idx = append ? mxs_chan->desc_count : 0;
  377. if (mxs_chan->status == DMA_IN_PROGRESS && !append)
  378. return NULL;
  379. if (sg_len + (append ? idx : 0) > NUM_CCW) {
  380. dev_err(mxs_dma->dma_device.dev,
  381. "maximum number of sg exceeded: %d > %d\n",
  382. sg_len, NUM_CCW);
  383. goto err_out;
  384. }
  385. mxs_chan->status = DMA_IN_PROGRESS;
  386. mxs_chan->flags = 0;
  387. /*
  388. * If the sg is prepared with append flag set, the sg
  389. * will be appended to the last prepared sg.
  390. */
  391. if (append) {
  392. BUG_ON(idx < 1);
  393. ccw = &mxs_chan->ccw[idx - 1];
  394. ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
  395. ccw->bits |= CCW_CHAIN;
  396. ccw->bits &= ~CCW_IRQ;
  397. ccw->bits &= ~CCW_DEC_SEM;
  398. } else {
  399. idx = 0;
  400. }
  401. if (direction == DMA_TRANS_NONE) {
  402. ccw = &mxs_chan->ccw[idx++];
  403. pio = (u32 *) sgl;
  404. for (j = 0; j < sg_len;)
  405. ccw->pio_words[j++] = *pio++;
  406. ccw->bits = 0;
  407. ccw->bits |= CCW_IRQ;
  408. ccw->bits |= CCW_DEC_SEM;
  409. if (flags & DMA_CTRL_ACK)
  410. ccw->bits |= CCW_WAIT4END;
  411. ccw->bits |= CCW_HALT_ON_TERM;
  412. ccw->bits |= CCW_TERM_FLUSH;
  413. ccw->bits |= BF_CCW(sg_len, PIO_NUM);
  414. ccw->bits |= BF_CCW(MXS_DMA_CMD_NO_XFER, COMMAND);
  415. } else {
  416. for_each_sg(sgl, sg, sg_len, i) {
  417. if (sg_dma_len(sg) > MAX_XFER_BYTES) {
  418. dev_err(mxs_dma->dma_device.dev, "maximum bytes for sg entry exceeded: %d > %d\n",
  419. sg_dma_len(sg), MAX_XFER_BYTES);
  420. goto err_out;
  421. }
  422. ccw = &mxs_chan->ccw[idx++];
  423. ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
  424. ccw->bufaddr = sg->dma_address;
  425. ccw->xfer_bytes = sg_dma_len(sg);
  426. ccw->bits = 0;
  427. ccw->bits |= CCW_CHAIN;
  428. ccw->bits |= CCW_HALT_ON_TERM;
  429. ccw->bits |= CCW_TERM_FLUSH;
  430. ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
  431. MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ,
  432. COMMAND);
  433. if (i + 1 == sg_len) {
  434. ccw->bits &= ~CCW_CHAIN;
  435. ccw->bits |= CCW_IRQ;
  436. ccw->bits |= CCW_DEC_SEM;
  437. if (flags & DMA_CTRL_ACK)
  438. ccw->bits |= CCW_WAIT4END;
  439. }
  440. }
  441. }
  442. mxs_chan->desc_count = idx;
  443. return &mxs_chan->desc;
  444. err_out:
  445. mxs_chan->status = DMA_ERROR;
  446. return NULL;
  447. }
  448. static struct dma_async_tx_descriptor *mxs_dma_prep_dma_cyclic(
  449. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  450. size_t period_len, enum dma_transfer_direction direction,
  451. void *context)
  452. {
  453. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  454. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  455. int num_periods = buf_len / period_len;
  456. int i = 0, buf = 0;
  457. if (mxs_chan->status == DMA_IN_PROGRESS)
  458. return NULL;
  459. mxs_chan->status = DMA_IN_PROGRESS;
  460. mxs_chan->flags |= MXS_DMA_SG_LOOP;
  461. if (num_periods > NUM_CCW) {
  462. dev_err(mxs_dma->dma_device.dev,
  463. "maximum number of sg exceeded: %d > %d\n",
  464. num_periods, NUM_CCW);
  465. goto err_out;
  466. }
  467. if (period_len > MAX_XFER_BYTES) {
  468. dev_err(mxs_dma->dma_device.dev,
  469. "maximum period size exceeded: %d > %d\n",
  470. period_len, MAX_XFER_BYTES);
  471. goto err_out;
  472. }
  473. while (buf < buf_len) {
  474. struct mxs_dma_ccw *ccw = &mxs_chan->ccw[i];
  475. if (i + 1 == num_periods)
  476. ccw->next = mxs_chan->ccw_phys;
  477. else
  478. ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * (i + 1);
  479. ccw->bufaddr = dma_addr;
  480. ccw->xfer_bytes = period_len;
  481. ccw->bits = 0;
  482. ccw->bits |= CCW_CHAIN;
  483. ccw->bits |= CCW_IRQ;
  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, COMMAND);
  488. dma_addr += period_len;
  489. buf += period_len;
  490. i++;
  491. }
  492. mxs_chan->desc_count = i;
  493. return &mxs_chan->desc;
  494. err_out:
  495. mxs_chan->status = DMA_ERROR;
  496. return NULL;
  497. }
  498. static int mxs_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  499. unsigned long arg)
  500. {
  501. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  502. int ret = 0;
  503. switch (cmd) {
  504. case DMA_TERMINATE_ALL:
  505. mxs_dma_reset_chan(mxs_chan);
  506. mxs_dma_disable_chan(mxs_chan);
  507. break;
  508. case DMA_PAUSE:
  509. mxs_dma_pause_chan(mxs_chan);
  510. break;
  511. case DMA_RESUME:
  512. mxs_dma_resume_chan(mxs_chan);
  513. break;
  514. default:
  515. ret = -ENOSYS;
  516. }
  517. return ret;
  518. }
  519. static enum dma_status mxs_dma_tx_status(struct dma_chan *chan,
  520. dma_cookie_t cookie, struct dma_tx_state *txstate)
  521. {
  522. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  523. dma_cookie_t last_used;
  524. last_used = chan->cookie;
  525. dma_set_tx_state(txstate, chan->completed_cookie, last_used, 0);
  526. return mxs_chan->status;
  527. }
  528. static void mxs_dma_issue_pending(struct dma_chan *chan)
  529. {
  530. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  531. mxs_dma_enable_chan(mxs_chan);
  532. }
  533. static int __init mxs_dma_init(struct mxs_dma_engine *mxs_dma)
  534. {
  535. int ret;
  536. ret = clk_prepare_enable(mxs_dma->clk);
  537. if (ret)
  538. return ret;
  539. ret = stmp_reset_block(mxs_dma->base);
  540. if (ret)
  541. goto err_out;
  542. /* enable apbh burst */
  543. if (dma_is_apbh(mxs_dma)) {
  544. writel(BM_APBH_CTRL0_APB_BURST_EN,
  545. mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
  546. writel(BM_APBH_CTRL0_APB_BURST8_EN,
  547. mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
  548. }
  549. /* enable irq for all the channels */
  550. writel(MXS_DMA_CHANNELS_MASK << MXS_DMA_CHANNELS,
  551. mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_SET);
  552. err_out:
  553. clk_disable_unprepare(mxs_dma->clk);
  554. return ret;
  555. }
  556. static int __init mxs_dma_probe(struct platform_device *pdev)
  557. {
  558. const struct platform_device_id *id_entry;
  559. const struct of_device_id *of_id;
  560. const struct mxs_dma_type *dma_type;
  561. struct mxs_dma_engine *mxs_dma;
  562. struct resource *iores;
  563. int ret, i;
  564. mxs_dma = kzalloc(sizeof(*mxs_dma), GFP_KERNEL);
  565. if (!mxs_dma)
  566. return -ENOMEM;
  567. of_id = of_match_device(mxs_dma_dt_ids, &pdev->dev);
  568. if (of_id)
  569. id_entry = of_id->data;
  570. else
  571. id_entry = platform_get_device_id(pdev);
  572. dma_type = (struct mxs_dma_type *)id_entry->driver_data;
  573. mxs_dma->type = dma_type->type;
  574. mxs_dma->dev_id = dma_type->id;
  575. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  576. if (!request_mem_region(iores->start, resource_size(iores),
  577. pdev->name)) {
  578. ret = -EBUSY;
  579. goto err_request_region;
  580. }
  581. mxs_dma->base = ioremap(iores->start, resource_size(iores));
  582. if (!mxs_dma->base) {
  583. ret = -ENOMEM;
  584. goto err_ioremap;
  585. }
  586. mxs_dma->clk = clk_get(&pdev->dev, NULL);
  587. if (IS_ERR(mxs_dma->clk)) {
  588. ret = PTR_ERR(mxs_dma->clk);
  589. goto err_clk;
  590. }
  591. dma_cap_set(DMA_SLAVE, mxs_dma->dma_device.cap_mask);
  592. dma_cap_set(DMA_CYCLIC, mxs_dma->dma_device.cap_mask);
  593. INIT_LIST_HEAD(&mxs_dma->dma_device.channels);
  594. /* Initialize channel parameters */
  595. for (i = 0; i < MXS_DMA_CHANNELS; i++) {
  596. struct mxs_dma_chan *mxs_chan = &mxs_dma->mxs_chans[i];
  597. mxs_chan->mxs_dma = mxs_dma;
  598. mxs_chan->chan.device = &mxs_dma->dma_device;
  599. dma_cookie_init(&mxs_chan->chan);
  600. tasklet_init(&mxs_chan->tasklet, mxs_dma_tasklet,
  601. (unsigned long) mxs_chan);
  602. /* Add the channel to mxs_chan list */
  603. list_add_tail(&mxs_chan->chan.device_node,
  604. &mxs_dma->dma_device.channels);
  605. }
  606. ret = mxs_dma_init(mxs_dma);
  607. if (ret)
  608. goto err_init;
  609. mxs_dma->dma_device.dev = &pdev->dev;
  610. /* mxs_dma gets 65535 bytes maximum sg size */
  611. mxs_dma->dma_device.dev->dma_parms = &mxs_dma->dma_parms;
  612. dma_set_max_seg_size(mxs_dma->dma_device.dev, MAX_XFER_BYTES);
  613. mxs_dma->dma_device.device_alloc_chan_resources = mxs_dma_alloc_chan_resources;
  614. mxs_dma->dma_device.device_free_chan_resources = mxs_dma_free_chan_resources;
  615. mxs_dma->dma_device.device_tx_status = mxs_dma_tx_status;
  616. mxs_dma->dma_device.device_prep_slave_sg = mxs_dma_prep_slave_sg;
  617. mxs_dma->dma_device.device_prep_dma_cyclic = mxs_dma_prep_dma_cyclic;
  618. mxs_dma->dma_device.device_control = mxs_dma_control;
  619. mxs_dma->dma_device.device_issue_pending = mxs_dma_issue_pending;
  620. ret = dma_async_device_register(&mxs_dma->dma_device);
  621. if (ret) {
  622. dev_err(mxs_dma->dma_device.dev, "unable to register\n");
  623. goto err_init;
  624. }
  625. dev_info(mxs_dma->dma_device.dev, "initialized\n");
  626. return 0;
  627. err_init:
  628. clk_put(mxs_dma->clk);
  629. err_clk:
  630. iounmap(mxs_dma->base);
  631. err_ioremap:
  632. release_mem_region(iores->start, resource_size(iores));
  633. err_request_region:
  634. kfree(mxs_dma);
  635. return ret;
  636. }
  637. static struct platform_driver mxs_dma_driver = {
  638. .driver = {
  639. .name = "mxs-dma",
  640. .of_match_table = mxs_dma_dt_ids,
  641. },
  642. .id_table = mxs_dma_ids,
  643. };
  644. static int __init mxs_dma_module_init(void)
  645. {
  646. return platform_driver_probe(&mxs_dma_driver, mxs_dma_probe);
  647. }
  648. subsys_initcall(mxs_dma_module_init);