ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc64 / kernel / pmac_setup.c
1 /*
2  *  arch/ppc/platforms/setup.c
3  *
4  *  PowerPC version
5  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6  *
7  *  Adapted for Power Macintosh by Paul Mackerras
8  *    Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
9  *
10  *  Derived from "arch/alpha/kernel/setup.c"
11  *    Copyright (C) 1995 Linus Torvalds
12  *
13  *  Maintained by Benjamin Herrenschmidt (benh@kernel.crashing.org)
14  *
15  *  This program is free software; you can redistribute it and/or
16  *  modify it under the terms of the GNU General Public License
17  *  as published by the Free Software Foundation; either version
18  *  2 of the License, or (at your option) any later version.
19  *
20  */
21
22 /*
23  * bootup setup stuff..
24  */
25
26 #include <linux/config.h>
27 #include <linux/init.h>
28 #include <linux/errno.h>
29 #include <linux/sched.h>
30 #include <linux/kernel.h>
31 #include <linux/mm.h>
32 #include <linux/stddef.h>
33 #include <linux/unistd.h>
34 #include <linux/ptrace.h>
35 #include <linux/slab.h>
36 #include <linux/user.h>
37 #include <linux/a.out.h>
38 #include <linux/tty.h>
39 #include <linux/string.h>
40 #include <linux/delay.h>
41 #include <linux/ioport.h>
42 #include <linux/major.h>
43 #include <linux/initrd.h>
44 #include <linux/vt_kern.h>
45 #include <linux/console.h>
46 #include <linux/ide.h>
47 #include <linux/pci.h>
48 #include <linux/adb.h>
49 #include <linux/cuda.h>
50 #include <linux/pmu.h>
51 #include <linux/irq.h>
52 #include <linux/seq_file.h>
53 #include <linux/root_dev.h>
54
55 #include <asm/processor.h>
56 #include <asm/sections.h>
57 #include <asm/prom.h>
58 #include <asm/system.h>
59 #include <asm/pgtable.h>
60 #include <asm/bitops.h>
61 #include <asm/io.h>
62 #include <asm/pci-bridge.h>
63 #include <asm/iommu.h>
64 #include <asm/machdep.h>
65 #include <asm/dma.h>
66 #include <asm/bootx.h>
67 #include <asm/btext.h>
68 #include <asm/cputable.h>
69 #include <asm/pmac_feature.h>
70 #include <asm/time.h>
71 #include <asm/of_device.h>
72 #include <asm/lmb.h>
73
74 #include "pmac.h"
75
76 static int current_root_goodness = -1;
77 #define DEFAULT_ROOT_DEVICE Root_SDA1   /* sda1 - slightly silly choice */
78
79 extern  int powersave_nap;
80 int sccdbg;
81
82 extern void udbg_init_scc(struct device_node *np);
83
84 #ifdef CONFIG_BOOTX_TEXT
85 void pmac_progress(char *s, unsigned short hex);
86 #endif
87
88 void __pmac pmac_show_cpuinfo(struct seq_file *m)
89 {
90         struct device_node *np;
91         char *pp;
92         int plen;
93         char* mbname;
94         int mbmodel = pmac_call_feature(PMAC_FTR_GET_MB_INFO, NULL,
95                                         PMAC_MB_INFO_MODEL, 0);
96         unsigned int mbflags = pmac_call_feature(PMAC_FTR_GET_MB_INFO, NULL,
97                                                  PMAC_MB_INFO_FLAGS, 0);
98
99         if (pmac_call_feature(PMAC_FTR_GET_MB_INFO, NULL, PMAC_MB_INFO_NAME,
100                               (long)&mbname) != 0)
101                 mbname = "Unknown";
102         
103         /* find motherboard type */
104         seq_printf(m, "machine\t\t: ");
105         np = find_devices("device-tree");
106         if (np != NULL) {
107                 pp = (char *) get_property(np, "model", NULL);
108                 if (pp != NULL)
109                         seq_printf(m, "%s\n", pp);
110                 else
111                         seq_printf(m, "PowerMac\n");
112                 pp = (char *) get_property(np, "compatible", &plen);
113                 if (pp != NULL) {
114                         seq_printf(m, "motherboard\t:");
115                         while (plen > 0) {
116                                 int l = strlen(pp) + 1;
117                                 seq_printf(m, " %s", pp);
118                                 plen -= l;
119                                 pp += l;
120                         }
121                         seq_printf(m, "\n");
122                 }
123         } else
124                 seq_printf(m, "PowerMac\n");
125
126         /* print parsed model */
127         seq_printf(m, "detected as\t: %d (%s)\n", mbmodel, mbname);
128         seq_printf(m, "pmac flags\t: %08x\n", mbflags);
129
130         /* Indicate newworld */
131         seq_printf(m, "pmac-generation\t: NewWorld\n");
132 }
133
134
135 void __init pmac_setup_arch(void)
136 {
137         struct device_node *cpu;
138         int *fp;
139         unsigned long pvr;
140
141         pvr = PVR_VER(mfspr(PVR));
142
143         /* Set loops_per_jiffy to a half-way reasonable value,
144            for use until calibrate_delay gets called. */
145         cpu = find_type_devices("cpu");
146         if (cpu != 0) {
147                 fp = (int *) get_property(cpu, "clock-frequency", NULL);
148                 if (fp != 0) {
149                         if (pvr == 4 || pvr >= 8)
150                                 /* 604, G3, G4 etc. */
151                                 loops_per_jiffy = *fp / HZ;
152                         else
153                                 /* 601, 603, etc. */
154                                 loops_per_jiffy = *fp / (2*HZ);
155                 } else
156                         loops_per_jiffy = 50000000 / HZ;
157         }
158         
159         /* We can NAP */
160         powersave_nap = 1;
161
162         /* Initialize the PMU */
163         find_via_pmu();
164
165         /* Init NVRAM access */
166         pmac_nvram_init();
167
168         /* Setup SMP callback */
169 #ifdef CONFIG_SMP
170         pmac_setup_smp();
171 #endif
172
173         /* Setup the PCI DMA to "direct" by default. May be overriden
174          * by iommu later on
175          */
176         pci_dma_init_direct();
177
178         /* Lookup PCI hosts */
179         pmac_pci_init();
180
181 #ifdef CONFIG_DUMMY_CONSOLE
182         conswitchp = &dummy_con;
183 #endif
184 }
185
186 extern char *bootpath;
187 extern char *bootdevice;
188 void *boot_host;
189 int boot_target;
190 int boot_part;
191 extern dev_t boot_dev;
192
193 #ifdef CONFIG_SCSI
194 void __init note_scsi_host(struct device_node *node, void *host)
195 {
196         int l;
197         char *p;
198
199         l = strlen(node->full_name);
200         if (bootpath != NULL && bootdevice != NULL
201             && strncmp(node->full_name, bootdevice, l) == 0
202             && (bootdevice[l] == '/' || bootdevice[l] == 0)) {
203                 boot_host = host;
204                 /*
205                  * There's a bug in OF 1.0.5.  (Why am I not surprised.)
206                  * If you pass a path like scsi/sd@1:0 to canon, it returns
207                  * something like /bandit@F2000000/gc@10/53c94@10000/sd@0,0
208                  * That is, the scsi target number doesn't get preserved.
209                  * So we pick the target number out of bootpath and use that.
210                  */
211                 p = strstr(bootpath, "/sd@");
212                 if (p != NULL) {
213                         p += 4;
214                         boot_target = simple_strtoul(p, NULL, 10);
215                         p = strchr(p, ':');
216                         if (p != NULL)
217                                 boot_part = simple_strtoul(p + 1, NULL, 10);
218                 }
219         }
220 }
221 #endif
222
223 #if defined(CONFIG_BLK_DEV_IDE) && defined(CONFIG_BLK_DEV_IDE_PMAC)
224 static dev_t __init find_ide_boot(void)
225 {
226         char *p;
227         int n;
228         dev_t __init pmac_find_ide_boot(char *bootdevice, int n);
229
230         if (bootdevice == NULL)
231                 return 0;
232         p = strrchr(bootdevice, '/');
233         if (p == NULL)
234                 return 0;
235         n = p - bootdevice;
236
237         return pmac_find_ide_boot(bootdevice, n);
238 }
239 #endif /* CONFIG_BLK_DEV_IDE && CONFIG_BLK_DEV_IDE_PMAC */
240
241 void __init find_boot_device(void)
242 {
243 #if defined(CONFIG_BLK_DEV_IDE) && defined(CONFIG_BLK_DEV_IDE_PMAC)
244         boot_dev = find_ide_boot();
245 #endif
246 }
247
248 static int initializing = 1;
249
250 static int pmac_late_init(void)
251 {
252         initializing = 0;
253         return 0;
254 }
255
256 late_initcall(pmac_late_init);
257
258 /* can't be __init - can be called whenever a disk is first accessed */
259 void __pmac note_bootable_part(dev_t dev, int part, int goodness)
260 {
261         static int found_boot = 0;
262         char *p;
263
264         if (!initializing)
265                 return;
266         if ((goodness <= current_root_goodness) &&
267             ROOT_DEV != DEFAULT_ROOT_DEVICE)
268                 return;
269         p = strstr(saved_command_line, "root=");
270         if (p != NULL && (p == saved_command_line || p[-1] == ' '))
271                 return;
272
273         if (!found_boot) {
274                 find_boot_device();
275                 found_boot = 1;
276         }
277         if (!boot_dev || dev == boot_dev) {
278                 ROOT_DEV = dev + part;
279                 boot_dev = 0;
280                 current_root_goodness = goodness;
281         }
282 }
283
284 void __pmac pmac_restart(char *cmd)
285 {
286         pmu_restart();
287 }
288
289 void __pmac pmac_power_off(void)
290 {
291         pmu_shutdown();
292 }
293
294 void __pmac pmac_halt(void)
295 {
296         pmac_power_off();
297 }
298
299 #ifdef CONFIG_BOOTX_TEXT
300 static int dummy_getc_poll(void)
301 {
302         return -1;
303 }
304
305 static unsigned char dummy_getc(void)
306 {
307         return 0;
308 }
309
310 static void btext_putc(unsigned char c)
311 {
312         btext_drawchar(c);
313 }
314 #endif /* CONFIG_BOOTX_TEXT */
315
316 /* 
317  * Early initialization.
318  * Relocation is on but do not reference unbolted pages
319  * Also, device-tree hasn't been "finished", so don't muck with
320  * it too much
321  */
322 void __init pmac_init_early(void)
323 {
324         hpte_init_pSeries();
325
326 #ifdef CONFIG_BOOTX_TEXT
327         ppc_md.udbg_putc = btext_putc;
328         ppc_md.udbg_getc = dummy_getc;
329         ppc_md.udbg_getc_poll = dummy_getc_poll;
330 #endif /* CONFIG_BOOTX_TEXT */
331 }
332
333 extern void* OpenPIC_Addr;
334 extern void* OpenPIC2_Addr;
335 extern u_int OpenPIC_NumInitSenses;
336 extern u_char *OpenPIC_InitSenses;
337 extern void openpic_init(int main_pic, int offset, unsigned char* chrp_ack,
338                          int programmer_switch_irq);
339 extern void openpic2_init(int offset);
340 extern int openpic_get_irq(struct pt_regs *regs);
341 extern int openpic2_get_irq(struct pt_regs *regs);
342
343 static int pmac_cascade_irq = -1;
344
345 static irqreturn_t pmac_u3_do_cascade(int cpl, void *dev_id, struct pt_regs *regs)
346 {
347         int irq;
348
349         for (;;) {
350                 irq = openpic2_get_irq(regs);
351                 if (irq == -1)
352                         break;
353                 ppc_irq_dispatch_handler(regs, irq);
354         }
355         return IRQ_HANDLED;
356 }
357
358 static __init void pmac_init_IRQ(void)
359 {
360         struct device_node *irqctrler  = NULL;
361         struct device_node *irqctrler2 = NULL;
362         struct device_node *np = NULL;
363
364         /* We first try to detect Apple's new Core99 chipset, since mac-io
365          * is quite different on those machines and contains an IBM MPIC2.
366          */
367         while ((np = of_find_node_by_type(np, "open-pic")) != NULL) {
368                 struct device_node *parent = of_get_parent(np);
369                 if (parent && !strcmp(parent->name, "u3"))
370                         irqctrler2 = of_node_get(np);
371                 else
372                         irqctrler = of_node_get(np);
373                 of_node_put(parent);
374         }
375         if (irqctrler != NULL && irqctrler->n_addrs > 0) {
376                 unsigned char senses[128];
377
378                 printk(KERN_INFO "PowerMac using OpenPIC irq controller at 0x%08x\n",
379                        (unsigned int)irqctrler->addrs[0].address);
380
381                 prom_get_irq_senses(senses, 0, 128);
382                 OpenPIC_InitSenses = senses;
383                 OpenPIC_NumInitSenses = 128;
384                 OpenPIC_Addr = ioremap(irqctrler->addrs[0].address,
385                                        irqctrler->addrs[0].size);
386                 openpic_init(1, 0, NULL, -1);
387
388                 if (irqctrler2 != NULL && irqctrler2->n_intrs > 0 &&
389                     irqctrler2->n_addrs > 0) {
390                         printk(KERN_INFO "Slave OpenPIC at 0x%08x hooked on IRQ %d\n",
391                                (u32)irqctrler2->addrs[0].address,
392                                irqctrler2->intrs[0].line);
393                         pmac_call_feature(PMAC_FTR_ENABLE_MPIC, irqctrler2, 0, 0);
394                         OpenPIC2_Addr = ioremap(irqctrler2->addrs[0].address,
395                                                 irqctrler2->addrs[0].size);
396                         prom_get_irq_senses(senses, 128, 128 + 128);
397                         OpenPIC_InitSenses = senses;
398                         OpenPIC_NumInitSenses = 128;
399                         openpic2_init(128);
400                         pmac_cascade_irq = irqctrler2->intrs[0].line;
401                 }
402         }
403         of_node_put(irqctrler);
404         of_node_put(irqctrler2);
405 }
406
407 /* We cannot do request_irq too early ... Right now, we get the
408  * cascade as a core_initcall, which should be fine for our needs
409  */
410 static int __init pmac_irq_cascade_init(void)
411 {
412         if (request_irq(pmac_cascade_irq, pmac_u3_do_cascade, 0,
413                         "U3->K2 Cascade", NULL))
414                 printk(KERN_ERR "Unable to get OpenPIC IRQ for cascade\n");
415         return 0;
416 }
417
418 core_initcall(pmac_irq_cascade_init);
419
420 void __init pmac_init(unsigned long r3, unsigned long r4, unsigned long r5,
421                       unsigned long r6, unsigned long r7)
422 {
423         /* Probe motherboard chipset */
424         pmac_feature_init();
425
426         /* Init SCC */
427         if (strstr(cmd_line, "sccdbg")) {
428                 sccdbg = 1;
429                 udbg_init_scc(NULL);
430         }
431
432         /* Fill up the machine description */
433         ppc_md.setup_arch     = pmac_setup_arch;
434         ppc_md.get_cpuinfo    = pmac_show_cpuinfo;
435
436         ppc_md.init_IRQ       = pmac_init_IRQ;
437         ppc_md.get_irq        = openpic_get_irq;
438
439         ppc_md.pcibios_fixup  = pmac_pcibios_fixup;
440
441         ppc_md.restart        = pmac_restart;
442         ppc_md.power_off      = pmac_power_off;
443         ppc_md.halt           = pmac_halt;
444
445         ppc_md.get_boot_time  = pmac_get_boot_time;
446         ppc_md.set_rtc_time   = pmac_set_rtc_time;
447         ppc_md.get_rtc_time   = pmac_get_rtc_time;
448         ppc_md.calibrate_decr = pmac_calibrate_decr;
449
450         ppc_md.feature_call   = pmac_do_feature_call;
451
452
453 #ifdef CONFIG_BOOTX_TEXT
454         ppc_md.progress       = pmac_progress;
455 #endif /* CONFIG_BOOTX_TEXT */
456
457         if (ppc_md.progress) ppc_md.progress("pmac_init(): exit", 0);
458
459 }
460
461 #ifdef CONFIG_BOOTX_TEXT
462 void __init pmac_progress(char *s, unsigned short hex)
463 {
464         if (sccdbg) {
465                 udbg_puts(s);
466                 udbg_putc('\n');
467         }
468         else if (boot_text_mapped) {
469                 btext_drawstring(s);
470                 btext_drawchar('\n');
471         }
472 }
473 #endif /* CONFIG_BOOTX_TEXT */
474
475 static int __init pmac_declare_of_platform_devices(void)
476 {
477         struct device_node *np;
478
479         np = find_devices("u3");
480         if (np) {
481                 for (np = np->child; np != NULL; np = np->sibling)
482                         if (strncmp(np->name, "i2c", 3) == 0) {
483                                 of_platform_device_create(np, "u3-i2c");
484                                 break;
485                         }
486         }
487
488         return 0;
489 }
490
491 device_initcall(pmac_declare_of_platform_devices);