blob: 1536583eece0d0ed260604e8657b11cc81a323f6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/mach-imx/dma.c
3 *
4 * imx DMA registration and IRQ dispatching
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
Pavel Pisa999331a2006-04-02 16:58:37 +010010 * 2004-03-03 Sascha Hauer <sascha@saschahauer.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * initial version heavily inspired by
12 * linux/arch/arm/mach-pxa/dma.c
Pavel Pisa999331a2006-04-02 16:58:37 +010013 *
14 * 2005-04-17 Pavel Pisa <pisa@cmp.felk.cvut.cz>
15 * Changed to support scatter gather DMA
16 * by taking Russell's code from RiscPC
17 *
Pavel Pisafa3e6862006-06-22 22:21:03 +010018 * 2006-05-31 Pavel Pisa <pisa@cmp.felk.cvut.cz>
19 * Corrected error handling code.
20 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
Pavel Pisa999331a2006-04-02 16:58:37 +010023#undef DEBUG
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/interrupt.h>
29#include <linux/errno.h>
30
Russell Kingdcea83a2008-11-29 11:40:28 +000031#include <asm/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/system.h>
33#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010034#include <mach/hardware.h>
Russell Kingdcea83a2008-11-29 11:40:28 +000035#include <mach/dma.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010036#include <mach/imx-dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Pavel Pisa999331a2006-04-02 16:58:37 +010038struct imx_dma_channel imx_dma_channels[IMX_DMA_CHANNELS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Pavel Pisa999331a2006-04-02 16:58:37 +010040/*
41 * imx_dma_sg_next - prepare next chunk for scatter-gather DMA emulation
42 * @dma_ch: i.MX DMA channel number
43 * @lastcount: number of bytes transferred during last transfer
44 *
45 * Functions prepares DMA controller for next sg data chunk transfer.
46 * The @lastcount argument informs function about number of bytes transferred
47 * during last block. Zero value can be used for @lastcount to setup DMA
48 * for the first chunk.
49 */
50static inline int imx_dma_sg_next(imx_dmach_t dma_ch, unsigned int lastcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Pavel Pisa999331a2006-04-02 16:58:37 +010052 struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
53 unsigned int nextcount;
54 unsigned int nextaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Pavel Pisa999331a2006-04-02 16:58:37 +010056 if (!imxdma->name) {
57 printk(KERN_CRIT "%s: called for not allocated channel %d\n",
Harvey Harrison8e86f422008-03-04 15:08:02 -080058 __func__, dma_ch);
Pavel Pisa999331a2006-04-02 16:58:37 +010059 return 0;
60 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Pavel Pisa999331a2006-04-02 16:58:37 +010062 imxdma->resbytes -= lastcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Pavel Pisa999331a2006-04-02 16:58:37 +010064 if (!imxdma->sg) {
65 pr_debug("imxdma%d: no sg data\n", dma_ch);
66 return 0;
67 }
68
69 imxdma->sgbc += lastcount;
70 if ((imxdma->sgbc >= imxdma->sg->length) || !imxdma->resbytes) {
71 if ((imxdma->sgcount <= 1) || !imxdma->resbytes) {
72 pr_debug("imxdma%d: sg transfer limit reached\n",
73 dma_ch);
74 imxdma->sgcount=0;
75 imxdma->sg = NULL;
76 return 0;
77 } else {
78 imxdma->sgcount--;
79 imxdma->sg++;
80 imxdma->sgbc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82 }
Pavel Pisa999331a2006-04-02 16:58:37 +010083 nextcount = imxdma->sg->length - imxdma->sgbc;
84 nextaddr = imxdma->sg->dma_address + imxdma->sgbc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Pavel Pisa999331a2006-04-02 16:58:37 +010086 if(imxdma->resbytes < nextcount)
87 nextcount = imxdma->resbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Pavel Pisa999331a2006-04-02 16:58:37 +010089 if ((imxdma->dma_mode & DMA_MODE_MASK) == DMA_MODE_READ)
90 DAR(dma_ch) = nextaddr;
91 else
92 SAR(dma_ch) = nextaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Pavel Pisa999331a2006-04-02 16:58:37 +010094 CNTR(dma_ch) = nextcount;
95 pr_debug("imxdma%d: next sg chunk dst 0x%08x, src 0x%08x, size 0x%08x\n",
96 dma_ch, DAR(dma_ch), SAR(dma_ch), CNTR(dma_ch));
97
98 return nextcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Pavel Pisa999331a2006-04-02 16:58:37 +0100101/*
102 * imx_dma_setup_sg_base - scatter-gather DMA emulation
103 * @dma_ch: i.MX DMA channel number
104 * @sg: pointer to the scatter-gather list/vector
105 * @sgcount: scatter-gather list hungs count
106 *
107 * Functions sets up i.MX DMA state for emulated scatter-gather transfer
108 * and sets up channel registers to be ready for the first chunk
109 */
110static int
111imx_dma_setup_sg_base(imx_dmach_t dma_ch,
112 struct scatterlist *sg, unsigned int sgcount)
113{
114 struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
115
116 imxdma->sg = sg;
117 imxdma->sgcount = sgcount;
118 imxdma->sgbc = 0;
119 return imx_dma_sg_next(dma_ch, 0);
120}
121
122/**
123 * imx_dma_setup_single - setup i.MX DMA channel for linear memory to/from device transfer
124 * @dma_ch: i.MX DMA channel number
125 * @dma_address: the DMA/physical memory address of the linear data block
126 * to transfer
127 * @dma_length: length of the data block in bytes
128 * @dev_addr: physical device port address
129 * @dmamode: DMA transfer mode, %DMA_MODE_READ from the device to the memory
130 * or %DMA_MODE_WRITE from memory to the device
131 *
132 * The function setups DMA channel source and destination addresses for transfer
133 * specified by provided parameters. The scatter-gather emulation is disabled,
134 * because linear data block
Simon Arlott6cbdc8c2007-05-11 20:40:30 +0100135 * form the physical address range is transferred.
Pavel Pisa999331a2006-04-02 16:58:37 +0100136 * Return value: if incorrect parameters are provided -%EINVAL.
137 * Zero indicates success.
138 */
139int
140imx_dma_setup_single(imx_dmach_t dma_ch, dma_addr_t dma_address,
141 unsigned int dma_length, unsigned int dev_addr,
Russell Kingdcea83a2008-11-29 11:40:28 +0000142 unsigned int dmamode)
Pavel Pisa999331a2006-04-02 16:58:37 +0100143{
144 struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
145
146 imxdma->sg = NULL;
147 imxdma->sgcount = 0;
148 imxdma->dma_mode = dmamode;
149 imxdma->resbytes = dma_length;
150
151 if (!dma_address) {
152 printk(KERN_ERR "imxdma%d: imx_dma_setup_single null address\n",
153 dma_ch);
154 return -EINVAL;
155 }
156
157 if (!dma_length) {
158 printk(KERN_ERR "imxdma%d: imx_dma_setup_single zero length\n",
159 dma_ch);
160 return -EINVAL;
161 }
162
163 if ((dmamode & DMA_MODE_MASK) == DMA_MODE_READ) {
164 pr_debug("imxdma%d: mx_dma_setup_single2dev dma_addressg=0x%08x dma_length=%d dev_addr=0x%08x for read\n",
165 dma_ch, (unsigned int)dma_address, dma_length,
166 dev_addr);
167 SAR(dma_ch) = dev_addr;
168 DAR(dma_ch) = (unsigned int)dma_address;
169 } else if ((dmamode & DMA_MODE_MASK) == DMA_MODE_WRITE) {
170 pr_debug("imxdma%d: mx_dma_setup_single2dev dma_addressg=0x%08x dma_length=%d dev_addr=0x%08x for write\n",
171 dma_ch, (unsigned int)dma_address, dma_length,
172 dev_addr);
173 SAR(dma_ch) = (unsigned int)dma_address;
174 DAR(dma_ch) = dev_addr;
175 } else {
176 printk(KERN_ERR "imxdma%d: imx_dma_setup_single bad dmamode\n",
177 dma_ch);
178 return -EINVAL;
179 }
180
181 CNTR(dma_ch) = dma_length;
182
183 return 0;
184}
185
186/**
187 * imx_dma_setup_sg - setup i.MX DMA channel SG list to/from device transfer
188 * @dma_ch: i.MX DMA channel number
189 * @sg: pointer to the scatter-gather list/vector
190 * @sgcount: scatter-gather list hungs count
191 * @dma_length: total length of the transfer request in bytes
192 * @dev_addr: physical device port address
193 * @dmamode: DMA transfer mode, %DMA_MODE_READ from the device to the memory
194 * or %DMA_MODE_WRITE from memory to the device
195 *
Simon Arlott6cbdc8c2007-05-11 20:40:30 +0100196 * The function sets up DMA channel state and registers to be ready for transfer
Pavel Pisa999331a2006-04-02 16:58:37 +0100197 * specified by provided parameters. The scatter-gather emulation is set up
198 * according to the parameters.
199 *
200 * The full preparation of the transfer requires setup of more register
201 * by the caller before imx_dma_enable() can be called.
202 *
203 * %BLR(dma_ch) holds transfer burst length in bytes, 0 means 64 bytes
204 *
205 * %RSSR(dma_ch) has to be set to the DMA request line source %DMA_REQ_xxx
206 *
207 * %CCR(dma_ch) has to specify transfer parameters, the next settings is typical
208 * for linear or simple scatter-gather transfers if %DMA_MODE_READ is specified
209 *
210 * %CCR_DMOD_LINEAR | %CCR_DSIZ_32 | %CCR_SMOD_FIFO | %CCR_SSIZ_x
211 *
212 * The typical setup for %DMA_MODE_WRITE is specified by next options combination
213 *
214 * %CCR_SMOD_LINEAR | %CCR_SSIZ_32 | %CCR_DMOD_FIFO | %CCR_DSIZ_x
215 *
Simon Arlott6cbdc8c2007-05-11 20:40:30 +0100216 * Be careful here and do not mistakenly mix source and target device
Pavel Pisa999331a2006-04-02 16:58:37 +0100217 * port sizes constants, they are really different:
218 * %CCR_SSIZ_8, %CCR_SSIZ_16, %CCR_SSIZ_32,
219 * %CCR_DSIZ_8, %CCR_DSIZ_16, %CCR_DSIZ_32
220 *
221 * Return value: if incorrect parameters are provided -%EINVAL.
222 * Zero indicates success.
223 */
224int
225imx_dma_setup_sg(imx_dmach_t dma_ch,
226 struct scatterlist *sg, unsigned int sgcount, unsigned int dma_length,
Russell Kingdcea83a2008-11-29 11:40:28 +0000227 unsigned int dev_addr, unsigned int dmamode)
Pavel Pisa999331a2006-04-02 16:58:37 +0100228{
229 int res;
230 struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
231
232 imxdma->sg = NULL;
233 imxdma->sgcount = 0;
234 imxdma->dma_mode = dmamode;
235 imxdma->resbytes = dma_length;
236
237 if (!sg || !sgcount) {
238 printk(KERN_ERR "imxdma%d: imx_dma_setup_sg epty sg list\n",
239 dma_ch);
240 return -EINVAL;
241 }
242
243 if (!sg->length) {
244 printk(KERN_ERR "imxdma%d: imx_dma_setup_sg zero length\n",
245 dma_ch);
246 return -EINVAL;
247 }
248
249 if ((dmamode & DMA_MODE_MASK) == DMA_MODE_READ) {
250 pr_debug("imxdma%d: mx_dma_setup_sg2dev sg=%p sgcount=%d total length=%d dev_addr=0x%08x for read\n",
251 dma_ch, sg, sgcount, dma_length, dev_addr);
252 SAR(dma_ch) = dev_addr;
253 } else if ((dmamode & DMA_MODE_MASK) == DMA_MODE_WRITE) {
254 pr_debug("imxdma%d: mx_dma_setup_sg2dev sg=%p sgcount=%d total length=%d dev_addr=0x%08x for write\n",
255 dma_ch, sg, sgcount, dma_length, dev_addr);
256 DAR(dma_ch) = dev_addr;
257 } else {
258 printk(KERN_ERR "imxdma%d: imx_dma_setup_sg bad dmamode\n",
259 dma_ch);
260 return -EINVAL;
261 }
262
263 res = imx_dma_setup_sg_base(dma_ch, sg, sgcount);
264 if (res <= 0) {
265 printk(KERN_ERR "imxdma%d: no sg chunk ready\n", dma_ch);
266 return -EINVAL;
267 }
268
269 return 0;
270}
271
272/**
273 * imx_dma_setup_handlers - setup i.MX DMA channel end and error notification handlers
274 * @dma_ch: i.MX DMA channel number
275 * @irq_handler: the pointer to the function called if the transfer
276 * ends successfully
277 * @err_handler: the pointer to the function called if the premature
278 * end caused by error occurs
279 * @data: user specified value to be passed to the handlers
280 */
281int
282imx_dma_setup_handlers(imx_dmach_t dma_ch,
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700283 void (*irq_handler) (int, void *),
284 void (*err_handler) (int, void *, int),
Pavel Pisa999331a2006-04-02 16:58:37 +0100285 void *data)
286{
287 struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
288 unsigned long flags;
289
290 if (!imxdma->name) {
291 printk(KERN_CRIT "%s: called for not allocated channel %d\n",
Harvey Harrison8e86f422008-03-04 15:08:02 -0800292 __func__, dma_ch);
Pavel Pisa999331a2006-04-02 16:58:37 +0100293 return -ENODEV;
294 }
295
296 local_irq_save(flags);
297 DISR = (1 << dma_ch);
298 imxdma->irq_handler = irq_handler;
299 imxdma->err_handler = err_handler;
300 imxdma->data = data;
301 local_irq_restore(flags);
302 return 0;
303}
304
305/**
306 * imx_dma_enable - function to start i.MX DMA channel operation
307 * @dma_ch: i.MX DMA channel number
308 *
309 * The channel has to be allocated by driver through imx_dma_request()
310 * or imx_dma_request_by_prio() function.
311 * The transfer parameters has to be set to the channel registers through
312 * call of the imx_dma_setup_single() or imx_dma_setup_sg() function
313 * and registers %BLR(dma_ch), %RSSR(dma_ch) and %CCR(dma_ch) has to
314 * be set prior this function call by the channel user.
315 */
316void imx_dma_enable(imx_dmach_t dma_ch)
317{
318 struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
319 unsigned long flags;
320
321 pr_debug("imxdma%d: imx_dma_enable\n", dma_ch);
322
323 if (!imxdma->name) {
324 printk(KERN_CRIT "%s: called for not allocated channel %d\n",
Harvey Harrison8e86f422008-03-04 15:08:02 -0800325 __func__, dma_ch);
Pavel Pisa999331a2006-04-02 16:58:37 +0100326 return;
327 }
328
329 local_irq_save(flags);
330 DISR = (1 << dma_ch);
331 DIMR &= ~(1 << dma_ch);
332 CCR(dma_ch) |= CCR_CEN;
333 local_irq_restore(flags);
334}
335
336/**
337 * imx_dma_disable - stop, finish i.MX DMA channel operatin
338 * @dma_ch: i.MX DMA channel number
339 */
340void imx_dma_disable(imx_dmach_t dma_ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 unsigned long flags;
343
Pavel Pisa999331a2006-04-02 16:58:37 +0100344 pr_debug("imxdma%d: imx_dma_disable\n", dma_ch);
345
346 local_irq_save(flags);
347 DIMR |= (1 << dma_ch);
348 CCR(dma_ch) &= ~CCR_CEN;
349 DISR = (1 << dma_ch);
350 local_irq_restore(flags);
351}
352
353/**
354 * imx_dma_request - request/allocate specified channel number
355 * @dma_ch: i.MX DMA channel number
356 * @name: the driver/caller own non-%NULL identification
357 */
358int imx_dma_request(imx_dmach_t dma_ch, const char *name)
359{
360 struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
361 unsigned long flags;
362
363 /* basic sanity checks */
364 if (!name)
365 return -EINVAL;
366
367 if (dma_ch >= IMX_DMA_CHANNELS) {
368 printk(KERN_CRIT "%s: called for non-existed channel %d\n",
Harvey Harrison8e86f422008-03-04 15:08:02 -0800369 __func__, dma_ch);
Pavel Pisa999331a2006-04-02 16:58:37 +0100370 return -EINVAL;
371 }
372
373 local_irq_save(flags);
374 if (imxdma->name) {
375 local_irq_restore(flags);
376 return -ENODEV;
377 }
378
379 imxdma->name = name;
380 imxdma->irq_handler = NULL;
381 imxdma->err_handler = NULL;
382 imxdma->data = NULL;
383 imxdma->sg = NULL;
384 local_irq_restore(flags);
385 return 0;
386}
387
388/**
389 * imx_dma_free - release previously acquired channel
390 * @dma_ch: i.MX DMA channel number
391 */
392void imx_dma_free(imx_dmach_t dma_ch)
393{
394 unsigned long flags;
395 struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
396
397 if (!imxdma->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 printk(KERN_CRIT
399 "%s: trying to free channel %d which is already freed\n",
Harvey Harrison8e86f422008-03-04 15:08:02 -0800400 __func__, dma_ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return;
402 }
403
404 local_irq_save(flags);
Pavel Pisa999331a2006-04-02 16:58:37 +0100405 /* Disable interrupts */
406 DIMR |= (1 << dma_ch);
407 CCR(dma_ch) &= ~CCR_CEN;
408 imxdma->name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 local_irq_restore(flags);
410}
411
Pavel Pisa999331a2006-04-02 16:58:37 +0100412/**
413 * imx_dma_request_by_prio - find and request some of free channels best suiting requested priority
Pavel Pisa999331a2006-04-02 16:58:37 +0100414 * @name: the driver/caller own non-%NULL identification
415 * @prio: one of the hardware distinguished priority level:
416 * %DMA_PRIO_HIGH, %DMA_PRIO_MEDIUM, %DMA_PRIO_LOW
417 *
418 * This function tries to find free channel in the specified priority group
419 * if the priority cannot be achieved it tries to look for free channel
420 * in the higher and then even lower priority groups.
421 *
422 * Return value: If there is no free channel to allocate, -%ENODEV is returned.
Paulius Zaleckasf7def13e2008-06-25 13:25:13 +0100423 * On successful allocation channel is returned.
Pavel Pisa999331a2006-04-02 16:58:37 +0100424 */
Paulius Zaleckasf7def13e2008-06-25 13:25:13 +0100425imx_dmach_t imx_dma_request_by_prio(const char *name, imx_dma_prio prio)
Pavel Pisa999331a2006-04-02 16:58:37 +0100426{
427 int i;
428 int best;
429
430 switch (prio) {
431 case (DMA_PRIO_HIGH):
432 best = 8;
433 break;
434 case (DMA_PRIO_MEDIUM):
435 best = 4;
436 break;
437 case (DMA_PRIO_LOW):
438 default:
439 best = 0;
440 break;
441 }
442
443 for (i = best; i < IMX_DMA_CHANNELS; i++) {
444 if (!imx_dma_request(i, name)) {
Paulius Zaleckasf7def13e2008-06-25 13:25:13 +0100445 return i;
Pavel Pisa999331a2006-04-02 16:58:37 +0100446 }
447 }
448
449 for (i = best - 1; i >= 0; i--) {
450 if (!imx_dma_request(i, name)) {
Paulius Zaleckasf7def13e2008-06-25 13:25:13 +0100451 return i;
Pavel Pisa999331a2006-04-02 16:58:37 +0100452 }
453 }
454
Harvey Harrison8e86f422008-03-04 15:08:02 -0800455 printk(KERN_ERR "%s: no free DMA channel found\n", __func__);
Pavel Pisa999331a2006-04-02 16:58:37 +0100456
457 return -ENODEV;
458}
459
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700460static irqreturn_t dma_err_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 int i, disr = DISR;
Pavel Pisa999331a2006-04-02 16:58:37 +0100463 struct imx_dma_channel *channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 unsigned int err_mask = DBTOSR | DRTOSR | DSESR | DBOSR;
Pavel Pisafa3e6862006-06-22 22:21:03 +0100465 int errcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Pavel Pisafa3e6862006-06-22 22:21:03 +0100467 DISR = disr & err_mask;
Pavel Pisa999331a2006-04-02 16:58:37 +0100468 for (i = 0; i < IMX_DMA_CHANNELS; i++) {
Pavel Pisafa3e6862006-06-22 22:21:03 +0100469 if(!(err_mask & (1 << i)))
470 continue;
Pavel Pisa999331a2006-04-02 16:58:37 +0100471 channel = &imx_dma_channels[i];
Pavel Pisafa3e6862006-06-22 22:21:03 +0100472 errcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Pavel Pisafa3e6862006-06-22 22:21:03 +0100474 if (DBTOSR & (1 << i)) {
475 DBTOSR = (1 << i);
476 errcode |= IMX_DMA_ERR_BURST;
477 }
478 if (DRTOSR & (1 << i)) {
479 DRTOSR = (1 << i);
480 errcode |= IMX_DMA_ERR_REQUEST;
481 }
482 if (DSESR & (1 << i)) {
483 DSESR = (1 << i);
484 errcode |= IMX_DMA_ERR_TRANSFER;
485 }
486 if (DBOSR & (1 << i)) {
487 DBOSR = (1 << i);
488 errcode |= IMX_DMA_ERR_BUFFER;
489 }
490
491 /*
492 * The cleaning of @sg field would be questionable
493 * there, because its value can help to compute
Simon Arlott6cbdc8c2007-05-11 20:40:30 +0100494 * remaining/transferred bytes count in the handler
Pavel Pisafa3e6862006-06-22 22:21:03 +0100495 */
496 /*imx_dma_channels[i].sg = NULL;*/
497
498 if (channel->name && channel->err_handler) {
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700499 channel->err_handler(i, channel->data, errcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 continue;
501 }
502
Pavel Pisa999331a2006-04-02 16:58:37 +0100503 imx_dma_channels[i].sg = NULL;
504
Pavel Pisafa3e6862006-06-22 22:21:03 +0100505 printk(KERN_WARNING
506 "DMA timeout on channel %d (%s) -%s%s%s%s\n",
507 i, channel->name,
508 errcode&IMX_DMA_ERR_BURST? " burst":"",
509 errcode&IMX_DMA_ERR_REQUEST? " request":"",
510 errcode&IMX_DMA_ERR_TRANSFER? " transfer":"",
511 errcode&IMX_DMA_ERR_BUFFER? " buffer":"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 return IRQ_HANDLED;
514}
515
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700516static irqreturn_t dma_irq_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 int i, disr = DISR;
519
Pavel Pisa999331a2006-04-02 16:58:37 +0100520 pr_debug("imxdma: dma_irq_handler called, disr=0x%08x\n",
521 disr);
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 DISR = disr;
Pavel Pisa999331a2006-04-02 16:58:37 +0100524 for (i = 0; i < IMX_DMA_CHANNELS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (disr & (1 << i)) {
Pavel Pisa999331a2006-04-02 16:58:37 +0100526 struct imx_dma_channel *channel = &imx_dma_channels[i];
527 if (channel->name) {
528 if (imx_dma_sg_next(i, CNTR(i))) {
529 CCR(i) &= ~CCR_CEN;
530 mb();
531 CCR(i) |= CCR_CEN;
532 } else {
533 if (channel->irq_handler)
534 channel->irq_handler(i,
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700535 channel->data);
Pavel Pisa999331a2006-04-02 16:58:37 +0100536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 } else {
538 /*
539 * IRQ for an unregistered DMA channel:
540 * let's clear the interrupts and disable it.
541 */
542 printk(KERN_WARNING
543 "spurious IRQ for DMA channel %d\n", i);
544 }
545 }
546 }
547 return IRQ_HANDLED;
548}
549
Pavel Pisa999331a2006-04-02 16:58:37 +0100550static int __init imx_dma_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 int ret;
Pavel Pisa999331a2006-04-02 16:58:37 +0100553 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /* reset DMA module */
556 DCR = DCR_DRST;
557
558 ret = request_irq(DMA_INT, dma_irq_handler, 0, "DMA", NULL);
559 if (ret) {
560 printk(KERN_CRIT "Wow! Can't register IRQ for DMA\n");
561 return ret;
562 }
563
564 ret = request_irq(DMA_ERR, dma_err_handler, 0, "DMA", NULL);
565 if (ret) {
566 printk(KERN_CRIT "Wow! Can't register ERRIRQ for DMA\n");
567 free_irq(DMA_INT, NULL);
568 }
569
570 /* enable DMA module */
571 DCR = DCR_DEN;
572
573 /* clear all interrupts */
Pavel Pisa999331a2006-04-02 16:58:37 +0100574 DISR = (1 << IMX_DMA_CHANNELS) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 /* enable interrupts */
Pavel Pisa999331a2006-04-02 16:58:37 +0100577 DIMR = (1 << IMX_DMA_CHANNELS) - 1;
578
579 for (i = 0; i < IMX_DMA_CHANNELS; i++) {
580 imx_dma_channels[i].sg = NULL;
581 imx_dma_channels[i].dma_num = i;
582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 return ret;
585}
586
587arch_initcall(imx_dma_init);
588
Pavel Pisa999331a2006-04-02 16:58:37 +0100589EXPORT_SYMBOL(imx_dma_setup_single);
590EXPORT_SYMBOL(imx_dma_setup_sg);
591EXPORT_SYMBOL(imx_dma_setup_handlers);
592EXPORT_SYMBOL(imx_dma_enable);
593EXPORT_SYMBOL(imx_dma_disable);
594EXPORT_SYMBOL(imx_dma_request);
595EXPORT_SYMBOL(imx_dma_free);
596EXPORT_SYMBOL(imx_dma_request_by_prio);
597EXPORT_SYMBOL(imx_dma_channels);