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)
51 u32 tmp; /* used to set 32 bit integer properties */
52 char *str; /* used to set string properties */
54 err = fdt_check_header(fdt);
56 printf("fdt_chosen: %s\n", fdt_strerror(err));
60 if (initrd_start && initrd_end) {
61 struct fdt_reserve_entry re;
66 err = fdt_num_reservemap(fdt, &used, &total);
68 printf("fdt_chosen: %s\n", fdt_strerror(err));
73 "no room in the reserved map (%d of %d)\n",
78 * Look for an existing entry and update it. If we don't find
79 * the entry, we will j be the next available slot.
81 for (j = 0; j < used; j++) {
82 err = fdt_get_reservemap(fdt, j, &re);
83 if (re.address == initrd_start) {
87 err = fdt_replace_reservemap_entry(fdt, j,
88 initrd_start, initrd_end - initrd_start + 1);
90 printf("fdt_chosen: %s\n", fdt_strerror(err));
96 * Find the "chosen" node.
98 nodeoffset = fdt_find_node_by_path (fdt, "/chosen");
101 * If we have a "chosen" node already the "force the writing"
102 * is not set, our job is done.
104 if ((nodeoffset >= 0) && !force)
108 * No "chosen" node in the blob: create it.
110 if (nodeoffset < 0) {
112 * Create a new node "/chosen" (offset 0 is root level)
114 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
115 if (nodeoffset < 0) {
116 printf("WARNING: could not create /chosen %s.\n",
117 fdt_strerror(nodeoffset));
123 * Update pre-existing properties, create them if non-existant.
125 str = getenv("bootargs");
127 err = fdt_setprop(fdt, nodeoffset,
128 "bootargs", str, strlen(str)+1);
130 printf("WARNING: could not set bootargs %s.\n",
133 if (initrd_start && initrd_end) {
134 tmp = __cpu_to_be32(initrd_start);
135 err = fdt_setprop(fdt, nodeoffset,
136 "linux,initrd-start", &tmp, sizeof(tmp));
139 "could not set linux,initrd-start %s.\n",
141 tmp = __cpu_to_be32(initrd_end);
142 err = fdt_setprop(fdt, nodeoffset,
143 "linux,initrd-end", &tmp, sizeof(tmp));
145 printf("WARNING: could not set linux,initrd-end %s.\n",
148 #ifdef OF_STDOUT_PATH
149 err = fdt_setprop(fdt, nodeoffset,
150 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
152 printf("WARNING: could not set linux,stdout-path %s.\n",
159 /********************************************************************/
161 #ifdef CONFIG_OF_HAS_UBOOT_ENV
163 /* Function that returns a character from the environment */
164 extern uchar(*env_get_char) (int);
167 int fdt_env(void *fdt)
173 static char tmpenv[256];
175 err = fdt_check_header(fdt);
177 printf("fdt_env: %s\n", fdt_strerror(err));
182 * See if we already have a "u-boot-env" node, delete it if so.
183 * Then create a new empty node.
185 nodeoffset = fdt_find_node_by_path (fdt, "/u-boot-env");
186 if (nodeoffset >= 0) {
187 err = fdt_del_node(fdt, nodeoffset);
189 printf("fdt_env: %s\n", fdt_strerror(err));
194 * Create a new node "/u-boot-env" (offset 0 is root level)
196 nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
197 if (nodeoffset < 0) {
198 printf("WARNING: could not create /u-boot-env %s.\n",
199 fdt_strerror(nodeoffset));
203 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
204 char *s, *lval, *rval;
207 * Find the end of the name=definition
209 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
212 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
213 *s++ = env_get_char(k);
217 * Find the first '=': it separates the name from the value
219 s = strchr(tmpenv, '=');
225 err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
227 printf("WARNING: could not set %s %s.\n",
228 lval, fdt_strerror(err));
234 #endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
236 /********************************************************************/
238 #ifdef CONFIG_OF_HAS_BD_T
240 #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
242 static const struct {
253 #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
254 || defined(CONFIG_E500)
257 #if defined(CONFIG_MPC5xxx)
260 #if defined(CONFIG_MPC83XX)
263 #if defined(CONFIG_MPC8220)
281 #if defined(CONFIG_MPC5xxx)
289 int fdt_bd_t(void *fdt)
294 u32 tmp; /* used to set 32 bit integer properties */
297 err = fdt_check_header(fdt);
299 printf("fdt_bd_t: %s\n", fdt_strerror(err));
304 * See if we already have a "bd_t" node, delete it if so.
305 * Then create a new empty node.
307 nodeoffset = fdt_find_node_by_path (fdt, "/bd_t");
308 if (nodeoffset >= 0) {
309 err = fdt_del_node(fdt, nodeoffset);
311 printf("fdt_bd_t: %s\n", fdt_strerror(err));
316 * Create a new node "/bd_t" (offset 0 is root level)
318 nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
319 if (nodeoffset < 0) {
320 printf("WARNING: could not create /bd_t %s.\n",
321 fdt_strerror(nodeoffset));
322 printf("fdt_bd_t: %s\n", fdt_strerror(nodeoffset));
326 * Use the string/pointer structure to create the entries...
328 for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
329 tmp = cpu_to_be32(getenv("bootargs"));
330 err = fdt_setprop(fdt, nodeoffset,
331 bd_map[i].name, &tmp, sizeof(tmp));
333 printf("WARNING: could not set %s %s.\n",
334 bd_map[i].name, fdt_strerror(err));
337 * Add a couple of oddball entries...
339 err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
341 printf("WARNING: could not set enetaddr %s.\n",
343 err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
345 printf("WARNING: could not set ethspeed %s.\n",
349 #endif /* ifdef CONFIG_OF_HAS_BD_T */
351 #endif /* CONFIG_OF_LIBFDT */