mxs-dma.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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_RESET_CHANNEL 16
  42. #define HW_APBHX_CTRL1 0x010
  43. #define HW_APBHX_CTRL2 0x020
  44. #define HW_APBHX_CHANNEL_CTRL 0x030
  45. #define BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL 16
  46. #define HW_APBH_VERSION (cpu_is_mx23() ? 0x3f0 : 0x800)
  47. #define HW_APBX_VERSION 0x800
  48. #define BP_APBHX_VERSION_MAJOR 24
  49. #define HW_APBHX_CHn_NXTCMDAR(n) \
  50. (((dma_is_apbh() && apbh_is_old()) ? 0x050 : 0x110) + (n) * 0x70)
  51. #define HW_APBHX_CHn_SEMA(n) \
  52. (((dma_is_apbh() && apbh_is_old()) ? 0x080 : 0x140) + (n) * 0x70)
  53. /*
  54. * ccw bits definitions
  55. *
  56. * COMMAND: 0..1 (2)
  57. * CHAIN: 2 (1)
  58. * IRQ: 3 (1)
  59. * NAND_LOCK: 4 (1) - not implemented
  60. * NAND_WAIT4READY: 5 (1) - not implemented
  61. * DEC_SEM: 6 (1)
  62. * WAIT4END: 7 (1)
  63. * HALT_ON_TERMINATE: 8 (1)
  64. * TERMINATE_FLUSH: 9 (1)
  65. * RESERVED: 10..11 (2)
  66. * PIO_NUM: 12..15 (4)
  67. */
  68. #define BP_CCW_COMMAND 0
  69. #define BM_CCW_COMMAND (3 << 0)
  70. #define CCW_CHAIN (1 << 2)
  71. #define CCW_IRQ (1 << 3)
  72. #define CCW_DEC_SEM (1 << 6)
  73. #define CCW_WAIT4END (1 << 7)
  74. #define CCW_HALT_ON_TERM (1 << 8)
  75. #define CCW_TERM_FLUSH (1 << 9)
  76. #define BP_CCW_PIO_NUM 12
  77. #define BM_CCW_PIO_NUM (0xf << 12)
  78. #define BF_CCW(value, field) (((value) << BP_CCW_##field) & BM_CCW_##field)
  79. #define MXS_DMA_CMD_NO_XFER 0
  80. #define MXS_DMA_CMD_WRITE 1
  81. #define MXS_DMA_CMD_READ 2
  82. #define MXS_DMA_CMD_DMA_SENSE 3 /* not implemented */
  83. struct mxs_dma_ccw {
  84. u32 next;
  85. u16 bits;
  86. u16 xfer_bytes;
  87. #define MAX_XFER_BYTES 0xff00
  88. u32 bufaddr;
  89. #define MXS_PIO_WORDS 16
  90. u32 pio_words[MXS_PIO_WORDS];
  91. };
  92. #define NUM_CCW (int)(PAGE_SIZE / sizeof(struct mxs_dma_ccw))
  93. struct mxs_dma_chan {
  94. struct mxs_dma_engine *mxs_dma;
  95. struct dma_chan chan;
  96. struct dma_async_tx_descriptor desc;
  97. struct tasklet_struct tasklet;
  98. int chan_irq;
  99. struct mxs_dma_ccw *ccw;
  100. dma_addr_t ccw_phys;
  101. int desc_count;
  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 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 dma_cookie_t mxs_dma_assign_cookie(struct mxs_dma_chan *mxs_chan)
  170. {
  171. dma_cookie_t cookie = mxs_chan->chan.cookie;
  172. if (++cookie < 0)
  173. cookie = 1;
  174. mxs_chan->chan.cookie = cookie;
  175. mxs_chan->desc.cookie = cookie;
  176. return cookie;
  177. }
  178. static struct mxs_dma_chan *to_mxs_dma_chan(struct dma_chan *chan)
  179. {
  180. return container_of(chan, struct mxs_dma_chan, chan);
  181. }
  182. static dma_cookie_t mxs_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  183. {
  184. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(tx->chan);
  185. mxs_dma_enable_chan(mxs_chan);
  186. return mxs_dma_assign_cookie(mxs_chan);
  187. }
  188. static void mxs_dma_tasklet(unsigned long data)
  189. {
  190. struct mxs_dma_chan *mxs_chan = (struct mxs_dma_chan *) data;
  191. if (mxs_chan->desc.callback)
  192. mxs_chan->desc.callback(mxs_chan->desc.callback_param);
  193. }
  194. static irqreturn_t mxs_dma_int_handler(int irq, void *dev_id)
  195. {
  196. struct mxs_dma_engine *mxs_dma = dev_id;
  197. u32 stat1, stat2;
  198. /* completion status */
  199. stat1 = readl(mxs_dma->base + HW_APBHX_CTRL1);
  200. stat1 &= MXS_DMA_CHANNELS_MASK;
  201. writel(stat1, mxs_dma->base + HW_APBHX_CTRL1 + MXS_CLR_ADDR);
  202. /* error status */
  203. stat2 = readl(mxs_dma->base + HW_APBHX_CTRL2);
  204. writel(stat2, mxs_dma->base + HW_APBHX_CTRL2 + MXS_CLR_ADDR);
  205. /*
  206. * When both completion and error of termination bits set at the
  207. * same time, we do not take it as an error. IOW, it only becomes
  208. * an error we need to handle here in case of either it's (1) a bus
  209. * error or (2) a termination error with no completion.
  210. */
  211. stat2 = ((stat2 >> MXS_DMA_CHANNELS) & stat2) | /* (1) */
  212. (~(stat2 >> MXS_DMA_CHANNELS) & stat2 & ~stat1); /* (2) */
  213. /* combine error and completion status for checking */
  214. stat1 = (stat2 << MXS_DMA_CHANNELS) | stat1;
  215. while (stat1) {
  216. int channel = fls(stat1) - 1;
  217. struct mxs_dma_chan *mxs_chan =
  218. &mxs_dma->mxs_chans[channel % MXS_DMA_CHANNELS];
  219. if (channel >= MXS_DMA_CHANNELS) {
  220. dev_dbg(mxs_dma->dma_device.dev,
  221. "%s: error in channel %d\n", __func__,
  222. channel - MXS_DMA_CHANNELS);
  223. mxs_chan->status = DMA_ERROR;
  224. mxs_dma_reset_chan(mxs_chan);
  225. } else {
  226. if (mxs_chan->flags & MXS_DMA_SG_LOOP)
  227. mxs_chan->status = DMA_IN_PROGRESS;
  228. else
  229. mxs_chan->status = DMA_SUCCESS;
  230. }
  231. stat1 &= ~(1 << channel);
  232. if (mxs_chan->status == DMA_SUCCESS)
  233. mxs_chan->last_completed = mxs_chan->desc.cookie;
  234. /* schedule tasklet on this channel */
  235. tasklet_schedule(&mxs_chan->tasklet);
  236. }
  237. return IRQ_HANDLED;
  238. }
  239. static int mxs_dma_alloc_chan_resources(struct dma_chan *chan)
  240. {
  241. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  242. struct mxs_dma_data *data = chan->private;
  243. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  244. int ret;
  245. if (!data)
  246. return -EINVAL;
  247. mxs_chan->chan_irq = data->chan_irq;
  248. mxs_chan->ccw = dma_alloc_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
  249. &mxs_chan->ccw_phys, GFP_KERNEL);
  250. if (!mxs_chan->ccw) {
  251. ret = -ENOMEM;
  252. goto err_alloc;
  253. }
  254. memset(mxs_chan->ccw, 0, PAGE_SIZE);
  255. if (mxs_chan->chan_irq != NO_IRQ) {
  256. ret = request_irq(mxs_chan->chan_irq, mxs_dma_int_handler,
  257. 0, "mxs-dma", mxs_dma);
  258. if (ret)
  259. goto err_irq;
  260. }
  261. ret = clk_prepare_enable(mxs_dma->clk);
  262. if (ret)
  263. goto err_clk;
  264. mxs_dma_reset_chan(mxs_chan);
  265. dma_async_tx_descriptor_init(&mxs_chan->desc, chan);
  266. mxs_chan->desc.tx_submit = mxs_dma_tx_submit;
  267. /* the descriptor is ready */
  268. async_tx_ack(&mxs_chan->desc);
  269. return 0;
  270. err_clk:
  271. free_irq(mxs_chan->chan_irq, mxs_dma);
  272. err_irq:
  273. dma_free_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
  274. mxs_chan->ccw, mxs_chan->ccw_phys);
  275. err_alloc:
  276. return ret;
  277. }
  278. static void mxs_dma_free_chan_resources(struct dma_chan *chan)
  279. {
  280. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  281. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  282. mxs_dma_disable_chan(mxs_chan);
  283. free_irq(mxs_chan->chan_irq, mxs_dma);
  284. dma_free_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
  285. mxs_chan->ccw, mxs_chan->ccw_phys);
  286. clk_disable_unprepare(mxs_dma->clk);
  287. }
  288. static struct dma_async_tx_descriptor *mxs_dma_prep_slave_sg(
  289. struct dma_chan *chan, struct scatterlist *sgl,
  290. unsigned int sg_len, enum dma_transfer_direction direction,
  291. unsigned long append)
  292. {
  293. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  294. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  295. struct mxs_dma_ccw *ccw;
  296. struct scatterlist *sg;
  297. int i, j;
  298. u32 *pio;
  299. int idx = append ? mxs_chan->desc_count : 0;
  300. if (mxs_chan->status == DMA_IN_PROGRESS && !append)
  301. return NULL;
  302. if (sg_len + (append ? idx : 0) > NUM_CCW) {
  303. dev_err(mxs_dma->dma_device.dev,
  304. "maximum number of sg exceeded: %d > %d\n",
  305. sg_len, NUM_CCW);
  306. goto err_out;
  307. }
  308. mxs_chan->status = DMA_IN_PROGRESS;
  309. mxs_chan->flags = 0;
  310. /*
  311. * If the sg is prepared with append flag set, the sg
  312. * will be appended to the last prepared sg.
  313. */
  314. if (append) {
  315. BUG_ON(idx < 1);
  316. ccw = &mxs_chan->ccw[idx - 1];
  317. ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
  318. ccw->bits |= CCW_CHAIN;
  319. ccw->bits &= ~CCW_IRQ;
  320. ccw->bits &= ~CCW_DEC_SEM;
  321. ccw->bits &= ~CCW_WAIT4END;
  322. } else {
  323. idx = 0;
  324. }
  325. if (direction == DMA_TRANS_NONE) {
  326. ccw = &mxs_chan->ccw[idx++];
  327. pio = (u32 *) sgl;
  328. for (j = 0; j < sg_len;)
  329. ccw->pio_words[j++] = *pio++;
  330. ccw->bits = 0;
  331. ccw->bits |= CCW_IRQ;
  332. ccw->bits |= CCW_DEC_SEM;
  333. ccw->bits |= CCW_WAIT4END;
  334. ccw->bits |= CCW_HALT_ON_TERM;
  335. ccw->bits |= CCW_TERM_FLUSH;
  336. ccw->bits |= BF_CCW(sg_len, PIO_NUM);
  337. ccw->bits |= BF_CCW(MXS_DMA_CMD_NO_XFER, COMMAND);
  338. } else {
  339. for_each_sg(sgl, sg, sg_len, i) {
  340. if (sg->length > MAX_XFER_BYTES) {
  341. dev_err(mxs_dma->dma_device.dev, "maximum bytes for sg entry exceeded: %d > %d\n",
  342. sg->length, MAX_XFER_BYTES);
  343. goto err_out;
  344. }
  345. ccw = &mxs_chan->ccw[idx++];
  346. ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
  347. ccw->bufaddr = sg->dma_address;
  348. ccw->xfer_bytes = sg->length;
  349. ccw->bits = 0;
  350. ccw->bits |= CCW_CHAIN;
  351. ccw->bits |= CCW_HALT_ON_TERM;
  352. ccw->bits |= CCW_TERM_FLUSH;
  353. ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
  354. MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ,
  355. COMMAND);
  356. if (i + 1 == sg_len) {
  357. ccw->bits &= ~CCW_CHAIN;
  358. ccw->bits |= CCW_IRQ;
  359. ccw->bits |= CCW_DEC_SEM;
  360. ccw->bits |= CCW_WAIT4END;
  361. }
  362. }
  363. }
  364. mxs_chan->desc_count = idx;
  365. return &mxs_chan->desc;
  366. err_out:
  367. mxs_chan->status = DMA_ERROR;
  368. return NULL;
  369. }
  370. static struct dma_async_tx_descriptor *mxs_dma_prep_dma_cyclic(
  371. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  372. size_t period_len, enum dma_transfer_direction direction)
  373. {
  374. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  375. struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
  376. int num_periods = buf_len / period_len;
  377. int i = 0, buf = 0;
  378. if (mxs_chan->status == DMA_IN_PROGRESS)
  379. return NULL;
  380. mxs_chan->status = DMA_IN_PROGRESS;
  381. mxs_chan->flags |= MXS_DMA_SG_LOOP;
  382. if (num_periods > NUM_CCW) {
  383. dev_err(mxs_dma->dma_device.dev,
  384. "maximum number of sg exceeded: %d > %d\n",
  385. num_periods, NUM_CCW);
  386. goto err_out;
  387. }
  388. if (period_len > MAX_XFER_BYTES) {
  389. dev_err(mxs_dma->dma_device.dev,
  390. "maximum period size exceeded: %d > %d\n",
  391. period_len, MAX_XFER_BYTES);
  392. goto err_out;
  393. }
  394. while (buf < buf_len) {
  395. struct mxs_dma_ccw *ccw = &mxs_chan->ccw[i];
  396. if (i + 1 == num_periods)
  397. ccw->next = mxs_chan->ccw_phys;
  398. else
  399. ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * (i + 1);
  400. ccw->bufaddr = dma_addr;
  401. ccw->xfer_bytes = period_len;
  402. ccw->bits = 0;
  403. ccw->bits |= CCW_CHAIN;
  404. ccw->bits |= CCW_IRQ;
  405. ccw->bits |= CCW_HALT_ON_TERM;
  406. ccw->bits |= CCW_TERM_FLUSH;
  407. ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
  408. MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ, COMMAND);
  409. dma_addr += period_len;
  410. buf += period_len;
  411. i++;
  412. }
  413. mxs_chan->desc_count = i;
  414. return &mxs_chan->desc;
  415. err_out:
  416. mxs_chan->status = DMA_ERROR;
  417. return NULL;
  418. }
  419. static int mxs_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  420. unsigned long arg)
  421. {
  422. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  423. int ret = 0;
  424. switch (cmd) {
  425. case DMA_TERMINATE_ALL:
  426. mxs_dma_reset_chan(mxs_chan);
  427. mxs_dma_disable_chan(mxs_chan);
  428. break;
  429. case DMA_PAUSE:
  430. mxs_dma_pause_chan(mxs_chan);
  431. break;
  432. case DMA_RESUME:
  433. mxs_dma_resume_chan(mxs_chan);
  434. break;
  435. default:
  436. ret = -ENOSYS;
  437. }
  438. return ret;
  439. }
  440. static enum dma_status mxs_dma_tx_status(struct dma_chan *chan,
  441. dma_cookie_t cookie, struct dma_tx_state *txstate)
  442. {
  443. struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
  444. dma_cookie_t last_used;
  445. last_used = chan->cookie;
  446. dma_set_tx_state(txstate, mxs_chan->last_completed, last_used, 0);
  447. return mxs_chan->status;
  448. }
  449. static void mxs_dma_issue_pending(struct dma_chan *chan)
  450. {
  451. /*
  452. * Nothing to do. We only have a single descriptor.
  453. */
  454. }
  455. static int __init mxs_dma_init(struct mxs_dma_engine *mxs_dma)
  456. {
  457. int ret;
  458. ret = clk_prepare_enable(mxs_dma->clk);
  459. if (ret)
  460. return ret;
  461. ret = mxs_reset_block(mxs_dma->base);
  462. if (ret)
  463. goto err_out;
  464. /* only major version matters */
  465. mxs_dma->version = readl(mxs_dma->base +
  466. ((mxs_dma->dev_id == MXS_DMA_APBX) ?
  467. HW_APBX_VERSION : HW_APBH_VERSION)) >>
  468. BP_APBHX_VERSION_MAJOR;
  469. /* enable apbh burst */
  470. if (dma_is_apbh()) {
  471. writel(BM_APBH_CTRL0_APB_BURST_EN,
  472. mxs_dma->base + HW_APBHX_CTRL0 + MXS_SET_ADDR);
  473. writel(BM_APBH_CTRL0_APB_BURST8_EN,
  474. mxs_dma->base + HW_APBHX_CTRL0 + MXS_SET_ADDR);
  475. }
  476. /* enable irq for all the channels */
  477. writel(MXS_DMA_CHANNELS_MASK << MXS_DMA_CHANNELS,
  478. mxs_dma->base + HW_APBHX_CTRL1 + MXS_SET_ADDR);
  479. err_out:
  480. clk_disable_unprepare(mxs_dma->clk);
  481. return ret;
  482. }
  483. static int __init mxs_dma_probe(struct platform_device *pdev)
  484. {
  485. const struct platform_device_id *id_entry =
  486. platform_get_device_id(pdev);
  487. struct mxs_dma_engine *mxs_dma;
  488. struct resource *iores;
  489. int ret, i;
  490. mxs_dma = kzalloc(sizeof(*mxs_dma), GFP_KERNEL);
  491. if (!mxs_dma)
  492. return -ENOMEM;
  493. mxs_dma->dev_id = id_entry->driver_data;
  494. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  495. if (!request_mem_region(iores->start, resource_size(iores),
  496. pdev->name)) {
  497. ret = -EBUSY;
  498. goto err_request_region;
  499. }
  500. mxs_dma->base = ioremap(iores->start, resource_size(iores));
  501. if (!mxs_dma->base) {
  502. ret = -ENOMEM;
  503. goto err_ioremap;
  504. }
  505. mxs_dma->clk = clk_get(&pdev->dev, NULL);
  506. if (IS_ERR(mxs_dma->clk)) {
  507. ret = PTR_ERR(mxs_dma->clk);
  508. goto err_clk;
  509. }
  510. dma_cap_set(DMA_SLAVE, mxs_dma->dma_device.cap_mask);
  511. dma_cap_set(DMA_CYCLIC, mxs_dma->dma_device.cap_mask);
  512. INIT_LIST_HEAD(&mxs_dma->dma_device.channels);
  513. /* Initialize channel parameters */
  514. for (i = 0; i < MXS_DMA_CHANNELS; i++) {
  515. struct mxs_dma_chan *mxs_chan = &mxs_dma->mxs_chans[i];
  516. mxs_chan->mxs_dma = mxs_dma;
  517. mxs_chan->chan.device = &mxs_dma->dma_device;
  518. tasklet_init(&mxs_chan->tasklet, mxs_dma_tasklet,
  519. (unsigned long) mxs_chan);
  520. /* Add the channel to mxs_chan list */
  521. list_add_tail(&mxs_chan->chan.device_node,
  522. &mxs_dma->dma_device.channels);
  523. }
  524. ret = mxs_dma_init(mxs_dma);
  525. if (ret)
  526. goto err_init;
  527. mxs_dma->dma_device.dev = &pdev->dev;
  528. /* mxs_dma gets 65535 bytes maximum sg size */
  529. mxs_dma->dma_device.dev->dma_parms = &mxs_dma->dma_parms;
  530. dma_set_max_seg_size(mxs_dma->dma_device.dev, MAX_XFER_BYTES);
  531. mxs_dma->dma_device.device_alloc_chan_resources = mxs_dma_alloc_chan_resources;
  532. mxs_dma->dma_device.device_free_chan_resources = mxs_dma_free_chan_resources;
  533. mxs_dma->dma_device.device_tx_status = mxs_dma_tx_status;
  534. mxs_dma->dma_device.device_prep_slave_sg = mxs_dma_prep_slave_sg;
  535. mxs_dma->dma_device.device_prep_dma_cyclic = mxs_dma_prep_dma_cyclic;
  536. mxs_dma->dma_device.device_control = mxs_dma_control;
  537. mxs_dma->dma_device.device_issue_pending = mxs_dma_issue_pending;
  538. ret = dma_async_device_register(&mxs_dma->dma_device);
  539. if (ret) {
  540. dev_err(mxs_dma->dma_device.dev, "unable to register\n");
  541. goto err_init;
  542. }
  543. dev_info(mxs_dma->dma_device.dev, "initialized\n");
  544. return 0;
  545. err_init:
  546. clk_put(mxs_dma->clk);
  547. err_clk:
  548. iounmap(mxs_dma->base);
  549. err_ioremap:
  550. release_mem_region(iores->start, resource_size(iores));
  551. err_request_region:
  552. kfree(mxs_dma);
  553. return ret;
  554. }
  555. static struct platform_device_id mxs_dma_type[] = {
  556. {
  557. .name = "mxs-dma-apbh",
  558. .driver_data = MXS_DMA_APBH,
  559. }, {
  560. .name = "mxs-dma-apbx",
  561. .driver_data = MXS_DMA_APBX,
  562. }, {
  563. /* end of list */
  564. }
  565. };
  566. static struct platform_driver mxs_dma_driver = {
  567. .driver = {
  568. .name = "mxs-dma",
  569. },
  570. .id_table = mxs_dma_type,
  571. };
  572. static int __init mxs_dma_module_init(void)
  573. {
  574. return platform_driver_probe(&mxs_dma_driver, mxs_dma_probe);
  575. }
  576. subsys_initcall(mxs_dma_module_init);