blob: d5743b445d1ee4346148f71a07f3ef76aaa2a5f2 [file] [log] [blame]
Gerald Van Barenc4a57ea2007-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 Barencfede442007-04-25 22:23:36 -040040/*
41 * fdt points to our working device tree.
42 */
43struct fdt_header *fdt;
44
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040045/********************************************************************/
46
Kumar Galabf771dc2007-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{
Kumar Galac8ab7052007-10-24 11:04:22 -050062 int nodeoff = fdt_path_offset(fdt, node);
Kumar Galabf771dc2007-10-24 10:21:57 -050063
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 Barenc4a57ea2007-04-06 14:19:43 -040073int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
74{
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040075 int nodeoffset;
76 int err;
Gerald Van Baren43555f42007-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 Barenc4a57ea2007-04-06 14:19:43 -040079
80 err = fdt_check_header(fdt);
81 if (err < 0) {
Gerald Van Baren0dc25122007-08-07 21:14:22 -040082 printf("fdt_chosen: %s\n", fdt_strerror(err));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040083 return err;
84 }
85
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040086 if (initrd_start && initrd_end) {
Kumar Galac8ab7052007-10-24 11:04:22 -050087 uint64_t addr, size;
88 int total = fdt_num_mem_rsv(fdt);
Gerald Van Barenc9502b92007-04-14 22:51:24 -040089 int j;
90
Gerald Van Barenc9502b92007-04-14 22:51:24 -040091 /*
92 * Look for an existing entry and update it. If we don't find
93 * the entry, we will j be the next available slot.
94 */
Kumar Galac8ab7052007-10-24 11:04:22 -050095 for (j = 0; j < total; j++) {
96 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
97 if (addr == initrd_start) {
98 fdt_del_mem_rsv(fdt, j);
Gerald Van Barenc9502b92007-04-14 22:51:24 -040099 break;
100 }
101 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500102
103 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1);
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400104 if (err < 0) {
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400105 printf("fdt_chosen: %s\n", fdt_strerror(err));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400106 return err;
107 }
108 }
109
110 /*
111 * Find the "chosen" node.
112 */
Kumar Galac8ab7052007-10-24 11:04:22 -0500113 nodeoffset = fdt_path_offset (fdt, "/chosen");
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400114
115 /*
116 * If we have a "chosen" node already the "force the writing"
117 * is not set, our job is done.
118 */
119 if ((nodeoffset >= 0) && !force)
120 return 0;
121
122 /*
123 * No "chosen" node in the blob: create it.
124 */
125 if (nodeoffset < 0) {
126 /*
127 * Create a new node "/chosen" (offset 0 is root level)
128 */
129 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
130 if (nodeoffset < 0) {
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400131 printf("WARNING: could not create /chosen %s.\n",
Gerald Van Baren43555f42007-05-25 22:08:57 -0400132 fdt_strerror(nodeoffset));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400133 return nodeoffset;
134 }
135 }
136
137 /*
138 * Update pre-existing properties, create them if non-existant.
139 */
140 str = getenv("bootargs");
141 if (str != NULL) {
Gerald Van Baren43555f42007-05-25 22:08:57 -0400142 err = fdt_setprop(fdt, nodeoffset,
143 "bootargs", str, strlen(str)+1);
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400144 if (err < 0)
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400145 printf("WARNING: could not set bootargs %s.\n",
Gerald Van Baren43555f42007-05-25 22:08:57 -0400146 fdt_strerror(err));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400147 }
148 if (initrd_start && initrd_end) {
149 tmp = __cpu_to_be32(initrd_start);
Gerald Van Baren43555f42007-05-25 22:08:57 -0400150 err = fdt_setprop(fdt, nodeoffset,
151 "linux,initrd-start", &tmp, sizeof(tmp));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400152 if (err < 0)
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400153 printf("WARNING: "
154 "could not set linux,initrd-start %s.\n",
Gerald Van Baren43555f42007-05-25 22:08:57 -0400155 fdt_strerror(err));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400156 tmp = __cpu_to_be32(initrd_end);
Gerald Van Baren43555f42007-05-25 22:08:57 -0400157 err = fdt_setprop(fdt, nodeoffset,
158 "linux,initrd-end", &tmp, sizeof(tmp));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400159 if (err < 0)
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400160 printf("WARNING: could not set linux,initrd-end %s.\n",
Gerald Van Baren43555f42007-05-25 22:08:57 -0400161 fdt_strerror(err));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400162 }
163#ifdef OF_STDOUT_PATH
Gerald Van Baren43555f42007-05-25 22:08:57 -0400164 err = fdt_setprop(fdt, nodeoffset,
165 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400166 if (err < 0)
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400167 printf("WARNING: could not set linux,stdout-path %s.\n",
Gerald Van Baren43555f42007-05-25 22:08:57 -0400168 fdt_strerror(err));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400169#endif
170
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400171 return err;
172}
173
174/********************************************************************/
175
176#ifdef CONFIG_OF_HAS_UBOOT_ENV
177
178/* Function that returns a character from the environment */
179extern uchar(*env_get_char) (int);
180
181
182int fdt_env(void *fdt)
183{
184 int nodeoffset;
185 int err;
186 int k, nxt;
187 int i;
188 static char tmpenv[256];
189
190 err = fdt_check_header(fdt);
191 if (err < 0) {
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400192 printf("fdt_env: %s\n", fdt_strerror(err));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400193 return err;
194 }
195
196 /*
197 * See if we already have a "u-boot-env" node, delete it if so.
198 * Then create a new empty node.
199 */
Kumar Galac8ab7052007-10-24 11:04:22 -0500200 nodeoffset = fdt_path_offset (fdt, "/u-boot-env");
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400201 if (nodeoffset >= 0) {
202 err = fdt_del_node(fdt, nodeoffset);
203 if (err < 0) {
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400204 printf("fdt_env: %s\n", fdt_strerror(err));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400205 return err;
206 }
207 }
208 /*
209 * Create a new node "/u-boot-env" (offset 0 is root level)
210 */
211 nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
212 if (nodeoffset < 0) {
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400213 printf("WARNING: could not create /u-boot-env %s.\n",
Gerald Van Baren43555f42007-05-25 22:08:57 -0400214 fdt_strerror(nodeoffset));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400215 return nodeoffset;
216 }
217
218 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
219 char *s, *lval, *rval;
220
221 /*
222 * Find the end of the name=definition
223 */
224 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
225 ;
226 s = tmpenv;
227 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
228 *s++ = env_get_char(k);
229 *s++ = '\0';
230 lval = tmpenv;
231 /*
232 * Find the first '=': it separates the name from the value
233 */
234 s = strchr(tmpenv, '=');
235 if (s != NULL) {
236 *s++ = '\0';
237 rval = s;
238 } else
239 continue;
240 err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
241 if (err < 0) {
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400242 printf("WARNING: could not set %s %s.\n",
Gerald Van Baren43555f42007-05-25 22:08:57 -0400243 lval, fdt_strerror(err));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400244 return err;
245 }
246 }
247 return 0;
248}
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400249#endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400250
251/********************************************************************/
252
253#ifdef CONFIG_OF_HAS_BD_T
254
255#define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
256
257static const struct {
258 const char *name;
259 int offset;
260} bd_map[] = {
261 BDM(memstart),
262 BDM(memsize),
263 BDM(flashstart),
264 BDM(flashsize),
265 BDM(flashoffset),
266 BDM(sramstart),
267 BDM(sramsize),
268#if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
269 || defined(CONFIG_E500)
270 BDM(immr_base),
271#endif
272#if defined(CONFIG_MPC5xxx)
273 BDM(mbar_base),
274#endif
275#if defined(CONFIG_MPC83XX)
276 BDM(immrbar),
277#endif
278#if defined(CONFIG_MPC8220)
279 BDM(mbar_base),
280 BDM(inpfreq),
281 BDM(pcifreq),
282 BDM(pevfreq),
283 BDM(flbfreq),
284 BDM(vcofreq),
285#endif
286 BDM(bootflags),
287 BDM(ip_addr),
288 BDM(intfreq),
289 BDM(busfreq),
290#ifdef CONFIG_CPM2
291 BDM(cpmfreq),
292 BDM(brgfreq),
293 BDM(sccfreq),
294 BDM(vco),
295#endif
296#if defined(CONFIG_MPC5xxx)
297 BDM(ipbfreq),
298 BDM(pcifreq),
299#endif
300 BDM(baudrate),
301};
302
303
304int fdt_bd_t(void *fdt)
305{
306 bd_t *bd = gd->bd;
307 int nodeoffset;
308 int err;
Gerald Van Baren43555f42007-05-25 22:08:57 -0400309 u32 tmp; /* used to set 32 bit integer properties */
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400310 int i;
311
312 err = fdt_check_header(fdt);
313 if (err < 0) {
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400314 printf("fdt_bd_t: %s\n", fdt_strerror(err));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400315 return err;
316 }
317
318 /*
319 * See if we already have a "bd_t" node, delete it if so.
320 * Then create a new empty node.
321 */
Kumar Galac8ab7052007-10-24 11:04:22 -0500322 nodeoffset = fdt_path_offset (fdt, "/bd_t");
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400323 if (nodeoffset >= 0) {
324 err = fdt_del_node(fdt, nodeoffset);
325 if (err < 0) {
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400326 printf("fdt_bd_t: %s\n", fdt_strerror(err));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400327 return err;
328 }
329 }
330 /*
331 * Create a new node "/bd_t" (offset 0 is root level)
332 */
333 nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
334 if (nodeoffset < 0) {
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400335 printf("WARNING: could not create /bd_t %s.\n",
Gerald Van Baren43555f42007-05-25 22:08:57 -0400336 fdt_strerror(nodeoffset));
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400337 printf("fdt_bd_t: %s\n", fdt_strerror(nodeoffset));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400338 return nodeoffset;
339 }
340 /*
341 * Use the string/pointer structure to create the entries...
342 */
343 for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
344 tmp = cpu_to_be32(getenv("bootargs"));
Gerald Van Baren43555f42007-05-25 22:08:57 -0400345 err = fdt_setprop(fdt, nodeoffset,
346 bd_map[i].name, &tmp, sizeof(tmp));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400347 if (err < 0)
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400348 printf("WARNING: could not set %s %s.\n",
Gerald Van Baren43555f42007-05-25 22:08:57 -0400349 bd_map[i].name, fdt_strerror(err));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400350 }
351 /*
352 * Add a couple of oddball entries...
353 */
354 err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
355 if (err < 0)
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400356 printf("WARNING: could not set enetaddr %s.\n",
Gerald Van Baren43555f42007-05-25 22:08:57 -0400357 fdt_strerror(err));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400358 err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
359 if (err < 0)
Gerald Van Baren0dc25122007-08-07 21:14:22 -0400360 printf("WARNING: could not set ethspeed %s.\n",
Gerald Van Baren43555f42007-05-25 22:08:57 -0400361 fdt_strerror(err));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400362 return 0;
363}
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400364#endif /* ifdef CONFIG_OF_HAS_BD_T */
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400365
Kumar Gala7e64cf82007-11-03 19:46:28 -0500366void do_fixup_by_path(void *fdt, const char *path, const char *prop,
367 const void *val, int len, int create)
368{
369#if defined(DEBUG)
370 int i;
371 debug("Updating property '%s/%s' = ", node, prop);
372 for (i = 0; i < len; i++)
373 debug(" %.2x", *(u8*)(val+i));
374 debug("\n");
375#endif
376 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
377 if (rc)
378 printf("Unable to update property %s:%s, err=%s\n",
379 path, prop, fdt_strerror(rc));
380}
381
382void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
383 u32 val, int create)
384{
385 val = cpu_to_fdt32(val);
386 do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
387}
388
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400389#endif /* CONFIG_OF_LIBFDT */