1f55c73aeSArchana Sathyakumar // SPDX-License-Identifier: GPL-2.0 2f55c73aeSArchana Sathyakumar /* 3b2bb01edSLina Iyer * Copyright (c) 2017-2019, The Linux Foundation. All rights reserved. 4f55c73aeSArchana Sathyakumar */ 5f55c73aeSArchana Sathyakumar 6f55c73aeSArchana Sathyakumar #include <linux/err.h> 7f55c73aeSArchana Sathyakumar #include <linux/init.h> 8f55c73aeSArchana Sathyakumar #include <linux/irq.h> 9f55c73aeSArchana Sathyakumar #include <linux/irqchip.h> 10f55c73aeSArchana Sathyakumar #include <linux/irqdomain.h> 11f55c73aeSArchana Sathyakumar #include <linux/io.h> 12f55c73aeSArchana Sathyakumar #include <linux/kernel.h> 13f55c73aeSArchana Sathyakumar #include <linux/of.h> 14f55c73aeSArchana Sathyakumar #include <linux/of_address.h> 15f55c73aeSArchana Sathyakumar #include <linux/of_device.h> 16f55c73aeSArchana Sathyakumar #include <linux/spinlock.h> 17f55c73aeSArchana Sathyakumar #include <linux/platform_device.h> 18f55c73aeSArchana Sathyakumar #include <linux/slab.h> 19f55c73aeSArchana Sathyakumar #include <linux/types.h> 20f55c73aeSArchana Sathyakumar 21b2bb01edSLina Iyer #define PDC_MAX_IRQS 168 22f55c73aeSArchana Sathyakumar 23f55c73aeSArchana Sathyakumar #define CLEAR_INTR(reg, intr) (reg & ~(1 << intr)) 24f55c73aeSArchana Sathyakumar #define ENABLE_INTR(reg, intr) (reg | (1 << intr)) 25f55c73aeSArchana Sathyakumar 26f55c73aeSArchana Sathyakumar #define IRQ_ENABLE_BANK 0x10 27f55c73aeSArchana Sathyakumar #define IRQ_i_CFG 0x110 28f55c73aeSArchana Sathyakumar 29f55c73aeSArchana Sathyakumar struct pdc_pin_region { 30f55c73aeSArchana Sathyakumar u32 pin_base; 31f55c73aeSArchana Sathyakumar u32 parent_base; 32f55c73aeSArchana Sathyakumar u32 cnt; 33f55c73aeSArchana Sathyakumar }; 34f55c73aeSArchana Sathyakumar 35f55c73aeSArchana Sathyakumar static DEFINE_RAW_SPINLOCK(pdc_lock); 36f55c73aeSArchana Sathyakumar static void __iomem *pdc_base; 37f55c73aeSArchana Sathyakumar static struct pdc_pin_region *pdc_region; 38f55c73aeSArchana Sathyakumar static int pdc_region_cnt; 39f55c73aeSArchana Sathyakumar 40f55c73aeSArchana Sathyakumar static void pdc_reg_write(int reg, u32 i, u32 val) 41f55c73aeSArchana Sathyakumar { 42f55c73aeSArchana Sathyakumar writel_relaxed(val, pdc_base + reg + i * sizeof(u32)); 43f55c73aeSArchana Sathyakumar } 44f55c73aeSArchana Sathyakumar 45f55c73aeSArchana Sathyakumar static u32 pdc_reg_read(int reg, u32 i) 46f55c73aeSArchana Sathyakumar { 47f55c73aeSArchana Sathyakumar return readl_relaxed(pdc_base + reg + i * sizeof(u32)); 48f55c73aeSArchana Sathyakumar } 49f55c73aeSArchana Sathyakumar 50f55c73aeSArchana Sathyakumar static void pdc_enable_intr(struct irq_data *d, bool on) 51f55c73aeSArchana Sathyakumar { 52f55c73aeSArchana Sathyakumar int pin_out = d->hwirq; 53f55c73aeSArchana Sathyakumar u32 index, mask; 54f55c73aeSArchana Sathyakumar u32 enable; 55f55c73aeSArchana Sathyakumar 56f55c73aeSArchana Sathyakumar index = pin_out / 32; 57f55c73aeSArchana Sathyakumar mask = pin_out % 32; 58f55c73aeSArchana Sathyakumar 59f55c73aeSArchana Sathyakumar raw_spin_lock(&pdc_lock); 60f55c73aeSArchana Sathyakumar enable = pdc_reg_read(IRQ_ENABLE_BANK, index); 61f55c73aeSArchana Sathyakumar enable = on ? ENABLE_INTR(enable, mask) : CLEAR_INTR(enable, mask); 62f55c73aeSArchana Sathyakumar pdc_reg_write(IRQ_ENABLE_BANK, index, enable); 63f55c73aeSArchana Sathyakumar raw_spin_unlock(&pdc_lock); 64f55c73aeSArchana Sathyakumar } 65f55c73aeSArchana Sathyakumar 66*da3f875aSLina Iyer static void qcom_pdc_gic_disable(struct irq_data *d) 67f55c73aeSArchana Sathyakumar { 68f55c73aeSArchana Sathyakumar pdc_enable_intr(d, false); 69*da3f875aSLina Iyer irq_chip_disable_parent(d); 70*da3f875aSLina Iyer } 71*da3f875aSLina Iyer 72*da3f875aSLina Iyer static void qcom_pdc_gic_enable(struct irq_data *d) 73*da3f875aSLina Iyer { 74*da3f875aSLina Iyer pdc_enable_intr(d, true); 75*da3f875aSLina Iyer irq_chip_enable_parent(d); 76*da3f875aSLina Iyer } 77*da3f875aSLina Iyer 78*da3f875aSLina Iyer static void qcom_pdc_gic_mask(struct irq_data *d) 79*da3f875aSLina Iyer { 80f55c73aeSArchana Sathyakumar irq_chip_mask_parent(d); 81f55c73aeSArchana Sathyakumar } 82f55c73aeSArchana Sathyakumar 83f55c73aeSArchana Sathyakumar static void qcom_pdc_gic_unmask(struct irq_data *d) 84f55c73aeSArchana Sathyakumar { 85f55c73aeSArchana Sathyakumar irq_chip_unmask_parent(d); 86f55c73aeSArchana Sathyakumar } 87f55c73aeSArchana Sathyakumar 88f55c73aeSArchana Sathyakumar /* 89f55c73aeSArchana Sathyakumar * GIC does not handle falling edge or active low. To allow falling edge and 90f55c73aeSArchana Sathyakumar * active low interrupts to be handled at GIC, PDC has an inverter that inverts 91f55c73aeSArchana Sathyakumar * falling edge into a rising edge and active low into an active high. 92f55c73aeSArchana Sathyakumar * For the inverter to work, the polarity bit in the IRQ_CONFIG register has to 93f55c73aeSArchana Sathyakumar * set as per the table below. 94f55c73aeSArchana Sathyakumar * Level sensitive active low LOW 95f55c73aeSArchana Sathyakumar * Rising edge sensitive NOT USED 96f55c73aeSArchana Sathyakumar * Falling edge sensitive LOW 97f55c73aeSArchana Sathyakumar * Dual Edge sensitive NOT USED 98f55c73aeSArchana Sathyakumar * Level sensitive active High HIGH 99f55c73aeSArchana Sathyakumar * Falling Edge sensitive NOT USED 100f55c73aeSArchana Sathyakumar * Rising edge sensitive HIGH 101f55c73aeSArchana Sathyakumar * Dual Edge sensitive HIGH 102f55c73aeSArchana Sathyakumar */ 103f55c73aeSArchana Sathyakumar enum pdc_irq_config_bits { 104f55c73aeSArchana Sathyakumar PDC_LEVEL_LOW = 0b000, 105f55c73aeSArchana Sathyakumar PDC_EDGE_FALLING = 0b010, 106f55c73aeSArchana Sathyakumar PDC_LEVEL_HIGH = 0b100, 107f55c73aeSArchana Sathyakumar PDC_EDGE_RISING = 0b110, 108f55c73aeSArchana Sathyakumar PDC_EDGE_DUAL = 0b111, 109f55c73aeSArchana Sathyakumar }; 110f55c73aeSArchana Sathyakumar 111f55c73aeSArchana Sathyakumar /** 112f55c73aeSArchana Sathyakumar * qcom_pdc_gic_set_type: Configure PDC for the interrupt 113f55c73aeSArchana Sathyakumar * 114f55c73aeSArchana Sathyakumar * @d: the interrupt data 115f55c73aeSArchana Sathyakumar * @type: the interrupt type 116f55c73aeSArchana Sathyakumar * 117f55c73aeSArchana Sathyakumar * If @type is edge triggered, forward that as Rising edge as PDC 118f55c73aeSArchana Sathyakumar * takes care of converting falling edge to rising edge signal 119f55c73aeSArchana Sathyakumar * If @type is level, then forward that as level high as PDC 120f55c73aeSArchana Sathyakumar * takes care of converting falling edge to rising edge signal 121f55c73aeSArchana Sathyakumar */ 122f55c73aeSArchana Sathyakumar static int qcom_pdc_gic_set_type(struct irq_data *d, unsigned int type) 123f55c73aeSArchana Sathyakumar { 124f55c73aeSArchana Sathyakumar int pin_out = d->hwirq; 125f55c73aeSArchana Sathyakumar enum pdc_irq_config_bits pdc_type; 126f55c73aeSArchana Sathyakumar 127f55c73aeSArchana Sathyakumar switch (type) { 128f55c73aeSArchana Sathyakumar case IRQ_TYPE_EDGE_RISING: 129f55c73aeSArchana Sathyakumar pdc_type = PDC_EDGE_RISING; 130f55c73aeSArchana Sathyakumar break; 131f55c73aeSArchana Sathyakumar case IRQ_TYPE_EDGE_FALLING: 132f55c73aeSArchana Sathyakumar pdc_type = PDC_EDGE_FALLING; 133f55c73aeSArchana Sathyakumar type = IRQ_TYPE_EDGE_RISING; 134f55c73aeSArchana Sathyakumar break; 135f55c73aeSArchana Sathyakumar case IRQ_TYPE_EDGE_BOTH: 136f55c73aeSArchana Sathyakumar pdc_type = PDC_EDGE_DUAL; 1377bae48b2SLina Iyer type = IRQ_TYPE_EDGE_RISING; 138f55c73aeSArchana Sathyakumar break; 139f55c73aeSArchana Sathyakumar case IRQ_TYPE_LEVEL_HIGH: 140f55c73aeSArchana Sathyakumar pdc_type = PDC_LEVEL_HIGH; 141f55c73aeSArchana Sathyakumar break; 142f55c73aeSArchana Sathyakumar case IRQ_TYPE_LEVEL_LOW: 143f55c73aeSArchana Sathyakumar pdc_type = PDC_LEVEL_LOW; 144f55c73aeSArchana Sathyakumar type = IRQ_TYPE_LEVEL_HIGH; 145f55c73aeSArchana Sathyakumar break; 146f55c73aeSArchana Sathyakumar default: 147f55c73aeSArchana Sathyakumar WARN_ON(1); 148f55c73aeSArchana Sathyakumar return -EINVAL; 149f55c73aeSArchana Sathyakumar } 150f55c73aeSArchana Sathyakumar 151f55c73aeSArchana Sathyakumar pdc_reg_write(IRQ_i_CFG, pin_out, pdc_type); 152f55c73aeSArchana Sathyakumar 153f55c73aeSArchana Sathyakumar return irq_chip_set_type_parent(d, type); 154f55c73aeSArchana Sathyakumar } 155f55c73aeSArchana Sathyakumar 156f55c73aeSArchana Sathyakumar static struct irq_chip qcom_pdc_gic_chip = { 157f55c73aeSArchana Sathyakumar .name = "PDC", 158f55c73aeSArchana Sathyakumar .irq_eoi = irq_chip_eoi_parent, 159f55c73aeSArchana Sathyakumar .irq_mask = qcom_pdc_gic_mask, 160f55c73aeSArchana Sathyakumar .irq_unmask = qcom_pdc_gic_unmask, 161*da3f875aSLina Iyer .irq_disable = qcom_pdc_gic_disable, 162*da3f875aSLina Iyer .irq_enable = qcom_pdc_gic_enable, 163f55c73aeSArchana Sathyakumar .irq_retrigger = irq_chip_retrigger_hierarchy, 164f55c73aeSArchana Sathyakumar .irq_set_type = qcom_pdc_gic_set_type, 165f55c73aeSArchana Sathyakumar .flags = IRQCHIP_MASK_ON_SUSPEND | 166f55c73aeSArchana Sathyakumar IRQCHIP_SET_TYPE_MASKED | 167f55c73aeSArchana Sathyakumar IRQCHIP_SKIP_SET_WAKE, 168f55c73aeSArchana Sathyakumar .irq_set_vcpu_affinity = irq_chip_set_vcpu_affinity_parent, 169f55c73aeSArchana Sathyakumar .irq_set_affinity = irq_chip_set_affinity_parent, 170f55c73aeSArchana Sathyakumar }; 171f55c73aeSArchana Sathyakumar 172f55c73aeSArchana Sathyakumar static irq_hw_number_t get_parent_hwirq(int pin) 173f55c73aeSArchana Sathyakumar { 174f55c73aeSArchana Sathyakumar int i; 175f55c73aeSArchana Sathyakumar struct pdc_pin_region *region; 176f55c73aeSArchana Sathyakumar 177f55c73aeSArchana Sathyakumar for (i = 0; i < pdc_region_cnt; i++) { 178f55c73aeSArchana Sathyakumar region = &pdc_region[i]; 179f55c73aeSArchana Sathyakumar if (pin >= region->pin_base && 180f55c73aeSArchana Sathyakumar pin < region->pin_base + region->cnt) 181f55c73aeSArchana Sathyakumar return (region->parent_base + pin - region->pin_base); 182f55c73aeSArchana Sathyakumar } 183f55c73aeSArchana Sathyakumar 184f55c73aeSArchana Sathyakumar WARN_ON(1); 185f55c73aeSArchana Sathyakumar return ~0UL; 186f55c73aeSArchana Sathyakumar } 187f55c73aeSArchana Sathyakumar 188f55c73aeSArchana Sathyakumar static int qcom_pdc_translate(struct irq_domain *d, struct irq_fwspec *fwspec, 189f55c73aeSArchana Sathyakumar unsigned long *hwirq, unsigned int *type) 190f55c73aeSArchana Sathyakumar { 191f55c73aeSArchana Sathyakumar if (is_of_node(fwspec->fwnode)) { 192f55c73aeSArchana Sathyakumar if (fwspec->param_count != 2) 193f55c73aeSArchana Sathyakumar return -EINVAL; 194f55c73aeSArchana Sathyakumar 195f55c73aeSArchana Sathyakumar *hwirq = fwspec->param[0]; 196f55c73aeSArchana Sathyakumar *type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK; 197f55c73aeSArchana Sathyakumar return 0; 198f55c73aeSArchana Sathyakumar } 199f55c73aeSArchana Sathyakumar 200f55c73aeSArchana Sathyakumar return -EINVAL; 201f55c73aeSArchana Sathyakumar } 202f55c73aeSArchana Sathyakumar 203f55c73aeSArchana Sathyakumar static int qcom_pdc_alloc(struct irq_domain *domain, unsigned int virq, 204f55c73aeSArchana Sathyakumar unsigned int nr_irqs, void *data) 205f55c73aeSArchana Sathyakumar { 206f55c73aeSArchana Sathyakumar struct irq_fwspec *fwspec = data; 207f55c73aeSArchana Sathyakumar struct irq_fwspec parent_fwspec; 208f55c73aeSArchana Sathyakumar irq_hw_number_t hwirq, parent_hwirq; 209f55c73aeSArchana Sathyakumar unsigned int type; 210f55c73aeSArchana Sathyakumar int ret; 211f55c73aeSArchana Sathyakumar 212f55c73aeSArchana Sathyakumar ret = qcom_pdc_translate(domain, fwspec, &hwirq, &type); 213f55c73aeSArchana Sathyakumar if (ret) 214f55c73aeSArchana Sathyakumar return -EINVAL; 215f55c73aeSArchana Sathyakumar 216f55c73aeSArchana Sathyakumar parent_hwirq = get_parent_hwirq(hwirq); 217f55c73aeSArchana Sathyakumar if (parent_hwirq == ~0UL) 218f55c73aeSArchana Sathyakumar return -EINVAL; 219f55c73aeSArchana Sathyakumar 220f55c73aeSArchana Sathyakumar ret = irq_domain_set_hwirq_and_chip(domain, virq, hwirq, 221f55c73aeSArchana Sathyakumar &qcom_pdc_gic_chip, NULL); 222f55c73aeSArchana Sathyakumar if (ret) 223f55c73aeSArchana Sathyakumar return ret; 224f55c73aeSArchana Sathyakumar 225f55c73aeSArchana Sathyakumar if (type & IRQ_TYPE_EDGE_BOTH) 226f55c73aeSArchana Sathyakumar type = IRQ_TYPE_EDGE_RISING; 227f55c73aeSArchana Sathyakumar 228f55c73aeSArchana Sathyakumar if (type & IRQ_TYPE_LEVEL_MASK) 229f55c73aeSArchana Sathyakumar type = IRQ_TYPE_LEVEL_HIGH; 230f55c73aeSArchana Sathyakumar 231f55c73aeSArchana Sathyakumar parent_fwspec.fwnode = domain->parent->fwnode; 232f55c73aeSArchana Sathyakumar parent_fwspec.param_count = 3; 233f55c73aeSArchana Sathyakumar parent_fwspec.param[0] = 0; 234f55c73aeSArchana Sathyakumar parent_fwspec.param[1] = parent_hwirq; 235f55c73aeSArchana Sathyakumar parent_fwspec.param[2] = type; 236f55c73aeSArchana Sathyakumar 237f55c73aeSArchana Sathyakumar return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, 238f55c73aeSArchana Sathyakumar &parent_fwspec); 239f55c73aeSArchana Sathyakumar } 240f55c73aeSArchana Sathyakumar 241f55c73aeSArchana Sathyakumar static const struct irq_domain_ops qcom_pdc_ops = { 242f55c73aeSArchana Sathyakumar .translate = qcom_pdc_translate, 243f55c73aeSArchana Sathyakumar .alloc = qcom_pdc_alloc, 244f55c73aeSArchana Sathyakumar .free = irq_domain_free_irqs_common, 245f55c73aeSArchana Sathyakumar }; 246f55c73aeSArchana Sathyakumar 247f55c73aeSArchana Sathyakumar static int pdc_setup_pin_mapping(struct device_node *np) 248f55c73aeSArchana Sathyakumar { 249f55c73aeSArchana Sathyakumar int ret, n; 250f55c73aeSArchana Sathyakumar 251f55c73aeSArchana Sathyakumar n = of_property_count_elems_of_size(np, "qcom,pdc-ranges", sizeof(u32)); 252f55c73aeSArchana Sathyakumar if (n <= 0 || n % 3) 253f55c73aeSArchana Sathyakumar return -EINVAL; 254f55c73aeSArchana Sathyakumar 255f55c73aeSArchana Sathyakumar pdc_region_cnt = n / 3; 256f55c73aeSArchana Sathyakumar pdc_region = kcalloc(pdc_region_cnt, sizeof(*pdc_region), GFP_KERNEL); 257f55c73aeSArchana Sathyakumar if (!pdc_region) { 258f55c73aeSArchana Sathyakumar pdc_region_cnt = 0; 259f55c73aeSArchana Sathyakumar return -ENOMEM; 260f55c73aeSArchana Sathyakumar } 261f55c73aeSArchana Sathyakumar 262f55c73aeSArchana Sathyakumar for (n = 0; n < pdc_region_cnt; n++) { 263f55c73aeSArchana Sathyakumar ret = of_property_read_u32_index(np, "qcom,pdc-ranges", 264f55c73aeSArchana Sathyakumar n * 3 + 0, 265f55c73aeSArchana Sathyakumar &pdc_region[n].pin_base); 266f55c73aeSArchana Sathyakumar if (ret) 267f55c73aeSArchana Sathyakumar return ret; 268f55c73aeSArchana Sathyakumar ret = of_property_read_u32_index(np, "qcom,pdc-ranges", 269f55c73aeSArchana Sathyakumar n * 3 + 1, 270f55c73aeSArchana Sathyakumar &pdc_region[n].parent_base); 271f55c73aeSArchana Sathyakumar if (ret) 272f55c73aeSArchana Sathyakumar return ret; 273f55c73aeSArchana Sathyakumar ret = of_property_read_u32_index(np, "qcom,pdc-ranges", 274f55c73aeSArchana Sathyakumar n * 3 + 2, 275f55c73aeSArchana Sathyakumar &pdc_region[n].cnt); 276f55c73aeSArchana Sathyakumar if (ret) 277f55c73aeSArchana Sathyakumar return ret; 278f55c73aeSArchana Sathyakumar } 279f55c73aeSArchana Sathyakumar 280f55c73aeSArchana Sathyakumar return 0; 281f55c73aeSArchana Sathyakumar } 282f55c73aeSArchana Sathyakumar 283f55c73aeSArchana Sathyakumar static int qcom_pdc_init(struct device_node *node, struct device_node *parent) 284f55c73aeSArchana Sathyakumar { 285f55c73aeSArchana Sathyakumar struct irq_domain *parent_domain, *pdc_domain; 286f55c73aeSArchana Sathyakumar int ret; 287f55c73aeSArchana Sathyakumar 288f55c73aeSArchana Sathyakumar pdc_base = of_iomap(node, 0); 289f55c73aeSArchana Sathyakumar if (!pdc_base) { 290f55c73aeSArchana Sathyakumar pr_err("%pOF: unable to map PDC registers\n", node); 291f55c73aeSArchana Sathyakumar return -ENXIO; 292f55c73aeSArchana Sathyakumar } 293f55c73aeSArchana Sathyakumar 294f55c73aeSArchana Sathyakumar parent_domain = irq_find_host(parent); 295f55c73aeSArchana Sathyakumar if (!parent_domain) { 296f55c73aeSArchana Sathyakumar pr_err("%pOF: unable to find PDC's parent domain\n", node); 297f55c73aeSArchana Sathyakumar ret = -ENXIO; 298f55c73aeSArchana Sathyakumar goto fail; 299f55c73aeSArchana Sathyakumar } 300f55c73aeSArchana Sathyakumar 301f55c73aeSArchana Sathyakumar ret = pdc_setup_pin_mapping(node); 302f55c73aeSArchana Sathyakumar if (ret) { 303f55c73aeSArchana Sathyakumar pr_err("%pOF: failed to init PDC pin-hwirq mapping\n", node); 304f55c73aeSArchana Sathyakumar goto fail; 305f55c73aeSArchana Sathyakumar } 306f55c73aeSArchana Sathyakumar 307f55c73aeSArchana Sathyakumar pdc_domain = irq_domain_create_hierarchy(parent_domain, 0, PDC_MAX_IRQS, 308f55c73aeSArchana Sathyakumar of_fwnode_handle(node), 309f55c73aeSArchana Sathyakumar &qcom_pdc_ops, NULL); 310f55c73aeSArchana Sathyakumar if (!pdc_domain) { 311f55c73aeSArchana Sathyakumar pr_err("%pOF: GIC domain add failed\n", node); 312f55c73aeSArchana Sathyakumar ret = -ENOMEM; 313f55c73aeSArchana Sathyakumar goto fail; 314f55c73aeSArchana Sathyakumar } 315f55c73aeSArchana Sathyakumar 316f55c73aeSArchana Sathyakumar return 0; 317f55c73aeSArchana Sathyakumar 318f55c73aeSArchana Sathyakumar fail: 319f55c73aeSArchana Sathyakumar kfree(pdc_region); 320f55c73aeSArchana Sathyakumar iounmap(pdc_base); 321f55c73aeSArchana Sathyakumar return ret; 322f55c73aeSArchana Sathyakumar } 323f55c73aeSArchana Sathyakumar 3248e4d5a5bSRajendra Nayak IRQCHIP_DECLARE(qcom_pdc, "qcom,pdc", qcom_pdc_init); 325