Skip to contentSkip to conversation

common: Add fdt network helper

PATCH Superseded Tony Dinh 4 participants 24 messages Aug 6, 2021 – Sep 25, 2021

lore
Share & tools
lore

Tony Dinh

author
#0 lore raw

cover common: Add fdt network helper

At the moment, there is no common fdt helper function specific to decoding network related information from FDTs. This new helper functional group fdt_support_net is intended to be used by board-specific code within U-Boot for various network related chores.

In this patch, create the 1st function fdt_get_phy_addr to parse the device tree to find the PHY addess of a specific ethernet device.

Tony Dinh (3):
Add fdt network helper header file
Add fdt network helper functions
Add fdt network helper to Makefile

 common/Makefile           |  2 +-
 common/fdt_support_net.c  | 46 +++++++++++++++++++++++++++++++++++++++
 include/fdt_support_net.h | 39 +++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 common/fdt_support_net.c
 create mode 100644 include/fdt_support_net.h
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH 1/3 Add fdt network helper header file

Add include header file include/fdt_support_net.h

---

 include/fdt_support_net.h | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 include/fdt_support_net.h
include/fdt_support_net.h new file +39 -0
new file mode 100644
index 0000000000..4fe447f803
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0+
+ *
+ * Copyright (C) 2021 Tony Dinh <[email protected]>
+ */
+
+#ifndef __fdt_support_net_h
+#define __fdt_support_net_h
+
+/**
+ * This file contains convenience functions for decoding network related
+ * information from FDTs. It is intended to be used by board-specific code
+ * within U-Boot.
+ */
+
+/*
+ * fdt_get_phy_addr - Return the Ethernet PHY address
+ *
+ * Convenience function to return the PHY address of an
+ * ethernet device given its full path as defined in the device tree
+ *
+ * @path	full path to the network device node
+ * @return	PHY address, or -1 if it does not exist
+ *
+ * Usage examples:
+ *
+ * Get PHY address of eth0 for a Kirkwood board as defined in kirkwood.dtsi
+ *	int phyaddr;
+ *	char *eth0_path = "/ocp@f1000000/ethernet-controller@72000";
+ *	phyaddr = fdt_get_phy_addr(eth0_path);
+ *
+ * Get PHY address of eth1 for a Armada 38x board as defined
+ * in armada-38x.dtsi
+ *	int phyaddr;
+ *	char *eth1_path = "/soc/ethernet@30000";
+ *	phyaddr = fdt_get_phy_addr(eth1_path);
+ */
+int fdt_get_phy_addr(const char *path);
+
+#endif
Signature · 1 line
2.20.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] Add fdt network helper header file

Quoted 27 lines · Tony Dinh

Add include header file include/fdt_support_net.h

---

  include/fdt_support_net.h | 39 +++++++++++++++++++++++++++++++++++++++
  1 file changed, 39 insertions(+)

create mode 100644 include/fdt_support_net.h

include/fdt_support_net.h new file +12 -0
new file mode 100644
index 0000000000..4fe447f803
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0+
+ *
+ * Copyright (C) 2021 Tony Dinh <[email protected]>
+ */
+
+#ifndef __fdt_support_net_h
+#define __fdt_support_net_h
+
+/**
+ * This file contains convenience functions for decoding network related
+ * information from FDTs. It is intended to be used by board-specific code
+ * within U-Boot.

include/fdt_support_net.h line 22

Quoted 10 lines
include/fdt_support_net.h new file +10 -0
+ */
+
+/*
+ * fdt_get_phy_addr - Return the Ethernet PHY address
+ *
+ * Convenience function to return the PHY address of an
+ * ethernet device given its full path as defined in the device tree
+ *
+ * @path	full path to the network device node
+ * @return	PHY address, or -1 if it does not exist

Please change from "-1" to some meaningful error return code.

Other than that:

Thanks,
Stefan

Quoted 8 lines
include/fdt_support_net.h new file +7 -0
+ *
+ * Usage examples:
+ *
+ * Get PHY address of eth0 for a Kirkwood board as defined in kirkwood.dtsi
+ *	int phyaddr;
+ *	char *eth0_path = "/ocp@f1000000/ethernet-controller@72000";
+ *	phyaddr = fdt_get_phy_addr(eth0_path);

include/fdt_support_net.h line 39

Quoted 10 lines
include/fdt_support_net.h new file +10 -0
+ *
+ * Get PHY address of eth1 for a Armada 38x board as defined
+ * in armada-38x.dtsi
+ *	int phyaddr;
+ *	char *eth1_path = "/soc/ethernet@30000";
+ *	phyaddr = fdt_get_phy_addr(eth1_path);
+ */
+int fdt_get_phy_addr(const char *path);
+
+#endif

Viele Grüße,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
Acked-by on v1 1/3
#3 lore raw

Re: [PATCH 1/3] Add fdt network helper header file

In reply to Tony Dinh
Quoted 56 lines · Tony Dinh

Add include header file include/fdt_support_net.h

---

 include/fdt_support_net.h | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 include/fdt_support_net.h
include/fdt_support_net.h new file +39 -1
new file mode 100644
index 0000000000..4fe447f803
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0+
+ *
+ * Copyright (C) 2021 Tony Dinh <[email protected]>
+ */
+
+#ifndef __fdt_support_net_h
+#define __fdt_support_net_h
+
+/**
+ * This file contains convenience functions for decoding network related
+ * information from FDTs. It is intended to be used by board-specific code
+ * within U-Boot.
+ */
+
+/*
+ * fdt_get_phy_addr - Return the Ethernet PHY address
+ *
+ * Convenience function to return the PHY address of an
+ * ethernet device given its full path as defined in the device tree
+ *
+ * @path       full path to the network device node
+ * @return     PHY address, or -1 if it does not exist
+ *
+ * Usage examples:
+ *
+ * Get PHY address of eth0 for a Kirkwood board as defined in kirkwood.dtsi
+ *     int phyaddr;
+ *     char *eth0_path = "/ocp@f1000000/ethernet-controller@72000";
+ *     phyaddr = fdt_get_phy_addr(eth0_path);
+ *
+ * Get PHY address of eth1 for a Armada 38x board as defined
+ * in armada-38x.dtsi
+ *     int phyaddr;
+ *     char *eth1_path = "/soc/ethernet@30000";
+ *     phyaddr = fdt_get_phy_addr(eth1_path);
+ */
+int fdt_get_phy_addr(const char *path);
+
+#endif
--
Quoted 2 lines

2.20.1

#4 lore raw

Re: [PATCH 1/3] Add fdt network helper header file

In reply to Tony Dinh

Hi Tony,

Quoted 9 lines · Tony Dinh

Add include header file include/fdt_support_net.h

---

 include/fdt_support_net.h | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 include/fdt_support_net.h

Sorry for the delay but I am only now coming to this after being away for a month and finding it in my queue.

Can you please
- move this to drivers/core/of_extra.c and include/dm/ofnode.h
- use the livetree interface (ofnode) instead of fdt
- merge everything into a single patch since the first patch creates a build error

Regards,
Simon

author
#5 lore raw

PATCH 2/3 Add fdt network helper functions

In reply to Tony Dinh

Add fdt network helper functions common/fdt_support_net.c

---

 common/fdt_support_net.c | 46 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
 create mode 100644 common/fdt_support_net.c
common/fdt_support_net.c new file +46 -0
new file mode 100644
index 0000000000..06fa2175b4
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2021 Tony Dinh <[email protected]>
+ */
+
+#include <common.h>
+#include <linux/libfdt.h>
+#include <fdt_support.h>
+#include <asm/global_data.h>
+#include <fdt_support_net.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 -1;
+			/* 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 -1;
+}
Signature · 1 line
2.20.1
#6 lore raw

Re: [PATCH 2/3] Add fdt network helper functions

Hi Tony,

a few nits...

Quoted 15 lines · Tony Dinh

Add fdt network helper functions common/fdt_support_net.c

---

  common/fdt_support_net.c | 46 ++++++++++++++++++++++++++++++++++++++++
  1 file changed, 46 insertions(+)

create mode 100644 common/fdt_support_net.c

common/fdt_support_net.c new file +0 -0
new file mode 100644
index 0000000000..06fa2175b4

common/fdt_support_net.c line 6

Quoted 6 lines
common/fdt_support_net.c new file +6 -0
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2021 Tony Dinh <[email protected]>
+ */
+
+#include <common.h>

Including "common.h" is deprecated. Please remove it and include the missing other headers instead (if any).

common/fdt_support_net.c line 10

Quoted 4 lines
common/fdt_support_net.c new file +4 -0
+#include <linux/libfdt.h>
+#include <fdt_support.h>
+#include <asm/global_data.h>
+#include <fdt_support_net.h>

Sorting would be good as well.

Quoted 8 lines
common/fdt_support_net.c new file +8 -0
+
+DECLARE_GLOBAL_DATA_PTR;
+
+int fdt_get_phy_addr(const char *path)
+{
+	const void *fdt = gd->fdt_blob;
+	const u32 *reg;
+	const u32 *val;

common/fdt_support_net.c line 28

Quoted 10 lines
common/fdt_support_net.c new file +10 -0
+	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);

Above is a multi-line statement which is better included in parentheses.

And I personally would add an empty line here.

common/fdt_support_net.c line 32

Quoted 4 lines
common/fdt_support_net.c new file +4 -0
+		if (val) {
+			phandle = fdt32_to_cpu(*val);
+			if (!phandle)
+				return -1;

Could you instead of returning "-1" return some matching error code defines?

And I personally would add an empty line here.

common/fdt_support_net.c line 45

Quoted 13 lines
common/fdt_support_net.c new file +13 -0
+			/* 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 -1;

Again, please change to some matching error define here.

Thanks,
Stefan

author
#7 lore raw

Re: [PATCH 2/3] Add fdt network helper functions

Hi Stefan,

Quoted 86 lines · Stefan Roese

Hi Tony,

a few nits...

Quoted 15 lines · Tony Dinh

Add fdt network helper functions common/fdt_support_net.c

---

  common/fdt_support_net.c | 46 ++++++++++++++++++++++++++++++++++++++++
  1 file changed, 46 insertions(+)

create mode 100644 common/fdt_support_net.c

common/fdt_support_net.c new file +0 -0
new file mode 100644
index 0000000000..06fa2175b4

common/fdt_support_net.c line 6

Quoted 6 lines
common/fdt_support_net.c new file +6 -0
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2021 Tony Dinh <[email protected]>
+ */
+
+#include <common.h>

Including "common.h" is deprecated. Please remove it and include the missing other headers instead (if any).

common/fdt_support_net.c line 10

Quoted 4 lines
common/fdt_support_net.c new file +4 -0
+#include <linux/libfdt.h>
+#include <fdt_support.h>
+#include <asm/global_data.h>
+#include <fdt_support_net.h>

Sorting would be good as well.

Quoted 8 lines
common/fdt_support_net.c new file +8 -0
+
+DECLARE_GLOBAL_DATA_PTR;
+
+int fdt_get_phy_addr(const char *path)
+{
+     const void *fdt = gd->fdt_blob;
+     const u32 *reg;
+     const u32 *val;

common/fdt_support_net.c line 28

Quoted 10 lines
common/fdt_support_net.c new file +10 -0
+     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);

Above is a multi-line statement which is better included in parentheses.

And I personally would add an empty line here.

common/fdt_support_net.c line 32

Quoted 4 lines
common/fdt_support_net.c new file +4 -0
+             if (val) {
+                     phandle = fdt32_to_cpu(*val);
+                     if (!phandle)
+                             return -1;

Could you instead of returning "-1" return some matching error code defines?

And I personally would add an empty line here.

common/fdt_support_net.c line 45

Quoted 13 lines
common/fdt_support_net.c new file +13 -0
+                     /* 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 -1;
Quoted 2 lines

Again, please change to some matching error define here.

Will do all of the above and send in the V2 patch, after Ramon and Joe had any feedback.

Thanks,
Tony

Quoted 2 lines

Thanks,
Stefan

Acked-by
#8 lore raw

Re: [PATCH 2/3] Add fdt network helper functions

Quoted 90 lines · Tony Dinh

Hi Stefan,

Quoted 86 lines · Stefan Roese

Hi Tony,

a few nits...

Quoted 15 lines · Tony Dinh

Add fdt network helper functions common/fdt_support_net.c

---

  common/fdt_support_net.c | 46 ++++++++++++++++++++++++++++++++++++++++
  1 file changed, 46 insertions(+)

create mode 100644 common/fdt_support_net.c

common/fdt_support_net.c new file +0 -0
new file mode 100644
index 0000000000..06fa2175b4

common/fdt_support_net.c line 6

Quoted 6 lines
common/fdt_support_net.c new file +6 -0
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2021 Tony Dinh <[email protected]>
+ */
+
+#include <common.h>

Including "common.h" is deprecated. Please remove it and include the missing other headers instead (if any).

common/fdt_support_net.c line 10

Quoted 4 lines
common/fdt_support_net.c new file +4 -0
+#include <linux/libfdt.h>
+#include <fdt_support.h>
+#include <asm/global_data.h>
+#include <fdt_support_net.h>

Sorting would be good as well.

Quoted 8 lines
common/fdt_support_net.c new file +8 -0
+
+DECLARE_GLOBAL_DATA_PTR;
+
+int fdt_get_phy_addr(const char *path)
+{
+     const void *fdt = gd->fdt_blob;
+     const u32 *reg;
+     const u32 *val;

common/fdt_support_net.c line 28

Quoted 10 lines
common/fdt_support_net.c new file +10 -0
+     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);

Above is a multi-line statement which is better included in parentheses.

And I personally would add an empty line here.

common/fdt_support_net.c line 32

Quoted 4 lines
common/fdt_support_net.c new file +4 -0
+             if (val) {
+                     phandle = fdt32_to_cpu(*val);
+                     if (!phandle)
+                             return -1;

Could you instead of returning "-1" return some matching error code defines?

And I personally would add an empty line here.

Quoted 13 lines
common/fdt_support_net.c new file +13 -0
+                     /* 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 -1;
Quoted 11 lines
Quoted 2 lines

Again, please change to some matching error define here.

Will do all of the above and send in the V2 patch, after Ramon and Joe had any feedback.

Thanks,
Tony

Quoted 2 lines

Thanks,
Stefan

author
#9 lore raw

PATCH 3/3 Add fdt network helper to Makefile

In reply to Tony Dinh

Add fdt_support_net.c to common/Makefile

---

 common/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/Makefile +1 -1
index 9063ed9391..94678d26d8 100644
@@ -28,7 +28,7 @@ obj-$(CONFIG_CMD_BOOTZ) += bootm.o bootm_os.o
 obj-$(CONFIG_CMD_BOOTI) += bootm.o bootm_os.o
 
 obj-$(CONFIG_CMD_BEDBUG) += bedbug.o
-obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += fdt_support.o
+obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += fdt_support.o fdt_support_net.o
 obj-$(CONFIG_MII) += miiphyutil.o
 obj-$(CONFIG_CMD_MII) += miiphyutil.o
 obj-$(CONFIG_PHYLIB) += miiphyutil.o
Signature · 1 line
2.20.1
Reviewed-by on v1 3/3
#10 lore raw

Re: [PATCH 3/3] Add fdt network helper to Makefile

Quoted 14 lines · Tony Dinh

Add fdt_support_net.c to common/Makefile

---

  common/Makefile | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
common/Makefile +0 -0
index 9063ed9391..94678d26d8 100644

common/Makefile obj-$(CONFIG_CMD_BOOTZ) += bootm.o bootm_os.o · line 34

Quoted 8 lines
common/Makefile +1 -1
@@ -28,7 +28,7 @@ obj-$(CONFIG_CMD_BOOTZ) += bootm.o bootm_os.o
  obj-$(CONFIG_CMD_BOOTI) += bootm.o bootm_os.o
 
  obj-$(CONFIG_CMD_BEDBUG) += bedbug.o
-obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += fdt_support.o
+obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += fdt_support.o fdt_support_net.o
  obj-$(CONFIG_MII) += miiphyutil.o
  obj-$(CONFIG_CMD_MII) += miiphyutil.o
  obj-$(CONFIG_PHYLIB) += miiphyutil.o

Thanks,
Stefan

Acked-by on v1 3/3
#11 lore raw

Re: [PATCH 3/3] Add fdt network helper to Makefile

In reply to Tony Dinh
Quoted 23 lines · Tony Dinh

Add fdt_support_net.c to common/Makefile

---

 common/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/Makefile +1 -2
index 9063ed9391..94678d26d8 100644
@@ -28,7 +28,7 @@ obj-$(CONFIG_CMD_BOOTZ) += bootm.o bootm_os.o
 obj-$(CONFIG_CMD_BOOTI) += bootm.o bootm_os.o
 
 obj-$(CONFIG_CMD_BEDBUG) += bedbug.o
-obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += fdt_support.o
+obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += fdt_support.o fdt_support_net.o
 obj-$(CONFIG_MII) += miiphyutil.o
 obj-$(CONFIG_CMD_MII) += miiphyutil.o
 obj-$(CONFIG_PHYLIB) += miiphyutil.o
--
Quoted 2 lines

2.20.1

#12 lore raw
In reply to Tony Dinh

Hi Ramin, Hi Joe,

Quoted 7 lines · Tony Dinh

At the moment, there is no common fdt helper function specific to decoding network related information from FDTs. This new helper functional group fdt_support_net is intended to be used by board-specific code within U-Boot for various network related chores.

In this patch, create the 1st function fdt_get_phy_addr to parse the device tree to find the PHY addess of a specific ethernet device.

Ramon & Joe, could you please also take a look at this patchset? And let us know if this is okay or if you have some comments?

Thanks,
Stefan

Quoted 6 lines

Tony Dinh (3):
Add fdt network helper header file
Add fdt network helper functions
Add fdt network helper to Makefile

Quoted 7 lines
  common/Makefile           |  2 +-
  common/fdt_support_net.c  | 46 +++++++++++++++++++++++++++++++++++++++
  include/fdt_support_net.h | 39 +++++++++++++++++++++++++++++++++
  3 files changed, 86 insertions(+), 1 deletion(-)

create mode 100644 common/fdt_support_net.c
create mode 100644 include/fdt_support_net.h

Viele Grüße,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
author
#13 lore raw

PATCH v2 1/3 common: Add fdt network helper

In reply to Tony Dinh

Add fdt network helper to Makefile

---

(no changes since v1)

 common/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/Makefile +1 -1
index 9063ed9391..94678d26d8 100644
@@ -28,7 +28,7 @@ obj-$(CONFIG_CMD_BOOTZ) += bootm.o bootm_os.o
 obj-$(CONFIG_CMD_BOOTI) += bootm.o bootm_os.o
 
 obj-$(CONFIG_CMD_BEDBUG) += bedbug.o
-obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += fdt_support.o
+obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += fdt_support.o fdt_support_net.o
 obj-$(CONFIG_MII) += miiphyutil.o
 obj-$(CONFIG_CMD_MII) += miiphyutil.o
 obj-$(CONFIG_PHYLIB) += miiphyutil.o
Signature · 1 line
2.20.1
author
#14 lore raw

PATCH v2 2/3 common: Add fdt network helper header file

Add include header file fdt_support_net.h

---

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

 include/fdt_support_net.h | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 include/fdt_support_net.h
include/fdt_support_net.h new file +39 -0
new file mode 100644
index 0000000000..87852ffc41
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: GPL-2.0+
+ *
+ * Copyright (C) 2021 Tony Dinh <[email protected]>
+ */
+
+#ifndef __fdt_support_net_h
+#define __fdt_support_net_h
+
+/**
+ * This file contains convenience functions for decoding network related
+ * information from FDTs. It is intended to be used by board-specific code
+ * within U-Boot.
+ */
+
+/*
+ * fdt_get_phy_addr - Return the Ethernet PHY address
+ *
+ * Convenience function to return the PHY address of an
+ * ethernet device given its full path as defined in the device tree
+ *
+ * @path	full path to the network device node
+ * @return	PHY address, or -FDT_ERR_NOTFOUND if it does not exist
+ *
+ * Usage examples:
+ *
+ * Get PHY address of eth0 for a Kirkwood board as defined in kirkwood.dtsi
+ *	int phyaddr;
+ *	char *eth0_path = "/ocp@f1000000/ethernet-controller@72000";
+ *	phyaddr = fdt_get_phy_addr(eth0_path);
+ *
+ * Get PHY address of eth1 for a Armada 38x board as defined
+ * in armada-38x.dtsi
+ *	int phyaddr;
+ *	char *eth1_path = "/soc/ethernet@30000";
+ *	phyaddr = fdt_get_phy_addr(eth1_path);
+ */
+int fdt_get_phy_addr(const char *path);
+
+#endif
Signature · 1 line
2.20.1
author
#15 lore raw

PATCH v2 3/3 common: Add fdt network helper functions

In reply to Tony Dinh

Add fdt network helper functions fdt_support_net.c

---

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
common/fdt_support_net.c new file +47 -0
new file mode 100644
index 0000000000..46fa12dd7c
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2021 Tony Dinh <[email protected]>
+ */
+
+#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;
+}
Signature · 1 line
2.20.1
#16 lore raw
In reply to Tony Dinh

Hi Tony,

Quoted 15 lines · Tony Dinh

At the moment, there is no common fdt helper function specific to decoding network related information from FDTs. This new helper functional group fdt_support_net is intended to be used by board-specific code within U-Boot for various network related chores.

In this patch, create the 1st function fdt_get_phy_addr to parse the device tree to find the PHY addess of a specific ethernet device.

Tony Dinh (3):
Add fdt network helper header file
Add fdt network helper functions
Add fdt network helper to Makefile

Quoted 6 lines
 common/Makefile           |  2 +-
 common/fdt_support_net.c  | 46 +++++++++++++++++++++++++++++++++++++++
 include/fdt_support_net.h | 39 +++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 common/fdt_support_net.c
 create mode 100644 include/fdt_support_net.h

Can this use livetre and also have some tests?

Regards,
Simon

author
#17 lore raw

Hi Simon,

Quoted 19 lines · Simon Glass

Hi Tony,

Quoted 15 lines · Tony Dinh

At the moment, there is no common fdt helper function specific to decoding network related information from FDTs. This new helper functional group fdt_support_net is intended to be used by board-specific code within U-Boot for various network related chores.

In this patch, create the 1st function fdt_get_phy_addr to parse the device tree to find the PHY addess of a specific ethernet device.

Tony Dinh (3):
Add fdt network helper header file
Add fdt network helper functions
Add fdt network helper to Makefile

Quoted 8 lines
Quoted 6 lines
 common/Makefile           |  2 +-
 common/fdt_support_net.c  | 46 +++++++++++++++++++++++++++++++++++++++
 include/fdt_support_net.h | 39 +++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 common/fdt_support_net.c
 create mode 100644 include/fdt_support_net.h

Can this use livetre and also have some tests?

I have not enabled livetree for any of the boards I have. So I just modeled this using the existing ./common/fdt_support.c!

I do agree we should start using livetree in fdt helpers, if I understood it correctly, it should work for both flattree and livetree. Perhaps we could have another patch series after this? I am preparing another Kirkwood board support patch that I could hold off submitting and enable livetree to use that as a vehicle for testing.

Thanks,
Tony

Quoted 2 lines

Regards,
Simon

author
#18 lore raw

Hi Simon,

Quoted 35 lines · Tony Dinh

Hi Simon,

Quoted 19 lines · Simon Glass

Hi Tony,

Quoted 15 lines · Tony Dinh

At the moment, there is no common fdt helper function specific to decoding network related information from FDTs. This new helper functional group fdt_support_net is intended to be used by board-specific code within U-Boot for various network related chores.

In this patch, create the 1st function fdt_get_phy_addr to parse the device tree to find the PHY addess of a specific ethernet device.

Tony Dinh (3):
Add fdt network helper header file
Add fdt network helper functions
Add fdt network helper to Makefile

Quoted 8 lines
Quoted 6 lines
 common/Makefile           |  2 +-
 common/fdt_support_net.c  | 46 +++++++++++++++++++++++++++++++++++++++
 include/fdt_support_net.h | 39 +++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 common/fdt_support_net.c
 create mode 100644 include/fdt_support_net.h

Can this use livetre and also have some tests?

I have not enabled livetree for any of the boards I have. So I just modeled this using the existing ./common/fdt_support.c!

Quoted 11 lines

I do agree we should start using livetree in fdt helpers, if I understood it correctly, it should work for both flattree and livetree. Perhaps we could have another patch series after this? I am preparing another Kirkwood board support patch that I could hold off submitting and enable livetree to use that as a vehicle for testing.

Thanks,
Tony

Quoted 2 lines

Regards,
Simon

So is it OK for this patch to be accepted as is? It'll take a while for me to switch to the livetree approach and do testing.

Thanks,
Tony

#19 lore raw
In reply to Tony Dinh

Hi Tony,

Quoted 30 lines · Tony Dinh

Hi Simon,

Quoted 26 lines · Simon Glass

Hi Tony,

Quoted 21 lines · Tony Dinh

At the moment, there is no common fdt helper function specific to decoding network related information from FDTs. This new helper functional group fdt_support_net is intended to be used by board-specific code within U-Boot for various network related chores.

In this patch, create the 1st function fdt_get_phy_addr to parse the device tree to find the PHY addess of a specific ethernet device.

Tony Dinh (3):
Add fdt network helper header file
Add fdt network helper functions
Add fdt network helper to Makefile

 common/Makefile           |  2 +-
 common/fdt_support_net.c  | 46 +++++++++++++++++++++++++++++++++++++++
 include/fdt_support_net.h | 39 +++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 common/fdt_support_net.c
 create mode 100644 include/fdt_support_net.h
Quoted 7 lines
Quoted 1 line

Can this use livetre and also have some tests?

I have not enabled livetree for any of the boards I have. So I just modeled this using the existing ./common/fdt_support.c!

I do agree we should start using livetree in fdt helpers, if I understood it correctly, it should work for both flattree and

OK good, yes that's right.

Quoted 3 lines

livetree. Perhaps we could have another patch series after this? I am preparing another Kirkwood board support patch that I could hold off submitting and enable livetree to use that as a vehicle for testing.

I think it is better to use livetree in this patch. For testing, you can use sandbox for testing (see for example test/dm/ofnode.c)

Regards,
Simon

author
#20 lore raw

Hi Simon,

Quoted 48 lines · Simon Glass

Hi Tony,

Quoted 30 lines · Tony Dinh

Hi Simon,

Quoted 26 lines · Simon Glass

Hi Tony,

Quoted 21 lines · Tony Dinh

At the moment, there is no common fdt helper function specific to decoding network related information from FDTs. This new helper functional group fdt_support_net is intended to be used by board-specific code within U-Boot for various network related chores.

In this patch, create the 1st function fdt_get_phy_addr to parse the device tree to find the PHY addess of a specific ethernet device.

Tony Dinh (3):
Add fdt network helper header file
Add fdt network helper functions
Add fdt network helper to Makefile

 common/Makefile           |  2 +-
 common/fdt_support_net.c  | 46 +++++++++++++++++++++++++++++++++++++++
 include/fdt_support_net.h | 39 +++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 common/fdt_support_net.c
 create mode 100644 include/fdt_support_net.h
Quoted 7 lines
Quoted 1 line

Can this use livetre and also have some tests?

I have not enabled livetree for any of the boards I have. So I just modeled this using the existing ./common/fdt_support.c!

I do agree we should start using livetree in fdt helpers, if I understood it correctly, it should work for both flattree and

OK good, yes that's right.

Quoted 3 lines

livetree. Perhaps we could have another patch series after this? I am preparing another Kirkwood board support patch that I could hold off submitting and enable livetree to use that as a vehicle for testing.

Quoted 5 lines

I think it is better to use livetree in this patch. For testing, you can use sandbox for testing (see for example test/dm/ofnode.c)

Regards,
Simon

It seems it is too time consuming to implement this using livetree calls (with my limited understanding about livetree). I spent a few hours reading ./include/dm/read.h and ./include/dm/ofnode.h, and it is not apparent to me which functions to use. I see that we have eth_get_dev_by_name(), that's a start.

Do you have any objection if I submit this function as a patch to ./common/fdt_support.c? fdt_support.c file is all flatree implementation. And by the way, this new function fdt_get_phy_addr() has been tested with several Kirkwood boards that I have been converting to DM Ethernet.

When the time comes that it's mandatory to convert all to livetree calls, I'll be glad to help.

Thanks,
Tony

author
#21 lore raw

Hey Simon,

Quoted 64 lines · Tony Dinh

Hi Simon,

Quoted 48 lines · Simon Glass

Hi Tony,

Quoted 30 lines · Tony Dinh

Hi Simon,

Quoted 26 lines · Simon Glass

Hi Tony,

Quoted 21 lines · Tony Dinh

At the moment, there is no common fdt helper function specific to decoding network related information from FDTs. This new helper functional group fdt_support_net is intended to be used by board-specific code within U-Boot for various network related chores.

In this patch, create the 1st function fdt_get_phy_addr to parse the device tree to find the PHY addess of a specific ethernet device.

Tony Dinh (3):
Add fdt network helper header file
Add fdt network helper functions
Add fdt network helper to Makefile

 common/Makefile           |  2 +-
 common/fdt_support_net.c  | 46 +++++++++++++++++++++++++++++++++++++++
 include/fdt_support_net.h | 39 +++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 common/fdt_support_net.c
 create mode 100644 include/fdt_support_net.h
Quoted 7 lines
Quoted 1 line

Can this use livetre and also have some tests?

I have not enabled livetree for any of the boards I have. So I just modeled this using the existing ./common/fdt_support.c!

I do agree we should start using livetree in fdt helpers, if I understood it correctly, it should work for both flattree and

OK good, yes that's right.

Quoted 3 lines

livetree. Perhaps we could have another patch series after this? I am preparing another Kirkwood board support patch that I could hold off submitting and enable livetree to use that as a vehicle for testing.

Quoted 5 lines

I think it is better to use livetree in this patch. For testing, you can use sandbox for testing (see for example test/dm/ofnode.c)

Regards,
Simon

It seems it is too time consuming to implement this using livetree calls (with my limited understanding about livetree). I spent a few hours reading ./include/dm/read.h and ./include/dm/ofnode.h, and it is not apparent to me which functions to use. I see that we have eth_get_dev_by_name(), that's a start.

Quoted 8 lines

Do you have any objection if I submit this function as a patch to ./common/fdt_support.c? fdt_support.c file is all flatree implementation. And by the way, this new function fdt_get_phy_addr() has been tested with several Kirkwood boards that I have been converting to DM Ethernet.

When the time comes that it's mandatory to convert all to livetree calls, I'll be glad to help.

I know you're as busy as always ;) But I would appreciate either an ACK or NACK on my proposal to submit this as a patch to ./common/fdt_support.c.

Thanks,
Tony

#22 lore raw

Hi Tony,

Quoted 74 lines · Tony Dinh

Hey Simon,

Quoted 70 lines · Tony Dinh

Hi Simon,

Quoted 48 lines · Simon Glass

Hi Tony,

Quoted 30 lines · Tony Dinh

Hi Simon,

Quoted 26 lines · Simon Glass

Hi Tony,

Quoted 21 lines · Tony Dinh

At the moment, there is no common fdt helper function specific to decoding network related information from FDTs. This new helper functional group fdt_support_net is intended to be used by board-specific code within U-Boot for various network related chores.

In this patch, create the 1st function fdt_get_phy_addr to parse the device tree to find the PHY addess of a specific ethernet device.

Tony Dinh (3):
Add fdt network helper header file
Add fdt network helper functions
Add fdt network helper to Makefile

 common/Makefile           |  2 +-
 common/fdt_support_net.c  | 46 +++++++++++++++++++++++++++++++++++++++
 include/fdt_support_net.h | 39 +++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 common/fdt_support_net.c
 create mode 100644 include/fdt_support_net.h
Quoted 7 lines
Quoted 1 line

Can this use livetre and also have some tests?

I have not enabled livetree for any of the boards I have. So I just modeled this using the existing ./common/fdt_support.c!

I do agree we should start using livetree in fdt helpers, if I understood it correctly, it should work for both flattree and

OK good, yes that's right.

Quoted 3 lines

livetree. Perhaps we could have another patch series after this? I am preparing another Kirkwood board support patch that I could hold off submitting and enable livetree to use that as a vehicle for testing.

Quoted 5 lines

I think it is better to use livetree in this patch. For testing, you can use sandbox for testing (see for example test/dm/ofnode.c)

Regards,
Simon

It seems it is too time consuming to implement this using livetree calls (with my limited understanding about livetree). I spent a few hours reading ./include/dm/read.h and ./include/dm/ofnode.h, and it is not apparent to me which functions to use. I see that we have eth_get_dev_by_name(), that's a start.

Do you have any objection if I submit this function as a patch to ./common/fdt_support.c? fdt_support.c file is all flatree implementation. And by the way, this new function fdt_get_phy_addr() has been tested with several Kirkwood boards that I have been converting to DM Ethernet.

Quoted 6 lines
Quoted 2 lines

When the time comes that it's mandatory to convert all to livetree calls, I'll be glad to help.

I know you're as busy as always ;) But I would appreciate either an ACK or NACK on my proposal to submit this as a patch to ./common/fdt_support.c.

We should not be adding new functions that use flattree. If you are unsure which functions to use, please ask. But some examples:

int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);

u32 ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);

const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);

const char *ofnode_read_string(ofnode node, const char *propname);

It is easy enough to convert an offset into an ofnode and vice versa.

For adding tests, see test/dm/ofnode.c for examples.

https://u-boot.readthedocs.io/en/latest/develop/tests_sandbox.html

To run a particular test, a quick way is:

make sandbox_defconfig
make
./u-boot -T -c "ut dm ofnode"
./u-boot -T -c "ut dm dm_test_ofnode_get_by_phandle"

This bash function will run tests which match a particular string:

function pyt {
test/py/test.py -B sandbox --build-dir /tmp/b/sandbox ${1:+"-k $1"} }

e.g to run all ofnode tests:

$ pyt ofnode
=============================== test session starts
================================
platform linux -- Python 3.9.2, pytest-6.0.2, py-1.10.0, pluggy-0.13.0
rootdir: /home/sjg/c/src/third_party/u-boot/files/test/py, configfile:
pytest.ini
plugins: forked-1.3.0, hypothesis-5.43.3, xdist-2.2.0
collected 843 items / 828 deselected / 15 selected

test/py/tests/test_ut.py ...............

      [100%]

======================== 15 passed, 828 deselected in 0.67s
========================

Regards,
Simon

author
#23 lore raw

Hi Simon,

Quoted 81 lines · Simon Glass

Hi Tony,

Quoted 77 lines · Tony Dinh

Hey Simon,

Quoted 72 lines · Tony Dinh

Hi Simon,

Quoted 48 lines · Simon Glass

Hi Tony,

Quoted 30 lines · Tony Dinh

Hi Simon,

Quoted 26 lines · Simon Glass

Hi Tony,

Quoted 21 lines · Tony Dinh

At the moment, there is no common fdt helper function specific to decoding network related information from FDTs. This new helper functional group fdt_support_net is intended to be used by board-specific code within U-Boot for various network related chores.

In this patch, create the 1st function fdt_get_phy_addr to parse the device tree to find the PHY addess of a specific ethernet device.

Tony Dinh (3):
Add fdt network helper header file
Add fdt network helper functions
Add fdt network helper to Makefile

 common/Makefile           |  2 +-
 common/fdt_support_net.c  | 46 +++++++++++++++++++++++++++++++++++++++
 include/fdt_support_net.h | 39 +++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100644 common/fdt_support_net.c
 create mode 100644 include/fdt_support_net.h
Quoted 7 lines
Quoted 1 line

Can this use livetre and also have some tests?

I have not enabled livetree for any of the boards I have. So I just modeled this using the existing ./common/fdt_support.c!

I do agree we should start using livetree in fdt helpers, if I understood it correctly, it should work for both flattree and

OK good, yes that's right.

Quoted 3 lines

livetree. Perhaps we could have another patch series after this? I am preparing another Kirkwood board support patch that I could hold off submitting and enable livetree to use that as a vehicle for testing.

Quoted 5 lines

I think it is better to use livetree in this patch. For testing, you can use sandbox for testing (see for example test/dm/ofnode.c)

Regards,
Simon

It seems it is too time consuming to implement this using livetree calls (with my limited understanding about livetree). I spent a few hours reading ./include/dm/read.h and ./include/dm/ofnode.h, and it is not apparent to me which functions to use. I see that we have eth_get_dev_by_name(), that's a start.

Do you have any objection if I submit this function as a patch to ./common/fdt_support.c? fdt_support.c file is all flatree implementation. And by the way, this new function fdt_get_phy_addr() has been tested with several Kirkwood boards that I have been converting to DM Ethernet.

When the time comes that it's mandatory to convert all to livetree calls, I'll be glad to help.

Quoted 6 lines
Quoted 3 lines

I know you're as busy as always ;) But I would appreciate either an ACK or NACK on my proposal to submit this as a patch to ./common/fdt_support.c.

We should not be adding new functions that use flattree. If you are unsure which functions to use, please ask. But some examples:

Really appreciate this quick howto. I'll document this in the header prologue when I can get back to work on this patch.

Thanks,
Tony

Quoted 48 lines

int ofnode_read_u32(ofnode node, const char *propname, u32 *outp);

u32 ofnode_read_u32_default(ofnode ref, const char *propname, u32 def);

const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep);

const char *ofnode_read_string(ofnode node, const char *propname);

It is easy enough to convert an offset into an ofnode and vice versa.

For adding tests, see test/dm/ofnode.c for examples.

https://u-boot.readthedocs.io/en/latest/develop/tests_sandbox.html

To run a particular test, a quick way is:

make sandbox_defconfig
make
./u-boot -T -c "ut dm ofnode"
./u-boot -T -c "ut dm dm_test_ofnode_get_by_phandle"

This bash function will run tests which match a particular string:

function pyt {
test/py/test.py -B sandbox --build-dir /tmp/b/sandbox ${1:+"-k $1"} }

e.g to run all ofnode tests:

$ pyt ofnode
=============================== test session starts
================================
platform linux -- Python 3.9.2, pytest-6.0.2, py-1.10.0, pluggy-0.13.0
rootdir: /home/sjg/c/src/third_party/u-boot/files/test/py, configfile:
pytest.ini
plugins: forked-1.3.0, hypothesis-5.43.3, xdist-2.2.0
collected 843 items / 828 deselected / 15 selected

test/py/tests/test_ut.py ...............

      [100%]

======================== 15 passed, 828 deselected in 0.67s
========================

Regards,
Simon