John Crispin | 19d3814 | 2013-01-20 22:00:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify it |
| 3 | * under the terms of the GNU General Public License version 2 as published |
| 4 | * by the Free Software Foundation. |
| 5 | * |
| 6 | * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org> |
| 7 | * Copyright (C) 2013 John Crispin <blogic@openwrt.org> |
| 8 | */ |
| 9 | |
| 10 | #include <linux/io.h> |
| 11 | #include <linux/bitops.h> |
| 12 | #include <linux/of_platform.h> |
| 13 | #include <linux/of_address.h> |
| 14 | #include <linux/of_irq.h> |
| 15 | #include <linux/irqdomain.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | |
| 18 | #include <asm/irq_cpu.h> |
| 19 | #include <asm/mipsregs.h> |
| 20 | |
| 21 | #include "common.h" |
| 22 | |
John Crispin | 19d3814 | 2013-01-20 22:00:50 +0100 | [diff] [blame] | 23 | #define INTC_INT_GLOBAL BIT(31) |
| 24 | |
| 25 | #define RALINK_CPU_IRQ_INTC (MIPS_CPU_IRQ_BASE + 2) |
Gabor Juhos | 48b4aba | 2013-04-10 09:07:27 +0200 | [diff] [blame] | 26 | #define RALINK_CPU_IRQ_PCI (MIPS_CPU_IRQ_BASE + 4) |
John Crispin | 19d3814 | 2013-01-20 22:00:50 +0100 | [diff] [blame] | 27 | #define RALINK_CPU_IRQ_FE (MIPS_CPU_IRQ_BASE + 5) |
| 28 | #define RALINK_CPU_IRQ_WIFI (MIPS_CPU_IRQ_BASE + 6) |
| 29 | #define RALINK_CPU_IRQ_COUNTER (MIPS_CPU_IRQ_BASE + 7) |
| 30 | |
| 31 | /* we have a cascade of 8 irqs */ |
| 32 | #define RALINK_INTC_IRQ_BASE 8 |
| 33 | |
| 34 | /* we have 32 SoC irqs */ |
| 35 | #define RALINK_INTC_IRQ_COUNT 32 |
| 36 | |
| 37 | #define RALINK_INTC_IRQ_PERFC (RALINK_INTC_IRQ_BASE + 9) |
| 38 | |
John Crispin | b96e6e9 | 2014-10-26 11:26:04 +0100 | [diff] [blame] | 39 | enum rt_intc_regs_enum { |
| 40 | INTC_REG_STATUS0 = 0, |
| 41 | INTC_REG_STATUS1, |
| 42 | INTC_REG_TYPE, |
| 43 | INTC_REG_RAW_STATUS, |
| 44 | INTC_REG_ENABLE, |
| 45 | INTC_REG_DISABLE, |
| 46 | }; |
| 47 | |
| 48 | static u32 rt_intc_regs[] = { |
| 49 | [INTC_REG_STATUS0] = 0x00, |
| 50 | [INTC_REG_STATUS1] = 0x04, |
| 51 | [INTC_REG_TYPE] = 0x20, |
| 52 | [INTC_REG_RAW_STATUS] = 0x30, |
| 53 | [INTC_REG_ENABLE] = 0x34, |
| 54 | [INTC_REG_DISABLE] = 0x38, |
| 55 | }; |
| 56 | |
John Crispin | 19d3814 | 2013-01-20 22:00:50 +0100 | [diff] [blame] | 57 | static void __iomem *rt_intc_membase; |
John Crispin | b96e6e9 | 2014-10-26 11:26:04 +0100 | [diff] [blame] | 58 | |
Andrew Bresticker | a669efc | 2014-09-18 14:47:12 -0700 | [diff] [blame] | 59 | static int rt_perfcount_irq; |
John Crispin | 19d3814 | 2013-01-20 22:00:50 +0100 | [diff] [blame] | 60 | |
| 61 | static inline void rt_intc_w32(u32 val, unsigned reg) |
| 62 | { |
John Crispin | b96e6e9 | 2014-10-26 11:26:04 +0100 | [diff] [blame] | 63 | __raw_writel(val, rt_intc_membase + rt_intc_regs[reg]); |
John Crispin | 19d3814 | 2013-01-20 22:00:50 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static inline u32 rt_intc_r32(unsigned reg) |
| 67 | { |
John Crispin | b96e6e9 | 2014-10-26 11:26:04 +0100 | [diff] [blame] | 68 | return __raw_readl(rt_intc_membase + rt_intc_regs[reg]); |
John Crispin | 19d3814 | 2013-01-20 22:00:50 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static void ralink_intc_irq_unmask(struct irq_data *d) |
| 72 | { |
| 73 | rt_intc_w32(BIT(d->hwirq), INTC_REG_ENABLE); |
| 74 | } |
| 75 | |
| 76 | static void ralink_intc_irq_mask(struct irq_data *d) |
| 77 | { |
| 78 | rt_intc_w32(BIT(d->hwirq), INTC_REG_DISABLE); |
| 79 | } |
| 80 | |
| 81 | static struct irq_chip ralink_intc_irq_chip = { |
| 82 | .name = "INTC", |
| 83 | .irq_unmask = ralink_intc_irq_unmask, |
| 84 | .irq_mask = ralink_intc_irq_mask, |
| 85 | .irq_mask_ack = ralink_intc_irq_mask, |
| 86 | }; |
| 87 | |
Andrew Bresticker | a669efc | 2014-09-18 14:47:12 -0700 | [diff] [blame] | 88 | int get_c0_perfcount_int(void) |
| 89 | { |
| 90 | return rt_perfcount_irq; |
| 91 | } |
| 92 | |
Paul Gortmaker | 078a55f | 2013-06-18 13:38:59 +0000 | [diff] [blame] | 93 | unsigned int get_c0_compare_int(void) |
John Crispin | 19d3814 | 2013-01-20 22:00:50 +0100 | [diff] [blame] | 94 | { |
| 95 | return CP0_LEGACY_COMPARE_IRQ; |
| 96 | } |
| 97 | |
| 98 | static void ralink_intc_irq_handler(unsigned int irq, struct irq_desc *desc) |
| 99 | { |
| 100 | u32 pending = rt_intc_r32(INTC_REG_STATUS0); |
| 101 | |
| 102 | if (pending) { |
Jiang Liu | 25aae56 | 2015-05-20 17:59:51 +0800 | [diff] [blame] | 103 | struct irq_domain *domain = irq_desc_get_handler_data(desc); |
John Crispin | 19d3814 | 2013-01-20 22:00:50 +0100 | [diff] [blame] | 104 | generic_handle_irq(irq_find_mapping(domain, __ffs(pending))); |
| 105 | } else { |
| 106 | spurious_interrupt(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | asmlinkage void plat_irq_dispatch(void) |
| 111 | { |
| 112 | unsigned long pending; |
| 113 | |
| 114 | pending = read_c0_status() & read_c0_cause() & ST0_IM; |
| 115 | |
| 116 | if (pending & STATUSF_IP7) |
| 117 | do_IRQ(RALINK_CPU_IRQ_COUNTER); |
| 118 | |
| 119 | else if (pending & STATUSF_IP5) |
| 120 | do_IRQ(RALINK_CPU_IRQ_FE); |
| 121 | |
| 122 | else if (pending & STATUSF_IP6) |
| 123 | do_IRQ(RALINK_CPU_IRQ_WIFI); |
| 124 | |
Gabor Juhos | 48b4aba | 2013-04-10 09:07:27 +0200 | [diff] [blame] | 125 | else if (pending & STATUSF_IP4) |
| 126 | do_IRQ(RALINK_CPU_IRQ_PCI); |
| 127 | |
John Crispin | 19d3814 | 2013-01-20 22:00:50 +0100 | [diff] [blame] | 128 | else if (pending & STATUSF_IP2) |
| 129 | do_IRQ(RALINK_CPU_IRQ_INTC); |
| 130 | |
| 131 | else |
| 132 | spurious_interrupt(); |
| 133 | } |
| 134 | |
| 135 | static int intc_map(struct irq_domain *d, unsigned int irq, irq_hw_number_t hw) |
| 136 | { |
| 137 | irq_set_chip_and_handler(irq, &ralink_intc_irq_chip, handle_level_irq); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static const struct irq_domain_ops irq_domain_ops = { |
| 143 | .xlate = irq_domain_xlate_onecell, |
| 144 | .map = intc_map, |
| 145 | }; |
| 146 | |
| 147 | static int __init intc_of_init(struct device_node *node, |
| 148 | struct device_node *parent) |
| 149 | { |
| 150 | struct resource res; |
| 151 | struct irq_domain *domain; |
Gabor Juhos | d3d2b42 | 2013-01-31 20:43:30 +0100 | [diff] [blame] | 152 | int irq; |
John Crispin | 19d3814 | 2013-01-20 22:00:50 +0100 | [diff] [blame] | 153 | |
John Crispin | b96e6e9 | 2014-10-26 11:26:04 +0100 | [diff] [blame] | 154 | if (!of_property_read_u32_array(node, "ralink,intc-registers", |
| 155 | rt_intc_regs, 6)) |
| 156 | pr_info("intc: using register map from devicetree\n"); |
| 157 | |
Gabor Juhos | d3d2b42 | 2013-01-31 20:43:30 +0100 | [diff] [blame] | 158 | irq = irq_of_parse_and_map(node, 0); |
| 159 | if (!irq) |
| 160 | panic("Failed to get INTC IRQ"); |
John Crispin | 19d3814 | 2013-01-20 22:00:50 +0100 | [diff] [blame] | 161 | |
| 162 | if (of_address_to_resource(node, 0, &res)) |
| 163 | panic("Failed to get intc memory range"); |
| 164 | |
| 165 | if (request_mem_region(res.start, resource_size(&res), |
| 166 | res.name) < 0) |
| 167 | pr_err("Failed to request intc memory"); |
| 168 | |
| 169 | rt_intc_membase = ioremap_nocache(res.start, |
| 170 | resource_size(&res)); |
| 171 | if (!rt_intc_membase) |
| 172 | panic("Failed to remap intc memory"); |
| 173 | |
| 174 | /* disable all interrupts */ |
| 175 | rt_intc_w32(~0, INTC_REG_DISABLE); |
| 176 | |
| 177 | /* route all INTC interrupts to MIPS HW0 interrupt */ |
| 178 | rt_intc_w32(0, INTC_REG_TYPE); |
| 179 | |
| 180 | domain = irq_domain_add_legacy(node, RALINK_INTC_IRQ_COUNT, |
| 181 | RALINK_INTC_IRQ_BASE, 0, &irq_domain_ops, NULL); |
| 182 | if (!domain) |
| 183 | panic("Failed to add irqdomain"); |
| 184 | |
| 185 | rt_intc_w32(INTC_INT_GLOBAL, INTC_REG_ENABLE); |
| 186 | |
Thomas Gleixner | 5c1642e | 2015-06-21 21:00:43 +0200 | [diff] [blame] | 187 | irq_set_chained_handler_and_data(irq, ralink_intc_irq_handler, domain); |
John Crispin | 19d3814 | 2013-01-20 22:00:50 +0100 | [diff] [blame] | 188 | |
John Crispin | 2947382 | 2013-03-16 16:28:54 +0100 | [diff] [blame] | 189 | /* tell the kernel which irq is used for performance monitoring */ |
Andrew Bresticker | a669efc | 2014-09-18 14:47:12 -0700 | [diff] [blame] | 190 | rt_perfcount_irq = irq_create_mapping(domain, 9); |
John Crispin | 19d3814 | 2013-01-20 22:00:50 +0100 | [diff] [blame] | 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static struct of_device_id __initdata of_irq_ids[] = { |
Andrew Bresticker | afe8dc2 | 2014-09-18 14:47:08 -0700 | [diff] [blame] | 196 | { .compatible = "mti,cpu-interrupt-controller", .data = mips_cpu_irq_of_init }, |
John Crispin | 19d3814 | 2013-01-20 22:00:50 +0100 | [diff] [blame] | 197 | { .compatible = "ralink,rt2880-intc", .data = intc_of_init }, |
| 198 | {}, |
| 199 | }; |
| 200 | |
| 201 | void __init arch_init_irq(void) |
| 202 | { |
| 203 | of_irq_init(of_irq_ids); |
| 204 | } |
| 205 | |