[PATCH 14/22] mci: sdhci: Add common wait for idle function

Sascha Hauer s.hauer at pengutronix.de
Thu Aug 3 03:49:55 PDT 2023


Waiting for the controller being idle is a common pattern in the
different SDHCI drivers we have. Add a common function for it.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 drivers/mci/arasan-sdhci.c           | 13 ++-----------
 drivers/mci/atmel-sdhci-common.c     | 25 ++++++-------------------
 drivers/mci/dove-sdhci.c             | 23 ++++-------------------
 drivers/mci/mci-bcm2835.c            | 13 +++----------
 drivers/mci/rockchip-dwcmshc-sdhci.c | 16 +++-------------
 drivers/mci/sdhci.c                  | 22 ++++++++++++++++++++++
 drivers/mci/sdhci.h                  |  1 +
 7 files changed, 41 insertions(+), 72 deletions(-)

diff --git a/drivers/mci/arasan-sdhci.c b/drivers/mci/arasan-sdhci.c
index 71e974c384..bf89f74336 100644
--- a/drivers/mci/arasan-sdhci.c
+++ b/drivers/mci/arasan-sdhci.c
@@ -149,18 +149,9 @@ static int arasan_sdhci_send_cmd(struct mci_host *mci, struct mci_cmd *cmd,
 	u32 mask, command, xfer;
 	int ret;
 
-	/* Wait for idle before next command */
-	mask = SDHCI_CMD_INHIBIT_CMD;
-	if (cmd->cmdidx != MMC_CMD_STOP_TRANSMISSION)
-		mask |= SDHCI_CMD_INHIBIT_DATA;
-
-	ret = wait_on_timeout(10 * MSECOND,
-			      !(sdhci_read32(&host->sdhci, SDHCI_PRESENT_STATE) & mask));
-	if (ret) {
-		dev_err(host->mci.hw_dev,
-			"SDHCI timeout while waiting for idle\n");
+	ret = sdhci_wait_idle(&host->sdhci, cmd);
+	if (ret)
 		return ret;
-	}
 
 	sdhci_write32(&host->sdhci, SDHCI_INT_STATUS, ~0);
 
diff --git a/drivers/mci/atmel-sdhci-common.c b/drivers/mci/atmel-sdhci-common.c
index 58ba0b9b3d..171737ab01 100644
--- a/drivers/mci/atmel-sdhci-common.c
+++ b/drivers/mci/atmel-sdhci-common.c
@@ -94,21 +94,13 @@ int at91_sdhci_send_command(struct at91_sdhci *host, struct mci_cmd *cmd,
 {
 	unsigned command, xfer;
 	struct sdhci *sdhci = &host->sdhci;
-	u32 mask, state;
+	u32 mask;
 	int status;
 	int ret;
 
-	/* Wait for idle before next command */
-	mask = SDHCI_CMD_INHIBIT_CMD;
-	if (cmd->cmdidx != MMC_CMD_STOP_TRANSMISSION)
-		mask |= SDHCI_CMD_INHIBIT_DATA;
-
-	ret = sdhci_read32_poll_timeout(sdhci, SDHCI_PRESENT_STATE, state,
-					!(state & mask), 100 * USEC_PER_MSEC);
-	if (ret) {
-		dev_err(host->dev, "timeout while waiting for idle\n");
+	ret = sdhci_wait_idle(&host->sdhci, cmd);
+	if (ret)
 		return ret;
-	}
 
 	sdhci_write32(sdhci, SDHCI_INT_STATUS, ~0U);
 
@@ -193,17 +185,12 @@ static int at91_sdhci_set_clock(struct at91_sdhci *host, unsigned clock)
 	struct sdhci *sdhci = &host->sdhci;
 	unsigned clk = 0, clk_div;
 	unsigned reg;
-	u32 present_mask, caps, caps_clk_mult;
+	u32 caps, caps_clk_mult;
 	int ret;
 
-	present_mask = SDHCI_CMD_INHIBIT_CMD | SDHCI_CMD_INHIBIT_DATA;
-	ret = sdhci_read32_poll_timeout(sdhci, SDHCI_PRESENT_STATE, reg,
-					!(reg & present_mask),
-					100 * USEC_PER_MSEC);
-	if (ret) {
-		dev_warn(host->dev, "Timeout waiting for CMD and DAT Inhibit bits\n");
+	ret = sdhci_wait_idle(&host->sdhci, NULL);
+	if (ret)
 		return ret;
-	}
 
 	sdhci_write16(sdhci, SDHCI_CLOCK_CONTROL, 0);
 
diff --git a/drivers/mci/dove-sdhci.c b/drivers/mci/dove-sdhci.c
index 5954058e6b..8a4c4a11b4 100644
--- a/drivers/mci/dove-sdhci.c
+++ b/drivers/mci/dove-sdhci.c
@@ -57,31 +57,16 @@ static int dove_sdhci_wait_for_done(struct dove_sdhci *host, u16 mask)
 static int dove_sdhci_mci_send_cmd(struct mci_host *mci, struct mci_cmd *cmd,
 				struct mci_data *data)
 {
-	u16 val;
 	u32 command, xfer;
-	u64 start;
 	int ret;
 	unsigned int num_bytes = 0;
 	struct dove_sdhci *host = priv_from_mci_host(mci);
 
-	sdhci_write32(&host->sdhci, SDHCI_INT_STATUS, ~0);
-
-	/* Do not wait for CMD_INHIBIT_DAT on stop commands */
-	if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
-		val = SDHCI_CMD_INHIBIT_CMD;
-	else
-		val = SDHCI_CMD_INHIBIT_CMD | SDHCI_CMD_INHIBIT_DATA;
+	ret = sdhci_wait_idle(&host->sdhci, cmd);
+	if (ret)
+		return ret;
 
-	/* Wait for bus idle */
-	start = get_time_ns();
-	while (1) {
-		if (!(sdhci_read16(&host->sdhci, SDHCI_PRESENT_STATE) & val))
-			break;
-		if (is_timeout(start, 10 * MSECOND)) {
-			dev_err(host->mci.hw_dev, "SDHCI timeout while waiting for idle\n");
-			return -ETIMEDOUT;
-		}
-	}
+	sdhci_write32(&host->sdhci, SDHCI_INT_STATUS, ~0);
 
 	/* setup transfer data */
 	if (data) {
diff --git a/drivers/mci/mci-bcm2835.c b/drivers/mci/mci-bcm2835.c
index 92f4498ea5..2b541e4c05 100644
--- a/drivers/mci/mci-bcm2835.c
+++ b/drivers/mci/mci-bcm2835.c
@@ -118,7 +118,6 @@ static int bcm2835_mci_request(struct mci_host *mci, struct mci_cmd *cmd,
 		struct mci_data *data) {
 	u32 command, block_data = 0, transfer_mode = 0;
 	int ret;
-	u32 wait_inhibit_mask = SDHCI_CMD_INHIBIT_CMD | SDHCI_CMD_INHIBIT_DATA;
 	struct bcm2835_mci_host *host = to_bcm2835_host(mci);
 
 	sdhci_set_cmd_xfer_mode(&host->sdhci, cmd, data, false,
@@ -129,15 +128,9 @@ static int bcm2835_mci_request(struct mci_host *mci, struct mci_cmd *cmd,
 		block_data |= data->blocksize;
 	}
 
-	/* We shouldn't wait for data inihibit for stop commands, even
-	though they might use busy signaling */
-	if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
-		wait_inhibit_mask = SDHCI_CMD_INHIBIT_CMD;
-
-	/*Wait for old command*/
-	while (sdhci_read32(&host->sdhci, SDHCI_PRESENT_STATE)
-			& wait_inhibit_mask)
-		;
+	ret = sdhci_wait_idle(&host->sdhci, cmd);
+	if (ret)
+		return ret;
 
 	sdhci_write32(&host->sdhci, SDHCI_INT_ENABLE, 0xFFFFFFFF);
 	sdhci_write32(&host->sdhci, SDHCI_INT_STATUS, 0xFFFFFFFF);
diff --git a/drivers/mci/rockchip-dwcmshc-sdhci.c b/drivers/mci/rockchip-dwcmshc-sdhci.c
index 41edeb158b..03f463a3f6 100644
--- a/drivers/mci/rockchip-dwcmshc-sdhci.c
+++ b/drivers/mci/rockchip-dwcmshc-sdhci.c
@@ -245,23 +245,13 @@ static int rk_sdhci_send_cmd(struct mci_host *mci, struct mci_cmd *cmd,
 				 struct mci_data *data)
 {
 	struct rk_sdhci_host *host = to_rk_sdhci_host(mci);
-	u32 mask, command, xfer;
+	u32 command, xfer;
 	int ret;
 	dma_addr_t dma;
 
-	/* Wait for idle before next command */
-	mask = SDHCI_CMD_INHIBIT_CMD;
-	if (cmd->cmdidx != MMC_CMD_STOP_TRANSMISSION)
-		mask |= SDHCI_CMD_INHIBIT_DATA;
-
-	ret = wait_on_timeout(10 * MSECOND,
-			!(sdhci_read32(&host->sdhci, SDHCI_PRESENT_STATE) & mask));
-
-	if (ret) {
-		dev_err(host->mci.hw_dev,
-				"SDHCI timeout while waiting for idle\n");
+	ret = sdhci_wait_idle(&host->sdhci, cmd);
+	if (ret)
 		return ret;
-	}
 
 	sdhci_write32(&host->sdhci, SDHCI_INT_STATUS, ~0);
 
diff --git a/drivers/mci/sdhci.c b/drivers/mci/sdhci.c
index 635884e2a2..8c543c1b5b 100644
--- a/drivers/mci/sdhci.c
+++ b/drivers/mci/sdhci.c
@@ -463,6 +463,28 @@ void sdhci_enable_clk(struct sdhci *host, u16 clk)
 	sdhci_write16(host, SDHCI_CLOCK_CONTROL, clk);
 }
 
+int sdhci_wait_idle(struct sdhci *host, struct mci_cmd *cmd)
+{
+	u32 mask;
+	int ret;
+
+	mask = SDHCI_CMD_INHIBIT_CMD | SDHCI_CMD_INHIBIT_DATA;
+
+	if (cmd && cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
+		mask &= ~SDHCI_CMD_INHIBIT_DATA;
+
+	ret = wait_on_timeout(10 * MSECOND,
+			!(sdhci_read32(host, SDHCI_PRESENT_STATE) & mask));
+
+	if (ret) {
+		dev_err(host->mci->hw_dev,
+				"SDHCI timeout while waiting for idle\n");
+		return -EBUSY;
+	}
+
+	return 0;
+}
+
 void sdhci_set_clock(struct sdhci *host, unsigned int clock, unsigned int input_clock)
 {
 	u16 clk;
diff --git a/drivers/mci/sdhci.h b/drivers/mci/sdhci.h
index fe8c25cb9c..c8e7145d07 100644
--- a/drivers/mci/sdhci.h
+++ b/drivers/mci/sdhci.h
@@ -257,6 +257,7 @@ static inline void sdhci_write8(struct sdhci *host, int reg, u32 val)
 }
 
 #define SDHCI_NO_DMA DMA_ERROR_CODE
+int sdhci_wait_idle(struct sdhci *host, struct mci_cmd *cmd);
 int sdhci_wait_for_done(struct sdhci *host, u32 mask);
 void sdhci_read_response(struct sdhci *host, struct mci_cmd *cmd);
 void sdhci_set_cmd_xfer_mode(struct sdhci *host, struct mci_cmd *cmd,
-- 
2.39.2




More information about the barebox mailing list