irq.c 6.0 KB
Newer Older
T
Tzachi Perelstein 已提交
1
/*
2
 * arch/arm/mach-orion5x/irq.c
T
Tzachi Perelstein 已提交
3 4 5 6 7 8
 *
 * Core IRQ functions for Marvell Orion System On Chip
 *
 * Maintainer: Tzachi Perelstein <tzachi@marvell.com>
 *
 * This file is licensed under the terms of the GNU General Public
L
Lennert Buytenhek 已提交
9
 * License version 2.  This program is licensed "as is" without any
T
Tzachi Perelstein 已提交
10 11 12 13 14 15 16
 * warranty of any kind, whether express or implied.
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <asm/gpio.h>
17
#include <asm/io.h>
18
#include <mach/orion5x.h>
19
#include <plat/irq.h>
T
Tzachi Perelstein 已提交
20 21 22 23
#include "common.h"

/*****************************************************************************
 * Orion GPIO IRQ
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 *
 * GPIO_IN_POL register controlls whether GPIO_DATA_IN will hold the same
 * value of the line or the opposite value.
 *
 * Level IRQ handlers: DATA_IN is used directly as cause register.
 *                     Interrupt are masked by LEVEL_MASK registers.
 * Edge IRQ handlers:  Change in DATA_IN are latched in EDGE_CAUSE.
 *                     Interrupt are masked by EDGE_MASK registers.
 * Both-edge handlers: Similar to regular Edge handlers, but also swaps
 *                     the polarity to catch the next line transaction.
 *                     This is a race condition that might not perfectly
 *                     work on some use cases.
 *
 * Every eight GPIO lines are grouped (OR'ed) before going up to main
 * cause register.
 *
 *                    EDGE  cause    mask
 *        data-in   /--------| |-----| |----\
 *     -----| |-----                         ---- to main cause reg
 *           X      \----------------| |----/
 *        polarity    LEVEL          mask
 *
T
Tzachi Perelstein 已提交
46
 ****************************************************************************/
47
static void orion5x_gpio_irq_ack(u32 irq)
48 49 50 51 52 53
{
	int pin = irq_to_gpio(irq);
	if (irq_desc[irq].status & IRQ_LEVEL)
		/*
		 * Mask bit for level interrupt
		 */
54
		orion5x_clrbits(GPIO_LEVEL_MASK, 1 << pin);
55 56 57 58
	else
		/*
		 * Clear casue bit for egde interrupt
		 */
59
		orion5x_clrbits(GPIO_EDGE_CAUSE, 1 << pin);
60 61
}

62
static void orion5x_gpio_irq_mask(u32 irq)
T
Tzachi Perelstein 已提交
63 64
{
	int pin = irq_to_gpio(irq);
65
	if (irq_desc[irq].status & IRQ_LEVEL)
66
		orion5x_clrbits(GPIO_LEVEL_MASK, 1 << pin);
67
	else
68
		orion5x_clrbits(GPIO_EDGE_MASK, 1 << pin);
T
Tzachi Perelstein 已提交
69 70
}

71
static void orion5x_gpio_irq_unmask(u32 irq)
T
Tzachi Perelstein 已提交
72 73
{
	int pin = irq_to_gpio(irq);
74
	if (irq_desc[irq].status & IRQ_LEVEL)
75
		orion5x_setbits(GPIO_LEVEL_MASK, 1 << pin);
76
	else
77
		orion5x_setbits(GPIO_EDGE_MASK, 1 << pin);
T
Tzachi Perelstein 已提交
78 79
}

80
static int orion5x_gpio_set_irq_type(u32 irq, u32 type)
T
Tzachi Perelstein 已提交
81 82
{
	int pin = irq_to_gpio(irq);
83
	struct irq_desc *desc;
T
Tzachi Perelstein 已提交
84

85
	if ((readl(GPIO_IO_CONF) & (1 << pin)) == 0) {
86
		printk(KERN_ERR "orion5x_gpio_set_irq_type failed "
T
Tzachi Perelstein 已提交
87 88 89 90
				"(irq %d, pin %d).\n", irq, pin);
		return -EINVAL;
	}

91 92
	desc = irq_desc + irq;

T
Tzachi Perelstein 已提交
93
	switch (type) {
94
	case IRQ_TYPE_LEVEL_HIGH:
95 96
		desc->handle_irq = handle_level_irq;
		desc->status |= IRQ_LEVEL;
97
		orion5x_clrbits(GPIO_IN_POL, (1 << pin));
T
Tzachi Perelstein 已提交
98
		break;
99
	case IRQ_TYPE_LEVEL_LOW:
100 101
		desc->handle_irq = handle_level_irq;
		desc->status |= IRQ_LEVEL;
102
		orion5x_setbits(GPIO_IN_POL, (1 << pin));
103
		break;
104
	case IRQ_TYPE_EDGE_RISING:
105 106
		desc->handle_irq = handle_edge_irq;
		desc->status &= ~IRQ_LEVEL;
107
		orion5x_clrbits(GPIO_IN_POL, (1 << pin));
108
		break;
109
	case IRQ_TYPE_EDGE_FALLING:
110 111
		desc->handle_irq = handle_edge_irq;
		desc->status &= ~IRQ_LEVEL;
112
		orion5x_setbits(GPIO_IN_POL, (1 << pin));
113
		break;
114
	case IRQ_TYPE_EDGE_BOTH:
115 116 117 118 119
		desc->handle_irq = handle_edge_irq;
		desc->status &= ~IRQ_LEVEL;
		/*
		 * set initial polarity based on current input level
		 */
120
		if ((readl(GPIO_IN_POL) ^ readl(GPIO_DATA_IN))
121
		    & (1 << pin))
122
			orion5x_setbits(GPIO_IN_POL, (1 << pin)); /* falling */
123
		else
124
			orion5x_clrbits(GPIO_IN_POL, (1 << pin)); /* rising */
125

T
Tzachi Perelstein 已提交
126 127 128 129 130 131
		break;
	default:
		printk(KERN_ERR "failed to set irq=%d (type=%d)\n", irq, type);
		return -EINVAL;
	}

132 133 134
	desc->status &= ~IRQ_TYPE_SENSE_MASK;
	desc->status |= type & IRQ_TYPE_SENSE_MASK;

T
Tzachi Perelstein 已提交
135 136 137
	return 0;
}

138
static struct irq_chip orion5x_gpio_irq_chip = {
T
Tzachi Perelstein 已提交
139
	.name		= "Orion-IRQ-GPIO",
140 141 142 143
	.ack		= orion5x_gpio_irq_ack,
	.mask		= orion5x_gpio_irq_mask,
	.unmask		= orion5x_gpio_irq_unmask,
	.set_type	= orion5x_gpio_set_irq_type,
T
Tzachi Perelstein 已提交
144 145
};

146
static void orion5x_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
T
Tzachi Perelstein 已提交
147
{
148
	u32 cause, offs, pin;
T
Tzachi Perelstein 已提交
149

150 151
	BUG_ON(irq < IRQ_ORION5X_GPIO_0_7 || irq > IRQ_ORION5X_GPIO_24_31);
	offs = (irq - IRQ_ORION5X_GPIO_0_7) * 8;
152 153
	cause = (readl(GPIO_DATA_IN) & readl(GPIO_LEVEL_MASK)) |
		(readl(GPIO_EDGE_CAUSE) & readl(GPIO_EDGE_MASK));
154 155 156 157 158

	for (pin = offs; pin < offs + 8; pin++) {
		if (cause & (1 << pin)) {
			irq = gpio_to_irq(pin);
			desc = irq_desc + irq;
159
			if ((desc->status & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
160
				/* Swap polarity (race with GPIO line) */
161
				u32 polarity = readl(GPIO_IN_POL);
162
				polarity ^= 1 << pin;
163
				writel(polarity, GPIO_IN_POL);
T
Tzachi Perelstein 已提交
164
			}
165
			generic_handle_irq(irq);
T
Tzachi Perelstein 已提交
166 167 168 169
		}
	}
}

170
static void __init orion5x_init_gpio_irq(void)
T
Tzachi Perelstein 已提交
171 172
{
	int i;
173
	struct irq_desc *desc;
T
Tzachi Perelstein 已提交
174 175 176 177

	/*
	 * Mask and clear GPIO IRQ interrupts
	 */
178 179 180
	writel(0x0, GPIO_LEVEL_MASK);
	writel(0x0, GPIO_EDGE_MASK);
	writel(0x0, GPIO_EDGE_CAUSE);
T
Tzachi Perelstein 已提交
181 182

	/*
183 184
	 * Register chained level handlers for GPIO IRQs by default.
	 * User can use set_type() if he wants to use edge types handlers.
T
Tzachi Perelstein 已提交
185
	 */
186 187
	for (i = IRQ_ORION5X_GPIO_START; i < NR_IRQS; i++) {
		set_irq_chip(i, &orion5x_gpio_irq_chip);
T
Tzachi Perelstein 已提交
188
		set_irq_handler(i, handle_level_irq);
189 190
		desc = irq_desc + i;
		desc->status |= IRQ_LEVEL;
T
Tzachi Perelstein 已提交
191 192
		set_irq_flags(i, IRQF_VALID);
	}
193 194 195 196
	set_irq_chained_handler(IRQ_ORION5X_GPIO_0_7, orion5x_gpio_irq_handler);
	set_irq_chained_handler(IRQ_ORION5X_GPIO_8_15, orion5x_gpio_irq_handler);
	set_irq_chained_handler(IRQ_ORION5X_GPIO_16_23, orion5x_gpio_irq_handler);
	set_irq_chained_handler(IRQ_ORION5X_GPIO_24_31, orion5x_gpio_irq_handler);
T
Tzachi Perelstein 已提交
197 198 199 200 201
}

/*****************************************************************************
 * Orion Main IRQ
 ****************************************************************************/
202
static void __init orion5x_init_main_irq(void)
T
Tzachi Perelstein 已提交
203
{
204
	orion_irq_init(0, (void __iomem *)MAIN_IRQ_MASK);
T
Tzachi Perelstein 已提交
205 206
}

207
void __init orion5x_init_irq(void)
T
Tzachi Perelstein 已提交
208
{
209 210
	orion5x_init_main_irq();
	orion5x_init_gpio_irq();
T
Tzachi Perelstein 已提交
211
}