mxs-dma.c 19 KB

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