1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2007-2013 Michal Simek <monstr@monstr.eu>
3*4882a593Smuzhiyun * Copyright (C) 2012-2013 Xilinx, Inc.
4*4882a593Smuzhiyun * Copyright (C) 2007-2009 PetaLogix
5*4882a593Smuzhiyun * Copyright (C) 2006 Atmark Techno, Inc.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * This file is subject to the terms and conditions of the GNU General Public
8*4882a593Smuzhiyun * License. See the file "COPYING" in the main directory of this archive
9*4882a593Smuzhiyun * for more details.
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/irqdomain.h>
13*4882a593Smuzhiyun #include <linux/irq.h>
14*4882a593Smuzhiyun #include <linux/irqchip.h>
15*4882a593Smuzhiyun #include <linux/irqchip/chained_irq.h>
16*4882a593Smuzhiyun #include <linux/of_address.h>
17*4882a593Smuzhiyun #include <linux/io.h>
18*4882a593Smuzhiyun #include <linux/jump_label.h>
19*4882a593Smuzhiyun #include <linux/bug.h>
20*4882a593Smuzhiyun #include <linux/of_irq.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun /* No one else should require these constants, so define them locally here. */
23*4882a593Smuzhiyun #define ISR 0x00 /* Interrupt Status Register */
24*4882a593Smuzhiyun #define IPR 0x04 /* Interrupt Pending Register */
25*4882a593Smuzhiyun #define IER 0x08 /* Interrupt Enable Register */
26*4882a593Smuzhiyun #define IAR 0x0c /* Interrupt Acknowledge Register */
27*4882a593Smuzhiyun #define SIE 0x10 /* Set Interrupt Enable bits */
28*4882a593Smuzhiyun #define CIE 0x14 /* Clear Interrupt Enable bits */
29*4882a593Smuzhiyun #define IVR 0x18 /* Interrupt Vector Register */
30*4882a593Smuzhiyun #define MER 0x1c /* Master Enable Register */
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #define MER_ME (1<<0)
33*4882a593Smuzhiyun #define MER_HIE (1<<1)
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun static DEFINE_STATIC_KEY_FALSE(xintc_is_be);
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun struct xintc_irq_chip {
38*4882a593Smuzhiyun void __iomem *base;
39*4882a593Smuzhiyun struct irq_domain *root_domain;
40*4882a593Smuzhiyun u32 intr_mask;
41*4882a593Smuzhiyun u32 nr_irq;
42*4882a593Smuzhiyun };
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun static struct xintc_irq_chip *primary_intc;
45*4882a593Smuzhiyun
xintc_write(struct xintc_irq_chip * irqc,int reg,u32 data)46*4882a593Smuzhiyun static void xintc_write(struct xintc_irq_chip *irqc, int reg, u32 data)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun if (static_branch_unlikely(&xintc_is_be))
49*4882a593Smuzhiyun iowrite32be(data, irqc->base + reg);
50*4882a593Smuzhiyun else
51*4882a593Smuzhiyun iowrite32(data, irqc->base + reg);
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun
xintc_read(struct xintc_irq_chip * irqc,int reg)54*4882a593Smuzhiyun static u32 xintc_read(struct xintc_irq_chip *irqc, int reg)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun if (static_branch_unlikely(&xintc_is_be))
57*4882a593Smuzhiyun return ioread32be(irqc->base + reg);
58*4882a593Smuzhiyun else
59*4882a593Smuzhiyun return ioread32(irqc->base + reg);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
intc_enable_or_unmask(struct irq_data * d)62*4882a593Smuzhiyun static void intc_enable_or_unmask(struct irq_data *d)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun struct xintc_irq_chip *irqc = irq_data_get_irq_chip_data(d);
65*4882a593Smuzhiyun unsigned long mask = BIT(d->hwirq);
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun pr_debug("irq-xilinx: enable_or_unmask: %ld\n", d->hwirq);
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /* ack level irqs because they can't be acked during
70*4882a593Smuzhiyun * ack function since the handle_level_irq function
71*4882a593Smuzhiyun * acks the irq before calling the interrupt handler
72*4882a593Smuzhiyun */
73*4882a593Smuzhiyun if (irqd_is_level_type(d))
74*4882a593Smuzhiyun xintc_write(irqc, IAR, mask);
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun xintc_write(irqc, SIE, mask);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
intc_disable_or_mask(struct irq_data * d)79*4882a593Smuzhiyun static void intc_disable_or_mask(struct irq_data *d)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun struct xintc_irq_chip *irqc = irq_data_get_irq_chip_data(d);
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun pr_debug("irq-xilinx: disable: %ld\n", d->hwirq);
84*4882a593Smuzhiyun xintc_write(irqc, CIE, BIT(d->hwirq));
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
intc_ack(struct irq_data * d)87*4882a593Smuzhiyun static void intc_ack(struct irq_data *d)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun struct xintc_irq_chip *irqc = irq_data_get_irq_chip_data(d);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun pr_debug("irq-xilinx: ack: %ld\n", d->hwirq);
92*4882a593Smuzhiyun xintc_write(irqc, IAR, BIT(d->hwirq));
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
intc_mask_ack(struct irq_data * d)95*4882a593Smuzhiyun static void intc_mask_ack(struct irq_data *d)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun struct xintc_irq_chip *irqc = irq_data_get_irq_chip_data(d);
98*4882a593Smuzhiyun unsigned long mask = BIT(d->hwirq);
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun pr_debug("irq-xilinx: disable_and_ack: %ld\n", d->hwirq);
101*4882a593Smuzhiyun xintc_write(irqc, CIE, mask);
102*4882a593Smuzhiyun xintc_write(irqc, IAR, mask);
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun static struct irq_chip intc_dev = {
106*4882a593Smuzhiyun .name = "Xilinx INTC",
107*4882a593Smuzhiyun .irq_unmask = intc_enable_or_unmask,
108*4882a593Smuzhiyun .irq_mask = intc_disable_or_mask,
109*4882a593Smuzhiyun .irq_ack = intc_ack,
110*4882a593Smuzhiyun .irq_mask_ack = intc_mask_ack,
111*4882a593Smuzhiyun };
112*4882a593Smuzhiyun
xintc_get_irq_local(struct xintc_irq_chip * irqc)113*4882a593Smuzhiyun static unsigned int xintc_get_irq_local(struct xintc_irq_chip *irqc)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun unsigned int irq = 0;
116*4882a593Smuzhiyun u32 hwirq;
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun hwirq = xintc_read(irqc, IVR);
119*4882a593Smuzhiyun if (hwirq != -1U)
120*4882a593Smuzhiyun irq = irq_find_mapping(irqc->root_domain, hwirq);
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun pr_debug("irq-xilinx: hwirq=%d, irq=%d\n", hwirq, irq);
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun return irq;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
xintc_get_irq(void)127*4882a593Smuzhiyun unsigned int xintc_get_irq(void)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun unsigned int irq = -1;
130*4882a593Smuzhiyun u32 hwirq;
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun hwirq = xintc_read(primary_intc, IVR);
133*4882a593Smuzhiyun if (hwirq != -1U)
134*4882a593Smuzhiyun irq = irq_find_mapping(primary_intc->root_domain, hwirq);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun pr_debug("irq-xilinx: hwirq=%d, irq=%d\n", hwirq, irq);
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun return irq;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
xintc_map(struct irq_domain * d,unsigned int irq,irq_hw_number_t hw)141*4882a593Smuzhiyun static int xintc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun struct xintc_irq_chip *irqc = d->host_data;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun if (irqc->intr_mask & BIT(hw)) {
146*4882a593Smuzhiyun irq_set_chip_and_handler_name(irq, &intc_dev,
147*4882a593Smuzhiyun handle_edge_irq, "edge");
148*4882a593Smuzhiyun irq_clear_status_flags(irq, IRQ_LEVEL);
149*4882a593Smuzhiyun } else {
150*4882a593Smuzhiyun irq_set_chip_and_handler_name(irq, &intc_dev,
151*4882a593Smuzhiyun handle_level_irq, "level");
152*4882a593Smuzhiyun irq_set_status_flags(irq, IRQ_LEVEL);
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun irq_set_chip_data(irq, irqc);
155*4882a593Smuzhiyun return 0;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun static const struct irq_domain_ops xintc_irq_domain_ops = {
159*4882a593Smuzhiyun .xlate = irq_domain_xlate_onetwocell,
160*4882a593Smuzhiyun .map = xintc_map,
161*4882a593Smuzhiyun };
162*4882a593Smuzhiyun
xil_intc_irq_handler(struct irq_desc * desc)163*4882a593Smuzhiyun static void xil_intc_irq_handler(struct irq_desc *desc)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun struct irq_chip *chip = irq_desc_get_chip(desc);
166*4882a593Smuzhiyun struct xintc_irq_chip *irqc;
167*4882a593Smuzhiyun u32 pending;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun irqc = irq_data_get_irq_handler_data(&desc->irq_data);
170*4882a593Smuzhiyun chained_irq_enter(chip, desc);
171*4882a593Smuzhiyun do {
172*4882a593Smuzhiyun pending = xintc_get_irq_local(irqc);
173*4882a593Smuzhiyun if (pending == 0)
174*4882a593Smuzhiyun break;
175*4882a593Smuzhiyun generic_handle_irq(pending);
176*4882a593Smuzhiyun } while (true);
177*4882a593Smuzhiyun chained_irq_exit(chip, desc);
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun
xilinx_intc_of_init(struct device_node * intc,struct device_node * parent)180*4882a593Smuzhiyun static int __init xilinx_intc_of_init(struct device_node *intc,
181*4882a593Smuzhiyun struct device_node *parent)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun struct xintc_irq_chip *irqc;
184*4882a593Smuzhiyun int ret, irq;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun irqc = kzalloc(sizeof(*irqc), GFP_KERNEL);
187*4882a593Smuzhiyun if (!irqc)
188*4882a593Smuzhiyun return -ENOMEM;
189*4882a593Smuzhiyun irqc->base = of_iomap(intc, 0);
190*4882a593Smuzhiyun BUG_ON(!irqc->base);
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun ret = of_property_read_u32(intc, "xlnx,num-intr-inputs", &irqc->nr_irq);
193*4882a593Smuzhiyun if (ret < 0) {
194*4882a593Smuzhiyun pr_err("irq-xilinx: unable to read xlnx,num-intr-inputs\n");
195*4882a593Smuzhiyun goto error;
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun ret = of_property_read_u32(intc, "xlnx,kind-of-intr", &irqc->intr_mask);
199*4882a593Smuzhiyun if (ret < 0) {
200*4882a593Smuzhiyun pr_warn("irq-xilinx: unable to read xlnx,kind-of-intr\n");
201*4882a593Smuzhiyun irqc->intr_mask = 0;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun if (irqc->intr_mask >> irqc->nr_irq)
205*4882a593Smuzhiyun pr_warn("irq-xilinx: mismatch in kind-of-intr param\n");
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun pr_info("irq-xilinx: %pOF: num_irq=%d, edge=0x%x\n",
208*4882a593Smuzhiyun intc, irqc->nr_irq, irqc->intr_mask);
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun /*
212*4882a593Smuzhiyun * Disable all external interrupts until they are
213*4882a593Smuzhiyun * explicity requested.
214*4882a593Smuzhiyun */
215*4882a593Smuzhiyun xintc_write(irqc, IER, 0);
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun /* Acknowledge any pending interrupts just in case. */
218*4882a593Smuzhiyun xintc_write(irqc, IAR, 0xffffffff);
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /* Turn on the Master Enable. */
221*4882a593Smuzhiyun xintc_write(irqc, MER, MER_HIE | MER_ME);
222*4882a593Smuzhiyun if (xintc_read(irqc, MER) != (MER_HIE | MER_ME)) {
223*4882a593Smuzhiyun static_branch_enable(&xintc_is_be);
224*4882a593Smuzhiyun xintc_write(irqc, MER, MER_HIE | MER_ME);
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun irqc->root_domain = irq_domain_add_linear(intc, irqc->nr_irq,
228*4882a593Smuzhiyun &xintc_irq_domain_ops, irqc);
229*4882a593Smuzhiyun if (!irqc->root_domain) {
230*4882a593Smuzhiyun pr_err("irq-xilinx: Unable to create IRQ domain\n");
231*4882a593Smuzhiyun ret = -EINVAL;
232*4882a593Smuzhiyun goto error;
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun if (parent) {
236*4882a593Smuzhiyun irq = irq_of_parse_and_map(intc, 0);
237*4882a593Smuzhiyun if (irq) {
238*4882a593Smuzhiyun irq_set_chained_handler_and_data(irq,
239*4882a593Smuzhiyun xil_intc_irq_handler,
240*4882a593Smuzhiyun irqc);
241*4882a593Smuzhiyun } else {
242*4882a593Smuzhiyun pr_err("irq-xilinx: interrupts property not in DT\n");
243*4882a593Smuzhiyun ret = -EINVAL;
244*4882a593Smuzhiyun goto error;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun } else {
247*4882a593Smuzhiyun primary_intc = irqc;
248*4882a593Smuzhiyun irq_set_default_host(primary_intc->root_domain);
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun return 0;
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun error:
254*4882a593Smuzhiyun iounmap(irqc->base);
255*4882a593Smuzhiyun kfree(irqc);
256*4882a593Smuzhiyun return ret;
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun IRQCHIP_DECLARE(xilinx_intc_xps, "xlnx,xps-intc-1.00.a", xilinx_intc_of_init);
261*4882a593Smuzhiyun IRQCHIP_DECLARE(xilinx_intc_opb, "xlnx,opb-intc-1.00.c", xilinx_intc_of_init);
262