[U-Boot] [PATCH 01/23] fdt: Add functions to query a node's #address- and #size-cells

Thierry Reding
2014-08-18T07:16:14Z

From: Thierry Reding <treding@nvidia.com>

Given a device tree node, the fdt_n_addr_cells() function will walk up
the device tree and search for an #address-cells property. It returns
the number of cells required by the device tree node to represent an
address.

Similarly the fdt_n_size_cells() function returns the number of cells
required by the given device tree node to represent a size. It achieves
that by walking up the device tree in seach for a #size-cells property.

If no #address-cells or #size-cells property can be found, both of the
functions return 1.

Signed-off-by: Thierry Reding <treding@nvidia.com>
---
include/libfdt.h | 28 ++++++++++++++++++++++++++++
lib/libfdt/fdt_ro.c | 36 ++++++++++++++++++++++++++++++++++++
2 files changed, 64 insertions(+)

diff --git a/include/libfdt.h b/include/libfdt.h
index a1ef1e15df3d..e7f991b388cf 100644
--- a/include/libfdt.h
+++ b/include/libfdt.h
@@ -1638,4 +1638,32 @@ int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
struct fdt_region region[], int max_regions,
char *path, int path_len, int add_string_tab);

+/**
+ * fdt_n_addr_cells() - find the number of address cells required by a node
+ *
+ * Looks up the #address-cells property of the node to examine. If that has
+ * no such property, walks up the device tree until it finds one in one of
+ * the device's parents. If no #address-cells property is found, it is
+ * assumed to be 1.
+ *
+ * @param fdt FDT blob
+ * @param node node to examine
+ * @return number of address cells
+ */
+int fdt_n_addr_cells(const void *fdt, int node);
+
+/**
+ * fdt_n_size_cells() - find the number of size cells required by a node
+ *
+ * Looks up the #size-cells property of the node to examine. If that has no
+ * such property, walks up the device tree until it finds one in one of the
+ * device's parents. If no #size-cells property is found, it is assumed to
+ * be 1.
+ *
+ * @param fdt FDT blob
+ * @param node node to examine
+ * @return number of size cells
+ */
+int fdt_n_size_cells(const void *fdt, int node);
+
#endif /* _LIBFDT_H */
diff --git a/lib/libfdt/fdt_ro.c b/lib/libfdt/fdt_ro.c
index 36af0435254b..17cd11333c1e 100644
--- a/lib/libfdt/fdt_ro.c
+++ b/lib/libfdt/fdt_ro.c
@@ -530,3 +530,39 @@ int fdt_node_offset_by_compatible(const void *fdt, int startoffset,

return offset; /* error from fdt_next_node() */
}
+
+int fdt_n_addr_cells(const void *fdt, int node)
+{
+ const fdt32_t *prop;
+ int len, parent;
+
+ do {
+ parent = fdt_parent_offset(fdt, node);
+ if (parent >= 0)
+ node = parent;
+
+ prop = fdt_getprop(fdt, node, "#address-cells", &len);
+ if (prop)
+ return fdt32_to_cpu(*prop);
+ } while (parent >= 0);
+
+ return 1;
+}
+
+int fdt_n_size_cells(const void *fdt, int node)
+{
+ const fdt32_t *prop;
+ int len, parent;
+
+ do {
+ parent = fdt_parent_offset(fdt, node);
+ if (parent >= 0)
+ node = parent;
+
+ prop = fdt_getprop(fdt, node, "#size-cells", &len);
+ if (prop)
+ return fdt32_to_cpu(*prop);
+ } while (parent >= 0);
+
+ return 1;
+}
--
2.0.4