From 455f35345b66d475400cbba582703c17dee49cd6 Mon Sep 17 00:00:00 2001
From: Ishan Deshpande <IshanShirish.Deshpande@infineon.com>
Date: Wed, 13 Sep 2023 00:40:46 -0500
Subject: [PATCH 231/296] brcmfmac: Change bus width independent of
 sdio_idleclk

1) In the existing implementation, sdio bus width was changed to 1-bit
   mode before bus goes to sleep (kso=0) only if idleclk is enabled
   using module param. Similarly bus width was changed back to 4-bit mode
   on kso=1 (only on clock gating enabled)
2) This bus width change should be independent of idleclk as kso and bus
   width change are atomic operations
3) Fix is to remove the dependency of idleclk for bus width change, it
   should change to 1-bit mode just before every kso=0 and change to 4-bit
   mode just after every kso=1


Signed-off-by: Ishan Deshpande <IshanShirish.Deshpande@infineon.com>
---
 .../broadcom/brcm80211/brcmfmac/sdio.c        | 20 +++++++++----------
 .../broadcom/brcm80211/brcmfmac/sdio.h        |  2 ++
 2 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
index afb48625b4b9..2be1fc7fe9e2 100644
--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
+++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
@@ -772,6 +772,10 @@ brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
 	/* Start time of kso_sequence */
 	start_jiffy = jiffies;
 
+	/* Change bus width to 1-bit mode before kso 0 */
+	if (!on)
+		brcmf_sdio_set_sdbus_clk_width(bus, SDIO_SDMODE_1BIT);
+
 	/* 1st KSO write goes to AOS wake up core if device is asleep  */
 	brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, wr_val, &err);
 
@@ -859,9 +863,11 @@ brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
 		brcmf_dbg(SDIO, "INFO: KSO=%d try_cnt=%d err_cnt=%d kso_seq_time=%uus rd_val=0x%x err=%d\n",
 			  on, try_cnt, err_cnt, kso_loop_time, rd_val, err);
 
-	if (on)
+	if (on) {
+		/* Change the bus width to 4-bit mode on kso 1 */
+		brcmf_sdio_set_sdbus_clk_width(bus, SDIO_SDMODE_4BIT);
 		sdio_retune_release(bus->sdiodev->func1);
-
+	}
 	sdio_retune_crc_enable(bus->sdiodev->func1);
 
 	return err;
@@ -1056,7 +1062,7 @@ static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
 }
 
 /* Transition SD and backplane clock readiness */
-static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
+int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
 {
 #ifdef DEBUG
 	uint oldstate = bus->clkstate;
@@ -1198,10 +1204,6 @@ brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
 						   SBSDIO_ALP_AVAIL_REQ, &err);
 			}
 
-			if (bus->idleclock == BRCMF_IDLE_STOP)
-				brcmf_sdio_set_sdbus_clk_width(bus,
-							       SDIO_SDMODE_1BIT);
-
 			err = brcmf_sdio_kso_control(bus, false);
 
 			if (bus->idleclock == BRCMF_IDLE_STOP)
@@ -1211,10 +1213,6 @@ brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
 				brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
 
 			err = brcmf_sdio_kso_control(bus, true);
-
-			if (bus->idleclock == BRCMF_IDLE_STOP)
-				brcmf_sdio_set_sdbus_clk_width(bus,
-							       SDIO_SDMODE_4BIT);
 		}
 		if (err) {
 			brcmf_err("error while changing bus sleep state %d\n",
diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.h b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.h
index aad18306f8d6..d9e0d156365b 100644
--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.h
+++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.h
@@ -451,6 +451,8 @@ void brcmf_sdio_isr(struct brcmf_sdio *bus, bool in_isr);
 void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, bool active);
 void brcmf_sdio_wowl_config(struct device *dev, bool enabled);
 int brcmf_sdio_sleep(struct brcmf_sdio *bus, bool sleep);
+int brcmf_sdio_set_sdbus_clk_width(struct brcmf_sdio *bus, unsigned int flags);
+int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok);
 void brcmf_sdio_trigger_dpc(struct brcmf_sdio *bus);
 u32 brcmf_sdio_get_enum_addr(struct brcmf_sdio *bus);
 
-- 
2.25.1

