Skip to contentSkip to conversation

Add support for TH1520-integrated GMACs

PATCH In review Yao Zi 2 participants 11 messages Jul 10, 2025 – Jul 17, 2025

Subsystems (4)
lore
Share & tools
lore

Yao Zi

author
#0 lore raw

cover Add support for TH1520-integrated GMACs

TH1520 SoC integrates two MAC controllers based on Designware IP that could operate at 1 Gbps. This series ports the MAC glue driver from Linux, and then enables networking on the TH1520-based Lichee Pi 4A board.

The driver could send and receive data correctly under link speed of 10Mbps, 100Mbps and 1Gbps. Under 1Gbps, tftp could transfer data at more than 10MiB/s.

Note the bug fixed by the first patch doesn't cause any problem with existing supported peripherals, thus the fix isn't urgent.

Yao Zi (5):
clk: thead: th1520-ap: Correctly handle flags for dividers
riscv: cpu: th1520: Limit upper RAM boundary to 4 GiB
drivers: net: Add T-Head DWMAC glue layer
riscv: dts: th1520: Describe GMACs and enable them on Lichee Pi 4A
configs: th1520_lpi4a: Enable network support

 MAINTAINERS                                 |   1 +
 arch/riscv/cpu/th1520/dram.c                |  16 ++
 arch/riscv/dts/th1520-lichee-module-4a.dtsi | 119 ++++++++
 arch/riscv/dts/th1520.dtsi                  |  42 +++
 configs/th1520_lpi4a_defconfig              |   7 +-
 drivers/clk/thead/clk-th1520-ap.c           |   4 +-
 drivers/net/Kconfig                         |   8 +
 drivers/net/Makefile                        |   1 +
 drivers/net/dwmac_thead.c                   | 288 ++++++++++++++++++++
 9 files changed, 484 insertions(+), 2 deletions(-)
 create mode 100644 drivers/net/dwmac_thead.c
Signature · 1 line
2.50.0
author
#1 lore raw

PATCH 1/5 clk: thead: th1520-ap: Correctly handle flags for dividers

Unlike the gate clocks which make no use of flags, most dividers in TH1520 SoC are one-based, thus are applied with CLK_DIVIDER_ONE_BASED flag. We couldn't simply ignore the flag, which causes wrong results when calculating the clock rates.

Add a member to ccu_div_internal for defining the flags, and pass it to divider_recalc_rate(). With this fix, frequency of all the clocks match the Linux kernel's calculation.

Fixes e6bfa6fc94f ("clk: thead: Port clock controller driver of TH1520 SoC")
---
 drivers/clk/thead/clk-th1520-ap.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/clk/thead/clk-th1520-ap.c +3 -1
index b80ad05b8ad..822cf0809d5 100644
@@ -32,6 +32,7 @@ struct ccu_internal {
 struct ccu_div_internal {
 	u8 shift;
 	u8 width;
+	unsigned long flags;
 };
 
 struct ccu_common {
@@ -79,6 +80,7 @@ struct ccu_pll {
 	{								\
 		.shift	= _shift,					\
 		.width	= _width,					\
+		.flags	= _flags,					\
 	}
 
 #define CCU_GATE(_clkid, _struct, _name, _parent, _reg, _gate, _flags)	\
@@ -182,7 +184,7 @@ static unsigned long ccu_div_get_rate(struct clk *clk)
 	val = val >> cd->div.shift;
 	val &= GENMASK(cd->div.width - 1, 0);
 	rate = divider_recalc_rate(clk, clk_get_parent_rate(clk), val, NULL,
-				   0, cd->div.width);
+				   cd->div.flags, cd->div.width);
 
 	return rate;
 }
Signature · 1 line
2.50.0
Acked-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] clk: thead: th1520-ap: Correctly handle flags for dividers

Quoted 9 lines · Yao Zi

Unlike the gate clocks which make no use of flags, most dividers in TH1520 SoC are one-based, thus are applied with CLK_DIVIDER_ONE_BASED flag. We couldn't simply ignore the flag, which causes wrong results when calculating the clock rates.

Add a member to ccu_div_internal for defining the flags, and pass it to divider_recalc_rate(). With this fix, frequency of all the clocks match the Linux kernel's calculation.

Quoted 5 lines
Fixes e6bfa6fc94f ("clk: thead: Port clock controller driver of TH1520 SoC")
---
 drivers/clk/thead/clk-th1520-ap.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
author
#3 lore raw

PATCH 2/5 riscv: cpu: th1520: Limit upper RAM boundary to 4 GiB

In reply to Yao Zi

TH1520 SoC ships DMA peripherals that could only reach the first 32-bit range of memory, for example, the GMAC controllers. Let's limit the usable top of RAM below 4GiB to ensure DMA allocations are accessible to all peripherals.

---
 arch/riscv/cpu/th1520/dram.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
arch/riscv/cpu/th1520/dram.c +16 -0
index 91007c0a3d3..8a0ca26785e 100644
@@ -19,3 +19,19 @@ int dram_init_banksize(void)
 {
 	return fdtdec_setup_memory_banksize();
 }
+
+phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
+{
+	/*
+	 * Ensure that we run from first 4GB so that all
+	 * addresses used by U-Boot are 32bit addresses.
+	 *
+	 * This in-turn ensures that 32bit DMA capable
+	 * devices work fine because DMA mapping APIs will
+	 * provide 32bit DMA addresses only.
+	 */
+	if (gd->ram_top > SZ_4G)
+		return SZ_4G;
+
+	return gd->ram_top;
+}
Signature · 1 line
2.50.0
Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] riscv: cpu: th1520: Limit upper RAM boundary to 4 GiB

Quoted 9 lines · Yao Zi

TH1520 SoC ships DMA peripherals that could only reach the first 32-bit range of memory, for example, the GMAC controllers. Let's limit the usable top of RAM below 4GiB to ensure DMA allocations are accessible to all peripherals.

---
 arch/riscv/cpu/th1520/dram.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
author
#5 lore raw

PATCH 3/5 drivers: net: Add T-Head DWMAC glue layer

In reply to Yao Zi

The Designware IP integrated in TH1520 SoC requires extra clock configuration to operate correctly. The Linux kernel's T-Head DWMAC glue driver is ported and adapted to U-Boot's API.

---
 MAINTAINERS               |   1 +
 drivers/net/Kconfig       |   8 ++
 drivers/net/Makefile      |   1 +
 drivers/net/dwmac_thead.c | 288 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 298 insertions(+)
 create mode 100644 drivers/net/dwmac_thead.c
MAINTAINERS +1 -0
index d5264c8f5df..438f24f1ff7 100644
@@ -1576,6 +1576,7 @@ M:	Yao Zi <[email protected]>
 S:	Maintained
 F:	arch/riscv/cpu/th1520/
 F:	drivers/clk/thead/clk-th1520-ap.c
+F:	drivers/net/dwmac_thead.c
 F:	drivers/pinctrl/pinctrl-th1520.c
 F:	drivers/ram/thead/th1520_ddr.c
 
drivers/net/Kconfig +8 -0
index 950ed0f25a9..d942fa4e202 100644
@@ -411,6 +411,14 @@ config ETH_DESIGNWARE_S700
 	  This provides glue layer to use Synopsys Designware Ethernet MAC
 	  present on Actions S700 SoC.
 
+config ETH_DESIGNWARE_THEAD
+	bool "T-Head glue driver for Synopsys Designware Ethernet MAC"
+	depends on ETH_DESIGNWARE
+	select DW_ALTDESCRIPTOR
+	help
+	  This provides glue layer to use Synopsys Designware Ethernet MAC
+	  present on T-Head SoCs.
+
 config DW_ALTDESCRIPTOR
 	bool "Designware Ethernet MAC uses alternate (enhanced) descriptors"
 	depends on ETH_DESIGNWARE
drivers/net/Makefile +1 -0
index 67bba3a8536..79cc8b422b0 100644
@@ -38,6 +38,7 @@ obj-$(CONFIG_ETH_DESIGNWARE) += designware.o
 obj-$(CONFIG_ETH_DESIGNWARE_MESON8B) += dwmac_meson8b.o
 obj-$(CONFIG_ETH_DESIGNWARE_S700) += dwmac_s700.o
 obj-$(CONFIG_ETH_DESIGNWARE_SOCFPGA) += dwmac_socfpga.o
+obj-$(CONFIG_ETH_DESIGNWARE_THEAD) += dwmac_thead.o
 obj-$(CONFIG_ETH_SANDBOX) += sandbox.o
 obj-$(CONFIG_ETH_SANDBOX_RAW) += sandbox-raw-bus.o
 obj-$(CONFIG_ETH_SANDBOX_RAW) += sandbox-raw.o
drivers/net/dwmac_thead.c new file +288 -0

288 lines not on this page · raw message

Signature · 1 line
2.50.0
Acked-by on v1 3/5
#6 lore raw

Re: [PATCH 3/5] drivers: net: Add T-Head DWMAC glue layer

Quoted 4 lines · Yao Zi

The Designware IP integrated in TH1520 SoC requires extra clock configuration to operate correctly. The Linux kernel's T-Head DWMAC glue driver is ported and adapted to U-Boot's API.

Quoted 8 lines
---
 MAINTAINERS               |   1 +
 drivers/net/Kconfig       |   8 ++
 drivers/net/Makefile      |   1 +
 drivers/net/dwmac_thead.c | 288 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 298 insertions(+)
 create mode 100644 drivers/net/dwmac_thead.c
author
#7 lore raw

PATCH 4/5 riscv: dts: th1520: Describe GMACs and enable them on Lichee Pi 4A

In reply to Yao Zi

TH1520 SoC ships two MAC controllers based on Designware Ethernet IP that are capable of Gigabit operation. Describe them in SoC devicetree and enable them for Lichee Pi 4A.

---
 arch/riscv/dts/th1520-lichee-module-4a.dtsi | 119 ++++++++++++++++++++
 arch/riscv/dts/th1520.dtsi                  |  42 +++++++
 2 files changed, 161 insertions(+)
arch/riscv/dts/th1520-lichee-module-4a.dtsi +119 -0
index 9b255f8243c..eecd3e9832a 100644
@@ -11,6 +11,11 @@
 	model = "Sipeed Lichee Module 4A";
 	compatible = "sipeed,lichee-module-4a", "thead,th1520";
 
+	aliases {
+		ethernet0 = &gmac0;
+		ethernet1 = &gmac1;
+	};
+
 	memory@0 {
 		device_type = "memory";
 		reg = <0x0 0x00000000 0x2 0x00000000>;
@@ -38,6 +43,120 @@
 	status = "okay";
 };
 
+&gmac0 {
+	pinctrl-names = "default";
+	pinctrl-0 = <&gmac0_pins>, <&mdio0_pins>;
+	phy-handle = <&phy0>;
+	phy-mode = "rgmii-id";
+	status = "okay";
+};
+
+&gmac1 {
+	pinctrl-names = "default";
+	pinctrl-0 = <&gmac1_pins>;
+	phy-handle = <&phy1>;
+	phy-mode = "rgmii-id";
+	status = "okay";
+};
+
+&mdio0 {
+	phy0: ethernet-phy@1 {
+		reg = <1>;
+	};
+
+	phy1: ethernet-phy@2 {
+		reg = <2>;
+	};
+};
+
+&padctrl0_apsys {
+	gmac0_pins: gmac0-0 {
+		tx-pins {
+			pins = "GMAC0_TX_CLK",
+			       "GMAC0_TXEN",
+			       "GMAC0_TXD0",
+			       "GMAC0_TXD1",
+			       "GMAC0_TXD2",
+			       "GMAC0_TXD3";
+			function = "gmac0";
+			bias-disable;
+			drive-strength = <25>;
+			input-disable;
+			input-schmitt-disable;
+			slew-rate = <0>;
+		};
+
+		rx-pins {
+			pins = "GMAC0_RX_CLK",
+			       "GMAC0_RXDV",
+			       "GMAC0_RXD0",
+			       "GMAC0_RXD1",
+			       "GMAC0_RXD2",
+			       "GMAC0_RXD3";
+			function = "gmac0";
+			bias-disable;
+			drive-strength = <1>;
+			input-enable;
+			input-schmitt-disable;
+			slew-rate = <0>;
+		};
+	};
+
+	gmac1_pins: gmac1-0 {
+		tx-pins {
+			pins = "GPIO2_18", /* GMAC1_TX_CLK */
+			       "GPIO2_20", /* GMAC1_TXEN */
+			       "GPIO2_21", /* GMAC1_TXD0 */
+			       "GPIO2_22", /* GMAC1_TXD1 */
+			       "GPIO2_23", /* GMAC1_TXD2 */
+			       "GPIO2_24"; /* GMAC1_TXD3 */
+			function = "gmac1";
+			bias-disable;
+			drive-strength = <25>;
+			input-disable;
+			input-schmitt-disable;
+			slew-rate = <0>;
+		};
+
+		rx-pins {
+			pins = "GPIO2_19", /* GMAC1_RX_CLK */
+			       "GPIO2_25", /* GMAC1_RXDV */
+			       "GPIO2_30", /* GMAC1_RXD0 */
+			       "GPIO2_31", /* GMAC1_RXD1 */
+			       "GPIO3_0",  /* GMAC1_RXD2 */
+			       "GPIO3_1";  /* GMAC1_RXD3 */
+			function = "gmac1";
+			bias-disable;
+			drive-strength = <1>;
+			input-enable;
+			input-schmitt-disable;
+			slew-rate = <0>;
+		};
+	};
+
+	mdio0_pins: mdio0-0 {
+		mdc-pins {
+			pins = "GMAC0_MDC";
+			function = "gmac0";
+			bias-disable;
+			drive-strength = <13>;
+			input-disable;
+			input-schmitt-disable;
+			slew-rate = <0>;
+		};
+
+		mdio-pins {
+			pins = "GMAC0_MDIO";
+			function = "gmac0";
+			bias-disable;
+			drive-strength = <13>;
+			input-enable;
+			input-schmitt-enable;
+			slew-rate = <0>;
+		};
+	};
+};
+
 &sdio0 {
 	bus-width = <4>;
 	max-frequency = <198000000>;
arch/riscv/dts/th1520.dtsi +42 -0

48 lines not on this page · raw message

Signature · 1 line
2.50.0
Acked-by on v1 4/5
#8 lore raw

Re: [PATCH 4/5] riscv: dts: th1520: Describe GMACs and enable them on Lichee Pi 4A

Quoted 9 lines · Yao Zi

TH1520 SoC ships two MAC controllers based on Designware Ethernet IP that are capable of Gigabit operation. Describe them in SoC devicetree and enable them for Lichee Pi 4A.

---
 arch/riscv/dts/th1520-lichee-module-4a.dtsi | 119 ++++++++++++++++++++
 arch/riscv/dts/th1520.dtsi                  |  42 +++++++
 2 files changed, 161 insertions(+)
author
#9 lore raw

PATCH 5/5 configs: th1520_lpi4a: Enable network support

In reply to Yao Zi

Enable the network stack, the designware ethernet driver and corresponding glue driver. The Lichee Pi 4A board ships two RTL8211F phys, both attached to GMAC 0, thus support for Realtek phys and DM support for MDIO devices are enabled as well.

---
 configs/th1520_lpi4a_defconfig | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
configs/th1520_lpi4a_defconfig +6 -1
index 78e3b25ab82..2763cbb428a 100644
@@ -76,7 +76,7 @@ CONFIG_MMC_SPEED_MODE_SET=y
 CONFIG_PARTITION_TYPE_GUID=y
 CONFIG_ENV_RELOC_GD_ENV_ADDR=y
 CONFIG_VERSION_VARIABLE=y
-CONFIG_NO_NET=y
+CONFIG_NET_RANDOM_ETHADDR=y
 # CONFIG_BLOCK_CACHE is not set
 CONFIG_DWAPB_GPIO=y
 # CONFIG_I2C is not set
@@ -90,6 +90,11 @@ CONFIG_MMC_SDHCI=y
 CONFIG_MMC_SDHCI_ADMA=y
 CONFIG_MMC_SDHCI_SNPS=y
 # CONFIG_MTD is not set
+CONFIG_PHY_REALTEK=y
+CONFIG_DM_MDIO=y
+CONFIG_DM_ETH_PHY=y
+CONFIG_ETH_DESIGNWARE=y
+CONFIG_ETH_DESIGNWARE_THEAD=y
 CONFIG_PINCTRL=y
 # CONFIG_POWER is not set
 CONFIG_RAM=y
Signature · 1 line
2.50.0
Reviewed-by on v1 5/5
#10 lore raw

Re: [PATCH 5/5] configs: th1520_lpi4a: Enable network support

Quoted 9 lines · Yao Zi

Enable the network stack, the designware ethernet driver and corresponding glue driver. The Lichee Pi 4A board ships two RTL8211F phys, both attached to GMAC 0, thus support for Realtek phys and DM support for MDIO devices are enabled as well.

---
 configs/th1520_lpi4a_defconfig | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)