core: ofnode: Have ofnode_read_u32_default return a u32
[oweals/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 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 int 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 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
137 ofnode 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
163 int 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
179 ofnode 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
189 ofnode 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
199 ofnode 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
213 const 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
222 ofnode 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
235 int 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
254 fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
255 {
256         int na, ns;
257
258         if (ofnode_is_np(node)) {
259                 const __be32 *prop_val;
260                 uint flags;
261
262                 prop_val = of_get_address(ofnode_to_np(node), index,
263                                           (u64 *)size, &flags);
264                 if (!prop_val)
265                         return FDT_ADDR_T_NONE;
266
267                 ns = of_n_size_cells(ofnode_to_np(node));
268
269                 if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
270                         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                 }
275         } else {
276                 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",
280                                                   index, na, ns, size, true);
281         }
282
283         return FDT_ADDR_T_NONE;
284 }
285
286 fdt_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
293 fdt_addr_t ofnode_get_addr(ofnode node)
294 {
295         return ofnode_get_addr_index(node, 0);
296 }
297
298 int 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
319 int 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
336 int 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
346 static 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
355 static 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
364 int 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),
374                                                  list_name, cells_name, index,
375                                                  &args);
376                 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,
384                                                      ofnode_to_offset(node),
385                                                      list_name, cells_name,
386                                                      cell_count, index, &args);
387                 if (ret)
388                         return ret;
389                 ofnode_from_fdtdec_phandle_args(&args, out_args);
390         }
391
392         return 0;
393 }
394
395 int 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
407 ofnode 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
415 const 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
424 ofnode 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
435 static 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
458 int 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
470         i = 0;
471         ofnode_for_each_subnode(node, timings) {
472                 if (i++ == index)
473                         break;
474         }
475
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
523 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
524 {
525         if (ofnode_is_np(node))
526                 return of_get_property(ofnode_to_np(node), propname, lenp);
527         else
528                 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
529                                    propname, lenp);
530 }
531
532 bool 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
541 fdt_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);
548                 const __be32 *prop = of_get_property(np, property, &psize);
549
550                 if (!prop)
551                         return FDT_ADDR_T_NONE;
552                 na = of_n_addr_cells(np);
553                 ns = of_n_size_cells(np);
554                 *sizep = of_read_number(prop + na, ns);
555
556                 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
557                         return of_translate_address(np, prop);
558                 else
559                         return of_read_number(prop, na);
560         } else {
561                 return fdtdec_get_addr_size(gd->fdt_blob,
562                                             ofnode_to_offset(node), property,
563                                             sizep);
564         }
565 }
566
567 const 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
585 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
586                          const char *propname, struct fdt_pci_addr *addr)
587 {
588         const fdt32_t *cell;
589         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          */
600         cell = ofnode_get_property(node, propname, &len);
601         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;
618                         }
619
620                         cell += (FDT_PCI_ADDR_CELLS +
621                                  FDT_PCI_SIZE_CELLS);
622                 }
623
624                 if (i == num) {
625                         ret = -ENXIO;
626                         goto fail;
627                 }
628
629                 return 0;
630         }
631
632         ret = -EINVAL;
633
634 fail:
635         debug("(not found)\n");
636         return ret;
637 }
638
639 int 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
675 int ofnode_read_addr_cells(ofnode node)
676 {
677         if (ofnode_is_np(node))
678                 return of_n_addr_cells(ofnode_to_np(node));
679         else  /* NOTE: this call should walk up the parent stack */
680                 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
681 }
682
683 int ofnode_read_size_cells(ofnode node)
684 {
685         if (ofnode_is_np(node))
686                 return of_n_size_cells(ofnode_to_np(node));
687         else  /* NOTE: this call should walk up the parent stack */
688                 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
689 }
690
691 int 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
699 int ofnode_read_simple_size_cells(ofnode node)
700 {
701         if (ofnode_is_np(node))
702                 return of_simple_size_cells(ofnode_to_np(node));
703         else
704                 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
705 }
706
707 bool ofnode_pre_reloc(ofnode node)
708 {
709 #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
716         if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
717                 return true;
718         if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
719                 return true;
720
721         /*
722          * In regular builds individual spl and tpl handling both
723          * count as handled pre-relocation for later second init.
724          */
725         if (ofnode_read_bool(node, "u-boot,dm-spl") ||
726             ofnode_read_bool(node, "u-boot,dm-tpl"))
727                 return true;
728
729         return false;
730 #endif
731 }
732
733 int 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 }
752
753 int 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 }
764
765 u64 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 }
772
773 int 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 }
783
784 ofnode 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 }
795
796 ofnode 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 }
809
810 int 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);
843         if (!new->name) {
844                 free(new);
845                 return -ENOMEM;
846         }
847
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
857 int 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
869 int 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 }