Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic Broadcom Set Top Box Level 2 Interrupt controller driver |
| 3 | * |
Doug Berger | 49aa6ef | 2017-09-18 17:59:58 -0700 | [diff] [blame] | 4 | * Copyright (C) 2014-2017 Broadcom |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 17 | |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/platform_device.h> |
Kevin Cernekee | 05f1275 | 2014-11-06 22:44:20 -0800 | [diff] [blame] | 22 | #include <linux/spinlock.h> |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 23 | #include <linux/of.h> |
| 24 | #include <linux/of_irq.h> |
| 25 | #include <linux/of_address.h> |
| 26 | #include <linux/of_platform.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/irq.h> |
| 29 | #include <linux/io.h> |
| 30 | #include <linux/irqdomain.h> |
| 31 | #include <linux/irqchip.h> |
| 32 | #include <linux/irqchip/chained_irq.h> |
| 33 | |
Doug Berger | c0ca726 | 2017-09-18 18:00:00 -0700 | [diff] [blame] | 34 | struct brcmstb_intc_init_params { |
| 35 | irq_flow_handler_t handler; |
| 36 | int cpu_status; |
| 37 | int cpu_clear; |
| 38 | int cpu_mask_status; |
| 39 | int cpu_mask_set; |
| 40 | int cpu_mask_clear; |
| 41 | }; |
| 42 | |
| 43 | /* Register offsets in the L2 latched interrupt controller */ |
| 44 | static const struct brcmstb_intc_init_params l2_edge_intc_init = { |
| 45 | .handler = handle_edge_irq, |
| 46 | .cpu_status = 0x00, |
| 47 | .cpu_clear = 0x08, |
| 48 | .cpu_mask_status = 0x0c, |
| 49 | .cpu_mask_set = 0x10, |
| 50 | .cpu_mask_clear = 0x14 |
| 51 | }; |
| 52 | |
| 53 | /* Register offsets in the L2 level interrupt controller */ |
| 54 | static const struct brcmstb_intc_init_params l2_lvl_intc_init = { |
| 55 | .handler = handle_level_irq, |
| 56 | .cpu_status = 0x00, |
| 57 | .cpu_clear = -1, /* Register not present */ |
| 58 | .cpu_mask_status = 0x04, |
| 59 | .cpu_mask_set = 0x08, |
| 60 | .cpu_mask_clear = 0x0C |
| 61 | }; |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 62 | |
| 63 | /* L2 intc private data structure */ |
| 64 | struct brcmstb_l2_intc_data { |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 65 | struct irq_domain *domain; |
Doug Berger | 49aa6ef | 2017-09-18 17:59:58 -0700 | [diff] [blame] | 66 | struct irq_chip_generic *gc; |
Doug Berger | 8480ca4 | 2017-09-18 17:59:59 -0700 | [diff] [blame] | 67 | int status_offset; |
| 68 | int mask_offset; |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 69 | bool can_wake; |
| 70 | u32 saved_mask; /* for suspend/resume */ |
| 71 | }; |
| 72 | |
Doug Berger | 49aa6ef | 2017-09-18 17:59:58 -0700 | [diff] [blame] | 73 | /** |
| 74 | * brcmstb_l2_mask_and_ack - Mask and ack pending interrupt |
| 75 | * @d: irq_data |
| 76 | * |
| 77 | * Chip has separate enable/disable registers instead of a single mask |
| 78 | * register and pending interrupt is acknowledged by setting a bit. |
| 79 | * |
| 80 | * Note: This function is generic and could easily be added to the |
| 81 | * generic irqchip implementation if there ever becomes a will to do so. |
| 82 | * Perhaps with a name like irq_gc_mask_disable_and_ack_set(). |
| 83 | * |
| 84 | * e.g.: https://patchwork.kernel.org/patch/9831047/ |
| 85 | */ |
| 86 | static void brcmstb_l2_mask_and_ack(struct irq_data *d) |
| 87 | { |
| 88 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
| 89 | struct irq_chip_type *ct = irq_data_get_chip_type(d); |
| 90 | u32 mask = d->mask; |
| 91 | |
| 92 | irq_gc_lock(gc); |
| 93 | irq_reg_writel(gc, mask, ct->regs.disable); |
| 94 | *ct->mask_cache &= ~mask; |
| 95 | irq_reg_writel(gc, mask, ct->regs.ack); |
| 96 | irq_gc_unlock(gc); |
| 97 | } |
| 98 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 99 | static void brcmstb_l2_intc_irq_handle(struct irq_desc *desc) |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 100 | { |
| 101 | struct brcmstb_l2_intc_data *b = irq_desc_get_handler_data(desc); |
| 102 | struct irq_chip *chip = irq_desc_get_chip(desc); |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 103 | unsigned int irq; |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 104 | u32 status; |
| 105 | |
| 106 | chained_irq_enter(chip, desc); |
| 107 | |
Doug Berger | 8480ca4 | 2017-09-18 17:59:59 -0700 | [diff] [blame] | 108 | status = irq_reg_readl(b->gc, b->status_offset) & |
| 109 | ~(irq_reg_readl(b->gc, b->mask_offset)); |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 110 | |
| 111 | if (status == 0) { |
Kevin Cernekee | 05f1275 | 2014-11-06 22:44:20 -0800 | [diff] [blame] | 112 | raw_spin_lock(&desc->lock); |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 113 | handle_bad_irq(desc); |
Kevin Cernekee | 05f1275 | 2014-11-06 22:44:20 -0800 | [diff] [blame] | 114 | raw_spin_unlock(&desc->lock); |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 115 | goto out; |
| 116 | } |
| 117 | |
| 118 | do { |
| 119 | irq = ffs(status) - 1; |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 120 | status &= ~(1 << irq); |
Doug Berger | 49aa6ef | 2017-09-18 17:59:58 -0700 | [diff] [blame] | 121 | generic_handle_irq(irq_linear_revmap(b->domain, irq)); |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 122 | } while (status); |
| 123 | out: |
| 124 | chained_irq_exit(chip, desc); |
| 125 | } |
| 126 | |
| 127 | static void brcmstb_l2_intc_suspend(struct irq_data *d) |
| 128 | { |
| 129 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
Doug Berger | 8480ca4 | 2017-09-18 17:59:59 -0700 | [diff] [blame] | 130 | struct irq_chip_type *ct = irq_data_get_chip_type(d); |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 131 | struct brcmstb_l2_intc_data *b = gc->private; |
Doug Berger | 3351788 | 2019-02-20 14:15:28 -0800 | [diff] [blame^] | 132 | unsigned long flags; |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 133 | |
Doug Berger | 3351788 | 2019-02-20 14:15:28 -0800 | [diff] [blame^] | 134 | irq_gc_lock_irqsave(gc, flags); |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 135 | /* Save the current mask */ |
Doug Berger | 8480ca4 | 2017-09-18 17:59:59 -0700 | [diff] [blame] | 136 | b->saved_mask = irq_reg_readl(gc, ct->regs.mask); |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 137 | |
| 138 | if (b->can_wake) { |
| 139 | /* Program the wakeup mask */ |
Doug Berger | 8480ca4 | 2017-09-18 17:59:59 -0700 | [diff] [blame] | 140 | irq_reg_writel(gc, ~gc->wake_active, ct->regs.disable); |
| 141 | irq_reg_writel(gc, gc->wake_active, ct->regs.enable); |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 142 | } |
Doug Berger | 3351788 | 2019-02-20 14:15:28 -0800 | [diff] [blame^] | 143 | irq_gc_unlock_irqrestore(gc, flags); |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static void brcmstb_l2_intc_resume(struct irq_data *d) |
| 147 | { |
| 148 | struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); |
Doug Berger | 8480ca4 | 2017-09-18 17:59:59 -0700 | [diff] [blame] | 149 | struct irq_chip_type *ct = irq_data_get_chip_type(d); |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 150 | struct brcmstb_l2_intc_data *b = gc->private; |
Doug Berger | 3351788 | 2019-02-20 14:15:28 -0800 | [diff] [blame^] | 151 | unsigned long flags; |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 152 | |
Doug Berger | 3351788 | 2019-02-20 14:15:28 -0800 | [diff] [blame^] | 153 | irq_gc_lock_irqsave(gc, flags); |
Doug Berger | c0ca726 | 2017-09-18 18:00:00 -0700 | [diff] [blame] | 154 | if (ct->chip.irq_ack) { |
Doug Berger | 8480ca4 | 2017-09-18 17:59:59 -0700 | [diff] [blame] | 155 | /* Clear unmasked non-wakeup interrupts */ |
| 156 | irq_reg_writel(gc, ~b->saved_mask & ~gc->wake_active, |
| 157 | ct->regs.ack); |
| 158 | } |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 159 | |
| 160 | /* Restore the saved mask */ |
Doug Berger | 8480ca4 | 2017-09-18 17:59:59 -0700 | [diff] [blame] | 161 | irq_reg_writel(gc, b->saved_mask, ct->regs.disable); |
| 162 | irq_reg_writel(gc, ~b->saved_mask, ct->regs.enable); |
Doug Berger | 3351788 | 2019-02-20 14:15:28 -0800 | [diff] [blame^] | 163 | irq_gc_unlock_irqrestore(gc, flags); |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Ben Dooks | 2ae9add | 2016-06-08 19:02:20 +0100 | [diff] [blame] | 166 | static int __init brcmstb_l2_intc_of_init(struct device_node *np, |
Doug Berger | c0ca726 | 2017-09-18 18:00:00 -0700 | [diff] [blame] | 167 | struct device_node *parent, |
| 168 | const struct brcmstb_intc_init_params |
| 169 | *init_params) |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 170 | { |
| 171 | unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN; |
| 172 | struct brcmstb_l2_intc_data *data; |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 173 | struct irq_chip_type *ct; |
| 174 | int ret; |
Kevin Cernekee | 1abbdba | 2014-11-06 22:44:29 -0800 | [diff] [blame] | 175 | unsigned int flags; |
Doug Berger | 49aa6ef | 2017-09-18 17:59:58 -0700 | [diff] [blame] | 176 | int parent_irq; |
| 177 | void __iomem *base; |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 178 | |
| 179 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 180 | if (!data) |
| 181 | return -ENOMEM; |
| 182 | |
Doug Berger | 49aa6ef | 2017-09-18 17:59:58 -0700 | [diff] [blame] | 183 | base = of_iomap(np, 0); |
| 184 | if (!base) { |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 185 | pr_err("failed to remap intc L2 registers\n"); |
| 186 | ret = -ENOMEM; |
| 187 | goto out_free; |
| 188 | } |
| 189 | |
| 190 | /* Disable all interrupts by default */ |
Doug Berger | c0ca726 | 2017-09-18 18:00:00 -0700 | [diff] [blame] | 191 | writel(0xffffffff, base + init_params->cpu_mask_set); |
Brian Norris | c9ae71e | 2014-12-25 09:49:02 -0800 | [diff] [blame] | 192 | |
| 193 | /* Wakeup interrupts may be retained from S5 (cold boot) */ |
| 194 | data->can_wake = of_property_read_bool(np, "brcm,irq-can-wake"); |
Doug Berger | c0ca726 | 2017-09-18 18:00:00 -0700 | [diff] [blame] | 195 | if (!data->can_wake && (init_params->cpu_clear >= 0)) |
| 196 | writel(0xffffffff, base + init_params->cpu_clear); |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 197 | |
Doug Berger | 49aa6ef | 2017-09-18 17:59:58 -0700 | [diff] [blame] | 198 | parent_irq = irq_of_parse_and_map(np, 0); |
| 199 | if (!parent_irq) { |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 200 | pr_err("failed to find parent interrupt\n"); |
Dmitry Torokhov | d99ba44 | 2014-11-14 14:16:42 -0800 | [diff] [blame] | 201 | ret = -EINVAL; |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 202 | goto out_unmap; |
| 203 | } |
| 204 | |
| 205 | data->domain = irq_domain_add_linear(np, 32, |
| 206 | &irq_generic_chip_ops, NULL); |
| 207 | if (!data->domain) { |
| 208 | ret = -ENOMEM; |
| 209 | goto out_unmap; |
| 210 | } |
| 211 | |
Kevin Cernekee | 1abbdba | 2014-11-06 22:44:29 -0800 | [diff] [blame] | 212 | /* MIPS chips strapped for BE will automagically configure the |
| 213 | * peripheral registers for CPU-native byte order. |
| 214 | */ |
| 215 | flags = 0; |
| 216 | if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) |
| 217 | flags |= IRQ_GC_BE_IO; |
| 218 | |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 219 | /* Allocate a single Generic IRQ chip for this node */ |
| 220 | ret = irq_alloc_domain_generic_chips(data->domain, 32, 1, |
Doug Berger | c0ca726 | 2017-09-18 18:00:00 -0700 | [diff] [blame] | 221 | np->full_name, init_params->handler, clr, 0, flags); |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 222 | if (ret) { |
| 223 | pr_err("failed to allocate generic irq chip\n"); |
| 224 | goto out_free_domain; |
| 225 | } |
| 226 | |
| 227 | /* Set the IRQ chaining logic */ |
Doug Berger | 49aa6ef | 2017-09-18 17:59:58 -0700 | [diff] [blame] | 228 | irq_set_chained_handler_and_data(parent_irq, |
Thomas Gleixner | f286c17 | 2015-06-21 21:10:52 +0200 | [diff] [blame] | 229 | brcmstb_l2_intc_irq_handle, data); |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 230 | |
Doug Berger | 49aa6ef | 2017-09-18 17:59:58 -0700 | [diff] [blame] | 231 | data->gc = irq_get_domain_generic_chip(data->domain, 0); |
| 232 | data->gc->reg_base = base; |
| 233 | data->gc->private = data; |
Doug Berger | c0ca726 | 2017-09-18 18:00:00 -0700 | [diff] [blame] | 234 | data->status_offset = init_params->cpu_status; |
| 235 | data->mask_offset = init_params->cpu_mask_status; |
Doug Berger | 8480ca4 | 2017-09-18 17:59:59 -0700 | [diff] [blame] | 236 | |
Doug Berger | 49aa6ef | 2017-09-18 17:59:58 -0700 | [diff] [blame] | 237 | ct = data->gc->chip_types; |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 238 | |
Doug Berger | c0ca726 | 2017-09-18 18:00:00 -0700 | [diff] [blame] | 239 | if (init_params->cpu_clear >= 0) { |
| 240 | ct->regs.ack = init_params->cpu_clear; |
| 241 | ct->chip.irq_ack = irq_gc_ack_set_bit; |
| 242 | ct->chip.irq_mask_ack = brcmstb_l2_mask_and_ack; |
| 243 | } else { |
| 244 | /* No Ack - but still slightly more efficient to define this */ |
| 245 | ct->chip.irq_mask_ack = irq_gc_mask_disable_reg; |
| 246 | } |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 247 | |
| 248 | ct->chip.irq_mask = irq_gc_mask_disable_reg; |
Doug Berger | c0ca726 | 2017-09-18 18:00:00 -0700 | [diff] [blame] | 249 | ct->regs.disable = init_params->cpu_mask_set; |
| 250 | ct->regs.mask = init_params->cpu_mask_status; |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 251 | |
| 252 | ct->chip.irq_unmask = irq_gc_unmask_enable_reg; |
Doug Berger | c0ca726 | 2017-09-18 18:00:00 -0700 | [diff] [blame] | 253 | ct->regs.enable = init_params->cpu_mask_clear; |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 254 | |
| 255 | ct->chip.irq_suspend = brcmstb_l2_intc_suspend; |
| 256 | ct->chip.irq_resume = brcmstb_l2_intc_resume; |
Florian Fainelli | c017d21 | 2017-07-27 15:38:17 -0700 | [diff] [blame] | 257 | ct->chip.irq_pm_shutdown = brcmstb_l2_intc_suspend; |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 258 | |
Brian Norris | c9ae71e | 2014-12-25 09:49:02 -0800 | [diff] [blame] | 259 | if (data->can_wake) { |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 260 | /* This IRQ chip can wake the system, set all child interrupts |
| 261 | * in wake_enabled mask |
| 262 | */ |
Doug Berger | 49aa6ef | 2017-09-18 17:59:58 -0700 | [diff] [blame] | 263 | data->gc->wake_enabled = 0xffffffff; |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 264 | ct->chip.irq_set_wake = irq_gc_set_wake; |
| 265 | } |
| 266 | |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 267 | return 0; |
| 268 | |
| 269 | out_free_domain: |
| 270 | irq_domain_remove(data->domain); |
| 271 | out_unmap: |
Doug Berger | 49aa6ef | 2017-09-18 17:59:58 -0700 | [diff] [blame] | 272 | iounmap(base); |
Florian Fainelli | 7f646e9 | 2014-05-23 17:40:53 -0700 | [diff] [blame] | 273 | out_free: |
| 274 | kfree(data); |
| 275 | return ret; |
| 276 | } |
Doug Berger | c0ca726 | 2017-09-18 18:00:00 -0700 | [diff] [blame] | 277 | |
| 278 | int __init brcmstb_l2_edge_intc_of_init(struct device_node *np, |
| 279 | struct device_node *parent) |
| 280 | { |
| 281 | return brcmstb_l2_intc_of_init(np, parent, &l2_edge_intc_init); |
| 282 | } |
| 283 | IRQCHIP_DECLARE(brcmstb_l2_intc, "brcm,l2-intc", brcmstb_l2_edge_intc_of_init); |
| 284 | |
| 285 | int __init brcmstb_l2_lvl_intc_of_init(struct device_node *np, |
| 286 | struct device_node *parent) |
| 287 | { |
| 288 | return brcmstb_l2_intc_of_init(np, parent, &l2_lvl_intc_init); |
| 289 | } |
| 290 | IRQCHIP_DECLARE(bcm7271_l2_intc, "brcm,bcm7271-l2-intc", |
| 291 | brcmstb_l2_lvl_intc_of_init); |