3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
25 #include <linux/ctype.h>
26 #include <linux/types.h>
27 #include <asm/global_data.h>
30 #include <fdt_support.h>
34 * Global data (for the gd->bd)
36 DECLARE_GLOBAL_DATA_PTR;
39 * fdt points to our working device tree.
41 struct fdt_header *fdt;
45 * fdt_find_and_setprop: Find a node and set it's property
47 * @fdt: ptr to device tree
49 * @prop: property name
50 * @val: ptr to new value
51 * @len: length of new property value
52 * @create: flag to create the property if it doesn't exist
54 * Convenience function to directly set a property given the path to the node.
56 int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
57 const void *val, int len, int create)
59 int nodeoff = fdt_path_offset(fdt, node);
64 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL))
65 return 0; /* create flag not set; so exit quietly */
67 return fdt_setprop(fdt, nodeoff, prop, val, len);
70 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
71 static int fdt_fixup_stdout(void *fdt, int choosenoff)
74 #ifdef CONFIG_CONS_INDEX
76 char sername[9] = { 0 };
79 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
81 err = node = fdt_path_offset(fdt, "/aliases");
84 path = fdt_getprop(fdt, node, sername, &len);
86 char *p = malloc(len);
87 err = -FDT_ERR_NOSPACE;
90 err = fdt_setprop(fdt, choosenoff,
91 "linux,stdout-path", p, len);
100 printf("WARNING: could not set linux,stdout-path %s.\n",
107 int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
111 u32 tmp; /* used to set 32 bit integer properties */
112 char *str; /* used to set string properties */
115 err = fdt_check_header(fdt);
117 printf("fdt_chosen: %s\n", fdt_strerror(err));
121 if (initrd_start && initrd_end) {
123 int total = fdt_num_mem_rsv(fdt);
127 * Look for an existing entry and update it. If we don't find
128 * the entry, we will j be the next available slot.
130 for (j = 0; j < total; j++) {
131 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
132 if (addr == initrd_start) {
133 fdt_del_mem_rsv(fdt, j);
138 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1);
140 printf("fdt_chosen: %s\n", fdt_strerror(err));
146 * Find the "chosen" node.
148 nodeoffset = fdt_path_offset (fdt, "/chosen");
151 * If there is no "chosen" node in the blob, create it.
153 if (nodeoffset < 0) {
155 * Create a new node "/chosen" (offset 0 is root level)
157 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
158 if (nodeoffset < 0) {
159 printf("WARNING: could not create /chosen %s.\n",
160 fdt_strerror(nodeoffset));
166 * Create /chosen properites that don't exist in the fdt.
167 * If the property exists, update it only if the "force" parameter
170 str = getenv("bootargs");
172 path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
173 if ((path == NULL) || force) {
174 err = fdt_setprop(fdt, nodeoffset,
175 "bootargs", str, strlen(str)+1);
177 printf("WARNING: could not set bootargs %s.\n",
181 if (initrd_start && initrd_end) {
182 path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
183 if ((path == NULL) || force) {
184 tmp = __cpu_to_be32(initrd_start);
185 err = fdt_setprop(fdt, nodeoffset,
186 "linux,initrd-start", &tmp, sizeof(tmp));
189 "could not set linux,initrd-start %s.\n",
191 tmp = __cpu_to_be32(initrd_end);
192 err = fdt_setprop(fdt, nodeoffset,
193 "linux,initrd-end", &tmp, sizeof(tmp));
195 printf("WARNING: could not set linux,initrd-end %s.\n",
200 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
201 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
202 if ((path == NULL) || force)
203 err = fdt_fixup_stdout(fdt, nodeoffset);
206 #ifdef OF_STDOUT_PATH
207 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
208 if ((path == NULL) || force) {
209 err = fdt_setprop(fdt, nodeoffset,
210 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
212 printf("WARNING: could not set linux,stdout-path %s.\n",
220 void do_fixup_by_path(void *fdt, const char *path, const char *prop,
221 const void *val, int len, int create)
225 debug("Updating property '%s/%s' = ", path, prop);
226 for (i = 0; i < len; i++)
227 debug(" %.2x", *(u8*)(val+i));
230 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
232 printf("Unable to update property %s:%s, err=%s\n",
233 path, prop, fdt_strerror(rc));
236 void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
239 val = cpu_to_fdt32(val);
240 do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
243 void do_fixup_by_prop(void *fdt,
244 const char *pname, const void *pval, int plen,
245 const char *prop, const void *val, int len,
251 debug("Updating property '%s' = ", prop);
252 for (i = 0; i < len; i++)
253 debug(" %.2x", *(u8*)(val+i));
256 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
257 while (off != -FDT_ERR_NOTFOUND) {
258 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
259 fdt_setprop(fdt, off, prop, val, len);
260 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
264 void do_fixup_by_prop_u32(void *fdt,
265 const char *pname, const void *pval, int plen,
266 const char *prop, u32 val, int create)
268 val = cpu_to_fdt32(val);
269 do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create);
272 void do_fixup_by_compat(void *fdt, const char *compat,
273 const char *prop, const void *val, int len, int create)
278 debug("Updating property '%s' = ", prop);
279 for (i = 0; i < len; i++)
280 debug(" %.2x", *(u8*)(val+i));
283 off = fdt_node_offset_by_compatible(fdt, -1, compat);
284 while (off != -FDT_ERR_NOTFOUND) {
285 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
286 fdt_setprop(fdt, off, prop, val, len);
287 off = fdt_node_offset_by_compatible(fdt, off, compat);
291 void do_fixup_by_compat_u32(void *fdt, const char *compat,
292 const char *prop, u32 val, int create)
294 val = cpu_to_fdt32(val);
295 do_fixup_by_compat(fdt, compat, prop, &val, 4, create);
298 int fdt_fixup_memory(void *blob, u64 start, u64 size)
300 int err, nodeoffset, len = 0;
302 const u32 *addrcell, *sizecell;
304 err = fdt_check_header(blob);
306 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
310 /* update, or add and update /memory node */
311 nodeoffset = fdt_path_offset(blob, "/memory");
312 if (nodeoffset < 0) {
313 nodeoffset = fdt_add_subnode(blob, 0, "memory");
315 printf("WARNING: could not create /memory: %s.\n",
316 fdt_strerror(nodeoffset));
319 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
322 printf("WARNING: could not set %s %s.\n", "device_type",
327 addrcell = fdt_getprop(blob, 0, "#address-cells", NULL);
328 /* use shifts and mask to ensure endianness */
329 if ((addrcell) && (*addrcell == 2)) {
330 tmp[0] = (start >> 56) & 0xff;
331 tmp[1] = (start >> 48) & 0xff;
332 tmp[2] = (start >> 40) & 0xff;
333 tmp[3] = (start >> 32) & 0xff;
334 tmp[4] = (start >> 24) & 0xff;
335 tmp[5] = (start >> 16) & 0xff;
336 tmp[6] = (start >> 8) & 0xff;
337 tmp[7] = (start ) & 0xff;
340 tmp[0] = (start >> 24) & 0xff;
341 tmp[1] = (start >> 16) & 0xff;
342 tmp[2] = (start >> 8) & 0xff;
343 tmp[3] = (start ) & 0xff;
347 sizecell = fdt_getprop(blob, 0, "#size-cells", NULL);
348 /* use shifts and mask to ensure endianness */
349 if ((sizecell) && (*sizecell == 2)) {
350 tmp[0+len] = (size >> 56) & 0xff;
351 tmp[1+len] = (size >> 48) & 0xff;
352 tmp[2+len] = (size >> 40) & 0xff;
353 tmp[3+len] = (size >> 32) & 0xff;
354 tmp[4+len] = (size >> 24) & 0xff;
355 tmp[5+len] = (size >> 16) & 0xff;
356 tmp[6+len] = (size >> 8) & 0xff;
357 tmp[7+len] = (size ) & 0xff;
360 tmp[0+len] = (size >> 24) & 0xff;
361 tmp[1+len] = (size >> 16) & 0xff;
362 tmp[2+len] = (size >> 8) & 0xff;
363 tmp[3+len] = (size ) & 0xff;
367 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
369 printf("WARNING: could not set %s %s.\n",
370 "reg", fdt_strerror(err));
376 #if defined(CONFIG_HAS_ETH0) || defined(CONFIG_HAS_ETH1) ||\
377 defined(CONFIG_HAS_ETH2) || defined(CONFIG_HAS_ETH3)
379 void fdt_fixup_ethernet(void *fdt, bd_t *bd)
384 node = fdt_path_offset(fdt, "/aliases");
386 #if defined(CONFIG_HAS_ETH0)
387 path = fdt_getprop(fdt, node, "ethernet0", NULL);
389 do_fixup_by_path(fdt, path, "mac-address",
390 bd->bi_enetaddr, 6, 0);
391 do_fixup_by_path(fdt, path, "local-mac-address",
392 bd->bi_enetaddr, 6, 1);
395 #if defined(CONFIG_HAS_ETH1)
396 path = fdt_getprop(fdt, node, "ethernet1", NULL);
398 do_fixup_by_path(fdt, path, "mac-address",
399 bd->bi_enet1addr, 6, 0);
400 do_fixup_by_path(fdt, path, "local-mac-address",
401 bd->bi_enet1addr, 6, 1);
404 #if defined(CONFIG_HAS_ETH2)
405 path = fdt_getprop(fdt, node, "ethernet2", NULL);
407 do_fixup_by_path(fdt, path, "mac-address",
408 bd->bi_enet2addr, 6, 0);
409 do_fixup_by_path(fdt, path, "local-mac-address",
410 bd->bi_enet2addr, 6, 1);
413 #if defined(CONFIG_HAS_ETH3)
414 path = fdt_getprop(fdt, node, "ethernet3", NULL);
416 do_fixup_by_path(fdt, path, "mac-address",
417 bd->bi_enet3addr, 6, 0);
418 do_fixup_by_path(fdt, path, "local-mac-address",
419 bd->bi_enet3addr, 6, 1);