[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20260116173920.371523-1-dam.dejean@gmail.com>
Date: Fri, 16 Jan 2026 18:39:19 +0100
From: Damien Dejean <dam.dejean@...il.com>
To: netdev@...r.kernel.org
Cc: Damien Dejean <dam.dejean@...il.com>
Subject: [PATCH 1/2] net: phy: realtek: add RTL8224 pair swap support
The RTL8224 has a register to configure a pair swap (from ABCD order to
DCBA) providing PCB designers more flexbility when wiring the chip. The
swap parameter has to be set correctly for each of the 4 ports before
the chip can detect a link.
After a reset, this register is (unfortunately) left in a random state,
thus it has to be initialized. On most of the devices the bootloader
does it once for all and we can rely on the value set, on some other it
is not and the kernel has to do it.
The MDI pair swap can be set in the device tree using the property
realtek,mdi-pair-swap. The property is set to "off" to disable the pair
swap, "on" to enable it, or "keep"/unset to keep the current
configuration.
Signed-off-by: Damien Dejean <dam.dejean@...il.com>
---
.../bindings/net/realtek,rtl82xx.yaml | 13 ++++
drivers/net/phy/realtek/Kconfig | 1 +
drivers/net/phy/realtek/realtek_main.c | 74 +++++++++++++++++++
3 files changed, 88 insertions(+)
diff --git a/Documentation/devicetree/bindings/net/realtek,rtl82xx.yaml b/Documentation/devicetree/bindings/net/realtek,rtl82xx.yaml
index 2b5697bd7c5d..2d04d90f8b97 100644
--- a/Documentation/devicetree/bindings/net/realtek,rtl82xx.yaml
+++ b/Documentation/devicetree/bindings/net/realtek,rtl82xx.yaml
@@ -55,6 +55,18 @@ properties:
description:
Enable Wake-on-LAN support for the RTL8211F PHY.
+ realtek,mdi-pair-swap:
+ description:
+ Enable or disable the swap of the ethernet pairs (from ABCD to DCBA).
+ The "keep" setting will keep the pair configuration at whatever its
+ current state is.
+ $ref: /schemas/types.yaml#/definitions/string
+ enum:
+ - keep
+ - on
+ - off
+ default: keep
+
unevaluatedProperties: false
allOf:
@@ -79,5 +91,6 @@ examples:
reg = <1>;
realtek,clkout-disable;
realtek,aldps-enable;
+ realtek,mdi-pair-swap = "on";
};
};
diff --git a/drivers/net/phy/realtek/Kconfig b/drivers/net/phy/realtek/Kconfig
index b05c2a1e9024..a741b34d193e 100644
--- a/drivers/net/phy/realtek/Kconfig
+++ b/drivers/net/phy/realtek/Kconfig
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: GPL-2.0-only
config REALTEK_PHY
tristate "Realtek PHYs"
+ select PHY_PACKAGE
help
Currently supports RTL821x/RTL822x and fast ethernet PHYs
diff --git a/drivers/net/phy/realtek/realtek_main.c b/drivers/net/phy/realtek/realtek_main.c
index 6ff0385201a5..e01bfad37cf5 100644
--- a/drivers/net/phy/realtek/realtek_main.c
+++ b/drivers/net/phy/realtek/realtek_main.c
@@ -18,6 +18,7 @@
#include <linux/clk.h>
#include <linux/string_choices.h>
+#include "../phylib.h"
#include "realtek.h"
#define RTL8201F_IER 0x13
@@ -162,6 +163,8 @@
#define RTL8224_SRAM_RTCT_LEN(pair) (0x8028 + (pair) * 4)
+#define RTL8224_VND1_MDI_PAIR_SWAP 0xa90
+
#define RTL8366RB_POWER_SAVE 0x15
#define RTL8366RB_POWER_SAVE_ON BIT(12)
@@ -208,6 +211,16 @@ struct rtl821x_priv {
u16 iner;
};
+enum pair_swap_state {
+ PAIR_SWAP_KEEP = 0,
+ PAIR_SWAP_OFF = 1,
+ PAIR_SWAP_ON = 2,
+};
+
+struct rtl8224_priv {
+ enum pair_swap_state pair_swap;
+};
+
static int rtl821x_read_page(struct phy_device *phydev)
{
return __phy_read(phydev, RTL821x_PAGE_SELECT);
@@ -1683,6 +1696,65 @@ static int rtl8224_cable_test_get_status(struct phy_device *phydev, bool *finish
return rtl8224_cable_test_report(phydev, finished);
}
+static void rtl8224_mdi_pair_swap(struct phy_device *phydev, bool swap)
+{
+ u32 val;
+ u8 port_offset;
+
+ port_offset = phydev->mdio.addr & 3;
+ val = __phy_package_read_mmd(phydev, 0, MDIO_MMD_VEND1,
+ RTL8224_VND1_MDI_PAIR_SWAP);
+ if (swap)
+ val |= (1 << port_offset);
+ else
+ val &= ~(1 << port_offset);
+
+ __phy_package_write_mmd(phydev, 0, MDIO_MMD_VEND1,
+ RTL8224_VND1_MDI_PAIR_SWAP, val);
+}
+
+static int rtl8224_config_init(struct phy_device *phydev)
+{
+ struct rtl8224_priv *priv = phydev->priv;
+
+ if (priv->pair_swap != PAIR_SWAP_KEEP)
+ rtl8224_mdi_pair_swap(phydev, priv->pair_swap == PAIR_SWAP_ON);
+
+ return 0;
+}
+
+static enum pair_swap_state rtlgen_pair_swap_state_get(const struct device_node *np)
+{
+ const char *state = NULL;
+
+ if (!of_property_read_string(np, "realtek,mdi-pair-swap", &state)) {
+ if (!strcmp(state, "off"))
+ return PAIR_SWAP_OFF;
+ if (!strcmp(state, "on"))
+ return PAIR_SWAP_ON;
+ }
+
+ return PAIR_SWAP_KEEP;
+}
+
+static int rtl8224_probe(struct phy_device *phydev)
+{
+ struct device *dev = &phydev->mdio.dev;
+ struct rtl8224_priv *priv;
+
+ priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+ if (!priv)
+ return -ENOMEM;
+ phydev->priv = priv;
+
+ priv->pair_swap = rtlgen_pair_swap_state_get(dev->of_node);
+
+ /* Device has 4 ports */
+ devm_phy_package_join(dev, phydev, phydev->mdio.addr & (~3), 0);
+
+ return 0;
+}
+
static bool rtlgen_supports_2_5gbps(struct phy_device *phydev)
{
int val;
@@ -2212,6 +2284,8 @@ static struct phy_driver realtek_drvs[] = {
PHY_ID_MATCH_EXACT(0x001ccad0),
.name = "RTL8224 2.5Gbps PHY",
.flags = PHY_POLL_CABLE_TEST,
+ .probe = rtl8224_probe,
+ .config_init = rtl8224_config_init,
.get_features = rtl822x_c45_get_features,
.config_aneg = rtl822x_c45_config_aneg,
.read_status = rtl822x_c45_read_status,
base-commit: 983d014aafb14ee5e4915465bf8948e8f3a723b5
--
2.47.3
Powered by blists - more mailing lists