2 * Generic Broadcom Set Top Box Level 2 Interrupt controller driver
4 * Copyright (C) 2014-2017 Broadcom
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.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/spinlock.h>
24 #include <linux/of_irq.h>
25 #include <linux/of_address.h>
26 #include <linux/of_platform.h>
27 #include <linux/interrupt.h>
28 #include <linux/irq.h>
30 #include <linux/irqdomain.h>
31 #include <linux/irqchip.h>
32 #include <linux/irqchip/chained_irq.h>
34 struct brcmstb_intc_init_params
{
35 irq_flow_handler_t handler
;
43 /* Register offsets in the L2 latched interrupt controller */
44 static const struct brcmstb_intc_init_params l2_edge_intc_init
= {
45 .handler
= handle_edge_irq
,
48 .cpu_mask_status
= 0x0c,
50 .cpu_mask_clear
= 0x14
53 /* Register offsets in the L2 level interrupt controller */
54 static const struct brcmstb_intc_init_params l2_lvl_intc_init
= {
55 .handler
= handle_level_irq
,
57 .cpu_clear
= -1, /* Register not present */
58 .cpu_mask_status
= 0x04,
60 .cpu_mask_clear
= 0x0C
63 /* L2 intc private data structure */
64 struct brcmstb_l2_intc_data
{
65 struct irq_domain
*domain
;
66 struct irq_chip_generic
*gc
;
70 u32 saved_mask
; /* for suspend/resume */
74 * brcmstb_l2_mask_and_ack - Mask and ack pending interrupt
77 * Chip has separate enable/disable registers instead of a single mask
78 * register and pending interrupt is acknowledged by setting a bit.
80 * Note: This function is generic and could easily be added to the
81 * generic irqchip implementation if there ever becomes a will to do so.
82 * Perhaps with a name like irq_gc_mask_disable_and_ack_set().
84 * e.g.: https://patchwork.kernel.org/patch/9831047/
86 static void brcmstb_l2_mask_and_ack(struct irq_data
*d
)
88 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
89 struct irq_chip_type
*ct
= irq_data_get_chip_type(d
);
93 irq_reg_writel(gc
, mask
, ct
->regs
.disable
);
94 *ct
->mask_cache
&= ~mask
;
95 irq_reg_writel(gc
, mask
, ct
->regs
.ack
);
99 static void brcmstb_l2_intc_irq_handle(struct irq_desc
*desc
)
101 struct brcmstb_l2_intc_data
*b
= irq_desc_get_handler_data(desc
);
102 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
106 chained_irq_enter(chip
, desc
);
108 status
= irq_reg_readl(b
->gc
, b
->status_offset
) &
109 ~(irq_reg_readl(b
->gc
, b
->mask_offset
));
112 raw_spin_lock(&desc
->lock
);
113 handle_bad_irq(desc
);
114 raw_spin_unlock(&desc
->lock
);
119 irq
= ffs(status
) - 1;
120 status
&= ~(1 << irq
);
121 generic_handle_irq(irq_linear_revmap(b
->domain
, irq
));
124 chained_irq_exit(chip
, desc
);
127 static void brcmstb_l2_intc_suspend(struct irq_data
*d
)
129 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
130 struct irq_chip_type
*ct
= irq_data_get_chip_type(d
);
131 struct brcmstb_l2_intc_data
*b
= gc
->private;
134 /* Save the current mask */
135 b
->saved_mask
= irq_reg_readl(gc
, ct
->regs
.mask
);
138 /* Program the wakeup mask */
139 irq_reg_writel(gc
, ~gc
->wake_active
, ct
->regs
.disable
);
140 irq_reg_writel(gc
, gc
->wake_active
, ct
->regs
.enable
);
145 static void brcmstb_l2_intc_resume(struct irq_data
*d
)
147 struct irq_chip_generic
*gc
= irq_data_get_irq_chip_data(d
);
148 struct irq_chip_type
*ct
= irq_data_get_chip_type(d
);
149 struct brcmstb_l2_intc_data
*b
= gc
->private;
152 if (ct
->chip
.irq_ack
) {
153 /* Clear unmasked non-wakeup interrupts */
154 irq_reg_writel(gc
, ~b
->saved_mask
& ~gc
->wake_active
,
158 /* Restore the saved mask */
159 irq_reg_writel(gc
, b
->saved_mask
, ct
->regs
.disable
);
160 irq_reg_writel(gc
, ~b
->saved_mask
, ct
->regs
.enable
);
164 static int __init
brcmstb_l2_intc_of_init(struct device_node
*np
,
165 struct device_node
*parent
,
166 const struct brcmstb_intc_init_params
169 unsigned int clr
= IRQ_NOREQUEST
| IRQ_NOPROBE
| IRQ_NOAUTOEN
;
170 struct brcmstb_l2_intc_data
*data
;
171 struct irq_chip_type
*ct
;
177 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
181 base
= of_iomap(np
, 0);
183 pr_err("failed to remap intc L2 registers\n");
188 /* Disable all interrupts by default */
189 writel(0xffffffff, base
+ init_params
->cpu_mask_set
);
191 /* Wakeup interrupts may be retained from S5 (cold boot) */
192 data
->can_wake
= of_property_read_bool(np
, "brcm,irq-can-wake");
193 if (!data
->can_wake
&& (init_params
->cpu_clear
>= 0))
194 writel(0xffffffff, base
+ init_params
->cpu_clear
);
196 parent_irq
= irq_of_parse_and_map(np
, 0);
198 pr_err("failed to find parent interrupt\n");
203 data
->domain
= irq_domain_add_linear(np
, 32,
204 &irq_generic_chip_ops
, NULL
);
210 /* MIPS chips strapped for BE will automagically configure the
211 * peripheral registers for CPU-native byte order.
214 if (IS_ENABLED(CONFIG_MIPS
) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
))
215 flags
|= IRQ_GC_BE_IO
;
217 /* Allocate a single Generic IRQ chip for this node */
218 ret
= irq_alloc_domain_generic_chips(data
->domain
, 32, 1,
219 np
->full_name
, init_params
->handler
, clr
, 0, flags
);
221 pr_err("failed to allocate generic irq chip\n");
222 goto out_free_domain
;
225 /* Set the IRQ chaining logic */
226 irq_set_chained_handler_and_data(parent_irq
,
227 brcmstb_l2_intc_irq_handle
, data
);
229 data
->gc
= irq_get_domain_generic_chip(data
->domain
, 0);
230 data
->gc
->reg_base
= base
;
231 data
->gc
->private = data
;
232 data
->status_offset
= init_params
->cpu_status
;
233 data
->mask_offset
= init_params
->cpu_mask_status
;
235 ct
= data
->gc
->chip_types
;
237 if (init_params
->cpu_clear
>= 0) {
238 ct
->regs
.ack
= init_params
->cpu_clear
;
239 ct
->chip
.irq_ack
= irq_gc_ack_set_bit
;
240 ct
->chip
.irq_mask_ack
= brcmstb_l2_mask_and_ack
;
242 /* No Ack - but still slightly more efficient to define this */
243 ct
->chip
.irq_mask_ack
= irq_gc_mask_disable_reg
;
246 ct
->chip
.irq_mask
= irq_gc_mask_disable_reg
;
247 ct
->regs
.disable
= init_params
->cpu_mask_set
;
248 ct
->regs
.mask
= init_params
->cpu_mask_status
;
250 ct
->chip
.irq_unmask
= irq_gc_unmask_enable_reg
;
251 ct
->regs
.enable
= init_params
->cpu_mask_clear
;
253 ct
->chip
.irq_suspend
= brcmstb_l2_intc_suspend
;
254 ct
->chip
.irq_resume
= brcmstb_l2_intc_resume
;
255 ct
->chip
.irq_pm_shutdown
= brcmstb_l2_intc_suspend
;
257 if (data
->can_wake
) {
258 /* This IRQ chip can wake the system, set all child interrupts
259 * in wake_enabled mask
261 data
->gc
->wake_enabled
= 0xffffffff;
262 ct
->chip
.irq_set_wake
= irq_gc_set_wake
;
268 irq_domain_remove(data
->domain
);
276 int __init
brcmstb_l2_edge_intc_of_init(struct device_node
*np
,
277 struct device_node
*parent
)
279 return brcmstb_l2_intc_of_init(np
, parent
, &l2_edge_intc_init
);
281 IRQCHIP_DECLARE(brcmstb_l2_intc
, "brcm,l2-intc", brcmstb_l2_edge_intc_of_init
);
283 int __init
brcmstb_l2_lvl_intc_of_init(struct device_node
*np
,
284 struct device_node
*parent
)
286 return brcmstb_l2_intc_of_init(np
, parent
, &l2_lvl_intc_init
);
288 IRQCHIP_DECLARE(bcm7271_l2_intc
, "brcm,bcm7271-l2-intc",
289 brcmstb_l2_lvl_intc_of_init
);