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