Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 1 | /* |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 2 | * arch/arm/mach-orion5x/irq.c |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 3 | * |
| 4 | * Core IRQ functions for Marvell Orion System On Chip |
| 5 | * |
| 6 | * Maintainer: Tzachi Perelstein <tzachi@marvell.com> |
| 7 | * |
| 8 | * This file is licensed under the terms of the GNU General Public |
Lennert Buytenhek | 159ffb3 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 9 | * License version 2. This program is licensed "as is" without any |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 10 | * warranty of any kind, whether express or implied. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/irq.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame^] | 16 | #include <linux/io.h> |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 17 | #include <asm/gpio.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 18 | #include <mach/orion5x.h> |
Lennert Buytenhek | 6f088f1 | 2008-08-09 13:44:58 +0200 | [diff] [blame] | 19 | #include <plat/irq.h> |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 20 | #include "common.h" |
| 21 | |
| 22 | /***************************************************************************** |
| 23 | * Orion GPIO IRQ |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 24 | * |
| 25 | * GPIO_IN_POL register controlls whether GPIO_DATA_IN will hold the same |
| 26 | * value of the line or the opposite value. |
| 27 | * |
| 28 | * Level IRQ handlers: DATA_IN is used directly as cause register. |
| 29 | * Interrupt are masked by LEVEL_MASK registers. |
| 30 | * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE. |
| 31 | * Interrupt are masked by EDGE_MASK registers. |
| 32 | * Both-edge handlers: Similar to regular Edge handlers, but also swaps |
| 33 | * the polarity to catch the next line transaction. |
| 34 | * This is a race condition that might not perfectly |
| 35 | * work on some use cases. |
| 36 | * |
| 37 | * Every eight GPIO lines are grouped (OR'ed) before going up to main |
| 38 | * cause register. |
| 39 | * |
| 40 | * EDGE cause mask |
| 41 | * data-in /--------| |-----| |----\ |
| 42 | * -----| |----- ---- to main cause reg |
| 43 | * X \----------------| |----/ |
| 44 | * polarity LEVEL mask |
| 45 | * |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 46 | ****************************************************************************/ |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 47 | static void orion5x_gpio_irq_ack(u32 irq) |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 48 | { |
| 49 | int pin = irq_to_gpio(irq); |
| 50 | if (irq_desc[irq].status & IRQ_LEVEL) |
| 51 | /* |
| 52 | * Mask bit for level interrupt |
| 53 | */ |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 54 | orion5x_clrbits(GPIO_LEVEL_MASK, 1 << pin); |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 55 | else |
| 56 | /* |
| 57 | * Clear casue bit for egde interrupt |
| 58 | */ |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 59 | orion5x_clrbits(GPIO_EDGE_CAUSE, 1 << pin); |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 62 | static void orion5x_gpio_irq_mask(u32 irq) |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 63 | { |
| 64 | int pin = irq_to_gpio(irq); |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 65 | if (irq_desc[irq].status & IRQ_LEVEL) |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 66 | orion5x_clrbits(GPIO_LEVEL_MASK, 1 << pin); |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 67 | else |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 68 | orion5x_clrbits(GPIO_EDGE_MASK, 1 << pin); |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 71 | static void orion5x_gpio_irq_unmask(u32 irq) |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 72 | { |
| 73 | int pin = irq_to_gpio(irq); |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 74 | if (irq_desc[irq].status & IRQ_LEVEL) |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 75 | orion5x_setbits(GPIO_LEVEL_MASK, 1 << pin); |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 76 | else |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 77 | orion5x_setbits(GPIO_EDGE_MASK, 1 << pin); |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 80 | static int orion5x_gpio_set_irq_type(u32 irq, u32 type) |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 81 | { |
| 82 | int pin = irq_to_gpio(irq); |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 83 | struct irq_desc *desc; |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 84 | |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 85 | if ((readl(GPIO_IO_CONF) & (1 << pin)) == 0) { |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 86 | printk(KERN_ERR "orion5x_gpio_set_irq_type failed " |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 87 | "(irq %d, pin %d).\n", irq, pin); |
| 88 | return -EINVAL; |
| 89 | } |
| 90 | |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 91 | desc = irq_desc + irq; |
| 92 | |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 93 | switch (type) { |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 94 | case IRQ_TYPE_LEVEL_HIGH: |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 95 | desc->handle_irq = handle_level_irq; |
| 96 | desc->status |= IRQ_LEVEL; |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 97 | orion5x_clrbits(GPIO_IN_POL, (1 << pin)); |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 98 | break; |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 99 | case IRQ_TYPE_LEVEL_LOW: |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 100 | desc->handle_irq = handle_level_irq; |
| 101 | desc->status |= IRQ_LEVEL; |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 102 | orion5x_setbits(GPIO_IN_POL, (1 << pin)); |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 103 | break; |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 104 | case IRQ_TYPE_EDGE_RISING: |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 105 | desc->handle_irq = handle_edge_irq; |
| 106 | desc->status &= ~IRQ_LEVEL; |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 107 | orion5x_clrbits(GPIO_IN_POL, (1 << pin)); |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 108 | break; |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 109 | case IRQ_TYPE_EDGE_FALLING: |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 110 | desc->handle_irq = handle_edge_irq; |
| 111 | desc->status &= ~IRQ_LEVEL; |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 112 | orion5x_setbits(GPIO_IN_POL, (1 << pin)); |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 113 | break; |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 114 | case IRQ_TYPE_EDGE_BOTH: |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 115 | desc->handle_irq = handle_edge_irq; |
| 116 | desc->status &= ~IRQ_LEVEL; |
| 117 | /* |
| 118 | * set initial polarity based on current input level |
| 119 | */ |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 120 | if ((readl(GPIO_IN_POL) ^ readl(GPIO_DATA_IN)) |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 121 | & (1 << pin)) |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 122 | orion5x_setbits(GPIO_IN_POL, (1 << pin)); /* falling */ |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 123 | else |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 124 | orion5x_clrbits(GPIO_IN_POL, (1 << pin)); /* rising */ |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 125 | |
| 126 | break; |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 127 | default: |
| 128 | printk(KERN_ERR "failed to set irq=%d (type=%d)\n", irq, type); |
| 129 | return -EINVAL; |
| 130 | } |
| 131 | |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 132 | desc->status &= ~IRQ_TYPE_SENSE_MASK; |
| 133 | desc->status |= type & IRQ_TYPE_SENSE_MASK; |
| 134 | |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 138 | static struct irq_chip orion5x_gpio_irq_chip = { |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 139 | .name = "Orion-IRQ-GPIO", |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 140 | .ack = orion5x_gpio_irq_ack, |
| 141 | .mask = orion5x_gpio_irq_mask, |
| 142 | .unmask = orion5x_gpio_irq_unmask, |
| 143 | .set_type = orion5x_gpio_set_irq_type, |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 144 | }; |
| 145 | |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 146 | static void orion5x_gpio_irq_handler(unsigned int irq, struct irq_desc *desc) |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 147 | { |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 148 | u32 cause, offs, pin; |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 149 | |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 150 | BUG_ON(irq < IRQ_ORION5X_GPIO_0_7 || irq > IRQ_ORION5X_GPIO_24_31); |
| 151 | offs = (irq - IRQ_ORION5X_GPIO_0_7) * 8; |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 152 | cause = (readl(GPIO_DATA_IN) & readl(GPIO_LEVEL_MASK)) | |
| 153 | (readl(GPIO_EDGE_CAUSE) & readl(GPIO_EDGE_MASK)); |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 154 | |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 155 | for (pin = offs; pin < offs + 8; pin++) { |
| 156 | if (cause & (1 << pin)) { |
| 157 | irq = gpio_to_irq(pin); |
| 158 | desc = irq_desc + irq; |
Dmitry Baryshkov | 6cab486 | 2008-07-27 04:23:31 +0100 | [diff] [blame] | 159 | if ((desc->status & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) { |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 160 | /* Swap polarity (race with GPIO line) */ |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 161 | u32 polarity = readl(GPIO_IN_POL); |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 162 | polarity ^= 1 << pin; |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 163 | writel(polarity, GPIO_IN_POL); |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 164 | } |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 165 | desc_handle_irq(irq, desc); |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 170 | static void __init orion5x_init_gpio_irq(void) |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 171 | { |
| 172 | int i; |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 173 | struct irq_desc *desc; |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 174 | |
| 175 | /* |
| 176 | * Mask and clear GPIO IRQ interrupts |
| 177 | */ |
Lennert Buytenhek | 79e90dd | 2008-05-28 16:43:48 +0200 | [diff] [blame] | 178 | writel(0x0, GPIO_LEVEL_MASK); |
| 179 | writel(0x0, GPIO_EDGE_MASK); |
| 180 | writel(0x0, GPIO_EDGE_CAUSE); |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 181 | |
| 182 | /* |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 183 | * Register chained level handlers for GPIO IRQs by default. |
| 184 | * User can use set_type() if he wants to use edge types handlers. |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 185 | */ |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 186 | for (i = IRQ_ORION5X_GPIO_START; i < NR_IRQS; i++) { |
| 187 | set_irq_chip(i, &orion5x_gpio_irq_chip); |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 188 | set_irq_handler(i, handle_level_irq); |
Tzachi Perelstein | f006661 | 2007-11-15 10:57:48 +0200 | [diff] [blame] | 189 | desc = irq_desc + i; |
| 190 | desc->status |= IRQ_LEVEL; |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 191 | set_irq_flags(i, IRQF_VALID); |
| 192 | } |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 193 | set_irq_chained_handler(IRQ_ORION5X_GPIO_0_7, orion5x_gpio_irq_handler); |
| 194 | set_irq_chained_handler(IRQ_ORION5X_GPIO_8_15, orion5x_gpio_irq_handler); |
| 195 | set_irq_chained_handler(IRQ_ORION5X_GPIO_16_23, orion5x_gpio_irq_handler); |
| 196 | set_irq_chained_handler(IRQ_ORION5X_GPIO_24_31, orion5x_gpio_irq_handler); |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /***************************************************************************** |
| 200 | * Orion Main IRQ |
| 201 | ****************************************************************************/ |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 202 | static void __init orion5x_init_main_irq(void) |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 203 | { |
Lennert Buytenhek | 01eb569 | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 204 | orion_irq_init(0, (void __iomem *)MAIN_IRQ_MASK); |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 205 | } |
| 206 | |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 207 | void __init orion5x_init_irq(void) |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 208 | { |
Lennert Buytenhek | 9dd0b19 | 2008-03-27 14:51:41 -0400 | [diff] [blame] | 209 | orion5x_init_main_irq(); |
| 210 | orion5x_init_gpio_irq(); |
Tzachi Perelstein | 3085de6 | 2007-10-23 15:14:42 -0400 | [diff] [blame] | 211 | } |