1 /* 2 * Function to read values from the device tree node attached to a udevice. 3 * 4 * Copyright (c) 2017 Google, Inc 5 * Written by Simon Glass <sjg@chromium.org> 6 * 7 * SPDX-License-Identifier: GPL-2.0+ 8 */ 9 10 #ifndef _DM_READ_H 11 #define _DM_READ_H 12 13 #include <dm/fdtaddr.h> 14 #include <dm/ofnode.h> 15 #include <dm/uclass.h> 16 17 struct resource; 18 19 #if CONFIG_IS_ENABLED(OF_LIVE) 20 static inline const struct device_node *dev_np(struct udevice *dev) 21 { 22 return ofnode_to_np(dev->node); 23 } 24 #else 25 static inline const struct device_node *dev_np(struct udevice *dev) 26 { 27 return NULL; 28 } 29 #endif 30 31 /** 32 * dev_ofnode() - get the DT node reference associated with a udevice 33 * 34 * @dev: device to check 35 * @return reference of the the device's DT node 36 */ 37 static inline ofnode dev_ofnode(struct udevice *dev) 38 { 39 return dev->node; 40 } 41 42 static inline bool dev_of_valid(struct udevice *dev) 43 { 44 return ofnode_valid(dev_ofnode(dev)); 45 } 46 47 #ifndef CONFIG_DM_DEV_READ_INLINE 48 /** 49 * dev_read_u32_default() - read a 32-bit integer from a device's DT property 50 * 51 * @dev: device to read DT property from 52 * @propname: name of the property to read from 53 * @def: default value to return if the property has no value 54 * @return property value, or @def if not found 55 */ 56 int dev_read_u32_default(struct udevice *dev, const char *propname, int def); 57 58 /** 59 * dev_read_s32_default() - read a signed 32-bit integer from a device's DT property 60 * 61 * @dev: device to read DT property from 62 * @propname: name of the property to read from 63 * @def: default value to return if the property has no value 64 * @return property value, or @def if not found 65 */ 66 int dev_read_s32_default(struct udevice *dev, const char *propname, int def); 67 68 /** 69 * dev_read_string() - Read a string from a device's DT property 70 * 71 * @dev: device to read DT property from 72 * @propname: name of the property to read 73 * @return string from property value, or NULL if there is no such property 74 */ 75 const char *dev_read_string(struct udevice *dev, const char *propname); 76 77 /** 78 * dev_read_bool() - read a boolean value from a device's DT property 79 * 80 * @dev: device to read DT property from 81 * @propname: name of property to read 82 * @return true if property is present (meaning true), false if not present 83 */ 84 bool dev_read_bool(struct udevice *dev, const char *propname); 85 86 /** 87 * dev_read_subnode() - find a named subnode of a device 88 * 89 * @dev: device whose DT node contains the subnode 90 * @subnode_name: name of subnode to find 91 * @return reference to subnode (which can be invalid if there is no such 92 * subnode) 93 */ 94 ofnode dev_read_subnode(struct udevice *dev, const char *subbnode_name); 95 96 /** 97 * dev_read_size() - read the size of a property 98 * 99 * @dev: device to check 100 * @propname: property to check 101 * @return size of property if present, or -EINVAL if not 102 */ 103 int dev_read_size(struct udevice *dev, const char *propname); 104 105 /** 106 * dev_read_addr_index() - Get the indexed reg property of a device 107 * 108 * @dev: Device to read from 109 * @index: the 'reg' property can hold a list of <addr, size> pairs 110 * and @index is used to select which one is required 111 * 112 * @return address or FDT_ADDR_T_NONE if not found 113 */ 114 fdt_addr_t dev_read_addr_index(struct udevice *dev, int index); 115 116 /** 117 * dev_read_addr() - Get the reg property of a device 118 * 119 * @dev: Device to read from 120 * 121 * @return address or FDT_ADDR_T_NONE if not found 122 */ 123 fdt_addr_t dev_read_addr(struct udevice *dev); 124 125 /** 126 * dev_read_addr_ptr() - Get the reg property of a device 127 * as a pointer 128 * 129 * @dev: Device to read from 130 * 131 * @return pointer or NULL if not found 132 */ 133 void *dev_read_addr_ptr(struct udevice *dev); 134 135 /** 136 * dev_read_addr_size() - get address and size from a device property 137 * 138 * This does no address translation. It simply reads an property that contains 139 * an address and a size value, one after the other. 140 * 141 * @dev: Device to read from 142 * @propname: property to read 143 * @sizep: place to put size value (on success) 144 * @return address value, or FDT_ADDR_T_NONE on error 145 */ 146 fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname, 147 fdt_size_t *sizep); 148 149 /** 150 * dev_read_name() - get the name of a device's node 151 * 152 * @node: valid node to look up 153 * @return name of node 154 */ 155 const char *dev_read_name(struct udevice *dev); 156 157 /** 158 * dev_read_stringlist_search() - find string in a string list and return index 159 * 160 * Note that it is possible for this function to succeed on property values 161 * that are not NUL-terminated. That's because the function will stop after 162 * finding the first occurrence of @string. This can for example happen with 163 * small-valued cell properties, such as #address-cells, when searching for 164 * the empty string. 165 * 166 * @dev: device to check 167 * @propname: name of the property containing the string list 168 * @string: string to look up in the string list 169 * 170 * @return: 171 * the index of the string in the list of strings 172 * -ENODATA if the property is not found 173 * -EINVAL on some other error 174 */ 175 int dev_read_stringlist_search(struct udevice *dev, const char *property, 176 const char *string); 177 178 /** 179 * dev_read_string_index() - obtain an indexed string from a string list 180 * 181 * @dev: device to examine 182 * @propname: name of the property containing the string list 183 * @index: index of the string to return 184 * @out: return location for the string 185 * 186 * @return: 187 * length of string, if found or -ve error value if not found 188 */ 189 int dev_read_string_index(struct udevice *dev, const char *propname, int index, 190 const char **outp); 191 192 /** 193 * dev_read_string_count() - find the number of strings in a string list 194 * 195 * @dev: device to examine 196 * @propname: name of the property containing the string list 197 * @return: 198 * number of strings in the list, or -ve error value if not found 199 */ 200 int dev_read_string_count(struct udevice *dev, const char *propname); 201 /** 202 * dev_read_phandle_with_args() - Find a node pointed by phandle in a list 203 * 204 * This function is useful to parse lists of phandles and their arguments. 205 * Returns 0 on success and fills out_args, on error returns appropriate 206 * errno value. 207 * 208 * Caller is responsible to call of_node_put() on the returned out_args->np 209 * pointer. 210 * 211 * Example: 212 * 213 * phandle1: node1 { 214 * #list-cells = <2>; 215 * } 216 * 217 * phandle2: node2 { 218 * #list-cells = <1>; 219 * } 220 * 221 * node3 { 222 * list = <&phandle1 1 2 &phandle2 3>; 223 * } 224 * 225 * To get a device_node of the `node2' node you may call this: 226 * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args); 227 * 228 * @dev: device whose node containing a list 229 * @list_name: property name that contains a list 230 * @cells_name: property name that specifies phandles' arguments count 231 * @cells_count: Cell count to use if @cells_name is NULL 232 * @index: index of a phandle to parse out 233 * @out_args: optional pointer to output arguments structure (will be filled) 234 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if 235 * @list_name does not exist, -EINVAL if a phandle was not found, 236 * @cells_name could not be found, the arguments were truncated or there 237 * were too many arguments. 238 */ 239 int dev_read_phandle_with_args(struct udevice *dev, const char *list_name, 240 const char *cells_name, int cell_count, 241 int index, 242 struct ofnode_phandle_args *out_args); 243 244 /** 245 * dev_count_phandle_with_args() - Return phandle number in a list 246 * 247 * This function is usefull to get phandle number contained in a property list. 248 * For example, this allows to allocate the right amount of memory to keep 249 * clock's reference contained into the "clocks" property. 250 * 251 * 252 * @dev: device whose node containing a list 253 * @list_name: property name that contains a list 254 * @cells_name: property name that specifies phandles' arguments count 255 * @Returns number of phandle found on success, on error returns appropriate 256 * errno value. 257 */ 258 259 int dev_count_phandle_with_args(struct udevice *dev, const char *list_name, 260 const char *cells_name); 261 262 /** 263 * dev_read_addr_cells() - Get the number of address cells for a device's node 264 * 265 * This walks back up the tree to find the closest #address-cells property 266 * which controls the given node. 267 * 268 * @dev: devioe to check 269 * @return number of address cells this node uses 270 */ 271 int dev_read_addr_cells(struct udevice *dev); 272 273 /** 274 * dev_remap_addr_index() - Get the indexed reg property of a device 275 * as a memory-mapped I/O pointer 276 * 277 * @dev: Device to read from 278 * @index: the 'reg' property can hold a list of <addr, size> pairs 279 * and @index is used to select which one is required 280 * 281 * Return: pointer or NULL if not found 282 */ 283 void *dev_remap_addr_index(struct udevice *dev, int index); 284 285 /** 286 * dev_read_size_cells() - Get the number of size cells for a device's node 287 * 288 * This walks back up the tree to find the closest #size-cells property 289 * which controls the given node. 290 * 291 * @dev: devioe to check 292 * @return number of size cells this node uses 293 */ 294 int dev_read_size_cells(struct udevice *dev); 295 296 /** 297 * dev_read_addr_cells() - Get the address cells property in a node 298 * 299 * This function matches fdt_address_cells(). 300 * 301 * @dev: devioe to check 302 * @return number of address cells this node uses 303 */ 304 int dev_read_simple_addr_cells(struct udevice *dev); 305 306 /** 307 * dev_read_size_cells() - Get the size cells property in a node 308 * 309 * This function matches fdt_size_cells(). 310 * 311 * @dev: devioe to check 312 * @return number of size cells this node uses 313 */ 314 int dev_read_simple_size_cells(struct udevice *dev); 315 316 /** 317 * dev_read_phandle() - Get the phandle from a device 318 * 319 * @dev: device to check 320 * @return phandle (1 or greater), or 0 if no phandle or other error 321 */ 322 int dev_read_phandle(struct udevice *dev); 323 324 /** 325 * dev_read_prop()- - read a property from a device's node 326 * 327 * @dev: device to check 328 * @propname: property to read 329 * @lenp: place to put length on success 330 * @return pointer to property, or NULL if not found 331 */ 332 const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp); 333 334 /** 335 * dev_read_first_prop()- get the reference of the first property 336 * 337 * Get reference to the first property of the node, it is used to iterate 338 * and read all the property with dev_read_prop_by_prop(). 339 * 340 * @dev: device to check 341 * @prop: place to put argument reference 342 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found 343 */ 344 int dev_read_first_prop(struct udevice *dev, struct ofprop *prop); 345 346 /** 347 * ofnode_get_next_property() - get the reference of the next property 348 * 349 * Get reference to the next property of the node, it is used to iterate 350 * and read all the property with dev_read_prop_by_prop(). 351 * 352 * @prop: reference of current argument and place to put reference of next one 353 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found 354 */ 355 int dev_read_next_prop(struct ofprop *prop); 356 357 /** 358 * dev_read_prop_by_prop() - get a pointer to the value of a property 359 * 360 * Get value for the property identified by the provided reference. 361 * 362 * @prop: reference on property 363 * @propname: If non-NULL, place to property name on success, 364 * @lenp: If non-NULL, place to put length on success 365 * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found 366 */ 367 const void *dev_read_prop_by_prop(struct ofprop *prop, 368 const char **propname, int *lenp); 369 370 /** 371 * dev_read_alias_seq() - Get the alias sequence number of a node 372 * 373 * This works out whether a node is pointed to by an alias, and if so, the 374 * sequence number of that alias. Aliases are of the form <base><num> where 375 * <num> is the sequence number. For example spi2 would be sequence number 2. 376 * 377 * @dev: device to look up 378 * @devnump: set to the sequence number if one is found 379 * @return 0 if a sequence was found, -ve if not 380 */ 381 int dev_read_alias_seq(struct udevice *dev, int *devnump); 382 383 /** 384 * dev_read_u32_array() - Find and read an array of 32 bit integers 385 * 386 * Search for a property in a device node and read 32-bit value(s) from 387 * it. 388 * 389 * The out_values is modified only if a valid u32 value can be decoded. 390 * 391 * @dev: device to look up 392 * @propname: name of the property to read 393 * @out_values: pointer to return value, modified only if return value is 0 394 * @sz: number of array elements to read 395 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if 396 * property does not have a value, and -EOVERFLOW if the property data isn't 397 * large enough. 398 */ 399 int dev_read_u32_array(struct udevice *dev, const char *propname, 400 u32 *out_values, size_t sz); 401 402 /** 403 * dev_write_u32_array() - Find and write an array of 32 bit integers 404 * 405 * Search for a property in a device node and write 32-bit value(s) to 406 * it. 407 * 408 * The out_values is modified only if a valid u32 value can be decoded. 409 * 410 * @dev: device to look up 411 * @propname: name of the property to read 412 * @values: pointer to update value, modified only if return value is 0 413 * @sz: number of array elements to read 414 * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if 415 * property does not have a value, and -EOVERFLOW if the property data isn't 416 * large enough. 417 */ 418 int dev_write_u32_array(struct udevice *dev, const char *propname, 419 u32 *values, size_t sz); 420 421 /** 422 * dev_read_first_subnode() - find the first subnode of a device's node 423 * 424 * @dev: device to look up 425 * @return reference to the first subnode (which can be invalid if the device's 426 * node has no subnodes) 427 */ 428 ofnode dev_read_first_subnode(struct udevice *dev); 429 430 /** 431 * ofnode_next_subnode() - find the next sibling of a subnode 432 * 433 * @node: valid reference to previous node (sibling) 434 * @return reference to the next subnode (which can be invalid if the node 435 * has no more siblings) 436 */ 437 ofnode dev_read_next_subnode(ofnode node); 438 439 /** 440 * dev_read_u8_array_ptr() - find an 8-bit array 441 * 442 * Look up a device's node property and return a pointer to its contents as a 443 * byte array of given length. The property must have at least enough data 444 * for the array (count bytes). It may have more, but this will be ignored. 445 * The data is not copied. 446 * 447 * @dev: device to look up 448 * @propname: name of property to find 449 * @sz: number of array elements 450 * @return pointer to byte array if found, or NULL if the property is not 451 * found or there is not enough data 452 */ 453 const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname, 454 size_t sz); 455 456 /** 457 * dev_read_enabled() - check whether a node is enabled 458 * 459 * This looks for a 'status' property. If this exists, then returns 1 if 460 * the status is 'ok' and 0 otherwise. If there is no status property, 461 * it returns 1 on the assumption that anything mentioned should be enabled 462 * by default. 463 * 464 * @dev: device to examine 465 * @return integer value 0 (not enabled) or 1 (enabled) 466 */ 467 int dev_read_enabled(struct udevice *dev); 468 469 /** 470 * dev_read_resource() - obtain an indexed resource from a device. 471 * 472 * @dev: device to examine 473 * @index index of the resource to retrieve (0 = first) 474 * @res returns the resource 475 * @return 0 if ok, negative on error 476 */ 477 int dev_read_resource(struct udevice *dev, uint index, struct resource *res); 478 479 /** 480 * dev_read_resource_byname() - obtain a named resource from a device. 481 * 482 * @dev: device to examine 483 * @name: name of the resource to retrieve 484 * @res: returns the resource 485 * @return 0 if ok, negative on error 486 */ 487 int dev_read_resource_byname(struct udevice *dev, const char *name, 488 struct resource *res); 489 490 /** 491 * dev_translate_address() - Tranlate a device-tree address 492 * 493 * Translate an address from the device-tree into a CPU physical address. This 494 * function walks up the tree and applies the various bus mappings along the 495 * way. 496 * 497 * @dev: device giving the context in which to translate the address 498 * @in_addr: pointer to the address to translate 499 * @return the translated address; OF_BAD_ADDR on error 500 */ 501 u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr); 502 #else /* CONFIG_DM_DEV_READ_INLINE is enabled */ 503 504 static inline int dev_read_u32_default(struct udevice *dev, 505 const char *propname, int def) 506 { 507 return ofnode_read_u32_default(dev_ofnode(dev), propname, def); 508 } 509 510 static inline const char *dev_read_string(struct udevice *dev, 511 const char *propname) 512 { 513 return ofnode_read_string(dev_ofnode(dev), propname); 514 } 515 516 static inline bool dev_read_bool(struct udevice *dev, const char *propname) 517 { 518 return ofnode_read_bool(dev_ofnode(dev), propname); 519 } 520 521 static inline ofnode dev_read_subnode(struct udevice *dev, 522 const char *subbnode_name) 523 { 524 return ofnode_find_subnode(dev_ofnode(dev), subbnode_name); 525 } 526 527 static inline int dev_read_size(struct udevice *dev, const char *propname) 528 { 529 return ofnode_read_size(dev_ofnode(dev), propname); 530 } 531 532 static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index) 533 { 534 return devfdt_get_addr_index(dev, index); 535 } 536 537 static inline fdt_addr_t dev_read_addr(struct udevice *dev) 538 { 539 return devfdt_get_addr(dev); 540 } 541 542 static inline void *dev_read_addr_ptr(struct udevice *dev) 543 { 544 return devfdt_get_addr_ptr(dev); 545 } 546 547 static inline fdt_addr_t dev_read_addr_size(struct udevice *dev, 548 const char *propname, 549 fdt_size_t *sizep) 550 { 551 return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep); 552 } 553 554 static inline const char *dev_read_name(struct udevice *dev) 555 { 556 if (!dev_of_valid(dev)) 557 return NULL; 558 return ofnode_get_name(dev_ofnode(dev)); 559 } 560 561 static inline int dev_read_stringlist_search(struct udevice *dev, 562 const char *propname, 563 const char *string) 564 { 565 return ofnode_stringlist_search(dev_ofnode(dev), propname, string); 566 } 567 568 static inline int dev_read_string_index(struct udevice *dev, 569 const char *propname, int index, 570 const char **outp) 571 { 572 return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp); 573 } 574 575 static inline int dev_read_string_count(struct udevice *dev, 576 const char *propname) 577 { 578 return ofnode_read_string_count(dev_ofnode(dev), propname); 579 } 580 581 static inline int dev_read_phandle_with_args(struct udevice *dev, 582 const char *list_name, const char *cells_name, int cell_count, 583 int index, struct ofnode_phandle_args *out_args) 584 { 585 return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name, 586 cells_name, cell_count, index, 587 out_args); 588 } 589 590 static inline int dev_count_phandle_with_args(struct udevice *dev, 591 const char *list_name, const char *cells_name) 592 { 593 return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name, 594 cells_name); 595 } 596 597 static inline int dev_read_addr_cells(struct udevice *dev) 598 { 599 /* NOTE: this call should walk up the parent stack */ 600 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev)); 601 } 602 603 static inline void *dev_remap_addr_index(struct udevice *dev, int index) 604 { 605 return devfdt_remap_addr_index(dev, index); 606 } 607 608 static inline int dev_read_size_cells(struct udevice *dev) 609 { 610 /* NOTE: this call should walk up the parent stack */ 611 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev)); 612 } 613 614 static inline int dev_read_simple_addr_cells(struct udevice *dev) 615 { 616 return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev)); 617 } 618 619 static inline int dev_read_simple_size_cells(struct udevice *dev) 620 { 621 return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev)); 622 } 623 624 static inline int dev_read_phandle(struct udevice *dev) 625 { 626 return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev)); 627 } 628 629 static inline const void *dev_read_prop(struct udevice *dev, 630 const char *propname, int *lenp) 631 { 632 return ofnode_get_property(dev_ofnode(dev), propname, lenp); 633 } 634 635 static inline int dev_read_first_prop(struct udevice *dev, struct ofprop *prop) 636 { 637 return ofnode_get_first_property(dev_ofnode(dev), prop); 638 } 639 640 static inline int dev_read_next_prop(struct ofprop *prop) 641 { 642 return ofnode_get_next_property(prop); 643 } 644 645 static inline const void *dev_read_prop_by_prop(struct ofprop *prop, 646 const char **propname, 647 int *lenp) 648 { 649 return ofnode_get_property_by_prop(prop, propname, lenp); 650 } 651 652 static inline int dev_read_alias_seq(struct udevice *dev, int *devnump) 653 { 654 return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name, 655 dev_of_offset(dev), devnump); 656 } 657 658 static inline int dev_read_u32_array(struct udevice *dev, const char *propname, 659 u32 *out_values, size_t sz) 660 { 661 if (!dev_of_valid(dev)) 662 return -EINVAL; 663 return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz); 664 } 665 666 static inline ofnode dev_read_first_subnode(struct udevice *dev) 667 { 668 return ofnode_first_subnode(dev_ofnode(dev)); 669 } 670 671 static inline ofnode dev_read_next_subnode(ofnode node) 672 { 673 return ofnode_next_subnode(node); 674 } 675 676 static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, 677 const char *propname, size_t sz) 678 { 679 return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz); 680 } 681 682 static inline int dev_read_enabled(struct udevice *dev) 683 { 684 return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev)); 685 } 686 687 static inline int dev_read_resource(struct udevice *dev, uint index, 688 struct resource *res) 689 { 690 return ofnode_read_resource(dev_ofnode(dev), index, res); 691 } 692 693 static inline int dev_read_resource_byname(struct udevice *dev, 694 const char *name, 695 struct resource *res) 696 { 697 return ofnode_read_resource_byname(dev_ofnode(dev), name, res); 698 } 699 700 static inline u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr) 701 { 702 return ofnode_translate_address(dev_ofnode(dev), in_addr); 703 } 704 705 #endif /* CONFIG_DM_DEV_READ_INLINE */ 706 707 /** 708 * dev_for_each_subnode() - Helper function to iterate through subnodes 709 * 710 * This creates a for() loop which works through the subnodes in a device's 711 * device-tree node. 712 * 713 * @subnode: ofnode holding the current subnode 714 * @dev: device to use for interation (struct udevice *) 715 */ 716 #define dev_for_each_subnode(subnode, dev) \ 717 for (subnode = dev_read_first_subnode(dev); \ 718 ofnode_valid(subnode); \ 719 subnode = ofnode_next_subnode(subnode)) 720 721 /** 722 * dev_for_each_property() - Helper function to iterate through property 723 * 724 * This creates a for() loop which works through the property in a device's 725 * device-tree node. 726 * 727 * @prop: struct ofprop holding the current property 728 * @dev: device to use for interation (struct udevice *) 729 */ 730 #define dev_for_each_property(prop, dev) \ 731 for (int ret_prop = dev_read_first_prop(dev, &prop); \ 732 !ret_prop; \ 733 ret_prop = dev_read_next_prop(&prop)) 734 735 #endif 736