imx-dma.c 11 KB

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