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 assert(ofnode_valid(node));
22 debug("%s: %s: ", __func__, propname);
24 if (ofnode_is_np(node)) {
25 return of_read_u32(ofnode_to_np(node), propname, outp);
30 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
32 if (!cell || len < sizeof(int)) {
33 debug("(not found)\n");
36 *outp = fdt32_to_cpu(cell[0]);
38 debug("%#x (%d)\n", *outp, *outp);
43 u32 ofnode_read_u32_default(ofnode node, const char *propname, u32 def)
45 assert(ofnode_valid(node));
46 ofnode_read_u32(node, propname, &def);
51 int ofnode_read_s32_default(ofnode node, const char *propname, s32 def)
53 assert(ofnode_valid(node));
54 ofnode_read_u32(node, propname, (u32 *)&def);
59 int ofnode_read_u64(ofnode node, const char *propname, u64 *outp)
61 const unaligned_fdt64_t *cell;
64 assert(ofnode_valid(node));
65 debug("%s: %s: ", __func__, propname);
67 if (ofnode_is_np(node))
68 return of_read_u64(ofnode_to_np(node), propname, outp);
70 cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
72 if (!cell || len < sizeof(*cell)) {
73 debug("(not found)\n");
76 *outp = fdt64_to_cpu(cell[0]);
77 debug("%#llx (%lld)\n", (unsigned long long)*outp,
78 (unsigned long long)*outp);
83 u64 ofnode_read_u64_default(ofnode node, const char *propname, u64 def)
85 assert(ofnode_valid(node));
86 ofnode_read_u64(node, propname, &def);
91 bool ofnode_read_bool(ofnode node, const char *propname)
95 assert(ofnode_valid(node));
96 debug("%s: %s: ", __func__, propname);
98 prop = ofnode_get_property(node, propname, NULL);
100 debug("%s\n", prop ? "true" : "false");
102 return prop ? true : false;
105 const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep)
107 const char *val = NULL;
110 assert(ofnode_valid(node));
111 debug("%s: %s: ", __func__, propname);
113 if (ofnode_is_np(node)) {
114 struct property *prop = of_find_property(
115 ofnode_to_np(node), propname, &len);
122 val = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
126 debug("<not found>\n");
128 *sizep = -FDT_ERR_NOTFOUND;
137 const char *ofnode_read_string(ofnode node, const char *propname)
142 str = ofnode_read_prop(node, propname, &len);
146 if (strnlen(str, len) >= len) {
147 debug("<invalid>\n");
155 int ofnode_read_size(ofnode node, const char *propname)
159 if (!ofnode_read_prop(node, propname, &len))
165 ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
169 assert(ofnode_valid(node));
170 debug("%s: %s: ", __func__, subnode_name);
172 if (ofnode_is_np(node)) {
173 const struct device_node *np = ofnode_to_np(node);
175 for (np = np->child; np; np = np->sibling) {
176 if (!strcmp(subnode_name, np->name))
179 subnode = np_to_ofnode(np);
181 int ooffset = fdt_subnode_offset(gd->fdt_blob,
182 ofnode_to_offset(node), subnode_name);
183 subnode = offset_to_ofnode(ooffset);
185 debug("%s\n", ofnode_valid(subnode) ?
186 ofnode_get_name(subnode) : "<none>");
191 int ofnode_read_u32_array(ofnode node, const char *propname,
192 u32 *out_values, size_t sz)
194 assert(ofnode_valid(node));
195 debug("%s: %s: ", __func__, propname);
197 if (ofnode_is_np(node)) {
198 return of_read_u32_array(ofnode_to_np(node), propname,
201 return fdtdec_get_int_array(gd->fdt_blob,
202 ofnode_to_offset(node), propname,
207 ofnode ofnode_first_subnode(ofnode node)
209 assert(ofnode_valid(node));
210 if (ofnode_is_np(node))
211 return np_to_ofnode(node.np->child);
213 return offset_to_ofnode(
214 fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
217 ofnode ofnode_next_subnode(ofnode node)
219 assert(ofnode_valid(node));
220 if (ofnode_is_np(node))
221 return np_to_ofnode(node.np->sibling);
223 return offset_to_ofnode(
224 fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
227 ofnode ofnode_get_parent(ofnode node)
231 assert(ofnode_valid(node));
232 if (ofnode_is_np(node))
233 parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
235 parent.of_offset = fdt_parent_offset(gd->fdt_blob,
236 ofnode_to_offset(node));
241 const char *ofnode_get_name(ofnode node)
243 if (!ofnode_valid(node)) {
244 debug("%s node not valid\n", __func__);
248 if (ofnode_is_np(node))
249 return strrchr(node.np->full_name, '/') + 1;
251 return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
254 ofnode ofnode_get_by_phandle(uint phandle)
258 if (of_live_active())
259 node = np_to_ofnode(of_find_node_by_phandle(phandle));
261 node.of_offset = fdt_node_offset_by_phandle(gd->fdt_blob,
267 fdt_addr_t ofnode_get_addr_size_index(ofnode node, int index, fdt_size_t *size)
271 if (ofnode_is_np(node)) {
272 const __be32 *prop_val;
276 prop_val = of_get_address(ofnode_to_np(node), index, &size64,
279 return FDT_ADDR_T_NONE;
283 ns = of_n_size_cells(ofnode_to_np(node));
285 if (IS_ENABLED(CONFIG_OF_TRANSLATE) && ns > 0) {
286 return of_translate_address(ofnode_to_np(node), prop_val);
288 na = of_n_addr_cells(ofnode_to_np(node));
289 return of_read_number(prop_val, na);
292 na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
293 ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
294 return fdtdec_get_addr_size_fixed(gd->fdt_blob,
295 ofnode_to_offset(node), "reg",
296 index, na, ns, size, true);
299 return FDT_ADDR_T_NONE;
302 fdt_addr_t ofnode_get_addr_index(ofnode node, int index)
306 return ofnode_get_addr_size_index(node, index, &size);
309 fdt_addr_t ofnode_get_addr(ofnode node)
311 return ofnode_get_addr_index(node, 0);
314 int ofnode_stringlist_search(ofnode node, const char *property,
317 if (ofnode_is_np(node)) {
318 return of_property_match_string(ofnode_to_np(node),
323 ret = fdt_stringlist_search(gd->fdt_blob,
324 ofnode_to_offset(node), property,
326 if (ret == -FDT_ERR_NOTFOUND)
335 int ofnode_read_string_index(ofnode node, const char *property, int index,
338 if (ofnode_is_np(node)) {
339 return of_property_read_string_index(ofnode_to_np(node),
340 property, index, outp);
344 *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
345 property, index, &len);
352 int ofnode_read_string_count(ofnode node, const char *property)
354 if (ofnode_is_np(node)) {
355 return of_property_count_strings(ofnode_to_np(node), property);
357 return fdt_stringlist_count(gd->fdt_blob,
358 ofnode_to_offset(node), property);
362 static void ofnode_from_fdtdec_phandle_args(struct fdtdec_phandle_args *in,
363 struct ofnode_phandle_args *out)
365 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
366 out->node = offset_to_ofnode(in->node);
367 out->args_count = in->args_count;
368 memcpy(out->args, in->args, sizeof(out->args));
371 static void ofnode_from_of_phandle_args(struct of_phandle_args *in,
372 struct ofnode_phandle_args *out)
374 assert(OF_MAX_PHANDLE_ARGS == MAX_PHANDLE_ARGS);
375 out->node = np_to_ofnode(in->np);
376 out->args_count = in->args_count;
377 memcpy(out->args, in->args, sizeof(out->args));
380 int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
381 const char *cells_name, int cell_count,
383 struct ofnode_phandle_args *out_args)
385 if (ofnode_is_np(node)) {
386 struct of_phandle_args args;
389 ret = of_parse_phandle_with_args(ofnode_to_np(node),
390 list_name, cells_name, index,
394 ofnode_from_of_phandle_args(&args, out_args);
396 struct fdtdec_phandle_args args;
399 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
400 ofnode_to_offset(node),
401 list_name, cells_name,
402 cell_count, index, &args);
405 ofnode_from_fdtdec_phandle_args(&args, out_args);
411 int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
412 const char *cells_name)
414 if (ofnode_is_np(node))
415 return of_count_phandle_with_args(ofnode_to_np(node),
416 list_name, cells_name);
418 return fdtdec_parse_phandle_with_args(gd->fdt_blob,
419 ofnode_to_offset(node), list_name, cells_name,
423 ofnode ofnode_path(const char *path)
425 if (of_live_active())
426 return np_to_ofnode(of_find_node_by_path(path));
428 return offset_to_ofnode(fdt_path_offset(gd->fdt_blob, path));
431 const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
435 chosen_node = ofnode_path("/chosen");
437 return ofnode_read_prop(chosen_node, propname, sizep);
440 const char *ofnode_read_chosen_string(const char *propname)
442 return ofnode_read_chosen_prop(propname, NULL);
445 ofnode ofnode_get_chosen_node(const char *name)
449 prop = ofnode_read_chosen_prop(name, NULL);
451 return ofnode_null();
453 return ofnode_path(prop);
456 static int decode_timing_property(ofnode node, const char *name,
457 struct timing_entry *result)
461 length = ofnode_read_size(node, name);
463 debug("%s: could not find property %s\n",
464 ofnode_get_name(node), name);
468 if (length == sizeof(u32)) {
469 result->typ = ofnode_read_u32_default(node, name, 0);
470 result->min = result->typ;
471 result->max = result->typ;
473 ret = ofnode_read_u32_array(node, name, &result->min, 3);
479 int ofnode_decode_display_timing(ofnode parent, int index,
480 struct display_timing *dt)
483 ofnode timings, node;
487 timings = ofnode_find_subnode(parent, "display-timings");
488 if (!ofnode_valid(timings))
492 ofnode_for_each_subnode(node, timings) {
497 if (!ofnode_valid(node))
500 memset(dt, 0, sizeof(*dt));
502 ret |= decode_timing_property(node, "hback-porch", &dt->hback_porch);
503 ret |= decode_timing_property(node, "hfront-porch", &dt->hfront_porch);
504 ret |= decode_timing_property(node, "hactive", &dt->hactive);
505 ret |= decode_timing_property(node, "hsync-len", &dt->hsync_len);
506 ret |= decode_timing_property(node, "vback-porch", &dt->vback_porch);
507 ret |= decode_timing_property(node, "vfront-porch", &dt->vfront_porch);
508 ret |= decode_timing_property(node, "vactive", &dt->vactive);
509 ret |= decode_timing_property(node, "vsync-len", &dt->vsync_len);
510 ret |= decode_timing_property(node, "clock-frequency", &dt->pixelclock);
513 val = ofnode_read_u32_default(node, "vsync-active", -1);
515 dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
516 DISPLAY_FLAGS_VSYNC_LOW;
518 val = ofnode_read_u32_default(node, "hsync-active", -1);
520 dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
521 DISPLAY_FLAGS_HSYNC_LOW;
523 val = ofnode_read_u32_default(node, "de-active", -1);
525 dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
526 DISPLAY_FLAGS_DE_LOW;
528 val = ofnode_read_u32_default(node, "pixelclk-active", -1);
530 dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
531 DISPLAY_FLAGS_PIXDATA_NEGEDGE;
534 if (ofnode_read_bool(node, "interlaced"))
535 dt->flags |= DISPLAY_FLAGS_INTERLACED;
536 if (ofnode_read_bool(node, "doublescan"))
537 dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
538 if (ofnode_read_bool(node, "doubleclk"))
539 dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
544 const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
546 if (ofnode_is_np(node))
547 return of_get_property(ofnode_to_np(node), propname, lenp);
549 return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
553 bool ofnode_is_available(ofnode node)
555 if (ofnode_is_np(node))
556 return of_device_is_available(ofnode_to_np(node));
558 return fdtdec_get_is_enabled(gd->fdt_blob,
559 ofnode_to_offset(node));
562 fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
565 if (ofnode_is_np(node)) {
568 const struct device_node *np = ofnode_to_np(node);
569 const __be32 *prop = of_get_property(np, property, &psize);
572 return FDT_ADDR_T_NONE;
573 na = of_n_addr_cells(np);
574 ns = of_n_size_cells(np);
575 *sizep = of_read_number(prop + na, ns);
577 if (CONFIG_IS_ENABLED(OF_TRANSLATE) && ns > 0)
578 return of_translate_address(np, prop);
580 return of_read_number(prop, na);
582 return fdtdec_get_addr_size(gd->fdt_blob,
583 ofnode_to_offset(node), property,
588 const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
591 if (ofnode_is_np(node)) {
592 const struct device_node *np = ofnode_to_np(node);
594 const __be32 *prop = of_get_property(np, propname, &psize);
596 if (!prop || sz != psize)
598 return (uint8_t *)prop;
601 return fdtdec_locate_byte_array(gd->fdt_blob,
602 ofnode_to_offset(node), propname, sz);
606 int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
607 const char *propname, struct fdt_pci_addr *addr)
613 debug("%s: %s: ", __func__, propname);
616 * If we follow the pci bus bindings strictly, we should check
617 * the value of the node's parent node's #address-cells and
618 * #size-cells. They need to be 3 and 2 accordingly. However,
619 * for simplicity we skip the check here.
621 cell = ofnode_get_property(node, propname, &len);
625 if ((len % FDT_PCI_REG_SIZE) == 0) {
626 int num = len / FDT_PCI_REG_SIZE;
629 for (i = 0; i < num; i++) {
630 debug("pci address #%d: %08lx %08lx %08lx\n", i,
631 (ulong)fdt32_to_cpu(cell[0]),
632 (ulong)fdt32_to_cpu(cell[1]),
633 (ulong)fdt32_to_cpu(cell[2]));
634 if ((fdt32_to_cpu(*cell) & type) == type) {
635 addr->phys_hi = fdt32_to_cpu(cell[0]);
636 addr->phys_mid = fdt32_to_cpu(cell[1]);
637 addr->phys_lo = fdt32_to_cpu(cell[2]);
641 cell += (FDT_PCI_ADDR_CELLS +
656 debug("(not found)\n");
660 int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
662 const char *list, *end;
665 list = ofnode_get_property(node, "compatible", &len);
672 if (len >= strlen("pciVVVV,DDDD")) {
673 char *s = strstr(list, "pci");
676 * check if the string is something like pciVVVV,DDDD.RR
677 * or just pciVVVV,DDDD
679 if (s && s[7] == ',' &&
680 (s[12] == '.' || s[12] == 0)) {
682 *vendor = simple_strtol(s, NULL, 16);
685 *device = simple_strtol(s, NULL, 16);
696 int ofnode_read_addr_cells(ofnode node)
698 if (ofnode_is_np(node))
699 return of_n_addr_cells(ofnode_to_np(node));
700 else /* NOTE: this call should walk up the parent stack */
701 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
704 int ofnode_read_size_cells(ofnode node)
706 if (ofnode_is_np(node))
707 return of_n_size_cells(ofnode_to_np(node));
708 else /* NOTE: this call should walk up the parent stack */
709 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
712 int ofnode_read_simple_addr_cells(ofnode node)
714 if (ofnode_is_np(node))
715 return of_simple_addr_cells(ofnode_to_np(node));
717 return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
720 int ofnode_read_simple_size_cells(ofnode node)
722 if (ofnode_is_np(node))
723 return of_simple_size_cells(ofnode_to_np(node));
725 return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
728 bool ofnode_pre_reloc(ofnode node)
730 #if defined(CONFIG_SPL_BUILD) || defined(CONFIG_TPL_BUILD)
731 /* for SPL and TPL the remaining nodes after the fdtgrep 1st pass
732 * had property dm-pre-reloc or u-boot,dm-spl/tpl.
733 * They are removed in final dtb (fdtgrep 2nd pass)
737 if (ofnode_read_bool(node, "u-boot,dm-pre-reloc"))
739 if (ofnode_read_bool(node, "u-boot,dm-pre-proper"))
743 * In regular builds individual spl and tpl handling both
744 * count as handled pre-relocation for later second init.
746 if (ofnode_read_bool(node, "u-boot,dm-spl") ||
747 ofnode_read_bool(node, "u-boot,dm-tpl"))
754 int ofnode_read_resource(ofnode node, uint index, struct resource *res)
756 if (ofnode_is_np(node)) {
757 return of_address_to_resource(ofnode_to_np(node), index, res);
759 struct fdt_resource fres;
762 ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
763 "reg", index, &fres);
766 memset(res, '\0', sizeof(*res));
767 res->start = fres.start;
774 int ofnode_read_resource_byname(ofnode node, const char *name,
775 struct resource *res)
779 index = ofnode_stringlist_search(node, "reg-names", name);
783 return ofnode_read_resource(node, index, res);
786 u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
788 if (ofnode_is_np(node))
789 return of_translate_address(ofnode_to_np(node), in_addr);
791 return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
794 u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
796 if (ofnode_is_np(node))
797 return of_translate_dma_address(ofnode_to_np(node), in_addr);
799 return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
802 int ofnode_device_is_compatible(ofnode node, const char *compat)
804 if (ofnode_is_np(node))
805 return of_device_is_compatible(ofnode_to_np(node), compat,
808 return !fdt_node_check_compatible(gd->fdt_blob,
809 ofnode_to_offset(node),
813 ofnode ofnode_by_compatible(ofnode from, const char *compat)
815 if (of_live_active()) {
816 return np_to_ofnode(of_find_compatible_node(
817 (struct device_node *)ofnode_to_np(from), NULL,
820 return offset_to_ofnode(fdt_node_offset_by_compatible(
821 gd->fdt_blob, ofnode_to_offset(from), compat));
825 ofnode ofnode_by_prop_value(ofnode from, const char *propname,
826 const void *propval, int proplen)
828 if (of_live_active()) {
829 return np_to_ofnode(of_find_node_by_prop_value(
830 (struct device_node *)ofnode_to_np(from), propname,
833 return offset_to_ofnode(fdt_node_offset_by_prop_value(
834 gd->fdt_blob, ofnode_to_offset(from),
835 propname, propval, proplen));
839 int ofnode_write_prop(ofnode node, const char *propname, int len,
842 const struct device_node *np = ofnode_to_np(node);
844 struct property *pp_last = NULL;
845 struct property *new;
847 if (!of_live_active())
853 for (pp = np->properties; pp; pp = pp->next) {
854 if (strcmp(pp->name, propname) == 0) {
855 /* Property exists -> change value */
856 pp->value = (void *)value;
866 /* Property does not exist -> append new property */
867 new = malloc(sizeof(struct property));
871 new->name = strdup(propname);
877 new->value = (void *)value;
886 int ofnode_write_string(ofnode node, const char *propname, const char *value)
888 if (!of_live_active())
891 assert(ofnode_valid(node));
893 debug("%s: %s = %s", __func__, propname, value);
895 return ofnode_write_prop(node, propname, strlen(value) + 1, value);
898 int ofnode_set_enabled(ofnode node, bool value)
900 if (!of_live_active())
903 assert(ofnode_valid(node));
906 return ofnode_write_string(node, "status", "okay");
908 return ofnode_write_string(node, "status", "disabled");