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