From f438c61bec0eecdee424dfae35f4f9e48105ca01 Mon Sep 17 00:00:00 2001
From: Vignesh Raghavendra <vigneshr@ti.com>
Date: Thu, 14 Oct 2021 14:14:26 +0530
Subject: [PATCH] net: ethernet: ti: icssg_prueth: Zero shared buffers before
 starting up

Zero out shared MSMC buffers and PRU shared memories before starting up
interfaces. This is required for clean switch b/w Dual EMAC and Switch
mode.
Clear all buffers at once when first interface is starting up. This
requires to keep track of number of MAC ports initialized, hence
introduce prueth->emacs_initialized flag to keep track of this, drop
prueth->iep_initialized flag as its redundant.

Signed-off-by: Vignesh Raghavendra <vigneshr@ti.com>
---
 drivers/net/ethernet/ti/icssg_prueth.c | 20 ++++++++++++--------
 drivers/net/ethernet/ti/icssg_prueth.h |  3 ++-
 2 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/drivers/net/ethernet/ti/icssg_prueth.c b/drivers/net/ethernet/ti/icssg_prueth.c
index eed91038bde2..715f9f7822d3 100644
--- a/drivers/net/ethernet/ti/icssg_prueth.c
+++ b/drivers/net/ethernet/ti/icssg_prueth.c
@@ -1547,10 +1547,13 @@ static int emac_ndo_open(struct net_device *ndev)
 	int max_rx_flows;
 	int rx_flow;
 
-	/* clear SMEM of this slice */
+	/* clear SMEM and MSMC settings for all slices */
+	if (!prueth->emacs_initialized) {
+		memset_io(prueth->msmcram.va, 0, prueth->msmcram.size);
+		memset_io(prueth->shram.va, 0, ICSSG_CONFIG_OFFSET_SLICE1 * PRUETH_NUM_MACS);
+	}
+
 	if (emac->is_sr1) {
-		memset_io(prueth->shram.va + slice * ICSSG_CONFIG_OFFSET_SLICE1,
-			  0, ICSSG_CONFIG_OFFSET_SLICE1);
 		/* For SR1, high priority channel is used exclusively for
 		 * management messages. Do reduce number of data channels.
 		 */
@@ -1643,11 +1646,10 @@ static int emac_ndo_open(struct net_device *ndev)
 	if (ret)
 		goto free_rx_mgmt_ts_irq;
 
-	if (!emac->is_sr1 && !prueth->iep_initialized) {
+	if (!emac->is_sr1 && !prueth->emacs_initialized) {
 		ret = icss_iep_init(emac->iep, &prueth_iep_clockops,
 				    emac, IEP_DEFAULT_CYCLE_TIME_NS);
 	}
-	prueth->iep_initialized++;
 
 	if (!emac->is_sr1) {
 		ret = request_threaded_irq(emac->tx_ts_irq, NULL, prueth_tx_ts_irq,
@@ -1692,6 +1694,8 @@ static int emac_ndo_open(struct net_device *ndev)
 	/* start PHY */
 	phy_start(emac->phydev);
 
+	prueth->emacs_initialized++;
+
 	if (netif_msg_drv(emac))
 		dev_notice(&ndev->dev, "started\n");
 
@@ -1793,11 +1797,9 @@ static int emac_ndo_stop(struct net_device *ndev)
 
 	napi_disable(&emac->napi_rx);
 
-	if (!emac->is_sr1 && prueth->iep_initialized == 1)
+	if (!emac->is_sr1 && prueth->emacs_initialized == 1)
 		icss_iep_exit(emac->iep);
 
-	prueth->iep_initialized--;
-
 	cancel_work_sync(&emac->rx_mode_work);
 	/* stop PRUs */
 	prueth_emac_stop(emac);
@@ -1821,6 +1823,8 @@ static int emac_ndo_stop(struct net_device *ndev)
 	prueth_cleanup_rx_chns(emac, &emac->rx_chns, max_rx_flows);
 	prueth_cleanup_tx_chns(emac);
 
+	prueth->emacs_initialized--;
+
 	if (netif_msg_drv(emac))
 		dev_notice(&ndev->dev, "stopped\n");
 
diff --git a/drivers/net/ethernet/ti/icssg_prueth.h b/drivers/net/ethernet/ti/icssg_prueth.h
index 94490ecd920d..12db0a91d4c5 100644
--- a/drivers/net/ethernet/ti/icssg_prueth.h
+++ b/drivers/net/ethernet/ti/icssg_prueth.h
@@ -229,10 +229,11 @@ struct prueth {
 	struct platform_device *pdev;
 	struct icss_iep *iep0;
 	struct icss_iep *iep1;
-	int iep_initialized;
 	struct prueth_pdata pdata;
 	struct prueth_vlan_tbl *vlan_tbl;
 	u8 icssg_hwcmdseq;
+
+	int emacs_initialized;
 };
 
 struct emac_tx_ts_response_sr1 {
