blob: 5ed874d0dfde21b86ad108882a95cc478fb9e1ef [file] [log] [blame]
Gerald Van Baren64dbbd42007-04-06 14:19:43 -04001/*
2 * (C) Copyright 2007
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
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.
12 *
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.
17 *
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,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <linux/ctype.h>
26#include <linux/types.h>
27
28#ifdef CONFIG_OF_LIBFDT
29
30#include <asm/global_data.h>
31#include <fdt.h>
32#include <libfdt.h>
33#include <fdt_support.h>
34
35/*
36 * Global data (for the gd->bd)
37 */
38DECLARE_GLOBAL_DATA_PTR;
39
Gerald Van Barenbb930e72007-04-25 22:23:36 -040040/*
41 * fdt points to our working device tree.
42 */
43struct fdt_header *fdt;
44
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040045/********************************************************************/
46
Kumar Galaa3c29332007-10-24 10:21:57 -050047/**
48 * fdt_find_and_setprop: Find a node and set it's property
49 *
50 * @fdt: ptr to device tree
51 * @node: path of node
52 * @prop: property name
53 * @val: ptr to new value
54 * @len: length of new property value
55 * @create: flag to create the property if it doesn't exist
56 *
57 * Convenience function to directly set a property given the path to the node.
58 */
59int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
60 const void *val, int len, int create)
61{
62 int nodeoff = fdt_find_node_by_path(fdt, node);
63
64 if (nodeoff < 0)
65 return nodeoff;
66
67 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL))
68 return 0; /* create flag not set; so exit quietly */
69
70 return fdt_setprop(fdt, nodeoff, prop, val, len);
71}
72
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040073int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
74{
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040075 int nodeoffset;
76 int err;
Gerald Van Baren35ec3982007-05-25 22:08:57 -040077 u32 tmp; /* used to set 32 bit integer properties */
78 char *str; /* used to set string properties */
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040079
80 err = fdt_check_header(fdt);
81 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -040082 printf("fdt_chosen: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040083 return err;
84 }
85
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040086 if (initrd_start && initrd_end) {
Gerald Van Baren7651f8b2007-04-19 23:14:39 -040087 struct fdt_reserve_entry re;
Gerald Van Barenc28abb92007-04-14 22:51:24 -040088 int used;
89 int total;
90 int j;
91
92 err = fdt_num_reservemap(fdt, &used, &total);
93 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -040094 printf("fdt_chosen: %s\n", fdt_strerror(err));
Gerald Van Barenc28abb92007-04-14 22:51:24 -040095 return err;
96 }
97 if (used >= total) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -040098 printf("WARNING: "
Gerald Van Baren35ec3982007-05-25 22:08:57 -040099 "no room in the reserved map (%d of %d)\n",
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400100 used, total);
101 return -1;
102 }
103 /*
104 * Look for an existing entry and update it. If we don't find
105 * the entry, we will j be the next available slot.
106 */
107 for (j = 0; j < used; j++) {
108 err = fdt_get_reservemap(fdt, j, &re);
Gerald Van Baren7651f8b2007-04-19 23:14:39 -0400109 if (re.address == initrd_start) {
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400110 break;
111 }
112 }
113 err = fdt_replace_reservemap_entry(fdt, j,
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400114 initrd_start, initrd_end - initrd_start + 1);
115 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400116 printf("fdt_chosen: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400117 return err;
118 }
119 }
120
121 /*
122 * Find the "chosen" node.
123 */
Gerald Van Baren1a861162007-06-06 22:47:58 -0400124 nodeoffset = fdt_find_node_by_path (fdt, "/chosen");
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400125
126 /*
127 * If we have a "chosen" node already the "force the writing"
128 * is not set, our job is done.
129 */
130 if ((nodeoffset >= 0) && !force)
131 return 0;
132
133 /*
134 * No "chosen" node in the blob: create it.
135 */
136 if (nodeoffset < 0) {
137 /*
138 * Create a new node "/chosen" (offset 0 is root level)
139 */
140 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
141 if (nodeoffset < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400142 printf("WARNING: could not create /chosen %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400143 fdt_strerror(nodeoffset));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400144 return nodeoffset;
145 }
146 }
147
148 /*
149 * Update pre-existing properties, create them if non-existant.
150 */
151 str = getenv("bootargs");
152 if (str != NULL) {
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400153 err = fdt_setprop(fdt, nodeoffset,
154 "bootargs", str, strlen(str)+1);
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400155 if (err < 0)
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400156 printf("WARNING: could not set bootargs %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400157 fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400158 }
159 if (initrd_start && initrd_end) {
160 tmp = __cpu_to_be32(initrd_start);
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400161 err = fdt_setprop(fdt, nodeoffset,
162 "linux,initrd-start", &tmp, sizeof(tmp));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400163 if (err < 0)
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400164 printf("WARNING: "
165 "could not set linux,initrd-start %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400166 fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400167 tmp = __cpu_to_be32(initrd_end);
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400168 err = fdt_setprop(fdt, nodeoffset,
169 "linux,initrd-end", &tmp, sizeof(tmp));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400170 if (err < 0)
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400171 printf("WARNING: could not set linux,initrd-end %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400172 fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400173 }
174#ifdef OF_STDOUT_PATH
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400175 err = fdt_setprop(fdt, nodeoffset,
176 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400177 if (err < 0)
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400178 printf("WARNING: could not set linux,stdout-path %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400179 fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400180#endif
181
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400182 return err;
183}
184
185/********************************************************************/
186
187#ifdef CONFIG_OF_HAS_UBOOT_ENV
188
189/* Function that returns a character from the environment */
190extern uchar(*env_get_char) (int);
191
192
193int fdt_env(void *fdt)
194{
195 int nodeoffset;
196 int err;
197 int k, nxt;
198 int i;
199 static char tmpenv[256];
200
201 err = fdt_check_header(fdt);
202 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400203 printf("fdt_env: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400204 return err;
205 }
206
207 /*
208 * See if we already have a "u-boot-env" node, delete it if so.
209 * Then create a new empty node.
210 */
Gerald Van Baren1a861162007-06-06 22:47:58 -0400211 nodeoffset = fdt_find_node_by_path (fdt, "/u-boot-env");
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400212 if (nodeoffset >= 0) {
213 err = fdt_del_node(fdt, nodeoffset);
214 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400215 printf("fdt_env: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400216 return err;
217 }
218 }
219 /*
220 * Create a new node "/u-boot-env" (offset 0 is root level)
221 */
222 nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
223 if (nodeoffset < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400224 printf("WARNING: could not create /u-boot-env %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400225 fdt_strerror(nodeoffset));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400226 return nodeoffset;
227 }
228
229 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
230 char *s, *lval, *rval;
231
232 /*
233 * Find the end of the name=definition
234 */
235 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
236 ;
237 s = tmpenv;
238 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
239 *s++ = env_get_char(k);
240 *s++ = '\0';
241 lval = tmpenv;
242 /*
243 * Find the first '=': it separates the name from the value
244 */
245 s = strchr(tmpenv, '=');
246 if (s != NULL) {
247 *s++ = '\0';
248 rval = s;
249 } else
250 continue;
251 err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
252 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400253 printf("WARNING: could not set %s %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400254 lval, fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400255 return err;
256 }
257 }
258 return 0;
259}
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400260#endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400261
262/********************************************************************/
263
264#ifdef CONFIG_OF_HAS_BD_T
265
266#define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
267
268static const struct {
269 const char *name;
270 int offset;
271} bd_map[] = {
272 BDM(memstart),
273 BDM(memsize),
274 BDM(flashstart),
275 BDM(flashsize),
276 BDM(flashoffset),
277 BDM(sramstart),
278 BDM(sramsize),
279#if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
280 || defined(CONFIG_E500)
281 BDM(immr_base),
282#endif
283#if defined(CONFIG_MPC5xxx)
284 BDM(mbar_base),
285#endif
286#if defined(CONFIG_MPC83XX)
287 BDM(immrbar),
288#endif
289#if defined(CONFIG_MPC8220)
290 BDM(mbar_base),
291 BDM(inpfreq),
292 BDM(pcifreq),
293 BDM(pevfreq),
294 BDM(flbfreq),
295 BDM(vcofreq),
296#endif
297 BDM(bootflags),
298 BDM(ip_addr),
299 BDM(intfreq),
300 BDM(busfreq),
301#ifdef CONFIG_CPM2
302 BDM(cpmfreq),
303 BDM(brgfreq),
304 BDM(sccfreq),
305 BDM(vco),
306#endif
307#if defined(CONFIG_MPC5xxx)
308 BDM(ipbfreq),
309 BDM(pcifreq),
310#endif
311 BDM(baudrate),
312};
313
314
315int fdt_bd_t(void *fdt)
316{
317 bd_t *bd = gd->bd;
318 int nodeoffset;
319 int err;
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400320 u32 tmp; /* used to set 32 bit integer properties */
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400321 int i;
322
323 err = fdt_check_header(fdt);
324 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400325 printf("fdt_bd_t: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400326 return err;
327 }
328
329 /*
330 * See if we already have a "bd_t" node, delete it if so.
331 * Then create a new empty node.
332 */
Gerald Van Baren1a861162007-06-06 22:47:58 -0400333 nodeoffset = fdt_find_node_by_path (fdt, "/bd_t");
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400334 if (nodeoffset >= 0) {
335 err = fdt_del_node(fdt, nodeoffset);
336 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400337 printf("fdt_bd_t: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400338 return err;
339 }
340 }
341 /*
342 * Create a new node "/bd_t" (offset 0 is root level)
343 */
344 nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
345 if (nodeoffset < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400346 printf("WARNING: could not create /bd_t %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400347 fdt_strerror(nodeoffset));
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400348 printf("fdt_bd_t: %s\n", fdt_strerror(nodeoffset));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400349 return nodeoffset;
350 }
351 /*
352 * Use the string/pointer structure to create the entries...
353 */
354 for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
355 tmp = cpu_to_be32(getenv("bootargs"));
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400356 err = fdt_setprop(fdt, nodeoffset,
357 bd_map[i].name, &tmp, sizeof(tmp));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400358 if (err < 0)
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400359 printf("WARNING: could not set %s %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400360 bd_map[i].name, fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400361 }
362 /*
363 * Add a couple of oddball entries...
364 */
365 err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
366 if (err < 0)
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400367 printf("WARNING: could not set enetaddr %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400368 fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400369 err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
370 if (err < 0)
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400371 printf("WARNING: could not set ethspeed %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400372 fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400373 return 0;
374}
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400375#endif /* ifdef CONFIG_OF_HAS_BD_T */
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400376
Kumar Galae93becf2007-11-03 19:46:28 -0500377void do_fixup_by_path(void *fdt, const char *path, const char *prop,
378 const void *val, int len, int create)
379{
380#if defined(DEBUG)
381 int i;
382 debug("Updating property '%s/%s' = ", node, prop);
383 for (i = 0; i < len; i++)
384 debug(" %.2x", *(u8*)(val+i));
385 debug("\n");
386#endif
387 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
388 if (rc)
389 printf("Unable to update property %s:%s, err=%s\n",
390 path, prop, fdt_strerror(rc));
391}
392
393void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
394 u32 val, int create)
395{
396 val = cpu_to_fdt32(val);
397 do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
398}
399
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400400#endif /* CONFIG_OF_LIBFDT */