1b06eb017SChristian Ruppert /* 2b06eb017SChristian Ruppert * Abilis Systems interrupt controller driver 3b06eb017SChristian Ruppert * 4b06eb017SChristian Ruppert * Copyright (C) Abilis Systems 2012 5b06eb017SChristian Ruppert * 6b06eb017SChristian Ruppert * Author: Christian Ruppert <christian.ruppert@abilis.com> 7b06eb017SChristian Ruppert * 8b06eb017SChristian Ruppert * This program is free software; you can redistribute it and/or modify 9b06eb017SChristian Ruppert * it under the terms of the GNU General Public License version 2 as 10b06eb017SChristian Ruppert * published by the Free Software Foundation. 11b06eb017SChristian Ruppert * 12b06eb017SChristian Ruppert * This program is distributed in the hope that it will be useful, 13b06eb017SChristian Ruppert * but WITHOUT ANY WARRANTY; without even the implied warranty of 14b06eb017SChristian Ruppert * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15b06eb017SChristian Ruppert * GNU General Public License for more details. 16b06eb017SChristian Ruppert * 17b06eb017SChristian Ruppert * You should have received a copy of the GNU General Public License 18b06eb017SChristian Ruppert * along with this program; if not, write to the Free Software 19b06eb017SChristian Ruppert * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20b06eb017SChristian Ruppert */ 21b06eb017SChristian Ruppert 22b06eb017SChristian Ruppert #include <linux/interrupt.h> 23b06eb017SChristian Ruppert #include <linux/irqdomain.h> 24b06eb017SChristian Ruppert #include <linux/irq.h> 2541a83e06SJoel Porquet #include <linux/irqchip.h> 26b06eb017SChristian Ruppert #include <linux/of_irq.h> 27b06eb017SChristian Ruppert #include <linux/of_address.h> 28b06eb017SChristian Ruppert #include <linux/of_platform.h> 29b06eb017SChristian Ruppert #include <linux/io.h> 30b06eb017SChristian Ruppert #include <linux/slab.h> 31b06eb017SChristian Ruppert #include <linux/bitops.h> 32b06eb017SChristian Ruppert 33b06eb017SChristian Ruppert #define AB_IRQCTL_INT_ENABLE 0x00 34b06eb017SChristian Ruppert #define AB_IRQCTL_INT_STATUS 0x04 35b06eb017SChristian Ruppert #define AB_IRQCTL_SRC_MODE 0x08 36b06eb017SChristian Ruppert #define AB_IRQCTL_SRC_POLARITY 0x0C 37b06eb017SChristian Ruppert #define AB_IRQCTL_INT_MODE 0x10 38b06eb017SChristian Ruppert #define AB_IRQCTL_INT_POLARITY 0x14 39b06eb017SChristian Ruppert #define AB_IRQCTL_INT_FORCE 0x18 40b06eb017SChristian Ruppert 41b06eb017SChristian Ruppert #define AB_IRQCTL_MAXIRQ 32 42b06eb017SChristian Ruppert 43b06eb017SChristian Ruppert static inline void ab_irqctl_writereg(struct irq_chip_generic *gc, u32 reg, 44b06eb017SChristian Ruppert u32 val) 45b06eb017SChristian Ruppert { 46332fd7c4SKevin Cernekee irq_reg_writel(gc, val, reg); 47b06eb017SChristian Ruppert } 48b06eb017SChristian Ruppert 49b06eb017SChristian Ruppert static inline u32 ab_irqctl_readreg(struct irq_chip_generic *gc, u32 reg) 50b06eb017SChristian Ruppert { 51332fd7c4SKevin Cernekee return irq_reg_readl(gc, reg); 52b06eb017SChristian Ruppert } 53b06eb017SChristian Ruppert 54b06eb017SChristian Ruppert static int tb10x_irq_set_type(struct irq_data *data, unsigned int flow_type) 55b06eb017SChristian Ruppert { 56b06eb017SChristian Ruppert struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data); 57b06eb017SChristian Ruppert uint32_t im, mod, pol; 58b06eb017SChristian Ruppert 59b06eb017SChristian Ruppert im = data->mask; 60b06eb017SChristian Ruppert 61b06eb017SChristian Ruppert irq_gc_lock(gc); 62b06eb017SChristian Ruppert 63b06eb017SChristian Ruppert mod = ab_irqctl_readreg(gc, AB_IRQCTL_SRC_MODE) | im; 64b06eb017SChristian Ruppert pol = ab_irqctl_readreg(gc, AB_IRQCTL_SRC_POLARITY) | im; 65b06eb017SChristian Ruppert 66b06eb017SChristian Ruppert switch (flow_type & IRQF_TRIGGER_MASK) { 67b06eb017SChristian Ruppert case IRQ_TYPE_EDGE_FALLING: 68b06eb017SChristian Ruppert pol ^= im; 69b06eb017SChristian Ruppert break; 70b06eb017SChristian Ruppert case IRQ_TYPE_LEVEL_HIGH: 71b06eb017SChristian Ruppert mod ^= im; 72b06eb017SChristian Ruppert break; 73b06eb017SChristian Ruppert case IRQ_TYPE_NONE: 74b06eb017SChristian Ruppert flow_type = IRQ_TYPE_LEVEL_LOW; 75b06eb017SChristian Ruppert case IRQ_TYPE_LEVEL_LOW: 76b06eb017SChristian Ruppert mod ^= im; 77b06eb017SChristian Ruppert pol ^= im; 78b06eb017SChristian Ruppert break; 79b06eb017SChristian Ruppert case IRQ_TYPE_EDGE_RISING: 80b06eb017SChristian Ruppert break; 81b06eb017SChristian Ruppert default: 82b06eb017SChristian Ruppert irq_gc_unlock(gc); 83b06eb017SChristian Ruppert pr_err("%s: Cannot assign multiple trigger modes to IRQ %d.\n", 84b06eb017SChristian Ruppert __func__, data->irq); 85b06eb017SChristian Ruppert return -EBADR; 86b06eb017SChristian Ruppert } 87b06eb017SChristian Ruppert 88b06eb017SChristian Ruppert irqd_set_trigger_type(data, flow_type); 89b06eb017SChristian Ruppert irq_setup_alt_chip(data, flow_type); 90b06eb017SChristian Ruppert 91b06eb017SChristian Ruppert ab_irqctl_writereg(gc, AB_IRQCTL_SRC_MODE, mod); 92b06eb017SChristian Ruppert ab_irqctl_writereg(gc, AB_IRQCTL_SRC_POLARITY, pol); 93b06eb017SChristian Ruppert ab_irqctl_writereg(gc, AB_IRQCTL_INT_STATUS, im); 94b06eb017SChristian Ruppert 95b06eb017SChristian Ruppert irq_gc_unlock(gc); 96b06eb017SChristian Ruppert 97b06eb017SChristian Ruppert return IRQ_SET_MASK_OK; 98b06eb017SChristian Ruppert } 99b06eb017SChristian Ruppert 100b06eb017SChristian Ruppert static void tb10x_irq_cascade(unsigned int irq, struct irq_desc *desc) 101b06eb017SChristian Ruppert { 102b06eb017SChristian Ruppert struct irq_domain *domain = irq_desc_get_handler_data(desc); 103b06eb017SChristian Ruppert 104b06eb017SChristian Ruppert generic_handle_irq(irq_find_mapping(domain, irq)); 105b06eb017SChristian Ruppert } 106b06eb017SChristian Ruppert 107b06eb017SChristian Ruppert static int __init of_tb10x_init_irq(struct device_node *ictl, 108b06eb017SChristian Ruppert struct device_node *parent) 109b06eb017SChristian Ruppert { 110b06eb017SChristian Ruppert int i, ret, nrirqs = of_irq_count(ictl); 111b06eb017SChristian Ruppert struct resource mem; 112b06eb017SChristian Ruppert struct irq_chip_generic *gc; 113b06eb017SChristian Ruppert struct irq_domain *domain; 114b06eb017SChristian Ruppert void __iomem *reg_base; 115b06eb017SChristian Ruppert 116b06eb017SChristian Ruppert if (of_address_to_resource(ictl, 0, &mem)) { 117b06eb017SChristian Ruppert pr_err("%s: No registers declared in DeviceTree.\n", 118b06eb017SChristian Ruppert ictl->name); 119b06eb017SChristian Ruppert return -EINVAL; 120b06eb017SChristian Ruppert } 121b06eb017SChristian Ruppert 122b06eb017SChristian Ruppert if (!request_mem_region(mem.start, resource_size(&mem), 123b06eb017SChristian Ruppert ictl->name)) { 124b06eb017SChristian Ruppert pr_err("%s: Request mem region failed.\n", ictl->name); 125b06eb017SChristian Ruppert return -EBUSY; 126b06eb017SChristian Ruppert } 127b06eb017SChristian Ruppert 128b06eb017SChristian Ruppert reg_base = ioremap(mem.start, resource_size(&mem)); 129b06eb017SChristian Ruppert if (!reg_base) { 130b06eb017SChristian Ruppert ret = -EBUSY; 131b06eb017SChristian Ruppert pr_err("%s: ioremap failed.\n", ictl->name); 132b06eb017SChristian Ruppert goto ioremap_fail; 133b06eb017SChristian Ruppert } 134b06eb017SChristian Ruppert 135b06eb017SChristian Ruppert domain = irq_domain_add_linear(ictl, AB_IRQCTL_MAXIRQ, 136b06eb017SChristian Ruppert &irq_generic_chip_ops, NULL); 137b06eb017SChristian Ruppert if (!domain) { 138b06eb017SChristian Ruppert ret = -ENOMEM; 139b06eb017SChristian Ruppert pr_err("%s: Could not register interrupt domain.\n", 140b06eb017SChristian Ruppert ictl->name); 141b06eb017SChristian Ruppert goto irq_domain_add_fail; 142b06eb017SChristian Ruppert } 143b06eb017SChristian Ruppert 144b06eb017SChristian Ruppert ret = irq_alloc_domain_generic_chips(domain, AB_IRQCTL_MAXIRQ, 145b06eb017SChristian Ruppert 2, ictl->name, handle_level_irq, 146b06eb017SChristian Ruppert IRQ_NOREQUEST, IRQ_NOPROBE, 147b06eb017SChristian Ruppert IRQ_GC_INIT_MASK_CACHE); 148b06eb017SChristian Ruppert if (ret) { 149b06eb017SChristian Ruppert pr_err("%s: Could not allocate generic interrupt chip.\n", 150b06eb017SChristian Ruppert ictl->name); 151b06eb017SChristian Ruppert goto gc_alloc_fail; 152b06eb017SChristian Ruppert } 153b06eb017SChristian Ruppert 154b06eb017SChristian Ruppert gc = domain->gc->gc[0]; 155b06eb017SChristian Ruppert gc->reg_base = reg_base; 156b06eb017SChristian Ruppert 157b06eb017SChristian Ruppert gc->chip_types[0].type = IRQ_TYPE_LEVEL_MASK; 158b06eb017SChristian Ruppert gc->chip_types[0].chip.irq_mask = irq_gc_mask_clr_bit; 159b06eb017SChristian Ruppert gc->chip_types[0].chip.irq_unmask = irq_gc_mask_set_bit; 160b06eb017SChristian Ruppert gc->chip_types[0].chip.irq_set_type = tb10x_irq_set_type; 161b06eb017SChristian Ruppert gc->chip_types[0].regs.mask = AB_IRQCTL_INT_ENABLE; 162b06eb017SChristian Ruppert 163b06eb017SChristian Ruppert gc->chip_types[1].type = IRQ_TYPE_EDGE_BOTH; 164b06eb017SChristian Ruppert gc->chip_types[1].chip.name = gc->chip_types[0].chip.name; 165b06eb017SChristian Ruppert gc->chip_types[1].chip.irq_ack = irq_gc_ack_set_bit; 166b06eb017SChristian Ruppert gc->chip_types[1].chip.irq_mask = irq_gc_mask_clr_bit; 167b06eb017SChristian Ruppert gc->chip_types[1].chip.irq_unmask = irq_gc_mask_set_bit; 168b06eb017SChristian Ruppert gc->chip_types[1].chip.irq_set_type = tb10x_irq_set_type; 169b06eb017SChristian Ruppert gc->chip_types[1].regs.ack = AB_IRQCTL_INT_STATUS; 170b06eb017SChristian Ruppert gc->chip_types[1].regs.mask = AB_IRQCTL_INT_ENABLE; 171b06eb017SChristian Ruppert gc->chip_types[1].handler = handle_edge_irq; 172b06eb017SChristian Ruppert 173b06eb017SChristian Ruppert for (i = 0; i < nrirqs; i++) { 174b06eb017SChristian Ruppert unsigned int irq = irq_of_parse_and_map(ictl, i); 175b06eb017SChristian Ruppert 176*22890b0dSThomas Gleixner irq_set_chained_handler_and_data(irq, tb10x_irq_cascade, 177*22890b0dSThomas Gleixner domain); 178b06eb017SChristian Ruppert } 179b06eb017SChristian Ruppert 180b06eb017SChristian Ruppert ab_irqctl_writereg(gc, AB_IRQCTL_INT_ENABLE, 0); 181b06eb017SChristian Ruppert ab_irqctl_writereg(gc, AB_IRQCTL_INT_MODE, 0); 182b06eb017SChristian Ruppert ab_irqctl_writereg(gc, AB_IRQCTL_INT_POLARITY, 0); 183b06eb017SChristian Ruppert ab_irqctl_writereg(gc, AB_IRQCTL_INT_STATUS, ~0UL); 184b06eb017SChristian Ruppert 185b06eb017SChristian Ruppert return 0; 186b06eb017SChristian Ruppert 187b06eb017SChristian Ruppert gc_alloc_fail: 188b06eb017SChristian Ruppert irq_domain_remove(domain); 189b06eb017SChristian Ruppert irq_domain_add_fail: 190b06eb017SChristian Ruppert iounmap(reg_base); 191b06eb017SChristian Ruppert ioremap_fail: 192b06eb017SChristian Ruppert release_mem_region(mem.start, resource_size(&mem)); 193b06eb017SChristian Ruppert return ret; 194b06eb017SChristian Ruppert } 195b06eb017SChristian Ruppert IRQCHIP_DECLARE(tb10x_intc, "abilis,tb10x-ictl", of_tb10x_init_irq); 196