mxs-dma.c 22 KB

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