1*706cffc1SArd Biesheuvel /* 2*706cffc1SArd Biesheuvel * Driver for Socionext External Interrupt Unit (EXIU) 3*706cffc1SArd Biesheuvel * 4*706cffc1SArd Biesheuvel * Copyright (c) 2017 Linaro, Ltd. <ard.biesheuvel@linaro.org> 5*706cffc1SArd Biesheuvel * 6*706cffc1SArd Biesheuvel * Based on irq-tegra.c: 7*706cffc1SArd Biesheuvel * Copyright (C) 2011 Google, Inc. 8*706cffc1SArd Biesheuvel * Copyright (C) 2010,2013, NVIDIA Corporation 9*706cffc1SArd Biesheuvel * 10*706cffc1SArd Biesheuvel * This program is free software; you can redistribute it and/or modify 11*706cffc1SArd Biesheuvel * it under the terms of the GNU General Public License version 2 as 12*706cffc1SArd Biesheuvel * published by the Free Software Foundation. 13*706cffc1SArd Biesheuvel */ 14*706cffc1SArd Biesheuvel 15*706cffc1SArd Biesheuvel #include <linux/interrupt.h> 16*706cffc1SArd Biesheuvel #include <linux/io.h> 17*706cffc1SArd Biesheuvel #include <linux/irq.h> 18*706cffc1SArd Biesheuvel #include <linux/irqchip.h> 19*706cffc1SArd Biesheuvel #include <linux/irqdomain.h> 20*706cffc1SArd Biesheuvel #include <linux/of.h> 21*706cffc1SArd Biesheuvel #include <linux/of_address.h> 22*706cffc1SArd Biesheuvel #include <linux/of_irq.h> 23*706cffc1SArd Biesheuvel 24*706cffc1SArd Biesheuvel #include <dt-bindings/interrupt-controller/arm-gic.h> 25*706cffc1SArd Biesheuvel 26*706cffc1SArd Biesheuvel #define NUM_IRQS 32 27*706cffc1SArd Biesheuvel 28*706cffc1SArd Biesheuvel #define EIMASK 0x00 29*706cffc1SArd Biesheuvel #define EISRCSEL 0x04 30*706cffc1SArd Biesheuvel #define EIREQSTA 0x08 31*706cffc1SArd Biesheuvel #define EIRAWREQSTA 0x0C 32*706cffc1SArd Biesheuvel #define EIREQCLR 0x10 33*706cffc1SArd Biesheuvel #define EILVL 0x14 34*706cffc1SArd Biesheuvel #define EIEDG 0x18 35*706cffc1SArd Biesheuvel #define EISIR 0x1C 36*706cffc1SArd Biesheuvel 37*706cffc1SArd Biesheuvel struct exiu_irq_data { 38*706cffc1SArd Biesheuvel void __iomem *base; 39*706cffc1SArd Biesheuvel u32 spi_base; 40*706cffc1SArd Biesheuvel }; 41*706cffc1SArd Biesheuvel 42*706cffc1SArd Biesheuvel static void exiu_irq_eoi(struct irq_data *d) 43*706cffc1SArd Biesheuvel { 44*706cffc1SArd Biesheuvel struct exiu_irq_data *data = irq_data_get_irq_chip_data(d); 45*706cffc1SArd Biesheuvel 46*706cffc1SArd Biesheuvel writel(BIT(d->hwirq), data->base + EIREQCLR); 47*706cffc1SArd Biesheuvel irq_chip_eoi_parent(d); 48*706cffc1SArd Biesheuvel } 49*706cffc1SArd Biesheuvel 50*706cffc1SArd Biesheuvel static void exiu_irq_mask(struct irq_data *d) 51*706cffc1SArd Biesheuvel { 52*706cffc1SArd Biesheuvel struct exiu_irq_data *data = irq_data_get_irq_chip_data(d); 53*706cffc1SArd Biesheuvel u32 val; 54*706cffc1SArd Biesheuvel 55*706cffc1SArd Biesheuvel val = readl_relaxed(data->base + EIMASK) | BIT(d->hwirq); 56*706cffc1SArd Biesheuvel writel_relaxed(val, data->base + EIMASK); 57*706cffc1SArd Biesheuvel irq_chip_mask_parent(d); 58*706cffc1SArd Biesheuvel } 59*706cffc1SArd Biesheuvel 60*706cffc1SArd Biesheuvel static void exiu_irq_unmask(struct irq_data *d) 61*706cffc1SArd Biesheuvel { 62*706cffc1SArd Biesheuvel struct exiu_irq_data *data = irq_data_get_irq_chip_data(d); 63*706cffc1SArd Biesheuvel u32 val; 64*706cffc1SArd Biesheuvel 65*706cffc1SArd Biesheuvel val = readl_relaxed(data->base + EIMASK) & ~BIT(d->hwirq); 66*706cffc1SArd Biesheuvel writel_relaxed(val, data->base + EIMASK); 67*706cffc1SArd Biesheuvel irq_chip_unmask_parent(d); 68*706cffc1SArd Biesheuvel } 69*706cffc1SArd Biesheuvel 70*706cffc1SArd Biesheuvel static void exiu_irq_enable(struct irq_data *d) 71*706cffc1SArd Biesheuvel { 72*706cffc1SArd Biesheuvel struct exiu_irq_data *data = irq_data_get_irq_chip_data(d); 73*706cffc1SArd Biesheuvel u32 val; 74*706cffc1SArd Biesheuvel 75*706cffc1SArd Biesheuvel /* clear interrupts that were latched while disabled */ 76*706cffc1SArd Biesheuvel writel_relaxed(BIT(d->hwirq), data->base + EIREQCLR); 77*706cffc1SArd Biesheuvel 78*706cffc1SArd Biesheuvel val = readl_relaxed(data->base + EIMASK) & ~BIT(d->hwirq); 79*706cffc1SArd Biesheuvel writel_relaxed(val, data->base + EIMASK); 80*706cffc1SArd Biesheuvel irq_chip_enable_parent(d); 81*706cffc1SArd Biesheuvel } 82*706cffc1SArd Biesheuvel 83*706cffc1SArd Biesheuvel static int exiu_irq_set_type(struct irq_data *d, unsigned int type) 84*706cffc1SArd Biesheuvel { 85*706cffc1SArd Biesheuvel struct exiu_irq_data *data = irq_data_get_irq_chip_data(d); 86*706cffc1SArd Biesheuvel u32 val; 87*706cffc1SArd Biesheuvel 88*706cffc1SArd Biesheuvel val = readl_relaxed(data->base + EILVL); 89*706cffc1SArd Biesheuvel if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH) 90*706cffc1SArd Biesheuvel val |= BIT(d->hwirq); 91*706cffc1SArd Biesheuvel else 92*706cffc1SArd Biesheuvel val &= ~BIT(d->hwirq); 93*706cffc1SArd Biesheuvel writel_relaxed(val, data->base + EILVL); 94*706cffc1SArd Biesheuvel 95*706cffc1SArd Biesheuvel val = readl_relaxed(data->base + EIEDG); 96*706cffc1SArd Biesheuvel if (type == IRQ_TYPE_LEVEL_LOW || type == IRQ_TYPE_LEVEL_HIGH) 97*706cffc1SArd Biesheuvel val &= ~BIT(d->hwirq); 98*706cffc1SArd Biesheuvel else 99*706cffc1SArd Biesheuvel val |= BIT(d->hwirq); 100*706cffc1SArd Biesheuvel writel_relaxed(val, data->base + EIEDG); 101*706cffc1SArd Biesheuvel 102*706cffc1SArd Biesheuvel writel_relaxed(BIT(d->hwirq), data->base + EIREQCLR); 103*706cffc1SArd Biesheuvel 104*706cffc1SArd Biesheuvel return irq_chip_set_type_parent(d, IRQ_TYPE_LEVEL_HIGH); 105*706cffc1SArd Biesheuvel } 106*706cffc1SArd Biesheuvel 107*706cffc1SArd Biesheuvel static struct irq_chip exiu_irq_chip = { 108*706cffc1SArd Biesheuvel .name = "EXIU", 109*706cffc1SArd Biesheuvel .irq_eoi = exiu_irq_eoi, 110*706cffc1SArd Biesheuvel .irq_enable = exiu_irq_enable, 111*706cffc1SArd Biesheuvel .irq_mask = exiu_irq_mask, 112*706cffc1SArd Biesheuvel .irq_unmask = exiu_irq_unmask, 113*706cffc1SArd Biesheuvel .irq_set_type = exiu_irq_set_type, 114*706cffc1SArd Biesheuvel .irq_set_affinity = irq_chip_set_affinity_parent, 115*706cffc1SArd Biesheuvel .flags = IRQCHIP_SET_TYPE_MASKED | 116*706cffc1SArd Biesheuvel IRQCHIP_SKIP_SET_WAKE | 117*706cffc1SArd Biesheuvel IRQCHIP_EOI_THREADED | 118*706cffc1SArd Biesheuvel IRQCHIP_MASK_ON_SUSPEND, 119*706cffc1SArd Biesheuvel }; 120*706cffc1SArd Biesheuvel 121*706cffc1SArd Biesheuvel static int exiu_domain_translate(struct irq_domain *domain, 122*706cffc1SArd Biesheuvel struct irq_fwspec *fwspec, 123*706cffc1SArd Biesheuvel unsigned long *hwirq, 124*706cffc1SArd Biesheuvel unsigned int *type) 125*706cffc1SArd Biesheuvel { 126*706cffc1SArd Biesheuvel struct exiu_irq_data *info = domain->host_data; 127*706cffc1SArd Biesheuvel 128*706cffc1SArd Biesheuvel if (is_of_node(fwspec->fwnode)) { 129*706cffc1SArd Biesheuvel if (fwspec->param_count != 3) 130*706cffc1SArd Biesheuvel return -EINVAL; 131*706cffc1SArd Biesheuvel 132*706cffc1SArd Biesheuvel if (fwspec->param[0] != GIC_SPI) 133*706cffc1SArd Biesheuvel return -EINVAL; /* No PPI should point to this domain */ 134*706cffc1SArd Biesheuvel 135*706cffc1SArd Biesheuvel *hwirq = fwspec->param[1] - info->spi_base; 136*706cffc1SArd Biesheuvel *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK; 137*706cffc1SArd Biesheuvel return 0; 138*706cffc1SArd Biesheuvel } 139*706cffc1SArd Biesheuvel return -EINVAL; 140*706cffc1SArd Biesheuvel } 141*706cffc1SArd Biesheuvel 142*706cffc1SArd Biesheuvel static int exiu_domain_alloc(struct irq_domain *dom, unsigned int virq, 143*706cffc1SArd Biesheuvel unsigned int nr_irqs, void *data) 144*706cffc1SArd Biesheuvel { 145*706cffc1SArd Biesheuvel struct irq_fwspec *fwspec = data; 146*706cffc1SArd Biesheuvel struct irq_fwspec parent_fwspec; 147*706cffc1SArd Biesheuvel struct exiu_irq_data *info = dom->host_data; 148*706cffc1SArd Biesheuvel irq_hw_number_t hwirq; 149*706cffc1SArd Biesheuvel 150*706cffc1SArd Biesheuvel if (fwspec->param_count != 3) 151*706cffc1SArd Biesheuvel return -EINVAL; /* Not GIC compliant */ 152*706cffc1SArd Biesheuvel if (fwspec->param[0] != GIC_SPI) 153*706cffc1SArd Biesheuvel return -EINVAL; /* No PPI should point to this domain */ 154*706cffc1SArd Biesheuvel 155*706cffc1SArd Biesheuvel WARN_ON(nr_irqs != 1); 156*706cffc1SArd Biesheuvel hwirq = fwspec->param[1] - info->spi_base; 157*706cffc1SArd Biesheuvel irq_domain_set_hwirq_and_chip(dom, virq, hwirq, &exiu_irq_chip, info); 158*706cffc1SArd Biesheuvel 159*706cffc1SArd Biesheuvel parent_fwspec = *fwspec; 160*706cffc1SArd Biesheuvel parent_fwspec.fwnode = dom->parent->fwnode; 161*706cffc1SArd Biesheuvel return irq_domain_alloc_irqs_parent(dom, virq, nr_irqs, &parent_fwspec); 162*706cffc1SArd Biesheuvel } 163*706cffc1SArd Biesheuvel 164*706cffc1SArd Biesheuvel static const struct irq_domain_ops exiu_domain_ops = { 165*706cffc1SArd Biesheuvel .translate = exiu_domain_translate, 166*706cffc1SArd Biesheuvel .alloc = exiu_domain_alloc, 167*706cffc1SArd Biesheuvel .free = irq_domain_free_irqs_common, 168*706cffc1SArd Biesheuvel }; 169*706cffc1SArd Biesheuvel 170*706cffc1SArd Biesheuvel static int __init exiu_init(struct device_node *node, 171*706cffc1SArd Biesheuvel struct device_node *parent) 172*706cffc1SArd Biesheuvel { 173*706cffc1SArd Biesheuvel struct irq_domain *parent_domain, *domain; 174*706cffc1SArd Biesheuvel struct exiu_irq_data *data; 175*706cffc1SArd Biesheuvel int err; 176*706cffc1SArd Biesheuvel 177*706cffc1SArd Biesheuvel if (!parent) { 178*706cffc1SArd Biesheuvel pr_err("%pOF: no parent, giving up\n", node); 179*706cffc1SArd Biesheuvel return -ENODEV; 180*706cffc1SArd Biesheuvel } 181*706cffc1SArd Biesheuvel 182*706cffc1SArd Biesheuvel parent_domain = irq_find_host(parent); 183*706cffc1SArd Biesheuvel if (!parent_domain) { 184*706cffc1SArd Biesheuvel pr_err("%pOF: unable to obtain parent domain\n", node); 185*706cffc1SArd Biesheuvel return -ENXIO; 186*706cffc1SArd Biesheuvel } 187*706cffc1SArd Biesheuvel 188*706cffc1SArd Biesheuvel data = kzalloc(sizeof(*data), GFP_KERNEL); 189*706cffc1SArd Biesheuvel if (!data) 190*706cffc1SArd Biesheuvel return -ENOMEM; 191*706cffc1SArd Biesheuvel 192*706cffc1SArd Biesheuvel if (of_property_read_u32(node, "socionext,spi-base", &data->spi_base)) { 193*706cffc1SArd Biesheuvel pr_err("%pOF: failed to parse 'spi-base' property\n", node); 194*706cffc1SArd Biesheuvel err = -ENODEV; 195*706cffc1SArd Biesheuvel goto out_free; 196*706cffc1SArd Biesheuvel } 197*706cffc1SArd Biesheuvel 198*706cffc1SArd Biesheuvel data->base = of_iomap(node, 0); 199*706cffc1SArd Biesheuvel if (IS_ERR(data->base)) { 200*706cffc1SArd Biesheuvel err = PTR_ERR(data->base); 201*706cffc1SArd Biesheuvel goto out_free; 202*706cffc1SArd Biesheuvel } 203*706cffc1SArd Biesheuvel 204*706cffc1SArd Biesheuvel /* clear and mask all interrupts */ 205*706cffc1SArd Biesheuvel writel_relaxed(0xFFFFFFFF, data->base + EIREQCLR); 206*706cffc1SArd Biesheuvel writel_relaxed(0xFFFFFFFF, data->base + EIMASK); 207*706cffc1SArd Biesheuvel 208*706cffc1SArd Biesheuvel domain = irq_domain_add_hierarchy(parent_domain, 0, NUM_IRQS, node, 209*706cffc1SArd Biesheuvel &exiu_domain_ops, data); 210*706cffc1SArd Biesheuvel if (!domain) { 211*706cffc1SArd Biesheuvel pr_err("%pOF: failed to allocate domain\n", node); 212*706cffc1SArd Biesheuvel err = -ENOMEM; 213*706cffc1SArd Biesheuvel goto out_unmap; 214*706cffc1SArd Biesheuvel } 215*706cffc1SArd Biesheuvel 216*706cffc1SArd Biesheuvel pr_info("%pOF: %d interrupts forwarded to %pOF\n", node, NUM_IRQS, 217*706cffc1SArd Biesheuvel parent); 218*706cffc1SArd Biesheuvel 219*706cffc1SArd Biesheuvel return 0; 220*706cffc1SArd Biesheuvel 221*706cffc1SArd Biesheuvel out_unmap: 222*706cffc1SArd Biesheuvel iounmap(data->base); 223*706cffc1SArd Biesheuvel out_free: 224*706cffc1SArd Biesheuvel kfree(data); 225*706cffc1SArd Biesheuvel return err; 226*706cffc1SArd Biesheuvel } 227*706cffc1SArd Biesheuvel IRQCHIP_DECLARE(exiu, "socionext,synquacer-exiu", exiu_init); 228