[PATCH] powerpc: Remove device_node addrs/n_addr
[pandora-kernel.git] / arch / powerpc / platforms / powermac / feature.c
1 /*
2  *  arch/ppc/platforms/pmac_feature.c
3  *
4  *  Copyright (C) 1996-2001 Paul Mackerras (paulus@cs.anu.edu.au)
5  *                          Ben. Herrenschmidt (benh@kernel.crashing.org)
6  *
7  *  This program is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU General Public License
9  *  as published by the Free Software Foundation; either version
10  *  2 of the License, or (at your option) any later version.
11  *
12  *  TODO:
13  *
14  *   - Replace mdelay with some schedule loop if possible
15  *   - Shorten some obfuscated delays on some routines (like modem
16  *     power)
17  *   - Refcount some clocks (see darwin)
18  *   - Split split split...
19  *
20  */
21 #include <linux/config.h>
22 #include <linux/types.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/spinlock.h>
28 #include <linux/adb.h>
29 #include <linux/pmu.h>
30 #include <linux/ioport.h>
31 #include <linux/pci.h>
32 #include <asm/sections.h>
33 #include <asm/errno.h>
34 #include <asm/ohare.h>
35 #include <asm/heathrow.h>
36 #include <asm/keylargo.h>
37 #include <asm/uninorth.h>
38 #include <asm/io.h>
39 #include <asm/prom.h>
40 #include <asm/machdep.h>
41 #include <asm/pmac_feature.h>
42 #include <asm/dbdma.h>
43 #include <asm/pci-bridge.h>
44 #include <asm/pmac_low_i2c.h>
45
46 #undef DEBUG_FEATURE
47
48 #ifdef DEBUG_FEATURE
49 #define DBG(fmt...) printk(KERN_DEBUG fmt)
50 #else
51 #define DBG(fmt...)
52 #endif
53
54 #ifdef CONFIG_6xx
55 extern int powersave_lowspeed;
56 #endif
57
58 extern int powersave_nap;
59 extern struct device_node *k2_skiplist[2];
60
61
62 /*
63  * We use a single global lock to protect accesses. Each driver has
64  * to take care of its own locking
65  */
66 static DEFINE_SPINLOCK(feature_lock);
67
68 #define LOCK(flags)     spin_lock_irqsave(&feature_lock, flags);
69 #define UNLOCK(flags)   spin_unlock_irqrestore(&feature_lock, flags);
70
71
72 /*
73  * Instance of some macio stuffs
74  */
75 struct macio_chip macio_chips[MAX_MACIO_CHIPS];
76
77 struct macio_chip *macio_find(struct device_node *child, int type)
78 {
79         while(child) {
80                 int     i;
81
82                 for (i=0; i < MAX_MACIO_CHIPS && macio_chips[i].of_node; i++)
83                         if (child == macio_chips[i].of_node &&
84                             (!type || macio_chips[i].type == type))
85                                 return &macio_chips[i];
86                 child = child->parent;
87         }
88         return NULL;
89 }
90 EXPORT_SYMBOL_GPL(macio_find);
91
92 static const char *macio_names[] =
93 {
94         "Unknown",
95         "Grand Central",
96         "OHare",
97         "OHareII",
98         "Heathrow",
99         "Gatwick",
100         "Paddington",
101         "Keylargo",
102         "Pangea",
103         "Intrepid",
104         "K2"
105 };
106
107
108
109 /*
110  * Uninorth reg. access. Note that Uni-N regs are big endian
111  */
112
113 #define UN_REG(r)       (uninorth_base + ((r) >> 2))
114 #define UN_IN(r)        (in_be32(UN_REG(r)))
115 #define UN_OUT(r,v)     (out_be32(UN_REG(r), (v)))
116 #define UN_BIS(r,v)     (UN_OUT((r), UN_IN(r) | (v)))
117 #define UN_BIC(r,v)     (UN_OUT((r), UN_IN(r) & ~(v)))
118
119 static struct device_node *uninorth_node;
120 static u32 __iomem *uninorth_base;
121 static u32 uninorth_rev;
122 static int uninorth_u3;
123 static void __iomem *u3_ht;
124
125 /*
126  * For each motherboard family, we have a table of functions pointers
127  * that handle the various features.
128  */
129
130 typedef long (*feature_call)(struct device_node *node, long param, long value);
131
132 struct feature_table_entry {
133         unsigned int    selector;
134         feature_call    function;
135 };
136
137 struct pmac_mb_def
138 {
139         const char*                     model_string;
140         const char*                     model_name;
141         int                             model_id;
142         struct feature_table_entry*     features;
143         unsigned long                   board_flags;
144 };
145 static struct pmac_mb_def pmac_mb;
146
147 /*
148  * Here are the chip specific feature functions
149  */
150
151 static inline int simple_feature_tweak(struct device_node *node, int type,
152                                        int reg, u32 mask, int value)
153 {
154         struct macio_chip*      macio;
155         unsigned long           flags;
156
157         macio = macio_find(node, type);
158         if (!macio)
159                 return -ENODEV;
160         LOCK(flags);
161         if (value)
162                 MACIO_BIS(reg, mask);
163         else
164                 MACIO_BIC(reg, mask);
165         (void)MACIO_IN32(reg);
166         UNLOCK(flags);
167
168         return 0;
169 }
170
171 #ifndef CONFIG_POWER4
172
173 static long ohare_htw_scc_enable(struct device_node *node, long param,
174                                  long value)
175 {
176         struct macio_chip*      macio;
177         unsigned long           chan_mask;
178         unsigned long           fcr;
179         unsigned long           flags;
180         int                     htw, trans;
181         unsigned long           rmask;
182
183         macio = macio_find(node, 0);
184         if (!macio)
185                 return -ENODEV;
186         if (!strcmp(node->name, "ch-a"))
187                 chan_mask = MACIO_FLAG_SCCA_ON;
188         else if (!strcmp(node->name, "ch-b"))
189                 chan_mask = MACIO_FLAG_SCCB_ON;
190         else
191                 return -ENODEV;
192
193         htw = (macio->type == macio_heathrow || macio->type == macio_paddington
194                 || macio->type == macio_gatwick);
195         /* On these machines, the HRW_SCC_TRANS_EN_N bit mustn't be touched */
196         trans = (pmac_mb.model_id != PMAC_TYPE_YOSEMITE &&
197                  pmac_mb.model_id != PMAC_TYPE_YIKES);
198         if (value) {
199 #ifdef CONFIG_ADB_PMU
200                 if ((param & 0xfff) == PMAC_SCC_IRDA)
201                         pmu_enable_irled(1);
202 #endif /* CONFIG_ADB_PMU */
203                 LOCK(flags);
204                 fcr = MACIO_IN32(OHARE_FCR);
205                 /* Check if scc cell need enabling */
206                 if (!(fcr & OH_SCC_ENABLE)) {
207                         fcr |= OH_SCC_ENABLE;
208                         if (htw) {
209                                 /* Side effect: this will also power up the
210                                  * modem, but it's too messy to figure out on which
211                                  * ports this controls the tranceiver and on which
212                                  * it controls the modem
213                                  */
214                                 if (trans)
215                                         fcr &= ~HRW_SCC_TRANS_EN_N;
216                                 MACIO_OUT32(OHARE_FCR, fcr);
217                                 fcr |= (rmask = HRW_RESET_SCC);
218                                 MACIO_OUT32(OHARE_FCR, fcr);
219                         } else {
220                                 fcr |= (rmask = OH_SCC_RESET);
221                                 MACIO_OUT32(OHARE_FCR, fcr);
222                         }
223                         UNLOCK(flags);
224                         (void)MACIO_IN32(OHARE_FCR);
225                         mdelay(15);
226                         LOCK(flags);
227                         fcr &= ~rmask;
228                         MACIO_OUT32(OHARE_FCR, fcr);
229                 }
230                 if (chan_mask & MACIO_FLAG_SCCA_ON)
231                         fcr |= OH_SCCA_IO;
232                 if (chan_mask & MACIO_FLAG_SCCB_ON)
233                         fcr |= OH_SCCB_IO;
234                 MACIO_OUT32(OHARE_FCR, fcr);
235                 macio->flags |= chan_mask;
236                 UNLOCK(flags);
237                 if (param & PMAC_SCC_FLAG_XMON)
238                         macio->flags |= MACIO_FLAG_SCC_LOCKED;
239         } else {
240                 if (macio->flags & MACIO_FLAG_SCC_LOCKED)
241                         return -EPERM;
242                 LOCK(flags);
243                 fcr = MACIO_IN32(OHARE_FCR);
244                 if (chan_mask & MACIO_FLAG_SCCA_ON)
245                         fcr &= ~OH_SCCA_IO;
246                 if (chan_mask & MACIO_FLAG_SCCB_ON)
247                         fcr &= ~OH_SCCB_IO;
248                 MACIO_OUT32(OHARE_FCR, fcr);
249                 if ((fcr & (OH_SCCA_IO | OH_SCCB_IO)) == 0) {
250                         fcr &= ~OH_SCC_ENABLE;
251                         if (htw && trans)
252                                 fcr |= HRW_SCC_TRANS_EN_N;
253                         MACIO_OUT32(OHARE_FCR, fcr);
254                 }
255                 macio->flags &= ~(chan_mask);
256                 UNLOCK(flags);
257                 mdelay(10);
258 #ifdef CONFIG_ADB_PMU
259                 if ((param & 0xfff) == PMAC_SCC_IRDA)
260                         pmu_enable_irled(0);
261 #endif /* CONFIG_ADB_PMU */
262         }
263         return 0;
264 }
265
266 static long ohare_floppy_enable(struct device_node *node, long param,
267                                 long value)
268 {
269         return simple_feature_tweak(node, macio_ohare,
270                 OHARE_FCR, OH_FLOPPY_ENABLE, value);
271 }
272
273 static long ohare_mesh_enable(struct device_node *node, long param, long value)
274 {
275         return simple_feature_tweak(node, macio_ohare,
276                 OHARE_FCR, OH_MESH_ENABLE, value);
277 }
278
279 static long ohare_ide_enable(struct device_node *node, long param, long value)
280 {
281         switch(param) {
282         case 0:
283                 /* For some reason, setting the bit in set_initial_features()
284                  * doesn't stick. I'm still investigating... --BenH.
285                  */
286                 if (value)
287                         simple_feature_tweak(node, macio_ohare,
288                                 OHARE_FCR, OH_IOBUS_ENABLE, 1);
289                 return simple_feature_tweak(node, macio_ohare,
290                         OHARE_FCR, OH_IDE0_ENABLE, value);
291         case 1:
292                 return simple_feature_tweak(node, macio_ohare,
293                         OHARE_FCR, OH_BAY_IDE_ENABLE, value);
294         default:
295                 return -ENODEV;
296         }
297 }
298
299 static long ohare_ide_reset(struct device_node *node, long param, long value)
300 {
301         switch(param) {
302         case 0:
303                 return simple_feature_tweak(node, macio_ohare,
304                         OHARE_FCR, OH_IDE0_RESET_N, !value);
305         case 1:
306                 return simple_feature_tweak(node, macio_ohare,
307                         OHARE_FCR, OH_IDE1_RESET_N, !value);
308         default:
309                 return -ENODEV;
310         }
311 }
312
313 static long ohare_sleep_state(struct device_node *node, long param, long value)
314 {
315         struct macio_chip*      macio = &macio_chips[0];
316
317         if ((pmac_mb.board_flags & PMAC_MB_CAN_SLEEP) == 0)
318                 return -EPERM;
319         if (value == 1) {
320                 MACIO_BIC(OHARE_FCR, OH_IOBUS_ENABLE);
321         } else if (value == 0) {
322                 MACIO_BIS(OHARE_FCR, OH_IOBUS_ENABLE);
323         }
324
325         return 0;
326 }
327
328 static long heathrow_modem_enable(struct device_node *node, long param,
329                                   long value)
330 {
331         struct macio_chip*      macio;
332         u8                      gpio;
333         unsigned long           flags;
334
335         macio = macio_find(node, macio_unknown);
336         if (!macio)
337                 return -ENODEV;
338         gpio = MACIO_IN8(HRW_GPIO_MODEM_RESET) & ~1;
339         if (!value) {
340                 LOCK(flags);
341                 MACIO_OUT8(HRW_GPIO_MODEM_RESET, gpio);
342                 UNLOCK(flags);
343                 (void)MACIO_IN8(HRW_GPIO_MODEM_RESET);
344                 mdelay(250);
345         }
346         if (pmac_mb.model_id != PMAC_TYPE_YOSEMITE &&
347             pmac_mb.model_id != PMAC_TYPE_YIKES) {
348                 LOCK(flags);
349                 if (value)
350                         MACIO_BIC(HEATHROW_FCR, HRW_SCC_TRANS_EN_N);
351                 else
352                         MACIO_BIS(HEATHROW_FCR, HRW_SCC_TRANS_EN_N);
353                 UNLOCK(flags);
354                 (void)MACIO_IN32(HEATHROW_FCR);
355                 mdelay(250);
356         }
357         if (value) {
358                 LOCK(flags);
359                 MACIO_OUT8(HRW_GPIO_MODEM_RESET, gpio | 1);
360                 (void)MACIO_IN8(HRW_GPIO_MODEM_RESET);
361                 UNLOCK(flags); mdelay(250); LOCK(flags);
362                 MACIO_OUT8(HRW_GPIO_MODEM_RESET, gpio);
363                 (void)MACIO_IN8(HRW_GPIO_MODEM_RESET);
364                 UNLOCK(flags); mdelay(250); LOCK(flags);
365                 MACIO_OUT8(HRW_GPIO_MODEM_RESET, gpio | 1);
366                 (void)MACIO_IN8(HRW_GPIO_MODEM_RESET);
367                 UNLOCK(flags); mdelay(250);
368         }
369         return 0;
370 }
371
372 static long heathrow_floppy_enable(struct device_node *node, long param,
373                                    long value)
374 {
375         return simple_feature_tweak(node, macio_unknown,
376                 HEATHROW_FCR,
377                 HRW_SWIM_ENABLE|HRW_BAY_FLOPPY_ENABLE,
378                 value);
379 }
380
381 static long heathrow_mesh_enable(struct device_node *node, long param,
382                                  long value)
383 {
384         struct macio_chip*      macio;
385         unsigned long           flags;
386
387         macio = macio_find(node, macio_unknown);
388         if (!macio)
389                 return -ENODEV;
390         LOCK(flags);
391         /* Set clear mesh cell enable */
392         if (value)
393                 MACIO_BIS(HEATHROW_FCR, HRW_MESH_ENABLE);
394         else
395                 MACIO_BIC(HEATHROW_FCR, HRW_MESH_ENABLE);
396         (void)MACIO_IN32(HEATHROW_FCR);
397         udelay(10);
398         /* Set/Clear termination power */
399         if (value)
400                 MACIO_BIC(HEATHROW_MBCR, 0x04000000);
401         else
402                 MACIO_BIS(HEATHROW_MBCR, 0x04000000);
403         (void)MACIO_IN32(HEATHROW_MBCR);
404         udelay(10);
405         UNLOCK(flags);
406
407         return 0;
408 }
409
410 static long heathrow_ide_enable(struct device_node *node, long param,
411                                 long value)
412 {
413         switch(param) {
414         case 0:
415                 return simple_feature_tweak(node, macio_unknown,
416                         HEATHROW_FCR, HRW_IDE0_ENABLE, value);
417         case 1:
418                 return simple_feature_tweak(node, macio_unknown,
419                         HEATHROW_FCR, HRW_BAY_IDE_ENABLE, value);
420         default:
421                 return -ENODEV;
422         }
423 }
424
425 static long heathrow_ide_reset(struct device_node *node, long param,
426                                long value)
427 {
428         switch(param) {
429         case 0:
430                 return simple_feature_tweak(node, macio_unknown,
431                         HEATHROW_FCR, HRW_IDE0_RESET_N, !value);
432         case 1:
433                 return simple_feature_tweak(node, macio_unknown,
434                         HEATHROW_FCR, HRW_IDE1_RESET_N, !value);
435         default:
436                 return -ENODEV;
437         }
438 }
439
440 static long heathrow_bmac_enable(struct device_node *node, long param,
441                                  long value)
442 {
443         struct macio_chip*      macio;
444         unsigned long           flags;
445
446         macio = macio_find(node, 0);
447         if (!macio)
448                 return -ENODEV;
449         if (value) {
450                 LOCK(flags);
451                 MACIO_BIS(HEATHROW_FCR, HRW_BMAC_IO_ENABLE);
452                 MACIO_BIS(HEATHROW_FCR, HRW_BMAC_RESET);
453                 UNLOCK(flags);
454                 (void)MACIO_IN32(HEATHROW_FCR);
455                 mdelay(10);
456                 LOCK(flags);
457                 MACIO_BIC(HEATHROW_FCR, HRW_BMAC_RESET);
458                 UNLOCK(flags);
459                 (void)MACIO_IN32(HEATHROW_FCR);
460                 mdelay(10);
461         } else {
462                 LOCK(flags);
463                 MACIO_BIC(HEATHROW_FCR, HRW_BMAC_IO_ENABLE);
464                 UNLOCK(flags);
465         }
466         return 0;
467 }
468
469 static long heathrow_sound_enable(struct device_node *node, long param,
470                                   long value)
471 {
472         struct macio_chip*      macio;
473         unsigned long           flags;
474
475         /* B&W G3 and Yikes don't support that properly (the
476          * sound appear to never come back after beeing shut down).
477          */
478         if (pmac_mb.model_id == PMAC_TYPE_YOSEMITE ||
479             pmac_mb.model_id == PMAC_TYPE_YIKES)
480                 return 0;
481
482         macio = macio_find(node, 0);
483         if (!macio)
484                 return -ENODEV;
485         if (value) {
486                 LOCK(flags);
487                 MACIO_BIS(HEATHROW_FCR, HRW_SOUND_CLK_ENABLE);
488                 MACIO_BIC(HEATHROW_FCR, HRW_SOUND_POWER_N);
489                 UNLOCK(flags);
490                 (void)MACIO_IN32(HEATHROW_FCR);
491         } else {
492                 LOCK(flags);
493                 MACIO_BIS(HEATHROW_FCR, HRW_SOUND_POWER_N);
494                 MACIO_BIC(HEATHROW_FCR, HRW_SOUND_CLK_ENABLE);
495                 UNLOCK(flags);
496         }
497         return 0;
498 }
499
500 static u32 save_fcr[6];
501 static u32 save_mbcr;
502 static u32 save_gpio_levels[2];
503 static u8 save_gpio_extint[KEYLARGO_GPIO_EXTINT_CNT];
504 static u8 save_gpio_normal[KEYLARGO_GPIO_CNT];
505 static u32 save_unin_clock_ctl;
506 static struct dbdma_regs save_dbdma[13];
507 static struct dbdma_regs save_alt_dbdma[13];
508
509 static void dbdma_save(struct macio_chip *macio, struct dbdma_regs *save)
510 {
511         int i;
512
513         /* Save state & config of DBDMA channels */
514         for (i = 0; i < 13; i++) {
515                 volatile struct dbdma_regs __iomem * chan = (void __iomem *)
516                         (macio->base + ((0x8000+i*0x100)>>2));
517                 save[i].cmdptr_hi = in_le32(&chan->cmdptr_hi);
518                 save[i].cmdptr = in_le32(&chan->cmdptr);
519                 save[i].intr_sel = in_le32(&chan->intr_sel);
520                 save[i].br_sel = in_le32(&chan->br_sel);
521                 save[i].wait_sel = in_le32(&chan->wait_sel);
522         }
523 }
524
525 static void dbdma_restore(struct macio_chip *macio, struct dbdma_regs *save)
526 {
527         int i;
528
529         /* Save state & config of DBDMA channels */
530         for (i = 0; i < 13; i++) {
531                 volatile struct dbdma_regs __iomem * chan = (void __iomem *)
532                         (macio->base + ((0x8000+i*0x100)>>2));
533                 out_le32(&chan->control, (ACTIVE|DEAD|WAKE|FLUSH|PAUSE|RUN)<<16);
534                 while (in_le32(&chan->status) & ACTIVE)
535                         mb();
536                 out_le32(&chan->cmdptr_hi, save[i].cmdptr_hi);
537                 out_le32(&chan->cmdptr, save[i].cmdptr);
538                 out_le32(&chan->intr_sel, save[i].intr_sel);
539                 out_le32(&chan->br_sel, save[i].br_sel);
540                 out_le32(&chan->wait_sel, save[i].wait_sel);
541         }
542 }
543
544 static void heathrow_sleep(struct macio_chip *macio, int secondary)
545 {
546         if (secondary) {
547                 dbdma_save(macio, save_alt_dbdma);
548                 save_fcr[2] = MACIO_IN32(0x38);
549                 save_fcr[3] = MACIO_IN32(0x3c);
550         } else {
551                 dbdma_save(macio, save_dbdma);
552                 save_fcr[0] = MACIO_IN32(0x38);
553                 save_fcr[1] = MACIO_IN32(0x3c);
554                 save_mbcr = MACIO_IN32(0x34);
555                 /* Make sure sound is shut down */
556                 MACIO_BIS(HEATHROW_FCR, HRW_SOUND_POWER_N);
557                 MACIO_BIC(HEATHROW_FCR, HRW_SOUND_CLK_ENABLE);
558                 /* This seems to be necessary as well or the fan
559                  * keeps coming up and battery drains fast */
560                 MACIO_BIC(HEATHROW_FCR, HRW_IOBUS_ENABLE);
561                 MACIO_BIC(HEATHROW_FCR, HRW_IDE0_RESET_N);
562                 /* Make sure eth is down even if module or sleep
563                  * won't work properly */
564                 MACIO_BIC(HEATHROW_FCR, HRW_BMAC_IO_ENABLE | HRW_BMAC_RESET);
565         }
566         /* Make sure modem is shut down */
567         MACIO_OUT8(HRW_GPIO_MODEM_RESET,
568                 MACIO_IN8(HRW_GPIO_MODEM_RESET) & ~1);
569         MACIO_BIS(HEATHROW_FCR, HRW_SCC_TRANS_EN_N);
570         MACIO_BIC(HEATHROW_FCR, OH_SCCA_IO|OH_SCCB_IO|HRW_SCC_ENABLE);
571
572         /* Let things settle */
573         (void)MACIO_IN32(HEATHROW_FCR);
574 }
575
576 static void heathrow_wakeup(struct macio_chip *macio, int secondary)
577 {
578         if (secondary) {
579                 MACIO_OUT32(0x38, save_fcr[2]);
580                 (void)MACIO_IN32(0x38);
581                 mdelay(1);
582                 MACIO_OUT32(0x3c, save_fcr[3]);
583                 (void)MACIO_IN32(0x38);
584                 mdelay(10);
585                 dbdma_restore(macio, save_alt_dbdma);
586         } else {
587                 MACIO_OUT32(0x38, save_fcr[0] | HRW_IOBUS_ENABLE);
588                 (void)MACIO_IN32(0x38);
589                 mdelay(1);
590                 MACIO_OUT32(0x3c, save_fcr[1]);
591                 (void)MACIO_IN32(0x38);
592                 mdelay(1);
593                 MACIO_OUT32(0x34, save_mbcr);
594                 (void)MACIO_IN32(0x38);
595                 mdelay(10);
596                 dbdma_restore(macio, save_dbdma);
597         }
598 }
599
600 static long heathrow_sleep_state(struct device_node *node, long param,
601                                  long value)
602 {
603         if ((pmac_mb.board_flags & PMAC_MB_CAN_SLEEP) == 0)
604                 return -EPERM;
605         if (value == 1) {
606                 if (macio_chips[1].type == macio_gatwick)
607                         heathrow_sleep(&macio_chips[0], 1);
608                 heathrow_sleep(&macio_chips[0], 0);
609         } else if (value == 0) {
610                 heathrow_wakeup(&macio_chips[0], 0);
611                 if (macio_chips[1].type == macio_gatwick)
612                         heathrow_wakeup(&macio_chips[0], 1);
613         }
614         return 0;
615 }
616
617 static long core99_scc_enable(struct device_node *node, long param, long value)
618 {
619         struct macio_chip*      macio;
620         unsigned long           flags;
621         unsigned long           chan_mask;
622         u32                     fcr;
623
624         macio = macio_find(node, 0);
625         if (!macio)
626                 return -ENODEV;
627         if (!strcmp(node->name, "ch-a"))
628                 chan_mask = MACIO_FLAG_SCCA_ON;
629         else if (!strcmp(node->name, "ch-b"))
630                 chan_mask = MACIO_FLAG_SCCB_ON;
631         else
632                 return -ENODEV;
633
634         if (value) {
635                 int need_reset_scc = 0;
636                 int need_reset_irda = 0;
637
638                 LOCK(flags);
639                 fcr = MACIO_IN32(KEYLARGO_FCR0);
640                 /* Check if scc cell need enabling */
641                 if (!(fcr & KL0_SCC_CELL_ENABLE)) {
642                         fcr |= KL0_SCC_CELL_ENABLE;
643                         need_reset_scc = 1;
644                 }
645                 if (chan_mask & MACIO_FLAG_SCCA_ON) {
646                         fcr |= KL0_SCCA_ENABLE;
647                         /* Don't enable line drivers for I2S modem */
648                         if ((param & 0xfff) == PMAC_SCC_I2S1)
649                                 fcr &= ~KL0_SCC_A_INTF_ENABLE;
650                         else
651                                 fcr |= KL0_SCC_A_INTF_ENABLE;
652                 }
653                 if (chan_mask & MACIO_FLAG_SCCB_ON) {
654                         fcr |= KL0_SCCB_ENABLE;
655                         /* Perform irda specific inits */
656                         if ((param & 0xfff) == PMAC_SCC_IRDA) {
657                                 fcr &= ~KL0_SCC_B_INTF_ENABLE;
658                                 fcr |= KL0_IRDA_ENABLE;
659                                 fcr |= KL0_IRDA_CLK32_ENABLE | KL0_IRDA_CLK19_ENABLE;
660                                 fcr |= KL0_IRDA_SOURCE1_SEL;
661                                 fcr &= ~(KL0_IRDA_FAST_CONNECT|KL0_IRDA_DEFAULT1|KL0_IRDA_DEFAULT0);
662                                 fcr &= ~(KL0_IRDA_SOURCE2_SEL|KL0_IRDA_HIGH_BAND);
663                                 need_reset_irda = 1;
664                         } else
665                                 fcr |= KL0_SCC_B_INTF_ENABLE;
666                 }
667                 MACIO_OUT32(KEYLARGO_FCR0, fcr);
668                 macio->flags |= chan_mask;
669                 if (need_reset_scc)  {
670                         MACIO_BIS(KEYLARGO_FCR0, KL0_SCC_RESET);
671                         (void)MACIO_IN32(KEYLARGO_FCR0);
672                         UNLOCK(flags);
673                         mdelay(15);
674                         LOCK(flags);
675                         MACIO_BIC(KEYLARGO_FCR0, KL0_SCC_RESET);
676                 }
677                 if (need_reset_irda)  {
678                         MACIO_BIS(KEYLARGO_FCR0, KL0_IRDA_RESET);
679                         (void)MACIO_IN32(KEYLARGO_FCR0);
680                         UNLOCK(flags);
681                         mdelay(15);
682                         LOCK(flags);
683                         MACIO_BIC(KEYLARGO_FCR0, KL0_IRDA_RESET);
684                 }
685                 UNLOCK(flags);
686                 if (param & PMAC_SCC_FLAG_XMON)
687                         macio->flags |= MACIO_FLAG_SCC_LOCKED;
688         } else {
689                 if (macio->flags & MACIO_FLAG_SCC_LOCKED)
690                         return -EPERM;
691                 LOCK(flags);
692                 fcr = MACIO_IN32(KEYLARGO_FCR0);
693                 if (chan_mask & MACIO_FLAG_SCCA_ON)
694                         fcr &= ~KL0_SCCA_ENABLE;
695                 if (chan_mask & MACIO_FLAG_SCCB_ON) {
696                         fcr &= ~KL0_SCCB_ENABLE;
697                         /* Perform irda specific clears */
698                         if ((param & 0xfff) == PMAC_SCC_IRDA) {
699                                 fcr &= ~KL0_IRDA_ENABLE;
700                                 fcr &= ~(KL0_IRDA_CLK32_ENABLE | KL0_IRDA_CLK19_ENABLE);
701                                 fcr &= ~(KL0_IRDA_FAST_CONNECT|KL0_IRDA_DEFAULT1|KL0_IRDA_DEFAULT0);
702                                 fcr &= ~(KL0_IRDA_SOURCE1_SEL|KL0_IRDA_SOURCE2_SEL|KL0_IRDA_HIGH_BAND);
703                         }
704                 }
705                 MACIO_OUT32(KEYLARGO_FCR0, fcr);
706                 if ((fcr & (KL0_SCCA_ENABLE | KL0_SCCB_ENABLE)) == 0) {
707                         fcr &= ~KL0_SCC_CELL_ENABLE;
708                         MACIO_OUT32(KEYLARGO_FCR0, fcr);
709                 }
710                 macio->flags &= ~(chan_mask);
711                 UNLOCK(flags);
712                 mdelay(10);
713         }
714         return 0;
715 }
716
717 static long
718 core99_modem_enable(struct device_node *node, long param, long value)
719 {
720         struct macio_chip*      macio;
721         u8                      gpio;
722         unsigned long           flags;
723
724         /* Hack for internal USB modem */
725         if (node == NULL) {
726                 if (macio_chips[0].type != macio_keylargo)
727                         return -ENODEV;
728                 node = macio_chips[0].of_node;
729         }
730         macio = macio_find(node, 0);
731         if (!macio)
732                 return -ENODEV;
733         gpio = MACIO_IN8(KL_GPIO_MODEM_RESET);
734         gpio |= KEYLARGO_GPIO_OUTPUT_ENABLE;
735         gpio &= ~KEYLARGO_GPIO_OUTOUT_DATA;
736
737         if (!value) {
738                 LOCK(flags);
739                 MACIO_OUT8(KL_GPIO_MODEM_RESET, gpio);
740                 UNLOCK(flags);
741                 (void)MACIO_IN8(KL_GPIO_MODEM_RESET);
742                 mdelay(250);
743         }
744         LOCK(flags);
745         if (value) {
746                 MACIO_BIC(KEYLARGO_FCR2, KL2_ALT_DATA_OUT);
747                 UNLOCK(flags);
748                 (void)MACIO_IN32(KEYLARGO_FCR2);
749                 mdelay(250);
750         } else {
751                 MACIO_BIS(KEYLARGO_FCR2, KL2_ALT_DATA_OUT);
752                 UNLOCK(flags);
753         }
754         if (value) {
755                 LOCK(flags);
756                 MACIO_OUT8(KL_GPIO_MODEM_RESET, gpio | KEYLARGO_GPIO_OUTOUT_DATA);
757                 (void)MACIO_IN8(KL_GPIO_MODEM_RESET);
758                 UNLOCK(flags); mdelay(250); LOCK(flags);
759                 MACIO_OUT8(KL_GPIO_MODEM_RESET, gpio);
760                 (void)MACIO_IN8(KL_GPIO_MODEM_RESET);
761                 UNLOCK(flags); mdelay(250); LOCK(flags);
762                 MACIO_OUT8(KL_GPIO_MODEM_RESET, gpio | KEYLARGO_GPIO_OUTOUT_DATA);
763                 (void)MACIO_IN8(KL_GPIO_MODEM_RESET);
764                 UNLOCK(flags); mdelay(250);
765         }
766         return 0;
767 }
768
769 static long
770 pangea_modem_enable(struct device_node *node, long param, long value)
771 {
772         struct macio_chip*      macio;
773         u8                      gpio;
774         unsigned long           flags;
775
776         /* Hack for internal USB modem */
777         if (node == NULL) {
778                 if (macio_chips[0].type != macio_pangea &&
779                     macio_chips[0].type != macio_intrepid)
780                         return -ENODEV;
781                 node = macio_chips[0].of_node;
782         }
783         macio = macio_find(node, 0);
784         if (!macio)
785                 return -ENODEV;
786         gpio = MACIO_IN8(KL_GPIO_MODEM_RESET);
787         gpio |= KEYLARGO_GPIO_OUTPUT_ENABLE;
788         gpio &= ~KEYLARGO_GPIO_OUTOUT_DATA;
789
790         if (!value) {
791                 LOCK(flags);
792                 MACIO_OUT8(KL_GPIO_MODEM_RESET, gpio);
793                 UNLOCK(flags);
794                 (void)MACIO_IN8(KL_GPIO_MODEM_RESET);
795                 mdelay(250);
796         }
797         LOCK(flags);
798         if (value) {
799                 MACIO_OUT8(KL_GPIO_MODEM_POWER,
800                         KEYLARGO_GPIO_OUTPUT_ENABLE);
801                 UNLOCK(flags);
802                 (void)MACIO_IN32(KEYLARGO_FCR2);
803                 mdelay(250);
804         } else {
805                 MACIO_OUT8(KL_GPIO_MODEM_POWER,
806                         KEYLARGO_GPIO_OUTPUT_ENABLE | KEYLARGO_GPIO_OUTOUT_DATA);
807                 UNLOCK(flags);
808         }
809         if (value) {
810                 LOCK(flags);
811                 MACIO_OUT8(KL_GPIO_MODEM_RESET, gpio | KEYLARGO_GPIO_OUTOUT_DATA);
812                 (void)MACIO_IN8(KL_GPIO_MODEM_RESET);
813                 UNLOCK(flags); mdelay(250); LOCK(flags);
814                 MACIO_OUT8(KL_GPIO_MODEM_RESET, gpio);
815                 (void)MACIO_IN8(KL_GPIO_MODEM_RESET);
816                 UNLOCK(flags); mdelay(250); LOCK(flags);
817                 MACIO_OUT8(KL_GPIO_MODEM_RESET, gpio | KEYLARGO_GPIO_OUTOUT_DATA);
818                 (void)MACIO_IN8(KL_GPIO_MODEM_RESET);
819                 UNLOCK(flags); mdelay(250);
820         }
821         return 0;
822 }
823
824 static long
825 core99_ata100_enable(struct device_node *node, long value)
826 {
827         unsigned long flags;
828         struct pci_dev *pdev = NULL;
829         u8 pbus, pid;
830
831         if (uninorth_rev < 0x24)
832                 return -ENODEV;
833
834         LOCK(flags);
835         if (value)
836                 UN_BIS(UNI_N_CLOCK_CNTL, UNI_N_CLOCK_CNTL_ATA100);
837         else
838                 UN_BIC(UNI_N_CLOCK_CNTL, UNI_N_CLOCK_CNTL_ATA100);
839         (void)UN_IN(UNI_N_CLOCK_CNTL);
840         UNLOCK(flags);
841         udelay(20);
842
843         if (value) {
844                 if (pci_device_from_OF_node(node, &pbus, &pid) == 0)
845                         pdev = pci_find_slot(pbus, pid);
846                 if (pdev == NULL)
847                         return 0;
848                 pci_enable_device(pdev);
849                 pci_set_master(pdev);
850         }
851         return 0;
852 }
853
854 static long
855 core99_ide_enable(struct device_node *node, long param, long value)
856 {
857         /* Bus ID 0 to 2 are KeyLargo based IDE, busID 3 is U2
858          * based ata-100
859          */
860         switch(param) {
861             case 0:
862                 return simple_feature_tweak(node, macio_unknown,
863                         KEYLARGO_FCR1, KL1_EIDE0_ENABLE, value);
864             case 1:
865                 return simple_feature_tweak(node, macio_unknown,
866                         KEYLARGO_FCR1, KL1_EIDE1_ENABLE, value);
867             case 2:
868                 return simple_feature_tweak(node, macio_unknown,
869                         KEYLARGO_FCR1, KL1_UIDE_ENABLE, value);
870             case 3:
871                 return core99_ata100_enable(node, value);
872             default:
873                 return -ENODEV;
874         }
875 }
876
877 static long
878 core99_ide_reset(struct device_node *node, long param, long value)
879 {
880         switch(param) {
881             case 0:
882                 return simple_feature_tweak(node, macio_unknown,
883                         KEYLARGO_FCR1, KL1_EIDE0_RESET_N, !value);
884             case 1:
885                 return simple_feature_tweak(node, macio_unknown,
886                         KEYLARGO_FCR1, KL1_EIDE1_RESET_N, !value);
887             case 2:
888                 return simple_feature_tweak(node, macio_unknown,
889                         KEYLARGO_FCR1, KL1_UIDE_RESET_N, !value);
890             default:
891                 return -ENODEV;
892         }
893 }
894
895 static long
896 core99_gmac_enable(struct device_node *node, long param, long value)
897 {
898         unsigned long flags;
899
900         LOCK(flags);
901         if (value)
902                 UN_BIS(UNI_N_CLOCK_CNTL, UNI_N_CLOCK_CNTL_GMAC);
903         else
904                 UN_BIC(UNI_N_CLOCK_CNTL, UNI_N_CLOCK_CNTL_GMAC);
905         (void)UN_IN(UNI_N_CLOCK_CNTL);
906         UNLOCK(flags);
907         udelay(20);
908
909         return 0;
910 }
911
912 static long
913 core99_gmac_phy_reset(struct device_node *node, long param, long value)
914 {
915         unsigned long flags;
916         struct macio_chip *macio;
917
918         macio = &macio_chips[0];
919         if (macio->type != macio_keylargo && macio->type != macio_pangea &&
920             macio->type != macio_intrepid)
921                 return -ENODEV;
922
923         LOCK(flags);
924         MACIO_OUT8(KL_GPIO_ETH_PHY_RESET, KEYLARGO_GPIO_OUTPUT_ENABLE);
925         (void)MACIO_IN8(KL_GPIO_ETH_PHY_RESET);
926         UNLOCK(flags);
927         mdelay(10);
928         LOCK(flags);
929         MACIO_OUT8(KL_GPIO_ETH_PHY_RESET, /*KEYLARGO_GPIO_OUTPUT_ENABLE | */
930                 KEYLARGO_GPIO_OUTOUT_DATA);
931         UNLOCK(flags);
932         mdelay(10);
933
934         return 0;
935 }
936
937 static long
938 core99_sound_chip_enable(struct device_node *node, long param, long value)
939 {
940         struct macio_chip*      macio;
941         unsigned long           flags;
942
943         macio = macio_find(node, 0);
944         if (!macio)
945                 return -ENODEV;
946
947         /* Do a better probe code, screamer G4 desktops &
948          * iMacs can do that too, add a recalibrate  in
949          * the driver as well
950          */
951         if (pmac_mb.model_id == PMAC_TYPE_PISMO ||
952             pmac_mb.model_id == PMAC_TYPE_TITANIUM) {
953                 LOCK(flags);
954                 if (value)
955                         MACIO_OUT8(KL_GPIO_SOUND_POWER,
956                                 KEYLARGO_GPIO_OUTPUT_ENABLE |
957                                 KEYLARGO_GPIO_OUTOUT_DATA);
958                 else
959                         MACIO_OUT8(KL_GPIO_SOUND_POWER,
960                                 KEYLARGO_GPIO_OUTPUT_ENABLE);
961                 (void)MACIO_IN8(KL_GPIO_SOUND_POWER);
962                 UNLOCK(flags);
963         }
964         return 0;
965 }
966
967 static long
968 core99_airport_enable(struct device_node *node, long param, long value)
969 {
970         struct macio_chip*      macio;
971         unsigned long           flags;
972         int                     state;
973
974         macio = macio_find(node, 0);
975         if (!macio)
976                 return -ENODEV;
977
978         /* Hint: we allow passing of macio itself for the sake of the
979          * sleep code
980          */
981         if (node != macio->of_node &&
982             (!node->parent || node->parent != macio->of_node))
983                 return -ENODEV;
984         state = (macio->flags & MACIO_FLAG_AIRPORT_ON) != 0;
985         if (value == state)
986                 return 0;
987         if (value) {
988                 /* This code is a reproduction of OF enable-cardslot
989                  * and init-wireless methods, slightly hacked until
990                  * I got it working.
991                  */
992                 LOCK(flags);
993                 MACIO_OUT8(KEYLARGO_GPIO_0+0xf, 5);
994                 (void)MACIO_IN8(KEYLARGO_GPIO_0+0xf);
995                 UNLOCK(flags);
996                 mdelay(10);
997                 LOCK(flags);
998                 MACIO_OUT8(KEYLARGO_GPIO_0+0xf, 4);
999                 (void)MACIO_IN8(KEYLARGO_GPIO_0+0xf);
1000                 UNLOCK(flags);
1001
1002                 mdelay(10);
1003
1004                 LOCK(flags);
1005                 MACIO_BIC(KEYLARGO_FCR2, KL2_CARDSEL_16);
1006                 (void)MACIO_IN32(KEYLARGO_FCR2);
1007                 udelay(10);
1008                 MACIO_OUT8(KEYLARGO_GPIO_EXTINT_0+0xb, 0);
1009                 (void)MACIO_IN8(KEYLARGO_GPIO_EXTINT_0+0xb);
1010                 udelay(10);
1011                 MACIO_OUT8(KEYLARGO_GPIO_EXTINT_0+0xa, 0x28);
1012                 (void)MACIO_IN8(KEYLARGO_GPIO_EXTINT_0+0xa);
1013                 udelay(10);
1014                 MACIO_OUT8(KEYLARGO_GPIO_EXTINT_0+0xd, 0x28);
1015                 (void)MACIO_IN8(KEYLARGO_GPIO_EXTINT_0+0xd);
1016                 udelay(10);
1017                 MACIO_OUT8(KEYLARGO_GPIO_0+0xd, 0x28);
1018                 (void)MACIO_IN8(KEYLARGO_GPIO_0+0xd);
1019                 udelay(10);
1020                 MACIO_OUT8(KEYLARGO_GPIO_0+0xe, 0x28);
1021                 (void)MACIO_IN8(KEYLARGO_GPIO_0+0xe);
1022                 UNLOCK(flags);
1023                 udelay(10);
1024                 MACIO_OUT32(0x1c000, 0);
1025                 mdelay(1);
1026                 MACIO_OUT8(0x1a3e0, 0x41);
1027                 (void)MACIO_IN8(0x1a3e0);
1028                 udelay(10);
1029                 LOCK(flags);
1030                 MACIO_BIS(KEYLARGO_FCR2, KL2_CARDSEL_16);
1031                 (void)MACIO_IN32(KEYLARGO_FCR2);
1032                 UNLOCK(flags);
1033                 mdelay(100);
1034
1035                 macio->flags |= MACIO_FLAG_AIRPORT_ON;
1036         } else {
1037                 LOCK(flags);
1038                 MACIO_BIC(KEYLARGO_FCR2, KL2_CARDSEL_16);
1039                 (void)MACIO_IN32(KEYLARGO_FCR2);
1040                 MACIO_OUT8(KL_GPIO_AIRPORT_0, 0);
1041                 MACIO_OUT8(KL_GPIO_AIRPORT_1, 0);
1042                 MACIO_OUT8(KL_GPIO_AIRPORT_2, 0);
1043                 MACIO_OUT8(KL_GPIO_AIRPORT_3, 0);
1044                 MACIO_OUT8(KL_GPIO_AIRPORT_4, 0);
1045                 (void)MACIO_IN8(KL_GPIO_AIRPORT_4);
1046                 UNLOCK(flags);
1047
1048                 macio->flags &= ~MACIO_FLAG_AIRPORT_ON;
1049         }
1050         return 0;
1051 }
1052
1053 #ifdef CONFIG_SMP
1054 static long
1055 core99_reset_cpu(struct device_node *node, long param, long value)
1056 {
1057         unsigned int reset_io = 0;
1058         unsigned long flags;
1059         struct macio_chip *macio;
1060         struct device_node *np;
1061         const int dflt_reset_lines[] = {        KL_GPIO_RESET_CPU0,
1062                                                 KL_GPIO_RESET_CPU1,
1063                                                 KL_GPIO_RESET_CPU2,
1064                                                 KL_GPIO_RESET_CPU3 };
1065
1066         macio = &macio_chips[0];
1067         if (macio->type != macio_keylargo)
1068                 return -ENODEV;
1069
1070         np = find_path_device("/cpus");
1071         if (np == NULL)
1072                 return -ENODEV;
1073         for (np = np->child; np != NULL; np = np->sibling) {
1074                 u32 *num = (u32 *)get_property(np, "reg", NULL);
1075                 u32 *rst = (u32 *)get_property(np, "soft-reset", NULL);
1076                 if (num == NULL || rst == NULL)
1077                         continue;
1078                 if (param == *num) {
1079                         reset_io = *rst;
1080                         break;
1081                 }
1082         }
1083         if (np == NULL || reset_io == 0)
1084                 reset_io = dflt_reset_lines[param];
1085
1086         LOCK(flags);
1087         MACIO_OUT8(reset_io, KEYLARGO_GPIO_OUTPUT_ENABLE);
1088         (void)MACIO_IN8(reset_io);
1089         udelay(1);
1090         MACIO_OUT8(reset_io, 0);
1091         (void)MACIO_IN8(reset_io);
1092         UNLOCK(flags);
1093
1094         return 0;
1095 }
1096 #endif /* CONFIG_SMP */
1097
1098 static long
1099 core99_usb_enable(struct device_node *node, long param, long value)
1100 {
1101         struct macio_chip *macio;
1102         unsigned long flags;
1103         char *prop;
1104         int number;
1105         u32 reg;
1106
1107         macio = &macio_chips[0];
1108         if (macio->type != macio_keylargo && macio->type != macio_pangea &&
1109             macio->type != macio_intrepid)
1110                 return -ENODEV;
1111
1112         prop = (char *)get_property(node, "AAPL,clock-id", NULL);
1113         if (!prop)
1114                 return -ENODEV;
1115         if (strncmp(prop, "usb0u048", 8) == 0)
1116                 number = 0;
1117         else if (strncmp(prop, "usb1u148", 8) == 0)
1118                 number = 2;
1119         else if (strncmp(prop, "usb2u248", 8) == 0)
1120                 number = 4;
1121         else
1122                 return -ENODEV;
1123
1124         /* Sorry for the brute-force locking, but this is only used during
1125          * sleep and the timing seem to be critical
1126          */
1127         LOCK(flags);
1128         if (value) {
1129                 /* Turn ON */
1130                 if (number == 0) {
1131                         MACIO_BIC(KEYLARGO_FCR0, (KL0_USB0_PAD_SUSPEND0 | KL0_USB0_PAD_SUSPEND1));
1132                         (void)MACIO_IN32(KEYLARGO_FCR0);
1133                         UNLOCK(flags);
1134                         mdelay(1);
1135                         LOCK(flags);
1136                         MACIO_BIS(KEYLARGO_FCR0, KL0_USB0_CELL_ENABLE);
1137                 } else if (number == 2) {
1138                         MACIO_BIC(KEYLARGO_FCR0, (KL0_USB1_PAD_SUSPEND0 | KL0_USB1_PAD_SUSPEND1));
1139                         UNLOCK(flags);
1140                         (void)MACIO_IN32(KEYLARGO_FCR0);
1141                         mdelay(1);
1142                         LOCK(flags);
1143                         MACIO_BIS(KEYLARGO_FCR0, KL0_USB1_CELL_ENABLE);
1144                 } else if (number == 4) {
1145                         MACIO_BIC(KEYLARGO_FCR1, (KL1_USB2_PAD_SUSPEND0 | KL1_USB2_PAD_SUSPEND1));
1146                         UNLOCK(flags);
1147                         (void)MACIO_IN32(KEYLARGO_FCR1);
1148                         mdelay(1);
1149                         LOCK(flags);
1150                         MACIO_BIS(KEYLARGO_FCR1, KL1_USB2_CELL_ENABLE);
1151                 }
1152                 if (number < 4) {
1153                         reg = MACIO_IN32(KEYLARGO_FCR4);
1154                         reg &=  ~(KL4_PORT_WAKEUP_ENABLE(number) | KL4_PORT_RESUME_WAKE_EN(number) |
1155                                 KL4_PORT_CONNECT_WAKE_EN(number) | KL4_PORT_DISCONNECT_WAKE_EN(number));
1156                         reg &=  ~(KL4_PORT_WAKEUP_ENABLE(number+1) | KL4_PORT_RESUME_WAKE_EN(number+1) |
1157                                 KL4_PORT_CONNECT_WAKE_EN(number+1) | KL4_PORT_DISCONNECT_WAKE_EN(number+1));
1158                         MACIO_OUT32(KEYLARGO_FCR4, reg);
1159                         (void)MACIO_IN32(KEYLARGO_FCR4);
1160                         udelay(10);
1161                 } else {
1162                         reg = MACIO_IN32(KEYLARGO_FCR3);
1163                         reg &=  ~(KL3_IT_PORT_WAKEUP_ENABLE(0) | KL3_IT_PORT_RESUME_WAKE_EN(0) |
1164                                 KL3_IT_PORT_CONNECT_WAKE_EN(0) | KL3_IT_PORT_DISCONNECT_WAKE_EN(0));
1165                         reg &=  ~(KL3_IT_PORT_WAKEUP_ENABLE(1) | KL3_IT_PORT_RESUME_WAKE_EN(1) |
1166                                 KL3_IT_PORT_CONNECT_WAKE_EN(1) | KL3_IT_PORT_DISCONNECT_WAKE_EN(1));
1167                         MACIO_OUT32(KEYLARGO_FCR3, reg);
1168                         (void)MACIO_IN32(KEYLARGO_FCR3);
1169                         udelay(10);
1170                 }
1171                 if (macio->type == macio_intrepid) {
1172                         /* wait for clock stopped bits to clear */
1173                         u32 test0 = 0, test1 = 0;
1174                         u32 status0, status1;
1175                         int timeout = 1000;
1176
1177                         UNLOCK(flags);
1178                         switch (number) {
1179                         case 0:
1180                                 test0 = UNI_N_CLOCK_STOPPED_USB0;
1181                                 test1 = UNI_N_CLOCK_STOPPED_USB0PCI;
1182                                 break;
1183                         case 2:
1184                                 test0 = UNI_N_CLOCK_STOPPED_USB1;
1185                                 test1 = UNI_N_CLOCK_STOPPED_USB1PCI;
1186                                 break;
1187                         case 4:
1188                                 test0 = UNI_N_CLOCK_STOPPED_USB2;
1189                                 test1 = UNI_N_CLOCK_STOPPED_USB2PCI;
1190                                 break;
1191                         }
1192                         do {
1193                                 if (--timeout <= 0) {
1194                                         printk(KERN_ERR "core99_usb_enable: "
1195                                                "Timeout waiting for clocks\n");
1196                                         break;
1197                                 }
1198                                 mdelay(1);
1199                                 status0 = UN_IN(UNI_N_CLOCK_STOP_STATUS0);
1200                                 status1 = UN_IN(UNI_N_CLOCK_STOP_STATUS1);
1201                         } while ((status0 & test0) | (status1 & test1));
1202                         LOCK(flags);
1203                 }
1204         } else {
1205                 /* Turn OFF */
1206                 if (number < 4) {
1207                         reg = MACIO_IN32(KEYLARGO_FCR4);
1208                         reg |=  KL4_PORT_WAKEUP_ENABLE(number) | KL4_PORT_RESUME_WAKE_EN(number) |
1209                                 KL4_PORT_CONNECT_WAKE_EN(number) | KL4_PORT_DISCONNECT_WAKE_EN(number);
1210                         reg |=  KL4_PORT_WAKEUP_ENABLE(number+1) | KL4_PORT_RESUME_WAKE_EN(number+1) |
1211                                 KL4_PORT_CONNECT_WAKE_EN(number+1) | KL4_PORT_DISCONNECT_WAKE_EN(number+1);
1212                         MACIO_OUT32(KEYLARGO_FCR4, reg);
1213                         (void)MACIO_IN32(KEYLARGO_FCR4);
1214                         udelay(1);
1215                 } else {
1216                         reg = MACIO_IN32(KEYLARGO_FCR3);
1217                         reg |=  KL3_IT_PORT_WAKEUP_ENABLE(0) | KL3_IT_PORT_RESUME_WAKE_EN(0) |
1218                                 KL3_IT_PORT_CONNECT_WAKE_EN(0) | KL3_IT_PORT_DISCONNECT_WAKE_EN(0);
1219                         reg |=  KL3_IT_PORT_WAKEUP_ENABLE(1) | KL3_IT_PORT_RESUME_WAKE_EN(1) |
1220                                 KL3_IT_PORT_CONNECT_WAKE_EN(1) | KL3_IT_PORT_DISCONNECT_WAKE_EN(1);
1221                         MACIO_OUT32(KEYLARGO_FCR3, reg);
1222                         (void)MACIO_IN32(KEYLARGO_FCR3);
1223                         udelay(1);
1224                 }
1225                 if (number == 0) {
1226                         if (macio->type != macio_intrepid)
1227                                 MACIO_BIC(KEYLARGO_FCR0, KL0_USB0_CELL_ENABLE);
1228                         (void)MACIO_IN32(KEYLARGO_FCR0);
1229                         udelay(1);
1230                         MACIO_BIS(KEYLARGO_FCR0, (KL0_USB0_PAD_SUSPEND0 | KL0_USB0_PAD_SUSPEND1));
1231                         (void)MACIO_IN32(KEYLARGO_FCR0);
1232                 } else if (number == 2) {
1233                         if (macio->type != macio_intrepid)
1234                                 MACIO_BIC(KEYLARGO_FCR0, KL0_USB1_CELL_ENABLE);
1235                         (void)MACIO_IN32(KEYLARGO_FCR0);
1236                         udelay(1);
1237                         MACIO_BIS(KEYLARGO_FCR0, (KL0_USB1_PAD_SUSPEND0 | KL0_USB1_PAD_SUSPEND1));
1238                         (void)MACIO_IN32(KEYLARGO_FCR0);
1239                 } else if (number == 4) {
1240                         udelay(1);
1241                         MACIO_BIS(KEYLARGO_FCR1, (KL1_USB2_PAD_SUSPEND0 | KL1_USB2_PAD_SUSPEND1));
1242                         (void)MACIO_IN32(KEYLARGO_FCR1);
1243                 }
1244                 udelay(1);
1245         }
1246         UNLOCK(flags);
1247
1248         return 0;
1249 }
1250
1251 static long
1252 core99_firewire_enable(struct device_node *node, long param, long value)
1253 {
1254         unsigned long flags;
1255         struct macio_chip *macio;
1256
1257         macio = &macio_chips[0];
1258         if (macio->type != macio_keylargo && macio->type != macio_pangea &&
1259             macio->type != macio_intrepid)
1260                 return -ENODEV;
1261         if (!(macio->flags & MACIO_FLAG_FW_SUPPORTED))
1262                 return -ENODEV;
1263
1264         LOCK(flags);
1265         if (value) {
1266                 UN_BIS(UNI_N_CLOCK_CNTL, UNI_N_CLOCK_CNTL_FW);
1267                 (void)UN_IN(UNI_N_CLOCK_CNTL);
1268         } else {
1269                 UN_BIC(UNI_N_CLOCK_CNTL, UNI_N_CLOCK_CNTL_FW);
1270                 (void)UN_IN(UNI_N_CLOCK_CNTL);
1271         }
1272         UNLOCK(flags);
1273         mdelay(1);
1274
1275         return 0;
1276 }
1277
1278 static long
1279 core99_firewire_cable_power(struct device_node *node, long param, long value)
1280 {
1281         unsigned long flags;
1282         struct macio_chip *macio;
1283
1284         /* Trick: we allow NULL node */
1285         if ((pmac_mb.board_flags & PMAC_MB_HAS_FW_POWER) == 0)
1286                 return -ENODEV;
1287         macio = &macio_chips[0];
1288         if (macio->type != macio_keylargo && macio->type != macio_pangea &&
1289             macio->type != macio_intrepid)
1290                 return -ENODEV;
1291         if (!(macio->flags & MACIO_FLAG_FW_SUPPORTED))
1292                 return -ENODEV;
1293
1294         LOCK(flags);
1295         if (value) {
1296                 MACIO_OUT8(KL_GPIO_FW_CABLE_POWER , 0);
1297                 MACIO_IN8(KL_GPIO_FW_CABLE_POWER);
1298                 udelay(10);
1299         } else {
1300                 MACIO_OUT8(KL_GPIO_FW_CABLE_POWER , 4);
1301                 MACIO_IN8(KL_GPIO_FW_CABLE_POWER); udelay(10);
1302         }
1303         UNLOCK(flags);
1304         mdelay(1);
1305
1306         return 0;
1307 }
1308
1309 static long
1310 intrepid_aack_delay_enable(struct device_node *node, long param, long value)
1311 {
1312         unsigned long flags;
1313
1314         if (uninorth_rev < 0xd2)
1315                 return -ENODEV;
1316
1317         LOCK(flags);
1318         if (param)
1319                 UN_BIS(UNI_N_AACK_DELAY, UNI_N_AACK_DELAY_ENABLE);
1320         else
1321                 UN_BIC(UNI_N_AACK_DELAY, UNI_N_AACK_DELAY_ENABLE);
1322         UNLOCK(flags);
1323
1324         return 0;
1325 }
1326
1327
1328 #endif /* CONFIG_POWER4 */
1329
1330 static long
1331 core99_read_gpio(struct device_node *node, long param, long value)
1332 {
1333         struct macio_chip *macio = &macio_chips[0];
1334
1335         return MACIO_IN8(param);
1336 }
1337
1338
1339 static long
1340 core99_write_gpio(struct device_node *node, long param, long value)
1341 {
1342         struct macio_chip *macio = &macio_chips[0];
1343
1344         MACIO_OUT8(param, (u8)(value & 0xff));
1345         return 0;
1346 }
1347
1348 #ifdef CONFIG_POWER4
1349 static long g5_gmac_enable(struct device_node *node, long param, long value)
1350 {
1351         struct macio_chip *macio = &macio_chips[0];
1352         unsigned long flags;
1353
1354         if (node == NULL)
1355                 return -ENODEV;
1356
1357         LOCK(flags);
1358         if (value) {
1359                 MACIO_BIS(KEYLARGO_FCR1, K2_FCR1_GMAC_CLK_ENABLE);
1360                 mb();
1361                 k2_skiplist[0] = NULL;
1362         } else {
1363                 k2_skiplist[0] = node;
1364                 mb();
1365                 MACIO_BIC(KEYLARGO_FCR1, K2_FCR1_GMAC_CLK_ENABLE);
1366         }
1367         
1368         UNLOCK(flags);
1369         mdelay(1);
1370
1371         return 0;
1372 }
1373
1374 static long g5_fw_enable(struct device_node *node, long param, long value)
1375 {
1376         struct macio_chip *macio = &macio_chips[0];
1377         unsigned long flags;
1378
1379         if (node == NULL)
1380                 return -ENODEV;
1381
1382         LOCK(flags);
1383         if (value) {
1384                 MACIO_BIS(KEYLARGO_FCR1, K2_FCR1_FW_CLK_ENABLE);
1385                 mb();
1386                 k2_skiplist[1] = NULL;
1387         } else {
1388                 k2_skiplist[1] = node;
1389                 mb();
1390                 MACIO_BIC(KEYLARGO_FCR1, K2_FCR1_FW_CLK_ENABLE);
1391         }
1392         
1393         UNLOCK(flags);
1394         mdelay(1);
1395
1396         return 0;
1397 }
1398
1399 static long g5_mpic_enable(struct device_node *node, long param, long value)
1400 {
1401         unsigned long flags;
1402
1403         if (node->parent == NULL || strcmp(node->parent->name, "u3"))
1404                 return 0;
1405
1406         LOCK(flags);
1407         UN_BIS(U3_TOGGLE_REG, U3_MPIC_RESET | U3_MPIC_OUTPUT_ENABLE);
1408         UNLOCK(flags);
1409
1410         return 0;
1411 }
1412
1413 static long g5_eth_phy_reset(struct device_node *node, long param, long value)
1414 {
1415         struct macio_chip *macio = &macio_chips[0];
1416         struct device_node *phy;
1417         int need_reset;
1418
1419         /*
1420          * We must not reset the combo PHYs, only the BCM5221 found in
1421          * the iMac G5.
1422          */
1423         phy = of_get_next_child(node, NULL);
1424         if (!phy)
1425                 return -ENODEV;
1426         need_reset = device_is_compatible(phy, "B5221");
1427         of_node_put(phy);
1428         if (!need_reset)
1429                 return 0;
1430
1431         /* PHY reset is GPIO 29, not in device-tree unfortunately */
1432         MACIO_OUT8(K2_GPIO_EXTINT_0 + 29,
1433                    KEYLARGO_GPIO_OUTPUT_ENABLE | KEYLARGO_GPIO_OUTOUT_DATA);
1434         /* Thankfully, this is now always called at a time when we can
1435          * schedule by sungem.
1436          */
1437         msleep(10);
1438         MACIO_OUT8(K2_GPIO_EXTINT_0 + 29, 0);
1439
1440         return 0;
1441 }
1442
1443 static long g5_i2s_enable(struct device_node *node, long param, long value)
1444 {
1445         /* Very crude implementation for now */
1446         struct macio_chip *macio = &macio_chips[0];
1447         unsigned long flags;
1448         int cell;
1449         u32 fcrs[3][3] = {
1450                 { 0,
1451                   K2_FCR1_I2S0_CELL_ENABLE |
1452                   K2_FCR1_I2S0_CLK_ENABLE_BIT | K2_FCR1_I2S0_ENABLE,
1453                   KL3_I2S0_CLK18_ENABLE
1454                 },
1455                 { KL0_SCC_A_INTF_ENABLE,
1456                   K2_FCR1_I2S1_CELL_ENABLE |
1457                   K2_FCR1_I2S1_CLK_ENABLE_BIT | K2_FCR1_I2S1_ENABLE,
1458                   KL3_I2S1_CLK18_ENABLE
1459                 },
1460                 { KL0_SCC_B_INTF_ENABLE,
1461                   SH_FCR1_I2S2_CELL_ENABLE |
1462                   SH_FCR1_I2S2_CLK_ENABLE_BIT | SH_FCR1_I2S2_ENABLE,
1463                   SH_FCR3_I2S2_CLK18_ENABLE
1464                 },
1465         };
1466
1467         if (macio->type != macio_keylargo2 /* && macio->type != macio_shasta*/)
1468                 return -ENODEV;
1469         if (strncmp(node->name, "i2s-", 4))
1470                 return -ENODEV;
1471         cell = node->name[4] - 'a';
1472         switch(cell) {
1473         case 0:
1474         case 1:
1475                 break;
1476 #if 0
1477         case 2:
1478                 if (macio->type == macio_shasta)
1479                         break;
1480 #endif
1481         default:
1482                 return -ENODEV;
1483         }
1484
1485         LOCK(flags);
1486         if (value) {
1487                 MACIO_BIC(KEYLARGO_FCR0, fcrs[cell][0]);
1488                 MACIO_BIS(KEYLARGO_FCR1, fcrs[cell][1]);
1489                 MACIO_BIS(KEYLARGO_FCR3, fcrs[cell][2]);
1490         } else {
1491                 MACIO_BIC(KEYLARGO_FCR3, fcrs[cell][2]);
1492                 MACIO_BIC(KEYLARGO_FCR1, fcrs[cell][1]);
1493                 MACIO_BIS(KEYLARGO_FCR0, fcrs[cell][0]);
1494         }
1495         udelay(10);
1496         UNLOCK(flags);
1497
1498         return 0;
1499 }
1500
1501
1502 #ifdef CONFIG_SMP
1503 static long g5_reset_cpu(struct device_node *node, long param, long value)
1504 {
1505         unsigned int reset_io = 0;
1506         unsigned long flags;
1507         struct macio_chip *macio;
1508         struct device_node *np;
1509
1510         macio = &macio_chips[0];
1511         if (macio->type != macio_keylargo2)
1512                 return -ENODEV;
1513
1514         np = find_path_device("/cpus");
1515         if (np == NULL)
1516                 return -ENODEV;
1517         for (np = np->child; np != NULL; np = np->sibling) {
1518                 u32 *num = (u32 *)get_property(np, "reg", NULL);
1519                 u32 *rst = (u32 *)get_property(np, "soft-reset", NULL);
1520                 if (num == NULL || rst == NULL)
1521                         continue;
1522                 if (param == *num) {
1523                         reset_io = *rst;
1524                         break;
1525                 }
1526         }
1527         if (np == NULL || reset_io == 0)
1528                 return -ENODEV;
1529
1530         LOCK(flags);
1531         MACIO_OUT8(reset_io, KEYLARGO_GPIO_OUTPUT_ENABLE);
1532         (void)MACIO_IN8(reset_io);
1533         udelay(1);
1534         MACIO_OUT8(reset_io, 0);
1535         (void)MACIO_IN8(reset_io);
1536         UNLOCK(flags);
1537
1538         return 0;
1539 }
1540 #endif /* CONFIG_SMP */
1541
1542 /*
1543  * This can be called from pmac_smp so isn't static
1544  *
1545  * This takes the second CPU off the bus on dual CPU machines
1546  * running UP
1547  */
1548 void g5_phy_disable_cpu1(void)
1549 {
1550         UN_OUT(U3_API_PHY_CONFIG_1, 0);
1551 }
1552 #endif /* CONFIG_POWER4 */
1553
1554 #ifndef CONFIG_POWER4
1555
1556 static void
1557 keylargo_shutdown(struct macio_chip *macio, int sleep_mode)
1558 {
1559         u32 temp;
1560
1561         if (sleep_mode) {
1562                 mdelay(1);
1563                 MACIO_BIS(KEYLARGO_FCR0, KL0_USB_REF_SUSPEND);
1564                 (void)MACIO_IN32(KEYLARGO_FCR0);
1565                 mdelay(1);
1566         }
1567
1568         MACIO_BIC(KEYLARGO_FCR0,KL0_SCCA_ENABLE | KL0_SCCB_ENABLE |
1569                                 KL0_SCC_CELL_ENABLE |
1570                                 KL0_IRDA_ENABLE | KL0_IRDA_CLK32_ENABLE |
1571                                 KL0_IRDA_CLK19_ENABLE);
1572
1573         MACIO_BIC(KEYLARGO_MBCR, KL_MBCR_MB0_DEV_MASK);
1574         MACIO_BIS(KEYLARGO_MBCR, KL_MBCR_MB0_IDE_ENABLE);
1575
1576         MACIO_BIC(KEYLARGO_FCR1,
1577                 KL1_AUDIO_SEL_22MCLK | KL1_AUDIO_CLK_ENABLE_BIT |
1578                 KL1_AUDIO_CLK_OUT_ENABLE | KL1_AUDIO_CELL_ENABLE |
1579                 KL1_I2S0_CELL_ENABLE | KL1_I2S0_CLK_ENABLE_BIT |
1580                 KL1_I2S0_ENABLE | KL1_I2S1_CELL_ENABLE |
1581                 KL1_I2S1_CLK_ENABLE_BIT | KL1_I2S1_ENABLE |
1582                 KL1_EIDE0_ENABLE | KL1_EIDE0_RESET_N |
1583                 KL1_EIDE1_ENABLE | KL1_EIDE1_RESET_N |
1584                 KL1_UIDE_ENABLE);
1585
1586         MACIO_BIS(KEYLARGO_FCR2, KL2_ALT_DATA_OUT);
1587         MACIO_BIC(KEYLARGO_FCR2, KL2_IOBUS_ENABLE);
1588
1589         temp = MACIO_IN32(KEYLARGO_FCR3);
1590         if (macio->rev >= 2) {
1591                 temp |= KL3_SHUTDOWN_PLL2X;
1592                 if (sleep_mode)
1593                         temp |= KL3_SHUTDOWN_PLL_TOTAL;
1594         }
1595
1596         temp |= KL3_SHUTDOWN_PLLKW6 | KL3_SHUTDOWN_PLLKW4 |
1597                 KL3_SHUTDOWN_PLLKW35;
1598         if (sleep_mode)
1599                 temp |= KL3_SHUTDOWN_PLLKW12;
1600         temp &= ~(KL3_CLK66_ENABLE | KL3_CLK49_ENABLE | KL3_CLK45_ENABLE
1601                 | KL3_CLK31_ENABLE | KL3_I2S1_CLK18_ENABLE | KL3_I2S0_CLK18_ENABLE);
1602         if (sleep_mode)
1603                 temp &= ~(KL3_TIMER_CLK18_ENABLE | KL3_VIA_CLK16_ENABLE);
1604         MACIO_OUT32(KEYLARGO_FCR3, temp);
1605
1606         /* Flush posted writes & wait a bit */
1607         (void)MACIO_IN32(KEYLARGO_FCR0); mdelay(1);
1608 }
1609
1610 static void
1611 pangea_shutdown(struct macio_chip *macio, int sleep_mode)
1612 {
1613         u32 temp;
1614
1615         MACIO_BIC(KEYLARGO_FCR0,KL0_SCCA_ENABLE | KL0_SCCB_ENABLE |
1616                                 KL0_SCC_CELL_ENABLE |
1617                                 KL0_USB0_CELL_ENABLE | KL0_USB1_CELL_ENABLE);
1618
1619         MACIO_BIC(KEYLARGO_FCR1,
1620                 KL1_AUDIO_SEL_22MCLK | KL1_AUDIO_CLK_ENABLE_BIT |
1621                 KL1_AUDIO_CLK_OUT_ENABLE | KL1_AUDIO_CELL_ENABLE |
1622                 KL1_I2S0_CELL_ENABLE | KL1_I2S0_CLK_ENABLE_BIT |
1623                 KL1_I2S0_ENABLE | KL1_I2S1_CELL_ENABLE |
1624                 KL1_I2S1_CLK_ENABLE_BIT | KL1_I2S1_ENABLE |
1625                 KL1_UIDE_ENABLE);
1626         if (pmac_mb.board_flags & PMAC_MB_MOBILE)
1627                 MACIO_BIC(KEYLARGO_FCR1, KL1_UIDE_RESET_N);
1628
1629         MACIO_BIS(KEYLARGO_FCR2, KL2_ALT_DATA_OUT);
1630
1631         temp = MACIO_IN32(KEYLARGO_FCR3);
1632         temp |= KL3_SHUTDOWN_PLLKW6 | KL3_SHUTDOWN_PLLKW4 |
1633                 KL3_SHUTDOWN_PLLKW35;
1634         temp &= ~(KL3_CLK49_ENABLE | KL3_CLK45_ENABLE | KL3_CLK31_ENABLE
1635                 | KL3_I2S0_CLK18_ENABLE | KL3_I2S1_CLK18_ENABLE);
1636         if (sleep_mode)
1637                 temp &= ~(KL3_VIA_CLK16_ENABLE | KL3_TIMER_CLK18_ENABLE);
1638         MACIO_OUT32(KEYLARGO_FCR3, temp);
1639
1640         /* Flush posted writes & wait a bit */
1641         (void)MACIO_IN32(KEYLARGO_FCR0); mdelay(1);
1642 }
1643
1644 static void
1645 intrepid_shutdown(struct macio_chip *macio, int sleep_mode)
1646 {
1647         u32 temp;
1648
1649         MACIO_BIC(KEYLARGO_FCR0,KL0_SCCA_ENABLE | KL0_SCCB_ENABLE |
1650                   KL0_SCC_CELL_ENABLE);
1651
1652         MACIO_BIC(KEYLARGO_FCR1,
1653                   /*KL1_USB2_CELL_ENABLE |*/
1654                 KL1_I2S0_CELL_ENABLE | KL1_I2S0_CLK_ENABLE_BIT |
1655                 KL1_I2S0_ENABLE | KL1_I2S1_CELL_ENABLE |
1656                 KL1_I2S1_CLK_ENABLE_BIT | KL1_I2S1_ENABLE);
1657         if (pmac_mb.board_flags & PMAC_MB_MOBILE)
1658                 MACIO_BIC(KEYLARGO_FCR1, KL1_UIDE_RESET_N);
1659
1660         temp = MACIO_IN32(KEYLARGO_FCR3);
1661         temp &= ~(KL3_CLK49_ENABLE | KL3_CLK45_ENABLE |
1662                   KL3_I2S1_CLK18_ENABLE | KL3_I2S0_CLK18_ENABLE);
1663         if (sleep_mode)
1664                 temp &= ~(KL3_TIMER_CLK18_ENABLE | KL3_IT_VIA_CLK32_ENABLE);
1665         MACIO_OUT32(KEYLARGO_FCR3, temp);
1666
1667         /* Flush posted writes & wait a bit */
1668         (void)MACIO_IN32(KEYLARGO_FCR0);
1669         mdelay(10);
1670 }
1671
1672
1673 void pmac_tweak_clock_spreading(int enable)
1674 {
1675         struct macio_chip *macio = &macio_chips[0];
1676
1677         /* Hack for doing clock spreading on some machines PowerBooks and
1678          * iBooks. This implements the "platform-do-clockspreading" OF
1679          * property as decoded manually on various models. For safety, we also
1680          * check the product ID in the device-tree in cases we'll whack the i2c
1681          * chip to make reasonably sure we won't set wrong values in there
1682          *
1683          * Of course, ultimately, we have to implement a real parser for
1684          * the platform-do-* stuff...
1685          */
1686
1687         if (macio->type == macio_intrepid) {
1688                 struct device_node *clock =
1689                         of_find_node_by_path("/uni-n@f8000000/hw-clock");
1690                 if (clock && get_property(clock, "platform-do-clockspreading",
1691                                           NULL)) {
1692                         printk(KERN_INFO "%sabling clock spreading on Intrepid"
1693                                " ASIC\n", enable ? "En" : "Dis");
1694                         if (enable)
1695                                 UN_OUT(UNI_N_CLOCK_SPREADING, 2);
1696                         else
1697                                 UN_OUT(UNI_N_CLOCK_SPREADING, 0);
1698                         mdelay(40);
1699                 }
1700                 of_node_put(clock);
1701         }
1702
1703         while (machine_is_compatible("PowerBook5,2") ||
1704                machine_is_compatible("PowerBook5,3") ||
1705                machine_is_compatible("PowerBook6,2") ||
1706                machine_is_compatible("PowerBook6,3")) {
1707                 struct device_node *ui2c = of_find_node_by_type(NULL, "i2c");
1708                 struct device_node *dt = of_find_node_by_name(NULL, "device-tree");
1709                 u8 buffer[9];
1710                 u32 *productID;
1711                 int i, rc, changed = 0;
1712
1713                 if (dt == NULL)
1714                         break;
1715                 productID = (u32 *)get_property(dt, "pid#", NULL);
1716                 if (productID == NULL)
1717                         break;
1718                 while(ui2c) {
1719                         struct device_node *p = of_get_parent(ui2c);
1720                         if (p && !strcmp(p->name, "uni-n"))
1721                                 break;
1722                         ui2c = of_find_node_by_type(ui2c, "i2c");
1723                 }
1724                 if (ui2c == NULL)
1725                         break;
1726                 DBG("Trying to bump clock speed for PID: %08x...\n", *productID);
1727                 rc = pmac_low_i2c_open(ui2c, 1);
1728                 if (rc != 0)
1729                         break;
1730                 pmac_low_i2c_setmode(ui2c, pmac_low_i2c_mode_combined);
1731                 rc = pmac_low_i2c_xfer(ui2c, 0xd2 | pmac_low_i2c_read, 0x80, buffer, 9);
1732                 DBG("read result: %d,", rc);
1733                 if (rc != 0) {
1734                         pmac_low_i2c_close(ui2c);
1735                         break;
1736                 }
1737                 for (i=0; i<9; i++)
1738                         DBG(" %02x", buffer[i]);
1739                 DBG("\n");
1740
1741                 switch(*productID) {
1742                 case 0x1182:    /* AlBook 12" rev 2 */
1743                 case 0x1183:    /* iBook G4 12" */
1744                         buffer[0] = (buffer[0] & 0x8f) | 0x70;
1745                         buffer[2] = (buffer[2] & 0x7f) | 0x00;
1746                         buffer[5] = (buffer[5] & 0x80) | 0x31;
1747                         buffer[6] = (buffer[6] & 0x40) | 0xb0;
1748                         buffer[7] = (buffer[7] & 0x00) | (enable ? 0xc0 : 0xba);
1749                         buffer[8] = (buffer[8] & 0x00) | 0x30;
1750                         changed = 1;
1751                         break;
1752                 case 0x3142:    /* AlBook 15" (ATI M10) */
1753                 case 0x3143:    /* AlBook 17" (ATI M10) */
1754                         buffer[0] = (buffer[0] & 0xaf) | 0x50;
1755                         buffer[2] = (buffer[2] & 0x7f) | 0x00;
1756                         buffer[5] = (buffer[5] & 0x80) | 0x31;
1757                         buffer[6] = (buffer[6] & 0x40) | 0xb0;
1758                         buffer[7] = (buffer[7] & 0x00) | (enable ? 0xd0 : 0xc0);
1759                         buffer[8] = (buffer[8] & 0x00) | 0x30;
1760                         changed = 1;
1761                         break;
1762                 default:
1763                         DBG("i2c-hwclock: Machine model not handled\n");
1764                         break;
1765                 }
1766                 if (!changed) {
1767                         pmac_low_i2c_close(ui2c);
1768                         break;
1769                 }
1770                 printk(KERN_INFO "%sabling clock spreading on i2c clock chip\n",
1771                        enable ? "En" : "Dis");
1772
1773                 pmac_low_i2c_setmode(ui2c, pmac_low_i2c_mode_stdsub);
1774                 rc = pmac_low_i2c_xfer(ui2c, 0xd2 | pmac_low_i2c_write, 0x80, buffer, 9);
1775                 DBG("write result: %d,", rc);
1776                 pmac_low_i2c_setmode(ui2c, pmac_low_i2c_mode_combined);
1777                 rc = pmac_low_i2c_xfer(ui2c, 0xd2 | pmac_low_i2c_read, 0x80, buffer, 9);
1778                 DBG("read result: %d,", rc);
1779                 if (rc != 0) {
1780                         pmac_low_i2c_close(ui2c);
1781                         break;
1782                 }
1783                 for (i=0; i<9; i++)
1784                         DBG(" %02x", buffer[i]);
1785                 pmac_low_i2c_close(ui2c);
1786                 break;
1787         }
1788 }
1789
1790
1791 static int
1792 core99_sleep(void)
1793 {
1794         struct macio_chip *macio;
1795         int i;
1796
1797         macio = &macio_chips[0];
1798         if (macio->type != macio_keylargo && macio->type != macio_pangea &&
1799             macio->type != macio_intrepid)
1800                 return -ENODEV;
1801
1802         /* We power off the wireless slot in case it was not done
1803          * by the driver. We don't power it on automatically however
1804          */
1805         if (macio->flags & MACIO_FLAG_AIRPORT_ON)
1806                 core99_airport_enable(macio->of_node, 0, 0);
1807
1808         /* We power off the FW cable. Should be done by the driver... */
1809         if (macio->flags & MACIO_FLAG_FW_SUPPORTED) {
1810                 core99_firewire_enable(NULL, 0, 0);
1811                 core99_firewire_cable_power(NULL, 0, 0);
1812         }
1813
1814         /* We make sure int. modem is off (in case driver lost it) */
1815         if (macio->type == macio_keylargo)
1816                 core99_modem_enable(macio->of_node, 0, 0);
1817         else
1818                 pangea_modem_enable(macio->of_node, 0, 0);
1819
1820         /* We make sure the sound is off as well */
1821         core99_sound_chip_enable(macio->of_node, 0, 0);
1822
1823         /*
1824          * Save various bits of KeyLargo
1825          */
1826
1827         /* Save the state of the various GPIOs */
1828         save_gpio_levels[0] = MACIO_IN32(KEYLARGO_GPIO_LEVELS0);
1829         save_gpio_levels[1] = MACIO_IN32(KEYLARGO_GPIO_LEVELS1);
1830         for (i=0; i<KEYLARGO_GPIO_EXTINT_CNT; i++)
1831                 save_gpio_extint[i] = MACIO_IN8(KEYLARGO_GPIO_EXTINT_0+i);
1832         for (i=0; i<KEYLARGO_GPIO_CNT; i++)
1833                 save_gpio_normal[i] = MACIO_IN8(KEYLARGO_GPIO_0+i);
1834
1835         /* Save the FCRs */
1836         if (macio->type == macio_keylargo)
1837                 save_mbcr = MACIO_IN32(KEYLARGO_MBCR);
1838         save_fcr[0] = MACIO_IN32(KEYLARGO_FCR0);
1839         save_fcr[1] = MACIO_IN32(KEYLARGO_FCR1);
1840         save_fcr[2] = MACIO_IN32(KEYLARGO_FCR2);
1841         save_fcr[3] = MACIO_IN32(KEYLARGO_FCR3);
1842         save_fcr[4] = MACIO_IN32(KEYLARGO_FCR4);
1843         if (macio->type == macio_pangea || macio->type == macio_intrepid)
1844                 save_fcr[5] = MACIO_IN32(KEYLARGO_FCR5);
1845
1846         /* Save state & config of DBDMA channels */
1847         dbdma_save(macio, save_dbdma);
1848
1849         /*
1850          * Turn off as much as we can
1851          */
1852         if (macio->type == macio_pangea)
1853                 pangea_shutdown(macio, 1);
1854         else if (macio->type == macio_intrepid)
1855                 intrepid_shutdown(macio, 1);
1856         else if (macio->type == macio_keylargo)
1857                 keylargo_shutdown(macio, 1);
1858
1859         /*
1860          * Put the host bridge to sleep
1861          */
1862
1863         save_unin_clock_ctl = UN_IN(UNI_N_CLOCK_CNTL);
1864         /* Note: do not switch GMAC off, driver does it when necessary, WOL must keep it
1865          * enabled !
1866          */
1867         UN_OUT(UNI_N_CLOCK_CNTL, save_unin_clock_ctl &
1868                ~(/*UNI_N_CLOCK_CNTL_GMAC|*/UNI_N_CLOCK_CNTL_FW/*|UNI_N_CLOCK_CNTL_PCI*/));
1869         udelay(100);
1870         UN_OUT(UNI_N_HWINIT_STATE, UNI_N_HWINIT_STATE_SLEEPING);
1871         UN_OUT(UNI_N_POWER_MGT, UNI_N_POWER_MGT_SLEEP);
1872         mdelay(10);
1873
1874         /*
1875          * FIXME: A bit of black magic with OpenPIC (don't ask me why)
1876          */
1877         if (pmac_mb.model_id == PMAC_TYPE_SAWTOOTH) {
1878                 MACIO_BIS(0x506e0, 0x00400000);
1879                 MACIO_BIS(0x506e0, 0x80000000);
1880         }
1881         return 0;
1882 }
1883
1884 static int
1885 core99_wake_up(void)
1886 {
1887         struct macio_chip *macio;
1888         int i;
1889
1890         macio = &macio_chips[0];
1891         if (macio->type != macio_keylargo && macio->type != macio_pangea &&
1892             macio->type != macio_intrepid)
1893                 return -ENODEV;
1894
1895         /*
1896          * Wakeup the host bridge
1897          */
1898         UN_OUT(UNI_N_POWER_MGT, UNI_N_POWER_MGT_NORMAL);
1899         udelay(10);
1900         UN_OUT(UNI_N_HWINIT_STATE, UNI_N_HWINIT_STATE_RUNNING);
1901         udelay(10);
1902
1903         /*
1904          * Restore KeyLargo
1905          */
1906
1907         if (macio->type == macio_keylargo) {
1908                 MACIO_OUT32(KEYLARGO_MBCR, save_mbcr);
1909                 (void)MACIO_IN32(KEYLARGO_MBCR); udelay(10);
1910         }
1911         MACIO_OUT32(KEYLARGO_FCR0, save_fcr[0]);
1912         (void)MACIO_IN32(KEYLARGO_FCR0); udelay(10);
1913         MACIO_OUT32(KEYLARGO_FCR1, save_fcr[1]);
1914         (void)MACIO_IN32(KEYLARGO_FCR1); udelay(10);
1915         MACIO_OUT32(KEYLARGO_FCR2, save_fcr[2]);
1916         (void)MACIO_IN32(KEYLARGO_FCR2); udelay(10);
1917         MACIO_OUT32(KEYLARGO_FCR3, save_fcr[3]);
1918         (void)MACIO_IN32(KEYLARGO_FCR3); udelay(10);
1919         MACIO_OUT32(KEYLARGO_FCR4, save_fcr[4]);
1920         (void)MACIO_IN32(KEYLARGO_FCR4); udelay(10);
1921         if (macio->type == macio_pangea || macio->type == macio_intrepid) {
1922                 MACIO_OUT32(KEYLARGO_FCR5, save_fcr[5]);
1923                 (void)MACIO_IN32(KEYLARGO_FCR5); udelay(10);
1924         }
1925
1926         dbdma_restore(macio, save_dbdma);
1927
1928         MACIO_OUT32(KEYLARGO_GPIO_LEVELS0, save_gpio_levels[0]);
1929         MACIO_OUT32(KEYLARGO_GPIO_LEVELS1, save_gpio_levels[1]);
1930         for (i=0; i<KEYLARGO_GPIO_EXTINT_CNT; i++)
1931                 MACIO_OUT8(KEYLARGO_GPIO_EXTINT_0+i, save_gpio_extint[i]);
1932         for (i=0; i<KEYLARGO_GPIO_CNT; i++)
1933                 MACIO_OUT8(KEYLARGO_GPIO_0+i, save_gpio_normal[i]);
1934
1935         /* FIXME more black magic with OpenPIC ... */
1936         if (pmac_mb.model_id == PMAC_TYPE_SAWTOOTH) {
1937                 MACIO_BIC(0x506e0, 0x00400000);
1938                 MACIO_BIC(0x506e0, 0x80000000);
1939         }
1940
1941         UN_OUT(UNI_N_CLOCK_CNTL, save_unin_clock_ctl);
1942         udelay(100);
1943
1944         return 0;
1945 }
1946
1947 static long
1948 core99_sleep_state(struct device_node *node, long param, long value)
1949 {
1950         /* Param == 1 means to enter the "fake sleep" mode that is
1951          * used for CPU speed switch
1952          */
1953         if (param == 1) {
1954                 if (value == 1) {
1955                         UN_OUT(UNI_N_HWINIT_STATE, UNI_N_HWINIT_STATE_SLEEPING);
1956                         UN_OUT(UNI_N_POWER_MGT, UNI_N_POWER_MGT_IDLE2);
1957                 } else {
1958                         UN_OUT(UNI_N_POWER_MGT, UNI_N_POWER_MGT_NORMAL);
1959                         udelay(10);
1960                         UN_OUT(UNI_N_HWINIT_STATE, UNI_N_HWINIT_STATE_RUNNING);
1961                         udelay(10);
1962                 }
1963                 return 0;
1964         }
1965         if ((pmac_mb.board_flags & PMAC_MB_CAN_SLEEP) == 0)
1966                 return -EPERM;
1967
1968         if (value == 1)
1969                 return core99_sleep();
1970         else if (value == 0)
1971                 return core99_wake_up();
1972         return 0;
1973 }
1974
1975 #endif /* CONFIG_POWER4 */
1976
1977 static long
1978 generic_dev_can_wake(struct device_node *node, long param, long value)
1979 {
1980         /* Todo: eventually check we are really dealing with on-board
1981          * video device ...
1982          */
1983
1984         if (pmac_mb.board_flags & PMAC_MB_MAY_SLEEP)
1985                 pmac_mb.board_flags |= PMAC_MB_CAN_SLEEP;
1986         return 0;
1987 }
1988
1989 static long generic_get_mb_info(struct device_node *node, long param, long value)
1990 {
1991         switch(param) {
1992                 case PMAC_MB_INFO_MODEL:
1993                         return pmac_mb.model_id;
1994                 case PMAC_MB_INFO_FLAGS:
1995                         return pmac_mb.board_flags;
1996                 case PMAC_MB_INFO_NAME:
1997                         /* hack hack hack... but should work */
1998                         *((const char **)value) = pmac_mb.model_name;
1999                         return 0;
2000         }
2001         return -EINVAL;
2002 }
2003
2004
2005 /*
2006  * Table definitions
2007  */
2008
2009 /* Used on any machine
2010  */
2011 static struct feature_table_entry any_features[] = {
2012         { PMAC_FTR_GET_MB_INFO,         generic_get_mb_info },
2013         { PMAC_FTR_DEVICE_CAN_WAKE,     generic_dev_can_wake },
2014         { 0, NULL }
2015 };
2016
2017 #ifndef CONFIG_POWER4
2018
2019 /* OHare based motherboards. Currently, we only use these on the
2020  * 2400,3400 and 3500 series powerbooks. Some older desktops seem
2021  * to have issues with turning on/off those asic cells
2022  */
2023 static struct feature_table_entry ohare_features[] = {
2024         { PMAC_FTR_SCC_ENABLE,          ohare_htw_scc_enable },
2025         { PMAC_FTR_SWIM3_ENABLE,        ohare_floppy_enable },
2026         { PMAC_FTR_MESH_ENABLE,         ohare_mesh_enable },
2027         { PMAC_FTR_IDE_ENABLE,          ohare_ide_enable},
2028         { PMAC_FTR_IDE_RESET,           ohare_ide_reset},
2029         { PMAC_FTR_SLEEP_STATE,         ohare_sleep_state },
2030         { 0, NULL }
2031 };
2032
2033 /* Heathrow desktop machines (Beige G3).
2034  * Separated as some features couldn't be properly tested
2035  * and the serial port control bits appear to confuse it.
2036  */
2037 static struct feature_table_entry heathrow_desktop_features[] = {
2038         { PMAC_FTR_SWIM3_ENABLE,        heathrow_floppy_enable },
2039         { PMAC_FTR_MESH_ENABLE,         heathrow_mesh_enable },
2040         { PMAC_FTR_IDE_ENABLE,          heathrow_ide_enable },
2041         { PMAC_FTR_IDE_RESET,           heathrow_ide_reset },
2042         { PMAC_FTR_BMAC_ENABLE,         heathrow_bmac_enable },
2043         { 0, NULL }
2044 };
2045
2046 /* Heathrow based laptop, that is the Wallstreet and mainstreet
2047  * powerbooks.
2048  */
2049 static struct feature_table_entry heathrow_laptop_features[] = {
2050         { PMAC_FTR_SCC_ENABLE,          ohare_htw_scc_enable },
2051         { PMAC_FTR_MODEM_ENABLE,        heathrow_modem_enable },
2052         { PMAC_FTR_SWIM3_ENABLE,        heathrow_floppy_enable },
2053         { PMAC_FTR_MESH_ENABLE,         heathrow_mesh_enable },
2054         { PMAC_FTR_IDE_ENABLE,          heathrow_ide_enable },
2055         { PMAC_FTR_IDE_RESET,           heathrow_ide_reset },
2056         { PMAC_FTR_BMAC_ENABLE,         heathrow_bmac_enable },
2057         { PMAC_FTR_SOUND_CHIP_ENABLE,   heathrow_sound_enable },
2058         { PMAC_FTR_SLEEP_STATE,         heathrow_sleep_state },
2059         { 0, NULL }
2060 };
2061
2062 /* Paddington based machines
2063  * The lombard (101) powerbook, first iMac models, B&W G3 and Yikes G4.
2064  */
2065 static struct feature_table_entry paddington_features[] = {
2066         { PMAC_FTR_SCC_ENABLE,          ohare_htw_scc_enable },
2067         { PMAC_FTR_MODEM_ENABLE,        heathrow_modem_enable },
2068         { PMAC_FTR_SWIM3_ENABLE,        heathrow_floppy_enable },
2069         { PMAC_FTR_MESH_ENABLE,         heathrow_mesh_enable },
2070         { PMAC_FTR_IDE_ENABLE,          heathrow_ide_enable },
2071         { PMAC_FTR_IDE_RESET,           heathrow_ide_reset },
2072         { PMAC_FTR_BMAC_ENABLE,         heathrow_bmac_enable },
2073         { PMAC_FTR_SOUND_CHIP_ENABLE,   heathrow_sound_enable },
2074         { PMAC_FTR_SLEEP_STATE,         heathrow_sleep_state },
2075         { 0, NULL }
2076 };
2077
2078 /* Core99 & MacRISC 2 machines (all machines released since the
2079  * iBook (included), that is all AGP machines, except pangea
2080  * chipset. The pangea chipset is the "combo" UniNorth/KeyLargo
2081  * used on iBook2 & iMac "flow power".
2082  */
2083 static struct feature_table_entry core99_features[] = {
2084         { PMAC_FTR_SCC_ENABLE,          core99_scc_enable },
2085         { PMAC_FTR_MODEM_ENABLE,        core99_modem_enable },
2086         { PMAC_FTR_IDE_ENABLE,          core99_ide_enable },
2087         { PMAC_FTR_IDE_RESET,           core99_ide_reset },
2088         { PMAC_FTR_GMAC_ENABLE,         core99_gmac_enable },
2089         { PMAC_FTR_GMAC_PHY_RESET,      core99_gmac_phy_reset },
2090         { PMAC_FTR_SOUND_CHIP_ENABLE,   core99_sound_chip_enable },
2091         { PMAC_FTR_AIRPORT_ENABLE,      core99_airport_enable },
2092         { PMAC_FTR_USB_ENABLE,          core99_usb_enable },
2093         { PMAC_FTR_1394_ENABLE,         core99_firewire_enable },
2094         { PMAC_FTR_1394_CABLE_POWER,    core99_firewire_cable_power },
2095         { PMAC_FTR_SLEEP_STATE,         core99_sleep_state },
2096 #ifdef CONFIG_SMP
2097         { PMAC_FTR_RESET_CPU,           core99_reset_cpu },
2098 #endif /* CONFIG_SMP */
2099         { PMAC_FTR_READ_GPIO,           core99_read_gpio },
2100         { PMAC_FTR_WRITE_GPIO,          core99_write_gpio },
2101         { 0, NULL }
2102 };
2103
2104 /* RackMac
2105  */
2106 static struct feature_table_entry rackmac_features[] = {
2107         { PMAC_FTR_SCC_ENABLE,          core99_scc_enable },
2108         { PMAC_FTR_IDE_ENABLE,          core99_ide_enable },
2109         { PMAC_FTR_IDE_RESET,           core99_ide_reset },
2110         { PMAC_FTR_GMAC_ENABLE,         core99_gmac_enable },
2111         { PMAC_FTR_GMAC_PHY_RESET,      core99_gmac_phy_reset },
2112         { PMAC_FTR_USB_ENABLE,          core99_usb_enable },
2113         { PMAC_FTR_1394_ENABLE,         core99_firewire_enable },
2114         { PMAC_FTR_1394_CABLE_POWER,    core99_firewire_cable_power },
2115         { PMAC_FTR_SLEEP_STATE,         core99_sleep_state },
2116 #ifdef CONFIG_SMP
2117         { PMAC_FTR_RESET_CPU,           core99_reset_cpu },
2118 #endif /* CONFIG_SMP */
2119         { PMAC_FTR_READ_GPIO,           core99_read_gpio },
2120         { PMAC_FTR_WRITE_GPIO,          core99_write_gpio },
2121         { 0, NULL }
2122 };
2123
2124 /* Pangea features
2125  */
2126 static struct feature_table_entry pangea_features[] = {
2127         { PMAC_FTR_SCC_ENABLE,          core99_scc_enable },
2128         { PMAC_FTR_MODEM_ENABLE,        pangea_modem_enable },
2129         { PMAC_FTR_IDE_ENABLE,          core99_ide_enable },
2130         { PMAC_FTR_IDE_RESET,           core99_ide_reset },
2131         { PMAC_FTR_GMAC_ENABLE,         core99_gmac_enable },
2132         { PMAC_FTR_GMAC_PHY_RESET,      core99_gmac_phy_reset },
2133         { PMAC_FTR_SOUND_CHIP_ENABLE,   core99_sound_chip_enable },
2134         { PMAC_FTR_AIRPORT_ENABLE,      core99_airport_enable },
2135         { PMAC_FTR_USB_ENABLE,          core99_usb_enable },
2136         { PMAC_FTR_1394_ENABLE,         core99_firewire_enable },
2137         { PMAC_FTR_1394_CABLE_POWER,    core99_firewire_cable_power },
2138         { PMAC_FTR_SLEEP_STATE,         core99_sleep_state },
2139         { PMAC_FTR_READ_GPIO,           core99_read_gpio },
2140         { PMAC_FTR_WRITE_GPIO,          core99_write_gpio },
2141         { 0, NULL }
2142 };
2143
2144 /* Intrepid features
2145  */
2146 static struct feature_table_entry intrepid_features[] = {
2147         { PMAC_FTR_SCC_ENABLE,          core99_scc_enable },
2148         { PMAC_FTR_MODEM_ENABLE,        pangea_modem_enable },
2149         { PMAC_FTR_IDE_ENABLE,          core99_ide_enable },
2150         { PMAC_FTR_IDE_RESET,           core99_ide_reset },
2151         { PMAC_FTR_GMAC_ENABLE,         core99_gmac_enable },
2152         { PMAC_FTR_GMAC_PHY_RESET,      core99_gmac_phy_reset },
2153         { PMAC_FTR_SOUND_CHIP_ENABLE,   core99_sound_chip_enable },
2154         { PMAC_FTR_AIRPORT_ENABLE,      core99_airport_enable },
2155         { PMAC_FTR_USB_ENABLE,          core99_usb_enable },
2156         { PMAC_FTR_1394_ENABLE,         core99_firewire_enable },
2157         { PMAC_FTR_1394_CABLE_POWER,    core99_firewire_cable_power },
2158         { PMAC_FTR_SLEEP_STATE,         core99_sleep_state },
2159         { PMAC_FTR_READ_GPIO,           core99_read_gpio },
2160         { PMAC_FTR_WRITE_GPIO,          core99_write_gpio },
2161         { PMAC_FTR_AACK_DELAY_ENABLE,   intrepid_aack_delay_enable },
2162         { 0, NULL }
2163 };
2164
2165 #else /* CONFIG_POWER4 */
2166
2167 /* G5 features
2168  */
2169 static struct feature_table_entry g5_features[] = {
2170         { PMAC_FTR_GMAC_ENABLE,         g5_gmac_enable },
2171         { PMAC_FTR_1394_ENABLE,         g5_fw_enable },
2172         { PMAC_FTR_ENABLE_MPIC,         g5_mpic_enable },
2173         { PMAC_FTR_GMAC_PHY_RESET,      g5_eth_phy_reset },
2174         { PMAC_FTR_SOUND_CHIP_ENABLE,   g5_i2s_enable },
2175 #ifdef CONFIG_SMP
2176         { PMAC_FTR_RESET_CPU,           g5_reset_cpu },
2177 #endif /* CONFIG_SMP */
2178         { PMAC_FTR_READ_GPIO,           core99_read_gpio },
2179         { PMAC_FTR_WRITE_GPIO,          core99_write_gpio },
2180         { 0, NULL }
2181 };
2182
2183 #endif /* CONFIG_POWER4 */
2184
2185 static struct pmac_mb_def pmac_mb_defs[] = {
2186 #ifndef CONFIG_POWER4
2187         /*
2188          * Desktops
2189          */
2190
2191         {       "AAPL,8500",                    "PowerMac 8500/8600",
2192                 PMAC_TYPE_PSURGE,               NULL,
2193                 0
2194         },
2195         {       "AAPL,9500",                    "PowerMac 9500/9600",
2196                 PMAC_TYPE_PSURGE,               NULL,
2197                 0
2198         },
2199         {       "AAPL,7200",                    "PowerMac 7200",
2200                 PMAC_TYPE_PSURGE,               NULL,
2201                 0
2202         },
2203         {       "AAPL,7300",                    "PowerMac 7200/7300",
2204                 PMAC_TYPE_PSURGE,               NULL,
2205                 0
2206         },
2207         {       "AAPL,7500",                    "PowerMac 7500",
2208                 PMAC_TYPE_PSURGE,               NULL,
2209                 0
2210         },
2211         {       "AAPL,ShinerESB",               "Apple Network Server",
2212                 PMAC_TYPE_ANS,                  NULL,
2213                 0
2214         },
2215         {       "AAPL,e407",                    "Alchemy",
2216                 PMAC_TYPE_ALCHEMY,              NULL,
2217                 0
2218         },
2219         {       "AAPL,e411",                    "Gazelle",
2220                 PMAC_TYPE_GAZELLE,              NULL,
2221                 0
2222         },
2223         {       "AAPL,Gossamer",                "PowerMac G3 (Gossamer)",
2224                 PMAC_TYPE_GOSSAMER,             heathrow_desktop_features,
2225                 0
2226         },
2227         {       "AAPL,PowerMac G3",             "PowerMac G3 (Silk)",
2228                 PMAC_TYPE_SILK,                 heathrow_desktop_features,
2229                 0
2230         },
2231         {       "PowerMac1,1",                  "Blue&White G3",
2232                 PMAC_TYPE_YOSEMITE,             paddington_features,
2233                 0
2234         },
2235         {       "PowerMac1,2",                  "PowerMac G4 PCI Graphics",
2236                 PMAC_TYPE_YIKES,                paddington_features,
2237                 0
2238         },
2239         {       "PowerMac2,1",                  "iMac FireWire",
2240                 PMAC_TYPE_FW_IMAC,              core99_features,
2241                 PMAC_MB_MAY_SLEEP | PMAC_MB_OLD_CORE99
2242         },
2243         {       "PowerMac2,2",                  "iMac FireWire",
2244                 PMAC_TYPE_FW_IMAC,              core99_features,
2245                 PMAC_MB_MAY_SLEEP | PMAC_MB_OLD_CORE99
2246         },
2247         {       "PowerMac3,1",                  "PowerMac G4 AGP Graphics",
2248                 PMAC_TYPE_SAWTOOTH,             core99_features,
2249                 PMAC_MB_OLD_CORE99
2250         },
2251         {       "PowerMac3,2",                  "PowerMac G4 AGP Graphics",
2252                 PMAC_TYPE_SAWTOOTH,             core99_features,
2253                 PMAC_MB_MAY_SLEEP | PMAC_MB_OLD_CORE99
2254         },
2255         {       "PowerMac3,3",                  "PowerMac G4 AGP Graphics",
2256                 PMAC_TYPE_SAWTOOTH,             core99_features,
2257                 PMAC_MB_MAY_SLEEP | PMAC_MB_OLD_CORE99
2258         },
2259         {       "PowerMac3,4",                  "PowerMac G4 Silver",
2260                 PMAC_TYPE_QUICKSILVER,          core99_features,
2261                 PMAC_MB_MAY_SLEEP
2262         },
2263         {       "PowerMac3,5",                  "PowerMac G4 Silver",
2264                 PMAC_TYPE_QUICKSILVER,          core99_features,
2265                 PMAC_MB_MAY_SLEEP
2266         },
2267         {       "PowerMac3,6",                  "PowerMac G4 Windtunnel",
2268                 PMAC_TYPE_WINDTUNNEL,           core99_features,
2269                 PMAC_MB_MAY_SLEEP,
2270         },
2271         {       "PowerMac4,1",                  "iMac \"Flower Power\"",
2272                 PMAC_TYPE_PANGEA_IMAC,          pangea_features,
2273                 PMAC_MB_MAY_SLEEP
2274         },
2275         {       "PowerMac4,2",                  "Flat panel iMac",
2276                 PMAC_TYPE_FLAT_PANEL_IMAC,      pangea_features,
2277                 PMAC_MB_CAN_SLEEP
2278         },
2279         {       "PowerMac4,4",                  "eMac",
2280                 PMAC_TYPE_EMAC,                 core99_features,
2281                 PMAC_MB_MAY_SLEEP
2282         },
2283         {       "PowerMac5,1",                  "PowerMac G4 Cube",
2284                 PMAC_TYPE_CUBE,                 core99_features,
2285                 PMAC_MB_MAY_SLEEP | PMAC_MB_OLD_CORE99
2286         },
2287         {       "PowerMac6,1",                  "Flat panel iMac",
2288                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2289                 PMAC_MB_MAY_SLEEP,
2290         },
2291         {       "PowerMac6,3",                  "Flat panel iMac",
2292                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2293                 PMAC_MB_MAY_SLEEP,
2294         },
2295         {       "PowerMac6,4",                  "eMac",
2296                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2297                 PMAC_MB_MAY_SLEEP,
2298         },
2299         {       "PowerMac10,1",                 "Mac mini",
2300                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2301                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER,
2302         },
2303         {       "iMac,1",                       "iMac (first generation)",
2304                 PMAC_TYPE_ORIG_IMAC,            paddington_features,
2305                 0
2306         },
2307
2308         /*
2309          * Xserve's
2310          */
2311
2312         {       "RackMac1,1",                   "XServe",
2313                 PMAC_TYPE_RACKMAC,              rackmac_features,
2314                 0,
2315         },
2316         {       "RackMac1,2",                   "XServe rev. 2",
2317                 PMAC_TYPE_RACKMAC,              rackmac_features,
2318                 0,
2319         },
2320
2321         /*
2322          * Laptops
2323          */
2324
2325         {       "AAPL,3400/2400",               "PowerBook 3400",
2326                 PMAC_TYPE_HOOPER,               ohare_features,
2327                 PMAC_MB_CAN_SLEEP | PMAC_MB_MOBILE
2328         },
2329         {       "AAPL,3500",                    "PowerBook 3500",
2330                 PMAC_TYPE_KANGA,                ohare_features,
2331                 PMAC_MB_CAN_SLEEP | PMAC_MB_MOBILE
2332         },
2333         {       "AAPL,PowerBook1998",           "PowerBook Wallstreet",
2334                 PMAC_TYPE_WALLSTREET,           heathrow_laptop_features,
2335                 PMAC_MB_CAN_SLEEP | PMAC_MB_MOBILE
2336         },
2337         {       "PowerBook1,1",                 "PowerBook 101 (Lombard)",
2338                 PMAC_TYPE_101_PBOOK,            paddington_features,
2339                 PMAC_MB_CAN_SLEEP | PMAC_MB_MOBILE
2340         },
2341         {       "PowerBook2,1",                 "iBook (first generation)",
2342                 PMAC_TYPE_ORIG_IBOOK,           core99_features,
2343                 PMAC_MB_CAN_SLEEP | PMAC_MB_OLD_CORE99 | PMAC_MB_MOBILE
2344         },
2345         {       "PowerBook2,2",                 "iBook FireWire",
2346                 PMAC_TYPE_FW_IBOOK,             core99_features,
2347                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER |
2348                 PMAC_MB_OLD_CORE99 | PMAC_MB_MOBILE
2349         },
2350         {       "PowerBook3,1",                 "PowerBook Pismo",
2351                 PMAC_TYPE_PISMO,                core99_features,
2352                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER |
2353                 PMAC_MB_OLD_CORE99 | PMAC_MB_MOBILE
2354         },
2355         {       "PowerBook3,2",                 "PowerBook Titanium",
2356                 PMAC_TYPE_TITANIUM,             core99_features,
2357                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE
2358         },
2359         {       "PowerBook3,3",                 "PowerBook Titanium II",
2360                 PMAC_TYPE_TITANIUM2,            core99_features,
2361                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE
2362         },
2363         {       "PowerBook3,4",                 "PowerBook Titanium III",
2364                 PMAC_TYPE_TITANIUM3,            core99_features,
2365                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE
2366         },
2367         {       "PowerBook3,5",                 "PowerBook Titanium IV",
2368                 PMAC_TYPE_TITANIUM4,            core99_features,
2369                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE
2370         },
2371         {       "PowerBook4,1",                 "iBook 2",
2372                 PMAC_TYPE_IBOOK2,               pangea_features,
2373                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE
2374         },
2375         {       "PowerBook4,2",                 "iBook 2",
2376                 PMAC_TYPE_IBOOK2,               pangea_features,
2377                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE
2378         },
2379         {       "PowerBook4,3",                 "iBook 2 rev. 2",
2380                 PMAC_TYPE_IBOOK2,               pangea_features,
2381                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE
2382         },
2383         {       "PowerBook5,1",                 "PowerBook G4 17\"",
2384                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2385                 PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE,
2386         },
2387         {       "PowerBook5,2",                 "PowerBook G4 15\"",
2388                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2389                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE,
2390         },
2391         {       "PowerBook5,3",                 "PowerBook G4 17\"",
2392                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2393                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE,
2394         },
2395         {       "PowerBook5,4",                 "PowerBook G4 15\"",
2396                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2397                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE,
2398         },
2399         {       "PowerBook5,5",                 "PowerBook G4 17\"",
2400                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2401                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE,
2402         },
2403         {       "PowerBook5,6",                 "PowerBook G4 15\"",
2404                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2405                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE,
2406         },
2407         {       "PowerBook5,7",                 "PowerBook G4 17\"",
2408                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2409                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE,
2410         },
2411         {       "PowerBook5,8",                 "PowerBook G4 15\"",
2412                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2413                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE,
2414         },
2415         {       "PowerBook5,9",                 "PowerBook G4 17\"",
2416                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2417                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE,
2418         },
2419         {       "PowerBook6,1",                 "PowerBook G4 12\"",
2420                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2421                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE,
2422         },
2423         {       "PowerBook6,2",                 "PowerBook G4",
2424                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2425                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE,
2426         },
2427         {       "PowerBook6,3",                 "iBook G4",
2428                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2429                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE,
2430         },
2431         {       "PowerBook6,4",                 "PowerBook G4 12\"",
2432                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2433                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE,
2434         },
2435         {       "PowerBook6,5",                 "iBook G4",
2436                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2437                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE,
2438         },
2439         {       "PowerBook6,7",                 "iBook G4",
2440                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2441                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE,
2442         },
2443         {       "PowerBook6,8",                 "PowerBook G4 12\"",
2444                 PMAC_TYPE_UNKNOWN_INTREPID,     intrepid_features,
2445                 PMAC_MB_MAY_SLEEP | PMAC_MB_HAS_FW_POWER | PMAC_MB_MOBILE,
2446         },
2447 #else /* CONFIG_POWER4 */
2448         {       "PowerMac7,2",                  "PowerMac G5",
2449                 PMAC_TYPE_POWERMAC_G5,          g5_features,
2450                 0,
2451         },
2452 #ifdef CONFIG_PPC64
2453         {       "PowerMac7,3",                  "PowerMac G5",
2454                 PMAC_TYPE_POWERMAC_G5,          g5_features,
2455                 0,
2456         },
2457         {       "PowerMac8,1",                  "iMac G5",
2458                 PMAC_TYPE_IMAC_G5,              g5_features,
2459                 0,
2460         },
2461         {       "PowerMac9,1",                  "PowerMac G5",
2462                 PMAC_TYPE_POWERMAC_G5_U3L,      g5_features,
2463                 0,
2464         },
2465         {       "RackMac3,1",                   "XServe G5",
2466                 PMAC_TYPE_XSERVE_G5,            g5_features,
2467                 0,
2468         },
2469 #endif /* CONFIG_PPC64 */
2470 #endif /* CONFIG_POWER4 */
2471 };
2472
2473 /*
2474  * The toplevel feature_call callback
2475  */
2476 long pmac_do_feature_call(unsigned int selector, ...)
2477 {
2478         struct device_node *node;
2479         long param, value;
2480         int i;
2481         feature_call func = NULL;
2482         va_list args;
2483
2484         if (pmac_mb.features)
2485                 for (i=0; pmac_mb.features[i].function; i++)
2486                         if (pmac_mb.features[i].selector == selector) {
2487                                 func = pmac_mb.features[i].function;
2488                                 break;
2489                         }
2490         if (!func)
2491                 for (i=0; any_features[i].function; i++)
2492                         if (any_features[i].selector == selector) {
2493                                 func = any_features[i].function;
2494                                 break;
2495                         }
2496         if (!func)
2497                 return -ENODEV;
2498
2499         va_start(args, selector);
2500         node = (struct device_node*)va_arg(args, void*);
2501         param = va_arg(args, long);
2502         value = va_arg(args, long);
2503         va_end(args);
2504
2505         return func(node, param, value);
2506 }
2507
2508 static int __init probe_motherboard(void)
2509 {
2510         int i;
2511         struct macio_chip *macio = &macio_chips[0];
2512         const char *model = NULL;
2513         struct device_node *dt;
2514
2515         /* Lookup known motherboard type in device-tree. First try an
2516          * exact match on the "model" property, then try a "compatible"
2517          * match is none is found.
2518          */
2519         dt = find_devices("device-tree");
2520         if (dt != NULL)
2521                 model = (const char *) get_property(dt, "model", NULL);
2522         for(i=0; model && i<(sizeof(pmac_mb_defs)/sizeof(struct pmac_mb_def)); i++) {
2523             if (strcmp(model, pmac_mb_defs[i].model_string) == 0) {
2524                 pmac_mb = pmac_mb_defs[i];
2525                 goto found;
2526             }
2527         }
2528         for(i=0; i<(sizeof(pmac_mb_defs)/sizeof(struct pmac_mb_def)); i++) {
2529             if (machine_is_compatible(pmac_mb_defs[i].model_string)) {
2530                 pmac_mb = pmac_mb_defs[i];
2531                 goto found;
2532             }
2533         }
2534
2535         /* Fallback to selection depending on mac-io chip type */
2536         switch(macio->type) {
2537 #ifndef CONFIG_POWER4
2538             case macio_grand_central:
2539                 pmac_mb.model_id = PMAC_TYPE_PSURGE;
2540                 pmac_mb.model_name = "Unknown PowerSurge";
2541                 break;
2542             case macio_ohare:
2543                 pmac_mb.model_id = PMAC_TYPE_UNKNOWN_OHARE;
2544                 pmac_mb.model_name = "Unknown OHare-based";
2545                 break;
2546             case macio_heathrow:
2547                 pmac_mb.model_id = PMAC_TYPE_UNKNOWN_HEATHROW;
2548                 pmac_mb.model_name = "Unknown Heathrow-based";
2549                 pmac_mb.features = heathrow_desktop_features;
2550                 break;
2551             case macio_paddington:
2552                 pmac_mb.model_id = PMAC_TYPE_UNKNOWN_PADDINGTON;
2553                 pmac_mb.model_name = "Unknown Paddington-based";
2554                 pmac_mb.features = paddington_features;
2555                 break;
2556             case macio_keylargo:
2557                 pmac_mb.model_id = PMAC_TYPE_UNKNOWN_CORE99;
2558                 pmac_mb.model_name = "Unknown Keylargo-based";
2559                 pmac_mb.features = core99_features;
2560                 break;
2561             case macio_pangea:
2562                 pmac_mb.model_id = PMAC_TYPE_UNKNOWN_PANGEA;
2563                 pmac_mb.model_name = "Unknown Pangea-based";
2564                 pmac_mb.features = pangea_features;
2565                 break;
2566             case macio_intrepid:
2567                 pmac_mb.model_id = PMAC_TYPE_UNKNOWN_INTREPID;
2568                 pmac_mb.model_name = "Unknown Intrepid-based";
2569                 pmac_mb.features = intrepid_features;
2570                 break;
2571 #else /* CONFIG_POWER4 */
2572         case macio_keylargo2:
2573                 pmac_mb.model_id = PMAC_TYPE_UNKNOWN_K2;
2574                 pmac_mb.model_name = "Unknown K2-based";
2575                 pmac_mb.features = g5_features;
2576                 break;
2577 #endif /* CONFIG_POWER4 */
2578         default:
2579                 return -ENODEV;
2580         }
2581 found:
2582 #ifndef CONFIG_POWER4
2583         /* Fixup Hooper vs. Comet */
2584         if (pmac_mb.model_id == PMAC_TYPE_HOOPER) {
2585                 u32 __iomem * mach_id_ptr = ioremap(0xf3000034, 4);
2586                 if (!mach_id_ptr)
2587                         return -ENODEV;
2588                 /* Here, I used to disable the media-bay on comet. It
2589                  * appears this is wrong, the floppy connector is actually
2590                  * a kind of media-bay and works with the current driver.
2591                  */
2592                 if (__raw_readl(mach_id_ptr) & 0x20000000UL)
2593                         pmac_mb.model_id = PMAC_TYPE_COMET;
2594                 iounmap(mach_id_ptr);
2595         }
2596 #endif /* CONFIG_POWER4 */
2597
2598 #ifdef CONFIG_6xx
2599         /* Set default value of powersave_nap on machines that support it.
2600          * It appears that uninorth rev 3 has a problem with it, we don't
2601          * enable it on those. In theory, the flush-on-lock property is
2602          * supposed to be set when not supported, but I'm not very confident
2603          * that all Apple OF revs did it properly, I do it the paranoid way.
2604          */
2605         while (uninorth_base && uninorth_rev > 3) {
2606                 struct device_node *np = find_path_device("/cpus");
2607                 if (!np || !np->child) {
2608                         printk(KERN_WARNING "Can't find CPU(s) in device tree !\n");
2609                         break;
2610                 }
2611                 np = np->child;
2612                 /* Nap mode not supported on SMP */
2613                 if (np->sibling)
2614                         break;
2615                 /* Nap mode not supported if flush-on-lock property is present */
2616                 if (get_property(np, "flush-on-lock", NULL))
2617                         break;
2618                 powersave_nap = 1;
2619                 printk(KERN_INFO "Processor NAP mode on idle enabled.\n");
2620                 break;
2621         }
2622
2623         /* On CPUs that support it (750FX), lowspeed by default during
2624          * NAP mode
2625          */
2626         powersave_lowspeed = 1;
2627 #endif /* CONFIG_6xx */
2628 #ifdef CONFIG_POWER4
2629         powersave_nap = 1;
2630 #endif
2631         /* Check for "mobile" machine */
2632         if (model && (strncmp(model, "PowerBook", 9) == 0
2633                    || strncmp(model, "iBook", 5) == 0))
2634                 pmac_mb.board_flags |= PMAC_MB_MOBILE;
2635
2636
2637         printk(KERN_INFO "PowerMac motherboard: %s\n", pmac_mb.model_name);
2638         return 0;
2639 }
2640
2641 /* Initialize the Core99 UniNorth host bridge and memory controller
2642  */
2643 static void __init probe_uninorth(void)
2644 {
2645         u32 *addrp;
2646         phys_addr_t address;
2647         unsigned long actrl;
2648
2649         /* Locate core99 Uni-N */
2650         uninorth_node = of_find_node_by_name(NULL, "uni-n");
2651         /* Locate G5 u3 */
2652         if (uninorth_node == NULL) {
2653                 uninorth_node = of_find_node_by_name(NULL, "u3");
2654                 uninorth_u3 = 1;
2655         }
2656         if (uninorth_node == NULL)
2657                 return;
2658
2659         addrp = (u32 *)get_property(uninorth_node, "reg", NULL);
2660         if (addrp == NULL)
2661                 return;
2662         address = of_translate_address(uninorth_node, addrp);
2663         if (address == 0)
2664                 return;
2665         uninorth_base = ioremap(address, 0x40000);
2666         uninorth_rev = in_be32(UN_REG(UNI_N_VERSION));
2667         if (uninorth_u3)
2668                 u3_ht = ioremap(address + U3_HT_CONFIG_BASE, 0x1000);
2669
2670         printk(KERN_INFO "Found %s memory controller & host bridge,"
2671                " revision: %d\n", uninorth_u3 ? "U3" : "UniNorth",
2672                uninorth_rev);
2673         printk(KERN_INFO "Mapped at 0x%08lx\n", (unsigned long)uninorth_base);
2674
2675         /* Set the arbitrer QAck delay according to what Apple does
2676          */
2677         if (uninorth_rev < 0x11) {
2678                 actrl = UN_IN(UNI_N_ARB_CTRL) & ~UNI_N_ARB_CTRL_QACK_DELAY_MASK;
2679                 actrl |= ((uninorth_rev < 3) ? UNI_N_ARB_CTRL_QACK_DELAY105 :
2680                         UNI_N_ARB_CTRL_QACK_DELAY) << UNI_N_ARB_CTRL_QACK_DELAY_SHIFT;
2681                 UN_OUT(UNI_N_ARB_CTRL, actrl);
2682         }
2683
2684         /* Some more magic as done by them in recent MacOS X on UniNorth
2685          * revs 1.5 to 2.O and Pangea. Seem to toggle the UniN Maxbus/PCI
2686          * memory timeout
2687          */
2688         if ((uninorth_rev >= 0x11 && uninorth_rev <= 0x24) || uninorth_rev == 0xc0)
2689                 UN_OUT(0x2160, UN_IN(0x2160) & 0x00ffffff);
2690 }
2691
2692 static void __init probe_one_macio(const char *name, const char *compat, int type)
2693 {
2694         struct device_node*     node;
2695         int                     i;
2696         volatile u32 __iomem    *base;
2697         u32                     *addrp, *revp;
2698         phys_addr_t             addr;
2699         u64                     size;
2700
2701         for (node = NULL; (node = of_find_node_by_name(node, name)) != NULL;) {
2702                 if (!compat)
2703                         break;
2704                 if (device_is_compatible(node, compat))
2705                         break;
2706         }
2707         if (!node)
2708                 return;
2709         for(i=0; i<MAX_MACIO_CHIPS; i++) {
2710                 if (!macio_chips[i].of_node)
2711                         break;
2712                 if (macio_chips[i].of_node == node)
2713                         return;
2714         }
2715
2716         if (i >= MAX_MACIO_CHIPS) {
2717                 printk(KERN_ERR "pmac_feature: Please increase MAX_MACIO_CHIPS !\n");
2718                 printk(KERN_ERR "pmac_feature: %s skipped\n", node->full_name);
2719                 return;
2720         }
2721         addrp = of_get_pci_address(node, 0, &size, NULL);
2722         if (addrp == NULL) {
2723                 printk(KERN_ERR "pmac_feature: %s: can't find base !\n",
2724                        node->full_name);
2725                 return;
2726         }
2727         addr = of_translate_address(node, addrp);
2728         if (addr == 0) {
2729                 printk(KERN_ERR "pmac_feature: %s, can't translate base !\n",
2730                        node->full_name);
2731                 return;
2732         }
2733         base = ioremap(addr, (unsigned long)size);
2734         if (!base) {
2735                 printk(KERN_ERR "pmac_feature: %s, can't map mac-io chip !\n",
2736                        node->full_name);
2737                 return;
2738         }
2739         if (type == macio_keylargo) {
2740                 u32 *did = (u32 *)get_property(node, "device-id", NULL);
2741                 if (*did == 0x00000025)
2742                         type = macio_pangea;
2743                 if (*did == 0x0000003e)
2744                         type = macio_intrepid;
2745         }
2746         macio_chips[i].of_node  = node;
2747         macio_chips[i].type     = type;
2748         macio_chips[i].base     = base;
2749         macio_chips[i].flags    = MACIO_FLAG_SCCB_ON | MACIO_FLAG_SCCB_ON;
2750         macio_chips[i].name     = macio_names[type];
2751         revp = (u32 *)get_property(node, "revision-id", NULL);
2752         if (revp)
2753                 macio_chips[i].rev = *revp;
2754         printk(KERN_INFO "Found a %s mac-io controller, rev: %d, mapped at 0x%p\n",
2755                 macio_names[type], macio_chips[i].rev, macio_chips[i].base);
2756 }
2757
2758 static int __init
2759 probe_macios(void)
2760 {
2761         /* Warning, ordering is important */
2762         probe_one_macio("gc", NULL, macio_grand_central);
2763         probe_one_macio("ohare", NULL, macio_ohare);
2764         probe_one_macio("pci106b,7", NULL, macio_ohareII);
2765         probe_one_macio("mac-io", "keylargo", macio_keylargo);
2766         probe_one_macio("mac-io", "paddington", macio_paddington);
2767         probe_one_macio("mac-io", "gatwick", macio_gatwick);
2768         probe_one_macio("mac-io", "heathrow", macio_heathrow);
2769         probe_one_macio("mac-io", "K2-Keylargo", macio_keylargo2);
2770
2771         /* Make sure the "main" macio chip appear first */
2772         if (macio_chips[0].type == macio_gatwick
2773             && macio_chips[1].type == macio_heathrow) {
2774                 struct macio_chip temp = macio_chips[0];
2775                 macio_chips[0] = macio_chips[1];
2776                 macio_chips[1] = temp;
2777         }
2778         if (macio_chips[0].type == macio_ohareII
2779             && macio_chips[1].type == macio_ohare) {
2780                 struct macio_chip temp = macio_chips[0];
2781                 macio_chips[0] = macio_chips[1];
2782                 macio_chips[1] = temp;
2783         }
2784         macio_chips[0].lbus.index = 0;
2785         macio_chips[1].lbus.index = 1;
2786
2787         return (macio_chips[0].of_node == NULL) ? -ENODEV : 0;
2788 }
2789
2790 static void __init
2791 initial_serial_shutdown(struct device_node *np)
2792 {
2793         int len;
2794         struct slot_names_prop {
2795                 int     count;
2796                 char    name[1];
2797         } *slots;
2798         char *conn;
2799         int port_type = PMAC_SCC_ASYNC;
2800         int modem = 0;
2801
2802         slots = (struct slot_names_prop *)get_property(np, "slot-names", &len);
2803         conn = get_property(np, "AAPL,connector", &len);
2804         if (conn && (strcmp(conn, "infrared") == 0))
2805                 port_type = PMAC_SCC_IRDA;
2806         else if (device_is_compatible(np, "cobalt"))
2807                 modem = 1;
2808         else if (slots && slots->count > 0) {
2809                 if (strcmp(slots->name, "IrDA") == 0)
2810                         port_type = PMAC_SCC_IRDA;
2811                 else if (strcmp(slots->name, "Modem") == 0)
2812                         modem = 1;
2813         }
2814         if (modem)
2815                 pmac_call_feature(PMAC_FTR_MODEM_ENABLE, np, 0, 0);
2816         pmac_call_feature(PMAC_FTR_SCC_ENABLE, np, port_type, 0);
2817 }
2818
2819 static void __init
2820 set_initial_features(void)
2821 {
2822         struct device_node *np;
2823
2824         /* That hack appears to be necessary for some StarMax motherboards
2825          * but I'm not too sure it was audited for side-effects on other
2826          * ohare based machines...
2827          * Since I still have difficulties figuring the right way to
2828          * differenciate them all and since that hack was there for a long
2829          * time, I'll keep it around
2830          */
2831         if (macio_chips[0].type == macio_ohare && !find_devices("via-pmu")) {
2832                 struct macio_chip *macio = &macio_chips[0];
2833                 MACIO_OUT32(OHARE_FCR, STARMAX_FEATURES);
2834         } else if (macio_chips[0].type == macio_ohare) {
2835                 struct macio_chip *macio = &macio_chips[0];
2836                 MACIO_BIS(OHARE_FCR, OH_IOBUS_ENABLE);
2837         } else if (macio_chips[1].type == macio_ohare) {
2838                 struct macio_chip *macio = &macio_chips[1];
2839                 MACIO_BIS(OHARE_FCR, OH_IOBUS_ENABLE);
2840         }
2841
2842 #ifdef CONFIG_POWER4
2843         if (macio_chips[0].type == macio_keylargo2) {
2844 #ifndef CONFIG_SMP
2845                 /* On SMP machines running UP, we have the second CPU eating
2846                  * bus cycles. We need to take it off the bus. This is done
2847                  * from pmac_smp for SMP kernels running on one CPU
2848                  */
2849                 np = of_find_node_by_type(NULL, "cpu");
2850                 if (np != NULL)
2851                         np = of_find_node_by_type(np, "cpu");
2852                 if (np != NULL) {
2853                         g5_phy_disable_cpu1();
2854                         of_node_put(np);
2855                 }
2856 #endif /* CONFIG_SMP */
2857                 /* Enable GMAC for now for PCI probing. It will be disabled
2858                  * later on after PCI probe
2859                  */
2860                 np = of_find_node_by_name(NULL, "ethernet");
2861                 while(np) {
2862                         if (device_is_compatible(np, "K2-GMAC"))
2863                                 g5_gmac_enable(np, 0, 1);
2864                         np = of_find_node_by_name(np, "ethernet");
2865                 }
2866
2867                 /* Enable FW before PCI probe. Will be disabled later on
2868                  * Note: We should have a batter way to check that we are
2869                  * dealing with uninorth internal cell and not a PCI cell
2870                  * on the external PCI. The code below works though.
2871                  */
2872                 np = of_find_node_by_name(NULL, "firewire");
2873                 while(np) {
2874                         if (device_is_compatible(np, "pci106b,5811")) {
2875                                 macio_chips[0].flags |= MACIO_FLAG_FW_SUPPORTED;
2876                                 g5_fw_enable(np, 0, 1);
2877                         }
2878                         np = of_find_node_by_name(np, "firewire");
2879                 }
2880         }
2881 #else /* CONFIG_POWER4 */
2882
2883         if (macio_chips[0].type == macio_keylargo ||
2884             macio_chips[0].type == macio_pangea ||
2885             macio_chips[0].type == macio_intrepid) {
2886                 /* Enable GMAC for now for PCI probing. It will be disabled
2887                  * later on after PCI probe
2888                  */
2889                 np = of_find_node_by_name(NULL, "ethernet");
2890                 while(np) {
2891                         if (np->parent
2892                             && device_is_compatible(np->parent, "uni-north")
2893                             && device_is_compatible(np, "gmac"))
2894                                 core99_gmac_enable(np, 0, 1);
2895                         np = of_find_node_by_name(np, "ethernet");
2896                 }
2897
2898                 /* Enable FW before PCI probe. Will be disabled later on
2899                  * Note: We should have a batter way to check that we are
2900                  * dealing with uninorth internal cell and not a PCI cell
2901                  * on the external PCI. The code below works though.
2902                  */
2903                 np = of_find_node_by_name(NULL, "firewire");
2904                 while(np) {
2905                         if (np->parent
2906                             && device_is_compatible(np->parent, "uni-north")
2907                             && (device_is_compatible(np, "pci106b,18") ||
2908                                 device_is_compatible(np, "pci106b,30") ||
2909                                 device_is_compatible(np, "pci11c1,5811"))) {
2910                                 macio_chips[0].flags |= MACIO_FLAG_FW_SUPPORTED;
2911                                 core99_firewire_enable(np, 0, 1);
2912                         }
2913                         np = of_find_node_by_name(np, "firewire");
2914                 }
2915
2916                 /* Enable ATA-100 before PCI probe. */
2917                 np = of_find_node_by_name(NULL, "ata-6");
2918                 while(np) {
2919                         if (np->parent
2920                             && device_is_compatible(np->parent, "uni-north")
2921                             && device_is_compatible(np, "kauai-ata")) {
2922                                 core99_ata100_enable(np, 1);
2923                         }
2924                         np = of_find_node_by_name(np, "ata-6");
2925                 }
2926
2927                 /* Switch airport off */
2928                 np = find_devices("radio");
2929                 while(np) {
2930                         if (np && np->parent == macio_chips[0].of_node) {
2931                                 macio_chips[0].flags |= MACIO_FLAG_AIRPORT_ON;
2932                                 core99_airport_enable(np, 0, 0);
2933                         }
2934                         np = np->next;
2935                 }
2936         }
2937
2938         /* On all machines that support sound PM, switch sound off */
2939         if (macio_chips[0].of_node)
2940                 pmac_do_feature_call(PMAC_FTR_SOUND_CHIP_ENABLE,
2941                         macio_chips[0].of_node, 0, 0);
2942
2943         /* While on some desktop G3s, we turn it back on */
2944         if (macio_chips[0].of_node && macio_chips[0].type == macio_heathrow
2945                 && (pmac_mb.model_id == PMAC_TYPE_GOSSAMER ||
2946                     pmac_mb.model_id == PMAC_TYPE_SILK)) {
2947                 struct macio_chip *macio = &macio_chips[0];
2948                 MACIO_BIS(HEATHROW_FCR, HRW_SOUND_CLK_ENABLE);
2949                 MACIO_BIC(HEATHROW_FCR, HRW_SOUND_POWER_N);
2950         }
2951
2952         /* Some machine models need the clock chip to be properly setup for
2953          * clock spreading now. This should be a platform function but we
2954          * don't do these at the moment
2955          */
2956         pmac_tweak_clock_spreading(1);
2957
2958 #endif /* CONFIG_POWER4 */
2959
2960         /* On all machines, switch modem & serial ports off */
2961         np = find_devices("ch-a");
2962         while(np) {
2963                 initial_serial_shutdown(np);
2964                 np = np->next;
2965         }
2966         np = find_devices("ch-b");
2967         while(np) {
2968                 initial_serial_shutdown(np);
2969                 np = np->next;
2970         }
2971 }
2972
2973 void __init
2974 pmac_feature_init(void)
2975 {
2976         /* Detect the UniNorth memory controller */
2977         probe_uninorth();
2978
2979         /* Probe mac-io controllers */
2980         if (probe_macios()) {
2981                 printk(KERN_WARNING "No mac-io chip found\n");
2982                 return;
2983         }
2984
2985         /* Setup low-level i2c stuffs */
2986         pmac_init_low_i2c();
2987
2988         /* Probe machine type */
2989         if (probe_motherboard())
2990                 printk(KERN_WARNING "Unknown PowerMac !\n");
2991
2992         /* Set some initial features (turn off some chips that will
2993          * be later turned on)
2994          */
2995         set_initial_features();
2996 }
2997
2998 #if 0
2999 static void dump_HT_speeds(char *name, u32 cfg, u32 frq)
3000 {
3001         int     freqs[16] = { 200,300,400,500,600,800,1000,0,0,0,0,0,0,0,0,0 };
3002         int     bits[8] = { 8,16,0,32,2,4,0,0 };
3003         int     freq = (frq >> 8) & 0xf;
3004
3005         if (freqs[freq] == 0)
3006                 printk("%s: Unknown HT link frequency %x\n", name, freq);
3007         else
3008                 printk("%s: %d MHz on main link, (%d in / %d out) bits width\n",
3009                        name, freqs[freq],
3010                        bits[(cfg >> 28) & 0x7], bits[(cfg >> 24) & 0x7]);
3011 }
3012
3013 void __init pmac_check_ht_link(void)
3014 {
3015         u32     ufreq, freq, ucfg, cfg;
3016         struct device_node *pcix_node;
3017         u8      px_bus, px_devfn;
3018         struct pci_controller *px_hose;
3019
3020         (void)in_be32(u3_ht + U3_HT_LINK_COMMAND);
3021         ucfg = cfg = in_be32(u3_ht + U3_HT_LINK_CONFIG);
3022         ufreq = freq = in_be32(u3_ht + U3_HT_LINK_FREQ);
3023         dump_HT_speeds("U3 HyperTransport", cfg, freq);
3024
3025         pcix_node = of_find_compatible_node(NULL, "pci", "pci-x");
3026         if (pcix_node == NULL) {
3027                 printk("No PCI-X bridge found\n");
3028                 return;
3029         }
3030         if (pci_device_from_OF_node(pcix_node, &px_bus, &px_devfn) != 0) {
3031                 printk("PCI-X bridge found but not matched to pci\n");
3032                 return;
3033         }
3034         px_hose = pci_find_hose_for_OF_device(pcix_node);
3035         if (px_hose == NULL) {
3036                 printk("PCI-X bridge found but not matched to host\n");
3037                 return;
3038         }       
3039         early_read_config_dword(px_hose, px_bus, px_devfn, 0xc4, &cfg);
3040         early_read_config_dword(px_hose, px_bus, px_devfn, 0xcc, &freq);
3041         dump_HT_speeds("PCI-X HT Uplink", cfg, freq);
3042         early_read_config_dword(px_hose, px_bus, px_devfn, 0xc8, &cfg);
3043         early_read_config_dword(px_hose, px_bus, px_devfn, 0xd0, &freq);
3044         dump_HT_speeds("PCI-X HT Downlink", cfg, freq);
3045 }
3046 #endif /* 0 */
3047
3048 /*
3049  * Early video resume hook
3050  */
3051
3052 static void (*pmac_early_vresume_proc)(void *data);
3053 static void *pmac_early_vresume_data;
3054
3055 void pmac_set_early_video_resume(void (*proc)(void *data), void *data)
3056 {
3057         if (_machine != _MACH_Pmac)
3058                 return;
3059         preempt_disable();
3060         pmac_early_vresume_proc = proc;
3061         pmac_early_vresume_data = data;
3062         preempt_enable();
3063 }
3064 EXPORT_SYMBOL(pmac_set_early_video_resume);
3065
3066 void pmac_call_early_video_resume(void)
3067 {
3068         if (pmac_early_vresume_proc)
3069                 pmac_early_vresume_proc(pmac_early_vresume_data);
3070 }
3071
3072 /*
3073  * AGP related suspend/resume code
3074  */
3075
3076 static struct pci_dev *pmac_agp_bridge;
3077 static int (*pmac_agp_suspend)(struct pci_dev *bridge);
3078 static int (*pmac_agp_resume)(struct pci_dev *bridge);
3079
3080 void pmac_register_agp_pm(struct pci_dev *bridge,
3081                                  int (*suspend)(struct pci_dev *bridge),
3082                                  int (*resume)(struct pci_dev *bridge))
3083 {
3084         if (suspend || resume) {
3085                 pmac_agp_bridge = bridge;
3086                 pmac_agp_suspend = suspend;
3087                 pmac_agp_resume = resume;
3088                 return;
3089         }
3090         if (bridge != pmac_agp_bridge)
3091                 return;
3092         pmac_agp_suspend = pmac_agp_resume = NULL;
3093         return;
3094 }
3095 EXPORT_SYMBOL(pmac_register_agp_pm);
3096
3097 void pmac_suspend_agp_for_card(struct pci_dev *dev)
3098 {
3099         if (pmac_agp_bridge == NULL || pmac_agp_suspend == NULL)
3100                 return;
3101         if (pmac_agp_bridge->bus != dev->bus)
3102                 return;
3103         pmac_agp_suspend(pmac_agp_bridge);
3104 }
3105 EXPORT_SYMBOL(pmac_suspend_agp_for_card);
3106
3107 void pmac_resume_agp_for_card(struct pci_dev *dev)
3108 {
3109         if (pmac_agp_bridge == NULL || pmac_agp_resume == NULL)
3110                 return;
3111         if (pmac_agp_bridge->bus != dev->bus)
3112                 return;
3113         pmac_agp_resume(pmac_agp_bridge);
3114 }
3115 EXPORT_SYMBOL(pmac_resume_agp_for_card);