blob: d50533338e78a560988f1480f409477849b552bc [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 Binacchib574d682020-12-30 00:16:21 +0100322 if (IS_ENABLED(CONFIG_OF_TRANSLATE) &&
323 (ns > 0 || gd_size_cells_0())) {
Mario Sixd007ebc2017-12-20 09:52:12 +0100324 return of_translate_address(ofnode_to_np(node), prop_val);
325 } else {
326 na = of_n_addr_cells(ofnode_to_np(node));
327 return of_read_number(prop_val, na);
328 }
Simon Glass049ae1b2017-05-18 20:09:01 -0600329 } else {
Keerthy34222a32018-11-19 11:44:47 +0530330 na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
331 ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
332 return fdtdec_get_addr_size_fixed(gd->fdt_blob,
333 ofnode_to_offset(node), "reg",
Keerthyd332e6e2019-04-24 17:19:53 +0530334 index, na, ns, size, true);
Simon Glass049ae1b2017-05-18 20:09:01 -0600335 }
336
337 return FDT_ADDR_T_NONE;
338}
339
Keerthyd332e6e2019-04-24 17:19:53 +0530340fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
341{
342 fdt_size_t size;
343
344 return ofnode_get_addr_size_index(node, index, &size);
345}
346
Simon Glass049ae1b2017-05-18 20:09:01 -0600347fdt_addr_t ofnode_get_addr(ofnode node)
348{
349 return ofnode_get_addr_index(node, 0);
350}
351
Chen Guanqiao223f17d2021-04-12 14:51:11 +0800352fdt_size_t ofnode_get_size(ofnode node)
353{
354 fdt_size_t size;
355
356 ofnode_get_addr_size_index(node, 0, &size);
357
358 return size;
359}
360
Simon Glassc4fc5622017-05-18 20:08:58 -0600361int ofnode_stringlist_search(ofnode node, const char *property,
362 const char *string)
363{
364 if (ofnode_is_np(node)) {
365 return of_property_match_string(ofnode_to_np(node),
366 property, string);
367 } else {
368 int ret;
369
370 ret = fdt_stringlist_search(gd->fdt_blob,
371 ofnode_to_offset(node), property,
372 string);
373 if (ret == -FDT_ERR_NOTFOUND)
374 return -ENODATA;
375 else if (ret < 0)
376 return -EINVAL;
377
378 return ret;
379 }
380}
381
382int ofnode_read_string_index(ofnode node, const char *property, int index,
383 const char **outp)
384{
385 if (ofnode_is_np(node)) {
386 return of_property_read_string_index(ofnode_to_np(node),
387 property, index, outp);
388 } else {
389 int len;
390
391 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
392 property, index, &len);
393 if (len < 0)
394 return -EINVAL;
395 return 0;
396 }
397}
398
Simon Glass5fdb0052017-06-12 06:21:28 -0600399int ofnode_read_string_count(ofnode node, const char *property)
400{
401 if (ofnode_is_np(node)) {
402 return of_property_count_strings(ofnode_to_np(node), property);
403 } else {
404 return fdt_stringlist_count(gd->fdt_blob,
405 ofnode_to_offset(node), property);
406 }
407}
408
Simon Glassc4fc5622017-05-18 20:08:58 -0600409static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
410 struct ofnode_phandle_args *out)
411{
412 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
413 out->node = offset_to_ofnode(in->node);
414 out->args_count = in->args_count;
415 memcpy(out->args, in->args, sizeof(out->args));
416}
417
418static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
419 struct ofnode_phandle_args *out)
420{
421 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
422 out->node = np_to_ofnode(in->np);
423 out->args_count = in->args_count;
424 memcpy(out->args, in->args, sizeof(out->args));
425}
426
427int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
428 const char *cells_name, int cell_count,
429 int index,
430 struct ofnode_phandle_args *out_args)
431{
432 if (ofnode_is_np(node)) {
433 struct of_phandle_args args;
434 int ret;
435
436 ret = of_parse_phandle_with_args(ofnode_to_np(node),
Patrick Delaunay21e3b042020-09-10 18:26:17 +0200437 list_name, cells_name,
438 cell_count, index,
Mario Sixf40d82c2018-01-15 11:07:17 +0100439 &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600440 if (ret)
441 return ret;
442 ofnode_from_of_phandle_args(&args, out_args);
443 } else {
444 struct fdtdec_phandle_args args;
445 int ret;
446
447 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
Mario Sixf40d82c2018-01-15 11:07:17 +0100448 ofnode_to_offset(node),
449 list_name, cells_name,
450 cell_count, index, &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600451 if (ret)
452 return ret;
453 ofnode_from_fdtdec_phandle_args(&args, out_args);
454 }
455
456 return 0;
457}
458
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200459int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200460 const char *cells_name, int cell_count)
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200461{
462 if (ofnode_is_np(node))
463 return of_count_phandle_with_args(ofnode_to_np(node),
Patrick Delaunayd776a842020-09-25 09:41:14 +0200464 list_name, cells_name, cell_count);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200465 else
466 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
467 ofnode_to_offset(node), list_name, cells_name,
Patrick Delaunayd776a842020-09-25 09:41:14 +0200468 cell_count, -1, NULL);
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200469}
470
Simon Glassc4fc5622017-05-18 20:08:58 -0600471ofnode ofnode_path(const char *path)
472{
473 if (of_live_active())
474 return np_to_ofnode(of_find_node_by_path(path));
475 else
476 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
477}
478
Simon Glasse09223c2020-01-27 08:49:46 -0700479const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
Simon Glassc4fc5622017-05-18 20:08:58 -0600480{
481 ofnode chosen_node;
482
483 chosen_node = ofnode_path("/chosen");
484
Simon Glasse09223c2020-01-27 08:49:46 -0700485 return ofnode_read_prop(chosen_node, propname, sizep);
Simon Glassc4fc5622017-05-18 20:08:58 -0600486}
487
Simon Glasse09223c2020-01-27 08:49:46 -0700488const char *ofnode_read_chosen_string(const char *propname)
489{
490 return ofnode_read_chosen_prop(propname, NULL);
491}
492
Simon Glassc4fc5622017-05-18 20:08:58 -0600493ofnode ofnode_get_chosen_node(const char *name)
494{
495 const char *prop;
496
Simon Glasse09223c2020-01-27 08:49:46 -0700497 prop = ofnode_read_chosen_prop(name, NULL);
Simon Glassc4fc5622017-05-18 20:08:58 -0600498 if (!prop)
499 return ofnode_null();
500
501 return ofnode_path(prop);
502}
503
Michal Simek92a88622020-07-28 12:51:08 +0200504const void *ofnode_read_aliases_prop(const char *propname, int *sizep)
505{
506 ofnode node;
507
508 node = ofnode_path("/aliases");
509
510 return ofnode_read_prop(node, propname, sizep);
511}
512
513ofnode ofnode_get_aliases_node(const char *name)
514{
515 const char *prop;
516
517 prop = ofnode_read_aliases_prop(name, NULL);
518 if (!prop)
519 return ofnode_null();
520
521 debug("%s: node_path: %s\n", __func__, prop);
522
523 return ofnode_path(prop);
524}
525
developerd93c8b42020-05-02 11:35:09 +0200526int ofnode_get_child_count(ofnode parent)
527{
528 ofnode child;
529 int num = 0;
530
531 ofnode_for_each_subnode(child, parent)
532 num++;
533
534 return num;
535}
536
Simon Glassc4fc5622017-05-18 20:08:58 -0600537static int decode_timing_property(ofnode node, const char *name,
538 struct timing_entry *result)
539{
540 int length, ret = 0;
541
542 length = ofnode_read_size(node, name);
543 if (length < 0) {
544 debug("%s: could not find property %s\n",
545 ofnode_get_name(node), name);
546 return length;
547 }
548
549 if (length == sizeof(u32)) {
550 result->typ = ofnode_read_u32_default(node, name, 0);
551 result->min = result->typ;
552 result->max = result->typ;
553 } else {
554 ret = ofnode_read_u32_array(node, name, &result->min, 3);
555 }
556
557 return ret;
558}
559
560int ofnode_decode_display_timing(ofnode parent, int index,
561 struct display_timing *dt)
562{
563 int i;
564 ofnode timings, node;
565 u32 val = 0;
566 int ret = 0;
567
568 timings = ofnode_find_subnode(parent, "display-timings");
569 if (!ofnode_valid(timings))
570 return -EINVAL;
571
Simon Glass28529762017-08-05 15:45:54 -0600572 i = 0;
573 ofnode_for_each_subnode(node, timings) {
574 if (i++ == index)
575 break;
576 }
Simon Glassc4fc5622017-05-18 20:08:58 -0600577
578 if (!ofnode_valid(node))
579 return -EINVAL;
580
581 memset(dt, 0, sizeof(*dt));
582
583 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
584 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
585 ret |= decode_timing_property(node, "hactive", &dt->hactive);
586 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
587 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
588 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
589 ret |= decode_timing_property(node, "vactive", &dt->vactive);
590 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
591 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
592
593 dt->flags = 0;
594 val = ofnode_read_u32_default(node, "vsync-active", -1);
595 if (val != -1) {
596 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
597 DISPLAY_FLAGS_VSYNC_LOW;
598 }
599 val = ofnode_read_u32_default(node, "hsync-active", -1);
600 if (val != -1) {
601 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
602 DISPLAY_FLAGS_HSYNC_LOW;
603 }
604 val = ofnode_read_u32_default(node, "de-active", -1);
605 if (val != -1) {
606 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
607 DISPLAY_FLAGS_DE_LOW;
608 }
609 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
610 if (val != -1) {
611 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
612 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
613 }
614
615 if (ofnode_read_bool(node, "interlaced"))
616 dt->flags |= DISPLAY_FLAGS_INTERLACED;
617 if (ofnode_read_bool(node, "doublescan"))
618 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
619 if (ofnode_read_bool(node, "doubleclk"))
620 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
621
622 return ret;
623}
624
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900625const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
Simon Glassc4fc5622017-05-18 20:08:58 -0600626{
Masahiro Yamada5052f1b2017-06-22 16:54:04 +0900627 if (ofnode_is_np(node))
628 return of_get_property(ofnode_to_np(node), propname, lenp);
629 else
Simon Glassc4fc5622017-05-18 20:08:58 -0600630 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
631 propname, lenp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600632}
633
Patrick Delaunaycaee1552020-01-13 11:34:56 +0100634int ofnode_get_first_property(ofnode node, struct ofprop *prop)
635{
636 prop->node = node;
637
638 if (ofnode_is_np(node)) {
639 prop->prop = of_get_first_property(ofnode_to_np(prop->node));
640 if (!prop->prop)
641 return -FDT_ERR_NOTFOUND;
642 } else {
643 prop->offset =
644 fdt_first_property_offset(gd->fdt_blob,
645 ofnode_to_offset(prop->node));
646 if (prop->offset < 0)
647 return prop->offset;
648 }
649
650 return 0;
651}
652
653int ofnode_get_next_property(struct ofprop *prop)
654{
655 if (ofnode_is_np(prop->node)) {
656 prop->prop = of_get_next_property(ofnode_to_np(prop->node),
657 prop->prop);
658 if (!prop->prop)
659 return -FDT_ERR_NOTFOUND;
660 } else {
661 prop->offset = fdt_next_property_offset(gd->fdt_blob,
662 prop->offset);
663 if (prop->offset < 0)
664 return prop->offset;
665 }
666
667 return 0;
668}
669
670const void *ofnode_get_property_by_prop(const struct ofprop *prop,
671 const char **propname, int *lenp)
672{
673 if (ofnode_is_np(prop->node))
674 return of_get_property_by_prop(ofnode_to_np(prop->node),
675 prop->prop, propname, lenp);
676 else
677 return fdt_getprop_by_offset(gd->fdt_blob,
678 prop->offset,
679 propname, lenp);
680}
681
Simon Glassc4fc5622017-05-18 20:08:58 -0600682bool ofnode_is_available(ofnode node)
683{
684 if (ofnode_is_np(node))
685 return of_device_is_available(ofnode_to_np(node));
686 else
687 return fdtdec_get_is_enabled(gd->fdt_blob,
688 ofnode_to_offset(node));
689}
690
691fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
692 fdt_size_t *sizep)
693{
694 if (ofnode_is_np(node)) {
695 int na, ns;
696 int psize;
697 const struct device_node *np = ofnode_to_np(node);
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200698 const __be32 *prop = of_get_property(np, property, &psize);
Simon Glassc4fc5622017-05-18 20:08:58 -0600699
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200700 if (!prop)
701 return FDT_ADDR_T_NONE;
Simon Glassc4fc5622017-05-18 20:08:58 -0600702 na = of_n_addr_cells(np);
Marek Vasut1638c172018-10-01 12:37:19 +0200703 ns = of_n_size_cells(np);
Simon Glassa67cc632017-05-18 20:09:27 -0600704 *sizep = of_read_number(prop + na, ns);
Marek Vasuta9dac492018-10-01 12:37:20 +0200705
Dario Binacchib574d682020-12-30 00:16:21 +0100706 if (CONFIG_IS_ENABLED(OF_TRANSLATE) &&
707 (ns > 0 || gd_size_cells_0())) {
Marek Vasuta9dac492018-10-01 12:37:20 +0200708 return of_translate_address(np, prop);
Dario Binacchib574d682020-12-30 00:16:21 +0100709 }
Marek Vasuta9dac492018-10-01 12:37:20 +0200710 else
711 return of_read_number(prop, na);
Simon Glassc4fc5622017-05-18 20:08:58 -0600712 } else {
713 return fdtdec_get_addr_size(gd->fdt_blob,
714 ofnode_to_offset(node), property,
715 sizep);
716 }
717}
718
719const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
720 size_t sz)
721{
722 if (ofnode_is_np(node)) {
723 const struct device_node *np = ofnode_to_np(node);
724 int psize;
725 const __be32 *prop = of_get_property(np, propname, &psize);
726
727 if (!prop || sz != psize)
728 return NULL;
729 return (uint8_t *)prop;
730
731 } else {
732 return fdtdec_locate_byte_array(gd->fdt_blob,
733 ofnode_to_offset(node), propname, sz);
734 }
735}
736
737int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
738 const char *propname, struct fdt_pci_addr *addr)
739{
Masahiro Yamada5c5991e2017-06-22 17:57:50 +0900740 const fdt32_t *cell;
Simon Glassc4fc5622017-05-18 20:08:58 -0600741 int len;
742 int ret = -ENOENT;
743
744 debug("%s: %s: ", __func__, propname);
745
746 /*
747 * If we follow the pci bus bindings strictly, we should check
748 * the value of the node's parent node's #address-cells and
749 * #size-cells. They need to be 3 and 2 accordingly. However,
750 * for simplicity we skip the check here.
751 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900752 cell = ofnode_get_property(node, propname, &len);
Simon Glassc4fc5622017-05-18 20:08:58 -0600753 if (!cell)
754 goto fail;
755
756 if ((len % FDT_PCI_REG_SIZE) == 0) {
757 int num = len / FDT_PCI_REG_SIZE;
758 int i;
759
760 for (i = 0; i < num; i++) {
761 debug("pci address #%d: %08lx %08lx %08lx\n", i,
762 (ulong)fdt32_to_cpu(cell[0]),
763 (ulong)fdt32_to_cpu(cell[1]),
764 (ulong)fdt32_to_cpu(cell[2]));
765 if ((fdt32_to_cpu(*cell) & type) == type) {
766 addr->phys_hi = fdt32_to_cpu(cell[0]);
767 addr->phys_mid = fdt32_to_cpu(cell[1]);
Simon Glassdfd43152019-09-25 08:55:46 -0600768 addr->phys_lo = fdt32_to_cpu(cell[2]);
Simon Glassc4fc5622017-05-18 20:08:58 -0600769 break;
Simon Glassc4fc5622017-05-18 20:08:58 -0600770 }
Mario Sixf40d82c2018-01-15 11:07:17 +0100771
772 cell += (FDT_PCI_ADDR_CELLS +
773 FDT_PCI_SIZE_CELLS);
Simon Glassc4fc5622017-05-18 20:08:58 -0600774 }
775
776 if (i == num) {
777 ret = -ENXIO;
778 goto fail;
779 }
780
781 return 0;
Simon Glassc4fc5622017-05-18 20:08:58 -0600782 }
783
Mario Sixf40d82c2018-01-15 11:07:17 +0100784 ret = -EINVAL;
785
Simon Glassc4fc5622017-05-18 20:08:58 -0600786fail:
787 debug("(not found)\n");
788 return ret;
789}
790
Bin Mengfa157712018-08-03 01:14:35 -0700791int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
792{
793 const char *list, *end;
794 int len;
795
796 list = ofnode_get_property(node, "compatible", &len);
797 if (!list)
798 return -ENOENT;
799
800 end = list + len;
801 while (list < end) {
802 len = strlen(list);
803 if (len >= strlen("pciVVVV,DDDD")) {
804 char *s = strstr(list, "pci");
805
806 /*
807 * check if the string is something like pciVVVV,DDDD.RR
808 * or just pciVVVV,DDDD
809 */
810 if (s && s[7] == ',' &&
811 (s[12] == '.' || s[12] == 0)) {
812 s += 3;
813 *vendor = simple_strtol(s, NULL, 16);
814
815 s += 5;
816 *device = simple_strtol(s, NULL, 16);
817
818 return 0;
819 }
820 }
821 list += (len + 1);
822 }
823
824 return -ENOENT;
825}
826
Simon Glassc4fc5622017-05-18 20:08:58 -0600827int ofnode_read_addr_cells(ofnode node)
828{
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200829 if (ofnode_is_np(node)) {
Simon Glassc4fc5622017-05-18 20:08:58 -0600830 return of_n_addr_cells(ofnode_to_np(node));
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200831 } else {
832 int parent = fdt_parent_offset(gd->fdt_blob,
833 ofnode_to_offset(node));
834
835 return fdt_address_cells(gd->fdt_blob, parent);
836 }
Simon Glassc4fc5622017-05-18 20:08:58 -0600837}
838
839int ofnode_read_size_cells(ofnode node)
840{
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200841 if (ofnode_is_np(node)) {
Simon Glassc4fc5622017-05-18 20:08:58 -0600842 return of_n_size_cells(ofnode_to_np(node));
Heinrich Schuchardt60a84af2020-07-25 21:38:49 +0200843 } else {
844 int parent = fdt_parent_offset(gd->fdt_blob,
845 ofnode_to_offset(node));
846
847 return fdt_size_cells(gd->fdt_blob, parent);
848 }
Simon Glass4191dc12017-06-12 06:21:31 -0600849}
850
851int ofnode_read_simple_addr_cells(ofnode node)
852{
853 if (ofnode_is_np(node))
854 return of_simple_addr_cells(ofnode_to_np(node));
855 else
856 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
857}
858
859int ofnode_read_simple_size_cells(ofnode node)
860{
861 if (ofnode_is_np(node))
862 return of_simple_size_cells(ofnode_to_np(node));
Simon Glassc4fc5622017-05-18 20:08:58 -0600863 else
864 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
865}
866
867bool ofnode_pre_reloc(ofnode node)
868{
Patrick Delaunay0b025b82019-02-11 12:49:57 +0100869#if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
870 /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
871 * had property dm-pre-reloc or u-boot,dm-spl/tpl.
872 * They are removed in final dtb (fdtgrep 2nd pass)
873 */
874 return true;
875#else
Masahiro Yamada6a61dd92017-06-22 16:54:03 +0900876 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
Simon Glassc4fc5622017-05-18 20:08:58 -0600877 return true;
Simon Glass23f22842018-10-01 12:22:18 -0600878 if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
879 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -0600880
Simon Glassc4fc5622017-05-18 20:08:58 -0600881 /*
882 * In regular builds individual spl and tpl handling both
883 * count as handled pre-relocation for later second init.
884 */
Masahiro Yamada6a61dd92017-06-22 16:54:03 +0900885 if (ofnode_read_bool(node, "u-boot,dm-spl") ||
886 ofnode_read_bool(node, "u-boot,dm-tpl"))
Simon Glassc4fc5622017-05-18 20:08:58 -0600887 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -0600888
889 return false;
Patrick Delaunay0b025b82019-02-11 12:49:57 +0100890#endif
Simon Glassc4fc5622017-05-18 20:08:58 -0600891}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600892
893int ofnode_read_resource(ofnode node, uint index, struct resource *res)
894{
895 if (ofnode_is_np(node)) {
896 return of_address_to_resource(ofnode_to_np(node), index, res);
897 } else {
898 struct fdt_resource fres;
899 int ret;
900
901 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
902 "reg", index, &fres);
903 if (ret < 0)
904 return -EINVAL;
905 memset(res, '\0', sizeof(*res));
906 res->start = fres.start;
907 res->end = fres.end;
908
909 return 0;
910 }
911}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900912
913int ofnode_read_resource_byname(ofnode node, const char *name,
914 struct resource *res)
915{
916 int index;
917
918 index = ofnode_stringlist_search(node, "reg-names", name);
919 if (index < 0)
920 return index;
921
922 return ofnode_read_resource(node, index, res);
923}
Mario Sixaefac062018-01-15 11:07:19 +0100924
925u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
926{
927 if (ofnode_is_np(node))
928 return of_translate_address(ofnode_to_np(node), in_addr);
929 else
930 return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
931}
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900932
Fabien Dessenne22236e02019-05-31 15:11:30 +0200933u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
934{
935 if (ofnode_is_np(node))
936 return of_translate_dma_address(ofnode_to_np(node), in_addr);
937 else
938 return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
939}
940
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +0100941int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus, u64 *size)
942{
943 if (ofnode_is_np(node))
944 return of_get_dma_range(ofnode_to_np(node), cpu, bus, size);
945 else
946 return fdt_get_dma_range(gd->fdt_blob, ofnode_to_offset(node),
947 cpu, bus, size);
948}
949
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900950int ofnode_device_is_compatible(ofnode node, const char *compat)
951{
952 if (ofnode_is_np(node))
953 return of_device_is_compatible(ofnode_to_np(node), compat,
954 NULL, NULL);
955 else
956 return !fdt_node_check_compatible(gd->fdt_blob,
957 ofnode_to_offset(node),
958 compat);
959}
Simon Glass954eeae2018-06-11 13:07:13 -0600960
961ofnode ofnode_by_compatible(ofnode from, const char *compat)
962{
963 if (of_live_active()) {
964 return np_to_ofnode(of_find_compatible_node(
965 (struct device_node *)ofnode_to_np(from), NULL,
966 compat));
967 } else {
968 return offset_to_ofnode(fdt_node_offset_by_compatible(
969 gd->fdt_blob, ofnode_to_offset(from), compat));
970 }
971}
Jens Wiklander7b68dad2018-08-20 11:09:58 +0200972
973ofnode ofnode_by_prop_value(ofnode from, const char *propname,
974 const void *propval, int proplen)
975{
976 if (of_live_active()) {
977 return np_to_ofnode(of_find_node_by_prop_value(
978 (struct device_node *)ofnode_to_np(from), propname,
979 propval, proplen));
980 } else {
981 return offset_to_ofnode(fdt_node_offset_by_prop_value(
982 gd->fdt_blob, ofnode_to_offset(from),
983 propname, propval, proplen));
984 }
985}
Mario Six047dafc2018-06-26 08:46:48 +0200986
987int ofnode_write_prop(ofnode node, const char *propname, int len,
988 const void *value)
989{
990 const struct device_node *np = ofnode_to_np(node);
991 struct property *pp;
992 struct property *pp_last = NULL;
993 struct property *new;
994
995 if (!of_live_active())
996 return -ENOSYS;
997
998 if (!np)
999 return -EINVAL;
1000
1001 for (pp = np->properties; pp; pp = pp->next) {
1002 if (strcmp(pp->name, propname) == 0) {
1003 /* Property exists -> change value */
1004 pp->value = (void *)value;
1005 pp->length = len;
1006 return 0;
1007 }
1008 pp_last = pp;
1009 }
1010
1011 if (!pp_last)
1012 return -ENOENT;
1013
1014 /* Property does not exist -> append new property */
1015 new = malloc(sizeof(struct property));
1016 if (!new)
1017 return -ENOMEM;
1018
1019 new->name = strdup(propname);
Mario Six95e885d2018-10-04 09:22:24 +02001020 if (!new->name) {
1021 free(new);
Mario Six047dafc2018-06-26 08:46:48 +02001022 return -ENOMEM;
Mario Six95e885d2018-10-04 09:22:24 +02001023 }
Mario Six047dafc2018-06-26 08:46:48 +02001024
1025 new->value = (void *)value;
1026 new->length = len;
1027 new->next = NULL;
1028
1029 pp_last->next = new;
1030
1031 return 0;
1032}
1033
1034int ofnode_write_string(ofnode node, const char *propname, const char *value)
1035{
1036 if (!of_live_active())
1037 return -ENOSYS;
1038
1039 assert(ofnode_valid(node));
1040
1041 debug("%s: %s = %s", __func__, propname, value);
1042
1043 return ofnode_write_prop(node, propname, strlen(value) + 1, value);
1044}
1045
1046int ofnode_set_enabled(ofnode node, bool value)
1047{
1048 if (!of_live_active())
1049 return -ENOSYS;
1050
1051 assert(ofnode_valid(node));
1052
1053 if (value)
1054 return ofnode_write_string(node, "status", "okay");
1055 else
Bin Menga9274aa2019-07-05 09:23:17 -07001056 return ofnode_write_string(node, "status", "disabled");
Mario Six047dafc2018-06-26 08:46:48 +02001057}