diff mbox series

[net-next,9/9] net: phy: vitesse: implement downshift in vsc73xx phys

Message ID 20240729210615.279952-10-paweldembicki@gmail.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series net: dsa: vsc73xx: fix MDIO bus access and PHY operations | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 42 this patch: 42
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 7 of 7 maintainers
netdev/build_clang success Errors and warnings before: 43 this patch: 43
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 43 this patch: 43
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 150 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Pawel Dembicki July 29, 2024, 9:06 p.m. UTC
This commit implements downshift feature in vsc73xx family phys.

By default downshift was enabled with maximum tries.

Signed-off-by: Pawel Dembicki <paweldembicki@gmail.com>
---
 drivers/net/phy/vitesse.c | 100 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 100 insertions(+)

Comments

Russell King (Oracle) July 29, 2024, 11:39 p.m. UTC | #1
On Mon, Jul 29, 2024 at 11:06:15PM +0200, Pawel Dembicki wrote:
> +static int vsc73xx_get_downshift(struct phy_device *phydev, u8 *data)
> +{
> +	int page, val, cnt, enable;
> +
> +	page = phy_select_page(phydev, MII_VSC73XX_EXT_PAGE_1E);
> +	if (page < 0)
> +		return page;
> +
> +	val = __phy_read(phydev, MII_VSC73XX_PHY_CTRL_EXT3);
> +	if (val < 0)
> +		goto restore_page;
> +
> +	enable = FIELD_GET(MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN, val);
> +	cnt = FIELD_GET(MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_CNT, val) + 2;
> +
> +	*data = enable ? cnt : DOWNSHIFT_DEV_DISABLE;
> +
> +restore_page:
> +	phy_restore_page(phydev, page, val);
> +	return 0;

If you're going to use phy_select_page()..phy_restore_page(), please
read the documentation. If the __phy_read() fails, then you're
returning zero from this function, but leaving *data uninitialised.
Surely not what you want.

In any case, this complexity is unnecessary.

static int vsc73xx_get_downshift(struct phy_device *phydev, u8 *data)
{
	int val, enable, cnt;

	val = phy_read_paged(phydev, MII_VSC73XX_EXT_PAGE_1E,
			     MII_VSC73XX_PHY_CTRL_EXT3);
	if (val < 0)
		return val;

	enable = FIELD_GET(MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN, val);
	cnt = FIELD_GET(MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_CNT, val) + 2;

	*data = enable ? cnt : DOWNSHIFT_DEV_DISABLE;

	return 0;
}

is all that it needs.

> +}
> +
> +static int vsc73xx_set_downshift(struct phy_device *phydev, u8 cnt)
> +{
> +	int page, ret, val;
> +
> +	if (cnt > MII_VSC73XX_DOWNSHIFT_MAX)
> +		return -E2BIG;
> +	else if (cnt == MII_VSC73XX_DOWNSHIFT_INVAL)
> +		return -EINVAL;
> +
> +	page = phy_select_page(phydev, MII_VSC73XX_EXT_PAGE_1E);
> +	if (page < 0)
> +		return page;
> +
> +	if (!cnt) {
> +		ret = __phy_clear_bits(phydev, MII_VSC73XX_PHY_CTRL_EXT3,
> +				       MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN);
> +	} else {
> +		val = MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN |
> +		      FIELD_PREP(MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_CNT,
> +				 cnt - 2);
> +
> +		ret = __phy_modify(phydev, MII_VSC73XX_PHY_CTRL_EXT3,
> +				   MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN |
> +				   MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_CNT,
> +				   val);
> +	}
> +
> +	ret = phy_restore_page(phydev, page, ret);
> +	if (ret < 0)
> +		return ret;

This is also needlessly over-complex.

	u16 mask, val;

	...

	mask = MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN;

	if (!cnt) {
		val = 0;
	} else {
		mask |= MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_CNT;
		val = MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN |
		      FIELD_PREP(MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_CNT,
				 cnt - 2);
	}

	ret = phy_modify_paged(phydev, MII_VSC73XX_EXT_PAGE_1E,
			       MII_VSC73XX_PHY_CTRL_EXT3, mask, val);
	if (ret < 0)
		return ret;

Would be equivalent to your code above, only simpler.
diff mbox series

Patch

diff --git a/drivers/net/phy/vitesse.c b/drivers/net/phy/vitesse.c
index 19b7bf189be5..9228c6652627 100644
--- a/drivers/net/phy/vitesse.c
+++ b/drivers/net/phy/vitesse.c
@@ -10,8 +10,10 @@ 
 #include <linux/mii.h>
 #include <linux/ethtool.h>
 #include <linux/phy.h>
+#include <linux/bitfield.h>
 
 /* Vitesse Extended Page Magic Register(s) */
+#define MII_VSC73XX_EXT_PAGE_1E		0x01
 #define MII_VSC82X4_EXT_PAGE_16E	0x10
 #define MII_VSC82X4_EXT_PAGE_17E	0x11
 #define MII_VSC82X4_EXT_PAGE_18E	0x12
@@ -60,6 +62,15 @@ 
 /* Vitesse Extended Page Access Register */
 #define MII_VSC82X4_EXT_PAGE_ACCESS	0x1f
 
+/* Vitesse VSC73XX Extended Control Register */
+#define MII_VSC73XX_PHY_CTRL_EXT3		0x14
+
+#define MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN	BIT(4)
+#define MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_CNT	GENMASK(3, 2)
+#define MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_STA	BIT(1)
+#define MII_VSC73XX_DOWNSHIFT_MAX		5
+#define MII_VSC73XX_DOWNSHIFT_INVAL		1
+
 /* VSC73XX PHY_BYPASS_CTRL register*/
 #define MII_VSC73XX_PHY_BYPASS_CTRL		MII_DCOUNTER
 #define MII_PBC_FORCED_SPEED_AUTO_MDIX_DIS	BIT(7)
@@ -133,6 +144,84 @@  static int vsc73xx_write_page(struct phy_device *phydev, int page)
 	return __phy_write(phydev, VSC73XX_EXT_PAGE_ACCESS, page);
 }
 
+static int vsc73xx_get_downshift(struct phy_device *phydev, u8 *data)
+{
+	int page, val, cnt, enable;
+
+	page = phy_select_page(phydev, MII_VSC73XX_EXT_PAGE_1E);
+	if (page < 0)
+		return page;
+
+	val = __phy_read(phydev, MII_VSC73XX_PHY_CTRL_EXT3);
+	if (val < 0)
+		goto restore_page;
+
+	enable = FIELD_GET(MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN, val);
+	cnt = FIELD_GET(MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_CNT, val) + 2;
+
+	*data = enable ? cnt : DOWNSHIFT_DEV_DISABLE;
+
+restore_page:
+	phy_restore_page(phydev, page, val);
+	return 0;
+}
+
+static int vsc73xx_set_downshift(struct phy_device *phydev, u8 cnt)
+{
+	int page, ret, val;
+
+	if (cnt > MII_VSC73XX_DOWNSHIFT_MAX)
+		return -E2BIG;
+	else if (cnt == MII_VSC73XX_DOWNSHIFT_INVAL)
+		return -EINVAL;
+
+	page = phy_select_page(phydev, MII_VSC73XX_EXT_PAGE_1E);
+	if (page < 0)
+		return page;
+
+	if (!cnt) {
+		ret = __phy_clear_bits(phydev, MII_VSC73XX_PHY_CTRL_EXT3,
+				       MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN);
+	} else {
+		val = MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN |
+		      FIELD_PREP(MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_CNT,
+				 cnt - 2);
+
+		ret = __phy_modify(phydev, MII_VSC73XX_PHY_CTRL_EXT3,
+				   MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN |
+				   MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_CNT,
+				   val);
+	}
+
+	ret = phy_restore_page(phydev, page, ret);
+	if (ret < 0)
+		return ret;
+
+	return genphy_soft_reset(phydev);
+}
+
+static int vsc73xx_get_tunable(struct phy_device *phydev,
+			       struct ethtool_tunable *tuna, void *data)
+{
+	switch (tuna->id) {
+	case ETHTOOL_PHY_DOWNSHIFT:
+		return vsc73xx_get_downshift(phydev, data);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static int vsc73xx_set_tunable(struct phy_device *phydev,
+			       struct ethtool_tunable *tuna, const void *data)
+{
+	switch (tuna->id) {
+	case ETHTOOL_PHY_DOWNSHIFT:
+		return vsc73xx_set_downshift(phydev, *(const u8 *)data);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
 static void vsc73xx_config_init(struct phy_device *phydev)
 {
 	/* Receiver init */
@@ -142,6 +231,9 @@  static void vsc73xx_config_init(struct phy_device *phydev)
 
 	/* Config LEDs 0x61 */
 	phy_modify(phydev, MII_TPISTATUS, 0xff00, 0x0061);
+
+	/* Enable downshift by default */
+	vsc73xx_set_downshift(phydev, MII_VSC73XX_DOWNSHIFT_MAX);
 }
 
 static int vsc738x_config_init(struct phy_device *phydev)
@@ -460,6 +552,8 @@  static struct phy_driver vsc82xx_driver[] = {
 	.config_aneg    = vsc73xx_config_aneg,
 	.read_page      = vsc73xx_read_page,
 	.write_page     = vsc73xx_write_page,
+	.get_tunable    = vsc73xx_get_tunable,
+	.set_tunable    = vsc73xx_set_tunable,
 }, {
 	.phy_id         = PHY_ID_VSC7388,
 	.name           = "Vitesse VSC7388",
@@ -469,6 +563,8 @@  static struct phy_driver vsc82xx_driver[] = {
 	.config_aneg    = vsc73xx_config_aneg,
 	.read_page      = vsc73xx_read_page,
 	.write_page     = vsc73xx_write_page,
+	.get_tunable    = vsc73xx_get_tunable,
+	.set_tunable    = vsc73xx_set_tunable,
 }, {
 	.phy_id         = PHY_ID_VSC7395,
 	.name           = "Vitesse VSC7395",
@@ -478,6 +574,8 @@  static struct phy_driver vsc82xx_driver[] = {
 	.config_aneg    = vsc73xx_config_aneg,
 	.read_page      = vsc73xx_read_page,
 	.write_page     = vsc73xx_write_page,
+	.get_tunable    = vsc73xx_get_tunable,
+	.set_tunable    = vsc73xx_set_tunable,
 }, {
 	.phy_id         = PHY_ID_VSC7398,
 	.name           = "Vitesse VSC7398",
@@ -487,6 +585,8 @@  static struct phy_driver vsc82xx_driver[] = {
 	.config_aneg    = vsc73xx_config_aneg,
 	.read_page      = vsc73xx_read_page,
 	.write_page     = vsc73xx_write_page,
+	.get_tunable    = vsc73xx_get_tunable,
+	.set_tunable    = vsc73xx_set_tunable,
 }, {
 	.phy_id         = PHY_ID_VSC8662,
 	.name           = "Vitesse VSC8662",