17728819cSStefan Wahren // SPDX-License-Identifier: GPL-2.0+
289214f00SSimon Arlott /*
389214f00SSimon Arlott * Copyright 2010 Broadcom
489214f00SSimon Arlott * Copyright 2012 Simon Arlott, Chris Boot, Stephen Warren
589214f00SSimon Arlott *
689214f00SSimon Arlott * Quirk 1: Shortcut interrupts don't set the bank 1/2 register pending bits
789214f00SSimon Arlott *
889214f00SSimon Arlott * If an interrupt fires on bank 1 that isn't in the shortcuts list, bit 8
989214f00SSimon Arlott * on bank 0 is set to signify that an interrupt in bank 1 has fired, and
1089214f00SSimon Arlott * to look in the bank 1 status register for more information.
1189214f00SSimon Arlott *
1289214f00SSimon Arlott * If an interrupt fires on bank 1 that _is_ in the shortcuts list, its
1389214f00SSimon Arlott * shortcut bit in bank 0 is set as well as its interrupt bit in the bank 1
1489214f00SSimon Arlott * status register, but bank 0 bit 8 is _not_ set.
1589214f00SSimon Arlott *
1689214f00SSimon Arlott * Quirk 2: You can't mask the register 1/2 pending interrupts
1789214f00SSimon Arlott *
1889214f00SSimon Arlott * In a proper cascaded interrupt controller, the interrupt lines with
1989214f00SSimon Arlott * cascaded interrupt controllers on them are just normal interrupt lines.
2089214f00SSimon Arlott * You can mask the interrupts and get on with things. With this controller
2189214f00SSimon Arlott * you can't do that.
2289214f00SSimon Arlott *
2389214f00SSimon Arlott * Quirk 3: The shortcut interrupts can't be (un)masked in bank 0
2489214f00SSimon Arlott *
2589214f00SSimon Arlott * Those interrupts that have shortcuts can only be masked/unmasked in
2689214f00SSimon Arlott * their respective banks' enable/disable registers. Doing so in the bank 0
2789214f00SSimon Arlott * enable/disable registers has no effect.
2889214f00SSimon Arlott *
2989214f00SSimon Arlott * The FIQ control register:
3089214f00SSimon Arlott * Bits 0-6: IRQ (index in order of interrupts from banks 1, 2, then 0)
3189214f00SSimon Arlott * Bit 7: Enable FIQ generation
3289214f00SSimon Arlott * Bits 8+: Unused
3389214f00SSimon Arlott *
3489214f00SSimon Arlott * An interrupt must be disabled before configuring it for FIQ generation
3589214f00SSimon Arlott * otherwise both handlers will fire at the same time!
3689214f00SSimon Arlott */
3789214f00SSimon Arlott
3889214f00SSimon Arlott #include <linux/io.h>
3989214f00SSimon Arlott #include <linux/slab.h>
4089214f00SSimon Arlott #include <linux/of_address.h>
4189214f00SSimon Arlott #include <linux/of_irq.h>
4241a83e06SJoel Porquet #include <linux/irqchip.h>
4389214f00SSimon Arlott #include <linux/irqdomain.h>
4489214f00SSimon Arlott
4589214f00SSimon Arlott #include <asm/exception.h>
465702941eSAxel Lin
4789214f00SSimon Arlott /* Put the bank and irq (32 bits) into the hwirq */
4889214f00SSimon Arlott #define MAKE_HWIRQ(b, n) ((b << 5) | (n))
4989214f00SSimon Arlott #define HWIRQ_BANK(i) (i >> 5)
5089214f00SSimon Arlott #define HWIRQ_BIT(i) BIT(i & 0x1f)
5189214f00SSimon Arlott
5289214f00SSimon Arlott #define NR_IRQS_BANK0 8
5389214f00SSimon Arlott #define BANK0_HWIRQ_MASK 0xff
5489214f00SSimon Arlott /* Shortcuts can't be disabled so any unknown new ones need to be masked */
5589214f00SSimon Arlott #define SHORTCUT1_MASK 0x00007c00
5689214f00SSimon Arlott #define SHORTCUT2_MASK 0x001f8000
5789214f00SSimon Arlott #define SHORTCUT_SHIFT 10
5889214f00SSimon Arlott #define BANK1_HWIRQ BIT(8)
5989214f00SSimon Arlott #define BANK2_HWIRQ BIT(9)
6089214f00SSimon Arlott #define BANK0_VALID_MASK (BANK0_HWIRQ_MASK | BANK1_HWIRQ | BANK2_HWIRQ \
6189214f00SSimon Arlott | SHORTCUT1_MASK | SHORTCUT2_MASK)
6289214f00SSimon Arlott
6389214f00SSimon Arlott #define REG_FIQ_CONTROL 0x0c
64bd59b343SLukas Wunner #define FIQ_CONTROL_ENABLE BIT(7)
6589214f00SSimon Arlott
6689214f00SSimon Arlott #define NR_BANKS 3
6789214f00SSimon Arlott #define IRQS_PER_BANK 32
6889214f00SSimon Arlott
69c376023bSNicolas Pitre static const int reg_pending[] __initconst = { 0x00, 0x04, 0x08 };
70c376023bSNicolas Pitre static const int reg_enable[] __initconst = { 0x18, 0x10, 0x14 };
71c376023bSNicolas Pitre static const int reg_disable[] __initconst = { 0x24, 0x1c, 0x20 };
72c376023bSNicolas Pitre static const int bank_irqs[] __initconst = { 8, 32, 32 };
7389214f00SSimon Arlott
7489214f00SSimon Arlott static const int shortcuts[] = {
7589214f00SSimon Arlott 7, 9, 10, 18, 19, /* Bank 1 */
7689214f00SSimon Arlott 21, 22, 23, 24, 25, 30 /* Bank 2 */
7789214f00SSimon Arlott };
7889214f00SSimon Arlott
7989214f00SSimon Arlott struct armctrl_ic {
8089214f00SSimon Arlott void __iomem *base;
8189214f00SSimon Arlott void __iomem *pending[NR_BANKS];
8289214f00SSimon Arlott void __iomem *enable[NR_BANKS];
8389214f00SSimon Arlott void __iomem *disable[NR_BANKS];
8489214f00SSimon Arlott struct irq_domain *domain;
8589214f00SSimon Arlott };
8689214f00SSimon Arlott
8789214f00SSimon Arlott static struct armctrl_ic intc __read_mostly;
888783dd3aSStephen Boyd static void __exception_irq_entry bcm2835_handle_irq(
895702941eSAxel Lin struct pt_regs *regs);
90bd0b9ac4SThomas Gleixner static void bcm2836_chained_handle_irq(struct irq_desc *desc);
9189214f00SSimon Arlott
armctrl_mask_irq(struct irq_data * d)9289214f00SSimon Arlott static void armctrl_mask_irq(struct irq_data *d)
9389214f00SSimon Arlott {
9489214f00SSimon Arlott writel_relaxed(HWIRQ_BIT(d->hwirq), intc.disable[HWIRQ_BANK(d->hwirq)]);
9589214f00SSimon Arlott }
9689214f00SSimon Arlott
armctrl_unmask_irq(struct irq_data * d)9789214f00SSimon Arlott static void armctrl_unmask_irq(struct irq_data *d)
9889214f00SSimon Arlott {
9989214f00SSimon Arlott writel_relaxed(HWIRQ_BIT(d->hwirq), intc.enable[HWIRQ_BANK(d->hwirq)]);
10089214f00SSimon Arlott }
10189214f00SSimon Arlott
10289214f00SSimon Arlott static struct irq_chip armctrl_chip = {
10389214f00SSimon Arlott .name = "ARMCTRL-level",
10489214f00SSimon Arlott .irq_mask = armctrl_mask_irq,
10589214f00SSimon Arlott .irq_unmask = armctrl_unmask_irq
10689214f00SSimon Arlott };
10789214f00SSimon Arlott
armctrl_xlate(struct irq_domain * d,struct device_node * ctrlr,const u32 * intspec,unsigned int intsize,unsigned long * out_hwirq,unsigned int * out_type)10889214f00SSimon Arlott static int armctrl_xlate(struct irq_domain *d, struct device_node *ctrlr,
10989214f00SSimon Arlott const u32 *intspec, unsigned int intsize,
11089214f00SSimon Arlott unsigned long *out_hwirq, unsigned int *out_type)
11189214f00SSimon Arlott {
11289214f00SSimon Arlott if (WARN_ON(intsize != 2))
11389214f00SSimon Arlott return -EINVAL;
11489214f00SSimon Arlott
11589214f00SSimon Arlott if (WARN_ON(intspec[0] >= NR_BANKS))
11689214f00SSimon Arlott return -EINVAL;
11789214f00SSimon Arlott
11889214f00SSimon Arlott if (WARN_ON(intspec[1] >= IRQS_PER_BANK))
11989214f00SSimon Arlott return -EINVAL;
12089214f00SSimon Arlott
12189214f00SSimon Arlott if (WARN_ON(intspec[0] == 0 && intspec[1] >= NR_IRQS_BANK0))
12289214f00SSimon Arlott return -EINVAL;
12389214f00SSimon Arlott
12489214f00SSimon Arlott *out_hwirq = MAKE_HWIRQ(intspec[0], intspec[1]);
12589214f00SSimon Arlott *out_type = IRQ_TYPE_NONE;
12689214f00SSimon Arlott return 0;
12789214f00SSimon Arlott }
12889214f00SSimon Arlott
12996009736SKrzysztof Kozlowski static const struct irq_domain_ops armctrl_ops = {
13089214f00SSimon Arlott .xlate = armctrl_xlate
13189214f00SSimon Arlott };
13289214f00SSimon Arlott
armctrl_of_init(struct device_node * node,struct device_node * parent,bool is_2836)13389214f00SSimon Arlott static int __init armctrl_of_init(struct device_node *node,
134a493f339SEric Anholt struct device_node *parent,
135a493f339SEric Anholt bool is_2836)
13689214f00SSimon Arlott {
13789214f00SSimon Arlott void __iomem *base;
13889214f00SSimon Arlott int irq, b, i;
139bd59b343SLukas Wunner u32 reg;
14089214f00SSimon Arlott
14189214f00SSimon Arlott base = of_iomap(node, 0);
14289214f00SSimon Arlott if (!base)
143e81f54c6SRob Herring panic("%pOF: unable to map IC registers\n", node);
14489214f00SSimon Arlott
14589214f00SSimon Arlott intc.domain = irq_domain_add_linear(node, MAKE_HWIRQ(NR_BANKS, 0),
14689214f00SSimon Arlott &armctrl_ops, NULL);
14789214f00SSimon Arlott if (!intc.domain)
148e81f54c6SRob Herring panic("%pOF: unable to create IRQ domain\n", node);
14989214f00SSimon Arlott
15089214f00SSimon Arlott for (b = 0; b < NR_BANKS; b++) {
15189214f00SSimon Arlott intc.pending[b] = base + reg_pending[b];
15289214f00SSimon Arlott intc.enable[b] = base + reg_enable[b];
15389214f00SSimon Arlott intc.disable[b] = base + reg_disable[b];
15489214f00SSimon Arlott
15589214f00SSimon Arlott for (i = 0; i < bank_irqs[b]; i++) {
15689214f00SSimon Arlott irq = irq_create_mapping(intc.domain, MAKE_HWIRQ(b, i));
15789214f00SSimon Arlott BUG_ON(irq <= 0);
15889214f00SSimon Arlott irq_set_chip_and_handler(irq, &armctrl_chip,
15989214f00SSimon Arlott handle_level_irq);
160d17cab44SRob Herring irq_set_probe(irq);
16189214f00SSimon Arlott }
162bd59b343SLukas Wunner
163bd59b343SLukas Wunner reg = readl_relaxed(intc.enable[b]);
164bd59b343SLukas Wunner if (reg) {
165bd59b343SLukas Wunner writel_relaxed(reg, intc.disable[b]);
166bd59b343SLukas Wunner pr_err(FW_BUG "Bootloader left irq enabled: "
167bd59b343SLukas Wunner "bank %d irq %*pbl\n", b, IRQS_PER_BANK, ®);
168bd59b343SLukas Wunner }
169bd59b343SLukas Wunner }
170bd59b343SLukas Wunner
171bd59b343SLukas Wunner reg = readl_relaxed(base + REG_FIQ_CONTROL);
172bd59b343SLukas Wunner if (reg & FIQ_CONTROL_ENABLE) {
173bd59b343SLukas Wunner writel_relaxed(0, base + REG_FIQ_CONTROL);
174bd59b343SLukas Wunner pr_err(FW_BUG "Bootloader left fiq enabled\n");
17589214f00SSimon Arlott }
1765702941eSAxel Lin
177a493f339SEric Anholt if (is_2836) {
178a493f339SEric Anholt int parent_irq = irq_of_parse_and_map(node, 0);
179a493f339SEric Anholt
180a493f339SEric Anholt if (!parent_irq) {
181e81f54c6SRob Herring panic("%pOF: unable to get parent interrupt.\n",
182e81f54c6SRob Herring node);
183a493f339SEric Anholt }
184a493f339SEric Anholt irq_set_chained_handler(parent_irq, bcm2836_chained_handle_irq);
185a493f339SEric Anholt } else {
1865702941eSAxel Lin set_handle_irq(bcm2835_handle_irq);
187a493f339SEric Anholt }
188a493f339SEric Anholt
18989214f00SSimon Arlott return 0;
19089214f00SSimon Arlott }
19189214f00SSimon Arlott
bcm2835_armctrl_of_init(struct device_node * node,struct device_node * parent)192a493f339SEric Anholt static int __init bcm2835_armctrl_of_init(struct device_node *node,
193a493f339SEric Anholt struct device_node *parent)
194a493f339SEric Anholt {
195a493f339SEric Anholt return armctrl_of_init(node, parent, false);
196a493f339SEric Anholt }
197a493f339SEric Anholt
bcm2836_armctrl_of_init(struct device_node * node,struct device_node * parent)198a493f339SEric Anholt static int __init bcm2836_armctrl_of_init(struct device_node *node,
199a493f339SEric Anholt struct device_node *parent)
200a493f339SEric Anholt {
201a493f339SEric Anholt return armctrl_of_init(node, parent, true);
202a493f339SEric Anholt }
203a493f339SEric Anholt
204a493f339SEric Anholt
20589214f00SSimon Arlott /*
20689214f00SSimon Arlott * Handle each interrupt across the entire interrupt controller. This reads the
20789214f00SSimon Arlott * status register before handling each interrupt, which is necessary given that
20889214f00SSimon Arlott * handle_IRQ may briefly re-enable interrupts for soft IRQ handling.
20989214f00SSimon Arlott */
21089214f00SSimon Arlott
armctrl_translate_bank(int bank)211de58e52fSEric Anholt static u32 armctrl_translate_bank(int bank)
21289214f00SSimon Arlott {
213de58e52fSEric Anholt u32 stat = readl_relaxed(intc.pending[bank]);
21489214f00SSimon Arlott
215de58e52fSEric Anholt return MAKE_HWIRQ(bank, ffs(stat) - 1);
21689214f00SSimon Arlott }
21789214f00SSimon Arlott
armctrl_translate_shortcut(int bank,u32 stat)218de58e52fSEric Anholt static u32 armctrl_translate_shortcut(int bank, u32 stat)
21989214f00SSimon Arlott {
220de58e52fSEric Anholt return MAKE_HWIRQ(bank, shortcuts[ffs(stat >> SHORTCUT_SHIFT) - 1]);
221de58e52fSEric Anholt }
222de58e52fSEric Anholt
get_next_armctrl_hwirq(void)223de58e52fSEric Anholt static u32 get_next_armctrl_hwirq(void)
224de58e52fSEric Anholt {
225de58e52fSEric Anholt u32 stat = readl_relaxed(intc.pending[0]) & BANK0_VALID_MASK;
226de58e52fSEric Anholt
227de58e52fSEric Anholt if (stat == 0)
228de58e52fSEric Anholt return ~0;
229de58e52fSEric Anholt else if (stat & BANK0_HWIRQ_MASK)
230de58e52fSEric Anholt return MAKE_HWIRQ(0, ffs(stat & BANK0_HWIRQ_MASK) - 1);
231de58e52fSEric Anholt else if (stat & SHORTCUT1_MASK)
232de58e52fSEric Anholt return armctrl_translate_shortcut(1, stat & SHORTCUT1_MASK);
233de58e52fSEric Anholt else if (stat & SHORTCUT2_MASK)
234de58e52fSEric Anholt return armctrl_translate_shortcut(2, stat & SHORTCUT2_MASK);
235de58e52fSEric Anholt else if (stat & BANK1_HWIRQ)
236de58e52fSEric Anholt return armctrl_translate_bank(1);
237de58e52fSEric Anholt else if (stat & BANK2_HWIRQ)
238de58e52fSEric Anholt return armctrl_translate_bank(2);
239de58e52fSEric Anholt else
240de58e52fSEric Anholt BUG();
24189214f00SSimon Arlott }
24289214f00SSimon Arlott
bcm2835_handle_irq(struct pt_regs * regs)2438783dd3aSStephen Boyd static void __exception_irq_entry bcm2835_handle_irq(
24489214f00SSimon Arlott struct pt_regs *regs)
24589214f00SSimon Arlott {
246de58e52fSEric Anholt u32 hwirq;
24789214f00SSimon Arlott
248de58e52fSEric Anholt while ((hwirq = get_next_armctrl_hwirq()) != ~0)
249*0953fb26SMark Rutland generic_handle_domain_irq(intc.domain, hwirq);
25089214f00SSimon Arlott }
2515702941eSAxel Lin
bcm2836_chained_handle_irq(struct irq_desc * desc)252bd0b9ac4SThomas Gleixner static void bcm2836_chained_handle_irq(struct irq_desc *desc)
253a493f339SEric Anholt {
254a493f339SEric Anholt u32 hwirq;
255a493f339SEric Anholt
256a493f339SEric Anholt while ((hwirq = get_next_armctrl_hwirq()) != ~0)
257046a6ee2SMarc Zyngier generic_handle_domain_irq(intc.domain, hwirq);
258a493f339SEric Anholt }
259a493f339SEric Anholt
260a493f339SEric Anholt IRQCHIP_DECLARE(bcm2835_armctrl_ic, "brcm,bcm2835-armctrl-ic",
261a493f339SEric Anholt bcm2835_armctrl_of_init);
262a493f339SEric Anholt IRQCHIP_DECLARE(bcm2836_armctrl_ic, "brcm,bcm2836-armctrl-ic",
263a493f339SEric Anholt bcm2836_armctrl_of_init);
264