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/interrupt.h>
22 #include <linux/irq.h>
26 #include <asm/mpc52xx.h>
27 #include "mpc52xx_pic.h"
33 /* MPC5200 device tree match tables */
34 static struct of_device_id mpc52xx_pic_ids[] __initdata = {
35 { .compatible = "fsl,mpc5200-pic", },
36 { .compatible = "mpc5200-pic", },
39 static struct of_device_id mpc52xx_sdma_ids[] __initdata = {
40 { .compatible = "fsl,mpc5200-bestcomm", },
41 { .compatible = "mpc5200-bestcomm", },
45 static struct mpc52xx_intr __iomem *intr;
46 static struct mpc52xx_sdma __iomem *sdma;
47 static struct irq_host *mpc52xx_irqhost = NULL;
49 static unsigned char mpc52xx_map_senses[4] = {
52 IRQ_TYPE_EDGE_FALLING,
60 static inline void io_be_setbit(u32 __iomem *addr, int bitno)
62 out_be32(addr, in_be32(addr) | (1 << bitno));
65 static inline void io_be_clrbit(u32 __iomem *addr, int bitno)
67 out_be32(addr, in_be32(addr) & ~(1 << bitno));
71 * IRQ[0-3] interrupt irq_chip
74 static void mpc52xx_extirq_mask(unsigned int virq)
79 irq = irq_map[virq].hwirq;
80 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
82 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
84 io_be_clrbit(&intr->ctrl, 11 - l2irq);
87 static void mpc52xx_extirq_unmask(unsigned int virq)
92 irq = irq_map[virq].hwirq;
93 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
95 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
97 io_be_setbit(&intr->ctrl, 11 - l2irq);
100 static void mpc52xx_extirq_ack(unsigned int virq)
105 irq = irq_map[virq].hwirq;
106 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
108 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
110 io_be_setbit(&intr->ctrl, 27-l2irq);
113 static int mpc52xx_extirq_set_type(unsigned int virq, unsigned int flow_type)
119 irq = irq_map[virq].hwirq;
120 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
122 pr_debug("%s: irq=%x. l2=%d flow_type=%d\n", __func__, irq, l2irq, flow_type);
125 case IRQF_TRIGGER_HIGH:
128 case IRQF_TRIGGER_RISING:
131 case IRQF_TRIGGER_FALLING:
134 case IRQF_TRIGGER_LOW:
141 ctrl_reg = in_be32(&intr->ctrl);
142 ctrl_reg &= ~(0x3 << (22 - (l2irq * 2)));
143 ctrl_reg |= (type << (22 - (l2irq * 2)));
144 out_be32(&intr->ctrl, ctrl_reg);
149 static struct irq_chip mpc52xx_extirq_irqchip = {
150 .typename = " MPC52xx IRQ[0-3] ",
151 .mask = mpc52xx_extirq_mask,
152 .unmask = mpc52xx_extirq_unmask,
153 .ack = mpc52xx_extirq_ack,
154 .set_type = mpc52xx_extirq_set_type,
158 * Main interrupt irq_chip
161 static void mpc52xx_main_mask(unsigned int virq)
166 irq = irq_map[virq].hwirq;
167 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
169 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
171 io_be_setbit(&intr->main_mask, 16 - l2irq);
174 static void mpc52xx_main_unmask(unsigned int virq)
179 irq = irq_map[virq].hwirq;
180 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
182 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
184 io_be_clrbit(&intr->main_mask, 16 - l2irq);
187 static struct irq_chip mpc52xx_main_irqchip = {
188 .typename = "MPC52xx Main",
189 .mask = mpc52xx_main_mask,
190 .mask_ack = mpc52xx_main_mask,
191 .unmask = mpc52xx_main_unmask,
195 * Peripherals interrupt irq_chip
198 static void mpc52xx_periph_mask(unsigned int virq)
203 irq = irq_map[virq].hwirq;
204 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
206 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
208 io_be_setbit(&intr->per_mask, 31 - l2irq);
211 static void mpc52xx_periph_unmask(unsigned int virq)
216 irq = irq_map[virq].hwirq;
217 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
219 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
221 io_be_clrbit(&intr->per_mask, 31 - l2irq);
224 static struct irq_chip mpc52xx_periph_irqchip = {
225 .typename = "MPC52xx Peripherals",
226 .mask = mpc52xx_periph_mask,
227 .mask_ack = mpc52xx_periph_mask,
228 .unmask = mpc52xx_periph_unmask,
232 * SDMA interrupt irq_chip
235 static void mpc52xx_sdma_mask(unsigned int virq)
240 irq = irq_map[virq].hwirq;
241 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
243 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
245 io_be_setbit(&sdma->IntMask, l2irq);
248 static void mpc52xx_sdma_unmask(unsigned int virq)
253 irq = irq_map[virq].hwirq;
254 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
256 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
258 io_be_clrbit(&sdma->IntMask, l2irq);
261 static void mpc52xx_sdma_ack(unsigned int virq)
266 irq = irq_map[virq].hwirq;
267 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
269 pr_debug("%s: irq=%x. l2=%d\n", __func__, irq, l2irq);
271 out_be32(&sdma->IntPend, 1 << l2irq);
274 static struct irq_chip mpc52xx_sdma_irqchip = {
275 .typename = "MPC52xx SDMA",
276 .mask = mpc52xx_sdma_mask,
277 .unmask = mpc52xx_sdma_unmask,
278 .ack = mpc52xx_sdma_ack,
285 static int mpc52xx_irqhost_xlate(struct irq_host *h, struct device_node *ct,
286 u32 * intspec, unsigned int intsize,
287 irq_hw_number_t * out_hwirq,
288 unsigned int *out_flags)
298 intrvect_l1 = (int)intspec[0];
299 intrvect_l2 = (int)intspec[1];
300 intrvect_type = (int)intspec[2];
303 (intrvect_l1 << MPC52xx_IRQ_L1_OFFSET) & MPC52xx_IRQ_L1_MASK;
305 (intrvect_l2 << MPC52xx_IRQ_L2_OFFSET) & MPC52xx_IRQ_L2_MASK;
307 pr_debug("return %x, l1=%d, l2=%d\n", intrvect_linux, intrvect_l1,
310 *out_hwirq = intrvect_linux;
311 *out_flags = mpc52xx_map_senses[intrvect_type];
317 * this function retrieves the correct IRQ type out
319 * Only externals IRQs needs this
321 static int mpc52xx_irqx_gettype(int irq)
326 ctrl_reg = in_be32(&intr->ctrl);
327 type = (ctrl_reg >> (22 - irq * 2)) & 0x3;
329 return mpc52xx_map_senses[type];
332 static int mpc52xx_irqhost_map(struct irq_host *h, unsigned int virq,
337 struct irq_chip *good_irqchip;
341 l1irq = (irq & MPC52xx_IRQ_L1_MASK) >> MPC52xx_IRQ_L1_OFFSET;
342 l2irq = (irq & MPC52xx_IRQ_L2_MASK) >> MPC52xx_IRQ_L2_OFFSET;
345 * Most of ours IRQs will be level low
346 * Only external IRQs on some platform may be others
348 type = IRQ_TYPE_LEVEL_LOW;
351 case MPC52xx_IRQ_L1_CRIT:
352 pr_debug("%s: Critical. l2=%x\n", __func__, l2irq);
356 type = mpc52xx_irqx_gettype(l2irq);
357 good_irqchip = &mpc52xx_extirq_irqchip;
360 case MPC52xx_IRQ_L1_MAIN:
361 pr_debug("%s: Main IRQ[1-3] l2=%x\n", __func__, l2irq);
363 if ((l2irq >= 1) && (l2irq <= 3)) {
364 type = mpc52xx_irqx_gettype(l2irq);
365 good_irqchip = &mpc52xx_extirq_irqchip;
367 good_irqchip = &mpc52xx_main_irqchip;
371 case MPC52xx_IRQ_L1_PERP:
372 pr_debug("%s: Peripherals. l2=%x\n", __func__, l2irq);
373 good_irqchip = &mpc52xx_periph_irqchip;
376 case MPC52xx_IRQ_L1_SDMA:
377 pr_debug("%s: SDMA. l2=%x\n", __func__, l2irq);
378 good_irqchip = &mpc52xx_sdma_irqchip;
382 pr_debug("%s: Error, unknown L1 IRQ (0x%x)\n", __func__, l1irq);
383 printk(KERN_ERR "Unknow IRQ!\n");
388 case IRQ_TYPE_EDGE_FALLING:
389 case IRQ_TYPE_EDGE_RISING:
390 good_handle = handle_edge_irq;
393 good_handle = handle_level_irq;
396 set_irq_chip_and_handler(virq, good_irqchip, good_handle);
398 pr_debug("%s: virq=%x, hw=%x. type=%x\n", __func__, virq,
404 static struct irq_host_ops mpc52xx_irqhost_ops = {
405 .xlate = mpc52xx_irqhost_xlate,
406 .map = mpc52xx_irqhost_map,
413 void __init mpc52xx_init_irq(void)
416 struct device_node *picnode;
417 struct device_node *np;
419 /* Remap the necessary zones */
420 picnode = of_find_matching_node(NULL, mpc52xx_pic_ids);
421 intr = of_iomap(picnode, 0);
423 panic(__FILE__ ": find_and_map failed on 'mpc5200-pic'. "
426 np = of_find_matching_node(NULL, mpc52xx_sdma_ids);
427 sdma = of_iomap(np, 0);
430 panic(__FILE__ ": find_and_map failed on 'mpc5200-bestcomm'. "
433 /* Disable all interrupt sources. */
434 out_be32(&sdma->IntPend, 0xffffffff); /* 1 means clear pending */
435 out_be32(&sdma->IntMask, 0xffffffff); /* 1 means disabled */
436 out_be32(&intr->per_mask, 0x7ffffc00); /* 1 means disabled */
437 out_be32(&intr->main_mask, 0x00010fff); /* 1 means disabled */
438 intr_ctrl = in_be32(&intr->ctrl);
439 intr_ctrl &= 0x00ff0000; /* Keeps IRQ[0-3] config */
440 intr_ctrl |= 0x0f000000 | /* clear IRQ 0-3 */
441 0x00001000 | /* MEE master external enable */
442 0x00000000 | /* 0 means disable IRQ 0-3 */
443 0x00000001; /* CEb route critical normally */
444 out_be32(&intr->ctrl, intr_ctrl);
446 /* Zero a bunch of the priority settings. */
447 out_be32(&intr->per_pri1, 0);
448 out_be32(&intr->per_pri2, 0);
449 out_be32(&intr->per_pri3, 0);
450 out_be32(&intr->main_pri1, 0);
451 out_be32(&intr->main_pri2, 0);
454 * As last step, add an irq host to translate the real
455 * hw irq information provided by the ofw to linux virq
458 mpc52xx_irqhost = irq_alloc_host(picnode, IRQ_HOST_MAP_LINEAR,
459 MPC52xx_IRQ_HIGHTESTHWIRQ,
460 &mpc52xx_irqhost_ops, -1);
462 if (!mpc52xx_irqhost)
463 panic(__FILE__ ": Cannot allocate the IRQ host\n");
465 printk(KERN_INFO "MPC52xx PIC is up and running!\n");
471 unsigned int mpc52xx_get_irq(void)
474 int irq = NO_IRQ_IGNORE;
476 status = in_be32(&intr->enc_status);
477 if (status & 0x00000400) { /* critical */
478 irq = (status >> 8) & 0x3;
479 if (irq == 2) /* high priority peripheral */
481 irq |= (MPC52xx_IRQ_L1_CRIT << MPC52xx_IRQ_L1_OFFSET) &
483 } else if (status & 0x00200000) { /* main */
484 irq = (status >> 16) & 0x1f;
485 if (irq == 4) /* low priority peripheral */
487 irq |= (MPC52xx_IRQ_L1_MAIN << MPC52xx_IRQ_L1_OFFSET) &
489 } else if (status & 0x20000000) { /* peripheral */
491 irq = (status >> 24) & 0x1f;
492 if (irq == 0) { /* bestcomm */
493 status = in_be32(&sdma->IntPend);
494 irq = ffs(status) - 1;
495 irq |= (MPC52xx_IRQ_L1_SDMA << MPC52xx_IRQ_L1_OFFSET) &
498 irq |= (MPC52xx_IRQ_L1_PERP << MPC52xx_IRQ_L1_OFFSET) &
503 pr_debug("%s: irq=%x. virq=%d\n", __func__, irq,
504 irq_linear_revmap(mpc52xx_irqhost, irq));
506 return irq_linear_revmap(mpc52xx_irqhost, irq);