mxs-dma.c 21 KB

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