mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH v2 3/5] mci: core: factor out MMC bus width selection for reuse
Date: Tue, 18 Apr 2023 11:30:38 +0200	[thread overview]
Message-ID: <20230418093040.1865982-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230418093040.1865982-1-a.fatoum@pengutronix.de>

Higher speed modes like DDR52 and HS200 can operate at both 8-bit and
4-bit bus widths. To make it easier to support these, split up
mci_startup_mmc, so the refactored parts can be called with different
timings later on.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2:
  - new patch
---
 drivers/mci/mci-core.c | 95 ++++++++++++++++++++++++++++--------------
 1 file changed, 64 insertions(+), 31 deletions(-)

diff --git a/drivers/mci/mci-core.c b/drivers/mci/mci-core.c
index 2abed0011722..bd5299e7a4f8 100644
--- a/drivers/mci/mci-core.c
+++ b/drivers/mci/mci-core.c
@@ -1167,34 +1167,51 @@ static int mci_startup_sd(struct mci *mci)
 	return 0;
 }
 
-static int mci_startup_mmc(struct mci *mci)
+static u32 mci_bus_width_ext_csd_bits(enum mci_bus_width bus_width)
+{
+	switch (bus_width) {
+	case MMC_BUS_WIDTH_8:
+		return EXT_CSD_BUS_WIDTH_8;
+	case MMC_BUS_WIDTH_4:
+		return EXT_CSD_BUS_WIDTH_4;
+	case MMC_BUS_WIDTH_1:
+	default:
+		return EXT_CSD_BUS_WIDTH_1;
+	}
+}
+
+static int mci_mmc_try_bus_width(struct mci *mci, enum mci_bus_width bus_width)
 {
-	struct mci_host *host = mci->host;
+	u32 ext_csd_bits;
 	int err;
+
+	ext_csd_bits = mci_bus_width_ext_csd_bits(bus_width);
+
+	err = mci_switch(mci, EXT_CSD_BUS_WIDTH, ext_csd_bits);
+	if (err < 0)
+		return err;
+
+	mci_set_bus_width(mci, bus_width);
+
+	err = mmc_compare_ext_csds(mci, bus_width);
+	if (err < 0)
+		return err;
+
+	return bus_width;
+}
+
+static int mci_mmc_select_bus_width(struct mci *mci)
+{
+	struct mci_host *host = mci->host;
+	int ret;
 	int idx = 0;
-	static unsigned ext_csd_bits[] = {
-		EXT_CSD_BUS_WIDTH_4,
-		EXT_CSD_BUS_WIDTH_8,
-	};
 	static enum mci_bus_width bus_widths[] = {
 		MMC_BUS_WIDTH_4,
 		MMC_BUS_WIDTH_8,
 	};
 
-	/* if possible, speed up the transfer */
-	if (mci_caps(mci) & MMC_CAP_MMC_HIGHSPEED) {
-		if (mci->card_caps & MMC_CAP_MMC_HIGHSPEED_52MHZ)
-			mci->tran_speed = 52000000;
-		else
-			mci->tran_speed = 26000000;
-
-		host->timing = MMC_TIMING_MMC_HS;
-	}
-
-	mci_set_clock(mci, mci->tran_speed);
-
 	if (!(host->host_caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
-		return 0;
+		return MMC_BUS_WIDTH_1;
 
 	/*
 	 * Unlike SD, MMC cards dont have a configuration register to notify
@@ -1206,7 +1223,6 @@ static int mci_startup_mmc(struct mci *mci)
 		idx = 1;
 
 	for (; idx >= 0; idx--) {
-
 		/*
 		 * Host is capable of 8bit transfer, then switch
 		 * the device to work in 8bit transfer mode. If the
@@ -1214,21 +1230,38 @@ static int mci_startup_mmc(struct mci *mci)
 		 * 4bit transfer mode. On success set the corresponding
 		 * bus width on the host.
 		 */
-		err = mci_switch(mci, EXT_CSD_BUS_WIDTH, ext_csd_bits[idx]);
-		if (err) {
-			if (idx == 0)
-				dev_warn(&mci->dev, "Changing MMC bus width failed: %d\n", err);
-			continue;
-		}
+		ret = mci_mmc_try_bus_width(mci, bus_widths[idx]);
+		if (ret > 0)
+			break;
+	}
 
-		mci_set_bus_width(mci, bus_widths[idx]);
+	return ret;
+}
 
-		err = mmc_compare_ext_csds(mci, bus_widths[idx]);
-		if (!err)
-			break;
+static int mci_startup_mmc(struct mci *mci)
+{
+	struct mci_host *host = mci->host;
+	int ret;
+
+	/* if possible, speed up the transfer */
+	if (mci_caps(mci) & MMC_CAP_MMC_HIGHSPEED) {
+		if (mci->card_caps & MMC_CAP_MMC_HIGHSPEED_52MHZ)
+			mci->tran_speed = 52000000;
+		else
+			mci->tran_speed = 26000000;
+
+		host->timing = MMC_TIMING_MMC_HS;
 	}
 
-	return err;
+	mci_set_clock(mci, mci->tran_speed);
+
+	ret = mci_mmc_select_bus_width(mci);
+	if (ret < 0) {
+		dev_warn(&mci->dev, "Changing MMC bus width failed: %d\n", ret);
+		return ret;
+	}
+
+	return 0;
 }
 
 /**
-- 
2.39.2




  parent reply	other threads:[~2023-04-18  9:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-18  9:30 [PATCH v2 1/5] mci: core: drop unused DDR/SDR constants Ahmad Fatoum
2023-04-18  9:30 ` [PATCH v2 2/5] mci: introduce new dedicated enum mmc_bus_width type Ahmad Fatoum
2023-04-18  9:30 ` Ahmad Fatoum [this message]
2023-04-18  9:30 ` [PATCH v2 4/5] mci: add eMMC DDR52 support Ahmad Fatoum
2023-04-18  9:30 ` [PATCH v2 5/5] mci: imx-esdhc: add uSDHC " Ahmad Fatoum
2023-04-21  6:29 ` [PATCH v2 1/5] mci: core: drop unused DDR/SDR constants Sascha Hauer

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=20230418093040.1865982-3-a.fatoum@pengutronix.de \
    --to=a.fatoum@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