mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH] mci: sdhci: Add and use common sdhci_wait_for_done()
Date: Tue, 21 Mar 2023 10:50:56 +0100	[thread overview]
Message-ID: <20230321095056.1333669-1-s.hauer@pengutronix.de> (raw)

We have different driver specific variants of functions polling for the
device ready in the tree. Add a common sdhci_wait_for_done() and use
it where appropriate. This fixes a few deficiencies with the driver
specific variants.

rk_sdhci_wait_for_done() didn't check the SDHCI_INT_TIMEOUT bit and
returned -EPERM instead when it ought to return -ETIMEDOUT. The core
tries to detect a SD card first and expects a -ETIMEDOUT for the setup
command when really a eMMC is connected. Only with a -ETIMEDOUT the core
tries to detect a eMMC next.

at91_sdhci_wait_for_done() returned the status instead of the expected
0 value for success.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/mci/arasan-sdhci.c           | 29 +---------------------------
 drivers/mci/atmel-sdhci-common.c     | 28 +--------------------------
 drivers/mci/rockchip-dwcmshc-sdhci.c | 25 +-----------------------
 drivers/mci/sdhci.c                  | 27 ++++++++++++++++++++++++++
 drivers/mci/sdhci.h                  |  1 +
 5 files changed, 31 insertions(+), 79 deletions(-)

diff --git a/drivers/mci/arasan-sdhci.c b/drivers/mci/arasan-sdhci.c
index 00a8ceed68..650de22b69 100644
--- a/drivers/mci/arasan-sdhci.c
+++ b/drivers/mci/arasan-sdhci.c
@@ -133,33 +133,6 @@ static void arasan_sdhci_set_ios(struct mci_host *mci, struct mci_ios *ios)
 	sdhci_write8(&host->sdhci, SDHCI_HOST_CONTROL, val);
 }
 
-static int arasan_sdhci_wait_for_done(struct arasan_sdhci_host *host, u32 mask)
-{
-	u64 start = get_time_ns();
-	u32 stat;
-
-	do {
-		stat = sdhci_read32(&host->sdhci, SDHCI_INT_STATUS);
-
-		if (stat & SDHCI_INT_TIMEOUT)
-			return -ETIMEDOUT;
-
-		if (stat & SDHCI_INT_ERROR) {
-			dev_err(host->mci.hw_dev, "SDHCI_INT_ERROR: 0x%08x\n",
-				stat);
-			return -EPERM;
-		}
-
-		if (is_timeout(start, 1000 * MSECOND)) {
-			dev_err(host->mci.hw_dev,
-				"SDHCI timeout while waiting for done\n");
-			return -ETIMEDOUT;
-		}
-	} while ((stat & mask) != mask);
-
-	return 0;
-}
-
 static void print_error(struct arasan_sdhci_host *host, int cmdidx, int ret)
 {
 	if (ret == -ETIMEDOUT)
@@ -213,7 +186,7 @@ static int arasan_sdhci_send_cmd(struct mci_host *mci, struct mci_cmd *cmd,
 	sdhci_write32(&host->sdhci, SDHCI_ARGUMENT, cmd->cmdarg);
 	sdhci_write16(&host->sdhci, SDHCI_COMMAND, command);
 
-	ret = arasan_sdhci_wait_for_done(host, mask);
+	ret = sdhci_wait_for_done(&host->sdhci, mask);
 	if (ret)
 		goto error;
 
diff --git a/drivers/mci/atmel-sdhci-common.c b/drivers/mci/atmel-sdhci-common.c
index 05a019beb6..58ba0b9b3d 100644
--- a/drivers/mci/atmel-sdhci-common.c
+++ b/drivers/mci/atmel-sdhci-common.c
@@ -89,32 +89,6 @@ exit:
 	return is_inserted;
 }
 
-static int at91_sdhci_wait_for_done(struct at91_sdhci *host, u32 mask)
-{
-	struct sdhci *sdhci = &host->sdhci;
-	u32 status;
-	int ret;
-
-	ret = sdhci_read32_poll_timeout(sdhci, SDHCI_INT_STATUS, status,
-					(status & mask) == mask || (status & SDHCI_INT_ERROR),
-					USEC_PER_SEC);
-
-	if (ret < 0) {
-		dev_err(host->dev, "SDHCI timeout while waiting for done\n");
-		return ret;
-	}
-
-	if (status & SDHCI_INT_TIMEOUT)
-		return -ETIMEDOUT;
-
-	if (status & SDHCI_INT_ERROR) {
-		dev_err(host->dev, "SDHCI_INT_STATUS: 0x%08x\n", status);
-		return -EPERM;
-	}
-
-	return status & 0xFFFF;
-}
-
 int at91_sdhci_send_command(struct at91_sdhci *host, struct mci_cmd *cmd,
 			    struct mci_data *data)
 {
@@ -158,7 +132,7 @@ int at91_sdhci_send_command(struct at91_sdhci *host, struct mci_cmd *cmd,
 	sdhci_write32(sdhci, SDHCI_ARGUMENT, cmd->cmdarg);
 	sdhci_write16(sdhci, SDHCI_COMMAND, command);
 
-	status = at91_sdhci_wait_for_done(host, mask);
+	status = sdhci_wait_for_done(&host->sdhci, mask);
 	if (status < 0)
 		goto error;
 
diff --git a/drivers/mci/rockchip-dwcmshc-sdhci.c b/drivers/mci/rockchip-dwcmshc-sdhci.c
index 4b4e8b7bd6..e055a8ea71 100644
--- a/drivers/mci/rockchip-dwcmshc-sdhci.c
+++ b/drivers/mci/rockchip-dwcmshc-sdhci.c
@@ -216,29 +216,6 @@ static void rk_sdhci_set_ios(struct mci_host *mci, struct mci_ios *ios)
 	sdhci_write8(&host->sdhci, SDHCI_HOST_CONTROL, val);
 }
 
-static int rk_sdhci_wait_for_done(struct rk_sdhci_host *host, u32 mask)
-{
-	u64 start = get_time_ns();
-	u16 stat;
-
-	do {
-		stat = sdhci_read16(&host->sdhci, SDHCI_INT_NORMAL_STATUS);
-		if (stat & SDHCI_INT_ERROR) {
-			dev_dbg(host->mci.hw_dev, "SDHCI_INT_ERROR: 0x%08x\n",
-				sdhci_read16(&host->sdhci, SDHCI_INT_ERROR_STATUS));
-			return -EPERM;
-		}
-
-		if (is_timeout(start, 1000 * MSECOND)) {
-			dev_err(host->mci.hw_dev,
-					"SDHCI timeout while waiting for done\n");
-			return -ETIMEDOUT;
-		}
-	} while ((stat & mask) != mask);
-
-	return 0;
-}
-
 static void print_error(struct rk_sdhci_host *host, int cmdidx)
 {
 	dev_dbg(host->mci.hw_dev,
@@ -285,7 +262,7 @@ static int rk_sdhci_send_cmd(struct mci_host *mci, struct mci_cmd *cmd,
 	sdhci_write32(&host->sdhci, SDHCI_ARGUMENT, cmd->cmdarg);
 	sdhci_write16(&host->sdhci, SDHCI_COMMAND, command);
 
-	ret = rk_sdhci_wait_for_done(host, SDHCI_INT_CMD_COMPLETE);
+	ret = sdhci_wait_for_done(&host->sdhci, SDHCI_INT_CMD_COMPLETE);
 	if (ret == -EPERM)
 		goto error;
 	else if (ret)
diff --git a/drivers/mci/sdhci.c b/drivers/mci/sdhci.c
index 2cdd3c3c8f..635884e2a2 100644
--- a/drivers/mci/sdhci.c
+++ b/drivers/mci/sdhci.c
@@ -124,6 +124,33 @@ void sdhci_set_bus_width(struct sdhci *host, int width)
 
 #endif
 
+int sdhci_wait_for_done(struct sdhci *sdhci, u32 mask)
+{
+	u64 start = get_time_ns();
+	u32 stat;
+
+	do {
+		stat = sdhci_read32(sdhci, SDHCI_INT_STATUS);
+
+		if (stat & SDHCI_INT_TIMEOUT)
+			return -ETIMEDOUT;
+
+		if (stat & SDHCI_INT_ERROR) {
+			dev_err(sdhci->mci->hw_dev, "SDHCI_INT_ERROR: 0x%08x\n",
+				stat);
+			return -EPERM;
+		}
+
+		if (is_timeout(start, 1000 * MSECOND)) {
+			dev_err(sdhci->mci->hw_dev,
+				"SDHCI timeout while waiting for done\n");
+			return -ETIMEDOUT;
+		}
+	} while ((stat & mask) != mask);
+
+	return 0;
+}
+
 void sdhci_setup_data_pio(struct sdhci *sdhci, struct mci_data *data)
 {
 	if (!data)
diff --git a/drivers/mci/sdhci.h b/drivers/mci/sdhci.h
index c538385939..fe8c25cb9c 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_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,
 			     struct mci_data *data, bool dma, u32 *command,
-- 
2.30.2




             reply	other threads:[~2023-03-21  9:52 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-21  9:50 Sascha Hauer [this message]
2023-03-27 13:01 ` Rouven Czerwinski
2023-03-28  9:58   ` Sascha Hauer
2023-03-28 12:33     ` Rouven Czerwinski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230321095056.1333669-1-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox