package: mtd: move bcm963xx_tag definition into source code
[openwrt/svn-archive/archive.git] / target / linux / brcm63xx / patches-3.14 / 320-irqchip-add-support-for-bcm6345-style-periphery-irq-.patch
1 From 7aaa70416d87434792b73077beb328202975e541 Mon Sep 17 00:00:00 2001
2 From: Jonas Gorski <jogo@openwrt.org>
3 Date: Sun, 30 Nov 2014 14:53:12 +0100
4 Subject: [PATCH 1/5] irqchip: add support for bcm6345-style periphery irq
5 controller
6
7 Signed-off-by: Jonas Gorski <jogo@openwrt.org>
8 ---
9 .../brcm,bcm6345-periph-intc.txt | 50 +++
10 drivers/irqchip/Kconfig | 4 +
11 drivers/irqchip/Makefile | 1 +
12 drivers/irqchip/irq-bcm6345-periph.c | 325 ++++++++++++++++++++
13 include/linux/irqchip/irq-bcm6345-periph.h | 16 +
14 5 files changed, 396 insertions(+)
15 create mode 100644 Documentation/devicetree/bindings/interrupt-controller/brcm,bcm6345-periph-intc.txt
16 create mode 100644 drivers/irqchip/irq-bcm6345-periph.c
17 create mode 100644 include/linux/irqchip/irq-bcm6345-periph.h
18
19 --- /dev/null
20 +++ b/Documentation/devicetree/bindings/interrupt-controller/brcm,bcm6345-periph-intc.txt
21 @@ -0,0 +1,50 @@
22 +Broadcom BCM6345 Level 1 periphery interrupt controller
23 +
24 +This block is a interrupt controller that is typically connected directly
25 +to one of the HW INT lines on each CPU. Every BCM63XX xDSL chip since
26 +BCM6345 has contained this hardware.
27 +
28 +Key elements of the hardware design include:
29 +
30 +- 32, 64, or 128 incoming level IRQ lines
31 +
32 +- All onchip peripherals are wired directly to an L2 input
33 +
34 +- A separate instance of the register set for each CPU, allowing individual
35 + peripheral IRQs to be routed to any CPU
36 +
37 +- No atomic mask/unmask operations
38 +
39 +- No polarity/level/edge settings
40 +
41 +- No FIFO or priority encoder logic; software is expected to read all
42 + 1-4 status words to determine which IRQs are pending
43 +
44 +Required properties:
45 +
46 +- compatible: Should be "brcm,bcm6345-periph-intc".
47 +- reg: Specifies the base physical address and size of the registers.
48 + Multiple register addresses may be specified, and must match the amount of
49 + parent interrupts.
50 +- interrupt-controller: Identifies the node as an interrupt controller.
51 +- #interrupt-cells: Specifies the number of cells needed to encode an interrupt
52 + source, should be 1.
53 +- interrupt-parent: Specifies the phandle to the parent interrupt controller
54 + this one is cascaded from.
55 +- interrupts: Specifies the interrupt line(s) in the interrupt-parent controller
56 + node, valid values depend on the type of parent interrupt controller.
57 + Multiple lines are used to route interrupts to different cpus, with the first
58 + assumed to be for the boot CPU.
59 +
60 +Example:
61 +
62 +periph_intc: interrupt-controller@f0406800 {
63 + compatible = "brcm,bcm6345-periph-intc";
64 + reg = <0x10000020 0x10>, <0x10000030 0x10>;
65 +
66 + interrupt-controller;
67 + #interrupt-cells = <1>;
68 +
69 + interrupt-parent = <&cpu_intc>;
70 + interrupts = <2>, <3>;
71 +};
72 --- a/drivers/irqchip/Kconfig
73 +++ b/drivers/irqchip/Kconfig
74 @@ -30,6 +30,10 @@ config ARM_VIC_NR
75 The maximum number of VICs available in the system, for
76 power management.
77
78 +config BCM6345_PERIPH_IRQ
79 + bool
80 + select IRQ_DOMAIN
81 +
82 config DW_APB_ICTL
83 bool
84 select IRQ_DOMAIN
85 --- a/drivers/irqchip/Makefile
86 +++ b/drivers/irqchip/Makefile
87 @@ -6,6 +6,7 @@ obj-$(CONFIG_ARCH_MMP) += irq-mmp.o
88 obj-$(CONFIG_ARCH_MVEBU) += irq-armada-370-xp.o
89 obj-$(CONFIG_ARCH_MXS) += irq-mxs.o
90 obj-$(CONFIG_ARCH_S3C24XX) += irq-s3c24xx.o
91 +obj-$(CONFIG_BCM6345_PERIPH_IRQ) += irq-bcm6345-periph.o
92 obj-$(CONFIG_DW_APB_ICTL) += irq-dw-apb-ictl.o
93 obj-$(CONFIG_METAG) += irq-metag-ext.o
94 obj-$(CONFIG_METAG_PERFCOUNTER_IRQS) += irq-metag.o
95 --- /dev/null
96 +++ b/drivers/irqchip/irq-bcm6345-periph.c
97 @@ -0,0 +1,341 @@
98 +/*
99 + * This file is subject to the terms and conditions of the GNU General Public
100 + * License. See the file "COPYING" in the main directory of this archive
101 + * for more details.
102 + *
103 + * Copyright (C) 2014 Jonas Gorski <jogo@openwrt.org>
104 + */
105 +
106 +#include <linux/ioport.h>
107 +#include <linux/irq.h>
108 +#include <linux/irqchip/chained_irq.h>
109 +#include <linux/irqchip/irq-bcm6345-periph.h>
110 +#include <linux/kernel.h>
111 +#include <linux/of.h>
112 +#include <linux/of_irq.h>
113 +#include <linux/of_address.h>
114 +#include <linux/slab.h>
115 +#include <linux/spinlock.h>
116 +
117 +#ifdef CONFIG_BCM63XX
118 +#include <asm/mach-bcm63xx/bcm63xx_irq.h>
119 +
120 +#define VIRQ_BASE IRQ_INTERNAL_BASE
121 +#else
122 +#define VIRQ_BASE 0
123 +#endif
124 +
125 +#include "irqchip.h"
126 +
127 +#define MAX_WORDS 4
128 +#define MAX_PARENT_IRQS 2
129 +#define IRQS_PER_WORD 32
130 +
131 +struct intc_block {
132 + int parent_irq;
133 + void __iomem *base;
134 + void __iomem *en_reg[MAX_WORDS];
135 + void __iomem *status_reg[MAX_WORDS];
136 + u32 mask_cache[MAX_WORDS];
137 +};
138 +
139 +struct intc_data {
140 + struct irq_chip chip;
141 + struct intc_block block[MAX_PARENT_IRQS];
142 +
143 + int num_words;
144 +
145 + struct irq_domain *domain;
146 + raw_spinlock_t lock;
147 +};
148 +
149 +static void bcm6345_periph_irq_handle(unsigned int irq, struct irq_desc *desc)
150 +{
151 + struct intc_data *data = irq_desc_get_handler_data(desc);
152 + struct irq_chip *chip = irq_desc_get_chip(desc);
153 + struct intc_block *block;
154 + unsigned int idx;
155 +
156 + chained_irq_enter(chip, desc);
157 +
158 + for (idx = 0; idx < MAX_PARENT_IRQS; idx++)
159 + if (irq == data->block[idx].parent_irq)
160 + block = &data->block[idx];
161 +
162 + for (idx = 0; idx < data->num_words; idx++) {
163 + int base = idx * IRQS_PER_WORD;
164 + unsigned long pending;
165 + int hw_irq;
166 +
167 + raw_spin_lock(&data->lock);
168 + pending = __raw_readl(block->en_reg[idx]) &
169 + __raw_readl(block->status_reg[idx]);
170 + raw_spin_unlock(&data->lock);
171 +
172 + for_each_set_bit(hw_irq, &pending, IRQS_PER_WORD) {
173 + int virq;
174 +
175 + virq = irq_find_mapping(data->domain, base + hw_irq);
176 + generic_handle_irq(virq);
177 + }
178 + }
179 +
180 + chained_irq_exit(chip, desc);
181 +}
182 +
183 +static void __bcm6345_periph_enable(struct intc_block *block, int reg, int bit,
184 + bool enable)
185 +{
186 + u32 val;
187 +
188 + val = __raw_readl(block->en_reg[reg]);
189 + if (enable)
190 + val |= BIT(bit);
191 + else
192 + val &= ~BIT(bit);
193 + __raw_writel(val, block->en_reg[reg]);
194 +}
195 +
196 +static void bcm6345_periph_irq_mask(struct irq_data *data)
197 +{
198 + unsigned int i, reg, bit;
199 + struct intc_data *priv = data->domain->host_data;
200 + irq_hw_number_t hwirq = irqd_to_hwirq(data);
201 +
202 + reg = hwirq / IRQS_PER_WORD;
203 + bit = hwirq % IRQS_PER_WORD;
204 +
205 + raw_spin_lock(&priv->lock);
206 + for (i = 0; i < MAX_PARENT_IRQS; i++) {
207 + struct intc_block *block = &priv->block[i];
208 +
209 + if (!block->parent_irq)
210 + break;
211 +
212 + __bcm6345_periph_enable(block, reg, bit, false);
213 + }
214 + raw_spin_unlock(&priv->lock);
215 +}
216 +
217 +static void bcm6345_periph_irq_unmask(struct irq_data *data)
218 +{
219 + struct intc_data *priv = data->domain->host_data;
220 + irq_hw_number_t hwirq = irqd_to_hwirq(data);
221 + unsigned int i, reg, bit;
222 +
223 + reg = hwirq / IRQS_PER_WORD;
224 + bit = hwirq % IRQS_PER_WORD;
225 +
226 + raw_spin_lock(&priv->lock);
227 + for (i = 0; i < MAX_PARENT_IRQS; i++) {
228 + struct intc_block *block = &priv->block[i];
229 +
230 + if (!block->parent_irq)
231 + break;
232 +
233 + if (block->mask_cache[reg] & BIT(bit))
234 + __bcm6345_periph_enable(block, reg, bit, true);
235 + else
236 + __bcm6345_periph_enable(block, reg, bit, false);
237 +
238 + }
239 + raw_spin_unlock(&priv->lock);
240 +}
241 +
242 +#ifdef CONFIG_SMP
243 +static int bcm6345_periph_set_affinity(struct irq_data *data,
244 + const struct cpumask *mask, bool force)
245 +{
246 + irq_hw_number_t hwirq = irqd_to_hwirq(data);
247 + struct intc_data *priv = data->domain->host_data;
248 + unsigned int i, reg, bit;
249 + unsigned long flags;
250 + bool enabled;
251 + int cpu;
252 +
253 + reg = hwirq / IRQS_PER_WORD;
254 + bit = hwirq % IRQS_PER_WORD;
255 +
256 + /* we could route to more than one cpu, but performance
257 + suffers, so fix it to one.
258 + */
259 + cpu = cpumask_any_and(mask, cpu_online_mask);
260 + if (cpu >= nr_cpu_ids)
261 + return -EINVAL;
262 +
263 + if (cpu >= MAX_PARENT_IRQS)
264 + return -EINVAL;
265 +
266 + if (!priv->block[cpu].parent_irq)
267 + return -EINVAL;
268 +
269 + raw_spin_lock_irqsave(&priv->lock, flags);
270 + enabled = !irqd_irq_masked(data);
271 + for (i = 0; i < MAX_PARENT_IRQS; i++) {
272 + struct intc_block *block = &priv->block[i];
273 +
274 + if (!block->parent_irq)
275 + break;
276 +
277 + if (i == cpu) {
278 + block->mask_cache[reg] |= BIT(bit);
279 + __bcm6345_periph_enable(block, reg, bit, enabled);
280 + } else {
281 + block->mask_cache[reg] &= ~BIT(bit);
282 + __bcm6345_periph_enable(block, reg, bit, false);
283 + }
284 + }
285 + raw_spin_unlock_irqrestore(&priv->lock, flags);
286 +
287 + return 0;
288 +}
289 +#endif
290 +
291 +static int bcm6345_periph_map(struct irq_domain *d, unsigned int irq,
292 + irq_hw_number_t hw)
293 +{
294 + struct intc_data *priv = d->host_data;
295 +
296 + irq_set_chip_and_handler(irq, &priv->chip, handle_level_irq);
297 +
298 + return 0;
299 +}
300 +
301 +static const struct irq_domain_ops bcm6345_periph_domain_ops = {
302 + .xlate = irq_domain_xlate_onecell,
303 + .map = bcm6345_periph_map,
304 +};
305 +
306 +static int __init __bcm6345_periph_intc_init(struct device_node *node,
307 + int num_blocks, int *irq,
308 + void __iomem **base, int num_words)
309 +{
310 + struct intc_data *data;
311 + unsigned int i, w, status_offset;
312 +
313 + data = kzalloc(sizeof(*data), GFP_KERNEL);
314 + if (!data)
315 + return -ENOMEM;
316 +
317 + raw_spin_lock_init(&data->lock);
318 +
319 + status_offset = num_words * sizeof(u32);
320 +
321 + for (i = 0; i < num_blocks; i++) {
322 + struct intc_block *block = &data->block[i];
323 +
324 + block->parent_irq = irq[i];
325 + block->base = base[i];
326 +
327 + for (w = 0; w < num_words; w++) {
328 + int word_offset = sizeof(u32) * ((num_words - w) - 1);
329 +
330 + block->en_reg[w] = base[i] + word_offset;
331 + block->status_reg[w] = base[i] + status_offset;
332 + block->status_reg[w] += word_offset;
333 +
334 + /* route all interrups to line 0 by default */
335 + if (i == 0)
336 + block->mask_cache[w] = 0xffffffff;
337 + }
338 +
339 + irq_set_handler_data(block->parent_irq, data);
340 + irq_set_chained_handler(block->parent_irq,
341 + bcm6345_periph_irq_handle);
342 + }
343 +
344 + data->num_words = num_words;
345 +
346 + data->chip.name = "bcm6345-periph-intc";
347 + data->chip.irq_mask = bcm6345_periph_irq_mask;
348 + data->chip.irq_unmask = bcm6345_periph_irq_unmask;
349 +
350 +#ifdef CONFIG_SMP
351 + if (num_blocks > 1)
352 + data->chip.irq_set_affinity = bcm6345_periph_set_affinity;
353 +#endif
354 +
355 + data->domain = irq_domain_add_simple(node, IRQS_PER_WORD * num_words,
356 + VIRQ_BASE,
357 + &bcm6345_periph_domain_ops, data);
358 + if (!data->domain) {
359 + kfree(data);
360 + return -EINVAL;
361 + }
362 +
363 + return 0;
364 +}
365 +
366 +void __init bcm6345_periph_intc_init(int num_blocks, int *irq,
367 + void __iomem **base, int num_words)
368 +{
369 + __bcm6345_periph_intc_init(NULL, num_blocks, irq, base, num_words);
370 +}
371 +
372 +#ifdef CONFIG_OF
373 +static int __init bcm6345_periph_of_init(struct device_node *node,
374 + struct device_node *parent)
375 +{
376 + struct resource res;
377 + int num_irqs, ret = -EINVAL;
378 + int irqs[MAX_PARENT_IRQS] = { 0 };
379 + void __iomem *bases[MAX_PARENT_IRQS] = { NULL };
380 + int words = 0;
381 + int i;
382 +
383 + num_irqs = of_irq_count(node);
384 +
385 + if (num_irqs < 1 || num_irqs > MAX_PARENT_IRQS)
386 + return -EINVAL;
387 +
388 + for (i = 0; i < num_irqs; i++) {
389 + resource_size_t size;
390 +
391 + irqs[i] = irq_of_parse_and_map(node, i);
392 + if (!irqs[i])
393 + goto out_unmap;
394 +
395 + if (of_address_to_resource(node, i, &res)) {
396 + goto out_unmap;
397 + }
398 +
399 + size = resource_size(&res);
400 + switch (size) {
401 + case 8:
402 + case 16:
403 + case 32:
404 + size = size / 8;
405 + break;
406 + default:
407 + goto out_unmap;
408 + }
409 +
410 + if (words && words != size) {
411 + ret = -EINVAL;
412 + goto out_unmap;
413 + }
414 + words = size;
415 +
416 + bases[i] = of_iomap(node, i);
417 + if (!bases[i]) {
418 + ret = -ENOMEM;
419 + goto out_unmap;
420 + }
421 + }
422 +
423 + ret = __bcm6345_periph_intc_init(node, num_irqs, irqs, bases, words);
424 + if (!ret)
425 + return 0;
426 +
427 +out_unmap:
428 + for (i = 0; i < num_irqs; i++) {
429 + iounmap(bases[i]);
430 + irq_dispose_mapping(irqs[i]);
431 + }
432 +
433 + return ret;
434 +}
435 +
436 +IRQCHIP_DECLARE(bcm6345_periph_intc, "brcm,bcm6345-periph-intc",
437 + bcm6345_periph_of_init);
438 +#endif
439 --- /dev/null
440 +++ b/include/linux/irqchip/irq-bcm6345-periph.h
441 @@ -0,0 +1,16 @@
442 +/*
443 + * This file is subject to the terms and conditions of the GNU General Public
444 + * License. See the file "COPYING" in the main directory of this archive
445 + * for more details.
446 + *
447 + * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
448 + * Copyright (C) 2008 Nicolas Schichan <nschichan@freebox.fr>
449 + */
450 +
451 +#ifndef __INCLUDE_LINUX_IRQCHIP_IRQ_BCM6345_PERIPH_H
452 +#define __INCLUDE_LINUX_IRQCHIP_IRQ_BCM6345_PERIPH_H
453 +
454 +void bcm6345_periph_intc_init(int num_blocks, int *irq, void __iomem **base,
455 + int num_words);
456 +
457 +#endif /* __INCLUDE_LINUX_IRQCHIP_IRQ_BCM6345_PERIPH_H */