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 #include "../drivers/qe/qe.h"
37 * Global data (for the gd->bd)
39 DECLARE_GLOBAL_DATA_PTR;
42 * fdt points to our working device tree.
44 struct fdt_header *fdt;
46 /********************************************************************/
49 * fdt_find_and_setprop: Find a node and set it's property
51 * @fdt: ptr to device tree
53 * @prop: property name
54 * @val: ptr to new value
55 * @len: length of new property value
56 * @create: flag to create the property if it doesn't exist
58 * Convenience function to directly set a property given the path to the node.
60 int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
61 const void *val, int len, int create)
63 int nodeoff = fdt_path_offset(fdt, node);
68 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL))
69 return 0; /* create flag not set; so exit quietly */
71 return fdt_setprop(fdt, nodeoff, prop, val, len);
74 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
75 static int fdt_fixup_stdout(void *fdt, int choosenoff)
78 #ifdef CONFIG_CONS_INDEX
80 char sername[9] = { 0 };
83 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
85 err = node = fdt_path_offset(fdt, "/aliases");
88 path = fdt_getprop(fdt, node, sername, &len);
90 char *p = malloc(len);
91 err = -FDT_ERR_NOSPACE;
94 err = fdt_setprop(fdt, choosenoff,
95 "linux,stdout-path", p, len);
104 printf("WARNING: could not set linux,stdout-path %s.\n",
111 int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
115 u32 tmp; /* used to set 32 bit integer properties */
116 char *str; /* used to set string properties */
119 err = fdt_check_header(fdt);
121 printf("fdt_chosen: %s\n", fdt_strerror(err));
125 if (initrd_start && initrd_end) {
127 int total = fdt_num_mem_rsv(fdt);
131 * Look for an existing entry and update it. If we don't find
132 * the entry, we will j be the next available slot.
134 for (j = 0; j < total; j++) {
135 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
136 if (addr == initrd_start) {
137 fdt_del_mem_rsv(fdt, j);
142 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1);
144 printf("fdt_chosen: %s\n", fdt_strerror(err));
150 * Find the "chosen" node.
152 nodeoffset = fdt_path_offset (fdt, "/chosen");
155 * If there is no "chosen" node in the blob, create it.
157 if (nodeoffset < 0) {
159 * Create a new node "/chosen" (offset 0 is root level)
161 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
162 if (nodeoffset < 0) {
163 printf("WARNING: could not create /chosen %s.\n",
164 fdt_strerror(nodeoffset));
170 * Create /chosen properites that don't exist in the fdt.
171 * If the property exists, update it only if the "force" parameter
174 str = getenv("bootargs");
176 path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
177 if ((path == NULL) || force) {
178 err = fdt_setprop(fdt, nodeoffset,
179 "bootargs", str, strlen(str)+1);
181 printf("WARNING: could not set bootargs %s.\n",
185 if (initrd_start && initrd_end) {
186 path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
187 if ((path == NULL) || force) {
188 tmp = __cpu_to_be32(initrd_start);
189 err = fdt_setprop(fdt, nodeoffset,
190 "linux,initrd-start", &tmp, sizeof(tmp));
193 "could not set linux,initrd-start %s.\n",
195 tmp = __cpu_to_be32(initrd_end);
196 err = fdt_setprop(fdt, nodeoffset,
197 "linux,initrd-end", &tmp, sizeof(tmp));
199 printf("WARNING: could not set linux,initrd-end %s.\n",
204 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
205 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
206 if ((path == NULL) || force)
207 err = fdt_fixup_stdout(fdt, nodeoffset);
210 #ifdef OF_STDOUT_PATH
211 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
212 if ((path == NULL) || force) {
213 err = fdt_setprop(fdt, nodeoffset,
214 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
216 printf("WARNING: could not set linux,stdout-path %s.\n",
224 /********************************************************************/
226 #ifdef CONFIG_OF_HAS_UBOOT_ENV
228 /* Function that returns a character from the environment */
229 extern uchar(*env_get_char) (int);
232 int fdt_env(void *fdt)
238 static char tmpenv[256];
240 err = fdt_check_header(fdt);
242 printf("fdt_env: %s\n", fdt_strerror(err));
247 * See if we already have a "u-boot-env" node, delete it if so.
248 * Then create a new empty node.
250 nodeoffset = fdt_path_offset (fdt, "/u-boot-env");
251 if (nodeoffset >= 0) {
252 err = fdt_del_node(fdt, nodeoffset);
254 printf("fdt_env: %s\n", fdt_strerror(err));
259 * Create a new node "/u-boot-env" (offset 0 is root level)
261 nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
262 if (nodeoffset < 0) {
263 printf("WARNING: could not create /u-boot-env %s.\n",
264 fdt_strerror(nodeoffset));
268 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
269 char *s, *lval, *rval;
272 * Find the end of the name=definition
274 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
277 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
278 *s++ = env_get_char(k);
282 * Find the first '=': it separates the name from the value
284 s = strchr(tmpenv, '=');
290 err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
292 printf("WARNING: could not set %s %s.\n",
293 lval, fdt_strerror(err));
299 #endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
301 /********************************************************************/
303 #ifdef CONFIG_OF_HAS_BD_T
305 #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
307 static const struct {
318 #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
319 || defined(CONFIG_E500)
322 #if defined(CONFIG_MPC5xxx)
325 #if defined(CONFIG_MPC83XX)
328 #if defined(CONFIG_MPC8220)
346 #if defined(CONFIG_MPC5xxx)
354 int fdt_bd_t(void *fdt)
359 u32 tmp; /* used to set 32 bit integer properties */
362 err = fdt_check_header(fdt);
364 printf("fdt_bd_t: %s\n", fdt_strerror(err));
369 * See if we already have a "bd_t" node, delete it if so.
370 * Then create a new empty node.
372 nodeoffset = fdt_path_offset (fdt, "/bd_t");
373 if (nodeoffset >= 0) {
374 err = fdt_del_node(fdt, nodeoffset);
376 printf("fdt_bd_t: %s\n", fdt_strerror(err));
381 * Create a new node "/bd_t" (offset 0 is root level)
383 nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
384 if (nodeoffset < 0) {
385 printf("WARNING: could not create /bd_t %s.\n",
386 fdt_strerror(nodeoffset));
387 printf("fdt_bd_t: %s\n", fdt_strerror(nodeoffset));
391 * Use the string/pointer structure to create the entries...
393 for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
394 tmp = cpu_to_be32(getenv("bootargs"));
395 err = fdt_setprop(fdt, nodeoffset,
396 bd_map[i].name, &tmp, sizeof(tmp));
398 printf("WARNING: could not set %s %s.\n",
399 bd_map[i].name, fdt_strerror(err));
402 * Add a couple of oddball entries...
404 err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
406 printf("WARNING: could not set enetaddr %s.\n",
408 err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
410 printf("WARNING: could not set ethspeed %s.\n",
414 #endif /* ifdef CONFIG_OF_HAS_BD_T */
416 void do_fixup_by_path(void *fdt, const char *path, const char *prop,
417 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 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
428 printf("Unable to update property %s:%s, err=%s\n",
429 path, prop, fdt_strerror(rc));
432 void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
435 val = cpu_to_fdt32(val);
436 do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
439 void do_fixup_by_prop(void *fdt,
440 const char *pname, const void *pval, int plen,
441 const char *prop, const void *val, int len,
447 debug("Updating property '%s/%s' = ", node, prop);
448 for (i = 0; i < len; i++)
449 debug(" %.2x", *(u8*)(val+i));
452 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
453 while (off != -FDT_ERR_NOTFOUND) {
454 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
455 fdt_setprop(fdt, off, prop, val, len);
456 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
460 void do_fixup_by_prop_u32(void *fdt,
461 const char *pname, const void *pval, int plen,
462 const char *prop, u32 val, int create)
464 val = cpu_to_fdt32(val);
465 do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create);
468 void do_fixup_by_compat(void *fdt, const char *compat,
469 const char *prop, const void *val, int len, int create)
474 debug("Updating property '%s/%s' = ", node, prop);
475 for (i = 0; i < len; i++)
476 debug(" %.2x", *(u8*)(val+i));
479 off = fdt_node_offset_by_compatible(fdt, -1, compat);
480 while (off != -FDT_ERR_NOTFOUND) {
481 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
482 fdt_setprop(fdt, off, prop, val, len);
483 off = fdt_node_offset_by_compatible(fdt, off, compat);
487 void do_fixup_by_compat_u32(void *fdt, const char *compat,
488 const char *prop, u32 val, int create)
490 val = cpu_to_fdt32(val);
491 do_fixup_by_compat(fdt, compat, prop, &val, 4, create);
494 int fdt_fixup_memory(void *blob, u64 start, u64 size)
496 int err, nodeoffset, len = 0;
498 const u32 *addrcell, *sizecell;
500 err = fdt_check_header(blob);
502 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
506 /* update, or add and update /memory node */
507 nodeoffset = fdt_path_offset(blob, "/memory");
508 if (nodeoffset < 0) {
509 nodeoffset = fdt_add_subnode(blob, 0, "memory");
511 printf("WARNING: could not create /memory: %s.\n",
512 fdt_strerror(nodeoffset));
515 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
518 printf("WARNING: could not set %s %s.\n", "device_type",
523 addrcell = fdt_getprop(blob, 0, "#address-cells", NULL);
524 /* use shifts and mask to ensure endianness */
525 if ((addrcell) && (*addrcell == 2)) {
526 tmp[0] = (start >> 56) & 0xff;
527 tmp[1] = (start >> 48) & 0xff;
528 tmp[2] = (start >> 40) & 0xff;
529 tmp[3] = (start >> 32) & 0xff;
530 tmp[4] = (start >> 24) & 0xff;
531 tmp[5] = (start >> 16) & 0xff;
532 tmp[6] = (start >> 8) & 0xff;
533 tmp[7] = (start ) & 0xff;
536 tmp[0] = (start >> 24) & 0xff;
537 tmp[1] = (start >> 16) & 0xff;
538 tmp[2] = (start >> 8) & 0xff;
539 tmp[3] = (start ) & 0xff;
543 sizecell = fdt_getprop(blob, 0, "#size-cells", NULL);
544 /* use shifts and mask to ensure endianness */
545 if ((sizecell) && (*sizecell == 2)) {
546 tmp[0+len] = (size >> 56) & 0xff;
547 tmp[1+len] = (size >> 48) & 0xff;
548 tmp[2+len] = (size >> 40) & 0xff;
549 tmp[3+len] = (size >> 32) & 0xff;
550 tmp[4+len] = (size >> 24) & 0xff;
551 tmp[5+len] = (size >> 16) & 0xff;
552 tmp[6+len] = (size >> 8) & 0xff;
553 tmp[7+len] = (size ) & 0xff;
556 tmp[0+len] = (size >> 24) & 0xff;
557 tmp[1+len] = (size >> 16) & 0xff;
558 tmp[2+len] = (size >> 8) & 0xff;
559 tmp[3+len] = (size ) & 0xff;
563 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
565 printf("WARNING: could not set %s %s.\n",
566 "reg", fdt_strerror(err));
572 #if defined(CONFIG_HAS_ETH0) || defined(CONFIG_HAS_ETH1) ||\
573 defined(CONFIG_HAS_ETH2) || defined(CONFIG_HAS_ETH3)
575 void fdt_fixup_ethernet(void *fdt, bd_t *bd)
580 node = fdt_path_offset(fdt, "/aliases");
582 #if defined(CONFIG_HAS_ETH0)
583 path = fdt_getprop(fdt, node, "ethernet0", NULL);
585 do_fixup_by_path(fdt, path, "mac-address",
586 bd->bi_enetaddr, 6, 0);
587 do_fixup_by_path(fdt, path, "local-mac-address",
588 bd->bi_enetaddr, 6, 1);
591 #if defined(CONFIG_HAS_ETH1)
592 path = fdt_getprop(fdt, node, "ethernet1", NULL);
594 do_fixup_by_path(fdt, path, "mac-address",
595 bd->bi_enet1addr, 6, 0);
596 do_fixup_by_path(fdt, path, "local-mac-address",
597 bd->bi_enet1addr, 6, 1);
600 #if defined(CONFIG_HAS_ETH2)
601 path = fdt_getprop(fdt, node, "ethernet2", NULL);
603 do_fixup_by_path(fdt, path, "mac-address",
604 bd->bi_enet2addr, 6, 0);
605 do_fixup_by_path(fdt, path, "local-mac-address",
606 bd->bi_enet2addr, 6, 1);
609 #if defined(CONFIG_HAS_ETH3)
610 path = fdt_getprop(fdt, node, "ethernet3", NULL);
612 do_fixup_by_path(fdt, path, "mac-address",
613 bd->bi_enet3addr, 6, 0);
614 do_fixup_by_path(fdt, path, "local-mac-address",
615 bd->bi_enet3addr, 6, 1);
623 * If a QE firmware has been uploaded, then add the 'firmware' node under
626 void fdt_fixup_qe_firmware(void *fdt)
628 struct qe_firmware_info *qe_fw_info;
631 qe_fw_info = qe_get_firmware_info();
635 node = fdt_path_offset(fdt, "/qe");
639 /* We assume the node doesn't exist yet */
640 node = fdt_add_subnode(fdt, node, "firmware");
644 ret = fdt_setprop(fdt, node, "extended-modes",
645 &qe_fw_info->extended_modes, sizeof(u64));
649 ret = fdt_setprop_string(fdt, node, "id", qe_fw_info->id);
653 ret = fdt_setprop(fdt, node, "virtual-traps", qe_fw_info->vtraps,
654 sizeof(qe_fw_info->vtraps));
661 fdt_del_node(fdt, node);