Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Interrupt controller driver for Xilinx Virtex FPGAs |
| 3 | * |
| 4 | * Copyright (C) 2007 Secret Lab Technologies Ltd. |
| 5 | * |
| 6 | * This file is licensed under the terms of the GNU General Public License |
| 7 | * version 2. This program is licensed "as is" without any warranty of any |
| 8 | * kind, whether express or implied. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * This is a driver for the interrupt controller typically found in |
| 14 | * Xilinx Virtex FPGA designs. |
| 15 | * |
| 16 | * The interrupt sense levels are hard coded into the FPGA design with |
| 17 | * typically a 1:1 relationship between irq lines and devices (no shared |
| 18 | * irq lines). Therefore, this driver does not attempt to handle edge |
| 19 | * and level interrupts differently. |
| 20 | */ |
| 21 | #undef DEBUG |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/irq.h> |
| 25 | #include <linux/of.h> |
Rob Herring | 26a2056 | 2013-09-26 07:40:04 -0500 | [diff] [blame] | 26 | #include <linux/of_address.h> |
Rob Herring | c11eede | 2013-11-10 23:19:08 -0600 | [diff] [blame] | 27 | #include <linux/of_irq.h> |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 28 | #include <asm/io.h> |
| 29 | #include <asm/processor.h> |
Grant Likely | 1745fbc | 2009-06-06 10:15:03 -0600 | [diff] [blame] | 30 | #include <asm/i8259.h> |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 31 | #include <asm/irq.h> |
| 32 | |
| 33 | /* |
| 34 | * INTC Registers |
| 35 | */ |
| 36 | #define XINTC_ISR 0 /* Interrupt Status */ |
| 37 | #define XINTC_IPR 4 /* Interrupt Pending */ |
| 38 | #define XINTC_IER 8 /* Interrupt Enable */ |
| 39 | #define XINTC_IAR 12 /* Interrupt Acknowledge */ |
| 40 | #define XINTC_SIE 16 /* Set Interrupt Enable bits */ |
| 41 | #define XINTC_CIE 20 /* Clear Interrupt Enable bits */ |
| 42 | #define XINTC_IVR 24 /* Interrupt Vector */ |
| 43 | #define XINTC_MER 28 /* Master Enable */ |
| 44 | |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 45 | static struct irq_domain *master_irqhost; |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 46 | |
John Linn | ba10eed | 2009-05-14 10:23:11 -0600 | [diff] [blame] | 47 | #define XILINX_INTC_MAXIRQS (32) |
| 48 | |
| 49 | /* The following table allows the interrupt type, edge or level, |
| 50 | * to be cached after being read from the device tree until the interrupt |
| 51 | * is mapped |
| 52 | */ |
| 53 | static int xilinx_intc_typetable[XILINX_INTC_MAXIRQS]; |
| 54 | |
| 55 | /* Map the interrupt type from the device tree to the interrupt types |
| 56 | * used by the interrupt subsystem |
| 57 | */ |
| 58 | static unsigned char xilinx_intc_map_senses[] = { |
| 59 | IRQ_TYPE_EDGE_RISING, |
| 60 | IRQ_TYPE_EDGE_FALLING, |
| 61 | IRQ_TYPE_LEVEL_HIGH, |
| 62 | IRQ_TYPE_LEVEL_LOW, |
| 63 | }; |
| 64 | |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 65 | /* |
John Linn | ba10eed | 2009-05-14 10:23:11 -0600 | [diff] [blame] | 66 | * The interrupt controller is setup such that it doesn't work well with |
| 67 | * the level interrupt handler in the kernel because the handler acks the |
| 68 | * interrupt before calling the application interrupt handler. To deal with |
| 69 | * that, we use 2 different irq chips so that different functions can be |
| 70 | * used for level and edge type interrupts. |
| 71 | * |
| 72 | * IRQ Chip common (across level and edge) operations |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 73 | */ |
Lennert Buytenhek | 73909af | 2011-03-08 22:27:07 +0000 | [diff] [blame] | 74 | static void xilinx_intc_mask(struct irq_data *d) |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 75 | { |
Grant Likely | 476eb49 | 2011-05-04 15:02:15 +1000 | [diff] [blame] | 76 | int irq = irqd_to_hwirq(d); |
Lennert Buytenhek | 73909af | 2011-03-08 22:27:07 +0000 | [diff] [blame] | 77 | void * regs = irq_data_get_irq_chip_data(d); |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 78 | pr_debug("mask: %d\n", irq); |
| 79 | out_be32(regs + XINTC_CIE, 1 << irq); |
| 80 | } |
| 81 | |
Lennert Buytenhek | 73909af | 2011-03-08 22:27:07 +0000 | [diff] [blame] | 82 | static int xilinx_intc_set_type(struct irq_data *d, unsigned int flow_type) |
John Linn | ba10eed | 2009-05-14 10:23:11 -0600 | [diff] [blame] | 83 | { |
John Linn | ba10eed | 2009-05-14 10:23:11 -0600 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * IRQ Chip level operations |
| 89 | */ |
Lennert Buytenhek | 73909af | 2011-03-08 22:27:07 +0000 | [diff] [blame] | 90 | static void xilinx_intc_level_unmask(struct irq_data *d) |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 91 | { |
Grant Likely | 476eb49 | 2011-05-04 15:02:15 +1000 | [diff] [blame] | 92 | int irq = irqd_to_hwirq(d); |
Lennert Buytenhek | 73909af | 2011-03-08 22:27:07 +0000 | [diff] [blame] | 93 | void * regs = irq_data_get_irq_chip_data(d); |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 94 | pr_debug("unmask: %d\n", irq); |
| 95 | out_be32(regs + XINTC_SIE, 1 << irq); |
John Linn | ba10eed | 2009-05-14 10:23:11 -0600 | [diff] [blame] | 96 | |
| 97 | /* ack level irqs because they can't be acked during |
| 98 | * ack function since the handle_level_irq function |
| 99 | * acks the irq before calling the inerrupt handler |
| 100 | */ |
| 101 | out_be32(regs + XINTC_IAR, 1 << irq); |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 102 | } |
| 103 | |
John Linn | ba10eed | 2009-05-14 10:23:11 -0600 | [diff] [blame] | 104 | static struct irq_chip xilinx_intc_level_irqchip = { |
Thomas Gleixner | b27df67 | 2009-11-18 23:44:21 +0000 | [diff] [blame] | 105 | .name = "Xilinx Level INTC", |
Lennert Buytenhek | 73909af | 2011-03-08 22:27:07 +0000 | [diff] [blame] | 106 | .irq_mask = xilinx_intc_mask, |
| 107 | .irq_mask_ack = xilinx_intc_mask, |
| 108 | .irq_unmask = xilinx_intc_level_unmask, |
| 109 | .irq_set_type = xilinx_intc_set_type, |
John Linn | ba10eed | 2009-05-14 10:23:11 -0600 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | /* |
| 113 | * IRQ Chip edge operations |
| 114 | */ |
Lennert Buytenhek | 73909af | 2011-03-08 22:27:07 +0000 | [diff] [blame] | 115 | static void xilinx_intc_edge_unmask(struct irq_data *d) |
John Linn | ba10eed | 2009-05-14 10:23:11 -0600 | [diff] [blame] | 116 | { |
Grant Likely | 476eb49 | 2011-05-04 15:02:15 +1000 | [diff] [blame] | 117 | int irq = irqd_to_hwirq(d); |
Lennert Buytenhek | 73909af | 2011-03-08 22:27:07 +0000 | [diff] [blame] | 118 | void *regs = irq_data_get_irq_chip_data(d); |
John Linn | ba10eed | 2009-05-14 10:23:11 -0600 | [diff] [blame] | 119 | pr_debug("unmask: %d\n", irq); |
| 120 | out_be32(regs + XINTC_SIE, 1 << irq); |
| 121 | } |
| 122 | |
Lennert Buytenhek | 73909af | 2011-03-08 22:27:07 +0000 | [diff] [blame] | 123 | static void xilinx_intc_edge_ack(struct irq_data *d) |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 124 | { |
Grant Likely | 476eb49 | 2011-05-04 15:02:15 +1000 | [diff] [blame] | 125 | int irq = irqd_to_hwirq(d); |
Lennert Buytenhek | 73909af | 2011-03-08 22:27:07 +0000 | [diff] [blame] | 126 | void * regs = irq_data_get_irq_chip_data(d); |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 127 | pr_debug("ack: %d\n", irq); |
| 128 | out_be32(regs + XINTC_IAR, 1 << irq); |
| 129 | } |
| 130 | |
John Linn | ba10eed | 2009-05-14 10:23:11 -0600 | [diff] [blame] | 131 | static struct irq_chip xilinx_intc_edge_irqchip = { |
Thomas Gleixner | b27df67 | 2009-11-18 23:44:21 +0000 | [diff] [blame] | 132 | .name = "Xilinx Edge INTC", |
Lennert Buytenhek | 73909af | 2011-03-08 22:27:07 +0000 | [diff] [blame] | 133 | .irq_mask = xilinx_intc_mask, |
| 134 | .irq_unmask = xilinx_intc_edge_unmask, |
| 135 | .irq_ack = xilinx_intc_edge_ack, |
| 136 | .irq_set_type = xilinx_intc_set_type, |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | /* |
| 140 | * IRQ Host operations |
| 141 | */ |
John Linn | ba10eed | 2009-05-14 10:23:11 -0600 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * xilinx_intc_xlate - translate virq# from device tree interrupts property |
| 145 | */ |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 146 | static int xilinx_intc_xlate(struct irq_domain *h, struct device_node *ct, |
Roman Fietze | 40d50cf | 2009-12-08 02:39:50 +0000 | [diff] [blame] | 147 | const u32 *intspec, unsigned int intsize, |
John Linn | ba10eed | 2009-05-14 10:23:11 -0600 | [diff] [blame] | 148 | irq_hw_number_t *out_hwirq, |
| 149 | unsigned int *out_flags) |
| 150 | { |
| 151 | if ((intsize < 2) || (intspec[0] >= XILINX_INTC_MAXIRQS)) |
| 152 | return -EINVAL; |
| 153 | |
| 154 | /* keep a copy of the interrupt type til the interrupt is mapped |
| 155 | */ |
| 156 | xilinx_intc_typetable[intspec[0]] = xilinx_intc_map_senses[intspec[1]]; |
| 157 | |
| 158 | /* Xilinx uses 2 interrupt entries, the 1st being the h/w |
| 159 | * interrupt number, the 2nd being the interrupt type, edge or level |
| 160 | */ |
| 161 | *out_hwirq = intspec[0]; |
| 162 | *out_flags = xilinx_intc_map_senses[intspec[1]]; |
| 163 | |
| 164 | return 0; |
| 165 | } |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 166 | static int xilinx_intc_map(struct irq_domain *h, unsigned int virq, |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 167 | irq_hw_number_t irq) |
| 168 | { |
Thomas Gleixner | ec775d0 | 2011-03-25 16:45:20 +0100 | [diff] [blame] | 169 | irq_set_chip_data(virq, h->host_data); |
John Linn | ba10eed | 2009-05-14 10:23:11 -0600 | [diff] [blame] | 170 | |
| 171 | if (xilinx_intc_typetable[irq] == IRQ_TYPE_LEVEL_HIGH || |
| 172 | xilinx_intc_typetable[irq] == IRQ_TYPE_LEVEL_LOW) { |
Thomas Gleixner | ec775d0 | 2011-03-25 16:45:20 +0100 | [diff] [blame] | 173 | irq_set_chip_and_handler(virq, &xilinx_intc_level_irqchip, |
| 174 | handle_level_irq); |
John Linn | ba10eed | 2009-05-14 10:23:11 -0600 | [diff] [blame] | 175 | } else { |
Thomas Gleixner | ec775d0 | 2011-03-25 16:45:20 +0100 | [diff] [blame] | 176 | irq_set_chip_and_handler(virq, &xilinx_intc_edge_irqchip, |
| 177 | handle_edge_irq); |
John Linn | ba10eed | 2009-05-14 10:23:11 -0600 | [diff] [blame] | 178 | } |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 182 | static struct irq_domain_ops xilinx_intc_ops = { |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 183 | .map = xilinx_intc_map, |
John Linn | ba10eed | 2009-05-14 10:23:11 -0600 | [diff] [blame] | 184 | .xlate = xilinx_intc_xlate, |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 185 | }; |
| 186 | |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 187 | struct irq_domain * __init |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 188 | xilinx_intc_init(struct device_node *np) |
| 189 | { |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 190 | struct irq_domain * irq; |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 191 | void * regs; |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 192 | |
| 193 | /* Find and map the intc registers */ |
Grant Likely | 1745fbc | 2009-06-06 10:15:03 -0600 | [diff] [blame] | 194 | regs = of_iomap(np, 0); |
| 195 | if (!regs) { |
| 196 | pr_err("xilinx_intc: could not map registers\n"); |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 197 | return NULL; |
| 198 | } |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 199 | |
| 200 | /* Setup interrupt controller */ |
| 201 | out_be32(regs + XINTC_IER, 0); /* disable all irqs */ |
| 202 | out_be32(regs + XINTC_IAR, ~(u32) 0); /* Acknowledge pending irqs */ |
| 203 | out_be32(regs + XINTC_MER, 0x3UL); /* Turn on the Master Enable. */ |
| 204 | |
Grant Likely | bae1d8f | 2012-02-14 14:06:50 -0700 | [diff] [blame] | 205 | /* Allocate and initialize an irq_domain structure. */ |
Grant Likely | a8db8cf | 2012-02-14 14:06:54 -0700 | [diff] [blame] | 206 | irq = irq_domain_add_linear(np, XILINX_INTC_MAXIRQS, &xilinx_intc_ops, |
| 207 | regs); |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 208 | if (!irq) |
| 209 | panic(__FILE__ ": Cannot allocate IRQ host\n"); |
Grant Likely | 1745fbc | 2009-06-06 10:15:03 -0600 | [diff] [blame] | 210 | |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 211 | return irq; |
| 212 | } |
| 213 | |
| 214 | int xilinx_intc_get_irq(void) |
| 215 | { |
| 216 | void * regs = master_irqhost->host_data; |
| 217 | pr_debug("get_irq:\n"); |
| 218 | return irq_linear_revmap(master_irqhost, in_be32(regs + XINTC_IVR)); |
| 219 | } |
| 220 | |
Grant Likely | 1745fbc | 2009-06-06 10:15:03 -0600 | [diff] [blame] | 221 | #if defined(CONFIG_PPC_I8259) |
| 222 | /* |
| 223 | * Support code for cascading to 8259 interrupt controllers |
| 224 | */ |
| 225 | static void xilinx_i8259_cascade(unsigned int irq, struct irq_desc *desc) |
| 226 | { |
Thomas Gleixner | ec775d0 | 2011-03-25 16:45:20 +0100 | [diff] [blame] | 227 | struct irq_chip *chip = irq_desc_get_chip(desc); |
Grant Likely | 1745fbc | 2009-06-06 10:15:03 -0600 | [diff] [blame] | 228 | unsigned int cascade_irq = i8259_irq(); |
Lennert Buytenhek | 73909af | 2011-03-08 22:27:07 +0000 | [diff] [blame] | 229 | |
Grant Likely | 1745fbc | 2009-06-06 10:15:03 -0600 | [diff] [blame] | 230 | if (cascade_irq) |
| 231 | generic_handle_irq(cascade_irq); |
| 232 | |
| 233 | /* Let xilinx_intc end the interrupt */ |
Lennert Buytenhek | 73909af | 2011-03-08 22:27:07 +0000 | [diff] [blame] | 234 | chip->irq_unmask(&desc->irq_data); |
Grant Likely | 1745fbc | 2009-06-06 10:15:03 -0600 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static void __init xilinx_i8259_setup_cascade(void) |
| 238 | { |
| 239 | struct device_node *cascade_node; |
| 240 | int cascade_irq; |
| 241 | |
| 242 | /* Initialize i8259 controller */ |
| 243 | cascade_node = of_find_compatible_node(NULL, NULL, "chrp,iic"); |
| 244 | if (!cascade_node) |
| 245 | return; |
| 246 | |
| 247 | cascade_irq = irq_of_parse_and_map(cascade_node, 0); |
| 248 | if (!cascade_irq) { |
| 249 | pr_err("virtex_ml510: Failed to map cascade interrupt\n"); |
| 250 | goto out; |
| 251 | } |
| 252 | |
| 253 | i8259_init(cascade_node, 0); |
Thomas Gleixner | ec775d0 | 2011-03-25 16:45:20 +0100 | [diff] [blame] | 254 | irq_set_chained_handler(cascade_irq, xilinx_i8259_cascade); |
Grant Likely | 1745fbc | 2009-06-06 10:15:03 -0600 | [diff] [blame] | 255 | |
Roderick Colenbrander | e52ba9c | 2009-06-06 10:15:24 -0600 | [diff] [blame] | 256 | /* Program irq 7 (usb/audio), 14/15 (ide) to level sensitive */ |
| 257 | /* This looks like a dirty hack to me --gcl */ |
| 258 | outb(0xc0, 0x4d0); |
| 259 | outb(0xc0, 0x4d1); |
| 260 | |
Grant Likely | 1745fbc | 2009-06-06 10:15:03 -0600 | [diff] [blame] | 261 | out: |
| 262 | of_node_put(cascade_node); |
| 263 | } |
| 264 | #else |
| 265 | static inline void xilinx_i8259_setup_cascade(void) { return; } |
| 266 | #endif /* defined(CONFIG_PPC_I8259) */ |
| 267 | |
Uwe Kleine-König | ce6d73c | 2014-09-10 21:56:38 +0200 | [diff] [blame] | 268 | static const struct of_device_id xilinx_intc_match[] __initconst = { |
Grant Likely | 1745fbc | 2009-06-06 10:15:03 -0600 | [diff] [blame] | 269 | { .compatible = "xlnx,opb-intc-1.00.c", }, |
| 270 | { .compatible = "xlnx,xps-intc-1.00.a", }, |
| 271 | {} |
| 272 | }; |
| 273 | |
| 274 | /* |
| 275 | * Initialize master Xilinx interrupt controller |
| 276 | */ |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 277 | void __init xilinx_intc_init_tree(void) |
| 278 | { |
| 279 | struct device_node *np; |
| 280 | |
| 281 | /* find top level interrupt controller */ |
Grant Likely | 1745fbc | 2009-06-06 10:15:03 -0600 | [diff] [blame] | 282 | for_each_matching_node(np, xilinx_intc_match) { |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 283 | if (!of_get_property(np, "interrupts", NULL)) |
| 284 | break; |
| 285 | } |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 286 | BUG_ON(!np); |
| 287 | |
| 288 | master_irqhost = xilinx_intc_init(np); |
| 289 | BUG_ON(!master_irqhost); |
| 290 | |
| 291 | irq_set_default_host(master_irqhost); |
| 292 | of_node_put(np); |
Grant Likely | 1745fbc | 2009-06-06 10:15:03 -0600 | [diff] [blame] | 293 | |
| 294 | xilinx_i8259_setup_cascade(); |
Grant Likely | 4dc9783 | 2007-10-02 12:15:23 +1000 | [diff] [blame] | 295 | } |