mxs-dma.c 20 KB

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