mxs-dma.c 18 KB

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