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;
46 /********************************************************************/
48 int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
53 u32 tmp; /* used to set 32 bit integer properties */
54 char *str; /* used to set string properties */
57 err = fdt_check_header(fdt);
59 printf("libfdt: %s\n", fdt_strerror(err));
63 if (initrd_start && initrd_end) {
64 struct fdt_reserve_entry re;
69 err = fdt_num_reservemap(fdt, &used, &total);
71 printf("libfdt: %s\n", fdt_strerror(err));
75 printf("WARNING fdt_chosen: no room in the reserved map (%d of %d)\n",
80 * Look for an existing entry and update it. If we don't find
81 * the entry, we will j be the next available slot.
83 for (j = 0; j < used; j++) {
84 err = fdt_get_reservemap(fdt, j, &re);
85 if (re.address == initrd_start) {
89 err = fdt_replace_reservemap_entry(fdt, j,
90 initrd_start, initrd_end - initrd_start + 1);
92 printf("libfdt: %s\n", fdt_strerror(err));
98 * Find the "chosen" node.
100 nodeoffset = fdt_path_offset (fdt, "/chosen");
103 * If we have a "chosen" node already the "force the writing"
104 * is not set, our job is done.
106 if ((nodeoffset >= 0) && !force)
110 * No "chosen" node in the blob: create it.
112 if (nodeoffset < 0) {
114 * Create a new node "/chosen" (offset 0 is root level)
116 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
117 if (nodeoffset < 0) {
118 printf("WARNING fdt_chosen: could not create the \"/chosen node\" (libfdt error %s).\n", fdt_strerror(nodeoffset));
124 * Update pre-existing properties, create them if non-existant.
126 str = getenv("bootargs");
128 err = fdt_setprop(fdt, nodeoffset, "bootargs", str, strlen(str)+1);
130 printf("WARNING fdt_chosen: could not set \"bootargs\" (libfdt error %s).\n", fdt_strerror(err));
132 if (initrd_start && initrd_end) {
133 tmp = __cpu_to_be32(initrd_start);
134 err = fdt_setprop(fdt, nodeoffset, "linux,initrd-start", &tmp, sizeof(tmp));
136 printf("WARNING fdt_chosen: could not set \"linux,initrd-start\" (libfdt error %s).\n", fdt_strerror(err));
137 tmp = __cpu_to_be32(initrd_end);
138 err = fdt_setprop(fdt, nodeoffset, "linux,initrd-end", &tmp, sizeof(tmp));
140 printf("WARNING fdt_chosen: could not set \"linux,initrd-end\" (libfdt error %s).\n", fdt_strerror(err));
142 #ifdef OF_STDOUT_PATH
143 err = fdt_setprop(fdt, nodeoffset, "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
145 printf("WARNING fdt_chosen: could not set \"linux,stdout-path\" (libfdt error %s).\n", fdt_strerror(err));
148 nodeoffset = fdt_path_offset (fdt, "/cpus");
149 if (nodeoffset >= 0) {
150 clock = cpu_to_be32(bd->bi_intfreq);
151 err = fdt_setprop(fdt, nodeoffset, "clock-frequency", &clock, 4);
153 printf("WARNING fdt_chosen: could not set \"clock-frequency\" (libfdt error %s).\n", fdt_strerror(err));
156 nodeoffset = fdt_path_offset (fdt, "/cpus/" OF_CPU "/timebase-frequency");
157 if (nodeoffset >= 0) {
158 clock = cpu_to_be32(OF_TBCLK);
159 err = fdt_setprop(fdt, nodeoffset, "clock-frequency", &clock, 4);
161 printf("WARNING fdt_chosen: could not set \"clock-frequency\" (libfdt error %s).\n", fdt_strerror(err));
167 /********************************************************************/
169 #ifdef CONFIG_OF_HAS_UBOOT_ENV
171 /* Function that returns a character from the environment */
172 extern uchar(*env_get_char) (int);
175 int fdt_env(void *fdt)
181 static char tmpenv[256];
183 err = fdt_check_header(fdt);
185 printf("libfdt: %s\n", fdt_strerror(err));
190 * See if we already have a "u-boot-env" node, delete it if so.
191 * Then create a new empty node.
193 nodeoffset = fdt_path_offset (fdt, "/u-boot-env");
194 if (nodeoffset >= 0) {
195 err = fdt_del_node(fdt, nodeoffset);
197 printf("libfdt: %s\n", fdt_strerror(err));
202 * Create a new node "/u-boot-env" (offset 0 is root level)
204 nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
205 if (nodeoffset < 0) {
206 printf("WARNING fdt_env: could not create the \"/u-boot-env node\" (libfdt error %s).\n", fdt_strerror(nodeoffset));
210 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
211 char *s, *lval, *rval;
214 * Find the end of the name=definition
216 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
219 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
220 *s++ = env_get_char(k);
224 * Find the first '=': it separates the name from the value
226 s = strchr(tmpenv, '=');
232 err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
234 printf("WARNING fdt_env: could not set \"%s\" (libfdt error %s).\n", lval, fdt_strerror(err));
240 #endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
242 /********************************************************************/
244 #ifdef CONFIG_OF_HAS_BD_T
246 #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
248 static const struct {
259 #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
260 || defined(CONFIG_E500)
263 #if defined(CONFIG_MPC5xxx)
266 #if defined(CONFIG_MPC83XX)
269 #if defined(CONFIG_MPC8220)
287 #if defined(CONFIG_MPC5xxx)
295 int fdt_bd_t(void *fdt)
300 u32 tmp; /* used to set 32 bit integer properties */
303 err = fdt_check_header(fdt);
305 printf("libfdt: %s\n", fdt_strerror(err));
310 * See if we already have a "bd_t" node, delete it if so.
311 * Then create a new empty node.
313 nodeoffset = fdt_path_offset (fdt, "/bd_t");
314 if (nodeoffset >= 0) {
315 err = fdt_del_node(fdt, nodeoffset);
317 printf("libfdt: %s\n", fdt_strerror(err));
322 * Create a new node "/bd_t" (offset 0 is root level)
324 nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
325 if (nodeoffset < 0) {
326 printf("WARNING fdt_bd_t: could not create the \"/bd_t node\" (libfdt error %s).\n", fdt_strerror(nodeoffset));
327 printf("libfdt: %s\n", fdt_strerror(nodeoffset));
331 * Use the string/pointer structure to create the entries...
333 for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
334 tmp = cpu_to_be32(getenv("bootargs"));
335 err = fdt_setprop(fdt, nodeoffset, bd_map[i].name, &tmp, sizeof(tmp));
337 printf("WARNING fdt_bd_t: could not set \"%s\" (libfdt error %s).\n", bd_map[i].name, fdt_strerror(err));
340 * Add a couple of oddball entries...
342 err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
344 printf("WARNING fdt_bd_t: could not set \"enetaddr\" (libfdt error %s).\n", fdt_strerror(err));
345 err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
347 printf("WARNING fdt_bd_t: could not set \"ethspeed\" (libfdt error %s).\n", fdt_strerror(err));
351 #endif /* ifdef CONFIG_OF_HAS_BD_T */
353 #endif /* CONFIG_OF_LIBFDT */