Skip to contentSkip to conversation

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

PATCH Superseded Frank 2 participants 3 messages Jun 28, 2022 – Jun 29, 2022

Subsystems (4)

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Frank

author maintainer
#0 lore raw

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

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).

---
 MAINTAINERS                 |   1 +
 drivers/net/phy/Kconfig     |   2 +-
 drivers/net/phy/motorcomm.c | 950 +++++++++++++++++++++++++++++++++++-
 3 files changed, 950 insertions(+), 3 deletions(-)
MAINTAINERS +1 -0
index a6d3bd9..59813c5 100644
@@ -13437,6 +13437,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 9fee639..9061c76 100644
@@ -253,7 +253,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 +948 -2

991 lines not on this page · raw message

Signature · 1 line
2.31.0.windows.1
maintainer
#1 lore raw

drivers/net/phy/motorcomm.c line 3

Quoted 7 lines
drivers/net/phy/motorcomm.c +1 -1
@@ -1,15 +1,115 @@
 // SPDX-License-Identifier: GPL-2.0+
 /*
- * Driver for Motorcomm PHYs
+ * motorcomm.c: Motorcomm 8511/8521 PHY driver.

Generally you don't include a files name in the file, because it can get renamed and nobody updates it.

drivers/net/phy/motorcomm.c line 12

Quoted 9 lines
drivers/net/phy/motorcomm.c +2 -0
  *
  * Author: Peter Geis <[email protected]>
+ * Author: Frank <[email protected]>
  */
 
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/phy.h>
+#include <linux/etherdevice.h>

The includes are currently sorted, please keep it so.

Quoted 30 lines
drivers/net/phy/motorcomm.c +27 -0
 #define PHY_ID_YT8511		0x0000010a
+#define PHY_ID_YT8521				0x0000011A
+
+/* YT8521 Register Overview
+ *	UTP Register space	|	FIBER Register space
+ *  ------------------------------------------------------------
+ * |	UTP MII			|	FIBER MII		|
+ * |	UTP MMD			|				|
+ * |	UTP Extended		|	FIBER Extended		|
+ *  ------------------------------------------------------------
+ * |			Common Extended				|
+ *  ------------------------------------------------------------
+ */
+
+/* 0x10 ~ 0x15 , 0x1E and 0x1F are common MII registers of yt phy */
+
+/* Specific Function Control Register */
+#define YTPHY_SPECIFIC_FUNCTION_CONTROL_REG	0x10
+
+/* 2b00 Manual MDI configuration
+ * 2b01 Manual MDIX configuration
+ * 2b10 Reserved
+ * 2b11 Enable automatic crossover for all modes  *default*
 
+
+/* 2b00 10 Mbps
+ * 2b01 100 Mbps
+ * 2b10 1000 Mbps
+ * 2b11 Reserved

drivers/net/phy/motorcomm.c

Quoted 10 lines
drivers/net/phy/motorcomm.c +10 -0
+
+/* 8 working modes:
+ * 3b000 UTP_TO_RGMII  *default*
+ * 3b001 FIBER_TO_RGMII
+ * 3b010 UTP_FIBER_TO_RGMII
+ * 3b011 UTP_TO_SGMII
+ * 3b100 SGPHY_TO_RGMAC
+ * 3b101 SGMAC_TO_RGPHY
+ * 3b110 UTP_TO_FIBER_AUTO
+ * 3b111 UTP_TO_FIBER_FORCE

I don't think this notation is used anywhere else in the kernel. It also took me a little while to figure it out. It would probably be better to just add comments next to the #define.

Andrew

maintainer
#2 lore raw
In reply to Frank
Quoted 8 lines
drivers/net/phy/motorcomm.c +8 -0
+static int yt8521_probe(struct phy_device *phydev)
+{
+	struct device *dev = &phydev->mdio.dev;
+	struct yt8521_priv *priv;
+	int chip_config;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)

drivers/net/phy/motorcomm.c err_restore_page: · line 583

Quoted 10 lines
drivers/net/phy/motorcomm.c +10 -0
+		return -ENOMEM;
+
+	phy_lock_mdio_bus(phydev);
+	chip_config = ytphy_read_ext(phydev, YT8521_CHIP_CONFIG_REG);
+	phy_unlock_mdio_bus(phydev);
+	if (chip_config < 0)
+		return chip_config;
+
+	priv->strap_mode = chip_config & YT8521_CCR_MODE_SEL_MASK;
+	switch (priv->strap_mode) {

Please add #defines for these magic numbers. In general, not magic numbers in code.

Quoted 33 lines
drivers/net/phy/motorcomm.c +33 -0
+	case 1:
+	case 4:
+	case 5:
+		priv->polling_mode = YT8521_MODE_FIBER;
+		break;
+	case 2:
+	case 6:
+	case 7:
+		priv->polling_mode = YT8521_MODE_POLL;
+		break;
+	case 3:
+	case 0:
+		priv->polling_mode = YT8521_MODE_UTP;
+		break;
+	}
+
+	phydev->priv = priv;
+
+	return 0;
+}
+
+/**
+ * yt8521_adjust_status() - adjust speed and duplex according to is_utp, then
+ * update speed and duplex to phydev
+ *
+ * @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.
+ *
+ * returns 0
+ */

drivers/net/phy/motorcomm.c err_restore_page: · line 626

Quoted 10 lines
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 = 1;

DUPLEX_FULL

drivers/net/phy/motorcomm.c err_restore_page: · line 631

Quoted 5 lines
drivers/net/phy/motorcomm.c +5 -0
+
+	speed_mode = (status & YTPHY_SSR_SPEED_MODE_MASK) >>
+		     YTPHY_SSR_SPEED_MODE_OFFSET;
+	switch (speed_mode) {
+	case 0:

Please add #defines for these magic numbers

Quoted 117 lines
drivers/net/phy/motorcomm.c +117 -0
+		if (is_utp)
+			speed = SPEED_10;
+		else
+			speed = SPEED_UNKNOWN;
+		break;
+	case 1:
+		speed = SPEED_100;
+		break;
+	case 2:
+		speed = SPEED_1000;
+		break;
+	default:
+		speed = SPEED_UNKNOWN;
+		break;
+	}
+
+	phydev->speed = speed;
+	phydev->duplex = duplex;
+	phydev->port = is_utp ? PORT_TP : PORT_FIBRE;
+
+	return 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;
+	u8 polling_mode = priv->polling_mode;
+	int old_link = phydev->link;
+	int old_page;
+	int status;
+	int ret;
+
+	old_page = yt8521_read_page_with_lock(phydev);
+	if (old_page)
+		return old_page;
+
+	/* YT8521 has two reg space (utp/fiber) and three work mode (utp/fiber/poll),
+	 * each reg space has MII standard regs. reg space should be properly set
+	 * before read link status. Poll mode combines utp and faber mode,so
+	 * need check both.
+	 */
+
+	if (polling_mode == YT8521_MODE_UTP ||
+	    polling_mode == YT8521_MODE_POLL) {
+		ret = yt8521_write_page_with_lock(phydev,
+						  YT8521_RSSR_UTP_SPACE);
+		if (ret < 0)
+			goto err_restore_page;
+
+		/* Update the link, but return if there was an error */
+		ret = genphy_update_link(phydev);
+		if (ret)
+			goto err_restore_page;
+
+		/* why bother the PHY if nothing can have changed */
+		if (phydev->autoneg == AUTONEG_ENABLE && old_link &&
+		    phydev->link)
+			return yt8521_write_page_with_lock(phydev, old_page);
+
+		phydev->speed = SPEED_UNKNOWN;
+		phydev->duplex = DUPLEX_UNKNOWN;
+		phydev->pause = 0;
+		phydev->asym_pause = 0;
+
+		/* Read YTPHY_SPECIFIC_STATUS_REG, which indicates the
+		 * speed and duplex of the PHY is actually using.
+		 */
+		status = phy_read(phydev, YTPHY_SPECIFIC_STATUS_REG);
+		if (status < 0)
+			goto err_restore_page;
+
+		/* check Link status real-time ,if linked then adjust the status*/
+		if (status & YTPHY_SSR_LINK) {
+			ret = yt8521_adjust_status(phydev, status, true);
+			if (ret < 0)
+				goto err_restore_page;
+		}
+		if (phydev->autoneg == AUTONEG_ENABLE &&
+		    phydev->autoneg_complete)
+			phy_resolve_aneg_pause(phydev);
+	}
+
+	if (polling_mode == YT8521_MODE_FIBER ||
+	    polling_mode == YT8521_MODE_POLL) {
+		ret = yt8521_write_page_with_lock(phydev,
+						  YT8521_RSSR_FIBER_SPACE);
+		if (ret < 0)
+			return ret;
+
+		/* Update the link, but return if there was an error */
+		ret = genphy_update_link(phydev);
+		if (ret)
+			goto err_restore_page;
+
+		/* why bother the PHY if nothing can have changed */
+		if (phydev->autoneg == AUTONEG_ENABLE && old_link &&
+		    phydev->link)
+			return yt8521_write_page_with_lock(phydev, old_page);
+
+		phydev->speed = SPEED_UNKNOWN;
+		phydev->duplex = DUPLEX_UNKNOWN;
+		phydev->pause = 0;
+		phydev->asym_pause = 0;
+
+		/* Read YTPHY_SPECIFIC_STATUS_REG, which indicates the
+		 * speed and duplex of the PHY is actually using.
+		 */
+		status = phy_read(phydev, YTPHY_SPECIFIC_STATUS_REG);
+		if (status < 0)
+			goto err_restore_page;
+

drivers/net/phy/motorcomm.c err_restore_page: · line 758

Quoted 10 lines
drivers/net/phy/motorcomm.c +10 -0
+		/* check Link status real-time ,if linked then adjust the status*/
+		if (status & YTPHY_SSR_LINK) {
+			ret = yt8521_adjust_status(phydev, status, false);
+			if (ret < 0)
+				goto err_restore_page;
+		}
+		if (phydev->autoneg == AUTONEG_ENABLE &&
+		    phydev->autoneg_complete)
+			phy_resolve_aneg_pause(phydev);
+	}

This looks very similar to the code above. Can it be turned into a helper?

Quoted 14 lines

+ * yt8521_modify_UTP_FIBER_BMCR - bits modify a PHY's BMCR register according to the
+ * current mode
+ * @phydev: the phy_device struct
+ * @mask: bit mask of bits to clear
+ * @set: bit mask of bits to set
+ *
+ * NOTE: Convenience function which allows a PHY‘s BMCR register to be
+ * modified as new register value = (old register value & ~mask) | set.
+ * YT8521 has two space (utp/fiber) and three mode (utp/fiber/poll), each space
+ * has MII_BMCR. poll mode combines utp and faber,so need do both.
+ * The caller must have taken the MDIO bus lock.
+ *
+ * returns 0 or negative errno code
+ */

drivers/net/phy/motorcomm.c err_restore_page: · line 783

Quoted 2 lines
drivers/net/phy/motorcomm.c +2 -0
+static int yt8521_modify_UTP_FIBER_BMCR(struct phy_device *phydev, u16 mask,
+					u16 set)

Please don't use upper case in function names.

Quoted 7 lines

+ * yt8521_soft_reset() - called to issue a PHY software reset
+ * @phydev: a pointer to a &struct phy_device
+ *
+ * returns 0 or negative errno code
+ */

drivers/net/phy/motorcomm.c +2 -0
+int yt8521_soft_reset(struct phy_device *phydev)
+{

drivers/net/phy/motorcomm.c err_restore_page: · line 830

Quoted 10 lines
drivers/net/phy/motorcomm.c +10 -0
+	int old_page;
+	int ret = 0;
+
+	old_page = phy_save_page(phydev);
+	if (old_page < 0)
+		goto err_restore_page;
+
+	ret = yt8521_modify_UTP_FIBER_BMCR(phydev, 0, BMCR_RESET);
+	if (ret < 0)
+		goto err_restore_page;

You should wait for the reset to completed. Can you actually use the core helper? Is the BMCR in the usual place? So long as you hold the lock, nothing is going to change the page, so you should be able to use the helper.

drivers/net/phy/motorcomm.c err_restore_page: · line 933

Quoted 2 lines
drivers/net/phy/motorcomm.c +2 -0
+	/* enable RXC clock when no wire plug */
+	ret = ytphy_modify_ext(phydev, 0xc, BIT(12), 0);

#defines please.

Quoted 37 lines
drivers/net/phy/motorcomm.c +37 -0
+	if (ret < 0)
+		goto err_restore_page;
+
+err_restore_page:
+	return phy_restore_page(phydev, old_page, ret);
+}
+
+/**
+ * yt8521_config_aneg() - change reg space then call genphy_config_aneg
+ * @phydev: a pointer to a &struct phy_device
+ *
+ * returns 0 or negative errno code
+ */
+int yt8521_config_aneg(struct phy_device *phydev)
+{
+	struct yt8521_priv *priv = phydev->priv;
+	u8 polling_mode = priv->polling_mode;
+	int old_page;
+	int ret;
+
+	old_page = yt8521_read_page_with_lock(phydev);
+	if (old_page)
+		return old_page;
+
+	if (polling_mode == YT8521_MODE_FIBER ||
+	    polling_mode == YT8521_MODE_POLL) {
+		ret = yt8521_write_page_with_lock(phydev,
+						  YT8521_RSSR_FIBER_SPACE);
+		if (ret < 0)
+			goto err_restore_page;
+
+		ret = genphy_config_aneg(phydev);
+		if (ret < 0)
+			goto err_restore_page;
+	}
+
+	if (polling_mode == YT8521_MODE_UTP ||

drivers/net/phy/motorcomm.c err_restore_page: · line 980

Quoted 10 lines
drivers/net/phy/motorcomm.c +10 -0
+	    polling_mode == YT8521_MODE_POLL) {
+		ret = yt8521_write_page_with_lock(phydev,
+						  YT8521_RSSR_UTP_SPACE);
+		if (ret < 0)
+			goto err_restore_page;
+
+		ret = genphy_config_aneg(phydev);
+		if (ret < 0)
+			goto err_restore_page;
+	}

Looks like this could be refactored to reduce duplication.

Quoted 46 lines
drivers/net/phy/motorcomm.c +46 -0
+
+	return yt8521_write_page_with_lock(phydev, old_page);
+
+err_restore_page:
+	yt8521_write_page_with_lock(phydev, old_page);
+	return ret;
+}
+
+/**
+ * yt8521_aneg_done() - determines the auto negotiation result
+ * @phydev: a pointer to a &struct phy_device
+ *
+ * returns 0(no link)or 1( fiber or utp link) or negative errno code
+ */
+int yt8521_aneg_done(struct phy_device *phydev)
+{
+	struct yt8521_priv *priv = phydev->priv;
+	u8 polling_mode = priv->polling_mode;
+	int link_fiber = 0;
+	int link_utp = 0;
+	int old_page;
+	int ret = 0;
+
+	old_page = phy_save_page(phydev);
+	if (old_page < 0)
+		goto err_restore_page;
+
+	if (polling_mode == YT8521_MODE_FIBER ||
+	    polling_mode == YT8521_MODE_POLL) {
+		/* switch to FIBER reg space*/
+		ret = yt8521_write_page(phydev, YT8521_RSSR_FIBER_SPACE);
+		if (ret < 0)
+			goto err_restore_page;
+
+		ret = __phy_read(phydev, YTPHY_SPECIFIC_STATUS_REG);
+		if (ret < 0)
+			goto err_restore_page;
+
+		link_fiber = !!(ret & YTPHY_SSR_LINK);
+	}
+
+	if (polling_mode == YT8521_MODE_UTP ||
+	    polling_mode == YT8521_MODE_POLL) {
+		/* switch to UTP reg space */
+		ret = yt8521_write_page(phydev, YT8521_RSSR_UTP_SPACE);
+		if (ret < 0)

drivers/net/phy/motorcomm.c err_restore_page: · line 1036

Quoted 10 lines
drivers/net/phy/motorcomm.c +10 -0
+			goto err_restore_page;
+
+		ret = __phy_read(phydev, YTPHY_SPECIFIC_STATUS_REG);
+		if (ret < 0)
+			goto err_restore_page;
+
+		link_utp = !!(ret & YTPHY_SSR_LINK);
+	}
+
+	ret = !!(link_fiber | link_utp);

Does this mean it can do both copper and fibre at the same time. And whichever gives up first wins?

Andrew