1 // SPDX-License-Identifier: GPL-2.0-only
3 * The R_INTC in Allwinner A31 and newer SoCs manages several types of
4 * interrupts, as shown below:
6 * NMI IRQ DIRECT IRQs MUXED IRQs
7 * bit 0 bits 1-15^ bits 19-31
9 * +---------+ +---------+ +---------+ +---------+
10 * | NMI Pad | | IRQ d | | IRQ m | | IRQ m+7 |
11 * +---------+ +---------+ +---------+ +---------+
14 * +------V------+ +------------+ | | | +--V------V--+ |
15 * | Invert/ | | Write 1 to | | | | | AND with | |
16 * | Edge Detect | | PENDING[0] | | | | | MUX[m/8] | |
17 * +-------------+ +------------+ | | | +------------+ |
19 * +--V-------V--+ +--V--+ | +--V--+ | +--V--+
20 * | Set Reset| | GIC | | | GIC | | | GIC |
21 * | Latch | | SPI | | | SPI |... | ...| SPI |
22 * +-------------+ | N+d | | | m | | | m+7 |
23 * | | +-----+ | +-----+ | +-----+
25 * +-------V-+ +-V----------+ +---------V--+ +--------V--------+
26 * | GIC SPI | | AND with | | AND with | | AND with |
27 * | N (=32) | | ENABLE[0] | | ENABLE[d] | | ENABLE[19+m/8] |
28 * +---------+ +------------+ +------------+ +-----------------+
30 * +------V-----+ +------V-----+ +--------V--------+
31 * | Read | | Read | | Read |
32 * | PENDING[0] | | PENDING[d] | | PENDING[19+m/8] |
33 * +------------+ +------------+ +-----------------+
35 * ^ bits 16-18 are direct IRQs for peripherals with banked interrupts, such as
36 * the MSGBOX. These IRQs do not map to any GIC SPI.
38 * The H6 variant adds two more (banked) direct IRQs and implements the full
39 * set of 128 mux bits. This requires a second set of top-level registers.
42 #include <linux/bitmap.h>
43 #include <linux/interrupt.h>
44 #include <linux/irq.h>
45 #include <linux/irqchip.h>
46 #include <linux/irqdomain.h>
48 #include <linux/of_address.h>
49 #include <linux/of_irq.h>
50 #include <linux/syscore_ops.h>
52 #include <dt-bindings/interrupt-controller/arm-gic.h>
54 #define SUN6I_NMI_CTRL (0x0c)
55 #define SUN6I_IRQ_PENDING(n) (0x10 + 4 * (n))
56 #define SUN6I_IRQ_ENABLE(n) (0x40 + 4 * (n))
57 #define SUN6I_MUX_ENABLE(n) (0xc0 + 4 * (n))
59 #define SUN6I_NMI_SRC_TYPE_LEVEL_LOW 0
60 #define SUN6I_NMI_SRC_TYPE_EDGE_FALLING 1
61 #define SUN6I_NMI_SRC_TYPE_LEVEL_HIGH 2
62 #define SUN6I_NMI_SRC_TYPE_EDGE_RISING 3
64 #define SUN6I_NMI_BIT BIT(0)
66 #define SUN6I_NMI_NEEDS_ACK ((void *)1)
68 #define SUN6I_NR_TOP_LEVEL_IRQS 64
69 #define SUN6I_NR_DIRECT_IRQS 16
70 #define SUN6I_NR_MUX_BITS 128
72 struct sun6i_r_intc_variant {
75 u32 mux_valid[BITS_TO_U32(SUN6I_NR_MUX_BITS)];
78 static void __iomem *base;
79 static irq_hw_number_t nmi_hwirq;
80 static DECLARE_BITMAP(wake_irq_enabled, SUN6I_NR_TOP_LEVEL_IRQS);
81 static DECLARE_BITMAP(wake_mux_enabled, SUN6I_NR_MUX_BITS);
82 static DECLARE_BITMAP(wake_mux_valid, SUN6I_NR_MUX_BITS);
84 static void sun6i_r_intc_ack_nmi(void)
86 writel_relaxed(SUN6I_NMI_BIT, base + SUN6I_IRQ_PENDING(0));
89 static void sun6i_r_intc_nmi_ack(struct irq_data *data)
91 if (irqd_get_trigger_type(data) & IRQ_TYPE_EDGE_BOTH)
92 sun6i_r_intc_ack_nmi();
94 data->chip_data = SUN6I_NMI_NEEDS_ACK;
97 static void sun6i_r_intc_nmi_eoi(struct irq_data *data)
99 /* For oneshot IRQs, delay the ack until the IRQ is unmasked. */
100 if (data->chip_data == SUN6I_NMI_NEEDS_ACK && !irqd_irq_masked(data)) {
101 data->chip_data = NULL;
102 sun6i_r_intc_ack_nmi();
105 irq_chip_eoi_parent(data);
108 static void sun6i_r_intc_nmi_unmask(struct irq_data *data)
110 if (data->chip_data == SUN6I_NMI_NEEDS_ACK) {
111 data->chip_data = NULL;
112 sun6i_r_intc_ack_nmi();
115 irq_chip_unmask_parent(data);
118 static int sun6i_r_intc_nmi_set_type(struct irq_data *data, unsigned int type)
123 case IRQ_TYPE_EDGE_RISING:
124 nmi_src_type = SUN6I_NMI_SRC_TYPE_EDGE_RISING;
126 case IRQ_TYPE_EDGE_FALLING:
127 nmi_src_type = SUN6I_NMI_SRC_TYPE_EDGE_FALLING;
129 case IRQ_TYPE_LEVEL_HIGH:
130 nmi_src_type = SUN6I_NMI_SRC_TYPE_LEVEL_HIGH;
132 case IRQ_TYPE_LEVEL_LOW:
133 nmi_src_type = SUN6I_NMI_SRC_TYPE_LEVEL_LOW;
139 writel_relaxed(nmi_src_type, base + SUN6I_NMI_CTRL);
142 * The "External NMI" GIC input connects to a latch inside R_INTC, not
143 * directly to the pin. So the GIC trigger type does not depend on the
144 * NMI pin trigger type.
146 return irq_chip_set_type_parent(data, IRQ_TYPE_LEVEL_HIGH);
149 static int sun6i_r_intc_nmi_set_irqchip_state(struct irq_data *data,
150 enum irqchip_irq_state which,
153 if (which == IRQCHIP_STATE_PENDING && !state)
154 sun6i_r_intc_ack_nmi();
156 return irq_chip_set_parent_state(data, which, state);
159 static int sun6i_r_intc_irq_set_wake(struct irq_data *data, unsigned int on)
161 unsigned long offset_from_nmi = data->hwirq - nmi_hwirq;
163 if (offset_from_nmi < SUN6I_NR_DIRECT_IRQS)
164 assign_bit(offset_from_nmi, wake_irq_enabled, on);
165 else if (test_bit(data->hwirq, wake_mux_valid))
166 assign_bit(data->hwirq, wake_mux_enabled, on);
168 /* Not wakeup capable. */
174 static struct irq_chip sun6i_r_intc_nmi_chip = {
175 .name = "sun6i-r-intc",
176 .irq_ack = sun6i_r_intc_nmi_ack,
177 .irq_mask = irq_chip_mask_parent,
178 .irq_unmask = sun6i_r_intc_nmi_unmask,
179 .irq_eoi = sun6i_r_intc_nmi_eoi,
180 .irq_set_affinity = irq_chip_set_affinity_parent,
181 .irq_set_type = sun6i_r_intc_nmi_set_type,
182 .irq_set_irqchip_state = sun6i_r_intc_nmi_set_irqchip_state,
183 .irq_set_wake = sun6i_r_intc_irq_set_wake,
184 .flags = IRQCHIP_SET_TYPE_MASKED,
187 static struct irq_chip sun6i_r_intc_wakeup_chip = {
188 .name = "sun6i-r-intc",
189 .irq_mask = irq_chip_mask_parent,
190 .irq_unmask = irq_chip_unmask_parent,
191 .irq_eoi = irq_chip_eoi_parent,
192 .irq_set_affinity = irq_chip_set_affinity_parent,
193 .irq_set_type = irq_chip_set_type_parent,
194 .irq_set_wake = sun6i_r_intc_irq_set_wake,
195 .flags = IRQCHIP_SET_TYPE_MASKED,
198 static int sun6i_r_intc_domain_translate(struct irq_domain *domain,
199 struct irq_fwspec *fwspec,
200 unsigned long *hwirq,
203 /* Accept the old two-cell binding for the NMI only. */
204 if (fwspec->param_count == 2 && fwspec->param[0] == 0) {
206 *type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
210 /* Otherwise this binding should match the GIC SPI binding. */
211 if (fwspec->param_count < 3)
213 if (fwspec->param[0] != GIC_SPI)
216 *hwirq = fwspec->param[1];
217 *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
222 static int sun6i_r_intc_domain_alloc(struct irq_domain *domain,
224 unsigned int nr_irqs, void *arg)
226 struct irq_fwspec *fwspec = arg;
227 struct irq_fwspec gic_fwspec;
232 ret = sun6i_r_intc_domain_translate(domain, fwspec, &hwirq, &type);
235 if (hwirq + nr_irqs > SUN6I_NR_MUX_BITS)
238 /* Construct a GIC-compatible fwspec from this fwspec. */
239 gic_fwspec = (struct irq_fwspec) {
240 .fwnode = domain->parent->fwnode,
242 .param = { GIC_SPI, hwirq, type },
245 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, &gic_fwspec);
249 for (i = 0; i < nr_irqs; ++i, ++hwirq, ++virq) {
250 if (hwirq == nmi_hwirq) {
251 irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
252 &sun6i_r_intc_nmi_chip, 0);
253 irq_set_handler(virq, handle_fasteoi_ack_irq);
255 irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
256 &sun6i_r_intc_wakeup_chip, 0);
263 static const struct irq_domain_ops sun6i_r_intc_domain_ops = {
264 .translate = sun6i_r_intc_domain_translate,
265 .alloc = sun6i_r_intc_domain_alloc,
266 .free = irq_domain_free_irqs_common,
269 static int sun6i_r_intc_suspend(void)
271 u32 buf[BITS_TO_U32(max(SUN6I_NR_TOP_LEVEL_IRQS, SUN6I_NR_MUX_BITS))];
274 /* Wake IRQs are enabled during system sleep and shutdown. */
275 bitmap_to_arr32(buf, wake_irq_enabled, SUN6I_NR_TOP_LEVEL_IRQS);
276 for (i = 0; i < BITS_TO_U32(SUN6I_NR_TOP_LEVEL_IRQS); ++i)
277 writel_relaxed(buf[i], base + SUN6I_IRQ_ENABLE(i));
278 bitmap_to_arr32(buf, wake_mux_enabled, SUN6I_NR_MUX_BITS);
279 for (i = 0; i < BITS_TO_U32(SUN6I_NR_MUX_BITS); ++i)
280 writel_relaxed(buf[i], base + SUN6I_MUX_ENABLE(i));
285 static void sun6i_r_intc_resume(void)
289 /* Only the NMI is relevant during normal operation. */
290 writel_relaxed(SUN6I_NMI_BIT, base + SUN6I_IRQ_ENABLE(0));
291 for (i = 1; i < BITS_TO_U32(SUN6I_NR_TOP_LEVEL_IRQS); ++i)
292 writel_relaxed(0, base + SUN6I_IRQ_ENABLE(i));
295 static void sun6i_r_intc_shutdown(void)
297 sun6i_r_intc_suspend();
300 static struct syscore_ops sun6i_r_intc_syscore_ops = {
301 .suspend = sun6i_r_intc_suspend,
302 .resume = sun6i_r_intc_resume,
303 .shutdown = sun6i_r_intc_shutdown,
306 static int __init sun6i_r_intc_init(struct device_node *node,
307 struct device_node *parent,
308 const struct sun6i_r_intc_variant *v)
310 struct irq_domain *domain, *parent_domain;
311 struct of_phandle_args nmi_parent;
314 /* Extract the NMI hwirq number from the OF node. */
315 ret = of_irq_parse_one(node, 0, &nmi_parent);
318 if (nmi_parent.args_count < 3 ||
319 nmi_parent.args[0] != GIC_SPI ||
320 nmi_parent.args[2] != IRQ_TYPE_LEVEL_HIGH)
322 nmi_hwirq = nmi_parent.args[1];
324 bitmap_set(wake_irq_enabled, v->first_mux_irq, v->nr_mux_irqs);
325 bitmap_from_arr32(wake_mux_valid, v->mux_valid, SUN6I_NR_MUX_BITS);
327 parent_domain = irq_find_host(parent);
328 if (!parent_domain) {
329 pr_err("%pOF: Failed to obtain parent domain\n", node);
333 base = of_io_request_and_map(node, 0, NULL);
335 pr_err("%pOF: Failed to map MMIO region\n", node);
336 return PTR_ERR(base);
339 domain = irq_domain_add_hierarchy(parent_domain, 0, 0, node,
340 &sun6i_r_intc_domain_ops, NULL);
342 pr_err("%pOF: Failed to allocate domain\n", node);
347 register_syscore_ops(&sun6i_r_intc_syscore_ops);
349 sun6i_r_intc_ack_nmi();
350 sun6i_r_intc_resume();
355 static const struct sun6i_r_intc_variant sun6i_a31_r_intc_variant __initconst = {
358 .mux_valid = { 0xffffffff, 0xfff80000, 0xffffffff, 0x0000000f },
361 static int __init sun6i_a31_r_intc_init(struct device_node *node,
362 struct device_node *parent)
364 return sun6i_r_intc_init(node, parent, &sun6i_a31_r_intc_variant);
366 IRQCHIP_DECLARE(sun6i_a31_r_intc, "allwinner,sun6i-a31-r-intc", sun6i_a31_r_intc_init);
368 static const struct sun6i_r_intc_variant sun50i_h6_r_intc_variant __initconst = {
371 .mux_valid = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff },
374 static int __init sun50i_h6_r_intc_init(struct device_node *node,
375 struct device_node *parent)
377 return sun6i_r_intc_init(node, parent, &sun50i_h6_r_intc_variant);
379 IRQCHIP_DECLARE(sun50i_h6_r_intc, "allwinner,sun50i-h6-r-intc", sun50i_h6_r_intc_init);