2 * Copyright (C) 2007-2013 Michal Simek <monstr@monstr.eu>
3 * Copyright (C) 2012-2013 Xilinx, Inc.
4 * Copyright (C) 2007-2009 PetaLogix
5 * Copyright (C) 2006 Atmark Techno, Inc.
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
12 #include <linux/irqdomain.h>
13 #include <linux/irq.h>
14 #include <linux/of_address.h>
16 #include <linux/bug.h>
18 #include "../../drivers/irqchip/irqchip.h"
20 static void __iomem
*intc_baseaddr
;
22 /* No one else should require these constants, so define them locally here. */
23 #define ISR 0x00 /* Interrupt Status Register */
24 #define IPR 0x04 /* Interrupt Pending Register */
25 #define IER 0x08 /* Interrupt Enable Register */
26 #define IAR 0x0c /* Interrupt Acknowledge Register */
27 #define SIE 0x10 /* Set Interrupt Enable bits */
28 #define CIE 0x14 /* Clear Interrupt Enable bits */
29 #define IVR 0x18 /* Interrupt Vector Register */
30 #define MER 0x1c /* Master Enable Register */
33 #define MER_HIE (1<<1)
35 static unsigned int (*read_fn
)(void __iomem
*);
36 static void (*write_fn
)(u32
, void __iomem
*);
38 static void intc_write32(u32 val
, void __iomem
*addr
)
43 static unsigned int intc_read32(void __iomem
*addr
)
45 return ioread32(addr
);
48 static void intc_write32_be(u32 val
, void __iomem
*addr
)
50 iowrite32be(val
, addr
);
53 static unsigned int intc_read32_be(void __iomem
*addr
)
55 return ioread32be(addr
);
58 static void intc_enable_or_unmask(struct irq_data
*d
)
60 unsigned long mask
= 1 << d
->hwirq
;
62 pr_debug("enable_or_unmask: %ld\n", d
->hwirq
);
64 /* ack level irqs because they can't be acked during
65 * ack function since the handle_level_irq function
66 * acks the irq before calling the interrupt handler
68 if (irqd_is_level_type(d
))
69 write_fn(mask
, intc_baseaddr
+ IAR
);
71 write_fn(mask
, intc_baseaddr
+ SIE
);
74 static void intc_disable_or_mask(struct irq_data
*d
)
76 pr_debug("disable: %ld\n", d
->hwirq
);
77 write_fn(1 << d
->hwirq
, intc_baseaddr
+ CIE
);
80 static void intc_ack(struct irq_data
*d
)
82 pr_debug("ack: %ld\n", d
->hwirq
);
83 write_fn(1 << d
->hwirq
, intc_baseaddr
+ IAR
);
86 static void intc_mask_ack(struct irq_data
*d
)
88 unsigned long mask
= 1 << d
->hwirq
;
90 pr_debug("disable_and_ack: %ld\n", d
->hwirq
);
91 write_fn(mask
, intc_baseaddr
+ CIE
);
92 write_fn(mask
, intc_baseaddr
+ IAR
);
95 static struct irq_chip intc_dev
= {
96 .name
= "Xilinx INTC",
97 .irq_unmask
= intc_enable_or_unmask
,
98 .irq_mask
= intc_disable_or_mask
,
100 .irq_mask_ack
= intc_mask_ack
,
103 static struct irq_domain
*root_domain
;
105 unsigned int get_irq(void)
107 unsigned int hwirq
, irq
= -1;
109 hwirq
= read_fn(intc_baseaddr
+ IVR
);
111 irq
= irq_find_mapping(root_domain
, hwirq
);
113 pr_debug("get_irq: hwirq=%d, irq=%d\n", hwirq
, irq
);
118 static int xintc_map(struct irq_domain
*d
, unsigned int irq
, irq_hw_number_t hw
)
120 u32 intr_mask
= (u32
)d
->host_data
;
122 if (intr_mask
& (1 << hw
)) {
123 irq_set_chip_and_handler_name(irq
, &intc_dev
,
124 handle_edge_irq
, "edge");
125 irq_clear_status_flags(irq
, IRQ_LEVEL
);
127 irq_set_chip_and_handler_name(irq
, &intc_dev
,
128 handle_level_irq
, "level");
129 irq_set_status_flags(irq
, IRQ_LEVEL
);
134 static const struct irq_domain_ops xintc_irq_domain_ops
= {
135 .xlate
= irq_domain_xlate_onetwocell
,
139 static int __init
xilinx_intc_of_init(struct device_node
*intc
,
140 struct device_node
*parent
)
142 u32 nr_irq
, intr_mask
;
145 intc_baseaddr
= of_iomap(intc
, 0);
146 BUG_ON(!intc_baseaddr
);
148 ret
= of_property_read_u32(intc
, "xlnx,num-intr-inputs", &nr_irq
);
150 pr_err("%s: unable to read xlnx,num-intr-inputs\n", __func__
);
154 ret
= of_property_read_u32(intc
, "xlnx,kind-of-intr", &intr_mask
);
156 pr_err("%s: unable to read xlnx,kind-of-intr\n", __func__
);
160 if (intr_mask
>> nr_irq
)
161 pr_warn("%s: mismatch in kind-of-intr param\n", __func__
);
163 pr_info("%s: num_irq=%d, edge=0x%x\n",
164 intc
->full_name
, nr_irq
, intr_mask
);
166 write_fn
= intc_write32
;
167 read_fn
= intc_read32
;
170 * Disable all external interrupts until they are
171 * explicity requested.
173 write_fn(0, intc_baseaddr
+ IER
);
175 /* Acknowledge any pending interrupts just in case. */
176 write_fn(0xffffffff, intc_baseaddr
+ IAR
);
178 /* Turn on the Master Enable. */
179 write_fn(MER_HIE
| MER_ME
, intc_baseaddr
+ MER
);
180 if (!(read_fn(intc_baseaddr
+ MER
) & (MER_HIE
| MER_ME
))) {
181 write_fn
= intc_write32_be
;
182 read_fn
= intc_read32_be
;
183 write_fn(MER_HIE
| MER_ME
, intc_baseaddr
+ MER
);
186 /* Yeah, okay, casting the intr_mask to a void* is butt-ugly, but I'm
187 * lazy and Michal can clean it up to something nicer when he tests
188 * and commits this patch. ~~gcl */
189 root_domain
= irq_domain_add_linear(intc
, nr_irq
, &xintc_irq_domain_ops
,
192 irq_set_default_host(root_domain
);
197 IRQCHIP_DECLARE(xilinx_intc
, "xlnx,xps-intc-1.00.a", xilinx_intc_of_init
);