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/irqchip.h>
15 #include <linux/irqchip/chained_irq.h>
16 #include <linux/of_address.h>
18 #include <linux/jump_label.h>
19 #include <linux/bug.h>
20 #include <linux/of_irq.h>
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 DEFINE_STATIC_KEY_FALSE(xintc_is_be
);
37 struct xintc_irq_chip
{
39 struct irq_domain
*root_domain
;
43 static struct xintc_irq_chip
*xintc_irqc
;
45 static void xintc_write(int reg
, u32 data
)
47 if (static_branch_unlikely(&xintc_is_be
))
48 iowrite32be(data
, xintc_irqc
->base
+ reg
);
50 iowrite32(data
, xintc_irqc
->base
+ reg
);
53 static unsigned int xintc_read(int reg
)
55 if (static_branch_unlikely(&xintc_is_be
))
56 return ioread32be(xintc_irqc
->base
+ reg
);
58 return ioread32(xintc_irqc
->base
+ reg
);
61 static void intc_enable_or_unmask(struct irq_data
*d
)
63 unsigned long mask
= 1 << d
->hwirq
;
65 pr_debug("irq-xilinx: enable_or_unmask: %ld\n", d
->hwirq
);
67 /* ack level irqs because they can't be acked during
68 * ack function since the handle_level_irq function
69 * acks the irq before calling the interrupt handler
71 if (irqd_is_level_type(d
))
72 xintc_write(IAR
, mask
);
74 xintc_write(SIE
, mask
);
77 static void intc_disable_or_mask(struct irq_data
*d
)
79 pr_debug("irq-xilinx: disable: %ld\n", d
->hwirq
);
80 xintc_write(CIE
, 1 << d
->hwirq
);
83 static void intc_ack(struct irq_data
*d
)
85 pr_debug("irq-xilinx: ack: %ld\n", d
->hwirq
);
86 xintc_write(IAR
, 1 << d
->hwirq
);
89 static void intc_mask_ack(struct irq_data
*d
)
91 unsigned long mask
= 1 << d
->hwirq
;
93 pr_debug("irq-xilinx: disable_and_ack: %ld\n", d
->hwirq
);
94 xintc_write(CIE
, mask
);
95 xintc_write(IAR
, mask
);
98 static struct irq_chip intc_dev
= {
99 .name
= "Xilinx INTC",
100 .irq_unmask
= intc_enable_or_unmask
,
101 .irq_mask
= intc_disable_or_mask
,
103 .irq_mask_ack
= intc_mask_ack
,
106 unsigned int xintc_get_irq(void)
108 unsigned int hwirq
, irq
= -1;
110 hwirq
= xintc_read(IVR
);
112 irq
= irq_find_mapping(xintc_irqc
->root_domain
, hwirq
);
114 pr_debug("irq-xilinx: hwirq=%d, irq=%d\n", hwirq
, irq
);
119 static int xintc_map(struct irq_domain
*d
, unsigned int irq
, irq_hw_number_t hw
)
121 if (xintc_irqc
->intr_mask
& (1 << hw
)) {
122 irq_set_chip_and_handler_name(irq
, &intc_dev
,
123 handle_edge_irq
, "edge");
124 irq_clear_status_flags(irq
, IRQ_LEVEL
);
126 irq_set_chip_and_handler_name(irq
, &intc_dev
,
127 handle_level_irq
, "level");
128 irq_set_status_flags(irq
, IRQ_LEVEL
);
133 static const struct irq_domain_ops xintc_irq_domain_ops
= {
134 .xlate
= irq_domain_xlate_onetwocell
,
138 static void xil_intc_irq_handler(struct irq_desc
*desc
)
140 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
143 chained_irq_enter(chip
, desc
);
145 pending
= xintc_get_irq();
148 generic_handle_irq(pending
);
150 chained_irq_exit(chip
, desc
);
153 static int __init
xilinx_intc_of_init(struct device_node
*intc
,
154 struct device_node
*parent
)
158 struct xintc_irq_chip
*irqc
;
161 pr_err("irq-xilinx: Multiple instances aren't supported\n");
165 irqc
= kzalloc(sizeof(*irqc
), GFP_KERNEL
);
171 irqc
->base
= of_iomap(intc
, 0);
174 ret
= of_property_read_u32(intc
, "xlnx,num-intr-inputs", &nr_irq
);
176 pr_err("irq-xilinx: unable to read xlnx,num-intr-inputs\n");
180 ret
= of_property_read_u32(intc
, "xlnx,kind-of-intr", &irqc
->intr_mask
);
182 pr_warn("irq-xilinx: unable to read xlnx,kind-of-intr\n");
186 if (irqc
->intr_mask
>> nr_irq
)
187 pr_warn("irq-xilinx: mismatch in kind-of-intr param\n");
189 pr_info("irq-xilinx: %s: num_irq=%d, edge=0x%x\n",
190 intc
->full_name
, nr_irq
, irqc
->intr_mask
);
194 * Disable all external interrupts until they are
195 * explicity requested.
199 /* Acknowledge any pending interrupts just in case. */
200 xintc_write(IAR
, 0xffffffff);
202 /* Turn on the Master Enable. */
203 xintc_write(MER
, MER_HIE
| MER_ME
);
204 if (!(xintc_read(MER
) & (MER_HIE
| MER_ME
))) {
205 static_branch_enable(&xintc_is_be
);
206 xintc_write(MER
, MER_HIE
| MER_ME
);
209 irqc
->root_domain
= irq_domain_add_linear(intc
, nr_irq
,
210 &xintc_irq_domain_ops
, irqc
);
211 if (!irqc
->root_domain
) {
212 pr_err("irq-xilinx: Unable to create IRQ domain\n");
217 irq
= irq_of_parse_and_map(intc
, 0);
219 irq_set_chained_handler_and_data(irq
,
220 xil_intc_irq_handler
,
223 pr_err("irq-xilinx: interrupts property not in DT\n");
228 irq_set_default_host(irqc
->root_domain
);
240 IRQCHIP_DECLARE(xilinx_intc_xps
, "xlnx,xps-intc-1.00.a", xilinx_intc_of_init
);
241 IRQCHIP_DECLARE(xilinx_intc_opb
, "xlnx,opb-intc-1.00.c", xilinx_intc_of_init
);