3 * Programmable Interrupt Controller functions for the Freescale MPC52xx.
5 * Copyright (C) 2006 bplan GmbH
7 * Based on the code from the 2.4 kernel by
8 * Dale Farnsworth <dfarnsworth@mvista.com> and Kent Borg.
10 * Copyright (C) 2004 Sylvain Munaut <tnt@246tNt.com>
11 * Copyright (C) 2003 Montavista Software, Inc
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
21 #include <linux/stddef.h>
22 #include <linux/init.h>
23 #include <linux/sched.h>
24 #include <linux/signal.h>
25 #include <linux/stddef.h>
26 #include <linux/delay.h>
27 #include <linux/irq.h>
28 #include <linux/hardirq.h>
31 #include <asm/processor.h>
32 #include <asm/system.h>
35 #include <asm/mpc52xx.h>
36 #include "mpc52xx_pic.h"
42 static struct mpc52xx_intr __iomem *intr;
43 static struct mpc52xx_sdma __iomem *sdma;
44 static struct irq_host *mpc52xx_irqhost = NULL;
46 static unsigned char mpc52xx_map_senses[4] = {
49 IRQ_TYPE_EDGE_FALLING,
57 static inline void io_be_setbit(u32 __iomem *addr, int bitno)
59 out_be32(addr, in_be32(addr) | (1 << bitno));
62 static inline void io_be_clrbit(u32 __iomem *addr, int bitno)
64 out_be32(addr, in_be32(addr) & ~(1 << bitno));
68 * IRQ[0-3] interrupt irq_chip
71 static void mpc52xx_extirq_mask(unsigned int virq)
76 irq = irq_map[virq].hwirq;
77 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
79 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
81 io_be_clrbit(&intr->ctrl, 11 - l2irq);
84 static void mpc52xx_extirq_unmask(unsigned int virq)
89 irq = irq_map[virq].hwirq;
90 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
92 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
94 io_be_setbit(&intr->ctrl, 11 - l2irq);
97 static void mpc52xx_extirq_ack(unsigned int virq)
102 irq = irq_map[virq].hwirq;
103 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
105 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
107 io_be_setbit(&intr->ctrl, 27-l2irq);
110 static struct irq_chip mpc52xx_extirq_irqchip = {
111 .typename = " MPC52xx IRQ[0-3] ",
112 .mask = mpc52xx_extirq_mask,
113 .unmask = mpc52xx_extirq_unmask,
114 .ack = mpc52xx_extirq_ack,
118 * Main interrupt irq_chip
121 static void mpc52xx_main_mask(unsigned int virq)
126 irq = irq_map[virq].hwirq;
127 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
129 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
131 io_be_setbit(&intr->main_mask, 16 - l2irq);
134 static void mpc52xx_main_unmask(unsigned int virq)
139 irq = irq_map[virq].hwirq;
140 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
142 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
144 io_be_clrbit(&intr->main_mask, 16 - l2irq);
147 static struct irq_chip mpc52xx_main_irqchip = {
148 .typename = "MPC52xx Main",
149 .mask = mpc52xx_main_mask,
150 .mask_ack = mpc52xx_main_mask,
151 .unmask = mpc52xx_main_unmask,
155 * Peripherals interrupt irq_chip
158 static void mpc52xx_periph_mask(unsigned int virq)
163 irq = irq_map[virq].hwirq;
164 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
166 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
168 io_be_setbit(&intr->per_mask, 31 - l2irq);
171 static void mpc52xx_periph_unmask(unsigned int virq)
176 irq = irq_map[virq].hwirq;
177 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
179 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
181 io_be_clrbit(&intr->per_mask, 31 - l2irq);
184 static struct irq_chip mpc52xx_periph_irqchip = {
185 .typename = "MPC52xx Peripherals",
186 .mask = mpc52xx_periph_mask,
187 .mask_ack = mpc52xx_periph_mask,
188 .unmask = mpc52xx_periph_unmask,
192 * SDMA interrupt irq_chip
195 static void mpc52xx_sdma_mask(unsigned int virq)
200 irq = irq_map[virq].hwirq;
201 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
203 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
205 io_be_setbit(&sdma->IntMask, l2irq);
208 static void mpc52xx_sdma_unmask(unsigned int virq)
213 irq = irq_map[virq].hwirq;
214 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
216 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
218 io_be_clrbit(&sdma->IntMask, l2irq);
221 static void mpc52xx_sdma_ack(unsigned int virq)
226 irq = irq_map[virq].hwirq;
227 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
229 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
231 out_be32(&sdma->IntPend, 1 << l2irq);
234 static struct irq_chip mpc52xx_sdma_irqchip = {
235 .typename = "MPC52xx SDMA",
236 .mask = mpc52xx_sdma_mask,
237 .unmask = mpc52xx_sdma_unmask,
238 .ack = mpc52xx_sdma_ack,
245 static int mpc52xx_irqhost_match(struct irq_host *h, struct device_node *node)
247 pr_debug("%s: node=%p\n", __func__, node);
248 return mpc52xx_irqhost->host_data == node;
251 static int mpc52xx_irqhost_xlate(struct irq_host *h, struct device_node *ct,
252 u32 * intspec, unsigned int intsize,
253 irq_hw_number_t * out_hwirq,
254 unsigned int *out_flags)
264 intrvect_l1 = (int)intspec[0];
265 intrvect_l2 = (int)intspec[1];
266 intrvect_type = (int)intspec[2];
269 (intrvect_l1 << MPC52xx_IRQ_L1_OFFSET) & MPC52xx_IRQ_L1_MASK;
271 (intrvect_l2 << MPC52xx_IRQ_L2_OFFSET) & MPC52xx_IRQ_L2_MASK;
273 pr_debug("return %x, l1=%d, l2=%d\n", intrvect_linux, intrvect_l1,
276 *out_hwirq = intrvect_linux;
277 *out_flags = mpc52xx_map_senses[intrvect_type];
283 * this function retrieves the correct IRQ type out
285 * Only externals IRQs needs this
287 static int mpc52xx_irqx_gettype(int irq)
292 ctrl_reg = in_be32(&intr->ctrl);
293 type = (ctrl_reg >> (22 - irq * 2)) & 0x3;
295 return mpc52xx_map_senses[type];
298 static int mpc52xx_irqhost_map(struct irq_host *h, unsigned int virq,
303 struct irq_chip *good_irqchip;
307 l1irq = (irq & MPC52xx_IRQ_L1_MASK) >> MPC52xx_IRQ_L1_OFFSET;
308 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
311 * Most of ours IRQs will be level low
312 * Only external IRQs on some platform may be others
314 type = IRQ_TYPE_LEVEL_LOW;
317 case MPC52xx_IRQ_L1_CRIT:
318 pr_debug("%s: Critical. l2=%x\n", __func__, l2irq);
322 type = mpc52xx_irqx_gettype(l2irq);
323 good_irqchip = &mpc52xx_extirq_irqchip;
326 case MPC52xx_IRQ_L1_MAIN:
327 pr_debug("%s: Main IRQ[1-3] l2=%x\n", __func__, l2irq);
329 if ((l2irq >= 1) && (l2irq <= 3)) {
330 type = mpc52xx_irqx_gettype(l2irq);
331 good_irqchip = &mpc52xx_extirq_irqchip;
333 good_irqchip = &mpc52xx_main_irqchip;
337 case MPC52xx_IRQ_L1_PERP:
338 pr_debug("%s: Peripherals. l2=%x\n", __func__, l2irq);
339 good_irqchip = &mpc52xx_periph_irqchip;
342 case MPC52xx_IRQ_L1_SDMA:
343 pr_debug("%s: SDMA. l2=%x\n", __func__, l2irq);
344 good_irqchip = &mpc52xx_sdma_irqchip;
348 pr_debug("%s: Error, unknown L1 IRQ (0x%x)\n", __func__, l1irq);
349 printk(KERN_ERR "Unknow IRQ!\n");
354 case IRQ_TYPE_EDGE_FALLING:
355 case IRQ_TYPE_EDGE_RISING:
356 good_handle = handle_edge_irq;
359 good_handle = handle_level_irq;
362 set_irq_chip_and_handler(virq, good_irqchip, good_handle);
364 pr_debug("%s: virq=%x, hw=%x. type=%x\n", __func__, virq,
370 static struct irq_host_ops mpc52xx_irqhost_ops = {
371 .match = mpc52xx_irqhost_match,
372 .xlate = mpc52xx_irqhost_xlate,
373 .map = mpc52xx_irqhost_map,
380 void __init mpc52xx_init_irq(void)
383 struct device_node *picnode;
385 /* Remap the necessary zones */
386 picnode = of_find_compatible_node(NULL, NULL, "mpc5200-pic");
388 intr = mpc52xx_find_and_map("mpc5200-pic");
390 panic(__FILE__ ": find_and_map failed on 'mpc5200-pic'. "
393 sdma = mpc52xx_find_and_map("mpc5200-bestcomm");
395 panic(__FILE__ ": find_and_map failed on 'mpc5200-bestcomm'. "
398 /* Disable all interrupt sources. */
399 out_be32(&sdma->IntPend, 0xffffffff); /* 1 means clear pending */
400 out_be32(&sdma->IntMask, 0xffffffff); /* 1 means disabled */
401 out_be32(&intr->per_mask, 0x7ffffc00); /* 1 means disabled */
402 out_be32(&intr->main_mask, 0x00010fff); /* 1 means disabled */
403 intr_ctrl = in_be32(&intr->ctrl);
404 intr_ctrl &= 0x00ff0000; /* Keeps IRQ[0-3] config */
405 intr_ctrl |= 0x0f000000 | /* clear IRQ 0-3 */
406 0x00001000 | /* MEE master external enable */
407 0x00000000 | /* 0 means disable IRQ 0-3 */
408 0x00000001; /* CEb route critical normally */
409 out_be32(&intr->ctrl, intr_ctrl);
411 /* Zero a bunch of the priority settings. */
412 out_be32(&intr->per_pri1, 0);
413 out_be32(&intr->per_pri2, 0);
414 out_be32(&intr->per_pri3, 0);
415 out_be32(&intr->main_pri1, 0);
416 out_be32(&intr->main_pri2, 0);
419 * As last step, add an irq host to translate the real
420 * hw irq information provided by the ofw to linux virq
423 mpc52xx_irqhost = irq_alloc_host(IRQ_HOST_MAP_LINEAR,
424 MPC52xx_IRQ_HIGHTESTHWIRQ,
425 &mpc52xx_irqhost_ops, -1);
427 if (!mpc52xx_irqhost)
428 panic(__FILE__ ": Cannot allocate the IRQ host\n");
430 mpc52xx_irqhost->host_data = picnode;
431 printk(KERN_INFO "MPC52xx PIC is up and running!\n");
437 unsigned int mpc52xx_get_irq(void)
440 int irq = NO_IRQ_IGNORE;
442 status = in_be32(&intr->enc_status);
443 if (status & 0x00000400) { /* critical */
444 irq = (status >> 8) & 0x3;
445 if (irq == 2) /* high priority peripheral */
447 irq |= (MPC52xx_IRQ_L1_CRIT << MPC52xx_IRQ_L1_OFFSET) &
449 } else if (status & 0x00200000) { /* main */
450 irq = (status >> 16) & 0x1f;
451 if (irq == 4) /* low priority peripheral */
453 irq |= (MPC52xx_IRQ_L1_MAIN << MPC52xx_IRQ_L1_OFFSET) &
455 } else if (status & 0x20000000) { /* peripheral */
457 irq = (status >> 24) & 0x1f;
458 if (irq == 0) { /* bestcomm */
459 status = in_be32(&sdma->IntPend);
460 irq = ffs(status) - 1;
461 irq |= (MPC52xx_IRQ_L1_SDMA << MPC52xx_IRQ_L1_OFFSET) &
464 irq |= (MPC52xx_IRQ_L1_PERP << MPC52xx_IRQ_L1_OFFSET) &
469 pr_debug("%s: irq=%x. virq=%d\n", __func__, irq,
470 irq_linear_revmap(mpc52xx_irqhost, irq));
472 return irq_linear_revmap(mpc52xx_irqhost, irq);