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 #include <asm/global_data.h> 28 #include <fdt.h> 29 #include <libfdt.h> 30 #include <fdt_support.h> 31 #include <exports.h> 32 33 /* 34 * Global data (for the gd->bd) 35 */ 36 DECLARE_GLOBAL_DATA_PTR; 37 38 39 /** 40 * fdt_find_and_setprop: Find a node and set it's property 41 * 42 * @fdt: ptr to device tree 43 * @node: path of node 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 48 * 49 * Convenience function to directly set a property given the path to the node. 50 */ 51 int fdt_find_and_setprop(void *fdt, const char *node, const char *prop, 52 const void *val, int len, int create) 53 { 54 int nodeoff = fdt_path_offset(fdt, node); 55 56 if (nodeoff < 0) 57 return nodeoff; 58 59 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL)) 60 return 0; /* create flag not set; so exit quietly */ 61 62 return fdt_setprop(fdt, nodeoff, prop, val, len); 63 } 64 65 #ifdef CONFIG_OF_STDOUT_VIA_ALIAS 66 static int fdt_fixup_stdout(void *fdt, int chosenoff) 67 { 68 int err = 0; 69 #ifdef CONFIG_CONS_INDEX 70 int node; 71 char sername[9] = { 0 }; 72 const char *path; 73 74 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1); 75 76 err = node = fdt_path_offset(fdt, "/aliases"); 77 if (node >= 0) { 78 int len; 79 path = fdt_getprop(fdt, node, sername, &len); 80 if (path) { 81 char *p = malloc(len); 82 err = -FDT_ERR_NOSPACE; 83 if (p) { 84 memcpy(p, path, len); 85 err = fdt_setprop(fdt, chosenoff, 86 "linux,stdout-path", p, len); 87 free(p); 88 } 89 } else { 90 err = len; 91 } 92 } 93 #endif 94 if (err < 0) 95 printf("WARNING: could not set linux,stdout-path %s.\n", 96 fdt_strerror(err)); 97 98 return err; 99 } 100 #endif 101 102 int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force) 103 { 104 int nodeoffset; 105 int err, j, total; 106 u32 tmp; 107 const char *path; 108 uint64_t addr, size; 109 110 /* Find the "chosen" node. */ 111 nodeoffset = fdt_path_offset (fdt, "/chosen"); 112 113 /* If there is no "chosen" node in the blob return */ 114 if (nodeoffset < 0) { 115 printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset)); 116 return nodeoffset; 117 } 118 119 /* just return if initrd_start/end aren't valid */ 120 if ((initrd_start == 0) || (initrd_end == 0)) 121 return 0; 122 123 total = fdt_num_mem_rsv(fdt); 124 125 /* 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. 128 */ 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); 133 break; 134 } 135 } 136 137 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1); 138 if (err < 0) { 139 printf("fdt_initrd: %s\n", fdt_strerror(err)); 140 return err; 141 } 142 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)); 148 if (err < 0) { 149 printf("WARNING: " 150 "could not set linux,initrd-start %s.\n", 151 fdt_strerror(err)); 152 return err; 153 } 154 tmp = __cpu_to_be32(initrd_end); 155 err = fdt_setprop(fdt, nodeoffset, 156 "linux,initrd-end", &tmp, sizeof(tmp)); 157 if (err < 0) { 158 printf("WARNING: could not set linux,initrd-end %s.\n", 159 fdt_strerror(err)); 160 161 return err; 162 } 163 } 164 165 return 0; 166 } 167 168 int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force) 169 { 170 int nodeoffset; 171 int err; 172 char *str; /* used to set string properties */ 173 const char *path; 174 175 err = fdt_check_header(fdt); 176 if (err < 0) { 177 printf("fdt_chosen: %s\n", fdt_strerror(err)); 178 return err; 179 } 180 181 /* 182 * Find the "chosen" node. 183 */ 184 nodeoffset = fdt_path_offset (fdt, "/chosen"); 185 186 /* 187 * If there is no "chosen" node in the blob, create it. 188 */ 189 if (nodeoffset < 0) { 190 /* 191 * Create a new node "/chosen" (offset 0 is root level) 192 */ 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)); 197 return nodeoffset; 198 } 199 } 200 201 /* 202 * Create /chosen properites that don't exist in the fdt. 203 * If the property exists, update it only if the "force" parameter 204 * is true. 205 */ 206 str = getenv("bootargs"); 207 if (str != NULL) { 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); 212 if (err < 0) 213 printf("WARNING: could not set bootargs %s.\n", 214 fdt_strerror(err)); 215 } 216 } 217 218 fdt_initrd(fdt, initrd_start, initrd_end, force); 219 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); 224 #endif 225 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); 231 if (err < 0) 232 printf("WARNING: could not set linux,stdout-path %s.\n", 233 fdt_strerror(err)); 234 } 235 #endif 236 237 return err; 238 } 239 240 void do_fixup_by_path(void *fdt, const char *path, const char *prop, 241 const void *val, int len, int create) 242 { 243 #if defined(DEBUG) 244 int i; 245 debug("Updating property '%s/%s' = ", path, prop); 246 for (i = 0; i < len; i++) 247 debug(" %.2x", *(u8*)(val+i)); 248 debug("\n"); 249 #endif 250 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create); 251 if (rc) 252 printf("Unable to update property %s:%s, err=%s\n", 253 path, prop, fdt_strerror(rc)); 254 } 255 256 void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop, 257 u32 val, int create) 258 { 259 val = cpu_to_fdt32(val); 260 do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create); 261 } 262 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, 266 int create) 267 { 268 int off; 269 #if defined(DEBUG) 270 int i; 271 debug("Updating property '%s' = ", prop); 272 for (i = 0; i < len; i++) 273 debug(" %.2x", *(u8*)(val+i)); 274 debug("\n"); 275 #endif 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); 281 } 282 } 283 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) 287 { 288 val = cpu_to_fdt32(val); 289 do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create); 290 } 291 292 void do_fixup_by_compat(void *fdt, const char *compat, 293 const char *prop, const void *val, int len, int create) 294 { 295 int off = -1; 296 #if defined(DEBUG) 297 int i; 298 debug("Updating property '%s' = ", prop); 299 for (i = 0; i < len; i++) 300 debug(" %.2x", *(u8*)(val+i)); 301 debug("\n"); 302 #endif 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); 308 } 309 } 310 311 void do_fixup_by_compat_u32(void *fdt, const char *compat, 312 const char *prop, u32 val, int create) 313 { 314 val = cpu_to_fdt32(val); 315 do_fixup_by_compat(fdt, compat, prop, &val, 4, create); 316 } 317 318 int fdt_fixup_memory(void *blob, u64 start, u64 size) 319 { 320 int err, nodeoffset, len = 0; 321 u8 tmp[16]; 322 const u32 *addrcell, *sizecell; 323 324 err = fdt_check_header(blob); 325 if (err < 0) { 326 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err)); 327 return err; 328 } 329 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"); 334 if (nodeoffset < 0) 335 printf("WARNING: could not create /memory: %s.\n", 336 fdt_strerror(nodeoffset)); 337 return nodeoffset; 338 } 339 err = fdt_setprop(blob, nodeoffset, "device_type", "memory", 340 sizeof("memory")); 341 if (err < 0) { 342 printf("WARNING: could not set %s %s.\n", "device_type", 343 fdt_strerror(err)); 344 return err; 345 } 346 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; 358 len = 8; 359 } else { 360 tmp[0] = (start >> 24) & 0xff; 361 tmp[1] = (start >> 16) & 0xff; 362 tmp[2] = (start >> 8) & 0xff; 363 tmp[3] = (start ) & 0xff; 364 len = 4; 365 } 366 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; 378 len += 8; 379 } else { 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; 384 len += 4; 385 } 386 387 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len); 388 if (err < 0) { 389 printf("WARNING: could not set %s %s.\n", 390 "reg", fdt_strerror(err)); 391 return err; 392 } 393 return 0; 394 } 395 396 void fdt_fixup_ethernet(void *fdt) 397 { 398 int node, i, j; 399 char enet[16], *tmp, *end; 400 char mac[16] = "ethaddr"; 401 const char *path; 402 unsigned char mac_addr[6]; 403 404 node = fdt_path_offset(fdt, "/aliases"); 405 if (node < 0) 406 return; 407 408 i = 0; 409 while ((tmp = getenv(mac)) != NULL) { 410 sprintf(enet, "ethernet%d", i); 411 path = fdt_getprop(fdt, node, enet, NULL); 412 if (!path) { 413 debug("No alias for %s\n", enet); 414 sprintf(mac, "eth%daddr", ++i); 415 continue; 416 } 417 418 for (j = 0; j < 6; j++) { 419 mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0; 420 if (tmp) 421 tmp = (*end) ? end+1 : end; 422 } 423 424 do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0); 425 do_fixup_by_path(fdt, path, "local-mac-address", 426 &mac_addr, 6, 1); 427 428 sprintf(mac, "eth%daddr", ++i); 429 } 430 } 431 432 #ifdef CONFIG_HAS_FSL_DR_USB 433 void fdt_fixup_dr_usb(void *blob, bd_t *bd) 434 { 435 char *mode; 436 char *type; 437 const char *compat = "fsl-usb2-dr"; 438 const char *prop_mode = "dr_mode"; 439 const char *prop_type = "phy_type"; 440 int node_offset; 441 int err; 442 443 mode = getenv("usb_dr_mode"); 444 type = getenv("usb_phy_type"); 445 if (!mode && !type) 446 return; 447 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)); 452 return; 453 } 454 455 if (mode) { 456 err = fdt_setprop(blob, node_offset, prop_mode, mode, 457 strlen(mode) + 1); 458 if (err < 0) 459 printf("WARNING: could not set %s for %s: %s.\n", 460 prop_mode, compat, fdt_strerror(err)); 461 } 462 463 if (type) { 464 err = fdt_setprop(blob, node_offset, prop_type, type, 465 strlen(type) + 1); 466 if (err < 0) 467 printf("WARNING: could not set %s for %s: %s.\n", 468 prop_type, compat, fdt_strerror(err)); 469 } 470 } 471 #endif /* CONFIG_HAS_FSL_DR_USB */ 472 473 #if defined(CONFIG_MPC83XX) || defined(CONFIG_MPC85xx) 474 /* 475 * update crypto node properties to a specified revision of the SEC 476 * called with sec_rev == 0 if not on an mpc8xxxE processor 477 */ 478 void fdt_fixup_crypto_node(void *blob, int sec_rev) 479 { 480 const struct sec_rev_prop { 481 u32 sec_rev; 482 u32 num_channels; 483 u32 channel_fifo_len; 484 u32 exec_units_mask; 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 */ 493 }; 494 char compat_strlist[ARRAY_SIZE(sec_rev_prop_list) * 495 sizeof("fsl,secX.Y")]; 496 int crypto_node, sec_idx, err; 497 char *p; 498 u32 val; 499 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) 503 return; 504 505 /* delete it if not on an E-processor */ 506 if (crypto_node > 0 && !sec_rev) { 507 fdt_del_node(blob, crypto_node); 508 return; 509 } 510 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) 514 break; 515 516 if (sec_idx == ARRAY_SIZE(sec_rev_prop_list)) { 517 puts("warning: unknown SEC revision number\n"); 518 return; 519 } 520 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); 523 if (err < 0) 524 printf("WARNING: could not set crypto property: %s\n", 525 fdt_strerror(err)); 526 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); 529 if (err < 0) 530 printf("WARNING: could not set crypto property: %s\n", 531 fdt_strerror(err)); 532 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); 535 if (err < 0) 536 printf("WARNING: could not set crypto property: %s\n", 537 fdt_strerror(err)); 538 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); 541 if (err < 0) 542 printf("WARNING: could not set crypto property: %s\n", 543 fdt_strerror(err)); 544 545 val = 0; 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; 551 sec_idx--; 552 } 553 err = fdt_setprop(blob, crypto_node, "compatible", &compat_strlist, val); 554 if (err < 0) 555 printf("WARNING: could not set crypto property: %s\n", 556 fdt_strerror(err)); 557 } 558 #endif /* defined(CONFIG_MPC83XX) || defined(CONFIG_MPC85xx) */ 559 560 /* Resize the fdt to its actual size + a bit of padding */ 561 int fdt_resize(void *blob) 562 { 563 int i; 564 uint64_t addr, size; 565 int total, ret; 566 uint actualsize; 567 568 if (!blob) 569 return 0; 570 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); 576 break; 577 } 578 } 579 580 /* Calculate the actual size of the fdt */ 581 actualsize = fdt_off_dt_strings(blob) + 582 fdt_size_dt_strings(blob); 583 584 /* Make it so the fdt ends on a page boundary */ 585 actualsize = ALIGN(actualsize, 0x1000); 586 actualsize = actualsize - ((uint)blob & 0xfff); 587 588 /* Change the fdt header to reflect the correct size */ 589 fdt_set_totalsize(blob, actualsize); 590 591 /* Add the new reservation */ 592 ret = fdt_add_mem_rsv(blob, (uint)blob, actualsize); 593 if (ret < 0) 594 return ret; 595 596 return actualsize; 597 } 598