1 // SPDX-License-Identifier: GPL-2.0-only
3 * Broadcom BCM7038 style Level 1 interrupt controller driver
5 * Copyright (C) 2014 Broadcom Corporation
6 * Author: Kevin Cernekee
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/bitops.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
16 #include <linux/ioport.h>
17 #include <linux/irq.h>
18 #include <linux/irqdomain.h>
19 #include <linux/module.h>
21 #include <linux/of_irq.h>
22 #include <linux/of_address.h>
23 #include <linux/of_platform.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/smp.h>
27 #include <linux/types.h>
28 #include <linux/irqchip.h>
29 #include <linux/irqchip/chained_irq.h>
30 #include <linux/syscore_ops.h>
32 #define IRQS_PER_WORD 32
33 #define REG_BYTES_PER_IRQ_WORD (sizeof(u32) * 4)
36 struct bcm7038_l1_cpu;
38 struct bcm7038_l1_chip {
41 struct irq_domain *domain;
42 struct bcm7038_l1_cpu *cpus[NR_CPUS];
43 #ifdef CONFIG_PM_SLEEP
44 struct list_head list;
45 u32 wake_mask[MAX_WORDS];
47 u32 irq_fwd_mask[MAX_WORDS];
48 u8 affinity[MAX_WORDS * IRQS_PER_WORD];
51 struct bcm7038_l1_cpu {
52 void __iomem *map_base;
57 * STATUS/MASK_STATUS/MASK_SET/MASK_CLEAR are packed one right after another:
60 * 0x1000_1400: W0_STATUS
61 * 0x1000_1404: W1_STATUS
62 * 0x1000_1408: W0_MASK_STATUS
63 * 0x1000_140c: W1_MASK_STATUS
64 * 0x1000_1410: W0_MASK_SET
65 * 0x1000_1414: W1_MASK_SET
66 * 0x1000_1418: W0_MASK_CLEAR
67 * 0x1000_141c: W1_MASK_CLEAR
70 * 0xf03e_1500: W0_STATUS
71 * 0xf03e_1504: W1_STATUS
72 * 0xf03e_1508: W2_STATUS
73 * 0xf03e_150c: W3_STATUS
74 * 0xf03e_1510: W4_STATUS
75 * 0xf03e_1514: W0_MASK_STATUS
76 * 0xf03e_1518: W1_MASK_STATUS
80 static inline unsigned int reg_status(struct bcm7038_l1_chip *intc,
83 return (0 * intc->n_words + word) * sizeof(u32);
86 static inline unsigned int reg_mask_status(struct bcm7038_l1_chip *intc,
89 return (1 * intc->n_words + word) * sizeof(u32);
92 static inline unsigned int reg_mask_set(struct bcm7038_l1_chip *intc,
95 return (2 * intc->n_words + word) * sizeof(u32);
98 static inline unsigned int reg_mask_clr(struct bcm7038_l1_chip *intc,
101 return (3 * intc->n_words + word) * sizeof(u32);
104 static inline u32 l1_readl(void __iomem *reg)
106 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
107 return ioread32be(reg);
112 static inline void l1_writel(u32 val, void __iomem *reg)
114 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
115 iowrite32be(val, reg);
120 static void bcm7038_l1_irq_handle(struct irq_desc *desc)
122 struct bcm7038_l1_chip *intc = irq_desc_get_handler_data(desc);
123 struct bcm7038_l1_cpu *cpu;
124 struct irq_chip *chip = irq_desc_get_chip(desc);
127 #if defined(CONFIG_SMP) && defined(CONFIG_MIPS)
128 cpu = intc->cpus[cpu_logical_map(smp_processor_id())];
133 chained_irq_enter(chip, desc);
135 for (idx = 0; idx < intc->n_words; idx++) {
136 int base = idx * IRQS_PER_WORD;
137 unsigned long pending, flags;
140 raw_spin_lock_irqsave(&intc->lock, flags);
141 pending = l1_readl(cpu->map_base + reg_status(intc, idx)) &
142 ~cpu->mask_cache[idx];
143 raw_spin_unlock_irqrestore(&intc->lock, flags);
145 for_each_set_bit(hwirq, &pending, IRQS_PER_WORD)
146 generic_handle_domain_irq(intc->domain, base + hwirq);
149 chained_irq_exit(chip, desc);
152 static void __bcm7038_l1_unmask(struct irq_data *d, unsigned int cpu_idx)
154 struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
155 u32 word = d->hwirq / IRQS_PER_WORD;
156 u32 mask = BIT(d->hwirq % IRQS_PER_WORD);
158 intc->cpus[cpu_idx]->mask_cache[word] &= ~mask;
159 l1_writel(mask, intc->cpus[cpu_idx]->map_base +
160 reg_mask_clr(intc, word));
163 static void __bcm7038_l1_mask(struct irq_data *d, unsigned int cpu_idx)
165 struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
166 u32 word = d->hwirq / IRQS_PER_WORD;
167 u32 mask = BIT(d->hwirq % IRQS_PER_WORD);
169 intc->cpus[cpu_idx]->mask_cache[word] |= mask;
170 l1_writel(mask, intc->cpus[cpu_idx]->map_base +
171 reg_mask_set(intc, word));
174 static void bcm7038_l1_unmask(struct irq_data *d)
176 struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
179 raw_spin_lock_irqsave(&intc->lock, flags);
180 __bcm7038_l1_unmask(d, intc->affinity[d->hwirq]);
181 raw_spin_unlock_irqrestore(&intc->lock, flags);
184 static void bcm7038_l1_mask(struct irq_data *d)
186 struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
189 raw_spin_lock_irqsave(&intc->lock, flags);
190 __bcm7038_l1_mask(d, intc->affinity[d->hwirq]);
191 raw_spin_unlock_irqrestore(&intc->lock, flags);
194 #if defined(CONFIG_MIPS) && defined(CONFIG_SMP)
195 static int bcm7038_l1_set_affinity(struct irq_data *d,
196 const struct cpumask *dest,
199 struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
201 irq_hw_number_t hw = d->hwirq;
202 u32 word = hw / IRQS_PER_WORD;
203 u32 mask = BIT(hw % IRQS_PER_WORD);
204 unsigned int first_cpu = cpumask_any_and(dest, cpu_online_mask);
207 raw_spin_lock_irqsave(&intc->lock, flags);
209 was_disabled = !!(intc->cpus[intc->affinity[hw]]->mask_cache[word] &
211 __bcm7038_l1_mask(d, intc->affinity[hw]);
212 intc->affinity[hw] = first_cpu;
214 __bcm7038_l1_unmask(d, first_cpu);
216 raw_spin_unlock_irqrestore(&intc->lock, flags);
217 irq_data_update_effective_affinity(d, cpumask_of(first_cpu));
223 static int __init bcm7038_l1_init_one(struct device_node *dn,
225 struct bcm7038_l1_chip *intc)
229 struct bcm7038_l1_cpu *cpu;
230 unsigned int i, n_words, parent_irq;
233 if (of_address_to_resource(dn, idx, &res))
235 sz = resource_size(&res);
236 n_words = sz / REG_BYTES_PER_IRQ_WORD;
238 if (n_words > MAX_WORDS)
240 else if (!intc->n_words)
241 intc->n_words = n_words;
242 else if (intc->n_words != n_words)
245 ret = of_property_read_u32_array(dn , "brcm,int-fwd-mask",
246 intc->irq_fwd_mask, n_words);
247 if (ret != 0 && ret != -EINVAL) {
248 /* property exists but has the wrong number of words */
249 pr_err("invalid brcm,int-fwd-mask property\n");
253 cpu = intc->cpus[idx] = kzalloc(sizeof(*cpu) + n_words * sizeof(u32),
258 cpu->map_base = ioremap(res.start, sz);
262 for (i = 0; i < n_words; i++) {
263 l1_writel(~intc->irq_fwd_mask[i],
264 cpu->map_base + reg_mask_set(intc, i));
265 l1_writel(intc->irq_fwd_mask[i],
266 cpu->map_base + reg_mask_clr(intc, i));
267 cpu->mask_cache[i] = ~intc->irq_fwd_mask[i];
270 parent_irq = irq_of_parse_and_map(dn, idx);
272 pr_err("failed to map parent interrupt %d\n", parent_irq);
276 if (of_property_read_bool(dn, "brcm,irq-can-wake"))
277 enable_irq_wake(parent_irq);
279 irq_set_chained_handler_and_data(parent_irq, bcm7038_l1_irq_handle,
285 #ifdef CONFIG_PM_SLEEP
287 * We keep a list of bcm7038_l1_chip used for suspend/resume. This hack is
288 * used because the struct chip_type suspend/resume hooks are not called
289 * unless chip_type is hooked onto a generic_chip. Since this driver does
290 * not use generic_chip, we need to manually hook our resume/suspend to
293 static LIST_HEAD(bcm7038_l1_intcs_list);
294 static DEFINE_RAW_SPINLOCK(bcm7038_l1_intcs_lock);
296 static int bcm7038_l1_suspend(void)
298 struct bcm7038_l1_chip *intc;
302 /* Wakeup interrupt should only come from the boot cpu */
303 #if defined(CONFIG_SMP) && defined(CONFIG_MIPS)
304 boot_cpu = cpu_logical_map(0);
309 list_for_each_entry(intc, &bcm7038_l1_intcs_list, list) {
310 for (word = 0; word < intc->n_words; word++) {
311 val = intc->wake_mask[word] | intc->irq_fwd_mask[word];
313 intc->cpus[boot_cpu]->map_base + reg_mask_set(intc, word));
315 intc->cpus[boot_cpu]->map_base + reg_mask_clr(intc, word));
322 static void bcm7038_l1_resume(void)
324 struct bcm7038_l1_chip *intc;
327 #if defined(CONFIG_SMP) && defined(CONFIG_MIPS)
328 boot_cpu = cpu_logical_map(0);
333 list_for_each_entry(intc, &bcm7038_l1_intcs_list, list) {
334 for (word = 0; word < intc->n_words; word++) {
335 l1_writel(intc->cpus[boot_cpu]->mask_cache[word],
336 intc->cpus[boot_cpu]->map_base + reg_mask_set(intc, word));
337 l1_writel(~intc->cpus[boot_cpu]->mask_cache[word],
338 intc->cpus[boot_cpu]->map_base + reg_mask_clr(intc, word));
343 static struct syscore_ops bcm7038_l1_syscore_ops = {
344 .suspend = bcm7038_l1_suspend,
345 .resume = bcm7038_l1_resume,
348 static int bcm7038_l1_set_wake(struct irq_data *d, unsigned int on)
350 struct bcm7038_l1_chip *intc = irq_data_get_irq_chip_data(d);
352 u32 word = d->hwirq / IRQS_PER_WORD;
353 u32 mask = BIT(d->hwirq % IRQS_PER_WORD);
355 raw_spin_lock_irqsave(&intc->lock, flags);
357 intc->wake_mask[word] |= mask;
359 intc->wake_mask[word] &= ~mask;
360 raw_spin_unlock_irqrestore(&intc->lock, flags);
366 static struct irq_chip bcm7038_l1_irq_chip = {
367 .name = "bcm7038-l1",
368 .irq_mask = bcm7038_l1_mask,
369 .irq_unmask = bcm7038_l1_unmask,
370 #if defined(CONFIG_SMP) && defined(CONFIG_MIPS)
371 .irq_set_affinity = bcm7038_l1_set_affinity,
373 #ifdef CONFIG_PM_SLEEP
374 .irq_set_wake = bcm7038_l1_set_wake,
378 static int bcm7038_l1_map(struct irq_domain *d, unsigned int virq,
379 irq_hw_number_t hw_irq)
381 struct bcm7038_l1_chip *intc = d->host_data;
382 u32 mask = BIT(hw_irq % IRQS_PER_WORD);
383 u32 word = hw_irq / IRQS_PER_WORD;
385 if (intc->irq_fwd_mask[word] & mask)
388 irq_set_chip_and_handler(virq, &bcm7038_l1_irq_chip, handle_level_irq);
389 irq_set_chip_data(virq, d->host_data);
390 irqd_set_single_target(irq_get_irq_data(virq));
394 static const struct irq_domain_ops bcm7038_l1_domain_ops = {
395 .xlate = irq_domain_xlate_onecell,
396 .map = bcm7038_l1_map,
399 static int __init bcm7038_l1_of_init(struct device_node *dn,
400 struct device_node *parent)
402 struct bcm7038_l1_chip *intc;
405 intc = kzalloc(sizeof(*intc), GFP_KERNEL);
409 raw_spin_lock_init(&intc->lock);
410 for_each_possible_cpu(idx) {
411 ret = bcm7038_l1_init_one(dn, idx, intc);
415 pr_err("failed to remap intc L1 registers\n");
420 intc->domain = irq_domain_add_linear(dn, IRQS_PER_WORD * intc->n_words,
421 &bcm7038_l1_domain_ops,
428 #ifdef CONFIG_PM_SLEEP
429 /* Add bcm7038_l1_chip into a list */
430 raw_spin_lock(&bcm7038_l1_intcs_lock);
431 list_add_tail(&intc->list, &bcm7038_l1_intcs_list);
432 raw_spin_unlock(&bcm7038_l1_intcs_lock);
434 if (list_is_singular(&bcm7038_l1_intcs_list))
435 register_syscore_ops(&bcm7038_l1_syscore_ops);
438 pr_info("registered BCM7038 L1 intc (%pOF, IRQs: %d)\n",
439 dn, IRQS_PER_WORD * intc->n_words);
444 for_each_possible_cpu(idx) {
445 struct bcm7038_l1_cpu *cpu = intc->cpus[idx];
449 iounmap(cpu->map_base);
458 IRQCHIP_PLATFORM_DRIVER_BEGIN(bcm7038_l1)
459 IRQCHIP_MATCH("brcm,bcm7038-l1-intc", bcm7038_l1_of_init)
460 IRQCHIP_PLATFORM_DRIVER_END(bcm7038_l1)
461 MODULE_DESCRIPTION("Broadcom STB 7038-style L1/L2 interrupt controller");
462 MODULE_LICENSE("GPL v2");