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