blob: f23d7651ea8476a0b27fbf13798b2b44bb943077 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Florian Fainellia5042de22014-09-09 17:44:21 -07002/*
3 * Broadcom BCM7120 style Level 2 interrupt controller driver
4 *
5 * Copyright (C) 2014 Broadcom Corporation
Florian Fainellia5042de22014-09-09 17:44:21 -07006 */
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10#include <linux/init.h>
11#include <linux/slab.h>
12#include <linux/module.h>
Kevin Cernekee7b7230e2014-12-25 09:49:05 -080013#include <linux/kernel.h>
Florian Fainellia5042de22014-09-09 17:44:21 -070014#include <linux/platform_device.h>
15#include <linux/of.h>
16#include <linux/of_irq.h>
17#include <linux/of_address.h>
18#include <linux/of_platform.h>
19#include <linux/interrupt.h>
20#include <linux/irq.h>
21#include <linux/io.h>
22#include <linux/irqdomain.h>
23#include <linux/reboot.h>
Kevin Cernekeec76acf42014-11-06 22:44:26 -080024#include <linux/bitops.h>
Joel Porquet41a83e062015-07-07 17:11:46 -040025#include <linux/irqchip.h>
Florian Fainellia5042de22014-09-09 17:44:21 -070026#include <linux/irqchip/chained_irq.h>
27
Florian Fainellia5042de22014-09-09 17:44:21 -070028/* Register offset in the L2 interrupt controller */
29#define IRQEN 0x00
30#define IRQSTAT 0x04
31
Kevin Cernekeec76acf42014-11-06 22:44:26 -080032#define MAX_WORDS 4
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -080033#define MAX_MAPPINGS (MAX_WORDS * 2)
Kevin Cernekeec76acf42014-11-06 22:44:26 -080034#define IRQS_PER_WORD 32
35
Florian Fainelli0aef3992015-07-23 15:52:21 -070036struct bcm7120_l1_intc_data {
37 struct bcm7120_l2_intc_data *b;
38 u32 irq_map_mask[MAX_WORDS];
39};
40
Florian Fainellia5042de22014-09-09 17:44:21 -070041struct bcm7120_l2_intc_data {
Kevin Cernekeec76acf42014-11-06 22:44:26 -080042 unsigned int n_words;
Kevin Cernekee5b5468c2014-12-25 09:49:03 -080043 void __iomem *map_base[MAX_MAPPINGS];
44 void __iomem *pair_base[MAX_WORDS];
45 int en_offset[MAX_WORDS];
46 int stat_offset[MAX_WORDS];
Florian Fainellia5042de22014-09-09 17:44:21 -070047 struct irq_domain *domain;
48 bool can_wake;
Kevin Cernekeec76acf42014-11-06 22:44:26 -080049 u32 irq_fwd_mask[MAX_WORDS];
Florian Fainelli0aef3992015-07-23 15:52:21 -070050 struct bcm7120_l1_intc_data *l1_data;
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -080051 int num_parent_irqs;
52 const __be32 *map_mask_prop;
Florian Fainellia5042de22014-09-09 17:44:21 -070053};
54
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +020055static void bcm7120_l2_intc_irq_handle(struct irq_desc *desc)
Florian Fainellia5042de22014-09-09 17:44:21 -070056{
Florian Fainelli0aef3992015-07-23 15:52:21 -070057 struct bcm7120_l1_intc_data *data = irq_desc_get_handler_data(desc);
58 struct bcm7120_l2_intc_data *b = data->b;
Florian Fainellia5042de22014-09-09 17:44:21 -070059 struct irq_chip *chip = irq_desc_get_chip(desc);
Kevin Cernekeec76acf42014-11-06 22:44:26 -080060 unsigned int idx;
Florian Fainellia5042de22014-09-09 17:44:21 -070061
62 chained_irq_enter(chip, desc);
63
Kevin Cernekeec76acf42014-11-06 22:44:26 -080064 for (idx = 0; idx < b->n_words; idx++) {
65 int base = idx * IRQS_PER_WORD;
66 struct irq_chip_generic *gc =
67 irq_get_domain_generic_chip(b->domain, base);
68 unsigned long pending;
69 int hwirq;
Florian Fainellia5042de22014-09-09 17:44:21 -070070
Kevin Cernekeec76acf42014-11-06 22:44:26 -080071 irq_gc_lock(gc);
Kevin Cernekee5b5468c2014-12-25 09:49:03 -080072 pending = irq_reg_readl(gc, b->stat_offset[idx]) &
Florian Fainelli0aef3992015-07-23 15:52:21 -070073 gc->mask_cache &
74 data->irq_map_mask[idx];
Kevin Cernekeec76acf42014-11-06 22:44:26 -080075 irq_gc_unlock(gc);
76
Marc Zyngier046a6ee2021-05-04 17:42:18 +010077 for_each_set_bit(hwirq, &pending, IRQS_PER_WORD)
78 generic_handle_domain_irq(b->domain, base + hwirq);
Florian Fainellia5042de22014-09-09 17:44:21 -070079 }
80
Florian Fainellia5042de22014-09-09 17:44:21 -070081 chained_irq_exit(chip, desc);
82}
83
Brian Norrisfd537762015-07-22 16:21:40 -070084static void bcm7120_l2_intc_suspend(struct irq_chip_generic *gc)
Florian Fainellia5042de22014-09-09 17:44:21 -070085{
Florian Fainellia5042de22014-09-09 17:44:21 -070086 struct bcm7120_l2_intc_data *b = gc->private;
Brian Norrisfd537762015-07-22 16:21:40 -070087 struct irq_chip_type *ct = gc->chip_types;
Florian Fainellia5042de22014-09-09 17:44:21 -070088
89 irq_gc_lock(gc);
Kevin Cernekeec17261f2014-11-06 22:44:28 -080090 if (b->can_wake)
Kevin Cernekee5b5468c2014-12-25 09:49:03 -080091 irq_reg_writel(gc, gc->mask_cache | gc->wake_active,
92 ct->regs.mask);
Florian Fainellia5042de22014-09-09 17:44:21 -070093 irq_gc_unlock(gc);
94}
95
Brian Norrisfd537762015-07-22 16:21:40 -070096static void bcm7120_l2_intc_resume(struct irq_chip_generic *gc)
Florian Fainellia5042de22014-09-09 17:44:21 -070097{
Brian Norrisfd537762015-07-22 16:21:40 -070098 struct irq_chip_type *ct = gc->chip_types;
Florian Fainellia5042de22014-09-09 17:44:21 -070099
100 /* Restore the saved mask */
101 irq_gc_lock(gc);
Kevin Cernekee5b5468c2014-12-25 09:49:03 -0800102 irq_reg_writel(gc, gc->mask_cache, ct->regs.mask);
Florian Fainellia5042de22014-09-09 17:44:21 -0700103 irq_gc_unlock(gc);
104}
105
106static int bcm7120_l2_intc_init_one(struct device_node *dn,
107 struct bcm7120_l2_intc_data *data,
Florian Fainelli0aef3992015-07-23 15:52:21 -0700108 int irq, u32 *valid_mask)
Florian Fainellia5042de22014-09-09 17:44:21 -0700109{
Florian Fainelli0aef3992015-07-23 15:52:21 -0700110 struct bcm7120_l1_intc_data *l1_data = &data->l1_data[irq];
Florian Fainellia5042de22014-09-09 17:44:21 -0700111 int parent_irq;
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800112 unsigned int idx;
Florian Fainellia5042de22014-09-09 17:44:21 -0700113
114 parent_irq = irq_of_parse_and_map(dn, irq);
Dmitry Torokhov714710e2014-11-14 14:16:14 -0800115 if (!parent_irq) {
Florian Fainellia5042de22014-09-09 17:44:21 -0700116 pr_err("failed to map interrupt %d\n", irq);
Dmitry Torokhov714710e2014-11-14 14:16:14 -0800117 return -EINVAL;
Florian Fainellia5042de22014-09-09 17:44:21 -0700118 }
119
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800120 /* For multiple parent IRQs with multiple words, this looks like:
121 * <irq0_w0 irq0_w1 irq1_w0 irq1_w1 ...>
Florian Fainelli0aef3992015-07-23 15:52:21 -0700122 *
123 * We need to associate a given parent interrupt with its corresponding
124 * map_mask in order to mask the status register with it because we
125 * have the same handler being called for multiple parent interrupts.
126 *
127 * This is typically something needed on BCM7xxx (STB chips).
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800128 */
Kevin Cernekee7b7230e2014-12-25 09:49:05 -0800129 for (idx = 0; idx < data->n_words; idx++) {
130 if (data->map_mask_prop) {
Florian Fainelli0aef3992015-07-23 15:52:21 -0700131 l1_data->irq_map_mask[idx] |=
Kevin Cernekee7b7230e2014-12-25 09:49:05 -0800132 be32_to_cpup(data->map_mask_prop +
133 irq * data->n_words + idx);
134 } else {
Florian Fainelli0aef3992015-07-23 15:52:21 -0700135 l1_data->irq_map_mask[idx] = 0xffffffff;
Kevin Cernekee7b7230e2014-12-25 09:49:05 -0800136 }
Florian Fainelli0aef3992015-07-23 15:52:21 -0700137 valid_mask[idx] |= l1_data->irq_map_mask[idx];
Kevin Cernekee7b7230e2014-12-25 09:49:05 -0800138 }
Florian Fainellia5042de22014-09-09 17:44:21 -0700139
Florian Fainelli0aef3992015-07-23 15:52:21 -0700140 l1_data->b = data;
Florian Fainellia5042de22014-09-09 17:44:21 -0700141
Florian Fainelli0aef3992015-07-23 15:52:21 -0700142 irq_set_chained_handler_and_data(parent_irq,
143 bcm7120_l2_intc_irq_handle, l1_data);
Justin Chenf4ccb742020-07-09 15:30:11 -0700144 if (data->can_wake)
145 enable_irq_wake(parent_irq);
146
Florian Fainellia5042de22014-09-09 17:44:21 -0700147 return 0;
148}
149
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800150static int __init bcm7120_l2_intc_iomap_7120(struct device_node *dn,
151 struct bcm7120_l2_intc_data *data)
152{
153 int ret;
154
155 data->map_base[0] = of_iomap(dn, 0);
156 if (!data->map_base[0]) {
157 pr_err("unable to map registers\n");
158 return -ENOMEM;
159 }
160
161 data->pair_base[0] = data->map_base[0];
162 data->en_offset[0] = IRQEN;
163 data->stat_offset[0] = IRQSTAT;
164 data->n_words = 1;
165
166 ret = of_property_read_u32_array(dn, "brcm,int-fwd-mask",
167 data->irq_fwd_mask, data->n_words);
168 if (ret != 0 && ret != -EINVAL) {
169 /* property exists but has the wrong number of words */
170 pr_err("invalid brcm,int-fwd-mask property\n");
171 return -EINVAL;
172 }
173
174 data->map_mask_prop = of_get_property(dn, "brcm,int-map-mask", &ret);
175 if (!data->map_mask_prop ||
176 (ret != (sizeof(__be32) * data->num_parent_irqs * data->n_words))) {
177 pr_err("invalid brcm,int-map-mask property\n");
178 return -EINVAL;
179 }
180
181 return 0;
182}
183
Kevin Cernekee7b7230e2014-12-25 09:49:05 -0800184static int __init bcm7120_l2_intc_iomap_3380(struct device_node *dn,
185 struct bcm7120_l2_intc_data *data)
186{
187 unsigned int gc_idx;
188
189 for (gc_idx = 0; gc_idx < MAX_WORDS; gc_idx++) {
190 unsigned int map_idx = gc_idx * 2;
191 void __iomem *en = of_iomap(dn, map_idx + 0);
192 void __iomem *stat = of_iomap(dn, map_idx + 1);
193 void __iomem *base = min(en, stat);
194
195 data->map_base[map_idx + 0] = en;
196 data->map_base[map_idx + 1] = stat;
197
198 if (!base)
199 break;
200
201 data->pair_base[gc_idx] = base;
202 data->en_offset[gc_idx] = en - base;
203 data->stat_offset[gc_idx] = stat - base;
204 }
205
206 if (!gc_idx) {
207 pr_err("unable to map registers\n");
208 return -EINVAL;
209 }
210
211 data->n_words = gc_idx;
212 return 0;
213}
214
Ben Dooksdde7e6d1a2016-06-08 18:59:58 +0100215static int __init bcm7120_l2_intc_probe(struct device_node *dn,
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800216 struct device_node *parent,
217 int (*iomap_regs_fn)(struct device_node *,
218 struct bcm7120_l2_intc_data *),
219 const char *intc_name)
Florian Fainellia5042de22014-09-09 17:44:21 -0700220{
221 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
222 struct bcm7120_l2_intc_data *data;
223 struct irq_chip_generic *gc;
224 struct irq_chip_type *ct;
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800225 int ret = 0;
Kevin Cernekeec17261f2014-11-06 22:44:28 -0800226 unsigned int idx, irq, flags;
Florian Fainelli0aef3992015-07-23 15:52:21 -0700227 u32 valid_mask[MAX_WORDS] = { };
Florian Fainellia5042de22014-09-09 17:44:21 -0700228
229 data = kzalloc(sizeof(*data), GFP_KERNEL);
230 if (!data)
231 return -ENOMEM;
232
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800233 data->num_parent_irqs = of_irq_count(dn);
234 if (data->num_parent_irqs <= 0) {
Florian Fainellia5042de22014-09-09 17:44:21 -0700235 pr_err("invalid number of parent interrupts\n");
236 ret = -ENOMEM;
237 goto out_unmap;
238 }
239
Florian Fainelli0aef3992015-07-23 15:52:21 -0700240 data->l1_data = kcalloc(data->num_parent_irqs, sizeof(*data->l1_data),
241 GFP_KERNEL);
242 if (!data->l1_data) {
243 ret = -ENOMEM;
244 goto out_free_l1_data;
245 }
246
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800247 ret = iomap_regs_fn(dn, data);
248 if (ret < 0)
Florian Fainelli0aef3992015-07-23 15:52:21 -0700249 goto out_free_l1_data;
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800250
Justin Chenf4ccb742020-07-09 15:30:11 -0700251 data->can_wake = of_property_read_bool(dn, "brcm,irq-can-wake");
252
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800253 for (irq = 0; irq < data->num_parent_irqs; irq++) {
Florian Fainelli0aef3992015-07-23 15:52:21 -0700254 ret = bcm7120_l2_intc_init_one(dn, data, irq, valid_mask);
Florian Fainellia5042de22014-09-09 17:44:21 -0700255 if (ret)
Florian Fainelli0aef3992015-07-23 15:52:21 -0700256 goto out_free_l1_data;
Florian Fainellia5042de22014-09-09 17:44:21 -0700257 }
258
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800259 data->domain = irq_domain_add_linear(dn, IRQS_PER_WORD * data->n_words,
260 &irq_generic_chip_ops, NULL);
Florian Fainellia5042de22014-09-09 17:44:21 -0700261 if (!data->domain) {
262 ret = -ENOMEM;
Florian Fainelli0aef3992015-07-23 15:52:21 -0700263 goto out_free_l1_data;
Florian Fainellia5042de22014-09-09 17:44:21 -0700264 }
265
Kevin Cernekeec17261f2014-11-06 22:44:28 -0800266 /* MIPS chips strapped for BE will automagically configure the
267 * peripheral registers for CPU-native byte order.
268 */
269 flags = IRQ_GC_INIT_MASK_CACHE;
270 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
271 flags |= IRQ_GC_BE_IO;
272
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800273 ret = irq_alloc_domain_generic_chips(data->domain, IRQS_PER_WORD, 1,
Kevin Cernekeec17261f2014-11-06 22:44:28 -0800274 dn->full_name, handle_level_irq, clr, 0, flags);
Florian Fainellia5042de22014-09-09 17:44:21 -0700275 if (ret) {
276 pr_err("failed to allocate generic irq chip\n");
277 goto out_free_domain;
278 }
279
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800280 for (idx = 0; idx < data->n_words; idx++) {
281 irq = idx * IRQS_PER_WORD;
282 gc = irq_get_domain_generic_chip(data->domain, irq);
283
Florian Fainelli0aef3992015-07-23 15:52:21 -0700284 gc->unused = 0xffffffff & ~valid_mask[idx];
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800285 gc->private = data;
286 ct = gc->chip_types;
287
Kevin Cernekee5b5468c2014-12-25 09:49:03 -0800288 gc->reg_base = data->pair_base[idx];
289 ct->regs.mask = data->en_offset[idx];
290
Florian Fainellib3046052017-08-30 17:29:16 -0700291 /* gc->reg_base is defined and so is gc->writel */
292 irq_reg_writel(gc, data->irq_fwd_mask[idx],
293 data->en_offset[idx]);
294
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800295 ct->chip.irq_mask = irq_gc_mask_clr_bit;
296 ct->chip.irq_unmask = irq_gc_mask_set_bit;
297 ct->chip.irq_ack = irq_gc_noop;
Brian Norrisfd537762015-07-22 16:21:40 -0700298 gc->suspend = bcm7120_l2_intc_suspend;
299 gc->resume = bcm7120_l2_intc_resume;
300
301 /*
302 * Initialize mask-cache, in case we need it for
303 * saving/restoring fwd mask even w/o any child interrupts
304 * installed
305 */
306 gc->mask_cache = irq_reg_readl(gc, ct->regs.mask);
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800307
308 if (data->can_wake) {
309 /* This IRQ chip can wake the system, set all
Ingo Molnara359f752021-03-22 04:21:30 +0100310 * relevant child interrupts in wake_enabled mask
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800311 */
312 gc->wake_enabled = 0xffffffff;
313 gc->wake_enabled &= ~gc->unused;
314 ct->chip.irq_set_wake = irq_gc_set_wake;
315 }
Florian Fainellia5042de22014-09-09 17:44:21 -0700316 }
317
Florian Fainelli082ce272019-03-20 12:39:19 -0700318 pr_info("registered %s intc (%pOF, parent IRQ(s): %d)\n",
319 intc_name, dn, data->num_parent_irqs);
320
Florian Fainellia5042de22014-09-09 17:44:21 -0700321 return 0;
322
323out_free_domain:
324 irq_domain_remove(data->domain);
Florian Fainelli0aef3992015-07-23 15:52:21 -0700325out_free_l1_data:
326 kfree(data->l1_data);
Florian Fainellia5042de22014-09-09 17:44:21 -0700327out_unmap:
Kevin Cernekee5b5468c2014-12-25 09:49:03 -0800328 for (idx = 0; idx < MAX_MAPPINGS; idx++) {
329 if (data->map_base[idx])
330 iounmap(data->map_base[idx]);
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800331 }
Florian Fainellia5042de22014-09-09 17:44:21 -0700332 kfree(data);
333 return ret;
334}
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800335
Ben Dooksdde7e6d1a2016-06-08 18:59:58 +0100336static int __init bcm7120_l2_intc_probe_7120(struct device_node *dn,
337 struct device_node *parent)
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800338{
339 return bcm7120_l2_intc_probe(dn, parent, bcm7120_l2_intc_iomap_7120,
340 "BCM7120 L2");
341}
342
Ben Dooksdde7e6d1a2016-06-08 18:59:58 +0100343static int __init bcm7120_l2_intc_probe_3380(struct device_node *dn,
344 struct device_node *parent)
Kevin Cernekee7b7230e2014-12-25 09:49:05 -0800345{
346 return bcm7120_l2_intc_probe(dn, parent, bcm7120_l2_intc_iomap_3380,
347 "BCM3380 L2");
348}
349
Kevin Cernekeea4fcbb82014-11-06 22:44:27 -0800350IRQCHIP_DECLARE(bcm7120_l2_intc, "brcm,bcm7120-l2-intc",
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800351 bcm7120_l2_intc_probe_7120);
Kevin Cernekee7b7230e2014-12-25 09:49:05 -0800352
353IRQCHIP_DECLARE(bcm3380_l2_intc, "brcm,bcm3380-l2-intc",
354 bcm7120_l2_intc_probe_3380);