1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2017 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
10 #include <fdt_support.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>
21 bool ofnode_name_eq(ofnode node, const char *name)
23 const char *node_name;
26 assert(ofnode_valid(node));
28 node_name = ofnode_get_name(node);
29 len = strchrnul(node_name, '@') - node_name;
31 return (strlen(name) == len) && !strncmp(node_name, name, len);
34 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
36 return ofnode_read_u32_index(node, propname, 0, outp);
39 u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
41 assert(ofnode_valid(node));
42 ofnode_read_u32_index(node, propname, 0, &def);
47 int ofnode_read_u32_index(ofnode node, const char *propname, int index,
53 assert(ofnode_valid(node));
54 debug("%s: %s: ", __func__, propname);
56 if (ofnode_is_np(node))
57 return of_read_u32_index(ofnode_to_np(node), propname, index,
60 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
63 debug("(not found)\n");
67 if (len < (sizeof(int) * (index + 1))) {
68 debug("(not large enough)\n");
72 *outp = fdt32_to_cpu(cell[index]);
73 debug("%#x (%d)\n", *outp, *outp);
78 u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
81 assert(ofnode_valid(node));
82 ofnode_read_u32_index(node, propname, index, &def);
87 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
89 assert(ofnode_valid(node));
90 ofnode_read_u32(node, propname, (u32 *)&def);
95 int ofnode_read_u64(ofnode node, const char *propname, u64 *outp)
97 const unaligned_fdt64_t *cell;
100 assert(ofnode_valid(node));
101 debug("%s: %s: ", __func__, propname);
103 if (ofnode_is_np(node))
104 return of_read_u64(ofnode_to_np(node), propname, outp);
106 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
108 if (!cell || len < sizeof(*cell)) {
109 debug("(not found)\n");
112 *outp = fdt64_to_cpu(cell[0]);
113 debug("%#llx (%lld)\n", (unsigned long long)*outp,
114 (unsigned long long)*outp);
119 u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def)
121 assert(ofnode_valid(node));
122 ofnode_read_u64(node, propname, &def);
127 bool ofnode_read_bool(ofnode node, const char *propname)
131 assert(ofnode_valid(node));
132 debug("%s: %s: ", __func__, propname);
134 prop = ofnode_get_property(node, propname, NULL);
136 debug("%s\n", prop ? "true" : "false");
138 return prop ? true : false;
141 const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep)
143 const char *val = NULL;
146 assert(ofnode_valid(node));
147 debug("%s: %s: ", __func__, propname);
149 if (ofnode_is_np(node)) {
150 struct property *prop = of_find_property(
151 ofnode_to_np(node), propname, &len);
158 val = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
162 debug("<not found>\n");
164 *sizep = -FDT_ERR_NOTFOUND;
173 const char *ofnode_read_string(ofnode node, const char *propname)
178 str = ofnode_read_prop(node, propname, &len);
182 if (strnlen(str, len) >= len) {
183 debug("<invalid>\n");
191 int ofnode_read_size(ofnode node, const char *propname)
195 if (!ofnode_read_prop(node, propname, &len))
201 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
205 assert(ofnode_valid(node));
206 debug("%s: %s: ", __func__, subnode_name);
208 if (ofnode_is_np(node)) {
209 const struct device_node *np = ofnode_to_np(node);
211 for (np = np->child; np; np = np->sibling) {
212 if (!strcmp(subnode_name, np->name))
215 subnode = np_to_ofnode(np);
217 int ooffset = fdt_subnode_offset(gd->fdt_blob,
218 ofnode_to_offset(node), subnode_name);
219 subnode = offset_to_ofnode(ooffset);
221 debug("%s\n", ofnode_valid(subnode) ?
222 ofnode_get_name(subnode) : "<none>");
227 int ofnode_read_u32_array(ofnode node, const char *propname,
228 u32 *out_values, size_t sz)
230 assert(ofnode_valid(node));
231 debug("%s: %s: ", __func__, propname);
233 if (ofnode_is_np(node)) {
234 return of_read_u32_array(ofnode_to_np(node), propname,
237 return fdtdec_get_int_array(gd->fdt_blob,
238 ofnode_to_offset(node), propname,
243 #if !CONFIG_IS_ENABLED(DM_INLINE_OFNODE)
244 bool ofnode_is_enabled(ofnode node)
246 if (ofnode_is_np(node)) {
247 return of_device_is_available(ofnode_to_np(node));
249 return fdtdec_get_is_enabled(gd->fdt_blob,
250 ofnode_to_offset(node));
254 ofnode ofnode_first_subnode(ofnode node)
256 assert(ofnode_valid(node));
257 if (ofnode_is_np(node))
258 return np_to_ofnode(node.np->child);
260 return offset_to_ofnode(
261 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
264 ofnode ofnode_next_subnode(ofnode node)
266 assert(ofnode_valid(node));
267 if (ofnode_is_np(node))
268 return np_to_ofnode(node.np->sibling);
270 return offset_to_ofnode(
271 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
273 #endif /* !DM_INLINE_OFNODE */
275 ofnode ofnode_get_parent(ofnode node)
279 assert(ofnode_valid(node));
280 if (ofnode_is_np(node))
281 parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
283 parent.of_offset = fdt_parent_offset(gd->fdt_blob,
284 ofnode_to_offset(node));
289 const char *ofnode_get_name(ofnode node)
291 if (!ofnode_valid(node)) {
292 debug("%s node not valid\n", __func__);
296 if (ofnode_is_np(node))
297 return strrchr(node.np->full_name, '/') + 1;
299 return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
302 int ofnode_get_path(ofnode node, char *buf, int buflen)
304 assert(ofnode_valid(node));
306 if (ofnode_is_np(node)) {
307 if (strlen(node.np->full_name) >= buflen)
310 strcpy(buf, node.np->full_name);
316 res = fdt_get_path(gd->fdt_blob, ofnode_to_offset(node), buf,
320 else if (res == -FDT_ERR_NOSPACE)
327 ofnode ofnode_get_by_phandle(uint phandle)
331 if (of_live_active())
332 node = np_to_ofnode(of_find_node_by_phandle(phandle));
334 node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
340 static fdt_addr_t __ofnode_get_addr_size_index(ofnode node, int index,
341 fdt_size_t *size, bool translate)
346 *size = FDT_SIZE_T_NONE;
348 if (ofnode_is_np(node)) {
349 const __be32 *prop_val;
353 prop_val = of_get_address(ofnode_to_np(node), index, &size64,
356 return FDT_ADDR_T_NONE;
361 ns = of_n_size_cells(ofnode_to_np(node));
363 if (translate && IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
364 return of_translate_address(ofnode_to_np(node), prop_val);
366 na = of_n_addr_cells(ofnode_to_np(node));
367 return of_read_number(prop_val, na);
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",
379 fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
381 return __ofnode_get_addr_size_index(node, index, size, true);
384 fdt_addr_t ofnode_get_addr_size_index_notrans(ofnode node, int index,
387 return __ofnode_get_addr_size_index(node, index, size, false);
390 fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
394 return ofnode_get_addr_size_index(node, index, &size);
397 fdt_addr_t ofnode_get_addr(ofnode node)
399 return ofnode_get_addr_index(node, 0);
402 fdt_size_t ofnode_get_size(ofnode node)
406 ofnode_get_addr_size_index(node, 0, &size);
411 int ofnode_stringlist_search(ofnode node, const char *property,
414 if (ofnode_is_np(node)) {
415 return of_property_match_string(ofnode_to_np(node),
420 ret = fdt_stringlist_search(gd->fdt_blob,
421 ofnode_to_offset(node), property,
423 if (ret == -FDT_ERR_NOTFOUND)
432 int ofnode_read_string_index(ofnode node, const char *property, int index,
435 if (ofnode_is_np(node)) {
436 return of_property_read_string_index(ofnode_to_np(node),
437 property, index, outp);
441 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
442 property, index, &len);
449 int ofnode_read_string_count(ofnode node, const char *property)
451 if (ofnode_is_np(node)) {
452 return of_property_count_strings(ofnode_to_np(node), property);
454 return fdt_stringlist_count(gd->fdt_blob,
455 ofnode_to_offset(node), property);
459 int ofnode_read_string_list(ofnode node, const char *property,
467 count = ofnode_read_string_count(node, property);
473 prop = calloc(count + 1, sizeof(char *));
477 for (i = 0; i < count; i++)
478 ofnode_read_string_index(node, property, i, &prop[i]);
485 static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
486 struct ofnode_phandle_args *out)
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));
494 static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
495 struct ofnode_phandle_args *out)
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));
503 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
504 const char *cells_name, int cell_count,
506 struct ofnode_phandle_args *out_args)
508 if (ofnode_is_np(node)) {
509 struct of_phandle_args args;
512 ret = of_parse_phandle_with_args(ofnode_to_np(node),
513 list_name, cells_name,
518 ofnode_from_of_phandle_args(&args, out_args);
520 struct fdtdec_phandle_args args;
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);
529 ofnode_from_fdtdec_phandle_args(&args, out_args);
535 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
536 const char *cells_name, int cell_count)
538 if (ofnode_is_np(node))
539 return of_count_phandle_with_args(ofnode_to_np(node),
540 list_name, cells_name, cell_count);
542 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
543 ofnode_to_offset(node), list_name, cells_name,
544 cell_count, -1, NULL);
547 ofnode ofnode_path(const char *path)
549 if (of_live_active())
550 return np_to_ofnode(of_find_node_by_path(path));
552 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
555 ofnode ofnode_path_root(oftree tree, const char *path)
557 if (of_live_active())
558 return np_to_ofnode(of_find_node_opts_by_path(tree.np, path,
560 else if (*path != '/' && tree.fdt != gd->fdt_blob)
561 return ofnode_null(); /* Aliases only on control FDT */
563 return offset_to_ofnode(fdt_path_offset(tree.fdt, path));
566 const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
570 chosen_node = ofnode_path("/chosen");
572 return ofnode_read_prop(chosen_node, propname, sizep);
575 const char *ofnode_read_chosen_string(const char *propname)
577 return ofnode_read_chosen_prop(propname, NULL);
580 ofnode ofnode_get_chosen_node(const char *name)
584 prop = ofnode_read_chosen_prop(name, NULL);
586 return ofnode_null();
588 return ofnode_path(prop);
591 const void *ofnode_read_aliases_prop(const char *propname, int *sizep)
595 node = ofnode_path("/aliases");
597 return ofnode_read_prop(node, propname, sizep);
600 ofnode ofnode_get_aliases_node(const char *name)
604 prop = ofnode_read_aliases_prop(name, NULL);
606 return ofnode_null();
608 debug("%s: node_path: %s\n", __func__, prop);
610 return ofnode_path(prop);
613 int ofnode_get_child_count(ofnode parent)
618 ofnode_for_each_subnode(child, parent)
624 static int decode_timing_property(ofnode node, const char *name,
625 struct timing_entry *result)
629 length = ofnode_read_size(node, name);
631 debug("%s: could not find property %s\n",
632 ofnode_get_name(node), name);
636 if (length == sizeof(u32)) {
637 result->typ = ofnode_read_u32_default(node, name, 0);
638 result->min = result->typ;
639 result->max = result->typ;
641 ret = ofnode_read_u32_array(node, name, &result->min, 3);
647 int ofnode_decode_display_timing(ofnode parent, int index,
648 struct display_timing *dt)
651 ofnode timings, node;
655 timings = ofnode_find_subnode(parent, "display-timings");
656 if (!ofnode_valid(timings))
660 ofnode_for_each_subnode(node, timings) {
665 if (!ofnode_valid(node))
668 memset(dt, 0, sizeof(*dt));
670 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
671 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
672 ret |= decode_timing_property(node, "hactive", &dt->hactive);
673 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
674 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
675 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
676 ret |= decode_timing_property(node, "vactive", &dt->vactive);
677 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
678 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
681 val = ofnode_read_u32_default(node, "vsync-active", -1);
683 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
684 DISPLAY_FLAGS_VSYNC_LOW;
686 val = ofnode_read_u32_default(node, "hsync-active", -1);
688 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
689 DISPLAY_FLAGS_HSYNC_LOW;
691 val = ofnode_read_u32_default(node, "de-active", -1);
693 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
694 DISPLAY_FLAGS_DE_LOW;
696 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
698 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
699 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
702 if (ofnode_read_bool(node, "interlaced"))
703 dt->flags |= DISPLAY_FLAGS_INTERLACED;
704 if (ofnode_read_bool(node, "doublescan"))
705 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
706 if (ofnode_read_bool(node, "doubleclk"))
707 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
712 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
714 if (ofnode_is_np(node))
715 return of_get_property(ofnode_to_np(node), propname, lenp);
717 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
721 int ofnode_get_first_property(ofnode node, struct ofprop *prop)
725 if (ofnode_is_np(node)) {
726 prop->prop = of_get_first_property(ofnode_to_np(prop->node));
728 return -FDT_ERR_NOTFOUND;
731 fdt_first_property_offset(gd->fdt_blob,
732 ofnode_to_offset(prop->node));
733 if (prop->offset < 0)
740 int ofnode_get_next_property(struct ofprop *prop)
742 if (ofnode_is_np(prop->node)) {
743 prop->prop = of_get_next_property(ofnode_to_np(prop->node),
746 return -FDT_ERR_NOTFOUND;
748 prop->offset = fdt_next_property_offset(gd->fdt_blob,
750 if (prop->offset < 0)
757 const void *ofnode_get_property_by_prop(const struct ofprop *prop,
758 const char **propname, int *lenp)
760 if (ofnode_is_np(prop->node))
761 return of_get_property_by_prop(ofnode_to_np(prop->node),
762 prop->prop, propname, lenp);
764 return fdt_getprop_by_offset(gd->fdt_blob,
769 bool ofnode_is_available(ofnode node)
771 if (ofnode_is_np(node))
772 return of_device_is_available(ofnode_to_np(node));
774 return fdtdec_get_is_enabled(gd->fdt_blob,
775 ofnode_to_offset(node));
778 fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
781 if (ofnode_is_np(node)) {
784 const struct device_node *np = ofnode_to_np(node);
785 const __be32 *prop = of_get_property(np, property, &psize);
788 return FDT_ADDR_T_NONE;
789 na = of_n_addr_cells(np);
790 ns = of_n_size_cells(np);
791 *sizep = of_read_number(prop + na, ns);
793 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
794 return of_translate_address(np, prop);
796 return of_read_number(prop, na);
798 return fdtdec_get_addr_size(gd->fdt_blob,
799 ofnode_to_offset(node), property,
804 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
807 if (ofnode_is_np(node)) {
808 const struct device_node *np = ofnode_to_np(node);
810 const __be32 *prop = of_get_property(np, propname, &psize);
812 if (!prop || sz != psize)
814 return (uint8_t *)prop;
817 return fdtdec_locate_byte_array(gd->fdt_blob,
818 ofnode_to_offset(node), propname, sz);
822 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
823 const char *propname, struct fdt_pci_addr *addr)
829 debug("%s: %s: ", __func__, propname);
832 * If we follow the pci bus bindings strictly, we should check
833 * the value of the node's parent node's #address-cells and
834 * #size-cells. They need to be 3 and 2 accordingly. However,
835 * for simplicity we skip the check here.
837 cell = ofnode_get_property(node, propname, &len);
841 if ((len % FDT_PCI_REG_SIZE) == 0) {
842 int num = len / FDT_PCI_REG_SIZE;
845 for (i = 0; i < num; i++) {
846 debug("pci address #%d: %08lx %08lx %08lx\n", i,
847 (ulong)fdt32_to_cpu(cell[0]),
848 (ulong)fdt32_to_cpu(cell[1]),
849 (ulong)fdt32_to_cpu(cell[2]));
850 if ((fdt32_to_cpu(*cell) & type) == type) {
851 addr->phys_hi = fdt32_to_cpu(cell[0]);
852 addr->phys_mid = fdt32_to_cpu(cell[1]);
853 addr->phys_lo = fdt32_to_cpu(cell[2]);
857 cell += (FDT_PCI_ADDR_CELLS +
872 debug("(not found)\n");
876 int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
878 const char *list, *end;
881 list = ofnode_get_property(node, "compatible", &len);
888 if (len >= strlen("pciVVVV,DDDD")) {
889 char *s = strstr(list, "pci");
892 * check if the string is something like pciVVVV,DDDD.RR
893 * or just pciVVVV,DDDD
895 if (s && s[7] == ',' &&
896 (s[12] == '.' || s[12] == 0)) {
898 *vendor = simple_strtol(s, NULL, 16);
901 *device = simple_strtol(s, NULL, 16);
912 int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device)
914 const char *list, *end;
917 list = ofnode_get_property(node, "compatible", &len);
926 if (len >= strlen("ethernet-phy-idVVVV,DDDD")) {
927 char *s = strstr(list, "ethernet-phy-id");
930 * check if the string is something like
931 * ethernet-phy-idVVVV,DDDD
933 if (s && s[19] == '.') {
934 s += strlen("ethernet-phy-id");
935 *vendor = simple_strtol(s, NULL, 16);
937 *device = simple_strtol(s, NULL, 16);
948 int ofnode_read_addr_cells(ofnode node)
950 if (ofnode_is_np(node)) {
951 return of_n_addr_cells(ofnode_to_np(node));
953 int parent = fdt_parent_offset(gd->fdt_blob,
954 ofnode_to_offset(node));
956 return fdt_address_cells(gd->fdt_blob, parent);
960 int ofnode_read_size_cells(ofnode node)
962 if (ofnode_is_np(node)) {
963 return of_n_size_cells(ofnode_to_np(node));
965 int parent = fdt_parent_offset(gd->fdt_blob,
966 ofnode_to_offset(node));
968 return fdt_size_cells(gd->fdt_blob, parent);
972 int ofnode_read_simple_addr_cells(ofnode node)
974 if (ofnode_is_np(node))
975 return of_simple_addr_cells(ofnode_to_np(node));
977 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
980 int ofnode_read_simple_size_cells(ofnode node)
982 if (ofnode_is_np(node))
983 return of_simple_size_cells(ofnode_to_np(node));
985 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
988 bool ofnode_pre_reloc(ofnode node)
990 #if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
991 /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
992 * had property dm-pre-reloc or u-boot,dm-spl/tpl.
993 * They are removed in final dtb (fdtgrep 2nd pass)
997 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
999 if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
1003 * In regular builds individual spl and tpl handling both
1004 * count as handled pre-relocation for later second init.
1006 if (ofnode_read_bool(node, "u-boot,dm-spl") ||
1007 ofnode_read_bool(node, "u-boot,dm-tpl"))
1014 int ofnode_read_resource(ofnode node, uint index, struct resource *res)
1016 if (ofnode_is_np(node)) {
1017 return of_address_to_resource(ofnode_to_np(node), index, res);
1019 struct fdt_resource fres;
1022 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
1023 "reg", index, &fres);
1026 memset(res, '\0', sizeof(*res));
1027 res->start = fres.start;
1028 res->end = fres.end;
1034 int ofnode_read_resource_byname(ofnode node, const char *name,
1035 struct resource *res)
1039 index = ofnode_stringlist_search(node, "reg-names", name);
1043 return ofnode_read_resource(node, index, res);
1046 u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
1048 if (ofnode_is_np(node))
1049 return of_translate_address(ofnode_to_np(node), in_addr);
1051 return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
1054 u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
1056 if (ofnode_is_np(node))
1057 return of_translate_dma_address(ofnode_to_np(node), in_addr);
1059 return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
1062 int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus, u64 *size)
1064 if (ofnode_is_np(node))
1065 return of_get_dma_range(ofnode_to_np(node), cpu, bus, size);
1067 return fdt_get_dma_range(gd->fdt_blob, ofnode_to_offset(node),
1071 int ofnode_device_is_compatible(ofnode node, const char *compat)
1073 if (ofnode_is_np(node))
1074 return of_device_is_compatible(ofnode_to_np(node), compat,
1077 return !fdt_node_check_compatible(gd->fdt_blob,
1078 ofnode_to_offset(node),
1082 ofnode ofnode_by_compatible(ofnode from, const char *compat)
1084 if (of_live_active()) {
1085 return np_to_ofnode(of_find_compatible_node(
1086 (struct device_node *)ofnode_to_np(from), NULL,
1089 return offset_to_ofnode(fdt_node_offset_by_compatible(
1090 gd->fdt_blob, ofnode_to_offset(from), compat));
1094 ofnode ofnode_by_prop_value(ofnode from, const char *propname,
1095 const void *propval, int proplen)
1097 if (of_live_active()) {
1098 return np_to_ofnode(of_find_node_by_prop_value(
1099 (struct device_node *)ofnode_to_np(from), propname,
1102 return offset_to_ofnode(fdt_node_offset_by_prop_value(
1103 gd->fdt_blob, ofnode_to_offset(from),
1104 propname, propval, proplen));
1108 int ofnode_write_prop(ofnode node, const char *propname, const void *value,
1111 const struct device_node *np = ofnode_to_np(node);
1112 struct property *pp;
1113 struct property *pp_last = NULL;
1114 struct property *new;
1116 if (!of_live_active())
1122 for (pp = np->properties; pp; pp = pp->next) {
1123 if (strcmp(pp->name, propname) == 0) {
1124 /* Property exists -> change value */
1125 pp->value = (void *)value;
1135 /* Property does not exist -> append new property */
1136 new = malloc(sizeof(struct property));
1140 new->name = strdup(propname);
1146 new->value = (void *)value;
1150 pp_last->next = new;
1155 int ofnode_write_string(ofnode node, const char *propname, const char *value)
1157 if (!of_live_active())
1160 assert(ofnode_valid(node));
1162 debug("%s: %s = %s", __func__, propname, value);
1164 return ofnode_write_prop(node, propname, value, strlen(value) + 1);
1167 int ofnode_set_enabled(ofnode node, bool value)
1169 if (!of_live_active())
1172 assert(ofnode_valid(node));
1175 return ofnode_write_string(node, "status", "okay");
1177 return ofnode_write_string(node, "status", "disabled");
1180 bool ofnode_conf_read_bool(const char *prop_name)
1184 node = ofnode_path("/config");
1185 if (!ofnode_valid(node))
1188 return ofnode_read_bool(node, prop_name);
1191 int ofnode_conf_read_int(const char *prop_name, int default_val)
1195 node = ofnode_path("/config");
1196 if (!ofnode_valid(node))
1199 return ofnode_read_u32_default(node, prop_name, default_val);
1202 const char *ofnode_conf_read_str(const char *prop_name)
1206 node = ofnode_path("/config");
1207 if (!ofnode_valid(node))
1210 return ofnode_read_string(node, prop_name);
1213 ofnode ofnode_get_phy_node(ofnode node)
1215 /* DT node properties that reference a PHY node */
1216 static const char * const phy_handle_str[] = {
1217 "phy-handle", "phy", "phy-device",
1219 struct ofnode_phandle_args args = {
1220 .node = ofnode_null()
1224 assert(ofnode_valid(node));
1226 for (i = 0; i < ARRAY_SIZE(phy_handle_str); i++)
1227 if (!ofnode_parse_phandle_with_args(node, phy_handle_str[i],
1234 phy_interface_t ofnode_read_phy_mode(ofnode node)
1239 assert(ofnode_valid(node));
1241 mode = ofnode_read_string(node, "phy-mode");
1243 mode = ofnode_read_string(node, "phy-connection-type");
1246 return PHY_INTERFACE_MODE_NA;
1248 for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
1249 if (!strcmp(mode, phy_interface_strings[i]))
1252 debug("%s: Invalid PHY interface '%s'\n", __func__, mode);
1254 return PHY_INTERFACE_MODE_NA;