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