Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
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>
6 */
7
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>
13
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
23
24#define RTL_ICTL_NUM_INPUTS 32
25
26#define REG(x) (realtek_ictl_base + x)
27
28static DEFINE_RAW_SPINLOCK(irq_lock);
29static void __iomem *realtek_ictl_base;
30
31/*
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
35 * lines {0..14}.
36 */
37#define IRR_OFFSET(idx) (4 * (3 - (idx * 4) / 32))
38#define IRR_SHIFT(idx) ((idx * 4) % 32)
39
40static void write_irr(void __iomem *irr0, int idx, u32 value)
41{
42 unsigned int offset = IRR_OFFSET(idx);
43 unsigned int shift = IRR_SHIFT(idx);
44 u32 irr;
45
46 irr = readl(irr0 + offset) & ~(0xf << shift);
47 irr |= (value & 0xf) << shift;
48 writel(irr, irr0 + offset);
49}
50
51static void realtek_ictl_unmask_irq(struct irq_data *i)
52{
53 unsigned long flags;
54 u32 value;
55
56 raw_spin_lock_irqsave(&irq_lock, flags);
57
58 value = readl(REG(RTL_ICTL_GIMR));
59 value |= BIT(i->hwirq);
60 writel(value, REG(RTL_ICTL_GIMR));
61
62 raw_spin_unlock_irqrestore(&irq_lock, flags);
63}
64
65static void realtek_ictl_mask_irq(struct irq_data *i)
66{
67 unsigned long flags;
68 u32 value;
69
70 raw_spin_lock_irqsave(&irq_lock, flags);
71
72 value = readl(REG(RTL_ICTL_GIMR));
73 value &= ~BIT(i->hwirq);
74 writel(value, REG(RTL_ICTL_GIMR));
75
76 raw_spin_unlock_irqrestore(&irq_lock, flags);
77}
78
79static 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,
83};
84
85static int intc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
86{
87 unsigned long flags;
88
89 irq_set_chip_and_handler(irq, &realtek_ictl_irq, handle_level_irq);
90
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);
94
95 return 0;
96}
97
98static const struct irq_domain_ops irq_domain_ops = {
99 .map = intc_map,
100 .xlate = irq_domain_xlate_onecell,
101};
102
103static void realtek_irq_dispatch(struct irq_desc *desc)
104{
105 struct irq_chip *chip = irq_desc_get_chip(desc);
106 struct irq_domain *domain;
107 unsigned long pending;
108 unsigned int soc_int;
109
110 chained_irq_enter(chip, desc);
111 pending = readl(REG(RTL_ICTL_GIMR)) & readl(REG(RTL_ICTL_GISR));
112
113 if (unlikely(!pending)) {
114 spurious_interrupt();
115 goto out;
116 }
117
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);
121
122out:
123 chained_irq_exit(chip, desc);
124}
125
126static int __init realtek_rtl_of_init(struct device_node *node, struct device_node *parent)
127{
128 struct of_phandle_args oirq;
129 struct irq_domain *domain;
130 unsigned int soc_irq;
131 int parent_irq;
132
133 realtek_ictl_base = of_iomap(node, 0);
134 if (!realtek_ictl_base)
135 return -ENXIO;
136
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);
141
142 if (WARN_ON(!of_irq_count(node))) {
143 /*
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.
148 */
149 oirq.np = of_find_compatible_node(NULL, NULL, "mti,cpu-interrupt-controller");
150 oirq.args_count = 1;
151 oirq.args[0] = 2;
152
153 parent_irq = irq_create_of_mapping(&oirq);
154
155 of_node_put(oirq.np);
156 } else {
157 parent_irq = of_irq_get(node, 0);
158 }
159
160 if (parent_irq < 0)
161 return parent_irq;
162 else if (!parent_irq)
163 return -ENODEV;
164
165 domain = irq_domain_add_linear(node, RTL_ICTL_NUM_INPUTS, &irq_domain_ops, NULL);
166 if (!domain)
167 return -ENOMEM;
168
169 irq_set_chained_handler_and_data(parent_irq, realtek_irq_dispatch, domain);
170
171 return 0;
172}
173
174IRQCHIP_DECLARE(realtek_rtl_intc, "realtek,rtl-intc", realtek_rtl_of_init);
1// SPDX-License-Identifier: GPL-2.0-only
2/*
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>
6 */
7
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>
13
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
23
24#define REG(x) (realtek_ictl_base + x)
25
26static DEFINE_RAW_SPINLOCK(irq_lock);
27static void __iomem *realtek_ictl_base;
28
29static void realtek_ictl_unmask_irq(struct irq_data *i)
30{
31 unsigned long flags;
32 u32 value;
33
34 raw_spin_lock_irqsave(&irq_lock, flags);
35
36 value = readl(REG(RTL_ICTL_GIMR));
37 value |= BIT(i->hwirq);
38 writel(value, REG(RTL_ICTL_GIMR));
39
40 raw_spin_unlock_irqrestore(&irq_lock, flags);
41}
42
43static void realtek_ictl_mask_irq(struct irq_data *i)
44{
45 unsigned long flags;
46 u32 value;
47
48 raw_spin_lock_irqsave(&irq_lock, flags);
49
50 value = readl(REG(RTL_ICTL_GIMR));
51 value &= ~BIT(i->hwirq);
52 writel(value, REG(RTL_ICTL_GIMR));
53
54 raw_spin_unlock_irqrestore(&irq_lock, flags);
55}
56
57static struct irq_chip realtek_ictl_irq = {
58 .name = "realtek-rtl-intc",
59 .irq_mask = realtek_ictl_mask_irq,
60 .irq_unmask = realtek_ictl_unmask_irq,
61};
62
63static int intc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
64{
65 irq_set_chip_and_handler(hw, &realtek_ictl_irq, handle_level_irq);
66
67 return 0;
68}
69
70static const struct irq_domain_ops irq_domain_ops = {
71 .map = intc_map,
72 .xlate = irq_domain_xlate_onecell,
73};
74
75static void realtek_irq_dispatch(struct irq_desc *desc)
76{
77 struct irq_chip *chip = irq_desc_get_chip(desc);
78 struct irq_domain *domain;
79 unsigned int pending;
80
81 chained_irq_enter(chip, desc);
82 pending = readl(REG(RTL_ICTL_GIMR)) & readl(REG(RTL_ICTL_GISR));
83 if (unlikely(!pending)) {
84 spurious_interrupt();
85 goto out;
86 }
87 domain = irq_desc_get_handler_data(desc);
88 generic_handle_domain_irq(domain, __ffs(pending));
89
90out:
91 chained_irq_exit(chip, desc);
92}
93
94/*
95 * SoC interrupts are cascaded to MIPS CPU interrupts according to the
96 * interrupt-map in the device tree. Each SoC interrupt gets 4 bits for
97 * the CPU interrupt in an Interrupt Routing Register. Max 32 SoC interrupts
98 * thus go into 4 IRRs.
99 */
100static int __init map_interrupts(struct device_node *node, struct irq_domain *domain)
101{
102 struct device_node *cpu_ictl;
103 const __be32 *imap;
104 u32 imaplen, soc_int, cpu_int, tmp, regs[4];
105 int ret, i, irr_regs[] = {
106 RTL_ICTL_IRR3,
107 RTL_ICTL_IRR2,
108 RTL_ICTL_IRR1,
109 RTL_ICTL_IRR0,
110 };
111 u8 mips_irqs_set;
112
113 ret = of_property_read_u32(node, "#address-cells", &tmp);
114 if (ret || tmp)
115 return -EINVAL;
116
117 imap = of_get_property(node, "interrupt-map", &imaplen);
118 if (!imap || imaplen % 3)
119 return -EINVAL;
120
121 mips_irqs_set = 0;
122 memset(regs, 0, sizeof(regs));
123 for (i = 0; i < imaplen; i += 3 * sizeof(u32)) {
124 soc_int = be32_to_cpup(imap);
125 if (soc_int > 31)
126 return -EINVAL;
127
128 cpu_ictl = of_find_node_by_phandle(be32_to_cpup(imap + 1));
129 if (!cpu_ictl)
130 return -EINVAL;
131 ret = of_property_read_u32(cpu_ictl, "#interrupt-cells", &tmp);
132 if (ret || tmp != 1)
133 return -EINVAL;
134 of_node_put(cpu_ictl);
135
136 cpu_int = be32_to_cpup(imap + 2);
137 if (cpu_int > 7)
138 return -EINVAL;
139
140 if (!(mips_irqs_set & BIT(cpu_int))) {
141 irq_set_chained_handler_and_data(cpu_int, realtek_irq_dispatch,
142 domain);
143 mips_irqs_set |= BIT(cpu_int);
144 }
145
146 regs[(soc_int * 4) / 32] |= cpu_int << (soc_int * 4) % 32;
147 imap += 3;
148 }
149
150 for (i = 0; i < 4; i++)
151 writel(regs[i], REG(irr_regs[i]));
152
153 return 0;
154}
155
156static int __init realtek_rtl_of_init(struct device_node *node, struct device_node *parent)
157{
158 struct irq_domain *domain;
159 int ret;
160
161 realtek_ictl_base = of_iomap(node, 0);
162 if (!realtek_ictl_base)
163 return -ENXIO;
164
165 /* Disable all cascaded interrupts */
166 writel(0, REG(RTL_ICTL_GIMR));
167
168 domain = irq_domain_add_simple(node, 32, 0,
169 &irq_domain_ops, NULL);
170
171 ret = map_interrupts(node, domain);
172 if (ret) {
173 pr_err("invalid interrupt map\n");
174 return ret;
175 }
176
177 return 0;
178}
179
180IRQCHIP_DECLARE(realtek_rtl_intc, "realtek,rtl-intc", realtek_rtl_of_init);