mxs-dma.c 21 KB

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