blob: 4fc29a7c432b010297acbb63a1f345816e3727ab [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>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090011#include <linux/libfdt.h>
Simon Glassc4fc5622017-05-18 20:08:58 -060012#include <dm/of_access.h>
Simon Glass049ae1b2017-05-18 20:09:01 -060013#include <dm/of_addr.h>
Simon Glassc4fc5622017-05-18 20:08:58 -060014#include <dm/ofnode.h>
15#include <linux/err.h>
Simon Glassf7bfcc42017-07-25 08:29:55 -060016#include <linux/ioport.h>
Simon Glassc4fc5622017-05-18 20:08:58 -060017
18int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
19{
20 assert(ofnode_valid(node));
21 debug("%s: %s: ", __func__, propname);
22
23 if (ofnode_is_np(node)) {
24 return of_read_u32(ofnode_to_np(node), propname, outp);
25 } else {
Masahiro Yamada5c5991e2017-06-22 17:57:50 +090026 const fdt32_t *cell;
Simon Glassc4fc5622017-05-18 20:08:58 -060027 int len;
28
29 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
30 propname, &len);
31 if (!cell || len < sizeof(int)) {
32 debug("(not found)\n");
33 return -EINVAL;
34 }
35 *outp = fdt32_to_cpu(cell[0]);
36 }
37 debug("%#x (%d)\n", *outp, *outp);
38
39 return 0;
40}
41
Trent Piepho5b775412019-05-10 17:48:20 +000042u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
Simon Glassc4fc5622017-05-18 20:08:58 -060043{
44 assert(ofnode_valid(node));
45 ofnode_read_u32(node, propname, &def);
46
47 return def;
48}
49
50int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
51{
52 assert(ofnode_valid(node));
53 ofnode_read_u32(node, propname, (u32 *)&def);
54
55 return def;
56}
57
Simon Glass9d54a7a2018-06-11 13:07:10 -060058int ofnode_read_u64(ofnode node, const char *propname, u64 *outp)
59{
Jean-Jacques Hiblot487f9172019-10-22 10:05:22 +020060 const unaligned_fdt64_t *cell;
Simon Glass9d54a7a2018-06-11 13:07:10 -060061 int len;
62
63 assert(ofnode_valid(node));
64 debug("%s: %s: ", __func__, propname);
65
66 if (ofnode_is_np(node))
67 return of_read_u64(ofnode_to_np(node), propname, outp);
68
69 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
70 &len);
71 if (!cell || len < sizeof(*cell)) {
72 debug("(not found)\n");
73 return -EINVAL;
74 }
75 *outp = fdt64_to_cpu(cell[0]);
76 debug("%#llx (%lld)\n", (unsigned long long)*outp,
77 (unsigned long long)*outp);
78
79 return 0;
80}
81
T Karthik Reddy478860d2019-09-02 16:34:30 +020082u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def)
Simon Glass9d54a7a2018-06-11 13:07:10 -060083{
84 assert(ofnode_valid(node));
85 ofnode_read_u64(node, propname, &def);
86
87 return def;
88}
89
Simon Glassc4fc5622017-05-18 20:08:58 -060090bool ofnode_read_bool(ofnode node, const char *propname)
91{
Masahiro Yamada5d434452017-06-22 16:54:07 +090092 const void *prop;
Simon Glassc4fc5622017-05-18 20:08:58 -060093
94 assert(ofnode_valid(node));
95 debug("%s: %s: ", __func__, propname);
96
Masahiro Yamada5d434452017-06-22 16:54:07 +090097 prop = ofnode_get_property(node, propname, NULL);
98
99 debug("%s\n", prop ? "true" : "false");
Simon Glassc4fc5622017-05-18 20:08:58 -0600100
Masahiro Yamada5d434452017-06-22 16:54:07 +0900101 return prop ? true : false;
Simon Glassc4fc5622017-05-18 20:08:58 -0600102}
103
Simon Glass0c2e9802020-01-27 08:49:44 -0700104const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep)
Simon Glassc4fc5622017-05-18 20:08:58 -0600105{
Simon Glass0c2e9802020-01-27 08:49:44 -0700106 const char *val = NULL;
107 int len;
Simon Glassc4fc5622017-05-18 20:08:58 -0600108
109 assert(ofnode_valid(node));
110 debug("%s: %s: ", __func__, propname);
111
112 if (ofnode_is_np(node)) {
113 struct property *prop = of_find_property(
Simon Glass0c2e9802020-01-27 08:49:44 -0700114 ofnode_to_np(node), propname, &len);
Simon Glassc4fc5622017-05-18 20:08:58 -0600115
116 if (prop) {
Simon Glass0c2e9802020-01-27 08:49:44 -0700117 val = prop->value;
Simon Glassc4fc5622017-05-18 20:08:58 -0600118 len = prop->length;
119 }
120 } else {
Simon Glass0c2e9802020-01-27 08:49:44 -0700121 val = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
Simon Glassc4fc5622017-05-18 20:08:58 -0600122 propname, &len);
123 }
Simon Glass0c2e9802020-01-27 08:49:44 -0700124 if (!val) {
Simon Glassc4fc5622017-05-18 20:08:58 -0600125 debug("<not found>\n");
Simon Glass0c2e9802020-01-27 08:49:44 -0700126 if (sizep)
127 *sizep = -FDT_ERR_NOTFOUND;
Simon Glassc4fc5622017-05-18 20:08:58 -0600128 return NULL;
129 }
Simon Glass0c2e9802020-01-27 08:49:44 -0700130 if (sizep)
131 *sizep = len;
132
133 return val;
134}
135
136const char *ofnode_read_string(ofnode node, const char *propname)
137{
138 const char *str;
139 int len;
140
141 str = ofnode_read_prop(node, propname, &len);
142 if (!str)
143 return NULL;
144
Simon Glassc4fc5622017-05-18 20:08:58 -0600145 if (strnlen(str, len) >= len) {
146 debug("<invalid>\n");
147 return NULL;
148 }
149 debug("%s\n", str);
150
151 return str;
152}
153
Simon Glass81c54b32020-01-27 08:49:45 -0700154int ofnode_read_size(ofnode node, const char *propname)
155{
156 int len;
157
158 if (!ofnode_read_prop(node, propname, &len))
159 return -EINVAL;
160
161 return len;
162}
163
Simon Glassc4fc5622017-05-18 20:08:58 -0600164ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
165{
166 ofnode subnode;
167
168 assert(ofnode_valid(node));
169 debug("%s: %s: ", __func__, subnode_name);
170
171 if (ofnode_is_np(node)) {
172 const struct device_node *np = ofnode_to_np(node);
173
174 for (np = np->child; np; np = np->sibling) {
175 if (!strcmp(subnode_name, np->name))
176 break;
177 }
178 subnode = np_to_ofnode(np);
179 } else {
180 int ooffset = fdt_subnode_offset(gd->fdt_blob,
181 ofnode_to_offset(node), subnode_name);
182 subnode = offset_to_ofnode(ooffset);
183 }
184 debug("%s\n", ofnode_valid(subnode) ?
185 ofnode_get_name(subnode) : "<none>");
186
187 return subnode;
188}
189
190int ofnode_read_u32_array(ofnode node, const char *propname,
191 u32 *out_values, size_t sz)
192{
193 assert(ofnode_valid(node));
194 debug("%s: %s: ", __func__, propname);
195
196 if (ofnode_is_np(node)) {
197 return of_read_u32_array(ofnode_to_np(node), propname,
198 out_values, sz);
199 } else {
200 return fdtdec_get_int_array(gd->fdt_blob,
201 ofnode_to_offset(node), propname,
202 out_values, sz);
203 }
204}
205
206ofnode ofnode_first_subnode(ofnode node)
207{
208 assert(ofnode_valid(node));
209 if (ofnode_is_np(node))
210 return np_to_ofnode(node.np->child);
211
212 return offset_to_ofnode(
213 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
214}
215
216ofnode ofnode_next_subnode(ofnode node)
217{
218 assert(ofnode_valid(node));
219 if (ofnode_is_np(node))
220 return np_to_ofnode(node.np->sibling);
221
222 return offset_to_ofnode(
223 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
224}
225
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100226ofnode ofnode_get_parent(ofnode node)
227{
228 ofnode parent;
229
230 assert(ofnode_valid(node));
231 if (ofnode_is_np(node))
232 parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
233 else
234 parent.of_offset = fdt_parent_offset(gd->fdt_blob,
235 ofnode_to_offset(node));
236
237 return parent;
238}
239
Simon Glassc4fc5622017-05-18 20:08:58 -0600240const char *ofnode_get_name(ofnode node)
241{
Kever Yang8d7976d2019-07-19 11:23:47 +0800242 if (!ofnode_valid(node)) {
243 debug("%s node not valid\n", __func__);
244 return NULL;
245 }
246
Simon Glassc4fc5622017-05-18 20:08:58 -0600247 if (ofnode_is_np(node))
248 return strrchr(node.np->full_name, '/') + 1;
249
250 return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
251}
252
Kever Yang37df0e02018-02-23 17:38:50 +0100253ofnode ofnode_get_by_phandle(uint phandle)
254{
255 ofnode node;
256
257 if (of_live_active())
258 node = np_to_ofnode(of_find_node_by_phandle(phandle));
259 else
260 node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
261 phandle);
262
263 return node;
264}
265
Keerthyd332e6e2019-04-24 17:19:53 +0530266fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
Simon Glass049ae1b2017-05-18 20:09:01 -0600267{
Keerthy34222a32018-11-19 11:44:47 +0530268 int na, ns;
Keerthy34222a32018-11-19 11:44:47 +0530269
Simon Glass049ae1b2017-05-18 20:09:01 -0600270 if (ofnode_is_np(node)) {
271 const __be32 *prop_val;
Simon Glass47f85de2019-09-25 08:55:50 -0600272 u64 size64;
Simon Glass049ae1b2017-05-18 20:09:01 -0600273 uint flags;
Simon Glass049ae1b2017-05-18 20:09:01 -0600274
Simon Glass47f85de2019-09-25 08:55:50 -0600275 prop_val = of_get_address(ofnode_to_np(node), index, &size64,
276 &flags);
Simon Glass049ae1b2017-05-18 20:09:01 -0600277 if (!prop_val)
278 return FDT_ADDR_T_NONE;
Simon Glass47f85de2019-09-25 08:55:50 -0600279 if (size)
280 *size = size64;
Mario Sixd007ebc2017-12-20 09:52:12 +0100281
Mario Six35616ef2018-03-12 14:53:33 +0100282 ns = of_n_size_cells(ofnode_to_np(node));
283
284 if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
Mario Sixd007ebc2017-12-20 09:52:12 +0100285 return of_translate_address(ofnode_to_np(node), prop_val);
286 } else {
287 na = of_n_addr_cells(ofnode_to_np(node));
288 return of_read_number(prop_val, na);
289 }
Simon Glass049ae1b2017-05-18 20:09:01 -0600290 } else {
Keerthy34222a32018-11-19 11:44:47 +0530291 na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
292 ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
293 return fdtdec_get_addr_size_fixed(gd->fdt_blob,
294 ofnode_to_offset(node), "reg",
Keerthyd332e6e2019-04-24 17:19:53 +0530295 index, na, ns, size, true);
Simon Glass049ae1b2017-05-18 20:09:01 -0600296 }
297
298 return FDT_ADDR_T_NONE;
299}
300
Keerthyd332e6e2019-04-24 17:19:53 +0530301fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
302{
303 fdt_size_t size;
304
305 return ofnode_get_addr_size_index(node, index, &size);
306}
307
Simon Glass049ae1b2017-05-18 20:09:01 -0600308fdt_addr_t ofnode_get_addr(ofnode node)
309{
310 return ofnode_get_addr_index(node, 0);
311}
312
Simon Glassc4fc5622017-05-18 20:08:58 -0600313int ofnode_stringlist_search(ofnode node, const char *property,
314 const char *string)
315{
316 if (ofnode_is_np(node)) {
317 return of_property_match_string(ofnode_to_np(node),
318 property, string);
319 } else {
320 int ret;
321
322 ret = fdt_stringlist_search(gd->fdt_blob,
323 ofnode_to_offset(node), property,
324 string);
325 if (ret == -FDT_ERR_NOTFOUND)
326 return -ENODATA;
327 else if (ret < 0)
328 return -EINVAL;
329
330 return ret;
331 }
332}
333
334int ofnode_read_string_index(ofnode node, const char *property, int index,
335 const char **outp)
336{
337 if (ofnode_is_np(node)) {
338 return of_property_read_string_index(ofnode_to_np(node),
339 property, index, outp);
340 } else {
341 int len;
342
343 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
344 property, index, &len);
345 if (len < 0)
346 return -EINVAL;
347 return 0;
348 }
349}
350
Simon Glass5fdb0052017-06-12 06:21:28 -0600351int ofnode_read_string_count(ofnode node, const char *property)
352{
353 if (ofnode_is_np(node)) {
354 return of_property_count_strings(ofnode_to_np(node), property);
355 } else {
356 return fdt_stringlist_count(gd->fdt_blob,
357 ofnode_to_offset(node), property);
358 }
359}
360
Simon Glassc4fc5622017-05-18 20:08:58 -0600361static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
362 struct ofnode_phandle_args *out)
363{
364 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
365 out->node = offset_to_ofnode(in->node);
366 out->args_count = in->args_count;
367 memcpy(out->args, in->args, sizeof(out->args));
368}
369
370static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
371 struct ofnode_phandle_args *out)
372{
373 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
374 out->node = np_to_ofnode(in->np);
375 out->args_count = in->args_count;
376 memcpy(out->args, in->args, sizeof(out->args));
377}
378
379int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
380 const char *cells_name, int cell_count,
381 int index,
382 struct ofnode_phandle_args *out_args)
383{
384 if (ofnode_is_np(node)) {
385 struct of_phandle_args args;
386 int ret;
387
388 ret = of_parse_phandle_with_args(ofnode_to_np(node),
Mario Sixf40d82c2018-01-15 11:07:17 +0100389 list_name, cells_name, index,
390 &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600391 if (ret)
392 return ret;
393 ofnode_from_of_phandle_args(&args, out_args);
394 } else {
395 struct fdtdec_phandle_args args;
396 int ret;
397
398 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
Mario Sixf40d82c2018-01-15 11:07:17 +0100399 ofnode_to_offset(node),
400 list_name, cells_name,
401 cell_count, index, &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600402 if (ret)
403 return ret;
404 ofnode_from_fdtdec_phandle_args(&args, out_args);
405 }
406
407 return 0;
408}
409
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200410int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
411 const char *cells_name)
412{
413 if (ofnode_is_np(node))
414 return of_count_phandle_with_args(ofnode_to_np(node),
415 list_name, cells_name);
416 else
417 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
418 ofnode_to_offset(node), list_name, cells_name,
419 0, -1, NULL);
420}
421
Simon Glassc4fc5622017-05-18 20:08:58 -0600422ofnode ofnode_path(const char *path)
423{
424 if (of_live_active())
425 return np_to_ofnode(of_find_node_by_path(path));
426 else
427 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
428}
429
Simon Glassf3455962020-01-27 08:49:43 -0700430const char *ofnode_read_chosen_string(const char *name)
Simon Glassc4fc5622017-05-18 20:08:58 -0600431{
432 ofnode chosen_node;
433
434 chosen_node = ofnode_path("/chosen");
435
436 return ofnode_read_string(chosen_node, name);
437}
438
439ofnode ofnode_get_chosen_node(const char *name)
440{
441 const char *prop;
442
Simon Glassf3455962020-01-27 08:49:43 -0700443 prop = ofnode_read_chosen_string(name);
Simon Glassc4fc5622017-05-18 20:08:58 -0600444 if (!prop)
445 return ofnode_null();
446
447 return ofnode_path(prop);
448}
449
450static int decode_timing_property(ofnode node, const char *name,
451 struct timing_entry *result)
452{
453 int length, ret = 0;
454
455 length = ofnode_read_size(node, name);
456 if (length < 0) {
457 debug("%s: could not find property %s\n",
458 ofnode_get_name(node), name);
459 return length;
460 }
461
462 if (length == sizeof(u32)) {
463 result->typ = ofnode_read_u32_default(node, name, 0);
464 result->min = result->typ;
465 result->max = result->typ;
466 } else {
467 ret = ofnode_read_u32_array(node, name, &result->min, 3);
468 }
469
470 return ret;
471}
472
473int ofnode_decode_display_timing(ofnode parent, int index,
474 struct display_timing *dt)
475{
476 int i;
477 ofnode timings, node;
478 u32 val = 0;
479 int ret = 0;
480
481 timings = ofnode_find_subnode(parent, "display-timings");
482 if (!ofnode_valid(timings))
483 return -EINVAL;
484
Simon Glass28529762017-08-05 15:45:54 -0600485 i = 0;
486 ofnode_for_each_subnode(node, timings) {
487 if (i++ == index)
488 break;
489 }
Simon Glassc4fc5622017-05-18 20:08:58 -0600490
491 if (!ofnode_valid(node))
492 return -EINVAL;
493
494 memset(dt, 0, sizeof(*dt));
495
496 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
497 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
498 ret |= decode_timing_property(node, "hactive", &dt->hactive);
499 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
500 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
501 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
502 ret |= decode_timing_property(node, "vactive", &dt->vactive);
503 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
504 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
505
506 dt->flags = 0;
507 val = ofnode_read_u32_default(node, "vsync-active", -1);
508 if (val != -1) {
509 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
510 DISPLAY_FLAGS_VSYNC_LOW;
511 }
512 val = ofnode_read_u32_default(node, "hsync-active", -1);
513 if (val != -1) {
514 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
515 DISPLAY_FLAGS_HSYNC_LOW;
516 }
517 val = ofnode_read_u32_default(node, "de-active", -1);
518 if (val != -1) {
519 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
520 DISPLAY_FLAGS_DE_LOW;
521 }
522 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
523 if (val != -1) {
524 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
525 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
526 }
527
528 if (ofnode_read_bool(node, "interlaced"))
529 dt->flags |= DISPLAY_FLAGS_INTERLACED;
530 if (ofnode_read_bool(node, "doublescan"))
531 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
532 if (ofnode_read_bool(node, "doubleclk"))
533 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
534
535 return ret;
536}
537
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900538const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
Simon Glassc4fc5622017-05-18 20:08:58 -0600539{
Masahiro Yamada5052f1b2017-06-22 16:54:04 +0900540 if (ofnode_is_np(node))
541 return of_get_property(ofnode_to_np(node), propname, lenp);
542 else
Simon Glassc4fc5622017-05-18 20:08:58 -0600543 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
544 propname, lenp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600545}
546
547bool ofnode_is_available(ofnode node)
548{
549 if (ofnode_is_np(node))
550 return of_device_is_available(ofnode_to_np(node));
551 else
552 return fdtdec_get_is_enabled(gd->fdt_blob,
553 ofnode_to_offset(node));
554}
555
556fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
557 fdt_size_t *sizep)
558{
559 if (ofnode_is_np(node)) {
560 int na, ns;
561 int psize;
562 const struct device_node *np = ofnode_to_np(node);
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200563 const __be32 *prop = of_get_property(np, property, &psize);
Simon Glassc4fc5622017-05-18 20:08:58 -0600564
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200565 if (!prop)
566 return FDT_ADDR_T_NONE;
Simon Glassc4fc5622017-05-18 20:08:58 -0600567 na = of_n_addr_cells(np);
Marek Vasut1638c172018-10-01 12:37:19 +0200568 ns = of_n_size_cells(np);
Simon Glassa67cc632017-05-18 20:09:27 -0600569 *sizep = of_read_number(prop + na, ns);
Marek Vasuta9dac492018-10-01 12:37:20 +0200570
Simon Glass019f9562019-04-25 21:58:36 -0600571 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
Marek Vasuta9dac492018-10-01 12:37:20 +0200572 return of_translate_address(np, prop);
573 else
574 return of_read_number(prop, na);
Simon Glassc4fc5622017-05-18 20:08:58 -0600575 } else {
576 return fdtdec_get_addr_size(gd->fdt_blob,
577 ofnode_to_offset(node), property,
578 sizep);
579 }
580}
581
582const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
583 size_t sz)
584{
585 if (ofnode_is_np(node)) {
586 const struct device_node *np = ofnode_to_np(node);
587 int psize;
588 const __be32 *prop = of_get_property(np, propname, &psize);
589
590 if (!prop || sz != psize)
591 return NULL;
592 return (uint8_t *)prop;
593
594 } else {
595 return fdtdec_locate_byte_array(gd->fdt_blob,
596 ofnode_to_offset(node), propname, sz);
597 }
598}
599
600int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
601 const char *propname, struct fdt_pci_addr *addr)
602{
Masahiro Yamada5c5991e2017-06-22 17:57:50 +0900603 const fdt32_t *cell;
Simon Glassc4fc5622017-05-18 20:08:58 -0600604 int len;
605 int ret = -ENOENT;
606
607 debug("%s: %s: ", __func__, propname);
608
609 /*
610 * If we follow the pci bus bindings strictly, we should check
611 * the value of the node's parent node's #address-cells and
612 * #size-cells. They need to be 3 and 2 accordingly. However,
613 * for simplicity we skip the check here.
614 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900615 cell = ofnode_get_property(node, propname, &len);
Simon Glassc4fc5622017-05-18 20:08:58 -0600616 if (!cell)
617 goto fail;
618
619 if ((len % FDT_PCI_REG_SIZE) == 0) {
620 int num = len / FDT_PCI_REG_SIZE;
621 int i;
622
623 for (i = 0; i < num; i++) {
624 debug("pci address #%d: %08lx %08lx %08lx\n", i,
625 (ulong)fdt32_to_cpu(cell[0]),
626 (ulong)fdt32_to_cpu(cell[1]),
627 (ulong)fdt32_to_cpu(cell[2]));
628 if ((fdt32_to_cpu(*cell) & type) == type) {
629 addr->phys_hi = fdt32_to_cpu(cell[0]);
630 addr->phys_mid = fdt32_to_cpu(cell[1]);
Simon Glassdfd43152019-09-25 08:55:46 -0600631 addr->phys_lo = fdt32_to_cpu(cell[2]);
Simon Glassc4fc5622017-05-18 20:08:58 -0600632 break;
Simon Glassc4fc5622017-05-18 20:08:58 -0600633 }
Mario Sixf40d82c2018-01-15 11:07:17 +0100634
635 cell += (FDT_PCI_ADDR_CELLS +
636 FDT_PCI_SIZE_CELLS);
Simon Glassc4fc5622017-05-18 20:08:58 -0600637 }
638
639 if (i == num) {
640 ret = -ENXIO;
641 goto fail;
642 }
643
644 return 0;
Simon Glassc4fc5622017-05-18 20:08:58 -0600645 }
646
Mario Sixf40d82c2018-01-15 11:07:17 +0100647 ret = -EINVAL;
648
Simon Glassc4fc5622017-05-18 20:08:58 -0600649fail:
650 debug("(not found)\n");
651 return ret;
652}
653
Bin Mengfa157712018-08-03 01:14:35 -0700654int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
655{
656 const char *list, *end;
657 int len;
658
659 list = ofnode_get_property(node, "compatible", &len);
660 if (!list)
661 return -ENOENT;
662
663 end = list + len;
664 while (list < end) {
665 len = strlen(list);
666 if (len >= strlen("pciVVVV,DDDD")) {
667 char *s = strstr(list, "pci");
668
669 /*
670 * check if the string is something like pciVVVV,DDDD.RR
671 * or just pciVVVV,DDDD
672 */
673 if (s && s[7] == ',' &&
674 (s[12] == '.' || s[12] == 0)) {
675 s += 3;
676 *vendor = simple_strtol(s, NULL, 16);
677
678 s += 5;
679 *device = simple_strtol(s, NULL, 16);
680
681 return 0;
682 }
683 }
684 list += (len + 1);
685 }
686
687 return -ENOENT;
688}
689
Simon Glassc4fc5622017-05-18 20:08:58 -0600690int ofnode_read_addr_cells(ofnode node)
691{
692 if (ofnode_is_np(node))
693 return of_n_addr_cells(ofnode_to_np(node));
Simon Glass4191dc12017-06-12 06:21:31 -0600694 else /* NOTE: this call should walk up the parent stack */
Simon Glassc4fc5622017-05-18 20:08:58 -0600695 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
696}
697
698int ofnode_read_size_cells(ofnode node)
699{
700 if (ofnode_is_np(node))
701 return of_n_size_cells(ofnode_to_np(node));
Simon Glass4191dc12017-06-12 06:21:31 -0600702 else /* NOTE: this call should walk up the parent stack */
703 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
704}
705
706int ofnode_read_simple_addr_cells(ofnode node)
707{
708 if (ofnode_is_np(node))
709 return of_simple_addr_cells(ofnode_to_np(node));
710 else
711 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
712}
713
714int ofnode_read_simple_size_cells(ofnode node)
715{
716 if (ofnode_is_np(node))
717 return of_simple_size_cells(ofnode_to_np(node));
Simon Glassc4fc5622017-05-18 20:08:58 -0600718 else
719 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
720}
721
722bool ofnode_pre_reloc(ofnode node)
723{
Patrick Delaunay0b025b82019-02-11 12:49:57 +0100724#if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
725 /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
726 * had property dm-pre-reloc or u-boot,dm-spl/tpl.
727 * They are removed in final dtb (fdtgrep 2nd pass)
728 */
729 return true;
730#else
Masahiro Yamada6a61dd92017-06-22 16:54:03 +0900731 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
Simon Glassc4fc5622017-05-18 20:08:58 -0600732 return true;
Simon Glass23f22842018-10-01 12:22:18 -0600733 if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
734 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -0600735
Simon Glassc4fc5622017-05-18 20:08:58 -0600736 /*
737 * In regular builds individual spl and tpl handling both
738 * count as handled pre-relocation for later second init.
739 */
Masahiro Yamada6a61dd92017-06-22 16:54:03 +0900740 if (ofnode_read_bool(node, "u-boot,dm-spl") ||
741 ofnode_read_bool(node, "u-boot,dm-tpl"))
Simon Glassc4fc5622017-05-18 20:08:58 -0600742 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -0600743
744 return false;
Patrick Delaunay0b025b82019-02-11 12:49:57 +0100745#endif
Simon Glassc4fc5622017-05-18 20:08:58 -0600746}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600747
748int ofnode_read_resource(ofnode node, uint index, struct resource *res)
749{
750 if (ofnode_is_np(node)) {
751 return of_address_to_resource(ofnode_to_np(node), index, res);
752 } else {
753 struct fdt_resource fres;
754 int ret;
755
756 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
757 "reg", index, &fres);
758 if (ret < 0)
759 return -EINVAL;
760 memset(res, '\0', sizeof(*res));
761 res->start = fres.start;
762 res->end = fres.end;
763
764 return 0;
765 }
766}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900767
768int ofnode_read_resource_byname(ofnode node, const char *name,
769 struct resource *res)
770{
771 int index;
772
773 index = ofnode_stringlist_search(node, "reg-names", name);
774 if (index < 0)
775 return index;
776
777 return ofnode_read_resource(node, index, res);
778}
Mario Sixaefac062018-01-15 11:07:19 +0100779
780u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
781{
782 if (ofnode_is_np(node))
783 return of_translate_address(ofnode_to_np(node), in_addr);
784 else
785 return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
786}
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900787
Fabien Dessenne22236e02019-05-31 15:11:30 +0200788u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
789{
790 if (ofnode_is_np(node))
791 return of_translate_dma_address(ofnode_to_np(node), in_addr);
792 else
793 return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
794}
795
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900796int ofnode_device_is_compatible(ofnode node, const char *compat)
797{
798 if (ofnode_is_np(node))
799 return of_device_is_compatible(ofnode_to_np(node), compat,
800 NULL, NULL);
801 else
802 return !fdt_node_check_compatible(gd->fdt_blob,
803 ofnode_to_offset(node),
804 compat);
805}
Simon Glass954eeae2018-06-11 13:07:13 -0600806
807ofnode ofnode_by_compatible(ofnode from, const char *compat)
808{
809 if (of_live_active()) {
810 return np_to_ofnode(of_find_compatible_node(
811 (struct device_node *)ofnode_to_np(from), NULL,
812 compat));
813 } else {
814 return offset_to_ofnode(fdt_node_offset_by_compatible(
815 gd->fdt_blob, ofnode_to_offset(from), compat));
816 }
817}
Jens Wiklander7b68dad2018-08-20 11:09:58 +0200818
819ofnode ofnode_by_prop_value(ofnode from, const char *propname,
820 const void *propval, int proplen)
821{
822 if (of_live_active()) {
823 return np_to_ofnode(of_find_node_by_prop_value(
824 (struct device_node *)ofnode_to_np(from), propname,
825 propval, proplen));
826 } else {
827 return offset_to_ofnode(fdt_node_offset_by_prop_value(
828 gd->fdt_blob, ofnode_to_offset(from),
829 propname, propval, proplen));
830 }
831}
Mario Six047dafc2018-06-26 08:46:48 +0200832
833int ofnode_write_prop(ofnode node, const char *propname, int len,
834 const void *value)
835{
836 const struct device_node *np = ofnode_to_np(node);
837 struct property *pp;
838 struct property *pp_last = NULL;
839 struct property *new;
840
841 if (!of_live_active())
842 return -ENOSYS;
843
844 if (!np)
845 return -EINVAL;
846
847 for (pp = np->properties; pp; pp = pp->next) {
848 if (strcmp(pp->name, propname) == 0) {
849 /* Property exists -> change value */
850 pp->value = (void *)value;
851 pp->length = len;
852 return 0;
853 }
854 pp_last = pp;
855 }
856
857 if (!pp_last)
858 return -ENOENT;
859
860 /* Property does not exist -> append new property */
861 new = malloc(sizeof(struct property));
862 if (!new)
863 return -ENOMEM;
864
865 new->name = strdup(propname);
Mario Six95e885d2018-10-04 09:22:24 +0200866 if (!new->name) {
867 free(new);
Mario Six047dafc2018-06-26 08:46:48 +0200868 return -ENOMEM;
Mario Six95e885d2018-10-04 09:22:24 +0200869 }
Mario Six047dafc2018-06-26 08:46:48 +0200870
871 new->value = (void *)value;
872 new->length = len;
873 new->next = NULL;
874
875 pp_last->next = new;
876
877 return 0;
878}
879
880int ofnode_write_string(ofnode node, const char *propname, const char *value)
881{
882 if (!of_live_active())
883 return -ENOSYS;
884
885 assert(ofnode_valid(node));
886
887 debug("%s: %s = %s", __func__, propname, value);
888
889 return ofnode_write_prop(node, propname, strlen(value) + 1, value);
890}
891
892int ofnode_set_enabled(ofnode node, bool value)
893{
894 if (!of_live_active())
895 return -ENOSYS;
896
897 assert(ofnode_valid(node));
898
899 if (value)
900 return ofnode_write_string(node, "status", "okay");
901 else
Bin Menga9274aa2019-07-05 09:23:17 -0700902 return ofnode_write_string(node, "status", "disabled");
Mario Six047dafc2018-06-26 08:46:48 +0200903}