at v4.15 41 kB view raw
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 32typedef u32 phandle; 33typedef u32 ihandle; 34 35struct 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) 52struct of_irq_controller; 53#endif 54 55struct 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 80struct of_phandle_args { 81 struct device_node *np; 82 int args_count; 83 uint32_t args[MAX_PHANDLE_ARGS]; 84}; 85 86struct 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 103struct of_reconfig_data { 104 struct device_node *dn; 105 struct property *prop; 106 struct property *old_prop; 107}; 108 109/* initialize a node */ 110extern struct kobj_type of_node_ktype; 111extern const struct fwnode_operations of_fwnode_ops; 112static 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 127extern struct device_node *of_node_get(struct device_node *node); 128extern void of_node_put(struct device_node *node); 129#else /* CONFIG_OF_DYNAMIC */ 130/* Dummy ref counting routines - to be implemented later */ 131static inline struct device_node *of_node_get(struct device_node *node) 132{ 133 return node; 134} 135static 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. */ 139extern struct device_node *of_root; 140extern struct device_node *of_chosen; 141extern struct device_node *of_aliases; 142extern struct device_node *of_stdout; 143extern 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 154void of_core_init(void); 155 156static 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 179static inline bool of_have_populated_dt(void) 180{ 181 return of_root != NULL; 182} 183 184static inline bool of_node_is_root(const struct device_node *node) 185{ 186 return node && (node->parent == NULL); 187} 188 189static inline int of_node_check_flag(struct device_node *n, unsigned long flag) 190{ 191 return test_bit(flag, &n->_flags); 192} 193 194static 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 200static inline void of_node_set_flag(struct device_node *n, unsigned long flag) 201{ 202 set_bit(flag, &n->_flags); 203} 204 205static 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) 211static inline int of_property_check_flag(struct property *p, unsigned long flag) 212{ 213 return test_bit(flag, &p->_flags); 214} 215 216static inline void of_property_set_flag(struct property *p, unsigned long flag) 217{ 218 set_bit(flag, &p->_flags); 219} 220 221static inline void of_property_clear_flag(struct property *p, unsigned long flag) 222{ 223 clear_bit(flag, &p->_flags); 224} 225#endif 226 227extern struct device_node *__of_find_all_nodes(struct device_node *prev); 228extern 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) */ 235static 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 */ 244static 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 263static 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) 271extern struct device_node *of_find_node_by_name(struct device_node *from, 272 const char *name); 273extern struct device_node *of_find_node_by_type(struct device_node *from, 274 const char *type); 275extern struct device_node *of_find_compatible_node(struct device_node *from, 276 const char *type, const char *compat); 277extern 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 282extern struct device_node *of_find_node_opts_by_path(const char *path, 283 const char **opts); 284static 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 289extern struct device_node *of_find_node_by_phandle(phandle handle); 290extern struct device_node *of_get_parent(const struct device_node *node); 291extern struct device_node *of_get_next_parent(struct device_node *node); 292extern struct device_node *of_get_next_child(const struct device_node *node, 293 struct device_node *prev); 294extern struct device_node *of_get_next_available_child( 295 const struct device_node *node, struct device_node *prev); 296 297extern struct device_node *of_get_child_by_name(const struct device_node *node, 298 const char *name); 299 300/* cache lookup */ 301extern struct device_node *of_find_next_cache_node(const struct device_node *); 302extern int of_find_last_cache_level(unsigned int cpu); 303extern struct device_node *of_find_node_with_property( 304 struct device_node *from, const char *prop_name); 305 306extern struct property *of_find_property(const struct device_node *np, 307 const char *name, 308 int *lenp); 309extern int of_property_count_elems_of_size(const struct device_node *np, 310 const char *propname, int elem_size); 311extern int of_property_read_u32_index(const struct device_node *np, 312 const char *propname, 313 u32 index, u32 *out_value); 314extern int of_property_read_u64_index(const struct device_node *np, 315 const char *propname, 316 u32 index, u64 *out_value); 317extern 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); 320extern 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); 323extern 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); 328extern int of_property_read_u64(const struct device_node *np, 329 const char *propname, u64 *out_value); 330extern 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 336extern int of_property_read_string(const struct device_node *np, 337 const char *propname, 338 const char **out_string); 339extern int of_property_match_string(const struct device_node *np, 340 const char *propname, 341 const char *string); 342extern 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); 345extern int of_device_is_compatible(const struct device_node *device, 346 const char *); 347extern int of_device_compatible_match(struct device_node *device, 348 const char *const *compat); 349extern bool of_device_is_available(const struct device_node *device); 350extern bool of_device_is_big_endian(const struct device_node *device); 351extern const void *of_get_property(const struct device_node *node, 352 const char *name, 353 int *lenp); 354extern 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 358extern int of_n_addr_cells(struct device_node *np); 359extern int of_n_size_cells(struct device_node *np); 360extern const struct of_device_id *of_match_node( 361 const struct of_device_id *matches, const struct device_node *node); 362extern int of_modalias_node(struct device_node *node, char *modalias, int len); 363extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args); 364extern struct device_node *of_parse_phandle(const struct device_node *np, 365 const char *phandle_name, 366 int index); 367extern 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); 370extern 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); 373extern 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 */ 377extern 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 383extern int of_phandle_iterator_next(struct of_phandle_iterator *it); 384extern int of_phandle_iterator_args(struct of_phandle_iterator *it, 385 uint32_t *args, 386 int size); 387 388extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align)); 389extern int of_alias_get_id(struct device_node *np, const char *stem); 390extern int of_alias_get_highest_id(const char *stem); 391 392extern int of_machine_is_compatible(const char *compat); 393 394extern int of_add_property(struct device_node *np, struct property *prop); 395extern int of_remove_property(struct device_node *np, struct property *prop); 396extern 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 405extern int of_attach_node(struct device_node *); 406extern 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 */ 428static 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 */ 458static 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 */ 486static 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 */ 514static 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 */ 534const __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 */ 543const char *of_prop_next_string(struct property *prop, const char *cur); 544 545bool of_console_check(struct device_node *dn, char *name, int index); 546 547#else /* CONFIG_OF */ 548 549static inline void of_core_init(void) 550{ 551} 552 553static inline bool is_of_node(const struct fwnode_handle *fwnode) 554{ 555 return false; 556} 557 558static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode) 559{ 560 return NULL; 561} 562 563static inline const char* of_node_full_name(const struct device_node *np) 564{ 565 return "<no-node>"; 566} 567 568static inline struct device_node *of_find_node_by_name(struct device_node *from, 569 const char *name) 570{ 571 return NULL; 572} 573 574static inline struct device_node *of_find_node_by_type(struct device_node *from, 575 const char *type) 576{ 577 return NULL; 578} 579 580static inline struct device_node *of_find_matching_node_and_match( 581 struct device_node *from, 582 const struct of_device_id *matches, 583 const struct of_device_id **match) 584{ 585 return NULL; 586} 587 588static inline struct device_node *of_find_node_by_path(const char *path) 589{ 590 return NULL; 591} 592 593static inline struct device_node *of_find_node_opts_by_path(const char *path, 594 const char **opts) 595{ 596 return NULL; 597} 598 599static inline struct device_node *of_find_node_by_phandle(phandle handle) 600{ 601 return NULL; 602} 603 604static inline struct device_node *of_get_parent(const struct device_node *node) 605{ 606 return NULL; 607} 608 609static inline struct device_node *of_get_next_child( 610 const struct device_node *node, struct device_node *prev) 611{ 612 return NULL; 613} 614 615static inline struct device_node *of_get_next_available_child( 616 const struct device_node *node, struct device_node *prev) 617{ 618 return NULL; 619} 620 621static inline struct device_node *of_find_node_with_property( 622 struct device_node *from, const char *prop_name) 623{ 624 return NULL; 625} 626 627#define of_fwnode_handle(node) NULL 628 629static inline bool of_have_populated_dt(void) 630{ 631 return false; 632} 633 634static inline struct device_node *of_get_child_by_name( 635 const struct device_node *node, 636 const char *name) 637{ 638 return NULL; 639} 640 641static inline int of_device_is_compatible(const struct device_node *device, 642 const char *name) 643{ 644 return 0; 645} 646 647static inline int of_device_compatible_match(struct device_node *device, 648 const char *const *compat) 649{ 650 return 0; 651} 652 653static inline bool of_device_is_available(const struct device_node *device) 654{ 655 return false; 656} 657 658static inline bool of_device_is_big_endian(const struct device_node *device) 659{ 660 return false; 661} 662 663static inline struct property *of_find_property(const struct device_node *np, 664 const char *name, 665 int *lenp) 666{ 667 return NULL; 668} 669 670static inline struct device_node *of_find_compatible_node( 671 struct device_node *from, 672 const char *type, 673 const char *compat) 674{ 675 return NULL; 676} 677 678static inline int of_property_count_elems_of_size(const struct device_node *np, 679 const char *propname, int elem_size) 680{ 681 return -ENOSYS; 682} 683 684static inline int of_property_read_u8_array(const struct device_node *np, 685 const char *propname, u8 *out_values, size_t sz) 686{ 687 return -ENOSYS; 688} 689 690static inline int of_property_read_u16_array(const struct device_node *np, 691 const char *propname, u16 *out_values, size_t sz) 692{ 693 return -ENOSYS; 694} 695 696static inline int of_property_read_u32_array(const struct device_node *np, 697 const char *propname, 698 u32 *out_values, size_t sz) 699{ 700 return -ENOSYS; 701} 702 703static inline int of_property_read_u64_array(const struct device_node *np, 704 const char *propname, 705 u64 *out_values, size_t sz) 706{ 707 return -ENOSYS; 708} 709 710static inline int of_property_read_u32_index(const struct device_node *np, 711 const char *propname, u32 index, u32 *out_value) 712{ 713 return -ENOSYS; 714} 715 716static inline int of_property_read_u64_index(const struct device_node *np, 717 const char *propname, u32 index, u64 *out_value) 718{ 719 return -ENOSYS; 720} 721 722static inline const void *of_get_property(const struct device_node *node, 723 const char *name, 724 int *lenp) 725{ 726 return NULL; 727} 728 729static inline struct device_node *of_get_cpu_node(int cpu, 730 unsigned int *thread) 731{ 732 return NULL; 733} 734 735static inline int of_n_addr_cells(struct device_node *np) 736{ 737 return 0; 738 739} 740static inline int of_n_size_cells(struct device_node *np) 741{ 742 return 0; 743} 744 745static inline int of_property_read_variable_u8_array(const struct device_node *np, 746 const char *propname, u8 *out_values, 747 size_t sz_min, size_t sz_max) 748{ 749 return -ENOSYS; 750} 751 752static inline int of_property_read_variable_u16_array(const struct device_node *np, 753 const char *propname, u16 *out_values, 754 size_t sz_min, size_t sz_max) 755{ 756 return -ENOSYS; 757} 758 759static inline int of_property_read_variable_u32_array(const struct device_node *np, 760 const char *propname, 761 u32 *out_values, 762 size_t sz_min, 763 size_t sz_max) 764{ 765 return -ENOSYS; 766} 767 768static inline int of_property_read_u64(const struct device_node *np, 769 const char *propname, u64 *out_value) 770{ 771 return -ENOSYS; 772} 773 774static inline int of_property_read_variable_u64_array(const struct device_node *np, 775 const char *propname, 776 u64 *out_values, 777 size_t sz_min, 778 size_t sz_max) 779{ 780 return -ENOSYS; 781} 782 783static inline int of_property_read_string(const struct device_node *np, 784 const char *propname, 785 const char **out_string) 786{ 787 return -ENOSYS; 788} 789 790static inline int of_property_match_string(const struct device_node *np, 791 const char *propname, 792 const char *string) 793{ 794 return -ENOSYS; 795} 796 797static inline int of_property_read_string_helper(const struct device_node *np, 798 const char *propname, 799 const char **out_strs, size_t sz, int index) 800{ 801 return -ENOSYS; 802} 803 804static inline struct device_node *of_parse_phandle(const struct device_node *np, 805 const char *phandle_name, 806 int index) 807{ 808 return NULL; 809} 810 811static inline int of_parse_phandle_with_args(const struct device_node *np, 812 const char *list_name, 813 const char *cells_name, 814 int index, 815 struct of_phandle_args *out_args) 816{ 817 return -ENOSYS; 818} 819 820static inline int of_parse_phandle_with_fixed_args(const struct device_node *np, 821 const char *list_name, int cells_count, int index, 822 struct of_phandle_args *out_args) 823{ 824 return -ENOSYS; 825} 826 827static inline int of_count_phandle_with_args(struct device_node *np, 828 const char *list_name, 829 const char *cells_name) 830{ 831 return -ENOSYS; 832} 833 834static inline int of_phandle_iterator_init(struct of_phandle_iterator *it, 835 const struct device_node *np, 836 const char *list_name, 837 const char *cells_name, 838 int cell_count) 839{ 840 return -ENOSYS; 841} 842 843static inline int of_phandle_iterator_next(struct of_phandle_iterator *it) 844{ 845 return -ENOSYS; 846} 847 848static inline int of_phandle_iterator_args(struct of_phandle_iterator *it, 849 uint32_t *args, 850 int size) 851{ 852 return 0; 853} 854 855static inline int of_alias_get_id(struct device_node *np, const char *stem) 856{ 857 return -ENOSYS; 858} 859 860static inline int of_alias_get_highest_id(const char *stem) 861{ 862 return -ENOSYS; 863} 864 865static inline int of_machine_is_compatible(const char *compat) 866{ 867 return 0; 868} 869 870static inline bool of_console_check(const struct device_node *dn, const char *name, int index) 871{ 872 return false; 873} 874 875static inline const __be32 *of_prop_next_u32(struct property *prop, 876 const __be32 *cur, u32 *pu) 877{ 878 return NULL; 879} 880 881static inline const char *of_prop_next_string(struct property *prop, 882 const char *cur) 883{ 884 return NULL; 885} 886 887static inline int of_node_check_flag(struct device_node *n, unsigned long flag) 888{ 889 return 0; 890} 891 892static inline int of_node_test_and_set_flag(struct device_node *n, 893 unsigned long flag) 894{ 895 return 0; 896} 897 898static inline void of_node_set_flag(struct device_node *n, unsigned long flag) 899{ 900} 901 902static inline void of_node_clear_flag(struct device_node *n, unsigned long flag) 903{ 904} 905 906static inline int of_property_check_flag(struct property *p, unsigned long flag) 907{ 908 return 0; 909} 910 911static inline void of_property_set_flag(struct property *p, unsigned long flag) 912{ 913} 914 915static inline void of_property_clear_flag(struct property *p, unsigned long flag) 916{ 917} 918 919#define of_match_ptr(_ptr) NULL 920#define of_match_node(_matches, _node) NULL 921#endif /* CONFIG_OF */ 922 923/* Default string compare functions, Allow arch asm/prom.h to override */ 924#if !defined(of_compat_cmp) 925#define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2)) 926#define of_prop_cmp(s1, s2) strcmp((s1), (s2)) 927#define of_node_cmp(s1, s2) strcasecmp((s1), (s2)) 928#endif 929 930#if defined(CONFIG_OF) && defined(CONFIG_NUMA) 931extern int of_node_to_nid(struct device_node *np); 932#else 933static inline int of_node_to_nid(struct device_node *device) 934{ 935 return NUMA_NO_NODE; 936} 937#endif 938 939#ifdef CONFIG_OF_NUMA 940extern int of_numa_init(void); 941#else 942static inline int of_numa_init(void) 943{ 944 return -ENOSYS; 945} 946#endif 947 948static inline struct device_node *of_find_matching_node( 949 struct device_node *from, 950 const struct of_device_id *matches) 951{ 952 return of_find_matching_node_and_match(from, matches, NULL); 953} 954 955/** 956 * of_property_count_u8_elems - Count the number of u8 elements in a property 957 * 958 * @np: device node from which the property value is to be read. 959 * @propname: name of the property to be searched. 960 * 961 * Search for a property in a device node and count the number of u8 elements 962 * in it. Returns number of elements on sucess, -EINVAL if the property does 963 * not exist or its length does not match a multiple of u8 and -ENODATA if the 964 * property does not have a value. 965 */ 966static inline int of_property_count_u8_elems(const struct device_node *np, 967 const char *propname) 968{ 969 return of_property_count_elems_of_size(np, propname, sizeof(u8)); 970} 971 972/** 973 * of_property_count_u16_elems - Count the number of u16 elements in a property 974 * 975 * @np: device node from which the property value is to be read. 976 * @propname: name of the property to be searched. 977 * 978 * Search for a property in a device node and count the number of u16 elements 979 * in it. Returns number of elements on sucess, -EINVAL if the property does 980 * not exist or its length does not match a multiple of u16 and -ENODATA if the 981 * property does not have a value. 982 */ 983static inline int of_property_count_u16_elems(const struct device_node *np, 984 const char *propname) 985{ 986 return of_property_count_elems_of_size(np, propname, sizeof(u16)); 987} 988 989/** 990 * of_property_count_u32_elems - Count the number of u32 elements in a property 991 * 992 * @np: device node from which the property value is to be read. 993 * @propname: name of the property to be searched. 994 * 995 * Search for a property in a device node and count the number of u32 elements 996 * in it. Returns number of elements on sucess, -EINVAL if the property does 997 * not exist or its length does not match a multiple of u32 and -ENODATA if the 998 * property does not have a value. 999 */ 1000static inline int of_property_count_u32_elems(const struct device_node *np, 1001 const char *propname) 1002{ 1003 return of_property_count_elems_of_size(np, propname, sizeof(u32)); 1004} 1005 1006/** 1007 * of_property_count_u64_elems - Count the number of u64 elements in a property 1008 * 1009 * @np: device node from which the property value is to be read. 1010 * @propname: name of the property to be searched. 1011 * 1012 * Search for a property in a device node and count the number of u64 elements 1013 * in it. Returns number of elements on sucess, -EINVAL if the property does 1014 * not exist or its length does not match a multiple of u64 and -ENODATA if the 1015 * property does not have a value. 1016 */ 1017static inline int of_property_count_u64_elems(const struct device_node *np, 1018 const char *propname) 1019{ 1020 return of_property_count_elems_of_size(np, propname, sizeof(u64)); 1021} 1022 1023/** 1024 * of_property_read_string_array() - Read an array of strings from a multiple 1025 * strings property. 1026 * @np: device node from which the property value is to be read. 1027 * @propname: name of the property to be searched. 1028 * @out_strs: output array of string pointers. 1029 * @sz: number of array elements to read. 1030 * 1031 * Search for a property in a device tree node and retrieve a list of 1032 * terminated string values (pointer to data, not a copy) in that property. 1033 * 1034 * If @out_strs is NULL, the number of strings in the property is returned. 1035 */ 1036static inline int of_property_read_string_array(const struct device_node *np, 1037 const char *propname, const char **out_strs, 1038 size_t sz) 1039{ 1040 return of_property_read_string_helper(np, propname, out_strs, sz, 0); 1041} 1042 1043/** 1044 * of_property_count_strings() - Find and return the number of strings from a 1045 * multiple strings property. 1046 * @np: device node from which the property value is to be read. 1047 * @propname: name of the property to be searched. 1048 * 1049 * Search for a property in a device tree node and retrieve the number of null 1050 * terminated string contain in it. Returns the number of strings on 1051 * success, -EINVAL if the property does not exist, -ENODATA if property 1052 * does not have a value, and -EILSEQ if the string is not null-terminated 1053 * within the length of the property data. 1054 */ 1055static inline int of_property_count_strings(const struct device_node *np, 1056 const char *propname) 1057{ 1058 return of_property_read_string_helper(np, propname, NULL, 0, 0); 1059} 1060 1061/** 1062 * of_property_read_string_index() - Find and read a string from a multiple 1063 * strings property. 1064 * @np: device node from which the property value is to be read. 1065 * @propname: name of the property to be searched. 1066 * @index: index of the string in the list of strings 1067 * @out_string: pointer to null terminated return string, modified only if 1068 * return value is 0. 1069 * 1070 * Search for a property in a device tree node and retrieve a null 1071 * terminated string value (pointer to data, not a copy) in the list of strings 1072 * contained in that property. 1073 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if 1074 * property does not have a value, and -EILSEQ if the string is not 1075 * null-terminated within the length of the property data. 1076 * 1077 * The out_string pointer is modified only if a valid string can be decoded. 1078 */ 1079static inline int of_property_read_string_index(const struct device_node *np, 1080 const char *propname, 1081 int index, const char **output) 1082{ 1083 int rc = of_property_read_string_helper(np, propname, output, 1, index); 1084 return rc < 0 ? rc : 0; 1085} 1086 1087/** 1088 * of_property_read_bool - Findfrom a property 1089 * @np: device node from which the property value is to be read. 1090 * @propname: name of the property to be searched. 1091 * 1092 * Search for a property in a device node. 1093 * Returns true if the property exists false otherwise. 1094 */ 1095static inline bool of_property_read_bool(const struct device_node *np, 1096 const char *propname) 1097{ 1098 struct property *prop = of_find_property(np, propname, NULL); 1099 1100 return prop ? true : false; 1101} 1102 1103static inline int of_property_read_u8(const struct device_node *np, 1104 const char *propname, 1105 u8 *out_value) 1106{ 1107 return of_property_read_u8_array(np, propname, out_value, 1); 1108} 1109 1110static inline int of_property_read_u16(const struct device_node *np, 1111 const char *propname, 1112 u16 *out_value) 1113{ 1114 return of_property_read_u16_array(np, propname, out_value, 1); 1115} 1116 1117static inline int of_property_read_u32(const struct device_node *np, 1118 const char *propname, 1119 u32 *out_value) 1120{ 1121 return of_property_read_u32_array(np, propname, out_value, 1); 1122} 1123 1124static inline int of_property_read_s32(const struct device_node *np, 1125 const char *propname, 1126 s32 *out_value) 1127{ 1128 return of_property_read_u32(np, propname, (u32*) out_value); 1129} 1130 1131#define of_for_each_phandle(it, err, np, ln, cn, cc) \ 1132 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \ 1133 err = of_phandle_iterator_next(it); \ 1134 err == 0; \ 1135 err = of_phandle_iterator_next(it)) 1136 1137#define of_property_for_each_u32(np, propname, prop, p, u) \ 1138 for (prop = of_find_property(np, propname, NULL), \ 1139 p = of_prop_next_u32(prop, NULL, &u); \ 1140 p; \ 1141 p = of_prop_next_u32(prop, p, &u)) 1142 1143#define of_property_for_each_string(np, propname, prop, s) \ 1144 for (prop = of_find_property(np, propname, NULL), \ 1145 s = of_prop_next_string(prop, NULL); \ 1146 s; \ 1147 s = of_prop_next_string(prop, s)) 1148 1149#define for_each_node_by_name(dn, name) \ 1150 for (dn = of_find_node_by_name(NULL, name); dn; \ 1151 dn = of_find_node_by_name(dn, name)) 1152#define for_each_node_by_type(dn, type) \ 1153 for (dn = of_find_node_by_type(NULL, type); dn; \ 1154 dn = of_find_node_by_type(dn, type)) 1155#define for_each_compatible_node(dn, type, compatible) \ 1156 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \ 1157 dn = of_find_compatible_node(dn, type, compatible)) 1158#define for_each_matching_node(dn, matches) \ 1159 for (dn = of_find_matching_node(NULL, matches); dn; \ 1160 dn = of_find_matching_node(dn, matches)) 1161#define for_each_matching_node_and_match(dn, matches, match) \ 1162 for (dn = of_find_matching_node_and_match(NULL, matches, match); \ 1163 dn; dn = of_find_matching_node_and_match(dn, matches, match)) 1164 1165#define for_each_child_of_node(parent, child) \ 1166 for (child = of_get_next_child(parent, NULL); child != NULL; \ 1167 child = of_get_next_child(parent, child)) 1168#define for_each_available_child_of_node(parent, child) \ 1169 for (child = of_get_next_available_child(parent, NULL); child != NULL; \ 1170 child = of_get_next_available_child(parent, child)) 1171 1172#define for_each_node_with_property(dn, prop_name) \ 1173 for (dn = of_find_node_with_property(NULL, prop_name); dn; \ 1174 dn = of_find_node_with_property(dn, prop_name)) 1175 1176static inline int of_get_child_count(const struct device_node *np) 1177{ 1178 struct device_node *child; 1179 int num = 0; 1180 1181 for_each_child_of_node(np, child) 1182 num++; 1183 1184 return num; 1185} 1186 1187static inline int of_get_available_child_count(const struct device_node *np) 1188{ 1189 struct device_node *child; 1190 int num = 0; 1191 1192 for_each_available_child_of_node(np, child) 1193 num++; 1194 1195 return num; 1196} 1197 1198#if defined(CONFIG_OF) && !defined(MODULE) 1199#define _OF_DECLARE(table, name, compat, fn, fn_type) \ 1200 static const struct of_device_id __of_table_##name \ 1201 __used __section(__##table##_of_table) \ 1202 = { .compatible = compat, \ 1203 .data = (fn == (fn_type)NULL) ? fn : fn } 1204#else 1205#define _OF_DECLARE(table, name, compat, fn, fn_type) \ 1206 static const struct of_device_id __of_table_##name \ 1207 __attribute__((unused)) \ 1208 = { .compatible = compat, \ 1209 .data = (fn == (fn_type)NULL) ? fn : fn } 1210#endif 1211 1212typedef int (*of_init_fn_2)(struct device_node *, struct device_node *); 1213typedef int (*of_init_fn_1_ret)(struct device_node *); 1214typedef void (*of_init_fn_1)(struct device_node *); 1215 1216#define OF_DECLARE_1(table, name, compat, fn) \ 1217 _OF_DECLARE(table, name, compat, fn, of_init_fn_1) 1218#define OF_DECLARE_1_RET(table, name, compat, fn) \ 1219 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret) 1220#define OF_DECLARE_2(table, name, compat, fn) \ 1221 _OF_DECLARE(table, name, compat, fn, of_init_fn_2) 1222 1223/** 1224 * struct of_changeset_entry - Holds a changeset entry 1225 * 1226 * @node: list_head for the log list 1227 * @action: notifier action 1228 * @np: pointer to the device node affected 1229 * @prop: pointer to the property affected 1230 * @old_prop: hold a pointer to the original property 1231 * 1232 * Every modification of the device tree during a changeset 1233 * is held in a list of of_changeset_entry structures. 1234 * That way we can recover from a partial application, or we can 1235 * revert the changeset 1236 */ 1237struct of_changeset_entry { 1238 struct list_head node; 1239 unsigned long action; 1240 struct device_node *np; 1241 struct property *prop; 1242 struct property *old_prop; 1243}; 1244 1245/** 1246 * struct of_changeset - changeset tracker structure 1247 * 1248 * @entries: list_head for the changeset entries 1249 * 1250 * changesets are a convenient way to apply bulk changes to the 1251 * live tree. In case of an error, changes are rolled-back. 1252 * changesets live on after initial application, and if not 1253 * destroyed after use, they can be reverted in one single call. 1254 */ 1255struct of_changeset { 1256 struct list_head entries; 1257}; 1258 1259enum of_reconfig_change { 1260 OF_RECONFIG_NO_CHANGE = 0, 1261 OF_RECONFIG_CHANGE_ADD, 1262 OF_RECONFIG_CHANGE_REMOVE, 1263}; 1264 1265#ifdef CONFIG_OF_DYNAMIC 1266extern int of_reconfig_notifier_register(struct notifier_block *); 1267extern int of_reconfig_notifier_unregister(struct notifier_block *); 1268extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd); 1269extern int of_reconfig_get_state_change(unsigned long action, 1270 struct of_reconfig_data *arg); 1271 1272extern void of_changeset_init(struct of_changeset *ocs); 1273extern void of_changeset_destroy(struct of_changeset *ocs); 1274extern int of_changeset_apply(struct of_changeset *ocs); 1275extern int of_changeset_revert(struct of_changeset *ocs); 1276extern int of_changeset_action(struct of_changeset *ocs, 1277 unsigned long action, struct device_node *np, 1278 struct property *prop); 1279 1280static inline int of_changeset_attach_node(struct of_changeset *ocs, 1281 struct device_node *np) 1282{ 1283 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL); 1284} 1285 1286static inline int of_changeset_detach_node(struct of_changeset *ocs, 1287 struct device_node *np) 1288{ 1289 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL); 1290} 1291 1292static inline int of_changeset_add_property(struct of_changeset *ocs, 1293 struct device_node *np, struct property *prop) 1294{ 1295 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop); 1296} 1297 1298static inline int of_changeset_remove_property(struct of_changeset *ocs, 1299 struct device_node *np, struct property *prop) 1300{ 1301 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop); 1302} 1303 1304static inline int of_changeset_update_property(struct of_changeset *ocs, 1305 struct device_node *np, struct property *prop) 1306{ 1307 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop); 1308} 1309#else /* CONFIG_OF_DYNAMIC */ 1310static inline int of_reconfig_notifier_register(struct notifier_block *nb) 1311{ 1312 return -EINVAL; 1313} 1314static inline int of_reconfig_notifier_unregister(struct notifier_block *nb) 1315{ 1316 return -EINVAL; 1317} 1318static inline int of_reconfig_notify(unsigned long action, 1319 struct of_reconfig_data *arg) 1320{ 1321 return -EINVAL; 1322} 1323static inline int of_reconfig_get_state_change(unsigned long action, 1324 struct of_reconfig_data *arg) 1325{ 1326 return -EINVAL; 1327} 1328#endif /* CONFIG_OF_DYNAMIC */ 1329 1330/** 1331 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node 1332 * @np: Pointer to the given device_node 1333 * 1334 * return true if present false otherwise 1335 */ 1336static inline bool of_device_is_system_power_controller(const struct device_node *np) 1337{ 1338 return of_property_read_bool(np, "system-power-controller"); 1339} 1340 1341/** 1342 * Overlay support 1343 */ 1344 1345enum of_overlay_notify_action { 1346 OF_OVERLAY_PRE_APPLY = 0, 1347 OF_OVERLAY_POST_APPLY, 1348 OF_OVERLAY_PRE_REMOVE, 1349 OF_OVERLAY_POST_REMOVE, 1350}; 1351 1352struct of_overlay_notify_data { 1353 struct device_node *overlay; 1354 struct device_node *target; 1355}; 1356 1357#ifdef CONFIG_OF_OVERLAY 1358 1359/* ID based overlays; the API for external users */ 1360int of_overlay_apply(struct device_node *tree, int *ovcs_id); 1361int of_overlay_remove(int *ovcs_id); 1362int of_overlay_remove_all(void); 1363 1364int of_overlay_notifier_register(struct notifier_block *nb); 1365int of_overlay_notifier_unregister(struct notifier_block *nb); 1366 1367#else 1368 1369static inline int of_overlay_apply(struct device_node *tree, int *ovcs_id) 1370{ 1371 return -ENOTSUPP; 1372} 1373 1374static inline int of_overlay_remove(int *ovcs_id) 1375{ 1376 return -ENOTSUPP; 1377} 1378 1379static inline int of_overlay_remove_all(void) 1380{ 1381 return -ENOTSUPP; 1382} 1383 1384static inline int of_overlay_notifier_register(struct notifier_block *nb) 1385{ 1386 return 0; 1387} 1388 1389static inline int of_overlay_notifier_unregister(struct notifier_block *nb) 1390{ 1391 return 0; 1392} 1393 1394#endif 1395 1396#endif /* _LINUX_OF_H */