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>
33 * Global data (for the gd->bd)
35 DECLARE_GLOBAL_DATA_PTR;
38 * fdt points to our working device tree.
40 struct fdt_header *fdt;
42 /********************************************************************/
45 * fdt_find_and_setprop: Find a node and set it's property
47 * @fdt: ptr to device tree
49 * @prop: property name
50 * @val: ptr to new value
51 * @len: length of new property value
52 * @create: flag to create the property if it doesn't exist
54 * Convenience function to directly set a property given the path to the node.
56 int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
57 const void *val, int len, int create)
59 int nodeoff = fdt_path_offset(fdt, node);
64 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL))
65 return 0; /* create flag not set; so exit quietly */
67 return fdt_setprop(fdt, nodeoff, prop, val, len);
70 int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
74 u32 tmp; /* used to set 32 bit integer properties */
75 char *str; /* used to set string properties */
77 err = fdt_check_header(fdt);
79 printf("fdt_chosen: %s\n", fdt_strerror(err));
83 if (initrd_start && initrd_end) {
85 int total = fdt_num_mem_rsv(fdt);
89 * Look for an existing entry and update it. If we don't find
90 * the entry, we will j be the next available slot.
92 for (j = 0; j < total; j++) {
93 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
94 if (addr == initrd_start) {
95 fdt_del_mem_rsv(fdt, j);
100 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1);
102 printf("fdt_chosen: %s\n", fdt_strerror(err));
108 * Find the "chosen" node.
110 nodeoffset = fdt_path_offset (fdt, "/chosen");
113 * If we have a "chosen" node already the "force the writing"
114 * is not set, our job is done.
116 if ((nodeoffset >= 0) && !force)
120 * No "chosen" node in the blob: create it.
122 if (nodeoffset < 0) {
124 * Create a new node "/chosen" (offset 0 is root level)
126 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
127 if (nodeoffset < 0) {
128 printf("WARNING: could not create /chosen %s.\n",
129 fdt_strerror(nodeoffset));
135 * Update pre-existing properties, create them if non-existant.
137 str = getenv("bootargs");
139 err = fdt_setprop(fdt, nodeoffset,
140 "bootargs", str, strlen(str)+1);
142 printf("WARNING: could not set bootargs %s.\n",
145 if (initrd_start && initrd_end) {
146 tmp = __cpu_to_be32(initrd_start);
147 err = fdt_setprop(fdt, nodeoffset,
148 "linux,initrd-start", &tmp, sizeof(tmp));
151 "could not set linux,initrd-start %s.\n",
153 tmp = __cpu_to_be32(initrd_end);
154 err = fdt_setprop(fdt, nodeoffset,
155 "linux,initrd-end", &tmp, sizeof(tmp));
157 printf("WARNING: could not set linux,initrd-end %s.\n",
160 #ifdef OF_STDOUT_PATH
161 err = fdt_setprop(fdt, nodeoffset,
162 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
164 printf("WARNING: could not set linux,stdout-path %s.\n",
171 /********************************************************************/
173 #ifdef CONFIG_OF_HAS_UBOOT_ENV
175 /* Function that returns a character from the environment */
176 extern uchar(*env_get_char) (int);
179 int fdt_env(void *fdt)
185 static char tmpenv[256];
187 err = fdt_check_header(fdt);
189 printf("fdt_env: %s\n", fdt_strerror(err));
194 * See if we already have a "u-boot-env" node, delete it if so.
195 * Then create a new empty node.
197 nodeoffset = fdt_path_offset (fdt, "/u-boot-env");
198 if (nodeoffset >= 0) {
199 err = fdt_del_node(fdt, nodeoffset);
201 printf("fdt_env: %s\n", fdt_strerror(err));
206 * Create a new node "/u-boot-env" (offset 0 is root level)
208 nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
209 if (nodeoffset < 0) {
210 printf("WARNING: could not create /u-boot-env %s.\n",
211 fdt_strerror(nodeoffset));
215 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
216 char *s, *lval, *rval;
219 * Find the end of the name=definition
221 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
224 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
225 *s++ = env_get_char(k);
229 * Find the first '=': it separates the name from the value
231 s = strchr(tmpenv, '=');
237 err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
239 printf("WARNING: could not set %s %s.\n",
240 lval, fdt_strerror(err));
246 #endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
248 /********************************************************************/
250 #ifdef CONFIG_OF_HAS_BD_T
252 #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
254 static const struct {
265 #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
266 || defined(CONFIG_E500)
269 #if defined(CONFIG_MPC5xxx)
272 #if defined(CONFIG_MPC83XX)
275 #if defined(CONFIG_MPC8220)
293 #if defined(CONFIG_MPC5xxx)
301 int fdt_bd_t(void *fdt)
306 u32 tmp; /* used to set 32 bit integer properties */
309 err = fdt_check_header(fdt);
311 printf("fdt_bd_t: %s\n", fdt_strerror(err));
316 * See if we already have a "bd_t" node, delete it if so.
317 * Then create a new empty node.
319 nodeoffset = fdt_path_offset (fdt, "/bd_t");
320 if (nodeoffset >= 0) {
321 err = fdt_del_node(fdt, nodeoffset);
323 printf("fdt_bd_t: %s\n", fdt_strerror(err));
328 * Create a new node "/bd_t" (offset 0 is root level)
330 nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
331 if (nodeoffset < 0) {
332 printf("WARNING: could not create /bd_t %s.\n",
333 fdt_strerror(nodeoffset));
334 printf("fdt_bd_t: %s\n", fdt_strerror(nodeoffset));
338 * Use the string/pointer structure to create the entries...
340 for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
341 tmp = cpu_to_be32(getenv("bootargs"));
342 err = fdt_setprop(fdt, nodeoffset,
343 bd_map[i].name, &tmp, sizeof(tmp));
345 printf("WARNING: could not set %s %s.\n",
346 bd_map[i].name, fdt_strerror(err));
349 * Add a couple of oddball entries...
351 err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
353 printf("WARNING: could not set enetaddr %s.\n",
355 err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
357 printf("WARNING: could not set ethspeed %s.\n",
361 #endif /* ifdef CONFIG_OF_HAS_BD_T */
363 void do_fixup_by_path(void *fdt, const char *path, const char *prop,
364 const void *val, int len, int create)
368 debug("Updating property '%s/%s' = ", node, prop);
369 for (i = 0; i < len; i++)
370 debug(" %.2x", *(u8*)(val+i));
373 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
375 printf("Unable to update property %s:%s, err=%s\n",
376 path, prop, fdt_strerror(rc));
379 void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
382 val = cpu_to_fdt32(val);
383 do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
386 void do_fixup_by_prop(void *fdt,
387 const char *pname, const void *pval, int plen,
388 const char *prop, const void *val, int len,
394 debug("Updating property '%s/%s' = ", node, prop);
395 for (i = 0; i < len; i++)
396 debug(" %.2x", *(u8*)(val+i));
399 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
400 while (off != -FDT_ERR_NOTFOUND) {
401 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
402 fdt_setprop(fdt, off, prop, val, len);
403 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
407 void do_fixup_by_prop_u32(void *fdt,
408 const char *pname, const void *pval, int plen,
409 const char *prop, u32 val, int create)
411 val = cpu_to_fdt32(val);
412 do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create);
415 void do_fixup_by_compat(void *fdt, const char *compat,
416 const char *prop, const void *val, int len, int create)
421 debug("Updating property '%s/%s' = ", node, prop);
422 for (i = 0; i < len; i++)
423 debug(" %.2x", *(u8*)(val+i));
426 off = fdt_node_offset_by_compatible(fdt, -1, compat);
427 while (off != -FDT_ERR_NOTFOUND) {
428 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
429 fdt_setprop(fdt, off, prop, val, len);
430 off = fdt_node_offset_by_compatible(fdt, off, compat);
434 void do_fixup_by_compat_u32(void *fdt, const char *compat,
435 const char *prop, u32 val, int create)
437 val = cpu_to_fdt32(val);
438 do_fixup_by_compat(fdt, compat, prop, &val, 4, create);
441 int fdt_fixup_memory(void *blob, u64 start, u64 size)
443 int err, nodeoffset, len = 0;
445 const u32 *addrcell, *sizecell;
447 err = fdt_check_header(blob);
449 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
453 /* update, or add and update /memory node */
454 nodeoffset = fdt_path_offset(blob, "/memory");
455 if (nodeoffset < 0) {
456 nodeoffset = fdt_add_subnode(blob, 0, "memory");
458 printf("WARNING: could not create /memory: %s.\n",
459 fdt_strerror(nodeoffset));
462 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
465 printf("WARNING: could not set %s %s.\n", "device_type",
470 addrcell = fdt_getprop(blob, 0, "#address-cells", NULL);
471 /* use shifts and mask to ensure endianness */
472 if ((addrcell) && (*addrcell == 2)) {
473 tmp[0] = (start >> 56) & 0xff;
474 tmp[1] = (start >> 48) & 0xff;
475 tmp[2] = (start >> 40) & 0xff;
476 tmp[3] = (start >> 32) & 0xff;
477 tmp[4] = (start >> 24) & 0xff;
478 tmp[5] = (start >> 16) & 0xff;
479 tmp[6] = (start >> 8) & 0xff;
480 tmp[7] = (start ) & 0xff;
483 tmp[0] = (start >> 24) & 0xff;
484 tmp[1] = (start >> 16) & 0xff;
485 tmp[2] = (start >> 8) & 0xff;
486 tmp[3] = (start ) & 0xff;
490 sizecell = fdt_getprop(blob, 0, "#size-cells", NULL);
491 /* use shifts and mask to ensure endianness */
492 if ((sizecell) && (*sizecell == 2)) {
493 tmp[0+len] = (size >> 56) & 0xff;
494 tmp[1+len] = (size >> 48) & 0xff;
495 tmp[2+len] = (size >> 40) & 0xff;
496 tmp[3+len] = (size >> 32) & 0xff;
497 tmp[4+len] = (size >> 24) & 0xff;
498 tmp[5+len] = (size >> 16) & 0xff;
499 tmp[6+len] = (size >> 8) & 0xff;
500 tmp[7+len] = (size ) & 0xff;
503 tmp[0+len] = (size >> 24) & 0xff;
504 tmp[1+len] = (size >> 16) & 0xff;
505 tmp[2+len] = (size >> 8) & 0xff;
506 tmp[3+len] = (size ) & 0xff;
510 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
512 printf("WARNING: could not set %s %s.\n",
513 "reg", fdt_strerror(err));
519 void fdt_fixup_ethernet(void *fdt, bd_t *bd)
524 node = fdt_path_offset(fdt, "/aliases");
526 #if defined(CONFIG_HAS_ETH0)
527 path = fdt_getprop(fdt, node, "ethernet0", NULL);
529 do_fixup_by_path(fdt, path, "mac-address",
530 bd->bi_enetaddr, 6, 0);
531 do_fixup_by_path(fdt, path, "local-mac-address",
532 bd->bi_enetaddr, 6, 1);
535 #if defined(CONFIG_HAS_ETH1)
536 path = fdt_getprop(fdt, node, "ethernet1", NULL);
538 do_fixup_by_path(fdt, path, "mac-address",
539 bd->bi_enet1addr, 6, 0);
540 do_fixup_by_path(fdt, path, "local-mac-address",
541 bd->bi_enet1addr, 6, 1);
544 #if defined(CONFIG_HAS_ETH2)
545 path = fdt_getprop(fdt, node, "ethernet2", NULL);
547 do_fixup_by_path(fdt, path, "mac-address",
548 bd->bi_enet2addr, 6, 0);
549 do_fixup_by_path(fdt, path, "local-mac-address",
550 bd->bi_enet2addr, 6, 1);
553 #if defined(CONFIG_HAS_ETH3)
554 path = fdt_getprop(fdt, node, "ethernet3", NULL);
556 do_fixup_by_path(fdt, path, "mac-address",
557 bd->bi_enet3addr, 6, 0);
558 do_fixup_by_path(fdt, path, "local-mac-address",
559 bd->bi_enet3addr, 6, 1);