blob: ce150bf00bdd91d66b1d4e24fe04608c23bdddca [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
eric miaoc01655042008-01-28 23:00:02 +000018#include <linux/sysdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/hardware.h>
21#include <asm/irq.h>
22#include <asm/mach/irq.h>
23#include <asm/arch/pxa-regs.h>
24
25#include "generic.h"
26
27
28/*
29 * This is for peripheral IRQs internal to the PXA chip.
30 */
31
32static void pxa_mask_low_irq(unsigned int irq)
33{
Eric Miao486c9552007-06-06 06:22:20 +010034 ICMR &= ~(1 << irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
37static void pxa_unmask_low_irq(unsigned int irq)
38{
Eric Miao486c9552007-06-06 06:22:20 +010039 ICMR |= (1 << irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
David Brownell38c677c2006-08-01 22:26:25 +010042static struct irq_chip pxa_internal_chip_low = {
43 .name = "SC",
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 .ack = pxa_mask_low_irq,
45 .mask = pxa_mask_low_irq,
46 .unmask = pxa_unmask_low_irq,
47};
48
Eric Miao53665a52007-06-06 06:36:04 +010049void __init pxa_init_irq_low(void)
50{
51 int irq;
52
53 /* disable all IRQs */
54 ICMR = 0;
55
56 /* all IRQs are IRQ, not FIQ */
57 ICLR = 0;
58
59 /* only unmasked interrupts kick us out of idle */
60 ICCR = 1;
61
62 for (irq = PXA_IRQ(0); irq <= PXA_IRQ(31); irq++) {
63 set_irq_chip(irq, &pxa_internal_chip_low);
64 set_irq_handler(irq, handle_level_irq);
65 set_irq_flags(irq, IRQF_VALID);
66 }
67}
68
eric miao2c8086a2007-09-11 19:13:17 -070069#if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 * This is for the second set of internal IRQs as found on the PXA27x.
73 */
74
75static void pxa_mask_high_irq(unsigned int irq)
76{
Eric Miao486c9552007-06-06 06:22:20 +010077 ICMR2 &= ~(1 << (irq - 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80static void pxa_unmask_high_irq(unsigned int irq)
81{
Eric Miao486c9552007-06-06 06:22:20 +010082 ICMR2 |= (1 << (irq - 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
David Brownell38c677c2006-08-01 22:26:25 +010085static struct irq_chip pxa_internal_chip_high = {
86 .name = "SC-hi",
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 .ack = pxa_mask_high_irq,
88 .mask = pxa_mask_high_irq,
89 .unmask = pxa_unmask_high_irq,
90};
91
Eric Miaoc08b7b32007-06-06 06:32:38 +010092void __init pxa_init_irq_high(void)
93{
94 int irq;
95
96 ICMR2 = 0;
97 ICLR2 = 0;
98
99 for (irq = PXA_IRQ(32); irq < PXA_IRQ(64); irq++) {
100 set_irq_chip(irq, &pxa_internal_chip_high);
101 set_irq_handler(irq, handle_level_irq);
102 set_irq_flags(irq, IRQF_VALID);
103 }
104}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#endif
106
107/*
108 * PXA GPIO edge detection for IRQs:
109 * IRQs are generated on Falling-Edge, Rising-Edge, or both.
110 * Use this instead of directly setting GRER/GFER.
111 */
112
113static long GPIO_IRQ_rising_edge[4];
114static long GPIO_IRQ_falling_edge[4];
115static long GPIO_IRQ_mask[4];
116
117static int pxa_gpio_irq_type(unsigned int irq, unsigned int type)
118{
119 int gpio, idx;
120
121 gpio = IRQ_TO_GPIO(irq);
122 idx = gpio >> 5;
123
124 if (type == IRQT_PROBE) {
125 /* Don't mess with enabled GPIOs using preconfigured edges or
Guennadi Liakhovetskie0331082006-06-28 16:42:02 +0100126 GPIOs set to alternate function or to output during probe */
127 if ((GPIO_IRQ_rising_edge[idx] | GPIO_IRQ_falling_edge[idx] | GPDR(gpio)) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 GPIO_bit(gpio))
129 return 0;
130 if (GAFR(gpio) & (0x3 << (((gpio) & 0xf)*2)))
131 return 0;
132 type = __IRQT_RISEDGE | __IRQT_FALEDGE;
133 }
134
135 /* printk(KERN_DEBUG "IRQ%d (GPIO%d): ", irq, gpio); */
136
137 pxa_gpio_mode(gpio | GPIO_IN);
138
139 if (type & __IRQT_RISEDGE) {
140 /* printk("rising "); */
141 __set_bit (gpio, GPIO_IRQ_rising_edge);
Philipp Zabel4fe4a2b2007-02-26 01:44:57 +0100142 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 __clear_bit (gpio, GPIO_IRQ_rising_edge);
Philipp Zabel4fe4a2b2007-02-26 01:44:57 +0100144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 if (type & __IRQT_FALEDGE) {
147 /* printk("falling "); */
148 __set_bit (gpio, GPIO_IRQ_falling_edge);
Philipp Zabel4fe4a2b2007-02-26 01:44:57 +0100149 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 __clear_bit (gpio, GPIO_IRQ_falling_edge);
Philipp Zabel4fe4a2b2007-02-26 01:44:57 +0100151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 /* printk("edges\n"); */
154
155 GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
156 GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
157 return 0;
158}
159
160/*
161 * GPIO IRQs must be acknowledged. This is for GPIO 0 and 1.
162 */
163
164static void pxa_ack_low_gpio(unsigned int irq)
165{
166 GEDR0 = (1 << (irq - IRQ_GPIO0));
167}
168
eric miaoa7bf4db2008-03-04 11:12:14 +0800169static void pxa_mask_low_gpio(unsigned int irq)
170{
171 ICMR &= ~(1 << (irq - PXA_IRQ(0)));
172}
173
174static void pxa_unmask_low_gpio(unsigned int irq)
175{
176 ICMR |= 1 << (irq - PXA_IRQ(0));
177}
178
David Brownell38c677c2006-08-01 22:26:25 +0100179static struct irq_chip pxa_low_gpio_chip = {
180 .name = "GPIO-l",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 .ack = pxa_ack_low_gpio,
eric miaoa7bf4db2008-03-04 11:12:14 +0800182 .mask = pxa_mask_low_gpio,
183 .unmask = pxa_unmask_low_gpio,
Russell King78019072005-09-04 19:43:13 +0100184 .set_type = pxa_gpio_irq_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
187/*
188 * Demux handler for GPIO>=2 edge detect interrupts
189 */
190
eric miao7a26d3a2008-03-04 10:57:18 +0800191#define GEDR_BITS (sizeof(gedr) * BITS_PER_BYTE)
192
Russell King10dd5ce2006-11-23 11:41:32 +0000193static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
eric miao7a26d3a2008-03-04 10:57:18 +0800195 int loop, bit, n;
196 unsigned long gedr[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 do {
eric miao7a26d3a2008-03-04 10:57:18 +0800199 gedr[0] = GEDR0 & GPIO_IRQ_mask[0] & ~3;
200 gedr[1] = GEDR1 & GPIO_IRQ_mask[1];
201 gedr[2] = GEDR2 & GPIO_IRQ_mask[2];
202 gedr[3] = GEDR3 & GPIO_IRQ_mask[3];
203
204 GEDR0 = gedr[0]; GEDR1 = gedr[1];
205 GEDR2 = gedr[2]; GEDR3 = gedr[3];
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 loop = 0;
eric miao7a26d3a2008-03-04 10:57:18 +0800208 bit = find_first_bit(gedr, GEDR_BITS);
209 while (bit < GEDR_BITS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 loop = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
eric miao7a26d3a2008-03-04 10:57:18 +0800212 n = PXA_GPIO_IRQ_BASE + bit;
213 desc_handle_irq(n, irq_desc + n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
eric miao7a26d3a2008-03-04 10:57:18 +0800215 bit = find_next_bit(gedr, GEDR_BITS, bit + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 } while (loop);
218}
219
220static void pxa_ack_muxed_gpio(unsigned int irq)
221{
222 int gpio = irq - IRQ_GPIO(2) + 2;
223 GEDR(gpio) = GPIO_bit(gpio);
224}
225
226static void pxa_mask_muxed_gpio(unsigned int irq)
227{
228 int gpio = irq - IRQ_GPIO(2) + 2;
229 __clear_bit(gpio, GPIO_IRQ_mask);
230 GRER(gpio) &= ~GPIO_bit(gpio);
231 GFER(gpio) &= ~GPIO_bit(gpio);
232}
233
234static void pxa_unmask_muxed_gpio(unsigned int irq)
235{
236 int gpio = irq - IRQ_GPIO(2) + 2;
237 int idx = gpio >> 5;
238 __set_bit(gpio, GPIO_IRQ_mask);
239 GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
240 GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
241}
242
David Brownell38c677c2006-08-01 22:26:25 +0100243static struct irq_chip pxa_muxed_gpio_chip = {
244 .name = "GPIO",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 .ack = pxa_ack_muxed_gpio,
246 .mask = pxa_mask_muxed_gpio,
247 .unmask = pxa_unmask_muxed_gpio,
Russell King78019072005-09-04 19:43:13 +0100248 .set_type = pxa_gpio_irq_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249};
250
Eric Miao348f2e32007-06-06 06:37:15 +0100251void __init pxa_init_irq_gpio(int gpio_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Eric Miao348f2e32007-06-06 06:37:15 +0100253 int irq, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
eric miao30f0b402007-08-29 10:18:47 +0100255 pxa_last_gpio = gpio_nr - 1;
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /* clear all GPIO edge detects */
Eric Miao348f2e32007-06-06 06:37:15 +0100258 for (i = 0; i < gpio_nr; i += 32) {
259 GFER(i) = 0;
260 GRER(i) = 0;
261 GEDR(i) = GEDR(i);
262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /* GPIO 0 and 1 must have their mask bit always set */
265 GPIO_IRQ_mask[0] = 3;
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 for (irq = IRQ_GPIO0; irq <= IRQ_GPIO1; irq++) {
268 set_irq_chip(irq, &pxa_low_gpio_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000269 set_irq_handler(irq, handle_edge_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
271 }
272
Samuelfd51bcc2007-08-28 19:56:34 +0100273 for (irq = IRQ_GPIO(2); irq < IRQ_GPIO(gpio_nr); irq++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 set_irq_chip(irq, &pxa_muxed_gpio_chip);
Russell King10dd5ce2006-11-23 11:41:32 +0000275 set_irq_handler(irq, handle_edge_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
277 }
278
279 /* Install handler for GPIO>=2 edge detect interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 set_irq_chained_handler(IRQ_GPIO_2_x, pxa_gpio_demux_handler);
Philipp Zabel1c44f5f2008-02-04 22:28:22 -0800281
282 pxa_init_gpio(gpio_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
eric miaoc95530c2007-08-29 10:22:17 +0100284
eric miaoa7bf4db2008-03-04 11:12:14 +0800285void __init pxa_init_gpio_set_wake(int (*set_wake)(unsigned int, unsigned int))
286{
287 pxa_low_gpio_chip.set_wake = set_wake;
288 pxa_muxed_gpio_chip.set_wake = set_wake;
289}
290
eric miaoc95530c2007-08-29 10:22:17 +0100291void __init pxa_init_irq_set_wake(int (*set_wake)(unsigned int, unsigned int))
292{
293 pxa_internal_chip_low.set_wake = set_wake;
294#ifdef CONFIG_PXA27x
295 pxa_internal_chip_high.set_wake = set_wake;
296#endif
eric miaoa7bf4db2008-03-04 11:12:14 +0800297 pxa_init_gpio_set_wake(set_wake);
eric miaoc95530c2007-08-29 10:22:17 +0100298}
eric miaoc01655042008-01-28 23:00:02 +0000299
300#ifdef CONFIG_PM
301static unsigned long saved_icmr[2];
302
303static int pxa_irq_suspend(struct sys_device *dev, pm_message_t state)
304{
305 switch (dev->id) {
306 case 0:
307 saved_icmr[0] = ICMR;
308 ICMR = 0;
309 break;
310#if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
311 case 1:
312 saved_icmr[1] = ICMR2;
313 ICMR2 = 0;
314 break;
315#endif
316 default:
317 return -EINVAL;
318 }
319
320 return 0;
321}
322
323static int pxa_irq_resume(struct sys_device *dev)
324{
325 switch (dev->id) {
326 case 0:
327 ICMR = saved_icmr[0];
328 ICLR = 0;
329 ICCR = 1;
330 break;
331#if defined(CONFIG_PXA27x) || defined(CONFIG_PXA3xx)
332 case 1:
333 ICMR2 = saved_icmr[1];
334 ICLR2 = 0;
335 break;
336#endif
337 default:
338 return -EINVAL;
339 }
340
341 return 0;
342}
343#else
344#define pxa_irq_suspend NULL
345#define pxa_irq_resume NULL
346#endif
347
348struct sysdev_class pxa_irq_sysclass = {
349 .name = "irq",
350 .suspend = pxa_irq_suspend,
351 .resume = pxa_irq_resume,
352};
353
354static int __init pxa_irq_init(void)
355{
356 return sysdev_class_register(&pxa_irq_sysclass);
357}
358
359core_initcall(pxa_irq_init);