Skip to contentSkip to conversation

mmc: Fix read-only detection with JMicron 388 chip

PATCH Superseded Takashi Iwai 2 participants 5 messages Apr 21, 2011

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mmc: Fix read-only detection with JMicron 388 chip

On HP laptops with JMicron 388 chip, the write-locked SD card isn't detected correctly as read-only in many cases. This is because the PRESENT_STATE register becomes unsable just after plugging, and it returns the WRITE_PROTECT bit wrongly at the first read.

This patch fixes the read-only detection by adding the own get_ro op for checking the register more intensively with a relatively long delay.

The patch is tested with 2.6.39-rc4 kernel.

---
 drivers/mmc/host/sdhci-pci.c |   34 ++++++++++++++++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)
drivers/mmc/host/sdhci-pci.c +34 -0
index a136be7..9037b2a 100644
@@ -346,6 +346,35 @@ static void jmicron_enable_mmc(struct sdhci_host *host, int on)
 	writeb(scratch, host->ioaddr + 0xC0);
 }
 
+/* As PRESENT_STATE register becomes unstable just after plugging,
+ * need to check the RO-cap multiple times with a relatively long delay.
+ */
+
+#define SAMPLE_COUNT	5
+
+static unsigned int jmicron_get_ro(struct sdhci_host *host)
+{
+	int i, ro_count;
+
+	ro_count = 0;
+	for (i = 0; i < SAMPLE_COUNT; i++) {
+		int present = sdhci_readl(host, SDHCI_PRESENT_STATE);
+		if (!(present & SDHCI_WRITE_PROTECT)) {
+			if (++ro_count > SAMPLE_COUNT / 2)
+				return 1;
+		}
+		msleep(30);
+	}
+	return 0;
+}
+
+static int sdhci_pci_enable_dma(struct sdhci_host *host);
+
+static struct sdhci_ops jmicron_pci_ops = {
+	.enable_dma	= sdhci_pci_enable_dma,
+	.get_ro		= jmicron_get_ro,
+};
+
 static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
 {
 	if (slot->chip->pdev->revision == 0) {
@@ -383,6 +412,11 @@ static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
 
 	slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
 
+	/* Replace the ops for unstable get_ro detection */
+	if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
+	    slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
+		slot->host->ops = &jmicron_pci_ops;
+
 	return 0;
 }
 
Signature · 1 line
1.7.4.2
#1 lore raw

Hi Takashi,

Quoted 58 lines · Takashi Iwai

On HP laptops with JMicron 388 chip, the write-locked SD card isn't detected correctly as read-only in many cases. This is because the PRESENT_STATE register becomes unsable just after plugging, and it returns the WRITE_PROTECT bit wrongly at the first read.

This patch fixes the read-only detection by adding the own get_ro op for checking the register more intensively with a relatively long delay.

The patch is tested with 2.6.39-rc4 kernel.

---
 drivers/mmc/host/sdhci-pci.c |   34 ++++++++++++++++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)
drivers/mmc/host/sdhci-pci.c +29 -0
index a136be7..9037b2a 100644
@@ -346,6 +346,35 @@ static void jmicron_enable_mmc(struct sdhci_host *host, int on)
 	writeb(scratch, host->ioaddr + 0xC0);
 }
 
+/* As PRESENT_STATE register becomes unstable just after plugging,
+ * need to check the RO-cap multiple times with a relatively long delay.
+ */
+
+#define SAMPLE_COUNT	5
+
+static unsigned int jmicron_get_ro(struct sdhci_host *host)
+{
+	int i, ro_count;
+
+	ro_count = 0;
+	for (i = 0; i < SAMPLE_COUNT; i++) {
+		int present = sdhci_readl(host, SDHCI_PRESENT_STATE);
+		if (!(present & SDHCI_WRITE_PROTECT)) {
+			if (++ro_count > SAMPLE_COUNT / 2)
+				return 1;
+		}
+		msleep(30);
+	}
+	return 0;
+}
+
+static int sdhci_pci_enable_dma(struct sdhci_host *host);
+
+static struct sdhci_ops jmicron_pci_ops = {
+	.enable_dma	= sdhci_pci_enable_dma,
+	.get_ro		= jmicron_get_ro,
+};
+
 static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
 {
 	if (slot->chip->pdev->revision == 0) {

drivers/mmc/host/sdhci-pci.c static int jmicron_probe_slot(struct sdhci_pci_slot *slot) · line 421

Quoted 10 lines
drivers/mmc/host/sdhci-pci.c +5 -0
@@ -383,6 +412,11 @@ static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
 
 	slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
 
+	/* Replace the ops for unstable get_ro detection */
+	if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
+	    slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
+		slot->host->ops = &jmicron_pci_ops;
+
 	return 0;
 }

I don't like overwriting ops here -- it's too magical, and now we have to maintain the ops table in two places. A quirk seems justified here, even though we're trying to reduce them in general. Can anyone find a better solution?

Thanks,

- Chris.

Signature · 2 lines
Chris Ball   <[email protected]>   <http://printf.net/>
One Laptop Per Child
#2 lore raw

At Thu, 21 Apr 2011 12:40:26 -0400,

Quoted 65 lines · Chris Ball

Hi Takashi,

Quoted 61 lines · Takashi Iwai

On HP laptops with JMicron 388 chip, the write-locked SD card isn't detected correctly as read-only in many cases. This is because the PRESENT_STATE register becomes unsable just after plugging, and it returns the WRITE_PROTECT bit wrongly at the first read.

This patch fixes the read-only detection by adding the own get_ro op for checking the register more intensively with a relatively long delay.

The patch is tested with 2.6.39-rc4 kernel.

---
 drivers/mmc/host/sdhci-pci.c |   34 ++++++++++++++++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)
drivers/mmc/host/sdhci-pci.c +29 -0
index a136be7..9037b2a 100644
@@ -346,6 +346,35 @@ static void jmicron_enable_mmc(struct sdhci_host *host, int on)
 	writeb(scratch, host->ioaddr + 0xC0);
 }
 
+/* As PRESENT_STATE register becomes unstable just after plugging,
+ * need to check the RO-cap multiple times with a relatively long delay.
+ */
+
+#define SAMPLE_COUNT	5
+
+static unsigned int jmicron_get_ro(struct sdhci_host *host)
+{
+	int i, ro_count;
+
+	ro_count = 0;
+	for (i = 0; i < SAMPLE_COUNT; i++) {
+		int present = sdhci_readl(host, SDHCI_PRESENT_STATE);
+		if (!(present & SDHCI_WRITE_PROTECT)) {
+			if (++ro_count > SAMPLE_COUNT / 2)
+				return 1;
+		}
+		msleep(30);
+	}
+	return 0;
+}
+
+static int sdhci_pci_enable_dma(struct sdhci_host *host);
+
+static struct sdhci_ops jmicron_pci_ops = {
+	.enable_dma	= sdhci_pci_enable_dma,
+	.get_ro		= jmicron_get_ro,
+};
+
 static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
 {
 	if (slot->chip->pdev->revision == 0) {
@@ -383,6 +412,11 @@ static int jmicron_probe_slot(struct sdhci_pci_slot *slot)
 
 	slot->host->mmc->caps |= MMC_CAP_BUS_WIDTH_TEST;
 
Quoted 11 lines

drivers/mmc/host/sdhci-pci.c static int jmicron_probe_slot(struct sdhci_pci_slot *slot) · line 421

Quoted 7 lines
drivers/mmc/host/sdhci-pci.c +5 -0
+	/* Replace the ops for unstable get_ro detection */
+	if (slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_SD ||
+	    slot->chip->pdev->device == PCI_DEVICE_ID_JMICRON_JMB388_ESD)
+		slot->host->ops = &jmicron_pci_ops;
+
 	return 0;
 }

I don't like overwriting ops here -- it's too magical, and now we have to maintain the ops table in two places. A quirk seems justified here, even though we're trying to reduce them in general.

Well, I also used quirk bit in my very first version I worked for 2.6.32 kernel. But when I looked at 2.6.39, quirks are almost full -- only the last one bit is left for bit 31. So I didn't want to finish it :)

Quoted 1 line

Can anyone find a better solution?

One way would be to copy the ops table itself in struct sdhci_host instead of keeping the ops table pointer. Then you can overwrite only the specific op in each probe_slot callback.

thanks,

Takashi

#3 lore raw

Hi Takashi,

Quoted 9 lines · Takashi Iwai
Quoted 3 lines

I don't like overwriting ops here -- it's too magical, and now we have to maintain the ops table in two places. A quirk seems justified here, even though we're trying to reduce them in general.

Well, I also used quirk bit in my very first version I worked for 2.6.32 kernel. But when I looked at 2.6.39, quirks are almost full -- only the last one bit is left for bit 31. So I didn't want to finish it :)

Quoted 5 lines
Quoted 1 line

Can anyone find a better solution?

One way would be to copy the ops table itself in struct sdhci_host instead of keeping the ops table pointer. Then you can overwrite only the specific op in each probe_slot callback.

I think I'd rather not touch ops at all and keep the code simple -- if you don't mind, please post a version with quirks, and I'll work on freeing up a few bits.

Thanks,

- Chris.

Signature · 2 lines
Chris Ball   <[email protected]>   <http://printf.net/>
One Laptop Per Child
#4 lore raw

At Thu, 21 Apr 2011 13:36:03 -0400,

Quoted 15 lines · Chris Ball

Hi Takashi,

Quoted 11 lines · Takashi Iwai
Quoted 3 lines

I don't like overwriting ops here -- it's too magical, and now we have to maintain the ops table in two places. A quirk seems justified here, even though we're trying to reduce them in general.

Well, I also used quirk bit in my very first version I worked for 2.6.32 kernel. But when I looked at 2.6.39, quirks are almost full -- only the last one bit is left for bit 31. So I didn't want to finish it :)

Quoted 1 line

Can anyone find a better solution?

Quoted 7 lines
Quoted 3 lines

One way would be to copy the ops table itself in struct sdhci_host instead of keeping the ops table pointer. Then you can overwrite only the specific op in each probe_slot callback.

I think I'd rather not touch ops at all and keep the code simple -- if you don't mind, please post a version with quirks, and I'll work on freeing up a few bits.

OK, I'll send shortly.

Takashi