From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: JohnTheCoolingFan <ivan8215145640@gmail.com>
Date: Fri, 9 Aug 2024 07:14:37 +0000
Subject: Allwinner H616: Setup internal PHY (EMAC1)

Signed-off-by: JohnTheCoolingFan <ivan8215145640@gmail.com>
---
 arch/arm/mach-sunxi/board.c           |  1 +
 arch/arm/mach-sunxi/clock_sun50i_h6.c |  4 ++
 board/sunxi/board.c                   | 18 ++++++
 drivers/net/sun8i_emac.c              |  7 +++
 drivers/pinctrl/sunxi/pinctrl-sunxi.c |  1 +
 8 files changed, 82 insertions(+)

diff --git a/arch/arm/mach-sunxi/board.c b/arch/arm/mach-sunxi/board.c
index 7d2d5d478fc..ca084bddda0 100644
--- a/arch/arm/mach-sunxi/board.c
+++ b/arch/arm/mach-sunxi/board.c
@@ -479,10 +479,11 @@ void board_init_f(ulong dummy)
 
 #if CONFIG_IS_ENABLED(I2C) && CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
 	/* Needed early by sunxi_board_init if PMU is enabled */
 	i2c_init_board();
 	i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
+	i2c_set_bus_num(0);
 #endif
 	sunxi_board_init();
 }
 #endif /* CONFIG_SPL_BUILD */
 
diff --git a/arch/arm/mach-sunxi/clock_sun50i_h6.c b/arch/arm/mach-sunxi/clock_sun50i_h6.c
index 7f60b8c79fc..8c97ddb66c4 100644
--- a/arch/arm/mach-sunxi/clock_sun50i_h6.c
+++ b/arch/arm/mach-sunxi/clock_sun50i_h6.c
@@ -51,10 +51,14 @@ void clock_init_safe(void)
 	/*
 	 * The mux and factor are set, but the clock will be enabled in
 	 * DRAM initialization code.
 	 */
 	writel(MBUS_CLK_SRC_PLL6X2 | MBUS_CLK_M(3), &ccm->mbus_cfg);
+
+	writel(0x10001, 0x030017ac);
+	writel(0x50, 0x0300a028);
+	writel(0x20, 0x0300a040);
 }
 #endif
 
 void clock_init_uart(void)
 {
diff --git a/board/sunxi/board.c b/board/sunxi/board.c
index 2163fac5e06..302a927f524 100644
--- a/board/sunxi/board.c
+++ b/board/sunxi/board.c
@@ -568,10 +580,11 @@ static void sunxi_spl_store_dram_size(phys_addr_t dram_size)
 }
 
 void sunxi_board_init(void)
 {
 	int power_failed = 0;
+	u8 data[2];
 
 #ifdef CONFIG_LED_STATUS
 	if (IS_ENABLED(CONFIG_SPL_DRIVERS_MISC))
 		status_led_init();
 #endif
@@ -658,10 +671,27 @@ void sunxi_board_init(void)
 	 */
 	if (!power_failed)
 		clock_set_pll1(get_board_sys_clk());
 	else
 		printf("Failed to set core voltage! Can't set CPU frequency\n");
+
+	i2c_set_bus_num(1);
+	data[0] = 0;
+	data[1] = 1;
+	i2c_write(0x10, 0xfe, 1, data, 2);
+	i2c_write(0x10, 2, 1, data, 2);
+	data[1] = 1;
+	i2c_write(0x10, 2, 1, data, 2);
+	data[1] = 0xf;
+	i2c_write(0x10, 0x16, 1, data, 2);
+	data[1] = 3;
+	i2c_write(0x10, 0x14, 1, data, 2);
+	data[1] = 0x60;
+	i2c_write(0x10, 0xfe, 1, data, 2);
+	data[0] = 0x08;
+	data[1] = 0x14;
+	i2c_write(0x10, 0, 1, data, 2);
 }
 #endif /* CONFIG_SPL_BUILD */
 
 #ifdef CONFIG_USB_GADGET
 int g_dnl_board_usb_cable_connected(void)
