Blame view

drivers/irqchip/irq-crossbar.c 8.42 KB
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
1
2
3
4
5
6
7
8
9
10
11
12
13
  /*
   *  drivers/irqchip/irq-crossbar.c
   *
   *  Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
   *  Author: Sricharan R <r.sricharan@ti.com>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   *
   */
  #include <linux/err.h>
  #include <linux/io.h>
41a83e06e   Joel Porquet   irqchip: Prepare ...
14
  #include <linux/irqchip.h>
783d31863   Marc Zyngier   irqchip: crossbar...
15
  #include <linux/irqdomain.h>
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
16
17
18
  #include <linux/of_address.h>
  #include <linux/of_irq.h>
  #include <linux/slab.h>
783d31863   Marc Zyngier   irqchip: crossbar...
19

96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
20
  #define IRQ_FREE	-1
1d50d2ce6   Nishanth Menon   irqchip: crossbar...
21
  #define IRQ_RESERVED	-2
64e0f8ba5   Nishanth Menon   irqchip: crossbar...
22
  #define IRQ_SKIP	-3
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
23
  #define GIC_IRQ_START	32
e30ef8abb   Nishanth Menon   irqchip: crossbar...
24
25
  /**
   * struct crossbar_device - crossbar device description
783d31863   Marc Zyngier   irqchip: crossbar...
26
   * @lock: spinlock serializing access to @irq_map
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
27
   * @int_max: maximum number of supported interrupts
a35057d1d   Nishanth Menon   irqchip: crossbar...
28
   * @safe_map: safe default value to initialize the crossbar
2f7d2fb71   Nishanth Menon   irqchip: crossbar...
29
   * @max_crossbar_sources: Maximum number of crossbar sources
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
30
31
32
   * @irq_map: array of interrupts to crossbar number mapping
   * @crossbar_base: crossbar base address
   * @register_offsets: offsets for each irq number
e30ef8abb   Nishanth Menon   irqchip: crossbar...
33
   * @write: register write function pointer
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
34
35
   */
  struct crossbar_device {
783d31863   Marc Zyngier   irqchip: crossbar...
36
  	raw_spinlock_t lock;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
37
  	uint int_max;
a35057d1d   Nishanth Menon   irqchip: crossbar...
38
  	uint safe_map;
2f7d2fb71   Nishanth Menon   irqchip: crossbar...
39
  	uint max_crossbar_sources;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
40
41
42
  	uint *irq_map;
  	void __iomem *crossbar_base;
  	int *register_offsets;
a35057d1d   Nishanth Menon   irqchip: crossbar...
43
  	void (*write)(int, int);
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
44
45
46
  };
  
  static struct crossbar_device *cb;
783d31863   Marc Zyngier   irqchip: crossbar...
47
  static void crossbar_writel(int irq_no, int cb_no)
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
48
49
50
  {
  	writel(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
  }
783d31863   Marc Zyngier   irqchip: crossbar...
51
  static void crossbar_writew(int irq_no, int cb_no)
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
52
53
54
  {
  	writew(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
  }
783d31863   Marc Zyngier   irqchip: crossbar...
55
  static void crossbar_writeb(int irq_no, int cb_no)
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
56
57
58
  {
  	writeb(cb_no, cb->crossbar_base + cb->register_offsets[irq_no]);
  }
783d31863   Marc Zyngier   irqchip: crossbar...
59
60
61
62
63
64
  static struct irq_chip crossbar_chip = {
  	.name			= "CBAR",
  	.irq_eoi		= irq_chip_eoi_parent,
  	.irq_mask		= irq_chip_mask_parent,
  	.irq_unmask		= irq_chip_unmask_parent,
  	.irq_retrigger		= irq_chip_retrigger_hierarchy,
e269ec423   Grygorii Strashko   irqchip/crossbar:...
65
  	.irq_set_type		= irq_chip_set_type_parent,
8200fe434   Grygorii Strashko   irqchip/crossbar:...
66
67
  	.flags			= IRQCHIP_MASK_ON_SUSPEND |
  				  IRQCHIP_SKIP_SET_WAKE,
783d31863   Marc Zyngier   irqchip: crossbar...
68
69
70
71
  #ifdef CONFIG_SMP
  	.irq_set_affinity	= irq_chip_set_affinity_parent,
  #endif
  };
6f16fc878   Nishanth Menon   irqchip: crossbar...
72

783d31863   Marc Zyngier   irqchip: crossbar...
73
74
  static int allocate_gic_irq(struct irq_domain *domain, unsigned virq,
  			    irq_hw_number_t hwirq)
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
75
  {
f833f57ff   Marc Zyngier   irqchip: Convert ...
76
  	struct irq_fwspec fwspec;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
77
  	int i;
783d31863   Marc Zyngier   irqchip: crossbar...
78
  	int err;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
79

f833f57ff   Marc Zyngier   irqchip: Convert ...
80
81
  	if (!irq_domain_get_of_node(domain->parent))
  		return -EINVAL;
783d31863   Marc Zyngier   irqchip: crossbar...
82
  	raw_spin_lock(&cb->lock);
ddee0fb46   Nishanth Menon   irqchip: crossbar...
83
  	for (i = cb->int_max - 1; i >= 0; i--) {
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
84
  		if (cb->irq_map[i] == IRQ_FREE) {
783d31863   Marc Zyngier   irqchip: crossbar...
85
86
  			cb->irq_map[i] = hwirq;
  			break;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
87
88
  		}
  	}
783d31863   Marc Zyngier   irqchip: crossbar...
89
  	raw_spin_unlock(&cb->lock);
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
90

783d31863   Marc Zyngier   irqchip: crossbar...
91
92
  	if (i < 0)
  		return -ENODEV;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
93

f833f57ff   Marc Zyngier   irqchip: Convert ...
94
95
96
97
98
  	fwspec.fwnode = domain->parent->fwnode;
  	fwspec.param_count = 3;
  	fwspec.param[0] = 0;	/* SPI */
  	fwspec.param[1] = i;
  	fwspec.param[2] = IRQ_TYPE_LEVEL_HIGH;
d360892d3   Nishanth Menon   irqchip: crossbar...
99

f833f57ff   Marc Zyngier   irqchip: Convert ...
100
  	err = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
783d31863   Marc Zyngier   irqchip: crossbar...
101
102
103
104
  	if (err)
  		cb->irq_map[i] = IRQ_FREE;
  	else
  		cb->write(i, hwirq);
29918b679   Nishanth Menon   irqchip: crossbar...
105

783d31863   Marc Zyngier   irqchip: crossbar...
106
  	return err;
29918b679   Nishanth Menon   irqchip: crossbar...
107
  }
783d31863   Marc Zyngier   irqchip: crossbar...
108
109
  static int crossbar_domain_alloc(struct irq_domain *d, unsigned int virq,
  				 unsigned int nr_irqs, void *data)
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
110
  {
f833f57ff   Marc Zyngier   irqchip: Convert ...
111
  	struct irq_fwspec *fwspec = data;
783d31863   Marc Zyngier   irqchip: crossbar...
112
113
  	irq_hw_number_t hwirq;
  	int i;
f833f57ff   Marc Zyngier   irqchip: Convert ...
114
  	if (fwspec->param_count != 3)
783d31863   Marc Zyngier   irqchip: crossbar...
115
  		return -EINVAL;	/* Not GIC compliant */
f833f57ff   Marc Zyngier   irqchip: Convert ...
116
  	if (fwspec->param[0] != 0)
783d31863   Marc Zyngier   irqchip: crossbar...
117
  		return -EINVAL;	/* No PPI should point to this domain */
f833f57ff   Marc Zyngier   irqchip: Convert ...
118
  	hwirq = fwspec->param[1];
783d31863   Marc Zyngier   irqchip: crossbar...
119
120
121
122
123
124
125
126
127
128
129
130
  	if ((hwirq + nr_irqs) > cb->max_crossbar_sources)
  		return -EINVAL;	/* Can't deal with this */
  
  	for (i = 0; i < nr_irqs; i++) {
  		int err = allocate_gic_irq(d, virq + i, hwirq + i);
  
  		if (err)
  			return err;
  
  		irq_domain_set_hwirq_and_chip(d, virq + i, hwirq + i,
  					      &crossbar_chip, NULL);
  	}
29918b679   Nishanth Menon   irqchip: crossbar...
131

96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
132
133
  	return 0;
  }
8b09a45dc   Sricharan R   irqchip: crossbar...
134
  /**
783d31863   Marc Zyngier   irqchip: crossbar...
135
136
137
138
   * crossbar_domain_free - unmap/free a crossbar<->irq connection
   * @domain: domain of irq to unmap
   * @virq: virq number
   * @nr_irqs: number of irqs to free
8b09a45dc   Sricharan R   irqchip: crossbar...
139
140
141
142
143
144
145
   *
   * We do not maintain a use count of total number of map/unmap
   * calls for a particular irq to find out if a irq can be really
   * unmapped. This is because unmap is called during irq_dispose_mapping(irq),
   * after which irq is anyways unusable. So an explicit map has to be called
   * after that.
   */
783d31863   Marc Zyngier   irqchip: crossbar...
146
147
  static void crossbar_domain_free(struct irq_domain *domain, unsigned int virq,
  				 unsigned int nr_irqs)
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
148
  {
783d31863   Marc Zyngier   irqchip: crossbar...
149
  	int i;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
150

783d31863   Marc Zyngier   irqchip: crossbar...
151
152
153
154
155
156
157
  	raw_spin_lock(&cb->lock);
  	for (i = 0; i < nr_irqs; i++) {
  		struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
  
  		irq_domain_reset_irq_data(d);
  		cb->irq_map[d->hwirq] = IRQ_FREE;
  		cb->write(d->hwirq, cb->safe_map);
a35057d1d   Nishanth Menon   irqchip: crossbar...
158
  	}
783d31863   Marc Zyngier   irqchip: crossbar...
159
  	raw_spin_unlock(&cb->lock);
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
160
  }
f833f57ff   Marc Zyngier   irqchip: Convert ...
161
162
163
164
  static int crossbar_domain_translate(struct irq_domain *d,
  				     struct irq_fwspec *fwspec,
  				     unsigned long *hwirq,
  				     unsigned int *type)
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
165
  {
f833f57ff   Marc Zyngier   irqchip: Convert ...
166
167
168
  	if (is_of_node(fwspec->fwnode)) {
  		if (fwspec->param_count != 3)
  			return -EINVAL;
783d31863   Marc Zyngier   irqchip: crossbar...
169

f833f57ff   Marc Zyngier   irqchip: Convert ...
170
171
172
173
174
  		/* No PPI should point to this domain */
  		if (fwspec->param[0] != 0)
  			return -EINVAL;
  
  		*hwirq = fwspec->param[1];
a2a8fa556   Jon Hunter   irqchip: Mask the...
175
  		*type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
f833f57ff   Marc Zyngier   irqchip: Convert ...
176
177
178
179
  		return 0;
  	}
  
  	return -EINVAL;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
180
  }
783d31863   Marc Zyngier   irqchip: crossbar...
181
  static const struct irq_domain_ops crossbar_domain_ops = {
f833f57ff   Marc Zyngier   irqchip: Convert ...
182
183
184
  	.alloc		= crossbar_domain_alloc,
  	.free		= crossbar_domain_free,
  	.translate	= crossbar_domain_translate,
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
185
186
187
188
  };
  
  static int __init crossbar_of_init(struct device_node *node)
  {
edb442def   Nishanth Menon   irqchip: crossbar...
189
  	int i, size, max = 0, reserved = 0, entry;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
190
  	const __be32 *irqsr;
edb442def   Nishanth Menon   irqchip: crossbar...
191
  	int ret = -ENOMEM;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
192

3894e9e82   Dan Carpenter   irqchip: irq-cros...
193
  	cb = kzalloc(sizeof(*cb), GFP_KERNEL);
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
194
195
  
  	if (!cb)
edb442def   Nishanth Menon   irqchip: crossbar...
196
  		return ret;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
197
198
199
  
  	cb->crossbar_base = of_iomap(node, 0);
  	if (!cb->crossbar_base)
3c44d5151   Nishanth Menon   irqchip: crossbar...
200
  		goto err_cb;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
201

2f7d2fb71   Nishanth Menon   irqchip: crossbar...
202
203
204
205
206
207
208
209
  	of_property_read_u32(node, "ti,max-crossbar-sources",
  			     &cb->max_crossbar_sources);
  	if (!cb->max_crossbar_sources) {
  		pr_err("missing 'ti,max-crossbar-sources' property
  ");
  		ret = -EINVAL;
  		goto err_base;
  	}
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
210
  	of_property_read_u32(node, "ti,max-irqs", &max);
edb442def   Nishanth Menon   irqchip: crossbar...
211
212
213
214
  	if (!max) {
  		pr_err("missing 'ti,max-irqs' property
  ");
  		ret = -EINVAL;
3c44d5151   Nishanth Menon   irqchip: crossbar...
215
  		goto err_base;
edb442def   Nishanth Menon   irqchip: crossbar...
216
  	}
4dbf45e3c   Nishanth Menon   irqchip: crossbar...
217
  	cb->irq_map = kcalloc(max, sizeof(int), GFP_KERNEL);
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
218
  	if (!cb->irq_map)
3c44d5151   Nishanth Menon   irqchip: crossbar...
219
  		goto err_base;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
  
  	cb->int_max = max;
  
  	for (i = 0; i < max; i++)
  		cb->irq_map[i] = IRQ_FREE;
  
  	/* Get and mark reserved irqs */
  	irqsr = of_get_property(node, "ti,irqs-reserved", &size);
  	if (irqsr) {
  		size /= sizeof(__be32);
  
  		for (i = 0; i < size; i++) {
  			of_property_read_u32_index(node,
  						   "ti,irqs-reserved",
  						   i, &entry);
702f7e36f   Dan Carpenter   irqchip: crossbar...
235
  			if (entry >= max) {
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
236
237
  				pr_err("Invalid reserved entry
  ");
edb442def   Nishanth Menon   irqchip: crossbar...
238
  				ret = -EINVAL;
3c44d5151   Nishanth Menon   irqchip: crossbar...
239
  				goto err_irq_map;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
240
  			}
1d50d2ce6   Nishanth Menon   irqchip: crossbar...
241
  			cb->irq_map[entry] = IRQ_RESERVED;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
242
243
  		}
  	}
64e0f8ba5   Nishanth Menon   irqchip: crossbar...
244
245
246
247
248
249
250
251
252
  	/* Skip irqs hardwired to bypass the crossbar */
  	irqsr = of_get_property(node, "ti,irqs-skip", &size);
  	if (irqsr) {
  		size /= sizeof(__be32);
  
  		for (i = 0; i < size; i++) {
  			of_property_read_u32_index(node,
  						   "ti,irqs-skip",
  						   i, &entry);
702f7e36f   Dan Carpenter   irqchip: crossbar...
253
  			if (entry >= max) {
64e0f8ba5   Nishanth Menon   irqchip: crossbar...
254
255
256
  				pr_err("Invalid skip entry
  ");
  				ret = -EINVAL;
3c44d5151   Nishanth Menon   irqchip: crossbar...
257
  				goto err_irq_map;
64e0f8ba5   Nishanth Menon   irqchip: crossbar...
258
259
260
261
  			}
  			cb->irq_map[entry] = IRQ_SKIP;
  		}
  	}
4dbf45e3c   Nishanth Menon   irqchip: crossbar...
262
  	cb->register_offsets = kcalloc(max, sizeof(int), GFP_KERNEL);
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
263
  	if (!cb->register_offsets)
3c44d5151   Nishanth Menon   irqchip: crossbar...
264
  		goto err_irq_map;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
  
  	of_property_read_u32(node, "ti,reg-size", &size);
  
  	switch (size) {
  	case 1:
  		cb->write = crossbar_writeb;
  		break;
  	case 2:
  		cb->write = crossbar_writew;
  		break;
  	case 4:
  		cb->write = crossbar_writel;
  		break;
  	default:
  		pr_err("Invalid reg-size property
  ");
edb442def   Nishanth Menon   irqchip: crossbar...
281
  		ret = -EINVAL;
3c44d5151   Nishanth Menon   irqchip: crossbar...
282
  		goto err_reg_offset;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
283
284
285
286
287
288
289
290
  		break;
  	}
  
  	/*
  	 * Register offsets are not linear because of the
  	 * reserved irqs. so find and store the offsets once.
  	 */
  	for (i = 0; i < max; i++) {
1d50d2ce6   Nishanth Menon   irqchip: crossbar...
291
  		if (cb->irq_map[i] == IRQ_RESERVED)
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
292
293
294
295
296
  			continue;
  
  		cb->register_offsets[i] = reserved;
  		reserved += size;
  	}
a35057d1d   Nishanth Menon   irqchip: crossbar...
297
  	of_property_read_u32(node, "ti,irqs-safe-map", &cb->safe_map);
a35057d1d   Nishanth Menon   irqchip: crossbar...
298
299
300
301
302
303
304
305
  	/* Initialize the crossbar with safe map to start with */
  	for (i = 0; i < max; i++) {
  		if (cb->irq_map[i] == IRQ_RESERVED ||
  		    cb->irq_map[i] == IRQ_SKIP)
  			continue;
  
  		cb->write(i, cb->safe_map);
  	}
783d31863   Marc Zyngier   irqchip: crossbar...
306
  	raw_spin_lock_init(&cb->lock);
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
307
  	return 0;
3c44d5151   Nishanth Menon   irqchip: crossbar...
308
  err_reg_offset:
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
309
  	kfree(cb->register_offsets);
3c44d5151   Nishanth Menon   irqchip: crossbar...
310
  err_irq_map:
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
311
  	kfree(cb->irq_map);
3c44d5151   Nishanth Menon   irqchip: crossbar...
312
  err_base:
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
313
  	iounmap(cb->crossbar_base);
3c44d5151   Nishanth Menon   irqchip: crossbar...
314
  err_cb:
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
315
  	kfree(cb);
99e37d0ea   Sricharan R   irqchip: crossbar...
316
317
  
  	cb = NULL;
edb442def   Nishanth Menon   irqchip: crossbar...
318
  	return ret;
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
319
  }
783d31863   Marc Zyngier   irqchip: crossbar...
320
321
  static int __init irqcrossbar_init(struct device_node *node,
  				   struct device_node *parent)
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
322
  {
783d31863   Marc Zyngier   irqchip: crossbar...
323
324
325
326
327
328
  	struct irq_domain *parent_domain, *domain;
  	int err;
  
  	if (!parent) {
  		pr_err("%s: no parent, giving up
  ", node->full_name);
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
329
  		return -ENODEV;
783d31863   Marc Zyngier   irqchip: crossbar...
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
  	}
  
  	parent_domain = irq_find_host(parent);
  	if (!parent_domain) {
  		pr_err("%s: unable to obtain parent domain
  ", node->full_name);
  		return -ENXIO;
  	}
  
  	err = crossbar_of_init(node);
  	if (err)
  		return err;
  
  	domain = irq_domain_add_hierarchy(parent_domain, 0,
  					  cb->max_crossbar_sources,
  					  node, &crossbar_domain_ops,
  					  NULL);
  	if (!domain) {
  		pr_err("%s: failed to allocated domain
  ", node->full_name);
  		return -ENOMEM;
  	}
96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
352

96ca848ef   Sricharan R   DRIVERS: IRQCHIP:...
353
354
  	return 0;
  }
783d31863   Marc Zyngier   irqchip: crossbar...
355
356
  
  IRQCHIP_DECLARE(ti_irqcrossbar, "ti,irq-crossbar", irqcrossbar_init);