# BRCM_VERSION=3
[bcm963xx.git] / kernel / linux / arch / arm / mach-pxa / irq.c
1 /*
2  *  linux/arch/arm/mach-pxa/irq.c
3  *
4  *  Generic PXA IRQ handling, GPIO IRQ demultiplexing, etc.
5  *
6  *  Author:     Nicolas Pitre
7  *  Created:    Jun 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/init.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/ptrace.h>
19
20 #include <asm/hardware.h>
21 #include <asm/irq.h>
22 #include <asm/mach/irq.h>
23
24 #include "generic.h"
25
26
27 /*
28  * This is for IRQs known as PXA_IRQ([8...31]).
29  */
30
31 static void pxa_mask_irq(unsigned int irq)
32 {
33         ICMR &= ~(1 << (irq + PXA_IRQ_SKIP));
34 }
35
36 static void pxa_unmask_irq(unsigned int irq)
37 {
38         ICMR |= (1 << (irq + PXA_IRQ_SKIP));
39 }
40
41 static struct irqchip pxa_internal_chip = {
42         .ack            = pxa_mask_irq,
43         .mask           = pxa_mask_irq,
44         .unmask         = pxa_unmask_irq,
45 };
46
47 /*
48  * PXA GPIO edge detection for IRQs:
49  * IRQs are generated on Falling-Edge, Rising-Edge, or both.
50  * Use this instead of directly setting GRER/GFER.
51  */
52
53 static long GPIO_IRQ_rising_edge[3];
54 static long GPIO_IRQ_falling_edge[3];
55 static long GPIO_IRQ_mask[3];
56
57 static int pxa_gpio_irq_type(unsigned int irq, unsigned int type)
58 {
59         int gpio, idx;
60
61         gpio = IRQ_TO_GPIO(irq);
62         idx = gpio >> 5;
63
64         if (type == IRQT_PROBE) {
65             /* Don't mess with enabled GPIOs using preconfigured edges or
66                GPIOs set to alternate function during probe */
67                 if ((GPIO_IRQ_rising_edge[idx] | GPIO_IRQ_falling_edge[idx]) &
68                     GPIO_bit(gpio))
69                         return 0;
70                 if (GAFR(gpio) & (0x3 << (((gpio) & 0xf)*2)))
71                         return 0;
72                 type = __IRQT_RISEDGE | __IRQT_FALEDGE;
73         }
74
75         printk(KERN_DEBUG "IRQ%d (GPIO%d): ", irq, gpio);
76
77         pxa_gpio_mode(gpio | GPIO_IN);
78
79         if (type & __IRQT_RISEDGE) {
80                 printk("rising ");
81                 __set_bit (gpio, GPIO_IRQ_rising_edge);
82         } else
83                 __clear_bit (gpio, GPIO_IRQ_rising_edge);
84
85         if (type & __IRQT_FALEDGE) {
86                 printk("falling ");
87                 __set_bit (gpio, GPIO_IRQ_falling_edge);
88         } else
89                 __clear_bit (gpio, GPIO_IRQ_falling_edge);
90
91         printk("edges\n");
92
93         GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
94         GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
95         return 0;
96 }
97
98 /*
99  * GPIO IRQs must be acknowledged.  This is for GPIO 0 and 1.
100  */
101
102 static void pxa_ack_low_gpio(unsigned int irq)
103 {
104         GEDR0 = (1 << (irq - IRQ_GPIO0));
105 }
106
107 static struct irqchip pxa_low_gpio_chip = {
108         .ack            = pxa_ack_low_gpio,
109         .mask           = pxa_mask_irq,
110         .unmask         = pxa_unmask_irq,
111         .type           = pxa_gpio_irq_type,
112 };
113
114 /*
115  * Demux handler for GPIO 2-80 edge detect interrupts
116  */
117
118 static void pxa_gpio_demux_handler(unsigned int irq, struct irqdesc *desc,
119                                    struct pt_regs *regs)
120 {
121         unsigned int mask;
122         int loop;
123
124         do {
125                 loop = 0;
126
127                 mask = GEDR0 & ~3;
128                 if (mask) {
129                         GEDR0 = mask;
130                         irq = IRQ_GPIO(2);
131                         desc = irq_desc + irq;
132                         mask >>= 2;
133                         do {
134                                 if (mask & 1)
135                                         desc->handle(irq, desc, regs);
136                                 irq++;
137                                 desc++;
138                                 mask >>= 1;
139                         } while (mask);
140                         loop = 1;
141                 }
142
143                 mask = GEDR1;
144                 if (mask) {
145                         GEDR1 = mask;
146                         irq = IRQ_GPIO(32);
147                         desc = irq_desc + irq;
148                         do {
149                                 if (mask & 1)
150                                         desc->handle(irq, desc, regs);
151                                 irq++;
152                                 desc++;
153                                 mask >>= 1;
154                         } while (mask);
155                         loop = 1;
156                 }
157
158                 mask = GEDR2;
159                 if (mask) {
160                         GEDR2 = mask;
161                         irq = IRQ_GPIO(64);
162                         desc = irq_desc + irq;
163                         do {
164                                 if (mask & 1)
165                                         desc->handle(irq, desc, regs);
166                                 irq++;
167                                 desc++;
168                                 mask >>= 1;
169                         } while (mask);
170                         loop = 1;
171                 }
172         } while (loop);
173 }
174
175 static void pxa_ack_muxed_gpio(unsigned int irq)
176 {
177         int gpio = irq - IRQ_GPIO(2) + 2;
178         GEDR(gpio) = GPIO_bit(gpio);
179 }
180
181 static void pxa_mask_muxed_gpio(unsigned int irq)
182 {
183         int gpio = irq - IRQ_GPIO(2) + 2;
184         __clear_bit(gpio, GPIO_IRQ_mask);
185         GRER(gpio) &= ~GPIO_bit(gpio);
186         GFER(gpio) &= ~GPIO_bit(gpio);
187 }
188
189 static void pxa_unmask_muxed_gpio(unsigned int irq)
190 {
191         int gpio = irq - IRQ_GPIO(2) + 2;
192         int idx = gpio >> 5;
193         __set_bit(gpio, GPIO_IRQ_mask);
194         GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
195         GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
196 }
197
198 static struct irqchip pxa_muxed_gpio_chip = {
199         .ack            = pxa_ack_muxed_gpio,
200         .mask           = pxa_mask_muxed_gpio,
201         .unmask         = pxa_unmask_muxed_gpio,
202         .type           = pxa_gpio_irq_type,
203 };
204
205
206 void __init pxa_init_irq(void)
207 {
208         int irq;
209
210         /* disable all IRQs */
211         ICMR = 0;
212
213         /* all IRQs are IRQ, not FIQ */
214         ICLR = 0;
215
216         /* clear all GPIO edge detects */
217         GFER0 = GFER1 = GFER2 = 0;
218         GRER0 = GRER1 = GRER2 = 0;
219         GEDR0 = GEDR0;
220         GEDR1 = GEDR1;
221         GEDR2 = GEDR2;
222
223         /* only unmasked interrupts kick us out of idle */
224         ICCR = 1;
225
226         /* GPIO 0 and 1 must have their mask bit always set */
227         GPIO_IRQ_mask[0] = 3;
228
229         for (irq = PXA_IRQ(PXA_IRQ_SKIP); irq <= PXA_IRQ(31); irq++) {
230                 set_irq_chip(irq, &pxa_internal_chip);
231                 set_irq_handler(irq, do_level_IRQ);
232                 set_irq_flags(irq, IRQF_VALID);
233         }
234
235         for (irq = IRQ_GPIO0; irq <= IRQ_GPIO1; irq++) {
236                 set_irq_chip(irq, &pxa_low_gpio_chip);
237                 set_irq_handler(irq, do_edge_IRQ);
238                 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
239         }
240
241         for (irq = IRQ_GPIO(2); irq <= IRQ_GPIO(80); irq++) {
242                 set_irq_chip(irq, &pxa_muxed_gpio_chip);
243                 set_irq_handler(irq, do_edge_IRQ);
244                 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
245         }
246
247         /* Install handler for GPIO 2-80 edge detect interrupts */
248         set_irq_chip(IRQ_GPIO_2_80, &pxa_internal_chip);
249         set_irq_chained_handler(IRQ_GPIO_2_80, pxa_gpio_demux_handler);
250 }