blob: 69099c4275ccb925cce7b7c69afc5ab4f76cfe6f [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
40
41/********************************************************************/
42
43int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
44{
45 bd_t *bd = gd->bd;
46 int nodeoffset;
47 int err;
48 u32 tmp; /* used to set 32 bit integer properties */
49 char *str; /* used to set string properties */
50 ulong clock;
51
52 err = fdt_check_header(fdt);
53 if (err < 0) {
54 printf("libfdt: %s\n", fdt_strerror(err));
55 return err;
56 }
57
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040058 if (initrd_start && initrd_end) {
Gerald Van Baren7651f8b2007-04-19 23:14:39 -040059 struct fdt_reserve_entry re;
Gerald Van Barenc28abb92007-04-14 22:51:24 -040060 int used;
61 int total;
62 int j;
63
64 err = fdt_num_reservemap(fdt, &used, &total);
65 if (err < 0) {
66 printf("libfdt: %s\n", fdt_strerror(err));
67 return err;
68 }
69 if (used >= total) {
70 printf("fdt_chosen: no room in the reserved map (%d of %d)\n",
71 used, total);
72 return -1;
73 }
74 /*
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.
77 */
78 for (j = 0; j < used; j++) {
79 err = fdt_get_reservemap(fdt, j, &re);
Gerald Van Baren7651f8b2007-04-19 23:14:39 -040080 if (re.address == initrd_start) {
Gerald Van Barenc28abb92007-04-14 22:51:24 -040081 break;
82 }
83 }
84 err = fdt_replace_reservemap_entry(fdt, j,
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040085 initrd_start, initrd_end - initrd_start + 1);
86 if (err < 0) {
87 printf("libfdt: %s\n", fdt_strerror(err));
88 return err;
89 }
90 }
91
92 /*
93 * Find the "chosen" node.
94 */
95 nodeoffset = fdt_path_offset (fdt, "/chosen");
96
97 /*
98 * If we have a "chosen" node already the "force the writing"
99 * is not set, our job is done.
100 */
101 if ((nodeoffset >= 0) && !force)
102 return 0;
103
104 /*
105 * No "chosen" node in the blob: create it.
106 */
107 if (nodeoffset < 0) {
108 /*
109 * Create a new node "/chosen" (offset 0 is root level)
110 */
111 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
112 if (nodeoffset < 0) {
113 printf("libfdt: %s\n", fdt_strerror(nodeoffset));
114 return nodeoffset;
115 }
116 }
117
118 /*
119 * Update pre-existing properties, create them if non-existant.
120 */
121 str = getenv("bootargs");
122 if (str != NULL) {
123 err = fdt_setprop(fdt, nodeoffset, "bootargs", str, strlen(str)+1);
124 if (err < 0)
125 printf("libfdt: %s\n", fdt_strerror(err));
126 }
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));
130 if (err < 0)
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));
134 if (err < 0)
135 printf("libfdt: %s\n", fdt_strerror(err));
136 }
137#ifdef OF_STDOUT_PATH
138 err = fdt_setprop(fdt, nodeoffset, "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
139 if (err < 0)
140 printf("libfdt: %s\n", fdt_strerror(err));
141#endif
142
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);
147 if (err < 0)
148 printf("libfdt: %s\n", fdt_strerror(err));
149 }
150#ifdef OF_TBCLK
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);
155 if (err < 0)
156 printf("libfdt: %s\n", fdt_strerror(err));
157 }
158#endif
159 return err;
160}
161
162/********************************************************************/
163
164#ifdef CONFIG_OF_HAS_UBOOT_ENV
165
166/* Function that returns a character from the environment */
167extern uchar(*env_get_char) (int);
168
169
170int fdt_env(void *fdt)
171{
172 int nodeoffset;
173 int err;
174 int k, nxt;
175 int i;
176 static char tmpenv[256];
177
178 err = fdt_check_header(fdt);
179 if (err < 0) {
180 printf("libfdt: %s\n", fdt_strerror(err));
181 return err;
182 }
183
184 /*
185 * See if we already have a "u-boot-env" node, delete it if so.
186 * Then create a new empty node.
187 */
188 nodeoffset = fdt_path_offset (fdt, "/u-boot-env");
189 if (nodeoffset >= 0) {
190 err = fdt_del_node(fdt, nodeoffset);
191 if (err < 0) {
192 printf("libfdt: %s\n", fdt_strerror(err));
193 return err;
194 }
195 }
196 /*
197 * Create a new node "/u-boot-env" (offset 0 is root level)
198 */
199 nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
200 if (nodeoffset < 0) {
201 printf("libfdt: %s\n", fdt_strerror(nodeoffset));
202 return nodeoffset;
203 }
204
205 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
206 char *s, *lval, *rval;
207
208 /*
209 * Find the end of the name=definition
210 */
211 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
212 ;
213 s = tmpenv;
214 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
215 *s++ = env_get_char(k);
216 *s++ = '\0';
217 lval = tmpenv;
218 /*
219 * Find the first '=': it separates the name from the value
220 */
221 s = strchr(tmpenv, '=');
222 if (s != NULL) {
223 *s++ = '\0';
224 rval = s;
225 } else
226 continue;
227 err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
228 if (err < 0) {
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400229 printf("libfdt: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400230 return err;
231 }
232 }
233 return 0;
234}
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400235#endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400236
237/********************************************************************/
238
239#ifdef CONFIG_OF_HAS_BD_T
240
241#define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
242
243static const struct {
244 const char *name;
245 int offset;
246} bd_map[] = {
247 BDM(memstart),
248 BDM(memsize),
249 BDM(flashstart),
250 BDM(flashsize),
251 BDM(flashoffset),
252 BDM(sramstart),
253 BDM(sramsize),
254#if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
255 || defined(CONFIG_E500)
256 BDM(immr_base),
257#endif
258#if defined(CONFIG_MPC5xxx)
259 BDM(mbar_base),
260#endif
261#if defined(CONFIG_MPC83XX)
262 BDM(immrbar),
263#endif
264#if defined(CONFIG_MPC8220)
265 BDM(mbar_base),
266 BDM(inpfreq),
267 BDM(pcifreq),
268 BDM(pevfreq),
269 BDM(flbfreq),
270 BDM(vcofreq),
271#endif
272 BDM(bootflags),
273 BDM(ip_addr),
274 BDM(intfreq),
275 BDM(busfreq),
276#ifdef CONFIG_CPM2
277 BDM(cpmfreq),
278 BDM(brgfreq),
279 BDM(sccfreq),
280 BDM(vco),
281#endif
282#if defined(CONFIG_MPC5xxx)
283 BDM(ipbfreq),
284 BDM(pcifreq),
285#endif
286 BDM(baudrate),
287};
288
289
290int fdt_bd_t(void *fdt)
291{
292 bd_t *bd = gd->bd;
293 int nodeoffset;
294 int err;
295 u32 tmp; /* used to set 32 bit integer properties */
296 int i;
297
298 err = fdt_check_header(fdt);
299 if (err < 0) {
300 printf("libfdt: %s\n", fdt_strerror(err));
301 return err;
302 }
303
304 /*
305 * See if we already have a "bd_t" node, delete it if so.
306 * Then create a new empty node.
307 */
308 nodeoffset = fdt_path_offset (fdt, "/bd_t");
309 if (nodeoffset >= 0) {
310 err = fdt_del_node(fdt, nodeoffset);
311 if (err < 0) {
312 printf("libfdt: %s\n", fdt_strerror(err));
313 return err;
314 }
315 }
316 /*
317 * Create a new node "/bd_t" (offset 0 is root level)
318 */
319 nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
320 if (nodeoffset < 0) {
321 printf("libfdt: %s\n", fdt_strerror(nodeoffset));
322 return nodeoffset;
323 }
324 /*
325 * Use the string/pointer structure to create the entries...
326 */
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));
330 if (err < 0)
331 printf("libfdt: %s\n", fdt_strerror(err));
332 }
333 /*
334 * Add a couple of oddball entries...
335 */
336 err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
337 if (err < 0)
338 printf("libfdt: %s\n", fdt_strerror(err));
339 err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
340 if (err < 0)
341 printf("libfdt: %s\n", fdt_strerror(err));
342
343 return 0;
344}
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400345#endif /* ifdef CONFIG_OF_HAS_BD_T */
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400346
347#endif /* CONFIG_OF_LIBFDT */