imx-dma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. imxdmac->chan.completed_cookie = imxdmac->desc.cookie;
  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. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  135. dma_cookie_t last_used;
  136. enum dma_status ret;
  137. last_used = chan->cookie;
  138. ret = dma_async_is_complete(cookie, chan->completed_cookie, last_used);
  139. dma_set_tx_state(txstate, chan->completed_cookie, last_used, 0);
  140. return ret;
  141. }
  142. static dma_cookie_t imxdma_tx_submit(struct dma_async_tx_descriptor *tx)
  143. {
  144. struct imxdma_channel *imxdmac = to_imxdma_chan(tx->chan);
  145. dma_cookie_t cookie;
  146. spin_lock_irq(&imxdmac->lock);
  147. cookie = dma_cookie_assign(tx);
  148. spin_unlock_irq(&imxdmac->lock);
  149. return cookie;
  150. }
  151. static int imxdma_alloc_chan_resources(struct dma_chan *chan)
  152. {
  153. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  154. struct imx_dma_data *data = chan->private;
  155. imxdmac->dma_request = data->dma_request;
  156. dma_async_tx_descriptor_init(&imxdmac->desc, chan);
  157. imxdmac->desc.tx_submit = imxdma_tx_submit;
  158. /* txd.flags will be overwritten in prep funcs */
  159. imxdmac->desc.flags = DMA_CTRL_ACK;
  160. imxdmac->status = DMA_SUCCESS;
  161. return 0;
  162. }
  163. static void imxdma_free_chan_resources(struct dma_chan *chan)
  164. {
  165. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  166. imx_dma_disable(imxdmac->imxdma_channel);
  167. if (imxdmac->sg_list) {
  168. kfree(imxdmac->sg_list);
  169. imxdmac->sg_list = NULL;
  170. }
  171. }
  172. static struct dma_async_tx_descriptor *imxdma_prep_slave_sg(
  173. struct dma_chan *chan, struct scatterlist *sgl,
  174. unsigned int sg_len, enum dma_transfer_direction direction,
  175. unsigned long flags)
  176. {
  177. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  178. struct scatterlist *sg;
  179. int i, ret, dma_length = 0;
  180. unsigned int dmamode;
  181. if (imxdmac->status == DMA_IN_PROGRESS)
  182. return NULL;
  183. imxdmac->status = DMA_IN_PROGRESS;
  184. for_each_sg(sgl, sg, sg_len, i) {
  185. dma_length += sg->length;
  186. }
  187. if (direction == DMA_DEV_TO_MEM)
  188. dmamode = DMA_MODE_READ;
  189. else
  190. dmamode = DMA_MODE_WRITE;
  191. switch (imxdmac->word_size) {
  192. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  193. if (sgl->length & 3 || sgl->dma_address & 3)
  194. return NULL;
  195. break;
  196. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  197. if (sgl->length & 1 || sgl->dma_address & 1)
  198. return NULL;
  199. break;
  200. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  201. break;
  202. default:
  203. return NULL;
  204. }
  205. ret = imx_dma_setup_sg(imxdmac->imxdma_channel, sgl, sg_len,
  206. dma_length, imxdmac->per_address, dmamode);
  207. if (ret)
  208. return NULL;
  209. return &imxdmac->desc;
  210. }
  211. static struct dma_async_tx_descriptor *imxdma_prep_dma_cyclic(
  212. struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
  213. size_t period_len, enum dma_transfer_direction direction)
  214. {
  215. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  216. struct imxdma_engine *imxdma = imxdmac->imxdma;
  217. int i, ret;
  218. unsigned int periods = buf_len / period_len;
  219. unsigned int dmamode;
  220. dev_dbg(imxdma->dev, "%s channel: %d buf_len=%d period_len=%d\n",
  221. __func__, imxdmac->channel, buf_len, period_len);
  222. if (imxdmac->status == DMA_IN_PROGRESS)
  223. return NULL;
  224. imxdmac->status = DMA_IN_PROGRESS;
  225. ret = imx_dma_setup_progression_handler(imxdmac->imxdma_channel,
  226. imxdma_progression);
  227. if (ret) {
  228. dev_err(imxdma->dev, "Failed to setup the DMA handler\n");
  229. return NULL;
  230. }
  231. if (imxdmac->sg_list)
  232. kfree(imxdmac->sg_list);
  233. imxdmac->sg_list = kcalloc(periods + 1,
  234. sizeof(struct scatterlist), GFP_KERNEL);
  235. if (!imxdmac->sg_list)
  236. return NULL;
  237. sg_init_table(imxdmac->sg_list, periods);
  238. for (i = 0; i < periods; i++) {
  239. imxdmac->sg_list[i].page_link = 0;
  240. imxdmac->sg_list[i].offset = 0;
  241. imxdmac->sg_list[i].dma_address = dma_addr;
  242. imxdmac->sg_list[i].length = period_len;
  243. dma_addr += period_len;
  244. }
  245. /* close the loop */
  246. imxdmac->sg_list[periods].offset = 0;
  247. imxdmac->sg_list[periods].length = 0;
  248. imxdmac->sg_list[periods].page_link =
  249. ((unsigned long)imxdmac->sg_list | 0x01) & ~0x02;
  250. if (direction == DMA_DEV_TO_MEM)
  251. dmamode = DMA_MODE_READ;
  252. else
  253. dmamode = DMA_MODE_WRITE;
  254. ret = imx_dma_setup_sg(imxdmac->imxdma_channel, imxdmac->sg_list, periods,
  255. IMX_DMA_LENGTH_LOOP, imxdmac->per_address, dmamode);
  256. if (ret)
  257. return NULL;
  258. return &imxdmac->desc;
  259. }
  260. static void imxdma_issue_pending(struct dma_chan *chan)
  261. {
  262. struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
  263. if (imxdmac->status == DMA_IN_PROGRESS)
  264. imx_dma_enable(imxdmac->imxdma_channel);
  265. }
  266. static int __init imxdma_probe(struct platform_device *pdev)
  267. {
  268. struct imxdma_engine *imxdma;
  269. int ret, i;
  270. imxdma = kzalloc(sizeof(*imxdma), GFP_KERNEL);
  271. if (!imxdma)
  272. return -ENOMEM;
  273. INIT_LIST_HEAD(&imxdma->dma_device.channels);
  274. dma_cap_set(DMA_SLAVE, imxdma->dma_device.cap_mask);
  275. dma_cap_set(DMA_CYCLIC, imxdma->dma_device.cap_mask);
  276. /* Initialize channel parameters */
  277. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  278. struct imxdma_channel *imxdmac = &imxdma->channel[i];
  279. imxdmac->imxdma_channel = imx_dma_request_by_prio("dmaengine",
  280. DMA_PRIO_MEDIUM);
  281. if ((int)imxdmac->channel < 0) {
  282. ret = -ENODEV;
  283. goto err_init;
  284. }
  285. imx_dma_setup_handlers(imxdmac->imxdma_channel,
  286. imxdma_irq_handler, imxdma_err_handler, imxdmac);
  287. imxdmac->imxdma = imxdma;
  288. spin_lock_init(&imxdmac->lock);
  289. imxdmac->chan.device = &imxdma->dma_device;
  290. imxdmac->channel = i;
  291. /* Add the channel to the DMAC list */
  292. list_add_tail(&imxdmac->chan.device_node, &imxdma->dma_device.channels);
  293. }
  294. imxdma->dev = &pdev->dev;
  295. imxdma->dma_device.dev = &pdev->dev;
  296. imxdma->dma_device.device_alloc_chan_resources = imxdma_alloc_chan_resources;
  297. imxdma->dma_device.device_free_chan_resources = imxdma_free_chan_resources;
  298. imxdma->dma_device.device_tx_status = imxdma_tx_status;
  299. imxdma->dma_device.device_prep_slave_sg = imxdma_prep_slave_sg;
  300. imxdma->dma_device.device_prep_dma_cyclic = imxdma_prep_dma_cyclic;
  301. imxdma->dma_device.device_control = imxdma_control;
  302. imxdma->dma_device.device_issue_pending = imxdma_issue_pending;
  303. platform_set_drvdata(pdev, imxdma);
  304. imxdma->dma_device.dev->dma_parms = &imxdma->dma_parms;
  305. dma_set_max_seg_size(imxdma->dma_device.dev, 0xffffff);
  306. ret = dma_async_device_register(&imxdma->dma_device);
  307. if (ret) {
  308. dev_err(&pdev->dev, "unable to register\n");
  309. goto err_init;
  310. }
  311. return 0;
  312. err_init:
  313. while (--i >= 0) {
  314. struct imxdma_channel *imxdmac = &imxdma->channel[i];
  315. imx_dma_free(imxdmac->imxdma_channel);
  316. }
  317. kfree(imxdma);
  318. return ret;
  319. }
  320. static int __exit imxdma_remove(struct platform_device *pdev)
  321. {
  322. struct imxdma_engine *imxdma = platform_get_drvdata(pdev);
  323. int i;
  324. dma_async_device_unregister(&imxdma->dma_device);
  325. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  326. struct imxdma_channel *imxdmac = &imxdma->channel[i];
  327. imx_dma_free(imxdmac->imxdma_channel);
  328. }
  329. kfree(imxdma);
  330. return 0;
  331. }
  332. static struct platform_driver imxdma_driver = {
  333. .driver = {
  334. .name = "imx-dma",
  335. },
  336. .remove = __exit_p(imxdma_remove),
  337. };
  338. static int __init imxdma_module_init(void)
  339. {
  340. return platform_driver_probe(&imxdma_driver, imxdma_probe);
  341. }
  342. subsys_initcall(imxdma_module_init);
  343. MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
  344. MODULE_DESCRIPTION("i.MX dma driver");
  345. MODULE_LICENSE("GPL");