blob: ea36bb00be80bfd8e578c5b739b99b55c3e89710 [file] [log] [blame]
Thomas Gleixnera912e802019-05-27 08:55:00 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Lars-Peter Clausen98698482010-07-17 11:08:43 +00002/*
3 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
Zhou Yanjieb8b01452019-10-02 19:25:25 +08004 * Ingenic XBurst platform IRQ support
Lars-Peter Clausen98698482010-07-17 11:08:43 +00005 */
6
7#include <linux/errno.h>
8#include <linux/init.h>
9#include <linux/types.h>
10#include <linux/interrupt.h>
11#include <linux/ioport.h>
Joel Porquet41a83e062015-07-07 17:11:46 -040012#include <linux/irqchip.h>
Paul Burton3aa94592015-05-24 16:11:28 +010013#include <linux/of_address.h>
Paul Burtonadbdce72015-05-24 16:11:21 +010014#include <linux/of_irq.h>
Lars-Peter Clausen98698482010-07-17 11:08:43 +000015#include <linux/timex.h>
16#include <linux/slab.h>
17#include <linux/delay.h>
18
Lars-Peter Clausen98698482010-07-17 11:08:43 +000019#include <asm/io.h>
Brian Norris942e22d2014-12-17 18:39:01 -080020
Paul Burtonfe778ec2015-05-24 16:11:25 +010021struct ingenic_intc_data {
22 void __iomem *base;
Paul Cercueil208caad2019-10-02 19:25:23 +080023 struct irq_domain *domain;
Paul Burton943d69c2015-05-24 16:11:26 +010024 unsigned num_chips;
Paul Burtonfe778ec2015-05-24 16:11:25 +010025};
Lars-Peter Clausen98698482010-07-17 11:08:43 +000026
27#define JZ_REG_INTC_STATUS 0x00
28#define JZ_REG_INTC_MASK 0x04
29#define JZ_REG_INTC_SET_MASK 0x08
30#define JZ_REG_INTC_CLEAR_MASK 0x0c
31#define JZ_REG_INTC_PENDING 0x10
Paul Burton943d69c2015-05-24 16:11:26 +010032#define CHIP_SIZE 0x20
Lars-Peter Clausen98698482010-07-17 11:08:43 +000033
Paul Burton2da01882015-05-24 16:11:29 +010034static irqreturn_t intc_cascade(int irq, void *data)
Lars-Peter Clausen98698482010-07-17 11:08:43 +000035{
Paul Burtonfe778ec2015-05-24 16:11:25 +010036 struct ingenic_intc_data *intc = irq_get_handler_data(irq);
Paul Cercueil208caad2019-10-02 19:25:23 +080037 struct irq_domain *domain = intc->domain;
Paul Cercueil8bc74642019-10-02 19:25:24 +080038 struct irq_chip_generic *gc;
Zhou Yanjieb8b01452019-10-02 19:25:25 +080039 uint32_t pending;
Paul Burton943d69c2015-05-24 16:11:26 +010040 unsigned i;
Lars-Peter Clausen98698482010-07-17 11:08:43 +000041
Paul Burton943d69c2015-05-24 16:11:26 +010042 for (i = 0; i < intc->num_chips; i++) {
Paul Cercueil8bc74642019-10-02 19:25:24 +080043 gc = irq_get_domain_generic_chip(domain, i * 32);
44
Zhou Yanjieb8b01452019-10-02 19:25:25 +080045 pending = irq_reg_readl(gc, JZ_REG_INTC_PENDING);
46 if (!pending)
Paul Burton943d69c2015-05-24 16:11:26 +010047 continue;
Lars-Peter Clausen98698482010-07-17 11:08:43 +000048
Zhou Yanjieb8b01452019-10-02 19:25:25 +080049 while (pending) {
50 int bit = __fls(pending);
51
Paul Cercueil1fd224e2020-01-13 13:33:29 -030052 irq = irq_linear_revmap(domain, bit + (i * 32));
Zhou Yanjieb8b01452019-10-02 19:25:25 +080053 generic_handle_irq(irq);
54 pending &= ~BIT(bit);
55 }
Paul Burton943d69c2015-05-24 16:11:26 +010056 }
Lars-Peter Clausen98698482010-07-17 11:08:43 +000057
58 return IRQ_HANDLED;
59}
60
Paul Burton943d69c2015-05-24 16:11:26 +010061static int __init ingenic_intc_of_init(struct device_node *node,
62 unsigned num_chips)
Lars-Peter Clausen98698482010-07-17 11:08:43 +000063{
Paul Burtonfe778ec2015-05-24 16:11:25 +010064 struct ingenic_intc_data *intc;
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +020065 struct irq_chip_generic *gc;
66 struct irq_chip_type *ct;
Paul Burton638c8852015-05-24 16:11:23 +010067 struct irq_domain *domain;
Paul Burtonfe778ec2015-05-24 16:11:25 +010068 int parent_irq, err = 0;
Paul Burton943d69c2015-05-24 16:11:26 +010069 unsigned i;
Paul Burtonfe778ec2015-05-24 16:11:25 +010070
71 intc = kzalloc(sizeof(*intc), GFP_KERNEL);
72 if (!intc) {
73 err = -ENOMEM;
74 goto out_err;
75 }
Paul Burton69ce4b22015-05-24 16:11:22 +010076
77 parent_irq = irq_of_parse_and_map(node, 0);
Paul Burtonfe778ec2015-05-24 16:11:25 +010078 if (!parent_irq) {
79 err = -EINVAL;
80 goto out_free;
81 }
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +020082
Paul Burtonfe778ec2015-05-24 16:11:25 +010083 err = irq_set_handler_data(parent_irq, intc);
84 if (err)
85 goto out_unmap_irq;
86
Paul Burton943d69c2015-05-24 16:11:26 +010087 intc->num_chips = num_chips;
Paul Burton3aa94592015-05-24 16:11:28 +010088 intc->base = of_iomap(node, 0);
89 if (!intc->base) {
90 err = -ENODEV;
91 goto out_unmap_irq;
92 }
Lars-Peter Clausen98698482010-07-17 11:08:43 +000093
Paul Cercueil1fd224e2020-01-13 13:33:29 -030094 domain = irq_domain_add_linear(node, num_chips * 32,
Paul Cercueil8bc74642019-10-02 19:25:24 +080095 &irq_generic_chip_ops, NULL);
Paul Cercueil52ecc872019-10-02 19:25:22 +080096 if (!domain) {
97 err = -ENOMEM;
98 goto out_unmap_base;
99 }
100
Paul Cercueil208caad2019-10-02 19:25:23 +0800101 intc->domain = domain;
102
Paul Cercueil8bc74642019-10-02 19:25:24 +0800103 err = irq_alloc_domain_generic_chips(domain, 32, 1, "INTC",
104 handle_level_irq, 0,
105 IRQ_NOPROBE | IRQ_LEVEL, 0);
106 if (err)
107 goto out_domain_remove;
Thomas Gleixner42b64f32011-03-23 21:08:53 +0000108
Paul Cercueil8bc74642019-10-02 19:25:24 +0800109 for (i = 0; i < num_chips; i++) {
110 gc = irq_get_domain_generic_chip(domain, i * 32);
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +0200111
Paul Burton943d69c2015-05-24 16:11:26 +0100112 gc->wake_enabled = IRQ_MSK(32);
Paul Cercueil8bc74642019-10-02 19:25:24 +0800113 gc->reg_base = intc->base + (i * CHIP_SIZE);
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +0200114
Paul Burton943d69c2015-05-24 16:11:26 +0100115 ct = gc->chip_types;
116 ct->regs.enable = JZ_REG_INTC_CLEAR_MASK;
117 ct->regs.disable = JZ_REG_INTC_SET_MASK;
118 ct->chip.irq_unmask = irq_gc_unmask_enable_reg;
119 ct->chip.irq_mask = irq_gc_mask_disable_reg;
120 ct->chip.irq_mask_ack = irq_gc_mask_disable_reg;
121 ct->chip.irq_set_wake = irq_gc_set_wake;
Paul Cercueil20b44b42019-10-02 19:25:21 +0800122 ct->chip.flags = IRQCHIP_MASK_ON_SUSPEND;
Lars-Peter Clausen83bc7692011-09-24 02:29:46 +0200123
Paul Cercueil8bc74642019-10-02 19:25:24 +0800124 /* Mask all irqs */
125 irq_reg_writel(gc, IRQ_MSK(32), JZ_REG_INTC_SET_MASK);
Paul Burton943d69c2015-05-24 16:11:26 +0100126 }
Lars-Peter Clausen98698482010-07-17 11:08:43 +0000127
Paul Cercueil821fc9e2020-08-19 20:06:02 +0200128 if (request_irq(parent_irq, intc_cascade, IRQF_NO_SUSPEND,
afzal mohammed2ef1cb72020-03-04 06:18:38 +0530129 "SoC intc cascade interrupt", NULL))
130 pr_err("Failed to register SoC intc cascade interrupt\n");
Paul Burtonadbdce72015-05-24 16:11:21 +0100131 return 0;
Paul Burtonfe778ec2015-05-24 16:11:25 +0100132
Paul Cercueil8bc74642019-10-02 19:25:24 +0800133out_domain_remove:
134 irq_domain_remove(domain);
Paul Cercueil52ecc872019-10-02 19:25:22 +0800135out_unmap_base:
136 iounmap(intc->base);
Paul Burtonfe778ec2015-05-24 16:11:25 +0100137out_unmap_irq:
138 irq_dispose_mapping(parent_irq);
139out_free:
140 kfree(intc);
141out_err:
142 return err;
Lars-Peter Clausen98698482010-07-17 11:08:43 +0000143}
Paul Burton943d69c2015-05-24 16:11:26 +0100144
145static int __init intc_1chip_of_init(struct device_node *node,
146 struct device_node *parent)
147{
148 return ingenic_intc_of_init(node, 1);
149}
150IRQCHIP_DECLARE(jz4740_intc, "ingenic,jz4740-intc", intc_1chip_of_init);
Paul Cercueil10475572018-07-13 16:49:09 +0200151IRQCHIP_DECLARE(jz4725b_intc, "ingenic,jz4725b-intc", intc_1chip_of_init);
Paul Burton24ccfa02015-05-24 16:11:30 +0100152
153static int __init intc_2chip_of_init(struct device_node *node,
154 struct device_node *parent)
155{
156 return ingenic_intc_of_init(node, 2);
157}
Paul Cercueil5fbecd22021-03-07 17:20:14 +0000158IRQCHIP_DECLARE(jz4760_intc, "ingenic,jz4760-intc", intc_2chip_of_init);
Paul Burton24ccfa02015-05-24 16:11:30 +0100159IRQCHIP_DECLARE(jz4770_intc, "ingenic,jz4770-intc", intc_2chip_of_init);
160IRQCHIP_DECLARE(jz4775_intc, "ingenic,jz4775-intc", intc_2chip_of_init);
161IRQCHIP_DECLARE(jz4780_intc, "ingenic,jz4780-intc", intc_2chip_of_init);