Skip to contentSkip to conversation

net: dsa: realtek: rtl8365mb: add SGMII support for RTL8367S

PATCH Superseded Johan Alvarado 2 participants 4 messages Jun 10 – Jun 12 1 message not in the archive

Subsystems (4)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH v2 1/2 net: dsa: realtek: rtl8365mb: add SGMII support for RTL8367S

The RTL8367S can mux its embedded SerDes to external interface 1, which is typically used to connect the switch to a CPU port. The chip info table already declares SGMII as a supported interface mode for this chip, but the driver only implements RGMII so far.

Implement SGMII support, with the configuration sequence derived from the GPL-licensed Realtek rtl8367c vendor driver as distributed in the Mercusys MR80X GPL code drop:

- Add accessors for the SerDes indirect access registers (SDS_INDACS), through which the SerDes internal registers are reached.

- Clear the line rate bypass bit for the external interface, tune the SerDes with the vendor-prescribed parameters, mux the SerDes to MAC8 in SGMII mode and only then take the SerDes out of reset, as the vendor driver does.

- Load firmware into the embedded DW8051 microcontroller, which performs the remaining SerDes initialization. The firmware is requested via the firmware API under the name realtek/rtl8367s-sgmii.bin.

- Force the SGMII link parameters (link, speed, duplex, flow control) in the SDS_MISC register from the phylink mac_link_up/down operations, in addition to the usual external interface force configuration. SGMII in-band autonegotiation is disabled, so only fixed-link and conventional PHY setups are supported, just like RGMII.

Tested on a Mercusys MR80X v2.20, where the RTL8367S is connected to the SoC over SGMII.

---
 drivers/net/dsa/realtek/rtl8365mb.c | 357 +++++++++++++++++++++++++++-
 1 file changed, 354 insertions(+), 3 deletions(-)
drivers/net/dsa/realtek/rtl8365mb.c +354 -3
index c35cef01ec26..13ce8e6f1c72 100644
@@ -40,7 +40,8 @@
  * driver has only been tested with a fixed-link, but in principle it should not
  * matter.
  *
- * NOTE: Currently, only the RGMII interface is implemented in this driver.
+ * NOTE: Currently, only the RGMII interface is implemented in this driver. On
+ * the RTL8367S, the SGMII interface is also supported.
  *
  * The interrupt line is asserted on link UP/DOWN events. The driver creates a
  * custom irqchip to handle this interrupt and demultiplex the events by reading
@@ -92,6 +93,7 @@
 
 #include <linux/bitfield.h>
 #include <linux/bitops.h>
+#include <linux/firmware.h>
 #include <linux/interrupt.h>
 #include <linux/irqdomain.h>
 #include <linux/mutex.h>
@@ -123,6 +125,7 @@
 
 /* Chip reset register */
 #define RTL8365MB_CHIP_RESET_REG	0x1322
+#define RTL8365MB_CHIP_RESET_DW8051_MASK	0x0010
 #define RTL8365MB_CHIP_RESET_SW_MASK	0x0002
 #define RTL8365MB_CHIP_RESET_HW_MASK	0x0001
 
@@ -232,6 +235,47 @@
 #define   RTL8365MB_EXT_RGMXF_RXDELAY_MASK	0x0007
 #define   RTL8365MB_EXT_RGMXF_TXDELAY_MASK	0x0008
 
+/* External interface line rate bypass register - one bit per interface */
+#define RTL8365MB_BYPASS_LINE_RATE_REG	0x03F7
+
+/* SerDes indirect access registers */
+#define RTL8365MB_SDS_INDACS_CMD_REG		0x6600
+#define   RTL8365MB_SDS_INDACS_CMD_RUN_MASK	0x0080
+#define   RTL8365MB_SDS_INDACS_CMD_WR_MASK	0x0040
+#define   RTL8365MB_SDS_INDACS_CMD_INDEX_MASK	0x0007
+#define RTL8365MB_SDS_INDACS_ADR_REG		0x6601
+#define RTL8365MB_SDS_INDACS_DATA_REG		0x6602
+
+/* SerDes miscellaneous configuration register */
+#define RTL8365MB_SDS_MISC_REG				0x1D11
+#define   RTL8365MB_SDS_MISC_SGMII_RXFC_MASK		0x4000
+#define   RTL8365MB_SDS_MISC_SGMII_TXFC_MASK		0x2000
+#define   RTL8365MB_SDS_MISC_MAC8_SEL_HSGMII_MASK	0x0800
+#define   RTL8365MB_SDS_MISC_SGMII_FDUP_MASK		0x0400
+#define   RTL8365MB_SDS_MISC_SGMII_LINK_MASK		0x0200
+#define   RTL8365MB_SDS_MISC_SGMII_SPD_MASK		0x0180
+#define   RTL8365MB_SDS_MISC_MAC8_SEL_SGMII_MASK	0x0040
+
+/* SerDes internal registers, accessed via the SDS_INDACS registers */
+#define RTL8365MB_SDS_REG_NWAY			0x0002
+#define   RTL8365MB_SDS_NWAY_EN_MASK		0x0200
+#define   RTL8365MB_SDS_NWAY_RESTART_MASK	0x0100
+#define RTL8365MB_SDS_REG_RESET			0x0003
+#define   RTL8365MB_SDS_RESET_DEASSERT		0x7106
+
+/* Embedded DW8051 microcontroller control registers. The microcontroller
+ * runs firmware which is required to bring up the SerDes for SGMII modes.
+ */
+#define RTL8365MB_MISC_CFG0_REG			0x130C
+#define   RTL8365MB_MISC_CFG0_DW8051_EN_MASK	0x0020
+#define RTL8365MB_DW8051_RDY_REG		0x1336
+#define   RTL8365MB_DW8051_RDY_IROM_MSB_MASK	0x0004
+#define   RTL8365MB_DW8051_RDY_ACS_IROM_EN_MASK	0x0002
+#define RTL8365MB_IROM_BASE			0xE000
+#define RTL8365MB_IROM_SIZE			0x2000
+
+#define RTL8365MB_DW8051_FW_NAME		"realtek/rtl8367s-sgmii.bin"
+
 /* External interface port speed values - used in DIGITAL_INTERFACE_FORCE */
 #define RTL8365MB_PORT_SPEED_10M	0
 #define RTL8365MB_PORT_SPEED_100M	1
@@ -470,6 +514,13 @@ static const struct rtl8365mb_jam_tbl_entry rtl8365mb_init_jam_common[] = {
 	{ 0x1D32, 0x0002 },
 };
 
+/* SGMII SerDes tuning parameters, lifted from the vendor driver sources */
+static const struct rtl8365mb_jam_tbl_entry rtl8365mb_sds_jam_sgmii[] = {
+	{ 0x0480, 0x04D7 }, { 0x0481, 0xF994 }, { 0x0482, 0x2420 },
+	{ 0x0483, 0x6960 }, { 0x0484, 0x9728 }, { 0x0423, 0x9D85 },
+	{ 0x0424, 0xD810 }, { 0x002E, 0x83F2 },
+};
+
 enum rtl8365mb_phy_interface_mode {
 	RTL8365MB_PHY_INTERFACE_MODE_INVAL = 0,
 	RTL8365MB_PHY_INTERFACE_MODE_INTERNAL = BIT(0),
@@ -947,6 +998,271 @@ static int rtl8365mb_ext_config_rgmii(struct realtek_priv *priv, int port,
 	return 0;
 }
 
+static int rtl8365mb_sds_write(struct realtek_priv *priv, int sds, u16 addr,
+			       u16 data)
+{
+	u32 val;
+	int ret;
+
+	ret = regmap_write(priv->map, RTL8365MB_SDS_INDACS_DATA_REG, data);
+	if (ret)
+		return ret;
+
+	ret = regmap_write(priv->map, RTL8365MB_SDS_INDACS_ADR_REG, addr);
+	if (ret)
+		return ret;
+
+	val = RTL8365MB_SDS_INDACS_CMD_RUN_MASK |
+	      RTL8365MB_SDS_INDACS_CMD_WR_MASK |
+	      FIELD_PREP(RTL8365MB_SDS_INDACS_CMD_INDEX_MASK, sds);
+	ret = regmap_write(priv->map, RTL8365MB_SDS_INDACS_CMD_REG, val);
+	if (ret)
+		return ret;
+
+	/* The vendor driver does not poll for completion, but a short delay
+	 * is needed before issuing the next command.
+	 */
+	usleep_range(10, 50);
+
+	return 0;
+}
+
+static int rtl8365mb_sds_read(struct realtek_priv *priv, int sds, u16 addr,
+			      u16 *data)
+{
+	u32 val;
+	int ret;
+
+	ret = regmap_write(priv->map, RTL8365MB_SDS_INDACS_ADR_REG, addr);
+	if (ret)
+		return ret;
+
+	val = RTL8365MB_SDS_INDACS_CMD_RUN_MASK |
+	      FIELD_PREP(RTL8365MB_SDS_INDACS_CMD_INDEX_MASK, sds);
+	ret = regmap_write(priv->map, RTL8365MB_SDS_INDACS_CMD_REG, val);
+	if (ret)
+		return ret;
+
+	usleep_range(10, 50);
+
+	ret = regmap_read(priv->map, RTL8365MB_SDS_INDACS_DATA_REG, &val);
+	if (ret)
+		return ret;
+
+	*data = val;
+
+	return 0;
+}
+
+static int rtl8365mb_dw8051_load_firmware(struct realtek_priv *priv)
+{
+	const struct firmware *fw;
+	int ret;
+	int i;
+
+	ret = request_firmware(&fw, RTL8365MB_DW8051_FW_NAME, priv->dev);
+	if (ret) {
+		dev_err(priv->dev, "failed to request firmware %s: %d\n",
+			RTL8365MB_DW8051_FW_NAME, ret);
+		return ret;
+	}
+
+	if (fw->size > RTL8365MB_IROM_SIZE) {
+		dev_err(priv->dev, "firmware %s too large (%zu bytes)\n",
+			RTL8365MB_DW8051_FW_NAME, fw->size);
+		ret = -EFBIG;
+		goto out_release;
+	}
+
+	/* Hold the microcontroller in reset while replacing its program */
+	ret = regmap_update_bits(priv->map, RTL8365MB_CHIP_RESET_REG,
+				 RTL8365MB_CHIP_RESET_DW8051_MASK,
+				 RTL8365MB_CHIP_RESET_DW8051_MASK);
+	if (ret)
+		goto out_release;
+
+	ret = regmap_update_bits(priv->map, RTL8365MB_MISC_CFG0_REG,
+				 RTL8365MB_MISC_CFG0_DW8051_EN_MASK,
+				 RTL8365MB_MISC_CFG0_DW8051_EN_MASK);
+	if (ret)
+		goto out_release;
+
+	/* Map the lower bank of the instruction ROM into the switch register
+	 * address space.
+	 */
+	ret = regmap_update_bits(priv->map, RTL8365MB_DW8051_RDY_REG,
+				 RTL8365MB_DW8051_RDY_ACS_IROM_EN_MASK,
+				 RTL8365MB_DW8051_RDY_ACS_IROM_EN_MASK);
+	if (ret)
+		goto out_release;
+
+	ret = regmap_update_bits(priv->map, RTL8365MB_DW8051_RDY_REG,
+				 RTL8365MB_DW8051_RDY_IROM_MSB_MASK, 0);
+	if (ret)
+		goto out_release;
+
+	for (i = 0; i < fw->size; i++) {
+		ret = regmap_write(priv->map, RTL8365MB_IROM_BASE + i,
+				   fw->data[i]);
+		if (ret)
+			goto out_release;
+	}
+
+	ret = regmap_update_bits(priv->map, RTL8365MB_DW8051_RDY_REG,
+				 RTL8365MB_DW8051_RDY_ACS_IROM_EN_MASK, 0);
+	if (ret)
+		goto out_release;
+
+	/* Release the microcontroller from reset to run the new program */
+	ret = regmap_update_bits(priv->map, RTL8365MB_CHIP_RESET_REG,
+				 RTL8365MB_CHIP_RESET_DW8051_MASK, 0);
+
+out_release:
+	release_firmware(fw);
+
+	return ret;
+}
+
+static int rtl8365mb_ext_config_sgmii(struct realtek_priv *priv, int port,
+				      phy_interface_t interface)
+{
+	const struct rtl8365mb_extint *extint =
+		rtl8365mb_get_port_extint(priv, port);
+	u16 val;
+	int ret;
+	int i;
+
+	if (!extint)
+		return -ENODEV;
+
+	/* The SerDes can only be muxed to external interface 1 */
+	if (extint->id != 1)
+		return -EOPNOTSUPP;
+
+	/* The vendor driver clears the line rate bypass for all interface
+	 * modes except TMII.
+	 */
+	ret = regmap_update_bits(priv->map, RTL8365MB_BYPASS_LINE_RATE_REG,
+				 BIT(extint->id), 0);
+	if (ret)
+		return ret;
+
+	/* Tune the SerDes with vendor-prescribed parameters */
+	for (i = 0; i < ARRAY_SIZE(rtl8365mb_sds_jam_sgmii); i++) {
+		ret = rtl8365mb_sds_write(priv, 0,
+					  rtl8365mb_sds_jam_sgmii[i].reg,
+					  rtl8365mb_sds_jam_sgmii[i].val);
+		if (ret)
+			return ret;
+	}
+
+	/* Mux the SerDes to MAC8 in SGMII mode */
+	ret = regmap_update_bits(priv->map, RTL8365MB_SDS_MISC_REG,
+				 RTL8365MB_SDS_MISC_MAC8_SEL_SGMII_MASK |
+					 RTL8365MB_SDS_MISC_MAC8_SEL_HSGMII_MASK,
+				 RTL8365MB_SDS_MISC_MAC8_SEL_SGMII_MASK);
+	if (ret)
+		return ret;
+
+	ret = regmap_update_bits(
+		priv->map, RTL8365MB_DIGITAL_INTERFACE_SELECT_REG(extint->id),
+		RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_MASK(extint->id),
+		RTL8365MB_EXT_PORT_MODE_SGMII
+			<< RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_OFFSET(
+				   extint->id));
+	if (ret)
+		return ret;
+
+	/* Take the SerDes out of reset. The vendor driver does this only
+	 * after the SerDes mux and the interface mode are configured.
+	 */
+	ret = rtl8365mb_sds_write(priv, 0, RTL8365MB_SDS_REG_RESET,
+				  RTL8365MB_SDS_RESET_DEASSERT);
+	if (ret)
+		return ret;
+
+	/* The embedded microcontroller performs the remaining SerDes
+	 * initialization. Load its firmware and give it some time to bring
+	 * the SerDes up.
+	 */
+	ret = rtl8365mb_dw8051_load_firmware(priv);
+	if (ret)
+		return ret;
+
+	msleep(300);
+
+	/* Pause the microcontroller while accessing the SerDes directly */
+	ret = regmap_update_bits(priv->map, RTL8365MB_MISC_CFG0_REG,
+				 RTL8365MB_MISC_CFG0_DW8051_EN_MASK, 0);
+	if (ret)
+		return ret;
+
+	/* Disable SGMII in-band autonegotiation: the link parameters are
+	 * forced in rtl8365mb_phylink_mac_link_up.
+	 */
+	ret = rtl8365mb_sds_read(priv, 0, RTL8365MB_SDS_REG_NWAY, &val);
+	if (ret)
+		return ret;
+
+	val &= ~RTL8365MB_SDS_NWAY_EN_MASK;
+	val |= RTL8365MB_SDS_NWAY_RESTART_MASK;
+
+	ret = rtl8365mb_sds_write(priv, 0, RTL8365MB_SDS_REG_NWAY, val);
+	if (ret)
+		return ret;
+
+	return regmap_update_bits(priv->map, RTL8365MB_MISC_CFG0_REG,
+				  RTL8365MB_MISC_CFG0_DW8051_EN_MASK,
+				  RTL8365MB_MISC_CFG0_DW8051_EN_MASK);
+}
+
+static bool rtl8365mb_interface_is_serdes(phy_interface_t interface)
+{
+	return interface == PHY_INTERFACE_MODE_SGMII;
+}
+
+static int rtl8365mb_sds_config_forcemode(struct realtek_priv *priv, bool link,
+					  int speed, int duplex, bool tx_pause,
+					  bool rx_pause)
+{
+	u32 mask = RTL8365MB_SDS_MISC_SGMII_RXFC_MASK |
+		   RTL8365MB_SDS_MISC_SGMII_TXFC_MASK |
+		   RTL8365MB_SDS_MISC_SGMII_FDUP_MASK |
+		   RTL8365MB_SDS_MISC_SGMII_LINK_MASK |
+		   RTL8365MB_SDS_MISC_SGMII_SPD_MASK;
+	u32 val = 0;
+	u32 r_speed;
+
+	if (link) {
+		if (speed == SPEED_1000) {
+			r_speed = RTL8365MB_PORT_SPEED_1000M;
+		} else if (speed == SPEED_100) {
+			r_speed = RTL8365MB_PORT_SPEED_100M;
+		} else if (speed == SPEED_10) {
+			r_speed = RTL8365MB_PORT_SPEED_10M;
+		} else {
+			dev_err(priv->dev, "unsupported port speed %s\n",
+				phy_speed_to_str(speed));
+			return -EINVAL;
+		}
+
+		val |= RTL8365MB_SDS_MISC_SGMII_LINK_MASK;
+		val |= FIELD_PREP(RTL8365MB_SDS_MISC_SGMII_SPD_MASK, r_speed);
+
+		if (duplex == DUPLEX_FULL)
+			val |= RTL8365MB_SDS_MISC_SGMII_FDUP_MASK;
+
+		if (tx_pause)
+			val |= RTL8365MB_SDS_MISC_SGMII_TXFC_MASK;
+
+		if (rx_pause)
+			val |= RTL8365MB_SDS_MISC_SGMII_RXFC_MASK;
+	}
+
+	return regmap_update_bits(priv->map, RTL8365MB_SDS_MISC_REG, mask,
+				  val);
+}
+
 static int rtl8365mb_ext_config_forcemode(struct realtek_priv *priv, int port,
 					  bool link, int speed, int duplex,
 					  bool tx_pause, bool rx_pause)
@@ -1046,6 +1362,10 @@ static void rtl8365mb_phylink_get_caps(struct dsa_switch *ds, int port,
 
 	if (extint->supported_interfaces & RTL8365MB_PHY_INTERFACE_MODE_RGMII)
 		phy_interface_set_rgmii(config->supported_interfaces);
+
+	if (extint->supported_interfaces & RTL8365MB_PHY_INTERFACE_MODE_SGMII)
+		__set_bit(PHY_INTERFACE_MODE_SGMII,
+			  config->supported_interfaces);
 }
 
 static void rtl8365mb_phylink_mac_config(struct phylink_config *config,
@@ -1073,6 +1393,15 @@ static void rtl8365mb_phylink_mac_config(struct phylink_config *config,
 		return;
 	}
 
+	if (rtl8365mb_interface_is_serdes(state->interface)) {
+		ret = rtl8365mb_ext_config_sgmii(priv, port, state->interface);
+		if (ret)
+			dev_err(priv->dev,
+				"failed to configure SGMII mode on port %d: %d\n",
+				port, ret);
+		return;
+	}
+
 	/* TODO: Implement MII and RMII modes, which the RTL8365MB-VC also
 	 * supports
 	 */
@@ -1093,7 +1422,8 @@ static void rtl8365mb_phylink_mac_link_down(struct phylink_config *config,
 	p = &mb->ports[port];
 	cancel_delayed_work_sync(&p->mib_work);
 
-	if (phy_interface_mode_is_rgmii(interface)) {
+	if (phy_interface_mode_is_rgmii(interface) ||
+	    rtl8365mb_interface_is_serdes(interface)) {
 		ret = rtl8365mb_ext_config_forcemode(priv, port, false, 0, 0,
 						     false, false);
 		if (ret)
@@ -1101,6 +1431,15 @@ static void rtl8365mb_phylink_mac_link_down(struct phylink_config *config,
 				"failed to reset forced mode on port %d: %d\n",
 				port, ret);
 
+		if (rtl8365mb_interface_is_serdes(interface)) {
+			ret = rtl8365mb_sds_config_forcemode(priv, false, 0, 0,
+							     false, false);
+			if (ret)
+				dev_err(priv->dev,
+					"failed to reset forced SGMII link on port %d: %d\n",
+					port, ret);
+		}
+
 		return;
 	}
 }
@@ -1123,7 +1462,8 @@ static void rtl8365mb_phylink_mac_link_up(struct phylink_config *config,
 	p = &mb->ports[port];
 	schedule_delayed_work(&p->mib_work, 0);
 
-	if (phy_interface_mode_is_rgmii(interface)) {
+	if (phy_interface_mode_is_rgmii(interface) ||
+	    rtl8365mb_interface_is_serdes(interface)) {
 		ret = rtl8365mb_ext_config_forcemode(priv, port, true, speed,
 						     duplex, tx_pause,
 						     rx_pause);
@@ -1132,6 +1472,16 @@ static void rtl8365mb_phylink_mac_link_up(struct phylink_config *config,
 				"failed to force mode on port %d: %d\n", port,
 				ret);
 
+		if (rtl8365mb_interface_is_serdes(interface)) {
+			ret = rtl8365mb_sds_config_forcemode(priv, true, speed,
+							     duplex, tx_pause,
+							     rx_pause);
+			if (ret)
+				dev_err(priv->dev,
+					"failed to force SGMII link on port %d: %d\n",
+					port, ret);
+		}
+
 		return;
 	}
 }
@@ -2206,5 +2556,6 @@ module_exit(rtl8365mb_exit);
 
 MODULE_AUTHOR("Alvin Šipraga <[email protected]>");
 MODULE_DESCRIPTION("Driver for RTL8365MB-VC ethernet switch");
+MODULE_FIRMWARE(RTL8365MB_DW8051_FW_NAME);
 MODULE_LICENSE("GPL");
 MODULE_IMPORT_NS("REALTEK_DSA");
Signature · 1 line
2.54.0
#2 lore raw
Quoted 29 lines

The RTL8367S can mux its embedded SerDes to external interface 1, which is typically used to connect the switch to a CPU port. The chip info table already declares SGMII as a supported interface mode for this chip, but the driver only implements RGMII so far.

Implement SGMII support, with the configuration sequence derived from the GPL-licensed Realtek rtl8367c vendor driver as distributed in the Mercusys MR80X GPL code drop:

- Add accessors for the SerDes indirect access registers (SDS_INDACS), through which the SerDes internal registers are reached.

- Clear the line rate bypass bit for the external interface, tune the SerDes with the vendor-prescribed parameters, mux the SerDes to MAC8 in SGMII mode and only then take the SerDes out of reset, as the vendor driver does.

- Load firmware into the embedded DW8051 microcontroller, which performs the remaining SerDes initialization. The firmware is requested via the firmware API under the name realtek/rtl8367s-sgmii.bin.

- Force the SGMII link parameters (link, speed, duplex, flow control) in the SDS_MISC register from the phylink mac_link_up/down operations, in addition to the usual external interface force configuration. SGMII in-band autonegotiation is disabled, so only fixed-link and conventional PHY setups are supported, just like RGMII.

Quoted 4 lines

Tested on a Mercusys MR80X v2.20, where the RTL8367S is connected to the SoC over SGMII.

This code is based on code from other developers. Please ensure proper credits/Co-developed-by tags are added if applicable.

Quoted 54 lines
drivers/net/dsa/realtek/rtl8365mb.c +54 -0
+
+static int rtl8365mb_ext_config_sgmii(struct realtek_priv *priv, int port,
+                                     phy_interface_t interface)
+{
+       const struct rtl8365mb_extint *extint =
+               rtl8365mb_get_port_extint(priv, port);
+       u16 val;
+       int ret;
+       int i;
+
+       if (!extint)
+               return -ENODEV;
+
+       /* The SerDes can only be muxed to external interface 1 */
+       if (extint->id != 1)
+               return -EOPNOTSUPP;
+
+       /* The vendor driver clears the line rate bypass for all interface
+        * modes except TMII.
+        */
+       ret = regmap_update_bits(priv->map, RTL8365MB_BYPASS_LINE_RATE_REG,
+                                BIT(extint->id), 0);
+       if (ret)
+               return ret;
+
+       /* Tune the SerDes with vendor-prescribed parameters */
+       for (i = 0; i < ARRAY_SIZE(rtl8365mb_sds_jam_sgmii); i++) {
+               ret = rtl8365mb_sds_write(priv, 0,
+                                         rtl8365mb_sds_jam_sgmii[i].reg,
+                                         rtl8365mb_sds_jam_sgmii[i].val);
+               if (ret)
+                       return ret;
+       }
+
+       /* Mux the SerDes to MAC8 in SGMII mode */
+       ret = regmap_update_bits(priv->map, RTL8365MB_SDS_MISC_REG,
+                                RTL8365MB_SDS_MISC_MAC8_SEL_SGMII_MASK |
+                                        RTL8365MB_SDS_MISC_MAC8_SEL_HSGMII_MASK,
+                                RTL8365MB_SDS_MISC_MAC8_SEL_SGMII_MASK);
+       if (ret)
+               return ret;
+
+       ret = regmap_update_bits(
+               priv->map, RTL8365MB_DIGITAL_INTERFACE_SELECT_REG(extint->id),
+               RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_MASK(extint->id),
+               RTL8365MB_EXT_PORT_MODE_SGMII
+                       << RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_OFFSET(
+                                  extint->id));
+       if (ret)
+               return ret;
+
+       /* Take the SerDes out of reset. The vendor driver does this only
+        * after the SerDes mux and the interface mode are configured.
+        */

drivers/net/dsa/realtek/rtl8365mb.c static int rtl8365mb_ext_config_rgmii(struct realtek_priv *priv, int port, · line 1188

Quoted 10 lines
drivers/net/dsa/realtek/rtl8365mb.c +10 -0
+       ret = rtl8365mb_sds_write(priv, 0, RTL8365MB_SDS_REG_RESET,
+                                 RTL8365MB_SDS_RESET_DEASSERT);
+       if (ret)
+               return ret;
+
+       /* The embedded microcontroller performs the remaining SerDes
+        * initialization. Load its firmware and give it some time to bring
+        * the SerDes up.
+        */
+       ret = rtl8365mb_dw8051_load_firmware(priv);

I checked the vendor firmware behavior and it essentially initializes the interface and runs an infinite loop polling for link state changes. Crucially, it writes to the exact same registers we already manage in this driver via phylink:

- RTL8365MB_DIGITAL_INTERFACE_SELECT_REG0
- RTL8365MB_DIGITAL_INTERFACE_SELECT_REG1
- RTL8365MB_DIGITAL_INTERFACE_FORCE_REG0
- RTL8365MB_DIGITAL_INTERFACE_FORCE_REG1
- RTL8365MB_DIGITAL_INTERFACE_FORCE_REG2
- RTL8365MB_EXT_RGMXF_REG0

By allowing the DW8051 to run, we introduce a "two heads controlling the switch" scenario. The firmware loops polling for port status and sets REG_EXT_FORCE_{0,1,2} behind our backs. While this might go unnoticed in a strictly fixed-link topology, it introduces race conditions and unexpected behaviors if phylink is also manipulating these registers.

Furthermore, analyzing the firmware reveals that right after deasserting the SerDes Reset, it applies the following sequence:

rtl8365mb_sds_write(priv, 0, 0x0000, 0x1401);
rtl8365mb_sds_write(priv, 0, 0x0000, 0x1403);

These are writes to the SerDes Basic Mode Control Register (BMCR, Register 0). 0x1400 asserts BMCR_ANENABLE | BMCR_ISOLATE. Toggling the lower vendor-specific bits (0x01 to 0x03) triggers a data-path reset / PLL resync. This clears out FIFOs and ensures a clean cold boot for the SGMII link, preventing silent drops or CRC errors.

I think we don't need the firmware at all. Requesting a proprietary binary that we don't control to do things phylink already does is unnecessary and adds a burden to linux-firmware.
I strongly suggest dropping rtl8365mb_dw8051_load_firmware() entirely, porting the BMCR reset sequence above directly into the driver, and keeping the 8051 disabled.

drivers/net/dsa/realtek/rtl8365mb.c static int rtl8365mb_ext_config_rgmii(struct realtek_priv *priv, int port, · line 1206

Quoted 13 lines
drivers/net/dsa/realtek/rtl8365mb.c +13 -0
+       /* Pause the microcontroller while accessing the SerDes directly */
+       ret = regmap_update_bits(priv->map, RTL8365MB_MISC_CFG0_REG,
+                                RTL8365MB_MISC_CFG0_DW8051_EN_MASK, 0);
+       if (ret)
+               return ret;
+
+       /* Disable SGMII in-band autonegotiation: the link parameters are
+        * forced in rtl8365mb_phylink_mac_link_up.
+        */
+       ret = rtl8365mb_sds_read(priv, 0, RTL8365MB_SDS_REG_NWAY, &val);
+       if (ret)
+               return ret;
+

If we intend to support non-fixed links in the future (where in-band AN is required), we would need to implement phylink_pcs and pcs_get_state instead of just forcing the MAC. For fixed-link it is fine, but we should plan accordingly.

drivers/net/dsa/realtek/rtl8365mb.c static int rtl8365mb_ext_config_rgmii(struct realtek_priv *priv, int port, · line 1218

Quoted 12 lines
drivers/net/dsa/realtek/rtl8365mb.c +12 -0
+       val &= ~RTL8365MB_SDS_NWAY_EN_MASK;
+       val |= RTL8365MB_SDS_NWAY_RESTART_MASK;
+
+       ret = rtl8365mb_sds_write(priv, 0, RTL8365MB_SDS_REG_NWAY, val);
+       if (ret)
+               return ret;
+
+       return regmap_update_bits(priv->map, RTL8365MB_MISC_CFG0_REG,
+                                 RTL8365MB_MISC_CFG0_DW8051_EN_MASK,
+                                 RTL8365MB_MISC_CFG0_DW8051_EN_MASK);
+}
+

Regards,

Luiz

#3 lore raw
Quoted 6 lines

I think we don't need the firmware at all. Requesting a proprietary binary that we don't control to do things phylink already does is unnecessary and adds a burden to linux-firmware.
I strongly suggest dropping rtl8365mb_dw8051_load_firmware() entirely, porting the BMCR reset sequence above directly into the driver, and keeping the 8051 disabled.

I tested exactly this on the MR80X v2: dropped the firmware loading, ported the 0x1401/0x1403 BMCR sequence right after the SerDes reset deassert, and kept the DW8051 permanently disabled (also removing the re-enable at the end of the SGMII config path).

Results in both SGMII and HSGMII modes:

- Link bring-up: 10/10 cold boots (power drained), 10/10 warm reboots, 10/10 module reload cycles (rmmod/insmod), and repeated ip link down/up — link came up cleanly every time.

- Sustained traffic, CPU-generated (iperf3 on the router, 30 min each direction): ~750 Mbit/s in both modes, CPU-bound. Zero CRC/symbol errors in the port MIB counters.

- NAT forwarding LAN->WAN (iperf3 between external hosts, 10-30 min runs): ~800-820 Mbit/s sustained, PHY-bound (1G user ports), again with zero CRC/symbol errors. A small number of buffer discards under sustained load (~0.07%), consistent with congestion rather than link-level corruption.

So I can confirm your analysis. v3 will drop the firmware entirely and carry this with your Suggested-by.

Quoted 4 lines

If we intend to support non-fixed links in the future (where in-band AN is required), we would need to implement phylink_pcs and pcs_get_state instead of just forcing the MAC. For fixed-link it is fine, but we should plan accordingly.

Agreed. I'd prefer to keep v3 scoped to fixed-link (matching what RGMII supports today) and leave the phylink_pcs conversion for a follow-up series.

Best regards,
Johan

#4 lore raw

PATCH v2 2/2 net: dsa: realtek: rtl8365mb: add HSGMII support for RTL8367S

In reply to a message not in the archive

In addition to SGMII, the RTL8367S SerDes also supports HSGMII, which carries 2.5 Gbps with the same signaling as SGMII at 2.5x clock rate. The chip info table already declares HSGMII as a supported interface mode for external interface 1.

Extend the SGMII configuration path to handle HSGMII, which phylink represents as 2500base-x:

- Use the HSGMII SerDes tuning parameters and external interface mode, and mux the SerDes to MAC8 in HSGMII mode. The parameters are again lifted from the GPL-licensed Realtek rtl8367c vendor driver.

- Advertise 2500base-x and MAC_2500FD on ports whose external interface supports HSGMII.

- Accept SPEED_2500 in the force mode configuration. The MAC speed field has no 2.5 Gbps value: the rate is determined by the HSGMII SerDes configuration, and the vendor driver programs the 1 Gbps value here, so do the same.

Tested on a Mercusys MR80X v2.20, where the RTL8367S is connected to the SoC over HSGMII.

---
 drivers/net/dsa/realtek/rtl8365mb.c | 61 +++++++++++++++++++++++------
 1 file changed, 48 insertions(+), 13 deletions(-)
drivers/net/dsa/realtek/rtl8365mb.c +48 -13
index 13ce8e6f1c72..fd8ec8bd6e55 100644
@@ -41,7 +41,7 @@
  * matter.
  *
  * NOTE: Currently, only the RGMII interface is implemented in this driver. On
- * the RTL8367S, the SGMII interface is also supported.
+ * the RTL8367S, the SGMII and HSGMII interfaces are also supported.
  *
  * The interrupt line is asserted on link UP/DOWN events. The driver creates a
  * custom irqchip to handle this interrupt and demultiplex the events by reading
@@ -521,6 +521,13 @@ static const struct rtl8365mb_jam_tbl_entry rtl8365mb_sds_jam_sgmii[] = {
 	{ 0x0424, 0xD810 }, { 0x002E, 0x83F2 },
 };
 
+/* HSGMII SerDes tuning parameters, lifted from the vendor driver sources */
+static const struct rtl8365mb_jam_tbl_entry rtl8365mb_sds_jam_hsgmii[] = {
+	{ 0x0500, 0x82F0 }, { 0x0501, 0xF195 }, { 0x0502, 0x31A2 },
+	{ 0x0503, 0x7960 }, { 0x0504, 0x9728 }, { 0x0423, 0x9D85 },
+	{ 0x0424, 0xD810 }, { 0x0001, 0x0F80 }, { 0x002E, 0x83F2 },
+};
+
 enum rtl8365mb_phy_interface_mode {
 	RTL8365MB_PHY_INTERFACE_MODE_INVAL = 0,
 	RTL8365MB_PHY_INTERFACE_MODE_INTERNAL = BIT(0),
@@ -1128,6 +1135,9 @@ static int rtl8365mb_ext_config_sgmii(struct realtek_priv *priv, int port,
 {
 	const struct rtl8365mb_extint *extint =
 		rtl8365mb_get_port_extint(priv, port);
+	const struct rtl8365mb_jam_tbl_entry *sds_jam;
+	size_t sds_jam_size;
+	u32 mode;
 	u16 val;
 	int ret;
 	int i;
@@ -1139,6 +1149,16 @@ static int rtl8365mb_ext_config_sgmii(struct realtek_priv *priv, int port,
 	if (extint->id != 1)
 		return -EOPNOTSUPP;
 
+	if (interface == PHY_INTERFACE_MODE_2500BASEX) {
+		sds_jam = rtl8365mb_sds_jam_hsgmii;
+		sds_jam_size = ARRAY_SIZE(rtl8365mb_sds_jam_hsgmii);
+		mode = RTL8365MB_EXT_PORT_MODE_HSGMII;
+	} else {
+		sds_jam = rtl8365mb_sds_jam_sgmii;
+		sds_jam_size = ARRAY_SIZE(rtl8365mb_sds_jam_sgmii);
+		mode = RTL8365MB_EXT_PORT_MODE_SGMII;
+	}
+
 	/* The vendor driver clears the line rate bypass for all interface
 	 * modes except TMII.
 	 */
@@ -1148,28 +1168,28 @@ static int rtl8365mb_ext_config_sgmii(struct realtek_priv *priv, int port,
 		return ret;
 
 	/* Tune the SerDes with vendor-prescribed parameters */
-	for (i = 0; i < ARRAY_SIZE(rtl8365mb_sds_jam_sgmii); i++) {
-		ret = rtl8365mb_sds_write(priv, 0,
-					  rtl8365mb_sds_jam_sgmii[i].reg,
-					  rtl8365mb_sds_jam_sgmii[i].val);
+	for (i = 0; i < sds_jam_size; i++) {
+		ret = rtl8365mb_sds_write(priv, 0, sds_jam[i].reg,
+					  sds_jam[i].val);
 		if (ret)
 			return ret;
 	}
 
-	/* Mux the SerDes to MAC8 in SGMII mode */
+	/* Mux the SerDes to MAC8 in the requested mode */
 	ret = regmap_update_bits(priv->map, RTL8365MB_SDS_MISC_REG,
 				 RTL8365MB_SDS_MISC_MAC8_SEL_SGMII_MASK |
 					 RTL8365MB_SDS_MISC_MAC8_SEL_HSGMII_MASK,
-				 RTL8365MB_SDS_MISC_MAC8_SEL_SGMII_MASK);
+				 mode == RTL8365MB_EXT_PORT_MODE_SGMII ?
+					 RTL8365MB_SDS_MISC_MAC8_SEL_SGMII_MASK :
+					 RTL8365MB_SDS_MISC_MAC8_SEL_HSGMII_MASK);
 	if (ret)
 		return ret;
 
 	ret = regmap_update_bits(
 		priv->map, RTL8365MB_DIGITAL_INTERFACE_SELECT_REG(extint->id),
 		RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_MASK(extint->id),
-		RTL8365MB_EXT_PORT_MODE_SGMII
-			<< RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_OFFSET(
-				   extint->id));
+		mode << RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_OFFSET(
+			       extint->id));
 	if (ret)
 		return ret;
 
@@ -1218,7 +1238,8 @@ static int rtl8365mb_ext_config_sgmii(struct realtek_priv *priv, int port,
 
 static bool rtl8365mb_interface_is_serdes(phy_interface_t interface)
 {
-	return interface == PHY_INTERFACE_MODE_SGMII;
+	return interface == PHY_INTERFACE_MODE_SGMII ||
+	       interface == PHY_INTERFACE_MODE_2500BASEX;
 }
 
 static int rtl8365mb_sds_config_forcemode(struct realtek_priv *priv, bool link,
@@ -1234,7 +1255,11 @@ static int rtl8365mb_sds_config_forcemode(struct realtek_priv *priv, bool link,
 	u32 r_speed;
 
 	if (link) {
-		if (speed == SPEED_1000) {
+		/* The speed field has no value for 2.5 Gbps: the rate is
+		 * determined by the HSGMII SerDes configuration, and the
+		 * vendor driver programs the 1 Gbps value here.
+		 */
+		if (speed == SPEED_2500 || speed == SPEED_1000) {
 			r_speed = RTL8365MB_PORT_SPEED_1000M;
 		} else if (speed == SPEED_100) {
 			r_speed = RTL8365MB_PORT_SPEED_100M;
@@ -1286,7 +1311,11 @@ static int rtl8365mb_ext_config_forcemode(struct realtek_priv *priv, int port,
 		r_rx_pause = rx_pause ? 1 : 0;
 		r_tx_pause = tx_pause ? 1 : 0;
 
-		if (speed == SPEED_1000) {
+		/* The speed field has no value for 2.5 Gbps: the rate is
+		 * determined by the HSGMII SerDes configuration, and the
+		 * vendor driver programs the 1 Gbps value here.
+		 */
+		if (speed == SPEED_2500 || speed == SPEED_1000) {
 			r_speed = RTL8365MB_PORT_SPEED_1000M;
 		} else if (speed == SPEED_100) {
 			r_speed = RTL8365MB_PORT_SPEED_100M;
@@ -1366,6 +1395,12 @@ static void rtl8365mb_phylink_get_caps(struct dsa_switch *ds, int port,
 	if (extint->supported_interfaces & RTL8365MB_PHY_INTERFACE_MODE_SGMII)
 		__set_bit(PHY_INTERFACE_MODE_SGMII,
 			  config->supported_interfaces);
+
+	if (extint->supported_interfaces & RTL8365MB_PHY_INTERFACE_MODE_HSGMII) {
+		__set_bit(PHY_INTERFACE_MODE_2500BASEX,
+			  config->supported_interfaces);
+		config->mac_capabilities |= MAC_2500FD;
+	}
 }
 
 static void rtl8365mb_phylink_mac_config(struct phylink_config *config,
Signature · 1 line
2.54.0