blob: 7eca00cd6613622b5adbd9b9bebde1a0f1575995 [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{
60 const fdt64_t *cell;
61 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
104const char *ofnode_read_string(ofnode node, const char *propname)
105{
106 const char *str = NULL;
107 int len = -1;
108
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(
114 ofnode_to_np(node), propname, NULL);
115
116 if (prop) {
117 str = prop->value;
118 len = prop->length;
119 }
120 } else {
121 str = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
122 propname, &len);
123 }
124 if (!str) {
125 debug("<not found>\n");
126 return NULL;
127 }
128 if (strnlen(str, len) >= len) {
129 debug("<invalid>\n");
130 return NULL;
131 }
132 debug("%s\n", str);
133
134 return str;
135}
136
137ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
138{
139 ofnode subnode;
140
141 assert(ofnode_valid(node));
142 debug("%s: %s: ", __func__, subnode_name);
143
144 if (ofnode_is_np(node)) {
145 const struct device_node *np = ofnode_to_np(node);
146
147 for (np = np->child; np; np = np->sibling) {
148 if (!strcmp(subnode_name, np->name))
149 break;
150 }
151 subnode = np_to_ofnode(np);
152 } else {
153 int ooffset = fdt_subnode_offset(gd->fdt_blob,
154 ofnode_to_offset(node), subnode_name);
155 subnode = offset_to_ofnode(ooffset);
156 }
157 debug("%s\n", ofnode_valid(subnode) ?
158 ofnode_get_name(subnode) : "<none>");
159
160 return subnode;
161}
162
163int ofnode_read_u32_array(ofnode node, const char *propname,
164 u32 *out_values, size_t sz)
165{
166 assert(ofnode_valid(node));
167 debug("%s: %s: ", __func__, propname);
168
169 if (ofnode_is_np(node)) {
170 return of_read_u32_array(ofnode_to_np(node), propname,
171 out_values, sz);
172 } else {
173 return fdtdec_get_int_array(gd->fdt_blob,
174 ofnode_to_offset(node), propname,
175 out_values, sz);
176 }
177}
178
179ofnode ofnode_first_subnode(ofnode node)
180{
181 assert(ofnode_valid(node));
182 if (ofnode_is_np(node))
183 return np_to_ofnode(node.np->child);
184
185 return offset_to_ofnode(
186 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
187}
188
189ofnode ofnode_next_subnode(ofnode node)
190{
191 assert(ofnode_valid(node));
192 if (ofnode_is_np(node))
193 return np_to_ofnode(node.np->sibling);
194
195 return offset_to_ofnode(
196 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
197}
198
Philipp Tomsich6fce1dd2018-02-23 17:38:49 +0100199ofnode ofnode_get_parent(ofnode node)
200{
201 ofnode parent;
202
203 assert(ofnode_valid(node));
204 if (ofnode_is_np(node))
205 parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
206 else
207 parent.of_offset = fdt_parent_offset(gd->fdt_blob,
208 ofnode_to_offset(node));
209
210 return parent;
211}
212
Simon Glassc4fc5622017-05-18 20:08:58 -0600213const char *ofnode_get_name(ofnode node)
214{
Kever Yang8d7976d2019-07-19 11:23:47 +0800215 if (!ofnode_valid(node)) {
216 debug("%s node not valid\n", __func__);
217 return NULL;
218 }
219
Simon Glassc4fc5622017-05-18 20:08:58 -0600220 if (ofnode_is_np(node))
221 return strrchr(node.np->full_name, '/') + 1;
222
223 return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
224}
225
Kever Yang37df0e02018-02-23 17:38:50 +0100226ofnode ofnode_get_by_phandle(uint phandle)
227{
228 ofnode node;
229
230 if (of_live_active())
231 node = np_to_ofnode(of_find_node_by_phandle(phandle));
232 else
233 node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
234 phandle);
235
236 return node;
237}
238
Simon Glassc4fc5622017-05-18 20:08:58 -0600239int ofnode_read_size(ofnode node, const char *propname)
240{
241 int len;
242
243 if (ofnode_is_np(node)) {
244 struct property *prop = of_find_property(
245 ofnode_to_np(node), propname, NULL);
246
247 if (prop)
248 return prop->length;
249 } else {
250 if (fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
251 &len))
252 return len;
253 }
254
255 return -EINVAL;
256}
257
Keerthyd332e6e2019-04-24 17:19:53 +0530258fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
Simon Glass049ae1b2017-05-18 20:09:01 -0600259{
Keerthy34222a32018-11-19 11:44:47 +0530260 int na, ns;
Keerthy34222a32018-11-19 11:44:47 +0530261
Simon Glass049ae1b2017-05-18 20:09:01 -0600262 if (ofnode_is_np(node)) {
263 const __be32 *prop_val;
264 uint flags;
Simon Glass049ae1b2017-05-18 20:09:01 -0600265
Keerthy34222a32018-11-19 11:44:47 +0530266 prop_val = of_get_address(ofnode_to_np(node), index,
Keerthyd332e6e2019-04-24 17:19:53 +0530267 (u64 *)size, &flags);
Simon Glass049ae1b2017-05-18 20:09:01 -0600268 if (!prop_val)
269 return FDT_ADDR_T_NONE;
Mario Sixd007ebc2017-12-20 09:52:12 +0100270
Mario Six35616ef2018-03-12 14:53:33 +0100271 ns = of_n_size_cells(ofnode_to_np(node));
272
273 if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
Mario Sixd007ebc2017-12-20 09:52:12 +0100274 return of_translate_address(ofnode_to_np(node), prop_val);
275 } else {
276 na = of_n_addr_cells(ofnode_to_np(node));
277 return of_read_number(prop_val, na);
278 }
Simon Glass049ae1b2017-05-18 20:09:01 -0600279 } else {
Keerthy34222a32018-11-19 11:44:47 +0530280 na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
281 ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
282 return fdtdec_get_addr_size_fixed(gd->fdt_blob,
283 ofnode_to_offset(node), "reg",
Keerthyd332e6e2019-04-24 17:19:53 +0530284 index, na, ns, size, true);
Simon Glass049ae1b2017-05-18 20:09:01 -0600285 }
286
287 return FDT_ADDR_T_NONE;
288}
289
Keerthyd332e6e2019-04-24 17:19:53 +0530290fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
291{
292 fdt_size_t size;
293
294 return ofnode_get_addr_size_index(node, index, &size);
295}
296
Simon Glass049ae1b2017-05-18 20:09:01 -0600297fdt_addr_t ofnode_get_addr(ofnode node)
298{
299 return ofnode_get_addr_index(node, 0);
300}
301
Simon Glassc4fc5622017-05-18 20:08:58 -0600302int ofnode_stringlist_search(ofnode node, const char *property,
303 const char *string)
304{
305 if (ofnode_is_np(node)) {
306 return of_property_match_string(ofnode_to_np(node),
307 property, string);
308 } else {
309 int ret;
310
311 ret = fdt_stringlist_search(gd->fdt_blob,
312 ofnode_to_offset(node), property,
313 string);
314 if (ret == -FDT_ERR_NOTFOUND)
315 return -ENODATA;
316 else if (ret < 0)
317 return -EINVAL;
318
319 return ret;
320 }
321}
322
323int ofnode_read_string_index(ofnode node, const char *property, int index,
324 const char **outp)
325{
326 if (ofnode_is_np(node)) {
327 return of_property_read_string_index(ofnode_to_np(node),
328 property, index, outp);
329 } else {
330 int len;
331
332 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
333 property, index, &len);
334 if (len < 0)
335 return -EINVAL;
336 return 0;
337 }
338}
339
Simon Glass5fdb0052017-06-12 06:21:28 -0600340int ofnode_read_string_count(ofnode node, const char *property)
341{
342 if (ofnode_is_np(node)) {
343 return of_property_count_strings(ofnode_to_np(node), property);
344 } else {
345 return fdt_stringlist_count(gd->fdt_blob,
346 ofnode_to_offset(node), property);
347 }
348}
349
Simon Glassc4fc5622017-05-18 20:08:58 -0600350static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
351 struct ofnode_phandle_args *out)
352{
353 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
354 out->node = offset_to_ofnode(in->node);
355 out->args_count = in->args_count;
356 memcpy(out->args, in->args, sizeof(out->args));
357}
358
359static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
360 struct ofnode_phandle_args *out)
361{
362 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
363 out->node = np_to_ofnode(in->np);
364 out->args_count = in->args_count;
365 memcpy(out->args, in->args, sizeof(out->args));
366}
367
368int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
369 const char *cells_name, int cell_count,
370 int index,
371 struct ofnode_phandle_args *out_args)
372{
373 if (ofnode_is_np(node)) {
374 struct of_phandle_args args;
375 int ret;
376
377 ret = of_parse_phandle_with_args(ofnode_to_np(node),
Mario Sixf40d82c2018-01-15 11:07:17 +0100378 list_name, cells_name, index,
379 &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600380 if (ret)
381 return ret;
382 ofnode_from_of_phandle_args(&args, out_args);
383 } else {
384 struct fdtdec_phandle_args args;
385 int ret;
386
387 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
Mario Sixf40d82c2018-01-15 11:07:17 +0100388 ofnode_to_offset(node),
389 list_name, cells_name,
390 cell_count, index, &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600391 if (ret)
392 return ret;
393 ofnode_from_fdtdec_phandle_args(&args, out_args);
394 }
395
396 return 0;
397}
398
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200399int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
400 const char *cells_name)
401{
402 if (ofnode_is_np(node))
403 return of_count_phandle_with_args(ofnode_to_np(node),
404 list_name, cells_name);
405 else
406 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
407 ofnode_to_offset(node), list_name, cells_name,
408 0, -1, NULL);
409}
410
Simon Glassc4fc5622017-05-18 20:08:58 -0600411ofnode ofnode_path(const char *path)
412{
413 if (of_live_active())
414 return np_to_ofnode(of_find_node_by_path(path));
415 else
416 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
417}
418
419const char *ofnode_get_chosen_prop(const char *name)
420{
421 ofnode chosen_node;
422
423 chosen_node = ofnode_path("/chosen");
424
425 return ofnode_read_string(chosen_node, name);
426}
427
428ofnode ofnode_get_chosen_node(const char *name)
429{
430 const char *prop;
431
432 prop = ofnode_get_chosen_prop(name);
433 if (!prop)
434 return ofnode_null();
435
436 return ofnode_path(prop);
437}
438
439static int decode_timing_property(ofnode node, const char *name,
440 struct timing_entry *result)
441{
442 int length, ret = 0;
443
444 length = ofnode_read_size(node, name);
445 if (length < 0) {
446 debug("%s: could not find property %s\n",
447 ofnode_get_name(node), name);
448 return length;
449 }
450
451 if (length == sizeof(u32)) {
452 result->typ = ofnode_read_u32_default(node, name, 0);
453 result->min = result->typ;
454 result->max = result->typ;
455 } else {
456 ret = ofnode_read_u32_array(node, name, &result->min, 3);
457 }
458
459 return ret;
460}
461
462int ofnode_decode_display_timing(ofnode parent, int index,
463 struct display_timing *dt)
464{
465 int i;
466 ofnode timings, node;
467 u32 val = 0;
468 int ret = 0;
469
470 timings = ofnode_find_subnode(parent, "display-timings");
471 if (!ofnode_valid(timings))
472 return -EINVAL;
473
Simon Glass28529762017-08-05 15:45:54 -0600474 i = 0;
475 ofnode_for_each_subnode(node, timings) {
476 if (i++ == index)
477 break;
478 }
Simon Glassc4fc5622017-05-18 20:08:58 -0600479
480 if (!ofnode_valid(node))
481 return -EINVAL;
482
483 memset(dt, 0, sizeof(*dt));
484
485 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
486 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
487 ret |= decode_timing_property(node, "hactive", &dt->hactive);
488 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
489 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
490 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
491 ret |= decode_timing_property(node, "vactive", &dt->vactive);
492 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
493 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
494
495 dt->flags = 0;
496 val = ofnode_read_u32_default(node, "vsync-active", -1);
497 if (val != -1) {
498 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
499 DISPLAY_FLAGS_VSYNC_LOW;
500 }
501 val = ofnode_read_u32_default(node, "hsync-active", -1);
502 if (val != -1) {
503 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
504 DISPLAY_FLAGS_HSYNC_LOW;
505 }
506 val = ofnode_read_u32_default(node, "de-active", -1);
507 if (val != -1) {
508 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
509 DISPLAY_FLAGS_DE_LOW;
510 }
511 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
512 if (val != -1) {
513 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
514 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
515 }
516
517 if (ofnode_read_bool(node, "interlaced"))
518 dt->flags |= DISPLAY_FLAGS_INTERLACED;
519 if (ofnode_read_bool(node, "doublescan"))
520 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
521 if (ofnode_read_bool(node, "doubleclk"))
522 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
523
524 return ret;
525}
526
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900527const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
Simon Glassc4fc5622017-05-18 20:08:58 -0600528{
Masahiro Yamada5052f1b2017-06-22 16:54:04 +0900529 if (ofnode_is_np(node))
530 return of_get_property(ofnode_to_np(node), propname, lenp);
531 else
Simon Glassc4fc5622017-05-18 20:08:58 -0600532 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
533 propname, lenp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600534}
535
536bool ofnode_is_available(ofnode node)
537{
538 if (ofnode_is_np(node))
539 return of_device_is_available(ofnode_to_np(node));
540 else
541 return fdtdec_get_is_enabled(gd->fdt_blob,
542 ofnode_to_offset(node));
543}
544
545fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
546 fdt_size_t *sizep)
547{
548 if (ofnode_is_np(node)) {
549 int na, ns;
550 int psize;
551 const struct device_node *np = ofnode_to_np(node);
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200552 const __be32 *prop = of_get_property(np, property, &psize);
Simon Glassc4fc5622017-05-18 20:08:58 -0600553
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200554 if (!prop)
555 return FDT_ADDR_T_NONE;
Simon Glassc4fc5622017-05-18 20:08:58 -0600556 na = of_n_addr_cells(np);
Marek Vasut1638c172018-10-01 12:37:19 +0200557 ns = of_n_size_cells(np);
Simon Glassa67cc632017-05-18 20:09:27 -0600558 *sizep = of_read_number(prop + na, ns);
Marek Vasuta9dac492018-10-01 12:37:20 +0200559
Simon Glass019f9562019-04-25 21:58:36 -0600560 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
Marek Vasuta9dac492018-10-01 12:37:20 +0200561 return of_translate_address(np, prop);
562 else
563 return of_read_number(prop, na);
Simon Glassc4fc5622017-05-18 20:08:58 -0600564 } else {
565 return fdtdec_get_addr_size(gd->fdt_blob,
566 ofnode_to_offset(node), property,
567 sizep);
568 }
569}
570
571const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
572 size_t sz)
573{
574 if (ofnode_is_np(node)) {
575 const struct device_node *np = ofnode_to_np(node);
576 int psize;
577 const __be32 *prop = of_get_property(np, propname, &psize);
578
579 if (!prop || sz != psize)
580 return NULL;
581 return (uint8_t *)prop;
582
583 } else {
584 return fdtdec_locate_byte_array(gd->fdt_blob,
585 ofnode_to_offset(node), propname, sz);
586 }
587}
588
589int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
590 const char *propname, struct fdt_pci_addr *addr)
591{
Masahiro Yamada5c5991e2017-06-22 17:57:50 +0900592 const fdt32_t *cell;
Simon Glassc4fc5622017-05-18 20:08:58 -0600593 int len;
594 int ret = -ENOENT;
595
596 debug("%s: %s: ", __func__, propname);
597
598 /*
599 * If we follow the pci bus bindings strictly, we should check
600 * the value of the node's parent node's #address-cells and
601 * #size-cells. They need to be 3 and 2 accordingly. However,
602 * for simplicity we skip the check here.
603 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900604 cell = ofnode_get_property(node, propname, &len);
Simon Glassc4fc5622017-05-18 20:08:58 -0600605 if (!cell)
606 goto fail;
607
608 if ((len % FDT_PCI_REG_SIZE) == 0) {
609 int num = len / FDT_PCI_REG_SIZE;
610 int i;
611
612 for (i = 0; i < num; i++) {
613 debug("pci address #%d: %08lx %08lx %08lx\n", i,
614 (ulong)fdt32_to_cpu(cell[0]),
615 (ulong)fdt32_to_cpu(cell[1]),
616 (ulong)fdt32_to_cpu(cell[2]));
617 if ((fdt32_to_cpu(*cell) & type) == type) {
618 addr->phys_hi = fdt32_to_cpu(cell[0]);
619 addr->phys_mid = fdt32_to_cpu(cell[1]);
620 addr->phys_lo = fdt32_to_cpu(cell[1]);
621 break;
Simon Glassc4fc5622017-05-18 20:08:58 -0600622 }
Mario Sixf40d82c2018-01-15 11:07:17 +0100623
624 cell += (FDT_PCI_ADDR_CELLS +
625 FDT_PCI_SIZE_CELLS);
Simon Glassc4fc5622017-05-18 20:08:58 -0600626 }
627
628 if (i == num) {
629 ret = -ENXIO;
630 goto fail;
631 }
632
633 return 0;
Simon Glassc4fc5622017-05-18 20:08:58 -0600634 }
635
Mario Sixf40d82c2018-01-15 11:07:17 +0100636 ret = -EINVAL;
637
Simon Glassc4fc5622017-05-18 20:08:58 -0600638fail:
639 debug("(not found)\n");
640 return ret;
641}
642
Bin Mengfa157712018-08-03 01:14:35 -0700643int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
644{
645 const char *list, *end;
646 int len;
647
648 list = ofnode_get_property(node, "compatible", &len);
649 if (!list)
650 return -ENOENT;
651
652 end = list + len;
653 while (list < end) {
654 len = strlen(list);
655 if (len >= strlen("pciVVVV,DDDD")) {
656 char *s = strstr(list, "pci");
657
658 /*
659 * check if the string is something like pciVVVV,DDDD.RR
660 * or just pciVVVV,DDDD
661 */
662 if (s && s[7] == ',' &&
663 (s[12] == '.' || s[12] == 0)) {
664 s += 3;
665 *vendor = simple_strtol(s, NULL, 16);
666
667 s += 5;
668 *device = simple_strtol(s, NULL, 16);
669
670 return 0;
671 }
672 }
673 list += (len + 1);
674 }
675
676 return -ENOENT;
677}
678
Simon Glassc4fc5622017-05-18 20:08:58 -0600679int ofnode_read_addr_cells(ofnode node)
680{
681 if (ofnode_is_np(node))
682 return of_n_addr_cells(ofnode_to_np(node));
Simon Glass4191dc12017-06-12 06:21:31 -0600683 else /* NOTE: this call should walk up the parent stack */
Simon Glassc4fc5622017-05-18 20:08:58 -0600684 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
685}
686
687int ofnode_read_size_cells(ofnode node)
688{
689 if (ofnode_is_np(node))
690 return of_n_size_cells(ofnode_to_np(node));
Simon Glass4191dc12017-06-12 06:21:31 -0600691 else /* NOTE: this call should walk up the parent stack */
692 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
693}
694
695int ofnode_read_simple_addr_cells(ofnode node)
696{
697 if (ofnode_is_np(node))
698 return of_simple_addr_cells(ofnode_to_np(node));
699 else
700 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
701}
702
703int ofnode_read_simple_size_cells(ofnode node)
704{
705 if (ofnode_is_np(node))
706 return of_simple_size_cells(ofnode_to_np(node));
Simon Glassc4fc5622017-05-18 20:08:58 -0600707 else
708 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
709}
710
711bool ofnode_pre_reloc(ofnode node)
712{
Patrick Delaunay0b025b82019-02-11 12:49:57 +0100713#if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
714 /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
715 * had property dm-pre-reloc or u-boot,dm-spl/tpl.
716 * They are removed in final dtb (fdtgrep 2nd pass)
717 */
718 return true;
719#else
Masahiro Yamada6a61dd92017-06-22 16:54:03 +0900720 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
Simon Glassc4fc5622017-05-18 20:08:58 -0600721 return true;
Simon Glass23f22842018-10-01 12:22:18 -0600722 if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
723 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -0600724
Simon Glassc4fc5622017-05-18 20:08:58 -0600725 /*
726 * In regular builds individual spl and tpl handling both
727 * count as handled pre-relocation for later second init.
728 */
Masahiro Yamada6a61dd92017-06-22 16:54:03 +0900729 if (ofnode_read_bool(node, "u-boot,dm-spl") ||
730 ofnode_read_bool(node, "u-boot,dm-tpl"))
Simon Glassc4fc5622017-05-18 20:08:58 -0600731 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -0600732
733 return false;
Patrick Delaunay0b025b82019-02-11 12:49:57 +0100734#endif
Simon Glassc4fc5622017-05-18 20:08:58 -0600735}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600736
737int ofnode_read_resource(ofnode node, uint index, struct resource *res)
738{
739 if (ofnode_is_np(node)) {
740 return of_address_to_resource(ofnode_to_np(node), index, res);
741 } else {
742 struct fdt_resource fres;
743 int ret;
744
745 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
746 "reg", index, &fres);
747 if (ret < 0)
748 return -EINVAL;
749 memset(res, '\0', sizeof(*res));
750 res->start = fres.start;
751 res->end = fres.end;
752
753 return 0;
754 }
755}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900756
757int ofnode_read_resource_byname(ofnode node, const char *name,
758 struct resource *res)
759{
760 int index;
761
762 index = ofnode_stringlist_search(node, "reg-names", name);
763 if (index < 0)
764 return index;
765
766 return ofnode_read_resource(node, index, res);
767}
Mario Sixaefac062018-01-15 11:07:19 +0100768
769u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
770{
771 if (ofnode_is_np(node))
772 return of_translate_address(ofnode_to_np(node), in_addr);
773 else
774 return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
775}
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900776
Fabien Dessenne22236e02019-05-31 15:11:30 +0200777u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
778{
779 if (ofnode_is_np(node))
780 return of_translate_dma_address(ofnode_to_np(node), in_addr);
781 else
782 return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
783}
784
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900785int ofnode_device_is_compatible(ofnode node, const char *compat)
786{
787 if (ofnode_is_np(node))
788 return of_device_is_compatible(ofnode_to_np(node), compat,
789 NULL, NULL);
790 else
791 return !fdt_node_check_compatible(gd->fdt_blob,
792 ofnode_to_offset(node),
793 compat);
794}
Simon Glass954eeae2018-06-11 13:07:13 -0600795
796ofnode ofnode_by_compatible(ofnode from, const char *compat)
797{
798 if (of_live_active()) {
799 return np_to_ofnode(of_find_compatible_node(
800 (struct device_node *)ofnode_to_np(from), NULL,
801 compat));
802 } else {
803 return offset_to_ofnode(fdt_node_offset_by_compatible(
804 gd->fdt_blob, ofnode_to_offset(from), compat));
805 }
806}
Jens Wiklander7b68dad2018-08-20 11:09:58 +0200807
808ofnode ofnode_by_prop_value(ofnode from, const char *propname,
809 const void *propval, int proplen)
810{
811 if (of_live_active()) {
812 return np_to_ofnode(of_find_node_by_prop_value(
813 (struct device_node *)ofnode_to_np(from), propname,
814 propval, proplen));
815 } else {
816 return offset_to_ofnode(fdt_node_offset_by_prop_value(
817 gd->fdt_blob, ofnode_to_offset(from),
818 propname, propval, proplen));
819 }
820}
Mario Six047dafc2018-06-26 08:46:48 +0200821
822int ofnode_write_prop(ofnode node, const char *propname, int len,
823 const void *value)
824{
825 const struct device_node *np = ofnode_to_np(node);
826 struct property *pp;
827 struct property *pp_last = NULL;
828 struct property *new;
829
830 if (!of_live_active())
831 return -ENOSYS;
832
833 if (!np)
834 return -EINVAL;
835
836 for (pp = np->properties; pp; pp = pp->next) {
837 if (strcmp(pp->name, propname) == 0) {
838 /* Property exists -> change value */
839 pp->value = (void *)value;
840 pp->length = len;
841 return 0;
842 }
843 pp_last = pp;
844 }
845
846 if (!pp_last)
847 return -ENOENT;
848
849 /* Property does not exist -> append new property */
850 new = malloc(sizeof(struct property));
851 if (!new)
852 return -ENOMEM;
853
854 new->name = strdup(propname);
Mario Six95e885d2018-10-04 09:22:24 +0200855 if (!new->name) {
856 free(new);
Mario Six047dafc2018-06-26 08:46:48 +0200857 return -ENOMEM;
Mario Six95e885d2018-10-04 09:22:24 +0200858 }
Mario Six047dafc2018-06-26 08:46:48 +0200859
860 new->value = (void *)value;
861 new->length = len;
862 new->next = NULL;
863
864 pp_last->next = new;
865
866 return 0;
867}
868
869int ofnode_write_string(ofnode node, const char *propname, const char *value)
870{
871 if (!of_live_active())
872 return -ENOSYS;
873
874 assert(ofnode_valid(node));
875
876 debug("%s: %s = %s", __func__, propname, value);
877
878 return ofnode_write_prop(node, propname, strlen(value) + 1, value);
879}
880
881int ofnode_set_enabled(ofnode node, bool value)
882{
883 if (!of_live_active())
884 return -ENOSYS;
885
886 assert(ofnode_valid(node));
887
888 if (value)
889 return ofnode_write_string(node, "status", "okay");
890 else
Bin Menga9274aa2019-07-05 09:23:17 -0700891 return ofnode_write_string(node, "status", "disabled");
Mario Six047dafc2018-06-26 08:46:48 +0200892}