2 * drivers/dma/imx-dma.c
4 * This file contains a driver for the Freescale i.MX DMA engine
7 * Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
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:
13 * http://www.opensource.org/licenses/gpl-license.html
14 * http://www.gnu.org/copyleft/gpl.html
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/types.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>
29 #include <mach/dma-v1.h>
30 #include <mach/hardware.h>
32 struct imxdma_channel {
33 struct imxdma_engine *imxdma;
35 unsigned int imxdma_channel;
37 enum dma_slave_buswidth word_size;
38 dma_addr_t per_address;
42 struct dma_async_tx_descriptor desc;
43 dma_cookie_t last_completed;
44 enum dma_status status;
46 struct scatterlist *sg_list;
49 #define MAX_DMA_CHANNELS 8
51 struct imxdma_engine {
53 struct device_dma_parameters dma_parms;
54 struct dma_device dma_device;
55 struct imxdma_channel channel[MAX_DMA_CHANNELS];
58 static struct imxdma_channel *to_imxdma_chan(struct dma_chan *chan)
60 return container_of(chan, struct imxdma_channel, chan);
63 static void imxdma_handle(struct imxdma_channel *imxdmac)
65 if (imxdmac->desc.callback)
66 imxdmac->desc.callback(imxdmac->desc.callback_param);
67 imxdmac->last_completed = imxdmac->desc.cookie;
70 static void imxdma_irq_handler(int channel, void *data)
72 struct imxdma_channel *imxdmac = data;
74 imxdmac->status = DMA_SUCCESS;
75 imxdma_handle(imxdmac);
78 static void imxdma_err_handler(int channel, void *data, int error)
80 struct imxdma_channel *imxdmac = data;
82 imxdmac->status = DMA_ERROR;
83 imxdma_handle(imxdmac);
86 static void imxdma_progression(int channel, void *data,
87 struct scatterlist *sg)
89 struct imxdma_channel *imxdmac = data;
91 imxdmac->status = DMA_SUCCESS;
92 imxdma_handle(imxdmac);
95 static int imxdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
98 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
99 struct dma_slave_config *dmaengine_cfg = (void *)arg;
101 unsigned int mode = 0;
104 case DMA_TERMINATE_ALL:
105 imxdmac->status = DMA_ERROR;
106 imx_dma_disable(imxdmac->imxdma_channel);
108 case DMA_SLAVE_CONFIG:
109 if (dmaengine_cfg->direction == DMA_FROM_DEVICE) {
110 imxdmac->per_address = dmaengine_cfg->src_addr;
111 imxdmac->watermark_level = dmaengine_cfg->src_maxburst;
112 imxdmac->word_size = dmaengine_cfg->src_addr_width;
114 imxdmac->per_address = dmaengine_cfg->dst_addr;
115 imxdmac->watermark_level = dmaengine_cfg->dst_maxburst;
116 imxdmac->word_size = dmaengine_cfg->dst_addr_width;
119 switch (imxdmac->word_size) {
120 case DMA_SLAVE_BUSWIDTH_1_BYTE:
121 mode = IMX_DMA_MEMSIZE_8;
123 case DMA_SLAVE_BUSWIDTH_2_BYTES:
124 mode = IMX_DMA_MEMSIZE_16;
127 case DMA_SLAVE_BUSWIDTH_4_BYTES:
128 mode = IMX_DMA_MEMSIZE_32;
131 ret = imx_dma_config_channel(imxdmac->imxdma_channel,
132 mode | IMX_DMA_TYPE_FIFO,
133 IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR,
134 imxdmac->dma_request, 1);
139 imx_dma_config_burstlen(imxdmac->imxdma_channel,
140 imxdmac->watermark_level * imxdmac->word_size);
150 static enum dma_status imxdma_tx_status(struct dma_chan *chan,
152 struct dma_tx_state *txstate)
154 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
155 dma_cookie_t last_used;
158 last_used = chan->cookie;
160 ret = dma_async_is_complete(cookie, imxdmac->last_completed, last_used);
161 dma_set_tx_state(txstate, imxdmac->last_completed, last_used, 0);
166 static dma_cookie_t imxdma_assign_cookie(struct imxdma_channel *imxdma)
168 dma_cookie_t cookie = imxdma->chan.cookie;
173 imxdma->chan.cookie = cookie;
174 imxdma->desc.cookie = cookie;
179 static dma_cookie_t imxdma_tx_submit(struct dma_async_tx_descriptor *tx)
181 struct imxdma_channel *imxdmac = to_imxdma_chan(tx->chan);
184 spin_lock_irq(&imxdmac->lock);
186 cookie = imxdma_assign_cookie(imxdmac);
188 imx_dma_enable(imxdmac->imxdma_channel);
190 spin_unlock_irq(&imxdmac->lock);
195 static int imxdma_alloc_chan_resources(struct dma_chan *chan)
197 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
198 struct imx_dma_data *data = chan->private;
200 imxdmac->dma_request = data->dma_request;
202 dma_async_tx_descriptor_init(&imxdmac->desc, chan);
203 imxdmac->desc.tx_submit = imxdma_tx_submit;
204 /* txd.flags will be overwritten in prep funcs */
205 imxdmac->desc.flags = DMA_CTRL_ACK;
207 imxdmac->status = DMA_SUCCESS;
212 static void imxdma_free_chan_resources(struct dma_chan *chan)
214 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
216 imx_dma_disable(imxdmac->imxdma_channel);
218 if (imxdmac->sg_list) {
219 kfree(imxdmac->sg_list);
220 imxdmac->sg_list = NULL;
224 static struct dma_async_tx_descriptor *imxdma_prep_slave_sg(
225 struct dma_chan *chan, struct scatterlist *sgl,
226 unsigned int sg_len, enum dma_data_direction direction,
229 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
230 struct scatterlist *sg;
231 int i, ret, dma_length = 0;
232 unsigned int dmamode;
234 if (imxdmac->status == DMA_IN_PROGRESS)
237 imxdmac->status = DMA_IN_PROGRESS;
239 for_each_sg(sgl, sg, sg_len, i) {
240 dma_length += sg->length;
243 if (direction == DMA_FROM_DEVICE)
244 dmamode = DMA_MODE_READ;
246 dmamode = DMA_MODE_WRITE;
248 switch (imxdmac->word_size) {
249 case DMA_SLAVE_BUSWIDTH_4_BYTES:
250 if (sgl->length & 3 || sgl->dma_address & 3)
253 case DMA_SLAVE_BUSWIDTH_2_BYTES:
254 if (sgl->length & 1 || sgl->dma_address & 1)
257 case DMA_SLAVE_BUSWIDTH_1_BYTE:
263 ret = imx_dma_setup_sg(imxdmac->imxdma_channel, sgl, sg_len,
264 dma_length, imxdmac->per_address, dmamode);
268 return &imxdmac->desc;
271 static struct dma_async_tx_descriptor *imxdma_prep_dma_cyclic(
272 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
273 size_t period_len, enum dma_data_direction direction)
275 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
276 struct imxdma_engine *imxdma = imxdmac->imxdma;
278 unsigned int periods = buf_len / period_len;
279 unsigned int dmamode;
281 dev_dbg(imxdma->dev, "%s channel: %d buf_len=%d period_len=%d\n",
282 __func__, imxdmac->channel, buf_len, period_len);
284 if (imxdmac->status == DMA_IN_PROGRESS)
286 imxdmac->status = DMA_IN_PROGRESS;
288 ret = imx_dma_setup_progression_handler(imxdmac->imxdma_channel,
291 dev_err(imxdma->dev, "Failed to setup the DMA handler\n");
295 if (imxdmac->sg_list)
296 kfree(imxdmac->sg_list);
298 imxdmac->sg_list = kcalloc(periods + 1,
299 sizeof(struct scatterlist), GFP_KERNEL);
300 if (!imxdmac->sg_list)
303 sg_init_table(imxdmac->sg_list, periods);
305 for (i = 0; i < periods; i++) {
306 imxdmac->sg_list[i].page_link = 0;
307 imxdmac->sg_list[i].offset = 0;
308 imxdmac->sg_list[i].dma_address = dma_addr;
309 imxdmac->sg_list[i].length = period_len;
310 dma_addr += period_len;
314 imxdmac->sg_list[periods].offset = 0;
315 imxdmac->sg_list[periods].length = 0;
316 imxdmac->sg_list[periods].page_link =
317 ((unsigned long)imxdmac->sg_list | 0x01) & ~0x02;
319 if (direction == DMA_FROM_DEVICE)
320 dmamode = DMA_MODE_READ;
322 dmamode = DMA_MODE_WRITE;
324 ret = imx_dma_setup_sg(imxdmac->imxdma_channel, imxdmac->sg_list, periods,
325 IMX_DMA_LENGTH_LOOP, imxdmac->per_address, dmamode);
329 return &imxdmac->desc;
332 static void imxdma_issue_pending(struct dma_chan *chan)
335 * Nothing to do. We only have a single descriptor
339 static int __init imxdma_probe(struct platform_device *pdev)
341 struct imxdma_engine *imxdma;
344 imxdma = kzalloc(sizeof(*imxdma), GFP_KERNEL);
348 INIT_LIST_HEAD(&imxdma->dma_device.channels);
350 dma_cap_set(DMA_SLAVE, imxdma->dma_device.cap_mask);
351 dma_cap_set(DMA_CYCLIC, imxdma->dma_device.cap_mask);
353 /* Initialize channel parameters */
354 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
355 struct imxdma_channel *imxdmac = &imxdma->channel[i];
357 imxdmac->imxdma_channel = imx_dma_request_by_prio("dmaengine",
359 if ((int)imxdmac->channel < 0) {
364 imx_dma_setup_handlers(imxdmac->imxdma_channel,
365 imxdma_irq_handler, imxdma_err_handler, imxdmac);
367 imxdmac->imxdma = imxdma;
368 spin_lock_init(&imxdmac->lock);
370 imxdmac->chan.device = &imxdma->dma_device;
371 imxdmac->channel = i;
373 /* Add the channel to the DMAC list */
374 list_add_tail(&imxdmac->chan.device_node, &imxdma->dma_device.channels);
377 imxdma->dev = &pdev->dev;
378 imxdma->dma_device.dev = &pdev->dev;
380 imxdma->dma_device.device_alloc_chan_resources = imxdma_alloc_chan_resources;
381 imxdma->dma_device.device_free_chan_resources = imxdma_free_chan_resources;
382 imxdma->dma_device.device_tx_status = imxdma_tx_status;
383 imxdma->dma_device.device_prep_slave_sg = imxdma_prep_slave_sg;
384 imxdma->dma_device.device_prep_dma_cyclic = imxdma_prep_dma_cyclic;
385 imxdma->dma_device.device_control = imxdma_control;
386 imxdma->dma_device.device_issue_pending = imxdma_issue_pending;
388 platform_set_drvdata(pdev, imxdma);
390 imxdma->dma_device.dev->dma_parms = &imxdma->dma_parms;
391 dma_set_max_seg_size(imxdma->dma_device.dev, 0xffffff);
393 ret = dma_async_device_register(&imxdma->dma_device);
395 dev_err(&pdev->dev, "unable to register\n");
403 struct imxdma_channel *imxdmac = &imxdma->channel[i];
404 imx_dma_free(imxdmac->imxdma_channel);
411 static int __exit imxdma_remove(struct platform_device *pdev)
413 struct imxdma_engine *imxdma = platform_get_drvdata(pdev);
416 dma_async_device_unregister(&imxdma->dma_device);
418 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
419 struct imxdma_channel *imxdmac = &imxdma->channel[i];
421 imx_dma_free(imxdmac->imxdma_channel);
429 static struct platform_driver imxdma_driver = {
433 .remove = __exit_p(imxdma_remove),
436 static int __init imxdma_module_init(void)
438 return platform_driver_probe(&imxdma_driver, imxdma_probe);
440 subsys_initcall(imxdma_module_init);
442 MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
443 MODULE_DESCRIPTION("i.MX dma driver");
444 MODULE_LICENSE("GPL");