blob: c9b09a9e6050ef52ec5e495173f7b53a4155c785 [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
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
5 * in a separate file
6 *
7 * Copyright (C) 1997 Paul Mackerras (paulus@samba.org)
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +11008 * Copyright (C) 2005 Benjamin Herrenschmidt (benh@kernel.crashing.org)
9 * IBM, Corp.
Paul Mackerras14cf11a2005-09-26 16:04:21 +100010 *
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.
15 *
16 */
17
Paul Mackerras14cf11a2005-09-26 16:04:21 +100018#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>
Paul Mackerras3c3f42d2005-10-10 22:58:41 +100027#include <linux/module.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100028
29#include <asm/sections.h>
30#include <asm/io.h>
31#include <asm/smp.h>
32#include <asm/prom.h>
33#include <asm/pci-bridge.h>
34#include <asm/time.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100035#include <asm/pmac_feature.h>
36#include <asm/mpic.h>
37
Paul Mackerras3c3f42d2005-10-10 22:58:41 +100038#include "pmac.h"
Paul Mackerras14cf11a2005-09-26 16:04:21 +100039
40/*
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
44 */
45extern irqreturn_t xmon_irq(int, void *, struct pt_regs *);
46
Paul Mackerras3c3f42d2005-10-10 22:58:41 +100047#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +100048struct pmac_irq_hw {
49 unsigned int event;
50 unsigned int enable;
51 unsigned int ack;
52 unsigned int level;
53};
54
55/* Default addresses */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +110056static volatile struct pmac_irq_hw __iomem *pmac_irq_hw[4];
Paul Mackerras14cf11a2005-09-26 16:04:21 +100057
58#define GC_LEVEL_MASK 0x3ff00000
59#define OHARE_LEVEL_MASK 0x1ff00000
60#define HEATHROW_LEVEL_MASK 0x1ff00000
61
62static int max_irqs;
63static int max_real_irqs;
64static u32 level_mask[4];
65
66static DEFINE_SPINLOCK(pmac_pic_lock);
67
Paul Mackerras14cf11a2005-09-26 16:04:21 +100068#define GATWICK_IRQ_POOL_SIZE 10
69static struct interrupt_info gatwick_int_pool[GATWICK_IRQ_POOL_SIZE];
70
Stephen Rothwell756e7102005-11-09 18:07:45 +110071#define NR_MASK_WORDS ((NR_IRQS + 31) / 32)
72static unsigned long ppc_lost_interrupts[NR_MASK_WORDS];
73
Paul Mackerras14cf11a2005-09-26 16:04:21 +100074/*
75 * Mark an irq as "lost". This is only used on the pmac
76 * since it can lose interrupts (see pmac_set_irq_mask).
77 * -- Cort
78 */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +110079void __set_lost(unsigned long irq_nr, int nokick)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100080{
81 if (!test_and_set_bit(irq_nr, ppc_lost_interrupts)) {
82 atomic_inc(&ppc_n_lost_interrupts);
83 if (!nokick)
84 set_dec(1);
85 }
86}
87
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +110088static void pmac_mask_and_ack_irq(unsigned int irq_nr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100089{
90 unsigned long bit = 1UL << (irq_nr & 0x1f);
91 int i = irq_nr >> 5;
92 unsigned long flags;
93
94 if ((unsigned)irq_nr >= max_irqs)
95 return;
96
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);
103 do {
104 /* make sure ack gets to controller before we enable
105 interrupts */
106 mb();
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);
110}
111
112static void pmac_set_irq_mask(unsigned int irq_nr, int nokicklost)
113{
114 unsigned long bit = 1UL << (irq_nr & 0x1f);
115 int i = irq_nr >> 5;
116 unsigned long flags;
117
118 if ((unsigned)irq_nr >= max_irqs)
119 return;
120
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]);
124
125 do {
126 /* make sure mask gets to controller before we
127 return to user */
128 mb();
129 } while((in_le32(&pmac_irq_hw[i]->enable) & bit)
130 != (ppc_cached_irq_mask[i] & bit));
131
132 /*
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.
136 */
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);
140}
141
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
144 */
145static unsigned int pmac_startup_irq(unsigned int irq_nr)
146{
147 unsigned long bit = 1UL << (irq_nr & 0x1f);
148 int i = irq_nr >> 5;
149
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);
154
155 return 0;
156}
157
158static void pmac_mask_irq(unsigned int irq_nr)
159{
160 clear_bit(irq_nr, ppc_cached_irq_mask);
161 pmac_set_irq_mask(irq_nr, 0);
162 mb();
163}
164
165static void pmac_unmask_irq(unsigned int irq_nr)
166{
167 set_bit(irq_nr, ppc_cached_irq_mask);
168 pmac_set_irq_mask(irq_nr, 0);
169}
170
171static void pmac_end_irq(unsigned int irq_nr)
172{
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);
177 }
178}
179
180
181struct 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,
187 .end = pmac_end_irq,
188};
189
190struct 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,
196 .end = pmac_end_irq,
197};
198
199static irqreturn_t gatwick_action(int cpl, void *dev_id, struct pt_regs *regs)
200{
201 int irq, bits;
202
203 for (irq = max_irqs; (irq -= 32) >= max_real_irqs; ) {
204 int i = irq >> 5;
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];
209 if (bits == 0)
210 continue;
211 irq += __ilog2(bits);
212 __do_IRQ(irq, regs);
213 return IRQ_HANDLED;
214 }
215 printk("gatwick irq not from gatwick pic\n");
216 return IRQ_NONE;
217}
218
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100219static int pmac_get_irq(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000220{
221 int irq;
222 unsigned long bits = 0;
223
224#ifdef CONFIG_SMP
225 void psurge_smp_message_recv(struct pt_regs *);
226
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 */
231 }
232#endif /* CONFIG_SMP */
233 for (irq = max_real_irqs; (irq -= 32) >= 0; ) {
234 int i = irq >> 5;
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];
239 if (bits == 0)
240 continue;
241 irq += __ilog2(bits);
242 break;
243 }
244
245 return irq;
246}
247
248/* This routine will fix some missing interrupt values in the device tree
249 * on the gatwick mac-io controller used by some PowerBooks
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100250 *
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
253 * device-tree.
254 *
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.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000258 */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100259static void __init pmac_fix_gatwick_interrupts(struct device_node *gw,
260 int irq_base)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000261{
262 struct device_node *node;
263 int count;
264
265 memset(gatwick_int_pool, 0, sizeof(gatwick_int_pool));
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000266 count = 0;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100267 for (node = NULL; (node = of_get_next_child(gw, node)) != NULL;) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000268 /* Fix SCC */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100269 if ((strcasecmp(node->name, "escc") == 0) && node->child) {
270 if (node->child->n_intrs < 3) {
271 node->child->intrs = &gatwick_int_pool[count];
272 count += 3;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000273 }
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100274 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"
279 " (%d,%d,%d)\n",
280 node->child->intrs[0].line,
281 node->child->intrs[1].line,
282 node->child->intrs[2].line);
283 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000284 /* Fix media-bay & left SWIM */
285 if (strcasecmp(node->name, "media-bay") == 0) {
286 struct device_node* ya_node;
287
288 if (node->n_intrs == 0)
289 node->intrs = &gatwick_int_pool[count++];
290 node->n_intrs = 1;
291 node->intrs[0].line = 29+irq_base;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100292 printk(KERN_INFO "irq: fixed media-bay on gatwick"
293 " (%d)\n", node->intrs[0].line);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000294
295 ya_node = node->child;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100296 while(ya_node) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000297 if (strcasecmp(ya_node->name, "floppy") == 0) {
298 if (ya_node->n_intrs < 2) {
299 ya_node->intrs = &gatwick_int_pool[count];
300 count += 2;
301 }
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);
307 }
308 if (strcasecmp(ya_node->name, "ata4") == 0) {
309 if (ya_node->n_intrs < 2) {
310 ya_node->intrs = &gatwick_int_pool[count];
311 count += 2;
312 }
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);
318 }
319 ya_node = ya_node->sibling;
320 }
321 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000322 }
323 if (count > 10) {
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);
327 }
328}
329
330/*
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.
335 */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100336static void __init enable_second_ohare(struct device_node *np)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000337{
338 unsigned char bus, devfn;
339 unsigned short cmd;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000340 struct device_node *ether;
341
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100342 /* 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.
345 */
346 if (pci_device_from_OF_node(np, &bus, &devfn) == 0) {
347 struct pci_controller* hose =
348 pci_find_hose_for_OF_device(np);
349 if (!hose) {
350 printk(KERN_ERR "Can't find PCI hose for OHare2 !\n");
351 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000352 }
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100353 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);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000357 }
358
359 /* Fix interrupt for the modem/ethernet combo controller. The number
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100360 * 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.
363 */
364 ether = of_find_node_by_name(NULL, "pci1011,14");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000365 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);
369 }
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100370 of_node_put(ether);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000371}
372
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000373#ifdef CONFIG_XMON
374static struct irqaction xmon_action = {
375 .handler = xmon_irq,
376 .flags = 0,
377 .mask = CPU_MASK_NONE,
378 .name = "NMI - XMON"
379};
380#endif
381
382static struct irqaction gatwick_cascade_action = {
383 .handler = gatwick_action,
Thomas Gleixner67144652006-07-01 19:29:22 -0700384 .flags = IRQF_DISABLED,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000385 .mask = CPU_MASK_NONE,
386 .name = "cascade",
387};
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100388
389static void __init pmac_pic_probe_oldstyle(void)
390{
391 int i;
392 int irq_cascade = -1;
393 struct device_node *master = NULL;
394 struct device_node *slave = NULL;
395 u8 __iomem *addr;
396 struct resource r;
397
398 /* Set our get_irq function */
399 ppc_md.get_irq = pmac_get_irq;
400
401 /*
402 * Find the interrupt controller type & node
403 */
404
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;
411
412 /* We might have a second cascaded ohare */
413 slave = of_find_node_by_name(NULL, "pci106b,7");
414 if (slave) {
415 max_irqs = 64;
416 level_mask[1] = OHARE_LEVEL_MASK;
417 enable_second_ohare(slave);
418 }
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;
422 level_mask[1] = 0;
423
424 /* We might have a second cascaded heathrow */
425 slave = of_find_node_by_name(master, "mac-io");
426
427 /* Check ordering of master & slave */
428 if (device_is_compatible(master, "gatwick")) {
429 struct device_node *tmp;
430 BUG_ON(slave == NULL);
431 tmp = master;
432 master = slave;
433 slave = tmp;
434 }
435
436 /* We found a slave */
437 if (slave) {
438 max_irqs = 128;
439 level_mask[2] = HEATHROW_LEVEL_MASK;
440 level_mask[3] = 0;
441 pmac_fix_gatwick_interrupts(slave, max_real_irqs);
442 }
443 }
444 BUG_ON(master == NULL);
445
446 /* Set the handler for the main PIC */
447 for ( i = 0; i < max_real_irqs ; i++ )
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700448 irq_desc[i].chip = &pmac_pic;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100449
450 /* Get addresses of first controller if we have a node for it */
451 BUG_ON(of_address_to_resource(master, 0, &r));
452
453 /* Map interrupts of primary controller */
454 addr = (u8 __iomem *) ioremap(r.start, 0x40);
455 i = 0;
456 pmac_irq_hw[i++] = (volatile struct pmac_irq_hw __iomem *)
457 (addr + 0x20);
458 if (max_real_irqs > 32)
459 pmac_irq_hw[i++] = (volatile struct pmac_irq_hw __iomem *)
460 (addr + 0x10);
461 of_node_put(master);
462
463 printk(KERN_INFO "irq: Found primary Apple PIC %s for %d irqs\n",
464 master->full_name, max_real_irqs);
465
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 *)
470 (addr + 0x20);
471 if (max_irqs > 64)
472 pmac_irq_hw[i++] =
473 (volatile struct pmac_irq_hw __iomem *)
474 (addr + 0x10);
475 irq_cascade = slave->intrs[0].line;
476
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);
480 }
481 of_node_put(slave);
482
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);
486
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;
491
492 /* Setup handlers for secondary controller and hook cascade irq*/
493 if (slave) {
494 for ( i = max_real_irqs ; i < max_irqs ; i++ )
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700495 irq_desc[i].chip = &gatwick_pic;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100496 setup_irq(irq_cascade, &gatwick_cascade_action);
497 }
498 printk(KERN_INFO "irq: System has %d possible interrupts\n", max_irqs);
499#ifdef CONFIG_XMON
500 setup_irq(20, &xmon_action);
501#endif
502}
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000503#endif /* CONFIG_PPC32 */
504
505static int pmac_u3_cascade(struct pt_regs *regs, void *data)
506{
507 return mpic_get_one_irq((struct mpic *)data, regs);
508}
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000509
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100510static void __init pmac_pic_setup_mpic_nmi(struct mpic *mpic)
511{
512#if defined(CONFIG_XMON) && defined(CONFIG_PPC32)
513 struct device_node* pswitch;
514 int nmi_irq;
515
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);
521 }
522 of_node_put(pswitch);
523#endif /* defined(CONFIG_XMON) && defined(CONFIG_PPC32) */
524}
525
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100526static struct mpic * __init pmac_setup_one_mpic(struct device_node *np,
527 int master)
528{
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 ";
533 struct resource r;
534 struct mpic *mpic;
535 unsigned int flags = master ? MPIC_PRIMARY : 0;
536 int rc;
537
538 rc = of_address_to_resource(np, 0, &r);
539 if (rc)
540 return NULL;
541
542 pmac_call_feature(PMAC_FTR_ENABLE_MPIC, np, 0, 0);
543
544 prom_get_irq_senses(senses, offset, offset + count);
545
546 flags |= MPIC_WANTS_RESET;
547 if (get_property(np, "big-endian", NULL))
548 flags |= MPIC_BIG_ENDIAN;
549
550 /* Primary Big Endian means HT interrupts. This is quite dodgy
551 * but works until I find a better way
552 */
553 if (master && (flags & MPIC_BIG_ENDIAN))
554 flags |= MPIC_BROKEN_U3;
555
556 mpic = mpic_alloc(r.start, flags, 0, offset, count, master ? 252 : 0,
557 senses, count, name);
558 if (mpic == NULL)
559 return NULL;
560
561 mpic_init(mpic);
562
563 return mpic;
564 }
565
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100566static int __init pmac_pic_probe_mpic(void)
567{
568 struct mpic *mpic1, *mpic2;
569 struct device_node *np, *master = NULL, *slave = NULL;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100570
571 /* We can have up to 2 MPICs cascaded */
572 for (np = NULL; (np = of_find_node_by_type(np, "open-pic"))
573 != NULL;) {
574 if (master == NULL &&
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100575 get_property(np, "interrupts", NULL) == NULL)
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100576 master = of_node_get(np);
577 else if (slave == NULL)
578 slave = of_node_get(np);
579 if (master && slave)
580 break;
581 }
582
583 /* Check for bogus setups */
584 if (master == NULL && slave != NULL) {
585 master = slave;
586 slave = NULL;
587 }
588
589 /* Not found, default to good old pmac pic */
590 if (master == NULL)
591 return -ENODEV;
592
593 /* Set master handler */
594 ppc_md.get_irq = mpic_get_irq;
595
596 /* Setup master */
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100597 mpic1 = pmac_setup_one_mpic(master, 1);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100598 BUG_ON(mpic1 == NULL);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100599
600 /* Install NMI if any */
601 pmac_pic_setup_mpic_nmi(mpic1);
602
603 of_node_put(master);
604
605 /* No slave, let's go out */
606 if (slave == NULL || slave->n_intrs < 1)
607 return 0;
608
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100609 mpic2 = pmac_setup_one_mpic(slave, 0);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100610 if (mpic2 == NULL) {
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100611 printk(KERN_ERR "Failed to setup slave MPIC\n");
612 of_node_put(slave);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100613 return 0;
614 }
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100615 mpic_setup_cascade(slave->intrs[0].line, pmac_u3_cascade, mpic2);
616
617 of_node_put(slave);
618 return 0;
619}
620
621
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000622void __init pmac_pic_init(void)
623{
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000624 /* 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.
626 */
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100627 if (pmac_pic_probe_mpic() == 0)
Paul Mackerras20c8c212005-09-28 20:28:14 +1000628 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000629
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000630#ifdef CONFIG_PPC32
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100631 pmac_pic_probe_oldstyle();
632#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000633}
634
Paul Mackerrasa0005032005-11-02 15:08:17 +1100635#if defined(CONFIG_PM) && defined(CONFIG_PPC32)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000636/*
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.
641 */
642unsigned long sleep_save_mask[2];
643
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...
646 */
647static int pmacpic_find_viaint(void)
648{
649 int viaint = -1;
650
651#ifdef CONFIG_ADB_PMU
652 struct device_node *np;
653
654 if (pmu_get_model() != PMU_OHARE_BASED)
655 goto not_found;
656 np = of_find_node_by_name(NULL, "via-pmu");
657 if (np == NULL)
658 goto not_found;
659 viaint = np->intrs[0].line;
660#endif /* CONFIG_ADB_PMU */
661
662not_found:
663 return viaint;
664}
665
666static int pmacpic_suspend(struct sys_device *sysdev, pm_message_t state)
667{
668 int viaint = pmacpic_find_viaint();
669
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;
674 if (viaint > 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 */
681 mb();
682 (void)in_le32(&pmac_irq_hw[0]->enable);
683
684 return 0;
685}
686
687static int pmacpic_resume(struct sys_device *sysdev)
688{
689 int i;
690
691 out_le32(&pmac_irq_hw[0]->enable, 0);
692 if (max_real_irqs > 32)
693 out_le32(&pmac_irq_hw[1]->enable, 0);
694 mb();
695 for (i = 0; i < max_real_irqs; ++i)
696 if (test_bit(i, sleep_save_mask))
697 pmac_unmask_irq(i);
698
699 return 0;
700}
701
Paul Mackerrasa0005032005-11-02 15:08:17 +1100702#endif /* CONFIG_PM && CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000703
704static struct sysdev_class pmacpic_sysclass = {
705 set_kset_name("pmac_pic"),
706};
707
708static struct sys_device device_pmacpic = {
709 .id = 0,
710 .cls = &pmacpic_sysclass,
711};
712
713static struct sysdev_driver driver_pmacpic = {
Paul Mackerrasa0005032005-11-02 15:08:17 +1100714#if defined(CONFIG_PM) && defined(CONFIG_PPC32)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000715 .suspend = &pmacpic_suspend,
716 .resume = &pmacpic_resume,
Paul Mackerrasa0005032005-11-02 15:08:17 +1100717#endif /* CONFIG_PM && CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000718};
719
720static int __init init_pmacpic_sysfs(void)
721{
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000722#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000723 if (max_irqs == 0)
724 return -ENODEV;
Paul Mackerras3c3f42d2005-10-10 22:58:41 +1000725#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000726 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);
730 return 0;
731}
732
733subsys_initcall(init_pmacpic_sysfs);
734