blob: e8441ee7454ce81f810373ca2558909e1b472cda [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
Kevin Cernekee5b5468c2014-12-25 09:49:03 -080037#define MAX_MAPPINGS MAX_WORDS
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];
Florian Fainellia5042de22014-09-09 17:44:21 -070050};
51
52static void bcm7120_l2_intc_irq_handle(unsigned int irq, struct irq_desc *desc)
53{
54 struct bcm7120_l2_intc_data *b = irq_desc_get_handler_data(desc);
55 struct irq_chip *chip = irq_desc_get_chip(desc);
Kevin Cernekeec76acf42014-11-06 22:44:26 -080056 unsigned int idx;
Florian Fainellia5042de22014-09-09 17:44:21 -070057
58 chained_irq_enter(chip, desc);
59
Kevin Cernekeec76acf42014-11-06 22:44:26 -080060 for (idx = 0; idx < b->n_words; idx++) {
61 int base = idx * IRQS_PER_WORD;
62 struct irq_chip_generic *gc =
63 irq_get_domain_generic_chip(b->domain, base);
64 unsigned long pending;
65 int hwirq;
Florian Fainellia5042de22014-09-09 17:44:21 -070066
Kevin Cernekeec76acf42014-11-06 22:44:26 -080067 irq_gc_lock(gc);
Kevin Cernekee5b5468c2014-12-25 09:49:03 -080068 pending = irq_reg_readl(gc, b->stat_offset[idx]) &
69 gc->mask_cache;
Kevin Cernekeec76acf42014-11-06 22:44:26 -080070 irq_gc_unlock(gc);
71
72 for_each_set_bit(hwirq, &pending, IRQS_PER_WORD) {
73 generic_handle_irq(irq_find_mapping(b->domain,
74 base + hwirq));
75 }
Florian Fainellia5042de22014-09-09 17:44:21 -070076 }
77
Florian Fainellia5042de22014-09-09 17:44:21 -070078 chained_irq_exit(chip, desc);
79}
80
81static void bcm7120_l2_intc_suspend(struct irq_data *d)
82{
83 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Kevin Cernekee5b5468c2014-12-25 09:49:03 -080084 struct irq_chip_type *ct = irq_data_get_chip_type(d);
Florian Fainellia5042de22014-09-09 17:44:21 -070085 struct bcm7120_l2_intc_data *b = gc->private;
Florian Fainellia5042de22014-09-09 17:44:21 -070086
87 irq_gc_lock(gc);
Kevin Cernekeec17261f2014-11-06 22:44:28 -080088 if (b->can_wake)
Kevin Cernekee5b5468c2014-12-25 09:49:03 -080089 irq_reg_writel(gc, gc->mask_cache | gc->wake_active,
90 ct->regs.mask);
Florian Fainellia5042de22014-09-09 17:44:21 -070091 irq_gc_unlock(gc);
92}
93
94static void bcm7120_l2_intc_resume(struct irq_data *d)
95{
96 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Kevin Cernekee5b5468c2014-12-25 09:49:03 -080097 struct irq_chip_type *ct = irq_data_get_chip_type(d);
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,
107 int irq, const __be32 *map_mask)
108{
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 */
121 for (idx = 0; idx < data->n_words; idx++)
122 data->irq_map_mask[idx] |=
123 be32_to_cpup(map_mask + irq * data->n_words + idx);
Florian Fainellia5042de22014-09-09 17:44:21 -0700124
125 irq_set_handler_data(parent_irq, data);
126 irq_set_chained_handler(parent_irq, bcm7120_l2_intc_irq_handle);
127
128 return 0;
129}
130
131int __init bcm7120_l2_intc_of_init(struct device_node *dn,
132 struct device_node *parent)
133{
134 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
135 struct bcm7120_l2_intc_data *data;
136 struct irq_chip_generic *gc;
137 struct irq_chip_type *ct;
138 const __be32 *map_mask;
139 int num_parent_irqs;
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800140 int ret = 0, len;
Kevin Cernekeec17261f2014-11-06 22:44:28 -0800141 unsigned int idx, irq, flags;
Florian Fainellia5042de22014-09-09 17:44:21 -0700142
143 data = kzalloc(sizeof(*data), GFP_KERNEL);
144 if (!data)
145 return -ENOMEM;
146
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800147 for (idx = 0; idx < MAX_WORDS; idx++) {
Kevin Cernekee5b5468c2014-12-25 09:49:03 -0800148 data->map_base[idx] = of_iomap(dn, idx);
149 if (!data->map_base[idx])
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800150 break;
Kevin Cernekee5b5468c2014-12-25 09:49:03 -0800151
152 data->pair_base[idx] = data->map_base[idx];
153 data->en_offset[idx] = IRQEN;
154 data->stat_offset[idx] = IRQSTAT;
155
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800156 data->n_words = idx + 1;
157 }
158 if (!data->n_words) {
Florian Fainellia5042de22014-09-09 17:44:21 -0700159 pr_err("failed to remap intc L2 registers\n");
160 ret = -ENOMEM;
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800161 goto out_unmap;
Florian Fainellia5042de22014-09-09 17:44:21 -0700162 }
163
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800164 /* Enable all interrupts specified in the interrupt forward mask;
165 * disable all others. If the property doesn't exist (-EINVAL),
166 * assume all zeroes.
Florian Fainellia5042de22014-09-09 17:44:21 -0700167 */
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800168 ret = of_property_read_u32_array(dn, "brcm,int-fwd-mask",
169 data->irq_fwd_mask, data->n_words);
170 if (ret == 0 || ret == -EINVAL) {
171 for (idx = 0; idx < data->n_words; idx++)
172 __raw_writel(data->irq_fwd_mask[idx],
Kevin Cernekee5b5468c2014-12-25 09:49:03 -0800173 data->pair_base[idx] +
174 data->en_offset[idx]);
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800175 } else {
176 /* property exists but has the wrong number of words */
177 pr_err("invalid int-fwd-mask property\n");
178 ret = -EINVAL;
179 goto out_unmap;
180 }
Florian Fainellia5042de22014-09-09 17:44:21 -0700181
182 num_parent_irqs = of_irq_count(dn);
183 if (num_parent_irqs <= 0) {
184 pr_err("invalid number of parent interrupts\n");
185 ret = -ENOMEM;
186 goto out_unmap;
187 }
188
189 map_mask = of_get_property(dn, "brcm,int-map-mask", &len);
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800190 if (!map_mask ||
191 (len != (sizeof(*map_mask) * num_parent_irqs * data->n_words))) {
Florian Fainellia5042de22014-09-09 17:44:21 -0700192 pr_err("invalid brcm,int-map-mask property\n");
193 ret = -EINVAL;
194 goto out_unmap;
195 }
196
197 for (irq = 0; irq < num_parent_irqs; irq++) {
198 ret = bcm7120_l2_intc_init_one(dn, data, irq, map_mask);
199 if (ret)
200 goto out_unmap;
201 }
202
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800203 data->domain = irq_domain_add_linear(dn, IRQS_PER_WORD * data->n_words,
204 &irq_generic_chip_ops, NULL);
Florian Fainellia5042de22014-09-09 17:44:21 -0700205 if (!data->domain) {
206 ret = -ENOMEM;
207 goto out_unmap;
208 }
209
Kevin Cernekeec17261f2014-11-06 22:44:28 -0800210 /* MIPS chips strapped for BE will automagically configure the
211 * peripheral registers for CPU-native byte order.
212 */
213 flags = IRQ_GC_INIT_MASK_CACHE;
214 if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
215 flags |= IRQ_GC_BE_IO;
216
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800217 ret = irq_alloc_domain_generic_chips(data->domain, IRQS_PER_WORD, 1,
Kevin Cernekeec17261f2014-11-06 22:44:28 -0800218 dn->full_name, handle_level_irq, clr, 0, flags);
Florian Fainellia5042de22014-09-09 17:44:21 -0700219 if (ret) {
220 pr_err("failed to allocate generic irq chip\n");
221 goto out_free_domain;
222 }
223
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800224 if (of_property_read_bool(dn, "brcm,irq-can-wake"))
Florian Fainellia5042de22014-09-09 17:44:21 -0700225 data->can_wake = true;
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800226
227 for (idx = 0; idx < data->n_words; idx++) {
228 irq = idx * IRQS_PER_WORD;
229 gc = irq_get_domain_generic_chip(data->domain, irq);
230
231 gc->unused = 0xffffffff & ~data->irq_map_mask[idx];
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800232 gc->private = data;
233 ct = gc->chip_types;
234
Kevin Cernekee5b5468c2014-12-25 09:49:03 -0800235 gc->reg_base = data->pair_base[idx];
236 ct->regs.mask = data->en_offset[idx];
237
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800238 ct->chip.irq_mask = irq_gc_mask_clr_bit;
239 ct->chip.irq_unmask = irq_gc_mask_set_bit;
240 ct->chip.irq_ack = irq_gc_noop;
241 ct->chip.irq_suspend = bcm7120_l2_intc_suspend;
242 ct->chip.irq_resume = bcm7120_l2_intc_resume;
243
244 if (data->can_wake) {
245 /* This IRQ chip can wake the system, set all
246 * relevant child interupts in wake_enabled mask
247 */
248 gc->wake_enabled = 0xffffffff;
249 gc->wake_enabled &= ~gc->unused;
250 ct->chip.irq_set_wake = irq_gc_set_wake;
251 }
Florian Fainellia5042de22014-09-09 17:44:21 -0700252 }
253
254 pr_info("registered BCM7120 L2 intc (mem: 0x%p, parent IRQ(s): %d)\n",
Kevin Cernekee5b5468c2014-12-25 09:49:03 -0800255 data->map_base[0], num_parent_irqs);
Florian Fainellia5042de22014-09-09 17:44:21 -0700256
257 return 0;
258
259out_free_domain:
260 irq_domain_remove(data->domain);
261out_unmap:
Kevin Cernekee5b5468c2014-12-25 09:49:03 -0800262 for (idx = 0; idx < MAX_MAPPINGS; idx++) {
263 if (data->map_base[idx])
264 iounmap(data->map_base[idx]);
Kevin Cernekeec76acf42014-11-06 22:44:26 -0800265 }
Florian Fainellia5042de22014-09-09 17:44:21 -0700266 kfree(data);
267 return ret;
268}
Kevin Cernekeea4fcbb82014-11-06 22:44:27 -0800269IRQCHIP_DECLARE(bcm7120_l2_intc, "brcm,bcm7120-l2-intc",
Florian Fainellia5042de22014-09-09 17:44:21 -0700270 bcm7120_l2_intc_of_init);