blob: c72c6e26737b610b52eaedd5e6d4fbc3ecdc9035 [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
82int ofnode_read_u64_default(ofnode node, const char *propname, u64 def)
83{
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{
215 assert(ofnode_valid(node));
216 if (ofnode_is_np(node))
217 return strrchr(node.np->full_name, '/') + 1;
218
219 return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
220}
221
Kever Yang37df0e02018-02-23 17:38:50 +0100222ofnode ofnode_get_by_phandle(uint phandle)
223{
224 ofnode node;
225
226 if (of_live_active())
227 node = np_to_ofnode(of_find_node_by_phandle(phandle));
228 else
229 node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
230 phandle);
231
232 return node;
233}
234
Simon Glassc4fc5622017-05-18 20:08:58 -0600235int ofnode_read_size(ofnode node, const char *propname)
236{
237 int len;
238
239 if (ofnode_is_np(node)) {
240 struct property *prop = of_find_property(
241 ofnode_to_np(node), propname, NULL);
242
243 if (prop)
244 return prop->length;
245 } else {
246 if (fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
247 &len))
248 return len;
249 }
250
251 return -EINVAL;
252}
253
Keerthyd332e6e2019-04-24 17:19:53 +0530254fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
Simon Glass049ae1b2017-05-18 20:09:01 -0600255{
Keerthy34222a32018-11-19 11:44:47 +0530256 int na, ns;
Keerthy34222a32018-11-19 11:44:47 +0530257
Simon Glass049ae1b2017-05-18 20:09:01 -0600258 if (ofnode_is_np(node)) {
259 const __be32 *prop_val;
260 uint flags;
Simon Glass049ae1b2017-05-18 20:09:01 -0600261
Keerthy34222a32018-11-19 11:44:47 +0530262 prop_val = of_get_address(ofnode_to_np(node), index,
Keerthyd332e6e2019-04-24 17:19:53 +0530263 (u64 *)size, &flags);
Simon Glass049ae1b2017-05-18 20:09:01 -0600264 if (!prop_val)
265 return FDT_ADDR_T_NONE;
Mario Sixd007ebc2017-12-20 09:52:12 +0100266
Mario Six35616ef2018-03-12 14:53:33 +0100267 ns = of_n_size_cells(ofnode_to_np(node));
268
269 if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
Mario Sixd007ebc2017-12-20 09:52:12 +0100270 return of_translate_address(ofnode_to_np(node), prop_val);
271 } else {
272 na = of_n_addr_cells(ofnode_to_np(node));
273 return of_read_number(prop_val, na);
274 }
Simon Glass049ae1b2017-05-18 20:09:01 -0600275 } else {
Keerthy34222a32018-11-19 11:44:47 +0530276 na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
277 ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
278 return fdtdec_get_addr_size_fixed(gd->fdt_blob,
279 ofnode_to_offset(node), "reg",
Keerthyd332e6e2019-04-24 17:19:53 +0530280 index, na, ns, size, true);
Simon Glass049ae1b2017-05-18 20:09:01 -0600281 }
282
283 return FDT_ADDR_T_NONE;
284}
285
Keerthyd332e6e2019-04-24 17:19:53 +0530286fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
287{
288 fdt_size_t size;
289
290 return ofnode_get_addr_size_index(node, index, &size);
291}
292
Simon Glass049ae1b2017-05-18 20:09:01 -0600293fdt_addr_t ofnode_get_addr(ofnode node)
294{
295 return ofnode_get_addr_index(node, 0);
296}
297
Simon Glassc4fc5622017-05-18 20:08:58 -0600298int ofnode_stringlist_search(ofnode node, const char *property,
299 const char *string)
300{
301 if (ofnode_is_np(node)) {
302 return of_property_match_string(ofnode_to_np(node),
303 property, string);
304 } else {
305 int ret;
306
307 ret = fdt_stringlist_search(gd->fdt_blob,
308 ofnode_to_offset(node), property,
309 string);
310 if (ret == -FDT_ERR_NOTFOUND)
311 return -ENODATA;
312 else if (ret < 0)
313 return -EINVAL;
314
315 return ret;
316 }
317}
318
319int ofnode_read_string_index(ofnode node, const char *property, int index,
320 const char **outp)
321{
322 if (ofnode_is_np(node)) {
323 return of_property_read_string_index(ofnode_to_np(node),
324 property, index, outp);
325 } else {
326 int len;
327
328 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
329 property, index, &len);
330 if (len < 0)
331 return -EINVAL;
332 return 0;
333 }
334}
335
Simon Glass5fdb0052017-06-12 06:21:28 -0600336int ofnode_read_string_count(ofnode node, const char *property)
337{
338 if (ofnode_is_np(node)) {
339 return of_property_count_strings(ofnode_to_np(node), property);
340 } else {
341 return fdt_stringlist_count(gd->fdt_blob,
342 ofnode_to_offset(node), property);
343 }
344}
345
Simon Glassc4fc5622017-05-18 20:08:58 -0600346static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
347 struct ofnode_phandle_args *out)
348{
349 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
350 out->node = offset_to_ofnode(in->node);
351 out->args_count = in->args_count;
352 memcpy(out->args, in->args, sizeof(out->args));
353}
354
355static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
356 struct ofnode_phandle_args *out)
357{
358 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
359 out->node = np_to_ofnode(in->np);
360 out->args_count = in->args_count;
361 memcpy(out->args, in->args, sizeof(out->args));
362}
363
364int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
365 const char *cells_name, int cell_count,
366 int index,
367 struct ofnode_phandle_args *out_args)
368{
369 if (ofnode_is_np(node)) {
370 struct of_phandle_args args;
371 int ret;
372
373 ret = of_parse_phandle_with_args(ofnode_to_np(node),
Mario Sixf40d82c2018-01-15 11:07:17 +0100374 list_name, cells_name, index,
375 &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600376 if (ret)
377 return ret;
378 ofnode_from_of_phandle_args(&args, out_args);
379 } else {
380 struct fdtdec_phandle_args args;
381 int ret;
382
383 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
Mario Sixf40d82c2018-01-15 11:07:17 +0100384 ofnode_to_offset(node),
385 list_name, cells_name,
386 cell_count, index, &args);
Simon Glassc4fc5622017-05-18 20:08:58 -0600387 if (ret)
388 return ret;
389 ofnode_from_fdtdec_phandle_args(&args, out_args);
390 }
391
392 return 0;
393}
394
Patrice Chotardbe7dd602017-07-18 11:57:08 +0200395int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
396 const char *cells_name)
397{
398 if (ofnode_is_np(node))
399 return of_count_phandle_with_args(ofnode_to_np(node),
400 list_name, cells_name);
401 else
402 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
403 ofnode_to_offset(node), list_name, cells_name,
404 0, -1, NULL);
405}
406
Simon Glassc4fc5622017-05-18 20:08:58 -0600407ofnode ofnode_path(const char *path)
408{
409 if (of_live_active())
410 return np_to_ofnode(of_find_node_by_path(path));
411 else
412 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
413}
414
415const char *ofnode_get_chosen_prop(const char *name)
416{
417 ofnode chosen_node;
418
419 chosen_node = ofnode_path("/chosen");
420
421 return ofnode_read_string(chosen_node, name);
422}
423
424ofnode ofnode_get_chosen_node(const char *name)
425{
426 const char *prop;
427
428 prop = ofnode_get_chosen_prop(name);
429 if (!prop)
430 return ofnode_null();
431
432 return ofnode_path(prop);
433}
434
435static int decode_timing_property(ofnode node, const char *name,
436 struct timing_entry *result)
437{
438 int length, ret = 0;
439
440 length = ofnode_read_size(node, name);
441 if (length < 0) {
442 debug("%s: could not find property %s\n",
443 ofnode_get_name(node), name);
444 return length;
445 }
446
447 if (length == sizeof(u32)) {
448 result->typ = ofnode_read_u32_default(node, name, 0);
449 result->min = result->typ;
450 result->max = result->typ;
451 } else {
452 ret = ofnode_read_u32_array(node, name, &result->min, 3);
453 }
454
455 return ret;
456}
457
458int ofnode_decode_display_timing(ofnode parent, int index,
459 struct display_timing *dt)
460{
461 int i;
462 ofnode timings, node;
463 u32 val = 0;
464 int ret = 0;
465
466 timings = ofnode_find_subnode(parent, "display-timings");
467 if (!ofnode_valid(timings))
468 return -EINVAL;
469
Simon Glass28529762017-08-05 15:45:54 -0600470 i = 0;
471 ofnode_for_each_subnode(node, timings) {
472 if (i++ == index)
473 break;
474 }
Simon Glassc4fc5622017-05-18 20:08:58 -0600475
476 if (!ofnode_valid(node))
477 return -EINVAL;
478
479 memset(dt, 0, sizeof(*dt));
480
481 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
482 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
483 ret |= decode_timing_property(node, "hactive", &dt->hactive);
484 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
485 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
486 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
487 ret |= decode_timing_property(node, "vactive", &dt->vactive);
488 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
489 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
490
491 dt->flags = 0;
492 val = ofnode_read_u32_default(node, "vsync-active", -1);
493 if (val != -1) {
494 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
495 DISPLAY_FLAGS_VSYNC_LOW;
496 }
497 val = ofnode_read_u32_default(node, "hsync-active", -1);
498 if (val != -1) {
499 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
500 DISPLAY_FLAGS_HSYNC_LOW;
501 }
502 val = ofnode_read_u32_default(node, "de-active", -1);
503 if (val != -1) {
504 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
505 DISPLAY_FLAGS_DE_LOW;
506 }
507 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
508 if (val != -1) {
509 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
510 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
511 }
512
513 if (ofnode_read_bool(node, "interlaced"))
514 dt->flags |= DISPLAY_FLAGS_INTERLACED;
515 if (ofnode_read_bool(node, "doublescan"))
516 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
517 if (ofnode_read_bool(node, "doubleclk"))
518 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
519
520 return ret;
521}
522
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900523const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
Simon Glassc4fc5622017-05-18 20:08:58 -0600524{
Masahiro Yamada5052f1b2017-06-22 16:54:04 +0900525 if (ofnode_is_np(node))
526 return of_get_property(ofnode_to_np(node), propname, lenp);
527 else
Simon Glassc4fc5622017-05-18 20:08:58 -0600528 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
529 propname, lenp);
Simon Glassc4fc5622017-05-18 20:08:58 -0600530}
531
532bool ofnode_is_available(ofnode node)
533{
534 if (ofnode_is_np(node))
535 return of_device_is_available(ofnode_to_np(node));
536 else
537 return fdtdec_get_is_enabled(gd->fdt_blob,
538 ofnode_to_offset(node));
539}
540
541fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
542 fdt_size_t *sizep)
543{
544 if (ofnode_is_np(node)) {
545 int na, ns;
546 int psize;
547 const struct device_node *np = ofnode_to_np(node);
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200548 const __be32 *prop = of_get_property(np, property, &psize);
Simon Glassc4fc5622017-05-18 20:08:58 -0600549
Klaus Gogeraf4b0212017-09-20 13:50:41 +0200550 if (!prop)
551 return FDT_ADDR_T_NONE;
Simon Glassc4fc5622017-05-18 20:08:58 -0600552 na = of_n_addr_cells(np);
Marek Vasut1638c172018-10-01 12:37:19 +0200553 ns = of_n_size_cells(np);
Simon Glassa67cc632017-05-18 20:09:27 -0600554 *sizep = of_read_number(prop + na, ns);
Marek Vasuta9dac492018-10-01 12:37:20 +0200555
Simon Glass019f9562019-04-25 21:58:36 -0600556 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
Marek Vasuta9dac492018-10-01 12:37:20 +0200557 return of_translate_address(np, prop);
558 else
559 return of_read_number(prop, na);
Simon Glassc4fc5622017-05-18 20:08:58 -0600560 } else {
561 return fdtdec_get_addr_size(gd->fdt_blob,
562 ofnode_to_offset(node), property,
563 sizep);
564 }
565}
566
567const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
568 size_t sz)
569{
570 if (ofnode_is_np(node)) {
571 const struct device_node *np = ofnode_to_np(node);
572 int psize;
573 const __be32 *prop = of_get_property(np, propname, &psize);
574
575 if (!prop || sz != psize)
576 return NULL;
577 return (uint8_t *)prop;
578
579 } else {
580 return fdtdec_locate_byte_array(gd->fdt_blob,
581 ofnode_to_offset(node), propname, sz);
582 }
583}
584
585int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
586 const char *propname, struct fdt_pci_addr *addr)
587{
Masahiro Yamada5c5991e2017-06-22 17:57:50 +0900588 const fdt32_t *cell;
Simon Glassc4fc5622017-05-18 20:08:58 -0600589 int len;
590 int ret = -ENOENT;
591
592 debug("%s: %s: ", __func__, propname);
593
594 /*
595 * If we follow the pci bus bindings strictly, we should check
596 * the value of the node's parent node's #address-cells and
597 * #size-cells. They need to be 3 and 2 accordingly. However,
598 * for simplicity we skip the check here.
599 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900600 cell = ofnode_get_property(node, propname, &len);
Simon Glassc4fc5622017-05-18 20:08:58 -0600601 if (!cell)
602 goto fail;
603
604 if ((len % FDT_PCI_REG_SIZE) == 0) {
605 int num = len / FDT_PCI_REG_SIZE;
606 int i;
607
608 for (i = 0; i < num; i++) {
609 debug("pci address #%d: %08lx %08lx %08lx\n", i,
610 (ulong)fdt32_to_cpu(cell[0]),
611 (ulong)fdt32_to_cpu(cell[1]),
612 (ulong)fdt32_to_cpu(cell[2]));
613 if ((fdt32_to_cpu(*cell) & type) == type) {
614 addr->phys_hi = fdt32_to_cpu(cell[0]);
615 addr->phys_mid = fdt32_to_cpu(cell[1]);
616 addr->phys_lo = fdt32_to_cpu(cell[1]);
617 break;
Simon Glassc4fc5622017-05-18 20:08:58 -0600618 }
Mario Sixf40d82c2018-01-15 11:07:17 +0100619
620 cell += (FDT_PCI_ADDR_CELLS +
621 FDT_PCI_SIZE_CELLS);
Simon Glassc4fc5622017-05-18 20:08:58 -0600622 }
623
624 if (i == num) {
625 ret = -ENXIO;
626 goto fail;
627 }
628
629 return 0;
Simon Glassc4fc5622017-05-18 20:08:58 -0600630 }
631
Mario Sixf40d82c2018-01-15 11:07:17 +0100632 ret = -EINVAL;
633
Simon Glassc4fc5622017-05-18 20:08:58 -0600634fail:
635 debug("(not found)\n");
636 return ret;
637}
638
Bin Mengfa157712018-08-03 01:14:35 -0700639int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
640{
641 const char *list, *end;
642 int len;
643
644 list = ofnode_get_property(node, "compatible", &len);
645 if (!list)
646 return -ENOENT;
647
648 end = list + len;
649 while (list < end) {
650 len = strlen(list);
651 if (len >= strlen("pciVVVV,DDDD")) {
652 char *s = strstr(list, "pci");
653
654 /*
655 * check if the string is something like pciVVVV,DDDD.RR
656 * or just pciVVVV,DDDD
657 */
658 if (s && s[7] == ',' &&
659 (s[12] == '.' || s[12] == 0)) {
660 s += 3;
661 *vendor = simple_strtol(s, NULL, 16);
662
663 s += 5;
664 *device = simple_strtol(s, NULL, 16);
665
666 return 0;
667 }
668 }
669 list += (len + 1);
670 }
671
672 return -ENOENT;
673}
674
Simon Glassc4fc5622017-05-18 20:08:58 -0600675int ofnode_read_addr_cells(ofnode node)
676{
677 if (ofnode_is_np(node))
678 return of_n_addr_cells(ofnode_to_np(node));
Simon Glass4191dc12017-06-12 06:21:31 -0600679 else /* NOTE: this call should walk up the parent stack */
Simon Glassc4fc5622017-05-18 20:08:58 -0600680 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
681}
682
683int ofnode_read_size_cells(ofnode node)
684{
685 if (ofnode_is_np(node))
686 return of_n_size_cells(ofnode_to_np(node));
Simon Glass4191dc12017-06-12 06:21:31 -0600687 else /* NOTE: this call should walk up the parent stack */
688 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
689}
690
691int ofnode_read_simple_addr_cells(ofnode node)
692{
693 if (ofnode_is_np(node))
694 return of_simple_addr_cells(ofnode_to_np(node));
695 else
696 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
697}
698
699int ofnode_read_simple_size_cells(ofnode node)
700{
701 if (ofnode_is_np(node))
702 return of_simple_size_cells(ofnode_to_np(node));
Simon Glassc4fc5622017-05-18 20:08:58 -0600703 else
704 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
705}
706
707bool ofnode_pre_reloc(ofnode node)
708{
Patrick Delaunay0b025b82019-02-11 12:49:57 +0100709#if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
710 /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
711 * had property dm-pre-reloc or u-boot,dm-spl/tpl.
712 * They are removed in final dtb (fdtgrep 2nd pass)
713 */
714 return true;
715#else
Masahiro Yamada6a61dd92017-06-22 16:54:03 +0900716 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
Simon Glassc4fc5622017-05-18 20:08:58 -0600717 return true;
Simon Glass23f22842018-10-01 12:22:18 -0600718 if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
719 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -0600720
Simon Glassc4fc5622017-05-18 20:08:58 -0600721 /*
722 * In regular builds individual spl and tpl handling both
723 * count as handled pre-relocation for later second init.
724 */
Masahiro Yamada6a61dd92017-06-22 16:54:03 +0900725 if (ofnode_read_bool(node, "u-boot,dm-spl") ||
726 ofnode_read_bool(node, "u-boot,dm-tpl"))
Simon Glassc4fc5622017-05-18 20:08:58 -0600727 return true;
Simon Glassc4fc5622017-05-18 20:08:58 -0600728
729 return false;
Patrick Delaunay0b025b82019-02-11 12:49:57 +0100730#endif
Simon Glassc4fc5622017-05-18 20:08:58 -0600731}
Simon Glassf7bfcc42017-07-25 08:29:55 -0600732
733int ofnode_read_resource(ofnode node, uint index, struct resource *res)
734{
735 if (ofnode_is_np(node)) {
736 return of_address_to_resource(ofnode_to_np(node), index, res);
737 } else {
738 struct fdt_resource fres;
739 int ret;
740
741 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
742 "reg", index, &fres);
743 if (ret < 0)
744 return -EINVAL;
745 memset(res, '\0', sizeof(*res));
746 res->start = fres.start;
747 res->end = fres.end;
748
749 return 0;
750 }
751}
Masahiro Yamada4dada2c2017-08-26 01:12:30 +0900752
753int ofnode_read_resource_byname(ofnode node, const char *name,
754 struct resource *res)
755{
756 int index;
757
758 index = ofnode_stringlist_search(node, "reg-names", name);
759 if (index < 0)
760 return index;
761
762 return ofnode_read_resource(node, index, res);
763}
Mario Sixaefac062018-01-15 11:07:19 +0100764
765u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
766{
767 if (ofnode_is_np(node))
768 return of_translate_address(ofnode_to_np(node), in_addr);
769 else
770 return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
771}
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900772
773int ofnode_device_is_compatible(ofnode node, const char *compat)
774{
775 if (ofnode_is_np(node))
776 return of_device_is_compatible(ofnode_to_np(node), compat,
777 NULL, NULL);
778 else
779 return !fdt_node_check_compatible(gd->fdt_blob,
780 ofnode_to_offset(node),
781 compat);
782}
Simon Glass954eeae2018-06-11 13:07:13 -0600783
784ofnode ofnode_by_compatible(ofnode from, const char *compat)
785{
786 if (of_live_active()) {
787 return np_to_ofnode(of_find_compatible_node(
788 (struct device_node *)ofnode_to_np(from), NULL,
789 compat));
790 } else {
791 return offset_to_ofnode(fdt_node_offset_by_compatible(
792 gd->fdt_blob, ofnode_to_offset(from), compat));
793 }
794}
Jens Wiklander7b68dad2018-08-20 11:09:58 +0200795
796ofnode ofnode_by_prop_value(ofnode from, const char *propname,
797 const void *propval, int proplen)
798{
799 if (of_live_active()) {
800 return np_to_ofnode(of_find_node_by_prop_value(
801 (struct device_node *)ofnode_to_np(from), propname,
802 propval, proplen));
803 } else {
804 return offset_to_ofnode(fdt_node_offset_by_prop_value(
805 gd->fdt_blob, ofnode_to_offset(from),
806 propname, propval, proplen));
807 }
808}
Mario Six047dafc2018-06-26 08:46:48 +0200809
810int ofnode_write_prop(ofnode node, const char *propname, int len,
811 const void *value)
812{
813 const struct device_node *np = ofnode_to_np(node);
814 struct property *pp;
815 struct property *pp_last = NULL;
816 struct property *new;
817
818 if (!of_live_active())
819 return -ENOSYS;
820
821 if (!np)
822 return -EINVAL;
823
824 for (pp = np->properties; pp; pp = pp->next) {
825 if (strcmp(pp->name, propname) == 0) {
826 /* Property exists -> change value */
827 pp->value = (void *)value;
828 pp->length = len;
829 return 0;
830 }
831 pp_last = pp;
832 }
833
834 if (!pp_last)
835 return -ENOENT;
836
837 /* Property does not exist -> append new property */
838 new = malloc(sizeof(struct property));
839 if (!new)
840 return -ENOMEM;
841
842 new->name = strdup(propname);
Mario Six95e885d2018-10-04 09:22:24 +0200843 if (!new->name) {
844 free(new);
Mario Six047dafc2018-06-26 08:46:48 +0200845 return -ENOMEM;
Mario Six95e885d2018-10-04 09:22:24 +0200846 }
Mario Six047dafc2018-06-26 08:46:48 +0200847
848 new->value = (void *)value;
849 new->length = len;
850 new->next = NULL;
851
852 pp_last->next = new;
853
854 return 0;
855}
856
857int ofnode_write_string(ofnode node, const char *propname, const char *value)
858{
859 if (!of_live_active())
860 return -ENOSYS;
861
862 assert(ofnode_valid(node));
863
864 debug("%s: %s = %s", __func__, propname, value);
865
866 return ofnode_write_prop(node, propname, strlen(value) + 1, value);
867}
868
869int ofnode_set_enabled(ofnode node, bool value)
870{
871 if (!of_live_active())
872 return -ENOSYS;
873
874 assert(ofnode_valid(node));
875
876 if (value)
877 return ofnode_write_string(node, "status", "okay");
878 else
879 return ofnode_write_string(node, "status", "disable");
880}