imx-dma.c 11 KB

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