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