Skip to contentSkip to conversation

mmc: sdhci_am654: Add workaround for card detect debounce timer

PATCH Superseded Faiz Abbas 2 participants 5 messages Jul 29, 2020 – Aug 5, 2020

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Faiz Abbas

author
#0 lore raw

PATCH 1/1 mmc: sdhci_am654: Add workaround for card detect debounce timer

There is a one time delay because of a card detect debounce timer in the controller IP. This timer runs as soon as power is applied to the module regardless of whether a card is present or not and any writes to SDHCI_POWER_ON will return 0 before it expires. This timeout has been measured to be about 1 second in am654x and j721e.

Write-and-read-back in a loop on SDHCI_POWER_ON for a maximum of 1.5 seconds to make sure that the controller actually powers on.

---
 drivers/mmc/host/sdhci_am654.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/mmc/host/sdhci_am654.c +21 -0
index 1718b9e8af63..55cff9de2f3e 100644
@@ -272,6 +272,7 @@ static void sdhci_j721e_4bit_set_clock(struct sdhci_host *host,
 	sdhci_set_clock(host, clock);
 }
 
+#define MAX_POWER_ON_TIMEOUT	1500 /* ms */
 static void sdhci_am654_write_b(struct sdhci_host *host, u8 val, int reg)
 {
 	unsigned char timing = host->mmc->ios.timing;
@@ -291,6 +292,26 @@ static void sdhci_am654_write_b(struct sdhci_host *host, u8 val, int reg)
 	}
 
 	writeb(val, host->ioaddr + reg);
+	if (reg == SDHCI_POWER_CONTROL && (val & SDHCI_POWER_ON)) {
+		/*
+		 * Power on will not happen until the card detect debounce
+		 * timer expires. Wait at least 1.5 seconds for the power on
+		 * bit to be set
+		 */
+		ktime_t timeout = ktime_add_ms(ktime_get(),
+					       MAX_POWER_ON_TIMEOUT);
+		do {
+			if (ktime_compare(ktime_get(), timeout) > 0) {
+				dev_warn(mmc_dev(host->mmc),
+					 "Power on failed\n");
+
+				return;
+			}
+
+			writeb(val, host->ioaddr + reg);
+			usleep_range(1000, 10000);
+		} while (!(readb(host->ioaddr + reg) & SDHCI_POWER_ON));
+	}
 }
 
 static int sdhci_am654_execute_tuning(struct mmc_host *mmc, u32 opcode)
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 27 lines · Faiz Abbas

There is a one time delay because of a card detect debounce timer in the controller IP. This timer runs as soon as power is applied to the module regardless of whether a card is present or not and any writes to SDHCI_POWER_ON will return 0 before it expires. This timeout has been measured to be about 1 second in am654x and j721e.

Write-and-read-back in a loop on SDHCI_POWER_ON for a maximum of 1.5 seconds to make sure that the controller actually powers on.

---
 drivers/mmc/host/sdhci_am654.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/mmc/host/sdhci_am654.c +1 -0
index 1718b9e8af63..55cff9de2f3e 100644
@@ -272,6 +272,7 @@ static void sdhci_j721e_4bit_set_clock(struct sdhci_host *host,
 	sdhci_set_clock(host, clock);
 }
 
+#define MAX_POWER_ON_TIMEOUT	1500 /* ms */
 static void sdhci_am654_write_b(struct sdhci_host *host, u8 val, int reg)
 {
 	unsigned char timing = host->mmc->ios.timing;

drivers/mmc/host/sdhci_am654.c static void sdhci_am654_write_b(struct sdhci_host *host, u8 val, int reg) · line 300

Quoted 9 lines
drivers/mmc/host/sdhci_am654.c +6 -0
@@ -291,6 +292,26 @@ static void sdhci_am654_write_b(struct sdhci_host *host, u8 val, int reg)
 	}
 
 	writeb(val, host->ioaddr + reg);
+	if (reg == SDHCI_POWER_CONTROL && (val & SDHCI_POWER_ON)) {
+		/*
+		 * Power on will not happen until the card detect debounce
+		 * timer expires. Wait at least 1.5 seconds for the power on
+		 * bit to be set
+		 */

Can you use readb_poll_timeout() here?

Quoted 18 lines
drivers/mmc/host/sdhci_am654.c +14 -0
+		ktime_t timeout = ktime_add_ms(ktime_get(),
+					       MAX_POWER_ON_TIMEOUT);
+		do {
+			if (ktime_compare(ktime_get(), timeout) > 0) {
+				dev_warn(mmc_dev(host->mmc),
+					 "Power on failed\n");
+
+				return;
+			}
+
+			writeb(val, host->ioaddr + reg);
+			usleep_range(1000, 10000);
+		} while (!(readb(host->ioaddr + reg) & SDHCI_POWER_ON));
+	}
 }
 
 static int sdhci_am654_execute_tuning(struct mmc_host *mmc, u32 opcode)
author
#2 lore raw

Hi Adrian,

Quoted 30 lines · Adrian Hunter
Quoted 29 lines · Faiz Abbas

There is a one time delay because of a card detect debounce timer in the controller IP. This timer runs as soon as power is applied to the module regardless of whether a card is present or not and any writes to SDHCI_POWER_ON will return 0 before it expires. This timeout has been measured to be about 1 second in am654x and j721e.

Write-and-read-back in a loop on SDHCI_POWER_ON for a maximum of 1.5 seconds to make sure that the controller actually powers on.

---
 drivers/mmc/host/sdhci_am654.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/mmc/host/sdhci_am654.c +1 -0
index 1718b9e8af63..55cff9de2f3e 100644
@@ -272,6 +272,7 @@ static void sdhci_j721e_4bit_set_clock(struct sdhci_host *host,
 	sdhci_set_clock(host, clock);
 }
 
+#define MAX_POWER_ON_TIMEOUT	1500 /* ms */
 static void sdhci_am654_write_b(struct sdhci_host *host, u8 val, int reg)
 {
 	unsigned char timing = host->mmc->ios.timing;
@@ -291,6 +292,26 @@ static void sdhci_am654_write_b(struct sdhci_host *host, u8 val, int reg)
 	}
 
Quoted 10 lines

drivers/mmc/host/sdhci_am654.c static void sdhci_am654_write_b(struct sdhci_host *host, u8 val, int reg) · line 300

Quoted 7 lines
drivers/mmc/host/sdhci_am654.c +6 -0
 	writeb(val, host->ioaddr + reg);
+	if (reg == SDHCI_POWER_CONTROL && (val & SDHCI_POWER_ON)) {
+		/*
+		 * Power on will not happen until the card detect debounce
+		 * timer expires. Wait at least 1.5 seconds for the power on
+		 * bit to be set
+		 */

Can you use readb_poll_timeout() here?

The loop is write -> readback -> check for set bit -> write again and so on until timeout so poll_timeout() calls will not work.

Thanks,
Faiz

maintainer
#3 lore raw
Quoted 44 lines · Faiz Abbas

Hi Adrian,

Quoted 30 lines · Adrian Hunter
Quoted 29 lines · Faiz Abbas

There is a one time delay because of a card detect debounce timer in the controller IP. This timer runs as soon as power is applied to the module regardless of whether a card is present or not and any writes to SDHCI_POWER_ON will return 0 before it expires. This timeout has been measured to be about 1 second in am654x and j721e.

Write-and-read-back in a loop on SDHCI_POWER_ON for a maximum of 1.5 seconds to make sure that the controller actually powers on.

---
 drivers/mmc/host/sdhci_am654.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/mmc/host/sdhci_am654.c +1 -0
index 1718b9e8af63..55cff9de2f3e 100644
@@ -272,6 +272,7 @@ static void sdhci_j721e_4bit_set_clock(struct sdhci_host *host,
 	sdhci_set_clock(host, clock);
 }
 
+#define MAX_POWER_ON_TIMEOUT	1500 /* ms */
 static void sdhci_am654_write_b(struct sdhci_host *host, u8 val, int reg)
 {
 	unsigned char timing = host->mmc->ios.timing;
@@ -291,6 +292,26 @@ static void sdhci_am654_write_b(struct sdhci_host *host, u8 val, int reg)
 	}
 
Quoted 10 lines

drivers/mmc/host/sdhci_am654.c static void sdhci_am654_write_b(struct sdhci_host *host, u8 val, int reg) · line 300

Quoted 7 lines
drivers/mmc/host/sdhci_am654.c +6 -0
 	writeb(val, host->ioaddr + reg);
+	if (reg == SDHCI_POWER_CONTROL && (val & SDHCI_POWER_ON)) {
+		/*
+		 * Power on will not happen until the card detect debounce
+		 * timer expires. Wait at least 1.5 seconds for the power on
+		 * bit to be set
+		 */

Can you use readb_poll_timeout() here?

Quoted 2 lines

The loop is write -> readback -> check for set bit -> write again and so on until timeout so poll_timeout() calls will not work.

I mentioned it because pedantically you need to check the condition again after a timeout. Alternatively, the read_poll_timeout macro can be used with a function, something like below (not even compile tested!)

static u8 write_power_on(struct sdhci_host *host, u8 val, int reg)

{
	writeb(val, host->ioaddr + reg);
	usleep_range(1000, 10000);
	return readb(host->ioaddr + reg);
}

#define MAX_POWER_ON_TIMEOUT 1500000 /* us */
static void sdhci_am654_write_b(struct sdhci_host *host, u8 val, int reg)

{
	unsigned char timing = host->mmc->ios.timing;
	u8 pwr;

	if (reg == SDHCI_HOST_CONTROL) {
		switch (timing) {
		/*
		 * According to the data manual, HISPD bit
		 * should not be set in these speed modes.
		 */
		case MMC_TIMING_SD_HS:
		case MMC_TIMING_MMC_HS:
		case MMC_TIMING_UHS_SDR12:
		case MMC_TIMING_UHS_SDR25:
			val &= ~SDHCI_CTRL_HISPD;
		}
	}

	writeb(val, host->ioaddr + reg);

	/*
	 * Power on will not happen until the card detect debounce
	 * timer expires. Wait at least 1.5 seconds for the power on
	 * bit to be set
	 */
	if (reg == SDHCI_POWER_CONTROL && (val & SDHCI_POWER_ON) &&
	    read_poll_timeout(write_power_on, pwr, (pwr & SDHCI_POWER_ON), 0,
			      MAX_POWER_ON_TIMEOUT, false, host, val, reg))
			dev_warn(mmc_dev(host->mmc), "Power on failed\n");
			return;
		}
	}
}