imx-dma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * drivers/dma/imx-dma.c
  3. *
  4. * This file contains a driver for the Freescale i.MX DMA engine
  5. * found on i.MX1/21/27
  6. *
  7. * Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  8. *
  9. * The code contained herein is licensed under the GNU General Public
  10. * License. You may obtain a copy of the GNU General Public License
  11. * Version 2 or later at the following locations:
  12. *
  13. * http://www.opensource.org/licenses/gpl-license.html
  14. * http://www.gnu.org/copyleft/gpl.html
  15. */
  16. #include <linux/init.h>
  17. #include <linux/types.h>
  18. #include <linux/mm.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/device.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/slab.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/dmaengine.h>
  26. #include <asm/irq.h>
  27. #include <mach/dma-v1.h>
  28. #include <mach/hardware.h>
  29. struct imxdma_channel {
  30. struct imxdma_engine *imxdma;
  31. unsigned int channel;
  32. unsigned int imxdma_channel;
  33. enum dma_slave_buswidth word_size;
  34. dma_addr_t per_address;
  35. u32 watermark_level;
  36. struct dma_chan chan;
  37. spinlock_t lock;
  38. struct dma_async_tx_descriptor desc;
  39. dma_cookie_t last_completed;
  40. enum dma_status status;
  41. int dma_request;
  42. struct scatterlist *sg_list;
  43. };
  44. #define MAX_DMA_CHANNELS 8
  45. struct imxdma_engine {
  46. struct device *dev;
  47. struct device_dma_parameters dma_parms;
  48. struct dma_device dma_device;
  49. struct imxdma_channel channel[MAX_DMA_CHANNELS];
  50. };
  51. static struct imxdma_channel *to_imxdma_chan(struct dma_chan *chan)
  52. {
  53. return container_of(chan, struct imxdma_channel, chan);
  54. }
  55. static void imxdma_handle(struct imxdma_channel *imxdmac)
  56. {
  57. if (imxdmac->desc.callback)
  58. imxdmac->desc.callback(imxdmac->desc.callback_param);
  59. imxdmac->last_completed = imxdmac->desc.cookie;
  60. }
  61. static void imxdma_irq_handler(int channel, void *data)
  62. {
  63. struct imxdma_channel *imxdmac = data;
  64. imxdmac->status = DMA_SUCCESS;
  65. imxdma_handle(imxdmac);
  66. }
  67. static void imxdma_err_handler(int channel, void *data, int error)
  68. {
  69. struct imxdma_channel *imxdmac = data;
  70. imxdmac->status = DMA_ERROR;
  71. imxdma_handle(imxdmac);
  72. }
  73. static void imxdma_progression(int channel, void *data,
  74. struct scatterlist *sg)
  75. {
  76. struct imxdma_channel *imxdmac = data;
  77. imxdmac->status = DMA_SUCCESS;
  78. imxdma_handle(imxdmac);
  79. }
  80. static int imxdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  81. unsigned long arg)
  82. {
  83. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  84. struct dma_slave_config *dmaengine_cfg = (void *)arg;
  85. int ret;
  86. unsigned int mode = 0;
  87. switch (cmd) {
  88. case DMA_TERMINATE_ALL:
  89. imxdmac->status = DMA_ERROR;
  90. imx_dma_disable(imxdmac->imxdma_channel);
  91. return 0;
  92. case DMA_SLAVE_CONFIG:
  93. if (dmaengine_cfg->direction == DMA_FROM_DEVICE) {
  94. imxdmac->per_address = dmaengine_cfg->src_addr;
  95. imxdmac->watermark_level = dmaengine_cfg->src_maxburst;
  96. imxdmac->word_size = dmaengine_cfg->src_addr_width;
  97. } else {
  98. imxdmac->per_address = dmaengine_cfg->dst_addr;
  99. imxdmac->watermark_level = dmaengine_cfg->dst_maxburst;
  100. imxdmac->word_size = dmaengine_cfg->dst_addr_width;
  101. }
  102. switch (imxdmac->word_size) {
  103. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  104. mode = IMX_DMA_MEMSIZE_8;
  105. break;
  106. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  107. mode = IMX_DMA_MEMSIZE_16;
  108. break;
  109. default:
  110. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  111. mode = IMX_DMA_MEMSIZE_32;
  112. break;
  113. }
  114. ret = imx_dma_config_channel(imxdmac->imxdma_channel,
  115. mode | IMX_DMA_TYPE_FIFO,
  116. IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR,
  117. imxdmac->dma_request, 1);
  118. if (ret)
  119. return ret;
  120. imx_dma_config_burstlen(imxdmac->imxdma_channel, imxdmac->watermark_level);
  121. return 0;
  122. default:
  123. return -ENOSYS;
  124. }
  125. return -EINVAL;
  126. }
  127. static enum dma_status imxdma_tx_status(struct dma_chan *chan,
  128. dma_cookie_t cookie,
  129. struct dma_tx_state *txstate)
  130. {
  131. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  132. dma_cookie_t last_used;
  133. enum dma_status ret;
  134. last_used = chan->cookie;
  135. ret = dma_async_is_complete(cookie, imxdmac->last_completed, last_used);
  136. dma_set_tx_state(txstate, imxdmac->last_completed, last_used, 0);
  137. return ret;
  138. }
  139. static dma_cookie_t imxdma_assign_cookie(struct imxdma_channel *imxdma)
  140. {
  141. dma_cookie_t cookie = imxdma->chan.cookie;
  142. if (++cookie < 0)
  143. cookie = 1;
  144. imxdma->chan.cookie = cookie;
  145. imxdma->desc.cookie = cookie;
  146. return cookie;
  147. }
  148. static dma_cookie_t imxdma_tx_submit(struct dma_async_tx_descriptor *tx)
  149. {
  150. struct imxdma_channel *imxdmac = to_imxdma_chan(tx->chan);
  151. dma_cookie_t cookie;
  152. spin_lock_irq(&imxdmac->lock);
  153. cookie = imxdma_assign_cookie(imxdmac);
  154. imx_dma_enable(imxdmac->imxdma_channel);
  155. spin_unlock_irq(&imxdmac->lock);
  156. return cookie;
  157. }
  158. static int imxdma_alloc_chan_resources(struct dma_chan *chan)
  159. {
  160. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  161. struct imx_dma_data *data = chan->private;
  162. imxdmac->dma_request = data->dma_request;
  163. dma_async_tx_descriptor_init(&imxdmac->desc, chan);
  164. imxdmac->desc.tx_submit = imxdma_tx_submit;
  165. /* txd.flags will be overwritten in prep funcs */
  166. imxdmac->desc.flags = DMA_CTRL_ACK;
  167. imxdmac->status = DMA_SUCCESS;
  168. return 0;
  169. }
  170. static void imxdma_free_chan_resources(struct dma_chan *chan)
  171. {
  172. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  173. imx_dma_disable(imxdmac->imxdma_channel);
  174. if (imxdmac->sg_list) {
  175. kfree(imxdmac->sg_list);
  176. imxdmac->sg_list = NULL;
  177. }
  178. }
  179. static struct dma_async_tx_descriptor *imxdma_prep_slave_sg(
  180. struct dma_chan *chan, struct scatterlist *sgl,
  181. unsigned int sg_len, enum dma_data_direction direction,
  182. unsigned long flags)
  183. {
  184. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  185. struct scatterlist *sg;
  186. int i, ret, dma_length = 0;
  187. unsigned int dmamode;
  188. if (imxdmac->status == DMA_IN_PROGRESS)
  189. return NULL;
  190. imxdmac->status = DMA_IN_PROGRESS;
  191. for_each_sg(sgl, sg, sg_len, i) {
  192. dma_length += sg->length;
  193. }
  194. if (direction == DMA_FROM_DEVICE)
  195. dmamode = DMA_MODE_READ;
  196. else
  197. dmamode = DMA_MODE_WRITE;
  198. switch (imxdmac->word_size) {
  199. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  200. if (sgl->length & 3 || sgl->dma_address & 3)
  201. return NULL;
  202. break;
  203. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  204. if (sgl->length & 1 || sgl->dma_address & 1)
  205. return NULL;
  206. break;
  207. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  208. break;
  209. default:
  210. return NULL;
  211. }
  212. ret = imx_dma_setup_sg(imxdmac->imxdma_channel, sgl, sg_len,
  213. dma_length, imxdmac->per_address, dmamode);
  214. if (ret)
  215. return NULL;
  216. return &imxdmac->desc;
  217. }
  218. static struct dma_async_tx_descriptor *imxdma_prep_dma_cyclic(
  219. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  220. size_t period_len, enum dma_data_direction direction)
  221. {
  222. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  223. struct imxdma_engine *imxdma = imxdmac->imxdma;
  224. int i, ret;
  225. unsigned int periods = buf_len / period_len;
  226. unsigned int dmamode;
  227. dev_dbg(imxdma->dev, "%s channel: %d buf_len=%d period_len=%d\n",
  228. __func__, imxdmac->channel, buf_len, period_len);
  229. if (imxdmac->status == DMA_IN_PROGRESS)
  230. return NULL;
  231. imxdmac->status = DMA_IN_PROGRESS;
  232. ret = imx_dma_setup_progression_handler(imxdmac->imxdma_channel,
  233. imxdma_progression);
  234. if (ret) {
  235. dev_err(imxdma->dev, "Failed to setup the DMA handler\n");
  236. return NULL;
  237. }
  238. if (imxdmac->sg_list)
  239. kfree(imxdmac->sg_list);
  240. imxdmac->sg_list = kcalloc(periods + 1,
  241. sizeof(struct scatterlist), GFP_KERNEL);
  242. if (!imxdmac->sg_list)
  243. return NULL;
  244. sg_init_table(imxdmac->sg_list, periods);
  245. for (i = 0; i < periods; i++) {
  246. imxdmac->sg_list[i].page_link = 0;
  247. imxdmac->sg_list[i].offset = 0;
  248. imxdmac->sg_list[i].dma_address = dma_addr;
  249. imxdmac->sg_list[i].length = period_len;
  250. dma_addr += period_len;
  251. }
  252. /* close the loop */
  253. imxdmac->sg_list[periods].offset = 0;
  254. imxdmac->sg_list[periods].length = 0;
  255. imxdmac->sg_list[periods].page_link =
  256. ((unsigned long)imxdmac->sg_list | 0x01) & ~0x02;
  257. if (direction == DMA_FROM_DEVICE)
  258. dmamode = DMA_MODE_READ;
  259. else
  260. dmamode = DMA_MODE_WRITE;
  261. ret = imx_dma_setup_sg(imxdmac->imxdma_channel, imxdmac->sg_list, periods,
  262. IMX_DMA_LENGTH_LOOP, imxdmac->per_address, dmamode);
  263. if (ret)
  264. return NULL;
  265. return &imxdmac->desc;
  266. }
  267. static void imxdma_issue_pending(struct dma_chan *chan)
  268. {
  269. /*
  270. * Nothing to do. We only have a single descriptor
  271. */
  272. }
  273. static int __init imxdma_probe(struct platform_device *pdev)
  274. {
  275. struct imxdma_engine *imxdma;
  276. int ret, i;
  277. imxdma = kzalloc(sizeof(*imxdma), GFP_KERNEL);
  278. if (!imxdma)
  279. return -ENOMEM;
  280. INIT_LIST_HEAD(&imxdma->dma_device.channels);
  281. dma_cap_set(DMA_SLAVE, imxdma->dma_device.cap_mask);
  282. dma_cap_set(DMA_CYCLIC, imxdma->dma_device.cap_mask);
  283. /* Initialize channel parameters */
  284. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  285. struct imxdma_channel *imxdmac = &imxdma->channel[i];
  286. imxdmac->imxdma_channel = imx_dma_request_by_prio("dmaengine",
  287. DMA_PRIO_MEDIUM);
  288. if ((int)imxdmac->channel < 0) {
  289. ret = -ENODEV;
  290. goto err_init;
  291. }
  292. imx_dma_setup_handlers(imxdmac->imxdma_channel,
  293. imxdma_irq_handler, imxdma_err_handler, imxdmac);
  294. imxdmac->imxdma = imxdma;
  295. spin_lock_init(&imxdmac->lock);
  296. imxdmac->chan.device = &imxdma->dma_device;
  297. imxdmac->channel = i;
  298. /* Add the channel to the DMAC list */
  299. list_add_tail(&imxdmac->chan.device_node, &imxdma->dma_device.channels);
  300. }
  301. imxdma->dev = &pdev->dev;
  302. imxdma->dma_device.dev = &pdev->dev;
  303. imxdma->dma_device.device_alloc_chan_resources = imxdma_alloc_chan_resources;
  304. imxdma->dma_device.device_free_chan_resources = imxdma_free_chan_resources;
  305. imxdma->dma_device.device_tx_status = imxdma_tx_status;
  306. imxdma->dma_device.device_prep_slave_sg = imxdma_prep_slave_sg;
  307. imxdma->dma_device.device_prep_dma_cyclic = imxdma_prep_dma_cyclic;
  308. imxdma->dma_device.device_control = imxdma_control;
  309. imxdma->dma_device.device_issue_pending = imxdma_issue_pending;
  310. platform_set_drvdata(pdev, imxdma);
  311. imxdma->dma_device.dev->dma_parms = &imxdma->dma_parms;
  312. dma_set_max_seg_size(imxdma->dma_device.dev, 0xffffff);
  313. ret = dma_async_device_register(&imxdma->dma_device);
  314. if (ret) {
  315. dev_err(&pdev->dev, "unable to register\n");
  316. goto err_init;
  317. }
  318. return 0;
  319. err_init:
  320. while (--i >= 0) {
  321. struct imxdma_channel *imxdmac = &imxdma->channel[i];
  322. imx_dma_free(imxdmac->imxdma_channel);
  323. }
  324. kfree(imxdma);
  325. return ret;
  326. }
  327. static int __exit imxdma_remove(struct platform_device *pdev)
  328. {
  329. struct imxdma_engine *imxdma = platform_get_drvdata(pdev);
  330. int i;
  331. dma_async_device_unregister(&imxdma->dma_device);
  332. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  333. struct imxdma_channel *imxdmac = &imxdma->channel[i];
  334. imx_dma_free(imxdmac->imxdma_channel);
  335. }
  336. kfree(imxdma);
  337. return 0;
  338. }
  339. static struct platform_driver imxdma_driver = {
  340. .driver = {
  341. .name = "imx-dma",
  342. },
  343. .remove = __exit_p(imxdma_remove),
  344. };
  345. static int __init imxdma_module_init(void)
  346. {
  347. return platform_driver_probe(&imxdma_driver, imxdma_probe);
  348. }
  349. subsys_initcall(imxdma_module_init);
  350. MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
  351. MODULE_DESCRIPTION("i.MX dma driver");
  352. MODULE_LICENSE("GPL");