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)
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 char *ofnode_read_string(ofnode node, const char *propname)
106 const char *str = 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, NULL);
121 str = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
125 debug("<not found>\n");
128 if (strnlen(str, len) >= len) {
129 debug("<invalid>\n");
137 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
141 assert(ofnode_valid(node));
142 debug("%s: %s: ", __func__, subnode_name);
144 if (ofnode_is_np(node)) {
145 const struct device_node *np = ofnode_to_np(node);
147 for (np = np->child; np; np = np->sibling) {
148 if (!strcmp(subnode_name, np->name))
151 subnode = np_to_ofnode(np);
153 int ooffset = fdt_subnode_offset(gd->fdt_blob,
154 ofnode_to_offset(node), subnode_name);
155 subnode = offset_to_ofnode(ooffset);
157 debug("%s\n", ofnode_valid(subnode) ?
158 ofnode_get_name(subnode) : "<none>");
163 int ofnode_read_u32_array(ofnode node, const char *propname,
164 u32 *out_values, size_t sz)
166 assert(ofnode_valid(node));
167 debug("%s: %s: ", __func__, propname);
169 if (ofnode_is_np(node)) {
170 return of_read_u32_array(ofnode_to_np(node), propname,
173 return fdtdec_get_int_array(gd->fdt_blob,
174 ofnode_to_offset(node), propname,
179 ofnode ofnode_first_subnode(ofnode node)
181 assert(ofnode_valid(node));
182 if (ofnode_is_np(node))
183 return np_to_ofnode(node.np->child);
185 return offset_to_ofnode(
186 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
189 ofnode ofnode_next_subnode(ofnode node)
191 assert(ofnode_valid(node));
192 if (ofnode_is_np(node))
193 return np_to_ofnode(node.np->sibling);
195 return offset_to_ofnode(
196 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
199 ofnode ofnode_get_parent(ofnode node)
203 assert(ofnode_valid(node));
204 if (ofnode_is_np(node))
205 parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
207 parent.of_offset = fdt_parent_offset(gd->fdt_blob,
208 ofnode_to_offset(node));
213 const char *ofnode_get_name(ofnode node)
215 if (!ofnode_valid(node)) {
216 debug("%s node not valid\n", __func__);
220 if (ofnode_is_np(node))
221 return strrchr(node.np->full_name, '/') + 1;
223 return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
226 ofnode ofnode_get_by_phandle(uint phandle)
230 if (of_live_active())
231 node = np_to_ofnode(of_find_node_by_phandle(phandle));
233 node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
239 int ofnode_read_size(ofnode node, const char *propname)
243 if (ofnode_is_np(node)) {
244 struct property *prop = of_find_property(
245 ofnode_to_np(node), propname, NULL);
250 if (fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
258 fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
262 if (ofnode_is_np(node)) {
263 const __be32 *prop_val;
267 prop_val = of_get_address(ofnode_to_np(node), index, &size64,
270 return FDT_ADDR_T_NONE;
274 ns = of_n_size_cells(ofnode_to_np(node));
276 if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
277 return of_translate_address(ofnode_to_np(node), prop_val);
279 na = of_n_addr_cells(ofnode_to_np(node));
280 return of_read_number(prop_val, na);
283 na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
284 ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
285 return fdtdec_get_addr_size_fixed(gd->fdt_blob,
286 ofnode_to_offset(node), "reg",
287 index, na, ns, size, true);
290 return FDT_ADDR_T_NONE;
293 fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
297 return ofnode_get_addr_size_index(node, index, &size);
300 fdt_addr_t ofnode_get_addr(ofnode node)
302 return ofnode_get_addr_index(node, 0);
305 int ofnode_stringlist_search(ofnode node, const char *property,
308 if (ofnode_is_np(node)) {
309 return of_property_match_string(ofnode_to_np(node),
314 ret = fdt_stringlist_search(gd->fdt_blob,
315 ofnode_to_offset(node), property,
317 if (ret == -FDT_ERR_NOTFOUND)
326 int ofnode_read_string_index(ofnode node, const char *property, int index,
329 if (ofnode_is_np(node)) {
330 return of_property_read_string_index(ofnode_to_np(node),
331 property, index, outp);
335 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
336 property, index, &len);
343 int ofnode_read_string_count(ofnode node, const char *property)
345 if (ofnode_is_np(node)) {
346 return of_property_count_strings(ofnode_to_np(node), property);
348 return fdt_stringlist_count(gd->fdt_blob,
349 ofnode_to_offset(node), property);
353 static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
354 struct ofnode_phandle_args *out)
356 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
357 out->node = offset_to_ofnode(in->node);
358 out->args_count = in->args_count;
359 memcpy(out->args, in->args, sizeof(out->args));
362 static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
363 struct ofnode_phandle_args *out)
365 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
366 out->node = np_to_ofnode(in->np);
367 out->args_count = in->args_count;
368 memcpy(out->args, in->args, sizeof(out->args));
371 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
372 const char *cells_name, int cell_count,
374 struct ofnode_phandle_args *out_args)
376 if (ofnode_is_np(node)) {
377 struct of_phandle_args args;
380 ret = of_parse_phandle_with_args(ofnode_to_np(node),
381 list_name, cells_name, index,
385 ofnode_from_of_phandle_args(&args, out_args);
387 struct fdtdec_phandle_args args;
390 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
391 ofnode_to_offset(node),
392 list_name, cells_name,
393 cell_count, index, &args);
396 ofnode_from_fdtdec_phandle_args(&args, out_args);
402 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
403 const char *cells_name)
405 if (ofnode_is_np(node))
406 return of_count_phandle_with_args(ofnode_to_np(node),
407 list_name, cells_name);
409 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
410 ofnode_to_offset(node), list_name, cells_name,
414 ofnode ofnode_path(const char *path)
416 if (of_live_active())
417 return np_to_ofnode(of_find_node_by_path(path));
419 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
422 const char *ofnode_get_chosen_prop(const char *name)
426 chosen_node = ofnode_path("/chosen");
428 return ofnode_read_string(chosen_node, name);
431 ofnode ofnode_get_chosen_node(const char *name)
435 prop = ofnode_get_chosen_prop(name);
437 return ofnode_null();
439 return ofnode_path(prop);
442 static int decode_timing_property(ofnode node, const char *name,
443 struct timing_entry *result)
447 length = ofnode_read_size(node, name);
449 debug("%s: could not find property %s\n",
450 ofnode_get_name(node), name);
454 if (length == sizeof(u32)) {
455 result->typ = ofnode_read_u32_default(node, name, 0);
456 result->min = result->typ;
457 result->max = result->typ;
459 ret = ofnode_read_u32_array(node, name, &result->min, 3);
465 int ofnode_decode_display_timing(ofnode parent, int index,
466 struct display_timing *dt)
469 ofnode timings, node;
473 timings = ofnode_find_subnode(parent, "display-timings");
474 if (!ofnode_valid(timings))
478 ofnode_for_each_subnode(node, timings) {
483 if (!ofnode_valid(node))
486 memset(dt, 0, sizeof(*dt));
488 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
489 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
490 ret |= decode_timing_property(node, "hactive", &dt->hactive);
491 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
492 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
493 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
494 ret |= decode_timing_property(node, "vactive", &dt->vactive);
495 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
496 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
499 val = ofnode_read_u32_default(node, "vsync-active", -1);
501 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
502 DISPLAY_FLAGS_VSYNC_LOW;
504 val = ofnode_read_u32_default(node, "hsync-active", -1);
506 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
507 DISPLAY_FLAGS_HSYNC_LOW;
509 val = ofnode_read_u32_default(node, "de-active", -1);
511 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
512 DISPLAY_FLAGS_DE_LOW;
514 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
516 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
517 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
520 if (ofnode_read_bool(node, "interlaced"))
521 dt->flags |= DISPLAY_FLAGS_INTERLACED;
522 if (ofnode_read_bool(node, "doublescan"))
523 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
524 if (ofnode_read_bool(node, "doubleclk"))
525 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
530 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
532 if (ofnode_is_np(node))
533 return of_get_property(ofnode_to_np(node), propname, lenp);
535 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
539 bool ofnode_is_available(ofnode node)
541 if (ofnode_is_np(node))
542 return of_device_is_available(ofnode_to_np(node));
544 return fdtdec_get_is_enabled(gd->fdt_blob,
545 ofnode_to_offset(node));
548 fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
551 if (ofnode_is_np(node)) {
554 const struct device_node *np = ofnode_to_np(node);
555 const __be32 *prop = of_get_property(np, property, &psize);
558 return FDT_ADDR_T_NONE;
559 na = of_n_addr_cells(np);
560 ns = of_n_size_cells(np);
561 *sizep = of_read_number(prop + na, ns);
563 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
564 return of_translate_address(np, prop);
566 return of_read_number(prop, na);
568 return fdtdec_get_addr_size(gd->fdt_blob,
569 ofnode_to_offset(node), property,
574 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
577 if (ofnode_is_np(node)) {
578 const struct device_node *np = ofnode_to_np(node);
580 const __be32 *prop = of_get_property(np, propname, &psize);
582 if (!prop || sz != psize)
584 return (uint8_t *)prop;
587 return fdtdec_locate_byte_array(gd->fdt_blob,
588 ofnode_to_offset(node), propname, sz);
592 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
593 const char *propname, struct fdt_pci_addr *addr)
599 debug("%s: %s: ", __func__, propname);
602 * If we follow the pci bus bindings strictly, we should check
603 * the value of the node's parent node's #address-cells and
604 * #size-cells. They need to be 3 and 2 accordingly. However,
605 * for simplicity we skip the check here.
607 cell = ofnode_get_property(node, propname, &len);
611 if ((len % FDT_PCI_REG_SIZE) == 0) {
612 int num = len / FDT_PCI_REG_SIZE;
615 for (i = 0; i < num; i++) {
616 debug("pci address #%d: %08lx %08lx %08lx\n", i,
617 (ulong)fdt32_to_cpu(cell[0]),
618 (ulong)fdt32_to_cpu(cell[1]),
619 (ulong)fdt32_to_cpu(cell[2]));
620 if ((fdt32_to_cpu(*cell) & type) == type) {
621 addr->phys_hi = fdt32_to_cpu(cell[0]);
622 addr->phys_mid = fdt32_to_cpu(cell[1]);
623 addr->phys_lo = fdt32_to_cpu(cell[2]);
627 cell += (FDT_PCI_ADDR_CELLS +
642 debug("(not found)\n");
646 int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
648 const char *list, *end;
651 list = ofnode_get_property(node, "compatible", &len);
658 if (len >= strlen("pciVVVV,DDDD")) {
659 char *s = strstr(list, "pci");
662 * check if the string is something like pciVVVV,DDDD.RR
663 * or just pciVVVV,DDDD
665 if (s && s[7] == ',' &&
666 (s[12] == '.' || s[12] == 0)) {
668 *vendor = simple_strtol(s, NULL, 16);
671 *device = simple_strtol(s, NULL, 16);
682 int ofnode_read_addr_cells(ofnode node)
684 if (ofnode_is_np(node))
685 return of_n_addr_cells(ofnode_to_np(node));
686 else /* NOTE: this call should walk up the parent stack */
687 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
690 int ofnode_read_size_cells(ofnode node)
692 if (ofnode_is_np(node))
693 return of_n_size_cells(ofnode_to_np(node));
694 else /* NOTE: this call should walk up the parent stack */
695 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
698 int ofnode_read_simple_addr_cells(ofnode node)
700 if (ofnode_is_np(node))
701 return of_simple_addr_cells(ofnode_to_np(node));
703 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
706 int ofnode_read_simple_size_cells(ofnode node)
708 if (ofnode_is_np(node))
709 return of_simple_size_cells(ofnode_to_np(node));
711 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
714 bool ofnode_pre_reloc(ofnode node)
716 #if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
717 /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
718 * had property dm-pre-reloc or u-boot,dm-spl/tpl.
719 * They are removed in final dtb (fdtgrep 2nd pass)
723 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
725 if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
729 * In regular builds individual spl and tpl handling both
730 * count as handled pre-relocation for later second init.
732 if (ofnode_read_bool(node, "u-boot,dm-spl") ||
733 ofnode_read_bool(node, "u-boot,dm-tpl"))
740 int ofnode_read_resource(ofnode node, uint index, struct resource *res)
742 if (ofnode_is_np(node)) {
743 return of_address_to_resource(ofnode_to_np(node), index, res);
745 struct fdt_resource fres;
748 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
749 "reg", index, &fres);
752 memset(res, '\0', sizeof(*res));
753 res->start = fres.start;
760 int ofnode_read_resource_byname(ofnode node, const char *name,
761 struct resource *res)
765 index = ofnode_stringlist_search(node, "reg-names", name);
769 return ofnode_read_resource(node, index, res);
772 u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
774 if (ofnode_is_np(node))
775 return of_translate_address(ofnode_to_np(node), in_addr);
777 return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
780 u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
782 if (ofnode_is_np(node))
783 return of_translate_dma_address(ofnode_to_np(node), in_addr);
785 return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
788 int ofnode_device_is_compatible(ofnode node, const char *compat)
790 if (ofnode_is_np(node))
791 return of_device_is_compatible(ofnode_to_np(node), compat,
794 return !fdt_node_check_compatible(gd->fdt_blob,
795 ofnode_to_offset(node),
799 ofnode ofnode_by_compatible(ofnode from, const char *compat)
801 if (of_live_active()) {
802 return np_to_ofnode(of_find_compatible_node(
803 (struct device_node *)ofnode_to_np(from), NULL,
806 return offset_to_ofnode(fdt_node_offset_by_compatible(
807 gd->fdt_blob, ofnode_to_offset(from), compat));
811 ofnode ofnode_by_prop_value(ofnode from, const char *propname,
812 const void *propval, int proplen)
814 if (of_live_active()) {
815 return np_to_ofnode(of_find_node_by_prop_value(
816 (struct device_node *)ofnode_to_np(from), propname,
819 return offset_to_ofnode(fdt_node_offset_by_prop_value(
820 gd->fdt_blob, ofnode_to_offset(from),
821 propname, propval, proplen));
825 int ofnode_write_prop(ofnode node, const char *propname, int len,
828 const struct device_node *np = ofnode_to_np(node);
830 struct property *pp_last = NULL;
831 struct property *new;
833 if (!of_live_active())
839 for (pp = np->properties; pp; pp = pp->next) {
840 if (strcmp(pp->name, propname) == 0) {
841 /* Property exists -> change value */
842 pp->value = (void *)value;
852 /* Property does not exist -> append new property */
853 new = malloc(sizeof(struct property));
857 new->name = strdup(propname);
863 new->value = (void *)value;
872 int ofnode_write_string(ofnode node, const char *propname, const char *value)
874 if (!of_live_active())
877 assert(ofnode_valid(node));
879 debug("%s: %s = %s", __func__, propname, value);
881 return ofnode_write_prop(node, propname, strlen(value) + 1, value);
884 int ofnode_set_enabled(ofnode node, bool value)
886 if (!of_live_active())
889 assert(ofnode_valid(node));
892 return ofnode_write_string(node, "status", "okay");
894 return ofnode_write_string(node, "status", "disabled");