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