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>
28 #ifdef CONFIG_OF_LIBFDT
30 #include <asm/global_data.h>
33 #include <fdt_support.h>
36 * Global data (for the gd->bd)
38 DECLARE_GLOBAL_DATA_PTR;
41 * fdt points to our working device tree.
43 struct fdt_header *fdt;
45 /********************************************************************/
48 * fdt_find_and_setprop: Find a node and set it's property
50 * @fdt: ptr to device tree
52 * @prop: property name
53 * @val: ptr to new value
54 * @len: length of new property value
55 * @create: flag to create the property if it doesn't exist
57 * Convenience function to directly set a property given the path to the node.
59 int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
60 const void *val, int len, int create)
62 int nodeoff = fdt_find_node_by_path(fdt, node);
67 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL))
68 return 0; /* create flag not set; so exit quietly */
70 return fdt_setprop(fdt, nodeoff, prop, val, len);
73 int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
77 u32 tmp; /* used to set 32 bit integer properties */
78 char *str; /* used to set string properties */
80 err = fdt_check_header(fdt);
82 printf("fdt_chosen: %s\n", fdt_strerror(err));
86 if (initrd_start && initrd_end) {
87 struct fdt_reserve_entry re;
92 err = fdt_num_reservemap(fdt, &used, &total);
94 printf("fdt_chosen: %s\n", fdt_strerror(err));
99 "no room in the reserved map (%d of %d)\n",
104 * Look for an existing entry and update it. If we don't find
105 * the entry, we will j be the next available slot.
107 for (j = 0; j < used; j++) {
108 err = fdt_get_reservemap(fdt, j, &re);
109 if (re.address == initrd_start) {
113 err = fdt_replace_reservemap_entry(fdt, j,
114 initrd_start, initrd_end - initrd_start + 1);
116 printf("fdt_chosen: %s\n", fdt_strerror(err));
122 * Find the "chosen" node.
124 nodeoffset = fdt_find_node_by_path (fdt, "/chosen");
127 * If we have a "chosen" node already the "force the writing"
128 * is not set, our job is done.
130 if ((nodeoffset >= 0) && !force)
134 * No "chosen" node in the blob: create it.
136 if (nodeoffset < 0) {
138 * Create a new node "/chosen" (offset 0 is root level)
140 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
141 if (nodeoffset < 0) {
142 printf("WARNING: could not create /chosen %s.\n",
143 fdt_strerror(nodeoffset));
149 * Update pre-existing properties, create them if non-existant.
151 str = getenv("bootargs");
153 err = fdt_setprop(fdt, nodeoffset,
154 "bootargs", str, strlen(str)+1);
156 printf("WARNING: could not set bootargs %s.\n",
159 if (initrd_start && initrd_end) {
160 tmp = __cpu_to_be32(initrd_start);
161 err = fdt_setprop(fdt, nodeoffset,
162 "linux,initrd-start", &tmp, sizeof(tmp));
165 "could not set linux,initrd-start %s.\n",
167 tmp = __cpu_to_be32(initrd_end);
168 err = fdt_setprop(fdt, nodeoffset,
169 "linux,initrd-end", &tmp, sizeof(tmp));
171 printf("WARNING: could not set linux,initrd-end %s.\n",
174 #ifdef OF_STDOUT_PATH
175 err = fdt_setprop(fdt, nodeoffset,
176 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
178 printf("WARNING: could not set linux,stdout-path %s.\n",
185 /********************************************************************/
187 #ifdef CONFIG_OF_HAS_UBOOT_ENV
189 /* Function that returns a character from the environment */
190 extern uchar(*env_get_char) (int);
193 int fdt_env(void *fdt)
199 static char tmpenv[256];
201 err = fdt_check_header(fdt);
203 printf("fdt_env: %s\n", fdt_strerror(err));
208 * See if we already have a "u-boot-env" node, delete it if so.
209 * Then create a new empty node.
211 nodeoffset = fdt_find_node_by_path (fdt, "/u-boot-env");
212 if (nodeoffset >= 0) {
213 err = fdt_del_node(fdt, nodeoffset);
215 printf("fdt_env: %s\n", fdt_strerror(err));
220 * Create a new node "/u-boot-env" (offset 0 is root level)
222 nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
223 if (nodeoffset < 0) {
224 printf("WARNING: could not create /u-boot-env %s.\n",
225 fdt_strerror(nodeoffset));
229 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
230 char *s, *lval, *rval;
233 * Find the end of the name=definition
235 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
238 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
239 *s++ = env_get_char(k);
243 * Find the first '=': it separates the name from the value
245 s = strchr(tmpenv, '=');
251 err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
253 printf("WARNING: could not set %s %s.\n",
254 lval, fdt_strerror(err));
260 #endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
262 /********************************************************************/
264 #ifdef CONFIG_OF_HAS_BD_T
266 #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
268 static const struct {
279 #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
280 || defined(CONFIG_E500)
283 #if defined(CONFIG_MPC5xxx)
286 #if defined(CONFIG_MPC83XX)
289 #if defined(CONFIG_MPC8220)
307 #if defined(CONFIG_MPC5xxx)
315 int fdt_bd_t(void *fdt)
320 u32 tmp; /* used to set 32 bit integer properties */
323 err = fdt_check_header(fdt);
325 printf("fdt_bd_t: %s\n", fdt_strerror(err));
330 * See if we already have a "bd_t" node, delete it if so.
331 * Then create a new empty node.
333 nodeoffset = fdt_find_node_by_path (fdt, "/bd_t");
334 if (nodeoffset >= 0) {
335 err = fdt_del_node(fdt, nodeoffset);
337 printf("fdt_bd_t: %s\n", fdt_strerror(err));
342 * Create a new node "/bd_t" (offset 0 is root level)
344 nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
345 if (nodeoffset < 0) {
346 printf("WARNING: could not create /bd_t %s.\n",
347 fdt_strerror(nodeoffset));
348 printf("fdt_bd_t: %s\n", fdt_strerror(nodeoffset));
352 * Use the string/pointer structure to create the entries...
354 for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
355 tmp = cpu_to_be32(getenv("bootargs"));
356 err = fdt_setprop(fdt, nodeoffset,
357 bd_map[i].name, &tmp, sizeof(tmp));
359 printf("WARNING: could not set %s %s.\n",
360 bd_map[i].name, fdt_strerror(err));
363 * Add a couple of oddball entries...
365 err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
367 printf("WARNING: could not set enetaddr %s.\n",
369 err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
371 printf("WARNING: could not set ethspeed %s.\n",
375 #endif /* ifdef CONFIG_OF_HAS_BD_T */
377 void do_fixup_by_path(void *fdt, const char *path, const char *prop,
378 const void *val, int len, int create)
382 debug("Updating property '%s/%s' = ", node, prop);
383 for (i = 0; i < len; i++)
384 debug(" %.2x", *(u8*)(val+i));
387 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
389 printf("Unable to update property %s:%s, err=%s\n",
390 path, prop, fdt_strerror(rc));
393 void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
396 val = cpu_to_fdt32(val);
397 do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
400 #endif /* CONFIG_OF_LIBFDT */