Add fdt network helper functions fdt_support_net.c

Reviewed-by: Stefan Roese <s...@denx.de>
Acked-by: Ramon Fried <rfried....@gmail.com>

Signed-off-by: Tony Dinh <mibo...@gmail.com>
---

Changes in v2:
- Return FDT_ERR_NOTFOUND if fdt_get_phy_addr failed to find PHY addr
- Coding standards.

 common/fdt_support_net.c | 47 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
 create mode 100644 common/fdt_support_net.c

diff --git a/common/fdt_support_net.c b/common/fdt_support_net.c
new file mode 100644
index 0000000000..46fa12dd7c
--- /dev/null
+++ b/common/fdt_support_net.c
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2021 Tony Dinh <mibo...@gmail.com>
+ */
+
+#include <asm/global_data.h>
+#include <fdt_support.h>
+#include <fdt_support_net.h>
+#include <linux/libfdt.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+int fdt_get_phy_addr(const char *path)
+{
+       const void *fdt = gd->fdt_blob;
+       const u32 *reg;
+       const u32 *val;
+       int node, phandle, addr;
+
+       /* Find the node by its full path */
+       node = fdt_path_offset(fdt, path);
+       if (node >= 0) {
+               /* Look up phy-handle */
+               val = fdt_getprop(fdt, node, "phy-handle", NULL);
+               if (!val) {
+                       /* Look up phy (deprecated property for phy handle) */
+                       val = fdt_getprop(fdt, node, "phy", NULL);
+               }
+               if (val) {
+                       phandle = fdt32_to_cpu(*val);
+                       if (!phandle)
+                               return -FDT_ERR_NOTFOUND;
+
+                       /* Follow it to its node */
+                       node = fdt_node_offset_by_phandle(fdt, phandle);
+                       if (node) {
+                               /* Look up reg */
+                               reg = fdt_getprop(fdt, node, "reg", NULL);
+                               if (reg) {
+                                       addr = fdt32_to_cpu(*reg);
+                                       return addr;
+                               }
+                       }
+               }
+       }
+       return -FDT_ERR_NOTFOUND;
+}
-- 
2.20.1

Reply via email to