2 * BK Id: SCCS/s.pmac_pic.c 1.34 01/07/03 22:00:10 paulus
4 #include <linux/config.h>
5 #include <linux/stddef.h>
6 #include <linux/init.h>
7 #include <linux/sched.h>
8 #include <linux/signal.h>
11 #include <asm/sections.h>
15 #include <asm/pci-bridge.h>
17 #include <asm/open_pic.h>
28 /* Default addresses */
29 static volatile struct pmac_irq_hw *pmac_irq_hw[4] __pmacdata = {
30 (struct pmac_irq_hw *) 0xf3000020,
31 (struct pmac_irq_hw *) 0xf3000010,
32 (struct pmac_irq_hw *) 0xf4000020,
33 (struct pmac_irq_hw *) 0xf4000010,
36 #define GC_LEVEL_MASK 0x3ff00000
37 #define OHARE_LEVEL_MASK 0x1ff00000
38 #define HEATHROW_LEVEL_MASK 0x1ff00000
40 static int max_irqs __pmacdata;
41 static int max_real_irqs __pmacdata;
42 static u32 level_mask[4] __pmacdata;
44 static spinlock_t pmac_pic_lock __pmacdata = SPIN_LOCK_UNLOCKED;
47 #define GATWICK_IRQ_POOL_SIZE 10
48 static struct interrupt_info gatwick_int_pool[GATWICK_IRQ_POOL_SIZE] __pmacdata;
51 * Mark an irq as "lost". This is only used on the pmac
52 * since it can lose interrupts (see pmac_set_irq_mask).
56 __set_lost(unsigned long irq_nr, int nokick)
58 if (!test_and_set_bit(irq_nr, ppc_lost_interrupts)) {
59 atomic_inc(&ppc_n_lost_interrupts);
66 pmac_mask_and_ack_irq(unsigned int irq_nr)
68 unsigned long bit = 1UL << (irq_nr & 0x1f);
72 if ((unsigned)irq_nr >= max_irqs)
75 clear_bit(irq_nr, ppc_cached_irq_mask);
76 if (test_and_clear_bit(irq_nr, ppc_lost_interrupts))
77 atomic_dec(&ppc_n_lost_interrupts);
78 spin_lock_irqsave(&pmac_pic_lock, flags);
79 out_le32(&pmac_irq_hw[i]->enable, ppc_cached_irq_mask[i]);
80 out_le32(&pmac_irq_hw[i]->ack, bit);
82 /* make sure ack gets to controller before we enable
85 } while((in_le32(&pmac_irq_hw[i]->enable) & bit)
86 != (ppc_cached_irq_mask[i] & bit));
87 spin_unlock_irqrestore(&pmac_pic_lock, flags);
90 static void __pmac pmac_set_irq_mask(unsigned int irq_nr, int nokicklost)
92 unsigned long bit = 1UL << (irq_nr & 0x1f);
96 if ((unsigned)irq_nr >= max_irqs)
99 spin_lock_irqsave(&pmac_pic_lock, flags);
100 /* enable unmasked interrupts */
101 out_le32(&pmac_irq_hw[i]->enable, ppc_cached_irq_mask[i]);
104 /* make sure mask gets to controller before we
107 } while((in_le32(&pmac_irq_hw[i]->enable) & bit)
108 != (ppc_cached_irq_mask[i] & bit));
111 * Unfortunately, setting the bit in the enable register
112 * when the device interrupt is already on *doesn't* set
113 * the bit in the flag register or request another interrupt.
115 if (bit & ppc_cached_irq_mask[i] & in_le32(&pmac_irq_hw[i]->level))
116 __set_lost((ulong)irq_nr, nokicklost);
117 spin_unlock_irqrestore(&pmac_pic_lock, flags);
120 static void __pmac pmac_mask_irq(unsigned int irq_nr)
122 clear_bit(irq_nr, ppc_cached_irq_mask);
123 pmac_set_irq_mask(irq_nr, 0);
127 static void __pmac pmac_unmask_irq(unsigned int irq_nr)
129 set_bit(irq_nr, ppc_cached_irq_mask);
130 pmac_set_irq_mask(irq_nr, 0);
133 static void __pmac pmac_end_irq(unsigned int irq_nr)
135 if (!(irq_desc[irq_nr].status & (IRQ_DISABLED|IRQ_INPROGRESS))) {
136 set_bit(irq_nr, ppc_cached_irq_mask);
137 pmac_set_irq_mask(irq_nr, 1);
142 struct hw_interrupt_type pmac_pic = {
148 pmac_mask_and_ack_irq,
153 struct hw_interrupt_type gatwick_pic = {
159 pmac_mask_and_ack_irq,
164 static void gatwick_action(int cpl, void *dev_id, struct pt_regs *regs)
168 for (irq = max_irqs; (irq -= 32) >= max_real_irqs; ) {
170 bits = in_le32(&pmac_irq_hw[i]->event) | ppc_lost_interrupts[i];
171 /* We must read level interrupts from the level register */
172 bits |= (in_le32(&pmac_irq_hw[i]->level) & level_mask[i]);
173 bits &= ppc_cached_irq_mask[i];
176 irq += __ilog2(bits);
179 /* The previous version of this code allowed for this case, we
180 * don't. Put this here to check for it.
183 if ( irq_desc[irq].handler != &gatwick_pic )
184 printk("gatwick irq not from gatwick pic\n");
186 ppc_irq_dispatch_handler( regs, irq );
190 pmac_get_irq(struct pt_regs *regs)
193 unsigned long bits = 0;
196 void psurge_smp_message_recv(struct pt_regs *);
198 /* IPI's are a hack on the powersurge -- Cort */
199 if ( smp_processor_id() != 0 ) {
200 psurge_smp_message_recv(regs);
201 return -2; /* ignore, already handled */
203 #endif /* CONFIG_SMP */
204 for (irq = max_real_irqs; (irq -= 32) >= 0; ) {
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);
219 /* This routine will fix some missing interrupt values in the device tree
220 * on the gatwick mac-io controller used by some PowerBooks
223 pmac_fix_gatwick_interrupts(struct device_node *gw, int irq_base)
225 struct device_node *node;
228 memset(gatwick_int_pool, 0, sizeof(gatwick_int_pool));
234 if (strcasecmp(node->name, "escc") == 0)
236 if (node->child->n_intrs < 3) {
237 node->child->intrs = &gatwick_int_pool[count];
240 node->child->n_intrs = 3;
241 node->child->intrs[0].line = 15+irq_base;
242 node->child->intrs[1].line = 4+irq_base;
243 node->child->intrs[2].line = 5+irq_base;
244 printk(KERN_INFO "irq: fixed SCC on second controller (%d,%d,%d)\n",
245 node->child->intrs[0].line,
246 node->child->intrs[1].line,
247 node->child->intrs[2].line);
249 /* Fix media-bay & left SWIM */
250 if (strcasecmp(node->name, "media-bay") == 0) {
251 struct device_node* ya_node;
253 if (node->n_intrs == 0)
254 node->intrs = &gatwick_int_pool[count++];
256 node->intrs[0].line = 29+irq_base;
257 printk(KERN_INFO "irq: fixed media-bay on second controller (%d)\n",
258 node->intrs[0].line);
260 ya_node = node->child;
263 if (strcasecmp(ya_node->name, "floppy") == 0) {
264 if (ya_node->n_intrs < 2) {
265 ya_node->intrs = &gatwick_int_pool[count];
268 ya_node->n_intrs = 2;
269 ya_node->intrs[0].line = 19+irq_base;
270 ya_node->intrs[1].line = 1+irq_base;
271 printk(KERN_INFO "irq: fixed floppy on second controller (%d,%d)\n",
272 ya_node->intrs[0].line, ya_node->intrs[1].line);
274 if (strcasecmp(ya_node->name, "ata4") == 0) {
275 if (ya_node->n_intrs < 2) {
276 ya_node->intrs = &gatwick_int_pool[count];
279 ya_node->n_intrs = 2;
280 ya_node->intrs[0].line = 14+irq_base;
281 ya_node->intrs[1].line = 3+irq_base;
282 printk(KERN_INFO "irq: fixed ide on second controller (%d,%d)\n",
283 ya_node->intrs[0].line, ya_node->intrs[1].line);
285 ya_node = ya_node->sibling;
288 node = node->sibling;
291 printk("WARNING !! Gatwick interrupt pool overflow\n");
292 printk(" GATWICK_IRQ_POOL_SIZE = %d\n", GATWICK_IRQ_POOL_SIZE);
293 printk(" requested = %d\n", count);
298 * The PowerBook 3400/2400/3500 can have a combo ethernet/modem
299 * card which includes an ohare chip that acts as a second interrupt
300 * controller. If we find this second ohare, set it up and fix the
301 * interrupt value in the device tree for the ethernet chip.
303 static int __init enable_second_ohare(void)
305 unsigned char bus, devfn;
308 struct device_node *irqctrler = find_devices("pci106b,7");
309 struct device_node *ether;
311 if (irqctrler == NULL || irqctrler->n_addrs <= 0)
313 addr = (unsigned long) ioremap(irqctrler->addrs[0].address, 0x40);
314 pmac_irq_hw[1] = (volatile struct pmac_irq_hw *)(addr + 0x20);
316 if (pci_device_from_OF_node(irqctrler, &bus, &devfn) == 0) {
317 struct pci_controller* hose = pci_find_hose_for_OF_device(irqctrler);
319 printk(KERN_ERR "Can't find PCI hose for OHare2 !\n");
321 early_read_config_word(hose, bus, devfn, PCI_COMMAND, &cmd);
322 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
323 cmd &= ~PCI_COMMAND_IO;
324 early_write_config_word(hose, bus, devfn, PCI_COMMAND, cmd);
328 /* Fix interrupt for the modem/ethernet combo controller. The number
329 in the device tree (27) is bogus (correct for the ethernet-only
330 board but not the combo ethernet/modem board).
331 The real interrupt is 28 on the second controller -> 28+32 = 60.
333 ether = find_devices("pci1011,14");
334 if (ether && ether->n_intrs > 0) {
335 ether->intrs[0].line = 60;
336 printk(KERN_INFO "irq: Fixed ethernet IRQ to %d\n",
337 ether->intrs[0].line);
340 /* Return the interrupt number of the cascade */
341 return irqctrler->intrs[0].line;
348 struct device_node *irqctrler;
350 int irq_cascade = -1;
352 /* We first try to detect Apple's new Core99 chipset, since mac-io
353 * is quite different on those machines and contains an IBM MPIC2.
355 irqctrler = find_type_devices("open-pic");
356 if (irqctrler != NULL)
358 printk("PowerMac using OpenPIC irq controller\n");
359 if (irqctrler->n_addrs > 0)
361 unsigned char senses[NR_IRQS];
363 prom_get_irq_senses(senses, 0, NR_IRQS);
364 OpenPIC_InitSenses = senses;
365 OpenPIC_NumInitSenses = NR_IRQS;
366 ppc_md.get_irq = openpic_get_irq;
367 OpenPIC_Addr = ioremap(irqctrler->addrs[0].address,
368 irqctrler->addrs[0].size);
372 struct device_node* pswitch;
375 pswitch = find_devices("programmer-switch");
376 if (pswitch && pswitch->n_intrs) {
377 nmi_irq = pswitch->intrs[0].line;
378 openpic_init_nmi_irq(nmi_irq);
379 request_irq(nmi_irq, xmon_irq, 0,
383 #endif /* CONFIG_XMON */
389 /* Get the level/edge settings, assume if it's not
390 * a Grand Central nor an OHare, then it's an Heathrow
393 if (find_devices("gc"))
394 level_mask[0] = GC_LEVEL_MASK;
395 else if (find_devices("ohare")) {
396 level_mask[0] = OHARE_LEVEL_MASK;
397 /* We might have a second cascaded ohare */
398 level_mask[1] = OHARE_LEVEL_MASK;
400 level_mask[0] = HEATHROW_LEVEL_MASK;
402 /* We might have a second cascaded heathrow */
403 level_mask[2] = HEATHROW_LEVEL_MASK;
408 * G3 powermacs and 1999 G3 PowerBooks have 64 interrupts,
409 * 1998 G3 Series PowerBooks have 128,
410 * other powermacs have 32.
411 * The combo ethernet/modem card for the Powerstar powerbooks
412 * (2400/3400/3500, ohare based) has a second ohare chip
413 * effectively making a total of 64.
415 max_irqs = max_real_irqs = 32;
416 irqctrler = find_devices("mac-io");
425 for ( i = 0; i < max_real_irqs ; i++ )
426 irq_desc[i].handler = &pmac_pic;
428 /* get addresses of first controller */
430 if (irqctrler->n_addrs > 0) {
431 addr = (unsigned long)
432 ioremap(irqctrler->addrs[0].address, 0x40);
433 for (i = 0; i < 2; ++i)
434 pmac_irq_hw[i] = (volatile struct pmac_irq_hw*)
435 (addr + (2 - i) * 0x10);
438 /* get addresses of second controller */
439 irqctrler = irqctrler->next;
440 if (irqctrler && irqctrler->n_addrs > 0) {
441 addr = (unsigned long)
442 ioremap(irqctrler->addrs[0].address, 0x40);
443 for (i = 2; i < 4; ++i)
444 pmac_irq_hw[i] = (volatile struct pmac_irq_hw*)
445 (addr + (4 - i) * 0x10);
446 irq_cascade = irqctrler->intrs[0].line;
447 if (device_is_compatible(irqctrler, "gatwick"))
448 pmac_fix_gatwick_interrupts(irqctrler, max_real_irqs);
451 /* older powermacs have a GC (grand central) or ohare at
452 f3000000, with interrupt control registers at f3000020. */
453 addr = (unsigned long) ioremap(0xf3000000, 0x40);
454 pmac_irq_hw[0] = (volatile struct pmac_irq_hw *) (addr + 0x20);
457 /* PowerBooks 3400 and 3500 can have a second controller in a second
458 ohare chip, on the combo ethernet/modem card */
459 if (machine_is_compatible("AAPL,3400/2400")
460 || machine_is_compatible("AAPL,3500"))
461 irq_cascade = enable_second_ohare();
463 /* disable all interrupts in all controllers */
464 for (i = 0; i * 32 < max_irqs; ++i)
465 out_le32(&pmac_irq_hw[i]->enable, 0);
466 /* mark level interrupts */
467 for (i = 0; i < max_irqs; i++)
468 if (level_mask[i >> 5] & (1UL << (i & 0x1f)))
469 irq_desc[i].status = IRQ_LEVEL;
471 /* get interrupt line of secondary interrupt controller */
472 if (irq_cascade >= 0) {
473 printk(KERN_INFO "irq: secondary controller on irq %d\n",
475 for ( i = max_real_irqs ; i < max_irqs ; i++ )
476 irq_desc[i].handler = &gatwick_pic;
477 request_irq( irq_cascade, gatwick_action, SA_INTERRUPT,
480 printk("System has %d possible interrupts\n", max_irqs);
481 if (max_irqs != max_real_irqs)
482 printk(KERN_DEBUG "%d interrupts on main controller\n",
486 request_irq(20, xmon_irq, 0, "NMI - XMON", 0);
487 #endif /* CONFIG_XMON */
490 #ifdef CONFIG_PMAC_PBOOK
492 * These procedures are used in implementing sleep on the powerbooks.
493 * sleep_save_intrs() saves the states of all interrupt enables
494 * and disables all interrupts except for the nominated one.
495 * sleep_restore_intrs() restores the states of all interrupt enables.
497 unsigned int sleep_save_mask[2];
500 pmac_sleep_save_intrs(int viaint)
502 sleep_save_mask[0] = ppc_cached_irq_mask[0];
503 sleep_save_mask[1] = ppc_cached_irq_mask[1];
504 ppc_cached_irq_mask[0] = 0;
505 ppc_cached_irq_mask[1] = 0;
507 set_bit(viaint, ppc_cached_irq_mask);
508 out_le32(&pmac_irq_hw[0]->enable, ppc_cached_irq_mask[0]);
509 if (max_real_irqs > 32)
510 out_le32(&pmac_irq_hw[1]->enable, ppc_cached_irq_mask[1]);
511 (void)in_le32(&pmac_irq_hw[0]->event);
512 /* make sure mask gets to controller before we return to caller */
514 (void)in_le32(&pmac_irq_hw[0]->enable);
518 pmac_sleep_restore_intrs(void)
522 out_le32(&pmac_irq_hw[0]->enable, 0);
523 if (max_real_irqs > 32)
524 out_le32(&pmac_irq_hw[1]->enable, 0);
526 for (i = 0; i < max_real_irqs; ++i)
527 if (test_bit(i, sleep_save_mask))
530 #endif /* CONFIG_PMAC_PBOOK */