From e3dd6804fa642a733b7a6932d60bb83b6363555c Mon Sep 17 00:00:00 2001
From: Sam Shih <sam.shih@mediatek.com>
Date: Fri, 2 Jun 2023 13:05:59 +0800
Subject: [PATCH] 
 [backport-networking-drivers][999-1701-add-default-setting-to-dsa-unused-port.patch]

---
 drivers/net/dsa/mt7530.c | 62 ++++++++++++++++++++++++++++++++++++++--
 drivers/net/dsa/mt7530.h |  1 +
 2 files changed, 60 insertions(+), 3 deletions(-)

diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
index 8ce3d51e8..008432d5c 100644
--- a/drivers/net/dsa/mt7530.c
+++ b/drivers/net/dsa/mt7530.c
@@ -1022,6 +1022,9 @@ mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
 	struct mt7530_priv *priv = ds->priv;
 	u32 stp_state;
 
+	if (dsa_is_unused_port(ds, port))
+		return;
+
 	switch (state) {
 	case BR_STATE_DISABLED:
 		stp_state = MT7530_STP_DISABLED;
@@ -1674,11 +1677,59 @@ mt7530_setup(struct dsa_switch *ds)
 	return 0;
 }
 
+static int
+setup_unused_ports(struct dsa_switch *ds, u32 pm)
+{
+	struct mt7530_priv *priv = ds->priv;
+	u32 egtag_mask = 0;
+	u32 egtag_val = 0;
+	int i;
+
+	if (!pm)
+		return 0;
+
+	for (i = 0; i < MT7530_NUM_PORTS; i++) {
+		if (!dsa_is_unused_port(ds, i))
+			continue;
+
+		/* Setup MAC port with maximum capability. */
+		if ((i == 5) || (i == 6))
+			if (priv->info->cpu_port_config)
+				priv->info->cpu_port_config(ds, i);
+
+		mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK | PCR_PORT_VLAN_MASK,
+			   PCR_MATRIX(pm) | MT7530_PORT_SECURITY_MODE);
+		egtag_mask |= ETAG_CTRL_P_MASK(i);
+		egtag_val |=  ETAG_CTRL_P(i, MT7530_VLAN_EGRESS_UNTAG);
+	}
+
+	/* Add unused ports to VLAN2 group for using IVL fdb. */
+	mt7530_write(priv, MT7530_VAWD1,
+		     IVL_MAC | VTAG_EN | PORT_MEM(pm) | VLAN_VALID);
+	mt7530_rmw(priv, MT7530_VAWD2, egtag_mask, egtag_val);
+	mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, MT753X_RESERVED_VLAN);
+
+	for (i = 0; i < MT7530_NUM_PORTS; i++) {
+		if (!dsa_is_unused_port(ds, i))
+			continue;
+
+		mt7530_rmw(priv, MT7530_PPBV1_P(i), G0_PORT_VID_MASK,
+			   G0_PORT_VID(MT753X_RESERVED_VLAN));
+		mt7530_rmw(priv, MT7530_SSP_P(i), FID_PST_MASK, MT7530_STP_FORWARDING);
+
+		dev_dbg(ds->dev, "Add unused port%d to reserved VLAN%d group\n",
+			i, MT753X_RESERVED_VLAN);
+	}
+
+	return 0;
+}
+
 static int
 mt7531_setup(struct dsa_switch *ds)
 {
 	struct mt7530_priv *priv = ds->priv;
 	struct mt7530_dummy_poll p;
+	u32 unused_pm = 0;
 	u32 val, id;
 	int ret, i;
 
@@ -1766,7 +1817,9 @@ mt7531_setup(struct dsa_switch *ds)
 
 		mt7530_set(priv, MT7531_DBG_CNT(i), MT7531_DIS_CLR);
 
-		if (dsa_is_cpu_port(ds, i))
+		if (dsa_is_unused_port(ds, i))
+			unused_pm |= BIT(i);
+		else if (dsa_is_cpu_port(ds, i))
 			mt753x_cpu_port_enable(ds, i);
 		else
 			mt7530_port_disable(ds, i);
@@ -1776,6 +1829,9 @@ mt7531_setup(struct dsa_switch *ds)
 			   PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT));
 	}
 
+	/* Group and enable unused ports as a standalone dumb switch. */
+	setup_unused_ports(ds, unused_pm);
+
 	ds->configure_vlan_while_not_filtering = true;
 
 	/* Flush the FDB table */
@@ -2100,7 +2156,7 @@ mt7531_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
 	case PHY_INTERFACE_MODE_RGMII_RXID:
 	case PHY_INTERFACE_MODE_RGMII_TXID:
 		dp = dsa_to_port(ds, port);
-		phydev = dp->slave->phydev;
+		phydev = (dp->slave) ? dp->slave->phydev : NULL;
 		return mt7531_rgmii_setup(priv, port, interface, phydev);
 	case PHY_INTERFACE_MODE_SGMII:
 		return mt7531_sgmii_setup_mode_an(priv, port, interface);
@@ -2640,7 +2696,7 @@ mt7530_probe(struct mdio_device *mdiodev)
 	if (!priv)
 		return -ENOMEM;
 
-	priv->ds = dsa_switch_alloc(&mdiodev->dev, DSA_MAX_PORTS);
+	priv->ds = dsa_switch_alloc(&mdiodev->dev, MT7530_NUM_PORTS);
 	if (!priv->ds)
 		return -ENOMEM;
 
diff --git a/drivers/net/dsa/mt7530.h b/drivers/net/dsa/mt7530.h
index 6424bc90e..aa758b2d2 100644
--- a/drivers/net/dsa/mt7530.h
+++ b/drivers/net/dsa/mt7530.h
@@ -10,6 +10,7 @@
 #define MT7530_CPU_PORT			6
 #define MT7530_NUM_FDB_RECORDS		2048
 #define MT7530_ALL_MEMBERS		0xff
+#define MT753X_RESERVED_VLAN		2
 
 enum mt753x_id {
 	ID_MT7530 = 0,
-- 
2.34.1

