2 * linux/arch/arm/mach-imx/dma.c
4 * imx DMA registration and IRQ dispatching
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.
10 * 2004-03-03 Sascha Hauer <sascha@saschahauer.de>
11 * initial version heavily inspired by
12 * linux/arch/arm/mach-pxa/dma.c
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
18 * 2006-05-31 Pavel Pisa <pisa@cmp.felk.cvut.cz>
19 * Corrected error handling code.
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/interrupt.h>
29 #include <linux/errno.h>
31 #include <asm/scatterlist.h>
32 #include <asm/system.h>
34 #include <mach/hardware.h>
36 #include <mach/imx-dma.h>
38 struct imx_dma_channel imx_dma_channels[IMX_DMA_CHANNELS];
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
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.
50 static inline int imx_dma_sg_next(imx_dmach_t dma_ch, unsigned int lastcount)
52 struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
53 unsigned int nextcount;
54 unsigned int nextaddr;
57 printk(KERN_CRIT "%s: called for not allocated channel %d\n",
62 imxdma->resbytes -= lastcount;
65 pr_debug("imxdma%d: no sg data\n", dma_ch);
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",
83 nextcount = imxdma->sg->length - imxdma->sgbc;
84 nextaddr = imxdma->sg->dma_address + imxdma->sgbc;
86 if(imxdma->resbytes < nextcount)
87 nextcount = imxdma->resbytes;
89 if ((imxdma->dma_mode & DMA_MODE_MASK) == DMA_MODE_READ)
90 DAR(dma_ch) = nextaddr;
92 SAR(dma_ch) = nextaddr;
94 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));
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
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
111 imx_dma_setup_sg_base(imx_dmach_t dma_ch,
112 struct scatterlist *sg, unsigned int sgcount)
114 struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
117 imxdma->sgcount = sgcount;
119 return imx_dma_sg_next(dma_ch, 0);
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
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
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
135 * form the physical address range is transferred.
136 * Return value: if incorrect parameters are provided -%EINVAL.
137 * Zero indicates success.
140 imx_dma_setup_single(imx_dmach_t dma_ch, dma_addr_t dma_address,
141 unsigned int dma_length, unsigned int dev_addr,
142 unsigned int dmamode)
144 struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
148 imxdma->dma_mode = dmamode;
149 imxdma->resbytes = dma_length;
152 printk(KERN_ERR "imxdma%d: imx_dma_setup_single null address\n",
158 printk(KERN_ERR "imxdma%d: imx_dma_setup_single zero length\n",
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,
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,
173 SAR(dma_ch) = (unsigned int)dma_address;
174 DAR(dma_ch) = dev_addr;
176 printk(KERN_ERR "imxdma%d: imx_dma_setup_single bad dmamode\n",
181 CNTR(dma_ch) = dma_length;
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
196 * The function sets up DMA channel state and registers to be ready for transfer
197 * specified by provided parameters. The scatter-gather emulation is set up
198 * according to the parameters.
200 * The full preparation of the transfer requires setup of more register
201 * by the caller before imx_dma_enable() can be called.
203 * %BLR(dma_ch) holds transfer burst length in bytes, 0 means 64 bytes
205 * %RSSR(dma_ch) has to be set to the DMA request line source %DMA_REQ_xxx
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
210 * %CCR_DMOD_LINEAR | %CCR_DSIZ_32 | %CCR_SMOD_FIFO | %CCR_SSIZ_x
212 * The typical setup for %DMA_MODE_WRITE is specified by next options combination
214 * %CCR_SMOD_LINEAR | %CCR_SSIZ_32 | %CCR_DMOD_FIFO | %CCR_DSIZ_x
216 * Be careful here and do not mistakenly mix source and target device
217 * 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
221 * Return value: if incorrect parameters are provided -%EINVAL.
222 * Zero indicates success.
225 imx_dma_setup_sg(imx_dmach_t dma_ch,
226 struct scatterlist *sg, unsigned int sgcount, unsigned int dma_length,
227 unsigned int dev_addr, unsigned int dmamode)
230 struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
234 imxdma->dma_mode = dmamode;
235 imxdma->resbytes = dma_length;
237 if (!sg || !sgcount) {
238 printk(KERN_ERR "imxdma%d: imx_dma_setup_sg epty sg list\n",
244 printk(KERN_ERR "imxdma%d: imx_dma_setup_sg zero length\n",
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;
258 printk(KERN_ERR "imxdma%d: imx_dma_setup_sg bad dmamode\n",
263 res = imx_dma_setup_sg_base(dma_ch, sg, sgcount);
265 printk(KERN_ERR "imxdma%d: no sg chunk ready\n", dma_ch);
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
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
282 imx_dma_setup_handlers(imx_dmach_t dma_ch,
283 void (*irq_handler) (int, void *),
284 void (*err_handler) (int, void *, int),
287 struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
291 printk(KERN_CRIT "%s: called for not allocated channel %d\n",
296 local_irq_save(flags);
297 DISR = (1 << dma_ch);
298 imxdma->irq_handler = irq_handler;
299 imxdma->err_handler = err_handler;
301 local_irq_restore(flags);
306 * imx_dma_enable - function to start i.MX DMA channel operation
307 * @dma_ch: i.MX DMA channel number
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.
316 void imx_dma_enable(imx_dmach_t dma_ch)
318 struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
321 pr_debug("imxdma%d: imx_dma_enable\n", dma_ch);
324 printk(KERN_CRIT "%s: called for not allocated channel %d\n",
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);
337 * imx_dma_disable - stop, finish i.MX DMA channel operatin
338 * @dma_ch: i.MX DMA channel number
340 void imx_dma_disable(imx_dmach_t dma_ch)
344 pr_debug("imxdma%d: imx_dma_disable\n", dma_ch);
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);
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
358 int imx_dma_request(imx_dmach_t dma_ch, const char *name)
360 struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
363 /* basic sanity checks */
367 if (dma_ch >= IMX_DMA_CHANNELS) {
368 printk(KERN_CRIT "%s: called for non-existed channel %d\n",
373 local_irq_save(flags);
375 local_irq_restore(flags);
380 imxdma->irq_handler = NULL;
381 imxdma->err_handler = NULL;
384 local_irq_restore(flags);
389 * imx_dma_free - release previously acquired channel
390 * @dma_ch: i.MX DMA channel number
392 void imx_dma_free(imx_dmach_t dma_ch)
395 struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
399 "%s: trying to free channel %d which is already freed\n",
404 local_irq_save(flags);
405 /* Disable interrupts */
406 DIMR |= (1 << dma_ch);
407 CCR(dma_ch) &= ~CCR_CEN;
409 local_irq_restore(flags);
413 * imx_dma_request_by_prio - find and request some of free channels best suiting requested priority
414 * @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
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.
422 * Return value: If there is no free channel to allocate, -%ENODEV is returned.
423 * On successful allocation channel is returned.
425 imx_dmach_t imx_dma_request_by_prio(const char *name, imx_dma_prio prio)
431 case (DMA_PRIO_HIGH):
434 case (DMA_PRIO_MEDIUM):
443 for (i = best; i < IMX_DMA_CHANNELS; i++) {
444 if (!imx_dma_request(i, name)) {
449 for (i = best - 1; i >= 0; i--) {
450 if (!imx_dma_request(i, name)) {
455 printk(KERN_ERR "%s: no free DMA channel found\n", __func__);
460 static irqreturn_t dma_err_handler(int irq, void *dev_id)
463 struct imx_dma_channel *channel;
464 unsigned int err_mask = DBTOSR | DRTOSR | DSESR | DBOSR;
467 DISR = disr & err_mask;
468 for (i = 0; i < IMX_DMA_CHANNELS; i++) {
469 if(!(err_mask & (1 << i)))
471 channel = &imx_dma_channels[i];
474 if (DBTOSR & (1 << i)) {
476 errcode |= IMX_DMA_ERR_BURST;
478 if (DRTOSR & (1 << i)) {
480 errcode |= IMX_DMA_ERR_REQUEST;
482 if (DSESR & (1 << i)) {
484 errcode |= IMX_DMA_ERR_TRANSFER;
486 if (DBOSR & (1 << i)) {
488 errcode |= IMX_DMA_ERR_BUFFER;
492 * The cleaning of @sg field would be questionable
493 * there, because its value can help to compute
494 * remaining/transferred bytes count in the handler
496 /*imx_dma_channels[i].sg = NULL;*/
498 if (channel->name && channel->err_handler) {
499 channel->err_handler(i, channel->data, errcode);
503 imx_dma_channels[i].sg = NULL;
506 "DMA timeout on channel %d (%s) -%s%s%s%s\n",
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":"");
516 static irqreturn_t dma_irq_handler(int irq, void *dev_id)
520 pr_debug("imxdma: dma_irq_handler called, disr=0x%08x\n",
524 for (i = 0; i < IMX_DMA_CHANNELS; i++) {
525 if (disr & (1 << i)) {
526 struct imx_dma_channel *channel = &imx_dma_channels[i];
528 if (imx_dma_sg_next(i, CNTR(i))) {
533 if (channel->irq_handler)
534 channel->irq_handler(i,
539 * IRQ for an unregistered DMA channel:
540 * let's clear the interrupts and disable it.
543 "spurious IRQ for DMA channel %d\n", i);
550 static int __init imx_dma_init(void)
555 /* reset DMA module */
558 ret = request_irq(DMA_INT, dma_irq_handler, 0, "DMA", NULL);
560 printk(KERN_CRIT "Wow! Can't register IRQ for DMA\n");
564 ret = request_irq(DMA_ERR, dma_err_handler, 0, "DMA", NULL);
566 printk(KERN_CRIT "Wow! Can't register ERRIRQ for DMA\n");
567 free_irq(DMA_INT, NULL);
570 /* enable DMA module */
573 /* clear all interrupts */
574 DISR = (1 << IMX_DMA_CHANNELS) - 1;
576 /* enable interrupts */
577 DIMR = (1 << IMX_DMA_CHANNELS) - 1;
579 for (i = 0; i < IMX_DMA_CHANNELS; i++) {
580 imx_dma_channels[i].sg = NULL;
581 imx_dma_channels[i].dma_num = i;
587 arch_initcall(imx_dma_init);
589 EXPORT_SYMBOL(imx_dma_setup_single);
590 EXPORT_SYMBOL(imx_dma_setup_sg);
591 EXPORT_SYMBOL(imx_dma_setup_handlers);
592 EXPORT_SYMBOL(imx_dma_enable);
593 EXPORT_SYMBOL(imx_dma_disable);
594 EXPORT_SYMBOL(imx_dma_request);
595 EXPORT_SYMBOL(imx_dma_free);
596 EXPORT_SYMBOL(imx_dma_request_by_prio);
597 EXPORT_SYMBOL(imx_dma_channels);