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;
40 * fdt_find_and_setprop: Find a node and set it's property
42 * @fdt: ptr to device tree
44 * @prop: property name
45 * @val: ptr to new value
46 * @len: length of new property value
47 * @create: flag to create the property if it doesn't exist
49 * Convenience function to directly set a property given the path to the node.
51 int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
52 const void *val, int len, int create)
54 int nodeoff = fdt_path_offset(fdt, node);
59 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL))
60 return 0; /* create flag not set; so exit quietly */
62 return fdt_setprop(fdt, nodeoff, prop, val, len);
65 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
66 static int fdt_fixup_stdout(void *fdt, int chosenoff)
69 #ifdef CONFIG_CONS_INDEX
71 char sername[9] = { 0 };
74 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
76 err = node = fdt_path_offset(fdt, "/aliases");
79 path = fdt_getprop(fdt, node, sername, &len);
81 char *p = malloc(len);
82 err = -FDT_ERR_NOSPACE;
85 err = fdt_setprop(fdt, chosenoff,
86 "linux,stdout-path", p, len);
95 printf("WARNING: could not set linux,stdout-path %s.\n",
102 int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force)
110 /* Find the "chosen" node. */
111 nodeoffset = fdt_path_offset (fdt, "/chosen");
113 /* If there is no "chosen" node in the blob return */
114 if (nodeoffset < 0) {
115 printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset));
119 /* just return if initrd_start/end aren't valid */
120 if ((initrd_start == 0) || (initrd_end == 0))
123 total = fdt_num_mem_rsv(fdt);
126 * Look for an existing entry and update it. If we don't find
127 * the entry, we will j be the next available slot.
129 for (j = 0; j < total; j++) {
130 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
131 if (addr == initrd_start) {
132 fdt_del_mem_rsv(fdt, j);
137 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1);
139 printf("fdt_initrd: %s\n", fdt_strerror(err));
143 path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
144 if ((path == NULL) || force) {
145 tmp = __cpu_to_be32(initrd_start);
146 err = fdt_setprop(fdt, nodeoffset,
147 "linux,initrd-start", &tmp, sizeof(tmp));
150 "could not set linux,initrd-start %s.\n",
154 tmp = __cpu_to_be32(initrd_end);
155 err = fdt_setprop(fdt, nodeoffset,
156 "linux,initrd-end", &tmp, sizeof(tmp));
158 printf("WARNING: could not set linux,initrd-end %s.\n",
168 int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
172 char *str; /* used to set string properties */
175 err = fdt_check_header(fdt);
177 printf("fdt_chosen: %s\n", fdt_strerror(err));
182 * Find the "chosen" node.
184 nodeoffset = fdt_path_offset (fdt, "/chosen");
187 * If there is no "chosen" node in the blob, create it.
189 if (nodeoffset < 0) {
191 * Create a new node "/chosen" (offset 0 is root level)
193 nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
194 if (nodeoffset < 0) {
195 printf("WARNING: could not create /chosen %s.\n",
196 fdt_strerror(nodeoffset));
202 * Create /chosen properites that don't exist in the fdt.
203 * If the property exists, update it only if the "force" parameter
206 str = getenv("bootargs");
208 path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
209 if ((path == NULL) || force) {
210 err = fdt_setprop(fdt, nodeoffset,
211 "bootargs", str, strlen(str)+1);
213 printf("WARNING: could not set bootargs %s.\n",
218 fdt_initrd(fdt, initrd_start, initrd_end, force);
220 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
221 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
222 if ((path == NULL) || force)
223 err = fdt_fixup_stdout(fdt, nodeoffset);
226 #ifdef OF_STDOUT_PATH
227 path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
228 if ((path == NULL) || force) {
229 err = fdt_setprop(fdt, nodeoffset,
230 "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
232 printf("WARNING: could not set linux,stdout-path %s.\n",
240 void do_fixup_by_path(void *fdt, const char *path, const char *prop,
241 const void *val, int len, int create)
245 debug("Updating property '%s/%s' = ", path, prop);
246 for (i = 0; i < len; i++)
247 debug(" %.2x", *(u8*)(val+i));
250 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
252 printf("Unable to update property %s:%s, err=%s\n",
253 path, prop, fdt_strerror(rc));
256 void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
259 val = cpu_to_fdt32(val);
260 do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
263 void do_fixup_by_prop(void *fdt,
264 const char *pname, const void *pval, int plen,
265 const char *prop, const void *val, int len,
271 debug("Updating property '%s' = ", prop);
272 for (i = 0; i < len; i++)
273 debug(" %.2x", *(u8*)(val+i));
276 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
277 while (off != -FDT_ERR_NOTFOUND) {
278 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
279 fdt_setprop(fdt, off, prop, val, len);
280 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
284 void do_fixup_by_prop_u32(void *fdt,
285 const char *pname, const void *pval, int plen,
286 const char *prop, u32 val, int create)
288 val = cpu_to_fdt32(val);
289 do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create);
292 void do_fixup_by_compat(void *fdt, const char *compat,
293 const char *prop, const void *val, int len, int create)
298 debug("Updating property '%s' = ", prop);
299 for (i = 0; i < len; i++)
300 debug(" %.2x", *(u8*)(val+i));
303 off = fdt_node_offset_by_compatible(fdt, -1, compat);
304 while (off != -FDT_ERR_NOTFOUND) {
305 if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
306 fdt_setprop(fdt, off, prop, val, len);
307 off = fdt_node_offset_by_compatible(fdt, off, compat);
311 void do_fixup_by_compat_u32(void *fdt, const char *compat,
312 const char *prop, u32 val, int create)
314 val = cpu_to_fdt32(val);
315 do_fixup_by_compat(fdt, compat, prop, &val, 4, create);
318 int fdt_fixup_memory(void *blob, u64 start, u64 size)
320 int err, nodeoffset, len = 0;
322 const u32 *addrcell, *sizecell;
324 err = fdt_check_header(blob);
326 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
330 /* update, or add and update /memory node */
331 nodeoffset = fdt_path_offset(blob, "/memory");
332 if (nodeoffset < 0) {
333 nodeoffset = fdt_add_subnode(blob, 0, "memory");
335 printf("WARNING: could not create /memory: %s.\n",
336 fdt_strerror(nodeoffset));
339 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
342 printf("WARNING: could not set %s %s.\n", "device_type",
347 addrcell = fdt_getprop(blob, 0, "#address-cells", NULL);
348 /* use shifts and mask to ensure endianness */
349 if ((addrcell) && (*addrcell == 2)) {
350 tmp[0] = (start >> 56) & 0xff;
351 tmp[1] = (start >> 48) & 0xff;
352 tmp[2] = (start >> 40) & 0xff;
353 tmp[3] = (start >> 32) & 0xff;
354 tmp[4] = (start >> 24) & 0xff;
355 tmp[5] = (start >> 16) & 0xff;
356 tmp[6] = (start >> 8) & 0xff;
357 tmp[7] = (start ) & 0xff;
360 tmp[0] = (start >> 24) & 0xff;
361 tmp[1] = (start >> 16) & 0xff;
362 tmp[2] = (start >> 8) & 0xff;
363 tmp[3] = (start ) & 0xff;
367 sizecell = fdt_getprop(blob, 0, "#size-cells", NULL);
368 /* use shifts and mask to ensure endianness */
369 if ((sizecell) && (*sizecell == 2)) {
370 tmp[0+len] = (size >> 56) & 0xff;
371 tmp[1+len] = (size >> 48) & 0xff;
372 tmp[2+len] = (size >> 40) & 0xff;
373 tmp[3+len] = (size >> 32) & 0xff;
374 tmp[4+len] = (size >> 24) & 0xff;
375 tmp[5+len] = (size >> 16) & 0xff;
376 tmp[6+len] = (size >> 8) & 0xff;
377 tmp[7+len] = (size ) & 0xff;
380 tmp[0+len] = (size >> 24) & 0xff;
381 tmp[1+len] = (size >> 16) & 0xff;
382 tmp[2+len] = (size >> 8) & 0xff;
383 tmp[3+len] = (size ) & 0xff;
387 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
389 printf("WARNING: could not set %s %s.\n",
390 "reg", fdt_strerror(err));
396 void fdt_fixup_ethernet(void *fdt)
399 char enet[16], *tmp, *end;
400 char mac[16] = "ethaddr";
402 unsigned char mac_addr[6];
404 node = fdt_path_offset(fdt, "/aliases");
409 while ((tmp = getenv(mac)) != NULL) {
410 sprintf(enet, "ethernet%d", i);
411 path = fdt_getprop(fdt, node, enet, NULL);
413 debug("No alias for %s\n", enet);
414 sprintf(mac, "eth%daddr", ++i);
418 for (j = 0; j < 6; j++) {
419 mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
421 tmp = (*end) ? end+1 : end;
424 do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
425 do_fixup_by_path(fdt, path, "local-mac-address",
428 sprintf(mac, "eth%daddr", ++i);
432 #ifdef CONFIG_HAS_FSL_DR_USB
433 void fdt_fixup_dr_usb(void *blob, bd_t *bd)
437 const char *compat = "fsl-usb2-dr";
438 const char *prop_mode = "dr_mode";
439 const char *prop_type = "phy_type";
443 mode = getenv("usb_dr_mode");
444 type = getenv("usb_phy_type");
448 node_offset = fdt_node_offset_by_compatible(blob, 0, compat);
449 if (node_offset < 0) {
450 printf("WARNING: could not find compatible node %s: %s.\n",
451 compat, fdt_strerror(node_offset));
456 err = fdt_setprop(blob, node_offset, prop_mode, mode,
459 printf("WARNING: could not set %s for %s: %s.\n",
460 prop_mode, compat, fdt_strerror(err));
464 err = fdt_setprop(blob, node_offset, prop_type, type,
467 printf("WARNING: could not set %s for %s: %s.\n",
468 prop_type, compat, fdt_strerror(err));
471 #endif /* CONFIG_HAS_FSL_DR_USB */
473 #if defined(CONFIG_MPC83XX) || defined(CONFIG_MPC85xx)
475 * update crypto node properties to a specified revision of the SEC
476 * called with sec_rev == 0 if not on an mpc8xxxE processor
478 void fdt_fixup_crypto_node(void *blob, int sec_rev)
480 const struct sec_rev_prop {
483 u32 channel_fifo_len;
485 u32 descriptor_types_mask;
486 } sec_rev_prop_list [] = {
487 { 0x0200, 4, 24, 0x07e, 0x01010ebf }, /* SEC 2.0 */
488 { 0x0201, 4, 24, 0x0fe, 0x012b0ebf }, /* SEC 2.1 */
489 { 0x0202, 1, 24, 0x04c, 0x0122003f }, /* SEC 2.2 */
490 { 0x0204, 4, 24, 0x07e, 0x012b0ebf }, /* SEC 2.4 */
491 { 0x0300, 4, 24, 0x9fe, 0x03ab0ebf }, /* SEC 3.0 */
492 { 0x0303, 4, 24, 0x97c, 0x03ab0abf }, /* SEC 3.3 */
494 char compat_strlist[ARRAY_SIZE(sec_rev_prop_list) *
495 sizeof("fsl,secX.Y")];
496 int crypto_node, sec_idx, err;
500 /* locate crypto node based on lowest common compatible */
501 crypto_node = fdt_node_offset_by_compatible(blob, -1, "fsl,sec2.0");
502 if (crypto_node == -FDT_ERR_NOTFOUND)
505 /* delete it if not on an E-processor */
506 if (crypto_node > 0 && !sec_rev) {
507 fdt_del_node(blob, crypto_node);
511 /* else we got called for possible uprev */
512 for (sec_idx = 0; sec_idx < ARRAY_SIZE(sec_rev_prop_list); sec_idx++)
513 if (sec_rev_prop_list[sec_idx].sec_rev == sec_rev)
516 if (sec_idx == ARRAY_SIZE(sec_rev_prop_list)) {
517 puts("warning: unknown SEC revision number\n");
521 val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].num_channels);
522 err = fdt_setprop(blob, crypto_node, "fsl,num-channels", &val, 4);
524 printf("WARNING: could not set crypto property: %s\n",
527 val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].descriptor_types_mask);
528 err = fdt_setprop(blob, crypto_node, "fsl,descriptor-types-mask", &val, 4);
530 printf("WARNING: could not set crypto property: %s\n",
533 val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].exec_units_mask);
534 err = fdt_setprop(blob, crypto_node, "fsl,exec-units-mask", &val, 4);
536 printf("WARNING: could not set crypto property: %s\n",
539 val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].channel_fifo_len);
540 err = fdt_setprop(blob, crypto_node, "fsl,channel-fifo-len", &val, 4);
542 printf("WARNING: could not set crypto property: %s\n",
546 while (sec_idx >= 0) {
547 p = compat_strlist + val;
548 val += sprintf(p, "fsl,sec%d.%d",
549 (sec_rev_prop_list[sec_idx].sec_rev & 0xff00) >> 8,
550 sec_rev_prop_list[sec_idx].sec_rev & 0x00ff) + 1;
553 err = fdt_setprop(blob, crypto_node, "compatible", &compat_strlist, val);
555 printf("WARNING: could not set crypto property: %s\n",
558 #endif /* defined(CONFIG_MPC83XX) || defined(CONFIG_MPC85xx) */
560 /* Resize the fdt to its actual size + a bit of padding */
561 int fdt_resize(void *blob)
571 total = fdt_num_mem_rsv(blob);
572 for (i = 0; i < total; i++) {
573 fdt_get_mem_rsv(blob, i, &addr, &size);
574 if (addr == (uint64_t)(u32)blob) {
575 fdt_del_mem_rsv(blob, i);
580 /* Calculate the actual size of the fdt */
581 actualsize = fdt_off_dt_strings(blob) +
582 fdt_size_dt_strings(blob);
584 /* Make it so the fdt ends on a page boundary */
585 actualsize = ALIGN(actualsize, 0x1000);
586 actualsize = actualsize - ((uint)blob & 0xfff);
588 /* Change the fdt header to reflect the correct size */
589 fdt_set_totalsize(blob, actualsize);
591 /* Add the new reservation */
592 ret = fdt_add_mem_rsv(blob, (uint)blob, actualsize);