xref: /openbmc/linux/drivers/irqchip/irq-nvic.c (revision 459c3bc8c497d6b665c2efd15ee7df0f4e19452a)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2292ec080SUwe Kleine-König /*
3292ec080SUwe Kleine-König  * drivers/irq/irq-nvic.c
4292ec080SUwe Kleine-König  *
5292ec080SUwe Kleine-König  * Copyright (C) 2008 ARM Limited, All Rights Reserved.
6292ec080SUwe Kleine-König  * Copyright (C) 2013 Pengutronix
7292ec080SUwe Kleine-König  *
8292ec080SUwe Kleine-König  * Support for the Nested Vectored Interrupt Controller found on the
9292ec080SUwe Kleine-König  * ARMv7-M CPUs (Cortex-M3/M4)
10292ec080SUwe Kleine-König  */
11292ec080SUwe Kleine-König #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
12292ec080SUwe Kleine-König 
13292ec080SUwe Kleine-König #include <linux/init.h>
14292ec080SUwe Kleine-König #include <linux/kernel.h>
15292ec080SUwe Kleine-König #include <linux/slab.h>
16292ec080SUwe Kleine-König #include <linux/err.h>
17292ec080SUwe Kleine-König #include <linux/io.h>
18292ec080SUwe Kleine-König #include <linux/of.h>
19292ec080SUwe Kleine-König #include <linux/of_address.h>
20292ec080SUwe Kleine-König #include <linux/irq.h>
2141a83e06SJoel Porquet #include <linux/irqchip.h>
22292ec080SUwe Kleine-König #include <linux/irqdomain.h>
23292ec080SUwe Kleine-König 
24292ec080SUwe Kleine-König #include <asm/v7m.h>
25292ec080SUwe Kleine-König #include <asm/exception.h>
26292ec080SUwe Kleine-König 
27292ec080SUwe Kleine-König #define NVIC_ISER		0x000
28292ec080SUwe Kleine-König #define NVIC_ICER		0x080
29292ec080SUwe Kleine-König #define NVIC_IPR		0x300
30292ec080SUwe Kleine-König 
31292ec080SUwe Kleine-König #define NVIC_MAX_BANKS		16
32292ec080SUwe Kleine-König /*
33292ec080SUwe Kleine-König  * Each bank handles 32 irqs. Only the 16th (= last) bank handles only
34292ec080SUwe Kleine-König  * 16 irqs.
35292ec080SUwe Kleine-König  */
36292ec080SUwe Kleine-König #define NVIC_MAX_IRQ		((NVIC_MAX_BANKS - 1) * 32 + 16)
37292ec080SUwe Kleine-König 
38292ec080SUwe Kleine-König static struct irq_domain *nvic_irq_domain;
39292ec080SUwe Kleine-König 
40292ec080SUwe Kleine-König asmlinkage void __exception_irq_entry
41292ec080SUwe Kleine-König nvic_handle_irq(irq_hw_number_t hwirq, struct pt_regs *regs)
42292ec080SUwe Kleine-König {
43292ec080SUwe Kleine-König 	unsigned int irq = irq_linear_revmap(nvic_irq_domain, hwirq);
44292ec080SUwe Kleine-König 
45292ec080SUwe Kleine-König 	handle_IRQ(irq, regs);
46292ec080SUwe Kleine-König }
47292ec080SUwe Kleine-König 
482d9f59f7SStefan Agner static int nvic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
492d9f59f7SStefan Agner 				unsigned int nr_irqs, void *arg)
502d9f59f7SStefan Agner {
512d9f59f7SStefan Agner 	int i, ret;
522d9f59f7SStefan Agner 	irq_hw_number_t hwirq;
532d9f59f7SStefan Agner 	unsigned int type = IRQ_TYPE_NONE;
54f833f57fSMarc Zyngier 	struct irq_fwspec *fwspec = arg;
552d9f59f7SStefan Agner 
56*459c3bc8SYash Shah 	ret = irq_domain_translate_onecell(domain, fwspec, &hwirq, &type);
572d9f59f7SStefan Agner 	if (ret)
582d9f59f7SStefan Agner 		return ret;
592d9f59f7SStefan Agner 
602d9f59f7SStefan Agner 	for (i = 0; i < nr_irqs; i++)
612d9f59f7SStefan Agner 		irq_map_generic_chip(domain, virq + i, hwirq + i);
622d9f59f7SStefan Agner 
632d9f59f7SStefan Agner 	return 0;
642d9f59f7SStefan Agner }
652d9f59f7SStefan Agner 
662d9f59f7SStefan Agner static const struct irq_domain_ops nvic_irq_domain_ops = {
67*459c3bc8SYash Shah 	.translate = irq_domain_translate_onecell,
682d9f59f7SStefan Agner 	.alloc = nvic_irq_domain_alloc,
692d9f59f7SStefan Agner 	.free = irq_domain_free_irqs_top,
702d9f59f7SStefan Agner };
712d9f59f7SStefan Agner 
72292ec080SUwe Kleine-König static int __init nvic_of_init(struct device_node *node,
73292ec080SUwe Kleine-König 			       struct device_node *parent)
74292ec080SUwe Kleine-König {
75292ec080SUwe Kleine-König 	unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
76292ec080SUwe Kleine-König 	unsigned int irqs, i, ret, numbanks;
77292ec080SUwe Kleine-König 	void __iomem *nvic_base;
78292ec080SUwe Kleine-König 
79292ec080SUwe Kleine-König 	numbanks = (readl_relaxed(V7M_SCS_ICTR) &
80292ec080SUwe Kleine-König 		    V7M_SCS_ICTR_INTLINESNUM_MASK) + 1;
81292ec080SUwe Kleine-König 
82292ec080SUwe Kleine-König 	nvic_base = of_iomap(node, 0);
83292ec080SUwe Kleine-König 	if (!nvic_base) {
84292ec080SUwe Kleine-König 		pr_warn("unable to map nvic registers\n");
85292ec080SUwe Kleine-König 		return -ENOMEM;
86292ec080SUwe Kleine-König 	}
87292ec080SUwe Kleine-König 
88292ec080SUwe Kleine-König 	irqs = numbanks * 32;
89292ec080SUwe Kleine-König 	if (irqs > NVIC_MAX_IRQ)
90292ec080SUwe Kleine-König 		irqs = NVIC_MAX_IRQ;
91292ec080SUwe Kleine-König 
92292ec080SUwe Kleine-König 	nvic_irq_domain =
932d9f59f7SStefan Agner 		irq_domain_add_linear(node, irqs, &nvic_irq_domain_ops, NULL);
942d9f59f7SStefan Agner 
95292ec080SUwe Kleine-König 	if (!nvic_irq_domain) {
96292ec080SUwe Kleine-König 		pr_warn("Failed to allocate irq domain\n");
97292ec080SUwe Kleine-König 		return -ENOMEM;
98292ec080SUwe Kleine-König 	}
99292ec080SUwe Kleine-König 
1005b8aae48SAxel Lin 	ret = irq_alloc_domain_generic_chips(nvic_irq_domain, 32, 1,
101292ec080SUwe Kleine-König 					     "nvic_irq", handle_fasteoi_irq,
102292ec080SUwe Kleine-König 					     clr, 0, IRQ_GC_INIT_MASK_CACHE);
103292ec080SUwe Kleine-König 	if (ret) {
104292ec080SUwe Kleine-König 		pr_warn("Failed to allocate irq chips\n");
105292ec080SUwe Kleine-König 		irq_domain_remove(nvic_irq_domain);
106292ec080SUwe Kleine-König 		return ret;
107292ec080SUwe Kleine-König 	}
108292ec080SUwe Kleine-König 
109292ec080SUwe Kleine-König 	for (i = 0; i < numbanks; ++i) {
110292ec080SUwe Kleine-König 		struct irq_chip_generic *gc;
111292ec080SUwe Kleine-König 
112292ec080SUwe Kleine-König 		gc = irq_get_domain_generic_chip(nvic_irq_domain, 32 * i);
113292ec080SUwe Kleine-König 		gc->reg_base = nvic_base + 4 * i;
114292ec080SUwe Kleine-König 		gc->chip_types[0].regs.enable = NVIC_ISER;
115292ec080SUwe Kleine-König 		gc->chip_types[0].regs.disable = NVIC_ICER;
116292ec080SUwe Kleine-König 		gc->chip_types[0].chip.irq_mask = irq_gc_mask_disable_reg;
117292ec080SUwe Kleine-König 		gc->chip_types[0].chip.irq_unmask = irq_gc_unmask_enable_reg;
1188b53ec26SDaniel Thompson 		/* This is a no-op as end of interrupt is signaled by the
1198b53ec26SDaniel Thompson 		 * exception return sequence.
1208b53ec26SDaniel Thompson 		 */
1218b53ec26SDaniel Thompson 		gc->chip_types[0].chip.irq_eoi = irq_gc_noop;
122292ec080SUwe Kleine-König 
123292ec080SUwe Kleine-König 		/* disable interrupts */
124292ec080SUwe Kleine-König 		writel_relaxed(~0, gc->reg_base + NVIC_ICER);
125292ec080SUwe Kleine-König 	}
126292ec080SUwe Kleine-König 
127292ec080SUwe Kleine-König 	/* Set priority on all interrupts */
128292ec080SUwe Kleine-König 	for (i = 0; i < irqs; i += 4)
129292ec080SUwe Kleine-König 		writel_relaxed(0, nvic_base + NVIC_IPR + i);
130292ec080SUwe Kleine-König 
131292ec080SUwe Kleine-König 	return 0;
132292ec080SUwe Kleine-König }
133292ec080SUwe Kleine-König IRQCHIP_DECLARE(armv7m_nvic, "arm,armv7m-nvic", nvic_of_init);
134