162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2011-12 Synopsys, Inc. (www.synopsys.com)
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/interrupt.h>
762306a36Sopenharmony_ci#include <linux/module.h>
862306a36Sopenharmony_ci#include <linux/of.h>
962306a36Sopenharmony_ci#include <linux/irqdomain.h>
1062306a36Sopenharmony_ci#include <linux/irqchip.h>
1162306a36Sopenharmony_ci#include <asm/irq.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#define NR_CPU_IRQS	32	/* number of irq lines coming in */
1462306a36Sopenharmony_ci#define TIMER0_IRQ	3	/* Fixed by ISA */
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/*
1762306a36Sopenharmony_ci * Early Hardware specific Interrupt setup
1862306a36Sopenharmony_ci * -Platform independent, needed for each CPU (not foldable into init_IRQ)
1962306a36Sopenharmony_ci * -Called very early (start_kernel -> setup_arch -> setup_processor)
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci * what it does ?
2262306a36Sopenharmony_ci * -Optionally, setup the High priority Interrupts as Level 2 IRQs
2362306a36Sopenharmony_ci */
2462306a36Sopenharmony_civoid arc_init_IRQ(void)
2562306a36Sopenharmony_ci{
2662306a36Sopenharmony_ci	unsigned int level_mask = 0, i;
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci       /* Is timer high priority Interrupt (Level2 in ARCompact jargon) */
2962306a36Sopenharmony_ci	level_mask |= IS_ENABLED(CONFIG_ARC_COMPACT_IRQ_LEVELS) << TIMER0_IRQ;
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci	/*
3262306a36Sopenharmony_ci	 * Write to register, even if no LV2 IRQs configured to reset it
3362306a36Sopenharmony_ci	 * in case bootloader had mucked with it
3462306a36Sopenharmony_ci	 */
3562306a36Sopenharmony_ci	write_aux_reg(AUX_IRQ_LEV, level_mask);
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	if (level_mask)
3862306a36Sopenharmony_ci		pr_info("Level-2 interrupts bitset %x\n", level_mask);
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci	/*
4162306a36Sopenharmony_ci	 * Disable all IRQ lines so faulty external hardware won't
4262306a36Sopenharmony_ci	 * trigger interrupt that kernel is not ready to handle.
4362306a36Sopenharmony_ci	 */
4462306a36Sopenharmony_ci	for (i = TIMER0_IRQ; i < NR_CPU_IRQS; i++) {
4562306a36Sopenharmony_ci		unsigned int ienb;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci		ienb = read_aux_reg(AUX_IENABLE);
4862306a36Sopenharmony_ci		ienb &= ~(1 << i);
4962306a36Sopenharmony_ci		write_aux_reg(AUX_IENABLE, ienb);
5062306a36Sopenharmony_ci	}
5162306a36Sopenharmony_ci}
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci/*
5462306a36Sopenharmony_ci * ARC700 core includes a simple on-chip intc supporting
5562306a36Sopenharmony_ci * -per IRQ enable/disable
5662306a36Sopenharmony_ci * -2 levels of interrupts (high/low)
5762306a36Sopenharmony_ci * -all interrupts being level triggered
5862306a36Sopenharmony_ci *
5962306a36Sopenharmony_ci * To reduce platform code, we assume all IRQs directly hooked-up into intc.
6062306a36Sopenharmony_ci * Platforms with external intc, hence cascaded IRQs, are free to over-ride
6162306a36Sopenharmony_ci * below, per IRQ.
6262306a36Sopenharmony_ci */
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_cistatic void arc_irq_mask(struct irq_data *data)
6562306a36Sopenharmony_ci{
6662306a36Sopenharmony_ci	unsigned int ienb;
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	ienb = read_aux_reg(AUX_IENABLE);
6962306a36Sopenharmony_ci	ienb &= ~(1 << data->hwirq);
7062306a36Sopenharmony_ci	write_aux_reg(AUX_IENABLE, ienb);
7162306a36Sopenharmony_ci}
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_cistatic void arc_irq_unmask(struct irq_data *data)
7462306a36Sopenharmony_ci{
7562306a36Sopenharmony_ci	unsigned int ienb;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	ienb = read_aux_reg(AUX_IENABLE);
7862306a36Sopenharmony_ci	ienb |= (1 << data->hwirq);
7962306a36Sopenharmony_ci	write_aux_reg(AUX_IENABLE, ienb);
8062306a36Sopenharmony_ci}
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cistatic struct irq_chip onchip_intc = {
8362306a36Sopenharmony_ci	.name           = "ARC In-core Intc",
8462306a36Sopenharmony_ci	.irq_mask	= arc_irq_mask,
8562306a36Sopenharmony_ci	.irq_unmask	= arc_irq_unmask,
8662306a36Sopenharmony_ci};
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_cistatic int arc_intc_domain_map(struct irq_domain *d, unsigned int irq,
8962306a36Sopenharmony_ci			       irq_hw_number_t hw)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	switch (hw) {
9262306a36Sopenharmony_ci	case TIMER0_IRQ:
9362306a36Sopenharmony_ci		irq_set_percpu_devid(irq);
9462306a36Sopenharmony_ci		irq_set_chip_and_handler(irq, &onchip_intc, handle_percpu_irq);
9562306a36Sopenharmony_ci		break;
9662306a36Sopenharmony_ci	default:
9762306a36Sopenharmony_ci		irq_set_chip_and_handler(irq, &onchip_intc, handle_level_irq);
9862306a36Sopenharmony_ci	}
9962306a36Sopenharmony_ci	return 0;
10062306a36Sopenharmony_ci}
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_cistatic const struct irq_domain_ops arc_intc_domain_ops = {
10362306a36Sopenharmony_ci	.xlate = irq_domain_xlate_onecell,
10462306a36Sopenharmony_ci	.map = arc_intc_domain_map,
10562306a36Sopenharmony_ci};
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_cistatic int __init
10862306a36Sopenharmony_ciinit_onchip_IRQ(struct device_node *intc, struct device_node *parent)
10962306a36Sopenharmony_ci{
11062306a36Sopenharmony_ci	struct irq_domain *root_domain;
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	if (parent)
11362306a36Sopenharmony_ci		panic("DeviceTree incore intc not a root irq controller\n");
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	root_domain = irq_domain_add_linear(intc, NR_CPU_IRQS,
11662306a36Sopenharmony_ci					    &arc_intc_domain_ops, NULL);
11762306a36Sopenharmony_ci	if (!root_domain)
11862306a36Sopenharmony_ci		panic("root irq domain not avail\n");
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	/*
12162306a36Sopenharmony_ci	 * Needed for primary domain lookup to succeed
12262306a36Sopenharmony_ci	 * This is a primary irqchip, and can never have a parent
12362306a36Sopenharmony_ci	 */
12462306a36Sopenharmony_ci	irq_set_default_host(root_domain);
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	return 0;
12762306a36Sopenharmony_ci}
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ciIRQCHIP_DECLARE(arc_intc, "snps,arc700-intc", init_onchip_IRQ);
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci/*
13262306a36Sopenharmony_ci * arch_local_irq_enable - Enable interrupts.
13362306a36Sopenharmony_ci *
13462306a36Sopenharmony_ci * 1. Explicitly called to re-enable interrupts
13562306a36Sopenharmony_ci * 2. Implicitly called from spin_unlock_irq, write_unlock_irq etc
13662306a36Sopenharmony_ci *    which maybe in hard ISR itself
13762306a36Sopenharmony_ci *
13862306a36Sopenharmony_ci * Semantics of this function change depending on where it is called from:
13962306a36Sopenharmony_ci *
14062306a36Sopenharmony_ci * -If called from hard-ISR, it must not invert interrupt priorities
14162306a36Sopenharmony_ci *  e.g. suppose TIMER is high priority (Level 2) IRQ
14262306a36Sopenharmony_ci *    Time hard-ISR, timer_interrupt( ) calls spin_unlock_irq several times.
14362306a36Sopenharmony_ci *    Here local_irq_enable( ) shd not re-enable lower priority interrupts
14462306a36Sopenharmony_ci * -If called from soft-ISR, it must re-enable all interrupts
14562306a36Sopenharmony_ci *    soft ISR are low priority jobs which can be very slow, thus all IRQs
14662306a36Sopenharmony_ci *    must be enabled while they run.
14762306a36Sopenharmony_ci *    Now hardware context wise we may still be in L2 ISR (not done rtie)
14862306a36Sopenharmony_ci *    still we must re-enable both L1 and L2 IRQs
14962306a36Sopenharmony_ci *  Another twist is prev scenario with flow being
15062306a36Sopenharmony_ci *     L1 ISR ==> interrupted by L2 ISR  ==> L2 soft ISR
15162306a36Sopenharmony_ci *     here we must not re-enable Ll as prev Ll Interrupt's h/w context will get
15262306a36Sopenharmony_ci *     over-written (this is deficiency in ARC700 Interrupt mechanism)
15362306a36Sopenharmony_ci */
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci#ifdef CONFIG_ARC_COMPACT_IRQ_LEVELS	/* Complex version for 2 IRQ levels */
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_civoid arch_local_irq_enable(void)
15862306a36Sopenharmony_ci{
15962306a36Sopenharmony_ci	unsigned long flags = arch_local_save_flags();
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci	if (flags & STATUS_A2_MASK)
16262306a36Sopenharmony_ci		flags |= STATUS_E2_MASK;
16362306a36Sopenharmony_ci	else if (flags & STATUS_A1_MASK)
16462306a36Sopenharmony_ci		flags |= STATUS_E1_MASK;
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	arch_local_irq_restore(flags);
16762306a36Sopenharmony_ci}
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ciEXPORT_SYMBOL(arch_local_irq_enable);
17062306a36Sopenharmony_ci#endif
171