The YT8531S supports 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 for the YT8531S, 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. This also relies on the previous fix, which makes the helper report a timeout instead of a stale positive BMCR value. 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 --- Changes in v2: - Attach the tunable ops to the YT8531S entry only: YT8531 has no .read_page/.write_page, so paged helpers would WARN_ONCE and fail with -EOPNOTSUPP on it (found in self-review). - Drop the forward declaration by moving the new functions below yt8521_modify_bmcr_paged(). - Reuse the existing YTPHY_SADCR_SPEED_RETRY_LIMIT macro for the default value instead of a magic number. - Use kernel-style multi-line comments. drivers/net/phy/motorcomm.c | 96 ++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/drivers/net/phy/motorcomm.c b/drivers/net/phy/motorcomm.c index ba3421ca4d..8de87897bc 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 @@ -1595,13 +1598,102 @@ static int yt8521_modify_bmcr_paged(struct phy_device *phydev, int page, if (!(ret & BMCR_RESET)) return phy_restore_page(phydev, old_page, 0); } - ret = -ETIMEDOUT; } err_restore_page: return phy_restore_page(phydev, old_page, ret); } +static int yt8531s_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 yt8531s_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 | + YTPHY_SADCR_SPEED_RETRY_LIMIT; + } 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 yt8531s_get_tunable(struct phy_device *phydev, + struct ethtool_tunable *tuna, void *data) +{ + switch (tuna->id) { + case ETHTOOL_PHY_DOWNSHIFT: + return yt8531s_get_downshift(phydev, data); + default: + return -EOPNOTSUPP; + } +} + +static int yt8531s_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 yt8531s_set_downshift(phydev, *(const u8 *)data); +} + /** * yt8521_modify_utp_fiber_bmcr - bits modify a PHY's BMCR register * @phydev: the phy_device struct @@ -3295,6 +3387,8 @@ static struct phy_driver motorcomm_phy_drvs[] = { }, { PHY_ID_MATCH_EXACT(PHY_ID_YT8531S), + .get_tunable = yt8531s_get_tunable, + .set_tunable = yt8531s_set_tunable, .name = "YT8531S Gigabit Ethernet", .get_features = yt8521_get_features, .probe = yt8521_probe, -- 2.43.0