driver: add dev_name inline
[barebox-mini2440.git] / common / ft_build.c
blob578a4b8ed36fa5264339455752eb9cfd0a6673ec
1 /*
2 * OF flat tree builder
3 * Written by: Pantelis Antoniou <pantelis.antoniou@gmail.com>
4 * Updated by: Matthew McClintock <msm@freescale.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19 * MA 02111-1307 USA
22 #include <common.h>
23 #include <boot.h>
24 #include <init.h>
25 #include <malloc.h>
26 #include <environment.h>
27 #include <asm/byteorder.h>
28 #include <stdio.h>
30 #include <errno.h>
31 #include <stddef.h>
33 #include <ft_build.h>
35 #undef DEBUG
37 /* align addr on a size boundary - adjust address up if needed -- Cort */
38 #define _ALIGN(addr,size) (((addr)+(size)-1)&(~((size)-1)))
39 #ifndef CONFIG_OF_BOOT_CPU
40 #define CONFIG_OF_BOOT_CPU 0
41 #endif
42 #define SIZE_OF_RSVMAP_ENTRY (2*sizeof(u64))
44 static void ft_put_word(struct ft_cxt *cxt, u32 v)
46 memmove(cxt->p + sizeof(u32), cxt->p, cxt->p_end - cxt->p);
48 *(u32 *) cxt->p = cpu_to_be32(v);
49 cxt->p += sizeof(u32);
50 cxt->p_end += sizeof(u32);
53 static inline void ft_put_bin(struct ft_cxt *cxt, const void *data, int sz)
55 int aligned_size = ((u8 *)_ALIGN((unsigned long)cxt->p + sz,
56 sizeof(u32))) - cxt->p;
58 memmove(cxt->p + aligned_size, cxt->p, cxt->p_end - cxt->p);
60 /* make sure the last bytes are zeroed */
61 memset(cxt->p + aligned_size - (aligned_size % sizeof(u32)), 0,
62 (aligned_size % sizeof(u32)));
64 memcpy(cxt->p, data, sz);
66 cxt->p += aligned_size;
67 cxt->p_end += aligned_size;
70 void ft_begin_node(struct ft_cxt *cxt, const char *name)
72 ft_put_word(cxt, OF_DT_BEGIN_NODE);
73 ft_put_bin(cxt, name, strlen(name) + 1);
76 void ft_end_node(struct ft_cxt *cxt)
78 ft_put_word(cxt, OF_DT_END_NODE);
81 void ft_nop(struct ft_cxt *cxt)
83 ft_put_word(cxt, OF_DT_NOP);
86 static int lookup_string(struct ft_cxt *cxt, const char *name)
88 u8 *p;
90 p = cxt->p;
91 while (p < cxt->p_end) {
92 if (strcmp((char *)p, name) == 0)
93 return p - cxt->p;
94 p += strlen((char *)p) + 1;
97 return -1;
100 void ft_prop(struct ft_cxt *cxt, const char *name, const void *data, int sz)
102 int off = 0;
104 off = lookup_string(cxt, name);
105 if (off == -1) {
106 memcpy(cxt->p_end, name, strlen(name) + 1);
107 off = cxt->p_end - cxt->p;
108 cxt->p_end += strlen(name) + 1;
111 /* now put offset from beginning of *STRUCTURE* */
112 /* will be fixed up at the end */
113 ft_put_word(cxt, OF_DT_PROP);
114 ft_put_word(cxt, sz);
115 ft_put_word(cxt, off);
116 ft_put_bin(cxt, data, sz);
119 void ft_prop_str(struct ft_cxt *cxt, const char *name, const char *str)
121 ft_prop(cxt, name, str, strlen(str) + 1);
124 void ft_prop_int(struct ft_cxt *cxt, const char *name, int val)
126 u32 v = cpu_to_be32((u32) val);
128 ft_prop(cxt, name, &v, sizeof(u32));
131 /* pick up and start working on a tree in place */
132 void ft_init_cxt(struct ft_cxt *cxt, void *blob)
134 struct boot_param_header *bph = blob;
136 memset(cxt, 0, sizeof(*cxt));
138 cxt->bph = bph;
139 bph->boot_cpuid_phys = CONFIG_OF_BOOT_CPU;
141 /* find beginning and end of reserve map table (zeros in last entry) */
142 cxt->p_rsvmap = (u8 *)bph + bph->off_mem_rsvmap;
143 while ( ((uint64_t *)cxt->p_rsvmap)[0] != 0 &&
144 ((uint64_t *)cxt->p_rsvmap)[1] != 0 ) {
145 cxt->p_rsvmap += SIZE_OF_RSVMAP_ENTRY;
148 cxt->p_start = (u8 *)bph + bph->off_dt_struct;
149 cxt->p_end = (u8 *)bph + bph->totalsize;
150 cxt->p = (u8 *)bph + bph->off_dt_strings;
153 /* add a reserver physical area to the rsvmap */
154 void ft_add_rsvmap(struct ft_cxt *cxt, u64 physstart, u64 physend)
156 memmove(cxt->p_rsvmap + SIZE_OF_RSVMAP_ENTRY, cxt->p_rsvmap,
157 cxt->p_end - cxt->p_rsvmap);
159 ((u64 *)cxt->p_rsvmap)[0] = cpu_to_be64(physstart);
160 ((u64 *)cxt->p_rsvmap)[1] = cpu_to_be64(physend);
161 ((u64 *)cxt->p_rsvmap)[2] = 0;
162 ((u64 *)cxt->p_rsvmap)[3] = 0;
164 cxt->p_rsvmap += SIZE_OF_RSVMAP_ENTRY;
165 cxt->p_start += SIZE_OF_RSVMAP_ENTRY;
166 cxt->p += SIZE_OF_RSVMAP_ENTRY;
167 cxt->p_end += SIZE_OF_RSVMAP_ENTRY;
170 void ft_end_tree(struct ft_cxt *cxt)
172 ft_put_word(cxt, OF_DT_END);
175 /* update the boot param header with correct values */
176 void ft_finalize_tree(struct ft_cxt *cxt) {
177 struct boot_param_header *bph = cxt->bph;
179 bph->totalsize = cxt->p_end - (u8 *)bph;
180 bph->off_dt_struct = cxt->p_start - (u8 *)bph;
181 bph->off_dt_strings = cxt->p - (u8 *)bph;
182 bph->dt_strings_size = cxt->p_end - cxt->p;
185 static inline int isprint(int c)
187 return c >= 0x20 && c <= 0x7e;
190 static int is_printable_string(const void *data, int len)
192 const char *s = data;
193 const char *ss;
195 /* zero length is not */
196 if (len == 0)
197 return 0;
199 /* must terminate with zero */
200 if (s[len - 1] != '\0')
201 return 0;
203 ss = s;
204 while (*s && isprint(*s))
205 s++;
207 /* not zero, or not done yet */
208 if (*s != '\0' || (s + 1 - ss) < len)
209 return 0;
211 return 1;
214 static void print_data(const void *data, int len)
216 int i;
217 const u8 *s;
219 /* no data, don't print */
220 if (len == 0)
221 return;
223 if (is_printable_string(data, len)) {
224 puts(" = \"");
225 puts(data);
226 puts("\"");
227 return;
230 switch (len) {
231 case 1: /* byte */
232 printf(" = <%02x>", (*(u8 *) data) & 0xff);
233 break;
234 case 2: /* half-word */
235 printf(" = <%04x>", be16_to_cpu(*(u16 *) data) & 0xffff);
236 break;
237 case 4: /* word */
238 printf(" = <%x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
239 break;
240 case 8: /* double-word */
241 printf(" = <%qx>", be64_to_cpu(*(uint64_t *) data));
242 break;
243 default: /* anything else... hexdump */
244 printf(" = [");
245 for (i = 0, s = data; i < len; i++)
246 printf("%02x%s", s[i], i < len - 1 ? " " : "");
247 printf("]");
249 break;
253 void ft_dump_blob(const void *bphp)
255 const struct boot_param_header *bph = bphp;
256 const uint64_t *p_rsvmap = (const uint64_t *)
257 ((const char *)bph + be32_to_cpu(bph->off_mem_rsvmap));
258 const u32 *p_struct = (const u32 *)
259 ((const char *)bph + be32_to_cpu(bph->off_dt_struct));
260 const u32 *p_strings = (const u32 *)
261 ((const char *)bph + be32_to_cpu(bph->off_dt_strings));
262 u32 tag;
263 const u32 *p;
264 const char *s, *t;
265 int depth, sz, shift;
266 int i;
267 uint64_t addr, size;
269 if (be32_to_cpu(bph->magic) != OF_DT_HEADER) {
270 /* not valid tree */
271 return;
274 depth = 0;
275 shift = 4;
277 for (i = 0;; i++) {
278 addr = be64_to_cpu(p_rsvmap[i * 2]);
279 size = be64_to_cpu(p_rsvmap[i * 2 + 1]);
280 if (addr == 0 && size == 0)
281 break;
283 printf("/memreserve/ %qx %qx;\n", addr, size);
286 p = p_struct;
287 while ((tag = be32_to_cpu(*p++)) != OF_DT_END) {
289 /* printf("tag: 0x%08x (%d)\n", tag, p - p_struct); */
291 if (tag == OF_DT_BEGIN_NODE) {
292 s = (const char *)p;
293 p = (u32 *) _ALIGN((unsigned long)p + strlen(s) + 1, 4);
295 printf("%*s%s {\n", depth * shift, "", s);
297 depth++;
298 continue;
301 if (tag == OF_DT_END_NODE) {
302 depth--;
304 printf("%*s};\n", depth * shift, "");
305 continue;
308 if (tag == OF_DT_NOP) {
309 printf("%*s[NOP]\n", depth * shift, "");
310 continue;
313 if (tag != OF_DT_PROP) {
314 fprintf(stderr, "%*s ** Unknown tag 0x%08x at 0x%x\n",
315 depth * shift, "", tag, --p);
316 break;
318 sz = be32_to_cpu(*p++);
319 s = (const char *)p_strings + be32_to_cpu(*p++);
320 t = (const char *)p;
321 p = (const u32 *)_ALIGN((unsigned long)p + sz, 4);
322 printf("%*s%s", depth * shift, "", s);
323 print_data(t, sz);
324 printf(";\n");
328 void ft_backtrack_node(struct ft_cxt *cxt)
330 int i = 4;
332 while (be32_to_cpu(*(u32 *) (cxt->p - i)) != OF_DT_END_NODE)
333 i += 4;
335 memmove (cxt->p - i, cxt->p, cxt->p_end - cxt->p);
337 cxt->p_end -= i;
338 cxt->p -= i;
341 void *ft_get_prop(void *bphp, const char *propname, int *szp)
343 struct boot_param_header *bph = bphp;
344 uint32_t *p_struct =
345 (uint32_t *) ((char *)bph + be32_to_cpu(bph->off_dt_struct));
346 uint32_t *p_strings =
347 (uint32_t *) ((char *)bph + be32_to_cpu(bph->off_dt_strings));
348 uint32_t version = be32_to_cpu(bph->version);
349 uint32_t tag;
350 uint32_t *p;
351 char *s, *t;
352 char *ss;
353 int sz;
354 static char path[256], prop[256];
356 path[0] = '\0';
358 p = p_struct;
359 while ((tag = be32_to_cpu(*p++)) != OF_DT_END) {
361 if (tag == OF_DT_BEGIN_NODE) {
362 s = (char *)p;
363 p = (uint32_t *) _ALIGN((unsigned long)p + strlen(s) +
364 1, 4);
365 strcat(path, s);
366 strcat(path, "/");
367 continue;
370 if (tag == OF_DT_END_NODE) {
371 path[strlen(path) - 1] = '\0';
372 ss = strrchr(path, '/');
373 if (ss != NULL)
374 ss[1] = '\0';
375 continue;
378 if (tag == OF_DT_NOP)
379 continue;
381 if (tag != OF_DT_PROP)
382 break;
384 sz = be32_to_cpu(*p++);
385 s = (char *)p_strings + be32_to_cpu(*p++);
386 if (version < 0x10 && sz >= 8)
387 p = (uint32_t *) _ALIGN((unsigned long)p, 8);
388 t = (char *)p;
389 p = (uint32_t *) _ALIGN((unsigned long)p + sz, 4);
391 strcpy(prop, path);
392 strcat(prop, s);
394 if (strcmp(prop, propname) == 0) {
395 *szp = sz;
396 return t;
400 return NULL;
403 /********************************************************************/
405 /* Function that returns a character from the environment */
406 extern uchar(*env_get_char) (int);
408 #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
410 #ifdef CONFIG_OF_HAS_BD_T
411 static const struct {
412 const char *name;
413 int offset;
414 } bd_map[] = {
415 BDM(memstart),
416 BDM(memsize),
417 BDM(flashstart),
418 BDM(flashsize),
419 BDM(flashoffset),
420 BDM(sramstart),
421 BDM(sramsize),
422 #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
423 || defined(CONFIG_E500)
424 BDM(immr_base),
425 #endif
426 #if defined(CONFIG_MPC5xxx)
427 BDM(mbar_base),
428 #endif
429 #if defined(CONFIG_MPC83XX)
430 BDM(immrbar),
431 #endif
432 #if defined(CONFIG_MPC8220)
433 BDM(mbar_base),
434 BDM(inpfreq),
435 BDM(pcifreq),
436 BDM(pevfreq),
437 BDM(flbfreq),
438 BDM(vcofreq),
439 #endif
440 BDM(bootflags),
441 BDM(ip_addr),
442 BDM(intfreq),
443 BDM(busfreq),
444 #ifdef CONFIG_CPM2
445 BDM(cpmfreq),
446 BDM(brgfreq),
447 BDM(sccfreq),
448 BDM(vco),
449 #endif
450 #if defined(CONFIG_MPC5xxx)
451 BDM(ipbfreq),
452 BDM(pcifreq),
453 #endif
454 BDM(baudrate),
456 #endif
458 void ft_setup(void *blob, bd_t * bd, ulong initrd_start, ulong initrd_end)
460 u32 *p;
461 int len;
462 struct ft_cxt cxt;
463 ulong clock;
464 #if defined(CONFIG_OF_HAS_UBOOT_ENV)
465 int k, nxt;
466 #endif
467 #if defined(CONFIG_OF_HAS_BD_T)
468 u8 *end;
469 #endif
470 #if defined(CONFIG_OF_HAS_UBOOT_ENV) || defined(CONFIG_OF_HAS_BD_T)
471 int i;
472 static char tmpenv[256];
473 #endif
475 /* disable OF tree; booting old kernel */
476 if (getenv("disable_of") != NULL) {
477 memcpy(blob, bd, sizeof(*bd));
478 return;
481 #ifdef DEBUG
482 printf ("recieved oftree\n");
483 ft_dump_blob(blob);
484 #endif
486 ft_init_cxt(&cxt, blob);
488 if (initrd_start && initrd_end)
489 ft_add_rsvmap(&cxt, initrd_start, initrd_end - initrd_start + 1);
491 /* back into root */
492 ft_backtrack_node(&cxt);
494 #ifdef CONFIG_OF_HAS_UBOOT_ENV
495 ft_begin_node(&cxt, "u-boot-env");
497 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
498 char *s, *lval, *rval;
500 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) ;
501 s = tmpenv;
502 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
503 *s++ = env_get_char(k);
504 *s++ = '\0';
505 lval = tmpenv;
506 s = strchr(tmpenv, '=');
507 if (s != NULL) {
508 *s++ = '\0';
509 rval = s;
510 } else
511 continue;
512 ft_prop_str(&cxt, lval, rval);
515 ft_end_node(&cxt);
516 #endif
518 ft_begin_node(&cxt, "chosen");
519 ft_prop_str(&cxt, "name", "chosen");
521 ft_prop_str(&cxt, "bootargs", getenv("bootargs"));
522 ft_prop_int(&cxt, "linux,platform", 0x600); /* what is this? */
523 if (initrd_start && initrd_end) {
524 ft_prop_int(&cxt, "linux,initrd-start", initrd_start);
525 ft_prop_int(&cxt, "linux,initrd-end", initrd_end);
527 #ifdef OF_STDOUT_PATH
528 ft_prop_str(&cxt, "linux,stdout-path", OF_STDOUT_PATH);
529 #endif
531 ft_end_node(&cxt);
533 ft_end_node(&cxt); /* end root */
535 ft_end_tree(&cxt);
536 ft_finalize_tree(&cxt);
538 #ifdef CONFIG_OF_HAS_BD_T
539 /* paste the bd_t at the end of the flat tree */
540 end = (char *)blob +
541 be32_to_cpu(((struct boot_param_header *)blob)->totalsize);
542 memcpy(end, bd, sizeof(*bd));
543 #endif
545 #ifdef CONFIG_PPC
547 #ifdef CONFIG_OF_HAS_BD_T
548 for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
549 uint32_t v;
551 sprintf(tmpenv, "/bd_t/%s", bd_map[i].name);
552 v = *(uint32_t *)((char *)bd + bd_map[i].offset);
554 p = ft_get_prop(blob, tmpenv, &len);
555 if (p != NULL)
556 *p = cpu_to_be32(v);
559 p = ft_get_prop(blob, "/bd_t/enetaddr", &len);
560 if (p != NULL)
561 memcpy(p, bd->bi_enetaddr, 6);
563 p = ft_get_prop(blob, "/bd_t/ethspeed", &len);
564 if (p != NULL)
565 *p = cpu_to_be32((uint32_t) bd->bi_ethspeed);
566 #endif
568 clock = bd->bi_intfreq;
569 p = ft_get_prop(blob, "/cpus/" OF_CPU "/clock-frequency", &len);
570 if (p != NULL)
571 *p = cpu_to_be32(clock);
573 #ifdef OF_TBCLK
574 clock = OF_TBCLK;
575 p = ft_get_prop(blob, "/cpus/" OF_CPU "/timebase-frequency", &len);
576 if (p != NULL)
577 *p = cpu_to_be32(clock);
578 #endif
579 #endif /* __powerpc__ */
581 #ifdef CONFIG_OF_BOARD_SETUP
582 ft_board_setup(blob, bd);
583 #endif
585 /* in case the size changed in the platform code */
586 ft_finalize_tree(&cxt);
588 #ifdef DEBUG
589 printf("final OF-tree\n");
590 ft_dump_blob(blob);
591 #endif
594 static int oftree_handler_cmdline_parse(struct image_data *data, int opt,
595 char *optarg)
597 switch(opt) {
598 case 'o':
599 printf("using oftree %s\n", optarg);
600 data->oftree = optarg;
601 return 0;
602 default:
603 return 1;
607 static struct image_handler of_handler = {
608 .cmdline_options = "o:",
609 .cmdline_parse = oftree_handler_cmdline_parse,
610 .help_string = " -o <oftree> use oftree",
613 static int oftree_register_image_handler(void)
615 return register_image_handler(&of_handler);
618 late_initcall(oftree_register_image_handler);