1 /* 2 * (C) Copyright 2007 3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com 4 * 5 * Copyright 2010-2011 Freescale Semiconductor, Inc. 6 * 7 * SPDX-License-Identifier: GPL-2.0+ 8 */ 9 10 #include <common.h> 11 #include <exports.h> 12 #include <fdt_support.h> 13 #include <fdtdec.h> 14 #include <inttypes.h> 15 #ifdef CONFIG_MTD_BLK 16 #include <mtd_blk.h> 17 #endif 18 #include <stdio_dev.h> 19 #include <asm/arch/hotkey.h> 20 #include <asm/global_data.h> 21 #include <linux/ctype.h> 22 #include <linux/libfdt.h> 23 #include <linux/types.h> 24 25 DECLARE_GLOBAL_DATA_PTR; 26 27 /** 28 * fdt_getprop_u32_default_node - Return a node's property or a default 29 * 30 * @fdt: ptr to device tree 31 * @off: offset of node 32 * @cell: cell offset in property 33 * @prop: property name 34 * @dflt: default value if the property isn't found 35 * 36 * Convenience function to return a node's property or a default value if 37 * the property doesn't exist. 38 */ 39 u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell, 40 const char *prop, const u32 dflt) 41 { 42 const fdt32_t *val; 43 int len; 44 45 val = fdt_getprop(fdt, off, prop, &len); 46 47 /* Check if property exists */ 48 if (!val) 49 return dflt; 50 51 /* Check if property is long enough */ 52 if (len < ((cell + 1) * sizeof(uint32_t))) 53 return dflt; 54 55 return fdt32_to_cpu(*val); 56 } 57 58 /** 59 * fdt_getprop_u32_default - Find a node and return it's property or a default 60 * 61 * @fdt: ptr to device tree 62 * @path: path of node 63 * @prop: property name 64 * @dflt: default value if the property isn't found 65 * 66 * Convenience function to find a node and return it's property or a 67 * default value if it doesn't exist. 68 */ 69 u32 fdt_getprop_u32_default(const void *fdt, const char *path, 70 const char *prop, const u32 dflt) 71 { 72 int off; 73 74 off = fdt_path_offset(fdt, path); 75 if (off < 0) 76 return dflt; 77 78 return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt); 79 } 80 81 /** 82 * fdt_find_and_setprop: Find a node and set it's property 83 * 84 * @fdt: ptr to device tree 85 * @node: path of node 86 * @prop: property name 87 * @val: ptr to new value 88 * @len: length of new property value 89 * @create: flag to create the property if it doesn't exist 90 * 91 * Convenience function to directly set a property given the path to the node. 92 */ 93 int fdt_find_and_setprop(void *fdt, const char *node, const char *prop, 94 const void *val, int len, int create) 95 { 96 int nodeoff = fdt_path_offset(fdt, node); 97 98 if (nodeoff < 0) 99 return nodeoff; 100 101 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL)) 102 return 0; /* create flag not set; so exit quietly */ 103 104 return fdt_setprop(fdt, nodeoff, prop, val, len); 105 } 106 107 /** 108 * fdt_find_or_add_subnode() - find or possibly add a subnode of a given node 109 * 110 * @fdt: pointer to the device tree blob 111 * @parentoffset: structure block offset of a node 112 * @name: name of the subnode to locate 113 * 114 * fdt_subnode_offset() finds a subnode of the node with a given name. 115 * If the subnode does not exist, it will be created. 116 */ 117 int fdt_find_or_add_subnode(void *fdt, int parentoffset, const char *name) 118 { 119 int offset; 120 121 offset = fdt_subnode_offset(fdt, parentoffset, name); 122 123 if (offset == -FDT_ERR_NOTFOUND) 124 offset = fdt_add_subnode(fdt, parentoffset, name); 125 126 if (offset < 0) 127 printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset)); 128 129 return offset; 130 } 131 132 /* rename to CONFIG_OF_STDOUT_PATH ? */ 133 #if defined(OF_STDOUT_PATH) 134 static int fdt_fixup_stdout(void *fdt, int chosenoff) 135 { 136 return fdt_setprop(fdt, chosenoff, "linux,stdout-path", 137 OF_STDOUT_PATH, strlen(OF_STDOUT_PATH) + 1); 138 } 139 #elif defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX) 140 static int fdt_fixup_stdout(void *fdt, int chosenoff) 141 { 142 int err; 143 int aliasoff; 144 char sername[9] = { 0 }; 145 const void *path; 146 int len; 147 char tmp[256]; /* long enough */ 148 149 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1); 150 151 aliasoff = fdt_path_offset(fdt, "/aliases"); 152 if (aliasoff < 0) { 153 err = aliasoff; 154 goto noalias; 155 } 156 157 path = fdt_getprop(fdt, aliasoff, sername, &len); 158 if (!path) { 159 err = len; 160 goto noalias; 161 } 162 163 /* fdt_setprop may break "path" so we copy it to tmp buffer */ 164 memcpy(tmp, path, len); 165 166 err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len); 167 if (err < 0) 168 printf("WARNING: could not set linux,stdout-path %s.\n", 169 fdt_strerror(err)); 170 171 return err; 172 173 noalias: 174 printf("WARNING: %s: could not read %s alias: %s\n", 175 __func__, sername, fdt_strerror(err)); 176 177 return 0; 178 } 179 #else 180 static int fdt_fixup_stdout(void *fdt, int chosenoff) 181 { 182 return 0; 183 } 184 #endif 185 186 static inline int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name, 187 uint64_t val, int is_u64) 188 { 189 if (is_u64) 190 return fdt_setprop_u64(fdt, nodeoffset, name, val); 191 else 192 return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val); 193 } 194 195 int fdt_root(void *fdt) 196 { 197 char *serial; 198 int err; 199 200 err = fdt_check_header(fdt); 201 if (err < 0) { 202 printf("fdt_root: %s\n", fdt_strerror(err)); 203 return err; 204 } 205 206 serial = env_get("serial#"); 207 if (serial) { 208 err = fdt_setprop(fdt, 0, "serial-number", serial, 209 strlen(serial) + 1); 210 211 if (err < 0) { 212 printf("WARNING: could not set serial-number %s.\n", 213 fdt_strerror(err)); 214 return err; 215 } 216 } 217 218 return 0; 219 } 220 221 int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end) 222 { 223 int nodeoffset; 224 int err, j, total; 225 int is_u64; 226 uint64_t addr, size; 227 228 /* just return if the size of initrd is zero */ 229 if (initrd_start == initrd_end) 230 return 0; 231 232 /* find or create "/chosen" node. */ 233 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen"); 234 if (nodeoffset < 0) 235 return nodeoffset; 236 237 total = fdt_num_mem_rsv(fdt); 238 239 /* 240 * Look for an existing entry and update it. If we don't find 241 * the entry, we will j be the next available slot. 242 */ 243 for (j = 0; j < total; j++) { 244 err = fdt_get_mem_rsv(fdt, j, &addr, &size); 245 if (addr == initrd_start) { 246 fdt_del_mem_rsv(fdt, j); 247 break; 248 } 249 } 250 251 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start); 252 if (err < 0) { 253 printf("fdt_initrd: %s\n", fdt_strerror(err)); 254 return err; 255 } 256 257 is_u64 = (fdt_address_cells(fdt, 0) == 2); 258 259 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start", 260 (uint64_t)initrd_start, is_u64); 261 262 if (err < 0) { 263 printf("WARNING: could not set linux,initrd-start %s.\n", 264 fdt_strerror(err)); 265 return err; 266 } 267 268 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end", 269 (uint64_t)initrd_end, is_u64); 270 271 if (err < 0) { 272 printf("WARNING: could not set linux,initrd-end %s.\n", 273 fdt_strerror(err)); 274 275 return err; 276 } 277 278 return 0; 279 } 280 281 int fdt_chosen(void *fdt) 282 { 283 /* 284 * "bootargs_ext" is used when dtbo is applied. 285 */ 286 const char *arr_bootargs[] = { "bootargs", "bootargs_ext" }; 287 int nodeoffset; 288 int err; 289 int i; 290 char *str; /* used to set string properties */ 291 int dump; 292 293 dump = is_hotkey(HK_CMDLINE); 294 295 err = fdt_check_header(fdt); 296 if (err < 0) { 297 printf("fdt_chosen: %s\n", fdt_strerror(err)); 298 return err; 299 } 300 301 /* find or create "/chosen" node. */ 302 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen"); 303 if (nodeoffset < 0) 304 return nodeoffset; 305 306 str = env_get("bootargs"); 307 if (str) { 308 #ifdef CONFIG_ARCH_ROCKCHIP 309 const char *bootargs; 310 311 if (dump) 312 printf("## U-Boot bootargs: %s\n", str); 313 314 for (i = 0; i < ARRAY_SIZE(arr_bootargs); i++) { 315 bootargs = fdt_getprop(fdt, nodeoffset, 316 arr_bootargs[i], NULL); 317 if (bootargs) { 318 if (dump) 319 printf("## Kernel %s: %s\n", 320 arr_bootargs[i], bootargs); 321 /* 322 * Append kernel bootargs 323 * If use AB system, delete default "root=" which route 324 * to rootfs. Then the ab bootctl will choose the 325 * high priority system to boot and add its UUID 326 * to cmdline. The format is "roo=PARTUUID=xxxx...". 327 */ 328 hotkey_run(HK_INITCALL); 329 #ifdef CONFIG_ANDROID_AB 330 env_update_filter("bootargs", bootargs, "root="); 331 #else 332 env_update("bootargs", bootargs); 333 #endif 334 #ifdef CONFIG_MTD_BLK 335 char *mtd_par_info = mtd_part_parse(); 336 337 if (mtd_par_info) { 338 if (memcmp(env_get("devtype"), "mtd", 3) == 0) 339 env_update("bootargs", mtd_par_info); 340 } 341 #endif 342 /* 343 * Initrd fixup: remove unused "initrd=0x...,0x...", 344 * this for compatible with legacy parameter.txt 345 */ 346 env_delete("bootargs", "initrd=", 0); 347 348 /* 349 * If uart is required to be disabled during 350 * power on, it would be not initialized by 351 * any pre-loader and U-Boot. 352 * 353 * If we don't remove earlycon from commandline, 354 * kernel hangs while using earlycon to putc/getc 355 * which may dead loop for waiting uart status. 356 * (It seems the root cause is baundrate is not 357 * initilalized) 358 * 359 * So let's remove earlycon from commandline. 360 */ 361 if (gd->flags & GD_FLG_DISABLE_CONSOLE) 362 env_delete("bootargs", "earlycon=", 0); 363 } 364 #endif 365 } 366 367 str = env_get("bootargs"); 368 err = fdt_setprop(fdt, nodeoffset, "bootargs", str, 369 strlen(str) + 1); 370 if (err < 0) { 371 printf("WARNING: could not set bootargs %s.\n", 372 fdt_strerror(err)); 373 return err; 374 } 375 } 376 377 if (dump) 378 printf("## Merged bootargs: %s\n", env_get("bootargs")); 379 380 return fdt_fixup_stdout(fdt, nodeoffset); 381 } 382 383 void do_fixup_by_path(void *fdt, const char *path, const char *prop, 384 const void *val, int len, int create) 385 { 386 #if defined(DEBUG) 387 int i; 388 debug("Updating property '%s/%s' = ", path, prop); 389 for (i = 0; i < len; i++) 390 debug(" %.2x", *(u8*)(val+i)); 391 debug("\n"); 392 #endif 393 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create); 394 if (rc) 395 printf("Unable to update property %s:%s, err=%s\n", 396 path, prop, fdt_strerror(rc)); 397 } 398 399 void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop, 400 u32 val, int create) 401 { 402 fdt32_t tmp = cpu_to_fdt32(val); 403 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create); 404 } 405 406 void do_fixup_by_prop(void *fdt, 407 const char *pname, const void *pval, int plen, 408 const char *prop, const void *val, int len, 409 int create) 410 { 411 int off; 412 #if defined(DEBUG) 413 int i; 414 debug("Updating property '%s' = ", prop); 415 for (i = 0; i < len; i++) 416 debug(" %.2x", *(u8*)(val+i)); 417 debug("\n"); 418 #endif 419 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen); 420 while (off != -FDT_ERR_NOTFOUND) { 421 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL)) 422 fdt_setprop(fdt, off, prop, val, len); 423 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen); 424 } 425 } 426 427 void do_fixup_by_prop_u32(void *fdt, 428 const char *pname, const void *pval, int plen, 429 const char *prop, u32 val, int create) 430 { 431 fdt32_t tmp = cpu_to_fdt32(val); 432 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create); 433 } 434 435 void do_fixup_by_compat(void *fdt, const char *compat, 436 const char *prop, const void *val, int len, int create) 437 { 438 int off = -1; 439 #if defined(DEBUG) 440 int i; 441 debug("Updating property '%s' = ", prop); 442 for (i = 0; i < len; i++) 443 debug(" %.2x", *(u8*)(val+i)); 444 debug("\n"); 445 #endif 446 off = fdt_node_offset_by_compatible(fdt, -1, compat); 447 while (off != -FDT_ERR_NOTFOUND) { 448 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL)) 449 fdt_setprop(fdt, off, prop, val, len); 450 off = fdt_node_offset_by_compatible(fdt, off, compat); 451 } 452 } 453 454 void do_fixup_by_compat_u32(void *fdt, const char *compat, 455 const char *prop, u32 val, int create) 456 { 457 fdt32_t tmp = cpu_to_fdt32(val); 458 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create); 459 } 460 461 #ifdef CONFIG_ARCH_FIXUP_FDT_MEMORY 462 /* 463 * fdt_pack_reg - pack address and size array into the "reg"-suitable stream 464 */ 465 static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size, 466 int n) 467 { 468 int i; 469 int address_cells = fdt_address_cells(fdt, 0); 470 int size_cells = fdt_size_cells(fdt, 0); 471 char *p = buf; 472 473 for (i = 0; i < n; i++) { 474 if (address_cells == 2) 475 *(fdt64_t *)p = cpu_to_fdt64(address[i]); 476 else 477 *(fdt32_t *)p = cpu_to_fdt32(address[i]); 478 p += 4 * address_cells; 479 480 if (size_cells == 2) 481 *(fdt64_t *)p = cpu_to_fdt64(size[i]); 482 else 483 *(fdt32_t *)p = cpu_to_fdt32(size[i]); 484 p += 4 * size_cells; 485 } 486 487 return p - (char *)buf; 488 } 489 490 int fdt_record_loadable(void *blob, u32 index, const char *name, 491 uintptr_t load_addr, u32 size, uintptr_t entry_point, 492 const char *type, const char *os) 493 { 494 int err, node; 495 496 err = fdt_check_header(blob); 497 if (err < 0) { 498 printf("%s: %s\n", __func__, fdt_strerror(err)); 499 return err; 500 } 501 502 /* find or create "/fit-images" node */ 503 node = fdt_find_or_add_subnode(blob, 0, "fit-images"); 504 if (node < 0) 505 return node; 506 507 /* find or create "/fit-images/<name>" node */ 508 node = fdt_find_or_add_subnode(blob, node, name); 509 if (node < 0) 510 return node; 511 512 /* 513 * We record these as 32bit entities, possibly truncating addresses. 514 * However, spl_fit.c is not 64bit safe either: i.e. we should not 515 * have an issue here. 516 */ 517 fdt_setprop_u32(blob, node, "load-addr", load_addr); 518 if (entry_point != -1) 519 fdt_setprop_u32(blob, node, "entry-point", entry_point); 520 fdt_setprop_u32(blob, node, "size", size); 521 if (type) 522 fdt_setprop_string(blob, node, "type", type); 523 if (os) 524 fdt_setprop_string(blob, node, "os", os); 525 526 return node; 527 } 528 529 #ifdef CONFIG_NR_DRAM_BANKS 530 #define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS 531 #else 532 #define MEMORY_BANKS_MAX 4 533 #endif 534 int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks) 535 { 536 int err, nodeoffset; 537 int len; 538 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */ 539 540 if (banks > MEMORY_BANKS_MAX) { 541 printf("%s: num banks %d exceeds hardcoded limit %d." 542 " Recompile with higher MEMORY_BANKS_MAX?\n", 543 __FUNCTION__, banks, MEMORY_BANKS_MAX); 544 return -1; 545 } 546 547 err = fdt_check_header(blob); 548 if (err < 0) { 549 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err)); 550 return err; 551 } 552 553 /* find or create "/memory" node. */ 554 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory"); 555 if (nodeoffset < 0) 556 return nodeoffset; 557 558 err = fdt_setprop(blob, nodeoffset, "device_type", "memory", 559 sizeof("memory")); 560 if (err < 0) { 561 printf("WARNING: could not set %s %s.\n", "device_type", 562 fdt_strerror(err)); 563 return err; 564 } 565 566 if (!banks) 567 return 0; 568 569 len = fdt_pack_reg(blob, tmp, start, size, banks); 570 571 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len); 572 if (err < 0) { 573 printf("WARNING: could not set %s %s.\n", 574 "reg", fdt_strerror(err)); 575 return err; 576 } 577 return 0; 578 } 579 580 int fdt_fixup_memory(void *blob, u64 start, u64 size) 581 { 582 return fdt_fixup_memory_banks(blob, &start, &size, 1); 583 } 584 585 int fdt_update_reserved_memory(void *blob, char *name, u64 start, u64 size) 586 { 587 int nodeoffset, len, err; 588 u8 tmp[16]; /* Up to 64-bit address + 64-bit size */ 589 590 #if 0 591 /*name is rockchip_logo*/ 592 nodeoffset = fdt_find_or_add_subnode(blob, 0, "reserved-memory"); 593 if (nodeoffset < 0) 594 return nodeoffset; 595 printf("hjc>>reserved-memory>>%s, nodeoffset:%d\n", __func__, nodeoffset); 596 nodeoffset = fdt_find_or_add_subnode(blob, nodeoffset, name); 597 if (nodeoffset < 0) 598 return nodeoffset; 599 #else 600 nodeoffset = fdt_node_offset_by_compatible(blob, 0, name); 601 if (nodeoffset < 0) 602 debug("Can't find nodeoffset: %d\n", nodeoffset); 603 #endif 604 len = fdt_pack_reg(blob, tmp, &start, &size, 1); 605 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len); 606 if (err < 0) { 607 printf("WARNING: could not set %s %s.\n", 608 "reg", fdt_strerror(err)); 609 return err; 610 } 611 612 return nodeoffset; 613 } 614 #endif 615 616 void fdt_fixup_ethernet(void *fdt) 617 { 618 int i = 0, j, prop; 619 char *tmp, *end; 620 char mac[16]; 621 const char *path; 622 unsigned char mac_addr[ARP_HLEN]; 623 int offset; 624 #ifdef FDT_SEQ_MACADDR_FROM_ENV 625 int nodeoff; 626 const struct fdt_property *fdt_prop; 627 #endif 628 629 if (fdt_path_offset(fdt, "/aliases") < 0) 630 return; 631 632 /* Cycle through all aliases */ 633 for (prop = 0; ; prop++) { 634 const char *name; 635 636 /* FDT might have been edited, recompute the offset */ 637 offset = fdt_first_property_offset(fdt, 638 fdt_path_offset(fdt, "/aliases")); 639 /* Select property number 'prop' */ 640 for (j = 0; j < prop; j++) 641 offset = fdt_next_property_offset(fdt, offset); 642 643 if (offset < 0) 644 break; 645 646 path = fdt_getprop_by_offset(fdt, offset, &name, NULL); 647 if (!strncmp(name, "ethernet", 8)) { 648 /* Treat plain "ethernet" same as "ethernet0". */ 649 if (!strcmp(name, "ethernet") 650 #ifdef FDT_SEQ_MACADDR_FROM_ENV 651 || !strcmp(name, "ethernet0") 652 #endif 653 ) 654 i = 0; 655 #ifndef FDT_SEQ_MACADDR_FROM_ENV 656 else 657 i = trailing_strtol(name); 658 #endif 659 if (i != -1) { 660 if (i == 0) 661 strcpy(mac, "ethaddr"); 662 else 663 sprintf(mac, "eth%daddr", i); 664 } else { 665 continue; 666 } 667 #ifdef FDT_SEQ_MACADDR_FROM_ENV 668 nodeoff = fdt_path_offset(fdt, path); 669 fdt_prop = fdt_get_property(fdt, nodeoff, "status", 670 NULL); 671 if (fdt_prop && !strcmp(fdt_prop->data, "disabled")) 672 continue; 673 i++; 674 #endif 675 tmp = env_get(mac); 676 if (!tmp) 677 continue; 678 679 for (j = 0; j < 6; j++) { 680 mac_addr[j] = tmp ? 681 simple_strtoul(tmp, &end, 16) : 0; 682 if (tmp) 683 tmp = (*end) ? end + 1 : end; 684 } 685 686 do_fixup_by_path(fdt, path, "mac-address", 687 &mac_addr, 6, 0); 688 do_fixup_by_path(fdt, path, "local-mac-address", 689 &mac_addr, 6, 1); 690 } 691 } 692 } 693 694 /* Resize the fdt to its actual size + a bit of padding */ 695 int fdt_shrink_to_minimum(void *blob, uint extrasize) 696 { 697 int i; 698 uint64_t addr, size; 699 int total, ret; 700 uint actualsize; 701 702 if (!blob) 703 return 0; 704 705 total = fdt_num_mem_rsv(blob); 706 for (i = 0; i < total; i++) { 707 fdt_get_mem_rsv(blob, i, &addr, &size); 708 if (addr == (uintptr_t)blob) { 709 fdt_del_mem_rsv(blob, i); 710 break; 711 } 712 } 713 714 /* 715 * Calculate the actual size of the fdt 716 * plus the size needed for 5 fdt_add_mem_rsv, one 717 * for the fdt itself and 4 for a possible initrd 718 * ((initrd-start + initrd-end) * 2 (name & value)) 719 */ 720 actualsize = fdt_off_dt_strings(blob) + 721 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry); 722 723 actualsize += extrasize; 724 /* Make it so the fdt ends on a page boundary */ 725 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000); 726 actualsize = actualsize - ((uintptr_t)blob & 0xfff); 727 728 /* Change the fdt header to reflect the correct size */ 729 fdt_set_totalsize(blob, actualsize); 730 731 /* Add the new reservation */ 732 ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize); 733 if (ret < 0) 734 return ret; 735 736 return actualsize; 737 } 738 739 #ifdef CONFIG_PCI 740 #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4 741 742 #define FDT_PCI_PREFETCH (0x40000000) 743 #define FDT_PCI_MEM32 (0x02000000) 744 #define FDT_PCI_IO (0x01000000) 745 #define FDT_PCI_MEM64 (0x03000000) 746 747 int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) { 748 749 int addrcell, sizecell, len, r; 750 u32 *dma_range; 751 /* sized based on pci addr cells, size-cells, & address-cells */ 752 u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN]; 753 754 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1); 755 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1); 756 757 dma_range = &dma_ranges[0]; 758 for (r = 0; r < hose->region_count; r++) { 759 u64 bus_start, phys_start, size; 760 761 /* skip if !PCI_REGION_SYS_MEMORY */ 762 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY)) 763 continue; 764 765 bus_start = (u64)hose->regions[r].bus_start; 766 phys_start = (u64)hose->regions[r].phys_start; 767 size = (u64)hose->regions[r].size; 768 769 dma_range[0] = 0; 770 if (size >= 0x100000000ull) 771 dma_range[0] |= FDT_PCI_MEM64; 772 else 773 dma_range[0] |= FDT_PCI_MEM32; 774 if (hose->regions[r].flags & PCI_REGION_PREFETCH) 775 dma_range[0] |= FDT_PCI_PREFETCH; 776 #ifdef CONFIG_SYS_PCI_64BIT 777 dma_range[1] = bus_start >> 32; 778 #else 779 dma_range[1] = 0; 780 #endif 781 dma_range[2] = bus_start & 0xffffffff; 782 783 if (addrcell == 2) { 784 dma_range[3] = phys_start >> 32; 785 dma_range[4] = phys_start & 0xffffffff; 786 } else { 787 dma_range[3] = phys_start & 0xffffffff; 788 } 789 790 if (sizecell == 2) { 791 dma_range[3 + addrcell + 0] = size >> 32; 792 dma_range[3 + addrcell + 1] = size & 0xffffffff; 793 } else { 794 dma_range[3 + addrcell + 0] = size & 0xffffffff; 795 } 796 797 dma_range += (3 + addrcell + sizecell); 798 } 799 800 len = dma_range - &dma_ranges[0]; 801 if (len) 802 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4); 803 804 return 0; 805 } 806 #endif 807 808 int fdt_increase_size(void *fdt, int add_len) 809 { 810 int newlen; 811 812 newlen = fdt_totalsize(fdt) + add_len; 813 814 /* Open in place with a new len */ 815 return fdt_open_into(fdt, fdt, newlen); 816 } 817 818 #ifdef CONFIG_FDT_FIXUP_PARTITIONS 819 #include <jffs2/load_kernel.h> 820 #include <mtd_node.h> 821 822 struct reg_cell { 823 unsigned int r0; 824 unsigned int r1; 825 }; 826 827 int fdt_del_subnodes(const void *blob, int parent_offset) 828 { 829 int off, ndepth; 830 int ret; 831 832 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth); 833 (off >= 0) && (ndepth > 0); 834 off = fdt_next_node(blob, off, &ndepth)) { 835 if (ndepth == 1) { 836 debug("delete %s: offset: %x\n", 837 fdt_get_name(blob, off, 0), off); 838 ret = fdt_del_node((void *)blob, off); 839 if (ret < 0) { 840 printf("Can't delete node: %s\n", 841 fdt_strerror(ret)); 842 return ret; 843 } else { 844 ndepth = 0; 845 off = parent_offset; 846 } 847 } 848 } 849 return 0; 850 } 851 852 int fdt_del_partitions(void *blob, int parent_offset) 853 { 854 const void *prop; 855 int ndepth = 0; 856 int off; 857 int ret; 858 859 off = fdt_next_node(blob, parent_offset, &ndepth); 860 if (off > 0 && ndepth == 1) { 861 prop = fdt_getprop(blob, off, "label", NULL); 862 if (prop == NULL) { 863 /* 864 * Could not find label property, nand {}; node? 865 * Check subnode, delete partitions there if any. 866 */ 867 return fdt_del_partitions(blob, off); 868 } else { 869 ret = fdt_del_subnodes(blob, parent_offset); 870 if (ret < 0) { 871 printf("Can't remove subnodes: %s\n", 872 fdt_strerror(ret)); 873 return ret; 874 } 875 } 876 } 877 return 0; 878 } 879 880 int fdt_node_set_part_info(void *blob, int parent_offset, 881 struct mtd_device *dev) 882 { 883 struct list_head *pentry; 884 struct part_info *part; 885 struct reg_cell cell; 886 int off, ndepth = 0; 887 int part_num, ret; 888 char buf[64]; 889 890 ret = fdt_del_partitions(blob, parent_offset); 891 if (ret < 0) 892 return ret; 893 894 /* 895 * Check if it is nand {}; subnode, adjust 896 * the offset in this case 897 */ 898 off = fdt_next_node(blob, parent_offset, &ndepth); 899 if (off > 0 && ndepth == 1) 900 parent_offset = off; 901 902 part_num = 0; 903 list_for_each_prev(pentry, &dev->parts) { 904 int newoff; 905 906 part = list_entry(pentry, struct part_info, link); 907 908 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n", 909 part_num, part->name, part->size, 910 part->offset, part->mask_flags); 911 912 sprintf(buf, "partition@%llx", part->offset); 913 add_sub: 914 ret = fdt_add_subnode(blob, parent_offset, buf); 915 if (ret == -FDT_ERR_NOSPACE) { 916 ret = fdt_increase_size(blob, 512); 917 if (!ret) 918 goto add_sub; 919 else 920 goto err_size; 921 } else if (ret < 0) { 922 printf("Can't add partition node: %s\n", 923 fdt_strerror(ret)); 924 return ret; 925 } 926 newoff = ret; 927 928 /* Check MTD_WRITEABLE_CMD flag */ 929 if (part->mask_flags & 1) { 930 add_ro: 931 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0); 932 if (ret == -FDT_ERR_NOSPACE) { 933 ret = fdt_increase_size(blob, 512); 934 if (!ret) 935 goto add_ro; 936 else 937 goto err_size; 938 } else if (ret < 0) 939 goto err_prop; 940 } 941 942 cell.r0 = cpu_to_fdt32(part->offset); 943 cell.r1 = cpu_to_fdt32(part->size); 944 add_reg: 945 ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell)); 946 if (ret == -FDT_ERR_NOSPACE) { 947 ret = fdt_increase_size(blob, 512); 948 if (!ret) 949 goto add_reg; 950 else 951 goto err_size; 952 } else if (ret < 0) 953 goto err_prop; 954 955 add_label: 956 ret = fdt_setprop_string(blob, newoff, "label", part->name); 957 if (ret == -FDT_ERR_NOSPACE) { 958 ret = fdt_increase_size(blob, 512); 959 if (!ret) 960 goto add_label; 961 else 962 goto err_size; 963 } else if (ret < 0) 964 goto err_prop; 965 966 part_num++; 967 } 968 return 0; 969 err_size: 970 printf("Can't increase blob size: %s\n", fdt_strerror(ret)); 971 return ret; 972 err_prop: 973 printf("Can't add property: %s\n", fdt_strerror(ret)); 974 return ret; 975 } 976 977 /* 978 * Update partitions in nor/nand nodes using info from 979 * mtdparts environment variable. The nodes to update are 980 * specified by node_info structure which contains mtd device 981 * type and compatible string: E. g. the board code in 982 * ft_board_setup() could use: 983 * 984 * struct node_info nodes[] = { 985 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, }, 986 * { "cfi-flash", MTD_DEV_TYPE_NOR, }, 987 * }; 988 * 989 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes)); 990 */ 991 void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size) 992 { 993 struct node_info *ni = node_info; 994 struct mtd_device *dev; 995 int i, idx; 996 int noff; 997 998 if (mtdparts_init() != 0) 999 return; 1000 1001 for (i = 0; i < node_info_size; i++) { 1002 idx = 0; 1003 noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat); 1004 while (noff != -FDT_ERR_NOTFOUND) { 1005 debug("%s: %s, mtd dev type %d\n", 1006 fdt_get_name(blob, noff, 0), 1007 ni[i].compat, ni[i].type); 1008 dev = device_find(ni[i].type, idx++); 1009 if (dev) { 1010 if (fdt_node_set_part_info(blob, noff, dev)) 1011 return; /* return on error */ 1012 } 1013 1014 /* Jump to next flash node */ 1015 noff = fdt_node_offset_by_compatible(blob, noff, 1016 ni[i].compat); 1017 } 1018 } 1019 } 1020 #endif 1021 1022 void fdt_del_node_and_alias(void *blob, const char *alias) 1023 { 1024 int off = fdt_path_offset(blob, alias); 1025 1026 if (off < 0) 1027 return; 1028 1029 fdt_del_node(blob, off); 1030 1031 off = fdt_path_offset(blob, "/aliases"); 1032 fdt_delprop(blob, off, alias); 1033 } 1034 1035 /* Max address size we deal with */ 1036 #define OF_MAX_ADDR_CELLS 4 1037 #define OF_BAD_ADDR FDT_ADDR_T_NONE 1038 #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \ 1039 (ns) > 0) 1040 1041 /* Debug utility */ 1042 #ifdef DEBUG 1043 static void of_dump_addr(const char *s, const fdt32_t *addr, int na) 1044 { 1045 printf("%s", s); 1046 while(na--) 1047 printf(" %08x", *(addr++)); 1048 printf("\n"); 1049 } 1050 #else 1051 static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { } 1052 #endif 1053 1054 /** 1055 * struct of_bus - Callbacks for bus specific translators 1056 * @name: A string used to identify this bus in debug output. 1057 * @addresses: The name of the DT property from which addresses are 1058 * to be read, typically "reg". 1059 * @match: Return non-zero if the node whose parent is at 1060 * parentoffset in the FDT blob corresponds to a bus 1061 * of this type, otherwise return zero. If NULL a match 1062 * is assumed. 1063 * @count_cells:Count how many cells (be32 values) a node whose parent 1064 * is at parentoffset in the FDT blob will require to 1065 * represent its address (written to *addrc) & size 1066 * (written to *sizec). 1067 * @map: Map the address addr from the address space of this 1068 * bus to that of its parent, making use of the ranges 1069 * read from DT to an array at range. na and ns are the 1070 * number of cells (be32 values) used to hold and address 1071 * or size, respectively, for this bus. pna is the number 1072 * of cells used to hold an address for the parent bus. 1073 * Returns the address in the address space of the parent 1074 * bus. 1075 * @translate: Update the value of the address cells at addr within an 1076 * FDT by adding offset to it. na specifies the number of 1077 * cells used to hold the address being translated. Returns 1078 * zero on success, non-zero on error. 1079 * 1080 * Each bus type will include a struct of_bus in the of_busses array, 1081 * providing implementations of some or all of the functions used to 1082 * match the bus & handle address translation for its children. 1083 */ 1084 struct of_bus { 1085 const char *name; 1086 const char *addresses; 1087 int (*match)(const void *blob, int parentoffset); 1088 void (*count_cells)(const void *blob, int parentoffset, 1089 int *addrc, int *sizec); 1090 u64 (*map)(fdt32_t *addr, const fdt32_t *range, 1091 int na, int ns, int pna); 1092 int (*translate)(fdt32_t *addr, u64 offset, int na); 1093 }; 1094 1095 /* Default translator (generic bus) */ 1096 void fdt_support_default_count_cells(const void *blob, int parentoffset, 1097 int *addrc, int *sizec) 1098 { 1099 const fdt32_t *prop; 1100 1101 if (addrc) 1102 *addrc = fdt_address_cells(blob, parentoffset); 1103 1104 if (sizec) { 1105 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL); 1106 if (prop) 1107 *sizec = be32_to_cpup(prop); 1108 else 1109 *sizec = 1; 1110 } 1111 } 1112 1113 static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range, 1114 int na, int ns, int pna) 1115 { 1116 u64 cp, s, da; 1117 1118 cp = fdt_read_number(range, na); 1119 s = fdt_read_number(range + na + pna, ns); 1120 da = fdt_read_number(addr, na); 1121 1122 debug("OF: default map, cp=%" PRIu64 ", s=%" PRIu64 1123 ", da=%" PRIu64 "\n", cp, s, da); 1124 1125 if (da < cp || da >= (cp + s)) 1126 return OF_BAD_ADDR; 1127 return da - cp; 1128 } 1129 1130 static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na) 1131 { 1132 u64 a = fdt_read_number(addr, na); 1133 memset(addr, 0, na * 4); 1134 a += offset; 1135 if (na > 1) 1136 addr[na - 2] = cpu_to_fdt32(a >> 32); 1137 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu); 1138 1139 return 0; 1140 } 1141 1142 #ifdef CONFIG_OF_ISA_BUS 1143 1144 /* ISA bus translator */ 1145 static int of_bus_isa_match(const void *blob, int parentoffset) 1146 { 1147 const char *name; 1148 1149 name = fdt_get_name(blob, parentoffset, NULL); 1150 if (!name) 1151 return 0; 1152 1153 return !strcmp(name, "isa"); 1154 } 1155 1156 static void of_bus_isa_count_cells(const void *blob, int parentoffset, 1157 int *addrc, int *sizec) 1158 { 1159 if (addrc) 1160 *addrc = 2; 1161 if (sizec) 1162 *sizec = 1; 1163 } 1164 1165 static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range, 1166 int na, int ns, int pna) 1167 { 1168 u64 cp, s, da; 1169 1170 /* Check address type match */ 1171 if ((addr[0] ^ range[0]) & cpu_to_be32(1)) 1172 return OF_BAD_ADDR; 1173 1174 cp = fdt_read_number(range + 1, na - 1); 1175 s = fdt_read_number(range + na + pna, ns); 1176 da = fdt_read_number(addr + 1, na - 1); 1177 1178 debug("OF: ISA map, cp=%" PRIu64 ", s=%" PRIu64 1179 ", da=%" PRIu64 "\n", cp, s, da); 1180 1181 if (da < cp || da >= (cp + s)) 1182 return OF_BAD_ADDR; 1183 return da - cp; 1184 } 1185 1186 static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na) 1187 { 1188 return of_bus_default_translate(addr + 1, offset, na - 1); 1189 } 1190 1191 #endif /* CONFIG_OF_ISA_BUS */ 1192 1193 /* Array of bus specific translators */ 1194 static struct of_bus of_busses[] = { 1195 #ifdef CONFIG_OF_ISA_BUS 1196 /* ISA */ 1197 { 1198 .name = "isa", 1199 .addresses = "reg", 1200 .match = of_bus_isa_match, 1201 .count_cells = of_bus_isa_count_cells, 1202 .map = of_bus_isa_map, 1203 .translate = of_bus_isa_translate, 1204 }, 1205 #endif /* CONFIG_OF_ISA_BUS */ 1206 /* Default */ 1207 { 1208 .name = "default", 1209 .addresses = "reg", 1210 .count_cells = fdt_support_default_count_cells, 1211 .map = of_bus_default_map, 1212 .translate = of_bus_default_translate, 1213 }, 1214 }; 1215 1216 static struct of_bus *of_match_bus(const void *blob, int parentoffset) 1217 { 1218 struct of_bus *bus; 1219 1220 if (ARRAY_SIZE(of_busses) == 1) 1221 return of_busses; 1222 1223 for (bus = of_busses; bus; bus++) { 1224 if (!bus->match || bus->match(blob, parentoffset)) 1225 return bus; 1226 } 1227 1228 /* 1229 * We should always have matched the default bus at least, since 1230 * it has a NULL match field. If we didn't then it somehow isn't 1231 * in the of_busses array or something equally catastrophic has 1232 * gone wrong. 1233 */ 1234 assert(0); 1235 return NULL; 1236 } 1237 1238 static int of_translate_one(const void *blob, int parent, struct of_bus *bus, 1239 struct of_bus *pbus, fdt32_t *addr, 1240 int na, int ns, int pna, const char *rprop) 1241 { 1242 const fdt32_t *ranges; 1243 int rlen; 1244 int rone; 1245 u64 offset = OF_BAD_ADDR; 1246 1247 /* Normally, an absence of a "ranges" property means we are 1248 * crossing a non-translatable boundary, and thus the addresses 1249 * below the current not cannot be converted to CPU physical ones. 1250 * Unfortunately, while this is very clear in the spec, it's not 1251 * what Apple understood, and they do have things like /uni-n or 1252 * /ht nodes with no "ranges" property and a lot of perfectly 1253 * useable mapped devices below them. Thus we treat the absence of 1254 * "ranges" as equivalent to an empty "ranges" property which means 1255 * a 1:1 translation at that level. It's up to the caller not to try 1256 * to translate addresses that aren't supposed to be translated in 1257 * the first place. --BenH. 1258 */ 1259 ranges = fdt_getprop(blob, parent, rprop, &rlen); 1260 if (ranges == NULL || rlen == 0) { 1261 offset = fdt_read_number(addr, na); 1262 memset(addr, 0, pna * 4); 1263 debug("OF: no ranges, 1:1 translation\n"); 1264 goto finish; 1265 } 1266 1267 debug("OF: walking ranges...\n"); 1268 1269 /* Now walk through the ranges */ 1270 rlen /= 4; 1271 rone = na + pna + ns; 1272 for (; rlen >= rone; rlen -= rone, ranges += rone) { 1273 offset = bus->map(addr, ranges, na, ns, pna); 1274 if (offset != OF_BAD_ADDR) 1275 break; 1276 } 1277 if (offset == OF_BAD_ADDR) { 1278 debug("OF: not found !\n"); 1279 return 1; 1280 } 1281 memcpy(addr, ranges + na, 4 * pna); 1282 1283 finish: 1284 of_dump_addr("OF: parent translation for:", addr, pna); 1285 debug("OF: with offset: %" PRIu64 "\n", offset); 1286 1287 /* Translate it into parent bus space */ 1288 return pbus->translate(addr, offset, pna); 1289 } 1290 1291 /* 1292 * Translate an address from the device-tree into a CPU physical address, 1293 * this walks up the tree and applies the various bus mappings on the 1294 * way. 1295 * 1296 * Note: We consider that crossing any level with #size-cells == 0 to mean 1297 * that translation is impossible (that is we are not dealing with a value 1298 * that can be mapped to a cpu physical address). This is not really specified 1299 * that way, but this is traditionally the way IBM at least do things 1300 */ 1301 static u64 __of_translate_address(const void *blob, int node_offset, 1302 const fdt32_t *in_addr, const char *rprop) 1303 { 1304 int parent; 1305 struct of_bus *bus, *pbus; 1306 fdt32_t addr[OF_MAX_ADDR_CELLS]; 1307 int na, ns, pna, pns; 1308 u64 result = OF_BAD_ADDR; 1309 1310 debug("OF: ** translation for device %s **\n", 1311 fdt_get_name(blob, node_offset, NULL)); 1312 1313 /* Get parent & match bus type */ 1314 parent = fdt_parent_offset(blob, node_offset); 1315 if (parent < 0) 1316 goto bail; 1317 bus = of_match_bus(blob, parent); 1318 1319 /* Cound address cells & copy address locally */ 1320 bus->count_cells(blob, parent, &na, &ns); 1321 if (!OF_CHECK_COUNTS(na, ns)) { 1322 printf("%s: Bad cell count for %s\n", __FUNCTION__, 1323 fdt_get_name(blob, node_offset, NULL)); 1324 goto bail; 1325 } 1326 memcpy(addr, in_addr, na * 4); 1327 1328 debug("OF: bus is %s (na=%d, ns=%d) on %s\n", 1329 bus->name, na, ns, fdt_get_name(blob, parent, NULL)); 1330 of_dump_addr("OF: translating address:", addr, na); 1331 1332 /* Translate */ 1333 for (;;) { 1334 /* Switch to parent bus */ 1335 node_offset = parent; 1336 parent = fdt_parent_offset(blob, node_offset); 1337 1338 /* If root, we have finished */ 1339 if (parent < 0) { 1340 debug("OF: reached root node\n"); 1341 result = fdt_read_number(addr, na); 1342 break; 1343 } 1344 1345 /* Get new parent bus and counts */ 1346 pbus = of_match_bus(blob, parent); 1347 pbus->count_cells(blob, parent, &pna, &pns); 1348 if (!OF_CHECK_COUNTS(pna, pns)) { 1349 printf("%s: Bad cell count for %s\n", __FUNCTION__, 1350 fdt_get_name(blob, node_offset, NULL)); 1351 break; 1352 } 1353 1354 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n", 1355 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL)); 1356 1357 /* Apply bus translation */ 1358 if (of_translate_one(blob, node_offset, bus, pbus, 1359 addr, na, ns, pna, rprop)) 1360 break; 1361 1362 /* Complete the move up one level */ 1363 na = pna; 1364 ns = pns; 1365 bus = pbus; 1366 1367 of_dump_addr("OF: one level translation:", addr, na); 1368 } 1369 bail: 1370 1371 return result; 1372 } 1373 1374 u64 fdt_translate_address(const void *blob, int node_offset, 1375 const fdt32_t *in_addr) 1376 { 1377 return __of_translate_address(blob, node_offset, in_addr, "ranges"); 1378 } 1379 1380 /** 1381 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and 1382 * who's reg property matches a physical cpu address 1383 * 1384 * @blob: ptr to device tree 1385 * @compat: compatiable string to match 1386 * @compat_off: property name 1387 * 1388 */ 1389 int fdt_node_offset_by_compat_reg(void *blob, const char *compat, 1390 phys_addr_t compat_off) 1391 { 1392 int len, off = fdt_node_offset_by_compatible(blob, -1, compat); 1393 while (off != -FDT_ERR_NOTFOUND) { 1394 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len); 1395 if (reg) { 1396 if (compat_off == fdt_translate_address(blob, off, reg)) 1397 return off; 1398 } 1399 off = fdt_node_offset_by_compatible(blob, off, compat); 1400 } 1401 1402 return -FDT_ERR_NOTFOUND; 1403 } 1404 1405 /** 1406 * fdt_alloc_phandle: Return next free phandle value 1407 * 1408 * @blob: ptr to device tree 1409 */ 1410 int fdt_alloc_phandle(void *blob) 1411 { 1412 int offset; 1413 uint32_t phandle = 0; 1414 1415 for (offset = fdt_next_node(blob, -1, NULL); offset >= 0; 1416 offset = fdt_next_node(blob, offset, NULL)) { 1417 phandle = max(phandle, fdt_get_phandle(blob, offset)); 1418 } 1419 1420 return phandle + 1; 1421 } 1422 1423 /* 1424 * fdt_set_phandle: Create a phandle property for the given node 1425 * 1426 * @fdt: ptr to device tree 1427 * @nodeoffset: node to update 1428 * @phandle: phandle value to set (must be unique) 1429 */ 1430 int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle) 1431 { 1432 int ret; 1433 1434 #ifdef DEBUG 1435 int off = fdt_node_offset_by_phandle(fdt, phandle); 1436 1437 if ((off >= 0) && (off != nodeoffset)) { 1438 char buf[64]; 1439 1440 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf)); 1441 printf("Trying to update node %s with phandle %u ", 1442 buf, phandle); 1443 1444 fdt_get_path(fdt, off, buf, sizeof(buf)); 1445 printf("that already exists in node %s.\n", buf); 1446 return -FDT_ERR_BADPHANDLE; 1447 } 1448 #endif 1449 1450 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle); 1451 if (ret < 0) 1452 return ret; 1453 1454 /* 1455 * For now, also set the deprecated "linux,phandle" property, so that we 1456 * don't break older kernels. 1457 */ 1458 ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle); 1459 1460 return ret; 1461 } 1462 1463 /* 1464 * fdt_create_phandle: Create a phandle property for the given node 1465 * 1466 * @fdt: ptr to device tree 1467 * @nodeoffset: node to update 1468 */ 1469 unsigned int fdt_create_phandle(void *fdt, int nodeoffset) 1470 { 1471 /* see if there is a phandle already */ 1472 int phandle = fdt_get_phandle(fdt, nodeoffset); 1473 1474 /* if we got 0, means no phandle so create one */ 1475 if (phandle == 0) { 1476 int ret; 1477 1478 phandle = fdt_alloc_phandle(fdt); 1479 ret = fdt_set_phandle(fdt, nodeoffset, phandle); 1480 if (ret < 0) { 1481 printf("Can't set phandle %u: %s\n", phandle, 1482 fdt_strerror(ret)); 1483 return 0; 1484 } 1485 } 1486 1487 return phandle; 1488 } 1489 1490 /* 1491 * fdt_set_node_status: Set status for the given node 1492 * 1493 * @fdt: ptr to device tree 1494 * @nodeoffset: node to update 1495 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, 1496 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE 1497 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE 1498 */ 1499 int fdt_set_node_status(void *fdt, int nodeoffset, 1500 enum fdt_status status, unsigned int error_code) 1501 { 1502 char buf[16]; 1503 int ret = 0; 1504 1505 if (nodeoffset < 0) 1506 return nodeoffset; 1507 1508 switch (status) { 1509 case FDT_STATUS_OKAY: 1510 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay"); 1511 break; 1512 case FDT_STATUS_DISABLED: 1513 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled"); 1514 break; 1515 case FDT_STATUS_FAIL: 1516 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail"); 1517 break; 1518 case FDT_STATUS_FAIL_ERROR_CODE: 1519 sprintf(buf, "fail-%d", error_code); 1520 ret = fdt_setprop_string(fdt, nodeoffset, "status", buf); 1521 break; 1522 default: 1523 printf("Invalid fdt status: %x\n", status); 1524 ret = -1; 1525 break; 1526 } 1527 1528 return ret; 1529 } 1530 1531 /* 1532 * fdt_set_status_by_alias: Set status for the given node given an alias 1533 * 1534 * @fdt: ptr to device tree 1535 * @alias: alias of node to update 1536 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, 1537 * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE 1538 * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE 1539 */ 1540 int fdt_set_status_by_alias(void *fdt, const char* alias, 1541 enum fdt_status status, unsigned int error_code) 1542 { 1543 int offset = fdt_path_offset(fdt, alias); 1544 1545 return fdt_set_node_status(fdt, offset, status, error_code); 1546 } 1547 1548 #if defined(CONFIG_VIDEO) || defined(CONFIG_LCD) 1549 int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf) 1550 { 1551 int noff; 1552 int ret; 1553 1554 noff = fdt_node_offset_by_compatible(blob, -1, compat); 1555 if (noff != -FDT_ERR_NOTFOUND) { 1556 debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat); 1557 add_edid: 1558 ret = fdt_setprop(blob, noff, "edid", edid_buf, 128); 1559 if (ret == -FDT_ERR_NOSPACE) { 1560 ret = fdt_increase_size(blob, 512); 1561 if (!ret) 1562 goto add_edid; 1563 else 1564 goto err_size; 1565 } else if (ret < 0) { 1566 printf("Can't add property: %s\n", fdt_strerror(ret)); 1567 return ret; 1568 } 1569 } 1570 return 0; 1571 err_size: 1572 printf("Can't increase blob size: %s\n", fdt_strerror(ret)); 1573 return ret; 1574 } 1575 #endif 1576 1577 /* 1578 * Verify the physical address of device tree node for a given alias 1579 * 1580 * This function locates the device tree node of a given alias, and then 1581 * verifies that the physical address of that device matches the given 1582 * parameter. It displays a message if there is a mismatch. 1583 * 1584 * Returns 1 on success, 0 on failure 1585 */ 1586 int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr) 1587 { 1588 const char *path; 1589 const fdt32_t *reg; 1590 int node, len; 1591 u64 dt_addr; 1592 1593 path = fdt_getprop(fdt, anode, alias, NULL); 1594 if (!path) { 1595 /* If there's no such alias, then it's not a failure */ 1596 return 1; 1597 } 1598 1599 node = fdt_path_offset(fdt, path); 1600 if (node < 0) { 1601 printf("Warning: device tree alias '%s' points to invalid " 1602 "node %s.\n", alias, path); 1603 return 0; 1604 } 1605 1606 reg = fdt_getprop(fdt, node, "reg", &len); 1607 if (!reg) { 1608 printf("Warning: device tree node '%s' has no address.\n", 1609 path); 1610 return 0; 1611 } 1612 1613 dt_addr = fdt_translate_address(fdt, node, reg); 1614 if (addr != dt_addr) { 1615 printf("Warning: U-Boot configured device %s at address %" 1616 PRIx64 ",\n but the device tree has it address %" 1617 PRIx64 ".\n", alias, addr, dt_addr); 1618 return 0; 1619 } 1620 1621 return 1; 1622 } 1623 1624 /* 1625 * Returns the base address of an SOC or PCI node 1626 */ 1627 u64 fdt_get_base_address(const void *fdt, int node) 1628 { 1629 int size; 1630 const fdt32_t *prop; 1631 1632 prop = fdt_getprop(fdt, node, "reg", &size); 1633 1634 return prop ? fdt_translate_address(fdt, node, prop) : 0; 1635 } 1636 1637 /* 1638 * Read a property of size <prop_len>. Currently only supports 1 or 2 cells. 1639 */ 1640 static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off, 1641 uint64_t *val, int cells) 1642 { 1643 const fdt32_t *prop32 = &prop[cell_off]; 1644 const fdt64_t *prop64 = (const fdt64_t *)&prop[cell_off]; 1645 1646 if ((cell_off + cells) > prop_len) 1647 return -FDT_ERR_NOSPACE; 1648 1649 switch (cells) { 1650 case 1: 1651 *val = fdt32_to_cpu(*prop32); 1652 break; 1653 case 2: 1654 *val = fdt64_to_cpu(*prop64); 1655 break; 1656 default: 1657 return -FDT_ERR_NOSPACE; 1658 } 1659 1660 return 0; 1661 } 1662 1663 /** 1664 * fdt_read_range - Read a node's n'th range property 1665 * 1666 * @fdt: ptr to device tree 1667 * @node: offset of node 1668 * @n: range index 1669 * @child_addr: pointer to storage for the "child address" field 1670 * @addr: pointer to storage for the CPU view translated physical start 1671 * @len: pointer to storage for the range length 1672 * 1673 * Convenience function that reads and interprets a specific range out of 1674 * a number of the "ranges" property array. 1675 */ 1676 int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr, 1677 uint64_t *addr, uint64_t *len) 1678 { 1679 int pnode = fdt_parent_offset(fdt, node); 1680 const fdt32_t *ranges; 1681 int pacells; 1682 int acells; 1683 int scells; 1684 int ranges_len; 1685 int cell = 0; 1686 int r = 0; 1687 1688 /* 1689 * The "ranges" property is an array of 1690 * { <child address> <parent address> <size in child address space> } 1691 * 1692 * All 3 elements can span a diffent number of cells. Fetch their size. 1693 */ 1694 pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1); 1695 acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1); 1696 scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1); 1697 1698 /* Now try to get the ranges property */ 1699 ranges = fdt_getprop(fdt, node, "ranges", &ranges_len); 1700 if (!ranges) 1701 return -FDT_ERR_NOTFOUND; 1702 ranges_len /= sizeof(uint32_t); 1703 1704 /* Jump to the n'th entry */ 1705 cell = n * (pacells + acells + scells); 1706 1707 /* Read <child address> */ 1708 if (child_addr) { 1709 r = fdt_read_prop(ranges, ranges_len, cell, child_addr, 1710 acells); 1711 if (r) 1712 return r; 1713 } 1714 cell += acells; 1715 1716 /* Read <parent address> */ 1717 if (addr) 1718 *addr = fdt_translate_address(fdt, node, ranges + cell); 1719 cell += pacells; 1720 1721 /* Read <size in child address space> */ 1722 if (len) { 1723 r = fdt_read_prop(ranges, ranges_len, cell, len, scells); 1724 if (r) 1725 return r; 1726 } 1727 1728 return 0; 1729 } 1730 1731 /** 1732 * fdt_setup_simplefb_node - Fill and enable a simplefb node 1733 * 1734 * @fdt: ptr to device tree 1735 * @node: offset of the simplefb node 1736 * @base_address: framebuffer base address 1737 * @width: width in pixels 1738 * @height: height in pixels 1739 * @stride: bytes per line 1740 * @format: pixel format string 1741 * 1742 * Convenience function to fill and enable a simplefb node. 1743 */ 1744 int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width, 1745 u32 height, u32 stride, const char *format) 1746 { 1747 char name[32]; 1748 fdt32_t cells[4]; 1749 int i, addrc, sizec, ret; 1750 1751 fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node), 1752 &addrc, &sizec); 1753 i = 0; 1754 if (addrc == 2) 1755 cells[i++] = cpu_to_fdt32(base_address >> 32); 1756 cells[i++] = cpu_to_fdt32(base_address); 1757 if (sizec == 2) 1758 cells[i++] = 0; 1759 cells[i++] = cpu_to_fdt32(height * stride); 1760 1761 ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i); 1762 if (ret < 0) 1763 return ret; 1764 1765 snprintf(name, sizeof(name), "framebuffer@%" PRIx64, base_address); 1766 ret = fdt_set_name(fdt, node, name); 1767 if (ret < 0) 1768 return ret; 1769 1770 ret = fdt_setprop_u32(fdt, node, "width", width); 1771 if (ret < 0) 1772 return ret; 1773 1774 ret = fdt_setprop_u32(fdt, node, "height", height); 1775 if (ret < 0) 1776 return ret; 1777 1778 ret = fdt_setprop_u32(fdt, node, "stride", stride); 1779 if (ret < 0) 1780 return ret; 1781 1782 ret = fdt_setprop_string(fdt, node, "format", format); 1783 if (ret < 0) 1784 return ret; 1785 1786 ret = fdt_setprop_string(fdt, node, "status", "okay"); 1787 if (ret < 0) 1788 return ret; 1789 1790 return 0; 1791 } 1792 1793 /* 1794 * Update native-mode in display-timings from display environment variable. 1795 * The node to update are specified by path. 1796 */ 1797 int fdt_fixup_display(void *blob, const char *path, const char *display) 1798 { 1799 int off, toff; 1800 1801 if (!display || !path) 1802 return -FDT_ERR_NOTFOUND; 1803 1804 toff = fdt_path_offset(blob, path); 1805 if (toff >= 0) 1806 toff = fdt_subnode_offset(blob, toff, "display-timings"); 1807 if (toff < 0) 1808 return toff; 1809 1810 for (off = fdt_first_subnode(blob, toff); 1811 off >= 0; 1812 off = fdt_next_subnode(blob, off)) { 1813 uint32_t h = fdt_get_phandle(blob, off); 1814 debug("%s:0x%x\n", fdt_get_name(blob, off, NULL), 1815 fdt32_to_cpu(h)); 1816 if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0) 1817 return fdt_setprop_u32(blob, toff, "native-mode", h); 1818 } 1819 return toff; 1820 } 1821 1822 #ifdef CONFIG_OF_LIBFDT_OVERLAY 1823 /** 1824 * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting 1825 * 1826 * @fdt: ptr to device tree 1827 * @fdto: ptr to device tree overlay 1828 * 1829 * Convenience function to apply an overlay and display helpful messages 1830 * in the case of an error 1831 */ 1832 int fdt_overlay_apply_verbose(void *fdt, void *fdto) 1833 { 1834 int err; 1835 bool has_symbols; 1836 1837 err = fdt_path_offset(fdt, "/__symbols__"); 1838 has_symbols = err >= 0; 1839 1840 err = fdt_overlay_apply(fdt, fdto); 1841 if (err < 0) { 1842 printf("failed on fdt_overlay_apply(): %s\n", 1843 fdt_strerror(err)); 1844 if (!has_symbols) { 1845 printf("base fdt does did not have a /__symbols__ node\n"); 1846 printf("make sure you've compiled with -@\n"); 1847 } 1848 } 1849 return err; 1850 } 1851 #endif 1852