1 /* 2 * Originally from Linux v4.9 3 * Paul Mackerras August 1996. 4 * Copyright (C) 1996-2005 Paul Mackerras. 5 * 6 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner. 7 * {engebret|bergner}@us.ibm.com 8 * 9 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net 10 * 11 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and 12 * Grant Likely. 13 * 14 * Modified for U-Boot 15 * Copyright (c) 2017 Google, Inc 16 * 17 * This file follows drivers/of/base.c with functions in the same order as the 18 * Linux version. 19 * 20 * SPDX-License-Identifier: GPL-2.0+ 21 */ 22 23 #include <common.h> 24 #include <linux/libfdt.h> 25 #include <dm/of_access.h> 26 #include <linux/ctype.h> 27 #include <linux/err.h> 28 #include <linux/ioport.h> 29 30 DECLARE_GLOBAL_DATA_PTR; 31 32 /* list of struct alias_prop aliases */ 33 LIST_HEAD(aliases_lookup); 34 35 /* "/aliaes" node */ 36 static struct device_node *of_aliases; 37 38 /* "/chosen" node */ 39 static struct device_node *of_chosen; 40 41 /* node pointed to by the stdout-path alias */ 42 static struct device_node *of_stdout; 43 44 /* pointer to options given after the alias (separated by :) or NULL if none */ 45 static const char *of_stdout_options; 46 47 /** 48 * struct alias_prop - Alias property in 'aliases' node 49 * 50 * The structure represents one alias property of 'aliases' node as 51 * an entry in aliases_lookup list. 52 * 53 * @link: List node to link the structure in aliases_lookup list 54 * @alias: Alias property name 55 * @np: Pointer to device_node that the alias stands for 56 * @id: Index value from end of alias name 57 * @stem: Alias string without the index 58 */ 59 struct alias_prop { 60 struct list_head link; 61 const char *alias; 62 struct device_node *np; 63 int id; 64 char stem[0]; 65 }; 66 67 int of_n_addr_cells(const struct device_node *np) 68 { 69 const __be32 *ip; 70 71 do { 72 if (np->parent) 73 np = np->parent; 74 ip = of_get_property(np, "#address-cells", NULL); 75 if (ip) 76 return be32_to_cpup(ip); 77 } while (np->parent); 78 79 /* No #address-cells property for the root node */ 80 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT; 81 } 82 83 int of_n_size_cells(const struct device_node *np) 84 { 85 const __be32 *ip; 86 87 do { 88 if (np->parent) 89 np = np->parent; 90 ip = of_get_property(np, "#size-cells", NULL); 91 if (ip) 92 return be32_to_cpup(ip); 93 } while (np->parent); 94 95 /* No #size-cells property for the root node */ 96 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT; 97 } 98 99 int of_simple_addr_cells(const struct device_node *np) 100 { 101 const __be32 *ip; 102 103 ip = of_get_property(np, "#address-cells", NULL); 104 if (ip) 105 return be32_to_cpup(ip); 106 107 /* Return a default of 2 to match fdt_address_cells()*/ 108 return 2; 109 } 110 111 int of_simple_size_cells(const struct device_node *np) 112 { 113 const __be32 *ip; 114 115 ip = of_get_property(np, "#size-cells", NULL); 116 if (ip) 117 return be32_to_cpup(ip); 118 119 /* Return a default of 2 to match fdt_size_cells()*/ 120 return 2; 121 } 122 123 struct property *of_find_property(const struct device_node *np, 124 const char *name, int *lenp) 125 { 126 struct property *pp; 127 128 if (!np) 129 return NULL; 130 131 for (pp = np->properties; pp; pp = pp->next) { 132 if (strcmp(pp->name, name) == 0) { 133 if (lenp) 134 *lenp = pp->length; 135 break; 136 } 137 } 138 if (!pp && lenp) 139 *lenp = -FDT_ERR_NOTFOUND; 140 141 return pp; 142 } 143 144 struct device_node *of_find_all_nodes(struct device_node *prev) 145 { 146 struct device_node *np; 147 148 if (!prev) { 149 np = gd->of_root; 150 } else if (prev->child) { 151 np = prev->child; 152 } else { 153 /* 154 * Walk back up looking for a sibling, or the end of the 155 * structure 156 */ 157 np = prev; 158 while (np->parent && !np->sibling) 159 np = np->parent; 160 np = np->sibling; /* Might be null at the end of the tree */ 161 } 162 163 return np; 164 } 165 166 const void *of_get_property(const struct device_node *np, const char *name, 167 int *lenp) 168 { 169 struct property *pp = of_find_property(np, name, lenp); 170 171 return pp ? pp->value : NULL; 172 } 173 174 static const char *of_prop_next_string(struct property *prop, const char *cur) 175 { 176 const void *curv = cur; 177 178 if (!prop) 179 return NULL; 180 181 if (!cur) 182 return prop->value; 183 184 curv += strlen(cur) + 1; 185 if (curv >= prop->value + prop->length) 186 return NULL; 187 188 return curv; 189 } 190 191 int of_device_is_compatible(const struct device_node *device, 192 const char *compat, const char *type, 193 const char *name) 194 { 195 struct property *prop; 196 const char *cp; 197 int index = 0, score = 0; 198 199 /* Compatible match has highest priority */ 200 if (compat && compat[0]) { 201 prop = of_find_property(device, "compatible", NULL); 202 for (cp = of_prop_next_string(prop, NULL); cp; 203 cp = of_prop_next_string(prop, cp), index++) { 204 if (of_compat_cmp(cp, compat, strlen(compat)) == 0) { 205 score = INT_MAX/2 - (index << 2); 206 break; 207 } 208 } 209 if (!score) 210 return 0; 211 } 212 213 /* Matching type is better than matching name */ 214 if (type && type[0]) { 215 if (!device->type || of_node_cmp(type, device->type)) 216 return 0; 217 score += 2; 218 } 219 220 /* Matching name is a bit better than not */ 221 if (name && name[0]) { 222 if (!device->name || of_node_cmp(name, device->name)) 223 return 0; 224 score++; 225 } 226 227 return score; 228 } 229 230 bool of_device_is_available(const struct device_node *device) 231 { 232 const char *status; 233 int statlen; 234 235 if (!device) 236 return false; 237 238 status = of_get_property(device, "status", &statlen); 239 if (status == NULL) 240 return true; 241 242 if (statlen > 0) { 243 if (!strcmp(status, "okay")) 244 return true; 245 } 246 247 return false; 248 } 249 250 struct device_node *of_get_parent(const struct device_node *node) 251 { 252 const struct device_node *np; 253 254 if (!node) 255 return NULL; 256 257 np = of_node_get(node->parent); 258 259 return (struct device_node *)np; 260 } 261 262 static struct device_node *__of_get_next_child(const struct device_node *node, 263 struct device_node *prev) 264 { 265 struct device_node *next; 266 267 if (!node) 268 return NULL; 269 270 next = prev ? prev->sibling : node->child; 271 /* 272 * coverity[dead_error_line : FALSE] 273 * Dead code here since our current implementation of of_node_get() 274 * always returns NULL (Coverity CID 163245). But we leave it as is 275 * since we may want to implement get/put later. 276 */ 277 for (; next; next = next->sibling) 278 if (of_node_get(next)) 279 break; 280 of_node_put(prev); 281 return next; 282 } 283 284 #define __for_each_child_of_node(parent, child) \ 285 for (child = __of_get_next_child(parent, NULL); child != NULL; \ 286 child = __of_get_next_child(parent, child)) 287 288 static struct device_node *__of_find_node_by_path(struct device_node *parent, 289 const char *path) 290 { 291 struct device_node *child; 292 int len; 293 294 len = strcspn(path, "/:"); 295 if (!len) 296 return NULL; 297 298 __for_each_child_of_node(parent, child) { 299 const char *name = strrchr(child->full_name, '/'); 300 301 name++; 302 if (strncmp(path, name, len) == 0 && (strlen(name) == len)) 303 return child; 304 } 305 return NULL; 306 } 307 308 #define for_each_property_of_node(dn, pp) \ 309 for (pp = dn->properties; pp != NULL; pp = pp->next) 310 311 struct device_node *of_find_node_opts_by_path(const char *path, 312 const char **opts) 313 { 314 struct device_node *np = NULL; 315 struct property *pp; 316 const char *separator = strchr(path, ':'); 317 318 if (opts) 319 *opts = separator ? separator + 1 : NULL; 320 321 if (strcmp(path, "/") == 0) 322 return of_node_get(gd->of_root); 323 324 /* The path could begin with an alias */ 325 if (*path != '/') { 326 int len; 327 const char *p = separator; 328 329 if (!p) 330 p = strchrnul(path, '/'); 331 len = p - path; 332 333 /* of_aliases must not be NULL */ 334 if (!of_aliases) 335 return NULL; 336 337 for_each_property_of_node(of_aliases, pp) { 338 if (strlen(pp->name) == len && !strncmp(pp->name, path, 339 len)) { 340 np = of_find_node_by_path(pp->value); 341 break; 342 } 343 } 344 if (!np) 345 return NULL; 346 path = p; 347 } 348 349 /* Step down the tree matching path components */ 350 if (!np) 351 np = of_node_get(gd->of_root); 352 while (np && *path == '/') { 353 struct device_node *tmp = np; 354 355 path++; /* Increment past '/' delimiter */ 356 np = __of_find_node_by_path(np, path); 357 of_node_put(tmp); 358 path = strchrnul(path, '/'); 359 if (separator && separator < path) 360 break; 361 } 362 363 return np; 364 } 365 366 struct device_node *of_find_compatible_node(struct device_node *from, 367 const char *type, const char *compatible) 368 { 369 struct device_node *np; 370 371 for_each_of_allnodes_from(from, np) 372 if (of_device_is_compatible(np, compatible, type, NULL) && 373 of_node_get(np)) 374 break; 375 of_node_put(from); 376 377 return np; 378 } 379 380 struct device_node *of_find_node_by_phandle(phandle handle) 381 { 382 struct device_node *np; 383 384 if (!handle) 385 return NULL; 386 387 for_each_of_allnodes(np) 388 if (np->phandle == handle) 389 break; 390 391 #ifdef CONFIG_USING_KERNEL_DTB_V2 392 /* If not find in kernel fdt, traverse u-boot fdt */ 393 if (!np) { 394 for (np = gd->of_root_f; np; np = of_find_all_nodes(np)) { 395 if (np->phandle == handle) 396 break; 397 } 398 } 399 #endif 400 (void)of_node_get(np); 401 402 return np; 403 } 404 405 /** 406 * of_find_property_value_of_size() - find property of given size 407 * 408 * Search for a property in a device node and validate the requested size. 409 * 410 * @np: device node from which the property value is to be read. 411 * @propname: name of the property to be searched. 412 * @len: requested length of property value 413 * 414 * @return the property value on success, -EINVAL if the property does not 415 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the 416 * property data isn't large enough. 417 */ 418 static void *of_find_property_value_of_size(const struct device_node *np, 419 const char *propname, u32 len) 420 { 421 struct property *prop = of_find_property(np, propname, NULL); 422 423 if (!prop) 424 return ERR_PTR(-EINVAL); 425 if (!prop->value) 426 return ERR_PTR(-ENODATA); 427 if (len > prop->length) 428 return ERR_PTR(-EOVERFLOW); 429 430 return prop->value; 431 } 432 433 int of_read_u32(const struct device_node *np, const char *propname, u32 *outp) 434 { 435 const __be32 *val; 436 437 debug("%s: %s: ", __func__, propname); 438 if (!np) 439 return -EINVAL; 440 val = of_find_property_value_of_size(np, propname, sizeof(*outp)); 441 if (IS_ERR(val)) { 442 debug("(not found)\n"); 443 return PTR_ERR(val); 444 } 445 446 *outp = be32_to_cpup(val); 447 debug("%#x (%d)\n", *outp, *outp); 448 449 return 0; 450 } 451 452 /** 453 * of_property_read_u64 - Find and read a 64 bit integer from a property 454 * @np: device node from which the property value is to be read. 455 * @propname: name of the property to be searched. 456 * @out_value: pointer to return value, modified only if return value is 0. 457 * 458 * Search for a property in a device node and read a 64-bit value from 459 * it. Returns 0 on success, -EINVAL if the property does not exist, 460 * -ENODATA if property does not have a value, and -EOVERFLOW if the 461 * property data isn't large enough. 462 * 463 * The out_value is modified only if a valid u64 value can be decoded. 464 */ 465 int of_property_read_u64(const struct device_node *np, const char *propname, 466 u64 *out_value) 467 { 468 const __be32 *val = of_find_property_value_of_size(np, propname, 469 sizeof(*out_value)); 470 471 if (IS_ERR(val)) 472 return PTR_ERR(val); 473 474 *out_value = of_read_number(val, 2); 475 476 return 0; 477 } 478 479 int of_read_u32_array(const struct device_node *np, const char *propname, 480 u32 *out_values, size_t sz) 481 { 482 const __be32 *val; 483 484 debug("%s: %s: ", __func__, propname); 485 val = of_find_property_value_of_size(np, propname, 486 sz * sizeof(*out_values)); 487 488 if (IS_ERR(val)) 489 return PTR_ERR(val); 490 491 debug("size %zd\n", sz); 492 while (sz--) 493 *out_values++ = be32_to_cpup(val++); 494 495 return 0; 496 } 497 498 int of_write_u32_array(const struct device_node *np, const char *propname, 499 u32 *values, size_t sz) 500 { 501 __be32 *val; 502 503 debug("%s: %s: ", __func__, propname); 504 val = of_find_property_value_of_size(np, propname, 505 sz * sizeof(*values)); 506 507 if (IS_ERR(val)) 508 return PTR_ERR(val); 509 510 debug("size %zd\n", sz); 511 while (sz--) 512 *val++ = cpu_to_be32p(values++); 513 514 return 0; 515 } 516 517 int of_property_match_string(const struct device_node *np, const char *propname, 518 const char *string) 519 { 520 const struct property *prop = of_find_property(np, propname, NULL); 521 size_t l; 522 int i; 523 const char *p, *end; 524 525 if (!prop) 526 return -EINVAL; 527 if (!prop->value) 528 return -ENODATA; 529 530 p = prop->value; 531 end = p + prop->length; 532 533 for (i = 0; p < end; i++, p += l) { 534 l = strnlen(p, end - p) + 1; 535 if (p + l > end) 536 return -EILSEQ; 537 debug("comparing %s with %s\n", string, p); 538 if (strcmp(string, p) == 0) 539 return i; /* Found it; return index */ 540 } 541 return -ENODATA; 542 } 543 544 /** 545 * of_property_read_string_helper() - Utility helper for parsing string properties 546 * @np: device node from which the property value is to be read. 547 * @propname: name of the property to be searched. 548 * @out_strs: output array of string pointers. 549 * @sz: number of array elements to read. 550 * @skip: Number of strings to skip over at beginning of list. 551 * 552 * Don't call this function directly. It is a utility helper for the 553 * of_property_read_string*() family of functions. 554 */ 555 int of_property_read_string_helper(const struct device_node *np, 556 const char *propname, const char **out_strs, 557 size_t sz, int skip) 558 { 559 const struct property *prop = of_find_property(np, propname, NULL); 560 int l = 0, i = 0; 561 const char *p, *end; 562 563 if (!prop) 564 return -EINVAL; 565 if (!prop->value) 566 return -ENODATA; 567 p = prop->value; 568 end = p + prop->length; 569 570 for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) { 571 l = strnlen(p, end - p) + 1; 572 if (p + l > end) 573 return -EILSEQ; 574 if (out_strs && i >= skip) 575 *out_strs++ = p; 576 } 577 i -= skip; 578 return i <= 0 ? -ENODATA : i; 579 } 580 581 static int __of_parse_phandle_with_args(const struct device_node *np, 582 const char *list_name, 583 const char *cells_name, 584 int cell_count, int index, 585 struct of_phandle_args *out_args) 586 { 587 const __be32 *list, *list_end; 588 int rc = 0, cur_index = 0; 589 uint32_t count = 0; 590 struct device_node *node = NULL; 591 phandle phandle; 592 int size; 593 594 /* Retrieve the phandle list property */ 595 list = of_get_property(np, list_name, &size); 596 if (!list) 597 return -ENOENT; 598 list_end = list + size / sizeof(*list); 599 600 /* Loop over the phandles until all the requested entry is found */ 601 while (list < list_end) { 602 rc = -EINVAL; 603 count = 0; 604 605 /* 606 * If phandle is 0, then it is an empty entry with no 607 * arguments. Skip forward to the next entry. 608 */ 609 phandle = be32_to_cpup(list++); 610 if (phandle) { 611 /* 612 * Find the provider node and parse the #*-cells 613 * property to determine the argument length. 614 * 615 * This is not needed if the cell count is hard-coded 616 * (i.e. cells_name not set, but cell_count is set), 617 * except when we're going to return the found node 618 * below. 619 */ 620 if (cells_name || cur_index == index) { 621 node = of_find_node_by_phandle(phandle); 622 if (!node) { 623 debug("%s: could not find phandle\n", 624 np->full_name); 625 goto err; 626 } 627 } 628 629 if (cells_name) { 630 if (of_read_u32(node, cells_name, &count)) { 631 debug("%s: could not get %s for %s\n", 632 np->full_name, cells_name, 633 node->full_name); 634 goto err; 635 } 636 } else { 637 count = cell_count; 638 } 639 640 /* 641 * Make sure that the arguments actually fit in the 642 * remaining property data length 643 */ 644 if (list + count > list_end) { 645 debug("%s: arguments longer than property\n", 646 np->full_name); 647 goto err; 648 } 649 } 650 651 /* 652 * All of the error cases above bail out of the loop, so at 653 * this point, the parsing is successful. If the requested 654 * index matches, then fill the out_args structure and return, 655 * or return -ENOENT for an empty entry. 656 */ 657 rc = -ENOENT; 658 if (cur_index == index) { 659 if (!phandle) 660 goto err; 661 662 if (out_args) { 663 int i; 664 if (WARN_ON(count > OF_MAX_PHANDLE_ARGS)) 665 count = OF_MAX_PHANDLE_ARGS; 666 out_args->np = node; 667 out_args->args_count = count; 668 for (i = 0; i < count; i++) 669 out_args->args[i] = 670 be32_to_cpup(list++); 671 } else { 672 of_node_put(node); 673 } 674 675 /* Found it! return success */ 676 return 0; 677 } 678 679 of_node_put(node); 680 node = NULL; 681 list += count; 682 cur_index++; 683 } 684 685 /* 686 * Unlock node before returning result; will be one of: 687 * -ENOENT : index is for empty phandle 688 * -EINVAL : parsing error on data 689 * [1..n] : Number of phandle (count mode; when index = -1) 690 */ 691 rc = index < 0 ? cur_index : -ENOENT; 692 err: 693 if (node) 694 of_node_put(node); 695 return rc; 696 } 697 698 struct device_node *of_parse_phandle(const struct device_node *np, 699 const char *phandle_name, int index) 700 { 701 struct of_phandle_args args; 702 703 if (index < 0) 704 return NULL; 705 706 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index, 707 &args)) 708 return NULL; 709 710 return args.np; 711 } 712 713 int of_parse_phandle_with_args(const struct device_node *np, 714 const char *list_name, const char *cells_name, 715 int index, struct of_phandle_args *out_args) 716 { 717 if (index < 0) 718 return -EINVAL; 719 720 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, 721 index, out_args); 722 } 723 724 int of_count_phandle_with_args(const struct device_node *np, 725 const char *list_name, const char *cells_name) 726 { 727 return __of_parse_phandle_with_args(np, list_name, cells_name, 0, 728 -1, NULL); 729 } 730 731 static void of_alias_add(struct alias_prop *ap, struct device_node *np, 732 int id, const char *stem, int stem_len) 733 { 734 struct alias_prop *oldap; 735 ap->np = np; 736 ap->id = id; 737 strncpy(ap->stem, stem, stem_len); 738 ap->stem[stem_len] = 0; 739 740 /* Delete U-Boot alias which is same with kernel */ 741 mutex_lock(&of_mutex); 742 list_for_each_entry(oldap, &aliases_lookup, link) { 743 if (stem && !strcmp(stem, oldap->alias) && (id == oldap->id)) { 744 /* Always use from U-Boot aliase */ 745 if (strcmp(stem, "mmc")) 746 continue; 747 748 list_del(&oldap->link); 749 break; 750 } 751 } 752 mutex_unlock(&of_mutex); 753 754 list_add_tail(&ap->link, &aliases_lookup); 755 debug("adding DT alias:%s: stem=%s id=%i node=%s\n", 756 ap->alias, ap->stem, ap->id, of_node_full_name(np)); 757 } 758 759 int of_alias_scan(void) 760 { 761 struct property *pp; 762 763 of_aliases = of_find_node_by_path("/aliases"); 764 of_chosen = of_find_node_by_path("/chosen"); 765 if (of_chosen == NULL) 766 of_chosen = of_find_node_by_path("/chosen@0"); 767 768 if (of_chosen) { 769 const char *name; 770 771 name = of_get_property(of_chosen, "stdout-path", NULL); 772 if (name) 773 of_stdout = of_find_node_opts_by_path(name, 774 &of_stdout_options); 775 } 776 777 if (!of_aliases) 778 return 0; 779 780 for_each_property_of_node(of_aliases, pp) { 781 const char *start = pp->name; 782 const char *end = start + strlen(start); 783 struct device_node *np; 784 struct alias_prop *ap; 785 ulong id; 786 int len; 787 788 /* Skip those we do not want to proceed */ 789 if (!strcmp(pp->name, "name") || 790 !strcmp(pp->name, "phandle") || 791 !strcmp(pp->name, "linux,phandle")) 792 continue; 793 794 np = of_find_node_by_path(pp->value); 795 if (!np) 796 continue; 797 798 /* 799 * walk the alias backwards to extract the id and work out 800 * the 'stem' string 801 */ 802 while (isdigit(*(end-1)) && end > start) 803 end--; 804 len = end - start; 805 806 if (strict_strtoul(end, 10, &id) < 0) 807 continue; 808 809 /* Allocate an alias_prop with enough space for the stem */ 810 ap = malloc(sizeof(*ap) + len + 1); 811 if (!ap) 812 return -ENOMEM; 813 memset(ap, 0, sizeof(*ap) + len + 1); 814 ap->alias = start; 815 of_alias_add(ap, np, id, start, len); 816 } 817 818 return 0; 819 } 820 821 int of_alias_get_id(const struct device_node *np, const char *stem) 822 { 823 struct alias_prop *app; 824 int id = -ENODEV; 825 826 mutex_lock(&of_mutex); 827 list_for_each_entry(app, &aliases_lookup, link) { 828 if (strcmp(app->stem, stem) != 0) 829 continue; 830 831 if (np == app->np) { 832 id = app->id; 833 break; 834 } 835 } 836 mutex_unlock(&of_mutex); 837 838 return id; 839 } 840 841 struct device_node *of_alias_get_dev(const char *stem, int id) 842 { 843 struct alias_prop *app; 844 struct device_node *np = NULL; 845 846 mutex_lock(&of_mutex); 847 list_for_each_entry(app, &aliases_lookup, link) { 848 if (strcmp(app->stem, stem) != 0) 849 continue; 850 851 if (id == app->id) { 852 np = app->np; 853 break; 854 } 855 } 856 mutex_unlock(&of_mutex); 857 858 return np; 859 } 860 861 struct device_node *of_alias_dump(void) 862 { 863 struct alias_prop *app; 864 struct device_node *np = NULL; 865 866 mutex_lock(&of_mutex); 867 list_for_each_entry(app, &aliases_lookup, link) { 868 printf("%10s%d: %20s, phandle=%d %4s\n", 869 app->stem, app->id, 870 app->np->full_name, app->np->phandle, 871 of_get_property(app->np, "u-boot,dm-pre-reloc", NULL) || 872 of_get_property(app->np, "u-boot,dm-spl", NULL) ? "*" : ""); 873 } 874 mutex_unlock(&of_mutex); 875 876 return np; 877 } 878 879 struct device_node *of_get_stdout(void) 880 { 881 return of_stdout; 882 } 883