xref: /openbmc/linux/arch/powerpc/platforms/52xx/mpc52xx_pic.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
139d074b2SGrant Likely /*
239d074b2SGrant Likely  *
339d074b2SGrant Likely  * Programmable Interrupt Controller functions for the Freescale MPC52xx.
439d074b2SGrant Likely  *
5bcb73f56SGrant Likely  * Copyright (C) 2008 Secret Lab Technologies Ltd.
639d074b2SGrant Likely  * Copyright (C) 2006 bplan GmbH
7bcb73f56SGrant Likely  * Copyright (C) 2004 Sylvain Munaut <tnt@246tNt.com>
8bcb73f56SGrant Likely  * Copyright (C) 2003 Montavista Software, Inc
939d074b2SGrant Likely  *
1039d074b2SGrant Likely  * Based on the code from the 2.4 kernel by
1139d074b2SGrant Likely  * Dale Farnsworth <dfarnsworth@mvista.com> and Kent Borg.
1239d074b2SGrant Likely  *
1339d074b2SGrant Likely  * This file is licensed under the terms of the GNU General Public License
1439d074b2SGrant Likely  * version 2. This program is licensed "as is" without any warranty of any
1539d074b2SGrant Likely  * kind, whether express or implied.
1639d074b2SGrant Likely  *
1739d074b2SGrant Likely  */
1839d074b2SGrant Likely 
19bcb73f56SGrant Likely /*
20bcb73f56SGrant Likely  * This is the device driver for the MPC5200 interrupt controller.
21bcb73f56SGrant Likely  *
22bcb73f56SGrant Likely  * hardware overview
23bcb73f56SGrant Likely  * -----------------
24bcb73f56SGrant Likely  * The MPC5200 interrupt controller groups the all interrupt sources into
25bcb73f56SGrant Likely  * three groups called 'critical', 'main', and 'peripheral'.  The critical
26bcb73f56SGrant Likely  * group has 3 irqs, External IRQ0, slice timer 0 irq, and wake from deep
27bcb73f56SGrant Likely  * sleep.  Main group include the other 3 external IRQs, slice timer 1, RTC,
28bcb73f56SGrant Likely  * gpios, and the general purpose timers.  Peripheral group contains the
29bcb73f56SGrant Likely  * remaining irq sources from all of the on-chip peripherals (PSCs, Ethernet,
30bcb73f56SGrant Likely  * USB, DMA, etc).
31bcb73f56SGrant Likely  *
32bcb73f56SGrant Likely  * virqs
33bcb73f56SGrant Likely  * -----
34bcb73f56SGrant Likely  * The Linux IRQ subsystem requires that each irq source be assigned a
35bcb73f56SGrant Likely  * system wide unique IRQ number starting at 1 (0 means no irq).  Since
36bcb73f56SGrant Likely  * systems can have multiple interrupt controllers, the virtual IRQ (virq)
37bcb73f56SGrant Likely  * infrastructure lets each interrupt controller to define a local set
38bcb73f56SGrant Likely  * of IRQ numbers and the virq infrastructure maps those numbers into
39bcb73f56SGrant Likely  * a unique range of the global IRQ# space.
40bcb73f56SGrant Likely  *
41bcb73f56SGrant Likely  * To define a range of virq numbers for this controller, this driver first
42bcb73f56SGrant Likely  * assigns a number to each of the irq groups (called the level 1 or L1
43bcb73f56SGrant Likely  * value).  Within each group individual irq sources are also assigned a
44bcb73f56SGrant Likely  * number, as defined by the MPC5200 user guide, and refers to it as the
45bcb73f56SGrant Likely  * level 2 or L2 value.  The virq number is determined by shifting up the
46bcb73f56SGrant Likely  * L1 value by MPC52xx_IRQ_L1_OFFSET and ORing it with the L2 value.
47bcb73f56SGrant Likely  *
48bcb73f56SGrant Likely  * For example, the TMR0 interrupt is irq 9 in the main group.  The
49bcb73f56SGrant Likely  * virq for TMR0 is calculated by ((1 << MPC52xx_IRQ_L1_OFFSET) | 9).
50bcb73f56SGrant Likely  *
51bcb73f56SGrant Likely  * The observant reader will also notice that this driver defines a 4th
52bcb73f56SGrant Likely  * interrupt group called 'bestcomm'.  The bestcomm group isn't physically
53bcb73f56SGrant Likely  * part of the MPC5200 interrupt controller, but it is used here to assign
54bcb73f56SGrant Likely  * a separate virq number for each bestcomm task (since any of the 16
55bcb73f56SGrant Likely  * bestcomm tasks can cause the bestcomm interrupt to be raised).  When a
56bcb73f56SGrant Likely  * bestcomm interrupt occurs (peripheral group, irq 0) this driver determines
57bcb73f56SGrant Likely  * which task needs servicing and returns the irq number for that task.  This
58bcb73f56SGrant Likely  * allows drivers which use bestcomm to define their own interrupt handlers.
59bcb73f56SGrant Likely  *
60bcb73f56SGrant Likely  * irq_chip structures
61bcb73f56SGrant Likely  * -------------------
62bcb73f56SGrant Likely  * For actually manipulating IRQs (masking, enabling, clearing, etc) this
63bcb73f56SGrant Likely  * driver defines four separate 'irq_chip' structures, one for the main
64bcb73f56SGrant Likely  * group, one for the peripherals group, one for the bestcomm group and one
65bcb73f56SGrant Likely  * for external interrupts.  The irq_chip structures provide the hooks needed
66bcb73f56SGrant Likely  * to manipulate each IRQ source, and since each group is has a separate set
67bcb73f56SGrant Likely  * of registers for controlling the irq, it makes sense to divide up the
68bcb73f56SGrant Likely  * hooks along those lines.
69bcb73f56SGrant Likely  *
70bcb73f56SGrant Likely  * You'll notice that there is not an irq_chip for the critical group and
71bcb73f56SGrant Likely  * you'll also notice that there is an irq_chip defined for external
72bcb73f56SGrant Likely  * interrupts even though there is no external interrupt group.  The reason
73bcb73f56SGrant Likely  * for this is that the four external interrupts are all managed with the same
74bcb73f56SGrant Likely  * register even though one of the external IRQs is in the critical group and
75bcb73f56SGrant Likely  * the other three are in the main group.  For this reason it makes sense for
76bcb73f56SGrant Likely  * the 4 external irqs to be managed using a separate set of hooks.  The
77bcb73f56SGrant Likely  * reason there is no crit irq_chip is that of the 3 irqs in the critical
78bcb73f56SGrant Likely  * group, only external interrupt is actually support at this time by this
79bcb73f56SGrant Likely  * driver and since external interrupt is the only one used, it can just
80bcb73f56SGrant Likely  * be directed to make use of the external irq irq_chip.
81bcb73f56SGrant Likely  *
82bcb73f56SGrant Likely  * device tree bindings
83bcb73f56SGrant Likely  * --------------------
84bcb73f56SGrant Likely  * The device tree bindings for this controller reflect the two level
85bcb73f56SGrant Likely  * organization of irqs in the device.  #interrupt-cells = <3> where the
86bcb73f56SGrant Likely  * first cell is the group number [0..3], the second cell is the irq
87bcb73f56SGrant Likely  * number in the group, and the third cell is the sense type (level/edge).
88bcb73f56SGrant Likely  * For reference, the following is a list of the interrupt property values
89bcb73f56SGrant Likely  * associated with external interrupt sources on the MPC5200 (just because
90bcb73f56SGrant Likely  * it is non-obvious to determine what the interrupts property should be
91bcb73f56SGrant Likely  * when reading the mpc5200 manual and it is a frequently asked question).
92bcb73f56SGrant Likely  *
93bcb73f56SGrant Likely  * External interrupts:
94bcb73f56SGrant Likely  * <0 0 n>	external irq0, n is sense	(n=0: level high,
95bcb73f56SGrant Likely  * <1 1 n>	external irq1, n is sense	 n=1: edge rising,
96bcb73f56SGrant Likely  * <1 2 n>	external irq2, n is sense	 n=2: edge falling,
97bcb73f56SGrant Likely  * <1 3 n>	external irq3, n is sense	 n=3: level low)
98bcb73f56SGrant Likely  */
9939d074b2SGrant Likely #undef DEBUG
10039d074b2SGrant Likely 
101f800ab44SSascha Hauer #include <linux/interrupt.h>
10239d074b2SGrant Likely #include <linux/irq.h>
1039fe2e796SGrant Likely #include <linux/of.h>
104*e6f6390aSChristophe Leroy #include <linux/of_address.h>
105*e6f6390aSChristophe Leroy #include <linux/of_irq.h>
10639d074b2SGrant Likely #include <asm/io.h>
10739d074b2SGrant Likely #include <asm/mpc52xx.h>
10839d074b2SGrant Likely 
109bcb73f56SGrant Likely /* HW IRQ mapping */
110bcb73f56SGrant Likely #define MPC52xx_IRQ_L1_CRIT	(0)
111bcb73f56SGrant Likely #define MPC52xx_IRQ_L1_MAIN	(1)
112bcb73f56SGrant Likely #define MPC52xx_IRQ_L1_PERP	(2)
113bcb73f56SGrant Likely #define MPC52xx_IRQ_L1_SDMA	(3)
114bcb73f56SGrant Likely 
115bcb73f56SGrant Likely #define MPC52xx_IRQ_L1_OFFSET	(6)
116bcb73f56SGrant Likely #define MPC52xx_IRQ_L1_MASK	(0x00c0)
117bcb73f56SGrant Likely #define MPC52xx_IRQ_L2_MASK	(0x003f)
118bcb73f56SGrant Likely 
119bcb73f56SGrant Likely #define MPC52xx_IRQ_HIGHTESTHWIRQ (0xd0)
120bcb73f56SGrant Likely 
12139d074b2SGrant Likely 
12266ffbe49SGrant Likely /* MPC5200 device tree match tables */
123ce6d73c9SUwe Kleine-König static const struct of_device_id mpc52xx_pic_ids[] __initconst = {
12466ffbe49SGrant Likely 	{ .compatible = "fsl,mpc5200-pic", },
12566ffbe49SGrant Likely 	{ .compatible = "mpc5200-pic", },
12666ffbe49SGrant Likely 	{}
12766ffbe49SGrant Likely };
128ce6d73c9SUwe Kleine-König static const struct of_device_id mpc52xx_sdma_ids[] __initconst = {
12966ffbe49SGrant Likely 	{ .compatible = "fsl,mpc5200-bestcomm", },
13066ffbe49SGrant Likely 	{ .compatible = "mpc5200-bestcomm", },
13166ffbe49SGrant Likely 	{}
13266ffbe49SGrant Likely };
13366ffbe49SGrant Likely 
13439d074b2SGrant Likely static struct mpc52xx_intr __iomem *intr;
13539d074b2SGrant Likely static struct mpc52xx_sdma __iomem *sdma;
136bae1d8f1SGrant Likely static struct irq_domain *mpc52xx_irqhost = NULL;
13739d074b2SGrant Likely 
13839d074b2SGrant Likely static unsigned char mpc52xx_map_senses[4] = {
13939d074b2SGrant Likely 	IRQ_TYPE_LEVEL_HIGH,
14039d074b2SGrant Likely 	IRQ_TYPE_EDGE_RISING,
14139d074b2SGrant Likely 	IRQ_TYPE_EDGE_FALLING,
14239d074b2SGrant Likely 	IRQ_TYPE_LEVEL_LOW,
14339d074b2SGrant Likely };
14439d074b2SGrant Likely 
145bcb73f56SGrant Likely /* Utility functions */
io_be_setbit(u32 __iomem * addr,int bitno)14639d074b2SGrant Likely static inline void io_be_setbit(u32 __iomem *addr, int bitno)
14739d074b2SGrant Likely {
14839d074b2SGrant Likely 	out_be32(addr, in_be32(addr) | (1 << bitno));
14939d074b2SGrant Likely }
15039d074b2SGrant Likely 
io_be_clrbit(u32 __iomem * addr,int bitno)15139d074b2SGrant Likely static inline void io_be_clrbit(u32 __iomem *addr, int bitno)
15239d074b2SGrant Likely {
15339d074b2SGrant Likely 	out_be32(addr, in_be32(addr) & ~(1 << bitno));
15439d074b2SGrant Likely }
15539d074b2SGrant Likely 
15639d074b2SGrant Likely /*
15739d074b2SGrant Likely  * IRQ[0-3] interrupt irq_chip
15839d074b2SGrant Likely  */
mpc52xx_extirq_mask(struct irq_data * d)1598a2df7a0SLennert Buytenhek static void mpc52xx_extirq_mask(struct irq_data *d)
16039d074b2SGrant Likely {
161476eb491SGrant Likely 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
16239d074b2SGrant Likely 	io_be_clrbit(&intr->ctrl, 11 - l2irq);
16339d074b2SGrant Likely }
16439d074b2SGrant Likely 
mpc52xx_extirq_unmask(struct irq_data * d)1658a2df7a0SLennert Buytenhek static void mpc52xx_extirq_unmask(struct irq_data *d)
16639d074b2SGrant Likely {
167476eb491SGrant Likely 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
16839d074b2SGrant Likely 	io_be_setbit(&intr->ctrl, 11 - l2irq);
16939d074b2SGrant Likely }
17039d074b2SGrant Likely 
mpc52xx_extirq_ack(struct irq_data * d)1718a2df7a0SLennert Buytenhek static void mpc52xx_extirq_ack(struct irq_data *d)
17239d074b2SGrant Likely {
173476eb491SGrant Likely 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
17439d074b2SGrant Likely 	io_be_setbit(&intr->ctrl, 27-l2irq);
17539d074b2SGrant Likely }
17639d074b2SGrant Likely 
mpc52xx_extirq_set_type(struct irq_data * d,unsigned int flow_type)1778a2df7a0SLennert Buytenhek static int mpc52xx_extirq_set_type(struct irq_data *d, unsigned int flow_type)
178f800ab44SSascha Hauer {
179f800ab44SSascha Hauer 	u32 ctrl_reg, type;
180476eb491SGrant Likely 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
1818f2558deSGrant Likely 	void *handler = handle_level_irq;
182f800ab44SSascha Hauer 
183476eb491SGrant Likely 	pr_debug("%s: irq=%x. l2=%d flow_type=%d\n", __func__,
184476eb491SGrant Likely 		(int) irqd_to_hwirq(d), l2irq, flow_type);
185f800ab44SSascha Hauer 
186f800ab44SSascha Hauer 	switch (flow_type) {
1878f2558deSGrant Likely 	case IRQF_TRIGGER_HIGH: type = 0; break;
1888f2558deSGrant Likely 	case IRQF_TRIGGER_RISING: type = 1; handler = handle_edge_irq; break;
1898f2558deSGrant Likely 	case IRQF_TRIGGER_FALLING: type = 2; handler = handle_edge_irq; break;
1908f2558deSGrant Likely 	case IRQF_TRIGGER_LOW: type = 3; break;
191f800ab44SSascha Hauer 	default:
192f800ab44SSascha Hauer 		type = 0;
193f800ab44SSascha Hauer 	}
194f800ab44SSascha Hauer 
195f800ab44SSascha Hauer 	ctrl_reg = in_be32(&intr->ctrl);
196f800ab44SSascha Hauer 	ctrl_reg &= ~(0x3 << (22 - (l2irq * 2)));
197f800ab44SSascha Hauer 	ctrl_reg |= (type << (22 - (l2irq * 2)));
198f800ab44SSascha Hauer 	out_be32(&intr->ctrl, ctrl_reg);
199f800ab44SSascha Hauer 
2006b83bd94SThomas Gleixner 	irq_set_handler_locked(d, handler);
2018f2558deSGrant Likely 
202f800ab44SSascha Hauer 	return 0;
203f800ab44SSascha Hauer }
204f800ab44SSascha Hauer 
20539d074b2SGrant Likely static struct irq_chip mpc52xx_extirq_irqchip = {
206b27df672SThomas Gleixner 	.name = "MPC52xx External",
2078a2df7a0SLennert Buytenhek 	.irq_mask = mpc52xx_extirq_mask,
2088a2df7a0SLennert Buytenhek 	.irq_unmask = mpc52xx_extirq_unmask,
2098a2df7a0SLennert Buytenhek 	.irq_ack = mpc52xx_extirq_ack,
2108a2df7a0SLennert Buytenhek 	.irq_set_type = mpc52xx_extirq_set_type,
21139d074b2SGrant Likely };
21239d074b2SGrant Likely 
21339d074b2SGrant Likely /*
21439d074b2SGrant Likely  * Main interrupt irq_chip
21539d074b2SGrant Likely  */
mpc52xx_null_set_type(struct irq_data * d,unsigned int flow_type)2168a2df7a0SLennert Buytenhek static int mpc52xx_null_set_type(struct irq_data *d, unsigned int flow_type)
2178f2558deSGrant Likely {
2188f2558deSGrant Likely 	return 0; /* Do nothing so that the sense mask will get updated */
2198f2558deSGrant Likely }
2208f2558deSGrant Likely 
mpc52xx_main_mask(struct irq_data * d)2218a2df7a0SLennert Buytenhek static void mpc52xx_main_mask(struct irq_data *d)
22239d074b2SGrant Likely {
223476eb491SGrant Likely 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
22422132178SDomen Puncer 	io_be_setbit(&intr->main_mask, 16 - l2irq);
22539d074b2SGrant Likely }
22639d074b2SGrant Likely 
mpc52xx_main_unmask(struct irq_data * d)2278a2df7a0SLennert Buytenhek static void mpc52xx_main_unmask(struct irq_data *d)
22839d074b2SGrant Likely {
229476eb491SGrant Likely 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
23022132178SDomen Puncer 	io_be_clrbit(&intr->main_mask, 16 - l2irq);
23139d074b2SGrant Likely }
23239d074b2SGrant Likely 
23339d074b2SGrant Likely static struct irq_chip mpc52xx_main_irqchip = {
234b27df672SThomas Gleixner 	.name = "MPC52xx Main",
2358a2df7a0SLennert Buytenhek 	.irq_mask = mpc52xx_main_mask,
2368a2df7a0SLennert Buytenhek 	.irq_mask_ack = mpc52xx_main_mask,
2378a2df7a0SLennert Buytenhek 	.irq_unmask = mpc52xx_main_unmask,
2388a2df7a0SLennert Buytenhek 	.irq_set_type = mpc52xx_null_set_type,
23939d074b2SGrant Likely };
24039d074b2SGrant Likely 
24139d074b2SGrant Likely /*
24239d074b2SGrant Likely  * Peripherals interrupt irq_chip
24339d074b2SGrant Likely  */
mpc52xx_periph_mask(struct irq_data * d)2448a2df7a0SLennert Buytenhek static void mpc52xx_periph_mask(struct irq_data *d)
24539d074b2SGrant Likely {
246476eb491SGrant Likely 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
24739d074b2SGrant Likely 	io_be_setbit(&intr->per_mask, 31 - l2irq);
24839d074b2SGrant Likely }
24939d074b2SGrant Likely 
mpc52xx_periph_unmask(struct irq_data * d)2508a2df7a0SLennert Buytenhek static void mpc52xx_periph_unmask(struct irq_data *d)
25139d074b2SGrant Likely {
252476eb491SGrant Likely 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
25339d074b2SGrant Likely 	io_be_clrbit(&intr->per_mask, 31 - l2irq);
25439d074b2SGrant Likely }
25539d074b2SGrant Likely 
25639d074b2SGrant Likely static struct irq_chip mpc52xx_periph_irqchip = {
257b27df672SThomas Gleixner 	.name = "MPC52xx Peripherals",
2588a2df7a0SLennert Buytenhek 	.irq_mask = mpc52xx_periph_mask,
2598a2df7a0SLennert Buytenhek 	.irq_mask_ack = mpc52xx_periph_mask,
2608a2df7a0SLennert Buytenhek 	.irq_unmask = mpc52xx_periph_unmask,
2618a2df7a0SLennert Buytenhek 	.irq_set_type = mpc52xx_null_set_type,
26239d074b2SGrant Likely };
26339d074b2SGrant Likely 
26439d074b2SGrant Likely /*
26539d074b2SGrant Likely  * SDMA interrupt irq_chip
26639d074b2SGrant Likely  */
mpc52xx_sdma_mask(struct irq_data * d)2678a2df7a0SLennert Buytenhek static void mpc52xx_sdma_mask(struct irq_data *d)
26839d074b2SGrant Likely {
269476eb491SGrant Likely 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
27039d074b2SGrant Likely 	io_be_setbit(&sdma->IntMask, l2irq);
27139d074b2SGrant Likely }
27239d074b2SGrant Likely 
mpc52xx_sdma_unmask(struct irq_data * d)2738a2df7a0SLennert Buytenhek static void mpc52xx_sdma_unmask(struct irq_data *d)
27439d074b2SGrant Likely {
275476eb491SGrant Likely 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
27639d074b2SGrant Likely 	io_be_clrbit(&sdma->IntMask, l2irq);
27739d074b2SGrant Likely }
27839d074b2SGrant Likely 
mpc52xx_sdma_ack(struct irq_data * d)2798a2df7a0SLennert Buytenhek static void mpc52xx_sdma_ack(struct irq_data *d)
28039d074b2SGrant Likely {
281476eb491SGrant Likely 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
28239d074b2SGrant Likely 	out_be32(&sdma->IntPend, 1 << l2irq);
28339d074b2SGrant Likely }
28439d074b2SGrant Likely 
28539d074b2SGrant Likely static struct irq_chip mpc52xx_sdma_irqchip = {
286b27df672SThomas Gleixner 	.name = "MPC52xx SDMA",
2878a2df7a0SLennert Buytenhek 	.irq_mask = mpc52xx_sdma_mask,
2888a2df7a0SLennert Buytenhek 	.irq_unmask = mpc52xx_sdma_unmask,
2898a2df7a0SLennert Buytenhek 	.irq_ack = mpc52xx_sdma_ack,
2908a2df7a0SLennert Buytenhek 	.irq_set_type = mpc52xx_null_set_type,
29139d074b2SGrant Likely };
29239d074b2SGrant Likely 
293bcb73f56SGrant Likely /**
2948f2558deSGrant Likely  * mpc52xx_is_extirq - Returns true if hwirq number is for an external IRQ
2958f2558deSGrant Likely  */
mpc52xx_is_extirq(int l1,int l2)2968f2558deSGrant Likely static int mpc52xx_is_extirq(int l1, int l2)
2978f2558deSGrant Likely {
2988f2558deSGrant Likely 	return ((l1 == 0) && (l2 == 0)) ||
2998f2558deSGrant Likely 	       ((l1 == 1) && (l2 >= 1) && (l2 <= 3));
3008f2558deSGrant Likely }
3018f2558deSGrant Likely 
3028f2558deSGrant Likely /**
303bcb73f56SGrant Likely  * mpc52xx_irqhost_xlate - translate virq# from device tree interrupts property
30439d074b2SGrant Likely  */
mpc52xx_irqhost_xlate(struct irq_domain * h,struct device_node * ct,const u32 * intspec,unsigned int intsize,irq_hw_number_t * out_hwirq,unsigned int * out_flags)305bae1d8f1SGrant Likely static int mpc52xx_irqhost_xlate(struct irq_domain *h, struct device_node *ct,
30640d50cf7SRoman Fietze 				 const u32 *intspec, unsigned int intsize,
30739d074b2SGrant Likely 				 irq_hw_number_t *out_hwirq,
30839d074b2SGrant Likely 				 unsigned int *out_flags)
30939d074b2SGrant Likely {
31039d074b2SGrant Likely 	int intrvect_l1;
31139d074b2SGrant Likely 	int intrvect_l2;
31239d074b2SGrant Likely 	int intrvect_type;
31339d074b2SGrant Likely 	int intrvect_linux;
31439d074b2SGrant Likely 
31539d074b2SGrant Likely 	if (intsize != 3)
31639d074b2SGrant Likely 		return -1;
31739d074b2SGrant Likely 
31839d074b2SGrant Likely 	intrvect_l1 = (int)intspec[0];
31939d074b2SGrant Likely 	intrvect_l2 = (int)intspec[1];
3208f2558deSGrant Likely 	intrvect_type = (int)intspec[2] & 0x3;
32139d074b2SGrant Likely 
322bcb73f56SGrant Likely 	intrvect_linux = (intrvect_l1 << MPC52xx_IRQ_L1_OFFSET) &
323bcb73f56SGrant Likely 			 MPC52xx_IRQ_L1_MASK;
324bcb73f56SGrant Likely 	intrvect_linux |= intrvect_l2 & MPC52xx_IRQ_L2_MASK;
32539d074b2SGrant Likely 
32639d074b2SGrant Likely 	*out_hwirq = intrvect_linux;
3278f2558deSGrant Likely 	*out_flags = IRQ_TYPE_LEVEL_LOW;
3288f2558deSGrant Likely 	if (mpc52xx_is_extirq(intrvect_l1, intrvect_l2))
32939d074b2SGrant Likely 		*out_flags = mpc52xx_map_senses[intrvect_type];
33039d074b2SGrant Likely 
3318f2558deSGrant Likely 	pr_debug("return %x, l1=%d, l2=%d\n", intrvect_linux, intrvect_l1,
3328f2558deSGrant Likely 		 intrvect_l2);
33339d074b2SGrant Likely 	return 0;
33439d074b2SGrant Likely }
33539d074b2SGrant Likely 
336bcb73f56SGrant Likely /**
337bcb73f56SGrant Likely  * mpc52xx_irqhost_map - Hook to map from virq to an irq_chip structure
338bcb73f56SGrant Likely  */
mpc52xx_irqhost_map(struct irq_domain * h,unsigned int virq,irq_hw_number_t irq)339bae1d8f1SGrant Likely static int mpc52xx_irqhost_map(struct irq_domain *h, unsigned int virq,
34039d074b2SGrant Likely 			       irq_hw_number_t irq)
34139d074b2SGrant Likely {
34239d074b2SGrant Likely 	int l1irq;
34339d074b2SGrant Likely 	int l2irq;
3443f649ab7SKees Cook 	struct irq_chip *irqchip;
3458f2558deSGrant Likely 	void *hndlr;
34639d074b2SGrant Likely 	int type;
3478f2558deSGrant Likely 	u32 reg;
34839d074b2SGrant Likely 
34939d074b2SGrant Likely 	l1irq = (irq & MPC52xx_IRQ_L1_MASK) >> MPC52xx_IRQ_L1_OFFSET;
350bcb73f56SGrant Likely 	l2irq = irq & MPC52xx_IRQ_L2_MASK;
35139d074b2SGrant Likely 
35239d074b2SGrant Likely 	/*
3538f2558deSGrant Likely 	 * External IRQs are handled differently by the hardware so they are
3548f2558deSGrant Likely 	 * handled by a dedicated irq_chip structure.
35539d074b2SGrant Likely 	 */
3568f2558deSGrant Likely 	if (mpc52xx_is_extirq(l1irq, l2irq)) {
3578f2558deSGrant Likely 		reg = in_be32(&intr->ctrl);
3588f2558deSGrant Likely 		type = mpc52xx_map_senses[(reg >> (22 - l2irq * 2)) & 0x3];
3598f2558deSGrant Likely 		if ((type == IRQ_TYPE_EDGE_FALLING) ||
3608f2558deSGrant Likely 		    (type == IRQ_TYPE_EDGE_RISING))
3618f2558deSGrant Likely 			hndlr = handle_edge_irq;
3628f2558deSGrant Likely 		else
3638f2558deSGrant Likely 			hndlr = handle_level_irq;
36439d074b2SGrant Likely 
365ec775d0eSThomas Gleixner 		irq_set_chip_and_handler(virq, &mpc52xx_extirq_irqchip, hndlr);
3668f2558deSGrant Likely 		pr_debug("%s: External IRQ%i virq=%x, hw=%x. type=%x\n",
3678f2558deSGrant Likely 			 __func__, l2irq, virq, (int)irq, type);
3688f2558deSGrant Likely 		return 0;
36939d074b2SGrant Likely 	}
37039d074b2SGrant Likely 
3718f2558deSGrant Likely 	/* It is an internal SOC irq.  Choose the correct irq_chip */
3728f2558deSGrant Likely 	switch (l1irq) {
3738f2558deSGrant Likely 	case MPC52xx_IRQ_L1_MAIN: irqchip = &mpc52xx_main_irqchip; break;
3748f2558deSGrant Likely 	case MPC52xx_IRQ_L1_PERP: irqchip = &mpc52xx_periph_irqchip; break;
3758f2558deSGrant Likely 	case MPC52xx_IRQ_L1_SDMA: irqchip = &mpc52xx_sdma_irqchip; break;
376e34298c9SWolfram Sang 	case MPC52xx_IRQ_L1_CRIT:
377e34298c9SWolfram Sang 		pr_warn("%s: Critical IRQ #%d is unsupported! Nopping it.\n",
378509a02dfSWolfram Sang 			__func__, l2irq);
379e34298c9SWolfram Sang 		irq_set_chip(virq, &no_irq_chip);
380e34298c9SWolfram Sang 		return 0;
38139d074b2SGrant Likely 	}
38239d074b2SGrant Likely 
383ec775d0eSThomas Gleixner 	irq_set_chip_and_handler(virq, irqchip, handle_level_irq);
3848f2558deSGrant Likely 	pr_debug("%s: virq=%x, l1=%i, l2=%i\n", __func__, virq, l1irq, l2irq);
38539d074b2SGrant Likely 
38639d074b2SGrant Likely 	return 0;
38739d074b2SGrant Likely }
38839d074b2SGrant Likely 
3899f70b8ebSGrant Likely static const struct irq_domain_ops mpc52xx_irqhost_ops = {
39039d074b2SGrant Likely 	.xlate = mpc52xx_irqhost_xlate,
39139d074b2SGrant Likely 	.map = mpc52xx_irqhost_map,
39239d074b2SGrant Likely };
39339d074b2SGrant Likely 
394bcb73f56SGrant Likely /**
395bcb73f56SGrant Likely  * mpc52xx_init_irq - Initialize and register with the virq subsystem
396bcb73f56SGrant Likely  *
397bcb73f56SGrant Likely  * Hook for setting up IRQs on an mpc5200 system.  A pointer to this function
398bcb73f56SGrant Likely  * is to be put into the machine definition structure.
399bcb73f56SGrant Likely  *
400bcb73f56SGrant Likely  * This function searches the device tree for an MPC5200 interrupt controller,
401bcb73f56SGrant Likely  * initializes it, and registers it with the virq subsystem.
40239d074b2SGrant Likely  */
mpc52xx_init_irq(void)40339d074b2SGrant Likely void __init mpc52xx_init_irq(void)
40439d074b2SGrant Likely {
40539d074b2SGrant Likely 	u32 intr_ctrl;
4066065170cSGrant Likely 	struct device_node *picnode;
40775ca399eSGrant Likely 	struct device_node *np;
40839d074b2SGrant Likely 
40939d074b2SGrant Likely 	/* Remap the necessary zones */
41066ffbe49SGrant Likely 	picnode = of_find_matching_node(NULL, mpc52xx_pic_ids);
41175ca399eSGrant Likely 	intr = of_iomap(picnode, 0);
4126065170cSGrant Likely 	if (!intr)
413e3aba81dSGrant Likely 		panic(__FILE__	": find_and_map failed on 'mpc5200-pic'. "
4146065170cSGrant Likely 				"Check node !");
41539d074b2SGrant Likely 
41666ffbe49SGrant Likely 	np = of_find_matching_node(NULL, mpc52xx_sdma_ids);
41775ca399eSGrant Likely 	sdma = of_iomap(np, 0);
41875ca399eSGrant Likely 	of_node_put(np);
4196065170cSGrant Likely 	if (!sdma)
420e3aba81dSGrant Likely 		panic(__FILE__	": find_and_map failed on 'mpc5200-bestcomm'. "
4216065170cSGrant Likely 				"Check node !");
42239d074b2SGrant Likely 
4238f2558deSGrant Likely 	pr_debug("MPC5200 IRQ controller mapped to 0x%p\n", intr);
4248f2558deSGrant Likely 
42539d074b2SGrant Likely 	/* Disable all interrupt sources. */
42639d074b2SGrant Likely 	out_be32(&sdma->IntPend, 0xffffffff);	/* 1 means clear pending */
42739d074b2SGrant Likely 	out_be32(&sdma->IntMask, 0xffffffff);	/* 1 means disabled */
42839d074b2SGrant Likely 	out_be32(&intr->per_mask, 0x7ffffc00);	/* 1 means disabled */
42939d074b2SGrant Likely 	out_be32(&intr->main_mask, 0x00010fff);	/* 1 means disabled */
43039d074b2SGrant Likely 	intr_ctrl = in_be32(&intr->ctrl);
43139d074b2SGrant Likely 	intr_ctrl &= 0x00ff0000;	/* Keeps IRQ[0-3] config */
43239d074b2SGrant Likely 	intr_ctrl |=	0x0f000000 |	/* clear IRQ 0-3 */
43339d074b2SGrant Likely 			0x00001000 |	/* MEE master external enable */
43439d074b2SGrant Likely 			0x00000000 |	/* 0 means disable IRQ 0-3 */
43539d074b2SGrant Likely 			0x00000001;	/* CEb route critical normally */
43639d074b2SGrant Likely 	out_be32(&intr->ctrl, intr_ctrl);
43739d074b2SGrant Likely 
43839d074b2SGrant Likely 	/* Zero a bunch of the priority settings. */
43939d074b2SGrant Likely 	out_be32(&intr->per_pri1, 0);
44039d074b2SGrant Likely 	out_be32(&intr->per_pri2, 0);
44139d074b2SGrant Likely 	out_be32(&intr->per_pri3, 0);
44239d074b2SGrant Likely 	out_be32(&intr->main_pri1, 0);
44339d074b2SGrant Likely 	out_be32(&intr->main_pri2, 0);
44439d074b2SGrant Likely 
44539d074b2SGrant Likely 	/*
44639d074b2SGrant Likely 	 * As last step, add an irq host to translate the real
44739d074b2SGrant Likely 	 * hw irq information provided by the ofw to linux virq
44839d074b2SGrant Likely 	 */
449a8db8cf0SGrant Likely 	mpc52xx_irqhost = irq_domain_add_linear(picnode,
4506065170cSGrant Likely 	                                 MPC52xx_IRQ_HIGHTESTHWIRQ,
451a8db8cf0SGrant Likely 	                                 &mpc52xx_irqhost_ops, NULL);
45239d074b2SGrant Likely 
4536065170cSGrant Likely 	if (!mpc52xx_irqhost)
4546065170cSGrant Likely 		panic(__FILE__ ": Cannot allocate the IRQ host\n");
4556065170cSGrant Likely 
456dd952cbbSGrant Likely 	irq_set_default_host(mpc52xx_irqhost);
457dd952cbbSGrant Likely 
458bcb73f56SGrant Likely 	pr_info("MPC52xx PIC is up and running!\n");
45939d074b2SGrant Likely }
46039d074b2SGrant Likely 
461bcb73f56SGrant Likely /**
462bcb73f56SGrant Likely  * mpc52xx_get_irq - Get pending interrupt number hook function
463bcb73f56SGrant Likely  *
46425985edcSLucas De Marchi  * Called by the interrupt handler to determine what IRQ handler needs to be
465bcb73f56SGrant Likely  * executed.
466bcb73f56SGrant Likely  *
467bcb73f56SGrant Likely  * Status of pending interrupts is determined by reading the encoded status
468bcb73f56SGrant Likely  * register.  The encoded status register has three fields; one for each of the
469bcb73f56SGrant Likely  * types of interrupts defined by the controller - 'critical', 'main' and
470bcb73f56SGrant Likely  * 'peripheral'.  This function reads the status register and returns the IRQ
471bcb73f56SGrant Likely  * number associated with the highest priority pending interrupt.  'Critical'
472bcb73f56SGrant Likely  * interrupts have the highest priority, followed by 'main' interrupts, and
473bcb73f56SGrant Likely  * then 'peripheral'.
474bcb73f56SGrant Likely  *
475bcb73f56SGrant Likely  * The mpc5200 interrupt controller can be configured to boost the priority
476bcb73f56SGrant Likely  * of individual 'peripheral' interrupts.  If this is the case then a special
477bcb73f56SGrant Likely  * value will appear in either the crit or main fields indicating a high
478bcb73f56SGrant Likely  * or medium priority peripheral irq has occurred.
479bcb73f56SGrant Likely  *
480bcb73f56SGrant Likely  * This function checks each of the 3 irq request fields and returns the
481bcb73f56SGrant Likely  * first pending interrupt that it finds.
482bcb73f56SGrant Likely  *
483bcb73f56SGrant Likely  * This function also identifies a 4th type of interrupt; 'bestcomm'.  Each
484bcb73f56SGrant Likely  * bestcomm DMA task can raise the bestcomm peripheral interrupt.  When this
485bcb73f56SGrant Likely  * occurs at task-specific IRQ# is decoded so that each task can have its
486bcb73f56SGrant Likely  * own IRQ handler.
48739d074b2SGrant Likely  */
mpc52xx_get_irq(void)48839d074b2SGrant Likely unsigned int mpc52xx_get_irq(void)
48939d074b2SGrant Likely {
49039d074b2SGrant Likely 	u32 status;
49167347ebaSMilton Miller 	int irq;
49239d074b2SGrant Likely 
49339d074b2SGrant Likely 	status = in_be32(&intr->enc_status);
49439d074b2SGrant Likely 	if (status & 0x00000400) {	/* critical */
49539d074b2SGrant Likely 		irq = (status >> 8) & 0x3;
49639d074b2SGrant Likely 		if (irq == 2)	/* high priority peripheral */
49739d074b2SGrant Likely 			goto peripheral;
498bcb73f56SGrant Likely 		irq |= (MPC52xx_IRQ_L1_CRIT << MPC52xx_IRQ_L1_OFFSET);
49939d074b2SGrant Likely 	} else if (status & 0x00200000) {	/* main */
50039d074b2SGrant Likely 		irq = (status >> 16) & 0x1f;
50139d074b2SGrant Likely 		if (irq == 4)	/* low priority peripheral */
50239d074b2SGrant Likely 			goto peripheral;
503bcb73f56SGrant Likely 		irq |= (MPC52xx_IRQ_L1_MAIN << MPC52xx_IRQ_L1_OFFSET);
50439d074b2SGrant Likely 	} else if (status & 0x20000000) {	/* peripheral */
50539d074b2SGrant Likely 	      peripheral:
50639d074b2SGrant Likely 		irq = (status >> 24) & 0x1f;
50739d074b2SGrant Likely 		if (irq == 0) {	/* bestcomm */
50839d074b2SGrant Likely 			status = in_be32(&sdma->IntPend);
50939d074b2SGrant Likely 			irq = ffs(status) - 1;
510bcb73f56SGrant Likely 			irq |= (MPC52xx_IRQ_L1_SDMA << MPC52xx_IRQ_L1_OFFSET);
5116065170cSGrant Likely 		} else {
512bcb73f56SGrant Likely 			irq |= (MPC52xx_IRQ_L1_PERP << MPC52xx_IRQ_L1_OFFSET);
51339d074b2SGrant Likely 		}
51467347ebaSMilton Miller 	} else {
515ef24ba70SMichael Ellerman 		return 0;
5166065170cSGrant Likely 	}
51739d074b2SGrant Likely 
51839d074b2SGrant Likely 	return irq_linear_revmap(mpc52xx_irqhost, irq);
51939d074b2SGrant Likely }
520