162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci *
362306a36Sopenharmony_ci * Programmable Interrupt Controller functions for the Freescale MPC52xx.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2008 Secret Lab Technologies Ltd.
662306a36Sopenharmony_ci * Copyright (C) 2006 bplan GmbH
762306a36Sopenharmony_ci * Copyright (C) 2004 Sylvain Munaut <tnt@246tNt.com>
862306a36Sopenharmony_ci * Copyright (C) 2003 Montavista Software, Inc
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * Based on the code from the 2.4 kernel by
1162306a36Sopenharmony_ci * Dale Farnsworth <dfarnsworth@mvista.com> and Kent Borg.
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * This file is licensed under the terms of the GNU General Public License
1462306a36Sopenharmony_ci * version 2. This program is licensed "as is" without any warranty of any
1562306a36Sopenharmony_ci * kind, whether express or implied.
1662306a36Sopenharmony_ci *
1762306a36Sopenharmony_ci */
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/*
2062306a36Sopenharmony_ci * This is the device driver for the MPC5200 interrupt controller.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci * hardware overview
2362306a36Sopenharmony_ci * -----------------
2462306a36Sopenharmony_ci * The MPC5200 interrupt controller groups the all interrupt sources into
2562306a36Sopenharmony_ci * three groups called 'critical', 'main', and 'peripheral'.  The critical
2662306a36Sopenharmony_ci * group has 3 irqs, External IRQ0, slice timer 0 irq, and wake from deep
2762306a36Sopenharmony_ci * sleep.  Main group include the other 3 external IRQs, slice timer 1, RTC,
2862306a36Sopenharmony_ci * gpios, and the general purpose timers.  Peripheral group contains the
2962306a36Sopenharmony_ci * remaining irq sources from all of the on-chip peripherals (PSCs, Ethernet,
3062306a36Sopenharmony_ci * USB, DMA, etc).
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci * virqs
3362306a36Sopenharmony_ci * -----
3462306a36Sopenharmony_ci * The Linux IRQ subsystem requires that each irq source be assigned a
3562306a36Sopenharmony_ci * system wide unique IRQ number starting at 1 (0 means no irq).  Since
3662306a36Sopenharmony_ci * systems can have multiple interrupt controllers, the virtual IRQ (virq)
3762306a36Sopenharmony_ci * infrastructure lets each interrupt controller to define a local set
3862306a36Sopenharmony_ci * of IRQ numbers and the virq infrastructure maps those numbers into
3962306a36Sopenharmony_ci * a unique range of the global IRQ# space.
4062306a36Sopenharmony_ci *
4162306a36Sopenharmony_ci * To define a range of virq numbers for this controller, this driver first
4262306a36Sopenharmony_ci * assigns a number to each of the irq groups (called the level 1 or L1
4362306a36Sopenharmony_ci * value).  Within each group individual irq sources are also assigned a
4462306a36Sopenharmony_ci * number, as defined by the MPC5200 user guide, and refers to it as the
4562306a36Sopenharmony_ci * level 2 or L2 value.  The virq number is determined by shifting up the
4662306a36Sopenharmony_ci * L1 value by MPC52xx_IRQ_L1_OFFSET and ORing it with the L2 value.
4762306a36Sopenharmony_ci *
4862306a36Sopenharmony_ci * For example, the TMR0 interrupt is irq 9 in the main group.  The
4962306a36Sopenharmony_ci * virq for TMR0 is calculated by ((1 << MPC52xx_IRQ_L1_OFFSET) | 9).
5062306a36Sopenharmony_ci *
5162306a36Sopenharmony_ci * The observant reader will also notice that this driver defines a 4th
5262306a36Sopenharmony_ci * interrupt group called 'bestcomm'.  The bestcomm group isn't physically
5362306a36Sopenharmony_ci * part of the MPC5200 interrupt controller, but it is used here to assign
5462306a36Sopenharmony_ci * a separate virq number for each bestcomm task (since any of the 16
5562306a36Sopenharmony_ci * bestcomm tasks can cause the bestcomm interrupt to be raised).  When a
5662306a36Sopenharmony_ci * bestcomm interrupt occurs (peripheral group, irq 0) this driver determines
5762306a36Sopenharmony_ci * which task needs servicing and returns the irq number for that task.  This
5862306a36Sopenharmony_ci * allows drivers which use bestcomm to define their own interrupt handlers.
5962306a36Sopenharmony_ci *
6062306a36Sopenharmony_ci * irq_chip structures
6162306a36Sopenharmony_ci * -------------------
6262306a36Sopenharmony_ci * For actually manipulating IRQs (masking, enabling, clearing, etc) this
6362306a36Sopenharmony_ci * driver defines four separate 'irq_chip' structures, one for the main
6462306a36Sopenharmony_ci * group, one for the peripherals group, one for the bestcomm group and one
6562306a36Sopenharmony_ci * for external interrupts.  The irq_chip structures provide the hooks needed
6662306a36Sopenharmony_ci * to manipulate each IRQ source, and since each group is has a separate set
6762306a36Sopenharmony_ci * of registers for controlling the irq, it makes sense to divide up the
6862306a36Sopenharmony_ci * hooks along those lines.
6962306a36Sopenharmony_ci *
7062306a36Sopenharmony_ci * You'll notice that there is not an irq_chip for the critical group and
7162306a36Sopenharmony_ci * you'll also notice that there is an irq_chip defined for external
7262306a36Sopenharmony_ci * interrupts even though there is no external interrupt group.  The reason
7362306a36Sopenharmony_ci * for this is that the four external interrupts are all managed with the same
7462306a36Sopenharmony_ci * register even though one of the external IRQs is in the critical group and
7562306a36Sopenharmony_ci * the other three are in the main group.  For this reason it makes sense for
7662306a36Sopenharmony_ci * the 4 external irqs to be managed using a separate set of hooks.  The
7762306a36Sopenharmony_ci * reason there is no crit irq_chip is that of the 3 irqs in the critical
7862306a36Sopenharmony_ci * group, only external interrupt is actually support at this time by this
7962306a36Sopenharmony_ci * driver and since external interrupt is the only one used, it can just
8062306a36Sopenharmony_ci * be directed to make use of the external irq irq_chip.
8162306a36Sopenharmony_ci *
8262306a36Sopenharmony_ci * device tree bindings
8362306a36Sopenharmony_ci * --------------------
8462306a36Sopenharmony_ci * The device tree bindings for this controller reflect the two level
8562306a36Sopenharmony_ci * organization of irqs in the device.  #interrupt-cells = <3> where the
8662306a36Sopenharmony_ci * first cell is the group number [0..3], the second cell is the irq
8762306a36Sopenharmony_ci * number in the group, and the third cell is the sense type (level/edge).
8862306a36Sopenharmony_ci * For reference, the following is a list of the interrupt property values
8962306a36Sopenharmony_ci * associated with external interrupt sources on the MPC5200 (just because
9062306a36Sopenharmony_ci * it is non-obvious to determine what the interrupts property should be
9162306a36Sopenharmony_ci * when reading the mpc5200 manual and it is a frequently asked question).
9262306a36Sopenharmony_ci *
9362306a36Sopenharmony_ci * External interrupts:
9462306a36Sopenharmony_ci * <0 0 n>	external irq0, n is sense	(n=0: level high,
9562306a36Sopenharmony_ci * <1 1 n>	external irq1, n is sense	 n=1: edge rising,
9662306a36Sopenharmony_ci * <1 2 n>	external irq2, n is sense	 n=2: edge falling,
9762306a36Sopenharmony_ci * <1 3 n>	external irq3, n is sense	 n=3: level low)
9862306a36Sopenharmony_ci */
9962306a36Sopenharmony_ci#undef DEBUG
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci#include <linux/interrupt.h>
10262306a36Sopenharmony_ci#include <linux/irq.h>
10362306a36Sopenharmony_ci#include <linux/of.h>
10462306a36Sopenharmony_ci#include <linux/of_address.h>
10562306a36Sopenharmony_ci#include <linux/of_irq.h>
10662306a36Sopenharmony_ci#include <asm/io.h>
10762306a36Sopenharmony_ci#include <asm/mpc52xx.h>
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci/* HW IRQ mapping */
11062306a36Sopenharmony_ci#define MPC52xx_IRQ_L1_CRIT	(0)
11162306a36Sopenharmony_ci#define MPC52xx_IRQ_L1_MAIN	(1)
11262306a36Sopenharmony_ci#define MPC52xx_IRQ_L1_PERP	(2)
11362306a36Sopenharmony_ci#define MPC52xx_IRQ_L1_SDMA	(3)
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci#define MPC52xx_IRQ_L1_OFFSET	(6)
11662306a36Sopenharmony_ci#define MPC52xx_IRQ_L1_MASK	(0x00c0)
11762306a36Sopenharmony_ci#define MPC52xx_IRQ_L2_MASK	(0x003f)
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci#define MPC52xx_IRQ_HIGHTESTHWIRQ (0xd0)
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci/* MPC5200 device tree match tables */
12362306a36Sopenharmony_cistatic const struct of_device_id mpc52xx_pic_ids[] __initconst = {
12462306a36Sopenharmony_ci	{ .compatible = "fsl,mpc5200-pic", },
12562306a36Sopenharmony_ci	{ .compatible = "mpc5200-pic", },
12662306a36Sopenharmony_ci	{}
12762306a36Sopenharmony_ci};
12862306a36Sopenharmony_cistatic const struct of_device_id mpc52xx_sdma_ids[] __initconst = {
12962306a36Sopenharmony_ci	{ .compatible = "fsl,mpc5200-bestcomm", },
13062306a36Sopenharmony_ci	{ .compatible = "mpc5200-bestcomm", },
13162306a36Sopenharmony_ci	{}
13262306a36Sopenharmony_ci};
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_cistatic struct mpc52xx_intr __iomem *intr;
13562306a36Sopenharmony_cistatic struct mpc52xx_sdma __iomem *sdma;
13662306a36Sopenharmony_cistatic struct irq_domain *mpc52xx_irqhost = NULL;
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_cistatic unsigned char mpc52xx_map_senses[4] = {
13962306a36Sopenharmony_ci	IRQ_TYPE_LEVEL_HIGH,
14062306a36Sopenharmony_ci	IRQ_TYPE_EDGE_RISING,
14162306a36Sopenharmony_ci	IRQ_TYPE_EDGE_FALLING,
14262306a36Sopenharmony_ci	IRQ_TYPE_LEVEL_LOW,
14362306a36Sopenharmony_ci};
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci/* Utility functions */
14662306a36Sopenharmony_cistatic inline void io_be_setbit(u32 __iomem *addr, int bitno)
14762306a36Sopenharmony_ci{
14862306a36Sopenharmony_ci	out_be32(addr, in_be32(addr) | (1 << bitno));
14962306a36Sopenharmony_ci}
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_cistatic inline void io_be_clrbit(u32 __iomem *addr, int bitno)
15262306a36Sopenharmony_ci{
15362306a36Sopenharmony_ci	out_be32(addr, in_be32(addr) & ~(1 << bitno));
15462306a36Sopenharmony_ci}
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci/*
15762306a36Sopenharmony_ci * IRQ[0-3] interrupt irq_chip
15862306a36Sopenharmony_ci */
15962306a36Sopenharmony_cistatic void mpc52xx_extirq_mask(struct irq_data *d)
16062306a36Sopenharmony_ci{
16162306a36Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
16262306a36Sopenharmony_ci	io_be_clrbit(&intr->ctrl, 11 - l2irq);
16362306a36Sopenharmony_ci}
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_cistatic void mpc52xx_extirq_unmask(struct irq_data *d)
16662306a36Sopenharmony_ci{
16762306a36Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
16862306a36Sopenharmony_ci	io_be_setbit(&intr->ctrl, 11 - l2irq);
16962306a36Sopenharmony_ci}
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_cistatic void mpc52xx_extirq_ack(struct irq_data *d)
17262306a36Sopenharmony_ci{
17362306a36Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
17462306a36Sopenharmony_ci	io_be_setbit(&intr->ctrl, 27-l2irq);
17562306a36Sopenharmony_ci}
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_cistatic int mpc52xx_extirq_set_type(struct irq_data *d, unsigned int flow_type)
17862306a36Sopenharmony_ci{
17962306a36Sopenharmony_ci	u32 ctrl_reg, type;
18062306a36Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
18162306a36Sopenharmony_ci	void *handler = handle_level_irq;
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci	pr_debug("%s: irq=%x. l2=%d flow_type=%d\n", __func__,
18462306a36Sopenharmony_ci		(int) irqd_to_hwirq(d), l2irq, flow_type);
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	switch (flow_type) {
18762306a36Sopenharmony_ci	case IRQF_TRIGGER_HIGH: type = 0; break;
18862306a36Sopenharmony_ci	case IRQF_TRIGGER_RISING: type = 1; handler = handle_edge_irq; break;
18962306a36Sopenharmony_ci	case IRQF_TRIGGER_FALLING: type = 2; handler = handle_edge_irq; break;
19062306a36Sopenharmony_ci	case IRQF_TRIGGER_LOW: type = 3; break;
19162306a36Sopenharmony_ci	default:
19262306a36Sopenharmony_ci		type = 0;
19362306a36Sopenharmony_ci	}
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci	ctrl_reg = in_be32(&intr->ctrl);
19662306a36Sopenharmony_ci	ctrl_reg &= ~(0x3 << (22 - (l2irq * 2)));
19762306a36Sopenharmony_ci	ctrl_reg |= (type << (22 - (l2irq * 2)));
19862306a36Sopenharmony_ci	out_be32(&intr->ctrl, ctrl_reg);
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci	irq_set_handler_locked(d, handler);
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci	return 0;
20362306a36Sopenharmony_ci}
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_cistatic struct irq_chip mpc52xx_extirq_irqchip = {
20662306a36Sopenharmony_ci	.name = "MPC52xx External",
20762306a36Sopenharmony_ci	.irq_mask = mpc52xx_extirq_mask,
20862306a36Sopenharmony_ci	.irq_unmask = mpc52xx_extirq_unmask,
20962306a36Sopenharmony_ci	.irq_ack = mpc52xx_extirq_ack,
21062306a36Sopenharmony_ci	.irq_set_type = mpc52xx_extirq_set_type,
21162306a36Sopenharmony_ci};
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci/*
21462306a36Sopenharmony_ci * Main interrupt irq_chip
21562306a36Sopenharmony_ci */
21662306a36Sopenharmony_cistatic int mpc52xx_null_set_type(struct irq_data *d, unsigned int flow_type)
21762306a36Sopenharmony_ci{
21862306a36Sopenharmony_ci	return 0; /* Do nothing so that the sense mask will get updated */
21962306a36Sopenharmony_ci}
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_cistatic void mpc52xx_main_mask(struct irq_data *d)
22262306a36Sopenharmony_ci{
22362306a36Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
22462306a36Sopenharmony_ci	io_be_setbit(&intr->main_mask, 16 - l2irq);
22562306a36Sopenharmony_ci}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_cistatic void mpc52xx_main_unmask(struct irq_data *d)
22862306a36Sopenharmony_ci{
22962306a36Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
23062306a36Sopenharmony_ci	io_be_clrbit(&intr->main_mask, 16 - l2irq);
23162306a36Sopenharmony_ci}
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_cistatic struct irq_chip mpc52xx_main_irqchip = {
23462306a36Sopenharmony_ci	.name = "MPC52xx Main",
23562306a36Sopenharmony_ci	.irq_mask = mpc52xx_main_mask,
23662306a36Sopenharmony_ci	.irq_mask_ack = mpc52xx_main_mask,
23762306a36Sopenharmony_ci	.irq_unmask = mpc52xx_main_unmask,
23862306a36Sopenharmony_ci	.irq_set_type = mpc52xx_null_set_type,
23962306a36Sopenharmony_ci};
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci/*
24262306a36Sopenharmony_ci * Peripherals interrupt irq_chip
24362306a36Sopenharmony_ci */
24462306a36Sopenharmony_cistatic void mpc52xx_periph_mask(struct irq_data *d)
24562306a36Sopenharmony_ci{
24662306a36Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
24762306a36Sopenharmony_ci	io_be_setbit(&intr->per_mask, 31 - l2irq);
24862306a36Sopenharmony_ci}
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_cistatic void mpc52xx_periph_unmask(struct irq_data *d)
25162306a36Sopenharmony_ci{
25262306a36Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
25362306a36Sopenharmony_ci	io_be_clrbit(&intr->per_mask, 31 - l2irq);
25462306a36Sopenharmony_ci}
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_cistatic struct irq_chip mpc52xx_periph_irqchip = {
25762306a36Sopenharmony_ci	.name = "MPC52xx Peripherals",
25862306a36Sopenharmony_ci	.irq_mask = mpc52xx_periph_mask,
25962306a36Sopenharmony_ci	.irq_mask_ack = mpc52xx_periph_mask,
26062306a36Sopenharmony_ci	.irq_unmask = mpc52xx_periph_unmask,
26162306a36Sopenharmony_ci	.irq_set_type = mpc52xx_null_set_type,
26262306a36Sopenharmony_ci};
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci/*
26562306a36Sopenharmony_ci * SDMA interrupt irq_chip
26662306a36Sopenharmony_ci */
26762306a36Sopenharmony_cistatic void mpc52xx_sdma_mask(struct irq_data *d)
26862306a36Sopenharmony_ci{
26962306a36Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
27062306a36Sopenharmony_ci	io_be_setbit(&sdma->IntMask, l2irq);
27162306a36Sopenharmony_ci}
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_cistatic void mpc52xx_sdma_unmask(struct irq_data *d)
27462306a36Sopenharmony_ci{
27562306a36Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
27662306a36Sopenharmony_ci	io_be_clrbit(&sdma->IntMask, l2irq);
27762306a36Sopenharmony_ci}
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_cistatic void mpc52xx_sdma_ack(struct irq_data *d)
28062306a36Sopenharmony_ci{
28162306a36Sopenharmony_ci	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
28262306a36Sopenharmony_ci	out_be32(&sdma->IntPend, 1 << l2irq);
28362306a36Sopenharmony_ci}
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_cistatic struct irq_chip mpc52xx_sdma_irqchip = {
28662306a36Sopenharmony_ci	.name = "MPC52xx SDMA",
28762306a36Sopenharmony_ci	.irq_mask = mpc52xx_sdma_mask,
28862306a36Sopenharmony_ci	.irq_unmask = mpc52xx_sdma_unmask,
28962306a36Sopenharmony_ci	.irq_ack = mpc52xx_sdma_ack,
29062306a36Sopenharmony_ci	.irq_set_type = mpc52xx_null_set_type,
29162306a36Sopenharmony_ci};
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ci/**
29462306a36Sopenharmony_ci * mpc52xx_is_extirq - Returns true if hwirq number is for an external IRQ
29562306a36Sopenharmony_ci */
29662306a36Sopenharmony_cistatic int mpc52xx_is_extirq(int l1, int l2)
29762306a36Sopenharmony_ci{
29862306a36Sopenharmony_ci	return ((l1 == 0) && (l2 == 0)) ||
29962306a36Sopenharmony_ci	       ((l1 == 1) && (l2 >= 1) && (l2 <= 3));
30062306a36Sopenharmony_ci}
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci/**
30362306a36Sopenharmony_ci * mpc52xx_irqhost_xlate - translate virq# from device tree interrupts property
30462306a36Sopenharmony_ci */
30562306a36Sopenharmony_cistatic int mpc52xx_irqhost_xlate(struct irq_domain *h, struct device_node *ct,
30662306a36Sopenharmony_ci				 const u32 *intspec, unsigned int intsize,
30762306a36Sopenharmony_ci				 irq_hw_number_t *out_hwirq,
30862306a36Sopenharmony_ci				 unsigned int *out_flags)
30962306a36Sopenharmony_ci{
31062306a36Sopenharmony_ci	int intrvect_l1;
31162306a36Sopenharmony_ci	int intrvect_l2;
31262306a36Sopenharmony_ci	int intrvect_type;
31362306a36Sopenharmony_ci	int intrvect_linux;
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci	if (intsize != 3)
31662306a36Sopenharmony_ci		return -1;
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_ci	intrvect_l1 = (int)intspec[0];
31962306a36Sopenharmony_ci	intrvect_l2 = (int)intspec[1];
32062306a36Sopenharmony_ci	intrvect_type = (int)intspec[2] & 0x3;
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci	intrvect_linux = (intrvect_l1 << MPC52xx_IRQ_L1_OFFSET) &
32362306a36Sopenharmony_ci			 MPC52xx_IRQ_L1_MASK;
32462306a36Sopenharmony_ci	intrvect_linux |= intrvect_l2 & MPC52xx_IRQ_L2_MASK;
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci	*out_hwirq = intrvect_linux;
32762306a36Sopenharmony_ci	*out_flags = IRQ_TYPE_LEVEL_LOW;
32862306a36Sopenharmony_ci	if (mpc52xx_is_extirq(intrvect_l1, intrvect_l2))
32962306a36Sopenharmony_ci		*out_flags = mpc52xx_map_senses[intrvect_type];
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ci	pr_debug("return %x, l1=%d, l2=%d\n", intrvect_linux, intrvect_l1,
33262306a36Sopenharmony_ci		 intrvect_l2);
33362306a36Sopenharmony_ci	return 0;
33462306a36Sopenharmony_ci}
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci/**
33762306a36Sopenharmony_ci * mpc52xx_irqhost_map - Hook to map from virq to an irq_chip structure
33862306a36Sopenharmony_ci */
33962306a36Sopenharmony_cistatic int mpc52xx_irqhost_map(struct irq_domain *h, unsigned int virq,
34062306a36Sopenharmony_ci			       irq_hw_number_t irq)
34162306a36Sopenharmony_ci{
34262306a36Sopenharmony_ci	int l1irq;
34362306a36Sopenharmony_ci	int l2irq;
34462306a36Sopenharmony_ci	struct irq_chip *irqchip;
34562306a36Sopenharmony_ci	void *hndlr;
34662306a36Sopenharmony_ci	int type;
34762306a36Sopenharmony_ci	u32 reg;
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci	l1irq = (irq & MPC52xx_IRQ_L1_MASK) >> MPC52xx_IRQ_L1_OFFSET;
35062306a36Sopenharmony_ci	l2irq = irq & MPC52xx_IRQ_L2_MASK;
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci	/*
35362306a36Sopenharmony_ci	 * External IRQs are handled differently by the hardware so they are
35462306a36Sopenharmony_ci	 * handled by a dedicated irq_chip structure.
35562306a36Sopenharmony_ci	 */
35662306a36Sopenharmony_ci	if (mpc52xx_is_extirq(l1irq, l2irq)) {
35762306a36Sopenharmony_ci		reg = in_be32(&intr->ctrl);
35862306a36Sopenharmony_ci		type = mpc52xx_map_senses[(reg >> (22 - l2irq * 2)) & 0x3];
35962306a36Sopenharmony_ci		if ((type == IRQ_TYPE_EDGE_FALLING) ||
36062306a36Sopenharmony_ci		    (type == IRQ_TYPE_EDGE_RISING))
36162306a36Sopenharmony_ci			hndlr = handle_edge_irq;
36262306a36Sopenharmony_ci		else
36362306a36Sopenharmony_ci			hndlr = handle_level_irq;
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci		irq_set_chip_and_handler(virq, &mpc52xx_extirq_irqchip, hndlr);
36662306a36Sopenharmony_ci		pr_debug("%s: External IRQ%i virq=%x, hw=%x. type=%x\n",
36762306a36Sopenharmony_ci			 __func__, l2irq, virq, (int)irq, type);
36862306a36Sopenharmony_ci		return 0;
36962306a36Sopenharmony_ci	}
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci	/* It is an internal SOC irq.  Choose the correct irq_chip */
37262306a36Sopenharmony_ci	switch (l1irq) {
37362306a36Sopenharmony_ci	case MPC52xx_IRQ_L1_MAIN: irqchip = &mpc52xx_main_irqchip; break;
37462306a36Sopenharmony_ci	case MPC52xx_IRQ_L1_PERP: irqchip = &mpc52xx_periph_irqchip; break;
37562306a36Sopenharmony_ci	case MPC52xx_IRQ_L1_SDMA: irqchip = &mpc52xx_sdma_irqchip; break;
37662306a36Sopenharmony_ci	case MPC52xx_IRQ_L1_CRIT:
37762306a36Sopenharmony_ci		pr_warn("%s: Critical IRQ #%d is unsupported! Nopping it.\n",
37862306a36Sopenharmony_ci			__func__, l2irq);
37962306a36Sopenharmony_ci		irq_set_chip(virq, &no_irq_chip);
38062306a36Sopenharmony_ci		return 0;
38162306a36Sopenharmony_ci	}
38262306a36Sopenharmony_ci
38362306a36Sopenharmony_ci	irq_set_chip_and_handler(virq, irqchip, handle_level_irq);
38462306a36Sopenharmony_ci	pr_debug("%s: virq=%x, l1=%i, l2=%i\n", __func__, virq, l1irq, l2irq);
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	return 0;
38762306a36Sopenharmony_ci}
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_cistatic const struct irq_domain_ops mpc52xx_irqhost_ops = {
39062306a36Sopenharmony_ci	.xlate = mpc52xx_irqhost_xlate,
39162306a36Sopenharmony_ci	.map = mpc52xx_irqhost_map,
39262306a36Sopenharmony_ci};
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci/**
39562306a36Sopenharmony_ci * mpc52xx_init_irq - Initialize and register with the virq subsystem
39662306a36Sopenharmony_ci *
39762306a36Sopenharmony_ci * Hook for setting up IRQs on an mpc5200 system.  A pointer to this function
39862306a36Sopenharmony_ci * is to be put into the machine definition structure.
39962306a36Sopenharmony_ci *
40062306a36Sopenharmony_ci * This function searches the device tree for an MPC5200 interrupt controller,
40162306a36Sopenharmony_ci * initializes it, and registers it with the virq subsystem.
40262306a36Sopenharmony_ci */
40362306a36Sopenharmony_civoid __init mpc52xx_init_irq(void)
40462306a36Sopenharmony_ci{
40562306a36Sopenharmony_ci	u32 intr_ctrl;
40662306a36Sopenharmony_ci	struct device_node *picnode;
40762306a36Sopenharmony_ci	struct device_node *np;
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ci	/* Remap the necessary zones */
41062306a36Sopenharmony_ci	picnode = of_find_matching_node(NULL, mpc52xx_pic_ids);
41162306a36Sopenharmony_ci	intr = of_iomap(picnode, 0);
41262306a36Sopenharmony_ci	if (!intr)
41362306a36Sopenharmony_ci		panic(__FILE__	": find_and_map failed on 'mpc5200-pic'. "
41462306a36Sopenharmony_ci				"Check node !");
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ci	np = of_find_matching_node(NULL, mpc52xx_sdma_ids);
41762306a36Sopenharmony_ci	sdma = of_iomap(np, 0);
41862306a36Sopenharmony_ci	of_node_put(np);
41962306a36Sopenharmony_ci	if (!sdma)
42062306a36Sopenharmony_ci		panic(__FILE__	": find_and_map failed on 'mpc5200-bestcomm'. "
42162306a36Sopenharmony_ci				"Check node !");
42262306a36Sopenharmony_ci
42362306a36Sopenharmony_ci	pr_debug("MPC5200 IRQ controller mapped to 0x%p\n", intr);
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci	/* Disable all interrupt sources. */
42662306a36Sopenharmony_ci	out_be32(&sdma->IntPend, 0xffffffff);	/* 1 means clear pending */
42762306a36Sopenharmony_ci	out_be32(&sdma->IntMask, 0xffffffff);	/* 1 means disabled */
42862306a36Sopenharmony_ci	out_be32(&intr->per_mask, 0x7ffffc00);	/* 1 means disabled */
42962306a36Sopenharmony_ci	out_be32(&intr->main_mask, 0x00010fff);	/* 1 means disabled */
43062306a36Sopenharmony_ci	intr_ctrl = in_be32(&intr->ctrl);
43162306a36Sopenharmony_ci	intr_ctrl &= 0x00ff0000;	/* Keeps IRQ[0-3] config */
43262306a36Sopenharmony_ci	intr_ctrl |=	0x0f000000 |	/* clear IRQ 0-3 */
43362306a36Sopenharmony_ci			0x00001000 |	/* MEE master external enable */
43462306a36Sopenharmony_ci			0x00000000 |	/* 0 means disable IRQ 0-3 */
43562306a36Sopenharmony_ci			0x00000001;	/* CEb route critical normally */
43662306a36Sopenharmony_ci	out_be32(&intr->ctrl, intr_ctrl);
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci	/* Zero a bunch of the priority settings. */
43962306a36Sopenharmony_ci	out_be32(&intr->per_pri1, 0);
44062306a36Sopenharmony_ci	out_be32(&intr->per_pri2, 0);
44162306a36Sopenharmony_ci	out_be32(&intr->per_pri3, 0);
44262306a36Sopenharmony_ci	out_be32(&intr->main_pri1, 0);
44362306a36Sopenharmony_ci	out_be32(&intr->main_pri2, 0);
44462306a36Sopenharmony_ci
44562306a36Sopenharmony_ci	/*
44662306a36Sopenharmony_ci	 * As last step, add an irq host to translate the real
44762306a36Sopenharmony_ci	 * hw irq information provided by the ofw to linux virq
44862306a36Sopenharmony_ci	 */
44962306a36Sopenharmony_ci	mpc52xx_irqhost = irq_domain_add_linear(picnode,
45062306a36Sopenharmony_ci	                                 MPC52xx_IRQ_HIGHTESTHWIRQ,
45162306a36Sopenharmony_ci	                                 &mpc52xx_irqhost_ops, NULL);
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci	if (!mpc52xx_irqhost)
45462306a36Sopenharmony_ci		panic(__FILE__ ": Cannot allocate the IRQ host\n");
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci	irq_set_default_host(mpc52xx_irqhost);
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_ci	pr_info("MPC52xx PIC is up and running!\n");
45962306a36Sopenharmony_ci}
46062306a36Sopenharmony_ci
46162306a36Sopenharmony_ci/**
46262306a36Sopenharmony_ci * mpc52xx_get_irq - Get pending interrupt number hook function
46362306a36Sopenharmony_ci *
46462306a36Sopenharmony_ci * Called by the interrupt handler to determine what IRQ handler needs to be
46562306a36Sopenharmony_ci * executed.
46662306a36Sopenharmony_ci *
46762306a36Sopenharmony_ci * Status of pending interrupts is determined by reading the encoded status
46862306a36Sopenharmony_ci * register.  The encoded status register has three fields; one for each of the
46962306a36Sopenharmony_ci * types of interrupts defined by the controller - 'critical', 'main' and
47062306a36Sopenharmony_ci * 'peripheral'.  This function reads the status register and returns the IRQ
47162306a36Sopenharmony_ci * number associated with the highest priority pending interrupt.  'Critical'
47262306a36Sopenharmony_ci * interrupts have the highest priority, followed by 'main' interrupts, and
47362306a36Sopenharmony_ci * then 'peripheral'.
47462306a36Sopenharmony_ci *
47562306a36Sopenharmony_ci * The mpc5200 interrupt controller can be configured to boost the priority
47662306a36Sopenharmony_ci * of individual 'peripheral' interrupts.  If this is the case then a special
47762306a36Sopenharmony_ci * value will appear in either the crit or main fields indicating a high
47862306a36Sopenharmony_ci * or medium priority peripheral irq has occurred.
47962306a36Sopenharmony_ci *
48062306a36Sopenharmony_ci * This function checks each of the 3 irq request fields and returns the
48162306a36Sopenharmony_ci * first pending interrupt that it finds.
48262306a36Sopenharmony_ci *
48362306a36Sopenharmony_ci * This function also identifies a 4th type of interrupt; 'bestcomm'.  Each
48462306a36Sopenharmony_ci * bestcomm DMA task can raise the bestcomm peripheral interrupt.  When this
48562306a36Sopenharmony_ci * occurs at task-specific IRQ# is decoded so that each task can have its
48662306a36Sopenharmony_ci * own IRQ handler.
48762306a36Sopenharmony_ci */
48862306a36Sopenharmony_ciunsigned int mpc52xx_get_irq(void)
48962306a36Sopenharmony_ci{
49062306a36Sopenharmony_ci	u32 status;
49162306a36Sopenharmony_ci	int irq;
49262306a36Sopenharmony_ci
49362306a36Sopenharmony_ci	status = in_be32(&intr->enc_status);
49462306a36Sopenharmony_ci	if (status & 0x00000400) {	/* critical */
49562306a36Sopenharmony_ci		irq = (status >> 8) & 0x3;
49662306a36Sopenharmony_ci		if (irq == 2)	/* high priority peripheral */
49762306a36Sopenharmony_ci			goto peripheral;
49862306a36Sopenharmony_ci		irq |= (MPC52xx_IRQ_L1_CRIT << MPC52xx_IRQ_L1_OFFSET);
49962306a36Sopenharmony_ci	} else if (status & 0x00200000) {	/* main */
50062306a36Sopenharmony_ci		irq = (status >> 16) & 0x1f;
50162306a36Sopenharmony_ci		if (irq == 4)	/* low priority peripheral */
50262306a36Sopenharmony_ci			goto peripheral;
50362306a36Sopenharmony_ci		irq |= (MPC52xx_IRQ_L1_MAIN << MPC52xx_IRQ_L1_OFFSET);
50462306a36Sopenharmony_ci	} else if (status & 0x20000000) {	/* peripheral */
50562306a36Sopenharmony_ci	      peripheral:
50662306a36Sopenharmony_ci		irq = (status >> 24) & 0x1f;
50762306a36Sopenharmony_ci		if (irq == 0) {	/* bestcomm */
50862306a36Sopenharmony_ci			status = in_be32(&sdma->IntPend);
50962306a36Sopenharmony_ci			irq = ffs(status) - 1;
51062306a36Sopenharmony_ci			irq |= (MPC52xx_IRQ_L1_SDMA << MPC52xx_IRQ_L1_OFFSET);
51162306a36Sopenharmony_ci		} else {
51262306a36Sopenharmony_ci			irq |= (MPC52xx_IRQ_L1_PERP << MPC52xx_IRQ_L1_OFFSET);
51362306a36Sopenharmony_ci		}
51462306a36Sopenharmony_ci	} else {
51562306a36Sopenharmony_ci		return 0;
51662306a36Sopenharmony_ci	}
51762306a36Sopenharmony_ci
51862306a36Sopenharmony_ci	return irq_linear_revmap(mpc52xx_irqhost, irq);
51962306a36Sopenharmony_ci}
520