blob: b70679f8bb654ac0e1cc7bdaf2c4922e40283361 [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>
16#include <linux/platform_device.h>
17#include <linux/of.h>
18#include <linux/of_irq.h>
19#include <linux/of_address.h>
20#include <linux/of_platform.h>
21#include <linux/interrupt.h>
22#include <linux/irq.h>
23#include <linux/io.h>
24#include <linux/irqdomain.h>
25#include <linux/reboot.h>
26#include <linux/irqchip/chained_irq.h>
27
28#include "irqchip.h"
29
Florian Fainellia5042de22014-09-09 17:44:21 -070030/* Register offset in the L2 interrupt controller */
31#define IRQEN 0x00
32#define IRQSTAT 0x04
33
34struct bcm7120_l2_intc_data {
35 void __iomem *base;
36 struct irq_domain *domain;
37 bool can_wake;
38 u32 irq_fwd_mask;
39 u32 irq_map_mask;
40 u32 saved_mask;
41};
42
43static void bcm7120_l2_intc_irq_handle(unsigned int irq, struct irq_desc *desc)
44{
45 struct bcm7120_l2_intc_data *b = irq_desc_get_handler_data(desc);
46 struct irq_chip *chip = irq_desc_get_chip(desc);
47 u32 status;
48
49 chained_irq_enter(chip, desc);
50
51 status = __raw_readl(b->base + IRQSTAT);
Kevin Cernekeef668f072014-11-06 22:44:21 -080052 while (status) {
Florian Fainellia5042de22014-09-09 17:44:21 -070053 irq = ffs(status) - 1;
54 status &= ~(1 << irq);
55 generic_handle_irq(irq_find_mapping(b->domain, irq));
Kevin Cernekeef668f072014-11-06 22:44:21 -080056 }
Florian Fainellia5042de22014-09-09 17:44:21 -070057
Florian Fainellia5042de22014-09-09 17:44:21 -070058 chained_irq_exit(chip, desc);
59}
60
61static void bcm7120_l2_intc_suspend(struct irq_data *d)
62{
63 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
64 struct bcm7120_l2_intc_data *b = gc->private;
65 u32 reg;
66
67 irq_gc_lock(gc);
68 /* Save the current mask and the interrupt forward mask */
Kevin Cernekee38e3a6e2014-11-06 22:44:23 -080069 b->saved_mask = __raw_readl(b->base + IRQEN) | b->irq_fwd_mask;
Florian Fainellia5042de22014-09-09 17:44:21 -070070 if (b->can_wake) {
71 reg = b->saved_mask | gc->wake_active;
Kevin Cernekee38e3a6e2014-11-06 22:44:23 -080072 __raw_writel(reg, b->base + IRQEN);
Florian Fainellia5042de22014-09-09 17:44:21 -070073 }
74 irq_gc_unlock(gc);
75}
76
77static void bcm7120_l2_intc_resume(struct irq_data *d)
78{
79 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
80 struct bcm7120_l2_intc_data *b = gc->private;
81
82 /* Restore the saved mask */
83 irq_gc_lock(gc);
Kevin Cernekee38e3a6e2014-11-06 22:44:23 -080084 __raw_writel(b->saved_mask, b->base + IRQEN);
Florian Fainellia5042de22014-09-09 17:44:21 -070085 irq_gc_unlock(gc);
86}
87
88static int bcm7120_l2_intc_init_one(struct device_node *dn,
89 struct bcm7120_l2_intc_data *data,
90 int irq, const __be32 *map_mask)
91{
92 int parent_irq;
93
94 parent_irq = irq_of_parse_and_map(dn, irq);
95 if (parent_irq < 0) {
96 pr_err("failed to map interrupt %d\n", irq);
97 return parent_irq;
98 }
99
100 data->irq_map_mask |= be32_to_cpup(map_mask + irq);
101
102 irq_set_handler_data(parent_irq, data);
103 irq_set_chained_handler(parent_irq, bcm7120_l2_intc_irq_handle);
104
105 return 0;
106}
107
108int __init bcm7120_l2_intc_of_init(struct device_node *dn,
109 struct device_node *parent)
110{
111 unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
112 struct bcm7120_l2_intc_data *data;
113 struct irq_chip_generic *gc;
114 struct irq_chip_type *ct;
115 const __be32 *map_mask;
116 int num_parent_irqs;
117 int ret = 0, len, irq;
118
119 data = kzalloc(sizeof(*data), GFP_KERNEL);
120 if (!data)
121 return -ENOMEM;
122
123 data->base = of_iomap(dn, 0);
124 if (!data->base) {
125 pr_err("failed to remap intc L2 registers\n");
126 ret = -ENOMEM;
127 goto out_free;
128 }
129
130 if (of_property_read_u32(dn, "brcm,int-fwd-mask", &data->irq_fwd_mask))
131 data->irq_fwd_mask = 0;
132
133 /* Enable all interrupt specified in the interrupt forward mask and have
134 * the other disabled
135 */
136 __raw_writel(data->irq_fwd_mask, data->base + IRQEN);
137
138 num_parent_irqs = of_irq_count(dn);
139 if (num_parent_irqs <= 0) {
140 pr_err("invalid number of parent interrupts\n");
141 ret = -ENOMEM;
142 goto out_unmap;
143 }
144
145 map_mask = of_get_property(dn, "brcm,int-map-mask", &len);
146 if (!map_mask || (len != (sizeof(*map_mask) * num_parent_irqs))) {
147 pr_err("invalid brcm,int-map-mask property\n");
148 ret = -EINVAL;
149 goto out_unmap;
150 }
151
152 for (irq = 0; irq < num_parent_irqs; irq++) {
153 ret = bcm7120_l2_intc_init_one(dn, data, irq, map_mask);
154 if (ret)
155 goto out_unmap;
156 }
157
158 data->domain = irq_domain_add_linear(dn, 32,
159 &irq_generic_chip_ops, NULL);
160 if (!data->domain) {
161 ret = -ENOMEM;
162 goto out_unmap;
163 }
164
165 ret = irq_alloc_domain_generic_chips(data->domain, 32, 1,
166 dn->full_name, handle_level_irq, clr, 0,
167 IRQ_GC_INIT_MASK_CACHE);
168 if (ret) {
169 pr_err("failed to allocate generic irq chip\n");
170 goto out_free_domain;
171 }
172
173 gc = irq_get_domain_generic_chip(data->domain, 0);
Kevin Cernekee0b5cb322014-11-06 22:44:24 -0800174 gc->unused = 0xffffffff & ~data->irq_map_mask;
Florian Fainellia5042de22014-09-09 17:44:21 -0700175 gc->reg_base = data->base;
176 gc->private = data;
177 ct = gc->chip_types;
178
179 ct->regs.mask = IRQEN;
180 ct->chip.irq_mask = irq_gc_mask_clr_bit;
181 ct->chip.irq_unmask = irq_gc_mask_set_bit;
182 ct->chip.irq_ack = irq_gc_noop;
183 ct->chip.irq_suspend = bcm7120_l2_intc_suspend;
184 ct->chip.irq_resume = bcm7120_l2_intc_resume;
185
186 if (of_property_read_bool(dn, "brcm,irq-can-wake")) {
187 data->can_wake = true;
188 /* This IRQ chip can wake the system, set all relevant child
189 * interupts in wake_enabled mask
190 */
191 gc->wake_enabled = 0xffffffff;
192 gc->wake_enabled &= ~gc->unused;
193 ct->chip.irq_set_wake = irq_gc_set_wake;
194 }
195
196 pr_info("registered BCM7120 L2 intc (mem: 0x%p, parent IRQ(s): %d)\n",
197 data->base, num_parent_irqs);
198
199 return 0;
200
201out_free_domain:
202 irq_domain_remove(data->domain);
203out_unmap:
204 iounmap(data->base);
205out_free:
206 kfree(data);
207 return ret;
208}
209IRQCHIP_DECLARE(brcmstb_l2_intc, "brcm,bcm7120-l2-intc",
210 bcm7120_l2_intc_of_init);