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