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