981db9878b27ae6f33e9c51a0e0be7b5c28b5ae7
[cascardo/linux.git] / arch / arm / mach-sa1100 / irq.c
1 /*
2  * linux/arch/arm/mach-sa1100/irq.c
3  *
4  * Copyright (C) 1999-2001 Nicolas Pitre
5  *
6  * Generic IRQ handling for the SA11x0, GPIO 11-27 IRQ demultiplexing.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/io.h>
16 #include <linux/irq.h>
17 #include <linux/irqdomain.h>
18 #include <linux/ioport.h>
19 #include <linux/syscore_ops.h>
20
21 #include <mach/hardware.h>
22 #include <mach/irqs.h>
23 #include <asm/mach/irq.h>
24 #include <asm/exception.h>
25
26 #include "generic.h"
27
28
29 /*
30  * SA1100 GPIO edge detection for IRQs:
31  * IRQs are generated on Falling-Edge, Rising-Edge, or both.
32  * Use this instead of directly setting GRER/GFER.
33  */
34 static int GPIO_IRQ_rising_edge;
35 static int GPIO_IRQ_falling_edge;
36 static int GPIO_IRQ_mask = (1 << 11) - 1;
37
38 static int sa1100_gpio_type(struct irq_data *d, unsigned int type)
39 {
40         unsigned int mask;
41
42         mask = BIT(d->hwirq);
43
44         if (type == IRQ_TYPE_PROBE) {
45                 if ((GPIO_IRQ_rising_edge | GPIO_IRQ_falling_edge) & mask)
46                         return 0;
47                 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
48         }
49
50         if (type & IRQ_TYPE_EDGE_RISING) {
51                 GPIO_IRQ_rising_edge |= mask;
52         } else
53                 GPIO_IRQ_rising_edge &= ~mask;
54         if (type & IRQ_TYPE_EDGE_FALLING) {
55                 GPIO_IRQ_falling_edge |= mask;
56         } else
57                 GPIO_IRQ_falling_edge &= ~mask;
58
59         GRER = GPIO_IRQ_rising_edge & GPIO_IRQ_mask;
60         GFER = GPIO_IRQ_falling_edge & GPIO_IRQ_mask;
61
62         return 0;
63 }
64
65 /*
66  * GPIO IRQs must be acknowledged.  This is for IRQs from GPIO0 to 10.
67  */
68 static void sa1100_low_gpio_ack(struct irq_data *d)
69 {
70         GEDR = BIT(d->hwirq);
71 }
72
73 static void sa1100_low_gpio_mask(struct irq_data *d)
74 {
75         ICMR &= ~BIT(d->hwirq);
76 }
77
78 static void sa1100_low_gpio_unmask(struct irq_data *d)
79 {
80         ICMR |= BIT(d->hwirq);
81 }
82
83 static int sa1100_low_gpio_wake(struct irq_data *d, unsigned int on)
84 {
85         if (on)
86                 PWER |= BIT(d->hwirq);
87         else
88                 PWER &= ~BIT(d->hwirq);
89         return 0;
90 }
91
92 static struct irq_chip sa1100_low_gpio_chip = {
93         .name           = "GPIO-l",
94         .irq_ack        = sa1100_low_gpio_ack,
95         .irq_mask       = sa1100_low_gpio_mask,
96         .irq_unmask     = sa1100_low_gpio_unmask,
97         .irq_set_type   = sa1100_gpio_type,
98         .irq_set_wake   = sa1100_low_gpio_wake,
99 };
100
101 static int sa1100_low_gpio_irqdomain_map(struct irq_domain *d,
102                 unsigned int irq, irq_hw_number_t hwirq)
103 {
104         irq_set_chip_and_handler(irq, &sa1100_low_gpio_chip,
105                                  handle_edge_irq);
106         set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
107
108         return 0;
109 }
110
111 static struct irq_domain_ops sa1100_low_gpio_irqdomain_ops = {
112         .map = sa1100_low_gpio_irqdomain_map,
113         .xlate = irq_domain_xlate_onetwocell,
114 };
115
116 static struct irq_domain *sa1100_low_gpio_irqdomain;
117
118 /*
119  * IRQ11 (GPIO11 through 27) handler.  We enter here with the
120  * irq_controller_lock held, and IRQs disabled.  Decode the IRQ
121  * and call the handler.
122  */
123 static void
124 sa1100_high_gpio_handler(unsigned int irq, struct irq_desc *desc)
125 {
126         unsigned int mask;
127
128         mask = GEDR & 0xfffff800;
129         do {
130                 /*
131                  * clear down all currently active IRQ sources.
132                  * We will be processing them all.
133                  */
134                 GEDR = mask;
135
136                 irq = IRQ_GPIO11;
137                 mask >>= 11;
138                 do {
139                         if (mask & 1)
140                                 generic_handle_irq(irq);
141                         mask >>= 1;
142                         irq++;
143                 } while (mask);
144
145                 mask = GEDR & 0xfffff800;
146         } while (mask);
147 }
148
149 /*
150  * Like GPIO0 to 10, GPIO11-27 IRQs need to be handled specially.
151  * In addition, the IRQs are all collected up into one bit in the
152  * interrupt controller registers.
153  */
154 static void sa1100_high_gpio_ack(struct irq_data *d)
155 {
156         unsigned int mask = BIT(d->hwirq);
157
158         GEDR = mask;
159 }
160
161 static void sa1100_high_gpio_mask(struct irq_data *d)
162 {
163         unsigned int mask = BIT(d->hwirq);
164
165         GPIO_IRQ_mask &= ~mask;
166
167         GRER &= ~mask;
168         GFER &= ~mask;
169 }
170
171 static void sa1100_high_gpio_unmask(struct irq_data *d)
172 {
173         unsigned int mask = BIT(d->hwirq);
174
175         GPIO_IRQ_mask |= mask;
176
177         GRER = GPIO_IRQ_rising_edge & GPIO_IRQ_mask;
178         GFER = GPIO_IRQ_falling_edge & GPIO_IRQ_mask;
179 }
180
181 static int sa1100_high_gpio_wake(struct irq_data *d, unsigned int on)
182 {
183         if (on)
184                 PWER |= BIT(d->hwirq);
185         else
186                 PWER &= ~BIT(d->hwirq);
187         return 0;
188 }
189
190 static struct irq_chip sa1100_high_gpio_chip = {
191         .name           = "GPIO-h",
192         .irq_ack        = sa1100_high_gpio_ack,
193         .irq_mask       = sa1100_high_gpio_mask,
194         .irq_unmask     = sa1100_high_gpio_unmask,
195         .irq_set_type   = sa1100_gpio_type,
196         .irq_set_wake   = sa1100_high_gpio_wake,
197 };
198
199 static int sa1100_high_gpio_irqdomain_map(struct irq_domain *d,
200                 unsigned int irq, irq_hw_number_t hwirq)
201 {
202         irq_set_chip_and_handler(irq, &sa1100_high_gpio_chip,
203                                  handle_edge_irq);
204         set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
205
206         return 0;
207 }
208
209 static struct irq_domain_ops sa1100_high_gpio_irqdomain_ops = {
210         .map = sa1100_high_gpio_irqdomain_map,
211         .xlate = irq_domain_xlate_onetwocell,
212 };
213
214 static struct irq_domain *sa1100_high_gpio_irqdomain;
215
216 /*
217  * We don't need to ACK IRQs on the SA1100 unless they're GPIOs
218  * this is for internal IRQs i.e. from IRQ LCD to RTCAlrm.
219  */
220 static void sa1100_mask_irq(struct irq_data *d)
221 {
222         ICMR &= ~BIT(d->hwirq);
223 }
224
225 static void sa1100_unmask_irq(struct irq_data *d)
226 {
227         ICMR |= BIT(d->hwirq);
228 }
229
230 /*
231  * Apart form GPIOs, only the RTC alarm can be a wakeup event.
232  */
233 static int sa1100_set_wake(struct irq_data *d, unsigned int on)
234 {
235         if (BIT(d->hwirq) == IC_RTCAlrm) {
236                 if (on)
237                         PWER |= PWER_RTC;
238                 else
239                         PWER &= ~PWER_RTC;
240                 return 0;
241         }
242         return -EINVAL;
243 }
244
245 static struct irq_chip sa1100_normal_chip = {
246         .name           = "SC",
247         .irq_ack        = sa1100_mask_irq,
248         .irq_mask       = sa1100_mask_irq,
249         .irq_unmask     = sa1100_unmask_irq,
250         .irq_set_wake   = sa1100_set_wake,
251 };
252
253 static int sa1100_normal_irqdomain_map(struct irq_domain *d,
254                 unsigned int irq, irq_hw_number_t hwirq)
255 {
256         irq_set_chip_and_handler(irq, &sa1100_normal_chip,
257                                  handle_level_irq);
258         set_irq_flags(irq, IRQF_VALID);
259
260         return 0;
261 }
262
263 static struct irq_domain_ops sa1100_normal_irqdomain_ops = {
264         .map = sa1100_normal_irqdomain_map,
265         .xlate = irq_domain_xlate_onetwocell,
266 };
267
268 static struct irq_domain *sa1100_normal_irqdomain;
269
270 static struct resource irq_resource =
271         DEFINE_RES_MEM_NAMED(0x90050000, SZ_64K, "irqs");
272
273 static struct sa1100irq_state {
274         unsigned int    saved;
275         unsigned int    icmr;
276         unsigned int    iclr;
277         unsigned int    iccr;
278 } sa1100irq_state;
279
280 static int sa1100irq_suspend(void)
281 {
282         struct sa1100irq_state *st = &sa1100irq_state;
283
284         st->saved = 1;
285         st->icmr = ICMR;
286         st->iclr = ICLR;
287         st->iccr = ICCR;
288
289         /*
290          * Disable all GPIO-based interrupts.
291          */
292         ICMR &= ~(IC_GPIO11_27|IC_GPIO10|IC_GPIO9|IC_GPIO8|IC_GPIO7|
293                   IC_GPIO6|IC_GPIO5|IC_GPIO4|IC_GPIO3|IC_GPIO2|
294                   IC_GPIO1|IC_GPIO0);
295
296         /*
297          * Set the appropriate edges for wakeup.
298          */
299         GRER = PWER & GPIO_IRQ_rising_edge;
300         GFER = PWER & GPIO_IRQ_falling_edge;
301         
302         /*
303          * Clear any pending GPIO interrupts.
304          */
305         GEDR = GEDR;
306
307         return 0;
308 }
309
310 static void sa1100irq_resume(void)
311 {
312         struct sa1100irq_state *st = &sa1100irq_state;
313
314         if (st->saved) {
315                 ICCR = st->iccr;
316                 ICLR = st->iclr;
317
318                 GRER = GPIO_IRQ_rising_edge & GPIO_IRQ_mask;
319                 GFER = GPIO_IRQ_falling_edge & GPIO_IRQ_mask;
320
321                 ICMR = st->icmr;
322         }
323 }
324
325 static struct syscore_ops sa1100irq_syscore_ops = {
326         .suspend        = sa1100irq_suspend,
327         .resume         = sa1100irq_resume,
328 };
329
330 static int __init sa1100irq_init_devicefs(void)
331 {
332         register_syscore_ops(&sa1100irq_syscore_ops);
333         return 0;
334 }
335
336 device_initcall(sa1100irq_init_devicefs);
337
338 static asmlinkage void __exception_irq_entry
339 sa1100_handle_irq(struct pt_regs *regs)
340 {
341         uint32_t icip, icmr, mask;
342
343         do {
344                 icip = (ICIP);
345                 icmr = (ICMR);
346                 mask = icip & icmr;
347
348                 if (mask == 0)
349                         break;
350
351                 handle_IRQ(ffs(mask) - 1 + IRQ_GPIO0, regs);
352         } while (1);
353 }
354
355 void __init sa1100_init_irq(void)
356 {
357         request_resource(&iomem_resource, &irq_resource);
358
359         /* disable all IRQs */
360         ICMR = 0;
361
362         /* all IRQs are IRQ, not FIQ */
363         ICLR = 0;
364
365         /* clear all GPIO edge detects */
366         GFER = 0;
367         GRER = 0;
368         GEDR = -1;
369
370         /*
371          * Whatever the doc says, this has to be set for the wait-on-irq
372          * instruction to work... on a SA1100 rev 9 at least.
373          */
374         ICCR = 1;
375
376         sa1100_low_gpio_irqdomain = irq_domain_add_legacy(NULL,
377                         11, IRQ_GPIO0, 0,
378                         &sa1100_low_gpio_irqdomain_ops, NULL);
379
380         sa1100_normal_irqdomain = irq_domain_add_legacy(NULL,
381                         21, IRQ_GPIO11_27, 11,
382                         &sa1100_normal_irqdomain_ops, NULL);
383
384         sa1100_high_gpio_irqdomain = irq_domain_add_legacy(NULL,
385                         17, IRQ_GPIO11, 11,
386                         &sa1100_high_gpio_irqdomain_ops, NULL);
387
388         /*
389          * Install handler for GPIO 11-27 edge detect interrupts
390          */
391         irq_set_chained_handler(IRQ_GPIO11_27, sa1100_high_gpio_handler);
392
393         set_handle_irq(sa1100_handle_irq);
394
395         sa1100_init_gpio();
396 }