Blame view

drivers/irqchip/irq-bcm7120-l2.c 9.06 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
a5042de26   Florian Fainelli   irqchip: bcm7120-...
2
3
4
5
  /*
   * Broadcom BCM7120 style Level 2 interrupt controller driver
   *
   * Copyright (C) 2014 Broadcom Corporation
a5042de26   Florian Fainelli   irqchip: bcm7120-...
6
7
8
9
10
11
12
   */
  
  #define pr_fmt(fmt)	KBUILD_MODNAME	": " fmt
  
  #include <linux/init.h>
  #include <linux/slab.h>
  #include <linux/module.h>
7b7230e70   Kevin Cernekee   IRQCHIP: bcm7120-...
13
  #include <linux/kernel.h>
a5042de26   Florian Fainelli   irqchip: bcm7120-...
14
15
16
17
18
19
20
21
22
23
  #include <linux/platform_device.h>
  #include <linux/of.h>
  #include <linux/of_irq.h>
  #include <linux/of_address.h>
  #include <linux/of_platform.h>
  #include <linux/interrupt.h>
  #include <linux/irq.h>
  #include <linux/io.h>
  #include <linux/irqdomain.h>
  #include <linux/reboot.h>
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
24
  #include <linux/bitops.h>
41a83e06e   Joel Porquet   irqchip: Prepare ...
25
  #include <linux/irqchip.h>
a5042de26   Florian Fainelli   irqchip: bcm7120-...
26
  #include <linux/irqchip/chained_irq.h>
a5042de26   Florian Fainelli   irqchip: bcm7120-...
27
28
29
  /* Register offset in the L2 interrupt controller */
  #define IRQEN		0x00
  #define IRQSTAT		0x04
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
30
  #define MAX_WORDS	4
ca40f1b23   Kevin Cernekee   IRQCHIP: bcm7120-...
31
  #define MAX_MAPPINGS	(MAX_WORDS * 2)
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
32
  #define IRQS_PER_WORD	32
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
33
34
35
36
  struct bcm7120_l1_intc_data {
  	struct bcm7120_l2_intc_data *b;
  	u32 irq_map_mask[MAX_WORDS];
  };
a5042de26   Florian Fainelli   irqchip: bcm7120-...
37
  struct bcm7120_l2_intc_data {
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
38
  	unsigned int n_words;
5b5468cf1   Kevin Cernekee   IRQCHIP: bcm7120-...
39
40
41
42
  	void __iomem *map_base[MAX_MAPPINGS];
  	void __iomem *pair_base[MAX_WORDS];
  	int en_offset[MAX_WORDS];
  	int stat_offset[MAX_WORDS];
a5042de26   Florian Fainelli   irqchip: bcm7120-...
43
44
  	struct irq_domain *domain;
  	bool can_wake;
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
45
  	u32 irq_fwd_mask[MAX_WORDS];
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
46
  	struct bcm7120_l1_intc_data *l1_data;
ca40f1b23   Kevin Cernekee   IRQCHIP: bcm7120-...
47
48
  	int num_parent_irqs;
  	const __be32 *map_mask_prop;
a5042de26   Florian Fainelli   irqchip: bcm7120-...
49
  };
bd0b9ac40   Thomas Gleixner   genirq: Remove ir...
50
  static void bcm7120_l2_intc_irq_handle(struct irq_desc *desc)
a5042de26   Florian Fainelli   irqchip: bcm7120-...
51
  {
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
52
53
  	struct bcm7120_l1_intc_data *data = irq_desc_get_handler_data(desc);
  	struct bcm7120_l2_intc_data *b = data->b;
a5042de26   Florian Fainelli   irqchip: bcm7120-...
54
  	struct irq_chip *chip = irq_desc_get_chip(desc);
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
55
  	unsigned int idx;
a5042de26   Florian Fainelli   irqchip: bcm7120-...
56
57
  
  	chained_irq_enter(chip, desc);
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
58
59
60
61
62
63
64
65
  	for (idx = 0; idx < b->n_words; idx++) {
  		int base = idx * IRQS_PER_WORD;
  		struct irq_chip_generic *gc =
  			irq_get_domain_generic_chip(b->domain, base);
  		unsigned long pending;
  		int hwirq;
  
  		irq_gc_lock(gc);
5b5468cf1   Kevin Cernekee   IRQCHIP: bcm7120-...
66
  		pending = irq_reg_readl(gc, b->stat_offset[idx]) &
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
67
68
  					    gc->mask_cache &
  					    data->irq_map_mask[idx];
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
69
70
71
72
73
74
  		irq_gc_unlock(gc);
  
  		for_each_set_bit(hwirq, &pending, IRQS_PER_WORD) {
  			generic_handle_irq(irq_find_mapping(b->domain,
  					   base + hwirq));
  		}
a5042de26   Florian Fainelli   irqchip: bcm7120-...
75
  	}
a5042de26   Florian Fainelli   irqchip: bcm7120-...
76
77
  	chained_irq_exit(chip, desc);
  }
fd5377667   Brian Norris   irqchip/bcm7120-l...
78
  static void bcm7120_l2_intc_suspend(struct irq_chip_generic *gc)
a5042de26   Florian Fainelli   irqchip: bcm7120-...
79
  {
a5042de26   Florian Fainelli   irqchip: bcm7120-...
80
  	struct bcm7120_l2_intc_data *b = gc->private;
fd5377667   Brian Norris   irqchip/bcm7120-l...
81
  	struct irq_chip_type *ct = gc->chip_types;
a5042de26   Florian Fainelli   irqchip: bcm7120-...
82
83
  
  	irq_gc_lock(gc);
c17261fac   Kevin Cernekee   irqchip: bcm7120-...
84
  	if (b->can_wake)
5b5468cf1   Kevin Cernekee   IRQCHIP: bcm7120-...
85
86
  		irq_reg_writel(gc, gc->mask_cache | gc->wake_active,
  			       ct->regs.mask);
a5042de26   Florian Fainelli   irqchip: bcm7120-...
87
88
  	irq_gc_unlock(gc);
  }
fd5377667   Brian Norris   irqchip/bcm7120-l...
89
  static void bcm7120_l2_intc_resume(struct irq_chip_generic *gc)
a5042de26   Florian Fainelli   irqchip: bcm7120-...
90
  {
fd5377667   Brian Norris   irqchip/bcm7120-l...
91
  	struct irq_chip_type *ct = gc->chip_types;
a5042de26   Florian Fainelli   irqchip: bcm7120-...
92
93
94
  
  	/* Restore the saved mask */
  	irq_gc_lock(gc);
5b5468cf1   Kevin Cernekee   IRQCHIP: bcm7120-...
95
  	irq_reg_writel(gc, gc->mask_cache, ct->regs.mask);
a5042de26   Florian Fainelli   irqchip: bcm7120-...
96
97
98
99
100
  	irq_gc_unlock(gc);
  }
  
  static int bcm7120_l2_intc_init_one(struct device_node *dn,
  					struct bcm7120_l2_intc_data *data,
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
101
  					int irq, u32 *valid_mask)
a5042de26   Florian Fainelli   irqchip: bcm7120-...
102
  {
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
103
  	struct bcm7120_l1_intc_data *l1_data = &data->l1_data[irq];
a5042de26   Florian Fainelli   irqchip: bcm7120-...
104
  	int parent_irq;
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
105
  	unsigned int idx;
a5042de26   Florian Fainelli   irqchip: bcm7120-...
106
107
  
  	parent_irq = irq_of_parse_and_map(dn, irq);
714710e1a   Dmitry Torokhov   irqchip: bcm7120-...
108
  	if (!parent_irq) {
a5042de26   Florian Fainelli   irqchip: bcm7120-...
109
110
  		pr_err("failed to map interrupt %d
  ", irq);
714710e1a   Dmitry Torokhov   irqchip: bcm7120-...
111
  		return -EINVAL;
a5042de26   Florian Fainelli   irqchip: bcm7120-...
112
  	}
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
113
114
  	/* For multiple parent IRQs with multiple words, this looks like:
  	 * <irq0_w0 irq0_w1 irq1_w0 irq1_w1 ...>
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
115
116
117
118
119
120
  	 *
  	 * We need to associate a given parent interrupt with its corresponding
  	 * map_mask in order to mask the status register with it because we
  	 * have the same handler being called for multiple parent interrupts.
  	 *
  	 * This is typically something needed on BCM7xxx (STB chips).
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
121
  	 */
7b7230e70   Kevin Cernekee   IRQCHIP: bcm7120-...
122
123
  	for (idx = 0; idx < data->n_words; idx++) {
  		if (data->map_mask_prop) {
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
124
  			l1_data->irq_map_mask[idx] |=
7b7230e70   Kevin Cernekee   IRQCHIP: bcm7120-...
125
126
127
  				be32_to_cpup(data->map_mask_prop +
  					     irq * data->n_words + idx);
  		} else {
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
128
  			l1_data->irq_map_mask[idx] = 0xffffffff;
7b7230e70   Kevin Cernekee   IRQCHIP: bcm7120-...
129
  		}
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
130
  		valid_mask[idx] |= l1_data->irq_map_mask[idx];
7b7230e70   Kevin Cernekee   IRQCHIP: bcm7120-...
131
  	}
a5042de26   Florian Fainelli   irqchip: bcm7120-...
132

0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
133
  	l1_data->b = data;
a5042de26   Florian Fainelli   irqchip: bcm7120-...
134

0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
135
136
  	irq_set_chained_handler_and_data(parent_irq,
  					 bcm7120_l2_intc_irq_handle, l1_data);
f4ccb7456   Justin Chen   irqchip/bcm7120-l...
137
138
  	if (data->can_wake)
  		enable_irq_wake(parent_irq);
a5042de26   Florian Fainelli   irqchip: bcm7120-...
139
140
  	return 0;
  }
ca40f1b23   Kevin Cernekee   IRQCHIP: bcm7120-...
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
  static int __init bcm7120_l2_intc_iomap_7120(struct device_node *dn,
  					     struct bcm7120_l2_intc_data *data)
  {
  	int ret;
  
  	data->map_base[0] = of_iomap(dn, 0);
  	if (!data->map_base[0]) {
  		pr_err("unable to map registers
  ");
  		return -ENOMEM;
  	}
  
  	data->pair_base[0] = data->map_base[0];
  	data->en_offset[0] = IRQEN;
  	data->stat_offset[0] = IRQSTAT;
  	data->n_words = 1;
  
  	ret = of_property_read_u32_array(dn, "brcm,int-fwd-mask",
  					 data->irq_fwd_mask, data->n_words);
  	if (ret != 0 && ret != -EINVAL) {
  		/* property exists but has the wrong number of words */
  		pr_err("invalid brcm,int-fwd-mask property
  ");
  		return -EINVAL;
  	}
  
  	data->map_mask_prop = of_get_property(dn, "brcm,int-map-mask", &ret);
  	if (!data->map_mask_prop ||
  	    (ret != (sizeof(__be32) * data->num_parent_irqs * data->n_words))) {
  		pr_err("invalid brcm,int-map-mask property
  ");
  		return -EINVAL;
  	}
  
  	return 0;
  }
7b7230e70   Kevin Cernekee   IRQCHIP: bcm7120-...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  static int __init bcm7120_l2_intc_iomap_3380(struct device_node *dn,
  					     struct bcm7120_l2_intc_data *data)
  {
  	unsigned int gc_idx;
  
  	for (gc_idx = 0; gc_idx < MAX_WORDS; gc_idx++) {
  		unsigned int map_idx = gc_idx * 2;
  		void __iomem *en = of_iomap(dn, map_idx + 0);
  		void __iomem *stat = of_iomap(dn, map_idx + 1);
  		void __iomem *base = min(en, stat);
  
  		data->map_base[map_idx + 0] = en;
  		data->map_base[map_idx + 1] = stat;
  
  		if (!base)
  			break;
  
  		data->pair_base[gc_idx] = base;
  		data->en_offset[gc_idx] = en - base;
  		data->stat_offset[gc_idx] = stat - base;
  	}
  
  	if (!gc_idx) {
  		pr_err("unable to map registers
  ");
  		return -EINVAL;
  	}
  
  	data->n_words = gc_idx;
  	return 0;
  }
dde7e6d1a   Ben Dooks   irqchip/bcm7120-l...
208
  static int __init bcm7120_l2_intc_probe(struct device_node *dn,
ca40f1b23   Kevin Cernekee   IRQCHIP: bcm7120-...
209
210
211
212
  				 struct device_node *parent,
  				 int (*iomap_regs_fn)(struct device_node *,
  					struct bcm7120_l2_intc_data *),
  				 const char *intc_name)
a5042de26   Florian Fainelli   irqchip: bcm7120-...
213
214
215
216
217
  {
  	unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
  	struct bcm7120_l2_intc_data *data;
  	struct irq_chip_generic *gc;
  	struct irq_chip_type *ct;
ca40f1b23   Kevin Cernekee   IRQCHIP: bcm7120-...
218
  	int ret = 0;
c17261fac   Kevin Cernekee   irqchip: bcm7120-...
219
  	unsigned int idx, irq, flags;
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
220
  	u32 valid_mask[MAX_WORDS] = { };
a5042de26   Florian Fainelli   irqchip: bcm7120-...
221
222
223
224
  
  	data = kzalloc(sizeof(*data), GFP_KERNEL);
  	if (!data)
  		return -ENOMEM;
ca40f1b23   Kevin Cernekee   IRQCHIP: bcm7120-...
225
226
  	data->num_parent_irqs = of_irq_count(dn);
  	if (data->num_parent_irqs <= 0) {
a5042de26   Florian Fainelli   irqchip: bcm7120-...
227
228
229
230
231
  		pr_err("invalid number of parent interrupts
  ");
  		ret = -ENOMEM;
  		goto out_unmap;
  	}
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
232
233
234
235
236
237
  	data->l1_data = kcalloc(data->num_parent_irqs, sizeof(*data->l1_data),
  				GFP_KERNEL);
  	if (!data->l1_data) {
  		ret = -ENOMEM;
  		goto out_free_l1_data;
  	}
ca40f1b23   Kevin Cernekee   IRQCHIP: bcm7120-...
238
239
  	ret = iomap_regs_fn(dn, data);
  	if (ret < 0)
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
240
  		goto out_free_l1_data;
ca40f1b23   Kevin Cernekee   IRQCHIP: bcm7120-...
241

f4ccb7456   Justin Chen   irqchip/bcm7120-l...
242
  	data->can_wake = of_property_read_bool(dn, "brcm,irq-can-wake");
ca40f1b23   Kevin Cernekee   IRQCHIP: bcm7120-...
243
  	for (irq = 0; irq < data->num_parent_irqs; irq++) {
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
244
  		ret = bcm7120_l2_intc_init_one(dn, data, irq, valid_mask);
a5042de26   Florian Fainelli   irqchip: bcm7120-...
245
  		if (ret)
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
246
  			goto out_free_l1_data;
a5042de26   Florian Fainelli   irqchip: bcm7120-...
247
  	}
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
248
249
  	data->domain = irq_domain_add_linear(dn, IRQS_PER_WORD * data->n_words,
  					     &irq_generic_chip_ops, NULL);
a5042de26   Florian Fainelli   irqchip: bcm7120-...
250
251
  	if (!data->domain) {
  		ret = -ENOMEM;
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
252
  		goto out_free_l1_data;
a5042de26   Florian Fainelli   irqchip: bcm7120-...
253
  	}
c17261fac   Kevin Cernekee   irqchip: bcm7120-...
254
255
256
257
258
259
  	/* MIPS chips strapped for BE will automagically configure the
  	 * peripheral registers for CPU-native byte order.
  	 */
  	flags = IRQ_GC_INIT_MASK_CACHE;
  	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  		flags |= IRQ_GC_BE_IO;
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
260
  	ret = irq_alloc_domain_generic_chips(data->domain, IRQS_PER_WORD, 1,
c17261fac   Kevin Cernekee   irqchip: bcm7120-...
261
  				dn->full_name, handle_level_irq, clr, 0, flags);
a5042de26   Florian Fainelli   irqchip: bcm7120-...
262
263
264
265
266
  	if (ret) {
  		pr_err("failed to allocate generic irq chip
  ");
  		goto out_free_domain;
  	}
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
267
268
269
  	for (idx = 0; idx < data->n_words; idx++) {
  		irq = idx * IRQS_PER_WORD;
  		gc = irq_get_domain_generic_chip(data->domain, irq);
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
270
  		gc->unused = 0xffffffff & ~valid_mask[idx];
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
271
272
  		gc->private = data;
  		ct = gc->chip_types;
5b5468cf1   Kevin Cernekee   IRQCHIP: bcm7120-...
273
274
  		gc->reg_base = data->pair_base[idx];
  		ct->regs.mask = data->en_offset[idx];
b304605f0   Florian Fainelli   irqchip/irq-bcm71...
275
276
277
  		/* gc->reg_base is defined and so is gc->writel */
  		irq_reg_writel(gc, data->irq_fwd_mask[idx],
  			       data->en_offset[idx]);
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
278
279
280
  		ct->chip.irq_mask = irq_gc_mask_clr_bit;
  		ct->chip.irq_unmask = irq_gc_mask_set_bit;
  		ct->chip.irq_ack = irq_gc_noop;
fd5377667   Brian Norris   irqchip/bcm7120-l...
281
282
283
284
285
286
287
288
289
  		gc->suspend = bcm7120_l2_intc_suspend;
  		gc->resume = bcm7120_l2_intc_resume;
  
  		/*
  		 * Initialize mask-cache, in case we need it for
  		 * saving/restoring fwd mask even w/o any child interrupts
  		 * installed
  		 */
  		gc->mask_cache = irq_reg_readl(gc, ct->regs.mask);
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
290
291
292
293
294
295
296
297
298
  
  		if (data->can_wake) {
  			/* This IRQ chip can wake the system, set all
  			 * relevant child interupts in wake_enabled mask
  			 */
  			gc->wake_enabled = 0xffffffff;
  			gc->wake_enabled &= ~gc->unused;
  			ct->chip.irq_set_wake = irq_gc_set_wake;
  		}
a5042de26   Florian Fainelli   irqchip: bcm7120-...
299
  	}
082ce27ff   Florian Fainelli   irqchip/bcm: Rest...
300
301
302
  	pr_info("registered %s intc (%pOF, parent IRQ(s): %d)
  ",
  		intc_name, dn, data->num_parent_irqs);
a5042de26   Florian Fainelli   irqchip: bcm7120-...
303
304
305
306
  	return 0;
  
  out_free_domain:
  	irq_domain_remove(data->domain);
0aef3997e   Florian Fainelli   irqchip/bcm7120-l...
307
308
  out_free_l1_data:
  	kfree(data->l1_data);
a5042de26   Florian Fainelli   irqchip: bcm7120-...
309
  out_unmap:
5b5468cf1   Kevin Cernekee   IRQCHIP: bcm7120-...
310
311
312
  	for (idx = 0; idx < MAX_MAPPINGS; idx++) {
  		if (data->map_base[idx])
  			iounmap(data->map_base[idx]);
c76acf4df   Kevin Cernekee   irqchip: bcm7120-...
313
  	}
a5042de26   Florian Fainelli   irqchip: bcm7120-...
314
315
316
  	kfree(data);
  	return ret;
  }
ca40f1b23   Kevin Cernekee   IRQCHIP: bcm7120-...
317

dde7e6d1a   Ben Dooks   irqchip/bcm7120-l...
318
319
  static int __init bcm7120_l2_intc_probe_7120(struct device_node *dn,
  					     struct device_node *parent)
ca40f1b23   Kevin Cernekee   IRQCHIP: bcm7120-...
320
321
322
323
  {
  	return bcm7120_l2_intc_probe(dn, parent, bcm7120_l2_intc_iomap_7120,
  				     "BCM7120 L2");
  }
dde7e6d1a   Ben Dooks   irqchip/bcm7120-l...
324
325
  static int __init bcm7120_l2_intc_probe_3380(struct device_node *dn,
  					     struct device_node *parent)
7b7230e70   Kevin Cernekee   IRQCHIP: bcm7120-...
326
327
328
329
  {
  	return bcm7120_l2_intc_probe(dn, parent, bcm7120_l2_intc_iomap_3380,
  				     "BCM3380 L2");
  }
a4fcbb861   Kevin Cernekee   irqchip: bcm7120-...
330
  IRQCHIP_DECLARE(bcm7120_l2_intc, "brcm,bcm7120-l2-intc",
ca40f1b23   Kevin Cernekee   IRQCHIP: bcm7120-...
331
  		bcm7120_l2_intc_probe_7120);
7b7230e70   Kevin Cernekee   IRQCHIP: bcm7120-...
332
333
334
  
  IRQCHIP_DECLARE(bcm3380_l2_intc, "brcm,bcm3380-l2-intc",
  		bcm7120_l2_intc_probe_3380);