Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 19 Apr 2011 09:20:26 -0600 (MDT)
From:      Ian Lepore <freebsd@damnhippie.dyndns.org>
To:        FreeBSD-gnats-submit@FreeBSD.org
Subject:   arm/156496: [patch] Minor bugfixes and enhancements to mmc and mmcsd drivers
Message-ID:  <201104191520.p3JFKQta028382@revolution.hippie.lan>
Resent-Message-ID: <201104191540.p3JFe3jq085382@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         156496
>Category:       arm
>Synopsis:       [patch] Minor bugfixes and enhancements to mmc and mmcsd drivers
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-arm
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Apr 19 15:40:03 UTC 2011
>Closed-Date:
>Last-Modified:
>Originator:     Ian Lepore <freebsd@damnhippie.dyndns.org>
>Release:        FreeBSD 8.2-RC3 arm
>Organization:
none
>Environment:
FreeBSD dvb 8.2-RC3 FreeBSD 8.2-RC3 #49: Tue Feb 15 22:52:14 UTC 2011     root@revolution.hippie.lan:/usr/obj/arm/usr/src/sys/DVB  arm

>Description:
This applies several fixes and minor enhancements to the mmc and mmcsd drivers.

Fixes:

 - When switching to 4-bit operation, send a SET_CLR_CARD_DETECT command
   to disconnect the card-detect pullup resistor from the DAT3 line before 
   sending the SET_BUS_WIDTH command.

 - Add the missing "reserved" zero entry to the mantissa table used to 
   decode various CSD fields.  This was causing SD cards to report that they
   could run at 30mhz instead of the maximum 25mhz mandated in the spec.

Enhancements:

 - At the MMC layer, format various info from the CID into a string that
   uniquely identifies the card instance (manufacturer number, serial 
   number, product name and revision,etc).  Export it as an instance variable.

 - At the MMCSD layer, display the formatted card id string, and also report
   the clock speed of the hardware (not the card's max speed), and the number
   of bits and number of blocks per transfer.  It comes out like this now:

     mmcsd0: 968MB <SD SD01G 8.0 SN 276886905 Mfg 08/2008 by 3 SD> at mmc0 22.5MHz/4bit/128-block

These diffs were generated against code from FreeBSD 9 as it existed on 
March 15 2011 (re-tested on April 19 2011).

These changes have been tested on ARM at91rm9200 hardware.  I don't have 
access to an sdhci controller for testing, but since these changes don't 
affect the mmc<->underlying-bridge-hardware interface in any way, I don't 
anticipate any problems.

Work sponsored by: Symmetricom, Inc.

>How-To-Repeat:

>Fix:

--- patch-mmcsd begins here ---
--- sys/dev/mmc/mmc.c	2011-03-15 08:45:51.000000000 -0600
+++ sys/dev/mmc2/mmc.c	2011-03-15 08:54:38.000000000 -0600
@@ -101,6 +101,7 @@
 	uint32_t tran_speed;	/* Max speed in normal mode */
 	uint32_t hs_tran_speed;	/* Max speed in high speed mode */
 	uint32_t erase_sector;	/* Card native erase sector size */
+	char card_id_string[64];/* Formatted CID info (serial, mfg, etc) */
 };
 
 #define CMD_RETRIES	3
@@ -606,19 +607,26 @@
 
 	if (mmcbr_get_mode(sc->dev) == mode_sd) {
 		memset(&cmd, 0, sizeof(struct mmc_command));
-		cmd.opcode = ACMD_SET_BUS_WIDTH;
+		cmd.opcode = ACMD_SET_CLR_CARD_DETECT;
 		cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
-		switch (width) {
-		case bus_width_1:
-			cmd.arg = SD_BUS_WIDTH_1;
-			break;
-		case bus_width_4:
-			cmd.arg = SD_BUS_WIDTH_4;
-			break;
-		default:
-			return (MMC_ERR_INVALID);
-		}
+		cmd.arg   = SD_CLR_CARD_DETECT;
 		err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
+		if (err == 0) {
+			memset(&cmd, 0, sizeof(struct mmc_command));
+			cmd.opcode = ACMD_SET_BUS_WIDTH;
+			cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
+			switch (width) {
+			case bus_width_1:
+				cmd.arg = SD_BUS_WIDTH_1;
+				break;
+			case bus_width_4:
+				cmd.arg = SD_BUS_WIDTH_4;
+				break;
+			default:
+				return (MMC_ERR_INVALID);
+			}
+			err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
+		}
 	} else {
 		switch (width) {
 		case bus_width_1:
@@ -788,11 +796,45 @@
 	cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
 }
 
+static void
+mmc_format_card_id_string(struct mmc_ivars * ivar)
+{
+	char oidstr[8];
+	uint8_t c1 = (ivar->cid.oid >> 8) & 0x0FF;
+	uint8_t c2 = ivar->cid.oid & 0x0FF;
+
+	/* Format a card ID string for use by the mmcsd driver, it's what 
+	 * appears between the <> in the following:
+	 *
+	 * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
+	 * 22.5MHz/4bit/128-block
+	 *
+	 * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
+	 * and our max formatted length is currently 55 bytes if every field
+	 * contains the largest value.
+	 *
+	 * Sometimes the oid is two printable ascii chars; when it's not,
+	 * format it as 0xnnnn instead.
+	 */
+
+	if (c1 > 0x1F && c1 < 0x7F && c2 > 0x1F && c2 < 0x7F)
+		snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
+	else
+		snprintf(oidstr, sizeof(oidstr), "0x%04X", ivar->cid.oid);
+        
+	snprintf(ivar->card_id_string, sizeof(ivar->card_id_string),
+            "%s%s %s %d.%d SN %d Mfg %02d/%04d by %d %s",
+		 ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "", 
+		 ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
+		 ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year,
+		 ivar->cid.mid, oidstr);
+}
+
 static const int exp[8] = {
 	1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
 };
 static const int mant[16] = {
-	10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
+	0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
 };
 static const int cur_min[8] = {
 	500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
@@ -1080,13 +1109,8 @@
 {
 	device_printf(dev, "Card at relative address %d%s:\n",
 	    ivar->rca, newcard ? " added" : "");
-	device_printf(dev, " card: %s%s (0x%x/0x%x/\"%s\" rev %d.%d "
-	    "m/d %02d.%04d s/n %08x)\n",
-	    ivar->mode == mode_sd ? "SD" : "MMC",
-	    ivar->high_cap ? " High Capacity" : "", 
-	    ivar->cid.mid, ivar->cid.oid,
-	    ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
-	    ivar->cid.mdt_month, ivar->cid.mdt_year, ivar->cid.psn);
+	device_printf(dev, " card: %s\n",
+		      ivar->card_id_string);
 	device_printf(dev, " bus: %ubit, %uMHz%s\n",
 	    (ivar->bus_width == bus_width_1 ? 1 :
 	    (ivar->bus_width == bus_width_4 ? 4 : 8)),
@@ -1188,6 +1212,7 @@
 			if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
 			    (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
 				ivar->bus_width = bus_width_4;
+			mmc_format_card_id_string(ivar);
 			if (bootverbose || mmc_debug)
 				mmc_log_card(sc->dev, ivar, newcard);
 			if (newcard) {
@@ -1245,6 +1270,7 @@
 			ivar->bus_width = bus_width_1;
 			ivar->timing = bus_timing_normal;
 		}
+		mmc_format_card_id_string(ivar);
 		if (bootverbose || mmc_debug)
 			mmc_log_card(sc->dev, ivar, newcard);
 		if (newcard) {
@@ -1477,6 +1503,9 @@
 	case MMC_IVAR_MAX_DATA:
 		*(int *)result = mmcbr_get_max_data(bus);
 		break;
+	case MMC_IVAR_CARD_ID_STRING:
+		*(char **)result = ivar->card_id_string;
+		break;
 	}
 	return (0);
 }
--- sys/dev/mmc/mmcsd.c	2011-03-15 08:45:51.000000000 -0600
+++ sys/dev/mmc2/mmcsd.c	2011-03-15 07:29:59.000000000 -0600
@@ -68,9 +68,17 @@
 
 #include <dev/mmc/mmcvar.h>
 #include <dev/mmc/mmcreg.h>
-
+#include <dev/mmc/mmcbrvar.h>
 #include "mmcbus_if.h"
 
+/* The kthread_* functions were renamed to kproc_* in FreeBSD 8;
+ * these defines help backport this code to earlier versions.
+ */
+#if __FreeBSD__ < 8
+#define kproc_create   kthread_create
+#define kproc_exit     kthread_exit
+#endif
+
 struct mmcsd_softc {
 	device_t dev;
 	struct mtx sc_mtx;
@@ -95,7 +103,6 @@
 	off_t offset, size_t length);
 static void mmcsd_task(void *arg);
 
-static const char *mmcsd_card_name(device_t dev);
 static int mmcsd_bus_bit_width(device_t dev);
 
 #define MMCSD_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
@@ -122,6 +129,8 @@
 	struct mmcsd_softc *sc;
 	struct disk *d;
 	intmax_t mb;
+	uint32_t speed;
+	uint32_t maxblocks;
 	char unit;
 
 	sc = device_get_softc(dev);
@@ -157,11 +166,21 @@
 		unit = 'G';
 		mb /= 1024;
 	}
-	device_printf(dev, "%ju%cB <%s Memory Card>%s at %s %dMHz/%dbit\n",
-	    mb, unit, mmcsd_card_name(dev),
+        /* Report the clock speed of the underlying hardware, which might be
+         * different than what the card reports due to hardware limitations.
+         * Report how many blocks the hardware transfers at once, but clip the
+         * number to MAXPHYS since the system won't initiate bigger transfers.
+         */
+	speed = mmcbr_get_clock(device_get_parent(dev));
+	maxblocks = mmc_get_max_data(dev);
+	if (maxblocks > MAXPHYS)
+		maxblocks = MAXPHYS;
+	device_printf(dev, "%ju%cB <%s>%s at %s %d.%01dMHz/%dbit/%d-block\n",
+	    mb, unit, mmc_get_card_id_string(dev),
 	    mmc_get_read_only(dev) ? " (read-only)" : "",
 	    device_get_nameunit(device_get_parent(dev)),
-	    mmc_get_tran_speed(dev) / 1000000, mmcsd_bus_bit_width(dev));
+	    speed / 1000000, (speed / 100000) % 10, 
+	    mmcsd_bus_bit_width(dev), maxblocks);
 	disk_create(d, DISK_VERSION);
 	bioq_init(&sc->bio_queue);
 
@@ -500,16 +519,6 @@
 	kproc_exit(0);
 }
 
-static const char *
-mmcsd_card_name(device_t dev)
-{
-	if (mmc_get_card_type(dev) == mode_mmc)
-		return ("MMC");
-	if (mmc_get_high_cap(dev))
-		return ("SDHC");
-	return ("SD");
-}
-
 static int
 mmcsd_bus_bit_width(device_t dev)
 {
--- sys/dev/mmc/mmcreg.h	2011-03-15 08:45:51.000000000 -0600
+++ sys/dev/mmc2/mmcreg.h	2011-03-14 18:10:10.000000000 -0600
@@ -330,6 +330,9 @@
 #define SD_SWITCH_HS_MODE	1
 #define SD_SWITCH_NOCHANGE	0xF
 
+#define SD_CLR_CARD_DETECT      0
+#define SD_SET_CARD_DETECT      1
+
 #define	SD_MAX_HS		50000000
 
 /* OCR bits */
--- sys/dev/mmc/mmcvar.h	2011-03-15 08:45:51.000000000 -0600
+++ sys/dev/mmc2/mmcvar.h	2011-03-15 08:13:20.000000000 -0600
@@ -69,6 +69,7 @@
     MMC_IVAR_BUS_WIDTH,
     MMC_IVAR_ERASE_SECTOR,
     MMC_IVAR_MAX_DATA,
+    MMC_IVAR_CARD_ID_STRING,
 //    MMC_IVAR_,
 };
 
@@ -89,5 +90,6 @@
 MMC_ACCESSOR(bus_width, BUS_WIDTH, int)
 MMC_ACCESSOR(erase_sector, ERASE_SECTOR, int)
 MMC_ACCESSOR(max_data, MAX_DATA, int)
+MMC_ACCESSOR(card_id_string, CARD_ID_STRING, const char *)
 
 #endif /* DEV_MMC_MMCVAR_H */
--- patch-mmcsd ends here ---

>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201104191520.p3JFKQta028382>