xref: /openbmc/linux/drivers/irqchip/irq-nvic.c (revision d2912cb15bdda8ba4a5dd73396ad62641af2f520)
1*d2912cb1SThomas 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 
48f833f57fSMarc Zyngier static int nvic_irq_domain_translate(struct irq_domain *d,
49f833f57fSMarc Zyngier 				     struct irq_fwspec *fwspec,
50f833f57fSMarc Zyngier 				     unsigned long *hwirq, unsigned int *type)
51f833f57fSMarc Zyngier {
52f833f57fSMarc Zyngier 	if (WARN_ON(fwspec->param_count < 1))
53f833f57fSMarc Zyngier 		return -EINVAL;
54f833f57fSMarc Zyngier 	*hwirq = fwspec->param[0];
55f833f57fSMarc Zyngier 	*type = IRQ_TYPE_NONE;
56f833f57fSMarc Zyngier 	return 0;
57f833f57fSMarc Zyngier }
58f833f57fSMarc Zyngier 
592d9f59f7SStefan Agner static int nvic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
602d9f59f7SStefan Agner 				unsigned int nr_irqs, void *arg)
612d9f59f7SStefan Agner {
622d9f59f7SStefan Agner 	int i, ret;
632d9f59f7SStefan Agner 	irq_hw_number_t hwirq;
642d9f59f7SStefan Agner 	unsigned int type = IRQ_TYPE_NONE;
65f833f57fSMarc Zyngier 	struct irq_fwspec *fwspec = arg;
662d9f59f7SStefan Agner 
67f833f57fSMarc Zyngier 	ret = nvic_irq_domain_translate(domain, fwspec, &hwirq, &type);
682d9f59f7SStefan Agner 	if (ret)
692d9f59f7SStefan Agner 		return ret;
702d9f59f7SStefan Agner 
712d9f59f7SStefan Agner 	for (i = 0; i < nr_irqs; i++)
722d9f59f7SStefan Agner 		irq_map_generic_chip(domain, virq + i, hwirq + i);
732d9f59f7SStefan Agner 
742d9f59f7SStefan Agner 	return 0;
752d9f59f7SStefan Agner }
762d9f59f7SStefan Agner 
772d9f59f7SStefan Agner static const struct irq_domain_ops nvic_irq_domain_ops = {
78f833f57fSMarc Zyngier 	.translate = nvic_irq_domain_translate,
792d9f59f7SStefan Agner 	.alloc = nvic_irq_domain_alloc,
802d9f59f7SStefan Agner 	.free = irq_domain_free_irqs_top,
812d9f59f7SStefan Agner };
822d9f59f7SStefan Agner 
83292ec080SUwe Kleine-König static int __init nvic_of_init(struct device_node *node,
84292ec080SUwe Kleine-König 			       struct device_node *parent)
85292ec080SUwe Kleine-König {
86292ec080SUwe Kleine-König 	unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
87292ec080SUwe Kleine-König 	unsigned int irqs, i, ret, numbanks;
88292ec080SUwe Kleine-König 	void __iomem *nvic_base;
89292ec080SUwe Kleine-König 
90292ec080SUwe Kleine-König 	numbanks = (readl_relaxed(V7M_SCS_ICTR) &
91292ec080SUwe Kleine-König 		    V7M_SCS_ICTR_INTLINESNUM_MASK) + 1;
92292ec080SUwe Kleine-König 
93292ec080SUwe Kleine-König 	nvic_base = of_iomap(node, 0);
94292ec080SUwe Kleine-König 	if (!nvic_base) {
95292ec080SUwe Kleine-König 		pr_warn("unable to map nvic registers\n");
96292ec080SUwe Kleine-König 		return -ENOMEM;
97292ec080SUwe Kleine-König 	}
98292ec080SUwe Kleine-König 
99292ec080SUwe Kleine-König 	irqs = numbanks * 32;
100292ec080SUwe Kleine-König 	if (irqs > NVIC_MAX_IRQ)
101292ec080SUwe Kleine-König 		irqs = NVIC_MAX_IRQ;
102292ec080SUwe Kleine-König 
103292ec080SUwe Kleine-König 	nvic_irq_domain =
1042d9f59f7SStefan Agner 		irq_domain_add_linear(node, irqs, &nvic_irq_domain_ops, NULL);
1052d9f59f7SStefan Agner 
106292ec080SUwe Kleine-König 	if (!nvic_irq_domain) {
107292ec080SUwe Kleine-König 		pr_warn("Failed to allocate irq domain\n");
108292ec080SUwe Kleine-König 		return -ENOMEM;
109292ec080SUwe Kleine-König 	}
110292ec080SUwe Kleine-König 
1115b8aae48SAxel Lin 	ret = irq_alloc_domain_generic_chips(nvic_irq_domain, 32, 1,
112292ec080SUwe Kleine-König 					     "nvic_irq", handle_fasteoi_irq,
113292ec080SUwe Kleine-König 					     clr, 0, IRQ_GC_INIT_MASK_CACHE);
114292ec080SUwe Kleine-König 	if (ret) {
115292ec080SUwe Kleine-König 		pr_warn("Failed to allocate irq chips\n");
116292ec080SUwe Kleine-König 		irq_domain_remove(nvic_irq_domain);
117292ec080SUwe Kleine-König 		return ret;
118292ec080SUwe Kleine-König 	}
119292ec080SUwe Kleine-König 
120292ec080SUwe Kleine-König 	for (i = 0; i < numbanks; ++i) {
121292ec080SUwe Kleine-König 		struct irq_chip_generic *gc;
122292ec080SUwe Kleine-König 
123292ec080SUwe Kleine-König 		gc = irq_get_domain_generic_chip(nvic_irq_domain, 32 * i);
124292ec080SUwe Kleine-König 		gc->reg_base = nvic_base + 4 * i;
125292ec080SUwe Kleine-König 		gc->chip_types[0].regs.enable = NVIC_ISER;
126292ec080SUwe Kleine-König 		gc->chip_types[0].regs.disable = NVIC_ICER;
127292ec080SUwe Kleine-König 		gc->chip_types[0].chip.irq_mask = irq_gc_mask_disable_reg;
128292ec080SUwe Kleine-König 		gc->chip_types[0].chip.irq_unmask = irq_gc_unmask_enable_reg;
1298b53ec26SDaniel Thompson 		/* This is a no-op as end of interrupt is signaled by the
1308b53ec26SDaniel Thompson 		 * exception return sequence.
1318b53ec26SDaniel Thompson 		 */
1328b53ec26SDaniel Thompson 		gc->chip_types[0].chip.irq_eoi = irq_gc_noop;
133292ec080SUwe Kleine-König 
134292ec080SUwe Kleine-König 		/* disable interrupts */
135292ec080SUwe Kleine-König 		writel_relaxed(~0, gc->reg_base + NVIC_ICER);
136292ec080SUwe Kleine-König 	}
137292ec080SUwe Kleine-König 
138292ec080SUwe Kleine-König 	/* Set priority on all interrupts */
139292ec080SUwe Kleine-König 	for (i = 0; i < irqs; i += 4)
140292ec080SUwe Kleine-König 		writel_relaxed(0, nvic_base + NVIC_IPR + i);
141292ec080SUwe Kleine-König 
142292ec080SUwe Kleine-König 	return 0;
143292ec080SUwe Kleine-König }
144292ec080SUwe Kleine-König IRQCHIP_DECLARE(armv7m_nvic, "arm,armv7m-nvic", nvic_of_init);
145