blob: 691d20eb0bec1137c403c6d91097c67a1d906fac [file] [log] [blame]
Florian Fainelli7f646e92014-05-23 17:40:53 -07001/*
2 * Generic Broadcom Set Top Box Level 2 Interrupt controller driver
3 *
Doug Berger49aa6ef2017-09-18 17:59:58 -07004 * Copyright (C) 2014-2017 Broadcom
Florian Fainelli7f646e92014-05-23 17:40:53 -07005 *
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 Cernekee05f12752014-11-06 22:44:20 -080022#include <linux/spinlock.h>
Florian Fainelli7f646e92014-05-23 17:40:53 -070023#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 Bergerc0ca7262017-09-18 18:00:00 -070034struct 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 */
44static 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 */
54static 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 Fainelli7f646e92014-05-23 17:40:53 -070062
63/* L2 intc private data structure */
64struct brcmstb_l2_intc_data {
Florian Fainelli7f646e92014-05-23 17:40:53 -070065 struct irq_domain *domain;
Doug Berger49aa6ef2017-09-18 17:59:58 -070066 struct irq_chip_generic *gc;
Doug Berger8480ca42017-09-18 17:59:59 -070067 int status_offset;
68 int mask_offset;
Florian Fainelli7f646e92014-05-23 17:40:53 -070069 bool can_wake;
70 u32 saved_mask; /* for suspend/resume */
71};
72
Doug Berger49aa6ef2017-09-18 17:59:58 -070073/**
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 */
86static 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 Gleixnerbd0b9ac2015-09-14 10:42:37 +020099static void brcmstb_l2_intc_irq_handle(struct irq_desc *desc)
Florian Fainelli7f646e92014-05-23 17:40:53 -0700100{
101 struct brcmstb_l2_intc_data *b = irq_desc_get_handler_data(desc);
102 struct irq_chip *chip = irq_desc_get_chip(desc);
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200103 unsigned int irq;
Florian Fainelli7f646e92014-05-23 17:40:53 -0700104 u32 status;
105
106 chained_irq_enter(chip, desc);
107
Doug Berger8480ca42017-09-18 17:59:59 -0700108 status = irq_reg_readl(b->gc, b->status_offset) &
109 ~(irq_reg_readl(b->gc, b->mask_offset));
Florian Fainelli7f646e92014-05-23 17:40:53 -0700110
111 if (status == 0) {
Kevin Cernekee05f12752014-11-06 22:44:20 -0800112 raw_spin_lock(&desc->lock);
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200113 handle_bad_irq(desc);
Kevin Cernekee05f12752014-11-06 22:44:20 -0800114 raw_spin_unlock(&desc->lock);
Florian Fainelli7f646e92014-05-23 17:40:53 -0700115 goto out;
116 }
117
118 do {
119 irq = ffs(status) - 1;
Florian Fainelli7f646e92014-05-23 17:40:53 -0700120 status &= ~(1 << irq);
Doug Berger49aa6ef2017-09-18 17:59:58 -0700121 generic_handle_irq(irq_linear_revmap(b->domain, irq));
Florian Fainelli7f646e92014-05-23 17:40:53 -0700122 } while (status);
123out:
124 chained_irq_exit(chip, desc);
125}
126
127static void brcmstb_l2_intc_suspend(struct irq_data *d)
128{
129 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Doug Berger8480ca42017-09-18 17:59:59 -0700130 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Florian Fainelli7f646e92014-05-23 17:40:53 -0700131 struct brcmstb_l2_intc_data *b = gc->private;
132
133 irq_gc_lock(gc);
134 /* Save the current mask */
Doug Berger8480ca42017-09-18 17:59:59 -0700135 b->saved_mask = irq_reg_readl(gc, ct->regs.mask);
Florian Fainelli7f646e92014-05-23 17:40:53 -0700136
137 if (b->can_wake) {
138 /* Program the wakeup mask */
Doug Berger8480ca42017-09-18 17:59:59 -0700139 irq_reg_writel(gc, ~gc->wake_active, ct->regs.disable);
140 irq_reg_writel(gc, gc->wake_active, ct->regs.enable);
Florian Fainelli7f646e92014-05-23 17:40:53 -0700141 }
142 irq_gc_unlock(gc);
143}
144
145static void brcmstb_l2_intc_resume(struct irq_data *d)
146{
147 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Doug Berger8480ca42017-09-18 17:59:59 -0700148 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Florian Fainelli7f646e92014-05-23 17:40:53 -0700149 struct brcmstb_l2_intc_data *b = gc->private;
150
151 irq_gc_lock(gc);
Doug Bergerc0ca7262017-09-18 18:00:00 -0700152 if (ct->chip.irq_ack) {
Doug Berger8480ca42017-09-18 17:59:59 -0700153 /* Clear unmasked non-wakeup interrupts */
154 irq_reg_writel(gc, ~b->saved_mask & ~gc->wake_active,
155 ct->regs.ack);
156 }
Florian Fainelli7f646e92014-05-23 17:40:53 -0700157
158 /* Restore the saved mask */
Doug Berger8480ca42017-09-18 17:59:59 -0700159 irq_reg_writel(gc, b->saved_mask, ct->regs.disable);
160 irq_reg_writel(gc, ~b->saved_mask, ct->regs.enable);
Florian Fainelli7f646e92014-05-23 17:40:53 -0700161 irq_gc_unlock(gc);
162}
163
Ben Dooks2ae9add2016-06-08 19:02:20 +0100164static int __init brcmstb_l2_intc_of_init(struct device_node *np,
Doug Bergerc0ca7262017-09-18 18:00:00 -0700165 struct device_node *parent,
166 const struct brcmstb_intc_init_params
167 *init_params)
Florian Fainelli7f646e92014-05-23 17:40:53 -0700168{
169 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
170 struct brcmstb_l2_intc_data *data;
Florian Fainelli7f646e92014-05-23 17:40:53 -0700171 struct irq_chip_type *ct;
172 int ret;
Kevin Cernekee1abbdba2014-11-06 22:44:29 -0800173 unsigned int flags;
Doug Berger49aa6ef2017-09-18 17:59:58 -0700174 int parent_irq;
175 void __iomem *base;
Florian Fainelli7f646e92014-05-23 17:40:53 -0700176
177 data = kzalloc(sizeof(*data), GFP_KERNEL);
178 if (!data)
179 return -ENOMEM;
180
Doug Berger49aa6ef2017-09-18 17:59:58 -0700181 base = of_iomap(np, 0);
182 if (!base) {
Florian Fainelli7f646e92014-05-23 17:40:53 -0700183 pr_err("failed to remap intc L2 registers\n");
184 ret = -ENOMEM;
185 goto out_free;
186 }
187
188 /* Disable all interrupts by default */
Doug Bergerc0ca7262017-09-18 18:00:00 -0700189 writel(0xffffffff, base + init_params->cpu_mask_set);
Brian Norrisc9ae71e2014-12-25 09:49:02 -0800190
191 /* Wakeup interrupts may be retained from S5 (cold boot) */
192 data->can_wake = of_property_read_bool(np, "brcm,irq-can-wake");
Doug Bergerc0ca7262017-09-18 18:00:00 -0700193 if (!data->can_wake && (init_params->cpu_clear >= 0))
194 writel(0xffffffff, base + init_params->cpu_clear);
Florian Fainelli7f646e92014-05-23 17:40:53 -0700195
Doug Berger49aa6ef2017-09-18 17:59:58 -0700196 parent_irq = irq_of_parse_and_map(np, 0);
197 if (!parent_irq) {
Florian Fainelli7f646e92014-05-23 17:40:53 -0700198 pr_err("failed to find parent interrupt\n");
Dmitry Torokhovd99ba442014-11-14 14:16:42 -0800199 ret = -EINVAL;
Florian Fainelli7f646e92014-05-23 17:40:53 -0700200 goto out_unmap;
201 }
202
203 data->domain = irq_domain_add_linear(np, 32,
204 &irq_generic_chip_ops, NULL);
205 if (!data->domain) {
206 ret = -ENOMEM;
207 goto out_unmap;
208 }
209
Kevin Cernekee1abbdba2014-11-06 22:44:29 -0800210 /* MIPS chips strapped for BE will automagically configure the
211 * peripheral registers for CPU-native byte order.
212 */
213 flags = 0;
214 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
215 flags |= IRQ_GC_BE_IO;
216
Florian Fainelli7f646e92014-05-23 17:40:53 -0700217 /* Allocate a single Generic IRQ chip for this node */
218 ret = irq_alloc_domain_generic_chips(data->domain, 32, 1,
Doug Bergerc0ca7262017-09-18 18:00:00 -0700219 np->full_name, init_params->handler, clr, 0, flags);
Florian Fainelli7f646e92014-05-23 17:40:53 -0700220 if (ret) {
221 pr_err("failed to allocate generic irq chip\n");
222 goto out_free_domain;
223 }
224
225 /* Set the IRQ chaining logic */
Doug Berger49aa6ef2017-09-18 17:59:58 -0700226 irq_set_chained_handler_and_data(parent_irq,
Thomas Gleixnerf286c172015-06-21 21:10:52 +0200227 brcmstb_l2_intc_irq_handle, data);
Florian Fainelli7f646e92014-05-23 17:40:53 -0700228
Doug Berger49aa6ef2017-09-18 17:59:58 -0700229 data->gc = irq_get_domain_generic_chip(data->domain, 0);
230 data->gc->reg_base = base;
231 data->gc->private = data;
Doug Bergerc0ca7262017-09-18 18:00:00 -0700232 data->status_offset = init_params->cpu_status;
233 data->mask_offset = init_params->cpu_mask_status;
Doug Berger8480ca42017-09-18 17:59:59 -0700234
Doug Berger49aa6ef2017-09-18 17:59:58 -0700235 ct = data->gc->chip_types;
Florian Fainelli7f646e92014-05-23 17:40:53 -0700236
Doug Bergerc0ca7262017-09-18 18:00:00 -0700237 if (init_params->cpu_clear >= 0) {
238 ct->regs.ack = init_params->cpu_clear;
239 ct->chip.irq_ack = irq_gc_ack_set_bit;
240 ct->chip.irq_mask_ack = brcmstb_l2_mask_and_ack;
241 } else {
242 /* No Ack - but still slightly more efficient to define this */
243 ct->chip.irq_mask_ack = irq_gc_mask_disable_reg;
244 }
Florian Fainelli7f646e92014-05-23 17:40:53 -0700245
246 ct->chip.irq_mask = irq_gc_mask_disable_reg;
Doug Bergerc0ca7262017-09-18 18:00:00 -0700247 ct->regs.disable = init_params->cpu_mask_set;
248 ct->regs.mask = init_params->cpu_mask_status;
Florian Fainelli7f646e92014-05-23 17:40:53 -0700249
250 ct->chip.irq_unmask = irq_gc_unmask_enable_reg;
Doug Bergerc0ca7262017-09-18 18:00:00 -0700251 ct->regs.enable = init_params->cpu_mask_clear;
Florian Fainelli7f646e92014-05-23 17:40:53 -0700252
253 ct->chip.irq_suspend = brcmstb_l2_intc_suspend;
254 ct->chip.irq_resume = brcmstb_l2_intc_resume;
Florian Fainellic017d212017-07-27 15:38:17 -0700255 ct->chip.irq_pm_shutdown = brcmstb_l2_intc_suspend;
Florian Fainelli7f646e92014-05-23 17:40:53 -0700256
Brian Norrisc9ae71e2014-12-25 09:49:02 -0800257 if (data->can_wake) {
Florian Fainelli7f646e92014-05-23 17:40:53 -0700258 /* This IRQ chip can wake the system, set all child interrupts
259 * in wake_enabled mask
260 */
Doug Berger49aa6ef2017-09-18 17:59:58 -0700261 data->gc->wake_enabled = 0xffffffff;
Florian Fainelli7f646e92014-05-23 17:40:53 -0700262 ct->chip.irq_set_wake = irq_gc_set_wake;
263 }
264
265 pr_info("registered L2 intc (mem: 0x%p, parent irq: %d)\n",
Doug Berger49aa6ef2017-09-18 17:59:58 -0700266 base, parent_irq);
Florian Fainelli7f646e92014-05-23 17:40:53 -0700267
268 return 0;
269
270out_free_domain:
271 irq_domain_remove(data->domain);
272out_unmap:
Doug Berger49aa6ef2017-09-18 17:59:58 -0700273 iounmap(base);
Florian Fainelli7f646e92014-05-23 17:40:53 -0700274out_free:
275 kfree(data);
276 return ret;
277}
Doug Bergerc0ca7262017-09-18 18:00:00 -0700278
279int __init brcmstb_l2_edge_intc_of_init(struct device_node *np,
280 struct device_node *parent)
281{
282 return brcmstb_l2_intc_of_init(np, parent, &l2_edge_intc_init);
283}
284IRQCHIP_DECLARE(brcmstb_l2_intc, "brcm,l2-intc", brcmstb_l2_edge_intc_of_init);
285
286int __init brcmstb_l2_lvl_intc_of_init(struct device_node *np,
287 struct device_node *parent)
288{
289 return brcmstb_l2_intc_of_init(np, parent, &l2_lvl_intc_init);
290}
291IRQCHIP_DECLARE(bcm7271_l2_intc, "brcm,bcm7271-l2-intc",
292 brcmstb_l2_lvl_intc_of_init);