@@ -981,6 +981,18 @@ int fdtdec_setup_mem_size_base(void);
*/
int fdtdec_setup_memory_banksize(void);
+/**
+ * fdtdec_generate_phandle() - return a new, unused phandle for an FDT blob
+ *
+ * Returns a phandle that can be used to refer to a new device tree node. It
+ * is one higher than the currently highest phandle in the given FDT blob.
+ *
+ * @param blob FDT blob
+ * @param maxp return location for the new phandle
+ * @return 0 on success or a negative error code on failure
+ */
+int fdtdec_generate_phandle(const void *blob, uint32_t *phandle);
+
/**
* Set up the device tree ready for use
*/
@@ -1243,6 +1243,34 @@ __weak void *board_fdt_blob_setup(void)
}
#endif
+int fdtdec_generate_phandle(const void *blob, uint32_t *phandle)
+{
+ uint32_t max = 0;
+ int offset = -1;
+
+ while (true) {
+ uint32_t value;
+
+ offset = fdt_next_node(blob, offset, NULL);
+ if (offset < 0) {
+ if (offset == -FDT_ERR_NOTFOUND)
+ break;
+
+ return offset;
+ }
+
+ value = fdt_get_phandle(blob, offset);
+
+ if (value > max)
+ max = value;
+ }
+
+ if (phandle)
+ *phandle = max + 1;
+
+ return 0;
+}
+
int fdtdec_setup(void)
{
#if CONFIG_IS_ENABLED(OF_CONTROL)