Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | cb13a1b | 2022-09-06 20:27:26 -0600 | [diff] [blame] | 7 | #define LOG_CATEGORY LOGC_DT |
| 8 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <fdtdec.h> |
| 12 | #include <fdt_support.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <malloc.h> |
Simon Glass | 722281b | 2023-06-01 10:22:42 -0600 | [diff] [blame] | 15 | #include <of_live.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 16 | #include <linux/libfdt.h> |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 17 | #include <dm/of_access.h> |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 18 | #include <dm/of_addr.h> |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 19 | #include <dm/ofnode.h> |
| 20 | #include <linux/err.h> |
Simon Glass | f7bfcc4 | 2017-07-25 08:29:55 -0600 | [diff] [blame] | 21 | #include <linux/ioport.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 22 | #include <asm/global_data.h> |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 23 | |
Simon Glass | cb13a1b | 2022-09-06 20:27:26 -0600 | [diff] [blame] | 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | |
| 26 | #if CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) |
| 27 | static void *oftree_list[CONFIG_OFNODE_MULTI_TREE_MAX]; |
| 28 | static int oftree_count; |
| 29 | |
| 30 | void oftree_reset(void) |
| 31 | { |
| 32 | if (gd->flags & GD_FLG_RELOC) { |
| 33 | oftree_count = 0; |
| 34 | oftree_list[oftree_count++] = (void *)gd->fdt_blob; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | static int oftree_find(const void *fdt) |
| 39 | { |
| 40 | int i; |
| 41 | |
| 42 | for (i = 0; i < oftree_count; i++) { |
| 43 | if (fdt == oftree_list[i]) |
| 44 | return i; |
| 45 | } |
| 46 | |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | static oftree oftree_ensure(void *fdt) |
| 51 | { |
| 52 | oftree tree; |
| 53 | int i; |
| 54 | |
Simon Glass | 722281b | 2023-06-01 10:22:42 -0600 | [diff] [blame] | 55 | if (of_live_active()) { |
| 56 | struct device_node *root; |
| 57 | int ret; |
| 58 | |
| 59 | ret = unflatten_device_tree(fdt, &root); |
| 60 | if (ret) { |
| 61 | log_err("Failed to create live tree: err=%d\n", ret); |
| 62 | return oftree_null(); |
| 63 | } |
| 64 | tree = oftree_from_np(root); |
| 65 | |
| 66 | return tree; |
| 67 | } |
| 68 | |
Simon Glass | cb13a1b | 2022-09-06 20:27:26 -0600 | [diff] [blame] | 69 | if (gd->flags & GD_FLG_RELOC) { |
| 70 | i = oftree_find(fdt); |
| 71 | if (i == -1) { |
| 72 | if (oftree_count == CONFIG_OFNODE_MULTI_TREE_MAX) { |
| 73 | log_warning("Too many registered device trees (max %d)\n", |
| 74 | CONFIG_OFNODE_MULTI_TREE_MAX); |
| 75 | return oftree_null(); |
| 76 | } |
| 77 | |
| 78 | /* register the new tree */ |
| 79 | i = oftree_count++; |
| 80 | oftree_list[i] = fdt; |
| 81 | log_debug("oftree: registered tree %d: %p\n", i, fdt); |
| 82 | } |
| 83 | } else { |
| 84 | if (fdt != gd->fdt_blob) { |
Simon Glass | 67f8e11 | 2023-06-01 10:22:41 -0600 | [diff] [blame] | 85 | log_debug("Only the control FDT can be accessed before relocation\n"); |
Simon Glass | cb13a1b | 2022-09-06 20:27:26 -0600 | [diff] [blame] | 86 | return oftree_null(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | tree.fdt = fdt; |
| 91 | |
| 92 | return tree; |
| 93 | } |
| 94 | |
Simon Glass | 722281b | 2023-06-01 10:22:42 -0600 | [diff] [blame] | 95 | void oftree_dispose(oftree tree) |
| 96 | { |
| 97 | if (of_live_active()) |
| 98 | of_live_free(tree.np); |
| 99 | } |
| 100 | |
Simon Glass | cb13a1b | 2022-09-06 20:27:26 -0600 | [diff] [blame] | 101 | void *ofnode_lookup_fdt(ofnode node) |
| 102 | { |
| 103 | if (gd->flags & GD_FLG_RELOC) { |
| 104 | uint i = OFTREE_TREE_ID(node.of_offset); |
| 105 | |
Dan Carpenter | 408ccac | 2023-07-26 09:59:52 +0300 | [diff] [blame] | 106 | if (i >= oftree_count) { |
Simon Glass | cb13a1b | 2022-09-06 20:27:26 -0600 | [diff] [blame] | 107 | log_debug("Invalid tree ID %x\n", i); |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | return oftree_list[i]; |
| 112 | } else { |
| 113 | return (void *)gd->fdt_blob; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void *ofnode_to_fdt(ofnode node) |
| 118 | { |
| 119 | #ifdef OF_CHECKS |
| 120 | if (of_live_active()) |
| 121 | return NULL; |
| 122 | #endif |
| 123 | if (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) && ofnode_valid(node)) |
| 124 | return ofnode_lookup_fdt(node); |
| 125 | |
| 126 | /* Use the control FDT by default */ |
| 127 | return (void *)gd->fdt_blob; |
| 128 | } |
| 129 | |
Simon Glass | 45ae59d | 2022-09-06 20:27:24 -0600 | [diff] [blame] | 130 | /** |
Simon Glass | cb13a1b | 2022-09-06 20:27:26 -0600 | [diff] [blame] | 131 | * ofnode_to_offset() - convert an ofnode to a flat DT offset |
| 132 | * |
| 133 | * This cannot be called if the reference contains a node pointer. |
| 134 | * |
| 135 | * @node: Reference containing offset (possibly invalid) |
| 136 | * Return: DT offset (can be -1) |
| 137 | */ |
| 138 | int ofnode_to_offset(ofnode node) |
| 139 | { |
| 140 | #ifdef OF_CHECKS |
| 141 | if (of_live_active()) |
| 142 | return -1; |
| 143 | #endif |
| 144 | if (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) && node.of_offset >= 0) |
| 145 | return OFTREE_OFFSET(node.of_offset); |
| 146 | |
| 147 | return node.of_offset; |
| 148 | } |
| 149 | |
| 150 | oftree oftree_from_fdt(void *fdt) |
| 151 | { |
| 152 | oftree tree; |
| 153 | |
| 154 | if (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE)) |
| 155 | return oftree_ensure(fdt); |
| 156 | |
Simon Glass | 994048b | 2023-06-01 10:22:31 -0600 | [diff] [blame] | 157 | #ifdef OF_CHECKS |
| 158 | if (of_live_active()) |
| 159 | return oftree_null(); |
| 160 | #endif |
Simon Glass | cb13a1b | 2022-09-06 20:27:26 -0600 | [diff] [blame] | 161 | tree.fdt = fdt; |
| 162 | |
| 163 | return tree; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * noffset_to_ofnode() - convert a DT offset to an ofnode |
| 168 | * |
| 169 | * @other_node: Node in the same tree to use as a reference |
| 170 | * @of_offset: DT offset (either valid, or -1) |
| 171 | * Return: reference to the associated DT offset |
| 172 | */ |
| 173 | ofnode noffset_to_ofnode(ofnode other_node, int of_offset) |
| 174 | { |
| 175 | ofnode node; |
| 176 | |
| 177 | if (of_live_active()) |
| 178 | node.np = NULL; |
| 179 | else if (!CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) || of_offset < 0 || |
| 180 | !ofnode_valid(other_node)) |
| 181 | node.of_offset = of_offset; |
| 182 | else |
| 183 | node.of_offset = OFTREE_MAKE_NODE(other_node.of_offset, |
| 184 | of_offset); |
| 185 | |
| 186 | return node; |
| 187 | } |
| 188 | |
| 189 | #else /* !OFNODE_MULTI_TREE */ |
| 190 | |
| 191 | static inline int oftree_find(const void *fdt) |
| 192 | { |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | #endif /* OFNODE_MULTI_TREE */ |
| 197 | |
| 198 | /** |
Simon Glass | 45ae59d | 2022-09-06 20:27:24 -0600 | [diff] [blame] | 199 | * ofnode_from_tree_offset() - get an ofnode from a tree offset (flat tree) |
| 200 | * |
Simon Glass | cb13a1b | 2022-09-06 20:27:26 -0600 | [diff] [blame] | 201 | * Looks up the tree and returns an ofnode with the correct of_offset (i.e. |
| 202 | * containing the tree ID). |
Simon Glass | 45ae59d | 2022-09-06 20:27:24 -0600 | [diff] [blame] | 203 | * |
Simon Glass | cb13a1b | 2022-09-06 20:27:26 -0600 | [diff] [blame] | 204 | * If @offset is < 0 then this returns an ofnode with that offset and no tree |
| 205 | * ID. |
Simon Glass | 45ae59d | 2022-09-06 20:27:24 -0600 | [diff] [blame] | 206 | * |
| 207 | * @tree: tree to check |
| 208 | * @offset: offset within that tree (can be < 0) |
Simon Glass | cb13a1b | 2022-09-06 20:27:26 -0600 | [diff] [blame] | 209 | * @return node for that offset, with the correct ID |
Simon Glass | 45ae59d | 2022-09-06 20:27:24 -0600 | [diff] [blame] | 210 | */ |
| 211 | static ofnode ofnode_from_tree_offset(oftree tree, int offset) |
| 212 | { |
| 213 | ofnode node; |
| 214 | |
Simon Glass | cb13a1b | 2022-09-06 20:27:26 -0600 | [diff] [blame] | 215 | if (CONFIG_IS_ENABLED(OFNODE_MULTI_TREE) && offset >= 0) { |
| 216 | int tree_id = oftree_find(tree.fdt); |
| 217 | |
| 218 | if (tree_id == -1) |
| 219 | return ofnode_null(); |
| 220 | node.of_offset = OFTREE_NODE(tree_id, offset); |
| 221 | } else { |
| 222 | node.of_offset = offset; |
| 223 | } |
Simon Glass | 45ae59d | 2022-09-06 20:27:24 -0600 | [diff] [blame] | 224 | |
| 225 | return node; |
| 226 | } |
| 227 | |
Kishon Vijay Abraham I | d6388f2 | 2021-07-21 21:28:30 +0530 | [diff] [blame] | 228 | bool ofnode_name_eq(ofnode node, const char *name) |
| 229 | { |
| 230 | const char *node_name; |
| 231 | size_t len; |
| 232 | |
| 233 | assert(ofnode_valid(node)); |
| 234 | |
| 235 | node_name = ofnode_get_name(node); |
| 236 | len = strchrnul(node_name, '@') - node_name; |
| 237 | |
| 238 | return (strlen(name) == len) && !strncmp(node_name, name, len); |
| 239 | } |
| 240 | |
Stefan Herbrechtsmeier | 1b090e6 | 2022-06-14 15:21:30 +0200 | [diff] [blame] | 241 | int ofnode_read_u8(ofnode node, const char *propname, u8 *outp) |
| 242 | { |
| 243 | const u8 *cell; |
| 244 | int len; |
| 245 | |
| 246 | assert(ofnode_valid(node)); |
| 247 | debug("%s: %s: ", __func__, propname); |
| 248 | |
| 249 | if (ofnode_is_np(node)) |
| 250 | return of_read_u8(ofnode_to_np(node), propname, outp); |
| 251 | |
| 252 | cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname, |
| 253 | &len); |
| 254 | if (!cell || len < sizeof(*cell)) { |
| 255 | debug("(not found)\n"); |
| 256 | return -EINVAL; |
| 257 | } |
| 258 | *outp = *cell; |
| 259 | debug("%#x (%d)\n", *outp, *outp); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | u8 ofnode_read_u8_default(ofnode node, const char *propname, u8 def) |
| 265 | { |
| 266 | assert(ofnode_valid(node)); |
| 267 | ofnode_read_u8(node, propname, &def); |
| 268 | |
| 269 | return def; |
| 270 | } |
| 271 | |
| 272 | int ofnode_read_u16(ofnode node, const char *propname, u16 *outp) |
| 273 | { |
| 274 | const fdt16_t *cell; |
| 275 | int len; |
| 276 | |
| 277 | assert(ofnode_valid(node)); |
| 278 | debug("%s: %s: ", __func__, propname); |
| 279 | |
| 280 | if (ofnode_is_np(node)) |
| 281 | return of_read_u16(ofnode_to_np(node), propname, outp); |
| 282 | |
| 283 | cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname, |
| 284 | &len); |
| 285 | if (!cell || len < sizeof(*cell)) { |
| 286 | debug("(not found)\n"); |
| 287 | return -EINVAL; |
| 288 | } |
| 289 | *outp = be16_to_cpup(cell); |
| 290 | debug("%#x (%d)\n", *outp, *outp); |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | u16 ofnode_read_u16_default(ofnode node, const char *propname, u16 def) |
| 296 | { |
| 297 | assert(ofnode_valid(node)); |
| 298 | ofnode_read_u16(node, propname, &def); |
| 299 | |
| 300 | return def; |
| 301 | } |
| 302 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 303 | int ofnode_read_u32(ofnode node, const char *propname, u32 *outp) |
| 304 | { |
Dario Binacchi | b3f1cdd | 2020-03-29 18:04:42 +0200 | [diff] [blame] | 305 | return ofnode_read_u32_index(node, propname, 0, outp); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 306 | } |
| 307 | |
Trent Piepho | 5b77541 | 2019-05-10 17:48:20 +0000 | [diff] [blame] | 308 | u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 309 | { |
| 310 | assert(ofnode_valid(node)); |
Dario Binacchi | b3f1cdd | 2020-03-29 18:04:42 +0200 | [diff] [blame] | 311 | ofnode_read_u32_index(node, propname, 0, &def); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 312 | |
| 313 | return def; |
| 314 | } |
| 315 | |
Dario Binacchi | 81d80b5 | 2020-03-29 18:04:41 +0200 | [diff] [blame] | 316 | int ofnode_read_u32_index(ofnode node, const char *propname, int index, |
| 317 | u32 *outp) |
| 318 | { |
| 319 | const fdt32_t *cell; |
| 320 | int len; |
| 321 | |
| 322 | assert(ofnode_valid(node)); |
| 323 | debug("%s: %s: ", __func__, propname); |
| 324 | |
| 325 | if (ofnode_is_np(node)) |
| 326 | return of_read_u32_index(ofnode_to_np(node), propname, index, |
| 327 | outp); |
| 328 | |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 329 | cell = fdt_getprop(ofnode_to_fdt(node), ofnode_to_offset(node), |
| 330 | propname, &len); |
Dario Binacchi | 81d80b5 | 2020-03-29 18:04:41 +0200 | [diff] [blame] | 331 | if (!cell) { |
| 332 | debug("(not found)\n"); |
| 333 | return -EINVAL; |
| 334 | } |
| 335 | |
| 336 | if (len < (sizeof(int) * (index + 1))) { |
| 337 | debug("(not large enough)\n"); |
| 338 | return -EOVERFLOW; |
| 339 | } |
| 340 | |
| 341 | *outp = fdt32_to_cpu(cell[index]); |
| 342 | debug("%#x (%d)\n", *outp, *outp); |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index, |
| 348 | u32 def) |
| 349 | { |
| 350 | assert(ofnode_valid(node)); |
| 351 | ofnode_read_u32_index(node, propname, index, &def); |
| 352 | |
| 353 | return def; |
| 354 | } |
| 355 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 356 | int ofnode_read_s32_default(ofnode node, const char *propname, s32 def) |
| 357 | { |
| 358 | assert(ofnode_valid(node)); |
| 359 | ofnode_read_u32(node, propname, (u32 *)&def); |
| 360 | |
| 361 | return def; |
| 362 | } |
| 363 | |
Simon Glass | 9d54a7a | 2018-06-11 13:07:10 -0600 | [diff] [blame] | 364 | int ofnode_read_u64(ofnode node, const char *propname, u64 *outp) |
| 365 | { |
Jean-Jacques Hiblot | 487f917 | 2019-10-22 10:05:22 +0200 | [diff] [blame] | 366 | const unaligned_fdt64_t *cell; |
Simon Glass | 9d54a7a | 2018-06-11 13:07:10 -0600 | [diff] [blame] | 367 | int len; |
| 368 | |
| 369 | assert(ofnode_valid(node)); |
| 370 | debug("%s: %s: ", __func__, propname); |
| 371 | |
| 372 | if (ofnode_is_np(node)) |
| 373 | return of_read_u64(ofnode_to_np(node), propname, outp); |
| 374 | |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 375 | cell = fdt_getprop(ofnode_to_fdt(node), ofnode_to_offset(node), |
| 376 | propname, &len); |
Simon Glass | 9d54a7a | 2018-06-11 13:07:10 -0600 | [diff] [blame] | 377 | if (!cell || len < sizeof(*cell)) { |
| 378 | debug("(not found)\n"); |
| 379 | return -EINVAL; |
| 380 | } |
| 381 | *outp = fdt64_to_cpu(cell[0]); |
| 382 | debug("%#llx (%lld)\n", (unsigned long long)*outp, |
| 383 | (unsigned long long)*outp); |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
T Karthik Reddy | 478860d | 2019-09-02 16:34:30 +0200 | [diff] [blame] | 388 | u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def) |
Simon Glass | 9d54a7a | 2018-06-11 13:07:10 -0600 | [diff] [blame] | 389 | { |
| 390 | assert(ofnode_valid(node)); |
| 391 | ofnode_read_u64(node, propname, &def); |
| 392 | |
| 393 | return def; |
| 394 | } |
| 395 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 396 | bool ofnode_read_bool(ofnode node, const char *propname) |
| 397 | { |
Masahiro Yamada | 5d43445 | 2017-06-22 16:54:07 +0900 | [diff] [blame] | 398 | const void *prop; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 399 | |
| 400 | assert(ofnode_valid(node)); |
| 401 | debug("%s: %s: ", __func__, propname); |
| 402 | |
Masahiro Yamada | 5d43445 | 2017-06-22 16:54:07 +0900 | [diff] [blame] | 403 | prop = ofnode_get_property(node, propname, NULL); |
| 404 | |
| 405 | debug("%s\n", prop ? "true" : "false"); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 406 | |
Masahiro Yamada | 5d43445 | 2017-06-22 16:54:07 +0900 | [diff] [blame] | 407 | return prop ? true : false; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 408 | } |
| 409 | |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 410 | const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 411 | { |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 412 | const char *val = NULL; |
| 413 | int len; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 414 | |
| 415 | assert(ofnode_valid(node)); |
| 416 | debug("%s: %s: ", __func__, propname); |
| 417 | |
| 418 | if (ofnode_is_np(node)) { |
| 419 | struct property *prop = of_find_property( |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 420 | ofnode_to_np(node), propname, &len); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 421 | |
| 422 | if (prop) { |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 423 | val = prop->value; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 424 | len = prop->length; |
| 425 | } |
| 426 | } else { |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 427 | val = fdt_getprop(ofnode_to_fdt(node), ofnode_to_offset(node), |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 428 | propname, &len); |
| 429 | } |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 430 | if (!val) { |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 431 | debug("<not found>\n"); |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 432 | if (sizep) |
| 433 | *sizep = -FDT_ERR_NOTFOUND; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 434 | return NULL; |
| 435 | } |
Simon Glass | 0c2e980 | 2020-01-27 08:49:44 -0700 | [diff] [blame] | 436 | if (sizep) |
| 437 | *sizep = len; |
| 438 | |
| 439 | return val; |
| 440 | } |
| 441 | |
| 442 | const char *ofnode_read_string(ofnode node, const char *propname) |
| 443 | { |
| 444 | const char *str; |
| 445 | int len; |
| 446 | |
| 447 | str = ofnode_read_prop(node, propname, &len); |
| 448 | if (!str) |
| 449 | return NULL; |
| 450 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 451 | if (strnlen(str, len) >= len) { |
| 452 | debug("<invalid>\n"); |
| 453 | return NULL; |
| 454 | } |
| 455 | debug("%s\n", str); |
| 456 | |
| 457 | return str; |
| 458 | } |
| 459 | |
Simon Glass | 81c54b3 | 2020-01-27 08:49:45 -0700 | [diff] [blame] | 460 | int ofnode_read_size(ofnode node, const char *propname) |
| 461 | { |
| 462 | int len; |
| 463 | |
| 464 | if (!ofnode_read_prop(node, propname, &len)) |
| 465 | return -EINVAL; |
| 466 | |
| 467 | return len; |
| 468 | } |
| 469 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 470 | ofnode ofnode_find_subnode(ofnode node, const char *subnode_name) |
| 471 | { |
| 472 | ofnode subnode; |
| 473 | |
| 474 | assert(ofnode_valid(node)); |
| 475 | debug("%s: %s: ", __func__, subnode_name); |
| 476 | |
| 477 | if (ofnode_is_np(node)) { |
Simon Glass | 9036c5c | 2022-09-06 20:27:04 -0600 | [diff] [blame] | 478 | struct device_node *np = ofnode_to_np(node); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 479 | |
| 480 | for (np = np->child; np; np = np->sibling) { |
| 481 | if (!strcmp(subnode_name, np->name)) |
| 482 | break; |
| 483 | } |
| 484 | subnode = np_to_ofnode(np); |
| 485 | } else { |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 486 | int ooffset = fdt_subnode_offset(ofnode_to_fdt(node), |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 487 | ofnode_to_offset(node), subnode_name); |
Simon Glass | 37dcd91 | 2022-09-06 20:27:23 -0600 | [diff] [blame] | 488 | subnode = noffset_to_ofnode(node, ooffset); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 489 | } |
| 490 | debug("%s\n", ofnode_valid(subnode) ? |
| 491 | ofnode_get_name(subnode) : "<none>"); |
| 492 | |
| 493 | return subnode; |
| 494 | } |
| 495 | |
| 496 | int ofnode_read_u32_array(ofnode node, const char *propname, |
| 497 | u32 *out_values, size_t sz) |
| 498 | { |
| 499 | assert(ofnode_valid(node)); |
| 500 | debug("%s: %s: ", __func__, propname); |
| 501 | |
| 502 | if (ofnode_is_np(node)) { |
| 503 | return of_read_u32_array(ofnode_to_np(node), propname, |
| 504 | out_values, sz); |
| 505 | } else { |
Simon Glass | e3be5fc | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 506 | int ret; |
| 507 | |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 508 | ret = fdtdec_get_int_array(ofnode_to_fdt(node), |
Simon Glass | e3be5fc | 2022-09-06 20:27:18 -0600 | [diff] [blame] | 509 | ofnode_to_offset(node), propname, |
| 510 | out_values, sz); |
| 511 | |
| 512 | /* get the error right, but space is more important in SPL */ |
| 513 | if (!IS_ENABLED(CONFIG_SPL_BUILD)) { |
| 514 | if (ret == -FDT_ERR_NOTFOUND) |
| 515 | return -EINVAL; |
| 516 | else if (ret == -FDT_ERR_BADLAYOUT) |
| 517 | return -EOVERFLOW; |
| 518 | } |
| 519 | return ret; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
Simon Glass | 39f1d28 | 2020-12-16 17:25:06 -0700 | [diff] [blame] | 523 | #if !CONFIG_IS_ENABLED(DM_INLINE_OFNODE) |
Simon Glass | 5de5b3b | 2020-11-28 17:50:02 -0700 | [diff] [blame] | 524 | bool ofnode_is_enabled(ofnode node) |
| 525 | { |
| 526 | if (ofnode_is_np(node)) { |
| 527 | return of_device_is_available(ofnode_to_np(node)); |
| 528 | } else { |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 529 | return fdtdec_get_is_enabled(ofnode_to_fdt(node), |
Simon Glass | 5de5b3b | 2020-11-28 17:50:02 -0700 | [diff] [blame] | 530 | ofnode_to_offset(node)); |
| 531 | } |
| 532 | } |
| 533 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 534 | ofnode ofnode_first_subnode(ofnode node) |
| 535 | { |
| 536 | assert(ofnode_valid(node)); |
| 537 | if (ofnode_is_np(node)) |
| 538 | return np_to_ofnode(node.np->child); |
| 539 | |
Simon Glass | 37dcd91 | 2022-09-06 20:27:23 -0600 | [diff] [blame] | 540 | return noffset_to_ofnode(node, |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 541 | fdt_first_subnode(ofnode_to_fdt(node), ofnode_to_offset(node))); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | ofnode ofnode_next_subnode(ofnode node) |
| 545 | { |
| 546 | assert(ofnode_valid(node)); |
| 547 | if (ofnode_is_np(node)) |
| 548 | return np_to_ofnode(node.np->sibling); |
| 549 | |
Simon Glass | 37dcd91 | 2022-09-06 20:27:23 -0600 | [diff] [blame] | 550 | return noffset_to_ofnode(node, |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 551 | fdt_next_subnode(ofnode_to_fdt(node), ofnode_to_offset(node))); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 552 | } |
Simon Glass | 39f1d28 | 2020-12-16 17:25:06 -0700 | [diff] [blame] | 553 | #endif /* !DM_INLINE_OFNODE */ |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 554 | |
Philipp Tomsich | 6fce1dd | 2018-02-23 17:38:49 +0100 | [diff] [blame] | 555 | ofnode ofnode_get_parent(ofnode node) |
| 556 | { |
| 557 | ofnode parent; |
| 558 | |
| 559 | assert(ofnode_valid(node)); |
| 560 | if (ofnode_is_np(node)) |
| 561 | parent = np_to_ofnode(of_get_parent(ofnode_to_np(node))); |
| 562 | else |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 563 | parent.of_offset = fdt_parent_offset(ofnode_to_fdt(node), |
Philipp Tomsich | 6fce1dd | 2018-02-23 17:38:49 +0100 | [diff] [blame] | 564 | ofnode_to_offset(node)); |
| 565 | |
| 566 | return parent; |
| 567 | } |
| 568 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 569 | const char *ofnode_get_name(ofnode node) |
| 570 | { |
Kever Yang | 8d7976d | 2019-07-19 11:23:47 +0800 | [diff] [blame] | 571 | if (!ofnode_valid(node)) { |
| 572 | debug("%s node not valid\n", __func__); |
| 573 | return NULL; |
| 574 | } |
| 575 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 576 | if (ofnode_is_np(node)) |
Simon Glass | 91d89a8 | 2022-09-06 20:27:15 -0600 | [diff] [blame] | 577 | return node.np->name; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 578 | |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 579 | return fdt_get_name(ofnode_to_fdt(node), ofnode_to_offset(node), NULL); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 580 | } |
| 581 | |
Marek Behún | e897e3c | 2021-05-26 14:08:18 +0200 | [diff] [blame] | 582 | int ofnode_get_path(ofnode node, char *buf, int buflen) |
| 583 | { |
| 584 | assert(ofnode_valid(node)); |
| 585 | |
| 586 | if (ofnode_is_np(node)) { |
| 587 | if (strlen(node.np->full_name) >= buflen) |
| 588 | return -ENOSPC; |
| 589 | |
| 590 | strcpy(buf, node.np->full_name); |
| 591 | |
| 592 | return 0; |
| 593 | } else { |
| 594 | int res; |
| 595 | |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 596 | res = fdt_get_path(ofnode_to_fdt(node), ofnode_to_offset(node), buf, |
Marek Behún | e897e3c | 2021-05-26 14:08:18 +0200 | [diff] [blame] | 597 | buflen); |
| 598 | if (!res) |
| 599 | return res; |
| 600 | else if (res == -FDT_ERR_NOSPACE) |
| 601 | return -ENOSPC; |
| 602 | else |
| 603 | return -EINVAL; |
| 604 | } |
| 605 | } |
| 606 | |
Kever Yang | 37df0e0 | 2018-02-23 17:38:50 +0100 | [diff] [blame] | 607 | ofnode ofnode_get_by_phandle(uint phandle) |
| 608 | { |
| 609 | ofnode node; |
| 610 | |
| 611 | if (of_live_active()) |
Simon Glass | 176dd43 | 2022-09-06 20:26:57 -0600 | [diff] [blame] | 612 | node = np_to_ofnode(of_find_node_by_phandle(NULL, phandle)); |
Kever Yang | 37df0e0 | 2018-02-23 17:38:50 +0100 | [diff] [blame] | 613 | else |
| 614 | node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob, |
| 615 | phandle); |
| 616 | |
| 617 | return node; |
| 618 | } |
| 619 | |
Simon Glass | 95fd209 | 2022-09-06 20:27:22 -0600 | [diff] [blame] | 620 | ofnode oftree_get_by_phandle(oftree tree, uint phandle) |
| 621 | { |
| 622 | ofnode node; |
| 623 | |
| 624 | if (of_live_active()) |
| 625 | node = np_to_ofnode(of_find_node_by_phandle(tree.np, phandle)); |
| 626 | else |
Simon Glass | f912a72 | 2022-09-06 20:27:27 -0600 | [diff] [blame] | 627 | node = ofnode_from_tree_offset(tree, |
Simon Glass | 95fd209 | 2022-09-06 20:27:22 -0600 | [diff] [blame] | 628 | fdt_node_offset_by_phandle(oftree_lookup_fdt(tree), |
Simon Glass | f912a72 | 2022-09-06 20:27:27 -0600 | [diff] [blame] | 629 | phandle)); |
Simon Glass | 95fd209 | 2022-09-06 20:27:22 -0600 | [diff] [blame] | 630 | |
| 631 | return node; |
| 632 | } |
| 633 | |
Marek Behún | 177ab7f | 2021-05-26 14:08:17 +0200 | [diff] [blame] | 634 | static fdt_addr_t __ofnode_get_addr_size_index(ofnode node, int index, |
| 635 | fdt_size_t *size, bool translate) |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 636 | { |
Keerthy | 34222a3 | 2018-11-19 11:44:47 +0530 | [diff] [blame] | 637 | int na, ns; |
Keerthy | 34222a3 | 2018-11-19 11:44:47 +0530 | [diff] [blame] | 638 | |
Chen Guanqiao | 2d1034b | 2021-07-12 15:40:20 +0800 | [diff] [blame] | 639 | if (size) |
| 640 | *size = FDT_SIZE_T_NONE; |
Chen Guanqiao | 223f17d | 2021-04-12 14:51:11 +0800 | [diff] [blame] | 641 | |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 642 | if (ofnode_is_np(node)) { |
| 643 | const __be32 *prop_val; |
Simon Glass | 47f85de | 2019-09-25 08:55:50 -0600 | [diff] [blame] | 644 | u64 size64; |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 645 | uint flags; |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 646 | |
Simon Glass | 47f85de | 2019-09-25 08:55:50 -0600 | [diff] [blame] | 647 | prop_val = of_get_address(ofnode_to_np(node), index, &size64, |
| 648 | &flags); |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 649 | if (!prop_val) |
| 650 | return FDT_ADDR_T_NONE; |
Chen Guanqiao | 2d1034b | 2021-07-12 15:40:20 +0800 | [diff] [blame] | 651 | |
Simon Glass | 47f85de | 2019-09-25 08:55:50 -0600 | [diff] [blame] | 652 | if (size) |
| 653 | *size = size64; |
Mario Six | d007ebc | 2017-12-20 09:52:12 +0100 | [diff] [blame] | 654 | |
Mario Six | 35616ef | 2018-03-12 14:53:33 +0100 | [diff] [blame] | 655 | ns = of_n_size_cells(ofnode_to_np(node)); |
| 656 | |
Marek Behún | 177ab7f | 2021-05-26 14:08:17 +0200 | [diff] [blame] | 657 | if (translate && IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) { |
Mario Six | d007ebc | 2017-12-20 09:52:12 +0100 | [diff] [blame] | 658 | return of_translate_address(ofnode_to_np(node), prop_val); |
| 659 | } else { |
| 660 | na = of_n_addr_cells(ofnode_to_np(node)); |
| 661 | return of_read_number(prop_val, na); |
| 662 | } |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 663 | } else { |
Keerthy | 34222a3 | 2018-11-19 11:44:47 +0530 | [diff] [blame] | 664 | na = ofnode_read_simple_addr_cells(ofnode_get_parent(node)); |
| 665 | ns = ofnode_read_simple_size_cells(ofnode_get_parent(node)); |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 666 | return fdtdec_get_addr_size_fixed(ofnode_to_fdt(node), |
Keerthy | 34222a3 | 2018-11-19 11:44:47 +0530 | [diff] [blame] | 667 | ofnode_to_offset(node), "reg", |
Marek Behún | 177ab7f | 2021-05-26 14:08:17 +0200 | [diff] [blame] | 668 | index, na, ns, size, |
| 669 | translate); |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 670 | } |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 671 | } |
| 672 | |
Marek Behún | 177ab7f | 2021-05-26 14:08:17 +0200 | [diff] [blame] | 673 | fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size) |
| 674 | { |
| 675 | return __ofnode_get_addr_size_index(node, index, size, true); |
| 676 | } |
| 677 | |
| 678 | fdt_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index, |
| 679 | fdt_size_t *size) |
| 680 | { |
| 681 | return __ofnode_get_addr_size_index(node, index, size, false); |
| 682 | } |
| 683 | |
Keerthy | d332e6e | 2019-04-24 17:19:53 +0530 | [diff] [blame] | 684 | fdt_addr_t ofnode_get_addr_index(ofnode node, int index) |
| 685 | { |
| 686 | fdt_size_t size; |
| 687 | |
| 688 | return ofnode_get_addr_size_index(node, index, &size); |
| 689 | } |
| 690 | |
Simon Glass | 049ae1b | 2017-05-18 20:09:01 -0600 | [diff] [blame] | 691 | fdt_addr_t ofnode_get_addr(ofnode node) |
| 692 | { |
| 693 | return ofnode_get_addr_index(node, 0); |
| 694 | } |
| 695 | |
Chen Guanqiao | 223f17d | 2021-04-12 14:51:11 +0800 | [diff] [blame] | 696 | fdt_size_t ofnode_get_size(ofnode node) |
| 697 | { |
| 698 | fdt_size_t size; |
| 699 | |
| 700 | ofnode_get_addr_size_index(node, 0, &size); |
| 701 | |
| 702 | return size; |
| 703 | } |
| 704 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 705 | int ofnode_stringlist_search(ofnode node, const char *property, |
| 706 | const char *string) |
| 707 | { |
| 708 | if (ofnode_is_np(node)) { |
| 709 | return of_property_match_string(ofnode_to_np(node), |
| 710 | property, string); |
| 711 | } else { |
| 712 | int ret; |
| 713 | |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 714 | ret = fdt_stringlist_search(ofnode_to_fdt(node), |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 715 | ofnode_to_offset(node), property, |
| 716 | string); |
| 717 | if (ret == -FDT_ERR_NOTFOUND) |
| 718 | return -ENODATA; |
| 719 | else if (ret < 0) |
| 720 | return -EINVAL; |
| 721 | |
| 722 | return ret; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | int ofnode_read_string_index(ofnode node, const char *property, int index, |
| 727 | const char **outp) |
| 728 | { |
| 729 | if (ofnode_is_np(node)) { |
| 730 | return of_property_read_string_index(ofnode_to_np(node), |
| 731 | property, index, outp); |
| 732 | } else { |
| 733 | int len; |
| 734 | |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 735 | *outp = fdt_stringlist_get(ofnode_to_fdt(node), |
| 736 | ofnode_to_offset(node), |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 737 | property, index, &len); |
| 738 | if (len < 0) |
| 739 | return -EINVAL; |
| 740 | return 0; |
| 741 | } |
| 742 | } |
| 743 | |
Simon Glass | 5fdb005 | 2017-06-12 06:21:28 -0600 | [diff] [blame] | 744 | int ofnode_read_string_count(ofnode node, const char *property) |
| 745 | { |
| 746 | if (ofnode_is_np(node)) { |
| 747 | return of_property_count_strings(ofnode_to_np(node), property); |
| 748 | } else { |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 749 | return fdt_stringlist_count(ofnode_to_fdt(node), |
Simon Glass | 5fdb005 | 2017-06-12 06:21:28 -0600 | [diff] [blame] | 750 | ofnode_to_offset(node), property); |
| 751 | } |
| 752 | } |
| 753 | |
Simon Glass | 9580bfc | 2021-10-23 17:26:07 -0600 | [diff] [blame] | 754 | int ofnode_read_string_list(ofnode node, const char *property, |
| 755 | const char ***listp) |
| 756 | { |
| 757 | const char **prop; |
| 758 | int count; |
| 759 | int i; |
| 760 | |
| 761 | *listp = NULL; |
| 762 | count = ofnode_read_string_count(node, property); |
| 763 | if (count < 0) |
| 764 | return count; |
| 765 | if (!count) |
| 766 | return 0; |
| 767 | |
| 768 | prop = calloc(count + 1, sizeof(char *)); |
| 769 | if (!prop) |
| 770 | return -ENOMEM; |
| 771 | |
| 772 | for (i = 0; i < count; i++) |
| 773 | ofnode_read_string_index(node, property, i, &prop[i]); |
| 774 | prop[count] = NULL; |
| 775 | *listp = prop; |
| 776 | |
| 777 | return count; |
| 778 | } |
| 779 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 780 | static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in, |
| 781 | struct ofnode_phandle_args *out) |
| 782 | { |
| 783 | assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS); |
| 784 | out->node = offset_to_ofnode(in->node); |
| 785 | out->args_count = in->args_count; |
| 786 | memcpy(out->args, in->args, sizeof(out->args)); |
| 787 | } |
| 788 | |
| 789 | static void ofnode_from_of_phandle_args(struct of_phandle_args *in, |
| 790 | struct ofnode_phandle_args *out) |
| 791 | { |
| 792 | assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS); |
| 793 | out->node = np_to_ofnode(in->np); |
| 794 | out->args_count = in->args_count; |
| 795 | memcpy(out->args, in->args, sizeof(out->args)); |
| 796 | } |
| 797 | |
| 798 | int ofnode_parse_phandle_with_args(ofnode node, const char *list_name, |
| 799 | const char *cells_name, int cell_count, |
| 800 | int index, |
| 801 | struct ofnode_phandle_args *out_args) |
| 802 | { |
| 803 | if (ofnode_is_np(node)) { |
| 804 | struct of_phandle_args args; |
| 805 | int ret; |
| 806 | |
| 807 | ret = of_parse_phandle_with_args(ofnode_to_np(node), |
Patrick Delaunay | 21e3b04 | 2020-09-10 18:26:17 +0200 | [diff] [blame] | 808 | list_name, cells_name, |
| 809 | cell_count, index, |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 810 | &args); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 811 | if (ret) |
| 812 | return ret; |
| 813 | ofnode_from_of_phandle_args(&args, out_args); |
| 814 | } else { |
| 815 | struct fdtdec_phandle_args args; |
| 816 | int ret; |
| 817 | |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 818 | ret = fdtdec_parse_phandle_with_args(ofnode_to_fdt(node), |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 819 | ofnode_to_offset(node), |
| 820 | list_name, cells_name, |
| 821 | cell_count, index, &args); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 822 | if (ret) |
| 823 | return ret; |
| 824 | ofnode_from_fdtdec_phandle_args(&args, out_args); |
| 825 | } |
| 826 | |
| 827 | return 0; |
| 828 | } |
| 829 | |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 830 | int ofnode_count_phandle_with_args(ofnode node, const char *list_name, |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 831 | const char *cells_name, int cell_count) |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 832 | { |
| 833 | if (ofnode_is_np(node)) |
| 834 | return of_count_phandle_with_args(ofnode_to_np(node), |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 835 | list_name, cells_name, cell_count); |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 836 | else |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 837 | return fdtdec_parse_phandle_with_args(ofnode_to_fdt(node), |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 838 | ofnode_to_offset(node), list_name, cells_name, |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 839 | cell_count, -1, NULL); |
Patrice Chotard | be7dd60 | 2017-07-18 11:57:08 +0200 | [diff] [blame] | 840 | } |
| 841 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 842 | ofnode ofnode_path(const char *path) |
| 843 | { |
| 844 | if (of_live_active()) |
| 845 | return np_to_ofnode(of_find_node_by_path(path)); |
| 846 | else |
| 847 | return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path)); |
| 848 | } |
| 849 | |
Simon Glass | 45ae59d | 2022-09-06 20:27:24 -0600 | [diff] [blame] | 850 | ofnode oftree_root(oftree tree) |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 851 | { |
Simon Glass | 45ae59d | 2022-09-06 20:27:24 -0600 | [diff] [blame] | 852 | if (of_live_active()) { |
| 853 | return np_to_ofnode(tree.np); |
| 854 | } else { |
| 855 | return ofnode_from_tree_offset(tree, 0); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | ofnode oftree_path(oftree tree, const char *path) |
| 860 | { |
| 861 | if (of_live_active()) { |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 862 | return np_to_ofnode(of_find_node_opts_by_path(tree.np, path, |
| 863 | NULL)); |
Simon Glass | 45ae59d | 2022-09-06 20:27:24 -0600 | [diff] [blame] | 864 | } else if (*path != '/' && tree.fdt != gd->fdt_blob) { |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 865 | return ofnode_null(); /* Aliases only on control FDT */ |
Simon Glass | 45ae59d | 2022-09-06 20:27:24 -0600 | [diff] [blame] | 866 | } else { |
| 867 | int offset = fdt_path_offset(tree.fdt, path); |
| 868 | |
| 869 | return ofnode_from_tree_offset(tree, offset); |
| 870 | } |
Simon Glass | ef75c59 | 2022-07-30 15:52:08 -0600 | [diff] [blame] | 871 | } |
| 872 | |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 873 | const void *ofnode_read_chosen_prop(const char *propname, int *sizep) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 874 | { |
| 875 | ofnode chosen_node; |
| 876 | |
| 877 | chosen_node = ofnode_path("/chosen"); |
| 878 | |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 879 | return ofnode_read_prop(chosen_node, propname, sizep); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 880 | } |
| 881 | |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 882 | const char *ofnode_read_chosen_string(const char *propname) |
| 883 | { |
| 884 | return ofnode_read_chosen_prop(propname, NULL); |
| 885 | } |
| 886 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 887 | ofnode ofnode_get_chosen_node(const char *name) |
| 888 | { |
| 889 | const char *prop; |
| 890 | |
Simon Glass | e09223c | 2020-01-27 08:49:46 -0700 | [diff] [blame] | 891 | prop = ofnode_read_chosen_prop(name, NULL); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 892 | if (!prop) |
| 893 | return ofnode_null(); |
| 894 | |
| 895 | return ofnode_path(prop); |
| 896 | } |
| 897 | |
Michal Simek | 92a8862 | 2020-07-28 12:51:08 +0200 | [diff] [blame] | 898 | const void *ofnode_read_aliases_prop(const char *propname, int *sizep) |
| 899 | { |
| 900 | ofnode node; |
| 901 | |
| 902 | node = ofnode_path("/aliases"); |
| 903 | |
| 904 | return ofnode_read_prop(node, propname, sizep); |
| 905 | } |
| 906 | |
| 907 | ofnode ofnode_get_aliases_node(const char *name) |
| 908 | { |
| 909 | const char *prop; |
| 910 | |
| 911 | prop = ofnode_read_aliases_prop(name, NULL); |
| 912 | if (!prop) |
| 913 | return ofnode_null(); |
| 914 | |
| 915 | debug("%s: node_path: %s\n", __func__, prop); |
| 916 | |
| 917 | return ofnode_path(prop); |
| 918 | } |
| 919 | |
developer | d93c8b4 | 2020-05-02 11:35:09 +0200 | [diff] [blame] | 920 | int ofnode_get_child_count(ofnode parent) |
| 921 | { |
| 922 | ofnode child; |
| 923 | int num = 0; |
| 924 | |
| 925 | ofnode_for_each_subnode(child, parent) |
| 926 | num++; |
| 927 | |
| 928 | return num; |
| 929 | } |
| 930 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 931 | static int decode_timing_property(ofnode node, const char *name, |
| 932 | struct timing_entry *result) |
| 933 | { |
| 934 | int length, ret = 0; |
| 935 | |
| 936 | length = ofnode_read_size(node, name); |
| 937 | if (length < 0) { |
| 938 | debug("%s: could not find property %s\n", |
| 939 | ofnode_get_name(node), name); |
| 940 | return length; |
| 941 | } |
| 942 | |
| 943 | if (length == sizeof(u32)) { |
| 944 | result->typ = ofnode_read_u32_default(node, name, 0); |
| 945 | result->min = result->typ; |
| 946 | result->max = result->typ; |
| 947 | } else { |
| 948 | ret = ofnode_read_u32_array(node, name, &result->min, 3); |
| 949 | } |
| 950 | |
| 951 | return ret; |
| 952 | } |
| 953 | |
| 954 | int ofnode_decode_display_timing(ofnode parent, int index, |
| 955 | struct display_timing *dt) |
| 956 | { |
| 957 | int i; |
| 958 | ofnode timings, node; |
| 959 | u32 val = 0; |
| 960 | int ret = 0; |
| 961 | |
| 962 | timings = ofnode_find_subnode(parent, "display-timings"); |
| 963 | if (!ofnode_valid(timings)) |
| 964 | return -EINVAL; |
| 965 | |
Simon Glass | 2852976 | 2017-08-05 15:45:54 -0600 | [diff] [blame] | 966 | i = 0; |
| 967 | ofnode_for_each_subnode(node, timings) { |
| 968 | if (i++ == index) |
| 969 | break; |
| 970 | } |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 971 | |
| 972 | if (!ofnode_valid(node)) |
| 973 | return -EINVAL; |
| 974 | |
| 975 | memset(dt, 0, sizeof(*dt)); |
| 976 | |
| 977 | ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch); |
| 978 | ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch); |
| 979 | ret |= decode_timing_property(node, "hactive", &dt->hactive); |
| 980 | ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len); |
| 981 | ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch); |
| 982 | ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch); |
| 983 | ret |= decode_timing_property(node, "vactive", &dt->vactive); |
| 984 | ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len); |
| 985 | ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock); |
| 986 | |
| 987 | dt->flags = 0; |
| 988 | val = ofnode_read_u32_default(node, "vsync-active", -1); |
| 989 | if (val != -1) { |
| 990 | dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH : |
| 991 | DISPLAY_FLAGS_VSYNC_LOW; |
| 992 | } |
| 993 | val = ofnode_read_u32_default(node, "hsync-active", -1); |
| 994 | if (val != -1) { |
| 995 | dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH : |
| 996 | DISPLAY_FLAGS_HSYNC_LOW; |
| 997 | } |
| 998 | val = ofnode_read_u32_default(node, "de-active", -1); |
| 999 | if (val != -1) { |
| 1000 | dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH : |
| 1001 | DISPLAY_FLAGS_DE_LOW; |
| 1002 | } |
| 1003 | val = ofnode_read_u32_default(node, "pixelclk-active", -1); |
| 1004 | if (val != -1) { |
| 1005 | dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE : |
| 1006 | DISPLAY_FLAGS_PIXDATA_NEGEDGE; |
| 1007 | } |
| 1008 | |
| 1009 | if (ofnode_read_bool(node, "interlaced")) |
| 1010 | dt->flags |= DISPLAY_FLAGS_INTERLACED; |
| 1011 | if (ofnode_read_bool(node, "doublescan")) |
| 1012 | dt->flags |= DISPLAY_FLAGS_DOUBLESCAN; |
| 1013 | if (ofnode_read_bool(node, "doubleclk")) |
| 1014 | dt->flags |= DISPLAY_FLAGS_DOUBLECLK; |
| 1015 | |
| 1016 | return ret; |
| 1017 | } |
| 1018 | |
Nikhil M Jain | ff40706 | 2023-01-31 15:35:14 +0530 | [diff] [blame] | 1019 | int ofnode_decode_panel_timing(ofnode parent, |
| 1020 | struct display_timing *dt) |
| 1021 | { |
| 1022 | ofnode timings; |
| 1023 | u32 val = 0; |
| 1024 | int ret = 0; |
| 1025 | |
Raphael Gallais-Pou | a853b92 | 2023-05-11 16:36:52 +0200 | [diff] [blame] | 1026 | timings = ofnode_find_subnode(parent, "panel-timing"); |
Nikhil M Jain | ff40706 | 2023-01-31 15:35:14 +0530 | [diff] [blame] | 1027 | if (!ofnode_valid(timings)) |
| 1028 | return -EINVAL; |
| 1029 | memset(dt, 0, sizeof(*dt)); |
| 1030 | ret |= decode_timing_property(timings, "hback-porch", &dt->hback_porch); |
| 1031 | ret |= decode_timing_property(timings, "hfront-porch", &dt->hfront_porch); |
| 1032 | ret |= decode_timing_property(timings, "hactive", &dt->hactive); |
| 1033 | ret |= decode_timing_property(timings, "hsync-len", &dt->hsync_len); |
| 1034 | ret |= decode_timing_property(timings, "vback-porch", &dt->vback_porch); |
| 1035 | ret |= decode_timing_property(timings, "vfront-porch", &dt->vfront_porch); |
| 1036 | ret |= decode_timing_property(timings, "vactive", &dt->vactive); |
| 1037 | ret |= decode_timing_property(timings, "vsync-len", &dt->vsync_len); |
| 1038 | ret |= decode_timing_property(timings, "clock-frequency", &dt->pixelclock); |
| 1039 | dt->flags = 0; |
| 1040 | if (!ofnode_read_u32(timings, "vsync-active", &val)) { |
| 1041 | dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH : |
| 1042 | DISPLAY_FLAGS_VSYNC_LOW; |
| 1043 | } |
| 1044 | if (!ofnode_read_u32(timings, "hsync-active", &val)) { |
| 1045 | dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH : |
| 1046 | DISPLAY_FLAGS_HSYNC_LOW; |
| 1047 | } |
| 1048 | if (!ofnode_read_u32(timings, "de-active", &val)) { |
| 1049 | dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH : |
| 1050 | DISPLAY_FLAGS_DE_LOW; |
| 1051 | } |
| 1052 | if (!ofnode_read_u32(timings, "pixelclk-active", &val)) { |
| 1053 | dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE : |
| 1054 | DISPLAY_FLAGS_PIXDATA_NEGEDGE; |
| 1055 | } |
| 1056 | if (ofnode_read_bool(timings, "interlaced")) |
| 1057 | dt->flags |= DISPLAY_FLAGS_INTERLACED; |
| 1058 | if (ofnode_read_bool(timings, "doublescan")) |
| 1059 | dt->flags |= DISPLAY_FLAGS_DOUBLESCAN; |
| 1060 | if (ofnode_read_bool(timings, "doubleclk")) |
| 1061 | dt->flags |= DISPLAY_FLAGS_DOUBLECLK; |
| 1062 | |
| 1063 | return ret; |
| 1064 | } |
| 1065 | |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 1066 | const void *ofnode_get_property(ofnode node, const char *propname, int *lenp) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1067 | { |
Masahiro Yamada | 5052f1b | 2017-06-22 16:54:04 +0900 | [diff] [blame] | 1068 | if (ofnode_is_np(node)) |
| 1069 | return of_get_property(ofnode_to_np(node), propname, lenp); |
| 1070 | else |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1071 | return fdt_getprop(ofnode_to_fdt(node), ofnode_to_offset(node), |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1072 | propname, lenp); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1073 | } |
| 1074 | |
Simon Glass | fec058d | 2022-09-06 20:27:13 -0600 | [diff] [blame] | 1075 | int ofnode_first_property(ofnode node, struct ofprop *prop) |
Patrick Delaunay | caee155 | 2020-01-13 11:34:56 +0100 | [diff] [blame] | 1076 | { |
| 1077 | prop->node = node; |
| 1078 | |
| 1079 | if (ofnode_is_np(node)) { |
| 1080 | prop->prop = of_get_first_property(ofnode_to_np(prop->node)); |
| 1081 | if (!prop->prop) |
| 1082 | return -FDT_ERR_NOTFOUND; |
| 1083 | } else { |
| 1084 | prop->offset = |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1085 | fdt_first_property_offset(ofnode_to_fdt(node), |
Patrick Delaunay | caee155 | 2020-01-13 11:34:56 +0100 | [diff] [blame] | 1086 | ofnode_to_offset(prop->node)); |
| 1087 | if (prop->offset < 0) |
| 1088 | return prop->offset; |
| 1089 | } |
| 1090 | |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
Simon Glass | fec058d | 2022-09-06 20:27:13 -0600 | [diff] [blame] | 1094 | int ofnode_next_property(struct ofprop *prop) |
Patrick Delaunay | caee155 | 2020-01-13 11:34:56 +0100 | [diff] [blame] | 1095 | { |
| 1096 | if (ofnode_is_np(prop->node)) { |
| 1097 | prop->prop = of_get_next_property(ofnode_to_np(prop->node), |
| 1098 | prop->prop); |
| 1099 | if (!prop->prop) |
| 1100 | return -FDT_ERR_NOTFOUND; |
| 1101 | } else { |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1102 | prop->offset = |
| 1103 | fdt_next_property_offset(ofnode_to_fdt(prop->node), |
| 1104 | prop->offset); |
Patrick Delaunay | caee155 | 2020-01-13 11:34:56 +0100 | [diff] [blame] | 1105 | if (prop->offset < 0) |
| 1106 | return prop->offset; |
| 1107 | } |
| 1108 | |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
Simon Glass | d0aff8b | 2022-09-06 20:27:14 -0600 | [diff] [blame] | 1112 | const void *ofprop_get_property(const struct ofprop *prop, |
| 1113 | const char **propname, int *lenp) |
Patrick Delaunay | caee155 | 2020-01-13 11:34:56 +0100 | [diff] [blame] | 1114 | { |
| 1115 | if (ofnode_is_np(prop->node)) |
| 1116 | return of_get_property_by_prop(ofnode_to_np(prop->node), |
| 1117 | prop->prop, propname, lenp); |
| 1118 | else |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1119 | return fdt_getprop_by_offset(ofnode_to_fdt(prop->node), |
Patrick Delaunay | caee155 | 2020-01-13 11:34:56 +0100 | [diff] [blame] | 1120 | prop->offset, |
| 1121 | propname, lenp); |
| 1122 | } |
| 1123 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1124 | fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property, |
| 1125 | fdt_size_t *sizep) |
| 1126 | { |
| 1127 | if (ofnode_is_np(node)) { |
| 1128 | int na, ns; |
| 1129 | int psize; |
| 1130 | const struct device_node *np = ofnode_to_np(node); |
Klaus Goger | af4b021 | 2017-09-20 13:50:41 +0200 | [diff] [blame] | 1131 | const __be32 *prop = of_get_property(np, property, &psize); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1132 | |
Klaus Goger | af4b021 | 2017-09-20 13:50:41 +0200 | [diff] [blame] | 1133 | if (!prop) |
| 1134 | return FDT_ADDR_T_NONE; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1135 | na = of_n_addr_cells(np); |
Marek Vasut | 1638c17 | 2018-10-01 12:37:19 +0200 | [diff] [blame] | 1136 | ns = of_n_size_cells(np); |
Simon Glass | a67cc63 | 2017-05-18 20:09:27 -0600 | [diff] [blame] | 1137 | *sizep = of_read_number(prop + na, ns); |
Marek Vasut | a9dac49 | 2018-10-01 12:37:20 +0200 | [diff] [blame] | 1138 | |
Dario Binacchi | f8fc703 | 2021-05-01 17:05:26 +0200 | [diff] [blame] | 1139 | if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0) |
Marek Vasut | a9dac49 | 2018-10-01 12:37:20 +0200 | [diff] [blame] | 1140 | return of_translate_address(np, prop); |
| 1141 | else |
| 1142 | return of_read_number(prop, na); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1143 | } else { |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1144 | return fdtdec_get_addr_size(ofnode_to_fdt(node), |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1145 | ofnode_to_offset(node), property, |
| 1146 | sizep); |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname, |
| 1151 | size_t sz) |
| 1152 | { |
| 1153 | if (ofnode_is_np(node)) { |
| 1154 | const struct device_node *np = ofnode_to_np(node); |
| 1155 | int psize; |
| 1156 | const __be32 *prop = of_get_property(np, propname, &psize); |
| 1157 | |
| 1158 | if (!prop || sz != psize) |
| 1159 | return NULL; |
| 1160 | return (uint8_t *)prop; |
| 1161 | |
| 1162 | } else { |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1163 | return fdtdec_locate_byte_array(ofnode_to_fdt(node), |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1164 | ofnode_to_offset(node), propname, sz); |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type, |
| 1169 | const char *propname, struct fdt_pci_addr *addr) |
| 1170 | { |
Masahiro Yamada | 5c5991e | 2017-06-22 17:57:50 +0900 | [diff] [blame] | 1171 | const fdt32_t *cell; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1172 | int len; |
| 1173 | int ret = -ENOENT; |
| 1174 | |
| 1175 | debug("%s: %s: ", __func__, propname); |
| 1176 | |
| 1177 | /* |
| 1178 | * If we follow the pci bus bindings strictly, we should check |
| 1179 | * the value of the node's parent node's #address-cells and |
| 1180 | * #size-cells. They need to be 3 and 2 accordingly. However, |
| 1181 | * for simplicity we skip the check here. |
| 1182 | */ |
Masahiro Yamada | 9cf85cb | 2017-06-22 16:54:05 +0900 | [diff] [blame] | 1183 | cell = ofnode_get_property(node, propname, &len); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1184 | if (!cell) |
| 1185 | goto fail; |
| 1186 | |
| 1187 | if ((len % FDT_PCI_REG_SIZE) == 0) { |
| 1188 | int num = len / FDT_PCI_REG_SIZE; |
| 1189 | int i; |
| 1190 | |
| 1191 | for (i = 0; i < num; i++) { |
| 1192 | debug("pci address #%d: %08lx %08lx %08lx\n", i, |
| 1193 | (ulong)fdt32_to_cpu(cell[0]), |
| 1194 | (ulong)fdt32_to_cpu(cell[1]), |
| 1195 | (ulong)fdt32_to_cpu(cell[2])); |
| 1196 | if ((fdt32_to_cpu(*cell) & type) == type) { |
| 1197 | addr->phys_hi = fdt32_to_cpu(cell[0]); |
| 1198 | addr->phys_mid = fdt32_to_cpu(cell[1]); |
Simon Glass | dfd4315 | 2019-09-25 08:55:46 -0600 | [diff] [blame] | 1199 | addr->phys_lo = fdt32_to_cpu(cell[2]); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1200 | break; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1201 | } |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 1202 | |
| 1203 | cell += (FDT_PCI_ADDR_CELLS + |
| 1204 | FDT_PCI_SIZE_CELLS); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | if (i == num) { |
| 1208 | ret = -ENXIO; |
| 1209 | goto fail; |
| 1210 | } |
| 1211 | |
| 1212 | return 0; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1213 | } |
| 1214 | |
Mario Six | f40d82c | 2018-01-15 11:07:17 +0100 | [diff] [blame] | 1215 | ret = -EINVAL; |
| 1216 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1217 | fail: |
| 1218 | debug("(not found)\n"); |
| 1219 | return ret; |
| 1220 | } |
| 1221 | |
Bin Meng | fa15771 | 2018-08-03 01:14:35 -0700 | [diff] [blame] | 1222 | int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device) |
| 1223 | { |
| 1224 | const char *list, *end; |
| 1225 | int len; |
| 1226 | |
| 1227 | list = ofnode_get_property(node, "compatible", &len); |
| 1228 | if (!list) |
| 1229 | return -ENOENT; |
| 1230 | |
| 1231 | end = list + len; |
| 1232 | while (list < end) { |
| 1233 | len = strlen(list); |
| 1234 | if (len >= strlen("pciVVVV,DDDD")) { |
| 1235 | char *s = strstr(list, "pci"); |
| 1236 | |
| 1237 | /* |
| 1238 | * check if the string is something like pciVVVV,DDDD.RR |
| 1239 | * or just pciVVVV,DDDD |
| 1240 | */ |
| 1241 | if (s && s[7] == ',' && |
| 1242 | (s[12] == '.' || s[12] == 0)) { |
| 1243 | s += 3; |
| 1244 | *vendor = simple_strtol(s, NULL, 16); |
| 1245 | |
| 1246 | s += 5; |
| 1247 | *device = simple_strtol(s, NULL, 16); |
| 1248 | |
| 1249 | return 0; |
| 1250 | } |
| 1251 | } |
| 1252 | list += (len + 1); |
| 1253 | } |
| 1254 | |
| 1255 | return -ENOENT; |
| 1256 | } |
| 1257 | |
Michal Simek | a253c3b | 2022-02-23 15:45:40 +0100 | [diff] [blame] | 1258 | int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device) |
| 1259 | { |
| 1260 | const char *list, *end; |
| 1261 | int len; |
| 1262 | |
| 1263 | list = ofnode_get_property(node, "compatible", &len); |
| 1264 | |
| 1265 | if (!list) |
| 1266 | return -ENOENT; |
| 1267 | |
| 1268 | end = list + len; |
| 1269 | while (list < end) { |
| 1270 | len = strlen(list); |
| 1271 | |
Michal Simek | c633cdf | 2022-10-31 17:08:44 -0700 | [diff] [blame] | 1272 | if (len >= strlen("ethernet-phy-idVVVV.DDDD")) { |
Michal Simek | a253c3b | 2022-02-23 15:45:40 +0100 | [diff] [blame] | 1273 | char *s = strstr(list, "ethernet-phy-id"); |
| 1274 | |
| 1275 | /* |
| 1276 | * check if the string is something like |
Michal Simek | c633cdf | 2022-10-31 17:08:44 -0700 | [diff] [blame] | 1277 | * ethernet-phy-idVVVV.DDDD |
Michal Simek | a253c3b | 2022-02-23 15:45:40 +0100 | [diff] [blame] | 1278 | */ |
| 1279 | if (s && s[19] == '.') { |
| 1280 | s += strlen("ethernet-phy-id"); |
| 1281 | *vendor = simple_strtol(s, NULL, 16); |
| 1282 | s += 5; |
| 1283 | *device = simple_strtol(s, NULL, 16); |
| 1284 | |
| 1285 | return 0; |
| 1286 | } |
| 1287 | } |
| 1288 | list += (len + 1); |
| 1289 | } |
| 1290 | |
| 1291 | return -ENOENT; |
| 1292 | } |
| 1293 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1294 | int ofnode_read_addr_cells(ofnode node) |
| 1295 | { |
Heinrich Schuchardt | 60a84af | 2020-07-25 21:38:49 +0200 | [diff] [blame] | 1296 | if (ofnode_is_np(node)) { |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1297 | return of_n_addr_cells(ofnode_to_np(node)); |
Heinrich Schuchardt | 60a84af | 2020-07-25 21:38:49 +0200 | [diff] [blame] | 1298 | } else { |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1299 | int parent = fdt_parent_offset(ofnode_to_fdt(node), |
Heinrich Schuchardt | 60a84af | 2020-07-25 21:38:49 +0200 | [diff] [blame] | 1300 | ofnode_to_offset(node)); |
| 1301 | |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1302 | return fdt_address_cells(ofnode_to_fdt(node), parent); |
Heinrich Schuchardt | 60a84af | 2020-07-25 21:38:49 +0200 | [diff] [blame] | 1303 | } |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | int ofnode_read_size_cells(ofnode node) |
| 1307 | { |
Heinrich Schuchardt | 60a84af | 2020-07-25 21:38:49 +0200 | [diff] [blame] | 1308 | if (ofnode_is_np(node)) { |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1309 | return of_n_size_cells(ofnode_to_np(node)); |
Heinrich Schuchardt | 60a84af | 2020-07-25 21:38:49 +0200 | [diff] [blame] | 1310 | } else { |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1311 | int parent = fdt_parent_offset(ofnode_to_fdt(node), |
Heinrich Schuchardt | 60a84af | 2020-07-25 21:38:49 +0200 | [diff] [blame] | 1312 | ofnode_to_offset(node)); |
| 1313 | |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1314 | return fdt_size_cells(ofnode_to_fdt(node), parent); |
Heinrich Schuchardt | 60a84af | 2020-07-25 21:38:49 +0200 | [diff] [blame] | 1315 | } |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | int ofnode_read_simple_addr_cells(ofnode node) |
| 1319 | { |
| 1320 | if (ofnode_is_np(node)) |
| 1321 | return of_simple_addr_cells(ofnode_to_np(node)); |
| 1322 | else |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1323 | return fdt_address_cells(ofnode_to_fdt(node), |
| 1324 | ofnode_to_offset(node)); |
Simon Glass | 4191dc1 | 2017-06-12 06:21:31 -0600 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | int ofnode_read_simple_size_cells(ofnode node) |
| 1328 | { |
| 1329 | if (ofnode_is_np(node)) |
| 1330 | return of_simple_size_cells(ofnode_to_np(node)); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1331 | else |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1332 | return fdt_size_cells(ofnode_to_fdt(node), |
| 1333 | ofnode_to_offset(node)); |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | bool ofnode_pre_reloc(ofnode node) |
| 1337 | { |
Patrick Delaunay | 0b025b8 | 2019-02-11 12:49:57 +0100 | [diff] [blame] | 1338 | #if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD) |
| 1339 | /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass |
Simon Glass | fc1aa35 | 2023-02-13 08:56:34 -0700 | [diff] [blame] | 1340 | * had property bootph-all or bootph-pre-sram/bootph-pre-ram. |
Patrick Delaunay | 0b025b8 | 2019-02-11 12:49:57 +0100 | [diff] [blame] | 1341 | * They are removed in final dtb (fdtgrep 2nd pass) |
| 1342 | */ |
| 1343 | return true; |
| 1344 | #else |
Simon Glass | fc1aa35 | 2023-02-13 08:56:34 -0700 | [diff] [blame] | 1345 | if (ofnode_read_bool(node, "bootph-all")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1346 | return true; |
Simon Glass | fc1aa35 | 2023-02-13 08:56:34 -0700 | [diff] [blame] | 1347 | if (ofnode_read_bool(node, "bootph-some-ram")) |
Simon Glass | 23f2284 | 2018-10-01 12:22:18 -0600 | [diff] [blame] | 1348 | return true; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1349 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1350 | /* |
| 1351 | * In regular builds individual spl and tpl handling both |
| 1352 | * count as handled pre-relocation for later second init. |
| 1353 | */ |
Simon Glass | fc1aa35 | 2023-02-13 08:56:34 -0700 | [diff] [blame] | 1354 | if (ofnode_read_bool(node, "bootph-pre-ram") || |
| 1355 | ofnode_read_bool(node, "bootph-pre-sram")) |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1356 | return true; |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1357 | |
Simon Glass | c2727e5 | 2023-02-13 08:56:32 -0700 | [diff] [blame] | 1358 | if (IS_ENABLED(CONFIG_OF_TAG_MIGRATE)) { |
| 1359 | /* detect and handle old tags */ |
| 1360 | if (ofnode_read_bool(node, "u-boot,dm-pre-reloc") || |
| 1361 | ofnode_read_bool(node, "u-boot,dm-pre-proper") || |
| 1362 | ofnode_read_bool(node, "u-boot,dm-spl") || |
| 1363 | ofnode_read_bool(node, "u-boot,dm-tpl") || |
| 1364 | ofnode_read_bool(node, "u-boot,dm-vpl")) { |
| 1365 | gd->flags |= GD_FLG_OF_TAG_MIGRATE; |
| 1366 | return true; |
| 1367 | } |
| 1368 | } |
| 1369 | |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1370 | return false; |
Patrick Delaunay | 0b025b8 | 2019-02-11 12:49:57 +0100 | [diff] [blame] | 1371 | #endif |
Simon Glass | c4fc562 | 2017-05-18 20:08:58 -0600 | [diff] [blame] | 1372 | } |
Simon Glass | f7bfcc4 | 2017-07-25 08:29:55 -0600 | [diff] [blame] | 1373 | |
| 1374 | int ofnode_read_resource(ofnode node, uint index, struct resource *res) |
| 1375 | { |
| 1376 | if (ofnode_is_np(node)) { |
| 1377 | return of_address_to_resource(ofnode_to_np(node), index, res); |
| 1378 | } else { |
| 1379 | struct fdt_resource fres; |
| 1380 | int ret; |
| 1381 | |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1382 | ret = fdt_get_resource(ofnode_to_fdt(node), |
| 1383 | ofnode_to_offset(node), |
Simon Glass | f7bfcc4 | 2017-07-25 08:29:55 -0600 | [diff] [blame] | 1384 | "reg", index, &fres); |
| 1385 | if (ret < 0) |
| 1386 | return -EINVAL; |
| 1387 | memset(res, '\0', sizeof(*res)); |
| 1388 | res->start = fres.start; |
| 1389 | res->end = fres.end; |
| 1390 | |
| 1391 | return 0; |
| 1392 | } |
| 1393 | } |
Masahiro Yamada | 4dada2c | 2017-08-26 01:12:30 +0900 | [diff] [blame] | 1394 | |
| 1395 | int ofnode_read_resource_byname(ofnode node, const char *name, |
| 1396 | struct resource *res) |
| 1397 | { |
| 1398 | int index; |
| 1399 | |
| 1400 | index = ofnode_stringlist_search(node, "reg-names", name); |
| 1401 | if (index < 0) |
| 1402 | return index; |
| 1403 | |
| 1404 | return ofnode_read_resource(node, index, res); |
| 1405 | } |
Mario Six | aefac06 | 2018-01-15 11:07:19 +0100 | [diff] [blame] | 1406 | |
| 1407 | u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr) |
| 1408 | { |
| 1409 | if (ofnode_is_np(node)) |
| 1410 | return of_translate_address(ofnode_to_np(node), in_addr); |
| 1411 | else |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1412 | return fdt_translate_address(ofnode_to_fdt(node), |
| 1413 | ofnode_to_offset(node), in_addr); |
Mario Six | aefac06 | 2018-01-15 11:07:19 +0100 | [diff] [blame] | 1414 | } |
Masahiro Yamada | 9349bcc | 2018-04-19 12:14:02 +0900 | [diff] [blame] | 1415 | |
Fabien Dessenne | 22236e0 | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 1416 | u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr) |
| 1417 | { |
| 1418 | if (ofnode_is_np(node)) |
| 1419 | return of_translate_dma_address(ofnode_to_np(node), in_addr); |
| 1420 | else |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1421 | return fdt_translate_dma_address(ofnode_to_fdt(node), |
| 1422 | ofnode_to_offset(node), in_addr); |
Fabien Dessenne | 22236e0 | 2019-05-31 15:11:30 +0200 | [diff] [blame] | 1423 | } |
| 1424 | |
Nicolas Saenz Julienne | 50d2fa4 | 2021-01-12 13:55:22 +0100 | [diff] [blame] | 1425 | int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus, u64 *size) |
| 1426 | { |
| 1427 | if (ofnode_is_np(node)) |
| 1428 | return of_get_dma_range(ofnode_to_np(node), cpu, bus, size); |
| 1429 | else |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1430 | return fdt_get_dma_range(ofnode_to_fdt(node), |
| 1431 | ofnode_to_offset(node), |
Nicolas Saenz Julienne | 50d2fa4 | 2021-01-12 13:55:22 +0100 | [diff] [blame] | 1432 | cpu, bus, size); |
| 1433 | } |
| 1434 | |
Masahiro Yamada | 9349bcc | 2018-04-19 12:14:02 +0900 | [diff] [blame] | 1435 | int ofnode_device_is_compatible(ofnode node, const char *compat) |
| 1436 | { |
| 1437 | if (ofnode_is_np(node)) |
| 1438 | return of_device_is_compatible(ofnode_to_np(node), compat, |
| 1439 | NULL, NULL); |
| 1440 | else |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1441 | return !fdt_node_check_compatible(ofnode_to_fdt(node), |
Masahiro Yamada | 9349bcc | 2018-04-19 12:14:02 +0900 | [diff] [blame] | 1442 | ofnode_to_offset(node), |
| 1443 | compat); |
| 1444 | } |
Simon Glass | 954eeae | 2018-06-11 13:07:13 -0600 | [diff] [blame] | 1445 | |
| 1446 | ofnode ofnode_by_compatible(ofnode from, const char *compat) |
| 1447 | { |
| 1448 | if (of_live_active()) { |
| 1449 | return np_to_ofnode(of_find_compatible_node( |
| 1450 | (struct device_node *)ofnode_to_np(from), NULL, |
| 1451 | compat)); |
| 1452 | } else { |
Simon Glass | 37dcd91 | 2022-09-06 20:27:23 -0600 | [diff] [blame] | 1453 | return noffset_to_ofnode(from, |
| 1454 | fdt_node_offset_by_compatible(ofnode_to_fdt(from), |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1455 | ofnode_to_offset(from), compat)); |
Simon Glass | 954eeae | 2018-06-11 13:07:13 -0600 | [diff] [blame] | 1456 | } |
| 1457 | } |
Jens Wiklander | 7b68dad | 2018-08-20 11:09:58 +0200 | [diff] [blame] | 1458 | |
| 1459 | ofnode ofnode_by_prop_value(ofnode from, const char *propname, |
| 1460 | const void *propval, int proplen) |
| 1461 | { |
| 1462 | if (of_live_active()) { |
| 1463 | return np_to_ofnode(of_find_node_by_prop_value( |
| 1464 | (struct device_node *)ofnode_to_np(from), propname, |
| 1465 | propval, proplen)); |
| 1466 | } else { |
Simon Glass | 37dcd91 | 2022-09-06 20:27:23 -0600 | [diff] [blame] | 1467 | return noffset_to_ofnode(from, |
| 1468 | fdt_node_offset_by_prop_value(ofnode_to_fdt(from), |
| 1469 | ofnode_to_offset(from), propname, propval, |
| 1470 | proplen)); |
Jens Wiklander | 7b68dad | 2018-08-20 11:09:58 +0200 | [diff] [blame] | 1471 | } |
| 1472 | } |
Mario Six | 047dafc | 2018-06-26 08:46:48 +0200 | [diff] [blame] | 1473 | |
Simon Glass | 5e2cd5e | 2022-07-30 15:52:10 -0600 | [diff] [blame] | 1474 | int ofnode_write_prop(ofnode node, const char *propname, const void *value, |
Simon Glass | 17abed0 | 2022-09-06 20:27:32 -0600 | [diff] [blame] | 1475 | int len, bool copy) |
Mario Six | 047dafc | 2018-06-26 08:46:48 +0200 | [diff] [blame] | 1476 | { |
Simon Glass | 17abed0 | 2022-09-06 20:27:32 -0600 | [diff] [blame] | 1477 | if (of_live_active()) { |
| 1478 | void *newval; |
| 1479 | int ret; |
| 1480 | |
| 1481 | if (copy) { |
| 1482 | newval = malloc(len); |
| 1483 | if (!newval) |
| 1484 | return log_ret(-ENOMEM); |
| 1485 | memcpy(newval, value, len); |
| 1486 | value = newval; |
| 1487 | } |
| 1488 | ret = of_write_prop(ofnode_to_np(node), propname, len, value); |
| 1489 | if (ret && copy) |
| 1490 | free(newval); |
| 1491 | return ret; |
| 1492 | } else { |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1493 | return fdt_setprop(ofnode_to_fdt(node), ofnode_to_offset(node), |
Simon Glass | 3ee3d15 | 2022-07-30 15:52:13 -0600 | [diff] [blame] | 1494 | propname, value, len); |
Simon Glass | 17abed0 | 2022-09-06 20:27:32 -0600 | [diff] [blame] | 1495 | } |
Mario Six | 047dafc | 2018-06-26 08:46:48 +0200 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | int ofnode_write_string(ofnode node, const char *propname, const char *value) |
| 1499 | { |
Mario Six | 047dafc | 2018-06-26 08:46:48 +0200 | [diff] [blame] | 1500 | assert(ofnode_valid(node)); |
| 1501 | |
| 1502 | debug("%s: %s = %s", __func__, propname, value); |
| 1503 | |
Simon Glass | 17abed0 | 2022-09-06 20:27:32 -0600 | [diff] [blame] | 1504 | return ofnode_write_prop(node, propname, value, strlen(value) + 1, |
| 1505 | false); |
Mario Six | 047dafc | 2018-06-26 08:46:48 +0200 | [diff] [blame] | 1506 | } |
| 1507 | |
Simon Glass | d28e31e | 2022-07-30 15:52:14 -0600 | [diff] [blame] | 1508 | int ofnode_write_u32(ofnode node, const char *propname, u32 value) |
| 1509 | { |
| 1510 | fdt32_t *val; |
| 1511 | |
| 1512 | assert(ofnode_valid(node)); |
| 1513 | |
| 1514 | log_debug("%s = %x", propname, value); |
| 1515 | val = malloc(sizeof(*val)); |
| 1516 | if (!val) |
| 1517 | return -ENOMEM; |
| 1518 | *val = cpu_to_fdt32(value); |
| 1519 | |
Simon Glass | 17abed0 | 2022-09-06 20:27:32 -0600 | [diff] [blame] | 1520 | return ofnode_write_prop(node, propname, val, sizeof(value), false); |
Simon Glass | d28e31e | 2022-07-30 15:52:14 -0600 | [diff] [blame] | 1521 | } |
| 1522 | |
Mario Six | 047dafc | 2018-06-26 08:46:48 +0200 | [diff] [blame] | 1523 | int ofnode_set_enabled(ofnode node, bool value) |
| 1524 | { |
Mario Six | 047dafc | 2018-06-26 08:46:48 +0200 | [diff] [blame] | 1525 | assert(ofnode_valid(node)); |
| 1526 | |
| 1527 | if (value) |
| 1528 | return ofnode_write_string(node, "status", "okay"); |
| 1529 | else |
Bin Meng | a9274aa | 2019-07-05 09:23:17 -0700 | [diff] [blame] | 1530 | return ofnode_write_string(node, "status", "disabled"); |
Mario Six | 047dafc | 2018-06-26 08:46:48 +0200 | [diff] [blame] | 1531 | } |
Simon Glass | 0034d96 | 2021-08-07 07:24:01 -0600 | [diff] [blame] | 1532 | |
| 1533 | bool ofnode_conf_read_bool(const char *prop_name) |
| 1534 | { |
| 1535 | ofnode node; |
| 1536 | |
| 1537 | node = ofnode_path("/config"); |
| 1538 | if (!ofnode_valid(node)) |
| 1539 | return false; |
| 1540 | |
| 1541 | return ofnode_read_bool(node, prop_name); |
| 1542 | } |
| 1543 | |
| 1544 | int ofnode_conf_read_int(const char *prop_name, int default_val) |
| 1545 | { |
| 1546 | ofnode node; |
| 1547 | |
| 1548 | node = ofnode_path("/config"); |
| 1549 | if (!ofnode_valid(node)) |
| 1550 | return default_val; |
| 1551 | |
| 1552 | return ofnode_read_u32_default(node, prop_name, default_val); |
| 1553 | } |
| 1554 | |
| 1555 | const char *ofnode_conf_read_str(const char *prop_name) |
| 1556 | { |
| 1557 | ofnode node; |
| 1558 | |
| 1559 | node = ofnode_path("/config"); |
| 1560 | if (!ofnode_valid(node)) |
| 1561 | return NULL; |
| 1562 | |
| 1563 | return ofnode_read_string(node, prop_name); |
| 1564 | } |
Marek Behún | f4f1ddc | 2022-04-07 00:32:57 +0200 | [diff] [blame] | 1565 | |
| 1566 | ofnode ofnode_get_phy_node(ofnode node) |
| 1567 | { |
| 1568 | /* DT node properties that reference a PHY node */ |
| 1569 | static const char * const phy_handle_str[] = { |
| 1570 | "phy-handle", "phy", "phy-device", |
| 1571 | }; |
| 1572 | struct ofnode_phandle_args args = { |
| 1573 | .node = ofnode_null() |
| 1574 | }; |
| 1575 | int i; |
| 1576 | |
| 1577 | assert(ofnode_valid(node)); |
| 1578 | |
| 1579 | for (i = 0; i < ARRAY_SIZE(phy_handle_str); i++) |
| 1580 | if (!ofnode_parse_phandle_with_args(node, phy_handle_str[i], |
| 1581 | NULL, 0, 0, &args)) |
| 1582 | break; |
| 1583 | |
| 1584 | return args.node; |
| 1585 | } |
Marek Behún | bc19477 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 1586 | |
| 1587 | phy_interface_t ofnode_read_phy_mode(ofnode node) |
| 1588 | { |
| 1589 | const char *mode; |
| 1590 | int i; |
| 1591 | |
| 1592 | assert(ofnode_valid(node)); |
| 1593 | |
| 1594 | mode = ofnode_read_string(node, "phy-mode"); |
| 1595 | if (!mode) |
| 1596 | mode = ofnode_read_string(node, "phy-connection-type"); |
| 1597 | |
| 1598 | if (!mode) |
Marek Behún | 48631e4 | 2022-04-07 00:33:03 +0200 | [diff] [blame] | 1599 | return PHY_INTERFACE_MODE_NA; |
Marek Behún | bc19477 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 1600 | |
Marek Behún | 21a1836 | 2022-04-07 00:33:02 +0200 | [diff] [blame] | 1601 | for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++) |
Marek Behún | bc19477 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 1602 | if (!strcmp(mode, phy_interface_strings[i])) |
| 1603 | return i; |
| 1604 | |
| 1605 | debug("%s: Invalid PHY interface '%s'\n", __func__, mode); |
| 1606 | |
Marek Behún | 48631e4 | 2022-04-07 00:33:03 +0200 | [diff] [blame] | 1607 | return PHY_INTERFACE_MODE_NA; |
Marek Behún | bc19477 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 1608 | } |
Simon Glass | 56bc332 | 2022-09-06 20:27:02 -0600 | [diff] [blame] | 1609 | |
| 1610 | int ofnode_add_subnode(ofnode node, const char *name, ofnode *subnodep) |
| 1611 | { |
| 1612 | ofnode subnode; |
| 1613 | int ret = 0; |
| 1614 | |
| 1615 | assert(ofnode_valid(node)); |
| 1616 | |
| 1617 | if (ofnode_is_np(node)) { |
| 1618 | struct device_node *np, *child; |
| 1619 | |
| 1620 | np = (struct device_node *)ofnode_to_np(node); |
| 1621 | ret = of_add_subnode(np, name, -1, &child); |
| 1622 | if (ret && ret != -EEXIST) |
| 1623 | return ret; |
| 1624 | subnode = np_to_ofnode(child); |
| 1625 | } else { |
Simon Glass | 04fa09a | 2022-09-06 20:27:20 -0600 | [diff] [blame] | 1626 | void *fdt = ofnode_to_fdt(node); |
Simon Glass | 56bc332 | 2022-09-06 20:27:02 -0600 | [diff] [blame] | 1627 | int poffset = ofnode_to_offset(node); |
| 1628 | int offset; |
| 1629 | |
| 1630 | offset = fdt_add_subnode(fdt, poffset, name); |
| 1631 | if (offset == -FDT_ERR_EXISTS) { |
| 1632 | offset = fdt_subnode_offset(fdt, poffset, name); |
| 1633 | ret = -EEXIST; |
| 1634 | } |
| 1635 | if (offset < 0) |
| 1636 | return -EINVAL; |
Simon Glass | 37dcd91 | 2022-09-06 20:27:23 -0600 | [diff] [blame] | 1637 | subnode = noffset_to_ofnode(node, offset); |
Simon Glass | 56bc332 | 2022-09-06 20:27:02 -0600 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | *subnodep = subnode; |
| 1641 | |
| 1642 | return ret; /* 0 or -EEXIST */ |
| 1643 | } |
Simon Glass | 7a7229a | 2022-09-06 20:27:33 -0600 | [diff] [blame] | 1644 | |
| 1645 | int ofnode_copy_props(ofnode src, ofnode dst) |
| 1646 | { |
| 1647 | struct ofprop prop; |
| 1648 | |
| 1649 | ofnode_for_each_prop(prop, src) { |
| 1650 | const char *name; |
| 1651 | const char *val; |
| 1652 | int len, ret; |
| 1653 | |
| 1654 | val = ofprop_get_property(&prop, &name, &len); |
| 1655 | if (!val) { |
| 1656 | log_debug("Cannot read prop (err=%d)\n", len); |
| 1657 | return log_msg_ret("get", -EINVAL); |
| 1658 | } |
| 1659 | ret = ofnode_write_prop(dst, name, val, len, true); |
| 1660 | if (ret) { |
| 1661 | log_debug("Cannot write prop (err=%d)\n", ret); |
| 1662 | return log_msg_ret("wr", -EINVAL); |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | return 0; |
| 1667 | } |