xref: /linux/arch/mips/ralink/irq.c (revision 03c11eb3b16dc0058589751dfd91f254be2be613)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
219d3814eSJohn Crispin /*
319d3814eSJohn Crispin  *
419d3814eSJohn Crispin  * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
597b92108SJohn Crispin  * Copyright (C) 2013 John Crispin <john@phrozen.org>
619d3814eSJohn Crispin  */
719d3814eSJohn Crispin 
819d3814eSJohn Crispin #include <linux/io.h>
919d3814eSJohn Crispin #include <linux/bitops.h>
10*657c45b3SRob Herring #include <linux/of.h>
1119d3814eSJohn Crispin #include <linux/of_address.h>
1219d3814eSJohn Crispin #include <linux/of_irq.h>
1319d3814eSJohn Crispin #include <linux/irqdomain.h>
1419d3814eSJohn Crispin #include <linux/interrupt.h>
1519d3814eSJohn Crispin 
1619d3814eSJohn Crispin #include <asm/irq_cpu.h>
1719d3814eSJohn Crispin #include <asm/mipsregs.h>
1819d3814eSJohn Crispin 
1919d3814eSJohn Crispin #include "common.h"
2019d3814eSJohn Crispin 
2119d3814eSJohn Crispin #define INTC_INT_GLOBAL		BIT(31)
2219d3814eSJohn Crispin 
2319d3814eSJohn Crispin #define RALINK_CPU_IRQ_INTC	(MIPS_CPU_IRQ_BASE + 2)
2448b4aba7SGabor Juhos #define RALINK_CPU_IRQ_PCI	(MIPS_CPU_IRQ_BASE + 4)
2519d3814eSJohn Crispin #define RALINK_CPU_IRQ_FE	(MIPS_CPU_IRQ_BASE + 5)
2619d3814eSJohn Crispin #define RALINK_CPU_IRQ_WIFI	(MIPS_CPU_IRQ_BASE + 6)
2719d3814eSJohn Crispin #define RALINK_CPU_IRQ_COUNTER	(MIPS_CPU_IRQ_BASE + 7)
2819d3814eSJohn Crispin 
2919d3814eSJohn Crispin /* we have a cascade of 8 irqs */
3019d3814eSJohn Crispin #define RALINK_INTC_IRQ_BASE	8
3119d3814eSJohn Crispin 
3219d3814eSJohn Crispin /* we have 32 SoC irqs */
3319d3814eSJohn Crispin #define RALINK_INTC_IRQ_COUNT	32
3419d3814eSJohn Crispin 
3519d3814eSJohn Crispin #define RALINK_INTC_IRQ_PERFC   (RALINK_INTC_IRQ_BASE + 9)
3619d3814eSJohn Crispin 
37b96e6e9fSJohn Crispin enum rt_intc_regs_enum {
38b96e6e9fSJohn Crispin 	INTC_REG_STATUS0 = 0,
39b96e6e9fSJohn Crispin 	INTC_REG_STATUS1,
40b96e6e9fSJohn Crispin 	INTC_REG_TYPE,
41b96e6e9fSJohn Crispin 	INTC_REG_RAW_STATUS,
42b96e6e9fSJohn Crispin 	INTC_REG_ENABLE,
43b96e6e9fSJohn Crispin 	INTC_REG_DISABLE,
44b96e6e9fSJohn Crispin };
45b96e6e9fSJohn Crispin 
46b96e6e9fSJohn Crispin static u32 rt_intc_regs[] = {
47b96e6e9fSJohn Crispin 	[INTC_REG_STATUS0] = 0x00,
48b96e6e9fSJohn Crispin 	[INTC_REG_STATUS1] = 0x04,
49b96e6e9fSJohn Crispin 	[INTC_REG_TYPE] = 0x20,
50b96e6e9fSJohn Crispin 	[INTC_REG_RAW_STATUS] = 0x30,
51b96e6e9fSJohn Crispin 	[INTC_REG_ENABLE] = 0x34,
52b96e6e9fSJohn Crispin 	[INTC_REG_DISABLE] = 0x38,
53b96e6e9fSJohn Crispin };
54b96e6e9fSJohn Crispin 
5519d3814eSJohn Crispin static void __iomem *rt_intc_membase;
56b96e6e9fSJohn Crispin 
57a669efc4SAndrew Bresticker static int rt_perfcount_irq;
5819d3814eSJohn Crispin 
rt_intc_w32(u32 val,unsigned reg)5919d3814eSJohn Crispin static inline void rt_intc_w32(u32 val, unsigned reg)
6019d3814eSJohn Crispin {
61b96e6e9fSJohn Crispin 	__raw_writel(val, rt_intc_membase + rt_intc_regs[reg]);
6219d3814eSJohn Crispin }
6319d3814eSJohn Crispin 
rt_intc_r32(unsigned reg)6419d3814eSJohn Crispin static inline u32 rt_intc_r32(unsigned reg)
6519d3814eSJohn Crispin {
66b96e6e9fSJohn Crispin 	return __raw_readl(rt_intc_membase + rt_intc_regs[reg]);
6719d3814eSJohn Crispin }
6819d3814eSJohn Crispin 
ralink_intc_irq_unmask(struct irq_data * d)6919d3814eSJohn Crispin static void ralink_intc_irq_unmask(struct irq_data *d)
7019d3814eSJohn Crispin {
7119d3814eSJohn Crispin 	rt_intc_w32(BIT(d->hwirq), INTC_REG_ENABLE);
7219d3814eSJohn Crispin }
7319d3814eSJohn Crispin 
ralink_intc_irq_mask(struct irq_data * d)7419d3814eSJohn Crispin static void ralink_intc_irq_mask(struct irq_data *d)
7519d3814eSJohn Crispin {
7619d3814eSJohn Crispin 	rt_intc_w32(BIT(d->hwirq), INTC_REG_DISABLE);
7719d3814eSJohn Crispin }
7819d3814eSJohn Crispin 
7919d3814eSJohn Crispin static struct irq_chip ralink_intc_irq_chip = {
8019d3814eSJohn Crispin 	.name		= "INTC",
8119d3814eSJohn Crispin 	.irq_unmask	= ralink_intc_irq_unmask,
8219d3814eSJohn Crispin 	.irq_mask	= ralink_intc_irq_mask,
8319d3814eSJohn Crispin 	.irq_mask_ack	= ralink_intc_irq_mask,
8419d3814eSJohn Crispin };
8519d3814eSJohn Crispin 
get_c0_perfcount_int(void)86a669efc4SAndrew Bresticker int get_c0_perfcount_int(void)
87a669efc4SAndrew Bresticker {
88a669efc4SAndrew Bresticker 	return rt_perfcount_irq;
89a669efc4SAndrew Bresticker }
900cb0985fSFelix Fietkau EXPORT_SYMBOL_GPL(get_c0_perfcount_int);
91a669efc4SAndrew Bresticker 
get_c0_compare_int(void)92078a55fcSPaul Gortmaker unsigned int get_c0_compare_int(void)
9319d3814eSJohn Crispin {
9419d3814eSJohn Crispin 	return CP0_LEGACY_COMPARE_IRQ;
9519d3814eSJohn Crispin }
9619d3814eSJohn Crispin 
ralink_intc_irq_handler(struct irq_desc * desc)97bd0b9ac4SThomas Gleixner static void ralink_intc_irq_handler(struct irq_desc *desc)
9819d3814eSJohn Crispin {
9919d3814eSJohn Crispin 	u32 pending = rt_intc_r32(INTC_REG_STATUS0);
10019d3814eSJohn Crispin 
10119d3814eSJohn Crispin 	if (pending) {
10225aae561SJiang Liu 		struct irq_domain *domain = irq_desc_get_handler_data(desc);
1030661cb2aSMarc Zyngier 		generic_handle_domain_irq(domain, __ffs(pending));
10419d3814eSJohn Crispin 	} else {
10519d3814eSJohn Crispin 		spurious_interrupt();
10619d3814eSJohn Crispin 	}
10719d3814eSJohn Crispin }
10819d3814eSJohn Crispin 
plat_irq_dispatch(void)10919d3814eSJohn Crispin asmlinkage void plat_irq_dispatch(void)
11019d3814eSJohn Crispin {
11119d3814eSJohn Crispin 	unsigned long pending;
11219d3814eSJohn Crispin 
11319d3814eSJohn Crispin 	pending = read_c0_status() & read_c0_cause() & ST0_IM;
11419d3814eSJohn Crispin 
11519d3814eSJohn Crispin 	if (pending & STATUSF_IP7)
11619d3814eSJohn Crispin 		do_IRQ(RALINK_CPU_IRQ_COUNTER);
11719d3814eSJohn Crispin 
11819d3814eSJohn Crispin 	else if (pending & STATUSF_IP5)
11919d3814eSJohn Crispin 		do_IRQ(RALINK_CPU_IRQ_FE);
12019d3814eSJohn Crispin 
12119d3814eSJohn Crispin 	else if (pending & STATUSF_IP6)
12219d3814eSJohn Crispin 		do_IRQ(RALINK_CPU_IRQ_WIFI);
12319d3814eSJohn Crispin 
12448b4aba7SGabor Juhos 	else if (pending & STATUSF_IP4)
12548b4aba7SGabor Juhos 		do_IRQ(RALINK_CPU_IRQ_PCI);
12648b4aba7SGabor Juhos 
12719d3814eSJohn Crispin 	else if (pending & STATUSF_IP2)
12819d3814eSJohn Crispin 		do_IRQ(RALINK_CPU_IRQ_INTC);
12919d3814eSJohn Crispin 
13019d3814eSJohn Crispin 	else
13119d3814eSJohn Crispin 		spurious_interrupt();
13219d3814eSJohn Crispin }
13319d3814eSJohn Crispin 
intc_map(struct irq_domain * d,unsigned int irq,irq_hw_number_t hw)13419d3814eSJohn Crispin static int intc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw)
13519d3814eSJohn Crispin {
13619d3814eSJohn Crispin 	irq_set_chip_and_handler(irq, &ralink_intc_irq_chip, handle_level_irq);
13719d3814eSJohn Crispin 
13819d3814eSJohn Crispin 	return 0;
13919d3814eSJohn Crispin }
14019d3814eSJohn Crispin 
14119d3814eSJohn Crispin static const struct irq_domain_ops irq_domain_ops = {
14219d3814eSJohn Crispin 	.xlate = irq_domain_xlate_onecell,
14319d3814eSJohn Crispin 	.map = intc_map,
14419d3814eSJohn Crispin };
14519d3814eSJohn Crispin 
intc_of_init(struct device_node * node,struct device_node * parent)14619d3814eSJohn Crispin static int __init intc_of_init(struct device_node *node,
14719d3814eSJohn Crispin 			       struct device_node *parent)
14819d3814eSJohn Crispin {
14919d3814eSJohn Crispin 	struct resource res;
15019d3814eSJohn Crispin 	struct irq_domain *domain;
151d3d2b420SGabor Juhos 	int irq;
15219d3814eSJohn Crispin 
153b96e6e9fSJohn Crispin 	if (!of_property_read_u32_array(node, "ralink,intc-registers",
154b96e6e9fSJohn Crispin 					rt_intc_regs, 6))
155b96e6e9fSJohn Crispin 		pr_info("intc: using register map from devicetree\n");
156b96e6e9fSJohn Crispin 
157d3d2b420SGabor Juhos 	irq = irq_of_parse_and_map(node, 0);
158d3d2b420SGabor Juhos 	if (!irq)
159d3d2b420SGabor Juhos 		panic("Failed to get INTC IRQ");
16019d3814eSJohn Crispin 
16119d3814eSJohn Crispin 	if (of_address_to_resource(node, 0, &res))
16219d3814eSJohn Crispin 		panic("Failed to get intc memory range");
16319d3814eSJohn Crispin 
1646d2700a9SArnd Bergmann 	if (!request_mem_region(res.start, resource_size(&res),
1656d2700a9SArnd Bergmann 				res.name))
16619d3814eSJohn Crispin 		pr_err("Failed to request intc memory");
16719d3814eSJohn Crispin 
1684bdc0d67SChristoph Hellwig 	rt_intc_membase = ioremap(res.start,
16919d3814eSJohn Crispin 					resource_size(&res));
17019d3814eSJohn Crispin 	if (!rt_intc_membase)
17119d3814eSJohn Crispin 		panic("Failed to remap intc memory");
17219d3814eSJohn Crispin 
17319d3814eSJohn Crispin 	/* disable all interrupts */
17419d3814eSJohn Crispin 	rt_intc_w32(~0, INTC_REG_DISABLE);
17519d3814eSJohn Crispin 
17619d3814eSJohn Crispin 	/* route all INTC interrupts to MIPS HW0 interrupt */
17719d3814eSJohn Crispin 	rt_intc_w32(0, INTC_REG_TYPE);
17819d3814eSJohn Crispin 
17919d3814eSJohn Crispin 	domain = irq_domain_add_legacy(node, RALINK_INTC_IRQ_COUNT,
18019d3814eSJohn Crispin 			RALINK_INTC_IRQ_BASE, 0, &irq_domain_ops, NULL);
18119d3814eSJohn Crispin 	if (!domain)
18219d3814eSJohn Crispin 		panic("Failed to add irqdomain");
18319d3814eSJohn Crispin 
18419d3814eSJohn Crispin 	rt_intc_w32(INTC_INT_GLOBAL, INTC_REG_ENABLE);
18519d3814eSJohn Crispin 
1865c1642e4SThomas Gleixner 	irq_set_chained_handler_and_data(irq, ralink_intc_irq_handler, domain);
18719d3814eSJohn Crispin 
18829473822SJohn Crispin 	/* tell the kernel which irq is used for performance monitoring */
189a669efc4SAndrew Bresticker 	rt_perfcount_irq = irq_create_mapping(domain, 9);
19019d3814eSJohn Crispin 
19119d3814eSJohn Crispin 	return 0;
19219d3814eSJohn Crispin }
19319d3814eSJohn Crispin 
19419d3814eSJohn Crispin static struct of_device_id __initdata of_irq_ids[] = {
195afe8dc25SAndrew Bresticker 	{ .compatible = "mti,cpu-interrupt-controller", .data = mips_cpu_irq_of_init },
19619d3814eSJohn Crispin 	{ .compatible = "ralink,rt2880-intc", .data = intc_of_init },
19719d3814eSJohn Crispin 	{},
19819d3814eSJohn Crispin };
19919d3814eSJohn Crispin 
arch_init_irq(void)20019d3814eSJohn Crispin void __init arch_init_irq(void)
20119d3814eSJohn Crispin {
20219d3814eSJohn Crispin 	of_irq_init(of_irq_ids);
20319d3814eSJohn Crispin }
20419d3814eSJohn Crispin 
205