imx-dma.c 10 KB

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