xref: /OK3568_Linux_fs/kernel/arch/powerpc/platforms/52xx/mpc52xx_pic.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *
3*4882a593Smuzhiyun  * Programmable Interrupt Controller functions for the Freescale MPC52xx.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2008 Secret Lab Technologies Ltd.
6*4882a593Smuzhiyun  * Copyright (C) 2006 bplan GmbH
7*4882a593Smuzhiyun  * Copyright (C) 2004 Sylvain Munaut <tnt@246tNt.com>
8*4882a593Smuzhiyun  * Copyright (C) 2003 Montavista Software, Inc
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * Based on the code from the 2.4 kernel by
11*4882a593Smuzhiyun  * Dale Farnsworth <dfarnsworth@mvista.com> and Kent Borg.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * This file is licensed under the terms of the GNU General Public License
14*4882a593Smuzhiyun  * version 2. This program is licensed "as is" without any warranty of any
15*4882a593Smuzhiyun  * kind, whether express or implied.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  */
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun  * This is the device driver for the MPC5200 interrupt controller.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * hardware overview
23*4882a593Smuzhiyun  * -----------------
24*4882a593Smuzhiyun  * The MPC5200 interrupt controller groups the all interrupt sources into
25*4882a593Smuzhiyun  * three groups called 'critical', 'main', and 'peripheral'.  The critical
26*4882a593Smuzhiyun  * group has 3 irqs, External IRQ0, slice timer 0 irq, and wake from deep
27*4882a593Smuzhiyun  * sleep.  Main group include the other 3 external IRQs, slice timer 1, RTC,
28*4882a593Smuzhiyun  * gpios, and the general purpose timers.  Peripheral group contains the
29*4882a593Smuzhiyun  * remaining irq sources from all of the on-chip peripherals (PSCs, Ethernet,
30*4882a593Smuzhiyun  * USB, DMA, etc).
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * virqs
33*4882a593Smuzhiyun  * -----
34*4882a593Smuzhiyun  * The Linux IRQ subsystem requires that each irq source be assigned a
35*4882a593Smuzhiyun  * system wide unique IRQ number starting at 1 (0 means no irq).  Since
36*4882a593Smuzhiyun  * systems can have multiple interrupt controllers, the virtual IRQ (virq)
37*4882a593Smuzhiyun  * infrastructure lets each interrupt controller to define a local set
38*4882a593Smuzhiyun  * of IRQ numbers and the virq infrastructure maps those numbers into
39*4882a593Smuzhiyun  * a unique range of the global IRQ# space.
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  * To define a range of virq numbers for this controller, this driver first
42*4882a593Smuzhiyun  * assigns a number to each of the irq groups (called the level 1 or L1
43*4882a593Smuzhiyun  * value).  Within each group individual irq sources are also assigned a
44*4882a593Smuzhiyun  * number, as defined by the MPC5200 user guide, and refers to it as the
45*4882a593Smuzhiyun  * level 2 or L2 value.  The virq number is determined by shifting up the
46*4882a593Smuzhiyun  * L1 value by MPC52xx_IRQ_L1_OFFSET and ORing it with the L2 value.
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  * For example, the TMR0 interrupt is irq 9 in the main group.  The
49*4882a593Smuzhiyun  * virq for TMR0 is calculated by ((1 << MPC52xx_IRQ_L1_OFFSET) | 9).
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * The observant reader will also notice that this driver defines a 4th
52*4882a593Smuzhiyun  * interrupt group called 'bestcomm'.  The bestcomm group isn't physically
53*4882a593Smuzhiyun  * part of the MPC5200 interrupt controller, but it is used here to assign
54*4882a593Smuzhiyun  * a separate virq number for each bestcomm task (since any of the 16
55*4882a593Smuzhiyun  * bestcomm tasks can cause the bestcomm interrupt to be raised).  When a
56*4882a593Smuzhiyun  * bestcomm interrupt occurs (peripheral group, irq 0) this driver determines
57*4882a593Smuzhiyun  * which task needs servicing and returns the irq number for that task.  This
58*4882a593Smuzhiyun  * allows drivers which use bestcomm to define their own interrupt handlers.
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * irq_chip structures
61*4882a593Smuzhiyun  * -------------------
62*4882a593Smuzhiyun  * For actually manipulating IRQs (masking, enabling, clearing, etc) this
63*4882a593Smuzhiyun  * driver defines four separate 'irq_chip' structures, one for the main
64*4882a593Smuzhiyun  * group, one for the peripherals group, one for the bestcomm group and one
65*4882a593Smuzhiyun  * for external interrupts.  The irq_chip structures provide the hooks needed
66*4882a593Smuzhiyun  * to manipulate each IRQ source, and since each group is has a separate set
67*4882a593Smuzhiyun  * of registers for controlling the irq, it makes sense to divide up the
68*4882a593Smuzhiyun  * hooks along those lines.
69*4882a593Smuzhiyun  *
70*4882a593Smuzhiyun  * You'll notice that there is not an irq_chip for the critical group and
71*4882a593Smuzhiyun  * you'll also notice that there is an irq_chip defined for external
72*4882a593Smuzhiyun  * interrupts even though there is no external interrupt group.  The reason
73*4882a593Smuzhiyun  * for this is that the four external interrupts are all managed with the same
74*4882a593Smuzhiyun  * register even though one of the external IRQs is in the critical group and
75*4882a593Smuzhiyun  * the other three are in the main group.  For this reason it makes sense for
76*4882a593Smuzhiyun  * the 4 external irqs to be managed using a separate set of hooks.  The
77*4882a593Smuzhiyun  * reason there is no crit irq_chip is that of the 3 irqs in the critical
78*4882a593Smuzhiyun  * group, only external interrupt is actually support at this time by this
79*4882a593Smuzhiyun  * driver and since external interrupt is the only one used, it can just
80*4882a593Smuzhiyun  * be directed to make use of the external irq irq_chip.
81*4882a593Smuzhiyun  *
82*4882a593Smuzhiyun  * device tree bindings
83*4882a593Smuzhiyun  * --------------------
84*4882a593Smuzhiyun  * The device tree bindings for this controller reflect the two level
85*4882a593Smuzhiyun  * organization of irqs in the device.  #interrupt-cells = <3> where the
86*4882a593Smuzhiyun  * first cell is the group number [0..3], the second cell is the irq
87*4882a593Smuzhiyun  * number in the group, and the third cell is the sense type (level/edge).
88*4882a593Smuzhiyun  * For reference, the following is a list of the interrupt property values
89*4882a593Smuzhiyun  * associated with external interrupt sources on the MPC5200 (just because
90*4882a593Smuzhiyun  * it is non-obvious to determine what the interrupts property should be
91*4882a593Smuzhiyun  * when reading the mpc5200 manual and it is a frequently asked question).
92*4882a593Smuzhiyun  *
93*4882a593Smuzhiyun  * External interrupts:
94*4882a593Smuzhiyun  * <0 0 n>	external irq0, n is sense	(n=0: level high,
95*4882a593Smuzhiyun  * <1 1 n>	external irq1, n is sense	 n=1: edge rising,
96*4882a593Smuzhiyun  * <1 2 n>	external irq2, n is sense	 n=2: edge falling,
97*4882a593Smuzhiyun  * <1 3 n>	external irq3, n is sense	 n=3: level low)
98*4882a593Smuzhiyun  */
99*4882a593Smuzhiyun #undef DEBUG
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun #include <linux/interrupt.h>
102*4882a593Smuzhiyun #include <linux/irq.h>
103*4882a593Smuzhiyun #include <linux/of.h>
104*4882a593Smuzhiyun #include <asm/io.h>
105*4882a593Smuzhiyun #include <asm/prom.h>
106*4882a593Smuzhiyun #include <asm/mpc52xx.h>
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun /* HW IRQ mapping */
109*4882a593Smuzhiyun #define MPC52xx_IRQ_L1_CRIT	(0)
110*4882a593Smuzhiyun #define MPC52xx_IRQ_L1_MAIN	(1)
111*4882a593Smuzhiyun #define MPC52xx_IRQ_L1_PERP	(2)
112*4882a593Smuzhiyun #define MPC52xx_IRQ_L1_SDMA	(3)
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun #define MPC52xx_IRQ_L1_OFFSET	(6)
115*4882a593Smuzhiyun #define MPC52xx_IRQ_L1_MASK	(0x00c0)
116*4882a593Smuzhiyun #define MPC52xx_IRQ_L2_MASK	(0x003f)
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun #define MPC52xx_IRQ_HIGHTESTHWIRQ (0xd0)
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun /* MPC5200 device tree match tables */
122*4882a593Smuzhiyun static const struct of_device_id mpc52xx_pic_ids[] __initconst = {
123*4882a593Smuzhiyun 	{ .compatible = "fsl,mpc5200-pic", },
124*4882a593Smuzhiyun 	{ .compatible = "mpc5200-pic", },
125*4882a593Smuzhiyun 	{}
126*4882a593Smuzhiyun };
127*4882a593Smuzhiyun static const struct of_device_id mpc52xx_sdma_ids[] __initconst = {
128*4882a593Smuzhiyun 	{ .compatible = "fsl,mpc5200-bestcomm", },
129*4882a593Smuzhiyun 	{ .compatible = "mpc5200-bestcomm", },
130*4882a593Smuzhiyun 	{}
131*4882a593Smuzhiyun };
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun static struct mpc52xx_intr __iomem *intr;
134*4882a593Smuzhiyun static struct mpc52xx_sdma __iomem *sdma;
135*4882a593Smuzhiyun static struct irq_domain *mpc52xx_irqhost = NULL;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun static unsigned char mpc52xx_map_senses[4] = {
138*4882a593Smuzhiyun 	IRQ_TYPE_LEVEL_HIGH,
139*4882a593Smuzhiyun 	IRQ_TYPE_EDGE_RISING,
140*4882a593Smuzhiyun 	IRQ_TYPE_EDGE_FALLING,
141*4882a593Smuzhiyun 	IRQ_TYPE_LEVEL_LOW,
142*4882a593Smuzhiyun };
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun /* Utility functions */
io_be_setbit(u32 __iomem * addr,int bitno)145*4882a593Smuzhiyun static inline void io_be_setbit(u32 __iomem *addr, int bitno)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	out_be32(addr, in_be32(addr) | (1 << bitno));
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
io_be_clrbit(u32 __iomem * addr,int bitno)150*4882a593Smuzhiyun static inline void io_be_clrbit(u32 __iomem *addr, int bitno)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	out_be32(addr, in_be32(addr) & ~(1 << bitno));
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun /*
156*4882a593Smuzhiyun  * IRQ[0-3] interrupt irq_chip
157*4882a593Smuzhiyun  */
mpc52xx_extirq_mask(struct irq_data * d)158*4882a593Smuzhiyun static void mpc52xx_extirq_mask(struct irq_data *d)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
161*4882a593Smuzhiyun 	io_be_clrbit(&intr->ctrl, 11 - l2irq);
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
mpc52xx_extirq_unmask(struct irq_data * d)164*4882a593Smuzhiyun static void mpc52xx_extirq_unmask(struct irq_data *d)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
167*4882a593Smuzhiyun 	io_be_setbit(&intr->ctrl, 11 - l2irq);
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun 
mpc52xx_extirq_ack(struct irq_data * d)170*4882a593Smuzhiyun static void mpc52xx_extirq_ack(struct irq_data *d)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
173*4882a593Smuzhiyun 	io_be_setbit(&intr->ctrl, 27-l2irq);
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
mpc52xx_extirq_set_type(struct irq_data * d,unsigned int flow_type)176*4882a593Smuzhiyun static int mpc52xx_extirq_set_type(struct irq_data *d, unsigned int flow_type)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun 	u32 ctrl_reg, type;
179*4882a593Smuzhiyun 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
180*4882a593Smuzhiyun 	void *handler = handle_level_irq;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	pr_debug("%s: irq=%x. l2=%d flow_type=%d\n", __func__,
183*4882a593Smuzhiyun 		(int) irqd_to_hwirq(d), l2irq, flow_type);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	switch (flow_type) {
186*4882a593Smuzhiyun 	case IRQF_TRIGGER_HIGH: type = 0; break;
187*4882a593Smuzhiyun 	case IRQF_TRIGGER_RISING: type = 1; handler = handle_edge_irq; break;
188*4882a593Smuzhiyun 	case IRQF_TRIGGER_FALLING: type = 2; handler = handle_edge_irq; break;
189*4882a593Smuzhiyun 	case IRQF_TRIGGER_LOW: type = 3; break;
190*4882a593Smuzhiyun 	default:
191*4882a593Smuzhiyun 		type = 0;
192*4882a593Smuzhiyun 	}
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	ctrl_reg = in_be32(&intr->ctrl);
195*4882a593Smuzhiyun 	ctrl_reg &= ~(0x3 << (22 - (l2irq * 2)));
196*4882a593Smuzhiyun 	ctrl_reg |= (type << (22 - (l2irq * 2)));
197*4882a593Smuzhiyun 	out_be32(&intr->ctrl, ctrl_reg);
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	irq_set_handler_locked(d, handler);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	return 0;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun static struct irq_chip mpc52xx_extirq_irqchip = {
205*4882a593Smuzhiyun 	.name = "MPC52xx External",
206*4882a593Smuzhiyun 	.irq_mask = mpc52xx_extirq_mask,
207*4882a593Smuzhiyun 	.irq_unmask = mpc52xx_extirq_unmask,
208*4882a593Smuzhiyun 	.irq_ack = mpc52xx_extirq_ack,
209*4882a593Smuzhiyun 	.irq_set_type = mpc52xx_extirq_set_type,
210*4882a593Smuzhiyun };
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun /*
213*4882a593Smuzhiyun  * Main interrupt irq_chip
214*4882a593Smuzhiyun  */
mpc52xx_null_set_type(struct irq_data * d,unsigned int flow_type)215*4882a593Smuzhiyun static int mpc52xx_null_set_type(struct irq_data *d, unsigned int flow_type)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun 	return 0; /* Do nothing so that the sense mask will get updated */
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun 
mpc52xx_main_mask(struct irq_data * d)220*4882a593Smuzhiyun static void mpc52xx_main_mask(struct irq_data *d)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
223*4882a593Smuzhiyun 	io_be_setbit(&intr->main_mask, 16 - l2irq);
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
mpc52xx_main_unmask(struct irq_data * d)226*4882a593Smuzhiyun static void mpc52xx_main_unmask(struct irq_data *d)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
229*4882a593Smuzhiyun 	io_be_clrbit(&intr->main_mask, 16 - l2irq);
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun static struct irq_chip mpc52xx_main_irqchip = {
233*4882a593Smuzhiyun 	.name = "MPC52xx Main",
234*4882a593Smuzhiyun 	.irq_mask = mpc52xx_main_mask,
235*4882a593Smuzhiyun 	.irq_mask_ack = mpc52xx_main_mask,
236*4882a593Smuzhiyun 	.irq_unmask = mpc52xx_main_unmask,
237*4882a593Smuzhiyun 	.irq_set_type = mpc52xx_null_set_type,
238*4882a593Smuzhiyun };
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun /*
241*4882a593Smuzhiyun  * Peripherals interrupt irq_chip
242*4882a593Smuzhiyun  */
mpc52xx_periph_mask(struct irq_data * d)243*4882a593Smuzhiyun static void mpc52xx_periph_mask(struct irq_data *d)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
246*4882a593Smuzhiyun 	io_be_setbit(&intr->per_mask, 31 - l2irq);
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun 
mpc52xx_periph_unmask(struct irq_data * d)249*4882a593Smuzhiyun static void mpc52xx_periph_unmask(struct irq_data *d)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
252*4882a593Smuzhiyun 	io_be_clrbit(&intr->per_mask, 31 - l2irq);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun static struct irq_chip mpc52xx_periph_irqchip = {
256*4882a593Smuzhiyun 	.name = "MPC52xx Peripherals",
257*4882a593Smuzhiyun 	.irq_mask = mpc52xx_periph_mask,
258*4882a593Smuzhiyun 	.irq_mask_ack = mpc52xx_periph_mask,
259*4882a593Smuzhiyun 	.irq_unmask = mpc52xx_periph_unmask,
260*4882a593Smuzhiyun 	.irq_set_type = mpc52xx_null_set_type,
261*4882a593Smuzhiyun };
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun /*
264*4882a593Smuzhiyun  * SDMA interrupt irq_chip
265*4882a593Smuzhiyun  */
mpc52xx_sdma_mask(struct irq_data * d)266*4882a593Smuzhiyun static void mpc52xx_sdma_mask(struct irq_data *d)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
269*4882a593Smuzhiyun 	io_be_setbit(&sdma->IntMask, l2irq);
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun 
mpc52xx_sdma_unmask(struct irq_data * d)272*4882a593Smuzhiyun static void mpc52xx_sdma_unmask(struct irq_data *d)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
275*4882a593Smuzhiyun 	io_be_clrbit(&sdma->IntMask, l2irq);
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun 
mpc52xx_sdma_ack(struct irq_data * d)278*4882a593Smuzhiyun static void mpc52xx_sdma_ack(struct irq_data *d)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun 	int l2irq = irqd_to_hwirq(d) & MPC52xx_IRQ_L2_MASK;
281*4882a593Smuzhiyun 	out_be32(&sdma->IntPend, 1 << l2irq);
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun static struct irq_chip mpc52xx_sdma_irqchip = {
285*4882a593Smuzhiyun 	.name = "MPC52xx SDMA",
286*4882a593Smuzhiyun 	.irq_mask = mpc52xx_sdma_mask,
287*4882a593Smuzhiyun 	.irq_unmask = mpc52xx_sdma_unmask,
288*4882a593Smuzhiyun 	.irq_ack = mpc52xx_sdma_ack,
289*4882a593Smuzhiyun 	.irq_set_type = mpc52xx_null_set_type,
290*4882a593Smuzhiyun };
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun /**
293*4882a593Smuzhiyun  * mpc52xx_is_extirq - Returns true if hwirq number is for an external IRQ
294*4882a593Smuzhiyun  */
mpc52xx_is_extirq(int l1,int l2)295*4882a593Smuzhiyun static int mpc52xx_is_extirq(int l1, int l2)
296*4882a593Smuzhiyun {
297*4882a593Smuzhiyun 	return ((l1 == 0) && (l2 == 0)) ||
298*4882a593Smuzhiyun 	       ((l1 == 1) && (l2 >= 1) && (l2 <= 3));
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun /**
302*4882a593Smuzhiyun  * mpc52xx_irqhost_xlate - translate virq# from device tree interrupts property
303*4882a593Smuzhiyun  */
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)304*4882a593Smuzhiyun static int mpc52xx_irqhost_xlate(struct irq_domain *h, struct device_node *ct,
305*4882a593Smuzhiyun 				 const u32 *intspec, unsigned int intsize,
306*4882a593Smuzhiyun 				 irq_hw_number_t *out_hwirq,
307*4882a593Smuzhiyun 				 unsigned int *out_flags)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun 	int intrvect_l1;
310*4882a593Smuzhiyun 	int intrvect_l2;
311*4882a593Smuzhiyun 	int intrvect_type;
312*4882a593Smuzhiyun 	int intrvect_linux;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	if (intsize != 3)
315*4882a593Smuzhiyun 		return -1;
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	intrvect_l1 = (int)intspec[0];
318*4882a593Smuzhiyun 	intrvect_l2 = (int)intspec[1];
319*4882a593Smuzhiyun 	intrvect_type = (int)intspec[2] & 0x3;
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	intrvect_linux = (intrvect_l1 << MPC52xx_IRQ_L1_OFFSET) &
322*4882a593Smuzhiyun 			 MPC52xx_IRQ_L1_MASK;
323*4882a593Smuzhiyun 	intrvect_linux |= intrvect_l2 & MPC52xx_IRQ_L2_MASK;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	*out_hwirq = intrvect_linux;
326*4882a593Smuzhiyun 	*out_flags = IRQ_TYPE_LEVEL_LOW;
327*4882a593Smuzhiyun 	if (mpc52xx_is_extirq(intrvect_l1, intrvect_l2))
328*4882a593Smuzhiyun 		*out_flags = mpc52xx_map_senses[intrvect_type];
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	pr_debug("return %x, l1=%d, l2=%d\n", intrvect_linux, intrvect_l1,
331*4882a593Smuzhiyun 		 intrvect_l2);
332*4882a593Smuzhiyun 	return 0;
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun /**
336*4882a593Smuzhiyun  * mpc52xx_irqhost_map - Hook to map from virq to an irq_chip structure
337*4882a593Smuzhiyun  */
mpc52xx_irqhost_map(struct irq_domain * h,unsigned int virq,irq_hw_number_t irq)338*4882a593Smuzhiyun static int mpc52xx_irqhost_map(struct irq_domain *h, unsigned int virq,
339*4882a593Smuzhiyun 			       irq_hw_number_t irq)
340*4882a593Smuzhiyun {
341*4882a593Smuzhiyun 	int l1irq;
342*4882a593Smuzhiyun 	int l2irq;
343*4882a593Smuzhiyun 	struct irq_chip *irqchip;
344*4882a593Smuzhiyun 	void *hndlr;
345*4882a593Smuzhiyun 	int type;
346*4882a593Smuzhiyun 	u32 reg;
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	l1irq = (irq & MPC52xx_IRQ_L1_MASK) >> MPC52xx_IRQ_L1_OFFSET;
349*4882a593Smuzhiyun 	l2irq = irq & MPC52xx_IRQ_L2_MASK;
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	/*
352*4882a593Smuzhiyun 	 * External IRQs are handled differently by the hardware so they are
353*4882a593Smuzhiyun 	 * handled by a dedicated irq_chip structure.
354*4882a593Smuzhiyun 	 */
355*4882a593Smuzhiyun 	if (mpc52xx_is_extirq(l1irq, l2irq)) {
356*4882a593Smuzhiyun 		reg = in_be32(&intr->ctrl);
357*4882a593Smuzhiyun 		type = mpc52xx_map_senses[(reg >> (22 - l2irq * 2)) & 0x3];
358*4882a593Smuzhiyun 		if ((type == IRQ_TYPE_EDGE_FALLING) ||
359*4882a593Smuzhiyun 		    (type == IRQ_TYPE_EDGE_RISING))
360*4882a593Smuzhiyun 			hndlr = handle_edge_irq;
361*4882a593Smuzhiyun 		else
362*4882a593Smuzhiyun 			hndlr = handle_level_irq;
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 		irq_set_chip_and_handler(virq, &mpc52xx_extirq_irqchip, hndlr);
365*4882a593Smuzhiyun 		pr_debug("%s: External IRQ%i virq=%x, hw=%x. type=%x\n",
366*4882a593Smuzhiyun 			 __func__, l2irq, virq, (int)irq, type);
367*4882a593Smuzhiyun 		return 0;
368*4882a593Smuzhiyun 	}
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	/* It is an internal SOC irq.  Choose the correct irq_chip */
371*4882a593Smuzhiyun 	switch (l1irq) {
372*4882a593Smuzhiyun 	case MPC52xx_IRQ_L1_MAIN: irqchip = &mpc52xx_main_irqchip; break;
373*4882a593Smuzhiyun 	case MPC52xx_IRQ_L1_PERP: irqchip = &mpc52xx_periph_irqchip; break;
374*4882a593Smuzhiyun 	case MPC52xx_IRQ_L1_SDMA: irqchip = &mpc52xx_sdma_irqchip; break;
375*4882a593Smuzhiyun 	case MPC52xx_IRQ_L1_CRIT:
376*4882a593Smuzhiyun 		pr_warn("%s: Critical IRQ #%d is unsupported! Nopping it.\n",
377*4882a593Smuzhiyun 			__func__, l2irq);
378*4882a593Smuzhiyun 		irq_set_chip(virq, &no_irq_chip);
379*4882a593Smuzhiyun 		return 0;
380*4882a593Smuzhiyun 	}
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	irq_set_chip_and_handler(virq, irqchip, handle_level_irq);
383*4882a593Smuzhiyun 	pr_debug("%s: virq=%x, l1=%i, l2=%i\n", __func__, virq, l1irq, l2irq);
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	return 0;
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun static const struct irq_domain_ops mpc52xx_irqhost_ops = {
389*4882a593Smuzhiyun 	.xlate = mpc52xx_irqhost_xlate,
390*4882a593Smuzhiyun 	.map = mpc52xx_irqhost_map,
391*4882a593Smuzhiyun };
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun /**
394*4882a593Smuzhiyun  * mpc52xx_init_irq - Initialize and register with the virq subsystem
395*4882a593Smuzhiyun  *
396*4882a593Smuzhiyun  * Hook for setting up IRQs on an mpc5200 system.  A pointer to this function
397*4882a593Smuzhiyun  * is to be put into the machine definition structure.
398*4882a593Smuzhiyun  *
399*4882a593Smuzhiyun  * This function searches the device tree for an MPC5200 interrupt controller,
400*4882a593Smuzhiyun  * initializes it, and registers it with the virq subsystem.
401*4882a593Smuzhiyun  */
mpc52xx_init_irq(void)402*4882a593Smuzhiyun void __init mpc52xx_init_irq(void)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun 	u32 intr_ctrl;
405*4882a593Smuzhiyun 	struct device_node *picnode;
406*4882a593Smuzhiyun 	struct device_node *np;
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	/* Remap the necessary zones */
409*4882a593Smuzhiyun 	picnode = of_find_matching_node(NULL, mpc52xx_pic_ids);
410*4882a593Smuzhiyun 	intr = of_iomap(picnode, 0);
411*4882a593Smuzhiyun 	if (!intr)
412*4882a593Smuzhiyun 		panic(__FILE__	": find_and_map failed on 'mpc5200-pic'. "
413*4882a593Smuzhiyun 				"Check node !");
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	np = of_find_matching_node(NULL, mpc52xx_sdma_ids);
416*4882a593Smuzhiyun 	sdma = of_iomap(np, 0);
417*4882a593Smuzhiyun 	of_node_put(np);
418*4882a593Smuzhiyun 	if (!sdma)
419*4882a593Smuzhiyun 		panic(__FILE__	": find_and_map failed on 'mpc5200-bestcomm'. "
420*4882a593Smuzhiyun 				"Check node !");
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	pr_debug("MPC5200 IRQ controller mapped to 0x%p\n", intr);
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	/* Disable all interrupt sources. */
425*4882a593Smuzhiyun 	out_be32(&sdma->IntPend, 0xffffffff);	/* 1 means clear pending */
426*4882a593Smuzhiyun 	out_be32(&sdma->IntMask, 0xffffffff);	/* 1 means disabled */
427*4882a593Smuzhiyun 	out_be32(&intr->per_mask, 0x7ffffc00);	/* 1 means disabled */
428*4882a593Smuzhiyun 	out_be32(&intr->main_mask, 0x00010fff);	/* 1 means disabled */
429*4882a593Smuzhiyun 	intr_ctrl = in_be32(&intr->ctrl);
430*4882a593Smuzhiyun 	intr_ctrl &= 0x00ff0000;	/* Keeps IRQ[0-3] config */
431*4882a593Smuzhiyun 	intr_ctrl |=	0x0f000000 |	/* clear IRQ 0-3 */
432*4882a593Smuzhiyun 			0x00001000 |	/* MEE master external enable */
433*4882a593Smuzhiyun 			0x00000000 |	/* 0 means disable IRQ 0-3 */
434*4882a593Smuzhiyun 			0x00000001;	/* CEb route critical normally */
435*4882a593Smuzhiyun 	out_be32(&intr->ctrl, intr_ctrl);
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	/* Zero a bunch of the priority settings. */
438*4882a593Smuzhiyun 	out_be32(&intr->per_pri1, 0);
439*4882a593Smuzhiyun 	out_be32(&intr->per_pri2, 0);
440*4882a593Smuzhiyun 	out_be32(&intr->per_pri3, 0);
441*4882a593Smuzhiyun 	out_be32(&intr->main_pri1, 0);
442*4882a593Smuzhiyun 	out_be32(&intr->main_pri2, 0);
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	/*
445*4882a593Smuzhiyun 	 * As last step, add an irq host to translate the real
446*4882a593Smuzhiyun 	 * hw irq information provided by the ofw to linux virq
447*4882a593Smuzhiyun 	 */
448*4882a593Smuzhiyun 	mpc52xx_irqhost = irq_domain_add_linear(picnode,
449*4882a593Smuzhiyun 	                                 MPC52xx_IRQ_HIGHTESTHWIRQ,
450*4882a593Smuzhiyun 	                                 &mpc52xx_irqhost_ops, NULL);
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	if (!mpc52xx_irqhost)
453*4882a593Smuzhiyun 		panic(__FILE__ ": Cannot allocate the IRQ host\n");
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	irq_set_default_host(mpc52xx_irqhost);
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	pr_info("MPC52xx PIC is up and running!\n");
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun /**
461*4882a593Smuzhiyun  * mpc52xx_get_irq - Get pending interrupt number hook function
462*4882a593Smuzhiyun  *
463*4882a593Smuzhiyun  * Called by the interrupt handler to determine what IRQ handler needs to be
464*4882a593Smuzhiyun  * executed.
465*4882a593Smuzhiyun  *
466*4882a593Smuzhiyun  * Status of pending interrupts is determined by reading the encoded status
467*4882a593Smuzhiyun  * register.  The encoded status register has three fields; one for each of the
468*4882a593Smuzhiyun  * types of interrupts defined by the controller - 'critical', 'main' and
469*4882a593Smuzhiyun  * 'peripheral'.  This function reads the status register and returns the IRQ
470*4882a593Smuzhiyun  * number associated with the highest priority pending interrupt.  'Critical'
471*4882a593Smuzhiyun  * interrupts have the highest priority, followed by 'main' interrupts, and
472*4882a593Smuzhiyun  * then 'peripheral'.
473*4882a593Smuzhiyun  *
474*4882a593Smuzhiyun  * The mpc5200 interrupt controller can be configured to boost the priority
475*4882a593Smuzhiyun  * of individual 'peripheral' interrupts.  If this is the case then a special
476*4882a593Smuzhiyun  * value will appear in either the crit or main fields indicating a high
477*4882a593Smuzhiyun  * or medium priority peripheral irq has occurred.
478*4882a593Smuzhiyun  *
479*4882a593Smuzhiyun  * This function checks each of the 3 irq request fields and returns the
480*4882a593Smuzhiyun  * first pending interrupt that it finds.
481*4882a593Smuzhiyun  *
482*4882a593Smuzhiyun  * This function also identifies a 4th type of interrupt; 'bestcomm'.  Each
483*4882a593Smuzhiyun  * bestcomm DMA task can raise the bestcomm peripheral interrupt.  When this
484*4882a593Smuzhiyun  * occurs at task-specific IRQ# is decoded so that each task can have its
485*4882a593Smuzhiyun  * own IRQ handler.
486*4882a593Smuzhiyun  */
mpc52xx_get_irq(void)487*4882a593Smuzhiyun unsigned int mpc52xx_get_irq(void)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun 	u32 status;
490*4882a593Smuzhiyun 	int irq;
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	status = in_be32(&intr->enc_status);
493*4882a593Smuzhiyun 	if (status & 0x00000400) {	/* critical */
494*4882a593Smuzhiyun 		irq = (status >> 8) & 0x3;
495*4882a593Smuzhiyun 		if (irq == 2)	/* high priority peripheral */
496*4882a593Smuzhiyun 			goto peripheral;
497*4882a593Smuzhiyun 		irq |= (MPC52xx_IRQ_L1_CRIT << MPC52xx_IRQ_L1_OFFSET);
498*4882a593Smuzhiyun 	} else if (status & 0x00200000) {	/* main */
499*4882a593Smuzhiyun 		irq = (status >> 16) & 0x1f;
500*4882a593Smuzhiyun 		if (irq == 4)	/* low priority peripheral */
501*4882a593Smuzhiyun 			goto peripheral;
502*4882a593Smuzhiyun 		irq |= (MPC52xx_IRQ_L1_MAIN << MPC52xx_IRQ_L1_OFFSET);
503*4882a593Smuzhiyun 	} else if (status & 0x20000000) {	/* peripheral */
504*4882a593Smuzhiyun 	      peripheral:
505*4882a593Smuzhiyun 		irq = (status >> 24) & 0x1f;
506*4882a593Smuzhiyun 		if (irq == 0) {	/* bestcomm */
507*4882a593Smuzhiyun 			status = in_be32(&sdma->IntPend);
508*4882a593Smuzhiyun 			irq = ffs(status) - 1;
509*4882a593Smuzhiyun 			irq |= (MPC52xx_IRQ_L1_SDMA << MPC52xx_IRQ_L1_OFFSET);
510*4882a593Smuzhiyun 		} else {
511*4882a593Smuzhiyun 			irq |= (MPC52xx_IRQ_L1_PERP << MPC52xx_IRQ_L1_OFFSET);
512*4882a593Smuzhiyun 		}
513*4882a593Smuzhiyun 	} else {
514*4882a593Smuzhiyun 		return 0;
515*4882a593Smuzhiyun 	}
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	return irq_linear_revmap(mpc52xx_irqhost, irq);
518*4882a593Smuzhiyun }
519