Skip to contentSkip to conversation

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

PATCH Superseded Frank 2 participants 2 messages Sep 14, 2022

Subsystems (4)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Frank

author maintainer
#0 lore raw

PATCH v6 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 v6:
Hi Andrew

Quoted 2 lines

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

yes, surely 100M fibre does work and since Fiber 100M advertise bit(ADVERTISE_100FULL) is conflict with ADVERTISE_1000XPSE_ASYM we can only advertise the latter.

Quoted 15 lines

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.

Quoted 5 lines

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.

We add combo_advertising which is used for case of YT8521 in combo mode, this means that yt8521 may work in utp or fiber mode which depends on which media is connected. Advertising bits will be updated before calling yt8521_fiber_config_aneg/genphy_config_aneg.

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 | 1308 ++++++++++++++++++++++++++++++++++-
 3 files changed, 1308 insertions(+), 3 deletions(-)
MAINTAINERS +1 -0
index 74036b51911d..c8df8773f2d3 100644
@@ -13785,6 +13785,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 c57a0262fb64..040c8bf6d05b 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 +1306 -2

1,349 lines not on this page · raw message

Signature · 1 line
2.31.0.windows.1
#1 lore raw
Quoted 5 lines · Frank
drivers/net/phy/motorcomm.c +5 -0
+static int ytphy_modify_ext(struct phy_device *phydev, u16 regnum, u16 mask,
+			    u16 set)
+{
+	u16 val;
+	int ret;

drivers/net/phy/motorcomm.c line 346

Quoted 10 lines
drivers/net/phy/motorcomm.c +10 -0
+
+	ret = ytphy_read_ext(phydev, regnum);
+	if (ret < 0)
+		return ret;
+
+	val = ret & 0xffff;
+	val &= ~mask;
+	val |= set;
+
+	return ytphy_write_ext(phydev, regnum, val);

Is there a reason not to implement this as:

	ret = __phy_write(phydev, YTPHY_PAGE_SELECT, regnum);
	if (ret < 0)
		return ret;

	return __phy_modify(phydev, YTPHY_PAGE_DATA, mask, set);

The considerations would be:
1) Does the page select register need to be written prior to every access?
2) Do we always have to write the value back even when the value hasn't changed by action of the mask and set parameters?

Quoted 52 lines
drivers/net/phy/motorcomm.c +52 -0
+/**
+ * yt8521_read_status() -  determines the negotiated speed and duplex
+ * @phydev: a pointer to a &struct phy_device
+ *
+ * returns 0 or negative errno code
+ */
+static int yt8521_read_status(struct phy_device *phydev)
+{
+	struct yt8521_priv *priv = phydev->priv;
+	int link_utp = 0;
+	int link_fiber;
+	int link;
+	int ret;
+
+	if (priv->reg_page != YT8521_RSSR_TO_BE_ARBITRATED) {
+		link = yt8521_read_status_paged(phydev, priv->reg_page);
+		if (link < 0)
+			return link;
+	} else {
+		/* when page is YT8521_RSSR_TO_BE_ARBITRATED, arbitration is
+		 * needed. by default, fiber is of higher priority.
+		 */
+
+		link_fiber = yt8521_read_status_paged(phydev,
+						      YT8521_RSSR_FIBER_SPACE);
+		if (link_fiber < 0)
+			return link_fiber;
+
+		if (!link_fiber) {
+			link_utp = yt8521_read_status_paged(phydev,
+							    YT8521_RSSR_UTP_SPACE);
+			if (link_utp < 0)
+				return link_utp;
+		}
+
+		link = link_utp || link_fiber;
+	}
+
+	if (link) {
+		if (phydev->link == 0) {
+			/* arbitrate reg space based on linkup media type. */
+			if (priv->polling_mode == YT8521_MODE_POLL &&
+			    priv->reg_page == YT8521_RSSR_TO_BE_ARBITRATED) {
+				if (link_fiber)
+					priv->reg_page =
+						YT8521_RSSR_FIBER_SPACE;
+				else
+					priv->reg_page = YT8521_RSSR_UTP_SPACE;
+
+				ret = ytphy_write_ext_with_lock(phydev,
+								YT8521_REG_SPACE_SELECT_REG,
+								priv->reg_page);

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

Quoted 10 lines
drivers/net/phy/motorcomm.c +10 -0
+				if (ret < 0)
+					return ret;
+
+				phydev->port = link_fiber ? PORT_FIBRE : PORT_TP;
+
+				phydev_info(phydev,
+					    "%s, phy addr: %d, link up, media: %s\n",
+					    __func__, phydev->mdio.addr,
+					    (phydev->port == PORT_TP) ?
+					    "UTP" : "Fiber");

Why do you need to print the PHY address? Isn't the driver model's printing of the device and driver name by phydev_info() sufficient? The same comment applies elsewhere.

Quoted 12 lines
drivers/net/phy/motorcomm.c +12 -0
+static int yt8521_modify_bmcr_paged(struct phy_device *phydev, int page,
+				    u16 mask, u16 set)
+{
+	int max_cnt = 500; /* the max wait time of reset ~ 500 ms */
+	int old_page;
+	int ret = 0;
+
+	old_page = phy_select_page(phydev, page & YT8521_RSSR_SPACE_MASK);
+	if (old_page < 0)
+		goto err_restore_page;
+
+	ret = __phy_modify(phydev, MII_BMCR, mask, set);

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

Quoted 10 lines
drivers/net/phy/motorcomm.c +10 -0
+	if (ret < 0)
+		goto err_restore_page;
+
+	/* If it is reset, need to wait for the reset to complete */
+	if (set == BMCR_RESET) {
+		while (max_cnt--) {
+			/* unlock mdio bus during sleep */
+			phy_unlock_mdio_bus(phydev);
+			usleep_range(1000, 1100);
+			phy_lock_mdio_bus(phydev);

Dropping the lock makes phy_select_page()..phy_restore_page() unsafe since some other code path could change the page selection register while you're sleeping here. If you need to do this, then use phy_restore_page() and re-acquire using phy_select_page() afterwards.

The same comment applies everywhere where you drop the mdio bus lock while in a phy_select_page()..phy_restore_page() region. Every case of that is buggy.

Quoted 15 lines
drivers/net/phy/motorcomm.c +15 -0
+static int yt8521_fiber_setup_forced(struct phy_device *phydev)
+{
+	int max_cnt = 500; /* the max wait time of reset ~ 500 ms */
+	u16 val;
+	int ret;
+
+	if (phydev->speed == SPEED_1000)
+		val = YTPHY_MCR_FIBER_1000BX;
+	else if (phydev->speed == SPEED_100)
+		val = YTPHY_MCR_FIBER_100FX;
+	else
+		return -EINVAL;
+
+	ret =  phy_modify(phydev,  MII_BMCR, BMCR_ANENABLE, 0);
+	if (ret < 0)

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

Quoted 10 lines
drivers/net/phy/motorcomm.c +10 -0
+		return ret;
+
+	/* disable Fiber auto sensing */
+	ret =  ytphy_modify_ext_with_lock(phydev, YT8521_LINK_TIMER_CFG2_REG,
+					  YT8521_LTCR_EN_AUTOSEN, 0);
+	if (ret < 0)
+		return ret;
+
+	phydev->pause = 0;
+	phydev->asym_pause = 0;

Isn't that the responsibility of read_status() ?

Quoted 47 lines
drivers/net/phy/motorcomm.c +47 -0
+/**
+ * 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)
+{
+	__ETHTOOL_DECLARE_LINK_MODE_MASK(fiber_supported);
+	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->advertising should be updated.
+	 */
+	if (priv->reg_page == YT8521_RSSR_TO_BE_ARBITRATED) {
+		linkmode_zero(fiber_supported);
+		yt8521_prepare_fiber_features(phydev, fiber_supported);
+
+		/* prepare fiber_supported, then setup advertising. */
+		if (page == YT8521_RSSR_FIBER_SPACE) {
+			linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
+					 fiber_supported);
+			linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
+					 fiber_supported);
+			linkmode_and(phydev->advertising,
+				     priv->combo_advertising, fiber_supported);
+		} else {
+			/* ETHTOOL_LINK_MODE_Autoneg_BIT is also used in utp */
+			linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
+					   fiber_supported);
+			linkmode_andnot(phydev->advertising,
+					priv->combo_advertising,
+					fiber_supported);
+		}
+	}
+

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

Quoted 10 lines
drivers/net/phy/motorcomm.c +10 -0
+	/* unlock mdio bus during genphy_config_aneg/yt8521_fiber_config_aneg,
+	 * because it will operate this lock.
+	 */
+	phy_unlock_mdio_bus(phydev);
+	if (page == YT8521_RSSR_FIBER_SPACE)
+		ret = yt8521_fiber_config_aneg(phydev);
+	else
+		ret = genphy_config_aneg(phydev);
+
+	phy_lock_mdio_bus(phydev);

As previously pointed out, dropping the MDIO bus lock in a phy_select_page()..phy_restore_page() region unsafe. I think you need to ensure that yt8521_fiber_config_aneg() is safe to be called under the lock, and I suspect having a version of genphy_config_aneg() which can be called with the lock held would be a better approach.

Quoted 20 lines
drivers/net/phy/motorcomm.c +20 -0
+/**
+ * yt8521_get_features_paged() -  read supported link modes for 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_get_features_paged(struct phy_device *phydev, int page)
+{
+	int old_page;
+	int ret = 0;
+
+	page &= YT8521_RSSR_SPACE_MASK;
+	if (page == YT8521_RSSR_FIBER_SPACE) {
+		linkmode_zero(phydev->supported);
+		yt8521_prepare_fiber_features(phydev, phydev->supported);
+		return 0;
+	}
+

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

Quoted 10 lines
drivers/net/phy/motorcomm.c +10 -0
+	old_page = phy_select_page(phydev, page);
+	if (old_page < 0)
+		goto err_restore_page;
+
+	/* 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);

As previously pointed out, this makes the page select/page restore unsafe.

Thanks.

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!