blob: f4fdc94ad2c0a70953af6644c9920de5d204eb86 [file] [log] [blame]
Grant Likely4dc97832007-10-02 12:15:23 +10001/*
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 Herring26a20562013-09-26 07:40:04 -050026#include <linux/of_address.h>
Grant Likely4dc97832007-10-02 12:15:23 +100027#include <asm/io.h>
28#include <asm/processor.h>
Grant Likely1745fbc2009-06-06 10:15:03 -060029#include <asm/i8259.h>
Grant Likely4dc97832007-10-02 12:15:23 +100030#include <asm/irq.h>
31
32/*
33 * INTC Registers
34 */
35#define XINTC_ISR 0 /* Interrupt Status */
36#define XINTC_IPR 4 /* Interrupt Pending */
37#define XINTC_IER 8 /* Interrupt Enable */
38#define XINTC_IAR 12 /* Interrupt Acknowledge */
39#define XINTC_SIE 16 /* Set Interrupt Enable bits */
40#define XINTC_CIE 20 /* Clear Interrupt Enable bits */
41#define XINTC_IVR 24 /* Interrupt Vector */
42#define XINTC_MER 28 /* Master Enable */
43
Grant Likelybae1d8f2012-02-14 14:06:50 -070044static struct irq_domain *master_irqhost;
Grant Likely4dc97832007-10-02 12:15:23 +100045
John Linnba10eed2009-05-14 10:23:11 -060046#define XILINX_INTC_MAXIRQS (32)
47
48/* The following table allows the interrupt type, edge or level,
49 * to be cached after being read from the device tree until the interrupt
50 * is mapped
51 */
52static int xilinx_intc_typetable[XILINX_INTC_MAXIRQS];
53
54/* Map the interrupt type from the device tree to the interrupt types
55 * used by the interrupt subsystem
56 */
57static unsigned char xilinx_intc_map_senses[] = {
58 IRQ_TYPE_EDGE_RISING,
59 IRQ_TYPE_EDGE_FALLING,
60 IRQ_TYPE_LEVEL_HIGH,
61 IRQ_TYPE_LEVEL_LOW,
62};
63
Grant Likely4dc97832007-10-02 12:15:23 +100064/*
John Linnba10eed2009-05-14 10:23:11 -060065 * The interrupt controller is setup such that it doesn't work well with
66 * the level interrupt handler in the kernel because the handler acks the
67 * interrupt before calling the application interrupt handler. To deal with
68 * that, we use 2 different irq chips so that different functions can be
69 * used for level and edge type interrupts.
70 *
71 * IRQ Chip common (across level and edge) operations
Grant Likely4dc97832007-10-02 12:15:23 +100072 */
Lennert Buytenhek73909af2011-03-08 22:27:07 +000073static void xilinx_intc_mask(struct irq_data *d)
Grant Likely4dc97832007-10-02 12:15:23 +100074{
Grant Likely476eb492011-05-04 15:02:15 +100075 int irq = irqd_to_hwirq(d);
Lennert Buytenhek73909af2011-03-08 22:27:07 +000076 void * regs = irq_data_get_irq_chip_data(d);
Grant Likely4dc97832007-10-02 12:15:23 +100077 pr_debug("mask: %d\n", irq);
78 out_be32(regs + XINTC_CIE, 1 << irq);
79}
80
Lennert Buytenhek73909af2011-03-08 22:27:07 +000081static int xilinx_intc_set_type(struct irq_data *d, unsigned int flow_type)
John Linnba10eed2009-05-14 10:23:11 -060082{
John Linnba10eed2009-05-14 10:23:11 -060083 return 0;
84}
85
86/*
87 * IRQ Chip level operations
88 */
Lennert Buytenhek73909af2011-03-08 22:27:07 +000089static void xilinx_intc_level_unmask(struct irq_data *d)
Grant Likely4dc97832007-10-02 12:15:23 +100090{
Grant Likely476eb492011-05-04 15:02:15 +100091 int irq = irqd_to_hwirq(d);
Lennert Buytenhek73909af2011-03-08 22:27:07 +000092 void * regs = irq_data_get_irq_chip_data(d);
Grant Likely4dc97832007-10-02 12:15:23 +100093 pr_debug("unmask: %d\n", irq);
94 out_be32(regs + XINTC_SIE, 1 << irq);
John Linnba10eed2009-05-14 10:23:11 -060095
96 /* ack level irqs because they can't be acked during
97 * ack function since the handle_level_irq function
98 * acks the irq before calling the inerrupt handler
99 */
100 out_be32(regs + XINTC_IAR, 1 << irq);
Grant Likely4dc97832007-10-02 12:15:23 +1000101}
102
John Linnba10eed2009-05-14 10:23:11 -0600103static struct irq_chip xilinx_intc_level_irqchip = {
Thomas Gleixnerb27df672009-11-18 23:44:21 +0000104 .name = "Xilinx Level INTC",
Lennert Buytenhek73909af2011-03-08 22:27:07 +0000105 .irq_mask = xilinx_intc_mask,
106 .irq_mask_ack = xilinx_intc_mask,
107 .irq_unmask = xilinx_intc_level_unmask,
108 .irq_set_type = xilinx_intc_set_type,
John Linnba10eed2009-05-14 10:23:11 -0600109};
110
111/*
112 * IRQ Chip edge operations
113 */
Lennert Buytenhek73909af2011-03-08 22:27:07 +0000114static void xilinx_intc_edge_unmask(struct irq_data *d)
John Linnba10eed2009-05-14 10:23:11 -0600115{
Grant Likely476eb492011-05-04 15:02:15 +1000116 int irq = irqd_to_hwirq(d);
Lennert Buytenhek73909af2011-03-08 22:27:07 +0000117 void *regs = irq_data_get_irq_chip_data(d);
John Linnba10eed2009-05-14 10:23:11 -0600118 pr_debug("unmask: %d\n", irq);
119 out_be32(regs + XINTC_SIE, 1 << irq);
120}
121
Lennert Buytenhek73909af2011-03-08 22:27:07 +0000122static void xilinx_intc_edge_ack(struct irq_data *d)
Grant Likely4dc97832007-10-02 12:15:23 +1000123{
Grant Likely476eb492011-05-04 15:02:15 +1000124 int irq = irqd_to_hwirq(d);
Lennert Buytenhek73909af2011-03-08 22:27:07 +0000125 void * regs = irq_data_get_irq_chip_data(d);
Grant Likely4dc97832007-10-02 12:15:23 +1000126 pr_debug("ack: %d\n", irq);
127 out_be32(regs + XINTC_IAR, 1 << irq);
128}
129
John Linnba10eed2009-05-14 10:23:11 -0600130static struct irq_chip xilinx_intc_edge_irqchip = {
Thomas Gleixnerb27df672009-11-18 23:44:21 +0000131 .name = "Xilinx Edge INTC",
Lennert Buytenhek73909af2011-03-08 22:27:07 +0000132 .irq_mask = xilinx_intc_mask,
133 .irq_unmask = xilinx_intc_edge_unmask,
134 .irq_ack = xilinx_intc_edge_ack,
135 .irq_set_type = xilinx_intc_set_type,
Grant Likely4dc97832007-10-02 12:15:23 +1000136};
137
138/*
139 * IRQ Host operations
140 */
John Linnba10eed2009-05-14 10:23:11 -0600141
142/**
143 * xilinx_intc_xlate - translate virq# from device tree interrupts property
144 */
Grant Likelybae1d8f2012-02-14 14:06:50 -0700145static int xilinx_intc_xlate(struct irq_domain *h, struct device_node *ct,
Roman Fietze40d50cf2009-12-08 02:39:50 +0000146 const u32 *intspec, unsigned int intsize,
John Linnba10eed2009-05-14 10:23:11 -0600147 irq_hw_number_t *out_hwirq,
148 unsigned int *out_flags)
149{
150 if ((intsize < 2) || (intspec[0] >= XILINX_INTC_MAXIRQS))
151 return -EINVAL;
152
153 /* keep a copy of the interrupt type til the interrupt is mapped
154 */
155 xilinx_intc_typetable[intspec[0]] = xilinx_intc_map_senses[intspec[1]];
156
157 /* Xilinx uses 2 interrupt entries, the 1st being the h/w
158 * interrupt number, the 2nd being the interrupt type, edge or level
159 */
160 *out_hwirq = intspec[0];
161 *out_flags = xilinx_intc_map_senses[intspec[1]];
162
163 return 0;
164}
Grant Likelybae1d8f2012-02-14 14:06:50 -0700165static int xilinx_intc_map(struct irq_domain *h, unsigned int virq,
Grant Likely4dc97832007-10-02 12:15:23 +1000166 irq_hw_number_t irq)
167{
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100168 irq_set_chip_data(virq, h->host_data);
John Linnba10eed2009-05-14 10:23:11 -0600169
170 if (xilinx_intc_typetable[irq] == IRQ_TYPE_LEVEL_HIGH ||
171 xilinx_intc_typetable[irq] == IRQ_TYPE_LEVEL_LOW) {
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100172 irq_set_chip_and_handler(virq, &xilinx_intc_level_irqchip,
173 handle_level_irq);
John Linnba10eed2009-05-14 10:23:11 -0600174 } else {
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100175 irq_set_chip_and_handler(virq, &xilinx_intc_edge_irqchip,
176 handle_edge_irq);
John Linnba10eed2009-05-14 10:23:11 -0600177 }
Grant Likely4dc97832007-10-02 12:15:23 +1000178 return 0;
179}
180
Grant Likelybae1d8f2012-02-14 14:06:50 -0700181static struct irq_domain_ops xilinx_intc_ops = {
Grant Likely4dc97832007-10-02 12:15:23 +1000182 .map = xilinx_intc_map,
John Linnba10eed2009-05-14 10:23:11 -0600183 .xlate = xilinx_intc_xlate,
Grant Likely4dc97832007-10-02 12:15:23 +1000184};
185
Grant Likelybae1d8f2012-02-14 14:06:50 -0700186struct irq_domain * __init
Grant Likely4dc97832007-10-02 12:15:23 +1000187xilinx_intc_init(struct device_node *np)
188{
Grant Likelybae1d8f2012-02-14 14:06:50 -0700189 struct irq_domain * irq;
Grant Likely4dc97832007-10-02 12:15:23 +1000190 void * regs;
Grant Likely4dc97832007-10-02 12:15:23 +1000191
192 /* Find and map the intc registers */
Grant Likely1745fbc2009-06-06 10:15:03 -0600193 regs = of_iomap(np, 0);
194 if (!regs) {
195 pr_err("xilinx_intc: could not map registers\n");
Grant Likely4dc97832007-10-02 12:15:23 +1000196 return NULL;
197 }
Grant Likely4dc97832007-10-02 12:15:23 +1000198
199 /* Setup interrupt controller */
200 out_be32(regs + XINTC_IER, 0); /* disable all irqs */
201 out_be32(regs + XINTC_IAR, ~(u32) 0); /* Acknowledge pending irqs */
202 out_be32(regs + XINTC_MER, 0x3UL); /* Turn on the Master Enable. */
203
Grant Likelybae1d8f2012-02-14 14:06:50 -0700204 /* Allocate and initialize an irq_domain structure. */
Grant Likelya8db8cf2012-02-14 14:06:54 -0700205 irq = irq_domain_add_linear(np, XILINX_INTC_MAXIRQS, &xilinx_intc_ops,
206 regs);
Grant Likely4dc97832007-10-02 12:15:23 +1000207 if (!irq)
208 panic(__FILE__ ": Cannot allocate IRQ host\n");
Grant Likely1745fbc2009-06-06 10:15:03 -0600209
Grant Likely4dc97832007-10-02 12:15:23 +1000210 return irq;
211}
212
213int xilinx_intc_get_irq(void)
214{
215 void * regs = master_irqhost->host_data;
216 pr_debug("get_irq:\n");
217 return irq_linear_revmap(master_irqhost, in_be32(regs + XINTC_IVR));
218}
219
Grant Likely1745fbc2009-06-06 10:15:03 -0600220#if defined(CONFIG_PPC_I8259)
221/*
222 * Support code for cascading to 8259 interrupt controllers
223 */
224static void xilinx_i8259_cascade(unsigned int irq, struct irq_desc *desc)
225{
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100226 struct irq_chip *chip = irq_desc_get_chip(desc);
Grant Likely1745fbc2009-06-06 10:15:03 -0600227 unsigned int cascade_irq = i8259_irq();
Lennert Buytenhek73909af2011-03-08 22:27:07 +0000228
Grant Likely1745fbc2009-06-06 10:15:03 -0600229 if (cascade_irq)
230 generic_handle_irq(cascade_irq);
231
232 /* Let xilinx_intc end the interrupt */
Lennert Buytenhek73909af2011-03-08 22:27:07 +0000233 chip->irq_unmask(&desc->irq_data);
Grant Likely1745fbc2009-06-06 10:15:03 -0600234}
235
236static void __init xilinx_i8259_setup_cascade(void)
237{
238 struct device_node *cascade_node;
239 int cascade_irq;
240
241 /* Initialize i8259 controller */
242 cascade_node = of_find_compatible_node(NULL, NULL, "chrp,iic");
243 if (!cascade_node)
244 return;
245
246 cascade_irq = irq_of_parse_and_map(cascade_node, 0);
247 if (!cascade_irq) {
248 pr_err("virtex_ml510: Failed to map cascade interrupt\n");
249 goto out;
250 }
251
252 i8259_init(cascade_node, 0);
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100253 irq_set_chained_handler(cascade_irq, xilinx_i8259_cascade);
Grant Likely1745fbc2009-06-06 10:15:03 -0600254
Roderick Colenbrandere52ba9c2009-06-06 10:15:24 -0600255 /* Program irq 7 (usb/audio), 14/15 (ide) to level sensitive */
256 /* This looks like a dirty hack to me --gcl */
257 outb(0xc0, 0x4d0);
258 outb(0xc0, 0x4d1);
259
Grant Likely1745fbc2009-06-06 10:15:03 -0600260 out:
261 of_node_put(cascade_node);
262}
263#else
264static inline void xilinx_i8259_setup_cascade(void) { return; }
265#endif /* defined(CONFIG_PPC_I8259) */
266
267static struct of_device_id xilinx_intc_match[] __initconst = {
268 { .compatible = "xlnx,opb-intc-1.00.c", },
269 { .compatible = "xlnx,xps-intc-1.00.a", },
270 {}
271};
272
273/*
274 * Initialize master Xilinx interrupt controller
275 */
Grant Likely4dc97832007-10-02 12:15:23 +1000276void __init xilinx_intc_init_tree(void)
277{
278 struct device_node *np;
279
280 /* find top level interrupt controller */
Grant Likely1745fbc2009-06-06 10:15:03 -0600281 for_each_matching_node(np, xilinx_intc_match) {
Grant Likely4dc97832007-10-02 12:15:23 +1000282 if (!of_get_property(np, "interrupts", NULL))
283 break;
284 }
Grant Likely4dc97832007-10-02 12:15:23 +1000285 BUG_ON(!np);
286
287 master_irqhost = xilinx_intc_init(np);
288 BUG_ON(!master_irqhost);
289
290 irq_set_default_host(master_irqhost);
291 of_node_put(np);
Grant Likely1745fbc2009-06-06 10:15:03 -0600292
293 xilinx_i8259_setup_cascade();
Grant Likely4dc97832007-10-02 12:15:23 +1000294}