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 /********************************************************************/
47 int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
52 u32 tmp; /* used to set 32 bit integer properties */
53 char *str; /* used to set string properties */
55 err = fdt_check_header(fdt);
57 printf("libfdt: %s\n", fdt_strerror(err));
61 if (initrd_start && initrd_end) {
62 struct fdt_reserve_entry re;
67 err = fdt_num_reservemap(fdt, &used, &total);
69 printf("libfdt: %s\n", fdt_strerror(err));
73 printf("WARNING fdt_chosen: "
74 "no room in the reserved map (%d of %d)\n",
79 * Look for an existing entry and update it. If we don't find
80 * the entry, we will j be the next available slot.
82 for (j = 0; j < used; j++) {
83 err = fdt_get_reservemap(fdt, j, &re);
84 if (re.address == initrd_start) {
88 err = fdt_replace_reservemap_entry(fdt, j,
89 initrd_start, initrd_end - initrd_start + 1);
91 printf("libfdt: %s\n", fdt_strerror(err));
97 * Find the "chosen" node.
99 nodeoffset = fdt_find_node_by_path (fdt, "/chosen");
102 * If we have a "chosen" node already the "force the writing"
103 * is not set, our job is done.
105 if ((nodeoffset >= 0) && !force)
109 * No "chosen" node in the blob: create it.
111 if (nodeoffset < 0) {
113 * Create a new node "/chosen" (offset 0 is root level)
115 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
116 if (nodeoffset < 0) {
117 printf("WARNING fdt_chosen: "
118 "could not create the /chosen node (%s).\n",
119 fdt_strerror(nodeoffset));
125 * Update pre-existing properties, create them if non-existant.
127 str = getenv("bootargs");
129 err = fdt_setprop(fdt, nodeoffset,
130 "bootargs", str, strlen(str)+1);
132 printf("WARNING fdt_chosen: "
133 "could not set bootargs (%s).\n",
136 if (initrd_start && initrd_end) {
137 tmp = __cpu_to_be32(initrd_start);
138 err = fdt_setprop(fdt, nodeoffset,
139 "linux,initrd-start", &tmp, sizeof(tmp));
141 printf("WARNING fdt_chosen: "
142 "could not set linux,initrd-start (%s).\n",
144 tmp = __cpu_to_be32(initrd_end);
145 err = fdt_setprop(fdt, nodeoffset,
146 "linux,initrd-end", &tmp, sizeof(tmp));
148 printf("WARNING fdt_chosen: "
149 "could not set linux,initrd-end (%s).\n",
152 #ifdef OF_STDOUT_PATH
153 err = fdt_setprop(fdt, nodeoffset,
154 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
156 printf("WARNING fdt_chosen: "
157 "could not set linux,stdout-path (%s).\n",
164 /********************************************************************/
166 #ifdef CONFIG_OF_HAS_UBOOT_ENV
168 /* Function that returns a character from the environment */
169 extern uchar(*env_get_char) (int);
172 int fdt_env(void *fdt)
178 static char tmpenv[256];
180 err = fdt_check_header(fdt);
182 printf("libfdt: %s\n", fdt_strerror(err));
187 * See if we already have a "u-boot-env" node, delete it if so.
188 * Then create a new empty node.
190 nodeoffset = fdt_find_node_by_path (fdt, "/u-boot-env");
191 if (nodeoffset >= 0) {
192 err = fdt_del_node(fdt, nodeoffset);
194 printf("libfdt: %s\n", fdt_strerror(err));
199 * Create a new node "/u-boot-env" (offset 0 is root level)
201 nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
202 if (nodeoffset < 0) {
203 printf("WARNING fdt_env: "
204 "could not create the /u-boot-env node (%s).\n",
205 fdt_strerror(nodeoffset));
209 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
210 char *s, *lval, *rval;
213 * Find the end of the name=definition
215 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
218 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
219 *s++ = env_get_char(k);
223 * Find the first '=': it separates the name from the value
225 s = strchr(tmpenv, '=');
231 err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
233 printf("WARNING fdt_env: "
234 "could not set %s (%s).\n",
235 lval, fdt_strerror(err));
241 #endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
243 /********************************************************************/
245 #ifdef CONFIG_OF_HAS_BD_T
247 #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
249 static const struct {
260 #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
261 || defined(CONFIG_E500)
264 #if defined(CONFIG_MPC5xxx)
267 #if defined(CONFIG_MPC83XX)
270 #if defined(CONFIG_MPC8220)
288 #if defined(CONFIG_MPC5xxx)
296 int fdt_bd_t(void *fdt)
301 u32 tmp; /* used to set 32 bit integer properties */
304 err = fdt_check_header(fdt);
306 printf("libfdt: %s\n", fdt_strerror(err));
311 * See if we already have a "bd_t" node, delete it if so.
312 * Then create a new empty node.
314 nodeoffset = fdt_find_node_by_path (fdt, "/bd_t");
315 if (nodeoffset >= 0) {
316 err = fdt_del_node(fdt, nodeoffset);
318 printf("libfdt: %s\n", fdt_strerror(err));
323 * Create a new node "/bd_t" (offset 0 is root level)
325 nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
326 if (nodeoffset < 0) {
327 printf("WARNING fdt_bd_t: "
328 "could not create the /bd_t node (%s).\n",
329 fdt_strerror(nodeoffset));
330 printf("libfdt: %s\n", fdt_strerror(nodeoffset));
334 * Use the string/pointer structure to create the entries...
336 for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
337 tmp = cpu_to_be32(getenv("bootargs"));
338 err = fdt_setprop(fdt, nodeoffset,
339 bd_map[i].name, &tmp, sizeof(tmp));
341 printf("WARNING fdt_bd_t: "
342 "could not set %s (%s).\n",
343 bd_map[i].name, fdt_strerror(err));
346 * Add a couple of oddball entries...
348 err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
350 printf("WARNING fdt_bd_t: "
351 "could not set enetaddr (%s).\n",
353 err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
355 printf("WARNING fdt_bd_t: "
356 "could not set ethspeed (%s).\n",
360 #endif /* ifdef CONFIG_OF_HAS_BD_T */
362 #endif /* CONFIG_OF_LIBFDT */