blob: c885a5c4632a3c820c4f66cb95fae406afe86596 [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
Brian Norrisfd537762015-07-22 16:21:40 -070083static void bcm7120_l2_intc_suspend(struct irq_chip_generic *gc)
Florian Fainellia5042de22014-09-09 17:44:21 -070084{
Florian Fainellia5042de22014-09-09 17:44:21 -070085 struct bcm7120_l2_intc_data *b = gc->private;
Brian Norrisfd537762015-07-22 16:21:40 -070086 struct irq_chip_type *ct = gc->chip_types;
Florian Fainellia5042de22014-09-09 17:44:21 -070087
88 irq_gc_lock(gc);
Kevin Cernekeec17261f2014-11-06 22:44:28 -080089 if (b->can_wake)
Kevin Cernekee5b5468c2014-12-25 09:49:03 -080090 irq_reg_writel(gc, gc->mask_cache | gc->wake_active,
91 ct->regs.mask);
Florian Fainellia5042de22014-09-09 17:44:21 -070092 irq_gc_unlock(gc);
93}
94
Brian Norrisfd537762015-07-22 16:21:40 -070095static void bcm7120_l2_intc_resume(struct irq_chip_generic *gc)
Florian Fainellia5042de22014-09-09 17:44:21 -070096{
Brian Norrisfd537762015-07-22 16:21:40 -070097 struct irq_chip_type *ct = gc->chip_types;
Florian Fainellia5042de22014-09-09 17:44:21 -070098
99 /* Restore the saved mask */
100 irq_gc_lock(gc);
Kevin Cernekee5b5468c2014-12-25 09:49:03 -0800101 irq_reg_writel(gc, gc->mask_cache, ct->regs.mask);
Florian Fainellia5042de22014-09-09 17:44:21 -0700102 irq_gc_unlock(gc);
103}
104
105static int bcm7120_l2_intc_init_one(struct device_node *dn,
106 struct bcm7120_l2_intc_data *data,
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800107 int irq)
Florian Fainellia5042de22014-09-09 17:44:21 -0700108{
109 int parent_irq;
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800110 unsigned int idx;
Florian Fainellia5042de22014-09-09 17:44:21 -0700111
112 parent_irq = irq_of_parse_and_map(dn, irq);
Dmitry Torokhov714710e2014-11-14 14:16:14 -0800113 if (!parent_irq) {
Florian Fainellia5042de22014-09-09 17:44:21 -0700114 pr_err("failed to map interrupt %d\n", irq);
Dmitry Torokhov714710e2014-11-14 14:16:14 -0800115 return -EINVAL;
Florian Fainellia5042de22014-09-09 17:44:21 -0700116 }
117
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800118 /* For multiple parent IRQs with multiple words, this looks like:
119 * <irq0_w0 irq0_w1 irq1_w0 irq1_w1 ...>
120 */
Kevin Cernekee7b7230e2014-12-25 09:49:05 -0800121 for (idx = 0; idx < data->n_words; idx++) {
122 if (data->map_mask_prop) {
123 data->irq_map_mask[idx] |=
124 be32_to_cpup(data->map_mask_prop +
125 irq * data->n_words + idx);
126 } else {
127 data->irq_map_mask[idx] = 0xffffffff;
128 }
129 }
Florian Fainellia5042de22014-09-09 17:44:21 -0700130
Thomas Gleixner99e32ab2015-06-21 21:10:51 +0200131 irq_set_chained_handler_and_data(parent_irq,
132 bcm7120_l2_intc_irq_handle, data);
Florian Fainellia5042de22014-09-09 17:44:21 -0700133
134 return 0;
135}
136
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800137static int __init bcm7120_l2_intc_iomap_7120(struct device_node *dn,
138 struct bcm7120_l2_intc_data *data)
139{
140 int ret;
141
142 data->map_base[0] = of_iomap(dn, 0);
143 if (!data->map_base[0]) {
144 pr_err("unable to map registers\n");
145 return -ENOMEM;
146 }
147
148 data->pair_base[0] = data->map_base[0];
149 data->en_offset[0] = IRQEN;
150 data->stat_offset[0] = IRQSTAT;
151 data->n_words = 1;
152
153 ret = of_property_read_u32_array(dn, "brcm,int-fwd-mask",
154 data->irq_fwd_mask, data->n_words);
155 if (ret != 0 && ret != -EINVAL) {
156 /* property exists but has the wrong number of words */
157 pr_err("invalid brcm,int-fwd-mask property\n");
158 return -EINVAL;
159 }
160
161 data->map_mask_prop = of_get_property(dn, "brcm,int-map-mask", &ret);
162 if (!data->map_mask_prop ||
163 (ret != (sizeof(__be32) * data->num_parent_irqs * data->n_words))) {
164 pr_err("invalid brcm,int-map-mask property\n");
165 return -EINVAL;
166 }
167
168 return 0;
169}
170
Kevin Cernekee7b7230e2014-12-25 09:49:05 -0800171static int __init bcm7120_l2_intc_iomap_3380(struct device_node *dn,
172 struct bcm7120_l2_intc_data *data)
173{
174 unsigned int gc_idx;
175
176 for (gc_idx = 0; gc_idx < MAX_WORDS; gc_idx++) {
177 unsigned int map_idx = gc_idx * 2;
178 void __iomem *en = of_iomap(dn, map_idx + 0);
179 void __iomem *stat = of_iomap(dn, map_idx + 1);
180 void __iomem *base = min(en, stat);
181
182 data->map_base[map_idx + 0] = en;
183 data->map_base[map_idx + 1] = stat;
184
185 if (!base)
186 break;
187
188 data->pair_base[gc_idx] = base;
189 data->en_offset[gc_idx] = en - base;
190 data->stat_offset[gc_idx] = stat - base;
191 }
192
193 if (!gc_idx) {
194 pr_err("unable to map registers\n");
195 return -EINVAL;
196 }
197
198 data->n_words = gc_idx;
199 return 0;
200}
201
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800202int __init bcm7120_l2_intc_probe(struct device_node *dn,
203 struct device_node *parent,
204 int (*iomap_regs_fn)(struct device_node *,
205 struct bcm7120_l2_intc_data *),
206 const char *intc_name)
Florian Fainellia5042de22014-09-09 17:44:21 -0700207{
208 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
209 struct bcm7120_l2_intc_data *data;
210 struct irq_chip_generic *gc;
211 struct irq_chip_type *ct;
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800212 int ret = 0;
Kevin Cernekeec17261f2014-11-06 22:44:28 -0800213 unsigned int idx, irq, flags;
Florian Fainellia5042de22014-09-09 17:44:21 -0700214
215 data = kzalloc(sizeof(*data), GFP_KERNEL);
216 if (!data)
217 return -ENOMEM;
218
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800219 data->num_parent_irqs = of_irq_count(dn);
220 if (data->num_parent_irqs <= 0) {
Florian Fainellia5042de22014-09-09 17:44:21 -0700221 pr_err("invalid number of parent interrupts\n");
222 ret = -ENOMEM;
223 goto out_unmap;
224 }
225
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800226 ret = iomap_regs_fn(dn, data);
227 if (ret < 0)
Florian Fainellia5042de22014-09-09 17:44:21 -0700228 goto out_unmap;
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800229
230 for (idx = 0; idx < data->n_words; idx++) {
231 __raw_writel(data->irq_fwd_mask[idx],
232 data->pair_base[idx] +
233 data->en_offset[idx]);
Florian Fainellia5042de22014-09-09 17:44:21 -0700234 }
235
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800236 for (irq = 0; irq < data->num_parent_irqs; irq++) {
237 ret = bcm7120_l2_intc_init_one(dn, data, irq);
Florian Fainellia5042de22014-09-09 17:44:21 -0700238 if (ret)
239 goto out_unmap;
240 }
241
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800242 data->domain = irq_domain_add_linear(dn, IRQS_PER_WORD * data->n_words,
243 &irq_generic_chip_ops, NULL);
Florian Fainellia5042de22014-09-09 17:44:21 -0700244 if (!data->domain) {
245 ret = -ENOMEM;
246 goto out_unmap;
247 }
248
Kevin Cernekeec17261f2014-11-06 22:44:28 -0800249 /* MIPS chips strapped for BE will automagically configure the
250 * peripheral registers for CPU-native byte order.
251 */
252 flags = IRQ_GC_INIT_MASK_CACHE;
253 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
254 flags |= IRQ_GC_BE_IO;
255
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800256 ret = irq_alloc_domain_generic_chips(data->domain, IRQS_PER_WORD, 1,
Kevin Cernekeec17261f2014-11-06 22:44:28 -0800257 dn->full_name, handle_level_irq, clr, 0, flags);
Florian Fainellia5042de22014-09-09 17:44:21 -0700258 if (ret) {
259 pr_err("failed to allocate generic irq chip\n");
260 goto out_free_domain;
261 }
262
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800263 if (of_property_read_bool(dn, "brcm,irq-can-wake"))
Florian Fainellia5042de22014-09-09 17:44:21 -0700264 data->can_wake = true;
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800265
266 for (idx = 0; idx < data->n_words; idx++) {
267 irq = idx * IRQS_PER_WORD;
268 gc = irq_get_domain_generic_chip(data->domain, irq);
269
270 gc->unused = 0xffffffff & ~data->irq_map_mask[idx];
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800271 gc->private = data;
272 ct = gc->chip_types;
273
Kevin Cernekee5b5468c2014-12-25 09:49:03 -0800274 gc->reg_base = data->pair_base[idx];
275 ct->regs.mask = data->en_offset[idx];
276
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800277 ct->chip.irq_mask = irq_gc_mask_clr_bit;
278 ct->chip.irq_unmask = irq_gc_mask_set_bit;
279 ct->chip.irq_ack = irq_gc_noop;
Brian Norrisfd537762015-07-22 16:21:40 -0700280 gc->suspend = bcm7120_l2_intc_suspend;
281 gc->resume = bcm7120_l2_intc_resume;
282
283 /*
284 * Initialize mask-cache, in case we need it for
285 * saving/restoring fwd mask even w/o any child interrupts
286 * installed
287 */
288 gc->mask_cache = irq_reg_readl(gc, ct->regs.mask);
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800289
290 if (data->can_wake) {
291 /* This IRQ chip can wake the system, set all
292 * relevant child interupts in wake_enabled mask
293 */
294 gc->wake_enabled = 0xffffffff;
295 gc->wake_enabled &= ~gc->unused;
296 ct->chip.irq_set_wake = irq_gc_set_wake;
297 }
Florian Fainellia5042de22014-09-09 17:44:21 -0700298 }
299
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800300 pr_info("registered %s intc (mem: 0x%p, parent IRQ(s): %d)\n",
301 intc_name, data->map_base[0], data->num_parent_irqs);
Florian Fainellia5042de22014-09-09 17:44:21 -0700302
303 return 0;
304
305out_free_domain:
306 irq_domain_remove(data->domain);
307out_unmap:
Kevin Cernekee5b5468c2014-12-25 09:49:03 -0800308 for (idx = 0; idx < MAX_MAPPINGS; idx++) {
309 if (data->map_base[idx])
310 iounmap(data->map_base[idx]);
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800311 }
Florian Fainellia5042de22014-09-09 17:44:21 -0700312 kfree(data);
313 return ret;
314}
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800315
316int __init bcm7120_l2_intc_probe_7120(struct device_node *dn,
317 struct device_node *parent)
318{
319 return bcm7120_l2_intc_probe(dn, parent, bcm7120_l2_intc_iomap_7120,
320 "BCM7120 L2");
321}
322
Kevin Cernekee7b7230e2014-12-25 09:49:05 -0800323int __init bcm7120_l2_intc_probe_3380(struct device_node *dn,
324 struct device_node *parent)
325{
326 return bcm7120_l2_intc_probe(dn, parent, bcm7120_l2_intc_iomap_3380,
327 "BCM3380 L2");
328}
329
Kevin Cernekeea4fcbb82014-11-06 22:44:27 -0800330IRQCHIP_DECLARE(bcm7120_l2_intc, "brcm,bcm7120-l2-intc",
Kevin Cernekeeca40f1b2014-12-25 09:49:04 -0800331 bcm7120_l2_intc_probe_7120);
Kevin Cernekee7b7230e2014-12-25 09:49:05 -0800332
333IRQCHIP_DECLARE(bcm3380_l2_intc, "brcm,bcm3380-l2-intc",
334 bcm7120_l2_intc_probe_3380);