145051539SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b06eb017SChristian Ruppert /*
3b06eb017SChristian Ruppert * Abilis Systems interrupt controller driver
4b06eb017SChristian Ruppert *
5b06eb017SChristian Ruppert * Copyright (C) Abilis Systems 2012
6b06eb017SChristian Ruppert *
7b06eb017SChristian Ruppert * Author: Christian Ruppert <christian.ruppert@abilis.com>
8b06eb017SChristian Ruppert */
9b06eb017SChristian Ruppert
10b06eb017SChristian Ruppert #include <linux/interrupt.h>
11b06eb017SChristian Ruppert #include <linux/irqdomain.h>
12b06eb017SChristian Ruppert #include <linux/irq.h>
1341a83e06SJoel Porquet #include <linux/irqchip.h>
14b06eb017SChristian Ruppert #include <linux/of_irq.h>
15b06eb017SChristian Ruppert #include <linux/of_address.h>
16b06eb017SChristian Ruppert #include <linux/io.h>
17b06eb017SChristian Ruppert #include <linux/slab.h>
18b06eb017SChristian Ruppert #include <linux/bitops.h>
19b06eb017SChristian Ruppert
20b06eb017SChristian Ruppert #define AB_IRQCTL_INT_ENABLE 0x00
21b06eb017SChristian Ruppert #define AB_IRQCTL_INT_STATUS 0x04
22b06eb017SChristian Ruppert #define AB_IRQCTL_SRC_MODE 0x08
23b06eb017SChristian Ruppert #define AB_IRQCTL_SRC_POLARITY 0x0C
24b06eb017SChristian Ruppert #define AB_IRQCTL_INT_MODE 0x10
25b06eb017SChristian Ruppert #define AB_IRQCTL_INT_POLARITY 0x14
26b06eb017SChristian Ruppert #define AB_IRQCTL_INT_FORCE 0x18
27b06eb017SChristian Ruppert
28b06eb017SChristian Ruppert #define AB_IRQCTL_MAXIRQ 32
29b06eb017SChristian Ruppert
ab_irqctl_writereg(struct irq_chip_generic * gc,u32 reg,u32 val)30b06eb017SChristian Ruppert static inline void ab_irqctl_writereg(struct irq_chip_generic *gc, u32 reg,
31b06eb017SChristian Ruppert u32 val)
32b06eb017SChristian Ruppert {
33332fd7c4SKevin Cernekee irq_reg_writel(gc, val, reg);
34b06eb017SChristian Ruppert }
35b06eb017SChristian Ruppert
ab_irqctl_readreg(struct irq_chip_generic * gc,u32 reg)36b06eb017SChristian Ruppert static inline u32 ab_irqctl_readreg(struct irq_chip_generic *gc, u32 reg)
37b06eb017SChristian Ruppert {
38332fd7c4SKevin Cernekee return irq_reg_readl(gc, reg);
39b06eb017SChristian Ruppert }
40b06eb017SChristian Ruppert
tb10x_irq_set_type(struct irq_data * data,unsigned int flow_type)41b06eb017SChristian Ruppert static int tb10x_irq_set_type(struct irq_data *data, unsigned int flow_type)
42b06eb017SChristian Ruppert {
43b06eb017SChristian Ruppert struct irq_chip_generic *gc = irq_data_get_irq_chip_data(data);
44b06eb017SChristian Ruppert uint32_t im, mod, pol;
45b06eb017SChristian Ruppert
46b06eb017SChristian Ruppert im = data->mask;
47b06eb017SChristian Ruppert
48b06eb017SChristian Ruppert irq_gc_lock(gc);
49b06eb017SChristian Ruppert
50b06eb017SChristian Ruppert mod = ab_irqctl_readreg(gc, AB_IRQCTL_SRC_MODE) | im;
51b06eb017SChristian Ruppert pol = ab_irqctl_readreg(gc, AB_IRQCTL_SRC_POLARITY) | im;
52b06eb017SChristian Ruppert
53b06eb017SChristian Ruppert switch (flow_type & IRQF_TRIGGER_MASK) {
54b06eb017SChristian Ruppert case IRQ_TYPE_EDGE_FALLING:
55b06eb017SChristian Ruppert pol ^= im;
56b06eb017SChristian Ruppert break;
57b06eb017SChristian Ruppert case IRQ_TYPE_LEVEL_HIGH:
58b06eb017SChristian Ruppert mod ^= im;
59b06eb017SChristian Ruppert break;
60b06eb017SChristian Ruppert case IRQ_TYPE_NONE:
61b06eb017SChristian Ruppert flow_type = IRQ_TYPE_LEVEL_LOW;
62a6992bbeSRandy Dunlap fallthrough;
63b06eb017SChristian Ruppert case IRQ_TYPE_LEVEL_LOW:
64b06eb017SChristian Ruppert mod ^= im;
65b06eb017SChristian Ruppert pol ^= im;
66b06eb017SChristian Ruppert break;
67b06eb017SChristian Ruppert case IRQ_TYPE_EDGE_RISING:
68b06eb017SChristian Ruppert break;
69b06eb017SChristian Ruppert default:
70b06eb017SChristian Ruppert irq_gc_unlock(gc);
71b06eb017SChristian Ruppert pr_err("%s: Cannot assign multiple trigger modes to IRQ %d.\n",
72b06eb017SChristian Ruppert __func__, data->irq);
73b06eb017SChristian Ruppert return -EBADR;
74b06eb017SChristian Ruppert }
75b06eb017SChristian Ruppert
76b06eb017SChristian Ruppert irqd_set_trigger_type(data, flow_type);
77b06eb017SChristian Ruppert irq_setup_alt_chip(data, flow_type);
78b06eb017SChristian Ruppert
79b06eb017SChristian Ruppert ab_irqctl_writereg(gc, AB_IRQCTL_SRC_MODE, mod);
80b06eb017SChristian Ruppert ab_irqctl_writereg(gc, AB_IRQCTL_SRC_POLARITY, pol);
81b06eb017SChristian Ruppert ab_irqctl_writereg(gc, AB_IRQCTL_INT_STATUS, im);
82b06eb017SChristian Ruppert
83b06eb017SChristian Ruppert irq_gc_unlock(gc);
84b06eb017SChristian Ruppert
85b06eb017SChristian Ruppert return IRQ_SET_MASK_OK;
86b06eb017SChristian Ruppert }
87b06eb017SChristian Ruppert
tb10x_irq_cascade(struct irq_desc * desc)88bd0b9ac4SThomas Gleixner static void tb10x_irq_cascade(struct irq_desc *desc)
89b06eb017SChristian Ruppert {
90b06eb017SChristian Ruppert struct irq_domain *domain = irq_desc_get_handler_data(desc);
91e616e9afSThomas Gleixner unsigned int irq = irq_desc_get_irq(desc);
92b06eb017SChristian Ruppert
93*046a6ee2SMarc Zyngier generic_handle_domain_irq(domain, irq);
94b06eb017SChristian Ruppert }
95b06eb017SChristian Ruppert
of_tb10x_init_irq(struct device_node * ictl,struct device_node * parent)96b06eb017SChristian Ruppert static int __init of_tb10x_init_irq(struct device_node *ictl,
97b06eb017SChristian Ruppert struct device_node *parent)
98b06eb017SChristian Ruppert {
99b06eb017SChristian Ruppert int i, ret, nrirqs = of_irq_count(ictl);
100b06eb017SChristian Ruppert struct resource mem;
101b06eb017SChristian Ruppert struct irq_chip_generic *gc;
102b06eb017SChristian Ruppert struct irq_domain *domain;
103b06eb017SChristian Ruppert void __iomem *reg_base;
104b06eb017SChristian Ruppert
105b06eb017SChristian Ruppert if (of_address_to_resource(ictl, 0, &mem)) {
1062ef790dcSRob Herring pr_err("%pOFn: No registers declared in DeviceTree.\n",
1072ef790dcSRob Herring ictl);
108b06eb017SChristian Ruppert return -EINVAL;
109b06eb017SChristian Ruppert }
110b06eb017SChristian Ruppert
111b06eb017SChristian Ruppert if (!request_mem_region(mem.start, resource_size(&mem),
1122ef790dcSRob Herring ictl->full_name)) {
1132ef790dcSRob Herring pr_err("%pOFn: Request mem region failed.\n", ictl);
114b06eb017SChristian Ruppert return -EBUSY;
115b06eb017SChristian Ruppert }
116b06eb017SChristian Ruppert
117b06eb017SChristian Ruppert reg_base = ioremap(mem.start, resource_size(&mem));
118b06eb017SChristian Ruppert if (!reg_base) {
119b06eb017SChristian Ruppert ret = -EBUSY;
1202ef790dcSRob Herring pr_err("%pOFn: ioremap failed.\n", ictl);
121b06eb017SChristian Ruppert goto ioremap_fail;
122b06eb017SChristian Ruppert }
123b06eb017SChristian Ruppert
124b06eb017SChristian Ruppert domain = irq_domain_add_linear(ictl, AB_IRQCTL_MAXIRQ,
125b06eb017SChristian Ruppert &irq_generic_chip_ops, NULL);
126b06eb017SChristian Ruppert if (!domain) {
127b06eb017SChristian Ruppert ret = -ENOMEM;
1282ef790dcSRob Herring pr_err("%pOFn: Could not register interrupt domain.\n",
1292ef790dcSRob Herring ictl);
130b06eb017SChristian Ruppert goto irq_domain_add_fail;
131b06eb017SChristian Ruppert }
132b06eb017SChristian Ruppert
133b06eb017SChristian Ruppert ret = irq_alloc_domain_generic_chips(domain, AB_IRQCTL_MAXIRQ,
134b06eb017SChristian Ruppert 2, ictl->name, handle_level_irq,
135b06eb017SChristian Ruppert IRQ_NOREQUEST, IRQ_NOPROBE,
136b06eb017SChristian Ruppert IRQ_GC_INIT_MASK_CACHE);
137b06eb017SChristian Ruppert if (ret) {
1382ef790dcSRob Herring pr_err("%pOFn: Could not allocate generic interrupt chip.\n",
1392ef790dcSRob Herring ictl);
140b06eb017SChristian Ruppert goto gc_alloc_fail;
141b06eb017SChristian Ruppert }
142b06eb017SChristian Ruppert
143b06eb017SChristian Ruppert gc = domain->gc->gc[0];
144b06eb017SChristian Ruppert gc->reg_base = reg_base;
145b06eb017SChristian Ruppert
146b06eb017SChristian Ruppert gc->chip_types[0].type = IRQ_TYPE_LEVEL_MASK;
147b06eb017SChristian Ruppert gc->chip_types[0].chip.irq_mask = irq_gc_mask_clr_bit;
148b06eb017SChristian Ruppert gc->chip_types[0].chip.irq_unmask = irq_gc_mask_set_bit;
149b06eb017SChristian Ruppert gc->chip_types[0].chip.irq_set_type = tb10x_irq_set_type;
150b06eb017SChristian Ruppert gc->chip_types[0].regs.mask = AB_IRQCTL_INT_ENABLE;
151b06eb017SChristian Ruppert
152b06eb017SChristian Ruppert gc->chip_types[1].type = IRQ_TYPE_EDGE_BOTH;
153b06eb017SChristian Ruppert gc->chip_types[1].chip.name = gc->chip_types[0].chip.name;
154b06eb017SChristian Ruppert gc->chip_types[1].chip.irq_ack = irq_gc_ack_set_bit;
155b06eb017SChristian Ruppert gc->chip_types[1].chip.irq_mask = irq_gc_mask_clr_bit;
156b06eb017SChristian Ruppert gc->chip_types[1].chip.irq_unmask = irq_gc_mask_set_bit;
157b06eb017SChristian Ruppert gc->chip_types[1].chip.irq_set_type = tb10x_irq_set_type;
158b06eb017SChristian Ruppert gc->chip_types[1].regs.ack = AB_IRQCTL_INT_STATUS;
159b06eb017SChristian Ruppert gc->chip_types[1].regs.mask = AB_IRQCTL_INT_ENABLE;
160b06eb017SChristian Ruppert gc->chip_types[1].handler = handle_edge_irq;
161b06eb017SChristian Ruppert
162b06eb017SChristian Ruppert for (i = 0; i < nrirqs; i++) {
163b06eb017SChristian Ruppert unsigned int irq = irq_of_parse_and_map(ictl, i);
164b06eb017SChristian Ruppert
16522890b0dSThomas Gleixner irq_set_chained_handler_and_data(irq, tb10x_irq_cascade,
16622890b0dSThomas Gleixner domain);
167b06eb017SChristian Ruppert }
168b06eb017SChristian Ruppert
169b06eb017SChristian Ruppert ab_irqctl_writereg(gc, AB_IRQCTL_INT_ENABLE, 0);
170b06eb017SChristian Ruppert ab_irqctl_writereg(gc, AB_IRQCTL_INT_MODE, 0);
171b06eb017SChristian Ruppert ab_irqctl_writereg(gc, AB_IRQCTL_INT_POLARITY, 0);
172b06eb017SChristian Ruppert ab_irqctl_writereg(gc, AB_IRQCTL_INT_STATUS, ~0UL);
173b06eb017SChristian Ruppert
174b06eb017SChristian Ruppert return 0;
175b06eb017SChristian Ruppert
176b06eb017SChristian Ruppert gc_alloc_fail:
177b06eb017SChristian Ruppert irq_domain_remove(domain);
178b06eb017SChristian Ruppert irq_domain_add_fail:
179b06eb017SChristian Ruppert iounmap(reg_base);
180b06eb017SChristian Ruppert ioremap_fail:
181b06eb017SChristian Ruppert release_mem_region(mem.start, resource_size(&mem));
182b06eb017SChristian Ruppert return ret;
183b06eb017SChristian Ruppert }
184b06eb017SChristian Ruppert IRQCHIP_DECLARE(tb10x_intc, "abilis,tb10x-ictl", of_tb10x_init_irq);
185