blob: 7de378e98cf209bc61decefc5fcf762824a31d35 [file] [log] [blame]
Florian Fainellia5042de22014-09-09 17:44:21 -07001/*
2 * Broadcom BCM7120 style Level 2 interrupt controller driver
3 *
4 * Copyright (C) 2014 Broadcom Corporation
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
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/init.h>
14#include <linux/slab.h>
15#include <linux/module.h>
Kevin Cernekeec17261f2014-11-06 22:44:28 -080016#include <linux/kconfig.h>
Kevin Cernekee7b7230e2014-12-25 09:49:05 -080017#include <linux/kernel.h>
Florian Fainellia5042de22014-09-09 17:44:21 -070018#include <linux/platform_device.h>
19#include <linux/of.h>
20#include <linux/of_irq.h>
21#include <linux/of_address.h>
22#include <linux/of_platform.h>
23#include <linux/interrupt.h>
24#include <linux/irq.h>
25#include <linux/io.h>
26#include <linux/irqdomain.h>
27#include <linux/reboot.h>
Kevin Cernekeec76acf42014-11-06 22:44:26 -080028#include <linux/bitops.h>
Joel Porquet41a83e062015-07-07 17:11:46 -040029#include <linux/irqchip.h>
Florian Fainellia5042de22014-09-09 17:44:21 -070030#include <linux/irqchip/chained_irq.h>
31
Florian Fainellia5042de22014-09-09 17:44:21 -070032/* Register offset in the L2 interrupt controller */
33#define IRQEN 0x00
34#define IRQSTAT 0x04
35
Kevin Cernekeec76acf42014-11-06 22:44:26 -080036#define MAX_WORDS 4
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -080037#define MAX_MAPPINGS (MAX_WORDS * 2)
Kevin Cernekeec76acf42014-11-06 22:44:26 -080038#define IRQS_PER_WORD 32
39
Florian Fainellia5042de22014-09-09 17:44:21 -070040struct bcm7120_l2_intc_data {
Kevin Cernekeec76acf42014-11-06 22:44:26 -080041 unsigned int n_words;
Kevin Cernekee5b5468c2014-12-25 09:49:03 -080042 void __iomem *map_base[MAX_MAPPINGS];
43 void __iomem *pair_base[MAX_WORDS];
44 int en_offset[MAX_WORDS];
45 int stat_offset[MAX_WORDS];
Florian Fainellia5042de22014-09-09 17:44:21 -070046 struct irq_domain *domain;
47 bool can_wake;
Kevin Cernekeec76acf42014-11-06 22:44:26 -080048 u32 irq_fwd_mask[MAX_WORDS];
49 u32 irq_map_mask[MAX_WORDS];
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -080050 int num_parent_irqs;
51 const __be32 *map_mask_prop;
Florian Fainellia5042de22014-09-09 17:44:21 -070052};
53
54static void bcm7120_l2_intc_irq_handle(unsigned int irq, struct irq_desc *desc)
55{
56 struct bcm7120_l2_intc_data *b = irq_desc_get_handler_data(desc);
57 struct irq_chip *chip = irq_desc_get_chip(desc);
Kevin Cernekeec76acf42014-11-06 22:44:26 -080058 unsigned int idx;
Florian Fainellia5042de22014-09-09 17:44:21 -070059
60 chained_irq_enter(chip, desc);
61
Kevin Cernekeec76acf42014-11-06 22:44:26 -080062 for (idx = 0; idx < b->n_words; idx++) {
63 int base = idx * IRQS_PER_WORD;
64 struct irq_chip_generic *gc =
65 irq_get_domain_generic_chip(b->domain, base);
66 unsigned long pending;
67 int hwirq;
Florian Fainellia5042de22014-09-09 17:44:21 -070068
Kevin Cernekeec76acf42014-11-06 22:44:26 -080069 irq_gc_lock(gc);
Kevin Cernekee5b5468c2014-12-25 09:49:03 -080070 pending = irq_reg_readl(gc, b->stat_offset[idx]) &
71 gc->mask_cache;
Kevin Cernekeec76acf42014-11-06 22:44:26 -080072 irq_gc_unlock(gc);
73
74 for_each_set_bit(hwirq, &pending, IRQS_PER_WORD) {
75 generic_handle_irq(irq_find_mapping(b->domain,
76 base + hwirq));
77 }
Florian Fainellia5042de22014-09-09 17:44:21 -070078 }
79
Florian Fainellia5042de22014-09-09 17:44:21 -070080 chained_irq_exit(chip, desc);
81}
82
83static void bcm7120_l2_intc_suspend(struct irq_data *d)
84{
85 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Kevin Cernekee5b5468c2014-12-25 09:49:03 -080086 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Florian Fainellia5042de22014-09-09 17:44:21 -070087 struct bcm7120_l2_intc_data *b = gc->private;
Florian Fainellia5042de22014-09-09 17:44:21 -070088
89 irq_gc_lock(gc);
Kevin Cernekeec17261f2014-11-06 22:44:28 -080090 if (b->can_wake)
Kevin Cernekee5b5468c2014-12-25 09:49:03 -080091 irq_reg_writel(gc, gc->mask_cache | gc->wake_active,
92 ct->regs.mask);
Florian Fainellia5042de22014-09-09 17:44:21 -070093 irq_gc_unlock(gc);
94}
95
96static void bcm7120_l2_intc_resume(struct irq_data *d)
97{
98 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Kevin Cernekee5b5468c2014-12-25 09:49:03 -080099 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Florian Fainellia5042de22014-09-09 17:44:21 -0700100
101 /* Restore the saved mask */
102 irq_gc_lock(gc);
Kevin Cernekee5b5468c2014-12-25 09:49:03 -0800103 irq_reg_writel(gc, gc->mask_cache, ct->regs.mask);
Florian Fainellia5042de22014-09-09 17:44:21 -0700104 irq_gc_unlock(gc);
105}
106
107static int bcm7120_l2_intc_init_one(struct device_node *dn,
108 struct bcm7120_l2_intc_data *data,
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800109 int irq)
Florian Fainellia5042de22014-09-09 17:44:21 -0700110{
111 int parent_irq;
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800112 unsigned int idx;
Florian Fainellia5042de22014-09-09 17:44:21 -0700113
114 parent_irq = irq_of_parse_and_map(dn, irq);
Dmitry Torokhov714710e2014-11-14 14:16:14 -0800115 if (!parent_irq) {
Florian Fainellia5042de22014-09-09 17:44:21 -0700116 pr_err("failed to map interrupt %d\n", irq);
Dmitry Torokhov714710e2014-11-14 14:16:14 -0800117 return -EINVAL;
Florian Fainellia5042de22014-09-09 17:44:21 -0700118 }
119
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800120 /* For multiple parent IRQs with multiple words, this looks like:
121 * <irq0_w0 irq0_w1 irq1_w0 irq1_w1 ...>
122 */
Kevin Cernekee7b7230e2014-12-25 09:49:05 -0800123 for (idx = 0; idx < data->n_words; idx++) {
124 if (data->map_mask_prop) {
125 data->irq_map_mask[idx] |=
126 be32_to_cpup(data->map_mask_prop +
127 irq * data->n_words + idx);
128 } else {
129 data->irq_map_mask[idx] = 0xffffffff;
130 }
131 }
Florian Fainellia5042de22014-09-09 17:44:21 -0700132
133 irq_set_handler_data(parent_irq, data);
134 irq_set_chained_handler(parent_irq, bcm7120_l2_intc_irq_handle);
135
136 return 0;
137}
138
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800139static int __init bcm7120_l2_intc_iomap_7120(struct device_node *dn,
140 struct bcm7120_l2_intc_data *data)
141{
142 int ret;
143
144 data->map_base[0] = of_iomap(dn, 0);
145 if (!data->map_base[0]) {
146 pr_err("unable to map registers\n");
147 return -ENOMEM;
148 }
149
150 data->pair_base[0] = data->map_base[0];
151 data->en_offset[0] = IRQEN;
152 data->stat_offset[0] = IRQSTAT;
153 data->n_words = 1;
154
155 ret = of_property_read_u32_array(dn, "brcm,int-fwd-mask",
156 data->irq_fwd_mask, data->n_words);
157 if (ret != 0 && ret != -EINVAL) {
158 /* property exists but has the wrong number of words */
159 pr_err("invalid brcm,int-fwd-mask property\n");
160 return -EINVAL;
161 }
162
163 data->map_mask_prop = of_get_property(dn, "brcm,int-map-mask", &ret);
164 if (!data->map_mask_prop ||
165 (ret != (sizeof(__be32) * data->num_parent_irqs * data->n_words))) {
166 pr_err("invalid brcm,int-map-mask property\n");
167 return -EINVAL;
168 }
169
170 return 0;
171}
172
Kevin Cernekee7b7230e2014-12-25 09:49:05 -0800173static int __init bcm7120_l2_intc_iomap_3380(struct device_node *dn,
174 struct bcm7120_l2_intc_data *data)
175{
176 unsigned int gc_idx;
177
178 for (gc_idx = 0; gc_idx < MAX_WORDS; gc_idx++) {
179 unsigned int map_idx = gc_idx * 2;
180 void __iomem *en = of_iomap(dn, map_idx + 0);
181 void __iomem *stat = of_iomap(dn, map_idx + 1);
182 void __iomem *base = min(en, stat);
183
184 data->map_base[map_idx + 0] = en;
185 data->map_base[map_idx + 1] = stat;
186
187 if (!base)
188 break;
189
190 data->pair_base[gc_idx] = base;
191 data->en_offset[gc_idx] = en - base;
192 data->stat_offset[gc_idx] = stat - base;
193 }
194
195 if (!gc_idx) {
196 pr_err("unable to map registers\n");
197 return -EINVAL;
198 }
199
200 data->n_words = gc_idx;
201 return 0;
202}
203
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800204int __init bcm7120_l2_intc_probe(struct device_node *dn,
205 struct device_node *parent,
206 int (*iomap_regs_fn)(struct device_node *,
207 struct bcm7120_l2_intc_data *),
208 const char *intc_name)
Florian Fainellia5042de22014-09-09 17:44:21 -0700209{
210 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
211 struct bcm7120_l2_intc_data *data;
212 struct irq_chip_generic *gc;
213 struct irq_chip_type *ct;
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800214 int ret = 0;
Kevin Cernekeec17261f2014-11-06 22:44:28 -0800215 unsigned int idx, irq, flags;
Florian Fainellia5042de22014-09-09 17:44:21 -0700216
217 data = kzalloc(sizeof(*data), GFP_KERNEL);
218 if (!data)
219 return -ENOMEM;
220
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800221 data->num_parent_irqs = of_irq_count(dn);
222 if (data->num_parent_irqs <= 0) {
Florian Fainellia5042de22014-09-09 17:44:21 -0700223 pr_err("invalid number of parent interrupts\n");
224 ret = -ENOMEM;
225 goto out_unmap;
226 }
227
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800228 ret = iomap_regs_fn(dn, data);
229 if (ret < 0)
Florian Fainellia5042de22014-09-09 17:44:21 -0700230 goto out_unmap;
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800231
232 for (idx = 0; idx < data->n_words; idx++) {
233 __raw_writel(data->irq_fwd_mask[idx],
234 data->pair_base[idx] +
235 data->en_offset[idx]);
Florian Fainellia5042de22014-09-09 17:44:21 -0700236 }
237
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800238 for (irq = 0; irq < data->num_parent_irqs; irq++) {
239 ret = bcm7120_l2_intc_init_one(dn, data, irq);
Florian Fainellia5042de22014-09-09 17:44:21 -0700240 if (ret)
241 goto out_unmap;
242 }
243
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800244 data->domain = irq_domain_add_linear(dn, IRQS_PER_WORD * data->n_words,
245 &irq_generic_chip_ops, NULL);
Florian Fainellia5042de22014-09-09 17:44:21 -0700246 if (!data->domain) {
247 ret = -ENOMEM;
248 goto out_unmap;
249 }
250
Kevin Cernekeec17261f2014-11-06 22:44:28 -0800251 /* MIPS chips strapped for BE will automagically configure the
252 * peripheral registers for CPU-native byte order.
253 */
254 flags = IRQ_GC_INIT_MASK_CACHE;
255 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
256 flags |= IRQ_GC_BE_IO;
257
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800258 ret = irq_alloc_domain_generic_chips(data->domain, IRQS_PER_WORD, 1,
Kevin Cernekeec17261f2014-11-06 22:44:28 -0800259 dn->full_name, handle_level_irq, clr, 0, flags);
Florian Fainellia5042de22014-09-09 17:44:21 -0700260 if (ret) {
261 pr_err("failed to allocate generic irq chip\n");
262 goto out_free_domain;
263 }
264
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800265 if (of_property_read_bool(dn, "brcm,irq-can-wake"))
Florian Fainellia5042de22014-09-09 17:44:21 -0700266 data->can_wake = true;
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800267
268 for (idx = 0; idx < data->n_words; idx++) {
269 irq = idx * IRQS_PER_WORD;
270 gc = irq_get_domain_generic_chip(data->domain, irq);
271
272 gc->unused = 0xffffffff & ~data->irq_map_mask[idx];
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800273 gc->private = data;
274 ct = gc->chip_types;
275
Kevin Cernekee5b5468c2014-12-25 09:49:03 -0800276 gc->reg_base = data->pair_base[idx];
277 ct->regs.mask = data->en_offset[idx];
278
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800279 ct->chip.irq_mask = irq_gc_mask_clr_bit;
280 ct->chip.irq_unmask = irq_gc_mask_set_bit;
281 ct->chip.irq_ack = irq_gc_noop;
282 ct->chip.irq_suspend = bcm7120_l2_intc_suspend;
283 ct->chip.irq_resume = bcm7120_l2_intc_resume;
284
285 if (data->can_wake) {
286 /* This IRQ chip can wake the system, set all
287 * relevant child interupts in wake_enabled mask
288 */
289 gc->wake_enabled = 0xffffffff;
290 gc->wake_enabled &= ~gc->unused;
291 ct->chip.irq_set_wake = irq_gc_set_wake;
292 }
Florian Fainellia5042de22014-09-09 17:44:21 -0700293 }
294
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800295 pr_info("registered %s intc (mem: 0x%p, parent IRQ(s): %d)\n",
296 intc_name, data->map_base[0], data->num_parent_irqs);
Florian Fainellia5042de22014-09-09 17:44:21 -0700297
298 return 0;
299
300out_free_domain:
301 irq_domain_remove(data->domain);
302out_unmap:
Kevin Cernekee5b5468c2014-12-25 09:49:03 -0800303 for (idx = 0; idx < MAX_MAPPINGS; idx++) {
304 if (data->map_base[idx])
305 iounmap(data->map_base[idx]);
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800306 }
Florian Fainellia5042de22014-09-09 17:44:21 -0700307 kfree(data);
308 return ret;
309}
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800310
311int __init bcm7120_l2_intc_probe_7120(struct device_node *dn,
312 struct device_node *parent)
313{
314 return bcm7120_l2_intc_probe(dn, parent, bcm7120_l2_intc_iomap_7120,
315 "BCM7120 L2");
316}
317
Kevin Cernekee7b7230e2014-12-25 09:49:05 -0800318int __init bcm7120_l2_intc_probe_3380(struct device_node *dn,
319 struct device_node *parent)
320{
321 return bcm7120_l2_intc_probe(dn, parent, bcm7120_l2_intc_iomap_3380,
322 "BCM3380 L2");
323}
324
Kevin Cernekeea4fcbb82014-11-06 22:44:27 -0800325IRQCHIP_DECLARE(bcm7120_l2_intc, "brcm,bcm7120-l2-intc",
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800326 bcm7120_l2_intc_probe_7120);
Kevin Cernekee7b7230e2014-12-25 09:49:05 -0800327
328IRQCHIP_DECLARE(bcm3380_l2_intc, "brcm,bcm3380-l2-intc",
329 bcm7120_l2_intc_probe_3380);