blob: 3296a7337f25d972f436a8932fd306149438e4ea [file] [log] [blame]
Sascha Hauer1f1846c2010-10-06 10:25:55 +02001/*
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>
Axel Linf8de8f42011-08-30 15:08:24 +080017#include <linux/module.h>
Sascha Hauer1f1846c2010-10-06 10:25:55 +020018#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>
Paul Gortmaker5c45ad72011-07-31 16:14:17 -040027#include <linux/module.h>
Sascha Hauer1f1846c2010-10-06 10:25:55 +020028
29#include <asm/irq.h>
30#include <mach/dma-v1.h>
31#include <mach/hardware.h>
32
33struct imxdma_channel {
34 struct imxdma_engine *imxdma;
35 unsigned int channel;
36 unsigned int imxdma_channel;
37
38 enum dma_slave_buswidth word_size;
39 dma_addr_t per_address;
40 u32 watermark_level;
41 struct dma_chan chan;
42 spinlock_t lock;
43 struct dma_async_tx_descriptor desc;
44 dma_cookie_t last_completed;
45 enum dma_status status;
46 int dma_request;
47 struct scatterlist *sg_list;
48};
49
50#define MAX_DMA_CHANNELS 8
51
52struct imxdma_engine {
53 struct device *dev;
Sascha Hauer1e070a62011-01-12 13:14:37 +010054 struct device_dma_parameters dma_parms;
Sascha Hauer1f1846c2010-10-06 10:25:55 +020055 struct dma_device dma_device;
56 struct imxdma_channel channel[MAX_DMA_CHANNELS];
57};
58
59static struct imxdma_channel *to_imxdma_chan(struct dma_chan *chan)
60{
61 return container_of(chan, struct imxdma_channel, chan);
62}
63
64static void imxdma_handle(struct imxdma_channel *imxdmac)
65{
66 if (imxdmac->desc.callback)
67 imxdmac->desc.callback(imxdmac->desc.callback_param);
68 imxdmac->last_completed = imxdmac->desc.cookie;
69}
70
71static void imxdma_irq_handler(int channel, void *data)
72{
73 struct imxdma_channel *imxdmac = data;
74
75 imxdmac->status = DMA_SUCCESS;
76 imxdma_handle(imxdmac);
77}
78
79static void imxdma_err_handler(int channel, void *data, int error)
80{
81 struct imxdma_channel *imxdmac = data;
82
83 imxdmac->status = DMA_ERROR;
84 imxdma_handle(imxdmac);
85}
86
87static void imxdma_progression(int channel, void *data,
88 struct scatterlist *sg)
89{
90 struct imxdma_channel *imxdmac = data;
91
92 imxdmac->status = DMA_SUCCESS;
93 imxdma_handle(imxdmac);
94}
95
96static int imxdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
97 unsigned long arg)
98{
99 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
100 struct dma_slave_config *dmaengine_cfg = (void *)arg;
101 int ret;
102 unsigned int mode = 0;
103
104 switch (cmd) {
105 case DMA_TERMINATE_ALL:
106 imxdmac->status = DMA_ERROR;
107 imx_dma_disable(imxdmac->imxdma_channel);
108 return 0;
109 case DMA_SLAVE_CONFIG:
Vinod Kouldb8196d2011-10-13 22:34:23 +0530110 if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) {
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200111 imxdmac->per_address = dmaengine_cfg->src_addr;
112 imxdmac->watermark_level = dmaengine_cfg->src_maxburst;
113 imxdmac->word_size = dmaengine_cfg->src_addr_width;
114 } else {
115 imxdmac->per_address = dmaengine_cfg->dst_addr;
116 imxdmac->watermark_level = dmaengine_cfg->dst_maxburst;
117 imxdmac->word_size = dmaengine_cfg->dst_addr_width;
118 }
119
120 switch (imxdmac->word_size) {
121 case DMA_SLAVE_BUSWIDTH_1_BYTE:
122 mode = IMX_DMA_MEMSIZE_8;
123 break;
124 case DMA_SLAVE_BUSWIDTH_2_BYTES:
125 mode = IMX_DMA_MEMSIZE_16;
126 break;
127 default:
128 case DMA_SLAVE_BUSWIDTH_4_BYTES:
129 mode = IMX_DMA_MEMSIZE_32;
130 break;
131 }
132 ret = imx_dma_config_channel(imxdmac->imxdma_channel,
133 mode | IMX_DMA_TYPE_FIFO,
134 IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR,
135 imxdmac->dma_request, 1);
136
137 if (ret)
138 return ret;
139
Sascha Hauer6584cb82011-07-06 11:18:33 +0200140 imx_dma_config_burstlen(imxdmac->imxdma_channel,
141 imxdmac->watermark_level * imxdmac->word_size);
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200142
143 return 0;
144 default:
145 return -ENOSYS;
146 }
147
148 return -EINVAL;
149}
150
151static enum dma_status imxdma_tx_status(struct dma_chan *chan,
152 dma_cookie_t cookie,
153 struct dma_tx_state *txstate)
154{
155 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
156 dma_cookie_t last_used;
157 enum dma_status ret;
158
159 last_used = chan->cookie;
160
161 ret = dma_async_is_complete(cookie, imxdmac->last_completed, last_used);
162 dma_set_tx_state(txstate, imxdmac->last_completed, last_used, 0);
163
164 return ret;
165}
166
167static dma_cookie_t imxdma_assign_cookie(struct imxdma_channel *imxdma)
168{
169 dma_cookie_t cookie = imxdma->chan.cookie;
170
171 if (++cookie < 0)
172 cookie = 1;
173
174 imxdma->chan.cookie = cookie;
175 imxdma->desc.cookie = cookie;
176
177 return cookie;
178}
179
180static dma_cookie_t imxdma_tx_submit(struct dma_async_tx_descriptor *tx)
181{
182 struct imxdma_channel *imxdmac = to_imxdma_chan(tx->chan);
183 dma_cookie_t cookie;
184
185 spin_lock_irq(&imxdmac->lock);
186
187 cookie = imxdma_assign_cookie(imxdmac);
188
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200189 spin_unlock_irq(&imxdmac->lock);
190
191 return cookie;
192}
193
194static int imxdma_alloc_chan_resources(struct dma_chan *chan)
195{
196 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
197 struct imx_dma_data *data = chan->private;
198
199 imxdmac->dma_request = data->dma_request;
200
201 dma_async_tx_descriptor_init(&imxdmac->desc, chan);
202 imxdmac->desc.tx_submit = imxdma_tx_submit;
203 /* txd.flags will be overwritten in prep funcs */
204 imxdmac->desc.flags = DMA_CTRL_ACK;
205
206 imxdmac->status = DMA_SUCCESS;
207
208 return 0;
209}
210
211static void imxdma_free_chan_resources(struct dma_chan *chan)
212{
213 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
214
215 imx_dma_disable(imxdmac->imxdma_channel);
216
217 if (imxdmac->sg_list) {
218 kfree(imxdmac->sg_list);
219 imxdmac->sg_list = NULL;
220 }
221}
222
223static struct dma_async_tx_descriptor *imxdma_prep_slave_sg(
224 struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530225 unsigned int sg_len, enum dma_transfer_direction direction,
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200226 unsigned long flags)
227{
228 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
229 struct scatterlist *sg;
230 int i, ret, dma_length = 0;
231 unsigned int dmamode;
232
233 if (imxdmac->status == DMA_IN_PROGRESS)
234 return NULL;
235
236 imxdmac->status = DMA_IN_PROGRESS;
237
238 for_each_sg(sgl, sg, sg_len, i) {
239 dma_length += sg->length;
240 }
241
Vinod Kouldb8196d2011-10-13 22:34:23 +0530242 if (direction == DMA_DEV_TO_MEM)
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200243 dmamode = DMA_MODE_READ;
244 else
245 dmamode = DMA_MODE_WRITE;
246
Sascha Hauerd07102a2011-01-12 14:13:23 +0100247 switch (imxdmac->word_size) {
248 case DMA_SLAVE_BUSWIDTH_4_BYTES:
249 if (sgl->length & 3 || sgl->dma_address & 3)
250 return NULL;
251 break;
252 case DMA_SLAVE_BUSWIDTH_2_BYTES:
253 if (sgl->length & 1 || sgl->dma_address & 1)
254 return NULL;
255 break;
256 case DMA_SLAVE_BUSWIDTH_1_BYTE:
257 break;
258 default:
259 return NULL;
260 }
261
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200262 ret = imx_dma_setup_sg(imxdmac->imxdma_channel, sgl, sg_len,
263 dma_length, imxdmac->per_address, dmamode);
264 if (ret)
265 return NULL;
266
267 return &imxdmac->desc;
268}
269
270static struct dma_async_tx_descriptor *imxdma_prep_dma_cyclic(
271 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530272 size_t period_len, enum dma_transfer_direction direction)
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200273{
274 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
275 struct imxdma_engine *imxdma = imxdmac->imxdma;
276 int i, ret;
277 unsigned int periods = buf_len / period_len;
278 unsigned int dmamode;
279
280 dev_dbg(imxdma->dev, "%s channel: %d buf_len=%d period_len=%d\n",
281 __func__, imxdmac->channel, buf_len, period_len);
282
283 if (imxdmac->status == DMA_IN_PROGRESS)
284 return NULL;
285 imxdmac->status = DMA_IN_PROGRESS;
286
287 ret = imx_dma_setup_progression_handler(imxdmac->imxdma_channel,
288 imxdma_progression);
289 if (ret) {
290 dev_err(imxdma->dev, "Failed to setup the DMA handler\n");
291 return NULL;
292 }
293
294 if (imxdmac->sg_list)
295 kfree(imxdmac->sg_list);
296
297 imxdmac->sg_list = kcalloc(periods + 1,
298 sizeof(struct scatterlist), GFP_KERNEL);
299 if (!imxdmac->sg_list)
300 return NULL;
301
302 sg_init_table(imxdmac->sg_list, periods);
303
304 for (i = 0; i < periods; i++) {
305 imxdmac->sg_list[i].page_link = 0;
306 imxdmac->sg_list[i].offset = 0;
307 imxdmac->sg_list[i].dma_address = dma_addr;
308 imxdmac->sg_list[i].length = period_len;
309 dma_addr += period_len;
310 }
311
312 /* close the loop */
313 imxdmac->sg_list[periods].offset = 0;
314 imxdmac->sg_list[periods].length = 0;
315 imxdmac->sg_list[periods].page_link =
316 ((unsigned long)imxdmac->sg_list | 0x01) & ~0x02;
317
Vinod Kouldb8196d2011-10-13 22:34:23 +0530318 if (direction == DMA_DEV_TO_MEM)
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200319 dmamode = DMA_MODE_READ;
320 else
321 dmamode = DMA_MODE_WRITE;
322
323 ret = imx_dma_setup_sg(imxdmac->imxdma_channel, imxdmac->sg_list, periods,
324 IMX_DMA_LENGTH_LOOP, imxdmac->per_address, dmamode);
325 if (ret)
326 return NULL;
327
328 return &imxdmac->desc;
329}
330
331static void imxdma_issue_pending(struct dma_chan *chan)
332{
Sascha Hauer5b316872012-01-09 10:32:49 +0100333 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
334
335 if (imxdmac->status == DMA_IN_PROGRESS)
336 imx_dma_enable(imxdmac->imxdma_channel);
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200337}
338
339static int __init imxdma_probe(struct platform_device *pdev)
340{
341 struct imxdma_engine *imxdma;
342 int ret, i;
343
344 imxdma = kzalloc(sizeof(*imxdma), GFP_KERNEL);
345 if (!imxdma)
346 return -ENOMEM;
347
348 INIT_LIST_HEAD(&imxdma->dma_device.channels);
349
Sascha Hauerf8a356f2011-01-31 11:35:59 +0100350 dma_cap_set(DMA_SLAVE, imxdma->dma_device.cap_mask);
351 dma_cap_set(DMA_CYCLIC, imxdma->dma_device.cap_mask);
352
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200353 /* Initialize channel parameters */
354 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
355 struct imxdma_channel *imxdmac = &imxdma->channel[i];
356
357 imxdmac->imxdma_channel = imx_dma_request_by_prio("dmaengine",
358 DMA_PRIO_MEDIUM);
Sascha Hauer8267f162010-10-20 08:37:19 +0200359 if ((int)imxdmac->channel < 0) {
360 ret = -ENODEV;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200361 goto err_init;
Sascha Hauer8267f162010-10-20 08:37:19 +0200362 }
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200363
364 imx_dma_setup_handlers(imxdmac->imxdma_channel,
365 imxdma_irq_handler, imxdma_err_handler, imxdmac);
366
367 imxdmac->imxdma = imxdma;
368 spin_lock_init(&imxdmac->lock);
369
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200370 imxdmac->chan.device = &imxdma->dma_device;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200371 imxdmac->channel = i;
372
373 /* Add the channel to the DMAC list */
374 list_add_tail(&imxdmac->chan.device_node, &imxdma->dma_device.channels);
375 }
376
377 imxdma->dev = &pdev->dev;
378 imxdma->dma_device.dev = &pdev->dev;
379
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;
387
388 platform_set_drvdata(pdev, imxdma);
389
Sascha Hauer1e070a62011-01-12 13:14:37 +0100390 imxdma->dma_device.dev->dma_parms = &imxdma->dma_parms;
391 dma_set_max_seg_size(imxdma->dma_device.dev, 0xffffff);
392
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200393 ret = dma_async_device_register(&imxdma->dma_device);
394 if (ret) {
395 dev_err(&pdev->dev, "unable to register\n");
396 goto err_init;
397 }
398
399 return 0;
400
401err_init:
Axel Lincbeae412010-11-02 09:12:57 +0800402 while (--i >= 0) {
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200403 struct imxdma_channel *imxdmac = &imxdma->channel[i];
404 imx_dma_free(imxdmac->imxdma_channel);
405 }
406
407 kfree(imxdma);
408 return ret;
409}
410
411static int __exit imxdma_remove(struct platform_device *pdev)
412{
413 struct imxdma_engine *imxdma = platform_get_drvdata(pdev);
414 int i;
415
416 dma_async_device_unregister(&imxdma->dma_device);
417
418 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
419 struct imxdma_channel *imxdmac = &imxdma->channel[i];
420
421 imx_dma_free(imxdmac->imxdma_channel);
422 }
423
424 kfree(imxdma);
425
426 return 0;
427}
428
429static struct platform_driver imxdma_driver = {
430 .driver = {
431 .name = "imx-dma",
432 },
433 .remove = __exit_p(imxdma_remove),
434};
435
436static int __init imxdma_module_init(void)
437{
438 return platform_driver_probe(&imxdma_driver, imxdma_probe);
439}
440subsys_initcall(imxdma_module_init);
441
442MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
443MODULE_DESCRIPTION("i.MX dma driver");
444MODULE_LICENSE("GPL");