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