fix to allow usb modules to compile
[linux-2.4.21-pre4.git] / arch / ppc / platforms / prep_setup.c
1 /*
2  * BK Id: SCCS/s.prep_setup.c 1.76 04/16/02 21:42:09 paulus
3  */
4 /*
5  *  arch/ppc/platforms/setup.c
6  *
7  *  Copyright (C) 1995  Linus Torvalds
8  *  Adapted from 'alpha' version by Gary Thomas
9  *  Modified by Cort Dougan (cort@cs.nmt.edu)
10  *
11  * Support for PReP (Motorola MTX/MVME)
12  * by Troy Benjegerdes (hozer@drgw.net)
13  */
14
15 /*
16  * bootup setup stuff..
17  */
18
19 #include <linux/config.h>
20 #include <linux/delay.h>
21 #include <linux/module.h>
22 #include <linux/errno.h>
23 #include <linux/sched.h>
24 #include <linux/kernel.h>
25 #include <linux/mm.h>
26 #include <linux/stddef.h>
27 #include <linux/unistd.h>
28 #include <linux/ptrace.h>
29 #include <linux/slab.h>
30 #include <linux/user.h>
31 #include <linux/a.out.h>
32 #include <linux/tty.h>
33 #include <linux/major.h>
34 #include <linux/interrupt.h>
35 #include <linux/reboot.h>
36 #include <linux/init.h>
37 #include <linux/blk.h>
38 #include <linux/ioport.h>
39 #include <linux/console.h>
40 #include <linux/timex.h>
41 #include <linux/pci.h>
42 #include <linux/ide.h>
43 #include <linux/seq_file.h>
44
45 #include <asm/sections.h>
46 #include <asm/mmu.h>
47 #include <asm/processor.h>
48 #include <asm/residual.h>
49 #include <asm/io.h>
50 #include <asm/pgtable.h>
51 #include <asm/cache.h>
52 #include <asm/dma.h>
53 #include <asm/machdep.h>
54 #include <asm/mk48t59.h>
55 #include <asm/prep_nvram.h>
56 #include <asm/raven.h>
57 #include <asm/keyboard.h>
58 #include <asm/vga.h>
59 #include <asm/time.h>
60 #include <asm/i8259.h>
61 #include <asm/open_pic.h>
62 #include <asm/pci-bridge.h>
63 #include <asm/bootinfo.h>
64
65 unsigned char ucSystemType;
66 unsigned char ucBoardRev;
67 unsigned char ucBoardRevMaj, ucBoardRevMin;
68
69 extern unsigned long mc146818_get_rtc_time(void);
70 extern int mc146818_set_rtc_time(unsigned long nowtime);
71 extern unsigned long mk48t59_get_rtc_time(void);
72 extern int mk48t59_set_rtc_time(unsigned long nowtime);
73
74 extern unsigned char prep_nvram_read_val(int addr);
75 extern void prep_nvram_write_val(int addr,
76                                  unsigned char val);
77 extern unsigned char rs_nvram_read_val(int addr);
78 extern void rs_nvram_write_val(int addr,
79                                  unsigned char val);
80 extern void ibm_prep_init(void);
81
82 extern int pckbd_setkeycode(unsigned int scancode, unsigned int keycode);
83 extern int pckbd_getkeycode(unsigned int scancode);
84 extern int pckbd_translate(unsigned char scancode, unsigned char *keycode,
85                 char raw_mode);
86 extern char pckbd_unexpected_up(unsigned char keycode);
87 extern void pckbd_leds(unsigned char leds);
88 extern void pckbd_init_hw(void);
89 extern unsigned char pckbd_sysrq_xlate[];
90
91 extern void prep_find_bridges(void);
92 extern char saved_command_line[];
93
94 int _prep_type;
95
96 extern void prep_sandalfoot_setup_pci(char *irq_edge_mask_lo, char *irq_edge_mask_hi);
97 extern void prep_thinkpad_setup_pci(char *irq_edge_mask_lo, char *irq_edge_mask_hi);
98 extern void prep_carolina_setup_pci(char *irq_edge_mask_lo, char *irq_edge_mask_hi);
99 extern void prep_tiger1_setup_pci(char *irq_edge_mask_lo, char *irq_edge_mask_hi);
100
101
102 #define cached_21       (((char *)(ppc_cached_irq_mask))[3])
103 #define cached_A1       (((char *)(ppc_cached_irq_mask))[2])
104
105 /* for the mac fs */
106 kdev_t boot_dev;
107
108 #ifdef CONFIG_SOUND_CS4232 
109 long ppc_cs4232_dma, ppc_cs4232_dma2;
110 #endif
111
112 extern PTE *Hash, *Hash_end;
113 extern unsigned long Hash_size, Hash_mask;
114 extern int probingmem;
115 extern unsigned long loops_per_jiffy;
116
117 #ifdef CONFIG_SOUND_CS4232 
118 EXPORT_SYMBOL(ppc_cs4232_dma);
119 EXPORT_SYMBOL(ppc_cs4232_dma2);
120 #endif
121
122 /* useful ISA ports */
123 #define PREP_SYSCTL     0x81c
124 /* present in the IBM reference design; possibly identical in Mot boxes: */
125 #define PREP_IBM_SIMM_ID        0x803 /* SIMM size: 32 or 8 MiB */
126 #define PREP_IBM_SIMM_PRESENCE  0x804
127 #define PREP_IBM_EQUIPMENT      0x80c
128 #define PREP_IBM_L2INFO 0x80d
129 #define PREP_IBM_PM1    0x82a /* power management register 1 */
130 #define PREP_IBM_PLANAR 0x852 /* planar ID - identifies the motherboard */
131
132 /* Equipment Present Register masks: */
133 #define PREP_IBM_EQUIPMENT_RESERVED     0x80
134 #define PREP_IBM_EQUIPMENT_SCSIFUSE     0x40
135 #define PREP_IBM_EQUIPMENT_L2_COPYBACK  0x08
136 #define PREP_IBM_EQUIPMENT_L2_256       0x04
137 #define PREP_IBM_EQUIPMENT_CPU  0x02
138 #define PREP_IBM_EQUIPMENT_L2   0x01
139
140 /* planar ID values: */
141 /* Sandalfoot/Sandalbow (6015/7020) */
142 #define PREP_IBM_SANDALFOOT     0xfc
143 /* Woodfield, Thinkpad 850/860 (6042/7249) */
144 #define PREP_IBM_THINKPAD       0xff /* planar ID unimplemented */
145 /* PowerSeries 830/850 (6050/6070) */
146 #define PREP_IBM_CAROLINA_IDE_0 0xf0
147 #define PREP_IBM_CAROLINA_IDE_1 0xf1
148 #define PREP_IBM_CAROLINA_IDE_2 0xf2
149 /* 7248-43P */
150 #define PREP_IBM_CAROLINA_SCSI_0        0xf4
151 #define PREP_IBM_CAROLINA_SCSI_1        0xf5
152 #define PREP_IBM_CAROLINA_SCSI_2        0xf6
153 #define PREP_IBM_CAROLINA_SCSI_3        0xf7 /* missing from Carolina Tech Spec */
154 /* Tiger1 (7043-140) */
155 #define PREP_IBM_TIGER1_133             0xd1
156 #define PREP_IBM_TIGER1_166             0xd2
157 #define PREP_IBM_TIGER1_180             0xd3
158 #define PREP_IBM_TIGER1_xxx             0xd4 /* unknown, but probably exists */
159 #define PREP_IBM_TIGER1_333             0xd5 /* missing from Tiger Tech Spec */
160
161 /* setup_ibm_pci:
162  *      set Motherboard_map_name, Motherboard_map, Motherboard_routes.
163  *      return 8259 edge/level masks.
164  */
165 void (*setup_ibm_pci)(char *irq_lo, char *irq_hi);
166
167 extern char *Motherboard_map_name; /* for use in *_cpuinfo */
168
169 /*
170  * As found in the PReP reference implementation.
171  * Used by Thinkpad, Sandalfoot (6015/7020), and all Motorola PReP.
172  */
173 static void __init
174 prep_gen_enable_l2(void)
175 {
176         outb(inb(PREP_SYSCTL) | 0x3, PREP_SYSCTL);
177 }
178
179 /* Used by Carolina and Tiger1 */
180 static void __init
181 prep_carolina_enable_l2(void)
182 {
183         outb(inb(PREP_SYSCTL) | 0xc0, PREP_SYSCTL);
184 }
185
186 /* cpuinfo code common to all IBM PReP */
187 static void __prep
188 prep_ibm_cpuinfo(struct seq_file *m)
189 {
190         unsigned int equip_reg = inb(PREP_IBM_EQUIPMENT);
191
192         seq_printf(m, "machine\t\t: PReP %s\n", Motherboard_map_name);
193
194         seq_printf(m, "upgrade cpu\t: ");
195         if (equip_reg & PREP_IBM_EQUIPMENT_CPU) {
196                 seq_printf(m, "not ");
197         }
198         seq_printf(m, "present\n");
199
200         /* print info about the SCSI fuse */
201         seq_printf(m, "scsi fuse\t: ");
202         if (equip_reg & PREP_IBM_EQUIPMENT_SCSIFUSE) 
203                 seq_printf(m, "ok");
204         else
205                 seq_printf(m, "bad");
206         seq_printf(m, "\n");
207
208 #ifdef CONFIG_PREP_RESIDUAL
209         /* print info about SIMMs */
210         if (res->ResidualLength != 0) {
211                 int i;
212                 seq_printf(m, "simms\t\t: ");
213                 for (i = 0; (res->ActualNumMemories) && (i < MAX_MEMS); i++) {
214                         if (res->Memories[i].SIMMSize != 0)
215                                 seq_printf(m, "%d:%ldMiB ", i,
216                                         (res->Memories[i].SIMMSize > 1024) ? 
217                                         res->Memories[i].SIMMSize>>20 : 
218                                         res->Memories[i].SIMMSize);
219                 }
220                 seq_printf(m, "\n");
221         }
222 #endif
223 }
224
225 static int __prep
226 prep_sandalfoot_cpuinfo(struct seq_file *m)
227 {
228         unsigned int equip_reg = inb(PREP_IBM_EQUIPMENT);
229
230         prep_ibm_cpuinfo(m);
231
232         /* report amount and type of L2 cache present */
233         seq_printf(m, "L2 cache\t: ");
234         if (equip_reg & PREP_IBM_EQUIPMENT_L2) {
235                 seq_printf(m, "not present");
236         } else {
237                 if (equip_reg & PREP_IBM_EQUIPMENT_L2_256)
238                         seq_printf(m, "256KiB");
239                 else
240                         seq_printf(m, "unknown size");
241
242                 if (equip_reg & PREP_IBM_EQUIPMENT_L2_COPYBACK)
243                         seq_printf(m, ", copy-back");
244                 else
245                         seq_printf(m, ", write-through");
246         }
247         seq_printf(m, "\n");
248
249         return 0;
250 }
251
252 static int __prep
253 prep_thinkpad_cpuinfo(struct seq_file *m)
254 {
255         unsigned int equip_reg = inb(PREP_IBM_EQUIPMENT);
256         char *cpubus_speed, *pci_speed;
257
258         prep_ibm_cpuinfo(m);
259
260         /* report amount and type of L2 cache present */
261         seq_printf(m, "l2 cache\t: ");
262         if ((equip_reg & 0x1) == 0) {
263                 switch ((equip_reg & 0xc) >> 2) {
264                         case 0x0:
265                                 seq_printf(m, "128KiB look-aside 2-way write-through\n");
266                                 break;
267                         case 0x1:
268                                 seq_printf(m, "512KiB look-aside direct-mapped write-back\n");
269                                 break;
270                         case 0x2:
271                                 seq_printf(m, "256KiB look-aside 2-way write-through\n");
272                                 break;
273                         case 0x3:
274                                 seq_printf(m, "256KiB look-aside direct-mapped write-back\n");
275                                 break;
276                 }
277         } else {
278                 seq_printf(m, "not present\n");
279         }
280
281         /* report bus speeds because we can */
282         if ((equip_reg & 0x80) == 0) {
283                 switch ((equip_reg & 0x30) >> 4) {
284                         case 0x1:
285                                 cpubus_speed = "50";
286                                 pci_speed = "25";
287                                 break;
288                         case 0x3:
289                                 cpubus_speed = "66";
290                                 pci_speed = "33";
291                                 break;
292                         default:
293                                 cpubus_speed = "unknown";
294                                 pci_speed = "unknown";
295                                 break;
296                 }
297         } else {
298                 switch ((equip_reg & 0x30) >> 4) {
299                         case 0x1:
300                                 cpubus_speed = "25";
301                                 pci_speed = "25";
302                                 break;
303                         case 0x2:
304                                 cpubus_speed = "60";
305                                 pci_speed = "30";
306                                 break;
307                         case 0x3:
308                                 cpubus_speed = "33";
309                                 pci_speed = "33";
310                                 break;
311                         default:
312                                 cpubus_speed = "unknown";
313                                 pci_speed = "unknown";
314                                 break;
315                 }
316         }
317         seq_printf(m, "60x bus\t\t: %sMHz\n", cpubus_speed);
318         seq_printf(m, "pci bus\t\t: %sMHz\n", pci_speed);
319
320         return 0;
321 }
322
323 static int __prep
324 prep_carolina_cpuinfo(struct seq_file *m)
325 {
326         unsigned int equip_reg = inb(PREP_IBM_EQUIPMENT);
327
328         prep_ibm_cpuinfo(m);
329
330         /* report amount and type of L2 cache present */
331         seq_printf(m, "l2 cache\t: ");
332         if ((equip_reg & 0x1) == 0) {
333                 unsigned int l2_reg = inb(PREP_IBM_L2INFO);
334
335                 /* L2 size */
336                 switch ((l2_reg & 0x60) >> 5) {
337                         case 0:
338                                 seq_printf(m, "256KiB");
339                                 break;
340                         case 1:
341                                 seq_printf(m, "512KiB");
342                                 break;
343                         default:
344                                 seq_printf(m, "unknown size");
345                                 break;
346                 }
347
348                 /* L2 type */
349                 if ((l2_reg & 0x3) == 0)
350                         seq_printf(m, ", async");
351                 else if ((l2_reg & 0x3) == 1)
352                         seq_printf(m, ", sync");
353                 else
354                         seq_printf(m, ", unknown type");
355
356                 seq_printf(m, "\n");
357         } else {
358                 seq_printf(m, "not present\n");
359         }
360
361         return 0;
362 }
363
364 static int __prep
365 prep_tiger1_cpuinfo(struct seq_file *m)
366 {
367         unsigned int l2_reg = inb(PREP_IBM_L2INFO);
368
369         prep_ibm_cpuinfo(m);
370
371         /* report amount and type of L2 cache present */
372         seq_printf(m, "l2 cache\t: ");
373         if ((l2_reg & 0xf) == 0xf) {
374                 seq_printf(m, "not present\n");
375         } else {
376                 if (l2_reg & 0x8)
377                         seq_printf(m, "async, ");
378                 else
379                         seq_printf(m, "sync burst, ");
380                 
381                 if (l2_reg & 0x4)
382                         seq_printf(m, "parity, ");
383                 else
384                         seq_printf(m, "no parity, ");
385                 
386                 switch (l2_reg & 0x3) {
387                         case 0x0:
388                                 seq_printf(m, "256KiB\n");
389                                 break;
390                         case 0x1:
391                                 seq_printf(m, "512KiB\n");
392                                 break;
393                         case 0x2:
394                                 seq_printf(m, "1MiB\n");
395                                 break;
396                         default:
397                                 seq_printf(m, "unknown size\n");
398                                 break;
399                 }
400         }
401
402         return 0;
403 }
404
405
406 /* Used by all Motorola PReP */
407 static int __prep
408 prep_mot_cpuinfo(struct seq_file *m)
409 {
410         unsigned int cachew = *((unsigned char *)CACHECRBA);
411
412         seq_printf(m, "machine\t\t: PReP %s\n", Motherboard_map_name);
413
414         /* report amount and type of L2 cache present */
415         seq_printf(m, "l2 cache\t: ");
416         switch (cachew & L2CACHE_MASK) {
417                 case L2CACHE_512KB:
418                         seq_printf(m, "512KiB");
419                         break;
420                 case L2CACHE_256KB:
421                         seq_printf(m, "256KiB");
422                         break;
423                 case L2CACHE_1MB:
424                         seq_printf(m, "1MiB");
425                         break;
426                 case L2CACHE_NONE:
427                         seq_printf(m, "none\n");
428                         goto no_l2;
429                         break;
430                 default:
431                         seq_printf(m, "%x\n", cachew);
432         }
433
434         seq_printf(m, ", parity %s",
435                         (cachew & L2CACHE_PARITY)? "enabled" : "disabled");
436
437         seq_printf(m, " SRAM:");
438
439         switch ( ((cachew & 0xf0) >> 4) & ~(0x3) ) {
440                 case 1: seq_printf(m, "synchronous, parity, flow-through\n");
441                                 break;
442                 case 2: seq_printf(m, "asynchronous, no parity\n");
443                                 break;
444                 case 3: seq_printf(m, "asynchronous, parity\n");
445                                 break;
446                 default:seq_printf(m, "synchronous, pipelined, no parity\n");
447                                 break;
448         }
449
450 no_l2:
451 #ifdef CONFIG_PREP_RESIDUAL
452         /* print info about SIMMs */
453         if (res->ResidualLength != 0) {
454                 int i;
455                 seq_printf(m, "simms\t\t: ");
456                 for (i = 0; (res->ActualNumMemories) && (i < MAX_MEMS); i++) {
457                         if (res->Memories[i].SIMMSize != 0)
458                                 seq_printf(m, "%d:%ldM ", i,
459                                         (res->Memories[i].SIMMSize > 1024) ? 
460                                         res->Memories[i].SIMMSize>>20 : 
461                                         res->Memories[i].SIMMSize);
462                 }
463                 seq_printf(m, "\n");
464         }
465 #endif
466
467         return 0;
468 }
469
470 static void __prep
471 prep_restart(char *cmd)
472 {
473 #define PREP_SP92       0x92    /* Special Port 92 */
474         __cli(); /* no interrupts */
475
476         /* set exception prefix high - to the prom */
477         _nmask_and_or_msr(0, MSR_IP);
478
479         /* make sure bit 0 (reset) is a 0 */
480         outb( inb(PREP_SP92) & ~1L , PREP_SP92);
481         /* signal a reset to system control port A - soft reset */
482         outb( inb(PREP_SP92) | 1 , PREP_SP92);
483
484         while ( 1 ) ;
485         /* not reached */
486 #undef PREP_SP92
487 }
488
489 static void __prep
490 prep_halt(void)
491 {
492         __cli(); /* no interrupts */
493
494         /* set exception prefix high - to the prom */
495         _nmask_and_or_msr(0, MSR_IP);
496
497         while ( 1 ) ;
498         /* not reached */
499 }
500
501 /* Carrera is the power manager in the Thinkpads. Unfortunately not much is
502  * known about it, so we can't power down.
503  */
504 static void __prep
505 prep_carrera_poweroff(void)
506 {
507         prep_halt();
508 }
509
510 /*
511  * On most IBM PReP's, power management is handled by a Signetics 87c750
512  * behind the Utah component on the ISA bus. To access the 750 you must write
513  * a series of nibbles to port 0x82a (decoded by the Utah). This is described
514  * somewhat in the IBM Carolina Technical Specification.
515  * -Hollis
516  */
517 static void __prep
518 utah_sig87c750_setbit(unsigned int bytenum, unsigned int bitnum, int value)
519 {
520         /*
521          * byte1: 0 0 0 1 0  d  a5 a4
522          * byte2: 0 0 0 1 a3 a2 a1 a0
523          *
524          * d = the bit's value, enabled or disabled
525          * (a5 a4 a3) = the byte number, minus 20
526          * (a2 a1 a0) = the bit number
527          *
528          * example: set the 5th bit of byte 21 (21.5)
529          *     a5 a4 a3 = 001 (byte 1)
530          *     a2 a1 a0 = 101 (bit 5)
531          *
532          *     byte1 = 0001 0100 (0x14)
533          *     byte2 = 0001 1101 (0x1d)
534          */
535         unsigned char byte1=0x10, byte2=0x10;
536
537         /* the 750's '20.0' is accessed as '0.0' through Utah (which adds 20) */
538         bytenum -= 20;
539
540         byte1 |= (!!value) << 2;                /* set d */
541         byte1 |= (bytenum >> 1) & 0x3;  /* set a5, a4 */
542
543         byte2 |= (bytenum & 0x1) << 3;  /* set a3 */
544         byte2 |= bitnum & 0x7;                  /* set a2, a1, a0 */
545
546         outb(byte1, PREP_IBM_PM1);      /* first nibble */
547         mb();
548         udelay(100);                            /* important: let controller recover */
549
550         outb(byte2, PREP_IBM_PM1);      /* second nibble */
551         mb();
552         udelay(100);                            /* important: let controller recover */
553 }
554
555 static void __prep
556 prep_sig750_poweroff(void)
557 {
558         /* tweak the power manager found in most IBM PRePs (except Thinkpads) */
559         unsigned long flags;
560         __cli();
561         /* set exception prefix high - to the prom */
562         save_flags( flags );
563         restore_flags( flags|MSR_IP );
564
565         utah_sig87c750_setbit(21, 5, 1); /* set bit 21.5, "PMEXEC_OFF" */
566
567         while (1) ;
568         /* not reached */
569 }
570
571 static int __prep
572 prep_show_percpuinfo(struct seq_file *m, int i)
573 {
574         /* PREP's without residual data will give incorrect values here */
575         seq_printf(m, "clock\t\t: ");
576 #ifdef CONFIG_PREP_RESIDUAL     
577         if (res->ResidualLength)
578                 seq_printf(m, "%ldMHz\n",
579                            (res->VitalProductData.ProcessorHz > 1024) ?
580                            res->VitalProductData.ProcessorHz / 1000000 :
581                            res->VitalProductData.ProcessorHz);
582         else
583 #endif /* CONFIG_PREP_RESIDUAL */
584                 seq_printf(m, "???\n");
585
586         return 0;
587 }
588
589 #ifdef CONFIG_SOUND_CS4232 
590 static long __init masktoint(unsigned int i)
591 {
592         int t = -1;
593         while (i >> ++t)
594                 ;
595         return (t-1);
596 }
597
598 /*
599  * ppc_cs4232_dma and ppc_cs4232_dma2 are used in include/asm/dma.h
600  * to distinguish sound dma-channels from others. This is because 
601  * blocksize on 16 bit dma-channels 5,6,7 is 128k, but
602  * the cs4232.c uses 64k like on 8 bit dma-channels 0,1,2,3
603  */
604
605 static void __init prep_init_sound(void)
606 {
607         PPC_DEVICE *audiodevice = NULL;
608
609         /*
610          * Get the needed resource informations from residual data.
611          * 
612          */
613 #ifdef CONFIG_PREP_RESIDUAL
614         audiodevice = residual_find_device(~0, NULL, MultimediaController,
615                         AudioController, -1, 0);
616         if (audiodevice != NULL) {
617                 PnP_TAG_PACKET *pkt;
618
619                 pkt = PnP_find_packet((unsigned char *)&res->DevicePnPHeap[audiodevice->AllocatedOffset],
620                                 S5_Packet, 0);
621                 if (pkt != NULL)
622                         ppc_cs4232_dma = masktoint(pkt->S5_Pack.DMAMask);
623                 pkt = PnP_find_packet((unsigned char*)&res->DevicePnPHeap[audiodevice->AllocatedOffset],
624                                 S5_Packet, 1);
625                 if (pkt != NULL)
626                         ppc_cs4232_dma2 = masktoint(pkt->S5_Pack.DMAMask);
627         }
628 #endif
629
630         /*
631          * These are the PReP specs' defaults for the cs4231.  We use these
632          * as fallback incase we don't have residual data.
633          * At least the IBM Thinkpad 850 with IDE DMA Channels at 6 and 7 
634          * will use the other values.
635          */
636         if (audiodevice == NULL) {
637                 switch (_prep_type) {
638                 case _PREP_IBM:
639                         ppc_cs4232_dma = 1;
640                         ppc_cs4232_dma2 = -1;
641                         break;
642                 default: 
643                         ppc_cs4232_dma = 6;
644                         ppc_cs4232_dma2 = 7;
645                 }
646         }
647
648         /*
649          * Find a way to push these informations to the cs4232 driver
650          * Give it out with printk, when not in cmd_line?
651          * Append it to  cmd_line and saved_command_line?
652          * Format is cs4232=io,irq,dma,dma2
653          */
654 }
655 #endif /* CONFIG_SOUND_CS4232 */
656
657 /*
658  * Fill out screen_info according to the residual data. This allows us to use
659  * at least vesafb.
660  */
661 static void __init
662 prep_init_vesa(void)
663 {
664 #if defined(CONFIG_PREP_RESIDUAL) && \
665         (defined(CONFIG_FB_VGA16) || defined(CONFIG_FB_VGA_16_MODULE) || \
666          defined(CONFIG_FB_VESA))
667         PPC_DEVICE *vgadev;
668
669         vgadev = residual_find_device(~0, NULL, DisplayController, SVGAController,
670                                                                         -1, 0);
671         if (vgadev != NULL) {
672                 PnP_TAG_PACKET *pkt;
673
674                 pkt = PnP_find_large_vendor_packet(
675                                 (unsigned char *)&res->DevicePnPHeap[vgadev->AllocatedOffset],
676                                 0x04, 0); /* 0x04 = Display Tag */
677                 if (pkt != NULL) {
678                         unsigned char *ptr = (unsigned char *)pkt;
679
680                         if (ptr[4]) {
681                                 /* graphics mode */
682                                 screen_info.orig_video_isVGA = VIDEO_TYPE_VLFB;
683
684                                 screen_info.lfb_depth = ptr[4] * 8;
685
686                                 screen_info.lfb_width = swab16(*(short *)(ptr+6));
687                                 screen_info.lfb_height = swab16(*(short *)(ptr+8));
688                                 screen_info.lfb_linelength = swab16(*(short *)(ptr+10));
689
690                                 screen_info.lfb_base = swab32(*(long *)(ptr+12));
691                                 screen_info.lfb_size = swab32(*(long *)(ptr+20)) / 65536;
692                         }
693                 }
694         }
695 #endif /* CONFIG_PREP_RESIDUAL */
696 }
697
698 static void __init
699 prep_setup_arch(void)
700 {
701         unsigned char reg;
702         int is_ide=0;
703
704         /* init to some ~sane value until calibrate_delay() runs */
705         loops_per_jiffy = 50000000;
706         
707         /* Lookup PCI host bridges */
708         prep_find_bridges();
709         
710         /* Set up floppy in PS/2 mode */
711         outb(0x09, SIO_CONFIG_RA);
712         reg = inb(SIO_CONFIG_RD);
713         reg = (reg & 0x3F) | 0x40;
714         outb(reg, SIO_CONFIG_RD);
715         outb(reg, SIO_CONFIG_RD);       /* Have to write twice to change! */
716
717         switch ( _prep_type )
718         {
719         case _PREP_IBM:
720                 reg = inb(PREP_IBM_PLANAR);
721                 printk(KERN_INFO "IBM planar ID: %08x", reg);
722                 switch (reg) {
723                         case PREP_IBM_SANDALFOOT:
724                                 prep_gen_enable_l2();
725                                 setup_ibm_pci = prep_sandalfoot_setup_pci;
726                                 ppc_md.power_off = prep_sig750_poweroff;
727                                 ppc_md.show_cpuinfo = prep_sandalfoot_cpuinfo;
728                                 break;
729                         case PREP_IBM_THINKPAD:
730                                 prep_gen_enable_l2();
731                                 setup_ibm_pci = prep_thinkpad_setup_pci;
732                                 ppc_md.power_off = prep_carrera_poweroff;
733                                 ppc_md.show_cpuinfo = prep_thinkpad_cpuinfo;
734                                 break;
735                         default:
736                                 printk(" -- unknown! Assuming Carolina");
737                         case PREP_IBM_CAROLINA_IDE_0:
738                         case PREP_IBM_CAROLINA_IDE_1:
739                         case PREP_IBM_CAROLINA_IDE_2:
740                                 is_ide = 1;
741                         case PREP_IBM_CAROLINA_SCSI_0:
742                         case PREP_IBM_CAROLINA_SCSI_1:
743                         case PREP_IBM_CAROLINA_SCSI_2:
744                         case PREP_IBM_CAROLINA_SCSI_3:
745                                 prep_carolina_enable_l2();
746                                 setup_ibm_pci = prep_carolina_setup_pci;
747                                 ppc_md.power_off = prep_sig750_poweroff;
748                                 ppc_md.show_cpuinfo = prep_carolina_cpuinfo;
749                                 break;
750                         case PREP_IBM_TIGER1_133:
751                         case PREP_IBM_TIGER1_166:
752                         case PREP_IBM_TIGER1_180:
753                         case PREP_IBM_TIGER1_xxx:
754                         case PREP_IBM_TIGER1_333:
755                                 prep_carolina_enable_l2();
756                                 setup_ibm_pci = prep_tiger1_setup_pci;
757                                 ppc_md.power_off = prep_sig750_poweroff;
758                                 ppc_md.show_cpuinfo = prep_tiger1_cpuinfo;
759                                 break;
760                 }
761                 printk("\n");
762
763                 /* default root device */
764                 if (is_ide)
765                         ROOT_DEV = to_kdev_t(0x0303); /* hda3 */
766                 else
767                         ROOT_DEV = to_kdev_t(0x0803); /* sda3 */
768
769                 break;
770         case _PREP_Motorola:
771                 prep_gen_enable_l2();
772                 ppc_md.power_off = prep_halt;
773                 ppc_md.show_cpuinfo = prep_mot_cpuinfo;
774
775 #ifdef CONFIG_BLK_DEV_INITRD
776                 if (initrd_start)
777                         ROOT_DEV = MKDEV(RAMDISK_MAJOR, 0); /* /dev/ram */
778                 else
779 #endif
780 #ifdef CONFIG_ROOT_NFS
781                         ROOT_DEV = to_kdev_t(0x00ff); /* /dev/nfs */
782 #else
783                         ROOT_DEV = to_kdev_t(0x0802); /* /dev/sda2 */
784 #endif
785                 break;
786         }
787
788         /* Read in NVRAM data */ 
789         init_prep_nvram();
790
791         /* if no bootargs, look in NVRAM */
792         if ( cmd_line[0] == '\0' ) {
793                 char *bootargs;
794                  bootargs = prep_nvram_get_var("bootargs");
795                  if (bootargs != NULL) {
796                          strcpy(cmd_line, bootargs);
797                          /* again.. */
798                          strcpy(saved_command_line, cmd_line);
799                 }
800         }
801
802 #ifdef CONFIG_SOUND_CS4232 
803         prep_init_sound();
804 #endif /* CONFIG_SOUND_CS4232 */
805
806         prep_init_vesa();
807
808         switch (_prep_type) {
809         case _PREP_Motorola:
810                 raven_init();
811                 break;
812         case _PREP_IBM:
813                 ibm_prep_init();
814                 break;
815         }
816
817 #ifdef CONFIG_VGA_CONSOLE
818         /* vgacon.c needs to know where we mapped IO memory in io_block_mapping() */
819         vgacon_remap_base = 0xf0000000;
820         conswitchp = &vga_con;
821 #elif defined(CONFIG_DUMMY_CONSOLE)
822         conswitchp = &dummy_con;
823 #endif
824 }
825
826 /*
827  * Determine the decrementer frequency from the residual data
828  * This allows for a faster boot as we do not need to calibrate the
829  * decrementer against another clock. This is important for embedded systems.
830  */
831 static int __init
832 prep_res_calibrate_decr(void)
833 {
834 #ifdef CONFIG_PREP_RESIDUAL
835         unsigned long freq, divisor = 4;
836
837         if ( res->VitalProductData.ProcessorBusHz ) {
838                 freq = res->VitalProductData.ProcessorBusHz;
839                 printk("time_init: decrementer frequency = %lu.%.6lu MHz\n",
840                                 (freq/divisor)/1000000,
841                                 (freq/divisor)%1000000);
842                 tb_to_us = mulhwu_scale_factor(freq/divisor, 1000000);
843                 tb_ticks_per_jiffy = freq / HZ / divisor;
844                 return 0;
845         } else
846 #endif  
847                 return 1;
848 }
849
850 /*
851  * Uses the on-board timer to calibrate the on-chip decrementer register
852  * for prep systems.  On the pmac the OF tells us what the frequency is
853  * but on prep we have to figure it out.
854  * -- Cort
855  */
856 /* Done with 3 interrupts: the first one primes the cache and the
857  * 2 following ones measure the interval. The precision of the method
858  * is still doubtful due to the short interval sampled.
859  */
860 static volatile int calibrate_steps __initdata = 3;
861 static unsigned tbstamp __initdata = 0;
862
863 static void __init
864 prep_calibrate_decr_handler(int irq, void *dev, struct pt_regs *regs)
865 {
866         unsigned long t, freq;
867         int step=--calibrate_steps;
868
869         t = get_tbl();
870         if (step > 0) {
871                 tbstamp = t;
872         } else {
873                 freq = (t - tbstamp)*HZ;
874                 printk("time_init: decrementer frequency = %lu.%.6lu MHz\n",
875                          freq/1000000, freq%1000000);
876                 tb_ticks_per_jiffy = freq / HZ;
877                 tb_to_us = mulhwu_scale_factor(freq, 1000000);
878         }
879 }
880
881 static void __init
882 prep_calibrate_decr(void)
883 {
884         int res;
885
886         /* Try and get this from the residual data. */
887         res = prep_res_calibrate_decr();
888
889         /* If we didn't get it from the residual data, try this. */
890         if ( res ) {
891                 unsigned long flags;
892
893                 save_flags(flags);
894
895 #define TIMER0_COUNT 0x40
896 #define TIMER_CONTROL 0x43
897                 /* set timer to periodic mode */
898                 outb_p(0x34,TIMER_CONTROL);/* binary, mode 2, LSB/MSB, ch 0 */
899                 /* set the clock to ~100 Hz */
900                 outb_p(LATCH & 0xff , TIMER0_COUNT);    /* LSB */
901                 outb(LATCH >> 8 , TIMER0_COUNT);        /* MSB */
902
903                 if (request_irq(0, prep_calibrate_decr_handler, 0, "timer", NULL) != 0)
904                         panic("Could not allocate timer IRQ!");
905                 __sti();
906                 /* wait for calibrate */
907                 while ( calibrate_steps )
908                         ;
909                 restore_flags(flags);
910                 free_irq( 0, NULL);
911         }
912 }
913
914 static long __init
915 mk48t59_init(void) {
916         unsigned char tmp;
917
918         tmp = ppc_md.nvram_read_val(MK48T59_RTC_CONTROLB);
919         if (tmp & MK48T59_RTC_CB_STOP) {
920                 printk("Warning: RTC was stopped, date will be wrong.\n");
921                 ppc_md.nvram_write_val(MK48T59_RTC_CONTROLB, 
922                                          tmp & ~MK48T59_RTC_CB_STOP);
923                 /* Low frequency crystal oscillators may take a very long
924                  * time to startup and stabilize. For now just ignore the
925                  * the issue, but attempting to calibrate the decrementer
926                  * from the RTC just after this wakeup is likely to be very 
927                  * inaccurate. Firmware should not allow to load
928                  * the OS with the clock stopped anyway...
929                  */
930         }
931         /* Ensure that the clock registers are updated */
932         tmp = ppc_md.nvram_read_val(MK48T59_RTC_CONTROLA);
933         tmp &= ~(MK48T59_RTC_CA_READ | MK48T59_RTC_CA_WRITE);
934         ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA, tmp);
935         return 0;
936 }
937
938 /* We use the NVRAM RTC to time a second to calibrate the decrementer,
939  * the RTC registers have just been set up in the right state by the
940  * preceding routine.
941  */
942 static void __init
943 mk48t59_calibrate_decr(void)
944 {
945         unsigned long freq;
946         unsigned long t1;
947         unsigned char save_control;
948         long i;
949         unsigned char sec;
950  
951                 
952         /* Make sure the time is not stopped. */
953         save_control = ppc_md.nvram_read_val(MK48T59_RTC_CONTROLB);
954         
955         ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA,
956                         (save_control & (~MK48T59_RTC_CB_STOP)));
957
958         /* Now make sure the read bit is off so the value will change. */
959         save_control = ppc_md.nvram_read_val(MK48T59_RTC_CONTROLA);
960         save_control &= ~MK48T59_RTC_CA_READ;
961         ppc_md.nvram_write_val(MK48T59_RTC_CONTROLA, save_control);
962
963
964         /* Read the seconds value to see when it changes. */
965         sec = ppc_md.nvram_read_val(MK48T59_RTC_SECONDS);
966         /* Actually this is bad for precision, we should have a loop in
967          * which we only read the seconds counter. nvram_read_val writes
968          * the address bytes on every call and this takes a lot of time.
969          * Perhaps an nvram_wait_change method returning a time
970          * stamp with a loop count as parameter would be the  solution.
971          */
972         for (i = 0 ; i < 1000000 ; i++) { /* may take up to 1 second... */
973                 t1 = get_tbl();
974                 if (ppc_md.nvram_read_val(MK48T59_RTC_SECONDS) != sec) {
975                         break;
976                 }
977         }
978
979         sec = ppc_md.nvram_read_val(MK48T59_RTC_SECONDS);
980         for (i = 0 ; i < 1000000 ; i++) { /* Should take up 1 second... */
981                 freq = get_tbl()-t1;
982                 if (ppc_md.nvram_read_val(MK48T59_RTC_SECONDS) != sec)
983                         break;
984         }
985
986         printk("time_init: decrementer frequency = %lu.%.6lu MHz\n",
987                  freq/1000000, freq%1000000);
988         tb_ticks_per_jiffy = freq / HZ;
989         tb_to_us = mulhwu_scale_factor(freq, 1000000);
990 }
991
992 static unsigned int __prep
993 prep_irq_cannonicalize(u_int irq)
994 {
995         if (irq == 2)
996         {
997                 return 9;
998         }
999         else
1000         {
1001                 return irq;
1002         }
1003 }
1004
1005 static void __init
1006 prep_init_IRQ(void)
1007 {
1008         int i;
1009         unsigned int pci_viddid, pci_did;
1010
1011         if (OpenPIC_Addr != NULL) {
1012                 openpic_init(NUM_8259_INTERRUPTS);
1013                 /* We have a cascade on OpenPIC IRQ 0, Linux IRQ 16 */
1014                 openpic_hookup_cascade(NUM_8259_INTERRUPTS, "82c59 cascade",
1015                                        i8259_irq);
1016         }
1017         for (i = 0; i < NUM_8259_INTERRUPTS; i++)
1018                 irq_desc[i].handler = &i8259_pic;
1019          /* If we have a Raven PCI bridge or a Hawk PCI bridge / Memory
1020           * controller, we poll (as they have a different int-ack address). */
1021         early_read_config_dword(0, 0, 0, PCI_VENDOR_ID, &pci_viddid);
1022         pci_did = (pci_viddid & 0xffff0000) >> 16;
1023         if (((pci_viddid & 0xffff) == PCI_VENDOR_ID_MOTOROLA)
1024                         && ((pci_did == PCI_DEVICE_ID_MOTOROLA_RAVEN)
1025                                 || (pci_did == PCI_DEVICE_ID_MOTOROLA_HAWK)))
1026                 i8259_init(0);
1027         else
1028                 /* PCI interrupt ack address given in section 6.1.8 of the
1029                  * PReP specification. */
1030                 i8259_init(0xbffffff0);
1031 }
1032
1033 #if defined(CONFIG_BLK_DEV_IDE) || defined(CONFIG_BLK_DEV_IDE_MODULE)
1034 /*
1035  * IDE stuff.
1036  */
1037 static int __prep
1038 prep_ide_default_irq(ide_ioreg_t base)
1039 {
1040         switch (base) {
1041                 case 0x1f0: return 13;
1042                 case 0x170: return 13;
1043                 case 0x1e8: return 11;
1044                 case 0x168: return 10;
1045                 case 0xfff0: return 14;         /* MCP(N)750 ide0 */
1046                 case 0xffe0: return 15;         /* MCP(N)750 ide1 */
1047                 default: return 0;
1048         }
1049 }
1050
1051 static ide_ioreg_t __prep
1052 prep_ide_default_io_base(int index)
1053 {
1054         switch (index) {
1055                 case 0: return 0x1f0;
1056                 case 1: return 0x170;
1057                 case 2: return 0x1e8;
1058                 case 3: return 0x168;
1059                 default:
1060                         return 0;
1061         }
1062 }
1063
1064 static void __init
1065 prep_ide_init_hwif_ports (hw_regs_t *hw, ide_ioreg_t data_port, ide_ioreg_t ctrl_port, int *irq)
1066 {
1067         ide_ioreg_t reg = data_port;
1068         int i;
1069
1070         for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
1071                 hw->io_ports[i] = reg;
1072                 reg += 1;
1073         }
1074         if (ctrl_port) {
1075                 hw->io_ports[IDE_CONTROL_OFFSET] = ctrl_port;
1076         } else {
1077                 hw->io_ports[IDE_CONTROL_OFFSET] = hw->io_ports[IDE_DATA_OFFSET] + 0x206;
1078         }
1079         if (irq != NULL)
1080                 *irq = 0;
1081 }
1082 #endif
1083
1084 #ifdef CONFIG_SMP
1085 /* PReP (MTX) support */
1086 static int __init
1087 smp_prep_probe(void)
1088 {
1089         extern int mot_multi;
1090
1091         if (mot_multi) {
1092                 openpic_request_IPIs();
1093                 smp_hw_index[1] = 1;
1094                 return 2;
1095         }
1096
1097         return 1;
1098 }
1099
1100 static void __init
1101 smp_prep_kick_cpu(int nr)
1102 {
1103         *(unsigned long *)KERNELBASE = nr;
1104         asm volatile("dcbf 0,%0"::"r"(KERNELBASE):"memory");
1105         printk("CPU1 released, waiting\n");
1106 }
1107
1108 static void __init
1109 smp_prep_setup_cpu(int cpu_nr)
1110 {
1111         if (OpenPIC_Addr)
1112                 do_openpic_setup_cpu();
1113 }
1114
1115 static struct smp_ops_t prep_smp_ops __prepdata = {
1116         smp_openpic_message_pass,
1117         smp_prep_probe,
1118         smp_prep_kick_cpu,
1119         smp_prep_setup_cpu,
1120 };
1121 #endif /* CONFIG_SMP */
1122
1123 /*
1124  * What ever boots us must pass in the ammount of memory.
1125  */
1126 static unsigned long __init
1127 prep_find_end_of_memory(void)
1128 {
1129         return boot_mem_size;
1130 }
1131
1132 /*
1133  * Setup the bat mappings we're going to load that cover
1134  * the io areas.  RAM was mapped by mapin_ram().
1135  * -- Cort
1136  */
1137 static void __init
1138 prep_map_io(void)
1139 {
1140         io_block_mapping(0x80000000, PREP_ISA_IO_BASE, 0x10000000, _PAGE_IO);
1141         io_block_mapping(0xf0000000, PREP_ISA_MEM_BASE, 0x08000000, _PAGE_IO);
1142 }
1143
1144 static void __init
1145 prep_init2(void)
1146 {
1147 #ifdef CONFIG_NVRAM
1148         request_region(PREP_NVRAM_AS0, 0x8, "nvram");
1149 #endif
1150         request_region(0x00,0x20,"dma1");
1151         request_region(0x40,0x20,"timer");
1152         request_region(0x80,0x10,"dma page reg");
1153         request_region(0xc0,0x20,"dma2");
1154 }
1155
1156 void __init
1157 prep_init(unsigned long r3, unsigned long r4, unsigned long r5,
1158                 unsigned long r6, unsigned long r7)
1159 {
1160 #ifdef CONFIG_PREP_RESIDUAL     
1161         /* make a copy of residual data */
1162         if ( r3 ) {
1163                 memcpy((void *)res,(void *)(r3+KERNELBASE),
1164                          sizeof(RESIDUAL));
1165         }
1166 #endif
1167
1168         isa_io_base = PREP_ISA_IO_BASE;
1169         isa_mem_base = PREP_ISA_MEM_BASE;
1170         pci_dram_offset = PREP_PCI_DRAM_OFFSET;
1171         ISA_DMA_THRESHOLD = 0x00ffffff;
1172         DMA_MODE_READ = 0x44;
1173         DMA_MODE_WRITE = 0x48;
1174
1175         /* figure out what kind of prep workstation we are */
1176 #ifdef CONFIG_PREP_RESIDUAL     
1177         if ( res->ResidualLength != 0 ) {
1178                 if ( !strncmp(res->VitalProductData.PrintableModel,"IBM",3) )
1179                         _prep_type = _PREP_IBM;
1180                 else
1181                         _prep_type = _PREP_Motorola;
1182         } else /* assume motorola if no residual (netboot?) */
1183 #endif
1184         {
1185                 _prep_type = _PREP_Motorola;
1186         }
1187
1188         ppc_md.setup_arch     = prep_setup_arch;
1189         ppc_md.show_percpuinfo = prep_show_percpuinfo;
1190         ppc_md.show_cpuinfo   = NULL; /* set in prep_setup_arch() */
1191         ppc_md.irq_cannonicalize = prep_irq_cannonicalize;
1192         ppc_md.init_IRQ       = prep_init_IRQ;
1193         /* this gets changed later on if we have an OpenPIC -- Cort */
1194         ppc_md.get_irq        = i8259_irq;
1195         ppc_md.init           = prep_init2;
1196
1197         ppc_md.restart        = prep_restart;
1198         ppc_md.power_off      = NULL; /* set in prep_setup_arch() */
1199         ppc_md.halt           = prep_halt;
1200
1201         ppc_md.nvram_read_val = prep_nvram_read_val;
1202         ppc_md.nvram_write_val = prep_nvram_write_val;
1203
1204         ppc_md.time_init      = NULL;
1205         if (_prep_type == _PREP_IBM) {
1206                 ppc_md.set_rtc_time   = mc146818_set_rtc_time;
1207                 ppc_md.get_rtc_time   = mc146818_get_rtc_time;
1208                 ppc_md.calibrate_decr = prep_calibrate_decr;
1209         } else {
1210                 ppc_md.set_rtc_time   = mk48t59_set_rtc_time;
1211                 ppc_md.get_rtc_time   = mk48t59_get_rtc_time;
1212                 ppc_md.calibrate_decr = mk48t59_calibrate_decr;
1213                 ppc_md.time_init      = mk48t59_init;
1214         }
1215
1216         ppc_md.find_end_of_memory = prep_find_end_of_memory;
1217         ppc_md.setup_io_mappings = prep_map_io;
1218
1219 #if defined(CONFIG_BLK_DEV_IDE) || defined(CONFIG_BLK_DEV_IDE_MODULE)
1220         ppc_ide_md.default_irq = prep_ide_default_irq;
1221         ppc_ide_md.default_io_base = prep_ide_default_io_base;
1222         ppc_ide_md.ide_init_hwif = prep_ide_init_hwif_ports;
1223 #endif
1224
1225 #ifdef CONFIG_VT
1226         ppc_md.kbd_setkeycode    = pckbd_setkeycode;
1227         ppc_md.kbd_getkeycode    = pckbd_getkeycode;
1228         ppc_md.kbd_translate     = pckbd_translate;
1229         ppc_md.kbd_unexpected_up = pckbd_unexpected_up;
1230         ppc_md.kbd_leds          = pckbd_leds;
1231         ppc_md.kbd_init_hw       = pckbd_init_hw;
1232 #ifdef CONFIG_MAGIC_SYSRQ
1233         ppc_md.ppc_kbd_sysrq_xlate       = pckbd_sysrq_xlate;
1234         SYSRQ_KEY = 0x54;
1235 #endif
1236 #endif
1237
1238 #ifdef CONFIG_SMP
1239         ppc_md.smp_ops           = &prep_smp_ops;
1240 #endif /* CONFIG_SMP */
1241 }