blob: caaa682a4f3a0e09d4d8a50e91088446468aa319 [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
47int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
48{
49 bd_t *bd = gd->bd;
50 int nodeoffset;
51 int err;
Gerald Van Baren35ec3982007-05-25 22:08:57 -040052 u32 tmp; /* used to set 32 bit integer properties */
53 char *str; /* used to set string properties */
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040054
55 err = fdt_check_header(fdt);
56 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -040057 printf("fdt_chosen: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040058 return err;
59 }
60
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040061 if (initrd_start && initrd_end) {
Gerald Van Baren7651f8b2007-04-19 23:14:39 -040062 struct fdt_reserve_entry re;
Gerald Van Barenc28abb92007-04-14 22:51:24 -040063 int used;
64 int total;
65 int j;
66
67 err = fdt_num_reservemap(fdt, &used, &total);
68 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -040069 printf("fdt_chosen: %s\n", fdt_strerror(err));
Gerald Van Barenc28abb92007-04-14 22:51:24 -040070 return err;
71 }
72 if (used >= total) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -040073 printf("WARNING: "
Gerald Van Baren35ec3982007-05-25 22:08:57 -040074 "no room in the reserved map (%d of %d)\n",
Gerald Van Barenc28abb92007-04-14 22:51:24 -040075 used, total);
76 return -1;
77 }
78 /*
79 * Look for an existing entry and update it. If we don't find
80 * the entry, we will j be the next available slot.
81 */
82 for (j = 0; j < used; j++) {
83 err = fdt_get_reservemap(fdt, j, &re);
Gerald Van Baren7651f8b2007-04-19 23:14:39 -040084 if (re.address == initrd_start) {
Gerald Van Barenc28abb92007-04-14 22:51:24 -040085 break;
86 }
87 }
88 err = fdt_replace_reservemap_entry(fdt, j,
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040089 initrd_start, initrd_end - initrd_start + 1);
90 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -040091 printf("fdt_chosen: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040092 return err;
93 }
94 }
95
96 /*
97 * Find the "chosen" node.
98 */
Gerald Van Baren1a861162007-06-06 22:47:58 -040099 nodeoffset = fdt_find_node_by_path (fdt, "/chosen");
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400100
101 /*
102 * If we have a "chosen" node already the "force the writing"
103 * is not set, our job is done.
104 */
105 if ((nodeoffset >= 0) && !force)
106 return 0;
107
108 /*
109 * No "chosen" node in the blob: create it.
110 */
111 if (nodeoffset < 0) {
112 /*
113 * Create a new node "/chosen" (offset 0 is root level)
114 */
115 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
116 if (nodeoffset < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400117 printf("WARNING: could not create /chosen %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400118 fdt_strerror(nodeoffset));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400119 return nodeoffset;
120 }
121 }
122
123 /*
124 * Update pre-existing properties, create them if non-existant.
125 */
126 str = getenv("bootargs");
127 if (str != NULL) {
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400128 err = fdt_setprop(fdt, nodeoffset,
129 "bootargs", str, strlen(str)+1);
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400130 if (err < 0)
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400131 printf("WARNING: could not set bootargs %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400132 fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400133 }
134 if (initrd_start && initrd_end) {
135 tmp = __cpu_to_be32(initrd_start);
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400136 err = fdt_setprop(fdt, nodeoffset,
137 "linux,initrd-start", &tmp, sizeof(tmp));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400138 if (err < 0)
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400139 printf("WARNING: "
140 "could not set linux,initrd-start %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400141 fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400142 tmp = __cpu_to_be32(initrd_end);
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400143 err = fdt_setprop(fdt, nodeoffset,
144 "linux,initrd-end", &tmp, sizeof(tmp));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400145 if (err < 0)
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400146 printf("WARNING: could not set linux,initrd-end %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400147 fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400148 }
149#ifdef OF_STDOUT_PATH
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400150 err = fdt_setprop(fdt, nodeoffset,
151 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400152 if (err < 0)
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400153 printf("WARNING: could not set linux,stdout-path %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400154 fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400155#endif
156
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400157 return err;
158}
159
160/********************************************************************/
161
162#ifdef CONFIG_OF_HAS_UBOOT_ENV
163
164/* Function that returns a character from the environment */
165extern uchar(*env_get_char) (int);
166
167
168int fdt_env(void *fdt)
169{
170 int nodeoffset;
171 int err;
172 int k, nxt;
173 int i;
174 static char tmpenv[256];
175
176 err = fdt_check_header(fdt);
177 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400178 printf("fdt_env: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400179 return err;
180 }
181
182 /*
183 * See if we already have a "u-boot-env" node, delete it if so.
184 * Then create a new empty node.
185 */
Gerald Van Baren1a861162007-06-06 22:47:58 -0400186 nodeoffset = fdt_find_node_by_path (fdt, "/u-boot-env");
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400187 if (nodeoffset >= 0) {
188 err = fdt_del_node(fdt, nodeoffset);
189 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400190 printf("fdt_env: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400191 return err;
192 }
193 }
194 /*
195 * Create a new node "/u-boot-env" (offset 0 is root level)
196 */
197 nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
198 if (nodeoffset < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400199 printf("WARNING: could not create /u-boot-env %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400200 fdt_strerror(nodeoffset));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400201 return nodeoffset;
202 }
203
204 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
205 char *s, *lval, *rval;
206
207 /*
208 * Find the end of the name=definition
209 */
210 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
211 ;
212 s = tmpenv;
213 for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
214 *s++ = env_get_char(k);
215 *s++ = '\0';
216 lval = tmpenv;
217 /*
218 * Find the first '=': it separates the name from the value
219 */
220 s = strchr(tmpenv, '=');
221 if (s != NULL) {
222 *s++ = '\0';
223 rval = s;
224 } else
225 continue;
226 err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
227 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400228 printf("WARNING: could not set %s %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400229 lval, 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;
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400295 u32 tmp; /* used to set 32 bit integer properties */
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400296 int i;
297
298 err = fdt_check_header(fdt);
299 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400300 printf("fdt_bd_t: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400301 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 */
Gerald Van Baren1a861162007-06-06 22:47:58 -0400308 nodeoffset = fdt_find_node_by_path (fdt, "/bd_t");
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400309 if (nodeoffset >= 0) {
310 err = fdt_del_node(fdt, nodeoffset);
311 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400312 printf("fdt_bd_t: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400313 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) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400321 printf("WARNING: could not create /bd_t %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400322 fdt_strerror(nodeoffset));
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400323 printf("fdt_bd_t: %s\n", fdt_strerror(nodeoffset));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400324 return nodeoffset;
325 }
326 /*
327 * Use the string/pointer structure to create the entries...
328 */
329 for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
330 tmp = cpu_to_be32(getenv("bootargs"));
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400331 err = fdt_setprop(fdt, nodeoffset,
332 bd_map[i].name, &tmp, sizeof(tmp));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400333 if (err < 0)
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400334 printf("WARNING: could not set %s %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400335 bd_map[i].name, fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400336 }
337 /*
338 * Add a couple of oddball entries...
339 */
340 err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
341 if (err < 0)
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400342 printf("WARNING: could not set enetaddr %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400343 fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400344 err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
345 if (err < 0)
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400346 printf("WARNING: could not set ethspeed %s.\n",
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400347 fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400348 return 0;
349}
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400350#endif /* ifdef CONFIG_OF_HAS_BD_T */
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400351
352#endif /* CONFIG_OF_LIBFDT */