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/mod_devicetable.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 16 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 305 /* cache lookup */ 306 extern struct device_node *of_find_next_cache_node(const struct device_node *); 307 extern int of_find_last_cache_level(unsigned int cpu); 308 extern struct device_node *of_find_node_with_property( 309 struct device_node *from, const char *prop_name); 310 311 extern struct property *of_find_property(const struct device_node *np, 312 const char *name, 313 int *lenp); 314 extern int of_property_count_elems_of_size(const struct device_node *np, 315 const char *propname, int elem_size); 316 extern int of_property_read_u32_index(const struct device_node *np, 317 const char *propname, 318 u32 index, u32 *out_value); 319 extern int of_property_read_u64_index(const struct device_node *np, 320 const char *propname, 321 u32 index, u64 *out_value); 322 extern int of_property_read_variable_u8_array(const struct device_node *np, 323 const char *propname, u8 *out_values, 324 size_t sz_min, size_t sz_max); 325 extern int of_property_read_variable_u16_array(const struct device_node *np, 326 const char *propname, u16 *out_values, 327 size_t sz_min, size_t sz_max); 328 extern int of_property_read_variable_u32_array(const struct device_node *np, 329 const char *propname, 330 u32 *out_values, 331 size_t sz_min, 332 size_t sz_max); 333 extern int of_property_read_u64(const struct device_node *np, 334 const char *propname, u64 *out_value); 335 extern int of_property_read_variable_u64_array(const struct device_node *np, 336 const char *propname, 337 u64 *out_values, 338 size_t sz_min, 339 size_t sz_max); 340 341 extern int of_property_read_string(const struct device_node *np, 342 const char *propname, 343 const char **out_string); 344 extern int of_property_match_string(const struct device_node *np, 345 const char *propname, 346 const char *string); 347 extern int of_property_read_string_helper(const struct device_node *np, 348 const char *propname, 349 const char **out_strs, size_t sz, int index); 350 extern int of_device_is_compatible(const struct device_node *device, 351 const char *); 352 extern int of_device_compatible_match(const struct device_node *device, 353 const char *const *compat); 354 extern bool of_device_is_available(const struct device_node *device); 355 extern bool of_device_is_big_endian(const struct device_node *device); 356 extern const void *of_get_property(const struct device_node *node, 357 const char *name, 358 int *lenp); 359 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread); 360 extern struct device_node *of_cpu_device_node_get(int cpu); 361 extern int of_cpu_node_to_id(struct device_node *np); 362 extern struct device_node *of_get_next_cpu_node(struct device_node *prev); 363 extern struct device_node *of_get_cpu_state_node(const struct device_node *cpu_node, 364 int index); 365 extern u64 of_get_cpu_hwid(struct device_node *cpun, unsigned int thread); 366 367 extern int of_n_addr_cells(struct device_node *np); 368 extern int of_n_size_cells(struct device_node *np); 369 extern const struct of_device_id *of_match_node( 370 const struct of_device_id *matches, const struct device_node *node); 371 extern const void *of_device_get_match_data(const struct device *dev); 372 extern int of_alias_from_compatible(const struct device_node *node, char *alias, 373 int len); 374 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args); 375 extern int __of_parse_phandle_with_args(const struct device_node *np, 376 const char *list_name, const char *cells_name, int cell_count, 377 int index, struct of_phandle_args *out_args); 378 extern int of_parse_phandle_with_args_map(const struct device_node *np, 379 const char *list_name, const char *stem_name, int index, 380 struct of_phandle_args *out_args); 381 extern int of_count_phandle_with_args(const struct device_node *np, 382 const char *list_name, const char *cells_name); 383 384 /* module functions */ 385 extern ssize_t of_modalias(const struct device_node *np, char *str, ssize_t len); 386 extern int of_request_module(const struct device_node *np); 387 388 /* phandle iterator functions */ 389 extern int of_phandle_iterator_init(struct of_phandle_iterator *it, 390 const struct device_node *np, 391 const char *list_name, 392 const char *cells_name, 393 int cell_count); 394 395 extern int of_phandle_iterator_next(struct of_phandle_iterator *it); 396 extern int of_phandle_iterator_args(struct of_phandle_iterator *it, 397 uint32_t *args, 398 int size); 399 400 extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align)); 401 extern int of_alias_get_id(const struct device_node *np, const char *stem); 402 extern int of_alias_get_highest_id(const char *stem); 403 404 bool of_machine_compatible_match(const char *const *compats); 405 406 /** 407 * of_machine_is_compatible - Test root of device tree for a given compatible value 408 * @compat: compatible string to look for in root node's compatible property. 409 * 410 * Return: true if the root node has the given value in its compatible property. 411 */ 412 static inline bool of_machine_is_compatible(const char *compat) 413 { 414 const char *compats[] = { compat, NULL }; 415 416 return of_machine_compatible_match(compats); 417 } 418 419 extern int of_add_property(struct device_node *np, struct property *prop); 420 extern int of_remove_property(struct device_node *np, struct property *prop); 421 extern int of_update_property(struct device_node *np, struct property *newprop); 422 423 /* For updating the device tree at runtime */ 424 #define OF_RECONFIG_ATTACH_NODE 0x0001 425 #define OF_RECONFIG_DETACH_NODE 0x0002 426 #define OF_RECONFIG_ADD_PROPERTY 0x0003 427 #define OF_RECONFIG_REMOVE_PROPERTY 0x0004 428 #define OF_RECONFIG_UPDATE_PROPERTY 0x0005 429 430 extern int of_attach_node(struct device_node *); 431 extern int of_detach_node(struct device_node *); 432 433 #define of_match_ptr(_ptr) (_ptr) 434 435 /* 436 * u32 u; 437 * 438 * of_property_for_each_u32(np, "propname", u) 439 * printk("U32 value: %x\n", u); 440 */ 441 const __be32 *of_prop_next_u32(const struct property *prop, const __be32 *cur, 442 u32 *pu); 443 /* 444 * struct property *prop; 445 * const char *s; 446 * 447 * of_property_for_each_string(np, "propname", prop, s) 448 * printk("String value: %s\n", s); 449 */ 450 const char *of_prop_next_string(const struct property *prop, const char *cur); 451 452 bool of_console_check(const struct device_node *dn, char *name, int index); 453 454 int of_map_id(const struct device_node *np, u32 id, 455 const char *map_name, const char *map_mask_name, 456 struct device_node **target, u32 *id_out); 457 458 phys_addr_t of_dma_get_max_cpu_address(struct device_node *np); 459 460 struct kimage; 461 void *of_kexec_alloc_and_setup_fdt(const struct kimage *image, 462 unsigned long initrd_load_addr, 463 unsigned long initrd_len, 464 const char *cmdline, size_t extra_fdt_size); 465 #else /* CONFIG_OF */ 466 467 static inline void of_core_init(void) 468 { 469 } 470 471 static inline bool is_of_node(const struct fwnode_handle *fwnode) 472 { 473 return false; 474 } 475 476 static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode) 477 { 478 return NULL; 479 } 480 481 static inline bool of_node_name_eq(const struct device_node *np, const char *name) 482 { 483 return false; 484 } 485 486 static inline bool of_node_name_prefix(const struct device_node *np, const char *prefix) 487 { 488 return false; 489 } 490 491 static inline const char* of_node_full_name(const struct device_node *np) 492 { 493 return "<no-node>"; 494 } 495 496 static inline struct device_node *of_find_node_by_name(struct device_node *from, 497 const char *name) 498 { 499 return NULL; 500 } 501 502 static inline struct device_node *of_find_node_by_type(struct device_node *from, 503 const char *type) 504 { 505 return NULL; 506 } 507 508 static inline struct device_node *of_find_matching_node_and_match( 509 struct device_node *from, 510 const struct of_device_id *matches, 511 const struct of_device_id **match) 512 { 513 return NULL; 514 } 515 516 static inline struct device_node *of_find_node_by_path(const char *path) 517 { 518 return NULL; 519 } 520 521 static inline struct device_node *of_find_node_opts_by_path(const char *path, 522 const char **opts) 523 { 524 return NULL; 525 } 526 527 static inline struct device_node *of_find_node_by_phandle(phandle handle) 528 { 529 return NULL; 530 } 531 532 static inline struct device_node *of_get_parent(const struct device_node *node) 533 { 534 return NULL; 535 } 536 537 static inline struct device_node *of_get_next_parent(struct device_node *node) 538 { 539 return NULL; 540 } 541 542 static inline struct device_node *of_get_next_child( 543 const struct device_node *node, struct device_node *prev) 544 { 545 return NULL; 546 } 547 548 static inline struct device_node *of_get_next_available_child( 549 const struct device_node *node, struct device_node *prev) 550 { 551 return NULL; 552 } 553 554 static inline struct device_node *of_get_next_reserved_child( 555 const struct device_node *node, struct device_node *prev) 556 { 557 return NULL; 558 } 559 560 static inline struct device_node *of_find_node_with_property( 561 struct device_node *from, const char *prop_name) 562 { 563 return NULL; 564 } 565 566 #define of_fwnode_handle(node) NULL 567 568 static inline struct device_node *of_get_compatible_child(const struct device_node *parent, 569 const char *compatible) 570 { 571 return NULL; 572 } 573 574 static inline struct device_node *of_get_child_by_name( 575 const struct device_node *node, 576 const char *name) 577 { 578 return NULL; 579 } 580 581 static inline int of_device_is_compatible(const struct device_node *device, 582 const char *name) 583 { 584 return 0; 585 } 586 587 static inline int of_device_compatible_match(const struct device_node *device, 588 const char *const *compat) 589 { 590 return 0; 591 } 592 593 static inline bool of_device_is_available(const struct device_node *device) 594 { 595 return false; 596 } 597 598 static inline bool of_device_is_big_endian(const struct device_node *device) 599 { 600 return false; 601 } 602 603 static inline struct property *of_find_property(const struct device_node *np, 604 const char *name, 605 int *lenp) 606 { 607 return NULL; 608 } 609 610 static inline struct device_node *of_find_compatible_node( 611 struct device_node *from, 612 const char *type, 613 const char *compat) 614 { 615 return NULL; 616 } 617 618 static inline int of_property_count_elems_of_size(const struct device_node *np, 619 const char *propname, int elem_size) 620 { 621 return -ENOSYS; 622 } 623 624 static inline int of_property_read_u32_index(const struct device_node *np, 625 const char *propname, u32 index, u32 *out_value) 626 { 627 return -ENOSYS; 628 } 629 630 static inline int of_property_read_u64_index(const struct device_node *np, 631 const char *propname, u32 index, u64 *out_value) 632 { 633 return -ENOSYS; 634 } 635 636 static inline const void *of_get_property(const struct device_node *node, 637 const char *name, 638 int *lenp) 639 { 640 return NULL; 641 } 642 643 static inline struct device_node *of_get_cpu_node(int cpu, 644 unsigned int *thread) 645 { 646 return NULL; 647 } 648 649 static inline struct device_node *of_cpu_device_node_get(int cpu) 650 { 651 return NULL; 652 } 653 654 static inline int of_cpu_node_to_id(struct device_node *np) 655 { 656 return -ENODEV; 657 } 658 659 static inline struct device_node *of_get_next_cpu_node(struct device_node *prev) 660 { 661 return NULL; 662 } 663 664 static inline struct device_node *of_get_cpu_state_node(struct device_node *cpu_node, 665 int index) 666 { 667 return NULL; 668 } 669 670 static inline int of_n_addr_cells(struct device_node *np) 671 { 672 return 0; 673 674 } 675 static inline int of_n_size_cells(struct device_node *np) 676 { 677 return 0; 678 } 679 680 static inline int of_property_read_variable_u8_array(const struct device_node *np, 681 const char *propname, u8 *out_values, 682 size_t sz_min, size_t sz_max) 683 { 684 return -ENOSYS; 685 } 686 687 static inline int of_property_read_variable_u16_array(const struct device_node *np, 688 const char *propname, u16 *out_values, 689 size_t sz_min, size_t sz_max) 690 { 691 return -ENOSYS; 692 } 693 694 static inline int of_property_read_variable_u32_array(const struct device_node *np, 695 const char *propname, 696 u32 *out_values, 697 size_t sz_min, 698 size_t sz_max) 699 { 700 return -ENOSYS; 701 } 702 703 static inline int of_property_read_u64(const struct device_node *np, 704 const char *propname, u64 *out_value) 705 { 706 return -ENOSYS; 707 } 708 709 static inline int of_property_read_variable_u64_array(const struct device_node *np, 710 const char *propname, 711 u64 *out_values, 712 size_t sz_min, 713 size_t sz_max) 714 { 715 return -ENOSYS; 716 } 717 718 static inline int of_property_read_string(const struct device_node *np, 719 const char *propname, 720 const char **out_string) 721 { 722 return -ENOSYS; 723 } 724 725 static inline int of_property_match_string(const struct device_node *np, 726 const char *propname, 727 const char *string) 728 { 729 return -ENOSYS; 730 } 731 732 static inline int of_property_read_string_helper(const struct device_node *np, 733 const char *propname, 734 const char **out_strs, size_t sz, int index) 735 { 736 return -ENOSYS; 737 } 738 739 static inline int __of_parse_phandle_with_args(const struct device_node *np, 740 const char *list_name, 741 const char *cells_name, 742 int cell_count, 743 int index, 744 struct of_phandle_args *out_args) 745 { 746 return -ENOSYS; 747 } 748 749 static inline int of_parse_phandle_with_args_map(const struct device_node *np, 750 const char *list_name, 751 const char *stem_name, 752 int index, 753 struct of_phandle_args *out_args) 754 { 755 return -ENOSYS; 756 } 757 758 static inline int of_count_phandle_with_args(const struct device_node *np, 759 const char *list_name, 760 const char *cells_name) 761 { 762 return -ENOSYS; 763 } 764 765 static inline ssize_t of_modalias(const struct device_node *np, char *str, 766 ssize_t len) 767 { 768 return -ENODEV; 769 } 770 771 static inline int of_request_module(const struct device_node *np) 772 { 773 return -ENODEV; 774 } 775 776 static inline int of_phandle_iterator_init(struct of_phandle_iterator *it, 777 const struct device_node *np, 778 const char *list_name, 779 const char *cells_name, 780 int cell_count) 781 { 782 return -ENOSYS; 783 } 784 785 static inline int of_phandle_iterator_next(struct of_phandle_iterator *it) 786 { 787 return -ENOSYS; 788 } 789 790 static inline int of_phandle_iterator_args(struct of_phandle_iterator *it, 791 uint32_t *args, 792 int size) 793 { 794 return 0; 795 } 796 797 static inline int of_alias_get_id(struct device_node *np, const char *stem) 798 { 799 return -ENOSYS; 800 } 801 802 static inline int of_alias_get_highest_id(const char *stem) 803 { 804 return -ENOSYS; 805 } 806 807 static inline int of_machine_is_compatible(const char *compat) 808 { 809 return 0; 810 } 811 812 static inline int of_add_property(struct device_node *np, struct property *prop) 813 { 814 return 0; 815 } 816 817 static inline int of_remove_property(struct device_node *np, struct property *prop) 818 { 819 return 0; 820 } 821 822 static inline bool of_machine_compatible_match(const char *const *compats) 823 { 824 return false; 825 } 826 827 static inline bool of_console_check(const struct device_node *dn, const char *name, int index) 828 { 829 return false; 830 } 831 832 static inline const __be32 *of_prop_next_u32(const struct property *prop, 833 const __be32 *cur, u32 *pu) 834 { 835 return NULL; 836 } 837 838 static inline const char *of_prop_next_string(const struct property *prop, 839 const char *cur) 840 { 841 return NULL; 842 } 843 844 static inline int of_node_check_flag(struct device_node *n, unsigned long flag) 845 { 846 return 0; 847 } 848 849 static inline int of_node_test_and_set_flag(struct device_node *n, 850 unsigned long flag) 851 { 852 return 0; 853 } 854 855 static inline void of_node_set_flag(struct device_node *n, unsigned long flag) 856 { 857 } 858 859 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag) 860 { 861 } 862 863 static inline int of_property_check_flag(const struct property *p, 864 unsigned long flag) 865 { 866 return 0; 867 } 868 869 static inline void of_property_set_flag(struct property *p, unsigned long flag) 870 { 871 } 872 873 static inline void of_property_clear_flag(struct property *p, unsigned long flag) 874 { 875 } 876 877 static inline int of_map_id(const struct device_node *np, u32 id, 878 const char *map_name, const char *map_mask_name, 879 struct device_node **target, u32 *id_out) 880 { 881 return -EINVAL; 882 } 883 884 static inline phys_addr_t of_dma_get_max_cpu_address(struct device_node *np) 885 { 886 return PHYS_ADDR_MAX; 887 } 888 889 static inline const void *of_device_get_match_data(const struct device *dev) 890 { 891 return NULL; 892 } 893 894 #define of_match_ptr(_ptr) NULL 895 #define of_match_node(_matches, _node) NULL 896 #endif /* CONFIG_OF */ 897 898 /* Default string compare functions, Allow arch asm/prom.h to override */ 899 #if !defined(of_compat_cmp) 900 #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2)) 901 #define of_prop_cmp(s1, s2) strcmp((s1), (s2)) 902 #define of_node_cmp(s1, s2) strcasecmp((s1), (s2)) 903 #endif 904 905 static inline int of_prop_val_eq(const struct property *p1, const struct property *p2) 906 { 907 return p1->length == p2->length && 908 !memcmp(p1->value, p2->value, (size_t)p1->length); 909 } 910 911 #define for_each_property_of_node(dn, pp) \ 912 for (pp = dn->properties; pp != NULL; pp = pp->next) 913 914 #if defined(CONFIG_OF) && defined(CONFIG_NUMA) 915 extern int of_node_to_nid(struct device_node *np); 916 #else 917 static inline int of_node_to_nid(struct device_node *device) 918 { 919 return NUMA_NO_NODE; 920 } 921 #endif 922 923 #ifdef CONFIG_OF_NUMA 924 extern int of_numa_init(void); 925 #else 926 static inline int of_numa_init(void) 927 { 928 return -ENOSYS; 929 } 930 #endif 931 932 static inline struct device_node *of_find_matching_node( 933 struct device_node *from, 934 const struct of_device_id *matches) 935 { 936 return of_find_matching_node_and_match(from, matches, NULL); 937 } 938 939 static inline const char *of_node_get_device_type(const struct device_node *np) 940 { 941 return of_get_property(np, "device_type", NULL); 942 } 943 944 static inline bool of_node_is_type(const struct device_node *np, const char *type) 945 { 946 const char *match = of_node_get_device_type(np); 947 948 return np && match && type && !strcmp(match, type); 949 } 950 951 /** 952 * of_parse_phandle - Resolve a phandle property to a device_node pointer 953 * @np: Pointer to device node holding phandle property 954 * @phandle_name: Name of property holding a phandle value 955 * @index: For properties holding a table of phandles, this is the index into 956 * the table 957 * 958 * Return: The device_node pointer with refcount incremented. Use 959 * of_node_put() on it when done. 960 */ 961 static inline struct device_node *of_parse_phandle(const struct device_node *np, 962 const char *phandle_name, 963 int index) 964 { 965 struct of_phandle_args args; 966 967 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, 968 index, &args)) 969 return NULL; 970 971 return args.np; 972 } 973 974 /** 975 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list 976 * @np: pointer to a device tree node containing a list 977 * @list_name: property name that contains a list 978 * @cells_name: property name that specifies phandles' arguments count 979 * @index: index of a phandle to parse out 980 * @out_args: optional pointer to output arguments structure (will be filled) 981 * 982 * This function is useful to parse lists of phandles and their arguments. 983 * Returns 0 on success and fills out_args, on error returns appropriate 984 * errno value. 985 * 986 * Caller is responsible to call of_node_put() on the returned out_args->np 987 * pointer. 988 * 989 * Example:: 990 * 991 * phandle1: node1 { 992 * #list-cells = <2>; 993 * }; 994 * 995 * phandle2: node2 { 996 * #list-cells = <1>; 997 * }; 998 * 999 * node3 { 1000 * list = <&phandle1 1 2 &phandle2 3>; 1001 * }; 1002 * 1003 * To get a device_node of the ``node2`` node you may call this: 1004 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args); 1005 */ 1006 static inline int of_parse_phandle_with_args(const struct device_node *np, 1007 const char *list_name, 1008 const char *cells_name, 1009 int index, 1010 struct of_phandle_args *out_args) 1011 { 1012 int cell_count = -1; 1013 1014 /* If cells_name is NULL we assume a cell count of 0 */ 1015 if (!cells_name) 1016 cell_count = 0; 1017 1018 return __of_parse_phandle_with_args(np, list_name, cells_name, 1019 cell_count, index, out_args); 1020 } 1021 1022 /** 1023 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list 1024 * @np: pointer to a device tree node containing a list 1025 * @list_name: property name that contains a list 1026 * @cell_count: number of argument cells following the phandle 1027 * @index: index of a phandle to parse out 1028 * @out_args: optional pointer to output arguments structure (will be filled) 1029 * 1030 * This function is useful to parse lists of phandles and their arguments. 1031 * Returns 0 on success and fills out_args, on error returns appropriate 1032 * errno value. 1033 * 1034 * Caller is responsible to call of_node_put() on the returned out_args->np 1035 * pointer. 1036 * 1037 * Example:: 1038 * 1039 * phandle1: node1 { 1040 * }; 1041 * 1042 * phandle2: node2 { 1043 * }; 1044 * 1045 * node3 { 1046 * list = <&phandle1 0 2 &phandle2 2 3>; 1047 * }; 1048 * 1049 * To get a device_node of the ``node2`` node you may call this: 1050 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args); 1051 */ 1052 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np, 1053 const char *list_name, 1054 int cell_count, 1055 int index, 1056 struct of_phandle_args *out_args) 1057 { 1058 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count, 1059 index, out_args); 1060 } 1061 1062 /** 1063 * of_parse_phandle_with_optional_args() - Find a node pointed by phandle in a list 1064 * @np: pointer to a device tree node containing a list 1065 * @list_name: property name that contains a list 1066 * @cells_name: property name that specifies phandles' arguments count 1067 * @index: index of a phandle to parse out 1068 * @out_args: optional pointer to output arguments structure (will be filled) 1069 * 1070 * Same as of_parse_phandle_with_args() except that if the cells_name property 1071 * is not found, cell_count of 0 is assumed. 1072 * 1073 * This is used to useful, if you have a phandle which didn't have arguments 1074 * before and thus doesn't have a '#*-cells' property but is now migrated to 1075 * having arguments while retaining backwards compatibility. 1076 */ 1077 static inline int of_parse_phandle_with_optional_args(const struct device_node *np, 1078 const char *list_name, 1079 const char *cells_name, 1080 int index, 1081 struct of_phandle_args *out_args) 1082 { 1083 return __of_parse_phandle_with_args(np, list_name, cells_name, 1084 0, index, out_args); 1085 } 1086 1087 /** 1088 * of_phandle_args_equal() - Compare two of_phandle_args 1089 * @a1: First of_phandle_args to compare 1090 * @a2: Second of_phandle_args to compare 1091 * 1092 * Return: True if a1 and a2 are the same (same node pointer, same phandle 1093 * args), false otherwise. 1094 */ 1095 static inline bool of_phandle_args_equal(const struct of_phandle_args *a1, 1096 const struct of_phandle_args *a2) 1097 { 1098 return a1->np == a2->np && 1099 a1->args_count == a2->args_count && 1100 !memcmp(a1->args, a2->args, sizeof(a1->args[0]) * a1->args_count); 1101 } 1102 1103 /** 1104 * of_property_count_u8_elems - Count the number of u8 elements in a property 1105 * 1106 * @np: device node from which the property value is to be read. 1107 * @propname: name of the property to be searched. 1108 * 1109 * Search for a property in a device node and count the number of u8 elements 1110 * in it. 1111 * 1112 * Return: The number of elements on sucess, -EINVAL if the property does 1113 * not exist or its length does not match a multiple of u8 and -ENODATA if the 1114 * property does not have a value. 1115 */ 1116 static inline int of_property_count_u8_elems(const struct device_node *np, 1117 const char *propname) 1118 { 1119 return of_property_count_elems_of_size(np, propname, sizeof(u8)); 1120 } 1121 1122 /** 1123 * of_property_count_u16_elems - Count the number of u16 elements in a property 1124 * 1125 * @np: device node from which the property value is to be read. 1126 * @propname: name of the property to be searched. 1127 * 1128 * Search for a property in a device node and count the number of u16 elements 1129 * in it. 1130 * 1131 * Return: The number of elements on sucess, -EINVAL if the property does 1132 * not exist or its length does not match a multiple of u16 and -ENODATA if the 1133 * property does not have a value. 1134 */ 1135 static inline int of_property_count_u16_elems(const struct device_node *np, 1136 const char *propname) 1137 { 1138 return of_property_count_elems_of_size(np, propname, sizeof(u16)); 1139 } 1140 1141 /** 1142 * of_property_count_u32_elems - Count the number of u32 elements in a property 1143 * 1144 * @np: device node from which the property value is to be read. 1145 * @propname: name of the property to be searched. 1146 * 1147 * Search for a property in a device node and count the number of u32 elements 1148 * in it. 1149 * 1150 * Return: The number of elements on sucess, -EINVAL if the property does 1151 * not exist or its length does not match a multiple of u32 and -ENODATA if the 1152 * property does not have a value. 1153 */ 1154 static inline int of_property_count_u32_elems(const struct device_node *np, 1155 const char *propname) 1156 { 1157 return of_property_count_elems_of_size(np, propname, sizeof(u32)); 1158 } 1159 1160 /** 1161 * of_property_count_u64_elems - Count the number of u64 elements in a property 1162 * 1163 * @np: device node from which the property value is to be read. 1164 * @propname: name of the property to be searched. 1165 * 1166 * Search for a property in a device node and count the number of u64 elements 1167 * in it. 1168 * 1169 * Return: The number of elements on sucess, -EINVAL if the property does 1170 * not exist or its length does not match a multiple of u64 and -ENODATA if the 1171 * property does not have a value. 1172 */ 1173 static inline int of_property_count_u64_elems(const struct device_node *np, 1174 const char *propname) 1175 { 1176 return of_property_count_elems_of_size(np, propname, sizeof(u64)); 1177 } 1178 1179 /** 1180 * of_property_read_string_array() - Read an array of strings from a multiple 1181 * strings property. 1182 * @np: device node from which the property value is to be read. 1183 * @propname: name of the property to be searched. 1184 * @out_strs: output array of string pointers. 1185 * @sz: number of array elements to read. 1186 * 1187 * Search for a property in a device tree node and retrieve a list of 1188 * terminated string values (pointer to data, not a copy) in that property. 1189 * 1190 * Return: If @out_strs is NULL, the number of strings in the property is returned. 1191 */ 1192 static inline int of_property_read_string_array(const struct device_node *np, 1193 const char *propname, const char **out_strs, 1194 size_t sz) 1195 { 1196 return of_property_read_string_helper(np, propname, out_strs, sz, 0); 1197 } 1198 1199 /** 1200 * of_property_count_strings() - Find and return the number of strings from a 1201 * multiple strings property. 1202 * @np: device node from which the property value is to be read. 1203 * @propname: name of the property to be searched. 1204 * 1205 * Search for a property in a device tree node and retrieve the number of null 1206 * terminated string contain in it. 1207 * 1208 * Return: The number of strings on success, -EINVAL if the property does not 1209 * exist, -ENODATA if property does not have a value, and -EILSEQ if the string 1210 * is not null-terminated within the length of the property data. 1211 */ 1212 static inline int of_property_count_strings(const struct device_node *np, 1213 const char *propname) 1214 { 1215 return of_property_read_string_helper(np, propname, NULL, 0, 0); 1216 } 1217 1218 /** 1219 * of_property_read_string_index() - Find and read a string from a multiple 1220 * strings property. 1221 * @np: device node from which the property value is to be read. 1222 * @propname: name of the property to be searched. 1223 * @index: index of the string in the list of strings 1224 * @output: pointer to null terminated return string, modified only if 1225 * return value is 0. 1226 * 1227 * Search for a property in a device tree node and retrieve a null 1228 * terminated string value (pointer to data, not a copy) in the list of strings 1229 * contained in that property. 1230 * 1231 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if 1232 * property does not have a value, and -EILSEQ if the string is not 1233 * null-terminated within the length of the property data. 1234 * 1235 * The out_string pointer is modified only if a valid string can be decoded. 1236 */ 1237 static inline int of_property_read_string_index(const struct device_node *np, 1238 const char *propname, 1239 int index, const char **output) 1240 { 1241 int rc = of_property_read_string_helper(np, propname, output, 1, index); 1242 return rc < 0 ? rc : 0; 1243 } 1244 1245 /** 1246 * of_property_read_bool - Find a property 1247 * @np: device node from which the property value is to be read. 1248 * @propname: name of the property to be searched. 1249 * 1250 * Search for a boolean property in a device node. Usage on non-boolean 1251 * property types is deprecated. 1252 * 1253 * Return: true if the property exists false otherwise. 1254 */ 1255 static inline bool of_property_read_bool(const struct device_node *np, 1256 const char *propname) 1257 { 1258 const struct property *prop = of_find_property(np, propname, NULL); 1259 1260 return prop ? true : false; 1261 } 1262 1263 /** 1264 * of_property_present - Test if a property is present in a node 1265 * @np: device node to search for the property. 1266 * @propname: name of the property to be searched. 1267 * 1268 * Test for a property present in a device node. 1269 * 1270 * Return: true if the property exists false otherwise. 1271 */ 1272 static inline bool of_property_present(const struct device_node *np, const char *propname) 1273 { 1274 return of_property_read_bool(np, propname); 1275 } 1276 1277 /** 1278 * of_property_read_u8_array - Find and read an array of u8 from a property. 1279 * 1280 * @np: device node from which the property value is to be read. 1281 * @propname: name of the property to be searched. 1282 * @out_values: pointer to return value, modified only if return value is 0. 1283 * @sz: number of array elements to read 1284 * 1285 * Search for a property in a device node and read 8-bit value(s) from 1286 * it. 1287 * 1288 * dts entry of array should be like: 1289 * ``property = /bits/ 8 <0x50 0x60 0x70>;`` 1290 * 1291 * Return: 0 on success, -EINVAL if the property does not exist, 1292 * -ENODATA if property does not have a value, and -EOVERFLOW if the 1293 * property data isn't large enough. 1294 * 1295 * The out_values is modified only if a valid u8 value can be decoded. 1296 */ 1297 static inline int of_property_read_u8_array(const struct device_node *np, 1298 const char *propname, 1299 u8 *out_values, size_t sz) 1300 { 1301 int ret = of_property_read_variable_u8_array(np, propname, out_values, 1302 sz, 0); 1303 if (ret >= 0) 1304 return 0; 1305 else 1306 return ret; 1307 } 1308 1309 /** 1310 * of_property_read_u16_array - Find and read an array of u16 from a property. 1311 * 1312 * @np: device node from which the property value is to be read. 1313 * @propname: name of the property to be searched. 1314 * @out_values: pointer to return value, modified only if return value is 0. 1315 * @sz: number of array elements to read 1316 * 1317 * Search for a property in a device node and read 16-bit value(s) from 1318 * it. 1319 * 1320 * dts entry of array should be like: 1321 * ``property = /bits/ 16 <0x5000 0x6000 0x7000>;`` 1322 * 1323 * Return: 0 on success, -EINVAL if the property does not exist, 1324 * -ENODATA if property does not have a value, and -EOVERFLOW if the 1325 * property data isn't large enough. 1326 * 1327 * The out_values is modified only if a valid u16 value can be decoded. 1328 */ 1329 static inline int of_property_read_u16_array(const struct device_node *np, 1330 const char *propname, 1331 u16 *out_values, size_t sz) 1332 { 1333 int ret = of_property_read_variable_u16_array(np, propname, out_values, 1334 sz, 0); 1335 if (ret >= 0) 1336 return 0; 1337 else 1338 return ret; 1339 } 1340 1341 /** 1342 * of_property_read_u32_array - Find and read an array of 32 bit integers 1343 * from a property. 1344 * 1345 * @np: device node from which the property value is to be read. 1346 * @propname: name of the property to be searched. 1347 * @out_values: pointer to return value, modified only if return value is 0. 1348 * @sz: number of array elements to read 1349 * 1350 * Search for a property in a device node and read 32-bit value(s) from 1351 * it. 1352 * 1353 * Return: 0 on success, -EINVAL if the property does not exist, 1354 * -ENODATA if property does not have a value, and -EOVERFLOW if the 1355 * property data isn't large enough. 1356 * 1357 * The out_values is modified only if a valid u32 value can be decoded. 1358 */ 1359 static inline int of_property_read_u32_array(const struct device_node *np, 1360 const char *propname, 1361 u32 *out_values, size_t sz) 1362 { 1363 int ret = of_property_read_variable_u32_array(np, propname, out_values, 1364 sz, 0); 1365 if (ret >= 0) 1366 return 0; 1367 else 1368 return ret; 1369 } 1370 1371 /** 1372 * of_property_read_u64_array - Find and read an array of 64 bit integers 1373 * from a property. 1374 * 1375 * @np: device node from which the property value is to be read. 1376 * @propname: name of the property to be searched. 1377 * @out_values: pointer to return value, modified only if return value is 0. 1378 * @sz: number of array elements to read 1379 * 1380 * Search for a property in a device node and read 64-bit value(s) from 1381 * it. 1382 * 1383 * Return: 0 on success, -EINVAL if the property does not exist, 1384 * -ENODATA if property does not have a value, and -EOVERFLOW if the 1385 * property data isn't large enough. 1386 * 1387 * The out_values is modified only if a valid u64 value can be decoded. 1388 */ 1389 static inline int of_property_read_u64_array(const struct device_node *np, 1390 const char *propname, 1391 u64 *out_values, size_t sz) 1392 { 1393 int ret = of_property_read_variable_u64_array(np, propname, out_values, 1394 sz, 0); 1395 if (ret >= 0) 1396 return 0; 1397 else 1398 return ret; 1399 } 1400 1401 static inline int of_property_read_u8(const struct device_node *np, 1402 const char *propname, 1403 u8 *out_value) 1404 { 1405 return of_property_read_u8_array(np, propname, out_value, 1); 1406 } 1407 1408 static inline int of_property_read_u16(const struct device_node *np, 1409 const char *propname, 1410 u16 *out_value) 1411 { 1412 return of_property_read_u16_array(np, propname, out_value, 1); 1413 } 1414 1415 static inline int of_property_read_u32(const struct device_node *np, 1416 const char *propname, 1417 u32 *out_value) 1418 { 1419 return of_property_read_u32_array(np, propname, out_value, 1); 1420 } 1421 1422 static inline int of_property_read_s32(const struct device_node *np, 1423 const char *propname, 1424 s32 *out_value) 1425 { 1426 return of_property_read_u32(np, propname, (u32*) out_value); 1427 } 1428 1429 #define of_for_each_phandle(it, err, np, ln, cn, cc) \ 1430 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \ 1431 err = of_phandle_iterator_next(it); \ 1432 err == 0; \ 1433 err = of_phandle_iterator_next(it)) 1434 1435 #define of_property_for_each_u32(np, propname, u) \ 1436 for (struct {const struct property *prop; const __be32 *item; } _it = \ 1437 {of_find_property(np, propname, NULL), \ 1438 of_prop_next_u32(_it.prop, NULL, &u)}; \ 1439 _it.item; \ 1440 _it.item = of_prop_next_u32(_it.prop, _it.item, &u)) 1441 1442 #define of_property_for_each_string(np, propname, prop, s) \ 1443 for (prop = of_find_property(np, propname, NULL), \ 1444 s = of_prop_next_string(prop, NULL); \ 1445 s; \ 1446 s = of_prop_next_string(prop, s)) 1447 1448 #define for_each_node_by_name(dn, name) \ 1449 for (dn = of_find_node_by_name(NULL, name); dn; \ 1450 dn = of_find_node_by_name(dn, name)) 1451 #define for_each_node_by_type(dn, type) \ 1452 for (dn = of_find_node_by_type(NULL, type); dn; \ 1453 dn = of_find_node_by_type(dn, type)) 1454 #define for_each_compatible_node(dn, type, compatible) \ 1455 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \ 1456 dn = of_find_compatible_node(dn, type, compatible)) 1457 #define for_each_matching_node(dn, matches) \ 1458 for (dn = of_find_matching_node(NULL, matches); dn; \ 1459 dn = of_find_matching_node(dn, matches)) 1460 #define for_each_matching_node_and_match(dn, matches, match) \ 1461 for (dn = of_find_matching_node_and_match(NULL, matches, match); \ 1462 dn; dn = of_find_matching_node_and_match(dn, matches, match)) 1463 1464 #define for_each_child_of_node(parent, child) \ 1465 for (child = of_get_next_child(parent, NULL); child != NULL; \ 1466 child = of_get_next_child(parent, child)) 1467 1468 #define for_each_child_of_node_scoped(parent, child) \ 1469 for (struct device_node *child __free(device_node) = \ 1470 of_get_next_child(parent, NULL); \ 1471 child != NULL; \ 1472 child = of_get_next_child(parent, child)) 1473 1474 #define for_each_child_of_node_with_prefix(parent, child, prefix) \ 1475 for (struct device_node *child __free(device_node) = \ 1476 of_get_next_child_with_prefix(parent, NULL, prefix); \ 1477 child != NULL; \ 1478 child = of_get_next_child_with_prefix(parent, child, prefix)) 1479 1480 #define for_each_available_child_of_node(parent, child) \ 1481 for (child = of_get_next_available_child(parent, NULL); child != NULL; \ 1482 child = of_get_next_available_child(parent, child)) 1483 #define for_each_reserved_child_of_node(parent, child) \ 1484 for (child = of_get_next_reserved_child(parent, NULL); child != NULL; \ 1485 child = of_get_next_reserved_child(parent, child)) 1486 1487 #define for_each_available_child_of_node_scoped(parent, child) \ 1488 for (struct device_node *child __free(device_node) = \ 1489 of_get_next_available_child(parent, NULL); \ 1490 child != NULL; \ 1491 child = of_get_next_available_child(parent, child)) 1492 1493 #define for_each_of_cpu_node(cpu) \ 1494 for (cpu = of_get_next_cpu_node(NULL); cpu != NULL; \ 1495 cpu = of_get_next_cpu_node(cpu)) 1496 1497 #define for_each_node_with_property(dn, prop_name) \ 1498 for (dn = of_find_node_with_property(NULL, prop_name); dn; \ 1499 dn = of_find_node_with_property(dn, prop_name)) 1500 1501 static inline int of_get_child_count(const struct device_node *np) 1502 { 1503 struct device_node *child; 1504 int num = 0; 1505 1506 for_each_child_of_node(np, child) 1507 num++; 1508 1509 return num; 1510 } 1511 1512 static inline int of_get_available_child_count(const struct device_node *np) 1513 { 1514 struct device_node *child; 1515 int num = 0; 1516 1517 for_each_available_child_of_node(np, child) 1518 num++; 1519 1520 return num; 1521 } 1522 1523 #define _OF_DECLARE_STUB(table, name, compat, fn, fn_type) \ 1524 static const struct of_device_id __of_table_##name \ 1525 __attribute__((unused)) \ 1526 = { .compatible = compat, \ 1527 .data = (fn == (fn_type)NULL) ? fn : fn } 1528 1529 #if defined(CONFIG_OF) && !defined(MODULE) 1530 #define _OF_DECLARE(table, name, compat, fn, fn_type) \ 1531 static const struct of_device_id __of_table_##name \ 1532 __used __section("__" #table "_of_table") \ 1533 __aligned(__alignof__(struct of_device_id)) \ 1534 = { .compatible = compat, \ 1535 .data = (fn == (fn_type)NULL) ? fn : fn } 1536 #else 1537 #define _OF_DECLARE(table, name, compat, fn, fn_type) \ 1538 _OF_DECLARE_STUB(table, name, compat, fn, fn_type) 1539 #endif 1540 1541 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *); 1542 typedef int (*of_init_fn_1_ret)(struct device_node *); 1543 typedef void (*of_init_fn_1)(struct device_node *); 1544 1545 #define OF_DECLARE_1(table, name, compat, fn) \ 1546 _OF_DECLARE(table, name, compat, fn, of_init_fn_1) 1547 #define OF_DECLARE_1_RET(table, name, compat, fn) \ 1548 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret) 1549 #define OF_DECLARE_2(table, name, compat, fn) \ 1550 _OF_DECLARE(table, name, compat, fn, of_init_fn_2) 1551 1552 /** 1553 * struct of_changeset_entry - Holds a changeset entry 1554 * 1555 * @node: list_head for the log list 1556 * @action: notifier action 1557 * @np: pointer to the device node affected 1558 * @prop: pointer to the property affected 1559 * @old_prop: hold a pointer to the original property 1560 * 1561 * Every modification of the device tree during a changeset 1562 * is held in a list of of_changeset_entry structures. 1563 * That way we can recover from a partial application, or we can 1564 * revert the changeset 1565 */ 1566 struct of_changeset_entry { 1567 struct list_head node; 1568 unsigned long action; 1569 struct device_node *np; 1570 struct property *prop; 1571 struct property *old_prop; 1572 }; 1573 1574 /** 1575 * struct of_changeset - changeset tracker structure 1576 * 1577 * @entries: list_head for the changeset entries 1578 * 1579 * changesets are a convenient way to apply bulk changes to the 1580 * live tree. In case of an error, changes are rolled-back. 1581 * changesets live on after initial application, and if not 1582 * destroyed after use, they can be reverted in one single call. 1583 */ 1584 struct of_changeset { 1585 struct list_head entries; 1586 }; 1587 1588 enum of_reconfig_change { 1589 OF_RECONFIG_NO_CHANGE = 0, 1590 OF_RECONFIG_CHANGE_ADD, 1591 OF_RECONFIG_CHANGE_REMOVE, 1592 }; 1593 1594 struct notifier_block; 1595 1596 #ifdef CONFIG_OF_DYNAMIC 1597 extern int of_reconfig_notifier_register(struct notifier_block *); 1598 extern int of_reconfig_notifier_unregister(struct notifier_block *); 1599 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd); 1600 extern int of_reconfig_get_state_change(unsigned long action, 1601 struct of_reconfig_data *arg); 1602 1603 extern void of_changeset_init(struct of_changeset *ocs); 1604 extern void of_changeset_destroy(struct of_changeset *ocs); 1605 extern int of_changeset_apply(struct of_changeset *ocs); 1606 extern int of_changeset_revert(struct of_changeset *ocs); 1607 extern int of_changeset_action(struct of_changeset *ocs, 1608 unsigned long action, struct device_node *np, 1609 struct property *prop); 1610 1611 static inline int of_changeset_attach_node(struct of_changeset *ocs, 1612 struct device_node *np) 1613 { 1614 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL); 1615 } 1616 1617 static inline int of_changeset_detach_node(struct of_changeset *ocs, 1618 struct device_node *np) 1619 { 1620 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL); 1621 } 1622 1623 static inline int of_changeset_add_property(struct of_changeset *ocs, 1624 struct device_node *np, struct property *prop) 1625 { 1626 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop); 1627 } 1628 1629 static inline int of_changeset_remove_property(struct of_changeset *ocs, 1630 struct device_node *np, struct property *prop) 1631 { 1632 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop); 1633 } 1634 1635 static inline int of_changeset_update_property(struct of_changeset *ocs, 1636 struct device_node *np, struct property *prop) 1637 { 1638 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop); 1639 } 1640 1641 struct device_node *of_changeset_create_node(struct of_changeset *ocs, 1642 struct device_node *parent, 1643 const char *full_name); 1644 int of_changeset_add_prop_string(struct of_changeset *ocs, 1645 struct device_node *np, 1646 const char *prop_name, const char *str); 1647 int of_changeset_add_prop_string_array(struct of_changeset *ocs, 1648 struct device_node *np, 1649 const char *prop_name, 1650 const char * const *str_array, size_t sz); 1651 int of_changeset_add_prop_u32_array(struct of_changeset *ocs, 1652 struct device_node *np, 1653 const char *prop_name, 1654 const u32 *array, size_t sz); 1655 static inline int of_changeset_add_prop_u32(struct of_changeset *ocs, 1656 struct device_node *np, 1657 const char *prop_name, 1658 const u32 val) 1659 { 1660 return of_changeset_add_prop_u32_array(ocs, np, prop_name, &val, 1); 1661 } 1662 1663 int of_changeset_update_prop_string(struct of_changeset *ocs, 1664 struct device_node *np, 1665 const char *prop_name, const char *str); 1666 1667 int of_changeset_add_prop_bool(struct of_changeset *ocs, struct device_node *np, 1668 const char *prop_name); 1669 1670 #else /* CONFIG_OF_DYNAMIC */ 1671 static inline int of_reconfig_notifier_register(struct notifier_block *nb) 1672 { 1673 return -EINVAL; 1674 } 1675 static inline int of_reconfig_notifier_unregister(struct notifier_block *nb) 1676 { 1677 return -EINVAL; 1678 } 1679 static inline int of_reconfig_notify(unsigned long action, 1680 struct of_reconfig_data *arg) 1681 { 1682 return -EINVAL; 1683 } 1684 static inline int of_reconfig_get_state_change(unsigned long action, 1685 struct of_reconfig_data *arg) 1686 { 1687 return -EINVAL; 1688 } 1689 #endif /* CONFIG_OF_DYNAMIC */ 1690 1691 /** 1692 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node 1693 * @np: Pointer to the given device_node 1694 * 1695 * Return: true if present false otherwise 1696 */ 1697 static inline bool of_device_is_system_power_controller(const struct device_node *np) 1698 { 1699 return of_property_read_bool(np, "system-power-controller"); 1700 } 1701 1702 /** 1703 * of_have_populated_dt() - Has DT been populated by bootloader 1704 * 1705 * Return: True if a DTB has been populated by the bootloader and it isn't the 1706 * empty builtin one. False otherwise. 1707 */ 1708 static inline bool of_have_populated_dt(void) 1709 { 1710 #ifdef CONFIG_OF 1711 return of_property_present(of_root, "compatible"); 1712 #else 1713 return false; 1714 #endif 1715 } 1716 1717 /* 1718 * Overlay support 1719 */ 1720 1721 enum of_overlay_notify_action { 1722 OF_OVERLAY_INIT = 0, /* kzalloc() of ovcs sets this value */ 1723 OF_OVERLAY_PRE_APPLY, 1724 OF_OVERLAY_POST_APPLY, 1725 OF_OVERLAY_PRE_REMOVE, 1726 OF_OVERLAY_POST_REMOVE, 1727 }; 1728 1729 static inline const char *of_overlay_action_name(enum of_overlay_notify_action action) 1730 { 1731 static const char *const of_overlay_action_name[] = { 1732 "init", 1733 "pre-apply", 1734 "post-apply", 1735 "pre-remove", 1736 "post-remove", 1737 }; 1738 1739 return of_overlay_action_name[action]; 1740 } 1741 1742 struct of_overlay_notify_data { 1743 struct device_node *overlay; 1744 struct device_node *target; 1745 }; 1746 1747 #ifdef CONFIG_OF_OVERLAY 1748 1749 int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size, 1750 int *ovcs_id, const struct device_node *target_base); 1751 int of_overlay_remove(int *ovcs_id); 1752 int of_overlay_remove_all(void); 1753 1754 int of_overlay_notifier_register(struct notifier_block *nb); 1755 int of_overlay_notifier_unregister(struct notifier_block *nb); 1756 1757 #else 1758 1759 static inline int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size, 1760 int *ovcs_id, const struct device_node *target_base) 1761 { 1762 return -ENOTSUPP; 1763 } 1764 1765 static inline int of_overlay_remove(int *ovcs_id) 1766 { 1767 return -ENOTSUPP; 1768 } 1769 1770 static inline int of_overlay_remove_all(void) 1771 { 1772 return -ENOTSUPP; 1773 } 1774 1775 static inline int of_overlay_notifier_register(struct notifier_block *nb) 1776 { 1777 return 0; 1778 } 1779 1780 static inline int of_overlay_notifier_unregister(struct notifier_block *nb) 1781 { 1782 return 0; 1783 } 1784 1785 #endif 1786 1787 #endif /* _LINUX_OF_H */ 1788