1*644ec0a9SSimon Glass /* 2*644ec0a9SSimon Glass * Originally from Linux v4.9 3*644ec0a9SSimon Glass * Copyright (C) 1996-2005 Paul Mackerras. 4*644ec0a9SSimon Glass * 5*644ec0a9SSimon Glass * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp. 6*644ec0a9SSimon Glass * Updates for SPARC64 by David S. Miller 7*644ec0a9SSimon Glass * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp. 8*644ec0a9SSimon Glass * 9*644ec0a9SSimon Glass * Copyright (c) 2017 Google, Inc 10*644ec0a9SSimon Glass * Written by Simon Glass <sjg@chromium.org> 11*644ec0a9SSimon Glass * 12*644ec0a9SSimon Glass * Modified for U-Boot 13*644ec0a9SSimon Glass * Copyright (c) 2017 Google, Inc 14*644ec0a9SSimon Glass * 15*644ec0a9SSimon Glass * SPDX-License-Identifier: GPL-2.0+ 16*644ec0a9SSimon Glass */ 17*644ec0a9SSimon Glass 18*644ec0a9SSimon Glass #ifndef _DM_OF_ACCESS_H 19*644ec0a9SSimon Glass #define _DM_OF_ACCESS_H 20*644ec0a9SSimon Glass 21*644ec0a9SSimon Glass #include <dm/of.h> 22*644ec0a9SSimon Glass 23*644ec0a9SSimon Glass /** 24*644ec0a9SSimon Glass * of_find_all_nodes - Get next node in global list 25*644ec0a9SSimon Glass * @prev: Previous node or NULL to start iteration 26*644ec0a9SSimon Glass * of_node_put() will be called on it 27*644ec0a9SSimon Glass * 28*644ec0a9SSimon Glass * Returns a node pointer with refcount incremented, use 29*644ec0a9SSimon Glass * of_node_put() on it when done. 30*644ec0a9SSimon Glass */ 31*644ec0a9SSimon Glass struct device_node *of_find_all_nodes(struct device_node *prev); 32*644ec0a9SSimon Glass 33*644ec0a9SSimon Glass #define for_each_of_allnodes_from(from, dn) \ 34*644ec0a9SSimon Glass for (dn = of_find_all_nodes(from); dn; dn = of_find_all_nodes(dn)) 35*644ec0a9SSimon Glass #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn) 36*644ec0a9SSimon Glass 37*644ec0a9SSimon Glass /* Dummy functions to mirror Linux. These are not used in U-Boot */ 38*644ec0a9SSimon Glass #define of_node_get(x) (x) 39*644ec0a9SSimon Glass static inline void of_node_put(const struct device_node *np) { } 40*644ec0a9SSimon Glass 41*644ec0a9SSimon Glass /** 42*644ec0a9SSimon Glass * of_n_addr_cells() - Get the number of address cells for a node 43*644ec0a9SSimon Glass * 44*644ec0a9SSimon Glass * This walks back up the tree to find the closest #address-cells property 45*644ec0a9SSimon Glass * which controls the given node. 46*644ec0a9SSimon Glass * 47*644ec0a9SSimon Glass * @np: Node pointer to check 48*644ec0a9SSimon Glass * @return number of address cells this node uses 49*644ec0a9SSimon Glass */ 50*644ec0a9SSimon Glass int of_n_addr_cells(const struct device_node *np); 51*644ec0a9SSimon Glass 52*644ec0a9SSimon Glass /** 53*644ec0a9SSimon Glass * of_n_size_cells() - Get the number of size cells for a node 54*644ec0a9SSimon Glass * 55*644ec0a9SSimon Glass * This walks back up the tree to find the closest #size-cells property 56*644ec0a9SSimon Glass * which controls the given node. 57*644ec0a9SSimon Glass * 58*644ec0a9SSimon Glass * @np: Node pointer to check 59*644ec0a9SSimon Glass * @return number of size cells this node uses 60*644ec0a9SSimon Glass */ 61*644ec0a9SSimon Glass int of_n_size_cells(const struct device_node *np); 62*644ec0a9SSimon Glass 63*644ec0a9SSimon Glass /** 64*644ec0a9SSimon Glass * of_find_property() - find a property in a node 65*644ec0a9SSimon Glass * 66*644ec0a9SSimon Glass * @np: Pointer to device node holding property 67*644ec0a9SSimon Glass * @name: Name of property 68*644ec0a9SSimon Glass * @lenp: If non-NULL, returns length of property 69*644ec0a9SSimon Glass * @return pointer to property, or NULL if not found 70*644ec0a9SSimon Glass */ 71*644ec0a9SSimon Glass struct property *of_find_property(const struct device_node *np, 72*644ec0a9SSimon Glass const char *name, int *lenp); 73*644ec0a9SSimon Glass 74*644ec0a9SSimon Glass /** 75*644ec0a9SSimon Glass * of_get_property() - get a property value 76*644ec0a9SSimon Glass * 77*644ec0a9SSimon Glass * Find a property with a given name for a given node and return the value. 78*644ec0a9SSimon Glass * 79*644ec0a9SSimon Glass * @np: Pointer to device node holding property 80*644ec0a9SSimon Glass * @name: Name of property 81*644ec0a9SSimon Glass * @lenp: If non-NULL, returns length of property 82*644ec0a9SSimon Glass * @return pointer to property value, or NULL if not found 83*644ec0a9SSimon Glass */ 84*644ec0a9SSimon Glass const void *of_get_property(const struct device_node *np, const char *name, 85*644ec0a9SSimon Glass int *lenp); 86*644ec0a9SSimon Glass 87*644ec0a9SSimon Glass /** 88*644ec0a9SSimon Glass * of_device_is_compatible() - Check if the node matches given constraints 89*644ec0a9SSimon Glass * @device: pointer to node 90*644ec0a9SSimon Glass * @compat: required compatible string, NULL or "" for any match 91*644ec0a9SSimon Glass * @type: required device_type value, NULL or "" for any match 92*644ec0a9SSimon Glass * @name: required node name, NULL or "" for any match 93*644ec0a9SSimon Glass * 94*644ec0a9SSimon Glass * Checks if the given @compat, @type and @name strings match the 95*644ec0a9SSimon Glass * properties of the given @device. A constraints can be skipped by 96*644ec0a9SSimon Glass * passing NULL or an empty string as the constraint. 97*644ec0a9SSimon Glass * 98*644ec0a9SSimon Glass * @return 0 for no match, and a positive integer on match. The return 99*644ec0a9SSimon Glass * value is a relative score with larger values indicating better 100*644ec0a9SSimon Glass * matches. The score is weighted for the most specific compatible value 101*644ec0a9SSimon Glass * to get the highest score. Matching type is next, followed by matching 102*644ec0a9SSimon Glass * name. Practically speaking, this results in the following priority 103*644ec0a9SSimon Glass * order for matches: 104*644ec0a9SSimon Glass * 105*644ec0a9SSimon Glass * 1. specific compatible && type && name 106*644ec0a9SSimon Glass * 2. specific compatible && type 107*644ec0a9SSimon Glass * 3. specific compatible && name 108*644ec0a9SSimon Glass * 4. specific compatible 109*644ec0a9SSimon Glass * 5. general compatible && type && name 110*644ec0a9SSimon Glass * 6. general compatible && type 111*644ec0a9SSimon Glass * 7. general compatible && name 112*644ec0a9SSimon Glass * 8. general compatible 113*644ec0a9SSimon Glass * 9. type && name 114*644ec0a9SSimon Glass * 10. type 115*644ec0a9SSimon Glass * 11. name 116*644ec0a9SSimon Glass */ 117*644ec0a9SSimon Glass int of_device_is_compatible(const struct device_node *np, const char *compat, 118*644ec0a9SSimon Glass const char *type, const char *name); 119*644ec0a9SSimon Glass 120*644ec0a9SSimon Glass /** 121*644ec0a9SSimon Glass * of_device_is_available() - check if a device is available for use 122*644ec0a9SSimon Glass * 123*644ec0a9SSimon Glass * @device: Node to check for availability 124*644ec0a9SSimon Glass * 125*644ec0a9SSimon Glass * @return true if the status property is absent or set to "okay", false 126*644ec0a9SSimon Glass * otherwise 127*644ec0a9SSimon Glass */ 128*644ec0a9SSimon Glass bool of_device_is_available(const struct device_node *np); 129*644ec0a9SSimon Glass 130*644ec0a9SSimon Glass /** 131*644ec0a9SSimon Glass * of_get_parent() - Get a node's parent, if any 132*644ec0a9SSimon Glass * 133*644ec0a9SSimon Glass * @node: Node to check 134*644ec0a9SSimon Glass * @eturns a node pointer, or NULL if none 135*644ec0a9SSimon Glass */ 136*644ec0a9SSimon Glass struct device_node *of_get_parent(const struct device_node *np); 137*644ec0a9SSimon Glass 138*644ec0a9SSimon Glass /** 139*644ec0a9SSimon Glass * of_find_node_opts_by_path() - Find a node matching a full OF path 140*644ec0a9SSimon Glass * 141*644ec0a9SSimon Glass * @path: Either the full path to match, or if the path does not start with 142*644ec0a9SSimon Glass * '/', the name of a property of the /aliases node (an alias). In the 143*644ec0a9SSimon Glass * case of an alias, the node matching the alias' value will be returned. 144*644ec0a9SSimon Glass * @opts: Address of a pointer into which to store the start of an options 145*644ec0a9SSimon Glass * string appended to the end of the path with a ':' separator. Can be NULL 146*644ec0a9SSimon Glass * 147*644ec0a9SSimon Glass * Valid paths: 148*644ec0a9SSimon Glass * /foo/bar Full path 149*644ec0a9SSimon Glass * foo Valid alias 150*644ec0a9SSimon Glass * foo/bar Valid alias + relative path 151*644ec0a9SSimon Glass * 152*644ec0a9SSimon Glass * @return a node pointer or NULL if not found 153*644ec0a9SSimon Glass */ 154*644ec0a9SSimon Glass struct device_node *of_find_node_opts_by_path(const char *path, 155*644ec0a9SSimon Glass const char **opts); 156*644ec0a9SSimon Glass 157*644ec0a9SSimon Glass static inline struct device_node *of_find_node_by_path(const char *path) 158*644ec0a9SSimon Glass { 159*644ec0a9SSimon Glass return of_find_node_opts_by_path(path, NULL); 160*644ec0a9SSimon Glass } 161*644ec0a9SSimon Glass 162*644ec0a9SSimon Glass /** 163*644ec0a9SSimon Glass * of_find_compatible_node() - find a node based on its compatible string 164*644ec0a9SSimon Glass * 165*644ec0a9SSimon Glass * Find a node based on type and one of the tokens in its "compatible" property 166*644ec0a9SSimon Glass * @from: Node to start searching from or NULL. the node you pass will not be 167*644ec0a9SSimon Glass * searched, only the next one will; typically, you pass what the previous 168*644ec0a9SSimon Glass * call returned. 169*644ec0a9SSimon Glass * @type: The type string to match "device_type" or NULL to ignore 170*644ec0a9SSimon Glass * @compatible: The string to match to one of the tokens in the device 171*644ec0a9SSimon Glass * "compatible" list. 172*644ec0a9SSimon Glass * @return node pointer or NULL if not found 173*644ec0a9SSimon Glass */ 174*644ec0a9SSimon Glass struct device_node *of_find_compatible_node(struct device_node *from, 175*644ec0a9SSimon Glass const char *type, const char *compatible); 176*644ec0a9SSimon Glass 177*644ec0a9SSimon Glass /** 178*644ec0a9SSimon Glass * of_find_node_by_phandle() - Find a node given a phandle 179*644ec0a9SSimon Glass * 180*644ec0a9SSimon Glass * @handle: phandle of the node to find 181*644ec0a9SSimon Glass * 182*644ec0a9SSimon Glass * @return node pointer, or NULL if not found 183*644ec0a9SSimon Glass */ 184*644ec0a9SSimon Glass struct device_node *of_find_node_by_phandle(phandle handle); 185*644ec0a9SSimon Glass 186*644ec0a9SSimon Glass /** 187*644ec0a9SSimon Glass * of_read_u32() - Find and read a 32-bit integer from a property 188*644ec0a9SSimon Glass * 189*644ec0a9SSimon Glass * Search for a property in a device node and read a 32-bit value from 190*644ec0a9SSimon Glass * it. 191*644ec0a9SSimon Glass * 192*644ec0a9SSimon Glass * @np: device node from which the property value is to be read. 193*644ec0a9SSimon Glass * @propname: name of the property to be searched. 194*644ec0a9SSimon Glass * @outp: pointer to return value, modified only if return value is 0. 195*644ec0a9SSimon Glass * 196*644ec0a9SSimon Glass * @return 0 on success, -EINVAL if the property does not exist, 197*644ec0a9SSimon Glass * -ENODATA if property does not have a value, and -EOVERFLOW if the 198*644ec0a9SSimon Glass * property data isn't large enough. 199*644ec0a9SSimon Glass */ 200*644ec0a9SSimon Glass int of_read_u32(const struct device_node *np, const char *propname, u32 *outp); 201*644ec0a9SSimon Glass 202*644ec0a9SSimon Glass /** 203*644ec0a9SSimon Glass * of_read_u32_array() - Find and read an array of 32 bit integers 204*644ec0a9SSimon Glass * 205*644ec0a9SSimon Glass * Search for a property in a device node and read 32-bit value(s) from 206*644ec0a9SSimon Glass * it. 207*644ec0a9SSimon Glass * 208*644ec0a9SSimon Glass * @np: device node from which the property value is to be read. 209*644ec0a9SSimon Glass * @propname: name of the property to be searched. 210*644ec0a9SSimon Glass * @out_values: pointer to return value, modified only if return value is 0. 211*644ec0a9SSimon Glass * @sz: number of array elements to read 212*644ec0a9SSimon Glass * @return 0 on success, -EINVAL if the property does not exist, -ENODATA 213*644ec0a9SSimon Glass * if property does not have a value, and -EOVERFLOW is longer than sz. 214*644ec0a9SSimon Glass */ 215*644ec0a9SSimon Glass int of_read_u32_array(const struct device_node *np, const char *propname, 216*644ec0a9SSimon Glass u32 *out_values, size_t sz); 217*644ec0a9SSimon Glass 218*644ec0a9SSimon Glass /** 219*644ec0a9SSimon Glass * of_property_match_string() - Find string in a list and return index 220*644ec0a9SSimon Glass * 221*644ec0a9SSimon Glass * This function searches a string list property and returns the index 222*644ec0a9SSimon Glass * of a specific string value. 223*644ec0a9SSimon Glass * 224*644ec0a9SSimon Glass * @np: pointer to node containing string list property 225*644ec0a9SSimon Glass * @propname: string list property name 226*644ec0a9SSimon Glass * @string: pointer to string to search for in string list 227*644ec0a9SSimon Glass * @return 0 on success, -EINVAL if the property does not exist, -ENODATA 228*644ec0a9SSimon Glass * if property does not have a value, and -EOVERFLOW is longer than sz. 229*644ec0a9SSimon Glass */ 230*644ec0a9SSimon Glass int of_property_match_string(const struct device_node *np, const char *propname, 231*644ec0a9SSimon Glass const char *string); 232*644ec0a9SSimon Glass 233*644ec0a9SSimon Glass int of_property_read_string_helper(const struct device_node *np, 234*644ec0a9SSimon Glass const char *propname, const char **out_strs, 235*644ec0a9SSimon Glass size_t sz, int index); 236*644ec0a9SSimon Glass 237*644ec0a9SSimon Glass /** 238*644ec0a9SSimon Glass * of_property_read_string_index() - Find and read a string from a multiple 239*644ec0a9SSimon Glass * strings property. 240*644ec0a9SSimon Glass * @np: device node from which the property value is to be read. 241*644ec0a9SSimon Glass * @propname: name of the property to be searched. 242*644ec0a9SSimon Glass * @index: index of the string in the list of strings 243*644ec0a9SSimon Glass * @out_string: pointer to null terminated return string, modified only if 244*644ec0a9SSimon Glass * return value is 0. 245*644ec0a9SSimon Glass * 246*644ec0a9SSimon Glass * Search for a property in a device tree node and retrieve a null 247*644ec0a9SSimon Glass * terminated string value (pointer to data, not a copy) in the list of strings 248*644ec0a9SSimon Glass * contained in that property. 249*644ec0a9SSimon Glass * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if 250*644ec0a9SSimon Glass * property does not have a value, and -EILSEQ if the string is not 251*644ec0a9SSimon Glass * null-terminated within the length of the property data. 252*644ec0a9SSimon Glass * 253*644ec0a9SSimon Glass * The out_string pointer is modified only if a valid string can be decoded. 254*644ec0a9SSimon Glass */ 255*644ec0a9SSimon Glass static inline int of_property_read_string_index(const struct device_node *np, 256*644ec0a9SSimon Glass const char *propname, 257*644ec0a9SSimon Glass int index, const char **output) 258*644ec0a9SSimon Glass { 259*644ec0a9SSimon Glass int rc = of_property_read_string_helper(np, propname, output, 1, index); 260*644ec0a9SSimon Glass return rc < 0 ? rc : 0; 261*644ec0a9SSimon Glass } 262*644ec0a9SSimon Glass 263*644ec0a9SSimon Glass /** 264*644ec0a9SSimon Glass * of_parse_phandle - Resolve a phandle property to a device_node pointer 265*644ec0a9SSimon Glass * @np: Pointer to device node holding phandle property 266*644ec0a9SSimon Glass * @phandle_name: Name of property holding a phandle value 267*644ec0a9SSimon Glass * @index: For properties holding a table of phandles, this is the index into 268*644ec0a9SSimon Glass * the table 269*644ec0a9SSimon Glass * 270*644ec0a9SSimon Glass * Returns the device_node pointer with refcount incremented. Use 271*644ec0a9SSimon Glass * of_node_put() on it when done. 272*644ec0a9SSimon Glass */ 273*644ec0a9SSimon Glass struct device_node *of_parse_phandle(const struct device_node *np, 274*644ec0a9SSimon Glass const char *phandle_name, int index); 275*644ec0a9SSimon Glass 276*644ec0a9SSimon Glass /** 277*644ec0a9SSimon Glass * of_parse_phandle_with_args() - Find a node pointed by phandle in a list 278*644ec0a9SSimon Glass * 279*644ec0a9SSimon Glass * @np: pointer to a device tree node containing a list 280*644ec0a9SSimon Glass * @list_name: property name that contains a list 281*644ec0a9SSimon Glass * @cells_name: property name that specifies phandles' arguments count 282*644ec0a9SSimon Glass * @index: index of a phandle to parse out 283*644ec0a9SSimon Glass * @out_args: optional pointer to output arguments structure (will be filled) 284*644ec0a9SSimon Glass * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if 285*644ec0a9SSimon Glass * @list_name does not exist, -EINVAL if a phandle was not found, 286*644ec0a9SSimon Glass * @cells_name could not be found, the arguments were truncated or there 287*644ec0a9SSimon Glass * were too many arguments. 288*644ec0a9SSimon Glass * 289*644ec0a9SSimon Glass * This function is useful to parse lists of phandles and their arguments. 290*644ec0a9SSimon Glass * Returns 0 on success and fills out_args, on error returns appropriate 291*644ec0a9SSimon Glass * errno value. 292*644ec0a9SSimon Glass * 293*644ec0a9SSimon Glass * Caller is responsible to call of_node_put() on the returned out_args->np 294*644ec0a9SSimon Glass * pointer. 295*644ec0a9SSimon Glass * 296*644ec0a9SSimon Glass * Example: 297*644ec0a9SSimon Glass * 298*644ec0a9SSimon Glass * phandle1: node1 { 299*644ec0a9SSimon Glass * #list-cells = <2>; 300*644ec0a9SSimon Glass * } 301*644ec0a9SSimon Glass * 302*644ec0a9SSimon Glass * phandle2: node2 { 303*644ec0a9SSimon Glass * #list-cells = <1>; 304*644ec0a9SSimon Glass * } 305*644ec0a9SSimon Glass * 306*644ec0a9SSimon Glass * node3 { 307*644ec0a9SSimon Glass * list = <&phandle1 1 2 &phandle2 3>; 308*644ec0a9SSimon Glass * } 309*644ec0a9SSimon Glass * 310*644ec0a9SSimon Glass * To get a device_node of the `node2' node you may call this: 311*644ec0a9SSimon Glass * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args); 312*644ec0a9SSimon Glass */ 313*644ec0a9SSimon Glass int of_parse_phandle_with_args(const struct device_node *np, 314*644ec0a9SSimon Glass const char *list_name, const char *cells_name, 315*644ec0a9SSimon Glass int index, struct of_phandle_args *out_args); 316*644ec0a9SSimon Glass 317*644ec0a9SSimon Glass /** 318*644ec0a9SSimon Glass * of_alias_scan() - Scan all properties of the 'aliases' node 319*644ec0a9SSimon Glass * 320*644ec0a9SSimon Glass * The function scans all the properties of the 'aliases' node and populates 321*644ec0a9SSimon Glass * the lookup table with the properties. It returns the number of alias 322*644ec0a9SSimon Glass * properties found, or an error code in case of failure. 323*644ec0a9SSimon Glass * 324*644ec0a9SSimon Glass * @return 9 if OK, -ENOMEM if not enough memory 325*644ec0a9SSimon Glass */ 326*644ec0a9SSimon Glass int of_alias_scan(void); 327*644ec0a9SSimon Glass 328*644ec0a9SSimon Glass /** 329*644ec0a9SSimon Glass * of_alias_get_id - Get alias id for the given device_node 330*644ec0a9SSimon Glass * 331*644ec0a9SSimon Glass * Travels the lookup table to get the alias id for the given device_node and 332*644ec0a9SSimon Glass * alias stem. 333*644ec0a9SSimon Glass * 334*644ec0a9SSimon Glass * @np: Pointer to the given device_node 335*644ec0a9SSimon Glass * @stem: Alias stem of the given device_node 336*644ec0a9SSimon Glass * @return alias ID, if found, else -ENODEV 337*644ec0a9SSimon Glass */ 338*644ec0a9SSimon Glass int of_alias_get_id(const struct device_node *np, const char *stem); 339*644ec0a9SSimon Glass 340*644ec0a9SSimon Glass /** 341*644ec0a9SSimon Glass * of_get_stdout() - Get node to use for stdout 342*644ec0a9SSimon Glass * 343*644ec0a9SSimon Glass * @return node referred to by stdout-path alias, or NULL if none 344*644ec0a9SSimon Glass */ 345*644ec0a9SSimon Glass struct device_node *of_get_stdout(void); 346*644ec0a9SSimon Glass 347*644ec0a9SSimon Glass #endif 348