irq.c 7.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 *  linux/arch/arm/mach-pxa/irq.c
 *
 *  Generic PXA IRQ handling, GPIO IRQ demultiplexing, etc.
 *
 *  Author:	Nicolas Pitre
 *  Created:	Jun 15, 2001
 *  Copyright:	MontaVista Software Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/interrupt.h>

#include <asm/hardware.h>
#include <asm/irq.h>
#include <asm/mach/irq.h>
#include <asm/arch/pxa-regs.h>

#include "generic.h"


/*
 * This is for peripheral IRQs internal to the PXA chip.
 */

static void pxa_mask_low_irq(unsigned int irq)
{
33
	ICMR &= ~(1 << irq);
L
Linus Torvalds 已提交
34 35 36 37
}

static void pxa_unmask_low_irq(unsigned int irq)
{
38
	ICMR |= (1 << irq);
L
Linus Torvalds 已提交
39 40
}

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
static int pxa_set_wake(unsigned int irq, unsigned int on)
{
	u32	mask;

	switch (irq) {
	case IRQ_RTCAlrm:
		mask = PWER_RTC;
		break;
#ifdef CONFIG_PXA27x
	/* REVISIT can handle USBH1, USBH2, USB, MSL, USIM, ... */
#endif
	default:
		return -EINVAL;
	}
	if (on)
		PWER |= mask;
	else
		PWER &= ~mask;
	return 0;
}

62 63
static struct irq_chip pxa_internal_chip_low = {
	.name		= "SC",
L
Linus Torvalds 已提交
64 65 66
	.ack		= pxa_mask_low_irq,
	.mask		= pxa_mask_low_irq,
	.unmask		= pxa_unmask_low_irq,
67
	.set_wake	= pxa_set_wake,
L
Linus Torvalds 已提交
68 69
};

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
void __init pxa_init_irq_low(void)
{
	int irq;

	/* disable all IRQs */
	ICMR = 0;

	/* all IRQs are IRQ, not FIQ */
	ICLR = 0;

	/* only unmasked interrupts kick us out of idle */
	ICCR = 1;

	for (irq = PXA_IRQ(0); irq <= PXA_IRQ(31); irq++) {
		set_irq_chip(irq, &pxa_internal_chip_low);
		set_irq_handler(irq, handle_level_irq);
		set_irq_flags(irq, IRQF_VALID);
	}
}

90
#ifdef CONFIG_PXA27x
L
Linus Torvalds 已提交
91 92 93 94 95 96 97

/*
 * This is for the second set of internal IRQs as found on the PXA27x.
 */

static void pxa_mask_high_irq(unsigned int irq)
{
98
	ICMR2 &= ~(1 << (irq - 32));
L
Linus Torvalds 已提交
99 100 101 102
}

static void pxa_unmask_high_irq(unsigned int irq)
{
103
	ICMR2 |= (1 << (irq - 32));
L
Linus Torvalds 已提交
104 105
}

106 107
static struct irq_chip pxa_internal_chip_high = {
	.name		= "SC-hi",
L
Linus Torvalds 已提交
108 109 110 111 112
	.ack		= pxa_mask_high_irq,
	.mask		= pxa_mask_high_irq,
	.unmask		= pxa_unmask_high_irq,
};

113 114 115 116 117 118 119 120 121 122 123 124 125
void __init pxa_init_irq_high(void)
{
	int irq;

	ICMR2 = 0;
	ICLR2 = 0;

	for (irq = PXA_IRQ(32); irq < PXA_IRQ(64); irq++) {
		set_irq_chip(irq, &pxa_internal_chip_high);
		set_irq_handler(irq, handle_level_irq);
		set_irq_flags(irq, IRQF_VALID);
	}
}
L
Linus Torvalds 已提交
126 127
#endif

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
/* Note that if an input/irq line ever gets changed to an output during
 * suspend, the relevant PWER, PRER, and PFER bits should be cleared.
 */
#ifdef CONFIG_PXA27x

/* PXA27x:  Various gpios can issue wakeup events.  This logic only
 * handles the simple cases, not the WEMUX2 and WEMUX3 options
 */
#define PXA27x_GPIO_NOWAKE_MASK \
	((1 << 8) | (1 << 7) | (1 << 6) | (1 << 5) | (1 << 2))
#define	WAKEMASK(gpio) \
	(((gpio) <= 15) \
		? ((1 << (gpio)) & ~PXA27x_GPIO_NOWAKE_MASK) \
		: ((gpio == 35) ? (1 << 24) : 0))
#else

/* pxa 210, 250, 255, 26x:  gpios 0..15 can issue wakeups */
#define	WAKEMASK(gpio) (((gpio) <= 15) ? (1 << (gpio)) : 0)
#endif

L
Linus Torvalds 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160
/*
 * PXA GPIO edge detection for IRQs:
 * IRQs are generated on Falling-Edge, Rising-Edge, or both.
 * Use this instead of directly setting GRER/GFER.
 */

static long GPIO_IRQ_rising_edge[4];
static long GPIO_IRQ_falling_edge[4];
static long GPIO_IRQ_mask[4];

static int pxa_gpio_irq_type(unsigned int irq, unsigned int type)
{
	int gpio, idx;
161
	u32 mask;
L
Linus Torvalds 已提交
162 163 164

	gpio = IRQ_TO_GPIO(irq);
	idx = gpio >> 5;
165
	mask = WAKEMASK(gpio);
L
Linus Torvalds 已提交
166 167 168

	if (type == IRQT_PROBE) {
	    /* Don't mess with enabled GPIOs using preconfigured edges or
169 170
	       GPIOs set to alternate function or to output during probe */
		if ((GPIO_IRQ_rising_edge[idx] | GPIO_IRQ_falling_edge[idx] | GPDR(gpio)) &
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184
		    GPIO_bit(gpio))
			return 0;
		if (GAFR(gpio) & (0x3 << (((gpio) & 0xf)*2)))
			return 0;
		type = __IRQT_RISEDGE | __IRQT_FALEDGE;
	}

	/* printk(KERN_DEBUG "IRQ%d (GPIO%d): ", irq, gpio); */

	pxa_gpio_mode(gpio | GPIO_IN);

	if (type & __IRQT_RISEDGE) {
		/* printk("rising "); */
		__set_bit (gpio, GPIO_IRQ_rising_edge);
185 186
		PRER |= mask;
	} else {
L
Linus Torvalds 已提交
187
		__clear_bit (gpio, GPIO_IRQ_rising_edge);
188 189
		PRER &= ~mask;
	}
L
Linus Torvalds 已提交
190 191 192 193

	if (type & __IRQT_FALEDGE) {
		/* printk("falling "); */
		__set_bit (gpio, GPIO_IRQ_falling_edge);
194 195
		PFER |= mask;
	} else {
L
Linus Torvalds 已提交
196
		__clear_bit (gpio, GPIO_IRQ_falling_edge);
197 198
		PFER &= ~mask;
	}
L
Linus Torvalds 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

	/* printk("edges\n"); */

	GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
	GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
	return 0;
}

/*
 * GPIO IRQs must be acknowledged.  This is for GPIO 0 and 1.
 */

static void pxa_ack_low_gpio(unsigned int irq)
{
	GEDR0 = (1 << (irq - IRQ_GPIO0));
}

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
static int pxa_set_gpio_wake(unsigned int irq, unsigned int on)
{
	int	gpio = IRQ_TO_GPIO(irq);
	u32	mask = WAKEMASK(gpio);

	if (!mask)
		return -EINVAL;

	if (on)
		PWER |= mask;
	else
		PWER &= ~mask;
	return 0;
}


232 233
static struct irq_chip pxa_low_gpio_chip = {
	.name		= "GPIO-l",
L
Linus Torvalds 已提交
234 235 236
	.ack		= pxa_ack_low_gpio,
	.mask		= pxa_mask_low_irq,
	.unmask		= pxa_unmask_low_irq,
237
	.set_type	= pxa_gpio_irq_type,
238
	.set_wake	= pxa_set_gpio_wake,
L
Linus Torvalds 已提交
239 240 241 242 243 244
};

/*
 * Demux handler for GPIO>=2 edge detect interrupts
 */

245
static void pxa_gpio_demux_handler(unsigned int irq, struct irq_desc *desc)
L
Linus Torvalds 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
{
	unsigned int mask;
	int loop;

	do {
		loop = 0;

		mask = GEDR0 & ~3;
		if (mask) {
			GEDR0 = mask;
			irq = IRQ_GPIO(2);
			desc = irq_desc + irq;
			mask >>= 2;
			do {
				if (mask & 1)
261
					desc_handle_irq(irq, desc);
L
Linus Torvalds 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275
				irq++;
				desc++;
				mask >>= 1;
			} while (mask);
			loop = 1;
		}

		mask = GEDR1;
		if (mask) {
			GEDR1 = mask;
			irq = IRQ_GPIO(32);
			desc = irq_desc + irq;
			do {
				if (mask & 1)
276
					desc_handle_irq(irq, desc);
L
Linus Torvalds 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290
				irq++;
				desc++;
				mask >>= 1;
			} while (mask);
			loop = 1;
		}

		mask = GEDR2;
		if (mask) {
			GEDR2 = mask;
			irq = IRQ_GPIO(64);
			desc = irq_desc + irq;
			do {
				if (mask & 1)
291
					desc_handle_irq(irq, desc);
L
Linus Torvalds 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
				irq++;
				desc++;
				mask >>= 1;
			} while (mask);
			loop = 1;
		}

#if PXA_LAST_GPIO >= 96
		mask = GEDR3;
		if (mask) {
			GEDR3 = mask;
			irq = IRQ_GPIO(96);
			desc = irq_desc + irq;
			do {
				if (mask & 1)
307
					desc_handle_irq(irq, desc);
L
Linus Torvalds 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
				irq++;
				desc++;
				mask >>= 1;
			} while (mask);
			loop = 1;
		}
#endif
	} while (loop);
}

static void pxa_ack_muxed_gpio(unsigned int irq)
{
	int gpio = irq - IRQ_GPIO(2) + 2;
	GEDR(gpio) = GPIO_bit(gpio);
}

static void pxa_mask_muxed_gpio(unsigned int irq)
{
	int gpio = irq - IRQ_GPIO(2) + 2;
	__clear_bit(gpio, GPIO_IRQ_mask);
	GRER(gpio) &= ~GPIO_bit(gpio);
	GFER(gpio) &= ~GPIO_bit(gpio);
}

static void pxa_unmask_muxed_gpio(unsigned int irq)
{
	int gpio = irq - IRQ_GPIO(2) + 2;
	int idx = gpio >> 5;
	__set_bit(gpio, GPIO_IRQ_mask);
	GRER(gpio) = GPIO_IRQ_rising_edge[idx] & GPIO_IRQ_mask[idx];
	GFER(gpio) = GPIO_IRQ_falling_edge[idx] & GPIO_IRQ_mask[idx];
}

341 342
static struct irq_chip pxa_muxed_gpio_chip = {
	.name		= "GPIO",
L
Linus Torvalds 已提交
343 344 345
	.ack		= pxa_ack_muxed_gpio,
	.mask		= pxa_mask_muxed_gpio,
	.unmask		= pxa_unmask_muxed_gpio,
346
	.set_type	= pxa_gpio_irq_type,
347
	.set_wake	= pxa_set_gpio_wake,
L
Linus Torvalds 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
};

void __init pxa_init_irq(void)
{
	int irq;

	/* clear all GPIO edge detects */
	GFER0 = 0;
	GFER1 = 0;
	GFER2 = 0;
	GRER0 = 0;
	GRER1 = 0;
	GRER2 = 0;
	GEDR0 = GEDR0;
	GEDR1 = GEDR1;
	GEDR2 = GEDR2;

#ifdef CONFIG_PXA27x
	/* And similarly for the extra regs on the PXA27x */
	GFER3 = 0;
	GRER3 = 0;
	GEDR3 = GEDR3;
#endif

	/* GPIO 0 and 1 must have their mask bit always set */
	GPIO_IRQ_mask[0] = 3;

375
	pxa_init_irq_low();
376 377
#ifdef CONFIG_PXA27x
	pxa_init_irq_high();
L
Linus Torvalds 已提交
378 379 380 381
#endif

	for (irq = IRQ_GPIO0; irq <= IRQ_GPIO1; irq++) {
		set_irq_chip(irq, &pxa_low_gpio_chip);
382
		set_irq_handler(irq, handle_edge_irq);
L
Linus Torvalds 已提交
383 384 385 386 387
		set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
	}

	for (irq = IRQ_GPIO(2); irq <= IRQ_GPIO(PXA_LAST_GPIO); irq++) {
		set_irq_chip(irq, &pxa_muxed_gpio_chip);
388
		set_irq_handler(irq, handle_edge_irq);
L
Linus Torvalds 已提交
389 390 391 392 393 394 395
		set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
	}

	/* Install handler for GPIO>=2 edge detect interrupts */
	set_irq_chip(IRQ_GPIO_2_x, &pxa_internal_chip_low);
	set_irq_chained_handler(IRQ_GPIO_2_x, pxa_gpio_demux_handler);
}