[][kernel][mt7988][eth][Refactor SER triggering condition for the XGDM to GDM dynamically change]

[Description]
Refactor SER triggering condition for the XGDM to GDM dynamically change.

When connecting the 10G SFP+ RJ-45 module, the phylink will call
mtk_mac_config() twice. We added a flag to bypass the second one, but
this flag may cause 1G SFP optical module cannot trigger SER.
Therefore, we change the triggering condition to make sure every type
1G SFP module can trigger SER.

If without this patch, the SER might not be able to trigger whenever
switching to the 1G SFP optical module from the 10G module.

[Release-log]
N/A


Change-Id: Ie992a2f23c4850a12b959342910ec977c776d1be
Reviewed-on: https://gerrit.mediatek.inc/c/openwrt/feeds/mtk_openwrt_feeds/+/7468581
diff --git a/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_eth_dbg.h b/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_eth_dbg.h
index b70fc72..ec7167b 100755
--- a/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_eth_dbg.h
+++ b/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_eth_dbg.h
@@ -28,6 +28,7 @@
 #define MTK_FE_CDM6_FSM			0x328
 #define MTK_FE_GDM1_FSM			0x228
 #define MTK_FE_GDM2_FSM			0x22C
+#define MTK_FE_GDM3_FSM			0x23C
 #define MTK_FE_PSE_FREE			0x240
 #define MTK_FE_DROP_FQ			0x244
 #define MTK_FE_DROP_FC			0x248
diff --git a/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_eth_soc.c b/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_eth_soc.c
index 55307ff..9d62226 100755
--- a/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_eth_soc.c
+++ b/target/linux/mediatek/files-5.4/drivers/net/ethernet/mediatek/mtk_eth_soc.c
@@ -40,7 +40,6 @@
 static int mtk_msg_level = -1;
 atomic_t reset_lock = ATOMIC_INIT(0);
 atomic_t force = ATOMIC_INIT(0);
-atomic_t reset_pending = ATOMIC_INIT(0);
 
 module_param_named(msg_level, mtk_msg_level, int, 0);
 MODULE_PARM_DESC(msg_level, "Message level (-1=defaults,0=none,...,16=all)");
@@ -462,6 +461,28 @@
 	mtk_w32(eth, val, MTK_GSW_CFG);
 }
 
+static bool mtk_check_gmac23_idle(struct mtk_mac *mac)
+{
+	u32 mac_fsm, gdm_fsm;
+
+	mac_fsm = mtk_r32(mac->hw, MTK_MAC_FSM(mac->id));
+
+	switch (mac->id) {
+	case MTK_GMAC2_ID:
+		gdm_fsm = mtk_r32(mac->hw, MTK_FE_GDM2_FSM);
+		break;
+	case MTK_GMAC3_ID:
+		gdm_fsm = mtk_r32(mac->hw, MTK_FE_GDM3_FSM);
+		break;
+	};
+
+	if ((mac_fsm & 0xFFFF0000) == 0x01010000 &&
+	    (gdm_fsm & 0xFFFF0000) == 0x00000000)
+		return true;
+
+	return false;
+}
+
 static void mtk_setup_eee(struct mtk_mac *mac, bool enable)
 {
 	struct mtk_eth *eth = mac->hw;
@@ -765,13 +786,11 @@
 		 * when swtiching XGDM to GDM. Therefore, here trigger an SER
 		 * to let GDM go back to the initial state.
 		 */
-		if (mac->type != mac_type) {
-			if (atomic_read(&reset_pending) == 0) {
+		if (mac->type != mac_type && !mtk_check_gmac23_idle(mac)) {
+			if (!test_bit(MTK_RESETTING, &mac->hw->state)) {
 				atomic_inc(&force);
 				schedule_work(&eth->pending_work);
-				atomic_inc(&reset_pending);
-			} else
-				atomic_dec(&reset_pending);
+			}
 		}
 	}