imx-dma.c 11 KB

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