3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 #include <linux/ctype.h>
26 #include <linux/types.h>
27 #include <asm/global_data.h>
30 #include <fdt_support.h>
34 * Global data (for the gd->bd)
36 DECLARE_GLOBAL_DATA_PTR;
39 * fdt points to our working device tree.
41 struct fdt_header *fdt;
43 /********************************************************************/
46 * fdt_find_and_setprop: Find a node and set it's property
48 * @fdt: ptr to device tree
50 * @prop: property name
51 * @val: ptr to new value
52 * @len: length of new property value
53 * @create: flag to create the property if it doesn't exist
55 * Convenience function to directly set a property given the path to the node.
57 int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
58 const void *val, int len, int create)
60 int nodeoff = fdt_path_offset(fdt, node);
65 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL))
66 return 0; /* create flag not set; so exit quietly */
68 return fdt_setprop(fdt, nodeoff, prop, val, len);
71 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
72 static int fdt_fixup_stdout(void *fdt, int choosenoff)
75 #ifdef CONFIG_CONS_INDEX
77 char sername[9] = { 0 };
80 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
82 err = node = fdt_path_offset(fdt, "/aliases");
85 path = fdt_getprop(fdt, node, sername, &len);
87 char *p = malloc(len);
88 err = -FDT_ERR_NOSPACE;
91 err = fdt_setprop(fdt, choosenoff,
92 "linux,stdout-path", p, len);
101 printf("WARNING: could not set linux,stdout-path %s.\n",
108 int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
112 u32 tmp; /* used to set 32 bit integer properties */
113 char *str; /* used to set string properties */
115 err = fdt_check_header(fdt);
117 printf("fdt_chosen: %s\n", fdt_strerror(err));
121 if (initrd_start && initrd_end) {
123 int total = fdt_num_mem_rsv(fdt);
127 * Look for an existing entry and update it. If we don't find
128 * the entry, we will j be the next available slot.
130 for (j = 0; j < total; j++) {
131 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
132 if (addr == initrd_start) {
133 fdt_del_mem_rsv(fdt, j);
138 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1);
140 printf("fdt_chosen: %s\n", fdt_strerror(err));
146 * Find the "chosen" node.
148 nodeoffset = fdt_path_offset (fdt, "/chosen");
151 * If we have a "chosen" node already the "force the writing"
152 * is not set, our job is done.
154 if ((nodeoffset >= 0) && !force)
158 * No "chosen" node in the blob: create it.
160 if (nodeoffset < 0) {
162 * Create a new node "/chosen" (offset 0 is root level)
164 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
165 if (nodeoffset < 0) {
166 printf("WARNING: could not create /chosen %s.\n",
167 fdt_strerror(nodeoffset));
173 * Update pre-existing properties, create them if non-existant.
175 str = getenv("bootargs");
177 err = fdt_setprop(fdt, nodeoffset,
178 "bootargs", str, strlen(str)+1);
180 printf("WARNING: could not set bootargs %s.\n",
183 if (initrd_start && initrd_end) {
184 tmp = __cpu_to_be32(initrd_start);
185 err = fdt_setprop(fdt, nodeoffset,
186 "linux,initrd-start", &tmp, sizeof(tmp));
189 "could not set linux,initrd-start %s.\n",
191 tmp = __cpu_to_be32(initrd_end);
192 err = fdt_setprop(fdt, nodeoffset,
193 "linux,initrd-end", &tmp, sizeof(tmp));
195 printf("WARNING: could not set linux,initrd-end %s.\n",
199 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
200 err = fdt_fixup_stdout(fdt, nodeoffset);
203 #ifdef OF_STDOUT_PATH
204 err = fdt_setprop(fdt, nodeoffset,
205 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
207 printf("WARNING: could not set linux,stdout-path %s.\n",
214 /********************************************************************/
216 #ifdef CONFIG_OF_HAS_UBOOT_ENV
218 /* Function that returns a character from the environment */
219 extern uchar(*env_get_char) (int);
222 int fdt_env(void *fdt)
228 static char tmpenv[256];
230 err = fdt_check_header(fdt);
232 printf("fdt_env: %s\n", fdt_strerror(err));
237 * See if we already have a "u-boot-env" node, delete it if so.
238 * Then create a new empty node.
240 nodeoffset = fdt_path_offset (fdt, "/u-boot-env");
241 if (nodeoffset >= 0) {
242 err = fdt_del_node(fdt, nodeoffset);
244 printf("fdt_env: %s\n", fdt_strerror(err));
249 * Create a new node "/u-boot-env" (offset 0 is root level)
251 nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
252 if (nodeoffset < 0) {
253 printf("WARNING: could not create /u-boot-env %s.\n",
254 fdt_strerror(nodeoffset));
258 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
259 char *s, *lval, *rval;
262 * Find the end of the name=definition
264 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
267 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
268 *s++ = env_get_char(k);
272 * Find the first '=': it separates the name from the value
274 s = strchr(tmpenv, '=');
280 err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
282 printf("WARNING: could not set %s %s.\n",
283 lval, fdt_strerror(err));
289 #endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
291 /********************************************************************/
293 #ifdef CONFIG_OF_HAS_BD_T
295 #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
297 static const struct {
308 #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
309 || defined(CONFIG_E500)
312 #if defined(CONFIG_MPC5xxx)
315 #if defined(CONFIG_MPC83XX)
318 #if defined(CONFIG_MPC8220)
336 #if defined(CONFIG_MPC5xxx)
344 int fdt_bd_t(void *fdt)
349 u32 tmp; /* used to set 32 bit integer properties */
352 err = fdt_check_header(fdt);
354 printf("fdt_bd_t: %s\n", fdt_strerror(err));
359 * See if we already have a "bd_t" node, delete it if so.
360 * Then create a new empty node.
362 nodeoffset = fdt_path_offset (fdt, "/bd_t");
363 if (nodeoffset >= 0) {
364 err = fdt_del_node(fdt, nodeoffset);
366 printf("fdt_bd_t: %s\n", fdt_strerror(err));
371 * Create a new node "/bd_t" (offset 0 is root level)
373 nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
374 if (nodeoffset < 0) {
375 printf("WARNING: could not create /bd_t %s.\n",
376 fdt_strerror(nodeoffset));
377 printf("fdt_bd_t: %s\n", fdt_strerror(nodeoffset));
381 * Use the string/pointer structure to create the entries...
383 for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
384 tmp = cpu_to_be32(getenv("bootargs"));
385 err = fdt_setprop(fdt, nodeoffset,
386 bd_map[i].name, &tmp, sizeof(tmp));
388 printf("WARNING: could not set %s %s.\n",
389 bd_map[i].name, fdt_strerror(err));
392 * Add a couple of oddball entries...
394 err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
396 printf("WARNING: could not set enetaddr %s.\n",
398 err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
400 printf("WARNING: could not set ethspeed %s.\n",
404 #endif /* ifdef CONFIG_OF_HAS_BD_T */
406 void do_fixup_by_path(void *fdt, const char *path, const char *prop,
407 const void *val, int len, int create)
411 debug("Updating property '%s/%s' = ", node, prop);
412 for (i = 0; i < len; i++)
413 debug(" %.2x", *(u8*)(val+i));
416 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
418 printf("Unable to update property %s:%s, err=%s\n",
419 path, prop, fdt_strerror(rc));
422 void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
425 val = cpu_to_fdt32(val);
426 do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
429 void do_fixup_by_prop(void *fdt,
430 const char *pname, const void *pval, int plen,
431 const char *prop, const void *val, int len,
437 debug("Updating property '%s/%s' = ", node, prop);
438 for (i = 0; i < len; i++)
439 debug(" %.2x", *(u8*)(val+i));
442 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
443 while (off != -FDT_ERR_NOTFOUND) {
444 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
445 fdt_setprop(fdt, off, prop, val, len);
446 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
450 void do_fixup_by_prop_u32(void *fdt,
451 const char *pname, const void *pval, int plen,
452 const char *prop, u32 val, int create)
454 val = cpu_to_fdt32(val);
455 do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create);
458 void do_fixup_by_compat(void *fdt, const char *compat,
459 const char *prop, const void *val, int len, int create)
464 debug("Updating property '%s/%s' = ", node, prop);
465 for (i = 0; i < len; i++)
466 debug(" %.2x", *(u8*)(val+i));
469 off = fdt_node_offset_by_compatible(fdt, -1, compat);
470 while (off != -FDT_ERR_NOTFOUND) {
471 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
472 fdt_setprop(fdt, off, prop, val, len);
473 off = fdt_node_offset_by_compatible(fdt, off, compat);
477 void do_fixup_by_compat_u32(void *fdt, const char *compat,
478 const char *prop, u32 val, int create)
480 val = cpu_to_fdt32(val);
481 do_fixup_by_compat(fdt, compat, prop, &val, 4, create);
484 int fdt_fixup_memory(void *blob, u64 start, u64 size)
486 int err, nodeoffset, len = 0;
488 const u32 *addrcell, *sizecell;
490 err = fdt_check_header(blob);
492 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
496 /* update, or add and update /memory node */
497 nodeoffset = fdt_path_offset(blob, "/memory");
498 if (nodeoffset < 0) {
499 nodeoffset = fdt_add_subnode(blob, 0, "memory");
501 printf("WARNING: could not create /memory: %s.\n",
502 fdt_strerror(nodeoffset));
505 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
508 printf("WARNING: could not set %s %s.\n", "device_type",
513 addrcell = fdt_getprop(blob, 0, "#address-cells", NULL);
514 /* use shifts and mask to ensure endianness */
515 if ((addrcell) && (*addrcell == 2)) {
516 tmp[0] = (start >> 56) & 0xff;
517 tmp[1] = (start >> 48) & 0xff;
518 tmp[2] = (start >> 40) & 0xff;
519 tmp[3] = (start >> 32) & 0xff;
520 tmp[4] = (start >> 24) & 0xff;
521 tmp[5] = (start >> 16) & 0xff;
522 tmp[6] = (start >> 8) & 0xff;
523 tmp[7] = (start ) & 0xff;
526 tmp[0] = (start >> 24) & 0xff;
527 tmp[1] = (start >> 16) & 0xff;
528 tmp[2] = (start >> 8) & 0xff;
529 tmp[3] = (start ) & 0xff;
533 sizecell = fdt_getprop(blob, 0, "#size-cells", NULL);
534 /* use shifts and mask to ensure endianness */
535 if ((sizecell) && (*sizecell == 2)) {
536 tmp[0+len] = (size >> 56) & 0xff;
537 tmp[1+len] = (size >> 48) & 0xff;
538 tmp[2+len] = (size >> 40) & 0xff;
539 tmp[3+len] = (size >> 32) & 0xff;
540 tmp[4+len] = (size >> 24) & 0xff;
541 tmp[5+len] = (size >> 16) & 0xff;
542 tmp[6+len] = (size >> 8) & 0xff;
543 tmp[7+len] = (size ) & 0xff;
546 tmp[0+len] = (size >> 24) & 0xff;
547 tmp[1+len] = (size >> 16) & 0xff;
548 tmp[2+len] = (size >> 8) & 0xff;
549 tmp[3+len] = (size ) & 0xff;
553 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
555 printf("WARNING: could not set %s %s.\n",
556 "reg", fdt_strerror(err));
562 #if defined(CONFIG_HAS_ETH0) || defined(CONFIG_HAS_ETH1) ||\
563 defined(CONFIG_HAS_ETH2) || defined(CONFIG_HAS_ETH3)
565 void fdt_fixup_ethernet(void *fdt, bd_t *bd)
570 node = fdt_path_offset(fdt, "/aliases");
572 #if defined(CONFIG_HAS_ETH0)
573 path = fdt_getprop(fdt, node, "ethernet0", NULL);
575 do_fixup_by_path(fdt, path, "mac-address",
576 bd->bi_enetaddr, 6, 0);
577 do_fixup_by_path(fdt, path, "local-mac-address",
578 bd->bi_enetaddr, 6, 1);
581 #if defined(CONFIG_HAS_ETH1)
582 path = fdt_getprop(fdt, node, "ethernet1", NULL);
584 do_fixup_by_path(fdt, path, "mac-address",
585 bd->bi_enet1addr, 6, 0);
586 do_fixup_by_path(fdt, path, "local-mac-address",
587 bd->bi_enet1addr, 6, 1);
590 #if defined(CONFIG_HAS_ETH2)
591 path = fdt_getprop(fdt, node, "ethernet2", NULL);
593 do_fixup_by_path(fdt, path, "mac-address",
594 bd->bi_enet2addr, 6, 0);
595 do_fixup_by_path(fdt, path, "local-mac-address",
596 bd->bi_enet2addr, 6, 1);
599 #if defined(CONFIG_HAS_ETH3)
600 path = fdt_getprop(fdt, node, "ethernet3", NULL);
602 do_fixup_by_path(fdt, path, "mac-address",
603 bd->bi_enet3addr, 6, 0);
604 do_fixup_by_path(fdt, path, "local-mac-address",
605 bd->bi_enet3addr, 6, 1);