mxs-dma.c 18 KB

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