From: Thierry Reding <tred...@nvidia.com>

This function allows looking up the highest phandle value stored in a
device tree, which is useful to determine the next best phandle value
for new nodes.

Signed-off-by: Thierry Reding <tred...@nvidia.com>
---
 include/fdtdec.h | 12 ++++++++++++
 lib/fdtdec.c     | 28 ++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/include/fdtdec.h b/include/fdtdec.h
index a965c33157c9..5eb3c0c237a9 100644
--- a/include/fdtdec.h
+++ b/include/fdtdec.h
@@ -956,6 +956,18 @@ int fdtdec_setup_mem_size_base(void);
  */
 int fdtdec_setup_memory_banksize(void);
 
+/**
+ * fdtdec_get_max_phandle() - return the highest phandle in an FDT blob
+ *
+ * Returns the highest phandle in the given FDT blob. The result of this can
+ * be used to generate a new phandle by incrementing by one.
+ *
+ * @param blob FDT blob
+ * @param maxp return location for the highest phandle in the FDT blob
+ * @return 0 on success or a negative error code on failure
+ */
+int fdtdec_get_max_phandle(const void *blob, uint32_t *maxp);
+
 /**
  * Set up the device tree ready for use
  */
diff --git a/lib/fdtdec.c b/lib/fdtdec.c
index 09a7e133a539..f2af947c106e 100644
--- a/lib/fdtdec.c
+++ b/lib/fdtdec.c
@@ -1243,6 +1243,34 @@ __weak void *board_fdt_blob_setup(void)
 }
 #endif
 
+int fdtdec_get_max_phandle(const void *blob, uint32_t *maxp)
+{
+       uint32_t max = 0;
+       int offset = -1;
+
+       while (true) {
+               uint32_t phandle;
+
+               offset = fdt_next_node(blob, offset, NULL);
+               if (offset < 0) {
+                       if (offset == -FDT_ERR_NOTFOUND)
+                               break;
+
+                       return offset;
+               }
+
+               phandle = fdt_get_phandle(blob, offset);
+
+               if (phandle > max)
+                       max = phandle;
+       }
+
+       if (maxp)
+               *maxp = max;
+
+       return 0;
+}
+
 int fdtdec_setup(void)
 {
 #if CONFIG_IS_ENABLED(OF_CONTROL)
-- 
2.20.1

_______________________________________________
U-Boot mailing list
U-Boot@lists.denx.de
https://lists.denx.de/listinfo/u-boot

Reply via email to