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