1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 #ifndef _LINUX_OF_H 3 #define _LINUX_OF_H 4 /* 5 * Definitions for talking to the Open Firmware PROM on 6 * Power Macintosh and other computers. 7 * 8 * Copyright (C) 1996-2005 Paul Mackerras. 9 * 10 * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp. 11 * Updates for SPARC64 by David S. Miller 12 * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp. 13 */ 14 #include <linux/types.h> 15 #include <linux/bitops.h> 16 #include <linux/cleanup.h> 17 #include <linux/errno.h> 18 #include <linux/kobject.h> 19 #include <linux/device-id/of.h> 20 #include <linux/property.h> 21 #include <linux/list.h> 22 23 #include <asm/byteorder.h> 24 25 typedef u32 phandle; 26 typedef u32 ihandle; 27 28 struct property { 29 char *name; 30 int length; 31 void *value; 32 struct property *next; 33 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC) 34 unsigned long _flags; 35 #endif 36 #if defined(CONFIG_OF_PROMTREE) 37 unsigned int unique_id; 38 #endif 39 #if defined(CONFIG_OF_KOBJ) 40 struct bin_attribute attr; 41 #endif 42 }; 43 44 #if defined(CONFIG_SPARC) 45 struct of_irq_controller; 46 #endif 47 48 struct device_node { 49 const char *name; 50 phandle phandle; 51 const char *full_name; 52 struct fwnode_handle fwnode; 53 54 struct property *properties; 55 struct property *deadprops; /* removed properties */ 56 struct device_node *parent; 57 struct device_node *child; 58 struct device_node *sibling; 59 #if defined(CONFIG_OF_KOBJ) 60 struct kobject kobj; 61 #endif 62 unsigned long _flags; 63 void *data; 64 #if defined(CONFIG_SPARC) 65 unsigned int unique_id; 66 struct of_irq_controller *irq_trans; 67 #endif 68 }; 69 70 #define MAX_PHANDLE_ARGS NR_FWNODE_REFERENCE_ARGS 71 struct of_phandle_args { 72 struct device_node *np; 73 int args_count; 74 uint32_t args[MAX_PHANDLE_ARGS]; 75 }; 76 77 struct of_phandle_iterator { 78 /* Common iterator information */ 79 const char *cells_name; 80 int cell_count; 81 const struct device_node *parent; 82 83 /* List size information */ 84 const __be32 *list_end; 85 const __be32 *phandle_end; 86 87 /* Current position state */ 88 const __be32 *cur; 89 uint32_t cur_count; 90 phandle phandle; 91 struct device_node *node; 92 }; 93 94 struct of_reconfig_data { 95 struct device_node *dn; 96 struct property *prop; 97 struct property *old_prop; 98 }; 99 100 extern const struct kobj_type of_node_ktype; 101 extern const struct fwnode_operations of_fwnode_ops; 102 103 /** 104 * of_node_init - initialize a devicetree node 105 * @node: Pointer to device node that has been created by kzalloc() 106 * 107 * On return the device_node refcount is set to one. Use of_node_put() 108 * on @node when done to free the memory allocated for it. If the node 109 * is NOT a dynamic node the memory will not be freed. The decision of 110 * whether to free the memory will be done by node->release(), which is 111 * of_node_release(). 112 */ 113 static inline void of_node_init(struct device_node *node) 114 { 115 #if defined(CONFIG_OF_KOBJ) 116 kobject_init(&node->kobj, &of_node_ktype); 117 #endif 118 fwnode_init(&node->fwnode, &of_fwnode_ops); 119 } 120 121 #if defined(CONFIG_OF_KOBJ) 122 #define of_node_kobj(n) (&(n)->kobj) 123 #else 124 #define of_node_kobj(n) NULL 125 #endif 126 127 #ifdef CONFIG_OF_DYNAMIC 128 extern struct device_node *of_node_get(struct device_node *node); 129 extern void of_node_put(struct device_node *node); 130 #else /* CONFIG_OF_DYNAMIC */ 131 /* Dummy ref counting routines - to be implemented later */ 132 static inline struct device_node *of_node_get(struct device_node *node) 133 { 134 return node; 135 } 136 static inline void of_node_put(struct device_node *node) { } 137 #endif /* !CONFIG_OF_DYNAMIC */ 138 DEFINE_FREE(device_node, struct device_node *, if (_T) of_node_put(_T)) 139 140 /* Pointer for first entry in chain of all nodes. */ 141 extern struct device_node *of_root; 142 extern struct device_node *of_chosen; 143 extern struct device_node *of_aliases; 144 extern struct device_node *of_stdout; 145 146 /* 147 * struct device_node flag descriptions 148 * (need to be visible even when !CONFIG_OF) 149 */ 150 #define OF_DYNAMIC 1 /* (and properties) allocated via kmalloc */ 151 #define OF_DETACHED 2 /* detached from the device tree */ 152 #define OF_POPULATED 3 /* device already created */ 153 #define OF_POPULATED_BUS 4 /* platform bus created for children */ 154 #define OF_OVERLAY 5 /* allocated for an overlay */ 155 #define OF_OVERLAY_FREE_CSET 6 /* in overlay cset being freed */ 156 157 #define OF_BAD_ADDR ((u64)-1) 158 159 #ifdef CONFIG_OF 160 void of_core_init(void); 161 162 static inline bool is_of_node(const struct fwnode_handle *fwnode) 163 { 164 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &of_fwnode_ops; 165 } 166 167 #define to_of_node(__fwnode) \ 168 ({ \ 169 typeof(__fwnode) __to_of_node_fwnode = (__fwnode); \ 170 \ 171 is_of_node(__to_of_node_fwnode) ? \ 172 container_of(__to_of_node_fwnode, \ 173 struct device_node, fwnode) : \ 174 NULL; \ 175 }) 176 177 #define of_fwnode_handle(node) \ 178 ({ \ 179 typeof(node) __of_fwnode_handle_node = (node); \ 180 \ 181 __of_fwnode_handle_node ? \ 182 &__of_fwnode_handle_node->fwnode : NULL; \ 183 }) 184 185 static inline bool of_node_is_root(const struct device_node *node) 186 { 187 return node && (node->parent == NULL); 188 } 189 190 static inline int of_node_check_flag(const struct device_node *n, unsigned long flag) 191 { 192 return test_bit(flag, &n->_flags); 193 } 194 195 static inline int of_node_test_and_set_flag(struct device_node *n, 196 unsigned long flag) 197 { 198 return test_and_set_bit(flag, &n->_flags); 199 } 200 201 static inline void of_node_set_flag(struct device_node *n, unsigned long flag) 202 { 203 set_bit(flag, &n->_flags); 204 } 205 206 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag) 207 { 208 clear_bit(flag, &n->_flags); 209 } 210 211 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC) 212 static inline int of_property_check_flag(const struct property *p, unsigned long flag) 213 { 214 return test_bit(flag, &p->_flags); 215 } 216 217 static inline void of_property_set_flag(struct property *p, unsigned long flag) 218 { 219 set_bit(flag, &p->_flags); 220 } 221 222 static inline void of_property_clear_flag(struct property *p, unsigned long flag) 223 { 224 clear_bit(flag, &p->_flags); 225 } 226 #endif 227 228 extern struct device_node *__of_find_all_nodes(struct device_node *prev); 229 extern struct device_node *of_find_all_nodes(struct device_node *prev); 230 231 /* 232 * OF address retrieval & translation 233 */ 234 235 /* Helper to read a big number; size is in cells (not bytes) */ 236 static inline u64 of_read_number(const __be32 *cell, int size) 237 { 238 u64 r = 0; 239 for (; size--; cell++) 240 r = (r << 32) | be32_to_cpu(*cell); 241 return r; 242 } 243 244 /* Like of_read_number, but we want an unsigned long result */ 245 static inline unsigned long of_read_ulong(const __be32 *cell, int size) 246 { 247 /* toss away upper bits if unsigned long is smaller than u64 */ 248 return of_read_number(cell, size); 249 } 250 251 #if defined(CONFIG_SPARC) 252 #include <asm/prom.h> 253 #endif 254 255 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags) 256 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags) 257 258 extern bool of_node_name_eq(const struct device_node *np, const char *name); 259 extern bool of_node_name_prefix(const struct device_node *np, const char *prefix); 260 261 static inline const char *of_node_full_name(const struct device_node *np) 262 { 263 return np ? np->full_name : "<no-node>"; 264 } 265 266 #define for_each_of_allnodes_from(from, dn) \ 267 for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn)) 268 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn) 269 extern struct device_node *of_find_node_by_name(struct device_node *from, 270 const char *name); 271 extern struct device_node *of_find_node_by_type(struct device_node *from, 272 const char *type); 273 extern struct device_node *of_find_compatible_node(struct device_node *from, 274 const char *type, const char *compat); 275 extern struct device_node *of_find_matching_node_and_match( 276 struct device_node *from, 277 const struct of_device_id *matches, 278 const struct of_device_id **match); 279 280 extern struct device_node *of_find_node_opts_by_path(const char *path, 281 const char **opts); 282 static inline struct device_node *of_find_node_by_path(const char *path) 283 { 284 return of_find_node_opts_by_path(path, NULL); 285 } 286 287 extern struct device_node *of_find_node_by_phandle(phandle handle); 288 extern struct device_node *of_get_parent(const struct device_node *node); 289 extern struct device_node *of_get_next_parent(struct device_node *node); 290 extern struct device_node *of_get_next_child(const struct device_node *node, 291 struct device_node *prev); 292 extern struct device_node *of_get_next_child_with_prefix(const struct device_node *node, 293 struct device_node *prev, 294 const char *prefix); 295 extern struct device_node *of_get_next_available_child( 296 const struct device_node *node, struct device_node *prev); 297 extern struct device_node *of_get_next_reserved_child( 298 const struct device_node *node, struct device_node *prev); 299 300 extern struct device_node *of_get_compatible_child(const struct device_node *parent, 301 const char *compatible); 302 extern struct device_node *of_get_child_by_name(const struct device_node *node, 303 const char *name); 304 extern struct device_node *of_get_available_child_by_name(const struct device_node *node, 305 const char *name); 306 307 /* cache lookup */ 308 extern struct device_node *of_find_next_cache_node(const struct device_node *); 309 extern int of_find_last_cache_level(unsigned int cpu); 310 extern struct device_node *of_find_node_with_property( 311 struct device_node *from, const char *prop_name); 312 313 extern struct property *of_find_property(const struct device_node *np, 314 const char *name, 315 int *lenp); 316 extern bool of_property_read_bool(const struct device_node *np, const char *propname); 317 extern int of_property_count_elems_of_size(const struct device_node *np, 318 const char *propname, int elem_size); 319 extern int of_property_read_u8_index(const struct device_node *np, 320 const char *propname, 321 u32 index, u8 *out_value); 322 extern int of_property_read_u16_index(const struct device_node *np, 323 const char *propname, 324 u32 index, u16 *out_value); 325 extern int of_property_read_u32_index(const struct device_node *np, 326 const char *propname, 327 u32 index, u32 *out_value); 328 extern int of_property_read_u64_index(const struct device_node *np, 329 const char *propname, 330 u32 index, u64 *out_value); 331 extern int of_property_read_variable_u8_array(const struct device_node *np, 332 const char *propname, u8 *out_values, 333 size_t sz_min, size_t sz_max); 334 extern int of_property_read_variable_u16_array(const struct device_node *np, 335 const char *propname, u16 *out_values, 336 size_t sz_min, size_t sz_max); 337 extern int of_property_read_variable_u32_array(const struct device_node *np, 338 const char *propname, 339 u32 *out_values, 340 size_t sz_min, 341 size_t sz_max); 342 extern int of_property_read_u64(const struct device_node *np, 343 const char *propname, u64 *out_value); 344 extern int of_property_read_variable_u64_array(const struct device_node *np, 345 const char *propname, 346 u64 *out_values, 347 size_t sz_min, 348 size_t sz_max); 349 350 extern int of_property_read_string(const struct device_node *np, 351 const char *propname, 352 const char **out_string); 353 extern int of_property_match_string(const struct device_node *np, 354 const char *propname, 355 const char *string); 356 extern int of_property_read_string_helper(const struct device_node *np, 357 const char *propname, 358 const char **out_strs, size_t sz, int index); 359 extern int of_device_is_compatible(const struct device_node *device, 360 const char *); 361 extern int of_device_compatible_match(const struct device_node *device, 362 const char *const *compat); 363 extern bool of_device_is_available(const struct device_node *device); 364 extern bool of_device_is_big_endian(const struct device_node *device); 365 extern const void *of_get_property(const struct device_node *node, 366 const char *name, 367 int *lenp); 368 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread); 369 extern struct device_node *of_cpu_device_node_get(int cpu); 370 extern int of_cpu_node_to_id(struct device_node *np); 371 extern struct device_node *of_get_next_cpu_node(struct device_node *prev); 372 extern struct device_node *of_get_cpu_state_node(const struct device_node *cpu_node, 373 int index); 374 extern u64 of_get_cpu_hwid(struct device_node *cpun, unsigned int thread); 375 376 extern int of_n_addr_cells(struct device_node *np); 377 extern int of_n_size_cells(struct device_node *np); 378 extern const struct of_device_id *of_match_node( 379 const struct of_device_id *matches, const struct device_node *node); 380 extern const void *of_device_get_match_data(const struct device *dev); 381 extern int of_alias_from_compatible(const struct device_node *node, char *alias, 382 int len); 383 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args); 384 extern int __of_parse_phandle_with_args(const struct device_node *np, 385 const char *list_name, const char *cells_name, int cell_count, 386 int index, struct of_phandle_args *out_args); 387 extern int of_parse_phandle_with_args_map(const struct device_node *np, 388 const char *list_name, const char *stem_name, int index, 389 struct of_phandle_args *out_args); 390 extern int of_count_phandle_with_args(const struct device_node *np, 391 const char *list_name, const char *cells_name); 392 393 /* module functions */ 394 extern ssize_t of_modalias(const struct device_node *np, char *str, ssize_t len); 395 extern int of_request_module(const struct device_node *np); 396 397 /* phandle iterator functions */ 398 extern int of_phandle_iterator_init(struct of_phandle_iterator *it, 399 const struct device_node *np, 400 const char *list_name, 401 const char *cells_name, 402 int cell_count); 403 404 extern int of_phandle_iterator_next(struct of_phandle_iterator *it); 405 extern int of_phandle_iterator_args(struct of_phandle_iterator *it, 406 uint32_t *args, 407 int size); 408 409 extern int of_alias_get_id(const struct device_node *np, const char *stem); 410 extern int of_alias_get_highest_id(const char *stem); 411 412 bool of_machine_compatible_match(const char *const *compats); 413 const struct of_device_id *of_machine_get_match(const struct of_device_id *matches); 414 const void *of_machine_get_match_data(const struct of_device_id *matches); 415 416 /** 417 * of_machine_is_compatible - Test root of device tree for a given compatible value 418 * @compat: compatible string to look for in root node's compatible property. 419 * 420 * Return: true if the root node has the given value in its compatible property. 421 */ 422 static inline bool of_machine_is_compatible(const char *compat) 423 { 424 const char *compats[] = { compat, NULL }; 425 426 return of_machine_compatible_match(compats); 427 } 428 429 int of_machine_read_compatible(const char **compatible, unsigned int index); 430 int of_machine_read_model(const char **model); 431 432 extern int of_add_property(struct device_node *np, struct property *prop); 433 extern int of_remove_property(struct device_node *np, struct property *prop); 434 extern int of_update_property(struct device_node *np, struct property *newprop); 435 436 /* For updating the device tree at runtime */ 437 #define OF_RECONFIG_ATTACH_NODE 0x0001 438 #define OF_RECONFIG_DETACH_NODE 0x0002 439 #define OF_RECONFIG_ADD_PROPERTY 0x0003 440 #define OF_RECONFIG_REMOVE_PROPERTY 0x0004 441 #define OF_RECONFIG_UPDATE_PROPERTY 0x0005 442 443 extern int of_attach_node(struct device_node *); 444 extern int of_detach_node(struct device_node *); 445 446 #define of_match_ptr(_ptr) (_ptr) 447 448 /* 449 * u32 u; 450 * 451 * of_property_for_each_u32(np, "propname", u) 452 * printk("U32 value: %x\n", u); 453 */ 454 const __be32 *of_prop_next_u32(const struct property *prop, const __be32 *cur, 455 u32 *pu); 456 /* 457 * struct property *prop; 458 * const char *s; 459 * 460 * of_property_for_each_string(np, "propname", prop, s) 461 * printk("String value: %s\n", s); 462 */ 463 const char *of_prop_next_string(const struct property *prop, const char *cur); 464 465 bool of_console_check(const struct device_node *dn, char *name, int index); 466 467 int of_map_id(const struct device_node *np, u32 id, 468 const char *map_name, const char *cells_name, 469 const char *map_mask_name, 470 struct device_node * const *filter_np, 471 struct of_phandle_args *arg); 472 473 int of_map_iommu_id(const struct device_node *np, u32 id, 474 struct of_phandle_args *arg); 475 476 int of_map_msi_id(const struct device_node *np, u32 id, 477 struct device_node * const *filter_np, 478 struct of_phandle_args *arg); 479 480 phys_addr_t of_dma_get_max_cpu_address(struct device_node *np); 481 482 struct kimage; 483 void *of_kexec_alloc_and_setup_fdt(const struct kimage *image, 484 unsigned long initrd_load_addr, 485 unsigned long initrd_len, 486 const char *cmdline, size_t extra_fdt_size); 487 #else /* CONFIG_OF */ 488 489 static inline void of_core_init(void) 490 { 491 } 492 493 static inline bool is_of_node(const struct fwnode_handle *fwnode) 494 { 495 return false; 496 } 497 498 static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode) 499 { 500 return NULL; 501 } 502 503 static inline bool of_node_name_eq(const struct device_node *np, const char *name) 504 { 505 return false; 506 } 507 508 static inline bool of_node_name_prefix(const struct device_node *np, const char *prefix) 509 { 510 return false; 511 } 512 513 static inline const char* of_node_full_name(const struct device_node *np) 514 { 515 return "<no-node>"; 516 } 517 518 static inline struct device_node *of_find_node_by_name(struct device_node *from, 519 const char *name) 520 { 521 return NULL; 522 } 523 524 static inline struct device_node *of_find_node_by_type(struct device_node *from, 525 const char *type) 526 { 527 return NULL; 528 } 529 530 static inline struct device_node *of_find_matching_node_and_match( 531 struct device_node *from, 532 const struct of_device_id *matches, 533 const struct of_device_id **match) 534 { 535 return NULL; 536 } 537 538 static inline struct device_node *of_find_node_by_path(const char *path) 539 { 540 return NULL; 541 } 542 543 static inline struct device_node *of_find_node_opts_by_path(const char *path, 544 const char **opts) 545 { 546 return NULL; 547 } 548 549 static inline struct device_node *of_find_node_by_phandle(phandle handle) 550 { 551 return NULL; 552 } 553 554 static inline struct device_node *of_get_parent(const struct device_node *node) 555 { 556 return NULL; 557 } 558 559 static inline struct device_node *of_get_next_parent(struct device_node *node) 560 { 561 return NULL; 562 } 563 564 static inline struct device_node *of_get_next_child( 565 const struct device_node *node, struct device_node *prev) 566 { 567 return NULL; 568 } 569 570 static inline struct device_node *of_get_next_child_with_prefix( 571 const struct device_node *node, struct device_node *prev, 572 const char *prefix) 573 { 574 return NULL; 575 } 576 577 static inline struct device_node *of_get_next_available_child( 578 const struct device_node *node, struct device_node *prev) 579 { 580 return NULL; 581 } 582 583 static inline struct device_node *of_get_next_reserved_child( 584 const struct device_node *node, struct device_node *prev) 585 { 586 return NULL; 587 } 588 589 static inline struct device_node *of_find_node_with_property( 590 struct device_node *from, const char *prop_name) 591 { 592 return NULL; 593 } 594 595 #define of_fwnode_handle(node) NULL 596 597 static inline struct device_node *of_get_compatible_child(const struct device_node *parent, 598 const char *compatible) 599 { 600 return NULL; 601 } 602 603 static inline struct device_node *of_get_child_by_name( 604 const struct device_node *node, 605 const char *name) 606 { 607 return NULL; 608 } 609 610 static inline struct device_node *of_get_available_child_by_name( 611 const struct device_node *node, 612 const char *name) 613 { 614 return NULL; 615 } 616 617 static inline int of_device_is_compatible(const struct device_node *device, 618 const char *name) 619 { 620 return 0; 621 } 622 623 static inline int of_device_compatible_match(const struct device_node *device, 624 const char *const *compat) 625 { 626 return 0; 627 } 628 629 static inline bool of_device_is_available(const struct device_node *device) 630 { 631 return false; 632 } 633 634 static inline bool of_device_is_big_endian(const struct device_node *device) 635 { 636 return false; 637 } 638 639 static inline struct property *of_find_property(const struct device_node *np, 640 const char *name, 641 int *lenp) 642 { 643 return NULL; 644 } 645 646 static inline struct device_node *of_find_compatible_node( 647 struct device_node *from, 648 const char *type, 649 const char *compat) 650 { 651 return NULL; 652 } 653 654 static inline bool of_property_read_bool(const struct device_node *np, 655 const char *propname) 656 { 657 return false; 658 } 659 660 static inline int of_property_count_elems_of_size(const struct device_node *np, 661 const char *propname, int elem_size) 662 { 663 return -ENOSYS; 664 } 665 666 static inline int of_property_read_u8_index(const struct device_node *np, 667 const char *propname, u32 index, u8 *out_value) 668 { 669 return -ENOSYS; 670 } 671 672 static inline int of_property_read_u16_index(const struct device_node *np, 673 const char *propname, u32 index, u16 *out_value) 674 { 675 return -ENOSYS; 676 } 677 678 static inline int of_property_read_u32_index(const struct device_node *np, 679 const char *propname, u32 index, u32 *out_value) 680 { 681 return -ENOSYS; 682 } 683 684 static inline int of_property_read_u64_index(const struct device_node *np, 685 const char *propname, u32 index, u64 *out_value) 686 { 687 return -ENOSYS; 688 } 689 690 static inline const void *of_get_property(const struct device_node *node, 691 const char *name, 692 int *lenp) 693 { 694 return NULL; 695 } 696 697 static inline struct device_node *of_get_cpu_node(int cpu, 698 unsigned int *thread) 699 { 700 return NULL; 701 } 702 703 static inline struct device_node *of_cpu_device_node_get(int cpu) 704 { 705 return NULL; 706 } 707 708 static inline int of_cpu_node_to_id(struct device_node *np) 709 { 710 return -ENODEV; 711 } 712 713 static inline struct device_node *of_get_next_cpu_node(struct device_node *prev) 714 { 715 return NULL; 716 } 717 718 static inline struct device_node *of_get_cpu_state_node(struct device_node *cpu_node, 719 int index) 720 { 721 return NULL; 722 } 723 724 static inline int of_n_addr_cells(struct device_node *np) 725 { 726 return 0; 727 728 } 729 static inline int of_n_size_cells(struct device_node *np) 730 { 731 return 0; 732 } 733 734 static inline int of_property_read_variable_u8_array(const struct device_node *np, 735 const char *propname, u8 *out_values, 736 size_t sz_min, size_t sz_max) 737 { 738 return -ENOSYS; 739 } 740 741 static inline int of_property_read_variable_u16_array(const struct device_node *np, 742 const char *propname, u16 *out_values, 743 size_t sz_min, size_t sz_max) 744 { 745 return -ENOSYS; 746 } 747 748 static inline int of_property_read_variable_u32_array(const struct device_node *np, 749 const char *propname, 750 u32 *out_values, 751 size_t sz_min, 752 size_t sz_max) 753 { 754 return -ENOSYS; 755 } 756 757 static inline int of_property_read_u64(const struct device_node *np, 758 const char *propname, u64 *out_value) 759 { 760 return -ENOSYS; 761 } 762 763 static inline int of_property_read_variable_u64_array(const struct device_node *np, 764 const char *propname, 765 u64 *out_values, 766 size_t sz_min, 767 size_t sz_max) 768 { 769 return -ENOSYS; 770 } 771 772 static inline int of_property_read_string(const struct device_node *np, 773 const char *propname, 774 const char **out_string) 775 { 776 return -ENOSYS; 777 } 778 779 static inline int of_property_match_string(const struct device_node *np, 780 const char *propname, 781 const char *string) 782 { 783 return -ENOSYS; 784 } 785 786 static inline int of_property_read_string_helper(const struct device_node *np, 787 const char *propname, 788 const char **out_strs, size_t sz, int index) 789 { 790 return -ENOSYS; 791 } 792 793 static inline int __of_parse_phandle_with_args(const struct device_node *np, 794 const char *list_name, 795 const char *cells_name, 796 int cell_count, 797 int index, 798 struct of_phandle_args *out_args) 799 { 800 return -ENOSYS; 801 } 802 803 static inline int of_parse_phandle_with_args_map(const struct device_node *np, 804 const char *list_name, 805 const char *stem_name, 806 int index, 807 struct of_phandle_args *out_args) 808 { 809 return -ENOSYS; 810 } 811 812 static inline int of_count_phandle_with_args(const struct device_node *np, 813 const char *list_name, 814 const char *cells_name) 815 { 816 return -ENOSYS; 817 } 818 819 static inline ssize_t of_modalias(const struct device_node *np, char *str, 820 ssize_t len) 821 { 822 return -ENODEV; 823 } 824 825 static inline int of_request_module(const struct device_node *np) 826 { 827 return -ENODEV; 828 } 829 830 static inline int of_phandle_iterator_init(struct of_phandle_iterator *it, 831 const struct device_node *np, 832 const char *list_name, 833 const char *cells_name, 834 int cell_count) 835 { 836 return -ENOSYS; 837 } 838 839 static inline int of_phandle_iterator_next(struct of_phandle_iterator *it) 840 { 841 return -ENOSYS; 842 } 843 844 static inline int of_phandle_iterator_args(struct of_phandle_iterator *it, 845 uint32_t *args, 846 int size) 847 { 848 return 0; 849 } 850 851 static inline int of_alias_get_id(struct device_node *np, const char *stem) 852 { 853 return -ENOSYS; 854 } 855 856 static inline int of_alias_get_highest_id(const char *stem) 857 { 858 return -ENOSYS; 859 } 860 861 static inline int of_machine_is_compatible(const char *compat) 862 { 863 return 0; 864 } 865 866 static inline int of_machine_read_compatible(const char **compatible, 867 unsigned int index) 868 { 869 return -ENOSYS; 870 } 871 872 static inline int of_machine_read_model(const char **model) 873 { 874 return -ENOSYS; 875 } 876 877 static inline int of_add_property(struct device_node *np, struct property *prop) 878 { 879 return 0; 880 } 881 882 static inline int of_remove_property(struct device_node *np, struct property *prop) 883 { 884 return 0; 885 } 886 887 static inline bool of_machine_compatible_match(const char *const *compats) 888 { 889 return false; 890 } 891 892 static inline const struct of_device_id *of_machine_get_match(const struct of_device_id *matches) 893 { 894 return NULL; 895 } 896 897 static inline const void * 898 of_machine_get_match_data(const struct of_device_id *matches) 899 { 900 return NULL; 901 } 902 903 static inline bool of_console_check(const struct device_node *dn, const char *name, int index) 904 { 905 return false; 906 } 907 908 static inline const __be32 *of_prop_next_u32(const struct property *prop, 909 const __be32 *cur, u32 *pu) 910 { 911 return NULL; 912 } 913 914 static inline const char *of_prop_next_string(const struct property *prop, 915 const char *cur) 916 { 917 return NULL; 918 } 919 920 static inline int of_node_check_flag(struct device_node *n, unsigned long flag) 921 { 922 return 0; 923 } 924 925 static inline int of_node_test_and_set_flag(struct device_node *n, 926 unsigned long flag) 927 { 928 return 0; 929 } 930 931 static inline void of_node_set_flag(struct device_node *n, unsigned long flag) 932 { 933 } 934 935 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag) 936 { 937 } 938 939 static inline int of_property_check_flag(const struct property *p, 940 unsigned long flag) 941 { 942 return 0; 943 } 944 945 static inline void of_property_set_flag(struct property *p, unsigned long flag) 946 { 947 } 948 949 static inline void of_property_clear_flag(struct property *p, unsigned long flag) 950 { 951 } 952 953 static inline int of_map_id(const struct device_node *np, u32 id, 954 const char *map_name, const char *cells_name, 955 const char *map_mask_name, 956 struct device_node * const *filter_np, 957 struct of_phandle_args *arg) 958 { 959 return -EINVAL; 960 } 961 962 static inline int of_map_iommu_id(const struct device_node *np, u32 id, 963 struct of_phandle_args *arg) 964 { 965 return -EINVAL; 966 } 967 968 static inline int of_map_msi_id(const struct device_node *np, u32 id, 969 struct device_node * const *filter_np, 970 struct of_phandle_args *arg) 971 { 972 return -EINVAL; 973 } 974 975 static inline phys_addr_t of_dma_get_max_cpu_address(struct device_node *np) 976 { 977 return PHYS_ADDR_MAX; 978 } 979 980 static inline const void *of_device_get_match_data(const struct device *dev) 981 { 982 return NULL; 983 } 984 985 #define of_match_ptr(_ptr) NULL 986 #define of_match_node(_matches, _node) NULL 987 #endif /* CONFIG_OF */ 988 989 /* Default string compare functions, Allow arch asm/prom.h to override */ 990 #if !defined(of_compat_cmp) 991 #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2)) 992 #define of_prop_cmp(s1, s2) strcmp((s1), (s2)) 993 #define of_node_cmp(s1, s2) strcasecmp((s1), (s2)) 994 #endif 995 996 #define for_each_property_of_node(dn, pp) \ 997 for (pp = dn->properties; pp != NULL; pp = pp->next) 998 999 #if defined(CONFIG_OF) && defined(CONFIG_NUMA) 1000 extern int of_node_to_nid(struct device_node *np); 1001 #else 1002 static inline int of_node_to_nid(struct device_node *device) 1003 { 1004 return NUMA_NO_NODE; 1005 } 1006 #endif 1007 1008 #ifdef CONFIG_OF_NUMA 1009 extern int of_numa_init(void); 1010 #else 1011 static inline int of_numa_init(void) 1012 { 1013 return -ENOSYS; 1014 } 1015 #endif 1016 1017 static inline bool of_machine_device_match(const struct of_device_id *matches) 1018 { 1019 return of_machine_get_match(matches) != NULL; 1020 } 1021 1022 static inline struct device_node *of_find_matching_node( 1023 struct device_node *from, 1024 const struct of_device_id *matches) 1025 { 1026 return of_find_matching_node_and_match(from, matches, NULL); 1027 } 1028 1029 static inline const char *of_node_get_device_type(const struct device_node *np) 1030 { 1031 return of_get_property(np, "device_type", NULL); 1032 } 1033 1034 static inline bool of_node_is_type(const struct device_node *np, const char *type) 1035 { 1036 const char *match = of_node_get_device_type(np); 1037 1038 return np && match && type && !strcmp(match, type); 1039 } 1040 1041 /** 1042 * of_parse_phandle - Resolve a phandle property to a device_node pointer 1043 * @np: Pointer to device node holding phandle property 1044 * @phandle_name: Name of property holding a phandle value 1045 * @index: For properties holding a table of phandles, this is the index into 1046 * the table 1047 * 1048 * Return: The device_node pointer with refcount incremented. Use 1049 * of_node_put() on it when done. 1050 */ 1051 static inline struct device_node *of_parse_phandle(const struct device_node *np, 1052 const char *phandle_name, 1053 int index) 1054 { 1055 struct of_phandle_args args; 1056 1057 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, 1058 index, &args)) 1059 return NULL; 1060 1061 return args.np; 1062 } 1063 1064 /** 1065 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list 1066 * @np: pointer to a device tree node containing a list 1067 * @list_name: property name that contains a list 1068 * @cells_name: property name that specifies phandles' arguments count 1069 * @index: index of a phandle to parse out 1070 * @out_args: optional pointer to output arguments structure (will be filled) 1071 * 1072 * This function is useful to parse lists of phandles and their arguments. 1073 * Returns 0 on success and fills out_args, on error returns appropriate 1074 * errno value. 1075 * 1076 * Caller is responsible to call of_node_put() on the returned out_args->np 1077 * pointer. 1078 * 1079 * Example:: 1080 * 1081 * phandle1: node1 { 1082 * #list-cells = <2>; 1083 * }; 1084 * 1085 * phandle2: node2 { 1086 * #list-cells = <1>; 1087 * }; 1088 * 1089 * node3 { 1090 * list = <&phandle1 1 2 &phandle2 3>; 1091 * }; 1092 * 1093 * To get a device_node of the ``node2`` node you may call this: 1094 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args); 1095 */ 1096 static inline int of_parse_phandle_with_args(const struct device_node *np, 1097 const char *list_name, 1098 const char *cells_name, 1099 int index, 1100 struct of_phandle_args *out_args) 1101 { 1102 int cell_count = -1; 1103 1104 /* If cells_name is NULL we assume a cell count of 0 */ 1105 if (!cells_name) 1106 cell_count = 0; 1107 1108 return __of_parse_phandle_with_args(np, list_name, cells_name, 1109 cell_count, index, out_args); 1110 } 1111 1112 /** 1113 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list 1114 * @np: pointer to a device tree node containing a list 1115 * @list_name: property name that contains a list 1116 * @cell_count: number of argument cells following the phandle 1117 * @index: index of a phandle to parse out 1118 * @out_args: optional pointer to output arguments structure (will be filled) 1119 * 1120 * This function is useful to parse lists of phandles and their arguments. 1121 * Returns 0 on success and fills out_args, on error returns appropriate 1122 * errno value. 1123 * 1124 * Caller is responsible to call of_node_put() on the returned out_args->np 1125 * pointer. 1126 * 1127 * Example:: 1128 * 1129 * phandle1: node1 { 1130 * }; 1131 * 1132 * phandle2: node2 { 1133 * }; 1134 * 1135 * node3 { 1136 * list = <&phandle1 0 2 &phandle2 2 3>; 1137 * }; 1138 * 1139 * To get a device_node of the ``node2`` node you may call this: 1140 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args); 1141 */ 1142 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np, 1143 const char *list_name, 1144 int cell_count, 1145 int index, 1146 struct of_phandle_args *out_args) 1147 { 1148 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count, 1149 index, out_args); 1150 } 1151 1152 /** 1153 * of_parse_phandle_with_optional_args() - Find a node pointed by phandle in a list 1154 * @np: pointer to a device tree node containing a list 1155 * @list_name: property name that contains a list 1156 * @cells_name: property name that specifies phandles' arguments count 1157 * @index: index of a phandle to parse out 1158 * @out_args: optional pointer to output arguments structure (will be filled) 1159 * 1160 * Same as of_parse_phandle_with_args() except that if the cells_name property 1161 * is not found, cell_count of 0 is assumed. 1162 * 1163 * This is used to useful, if you have a phandle which didn't have arguments 1164 * before and thus doesn't have a '#*-cells' property but is now migrated to 1165 * having arguments while retaining backwards compatibility. 1166 */ 1167 static inline int of_parse_phandle_with_optional_args(const struct device_node *np, 1168 const char *list_name, 1169 const char *cells_name, 1170 int index, 1171 struct of_phandle_args *out_args) 1172 { 1173 return __of_parse_phandle_with_args(np, list_name, cells_name, 1174 0, index, out_args); 1175 } 1176 1177 /** 1178 * of_phandle_args_equal() - Compare two of_phandle_args 1179 * @a1: First of_phandle_args to compare 1180 * @a2: Second of_phandle_args to compare 1181 * 1182 * Return: True if a1 and a2 are the same (same node pointer, same phandle 1183 * args), false otherwise. 1184 */ 1185 static inline bool of_phandle_args_equal(const struct of_phandle_args *a1, 1186 const struct of_phandle_args *a2) 1187 { 1188 return a1->np == a2->np && 1189 a1->args_count == a2->args_count && 1190 !memcmp(a1->args, a2->args, sizeof(a1->args[0]) * a1->args_count); 1191 } 1192 1193 /** 1194 * of_property_count_u8_elems - Count the number of u8 elements in a property 1195 * 1196 * @np: device node from which the property value is to be read. 1197 * @propname: name of the property to be searched. 1198 * 1199 * Search for a property in a device node and count the number of u8 elements 1200 * in it. 1201 * 1202 * Return: The number of elements on success, -EINVAL if the property does 1203 * not exist or its length does not match a multiple of u8 and -ENODATA if the 1204 * property does not have a value. 1205 */ 1206 static inline int of_property_count_u8_elems(const struct device_node *np, 1207 const char *propname) 1208 { 1209 return of_property_count_elems_of_size(np, propname, sizeof(u8)); 1210 } 1211 1212 /** 1213 * of_property_count_u16_elems - Count the number of u16 elements in a property 1214 * 1215 * @np: device node from which the property value is to be read. 1216 * @propname: name of the property to be searched. 1217 * 1218 * Search for a property in a device node and count the number of u16 elements 1219 * in it. 1220 * 1221 * Return: The number of elements on success, -EINVAL if the property does 1222 * not exist or its length does not match a multiple of u16 and -ENODATA if the 1223 * property does not have a value. 1224 */ 1225 static inline int of_property_count_u16_elems(const struct device_node *np, 1226 const char *propname) 1227 { 1228 return of_property_count_elems_of_size(np, propname, sizeof(u16)); 1229 } 1230 1231 /** 1232 * of_property_count_u32_elems - Count the number of u32 elements in a property 1233 * 1234 * @np: device node from which the property value is to be read. 1235 * @propname: name of the property to be searched. 1236 * 1237 * Search for a property in a device node and count the number of u32 elements 1238 * in it. 1239 * 1240 * Return: The number of elements on success, -EINVAL if the property does 1241 * not exist or its length does not match a multiple of u32 and -ENODATA if the 1242 * property does not have a value. 1243 */ 1244 static inline int of_property_count_u32_elems(const struct device_node *np, 1245 const char *propname) 1246 { 1247 return of_property_count_elems_of_size(np, propname, sizeof(u32)); 1248 } 1249 1250 /** 1251 * of_property_count_u64_elems - Count the number of u64 elements in a property 1252 * 1253 * @np: device node from which the property value is to be read. 1254 * @propname: name of the property to be searched. 1255 * 1256 * Search for a property in a device node and count the number of u64 elements 1257 * in it. 1258 * 1259 * Return: The number of elements on success, -EINVAL if the property does 1260 * not exist or its length does not match a multiple of u64 and -ENODATA if the 1261 * property does not have a value. 1262 */ 1263 static inline int of_property_count_u64_elems(const struct device_node *np, 1264 const char *propname) 1265 { 1266 return of_property_count_elems_of_size(np, propname, sizeof(u64)); 1267 } 1268 1269 /** 1270 * of_property_read_string_array() - Read an array of strings from a multiple 1271 * strings property. 1272 * @np: device node from which the property value is to be read. 1273 * @propname: name of the property to be searched. 1274 * @out_strs: output array of string pointers. 1275 * @sz: number of array elements to read. 1276 * 1277 * Search for a property in a device tree node and retrieve a list of 1278 * terminated string values (pointer to data, not a copy) in that property. 1279 * 1280 * Return: If @out_strs is NULL, the number of strings in the property is returned. 1281 */ 1282 static inline int of_property_read_string_array(const struct device_node *np, 1283 const char *propname, const char **out_strs, 1284 size_t sz) 1285 { 1286 return of_property_read_string_helper(np, propname, out_strs, sz, 0); 1287 } 1288 1289 /** 1290 * of_property_count_strings() - Find and return the number of strings from a 1291 * multiple strings property. 1292 * @np: device node from which the property value is to be read. 1293 * @propname: name of the property to be searched. 1294 * 1295 * Search for a property in a device tree node and retrieve the number of null 1296 * terminated string contain in it. 1297 * 1298 * Return: The number of strings on success, -EINVAL if the property does not 1299 * exist, -ENODATA if property does not have a value, and -EILSEQ if the string 1300 * is not null-terminated within the length of the property data. 1301 */ 1302 static inline int of_property_count_strings(const struct device_node *np, 1303 const char *propname) 1304 { 1305 return of_property_read_string_helper(np, propname, NULL, 0, 0); 1306 } 1307 1308 /** 1309 * of_property_read_string_index() - Find and read a string from a multiple 1310 * strings property. 1311 * @np: device node from which the property value is to be read. 1312 * @propname: name of the property to be searched. 1313 * @index: index of the string in the list of strings 1314 * @output: pointer to null terminated return string, modified only if 1315 * return value is 0. 1316 * 1317 * Search for a property in a device tree node and retrieve a null 1318 * terminated string value (pointer to data, not a copy) in the list of strings 1319 * contained in that property. 1320 * 1321 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if 1322 * property does not have a value, and -EILSEQ if the string is not 1323 * null-terminated within the length of the property data. 1324 * 1325 * The out_string pointer is modified only if a valid string can be decoded. 1326 */ 1327 static inline int of_property_read_string_index(const struct device_node *np, 1328 const char *propname, 1329 int index, const char **output) 1330 { 1331 int rc = of_property_read_string_helper(np, propname, output, 1, index); 1332 return rc < 0 ? rc : 0; 1333 } 1334 1335 /** 1336 * of_property_present - Test if a property is present in a node 1337 * @np: device node to search for the property. 1338 * @propname: name of the property to be searched. 1339 * 1340 * Test for a property present in a device node. 1341 * 1342 * Return: true if the property exists false otherwise. 1343 */ 1344 static inline bool of_property_present(const struct device_node *np, const char *propname) 1345 { 1346 struct property *prop = of_find_property(np, propname, NULL); 1347 1348 return prop ? true : false; 1349 } 1350 1351 /** 1352 * of_property_read_u8_array - Find and read an array of u8 from a property. 1353 * 1354 * @np: device node from which the property value is to be read. 1355 * @propname: name of the property to be searched. 1356 * @out_values: pointer to return value, modified only if return value is 0. 1357 * @sz: number of array elements to read 1358 * 1359 * Search for a property in a device node and read 8-bit value(s) from 1360 * it. 1361 * 1362 * dts entry of array should be like: 1363 * ``property = /bits/ 8 <0x50 0x60 0x70>;`` 1364 * 1365 * Return: 0 on success, -EINVAL if the property does not exist, 1366 * -ENODATA if property does not have a value, and -EOVERFLOW if the 1367 * property data isn't large enough. 1368 * 1369 * The out_values is modified only if a valid u8 value can be decoded. 1370 */ 1371 static inline int of_property_read_u8_array(const struct device_node *np, 1372 const char *propname, 1373 u8 *out_values, size_t sz) 1374 { 1375 int ret = of_property_read_variable_u8_array(np, propname, out_values, 1376 sz, 0); 1377 if (ret >= 0) 1378 return 0; 1379 else 1380 return ret; 1381 } 1382 1383 /** 1384 * of_property_read_u16_array - Find and read an array of u16 from a property. 1385 * 1386 * @np: device node from which the property value is to be read. 1387 * @propname: name of the property to be searched. 1388 * @out_values: pointer to return value, modified only if return value is 0. 1389 * @sz: number of array elements to read 1390 * 1391 * Search for a property in a device node and read 16-bit value(s) from 1392 * it. 1393 * 1394 * dts entry of array should be like: 1395 * ``property = /bits/ 16 <0x5000 0x6000 0x7000>;`` 1396 * 1397 * Return: 0 on success, -EINVAL if the property does not exist, 1398 * -ENODATA if property does not have a value, and -EOVERFLOW if the 1399 * property data isn't large enough. 1400 * 1401 * The out_values is modified only if a valid u16 value can be decoded. 1402 */ 1403 static inline int of_property_read_u16_array(const struct device_node *np, 1404 const char *propname, 1405 u16 *out_values, size_t sz) 1406 { 1407 int ret = of_property_read_variable_u16_array(np, propname, out_values, 1408 sz, 0); 1409 if (ret >= 0) 1410 return 0; 1411 else 1412 return ret; 1413 } 1414 1415 /** 1416 * of_property_read_u32_array - Find and read an array of 32 bit integers 1417 * from a property. 1418 * 1419 * @np: device node from which the property value is to be read. 1420 * @propname: name of the property to be searched. 1421 * @out_values: pointer to return value, modified only if return value is 0. 1422 * @sz: number of array elements to read 1423 * 1424 * Search for a property in a device node and read 32-bit value(s) from 1425 * it. 1426 * 1427 * Return: 0 on success, -EINVAL if the property does not exist, 1428 * -ENODATA if property does not have a value, and -EOVERFLOW if the 1429 * property data isn't large enough. 1430 * 1431 * The out_values is modified only if a valid u32 value can be decoded. 1432 */ 1433 static inline int of_property_read_u32_array(const struct device_node *np, 1434 const char *propname, 1435 u32 *out_values, size_t sz) 1436 { 1437 int ret = of_property_read_variable_u32_array(np, propname, out_values, 1438 sz, 0); 1439 if (ret >= 0) 1440 return 0; 1441 else 1442 return ret; 1443 } 1444 1445 /** 1446 * of_property_read_u64_array - Find and read an array of 64 bit integers 1447 * from a property. 1448 * 1449 * @np: device node from which the property value is to be read. 1450 * @propname: name of the property to be searched. 1451 * @out_values: pointer to return value, modified only if return value is 0. 1452 * @sz: number of array elements to read 1453 * 1454 * Search for a property in a device node and read 64-bit value(s) from 1455 * it. 1456 * 1457 * Return: 0 on success, -EINVAL if the property does not exist, 1458 * -ENODATA if property does not have a value, and -EOVERFLOW if the 1459 * property data isn't large enough. 1460 * 1461 * The out_values is modified only if a valid u64 value can be decoded. 1462 */ 1463 static inline int of_property_read_u64_array(const struct device_node *np, 1464 const char *propname, 1465 u64 *out_values, size_t sz) 1466 { 1467 int ret = of_property_read_variable_u64_array(np, propname, out_values, 1468 sz, 0); 1469 if (ret >= 0) 1470 return 0; 1471 else 1472 return ret; 1473 } 1474 1475 static inline int of_property_read_u8(const struct device_node *np, 1476 const char *propname, 1477 u8 *out_value) 1478 { 1479 return of_property_read_u8_array(np, propname, out_value, 1); 1480 } 1481 1482 static inline int of_property_read_u16(const struct device_node *np, 1483 const char *propname, 1484 u16 *out_value) 1485 { 1486 return of_property_read_u16_array(np, propname, out_value, 1); 1487 } 1488 1489 static inline int of_property_read_u32(const struct device_node *np, 1490 const char *propname, 1491 u32 *out_value) 1492 { 1493 return of_property_read_u32_array(np, propname, out_value, 1); 1494 } 1495 1496 static inline int of_property_read_s32(const struct device_node *np, 1497 const char *propname, 1498 s32 *out_value) 1499 { 1500 return of_property_read_u32(np, propname, (u32*) out_value); 1501 } 1502 1503 static inline int of_property_read_s32_index(const struct device_node *np, 1504 const char *propname, u32 index, 1505 s32 *out_value) 1506 { 1507 return of_property_read_u32_index(np, propname, index, (u32 *)out_value); 1508 } 1509 1510 #define of_for_each_phandle(it, err, np, ln, cn, cc) \ 1511 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \ 1512 err = of_phandle_iterator_next(it); \ 1513 err == 0; \ 1514 err = of_phandle_iterator_next(it)) 1515 1516 #define of_property_for_each_u32(np, propname, u) \ 1517 for (struct {const struct property *prop; const __be32 *item; } _it = \ 1518 {of_find_property(np, propname, NULL), \ 1519 of_prop_next_u32(_it.prop, NULL, &u)}; \ 1520 _it.item; \ 1521 _it.item = of_prop_next_u32(_it.prop, _it.item, &u)) 1522 1523 #define of_property_for_each_string(np, propname, prop, s) \ 1524 for (prop = of_find_property(np, propname, NULL), \ 1525 s = of_prop_next_string(prop, NULL); \ 1526 s; \ 1527 s = of_prop_next_string(prop, s)) 1528 1529 #define for_each_node_by_name(dn, name) \ 1530 for (dn = of_find_node_by_name(NULL, name); dn; \ 1531 dn = of_find_node_by_name(dn, name)) 1532 #define for_each_node_by_type(dn, type) \ 1533 for (dn = of_find_node_by_type(NULL, type); dn; \ 1534 dn = of_find_node_by_type(dn, type)) 1535 #define for_each_compatible_node(dn, type, compatible) \ 1536 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \ 1537 dn = of_find_compatible_node(dn, type, compatible)) 1538 1539 #define for_each_compatible_node_scoped(dn, type, compatible) \ 1540 for (struct device_node *dn __free(device_node) = \ 1541 of_find_compatible_node(NULL, type, compatible); \ 1542 dn; \ 1543 dn = of_find_compatible_node(dn, type, compatible)) 1544 1545 #define for_each_matching_node(dn, matches) \ 1546 for (dn = of_find_matching_node(NULL, matches); dn; \ 1547 dn = of_find_matching_node(dn, matches)) 1548 #define for_each_matching_node_and_match(dn, matches, match) \ 1549 for (dn = of_find_matching_node_and_match(NULL, matches, match); \ 1550 dn; dn = of_find_matching_node_and_match(dn, matches, match)) 1551 1552 #define for_each_child_of_node(parent, child) \ 1553 for (child = of_get_next_child(parent, NULL); child != NULL; \ 1554 child = of_get_next_child(parent, child)) 1555 1556 #define for_each_child_of_node_scoped(parent, child) \ 1557 for (struct device_node *child __free(device_node) = \ 1558 of_get_next_child(parent, NULL); \ 1559 child != NULL; \ 1560 child = of_get_next_child(parent, child)) 1561 1562 #define for_each_child_of_node_with_prefix(parent, child, prefix) \ 1563 for (struct device_node *child __free(device_node) = \ 1564 of_get_next_child_with_prefix(parent, NULL, prefix); \ 1565 child != NULL; \ 1566 child = of_get_next_child_with_prefix(parent, child, prefix)) 1567 1568 #define for_each_available_child_of_node(parent, child) \ 1569 for (child = of_get_next_available_child(parent, NULL); child != NULL; \ 1570 child = of_get_next_available_child(parent, child)) 1571 #define for_each_reserved_child_of_node(parent, child) \ 1572 for (child = of_get_next_reserved_child(parent, NULL); child != NULL; \ 1573 child = of_get_next_reserved_child(parent, child)) 1574 1575 #define for_each_available_child_of_node_scoped(parent, child) \ 1576 for (struct device_node *child __free(device_node) = \ 1577 of_get_next_available_child(parent, NULL); \ 1578 child != NULL; \ 1579 child = of_get_next_available_child(parent, child)) 1580 1581 #define for_each_of_cpu_node(cpu) \ 1582 for (cpu = of_get_next_cpu_node(NULL); cpu != NULL; \ 1583 cpu = of_get_next_cpu_node(cpu)) 1584 1585 #define for_each_node_with_property(dn, prop_name) \ 1586 for (dn = of_find_node_with_property(NULL, prop_name); dn; \ 1587 dn = of_find_node_with_property(dn, prop_name)) 1588 1589 static inline int of_get_child_count(const struct device_node *np) 1590 { 1591 struct device_node *child; 1592 int num = 0; 1593 1594 for_each_child_of_node(np, child) 1595 num++; 1596 1597 return num; 1598 } 1599 1600 static inline int of_get_available_child_count(const struct device_node *np) 1601 { 1602 struct device_node *child; 1603 int num = 0; 1604 1605 for_each_available_child_of_node(np, child) 1606 num++; 1607 1608 return num; 1609 } 1610 1611 #define _OF_DECLARE_STUB(table, name, compat, fn, fn_type) \ 1612 static const struct of_device_id __of_table_##name \ 1613 __attribute__((unused)) \ 1614 = { .compatible = compat, \ 1615 .data = (fn == (fn_type)NULL) ? fn : fn } 1616 1617 #if defined(CONFIG_OF) && !defined(MODULE) 1618 #define _OF_DECLARE(table, name, compat, fn, fn_type) \ 1619 static const struct of_device_id __of_table_##name \ 1620 __used __section("__" #table "_of_table") \ 1621 __aligned(__alignof__(struct of_device_id)) \ 1622 = { .compatible = compat, \ 1623 .data = (fn == (fn_type)NULL) ? fn : fn } 1624 #else 1625 #define _OF_DECLARE(table, name, compat, fn, fn_type) \ 1626 _OF_DECLARE_STUB(table, name, compat, fn, fn_type) 1627 #endif 1628 1629 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *); 1630 typedef int (*of_init_fn_1_ret)(struct device_node *); 1631 typedef void (*of_init_fn_1)(struct device_node *); 1632 1633 #define OF_DECLARE_1(table, name, compat, fn) \ 1634 _OF_DECLARE(table, name, compat, fn, of_init_fn_1) 1635 #define OF_DECLARE_1_RET(table, name, compat, fn) \ 1636 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret) 1637 #define OF_DECLARE_2(table, name, compat, fn) \ 1638 _OF_DECLARE(table, name, compat, fn, of_init_fn_2) 1639 1640 /** 1641 * struct of_changeset_entry - Holds a changeset entry 1642 * 1643 * @node: list_head for the log list 1644 * @action: notifier action 1645 * @np: pointer to the device node affected 1646 * @prop: pointer to the property affected 1647 * @old_prop: hold a pointer to the original property 1648 * 1649 * Every modification of the device tree during a changeset 1650 * is held in a list of of_changeset_entry structures. 1651 * That way we can recover from a partial application, or we can 1652 * revert the changeset 1653 */ 1654 struct of_changeset_entry { 1655 struct list_head node; 1656 unsigned long action; 1657 struct device_node *np; 1658 struct property *prop; 1659 struct property *old_prop; 1660 }; 1661 1662 /** 1663 * struct of_changeset - changeset tracker structure 1664 * 1665 * @entries: list_head for the changeset entries 1666 * 1667 * changesets are a convenient way to apply bulk changes to the 1668 * live tree. In case of an error, changes are rolled-back. 1669 * changesets live on after initial application, and if not 1670 * destroyed after use, they can be reverted in one single call. 1671 */ 1672 struct of_changeset { 1673 struct list_head entries; 1674 }; 1675 1676 enum of_reconfig_change { 1677 OF_RECONFIG_NO_CHANGE = 0, 1678 OF_RECONFIG_CHANGE_ADD, 1679 OF_RECONFIG_CHANGE_REMOVE, 1680 }; 1681 1682 struct notifier_block; 1683 1684 #ifdef CONFIG_OF_DYNAMIC 1685 extern int of_reconfig_notifier_register(struct notifier_block *); 1686 extern int of_reconfig_notifier_unregister(struct notifier_block *); 1687 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd); 1688 extern int of_reconfig_get_state_change(unsigned long action, 1689 struct of_reconfig_data *arg); 1690 1691 extern void of_changeset_init(struct of_changeset *ocs); 1692 extern void of_changeset_destroy(struct of_changeset *ocs); 1693 extern int of_changeset_apply(struct of_changeset *ocs); 1694 extern int of_changeset_revert(struct of_changeset *ocs); 1695 extern int of_changeset_action(struct of_changeset *ocs, 1696 unsigned long action, struct device_node *np, 1697 struct property *prop); 1698 1699 static inline int of_changeset_attach_node(struct of_changeset *ocs, 1700 struct device_node *np) 1701 { 1702 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL); 1703 } 1704 1705 static inline int of_changeset_detach_node(struct of_changeset *ocs, 1706 struct device_node *np) 1707 { 1708 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL); 1709 } 1710 1711 static inline int of_changeset_add_property(struct of_changeset *ocs, 1712 struct device_node *np, struct property *prop) 1713 { 1714 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop); 1715 } 1716 1717 static inline int of_changeset_remove_property(struct of_changeset *ocs, 1718 struct device_node *np, struct property *prop) 1719 { 1720 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop); 1721 } 1722 1723 static inline int of_changeset_update_property(struct of_changeset *ocs, 1724 struct device_node *np, struct property *prop) 1725 { 1726 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop); 1727 } 1728 1729 struct device_node *of_changeset_create_node(struct of_changeset *ocs, 1730 struct device_node *parent, 1731 const char *full_name); 1732 int of_changeset_add_prop_string(struct of_changeset *ocs, 1733 struct device_node *np, 1734 const char *prop_name, const char *str); 1735 int of_changeset_add_prop_string_array(struct of_changeset *ocs, 1736 struct device_node *np, 1737 const char *prop_name, 1738 const char * const *str_array, size_t sz); 1739 int of_changeset_add_prop_u32_array(struct of_changeset *ocs, 1740 struct device_node *np, 1741 const char *prop_name, 1742 const u32 *array, size_t sz); 1743 static inline int of_changeset_add_prop_u32(struct of_changeset *ocs, 1744 struct device_node *np, 1745 const char *prop_name, 1746 const u32 val) 1747 { 1748 return of_changeset_add_prop_u32_array(ocs, np, prop_name, &val, 1); 1749 } 1750 1751 int of_changeset_update_prop_string(struct of_changeset *ocs, 1752 struct device_node *np, 1753 const char *prop_name, const char *str); 1754 1755 int of_changeset_add_prop_bool(struct of_changeset *ocs, struct device_node *np, 1756 const char *prop_name); 1757 1758 #else /* CONFIG_OF_DYNAMIC */ 1759 static inline int of_reconfig_notifier_register(struct notifier_block *nb) 1760 { 1761 return -EINVAL; 1762 } 1763 static inline int of_reconfig_notifier_unregister(struct notifier_block *nb) 1764 { 1765 return -EINVAL; 1766 } 1767 static inline int of_reconfig_notify(unsigned long action, 1768 struct of_reconfig_data *arg) 1769 { 1770 return -EINVAL; 1771 } 1772 static inline int of_reconfig_get_state_change(unsigned long action, 1773 struct of_reconfig_data *arg) 1774 { 1775 return -EINVAL; 1776 } 1777 #endif /* CONFIG_OF_DYNAMIC */ 1778 1779 /** 1780 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node 1781 * @np: Pointer to the given device_node 1782 * 1783 * Return: true if present false otherwise 1784 */ 1785 static inline bool of_device_is_system_power_controller(const struct device_node *np) 1786 { 1787 return of_property_read_bool(np, "system-power-controller"); 1788 } 1789 1790 /** 1791 * of_have_populated_dt() - Has DT been populated by bootloader 1792 * 1793 * Return: True if a DTB has been populated by the bootloader and it isn't the 1794 * empty builtin one. False otherwise. 1795 */ 1796 static inline bool of_have_populated_dt(void) 1797 { 1798 #ifdef CONFIG_OF 1799 return of_property_present(of_root, "compatible"); 1800 #else 1801 return false; 1802 #endif 1803 } 1804 1805 /* 1806 * Overlay support 1807 */ 1808 1809 enum of_overlay_notify_action { 1810 OF_OVERLAY_INIT = 0, /* kzalloc() of ovcs sets this value */ 1811 OF_OVERLAY_PRE_APPLY, 1812 OF_OVERLAY_POST_APPLY, 1813 OF_OVERLAY_PRE_REMOVE, 1814 OF_OVERLAY_POST_REMOVE, 1815 }; 1816 1817 static inline const char *of_overlay_action_name(enum of_overlay_notify_action action) 1818 { 1819 static const char *const of_overlay_action_name[] = { 1820 "init", 1821 "pre-apply", 1822 "post-apply", 1823 "pre-remove", 1824 "post-remove", 1825 }; 1826 1827 return of_overlay_action_name[action]; 1828 } 1829 1830 struct of_overlay_notify_data { 1831 struct device_node *overlay; 1832 struct device_node *target; 1833 }; 1834 1835 #ifdef CONFIG_OF_OVERLAY 1836 1837 int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size, 1838 int *ovcs_id, const struct device_node *target_base); 1839 int of_overlay_remove(int *ovcs_id); 1840 1841 int of_overlay_notifier_register(struct notifier_block *nb); 1842 int of_overlay_notifier_unregister(struct notifier_block *nb); 1843 1844 #else 1845 1846 static inline int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size, 1847 int *ovcs_id, const struct device_node *target_base) 1848 { 1849 return -ENOTSUPP; 1850 } 1851 1852 static inline int of_overlay_remove(int *ovcs_id) 1853 { 1854 return -ENOTSUPP; 1855 } 1856 1857 static inline int of_overlay_notifier_register(struct notifier_block *nb) 1858 { 1859 return 0; 1860 } 1861 1862 static inline int of_overlay_notifier_unregister(struct notifier_block *nb) 1863 { 1864 return 0; 1865 } 1866 1867 #endif 1868 1869 #endif /* _LINUX_OF_H */ 1870