]> git.ipfire.org Git - people/ms/u-boot.git/blob - common/fdt_support.c
Merge commit 'remotes/wd/master'
[people/ms/u-boot.git] / common / fdt_support.c
1 /*
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 */
38 DECLARE_GLOBAL_DATA_PTR;
39
40 /*
41 * fdt points to our working device tree.
42 */
43 struct fdt_header *fdt;
44
45 /********************************************************************/
46
47 int 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;
52 u32 tmp; /* used to set 32 bit integer properties */
53 char *str; /* used to set string properties */
54
55 err = fdt_check_header(fdt);
56 if (err < 0) {
57 printf("fdt_chosen: %s\n", fdt_strerror(err));
58 return err;
59 }
60
61 if (initrd_start && initrd_end) {
62 struct fdt_reserve_entry re;
63 int used;
64 int total;
65 int j;
66
67 err = fdt_num_reservemap(fdt, &used, &total);
68 if (err < 0) {
69 printf("fdt_chosen: %s\n", fdt_strerror(err));
70 return err;
71 }
72 if (used >= total) {
73 printf("WARNING: "
74 "no room in the reserved map (%d of %d)\n",
75 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);
84 if (re.address == initrd_start) {
85 break;
86 }
87 }
88 err = fdt_replace_reservemap_entry(fdt, j,
89 initrd_start, initrd_end - initrd_start + 1);
90 if (err < 0) {
91 printf("fdt_chosen: %s\n", fdt_strerror(err));
92 return err;
93 }
94 }
95
96 /*
97 * Find the "chosen" node.
98 */
99 nodeoffset = fdt_find_node_by_path (fdt, "/chosen");
100
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) {
117 printf("WARNING: could not create /chosen %s.\n",
118 fdt_strerror(nodeoffset));
119 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) {
128 err = fdt_setprop(fdt, nodeoffset,
129 "bootargs", str, strlen(str)+1);
130 if (err < 0)
131 printf("WARNING: could not set bootargs %s.\n",
132 fdt_strerror(err));
133 }
134 if (initrd_start && initrd_end) {
135 tmp = __cpu_to_be32(initrd_start);
136 err = fdt_setprop(fdt, nodeoffset,
137 "linux,initrd-start", &tmp, sizeof(tmp));
138 if (err < 0)
139 printf("WARNING: "
140 "could not set linux,initrd-start %s.\n",
141 fdt_strerror(err));
142 tmp = __cpu_to_be32(initrd_end);
143 err = fdt_setprop(fdt, nodeoffset,
144 "linux,initrd-end", &tmp, sizeof(tmp));
145 if (err < 0)
146 printf("WARNING: could not set linux,initrd-end %s.\n",
147 fdt_strerror(err));
148 }
149 #ifdef OF_STDOUT_PATH
150 err = fdt_setprop(fdt, nodeoffset,
151 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
152 if (err < 0)
153 printf("WARNING: could not set linux,stdout-path %s.\n",
154 fdt_strerror(err));
155 #endif
156
157 return err;
158 }
159
160 /********************************************************************/
161
162 #ifdef CONFIG_OF_HAS_UBOOT_ENV
163
164 /* Function that returns a character from the environment */
165 extern uchar(*env_get_char) (int);
166
167
168 int 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) {
178 printf("fdt_env: %s\n", fdt_strerror(err));
179 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 */
186 nodeoffset = fdt_find_node_by_path (fdt, "/u-boot-env");
187 if (nodeoffset >= 0) {
188 err = fdt_del_node(fdt, nodeoffset);
189 if (err < 0) {
190 printf("fdt_env: %s\n", fdt_strerror(err));
191 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) {
199 printf("WARNING: could not create /u-boot-env %s.\n",
200 fdt_strerror(nodeoffset));
201 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) {
228 printf("WARNING: could not set %s %s.\n",
229 lval, fdt_strerror(err));
230 return err;
231 }
232 }
233 return 0;
234 }
235 #endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
236
237 /********************************************************************/
238
239 #ifdef CONFIG_OF_HAS_BD_T
240
241 #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
242
243 static 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
290 int 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("fdt_bd_t: %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_find_node_by_path (fdt, "/bd_t");
309 if (nodeoffset >= 0) {
310 err = fdt_del_node(fdt, nodeoffset);
311 if (err < 0) {
312 printf("fdt_bd_t: %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("WARNING: could not create /bd_t %s.\n",
322 fdt_strerror(nodeoffset));
323 printf("fdt_bd_t: %s\n", fdt_strerror(nodeoffset));
324 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"));
331 err = fdt_setprop(fdt, nodeoffset,
332 bd_map[i].name, &tmp, sizeof(tmp));
333 if (err < 0)
334 printf("WARNING: could not set %s %s.\n",
335 bd_map[i].name, fdt_strerror(err));
336 }
337 /*
338 * Add a couple of oddball entries...
339 */
340 err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
341 if (err < 0)
342 printf("WARNING: could not set enetaddr %s.\n",
343 fdt_strerror(err));
344 err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
345 if (err < 0)
346 printf("WARNING: could not set ethspeed %s.\n",
347 fdt_strerror(err));
348 return 0;
349 }
350 #endif /* ifdef CONFIG_OF_HAS_BD_T */
351
352 #endif /* CONFIG_OF_LIBFDT */