mxs-dma.c 20 KB

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