144e08e70SPaul Burton /* 244e08e70SPaul Burton * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de> 344e08e70SPaul Burton * JZ4740 platform IRQ support 444e08e70SPaul Burton * 544e08e70SPaul Burton * This program is free software; you can redistribute it and/or modify it 644e08e70SPaul Burton * under the terms of the GNU General Public License as published by the 744e08e70SPaul Burton * Free Software Foundation; either version 2 of the License, or (at your 844e08e70SPaul Burton * option) any later version. 944e08e70SPaul Burton * 1044e08e70SPaul Burton * You should have received a copy of the GNU General Public License along 1144e08e70SPaul Burton * with this program; if not, write to the Free Software Foundation, Inc., 1244e08e70SPaul Burton * 675 Mass Ave, Cambridge, MA 02139, USA. 1344e08e70SPaul Burton * 1444e08e70SPaul Burton */ 1544e08e70SPaul Burton 1644e08e70SPaul Burton #include <linux/errno.h> 1744e08e70SPaul Burton #include <linux/init.h> 1844e08e70SPaul Burton #include <linux/types.h> 1944e08e70SPaul Burton #include <linux/interrupt.h> 2044e08e70SPaul Burton #include <linux/ioport.h> 2141a83e06SJoel Porquet #include <linux/irqchip.h> 2244e08e70SPaul Burton #include <linux/irqchip/ingenic.h> 2344e08e70SPaul Burton #include <linux/of_address.h> 2444e08e70SPaul Burton #include <linux/of_irq.h> 2544e08e70SPaul Burton #include <linux/timex.h> 2644e08e70SPaul Burton #include <linux/slab.h> 2744e08e70SPaul Burton #include <linux/delay.h> 2844e08e70SPaul Burton 2944e08e70SPaul Burton #include <asm/io.h> 3044e08e70SPaul Burton #include <asm/mach-jz4740/irq.h> 3144e08e70SPaul Burton 3244e08e70SPaul Burton struct ingenic_intc_data { 3344e08e70SPaul Burton void __iomem *base; 3444e08e70SPaul Burton unsigned num_chips; 3544e08e70SPaul Burton }; 3644e08e70SPaul Burton 3744e08e70SPaul Burton #define JZ_REG_INTC_STATUS 0x00 3844e08e70SPaul Burton #define JZ_REG_INTC_MASK 0x04 3944e08e70SPaul Burton #define JZ_REG_INTC_SET_MASK 0x08 4044e08e70SPaul Burton #define JZ_REG_INTC_CLEAR_MASK 0x0c 4144e08e70SPaul Burton #define JZ_REG_INTC_PENDING 0x10 4244e08e70SPaul Burton #define CHIP_SIZE 0x20 4344e08e70SPaul Burton 4444e08e70SPaul Burton static irqreturn_t intc_cascade(int irq, void *data) 4544e08e70SPaul Burton { 4644e08e70SPaul Burton struct ingenic_intc_data *intc = irq_get_handler_data(irq); 4744e08e70SPaul Burton uint32_t irq_reg; 4844e08e70SPaul Burton unsigned i; 4944e08e70SPaul Burton 5044e08e70SPaul Burton for (i = 0; i < intc->num_chips; i++) { 5144e08e70SPaul Burton irq_reg = readl(intc->base + (i * CHIP_SIZE) + 5244e08e70SPaul Burton JZ_REG_INTC_PENDING); 5344e08e70SPaul Burton if (!irq_reg) 5444e08e70SPaul Burton continue; 5544e08e70SPaul Burton 5644e08e70SPaul Burton generic_handle_irq(__fls(irq_reg) + (i * 32) + JZ4740_IRQ_BASE); 5744e08e70SPaul Burton } 5844e08e70SPaul Burton 5944e08e70SPaul Burton return IRQ_HANDLED; 6044e08e70SPaul Burton } 6144e08e70SPaul Burton 6244e08e70SPaul Burton static void intc_irq_set_mask(struct irq_chip_generic *gc, uint32_t mask) 6344e08e70SPaul Burton { 6444e08e70SPaul Burton struct irq_chip_regs *regs = &gc->chip_types->regs; 6544e08e70SPaul Burton 6644e08e70SPaul Burton writel(mask, gc->reg_base + regs->enable); 6744e08e70SPaul Burton writel(~mask, gc->reg_base + regs->disable); 6844e08e70SPaul Burton } 6944e08e70SPaul Burton 7044e08e70SPaul Burton void ingenic_intc_irq_suspend(struct irq_data *data) 7144e08e70SPaul Burton { 7244e08e70SPaul Burton struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data); 7344e08e70SPaul Burton intc_irq_set_mask(gc, gc->wake_active); 7444e08e70SPaul Burton } 7544e08e70SPaul Burton 7644e08e70SPaul Burton void ingenic_intc_irq_resume(struct irq_data *data) 7744e08e70SPaul Burton { 7844e08e70SPaul Burton struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data); 7944e08e70SPaul Burton intc_irq_set_mask(gc, gc->mask_cache); 8044e08e70SPaul Burton } 8144e08e70SPaul Burton 8244e08e70SPaul Burton static struct irqaction intc_cascade_action = { 8344e08e70SPaul Burton .handler = intc_cascade, 8444e08e70SPaul Burton .name = "SoC intc cascade interrupt", 8544e08e70SPaul Burton }; 8644e08e70SPaul Burton 8744e08e70SPaul Burton static int __init ingenic_intc_of_init(struct device_node *node, 8844e08e70SPaul Burton unsigned num_chips) 8944e08e70SPaul Burton { 9044e08e70SPaul Burton struct ingenic_intc_data *intc; 9144e08e70SPaul Burton struct irq_chip_generic *gc; 9244e08e70SPaul Burton struct irq_chip_type *ct; 9344e08e70SPaul Burton struct irq_domain *domain; 9444e08e70SPaul Burton int parent_irq, err = 0; 9544e08e70SPaul Burton unsigned i; 9644e08e70SPaul Burton 9744e08e70SPaul Burton intc = kzalloc(sizeof(*intc), GFP_KERNEL); 9844e08e70SPaul Burton if (!intc) { 9944e08e70SPaul Burton err = -ENOMEM; 10044e08e70SPaul Burton goto out_err; 10144e08e70SPaul Burton } 10244e08e70SPaul Burton 10344e08e70SPaul Burton parent_irq = irq_of_parse_and_map(node, 0); 10444e08e70SPaul Burton if (!parent_irq) { 10544e08e70SPaul Burton err = -EINVAL; 10644e08e70SPaul Burton goto out_free; 10744e08e70SPaul Burton } 10844e08e70SPaul Burton 10944e08e70SPaul Burton err = irq_set_handler_data(parent_irq, intc); 11044e08e70SPaul Burton if (err) 11144e08e70SPaul Burton goto out_unmap_irq; 11244e08e70SPaul Burton 11344e08e70SPaul Burton intc->num_chips = num_chips; 11444e08e70SPaul Burton intc->base = of_iomap(node, 0); 11544e08e70SPaul Burton if (!intc->base) { 11644e08e70SPaul Burton err = -ENODEV; 11744e08e70SPaul Burton goto out_unmap_irq; 11844e08e70SPaul Burton } 11944e08e70SPaul Burton 12044e08e70SPaul Burton for (i = 0; i < num_chips; i++) { 12144e08e70SPaul Burton /* Mask all irqs */ 12244e08e70SPaul Burton writel(0xffffffff, intc->base + (i * CHIP_SIZE) + 12344e08e70SPaul Burton JZ_REG_INTC_SET_MASK); 12444e08e70SPaul Burton 12544e08e70SPaul Burton gc = irq_alloc_generic_chip("INTC", 1, 12644e08e70SPaul Burton JZ4740_IRQ_BASE + (i * 32), 12744e08e70SPaul Burton intc->base + (i * CHIP_SIZE), 12844e08e70SPaul Burton handle_level_irq); 12944e08e70SPaul Burton 13044e08e70SPaul Burton gc->wake_enabled = IRQ_MSK(32); 13144e08e70SPaul Burton 13244e08e70SPaul Burton ct = gc->chip_types; 13344e08e70SPaul Burton ct->regs.enable = JZ_REG_INTC_CLEAR_MASK; 13444e08e70SPaul Burton ct->regs.disable = JZ_REG_INTC_SET_MASK; 13544e08e70SPaul Burton ct->chip.irq_unmask = irq_gc_unmask_enable_reg; 13644e08e70SPaul Burton ct->chip.irq_mask = irq_gc_mask_disable_reg; 13744e08e70SPaul Burton ct->chip.irq_mask_ack = irq_gc_mask_disable_reg; 13844e08e70SPaul Burton ct->chip.irq_set_wake = irq_gc_set_wake; 13944e08e70SPaul Burton ct->chip.irq_suspend = ingenic_intc_irq_suspend; 14044e08e70SPaul Burton ct->chip.irq_resume = ingenic_intc_irq_resume; 14144e08e70SPaul Burton 14244e08e70SPaul Burton irq_setup_generic_chip(gc, IRQ_MSK(32), 0, 0, 14344e08e70SPaul Burton IRQ_NOPROBE | IRQ_LEVEL); 14444e08e70SPaul Burton } 14544e08e70SPaul Burton 14644e08e70SPaul Burton domain = irq_domain_add_legacy(node, num_chips * 32, JZ4740_IRQ_BASE, 0, 14744e08e70SPaul Burton &irq_domain_simple_ops, NULL); 14844e08e70SPaul Burton if (!domain) 14944e08e70SPaul Burton pr_warn("unable to register IRQ domain\n"); 15044e08e70SPaul Burton 15144e08e70SPaul Burton setup_irq(parent_irq, &intc_cascade_action); 15244e08e70SPaul Burton return 0; 15344e08e70SPaul Burton 15444e08e70SPaul Burton out_unmap_irq: 15544e08e70SPaul Burton irq_dispose_mapping(parent_irq); 15644e08e70SPaul Burton out_free: 15744e08e70SPaul Burton kfree(intc); 15844e08e70SPaul Burton out_err: 15944e08e70SPaul Burton return err; 16044e08e70SPaul Burton } 16144e08e70SPaul Burton 16244e08e70SPaul Burton static int __init intc_1chip_of_init(struct device_node *node, 16344e08e70SPaul Burton struct device_node *parent) 16444e08e70SPaul Burton { 16544e08e70SPaul Burton return ingenic_intc_of_init(node, 1); 16644e08e70SPaul Burton } 16744e08e70SPaul Burton IRQCHIP_DECLARE(jz4740_intc, "ingenic,jz4740-intc", intc_1chip_of_init); 168*1047557cSPaul Cercueil IRQCHIP_DECLARE(jz4725b_intc, "ingenic,jz4725b-intc", intc_1chip_of_init); 16944e08e70SPaul Burton 17044e08e70SPaul Burton static int __init intc_2chip_of_init(struct device_node *node, 17144e08e70SPaul Burton struct device_node *parent) 17244e08e70SPaul Burton { 17344e08e70SPaul Burton return ingenic_intc_of_init(node, 2); 17444e08e70SPaul Burton } 17544e08e70SPaul Burton IRQCHIP_DECLARE(jz4770_intc, "ingenic,jz4770-intc", intc_2chip_of_init); 17644e08e70SPaul Burton IRQCHIP_DECLARE(jz4775_intc, "ingenic,jz4775-intc", intc_2chip_of_init); 17744e08e70SPaul Burton IRQCHIP_DECLARE(jz4780_intc, "ingenic,jz4780-intc", intc_2chip_of_init); 178