[PATCH v2 32/45] dm: core: Add an ofnode function to obtain the flat tree
Simon Glass
sjg at chromium.org
Wed Sep 7 04:27:20 CEST 2022
The flat device tree is assumed to be the control FDT but this is not
always the case. Update the ofnode implementation to obtain the node via
an function call so we can eventually add support for selecting different
trees.
Signed-off-by: Simon Glass <sjg at chromium.org>
---
(no changes since v1)
drivers/core/ofnode.c | 91 ++++++++++++++++++++++++-------------------
include/dm/ofnode.h | 19 +++++++++
2 files changed, 69 insertions(+), 41 deletions(-)
diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index 45bc7f9375d..8e1996e8aa3 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -57,8 +57,8 @@ int ofnode_read_u32_index(ofnode node, const char *propname, int index,
return of_read_u32_index(ofnode_to_np(node), propname, index,
outp);
- cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
- &len);
+ cell = fdt_getprop(ofnode_to_fdt(node), ofnode_to_offset(node),
+ propname, &len);
if (!cell) {
debug("(not found)\n");
return -EINVAL;
@@ -103,8 +103,8 @@ int ofnode_read_u64(ofnode node, const char *propname, u64 *outp)
if (ofnode_is_np(node))
return of_read_u64(ofnode_to_np(node), propname, outp);
- cell = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node), propname,
- &len);
+ cell = fdt_getprop(ofnode_to_fdt(node), ofnode_to_offset(node),
+ propname, &len);
if (!cell || len < sizeof(*cell)) {
debug("(not found)\n");
return -EINVAL;
@@ -155,7 +155,7 @@ const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep)
len = prop->length;
}
} else {
- val = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
+ val = fdt_getprop(ofnode_to_fdt(node), ofnode_to_offset(node),
propname, &len);
}
if (!val) {
@@ -214,7 +214,7 @@ ofnode ofnode_find_subnode(ofnode node, const char *subnode_name)
}
subnode = np_to_ofnode(np);
} else {
- int ooffset = fdt_subnode_offset(gd->fdt_blob,
+ int ooffset = fdt_subnode_offset(ofnode_to_fdt(node),
ofnode_to_offset(node), subnode_name);
subnode = offset_to_ofnode(ooffset);
}
@@ -236,7 +236,7 @@ int ofnode_read_u32_array(ofnode node, const char *propname,
} else {
int ret;
- ret = fdtdec_get_int_array(gd->fdt_blob,
+ ret = fdtdec_get_int_array(ofnode_to_fdt(node),
ofnode_to_offset(node), propname,
out_values, sz);
if (ret == -FDT_ERR_NOTFOUND)
@@ -253,7 +253,7 @@ bool ofnode_is_enabled(ofnode node)
if (ofnode_is_np(node)) {
return of_device_is_available(ofnode_to_np(node));
} else {
- return fdtdec_get_is_enabled(gd->fdt_blob,
+ return fdtdec_get_is_enabled(ofnode_to_fdt(node),
ofnode_to_offset(node));
}
}
@@ -265,7 +265,7 @@ ofnode ofnode_first_subnode(ofnode node)
return np_to_ofnode(node.np->child);
return offset_to_ofnode(
- fdt_first_subnode(gd->fdt_blob, ofnode_to_offset(node)));
+ fdt_first_subnode(ofnode_to_fdt(node), ofnode_to_offset(node)));
}
ofnode ofnode_next_subnode(ofnode node)
@@ -275,7 +275,7 @@ ofnode ofnode_next_subnode(ofnode node)
return np_to_ofnode(node.np->sibling);
return offset_to_ofnode(
- fdt_next_subnode(gd->fdt_blob, ofnode_to_offset(node)));
+ fdt_next_subnode(ofnode_to_fdt(node), ofnode_to_offset(node)));
}
#endif /* !DM_INLINE_OFNODE */
@@ -287,7 +287,7 @@ ofnode ofnode_get_parent(ofnode node)
if (ofnode_is_np(node))
parent = np_to_ofnode(of_get_parent(ofnode_to_np(node)));
else
- parent.of_offset = fdt_parent_offset(gd->fdt_blob,
+ parent.of_offset = fdt_parent_offset(ofnode_to_fdt(node),
ofnode_to_offset(node));
return parent;
@@ -303,7 +303,7 @@ const char *ofnode_get_name(ofnode node)
if (ofnode_is_np(node))
return node.np->name;
- return fdt_get_name(gd->fdt_blob, ofnode_to_offset(node), NULL);
+ return fdt_get_name(ofnode_to_fdt(node), ofnode_to_offset(node), NULL);
}
int ofnode_get_path(ofnode node, char *buf, int buflen)
@@ -320,7 +320,7 @@ int ofnode_get_path(ofnode node, char *buf, int buflen)
} else {
int res;
- res = fdt_get_path(gd->fdt_blob, ofnode_to_offset(node), buf,
+ res = fdt_get_path(ofnode_to_fdt(node), ofnode_to_offset(node), buf,
buflen);
if (!res)
return res;
@@ -376,7 +376,7 @@ static fdt_addr_t __ofnode_get_addr_size_index(ofnode node, int index,
} else {
na = ofnode_read_simple_addr_cells(ofnode_get_parent(node));
ns = ofnode_read_simple_size_cells(ofnode_get_parent(node));
- return fdtdec_get_addr_size_fixed(gd->fdt_blob,
+ return fdtdec_get_addr_size_fixed(ofnode_to_fdt(node),
ofnode_to_offset(node), "reg",
index, na, ns, size,
translate);
@@ -424,7 +424,7 @@ int ofnode_stringlist_search(ofnode node, const char *property,
} else {
int ret;
- ret = fdt_stringlist_search(gd->fdt_blob,
+ ret = fdt_stringlist_search(ofnode_to_fdt(node),
ofnode_to_offset(node), property,
string);
if (ret == -FDT_ERR_NOTFOUND)
@@ -445,7 +445,8 @@ int ofnode_read_string_index(ofnode node, const char *property, int index,
} else {
int len;
- *outp = fdt_stringlist_get(gd->fdt_blob, ofnode_to_offset(node),
+ *outp = fdt_stringlist_get(ofnode_to_fdt(node),
+ ofnode_to_offset(node),
property, index, &len);
if (len < 0)
return -EINVAL;
@@ -458,7 +459,7 @@ int ofnode_read_string_count(ofnode node, const char *property)
if (ofnode_is_np(node)) {
return of_property_count_strings(ofnode_to_np(node), property);
} else {
- return fdt_stringlist_count(gd->fdt_blob,
+ return fdt_stringlist_count(ofnode_to_fdt(node),
ofnode_to_offset(node), property);
}
}
@@ -527,7 +528,7 @@ int ofnode_parse_phandle_with_args(ofnode node, const char *list_name,
struct fdtdec_phandle_args args;
int ret;
- ret = fdtdec_parse_phandle_with_args(gd->fdt_blob,
+ ret = fdtdec_parse_phandle_with_args(ofnode_to_fdt(node),
ofnode_to_offset(node),
list_name, cells_name,
cell_count, index, &args);
@@ -546,7 +547,7 @@ int ofnode_count_phandle_with_args(ofnode node, const char *list_name,
return of_count_phandle_with_args(ofnode_to_np(node),
list_name, cells_name, cell_count);
else
- return fdtdec_parse_phandle_with_args(gd->fdt_blob,
+ return fdtdec_parse_phandle_with_args(ofnode_to_fdt(node),
ofnode_to_offset(node), list_name, cells_name,
cell_count, -1, NULL);
}
@@ -721,7 +722,7 @@ const void *ofnode_get_property(ofnode node, const char *propname, int *lenp)
if (ofnode_is_np(node))
return of_get_property(ofnode_to_np(node), propname, lenp);
else
- return fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
+ return fdt_getprop(ofnode_to_fdt(node), ofnode_to_offset(node),
propname, lenp);
}
@@ -735,7 +736,7 @@ int ofnode_first_property(ofnode node, struct ofprop *prop)
return -FDT_ERR_NOTFOUND;
} else {
prop->offset =
- fdt_first_property_offset(gd->fdt_blob,
+ fdt_first_property_offset(ofnode_to_fdt(node),
ofnode_to_offset(prop->node));
if (prop->offset < 0)
return prop->offset;
@@ -752,8 +753,9 @@ int ofnode_next_property(struct ofprop *prop)
if (!prop->prop)
return -FDT_ERR_NOTFOUND;
} else {
- prop->offset = fdt_next_property_offset(gd->fdt_blob,
- prop->offset);
+ prop->offset =
+ fdt_next_property_offset(ofnode_to_fdt(prop->node),
+ prop->offset);
if (prop->offset < 0)
return prop->offset;
}
@@ -768,7 +770,7 @@ const void *ofprop_get_property(const struct ofprop *prop,
return of_get_property_by_prop(ofnode_to_np(prop->node),
prop->prop, propname, lenp);
else
- return fdt_getprop_by_offset(gd->fdt_blob,
+ return fdt_getprop_by_offset(ofnode_to_fdt(prop->node),
prop->offset,
propname, lenp);
}
@@ -793,7 +795,7 @@ fdt_addr_t ofnode_get_addr_size(ofnode node, const char *property,
else
return of_read_number(prop, na);
} else {
- return fdtdec_get_addr_size(gd->fdt_blob,
+ return fdtdec_get_addr_size(ofnode_to_fdt(node),
ofnode_to_offset(node), property,
sizep);
}
@@ -812,7 +814,7 @@ const uint8_t *ofnode_read_u8_array_ptr(ofnode node, const char *propname,
return (uint8_t *)prop;
} else {
- return fdtdec_locate_byte_array(gd->fdt_blob,
+ return fdtdec_locate_byte_array(ofnode_to_fdt(node),
ofnode_to_offset(node), propname, sz);
}
}
@@ -948,10 +950,10 @@ int ofnode_read_addr_cells(ofnode node)
if (ofnode_is_np(node)) {
return of_n_addr_cells(ofnode_to_np(node));
} else {
- int parent = fdt_parent_offset(gd->fdt_blob,
+ int parent = fdt_parent_offset(ofnode_to_fdt(node),
ofnode_to_offset(node));
- return fdt_address_cells(gd->fdt_blob, parent);
+ return fdt_address_cells(ofnode_to_fdt(node), parent);
}
}
@@ -960,10 +962,10 @@ int ofnode_read_size_cells(ofnode node)
if (ofnode_is_np(node)) {
return of_n_size_cells(ofnode_to_np(node));
} else {
- int parent = fdt_parent_offset(gd->fdt_blob,
+ int parent = fdt_parent_offset(ofnode_to_fdt(node),
ofnode_to_offset(node));
- return fdt_size_cells(gd->fdt_blob, parent);
+ return fdt_size_cells(ofnode_to_fdt(node), parent);
}
}
@@ -972,7 +974,8 @@ int ofnode_read_simple_addr_cells(ofnode node)
if (ofnode_is_np(node))
return of_simple_addr_cells(ofnode_to_np(node));
else
- return fdt_address_cells(gd->fdt_blob, ofnode_to_offset(node));
+ return fdt_address_cells(ofnode_to_fdt(node),
+ ofnode_to_offset(node));
}
int ofnode_read_simple_size_cells(ofnode node)
@@ -980,7 +983,8 @@ int ofnode_read_simple_size_cells(ofnode node)
if (ofnode_is_np(node))
return of_simple_size_cells(ofnode_to_np(node));
else
- return fdt_size_cells(gd->fdt_blob, ofnode_to_offset(node));
+ return fdt_size_cells(ofnode_to_fdt(node),
+ ofnode_to_offset(node));
}
bool ofnode_pre_reloc(ofnode node)
@@ -1017,7 +1021,8 @@ int ofnode_read_resource(ofnode node, uint index, struct resource *res)
struct fdt_resource fres;
int ret;
- ret = fdt_get_resource(gd->fdt_blob, ofnode_to_offset(node),
+ ret = fdt_get_resource(ofnode_to_fdt(node),
+ ofnode_to_offset(node),
"reg", index, &fres);
if (ret < 0)
return -EINVAL;
@@ -1046,7 +1051,8 @@ u64 ofnode_translate_address(ofnode node, const fdt32_t *in_addr)
if (ofnode_is_np(node))
return of_translate_address(ofnode_to_np(node), in_addr);
else
- return fdt_translate_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
+ return fdt_translate_address(ofnode_to_fdt(node),
+ ofnode_to_offset(node), in_addr);
}
u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
@@ -1054,7 +1060,8 @@ u64 ofnode_translate_dma_address(ofnode node, const fdt32_t *in_addr)
if (ofnode_is_np(node))
return of_translate_dma_address(ofnode_to_np(node), in_addr);
else
- return fdt_translate_dma_address(gd->fdt_blob, ofnode_to_offset(node), in_addr);
+ return fdt_translate_dma_address(ofnode_to_fdt(node),
+ ofnode_to_offset(node), in_addr);
}
int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus, u64 *size)
@@ -1062,7 +1069,8 @@ int ofnode_get_dma_range(ofnode node, phys_addr_t *cpu, dma_addr_t *bus, u64 *si
if (ofnode_is_np(node))
return of_get_dma_range(ofnode_to_np(node), cpu, bus, size);
else
- return fdt_get_dma_range(gd->fdt_blob, ofnode_to_offset(node),
+ return fdt_get_dma_range(ofnode_to_fdt(node),
+ ofnode_to_offset(node),
cpu, bus, size);
}
@@ -1072,7 +1080,7 @@ int ofnode_device_is_compatible(ofnode node, const char *compat)
return of_device_is_compatible(ofnode_to_np(node), compat,
NULL, NULL);
else
- return !fdt_node_check_compatible(gd->fdt_blob,
+ return !fdt_node_check_compatible(ofnode_to_fdt(node),
ofnode_to_offset(node),
compat);
}
@@ -1085,7 +1093,8 @@ ofnode ofnode_by_compatible(ofnode from, const char *compat)
compat));
} else {
return offset_to_ofnode(fdt_node_offset_by_compatible(
- gd->fdt_blob, ofnode_to_offset(from), compat));
+ ofnode_to_fdt(from),
+ ofnode_to_offset(from), compat));
}
}
@@ -1098,7 +1107,7 @@ ofnode ofnode_by_prop_value(ofnode from, const char *propname,
propval, proplen));
} else {
return offset_to_ofnode(fdt_node_offset_by_prop_value(
- gd->fdt_blob, ofnode_to_offset(from),
+ ofnode_to_fdt(from), ofnode_to_offset(from),
propname, propval, proplen));
}
}
@@ -1109,7 +1118,7 @@ int ofnode_write_prop(ofnode node, const char *propname, const void *value,
if (of_live_active())
return of_write_prop(ofnode_to_np(node), propname, len, value);
else
- return fdt_setprop((void *)gd->fdt_blob, ofnode_to_offset(node),
+ return fdt_setprop(ofnode_to_fdt(node), ofnode_to_offset(node),
propname, value, len);
return 0;
@@ -1242,7 +1251,7 @@ int ofnode_add_subnode(ofnode node, const char *name, ofnode *subnodep)
return ret;
subnode = np_to_ofnode(child);
} else {
- void *fdt = (void *)gd->fdt_blob;
+ void *fdt = ofnode_to_fdt(node);
int poffset = ofnode_to_offset(node);
int offset;
diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h
index 46802366667..0423c072402 100644
--- a/include/dm/ofnode.h
+++ b/include/dm/ofnode.h
@@ -35,6 +35,25 @@ struct ofnode_phandle_args {
*/
static inline void oftree_reset(void) {}
+/**
+ * ofnode_to_fdt() - convert an ofnode to a flat DT pointer
+ *
+ * This cannot be called if the reference contains a node pointer.
+ *
+ * @node: Reference containing offset (possibly invalid)
+ * Return: DT offset (can be NULL)
+ */
+static inline void *ofnode_to_fdt(ofnode node)
+{
+#ifdef OF_CHECKS
+ if (of_live_active())
+ return NULL;
+#endif
+
+ /* Use the control FDT by default */
+ return (void *)gd->fdt_blob;
+}
+
/**
* ofnode_to_np() - convert an ofnode to a live DT node pointer
*
--
2.37.2.789.g6183377224-goog
More information about the U-Boot
mailing list