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