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 */
116 err = fdt_check_header(fdt);
118 printf("fdt_chosen: %s\n", fdt_strerror(err));
122 if (initrd_start && initrd_end) {
124 int total = fdt_num_mem_rsv(fdt);
128 * Look for an existing entry and update it. If we don't find
129 * the entry, we will j be the next available slot.
131 for (j = 0; j < total; j++) {
132 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
133 if (addr == initrd_start) {
134 fdt_del_mem_rsv(fdt, j);
139 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1);
141 printf("fdt_chosen: %s\n", fdt_strerror(err));
147 * Find the "chosen" node.
149 nodeoffset = fdt_path_offset (fdt, "/chosen");
152 * If there is no "chosen" node in the blob, create it.
154 if (nodeoffset < 0) {
156 * Create a new node "/chosen" (offset 0 is root level)
158 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
159 if (nodeoffset < 0) {
160 printf("WARNING: could not create /chosen %s.\n",
161 fdt_strerror(nodeoffset));
167 * Create /chosen properites that don't exist in the fdt.
168 * If the property exists, update it only if the "force" parameter
171 str = getenv("bootargs");
173 path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
174 if ((path == NULL) || force) {
175 err = fdt_setprop(fdt, nodeoffset,
176 "bootargs", str, strlen(str)+1);
178 printf("WARNING: could not set bootargs %s.\n",
182 if (initrd_start && initrd_end) {
183 path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
184 if ((path == NULL) || force) {
185 tmp = __cpu_to_be32(initrd_start);
186 err = fdt_setprop(fdt, nodeoffset,
187 "linux,initrd-start", &tmp, sizeof(tmp));
190 "could not set linux,initrd-start %s.\n",
192 tmp = __cpu_to_be32(initrd_end);
193 err = fdt_setprop(fdt, nodeoffset,
194 "linux,initrd-end", &tmp, sizeof(tmp));
196 printf("WARNING: could not set linux,initrd-end %s.\n",
201 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
202 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
203 if ((path == NULL) || force)
204 err = fdt_fixup_stdout(fdt, nodeoffset);
207 #ifdef OF_STDOUT_PATH
208 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
209 if ((path == NULL) || force) {
210 err = fdt_setprop(fdt, nodeoffset,
211 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
213 printf("WARNING: could not set linux,stdout-path %s.\n",
221 /********************************************************************/
223 #ifdef CONFIG_OF_HAS_UBOOT_ENV
225 /* Function that returns a character from the environment */
226 extern uchar(*env_get_char) (int);
229 int fdt_env(void *fdt)
235 static char tmpenv[256];
237 err = fdt_check_header(fdt);
239 printf("fdt_env: %s\n", fdt_strerror(err));
244 * See if we already have a "u-boot-env" node, delete it if so.
245 * Then create a new empty node.
247 nodeoffset = fdt_path_offset (fdt, "/u-boot-env");
248 if (nodeoffset >= 0) {
249 err = fdt_del_node(fdt, nodeoffset);
251 printf("fdt_env: %s\n", fdt_strerror(err));
256 * Create a new node "/u-boot-env" (offset 0 is root level)
258 nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
259 if (nodeoffset < 0) {
260 printf("WARNING: could not create /u-boot-env %s.\n",
261 fdt_strerror(nodeoffset));
265 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
266 char *s, *lval, *rval;
269 * Find the end of the name=definition
271 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
274 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
275 *s++ = env_get_char(k);
279 * Find the first '=': it separates the name from the value
281 s = strchr(tmpenv, '=');
287 err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
289 printf("WARNING: could not set %s %s.\n",
290 lval, fdt_strerror(err));
296 #endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
298 /********************************************************************/
300 #ifdef CONFIG_OF_HAS_BD_T
302 #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
304 static const struct {
315 #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
316 || defined(CONFIG_E500)
319 #if defined(CONFIG_MPC5xxx)
322 #if defined(CONFIG_MPC83XX)
325 #if defined(CONFIG_MPC8220)
343 #if defined(CONFIG_MPC5xxx)
351 int fdt_bd_t(void *fdt)
356 u32 tmp; /* used to set 32 bit integer properties */
359 err = fdt_check_header(fdt);
361 printf("fdt_bd_t: %s\n", fdt_strerror(err));
366 * See if we already have a "bd_t" node, delete it if so.
367 * Then create a new empty node.
369 nodeoffset = fdt_path_offset (fdt, "/bd_t");
370 if (nodeoffset >= 0) {
371 err = fdt_del_node(fdt, nodeoffset);
373 printf("fdt_bd_t: %s\n", fdt_strerror(err));
378 * Create a new node "/bd_t" (offset 0 is root level)
380 nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
381 if (nodeoffset < 0) {
382 printf("WARNING: could not create /bd_t %s.\n",
383 fdt_strerror(nodeoffset));
384 printf("fdt_bd_t: %s\n", fdt_strerror(nodeoffset));
388 * Use the string/pointer structure to create the entries...
390 for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
391 tmp = cpu_to_be32(getenv("bootargs"));
392 err = fdt_setprop(fdt, nodeoffset,
393 bd_map[i].name, &tmp, sizeof(tmp));
395 printf("WARNING: could not set %s %s.\n",
396 bd_map[i].name, fdt_strerror(err));
399 * Add a couple of oddball entries...
401 err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
403 printf("WARNING: could not set enetaddr %s.\n",
405 err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
407 printf("WARNING: could not set ethspeed %s.\n",
411 #endif /* ifdef CONFIG_OF_HAS_BD_T */
413 void do_fixup_by_path(void *fdt, const char *path, const char *prop,
414 const void *val, int len, int create)
418 debug("Updating property '%s/%s' = ", path, prop);
419 for (i = 0; i < len; i++)
420 debug(" %.2x", *(u8*)(val+i));
423 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
425 printf("Unable to update property %s:%s, err=%s\n",
426 path, prop, fdt_strerror(rc));
429 void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
432 val = cpu_to_fdt32(val);
433 do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
436 void do_fixup_by_prop(void *fdt,
437 const char *pname, const void *pval, int plen,
438 const char *prop, const void *val, int len,
444 debug("Updating property '%s' = ", prop);
445 for (i = 0; i < len; i++)
446 debug(" %.2x", *(u8*)(val+i));
449 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
450 while (off != -FDT_ERR_NOTFOUND) {
451 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
452 fdt_setprop(fdt, off, prop, val, len);
453 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
457 void do_fixup_by_prop_u32(void *fdt,
458 const char *pname, const void *pval, int plen,
459 const char *prop, u32 val, int create)
461 val = cpu_to_fdt32(val);
462 do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create);
465 void do_fixup_by_compat(void *fdt, const char *compat,
466 const char *prop, const void *val, int len, int create)
471 debug("Updating property '%s' = ", prop);
472 for (i = 0; i < len; i++)
473 debug(" %.2x", *(u8*)(val+i));
476 off = fdt_node_offset_by_compatible(fdt, -1, compat);
477 while (off != -FDT_ERR_NOTFOUND) {
478 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
479 fdt_setprop(fdt, off, prop, val, len);
480 off = fdt_node_offset_by_compatible(fdt, off, compat);
484 void do_fixup_by_compat_u32(void *fdt, const char *compat,
485 const char *prop, u32 val, int create)
487 val = cpu_to_fdt32(val);
488 do_fixup_by_compat(fdt, compat, prop, &val, 4, create);
491 int fdt_fixup_memory(void *blob, u64 start, u64 size)
493 int err, nodeoffset, len = 0;
495 const u32 *addrcell, *sizecell;
497 err = fdt_check_header(blob);
499 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
503 /* update, or add and update /memory node */
504 nodeoffset = fdt_path_offset(blob, "/memory");
505 if (nodeoffset < 0) {
506 nodeoffset = fdt_add_subnode(blob, 0, "memory");
508 printf("WARNING: could not create /memory: %s.\n",
509 fdt_strerror(nodeoffset));
512 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
515 printf("WARNING: could not set %s %s.\n", "device_type",
520 addrcell = fdt_getprop(blob, 0, "#address-cells", NULL);
521 /* use shifts and mask to ensure endianness */
522 if ((addrcell) && (*addrcell == 2)) {
523 tmp[0] = (start >> 56) & 0xff;
524 tmp[1] = (start >> 48) & 0xff;
525 tmp[2] = (start >> 40) & 0xff;
526 tmp[3] = (start >> 32) & 0xff;
527 tmp[4] = (start >> 24) & 0xff;
528 tmp[5] = (start >> 16) & 0xff;
529 tmp[6] = (start >> 8) & 0xff;
530 tmp[7] = (start ) & 0xff;
533 tmp[0] = (start >> 24) & 0xff;
534 tmp[1] = (start >> 16) & 0xff;
535 tmp[2] = (start >> 8) & 0xff;
536 tmp[3] = (start ) & 0xff;
540 sizecell = fdt_getprop(blob, 0, "#size-cells", NULL);
541 /* use shifts and mask to ensure endianness */
542 if ((sizecell) && (*sizecell == 2)) {
543 tmp[0+len] = (size >> 56) & 0xff;
544 tmp[1+len] = (size >> 48) & 0xff;
545 tmp[2+len] = (size >> 40) & 0xff;
546 tmp[3+len] = (size >> 32) & 0xff;
547 tmp[4+len] = (size >> 24) & 0xff;
548 tmp[5+len] = (size >> 16) & 0xff;
549 tmp[6+len] = (size >> 8) & 0xff;
550 tmp[7+len] = (size ) & 0xff;
553 tmp[0+len] = (size >> 24) & 0xff;
554 tmp[1+len] = (size >> 16) & 0xff;
555 tmp[2+len] = (size >> 8) & 0xff;
556 tmp[3+len] = (size ) & 0xff;
560 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
562 printf("WARNING: could not set %s %s.\n",
563 "reg", fdt_strerror(err));
569 #if defined(CONFIG_HAS_ETH0) || defined(CONFIG_HAS_ETH1) ||\
570 defined(CONFIG_HAS_ETH2) || defined(CONFIG_HAS_ETH3)
572 void fdt_fixup_ethernet(void *fdt, bd_t *bd)
577 node = fdt_path_offset(fdt, "/aliases");
579 #if defined(CONFIG_HAS_ETH0)
580 path = fdt_getprop(fdt, node, "ethernet0", NULL);
582 do_fixup_by_path(fdt, path, "mac-address",
583 bd->bi_enetaddr, 6, 0);
584 do_fixup_by_path(fdt, path, "local-mac-address",
585 bd->bi_enetaddr, 6, 1);
588 #if defined(CONFIG_HAS_ETH1)
589 path = fdt_getprop(fdt, node, "ethernet1", NULL);
591 do_fixup_by_path(fdt, path, "mac-address",
592 bd->bi_enet1addr, 6, 0);
593 do_fixup_by_path(fdt, path, "local-mac-address",
594 bd->bi_enet1addr, 6, 1);
597 #if defined(CONFIG_HAS_ETH2)
598 path = fdt_getprop(fdt, node, "ethernet2", NULL);
600 do_fixup_by_path(fdt, path, "mac-address",
601 bd->bi_enet2addr, 6, 0);
602 do_fixup_by_path(fdt, path, "local-mac-address",
603 bd->bi_enet2addr, 6, 1);
606 #if defined(CONFIG_HAS_ETH3)
607 path = fdt_getprop(fdt, node, "ethernet3", NULL);
609 do_fixup_by_path(fdt, path, "mac-address",
610 bd->bi_enet3addr, 6, 0);
611 do_fixup_by_path(fdt, path, "local-mac-address",
612 bd->bi_enet3addr, 6, 1);