xref: /linux/drivers/irqchip/irq-xilinx-intc.c (revision a5734de263e70c75e61778be038add78441610e3)
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>
150547dc78SZubair Lutfullah Kakakhel #include <linux/of_address.h>
160547dc78SZubair Lutfullah Kakakhel #include <linux/io.h>
170547dc78SZubair Lutfullah Kakakhel #include <linux/bug.h>
180547dc78SZubair Lutfullah Kakakhel 
190547dc78SZubair Lutfullah Kakakhel static void __iomem *intc_baseaddr;
200547dc78SZubair Lutfullah Kakakhel 
210547dc78SZubair Lutfullah Kakakhel /* No one else should require these constants, so define them locally here. */
220547dc78SZubair Lutfullah Kakakhel #define ISR 0x00			/* Interrupt Status Register */
230547dc78SZubair Lutfullah Kakakhel #define IPR 0x04			/* Interrupt Pending Register */
240547dc78SZubair Lutfullah Kakakhel #define IER 0x08			/* Interrupt Enable Register */
250547dc78SZubair Lutfullah Kakakhel #define IAR 0x0c			/* Interrupt Acknowledge Register */
260547dc78SZubair Lutfullah Kakakhel #define SIE 0x10			/* Set Interrupt Enable bits */
270547dc78SZubair Lutfullah Kakakhel #define CIE 0x14			/* Clear Interrupt Enable bits */
280547dc78SZubair Lutfullah Kakakhel #define IVR 0x18			/* Interrupt Vector Register */
290547dc78SZubair Lutfullah Kakakhel #define MER 0x1c			/* Master Enable Register */
300547dc78SZubair Lutfullah Kakakhel 
310547dc78SZubair Lutfullah Kakakhel #define MER_ME (1<<0)
320547dc78SZubair Lutfullah Kakakhel #define MER_HIE (1<<1)
330547dc78SZubair Lutfullah Kakakhel 
340547dc78SZubair Lutfullah Kakakhel static unsigned int (*read_fn)(void __iomem *);
350547dc78SZubair Lutfullah Kakakhel static void (*write_fn)(u32, void __iomem *);
360547dc78SZubair Lutfullah Kakakhel 
370547dc78SZubair Lutfullah Kakakhel static void intc_write32(u32 val, void __iomem *addr)
380547dc78SZubair Lutfullah Kakakhel {
390547dc78SZubair Lutfullah Kakakhel 	iowrite32(val, addr);
400547dc78SZubair Lutfullah Kakakhel }
410547dc78SZubair Lutfullah Kakakhel 
420547dc78SZubair Lutfullah Kakakhel static unsigned int intc_read32(void __iomem *addr)
430547dc78SZubair Lutfullah Kakakhel {
440547dc78SZubair Lutfullah Kakakhel 	return ioread32(addr);
450547dc78SZubair Lutfullah Kakakhel }
460547dc78SZubair Lutfullah Kakakhel 
470547dc78SZubair Lutfullah Kakakhel static void intc_write32_be(u32 val, void __iomem *addr)
480547dc78SZubair Lutfullah Kakakhel {
490547dc78SZubair Lutfullah Kakakhel 	iowrite32be(val, addr);
500547dc78SZubair Lutfullah Kakakhel }
510547dc78SZubair Lutfullah Kakakhel 
520547dc78SZubair Lutfullah Kakakhel static unsigned int intc_read32_be(void __iomem *addr)
530547dc78SZubair Lutfullah Kakakhel {
540547dc78SZubair Lutfullah Kakakhel 	return ioread32be(addr);
550547dc78SZubair Lutfullah Kakakhel }
560547dc78SZubair Lutfullah Kakakhel 
570547dc78SZubair Lutfullah Kakakhel static void intc_enable_or_unmask(struct irq_data *d)
580547dc78SZubair Lutfullah Kakakhel {
590547dc78SZubair Lutfullah Kakakhel 	unsigned long mask = 1 << d->hwirq;
600547dc78SZubair Lutfullah Kakakhel 
61*a5734de2SZubair Lutfullah Kakakhel 	pr_debug("irq-xilinx: enable_or_unmask: %ld\n", d->hwirq);
620547dc78SZubair Lutfullah Kakakhel 
630547dc78SZubair Lutfullah Kakakhel 	/* ack level irqs because they can't be acked during
640547dc78SZubair Lutfullah Kakakhel 	 * ack function since the handle_level_irq function
650547dc78SZubair Lutfullah Kakakhel 	 * acks the irq before calling the interrupt handler
660547dc78SZubair Lutfullah Kakakhel 	 */
670547dc78SZubair Lutfullah Kakakhel 	if (irqd_is_level_type(d))
680547dc78SZubair Lutfullah Kakakhel 		write_fn(mask, intc_baseaddr + IAR);
690547dc78SZubair Lutfullah Kakakhel 
700547dc78SZubair Lutfullah Kakakhel 	write_fn(mask, intc_baseaddr + SIE);
710547dc78SZubair Lutfullah Kakakhel }
720547dc78SZubair Lutfullah Kakakhel 
730547dc78SZubair Lutfullah Kakakhel static void intc_disable_or_mask(struct irq_data *d)
740547dc78SZubair Lutfullah Kakakhel {
75*a5734de2SZubair Lutfullah Kakakhel 	pr_debug("irq-xilinx: disable: %ld\n", d->hwirq);
760547dc78SZubair Lutfullah Kakakhel 	write_fn(1 << d->hwirq, intc_baseaddr + CIE);
770547dc78SZubair Lutfullah Kakakhel }
780547dc78SZubair Lutfullah Kakakhel 
790547dc78SZubair Lutfullah Kakakhel static void intc_ack(struct irq_data *d)
800547dc78SZubair Lutfullah Kakakhel {
81*a5734de2SZubair Lutfullah Kakakhel 	pr_debug("irq-xilinx: ack: %ld\n", d->hwirq);
820547dc78SZubair Lutfullah Kakakhel 	write_fn(1 << d->hwirq, intc_baseaddr + IAR);
830547dc78SZubair Lutfullah Kakakhel }
840547dc78SZubair Lutfullah Kakakhel 
850547dc78SZubair Lutfullah Kakakhel static void intc_mask_ack(struct irq_data *d)
860547dc78SZubair Lutfullah Kakakhel {
870547dc78SZubair Lutfullah Kakakhel 	unsigned long mask = 1 << d->hwirq;
880547dc78SZubair Lutfullah Kakakhel 
89*a5734de2SZubair Lutfullah Kakakhel 	pr_debug("irq-xilinx: disable_and_ack: %ld\n", d->hwirq);
900547dc78SZubair Lutfullah Kakakhel 	write_fn(mask, intc_baseaddr + CIE);
910547dc78SZubair Lutfullah Kakakhel 	write_fn(mask, intc_baseaddr + IAR);
920547dc78SZubair Lutfullah Kakakhel }
930547dc78SZubair Lutfullah Kakakhel 
940547dc78SZubair Lutfullah Kakakhel static struct irq_chip intc_dev = {
950547dc78SZubair Lutfullah Kakakhel 	.name = "Xilinx INTC",
960547dc78SZubair Lutfullah Kakakhel 	.irq_unmask = intc_enable_or_unmask,
970547dc78SZubair Lutfullah Kakakhel 	.irq_mask = intc_disable_or_mask,
980547dc78SZubair Lutfullah Kakakhel 	.irq_ack = intc_ack,
990547dc78SZubair Lutfullah Kakakhel 	.irq_mask_ack = intc_mask_ack,
1000547dc78SZubair Lutfullah Kakakhel };
1010547dc78SZubair Lutfullah Kakakhel 
1020547dc78SZubair Lutfullah Kakakhel static struct irq_domain *root_domain;
1030547dc78SZubair Lutfullah Kakakhel 
1040547dc78SZubair Lutfullah Kakakhel unsigned int get_irq(void)
1050547dc78SZubair Lutfullah Kakakhel {
1060547dc78SZubair Lutfullah Kakakhel 	unsigned int hwirq, irq = -1;
1070547dc78SZubair Lutfullah Kakakhel 
1080547dc78SZubair Lutfullah Kakakhel 	hwirq = read_fn(intc_baseaddr + IVR);
1090547dc78SZubair Lutfullah Kakakhel 	if (hwirq != -1U)
1100547dc78SZubair Lutfullah Kakakhel 		irq = irq_find_mapping(root_domain, hwirq);
1110547dc78SZubair Lutfullah Kakakhel 
112*a5734de2SZubair Lutfullah Kakakhel 	pr_debug("irq-xilinx: hwirq=%d, irq=%d\n", hwirq, irq);
1130547dc78SZubair Lutfullah Kakakhel 
1140547dc78SZubair Lutfullah Kakakhel 	return irq;
1150547dc78SZubair Lutfullah Kakakhel }
1160547dc78SZubair Lutfullah Kakakhel 
1170547dc78SZubair Lutfullah Kakakhel static int xintc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
1180547dc78SZubair Lutfullah Kakakhel {
1190547dc78SZubair Lutfullah Kakakhel 	u32 intr_mask = (u32)d->host_data;
1200547dc78SZubair Lutfullah Kakakhel 
1210547dc78SZubair Lutfullah Kakakhel 	if (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 
1380547dc78SZubair Lutfullah Kakakhel static int __init xilinx_intc_of_init(struct device_node *intc,
1390547dc78SZubair Lutfullah Kakakhel 					     struct device_node *parent)
1400547dc78SZubair Lutfullah Kakakhel {
1410547dc78SZubair Lutfullah Kakakhel 	u32 nr_irq, intr_mask;
1420547dc78SZubair Lutfullah Kakakhel 	int ret;
1430547dc78SZubair Lutfullah Kakakhel 
1440547dc78SZubair Lutfullah Kakakhel 	intc_baseaddr = of_iomap(intc, 0);
1450547dc78SZubair Lutfullah Kakakhel 	BUG_ON(!intc_baseaddr);
1460547dc78SZubair Lutfullah Kakakhel 
1470547dc78SZubair Lutfullah Kakakhel 	ret = of_property_read_u32(intc, "xlnx,num-intr-inputs", &nr_irq);
1480547dc78SZubair Lutfullah Kakakhel 	if (ret < 0) {
149*a5734de2SZubair Lutfullah Kakakhel 		pr_err("irq-xilinx: unable to read xlnx,num-intr-inputs\n");
1500547dc78SZubair Lutfullah Kakakhel 		return ret;
1510547dc78SZubair Lutfullah Kakakhel 	}
1520547dc78SZubair Lutfullah Kakakhel 
1530547dc78SZubair Lutfullah Kakakhel 	ret = of_property_read_u32(intc, "xlnx,kind-of-intr", &intr_mask);
1540547dc78SZubair Lutfullah Kakakhel 	if (ret < 0) {
155*a5734de2SZubair Lutfullah Kakakhel 		pr_err("irq-xilinx: unable to read xlnx,kind-of-intr\n");
1560547dc78SZubair Lutfullah Kakakhel 		return ret;
1570547dc78SZubair Lutfullah Kakakhel 	}
1580547dc78SZubair Lutfullah Kakakhel 
1590547dc78SZubair Lutfullah Kakakhel 	if (intr_mask >> nr_irq)
160*a5734de2SZubair Lutfullah Kakakhel 		pr_warn("irq-xilinx: mismatch in kind-of-intr param\n");
1610547dc78SZubair Lutfullah Kakakhel 
162*a5734de2SZubair Lutfullah Kakakhel 	pr_info("irq-xilinx: %s: num_irq=%d, edge=0x%x\n",
1630547dc78SZubair Lutfullah Kakakhel 		intc->full_name, nr_irq, intr_mask);
1640547dc78SZubair Lutfullah Kakakhel 
1650547dc78SZubair Lutfullah Kakakhel 	write_fn = intc_write32;
1660547dc78SZubair Lutfullah Kakakhel 	read_fn = intc_read32;
1670547dc78SZubair Lutfullah Kakakhel 
1680547dc78SZubair Lutfullah Kakakhel 	/*
1690547dc78SZubair Lutfullah Kakakhel 	 * Disable all external interrupts until they are
1700547dc78SZubair Lutfullah Kakakhel 	 * explicity requested.
1710547dc78SZubair Lutfullah Kakakhel 	 */
1720547dc78SZubair Lutfullah Kakakhel 	write_fn(0, intc_baseaddr + IER);
1730547dc78SZubair Lutfullah Kakakhel 
1740547dc78SZubair Lutfullah Kakakhel 	/* Acknowledge any pending interrupts just in case. */
1750547dc78SZubair Lutfullah Kakakhel 	write_fn(0xffffffff, intc_baseaddr + IAR);
1760547dc78SZubair Lutfullah Kakakhel 
1770547dc78SZubair Lutfullah Kakakhel 	/* Turn on the Master Enable. */
1780547dc78SZubair Lutfullah Kakakhel 	write_fn(MER_HIE | MER_ME, intc_baseaddr + MER);
1790547dc78SZubair Lutfullah Kakakhel 	if (!(read_fn(intc_baseaddr + MER) & (MER_HIE | MER_ME))) {
1800547dc78SZubair Lutfullah Kakakhel 		write_fn = intc_write32_be;
1810547dc78SZubair Lutfullah Kakakhel 		read_fn = intc_read32_be;
1820547dc78SZubair Lutfullah Kakakhel 		write_fn(MER_HIE | MER_ME, intc_baseaddr + MER);
1830547dc78SZubair Lutfullah Kakakhel 	}
1840547dc78SZubair Lutfullah Kakakhel 
1850547dc78SZubair Lutfullah Kakakhel 	/* Yeah, okay, casting the intr_mask to a void* is butt-ugly, but I'm
1860547dc78SZubair Lutfullah Kakakhel 	 * lazy and Michal can clean it up to something nicer when he tests
1870547dc78SZubair Lutfullah Kakakhel 	 * and commits this patch.  ~~gcl */
1880547dc78SZubair Lutfullah Kakakhel 	root_domain = irq_domain_add_linear(intc, nr_irq, &xintc_irq_domain_ops,
1890547dc78SZubair Lutfullah Kakakhel 							(void *)intr_mask);
1900547dc78SZubair Lutfullah Kakakhel 
1910547dc78SZubair Lutfullah Kakakhel 	irq_set_default_host(root_domain);
1920547dc78SZubair Lutfullah Kakakhel 
1930547dc78SZubair Lutfullah Kakakhel 	return 0;
1940547dc78SZubair Lutfullah Kakakhel }
1950547dc78SZubair Lutfullah Kakakhel 
1960547dc78SZubair Lutfullah Kakakhel IRQCHIP_DECLARE(xilinx_intc, "xlnx,xps-intc-1.00.a", xilinx_intc_of_init);
197