blob: bd41ef503c2067e2ad86ca08951f3b5fba6c8669 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassc4fc5622017-05-18 20:08:58 -06002/*
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassc4fc5622017-05-18 20:08:58 -06005 */
6
7#include <common.h>
8#include <dm.h>
9#include <fdtdec.h>
10#include <fdt_support.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090013#include <linux/libfdt.h>
Simon Glassc4fc5622017-05-18 20:08:58 -060014#include <dm/of_access.h>
Simon Glass049ae1b2017-05-18 20:09:01 -060015#include <dm/of_addr.h>
Simon Glassc4fc5622017-05-18 20:08:58 -060016#include <dm/ofnode.h>
17#include <linux/err.h>
Simon Glassf7bfcc42017-07-25 08:29:55 -060018#include <linux/ioport.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060019#include <asm/global_data.h>
Simon Glassc4fc5622017-05-18 20:08:58 -060020
Kishon Vijay Abraham Id6388f22021-07-21 21:28:30 +053021bool ofnode_name_eq(ofnode node, const char *name)
22{
23 const char *node_name;
24 size_t len;
25
26 assert(ofnode_valid(node));
27
28 node_name = ofnode_get_name(node);
29 len = strchrnul(node_name, '@') - node_name;
30
31 return (strlen(name) == len) && !strncmp(node_name, name, len);
32}
33
Simon Glassc4fc5622017-05-18 20:08:58 -060034int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
35{
Dario Binacchib3f1cdd2020-03-29 18:04:42 +020036 return ofnode_read_u32_index(node, propname, 0, outp);
Simon Glassc4fc5622017-05-18 20:08:58 -060037}
38
Trent Piepho5b775412019-05-10 17:48:20 +000039u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
Simon Glassc4fc5622017-05-18 20:08:58 -060040{
41 assert(ofnode_valid(node));
Dario Binacchib3f1cdd2020-03-29 18:04:42 +020042 ofnode_read_u32_index(node, propname, 0, &def);
Simon Glassc4fc5622017-05-18 20:08:58 -060043
44 return def;
45}
46
Dario Binacchi81d80b52020-03-29 18:04:41 +020047int ofnode_read_u32_index(ofnode node, const char *propname, int index,
48 u32 *outp)
49{
50 const fdt32_t *cell;
51 int len;
52
53 assert(ofnode_valid(node));
54 debug("%s: %s: ", __func__, propname);
55
56 if (ofnode_is_np(node))
57 return of_read_u32_index(ofnode_to_np(node), propname, index,
58 outp);
59
60 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
61 &len);
62 if (!cell) {
63 debug("(not found)\n");
64 return -EINVAL;
65 }
66
67 if (len < (sizeof(int) * (index + 1))) {
68 debug("(not large enough)\n");
69 return -EOVERFLOW;
70 }
71
72 *outp = fdt32_to_cpu(cell[index]);
73 debug("%#x (%d)\n", *outp, *outp);
74
75 return 0;
76}
77
78u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
79 u32 def)
80{
81 assert(ofnode_valid(node));
82 ofnode_read_u32_index(node, propname, index, &def);
83
84 return def;
85}
86
Simon Glassc4fc5622017-05-18 20:08:58 -060087int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
88{
89 assert(ofnode_valid(node));
90 ofnode_read_u32(node, propname, (u32 *)&def);
91
92 return def;
93}
94
Simon Glass9d54a7a2018-06-11 13:07:10 -060095int ofnode_read_u64(ofnode node, const char *propname, u64 *outp)
96{
Jean-Jacques Hiblot487f9172019-10-22 10:05:22 +020097 const unaligned_fdt64_t *cell;
Simon Glass9d54a7a2018-06-11 13:07:10 -060098 int len;
99
100 assert(ofnode_valid(node));
101 debug("%s: %s: ", __func__, propname);
102
103 if (ofnode_is_np(node))
104 return of_read_u64(ofnode_to_np(node), propname, outp);
105
106 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
107 &len);
108 if (!cell || len < sizeof(*cell)) {
109 debug("(not found)\n");
110 return -EINVAL;
111 }
112 *outp = fdt64_to_cpu(cell[0]);
113 debug("%#llx (%lld)\n", (unsigned long long)*outp,
114 (unsigned long long)*outp);
115
116 return 0;
117}
118
T Karthik Reddy478860d2019-09-02 16:34:30 +0200119u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def)
Simon Glass9d54a7a2018-06-11 13:07:10 -0600120{
121 assert(ofnode_valid(node));
122 ofnode_read_u64(node, propname, &def);
123
124 return def;
125}
126
Simon Glassc4fc5622017-05-18 20:08:58 -0600127bool ofnode_read_bool(ofnode node, const char *propname)
128{
Masahiro Yamada5d434452017-06-22 16:54:07 +0900129 const void *prop;
Simon Glassc4fc5622017-05-18 20:08:58 -0600130
131 assert(ofnode_valid(node));
132 debug("%s: %s: ", __func__, propname);
133
Masahiro Yamada5d434452017-06-22 16:54:07 +0900134 prop = ofnode_get_property(node, propname, NULL);
135
136 debug("%s\n", prop ? "true" : "false");
Simon Glassc4fc5622017-05-18 20:08:58 -0600137
Masahiro Yamada5d434452017-06-22 16:54:07 +0900138 return prop ? true : false;
Simon Glassc4fc5622017-05-18 20:08:58 -0600139}
140
Simon Glass0c2e9802020-01-27 08:49:44 -0700141const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep)
Simon Glassc4fc5622017-05-18 20:08:58 -0600142{
Simon Glass0c2e9802020-01-27 08:49:44 -0700143 const char *val = NULL;
144 int len;
Simon Glassc4fc5622017-05-18 20:08:58 -0600145
146 assert(ofnode_valid(node));
147 debug("%s: %s: ", __func__, propname);
148
149 if (ofnode_is_np(node)) {
150 struct property *prop = of_find_property(
Simon Glass0c2e9802020-01-27 08:49:44 -0700151 ofnode_to_np(node), propname, &len);
Simon Glassc4fc5622017-05-18 20:08:58 -0600152
153 if (prop) {
Simon Glass0c2e9802020-01-27 08:49:44 -0700154 val = prop->value;
Simon Glassc4fc5622017-05-18 20:08:58 -0600155 len = prop->length;
156 }
157 } else {
Simon Glass0c2e9802020-01-27 08:49:44 -0700158 val = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
Simon Glassc4fc5622017-05-18 20:08:58 -0600159 propname, &len);
160 }
Simon Glass0c2e9802020-01-27 08:49:44 -0700161 if (!val) {
Simon Glassc4fc5622017-05-18 20:08:58 -0600162 debug("<not found>\n");
Simon Glass0c2e9802020-01-27 08:49:44 -0700163 if (sizep)
164 *sizep = -FDT_ERR_NOTFOUND;
Simon Glassc4fc5622017-05-18 20:08:58 -0600165 return NULL;
166 }
Simon Glass0c2e9802020-01-27 08:49:44 -0700167 if (sizep)
168 *sizep = len;
169
170 return val;
171}
172
173const char *ofnode_read_string(ofnode node, const char *propname)
174{
175 const char *str;
176 int len;
177
178 str = ofnode_read_prop(node, propname, &len);
179 if (!str)
180 return NULL;
181
Simon Glassc4fc5622017-05-18 20:08:58 -0600182 if (strnlen(str, len) >= len) {
183 debug("<invalid>\n");
184 return NULL;
185 }
186 debug("%s\n", str);
187
188 return str;
189}
190
Simon Glass81c54b32020-01-27 08:49:45 -0700191int ofnode_read_size(ofnode node, const char *propname)
192{
193 int len;
194
195 if (!ofnode_read_prop(node, propname, &len))
196 return -EINVAL;
197
198 return len;
199}
200
Simon Glassc4fc5622017-05-18 20:08:58 -0600201ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
202{
203 ofnode subnode;
204
205 assert(ofnode_valid(node));
206 debug("%s: %s: ", __func__, subnode_name);
207
208 if (ofnode_is_np(node)) {
209 const struct device_node *np = ofnode_to_np(node);
210
211 for (np = np->child; np; np = np->sibling) {
212 if (!strcmp(subnode_name, np->name))
213 break;
214 }
215 subnode = np_to_ofnode(np);
216 } else {
217 int ooffset = fdt_subnode_offset(gd->fdt_blob,
218 ofnode_to_offset(node), subnode_name);
219 subnode = offset_to_ofnode(ooffset);
220 }
221 debug("%s\n", ofnode_valid(subnode) ?
222 ofnode_get_name(subnode) : "<none>");
223
224 return subnode;
225}
226
227int ofnode_read_u32_array(ofnode node, const char *propname,
228 u32 *out_values, size_t sz)
229{
230 assert(ofnode_valid(node));
231 debug("%s: %s: ", __func__, propname);
232
233 if (ofnode_is_np(node)) {
234 return of_read_u32_array(ofnode_to_np(node), propname,
235 out_values, sz);
236 } else {
237 return fdtdec_get_int_array(gd->fdt_blob,
238 ofnode_to_offset(node), propname,
239 out_values, sz);
240 }
241}
242
Simon Glass39f1d282020-12-16 17:25:06 -0700243#if !CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
Simon Glass5de5b3b2020-11-28 17:50:02 -0700244bool ofnode_is_enabled(ofnode node)
245{
246 if (ofnode_is_np(node)) {
247 return of_device_is_available(ofnode_to_np(node));
248 } else {
249 return fdtdec_get_is_enabled(gd->fdt_blob,
250 ofnode_to_offset(node));
251 }
252}
253
Simon Glassc4fc5622017-05-18 20:08:58 -0600254ofnode ofnode_first_subnode(ofnode node)
255{
256 assert(ofnode_valid(node));
257 if (ofnode_is_np(node))
258 return np_to_ofnode(node.np->child);
259
260 return offset_to_ofnode(
261 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
262}
263
264ofnode ofnode_next_subnode(ofnode node)
265{
266 assert(ofnode_valid(node));
267 if (ofnode_is_np(node))
268 return np_to_ofnode(node.np->sibling);
269
270 return offset_to_ofnode(
271 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
272}
Simon Glass39f1d282020-12-16 17:25:06 -0700273#endif /* !DM_INLINE_OFNODE */
Simon Glassc4fc5622017-05-18 20:08:58 -0600274
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100275ofnode ofnode_get_parent(ofnode node)
276{
277 ofnode parent;
278
279 assert(ofnode_valid(node));
280 if (ofnode_is_np(node))
281 parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
282 else
283 parent.of_offset = fdt_parent_offset(gd->fdt_blob,
284 ofnode_to_offset(node));
285
286 return parent;
287}
288
Simon Glassc4fc5622017-05-18 20:08:58 -0600289const char *ofnode_get_name(ofnode node)
290{
Kever Yang8d7976d2019-07-19 11:23:47 +0800291 if (!ofnode_valid(node)) {
292 debug("%s node not valid\n", __func__);
293 return NULL;
294 }
295
Simon Glassc4fc5622017-05-18 20:08:58 -0600296 if (ofnode_is_np(node))
297 return strrchr(node.np->full_name, '/') + 1;
298
299 return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
300}
301
Marek Behúne897e3c2021-05-26 14:08:18 +0200302int ofnode_get_path(ofnode node, char *buf, int buflen)
303{
304 assert(ofnode_valid(node));
305
306 if (ofnode_is_np(node)) {
307 if (strlen(node.np->full_name) >= buflen)
308 return -ENOSPC;
309
310 strcpy(buf, node.np->full_name);
311
312 return 0;
313 } else {
314 int res;
315
316 res = fdt_get_path(gd->fdt_blob, ofnode_to_offset(node), buf,
317 buflen);
318 if (!res)
319 return res;
320 else if (res == -FDT_ERR_NOSPACE)
321 return -ENOSPC;
322 else
323 return -EINVAL;
324 }
325}
326
Kever Yang37df0e02018-02-23 17:38:50 +0100327ofnode ofnode_get_by_phandle(uint phandle)
328{
329 ofnode node;
330
331 if (of_live_active())
332 node = np_to_ofnode(of_find_node_by_phandle(phandle));
333 else
334 node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
335 phandle);
336
337 return node;
338}
339
Marek Behún177ab7f2021-05-26 14:08:17 +0200340static fdt_addr_t __ofnode_get_addr_size_index(ofnode node, int index,
341 fdt_size_t *size, bool translate)
Simon Glass049ae1b2017-05-18 20:09:01 -0600342{
Keerthy34222a32018-11-19 11:44:47 +0530343 int na, ns;
Keerthy34222a32018-11-19 11:44:47 +0530344
Chen Guanqiao2d1034b2021-07-12 15:40:20 +0800345 if (size)
346 *size = FDT_SIZE_T_NONE;
Chen Guanqiao223f17d2021-04-12 14:51:11 +0800347
Simon Glass049ae1b2017-05-18 20:09:01 -0600348 if (ofnode_is_np(node)) {
349 const __be32 *prop_val;
Simon Glass47f85de2019-09-25 08:55:50 -0600350 u64 size64;
Simon Glass049ae1b2017-05-18 20:09:01 -0600351 uint flags;
Simon Glass049ae1b2017-05-18 20:09:01 -0600352
Simon Glass47f85de2019-09-25 08:55:50 -0600353 prop_val = of_get_address(ofnode_to_np(node), index, &size64,
354 &flags);
Simon Glass049ae1b2017-05-18 20:09:01 -0600355 if (!prop_val)
356 return FDT_ADDR_T_NONE;
Chen Guanqiao2d1034b2021-07-12 15:40:20 +0800357
Simon Glass47f85de2019-09-25 08:55:50 -0600358 if (size)
359 *size = size64;
Mario Sixd007ebc2017-12-20 09:52:12 +0100360
Mario Six35616ef2018-03-12 14:53:33 +0100361 ns = of_n_size_cells(ofnode_to_np(node));
362
Marek Behún177ab7f2021-05-26 14:08:17 +0200363 if (translate && IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
Mario Sixd007ebc2017-12-20 09:52:12 +0100364 return of_translate_address(ofnode_to_np(node), prop_val);
365 } else {
366 na = of_n_addr_cells(ofnode_to_np(node));
367 return of_read_number(prop_val, na);
368 }
Simon Glass049ae1b2017-05-18 20:09:01 -0600369 } else {
Keerthy34222a32018-11-19 11:44:47 +0530370 na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
371 ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
372 return fdtdec_get_addr_size_fixed(gd->fdt_blob,
373 ofnode_to_offset(node), "reg",
Marek Behún177ab7f2021-05-26 14:08:17 +0200374 index, na, ns, size,
375 translate);
Simon Glass049ae1b2017-05-18 20:09:01 -0600376 }
Simon Glass049ae1b2017-05-18 20:09:01 -0600377}
378
Marek Behún177ab7f2021-05-26 14:08:17 +0200379fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
380{
381 return __ofnode_get_addr_size_index(node, index, size, true);
382}
383
384fdt_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
385 fdt_size_t *size)
386{
387 return __ofnode_get_addr_size_index(node, index, size, false);
388}
389
Keerthyd332e6e2019-04-24 17:19:53 +0530390fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
391{
392 fdt_size_t size;
393
394 return ofnode_get_addr_size_index(node, index, &size);
395}
396
Simon Glass049ae1b2017-05-18 20:09:01 -0600397fdt_addr_t ofnode_get_addr(ofnode node)
398{
399 return ofnode_get_addr_index(node, 0);
400}
401
Chen Guanqiao223f17d2021-04-12 14:51:11 +0800402fdt_size_t ofnode_get_size(ofnode node)
403{
404 fdt_size_t size;
405
406 ofnode_get_addr_size_index(node, 0, &size);
407
408 return size;
409}
410
Simon Glassc4fc5622017-05-18 20:08:58 -0600411int ofnode_stringlist_search(ofnode node, const char *property,
412 const char *string)
413{
414 if (ofnode_is_np(node)) {
415 return of_property_match_string(ofnode_to_np(node),
416 property, string);
417 } else {
418 int ret;
419
420 ret = fdt_stringlist_search(gd->fdt_blob,
421 ofnode_to_offset(node), property,
422 string);
423 if (ret == -FDT_ERR_NOTFOUND)
424 return -ENODATA;
425 else if (ret < 0)
426 return -EINVAL;
427
428 return ret;
429 }
430}
431
432int ofnode_read_string_index(ofnode node, const char *property, int index,
433 const char **outp)
434{
435 if (ofnode_is_np(node)) {
436 return of_property_read_string_index(ofnode_to_np(node),
437 property, index, outp);
438 } else {
439 int len;
440
441 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
442 property, index, &len);
443 if (len < 0)
444 return -EINVAL;
445 return 0;
446 }
447}
448
Simon Glass5fdb0052017-06-12 06:21:28 -0600449int ofnode_read_string_count(ofnode node, const char *property)
450{
451 if (ofnode_is_np(node)) {
452 return of_property_count_strings(ofnode_to_np(node), property);
453 } else {
454 return fdt_stringlist_count(gd->fdt_blob,
455 ofnode_to_offset(node), property);
456 }
457}
458
Simon Glass9580bfc2021-10-23 17:26:07 -0600459int ofnode_read_string_list(ofnode node, const char *property,
460 const char ***listp)
461{
462 const char **prop;
463 int count;
464 int i;
465
466 *listp = NULL;
467 count = ofnode_read_string_count(node, property);
468 if (count < 0)
469 return count;
470 if (!count)
471 return 0;
472
473 prop = calloc(count + 1, sizeof(char *));
474 if (!prop)
475 return -ENOMEM;
476
477 for (i = 0; i < count; i++)
478 ofnode_read_string_index(node, property, i, &prop[i]);
479 prop[count] = NULL;
480 *listp = prop;
481
482 return count;
483}
484
Simon Glassc4fc5622017-05-18 20:08:58 -0600485static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
486 struct ofnode_phandle_args *out)
487{
488 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
489 out->node = offset_to_ofnode(in->node);
490 out->args_count = in->args_count;
491 memcpy(out->args, in->args, sizeof(out->args));
492}
493
494static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
495 struct ofnode_phandle_args *out)
496{
497 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
498 out->node = np_to_ofnode(in->np);
499 out->args_count = in->args_count;
500 memcpy(out->args, in->args, sizeof(out->args));
501}
502
503int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
504 const char *cells_name, int cell_count,
505 int index,
506 struct ofnode_phandle_args *out_args)
507{
508 if (ofnode_is_np(node)) {
509 struct of_phandle_args args;
510 int ret;
511
512 ret = of_parse_phandle_with_args(ofnode_to_np(node),
Patrick Delaunay21e3b042020-09-10 18:26:17 +0200513 list_name, cells_name,
514 cell_count, index,
Mario Sixf40d82c2018-01-15 11:07:17 +0100515 &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600516 if (ret)
517 return ret;
518 ofnode_from_of_phandle_args(&args, out_args);
519 } else {
520 struct fdtdec_phandle_args args;
521 int ret;
522
523 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
Mario Sixf40d82c2018-01-15 11:07:17 +0100524 ofnode_to_offset(node),
525 list_name, cells_name,
526 cell_count, index, &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600527 if (ret)
528 return ret;
529 ofnode_from_fdtdec_phandle_args(&args, out_args);
530 }
531
532 return 0;
533}
534
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200535int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200536 const char *cells_name, int cell_count)
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200537{
538 if (ofnode_is_np(node))
539 return of_count_phandle_with_args(ofnode_to_np(node),
Patrick Delaunayd776a842020-09-25 09:41:14 +0200540 list_name, cells_name, cell_count);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200541 else
542 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
543 ofnode_to_offset(node), list_name, cells_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200544 cell_count, -1, NULL);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200545}
546
Simon Glassc4fc5622017-05-18 20:08:58 -0600547ofnode ofnode_path(const char *path)
548{
549 if (of_live_active())
550 return np_to_ofnode(of_find_node_by_path(path));
551 else
552 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
553}
554
Simon Glassef75c592022-07-30 15:52:08 -0600555ofnode ofnode_path_root(oftree tree, const char *path)
556{
557 if (of_live_active())
558 return np_to_ofnode(of_find_node_opts_by_path(tree.np, path,
559 NULL));
560 else if (*path != '/' && tree.fdt != gd->fdt_blob)
561 return ofnode_null(); /* Aliases only on control FDT */
562 else
563 return offset_to_ofnode(fdt_path_offset(tree.fdt, path));
564}
565
Simon Glasse09223c2020-01-27 08:49:46 -0700566const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
Simon Glassc4fc5622017-05-18 20:08:58 -0600567{
568 ofnode chosen_node;
569
570 chosen_node = ofnode_path("/chosen");
571
Simon Glasse09223c2020-01-27 08:49:46 -0700572 return ofnode_read_prop(chosen_node, propname, sizep);
Simon Glassc4fc5622017-05-18 20:08:58 -0600573}
574
Simon Glasse09223c2020-01-27 08:49:46 -0700575const char *ofnode_read_chosen_string(const char *propname)
576{
577 return ofnode_read_chosen_prop(propname, NULL);
578}
579
Simon Glassc4fc5622017-05-18 20:08:58 -0600580ofnode ofnode_get_chosen_node(const char *name)
581{
582 const char *prop;
583
Simon Glasse09223c2020-01-27 08:49:46 -0700584 prop = ofnode_read_chosen_prop(name, NULL);
Simon Glassc4fc5622017-05-18 20:08:58 -0600585 if (!prop)
586 return ofnode_null();
587
588 return ofnode_path(prop);
589}
590
Michal Simek92a88622020-07-28 12:51:08 +0200591const void *ofnode_read_aliases_prop(const char *propname, int *sizep)
592{
593 ofnode node;
594
595 node = ofnode_path("/aliases");
596
597 return ofnode_read_prop(node, propname, sizep);
598}
599
600ofnode ofnode_get_aliases_node(const char *name)
601{
602 const char *prop;
603
604 prop = ofnode_read_aliases_prop(name, NULL);
605 if (!prop)
606 return ofnode_null();
607
608 debug("%s: node_path: %s\n", __func__, prop);
609
610 return ofnode_path(prop);
611}
612
developerd93c8b42020-05-02 11:35:09 +0200613int ofnode_get_child_count(ofnode parent)
614{
615 ofnode child;
616 int num = 0;
617
618 ofnode_for_each_subnode(child, parent)
619 num++;
620
621 return num;
622}
623
Simon Glassc4fc5622017-05-18 20:08:58 -0600624static int decode_timing_property(ofnode node, const char *name,
625 struct timing_entry *result)
626{
627 int length, ret = 0;
628
629 length = ofnode_read_size(node, name);
630 if (length < 0) {
631 debug("%s: could not find property %s\n",
632 ofnode_get_name(node), name);
633 return length;
634 }
635
636 if (length == sizeof(u32)) {
637 result->typ = ofnode_read_u32_default(node, name, 0);
638 result->min = result->typ;
639 result->max = result->typ;
640 } else {
641 ret = ofnode_read_u32_array(node, name, &result->min, 3);
642 }
643
644 return ret;
645}
646
647int ofnode_decode_display_timing(ofnode parent, int index,
648 struct display_timing *dt)
649{
650 int i;
651 ofnode timings, node;
652 u32 val = 0;
653 int ret = 0;
654
655 timings = ofnode_find_subnode(parent, "display-timings");
656 if (!ofnode_valid(timings))
657 return -EINVAL;
658
Simon Glass28529762017-08-05 15:45:54 -0600659 i = 0;
660 ofnode_for_each_subnode(node, timings) {
661 if (i++ == index)
662 break;
663 }
Simon Glassc4fc5622017-05-18 20:08:58 -0600664
665 if (!ofnode_valid(node))
666 return -EINVAL;
667
668 memset(dt, 0, sizeof(*dt));
669
670 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
671 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
672 ret |= decode_timing_property(node, "hactive", &dt->hactive);
673 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
674 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
675 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
676 ret |= decode_timing_property(node, "vactive", &dt->vactive);
677 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
678 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
679
680 dt->flags = 0;
681 val = ofnode_read_u32_default(node, "vsync-active", -1);
682 if (val != -1) {
683 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
684 DISPLAY_FLAGS_VSYNC_LOW;
685 }
686 val = ofnode_read_u32_default(node, "hsync-active", -1);
687 if (val != -1) {
688 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
689 DISPLAY_FLAGS_HSYNC_LOW;
690 }
691 val = ofnode_read_u32_default(node, "de-active", -1);
692 if (val != -1) {
693 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
694 DISPLAY_FLAGS_DE_LOW;
695 }
696 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
697 if (val != -1) {
698 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
699 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
700 }
701
702 if (ofnode_read_bool(node, "interlaced"))
703 dt->flags |= DISPLAY_FLAGS_INTERLACED;
704 if (ofnode_read_bool(node, "doublescan"))
705 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
706 if (ofnode_read_bool(node, "doubleclk"))
707 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
708
709 return ret;
710}
711
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900712const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
Simon Glassc4fc5622017-05-18 20:08:58 -0600713{
Masahiro Yamada5052f1b2017-06-22 16:54:04 +0900714 if (ofnode_is_np(node))
715 return of_get_property(ofnode_to_np(node), propname, lenp);
716 else
Simon Glassc4fc5622017-05-18 20:08:58 -0600717 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
718 propname, lenp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600719}
720
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100721int ofnode_get_first_property(ofnode node, struct ofprop *prop)
722{
723 prop->node = node;
724
725 if (ofnode_is_np(node)) {
726 prop->prop = of_get_first_property(ofnode_to_np(prop->node));
727 if (!prop->prop)
728 return -FDT_ERR_NOTFOUND;
729 } else {
730 prop->offset =
731 fdt_first_property_offset(gd->fdt_blob,
732 ofnode_to_offset(prop->node));
733 if (prop->offset < 0)
734 return prop->offset;
735 }
736
737 return 0;
738}
739
740int ofnode_get_next_property(struct ofprop *prop)
741{
742 if (ofnode_is_np(prop->node)) {
743 prop->prop = of_get_next_property(ofnode_to_np(prop->node),
744 prop->prop);
745 if (!prop->prop)
746 return -FDT_ERR_NOTFOUND;
747 } else {
748 prop->offset = fdt_next_property_offset(gd->fdt_blob,
749 prop->offset);
750 if (prop->offset < 0)
751 return prop->offset;
752 }
753
754 return 0;
755}
756
757const void *ofnode_get_property_by_prop(const struct ofprop *prop,
758 const char **propname, int *lenp)
759{
760 if (ofnode_is_np(prop->node))
761 return of_get_property_by_prop(ofnode_to_np(prop->node),
762 prop->prop, propname, lenp);
763 else
764 return fdt_getprop_by_offset(gd->fdt_blob,
765 prop->offset,
766 propname, lenp);
767}
768
Simon Glassc4fc5622017-05-18 20:08:58 -0600769bool ofnode_is_available(ofnode node)
770{
771 if (ofnode_is_np(node))
772 return of_device_is_available(ofnode_to_np(node));
773 else
774 return fdtdec_get_is_enabled(gd->fdt_blob,
775 ofnode_to_offset(node));
776}
777
778fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
779 fdt_size_t *sizep)
780{
781 if (ofnode_is_np(node)) {
782 int na, ns;
783 int psize;
784 const struct device_node *np = ofnode_to_np(node);
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200785 const __be32 *prop = of_get_property(np, property, &psize);
Simon Glassc4fc5622017-05-18 20:08:58 -0600786
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200787 if (!prop)
788 return FDT_ADDR_T_NONE;
Simon Glassc4fc5622017-05-18 20:08:58 -0600789 na = of_n_addr_cells(np);
Marek Vasut1638c172018-10-01 12:37:19 +0200790 ns = of_n_size_cells(np);
Simon Glassa67cc632017-05-18 20:09:27 -0600791 *sizep = of_read_number(prop + na, ns);
Marek Vasuta9dac492018-10-01 12:37:20 +0200792
Dario Binacchif8fc7032021-05-01 17:05:26 +0200793 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
Marek Vasuta9dac492018-10-01 12:37:20 +0200794 return of_translate_address(np, prop);
795 else
796 return of_read_number(prop, na);
Simon Glassc4fc5622017-05-18 20:08:58 -0600797 } else {
798 return fdtdec_get_addr_size(gd->fdt_blob,
799 ofnode_to_offset(node), property,
800 sizep);
801 }
802}
803
804const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
805 size_t sz)
806{
807 if (ofnode_is_np(node)) {
808 const struct device_node *np = ofnode_to_np(node);
809 int psize;
810 const __be32 *prop = of_get_property(np, propname, &psize);
811
812 if (!prop || sz != psize)
813 return NULL;
814 return (uint8_t *)prop;
815
816 } else {
817 return fdtdec_locate_byte_array(gd->fdt_blob,
818 ofnode_to_offset(node), propname, sz);
819 }
820}
821
822int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
823 const char *propname, struct fdt_pci_addr *addr)
824{
Masahiro Yamada5c5991e2017-06-22 17:57:50 +0900825 const fdt32_t *cell;
Simon Glassc4fc5622017-05-18 20:08:58 -0600826 int len;
827 int ret = -ENOENT;
828
829 debug("%s: %s: ", __func__, propname);
830
831 /*
832 * If we follow the pci bus bindings strictly, we should check
833 * the value of the node's parent node's #address-cells and
834 * #size-cells. They need to be 3 and 2 accordingly. However,
835 * for simplicity we skip the check here.
836 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900837 cell = ofnode_get_property(node, propname, &len);
Simon Glassc4fc5622017-05-18 20:08:58 -0600838 if (!cell)
839 goto fail;
840
841 if ((len % FDT_PCI_REG_SIZE) == 0) {
842 int num = len / FDT_PCI_REG_SIZE;
843 int i;
844
845 for (i = 0; i < num; i++) {
846 debug("pci address #%d: %08lx %08lx %08lx\n", i,
847 (ulong)fdt32_to_cpu(cell[0]),
848 (ulong)fdt32_to_cpu(cell[1]),
849 (ulong)fdt32_to_cpu(cell[2]));
850 if ((fdt32_to_cpu(*cell) & type) == type) {
851 addr->phys_hi = fdt32_to_cpu(cell[0]);
852 addr->phys_mid = fdt32_to_cpu(cell[1]);
Simon Glassdfd43152019-09-25 08:55:46 -0600853 addr->phys_lo = fdt32_to_cpu(cell[2]);
Simon Glassc4fc5622017-05-18 20:08:58 -0600854 break;
Simon Glassc4fc5622017-05-18 20:08:58 -0600855 }
Mario Sixf40d82c2018-01-15 11:07:17 +0100856
857 cell += (FDT_PCI_ADDR_CELLS +
858 FDT_PCI_SIZE_CELLS);
Simon Glassc4fc5622017-05-18 20:08:58 -0600859 }
860
861 if (i == num) {
862 ret = -ENXIO;
863 goto fail;
864 }
865
866 return 0;
Simon Glassc4fc5622017-05-18 20:08:58 -0600867 }
868
Mario Sixf40d82c2018-01-15 11:07:17 +0100869 ret = -EINVAL;
870
Simon Glassc4fc5622017-05-18 20:08:58 -0600871fail:
872 debug("(not found)\n");
873 return ret;
874}
875
Bin Mengfa157712018-08-03 01:14:35 -0700876int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
877{
878 const char *list, *end;
879 int len;
880
881 list = ofnode_get_property(node, "compatible", &len);
882 if (!list)
883 return -ENOENT;
884
885 end = list + len;
886 while (list < end) {
887 len = strlen(list);
888 if (len >= strlen("pciVVVV,DDDD")) {
889 char *s = strstr(list, "pci");
890
891 /*
892 * check if the string is something like pciVVVV,DDDD.RR
893 * or just pciVVVV,DDDD
894 */
895 if (s && s[7] == ',' &&
896 (s[12] == '.' || s[12] == 0)) {
897 s += 3;
898 *vendor = simple_strtol(s, NULL, 16);
899
900 s += 5;
901 *device = simple_strtol(s, NULL, 16);
902
903 return 0;
904 }
905 }
906 list += (len + 1);
907 }
908
909 return -ENOENT;
910}
911
Michal Simeka253c3b2022-02-23 15:45:40 +0100912int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device)
913{
914 const char *list, *end;
915 int len;
916
917 list = ofnode_get_property(node, "compatible", &len);
918
919 if (!list)
920 return -ENOENT;
921
922 end = list + len;
923 while (list < end) {
924 len = strlen(list);
925
926 if (len >= strlen("ethernet-phy-idVVVV,DDDD")) {
927 char *s = strstr(list, "ethernet-phy-id");
928
929 /*
930 * check if the string is something like
931 * ethernet-phy-idVVVV,DDDD
932 */
933 if (s && s[19] == '.') {
934 s += strlen("ethernet-phy-id");
935 *vendor = simple_strtol(s, NULL, 16);
936 s += 5;
937 *device = simple_strtol(s, NULL, 16);
938
939 return 0;
940 }
941 }
942 list += (len + 1);
943 }
944
945 return -ENOENT;
946}
947
Simon Glassc4fc5622017-05-18 20:08:58 -0600948int ofnode_read_addr_cells(ofnode node)
949{
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200950 if (ofnode_is_np(node)) {
Simon Glassc4fc5622017-05-18 20:08:58 -0600951 return of_n_addr_cells(ofnode_to_np(node));
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200952 } else {
953 int parent = fdt_parent_offset(gd->fdt_blob,
954 ofnode_to_offset(node));
955
956 return fdt_address_cells(gd->fdt_blob, parent);
957 }
Simon Glassc4fc5622017-05-18 20:08:58 -0600958}
959
960int ofnode_read_size_cells(ofnode node)
961{
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200962 if (ofnode_is_np(node)) {
Simon Glassc4fc5622017-05-18 20:08:58 -0600963 return of_n_size_cells(ofnode_to_np(node));
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200964 } else {
965 int parent = fdt_parent_offset(gd->fdt_blob,
966 ofnode_to_offset(node));
967
968 return fdt_size_cells(gd->fdt_blob, parent);
969 }
Simon Glass4191dc12017-06-12 06:21:31 -0600970}
971
972int ofnode_read_simple_addr_cells(ofnode node)
973{
974 if (ofnode_is_np(node))
975 return of_simple_addr_cells(ofnode_to_np(node));
976 else
977 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
978}
979
980int ofnode_read_simple_size_cells(ofnode node)
981{
982 if (ofnode_is_np(node))
983 return of_simple_size_cells(ofnode_to_np(node));
Simon Glassc4fc5622017-05-18 20:08:58 -0600984 else
985 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
986}
987
988bool ofnode_pre_reloc(ofnode node)
989{
Patrick Delaunay0b025b82019-02-11 12:49:57 +0100990#if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
991 /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
992 * had property dm-pre-reloc or u-boot,dm-spl/tpl.
993 * They are removed in final dtb (fdtgrep 2nd pass)
994 */
995 return true;
996#else
Masahiro Yamada6a61dd92017-06-22 16:54:03 +0900997 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
Simon Glassc4fc5622017-05-18 20:08:58 -0600998 return true;
Simon Glass23f22842018-10-01 12:22:18 -0600999 if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
1000 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -06001001
Simon Glassc4fc5622017-05-18 20:08:58 -06001002 /*
1003 * In regular builds individual spl and tpl handling both
1004 * count as handled pre-relocation for later second init.
1005 */
Masahiro Yamada6a61dd92017-06-22 16:54:03 +09001006 if (ofnode_read_bool(node, "u-boot,dm-spl") ||
1007 ofnode_read_bool(node, "u-boot,dm-tpl"))
Simon Glassc4fc5622017-05-18 20:08:58 -06001008 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -06001009
1010 return false;
Patrick Delaunay0b025b82019-02-11 12:49:57 +01001011#endif
Simon Glassc4fc5622017-05-18 20:08:58 -06001012}
Simon Glassf7bfcc42017-07-25 08:29:55 -06001013
1014int ofnode_read_resource(ofnode node, uint index, struct resource *res)
1015{
1016 if (ofnode_is_np(node)) {
1017 return of_address_to_resource(ofnode_to_np(node), index, res);
1018 } else {
1019 struct fdt_resource fres;
1020 int ret;
1021
1022 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
1023 "reg", index, &fres);
1024 if (ret < 0)
1025 return -EINVAL;
1026 memset(res, '\0', sizeof(*res));
1027 res->start = fres.start;
1028 res->end = fres.end;
1029
1030 return 0;
1031 }
1032}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +09001033
1034int ofnode_read_resource_byname(ofnode node, const char *name,
1035 struct resource *res)
1036{
1037 int index;
1038
1039 index = ofnode_stringlist_search(node, "reg-names", name);
1040 if (index < 0)
1041 return index;
1042
1043 return ofnode_read_resource(node, index, res);
1044}
Mario Sixaefac062018-01-15 11:07:19 +01001045
1046u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
1047{
1048 if (ofnode_is_np(node))
1049 return of_translate_address(ofnode_to_np(node), in_addr);
1050 else
1051 return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
1052}
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001053
Fabien Dessenne22236e02019-05-31 15:11:30 +02001054u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
1055{
1056 if (ofnode_is_np(node))
1057 return of_translate_dma_address(ofnode_to_np(node), in_addr);
1058 else
1059 return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
1060}
1061
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001062int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus, u64 *size)
1063{
1064 if (ofnode_is_np(node))
1065 return of_get_dma_range(ofnode_to_np(node), cpu, bus, size);
1066 else
1067 return fdt_get_dma_range(gd->fdt_blob, ofnode_to_offset(node),
1068 cpu, bus, size);
1069}
1070
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001071int ofnode_device_is_compatible(ofnode node, const char *compat)
1072{
1073 if (ofnode_is_np(node))
1074 return of_device_is_compatible(ofnode_to_np(node), compat,
1075 NULL, NULL);
1076 else
1077 return !fdt_node_check_compatible(gd->fdt_blob,
1078 ofnode_to_offset(node),
1079 compat);
1080}
Simon Glass954eeae2018-06-11 13:07:13 -06001081
1082ofnode ofnode_by_compatible(ofnode from, const char *compat)
1083{
1084 if (of_live_active()) {
1085 return np_to_ofnode(of_find_compatible_node(
1086 (struct device_node *)ofnode_to_np(from), NULL,
1087 compat));
1088 } else {
1089 return offset_to_ofnode(fdt_node_offset_by_compatible(
1090 gd->fdt_blob, ofnode_to_offset(from), compat));
1091 }
1092}
Jens Wiklander7b68dad2018-08-20 11:09:58 +02001093
1094ofnode ofnode_by_prop_value(ofnode from, const char *propname,
1095 const void *propval, int proplen)
1096{
1097 if (of_live_active()) {
1098 return np_to_ofnode(of_find_node_by_prop_value(
1099 (struct device_node *)ofnode_to_np(from), propname,
1100 propval, proplen));
1101 } else {
1102 return offset_to_ofnode(fdt_node_offset_by_prop_value(
1103 gd->fdt_blob, ofnode_to_offset(from),
1104 propname, propval, proplen));
1105 }
1106}
Mario Six047dafc2018-06-26 08:46:48 +02001107
1108int ofnode_write_prop(ofnode node, const char *propname, int len,
1109 const void *value)
1110{
1111 const struct device_node *np = ofnode_to_np(node);
1112 struct property *pp;
1113 struct property *pp_last = NULL;
1114 struct property *new;
1115
1116 if (!of_live_active())
1117 return -ENOSYS;
1118
1119 if (!np)
1120 return -EINVAL;
1121
1122 for (pp = np->properties; pp; pp = pp->next) {
1123 if (strcmp(pp->name, propname) == 0) {
1124 /* Property exists -> change value */
1125 pp->value = (void *)value;
1126 pp->length = len;
1127 return 0;
1128 }
1129 pp_last = pp;
1130 }
1131
1132 if (!pp_last)
1133 return -ENOENT;
1134
1135 /* Property does not exist -> append new property */
1136 new = malloc(sizeof(struct property));
1137 if (!new)
1138 return -ENOMEM;
1139
1140 new->name = strdup(propname);
Mario Six95e885d2018-10-04 09:22:24 +02001141 if (!new->name) {
1142 free(new);
Mario Six047dafc2018-06-26 08:46:48 +02001143 return -ENOMEM;
Mario Six95e885d2018-10-04 09:22:24 +02001144 }
Mario Six047dafc2018-06-26 08:46:48 +02001145
1146 new->value = (void *)value;
1147 new->length = len;
1148 new->next = NULL;
1149
1150 pp_last->next = new;
1151
1152 return 0;
1153}
1154
1155int ofnode_write_string(ofnode node, const char *propname, const char *value)
1156{
1157 if (!of_live_active())
1158 return -ENOSYS;
1159
1160 assert(ofnode_valid(node));
1161
1162 debug("%s: %s = %s", __func__, propname, value);
1163
1164 return ofnode_write_prop(node, propname, strlen(value) + 1, value);
1165}
1166
1167int ofnode_set_enabled(ofnode node, bool value)
1168{
1169 if (!of_live_active())
1170 return -ENOSYS;
1171
1172 assert(ofnode_valid(node));
1173
1174 if (value)
1175 return ofnode_write_string(node, "status", "okay");
1176 else
Bin Menga9274aa2019-07-05 09:23:17 -07001177 return ofnode_write_string(node, "status", "disabled");
Mario Six047dafc2018-06-26 08:46:48 +02001178}
Simon Glass0034d962021-08-07 07:24:01 -06001179
1180bool ofnode_conf_read_bool(const char *prop_name)
1181{
1182 ofnode node;
1183
1184 node = ofnode_path("/config");
1185 if (!ofnode_valid(node))
1186 return false;
1187
1188 return ofnode_read_bool(node, prop_name);
1189}
1190
1191int ofnode_conf_read_int(const char *prop_name, int default_val)
1192{
1193 ofnode node;
1194
1195 node = ofnode_path("/config");
1196 if (!ofnode_valid(node))
1197 return default_val;
1198
1199 return ofnode_read_u32_default(node, prop_name, default_val);
1200}
1201
1202const char *ofnode_conf_read_str(const char *prop_name)
1203{
1204 ofnode node;
1205
1206 node = ofnode_path("/config");
1207 if (!ofnode_valid(node))
1208 return NULL;
1209
1210 return ofnode_read_string(node, prop_name);
1211}
Marek Behúnf4f1ddc2022-04-07 00:32:57 +02001212
1213ofnode ofnode_get_phy_node(ofnode node)
1214{
1215 /* DT node properties that reference a PHY node */
1216 static const char * const phy_handle_str[] = {
1217 "phy-handle", "phy", "phy-device",
1218 };
1219 struct ofnode_phandle_args args = {
1220 .node = ofnode_null()
1221 };
1222 int i;
1223
1224 assert(ofnode_valid(node));
1225
1226 for (i = 0; i < ARRAY_SIZE(phy_handle_str); i++)
1227 if (!ofnode_parse_phandle_with_args(node, phy_handle_str[i],
1228 NULL, 0, 0, &args))
1229 break;
1230
1231 return args.node;
1232}
Marek Behúnbc194772022-04-07 00:33:01 +02001233
1234phy_interface_t ofnode_read_phy_mode(ofnode node)
1235{
1236 const char *mode;
1237 int i;
1238
1239 assert(ofnode_valid(node));
1240
1241 mode = ofnode_read_string(node, "phy-mode");
1242 if (!mode)
1243 mode = ofnode_read_string(node, "phy-connection-type");
1244
1245 if (!mode)
Marek Behún48631e42022-04-07 00:33:03 +02001246 return PHY_INTERFACE_MODE_NA;
Marek Behúnbc194772022-04-07 00:33:01 +02001247
Marek Behún21a18362022-04-07 00:33:02 +02001248 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
Marek Behúnbc194772022-04-07 00:33:01 +02001249 if (!strcmp(mode, phy_interface_strings[i]))
1250 return i;
1251
1252 debug("%s: Invalid PHY interface '%s'\n", __func__, mode);
1253
Marek Behún48631e42022-04-07 00:33:03 +02001254 return PHY_INTERFACE_MODE_NA;
Marek Behúnbc194772022-04-07 00:33:01 +02001255}