Netdev List
 help / color / mirror / Atom feed
From: ck <mjkchengke@163.com>
To: Frank.Sae@motor-comm.com, netdev@vger.kernel.org
Cc: andrew@lunn.ch, hkallweit1@gmail.com, linux@armlinux.org.uk,
	davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
	pabeni@redhat.com, mjkchengke@163.com
Subject: [PATCH] net: phy: motorcomm: add downshift tunable support for YT8531/YT8531S
Date: Wed, 25 Nov 2026 16:55:09 +0800	[thread overview]
Message-ID: <20261125085509.1844883-1-mjkchengke@163.com> (raw)

The YT8531 and YT8531S support Ethernet Line Speed Auto Downgrade:
when autonegotiation fails to establish a link after a configurable
number of attempts, the PHY downgrades to a lower speed and retries.
The number of attempts before downgrading is configured through the
Speed Auto Downgrade Control Register (0x14), bits 4:2, and the
feature is enabled through bit 5 of the same register.

Expose this as the ETHTOOL_PHY_DOWNSHIFT tunable, following the
semantics used by other drivers: the tunable value is the number of
autonegotiation attempts before downgrading (range 2-9), 0 disables
the feature, and DOWNSHIFT_DEV_DEFAULT_COUNT restores the power-on
default of 5 attempts.

The retry-limit field stores (attempts - 2) and only takes effect
after a software reset of the UTP register space. genphy_soft_reset()
resets the BMCR of the currently selected register space only, and
the YT8531S powers up with the fiber space selected, so use the
existing yt8521_modify_bmcr_paged() helper to reset the UTP space
explicitly.

Hardware-validated on a Phytium D2000 board with a YT8531S: setting
8 and 2 attempts, restoring the default, and disabling all commit
correctly to register 0x14; out-of-range values are rejected with
-ERANGE.

Signed-off-by: chengke <mjkchengke@163.com>
---
 drivers/net/phy/motorcomm.c | 97 +++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)

diff --git a/drivers/net/phy/motorcomm.c b/drivers/net/phy/motorcomm.c
index a87e83fdf5..08bf8441fb 100644
--- a/drivers/net/phy/motorcomm.c
+++ b/drivers/net/phy/motorcomm.c
@@ -97,6 +97,9 @@
  */
 #define YTPHY_SADCR_SPEED_RETRY_LIMIT		(0x3 << 2)
 
+#define YTPHY_SADCR_SPEED_RETRY_LIMIT_MASK		GENMASK(4, 2)
+#define YTPHY_SADCR_DOWNSHIFT_ATTEMPTS_MIN		2
+#define YTPHY_SADCR_DOWNSHIFT_ATTEMPTS_MAX		9
 /* Rx Error Counter Register */
 #define YTPHY_RX_ERROR_COUNTER_REG		0x15
 
@@ -1205,6 +1208,96 @@ static int yt8521_probe(struct phy_device *phydev)
 					  val);
 }
 
+static int yt8521_modify_bmcr_paged(struct phy_device *phydev, int page,
+				    u16 mask, u16 set);
+
+static int yt8531_get_downshift(struct phy_device *phydev, u8 *data)
+{
+	int val, cnt, enable;
+
+	val = phy_read_paged(phydev, YT8521_RSSR_UTP_SPACE,
+			     YTPHY_SPEED_AUTO_DOWNGRADE_CONTROL_REG);
+	if (val < 0)
+		return val;
+
+	enable = FIELD_GET(YTPHY_SADCR_SPEED_DOWNGRADE_EN, val);
+	/* The retry limit field stores (attempts - 2): the default 0x3
+	 * means 5 attempts before downgrading.
+	 */
+	cnt = FIELD_GET(YTPHY_SADCR_SPEED_RETRY_LIMIT_MASK, val) + 2;
+
+	*data = enable ? cnt : DOWNSHIFT_DEV_DISABLE;
+
+	return 0;
+}
+
+static int yt8531_set_downshift(struct phy_device *phydev, u8 cnt)
+{
+	u16 mask, set;
+	int err;
+
+	if (cnt == DOWNSHIFT_DEV_DEFAULT_COUNT) {
+		/* Restore the power-on default: enabled, 5 attempts */
+		mask = YTPHY_SADCR_SPEED_DOWNGRADE_EN |
+		       YTPHY_SADCR_SPEED_RETRY_LIMIT_MASK;
+		set = YTPHY_SADCR_SPEED_DOWNGRADE_EN |
+		      FIELD_PREP(YTPHY_SADCR_SPEED_RETRY_LIMIT_MASK, 3);
+	} else if (!cnt) {
+		mask = YTPHY_SADCR_SPEED_DOWNGRADE_EN;
+		set = 0;
+	} else {
+		/* The tunable value is the number of autoneg attempts
+		 * before downgrading, in range [2, 9]. The register
+		 * field stores (cnt - 2).
+		 */
+		if (cnt < YTPHY_SADCR_DOWNSHIFT_ATTEMPTS_MIN ||
+		    cnt > YTPHY_SADCR_DOWNSHIFT_ATTEMPTS_MAX)
+			return -ERANGE;
+
+		mask = YTPHY_SADCR_SPEED_DOWNGRADE_EN |
+		       YTPHY_SADCR_SPEED_RETRY_LIMIT_MASK;
+		set = YTPHY_SADCR_SPEED_DOWNGRADE_EN |
+		      FIELD_PREP(YTPHY_SADCR_SPEED_RETRY_LIMIT_MASK, cnt - 2);
+	}
+
+	/* The SADCR change is only committed by a UTP-space software reset.
+	 * genphy_soft_reset() resets the BMCR of the currently selected
+	 * register space only, and YT8531S powers up with the fiber space
+	 * selected, so reset the UTP space explicitly. Both helpers manage
+	 * their own paged access window.
+	 */
+	err = phy_modify_paged(phydev, YT8521_RSSR_UTP_SPACE,
+			       YTPHY_SPEED_AUTO_DOWNGRADE_CONTROL_REG,
+			       mask, set);
+	if (err < 0)
+		return err;
+
+	return yt8521_modify_bmcr_paged(phydev, YT8521_RSSR_UTP_SPACE,
+					0, BMCR_RESET);
+}
+
+static int yt8531_get_tunable(struct phy_device *phydev,
+			      struct ethtool_tunable *tuna, void *data)
+{
+	switch (tuna->id) {
+	case ETHTOOL_PHY_DOWNSHIFT:
+		return yt8531_get_downshift(phydev, data);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static int yt8531_set_tunable(struct phy_device *phydev,
+			      struct ethtool_tunable *tuna,
+			      const void *data)
+{
+	if (tuna->id != ETHTOOL_PHY_DOWNSHIFT ||
+	    tuna->len != sizeof(u8))
+		return -EOPNOTSUPP;
+
+	return yt8531_set_downshift(phydev, *(const u8 *)data);
+}
+
 static int yt8531_probe(struct phy_device *phydev)
 {
 	struct device *dev = &phydev->mdio.dev;
@@ -3280,6 +3373,8 @@ static struct phy_driver motorcomm_phy_drvs[] = {
 	},
 	{
 		PHY_ID_MATCH_EXACT(PHY_ID_YT8531),
+		.get_tunable	= yt8531_get_tunable,
+		.set_tunable	= yt8531_set_tunable,
 		.name		= "YT8531 Gigabit Ethernet",
 		.probe		= yt8531_probe,
 		.config_init	= yt8531_config_init,
@@ -3294,6 +3389,8 @@ static struct phy_driver motorcomm_phy_drvs[] = {
 	},
 	{
 		PHY_ID_MATCH_EXACT(PHY_ID_YT8531S),
+		.get_tunable	= yt8531_get_tunable,
+		.set_tunable	= yt8531_set_tunable,
 		.name		= "YT8531S Gigabit Ethernet",
 		.get_features	= yt8521_get_features,
 		.probe		= yt8521_probe,
-- 
2.43.0


             reply	other threads:[~2026-11-25  8:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  8:55 ck [this message]
2026-11-25  9:16 ` [PATCH v2 1/2] net: phy: motorcomm: return -ETIMEDOUT on BMCR reset timeout ck
2026-11-25  9:16   ` [PATCH v2 2/2] net: phy: motorcomm: add downshift tunable support for YT8531S ck
2026-11-25  9:23   ` [PATCH v2 1/2] net: phy: motorcomm: return -ETIMEDOUT on BMCR reset timeout netdev-bot+sinfo

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=20261125085509.1844883-1-mjkchengke@163.com \
    --to=mjkchengke@163.com \
    --cc=Frank.Sae@motor-comm.com \
    --cc=andrew@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=hkallweit1@gmail.com \
    --cc=kuba@kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /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