blob: 8eec8e1201d9dfd349cc7bcb557a0001cdda13c0 [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>
Florian Fainellia5042de22014-09-09 17:44:21 -070017#include <linux/platform_device.h>
18#include <linux/of.h>
19#include <linux/of_irq.h>
20#include <linux/of_address.h>
21#include <linux/of_platform.h>
22#include <linux/interrupt.h>
23#include <linux/irq.h>
24#include <linux/io.h>
25#include <linux/irqdomain.h>
26#include <linux/reboot.h>
Kevin Cernekeec76acf42014-11-06 22:44:26 -080027#include <linux/bitops.h>
Florian Fainellia5042de22014-09-09 17:44:21 -070028#include <linux/irqchip/chained_irq.h>
29
30#include "irqchip.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
37#define IRQS_PER_WORD 32
38
Florian Fainellia5042de22014-09-09 17:44:21 -070039struct bcm7120_l2_intc_data {
Kevin Cernekeec76acf42014-11-06 22:44:26 -080040 unsigned int n_words;
41 void __iomem *base[MAX_WORDS];
Florian Fainellia5042de22014-09-09 17:44:21 -070042 struct irq_domain *domain;
43 bool can_wake;
Kevin Cernekeec76acf42014-11-06 22:44:26 -080044 u32 irq_fwd_mask[MAX_WORDS];
45 u32 irq_map_mask[MAX_WORDS];
Florian Fainellia5042de22014-09-09 17:44:21 -070046};
47
48static void bcm7120_l2_intc_irq_handle(unsigned int irq, struct irq_desc *desc)
49{
50 struct bcm7120_l2_intc_data *b = irq_desc_get_handler_data(desc);
51 struct irq_chip *chip = irq_desc_get_chip(desc);
Kevin Cernekeec76acf42014-11-06 22:44:26 -080052 unsigned int idx;
Florian Fainellia5042de22014-09-09 17:44:21 -070053
54 chained_irq_enter(chip, desc);
55
Kevin Cernekeec76acf42014-11-06 22:44:26 -080056 for (idx = 0; idx < b->n_words; idx++) {
57 int base = idx * IRQS_PER_WORD;
58 struct irq_chip_generic *gc =
59 irq_get_domain_generic_chip(b->domain, base);
60 unsigned long pending;
61 int hwirq;
Florian Fainellia5042de22014-09-09 17:44:21 -070062
Kevin Cernekeec76acf42014-11-06 22:44:26 -080063 irq_gc_lock(gc);
Kevin Cernekeec17261f2014-11-06 22:44:28 -080064 pending = irq_reg_readl(gc, IRQSTAT) & gc->mask_cache;
Kevin Cernekeec76acf42014-11-06 22:44:26 -080065 irq_gc_unlock(gc);
66
67 for_each_set_bit(hwirq, &pending, IRQS_PER_WORD) {
68 generic_handle_irq(irq_find_mapping(b->domain,
69 base + hwirq));
70 }
Florian Fainellia5042de22014-09-09 17:44:21 -070071 }
72
Florian Fainellia5042de22014-09-09 17:44:21 -070073 chained_irq_exit(chip, desc);
74}
75
76static void bcm7120_l2_intc_suspend(struct irq_data *d)
77{
78 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
79 struct bcm7120_l2_intc_data *b = gc->private;
Florian Fainellia5042de22014-09-09 17:44:21 -070080
81 irq_gc_lock(gc);
Kevin Cernekeec17261f2014-11-06 22:44:28 -080082 if (b->can_wake)
83 irq_reg_writel(gc, gc->mask_cache | gc->wake_active, IRQEN);
Florian Fainellia5042de22014-09-09 17:44:21 -070084 irq_gc_unlock(gc);
85}
86
87static void bcm7120_l2_intc_resume(struct irq_data *d)
88{
89 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Florian Fainellia5042de22014-09-09 17:44:21 -070090
91 /* Restore the saved mask */
92 irq_gc_lock(gc);
Kevin Cernekeec17261f2014-11-06 22:44:28 -080093 irq_reg_writel(gc, gc->mask_cache, IRQEN);
Florian Fainellia5042de22014-09-09 17:44:21 -070094 irq_gc_unlock(gc);
95}
96
97static int bcm7120_l2_intc_init_one(struct device_node *dn,
98 struct bcm7120_l2_intc_data *data,
99 int irq, const __be32 *map_mask)
100{
101 int parent_irq;
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800102 unsigned int idx;
Florian Fainellia5042de22014-09-09 17:44:21 -0700103
104 parent_irq = irq_of_parse_and_map(dn, irq);
Dmitry Torokhov714710e2014-11-14 14:16:14 -0800105 if (!parent_irq) {
Florian Fainellia5042de22014-09-09 17:44:21 -0700106 pr_err("failed to map interrupt %d\n", irq);
Dmitry Torokhov714710e2014-11-14 14:16:14 -0800107 return -EINVAL;
Florian Fainellia5042de22014-09-09 17:44:21 -0700108 }
109
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800110 /* For multiple parent IRQs with multiple words, this looks like:
111 * <irq0_w0 irq0_w1 irq1_w0 irq1_w1 ...>
112 */
113 for (idx = 0; idx < data->n_words; idx++)
114 data->irq_map_mask[idx] |=
115 be32_to_cpup(map_mask + irq * data->n_words + idx);
Florian Fainellia5042de22014-09-09 17:44:21 -0700116
117 irq_set_handler_data(parent_irq, data);
118 irq_set_chained_handler(parent_irq, bcm7120_l2_intc_irq_handle);
119
120 return 0;
121}
122
123int __init bcm7120_l2_intc_of_init(struct device_node *dn,
124 struct device_node *parent)
125{
126 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
127 struct bcm7120_l2_intc_data *data;
128 struct irq_chip_generic *gc;
129 struct irq_chip_type *ct;
130 const __be32 *map_mask;
131 int num_parent_irqs;
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800132 int ret = 0, len;
Kevin Cernekeec17261f2014-11-06 22:44:28 -0800133 unsigned int idx, irq, flags;
Florian Fainellia5042de22014-09-09 17:44:21 -0700134
135 data = kzalloc(sizeof(*data), GFP_KERNEL);
136 if (!data)
137 return -ENOMEM;
138
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800139 for (idx = 0; idx < MAX_WORDS; idx++) {
140 data->base[idx] = of_iomap(dn, idx);
141 if (!data->base[idx])
142 break;
143 data->n_words = idx + 1;
144 }
145 if (!data->n_words) {
Florian Fainellia5042de22014-09-09 17:44:21 -0700146 pr_err("failed to remap intc L2 registers\n");
147 ret = -ENOMEM;
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800148 goto out_unmap;
Florian Fainellia5042de22014-09-09 17:44:21 -0700149 }
150
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800151 /* Enable all interrupts specified in the interrupt forward mask;
152 * disable all others. If the property doesn't exist (-EINVAL),
153 * assume all zeroes.
Florian Fainellia5042de22014-09-09 17:44:21 -0700154 */
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800155 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 for (idx = 0; idx < data->n_words; idx++)
159 __raw_writel(data->irq_fwd_mask[idx],
160 data->base[idx] + IRQEN);
161 } else {
162 /* property exists but has the wrong number of words */
163 pr_err("invalid int-fwd-mask property\n");
164 ret = -EINVAL;
165 goto out_unmap;
166 }
Florian Fainellia5042de22014-09-09 17:44:21 -0700167
168 num_parent_irqs = of_irq_count(dn);
169 if (num_parent_irqs <= 0) {
170 pr_err("invalid number of parent interrupts\n");
171 ret = -ENOMEM;
172 goto out_unmap;
173 }
174
175 map_mask = of_get_property(dn, "brcm,int-map-mask", &len);
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800176 if (!map_mask ||
177 (len != (sizeof(*map_mask) * num_parent_irqs * data->n_words))) {
Florian Fainellia5042de22014-09-09 17:44:21 -0700178 pr_err("invalid brcm,int-map-mask property\n");
179 ret = -EINVAL;
180 goto out_unmap;
181 }
182
183 for (irq = 0; irq < num_parent_irqs; irq++) {
184 ret = bcm7120_l2_intc_init_one(dn, data, irq, map_mask);
185 if (ret)
186 goto out_unmap;
187 }
188
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800189 data->domain = irq_domain_add_linear(dn, IRQS_PER_WORD * data->n_words,
190 &irq_generic_chip_ops, NULL);
Florian Fainellia5042de22014-09-09 17:44:21 -0700191 if (!data->domain) {
192 ret = -ENOMEM;
193 goto out_unmap;
194 }
195
Kevin Cernekeec17261f2014-11-06 22:44:28 -0800196 /* MIPS chips strapped for BE will automagically configure the
197 * peripheral registers for CPU-native byte order.
198 */
199 flags = IRQ_GC_INIT_MASK_CACHE;
200 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
201 flags |= IRQ_GC_BE_IO;
202
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800203 ret = irq_alloc_domain_generic_chips(data->domain, IRQS_PER_WORD, 1,
Kevin Cernekeec17261f2014-11-06 22:44:28 -0800204 dn->full_name, handle_level_irq, clr, 0, flags);
Florian Fainellia5042de22014-09-09 17:44:21 -0700205 if (ret) {
206 pr_err("failed to allocate generic irq chip\n");
207 goto out_free_domain;
208 }
209
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800210 if (of_property_read_bool(dn, "brcm,irq-can-wake"))
Florian Fainellia5042de22014-09-09 17:44:21 -0700211 data->can_wake = true;
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800212
213 for (idx = 0; idx < data->n_words; idx++) {
214 irq = idx * IRQS_PER_WORD;
215 gc = irq_get_domain_generic_chip(data->domain, irq);
216
217 gc->unused = 0xffffffff & ~data->irq_map_mask[idx];
218 gc->reg_base = data->base[idx];
219 gc->private = data;
220 ct = gc->chip_types;
221
222 ct->regs.mask = IRQEN;
223 ct->chip.irq_mask = irq_gc_mask_clr_bit;
224 ct->chip.irq_unmask = irq_gc_mask_set_bit;
225 ct->chip.irq_ack = irq_gc_noop;
226 ct->chip.irq_suspend = bcm7120_l2_intc_suspend;
227 ct->chip.irq_resume = bcm7120_l2_intc_resume;
228
229 if (data->can_wake) {
230 /* This IRQ chip can wake the system, set all
231 * relevant child interupts in wake_enabled mask
232 */
233 gc->wake_enabled = 0xffffffff;
234 gc->wake_enabled &= ~gc->unused;
235 ct->chip.irq_set_wake = irq_gc_set_wake;
236 }
Florian Fainellia5042de22014-09-09 17:44:21 -0700237 }
238
239 pr_info("registered BCM7120 L2 intc (mem: 0x%p, parent IRQ(s): %d)\n",
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800240 data->base[0], num_parent_irqs);
Florian Fainellia5042de22014-09-09 17:44:21 -0700241
242 return 0;
243
244out_free_domain:
245 irq_domain_remove(data->domain);
246out_unmap:
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800247 for (idx = 0; idx < MAX_WORDS; idx++) {
248 if (data->base[idx])
249 iounmap(data->base[idx]);
250 }
Florian Fainellia5042de22014-09-09 17:44:21 -0700251 kfree(data);
252 return ret;
253}
Kevin Cernekeea4fcbb82014-11-06 22:44:27 -0800254IRQCHIP_DECLARE(bcm7120_l2_intc, "brcm,bcm7120-l2-intc",
Florian Fainellia5042de22014-09-09 17:44:21 -0700255 bcm7120_l2_intc_of_init);