[net-next,v3,1/2] net: phy: realtek: Clean up RTL8211E ExtPage access

Michael Klein
2025-03-16T12:17:49Z

- Factor out RTL8211E extension page access code to
rtl8211e_modify_ext_page()/rtl8211e_read_ext_page() and add some
related #define:s
- Group RTL8211E_* and RTL8211F_* #define:s
- Clean up rtl8211e_config_init()

Signed-off-by: Michael Klein <michael@fossekall.de>
---
drivers/net/phy/realtek/realtek_main.c | 49 ++++++++++++++------------
1 file changed, 27 insertions(+), 22 deletions(-)

diff --git a/drivers/net/phy/realtek/realtek_main.c b/drivers/net/phy/realtek/realtek_main.c
index 7a0b19d66aca..f4ce6457d0ef 100644
--- a/drivers/net/phy/realtek/realtek_main.c
+++ b/drivers/net/phy/realtek/realtek_main.c
@@ -28,13 +28,21 @@

#define RTL821x_INSR 0x13

-#define RTL821x_EXT_PAGE_SELECT 0x1e
+#define RTL8211E_EXT_PAGE_SELECT 0x1e
#define RTL821x_PAGE_SELECT 0x1f

+#define RTL8211E_SET_EXT_PAGE 0x07
+
+#define RTL8211E_CTRL_DELAY BIT(13)
+#define RTL8211E_TX_DELAY BIT(12)
+#define RTL8211E_RX_DELAY BIT(11)
+
#define RTL8211F_PHYCR1 0x18
#define RTL8211F_PHYCR2 0x19
#define RTL8211F_INSR 0x1d

+#define RTL8211F_CLKOUT_EN BIT(0)
+
#define RTL8211F_LEDCR 0x10
#define RTL8211F_LEDCR_MODE BIT(15)
#define RTL8211F_LEDCR_ACT_TXRX BIT(4)
@@ -51,12 +59,6 @@
#define RTL8211F_ALDPS_ENABLE BIT(2)
#define RTL8211F_ALDPS_XTAL_OFF BIT(12)

-#define RTL8211E_CTRL_DELAY BIT(13)
-#define RTL8211E_TX_DELAY BIT(12)
-#define RTL8211E_RX_DELAY BIT(11)
-
-#define RTL8211F_CLKOUT_EN BIT(0)
-
#define RTL8201F_ISR 0x1e
#define RTL8201F_ISR_ANERR BIT(15)
#define RTL8201F_ISR_DUPLEX BIT(13)
@@ -134,6 +136,21 @@ static int rtl821x_write_page(struct phy_device *phydev, int page)
return __phy_write(phydev, RTL821x_PAGE_SELECT, page);
}

+static int rtl8211e_modify_ext_page(struct phy_device *phydev, u16 ext_page,
+ u32 regnum, u16 mask, u16 set)
+{
+ int oldpage, ret = 0;
+
+ oldpage = phy_select_page(phydev, RTL8211E_SET_EXT_PAGE);
+ if (oldpage >= 0) {
+ ret = __phy_write(phydev, RTL8211E_EXT_PAGE_SELECT, ext_page);
+ if (!ret)
+ ret = __phy_modify(phydev, regnum, mask, set);
+ }
+
+ return phy_restore_page(phydev, oldpage, ret);
+}
+
static int rtl821x_probe(struct phy_device *phydev)
{
struct device *dev = &phydev->mdio.dev;
@@ -600,7 +617,8 @@ static int rtl8211f_led_hw_control_set(struct phy_device *phydev, u8 index,

static int rtl8211e_config_init(struct phy_device *phydev)
{
- int ret = 0, oldpage;
+ const u16 delay_mask = RTL8211E_CTRL_DELAY |
+ RTL8211E_TX_DELAY | RTL8211E_RX_DELAY;
u16 val;

/* enable TX/RX delay for rgmii-* modes, and disable them for rgmii. */
@@ -630,20 +648,7 @@ static int rtl8211e_config_init(struct phy_device *phydev)
* 12 = RX Delay, 11 = TX Delay
* 10:0 = Test && debug settings reserved by realtek
*/
- oldpage = phy_select_page(phydev, 0x7);
- if (oldpage < 0)
- goto err_restore_page;
-
- ret = __phy_write(phydev, RTL821x_EXT_PAGE_SELECT, 0xa4);
- if (ret)
- goto err_restore_page;
-
- ret = __phy_modify(phydev, 0x1c, RTL8211E_CTRL_DELAY
- | RTL8211E_TX_DELAY | RTL8211E_RX_DELAY,
- val);
-
-err_restore_page:
- return phy_restore_page(phydev, oldpage, ret);
+ return rtl8211e_modify_ext_page(phydev, 0xa4, 0x1c, delay_mask, val);
}

static int rtl8211b_suspend(struct phy_device *phydev)
--
2.39.5