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