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