dm: core: Add ofnode_read_prop()
[platform/kernel/u-boot.git] / drivers / core / ofnode.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2017 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <fdtdec.h>
10 #include <fdt_support.h>
11 #include <linux/libfdt.h>
12 #include <dm/of_access.h>
13 #include <dm/of_addr.h>
14 #include <dm/ofnode.h>
15 #include <linux/err.h>
16 #include <linux/ioport.h>
17
18 int 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 {
26                 const fdt32_t *cell;
27                 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
42 u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
43 {
44         assert(ofnode_valid(node));
45         ofnode_read_u32(node, propname, &def);
46
47         return def;
48 }
49
50 int 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
58 int ofnode_read_u64(ofnode node, const char *propname, u64 *outp)
59 {
60         const unaligned_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
82 u64 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
90 bool ofnode_read_bool(ofnode node, const char *propname)
91 {
92         const void *prop;
93
94         assert(ofnode_valid(node));
95         debug("%s: %s: ", __func__, propname);
96
97         prop = ofnode_get_property(node, propname, NULL);
98
99         debug("%s\n", prop ? "true" : "false");
100
101         return prop ? true : false;
102 }
103
104 const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep)
105 {
106         const char *val = NULL;
107         int len;
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, &len);
115
116                 if (prop) {
117                         val = prop->value;
118                         len = prop->length;
119                 }
120         } else {
121                 val = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
122                                   propname, &len);
123         }
124         if (!val) {
125                 debug("<not found>\n");
126                 if (sizep)
127                         *sizep = -FDT_ERR_NOTFOUND;
128                 return NULL;
129         }
130         if (sizep)
131                 *sizep = len;
132
133         return val;
134 }
135
136 const 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
145         if (strnlen(str, len) >= len) {
146                 debug("<invalid>\n");
147                 return NULL;
148         }
149         debug("%s\n", str);
150
151         return str;
152 }
153
154 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
155 {
156         ofnode subnode;
157
158         assert(ofnode_valid(node));
159         debug("%s: %s: ", __func__, subnode_name);
160
161         if (ofnode_is_np(node)) {
162                 const struct device_node *np = ofnode_to_np(node);
163
164                 for (np = np->child; np; np = np->sibling) {
165                         if (!strcmp(subnode_name, np->name))
166                                 break;
167                 }
168                 subnode = np_to_ofnode(np);
169         } else {
170                 int ooffset = fdt_subnode_offset(gd->fdt_blob,
171                                 ofnode_to_offset(node), subnode_name);
172                 subnode = offset_to_ofnode(ooffset);
173         }
174         debug("%s\n", ofnode_valid(subnode) ?
175               ofnode_get_name(subnode) : "<none>");
176
177         return subnode;
178 }
179
180 int ofnode_read_u32_array(ofnode node, const char *propname,
181                           u32 *out_values, size_t sz)
182 {
183         assert(ofnode_valid(node));
184         debug("%s: %s: ", __func__, propname);
185
186         if (ofnode_is_np(node)) {
187                 return of_read_u32_array(ofnode_to_np(node), propname,
188                                          out_values, sz);
189         } else {
190                 return fdtdec_get_int_array(gd->fdt_blob,
191                                             ofnode_to_offset(node), propname,
192                                             out_values, sz);
193         }
194 }
195
196 ofnode ofnode_first_subnode(ofnode node)
197 {
198         assert(ofnode_valid(node));
199         if (ofnode_is_np(node))
200                 return np_to_ofnode(node.np->child);
201
202         return offset_to_ofnode(
203                 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
204 }
205
206 ofnode ofnode_next_subnode(ofnode node)
207 {
208         assert(ofnode_valid(node));
209         if (ofnode_is_np(node))
210                 return np_to_ofnode(node.np->sibling);
211
212         return offset_to_ofnode(
213                 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
214 }
215
216 ofnode ofnode_get_parent(ofnode node)
217 {
218         ofnode parent;
219
220         assert(ofnode_valid(node));
221         if (ofnode_is_np(node))
222                 parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
223         else
224                 parent.of_offset = fdt_parent_offset(gd->fdt_blob,
225                                                      ofnode_to_offset(node));
226
227         return parent;
228 }
229
230 const char *ofnode_get_name(ofnode node)
231 {
232         if (!ofnode_valid(node)) {
233                 debug("%s node not valid\n", __func__);
234                 return NULL;
235         }
236
237         if (ofnode_is_np(node))
238                 return strrchr(node.np->full_name, '/') + 1;
239
240         return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
241 }
242
243 ofnode ofnode_get_by_phandle(uint phandle)
244 {
245         ofnode node;
246
247         if (of_live_active())
248                 node = np_to_ofnode(of_find_node_by_phandle(phandle));
249         else
250                 node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
251                                                             phandle);
252
253         return node;
254 }
255
256 int ofnode_read_size(ofnode node, const char *propname)
257 {
258         int len;
259
260         if (ofnode_is_np(node)) {
261                 struct property *prop = of_find_property(
262                                 ofnode_to_np(node), propname, NULL);
263
264                 if (prop)
265                         return prop->length;
266         } else {
267                 if (fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
268                                 &len))
269                         return len;
270         }
271
272         return -EINVAL;
273 }
274
275 fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
276 {
277         int na, ns;
278
279         if (ofnode_is_np(node)) {
280                 const __be32 *prop_val;
281                 u64 size64;
282                 uint flags;
283
284                 prop_val = of_get_address(ofnode_to_np(node), index, &size64,
285                                           &flags);
286                 if (!prop_val)
287                         return FDT_ADDR_T_NONE;
288                 if (size)
289                         *size = size64;
290
291                 ns = of_n_size_cells(ofnode_to_np(node));
292
293                 if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
294                         return of_translate_address(ofnode_to_np(node), prop_val);
295                 } else {
296                         na = of_n_addr_cells(ofnode_to_np(node));
297                         return of_read_number(prop_val, na);
298                 }
299         } else {
300                 na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
301                 ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
302                 return fdtdec_get_addr_size_fixed(gd->fdt_blob,
303                                                   ofnode_to_offset(node), "reg",
304                                                   index, na, ns, size, true);
305         }
306
307         return FDT_ADDR_T_NONE;
308 }
309
310 fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
311 {
312         fdt_size_t size;
313
314         return ofnode_get_addr_size_index(node, index, &size);
315 }
316
317 fdt_addr_t ofnode_get_addr(ofnode node)
318 {
319         return ofnode_get_addr_index(node, 0);
320 }
321
322 int ofnode_stringlist_search(ofnode node, const char *property,
323                              const char *string)
324 {
325         if (ofnode_is_np(node)) {
326                 return of_property_match_string(ofnode_to_np(node),
327                                                 property, string);
328         } else {
329                 int ret;
330
331                 ret = fdt_stringlist_search(gd->fdt_blob,
332                                             ofnode_to_offset(node), property,
333                                             string);
334                 if (ret == -FDT_ERR_NOTFOUND)
335                         return -ENODATA;
336                 else if (ret < 0)
337                         return -EINVAL;
338
339                 return ret;
340         }
341 }
342
343 int ofnode_read_string_index(ofnode node, const char *property, int index,
344                              const char **outp)
345 {
346         if (ofnode_is_np(node)) {
347                 return of_property_read_string_index(ofnode_to_np(node),
348                                                      property, index, outp);
349         } else {
350                 int len;
351
352                 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
353                                            property, index, &len);
354                 if (len < 0)
355                         return -EINVAL;
356                 return 0;
357         }
358 }
359
360 int ofnode_read_string_count(ofnode node, const char *property)
361 {
362         if (ofnode_is_np(node)) {
363                 return of_property_count_strings(ofnode_to_np(node), property);
364         } else {
365                 return fdt_stringlist_count(gd->fdt_blob,
366                                             ofnode_to_offset(node), property);
367         }
368 }
369
370 static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
371                                             struct ofnode_phandle_args *out)
372 {
373         assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
374         out->node = offset_to_ofnode(in->node);
375         out->args_count = in->args_count;
376         memcpy(out->args, in->args, sizeof(out->args));
377 }
378
379 static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
380                                         struct ofnode_phandle_args *out)
381 {
382         assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
383         out->node = np_to_ofnode(in->np);
384         out->args_count = in->args_count;
385         memcpy(out->args, in->args, sizeof(out->args));
386 }
387
388 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
389                                    const char *cells_name, int cell_count,
390                                    int index,
391                                    struct ofnode_phandle_args *out_args)
392 {
393         if (ofnode_is_np(node)) {
394                 struct of_phandle_args args;
395                 int ret;
396
397                 ret = of_parse_phandle_with_args(ofnode_to_np(node),
398                                                  list_name, cells_name, index,
399                                                  &args);
400                 if (ret)
401                         return ret;
402                 ofnode_from_of_phandle_args(&args, out_args);
403         } else {
404                 struct fdtdec_phandle_args args;
405                 int ret;
406
407                 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
408                                                      ofnode_to_offset(node),
409                                                      list_name, cells_name,
410                                                      cell_count, index, &args);
411                 if (ret)
412                         return ret;
413                 ofnode_from_fdtdec_phandle_args(&args, out_args);
414         }
415
416         return 0;
417 }
418
419 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
420                                    const char *cells_name)
421 {
422         if (ofnode_is_np(node))
423                 return of_count_phandle_with_args(ofnode_to_np(node),
424                                 list_name, cells_name);
425         else
426                 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
427                                 ofnode_to_offset(node), list_name, cells_name,
428                                 0, -1, NULL);
429 }
430
431 ofnode ofnode_path(const char *path)
432 {
433         if (of_live_active())
434                 return np_to_ofnode(of_find_node_by_path(path));
435         else
436                 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
437 }
438
439 const char *ofnode_read_chosen_string(const char *name)
440 {
441         ofnode chosen_node;
442
443         chosen_node = ofnode_path("/chosen");
444
445         return ofnode_read_string(chosen_node, name);
446 }
447
448 ofnode ofnode_get_chosen_node(const char *name)
449 {
450         const char *prop;
451
452         prop = ofnode_read_chosen_string(name);
453         if (!prop)
454                 return ofnode_null();
455
456         return ofnode_path(prop);
457 }
458
459 static int decode_timing_property(ofnode node, const char *name,
460                                   struct timing_entry *result)
461 {
462         int length, ret = 0;
463
464         length = ofnode_read_size(node, name);
465         if (length < 0) {
466                 debug("%s: could not find property %s\n",
467                       ofnode_get_name(node), name);
468                 return length;
469         }
470
471         if (length == sizeof(u32)) {
472                 result->typ = ofnode_read_u32_default(node, name, 0);
473                 result->min = result->typ;
474                 result->max = result->typ;
475         } else {
476                 ret = ofnode_read_u32_array(node, name, &result->min, 3);
477         }
478
479         return ret;
480 }
481
482 int ofnode_decode_display_timing(ofnode parent, int index,
483                                  struct display_timing *dt)
484 {
485         int i;
486         ofnode timings, node;
487         u32 val = 0;
488         int ret = 0;
489
490         timings = ofnode_find_subnode(parent, "display-timings");
491         if (!ofnode_valid(timings))
492                 return -EINVAL;
493
494         i = 0;
495         ofnode_for_each_subnode(node, timings) {
496                 if (i++ == index)
497                         break;
498         }
499
500         if (!ofnode_valid(node))
501                 return -EINVAL;
502
503         memset(dt, 0, sizeof(*dt));
504
505         ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
506         ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
507         ret |= decode_timing_property(node, "hactive", &dt->hactive);
508         ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
509         ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
510         ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
511         ret |= decode_timing_property(node, "vactive", &dt->vactive);
512         ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
513         ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
514
515         dt->flags = 0;
516         val = ofnode_read_u32_default(node, "vsync-active", -1);
517         if (val != -1) {
518                 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
519                                 DISPLAY_FLAGS_VSYNC_LOW;
520         }
521         val = ofnode_read_u32_default(node, "hsync-active", -1);
522         if (val != -1) {
523                 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
524                                 DISPLAY_FLAGS_HSYNC_LOW;
525         }
526         val = ofnode_read_u32_default(node, "de-active", -1);
527         if (val != -1) {
528                 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
529                                 DISPLAY_FLAGS_DE_LOW;
530         }
531         val = ofnode_read_u32_default(node, "pixelclk-active", -1);
532         if (val != -1) {
533                 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
534                                 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
535         }
536
537         if (ofnode_read_bool(node, "interlaced"))
538                 dt->flags |= DISPLAY_FLAGS_INTERLACED;
539         if (ofnode_read_bool(node, "doublescan"))
540                 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
541         if (ofnode_read_bool(node, "doubleclk"))
542                 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
543
544         return ret;
545 }
546
547 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
548 {
549         if (ofnode_is_np(node))
550                 return of_get_property(ofnode_to_np(node), propname, lenp);
551         else
552                 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
553                                    propname, lenp);
554 }
555
556 bool ofnode_is_available(ofnode node)
557 {
558         if (ofnode_is_np(node))
559                 return of_device_is_available(ofnode_to_np(node));
560         else
561                 return fdtdec_get_is_enabled(gd->fdt_blob,
562                                              ofnode_to_offset(node));
563 }
564
565 fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
566                                 fdt_size_t *sizep)
567 {
568         if (ofnode_is_np(node)) {
569                 int na, ns;
570                 int psize;
571                 const struct device_node *np = ofnode_to_np(node);
572                 const __be32 *prop = of_get_property(np, property, &psize);
573
574                 if (!prop)
575                         return FDT_ADDR_T_NONE;
576                 na = of_n_addr_cells(np);
577                 ns = of_n_size_cells(np);
578                 *sizep = of_read_number(prop + na, ns);
579
580                 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
581                         return of_translate_address(np, prop);
582                 else
583                         return of_read_number(prop, na);
584         } else {
585                 return fdtdec_get_addr_size(gd->fdt_blob,
586                                             ofnode_to_offset(node), property,
587                                             sizep);
588         }
589 }
590
591 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
592                                         size_t sz)
593 {
594         if (ofnode_is_np(node)) {
595                 const struct device_node *np = ofnode_to_np(node);
596                 int psize;
597                 const __be32 *prop = of_get_property(np, propname, &psize);
598
599                 if (!prop || sz != psize)
600                         return NULL;
601                 return (uint8_t *)prop;
602
603         } else {
604                 return fdtdec_locate_byte_array(gd->fdt_blob,
605                                 ofnode_to_offset(node), propname, sz);
606         }
607 }
608
609 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
610                          const char *propname, struct fdt_pci_addr *addr)
611 {
612         const fdt32_t *cell;
613         int len;
614         int ret = -ENOENT;
615
616         debug("%s: %s: ", __func__, propname);
617
618         /*
619          * If we follow the pci bus bindings strictly, we should check
620          * the value of the node's parent node's #address-cells and
621          * #size-cells. They need to be 3 and 2 accordingly. However,
622          * for simplicity we skip the check here.
623          */
624         cell = ofnode_get_property(node, propname, &len);
625         if (!cell)
626                 goto fail;
627
628         if ((len % FDT_PCI_REG_SIZE) == 0) {
629                 int num = len / FDT_PCI_REG_SIZE;
630                 int i;
631
632                 for (i = 0; i < num; i++) {
633                         debug("pci address #%d: %08lx %08lx %08lx\n", i,
634                               (ulong)fdt32_to_cpu(cell[0]),
635                               (ulong)fdt32_to_cpu(cell[1]),
636                               (ulong)fdt32_to_cpu(cell[2]));
637                         if ((fdt32_to_cpu(*cell) & type) == type) {
638                                 addr->phys_hi = fdt32_to_cpu(cell[0]);
639                                 addr->phys_mid = fdt32_to_cpu(cell[1]);
640                                 addr->phys_lo = fdt32_to_cpu(cell[2]);
641                                 break;
642                         }
643
644                         cell += (FDT_PCI_ADDR_CELLS +
645                                  FDT_PCI_SIZE_CELLS);
646                 }
647
648                 if (i == num) {
649                         ret = -ENXIO;
650                         goto fail;
651                 }
652
653                 return 0;
654         }
655
656         ret = -EINVAL;
657
658 fail:
659         debug("(not found)\n");
660         return ret;
661 }
662
663 int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
664 {
665         const char *list, *end;
666         int len;
667
668         list = ofnode_get_property(node, "compatible", &len);
669         if (!list)
670                 return -ENOENT;
671
672         end = list + len;
673         while (list < end) {
674                 len = strlen(list);
675                 if (len >= strlen("pciVVVV,DDDD")) {
676                         char *s = strstr(list, "pci");
677
678                         /*
679                          * check if the string is something like pciVVVV,DDDD.RR
680                          * or just pciVVVV,DDDD
681                          */
682                         if (s && s[7] == ',' &&
683                             (s[12] == '.' || s[12] == 0)) {
684                                 s += 3;
685                                 *vendor = simple_strtol(s, NULL, 16);
686
687                                 s += 5;
688                                 *device = simple_strtol(s, NULL, 16);
689
690                                 return 0;
691                         }
692                 }
693                 list += (len + 1);
694         }
695
696         return -ENOENT;
697 }
698
699 int ofnode_read_addr_cells(ofnode node)
700 {
701         if (ofnode_is_np(node))
702                 return of_n_addr_cells(ofnode_to_np(node));
703         else  /* NOTE: this call should walk up the parent stack */
704                 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
705 }
706
707 int ofnode_read_size_cells(ofnode node)
708 {
709         if (ofnode_is_np(node))
710                 return of_n_size_cells(ofnode_to_np(node));
711         else  /* NOTE: this call should walk up the parent stack */
712                 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
713 }
714
715 int ofnode_read_simple_addr_cells(ofnode node)
716 {
717         if (ofnode_is_np(node))
718                 return of_simple_addr_cells(ofnode_to_np(node));
719         else
720                 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
721 }
722
723 int ofnode_read_simple_size_cells(ofnode node)
724 {
725         if (ofnode_is_np(node))
726                 return of_simple_size_cells(ofnode_to_np(node));
727         else
728                 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
729 }
730
731 bool ofnode_pre_reloc(ofnode node)
732 {
733 #if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
734         /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
735          * had property dm-pre-reloc or u-boot,dm-spl/tpl.
736          * They are removed in final dtb (fdtgrep 2nd pass)
737          */
738         return true;
739 #else
740         if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
741                 return true;
742         if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
743                 return true;
744
745         /*
746          * In regular builds individual spl and tpl handling both
747          * count as handled pre-relocation for later second init.
748          */
749         if (ofnode_read_bool(node, "u-boot,dm-spl") ||
750             ofnode_read_bool(node, "u-boot,dm-tpl"))
751                 return true;
752
753         return false;
754 #endif
755 }
756
757 int ofnode_read_resource(ofnode node, uint index, struct resource *res)
758 {
759         if (ofnode_is_np(node)) {
760                 return of_address_to_resource(ofnode_to_np(node), index, res);
761         } else {
762                 struct fdt_resource fres;
763                 int ret;
764
765                 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
766                                        "reg", index, &fres);
767                 if (ret < 0)
768                         return -EINVAL;
769                 memset(res, '\0', sizeof(*res));
770                 res->start = fres.start;
771                 res->end = fres.end;
772
773                 return 0;
774         }
775 }
776
777 int ofnode_read_resource_byname(ofnode node, const char *name,
778                                 struct resource *res)
779 {
780         int index;
781
782         index = ofnode_stringlist_search(node, "reg-names", name);
783         if (index < 0)
784                 return index;
785
786         return ofnode_read_resource(node, index, res);
787 }
788
789 u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
790 {
791         if (ofnode_is_np(node))
792                 return of_translate_address(ofnode_to_np(node), in_addr);
793         else
794                 return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
795 }
796
797 u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
798 {
799         if (ofnode_is_np(node))
800                 return of_translate_dma_address(ofnode_to_np(node), in_addr);
801         else
802                 return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
803 }
804
805 int ofnode_device_is_compatible(ofnode node, const char *compat)
806 {
807         if (ofnode_is_np(node))
808                 return of_device_is_compatible(ofnode_to_np(node), compat,
809                                                NULL, NULL);
810         else
811                 return !fdt_node_check_compatible(gd->fdt_blob,
812                                                   ofnode_to_offset(node),
813                                                   compat);
814 }
815
816 ofnode ofnode_by_compatible(ofnode from, const char *compat)
817 {
818         if (of_live_active()) {
819                 return np_to_ofnode(of_find_compatible_node(
820                         (struct device_node *)ofnode_to_np(from), NULL,
821                         compat));
822         } else {
823                 return offset_to_ofnode(fdt_node_offset_by_compatible(
824                                 gd->fdt_blob, ofnode_to_offset(from), compat));
825         }
826 }
827
828 ofnode ofnode_by_prop_value(ofnode from, const char *propname,
829                             const void *propval, int proplen)
830 {
831         if (of_live_active()) {
832                 return np_to_ofnode(of_find_node_by_prop_value(
833                         (struct device_node *)ofnode_to_np(from), propname,
834                         propval, proplen));
835         } else {
836                 return offset_to_ofnode(fdt_node_offset_by_prop_value(
837                                 gd->fdt_blob, ofnode_to_offset(from),
838                                 propname, propval, proplen));
839         }
840 }
841
842 int ofnode_write_prop(ofnode node, const char *propname, int len,
843                       const void *value)
844 {
845         const struct device_node *np = ofnode_to_np(node);
846         struct property *pp;
847         struct property *pp_last = NULL;
848         struct property *new;
849
850         if (!of_live_active())
851                 return -ENOSYS;
852
853         if (!np)
854                 return -EINVAL;
855
856         for (pp = np->properties; pp; pp = pp->next) {
857                 if (strcmp(pp->name, propname) == 0) {
858                         /* Property exists -> change value */
859                         pp->value = (void *)value;
860                         pp->length = len;
861                         return 0;
862                 }
863                 pp_last = pp;
864         }
865
866         if (!pp_last)
867                 return -ENOENT;
868
869         /* Property does not exist -> append new property */
870         new = malloc(sizeof(struct property));
871         if (!new)
872                 return -ENOMEM;
873
874         new->name = strdup(propname);
875         if (!new->name) {
876                 free(new);
877                 return -ENOMEM;
878         }
879
880         new->value = (void *)value;
881         new->length = len;
882         new->next = NULL;
883
884         pp_last->next = new;
885
886         return 0;
887 }
888
889 int ofnode_write_string(ofnode node, const char *propname, const char *value)
890 {
891         if (!of_live_active())
892                 return -ENOSYS;
893
894         assert(ofnode_valid(node));
895
896         debug("%s: %s = %s", __func__, propname, value);
897
898         return ofnode_write_prop(node, propname, strlen(value) + 1, value);
899 }
900
901 int ofnode_set_enabled(ofnode node, bool value)
902 {
903         if (!of_live_active())
904                 return -ENOSYS;
905
906         assert(ofnode_valid(node));
907
908         if (value)
909                 return ofnode_write_string(node, "status", "okay");
910         else
911                 return ofnode_write_string(node, "status", "disabled");
912 }