]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/powerpc/platforms/powermac/bootx_init.c
[PATCH] powerpc: Kill _machine and hard-coded platform numbers
[mirror_ubuntu-artful-kernel.git] / arch / powerpc / platforms / powermac / bootx_init.c
1 /*
2 * Early boot support code for BootX bootloader
3 *
4 * Copyright (C) 2005 Ben. Herrenschmidt (benh@kernel.crashing.org)
5 *
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.
10 */
11
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/init.h>
16 #include <linux/version.h>
17 #include <asm/sections.h>
18 #include <asm/prom.h>
19 #include <asm/page.h>
20 #include <asm/bootx.h>
21 #include <asm/bootinfo.h>
22 #include <asm/btext.h>
23 #include <asm/io.h>
24
25 #undef DEBUG
26 #define SET_BOOT_BAT
27
28 #ifdef DEBUG
29 #define DBG(fmt...) do { bootx_printf(fmt); } while(0)
30 #else
31 #define DBG(fmt...) do { } while(0)
32 #endif
33
34 extern void __start(unsigned long r3, unsigned long r4, unsigned long r5);
35
36 static unsigned long __initdata bootx_dt_strbase;
37 static unsigned long __initdata bootx_dt_strend;
38 static unsigned long __initdata bootx_node_chosen;
39 static boot_infos_t * __initdata bootx_info;
40 static char __initdata bootx_disp_path[256];
41
42 /* Is boot-info compatible ? */
43 #define BOOT_INFO_IS_COMPATIBLE(bi) \
44 ((bi)->compatible_version <= BOOT_INFO_VERSION)
45 #define BOOT_INFO_IS_V2_COMPATIBLE(bi) ((bi)->version >= 2)
46 #define BOOT_INFO_IS_V4_COMPATIBLE(bi) ((bi)->version >= 4)
47
48 #ifdef CONFIG_BOOTX_TEXT
49 static void __init bootx_printf(const char *format, ...)
50 {
51 const char *p, *q, *s;
52 va_list args;
53 unsigned long v;
54
55 va_start(args, format);
56 for (p = format; *p != 0; p = q) {
57 for (q = p; *q != 0 && *q != '\n' && *q != '%'; ++q)
58 ;
59 if (q > p)
60 btext_drawtext(p, q - p);
61 if (*q == 0)
62 break;
63 if (*q == '\n') {
64 ++q;
65 btext_flushline();
66 btext_drawstring("\r\n");
67 btext_flushline();
68 continue;
69 }
70 ++q;
71 if (*q == 0)
72 break;
73 switch (*q) {
74 case 's':
75 ++q;
76 s = va_arg(args, const char *);
77 if (s == NULL)
78 s = "<NULL>";
79 btext_drawstring(s);
80 break;
81 case 'x':
82 ++q;
83 v = va_arg(args, unsigned long);
84 btext_drawhex(v);
85 break;
86 }
87 }
88 }
89 #else /* CONFIG_BOOTX_TEXT */
90 static void __init bootx_printf(const char *format, ...) {}
91 #endif /* CONFIG_BOOTX_TEXT */
92
93 static void * __init bootx_early_getprop(unsigned long base,
94 unsigned long node,
95 char *prop)
96 {
97 struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
98 u32 *ppp = &np->properties;
99
100 while(*ppp) {
101 struct bootx_dt_prop *pp =
102 (struct bootx_dt_prop *)(base + *ppp);
103
104 if (strcmp((char *)((unsigned long)pp->name + base),
105 prop) == 0) {
106 return (void *)((unsigned long)pp->value + base);
107 }
108 ppp = &pp->next;
109 }
110 return NULL;
111 }
112
113 #define dt_push_token(token, mem) \
114 do { \
115 *(mem) = _ALIGN_UP(*(mem),4); \
116 *((u32 *)*(mem)) = token; \
117 *(mem) += 4; \
118 } while(0)
119
120 static unsigned long __init bootx_dt_find_string(char *str)
121 {
122 char *s, *os;
123
124 s = os = (char *)bootx_dt_strbase;
125 s += 4;
126 while (s < (char *)bootx_dt_strend) {
127 if (strcmp(s, str) == 0)
128 return s - os;
129 s += strlen(s) + 1;
130 }
131 return 0;
132 }
133
134 static void __init bootx_dt_add_prop(char *name, void *data, int size,
135 unsigned long *mem_end)
136 {
137 unsigned long soff = bootx_dt_find_string(name);
138 if (data == NULL)
139 size = 0;
140 if (soff == 0) {
141 bootx_printf("WARNING: Can't find string index for <%s>\n",
142 name);
143 return;
144 }
145 if (size > 0x20000) {
146 bootx_printf("WARNING: ignoring large property ");
147 bootx_printf("%s length 0x%x\n", name, size);
148 return;
149 }
150 dt_push_token(OF_DT_PROP, mem_end);
151 dt_push_token(size, mem_end);
152 dt_push_token(soff, mem_end);
153
154 /* push property content */
155 if (size && data) {
156 memcpy((void *)*mem_end, data, size);
157 *mem_end = _ALIGN_UP(*mem_end + size, 4);
158 }
159 }
160
161 static void __init bootx_add_chosen_props(unsigned long base,
162 unsigned long *mem_end)
163 {
164 u32 val;
165
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);
170 }
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);
176 }
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);
180 }
181
182 static void __init bootx_add_display_props(unsigned long base,
183 unsigned long *mem_end)
184 {
185 bootx_dt_add_prop("linux,boot-display", NULL, 0, mem_end);
186 bootx_dt_add_prop("linux,opened", NULL, 0, mem_end);
187 }
188
189 static void __init bootx_dt_add_string(char *s, unsigned long *mem_end)
190 {
191 unsigned int l = strlen(s) + 1;
192 memcpy((void *)*mem_end, s, l);
193 bootx_dt_strend = *mem_end = *mem_end + l;
194 }
195
196 static void __init bootx_scan_dt_build_strings(unsigned long base,
197 unsigned long node,
198 unsigned long *mem_end)
199 {
200 struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
201 u32 *cpp, *ppp = &np->properties;
202 unsigned long soff;
203 char *namep;
204
205 /* Keep refs to known nodes */
206 namep = np->full_name ? (char *)(base + np->full_name) : NULL;
207 if (namep == NULL) {
208 bootx_printf("Node without a full name !\n");
209 namep = "";
210 }
211 DBG("* strings: %s\n", namep);
212
213 if (!strcmp(namep, "/chosen")) {
214 DBG(" detected /chosen ! adding properties names !\n");
215 bootx_dt_add_string("linux,platform", mem_end);
216 bootx_dt_add_string("linux,stdout-path", mem_end);
217 bootx_dt_add_string("linux,initrd-start", mem_end);
218 bootx_dt_add_string("linux,initrd-end", mem_end);
219 bootx_dt_add_string("bootargs", mem_end);
220 bootx_node_chosen = node;
221 }
222 if (node == bootx_info->dispDeviceRegEntryOffset) {
223 DBG(" detected display ! adding properties names !\n");
224 bootx_dt_add_string("linux,boot-display", mem_end);
225 bootx_dt_add_string("linux,opened", mem_end);
226 strncpy(bootx_disp_path, namep, 255);
227 }
228
229 /* get and store all property names */
230 while (*ppp) {
231 struct bootx_dt_prop *pp =
232 (struct bootx_dt_prop *)(base + *ppp);
233
234 namep = pp->name ? (char *)(base + pp->name) : NULL;
235 if (namep == NULL || strcmp(namep, "name") == 0)
236 goto next;
237 /* get/create string entry */
238 soff = bootx_dt_find_string(namep);
239 if (soff == 0)
240 bootx_dt_add_string(namep, mem_end);
241 next:
242 ppp = &pp->next;
243 }
244
245 /* do all our children */
246 cpp = &np->child;
247 while(*cpp) {
248 np = (struct bootx_dt_node *)(base + *cpp);
249 bootx_scan_dt_build_strings(base, *cpp, mem_end);
250 cpp = &np->sibling;
251 }
252 }
253
254 static void __init bootx_scan_dt_build_struct(unsigned long base,
255 unsigned long node,
256 unsigned long *mem_end)
257 {
258 struct bootx_dt_node *np = (struct bootx_dt_node *)(base + node);
259 u32 *cpp, *ppp = &np->properties;
260 char *namep, *p, *ep, *lp;
261 int l;
262
263 dt_push_token(OF_DT_BEGIN_NODE, mem_end);
264
265 /* get the node's full name */
266 namep = np->full_name ? (char *)(base + np->full_name) : NULL;
267 if (namep == NULL)
268 namep = "";
269 l = strlen(namep);
270
271 DBG("* struct: %s\n", namep);
272
273 /* Fixup an Apple bug where they have bogus \0 chars in the
274 * middle of the path in some properties, and extract
275 * the unit name (everything after the last '/').
276 */
277 memcpy((void *)*mem_end, namep, l + 1);
278 namep = (char *)*mem_end;
279 for (lp = p = namep, ep = namep + l; p < ep; p++) {
280 if (*p == '/')
281 lp = namep;
282 else if (*p != 0)
283 *lp++ = *p;
284 }
285 *lp = 0;
286 *mem_end = _ALIGN_UP((unsigned long)lp + 1, 4);
287
288 /* get and store all properties */
289 while (*ppp) {
290 struct bootx_dt_prop *pp =
291 (struct bootx_dt_prop *)(base + *ppp);
292
293 namep = pp->name ? (char *)(base + pp->name) : NULL;
294 /* Skip "name" */
295 if (namep == NULL || !strcmp(namep, "name"))
296 goto next;
297 /* Skip "bootargs" in /chosen too as we replace it */
298 if (node == bootx_node_chosen && !strcmp(namep, "bootargs"))
299 goto next;
300
301 /* push property head */
302 bootx_dt_add_prop(namep,
303 pp->value ? (void *)(base + pp->value): NULL,
304 pp->length, mem_end);
305 next:
306 ppp = &pp->next;
307 }
308
309 if (node == bootx_node_chosen)
310 bootx_add_chosen_props(base, mem_end);
311 if (node == bootx_info->dispDeviceRegEntryOffset)
312 bootx_add_display_props(base, mem_end);
313
314 /* do all our children */
315 cpp = &np->child;
316 while(*cpp) {
317 np = (struct bootx_dt_node *)(base + *cpp);
318 bootx_scan_dt_build_struct(base, *cpp, mem_end);
319 cpp = &np->sibling;
320 }
321
322 dt_push_token(OF_DT_END_NODE, mem_end);
323 }
324
325 static unsigned long __init bootx_flatten_dt(unsigned long start)
326 {
327 boot_infos_t *bi = bootx_info;
328 unsigned long mem_start, mem_end;
329 struct boot_param_header *hdr;
330 unsigned long base;
331 u64 *rsvmap;
332
333 /* Start using memory after the big blob passed by BootX, get
334 * some space for the header
335 */
336 mem_start = mem_end = _ALIGN_UP(((unsigned long)bi) + start, 4);
337 DBG("Boot params header at: %x\n", mem_start);
338 hdr = (struct boot_param_header *)mem_start;
339 mem_end += sizeof(struct boot_param_header);
340 rsvmap = (u64 *)(_ALIGN_UP(mem_end, 8));
341 hdr->off_mem_rsvmap = ((unsigned long)rsvmap) - mem_start;
342 mem_end = ((unsigned long)rsvmap) + 8 * sizeof(u64);
343
344 /* Get base of tree */
345 base = ((unsigned long)bi) + bi->deviceTreeOffset;
346
347 /* Build string array */
348 DBG("Building string array at: %x\n", mem_end);
349 DBG("Device Tree Base=%x\n", base);
350 bootx_dt_strbase = mem_end;
351 mem_end += 4;
352 bootx_dt_strend = mem_end;
353 bootx_scan_dt_build_strings(base, 4, &mem_end);
354 hdr->off_dt_strings = bootx_dt_strbase - mem_start;
355 hdr->dt_strings_size = bootx_dt_strend - bootx_dt_strbase;
356
357 /* Build structure */
358 mem_end = _ALIGN(mem_end, 16);
359 DBG("Building device tree structure at: %x\n", mem_end);
360 hdr->off_dt_struct = mem_end - mem_start;
361 bootx_scan_dt_build_struct(base, 4, &mem_end);
362 dt_push_token(OF_DT_END, &mem_end);
363
364 /* Finish header */
365 hdr->boot_cpuid_phys = 0;
366 hdr->magic = OF_DT_HEADER;
367 hdr->totalsize = mem_end - mem_start;
368 hdr->version = OF_DT_VERSION;
369 /* Version 16 is not backward compatible */
370 hdr->last_comp_version = 0x10;
371
372 /* Reserve the whole thing and copy the reserve map in, we
373 * also bump mem_reserve_cnt to cause further reservations to
374 * fail since it's too late.
375 */
376 mem_end = _ALIGN(mem_end, PAGE_SIZE);
377 DBG("End of boot params: %x\n", mem_end);
378 rsvmap[0] = mem_start;
379 rsvmap[1] = mem_end;
380 rsvmap[2] = 0;
381 rsvmap[3] = 0;
382
383 return (unsigned long)hdr;
384 }
385
386
387 #ifdef CONFIG_BOOTX_TEXT
388 static void __init btext_welcome(boot_infos_t *bi)
389 {
390 unsigned long flags;
391 unsigned long pvr;
392
393 bootx_printf("Welcome to Linux, kernel " UTS_RELEASE "\n");
394 bootx_printf("\nlinked at : 0x%x", KERNELBASE);
395 bootx_printf("\nframe buffer at : 0x%x", bi->dispDeviceBase);
396 bootx_printf(" (phys), 0x%x", bi->logicalDisplayBase);
397 bootx_printf(" (log)");
398 bootx_printf("\nklimit : 0x%x",(unsigned long)klimit);
399 bootx_printf("\nboot_info at : 0x%x", bi);
400 __asm__ __volatile__ ("mfmsr %0" : "=r" (flags));
401 bootx_printf("\nMSR : 0x%x", flags);
402 __asm__ __volatile__ ("mfspr %0, 287" : "=r" (pvr));
403 bootx_printf("\nPVR : 0x%x", pvr);
404 pvr >>= 16;
405 if (pvr > 1) {
406 __asm__ __volatile__ ("mfspr %0, 1008" : "=r" (flags));
407 bootx_printf("\nHID0 : 0x%x", flags);
408 }
409 if (pvr == 8 || pvr == 12 || pvr == 0x800c) {
410 __asm__ __volatile__ ("mfspr %0, 1019" : "=r" (flags));
411 bootx_printf("\nICTC : 0x%x", flags);
412 }
413 #ifdef DEBUG
414 bootx_printf("\n\n");
415 bootx_printf("bi->deviceTreeOffset : 0x%x\n",
416 bi->deviceTreeOffset);
417 bootx_printf("bi->deviceTreeSize : 0x%x\n",
418 bi->deviceTreeSize);
419 #endif
420 bootx_printf("\n\n");
421 }
422 #endif /* CONFIG_BOOTX_TEXT */
423
424 void __init bootx_init(unsigned long r3, unsigned long r4)
425 {
426 boot_infos_t *bi = (boot_infos_t *) r4;
427 unsigned long hdr;
428 unsigned long space;
429 unsigned long ptr, x;
430 char *model;
431 unsigned long offset = reloc_offset();
432
433 reloc_got2(offset);
434
435 bootx_info = bi;
436
437 /* We haven't cleared any bss at this point, make sure
438 * what we need is initialized
439 */
440 bootx_dt_strbase = bootx_dt_strend = 0;
441 bootx_node_chosen = 0;
442 bootx_disp_path[0] = 0;
443
444 if (!BOOT_INFO_IS_V2_COMPATIBLE(bi))
445 bi->logicalDisplayBase = bi->dispDeviceBase;
446
447 #ifdef CONFIG_BOOTX_TEXT
448 btext_setup_display(bi->dispDeviceRect[2] - bi->dispDeviceRect[0],
449 bi->dispDeviceRect[3] - bi->dispDeviceRect[1],
450 bi->dispDeviceDepth, bi->dispDeviceRowBytes,
451 (unsigned long)bi->logicalDisplayBase);
452 btext_clearscreen();
453 btext_flushscreen();
454 #endif /* CONFIG_BOOTX_TEXT */
455
456 /*
457 * Test if boot-info is compatible. Done only in config
458 * CONFIG_BOOTX_TEXT since there is nothing much we can do
459 * with an incompatible version, except display a message
460 * and eventually hang the processor...
461 *
462 * I'll try to keep enough of boot-info compatible in the
463 * future to always allow display of this message;
464 */
465 if (!BOOT_INFO_IS_COMPATIBLE(bi)) {
466 bootx_printf(" !!! WARNING - Incompatible version"
467 " of BootX !!!\n\n\n");
468 for (;;)
469 ;
470 }
471 if (bi->architecture != BOOT_ARCH_PCI) {
472 bootx_printf(" !!! WARNING - Usupported machine"
473 " architecture !\n");
474 for (;;)
475 ;
476 }
477
478 #ifdef CONFIG_BOOTX_TEXT
479 btext_welcome(bi);
480 #endif
481 /* New BootX enters kernel with MMU off, i/os are not allowed
482 * here. This hack will have been done by the boostrap anyway.
483 */
484 if (bi->version < 4) {
485 /*
486 * XXX If this is an iMac, turn off the USB controller.
487 */
488 model = (char *) bootx_early_getprop(r4 + bi->deviceTreeOffset,
489 4, "model");
490 if (model
491 && (strcmp(model, "iMac,1") == 0
492 || strcmp(model, "PowerMac1,1") == 0)) {
493 bootx_printf("iMac,1 detected, shutting down USB \n");
494 out_le32((unsigned __iomem *)0x80880008, 1); /* XXX */
495 }
496 }
497
498 /* Get a pointer that points above the device tree, args, ramdisk,
499 * etc... to use for generating the flattened tree
500 */
501 if (bi->version < 5) {
502 space = bi->deviceTreeOffset + bi->deviceTreeSize;
503 if (bi->ramDisk)
504 space = bi->ramDisk + bi->ramDiskSize;
505 } else
506 space = bi->totalParamsSize;
507
508 bootx_printf("Total space used by parameters & ramdisk: %x \n", space);
509
510 /* New BootX will have flushed all TLBs and enters kernel with
511 * MMU switched OFF, so this should not be useful anymore.
512 */
513 if (bi->version < 4) {
514 bootx_printf("Touching pages...\n");
515
516 /*
517 * Touch each page to make sure the PTEs for them
518 * are in the hash table - the aim is to try to avoid
519 * getting DSI exceptions while copying the kernel image.
520 */
521 for (ptr = ((unsigned long) &_stext) & PAGE_MASK;
522 ptr < (unsigned long)bi + space; ptr += PAGE_SIZE)
523 x = *(volatile unsigned long *)ptr;
524 }
525
526 /* Ok, now we need to generate a flattened device-tree to pass
527 * to the kernel
528 */
529 bootx_printf("Preparing boot params...\n");
530
531 hdr = bootx_flatten_dt(space);
532
533 #ifdef CONFIG_BOOTX_TEXT
534 #ifdef SET_BOOT_BAT
535 bootx_printf("Preparing BAT...\n");
536 btext_prepare_BAT();
537 #else
538 btext_unmap();
539 #endif
540 #endif
541
542 reloc_got2(-offset);
543
544 __start(hdr, KERNELBASE + offset, 0);
545 }