xref: /openbmc/linux/drivers/irqchip/irq-jcore-aic.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1981b58f6SRich Felker /*
2981b58f6SRich Felker  * J-Core SoC AIC driver
3981b58f6SRich Felker  *
4981b58f6SRich Felker  * Copyright (C) 2015-2016 Smart Energy Instruments, Inc.
5981b58f6SRich Felker  *
6981b58f6SRich Felker  * This file is subject to the terms and conditions of the GNU General Public
7981b58f6SRich Felker  * License.  See the file "COPYING" in the main directory of this archive
8981b58f6SRich Felker  * for more details.
9981b58f6SRich Felker  */
10981b58f6SRich Felker 
11981b58f6SRich Felker #include <linux/irq.h>
12981b58f6SRich Felker #include <linux/io.h>
13981b58f6SRich Felker #include <linux/irqchip.h>
14981b58f6SRich Felker #include <linux/irqdomain.h>
15981b58f6SRich Felker #include <linux/cpu.h>
16981b58f6SRich Felker #include <linux/of.h>
17981b58f6SRich Felker #include <linux/of_address.h>
18981b58f6SRich Felker #include <linux/of_irq.h>
19981b58f6SRich Felker 
20981b58f6SRich Felker #define JCORE_AIC_MAX_HWIRQ	127
21981b58f6SRich Felker #define JCORE_AIC1_MIN_HWIRQ	16
22981b58f6SRich Felker #define JCORE_AIC2_MIN_HWIRQ	64
23981b58f6SRich Felker 
24981b58f6SRich Felker #define JCORE_AIC1_INTPRI_REG	8
25981b58f6SRich Felker 
26981b58f6SRich Felker static struct irq_chip jcore_aic;
27981b58f6SRich Felker 
28c024f06bSRich Felker /*
29c024f06bSRich Felker  * The J-Core AIC1 and AIC2 are cpu-local interrupt controllers and do
30c024f06bSRich Felker  * not distinguish or use distinct irq number ranges for per-cpu event
31c024f06bSRich Felker  * interrupts (timer, IPI). Since information to determine whether a
32c024f06bSRich Felker  * particular irq number should be treated as per-cpu is not available
33c024f06bSRich Felker  * at mapping time, we use a wrapper handler function which chooses
34c024f06bSRich Felker  * the right handler at runtime based on whether IRQF_PERCPU was used
35c024f06bSRich Felker  * when requesting the irq.
36c024f06bSRich Felker  */
37c024f06bSRich Felker 
handle_jcore_irq(struct irq_desc * desc)38c024f06bSRich Felker static void handle_jcore_irq(struct irq_desc *desc)
39c024f06bSRich Felker {
40c024f06bSRich Felker 	if (irqd_is_per_cpu(irq_desc_get_irq_data(desc)))
41c024f06bSRich Felker 		handle_percpu_irq(desc);
42c024f06bSRich Felker 	else
43c024f06bSRich Felker 		handle_simple_irq(desc);
44c024f06bSRich Felker }
45c024f06bSRich Felker 
jcore_aic_irqdomain_map(struct irq_domain * d,unsigned int irq,irq_hw_number_t hwirq)46981b58f6SRich Felker static int jcore_aic_irqdomain_map(struct irq_domain *d, unsigned int irq,
47981b58f6SRich Felker 				   irq_hw_number_t hwirq)
48981b58f6SRich Felker {
49981b58f6SRich Felker 	struct irq_chip *aic = d->host_data;
50981b58f6SRich Felker 
51c024f06bSRich Felker 	irq_set_chip_and_handler(irq, aic, handle_jcore_irq);
52981b58f6SRich Felker 
53981b58f6SRich Felker 	return 0;
54981b58f6SRich Felker }
55981b58f6SRich Felker 
56981b58f6SRich Felker static const struct irq_domain_ops jcore_aic_irqdomain_ops = {
57981b58f6SRich Felker 	.map = jcore_aic_irqdomain_map,
58981b58f6SRich Felker 	.xlate = irq_domain_xlate_onecell,
59981b58f6SRich Felker };
60981b58f6SRich Felker 
noop(struct irq_data * data)61981b58f6SRich Felker static void noop(struct irq_data *data)
62981b58f6SRich Felker {
63981b58f6SRich Felker }
64981b58f6SRich Felker 
aic_irq_of_init(struct device_node * node,struct device_node * parent)6521118df6SWei Yongjun static int __init aic_irq_of_init(struct device_node *node,
6621118df6SWei Yongjun 				  struct device_node *parent)
67981b58f6SRich Felker {
68981b58f6SRich Felker 	unsigned min_irq = JCORE_AIC2_MIN_HWIRQ;
69981b58f6SRich Felker 	unsigned dom_sz = JCORE_AIC_MAX_HWIRQ+1;
70981b58f6SRich Felker 	struct irq_domain *domain;
71*48482294SJohn Paul Adrian Glaubitz 	int ret;
72981b58f6SRich Felker 
73981b58f6SRich Felker 	pr_info("Initializing J-Core AIC\n");
74981b58f6SRich Felker 
75981b58f6SRich Felker 	/* AIC1 needs priority initialization to receive interrupts. */
76981b58f6SRich Felker 	if (of_device_is_compatible(node, "jcore,aic1")) {
77981b58f6SRich Felker 		unsigned cpu;
78981b58f6SRich Felker 
79981b58f6SRich Felker 		for_each_present_cpu(cpu) {
80981b58f6SRich Felker 			void __iomem *base = of_iomap(node, cpu);
81981b58f6SRich Felker 
82981b58f6SRich Felker 			if (!base) {
83981b58f6SRich Felker 				pr_err("Unable to map AIC for cpu %u\n", cpu);
84981b58f6SRich Felker 				return -ENOMEM;
85981b58f6SRich Felker 			}
86981b58f6SRich Felker 			__raw_writel(0xffffffff, base + JCORE_AIC1_INTPRI_REG);
87981b58f6SRich Felker 			iounmap(base);
88981b58f6SRich Felker 		}
89981b58f6SRich Felker 		min_irq = JCORE_AIC1_MIN_HWIRQ;
90981b58f6SRich Felker 	}
91981b58f6SRich Felker 
92981b58f6SRich Felker 	/*
93981b58f6SRich Felker 	 * The irq chip framework requires either mask/unmask or enable/disable
94981b58f6SRich Felker 	 * function pointers to be provided, but the hardware does not have any
95981b58f6SRich Felker 	 * such mechanism; the only interrupt masking is at the cpu level and
96981b58f6SRich Felker 	 * it affects all interrupts. We provide dummy mask/unmask. The hardware
97981b58f6SRich Felker 	 * handles all interrupt control and clears pending status when the cpu
98981b58f6SRich Felker 	 * accepts the interrupt.
99981b58f6SRich Felker 	 */
100981b58f6SRich Felker 	jcore_aic.irq_mask = noop;
101981b58f6SRich Felker 	jcore_aic.irq_unmask = noop;
102981b58f6SRich Felker 	jcore_aic.name = "AIC";
103981b58f6SRich Felker 
104*48482294SJohn Paul Adrian Glaubitz 	ret = irq_alloc_descs(-1, min_irq, dom_sz - min_irq,
105*48482294SJohn Paul Adrian Glaubitz 			      of_node_to_nid(node));
106*48482294SJohn Paul Adrian Glaubitz 
107*48482294SJohn Paul Adrian Glaubitz 	if (ret < 0)
108*48482294SJohn Paul Adrian Glaubitz 		return ret;
109*48482294SJohn Paul Adrian Glaubitz 
1105f8b938bSMarc Zyngier 	domain = irq_domain_add_legacy(node, dom_sz - min_irq, min_irq, min_irq,
1115f8b938bSMarc Zyngier 				       &jcore_aic_irqdomain_ops,
112981b58f6SRich Felker 				       &jcore_aic);
113981b58f6SRich Felker 	if (!domain)
114981b58f6SRich Felker 		return -ENOMEM;
115981b58f6SRich Felker 
116981b58f6SRich Felker 	return 0;
117981b58f6SRich Felker }
118981b58f6SRich Felker 
119981b58f6SRich Felker IRQCHIP_DECLARE(jcore_aic2, "jcore,aic2", aic_irq_of_init);
120981b58f6SRich Felker IRQCHIP_DECLARE(jcore_aic1, "jcore,aic1", aic_irq_of_init);
121