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