xref: /linux/drivers/irqchip/irq-xilinx-intc.c (revision 8a11da598e2ff96050a99e24772e1568e3e31c4d)
10547dc78SZubair Lutfullah Kakakhel /*
20547dc78SZubair Lutfullah Kakakhel  * Copyright (C) 2007-2013 Michal Simek <monstr@monstr.eu>
30547dc78SZubair Lutfullah Kakakhel  * Copyright (C) 2012-2013 Xilinx, Inc.
40547dc78SZubair Lutfullah Kakakhel  * Copyright (C) 2007-2009 PetaLogix
50547dc78SZubair Lutfullah Kakakhel  * Copyright (C) 2006 Atmark Techno, Inc.
60547dc78SZubair Lutfullah Kakakhel  *
70547dc78SZubair Lutfullah Kakakhel  * This file is subject to the terms and conditions of the GNU General Public
80547dc78SZubair Lutfullah Kakakhel  * License. See the file "COPYING" in the main directory of this archive
90547dc78SZubair Lutfullah Kakakhel  * for more details.
100547dc78SZubair Lutfullah Kakakhel  */
110547dc78SZubair Lutfullah Kakakhel 
120547dc78SZubair Lutfullah Kakakhel #include <linux/irqdomain.h>
130547dc78SZubair Lutfullah Kakakhel #include <linux/irq.h>
140547dc78SZubair Lutfullah Kakakhel #include <linux/irqchip.h>
159689c99eSZubair Lutfullah Kakakhel #include <linux/irqchip/chained_irq.h>
160547dc78SZubair Lutfullah Kakakhel #include <linux/of_address.h>
170547dc78SZubair Lutfullah Kakakhel #include <linux/io.h>
18591db74bSZubair Lutfullah Kakakhel #include <linux/jump_label.h>
190547dc78SZubair Lutfullah Kakakhel #include <linux/bug.h>
209689c99eSZubair Lutfullah Kakakhel #include <linux/of_irq.h>
210547dc78SZubair Lutfullah Kakakhel 
220547dc78SZubair Lutfullah Kakakhel /* No one else should require these constants, so define them locally here. */
230547dc78SZubair Lutfullah Kakakhel #define ISR 0x00			/* Interrupt Status Register */
240547dc78SZubair Lutfullah Kakakhel #define IPR 0x04			/* Interrupt Pending Register */
250547dc78SZubair Lutfullah Kakakhel #define IER 0x08			/* Interrupt Enable Register */
260547dc78SZubair Lutfullah Kakakhel #define IAR 0x0c			/* Interrupt Acknowledge Register */
270547dc78SZubair Lutfullah Kakakhel #define SIE 0x10			/* Set Interrupt Enable bits */
280547dc78SZubair Lutfullah Kakakhel #define CIE 0x14			/* Clear Interrupt Enable bits */
290547dc78SZubair Lutfullah Kakakhel #define IVR 0x18			/* Interrupt Vector Register */
300547dc78SZubair Lutfullah Kakakhel #define MER 0x1c			/* Master Enable Register */
310547dc78SZubair Lutfullah Kakakhel 
320547dc78SZubair Lutfullah Kakakhel #define MER_ME (1<<0)
330547dc78SZubair Lutfullah Kakakhel #define MER_HIE (1<<1)
340547dc78SZubair Lutfullah Kakakhel 
35591db74bSZubair Lutfullah Kakakhel static DEFINE_STATIC_KEY_FALSE(xintc_is_be);
360547dc78SZubair Lutfullah Kakakhel 
37591db74bSZubair Lutfullah Kakakhel struct xintc_irq_chip {
38591db74bSZubair Lutfullah Kakakhel 	void		__iomem *base;
39591db74bSZubair Lutfullah Kakakhel 	struct		irq_domain *root_domain;
40591db74bSZubair Lutfullah Kakakhel 	u32		intr_mask;
41591db74bSZubair Lutfullah Kakakhel };
42591db74bSZubair Lutfullah Kakakhel 
43591db74bSZubair Lutfullah Kakakhel static struct xintc_irq_chip *xintc_irqc;
44591db74bSZubair Lutfullah Kakakhel 
45591db74bSZubair Lutfullah Kakakhel static void xintc_write(int reg, u32 data)
460547dc78SZubair Lutfullah Kakakhel {
47591db74bSZubair Lutfullah Kakakhel 	if (static_branch_unlikely(&xintc_is_be))
48591db74bSZubair Lutfullah Kakakhel 		iowrite32be(data, xintc_irqc->base + reg);
49591db74bSZubair Lutfullah Kakakhel 	else
50591db74bSZubair Lutfullah Kakakhel 		iowrite32(data, xintc_irqc->base + reg);
510547dc78SZubair Lutfullah Kakakhel }
520547dc78SZubair Lutfullah Kakakhel 
53591db74bSZubair Lutfullah Kakakhel static unsigned int xintc_read(int reg)
540547dc78SZubair Lutfullah Kakakhel {
55591db74bSZubair Lutfullah Kakakhel 	if (static_branch_unlikely(&xintc_is_be))
56591db74bSZubair Lutfullah Kakakhel 		return ioread32be(xintc_irqc->base + reg);
57591db74bSZubair Lutfullah Kakakhel 	else
58591db74bSZubair Lutfullah Kakakhel 		return ioread32(xintc_irqc->base + reg);
590547dc78SZubair Lutfullah Kakakhel }
600547dc78SZubair Lutfullah Kakakhel 
610547dc78SZubair Lutfullah Kakakhel static void intc_enable_or_unmask(struct irq_data *d)
620547dc78SZubair Lutfullah Kakakhel {
630547dc78SZubair Lutfullah Kakakhel 	unsigned long mask = 1 << d->hwirq;
640547dc78SZubair Lutfullah Kakakhel 
65a5734de2SZubair Lutfullah Kakakhel 	pr_debug("irq-xilinx: enable_or_unmask: %ld\n", d->hwirq);
660547dc78SZubair Lutfullah Kakakhel 
670547dc78SZubair Lutfullah Kakakhel 	/* ack level irqs because they can't be acked during
680547dc78SZubair Lutfullah Kakakhel 	 * ack function since the handle_level_irq function
690547dc78SZubair Lutfullah Kakakhel 	 * acks the irq before calling the interrupt handler
700547dc78SZubair Lutfullah Kakakhel 	 */
710547dc78SZubair Lutfullah Kakakhel 	if (irqd_is_level_type(d))
72591db74bSZubair Lutfullah Kakakhel 		xintc_write(IAR, mask);
730547dc78SZubair Lutfullah Kakakhel 
74591db74bSZubair Lutfullah Kakakhel 	xintc_write(SIE, mask);
750547dc78SZubair Lutfullah Kakakhel }
760547dc78SZubair Lutfullah Kakakhel 
770547dc78SZubair Lutfullah Kakakhel static void intc_disable_or_mask(struct irq_data *d)
780547dc78SZubair Lutfullah Kakakhel {
79a5734de2SZubair Lutfullah Kakakhel 	pr_debug("irq-xilinx: disable: %ld\n", d->hwirq);
80591db74bSZubair Lutfullah Kakakhel 	xintc_write(CIE, 1 << d->hwirq);
810547dc78SZubair Lutfullah Kakakhel }
820547dc78SZubair Lutfullah Kakakhel 
830547dc78SZubair Lutfullah Kakakhel static void intc_ack(struct irq_data *d)
840547dc78SZubair Lutfullah Kakakhel {
85a5734de2SZubair Lutfullah Kakakhel 	pr_debug("irq-xilinx: ack: %ld\n", d->hwirq);
86591db74bSZubair Lutfullah Kakakhel 	xintc_write(IAR, 1 << d->hwirq);
870547dc78SZubair Lutfullah Kakakhel }
880547dc78SZubair Lutfullah Kakakhel 
890547dc78SZubair Lutfullah Kakakhel static void intc_mask_ack(struct irq_data *d)
900547dc78SZubair Lutfullah Kakakhel {
910547dc78SZubair Lutfullah Kakakhel 	unsigned long mask = 1 << d->hwirq;
920547dc78SZubair Lutfullah Kakakhel 
93a5734de2SZubair Lutfullah Kakakhel 	pr_debug("irq-xilinx: disable_and_ack: %ld\n", d->hwirq);
94591db74bSZubair Lutfullah Kakakhel 	xintc_write(CIE, mask);
95591db74bSZubair Lutfullah Kakakhel 	xintc_write(IAR, mask);
960547dc78SZubair Lutfullah Kakakhel }
970547dc78SZubair Lutfullah Kakakhel 
980547dc78SZubair Lutfullah Kakakhel static struct irq_chip intc_dev = {
990547dc78SZubair Lutfullah Kakakhel 	.name = "Xilinx INTC",
1000547dc78SZubair Lutfullah Kakakhel 	.irq_unmask = intc_enable_or_unmask,
1010547dc78SZubair Lutfullah Kakakhel 	.irq_mask = intc_disable_or_mask,
1020547dc78SZubair Lutfullah Kakakhel 	.irq_ack = intc_ack,
1030547dc78SZubair Lutfullah Kakakhel 	.irq_mask_ack = intc_mask_ack,
1040547dc78SZubair Lutfullah Kakakhel };
1050547dc78SZubair Lutfullah Kakakhel 
1062120a435SZubair Lutfullah Kakakhel unsigned int xintc_get_irq(void)
1070547dc78SZubair Lutfullah Kakakhel {
1080547dc78SZubair Lutfullah Kakakhel 	unsigned int hwirq, irq = -1;
1090547dc78SZubair Lutfullah Kakakhel 
110591db74bSZubair Lutfullah Kakakhel 	hwirq = xintc_read(IVR);
1110547dc78SZubair Lutfullah Kakakhel 	if (hwirq != -1U)
112591db74bSZubair Lutfullah Kakakhel 		irq = irq_find_mapping(xintc_irqc->root_domain, hwirq);
1130547dc78SZubair Lutfullah Kakakhel 
114a5734de2SZubair Lutfullah Kakakhel 	pr_debug("irq-xilinx: hwirq=%d, irq=%d\n", hwirq, irq);
1150547dc78SZubair Lutfullah Kakakhel 
1160547dc78SZubair Lutfullah Kakakhel 	return irq;
1170547dc78SZubair Lutfullah Kakakhel }
1180547dc78SZubair Lutfullah Kakakhel 
1190547dc78SZubair Lutfullah Kakakhel static int xintc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
1200547dc78SZubair Lutfullah Kakakhel {
121591db74bSZubair Lutfullah Kakakhel 	if (xintc_irqc->intr_mask & (1 << hw)) {
1220547dc78SZubair Lutfullah Kakakhel 		irq_set_chip_and_handler_name(irq, &intc_dev,
1230547dc78SZubair Lutfullah Kakakhel 						handle_edge_irq, "edge");
1240547dc78SZubair Lutfullah Kakakhel 		irq_clear_status_flags(irq, IRQ_LEVEL);
1250547dc78SZubair Lutfullah Kakakhel 	} else {
1260547dc78SZubair Lutfullah Kakakhel 		irq_set_chip_and_handler_name(irq, &intc_dev,
1270547dc78SZubair Lutfullah Kakakhel 						handle_level_irq, "level");
1280547dc78SZubair Lutfullah Kakakhel 		irq_set_status_flags(irq, IRQ_LEVEL);
1290547dc78SZubair Lutfullah Kakakhel 	}
1300547dc78SZubair Lutfullah Kakakhel 	return 0;
1310547dc78SZubair Lutfullah Kakakhel }
1320547dc78SZubair Lutfullah Kakakhel 
1330547dc78SZubair Lutfullah Kakakhel static const struct irq_domain_ops xintc_irq_domain_ops = {
1340547dc78SZubair Lutfullah Kakakhel 	.xlate = irq_domain_xlate_onetwocell,
1350547dc78SZubair Lutfullah Kakakhel 	.map = xintc_map,
1360547dc78SZubair Lutfullah Kakakhel };
1370547dc78SZubair Lutfullah Kakakhel 
1389689c99eSZubair Lutfullah Kakakhel static void xil_intc_irq_handler(struct irq_desc *desc)
1399689c99eSZubair Lutfullah Kakakhel {
1409689c99eSZubair Lutfullah Kakakhel 	struct irq_chip *chip = irq_desc_get_chip(desc);
1419689c99eSZubair Lutfullah Kakakhel 	u32 pending;
1429689c99eSZubair Lutfullah Kakakhel 
1439689c99eSZubair Lutfullah Kakakhel 	chained_irq_enter(chip, desc);
1449689c99eSZubair Lutfullah Kakakhel 	do {
1459689c99eSZubair Lutfullah Kakakhel 		pending = xintc_get_irq();
1469689c99eSZubair Lutfullah Kakakhel 		if (pending == -1U)
1479689c99eSZubair Lutfullah Kakakhel 			break;
1489689c99eSZubair Lutfullah Kakakhel 		generic_handle_irq(pending);
1499689c99eSZubair Lutfullah Kakakhel 	} while (true);
1509689c99eSZubair Lutfullah Kakakhel 	chained_irq_exit(chip, desc);
1519689c99eSZubair Lutfullah Kakakhel }
1529689c99eSZubair Lutfullah Kakakhel 
1530547dc78SZubair Lutfullah Kakakhel static int __init xilinx_intc_of_init(struct device_node *intc,
1540547dc78SZubair Lutfullah Kakakhel 					     struct device_node *parent)
1550547dc78SZubair Lutfullah Kakakhel {
156591db74bSZubair Lutfullah Kakakhel 	u32 nr_irq;
1579689c99eSZubair Lutfullah Kakakhel 	int ret, irq;
158591db74bSZubair Lutfullah Kakakhel 	struct xintc_irq_chip *irqc;
1590547dc78SZubair Lutfullah Kakakhel 
160591db74bSZubair Lutfullah Kakakhel 	if (xintc_irqc) {
161591db74bSZubair Lutfullah Kakakhel 		pr_err("irq-xilinx: Multiple instances aren't supported\n");
162591db74bSZubair Lutfullah Kakakhel 		return -EINVAL;
163591db74bSZubair Lutfullah Kakakhel 	}
164591db74bSZubair Lutfullah Kakakhel 
165591db74bSZubair Lutfullah Kakakhel 	irqc = kzalloc(sizeof(*irqc), GFP_KERNEL);
166591db74bSZubair Lutfullah Kakakhel 	if (!irqc)
167591db74bSZubair Lutfullah Kakakhel 		return -ENOMEM;
168591db74bSZubair Lutfullah Kakakhel 
169591db74bSZubair Lutfullah Kakakhel 	xintc_irqc = irqc;
170591db74bSZubair Lutfullah Kakakhel 
171591db74bSZubair Lutfullah Kakakhel 	irqc->base = of_iomap(intc, 0);
172591db74bSZubair Lutfullah Kakakhel 	BUG_ON(!irqc->base);
1730547dc78SZubair Lutfullah Kakakhel 
1740547dc78SZubair Lutfullah Kakakhel 	ret = of_property_read_u32(intc, "xlnx,num-intr-inputs", &nr_irq);
1750547dc78SZubair Lutfullah Kakakhel 	if (ret < 0) {
176a5734de2SZubair Lutfullah Kakakhel 		pr_err("irq-xilinx: unable to read xlnx,num-intr-inputs\n");
177591db74bSZubair Lutfullah Kakakhel 		goto err_alloc;
1780547dc78SZubair Lutfullah Kakakhel 	}
1790547dc78SZubair Lutfullah Kakakhel 
180591db74bSZubair Lutfullah Kakakhel 	ret = of_property_read_u32(intc, "xlnx,kind-of-intr", &irqc->intr_mask);
1810547dc78SZubair Lutfullah Kakakhel 	if (ret < 0) {
182*8a11da59SZubair Lutfullah Kakakhel 		pr_warn("irq-xilinx: unable to read xlnx,kind-of-intr\n");
183*8a11da59SZubair Lutfullah Kakakhel 		irqc->intr_mask = 0;
1840547dc78SZubair Lutfullah Kakakhel 	}
1850547dc78SZubair Lutfullah Kakakhel 
186591db74bSZubair Lutfullah Kakakhel 	if (irqc->intr_mask >> nr_irq)
187a5734de2SZubair Lutfullah Kakakhel 		pr_warn("irq-xilinx: mismatch in kind-of-intr param\n");
1880547dc78SZubair Lutfullah Kakakhel 
189a5734de2SZubair Lutfullah Kakakhel 	pr_info("irq-xilinx: %s: num_irq=%d, edge=0x%x\n",
190591db74bSZubair Lutfullah Kakakhel 		intc->full_name, nr_irq, irqc->intr_mask);
1910547dc78SZubair Lutfullah Kakakhel 
1920547dc78SZubair Lutfullah Kakakhel 
1930547dc78SZubair Lutfullah Kakakhel 	/*
1940547dc78SZubair Lutfullah Kakakhel 	 * Disable all external interrupts until they are
1950547dc78SZubair Lutfullah Kakakhel 	 * explicity requested.
1960547dc78SZubair Lutfullah Kakakhel 	 */
197591db74bSZubair Lutfullah Kakakhel 	xintc_write(IER, 0);
1980547dc78SZubair Lutfullah Kakakhel 
1990547dc78SZubair Lutfullah Kakakhel 	/* Acknowledge any pending interrupts just in case. */
200591db74bSZubair Lutfullah Kakakhel 	xintc_write(IAR, 0xffffffff);
2010547dc78SZubair Lutfullah Kakakhel 
2020547dc78SZubair Lutfullah Kakakhel 	/* Turn on the Master Enable. */
203591db74bSZubair Lutfullah Kakakhel 	xintc_write(MER, MER_HIE | MER_ME);
204591db74bSZubair Lutfullah Kakakhel 	if (!(xintc_read(MER) & (MER_HIE | MER_ME))) {
205591db74bSZubair Lutfullah Kakakhel 		static_branch_enable(&xintc_is_be);
206591db74bSZubair Lutfullah Kakakhel 		xintc_write(MER, MER_HIE | MER_ME);
2070547dc78SZubair Lutfullah Kakakhel 	}
2080547dc78SZubair Lutfullah Kakakhel 
209591db74bSZubair Lutfullah Kakakhel 	irqc->root_domain = irq_domain_add_linear(intc, nr_irq,
210591db74bSZubair Lutfullah Kakakhel 						  &xintc_irq_domain_ops, irqc);
211591db74bSZubair Lutfullah Kakakhel 	if (!irqc->root_domain) {
212591db74bSZubair Lutfullah Kakakhel 		pr_err("irq-xilinx: Unable to create IRQ domain\n");
213591db74bSZubair Lutfullah Kakakhel 		goto err_alloc;
214591db74bSZubair Lutfullah Kakakhel 	}
2150547dc78SZubair Lutfullah Kakakhel 
2169689c99eSZubair Lutfullah Kakakhel 	if (parent) {
2179689c99eSZubair Lutfullah Kakakhel 		irq = irq_of_parse_and_map(intc, 0);
2189689c99eSZubair Lutfullah Kakakhel 		if (irq) {
2199689c99eSZubair Lutfullah Kakakhel 			irq_set_chained_handler_and_data(irq,
2209689c99eSZubair Lutfullah Kakakhel 							 xil_intc_irq_handler,
2219689c99eSZubair Lutfullah Kakakhel 							 irqc);
2229689c99eSZubair Lutfullah Kakakhel 		} else {
2239689c99eSZubair Lutfullah Kakakhel 			pr_err("irq-xilinx: interrupts property not in DT\n");
2249689c99eSZubair Lutfullah Kakakhel 			ret = -EINVAL;
2259689c99eSZubair Lutfullah Kakakhel 			goto err_alloc;
2269689c99eSZubair Lutfullah Kakakhel 		}
2279689c99eSZubair Lutfullah Kakakhel 	} else {
228591db74bSZubair Lutfullah Kakakhel 		irq_set_default_host(irqc->root_domain);
2299689c99eSZubair Lutfullah Kakakhel 	}
2300547dc78SZubair Lutfullah Kakakhel 
2310547dc78SZubair Lutfullah Kakakhel 	return 0;
232591db74bSZubair Lutfullah Kakakhel 
233591db74bSZubair Lutfullah Kakakhel err_alloc:
234591db74bSZubair Lutfullah Kakakhel 	xintc_irqc = NULL;
235591db74bSZubair Lutfullah Kakakhel 	kfree(irqc);
236591db74bSZubair Lutfullah Kakakhel 	return ret;
237591db74bSZubair Lutfullah Kakakhel 
2380547dc78SZubair Lutfullah Kakakhel }
2390547dc78SZubair Lutfullah Kakakhel 
2400547dc78SZubair Lutfullah Kakakhel IRQCHIP_DECLARE(xilinx_intc, "xlnx,xps-intc-1.00.a", xilinx_intc_of_init);
241