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>
11 #include <linux/libfdt.h>
12 #include <dm/of_access.h>
13 #include <dm/of_addr.h>
14 #include <dm/ofnode.h>
15 #include <linux/err.h>
16 #include <linux/ioport.h>
18 int ofnode_read_u32(ofnode node, const char *propname, u32 *outp)
20 assert(ofnode_valid(node));
21 debug("%s: %s: ", __func__, propname);
23 if (ofnode_is_np(node)) {
24 return of_read_u32(ofnode_to_np(node), propname, outp);
29 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
31 if (!cell || len < sizeof(int)) {
32 debug("(not found)\n");
35 *outp = fdt32_to_cpu(cell[0]);
37 debug("%#x (%d)\n", *outp, *outp);
42 u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
44 assert(ofnode_valid(node));
45 ofnode_read_u32(node, propname, &def);
50 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
52 assert(ofnode_valid(node));
53 ofnode_read_u32(node, propname, (u32 *)&def);
58 int ofnode_read_u64(ofnode node, const char *propname, u64 *outp)
60 const unaligned_fdt64_t *cell;
63 assert(ofnode_valid(node));
64 debug("%s: %s: ", __func__, propname);
66 if (ofnode_is_np(node))
67 return of_read_u64(ofnode_to_np(node), propname, outp);
69 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
71 if (!cell || len < sizeof(*cell)) {
72 debug("(not found)\n");
75 *outp = fdt64_to_cpu(cell[0]);
76 debug("%#llx (%lld)\n", (unsigned long long)*outp,
77 (unsigned long long)*outp);
82 u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def)
84 assert(ofnode_valid(node));
85 ofnode_read_u64(node, propname, &def);
90 bool ofnode_read_bool(ofnode node, const char *propname)
94 assert(ofnode_valid(node));
95 debug("%s: %s: ", __func__, propname);
97 prop = ofnode_get_property(node, propname, NULL);
99 debug("%s\n", prop ? "true" : "false");
101 return prop ? true : false;
104 const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep)
106 const char *val = NULL;
109 assert(ofnode_valid(node));
110 debug("%s: %s: ", __func__, propname);
112 if (ofnode_is_np(node)) {
113 struct property *prop = of_find_property(
114 ofnode_to_np(node), propname, &len);
121 val = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
125 debug("<not found>\n");
127 *sizep = -FDT_ERR_NOTFOUND;
136 const char *ofnode_read_string(ofnode node, const char *propname)
141 str = ofnode_read_prop(node, propname, &len);
145 if (strnlen(str, len) >= len) {
146 debug("<invalid>\n");
154 int ofnode_read_size(ofnode node, const char *propname)
158 if (!ofnode_read_prop(node, propname, &len))
164 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
168 assert(ofnode_valid(node));
169 debug("%s: %s: ", __func__, subnode_name);
171 if (ofnode_is_np(node)) {
172 const struct device_node *np = ofnode_to_np(node);
174 for (np = np->child; np; np = np->sibling) {
175 if (!strcmp(subnode_name, np->name))
178 subnode = np_to_ofnode(np);
180 int ooffset = fdt_subnode_offset(gd->fdt_blob,
181 ofnode_to_offset(node), subnode_name);
182 subnode = offset_to_ofnode(ooffset);
184 debug("%s\n", ofnode_valid(subnode) ?
185 ofnode_get_name(subnode) : "<none>");
190 int ofnode_read_u32_array(ofnode node, const char *propname,
191 u32 *out_values, size_t sz)
193 assert(ofnode_valid(node));
194 debug("%s: %s: ", __func__, propname);
196 if (ofnode_is_np(node)) {
197 return of_read_u32_array(ofnode_to_np(node), propname,
200 return fdtdec_get_int_array(gd->fdt_blob,
201 ofnode_to_offset(node), propname,
206 ofnode ofnode_first_subnode(ofnode node)
208 assert(ofnode_valid(node));
209 if (ofnode_is_np(node))
210 return np_to_ofnode(node.np->child);
212 return offset_to_ofnode(
213 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
216 ofnode ofnode_next_subnode(ofnode node)
218 assert(ofnode_valid(node));
219 if (ofnode_is_np(node))
220 return np_to_ofnode(node.np->sibling);
222 return offset_to_ofnode(
223 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
226 ofnode ofnode_get_parent(ofnode node)
230 assert(ofnode_valid(node));
231 if (ofnode_is_np(node))
232 parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
234 parent.of_offset = fdt_parent_offset(gd->fdt_blob,
235 ofnode_to_offset(node));
240 const char *ofnode_get_name(ofnode node)
242 if (!ofnode_valid(node)) {
243 debug("%s node not valid\n", __func__);
247 if (ofnode_is_np(node))
248 return strrchr(node.np->full_name, '/') + 1;
250 return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
253 ofnode ofnode_get_by_phandle(uint phandle)
257 if (of_live_active())
258 node = np_to_ofnode(of_find_node_by_phandle(phandle));
260 node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
266 fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
270 if (ofnode_is_np(node)) {
271 const __be32 *prop_val;
275 prop_val = of_get_address(ofnode_to_np(node), index, &size64,
278 return FDT_ADDR_T_NONE;
282 ns = of_n_size_cells(ofnode_to_np(node));
284 if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
285 return of_translate_address(ofnode_to_np(node), prop_val);
287 na = of_n_addr_cells(ofnode_to_np(node));
288 return of_read_number(prop_val, na);
291 na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
292 ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
293 return fdtdec_get_addr_size_fixed(gd->fdt_blob,
294 ofnode_to_offset(node), "reg",
295 index, na, ns, size, true);
298 return FDT_ADDR_T_NONE;
301 fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
305 return ofnode_get_addr_size_index(node, index, &size);
308 fdt_addr_t ofnode_get_addr(ofnode node)
310 return ofnode_get_addr_index(node, 0);
313 int ofnode_stringlist_search(ofnode node, const char *property,
316 if (ofnode_is_np(node)) {
317 return of_property_match_string(ofnode_to_np(node),
322 ret = fdt_stringlist_search(gd->fdt_blob,
323 ofnode_to_offset(node), property,
325 if (ret == -FDT_ERR_NOTFOUND)
334 int ofnode_read_string_index(ofnode node, const char *property, int index,
337 if (ofnode_is_np(node)) {
338 return of_property_read_string_index(ofnode_to_np(node),
339 property, index, outp);
343 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
344 property, index, &len);
351 int ofnode_read_string_count(ofnode node, const char *property)
353 if (ofnode_is_np(node)) {
354 return of_property_count_strings(ofnode_to_np(node), property);
356 return fdt_stringlist_count(gd->fdt_blob,
357 ofnode_to_offset(node), property);
361 static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
362 struct ofnode_phandle_args *out)
364 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
365 out->node = offset_to_ofnode(in->node);
366 out->args_count = in->args_count;
367 memcpy(out->args, in->args, sizeof(out->args));
370 static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
371 struct ofnode_phandle_args *out)
373 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
374 out->node = np_to_ofnode(in->np);
375 out->args_count = in->args_count;
376 memcpy(out->args, in->args, sizeof(out->args));
379 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
380 const char *cells_name, int cell_count,
382 struct ofnode_phandle_args *out_args)
384 if (ofnode_is_np(node)) {
385 struct of_phandle_args args;
388 ret = of_parse_phandle_with_args(ofnode_to_np(node),
389 list_name, cells_name, index,
393 ofnode_from_of_phandle_args(&args, out_args);
395 struct fdtdec_phandle_args args;
398 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
399 ofnode_to_offset(node),
400 list_name, cells_name,
401 cell_count, index, &args);
404 ofnode_from_fdtdec_phandle_args(&args, out_args);
410 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
411 const char *cells_name)
413 if (ofnode_is_np(node))
414 return of_count_phandle_with_args(ofnode_to_np(node),
415 list_name, cells_name);
417 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
418 ofnode_to_offset(node), list_name, cells_name,
422 ofnode ofnode_path(const char *path)
424 if (of_live_active())
425 return np_to_ofnode(of_find_node_by_path(path));
427 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
430 const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
434 chosen_node = ofnode_path("/chosen");
436 return ofnode_read_prop(chosen_node, propname, sizep);
439 const char *ofnode_read_chosen_string(const char *propname)
441 return ofnode_read_chosen_prop(propname, NULL);
444 ofnode ofnode_get_chosen_node(const char *name)
448 prop = ofnode_read_chosen_prop(name, NULL);
450 return ofnode_null();
452 return ofnode_path(prop);
455 static int decode_timing_property(ofnode node, const char *name,
456 struct timing_entry *result)
460 length = ofnode_read_size(node, name);
462 debug("%s: could not find property %s\n",
463 ofnode_get_name(node), name);
467 if (length == sizeof(u32)) {
468 result->typ = ofnode_read_u32_default(node, name, 0);
469 result->min = result->typ;
470 result->max = result->typ;
472 ret = ofnode_read_u32_array(node, name, &result->min, 3);
478 int ofnode_decode_display_timing(ofnode parent, int index,
479 struct display_timing *dt)
482 ofnode timings, node;
486 timings = ofnode_find_subnode(parent, "display-timings");
487 if (!ofnode_valid(timings))
491 ofnode_for_each_subnode(node, timings) {
496 if (!ofnode_valid(node))
499 memset(dt, 0, sizeof(*dt));
501 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
502 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
503 ret |= decode_timing_property(node, "hactive", &dt->hactive);
504 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
505 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
506 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
507 ret |= decode_timing_property(node, "vactive", &dt->vactive);
508 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
509 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
512 val = ofnode_read_u32_default(node, "vsync-active", -1);
514 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
515 DISPLAY_FLAGS_VSYNC_LOW;
517 val = ofnode_read_u32_default(node, "hsync-active", -1);
519 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
520 DISPLAY_FLAGS_HSYNC_LOW;
522 val = ofnode_read_u32_default(node, "de-active", -1);
524 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
525 DISPLAY_FLAGS_DE_LOW;
527 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
529 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
530 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
533 if (ofnode_read_bool(node, "interlaced"))
534 dt->flags |= DISPLAY_FLAGS_INTERLACED;
535 if (ofnode_read_bool(node, "doublescan"))
536 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
537 if (ofnode_read_bool(node, "doubleclk"))
538 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
543 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
545 if (ofnode_is_np(node))
546 return of_get_property(ofnode_to_np(node), propname, lenp);
548 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
552 bool ofnode_is_available(ofnode node)
554 if (ofnode_is_np(node))
555 return of_device_is_available(ofnode_to_np(node));
557 return fdtdec_get_is_enabled(gd->fdt_blob,
558 ofnode_to_offset(node));
561 fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
564 if (ofnode_is_np(node)) {
567 const struct device_node *np = ofnode_to_np(node);
568 const __be32 *prop = of_get_property(np, property, &psize);
571 return FDT_ADDR_T_NONE;
572 na = of_n_addr_cells(np);
573 ns = of_n_size_cells(np);
574 *sizep = of_read_number(prop + na, ns);
576 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
577 return of_translate_address(np, prop);
579 return of_read_number(prop, na);
581 return fdtdec_get_addr_size(gd->fdt_blob,
582 ofnode_to_offset(node), property,
587 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
590 if (ofnode_is_np(node)) {
591 const struct device_node *np = ofnode_to_np(node);
593 const __be32 *prop = of_get_property(np, propname, &psize);
595 if (!prop || sz != psize)
597 return (uint8_t *)prop;
600 return fdtdec_locate_byte_array(gd->fdt_blob,
601 ofnode_to_offset(node), propname, sz);
605 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
606 const char *propname, struct fdt_pci_addr *addr)
612 debug("%s: %s: ", __func__, propname);
615 * If we follow the pci bus bindings strictly, we should check
616 * the value of the node's parent node's #address-cells and
617 * #size-cells. They need to be 3 and 2 accordingly. However,
618 * for simplicity we skip the check here.
620 cell = ofnode_get_property(node, propname, &len);
624 if ((len % FDT_PCI_REG_SIZE) == 0) {
625 int num = len / FDT_PCI_REG_SIZE;
628 for (i = 0; i < num; i++) {
629 debug("pci address #%d: %08lx %08lx %08lx\n", i,
630 (ulong)fdt32_to_cpu(cell[0]),
631 (ulong)fdt32_to_cpu(cell[1]),
632 (ulong)fdt32_to_cpu(cell[2]));
633 if ((fdt32_to_cpu(*cell) & type) == type) {
634 addr->phys_hi = fdt32_to_cpu(cell[0]);
635 addr->phys_mid = fdt32_to_cpu(cell[1]);
636 addr->phys_lo = fdt32_to_cpu(cell[2]);
640 cell += (FDT_PCI_ADDR_CELLS +
655 debug("(not found)\n");
659 int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
661 const char *list, *end;
664 list = ofnode_get_property(node, "compatible", &len);
671 if (len >= strlen("pciVVVV,DDDD")) {
672 char *s = strstr(list, "pci");
675 * check if the string is something like pciVVVV,DDDD.RR
676 * or just pciVVVV,DDDD
678 if (s && s[7] == ',' &&
679 (s[12] == '.' || s[12] == 0)) {
681 *vendor = simple_strtol(s, NULL, 16);
684 *device = simple_strtol(s, NULL, 16);
695 int ofnode_read_addr_cells(ofnode node)
697 if (ofnode_is_np(node))
698 return of_n_addr_cells(ofnode_to_np(node));
699 else /* NOTE: this call should walk up the parent stack */
700 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
703 int ofnode_read_size_cells(ofnode node)
705 if (ofnode_is_np(node))
706 return of_n_size_cells(ofnode_to_np(node));
707 else /* NOTE: this call should walk up the parent stack */
708 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
711 int ofnode_read_simple_addr_cells(ofnode node)
713 if (ofnode_is_np(node))
714 return of_simple_addr_cells(ofnode_to_np(node));
716 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
719 int ofnode_read_simple_size_cells(ofnode node)
721 if (ofnode_is_np(node))
722 return of_simple_size_cells(ofnode_to_np(node));
724 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
727 bool ofnode_pre_reloc(ofnode node)
729 #if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
730 /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
731 * had property dm-pre-reloc or u-boot,dm-spl/tpl.
732 * They are removed in final dtb (fdtgrep 2nd pass)
736 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
738 if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
742 * In regular builds individual spl and tpl handling both
743 * count as handled pre-relocation for later second init.
745 if (ofnode_read_bool(node, "u-boot,dm-spl") ||
746 ofnode_read_bool(node, "u-boot,dm-tpl"))
753 int ofnode_read_resource(ofnode node, uint index, struct resource *res)
755 if (ofnode_is_np(node)) {
756 return of_address_to_resource(ofnode_to_np(node), index, res);
758 struct fdt_resource fres;
761 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
762 "reg", index, &fres);
765 memset(res, '\0', sizeof(*res));
766 res->start = fres.start;
773 int ofnode_read_resource_byname(ofnode node, const char *name,
774 struct resource *res)
778 index = ofnode_stringlist_search(node, "reg-names", name);
782 return ofnode_read_resource(node, index, res);
785 u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
787 if (ofnode_is_np(node))
788 return of_translate_address(ofnode_to_np(node), in_addr);
790 return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
793 u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
795 if (ofnode_is_np(node))
796 return of_translate_dma_address(ofnode_to_np(node), in_addr);
798 return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
801 int ofnode_device_is_compatible(ofnode node, const char *compat)
803 if (ofnode_is_np(node))
804 return of_device_is_compatible(ofnode_to_np(node), compat,
807 return !fdt_node_check_compatible(gd->fdt_blob,
808 ofnode_to_offset(node),
812 ofnode ofnode_by_compatible(ofnode from, const char *compat)
814 if (of_live_active()) {
815 return np_to_ofnode(of_find_compatible_node(
816 (struct device_node *)ofnode_to_np(from), NULL,
819 return offset_to_ofnode(fdt_node_offset_by_compatible(
820 gd->fdt_blob, ofnode_to_offset(from), compat));
824 ofnode ofnode_by_prop_value(ofnode from, const char *propname,
825 const void *propval, int proplen)
827 if (of_live_active()) {
828 return np_to_ofnode(of_find_node_by_prop_value(
829 (struct device_node *)ofnode_to_np(from), propname,
832 return offset_to_ofnode(fdt_node_offset_by_prop_value(
833 gd->fdt_blob, ofnode_to_offset(from),
834 propname, propval, proplen));
838 int ofnode_write_prop(ofnode node, const char *propname, int len,
841 const struct device_node *np = ofnode_to_np(node);
843 struct property *pp_last = NULL;
844 struct property *new;
846 if (!of_live_active())
852 for (pp = np->properties; pp; pp = pp->next) {
853 if (strcmp(pp->name, propname) == 0) {
854 /* Property exists -> change value */
855 pp->value = (void *)value;
865 /* Property does not exist -> append new property */
866 new = malloc(sizeof(struct property));
870 new->name = strdup(propname);
876 new->value = (void *)value;
885 int ofnode_write_string(ofnode node, const char *propname, const char *value)
887 if (!of_live_active())
890 assert(ofnode_valid(node));
892 debug("%s: %s = %s", __func__, propname, value);
894 return ofnode_write_prop(node, propname, strlen(value) + 1, value);
897 int ofnode_set_enabled(ofnode node, bool value)
899 if (!of_live_active())
902 assert(ofnode_valid(node));
905 return ofnode_write_string(node, "status", "okay");
907 return ofnode_write_string(node, "status", "disabled");