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>
12 #include <linux/libfdt.h>
13 #include <dm/of_access.h>
14 #include <dm/of_addr.h>
15 #include <dm/ofnode.h>
16 #include <linux/err.h>
17 #include <linux/ioport.h>
19 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
21 return ofnode_read_u32_index(node, propname, 0, outp);
24 u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
26 assert(ofnode_valid(node));
27 ofnode_read_u32_index(node, propname, 0, &def);
32 int ofnode_read_u32_index(ofnode node, const char *propname, int index,
38 assert(ofnode_valid(node));
39 debug("%s: %s: ", __func__, propname);
41 if (ofnode_is_np(node))
42 return of_read_u32_index(ofnode_to_np(node), propname, index,
45 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
48 debug("(not found)\n");
52 if (len < (sizeof(int) * (index + 1))) {
53 debug("(not large enough)\n");
57 *outp = fdt32_to_cpu(cell[index]);
58 debug("%#x (%d)\n", *outp, *outp);
63 u32 ofnode_read_u32_index_default(ofnode node, const char *propname, int index,
66 assert(ofnode_valid(node));
67 ofnode_read_u32_index(node, propname, index, &def);
72 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
74 assert(ofnode_valid(node));
75 ofnode_read_u32(node, propname, (u32 *)&def);
80 int ofnode_read_u64(ofnode node, const char *propname, u64 *outp)
82 const unaligned_fdt64_t *cell;
85 assert(ofnode_valid(node));
86 debug("%s: %s: ", __func__, propname);
88 if (ofnode_is_np(node))
89 return of_read_u64(ofnode_to_np(node), propname, outp);
91 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
93 if (!cell || len < sizeof(*cell)) {
94 debug("(not found)\n");
97 *outp = fdt64_to_cpu(cell[0]);
98 debug("%#llx (%lld)\n", (unsigned long long)*outp,
99 (unsigned long long)*outp);
104 u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def)
106 assert(ofnode_valid(node));
107 ofnode_read_u64(node, propname, &def);
112 bool ofnode_read_bool(ofnode node, const char *propname)
116 assert(ofnode_valid(node));
117 debug("%s: %s: ", __func__, propname);
119 prop = ofnode_get_property(node, propname, NULL);
121 debug("%s\n", prop ? "true" : "false");
123 return prop ? true : false;
126 const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep)
128 const char *val = NULL;
131 assert(ofnode_valid(node));
132 debug("%s: %s: ", __func__, propname);
134 if (ofnode_is_np(node)) {
135 struct property *prop = of_find_property(
136 ofnode_to_np(node), propname, &len);
143 val = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
147 debug("<not found>\n");
149 *sizep = -FDT_ERR_NOTFOUND;
158 const char *ofnode_read_string(ofnode node, const char *propname)
163 str = ofnode_read_prop(node, propname, &len);
167 if (strnlen(str, len) >= len) {
168 debug("<invalid>\n");
176 int ofnode_read_size(ofnode node, const char *propname)
180 if (!ofnode_read_prop(node, propname, &len))
186 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
190 assert(ofnode_valid(node));
191 debug("%s: %s: ", __func__, subnode_name);
193 if (ofnode_is_np(node)) {
194 const struct device_node *np = ofnode_to_np(node);
196 for (np = np->child; np; np = np->sibling) {
197 if (!strcmp(subnode_name, np->name))
200 subnode = np_to_ofnode(np);
202 int ooffset = fdt_subnode_offset(gd->fdt_blob,
203 ofnode_to_offset(node), subnode_name);
204 subnode = offset_to_ofnode(ooffset);
206 debug("%s\n", ofnode_valid(subnode) ?
207 ofnode_get_name(subnode) : "<none>");
212 int ofnode_read_u32_array(ofnode node, const char *propname,
213 u32 *out_values, size_t sz)
215 assert(ofnode_valid(node));
216 debug("%s: %s: ", __func__, propname);
218 if (ofnode_is_np(node)) {
219 return of_read_u32_array(ofnode_to_np(node), propname,
222 return fdtdec_get_int_array(gd->fdt_blob,
223 ofnode_to_offset(node), propname,
228 ofnode ofnode_first_subnode(ofnode node)
230 assert(ofnode_valid(node));
231 if (ofnode_is_np(node))
232 return np_to_ofnode(node.np->child);
234 return offset_to_ofnode(
235 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
238 ofnode ofnode_next_subnode(ofnode node)
240 assert(ofnode_valid(node));
241 if (ofnode_is_np(node))
242 return np_to_ofnode(node.np->sibling);
244 return offset_to_ofnode(
245 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
248 ofnode ofnode_get_parent(ofnode node)
252 assert(ofnode_valid(node));
253 if (ofnode_is_np(node))
254 parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
256 parent.of_offset = fdt_parent_offset(gd->fdt_blob,
257 ofnode_to_offset(node));
262 const char *ofnode_get_name(ofnode node)
264 if (!ofnode_valid(node)) {
265 debug("%s node not valid\n", __func__);
269 if (ofnode_is_np(node))
270 return strrchr(node.np->full_name, '/') + 1;
272 return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
275 ofnode ofnode_get_by_phandle(uint phandle)
279 if (of_live_active())
280 node = np_to_ofnode(of_find_node_by_phandle(phandle));
282 node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
288 fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
292 if (ofnode_is_np(node)) {
293 const __be32 *prop_val;
297 prop_val = of_get_address(ofnode_to_np(node), index, &size64,
300 return FDT_ADDR_T_NONE;
304 ns = of_n_size_cells(ofnode_to_np(node));
306 if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
307 return of_translate_address(ofnode_to_np(node), prop_val);
309 na = of_n_addr_cells(ofnode_to_np(node));
310 return of_read_number(prop_val, na);
313 na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
314 ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
315 return fdtdec_get_addr_size_fixed(gd->fdt_blob,
316 ofnode_to_offset(node), "reg",
317 index, na, ns, size, true);
320 return FDT_ADDR_T_NONE;
323 fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
327 return ofnode_get_addr_size_index(node, index, &size);
330 fdt_addr_t ofnode_get_addr(ofnode node)
332 return ofnode_get_addr_index(node, 0);
335 int ofnode_stringlist_search(ofnode node, const char *property,
338 if (ofnode_is_np(node)) {
339 return of_property_match_string(ofnode_to_np(node),
344 ret = fdt_stringlist_search(gd->fdt_blob,
345 ofnode_to_offset(node), property,
347 if (ret == -FDT_ERR_NOTFOUND)
356 int ofnode_read_string_index(ofnode node, const char *property, int index,
359 if (ofnode_is_np(node)) {
360 return of_property_read_string_index(ofnode_to_np(node),
361 property, index, outp);
365 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
366 property, index, &len);
373 int ofnode_read_string_count(ofnode node, const char *property)
375 if (ofnode_is_np(node)) {
376 return of_property_count_strings(ofnode_to_np(node), property);
378 return fdt_stringlist_count(gd->fdt_blob,
379 ofnode_to_offset(node), property);
383 static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
384 struct ofnode_phandle_args *out)
386 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
387 out->node = offset_to_ofnode(in->node);
388 out->args_count = in->args_count;
389 memcpy(out->args, in->args, sizeof(out->args));
392 static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
393 struct ofnode_phandle_args *out)
395 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
396 out->node = np_to_ofnode(in->np);
397 out->args_count = in->args_count;
398 memcpy(out->args, in->args, sizeof(out->args));
401 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
402 const char *cells_name, int cell_count,
404 struct ofnode_phandle_args *out_args)
406 if (ofnode_is_np(node)) {
407 struct of_phandle_args args;
410 ret = of_parse_phandle_with_args(ofnode_to_np(node),
411 list_name, cells_name, index,
415 ofnode_from_of_phandle_args(&args, out_args);
417 struct fdtdec_phandle_args args;
420 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
421 ofnode_to_offset(node),
422 list_name, cells_name,
423 cell_count, index, &args);
426 ofnode_from_fdtdec_phandle_args(&args, out_args);
432 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
433 const char *cells_name)
435 if (ofnode_is_np(node))
436 return of_count_phandle_with_args(ofnode_to_np(node),
437 list_name, cells_name);
439 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
440 ofnode_to_offset(node), list_name, cells_name,
444 ofnode ofnode_path(const char *path)
446 if (of_live_active())
447 return np_to_ofnode(of_find_node_by_path(path));
449 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
452 const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
456 chosen_node = ofnode_path("/chosen");
458 return ofnode_read_prop(chosen_node, propname, sizep);
461 const char *ofnode_read_chosen_string(const char *propname)
463 return ofnode_read_chosen_prop(propname, NULL);
466 ofnode ofnode_get_chosen_node(const char *name)
470 prop = ofnode_read_chosen_prop(name, NULL);
472 return ofnode_null();
474 return ofnode_path(prop);
477 static int decode_timing_property(ofnode node, const char *name,
478 struct timing_entry *result)
482 length = ofnode_read_size(node, name);
484 debug("%s: could not find property %s\n",
485 ofnode_get_name(node), name);
489 if (length == sizeof(u32)) {
490 result->typ = ofnode_read_u32_default(node, name, 0);
491 result->min = result->typ;
492 result->max = result->typ;
494 ret = ofnode_read_u32_array(node, name, &result->min, 3);
500 int ofnode_decode_display_timing(ofnode parent, int index,
501 struct display_timing *dt)
504 ofnode timings, node;
508 timings = ofnode_find_subnode(parent, "display-timings");
509 if (!ofnode_valid(timings))
513 ofnode_for_each_subnode(node, timings) {
518 if (!ofnode_valid(node))
521 memset(dt, 0, sizeof(*dt));
523 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
524 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
525 ret |= decode_timing_property(node, "hactive", &dt->hactive);
526 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
527 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
528 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
529 ret |= decode_timing_property(node, "vactive", &dt->vactive);
530 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
531 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
534 val = ofnode_read_u32_default(node, "vsync-active", -1);
536 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
537 DISPLAY_FLAGS_VSYNC_LOW;
539 val = ofnode_read_u32_default(node, "hsync-active", -1);
541 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
542 DISPLAY_FLAGS_HSYNC_LOW;
544 val = ofnode_read_u32_default(node, "de-active", -1);
546 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
547 DISPLAY_FLAGS_DE_LOW;
549 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
551 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
552 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
555 if (ofnode_read_bool(node, "interlaced"))
556 dt->flags |= DISPLAY_FLAGS_INTERLACED;
557 if (ofnode_read_bool(node, "doublescan"))
558 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
559 if (ofnode_read_bool(node, "doubleclk"))
560 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
565 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
567 if (ofnode_is_np(node))
568 return of_get_property(ofnode_to_np(node), propname, lenp);
570 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
574 int ofnode_get_first_property(ofnode node, struct ofprop *prop)
578 if (ofnode_is_np(node)) {
579 prop->prop = of_get_first_property(ofnode_to_np(prop->node));
581 return -FDT_ERR_NOTFOUND;
584 fdt_first_property_offset(gd->fdt_blob,
585 ofnode_to_offset(prop->node));
586 if (prop->offset < 0)
593 int ofnode_get_next_property(struct ofprop *prop)
595 if (ofnode_is_np(prop->node)) {
596 prop->prop = of_get_next_property(ofnode_to_np(prop->node),
599 return -FDT_ERR_NOTFOUND;
601 prop->offset = fdt_next_property_offset(gd->fdt_blob,
603 if (prop->offset < 0)
610 const void *ofnode_get_property_by_prop(const struct ofprop *prop,
611 const char **propname, int *lenp)
613 if (ofnode_is_np(prop->node))
614 return of_get_property_by_prop(ofnode_to_np(prop->node),
615 prop->prop, propname, lenp);
617 return fdt_getprop_by_offset(gd->fdt_blob,
622 bool ofnode_is_available(ofnode node)
624 if (ofnode_is_np(node))
625 return of_device_is_available(ofnode_to_np(node));
627 return fdtdec_get_is_enabled(gd->fdt_blob,
628 ofnode_to_offset(node));
631 fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
634 if (ofnode_is_np(node)) {
637 const struct device_node *np = ofnode_to_np(node);
638 const __be32 *prop = of_get_property(np, property, &psize);
641 return FDT_ADDR_T_NONE;
642 na = of_n_addr_cells(np);
643 ns = of_n_size_cells(np);
644 *sizep = of_read_number(prop + na, ns);
646 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
647 return of_translate_address(np, prop);
649 return of_read_number(prop, na);
651 return fdtdec_get_addr_size(gd->fdt_blob,
652 ofnode_to_offset(node), property,
657 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
660 if (ofnode_is_np(node)) {
661 const struct device_node *np = ofnode_to_np(node);
663 const __be32 *prop = of_get_property(np, propname, &psize);
665 if (!prop || sz != psize)
667 return (uint8_t *)prop;
670 return fdtdec_locate_byte_array(gd->fdt_blob,
671 ofnode_to_offset(node), propname, sz);
675 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
676 const char *propname, struct fdt_pci_addr *addr)
682 debug("%s: %s: ", __func__, propname);
685 * If we follow the pci bus bindings strictly, we should check
686 * the value of the node's parent node's #address-cells and
687 * #size-cells. They need to be 3 and 2 accordingly. However,
688 * for simplicity we skip the check here.
690 cell = ofnode_get_property(node, propname, &len);
694 if ((len % FDT_PCI_REG_SIZE) == 0) {
695 int num = len / FDT_PCI_REG_SIZE;
698 for (i = 0; i < num; i++) {
699 debug("pci address #%d: %08lx %08lx %08lx\n", i,
700 (ulong)fdt32_to_cpu(cell[0]),
701 (ulong)fdt32_to_cpu(cell[1]),
702 (ulong)fdt32_to_cpu(cell[2]));
703 if ((fdt32_to_cpu(*cell) & type) == type) {
704 addr->phys_hi = fdt32_to_cpu(cell[0]);
705 addr->phys_mid = fdt32_to_cpu(cell[1]);
706 addr->phys_lo = fdt32_to_cpu(cell[2]);
710 cell += (FDT_PCI_ADDR_CELLS +
725 debug("(not found)\n");
729 int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
731 const char *list, *end;
734 list = ofnode_get_property(node, "compatible", &len);
741 if (len >= strlen("pciVVVV,DDDD")) {
742 char *s = strstr(list, "pci");
745 * check if the string is something like pciVVVV,DDDD.RR
746 * or just pciVVVV,DDDD
748 if (s && s[7] == ',' &&
749 (s[12] == '.' || s[12] == 0)) {
751 *vendor = simple_strtol(s, NULL, 16);
754 *device = simple_strtol(s, NULL, 16);
765 int ofnode_read_addr_cells(ofnode node)
767 if (ofnode_is_np(node))
768 return of_n_addr_cells(ofnode_to_np(node));
769 else /* NOTE: this call should walk up the parent stack */
770 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
773 int ofnode_read_size_cells(ofnode node)
775 if (ofnode_is_np(node))
776 return of_n_size_cells(ofnode_to_np(node));
777 else /* NOTE: this call should walk up the parent stack */
778 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
781 int ofnode_read_simple_addr_cells(ofnode node)
783 if (ofnode_is_np(node))
784 return of_simple_addr_cells(ofnode_to_np(node));
786 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
789 int ofnode_read_simple_size_cells(ofnode node)
791 if (ofnode_is_np(node))
792 return of_simple_size_cells(ofnode_to_np(node));
794 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
797 bool ofnode_pre_reloc(ofnode node)
799 #if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
800 /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
801 * had property dm-pre-reloc or u-boot,dm-spl/tpl.
802 * They are removed in final dtb (fdtgrep 2nd pass)
806 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
808 if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
812 * In regular builds individual spl and tpl handling both
813 * count as handled pre-relocation for later second init.
815 if (ofnode_read_bool(node, "u-boot,dm-spl") ||
816 ofnode_read_bool(node, "u-boot,dm-tpl"))
823 int ofnode_read_resource(ofnode node, uint index, struct resource *res)
825 if (ofnode_is_np(node)) {
826 return of_address_to_resource(ofnode_to_np(node), index, res);
828 struct fdt_resource fres;
831 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
832 "reg", index, &fres);
835 memset(res, '\0', sizeof(*res));
836 res->start = fres.start;
843 int ofnode_read_resource_byname(ofnode node, const char *name,
844 struct resource *res)
848 index = ofnode_stringlist_search(node, "reg-names", name);
852 return ofnode_read_resource(node, index, res);
855 u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
857 if (ofnode_is_np(node))
858 return of_translate_address(ofnode_to_np(node), in_addr);
860 return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
863 u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
865 if (ofnode_is_np(node))
866 return of_translate_dma_address(ofnode_to_np(node), in_addr);
868 return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
871 int ofnode_device_is_compatible(ofnode node, const char *compat)
873 if (ofnode_is_np(node))
874 return of_device_is_compatible(ofnode_to_np(node), compat,
877 return !fdt_node_check_compatible(gd->fdt_blob,
878 ofnode_to_offset(node),
882 ofnode ofnode_by_compatible(ofnode from, const char *compat)
884 if (of_live_active()) {
885 return np_to_ofnode(of_find_compatible_node(
886 (struct device_node *)ofnode_to_np(from), NULL,
889 return offset_to_ofnode(fdt_node_offset_by_compatible(
890 gd->fdt_blob, ofnode_to_offset(from), compat));
894 ofnode ofnode_by_prop_value(ofnode from, const char *propname,
895 const void *propval, int proplen)
897 if (of_live_active()) {
898 return np_to_ofnode(of_find_node_by_prop_value(
899 (struct device_node *)ofnode_to_np(from), propname,
902 return offset_to_ofnode(fdt_node_offset_by_prop_value(
903 gd->fdt_blob, ofnode_to_offset(from),
904 propname, propval, proplen));
908 int ofnode_write_prop(ofnode node, const char *propname, int len,
911 const struct device_node *np = ofnode_to_np(node);
913 struct property *pp_last = NULL;
914 struct property *new;
916 if (!of_live_active())
922 for (pp = np->properties; pp; pp = pp->next) {
923 if (strcmp(pp->name, propname) == 0) {
924 /* Property exists -> change value */
925 pp->value = (void *)value;
935 /* Property does not exist -> append new property */
936 new = malloc(sizeof(struct property));
940 new->name = strdup(propname);
946 new->value = (void *)value;
955 int ofnode_write_string(ofnode node, const char *propname, const char *value)
957 if (!of_live_active())
960 assert(ofnode_valid(node));
962 debug("%s: %s = %s", __func__, propname, value);
964 return ofnode_write_prop(node, propname, strlen(value) + 1, value);
967 int ofnode_set_enabled(ofnode node, bool value)
969 if (!of_live_active())
972 assert(ofnode_valid(node));
975 return ofnode_write_string(node, "status", "okay");
977 return ofnode_write_string(node, "status", "disabled");