blob: 2a6e43ddc6b93073dbfc36304a5996e9e7bd9577 [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
Simon Glass39f1d282020-12-16 17:25:06 -0700229#if !CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
Simon Glass5de5b3b2020-11-28 17:50:02 -0700230bool ofnode_is_enabled(ofnode node)
231{
232 if (ofnode_is_np(node)) {
233 return of_device_is_available(ofnode_to_np(node));
234 } else {
235 return fdtdec_get_is_enabled(gd->fdt_blob,
236 ofnode_to_offset(node));
237 }
238}
239
Simon Glassc4fc5622017-05-18 20:08:58 -0600240ofnode ofnode_first_subnode(ofnode node)
241{
242 assert(ofnode_valid(node));
243 if (ofnode_is_np(node))
244 return np_to_ofnode(node.np->child);
245
246 return offset_to_ofnode(
247 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
248}
249
250ofnode ofnode_next_subnode(ofnode node)
251{
252 assert(ofnode_valid(node));
253 if (ofnode_is_np(node))
254 return np_to_ofnode(node.np->sibling);
255
256 return offset_to_ofnode(
257 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
258}
Simon Glass39f1d282020-12-16 17:25:06 -0700259#endif /* !DM_INLINE_OFNODE */
Simon Glassc4fc5622017-05-18 20:08:58 -0600260
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100261ofnode ofnode_get_parent(ofnode node)
262{
263 ofnode parent;
264
265 assert(ofnode_valid(node));
266 if (ofnode_is_np(node))
267 parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
268 else
269 parent.of_offset = fdt_parent_offset(gd->fdt_blob,
270 ofnode_to_offset(node));
271
272 return parent;
273}
274
Simon Glassc4fc5622017-05-18 20:08:58 -0600275const char *ofnode_get_name(ofnode node)
276{
Kever Yang8d7976d2019-07-19 11:23:47 +0800277 if (!ofnode_valid(node)) {
278 debug("%s node not valid\n", __func__);
279 return NULL;
280 }
281
Simon Glassc4fc5622017-05-18 20:08:58 -0600282 if (ofnode_is_np(node))
283 return strrchr(node.np->full_name, '/') + 1;
284
285 return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
286}
287
Kever Yang37df0e02018-02-23 17:38:50 +0100288ofnode ofnode_get_by_phandle(uint phandle)
289{
290 ofnode node;
291
292 if (of_live_active())
293 node = np_to_ofnode(of_find_node_by_phandle(phandle));
294 else
295 node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
296 phandle);
297
298 return node;
299}
300
Keerthyd332e6e2019-04-24 17:19:53 +0530301fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
Simon Glass049ae1b2017-05-18 20:09:01 -0600302{
Keerthy34222a32018-11-19 11:44:47 +0530303 int na, ns;
Keerthy34222a32018-11-19 11:44:47 +0530304
Simon Glass049ae1b2017-05-18 20:09:01 -0600305 if (ofnode_is_np(node)) {
306 const __be32 *prop_val;
Simon Glass47f85de2019-09-25 08:55:50 -0600307 u64 size64;
Simon Glass049ae1b2017-05-18 20:09:01 -0600308 uint flags;
Simon Glass049ae1b2017-05-18 20:09:01 -0600309
Simon Glass47f85de2019-09-25 08:55:50 -0600310 prop_val = of_get_address(ofnode_to_np(node), index, &size64,
311 &flags);
Simon Glass049ae1b2017-05-18 20:09:01 -0600312 if (!prop_val)
313 return FDT_ADDR_T_NONE;
Simon Glass47f85de2019-09-25 08:55:50 -0600314 if (size)
315 *size = size64;
Mario Sixd007ebc2017-12-20 09:52:12 +0100316
Mario Six35616ef2018-03-12 14:53:33 +0100317 ns = of_n_size_cells(ofnode_to_np(node));
318
319 if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
Mario Sixd007ebc2017-12-20 09:52:12 +0100320 return of_translate_address(ofnode_to_np(node), prop_val);
321 } else {
322 na = of_n_addr_cells(ofnode_to_np(node));
323 return of_read_number(prop_val, na);
324 }
Simon Glass049ae1b2017-05-18 20:09:01 -0600325 } else {
Keerthy34222a32018-11-19 11:44:47 +0530326 na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
327 ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
328 return fdtdec_get_addr_size_fixed(gd->fdt_blob,
329 ofnode_to_offset(node), "reg",
Keerthyd332e6e2019-04-24 17:19:53 +0530330 index, na, ns, size, true);
Simon Glass049ae1b2017-05-18 20:09:01 -0600331 }
332
333 return FDT_ADDR_T_NONE;
334}
335
Keerthyd332e6e2019-04-24 17:19:53 +0530336fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
337{
338 fdt_size_t size;
339
340 return ofnode_get_addr_size_index(node, index, &size);
341}
342
Simon Glass049ae1b2017-05-18 20:09:01 -0600343fdt_addr_t ofnode_get_addr(ofnode node)
344{
345 return ofnode_get_addr_index(node, 0);
346}
347
Simon Glassc4fc5622017-05-18 20:08:58 -0600348int ofnode_stringlist_search(ofnode node, const char *property,
349 const char *string)
350{
351 if (ofnode_is_np(node)) {
352 return of_property_match_string(ofnode_to_np(node),
353 property, string);
354 } else {
355 int ret;
356
357 ret = fdt_stringlist_search(gd->fdt_blob,
358 ofnode_to_offset(node), property,
359 string);
360 if (ret == -FDT_ERR_NOTFOUND)
361 return -ENODATA;
362 else if (ret < 0)
363 return -EINVAL;
364
365 return ret;
366 }
367}
368
369int ofnode_read_string_index(ofnode node, const char *property, int index,
370 const char **outp)
371{
372 if (ofnode_is_np(node)) {
373 return of_property_read_string_index(ofnode_to_np(node),
374 property, index, outp);
375 } else {
376 int len;
377
378 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
379 property, index, &len);
380 if (len < 0)
381 return -EINVAL;
382 return 0;
383 }
384}
385
Simon Glass5fdb0052017-06-12 06:21:28 -0600386int ofnode_read_string_count(ofnode node, const char *property)
387{
388 if (ofnode_is_np(node)) {
389 return of_property_count_strings(ofnode_to_np(node), property);
390 } else {
391 return fdt_stringlist_count(gd->fdt_blob,
392 ofnode_to_offset(node), property);
393 }
394}
395
Simon Glassc4fc5622017-05-18 20:08:58 -0600396static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
397 struct ofnode_phandle_args *out)
398{
399 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
400 out->node = offset_to_ofnode(in->node);
401 out->args_count = in->args_count;
402 memcpy(out->args, in->args, sizeof(out->args));
403}
404
405static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
406 struct ofnode_phandle_args *out)
407{
408 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
409 out->node = np_to_ofnode(in->np);
410 out->args_count = in->args_count;
411 memcpy(out->args, in->args, sizeof(out->args));
412}
413
414int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
415 const char *cells_name, int cell_count,
416 int index,
417 struct ofnode_phandle_args *out_args)
418{
419 if (ofnode_is_np(node)) {
420 struct of_phandle_args args;
421 int ret;
422
423 ret = of_parse_phandle_with_args(ofnode_to_np(node),
Patrick Delaunay21e3b042020-09-10 18:26:17 +0200424 list_name, cells_name,
425 cell_count, index,
Mario Sixf40d82c2018-01-15 11:07:17 +0100426 &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600427 if (ret)
428 return ret;
429 ofnode_from_of_phandle_args(&args, out_args);
430 } else {
431 struct fdtdec_phandle_args args;
432 int ret;
433
434 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
Mario Sixf40d82c2018-01-15 11:07:17 +0100435 ofnode_to_offset(node),
436 list_name, cells_name,
437 cell_count, index, &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600438 if (ret)
439 return ret;
440 ofnode_from_fdtdec_phandle_args(&args, out_args);
441 }
442
443 return 0;
444}
445
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200446int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200447 const char *cells_name, int cell_count)
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200448{
449 if (ofnode_is_np(node))
450 return of_count_phandle_with_args(ofnode_to_np(node),
Patrick Delaunayd776a842020-09-25 09:41:14 +0200451 list_name, cells_name, cell_count);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200452 else
453 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
454 ofnode_to_offset(node), list_name, cells_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200455 cell_count, -1, NULL);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200456}
457
Simon Glassc4fc5622017-05-18 20:08:58 -0600458ofnode ofnode_path(const char *path)
459{
460 if (of_live_active())
461 return np_to_ofnode(of_find_node_by_path(path));
462 else
463 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
464}
465
Simon Glasse09223c2020-01-27 08:49:46 -0700466const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
Simon Glassc4fc5622017-05-18 20:08:58 -0600467{
468 ofnode chosen_node;
469
470 chosen_node = ofnode_path("/chosen");
471
Simon Glasse09223c2020-01-27 08:49:46 -0700472 return ofnode_read_prop(chosen_node, propname, sizep);
Simon Glassc4fc5622017-05-18 20:08:58 -0600473}
474
Simon Glasse09223c2020-01-27 08:49:46 -0700475const char *ofnode_read_chosen_string(const char *propname)
476{
477 return ofnode_read_chosen_prop(propname, NULL);
478}
479
Simon Glassc4fc5622017-05-18 20:08:58 -0600480ofnode ofnode_get_chosen_node(const char *name)
481{
482 const char *prop;
483
Simon Glasse09223c2020-01-27 08:49:46 -0700484 prop = ofnode_read_chosen_prop(name, NULL);
Simon Glassc4fc5622017-05-18 20:08:58 -0600485 if (!prop)
486 return ofnode_null();
487
488 return ofnode_path(prop);
489}
490
Michal Simek92a88622020-07-28 12:51:08 +0200491const void *ofnode_read_aliases_prop(const char *propname, int *sizep)
492{
493 ofnode node;
494
495 node = ofnode_path("/aliases");
496
497 return ofnode_read_prop(node, propname, sizep);
498}
499
500ofnode ofnode_get_aliases_node(const char *name)
501{
502 const char *prop;
503
504 prop = ofnode_read_aliases_prop(name, NULL);
505 if (!prop)
506 return ofnode_null();
507
508 debug("%s: node_path: %s\n", __func__, prop);
509
510 return ofnode_path(prop);
511}
512
developerd93c8b42020-05-02 11:35:09 +0200513int ofnode_get_child_count(ofnode parent)
514{
515 ofnode child;
516 int num = 0;
517
518 ofnode_for_each_subnode(child, parent)
519 num++;
520
521 return num;
522}
523
Simon Glassc4fc5622017-05-18 20:08:58 -0600524static int decode_timing_property(ofnode node, const char *name,
525 struct timing_entry *result)
526{
527 int length, ret = 0;
528
529 length = ofnode_read_size(node, name);
530 if (length < 0) {
531 debug("%s: could not find property %s\n",
532 ofnode_get_name(node), name);
533 return length;
534 }
535
536 if (length == sizeof(u32)) {
537 result->typ = ofnode_read_u32_default(node, name, 0);
538 result->min = result->typ;
539 result->max = result->typ;
540 } else {
541 ret = ofnode_read_u32_array(node, name, &result->min, 3);
542 }
543
544 return ret;
545}
546
547int ofnode_decode_display_timing(ofnode parent, int index,
548 struct display_timing *dt)
549{
550 int i;
551 ofnode timings, node;
552 u32 val = 0;
553 int ret = 0;
554
555 timings = ofnode_find_subnode(parent, "display-timings");
556 if (!ofnode_valid(timings))
557 return -EINVAL;
558
Simon Glass28529762017-08-05 15:45:54 -0600559 i = 0;
560 ofnode_for_each_subnode(node, timings) {
561 if (i++ == index)
562 break;
563 }
Simon Glassc4fc5622017-05-18 20:08:58 -0600564
565 if (!ofnode_valid(node))
566 return -EINVAL;
567
568 memset(dt, 0, sizeof(*dt));
569
570 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
571 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
572 ret |= decode_timing_property(node, "hactive", &dt->hactive);
573 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
574 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
575 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
576 ret |= decode_timing_property(node, "vactive", &dt->vactive);
577 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
578 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
579
580 dt->flags = 0;
581 val = ofnode_read_u32_default(node, "vsync-active", -1);
582 if (val != -1) {
583 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
584 DISPLAY_FLAGS_VSYNC_LOW;
585 }
586 val = ofnode_read_u32_default(node, "hsync-active", -1);
587 if (val != -1) {
588 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
589 DISPLAY_FLAGS_HSYNC_LOW;
590 }
591 val = ofnode_read_u32_default(node, "de-active", -1);
592 if (val != -1) {
593 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
594 DISPLAY_FLAGS_DE_LOW;
595 }
596 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
597 if (val != -1) {
598 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
599 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
600 }
601
602 if (ofnode_read_bool(node, "interlaced"))
603 dt->flags |= DISPLAY_FLAGS_INTERLACED;
604 if (ofnode_read_bool(node, "doublescan"))
605 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
606 if (ofnode_read_bool(node, "doubleclk"))
607 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
608
609 return ret;
610}
611
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900612const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
Simon Glassc4fc5622017-05-18 20:08:58 -0600613{
Masahiro Yamada5052f1b2017-06-22 16:54:04 +0900614 if (ofnode_is_np(node))
615 return of_get_property(ofnode_to_np(node), propname, lenp);
616 else
Simon Glassc4fc5622017-05-18 20:08:58 -0600617 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
618 propname, lenp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600619}
620
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100621int ofnode_get_first_property(ofnode node, struct ofprop *prop)
622{
623 prop->node = node;
624
625 if (ofnode_is_np(node)) {
626 prop->prop = of_get_first_property(ofnode_to_np(prop->node));
627 if (!prop->prop)
628 return -FDT_ERR_NOTFOUND;
629 } else {
630 prop->offset =
631 fdt_first_property_offset(gd->fdt_blob,
632 ofnode_to_offset(prop->node));
633 if (prop->offset < 0)
634 return prop->offset;
635 }
636
637 return 0;
638}
639
640int ofnode_get_next_property(struct ofprop *prop)
641{
642 if (ofnode_is_np(prop->node)) {
643 prop->prop = of_get_next_property(ofnode_to_np(prop->node),
644 prop->prop);
645 if (!prop->prop)
646 return -FDT_ERR_NOTFOUND;
647 } else {
648 prop->offset = fdt_next_property_offset(gd->fdt_blob,
649 prop->offset);
650 if (prop->offset < 0)
651 return prop->offset;
652 }
653
654 return 0;
655}
656
657const void *ofnode_get_property_by_prop(const struct ofprop *prop,
658 const char **propname, int *lenp)
659{
660 if (ofnode_is_np(prop->node))
661 return of_get_property_by_prop(ofnode_to_np(prop->node),
662 prop->prop, propname, lenp);
663 else
664 return fdt_getprop_by_offset(gd->fdt_blob,
665 prop->offset,
666 propname, lenp);
667}
668
Simon Glassc4fc5622017-05-18 20:08:58 -0600669bool ofnode_is_available(ofnode node)
670{
671 if (ofnode_is_np(node))
672 return of_device_is_available(ofnode_to_np(node));
673 else
674 return fdtdec_get_is_enabled(gd->fdt_blob,
675 ofnode_to_offset(node));
676}
677
678fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
679 fdt_size_t *sizep)
680{
681 if (ofnode_is_np(node)) {
682 int na, ns;
683 int psize;
684 const struct device_node *np = ofnode_to_np(node);
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200685 const __be32 *prop = of_get_property(np, property, &psize);
Simon Glassc4fc5622017-05-18 20:08:58 -0600686
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200687 if (!prop)
688 return FDT_ADDR_T_NONE;
Simon Glassc4fc5622017-05-18 20:08:58 -0600689 na = of_n_addr_cells(np);
Marek Vasut1638c172018-10-01 12:37:19 +0200690 ns = of_n_size_cells(np);
Simon Glassa67cc632017-05-18 20:09:27 -0600691 *sizep = of_read_number(prop + na, ns);
Marek Vasuta9dac492018-10-01 12:37:20 +0200692
Simon Glass019f9562019-04-25 21:58:36 -0600693 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
Marek Vasuta9dac492018-10-01 12:37:20 +0200694 return of_translate_address(np, prop);
695 else
696 return of_read_number(prop, na);
Simon Glassc4fc5622017-05-18 20:08:58 -0600697 } else {
698 return fdtdec_get_addr_size(gd->fdt_blob,
699 ofnode_to_offset(node), property,
700 sizep);
701 }
702}
703
704const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
705 size_t sz)
706{
707 if (ofnode_is_np(node)) {
708 const struct device_node *np = ofnode_to_np(node);
709 int psize;
710 const __be32 *prop = of_get_property(np, propname, &psize);
711
712 if (!prop || sz != psize)
713 return NULL;
714 return (uint8_t *)prop;
715
716 } else {
717 return fdtdec_locate_byte_array(gd->fdt_blob,
718 ofnode_to_offset(node), propname, sz);
719 }
720}
721
722int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
723 const char *propname, struct fdt_pci_addr *addr)
724{
Masahiro Yamada5c5991e2017-06-22 17:57:50 +0900725 const fdt32_t *cell;
Simon Glassc4fc5622017-05-18 20:08:58 -0600726 int len;
727 int ret = -ENOENT;
728
729 debug("%s: %s: ", __func__, propname);
730
731 /*
732 * If we follow the pci bus bindings strictly, we should check
733 * the value of the node's parent node's #address-cells and
734 * #size-cells. They need to be 3 and 2 accordingly. However,
735 * for simplicity we skip the check here.
736 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900737 cell = ofnode_get_property(node, propname, &len);
Simon Glassc4fc5622017-05-18 20:08:58 -0600738 if (!cell)
739 goto fail;
740
741 if ((len % FDT_PCI_REG_SIZE) == 0) {
742 int num = len / FDT_PCI_REG_SIZE;
743 int i;
744
745 for (i = 0; i < num; i++) {
746 debug("pci address #%d: %08lx %08lx %08lx\n", i,
747 (ulong)fdt32_to_cpu(cell[0]),
748 (ulong)fdt32_to_cpu(cell[1]),
749 (ulong)fdt32_to_cpu(cell[2]));
750 if ((fdt32_to_cpu(*cell) & type) == type) {
751 addr->phys_hi = fdt32_to_cpu(cell[0]);
752 addr->phys_mid = fdt32_to_cpu(cell[1]);
Simon Glassdfd43152019-09-25 08:55:46 -0600753 addr->phys_lo = fdt32_to_cpu(cell[2]);
Simon Glassc4fc5622017-05-18 20:08:58 -0600754 break;
Simon Glassc4fc5622017-05-18 20:08:58 -0600755 }
Mario Sixf40d82c2018-01-15 11:07:17 +0100756
757 cell += (FDT_PCI_ADDR_CELLS +
758 FDT_PCI_SIZE_CELLS);
Simon Glassc4fc5622017-05-18 20:08:58 -0600759 }
760
761 if (i == num) {
762 ret = -ENXIO;
763 goto fail;
764 }
765
766 return 0;
Simon Glassc4fc5622017-05-18 20:08:58 -0600767 }
768
Mario Sixf40d82c2018-01-15 11:07:17 +0100769 ret = -EINVAL;
770
Simon Glassc4fc5622017-05-18 20:08:58 -0600771fail:
772 debug("(not found)\n");
773 return ret;
774}
775
Bin Mengfa157712018-08-03 01:14:35 -0700776int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
777{
778 const char *list, *end;
779 int len;
780
781 list = ofnode_get_property(node, "compatible", &len);
782 if (!list)
783 return -ENOENT;
784
785 end = list + len;
786 while (list < end) {
787 len = strlen(list);
788 if (len >= strlen("pciVVVV,DDDD")) {
789 char *s = strstr(list, "pci");
790
791 /*
792 * check if the string is something like pciVVVV,DDDD.RR
793 * or just pciVVVV,DDDD
794 */
795 if (s && s[7] == ',' &&
796 (s[12] == '.' || s[12] == 0)) {
797 s += 3;
798 *vendor = simple_strtol(s, NULL, 16);
799
800 s += 5;
801 *device = simple_strtol(s, NULL, 16);
802
803 return 0;
804 }
805 }
806 list += (len + 1);
807 }
808
809 return -ENOENT;
810}
811
Simon Glassc4fc5622017-05-18 20:08:58 -0600812int ofnode_read_addr_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_addr_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_address_cells(gd->fdt_blob, parent);
821 }
Simon Glassc4fc5622017-05-18 20:08:58 -0600822}
823
824int ofnode_read_size_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_size_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_size_cells(gd->fdt_blob, parent);
833 }
Simon Glass4191dc12017-06-12 06:21:31 -0600834}
835
836int ofnode_read_simple_addr_cells(ofnode node)
837{
838 if (ofnode_is_np(node))
839 return of_simple_addr_cells(ofnode_to_np(node));
840 else
841 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
842}
843
844int ofnode_read_simple_size_cells(ofnode node)
845{
846 if (ofnode_is_np(node))
847 return of_simple_size_cells(ofnode_to_np(node));
Simon Glassc4fc5622017-05-18 20:08:58 -0600848 else
849 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
850}
851
852bool ofnode_pre_reloc(ofnode node)
853{
Patrick Delaunay0b025b82019-02-11 12:49:57 +0100854#if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
855 /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
856 * had property dm-pre-reloc or u-boot,dm-spl/tpl.
857 * They are removed in final dtb (fdtgrep 2nd pass)
858 */
859 return true;
860#else
Masahiro Yamada6a61dd92017-06-22 16:54:03 +0900861 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
Simon Glassc4fc5622017-05-18 20:08:58 -0600862 return true;
Simon Glass23f22842018-10-01 12:22:18 -0600863 if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
864 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -0600865
Simon Glassc4fc5622017-05-18 20:08:58 -0600866 /*
867 * In regular builds individual spl and tpl handling both
868 * count as handled pre-relocation for later second init.
869 */
Masahiro Yamada6a61dd92017-06-22 16:54:03 +0900870 if (ofnode_read_bool(node, "u-boot,dm-spl") ||
871 ofnode_read_bool(node, "u-boot,dm-tpl"))
Simon Glassc4fc5622017-05-18 20:08:58 -0600872 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -0600873
874 return false;
Patrick Delaunay0b025b82019-02-11 12:49:57 +0100875#endif
Simon Glassc4fc5622017-05-18 20:08:58 -0600876}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600877
878int ofnode_read_resource(ofnode node, uint index, struct resource *res)
879{
880 if (ofnode_is_np(node)) {
881 return of_address_to_resource(ofnode_to_np(node), index, res);
882 } else {
883 struct fdt_resource fres;
884 int ret;
885
886 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
887 "reg", index, &fres);
888 if (ret < 0)
889 return -EINVAL;
890 memset(res, '\0', sizeof(*res));
891 res->start = fres.start;
892 res->end = fres.end;
893
894 return 0;
895 }
896}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900897
898int ofnode_read_resource_byname(ofnode node, const char *name,
899 struct resource *res)
900{
901 int index;
902
903 index = ofnode_stringlist_search(node, "reg-names", name);
904 if (index < 0)
905 return index;
906
907 return ofnode_read_resource(node, index, res);
908}
Mario Sixaefac062018-01-15 11:07:19 +0100909
910u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
911{
912 if (ofnode_is_np(node))
913 return of_translate_address(ofnode_to_np(node), in_addr);
914 else
915 return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
916}
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900917
Fabien Dessenne22236e02019-05-31 15:11:30 +0200918u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
919{
920 if (ofnode_is_np(node))
921 return of_translate_dma_address(ofnode_to_np(node), in_addr);
922 else
923 return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
924}
925
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900926int ofnode_device_is_compatible(ofnode node, const char *compat)
927{
928 if (ofnode_is_np(node))
929 return of_device_is_compatible(ofnode_to_np(node), compat,
930 NULL, NULL);
931 else
932 return !fdt_node_check_compatible(gd->fdt_blob,
933 ofnode_to_offset(node),
934 compat);
935}
Simon Glass954eeae2018-06-11 13:07:13 -0600936
937ofnode ofnode_by_compatible(ofnode from, const char *compat)
938{
939 if (of_live_active()) {
940 return np_to_ofnode(of_find_compatible_node(
941 (struct device_node *)ofnode_to_np(from), NULL,
942 compat));
943 } else {
944 return offset_to_ofnode(fdt_node_offset_by_compatible(
945 gd->fdt_blob, ofnode_to_offset(from), compat));
946 }
947}
Jens Wiklander7b68dad2018-08-20 11:09:58 +0200948
949ofnode ofnode_by_prop_value(ofnode from, const char *propname,
950 const void *propval, int proplen)
951{
952 if (of_live_active()) {
953 return np_to_ofnode(of_find_node_by_prop_value(
954 (struct device_node *)ofnode_to_np(from), propname,
955 propval, proplen));
956 } else {
957 return offset_to_ofnode(fdt_node_offset_by_prop_value(
958 gd->fdt_blob, ofnode_to_offset(from),
959 propname, propval, proplen));
960 }
961}
Mario Six047dafc2018-06-26 08:46:48 +0200962
963int ofnode_write_prop(ofnode node, const char *propname, int len,
964 const void *value)
965{
966 const struct device_node *np = ofnode_to_np(node);
967 struct property *pp;
968 struct property *pp_last = NULL;
969 struct property *new;
970
971 if (!of_live_active())
972 return -ENOSYS;
973
974 if (!np)
975 return -EINVAL;
976
977 for (pp = np->properties; pp; pp = pp->next) {
978 if (strcmp(pp->name, propname) == 0) {
979 /* Property exists -> change value */
980 pp->value = (void *)value;
981 pp->length = len;
982 return 0;
983 }
984 pp_last = pp;
985 }
986
987 if (!pp_last)
988 return -ENOENT;
989
990 /* Property does not exist -> append new property */
991 new = malloc(sizeof(struct property));
992 if (!new)
993 return -ENOMEM;
994
995 new->name = strdup(propname);
Mario Six95e885d2018-10-04 09:22:24 +0200996 if (!new->name) {
997 free(new);
Mario Six047dafc2018-06-26 08:46:48 +0200998 return -ENOMEM;
Mario Six95e885d2018-10-04 09:22:24 +0200999 }
Mario Six047dafc2018-06-26 08:46:48 +02001000
1001 new->value = (void *)value;
1002 new->length = len;
1003 new->next = NULL;
1004
1005 pp_last->next = new;
1006
1007 return 0;
1008}
1009
1010int ofnode_write_string(ofnode node, const char *propname, const char *value)
1011{
1012 if (!of_live_active())
1013 return -ENOSYS;
1014
1015 assert(ofnode_valid(node));
1016
1017 debug("%s: %s = %s", __func__, propname, value);
1018
1019 return ofnode_write_prop(node, propname, strlen(value) + 1, value);
1020}
1021
1022int ofnode_set_enabled(ofnode node, bool value)
1023{
1024 if (!of_live_active())
1025 return -ENOSYS;
1026
1027 assert(ofnode_valid(node));
1028
1029 if (value)
1030 return ofnode_write_string(node, "status", "okay");
1031 else
Bin Menga9274aa2019-07-05 09:23:17 -07001032 return ofnode_write_string(node, "status", "disabled");
Mario Six047dafc2018-06-26 08:46:48 +02001033}