ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc / platforms / chrp_setup.c
1 /*
2  *  arch/ppc/platforms/setup.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  *  Adapted from 'alpha' version by Gary Thomas
6  *  Modified by Cort Dougan (cort@cs.nmt.edu)
7  */
8
9 /*
10  * bootup setup stuff..
11  */
12
13 #include <linux/config.h>
14 #include <linux/errno.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/stddef.h>
19 #include <linux/unistd.h>
20 #include <linux/ptrace.h>
21 #include <linux/slab.h>
22 #include <linux/user.h>
23 #include <linux/a.out.h>
24 #include <linux/tty.h>
25 #include <linux/major.h>
26 #include <linux/interrupt.h>
27 #include <linux/reboot.h>
28 #include <linux/init.h>
29 #include <linux/pci.h>
30 #include <linux/version.h>
31 #include <linux/adb.h>
32 #include <linux/module.h>
33 #include <linux/delay.h>
34 #include <linux/ide.h>
35 #include <linux/irq.h>
36 #include <linux/console.h>
37 #include <linux/seq_file.h>
38 #include <linux/root_dev.h>
39 #include <linux/initrd.h>
40
41 #include <asm/io.h>
42 #include <asm/pgtable.h>
43 #include <asm/prom.h>
44 #include <asm/gg2.h>
45 #include <asm/pci-bridge.h>
46 #include <asm/dma.h>
47 #include <asm/machdep.h>
48 #include <asm/irq.h>
49 #include <asm/hydra.h>
50 #include <asm/sections.h>
51 #include <asm/time.h>
52 #include <asm/btext.h>
53 #include <asm/i8259.h>
54 #include <asm/open_pic.h>
55 #include <asm/xmon.h>
56
57 unsigned long chrp_get_rtc_time(void);
58 int chrp_set_rtc_time(unsigned long nowtime);
59 void chrp_calibrate_decr(void);
60 long chrp_time_init(void);
61
62 void chrp_find_bridges(void);
63 void chrp_event_scan(void);
64 void rtas_display_progress(char *, unsigned short);
65 void rtas_indicator_progress(char *, unsigned short);
66 void btext_progress(char *, unsigned short);
67
68 extern unsigned long pmac_find_end_of_memory(void);
69 extern int of_show_percpuinfo(struct seq_file *, int);
70
71 /*
72  * XXX this should be in xmon.h, but putting it there means xmon.h
73  * has to include <linux/interrupt.h> (to get irqreturn_t), which
74  * causes all sorts of problems.  -- paulus
75  */
76 extern irqreturn_t xmon_irq(int, void *, struct pt_regs *);
77
78 extern dev_t boot_dev;
79
80 extern PTE *Hash, *Hash_end;
81 extern unsigned long Hash_size, Hash_mask;
82 extern int probingmem;
83 extern unsigned long loops_per_jiffy;
84 static int max_width;
85
86 #ifdef CONFIG_SMP
87 extern struct smp_ops_t chrp_smp_ops;
88 #endif
89
90 static const char *gg2_memtypes[4] = {
91         "FPM", "SDRAM", "EDO", "BEDO"
92 };
93 static const char *gg2_cachesizes[4] = {
94         "256 KB", "512 KB", "1 MB", "Reserved"
95 };
96 static const char *gg2_cachetypes[4] = {
97         "Asynchronous", "Reserved", "Flow-Through Synchronous",
98         "Pipelined Synchronous"
99 };
100 static const char *gg2_cachemodes[4] = {
101         "Disabled", "Write-Through", "Copy-Back", "Transparent Mode"
102 };
103
104 int __chrp
105 chrp_show_cpuinfo(struct seq_file *m)
106 {
107         int i, sdramen;
108         unsigned int t;
109         struct device_node *root;
110         const char *model = "";
111
112         root = find_path_device("/");
113         if (root)
114                 model = get_property(root, "model", NULL);
115         seq_printf(m, "machine\t\t: CHRP %s\n", model);
116
117         /* longtrail (goldengate) stuff */
118         if (!strncmp(model, "IBM,LongTrail", 13)) {
119                 /* VLSI VAS96011/12 `Golden Gate 2' */
120                 /* Memory banks */
121                 sdramen = (in_le32((unsigned *)(gg2_pci_config_base+
122                                                 GG2_PCI_DRAM_CTRL))
123                            >>31) & 1;
124                 for (i = 0; i < (sdramen ? 4 : 6); i++) {
125                         t = in_le32((unsigned *)(gg2_pci_config_base+
126                                                  GG2_PCI_DRAM_BANK0+
127                                                  i*4));
128                         if (!(t & 1))
129                                 continue;
130                         switch ((t>>8) & 0x1f) {
131                         case 0x1f:
132                                 model = "4 MB";
133                                 break;
134                         case 0x1e:
135                                 model = "8 MB";
136                                 break;
137                         case 0x1c:
138                                 model = "16 MB";
139                                 break;
140                         case 0x18:
141                                 model = "32 MB";
142                                 break;
143                         case 0x10:
144                                 model = "64 MB";
145                                 break;
146                         case 0x00:
147                                 model = "128 MB";
148                                 break;
149                         default:
150                                 model = "Reserved";
151                                 break;
152                         }
153                         seq_printf(m, "memory bank %d\t: %s %s\n", i, model,
154                                    gg2_memtypes[sdramen ? 1 : ((t>>1) & 3)]);
155                 }
156                 /* L2 cache */
157                 t = in_le32((unsigned *)(gg2_pci_config_base+GG2_PCI_CC_CTRL));
158                 seq_printf(m, "board l2\t: %s %s (%s)\n",
159                            gg2_cachesizes[(t>>7) & 3],
160                            gg2_cachetypes[(t>>2) & 3],
161                            gg2_cachemodes[t & 3]);
162         }
163         return 0;
164 }
165
166 /*
167  *  Fixes for the National Semiconductor PC78308VUL SuperI/O
168  *
169  *  Some versions of Open Firmware incorrectly initialize the IRQ settings
170  *  for keyboard and mouse
171  */
172 static inline void __init sio_write(u8 val, u8 index)
173 {
174         outb(index, 0x15c);
175         outb(val, 0x15d);
176 }
177
178 static inline u8 __init sio_read(u8 index)
179 {
180         outb(index, 0x15c);
181         return inb(0x15d);
182 }
183
184 static void __init sio_fixup_irq(const char *name, u8 device, u8 level,
185                                      u8 type)
186 {
187         u8 level0, type0, active;
188
189         /* select logical device */
190         sio_write(device, 0x07);
191         active = sio_read(0x30);
192         level0 = sio_read(0x70);
193         type0 = sio_read(0x71);
194         if (level0 != level || type0 != type || !active) {
195                 printk(KERN_WARNING "sio: %s irq level %d, type %d, %sactive: "
196                        "remapping to level %d, type %d, active\n",
197                        name, level0, type0, !active ? "in" : "", level, type);
198                 sio_write(0x01, 0x30);
199                 sio_write(level, 0x70);
200                 sio_write(type, 0x71);
201         }
202 }
203
204 static void __init sio_init(void)
205 {
206         struct device_node *root;
207
208         if ((root = find_path_device("/")) &&
209             !strncmp(get_property(root, "model", NULL), "IBM,LongTrail", 13)) {
210                 /* logical device 0 (KBC/Keyboard) */
211                 sio_fixup_irq("keyboard", 0, 1, 2);
212                 /* select logical device 1 (KBC/Mouse) */
213                 sio_fixup_irq("mouse", 1, 12, 2);
214         }
215 }
216
217
218 void __init
219 chrp_setup_arch(void)
220 {
221         struct device_node *device;
222
223         /* init to some ~sane value until calibrate_delay() runs */
224         loops_per_jiffy = 50000000/HZ;
225
226 #ifdef CONFIG_BLK_DEV_INITRD
227         /* this is fine for chrp */
228         initrd_below_start_ok = 1;
229
230         if (initrd_start)
231                 ROOT_DEV = Root_RAM0;
232         else
233 #endif
234                 ROOT_DEV = Root_SDA2; /* sda2 (sda1 is for the kernel) */
235
236         /* Lookup PCI host bridges */
237         chrp_find_bridges();
238
239 #ifndef CONFIG_PPC64BRIDGE
240         /*
241          *  Temporary fixes for PCI devices.
242          *  -- Geert
243          */
244         hydra_init();           /* Mac I/O */
245
246 #endif /* CONFIG_PPC64BRIDGE */
247
248         /*
249          *  Fix the Super I/O configuration
250          */
251         sio_init();
252
253         /*
254          *  Setup the console operations
255          */
256 #ifdef CONFIG_DUMMY_CONSOLE
257         conswitchp = &dummy_con;
258 #endif
259
260         /* Get the event scan rate for the rtas so we know how
261          * often it expects a heartbeat. -- Cort
262          */
263         if ( rtas_data ) {
264                 struct property *p;
265                 device = find_devices("rtas");
266                 for ( p = device->properties;
267                       p && strncmp(p->name, "rtas-event-scan-rate", 20);
268                       p = p->next )
269                         /* nothing */ ;
270                 if ( p && *(unsigned long *)p->value ) {
271                         ppc_md.heartbeat = chrp_event_scan;
272                         ppc_md.heartbeat_reset = (HZ/(*(unsigned long *)p->value)*30)-1;
273                         ppc_md.heartbeat_count = 1;
274                         printk("RTAS Event Scan Rate: %lu (%lu jiffies)\n",
275                                *(unsigned long *)p->value, ppc_md.heartbeat_reset );
276                 }
277         }
278
279         pci_create_OF_bus_map();
280 }
281
282 void __chrp
283 chrp_event_scan(void)
284 {
285         unsigned char log[1024];
286         unsigned long ret = 0;
287         /* XXX: we should loop until the hardware says no more error logs -- Cort */
288         call_rtas( "event-scan", 4, 1, &ret, 0xffffffff, 0,
289                    __pa(log), 1024 );
290         ppc_md.heartbeat_count = ppc_md.heartbeat_reset;
291 }
292
293 void __chrp
294 chrp_restart(char *cmd)
295 {
296         printk("RTAS system-reboot returned %d\n",
297                call_rtas("system-reboot", 0, 1, NULL));
298         for (;;);
299 }
300
301 void __chrp
302 chrp_power_off(void)
303 {
304         /* allow power on only with power button press */
305         printk("RTAS power-off returned %d\n",
306                call_rtas("power-off", 2, 1, NULL,0xffffffff,0xffffffff));
307         for (;;);
308 }
309
310 void __chrp
311 chrp_halt(void)
312 {
313         chrp_power_off();
314 }
315
316 u_int __chrp
317 chrp_irq_canonicalize(u_int irq)
318 {
319         if (irq == 2)
320                 return 9;
321         return irq;
322 }
323
324 /*
325  * Finds the open-pic node and sets OpenPIC_Addr based on its reg property.
326  * Then checks if it has an interrupt-ranges property.  If it does then
327  * we have a distributed open-pic, so call openpic_set_sources to tell
328  * the openpic code where to find the interrupt source registers.
329  */
330 static void __init chrp_find_openpic(void)
331 {
332         struct device_node *np;
333         int len, i;
334         unsigned int *iranges;
335         void *isu;
336
337         np = find_type_devices("open-pic");
338         if (np == NULL || np->n_addrs == 0)
339                 return;
340         printk(KERN_INFO "OpenPIC at %x (size %x)\n",
341                np->addrs[0].address, np->addrs[0].size);
342         OpenPIC_Addr = ioremap(np->addrs[0].address, 0x40000);
343         if (OpenPIC_Addr == NULL) {
344                 printk(KERN_ERR "Failed to map OpenPIC!\n");
345                 return;
346         }
347
348         iranges = (unsigned int *) get_property(np, "interrupt-ranges", &len);
349         if (iranges == NULL || len < 2 * sizeof(unsigned int))
350                 return;         /* not distributed */
351
352         /*
353          * The first pair of cells in interrupt-ranges refers to the
354          * IDU; subsequent pairs refer to the ISUs.
355          */
356         len /= 2 * sizeof(unsigned int);
357         if (np->n_addrs < len) {
358                 printk(KERN_ERR "Insufficient addresses for distributed"
359                        " OpenPIC (%d < %d)\n", np->n_addrs, len);
360                 return;
361         }
362         if (iranges[1] != 0) {
363                 printk(KERN_INFO "OpenPIC irqs %d..%d in IDU\n",
364                        iranges[0], iranges[0] + iranges[1] - 1);
365                 openpic_set_sources(iranges[0], iranges[1], NULL);
366         }
367         for (i = 1; i < len; ++i) {
368                 iranges += 2;
369                 printk(KERN_INFO "OpenPIC irqs %d..%d in ISU at %x (%x)\n",
370                        iranges[0], iranges[0] + iranges[1] - 1,
371                        np->addrs[i].address, np->addrs[i].size);
372                 isu = ioremap(np->addrs[i].address, np->addrs[i].size);
373                 if (isu != NULL)
374                         openpic_set_sources(iranges[0], iranges[1], isu);
375                 else
376                         printk(KERN_ERR "Failed to map OpenPIC ISU at %x!\n",
377                                np->addrs[i].address);
378         }
379 }
380
381 void __init chrp_init_IRQ(void)
382 {
383         struct device_node *np;
384         int i;
385         unsigned long chrp_int_ack;
386         unsigned char init_senses[NR_IRQS - NUM_8259_INTERRUPTS];
387 #if defined(CONFIG_VT) && defined(CONFIG_INPUT_ADBHID) && defined(XMON)
388         struct device_node *kbd;
389 #endif
390
391         for (np = find_devices("pci"); np != NULL; np = np->next) {
392                 unsigned int *addrp = (unsigned int *)
393                         get_property(np, "8259-interrupt-acknowledge", NULL);
394
395                 if (addrp == NULL)
396                         continue;
397                 chrp_int_ack = addrp[prom_n_addr_cells(np)-1];
398                 break;
399         }
400         if (np == NULL)
401                 printk(KERN_ERR "Cannot find PCI interrupt acknowledge address\n");
402
403         chrp_find_openpic();
404
405         prom_get_irq_senses(init_senses, NUM_8259_INTERRUPTS, NR_IRQS);
406         OpenPIC_InitSenses = init_senses;
407         OpenPIC_NumInitSenses = NR_IRQS - NUM_8259_INTERRUPTS;
408
409         openpic_init(NUM_8259_INTERRUPTS);
410         /* We have a cascade on OpenPIC IRQ 0, Linux IRQ 16 */
411         openpic_hookup_cascade(NUM_8259_INTERRUPTS, "82c59 cascade",
412                                i8259_irq);
413
414         for (i = 0; i < NUM_8259_INTERRUPTS; i++)
415                 irq_desc[i].handler = &i8259_pic;
416         i8259_init(chrp_int_ack);
417
418 #if defined(CONFIG_VT) && defined(CONFIG_INPUT_ADBHID) && defined(XMON)
419         /* see if there is a keyboard in the device tree
420            with a parent of type "adb" */
421         for (kbd = find_devices("keyboard"); kbd; kbd = kbd->next)
422                 if (kbd->parent && kbd->parent->type
423                     && strcmp(kbd->parent->type, "adb") == 0)
424                         break;
425         if (kbd)
426                 request_irq(HYDRA_INT_ADB_NMI, xmon_irq, 0, "XMON break", 0);
427 #endif
428 }
429
430 void __init
431 chrp_init2(void)
432 {
433 #ifdef CONFIG_NVRAM
434 // XX replace this in a more saner way
435 //      pmac_nvram_init();
436 #endif
437
438         request_region(0x20,0x20,"pic1");
439         request_region(0xa0,0x20,"pic2");
440         request_region(0x00,0x20,"dma1");
441         request_region(0x40,0x20,"timer");
442         request_region(0x80,0x10,"dma page reg");
443         request_region(0xc0,0x20,"dma2");
444
445         if (ppc_md.progress)
446                 ppc_md.progress("  Have fun!    ", 0x7777);
447 }
448
449 void __init
450 chrp_init(unsigned long r3, unsigned long r4, unsigned long r5,
451           unsigned long r6, unsigned long r7)
452 {
453 #ifdef CONFIG_BLK_DEV_INITRD
454         /* take care of initrd if we have one */
455         if ( r6 )
456         {
457                 initrd_start = r6 + KERNELBASE;
458                 initrd_end = r6 + r7 + KERNELBASE;
459         }
460 #endif /* CONFIG_BLK_DEV_INITRD */
461
462         ISA_DMA_THRESHOLD = ~0L;
463         DMA_MODE_READ = 0x44;
464         DMA_MODE_WRITE = 0x48;
465         isa_io_base = CHRP_ISA_IO_BASE;         /* default value */
466
467         ppc_md.setup_arch     = chrp_setup_arch;
468         ppc_md.show_percpuinfo = of_show_percpuinfo;
469         ppc_md.show_cpuinfo   = chrp_show_cpuinfo;
470         ppc_md.irq_canonicalize = chrp_irq_canonicalize;
471         ppc_md.init_IRQ       = chrp_init_IRQ;
472         ppc_md.get_irq        = openpic_get_irq;
473
474         ppc_md.init           = chrp_init2;
475
476         ppc_md.restart        = chrp_restart;
477         ppc_md.power_off      = chrp_power_off;
478         ppc_md.halt           = chrp_halt;
479
480         ppc_md.time_init      = chrp_time_init;
481         ppc_md.set_rtc_time   = chrp_set_rtc_time;
482         ppc_md.get_rtc_time   = chrp_get_rtc_time;
483         ppc_md.calibrate_decr = chrp_calibrate_decr;
484
485         ppc_md.find_end_of_memory = pmac_find_end_of_memory;
486
487         if (rtas_data) {
488                 struct device_node *rtas;
489                 unsigned int *p;
490
491                 rtas = find_devices("rtas");
492                 if (rtas != NULL) {
493                         if (get_property(rtas, "display-character", NULL)) {
494                                 ppc_md.progress = rtas_display_progress;
495                                 p = (unsigned int *) get_property
496                                        (rtas, "ibm,display-line-length", NULL);
497                                 if (p)
498                                         max_width = *p;
499                         } else if (get_property(rtas, "set-indicator", NULL))
500                                 ppc_md.progress = rtas_indicator_progress;
501                 }
502         }
503 #ifdef CONFIG_BOOTX_TEXT
504         if (ppc_md.progress == NULL && boot_text_mapped)
505                 ppc_md.progress = btext_progress;
506 #endif
507
508 #ifdef CONFIG_SMP
509         ppc_md.smp_ops = &chrp_smp_ops;
510 #endif /* CONFIG_SMP */
511
512         /*
513          * Print the banner, then scroll down so boot progress
514          * can be printed.  -- Cort
515          */
516         if (ppc_md.progress) ppc_md.progress("Linux/PPC "UTS_RELEASE"\n", 0x0);
517 }
518
519 void __chrp
520 rtas_display_progress(char *s, unsigned short hex)
521 {
522         int width;
523         char *os = s;
524
525         if ( call_rtas( "display-character", 1, 1, NULL, '\r' ) )
526                 return;
527
528         width = max_width;
529         while ( *os )
530         {
531                 if ( (*os == '\n') || (*os == '\r') )
532                         width = max_width;
533                 else
534                         width--;
535                 call_rtas( "display-character", 1, 1, NULL, *os++ );
536                 /* if we overwrite the screen length */
537                 if ( width == 0 )
538                         while ( (*os != 0) && (*os != '\n') && (*os != '\r') )
539                                 os++;
540         }
541
542         /*while ( width-- > 0 )*/
543         call_rtas( "display-character", 1, 1, NULL, ' ' );
544 }
545
546 void __chrp
547 rtas_indicator_progress(char *s, unsigned short hex)
548 {
549         call_rtas("set-indicator", 3, 1, NULL, 6, 0, hex);
550 }
551
552 #ifdef CONFIG_BOOTX_TEXT
553 void
554 btext_progress(char *s, unsigned short hex)
555 {
556         prom_print(s);
557         prom_print("\n");
558 }
559 #endif /* CONFIG_BOOTX_TEXT */