blob: 96a5dd20bd155db3bcaecfa7f967135b254c7b3e [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 Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090012#include <linux/libfdt.h>
Simon Glassc4fc5622017-05-18 20:08:58 -060013#include <dm/of_access.h>
Simon Glass049ae1b2017-05-18 20:09:01 -060014#include <dm/of_addr.h>
Simon Glassc4fc5622017-05-18 20:08:58 -060015#include <dm/ofnode.h>
16#include <linux/err.h>
Simon Glassf7bfcc42017-07-25 08:29:55 -060017#include <linux/ioport.h>
Simon Glassc4fc5622017-05-18 20:08:58 -060018
19int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
20{
21 assert(ofnode_valid(node));
22 debug("%s: %s: ", __func__, propname);
23
24 if (ofnode_is_np(node)) {
25 return of_read_u32(ofnode_to_np(node), propname, outp);
26 } else {
Masahiro Yamada5c5991e2017-06-22 17:57:50 +090027 const fdt32_t *cell;
Simon Glassc4fc5622017-05-18 20:08:58 -060028 int len;
29
30 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
31 propname, &len);
32 if (!cell || len < sizeof(int)) {
33 debug("(not found)\n");
34 return -EINVAL;
35 }
36 *outp = fdt32_to_cpu(cell[0]);
37 }
38 debug("%#x (%d)\n", *outp, *outp);
39
40 return 0;
41}
42
Trent Piepho5b775412019-05-10 17:48:20 +000043u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
Simon Glassc4fc5622017-05-18 20:08:58 -060044{
45 assert(ofnode_valid(node));
46 ofnode_read_u32(node, propname, &def);
47
48 return def;
49}
50
51int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
52{
53 assert(ofnode_valid(node));
54 ofnode_read_u32(node, propname, (u32 *)&def);
55
56 return def;
57}
58
Simon Glass9d54a7a2018-06-11 13:07:10 -060059int ofnode_read_u64(ofnode node, const char *propname, u64 *outp)
60{
Jean-Jacques Hiblot487f9172019-10-22 10:05:22 +020061 const unaligned_fdt64_t *cell;
Simon Glass9d54a7a2018-06-11 13:07:10 -060062 int len;
63
64 assert(ofnode_valid(node));
65 debug("%s: %s: ", __func__, propname);
66
67 if (ofnode_is_np(node))
68 return of_read_u64(ofnode_to_np(node), propname, outp);
69
70 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
71 &len);
72 if (!cell || len < sizeof(*cell)) {
73 debug("(not found)\n");
74 return -EINVAL;
75 }
76 *outp = fdt64_to_cpu(cell[0]);
77 debug("%#llx (%lld)\n", (unsigned long long)*outp,
78 (unsigned long long)*outp);
79
80 return 0;
81}
82
T Karthik Reddy478860d2019-09-02 16:34:30 +020083u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def)
Simon Glass9d54a7a2018-06-11 13:07:10 -060084{
85 assert(ofnode_valid(node));
86 ofnode_read_u64(node, propname, &def);
87
88 return def;
89}
90
Simon Glassc4fc5622017-05-18 20:08:58 -060091bool ofnode_read_bool(ofnode node, const char *propname)
92{
Masahiro Yamada5d434452017-06-22 16:54:07 +090093 const void *prop;
Simon Glassc4fc5622017-05-18 20:08:58 -060094
95 assert(ofnode_valid(node));
96 debug("%s: %s: ", __func__, propname);
97
Masahiro Yamada5d434452017-06-22 16:54:07 +090098 prop = ofnode_get_property(node, propname, NULL);
99
100 debug("%s\n", prop ? "true" : "false");
Simon Glassc4fc5622017-05-18 20:08:58 -0600101
Masahiro Yamada5d434452017-06-22 16:54:07 +0900102 return prop ? true : false;
Simon Glassc4fc5622017-05-18 20:08:58 -0600103}
104
Simon Glass0c2e9802020-01-27 08:49:44 -0700105const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep)
Simon Glassc4fc5622017-05-18 20:08:58 -0600106{
Simon Glass0c2e9802020-01-27 08:49:44 -0700107 const char *val = NULL;
108 int len;
Simon Glassc4fc5622017-05-18 20:08:58 -0600109
110 assert(ofnode_valid(node));
111 debug("%s: %s: ", __func__, propname);
112
113 if (ofnode_is_np(node)) {
114 struct property *prop = of_find_property(
Simon Glass0c2e9802020-01-27 08:49:44 -0700115 ofnode_to_np(node), propname, &len);
Simon Glassc4fc5622017-05-18 20:08:58 -0600116
117 if (prop) {
Simon Glass0c2e9802020-01-27 08:49:44 -0700118 val = prop->value;
Simon Glassc4fc5622017-05-18 20:08:58 -0600119 len = prop->length;
120 }
121 } else {
Simon Glass0c2e9802020-01-27 08:49:44 -0700122 val = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
Simon Glassc4fc5622017-05-18 20:08:58 -0600123 propname, &len);
124 }
Simon Glass0c2e9802020-01-27 08:49:44 -0700125 if (!val) {
Simon Glassc4fc5622017-05-18 20:08:58 -0600126 debug("<not found>\n");
Simon Glass0c2e9802020-01-27 08:49:44 -0700127 if (sizep)
128 *sizep = -FDT_ERR_NOTFOUND;
Simon Glassc4fc5622017-05-18 20:08:58 -0600129 return NULL;
130 }
Simon Glass0c2e9802020-01-27 08:49:44 -0700131 if (sizep)
132 *sizep = len;
133
134 return val;
135}
136
137const char *ofnode_read_string(ofnode node, const char *propname)
138{
139 const char *str;
140 int len;
141
142 str = ofnode_read_prop(node, propname, &len);
143 if (!str)
144 return NULL;
145
Simon Glassc4fc5622017-05-18 20:08:58 -0600146 if (strnlen(str, len) >= len) {
147 debug("<invalid>\n");
148 return NULL;
149 }
150 debug("%s\n", str);
151
152 return str;
153}
154
Simon Glass81c54b32020-01-27 08:49:45 -0700155int ofnode_read_size(ofnode node, const char *propname)
156{
157 int len;
158
159 if (!ofnode_read_prop(node, propname, &len))
160 return -EINVAL;
161
162 return len;
163}
164
Simon Glassc4fc5622017-05-18 20:08:58 -0600165ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
166{
167 ofnode subnode;
168
169 assert(ofnode_valid(node));
170 debug("%s: %s: ", __func__, subnode_name);
171
172 if (ofnode_is_np(node)) {
173 const struct device_node *np = ofnode_to_np(node);
174
175 for (np = np->child; np; np = np->sibling) {
176 if (!strcmp(subnode_name, np->name))
177 break;
178 }
179 subnode = np_to_ofnode(np);
180 } else {
181 int ooffset = fdt_subnode_offset(gd->fdt_blob,
182 ofnode_to_offset(node), subnode_name);
183 subnode = offset_to_ofnode(ooffset);
184 }
185 debug("%s\n", ofnode_valid(subnode) ?
186 ofnode_get_name(subnode) : "<none>");
187
188 return subnode;
189}
190
191int ofnode_read_u32_array(ofnode node, const char *propname,
192 u32 *out_values, size_t sz)
193{
194 assert(ofnode_valid(node));
195 debug("%s: %s: ", __func__, propname);
196
197 if (ofnode_is_np(node)) {
198 return of_read_u32_array(ofnode_to_np(node), propname,
199 out_values, sz);
200 } else {
201 return fdtdec_get_int_array(gd->fdt_blob,
202 ofnode_to_offset(node), propname,
203 out_values, sz);
204 }
205}
206
207ofnode ofnode_first_subnode(ofnode node)
208{
209 assert(ofnode_valid(node));
210 if (ofnode_is_np(node))
211 return np_to_ofnode(node.np->child);
212
213 return offset_to_ofnode(
214 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
215}
216
217ofnode ofnode_next_subnode(ofnode node)
218{
219 assert(ofnode_valid(node));
220 if (ofnode_is_np(node))
221 return np_to_ofnode(node.np->sibling);
222
223 return offset_to_ofnode(
224 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
225}
226
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100227ofnode ofnode_get_parent(ofnode node)
228{
229 ofnode parent;
230
231 assert(ofnode_valid(node));
232 if (ofnode_is_np(node))
233 parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
234 else
235 parent.of_offset = fdt_parent_offset(gd->fdt_blob,
236 ofnode_to_offset(node));
237
238 return parent;
239}
240
Simon Glassc4fc5622017-05-18 20:08:58 -0600241const char *ofnode_get_name(ofnode node)
242{
Kever Yang8d7976d2019-07-19 11:23:47 +0800243 if (!ofnode_valid(node)) {
244 debug("%s node not valid\n", __func__);
245 return NULL;
246 }
247
Simon Glassc4fc5622017-05-18 20:08:58 -0600248 if (ofnode_is_np(node))
249 return strrchr(node.np->full_name, '/') + 1;
250
251 return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
252}
253
Kever Yang37df0e02018-02-23 17:38:50 +0100254ofnode ofnode_get_by_phandle(uint phandle)
255{
256 ofnode node;
257
258 if (of_live_active())
259 node = np_to_ofnode(of_find_node_by_phandle(phandle));
260 else
261 node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
262 phandle);
263
264 return node;
265}
266
Keerthyd332e6e2019-04-24 17:19:53 +0530267fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
Simon Glass049ae1b2017-05-18 20:09:01 -0600268{
Keerthy34222a32018-11-19 11:44:47 +0530269 int na, ns;
Keerthy34222a32018-11-19 11:44:47 +0530270
Simon Glass049ae1b2017-05-18 20:09:01 -0600271 if (ofnode_is_np(node)) {
272 const __be32 *prop_val;
Simon Glass47f85de2019-09-25 08:55:50 -0600273 u64 size64;
Simon Glass049ae1b2017-05-18 20:09:01 -0600274 uint flags;
Simon Glass049ae1b2017-05-18 20:09:01 -0600275
Simon Glass47f85de2019-09-25 08:55:50 -0600276 prop_val = of_get_address(ofnode_to_np(node), index, &size64,
277 &flags);
Simon Glass049ae1b2017-05-18 20:09:01 -0600278 if (!prop_val)
279 return FDT_ADDR_T_NONE;
Simon Glass47f85de2019-09-25 08:55:50 -0600280 if (size)
281 *size = size64;
Mario Sixd007ebc2017-12-20 09:52:12 +0100282
Mario Six35616ef2018-03-12 14:53:33 +0100283 ns = of_n_size_cells(ofnode_to_np(node));
284
285 if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
Mario Sixd007ebc2017-12-20 09:52:12 +0100286 return of_translate_address(ofnode_to_np(node), prop_val);
287 } else {
288 na = of_n_addr_cells(ofnode_to_np(node));
289 return of_read_number(prop_val, na);
290 }
Simon Glass049ae1b2017-05-18 20:09:01 -0600291 } else {
Keerthy34222a32018-11-19 11:44:47 +0530292 na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
293 ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
294 return fdtdec_get_addr_size_fixed(gd->fdt_blob,
295 ofnode_to_offset(node), "reg",
Keerthyd332e6e2019-04-24 17:19:53 +0530296 index, na, ns, size, true);
Simon Glass049ae1b2017-05-18 20:09:01 -0600297 }
298
299 return FDT_ADDR_T_NONE;
300}
301
Keerthyd332e6e2019-04-24 17:19:53 +0530302fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
303{
304 fdt_size_t size;
305
306 return ofnode_get_addr_size_index(node, index, &size);
307}
308
Simon Glass049ae1b2017-05-18 20:09:01 -0600309fdt_addr_t ofnode_get_addr(ofnode node)
310{
311 return ofnode_get_addr_index(node, 0);
312}
313
Simon Glassc4fc5622017-05-18 20:08:58 -0600314int ofnode_stringlist_search(ofnode node, const char *property,
315 const char *string)
316{
317 if (ofnode_is_np(node)) {
318 return of_property_match_string(ofnode_to_np(node),
319 property, string);
320 } else {
321 int ret;
322
323 ret = fdt_stringlist_search(gd->fdt_blob,
324 ofnode_to_offset(node), property,
325 string);
326 if (ret == -FDT_ERR_NOTFOUND)
327 return -ENODATA;
328 else if (ret < 0)
329 return -EINVAL;
330
331 return ret;
332 }
333}
334
335int ofnode_read_string_index(ofnode node, const char *property, int index,
336 const char **outp)
337{
338 if (ofnode_is_np(node)) {
339 return of_property_read_string_index(ofnode_to_np(node),
340 property, index, outp);
341 } else {
342 int len;
343
344 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
345 property, index, &len);
346 if (len < 0)
347 return -EINVAL;
348 return 0;
349 }
350}
351
Simon Glass5fdb0052017-06-12 06:21:28 -0600352int ofnode_read_string_count(ofnode node, const char *property)
353{
354 if (ofnode_is_np(node)) {
355 return of_property_count_strings(ofnode_to_np(node), property);
356 } else {
357 return fdt_stringlist_count(gd->fdt_blob,
358 ofnode_to_offset(node), property);
359 }
360}
361
Simon Glassc4fc5622017-05-18 20:08:58 -0600362static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
363 struct ofnode_phandle_args *out)
364{
365 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
366 out->node = offset_to_ofnode(in->node);
367 out->args_count = in->args_count;
368 memcpy(out->args, in->args, sizeof(out->args));
369}
370
371static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
372 struct ofnode_phandle_args *out)
373{
374 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
375 out->node = np_to_ofnode(in->np);
376 out->args_count = in->args_count;
377 memcpy(out->args, in->args, sizeof(out->args));
378}
379
380int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
381 const char *cells_name, int cell_count,
382 int index,
383 struct ofnode_phandle_args *out_args)
384{
385 if (ofnode_is_np(node)) {
386 struct of_phandle_args args;
387 int ret;
388
389 ret = of_parse_phandle_with_args(ofnode_to_np(node),
Mario Sixf40d82c2018-01-15 11:07:17 +0100390 list_name, cells_name, index,
391 &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600392 if (ret)
393 return ret;
394 ofnode_from_of_phandle_args(&args, out_args);
395 } else {
396 struct fdtdec_phandle_args args;
397 int ret;
398
399 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
Mario Sixf40d82c2018-01-15 11:07:17 +0100400 ofnode_to_offset(node),
401 list_name, cells_name,
402 cell_count, index, &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600403 if (ret)
404 return ret;
405 ofnode_from_fdtdec_phandle_args(&args, out_args);
406 }
407
408 return 0;
409}
410
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200411int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
412 const char *cells_name)
413{
414 if (ofnode_is_np(node))
415 return of_count_phandle_with_args(ofnode_to_np(node),
416 list_name, cells_name);
417 else
418 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
419 ofnode_to_offset(node), list_name, cells_name,
420 0, -1, NULL);
421}
422
Simon Glassc4fc5622017-05-18 20:08:58 -0600423ofnode ofnode_path(const char *path)
424{
425 if (of_live_active())
426 return np_to_ofnode(of_find_node_by_path(path));
427 else
428 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
429}
430
Simon Glasse09223c2020-01-27 08:49:46 -0700431const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
Simon Glassc4fc5622017-05-18 20:08:58 -0600432{
433 ofnode chosen_node;
434
435 chosen_node = ofnode_path("/chosen");
436
Simon Glasse09223c2020-01-27 08:49:46 -0700437 return ofnode_read_prop(chosen_node, propname, sizep);
Simon Glassc4fc5622017-05-18 20:08:58 -0600438}
439
Simon Glasse09223c2020-01-27 08:49:46 -0700440const char *ofnode_read_chosen_string(const char *propname)
441{
442 return ofnode_read_chosen_prop(propname, NULL);
443}
444
Simon Glassc4fc5622017-05-18 20:08:58 -0600445ofnode ofnode_get_chosen_node(const char *name)
446{
447 const char *prop;
448
Simon Glasse09223c2020-01-27 08:49:46 -0700449 prop = ofnode_read_chosen_prop(name, NULL);
Simon Glassc4fc5622017-05-18 20:08:58 -0600450 if (!prop)
451 return ofnode_null();
452
453 return ofnode_path(prop);
454}
455
456static int decode_timing_property(ofnode node, const char *name,
457 struct timing_entry *result)
458{
459 int length, ret = 0;
460
461 length = ofnode_read_size(node, name);
462 if (length < 0) {
463 debug("%s: could not find property %s\n",
464 ofnode_get_name(node), name);
465 return length;
466 }
467
468 if (length == sizeof(u32)) {
469 result->typ = ofnode_read_u32_default(node, name, 0);
470 result->min = result->typ;
471 result->max = result->typ;
472 } else {
473 ret = ofnode_read_u32_array(node, name, &result->min, 3);
474 }
475
476 return ret;
477}
478
479int ofnode_decode_display_timing(ofnode parent, int index,
480 struct display_timing *dt)
481{
482 int i;
483 ofnode timings, node;
484 u32 val = 0;
485 int ret = 0;
486
487 timings = ofnode_find_subnode(parent, "display-timings");
488 if (!ofnode_valid(timings))
489 return -EINVAL;
490
Simon Glass28529762017-08-05 15:45:54 -0600491 i = 0;
492 ofnode_for_each_subnode(node, timings) {
493 if (i++ == index)
494 break;
495 }
Simon Glassc4fc5622017-05-18 20:08:58 -0600496
497 if (!ofnode_valid(node))
498 return -EINVAL;
499
500 memset(dt, 0, sizeof(*dt));
501
502 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
503 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
504 ret |= decode_timing_property(node, "hactive", &dt->hactive);
505 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
506 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
507 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
508 ret |= decode_timing_property(node, "vactive", &dt->vactive);
509 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
510 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
511
512 dt->flags = 0;
513 val = ofnode_read_u32_default(node, "vsync-active", -1);
514 if (val != -1) {
515 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
516 DISPLAY_FLAGS_VSYNC_LOW;
517 }
518 val = ofnode_read_u32_default(node, "hsync-active", -1);
519 if (val != -1) {
520 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
521 DISPLAY_FLAGS_HSYNC_LOW;
522 }
523 val = ofnode_read_u32_default(node, "de-active", -1);
524 if (val != -1) {
525 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
526 DISPLAY_FLAGS_DE_LOW;
527 }
528 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
529 if (val != -1) {
530 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
531 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
532 }
533
534 if (ofnode_read_bool(node, "interlaced"))
535 dt->flags |= DISPLAY_FLAGS_INTERLACED;
536 if (ofnode_read_bool(node, "doublescan"))
537 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
538 if (ofnode_read_bool(node, "doubleclk"))
539 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
540
541 return ret;
542}
543
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900544const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
Simon Glassc4fc5622017-05-18 20:08:58 -0600545{
Masahiro Yamada5052f1b2017-06-22 16:54:04 +0900546 if (ofnode_is_np(node))
547 return of_get_property(ofnode_to_np(node), propname, lenp);
548 else
Simon Glassc4fc5622017-05-18 20:08:58 -0600549 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
550 propname, lenp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600551}
552
553bool ofnode_is_available(ofnode node)
554{
555 if (ofnode_is_np(node))
556 return of_device_is_available(ofnode_to_np(node));
557 else
558 return fdtdec_get_is_enabled(gd->fdt_blob,
559 ofnode_to_offset(node));
560}
561
562fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
563 fdt_size_t *sizep)
564{
565 if (ofnode_is_np(node)) {
566 int na, ns;
567 int psize;
568 const struct device_node *np = ofnode_to_np(node);
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200569 const __be32 *prop = of_get_property(np, property, &psize);
Simon Glassc4fc5622017-05-18 20:08:58 -0600570
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200571 if (!prop)
572 return FDT_ADDR_T_NONE;
Simon Glassc4fc5622017-05-18 20:08:58 -0600573 na = of_n_addr_cells(np);
Marek Vasut1638c172018-10-01 12:37:19 +0200574 ns = of_n_size_cells(np);
Simon Glassa67cc632017-05-18 20:09:27 -0600575 *sizep = of_read_number(prop + na, ns);
Marek Vasuta9dac492018-10-01 12:37:20 +0200576
Simon Glass019f9562019-04-25 21:58:36 -0600577 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
Marek Vasuta9dac492018-10-01 12:37:20 +0200578 return of_translate_address(np, prop);
579 else
580 return of_read_number(prop, na);
Simon Glassc4fc5622017-05-18 20:08:58 -0600581 } else {
582 return fdtdec_get_addr_size(gd->fdt_blob,
583 ofnode_to_offset(node), property,
584 sizep);
585 }
586}
587
588const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
589 size_t sz)
590{
591 if (ofnode_is_np(node)) {
592 const struct device_node *np = ofnode_to_np(node);
593 int psize;
594 const __be32 *prop = of_get_property(np, propname, &psize);
595
596 if (!prop || sz != psize)
597 return NULL;
598 return (uint8_t *)prop;
599
600 } else {
601 return fdtdec_locate_byte_array(gd->fdt_blob,
602 ofnode_to_offset(node), propname, sz);
603 }
604}
605
606int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
607 const char *propname, struct fdt_pci_addr *addr)
608{
Masahiro Yamada5c5991e2017-06-22 17:57:50 +0900609 const fdt32_t *cell;
Simon Glassc4fc5622017-05-18 20:08:58 -0600610 int len;
611 int ret = -ENOENT;
612
613 debug("%s: %s: ", __func__, propname);
614
615 /*
616 * If we follow the pci bus bindings strictly, we should check
617 * the value of the node's parent node's #address-cells and
618 * #size-cells. They need to be 3 and 2 accordingly. However,
619 * for simplicity we skip the check here.
620 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900621 cell = ofnode_get_property(node, propname, &len);
Simon Glassc4fc5622017-05-18 20:08:58 -0600622 if (!cell)
623 goto fail;
624
625 if ((len % FDT_PCI_REG_SIZE) == 0) {
626 int num = len / FDT_PCI_REG_SIZE;
627 int i;
628
629 for (i = 0; i < num; i++) {
630 debug("pci address #%d: %08lx %08lx %08lx\n", i,
631 (ulong)fdt32_to_cpu(cell[0]),
632 (ulong)fdt32_to_cpu(cell[1]),
633 (ulong)fdt32_to_cpu(cell[2]));
634 if ((fdt32_to_cpu(*cell) & type) == type) {
635 addr->phys_hi = fdt32_to_cpu(cell[0]);
636 addr->phys_mid = fdt32_to_cpu(cell[1]);
Simon Glassdfd43152019-09-25 08:55:46 -0600637 addr->phys_lo = fdt32_to_cpu(cell[2]);
Simon Glassc4fc5622017-05-18 20:08:58 -0600638 break;
Simon Glassc4fc5622017-05-18 20:08:58 -0600639 }
Mario Sixf40d82c2018-01-15 11:07:17 +0100640
641 cell += (FDT_PCI_ADDR_CELLS +
642 FDT_PCI_SIZE_CELLS);
Simon Glassc4fc5622017-05-18 20:08:58 -0600643 }
644
645 if (i == num) {
646 ret = -ENXIO;
647 goto fail;
648 }
649
650 return 0;
Simon Glassc4fc5622017-05-18 20:08:58 -0600651 }
652
Mario Sixf40d82c2018-01-15 11:07:17 +0100653 ret = -EINVAL;
654
Simon Glassc4fc5622017-05-18 20:08:58 -0600655fail:
656 debug("(not found)\n");
657 return ret;
658}
659
Bin Mengfa157712018-08-03 01:14:35 -0700660int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
661{
662 const char *list, *end;
663 int len;
664
665 list = ofnode_get_property(node, "compatible", &len);
666 if (!list)
667 return -ENOENT;
668
669 end = list + len;
670 while (list < end) {
671 len = strlen(list);
672 if (len >= strlen("pciVVVV,DDDD")) {
673 char *s = strstr(list, "pci");
674
675 /*
676 * check if the string is something like pciVVVV,DDDD.RR
677 * or just pciVVVV,DDDD
678 */
679 if (s && s[7] == ',' &&
680 (s[12] == '.' || s[12] == 0)) {
681 s += 3;
682 *vendor = simple_strtol(s, NULL, 16);
683
684 s += 5;
685 *device = simple_strtol(s, NULL, 16);
686
687 return 0;
688 }
689 }
690 list += (len + 1);
691 }
692
693 return -ENOENT;
694}
695
Simon Glassc4fc5622017-05-18 20:08:58 -0600696int ofnode_read_addr_cells(ofnode node)
697{
698 if (ofnode_is_np(node))
699 return of_n_addr_cells(ofnode_to_np(node));
Simon Glass4191dc12017-06-12 06:21:31 -0600700 else /* NOTE: this call should walk up the parent stack */
Simon Glassc4fc5622017-05-18 20:08:58 -0600701 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
702}
703
704int ofnode_read_size_cells(ofnode node)
705{
706 if (ofnode_is_np(node))
707 return of_n_size_cells(ofnode_to_np(node));
Simon Glass4191dc12017-06-12 06:21:31 -0600708 else /* NOTE: this call should walk up the parent stack */
709 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
710}
711
712int ofnode_read_simple_addr_cells(ofnode node)
713{
714 if (ofnode_is_np(node))
715 return of_simple_addr_cells(ofnode_to_np(node));
716 else
717 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
718}
719
720int ofnode_read_simple_size_cells(ofnode node)
721{
722 if (ofnode_is_np(node))
723 return of_simple_size_cells(ofnode_to_np(node));
Simon Glassc4fc5622017-05-18 20:08:58 -0600724 else
725 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
726}
727
728bool ofnode_pre_reloc(ofnode node)
729{
Patrick Delaunay0b025b82019-02-11 12:49:57 +0100730#if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
731 /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
732 * had property dm-pre-reloc or u-boot,dm-spl/tpl.
733 * They are removed in final dtb (fdtgrep 2nd pass)
734 */
735 return true;
736#else
Masahiro Yamada6a61dd92017-06-22 16:54:03 +0900737 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
Simon Glassc4fc5622017-05-18 20:08:58 -0600738 return true;
Simon Glass23f22842018-10-01 12:22:18 -0600739 if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
740 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -0600741
Simon Glassc4fc5622017-05-18 20:08:58 -0600742 /*
743 * In regular builds individual spl and tpl handling both
744 * count as handled pre-relocation for later second init.
745 */
Masahiro Yamada6a61dd92017-06-22 16:54:03 +0900746 if (ofnode_read_bool(node, "u-boot,dm-spl") ||
747 ofnode_read_bool(node, "u-boot,dm-tpl"))
Simon Glassc4fc5622017-05-18 20:08:58 -0600748 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -0600749
750 return false;
Patrick Delaunay0b025b82019-02-11 12:49:57 +0100751#endif
Simon Glassc4fc5622017-05-18 20:08:58 -0600752}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600753
754int ofnode_read_resource(ofnode node, uint index, struct resource *res)
755{
756 if (ofnode_is_np(node)) {
757 return of_address_to_resource(ofnode_to_np(node), index, res);
758 } else {
759 struct fdt_resource fres;
760 int ret;
761
762 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
763 "reg", index, &fres);
764 if (ret < 0)
765 return -EINVAL;
766 memset(res, '\0', sizeof(*res));
767 res->start = fres.start;
768 res->end = fres.end;
769
770 return 0;
771 }
772}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900773
774int ofnode_read_resource_byname(ofnode node, const char *name,
775 struct resource *res)
776{
777 int index;
778
779 index = ofnode_stringlist_search(node, "reg-names", name);
780 if (index < 0)
781 return index;
782
783 return ofnode_read_resource(node, index, res);
784}
Mario Sixaefac062018-01-15 11:07:19 +0100785
786u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
787{
788 if (ofnode_is_np(node))
789 return of_translate_address(ofnode_to_np(node), in_addr);
790 else
791 return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
792}
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900793
Fabien Dessenne22236e02019-05-31 15:11:30 +0200794u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
795{
796 if (ofnode_is_np(node))
797 return of_translate_dma_address(ofnode_to_np(node), in_addr);
798 else
799 return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
800}
801
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900802int ofnode_device_is_compatible(ofnode node, const char *compat)
803{
804 if (ofnode_is_np(node))
805 return of_device_is_compatible(ofnode_to_np(node), compat,
806 NULL, NULL);
807 else
808 return !fdt_node_check_compatible(gd->fdt_blob,
809 ofnode_to_offset(node),
810 compat);
811}
Simon Glass954eeae2018-06-11 13:07:13 -0600812
813ofnode ofnode_by_compatible(ofnode from, const char *compat)
814{
815 if (of_live_active()) {
816 return np_to_ofnode(of_find_compatible_node(
817 (struct device_node *)ofnode_to_np(from), NULL,
818 compat));
819 } else {
820 return offset_to_ofnode(fdt_node_offset_by_compatible(
821 gd->fdt_blob, ofnode_to_offset(from), compat));
822 }
823}
Jens Wiklander7b68dad2018-08-20 11:09:58 +0200824
825ofnode ofnode_by_prop_value(ofnode from, const char *propname,
826 const void *propval, int proplen)
827{
828 if (of_live_active()) {
829 return np_to_ofnode(of_find_node_by_prop_value(
830 (struct device_node *)ofnode_to_np(from), propname,
831 propval, proplen));
832 } else {
833 return offset_to_ofnode(fdt_node_offset_by_prop_value(
834 gd->fdt_blob, ofnode_to_offset(from),
835 propname, propval, proplen));
836 }
837}
Mario Six047dafc2018-06-26 08:46:48 +0200838
839int ofnode_write_prop(ofnode node, const char *propname, int len,
840 const void *value)
841{
842 const struct device_node *np = ofnode_to_np(node);
843 struct property *pp;
844 struct property *pp_last = NULL;
845 struct property *new;
846
847 if (!of_live_active())
848 return -ENOSYS;
849
850 if (!np)
851 return -EINVAL;
852
853 for (pp = np->properties; pp; pp = pp->next) {
854 if (strcmp(pp->name, propname) == 0) {
855 /* Property exists -> change value */
856 pp->value = (void *)value;
857 pp->length = len;
858 return 0;
859 }
860 pp_last = pp;
861 }
862
863 if (!pp_last)
864 return -ENOENT;
865
866 /* Property does not exist -> append new property */
867 new = malloc(sizeof(struct property));
868 if (!new)
869 return -ENOMEM;
870
871 new->name = strdup(propname);
Mario Six95e885d2018-10-04 09:22:24 +0200872 if (!new->name) {
873 free(new);
Mario Six047dafc2018-06-26 08:46:48 +0200874 return -ENOMEM;
Mario Six95e885d2018-10-04 09:22:24 +0200875 }
Mario Six047dafc2018-06-26 08:46:48 +0200876
877 new->value = (void *)value;
878 new->length = len;
879 new->next = NULL;
880
881 pp_last->next = new;
882
883 return 0;
884}
885
886int ofnode_write_string(ofnode node, const char *propname, const char *value)
887{
888 if (!of_live_active())
889 return -ENOSYS;
890
891 assert(ofnode_valid(node));
892
893 debug("%s: %s = %s", __func__, propname, value);
894
895 return ofnode_write_prop(node, propname, strlen(value) + 1, value);
896}
897
898int ofnode_set_enabled(ofnode node, bool value)
899{
900 if (!of_live_active())
901 return -ENOSYS;
902
903 assert(ofnode_valid(node));
904
905 if (value)
906 return ofnode_write_string(node, "status", "okay");
907 else
Bin Menga9274aa2019-07-05 09:23:17 -0700908 return ofnode_write_string(node, "status", "disabled");
Mario Six047dafc2018-06-26 08:46:48 +0200909}