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>
20 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
22 return ofnode_read_u32_index(node, propname, 0, outp);
25 u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
27 assert(ofnode_valid(node));
28 ofnode_read_u32_index(node, propname, 0, &def);
33 int ofnode_read_u32_index(ofnode node, const char *propname, int index,
39 assert(ofnode_valid(node));
40 debug("%s: %s: ", __func__, propname);
42 if (ofnode_is_np(node))
43 return of_read_u32_index(ofnode_to_np(node), propname, index,
46 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
49 debug("(not found)\n");
53 if (len < (sizeof(int) * (index + 1))) {
54 debug("(not large enough)\n");
58 *outp = fdt32_to_cpu(cell[index]);
59 debug("%#x (%d)\n", *outp, *outp);
64 u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
67 assert(ofnode_valid(node));
68 ofnode_read_u32_index(node, propname, index, &def);
73 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
75 assert(ofnode_valid(node));
76 ofnode_read_u32(node, propname, (u32 *)&def);
81 int ofnode_read_u64(ofnode node, const char *propname, u64 *outp)
83 const unaligned_fdt64_t *cell;
86 assert(ofnode_valid(node));
87 debug("%s: %s: ", __func__, propname);
89 if (ofnode_is_np(node))
90 return of_read_u64(ofnode_to_np(node), propname, outp);
92 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
94 if (!cell || len < sizeof(*cell)) {
95 debug("(not found)\n");
98 *outp = fdt64_to_cpu(cell[0]);
99 debug("%#llx (%lld)\n", (unsigned long long)*outp,
100 (unsigned long long)*outp);
105 u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def)
107 assert(ofnode_valid(node));
108 ofnode_read_u64(node, propname, &def);
113 bool ofnode_read_bool(ofnode node, const char *propname)
117 assert(ofnode_valid(node));
118 debug("%s: %s: ", __func__, propname);
120 prop = ofnode_get_property(node, propname, NULL);
122 debug("%s\n", prop ? "true" : "false");
124 return prop ? true : false;
127 const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep)
129 const char *val = NULL;
132 assert(ofnode_valid(node));
133 debug("%s: %s: ", __func__, propname);
135 if (ofnode_is_np(node)) {
136 struct property *prop = of_find_property(
137 ofnode_to_np(node), propname, &len);
144 val = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
148 debug("<not found>\n");
150 *sizep = -FDT_ERR_NOTFOUND;
159 const char *ofnode_read_string(ofnode node, const char *propname)
164 str = ofnode_read_prop(node, propname, &len);
168 if (strnlen(str, len) >= len) {
169 debug("<invalid>\n");
177 int ofnode_read_size(ofnode node, const char *propname)
181 if (!ofnode_read_prop(node, propname, &len))
187 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
191 assert(ofnode_valid(node));
192 debug("%s: %s: ", __func__, subnode_name);
194 if (ofnode_is_np(node)) {
195 const struct device_node *np = ofnode_to_np(node);
197 for (np = np->child; np; np = np->sibling) {
198 if (!strcmp(subnode_name, np->name))
201 subnode = np_to_ofnode(np);
203 int ooffset = fdt_subnode_offset(gd->fdt_blob,
204 ofnode_to_offset(node), subnode_name);
205 subnode = offset_to_ofnode(ooffset);
207 debug("%s\n", ofnode_valid(subnode) ?
208 ofnode_get_name(subnode) : "<none>");
213 int ofnode_read_u32_array(ofnode node, const char *propname,
214 u32 *out_values, size_t sz)
216 assert(ofnode_valid(node));
217 debug("%s: %s: ", __func__, propname);
219 if (ofnode_is_np(node)) {
220 return of_read_u32_array(ofnode_to_np(node), propname,
223 return fdtdec_get_int_array(gd->fdt_blob,
224 ofnode_to_offset(node), propname,
229 bool ofnode_is_enabled(ofnode node)
231 if (ofnode_is_np(node)) {
232 return of_device_is_available(ofnode_to_np(node));
234 return fdtdec_get_is_enabled(gd->fdt_blob,
235 ofnode_to_offset(node));
239 ofnode ofnode_first_subnode(ofnode node)
241 assert(ofnode_valid(node));
242 if (ofnode_is_np(node))
243 return np_to_ofnode(node.np->child);
245 return offset_to_ofnode(
246 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
249 ofnode ofnode_next_subnode(ofnode node)
251 assert(ofnode_valid(node));
252 if (ofnode_is_np(node))
253 return np_to_ofnode(node.np->sibling);
255 return offset_to_ofnode(
256 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
259 ofnode ofnode_get_parent(ofnode node)
263 assert(ofnode_valid(node));
264 if (ofnode_is_np(node))
265 parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
267 parent.of_offset = fdt_parent_offset(gd->fdt_blob,
268 ofnode_to_offset(node));
273 const char *ofnode_get_name(ofnode node)
275 if (!ofnode_valid(node)) {
276 debug("%s node not valid\n", __func__);
280 if (ofnode_is_np(node))
281 return strrchr(node.np->full_name, '/') + 1;
283 return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
286 ofnode ofnode_get_by_phandle(uint phandle)
290 if (of_live_active())
291 node = np_to_ofnode(of_find_node_by_phandle(phandle));
293 node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
299 fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
303 if (ofnode_is_np(node)) {
304 const __be32 *prop_val;
308 prop_val = of_get_address(ofnode_to_np(node), index, &size64,
311 return FDT_ADDR_T_NONE;
315 ns = of_n_size_cells(ofnode_to_np(node));
317 if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
318 return of_translate_address(ofnode_to_np(node), prop_val);
320 na = of_n_addr_cells(ofnode_to_np(node));
321 return of_read_number(prop_val, na);
324 na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
325 ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
326 return fdtdec_get_addr_size_fixed(gd->fdt_blob,
327 ofnode_to_offset(node), "reg",
328 index, na, ns, size, true);
331 return FDT_ADDR_T_NONE;
334 fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
338 return ofnode_get_addr_size_index(node, index, &size);
341 fdt_addr_t ofnode_get_addr(ofnode node)
343 return ofnode_get_addr_index(node, 0);
346 int ofnode_stringlist_search(ofnode node, const char *property,
349 if (ofnode_is_np(node)) {
350 return of_property_match_string(ofnode_to_np(node),
355 ret = fdt_stringlist_search(gd->fdt_blob,
356 ofnode_to_offset(node), property,
358 if (ret == -FDT_ERR_NOTFOUND)
367 int ofnode_read_string_index(ofnode node, const char *property, int index,
370 if (ofnode_is_np(node)) {
371 return of_property_read_string_index(ofnode_to_np(node),
372 property, index, outp);
376 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
377 property, index, &len);
384 int ofnode_read_string_count(ofnode node, const char *property)
386 if (ofnode_is_np(node)) {
387 return of_property_count_strings(ofnode_to_np(node), property);
389 return fdt_stringlist_count(gd->fdt_blob,
390 ofnode_to_offset(node), property);
394 static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
395 struct ofnode_phandle_args *out)
397 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
398 out->node = offset_to_ofnode(in->node);
399 out->args_count = in->args_count;
400 memcpy(out->args, in->args, sizeof(out->args));
403 static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
404 struct ofnode_phandle_args *out)
406 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
407 out->node = np_to_ofnode(in->np);
408 out->args_count = in->args_count;
409 memcpy(out->args, in->args, sizeof(out->args));
412 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
413 const char *cells_name, int cell_count,
415 struct ofnode_phandle_args *out_args)
417 if (ofnode_is_np(node)) {
418 struct of_phandle_args args;
421 ret = of_parse_phandle_with_args(ofnode_to_np(node),
422 list_name, cells_name,
427 ofnode_from_of_phandle_args(&args, out_args);
429 struct fdtdec_phandle_args args;
432 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
433 ofnode_to_offset(node),
434 list_name, cells_name,
435 cell_count, index, &args);
438 ofnode_from_fdtdec_phandle_args(&args, out_args);
444 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
445 const char *cells_name, int cell_count)
447 if (ofnode_is_np(node))
448 return of_count_phandle_with_args(ofnode_to_np(node),
449 list_name, cells_name, cell_count);
451 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
452 ofnode_to_offset(node), list_name, cells_name,
453 cell_count, -1, NULL);
456 ofnode ofnode_path(const char *path)
458 if (of_live_active())
459 return np_to_ofnode(of_find_node_by_path(path));
461 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
464 const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
468 chosen_node = ofnode_path("/chosen");
470 return ofnode_read_prop(chosen_node, propname, sizep);
473 const char *ofnode_read_chosen_string(const char *propname)
475 return ofnode_read_chosen_prop(propname, NULL);
478 ofnode ofnode_get_chosen_node(const char *name)
482 prop = ofnode_read_chosen_prop(name, NULL);
484 return ofnode_null();
486 return ofnode_path(prop);
489 const void *ofnode_read_aliases_prop(const char *propname, int *sizep)
493 node = ofnode_path("/aliases");
495 return ofnode_read_prop(node, propname, sizep);
498 ofnode ofnode_get_aliases_node(const char *name)
502 prop = ofnode_read_aliases_prop(name, NULL);
504 return ofnode_null();
506 debug("%s: node_path: %s\n", __func__, prop);
508 return ofnode_path(prop);
511 int ofnode_get_child_count(ofnode parent)
516 ofnode_for_each_subnode(child, parent)
522 static int decode_timing_property(ofnode node, const char *name,
523 struct timing_entry *result)
527 length = ofnode_read_size(node, name);
529 debug("%s: could not find property %s\n",
530 ofnode_get_name(node), name);
534 if (length == sizeof(u32)) {
535 result->typ = ofnode_read_u32_default(node, name, 0);
536 result->min = result->typ;
537 result->max = result->typ;
539 ret = ofnode_read_u32_array(node, name, &result->min, 3);
545 int ofnode_decode_display_timing(ofnode parent, int index,
546 struct display_timing *dt)
549 ofnode timings, node;
553 timings = ofnode_find_subnode(parent, "display-timings");
554 if (!ofnode_valid(timings))
558 ofnode_for_each_subnode(node, timings) {
563 if (!ofnode_valid(node))
566 memset(dt, 0, sizeof(*dt));
568 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
569 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
570 ret |= decode_timing_property(node, "hactive", &dt->hactive);
571 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
572 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
573 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
574 ret |= decode_timing_property(node, "vactive", &dt->vactive);
575 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
576 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
579 val = ofnode_read_u32_default(node, "vsync-active", -1);
581 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
582 DISPLAY_FLAGS_VSYNC_LOW;
584 val = ofnode_read_u32_default(node, "hsync-active", -1);
586 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
587 DISPLAY_FLAGS_HSYNC_LOW;
589 val = ofnode_read_u32_default(node, "de-active", -1);
591 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
592 DISPLAY_FLAGS_DE_LOW;
594 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
596 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
597 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
600 if (ofnode_read_bool(node, "interlaced"))
601 dt->flags |= DISPLAY_FLAGS_INTERLACED;
602 if (ofnode_read_bool(node, "doublescan"))
603 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
604 if (ofnode_read_bool(node, "doubleclk"))
605 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
610 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
612 if (ofnode_is_np(node))
613 return of_get_property(ofnode_to_np(node), propname, lenp);
615 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
619 int ofnode_get_first_property(ofnode node, struct ofprop *prop)
623 if (ofnode_is_np(node)) {
624 prop->prop = of_get_first_property(ofnode_to_np(prop->node));
626 return -FDT_ERR_NOTFOUND;
629 fdt_first_property_offset(gd->fdt_blob,
630 ofnode_to_offset(prop->node));
631 if (prop->offset < 0)
638 int ofnode_get_next_property(struct ofprop *prop)
640 if (ofnode_is_np(prop->node)) {
641 prop->prop = of_get_next_property(ofnode_to_np(prop->node),
644 return -FDT_ERR_NOTFOUND;
646 prop->offset = fdt_next_property_offset(gd->fdt_blob,
648 if (prop->offset < 0)
655 const void *ofnode_get_property_by_prop(const struct ofprop *prop,
656 const char **propname, int *lenp)
658 if (ofnode_is_np(prop->node))
659 return of_get_property_by_prop(ofnode_to_np(prop->node),
660 prop->prop, propname, lenp);
662 return fdt_getprop_by_offset(gd->fdt_blob,
667 bool ofnode_is_available(ofnode node)
669 if (ofnode_is_np(node))
670 return of_device_is_available(ofnode_to_np(node));
672 return fdtdec_get_is_enabled(gd->fdt_blob,
673 ofnode_to_offset(node));
676 fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
679 if (ofnode_is_np(node)) {
682 const struct device_node *np = ofnode_to_np(node);
683 const __be32 *prop = of_get_property(np, property, &psize);
686 return FDT_ADDR_T_NONE;
687 na = of_n_addr_cells(np);
688 ns = of_n_size_cells(np);
689 *sizep = of_read_number(prop + na, ns);
691 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
692 return of_translate_address(np, prop);
694 return of_read_number(prop, na);
696 return fdtdec_get_addr_size(gd->fdt_blob,
697 ofnode_to_offset(node), property,
702 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
705 if (ofnode_is_np(node)) {
706 const struct device_node *np = ofnode_to_np(node);
708 const __be32 *prop = of_get_property(np, propname, &psize);
710 if (!prop || sz != psize)
712 return (uint8_t *)prop;
715 return fdtdec_locate_byte_array(gd->fdt_blob,
716 ofnode_to_offset(node), propname, sz);
720 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
721 const char *propname, struct fdt_pci_addr *addr)
727 debug("%s: %s: ", __func__, propname);
730 * If we follow the pci bus bindings strictly, we should check
731 * the value of the node's parent node's #address-cells and
732 * #size-cells. They need to be 3 and 2 accordingly. However,
733 * for simplicity we skip the check here.
735 cell = ofnode_get_property(node, propname, &len);
739 if ((len % FDT_PCI_REG_SIZE) == 0) {
740 int num = len / FDT_PCI_REG_SIZE;
743 for (i = 0; i < num; i++) {
744 debug("pci address #%d: %08lx %08lx %08lx\n", i,
745 (ulong)fdt32_to_cpu(cell[0]),
746 (ulong)fdt32_to_cpu(cell[1]),
747 (ulong)fdt32_to_cpu(cell[2]));
748 if ((fdt32_to_cpu(*cell) & type) == type) {
749 addr->phys_hi = fdt32_to_cpu(cell[0]);
750 addr->phys_mid = fdt32_to_cpu(cell[1]);
751 addr->phys_lo = fdt32_to_cpu(cell[2]);
755 cell += (FDT_PCI_ADDR_CELLS +
770 debug("(not found)\n");
774 int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
776 const char *list, *end;
779 list = ofnode_get_property(node, "compatible", &len);
786 if (len >= strlen("pciVVVV,DDDD")) {
787 char *s = strstr(list, "pci");
790 * check if the string is something like pciVVVV,DDDD.RR
791 * or just pciVVVV,DDDD
793 if (s && s[7] == ',' &&
794 (s[12] == '.' || s[12] == 0)) {
796 *vendor = simple_strtol(s, NULL, 16);
799 *device = simple_strtol(s, NULL, 16);
810 int ofnode_read_addr_cells(ofnode node)
812 if (ofnode_is_np(node)) {
813 return of_n_addr_cells(ofnode_to_np(node));
815 int parent = fdt_parent_offset(gd->fdt_blob,
816 ofnode_to_offset(node));
818 return fdt_address_cells(gd->fdt_blob, parent);
822 int ofnode_read_size_cells(ofnode node)
824 if (ofnode_is_np(node)) {
825 return of_n_size_cells(ofnode_to_np(node));
827 int parent = fdt_parent_offset(gd->fdt_blob,
828 ofnode_to_offset(node));
830 return fdt_size_cells(gd->fdt_blob, parent);
834 int ofnode_read_simple_addr_cells(ofnode node)
836 if (ofnode_is_np(node))
837 return of_simple_addr_cells(ofnode_to_np(node));
839 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
842 int ofnode_read_simple_size_cells(ofnode node)
844 if (ofnode_is_np(node))
845 return of_simple_size_cells(ofnode_to_np(node));
847 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
850 bool ofnode_pre_reloc(ofnode node)
852 #if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
853 /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
854 * had property dm-pre-reloc or u-boot,dm-spl/tpl.
855 * They are removed in final dtb (fdtgrep 2nd pass)
859 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
861 if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
865 * In regular builds individual spl and tpl handling both
866 * count as handled pre-relocation for later second init.
868 if (ofnode_read_bool(node, "u-boot,dm-spl") ||
869 ofnode_read_bool(node, "u-boot,dm-tpl"))
876 int ofnode_read_resource(ofnode node, uint index, struct resource *res)
878 if (ofnode_is_np(node)) {
879 return of_address_to_resource(ofnode_to_np(node), index, res);
881 struct fdt_resource fres;
884 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
885 "reg", index, &fres);
888 memset(res, '\0', sizeof(*res));
889 res->start = fres.start;
896 int ofnode_read_resource_byname(ofnode node, const char *name,
897 struct resource *res)
901 index = ofnode_stringlist_search(node, "reg-names", name);
905 return ofnode_read_resource(node, index, res);
908 u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
910 if (ofnode_is_np(node))
911 return of_translate_address(ofnode_to_np(node), in_addr);
913 return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
916 u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
918 if (ofnode_is_np(node))
919 return of_translate_dma_address(ofnode_to_np(node), in_addr);
921 return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
924 int ofnode_device_is_compatible(ofnode node, const char *compat)
926 if (ofnode_is_np(node))
927 return of_device_is_compatible(ofnode_to_np(node), compat,
930 return !fdt_node_check_compatible(gd->fdt_blob,
931 ofnode_to_offset(node),
935 ofnode ofnode_by_compatible(ofnode from, const char *compat)
937 if (of_live_active()) {
938 return np_to_ofnode(of_find_compatible_node(
939 (struct device_node *)ofnode_to_np(from), NULL,
942 return offset_to_ofnode(fdt_node_offset_by_compatible(
943 gd->fdt_blob, ofnode_to_offset(from), compat));
947 ofnode ofnode_by_prop_value(ofnode from, const char *propname,
948 const void *propval, int proplen)
950 if (of_live_active()) {
951 return np_to_ofnode(of_find_node_by_prop_value(
952 (struct device_node *)ofnode_to_np(from), propname,
955 return offset_to_ofnode(fdt_node_offset_by_prop_value(
956 gd->fdt_blob, ofnode_to_offset(from),
957 propname, propval, proplen));
961 int ofnode_write_prop(ofnode node, const char *propname, int len,
964 const struct device_node *np = ofnode_to_np(node);
966 struct property *pp_last = NULL;
967 struct property *new;
969 if (!of_live_active())
975 for (pp = np->properties; pp; pp = pp->next) {
976 if (strcmp(pp->name, propname) == 0) {
977 /* Property exists -> change value */
978 pp->value = (void *)value;
988 /* Property does not exist -> append new property */
989 new = malloc(sizeof(struct property));
993 new->name = strdup(propname);
999 new->value = (void *)value;
1003 pp_last->next = new;
1008 int ofnode_write_string(ofnode node, const char *propname, const char *value)
1010 if (!of_live_active())
1013 assert(ofnode_valid(node));
1015 debug("%s: %s = %s", __func__, propname, value);
1017 return ofnode_write_prop(node, propname, strlen(value) + 1, value);
1020 int ofnode_set_enabled(ofnode node, bool value)
1022 if (!of_live_active())
1025 assert(ofnode_valid(node));
1028 return ofnode_write_string(node, "status", "okay");
1030 return ofnode_write_string(node, "status", "disabled");