Skip to contentSkip to conversation

net: phy: Add driver for Motorcomm yt8521 gigabit ethernet phy

PATCH Superseded Frank 2 participants 2 messages Aug 31, 2022

Subsystems (4)

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Frank

author maintainer
#0 lore raw

PATCH v5 1/1 net: phy: Add driver for Motorcomm yt8521 gigabit ethernet phy

Add a driver for the motorcomm yt8521 gigabit ethernet phy. We have verified the driver on StarFive VisionFive development board, which is developed by Shanghai StarFive Technology Co., Ltd.. On the board, yt8521 gigabit ethernet phy works in utp mode, RGMII interface, supports 1000M/100M/10M speeds, and wol(magic package).

---
patch v5:
Hi Jakub,Andrew,Russell

Quoted 21 lines

The user could of changed the pause settings, which are going to be ignored here. Also, you should not assume the MAC can actually do asymmetric pause, not all can. phydev->advertising will be set to only include what the MAC can actually do.

The whole concept of having two line sides connected to one MAC and seeing which gets link first is unsupported in Linux. In theory, you want to be able to configure each line side differently. Maybe you want autoneg on copper, but fixed on fibre, asymmetric pause with fibre, but symmetric pause on copper, etc. Since there is only one instance of phydev here, you don't have anywhere to store two sets of configuration, nor any sort of kAPI to deal with two phydev structures etc. So the user experience is not so great.

With the Marvell Switches which also have this capability, i actually ignore it, use the phy-mode it decide which should be used, either copper or fibre, and leave the other powered off so it can never get link. There is at least one Marvell PHY which does however support first up wins, so this behaviour is not new. I just don't recommend it.

Quoted 3 lines

And it gets even more interesting when the SFP is actually copper. But not supported here.
since the integration with phylink is missing in this driver, that is

1. we changed the codes for getting/advertising of pause and other capabilities, the basic mechnism is that the capabilities and advertising will changed based on current linkuped media, please review.
2. as to the fibre/copper two lines support, this phy driver will handle to ensure linkup only on one media and it is transparent to MAC and uplayer. in practice, this do help users use the feature of the phy device, especially for the combo mode design in which customer can switch between fibre and copper automatically.

patch v4.4:
Hi Jakub
Fix "warning: variable 'changed'"

patch v4.3:
Hi Jakub
Kernel 6.0-rc1 is cut on 2022-08-14, we update new patch for Kernel 6.0-rc1.

patch v4.2:
fix "=0D" and "=20" which in patch v4.1.

patch v4.1:
Hi Andrew
Thanks very much and based on your comments we modified the patch v4.1 as below.

Quoted 4 lines

You have interrupt bits defined, you enable the WoL interrupt, but you don't have an interrupt handler. PHY interrupts are generally level interrupts, so on WoL, don't you end up with an interrupt storm, because you don't have anything clearing the WoL interrupt?

in the driver, only the interrupt for WOL is used according to uplayer WOL configuration via .set_wol. If WOL is enalbed, then the WOL interrupt is enabled accordingly; you know, for a system support WOL, the WOL interrupt will be handled by user's WOL circuit, and it will not cause interrupt storm to CPU core system.

for WOL interrupt, by default, we configure to pulse trigger mode, that is a pulse from High to low change, and the low level will hold for 672ms. user's WOL circuit will detected such volt level change as a WOL event trigger.

patch v4:
Hi Andrew,Jakub
Thanks very much and based on your comments we modified the patch v4 as below.

We evaluated the Marvell 10G driver and at803x you suggested. The 2 drivers implement SFP module attach/detach functions and we think these functions do not help yt8521 to do UTP/Fiber register space arbitration which you may concern in previous patch.

Yt8521 can detect utp/fiber media link status automatically. For the case of both media are connected, driver arbitrates the priority of the media (by default, driver takes fiber as higher priority) and report the media status to up layer(MAC).

patch v3:
Hi Andrew
Thanks and based on your comments we modified the patch as below.

Quoted 5 lines

It is generally not that simple. Fibre, you probably want 1000BaseX, unless the fibre module is actually copper, and then you want SGMII. So you need something to talk to the fibre module and ask it what it is. That something is phylink. Phylink does not support both copper and fibre at the same time for one MAC.

yes, you said it and for MAC, it does not support copper and Fiber at same time. but from Physical Layer, you know, sometimes both Copper and Fiber cables are connected. in this case, Phy driver should do arbitration and report to MAC which media should be used and Link-up.
This is the reason that the driver has a "polling mode", and by default, also this driver takes fiber as first priority which matches phy chip default behavior.

patch v2:
Hi Andrew, Russell King, Peter,
Thanks and based on your comments we modified the patch as below.

Quoted 2 lines

So there's only two possible pages that can be used in the extended register space?

Yes,there is only two register space (utp and fiber).

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+/* Extended Register's Data Register */
+#define YTPHY_PAGE_DATA                                0x1F

These are defined exactly the same way as below. Please reuse code where possible.

Yes, code will be reuse, but "YT8511_PAGE" need to be rename like "YTPHY_PAGE_DATA",as it is common register for yt phys.

patch v1:
Add a driver for the motorcomm yt8521 gigabit ethernet phy. We have verified the driver on StarFive VisionFive development board, which is developed by Shanghai StarFive Technology Co., Ltd.. On the board, yt8521 gigabit ethernet phy works in utp mode, RGMII interface, supports 1000M/100M/10M speeds, and wol(magic package).

Thanks and BR,
Frank

 MAINTAINERS                 |    1 +
 drivers/net/phy/Kconfig     |    2 +-
 drivers/net/phy/motorcomm.c | 1306 ++++++++++++++++++++++++++++++++++-
 3 files changed, 1306 insertions(+), 3 deletions(-)
MAINTAINERS +1 -0
index 8a5012b..994919b 100644
@@ -13780,6 +13780,7 @@ F:	include/uapi/linux/meye.h
 
 MOTORCOMM PHY DRIVER
 M:	Peter Geis <[email protected]>
+M:	Frank <[email protected]>
 L:	[email protected]
 S:	Maintained
 F:	drivers/net/phy/motorcomm.c
drivers/net/phy/Kconfig +1 -1
index c57a026..040c8bf 100644
@@ -260,7 +260,7 @@ config MOTORCOMM_PHY
 	tristate "Motorcomm PHYs"
 	help
 	  Enables support for Motorcomm network PHYs.
-	  Currently supports the YT8511 gigabit PHY.
+	  Currently supports the YT8511, YT8521 Gigabit Ethernet PHYs.
 
 config NATIONAL_PHY
 	tristate "National Semiconductor PHYs"
drivers/net/phy/motorcomm.c +1304 -2

1,347 lines not on this page · raw message

Signature · 1 line
2.31.0.windows.1
maintainer
#1 lore raw
Quoted 8 lines

+ * yt8521_adjust_status() - update speed and duplex to phydev. when in fiber
+ * mode, adjust speed and duplex.
+ *
+ * @phydev: a pointer to a &struct phy_device
+ * @status: yt8521 status read from YTPHY_SPECIFIC_STATUS_REG
+ * @is_utp: false(yt8521 work in fiber mode) or true(yt8521 work in utp mode)
+ *
+ * NOTE: there is no 10M speed mode in fiber mode, so need adjust.

You appear to only advertise 1G speeds. Does 100M actually work, but you don't advertise it?

drivers/net/phy/motorcomm.c static int yt8511_config_init(struct phy_device *phydev) · line 672

Quoted 13 lines

+ *
+ * returns 0
+ */

drivers/net/phy/motorcomm.c +10 -0
+static int yt8521_adjust_status(struct phy_device *phydev, int status,
+				bool is_utp)
+{
+	int speed_mode, duplex;
+	int speed;
+
+	if (is_utp)
+		duplex = (status & YTPHY_SSR_DUPLEX) >> YTPHY_SSR_DUPLEX_OFFSET;
+	else
+		duplex = DUPLEX_FULL;

I could be reading yt8521_fiber_config_aneg() wrong, but you appear to be ADVERTISE_1000XHALF. Yet here you force full duplex?

drivers/net/phy/motorcomm.c static int yt8511_config_init(struct phy_device *phydev) · line 683

Quoted 11 lines
drivers/net/phy/motorcomm.c +11 -0
+
+	speed_mode = (status & YTPHY_SSR_SPEED_MODE_MASK) >>
+		     YTPHY_SSR_SPEED_MODE_OFFSET;
+
+	switch (speed_mode) {
+	case YTPHY_SSR_SPEED_10M:
+		if (is_utp)
+			speed = SPEED_10;
+		else
+			speed = SPEED_UNKNOWN;
+		break;

Does this mean the hardware can actually report 10M, even though it does not support it?

Quoted 13 lines
drivers/net/phy/motorcomm.c +13 -0
+static int yt8521_fiber_config_aneg(struct phy_device *phydev)
+{
+	int err, changed = 0;
+	u16 adv;
+
+	if (phydev->autoneg != AUTONEG_ENABLE)
+		return yt8521_fiber_setup_forced(phydev);
+
+	err =  ytphy_modify_ext_with_lock(phydev, YTPHY_MISC_CONFIG_REG,
+					  YTPHY_MCR_FIBER_SPEED_MASK,
+					  YTPHY_MCR_FIBER_1000BX);
+	if (err < 0)
+		return err;

drivers/net/phy/motorcomm.c static int yt8511_config_init(struct phy_device *phydev) · line 1135

Quoted 10 lines
drivers/net/phy/motorcomm.c +10 -0
+
+	/* enable Fiber auto sensing */
+	err =  ytphy_modify_ext_with_lock(phydev, YT8521_LINK_TIMER_CFG2_REG,
+					  0, YT8521_LTCR_EN_AUTOSEN);
+	if (err < 0)
+		return err;
+
+	/* Only allow advertising what this PHY supports */
+	linkmode_and(phydev->advertising, phydev->advertising,
+		     phydev->supported);

phylib does this step for you. phydev->advertising should always be a subset of phydev->supported.

drivers/net/phy/motorcomm.c static int yt8511_config_init(struct phy_device *phydev) · line 1141

Quoted 6 lines
drivers/net/phy/motorcomm.c +6 -0
+
+	/* some mac not support ETHTOOL_LINK_MODE_1000baseX_Full_BIT, so use
+	 * ETHTOOL_LINK_MODE_1000baseT_Full_BIT instead.
+	 */
+	adv = linkmode_adv_to_mii_adv_x(phydev->advertising,
+					ETHTOOL_LINK_MODE_1000baseT_Full_BIT);

I don't follow this. When the PHY driver is loaded, it should set phydev->supported with everything it supports. So that should include both ETHTOOL_LINK_MODE_1000baseX_Full_BIT and ETHTOOL_LINK_MODE_1000baseT_Full_BIT.

Generally, the MAC removes modes by calling phy_set_max_speed(). Or it ask for the 1/2 duplex modes to be removed. I don't think i've seen a MAC remove ETHTOOL_LINK_MODE_1000baseX_Full_BIT.

Please can you point to code which does this.

Quoted 39 lines
drivers/net/phy/motorcomm.c +39 -0
+
+	/* Setup fiber advertisement */
+	err = phy_modify_changed(phydev, MII_ADVERTISE,
+				 ADVERTISE_1000XHALF | ADVERTISE_1000XFULL |
+				 ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM,
+				 adv);
+	if (err < 0)
+		return err;
+
+	if (err > 0)
+		changed = 1;
+
+	return genphy_check_and_restart_aneg(phydev, changed);
+}
+
+/**
+ * yt8521_config_aneg_paged() - switch reg space then call genphy_config_aneg
+ * of one page
+ * @phydev: a pointer to a &struct phy_device
+ * @page: The reg page(YT8521_RSSR_FIBER_SPACE/YT8521_RSSR_UTP_SPACE) to
+ * operate.
+ *
+ * returns 0 or negative errno code
+ */
+static int yt8521_config_aneg_paged(struct phy_device *phydev, int page)
+{
+	struct yt8521_priv *priv = phydev->priv;
+	int old_page;
+	int ret = 0;
+
+	page &= YT8521_RSSR_SPACE_MASK;
+
+	old_page = phy_select_page(phydev, page);
+	if (old_page < 0)
+		goto err_restore_page;
+
+	/* If reg_page is YT8521_RSSR_TO_BE_ARBITRATED,
+	 * phydev->supported and phydev->advertising should be updated.
+	 */

drivers/net/phy/motorcomm.c static int yt8511_config_init(struct phy_device *phydev) · line 1190

Quoted 10 lines
drivers/net/phy/motorcomm.c +10 -0
+	if (priv->reg_page == YT8521_RSSR_TO_BE_ARBITRATED) {
+		if (page == YT8521_RSSR_FIBER_SPACE)
+			linkmode_and(phydev->supported, priv->fiber_supported,
+				     priv->utp_mac_supported);
+		else
+			linkmode_and(phydev->supported, priv->utp_supported,
+				     priv->utp_mac_supported);
+
+		phy_advertise_supported(phydev);
+	}

This is another place the two line side model falls apart. You should not be modifying phydev->supported, because how do you get back the bits your cleared when it swaps from one to the other? phylib has no concept of phydev->supported changing at run time. It is set when the PHY probes, and only changed when the MAC remove what it does not support.

You need yt8521_fiber_config_aneg() to take a copy of phydev->advertising, mask out what fibre does not support, and program the hardware based on that. You need yt8521_utp_config_aneg() to take a copy of phydev->advertising, mask out what copper does not support, and program the hardware based on that. phydev->supported and phydev->advertising should always be a superset of both copper and fibre, if both are available.

Maybe, in a flow up patch, you can actually look at phydev->advertising, see all the fibre modes have been removed, fibre will never get link, and so power the fibre off. Or all the copper modes have been removed, the copper will never get link, so turn it off.

Quoted 9 lines
drivers/net/phy/motorcomm.c +9 -0
+static int yt8521_get_features_paged(struct phy_device *phydev, int page)
+{
+	struct yt8521_priv *priv = phydev->priv;
+	int old_page;
+	int ret = 0;
+
+	page &= YT8521_RSSR_SPACE_MASK;
+
+	old_page = phy_select_page(phydev, page);

drivers/net/phy/motorcomm.c static int yt8511_config_init(struct phy_device *phydev) · line 1340

Quoted 10 lines
drivers/net/phy/motorcomm.c +10 -0
+	if (old_page < 0)
+		goto err_restore_page;
+
+	if (page == YT8521_RSSR_FIBER_SPACE) {
+		linkmode_zero(priv->fiber_supported);
+
+		/* some mac not support 1000baseX_Full, so use 1000baseT_Full
+		 * instead.
+		 */
+		phylink_set(priv->fiber_supported, 1000baseT_Full);

Ah. That partially explains the above. But you really are doing 1000baseX_Full here, so that is what you should set as supported.

Quoted 42 lines
drivers/net/phy/motorcomm.c +42 -0
+		phylink_set(priv->fiber_supported, Autoneg);
+		phylink_set(priv->fiber_supported, Pause);
+		phylink_set(priv->fiber_supported, Asym_Pause);
+		linkmode_copy(phydev->supported, priv->fiber_supported);
+	} else {
+		linkmode_zero(priv->utp_supported);
+
+		/* unlock mdio bus during genphy_read_abilities,
+		 * because it will operate this lock.
+		 */
+		phy_unlock_mdio_bus(phydev);
+		ret = genphy_read_abilities(phydev);
+		phy_lock_mdio_bus(phydev);
+
+		linkmode_copy(priv->utp_supported, phydev->supported);
+	}
+
+err_restore_page:
+	return phy_restore_page(phydev, old_page, ret);
+}
+
+/**
+ * yt8521_get_features - switch reg space then call yt8521_get_features_paged
+ * @phydev: target phy_device struct
+ *
+ * returns 0 or negative errno code
+ */
+static int yt8521_get_features(struct phy_device *phydev)
+{
+	struct yt8521_priv *priv = phydev->priv;
+	int ret;
+
+	if (priv->reg_page != YT8521_RSSR_TO_BE_ARBITRATED) {
+		ret = yt8521_get_features_paged(phydev, priv->reg_page);
+	} else {
+		linkmode_zero(priv->utp_mac_supported);
+
+		/* Get the features of utp and fiber, save them to
+		 * priv->utp_supported and priv->fiber_supported.
+		 * Aftert get both features, phydev->supported is the features
+		 * of utp.
+		 */

drivers/net/phy/motorcomm.c static int yt8511_config_init(struct phy_device *phydev) · line 1392

Quoted 10 lines
drivers/net/phy/motorcomm.c +10 -0
+		ret = yt8521_get_features_paged(phydev,
+						YT8521_RSSR_FIBER_SPACE);
+		if (ret < 0)
+			return ret;
+
+		ret = yt8521_get_features_paged(phydev,
+						YT8521_RSSR_UTP_SPACE);
+		if (ret < 0)
+			return ret;
+	}

If you are strapped to just UTP, set phydev->supported to just what the copper side supports. If you are strapped to fibre, set phydev->supported to what the fibre side supports. And if you are in 'whatever wins first mode', you need to set it to the superset.

Andrew