blob: 7436f3ed4df6e0b81ae0959911018517d5c395a3 [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>
26#include <asm/io.h>
27#include <asm/processor.h>
Grant Likely1745fbc2009-06-06 10:15:03 -060028#include <asm/i8259.h>
Grant Likely4dc97832007-10-02 12:15:23 +100029#include <asm/irq.h>
30
31/*
32 * INTC Registers
33 */
34#define XINTC_ISR 0 /* Interrupt Status */
35#define XINTC_IPR 4 /* Interrupt Pending */
36#define XINTC_IER 8 /* Interrupt Enable */
37#define XINTC_IAR 12 /* Interrupt Acknowledge */
38#define XINTC_SIE 16 /* Set Interrupt Enable bits */
39#define XINTC_CIE 20 /* Clear Interrupt Enable bits */
40#define XINTC_IVR 24 /* Interrupt Vector */
41#define XINTC_MER 28 /* Master Enable */
42
43static struct irq_host *master_irqhost;
44
John Linnba10eed2009-05-14 10:23:11 -060045#define XILINX_INTC_MAXIRQS (32)
46
47/* The following table allows the interrupt type, edge or level,
48 * to be cached after being read from the device tree until the interrupt
49 * is mapped
50 */
51static int xilinx_intc_typetable[XILINX_INTC_MAXIRQS];
52
53/* Map the interrupt type from the device tree to the interrupt types
54 * used by the interrupt subsystem
55 */
56static unsigned char xilinx_intc_map_senses[] = {
57 IRQ_TYPE_EDGE_RISING,
58 IRQ_TYPE_EDGE_FALLING,
59 IRQ_TYPE_LEVEL_HIGH,
60 IRQ_TYPE_LEVEL_LOW,
61};
62
Grant Likely4dc97832007-10-02 12:15:23 +100063/*
John Linnba10eed2009-05-14 10:23:11 -060064 * The interrupt controller is setup such that it doesn't work well with
65 * the level interrupt handler in the kernel because the handler acks the
66 * interrupt before calling the application interrupt handler. To deal with
67 * that, we use 2 different irq chips so that different functions can be
68 * used for level and edge type interrupts.
69 *
70 * IRQ Chip common (across level and edge) operations
Grant Likely4dc97832007-10-02 12:15:23 +100071 */
Lennert Buytenhek73909af2011-03-08 22:27:07 +000072static void xilinx_intc_mask(struct irq_data *d)
Grant Likely4dc97832007-10-02 12:15:23 +100073{
Lennert Buytenhek73909af2011-03-08 22:27:07 +000074 int irq = virq_to_hw(d->irq);
75 void * regs = irq_data_get_irq_chip_data(d);
Grant Likely4dc97832007-10-02 12:15:23 +100076 pr_debug("mask: %d\n", irq);
77 out_be32(regs + XINTC_CIE, 1 << irq);
78}
79
Lennert Buytenhek73909af2011-03-08 22:27:07 +000080static int xilinx_intc_set_type(struct irq_data *d, unsigned int flow_type)
John Linnba10eed2009-05-14 10:23:11 -060081{
Lennert Buytenhek73909af2011-03-08 22:27:07 +000082 struct irq_desc *desc = irq_to_desc(d->irq);
John Linnba10eed2009-05-14 10:23:11 -060083
84 desc->status &= ~(IRQ_TYPE_SENSE_MASK | IRQ_LEVEL);
85 desc->status |= flow_type & IRQ_TYPE_SENSE_MASK;
86 if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
87 desc->status |= IRQ_LEVEL;
88 return 0;
89}
90
91/*
92 * IRQ Chip level operations
93 */
Lennert Buytenhek73909af2011-03-08 22:27:07 +000094static void xilinx_intc_level_unmask(struct irq_data *d)
Grant Likely4dc97832007-10-02 12:15:23 +100095{
Lennert Buytenhek73909af2011-03-08 22:27:07 +000096 int irq = virq_to_hw(d->irq);
97 void * regs = irq_data_get_irq_chip_data(d);
Grant Likely4dc97832007-10-02 12:15:23 +100098 pr_debug("unmask: %d\n", irq);
99 out_be32(regs + XINTC_SIE, 1 << irq);
John Linnba10eed2009-05-14 10:23:11 -0600100
101 /* ack level irqs because they can't be acked during
102 * ack function since the handle_level_irq function
103 * acks the irq before calling the inerrupt handler
104 */
105 out_be32(regs + XINTC_IAR, 1 << irq);
Grant Likely4dc97832007-10-02 12:15:23 +1000106}
107
John Linnba10eed2009-05-14 10:23:11 -0600108static struct irq_chip xilinx_intc_level_irqchip = {
Thomas Gleixnerb27df672009-11-18 23:44:21 +0000109 .name = "Xilinx Level INTC",
Lennert Buytenhek73909af2011-03-08 22:27:07 +0000110 .irq_mask = xilinx_intc_mask,
111 .irq_mask_ack = xilinx_intc_mask,
112 .irq_unmask = xilinx_intc_level_unmask,
113 .irq_set_type = xilinx_intc_set_type,
John Linnba10eed2009-05-14 10:23:11 -0600114};
115
116/*
117 * IRQ Chip edge operations
118 */
Lennert Buytenhek73909af2011-03-08 22:27:07 +0000119static void xilinx_intc_edge_unmask(struct irq_data *d)
John Linnba10eed2009-05-14 10:23:11 -0600120{
Lennert Buytenhek73909af2011-03-08 22:27:07 +0000121 int irq = virq_to_hw(d->irq);
122 void *regs = irq_data_get_irq_chip_data(d);
John Linnba10eed2009-05-14 10:23:11 -0600123 pr_debug("unmask: %d\n", irq);
124 out_be32(regs + XINTC_SIE, 1 << irq);
125}
126
Lennert Buytenhek73909af2011-03-08 22:27:07 +0000127static void xilinx_intc_edge_ack(struct irq_data *d)
Grant Likely4dc97832007-10-02 12:15:23 +1000128{
Lennert Buytenhek73909af2011-03-08 22:27:07 +0000129 int irq = virq_to_hw(d->irq);
130 void * regs = irq_data_get_irq_chip_data(d);
Grant Likely4dc97832007-10-02 12:15:23 +1000131 pr_debug("ack: %d\n", irq);
132 out_be32(regs + XINTC_IAR, 1 << irq);
133}
134
John Linnba10eed2009-05-14 10:23:11 -0600135static struct irq_chip xilinx_intc_edge_irqchip = {
Thomas Gleixnerb27df672009-11-18 23:44:21 +0000136 .name = "Xilinx Edge INTC",
Lennert Buytenhek73909af2011-03-08 22:27:07 +0000137 .irq_mask = xilinx_intc_mask,
138 .irq_unmask = xilinx_intc_edge_unmask,
139 .irq_ack = xilinx_intc_edge_ack,
140 .irq_set_type = xilinx_intc_set_type,
Grant Likely4dc97832007-10-02 12:15:23 +1000141};
142
143/*
144 * IRQ Host operations
145 */
John Linnba10eed2009-05-14 10:23:11 -0600146
147/**
148 * xilinx_intc_xlate - translate virq# from device tree interrupts property
149 */
150static int xilinx_intc_xlate(struct irq_host *h, struct device_node *ct,
Roman Fietze40d50cf2009-12-08 02:39:50 +0000151 const u32 *intspec, unsigned int intsize,
John Linnba10eed2009-05-14 10:23:11 -0600152 irq_hw_number_t *out_hwirq,
153 unsigned int *out_flags)
154{
155 if ((intsize < 2) || (intspec[0] >= XILINX_INTC_MAXIRQS))
156 return -EINVAL;
157
158 /* keep a copy of the interrupt type til the interrupt is mapped
159 */
160 xilinx_intc_typetable[intspec[0]] = xilinx_intc_map_senses[intspec[1]];
161
162 /* Xilinx uses 2 interrupt entries, the 1st being the h/w
163 * interrupt number, the 2nd being the interrupt type, edge or level
164 */
165 *out_hwirq = intspec[0];
166 *out_flags = xilinx_intc_map_senses[intspec[1]];
167
168 return 0;
169}
Grant Likely4dc97832007-10-02 12:15:23 +1000170static int xilinx_intc_map(struct irq_host *h, unsigned int virq,
171 irq_hw_number_t irq)
172{
173 set_irq_chip_data(virq, h->host_data);
John Linnba10eed2009-05-14 10:23:11 -0600174
175 if (xilinx_intc_typetable[irq] == IRQ_TYPE_LEVEL_HIGH ||
176 xilinx_intc_typetable[irq] == IRQ_TYPE_LEVEL_LOW) {
177 set_irq_chip_and_handler(virq, &xilinx_intc_level_irqchip,
178 handle_level_irq);
179 } else {
180 set_irq_chip_and_handler(virq, &xilinx_intc_edge_irqchip,
181 handle_edge_irq);
182 }
Grant Likely4dc97832007-10-02 12:15:23 +1000183 return 0;
184}
185
186static struct irq_host_ops xilinx_intc_ops = {
187 .map = xilinx_intc_map,
John Linnba10eed2009-05-14 10:23:11 -0600188 .xlate = xilinx_intc_xlate,
Grant Likely4dc97832007-10-02 12:15:23 +1000189};
190
191struct irq_host * __init
192xilinx_intc_init(struct device_node *np)
193{
194 struct irq_host * irq;
Grant Likely4dc97832007-10-02 12:15:23 +1000195 void * regs;
Grant Likely4dc97832007-10-02 12:15:23 +1000196
197 /* Find and map the intc registers */
Grant Likely1745fbc2009-06-06 10:15:03 -0600198 regs = of_iomap(np, 0);
199 if (!regs) {
200 pr_err("xilinx_intc: could not map registers\n");
Grant Likely4dc97832007-10-02 12:15:23 +1000201 return NULL;
202 }
Grant Likely4dc97832007-10-02 12:15:23 +1000203
204 /* Setup interrupt controller */
205 out_be32(regs + XINTC_IER, 0); /* disable all irqs */
206 out_be32(regs + XINTC_IAR, ~(u32) 0); /* Acknowledge pending irqs */
207 out_be32(regs + XINTC_MER, 0x3UL); /* Turn on the Master Enable. */
208
209 /* Allocate and initialize an irq_host structure. */
John Linnba10eed2009-05-14 10:23:11 -0600210 irq = irq_alloc_host(np, IRQ_HOST_MAP_LINEAR, XILINX_INTC_MAXIRQS,
211 &xilinx_intc_ops, -1);
Grant Likely4dc97832007-10-02 12:15:23 +1000212 if (!irq)
213 panic(__FILE__ ": Cannot allocate IRQ host\n");
214 irq->host_data = regs;
Grant Likely1745fbc2009-06-06 10:15:03 -0600215
Grant Likely4dc97832007-10-02 12:15:23 +1000216 return irq;
217}
218
219int xilinx_intc_get_irq(void)
220{
221 void * regs = master_irqhost->host_data;
222 pr_debug("get_irq:\n");
223 return irq_linear_revmap(master_irqhost, in_be32(regs + XINTC_IVR));
224}
225
Grant Likely1745fbc2009-06-06 10:15:03 -0600226#if defined(CONFIG_PPC_I8259)
227/*
228 * Support code for cascading to 8259 interrupt controllers
229 */
230static void xilinx_i8259_cascade(unsigned int irq, struct irq_desc *desc)
231{
Lennert Buytenhek73909af2011-03-08 22:27:07 +0000232 struct irq_chip *chip = get_irq_desc_chip(desc);
Grant Likely1745fbc2009-06-06 10:15:03 -0600233 unsigned int cascade_irq = i8259_irq();
Lennert Buytenhek73909af2011-03-08 22:27:07 +0000234
Grant Likely1745fbc2009-06-06 10:15:03 -0600235 if (cascade_irq)
236 generic_handle_irq(cascade_irq);
237
238 /* Let xilinx_intc end the interrupt */
Lennert Buytenhek73909af2011-03-08 22:27:07 +0000239 chip->irq_unmask(&desc->irq_data);
Grant Likely1745fbc2009-06-06 10:15:03 -0600240}
241
242static void __init xilinx_i8259_setup_cascade(void)
243{
244 struct device_node *cascade_node;
245 int cascade_irq;
246
247 /* Initialize i8259 controller */
248 cascade_node = of_find_compatible_node(NULL, NULL, "chrp,iic");
249 if (!cascade_node)
250 return;
251
252 cascade_irq = irq_of_parse_and_map(cascade_node, 0);
253 if (!cascade_irq) {
254 pr_err("virtex_ml510: Failed to map cascade interrupt\n");
255 goto out;
256 }
257
258 i8259_init(cascade_node, 0);
259 set_irq_chained_handler(cascade_irq, xilinx_i8259_cascade);
260
Roderick Colenbrandere52ba9c2009-06-06 10:15:24 -0600261 /* Program irq 7 (usb/audio), 14/15 (ide) to level sensitive */
262 /* This looks like a dirty hack to me --gcl */
263 outb(0xc0, 0x4d0);
264 outb(0xc0, 0x4d1);
265
Grant Likely1745fbc2009-06-06 10:15:03 -0600266 out:
267 of_node_put(cascade_node);
268}
269#else
270static inline void xilinx_i8259_setup_cascade(void) { return; }
271#endif /* defined(CONFIG_PPC_I8259) */
272
273static struct of_device_id xilinx_intc_match[] __initconst = {
274 { .compatible = "xlnx,opb-intc-1.00.c", },
275 { .compatible = "xlnx,xps-intc-1.00.a", },
276 {}
277};
278
279/*
280 * Initialize master Xilinx interrupt controller
281 */
Grant Likely4dc97832007-10-02 12:15:23 +1000282void __init xilinx_intc_init_tree(void)
283{
284 struct device_node *np;
285
286 /* find top level interrupt controller */
Grant Likely1745fbc2009-06-06 10:15:03 -0600287 for_each_matching_node(np, xilinx_intc_match) {
Grant Likely4dc97832007-10-02 12:15:23 +1000288 if (!of_get_property(np, "interrupts", NULL))
289 break;
290 }
Grant Likely4dc97832007-10-02 12:15:23 +1000291 BUG_ON(!np);
292
293 master_irqhost = xilinx_intc_init(np);
294 BUG_ON(!master_irqhost);
295
296 irq_set_default_host(master_irqhost);
297 of_node_put(np);
Grant Likely1745fbc2009-06-06 10:15:03 -0600298
299 xilinx_i8259_setup_cascade();
Grant Likely4dc97832007-10-02 12:15:23 +1000300}