Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Russell King | fa0fe48 | 2006-01-13 21:30:48 +0000 | [diff] [blame] | 2 | /* |
| 3 | * linux/arch/arm/common/vic.c |
| 4 | * |
| 5 | * Copyright (C) 1999 - 2003 ARM Limited |
| 6 | * Copyright (C) 2000 Deep Blue Solutions Ltd |
Russell King | fa0fe48 | 2006-01-13 21:30:48 +0000 | [diff] [blame] | 7 | */ |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 8 | |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 9 | #include <linux/export.h> |
Russell King | fa0fe48 | 2006-01-13 21:30:48 +0000 | [diff] [blame] | 10 | #include <linux/init.h> |
| 11 | #include <linux/list.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 12 | #include <linux/io.h> |
Olof Johansson | bc895b5 | 2013-04-02 15:07:37 -0700 | [diff] [blame] | 13 | #include <linux/irq.h> |
Joel Porquet | 41a83e06 | 2015-07-07 17:11:46 -0400 | [diff] [blame] | 14 | #include <linux/irqchip.h> |
Linus Walleij | f6da9fe | 2014-04-15 10:28:04 +0200 | [diff] [blame] | 15 | #include <linux/irqchip/chained_irq.h> |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 16 | #include <linux/irqdomain.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/of_address.h> |
| 19 | #include <linux/of_irq.h> |
Rafael J. Wysocki | 328f5cc | 2011-04-22 22:02:33 +0200 | [diff] [blame] | 20 | #include <linux/syscore_ops.h> |
Linus Walleij | 59fcf48 | 2009-09-14 12:25:34 +0100 | [diff] [blame] | 21 | #include <linux/device.h> |
Linus Walleij | f17a1f0 | 2009-08-04 01:01:02 +0100 | [diff] [blame] | 22 | #include <linux/amba/bus.h> |
Rob Herring | 9e47b8b | 2013-01-07 09:45:59 -0600 | [diff] [blame] | 23 | #include <linux/irqchip/arm-vic.h> |
Russell King | fa0fe48 | 2006-01-13 21:30:48 +0000 | [diff] [blame] | 24 | |
Jamie Iles | 1558368 | 2011-09-28 09:40:11 +0100 | [diff] [blame] | 25 | #include <asm/exception.h> |
Catalin Marinas | f36a3bb1 | 2013-01-18 15:20:06 +0000 | [diff] [blame] | 26 | #include <asm/irq.h> |
Russell King | fa0fe48 | 2006-01-13 21:30:48 +0000 | [diff] [blame] | 27 | |
Rob Herring | cf21af5 | 2012-11-06 13:14:26 -0600 | [diff] [blame] | 28 | #define VIC_IRQ_STATUS 0x00 |
| 29 | #define VIC_FIQ_STATUS 0x04 |
Linus Walleij | b0b92ab | 2020-06-07 23:51:24 +0200 | [diff] [blame] | 30 | #define VIC_RAW_STATUS 0x08 |
Rob Herring | cf21af5 | 2012-11-06 13:14:26 -0600 | [diff] [blame] | 31 | #define VIC_INT_SELECT 0x0c /* 1 = FIQ, 0 = IRQ */ |
Linus Walleij | b0b92ab | 2020-06-07 23:51:24 +0200 | [diff] [blame] | 32 | #define VIC_INT_ENABLE 0x10 /* 1 = enable, 0 = disable */ |
| 33 | #define VIC_INT_ENABLE_CLEAR 0x14 |
Rob Herring | cf21af5 | 2012-11-06 13:14:26 -0600 | [diff] [blame] | 34 | #define VIC_INT_SOFT 0x18 |
| 35 | #define VIC_INT_SOFT_CLEAR 0x1c |
| 36 | #define VIC_PROTECT 0x20 |
| 37 | #define VIC_PL190_VECT_ADDR 0x30 /* PL190 only */ |
| 38 | #define VIC_PL190_DEF_VECT_ADDR 0x34 /* PL190 only */ |
| 39 | |
| 40 | #define VIC_VECT_ADDR0 0x100 /* 0 to 15 (0..31 PL192) */ |
| 41 | #define VIC_VECT_CNTL0 0x200 /* 0 to 15 (0..31 PL192) */ |
| 42 | #define VIC_ITCR 0x300 /* VIC test control register */ |
| 43 | |
| 44 | #define VIC_VECT_CNTL_ENABLE (1 << 5) |
| 45 | |
| 46 | #define VIC_PL192_VECT_ADDR 0xF00 |
| 47 | |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 48 | /** |
| 49 | * struct vic_device - VIC PM device |
Linus Walleij | e641b98 | 2013-11-21 23:11:29 +0100 | [diff] [blame] | 50 | * @parent_irq: The parent IRQ number of the VIC if cascaded, or 0. |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 51 | * @irq: The IRQ number for the base of the VIC. |
| 52 | * @base: The register base for the VIC. |
Linus Walleij | ce94df9 | 2012-04-20 08:02:36 +0100 | [diff] [blame] | 53 | * @valid_sources: A bitmask of valid interrupts |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 54 | * @resume_sources: A bitmask of interrupts for resume. |
| 55 | * @resume_irqs: The IRQs enabled for resume. |
| 56 | * @int_select: Save for VIC_INT_SELECT. |
| 57 | * @int_enable: Save for VIC_INT_ENABLE. |
| 58 | * @soft_int: Save for VIC_INT_SOFT. |
| 59 | * @protect: Save for VIC_PROTECT. |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 60 | * @domain: The IRQ domain for the VIC. |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 61 | */ |
| 62 | struct vic_device { |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 63 | void __iomem *base; |
| 64 | int irq; |
Linus Walleij | ce94df9 | 2012-04-20 08:02:36 +0100 | [diff] [blame] | 65 | u32 valid_sources; |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 66 | u32 resume_sources; |
| 67 | u32 resume_irqs; |
| 68 | u32 int_select; |
| 69 | u32 int_enable; |
| 70 | u32 soft_int; |
| 71 | u32 protect; |
Grant Likely | 7529495 | 2012-02-14 14:06:57 -0700 | [diff] [blame] | 72 | struct irq_domain *domain; |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /* we cannot allocate memory when VICs are initially registered */ |
| 76 | static struct vic_device vic_devices[CONFIG_ARM_VIC_NR]; |
| 77 | |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 78 | static int vic_id; |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 79 | |
Rob Herring | a036802 | 2012-11-05 16:32:29 -0600 | [diff] [blame] | 80 | static void vic_handle_irq(struct pt_regs *regs); |
| 81 | |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 82 | /** |
| 83 | * vic_init2 - common initialisation code |
| 84 | * @base: Base of the VIC. |
| 85 | * |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 86 | * Common initialisation code for registration |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 87 | * and resume. |
| 88 | */ |
| 89 | static void vic_init2(void __iomem *base) |
| 90 | { |
| 91 | int i; |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 92 | |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 93 | for (i = 0; i < 16; i++) { |
| 94 | void __iomem *reg = base + VIC_VECT_CNTL0 + (i * 4); |
| 95 | writel(VIC_VECT_CNTL_ENABLE | i, reg); |
| 96 | } |
| 97 | |
| 98 | writel(32, base + VIC_PL190_DEF_VECT_ADDR); |
| 99 | } |
| 100 | |
Rafael J. Wysocki | 328f5cc | 2011-04-22 22:02:33 +0200 | [diff] [blame] | 101 | #ifdef CONFIG_PM |
| 102 | static void resume_one_vic(struct vic_device *vic) |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 103 | { |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 104 | void __iomem *base = vic->base; |
| 105 | |
| 106 | printk(KERN_DEBUG "%s: resuming vic at %p\n", __func__, base); |
| 107 | |
| 108 | /* re-initialise static settings */ |
| 109 | vic_init2(base); |
| 110 | |
| 111 | writel(vic->int_select, base + VIC_INT_SELECT); |
| 112 | writel(vic->protect, base + VIC_PROTECT); |
| 113 | |
| 114 | /* set the enabled ints and then clear the non-enabled */ |
| 115 | writel(vic->int_enable, base + VIC_INT_ENABLE); |
| 116 | writel(~vic->int_enable, base + VIC_INT_ENABLE_CLEAR); |
| 117 | |
| 118 | /* and the same for the soft-int register */ |
| 119 | |
| 120 | writel(vic->soft_int, base + VIC_INT_SOFT); |
| 121 | writel(~vic->soft_int, base + VIC_INT_SOFT_CLEAR); |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Rafael J. Wysocki | 328f5cc | 2011-04-22 22:02:33 +0200 | [diff] [blame] | 124 | static void vic_resume(void) |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 125 | { |
Rafael J. Wysocki | 328f5cc | 2011-04-22 22:02:33 +0200 | [diff] [blame] | 126 | int id; |
| 127 | |
| 128 | for (id = vic_id - 1; id >= 0; id--) |
| 129 | resume_one_vic(vic_devices + id); |
| 130 | } |
| 131 | |
| 132 | static void suspend_one_vic(struct vic_device *vic) |
| 133 | { |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 134 | void __iomem *base = vic->base; |
| 135 | |
| 136 | printk(KERN_DEBUG "%s: suspending vic at %p\n", __func__, base); |
| 137 | |
| 138 | vic->int_select = readl(base + VIC_INT_SELECT); |
| 139 | vic->int_enable = readl(base + VIC_INT_ENABLE); |
| 140 | vic->soft_int = readl(base + VIC_INT_SOFT); |
| 141 | vic->protect = readl(base + VIC_PROTECT); |
| 142 | |
| 143 | /* set the interrupts (if any) that are used for |
| 144 | * resuming the system */ |
| 145 | |
| 146 | writel(vic->resume_irqs, base + VIC_INT_ENABLE); |
| 147 | writel(~vic->resume_irqs, base + VIC_INT_ENABLE_CLEAR); |
Rafael J. Wysocki | 328f5cc | 2011-04-22 22:02:33 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static int vic_suspend(void) |
| 151 | { |
| 152 | int id; |
| 153 | |
| 154 | for (id = 0; id < vic_id; id++) |
| 155 | suspend_one_vic(vic_devices + id); |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
Ben Dooks | df042a5 | 2016-06-09 11:30:12 +0100 | [diff] [blame] | 160 | static struct syscore_ops vic_syscore_ops = { |
Rafael J. Wysocki | 328f5cc | 2011-04-22 22:02:33 +0200 | [diff] [blame] | 161 | .suspend = vic_suspend, |
| 162 | .resume = vic_resume, |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | /** |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 166 | * vic_pm_init - initicall to register VIC pm |
| 167 | * |
| 168 | * This is called via late_initcall() to register |
| 169 | * the resources for the VICs due to the early |
| 170 | * nature of the VIC's registration. |
| 171 | */ |
| 172 | static int __init vic_pm_init(void) |
| 173 | { |
Rafael J. Wysocki | 328f5cc | 2011-04-22 22:02:33 +0200 | [diff] [blame] | 174 | if (vic_id > 0) |
| 175 | register_syscore_ops(&vic_syscore_ops); |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 176 | |
| 177 | return 0; |
| 178 | } |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 179 | late_initcall(vic_pm_init); |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 180 | #endif /* CONFIG_PM */ |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 181 | |
Linus Walleij | ce94df9 | 2012-04-20 08:02:36 +0100 | [diff] [blame] | 182 | static struct irq_chip vic_chip; |
| 183 | |
| 184 | static int vic_irqdomain_map(struct irq_domain *d, unsigned int irq, |
| 185 | irq_hw_number_t hwirq) |
| 186 | { |
| 187 | struct vic_device *v = d->host_data; |
| 188 | |
| 189 | /* Skip invalid IRQs, only register handlers for the real ones */ |
| 190 | if (!(v->valid_sources & (1 << hwirq))) |
Grant Likely | d94ea3f | 2013-06-06 14:11:38 +0100 | [diff] [blame] | 191 | return -EPERM; |
Linus Walleij | ce94df9 | 2012-04-20 08:02:36 +0100 | [diff] [blame] | 192 | irq_set_chip_and_handler(irq, &vic_chip, handle_level_irq); |
| 193 | irq_set_chip_data(irq, v->base); |
Rob Herring | d17cab4 | 2015-08-29 18:01:22 -0500 | [diff] [blame] | 194 | irq_set_probe(irq); |
Linus Walleij | ce94df9 | 2012-04-20 08:02:36 +0100 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
Rob Herring | a036802 | 2012-11-05 16:32:29 -0600 | [diff] [blame] | 198 | /* |
| 199 | * Handle each interrupt in a single VIC. Returns non-zero if we've |
| 200 | * handled at least one interrupt. This reads the status register |
| 201 | * before handling each interrupt, which is necessary given that |
| 202 | * handle_IRQ may briefly re-enable interrupts for soft IRQ handling. |
| 203 | */ |
| 204 | static int handle_one_vic(struct vic_device *vic, struct pt_regs *regs) |
| 205 | { |
| 206 | u32 stat, irq; |
| 207 | int handled = 0; |
| 208 | |
| 209 | while ((stat = readl_relaxed(vic->base + VIC_IRQ_STATUS))) { |
| 210 | irq = ffs(stat) - 1; |
Marc Zyngier | 0af83b3 | 2014-08-26 11:03:30 +0100 | [diff] [blame] | 211 | handle_domain_irq(vic->domain, irq, regs); |
Rob Herring | a036802 | 2012-11-05 16:32:29 -0600 | [diff] [blame] | 212 | handled = 1; |
| 213 | } |
| 214 | |
| 215 | return handled; |
| 216 | } |
| 217 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 218 | static void vic_handle_irq_cascaded(struct irq_desc *desc) |
Linus Walleij | e641b98 | 2013-11-21 23:11:29 +0100 | [diff] [blame] | 219 | { |
| 220 | u32 stat, hwirq; |
Linus Walleij | f6da9fe | 2014-04-15 10:28:04 +0200 | [diff] [blame] | 221 | struct irq_chip *host_chip = irq_desc_get_chip(desc); |
Linus Walleij | e641b98 | 2013-11-21 23:11:29 +0100 | [diff] [blame] | 222 | struct vic_device *vic = irq_desc_get_handler_data(desc); |
| 223 | |
Linus Walleij | f6da9fe | 2014-04-15 10:28:04 +0200 | [diff] [blame] | 224 | chained_irq_enter(host_chip, desc); |
| 225 | |
Linus Walleij | e641b98 | 2013-11-21 23:11:29 +0100 | [diff] [blame] | 226 | while ((stat = readl_relaxed(vic->base + VIC_IRQ_STATUS))) { |
| 227 | hwirq = ffs(stat) - 1; |
| 228 | generic_handle_irq(irq_find_mapping(vic->domain, hwirq)); |
| 229 | } |
Linus Walleij | f6da9fe | 2014-04-15 10:28:04 +0200 | [diff] [blame] | 230 | |
| 231 | chained_irq_exit(host_chip, desc); |
Linus Walleij | e641b98 | 2013-11-21 23:11:29 +0100 | [diff] [blame] | 232 | } |
| 233 | |
Rob Herring | a036802 | 2012-11-05 16:32:29 -0600 | [diff] [blame] | 234 | /* |
| 235 | * Keep iterating over all registered VIC's until there are no pending |
| 236 | * interrupts. |
| 237 | */ |
Stephen Boyd | 8783dd3 | 2014-03-04 16:40:30 -0800 | [diff] [blame] | 238 | static void __exception_irq_entry vic_handle_irq(struct pt_regs *regs) |
Rob Herring | a036802 | 2012-11-05 16:32:29 -0600 | [diff] [blame] | 239 | { |
| 240 | int i, handled; |
| 241 | |
| 242 | do { |
| 243 | for (i = 0, handled = 0; i < vic_id; ++i) |
| 244 | handled |= handle_one_vic(&vic_devices[i], regs); |
| 245 | } while (handled); |
| 246 | } |
| 247 | |
Krzysztof Kozlowski | 9600973 | 2015-04-27 21:54:24 +0900 | [diff] [blame] | 248 | static const struct irq_domain_ops vic_irqdomain_ops = { |
Linus Walleij | ce94df9 | 2012-04-20 08:02:36 +0100 | [diff] [blame] | 249 | .map = vic_irqdomain_map, |
| 250 | .xlate = irq_domain_xlate_onetwocell, |
| 251 | }; |
| 252 | |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 253 | /** |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 254 | * vic_register() - Register a VIC. |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 255 | * @base: The base address of the VIC. |
Linus Walleij | e641b98 | 2013-11-21 23:11:29 +0100 | [diff] [blame] | 256 | * @parent_irq: The parent IRQ if cascaded, else 0. |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 257 | * @irq: The base IRQ for the VIC. |
Linus Walleij | fa943be | 2012-04-20 08:02:03 +0100 | [diff] [blame] | 258 | * @valid_sources: bitmask of valid interrupts |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 259 | * @resume_sources: bitmask of interrupts allowed for resume sources. |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 260 | * @node: The device tree node associated with the VIC. |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 261 | * |
| 262 | * Register the VIC with the system device tree so that it can be notified |
| 263 | * of suspend and resume requests and ensure that the correct actions are |
| 264 | * taken to re-instate the settings on resume. |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 265 | * |
| 266 | * This also configures the IRQ domain for the VIC. |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 267 | */ |
Linus Walleij | e641b98 | 2013-11-21 23:11:29 +0100 | [diff] [blame] | 268 | static void __init vic_register(void __iomem *base, unsigned int parent_irq, |
| 269 | unsigned int irq, |
Linus Walleij | fa943be | 2012-04-20 08:02:03 +0100 | [diff] [blame] | 270 | u32 valid_sources, u32 resume_sources, |
| 271 | struct device_node *node) |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 272 | { |
| 273 | struct vic_device *v; |
Linus Walleij | 5ced33b | 2012-12-26 01:39:16 +0100 | [diff] [blame] | 274 | int i; |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 275 | |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 276 | if (vic_id >= ARRAY_SIZE(vic_devices)) { |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 277 | printk(KERN_ERR "%s: too few VICs, increase CONFIG_ARM_VIC_NR\n", __func__); |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 278 | return; |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 279 | } |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 280 | |
| 281 | v = &vic_devices[vic_id]; |
| 282 | v->base = base; |
Linus Walleij | ce94df9 | 2012-04-20 08:02:36 +0100 | [diff] [blame] | 283 | v->valid_sources = valid_sources; |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 284 | v->resume_sources = resume_sources; |
Rob Herring | 7fb7d8a | 2012-11-20 19:55:27 -0600 | [diff] [blame] | 285 | set_handle_irq(vic_handle_irq); |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 286 | vic_id++; |
Linus Walleij | e641b98 | 2013-11-21 23:11:29 +0100 | [diff] [blame] | 287 | |
| 288 | if (parent_irq) { |
Thomas Gleixner | 9f21354 | 2015-06-21 21:11:00 +0200 | [diff] [blame] | 289 | irq_set_chained_handler_and_data(parent_irq, |
| 290 | vic_handle_irq_cascaded, v); |
Linus Walleij | e641b98 | 2013-11-21 23:11:29 +0100 | [diff] [blame] | 291 | } |
| 292 | |
Linus Walleij | 07c9249 | 2012-10-16 18:50:00 +0100 | [diff] [blame] | 293 | v->domain = irq_domain_add_simple(node, fls(valid_sources), irq, |
Linus Walleij | fa943be | 2012-04-20 08:02:03 +0100 | [diff] [blame] | 294 | &vic_irqdomain_ops, v); |
Linus Walleij | 5ced33b | 2012-12-26 01:39:16 +0100 | [diff] [blame] | 295 | /* create an IRQ mapping for each valid IRQ */ |
| 296 | for (i = 0; i < fls(valid_sources); i++) |
| 297 | if (valid_sources & (1 << i)) |
| 298 | irq_create_mapping(v->domain, i); |
Linus Walleij | 3b4df9d | 2013-11-24 20:18:57 +0100 | [diff] [blame] | 299 | /* If no base IRQ was passed, figure out our allocated base */ |
| 300 | if (irq) |
| 301 | v->irq = irq; |
| 302 | else |
| 303 | v->irq = irq_find_mapping(v->domain, 0); |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 304 | } |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 305 | |
Lennert Buytenhek | f013c98 | 2010-11-29 10:20:21 +0100 | [diff] [blame] | 306 | static void vic_ack_irq(struct irq_data *d) |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 307 | { |
Lennert Buytenhek | f013c98 | 2010-11-29 10:20:21 +0100 | [diff] [blame] | 308 | void __iomem *base = irq_data_get_irq_chip_data(d); |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 309 | unsigned int irq = d->hwirq; |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 310 | writel(1 << irq, base + VIC_INT_ENABLE_CLEAR); |
| 311 | /* moreover, clear the soft-triggered, in case it was the reason */ |
| 312 | writel(1 << irq, base + VIC_INT_SOFT_CLEAR); |
| 313 | } |
| 314 | |
Lennert Buytenhek | f013c98 | 2010-11-29 10:20:21 +0100 | [diff] [blame] | 315 | static void vic_mask_irq(struct irq_data *d) |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 316 | { |
Lennert Buytenhek | f013c98 | 2010-11-29 10:20:21 +0100 | [diff] [blame] | 317 | void __iomem *base = irq_data_get_irq_chip_data(d); |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 318 | unsigned int irq = d->hwirq; |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 319 | writel(1 << irq, base + VIC_INT_ENABLE_CLEAR); |
| 320 | } |
| 321 | |
Lennert Buytenhek | f013c98 | 2010-11-29 10:20:21 +0100 | [diff] [blame] | 322 | static void vic_unmask_irq(struct irq_data *d) |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 323 | { |
Lennert Buytenhek | f013c98 | 2010-11-29 10:20:21 +0100 | [diff] [blame] | 324 | void __iomem *base = irq_data_get_irq_chip_data(d); |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 325 | unsigned int irq = d->hwirq; |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 326 | writel(1 << irq, base + VIC_INT_ENABLE); |
| 327 | } |
| 328 | |
| 329 | #if defined(CONFIG_PM) |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 330 | static struct vic_device *vic_from_irq(unsigned int irq) |
| 331 | { |
| 332 | struct vic_device *v = vic_devices; |
| 333 | unsigned int base_irq = irq & ~31; |
| 334 | int id; |
| 335 | |
| 336 | for (id = 0; id < vic_id; id++, v++) { |
| 337 | if (v->irq == base_irq) |
| 338 | return v; |
| 339 | } |
| 340 | |
| 341 | return NULL; |
| 342 | } |
| 343 | |
Lennert Buytenhek | f013c98 | 2010-11-29 10:20:21 +0100 | [diff] [blame] | 344 | static int vic_set_wake(struct irq_data *d, unsigned int on) |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 345 | { |
Lennert Buytenhek | f013c98 | 2010-11-29 10:20:21 +0100 | [diff] [blame] | 346 | struct vic_device *v = vic_from_irq(d->irq); |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 347 | unsigned int off = d->hwirq; |
Ben Dooks | 3f1a567 | 2009-06-02 09:31:03 +0100 | [diff] [blame] | 348 | u32 bit = 1 << off; |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 349 | |
| 350 | if (!v) |
| 351 | return -EINVAL; |
| 352 | |
Ben Dooks | 3f1a567 | 2009-06-02 09:31:03 +0100 | [diff] [blame] | 353 | if (!(bit & v->resume_sources)) |
| 354 | return -EINVAL; |
| 355 | |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 356 | if (on) |
Ben Dooks | 3f1a567 | 2009-06-02 09:31:03 +0100 | [diff] [blame] | 357 | v->resume_irqs |= bit; |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 358 | else |
Ben Dooks | 3f1a567 | 2009-06-02 09:31:03 +0100 | [diff] [blame] | 359 | v->resume_irqs &= ~bit; |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 360 | |
| 361 | return 0; |
| 362 | } |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 363 | #else |
Ben Dooks | c07f87f | 2009-03-24 15:30:07 +0000 | [diff] [blame] | 364 | #define vic_set_wake NULL |
| 365 | #endif /* CONFIG_PM */ |
| 366 | |
David Brownell | 38c677c | 2006-08-01 22:26:25 +0100 | [diff] [blame] | 367 | static struct irq_chip vic_chip = { |
Hartley Sweeten | b0c4c89 | 2010-04-02 18:04:47 +0100 | [diff] [blame] | 368 | .name = "VIC", |
Lennert Buytenhek | f013c98 | 2010-11-29 10:20:21 +0100 | [diff] [blame] | 369 | .irq_ack = vic_ack_irq, |
| 370 | .irq_mask = vic_mask_irq, |
| 371 | .irq_unmask = vic_unmask_irq, |
| 372 | .irq_set_wake = vic_set_wake, |
Russell King | fa0fe48 | 2006-01-13 21:30:48 +0000 | [diff] [blame] | 373 | }; |
| 374 | |
Hartley Sweeten | b0c4c89 | 2010-04-02 18:04:47 +0100 | [diff] [blame] | 375 | static void __init vic_disable(void __iomem *base) |
| 376 | { |
| 377 | writel(0, base + VIC_INT_SELECT); |
| 378 | writel(0, base + VIC_INT_ENABLE); |
| 379 | writel(~0, base + VIC_INT_ENABLE_CLEAR); |
Hartley Sweeten | b0c4c89 | 2010-04-02 18:04:47 +0100 | [diff] [blame] | 380 | writel(0, base + VIC_ITCR); |
| 381 | writel(~0, base + VIC_INT_SOFT_CLEAR); |
| 382 | } |
| 383 | |
| 384 | static void __init vic_clear_interrupts(void __iomem *base) |
| 385 | { |
| 386 | unsigned int i; |
| 387 | |
| 388 | writel(0, base + VIC_PL190_VECT_ADDR); |
| 389 | for (i = 0; i < 19; i++) { |
| 390 | unsigned int value; |
| 391 | |
| 392 | value = readl(base + VIC_PL190_VECT_ADDR); |
| 393 | writel(value, base + VIC_PL190_VECT_ADDR); |
| 394 | } |
| 395 | } |
| 396 | |
Alessandro Rubini | 87e8824 | 2009-07-02 15:28:41 +0100 | [diff] [blame] | 397 | /* |
| 398 | * The PL190 cell from ARM has been modified by ST to handle 64 interrupts. |
| 399 | * The original cell has 32 interrupts, while the modified one has 64, |
| 400 | * replocating two blocks 0x00..0x1f in 0x20..0x3f. In that case |
| 401 | * the probe function is called twice, with base set to offset 000 |
| 402 | * and 020 within the page. We call this "second block". |
| 403 | */ |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 404 | static void __init vic_init_st(void __iomem *base, unsigned int irq_start, |
Jamie Iles | ad62267 | 2011-12-01 11:16:46 +0100 | [diff] [blame] | 405 | u32 vic_sources, struct device_node *node) |
Alessandro Rubini | 87e8824 | 2009-07-02 15:28:41 +0100 | [diff] [blame] | 406 | { |
| 407 | unsigned int i; |
| 408 | int vic_2nd_block = ((unsigned long)base & ~PAGE_MASK) != 0; |
| 409 | |
| 410 | /* Disable all interrupts initially. */ |
Hartley Sweeten | b0c4c89 | 2010-04-02 18:04:47 +0100 | [diff] [blame] | 411 | vic_disable(base); |
Alessandro Rubini | 87e8824 | 2009-07-02 15:28:41 +0100 | [diff] [blame] | 412 | |
| 413 | /* |
| 414 | * Make sure we clear all existing interrupts. The vector registers |
| 415 | * in this cell are after the second block of general registers, |
| 416 | * so we can address them using standard offsets, but only from |
| 417 | * the second base address, which is 0x20 in the page |
| 418 | */ |
| 419 | if (vic_2nd_block) { |
Hartley Sweeten | b0c4c89 | 2010-04-02 18:04:47 +0100 | [diff] [blame] | 420 | vic_clear_interrupts(base); |
Alessandro Rubini | 87e8824 | 2009-07-02 15:28:41 +0100 | [diff] [blame] | 421 | |
Alessandro Rubini | 87e8824 | 2009-07-02 15:28:41 +0100 | [diff] [blame] | 422 | /* ST has 16 vectors as well, but we don't enable them by now */ |
| 423 | for (i = 0; i < 16; i++) { |
| 424 | void __iomem *reg = base + VIC_VECT_CNTL0 + (i * 4); |
| 425 | writel(0, reg); |
| 426 | } |
| 427 | |
| 428 | writel(32, base + VIC_PL190_DEF_VECT_ADDR); |
| 429 | } |
| 430 | |
Linus Walleij | e641b98 | 2013-11-21 23:11:29 +0100 | [diff] [blame] | 431 | vic_register(base, 0, irq_start, vic_sources, 0, node); |
Alessandro Rubini | 87e8824 | 2009-07-02 15:28:41 +0100 | [diff] [blame] | 432 | } |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 433 | |
Linus Walleij | b0b92ab | 2020-06-07 23:51:24 +0200 | [diff] [blame] | 434 | static void __init __vic_init(void __iomem *base, int parent_irq, int irq_start, |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 435 | u32 vic_sources, u32 resume_sources, |
| 436 | struct device_node *node) |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 437 | { |
| 438 | unsigned int i; |
| 439 | u32 cellid = 0; |
| 440 | enum amba_vendor vendor; |
| 441 | |
| 442 | /* Identify which VIC cell this one is, by reading the ID */ |
| 443 | for (i = 0; i < 4; i++) { |
Arnd Bergmann | d4f3add | 2011-09-23 10:13:49 +0200 | [diff] [blame] | 444 | void __iomem *addr; |
| 445 | addr = (void __iomem *)((u32)base & PAGE_MASK) + 0xfe0 + (i * 4); |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 446 | cellid |= (readl(addr) & 0xff) << (8 * i); |
| 447 | } |
| 448 | vendor = (cellid >> 12) & 0xff; |
| 449 | printk(KERN_INFO "VIC @%p: id 0x%08x, vendor 0x%02x\n", |
| 450 | base, cellid, vendor); |
| 451 | |
| 452 | switch(vendor) { |
| 453 | case AMBA_VENDOR_ST: |
Jamie Iles | ad62267 | 2011-12-01 11:16:46 +0100 | [diff] [blame] | 454 | vic_init_st(base, irq_start, vic_sources, node); |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 455 | return; |
| 456 | default: |
| 457 | printk(KERN_WARNING "VIC: unknown vendor, continuing anyways\n"); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 458 | fallthrough; |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 459 | case AMBA_VENDOR_ARM: |
| 460 | break; |
| 461 | } |
| 462 | |
| 463 | /* Disable all interrupts initially. */ |
Hartley Sweeten | b0c4c89 | 2010-04-02 18:04:47 +0100 | [diff] [blame] | 464 | vic_disable(base); |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 465 | |
Hartley Sweeten | b0c4c89 | 2010-04-02 18:04:47 +0100 | [diff] [blame] | 466 | /* Make sure we clear all existing interrupts */ |
| 467 | vic_clear_interrupts(base); |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 468 | |
| 469 | vic_init2(base); |
| 470 | |
Linus Walleij | e641b98 | 2013-11-21 23:11:29 +0100 | [diff] [blame] | 471 | vic_register(base, parent_irq, irq_start, vic_sources, resume_sources, node); |
Hartley Sweeten | bb06b73 | 2010-01-12 19:09:12 +0100 | [diff] [blame] | 472 | } |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 473 | |
| 474 | /** |
| 475 | * vic_init() - initialise a vectored interrupt controller |
| 476 | * @base: iomem base address |
| 477 | * @irq_start: starting interrupt number, must be muliple of 32 |
| 478 | * @vic_sources: bitmask of interrupt sources to allow |
| 479 | * @resume_sources: bitmask of interrupt sources to allow for resume |
| 480 | */ |
| 481 | void __init vic_init(void __iomem *base, unsigned int irq_start, |
| 482 | u32 vic_sources, u32 resume_sources) |
| 483 | { |
Linus Walleij | e641b98 | 2013-11-21 23:11:29 +0100 | [diff] [blame] | 484 | __vic_init(base, 0, irq_start, vic_sources, resume_sources, NULL); |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | #ifdef CONFIG_OF |
Ben Dooks | df042a5 | 2016-06-09 11:30:12 +0100 | [diff] [blame] | 488 | static int __init vic_of_init(struct device_node *node, |
| 489 | struct device_node *parent) |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 490 | { |
| 491 | void __iomem *regs; |
Tomasz Figa | 81e9c17 | 2013-08-26 02:37:24 +0900 | [diff] [blame] | 492 | u32 interrupt_mask = ~0; |
| 493 | u32 wakeup_mask = ~0; |
Linus Walleij | a151110 | 2020-02-19 16:35:43 +0100 | [diff] [blame] | 494 | int parent_irq; |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 495 | |
| 496 | regs = of_iomap(node, 0); |
| 497 | if (WARN_ON(!regs)) |
| 498 | return -EIO; |
| 499 | |
Tomasz Figa | 81e9c17 | 2013-08-26 02:37:24 +0900 | [diff] [blame] | 500 | of_property_read_u32(node, "valid-mask", &interrupt_mask); |
| 501 | of_property_read_u32(node, "valid-wakeup-mask", &wakeup_mask); |
Linus Walleij | a151110 | 2020-02-19 16:35:43 +0100 | [diff] [blame] | 502 | parent_irq = of_irq_get(node, 0); |
| 503 | if (parent_irq < 0) |
| 504 | parent_irq = 0; |
Tomasz Figa | 81e9c17 | 2013-08-26 02:37:24 +0900 | [diff] [blame] | 505 | |
Linus Walleij | 07c9249 | 2012-10-16 18:50:00 +0100 | [diff] [blame] | 506 | /* |
Linus Walleij | 5ced33b | 2012-12-26 01:39:16 +0100 | [diff] [blame] | 507 | * Passing 0 as first IRQ makes the simple domain allocate descriptors |
Linus Walleij | 07c9249 | 2012-10-16 18:50:00 +0100 | [diff] [blame] | 508 | */ |
Linus Walleij | a151110 | 2020-02-19 16:35:43 +0100 | [diff] [blame] | 509 | __vic_init(regs, parent_irq, 0, interrupt_mask, wakeup_mask, node); |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 510 | |
| 511 | return 0; |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 512 | } |
Rob Herring | 44430ec | 2012-10-27 17:25:26 -0500 | [diff] [blame] | 513 | IRQCHIP_DECLARE(arm_pl190_vic, "arm,pl190-vic", vic_of_init); |
| 514 | IRQCHIP_DECLARE(arm_pl192_vic, "arm,pl192-vic", vic_of_init); |
| 515 | IRQCHIP_DECLARE(arm_versatile_vic, "arm,versatile-vic", vic_of_init); |
Jamie Iles | f9b28cc | 2011-09-27 11:00:46 +0100 | [diff] [blame] | 516 | #endif /* CONFIG OF */ |