uboot/include/dm/read.h
<<
>>
Prefs
   1/* SPDX-License-Identifier: GPL-2.0+ */
   2/*
   3 * Function to read values from the device tree node attached to a udevice.
   4 *
   5 * Copyright (c) 2017 Google, Inc
   6 * Written by Simon Glass <sjg@chromium.org>
   7 */
   8
   9#ifndef _DM_READ_H
  10#define _DM_READ_H
  11
  12#include <linux/errno.h>
  13
  14#include <dm/device.h>
  15#include <dm/fdtaddr.h>
  16#include <dm/ofnode.h>
  17#include <dm/uclass.h>
  18
  19struct resource;
  20
  21#if CONFIG_IS_ENABLED(OF_LIVE)
  22static inline const struct device_node *dev_np(const struct udevice *dev)
  23{
  24        return ofnode_to_np(dev_ofnode(dev));
  25}
  26#else
  27static inline const struct device_node *dev_np(const struct udevice *dev)
  28{
  29        return NULL;
  30}
  31#endif
  32
  33#if !defined(CONFIG_DM_DEV_READ_INLINE) || CONFIG_IS_ENABLED(OF_PLATDATA)
  34/**
  35 * dev_read_u8() - read a 8-bit integer from a device's DT property
  36 *
  37 * @dev:        device to read DT property from
  38 * @propname:   name of the property to read from
  39 * @outp:       place to put value (if found)
  40 * Return: 0 if OK, -ve on error
  41 */
  42int dev_read_u8(const struct udevice *dev, const char *propname, u8 *outp);
  43
  44/**
  45 * dev_read_u8_default() - read a 8-bit integer from a device's DT property
  46 *
  47 * @dev:        device to read DT property from
  48 * @propname:   name of the property to read from
  49 * @def:        default value to return if the property has no value
  50 * Return: property value, or @def if not found
  51 */
  52u8 dev_read_u8_default(const struct udevice *dev, const char *propname, u8 def);
  53
  54/**
  55 * dev_read_u16() - read a 16-bit integer from a device's DT property
  56 *
  57 * @dev:        device to read DT property from
  58 * @propname:   name of the property to read from
  59 * @outp:       place to put value (if found)
  60 * Return: 0 if OK, -ve on error
  61 */
  62int dev_read_u16(const struct udevice *dev, const char *propname, u16 *outp);
  63
  64/**
  65 * dev_read_u16_default() - read a 16-bit integer from a device's DT property
  66 *
  67 * @dev:        device to read DT property from
  68 * @propname:   name of the property to read from
  69 * @def:        default value to return if the property has no value
  70 * Return: property value, or @def if not found
  71 */
  72u16 dev_read_u16_default(const struct udevice *dev, const char *propname,
  73                         u16 def);
  74
  75/**
  76 * dev_read_u32() - read a 32-bit integer from a device's DT property
  77 *
  78 * @dev:        device to read DT property from
  79 * @propname:   name of the property to read from
  80 * @outp:       place to put value (if found)
  81 * Return: 0 if OK, -ve on error
  82 */
  83int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp);
  84
  85/**
  86 * dev_read_u32_default() - read a 32-bit integer from a device's DT property
  87 *
  88 * @dev:        device to read DT property from
  89 * @propname:   name of the property to read from
  90 * @def:        default value to return if the property has no value
  91 * Return: property value, or @def if not found
  92 */
  93int dev_read_u32_default(const struct udevice *dev, const char *propname,
  94                         int def);
  95
  96/**
  97 * dev_read_u32_index() - read an indexed 32-bit integer from a device's DT
  98 *                        property
  99 *
 100 * @dev:        device to read DT property from
 101 * @propname:   name of the property to read from
 102 * @index:      index of the integer to return
 103 * @outp:       place to put value (if found)
 104 * Return: 0 if OK, -ve on error
 105 */
 106int dev_read_u32_index(struct udevice *dev, const char *propname, int index,
 107                       u32 *outp);
 108
 109/**
 110 * dev_read_u32_index_default() - read an indexed 32-bit integer from a device's
 111 *                                DT property
 112 *
 113 * @dev:        device to read DT property from
 114 * @propname:   name of the property to read from
 115 * @index:      index of the integer to return
 116 * @def:        default value to return if the property has no value
 117 * Return: property value, or @def if not found
 118 */
 119u32 dev_read_u32_index_default(struct udevice *dev, const char *propname,
 120                               int index, u32 def);
 121
 122/**
 123 * dev_read_s32() - read a signed 32-bit integer from a device's DT property
 124 *
 125 * @dev:        device to read DT property from
 126 * @propname:   name of the property to read from
 127 * @outp:       place to put value (if found)
 128 * Return: 0 if OK, -ve on error
 129 */
 130int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp);
 131
 132/**
 133 * dev_read_s32_default() - read a signed 32-bit int from a device's DT property
 134 *
 135 * @dev:        device to read DT property from
 136 * @propname:   name of the property to read from
 137 * @def:        default value to return if the property has no value
 138 * Return: property value, or @def if not found
 139 */
 140int dev_read_s32_default(const struct udevice *dev, const char *propname,
 141                         int def);
 142
 143/**
 144 * dev_read_u32u() - read a 32-bit integer from a device's DT property
 145 *
 146 * This version uses a standard uint type.
 147 *
 148 * @dev:        device to read DT property from
 149 * @propname:   name of the property to read from
 150 * @outp:       place to put value (if found)
 151 * Return: 0 if OK, -ve on error
 152 */
 153int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp);
 154
 155/**
 156 * dev_read_u64() - read a 64-bit integer from a device's DT property
 157 *
 158 * @dev:        device to read DT property from
 159 * @propname:   name of the property to read from
 160 * @outp:       place to put value (if found)
 161 * Return: 0 if OK, -ve on error
 162 */
 163int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp);
 164
 165/**
 166 * dev_read_u64_default() - read a 64-bit integer from a device's DT property
 167 *
 168 * @dev:        device to read DT property from
 169 * @propname:   name of the property to read from
 170 * @def:        default value to return if the property has no value
 171 * Return: property value, or @def if not found
 172 */
 173u64 dev_read_u64_default(const struct udevice *dev, const char *propname,
 174                         u64 def);
 175
 176/**
 177 * dev_read_string() - Read a string from a device's DT property
 178 *
 179 * @dev:        device to read DT property from
 180 * @propname:   name of the property to read
 181 * Return: string from property value, or NULL if there is no such property
 182 */
 183const char *dev_read_string(const struct udevice *dev, const char *propname);
 184
 185/**
 186 * dev_read_bool() - read a boolean value from a device's DT property
 187 *
 188 * @dev:        device to read DT property from
 189 * @propname:   name of property to read
 190 * Return: true if property is present (meaning true), false if not present
 191 */
 192bool dev_read_bool(const struct udevice *dev, const char *propname);
 193
 194/**
 195 * dev_read_subnode() - find a named subnode of a device
 196 *
 197 * @dev:        device whose DT node contains the subnode
 198 * @subnode_name: name of subnode to find
 199 * Return: reference to subnode (which can be invalid if there is no such
 200 * subnode)
 201 */
 202ofnode dev_read_subnode(const struct udevice *dev, const char *subnode_name);
 203
 204/**
 205 * dev_read_size() - read the size of a property
 206 *
 207 * @dev: device to check
 208 * @propname: property to check
 209 * Return: size of property if present, or -EINVAL if not
 210 */
 211int dev_read_size(const struct udevice *dev, const char *propname);
 212
 213/**
 214 * dev_read_addr_index() - Get the indexed reg property of a device
 215 *
 216 * @dev: Device to read from
 217 * @index: the 'reg' property can hold a list of <addr, size> pairs
 218 *         and @index is used to select which one is required
 219 *
 220 * Return: address or FDT_ADDR_T_NONE if not found
 221 */
 222fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index);
 223
 224/**
 225 * dev_read_addr_index_ptr() - Get the indexed reg property of a device
 226 *                             as a pointer
 227 *
 228 * @dev: Device to read from
 229 * @index: the 'reg' property can hold a list of <addr, size> pairs
 230 *         and @index is used to select which one is required
 231 *
 232 * Return: pointer or NULL if not found
 233 */
 234void *dev_read_addr_index_ptr(const struct udevice *dev, int index);
 235
 236/**
 237 * dev_read_addr_size_index() - Get the indexed reg property of a device
 238 *
 239 * @dev: Device to read from
 240 * @index: the 'reg' property can hold a list of <addr, size> pairs
 241 *         and @index is used to select which one is required
 242 * @size: place to put size value (on success)
 243 *
 244 * Return: address or FDT_ADDR_T_NONE if not found
 245 */
 246fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index,
 247                                    fdt_size_t *size);
 248
 249/**
 250 * dev_remap_addr_index() - Get the indexed reg property of a device
 251 *                               as a memory-mapped I/O pointer
 252 *
 253 * @dev: Device to read from
 254 * @index: the 'reg' property can hold a list of <addr, size> pairs
 255 *         and @index is used to select which one is required
 256 *
 257 * Return: pointer or NULL if not found
 258 */
 259void *dev_remap_addr_index(const struct udevice *dev, int index);
 260
 261/**
 262 * dev_read_addr_name() - Get the reg property of a device, indexed by name
 263 *
 264 * @dev: Device to read from
 265 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
 266 *        'reg-names' property providing named-based identification. @index
 267 *        indicates the value to search for in 'reg-names'.
 268 *
 269 * Return: address or FDT_ADDR_T_NONE if not found
 270 */
 271fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name);
 272
 273/**
 274 * dev_read_addr_size_name() - Get the reg property of a device, indexed by name
 275 *
 276 * @dev: Device to read from
 277 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
 278 *        'reg-names' property providing named-based identification. @index
 279 *        indicates the value to search for in 'reg-names'.
 280 *  @size: place to put size value (on success)
 281 *
 282 * Return: address or FDT_ADDR_T_NONE if not found
 283 */
 284fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name,
 285                                   fdt_size_t *size);
 286
 287/**
 288 * dev_remap_addr_name() - Get the reg property of a device, indexed by name,
 289 *                         as a memory-mapped I/O pointer
 290 *
 291 * @dev: Device to read from
 292 * @name: the 'reg' property can hold a list of <addr, size> pairs, with the
 293 *        'reg-names' property providing named-based identification. @index
 294 *        indicates the value to search for in 'reg-names'.
 295 *
 296 * Return: pointer or NULL if not found
 297 */
 298void *dev_remap_addr_name(const struct udevice *dev, const char *name);
 299
 300/**
 301 * dev_read_addr() - Get the reg property of a device
 302 *
 303 * @dev: Device to read from
 304 *
 305 * Return: address or FDT_ADDR_T_NONE if not found
 306 */
 307fdt_addr_t dev_read_addr(const struct udevice *dev);
 308
 309/**
 310 * dev_read_addr_ptr() - Get the reg property of a device
 311 *                       as a pointer
 312 *
 313 * @dev: Device to read from
 314 *
 315 * Return: pointer or NULL if not found
 316 */
 317void *dev_read_addr_ptr(const struct udevice *dev);
 318
 319/**
 320 * dev_read_addr_pci() - Read an address and handle PCI address translation
 321 *
 322 * At present U-Boot does not have address translation logic for PCI in the
 323 * livetree implementation (of_addr.c). This special function supports this for
 324 * the flat tree implementation.
 325 *
 326 * This function should be removed (and code should use dev_read() instead)
 327 * once:
 328 *
 329 * 1. PCI address translation is added; and either
 330 * 2. everything uses livetree where PCI translation is used (which is feasible
 331 *    in SPL and U-Boot proper) or PCI address translation is added to
 332 *    fdtdec_get_addr() and friends.
 333 *
 334 * @dev: Device to read from
 335 * Return: address or FDT_ADDR_T_NONE if not found
 336 */
 337fdt_addr_t dev_read_addr_pci(const struct udevice *dev);
 338
 339/**
 340 * dev_remap_addr() - Get the reg property of a device as a
 341 *                         memory-mapped I/O pointer
 342 *
 343 * @dev: Device to read from
 344 *
 345 * Return: pointer or NULL if not found
 346 */
 347void *dev_remap_addr(const struct udevice *dev);
 348
 349/**
 350 * dev_read_addr_size() - get address and size from a device property
 351 *
 352 * This does no address translation. It simply reads an property that contains
 353 * an address and a size value, one after the other.
 354 *
 355 * @dev: Device to read from
 356 * @propname: property to read
 357 * @sizep: place to put size value (on success)
 358 * Return: address value, or FDT_ADDR_T_NONE on error
 359 */
 360fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *propname,
 361                              fdt_size_t *sizep);
 362
 363/**
 364 * dev_read_name() - get the name of a device's node
 365 *
 366 * @dev: Device to read from
 367 * Return: name of node
 368 */
 369const char *dev_read_name(const struct udevice *dev);
 370
 371/**
 372 * dev_read_stringlist_search() - find string in a string list and return index
 373 *
 374 * Note that it is possible for this function to succeed on property values
 375 * that are not NUL-terminated. That's because the function will stop after
 376 * finding the first occurrence of @string. This can for example happen with
 377 * small-valued cell properties, such as #address-cells, when searching for
 378 * the empty string.
 379 *
 380 * @dev: device to check
 381 * @propname: name of the property containing the string list
 382 * @string: string to look up in the string list
 383 *
 384 * Return:
 385 *   the index of the string in the list of strings
 386 *   -ENODATA if the property is not found
 387 *   -EINVAL on some other error
 388 */
 389int dev_read_stringlist_search(const struct udevice *dev, const char *propname,
 390                               const char *string);
 391
 392/**
 393 * dev_read_string_index() - obtain an indexed string from a string list
 394 *
 395 * @dev: device to examine
 396 * @propname: name of the property containing the string list
 397 * @index: index of the string to return
 398 * @outp: return location for the string
 399 *
 400 * Return:
 401 *   length of string, if found or -ve error value if not found
 402 */
 403int dev_read_string_index(const struct udevice *dev, const char *propname,
 404                          int index, const char **outp);
 405
 406/**
 407 * dev_read_string_count() - find the number of strings in a string list
 408 *
 409 * @dev: device to examine
 410 * @propname: name of the property containing the string list
 411 * Return:
 412 *   number of strings in the list, or -ve error value if not found
 413 */
 414int dev_read_string_count(const struct udevice *dev, const char *propname);
 415
 416/**
 417 * dev_read_string_list() - read a list of strings
 418 *
 419 * This produces a list of string pointers with each one pointing to a string
 420 * in the string list. If the property does not exist, it returns {NULL}.
 421 *
 422 * The data is allocated and the caller is reponsible for freeing the return
 423 * value (the list of string pointers). The strings themselves may not be
 424 * changed as they point directly into the devicetree property.
 425 *
 426 * @dev: device to examine
 427 * @propname: name of the property containing the string list
 428 * @listp: returns an allocated, NULL-terminated list of strings if the return
 429 *      value is > 0, else is set to NULL
 430 * Return:
 431 * number of strings in list, 0 if none, -ENOMEM if out of memory,
 432 * -ENOENT if no such property
 433 */
 434int dev_read_string_list(const struct udevice *dev, const char *propname,
 435                         const char ***listp);
 436
 437/**
 438 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list
 439 *
 440 * This function is useful to parse lists of phandles and their arguments.
 441 * Returns 0 on success and fills out_args, on error returns appropriate
 442 * errno value.
 443 *
 444 * Caller is responsible to call of_node_put() on the returned out_args->np
 445 * pointer.
 446 *
 447 * Example:
 448 *
 449 * .. code-block::
 450 *
 451 *   phandle1: node1 {
 452 *       #list-cells = <2>;
 453 *   };
 454 *   phandle2: node2 {
 455 *       #list-cells = <1>;
 456 *   };
 457 *   node3 {
 458 *       list = <&phandle1 1 2 &phandle2 3>;
 459 *   };
 460 *
 461 * To get a device_node of the `node2' node you may call this:
 462 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args);
 463 *
 464 * @dev:        device whose node containing a list
 465 * @list_name:  property name that contains a list
 466 * @cells_name: property name that specifies phandles' arguments count
 467 * @cell_count: Cell count to use if @cells_name is NULL
 468 * @index:      index of a phandle to parse out
 469 * @out_args:   optional pointer to output arguments structure (will be filled)
 470 * Return: 0 on success (with @out_args filled out if not NULL), -ENOENT if
 471 *      @list_name does not exist, -EINVAL if a phandle was not found,
 472 *      @cells_name could not be found, the arguments were truncated or there
 473 *      were too many arguments.
 474 */
 475int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name,
 476                               const char *cells_name, int cell_count,
 477                               int index, struct ofnode_phandle_args *out_args);
 478
 479/**
 480 * dev_count_phandle_with_args() - Return phandle number in a list
 481 *
 482 * This function is usefull to get phandle number contained in a property list.
 483 * For example, this allows to allocate the right amount of memory to keep
 484 * clock's reference contained into the "clocks" property.
 485 *
 486 * @dev:        device whose node containing a list
 487 * @list_name:  property name that contains a list
 488 * @cells_name: property name that specifies phandles' arguments count
 489 * @cell_count: Cell count to use if @cells_name is NULL
 490 * Return: number of phandle found on success, on error returns appropriate
 491 * errno value.
 492 */
 493
 494int dev_count_phandle_with_args(const struct udevice *dev,
 495                                const char *list_name, const char *cells_name,
 496                                int cell_count);
 497
 498/**
 499 * dev_read_addr_cells() - Get the number of address cells for a device's node
 500 *
 501 * This walks back up the tree to find the closest #address-cells property
 502 * which controls the given node.
 503 *
 504 * @dev: device to check
 505 * Return: number of address cells this node uses
 506 */
 507int dev_read_addr_cells(const struct udevice *dev);
 508
 509/**
 510 * dev_read_size_cells() - Get the number of size cells for a device's node
 511 *
 512 * This walks back up the tree to find the closest #size-cells property
 513 * which controls the given node.
 514 *
 515 * @dev: device to check
 516 * Return: number of size cells this node uses
 517 */
 518int dev_read_size_cells(const struct udevice *dev);
 519
 520/**
 521 * dev_read_addr_cells() - Get the address cells property in a node
 522 *
 523 * This function matches fdt_address_cells().
 524 *
 525 * @dev: device to check
 526 * Return: number of address cells this node uses
 527 */
 528int dev_read_simple_addr_cells(const struct udevice *dev);
 529
 530/**
 531 * dev_read_size_cells() - Get the size cells property in a node
 532 *
 533 * This function matches fdt_size_cells().
 534 *
 535 * @dev: device to check
 536 * Return: number of size cells this node uses
 537 */
 538int dev_read_simple_size_cells(const struct udevice *dev);
 539
 540/**
 541 * dev_read_phandle() - Get the phandle from a device
 542 *
 543 * @dev: device to check
 544 * Return: phandle (1 or greater), or 0 if no phandle or other error
 545 */
 546int dev_read_phandle(const struct udevice *dev);
 547
 548/**
 549 * dev_read_prop()- - read a property from a device's node
 550 *
 551 * @dev: device to check
 552 * @propname: property to read
 553 * @lenp: place to put length on success
 554 * Return: pointer to property, or NULL if not found
 555 */
 556const void *dev_read_prop(const struct udevice *dev, const char *propname,
 557                          int *lenp);
 558
 559/**
 560 * dev_read_first_prop()- get the reference of the first property
 561 *
 562 * Get reference to the first property of the node, it is used to iterate
 563 * and read all the property with dev_read_prop_by_prop().
 564 *
 565 * @dev: device to check
 566 * @prop: place to put argument reference
 567 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
 568 */
 569int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop);
 570
 571/**
 572 * ofnode_next_property() - get the reference of the next property
 573 *
 574 * Get reference to the next property of the node, it is used to iterate
 575 * and read all the property with dev_read_prop_by_prop().
 576 *
 577 * @prop: reference of current argument and place to put reference of next one
 578 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
 579 */
 580int dev_read_next_prop(struct ofprop *prop);
 581
 582/**
 583 * dev_read_prop_by_prop() - get a pointer to the value of a property
 584 *
 585 * Get value for the property identified by the provided reference.
 586 *
 587 * @prop: reference on property
 588 * @propname: If non-NULL, place to property name on success,
 589 * @lenp: If non-NULL, place to put length on success
 590 * Return: 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found
 591 */
 592const void *dev_read_prop_by_prop(struct ofprop *prop,
 593                                  const char **propname, int *lenp);
 594
 595/**
 596 * dev_read_alias_seq() - Get the alias sequence number of a node
 597 *
 598 * This works out whether a node is pointed to by an alias, and if so, the
 599 * sequence number of that alias. Aliases are of the form <base><num> where
 600 * <num> is the sequence number. For example spi2 would be sequence number 2.
 601 *
 602 * @dev: device to look up
 603 * @devnump: set to the sequence number if one is found
 604 * Return: 0 if a sequence was found, -ve if not
 605 */
 606int dev_read_alias_seq(const struct udevice *dev, int *devnump);
 607
 608/**
 609 * dev_read_u32_array() - Find and read an array of 32 bit integers
 610 *
 611 * Search for a property in a device node and read 32-bit value(s) from
 612 * it.
 613 *
 614 * The out_values is modified only if a valid u32 value can be decoded.
 615 *
 616 * @dev: device to look up
 617 * @propname:   name of the property to read
 618 * @out_values: pointer to return value, modified only if return value is 0
 619 * @sz:         number of array elements to read
 620 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
 621 * property does not have a value, and -EOVERFLOW if the property data isn't
 622 * large enough.
 623 */
 624int dev_read_u32_array(const struct udevice *dev, const char *propname,
 625                       u32 *out_values, size_t sz);
 626
 627/**
 628 * dev_read_first_subnode() - find the first subnode of a device's node
 629 *
 630 * @dev: device to look up
 631 * Return: reference to the first subnode (which can be invalid if the device's
 632 * node has no subnodes)
 633 */
 634ofnode dev_read_first_subnode(const struct udevice *dev);
 635
 636/**
 637 * ofnode_next_subnode() - find the next sibling of a subnode
 638 *
 639 * @node:       valid reference to previous node (sibling)
 640 * Return: reference to the next subnode (which can be invalid if the node
 641 * has no more siblings)
 642 */
 643ofnode dev_read_next_subnode(ofnode node);
 644
 645/**
 646 * dev_read_u8_array_ptr() - find an 8-bit array
 647 *
 648 * Look up a device's node property and return a pointer to its contents as a
 649 * byte array of given length. The property must have at least enough data
 650 * for the array (count bytes). It may have more, but this will be ignored.
 651 * The data is not copied.
 652 *
 653 * @dev: device to look up
 654 * @propname: name of property to find
 655 * @sz: number of array elements
 656 * Return:
 657 * pointer to byte array if found, or NULL if the property is not found or
 658 * there is not enough data
 659 */
 660const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
 661                                     const char *propname, size_t sz);
 662
 663/**
 664 * dev_read_enabled() - check whether a node is enabled
 665 *
 666 * This looks for a 'status' property. If this exists, then returns 1 if
 667 * the status is 'ok' and 0 otherwise. If there is no status property,
 668 * it returns 1 on the assumption that anything mentioned should be enabled
 669 * by default.
 670 *
 671 * @dev: device to examine
 672 * Return: integer value 0 (not enabled) or 1 (enabled)
 673 */
 674int dev_read_enabled(const struct udevice *dev);
 675
 676/**
 677 * dev_read_resource() - obtain an indexed resource from a device.
 678 *
 679 * @dev: device to examine
 680 * @index: index of the resource to retrieve (0 = first)
 681 * @res: returns the resource
 682 * Return: 0 if ok, negative on error
 683 */
 684int dev_read_resource(const struct udevice *dev, uint index,
 685                      struct resource *res);
 686
 687/**
 688 * dev_read_resource_byname() - obtain a named resource from a device.
 689 *
 690 * @dev: device to examine
 691 * @name: name of the resource to retrieve
 692 * @res: returns the resource
 693 * Return: 0 if ok, negative on error
 694 */
 695int dev_read_resource_byname(const struct udevice *dev, const char *name,
 696                             struct resource *res);
 697
 698/**
 699 * dev_translate_address() - Translate a device-tree address
 700 *
 701 * Translate an address from the device-tree into a CPU physical address.  This
 702 * function walks up the tree and applies the various bus mappings along the
 703 * way.
 704 *
 705 * @dev: device giving the context in which to translate the address
 706 * @in_addr: pointer to the address to translate
 707 * Return: the translated address; OF_BAD_ADDR on error
 708 */
 709u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr);
 710
 711/**
 712 * dev_translate_dma_address() - Translate a device-tree DMA address
 713 *
 714 * Translate a DMA address from the device-tree into a CPU physical address.
 715 * This function walks up the tree and applies the various bus mappings along
 716 * the way.
 717 *
 718 * @dev: device giving the context in which to translate the DMA address
 719 * @in_addr: pointer to the DMA address to translate
 720 * Return: the translated DMA address; OF_BAD_ADDR on error
 721 */
 722u64 dev_translate_dma_address(const struct udevice *dev,
 723                              const fdt32_t *in_addr);
 724
 725/**
 726 * dev_get_dma_range() - Get a device's DMA constraints
 727 *
 728 * Provide the address bases and size of the linear mapping between the CPU and
 729 * a device's BUS address space.
 730 *
 731 * @dev: device giving the context in which to translate the DMA address
 732 * @cpu: base address for CPU's view of memory
 733 * @bus: base address for BUS's view of memory
 734 * @size: size of the address space
 735 * Return: 0 if ok, negative on error
 736 */
 737int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
 738                      dma_addr_t *bus, u64 *size);
 739
 740/**
 741 * dev_read_alias_highest_id - Get highest alias id for the given stem
 742 * @stem:       Alias stem to be examined
 743 *
 744 * The function travels the lookup table to get the highest alias id for the
 745 * given alias stem.
 746 * Return: alias ID, if found, else -1
 747 */
 748int dev_read_alias_highest_id(const char *stem);
 749
 750/**
 751 * dev_get_child_count() - get the child count of a device
 752 *
 753 * @dev: device to use for interation (`struct udevice *`)
 754 * Return: the count of child subnode
 755 */
 756int dev_get_child_count(const struct udevice *dev);
 757
 758/**
 759 * dev_read_pci_bus_range - Read PCI bus-range resource
 760 *
 761 * Look at the bus range property of a device node and return the pci bus
 762 * range for this node.
 763 *
 764 * @dev: device to examine
 765 * @res: returns the resource
 766 * Return: 0 if ok, negative on error
 767 */
 768int dev_read_pci_bus_range(const struct udevice *dev, struct resource *res);
 769
 770/**
 771 * dev_decode_display_timing() - decode display timings
 772 *
 773 * Decode display timings from the supplied 'display-timings' node.
 774 * See doc/device-tree-bindings/video/display-timing.txt for binding
 775 * information.
 776 *
 777 * @dev: device to read DT display timings from. The node linked to the device
 778 *       contains a child node called 'display-timings' which in turn contains
 779 *       one or more display timing nodes.
 780 * @index: index number to read (0=first timing subnode)
 781 * @config: place to put timings
 782 * Return: 0 if OK, -FDT_ERR_NOTFOUND if not found
 783 */
 784int dev_decode_display_timing(const struct udevice *dev, int index,
 785                              struct display_timing *config);
 786
 787/**
 788 * dev_get_phy_node() - Get PHY node for a MAC (if not fixed-link)
 789 *
 790 * This function parses PHY handle from the Ethernet controller's ofnode
 791 * (trying all possible PHY handle property names), and returns the PHY ofnode.
 792 *
 793 * Before this is used, ofnode_phy_is_fixed_link() should be checked first, and
 794 * if the result to that is true, this function should not be called.
 795 *
 796 * @dev: device representing the MAC
 797 * Return: ofnode of the PHY, if it exists, otherwise an invalid ofnode
 798 */
 799ofnode dev_get_phy_node(const struct udevice *dev);
 800
 801/**
 802 * dev_read_phy_mode() - Read PHY connection type from a MAC
 803 *
 804 * This function parses the "phy-mode" / "phy-connection-type" property and
 805 * returns the corresponding PHY interface type.
 806 *
 807 * @dev: device representing the MAC
 808 * Return: one of PHY_INTERFACE_MODE_* constants, PHY_INTERFACE_MODE_NA on
 809 *         error
 810 */
 811phy_interface_t dev_read_phy_mode(const struct udevice *dev);
 812
 813#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
 814#include <asm/global_data.h>
 815
 816static inline int dev_read_u8(const struct udevice *dev,
 817                              const char *propname, u8 *outp)
 818{
 819        return ofnode_read_u8(dev_ofnode(dev), propname, outp);
 820}
 821
 822static inline int dev_read_u8_default(const struct udevice *dev,
 823                                      const char *propname, u8 def)
 824{
 825        return ofnode_read_u8_default(dev_ofnode(dev), propname, def);
 826}
 827
 828static inline int dev_read_u16(const struct udevice *dev,
 829                               const char *propname, u16 *outp)
 830{
 831        return ofnode_read_u16(dev_ofnode(dev), propname, outp);
 832}
 833
 834static inline int dev_read_u16_default(const struct udevice *dev,
 835                                       const char *propname, u16 def)
 836{
 837        return ofnode_read_u16_default(dev_ofnode(dev), propname, def);
 838}
 839
 840static inline int dev_read_u32(const struct udevice *dev,
 841                               const char *propname, u32 *outp)
 842{
 843        return ofnode_read_u32(dev_ofnode(dev), propname, outp);
 844}
 845
 846static inline int dev_read_u32_default(const struct udevice *dev,
 847                                       const char *propname, int def)
 848{
 849        return ofnode_read_u32_default(dev_ofnode(dev), propname, def);
 850}
 851
 852static inline int dev_read_u32_index(struct udevice *dev,
 853                                     const char *propname, int index, u32 *outp)
 854{
 855        return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp);
 856}
 857
 858static inline u32 dev_read_u32_index_default(struct udevice *dev,
 859                                             const char *propname, int index,
 860                                             u32 def)
 861{
 862        return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index,
 863                                             def);
 864}
 865
 866static inline int dev_read_s32(const struct udevice *dev,
 867                               const char *propname, s32 *outp)
 868{
 869        return ofnode_read_s32(dev_ofnode(dev), propname, outp);
 870}
 871
 872static inline int dev_read_s32_default(const struct udevice *dev,
 873                                       const char *propname, int def)
 874{
 875        return ofnode_read_s32_default(dev_ofnode(dev), propname, def);
 876}
 877
 878static inline int dev_read_u32u(const struct udevice *dev,
 879                                const char *propname, uint *outp)
 880{
 881        u32 val;
 882        int ret;
 883
 884        ret = ofnode_read_u32(dev_ofnode(dev), propname, &val);
 885        if (ret)
 886                return ret;
 887        *outp = val;
 888
 889        return 0;
 890}
 891
 892static inline int dev_read_u64(const struct udevice *dev,
 893                               const char *propname, u64 *outp)
 894{
 895        return ofnode_read_u64(dev_ofnode(dev), propname, outp);
 896}
 897
 898static inline u64 dev_read_u64_default(const struct udevice *dev,
 899                                       const char *propname, u64 def)
 900{
 901        return ofnode_read_u64_default(dev_ofnode(dev), propname, def);
 902}
 903
 904static inline const char *dev_read_string(const struct udevice *dev,
 905                                          const char *propname)
 906{
 907        return ofnode_read_string(dev_ofnode(dev), propname);
 908}
 909
 910static inline bool dev_read_bool(const struct udevice *dev,
 911                                 const char *propname)
 912{
 913        return ofnode_read_bool(dev_ofnode(dev), propname);
 914}
 915
 916static inline ofnode dev_read_subnode(const struct udevice *dev,
 917                                      const char *subbnode_name)
 918{
 919        return ofnode_find_subnode(dev_ofnode(dev), subbnode_name);
 920}
 921
 922static inline int dev_read_size(const struct udevice *dev, const char *propname)
 923{
 924        return ofnode_read_size(dev_ofnode(dev), propname);
 925}
 926
 927static inline fdt_addr_t dev_read_addr_index(const struct udevice *dev,
 928                                             int index)
 929{
 930        return devfdt_get_addr_index(dev, index);
 931}
 932
 933static inline void *dev_read_addr_index_ptr(const struct udevice *dev,
 934                                            int index)
 935{
 936        return devfdt_get_addr_index_ptr(dev, index);
 937}
 938
 939static inline fdt_addr_t dev_read_addr_size_index(const struct udevice *dev,
 940                                                  int index,
 941                                                  fdt_size_t *size)
 942{
 943        return devfdt_get_addr_size_index(dev, index, size);
 944}
 945
 946static inline fdt_addr_t dev_read_addr_name(const struct udevice *dev,
 947                                            const char *name)
 948{
 949        return devfdt_get_addr_name(dev, name);
 950}
 951
 952static inline fdt_addr_t dev_read_addr_size_name(const struct udevice *dev,
 953                                                 const char *name,
 954                                                 fdt_size_t *size)
 955{
 956        return devfdt_get_addr_size_name(dev, name, size);
 957}
 958
 959static inline fdt_addr_t dev_read_addr(const struct udevice *dev)
 960{
 961        return devfdt_get_addr(dev);
 962}
 963
 964static inline void *dev_read_addr_ptr(const struct udevice *dev)
 965{
 966        return devfdt_get_addr_ptr(dev);
 967}
 968
 969static inline fdt_addr_t dev_read_addr_pci(const struct udevice *dev)
 970{
 971        return devfdt_get_addr_pci(dev);
 972}
 973
 974static inline void *dev_remap_addr(const struct udevice *dev)
 975{
 976        return devfdt_remap_addr(dev);
 977}
 978
 979static inline void *dev_remap_addr_index(const struct udevice *dev, int index)
 980{
 981        return devfdt_remap_addr_index(dev, index);
 982}
 983
 984static inline void *dev_remap_addr_name(const struct udevice *dev,
 985                                        const char *name)
 986{
 987        return devfdt_remap_addr_name(dev, name);
 988}
 989
 990static inline fdt_addr_t dev_read_addr_size(const struct udevice *dev,
 991                                            const char *propname,
 992                                            fdt_size_t *sizep)
 993{
 994        return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep);
 995}
 996
 997static inline const char *dev_read_name(const struct udevice *dev)
 998{
 999        return ofnode_get_name(dev_ofnode(dev));
1000}
1001
1002static inline int dev_read_stringlist_search(const struct udevice *dev,
1003                                             const char *propname,
1004                                             const char *string)
1005{
1006        return ofnode_stringlist_search(dev_ofnode(dev), propname, string);
1007}
1008
1009static inline int dev_read_string_index(const struct udevice *dev,
1010                                        const char *propname, int index,
1011                                        const char **outp)
1012{
1013        return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp);
1014}
1015
1016static inline int dev_read_string_count(const struct udevice *dev,
1017                                        const char *propname)
1018{
1019        return ofnode_read_string_count(dev_ofnode(dev), propname);
1020}
1021
1022static inline int dev_read_string_list(const struct udevice *dev,
1023                                       const char *propname,
1024                                       const char ***listp)
1025{
1026        return ofnode_read_string_list(dev_ofnode(dev), propname, listp);
1027}
1028
1029static inline int dev_read_phandle_with_args(const struct udevice *dev,
1030                const char *list_name, const char *cells_name, int cell_count,
1031                int index, struct ofnode_phandle_args *out_args)
1032{
1033        return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name,
1034                                              cells_name, cell_count, index,
1035                                              out_args);
1036}
1037
1038static inline int dev_count_phandle_with_args(const struct udevice *dev,
1039                const char *list_name, const char *cells_name, int cell_count)
1040{
1041        return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name,
1042                                              cells_name, cell_count);
1043}
1044
1045static inline int dev_read_addr_cells(const struct udevice *dev)
1046{
1047        int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
1048
1049        return fdt_address_cells(gd->fdt_blob, parent);
1050}
1051
1052static inline int dev_read_size_cells(const struct udevice *dev)
1053{
1054        int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev));
1055
1056        return fdt_size_cells(gd->fdt_blob, parent);
1057}
1058
1059static inline int dev_read_simple_addr_cells(const struct udevice *dev)
1060{
1061        return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev));
1062}
1063
1064static inline int dev_read_simple_size_cells(const struct udevice *dev)
1065{
1066        return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev));
1067}
1068
1069static inline int dev_read_phandle(const struct udevice *dev)
1070{
1071        return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev));
1072}
1073
1074static inline const void *dev_read_prop(const struct udevice *dev,
1075                                        const char *propname, int *lenp)
1076{
1077        return ofnode_get_property(dev_ofnode(dev), propname, lenp);
1078}
1079
1080static inline int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop)
1081{
1082        return ofnode_first_property(dev_ofnode(dev), prop);
1083}
1084
1085static inline int dev_read_next_prop(struct ofprop *prop)
1086{
1087        return ofnode_next_property(prop);
1088}
1089
1090static inline const void *dev_read_prop_by_prop(struct ofprop *prop,
1091                                                const char **propname,
1092                                                int *lenp)
1093{
1094        return ofprop_get_property(prop, propname, lenp);
1095}
1096
1097static inline int dev_read_alias_seq(const struct udevice *dev, int *devnump)
1098{
1099#if CONFIG_IS_ENABLED(OF_CONTROL)
1100        return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
1101                                    dev_of_offset(dev), devnump);
1102#else
1103        return -ENOTSUPP;
1104#endif
1105}
1106
1107static inline int dev_read_u32_array(const struct udevice *dev,
1108                                     const char *propname, u32 *out_values,
1109                                     size_t sz)
1110{
1111        return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz);
1112}
1113
1114static inline ofnode dev_read_first_subnode(const struct udevice *dev)
1115{
1116        return ofnode_first_subnode(dev_ofnode(dev));
1117}
1118
1119static inline ofnode dev_read_next_subnode(ofnode node)
1120{
1121        return ofnode_next_subnode(node);
1122}
1123
1124static inline const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev,
1125                                                   const char *propname,
1126                                                   size_t sz)
1127{
1128        return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz);
1129}
1130
1131static inline int dev_read_enabled(const struct udevice *dev)
1132{
1133        return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev));
1134}
1135
1136static inline int dev_read_resource(const struct udevice *dev, uint index,
1137                                    struct resource *res)
1138{
1139        return ofnode_read_resource(dev_ofnode(dev), index, res);
1140}
1141
1142static inline int dev_read_resource_byname(const struct udevice *dev,
1143                                           const char *name,
1144                                           struct resource *res)
1145{
1146        return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
1147}
1148
1149static inline u64 dev_translate_address(const struct udevice *dev,
1150                                        const fdt32_t *in_addr)
1151{
1152        return ofnode_translate_address(dev_ofnode(dev), in_addr);
1153}
1154
1155static inline u64 dev_translate_dma_address(const struct udevice *dev,
1156                                            const fdt32_t *in_addr)
1157{
1158        return ofnode_translate_dma_address(dev_ofnode(dev), in_addr);
1159}
1160
1161static inline int dev_get_dma_range(const struct udevice *dev, phys_addr_t *cpu,
1162                                    dma_addr_t *bus, u64 *size)
1163{
1164        return ofnode_get_dma_range(dev_ofnode(dev), cpu, bus, size);
1165}
1166
1167static inline int dev_read_alias_highest_id(const char *stem)
1168{
1169        if (!CONFIG_IS_ENABLED(OF_LIBFDT) || !gd->fdt_blob)
1170                return -1;
1171        return fdtdec_get_alias_highest_id(gd->fdt_blob, stem);
1172}
1173
1174static inline int dev_get_child_count(const struct udevice *dev)
1175{
1176        return ofnode_get_child_count(dev_ofnode(dev));
1177}
1178
1179static inline int dev_decode_display_timing(const struct udevice *dev,
1180                                            int index,
1181                                            struct display_timing *config)
1182{
1183        return ofnode_decode_display_timing(dev_ofnode(dev), index, config);
1184}
1185
1186static inline ofnode dev_get_phy_node(const struct udevice *dev)
1187{
1188        return ofnode_get_phy_node(dev_ofnode(dev));
1189}
1190
1191static inline phy_interface_t dev_read_phy_mode(const struct udevice *dev)
1192{
1193        return ofnode_read_phy_mode(dev_ofnode(dev));
1194}
1195
1196#endif /* CONFIG_DM_DEV_READ_INLINE */
1197
1198/**
1199 * dev_for_each_subnode() - Helper function to iterate through subnodes
1200 *
1201 * This creates a for() loop which works through the subnodes in a device's
1202 * device-tree node.
1203 *
1204 * @subnode: ofnode holding the current subnode
1205 * @dev: device to use for interation (`struct udevice *`)
1206 */
1207#define dev_for_each_subnode(subnode, dev) \
1208        for (subnode = dev_read_first_subnode(dev); \
1209             ofnode_valid(subnode); \
1210             subnode = ofnode_next_subnode(subnode))
1211
1212/**
1213 * dev_for_each_property() - Helper function to iterate through property
1214 *
1215 * This creates a for() loop which works through the property in a device's
1216 * device-tree node.
1217 *
1218 * @prop: struct ofprop holding the current property
1219 * @dev: device to use for interation (`struct udevice *`)
1220 */
1221#define dev_for_each_property(prop, dev) \
1222        for (int ret_prop = dev_read_first_prop(dev, &prop); \
1223             !ret_prop; \
1224             ret_prop = dev_read_next_prop(&prop))
1225
1226#endif
1227