196ca848eSSricharan R /* 296ca848eSSricharan R * drivers/irqchip/irq-crossbar.c 396ca848eSSricharan R * 496ca848eSSricharan R * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com 596ca848eSSricharan R * Author: Sricharan R <r.sricharan@ti.com> 696ca848eSSricharan R * 796ca848eSSricharan R * This program is free software; you can redistribute it and/or modify 896ca848eSSricharan R * it under the terms of the GNU General Public License version 2 as 996ca848eSSricharan R * published by the Free Software Foundation. 1096ca848eSSricharan R * 1196ca848eSSricharan R */ 1296ca848eSSricharan R #include <linux/err.h> 1396ca848eSSricharan R #include <linux/io.h> 1496ca848eSSricharan R #include <linux/of_address.h> 1596ca848eSSricharan R #include <linux/of_irq.h> 1696ca848eSSricharan R #include <linux/slab.h> 1796ca848eSSricharan R #include <linux/irqchip/arm-gic.h> 18*4dbf45e3SNishanth Menon #include <linux/irqchip/irq-crossbar.h> 1996ca848eSSricharan R 2096ca848eSSricharan R #define IRQ_FREE -1 211d50d2ceSNishanth Menon #define IRQ_RESERVED -2 2264e0f8baSNishanth Menon #define IRQ_SKIP -3 2396ca848eSSricharan R #define GIC_IRQ_START 32 2496ca848eSSricharan R 2596ca848eSSricharan R /* 2696ca848eSSricharan R * @int_max: maximum number of supported interrupts 27a35057d1SNishanth Menon * @safe_map: safe default value to initialize the crossbar 2896ca848eSSricharan R * @irq_map: array of interrupts to crossbar number mapping 2996ca848eSSricharan R * @crossbar_base: crossbar base address 3096ca848eSSricharan R * @register_offsets: offsets for each irq number 3196ca848eSSricharan R */ 3296ca848eSSricharan R struct crossbar_device { 3396ca848eSSricharan R uint int_max; 34a35057d1SNishanth Menon uint safe_map; 3596ca848eSSricharan R uint *irq_map; 3696ca848eSSricharan R void __iomem *crossbar_base; 3796ca848eSSricharan R int *register_offsets; 3896ca848eSSricharan R void (*write)(int, int); 3996ca848eSSricharan R }; 4096ca848eSSricharan R 4196ca848eSSricharan R static struct crossbar_device *cb; 4296ca848eSSricharan R 4396ca848eSSricharan R static inline void crossbar_writel(int irq_no, int cb_no) 4496ca848eSSricharan R { 4596ca848eSSricharan R writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]); 4696ca848eSSricharan R } 4796ca848eSSricharan R 4896ca848eSSricharan R static inline void crossbar_writew(int irq_no, int cb_no) 4996ca848eSSricharan R { 5096ca848eSSricharan R writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]); 5196ca848eSSricharan R } 5296ca848eSSricharan R 5396ca848eSSricharan R static inline void crossbar_writeb(int irq_no, int cb_no) 5496ca848eSSricharan R { 5596ca848eSSricharan R writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]); 5696ca848eSSricharan R } 5796ca848eSSricharan R 586f16fc87SNishanth Menon static inline int get_prev_map_irq(int cb_no) 596f16fc87SNishanth Menon { 606f16fc87SNishanth Menon int i; 616f16fc87SNishanth Menon 62ddee0fb4SNishanth Menon for (i = cb->int_max - 1; i >= 0; i--) 636f16fc87SNishanth Menon if (cb->irq_map[i] == cb_no) 646f16fc87SNishanth Menon return i; 656f16fc87SNishanth Menon 666f16fc87SNishanth Menon return -ENODEV; 676f16fc87SNishanth Menon } 686f16fc87SNishanth Menon 6996ca848eSSricharan R static inline int allocate_free_irq(int cb_no) 7096ca848eSSricharan R { 7196ca848eSSricharan R int i; 7296ca848eSSricharan R 73ddee0fb4SNishanth Menon for (i = cb->int_max - 1; i >= 0; i--) { 7496ca848eSSricharan R if (cb->irq_map[i] == IRQ_FREE) { 7596ca848eSSricharan R cb->irq_map[i] = cb_no; 7696ca848eSSricharan R return i; 7796ca848eSSricharan R } 7896ca848eSSricharan R } 7996ca848eSSricharan R 8096ca848eSSricharan R return -ENODEV; 8196ca848eSSricharan R } 8296ca848eSSricharan R 8396ca848eSSricharan R static int crossbar_domain_map(struct irq_domain *d, unsigned int irq, 8496ca848eSSricharan R irq_hw_number_t hw) 8596ca848eSSricharan R { 8696ca848eSSricharan R cb->write(hw - GIC_IRQ_START, cb->irq_map[hw - GIC_IRQ_START]); 8796ca848eSSricharan R return 0; 8896ca848eSSricharan R } 8996ca848eSSricharan R 9096ca848eSSricharan R static void crossbar_domain_unmap(struct irq_domain *d, unsigned int irq) 9196ca848eSSricharan R { 9296ca848eSSricharan R irq_hw_number_t hw = irq_get_irq_data(irq)->hwirq; 9396ca848eSSricharan R 94a35057d1SNishanth Menon if (hw > GIC_IRQ_START) { 9596ca848eSSricharan R cb->irq_map[hw - GIC_IRQ_START] = IRQ_FREE; 96a35057d1SNishanth Menon cb->write(hw - GIC_IRQ_START, cb->safe_map); 97a35057d1SNishanth Menon } 9896ca848eSSricharan R } 9996ca848eSSricharan R 10096ca848eSSricharan R static int crossbar_domain_xlate(struct irq_domain *d, 10196ca848eSSricharan R struct device_node *controller, 10296ca848eSSricharan R const u32 *intspec, unsigned int intsize, 10396ca848eSSricharan R unsigned long *out_hwirq, 10496ca848eSSricharan R unsigned int *out_type) 10596ca848eSSricharan R { 106d4922a95SNishanth Menon int ret; 10796ca848eSSricharan R 1086f16fc87SNishanth Menon ret = get_prev_map_irq(intspec[1]); 109d4922a95SNishanth Menon if (ret >= 0) 1106f16fc87SNishanth Menon goto found; 1116f16fc87SNishanth Menon 11296ca848eSSricharan R ret = allocate_free_irq(intspec[1]); 11396ca848eSSricharan R 114d4922a95SNishanth Menon if (ret < 0) 11596ca848eSSricharan R return ret; 11696ca848eSSricharan R 1176f16fc87SNishanth Menon found: 11896ca848eSSricharan R *out_hwirq = ret + GIC_IRQ_START; 11996ca848eSSricharan R return 0; 12096ca848eSSricharan R } 12196ca848eSSricharan R 122*4dbf45e3SNishanth Menon static const struct irq_domain_ops routable_irq_domain_ops = { 12396ca848eSSricharan R .map = crossbar_domain_map, 12496ca848eSSricharan R .unmap = crossbar_domain_unmap, 12596ca848eSSricharan R .xlate = crossbar_domain_xlate 12696ca848eSSricharan R }; 12796ca848eSSricharan R 12896ca848eSSricharan R static int __init crossbar_of_init(struct device_node *node) 12996ca848eSSricharan R { 13096ca848eSSricharan R int i, size, max, reserved = 0, entry; 13196ca848eSSricharan R const __be32 *irqsr; 13296ca848eSSricharan R 1333894e9e8SDan Carpenter cb = kzalloc(sizeof(*cb), GFP_KERNEL); 13496ca848eSSricharan R 13596ca848eSSricharan R if (!cb) 13696ca848eSSricharan R return -ENOMEM; 13796ca848eSSricharan R 13896ca848eSSricharan R cb->crossbar_base = of_iomap(node, 0); 13996ca848eSSricharan R if (!cb->crossbar_base) 14096ca848eSSricharan R goto err1; 14196ca848eSSricharan R 14296ca848eSSricharan R of_property_read_u32(node, "ti,max-irqs", &max); 143*4dbf45e3SNishanth Menon cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL); 14496ca848eSSricharan R if (!cb->irq_map) 14596ca848eSSricharan R goto err2; 14696ca848eSSricharan R 14796ca848eSSricharan R cb->int_max = max; 14896ca848eSSricharan R 14996ca848eSSricharan R for (i = 0; i < max; i++) 15096ca848eSSricharan R cb->irq_map[i] = IRQ_FREE; 15196ca848eSSricharan R 15296ca848eSSricharan R /* Get and mark reserved irqs */ 15396ca848eSSricharan R irqsr = of_get_property(node, "ti,irqs-reserved", &size); 15496ca848eSSricharan R if (irqsr) { 15596ca848eSSricharan R size /= sizeof(__be32); 15696ca848eSSricharan R 15796ca848eSSricharan R for (i = 0; i < size; i++) { 15896ca848eSSricharan R of_property_read_u32_index(node, 15996ca848eSSricharan R "ti,irqs-reserved", 16096ca848eSSricharan R i, &entry); 16196ca848eSSricharan R if (entry > max) { 16296ca848eSSricharan R pr_err("Invalid reserved entry\n"); 16396ca848eSSricharan R goto err3; 16496ca848eSSricharan R } 1651d50d2ceSNishanth Menon cb->irq_map[entry] = IRQ_RESERVED; 16696ca848eSSricharan R } 16796ca848eSSricharan R } 16896ca848eSSricharan R 16964e0f8baSNishanth Menon /* Skip irqs hardwired to bypass the crossbar */ 17064e0f8baSNishanth Menon irqsr = of_get_property(node, "ti,irqs-skip", &size); 17164e0f8baSNishanth Menon if (irqsr) { 17264e0f8baSNishanth Menon size /= sizeof(__be32); 17364e0f8baSNishanth Menon 17464e0f8baSNishanth Menon for (i = 0; i < size; i++) { 17564e0f8baSNishanth Menon of_property_read_u32_index(node, 17664e0f8baSNishanth Menon "ti,irqs-skip", 17764e0f8baSNishanth Menon i, &entry); 17864e0f8baSNishanth Menon if (entry > max) { 17964e0f8baSNishanth Menon pr_err("Invalid skip entry\n"); 18064e0f8baSNishanth Menon ret = -EINVAL; 18164e0f8baSNishanth Menon goto err3; 18264e0f8baSNishanth Menon } 18364e0f8baSNishanth Menon cb->irq_map[entry] = IRQ_SKIP; 18464e0f8baSNishanth Menon } 18564e0f8baSNishanth Menon } 18664e0f8baSNishanth Menon 18764e0f8baSNishanth Menon 188*4dbf45e3SNishanth Menon cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL); 18996ca848eSSricharan R if (!cb->register_offsets) 19096ca848eSSricharan R goto err3; 19196ca848eSSricharan R 19296ca848eSSricharan R of_property_read_u32(node, "ti,reg-size", &size); 19396ca848eSSricharan R 19496ca848eSSricharan R switch (size) { 19596ca848eSSricharan R case 1: 19696ca848eSSricharan R cb->write = crossbar_writeb; 19796ca848eSSricharan R break; 19896ca848eSSricharan R case 2: 19996ca848eSSricharan R cb->write = crossbar_writew; 20096ca848eSSricharan R break; 20196ca848eSSricharan R case 4: 20296ca848eSSricharan R cb->write = crossbar_writel; 20396ca848eSSricharan R break; 20496ca848eSSricharan R default: 20596ca848eSSricharan R pr_err("Invalid reg-size property\n"); 20696ca848eSSricharan R goto err4; 20796ca848eSSricharan R break; 20896ca848eSSricharan R } 20996ca848eSSricharan R 21096ca848eSSricharan R /* 21196ca848eSSricharan R * Register offsets are not linear because of the 21296ca848eSSricharan R * reserved irqs. so find and store the offsets once. 21396ca848eSSricharan R */ 21496ca848eSSricharan R for (i = 0; i < max; i++) { 2151d50d2ceSNishanth Menon if (cb->irq_map[i] == IRQ_RESERVED) 21696ca848eSSricharan R continue; 21796ca848eSSricharan R 21896ca848eSSricharan R cb->register_offsets[i] = reserved; 21996ca848eSSricharan R reserved += size; 22096ca848eSSricharan R } 22196ca848eSSricharan R 222a35057d1SNishanth Menon of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map); 223a35057d1SNishanth Menon 224a35057d1SNishanth Menon /* Initialize the crossbar with safe map to start with */ 225a35057d1SNishanth Menon for (i = 0; i < max; i++) { 226a35057d1SNishanth Menon if (cb->irq_map[i] == IRQ_RESERVED || 227a35057d1SNishanth Menon cb->irq_map[i] == IRQ_SKIP) 228a35057d1SNishanth Menon continue; 229a35057d1SNishanth Menon 230a35057d1SNishanth Menon cb->write(i, cb->safe_map); 231a35057d1SNishanth Menon } 232a35057d1SNishanth Menon 23396ca848eSSricharan R register_routable_domain_ops(&routable_irq_domain_ops); 23496ca848eSSricharan R return 0; 23596ca848eSSricharan R 23696ca848eSSricharan R err4: 23796ca848eSSricharan R kfree(cb->register_offsets); 23896ca848eSSricharan R err3: 23996ca848eSSricharan R kfree(cb->irq_map); 24096ca848eSSricharan R err2: 24196ca848eSSricharan R iounmap(cb->crossbar_base); 24296ca848eSSricharan R err1: 24396ca848eSSricharan R kfree(cb); 24496ca848eSSricharan R return -ENOMEM; 24596ca848eSSricharan R } 24696ca848eSSricharan R 24796ca848eSSricharan R static const struct of_device_id crossbar_match[] __initconst = { 24896ca848eSSricharan R { .compatible = "ti,irq-crossbar" }, 24996ca848eSSricharan R {} 25096ca848eSSricharan R }; 25196ca848eSSricharan R 25296ca848eSSricharan R int __init irqcrossbar_init(void) 25396ca848eSSricharan R { 25496ca848eSSricharan R struct device_node *np; 25596ca848eSSricharan R np = of_find_matching_node(NULL, crossbar_match); 25696ca848eSSricharan R if (!np) 25796ca848eSSricharan R return -ENODEV; 25896ca848eSSricharan R 25996ca848eSSricharan R crossbar_of_init(np); 26096ca848eSSricharan R return 0; 26196ca848eSSricharan R } 262