Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Function to read values from the device tree node attached to a udevice. |
| 4 | * |
| 5 | * Copyright (c) 2017 Google, Inc |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #ifndef _DM_READ_H |
| 10 | #define _DM_READ_H |
| 11 | |
Dan Murphy | d3aa90e | 2020-07-23 07:01:38 -0500 | [diff] [blame] | 12 | #include <linux/errno.h> |
| 13 | |
Simon Glass | 992b603 | 2020-07-19 10:15:39 -0600 | [diff] [blame] | 14 | #include <dm/device.h> |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 15 | #include <dm/fdtaddr.h> |
| 16 | #include <dm/ofnode.h> |
| 17 | #include <dm/uclass.h> |
| 18 | |
Simon Glass | 170e17c | 2017-06-12 06:21:29 -0600 | [diff] [blame] | 19 | struct resource; |
| 20 | |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 21 | #if CONFIG_IS_ENABLED(OF_LIVE) |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 22 | static inline const struct device_node *dev_np(const struct udevice *dev) |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 23 | { |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 24 | return ofnode_to_np(dev_ofnode(dev)); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 25 | } |
| 26 | #else |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 27 | static inline const struct device_node *dev_np(const struct udevice *dev) |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 28 | { |
| 29 | return NULL; |
| 30 | } |
| 31 | #endif |
| 32 | |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 33 | #ifndef CONFIG_DM_DEV_READ_INLINE |
T Karthik Reddy | 478860d | 2019-09-02 16:34:30 +0200 | [diff] [blame] | 34 | |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 35 | /** |
Masahiro Yamada | 71d115f | 2017-12-30 02:00:05 +0900 | [diff] [blame] | 36 | * dev_read_u32() - read a 32-bit integer from a device's DT property |
| 37 | * |
| 38 | * @dev: device to read DT property from |
| 39 | * @propname: name of the property to read from |
| 40 | * @outp: place to put value (if found) |
| 41 | * @return 0 if OK, -ve on error |
| 42 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 43 | int dev_read_u32(const struct udevice *dev, const char *propname, u32 *outp); |
Masahiro Yamada | 71d115f | 2017-12-30 02:00:05 +0900 | [diff] [blame] | 44 | |
| 45 | /** |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 46 | * dev_read_u32_default() - read a 32-bit integer from a device's DT property |
| 47 | * |
| 48 | * @dev: device to read DT property from |
| 49 | * @propname: name of the property to read from |
| 50 | * @def: default value to return if the property has no value |
| 51 | * @return property value, or @def if not found |
| 52 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 53 | int dev_read_u32_default(const struct udevice *dev, const char *propname, |
| 54 | int def); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 55 | |
| 56 | /** |
Dario Binacchi | 81d80b5 | 2020-03-29 18:04:41 +0200 | [diff] [blame] | 57 | * dev_read_u32_index() - read an indexed 32-bit integer from a device's DT |
| 58 | * property |
| 59 | * |
| 60 | * @dev: device to read DT property from |
| 61 | * @propname: name of the property to read from |
| 62 | * @index: index of the integer to return |
| 63 | * @outp: place to put value (if found) |
| 64 | * @return 0 if OK, -ve on error |
| 65 | */ |
| 66 | int dev_read_u32_index(struct udevice *dev, const char *propname, int index, |
| 67 | u32 *outp); |
| 68 | |
| 69 | /** |
| 70 | * dev_read_u32_index_default() - read an indexed 32-bit integer from a device's |
| 71 | * DT property |
| 72 | * |
| 73 | * @dev: device to read DT property from |
| 74 | * @propname: name of the property to read from |
| 75 | * @index: index of the integer to return |
| 76 | * @def: default value to return if the property has no value |
| 77 | * @return property value, or @def if not found |
| 78 | */ |
| 79 | u32 dev_read_u32_index_default(struct udevice *dev, const char *propname, |
| 80 | int index, u32 def); |
| 81 | |
| 82 | /** |
Simon Glass | 6df01f9 | 2018-12-10 10:37:37 -0700 | [diff] [blame] | 83 | * dev_read_s32() - read a signed 32-bit integer from a device's DT property |
| 84 | * |
| 85 | * @dev: device to read DT property from |
| 86 | * @propname: name of the property to read from |
| 87 | * @outp: place to put value (if found) |
| 88 | * @return 0 if OK, -ve on error |
| 89 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 90 | int dev_read_s32(const struct udevice *dev, const char *propname, s32 *outp); |
Simon Glass | 6df01f9 | 2018-12-10 10:37:37 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * dev_read_s32_default() - read a signed 32-bit int from a device's DT property |
| 94 | * |
| 95 | * @dev: device to read DT property from |
| 96 | * @propname: name of the property to read from |
| 97 | * @def: default value to return if the property has no value |
| 98 | * @return property value, or @def if not found |
| 99 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 100 | int dev_read_s32_default(const struct udevice *dev, const char *propname, |
| 101 | int def); |
Simon Glass | 6df01f9 | 2018-12-10 10:37:37 -0700 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * dev_read_u32u() - read a 32-bit integer from a device's DT property |
| 105 | * |
| 106 | * This version uses a standard uint type. |
| 107 | * |
| 108 | * @dev: device to read DT property from |
| 109 | * @propname: name of the property to read from |
| 110 | * @outp: place to put value (if found) |
| 111 | * @return 0 if OK, -ve on error |
| 112 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 113 | int dev_read_u32u(const struct udevice *dev, const char *propname, uint *outp); |
Simon Glass | 6df01f9 | 2018-12-10 10:37:37 -0700 | [diff] [blame] | 114 | |
| 115 | /** |
T Karthik Reddy | 478860d | 2019-09-02 16:34:30 +0200 | [diff] [blame] | 116 | * dev_read_u64() - read a 64-bit integer from a device's DT property |
| 117 | * |
| 118 | * @dev: device to read DT property from |
| 119 | * @propname: name of the property to read from |
| 120 | * @outp: place to put value (if found) |
| 121 | * @return 0 if OK, -ve on error |
| 122 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 123 | int dev_read_u64(const struct udevice *dev, const char *propname, u64 *outp); |
T Karthik Reddy | 478860d | 2019-09-02 16:34:30 +0200 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * dev_read_u64_default() - read a 64-bit integer from a device's DT property |
| 127 | * |
| 128 | * @dev: device to read DT property from |
| 129 | * @propname: name of the property to read from |
| 130 | * @def: default value to return if the property has no value |
| 131 | * @return property value, or @def if not found |
| 132 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 133 | u64 dev_read_u64_default(const struct udevice *dev, const char *propname, |
| 134 | u64 def); |
T Karthik Reddy | 478860d | 2019-09-02 16:34:30 +0200 | [diff] [blame] | 135 | |
| 136 | /** |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 137 | * dev_read_string() - Read a string from a device's DT property |
| 138 | * |
| 139 | * @dev: device to read DT property from |
| 140 | * @propname: name of the property to read |
| 141 | * @return string from property value, or NULL if there is no such property |
| 142 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 143 | const char *dev_read_string(const struct udevice *dev, const char *propname); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * dev_read_bool() - read a boolean value from a device's DT property |
| 147 | * |
| 148 | * @dev: device to read DT property from |
| 149 | * @propname: name of property to read |
| 150 | * @return true if property is present (meaning true), false if not present |
| 151 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 152 | bool dev_read_bool(const struct udevice *dev, const char *propname); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * dev_read_subnode() - find a named subnode of a device |
| 156 | * |
| 157 | * @dev: device whose DT node contains the subnode |
| 158 | * @subnode_name: name of subnode to find |
| 159 | * @return reference to subnode (which can be invalid if there is no such |
| 160 | * subnode) |
| 161 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 162 | ofnode dev_read_subnode(const struct udevice *dev, const char *subbnode_name); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * dev_read_size() - read the size of a property |
| 166 | * |
| 167 | * @dev: device to check |
| 168 | * @propname: property to check |
| 169 | * @return size of property if present, or -EINVAL if not |
| 170 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 171 | int dev_read_size(const struct udevice *dev, const char *propname); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * dev_read_addr_index() - Get the indexed reg property of a device |
| 175 | * |
| 176 | * @dev: Device to read from |
| 177 | * @index: the 'reg' property can hold a list of <addr, size> pairs |
| 178 | * and @index is used to select which one is required |
| 179 | * |
| 180 | * @return address or FDT_ADDR_T_NONE if not found |
| 181 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 182 | fdt_addr_t dev_read_addr_index(const struct udevice *dev, int index); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 183 | |
| 184 | /** |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 185 | * dev_read_addr_size_index() - Get the indexed reg property of a device |
| 186 | * |
| 187 | * @dev: Device to read from |
| 188 | * @index: the 'reg' property can hold a list of <addr, size> pairs |
| 189 | * and @index is used to select which one is required |
| 190 | * @size: place to put size value (on success) |
| 191 | * |
| 192 | * @return address or FDT_ADDR_T_NONE if not found |
| 193 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 194 | fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, int index, |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 195 | fdt_size_t *size); |
| 196 | |
| 197 | /** |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 198 | * dev_remap_addr_index() - Get the indexed reg property of a device |
| 199 | * as a memory-mapped I/O pointer |
| 200 | * |
| 201 | * @dev: Device to read from |
| 202 | * @index: the 'reg' property can hold a list of <addr, size> pairs |
| 203 | * and @index is used to select which one is required |
| 204 | * |
| 205 | * @return pointer or NULL if not found |
| 206 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 207 | void *dev_remap_addr_index(const struct udevice *dev, int index); |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 208 | |
| 209 | /** |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 210 | * dev_read_addr_name() - Get the reg property of a device, indexed by name |
| 211 | * |
| 212 | * @dev: Device to read from |
| 213 | * @name: the 'reg' property can hold a list of <addr, size> pairs, with the |
| 214 | * 'reg-names' property providing named-based identification. @index |
| 215 | * indicates the value to search for in 'reg-names'. |
| 216 | * |
| 217 | * @return address or FDT_ADDR_T_NONE if not found |
| 218 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 219 | fdt_addr_t dev_read_addr_name(const struct udevice *dev, const char *name); |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 220 | |
| 221 | /** |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 222 | * dev_read_addr_size_name() - Get the reg property of a device, indexed by name |
| 223 | * |
| 224 | * @dev: Device to read from |
| 225 | * @name: the 'reg' property can hold a list of <addr, size> pairs, with the |
| 226 | * 'reg-names' property providing named-based identification. @index |
| 227 | * indicates the value to search for in 'reg-names'. |
| 228 | * @size: place to put size value (on success) |
| 229 | * |
| 230 | * @return address or FDT_ADDR_T_NONE if not found |
| 231 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 232 | fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, const char *name, |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 233 | fdt_size_t *size); |
| 234 | |
| 235 | /** |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 236 | * dev_remap_addr_name() - Get the reg property of a device, indexed by name, |
| 237 | * as a memory-mapped I/O pointer |
| 238 | * |
| 239 | * @dev: Device to read from |
| 240 | * @name: the 'reg' property can hold a list of <addr, size> pairs, with the |
| 241 | * 'reg-names' property providing named-based identification. @index |
| 242 | * indicates the value to search for in 'reg-names'. |
| 243 | * |
| 244 | * @return pointer or NULL if not found |
| 245 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 246 | void *dev_remap_addr_name(const struct udevice *dev, const char *name); |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 247 | |
| 248 | /** |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 249 | * dev_read_addr() - Get the reg property of a device |
| 250 | * |
| 251 | * @dev: Device to read from |
| 252 | * |
| 253 | * @return address or FDT_ADDR_T_NONE if not found |
| 254 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 255 | fdt_addr_t dev_read_addr(const struct udevice *dev); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 256 | |
| 257 | /** |
Philipp Tomsich | 7719b39 | 2017-09-11 22:04:12 +0200 | [diff] [blame] | 258 | * dev_read_addr_ptr() - Get the reg property of a device |
| 259 | * as a pointer |
| 260 | * |
| 261 | * @dev: Device to read from |
| 262 | * |
| 263 | * @return pointer or NULL if not found |
| 264 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 265 | void *dev_read_addr_ptr(const struct udevice *dev); |
Philipp Tomsich | 7719b39 | 2017-09-11 22:04:12 +0200 | [diff] [blame] | 266 | |
| 267 | /** |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 268 | * dev_read_addr_pci() - Read an address and handle PCI address translation |
| 269 | * |
| 270 | * At present U-Boot does not have address translation logic for PCI in the |
| 271 | * livetree implementation (of_addr.c). This special function supports this for |
| 272 | * the flat tree implementation. |
| 273 | * |
| 274 | * This function should be removed (and code should use dev_read() instead) |
| 275 | * once: |
| 276 | * |
| 277 | * 1. PCI address translation is added; and either |
| 278 | * 2. everything uses livetree where PCI translation is used (which is feasible |
| 279 | * in SPL and U-Boot proper) or PCI address translation is added to |
| 280 | * fdtdec_get_addr() and friends. |
| 281 | * |
| 282 | * @dev: Device to read from |
| 283 | * @return address or FDT_ADDR_T_NONE if not found |
| 284 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 285 | fdt_addr_t dev_read_addr_pci(const struct udevice *dev); |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 286 | |
| 287 | /** |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 288 | * dev_remap_addr() - Get the reg property of a device as a |
| 289 | * memory-mapped I/O pointer |
| 290 | * |
| 291 | * @dev: Device to read from |
| 292 | * |
| 293 | * @return pointer or NULL if not found |
| 294 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 295 | void *dev_remap_addr(const struct udevice *dev); |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 296 | |
| 297 | /** |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 298 | * dev_read_addr_size() - get address and size from a device property |
| 299 | * |
| 300 | * This does no address translation. It simply reads an property that contains |
| 301 | * an address and a size value, one after the other. |
| 302 | * |
| 303 | * @dev: Device to read from |
| 304 | * @propname: property to read |
| 305 | * @sizep: place to put size value (on success) |
| 306 | * @return address value, or FDT_ADDR_T_NONE on error |
| 307 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 308 | fdt_addr_t dev_read_addr_size(const struct udevice *dev, const char *propname, |
| 309 | fdt_size_t *sizep); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 310 | |
| 311 | /** |
| 312 | * dev_read_name() - get the name of a device's node |
| 313 | * |
Bin Meng | 4cf914d | 2019-07-05 09:23:18 -0700 | [diff] [blame] | 314 | * @dev: Device to read from |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 315 | * @return name of node |
| 316 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 317 | const char *dev_read_name(const struct udevice *dev); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 318 | |
| 319 | /** |
| 320 | * dev_read_stringlist_search() - find string in a string list and return index |
| 321 | * |
| 322 | * Note that it is possible for this function to succeed on property values |
| 323 | * that are not NUL-terminated. That's because the function will stop after |
| 324 | * finding the first occurrence of @string. This can for example happen with |
| 325 | * small-valued cell properties, such as #address-cells, when searching for |
| 326 | * the empty string. |
| 327 | * |
| 328 | * @dev: device to check |
| 329 | * @propname: name of the property containing the string list |
| 330 | * @string: string to look up in the string list |
| 331 | * |
| 332 | * @return: |
| 333 | * the index of the string in the list of strings |
| 334 | * -ENODATA if the property is not found |
| 335 | * -EINVAL on some other error |
| 336 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 337 | int dev_read_stringlist_search(const struct udevice *dev, const char *property, |
| 338 | const char *string); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 339 | |
| 340 | /** |
Jean-Jacques Hiblot | 8365ebd | 2017-09-21 17:03:09 +0200 | [diff] [blame] | 341 | * dev_read_string_index() - obtain an indexed string from a string list |
| 342 | * |
| 343 | * @dev: device to examine |
| 344 | * @propname: name of the property containing the string list |
| 345 | * @index: index of the string to return |
| 346 | * @out: return location for the string |
| 347 | * |
| 348 | * @return: |
| 349 | * length of string, if found or -ve error value if not found |
| 350 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 351 | int dev_read_string_index(const struct udevice *dev, const char *propname, |
| 352 | int index, const char **outp); |
Jean-Jacques Hiblot | 8365ebd | 2017-09-21 17:03:09 +0200 | [diff] [blame] | 353 | |
| 354 | /** |
| 355 | * dev_read_string_count() - find the number of strings in a string list |
| 356 | * |
| 357 | * @dev: device to examine |
| 358 | * @propname: name of the property containing the string list |
| 359 | * @return: |
| 360 | * number of strings in the list, or -ve error value if not found |
| 361 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 362 | int dev_read_string_count(const struct udevice *dev, const char *propname); |
Jean-Jacques Hiblot | 8365ebd | 2017-09-21 17:03:09 +0200 | [diff] [blame] | 363 | /** |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 364 | * dev_read_phandle_with_args() - Find a node pointed by phandle in a list |
| 365 | * |
| 366 | * This function is useful to parse lists of phandles and their arguments. |
| 367 | * Returns 0 on success and fills out_args, on error returns appropriate |
| 368 | * errno value. |
| 369 | * |
| 370 | * Caller is responsible to call of_node_put() on the returned out_args->np |
| 371 | * pointer. |
| 372 | * |
| 373 | * Example: |
| 374 | * |
| 375 | * phandle1: node1 { |
| 376 | * #list-cells = <2>; |
| 377 | * } |
| 378 | * |
| 379 | * phandle2: node2 { |
| 380 | * #list-cells = <1>; |
| 381 | * } |
| 382 | * |
| 383 | * node3 { |
| 384 | * list = <&phandle1 1 2 &phandle2 3>; |
| 385 | * } |
| 386 | * |
| 387 | * To get a device_node of the `node2' node you may call this: |
| 388 | * dev_read_phandle_with_args(dev, "list", "#list-cells", 0, 1, &args); |
| 389 | * |
| 390 | * @dev: device whose node containing a list |
| 391 | * @list_name: property name that contains a list |
| 392 | * @cells_name: property name that specifies phandles' arguments count |
| 393 | * @cells_count: Cell count to use if @cells_name is NULL |
| 394 | * @index: index of a phandle to parse out |
| 395 | * @out_args: optional pointer to output arguments structure (will be filled) |
| 396 | * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if |
| 397 | * @list_name does not exist, -EINVAL if a phandle was not found, |
| 398 | * @cells_name could not be found, the arguments were truncated or there |
| 399 | * were too many arguments. |
| 400 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 401 | int dev_read_phandle_with_args(const struct udevice *dev, const char *list_name, |
| 402 | const char *cells_name, int cell_count, |
| 403 | int index, struct ofnode_phandle_args *out_args); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 404 | |
| 405 | /** |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 406 | * dev_count_phandle_with_args() - Return phandle number in a list |
| 407 | * |
| 408 | * This function is usefull to get phandle number contained in a property list. |
| 409 | * For example, this allows to allocate the right amount of memory to keep |
| 410 | * clock's reference contained into the "clocks" property. |
| 411 | * |
| 412 | * |
| 413 | * @dev: device whose node containing a list |
| 414 | * @list_name: property name that contains a list |
| 415 | * @cells_name: property name that specifies phandles' arguments count |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 416 | * @cells_count: Cell count to use if @cells_name is NULL |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 417 | * @Returns number of phandle found on success, on error returns appropriate |
| 418 | * errno value. |
| 419 | */ |
| 420 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 421 | int dev_count_phandle_with_args(const struct udevice *dev, |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 422 | const char *list_name, const char *cells_name, |
| 423 | int cell_count); |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 424 | |
| 425 | /** |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 426 | * dev_read_addr_cells() - Get the number of address cells for a device's node |
| 427 | * |
| 428 | * This walks back up the tree to find the closest #address-cells property |
| 429 | * which controls the given node. |
| 430 | * |
Mario Six | 16408ee | 2018-01-15 11:09:36 +0100 | [diff] [blame] | 431 | * @dev: device to check |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 432 | * @return number of address cells this node uses |
| 433 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 434 | int dev_read_addr_cells(const struct udevice *dev); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 435 | |
| 436 | /** |
| 437 | * dev_read_size_cells() - Get the number of size cells for a device's node |
| 438 | * |
| 439 | * This walks back up the tree to find the closest #size-cells property |
| 440 | * which controls the given node. |
| 441 | * |
Mario Six | 16408ee | 2018-01-15 11:09:36 +0100 | [diff] [blame] | 442 | * @dev: device to check |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 443 | * @return number of size cells this node uses |
| 444 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 445 | int dev_read_size_cells(const struct udevice *dev); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 446 | |
| 447 | /** |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 448 | * dev_read_addr_cells() - Get the address cells property in a node |
| 449 | * |
| 450 | * This function matches fdt_address_cells(). |
| 451 | * |
Mario Six | 16408ee | 2018-01-15 11:09:36 +0100 | [diff] [blame] | 452 | * @dev: device to check |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 453 | * @return number of address cells this node uses |
| 454 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 455 | int dev_read_simple_addr_cells(const struct udevice *dev); |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 456 | |
| 457 | /** |
| 458 | * dev_read_size_cells() - Get the size cells property in a node |
| 459 | * |
| 460 | * This function matches fdt_size_cells(). |
| 461 | * |
Mario Six | 16408ee | 2018-01-15 11:09:36 +0100 | [diff] [blame] | 462 | * @dev: device to check |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 463 | * @return number of size cells this node uses |
| 464 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 465 | int dev_read_simple_size_cells(const struct udevice *dev); |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 466 | |
| 467 | /** |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 468 | * dev_read_phandle() - Get the phandle from a device |
| 469 | * |
| 470 | * @dev: device to check |
| 471 | * @return phandle (1 or greater), or 0 if no phandle or other error |
| 472 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 473 | int dev_read_phandle(const struct udevice *dev); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 474 | |
| 475 | /** |
| 476 | * dev_read_prop()- - read a property from a device's node |
| 477 | * |
| 478 | * @dev: device to check |
| 479 | * @propname: property to read |
| 480 | * @lenp: place to put length on success |
| 481 | * @return pointer to property, or NULL if not found |
| 482 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 483 | const void *dev_read_prop(const struct udevice *dev, const char *propname, |
| 484 | int *lenp); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 485 | |
| 486 | /** |
Patrick Delaunay | caee155 | 2020-01-13 11:34:56 +0100 | [diff] [blame] | 487 | * dev_read_first_prop()- get the reference of the first property |
| 488 | * |
| 489 | * Get reference to the first property of the node, it is used to iterate |
| 490 | * and read all the property with dev_read_prop_by_prop(). |
| 491 | * |
| 492 | * @dev: device to check |
| 493 | * @prop: place to put argument reference |
| 494 | * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found |
| 495 | */ |
| 496 | int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop); |
| 497 | |
| 498 | /** |
| 499 | * ofnode_get_next_property() - get the reference of the next property |
| 500 | * |
| 501 | * Get reference to the next property of the node, it is used to iterate |
| 502 | * and read all the property with dev_read_prop_by_prop(). |
| 503 | * |
| 504 | * @prop: reference of current argument and place to put reference of next one |
| 505 | * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found |
| 506 | */ |
| 507 | int dev_read_next_prop(struct ofprop *prop); |
| 508 | |
| 509 | /** |
| 510 | * dev_read_prop_by_prop() - get a pointer to the value of a property |
| 511 | * |
| 512 | * Get value for the property identified by the provided reference. |
| 513 | * |
| 514 | * @prop: reference on property |
| 515 | * @propname: If non-NULL, place to property name on success, |
| 516 | * @lenp: If non-NULL, place to put length on success |
| 517 | * @return 0 if OK, -ve on error. -FDT_ERR_NOTFOUND if not found |
| 518 | */ |
| 519 | const void *dev_read_prop_by_prop(struct ofprop *prop, |
| 520 | const char **propname, int *lenp); |
| 521 | |
| 522 | /** |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 523 | * dev_read_alias_seq() - Get the alias sequence number of a node |
| 524 | * |
| 525 | * This works out whether a node is pointed to by an alias, and if so, the |
| 526 | * sequence number of that alias. Aliases are of the form <base><num> where |
| 527 | * <num> is the sequence number. For example spi2 would be sequence number 2. |
| 528 | * |
| 529 | * @dev: device to look up |
| 530 | * @devnump: set to the sequence number if one is found |
| 531 | * @return 0 if a sequence was found, -ve if not |
| 532 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 533 | int dev_read_alias_seq(const struct udevice *dev, int *devnump); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 534 | |
| 535 | /** |
| 536 | * dev_read_u32_array() - Find and read an array of 32 bit integers |
| 537 | * |
| 538 | * Search for a property in a device node and read 32-bit value(s) from |
| 539 | * it. |
| 540 | * |
| 541 | * The out_values is modified only if a valid u32 value can be decoded. |
| 542 | * |
| 543 | * @dev: device to look up |
| 544 | * @propname: name of the property to read |
| 545 | * @out_values: pointer to return value, modified only if return value is 0 |
| 546 | * @sz: number of array elements to read |
| 547 | * @return 0 on success, -EINVAL if the property does not exist, -ENODATA if |
| 548 | * property does not have a value, and -EOVERFLOW if the property data isn't |
| 549 | * large enough. |
| 550 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 551 | int dev_read_u32_array(const struct udevice *dev, const char *propname, |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 552 | u32 *out_values, size_t sz); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 553 | |
| 554 | /** |
| 555 | * dev_read_first_subnode() - find the first subnode of a device's node |
| 556 | * |
| 557 | * @dev: device to look up |
| 558 | * @return reference to the first subnode (which can be invalid if the device's |
| 559 | * node has no subnodes) |
| 560 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 561 | ofnode dev_read_first_subnode(const struct udevice *dev); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 562 | |
| 563 | /** |
| 564 | * ofnode_next_subnode() - find the next sibling of a subnode |
| 565 | * |
| 566 | * @node: valid reference to previous node (sibling) |
| 567 | * @return reference to the next subnode (which can be invalid if the node |
| 568 | * has no more siblings) |
| 569 | */ |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 570 | ofnode dev_read_next_subnode(ofnode node); |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 571 | |
| 572 | /** |
| 573 | * dev_read_u8_array_ptr() - find an 8-bit array |
| 574 | * |
| 575 | * Look up a device's node property and return a pointer to its contents as a |
| 576 | * byte array of given length. The property must have at least enough data |
| 577 | * for the array (count bytes). It may have more, but this will be ignored. |
| 578 | * The data is not copied. |
| 579 | * |
| 580 | * @dev: device to look up |
| 581 | * @propname: name of property to find |
| 582 | * @sz: number of array elements |
| 583 | * @return pointer to byte array if found, or NULL if the property is not |
| 584 | * found or there is not enough data |
| 585 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 586 | const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev, |
| 587 | const char *propname, size_t sz); |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 588 | |
Simon Glass | fa031f8 | 2017-06-12 06:21:30 -0600 | [diff] [blame] | 589 | /** |
| 590 | * dev_read_enabled() - check whether a node is enabled |
| 591 | * |
| 592 | * This looks for a 'status' property. If this exists, then returns 1 if |
| 593 | * the status is 'ok' and 0 otherwise. If there is no status property, |
| 594 | * it returns 1 on the assumption that anything mentioned should be enabled |
| 595 | * by default. |
| 596 | * |
| 597 | * @dev: device to examine |
| 598 | * @return integer value 0 (not enabled) or 1 (enabled) |
| 599 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 600 | int dev_read_enabled(const struct udevice *dev); |
Simon Glass | fa031f8 | 2017-06-12 06:21:30 -0600 | [diff] [blame] | 601 | |
Simon Glass | f7bfcc4 | 2017-07-25 08:29:55 -0600 | [diff] [blame] | 602 | /** |
| 603 | * dev_read_resource() - obtain an indexed resource from a device. |
| 604 | * |
Masahiro Yamada | 4dada2c | 2017-08-26 01:12:30 +0900 | [diff] [blame] | 605 | * @dev: device to examine |
Simon Glass | f7bfcc4 | 2017-07-25 08:29:55 -0600 | [diff] [blame] | 606 | * @index index of the resource to retrieve (0 = first) |
| 607 | * @res returns the resource |
| 608 | * @return 0 if ok, negative on error |
| 609 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 610 | int dev_read_resource(const struct udevice *dev, uint index, |
| 611 | struct resource *res); |
Simon Glass | f7bfcc4 | 2017-07-25 08:29:55 -0600 | [diff] [blame] | 612 | |
Masahiro Yamada | 4dada2c | 2017-08-26 01:12:30 +0900 | [diff] [blame] | 613 | /** |
| 614 | * dev_read_resource_byname() - obtain a named resource from a device. |
| 615 | * |
| 616 | * @dev: device to examine |
| 617 | * @name: name of the resource to retrieve |
| 618 | * @res: returns the resource |
| 619 | * @return 0 if ok, negative on error |
| 620 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 621 | int dev_read_resource_byname(const struct udevice *dev, const char *name, |
Masahiro Yamada | 4dada2c | 2017-08-26 01:12:30 +0900 | [diff] [blame] | 622 | struct resource *res); |
| 623 | |
Mario Six | aefac06 | 2018-01-15 11:07:19 +0100 | [diff] [blame] | 624 | /** |
Fabien Dessenne | 22236e0 | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 625 | * dev_translate_address() - Translate a device-tree address |
Mario Six | aefac06 | 2018-01-15 11:07:19 +0100 | [diff] [blame] | 626 | * |
| 627 | * Translate an address from the device-tree into a CPU physical address. This |
| 628 | * function walks up the tree and applies the various bus mappings along the |
| 629 | * way. |
| 630 | * |
| 631 | * @dev: device giving the context in which to translate the address |
| 632 | * @in_addr: pointer to the address to translate |
| 633 | * @return the translated address; OF_BAD_ADDR on error |
| 634 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 635 | u64 dev_translate_address(const struct udevice *dev, const fdt32_t *in_addr); |
Michal Simek | d0e30a0 | 2019-01-31 16:30:59 +0100 | [diff] [blame] | 636 | |
| 637 | /** |
Fabien Dessenne | 22236e0 | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 638 | * dev_translate_dma_address() - Translate a device-tree DMA address |
| 639 | * |
| 640 | * Translate a DMA address from the device-tree into a CPU physical address. |
| 641 | * This function walks up the tree and applies the various bus mappings along |
| 642 | * the way. |
| 643 | * |
| 644 | * @dev: device giving the context in which to translate the DMA address |
| 645 | * @in_addr: pointer to the DMA address to translate |
| 646 | * @return the translated DMA address; OF_BAD_ADDR on error |
| 647 | */ |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 648 | u64 dev_translate_dma_address(const struct udevice *dev, |
| 649 | const fdt32_t *in_addr); |
Fabien Dessenne | 22236e0 | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 650 | |
| 651 | /** |
Michal Simek | d0e30a0 | 2019-01-31 16:30:59 +0100 | [diff] [blame] | 652 | * dev_read_alias_highest_id - Get highest alias id for the given stem |
| 653 | * @stem: Alias stem to be examined |
| 654 | * |
| 655 | * The function travels the lookup table to get the highest alias id for the |
| 656 | * given alias stem. |
| 657 | * @return alias ID, if found, else -1 |
| 658 | */ |
| 659 | int dev_read_alias_highest_id(const char *stem); |
| 660 | |
developer | d93c8b4 | 2020-05-02 11:35:09 +0200 | [diff] [blame] | 661 | /** |
| 662 | * dev_get_child_count() - get the child count of a device |
| 663 | * |
| 664 | * @dev: device to use for interation (struct udevice *) |
| 665 | * @return the count of child subnode |
| 666 | */ |
| 667 | int dev_get_child_count(const struct udevice *dev); |
| 668 | |
Stefan Roese | 0a9ecc5 | 2020-08-05 13:56:11 +0200 | [diff] [blame] | 669 | /** |
| 670 | * dev_read_pci_bus_range - Read PCI bus-range resource |
| 671 | * |
| 672 | * Look at the bus range property of a device node and return the pci bus |
| 673 | * range for this node. |
| 674 | * |
| 675 | * @dev: device to examine |
| 676 | * @res returns the resource |
| 677 | * @return 0 if ok, negative on error |
| 678 | */ |
| 679 | int dev_read_pci_bus_range(const struct udevice *dev, struct resource *res); |
| 680 | |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 681 | #else /* CONFIG_DM_DEV_READ_INLINE is enabled */ |
| 682 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 683 | static inline int dev_read_u32(const struct udevice *dev, |
Masahiro Yamada | 71d115f | 2017-12-30 02:00:05 +0900 | [diff] [blame] | 684 | const char *propname, u32 *outp) |
| 685 | { |
| 686 | return ofnode_read_u32(dev_ofnode(dev), propname, outp); |
| 687 | } |
| 688 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 689 | static inline int dev_read_u32_default(const struct udevice *dev, |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 690 | const char *propname, int def) |
| 691 | { |
| 692 | return ofnode_read_u32_default(dev_ofnode(dev), propname, def); |
| 693 | } |
| 694 | |
Dario Binacchi | 81d80b5 | 2020-03-29 18:04:41 +0200 | [diff] [blame] | 695 | static inline int dev_read_u32_index(struct udevice *dev, |
| 696 | const char *propname, int index, u32 *outp) |
| 697 | { |
| 698 | return ofnode_read_u32_index(dev_ofnode(dev), propname, index, outp); |
| 699 | } |
| 700 | |
| 701 | static inline u32 dev_read_u32_index_default(struct udevice *dev, |
| 702 | const char *propname, int index, |
| 703 | u32 def) |
| 704 | { |
| 705 | return ofnode_read_u32_index_default(dev_ofnode(dev), propname, index, |
| 706 | def); |
| 707 | } |
| 708 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 709 | static inline int dev_read_s32(const struct udevice *dev, |
Simon Glass | 6df01f9 | 2018-12-10 10:37:37 -0700 | [diff] [blame] | 710 | const char *propname, s32 *outp) |
| 711 | { |
| 712 | return ofnode_read_s32(dev_ofnode(dev), propname, outp); |
| 713 | } |
| 714 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 715 | static inline int dev_read_s32_default(const struct udevice *dev, |
Simon Glass | 6df01f9 | 2018-12-10 10:37:37 -0700 | [diff] [blame] | 716 | const char *propname, int def) |
| 717 | { |
| 718 | return ofnode_read_s32_default(dev_ofnode(dev), propname, def); |
| 719 | } |
| 720 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 721 | static inline int dev_read_u32u(const struct udevice *dev, |
Simon Glass | 6df01f9 | 2018-12-10 10:37:37 -0700 | [diff] [blame] | 722 | const char *propname, uint *outp) |
| 723 | { |
| 724 | u32 val; |
| 725 | int ret; |
| 726 | |
| 727 | ret = ofnode_read_u32(dev_ofnode(dev), propname, &val); |
| 728 | if (ret) |
| 729 | return ret; |
| 730 | *outp = val; |
| 731 | |
| 732 | return 0; |
| 733 | } |
| 734 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 735 | static inline int dev_read_u64(const struct udevice *dev, |
T Karthik Reddy | 478860d | 2019-09-02 16:34:30 +0200 | [diff] [blame] | 736 | const char *propname, u64 *outp) |
| 737 | { |
| 738 | return ofnode_read_u64(dev_ofnode(dev), propname, outp); |
| 739 | } |
| 740 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 741 | static inline u64 dev_read_u64_default(const struct udevice *dev, |
T Karthik Reddy | 478860d | 2019-09-02 16:34:30 +0200 | [diff] [blame] | 742 | const char *propname, u64 def) |
| 743 | { |
| 744 | return ofnode_read_u64_default(dev_ofnode(dev), propname, def); |
| 745 | } |
| 746 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 747 | static inline const char *dev_read_string(const struct udevice *dev, |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 748 | const char *propname) |
| 749 | { |
| 750 | return ofnode_read_string(dev_ofnode(dev), propname); |
| 751 | } |
| 752 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 753 | static inline bool dev_read_bool(const struct udevice *dev, |
| 754 | const char *propname) |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 755 | { |
| 756 | return ofnode_read_bool(dev_ofnode(dev), propname); |
| 757 | } |
| 758 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 759 | static inline ofnode dev_read_subnode(const struct udevice *dev, |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 760 | const char *subbnode_name) |
| 761 | { |
| 762 | return ofnode_find_subnode(dev_ofnode(dev), subbnode_name); |
| 763 | } |
| 764 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 765 | static inline int dev_read_size(const struct udevice *dev, const char *propname) |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 766 | { |
| 767 | return ofnode_read_size(dev_ofnode(dev), propname); |
| 768 | } |
| 769 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 770 | static inline fdt_addr_t dev_read_addr_index(const struct udevice *dev, |
| 771 | int index) |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 772 | { |
| 773 | return devfdt_get_addr_index(dev, index); |
| 774 | } |
| 775 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 776 | static inline fdt_addr_t dev_read_addr_size_index(const struct udevice *dev, |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 777 | int index, |
| 778 | fdt_size_t *size) |
| 779 | { |
| 780 | return devfdt_get_addr_size_index(dev, index, size); |
| 781 | } |
| 782 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 783 | static inline fdt_addr_t dev_read_addr_name(const struct udevice *dev, |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 784 | const char *name) |
| 785 | { |
| 786 | return devfdt_get_addr_name(dev, name); |
| 787 | } |
| 788 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 789 | static inline fdt_addr_t dev_read_addr_size_name(const struct udevice *dev, |
Sekhar Nori | f677c6f | 2019-08-01 19:12:56 +0530 | [diff] [blame] | 790 | const char *name, |
| 791 | fdt_size_t *size) |
| 792 | { |
| 793 | return devfdt_get_addr_size_name(dev, name, size); |
| 794 | } |
| 795 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 796 | static inline fdt_addr_t dev_read_addr(const struct udevice *dev) |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 797 | { |
| 798 | return devfdt_get_addr(dev); |
| 799 | } |
| 800 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 801 | static inline void *dev_read_addr_ptr(const struct udevice *dev) |
Philipp Tomsich | 7719b39 | 2017-09-11 22:04:12 +0200 | [diff] [blame] | 802 | { |
Ovidiu Panait | a633f00 | 2020-08-03 22:17:35 +0300 | [diff] [blame] | 803 | return devfdt_get_addr_ptr(dev); |
Philipp Tomsich | 7719b39 | 2017-09-11 22:04:12 +0200 | [diff] [blame] | 804 | } |
| 805 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 806 | static inline fdt_addr_t dev_read_addr_pci(const struct udevice *dev) |
Simon Glass | 23b2759 | 2019-09-15 12:08:58 -0600 | [diff] [blame] | 807 | { |
| 808 | return devfdt_get_addr_pci(dev); |
| 809 | } |
| 810 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 811 | static inline void *dev_remap_addr(const struct udevice *dev) |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 812 | { |
| 813 | return devfdt_remap_addr(dev); |
| 814 | } |
| 815 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 816 | static inline void *dev_remap_addr_index(const struct udevice *dev, int index) |
Álvaro Fernández Rojas | 67036129 | 2018-04-29 21:56:54 +0200 | [diff] [blame] | 817 | { |
| 818 | return devfdt_remap_addr_index(dev, index); |
| 819 | } |
| 820 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 821 | static inline void *dev_remap_addr_name(const struct udevice *dev, |
| 822 | const char *name) |
Álvaro Fernández Rojas | a318115 | 2018-12-03 19:37:09 +0100 | [diff] [blame] | 823 | { |
| 824 | return devfdt_remap_addr_name(dev, name); |
| 825 | } |
| 826 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 827 | static inline fdt_addr_t dev_read_addr_size(const struct udevice *dev, |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 828 | const char *propname, |
| 829 | fdt_size_t *sizep) |
| 830 | { |
| 831 | return ofnode_get_addr_size(dev_ofnode(dev), propname, sizep); |
| 832 | } |
| 833 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 834 | static inline const char *dev_read_name(const struct udevice *dev) |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 835 | { |
| 836 | return ofnode_get_name(dev_ofnode(dev)); |
| 837 | } |
| 838 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 839 | static inline int dev_read_stringlist_search(const struct udevice *dev, |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 840 | const char *propname, |
| 841 | const char *string) |
| 842 | { |
| 843 | return ofnode_stringlist_search(dev_ofnode(dev), propname, string); |
| 844 | } |
| 845 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 846 | static inline int dev_read_string_index(const struct udevice *dev, |
Jean-Jacques Hiblot | 8365ebd | 2017-09-21 17:03:09 +0200 | [diff] [blame] | 847 | const char *propname, int index, |
| 848 | const char **outp) |
| 849 | { |
| 850 | return ofnode_read_string_index(dev_ofnode(dev), propname, index, outp); |
| 851 | } |
| 852 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 853 | static inline int dev_read_string_count(const struct udevice *dev, |
Jean-Jacques Hiblot | 8365ebd | 2017-09-21 17:03:09 +0200 | [diff] [blame] | 854 | const char *propname) |
| 855 | { |
| 856 | return ofnode_read_string_count(dev_ofnode(dev), propname); |
| 857 | } |
| 858 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 859 | static inline int dev_read_phandle_with_args(const struct udevice *dev, |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 860 | const char *list_name, const char *cells_name, int cell_count, |
| 861 | int index, struct ofnode_phandle_args *out_args) |
| 862 | { |
| 863 | return ofnode_parse_phandle_with_args(dev_ofnode(dev), list_name, |
| 864 | cells_name, cell_count, index, |
| 865 | out_args); |
| 866 | } |
| 867 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 868 | static inline int dev_count_phandle_with_args(const struct udevice *dev, |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 869 | const char *list_name, const char *cells_name, int cell_count) |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 870 | { |
| 871 | return ofnode_count_phandle_with_args(dev_ofnode(dev), list_name, |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 872 | cells_name, cell_count); |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 873 | } |
| 874 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 875 | static inline int dev_read_addr_cells(const struct udevice *dev) |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 876 | { |
Heinrich Schuchardt | 60a84af | 2020-07-25 21:38:49 +0200 | [diff] [blame] | 877 | int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev)); |
| 878 | |
| 879 | return fdt_address_cells(gd->fdt_blob, parent); |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 880 | } |
| 881 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 882 | static inline int dev_read_size_cells(const struct udevice *dev) |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 883 | { |
Heinrich Schuchardt | 60a84af | 2020-07-25 21:38:49 +0200 | [diff] [blame] | 884 | int parent = fdt_parent_offset(gd->fdt_blob, dev_of_offset(dev)); |
| 885 | |
| 886 | return fdt_size_cells(gd->fdt_blob, parent); |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 887 | } |
| 888 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 889 | static inline int dev_read_simple_addr_cells(const struct udevice *dev) |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 890 | { |
| 891 | return fdt_address_cells(gd->fdt_blob, dev_of_offset(dev)); |
| 892 | } |
| 893 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 894 | static inline int dev_read_simple_size_cells(const struct udevice *dev) |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 895 | { |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 896 | return fdt_size_cells(gd->fdt_blob, dev_of_offset(dev)); |
| 897 | } |
| 898 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 899 | static inline int dev_read_phandle(const struct udevice *dev) |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 900 | { |
| 901 | return fdt_get_phandle(gd->fdt_blob, dev_of_offset(dev)); |
| 902 | } |
| 903 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 904 | static inline const void *dev_read_prop(const struct udevice *dev, |
Masahiro Yamada | baebec1 | 2017-07-17 12:18:39 +0900 | [diff] [blame] | 905 | const char *propname, int *lenp) |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 906 | { |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 907 | return ofnode_get_property(dev_ofnode(dev), propname, lenp); |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 908 | } |
| 909 | |
Patrick Delaunay | caee155 | 2020-01-13 11:34:56 +0100 | [diff] [blame] | 910 | static inline int dev_read_first_prop(const struct udevice *dev, struct ofprop *prop) |
| 911 | { |
| 912 | return ofnode_get_first_property(dev_ofnode(dev), prop); |
| 913 | } |
| 914 | |
| 915 | static inline int dev_read_next_prop(struct ofprop *prop) |
| 916 | { |
| 917 | return ofnode_get_next_property(prop); |
| 918 | } |
| 919 | |
| 920 | static inline const void *dev_read_prop_by_prop(struct ofprop *prop, |
| 921 | const char **propname, |
| 922 | int *lenp) |
| 923 | { |
| 924 | return ofnode_get_property_by_prop(prop, propname, lenp); |
| 925 | } |
| 926 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 927 | static inline int dev_read_alias_seq(const struct udevice *dev, int *devnump) |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 928 | { |
Marcel Ziswiler | 65c2bf6 | 2019-05-20 02:44:57 +0200 | [diff] [blame] | 929 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 930 | return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name, |
| 931 | dev_of_offset(dev), devnump); |
Marcel Ziswiler | 65c2bf6 | 2019-05-20 02:44:57 +0200 | [diff] [blame] | 932 | #else |
| 933 | return -ENOTSUPP; |
| 934 | #endif |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 935 | } |
| 936 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 937 | static inline int dev_read_u32_array(const struct udevice *dev, |
| 938 | const char *propname, u32 *out_values, |
| 939 | size_t sz) |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 940 | { |
| 941 | return ofnode_read_u32_array(dev_ofnode(dev), propname, out_values, sz); |
| 942 | } |
| 943 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 944 | static inline ofnode dev_read_first_subnode(const struct udevice *dev) |
Simon Glass | cfdae5c | 2017-05-18 20:09:04 -0600 | [diff] [blame] | 945 | { |
| 946 | return ofnode_first_subnode(dev_ofnode(dev)); |
| 947 | } |
| 948 | |
| 949 | static inline ofnode dev_read_next_subnode(ofnode node) |
| 950 | { |
| 951 | return ofnode_next_subnode(node); |
| 952 | } |
| 953 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 954 | static inline const uint8_t *dev_read_u8_array_ptr(const struct udevice *dev, |
Simon Glass | 000676b | 2020-01-27 08:49:47 -0700 | [diff] [blame] | 955 | const char *propname, |
| 956 | size_t sz) |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 957 | { |
| 958 | return ofnode_read_u8_array_ptr(dev_ofnode(dev), propname, sz); |
| 959 | } |
| 960 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 961 | static inline int dev_read_enabled(const struct udevice *dev) |
Simon Glass | fa031f8 | 2017-06-12 06:21:30 -0600 | [diff] [blame] | 962 | { |
| 963 | return fdtdec_get_is_enabled(gd->fdt_blob, dev_of_offset(dev)); |
| 964 | } |
| 965 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 966 | static inline int dev_read_resource(const struct udevice *dev, uint index, |
Simon Glass | f7bfcc4 | 2017-07-25 08:29:55 -0600 | [diff] [blame] | 967 | struct resource *res) |
| 968 | { |
| 969 | return ofnode_read_resource(dev_ofnode(dev), index, res); |
| 970 | } |
| 971 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 972 | static inline int dev_read_resource_byname(const struct udevice *dev, |
Masahiro Yamada | 4dada2c | 2017-08-26 01:12:30 +0900 | [diff] [blame] | 973 | const char *name, |
| 974 | struct resource *res) |
| 975 | { |
| 976 | return ofnode_read_resource_byname(dev_ofnode(dev), name, res); |
| 977 | } |
| 978 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 979 | static inline u64 dev_translate_address(const struct udevice *dev, |
| 980 | const fdt32_t *in_addr) |
Mario Six | aefac06 | 2018-01-15 11:07:19 +0100 | [diff] [blame] | 981 | { |
| 982 | return ofnode_translate_address(dev_ofnode(dev), in_addr); |
| 983 | } |
| 984 | |
Simon Glass | 2f9a612 | 2020-01-27 08:49:40 -0700 | [diff] [blame] | 985 | static inline u64 dev_translate_dma_address(const struct udevice *dev, |
| 986 | const fdt32_t *in_addr) |
Fabien Dessenne | 22236e0 | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 987 | { |
| 988 | return ofnode_translate_dma_address(dev_ofnode(dev), in_addr); |
| 989 | } |
| 990 | |
Michal Simek | d0e30a0 | 2019-01-31 16:30:59 +0100 | [diff] [blame] | 991 | static inline int dev_read_alias_highest_id(const char *stem) |
| 992 | { |
Michael Walle | eccf2d0 | 2020-06-02 01:47:08 +0200 | [diff] [blame] | 993 | if (!CONFIG_IS_ENABLED(OF_LIBFDT)) |
| 994 | return -1; |
Michal Simek | d0e30a0 | 2019-01-31 16:30:59 +0100 | [diff] [blame] | 995 | return fdtdec_get_alias_highest_id(gd->fdt_blob, stem); |
| 996 | } |
| 997 | |
developer | d93c8b4 | 2020-05-02 11:35:09 +0200 | [diff] [blame] | 998 | static inline int dev_get_child_count(const struct udevice *dev) |
| 999 | { |
| 1000 | return ofnode_get_child_count(dev_ofnode(dev)); |
| 1001 | } |
| 1002 | |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 1003 | #endif /* CONFIG_DM_DEV_READ_INLINE */ |
| 1004 | |
| 1005 | /** |
| 1006 | * dev_for_each_subnode() - Helper function to iterate through subnodes |
| 1007 | * |
| 1008 | * This creates a for() loop which works through the subnodes in a device's |
| 1009 | * device-tree node. |
| 1010 | * |
| 1011 | * @subnode: ofnode holding the current subnode |
| 1012 | * @dev: device to use for interation (struct udevice *) |
| 1013 | */ |
| 1014 | #define dev_for_each_subnode(subnode, dev) \ |
| 1015 | for (subnode = dev_read_first_subnode(dev); \ |
| 1016 | ofnode_valid(subnode); \ |
| 1017 | subnode = ofnode_next_subnode(subnode)) |
| 1018 | |
Patrick Delaunay | caee155 | 2020-01-13 11:34:56 +0100 | [diff] [blame] | 1019 | /** |
| 1020 | * dev_for_each_property() - Helper function to iterate through property |
| 1021 | * |
| 1022 | * This creates a for() loop which works through the property in a device's |
| 1023 | * device-tree node. |
| 1024 | * |
| 1025 | * @prop: struct ofprop holding the current property |
| 1026 | * @dev: device to use for interation (struct udevice *) |
| 1027 | */ |
| 1028 | #define dev_for_each_property(prop, dev) \ |
| 1029 | for (int ret_prop = dev_read_first_prop(dev, &prop); \ |
| 1030 | !ret_prop; \ |
| 1031 | ret_prop = dev_read_next_prop(&prop)) |
| 1032 | |
Simon Glass | 34ef0c2 | 2017-05-18 20:09:03 -0600 | [diff] [blame] | 1033 | #endif |