1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 Birger Koblitz <mail@birger-koblitz.de>
4 * Copyright (C) 2020 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2020 John Crispin <john@phrozen.org>
8 #include <linux/of_irq.h>
9 #include <linux/irqchip.h>
10 #include <linux/spinlock.h>
11 #include <linux/of_address.h>
12 #include <linux/irqchip/chained_irq.h>
14 /* Global Interrupt Mask Register */
15 #define RTL_ICTL_GIMR 0x00
16 /* Global Interrupt Status Register */
17 #define RTL_ICTL_GISR 0x04
18 /* Interrupt Routing Registers */
19 #define RTL_ICTL_IRR0 0x08
20 #define RTL_ICTL_IRR1 0x0c
21 #define RTL_ICTL_IRR2 0x10
22 #define RTL_ICTL_IRR3 0x14
24 #define RTL_ICTL_NUM_INPUTS 32
26 #define REG(x) (realtek_ictl_base + x)
28 static DEFINE_RAW_SPINLOCK(irq_lock);
29 static void __iomem *realtek_ictl_base;
32 * IRR0-IRR3 store 4 bits per interrupt, but Realtek uses inverted numbering,
33 * placing IRQ 31 in the first four bits. A routing value of '0' means the
34 * interrupt is left disconnected. Routing values {1..15} connect to output
37 #define IRR_OFFSET(idx) (4 * (3 - (idx * 4) / 32))
38 #define IRR_SHIFT(idx) ((idx * 4) % 32)
40 static void write_irr(void __iomem *irr0, int idx, u32 value)
42 unsigned int offset = IRR_OFFSET(idx);
43 unsigned int shift = IRR_SHIFT(idx);
46 irr = readl(irr0 + offset) & ~(0xf << shift);
47 irr |= (value & 0xf) << shift;
48 writel(irr, irr0 + offset);
51 static void realtek_ictl_unmask_irq(struct irq_data *i)
56 raw_spin_lock_irqsave(&irq_lock, flags);
58 value = readl(REG(RTL_ICTL_GIMR));
59 value |= BIT(i->hwirq);
60 writel(value, REG(RTL_ICTL_GIMR));
62 raw_spin_unlock_irqrestore(&irq_lock, flags);
65 static void realtek_ictl_mask_irq(struct irq_data *i)
70 raw_spin_lock_irqsave(&irq_lock, flags);
72 value = readl(REG(RTL_ICTL_GIMR));
73 value &= ~BIT(i->hwirq);
74 writel(value, REG(RTL_ICTL_GIMR));
76 raw_spin_unlock_irqrestore(&irq_lock, flags);
79 static struct irq_chip realtek_ictl_irq = {
80 .name = "realtek-rtl-intc",
81 .irq_mask = realtek_ictl_mask_irq,
82 .irq_unmask = realtek_ictl_unmask_irq,
85 static int intc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
89 irq_set_chip_and_handler(irq, &realtek_ictl_irq, handle_level_irq);
91 raw_spin_lock_irqsave(&irq_lock, flags);
92 write_irr(REG(RTL_ICTL_IRR0), hw, 1);
93 raw_spin_unlock_irqrestore(&irq_lock, flags);
98 static const struct irq_domain_ops irq_domain_ops = {
100 .xlate = irq_domain_xlate_onecell,
103 static void realtek_irq_dispatch(struct irq_desc *desc)
105 struct irq_chip *chip = irq_desc_get_chip(desc);
106 struct irq_domain *domain;
107 unsigned long pending;
108 unsigned int soc_int;
110 chained_irq_enter(chip, desc);
111 pending = readl(REG(RTL_ICTL_GIMR)) & readl(REG(RTL_ICTL_GISR));
113 if (unlikely(!pending)) {
114 spurious_interrupt();
118 domain = irq_desc_get_handler_data(desc);
119 for_each_set_bit(soc_int, &pending, 32)
120 generic_handle_domain_irq(domain, soc_int);
123 chained_irq_exit(chip, desc);
126 static int __init realtek_rtl_of_init(struct device_node *node, struct device_node *parent)
128 struct of_phandle_args oirq;
129 struct irq_domain *domain;
130 unsigned int soc_irq;
133 realtek_ictl_base = of_iomap(node, 0);
134 if (!realtek_ictl_base)
137 /* Disable all cascaded interrupts and clear routing */
138 writel(0, REG(RTL_ICTL_GIMR));
139 for (soc_irq = 0; soc_irq < RTL_ICTL_NUM_INPUTS; soc_irq++)
140 write_irr(REG(RTL_ICTL_IRR0), soc_irq, 0);
142 if (WARN_ON(!of_irq_count(node))) {
144 * If DT contains no parent interrupts, assume MIPS CPU IRQ 2
145 * (HW0) is connected to the first output. This is the case for
146 * all known hardware anyway. "interrupt-map" is deprecated, so
147 * don't bother trying to parse that.
149 oirq.np = of_find_compatible_node(NULL, NULL, "mti,cpu-interrupt-controller");
153 parent_irq = irq_create_of_mapping(&oirq);
155 of_node_put(oirq.np);
157 parent_irq = of_irq_get(node, 0);
162 else if (!parent_irq)
165 domain = irq_domain_add_linear(node, RTL_ICTL_NUM_INPUTS, &irq_domain_ops, NULL);
169 irq_set_chained_handler_and_data(parent_irq, realtek_irq_dispatch, domain);
174 IRQCHIP_DECLARE(realtek_rtl_intc, "realtek,rtl-intc", realtek_rtl_of_init);