1f11c7ab9SSimon Glass /* 2f11c7ab9SSimon Glass * Function to read values from the device tree node attached to a udevice. 3f11c7ab9SSimon Glass * 4f11c7ab9SSimon Glass * Copyright (c) 2017 Google, Inc 5f11c7ab9SSimon Glass * Written by Simon Glass <sjg@chromium.org> 6f11c7ab9SSimon Glass * 7f11c7ab9SSimon Glass * SPDX-License-Identifier: GPL-2.0+ 8f11c7ab9SSimon Glass */ 9f11c7ab9SSimon Glass 10f11c7ab9SSimon Glass #ifndef _DM_READ_H 11f11c7ab9SSimon Glass #define _DM_READ_H 12f11c7ab9SSimon Glass 13f11c7ab9SSimon Glass #include <dm/fdtaddr.h> 14f11c7ab9SSimon Glass #include <dm/ofnode.h> 15f11c7ab9SSimon Glass #include <dm/uclass.h> 16f11c7ab9SSimon Glass 17a4481012SSimon Glass struct resource; 18a4481012SSimon Glass 19f11c7ab9SSimon Glass #if CONFIG_IS_ENABLED(OF_LIVE) 20f11c7ab9SSimon Glass static inline const struct device_node *dev_np(struct udevice *dev) 21f11c7ab9SSimon Glass { 22f11c7ab9SSimon Glass return ofnode_to_np(dev->node); 23f11c7ab9SSimon Glass } 24f11c7ab9SSimon Glass #else 25f11c7ab9SSimon Glass static inline const struct device_node *dev_np(struct udevice *dev) 26f11c7ab9SSimon Glass { 27f11c7ab9SSimon Glass return NULL; 28f11c7ab9SSimon Glass } 29f11c7ab9SSimon Glass #endif 30f11c7ab9SSimon Glass 31f11c7ab9SSimon Glass /** 32f11c7ab9SSimon Glass * dev_ofnode() - get the DT node reference associated with a udevice 33f11c7ab9SSimon Glass * 34f11c7ab9SSimon Glass * @dev: device to check 35f11c7ab9SSimon Glass * @return reference of the the device's DT node 36f11c7ab9SSimon Glass */ 37f11c7ab9SSimon Glass static inline ofnode dev_ofnode(struct udevice *dev) 38f11c7ab9SSimon Glass { 39f11c7ab9SSimon Glass return dev->node; 40f11c7ab9SSimon Glass } 41f11c7ab9SSimon Glass 42f11c7ab9SSimon Glass static inline bool dev_of_valid(struct udevice *dev) 43f11c7ab9SSimon Glass { 44f11c7ab9SSimon Glass return ofnode_valid(dev_ofnode(dev)); 45f11c7ab9SSimon Glass } 46f11c7ab9SSimon Glass 4747a0fd3bSSimon Glass #ifndef CONFIG_DM_DEV_READ_INLINE 4847a0fd3bSSimon Glass /** 4947a0fd3bSSimon Glass * dev_read_u32_default() - read a 32-bit integer from a device's DT property 5047a0fd3bSSimon Glass * 5147a0fd3bSSimon Glass * @dev: device to read DT property from 5247a0fd3bSSimon Glass * @propname: name of the property to read from 5347a0fd3bSSimon Glass * @def: default value to return if the property has no value 5447a0fd3bSSimon Glass * @return property value, or @def if not found 5547a0fd3bSSimon Glass */ 5647a0fd3bSSimon Glass int dev_read_u32_default(struct udevice *dev, const char *propname, int def); 57f11c7ab9SSimon Glass 58f11c7ab9SSimon Glass /** 59270d4d86SJoseph Chen * dev_read_s32_default() - read a signed 32-bit integer from a device's DT property 60270d4d86SJoseph Chen * 61270d4d86SJoseph Chen * @dev: device to read DT property from 62270d4d86SJoseph Chen * @propname: name of the property to read from 63270d4d86SJoseph Chen * @def: default value to return if the property has no value 64270d4d86SJoseph Chen * @return property value, or @def if not found 65270d4d86SJoseph Chen */ 66270d4d86SJoseph Chen int dev_read_s32_default(struct udevice *dev, const char *propname, int def); 67270d4d86SJoseph Chen 68270d4d86SJoseph Chen /** 69f11c7ab9SSimon Glass * dev_read_string() - Read a string from a device's DT property 70f11c7ab9SSimon Glass * 71f11c7ab9SSimon Glass * @dev: device to read DT property from 72f11c7ab9SSimon Glass * @propname: name of the property to read 73f11c7ab9SSimon Glass * @return string from property value, or NULL if there is no such property 74f11c7ab9SSimon Glass */ 7547a0fd3bSSimon Glass const char *dev_read_string(struct udevice *dev, const char *propname); 76f11c7ab9SSimon Glass 77f11c7ab9SSimon Glass /** 78f11c7ab9SSimon Glass * dev_read_bool() - read a boolean value from a device's DT property 79f11c7ab9SSimon Glass * 80f11c7ab9SSimon Glass * @dev: device to read DT property from 81f11c7ab9SSimon Glass * @propname: name of property to read 82f11c7ab9SSimon Glass * @return true if property is present (meaning true), false if not present 83f11c7ab9SSimon Glass */ 8447a0fd3bSSimon Glass bool dev_read_bool(struct udevice *dev, const char *propname); 85f11c7ab9SSimon Glass 86f11c7ab9SSimon Glass /** 87f11c7ab9SSimon Glass * dev_read_subnode() - find a named subnode of a device 88f11c7ab9SSimon Glass * 89f11c7ab9SSimon Glass * @dev: device whose DT node contains the subnode 90f11c7ab9SSimon Glass * @subnode_name: name of subnode to find 91f11c7ab9SSimon Glass * @return reference to subnode (which can be invalid if there is no such 92f11c7ab9SSimon Glass * subnode) 93f11c7ab9SSimon Glass */ 9447a0fd3bSSimon Glass ofnode dev_read_subnode(struct udevice *dev, const char *subbnode_name); 95f11c7ab9SSimon Glass 96f11c7ab9SSimon Glass /** 97f11c7ab9SSimon Glass * dev_read_size() - read the size of a property 98f11c7ab9SSimon Glass * 99f11c7ab9SSimon Glass * @dev: device to check 100f11c7ab9SSimon Glass * @propname: property to check 101f11c7ab9SSimon Glass * @return size of property if present, or -EINVAL if not 102f11c7ab9SSimon Glass */ 10347a0fd3bSSimon Glass int dev_read_size(struct udevice *dev, const char *propname); 104f11c7ab9SSimon Glass 105f11c7ab9SSimon Glass /** 106f11c7ab9SSimon Glass * dev_read_addr_index() - Get the indexed reg property of a device 107f11c7ab9SSimon Glass * 108f11c7ab9SSimon Glass * @dev: Device to read from 109f11c7ab9SSimon Glass * @index: the 'reg' property can hold a list of <addr, size> pairs 110f11c7ab9SSimon Glass * and @index is used to select which one is required 111f11c7ab9SSimon Glass * 112f11c7ab9SSimon Glass * @return address or FDT_ADDR_T_NONE if not found 113f11c7ab9SSimon Glass */ 11447a0fd3bSSimon Glass fdt_addr_t dev_read_addr_index(struct udevice *dev, int index); 115f11c7ab9SSimon Glass 116f11c7ab9SSimon Glass /** 117f11c7ab9SSimon Glass * dev_read_addr() - Get the reg property of a device 118f11c7ab9SSimon Glass * 119f11c7ab9SSimon Glass * @dev: Device to read from 120f11c7ab9SSimon Glass * 121f11c7ab9SSimon Glass * @return address or FDT_ADDR_T_NONE if not found 122f11c7ab9SSimon Glass */ 12347a0fd3bSSimon Glass fdt_addr_t dev_read_addr(struct udevice *dev); 124f11c7ab9SSimon Glass 125f11c7ab9SSimon Glass /** 12618a0c4a6SPhilipp Tomsich * dev_read_addr_ptr() - Get the reg property of a device 12718a0c4a6SPhilipp Tomsich * as a pointer 12818a0c4a6SPhilipp Tomsich * 12918a0c4a6SPhilipp Tomsich * @dev: Device to read from 13018a0c4a6SPhilipp Tomsich * 13118a0c4a6SPhilipp Tomsich * @return pointer or NULL if not found 13218a0c4a6SPhilipp Tomsich */ 13318a0c4a6SPhilipp Tomsich void *dev_read_addr_ptr(struct udevice *dev); 13418a0c4a6SPhilipp Tomsich 13518a0c4a6SPhilipp Tomsich /** 136f11c7ab9SSimon Glass * dev_read_addr_size() - get address and size from a device property 137f11c7ab9SSimon Glass * 138f11c7ab9SSimon Glass * This does no address translation. It simply reads an property that contains 139f11c7ab9SSimon Glass * an address and a size value, one after the other. 140f11c7ab9SSimon Glass * 141f11c7ab9SSimon Glass * @dev: Device to read from 142f11c7ab9SSimon Glass * @propname: property to read 143f11c7ab9SSimon Glass * @sizep: place to put size value (on success) 144f11c7ab9SSimon Glass * @return address value, or FDT_ADDR_T_NONE on error 145f11c7ab9SSimon Glass */ 14647a0fd3bSSimon Glass fdt_addr_t dev_read_addr_size(struct udevice *dev, const char *propname, 14747a0fd3bSSimon Glass fdt_size_t *sizep); 148f11c7ab9SSimon Glass 149f11c7ab9SSimon Glass /** 150f11c7ab9SSimon Glass * dev_read_name() - get the name of a device's node 151f11c7ab9SSimon Glass * 152f11c7ab9SSimon Glass * @node: valid node to look up 153f11c7ab9SSimon Glass * @return name of node 154f11c7ab9SSimon Glass */ 15547a0fd3bSSimon Glass const char *dev_read_name(struct udevice *dev); 156f11c7ab9SSimon Glass 157f11c7ab9SSimon Glass /** 158f11c7ab9SSimon Glass * dev_read_stringlist_search() - find string in a string list and return index 159f11c7ab9SSimon Glass * 160f11c7ab9SSimon Glass * Note that it is possible for this function to succeed on property values 161f11c7ab9SSimon Glass * that are not NUL-terminated. That's because the function will stop after 162f11c7ab9SSimon Glass * finding the first occurrence of @string. This can for example happen with 163f11c7ab9SSimon Glass * small-valued cell properties, such as #address-cells, when searching for 164f11c7ab9SSimon Glass * the empty string. 165f11c7ab9SSimon Glass * 166f11c7ab9SSimon Glass * @dev: device to check 167f11c7ab9SSimon Glass * @propname: name of the property containing the string list 168f11c7ab9SSimon Glass * @string: string to look up in the string list 169f11c7ab9SSimon Glass * 170f11c7ab9SSimon Glass * @return: 171f11c7ab9SSimon Glass * the index of the string in the list of strings 172f11c7ab9SSimon Glass * -ENODATA if the property is not found 173f11c7ab9SSimon Glass * -EINVAL on some other error 174f11c7ab9SSimon Glass */ 17547a0fd3bSSimon Glass int dev_read_stringlist_search(struct udevice *dev, const char *property, 17647a0fd3bSSimon Glass const char *string); 177f11c7ab9SSimon Glass 178f11c7ab9SSimon Glass /** 17957a9c706SJean-Jacques Hiblot * dev_read_string_index() - obtain an indexed string from a string list 18057a9c706SJean-Jacques Hiblot * 18157a9c706SJean-Jacques Hiblot * @dev: device to examine 18257a9c706SJean-Jacques Hiblot * @propname: name of the property containing the string list 18357a9c706SJean-Jacques Hiblot * @index: index of the string to return 18457a9c706SJean-Jacques Hiblot * @out: return location for the string 18557a9c706SJean-Jacques Hiblot * 18657a9c706SJean-Jacques Hiblot * @return: 18757a9c706SJean-Jacques Hiblot * length of string, if found or -ve error value if not found 18857a9c706SJean-Jacques Hiblot */ 18957a9c706SJean-Jacques Hiblot int dev_read_string_index(struct udevice *dev, const char *propname, int index, 19057a9c706SJean-Jacques Hiblot const char **outp); 19157a9c706SJean-Jacques Hiblot 19257a9c706SJean-Jacques Hiblot /** 19357a9c706SJean-Jacques Hiblot * dev_read_string_count() - find the number of strings in a string list 19457a9c706SJean-Jacques Hiblot * 19557a9c706SJean-Jacques Hiblot * @dev: device to examine 19657a9c706SJean-Jacques Hiblot * @propname: name of the property containing the string list 19757a9c706SJean-Jacques Hiblot * @return: 19857a9c706SJean-Jacques Hiblot * number of strings in the list, or -ve error value if not found 19957a9c706SJean-Jacques Hiblot */ 20057a9c706SJean-Jacques Hiblot int dev_read_string_count(struct udevice *dev, const char *propname); 20157a9c706SJean-Jacques Hiblot /** 202f11c7ab9SSimon Glass * dev_read_phandle_with_args() - Find a node pointed by phandle in a list 203f11c7ab9SSimon Glass * 204f11c7ab9SSimon Glass * This function is useful to parse lists of phandles and their arguments. 205f11c7ab9SSimon Glass * Returns 0 on success and fills out_args, on error returns appropriate 206f11c7ab9SSimon Glass * errno value. 207f11c7ab9SSimon Glass * 208f11c7ab9SSimon Glass * Caller is responsible to call of_node_put() on the returned out_args->np 209f11c7ab9SSimon Glass * pointer. 210f11c7ab9SSimon Glass * 211f11c7ab9SSimon Glass * Example: 212f11c7ab9SSimon Glass * 213f11c7ab9SSimon Glass * phandle1: node1 { 214f11c7ab9SSimon Glass * #list-cells = <2>; 215f11c7ab9SSimon Glass * } 216f11c7ab9SSimon Glass * 217f11c7ab9SSimon Glass * phandle2: node2 { 218f11c7ab9SSimon Glass * #list-cells = <1>; 219f11c7ab9SSimon Glass * } 220f11c7ab9SSimon Glass * 221f11c7ab9SSimon Glass * node3 { 222f11c7ab9SSimon Glass * list = <&phandle1 1 2 &phandle2 3>; 223f11c7ab9SSimon Glass * } 224f11c7ab9SSimon Glass * 225f11c7ab9SSimon Glass * To get a device_node of the `node2' node you may call this: 226f11c7ab9SSimon Glass * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args); 227f11c7ab9SSimon Glass * 228f11c7ab9SSimon Glass * @dev: device whose node containing a list 229f11c7ab9SSimon Glass * @list_name: property name that contains a list 230f11c7ab9SSimon Glass * @cells_name: property name that specifies phandles' arguments count 231f11c7ab9SSimon Glass * @cells_count: Cell count to use if @cells_name is NULL 232f11c7ab9SSimon Glass * @index: index of a phandle to parse out 233f11c7ab9SSimon Glass * @out_args: optional pointer to output arguments structure (will be filled) 234f11c7ab9SSimon Glass * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if 235f11c7ab9SSimon Glass * @list_name does not exist, -EINVAL if a phandle was not found, 236f11c7ab9SSimon Glass * @cells_name could not be found, the arguments were truncated or there 237f11c7ab9SSimon Glass * were too many arguments. 238f11c7ab9SSimon Glass */ 23947a0fd3bSSimon Glass int dev_read_phandle_with_args(struct udevice *dev, const char *list_name, 24047a0fd3bSSimon Glass const char *cells_name, int cell_count, 24147a0fd3bSSimon Glass int index, 24247a0fd3bSSimon Glass struct ofnode_phandle_args *out_args); 243f11c7ab9SSimon Glass 244f11c7ab9SSimon Glass /** 245642346aeSPatrice Chotard * dev_count_phandle_with_args() - Return phandle number in a list 246642346aeSPatrice Chotard * 247642346aeSPatrice Chotard * This function is usefull to get phandle number contained in a property list. 248642346aeSPatrice Chotard * For example, this allows to allocate the right amount of memory to keep 249642346aeSPatrice Chotard * clock's reference contained into the "clocks" property. 250642346aeSPatrice Chotard * 251642346aeSPatrice Chotard * 252642346aeSPatrice Chotard * @dev: device whose node containing a list 253642346aeSPatrice Chotard * @list_name: property name that contains a list 254642346aeSPatrice Chotard * @cells_name: property name that specifies phandles' arguments count 255642346aeSPatrice Chotard * @Returns number of phandle found on success, on error returns appropriate 256642346aeSPatrice Chotard * errno value. 257642346aeSPatrice Chotard */ 258642346aeSPatrice Chotard 259642346aeSPatrice Chotard int dev_count_phandle_with_args(struct udevice *dev, const char *list_name, 260642346aeSPatrice Chotard const char *cells_name); 261642346aeSPatrice Chotard 262642346aeSPatrice Chotard /** 263f11c7ab9SSimon Glass * dev_read_addr_cells() - Get the number of address cells for a device's node 264f11c7ab9SSimon Glass * 265f11c7ab9SSimon Glass * This walks back up the tree to find the closest #address-cells property 266f11c7ab9SSimon Glass * which controls the given node. 267f11c7ab9SSimon Glass * 268f11c7ab9SSimon Glass * @dev: devioe to check 269f11c7ab9SSimon Glass * @return number of address cells this node uses 270f11c7ab9SSimon Glass */ 27147a0fd3bSSimon Glass int dev_read_addr_cells(struct udevice *dev); 272f11c7ab9SSimon Glass 273f11c7ab9SSimon Glass /** 274f11c7ab9SSimon Glass * dev_read_size_cells() - Get the number of size cells for a device's node 275f11c7ab9SSimon Glass * 276f11c7ab9SSimon Glass * This walks back up the tree to find the closest #size-cells property 277f11c7ab9SSimon Glass * which controls the given node. 278f11c7ab9SSimon Glass * 279f11c7ab9SSimon Glass * @dev: devioe to check 280f11c7ab9SSimon Glass * @return number of size cells this node uses 281f11c7ab9SSimon Glass */ 28247a0fd3bSSimon Glass int dev_read_size_cells(struct udevice *dev); 283f11c7ab9SSimon Glass 284f11c7ab9SSimon Glass /** 285878d68c0SSimon Glass * dev_read_addr_cells() - Get the address cells property in a node 286878d68c0SSimon Glass * 287878d68c0SSimon Glass * This function matches fdt_address_cells(). 288878d68c0SSimon Glass * 289878d68c0SSimon Glass * @dev: devioe to check 290878d68c0SSimon Glass * @return number of address cells this node uses 291878d68c0SSimon Glass */ 292878d68c0SSimon Glass int dev_read_simple_addr_cells(struct udevice *dev); 293878d68c0SSimon Glass 294878d68c0SSimon Glass /** 295878d68c0SSimon Glass * dev_read_size_cells() - Get the size cells property in a node 296878d68c0SSimon Glass * 297878d68c0SSimon Glass * This function matches fdt_size_cells(). 298878d68c0SSimon Glass * 299878d68c0SSimon Glass * @dev: devioe to check 300878d68c0SSimon Glass * @return number of size cells this node uses 301878d68c0SSimon Glass */ 302878d68c0SSimon Glass int dev_read_simple_size_cells(struct udevice *dev); 303878d68c0SSimon Glass 304878d68c0SSimon Glass /** 305f11c7ab9SSimon Glass * dev_read_phandle() - Get the phandle from a device 306f11c7ab9SSimon Glass * 307f11c7ab9SSimon Glass * @dev: device to check 308f11c7ab9SSimon Glass * @return phandle (1 or greater), or 0 if no phandle or other error 309f11c7ab9SSimon Glass */ 31047a0fd3bSSimon Glass int dev_read_phandle(struct udevice *dev); 311f11c7ab9SSimon Glass 312f11c7ab9SSimon Glass /** 313f11c7ab9SSimon Glass * dev_read_prop()- - read a property from a device's node 314f11c7ab9SSimon Glass * 315f11c7ab9SSimon Glass * @dev: device to check 316f11c7ab9SSimon Glass * @propname: property to read 317f11c7ab9SSimon Glass * @lenp: place to put length on success 318f11c7ab9SSimon Glass * @return pointer to property, or NULL if not found 319f11c7ab9SSimon Glass */ 320fd73621cSMasahiro Yamada const void *dev_read_prop(struct udevice *dev, const char *propname, int *lenp); 321f11c7ab9SSimon Glass 322f11c7ab9SSimon Glass /** 323f11c7ab9SSimon Glass * dev_read_alias_seq() - Get the alias sequence number of a node 324f11c7ab9SSimon Glass * 325f11c7ab9SSimon Glass * This works out whether a node is pointed to by an alias, and if so, the 326f11c7ab9SSimon Glass * sequence number of that alias. Aliases are of the form <base><num> where 327f11c7ab9SSimon Glass * <num> is the sequence number. For example spi2 would be sequence number 2. 328f11c7ab9SSimon Glass * 329f11c7ab9SSimon Glass * @dev: device to look up 330f11c7ab9SSimon Glass * @devnump: set to the sequence number if one is found 331f11c7ab9SSimon Glass * @return 0 if a sequence was found, -ve if not 332f11c7ab9SSimon Glass */ 33347a0fd3bSSimon Glass int dev_read_alias_seq(struct udevice *dev, int *devnump); 334f11c7ab9SSimon Glass 335f11c7ab9SSimon Glass /** 336f11c7ab9SSimon Glass * dev_read_u32_array() - Find and read an array of 32 bit integers 337f11c7ab9SSimon Glass * 338f11c7ab9SSimon Glass * Search for a property in a device node and read 32-bit value(s) from 339f11c7ab9SSimon Glass * it. 340f11c7ab9SSimon Glass * 341f11c7ab9SSimon Glass * The out_values is modified only if a valid u32 value can be decoded. 342f11c7ab9SSimon Glass * 343f11c7ab9SSimon Glass * @dev: device to look up 344f11c7ab9SSimon Glass * @propname: name of the property to read 345f11c7ab9SSimon Glass * @out_values: pointer to return value, modified only if return value is 0 346f11c7ab9SSimon Glass * @sz: number of array elements to read 347f11c7ab9SSimon Glass * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if 348f11c7ab9SSimon Glass * property does not have a value, and -EOVERFLOW if the property data isn't 349f11c7ab9SSimon Glass * large enough. 350f11c7ab9SSimon Glass */ 35147a0fd3bSSimon Glass int dev_read_u32_array(struct udevice *dev, const char *propname, 35247a0fd3bSSimon Glass u32 *out_values, size_t sz); 353f11c7ab9SSimon Glass 354f11c7ab9SSimon Glass /** 35504539b46SJoseph Chen * dev_write_u32_array() - Find and write an array of 32 bit integers 35604539b46SJoseph Chen * 35704539b46SJoseph Chen * Search for a property in a device node and write 32-bit value(s) to 35804539b46SJoseph Chen * it. 35904539b46SJoseph Chen * 36004539b46SJoseph Chen * The out_values is modified only if a valid u32 value can be decoded. 36104539b46SJoseph Chen * 36204539b46SJoseph Chen * @dev: device to look up 36304539b46SJoseph Chen * @propname: name of the property to read 36404539b46SJoseph Chen * @values: pointer to update value, modified only if return value is 0 36504539b46SJoseph Chen * @sz: number of array elements to read 36604539b46SJoseph Chen * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if 36704539b46SJoseph Chen * property does not have a value, and -EOVERFLOW if the property data isn't 36804539b46SJoseph Chen * large enough. 36904539b46SJoseph Chen */ 37004539b46SJoseph Chen int dev_write_u32_array(struct udevice *dev, const char *propname, 37104539b46SJoseph Chen u32 *values, size_t sz); 37204539b46SJoseph Chen 37304539b46SJoseph Chen /** 374f11c7ab9SSimon Glass * dev_read_first_subnode() - find the first subnode of a device's node 375f11c7ab9SSimon Glass * 376f11c7ab9SSimon Glass * @dev: device to look up 377f11c7ab9SSimon Glass * @return reference to the first subnode (which can be invalid if the device's 378f11c7ab9SSimon Glass * node has no subnodes) 379f11c7ab9SSimon Glass */ 38047a0fd3bSSimon Glass ofnode dev_read_first_subnode(struct udevice *dev); 381f11c7ab9SSimon Glass 382f11c7ab9SSimon Glass /** 383f11c7ab9SSimon Glass * ofnode_next_subnode() - find the next sibling of a subnode 384f11c7ab9SSimon Glass * 385f11c7ab9SSimon Glass * @node: valid reference to previous node (sibling) 386f11c7ab9SSimon Glass * @return reference to the next subnode (which can be invalid if the node 387f11c7ab9SSimon Glass * has no more siblings) 388f11c7ab9SSimon Glass */ 38947a0fd3bSSimon Glass ofnode dev_read_next_subnode(ofnode node); 390f11c7ab9SSimon Glass 391f11c7ab9SSimon Glass /** 392f11c7ab9SSimon Glass * dev_read_u8_array_ptr() - find an 8-bit array 393f11c7ab9SSimon Glass * 394f11c7ab9SSimon Glass * Look up a device's node property and return a pointer to its contents as a 395f11c7ab9SSimon Glass * byte array of given length. The property must have at least enough data 396f11c7ab9SSimon Glass * for the array (count bytes). It may have more, but this will be ignored. 397f11c7ab9SSimon Glass * The data is not copied. 398f11c7ab9SSimon Glass * 399f11c7ab9SSimon Glass * @dev: device to look up 400f11c7ab9SSimon Glass * @propname: name of property to find 401f11c7ab9SSimon Glass * @sz: number of array elements 402f11c7ab9SSimon Glass * @return pointer to byte array if found, or NULL if the property is not 403f11c7ab9SSimon Glass * found or there is not enough data 404f11c7ab9SSimon Glass */ 40547a0fd3bSSimon Glass const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, const char *propname, 40647a0fd3bSSimon Glass size_t sz); 40747a0fd3bSSimon Glass 408f7d6fcf7SSimon Glass /** 409f7d6fcf7SSimon Glass * dev_read_enabled() - check whether a node is enabled 410f7d6fcf7SSimon Glass * 411f7d6fcf7SSimon Glass * This looks for a 'status' property. If this exists, then returns 1 if 412f7d6fcf7SSimon Glass * the status is 'ok' and 0 otherwise. If there is no status property, 413f7d6fcf7SSimon Glass * it returns 1 on the assumption that anything mentioned should be enabled 414f7d6fcf7SSimon Glass * by default. 415f7d6fcf7SSimon Glass * 416f7d6fcf7SSimon Glass * @dev: device to examine 417f7d6fcf7SSimon Glass * @return integer value 0 (not enabled) or 1 (enabled) 418f7d6fcf7SSimon Glass */ 419f7d6fcf7SSimon Glass int dev_read_enabled(struct udevice *dev); 420f7d6fcf7SSimon Glass 421dcf98852SSimon Glass /** 422dcf98852SSimon Glass * dev_read_resource() - obtain an indexed resource from a device. 423dcf98852SSimon Glass * 4247b8b47bdSMasahiro Yamada * @dev: device to examine 425dcf98852SSimon Glass * @index index of the resource to retrieve (0 = first) 426dcf98852SSimon Glass * @res returns the resource 427dcf98852SSimon Glass * @return 0 if ok, negative on error 428dcf98852SSimon Glass */ 429dcf98852SSimon Glass int dev_read_resource(struct udevice *dev, uint index, struct resource *res); 430dcf98852SSimon Glass 4317b8b47bdSMasahiro Yamada /** 4327b8b47bdSMasahiro Yamada * dev_read_resource_byname() - obtain a named resource from a device. 4337b8b47bdSMasahiro Yamada * 4347b8b47bdSMasahiro Yamada * @dev: device to examine 4357b8b47bdSMasahiro Yamada * @name: name of the resource to retrieve 4367b8b47bdSMasahiro Yamada * @res: returns the resource 4377b8b47bdSMasahiro Yamada * @return 0 if ok, negative on error 4387b8b47bdSMasahiro Yamada */ 4397b8b47bdSMasahiro Yamada int dev_read_resource_byname(struct udevice *dev, const char *name, 4407b8b47bdSMasahiro Yamada struct resource *res); 4417b8b47bdSMasahiro Yamada 442*a9bd1c73SMario Six /** 443*a9bd1c73SMario Six * dev_translate_address() - Tranlate a device-tree address 444*a9bd1c73SMario Six * 445*a9bd1c73SMario Six * Translate an address from the device-tree into a CPU physical address. This 446*a9bd1c73SMario Six * function walks up the tree and applies the various bus mappings along the 447*a9bd1c73SMario Six * way. 448*a9bd1c73SMario Six * 449*a9bd1c73SMario Six * @dev: device giving the context in which to translate the address 450*a9bd1c73SMario Six * @in_addr: pointer to the address to translate 451*a9bd1c73SMario Six * @return the translated address; OF_BAD_ADDR on error 452*a9bd1c73SMario Six */ 453*a9bd1c73SMario Six u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr); 45447a0fd3bSSimon Glass #else /* CONFIG_DM_DEV_READ_INLINE is enabled */ 45547a0fd3bSSimon Glass 45647a0fd3bSSimon Glass static inline int dev_read_u32_default(struct udevice *dev, 45747a0fd3bSSimon Glass const char *propname, int def) 45847a0fd3bSSimon Glass { 45947a0fd3bSSimon Glass return ofnode_read_u32_default(dev_ofnode(dev), propname, def); 46047a0fd3bSSimon Glass } 46147a0fd3bSSimon Glass 46247a0fd3bSSimon Glass static inline const char *dev_read_string(struct udevice *dev, 46347a0fd3bSSimon Glass const char *propname) 46447a0fd3bSSimon Glass { 46547a0fd3bSSimon Glass return ofnode_read_string(dev_ofnode(dev), propname); 46647a0fd3bSSimon Glass } 46747a0fd3bSSimon Glass 46847a0fd3bSSimon Glass static inline bool dev_read_bool(struct udevice *dev, const char *propname) 46947a0fd3bSSimon Glass { 47047a0fd3bSSimon Glass return ofnode_read_bool(dev_ofnode(dev), propname); 47147a0fd3bSSimon Glass } 47247a0fd3bSSimon Glass 47347a0fd3bSSimon Glass static inline ofnode dev_read_subnode(struct udevice *dev, 47447a0fd3bSSimon Glass const char *subbnode_name) 47547a0fd3bSSimon Glass { 47647a0fd3bSSimon Glass return ofnode_find_subnode(dev_ofnode(dev), subbnode_name); 47747a0fd3bSSimon Glass } 47847a0fd3bSSimon Glass 47947a0fd3bSSimon Glass static inline int dev_read_size(struct udevice *dev, const char *propname) 48047a0fd3bSSimon Glass { 48147a0fd3bSSimon Glass return ofnode_read_size(dev_ofnode(dev), propname); 48247a0fd3bSSimon Glass } 48347a0fd3bSSimon Glass 48447a0fd3bSSimon Glass static inline fdt_addr_t dev_read_addr_index(struct udevice *dev, int index) 48547a0fd3bSSimon Glass { 48647a0fd3bSSimon Glass return devfdt_get_addr_index(dev, index); 48747a0fd3bSSimon Glass } 48847a0fd3bSSimon Glass 48947a0fd3bSSimon Glass static inline fdt_addr_t dev_read_addr(struct udevice *dev) 49047a0fd3bSSimon Glass { 49147a0fd3bSSimon Glass return devfdt_get_addr(dev); 49247a0fd3bSSimon Glass } 49347a0fd3bSSimon Glass 49418a0c4a6SPhilipp Tomsich static inline void *dev_read_addr_ptr(struct udevice *dev) 49518a0c4a6SPhilipp Tomsich { 49618a0c4a6SPhilipp Tomsich return devfdt_get_addr_ptr(dev); 49718a0c4a6SPhilipp Tomsich } 49818a0c4a6SPhilipp Tomsich 49947a0fd3bSSimon Glass static inline fdt_addr_t dev_read_addr_size(struct udevice *dev, 50047a0fd3bSSimon Glass const char *propname, 50147a0fd3bSSimon Glass fdt_size_t *sizep) 50247a0fd3bSSimon Glass { 50347a0fd3bSSimon Glass return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep); 50447a0fd3bSSimon Glass } 50547a0fd3bSSimon Glass 50647a0fd3bSSimon Glass static inline const char *dev_read_name(struct udevice *dev) 50747a0fd3bSSimon Glass { 508b283d2aeSKever Yang if (!dev_of_valid(dev)) 509b283d2aeSKever Yang return NULL; 51047a0fd3bSSimon Glass return ofnode_get_name(dev_ofnode(dev)); 51147a0fd3bSSimon Glass } 51247a0fd3bSSimon Glass 51347a0fd3bSSimon Glass static inline int dev_read_stringlist_search(struct udevice *dev, 51447a0fd3bSSimon Glass const char *propname, 51547a0fd3bSSimon Glass const char *string) 51647a0fd3bSSimon Glass { 51747a0fd3bSSimon Glass return ofnode_stringlist_search(dev_ofnode(dev), propname, string); 51847a0fd3bSSimon Glass } 51947a0fd3bSSimon Glass 52057a9c706SJean-Jacques Hiblot static inline int dev_read_string_index(struct udevice *dev, 52157a9c706SJean-Jacques Hiblot const char *propname, int index, 52257a9c706SJean-Jacques Hiblot const char **outp) 52357a9c706SJean-Jacques Hiblot { 52457a9c706SJean-Jacques Hiblot return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp); 52557a9c706SJean-Jacques Hiblot } 52657a9c706SJean-Jacques Hiblot 52757a9c706SJean-Jacques Hiblot static inline int dev_read_string_count(struct udevice *dev, 52857a9c706SJean-Jacques Hiblot const char *propname) 52957a9c706SJean-Jacques Hiblot { 53057a9c706SJean-Jacques Hiblot return ofnode_read_string_count(dev_ofnode(dev), propname); 53157a9c706SJean-Jacques Hiblot } 53257a9c706SJean-Jacques Hiblot 53347a0fd3bSSimon Glass static inline int dev_read_phandle_with_args(struct udevice *dev, 53447a0fd3bSSimon Glass const char *list_name, const char *cells_name, int cell_count, 53547a0fd3bSSimon Glass int index, struct ofnode_phandle_args *out_args) 53647a0fd3bSSimon Glass { 53747a0fd3bSSimon Glass return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name, 53847a0fd3bSSimon Glass cells_name, cell_count, index, 53947a0fd3bSSimon Glass out_args); 54047a0fd3bSSimon Glass } 54147a0fd3bSSimon Glass 542642346aeSPatrice Chotard static inline int dev_count_phandle_with_args(struct udevice *dev, 543642346aeSPatrice Chotard const char *list_name, const char *cells_name) 544642346aeSPatrice Chotard { 545642346aeSPatrice Chotard return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name, 546642346aeSPatrice Chotard cells_name); 547642346aeSPatrice Chotard } 548642346aeSPatrice Chotard 54947a0fd3bSSimon Glass static inline int dev_read_addr_cells(struct udevice *dev) 55047a0fd3bSSimon Glass { 551878d68c0SSimon Glass /* NOTE: this call should walk up the parent stack */ 55247a0fd3bSSimon Glass return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev)); 55347a0fd3bSSimon Glass } 55447a0fd3bSSimon Glass 55547a0fd3bSSimon Glass static inline int dev_read_size_cells(struct udevice *dev) 55647a0fd3bSSimon Glass { 557878d68c0SSimon Glass /* NOTE: this call should walk up the parent stack */ 558878d68c0SSimon Glass return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev)); 559878d68c0SSimon Glass } 560878d68c0SSimon Glass 561878d68c0SSimon Glass static inline int dev_read_simple_addr_cells(struct udevice *dev) 562878d68c0SSimon Glass { 563878d68c0SSimon Glass return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev)); 564878d68c0SSimon Glass } 565878d68c0SSimon Glass 566878d68c0SSimon Glass static inline int dev_read_simple_size_cells(struct udevice *dev) 567878d68c0SSimon Glass { 56847a0fd3bSSimon Glass return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev)); 56947a0fd3bSSimon Glass } 57047a0fd3bSSimon Glass 57147a0fd3bSSimon Glass static inline int dev_read_phandle(struct udevice *dev) 57247a0fd3bSSimon Glass { 57347a0fd3bSSimon Glass return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev)); 57447a0fd3bSSimon Glass } 57547a0fd3bSSimon Glass 576fd73621cSMasahiro Yamada static inline const void *dev_read_prop(struct udevice *dev, 57747a0fd3bSSimon Glass const char *propname, int *lenp) 57847a0fd3bSSimon Glass { 57961e51babSMasahiro Yamada return ofnode_get_property(dev_ofnode(dev), propname, lenp); 58047a0fd3bSSimon Glass } 58147a0fd3bSSimon Glass 58247a0fd3bSSimon Glass static inline int dev_read_alias_seq(struct udevice *dev, int *devnump) 58347a0fd3bSSimon Glass { 58447a0fd3bSSimon Glass return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name, 58547a0fd3bSSimon Glass dev_of_offset(dev), devnump); 58647a0fd3bSSimon Glass } 58747a0fd3bSSimon Glass 58847a0fd3bSSimon Glass static inline int dev_read_u32_array(struct udevice *dev, const char *propname, 58947a0fd3bSSimon Glass u32 *out_values, size_t sz) 59047a0fd3bSSimon Glass { 591b283d2aeSKever Yang if (!dev_of_valid(dev)) 592b283d2aeSKever Yang return -EINVAL; 59347a0fd3bSSimon Glass return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz); 59447a0fd3bSSimon Glass } 59547a0fd3bSSimon Glass 59647a0fd3bSSimon Glass static inline ofnode dev_read_first_subnode(struct udevice *dev) 59747a0fd3bSSimon Glass { 59847a0fd3bSSimon Glass return ofnode_first_subnode(dev_ofnode(dev)); 59947a0fd3bSSimon Glass } 60047a0fd3bSSimon Glass 60147a0fd3bSSimon Glass static inline ofnode dev_read_next_subnode(ofnode node) 60247a0fd3bSSimon Glass { 60347a0fd3bSSimon Glass return ofnode_next_subnode(node); 60447a0fd3bSSimon Glass } 60547a0fd3bSSimon Glass 606f11c7ab9SSimon Glass static inline const uint8_t *dev_read_u8_array_ptr(struct udevice *dev, 607f11c7ab9SSimon Glass const char *propname, size_t sz) 608f11c7ab9SSimon Glass { 609f11c7ab9SSimon Glass return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz); 610f11c7ab9SSimon Glass } 611f11c7ab9SSimon Glass 612f7d6fcf7SSimon Glass static inline int dev_read_enabled(struct udevice *dev) 613f7d6fcf7SSimon Glass { 614f7d6fcf7SSimon Glass return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev)); 615f7d6fcf7SSimon Glass } 616f7d6fcf7SSimon Glass 617dcf98852SSimon Glass static inline int dev_read_resource(struct udevice *dev, uint index, 618dcf98852SSimon Glass struct resource *res) 619dcf98852SSimon Glass { 620dcf98852SSimon Glass return ofnode_read_resource(dev_ofnode(dev), index, res); 621dcf98852SSimon Glass } 622dcf98852SSimon Glass 6237b8b47bdSMasahiro Yamada static inline int dev_read_resource_byname(struct udevice *dev, 6247b8b47bdSMasahiro Yamada const char *name, 6257b8b47bdSMasahiro Yamada struct resource *res) 6267b8b47bdSMasahiro Yamada { 6277b8b47bdSMasahiro Yamada return ofnode_read_resource_byname(dev_ofnode(dev), name, res); 6287b8b47bdSMasahiro Yamada } 6297b8b47bdSMasahiro Yamada 630*a9bd1c73SMario Six static inline u64 dev_translate_address(struct udevice *dev, const fdt32_t *in_addr) 631*a9bd1c73SMario Six { 632*a9bd1c73SMario Six return ofnode_translate_address(dev_ofnode(dev), in_addr); 633*a9bd1c73SMario Six } 634*a9bd1c73SMario Six 635f11c7ab9SSimon Glass #endif /* CONFIG_DM_DEV_READ_INLINE */ 636f11c7ab9SSimon Glass 637f11c7ab9SSimon Glass /** 638f11c7ab9SSimon Glass * dev_for_each_subnode() - Helper function to iterate through subnodes 639f11c7ab9SSimon Glass * 640f11c7ab9SSimon Glass * This creates a for() loop which works through the subnodes in a device's 641f11c7ab9SSimon Glass * device-tree node. 642f11c7ab9SSimon Glass * 643f11c7ab9SSimon Glass * @subnode: ofnode holding the current subnode 644f11c7ab9SSimon Glass * @dev: device to use for interation (struct udevice *) 645f11c7ab9SSimon Glass */ 646f11c7ab9SSimon Glass #define dev_for_each_subnode(subnode, dev) \ 647f11c7ab9SSimon Glass for (subnode = dev_read_first_subnode(dev); \ 648f11c7ab9SSimon Glass ofnode_valid(subnode); \ 649f11c7ab9SSimon Glass subnode = ofnode_next_subnode(subnode)) 650f11c7ab9SSimon Glass 651f11c7ab9SSimon Glass #endif 652