Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / arch / arm / mach-pxa / dma.c
1 /*
2  *  linux/arch/arm/mach-pxa/dma.c
3  *
4  *  PXA DMA registration and IRQ dispatching
5  *
6  *  Author:     Nicolas Pitre
7  *  Created:    Nov 15, 2001
8  *  Copyright:  MontaVista Software Inc.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License version 2 as
12  *  published by the Free Software Foundation.
13  */
14
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/interrupt.h>
19 #include <linux/errno.h>
20
21 #include <asm/system.h>
22 #include <asm/irq.h>
23 #include <asm/hardware.h>
24 #include <asm/dma.h>
25
26 #include <asm/arch/pxa-regs.h>
27
28 static struct dma_channel {
29         char *name;
30         void (*irq_handler)(int, void *, struct pt_regs *);
31         void *data;
32 } dma_channels[PXA_DMA_CHANNELS];
33
34
35 int pxa_request_dma (char *name, pxa_dma_prio prio,
36                          void (*irq_handler)(int, void *, struct pt_regs *),
37                          void *data)
38 {
39         unsigned long flags;
40         int i, found = 0;
41
42         /* basic sanity checks */
43         if (!name || !irq_handler)
44                 return -EINVAL;
45
46         local_irq_save(flags);
47
48         /* try grabbing a DMA channel with the requested priority */
49         for (i = prio; i < prio + PXA_DMA_NBCH(prio); i++) {
50                 if (!dma_channels[i].name) {
51                         found = 1;
52                         break;
53                 }
54         }
55
56         if (!found) {
57                 /* requested prio group is full, try hier priorities */
58                 for (i = prio-1; i >= 0; i--) {
59                         if (!dma_channels[i].name) {
60                                 found = 1;
61                                 break;
62                         }
63                 }
64         }
65
66         if (found) {
67                 DCSR(i) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
68                 dma_channels[i].name = name;
69                 dma_channels[i].irq_handler = irq_handler;
70                 dma_channels[i].data = data;
71         } else {
72                 printk (KERN_WARNING "No more available DMA channels for %s\n", name);
73                 i = -ENODEV;
74         }
75
76         local_irq_restore(flags);
77         return i;
78 }
79
80 void pxa_free_dma (int dma_ch)
81 {
82         unsigned long flags;
83
84         if (!dma_channels[dma_ch].name) {
85                 printk (KERN_CRIT
86                         "%s: trying to free channel %d which is already freed\n",
87                         __FUNCTION__, dma_ch);
88                 return;
89         }
90
91         local_irq_save(flags);
92         DCSR(dma_ch) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
93         dma_channels[dma_ch].name = NULL;
94         local_irq_restore(flags);
95 }
96
97 static irqreturn_t dma_irq_handler(int irq, void *dev_id, struct pt_regs *regs)
98 {
99         int i, dint = DINT;
100
101         for (i = 0; i < PXA_DMA_CHANNELS; i++) {
102                 if (dint & (1 << i)) {
103                         struct dma_channel *channel = &dma_channels[i];
104                         if (channel->name && channel->irq_handler) {
105                                 channel->irq_handler(i, channel->data, regs);
106                         } else {
107                                 /*
108                                  * IRQ for an unregistered DMA channel:
109                                  * let's clear the interrupts and disable it.
110                                  */
111                                 printk (KERN_WARNING "spurious IRQ for DMA channel %d\n", i);
112                                 DCSR(i) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
113                         }
114                 }
115         }
116         return IRQ_HANDLED;
117 }
118
119 static int __init pxa_dma_init (void)
120 {
121         int ret;
122
123         ret = request_irq (IRQ_DMA, dma_irq_handler, 0, "DMA", NULL);
124         if (ret)
125                 printk (KERN_CRIT "Wow!  Can't register IRQ for DMA\n");
126         return ret;
127 }
128
129 arch_initcall(pxa_dma_init);
130
131 EXPORT_SYMBOL(pxa_request_dma);
132 EXPORT_SYMBOL(pxa_free_dma);
133