blob: 6c771e364fb78af3fd642039c17c8b9bc83442ba [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
21int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
22{
Dario Binacchib3f1cdd2020-03-29 18:04:42 +020023 return ofnode_read_u32_index(node, propname, 0, outp);
Simon Glassc4fc5622017-05-18 20:08:58 -060024}
25
Trent Piepho5b775412019-05-10 17:48:20 +000026u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
Simon Glassc4fc5622017-05-18 20:08:58 -060027{
28 assert(ofnode_valid(node));
Dario Binacchib3f1cdd2020-03-29 18:04:42 +020029 ofnode_read_u32_index(node, propname, 0, &def);
Simon Glassc4fc5622017-05-18 20:08:58 -060030
31 return def;
32}
33
Dario Binacchi81d80b52020-03-29 18:04:41 +020034int ofnode_read_u32_index(ofnode node, const char *propname, int index,
35 u32 *outp)
36{
37 const fdt32_t *cell;
38 int len;
39
40 assert(ofnode_valid(node));
41 debug("%s: %s: ", __func__, propname);
42
43 if (ofnode_is_np(node))
44 return of_read_u32_index(ofnode_to_np(node), propname, index,
45 outp);
46
47 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
48 &len);
49 if (!cell) {
50 debug("(not found)\n");
51 return -EINVAL;
52 }
53
54 if (len < (sizeof(int) * (index + 1))) {
55 debug("(not large enough)\n");
56 return -EOVERFLOW;
57 }
58
59 *outp = fdt32_to_cpu(cell[index]);
60 debug("%#x (%d)\n", *outp, *outp);
61
62 return 0;
63}
64
65u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
66 u32 def)
67{
68 assert(ofnode_valid(node));
69 ofnode_read_u32_index(node, propname, index, &def);
70
71 return def;
72}
73
Simon Glassc4fc5622017-05-18 20:08:58 -060074int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
75{
76 assert(ofnode_valid(node));
77 ofnode_read_u32(node, propname, (u32 *)&def);
78
79 return def;
80}
81
Simon Glass9d54a7a2018-06-11 13:07:10 -060082int ofnode_read_u64(ofnode node, const char *propname, u64 *outp)
83{
Jean-Jacques Hiblot487f9172019-10-22 10:05:22 +020084 const unaligned_fdt64_t *cell;
Simon Glass9d54a7a2018-06-11 13:07:10 -060085 int len;
86
87 assert(ofnode_valid(node));
88 debug("%s: %s: ", __func__, propname);
89
90 if (ofnode_is_np(node))
91 return of_read_u64(ofnode_to_np(node), propname, outp);
92
93 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
94 &len);
95 if (!cell || len < sizeof(*cell)) {
96 debug("(not found)\n");
97 return -EINVAL;
98 }
99 *outp = fdt64_to_cpu(cell[0]);
100 debug("%#llx (%lld)\n", (unsigned long long)*outp,
101 (unsigned long long)*outp);
102
103 return 0;
104}
105
T Karthik Reddy478860d2019-09-02 16:34:30 +0200106u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def)
Simon Glass9d54a7a2018-06-11 13:07:10 -0600107{
108 assert(ofnode_valid(node));
109 ofnode_read_u64(node, propname, &def);
110
111 return def;
112}
113
Simon Glassc4fc5622017-05-18 20:08:58 -0600114bool ofnode_read_bool(ofnode node, const char *propname)
115{
Masahiro Yamada5d434452017-06-22 16:54:07 +0900116 const void *prop;
Simon Glassc4fc5622017-05-18 20:08:58 -0600117
118 assert(ofnode_valid(node));
119 debug("%s: %s: ", __func__, propname);
120
Masahiro Yamada5d434452017-06-22 16:54:07 +0900121 prop = ofnode_get_property(node, propname, NULL);
122
123 debug("%s\n", prop ? "true" : "false");
Simon Glassc4fc5622017-05-18 20:08:58 -0600124
Masahiro Yamada5d434452017-06-22 16:54:07 +0900125 return prop ? true : false;
Simon Glassc4fc5622017-05-18 20:08:58 -0600126}
127
Simon Glass0c2e9802020-01-27 08:49:44 -0700128const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep)
Simon Glassc4fc5622017-05-18 20:08:58 -0600129{
Simon Glass0c2e9802020-01-27 08:49:44 -0700130 const char *val = NULL;
131 int len;
Simon Glassc4fc5622017-05-18 20:08:58 -0600132
133 assert(ofnode_valid(node));
134 debug("%s: %s: ", __func__, propname);
135
136 if (ofnode_is_np(node)) {
137 struct property *prop = of_find_property(
Simon Glass0c2e9802020-01-27 08:49:44 -0700138 ofnode_to_np(node), propname, &len);
Simon Glassc4fc5622017-05-18 20:08:58 -0600139
140 if (prop) {
Simon Glass0c2e9802020-01-27 08:49:44 -0700141 val = prop->value;
Simon Glassc4fc5622017-05-18 20:08:58 -0600142 len = prop->length;
143 }
144 } else {
Simon Glass0c2e9802020-01-27 08:49:44 -0700145 val = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
Simon Glassc4fc5622017-05-18 20:08:58 -0600146 propname, &len);
147 }
Simon Glass0c2e9802020-01-27 08:49:44 -0700148 if (!val) {
Simon Glassc4fc5622017-05-18 20:08:58 -0600149 debug("<not found>\n");
Simon Glass0c2e9802020-01-27 08:49:44 -0700150 if (sizep)
151 *sizep = -FDT_ERR_NOTFOUND;
Simon Glassc4fc5622017-05-18 20:08:58 -0600152 return NULL;
153 }
Simon Glass0c2e9802020-01-27 08:49:44 -0700154 if (sizep)
155 *sizep = len;
156
157 return val;
158}
159
160const char *ofnode_read_string(ofnode node, const char *propname)
161{
162 const char *str;
163 int len;
164
165 str = ofnode_read_prop(node, propname, &len);
166 if (!str)
167 return NULL;
168
Simon Glassc4fc5622017-05-18 20:08:58 -0600169 if (strnlen(str, len) >= len) {
170 debug("<invalid>\n");
171 return NULL;
172 }
173 debug("%s\n", str);
174
175 return str;
176}
177
Simon Glass81c54b32020-01-27 08:49:45 -0700178int ofnode_read_size(ofnode node, const char *propname)
179{
180 int len;
181
182 if (!ofnode_read_prop(node, propname, &len))
183 return -EINVAL;
184
185 return len;
186}
187
Simon Glassc4fc5622017-05-18 20:08:58 -0600188ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
189{
190 ofnode subnode;
191
192 assert(ofnode_valid(node));
193 debug("%s: %s: ", __func__, subnode_name);
194
195 if (ofnode_is_np(node)) {
196 const struct device_node *np = ofnode_to_np(node);
197
198 for (np = np->child; np; np = np->sibling) {
199 if (!strcmp(subnode_name, np->name))
200 break;
201 }
202 subnode = np_to_ofnode(np);
203 } else {
204 int ooffset = fdt_subnode_offset(gd->fdt_blob,
205 ofnode_to_offset(node), subnode_name);
206 subnode = offset_to_ofnode(ooffset);
207 }
208 debug("%s\n", ofnode_valid(subnode) ?
209 ofnode_get_name(subnode) : "<none>");
210
211 return subnode;
212}
213
214int ofnode_read_u32_array(ofnode node, const char *propname,
215 u32 *out_values, size_t sz)
216{
217 assert(ofnode_valid(node));
218 debug("%s: %s: ", __func__, propname);
219
220 if (ofnode_is_np(node)) {
221 return of_read_u32_array(ofnode_to_np(node), propname,
222 out_values, sz);
223 } else {
224 return fdtdec_get_int_array(gd->fdt_blob,
225 ofnode_to_offset(node), propname,
226 out_values, sz);
227 }
228}
229
Simon Glass39f1d282020-12-16 17:25:06 -0700230#if !CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
Simon Glass5de5b3b2020-11-28 17:50:02 -0700231bool ofnode_is_enabled(ofnode node)
232{
233 if (ofnode_is_np(node)) {
234 return of_device_is_available(ofnode_to_np(node));
235 } else {
236 return fdtdec_get_is_enabled(gd->fdt_blob,
237 ofnode_to_offset(node));
238 }
239}
240
Simon Glassc4fc5622017-05-18 20:08:58 -0600241ofnode ofnode_first_subnode(ofnode node)
242{
243 assert(ofnode_valid(node));
244 if (ofnode_is_np(node))
245 return np_to_ofnode(node.np->child);
246
247 return offset_to_ofnode(
248 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
249}
250
251ofnode ofnode_next_subnode(ofnode node)
252{
253 assert(ofnode_valid(node));
254 if (ofnode_is_np(node))
255 return np_to_ofnode(node.np->sibling);
256
257 return offset_to_ofnode(
258 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
259}
Simon Glass39f1d282020-12-16 17:25:06 -0700260#endif /* !DM_INLINE_OFNODE */
Simon Glassc4fc5622017-05-18 20:08:58 -0600261
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100262ofnode ofnode_get_parent(ofnode node)
263{
264 ofnode parent;
265
266 assert(ofnode_valid(node));
267 if (ofnode_is_np(node))
268 parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
269 else
270 parent.of_offset = fdt_parent_offset(gd->fdt_blob,
271 ofnode_to_offset(node));
272
273 return parent;
274}
275
Simon Glassc4fc5622017-05-18 20:08:58 -0600276const char *ofnode_get_name(ofnode node)
277{
Kever Yang8d7976d2019-07-19 11:23:47 +0800278 if (!ofnode_valid(node)) {
279 debug("%s node not valid\n", __func__);
280 return NULL;
281 }
282
Simon Glassc4fc5622017-05-18 20:08:58 -0600283 if (ofnode_is_np(node))
284 return strrchr(node.np->full_name, '/') + 1;
285
286 return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
287}
288
Kever Yang37df0e02018-02-23 17:38:50 +0100289ofnode ofnode_get_by_phandle(uint phandle)
290{
291 ofnode node;
292
293 if (of_live_active())
294 node = np_to_ofnode(of_find_node_by_phandle(phandle));
295 else
296 node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
297 phandle);
298
299 return node;
300}
301
Keerthyd332e6e2019-04-24 17:19:53 +0530302fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
Simon Glass049ae1b2017-05-18 20:09:01 -0600303{
Keerthy34222a32018-11-19 11:44:47 +0530304 int na, ns;
Keerthy34222a32018-11-19 11:44:47 +0530305
Chen Guanqiao223f17d2021-04-12 14:51:11 +0800306 *size = FDT_SIZE_T_NONE;
307
Simon Glass049ae1b2017-05-18 20:09:01 -0600308 if (ofnode_is_np(node)) {
309 const __be32 *prop_val;
Simon Glass47f85de2019-09-25 08:55:50 -0600310 u64 size64;
Simon Glass049ae1b2017-05-18 20:09:01 -0600311 uint flags;
Simon Glass049ae1b2017-05-18 20:09:01 -0600312
Simon Glass47f85de2019-09-25 08:55:50 -0600313 prop_val = of_get_address(ofnode_to_np(node), index, &size64,
314 &flags);
Simon Glass049ae1b2017-05-18 20:09:01 -0600315 if (!prop_val)
316 return FDT_ADDR_T_NONE;
Simon Glass47f85de2019-09-25 08:55:50 -0600317 if (size)
318 *size = size64;
Mario Sixd007ebc2017-12-20 09:52:12 +0100319
Mario Six35616ef2018-03-12 14:53:33 +0100320 ns = of_n_size_cells(ofnode_to_np(node));
321
Dario Binacchif8fc7032021-05-01 17:05:26 +0200322 if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
Mario Sixd007ebc2017-12-20 09:52:12 +0100323 return of_translate_address(ofnode_to_np(node), prop_val);
324 } else {
325 na = of_n_addr_cells(ofnode_to_np(node));
326 return of_read_number(prop_val, na);
327 }
Simon Glass049ae1b2017-05-18 20:09:01 -0600328 } else {
Keerthy34222a32018-11-19 11:44:47 +0530329 na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
330 ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
331 return fdtdec_get_addr_size_fixed(gd->fdt_blob,
332 ofnode_to_offset(node), "reg",
Keerthyd332e6e2019-04-24 17:19:53 +0530333 index, na, ns, size, true);
Simon Glass049ae1b2017-05-18 20:09:01 -0600334 }
335
336 return FDT_ADDR_T_NONE;
337}
338
Keerthyd332e6e2019-04-24 17:19:53 +0530339fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
340{
341 fdt_size_t size;
342
343 return ofnode_get_addr_size_index(node, index, &size);
344}
345
Simon Glass049ae1b2017-05-18 20:09:01 -0600346fdt_addr_t ofnode_get_addr(ofnode node)
347{
348 return ofnode_get_addr_index(node, 0);
349}
350
Chen Guanqiao223f17d2021-04-12 14:51:11 +0800351fdt_size_t ofnode_get_size(ofnode node)
352{
353 fdt_size_t size;
354
355 ofnode_get_addr_size_index(node, 0, &size);
356
357 return size;
358}
359
Simon Glassc4fc5622017-05-18 20:08:58 -0600360int ofnode_stringlist_search(ofnode node, const char *property,
361 const char *string)
362{
363 if (ofnode_is_np(node)) {
364 return of_property_match_string(ofnode_to_np(node),
365 property, string);
366 } else {
367 int ret;
368
369 ret = fdt_stringlist_search(gd->fdt_blob,
370 ofnode_to_offset(node), property,
371 string);
372 if (ret == -FDT_ERR_NOTFOUND)
373 return -ENODATA;
374 else if (ret < 0)
375 return -EINVAL;
376
377 return ret;
378 }
379}
380
381int ofnode_read_string_index(ofnode node, const char *property, int index,
382 const char **outp)
383{
384 if (ofnode_is_np(node)) {
385 return of_property_read_string_index(ofnode_to_np(node),
386 property, index, outp);
387 } else {
388 int len;
389
390 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
391 property, index, &len);
392 if (len < 0)
393 return -EINVAL;
394 return 0;
395 }
396}
397
Simon Glass5fdb0052017-06-12 06:21:28 -0600398int ofnode_read_string_count(ofnode node, const char *property)
399{
400 if (ofnode_is_np(node)) {
401 return of_property_count_strings(ofnode_to_np(node), property);
402 } else {
403 return fdt_stringlist_count(gd->fdt_blob,
404 ofnode_to_offset(node), property);
405 }
406}
407
Simon Glassc4fc5622017-05-18 20:08:58 -0600408static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
409 struct ofnode_phandle_args *out)
410{
411 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
412 out->node = offset_to_ofnode(in->node);
413 out->args_count = in->args_count;
414 memcpy(out->args, in->args, sizeof(out->args));
415}
416
417static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
418 struct ofnode_phandle_args *out)
419{
420 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
421 out->node = np_to_ofnode(in->np);
422 out->args_count = in->args_count;
423 memcpy(out->args, in->args, sizeof(out->args));
424}
425
426int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
427 const char *cells_name, int cell_count,
428 int index,
429 struct ofnode_phandle_args *out_args)
430{
431 if (ofnode_is_np(node)) {
432 struct of_phandle_args args;
433 int ret;
434
435 ret = of_parse_phandle_with_args(ofnode_to_np(node),
Patrick Delaunay21e3b042020-09-10 18:26:17 +0200436 list_name, cells_name,
437 cell_count, index,
Mario Sixf40d82c2018-01-15 11:07:17 +0100438 &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600439 if (ret)
440 return ret;
441 ofnode_from_of_phandle_args(&args, out_args);
442 } else {
443 struct fdtdec_phandle_args args;
444 int ret;
445
446 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
Mario Sixf40d82c2018-01-15 11:07:17 +0100447 ofnode_to_offset(node),
448 list_name, cells_name,
449 cell_count, index, &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600450 if (ret)
451 return ret;
452 ofnode_from_fdtdec_phandle_args(&args, out_args);
453 }
454
455 return 0;
456}
457
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200458int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200459 const char *cells_name, int cell_count)
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200460{
461 if (ofnode_is_np(node))
462 return of_count_phandle_with_args(ofnode_to_np(node),
Patrick Delaunayd776a842020-09-25 09:41:14 +0200463 list_name, cells_name, cell_count);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200464 else
465 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
466 ofnode_to_offset(node), list_name, cells_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200467 cell_count, -1, NULL);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200468}
469
Simon Glassc4fc5622017-05-18 20:08:58 -0600470ofnode ofnode_path(const char *path)
471{
472 if (of_live_active())
473 return np_to_ofnode(of_find_node_by_path(path));
474 else
475 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
476}
477
Simon Glasse09223c2020-01-27 08:49:46 -0700478const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
Simon Glassc4fc5622017-05-18 20:08:58 -0600479{
480 ofnode chosen_node;
481
482 chosen_node = ofnode_path("/chosen");
483
Simon Glasse09223c2020-01-27 08:49:46 -0700484 return ofnode_read_prop(chosen_node, propname, sizep);
Simon Glassc4fc5622017-05-18 20:08:58 -0600485}
486
Simon Glasse09223c2020-01-27 08:49:46 -0700487const char *ofnode_read_chosen_string(const char *propname)
488{
489 return ofnode_read_chosen_prop(propname, NULL);
490}
491
Simon Glassc4fc5622017-05-18 20:08:58 -0600492ofnode ofnode_get_chosen_node(const char *name)
493{
494 const char *prop;
495
Simon Glasse09223c2020-01-27 08:49:46 -0700496 prop = ofnode_read_chosen_prop(name, NULL);
Simon Glassc4fc5622017-05-18 20:08:58 -0600497 if (!prop)
498 return ofnode_null();
499
500 return ofnode_path(prop);
501}
502
Michal Simek92a88622020-07-28 12:51:08 +0200503const void *ofnode_read_aliases_prop(const char *propname, int *sizep)
504{
505 ofnode node;
506
507 node = ofnode_path("/aliases");
508
509 return ofnode_read_prop(node, propname, sizep);
510}
511
512ofnode ofnode_get_aliases_node(const char *name)
513{
514 const char *prop;
515
516 prop = ofnode_read_aliases_prop(name, NULL);
517 if (!prop)
518 return ofnode_null();
519
520 debug("%s: node_path: %s\n", __func__, prop);
521
522 return ofnode_path(prop);
523}
524
developerd93c8b42020-05-02 11:35:09 +0200525int ofnode_get_child_count(ofnode parent)
526{
527 ofnode child;
528 int num = 0;
529
530 ofnode_for_each_subnode(child, parent)
531 num++;
532
533 return num;
534}
535
Simon Glassc4fc5622017-05-18 20:08:58 -0600536static int decode_timing_property(ofnode node, const char *name,
537 struct timing_entry *result)
538{
539 int length, ret = 0;
540
541 length = ofnode_read_size(node, name);
542 if (length < 0) {
543 debug("%s: could not find property %s\n",
544 ofnode_get_name(node), name);
545 return length;
546 }
547
548 if (length == sizeof(u32)) {
549 result->typ = ofnode_read_u32_default(node, name, 0);
550 result->min = result->typ;
551 result->max = result->typ;
552 } else {
553 ret = ofnode_read_u32_array(node, name, &result->min, 3);
554 }
555
556 return ret;
557}
558
559int ofnode_decode_display_timing(ofnode parent, int index,
560 struct display_timing *dt)
561{
562 int i;
563 ofnode timings, node;
564 u32 val = 0;
565 int ret = 0;
566
567 timings = ofnode_find_subnode(parent, "display-timings");
568 if (!ofnode_valid(timings))
569 return -EINVAL;
570
Simon Glass28529762017-08-05 15:45:54 -0600571 i = 0;
572 ofnode_for_each_subnode(node, timings) {
573 if (i++ == index)
574 break;
575 }
Simon Glassc4fc5622017-05-18 20:08:58 -0600576
577 if (!ofnode_valid(node))
578 return -EINVAL;
579
580 memset(dt, 0, sizeof(*dt));
581
582 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
583 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
584 ret |= decode_timing_property(node, "hactive", &dt->hactive);
585 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
586 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
587 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
588 ret |= decode_timing_property(node, "vactive", &dt->vactive);
589 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
590 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
591
592 dt->flags = 0;
593 val = ofnode_read_u32_default(node, "vsync-active", -1);
594 if (val != -1) {
595 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
596 DISPLAY_FLAGS_VSYNC_LOW;
597 }
598 val = ofnode_read_u32_default(node, "hsync-active", -1);
599 if (val != -1) {
600 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
601 DISPLAY_FLAGS_HSYNC_LOW;
602 }
603 val = ofnode_read_u32_default(node, "de-active", -1);
604 if (val != -1) {
605 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
606 DISPLAY_FLAGS_DE_LOW;
607 }
608 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
609 if (val != -1) {
610 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
611 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
612 }
613
614 if (ofnode_read_bool(node, "interlaced"))
615 dt->flags |= DISPLAY_FLAGS_INTERLACED;
616 if (ofnode_read_bool(node, "doublescan"))
617 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
618 if (ofnode_read_bool(node, "doubleclk"))
619 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
620
621 return ret;
622}
623
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900624const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
Simon Glassc4fc5622017-05-18 20:08:58 -0600625{
Masahiro Yamada5052f1b2017-06-22 16:54:04 +0900626 if (ofnode_is_np(node))
627 return of_get_property(ofnode_to_np(node), propname, lenp);
628 else
Simon Glassc4fc5622017-05-18 20:08:58 -0600629 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
630 propname, lenp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600631}
632
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100633int ofnode_get_first_property(ofnode node, struct ofprop *prop)
634{
635 prop->node = node;
636
637 if (ofnode_is_np(node)) {
638 prop->prop = of_get_first_property(ofnode_to_np(prop->node));
639 if (!prop->prop)
640 return -FDT_ERR_NOTFOUND;
641 } else {
642 prop->offset =
643 fdt_first_property_offset(gd->fdt_blob,
644 ofnode_to_offset(prop->node));
645 if (prop->offset < 0)
646 return prop->offset;
647 }
648
649 return 0;
650}
651
652int ofnode_get_next_property(struct ofprop *prop)
653{
654 if (ofnode_is_np(prop->node)) {
655 prop->prop = of_get_next_property(ofnode_to_np(prop->node),
656 prop->prop);
657 if (!prop->prop)
658 return -FDT_ERR_NOTFOUND;
659 } else {
660 prop->offset = fdt_next_property_offset(gd->fdt_blob,
661 prop->offset);
662 if (prop->offset < 0)
663 return prop->offset;
664 }
665
666 return 0;
667}
668
669const void *ofnode_get_property_by_prop(const struct ofprop *prop,
670 const char **propname, int *lenp)
671{
672 if (ofnode_is_np(prop->node))
673 return of_get_property_by_prop(ofnode_to_np(prop->node),
674 prop->prop, propname, lenp);
675 else
676 return fdt_getprop_by_offset(gd->fdt_blob,
677 prop->offset,
678 propname, lenp);
679}
680
Simon Glassc4fc5622017-05-18 20:08:58 -0600681bool ofnode_is_available(ofnode node)
682{
683 if (ofnode_is_np(node))
684 return of_device_is_available(ofnode_to_np(node));
685 else
686 return fdtdec_get_is_enabled(gd->fdt_blob,
687 ofnode_to_offset(node));
688}
689
690fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
691 fdt_size_t *sizep)
692{
693 if (ofnode_is_np(node)) {
694 int na, ns;
695 int psize;
696 const struct device_node *np = ofnode_to_np(node);
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200697 const __be32 *prop = of_get_property(np, property, &psize);
Simon Glassc4fc5622017-05-18 20:08:58 -0600698
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200699 if (!prop)
700 return FDT_ADDR_T_NONE;
Simon Glassc4fc5622017-05-18 20:08:58 -0600701 na = of_n_addr_cells(np);
Marek Vasut1638c172018-10-01 12:37:19 +0200702 ns = of_n_size_cells(np);
Simon Glassa67cc632017-05-18 20:09:27 -0600703 *sizep = of_read_number(prop + na, ns);
Marek Vasuta9dac492018-10-01 12:37:20 +0200704
Dario Binacchif8fc7032021-05-01 17:05:26 +0200705 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
Marek Vasuta9dac492018-10-01 12:37:20 +0200706 return of_translate_address(np, prop);
707 else
708 return of_read_number(prop, na);
Simon Glassc4fc5622017-05-18 20:08:58 -0600709 } else {
710 return fdtdec_get_addr_size(gd->fdt_blob,
711 ofnode_to_offset(node), property,
712 sizep);
713 }
714}
715
716const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
717 size_t sz)
718{
719 if (ofnode_is_np(node)) {
720 const struct device_node *np = ofnode_to_np(node);
721 int psize;
722 const __be32 *prop = of_get_property(np, propname, &psize);
723
724 if (!prop || sz != psize)
725 return NULL;
726 return (uint8_t *)prop;
727
728 } else {
729 return fdtdec_locate_byte_array(gd->fdt_blob,
730 ofnode_to_offset(node), propname, sz);
731 }
732}
733
734int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
735 const char *propname, struct fdt_pci_addr *addr)
736{
Masahiro Yamada5c5991e2017-06-22 17:57:50 +0900737 const fdt32_t *cell;
Simon Glassc4fc5622017-05-18 20:08:58 -0600738 int len;
739 int ret = -ENOENT;
740
741 debug("%s: %s: ", __func__, propname);
742
743 /*
744 * If we follow the pci bus bindings strictly, we should check
745 * the value of the node's parent node's #address-cells and
746 * #size-cells. They need to be 3 and 2 accordingly. However,
747 * for simplicity we skip the check here.
748 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900749 cell = ofnode_get_property(node, propname, &len);
Simon Glassc4fc5622017-05-18 20:08:58 -0600750 if (!cell)
751 goto fail;
752
753 if ((len % FDT_PCI_REG_SIZE) == 0) {
754 int num = len / FDT_PCI_REG_SIZE;
755 int i;
756
757 for (i = 0; i < num; i++) {
758 debug("pci address #%d: %08lx %08lx %08lx\n", i,
759 (ulong)fdt32_to_cpu(cell[0]),
760 (ulong)fdt32_to_cpu(cell[1]),
761 (ulong)fdt32_to_cpu(cell[2]));
762 if ((fdt32_to_cpu(*cell) & type) == type) {
763 addr->phys_hi = fdt32_to_cpu(cell[0]);
764 addr->phys_mid = fdt32_to_cpu(cell[1]);
Simon Glassdfd43152019-09-25 08:55:46 -0600765 addr->phys_lo = fdt32_to_cpu(cell[2]);
Simon Glassc4fc5622017-05-18 20:08:58 -0600766 break;
Simon Glassc4fc5622017-05-18 20:08:58 -0600767 }
Mario Sixf40d82c2018-01-15 11:07:17 +0100768
769 cell += (FDT_PCI_ADDR_CELLS +
770 FDT_PCI_SIZE_CELLS);
Simon Glassc4fc5622017-05-18 20:08:58 -0600771 }
772
773 if (i == num) {
774 ret = -ENXIO;
775 goto fail;
776 }
777
778 return 0;
Simon Glassc4fc5622017-05-18 20:08:58 -0600779 }
780
Mario Sixf40d82c2018-01-15 11:07:17 +0100781 ret = -EINVAL;
782
Simon Glassc4fc5622017-05-18 20:08:58 -0600783fail:
784 debug("(not found)\n");
785 return ret;
786}
787
Bin Mengfa157712018-08-03 01:14:35 -0700788int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
789{
790 const char *list, *end;
791 int len;
792
793 list = ofnode_get_property(node, "compatible", &len);
794 if (!list)
795 return -ENOENT;
796
797 end = list + len;
798 while (list < end) {
799 len = strlen(list);
800 if (len >= strlen("pciVVVV,DDDD")) {
801 char *s = strstr(list, "pci");
802
803 /*
804 * check if the string is something like pciVVVV,DDDD.RR
805 * or just pciVVVV,DDDD
806 */
807 if (s && s[7] == ',' &&
808 (s[12] == '.' || s[12] == 0)) {
809 s += 3;
810 *vendor = simple_strtol(s, NULL, 16);
811
812 s += 5;
813 *device = simple_strtol(s, NULL, 16);
814
815 return 0;
816 }
817 }
818 list += (len + 1);
819 }
820
821 return -ENOENT;
822}
823
Simon Glassc4fc5622017-05-18 20:08:58 -0600824int ofnode_read_addr_cells(ofnode node)
825{
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200826 if (ofnode_is_np(node)) {
Simon Glassc4fc5622017-05-18 20:08:58 -0600827 return of_n_addr_cells(ofnode_to_np(node));
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200828 } else {
829 int parent = fdt_parent_offset(gd->fdt_blob,
830 ofnode_to_offset(node));
831
832 return fdt_address_cells(gd->fdt_blob, parent);
833 }
Simon Glassc4fc5622017-05-18 20:08:58 -0600834}
835
836int ofnode_read_size_cells(ofnode node)
837{
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200838 if (ofnode_is_np(node)) {
Simon Glassc4fc5622017-05-18 20:08:58 -0600839 return of_n_size_cells(ofnode_to_np(node));
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200840 } else {
841 int parent = fdt_parent_offset(gd->fdt_blob,
842 ofnode_to_offset(node));
843
844 return fdt_size_cells(gd->fdt_blob, parent);
845 }
Simon Glass4191dc12017-06-12 06:21:31 -0600846}
847
848int ofnode_read_simple_addr_cells(ofnode node)
849{
850 if (ofnode_is_np(node))
851 return of_simple_addr_cells(ofnode_to_np(node));
852 else
853 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
854}
855
856int ofnode_read_simple_size_cells(ofnode node)
857{
858 if (ofnode_is_np(node))
859 return of_simple_size_cells(ofnode_to_np(node));
Simon Glassc4fc5622017-05-18 20:08:58 -0600860 else
861 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
862}
863
864bool ofnode_pre_reloc(ofnode node)
865{
Patrick Delaunay0b025b82019-02-11 12:49:57 +0100866#if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
867 /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
868 * had property dm-pre-reloc or u-boot,dm-spl/tpl.
869 * They are removed in final dtb (fdtgrep 2nd pass)
870 */
871 return true;
872#else
Masahiro Yamada6a61dd92017-06-22 16:54:03 +0900873 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
Simon Glassc4fc5622017-05-18 20:08:58 -0600874 return true;
Simon Glass23f22842018-10-01 12:22:18 -0600875 if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
876 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -0600877
Simon Glassc4fc5622017-05-18 20:08:58 -0600878 /*
879 * In regular builds individual spl and tpl handling both
880 * count as handled pre-relocation for later second init.
881 */
Masahiro Yamada6a61dd92017-06-22 16:54:03 +0900882 if (ofnode_read_bool(node, "u-boot,dm-spl") ||
883 ofnode_read_bool(node, "u-boot,dm-tpl"))
Simon Glassc4fc5622017-05-18 20:08:58 -0600884 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -0600885
886 return false;
Patrick Delaunay0b025b82019-02-11 12:49:57 +0100887#endif
Simon Glassc4fc5622017-05-18 20:08:58 -0600888}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600889
890int ofnode_read_resource(ofnode node, uint index, struct resource *res)
891{
892 if (ofnode_is_np(node)) {
893 return of_address_to_resource(ofnode_to_np(node), index, res);
894 } else {
895 struct fdt_resource fres;
896 int ret;
897
898 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
899 "reg", index, &fres);
900 if (ret < 0)
901 return -EINVAL;
902 memset(res, '\0', sizeof(*res));
903 res->start = fres.start;
904 res->end = fres.end;
905
906 return 0;
907 }
908}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900909
910int ofnode_read_resource_byname(ofnode node, const char *name,
911 struct resource *res)
912{
913 int index;
914
915 index = ofnode_stringlist_search(node, "reg-names", name);
916 if (index < 0)
917 return index;
918
919 return ofnode_read_resource(node, index, res);
920}
Mario Sixaefac062018-01-15 11:07:19 +0100921
922u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
923{
924 if (ofnode_is_np(node))
925 return of_translate_address(ofnode_to_np(node), in_addr);
926 else
927 return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
928}
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900929
Fabien Dessenne22236e02019-05-31 15:11:30 +0200930u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
931{
932 if (ofnode_is_np(node))
933 return of_translate_dma_address(ofnode_to_np(node), in_addr);
934 else
935 return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
936}
937
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +0100938int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus, u64 *size)
939{
940 if (ofnode_is_np(node))
941 return of_get_dma_range(ofnode_to_np(node), cpu, bus, size);
942 else
943 return fdt_get_dma_range(gd->fdt_blob, ofnode_to_offset(node),
944 cpu, bus, size);
945}
946
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900947int ofnode_device_is_compatible(ofnode node, const char *compat)
948{
949 if (ofnode_is_np(node))
950 return of_device_is_compatible(ofnode_to_np(node), compat,
951 NULL, NULL);
952 else
953 return !fdt_node_check_compatible(gd->fdt_blob,
954 ofnode_to_offset(node),
955 compat);
956}
Simon Glass954eeae2018-06-11 13:07:13 -0600957
958ofnode ofnode_by_compatible(ofnode from, const char *compat)
959{
960 if (of_live_active()) {
961 return np_to_ofnode(of_find_compatible_node(
962 (struct device_node *)ofnode_to_np(from), NULL,
963 compat));
964 } else {
965 return offset_to_ofnode(fdt_node_offset_by_compatible(
966 gd->fdt_blob, ofnode_to_offset(from), compat));
967 }
968}
Jens Wiklander7b68dad2018-08-20 11:09:58 +0200969
970ofnode ofnode_by_prop_value(ofnode from, const char *propname,
971 const void *propval, int proplen)
972{
973 if (of_live_active()) {
974 return np_to_ofnode(of_find_node_by_prop_value(
975 (struct device_node *)ofnode_to_np(from), propname,
976 propval, proplen));
977 } else {
978 return offset_to_ofnode(fdt_node_offset_by_prop_value(
979 gd->fdt_blob, ofnode_to_offset(from),
980 propname, propval, proplen));
981 }
982}
Mario Six047dafc2018-06-26 08:46:48 +0200983
984int ofnode_write_prop(ofnode node, const char *propname, int len,
985 const void *value)
986{
987 const struct device_node *np = ofnode_to_np(node);
988 struct property *pp;
989 struct property *pp_last = NULL;
990 struct property *new;
991
992 if (!of_live_active())
993 return -ENOSYS;
994
995 if (!np)
996 return -EINVAL;
997
998 for (pp = np->properties; pp; pp = pp->next) {
999 if (strcmp(pp->name, propname) == 0) {
1000 /* Property exists -> change value */
1001 pp->value = (void *)value;
1002 pp->length = len;
1003 return 0;
1004 }
1005 pp_last = pp;
1006 }
1007
1008 if (!pp_last)
1009 return -ENOENT;
1010
1011 /* Property does not exist -> append new property */
1012 new = malloc(sizeof(struct property));
1013 if (!new)
1014 return -ENOMEM;
1015
1016 new->name = strdup(propname);
Mario Six95e885d2018-10-04 09:22:24 +02001017 if (!new->name) {
1018 free(new);
Mario Six047dafc2018-06-26 08:46:48 +02001019 return -ENOMEM;
Mario Six95e885d2018-10-04 09:22:24 +02001020 }
Mario Six047dafc2018-06-26 08:46:48 +02001021
1022 new->value = (void *)value;
1023 new->length = len;
1024 new->next = NULL;
1025
1026 pp_last->next = new;
1027
1028 return 0;
1029}
1030
1031int ofnode_write_string(ofnode node, const char *propname, const char *value)
1032{
1033 if (!of_live_active())
1034 return -ENOSYS;
1035
1036 assert(ofnode_valid(node));
1037
1038 debug("%s: %s = %s", __func__, propname, value);
1039
1040 return ofnode_write_prop(node, propname, strlen(value) + 1, value);
1041}
1042
1043int ofnode_set_enabled(ofnode node, bool value)
1044{
1045 if (!of_live_active())
1046 return -ENOSYS;
1047
1048 assert(ofnode_valid(node));
1049
1050 if (value)
1051 return ofnode_write_string(node, "status", "okay");
1052 else
Bin Menga9274aa2019-07-05 09:23:17 -07001053 return ofnode_write_string(node, "status", "disabled");
Mario Six047dafc2018-06-26 08:46:48 +02001054}