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