blob: a68076bf351752be974853d54d5094e3af085255 [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 Glassc4fc5622017-05-18 20:08:58 -060019
20int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
21{
Dario Binacchib3f1cdd2020-03-29 18:04:42 +020022 return ofnode_read_u32_index(node, propname, 0, outp);
Simon Glassc4fc5622017-05-18 20:08:58 -060023}
24
Trent Piepho5b775412019-05-10 17:48:20 +000025u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
Simon Glassc4fc5622017-05-18 20:08:58 -060026{
27 assert(ofnode_valid(node));
Dario Binacchib3f1cdd2020-03-29 18:04:42 +020028 ofnode_read_u32_index(node, propname, 0, &def);
Simon Glassc4fc5622017-05-18 20:08:58 -060029
30 return def;
31}
32
Dario Binacchi81d80b52020-03-29 18:04:41 +020033int ofnode_read_u32_index(ofnode node, const char *propname, int index,
34 u32 *outp)
35{
36 const fdt32_t *cell;
37 int len;
38
39 assert(ofnode_valid(node));
40 debug("%s: %s: ", __func__, propname);
41
42 if (ofnode_is_np(node))
43 return of_read_u32_index(ofnode_to_np(node), propname, index,
44 outp);
45
46 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
47 &len);
48 if (!cell) {
49 debug("(not found)\n");
50 return -EINVAL;
51 }
52
53 if (len < (sizeof(int) * (index + 1))) {
54 debug("(not large enough)\n");
55 return -EOVERFLOW;
56 }
57
58 *outp = fdt32_to_cpu(cell[index]);
59 debug("%#x (%d)\n", *outp, *outp);
60
61 return 0;
62}
63
64u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
65 u32 def)
66{
67 assert(ofnode_valid(node));
68 ofnode_read_u32_index(node, propname, index, &def);
69
70 return def;
71}
72
Simon Glassc4fc5622017-05-18 20:08:58 -060073int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
74{
75 assert(ofnode_valid(node));
76 ofnode_read_u32(node, propname, (u32 *)&def);
77
78 return def;
79}
80
Simon Glass9d54a7a2018-06-11 13:07:10 -060081int ofnode_read_u64(ofnode node, const char *propname, u64 *outp)
82{
Jean-Jacques Hiblot487f9172019-10-22 10:05:22 +020083 const unaligned_fdt64_t *cell;
Simon Glass9d54a7a2018-06-11 13:07:10 -060084 int len;
85
86 assert(ofnode_valid(node));
87 debug("%s: %s: ", __func__, propname);
88
89 if (ofnode_is_np(node))
90 return of_read_u64(ofnode_to_np(node), propname, outp);
91
92 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
93 &len);
94 if (!cell || len < sizeof(*cell)) {
95 debug("(not found)\n");
96 return -EINVAL;
97 }
98 *outp = fdt64_to_cpu(cell[0]);
99 debug("%#llx (%lld)\n", (unsigned long long)*outp,
100 (unsigned long long)*outp);
101
102 return 0;
103}
104
T Karthik Reddy478860d2019-09-02 16:34:30 +0200105u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def)
Simon Glass9d54a7a2018-06-11 13:07:10 -0600106{
107 assert(ofnode_valid(node));
108 ofnode_read_u64(node, propname, &def);
109
110 return def;
111}
112
Simon Glassc4fc5622017-05-18 20:08:58 -0600113bool ofnode_read_bool(ofnode node, const char *propname)
114{
Masahiro Yamada5d434452017-06-22 16:54:07 +0900115 const void *prop;
Simon Glassc4fc5622017-05-18 20:08:58 -0600116
117 assert(ofnode_valid(node));
118 debug("%s: %s: ", __func__, propname);
119
Masahiro Yamada5d434452017-06-22 16:54:07 +0900120 prop = ofnode_get_property(node, propname, NULL);
121
122 debug("%s\n", prop ? "true" : "false");
Simon Glassc4fc5622017-05-18 20:08:58 -0600123
Masahiro Yamada5d434452017-06-22 16:54:07 +0900124 return prop ? true : false;
Simon Glassc4fc5622017-05-18 20:08:58 -0600125}
126
Simon Glass0c2e9802020-01-27 08:49:44 -0700127const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep)
Simon Glassc4fc5622017-05-18 20:08:58 -0600128{
Simon Glass0c2e9802020-01-27 08:49:44 -0700129 const char *val = NULL;
130 int len;
Simon Glassc4fc5622017-05-18 20:08:58 -0600131
132 assert(ofnode_valid(node));
133 debug("%s: %s: ", __func__, propname);
134
135 if (ofnode_is_np(node)) {
136 struct property *prop = of_find_property(
Simon Glass0c2e9802020-01-27 08:49:44 -0700137 ofnode_to_np(node), propname, &len);
Simon Glassc4fc5622017-05-18 20:08:58 -0600138
139 if (prop) {
Simon Glass0c2e9802020-01-27 08:49:44 -0700140 val = prop->value;
Simon Glassc4fc5622017-05-18 20:08:58 -0600141 len = prop->length;
142 }
143 } else {
Simon Glass0c2e9802020-01-27 08:49:44 -0700144 val = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
Simon Glassc4fc5622017-05-18 20:08:58 -0600145 propname, &len);
146 }
Simon Glass0c2e9802020-01-27 08:49:44 -0700147 if (!val) {
Simon Glassc4fc5622017-05-18 20:08:58 -0600148 debug("<not found>\n");
Simon Glass0c2e9802020-01-27 08:49:44 -0700149 if (sizep)
150 *sizep = -FDT_ERR_NOTFOUND;
Simon Glassc4fc5622017-05-18 20:08:58 -0600151 return NULL;
152 }
Simon Glass0c2e9802020-01-27 08:49:44 -0700153 if (sizep)
154 *sizep = len;
155
156 return val;
157}
158
159const char *ofnode_read_string(ofnode node, const char *propname)
160{
161 const char *str;
162 int len;
163
164 str = ofnode_read_prop(node, propname, &len);
165 if (!str)
166 return NULL;
167
Simon Glassc4fc5622017-05-18 20:08:58 -0600168 if (strnlen(str, len) >= len) {
169 debug("<invalid>\n");
170 return NULL;
171 }
172 debug("%s\n", str);
173
174 return str;
175}
176
Simon Glass81c54b32020-01-27 08:49:45 -0700177int ofnode_read_size(ofnode node, const char *propname)
178{
179 int len;
180
181 if (!ofnode_read_prop(node, propname, &len))
182 return -EINVAL;
183
184 return len;
185}
186
Simon Glassc4fc5622017-05-18 20:08:58 -0600187ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
188{
189 ofnode subnode;
190
191 assert(ofnode_valid(node));
192 debug("%s: %s: ", __func__, subnode_name);
193
194 if (ofnode_is_np(node)) {
195 const struct device_node *np = ofnode_to_np(node);
196
197 for (np = np->child; np; np = np->sibling) {
198 if (!strcmp(subnode_name, np->name))
199 break;
200 }
201 subnode = np_to_ofnode(np);
202 } else {
203 int ooffset = fdt_subnode_offset(gd->fdt_blob,
204 ofnode_to_offset(node), subnode_name);
205 subnode = offset_to_ofnode(ooffset);
206 }
207 debug("%s\n", ofnode_valid(subnode) ?
208 ofnode_get_name(subnode) : "<none>");
209
210 return subnode;
211}
212
213int ofnode_read_u32_array(ofnode node, const char *propname,
214 u32 *out_values, size_t sz)
215{
216 assert(ofnode_valid(node));
217 debug("%s: %s: ", __func__, propname);
218
219 if (ofnode_is_np(node)) {
220 return of_read_u32_array(ofnode_to_np(node), propname,
221 out_values, sz);
222 } else {
223 return fdtdec_get_int_array(gd->fdt_blob,
224 ofnode_to_offset(node), propname,
225 out_values, sz);
226 }
227}
228
229ofnode ofnode_first_subnode(ofnode node)
230{
231 assert(ofnode_valid(node));
232 if (ofnode_is_np(node))
233 return np_to_ofnode(node.np->child);
234
235 return offset_to_ofnode(
236 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
237}
238
239ofnode ofnode_next_subnode(ofnode node)
240{
241 assert(ofnode_valid(node));
242 if (ofnode_is_np(node))
243 return np_to_ofnode(node.np->sibling);
244
245 return offset_to_ofnode(
246 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
247}
248
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100249ofnode ofnode_get_parent(ofnode node)
250{
251 ofnode parent;
252
253 assert(ofnode_valid(node));
254 if (ofnode_is_np(node))
255 parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
256 else
257 parent.of_offset = fdt_parent_offset(gd->fdt_blob,
258 ofnode_to_offset(node));
259
260 return parent;
261}
262
Simon Glassc4fc5622017-05-18 20:08:58 -0600263const char *ofnode_get_name(ofnode node)
264{
Kever Yang8d7976d2019-07-19 11:23:47 +0800265 if (!ofnode_valid(node)) {
266 debug("%s node not valid\n", __func__);
267 return NULL;
268 }
269
Simon Glassc4fc5622017-05-18 20:08:58 -0600270 if (ofnode_is_np(node))
271 return strrchr(node.np->full_name, '/') + 1;
272
273 return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
274}
275
Kever Yang37df0e02018-02-23 17:38:50 +0100276ofnode ofnode_get_by_phandle(uint phandle)
277{
278 ofnode node;
279
280 if (of_live_active())
281 node = np_to_ofnode(of_find_node_by_phandle(phandle));
282 else
283 node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
284 phandle);
285
286 return node;
287}
288
Keerthyd332e6e2019-04-24 17:19:53 +0530289fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
Simon Glass049ae1b2017-05-18 20:09:01 -0600290{
Keerthy34222a32018-11-19 11:44:47 +0530291 int na, ns;
Keerthy34222a32018-11-19 11:44:47 +0530292
Simon Glass049ae1b2017-05-18 20:09:01 -0600293 if (ofnode_is_np(node)) {
294 const __be32 *prop_val;
Simon Glass47f85de2019-09-25 08:55:50 -0600295 u64 size64;
Simon Glass049ae1b2017-05-18 20:09:01 -0600296 uint flags;
Simon Glass049ae1b2017-05-18 20:09:01 -0600297
Simon Glass47f85de2019-09-25 08:55:50 -0600298 prop_val = of_get_address(ofnode_to_np(node), index, &size64,
299 &flags);
Simon Glass049ae1b2017-05-18 20:09:01 -0600300 if (!prop_val)
301 return FDT_ADDR_T_NONE;
Simon Glass47f85de2019-09-25 08:55:50 -0600302 if (size)
303 *size = size64;
Mario Sixd007ebc2017-12-20 09:52:12 +0100304
Mario Six35616ef2018-03-12 14:53:33 +0100305 ns = of_n_size_cells(ofnode_to_np(node));
306
307 if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
Mario Sixd007ebc2017-12-20 09:52:12 +0100308 return of_translate_address(ofnode_to_np(node), prop_val);
309 } else {
310 na = of_n_addr_cells(ofnode_to_np(node));
311 return of_read_number(prop_val, na);
312 }
Simon Glass049ae1b2017-05-18 20:09:01 -0600313 } else {
Keerthy34222a32018-11-19 11:44:47 +0530314 na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
315 ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
316 return fdtdec_get_addr_size_fixed(gd->fdt_blob,
317 ofnode_to_offset(node), "reg",
Keerthyd332e6e2019-04-24 17:19:53 +0530318 index, na, ns, size, true);
Simon Glass049ae1b2017-05-18 20:09:01 -0600319 }
320
321 return FDT_ADDR_T_NONE;
322}
323
Keerthyd332e6e2019-04-24 17:19:53 +0530324fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
325{
326 fdt_size_t size;
327
328 return ofnode_get_addr_size_index(node, index, &size);
329}
330
Simon Glass049ae1b2017-05-18 20:09:01 -0600331fdt_addr_t ofnode_get_addr(ofnode node)
332{
333 return ofnode_get_addr_index(node, 0);
334}
335
Simon Glassc4fc5622017-05-18 20:08:58 -0600336int ofnode_stringlist_search(ofnode node, const char *property,
337 const char *string)
338{
339 if (ofnode_is_np(node)) {
340 return of_property_match_string(ofnode_to_np(node),
341 property, string);
342 } else {
343 int ret;
344
345 ret = fdt_stringlist_search(gd->fdt_blob,
346 ofnode_to_offset(node), property,
347 string);
348 if (ret == -FDT_ERR_NOTFOUND)
349 return -ENODATA;
350 else if (ret < 0)
351 return -EINVAL;
352
353 return ret;
354 }
355}
356
357int ofnode_read_string_index(ofnode node, const char *property, int index,
358 const char **outp)
359{
360 if (ofnode_is_np(node)) {
361 return of_property_read_string_index(ofnode_to_np(node),
362 property, index, outp);
363 } else {
364 int len;
365
366 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
367 property, index, &len);
368 if (len < 0)
369 return -EINVAL;
370 return 0;
371 }
372}
373
Simon Glass5fdb0052017-06-12 06:21:28 -0600374int ofnode_read_string_count(ofnode node, const char *property)
375{
376 if (ofnode_is_np(node)) {
377 return of_property_count_strings(ofnode_to_np(node), property);
378 } else {
379 return fdt_stringlist_count(gd->fdt_blob,
380 ofnode_to_offset(node), property);
381 }
382}
383
Simon Glassc4fc5622017-05-18 20:08:58 -0600384static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
385 struct ofnode_phandle_args *out)
386{
387 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
388 out->node = offset_to_ofnode(in->node);
389 out->args_count = in->args_count;
390 memcpy(out->args, in->args, sizeof(out->args));
391}
392
393static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
394 struct ofnode_phandle_args *out)
395{
396 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
397 out->node = np_to_ofnode(in->np);
398 out->args_count = in->args_count;
399 memcpy(out->args, in->args, sizeof(out->args));
400}
401
402int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
403 const char *cells_name, int cell_count,
404 int index,
405 struct ofnode_phandle_args *out_args)
406{
407 if (ofnode_is_np(node)) {
408 struct of_phandle_args args;
409 int ret;
410
411 ret = of_parse_phandle_with_args(ofnode_to_np(node),
Patrick Delaunay21e3b042020-09-10 18:26:17 +0200412 list_name, cells_name,
413 cell_count, index,
Mario Sixf40d82c2018-01-15 11:07:17 +0100414 &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600415 if (ret)
416 return ret;
417 ofnode_from_of_phandle_args(&args, out_args);
418 } else {
419 struct fdtdec_phandle_args args;
420 int ret;
421
422 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
Mario Sixf40d82c2018-01-15 11:07:17 +0100423 ofnode_to_offset(node),
424 list_name, cells_name,
425 cell_count, index, &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600426 if (ret)
427 return ret;
428 ofnode_from_fdtdec_phandle_args(&args, out_args);
429 }
430
431 return 0;
432}
433
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200434int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200435 const char *cells_name, int cell_count)
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200436{
437 if (ofnode_is_np(node))
438 return of_count_phandle_with_args(ofnode_to_np(node),
Patrick Delaunayd776a842020-09-25 09:41:14 +0200439 list_name, cells_name, cell_count);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200440 else
441 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
442 ofnode_to_offset(node), list_name, cells_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200443 cell_count, -1, NULL);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200444}
445
Simon Glassc4fc5622017-05-18 20:08:58 -0600446ofnode ofnode_path(const char *path)
447{
448 if (of_live_active())
449 return np_to_ofnode(of_find_node_by_path(path));
450 else
451 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
452}
453
Simon Glasse09223c2020-01-27 08:49:46 -0700454const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
Simon Glassc4fc5622017-05-18 20:08:58 -0600455{
456 ofnode chosen_node;
457
458 chosen_node = ofnode_path("/chosen");
459
Simon Glasse09223c2020-01-27 08:49:46 -0700460 return ofnode_read_prop(chosen_node, propname, sizep);
Simon Glassc4fc5622017-05-18 20:08:58 -0600461}
462
Simon Glasse09223c2020-01-27 08:49:46 -0700463const char *ofnode_read_chosen_string(const char *propname)
464{
465 return ofnode_read_chosen_prop(propname, NULL);
466}
467
Simon Glassc4fc5622017-05-18 20:08:58 -0600468ofnode ofnode_get_chosen_node(const char *name)
469{
470 const char *prop;
471
Simon Glasse09223c2020-01-27 08:49:46 -0700472 prop = ofnode_read_chosen_prop(name, NULL);
Simon Glassc4fc5622017-05-18 20:08:58 -0600473 if (!prop)
474 return ofnode_null();
475
476 return ofnode_path(prop);
477}
478
Michal Simek92a88622020-07-28 12:51:08 +0200479const void *ofnode_read_aliases_prop(const char *propname, int *sizep)
480{
481 ofnode node;
482
483 node = ofnode_path("/aliases");
484
485 return ofnode_read_prop(node, propname, sizep);
486}
487
488ofnode ofnode_get_aliases_node(const char *name)
489{
490 const char *prop;
491
492 prop = ofnode_read_aliases_prop(name, NULL);
493 if (!prop)
494 return ofnode_null();
495
496 debug("%s: node_path: %s\n", __func__, prop);
497
498 return ofnode_path(prop);
499}
500
developerd93c8b42020-05-02 11:35:09 +0200501int ofnode_get_child_count(ofnode parent)
502{
503 ofnode child;
504 int num = 0;
505
506 ofnode_for_each_subnode(child, parent)
507 num++;
508
509 return num;
510}
511
Simon Glassc4fc5622017-05-18 20:08:58 -0600512static int decode_timing_property(ofnode node, const char *name,
513 struct timing_entry *result)
514{
515 int length, ret = 0;
516
517 length = ofnode_read_size(node, name);
518 if (length < 0) {
519 debug("%s: could not find property %s\n",
520 ofnode_get_name(node), name);
521 return length;
522 }
523
524 if (length == sizeof(u32)) {
525 result->typ = ofnode_read_u32_default(node, name, 0);
526 result->min = result->typ;
527 result->max = result->typ;
528 } else {
529 ret = ofnode_read_u32_array(node, name, &result->min, 3);
530 }
531
532 return ret;
533}
534
535int ofnode_decode_display_timing(ofnode parent, int index,
536 struct display_timing *dt)
537{
538 int i;
539 ofnode timings, node;
540 u32 val = 0;
541 int ret = 0;
542
543 timings = ofnode_find_subnode(parent, "display-timings");
544 if (!ofnode_valid(timings))
545 return -EINVAL;
546
Simon Glass28529762017-08-05 15:45:54 -0600547 i = 0;
548 ofnode_for_each_subnode(node, timings) {
549 if (i++ == index)
550 break;
551 }
Simon Glassc4fc5622017-05-18 20:08:58 -0600552
553 if (!ofnode_valid(node))
554 return -EINVAL;
555
556 memset(dt, 0, sizeof(*dt));
557
558 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
559 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
560 ret |= decode_timing_property(node, "hactive", &dt->hactive);
561 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
562 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
563 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
564 ret |= decode_timing_property(node, "vactive", &dt->vactive);
565 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
566 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
567
568 dt->flags = 0;
569 val = ofnode_read_u32_default(node, "vsync-active", -1);
570 if (val != -1) {
571 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
572 DISPLAY_FLAGS_VSYNC_LOW;
573 }
574 val = ofnode_read_u32_default(node, "hsync-active", -1);
575 if (val != -1) {
576 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
577 DISPLAY_FLAGS_HSYNC_LOW;
578 }
579 val = ofnode_read_u32_default(node, "de-active", -1);
580 if (val != -1) {
581 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
582 DISPLAY_FLAGS_DE_LOW;
583 }
584 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
585 if (val != -1) {
586 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
587 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
588 }
589
590 if (ofnode_read_bool(node, "interlaced"))
591 dt->flags |= DISPLAY_FLAGS_INTERLACED;
592 if (ofnode_read_bool(node, "doublescan"))
593 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
594 if (ofnode_read_bool(node, "doubleclk"))
595 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
596
597 return ret;
598}
599
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900600const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
Simon Glassc4fc5622017-05-18 20:08:58 -0600601{
Masahiro Yamada5052f1b2017-06-22 16:54:04 +0900602 if (ofnode_is_np(node))
603 return of_get_property(ofnode_to_np(node), propname, lenp);
604 else
Simon Glassc4fc5622017-05-18 20:08:58 -0600605 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
606 propname, lenp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600607}
608
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100609int ofnode_get_first_property(ofnode node, struct ofprop *prop)
610{
611 prop->node = node;
612
613 if (ofnode_is_np(node)) {
614 prop->prop = of_get_first_property(ofnode_to_np(prop->node));
615 if (!prop->prop)
616 return -FDT_ERR_NOTFOUND;
617 } else {
618 prop->offset =
619 fdt_first_property_offset(gd->fdt_blob,
620 ofnode_to_offset(prop->node));
621 if (prop->offset < 0)
622 return prop->offset;
623 }
624
625 return 0;
626}
627
628int ofnode_get_next_property(struct ofprop *prop)
629{
630 if (ofnode_is_np(prop->node)) {
631 prop->prop = of_get_next_property(ofnode_to_np(prop->node),
632 prop->prop);
633 if (!prop->prop)
634 return -FDT_ERR_NOTFOUND;
635 } else {
636 prop->offset = fdt_next_property_offset(gd->fdt_blob,
637 prop->offset);
638 if (prop->offset < 0)
639 return prop->offset;
640 }
641
642 return 0;
643}
644
645const void *ofnode_get_property_by_prop(const struct ofprop *prop,
646 const char **propname, int *lenp)
647{
648 if (ofnode_is_np(prop->node))
649 return of_get_property_by_prop(ofnode_to_np(prop->node),
650 prop->prop, propname, lenp);
651 else
652 return fdt_getprop_by_offset(gd->fdt_blob,
653 prop->offset,
654 propname, lenp);
655}
656
Simon Glassc4fc5622017-05-18 20:08:58 -0600657bool ofnode_is_available(ofnode node)
658{
659 if (ofnode_is_np(node))
660 return of_device_is_available(ofnode_to_np(node));
661 else
662 return fdtdec_get_is_enabled(gd->fdt_blob,
663 ofnode_to_offset(node));
664}
665
666fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
667 fdt_size_t *sizep)
668{
669 if (ofnode_is_np(node)) {
670 int na, ns;
671 int psize;
672 const struct device_node *np = ofnode_to_np(node);
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200673 const __be32 *prop = of_get_property(np, property, &psize);
Simon Glassc4fc5622017-05-18 20:08:58 -0600674
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200675 if (!prop)
676 return FDT_ADDR_T_NONE;
Simon Glassc4fc5622017-05-18 20:08:58 -0600677 na = of_n_addr_cells(np);
Marek Vasut1638c172018-10-01 12:37:19 +0200678 ns = of_n_size_cells(np);
Simon Glassa67cc632017-05-18 20:09:27 -0600679 *sizep = of_read_number(prop + na, ns);
Marek Vasuta9dac492018-10-01 12:37:20 +0200680
Simon Glass019f9562019-04-25 21:58:36 -0600681 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
Marek Vasuta9dac492018-10-01 12:37:20 +0200682 return of_translate_address(np, prop);
683 else
684 return of_read_number(prop, na);
Simon Glassc4fc5622017-05-18 20:08:58 -0600685 } else {
686 return fdtdec_get_addr_size(gd->fdt_blob,
687 ofnode_to_offset(node), property,
688 sizep);
689 }
690}
691
692const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
693 size_t sz)
694{
695 if (ofnode_is_np(node)) {
696 const struct device_node *np = ofnode_to_np(node);
697 int psize;
698 const __be32 *prop = of_get_property(np, propname, &psize);
699
700 if (!prop || sz != psize)
701 return NULL;
702 return (uint8_t *)prop;
703
704 } else {
705 return fdtdec_locate_byte_array(gd->fdt_blob,
706 ofnode_to_offset(node), propname, sz);
707 }
708}
709
710int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
711 const char *propname, struct fdt_pci_addr *addr)
712{
Masahiro Yamada5c5991e2017-06-22 17:57:50 +0900713 const fdt32_t *cell;
Simon Glassc4fc5622017-05-18 20:08:58 -0600714 int len;
715 int ret = -ENOENT;
716
717 debug("%s: %s: ", __func__, propname);
718
719 /*
720 * If we follow the pci bus bindings strictly, we should check
721 * the value of the node's parent node's #address-cells and
722 * #size-cells. They need to be 3 and 2 accordingly. However,
723 * for simplicity we skip the check here.
724 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900725 cell = ofnode_get_property(node, propname, &len);
Simon Glassc4fc5622017-05-18 20:08:58 -0600726 if (!cell)
727 goto fail;
728
729 if ((len % FDT_PCI_REG_SIZE) == 0) {
730 int num = len / FDT_PCI_REG_SIZE;
731 int i;
732
733 for (i = 0; i < num; i++) {
734 debug("pci address #%d: %08lx %08lx %08lx\n", i,
735 (ulong)fdt32_to_cpu(cell[0]),
736 (ulong)fdt32_to_cpu(cell[1]),
737 (ulong)fdt32_to_cpu(cell[2]));
738 if ((fdt32_to_cpu(*cell) & type) == type) {
739 addr->phys_hi = fdt32_to_cpu(cell[0]);
740 addr->phys_mid = fdt32_to_cpu(cell[1]);
Simon Glassdfd43152019-09-25 08:55:46 -0600741 addr->phys_lo = fdt32_to_cpu(cell[2]);
Simon Glassc4fc5622017-05-18 20:08:58 -0600742 break;
Simon Glassc4fc5622017-05-18 20:08:58 -0600743 }
Mario Sixf40d82c2018-01-15 11:07:17 +0100744
745 cell += (FDT_PCI_ADDR_CELLS +
746 FDT_PCI_SIZE_CELLS);
Simon Glassc4fc5622017-05-18 20:08:58 -0600747 }
748
749 if (i == num) {
750 ret = -ENXIO;
751 goto fail;
752 }
753
754 return 0;
Simon Glassc4fc5622017-05-18 20:08:58 -0600755 }
756
Mario Sixf40d82c2018-01-15 11:07:17 +0100757 ret = -EINVAL;
758
Simon Glassc4fc5622017-05-18 20:08:58 -0600759fail:
760 debug("(not found)\n");
761 return ret;
762}
763
Bin Mengfa157712018-08-03 01:14:35 -0700764int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
765{
766 const char *list, *end;
767 int len;
768
769 list = ofnode_get_property(node, "compatible", &len);
770 if (!list)
771 return -ENOENT;
772
773 end = list + len;
774 while (list < end) {
775 len = strlen(list);
776 if (len >= strlen("pciVVVV,DDDD")) {
777 char *s = strstr(list, "pci");
778
779 /*
780 * check if the string is something like pciVVVV,DDDD.RR
781 * or just pciVVVV,DDDD
782 */
783 if (s && s[7] == ',' &&
784 (s[12] == '.' || s[12] == 0)) {
785 s += 3;
786 *vendor = simple_strtol(s, NULL, 16);
787
788 s += 5;
789 *device = simple_strtol(s, NULL, 16);
790
791 return 0;
792 }
793 }
794 list += (len + 1);
795 }
796
797 return -ENOENT;
798}
799
Simon Glassc4fc5622017-05-18 20:08:58 -0600800int ofnode_read_addr_cells(ofnode node)
801{
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200802 if (ofnode_is_np(node)) {
Simon Glassc4fc5622017-05-18 20:08:58 -0600803 return of_n_addr_cells(ofnode_to_np(node));
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200804 } else {
805 int parent = fdt_parent_offset(gd->fdt_blob,
806 ofnode_to_offset(node));
807
808 return fdt_address_cells(gd->fdt_blob, parent);
809 }
Simon Glassc4fc5622017-05-18 20:08:58 -0600810}
811
812int ofnode_read_size_cells(ofnode node)
813{
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200814 if (ofnode_is_np(node)) {
Simon Glassc4fc5622017-05-18 20:08:58 -0600815 return of_n_size_cells(ofnode_to_np(node));
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200816 } else {
817 int parent = fdt_parent_offset(gd->fdt_blob,
818 ofnode_to_offset(node));
819
820 return fdt_size_cells(gd->fdt_blob, parent);
821 }
Simon Glass4191dc12017-06-12 06:21:31 -0600822}
823
824int ofnode_read_simple_addr_cells(ofnode node)
825{
826 if (ofnode_is_np(node))
827 return of_simple_addr_cells(ofnode_to_np(node));
828 else
829 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
830}
831
832int ofnode_read_simple_size_cells(ofnode node)
833{
834 if (ofnode_is_np(node))
835 return of_simple_size_cells(ofnode_to_np(node));
Simon Glassc4fc5622017-05-18 20:08:58 -0600836 else
837 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
838}
839
840bool ofnode_pre_reloc(ofnode node)
841{
Patrick Delaunay0b025b82019-02-11 12:49:57 +0100842#if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
843 /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
844 * had property dm-pre-reloc or u-boot,dm-spl/tpl.
845 * They are removed in final dtb (fdtgrep 2nd pass)
846 */
847 return true;
848#else
Masahiro Yamada6a61dd92017-06-22 16:54:03 +0900849 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
Simon Glassc4fc5622017-05-18 20:08:58 -0600850 return true;
Simon Glass23f22842018-10-01 12:22:18 -0600851 if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
852 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -0600853
Simon Glassc4fc5622017-05-18 20:08:58 -0600854 /*
855 * In regular builds individual spl and tpl handling both
856 * count as handled pre-relocation for later second init.
857 */
Masahiro Yamada6a61dd92017-06-22 16:54:03 +0900858 if (ofnode_read_bool(node, "u-boot,dm-spl") ||
859 ofnode_read_bool(node, "u-boot,dm-tpl"))
Simon Glassc4fc5622017-05-18 20:08:58 -0600860 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -0600861
862 return false;
Patrick Delaunay0b025b82019-02-11 12:49:57 +0100863#endif
Simon Glassc4fc5622017-05-18 20:08:58 -0600864}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600865
866int ofnode_read_resource(ofnode node, uint index, struct resource *res)
867{
868 if (ofnode_is_np(node)) {
869 return of_address_to_resource(ofnode_to_np(node), index, res);
870 } else {
871 struct fdt_resource fres;
872 int ret;
873
874 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
875 "reg", index, &fres);
876 if (ret < 0)
877 return -EINVAL;
878 memset(res, '\0', sizeof(*res));
879 res->start = fres.start;
880 res->end = fres.end;
881
882 return 0;
883 }
884}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900885
886int ofnode_read_resource_byname(ofnode node, const char *name,
887 struct resource *res)
888{
889 int index;
890
891 index = ofnode_stringlist_search(node, "reg-names", name);
892 if (index < 0)
893 return index;
894
895 return ofnode_read_resource(node, index, res);
896}
Mario Sixaefac062018-01-15 11:07:19 +0100897
898u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
899{
900 if (ofnode_is_np(node))
901 return of_translate_address(ofnode_to_np(node), in_addr);
902 else
903 return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
904}
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900905
Fabien Dessenne22236e02019-05-31 15:11:30 +0200906u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
907{
908 if (ofnode_is_np(node))
909 return of_translate_dma_address(ofnode_to_np(node), in_addr);
910 else
911 return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
912}
913
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900914int ofnode_device_is_compatible(ofnode node, const char *compat)
915{
916 if (ofnode_is_np(node))
917 return of_device_is_compatible(ofnode_to_np(node), compat,
918 NULL, NULL);
919 else
920 return !fdt_node_check_compatible(gd->fdt_blob,
921 ofnode_to_offset(node),
922 compat);
923}
Simon Glass954eeae2018-06-11 13:07:13 -0600924
925ofnode ofnode_by_compatible(ofnode from, const char *compat)
926{
927 if (of_live_active()) {
928 return np_to_ofnode(of_find_compatible_node(
929 (struct device_node *)ofnode_to_np(from), NULL,
930 compat));
931 } else {
932 return offset_to_ofnode(fdt_node_offset_by_compatible(
933 gd->fdt_blob, ofnode_to_offset(from), compat));
934 }
935}
Jens Wiklander7b68dad2018-08-20 11:09:58 +0200936
937ofnode ofnode_by_prop_value(ofnode from, const char *propname,
938 const void *propval, int proplen)
939{
940 if (of_live_active()) {
941 return np_to_ofnode(of_find_node_by_prop_value(
942 (struct device_node *)ofnode_to_np(from), propname,
943 propval, proplen));
944 } else {
945 return offset_to_ofnode(fdt_node_offset_by_prop_value(
946 gd->fdt_blob, ofnode_to_offset(from),
947 propname, propval, proplen));
948 }
949}
Mario Six047dafc2018-06-26 08:46:48 +0200950
951int ofnode_write_prop(ofnode node, const char *propname, int len,
952 const void *value)
953{
954 const struct device_node *np = ofnode_to_np(node);
955 struct property *pp;
956 struct property *pp_last = NULL;
957 struct property *new;
958
959 if (!of_live_active())
960 return -ENOSYS;
961
962 if (!np)
963 return -EINVAL;
964
965 for (pp = np->properties; pp; pp = pp->next) {
966 if (strcmp(pp->name, propname) == 0) {
967 /* Property exists -> change value */
968 pp->value = (void *)value;
969 pp->length = len;
970 return 0;
971 }
972 pp_last = pp;
973 }
974
975 if (!pp_last)
976 return -ENOENT;
977
978 /* Property does not exist -> append new property */
979 new = malloc(sizeof(struct property));
980 if (!new)
981 return -ENOMEM;
982
983 new->name = strdup(propname);
Mario Six95e885d2018-10-04 09:22:24 +0200984 if (!new->name) {
985 free(new);
Mario Six047dafc2018-06-26 08:46:48 +0200986 return -ENOMEM;
Mario Six95e885d2018-10-04 09:22:24 +0200987 }
Mario Six047dafc2018-06-26 08:46:48 +0200988
989 new->value = (void *)value;
990 new->length = len;
991 new->next = NULL;
992
993 pp_last->next = new;
994
995 return 0;
996}
997
998int ofnode_write_string(ofnode node, const char *propname, const char *value)
999{
1000 if (!of_live_active())
1001 return -ENOSYS;
1002
1003 assert(ofnode_valid(node));
1004
1005 debug("%s: %s = %s", __func__, propname, value);
1006
1007 return ofnode_write_prop(node, propname, strlen(value) + 1, value);
1008}
1009
1010int ofnode_set_enabled(ofnode node, bool value)
1011{
1012 if (!of_live_active())
1013 return -ENOSYS;
1014
1015 assert(ofnode_valid(node));
1016
1017 if (value)
1018 return ofnode_write_string(node, "status", "okay");
1019 else
Bin Menga9274aa2019-07-05 09:23:17 -07001020 return ofnode_write_string(node, "status", "disabled");
Mario Six047dafc2018-06-26 08:46:48 +02001021}