Linux 2.6.31.8
[linux/fpc-iii.git] / arch / powerpc / platforms / powermac / bootx_init.c
blobcf660916ae0b0101ac36824fd3a2db8fd3e13997
1 /*
2 * Early boot support code for BootX bootloader
4 * Copyright (C) 2005 Ben. Herrenschmidt (benh@kernel.crashing.org)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/init.h>
15 #include <linux/utsrelease.h>
16 #include <asm/sections.h>
17 #include <asm/prom.h>
18 #include <asm/page.h>
19 #include <asm/bootx.h>
20 #include <asm/btext.h>
21 #include <asm/io.h>
23 #undef DEBUG
24 #define SET_BOOT_BAT
26 #ifdef DEBUG
27 #define DBG(fmt...) do { bootx_printf(fmt); } while(0)
28 #else
29 #define DBG(fmt...) do { } while(0)
30 #endif
32 extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
34 static unsigned long __initdata bootx_dt_strbase;
35 static unsigned long __initdata bootx_dt_strend;
36 static unsigned long __initdata bootx_node_chosen;
37 static boot_infos_t * __initdata bootx_info;
38 static char __initdata bootx_disp_path[256];
40 /* Is boot-info compatible ? */
41 #define BOOT_INFO_IS_COMPATIBLE(bi) \
42 ((bi)->compatible_version <= BOOT_INFO_VERSION)
43 #define BOOT_INFO_IS_V2_COMPATIBLE(bi) ((bi)->version >= 2)
44 #define BOOT_INFO_IS_V4_COMPATIBLE(bi) ((bi)->version >= 4)
46 #ifdef CONFIG_BOOTX_TEXT
47 static void __init bootx_printf(const char *format, ...)
49 const char *p, *q, *s;
50 va_list args;
51 unsigned long v;
53 va_start(args, format);
54 for (p = format; *p != 0; p = q) {
55 for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
57 if (q > p)
58 btext_drawtext(p, q - p);
59 if (*q == 0)
60 break;
61 if (*q == '\n') {
62 ++q;
63 btext_flushline();
64 btext_drawstring("\r\n");
65 btext_flushline();
66 continue;
68 ++q;
69 if (*q == 0)
70 break;
71 switch (*q) {
72 case 's':
73 ++q;
74 s = va_arg(args, const char *);
75 if (s == NULL)
76 s = "<NULL>";
77 btext_drawstring(s);
78 break;
79 case 'x':
80 ++q;
81 v = va_arg(args, unsigned long);
82 btext_drawhex(v);
83 break;
87 #else /* CONFIG_BOOTX_TEXT */
88 static void __init bootx_printf(const char *format, ...) {}
89 #endif /* CONFIG_BOOTX_TEXT */
91 static void * __init bootx_early_getprop(unsigned long base,
92 unsigned long node,
93 char *prop)
95 struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
96 u32 *ppp = &np->properties;
98 while(*ppp) {
99 struct bootx_dt_prop *pp =
100 (struct bootx_dt_prop *)(base + *ppp);
102 if (strcmp((char *)((unsigned long)pp->name + base),
103 prop) == 0) {
104 return (void *)((unsigned long)pp->value + base);
106 ppp = &pp->next;
108 return NULL;
111 #define dt_push_token(token, mem) \
112 do { \
113 *(mem) = _ALIGN_UP(*(mem),4); \
114 *((u32 *)*(mem)) = token; \
115 *(mem) += 4; \
116 } while(0)
118 static unsigned long __init bootx_dt_find_string(char *str)
120 char *s, *os;
122 s = os = (char *)bootx_dt_strbase;
123 s += 4;
124 while (s < (char *)bootx_dt_strend) {
125 if (strcmp(s, str) == 0)
126 return s - os;
127 s += strlen(s) + 1;
129 return 0;
132 static void __init bootx_dt_add_prop(char *name, void *data, int size,
133 unsigned long *mem_end)
135 unsigned long soff = bootx_dt_find_string(name);
136 if (data == NULL)
137 size = 0;
138 if (soff == 0) {
139 bootx_printf("WARNING: Can't find string index for <%s>\n",
140 name);
141 return;
143 if (size > 0x20000) {
144 bootx_printf("WARNING: ignoring large property ");
145 bootx_printf("%s length 0x%x\n", name, size);
146 return;
148 dt_push_token(OF_DT_PROP, mem_end);
149 dt_push_token(size, mem_end);
150 dt_push_token(soff, mem_end);
152 /* push property content */
153 if (size && data) {
154 memcpy((void *)*mem_end, data, size);
155 *mem_end = _ALIGN_UP(*mem_end + size, 4);
159 static void __init bootx_add_chosen_props(unsigned long base,
160 unsigned long *mem_end)
162 u32 val;
164 bootx_dt_add_prop("linux,bootx", NULL, 0, mem_end);
166 if (bootx_info->kernelParamsOffset) {
167 char *args = (char *)((unsigned long)bootx_info) +
168 bootx_info->kernelParamsOffset;
169 bootx_dt_add_prop("bootargs", args, strlen(args) + 1, mem_end);
171 if (bootx_info->ramDisk) {
172 val = ((unsigned long)bootx_info) + bootx_info->ramDisk;
173 bootx_dt_add_prop("linux,initrd-start", &val, 4, mem_end);
174 val += bootx_info->ramDiskSize;
175 bootx_dt_add_prop("linux,initrd-end", &val, 4, mem_end);
177 if (strlen(bootx_disp_path))
178 bootx_dt_add_prop("linux,stdout-path", bootx_disp_path,
179 strlen(bootx_disp_path) + 1, mem_end);
182 static void __init bootx_add_display_props(unsigned long base,
183 unsigned long *mem_end,
184 int has_real_node)
186 boot_infos_t *bi = bootx_info;
187 u32 tmp;
189 if (has_real_node) {
190 bootx_dt_add_prop("linux,boot-display", NULL, 0, mem_end);
191 bootx_dt_add_prop("linux,opened", NULL, 0, mem_end);
192 } else
193 bootx_dt_add_prop("linux,bootx-noscreen", NULL, 0, mem_end);
195 tmp = bi->dispDeviceDepth;
196 bootx_dt_add_prop("linux,bootx-depth", &tmp, 4, mem_end);
197 tmp = bi->dispDeviceRect[2] - bi->dispDeviceRect[0];
198 bootx_dt_add_prop("linux,bootx-width", &tmp, 4, mem_end);
199 tmp = bi->dispDeviceRect[3] - bi->dispDeviceRect[1];
200 bootx_dt_add_prop("linux,bootx-height", &tmp, 4, mem_end);
201 tmp = bi->dispDeviceRowBytes;
202 bootx_dt_add_prop("linux,bootx-linebytes", &tmp, 4, mem_end);
203 tmp = (u32)bi->dispDeviceBase;
204 if (tmp == 0)
205 tmp = (u32)bi->logicalDisplayBase;
206 tmp += bi->dispDeviceRect[1] * bi->dispDeviceRowBytes;
207 tmp += bi->dispDeviceRect[0] * ((bi->dispDeviceDepth + 7) / 8);
208 bootx_dt_add_prop("linux,bootx-addr", &tmp, 4, mem_end);
211 static void __init bootx_dt_add_string(char *s, unsigned long *mem_end)
213 unsigned int l = strlen(s) + 1;
214 memcpy((void *)*mem_end, s, l);
215 bootx_dt_strend = *mem_end = *mem_end + l;
218 static void __init bootx_scan_dt_build_strings(unsigned long base,
219 unsigned long node,
220 unsigned long *mem_end)
222 struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
223 u32 *cpp, *ppp = &np->properties;
224 unsigned long soff;
225 char *namep;
227 /* Keep refs to known nodes */
228 namep = np->full_name ? (char *)(base + np->full_name) : NULL;
229 if (namep == NULL) {
230 bootx_printf("Node without a full name !\n");
231 namep = "";
233 DBG("* strings: %s\n", namep);
235 if (!strcmp(namep, "/chosen")) {
236 DBG(" detected /chosen ! adding properties names !\n");
237 bootx_dt_add_string("linux,bootx", mem_end);
238 bootx_dt_add_string("linux,stdout-path", mem_end);
239 bootx_dt_add_string("linux,initrd-start", mem_end);
240 bootx_dt_add_string("linux,initrd-end", mem_end);
241 bootx_dt_add_string("bootargs", mem_end);
242 bootx_node_chosen = node;
244 if (node == bootx_info->dispDeviceRegEntryOffset) {
245 DBG(" detected display ! adding properties names !\n");
246 bootx_dt_add_string("linux,boot-display", mem_end);
247 bootx_dt_add_string("linux,opened", mem_end);
248 strncpy(bootx_disp_path, namep, 255);
251 /* get and store all property names */
252 while (*ppp) {
253 struct bootx_dt_prop *pp =
254 (struct bootx_dt_prop *)(base + *ppp);
256 namep = pp->name ? (char *)(base + pp->name) : NULL;
257 if (namep == NULL || strcmp(namep, "name") == 0)
258 goto next;
259 /* get/create string entry */
260 soff = bootx_dt_find_string(namep);
261 if (soff == 0)
262 bootx_dt_add_string(namep, mem_end);
263 next:
264 ppp = &pp->next;
267 /* do all our children */
268 cpp = &np->child;
269 while(*cpp) {
270 np = (struct bootx_dt_node *)(base + *cpp);
271 bootx_scan_dt_build_strings(base, *cpp, mem_end);
272 cpp = &np->sibling;
276 static void __init bootx_scan_dt_build_struct(unsigned long base,
277 unsigned long node,
278 unsigned long *mem_end)
280 struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
281 u32 *cpp, *ppp = &np->properties;
282 char *namep, *p, *ep, *lp;
283 int l;
285 dt_push_token(OF_DT_BEGIN_NODE, mem_end);
287 /* get the node's full name */
288 namep = np->full_name ? (char *)(base + np->full_name) : NULL;
289 if (namep == NULL)
290 namep = "";
291 l = strlen(namep);
293 DBG("* struct: %s\n", namep);
295 /* Fixup an Apple bug where they have bogus \0 chars in the
296 * middle of the path in some properties, and extract
297 * the unit name (everything after the last '/').
299 memcpy((void *)*mem_end, namep, l + 1);
300 namep = (char *)*mem_end;
301 for (lp = p = namep, ep = namep + l; p < ep; p++) {
302 if (*p == '/')
303 lp = namep;
304 else if (*p != 0)
305 *lp++ = *p;
307 *lp = 0;
308 *mem_end = _ALIGN_UP((unsigned long)lp + 1, 4);
310 /* get and store all properties */
311 while (*ppp) {
312 struct bootx_dt_prop *pp =
313 (struct bootx_dt_prop *)(base + *ppp);
315 namep = pp->name ? (char *)(base + pp->name) : NULL;
316 /* Skip "name" */
317 if (namep == NULL || !strcmp(namep, "name"))
318 goto next;
319 /* Skip "bootargs" in /chosen too as we replace it */
320 if (node == bootx_node_chosen && !strcmp(namep, "bootargs"))
321 goto next;
323 /* push property head */
324 bootx_dt_add_prop(namep,
325 pp->value ? (void *)(base + pp->value): NULL,
326 pp->length, mem_end);
327 next:
328 ppp = &pp->next;
331 if (node == bootx_node_chosen) {
332 bootx_add_chosen_props(base, mem_end);
333 if (bootx_info->dispDeviceRegEntryOffset == 0)
334 bootx_add_display_props(base, mem_end, 0);
336 else if (node == bootx_info->dispDeviceRegEntryOffset)
337 bootx_add_display_props(base, mem_end, 1);
339 /* do all our children */
340 cpp = &np->child;
341 while(*cpp) {
342 np = (struct bootx_dt_node *)(base + *cpp);
343 bootx_scan_dt_build_struct(base, *cpp, mem_end);
344 cpp = &np->sibling;
347 dt_push_token(OF_DT_END_NODE, mem_end);
350 static unsigned long __init bootx_flatten_dt(unsigned long start)
352 boot_infos_t *bi = bootx_info;
353 unsigned long mem_start, mem_end;
354 struct boot_param_header *hdr;
355 unsigned long base;
356 u64 *rsvmap;
358 /* Start using memory after the big blob passed by BootX, get
359 * some space for the header
361 mem_start = mem_end = _ALIGN_UP(((unsigned long)bi) + start, 4);
362 DBG("Boot params header at: %x\n", mem_start);
363 hdr = (struct boot_param_header *)mem_start;
364 mem_end += sizeof(struct boot_param_header);
365 rsvmap = (u64 *)(_ALIGN_UP(mem_end, 8));
366 hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - mem_start;
367 mem_end = ((unsigned long)rsvmap) + 8 * sizeof(u64);
369 /* Get base of tree */
370 base = ((unsigned long)bi) + bi->deviceTreeOffset;
372 /* Build string array */
373 DBG("Building string array at: %x\n", mem_end);
374 DBG("Device Tree Base=%x\n", base);
375 bootx_dt_strbase = mem_end;
376 mem_end += 4;
377 bootx_dt_strend = mem_end;
378 bootx_scan_dt_build_strings(base, 4, &mem_end);
379 /* Add some strings */
380 bootx_dt_add_string("linux,bootx-noscreen", &mem_end);
381 bootx_dt_add_string("linux,bootx-depth", &mem_end);
382 bootx_dt_add_string("linux,bootx-width", &mem_end);
383 bootx_dt_add_string("linux,bootx-height", &mem_end);
384 bootx_dt_add_string("linux,bootx-linebytes", &mem_end);
385 bootx_dt_add_string("linux,bootx-addr", &mem_end);
386 /* Wrap up strings */
387 hdr->off_dt_strings = bootx_dt_strbase - mem_start;
388 hdr->dt_strings_size = bootx_dt_strend - bootx_dt_strbase;
390 /* Build structure */
391 mem_end = _ALIGN(mem_end, 16);
392 DBG("Building device tree structure at: %x\n", mem_end);
393 hdr->off_dt_struct = mem_end - mem_start;
394 bootx_scan_dt_build_struct(base, 4, &mem_end);
395 dt_push_token(OF_DT_END, &mem_end);
397 /* Finish header */
398 hdr->boot_cpuid_phys = 0;
399 hdr->magic = OF_DT_HEADER;
400 hdr->totalsize = mem_end - mem_start;
401 hdr->version = OF_DT_VERSION;
402 /* Version 16 is not backward compatible */
403 hdr->last_comp_version = 0x10;
405 /* Reserve the whole thing and copy the reserve map in, we
406 * also bump mem_reserve_cnt to cause further reservations to
407 * fail since it's too late.
409 mem_end = _ALIGN(mem_end, PAGE_SIZE);
410 DBG("End of boot params: %x\n", mem_end);
411 rsvmap[0] = mem_start;
412 rsvmap[1] = mem_end;
413 if (bootx_info->ramDisk) {
414 rsvmap[2] = ((unsigned long)bootx_info) + bootx_info->ramDisk;
415 rsvmap[3] = rsvmap[2] + bootx_info->ramDiskSize;
416 rsvmap[4] = 0;
417 rsvmap[5] = 0;
418 } else {
419 rsvmap[2] = 0;
420 rsvmap[3] = 0;
423 return (unsigned long)hdr;
427 #ifdef CONFIG_BOOTX_TEXT
428 static void __init btext_welcome(boot_infos_t *bi)
430 unsigned long flags;
431 unsigned long pvr;
433 bootx_printf("Welcome to Linux, kernel " UTS_RELEASE "\n");
434 bootx_printf("\nlinked at : 0x%x", KERNELBASE);
435 bootx_printf("\nframe buffer at : 0x%x", bi->dispDeviceBase);
436 bootx_printf(" (phys), 0x%x", bi->logicalDisplayBase);
437 bootx_printf(" (log)");
438 bootx_printf("\nklimit : 0x%x",(unsigned long)klimit);
439 bootx_printf("\nboot_info at : 0x%x", bi);
440 __asm__ __volatile__ ("mfmsr %0" : "=r" (flags));
441 bootx_printf("\nMSR : 0x%x", flags);
442 __asm__ __volatile__ ("mfspr %0, 287" : "=r" (pvr));
443 bootx_printf("\nPVR : 0x%x", pvr);
444 pvr >>= 16;
445 if (pvr > 1) {
446 __asm__ __volatile__ ("mfspr %0, 1008" : "=r" (flags));
447 bootx_printf("\nHID0 : 0x%x", flags);
449 if (pvr == 8 || pvr == 12 || pvr == 0x800c) {
450 __asm__ __volatile__ ("mfspr %0, 1019" : "=r" (flags));
451 bootx_printf("\nICTC : 0x%x", flags);
453 #ifdef DEBUG
454 bootx_printf("\n\n");
455 bootx_printf("bi->deviceTreeOffset : 0x%x\n",
456 bi->deviceTreeOffset);
457 bootx_printf("bi->deviceTreeSize : 0x%x\n",
458 bi->deviceTreeSize);
459 #endif
460 bootx_printf("\n\n");
462 #endif /* CONFIG_BOOTX_TEXT */
464 void __init bootx_init(unsigned long r3, unsigned long r4)
466 boot_infos_t *bi = (boot_infos_t *) r4;
467 unsigned long hdr;
468 unsigned long space;
469 unsigned long ptr, x;
470 char *model;
471 unsigned long offset = reloc_offset();
473 reloc_got2(offset);
475 bootx_info = bi;
477 /* We haven't cleared any bss at this point, make sure
478 * what we need is initialized
480 bootx_dt_strbase = bootx_dt_strend = 0;
481 bootx_node_chosen = 0;
482 bootx_disp_path[0] = 0;
484 if (!BOOT_INFO_IS_V2_COMPATIBLE(bi))
485 bi->logicalDisplayBase = bi->dispDeviceBase;
487 /* Fixup depth 16 -> 15 as that's what MacOS calls 16bpp */
488 if (bi->dispDeviceDepth == 16)
489 bi->dispDeviceDepth = 15;
492 #ifdef CONFIG_BOOTX_TEXT
493 ptr = (unsigned long)bi->logicalDisplayBase;
494 ptr += bi->dispDeviceRect[1] * bi->dispDeviceRowBytes;
495 ptr += bi->dispDeviceRect[0] * ((bi->dispDeviceDepth + 7) / 8);
496 btext_setup_display(bi->dispDeviceRect[2] - bi->dispDeviceRect[0],
497 bi->dispDeviceRect[3] - bi->dispDeviceRect[1],
498 bi->dispDeviceDepth, bi->dispDeviceRowBytes,
499 (unsigned long)bi->logicalDisplayBase);
500 btext_clearscreen();
501 btext_flushscreen();
502 #endif /* CONFIG_BOOTX_TEXT */
505 * Test if boot-info is compatible. Done only in config
506 * CONFIG_BOOTX_TEXT since there is nothing much we can do
507 * with an incompatible version, except display a message
508 * and eventually hang the processor...
510 * I'll try to keep enough of boot-info compatible in the
511 * future to always allow display of this message;
513 if (!BOOT_INFO_IS_COMPATIBLE(bi)) {
514 bootx_printf(" !!! WARNING - Incompatible version"
515 " of BootX !!!\n\n\n");
516 for (;;)
519 if (bi->architecture != BOOT_ARCH_PCI) {
520 bootx_printf(" !!! WARNING - Usupported machine"
521 " architecture !\n");
522 for (;;)
526 #ifdef CONFIG_BOOTX_TEXT
527 btext_welcome(bi);
528 #endif
530 /* New BootX enters kernel with MMU off, i/os are not allowed
531 * here. This hack will have been done by the boostrap anyway.
533 if (bi->version < 4) {
535 * XXX If this is an iMac, turn off the USB controller.
537 model = (char *) bootx_early_getprop(r4 + bi->deviceTreeOffset,
538 4, "model");
539 if (model
540 && (strcmp(model, "iMac,1") == 0
541 || strcmp(model, "PowerMac1,1") == 0)) {
542 bootx_printf("iMac,1 detected, shutting down USB \n");
543 out_le32((unsigned __iomem *)0x80880008, 1); /* XXX */
547 /* Get a pointer that points above the device tree, args, ramdisk,
548 * etc... to use for generating the flattened tree
550 if (bi->version < 5) {
551 space = bi->deviceTreeOffset + bi->deviceTreeSize;
552 if (bi->ramDisk >= space)
553 space = bi->ramDisk + bi->ramDiskSize;
554 } else
555 space = bi->totalParamsSize;
557 bootx_printf("Total space used by parameters & ramdisk: 0x%x \n", space);
559 /* New BootX will have flushed all TLBs and enters kernel with
560 * MMU switched OFF, so this should not be useful anymore.
562 if (bi->version < 4) {
563 bootx_printf("Touching pages...\n");
566 * Touch each page to make sure the PTEs for them
567 * are in the hash table - the aim is to try to avoid
568 * getting DSI exceptions while copying the kernel image.
570 for (ptr = ((unsigned long) &_stext) & PAGE_MASK;
571 ptr < (unsigned long)bi + space; ptr += PAGE_SIZE)
572 x = *(volatile unsigned long *)ptr;
575 /* Ok, now we need to generate a flattened device-tree to pass
576 * to the kernel
578 bootx_printf("Preparing boot params...\n");
580 hdr = bootx_flatten_dt(space);
582 #ifdef CONFIG_BOOTX_TEXT
583 #ifdef SET_BOOT_BAT
584 bootx_printf("Preparing BAT...\n");
585 btext_prepare_BAT();
586 #else
587 btext_unmap();
588 #endif
589 #endif
591 reloc_got2(-offset);
593 __start(hdr, KERNELBASE + offset, 0);