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>
35 #ifdef CONFIG_OF_BOARD_SETUP
36 void ft_board_setup(void *blob, bd_t *bd);
40 * Global data (for the gd->bd)
42 DECLARE_GLOBAL_DATA_PTR;
45 * fdt points to our working device tree.
47 struct fdt_header *fdt;
49 /********************************************************************/
51 int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
56 u32 tmp; /* used to set 32 bit integer properties */
57 char *str; /* used to set string properties */
59 err = fdt_check_header(fdt);
61 printf("libfdt: %s\n", fdt_strerror(err));
65 #ifdef CONFIG_OF_BOARD_SETUP
67 * ft_board_setup() sets various board-specific properties to
70 * STRICTLY SPEAKING, this is out of place, but it isn't clear
71 * where a better place would be.
73 ft_board_setup(fdt, bd);
76 if (initrd_start && initrd_end) {
77 struct fdt_reserve_entry re;
82 err = fdt_num_reservemap(fdt, &used, &total);
84 printf("libfdt: %s\n", fdt_strerror(err));
88 printf("WARNING fdt_chosen: "
89 "no room in the reserved map (%d of %d)\n",
94 * Look for an existing entry and update it. If we don't find
95 * the entry, we will j be the next available slot.
97 for (j = 0; j < used; j++) {
98 err = fdt_get_reservemap(fdt, j, &re);
99 if (re.address == initrd_start) {
103 err = fdt_replace_reservemap_entry(fdt, j,
104 initrd_start, initrd_end - initrd_start + 1);
106 printf("libfdt: %s\n", fdt_strerror(err));
112 * Find the "chosen" node.
114 nodeoffset = fdt_find_node_by_path (fdt, "/chosen");
117 * If we have a "chosen" node already the "force the writing"
118 * is not set, our job is done.
120 if ((nodeoffset >= 0) && !force)
124 * No "chosen" node in the blob: create it.
126 if (nodeoffset < 0) {
128 * Create a new node "/chosen" (offset 0 is root level)
130 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
131 if (nodeoffset < 0) {
132 printf("WARNING fdt_chosen: "
133 "could not create the \"/chosen node\" "
134 "(libfdt error %s).\n",
135 fdt_strerror(nodeoffset));
141 * Update pre-existing properties, create them if non-existant.
143 str = getenv("bootargs");
145 err = fdt_setprop(fdt, nodeoffset,
146 "bootargs", str, strlen(str)+1);
148 printf("WARNING fdt_chosen: "
149 "could not set \"bootargs\" "
150 "(libfdt error %s).\n",
153 if (initrd_start && initrd_end) {
154 tmp = __cpu_to_be32(initrd_start);
155 err = fdt_setprop(fdt, nodeoffset,
156 "linux,initrd-start", &tmp, sizeof(tmp));
158 printf("WARNING fdt_chosen: "
159 "could not set \"linux,initrd-start\" "
160 "(libfdt error %s).\n",
162 tmp = __cpu_to_be32(initrd_end);
163 err = fdt_setprop(fdt, nodeoffset,
164 "linux,initrd-end", &tmp, sizeof(tmp));
166 printf("WARNING fdt_chosen: "
167 "could not set \"linux,initrd-end\" "
168 "(libfdt error %s).\n",
171 #ifdef OF_STDOUT_PATH
172 err = fdt_setprop(fdt, nodeoffset,
173 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
175 printf("WARNING fdt_chosen: "
176 "could not set \"linux,stdout-path\" "
177 "(libfdt error %s).\n",
184 /********************************************************************/
186 #ifdef CONFIG_OF_HAS_UBOOT_ENV
188 /* Function that returns a character from the environment */
189 extern uchar(*env_get_char) (int);
192 int fdt_env(void *fdt)
198 static char tmpenv[256];
200 err = fdt_check_header(fdt);
202 printf("libfdt: %s\n", fdt_strerror(err));
207 * See if we already have a "u-boot-env" node, delete it if so.
208 * Then create a new empty node.
210 nodeoffset = fdt_find_node_by_path (fdt, "/u-boot-env");
211 if (nodeoffset >= 0) {
212 err = fdt_del_node(fdt, nodeoffset);
214 printf("libfdt: %s\n", fdt_strerror(err));
219 * Create a new node "/u-boot-env" (offset 0 is root level)
221 nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
222 if (nodeoffset < 0) {
223 printf("WARNING fdt_env: "
224 "could not create the \"/u-boot-env node\" "
225 "(libfdt error %s).\n",
226 fdt_strerror(nodeoffset));
230 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
231 char *s, *lval, *rval;
234 * Find the end of the name=definition
236 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
239 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
240 *s++ = env_get_char(k);
244 * Find the first '=': it separates the name from the value
246 s = strchr(tmpenv, '=');
252 err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
254 printf("WARNING fdt_env: "
255 "could not set \"%s\" "
256 "(libfdt error %s).\n",
257 lval, fdt_strerror(err));
263 #endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
265 /********************************************************************/
267 #ifdef CONFIG_OF_HAS_BD_T
269 #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
271 static const struct {
282 #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
283 || defined(CONFIG_E500)
286 #if defined(CONFIG_MPC5xxx)
289 #if defined(CONFIG_MPC83XX)
292 #if defined(CONFIG_MPC8220)
310 #if defined(CONFIG_MPC5xxx)
318 int fdt_bd_t(void *fdt)
323 u32 tmp; /* used to set 32 bit integer properties */
326 err = fdt_check_header(fdt);
328 printf("libfdt: %s\n", fdt_strerror(err));
333 * See if we already have a "bd_t" node, delete it if so.
334 * Then create a new empty node.
336 nodeoffset = fdt_find_node_by_path (fdt, "/bd_t");
337 if (nodeoffset >= 0) {
338 err = fdt_del_node(fdt, nodeoffset);
340 printf("libfdt: %s\n", fdt_strerror(err));
345 * Create a new node "/bd_t" (offset 0 is root level)
347 nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
348 if (nodeoffset < 0) {
349 printf("WARNING fdt_bd_t: "
350 "could not create the \"/bd_t node\" "
351 "(libfdt error %s).\n",
352 fdt_strerror(nodeoffset));
353 printf("libfdt: %s\n", fdt_strerror(nodeoffset));
357 * Use the string/pointer structure to create the entries...
359 for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
360 tmp = cpu_to_be32(getenv("bootargs"));
361 err = fdt_setprop(fdt, nodeoffset,
362 bd_map[i].name, &tmp, sizeof(tmp));
364 printf("WARNING fdt_bd_t: "
365 "could not set \"%s\" "
366 "(libfdt error %s).\n",
367 bd_map[i].name, fdt_strerror(err));
370 * Add a couple of oddball entries...
372 err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
374 printf("WARNING fdt_bd_t: "
375 "could not set \"enetaddr\" "
376 "(libfdt error %s).\n",
378 err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
380 printf("WARNING fdt_bd_t: "
381 "could not set \"ethspeed\" "
382 "(libfdt error %s).\n",
386 #endif /* ifdef CONFIG_OF_HAS_BD_T */
388 #endif /* CONFIG_OF_LIBFDT */