public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Damien Dejean <dam.dejean@gmail.com>
To: netdev@vger.kernel.org
Cc: Damien Dejean <dam.dejean@gmail.com>
Subject: [PATCH 2/2] net: phy: realtek: add RTL8224 polarity swap support
Date: Fri, 16 Jan 2026 18:39:20 +0100	[thread overview]
Message-ID: <20260116173920.371523-2-dam.dejean@gmail.com> (raw)
In-Reply-To: <20260116173920.371523-1-dam.dejean@gmail.com>

The RTL8224 has a register to configure the polarity of every pair of
each port. It provides device designers more flexbility when wiring the
chip.

Unfortunately, the register is left in an unknown state after a reset.
Thus on devices where the bootloader don't initialize it, the driver has
to do it to detect and use a link.

The MDI polarity swap can be set in the device tree using the property
realtek,mdi-polarity-swap. The u32 value is a bitfield where bit[0..3]
control the polarity of pairs A...D.

Signed-off-by: Damien Dejean <dam.dejean@gmail.com>
---
 .../bindings/net/realtek,rtl82xx.yaml         |  7 +++++
 drivers/net/phy/realtek/realtek_main.c        | 29 +++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/Documentation/devicetree/bindings/net/realtek,rtl82xx.yaml b/Documentation/devicetree/bindings/net/realtek,rtl82xx.yaml
index 2d04d90f8b97..4abcc5cfaf5f 100644
--- a/Documentation/devicetree/bindings/net/realtek,rtl82xx.yaml
+++ b/Documentation/devicetree/bindings/net/realtek,rtl82xx.yaml
@@ -67,6 +67,13 @@ properties:
       - off
     default: keep
 
+  realtek,mdi-polarity-swap:
+    description:
+      A bitmap to describe pair polarity swap. Bit 0 to swap polarity of pair A,
+      bit 1 to swap polarity of pair B, bit 2 to swap polarity of pair C and bit
+      3 to swap polarity of pair D.
+    $ref: /schemas/types.yaml#/definitions/uint32
+
 unevaluatedProperties: false
 
 allOf:
diff --git a/drivers/net/phy/realtek/realtek_main.c b/drivers/net/phy/realtek/realtek_main.c
index e01bfad37cf5..3bcee2f40a44 100644
--- a/drivers/net/phy/realtek/realtek_main.c
+++ b/drivers/net/phy/realtek/realtek_main.c
@@ -164,6 +164,7 @@
 #define RTL8224_SRAM_RTCT_LEN(pair)		(0x8028 + (pair) * 4)
 
 #define RTL8224_VND1_MDI_PAIR_SWAP		0xa90
+#define RTL8224_VND1_MDI_POLARITY_SWAP		0xa94
 
 #define RTL8366RB_POWER_SAVE			0x15
 #define RTL8366RB_POWER_SAVE_ON			BIT(12)
@@ -219,6 +220,7 @@ enum pair_swap_state {
 
 struct rtl8224_priv {
 	enum pair_swap_state pair_swap;
+	int polarity_swap;
 };
 
 static int rtl821x_read_page(struct phy_device *phydev)
@@ -1713,12 +1715,28 @@ static void rtl8224_mdi_pair_swap(struct phy_device *phydev, bool swap)
 				RTL8224_VND1_MDI_PAIR_SWAP, val);
 }
 
+static void rtl8224_mdi_polarity_swap(struct phy_device *phydev, u32 swap)
+{
+	u32 val;
+	u8 offset;
+
+	offset = (phydev->mdio.addr & 3) * 4;
+	val = __phy_package_read_mmd(phydev, 0, MDIO_MMD_VEND1,
+				     RTL8224_VND1_MDI_POLARITY_SWAP);
+	val &= ~(0xf << offset);
+	val |= (swap & 0xf) << offset;
+	__phy_package_write_mmd(phydev, 0, MDIO_MMD_VEND1,
+				RTL8224_VND1_MDI_POLARITY_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);
+	if (priv->polarity_swap >= 0)
+		rtl8224_mdi_polarity_swap(phydev, priv->polarity_swap);
 
 	return 0;
 }
@@ -1737,6 +1755,16 @@ static enum pair_swap_state rtlgen_pair_swap_state_get(const struct device_node
 	return PAIR_SWAP_KEEP;
 }
 
+static int rtlgen_polarity_swap_get(const struct device_node *np)
+{
+	u32 polarity;
+
+	if (!of_property_read_u32(np, "realtek,mdi-polarity-swap", &polarity))
+		return polarity;
+
+	return -1;
+}
+
 static int rtl8224_probe(struct phy_device *phydev)
 {
 	struct device *dev = &phydev->mdio.dev;
@@ -1748,6 +1776,7 @@ static int rtl8224_probe(struct phy_device *phydev)
 	phydev->priv = priv;
 
 	priv->pair_swap = rtlgen_pair_swap_state_get(dev->of_node);
+	priv->polarity_swap = rtlgen_polarity_swap_get(dev->of_node);
 
 	/* Device has 4 ports */
 	devm_phy_package_join(dev, phydev, phydev->mdio.addr & (~3), 0);
-- 
2.47.3


  reply	other threads:[~2026-01-16 17:39 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-16 17:39 [PATCH 1/2] net: phy: realtek: add RTL8224 pair swap support Damien Dejean
2026-01-16 17:39 ` Damien Dejean [this message]
2026-01-17 10:47   ` [PATCH 2/2] net: phy: realtek: add RTL8224 polarity " Krzysztof Kozlowski
2026-01-16 18:10 ` [PATCH 1/2] net: phy: realtek: add RTL8224 pair " Andrew Lunn
2026-01-19 17:54   ` Damien Dejean
2026-01-17 10:47 ` Krzysztof Kozlowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260116173920.371523-2-dam.dejean@gmail.com \
    --to=dam.dejean@gmail.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox