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 /********************************************************************/
43 int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
48 u32 tmp; /* used to set 32 bit integer properties */
49 char *str; /* used to set string properties */
52 err = fdt_check_header(fdt);
54 printf("libfdt: %s\n", fdt_strerror(err));
58 if (initrd_start && initrd_end) {
59 struct fdt_reserve_entry re;
64 err = fdt_num_reservemap(fdt, &used, &total);
66 printf("libfdt: %s\n", fdt_strerror(err));
70 printf("fdt_chosen: no room in the reserved map (%d of %d)\n",
75 * Look for an existing entry and update it. If we don't find
76 * the entry, we will j be the next available slot.
78 for (j = 0; j < used; j++) {
79 err = fdt_get_reservemap(fdt, j, &re);
80 if (re.address == initrd_start) {
84 err = fdt_replace_reservemap_entry(fdt, j,
85 initrd_start, initrd_end - initrd_start + 1);
87 printf("libfdt: %s\n", fdt_strerror(err));
93 * Find the "chosen" node.
95 nodeoffset = fdt_path_offset (fdt, "/chosen");
98 * If we have a "chosen" node already the "force the writing"
99 * is not set, our job is done.
101 if ((nodeoffset >= 0) && !force)
105 * No "chosen" node in the blob: create it.
107 if (nodeoffset < 0) {
109 * Create a new node "/chosen" (offset 0 is root level)
111 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
112 if (nodeoffset < 0) {
113 printf("libfdt: %s\n", fdt_strerror(nodeoffset));
119 * Update pre-existing properties, create them if non-existant.
121 str = getenv("bootargs");
123 err = fdt_setprop(fdt, nodeoffset, "bootargs", str, strlen(str)+1);
125 printf("libfdt: %s\n", fdt_strerror(err));
127 if (initrd_start && initrd_end) {
128 tmp = __cpu_to_be32(initrd_start);
129 err = fdt_setprop(fdt, nodeoffset, "linux,initrd-start", &tmp, sizeof(tmp));
131 printf("libfdt: %s\n", fdt_strerror(err));
132 tmp = __cpu_to_be32(initrd_end);
133 err = fdt_setprop(fdt, nodeoffset, "linux,initrd-end", &tmp, sizeof(tmp));
135 printf("libfdt: %s\n", fdt_strerror(err));
137 #ifdef OF_STDOUT_PATH
138 err = fdt_setprop(fdt, nodeoffset, "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
140 printf("libfdt: %s\n", fdt_strerror(err));
143 nodeoffset = fdt_path_offset (fdt, "/cpus");
144 if (nodeoffset >= 0) {
145 clock = cpu_to_be32(bd->bi_intfreq);
146 err = fdt_setprop(fdt, nodeoffset, "clock-frequency", &clock, 4);
148 printf("libfdt: %s\n", fdt_strerror(err));
151 nodeoffset = fdt_path_offset (fdt, "/cpus/" OF_CPU "/timebase-frequency");
152 if (nodeoffset >= 0) {
153 clock = cpu_to_be32(OF_TBCLK);
154 err = fdt_setprop(fdt, nodeoffset, "clock-frequency", &clock, 4);
156 printf("libfdt: %s\n", fdt_strerror(err));
162 /********************************************************************/
164 #ifdef CONFIG_OF_HAS_UBOOT_ENV
166 /* Function that returns a character from the environment */
167 extern uchar(*env_get_char) (int);
170 int fdt_env(void *fdt)
176 static char tmpenv[256];
178 err = fdt_check_header(fdt);
180 printf("libfdt: %s\n", fdt_strerror(err));
185 * See if we already have a "u-boot-env" node, delete it if so.
186 * Then create a new empty node.
188 nodeoffset = fdt_path_offset (fdt, "/u-boot-env");
189 if (nodeoffset >= 0) {
190 err = fdt_del_node(fdt, nodeoffset);
192 printf("libfdt: %s\n", fdt_strerror(err));
197 * Create a new node "/u-boot-env" (offset 0 is root level)
199 nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
200 if (nodeoffset < 0) {
201 printf("libfdt: %s\n", fdt_strerror(nodeoffset));
205 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
206 char *s, *lval, *rval;
209 * Find the end of the name=definition
211 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
214 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
215 *s++ = env_get_char(k);
219 * Find the first '=': it separates the name from the value
221 s = strchr(tmpenv, '=');
227 err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
229 printf("libfdt: %s\n", fdt_strerror(err));
235 #endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
237 /********************************************************************/
239 #ifdef CONFIG_OF_HAS_BD_T
241 #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
243 static const struct {
254 #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
255 || defined(CONFIG_E500)
258 #if defined(CONFIG_MPC5xxx)
261 #if defined(CONFIG_MPC83XX)
264 #if defined(CONFIG_MPC8220)
282 #if defined(CONFIG_MPC5xxx)
290 int fdt_bd_t(void *fdt)
295 u32 tmp; /* used to set 32 bit integer properties */
298 err = fdt_check_header(fdt);
300 printf("libfdt: %s\n", fdt_strerror(err));
305 * See if we already have a "bd_t" node, delete it if so.
306 * Then create a new empty node.
308 nodeoffset = fdt_path_offset (fdt, "/bd_t");
309 if (nodeoffset >= 0) {
310 err = fdt_del_node(fdt, nodeoffset);
312 printf("libfdt: %s\n", fdt_strerror(err));
317 * Create a new node "/bd_t" (offset 0 is root level)
319 nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
320 if (nodeoffset < 0) {
321 printf("libfdt: %s\n", fdt_strerror(nodeoffset));
325 * Use the string/pointer structure to create the entries...
327 for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
328 tmp = cpu_to_be32(getenv("bootargs"));
329 err = fdt_setprop(fdt, nodeoffset, bd_map[i].name, &tmp, sizeof(tmp));
331 printf("libfdt: %s\n", fdt_strerror(err));
334 * Add a couple of oddball entries...
336 err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
338 printf("libfdt: %s\n", fdt_strerror(err));
339 err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
341 printf("libfdt: %s\n", fdt_strerror(err));
345 #endif /* ifdef CONFIG_OF_HAS_BD_T */
347 #endif /* CONFIG_OF_LIBFDT */