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