Skip to content

Commit 22113ef

Browse files
Aries Leecjb
authored andcommitted
mmc: Test bus-width for old MMC devices
Some old MMC devices fail with the 4/8 bits the driver tries to use exclusively. This patch adds a test for the given bus setup and falls back to the lower bit mode (until 1-bit mode) when the test fails. [Major rework and refactoring by tiwai] [Quirk addition and many fixes by prakity] Signed-off-by: Aries Lee <arieslee@jmicron.com> Signed-off-by: Takashi Iwai <tiwai@suse.de> Signed-off-by: Philip Rakity <prakity@marvell.com> Tested-by: Philip Rakity <prakity@marvell.com> Signed-off-by: Chris Ball <cjb@laptop.org>
1 parent e6f29a8 commit 22113ef

7 files changed

Lines changed: 159 additions & 30 deletions

File tree

‎drivers/mmc/core/mmc.c‎

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -534,39 +534,57 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
534534
*/
535535
if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
536536
(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
537-
unsigned ext_csd_bit, bus_width;
538-
539-
if (host->caps & MMC_CAP_8_BIT_DATA) {
540-
if (ddr)
541-
ext_csd_bit = EXT_CSD_DDR_BUS_WIDTH_8;
542-
else
543-
ext_csd_bit = EXT_CSD_BUS_WIDTH_8;
544-
bus_width = MMC_BUS_WIDTH_8;
545-
} else {
546-
if (ddr)
547-
ext_csd_bit = EXT_CSD_DDR_BUS_WIDTH_4;
548-
else
549-
ext_csd_bit = EXT_CSD_BUS_WIDTH_4;
550-
bus_width = MMC_BUS_WIDTH_4;
537+
static unsigned ext_csd_bits[][2] = {
538+
{ EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
539+
{ EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
540+
{ EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
541+
};
542+
static unsigned bus_widths[] = {
543+
MMC_BUS_WIDTH_8,
544+
MMC_BUS_WIDTH_4,
545+
MMC_BUS_WIDTH_1
546+
};
547+
unsigned idx, bus_width = 0;
548+
549+
if (host->caps & MMC_CAP_8_BIT_DATA)
550+
idx = 0;
551+
else
552+
idx = 1;
553+
for (; idx < ARRAY_SIZE(bus_widths); idx++) {
554+
bus_width = bus_widths[idx];
555+
if (bus_width == MMC_BUS_WIDTH_1)
556+
ddr = 0; /* no DDR for 1-bit width */
557+
err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
558+
EXT_CSD_BUS_WIDTH,
559+
ext_csd_bits[idx][0]);
560+
if (!err) {
561+
/*
562+
* If controller can't handle bus width test,
563+
* use the highest bus width to maintain
564+
* compatibility with previous MMC behavior.
565+
*/
566+
if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
567+
break;
568+
mmc_set_bus_width_ddr(card->host,
569+
bus_width, MMC_SDR_MODE);
570+
err = mmc_bus_test(card, bus_width);
571+
if (!err)
572+
break;
573+
}
551574
}
552575

553-
err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
554-
EXT_CSD_BUS_WIDTH, ext_csd_bit);
555-
556-
if (err && err != -EBADMSG)
557-
goto free_card;
558-
576+
if (!err && ddr) {
577+
err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
578+
EXT_CSD_BUS_WIDTH,
579+
ext_csd_bits[idx][1]);
580+
}
559581
if (err) {
560582
printk(KERN_WARNING "%s: switch to bus width %d ddr %d "
561-
"failed\n", mmc_hostname(card->host),
562-
1 << bus_width, ddr);
563-
err = 0;
564-
} else {
565-
if (ddr)
566-
mmc_card_set_ddr_mode(card);
567-
else
568-
ddr = MMC_SDR_MODE;
569-
583+
"failed\n", mmc_hostname(card->host),
584+
1 << bus_width, ddr);
585+
goto free_card;
586+
} else if (ddr) {
587+
mmc_card_set_ddr_mode(card);
570588
mmc_set_bus_width_ddr(card->host, bus_width, ddr);
571589
}
572590
}

‎drivers/mmc/core/mmc_ops.c‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,104 @@ int mmc_send_status(struct mmc_card *card, u32 *status)
462462
return 0;
463463
}
464464

465+
static int
466+
mmc_send_bus_test(struct mmc_card *card, struct mmc_host *host, u8 opcode,
467+
u8 len)
468+
{
469+
struct mmc_request mrq;
470+
struct mmc_command cmd;
471+
struct mmc_data data;
472+
struct scatterlist sg;
473+
u8 *data_buf;
474+
u8 *test_buf;
475+
int i, err;
476+
static u8 testdata_8bit[8] = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0 };
477+
static u8 testdata_4bit[4] = { 0x5a, 0, 0, 0 };
478+
479+
/* dma onto stack is unsafe/nonportable, but callers to this
480+
* routine normally provide temporary on-stack buffers ...
481+
*/
482+
data_buf = kmalloc(len, GFP_KERNEL);
483+
if (!data_buf)
484+
return -ENOMEM;
485+
486+
if (len == 8)
487+
test_buf = testdata_8bit;
488+
else if (len == 4)
489+
test_buf = testdata_4bit;
490+
else {
491+
printk(KERN_ERR "%s: Invalid bus_width %d\n",
492+
mmc_hostname(host), len);
493+
kfree(data_buf);
494+
return -EINVAL;
495+
}
496+
497+
if (opcode == MMC_BUS_TEST_W)
498+
memcpy(data_buf, test_buf, len);
499+
500+
memset(&mrq, 0, sizeof(struct mmc_request));
501+
memset(&cmd, 0, sizeof(struct mmc_command));
502+
memset(&data, 0, sizeof(struct mmc_data));
503+
504+
mrq.cmd = &cmd;
505+
mrq.data = &data;
506+
cmd.opcode = opcode;
507+
cmd.arg = 0;
508+
509+
/* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we
510+
* rely on callers to never use this with "native" calls for reading
511+
* CSD or CID. Native versions of those commands use the R2 type,
512+
* not R1 plus a data block.
513+
*/
514+
cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
515+
516+
data.blksz = len;
517+
data.blocks = 1;
518+
if (opcode == MMC_BUS_TEST_R)
519+
data.flags = MMC_DATA_READ;
520+
else
521+
data.flags = MMC_DATA_WRITE;
522+
523+
data.sg = &sg;
524+
data.sg_len = 1;
525+
sg_init_one(&sg, data_buf, len);
526+
mmc_wait_for_req(host, &mrq);
527+
err = 0;
528+
if (opcode == MMC_BUS_TEST_R) {
529+
for (i = 0; i < len / 4; i++)
530+
if ((test_buf[i] ^ data_buf[i]) != 0xff) {
531+
err = -EIO;
532+
break;
533+
}
534+
}
535+
kfree(data_buf);
536+
537+
if (cmd.error)
538+
return cmd.error;
539+
if (data.error)
540+
return data.error;
541+
542+
return err;
543+
}
544+
545+
int mmc_bus_test(struct mmc_card *card, u8 bus_width)
546+
{
547+
int err, width;
548+
549+
if (bus_width == MMC_BUS_WIDTH_8)
550+
width = 8;
551+
else if (bus_width == MMC_BUS_WIDTH_4)
552+
width = 4;
553+
else if (bus_width == MMC_BUS_WIDTH_1)
554+
return 0; /* no need for test */
555+
else
556+
return -EINVAL;
557+
558+
/*
559+
* Ignore errors from BUS_TEST_W. BUS_TEST_R will fail if there
560+
* is a problem. This improves chances that the test will work.
561+
*/
562+
mmc_send_bus_test(card, card->host, MMC_BUS_TEST_W, width);
563+
err = mmc_send_bus_test(card, card->host, MMC_BUS_TEST_R, width);
564+
return err;
565+
}

‎drivers/mmc/core/mmc_ops.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ int mmc_send_cid(struct mmc_host *host, u32 *cid);
2626
int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp);
2727
int mmc_spi_set_crc(struct mmc_host *host, int use_crc);
2828
int mmc_card_sleepawake(struct mmc_host *host, int sleep);
29+
int mmc_bus_test(struct mmc_card *card, u8 bus_width);
2930

3031
#endif
3132

‎drivers/mmc/host/sdhci.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <linux/leds.h>
2525

26+
#include <linux/mmc/mmc.h>
2627
#include <linux/mmc/host.h>
2728

2829
#include "sdhci.h"
@@ -1521,7 +1522,11 @@ static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
15211522

15221523
if (intmask & SDHCI_INT_DATA_TIMEOUT)
15231524
host->data->error = -ETIMEDOUT;
1524-
else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1525+
else if (intmask & SDHCI_INT_DATA_END_BIT)
1526+
host->data->error = -EILSEQ;
1527+
else if ((intmask & SDHCI_INT_DATA_CRC) &&
1528+
SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
1529+
!= MMC_BUS_TEST_R)
15251530
host->data->error = -EILSEQ;
15261531
else if (intmask & SDHCI_INT_ADMA_ERROR) {
15271532
printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));

‎drivers/mmc/host/sdhci.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#define SDHCI_CMD_RESP_SHORT_BUSY 0x03
5353

5454
#define SDHCI_MAKE_CMD(c, f) (((c & 0xff) << 8) | (f & 0xff))
55+
#define SDHCI_GET_CMD(c) ((c>>8) & 0x3f)
5556

5657
#define SDHCI_RESPONSE 0x10
5758

‎include/linux/mmc/host.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ struct mmc_host {
172172
#define MMC_CAP_1_2V_DDR (1 << 12) /* can support */
173173
/* DDR mode at 1.2V */
174174
#define MMC_CAP_POWER_OFF_CARD (1 << 13) /* Can power off after boot */
175+
#define MMC_CAP_BUS_WIDTH_TEST (1 << 14) /* CMD14/CMD19 bus width ok */
175176

176177
mmc_pm_flag_t pm_caps; /* supported pm features */
177178

‎include/linux/mmc/mmc.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
#define MMC_READ_DAT_UNTIL_STOP 11 /* adtc [31:0] dadr R1 */
4141
#define MMC_STOP_TRANSMISSION 12 /* ac R1b */
4242
#define MMC_SEND_STATUS 13 /* ac [31:16] RCA R1 */
43+
#define MMC_BUS_TEST_R 14 /* adtc R1 */
4344
#define MMC_GO_INACTIVE_STATE 15 /* ac [31:16] RCA */
45+
#define MMC_BUS_TEST_W 19 /* adtc R1 */
4446
#define MMC_SPI_READ_OCR 58 /* spi spi_R3 */
4547
#define MMC_SPI_CRC_ON_OFF 59 /* spi [0:0] flag spi_R1 */
4648

0 commit comments

Comments
 (0)