From 90ca354228715df2b178839b3800eca2515d19dd Mon Sep 17 00:00:00 2001
From: avishad verma <avishad.verma@infineon.com>
Date: Thu, 22 May 2025 02:46:38 -0500
Subject: [PATCH 3/4] brcmfmac: Ignore Bus sleep changes in KSO sequence.

For DDR50 mode one additional CMD53 read is done in KSO sequence.
The purpose of ignore bus flag is to avoid printing error logs, during
KSO sequence. If we keep the ignore bus sleep flag in sdio sleep state
check function it will introduce some corner case where sdio CMD might
go through when KSO is disabled.

Fixes SWWLAN-153671

Signed-off-by: avishad verma <avishad.verma@infineon.com>
---
 .../broadcom/brcm80211/brcmfmac/bcmsdh.c      | 20 ++++++++++++-------
 .../broadcom/brcm80211/brcmfmac/sdio.c        | 12 +++++------
 .../broadcom/brcm80211/brcmfmac/sdio.h        |  1 +
 3 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/bcmsdh.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/bcmsdh.c
index f6ab73089456..2dc0a97d3c55 100644
--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/bcmsdh.c
+++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/bcmsdh.c
@@ -235,7 +235,8 @@ static int brcmf_sdiod_set_backplane_window(struct brcmf_sdio_dev *sdiodev,
 	int err = 0, i;
 
 	if (brcmf_sdio_bus_sleep_state(sdiodev->bus)) {
-		brcmf_err("ERROR: Write operation when bus is in sleep state\n");
+		if (!sdiodev->ignore_bus_error)
+			brcmf_err("ERROR: Write operation when bus is in sleep state\n");
 	}
 
 	if (sdiodev->sbwad_valid && (bar0 == sdiodev->sbwad))
@@ -261,7 +262,8 @@ u32 brcmf_sdiod_readl(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret)
 	int retval;
 
 	if (brcmf_sdio_bus_sleep_state(sdiodev->bus)) {
-		brcmf_err("ERROR: Read operation when bus is in sleep state\n");
+		if (!sdiodev->ignore_bus_error)
+			brcmf_err("ERROR: Read operation when bus is in sleep state\n");
 	}
 
 	retval = brcmf_sdiod_set_backplane_window(sdiodev, addr);
@@ -289,7 +291,8 @@ void brcmf_sdiod_writel(struct brcmf_sdio_dev *sdiodev, u32 addr,
 	int retval;
 
 	if (brcmf_sdio_bus_sleep_state(sdiodev->bus)) {
-		brcmf_err("ERROR: Write operation when bus is in sleep state\n");
+		if (!sdiodev->ignore_bus_error)
+			brcmf_err("ERROR: Write operation when bus is in sleep state\n");
 	}
 
 	retval = brcmf_sdiod_set_backplane_window(sdiodev, addr);
@@ -316,7 +319,8 @@ static int brcmf_sdiod_skbuff_read(struct brcmf_sdio_dev *sdiodev,
 	int err;
 
 	if (brcmf_sdio_bus_sleep_state(sdiodev->bus)) {
-		brcmf_err("ERROR: Read operation when bus is in sleep state\n");
+		if (!sdiodev->ignore_bus_error)
+			brcmf_err("ERROR: Read operation when bus is in sleep state\n");
 	}
 
 	/* Single skb use the standard mmc interface */
@@ -355,7 +359,8 @@ static int brcmf_sdiod_skbuff_write(struct brcmf_sdio_dev *sdiodev,
 	int err;
 
 	if (brcmf_sdio_bus_sleep_state(sdiodev->bus)) {
-		brcmf_err("ERROR: Write operation when bus is in sleep state\n");
+		if (!sdiodev->ignore_bus_error)
+			brcmf_err("ERROR: Write operation when bus is in sleep state\n");
 	}
 
 	/* Single skb use the standard mmc interface */
@@ -384,8 +389,9 @@ static int mmc_submit_one(struct mmc_data *md, struct mmc_request *mr,
 	int ret;
 
 	if (brcmf_sdio_bus_sleep_state(sdiodev->bus)) {
-		brcmf_err("ERROR: %s operation when bus is in sleep state\n",
-			  write ? "Write" : "Read");
+		if (!sdiodev->ignore_bus_error)
+			brcmf_err("ERROR: %s operation when bus is in sleep state\n",
+				  write ? "Write" : "Read");
 	}
 
 	md->sg_len = sg_cnt;
diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
index ca2ea3fe5cd5..411d04c644fc 100644
--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
+++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
@@ -571,7 +571,6 @@ struct brcmf_sdio {
 	struct task_ctl	thr_rxf_ctl;
 	spinlock_t rxf_lock;	/* lock for rxf idx protection */
 	bool h1_ddr50_mode;	/* H1 DDR50 Mode enabled*/
-	bool ignore_bus_error;	/* Ignore SDIO Bus access error*/
 };
 
 /* clkstate */
@@ -769,9 +768,6 @@ brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
 	/* Change bus width to 1-bit mode before kso 0 */
 	if (!on && bus->idleclock == BRCMF_IDLE_STOP)
 		brcmf_sdio_set_sdbus_clk_width(bus, SDIO_SDMODE_1BIT);
-	else
-	/* Set Flag to ignore SDIO Bus access error during KSO */
-		bus->ignore_bus_error = true;
 
 	/* 1st KSO write goes to AOS wake up core if device is asleep  */
 	brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, wr_val, &err);
@@ -856,14 +852,16 @@ brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
 		struct brcmf_sdio_dev *sdiod = bus->sdiodev;
 		u32 ret, chipid;
 
+		/* Set Flag to ignore SDIO Bus access error during KSO */
+		sdiod->ignore_bus_error = true;
 		chipid = brcmf_sdiod_readl(sdiod,
 					   bus->ci->ccsec->bus_corebase + SD_REG(chipid),
 					   &ret);
+		/* Clear Flag to ignore SDIO Bus access error during KSO */
+		sdiod->ignore_bus_error = false;
 		brcmf_dbg(SDIO, "chipid: 0x%x ret = 0x%x\n", chipid, ret);
 	}
 
-	/* Clear Flag to ignore SDIO Bus access error during KSO */
-	bus->ignore_bus_error = false;
 	sdio_retune_release(bus->sdiodev->func1);
 
 	if (kso_loop_time > KSO_MAX_SEQ_TIME_NS)
@@ -1247,7 +1245,7 @@ brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
 
 bool brcmf_sdio_bus_sleep_state(struct brcmf_sdio *bus)
 {
-	return bus->sleeping && !bus->ignore_bus_error;
+	return bus->sleeping;
 }
 
 #ifdef DEBUG
diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.h b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.h
index cde1b358eb03..59ee8d60f0e1 100644
--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.h
+++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.h
@@ -267,6 +267,7 @@ struct brcmf_sdio_dev {
 	struct brcmf_ulp fmac_ulp;
 	bool ulp;
 	bool redownload_fw;
+	bool ignore_bus_error;	/* Ignore SDIO Bus access error*/
 };
 
 /* sdio core registers */
-- 
2.25.0

