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/config.h>
19 #include <linux/stddef.h>
20 #include <linux/init.h>
21 #include <linux/sched.h>
22 #include <linux/signal.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/sysdev.h>
26 #include <linux/adb.h>
27 #include <linux/pmu.h>
28 #include <linux/module.h>
30 #include <asm/sections.h>
34 #include <asm/pci-bridge.h>
36 #include <asm/pmac_feature.h>
42 * XXX this should be in xmon.h, but putting it there means xmon.h
43 * has to include <linux/interrupt.h> (to get irqreturn_t), which
44 * causes all sorts of problems. -- paulus
46 extern irqreturn_t xmon_irq(int, void *, struct pt_regs *);
56 /* Default addresses */
57 static volatile struct pmac_irq_hw __iomem *pmac_irq_hw[4];
59 #define GC_LEVEL_MASK 0x3ff00000
60 #define OHARE_LEVEL_MASK 0x1ff00000
61 #define HEATHROW_LEVEL_MASK 0x1ff00000
64 static int max_real_irqs;
65 static u32 level_mask[4];
67 static DEFINE_SPINLOCK(pmac_pic_lock);
69 #define GATWICK_IRQ_POOL_SIZE 10
70 static struct interrupt_info gatwick_int_pool[GATWICK_IRQ_POOL_SIZE];
72 #define NR_MASK_WORDS ((NR_IRQS + 31) / 32)
73 static unsigned long ppc_lost_interrupts[NR_MASK_WORDS];
76 * Mark an irq as "lost". This is only used on the pmac
77 * since it can lose interrupts (see pmac_set_irq_mask).
80 void __set_lost(unsigned long irq_nr, int nokick)
82 if (!test_and_set_bit(irq_nr, ppc_lost_interrupts)) {
83 atomic_inc(&ppc_n_lost_interrupts);
89 static void pmac_mask_and_ack_irq(unsigned int irq_nr)
91 unsigned long bit = 1UL << (irq_nr & 0x1f);
95 if ((unsigned)irq_nr >= max_irqs)
98 clear_bit(irq_nr, ppc_cached_irq_mask);
99 if (test_and_clear_bit(irq_nr, ppc_lost_interrupts))
100 atomic_dec(&ppc_n_lost_interrupts);
101 spin_lock_irqsave(&pmac_pic_lock, flags);
102 out_le32(&pmac_irq_hw[i]->enable, ppc_cached_irq_mask[i]);
103 out_le32(&pmac_irq_hw[i]->ack, bit);
105 /* make sure ack gets to controller before we enable
108 } while((in_le32(&pmac_irq_hw[i]->enable) & bit)
109 != (ppc_cached_irq_mask[i] & bit));
110 spin_unlock_irqrestore(&pmac_pic_lock, flags);
113 static void pmac_set_irq_mask(unsigned int irq_nr, int nokicklost)
115 unsigned long bit = 1UL << (irq_nr & 0x1f);
119 if ((unsigned)irq_nr >= max_irqs)
122 spin_lock_irqsave(&pmac_pic_lock, flags);
123 /* enable unmasked interrupts */
124 out_le32(&pmac_irq_hw[i]->enable, ppc_cached_irq_mask[i]);
127 /* make sure mask gets to controller before we
130 } while((in_le32(&pmac_irq_hw[i]->enable) & bit)
131 != (ppc_cached_irq_mask[i] & bit));
134 * Unfortunately, setting the bit in the enable register
135 * when the device interrupt is already on *doesn't* set
136 * the bit in the flag register or request another interrupt.
138 if (bit & ppc_cached_irq_mask[i] & in_le32(&pmac_irq_hw[i]->level))
139 __set_lost((ulong)irq_nr, nokicklost);
140 spin_unlock_irqrestore(&pmac_pic_lock, flags);
143 /* When an irq gets requested for the first client, if it's an
144 * edge interrupt, we clear any previous one on the controller
146 static unsigned int pmac_startup_irq(unsigned int irq_nr)
148 unsigned long bit = 1UL << (irq_nr & 0x1f);
151 if ((irq_desc[irq_nr].status & IRQ_LEVEL) == 0)
152 out_le32(&pmac_irq_hw[i]->ack, bit);
153 set_bit(irq_nr, ppc_cached_irq_mask);
154 pmac_set_irq_mask(irq_nr, 0);
159 static void pmac_mask_irq(unsigned int irq_nr)
161 clear_bit(irq_nr, ppc_cached_irq_mask);
162 pmac_set_irq_mask(irq_nr, 0);
166 static void pmac_unmask_irq(unsigned int irq_nr)
168 set_bit(irq_nr, ppc_cached_irq_mask);
169 pmac_set_irq_mask(irq_nr, 0);
172 static void pmac_end_irq(unsigned int irq_nr)
174 if (!(irq_desc[irq_nr].status & (IRQ_DISABLED|IRQ_INPROGRESS))
175 && irq_desc[irq_nr].action) {
176 set_bit(irq_nr, ppc_cached_irq_mask);
177 pmac_set_irq_mask(irq_nr, 1);
182 struct hw_interrupt_type pmac_pic = {
183 .typename = " PMAC-PIC ",
184 .startup = pmac_startup_irq,
185 .enable = pmac_unmask_irq,
186 .disable = pmac_mask_irq,
187 .ack = pmac_mask_and_ack_irq,
191 struct hw_interrupt_type gatwick_pic = {
192 .typename = " GATWICK ",
193 .startup = pmac_startup_irq,
194 .enable = pmac_unmask_irq,
195 .disable = pmac_mask_irq,
196 .ack = pmac_mask_and_ack_irq,
200 static irqreturn_t gatwick_action(int cpl, void *dev_id, struct pt_regs *regs)
204 for (irq = max_irqs; (irq -= 32) >= max_real_irqs; ) {
206 bits = in_le32(&pmac_irq_hw[i]->event) | ppc_lost_interrupts[i];
207 /* We must read level interrupts from the level register */
208 bits |= (in_le32(&pmac_irq_hw[i]->level) & level_mask[i]);
209 bits &= ppc_cached_irq_mask[i];
212 irq += __ilog2(bits);
216 printk("gatwick irq not from gatwick pic\n");
220 static int pmac_get_irq(struct pt_regs *regs)
223 unsigned long bits = 0;
226 void psurge_smp_message_recv(struct pt_regs *);
228 /* IPI's are a hack on the powersurge -- Cort */
229 if ( smp_processor_id() != 0 ) {
230 psurge_smp_message_recv(regs);
231 return -2; /* ignore, already handled */
233 #endif /* CONFIG_SMP */
234 for (irq = max_real_irqs; (irq -= 32) >= 0; ) {
236 bits = in_le32(&pmac_irq_hw[i]->event) | ppc_lost_interrupts[i];
237 /* We must read level interrupts from the level register */
238 bits |= (in_le32(&pmac_irq_hw[i]->level) & level_mask[i]);
239 bits &= ppc_cached_irq_mask[i];
242 irq += __ilog2(bits);
249 /* This routine will fix some missing interrupt values in the device tree
250 * on the gatwick mac-io controller used by some PowerBooks
252 * Walking of OF nodes could use a bit more fixing up here, but it's not
253 * very important as this is all boot time code on static portions of the
256 * However, the modifications done to "intrs" will have to be removed and
257 * replaced with proper updates of the "interrupts" properties or
258 * AAPL,interrupts, yet to be decided, once the dynamic parsing is there.
260 static void __init pmac_fix_gatwick_interrupts(struct device_node *gw,
263 struct device_node *node;
266 memset(gatwick_int_pool, 0, sizeof(gatwick_int_pool));
268 for (node = NULL; (node = of_get_next_child(gw, node)) != NULL;) {
270 if ((strcasecmp(node->name, "escc") == 0) && node->child) {
271 if (node->child->n_intrs < 3) {
272 node->child->intrs = &gatwick_int_pool[count];
275 node->child->n_intrs = 3;
276 node->child->intrs[0].line = 15+irq_base;
277 node->child->intrs[1].line = 4+irq_base;
278 node->child->intrs[2].line = 5+irq_base;
279 printk(KERN_INFO "irq: fixed SCC on gatwick"
281 node->child->intrs[0].line,
282 node->child->intrs[1].line,
283 node->child->intrs[2].line);
285 /* Fix media-bay & left SWIM */
286 if (strcasecmp(node->name, "media-bay") == 0) {
287 struct device_node* ya_node;
289 if (node->n_intrs == 0)
290 node->intrs = &gatwick_int_pool[count++];
292 node->intrs[0].line = 29+irq_base;
293 printk(KERN_INFO "irq: fixed media-bay on gatwick"
294 " (%d)\n", node->intrs[0].line);
296 ya_node = node->child;
298 if (strcasecmp(ya_node->name, "floppy") == 0) {
299 if (ya_node->n_intrs < 2) {
300 ya_node->intrs = &gatwick_int_pool[count];
303 ya_node->n_intrs = 2;
304 ya_node->intrs[0].line = 19+irq_base;
305 ya_node->intrs[1].line = 1+irq_base;
306 printk(KERN_INFO "irq: fixed floppy on second controller (%d,%d)\n",
307 ya_node->intrs[0].line, ya_node->intrs[1].line);
309 if (strcasecmp(ya_node->name, "ata4") == 0) {
310 if (ya_node->n_intrs < 2) {
311 ya_node->intrs = &gatwick_int_pool[count];
314 ya_node->n_intrs = 2;
315 ya_node->intrs[0].line = 14+irq_base;
316 ya_node->intrs[1].line = 3+irq_base;
317 printk(KERN_INFO "irq: fixed ide on second controller (%d,%d)\n",
318 ya_node->intrs[0].line, ya_node->intrs[1].line);
320 ya_node = ya_node->sibling;
325 printk("WARNING !! Gatwick interrupt pool overflow\n");
326 printk(" GATWICK_IRQ_POOL_SIZE = %d\n", GATWICK_IRQ_POOL_SIZE);
327 printk(" requested = %d\n", count);
332 * The PowerBook 3400/2400/3500 can have a combo ethernet/modem
333 * card which includes an ohare chip that acts as a second interrupt
334 * controller. If we find this second ohare, set it up and fix the
335 * interrupt value in the device tree for the ethernet chip.
337 static void __init enable_second_ohare(struct device_node *np)
339 unsigned char bus, devfn;
341 struct device_node *ether;
343 /* This code doesn't strictly belong here, it could be part of
344 * either the PCI initialisation or the feature code. It's kept
345 * here for historical reasons.
347 if (pci_device_from_OF_node(np, &bus, &devfn) == 0) {
348 struct pci_controller* hose =
349 pci_find_hose_for_OF_device(np);
351 printk(KERN_ERR "Can't find PCI hose for OHare2 !\n");
354 early_read_config_word(hose, bus, devfn, PCI_COMMAND, &cmd);
355 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
356 cmd &= ~PCI_COMMAND_IO;
357 early_write_config_word(hose, bus, devfn, PCI_COMMAND, cmd);
360 /* Fix interrupt for the modem/ethernet combo controller. The number
361 * in the device tree (27) is bogus (correct for the ethernet-only
362 * board but not the combo ethernet/modem board).
363 * The real interrupt is 28 on the second controller -> 28+32 = 60.
365 ether = of_find_node_by_name(NULL, "pci1011,14");
366 if (ether && ether->n_intrs > 0) {
367 ether->intrs[0].line = 60;
368 printk(KERN_INFO "irq: Fixed ethernet IRQ to %d\n",
369 ether->intrs[0].line);
375 static struct irqaction xmon_action = {
378 .mask = CPU_MASK_NONE,
383 static struct irqaction gatwick_cascade_action = {
384 .handler = gatwick_action,
385 .flags = SA_INTERRUPT,
386 .mask = CPU_MASK_NONE,
390 static void __init pmac_pic_probe_oldstyle(void)
393 int irq_cascade = -1;
394 struct device_node *master = NULL;
395 struct device_node *slave = NULL;
399 /* Set our get_irq function */
400 ppc_md.get_irq = pmac_get_irq;
403 * Find the interrupt controller type & node
406 if ((master = of_find_node_by_name(NULL, "gc")) != NULL) {
407 max_irqs = max_real_irqs = 32;
408 level_mask[0] = GC_LEVEL_MASK;
409 } else if ((master = of_find_node_by_name(NULL, "ohare")) != NULL) {
410 max_irqs = max_real_irqs = 32;
411 level_mask[0] = OHARE_LEVEL_MASK;
413 /* We might have a second cascaded ohare */
414 slave = of_find_node_by_name(NULL, "pci106b,7");
417 level_mask[1] = OHARE_LEVEL_MASK;
418 enable_second_ohare(slave);
420 } else if ((master = of_find_node_by_name(NULL, "mac-io")) != NULL) {
421 max_irqs = max_real_irqs = 64;
422 level_mask[0] = HEATHROW_LEVEL_MASK;
425 /* We might have a second cascaded heathrow */
426 slave = of_find_node_by_name(master, "mac-io");
428 /* Check ordering of master & slave */
429 if (device_is_compatible(master, "gatwick")) {
430 struct device_node *tmp;
431 BUG_ON(slave == NULL);
437 /* We found a slave */
440 level_mask[2] = HEATHROW_LEVEL_MASK;
442 pmac_fix_gatwick_interrupts(slave, max_real_irqs);
445 BUG_ON(master == NULL);
447 /* Set the handler for the main PIC */
448 for ( i = 0; i < max_real_irqs ; i++ )
449 irq_desc[i].handler = &pmac_pic;
451 /* Get addresses of first controller if we have a node for it */
452 BUG_ON(of_address_to_resource(master, 0, &r));
454 /* Map interrupts of primary controller */
455 addr = (u8 __iomem *) ioremap(r.start, 0x40);
457 pmac_irq_hw[i++] = (volatile struct pmac_irq_hw __iomem *)
459 if (max_real_irqs > 32)
460 pmac_irq_hw[i++] = (volatile struct pmac_irq_hw __iomem *)
464 printk(KERN_INFO "irq: Found primary Apple PIC %s for %d irqs\n",
465 master->full_name, max_real_irqs);
467 /* Map interrupts of cascaded controller */
468 if (slave && !of_address_to_resource(slave, 0, &r)) {
469 addr = (u8 __iomem *)ioremap(r.start, 0x40);
470 pmac_irq_hw[i++] = (volatile struct pmac_irq_hw __iomem *)
474 (volatile struct pmac_irq_hw __iomem *)
476 irq_cascade = slave->intrs[0].line;
478 printk(KERN_INFO "irq: Found slave Apple PIC %s for %d irqs"
479 " cascade: %d\n", slave->full_name,
480 max_irqs - max_real_irqs, irq_cascade);
484 /* disable all interrupts in all controllers */
485 for (i = 0; i * 32 < max_irqs; ++i)
486 out_le32(&pmac_irq_hw[i]->enable, 0);
488 /* mark level interrupts */
489 for (i = 0; i < max_irqs; i++)
490 if (level_mask[i >> 5] & (1UL << (i & 0x1f)))
491 irq_desc[i].status = IRQ_LEVEL;
493 /* Setup handlers for secondary controller and hook cascade irq*/
495 for ( i = max_real_irqs ; i < max_irqs ; i++ )
496 irq_desc[i].handler = &gatwick_pic;
497 setup_irq(irq_cascade, &gatwick_cascade_action);
499 printk(KERN_INFO "irq: System has %d possible interrupts\n", max_irqs);
501 setup_irq(20, &xmon_action);
504 #endif /* CONFIG_PPC32 */
506 static int pmac_u3_cascade(struct pt_regs *regs, void *data)
508 return mpic_get_one_irq((struct mpic *)data, regs);
511 static void __init pmac_pic_setup_mpic_nmi(struct mpic *mpic)
513 #if defined(CONFIG_XMON) && defined(CONFIG_PPC32)
514 struct device_node* pswitch;
517 pswitch = of_find_node_by_name(NULL, "programmer-switch");
518 if (pswitch && pswitch->n_intrs) {
519 nmi_irq = pswitch->intrs[0].line;
520 mpic_irq_set_priority(nmi_irq, 9);
521 setup_irq(nmi_irq, &xmon_action);
523 of_node_put(pswitch);
524 #endif /* defined(CONFIG_XMON) && defined(CONFIG_PPC32) */
527 static struct mpic * __init pmac_setup_one_mpic(struct device_node *np,
530 unsigned char senses[128];
531 int offset = master ? 0 : 128;
532 int count = master ? 128 : 124;
533 const char *name = master ? " MPIC 1 " : " MPIC 2 ";
536 unsigned int flags = master ? MPIC_PRIMARY : 0;
539 rc = of_address_to_resource(np, 0, &r);
543 pmac_call_feature(PMAC_FTR_ENABLE_MPIC, np, 0, 0);
545 prom_get_irq_senses(senses, offset, offset + count);
547 flags |= MPIC_WANTS_RESET;
548 if (get_property(np, "big-endian", NULL))
549 flags |= MPIC_BIG_ENDIAN;
551 /* Primary Big Endian means HT interrupts. This is quite dodgy
552 * but works until I find a better way
554 if (master && (flags & MPIC_BIG_ENDIAN))
555 flags |= MPIC_BROKEN_U3;
557 mpic = mpic_alloc(r.start, flags, 0, offset, count, master ? 252 : 0,
558 senses, count, name);
567 static int __init pmac_pic_probe_mpic(void)
569 struct mpic *mpic1, *mpic2;
570 struct device_node *np, *master = NULL, *slave = NULL;
572 /* We can have up to 2 MPICs cascaded */
573 for (np = NULL; (np = of_find_node_by_type(np, "open-pic"))
575 if (master == NULL &&
576 get_property(np, "interrupts", NULL) == NULL)
577 master = of_node_get(np);
578 else if (slave == NULL)
579 slave = of_node_get(np);
584 /* Check for bogus setups */
585 if (master == NULL && slave != NULL) {
590 /* Not found, default to good old pmac pic */
594 /* Set master handler */
595 ppc_md.get_irq = mpic_get_irq;
598 mpic1 = pmac_setup_one_mpic(master, 1);
599 BUG_ON(mpic1 == NULL);
601 /* Install NMI if any */
602 pmac_pic_setup_mpic_nmi(mpic1);
606 /* No slave, let's go out */
607 if (slave == NULL || slave->n_intrs < 1)
610 mpic2 = pmac_setup_one_mpic(slave, 0);
612 printk(KERN_ERR "Failed to setup slave MPIC\n");
616 mpic_setup_cascade(slave->intrs[0].line, pmac_u3_cascade, mpic2);
623 void __init pmac_pic_init(void)
625 /* We first try to detect Apple's new Core99 chipset, since mac-io
626 * is quite different on those machines and contains an IBM MPIC2.
628 if (pmac_pic_probe_mpic() == 0)
632 pmac_pic_probe_oldstyle();
636 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
638 * These procedures are used in implementing sleep on the powerbooks.
639 * sleep_save_intrs() saves the states of all interrupt enables
640 * and disables all interrupts except for the nominated one.
641 * sleep_restore_intrs() restores the states of all interrupt enables.
643 unsigned long sleep_save_mask[2];
645 /* This used to be passed by the PMU driver but that link got
646 * broken with the new driver model. We use this tweak for now...
648 static int pmacpic_find_viaint(void)
652 #ifdef CONFIG_ADB_PMU
653 struct device_node *np;
655 if (pmu_get_model() != PMU_OHARE_BASED)
657 np = of_find_node_by_name(NULL, "via-pmu");
660 viaint = np->intrs[0].line;
661 #endif /* CONFIG_ADB_PMU */
667 static int pmacpic_suspend(struct sys_device *sysdev, pm_message_t state)
669 int viaint = pmacpic_find_viaint();
671 sleep_save_mask[0] = ppc_cached_irq_mask[0];
672 sleep_save_mask[1] = ppc_cached_irq_mask[1];
673 ppc_cached_irq_mask[0] = 0;
674 ppc_cached_irq_mask[1] = 0;
676 set_bit(viaint, ppc_cached_irq_mask);
677 out_le32(&pmac_irq_hw[0]->enable, ppc_cached_irq_mask[0]);
678 if (max_real_irqs > 32)
679 out_le32(&pmac_irq_hw[1]->enable, ppc_cached_irq_mask[1]);
680 (void)in_le32(&pmac_irq_hw[0]->event);
681 /* make sure mask gets to controller before we return to caller */
683 (void)in_le32(&pmac_irq_hw[0]->enable);
688 static int pmacpic_resume(struct sys_device *sysdev)
692 out_le32(&pmac_irq_hw[0]->enable, 0);
693 if (max_real_irqs > 32)
694 out_le32(&pmac_irq_hw[1]->enable, 0);
696 for (i = 0; i < max_real_irqs; ++i)
697 if (test_bit(i, sleep_save_mask))
703 #endif /* CONFIG_PM && CONFIG_PPC32 */
705 static struct sysdev_class pmacpic_sysclass = {
706 set_kset_name("pmac_pic"),
709 static struct sys_device device_pmacpic = {
711 .cls = &pmacpic_sysclass,
714 static struct sysdev_driver driver_pmacpic = {
715 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
716 .suspend = &pmacpic_suspend,
717 .resume = &pmacpic_resume,
718 #endif /* CONFIG_PM && CONFIG_PPC32 */
721 static int __init init_pmacpic_sysfs(void)
727 printk(KERN_DEBUG "Registering pmac pic with sysfs...\n");
728 sysdev_class_register(&pmacpic_sysclass);
729 sysdev_register(&device_pmacpic);
730 sysdev_driver_register(&pmacpic_sysclass, &driver_pmacpic);
734 subsys_initcall(init_pmacpic_sysfs);