Merge branch 'next' of https://source.denx.de/u-boot/custodians/u-boot-net
[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 <log.h>
12 #include <malloc.h>
13 #include <linux/libfdt.h>
14 #include <dm/of_access.h>
15 #include <dm/of_addr.h>
16 #include <dm/ofnode.h>
17 #include <linux/err.h>
18 #include <linux/ioport.h>
19 #include <asm/global_data.h>
20
21 bool ofnode_name_eq(ofnode node, const char *name)
22 {
23         const char *node_name;
24         size_t len;
25
26         assert(ofnode_valid(node));
27
28         node_name = ofnode_get_name(node);
29         len = strchrnul(node_name, '@') - node_name;
30
31         return (strlen(name) == len) && !strncmp(node_name, name, len);
32 }
33
34 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
35 {
36         return ofnode_read_u32_index(node, propname, 0, outp);
37 }
38
39 u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
40 {
41         assert(ofnode_valid(node));
42         ofnode_read_u32_index(node, propname, 0, &def);
43
44         return def;
45 }
46
47 int ofnode_read_u32_index(ofnode node, const char *propname, int index,
48                           u32 *outp)
49 {
50         const fdt32_t *cell;
51         int len;
52
53         assert(ofnode_valid(node));
54         debug("%s: %s: ", __func__, propname);
55
56         if (ofnode_is_np(node))
57                 return of_read_u32_index(ofnode_to_np(node), propname, index,
58                                          outp);
59
60         cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
61                            &len);
62         if (!cell) {
63                 debug("(not found)\n");
64                 return -EINVAL;
65         }
66
67         if (len < (sizeof(int) * (index + 1))) {
68                 debug("(not large enough)\n");
69                 return -EOVERFLOW;
70         }
71
72         *outp = fdt32_to_cpu(cell[index]);
73         debug("%#x (%d)\n", *outp, *outp);
74
75         return 0;
76 }
77
78 u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
79                                   u32 def)
80 {
81         assert(ofnode_valid(node));
82         ofnode_read_u32_index(node, propname, index, &def);
83
84         return def;
85 }
86
87 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
88 {
89         assert(ofnode_valid(node));
90         ofnode_read_u32(node, propname, (u32 *)&def);
91
92         return def;
93 }
94
95 int ofnode_read_u64(ofnode node, const char *propname, u64 *outp)
96 {
97         const unaligned_fdt64_t *cell;
98         int len;
99
100         assert(ofnode_valid(node));
101         debug("%s: %s: ", __func__, propname);
102
103         if (ofnode_is_np(node))
104                 return of_read_u64(ofnode_to_np(node), propname, outp);
105
106         cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
107                            &len);
108         if (!cell || len < sizeof(*cell)) {
109                 debug("(not found)\n");
110                 return -EINVAL;
111         }
112         *outp = fdt64_to_cpu(cell[0]);
113         debug("%#llx (%lld)\n", (unsigned long long)*outp,
114               (unsigned long long)*outp);
115
116         return 0;
117 }
118
119 u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def)
120 {
121         assert(ofnode_valid(node));
122         ofnode_read_u64(node, propname, &def);
123
124         return def;
125 }
126
127 bool ofnode_read_bool(ofnode node, const char *propname)
128 {
129         const void *prop;
130
131         assert(ofnode_valid(node));
132         debug("%s: %s: ", __func__, propname);
133
134         prop = ofnode_get_property(node, propname, NULL);
135
136         debug("%s\n", prop ? "true" : "false");
137
138         return prop ? true : false;
139 }
140
141 const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep)
142 {
143         const char *val = NULL;
144         int len;
145
146         assert(ofnode_valid(node));
147         debug("%s: %s: ", __func__, propname);
148
149         if (ofnode_is_np(node)) {
150                 struct property *prop = of_find_property(
151                                 ofnode_to_np(node), propname, &len);
152
153                 if (prop) {
154                         val = prop->value;
155                         len = prop->length;
156                 }
157         } else {
158                 val = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
159                                   propname, &len);
160         }
161         if (!val) {
162                 debug("<not found>\n");
163                 if (sizep)
164                         *sizep = -FDT_ERR_NOTFOUND;
165                 return NULL;
166         }
167         if (sizep)
168                 *sizep = len;
169
170         return val;
171 }
172
173 const char *ofnode_read_string(ofnode node, const char *propname)
174 {
175         const char *str;
176         int len;
177
178         str = ofnode_read_prop(node, propname, &len);
179         if (!str)
180                 return NULL;
181
182         if (strnlen(str, len) >= len) {
183                 debug("<invalid>\n");
184                 return NULL;
185         }
186         debug("%s\n", str);
187
188         return str;
189 }
190
191 int ofnode_read_size(ofnode node, const char *propname)
192 {
193         int len;
194
195         if (!ofnode_read_prop(node, propname, &len))
196                 return -EINVAL;
197
198         return len;
199 }
200
201 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
202 {
203         ofnode subnode;
204
205         assert(ofnode_valid(node));
206         debug("%s: %s: ", __func__, subnode_name);
207
208         if (ofnode_is_np(node)) {
209                 const struct device_node *np = ofnode_to_np(node);
210
211                 for (np = np->child; np; np = np->sibling) {
212                         if (!strcmp(subnode_name, np->name))
213                                 break;
214                 }
215                 subnode = np_to_ofnode(np);
216         } else {
217                 int ooffset = fdt_subnode_offset(gd->fdt_blob,
218                                 ofnode_to_offset(node), subnode_name);
219                 subnode = offset_to_ofnode(ooffset);
220         }
221         debug("%s\n", ofnode_valid(subnode) ?
222               ofnode_get_name(subnode) : "<none>");
223
224         return subnode;
225 }
226
227 int ofnode_read_u32_array(ofnode node, const char *propname,
228                           u32 *out_values, size_t sz)
229 {
230         assert(ofnode_valid(node));
231         debug("%s: %s: ", __func__, propname);
232
233         if (ofnode_is_np(node)) {
234                 return of_read_u32_array(ofnode_to_np(node), propname,
235                                          out_values, sz);
236         } else {
237                 return fdtdec_get_int_array(gd->fdt_blob,
238                                             ofnode_to_offset(node), propname,
239                                             out_values, sz);
240         }
241 }
242
243 #if !CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
244 bool ofnode_is_enabled(ofnode node)
245 {
246         if (ofnode_is_np(node)) {
247                 return of_device_is_available(ofnode_to_np(node));
248         } else {
249                 return fdtdec_get_is_enabled(gd->fdt_blob,
250                                              ofnode_to_offset(node));
251         }
252 }
253
254 ofnode ofnode_first_subnode(ofnode node)
255 {
256         assert(ofnode_valid(node));
257         if (ofnode_is_np(node))
258                 return np_to_ofnode(node.np->child);
259
260         return offset_to_ofnode(
261                 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
262 }
263
264 ofnode ofnode_next_subnode(ofnode node)
265 {
266         assert(ofnode_valid(node));
267         if (ofnode_is_np(node))
268                 return np_to_ofnode(node.np->sibling);
269
270         return offset_to_ofnode(
271                 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
272 }
273 #endif /* !DM_INLINE_OFNODE */
274
275 ofnode ofnode_get_parent(ofnode node)
276 {
277         ofnode parent;
278
279         assert(ofnode_valid(node));
280         if (ofnode_is_np(node))
281                 parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
282         else
283                 parent.of_offset = fdt_parent_offset(gd->fdt_blob,
284                                                      ofnode_to_offset(node));
285
286         return parent;
287 }
288
289 const char *ofnode_get_name(ofnode node)
290 {
291         if (!ofnode_valid(node)) {
292                 debug("%s node not valid\n", __func__);
293                 return NULL;
294         }
295
296         if (ofnode_is_np(node))
297                 return strrchr(node.np->full_name, '/') + 1;
298
299         return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
300 }
301
302 int ofnode_get_path(ofnode node, char *buf, int buflen)
303 {
304         assert(ofnode_valid(node));
305
306         if (ofnode_is_np(node)) {
307                 if (strlen(node.np->full_name) >= buflen)
308                         return -ENOSPC;
309
310                 strcpy(buf, node.np->full_name);
311
312                 return 0;
313         } else {
314                 int res;
315
316                 res = fdt_get_path(gd->fdt_blob, ofnode_to_offset(node), buf,
317                                    buflen);
318                 if (!res)
319                         return res;
320                 else if (res == -FDT_ERR_NOSPACE)
321                         return -ENOSPC;
322                 else
323                         return -EINVAL;
324         }
325 }
326
327 ofnode ofnode_get_by_phandle(uint phandle)
328 {
329         ofnode node;
330
331         if (of_live_active())
332                 node = np_to_ofnode(of_find_node_by_phandle(phandle));
333         else
334                 node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
335                                                             phandle);
336
337         return node;
338 }
339
340 static fdt_addr_t __ofnode_get_addr_size_index(ofnode node, int index,
341                                                fdt_size_t *size, bool translate)
342 {
343         int na, ns;
344
345         if (size)
346                 *size = FDT_SIZE_T_NONE;
347
348         if (ofnode_is_np(node)) {
349                 const __be32 *prop_val;
350                 u64 size64;
351                 uint flags;
352
353                 prop_val = of_get_address(ofnode_to_np(node), index, &size64,
354                                           &flags);
355                 if (!prop_val)
356                         return FDT_ADDR_T_NONE;
357
358                 if (size)
359                         *size = size64;
360
361                 ns = of_n_size_cells(ofnode_to_np(node));
362
363                 if (translate && IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
364                         return of_translate_address(ofnode_to_np(node), prop_val);
365                 } else {
366                         na = of_n_addr_cells(ofnode_to_np(node));
367                         return of_read_number(prop_val, na);
368                 }
369         } else {
370                 na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
371                 ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
372                 return fdtdec_get_addr_size_fixed(gd->fdt_blob,
373                                                   ofnode_to_offset(node), "reg",
374                                                   index, na, ns, size,
375                                                   translate);
376         }
377 }
378
379 fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
380 {
381         return __ofnode_get_addr_size_index(node, index, size, true);
382 }
383
384 fdt_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
385                                               fdt_size_t *size)
386 {
387         return __ofnode_get_addr_size_index(node, index, size, false);
388 }
389
390 fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
391 {
392         fdt_size_t size;
393
394         return ofnode_get_addr_size_index(node, index, &size);
395 }
396
397 fdt_addr_t ofnode_get_addr(ofnode node)
398 {
399         return ofnode_get_addr_index(node, 0);
400 }
401
402 fdt_size_t ofnode_get_size(ofnode node)
403 {
404         fdt_size_t size;
405
406         ofnode_get_addr_size_index(node, 0, &size);
407
408         return size;
409 }
410
411 int ofnode_stringlist_search(ofnode node, const char *property,
412                              const char *string)
413 {
414         if (ofnode_is_np(node)) {
415                 return of_property_match_string(ofnode_to_np(node),
416                                                 property, string);
417         } else {
418                 int ret;
419
420                 ret = fdt_stringlist_search(gd->fdt_blob,
421                                             ofnode_to_offset(node), property,
422                                             string);
423                 if (ret == -FDT_ERR_NOTFOUND)
424                         return -ENODATA;
425                 else if (ret < 0)
426                         return -EINVAL;
427
428                 return ret;
429         }
430 }
431
432 int ofnode_read_string_index(ofnode node, const char *property, int index,
433                              const char **outp)
434 {
435         if (ofnode_is_np(node)) {
436                 return of_property_read_string_index(ofnode_to_np(node),
437                                                      property, index, outp);
438         } else {
439                 int len;
440
441                 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
442                                            property, index, &len);
443                 if (len < 0)
444                         return -EINVAL;
445                 return 0;
446         }
447 }
448
449 int ofnode_read_string_count(ofnode node, const char *property)
450 {
451         if (ofnode_is_np(node)) {
452                 return of_property_count_strings(ofnode_to_np(node), property);
453         } else {
454                 return fdt_stringlist_count(gd->fdt_blob,
455                                             ofnode_to_offset(node), property);
456         }
457 }
458
459 int ofnode_read_string_list(ofnode node, const char *property,
460                             const char ***listp)
461 {
462         const char **prop;
463         int count;
464         int i;
465
466         *listp = NULL;
467         count = ofnode_read_string_count(node, property);
468         if (count < 0)
469                 return count;
470         if (!count)
471                 return 0;
472
473         prop = calloc(count + 1, sizeof(char *));
474         if (!prop)
475                 return -ENOMEM;
476
477         for (i = 0; i < count; i++)
478                 ofnode_read_string_index(node, property, i, &prop[i]);
479         prop[count] = NULL;
480         *listp = prop;
481
482         return count;
483 }
484
485 static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
486                                             struct ofnode_phandle_args *out)
487 {
488         assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
489         out->node = offset_to_ofnode(in->node);
490         out->args_count = in->args_count;
491         memcpy(out->args, in->args, sizeof(out->args));
492 }
493
494 static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
495                                         struct ofnode_phandle_args *out)
496 {
497         assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
498         out->node = np_to_ofnode(in->np);
499         out->args_count = in->args_count;
500         memcpy(out->args, in->args, sizeof(out->args));
501 }
502
503 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
504                                    const char *cells_name, int cell_count,
505                                    int index,
506                                    struct ofnode_phandle_args *out_args)
507 {
508         if (ofnode_is_np(node)) {
509                 struct of_phandle_args args;
510                 int ret;
511
512                 ret = of_parse_phandle_with_args(ofnode_to_np(node),
513                                                  list_name, cells_name,
514                                                  cell_count, index,
515                                                  &args);
516                 if (ret)
517                         return ret;
518                 ofnode_from_of_phandle_args(&args, out_args);
519         } else {
520                 struct fdtdec_phandle_args args;
521                 int ret;
522
523                 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
524                                                      ofnode_to_offset(node),
525                                                      list_name, cells_name,
526                                                      cell_count, index, &args);
527                 if (ret)
528                         return ret;
529                 ofnode_from_fdtdec_phandle_args(&args, out_args);
530         }
531
532         return 0;
533 }
534
535 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
536                                    const char *cells_name, int cell_count)
537 {
538         if (ofnode_is_np(node))
539                 return of_count_phandle_with_args(ofnode_to_np(node),
540                                 list_name, cells_name, cell_count);
541         else
542                 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
543                                 ofnode_to_offset(node), list_name, cells_name,
544                                 cell_count, -1, NULL);
545 }
546
547 ofnode ofnode_path(const char *path)
548 {
549         if (of_live_active())
550                 return np_to_ofnode(of_find_node_by_path(path));
551         else
552                 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
553 }
554
555 const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
556 {
557         ofnode chosen_node;
558
559         chosen_node = ofnode_path("/chosen");
560
561         return ofnode_read_prop(chosen_node, propname, sizep);
562 }
563
564 const char *ofnode_read_chosen_string(const char *propname)
565 {
566         return ofnode_read_chosen_prop(propname, NULL);
567 }
568
569 ofnode ofnode_get_chosen_node(const char *name)
570 {
571         const char *prop;
572
573         prop = ofnode_read_chosen_prop(name, NULL);
574         if (!prop)
575                 return ofnode_null();
576
577         return ofnode_path(prop);
578 }
579
580 const void *ofnode_read_aliases_prop(const char *propname, int *sizep)
581 {
582         ofnode node;
583
584         node = ofnode_path("/aliases");
585
586         return ofnode_read_prop(node, propname, sizep);
587 }
588
589 ofnode ofnode_get_aliases_node(const char *name)
590 {
591         const char *prop;
592
593         prop = ofnode_read_aliases_prop(name, NULL);
594         if (!prop)
595                 return ofnode_null();
596
597         debug("%s: node_path: %s\n", __func__, prop);
598
599         return ofnode_path(prop);
600 }
601
602 int ofnode_get_child_count(ofnode parent)
603 {
604         ofnode child;
605         int num = 0;
606
607         ofnode_for_each_subnode(child, parent)
608                 num++;
609
610         return num;
611 }
612
613 static int decode_timing_property(ofnode node, const char *name,
614                                   struct timing_entry *result)
615 {
616         int length, ret = 0;
617
618         length = ofnode_read_size(node, name);
619         if (length < 0) {
620                 debug("%s: could not find property %s\n",
621                       ofnode_get_name(node), name);
622                 return length;
623         }
624
625         if (length == sizeof(u32)) {
626                 result->typ = ofnode_read_u32_default(node, name, 0);
627                 result->min = result->typ;
628                 result->max = result->typ;
629         } else {
630                 ret = ofnode_read_u32_array(node, name, &result->min, 3);
631         }
632
633         return ret;
634 }
635
636 int ofnode_decode_display_timing(ofnode parent, int index,
637                                  struct display_timing *dt)
638 {
639         int i;
640         ofnode timings, node;
641         u32 val = 0;
642         int ret = 0;
643
644         timings = ofnode_find_subnode(parent, "display-timings");
645         if (!ofnode_valid(timings))
646                 return -EINVAL;
647
648         i = 0;
649         ofnode_for_each_subnode(node, timings) {
650                 if (i++ == index)
651                         break;
652         }
653
654         if (!ofnode_valid(node))
655                 return -EINVAL;
656
657         memset(dt, 0, sizeof(*dt));
658
659         ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
660         ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
661         ret |= decode_timing_property(node, "hactive", &dt->hactive);
662         ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
663         ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
664         ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
665         ret |= decode_timing_property(node, "vactive", &dt->vactive);
666         ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
667         ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
668
669         dt->flags = 0;
670         val = ofnode_read_u32_default(node, "vsync-active", -1);
671         if (val != -1) {
672                 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
673                                 DISPLAY_FLAGS_VSYNC_LOW;
674         }
675         val = ofnode_read_u32_default(node, "hsync-active", -1);
676         if (val != -1) {
677                 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
678                                 DISPLAY_FLAGS_HSYNC_LOW;
679         }
680         val = ofnode_read_u32_default(node, "de-active", -1);
681         if (val != -1) {
682                 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
683                                 DISPLAY_FLAGS_DE_LOW;
684         }
685         val = ofnode_read_u32_default(node, "pixelclk-active", -1);
686         if (val != -1) {
687                 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
688                                 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
689         }
690
691         if (ofnode_read_bool(node, "interlaced"))
692                 dt->flags |= DISPLAY_FLAGS_INTERLACED;
693         if (ofnode_read_bool(node, "doublescan"))
694                 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
695         if (ofnode_read_bool(node, "doubleclk"))
696                 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
697
698         return ret;
699 }
700
701 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
702 {
703         if (ofnode_is_np(node))
704                 return of_get_property(ofnode_to_np(node), propname, lenp);
705         else
706                 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
707                                    propname, lenp);
708 }
709
710 int ofnode_get_first_property(ofnode node, struct ofprop *prop)
711 {
712         prop->node = node;
713
714         if (ofnode_is_np(node)) {
715                 prop->prop = of_get_first_property(ofnode_to_np(prop->node));
716                 if (!prop->prop)
717                         return -FDT_ERR_NOTFOUND;
718         } else {
719                 prop->offset =
720                         fdt_first_property_offset(gd->fdt_blob,
721                                                   ofnode_to_offset(prop->node));
722                 if (prop->offset < 0)
723                         return prop->offset;
724         }
725
726         return 0;
727 }
728
729 int ofnode_get_next_property(struct ofprop *prop)
730 {
731         if (ofnode_is_np(prop->node)) {
732                 prop->prop = of_get_next_property(ofnode_to_np(prop->node),
733                                                   prop->prop);
734                 if (!prop->prop)
735                         return -FDT_ERR_NOTFOUND;
736         } else {
737                 prop->offset = fdt_next_property_offset(gd->fdt_blob,
738                                                         prop->offset);
739                 if (prop->offset  < 0)
740                         return prop->offset;
741         }
742
743         return 0;
744 }
745
746 const void *ofnode_get_property_by_prop(const struct ofprop *prop,
747                                         const char **propname, int *lenp)
748 {
749         if (ofnode_is_np(prop->node))
750                 return of_get_property_by_prop(ofnode_to_np(prop->node),
751                                                prop->prop, propname, lenp);
752         else
753                 return fdt_getprop_by_offset(gd->fdt_blob,
754                                              prop->offset,
755                                              propname, lenp);
756 }
757
758 bool ofnode_is_available(ofnode node)
759 {
760         if (ofnode_is_np(node))
761                 return of_device_is_available(ofnode_to_np(node));
762         else
763                 return fdtdec_get_is_enabled(gd->fdt_blob,
764                                              ofnode_to_offset(node));
765 }
766
767 fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
768                                 fdt_size_t *sizep)
769 {
770         if (ofnode_is_np(node)) {
771                 int na, ns;
772                 int psize;
773                 const struct device_node *np = ofnode_to_np(node);
774                 const __be32 *prop = of_get_property(np, property, &psize);
775
776                 if (!prop)
777                         return FDT_ADDR_T_NONE;
778                 na = of_n_addr_cells(np);
779                 ns = of_n_size_cells(np);
780                 *sizep = of_read_number(prop + na, ns);
781
782                 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
783                         return of_translate_address(np, prop);
784                 else
785                         return of_read_number(prop, na);
786         } else {
787                 return fdtdec_get_addr_size(gd->fdt_blob,
788                                             ofnode_to_offset(node), property,
789                                             sizep);
790         }
791 }
792
793 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
794                                         size_t sz)
795 {
796         if (ofnode_is_np(node)) {
797                 const struct device_node *np = ofnode_to_np(node);
798                 int psize;
799                 const __be32 *prop = of_get_property(np, propname, &psize);
800
801                 if (!prop || sz != psize)
802                         return NULL;
803                 return (uint8_t *)prop;
804
805         } else {
806                 return fdtdec_locate_byte_array(gd->fdt_blob,
807                                 ofnode_to_offset(node), propname, sz);
808         }
809 }
810
811 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
812                          const char *propname, struct fdt_pci_addr *addr)
813 {
814         const fdt32_t *cell;
815         int len;
816         int ret = -ENOENT;
817
818         debug("%s: %s: ", __func__, propname);
819
820         /*
821          * If we follow the pci bus bindings strictly, we should check
822          * the value of the node's parent node's #address-cells and
823          * #size-cells. They need to be 3 and 2 accordingly. However,
824          * for simplicity we skip the check here.
825          */
826         cell = ofnode_get_property(node, propname, &len);
827         if (!cell)
828                 goto fail;
829
830         if ((len % FDT_PCI_REG_SIZE) == 0) {
831                 int num = len / FDT_PCI_REG_SIZE;
832                 int i;
833
834                 for (i = 0; i < num; i++) {
835                         debug("pci address #%d: %08lx %08lx %08lx\n", i,
836                               (ulong)fdt32_to_cpu(cell[0]),
837                               (ulong)fdt32_to_cpu(cell[1]),
838                               (ulong)fdt32_to_cpu(cell[2]));
839                         if ((fdt32_to_cpu(*cell) & type) == type) {
840                                 addr->phys_hi = fdt32_to_cpu(cell[0]);
841                                 addr->phys_mid = fdt32_to_cpu(cell[1]);
842                                 addr->phys_lo = fdt32_to_cpu(cell[2]);
843                                 break;
844                         }
845
846                         cell += (FDT_PCI_ADDR_CELLS +
847                                  FDT_PCI_SIZE_CELLS);
848                 }
849
850                 if (i == num) {
851                         ret = -ENXIO;
852                         goto fail;
853                 }
854
855                 return 0;
856         }
857
858         ret = -EINVAL;
859
860 fail:
861         debug("(not found)\n");
862         return ret;
863 }
864
865 int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
866 {
867         const char *list, *end;
868         int len;
869
870         list = ofnode_get_property(node, "compatible", &len);
871         if (!list)
872                 return -ENOENT;
873
874         end = list + len;
875         while (list < end) {
876                 len = strlen(list);
877                 if (len >= strlen("pciVVVV,DDDD")) {
878                         char *s = strstr(list, "pci");
879
880                         /*
881                          * check if the string is something like pciVVVV,DDDD.RR
882                          * or just pciVVVV,DDDD
883                          */
884                         if (s && s[7] == ',' &&
885                             (s[12] == '.' || s[12] == 0)) {
886                                 s += 3;
887                                 *vendor = simple_strtol(s, NULL, 16);
888
889                                 s += 5;
890                                 *device = simple_strtol(s, NULL, 16);
891
892                                 return 0;
893                         }
894                 }
895                 list += (len + 1);
896         }
897
898         return -ENOENT;
899 }
900
901 int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device)
902 {
903         const char *list, *end;
904         int len;
905
906         list = ofnode_get_property(node, "compatible", &len);
907
908         if (!list)
909                 return -ENOENT;
910
911         end = list + len;
912         while (list < end) {
913                 len = strlen(list);
914
915                 if (len >= strlen("ethernet-phy-idVVVV,DDDD")) {
916                         char *s = strstr(list, "ethernet-phy-id");
917
918                         /*
919                          * check if the string is something like
920                          * ethernet-phy-idVVVV,DDDD
921                          */
922                         if (s && s[19] == '.') {
923                                 s += strlen("ethernet-phy-id");
924                                 *vendor = simple_strtol(s, NULL, 16);
925                                 s += 5;
926                                 *device = simple_strtol(s, NULL, 16);
927
928                                 return 0;
929                         }
930                 }
931                 list += (len + 1);
932         }
933
934         return -ENOENT;
935 }
936
937 int ofnode_read_addr_cells(ofnode node)
938 {
939         if (ofnode_is_np(node)) {
940                 return of_n_addr_cells(ofnode_to_np(node));
941         } else {
942                 int parent = fdt_parent_offset(gd->fdt_blob,
943                                                ofnode_to_offset(node));
944
945                 return fdt_address_cells(gd->fdt_blob, parent);
946         }
947 }
948
949 int ofnode_read_size_cells(ofnode node)
950 {
951         if (ofnode_is_np(node)) {
952                 return of_n_size_cells(ofnode_to_np(node));
953         } else {
954                 int parent = fdt_parent_offset(gd->fdt_blob,
955                                                ofnode_to_offset(node));
956
957                 return fdt_size_cells(gd->fdt_blob, parent);
958         }
959 }
960
961 int ofnode_read_simple_addr_cells(ofnode node)
962 {
963         if (ofnode_is_np(node))
964                 return of_simple_addr_cells(ofnode_to_np(node));
965         else
966                 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
967 }
968
969 int ofnode_read_simple_size_cells(ofnode node)
970 {
971         if (ofnode_is_np(node))
972                 return of_simple_size_cells(ofnode_to_np(node));
973         else
974                 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
975 }
976
977 bool ofnode_pre_reloc(ofnode node)
978 {
979 #if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
980         /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
981          * had property dm-pre-reloc or u-boot,dm-spl/tpl.
982          * They are removed in final dtb (fdtgrep 2nd pass)
983          */
984         return true;
985 #else
986         if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
987                 return true;
988         if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
989                 return true;
990
991         /*
992          * In regular builds individual spl and tpl handling both
993          * count as handled pre-relocation for later second init.
994          */
995         if (ofnode_read_bool(node, "u-boot,dm-spl") ||
996             ofnode_read_bool(node, "u-boot,dm-tpl"))
997                 return true;
998
999         return false;
1000 #endif
1001 }
1002
1003 int ofnode_read_resource(ofnode node, uint index, struct resource *res)
1004 {
1005         if (ofnode_is_np(node)) {
1006                 return of_address_to_resource(ofnode_to_np(node), index, res);
1007         } else {
1008                 struct fdt_resource fres;
1009                 int ret;
1010
1011                 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
1012                                        "reg", index, &fres);
1013                 if (ret < 0)
1014                         return -EINVAL;
1015                 memset(res, '\0', sizeof(*res));
1016                 res->start = fres.start;
1017                 res->end = fres.end;
1018
1019                 return 0;
1020         }
1021 }
1022
1023 int ofnode_read_resource_byname(ofnode node, const char *name,
1024                                 struct resource *res)
1025 {
1026         int index;
1027
1028         index = ofnode_stringlist_search(node, "reg-names", name);
1029         if (index < 0)
1030                 return index;
1031
1032         return ofnode_read_resource(node, index, res);
1033 }
1034
1035 u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
1036 {
1037         if (ofnode_is_np(node))
1038                 return of_translate_address(ofnode_to_np(node), in_addr);
1039         else
1040                 return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
1041 }
1042
1043 u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
1044 {
1045         if (ofnode_is_np(node))
1046                 return of_translate_dma_address(ofnode_to_np(node), in_addr);
1047         else
1048                 return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
1049 }
1050
1051 int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus, u64 *size)
1052 {
1053         if (ofnode_is_np(node))
1054                 return of_get_dma_range(ofnode_to_np(node), cpu, bus, size);
1055         else
1056                 return fdt_get_dma_range(gd->fdt_blob, ofnode_to_offset(node),
1057                                          cpu, bus, size);
1058 }
1059
1060 int ofnode_device_is_compatible(ofnode node, const char *compat)
1061 {
1062         if (ofnode_is_np(node))
1063                 return of_device_is_compatible(ofnode_to_np(node), compat,
1064                                                NULL, NULL);
1065         else
1066                 return !fdt_node_check_compatible(gd->fdt_blob,
1067                                                   ofnode_to_offset(node),
1068                                                   compat);
1069 }
1070
1071 ofnode ofnode_by_compatible(ofnode from, const char *compat)
1072 {
1073         if (of_live_active()) {
1074                 return np_to_ofnode(of_find_compatible_node(
1075                         (struct device_node *)ofnode_to_np(from), NULL,
1076                         compat));
1077         } else {
1078                 return offset_to_ofnode(fdt_node_offset_by_compatible(
1079                                 gd->fdt_blob, ofnode_to_offset(from), compat));
1080         }
1081 }
1082
1083 ofnode ofnode_by_prop_value(ofnode from, const char *propname,
1084                             const void *propval, int proplen)
1085 {
1086         if (of_live_active()) {
1087                 return np_to_ofnode(of_find_node_by_prop_value(
1088                         (struct device_node *)ofnode_to_np(from), propname,
1089                         propval, proplen));
1090         } else {
1091                 return offset_to_ofnode(fdt_node_offset_by_prop_value(
1092                                 gd->fdt_blob, ofnode_to_offset(from),
1093                                 propname, propval, proplen));
1094         }
1095 }
1096
1097 int ofnode_write_prop(ofnode node, const char *propname, int len,
1098                       const void *value)
1099 {
1100         const struct device_node *np = ofnode_to_np(node);
1101         struct property *pp;
1102         struct property *pp_last = NULL;
1103         struct property *new;
1104
1105         if (!of_live_active())
1106                 return -ENOSYS;
1107
1108         if (!np)
1109                 return -EINVAL;
1110
1111         for (pp = np->properties; pp; pp = pp->next) {
1112                 if (strcmp(pp->name, propname) == 0) {
1113                         /* Property exists -> change value */
1114                         pp->value = (void *)value;
1115                         pp->length = len;
1116                         return 0;
1117                 }
1118                 pp_last = pp;
1119         }
1120
1121         if (!pp_last)
1122                 return -ENOENT;
1123
1124         /* Property does not exist -> append new property */
1125         new = malloc(sizeof(struct property));
1126         if (!new)
1127                 return -ENOMEM;
1128
1129         new->name = strdup(propname);
1130         if (!new->name) {
1131                 free(new);
1132                 return -ENOMEM;
1133         }
1134
1135         new->value = (void *)value;
1136         new->length = len;
1137         new->next = NULL;
1138
1139         pp_last->next = new;
1140
1141         return 0;
1142 }
1143
1144 int ofnode_write_string(ofnode node, const char *propname, const char *value)
1145 {
1146         if (!of_live_active())
1147                 return -ENOSYS;
1148
1149         assert(ofnode_valid(node));
1150
1151         debug("%s: %s = %s", __func__, propname, value);
1152
1153         return ofnode_write_prop(node, propname, strlen(value) + 1, value);
1154 }
1155
1156 int ofnode_set_enabled(ofnode node, bool value)
1157 {
1158         if (!of_live_active())
1159                 return -ENOSYS;
1160
1161         assert(ofnode_valid(node));
1162
1163         if (value)
1164                 return ofnode_write_string(node, "status", "okay");
1165         else
1166                 return ofnode_write_string(node, "status", "disabled");
1167 }
1168
1169 bool ofnode_conf_read_bool(const char *prop_name)
1170 {
1171         ofnode node;
1172
1173         node = ofnode_path("/config");
1174         if (!ofnode_valid(node))
1175                 return false;
1176
1177         return ofnode_read_bool(node, prop_name);
1178 }
1179
1180 int ofnode_conf_read_int(const char *prop_name, int default_val)
1181 {
1182         ofnode node;
1183
1184         node = ofnode_path("/config");
1185         if (!ofnode_valid(node))
1186                 return default_val;
1187
1188         return ofnode_read_u32_default(node, prop_name, default_val);
1189 }
1190
1191 const char *ofnode_conf_read_str(const char *prop_name)
1192 {
1193         ofnode node;
1194
1195         node = ofnode_path("/config");
1196         if (!ofnode_valid(node))
1197                 return NULL;
1198
1199         return ofnode_read_string(node, prop_name);
1200 }
1201
1202 ofnode ofnode_get_phy_node(ofnode node)
1203 {
1204         /* DT node properties that reference a PHY node */
1205         static const char * const phy_handle_str[] = {
1206                 "phy-handle", "phy", "phy-device",
1207         };
1208         struct ofnode_phandle_args args = {
1209                 .node = ofnode_null()
1210         };
1211         int i;
1212
1213         assert(ofnode_valid(node));
1214
1215         for (i = 0; i < ARRAY_SIZE(phy_handle_str); i++)
1216                 if (!ofnode_parse_phandle_with_args(node, phy_handle_str[i],
1217                                                     NULL, 0, 0, &args))
1218                         break;
1219
1220         return args.node;
1221 }
1222
1223 phy_interface_t ofnode_read_phy_mode(ofnode node)
1224 {
1225         const char *mode;
1226         int i;
1227
1228         assert(ofnode_valid(node));
1229
1230         mode = ofnode_read_string(node, "phy-mode");
1231         if (!mode)
1232                 mode = ofnode_read_string(node, "phy-connection-type");
1233
1234         if (!mode)
1235                 return PHY_INTERFACE_MODE_NA;
1236
1237         for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1238                 if (!strcmp(mode, phy_interface_strings[i]))
1239                         return i;
1240
1241         debug("%s: Invalid PHY interface '%s'\n", __func__, mode);
1242
1243         return PHY_INTERFACE_MODE_NA;
1244 }