2 * Support for the interrupt controllers found on Power Macintosh,
3 * currently Apple's "Grand Central" interrupt controller in all
4 * it's incarnations. OpenPIC support used on newer machines is
7 * Copyright (C) 1997 Paul Mackerras (paulus@samba.org)
8 * Copyright (C) 2005 Benjamin Herrenschmidt (benh@kernel.crashing.org)
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
18 #include <linux/stddef.h>
19 #include <linux/init.h>
20 #include <linux/sched.h>
21 #include <linux/signal.h>
22 #include <linux/pci.h>
23 #include <linux/interrupt.h>
24 #include <linux/sysdev.h>
25 #include <linux/adb.h>
26 #include <linux/pmu.h>
27 #include <linux/module.h>
29 #include <asm/sections.h>
33 #include <asm/pci-bridge.h>
35 #include <asm/pmac_feature.h>
41 * XXX this should be in xmon.h, but putting it there means xmon.h
42 * has to include <linux/interrupt.h> (to get irqreturn_t), which
43 * causes all sorts of problems. -- paulus
45 extern irqreturn_t xmon_irq(int, void *, struct pt_regs *);
55 /* Default addresses */
56 static volatile struct pmac_irq_hw __iomem *pmac_irq_hw[4];
58 #define GC_LEVEL_MASK 0x3ff00000
59 #define OHARE_LEVEL_MASK 0x1ff00000
60 #define HEATHROW_LEVEL_MASK 0x1ff00000
63 static int max_real_irqs;
64 static u32 level_mask[4];
66 static DEFINE_SPINLOCK(pmac_pic_lock);
68 #define GATWICK_IRQ_POOL_SIZE 10
69 static struct interrupt_info gatwick_int_pool[GATWICK_IRQ_POOL_SIZE];
71 #define NR_MASK_WORDS ((NR_IRQS + 31) / 32)
72 static unsigned long ppc_lost_interrupts[NR_MASK_WORDS];
75 * Mark an irq as "lost". This is only used on the pmac
76 * since it can lose interrupts (see pmac_set_irq_mask).
79 void __set_lost(unsigned long irq_nr, int nokick)
81 if (!test_and_set_bit(irq_nr, ppc_lost_interrupts)) {
82 atomic_inc(&ppc_n_lost_interrupts);
88 static void pmac_mask_and_ack_irq(unsigned int irq_nr)
90 unsigned long bit = 1UL << (irq_nr & 0x1f);
94 if ((unsigned)irq_nr >= max_irqs)
97 clear_bit(irq_nr, ppc_cached_irq_mask);
98 if (test_and_clear_bit(irq_nr, ppc_lost_interrupts))
99 atomic_dec(&ppc_n_lost_interrupts);
100 spin_lock_irqsave(&pmac_pic_lock, flags);
101 out_le32(&pmac_irq_hw[i]->enable, ppc_cached_irq_mask[i]);
102 out_le32(&pmac_irq_hw[i]->ack, bit);
104 /* make sure ack gets to controller before we enable
107 } while((in_le32(&pmac_irq_hw[i]->enable) & bit)
108 != (ppc_cached_irq_mask[i] & bit));
109 spin_unlock_irqrestore(&pmac_pic_lock, flags);
112 static void pmac_set_irq_mask(unsigned int irq_nr, int nokicklost)
114 unsigned long bit = 1UL << (irq_nr & 0x1f);
118 if ((unsigned)irq_nr >= max_irqs)
121 spin_lock_irqsave(&pmac_pic_lock, flags);
122 /* enable unmasked interrupts */
123 out_le32(&pmac_irq_hw[i]->enable, ppc_cached_irq_mask[i]);
126 /* make sure mask gets to controller before we
129 } while((in_le32(&pmac_irq_hw[i]->enable) & bit)
130 != (ppc_cached_irq_mask[i] & bit));
133 * Unfortunately, setting the bit in the enable register
134 * when the device interrupt is already on *doesn't* set
135 * the bit in the flag register or request another interrupt.
137 if (bit & ppc_cached_irq_mask[i] & in_le32(&pmac_irq_hw[i]->level))
138 __set_lost((ulong)irq_nr, nokicklost);
139 spin_unlock_irqrestore(&pmac_pic_lock, flags);
142 /* When an irq gets requested for the first client, if it's an
143 * edge interrupt, we clear any previous one on the controller
145 static unsigned int pmac_startup_irq(unsigned int irq_nr)
147 unsigned long bit = 1UL << (irq_nr & 0x1f);
150 if ((irq_desc[irq_nr].status & IRQ_LEVEL) == 0)
151 out_le32(&pmac_irq_hw[i]->ack, bit);
152 set_bit(irq_nr, ppc_cached_irq_mask);
153 pmac_set_irq_mask(irq_nr, 0);
158 static void pmac_mask_irq(unsigned int irq_nr)
160 clear_bit(irq_nr, ppc_cached_irq_mask);
161 pmac_set_irq_mask(irq_nr, 0);
165 static void pmac_unmask_irq(unsigned int irq_nr)
167 set_bit(irq_nr, ppc_cached_irq_mask);
168 pmac_set_irq_mask(irq_nr, 0);
171 static void pmac_end_irq(unsigned int irq_nr)
173 if (!(irq_desc[irq_nr].status & (IRQ_DISABLED|IRQ_INPROGRESS))
174 && irq_desc[irq_nr].action) {
175 set_bit(irq_nr, ppc_cached_irq_mask);
176 pmac_set_irq_mask(irq_nr, 1);
181 struct hw_interrupt_type pmac_pic = {
182 .typename = " PMAC-PIC ",
183 .startup = pmac_startup_irq,
184 .enable = pmac_unmask_irq,
185 .disable = pmac_mask_irq,
186 .ack = pmac_mask_and_ack_irq,
190 struct hw_interrupt_type gatwick_pic = {
191 .typename = " GATWICK ",
192 .startup = pmac_startup_irq,
193 .enable = pmac_unmask_irq,
194 .disable = pmac_mask_irq,
195 .ack = pmac_mask_and_ack_irq,
199 static irqreturn_t gatwick_action(int cpl, void *dev_id, struct pt_regs *regs)
203 for (irq = max_irqs; (irq -= 32) >= max_real_irqs; ) {
205 bits = in_le32(&pmac_irq_hw[i]->event) | ppc_lost_interrupts[i];
206 /* We must read level interrupts from the level register */
207 bits |= (in_le32(&pmac_irq_hw[i]->level) & level_mask[i]);
208 bits &= ppc_cached_irq_mask[i];
211 irq += __ilog2(bits);
215 printk("gatwick irq not from gatwick pic\n");
219 static int pmac_get_irq(struct pt_regs *regs)
222 unsigned long bits = 0;
225 void psurge_smp_message_recv(struct pt_regs *);
227 /* IPI's are a hack on the powersurge -- Cort */
228 if ( smp_processor_id() != 0 ) {
229 psurge_smp_message_recv(regs);
230 return -2; /* ignore, already handled */
232 #endif /* CONFIG_SMP */
233 for (irq = max_real_irqs; (irq -= 32) >= 0; ) {
235 bits = in_le32(&pmac_irq_hw[i]->event) | ppc_lost_interrupts[i];
236 /* We must read level interrupts from the level register */
237 bits |= (in_le32(&pmac_irq_hw[i]->level) & level_mask[i]);
238 bits &= ppc_cached_irq_mask[i];
241 irq += __ilog2(bits);
248 /* This routine will fix some missing interrupt values in the device tree
249 * on the gatwick mac-io controller used by some PowerBooks
251 * Walking of OF nodes could use a bit more fixing up here, but it's not
252 * very important as this is all boot time code on static portions of the
255 * However, the modifications done to "intrs" will have to be removed and
256 * replaced with proper updates of the "interrupts" properties or
257 * AAPL,interrupts, yet to be decided, once the dynamic parsing is there.
259 static void __init pmac_fix_gatwick_interrupts(struct device_node *gw,
262 struct device_node *node;
265 memset(gatwick_int_pool, 0, sizeof(gatwick_int_pool));
267 for (node = NULL; (node = of_get_next_child(gw, node)) != NULL;) {
269 if ((strcasecmp(node->name, "escc") == 0) && node->child) {
270 if (node->child->n_intrs < 3) {
271 node->child->intrs = &gatwick_int_pool[count];
274 node->child->n_intrs = 3;
275 node->child->intrs[0].line = 15+irq_base;
276 node->child->intrs[1].line = 4+irq_base;
277 node->child->intrs[2].line = 5+irq_base;
278 printk(KERN_INFO "irq: fixed SCC on gatwick"
280 node->child->intrs[0].line,
281 node->child->intrs[1].line,
282 node->child->intrs[2].line);
284 /* Fix media-bay & left SWIM */
285 if (strcasecmp(node->name, "media-bay") == 0) {
286 struct device_node* ya_node;
288 if (node->n_intrs == 0)
289 node->intrs = &gatwick_int_pool[count++];
291 node->intrs[0].line = 29+irq_base;
292 printk(KERN_INFO "irq: fixed media-bay on gatwick"
293 " (%d)\n", node->intrs[0].line);
295 ya_node = node->child;
297 if (strcasecmp(ya_node->name, "floppy") == 0) {
298 if (ya_node->n_intrs < 2) {
299 ya_node->intrs = &gatwick_int_pool[count];
302 ya_node->n_intrs = 2;
303 ya_node->intrs[0].line = 19+irq_base;
304 ya_node->intrs[1].line = 1+irq_base;
305 printk(KERN_INFO "irq: fixed floppy on second controller (%d,%d)\n",
306 ya_node->intrs[0].line, ya_node->intrs[1].line);
308 if (strcasecmp(ya_node->name, "ata4") == 0) {
309 if (ya_node->n_intrs < 2) {
310 ya_node->intrs = &gatwick_int_pool[count];
313 ya_node->n_intrs = 2;
314 ya_node->intrs[0].line = 14+irq_base;
315 ya_node->intrs[1].line = 3+irq_base;
316 printk(KERN_INFO "irq: fixed ide on second controller (%d,%d)\n",
317 ya_node->intrs[0].line, ya_node->intrs[1].line);
319 ya_node = ya_node->sibling;
324 printk("WARNING !! Gatwick interrupt pool overflow\n");
325 printk(" GATWICK_IRQ_POOL_SIZE = %d\n", GATWICK_IRQ_POOL_SIZE);
326 printk(" requested = %d\n", count);
331 * The PowerBook 3400/2400/3500 can have a combo ethernet/modem
332 * card which includes an ohare chip that acts as a second interrupt
333 * controller. If we find this second ohare, set it up and fix the
334 * interrupt value in the device tree for the ethernet chip.
336 static void __init enable_second_ohare(struct device_node *np)
338 unsigned char bus, devfn;
340 struct device_node *ether;
342 /* This code doesn't strictly belong here, it could be part of
343 * either the PCI initialisation or the feature code. It's kept
344 * here for historical reasons.
346 if (pci_device_from_OF_node(np, &bus, &devfn) == 0) {
347 struct pci_controller* hose =
348 pci_find_hose_for_OF_device(np);
350 printk(KERN_ERR "Can't find PCI hose for OHare2 !\n");
353 early_read_config_word(hose, bus, devfn, PCI_COMMAND, &cmd);
354 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
355 cmd &= ~PCI_COMMAND_IO;
356 early_write_config_word(hose, bus, devfn, PCI_COMMAND, cmd);
359 /* Fix interrupt for the modem/ethernet combo controller. The number
360 * in the device tree (27) is bogus (correct for the ethernet-only
361 * board but not the combo ethernet/modem board).
362 * The real interrupt is 28 on the second controller -> 28+32 = 60.
364 ether = of_find_node_by_name(NULL, "pci1011,14");
365 if (ether && ether->n_intrs > 0) {
366 ether->intrs[0].line = 60;
367 printk(KERN_INFO "irq: Fixed ethernet IRQ to %d\n",
368 ether->intrs[0].line);
374 static struct irqaction xmon_action = {
377 .mask = CPU_MASK_NONE,
382 static struct irqaction gatwick_cascade_action = {
383 .handler = gatwick_action,
384 .flags = SA_INTERRUPT,
385 .mask = CPU_MASK_NONE,
389 static void __init pmac_pic_probe_oldstyle(void)
392 int irq_cascade = -1;
393 struct device_node *master = NULL;
394 struct device_node *slave = NULL;
398 /* Set our get_irq function */
399 ppc_md.get_irq = pmac_get_irq;
402 * Find the interrupt controller type & node
405 if ((master = of_find_node_by_name(NULL, "gc")) != NULL) {
406 max_irqs = max_real_irqs = 32;
407 level_mask[0] = GC_LEVEL_MASK;
408 } else if ((master = of_find_node_by_name(NULL, "ohare")) != NULL) {
409 max_irqs = max_real_irqs = 32;
410 level_mask[0] = OHARE_LEVEL_MASK;
412 /* We might have a second cascaded ohare */
413 slave = of_find_node_by_name(NULL, "pci106b,7");
416 level_mask[1] = OHARE_LEVEL_MASK;
417 enable_second_ohare(slave);
419 } else if ((master = of_find_node_by_name(NULL, "mac-io")) != NULL) {
420 max_irqs = max_real_irqs = 64;
421 level_mask[0] = HEATHROW_LEVEL_MASK;
424 /* We might have a second cascaded heathrow */
425 slave = of_find_node_by_name(master, "mac-io");
427 /* Check ordering of master & slave */
428 if (device_is_compatible(master, "gatwick")) {
429 struct device_node *tmp;
430 BUG_ON(slave == NULL);
436 /* We found a slave */
439 level_mask[2] = HEATHROW_LEVEL_MASK;
441 pmac_fix_gatwick_interrupts(slave, max_real_irqs);
444 BUG_ON(master == NULL);
446 /* Set the handler for the main PIC */
447 for ( i = 0; i < max_real_irqs ; i++ )
448 irq_desc[i].chip = &pmac_pic;
450 /* Get addresses of first controller if we have a node for it */
451 BUG_ON(of_address_to_resource(master, 0, &r));
453 /* Map interrupts of primary controller */
454 addr = (u8 __iomem *) ioremap(r.start, 0x40);
456 pmac_irq_hw[i++] = (volatile struct pmac_irq_hw __iomem *)
458 if (max_real_irqs > 32)
459 pmac_irq_hw[i++] = (volatile struct pmac_irq_hw __iomem *)
463 printk(KERN_INFO "irq: Found primary Apple PIC %s for %d irqs\n",
464 master->full_name, max_real_irqs);
466 /* Map interrupts of cascaded controller */
467 if (slave && !of_address_to_resource(slave, 0, &r)) {
468 addr = (u8 __iomem *)ioremap(r.start, 0x40);
469 pmac_irq_hw[i++] = (volatile struct pmac_irq_hw __iomem *)
473 (volatile struct pmac_irq_hw __iomem *)
475 irq_cascade = slave->intrs[0].line;
477 printk(KERN_INFO "irq: Found slave Apple PIC %s for %d irqs"
478 " cascade: %d\n", slave->full_name,
479 max_irqs - max_real_irqs, irq_cascade);
483 /* disable all interrupts in all controllers */
484 for (i = 0; i * 32 < max_irqs; ++i)
485 out_le32(&pmac_irq_hw[i]->enable, 0);
487 /* mark level interrupts */
488 for (i = 0; i < max_irqs; i++)
489 if (level_mask[i >> 5] & (1UL << (i & 0x1f)))
490 irq_desc[i].status = IRQ_LEVEL;
492 /* Setup handlers for secondary controller and hook cascade irq*/
494 for ( i = max_real_irqs ; i < max_irqs ; i++ )
495 irq_desc[i].chip = &gatwick_pic;
496 setup_irq(irq_cascade, &gatwick_cascade_action);
498 printk(KERN_INFO "irq: System has %d possible interrupts\n", max_irqs);
500 setup_irq(20, &xmon_action);
503 #endif /* CONFIG_PPC32 */
505 static int pmac_u3_cascade(struct pt_regs *regs, void *data)
507 return mpic_get_one_irq((struct mpic *)data, regs);
510 static void __init pmac_pic_setup_mpic_nmi(struct mpic *mpic)
512 #if defined(CONFIG_XMON) && defined(CONFIG_PPC32)
513 struct device_node* pswitch;
516 pswitch = of_find_node_by_name(NULL, "programmer-switch");
517 if (pswitch && pswitch->n_intrs) {
518 nmi_irq = pswitch->intrs[0].line;
519 mpic_irq_set_priority(nmi_irq, 9);
520 setup_irq(nmi_irq, &xmon_action);
522 of_node_put(pswitch);
523 #endif /* defined(CONFIG_XMON) && defined(CONFIG_PPC32) */
526 static struct mpic * __init pmac_setup_one_mpic(struct device_node *np,
529 unsigned char senses[128];
530 int offset = master ? 0 : 128;
531 int count = master ? 128 : 124;
532 const char *name = master ? " MPIC 1 " : " MPIC 2 ";
535 unsigned int flags = master ? MPIC_PRIMARY : 0;
538 rc = of_address_to_resource(np, 0, &r);
542 pmac_call_feature(PMAC_FTR_ENABLE_MPIC, np, 0, 0);
544 prom_get_irq_senses(senses, offset, offset + count);
546 flags |= MPIC_WANTS_RESET;
547 if (get_property(np, "big-endian", NULL))
548 flags |= MPIC_BIG_ENDIAN;
550 /* Primary Big Endian means HT interrupts. This is quite dodgy
551 * but works until I find a better way
553 if (master && (flags & MPIC_BIG_ENDIAN))
554 flags |= MPIC_BROKEN_U3;
556 mpic = mpic_alloc(r.start, flags, 0, offset, count, master ? 252 : 0,
557 senses, count, name);
566 static int __init pmac_pic_probe_mpic(void)
568 struct mpic *mpic1, *mpic2;
569 struct device_node *np, *master = NULL, *slave = NULL;
571 /* We can have up to 2 MPICs cascaded */
572 for (np = NULL; (np = of_find_node_by_type(np, "open-pic"))
574 if (master == NULL &&
575 get_property(np, "interrupts", NULL) == NULL)
576 master = of_node_get(np);
577 else if (slave == NULL)
578 slave = of_node_get(np);
583 /* Check for bogus setups */
584 if (master == NULL && slave != NULL) {
589 /* Not found, default to good old pmac pic */
593 /* Set master handler */
594 ppc_md.get_irq = mpic_get_irq;
597 mpic1 = pmac_setup_one_mpic(master, 1);
598 BUG_ON(mpic1 == NULL);
600 /* Install NMI if any */
601 pmac_pic_setup_mpic_nmi(mpic1);
605 /* No slave, let's go out */
606 if (slave == NULL || slave->n_intrs < 1)
609 mpic2 = pmac_setup_one_mpic(slave, 0);
611 printk(KERN_ERR "Failed to setup slave MPIC\n");
615 mpic_setup_cascade(slave->intrs[0].line, pmac_u3_cascade, mpic2);
622 void __init pmac_pic_init(void)
624 /* We first try to detect Apple's new Core99 chipset, since mac-io
625 * is quite different on those machines and contains an IBM MPIC2.
627 if (pmac_pic_probe_mpic() == 0)
631 pmac_pic_probe_oldstyle();
635 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
637 * These procedures are used in implementing sleep on the powerbooks.
638 * sleep_save_intrs() saves the states of all interrupt enables
639 * and disables all interrupts except for the nominated one.
640 * sleep_restore_intrs() restores the states of all interrupt enables.
642 unsigned long sleep_save_mask[2];
644 /* This used to be passed by the PMU driver but that link got
645 * broken with the new driver model. We use this tweak for now...
647 static int pmacpic_find_viaint(void)
651 #ifdef CONFIG_ADB_PMU
652 struct device_node *np;
654 if (pmu_get_model() != PMU_OHARE_BASED)
656 np = of_find_node_by_name(NULL, "via-pmu");
659 viaint = np->intrs[0].line;
660 #endif /* CONFIG_ADB_PMU */
666 static int pmacpic_suspend(struct sys_device *sysdev, pm_message_t state)
668 int viaint = pmacpic_find_viaint();
670 sleep_save_mask[0] = ppc_cached_irq_mask[0];
671 sleep_save_mask[1] = ppc_cached_irq_mask[1];
672 ppc_cached_irq_mask[0] = 0;
673 ppc_cached_irq_mask[1] = 0;
675 set_bit(viaint, ppc_cached_irq_mask);
676 out_le32(&pmac_irq_hw[0]->enable, ppc_cached_irq_mask[0]);
677 if (max_real_irqs > 32)
678 out_le32(&pmac_irq_hw[1]->enable, ppc_cached_irq_mask[1]);
679 (void)in_le32(&pmac_irq_hw[0]->event);
680 /* make sure mask gets to controller before we return to caller */
682 (void)in_le32(&pmac_irq_hw[0]->enable);
687 static int pmacpic_resume(struct sys_device *sysdev)
691 out_le32(&pmac_irq_hw[0]->enable, 0);
692 if (max_real_irqs > 32)
693 out_le32(&pmac_irq_hw[1]->enable, 0);
695 for (i = 0; i < max_real_irqs; ++i)
696 if (test_bit(i, sleep_save_mask))
702 #endif /* CONFIG_PM && CONFIG_PPC32 */
704 static struct sysdev_class pmacpic_sysclass = {
705 set_kset_name("pmac_pic"),
708 static struct sys_device device_pmacpic = {
710 .cls = &pmacpic_sysclass,
713 static struct sysdev_driver driver_pmacpic = {
714 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
715 .suspend = &pmacpic_suspend,
716 .resume = &pmacpic_resume,
717 #endif /* CONFIG_PM && CONFIG_PPC32 */
720 static int __init init_pmacpic_sysfs(void)
726 printk(KERN_DEBUG "Registering pmac pic with sysfs...\n");
727 sysdev_class_register(&pmacpic_sysclass);
728 sysdev_register(&device_pmacpic);
729 sysdev_driver_register(&pmacpic_sysclass, &driver_pmacpic);
733 subsys_initcall(init_pmacpic_sysfs);