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> 17591db74bSZubair Lutfullah Kakakhel #include <linux/jump_label.h> 180547dc78SZubair Lutfullah Kakakhel #include <linux/bug.h> 190547dc78SZubair Lutfullah Kakakhel 200547dc78SZubair Lutfullah Kakakhel /* No one else should require these constants, so define them locally here. */ 210547dc78SZubair Lutfullah Kakakhel #define ISR 0x00 /* Interrupt Status Register */ 220547dc78SZubair Lutfullah Kakakhel #define IPR 0x04 /* Interrupt Pending Register */ 230547dc78SZubair Lutfullah Kakakhel #define IER 0x08 /* Interrupt Enable Register */ 240547dc78SZubair Lutfullah Kakakhel #define IAR 0x0c /* Interrupt Acknowledge Register */ 250547dc78SZubair Lutfullah Kakakhel #define SIE 0x10 /* Set Interrupt Enable bits */ 260547dc78SZubair Lutfullah Kakakhel #define CIE 0x14 /* Clear Interrupt Enable bits */ 270547dc78SZubair Lutfullah Kakakhel #define IVR 0x18 /* Interrupt Vector Register */ 280547dc78SZubair Lutfullah Kakakhel #define MER 0x1c /* Master Enable Register */ 290547dc78SZubair Lutfullah Kakakhel 300547dc78SZubair Lutfullah Kakakhel #define MER_ME (1<<0) 310547dc78SZubair Lutfullah Kakakhel #define MER_HIE (1<<1) 320547dc78SZubair Lutfullah Kakakhel 33591db74bSZubair Lutfullah Kakakhel static DEFINE_STATIC_KEY_FALSE(xintc_is_be); 340547dc78SZubair Lutfullah Kakakhel 35591db74bSZubair Lutfullah Kakakhel struct xintc_irq_chip { 36591db74bSZubair Lutfullah Kakakhel void __iomem *base; 37591db74bSZubair Lutfullah Kakakhel struct irq_domain *root_domain; 38591db74bSZubair Lutfullah Kakakhel u32 intr_mask; 39591db74bSZubair Lutfullah Kakakhel }; 40591db74bSZubair Lutfullah Kakakhel 41591db74bSZubair Lutfullah Kakakhel static struct xintc_irq_chip *xintc_irqc; 42591db74bSZubair Lutfullah Kakakhel 43591db74bSZubair Lutfullah Kakakhel static void xintc_write(int reg, u32 data) 440547dc78SZubair Lutfullah Kakakhel { 45591db74bSZubair Lutfullah Kakakhel if (static_branch_unlikely(&xintc_is_be)) 46591db74bSZubair Lutfullah Kakakhel iowrite32be(data, xintc_irqc->base + reg); 47591db74bSZubair Lutfullah Kakakhel else 48591db74bSZubair Lutfullah Kakakhel iowrite32(data, xintc_irqc->base + reg); 490547dc78SZubair Lutfullah Kakakhel } 500547dc78SZubair Lutfullah Kakakhel 51591db74bSZubair Lutfullah Kakakhel static unsigned int xintc_read(int reg) 520547dc78SZubair Lutfullah Kakakhel { 53591db74bSZubair Lutfullah Kakakhel if (static_branch_unlikely(&xintc_is_be)) 54591db74bSZubair Lutfullah Kakakhel return ioread32be(xintc_irqc->base + reg); 55591db74bSZubair Lutfullah Kakakhel else 56591db74bSZubair Lutfullah Kakakhel return ioread32(xintc_irqc->base + reg); 570547dc78SZubair Lutfullah Kakakhel } 580547dc78SZubair Lutfullah Kakakhel 590547dc78SZubair Lutfullah Kakakhel static void intc_enable_or_unmask(struct irq_data *d) 600547dc78SZubair Lutfullah Kakakhel { 610547dc78SZubair Lutfullah Kakakhel unsigned long mask = 1 << d->hwirq; 620547dc78SZubair Lutfullah Kakakhel 63a5734de2SZubair Lutfullah Kakakhel pr_debug("irq-xilinx: enable_or_unmask: %ld\n", d->hwirq); 640547dc78SZubair Lutfullah Kakakhel 650547dc78SZubair Lutfullah Kakakhel /* ack level irqs because they can't be acked during 660547dc78SZubair Lutfullah Kakakhel * ack function since the handle_level_irq function 670547dc78SZubair Lutfullah Kakakhel * acks the irq before calling the interrupt handler 680547dc78SZubair Lutfullah Kakakhel */ 690547dc78SZubair Lutfullah Kakakhel if (irqd_is_level_type(d)) 70591db74bSZubair Lutfullah Kakakhel xintc_write(IAR, mask); 710547dc78SZubair Lutfullah Kakakhel 72591db74bSZubair Lutfullah Kakakhel xintc_write(SIE, mask); 730547dc78SZubair Lutfullah Kakakhel } 740547dc78SZubair Lutfullah Kakakhel 750547dc78SZubair Lutfullah Kakakhel static void intc_disable_or_mask(struct irq_data *d) 760547dc78SZubair Lutfullah Kakakhel { 77a5734de2SZubair Lutfullah Kakakhel pr_debug("irq-xilinx: disable: %ld\n", d->hwirq); 78591db74bSZubair Lutfullah Kakakhel xintc_write(CIE, 1 << d->hwirq); 790547dc78SZubair Lutfullah Kakakhel } 800547dc78SZubair Lutfullah Kakakhel 810547dc78SZubair Lutfullah Kakakhel static void intc_ack(struct irq_data *d) 820547dc78SZubair Lutfullah Kakakhel { 83a5734de2SZubair Lutfullah Kakakhel pr_debug("irq-xilinx: ack: %ld\n", d->hwirq); 84591db74bSZubair Lutfullah Kakakhel xintc_write(IAR, 1 << d->hwirq); 850547dc78SZubair Lutfullah Kakakhel } 860547dc78SZubair Lutfullah Kakakhel 870547dc78SZubair Lutfullah Kakakhel static void intc_mask_ack(struct irq_data *d) 880547dc78SZubair Lutfullah Kakakhel { 890547dc78SZubair Lutfullah Kakakhel unsigned long mask = 1 << d->hwirq; 900547dc78SZubair Lutfullah Kakakhel 91a5734de2SZubair Lutfullah Kakakhel pr_debug("irq-xilinx: disable_and_ack: %ld\n", d->hwirq); 92591db74bSZubair Lutfullah Kakakhel xintc_write(CIE, mask); 93591db74bSZubair Lutfullah Kakakhel xintc_write(IAR, mask); 940547dc78SZubair Lutfullah Kakakhel } 950547dc78SZubair Lutfullah Kakakhel 960547dc78SZubair Lutfullah Kakakhel static struct irq_chip intc_dev = { 970547dc78SZubair Lutfullah Kakakhel .name = "Xilinx INTC", 980547dc78SZubair Lutfullah Kakakhel .irq_unmask = intc_enable_or_unmask, 990547dc78SZubair Lutfullah Kakakhel .irq_mask = intc_disable_or_mask, 1000547dc78SZubair Lutfullah Kakakhel .irq_ack = intc_ack, 1010547dc78SZubair Lutfullah Kakakhel .irq_mask_ack = intc_mask_ack, 1020547dc78SZubair Lutfullah Kakakhel }; 1030547dc78SZubair Lutfullah Kakakhel 104*2120a435SZubair Lutfullah Kakakhel unsigned int xintc_get_irq(void) 1050547dc78SZubair Lutfullah Kakakhel { 1060547dc78SZubair Lutfullah Kakakhel unsigned int hwirq, irq = -1; 1070547dc78SZubair Lutfullah Kakakhel 108591db74bSZubair Lutfullah Kakakhel hwirq = xintc_read(IVR); 1090547dc78SZubair Lutfullah Kakakhel if (hwirq != -1U) 110591db74bSZubair Lutfullah Kakakhel irq = irq_find_mapping(xintc_irqc->root_domain, hwirq); 1110547dc78SZubair Lutfullah Kakakhel 112a5734de2SZubair 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 { 119591db74bSZubair Lutfullah Kakakhel if (xintc_irqc->intr_mask & (1 << hw)) { 1200547dc78SZubair Lutfullah Kakakhel irq_set_chip_and_handler_name(irq, &intc_dev, 1210547dc78SZubair Lutfullah Kakakhel handle_edge_irq, "edge"); 1220547dc78SZubair Lutfullah Kakakhel irq_clear_status_flags(irq, IRQ_LEVEL); 1230547dc78SZubair Lutfullah Kakakhel } else { 1240547dc78SZubair Lutfullah Kakakhel irq_set_chip_and_handler_name(irq, &intc_dev, 1250547dc78SZubair Lutfullah Kakakhel handle_level_irq, "level"); 1260547dc78SZubair Lutfullah Kakakhel irq_set_status_flags(irq, IRQ_LEVEL); 1270547dc78SZubair Lutfullah Kakakhel } 1280547dc78SZubair Lutfullah Kakakhel return 0; 1290547dc78SZubair Lutfullah Kakakhel } 1300547dc78SZubair Lutfullah Kakakhel 1310547dc78SZubair Lutfullah Kakakhel static const struct irq_domain_ops xintc_irq_domain_ops = { 1320547dc78SZubair Lutfullah Kakakhel .xlate = irq_domain_xlate_onetwocell, 1330547dc78SZubair Lutfullah Kakakhel .map = xintc_map, 1340547dc78SZubair Lutfullah Kakakhel }; 1350547dc78SZubair Lutfullah Kakakhel 1360547dc78SZubair Lutfullah Kakakhel static int __init xilinx_intc_of_init(struct device_node *intc, 1370547dc78SZubair Lutfullah Kakakhel struct device_node *parent) 1380547dc78SZubair Lutfullah Kakakhel { 139591db74bSZubair Lutfullah Kakakhel u32 nr_irq; 1400547dc78SZubair Lutfullah Kakakhel int ret; 141591db74bSZubair Lutfullah Kakakhel struct xintc_irq_chip *irqc; 1420547dc78SZubair Lutfullah Kakakhel 143591db74bSZubair Lutfullah Kakakhel if (xintc_irqc) { 144591db74bSZubair Lutfullah Kakakhel pr_err("irq-xilinx: Multiple instances aren't supported\n"); 145591db74bSZubair Lutfullah Kakakhel return -EINVAL; 146591db74bSZubair Lutfullah Kakakhel } 147591db74bSZubair Lutfullah Kakakhel 148591db74bSZubair Lutfullah Kakakhel irqc = kzalloc(sizeof(*irqc), GFP_KERNEL); 149591db74bSZubair Lutfullah Kakakhel if (!irqc) 150591db74bSZubair Lutfullah Kakakhel return -ENOMEM; 151591db74bSZubair Lutfullah Kakakhel 152591db74bSZubair Lutfullah Kakakhel xintc_irqc = irqc; 153591db74bSZubair Lutfullah Kakakhel 154591db74bSZubair Lutfullah Kakakhel irqc->base = of_iomap(intc, 0); 155591db74bSZubair Lutfullah Kakakhel BUG_ON(!irqc->base); 1560547dc78SZubair Lutfullah Kakakhel 1570547dc78SZubair Lutfullah Kakakhel ret = of_property_read_u32(intc, "xlnx,num-intr-inputs", &nr_irq); 1580547dc78SZubair Lutfullah Kakakhel if (ret < 0) { 159a5734de2SZubair Lutfullah Kakakhel pr_err("irq-xilinx: unable to read xlnx,num-intr-inputs\n"); 160591db74bSZubair Lutfullah Kakakhel goto err_alloc; 1610547dc78SZubair Lutfullah Kakakhel } 1620547dc78SZubair Lutfullah Kakakhel 163591db74bSZubair Lutfullah Kakakhel ret = of_property_read_u32(intc, "xlnx,kind-of-intr", &irqc->intr_mask); 1640547dc78SZubair Lutfullah Kakakhel if (ret < 0) { 165a5734de2SZubair Lutfullah Kakakhel pr_err("irq-xilinx: unable to read xlnx,kind-of-intr\n"); 166591db74bSZubair Lutfullah Kakakhel goto err_alloc; 1670547dc78SZubair Lutfullah Kakakhel } 1680547dc78SZubair Lutfullah Kakakhel 169591db74bSZubair Lutfullah Kakakhel if (irqc->intr_mask >> nr_irq) 170a5734de2SZubair Lutfullah Kakakhel pr_warn("irq-xilinx: mismatch in kind-of-intr param\n"); 1710547dc78SZubair Lutfullah Kakakhel 172a5734de2SZubair Lutfullah Kakakhel pr_info("irq-xilinx: %s: num_irq=%d, edge=0x%x\n", 173591db74bSZubair Lutfullah Kakakhel intc->full_name, nr_irq, irqc->intr_mask); 1740547dc78SZubair Lutfullah Kakakhel 1750547dc78SZubair Lutfullah Kakakhel 1760547dc78SZubair Lutfullah Kakakhel /* 1770547dc78SZubair Lutfullah Kakakhel * Disable all external interrupts until they are 1780547dc78SZubair Lutfullah Kakakhel * explicity requested. 1790547dc78SZubair Lutfullah Kakakhel */ 180591db74bSZubair Lutfullah Kakakhel xintc_write(IER, 0); 1810547dc78SZubair Lutfullah Kakakhel 1820547dc78SZubair Lutfullah Kakakhel /* Acknowledge any pending interrupts just in case. */ 183591db74bSZubair Lutfullah Kakakhel xintc_write(IAR, 0xffffffff); 1840547dc78SZubair Lutfullah Kakakhel 1850547dc78SZubair Lutfullah Kakakhel /* Turn on the Master Enable. */ 186591db74bSZubair Lutfullah Kakakhel xintc_write(MER, MER_HIE | MER_ME); 187591db74bSZubair Lutfullah Kakakhel if (!(xintc_read(MER) & (MER_HIE | MER_ME))) { 188591db74bSZubair Lutfullah Kakakhel static_branch_enable(&xintc_is_be); 189591db74bSZubair Lutfullah Kakakhel xintc_write(MER, MER_HIE | MER_ME); 1900547dc78SZubair Lutfullah Kakakhel } 1910547dc78SZubair Lutfullah Kakakhel 192591db74bSZubair Lutfullah Kakakhel irqc->root_domain = irq_domain_add_linear(intc, nr_irq, 193591db74bSZubair Lutfullah Kakakhel &xintc_irq_domain_ops, irqc); 194591db74bSZubair Lutfullah Kakakhel if (!irqc->root_domain) { 195591db74bSZubair Lutfullah Kakakhel pr_err("irq-xilinx: Unable to create IRQ domain\n"); 196591db74bSZubair Lutfullah Kakakhel goto err_alloc; 197591db74bSZubair Lutfullah Kakakhel } 1980547dc78SZubair Lutfullah Kakakhel 199591db74bSZubair Lutfullah Kakakhel irq_set_default_host(irqc->root_domain); 2000547dc78SZubair Lutfullah Kakakhel 2010547dc78SZubair Lutfullah Kakakhel return 0; 202591db74bSZubair Lutfullah Kakakhel 203591db74bSZubair Lutfullah Kakakhel err_alloc: 204591db74bSZubair Lutfullah Kakakhel xintc_irqc = NULL; 205591db74bSZubair Lutfullah Kakakhel kfree(irqc); 206591db74bSZubair Lutfullah Kakakhel return ret; 207591db74bSZubair Lutfullah Kakakhel 2080547dc78SZubair Lutfullah Kakakhel } 2090547dc78SZubair Lutfullah Kakakhel 2100547dc78SZubair Lutfullah Kakakhel IRQCHIP_DECLARE(xilinx_intc, "xlnx,xps-intc-1.00.a", xilinx_intc_of_init); 211