Skip to content

Commit f2119df

Browse files
amd-anathcjb
authored andcommitted
mmc: sd: add support for signal voltage switch procedure
Host Controller v3.00 adds another Capabilities register. Apart from other things, this new register indicates whether the Host Controller supports SDR50, SDR104, and DDR50 UHS-I modes. The spec doesn't mention about explicit support for SDR12 and SDR25 UHS-I modes, so the Host Controller v3.00 should support them by default. Also if the controller supports SDR104 mode, it will also support SDR50 mode as well. So depending on the host support, we set the corresponding MMC_CAP_* flags. One more new register. Host Control2 is added in v3.00, which is used during Signal Voltage Switch procedure described below. Since as per v3.00 spec, UHS-I supported hosts should set S18R to 1, we set S18R (bit 24) of OCR before sending ACMD41. We also need to set XPC (bit 28) of OCR in case the host can supply >150mA. This support is indicated by the Maximum Current Capabilities register of the Host Controller. If the response of ACMD41 has both CCS and S18A set, we start the signal voltage switch procedure, which if successfull, will switch the card from 3.3V signalling to 1.8V signalling. Signal voltage switch procedure adds support for a new command CMD11 in the Physical Layer Spec v3.01. As part of this procedure, we need to set 1.8V Signalling Enable (bit 3) of Host Control2 register, which if remains set after 5ms, means the switch to 1.8V signalling is successfull. Otherwise, we clear bit 24 of OCR and retry the initialization sequence. When we remove the card, and insert the same or another card, we need to make sure that we start with 3.3V signalling voltage. So we call mmc_set_signal_voltage() with MMC_SIGNAL_VOLTAGE_330 set so that we are back to 3.3V signalling voltage before we actually start initializing the card. Tested by Zhangfei Gao with a Toshiba uhs card and general hs card, on mmp2 in SDMA mode. Signed-off-by: Arindam Nath <[email protected]> Reviewed-by: Philip Rakity <[email protected]> Tested-by: Philip Rakity <[email protected]> Acked-by: Zhangfei Gao <[email protected]> Signed-off-by: Chris Ball <[email protected]>
1 parent cb87ea2 commit f2119df

File tree

7 files changed

+284
-17
lines changed

7 files changed

+284
-17
lines changed

drivers/mmc/core/core.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,38 @@ u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
942942
return ocr;
943943
}
944944

945+
int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
946+
{
947+
struct mmc_command cmd = {0};
948+
int err = 0;
949+
950+
BUG_ON(!host);
951+
952+
/*
953+
* Send CMD11 only if the request is to switch the card to
954+
* 1.8V signalling.
955+
*/
956+
if (signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
957+
cmd.opcode = SD_SWITCH_VOLTAGE;
958+
cmd.arg = 0;
959+
cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
960+
961+
err = mmc_wait_for_cmd(host, &cmd, 0);
962+
if (err)
963+
return err;
964+
965+
if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
966+
return -EIO;
967+
}
968+
969+
host->ios.signal_voltage = signal_voltage;
970+
971+
if (host->ops->start_signal_voltage_switch)
972+
err = host->ops->start_signal_voltage_switch(host, &host->ios);
973+
974+
return err;
975+
}
976+
945977
/*
946978
* Select timing parameters for host.
947979
*/

drivers/mmc/core/core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ void mmc_set_bus_width(struct mmc_host *host, unsigned int width);
4141
void mmc_set_bus_width_ddr(struct mmc_host *host, unsigned int width,
4242
unsigned int ddr);
4343
u32 mmc_select_voltage(struct mmc_host *host, u32 ocr);
44+
int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage);
4445
void mmc_set_timing(struct mmc_host *host, unsigned int timing);
4546

4647
static inline void mmc_delay(unsigned int ms)

drivers/mmc/core/sd.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ struct device_type sd_type = {
403403
int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid)
404404
{
405405
int err;
406+
u32 rocr;
406407

407408
/*
408409
* Since we're changing the OCR value, we seem to
@@ -420,12 +421,38 @@ int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid)
420421
*/
421422
err = mmc_send_if_cond(host, ocr);
422423
if (!err)
423-
ocr |= 1 << 30;
424+
ocr |= SD_OCR_CCS;
424425

425-
err = mmc_send_app_op_cond(host, ocr, NULL);
426+
/*
427+
* If the host supports one of UHS-I modes, request the card
428+
* to switch to 1.8V signaling level.
429+
*/
430+
if (host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
431+
MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))
432+
ocr |= SD_OCR_S18R;
433+
434+
/* If the host can supply more than 150mA, XPC should be set to 1. */
435+
if (host->caps & (MMC_CAP_SET_XPC_330 | MMC_CAP_SET_XPC_300 |
436+
MMC_CAP_SET_XPC_180))
437+
ocr |= SD_OCR_XPC;
438+
439+
try_again:
440+
err = mmc_send_app_op_cond(host, ocr, &rocr);
426441
if (err)
427442
return err;
428443

444+
/*
445+
* In case CCS and S18A in the response is set, start Signal Voltage
446+
* Switch procedure. SPI mode doesn't support CMD11.
447+
*/
448+
if (!mmc_host_is_spi(host) && ((rocr & 0x41000000) == 0x41000000)) {
449+
err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
450+
if (err) {
451+
ocr &= ~SD_OCR_S18R;
452+
goto try_again;
453+
}
454+
}
455+
429456
if (mmc_host_is_spi(host))
430457
err = mmc_send_cid(host, cid);
431458
else
@@ -773,6 +800,11 @@ int mmc_attach_sd(struct mmc_host *host)
773800
BUG_ON(!host);
774801
WARN_ON(!host->claimed);
775802

803+
/* Make sure we are at 3.3V signalling voltage */
804+
err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
805+
if (err)
806+
return err;
807+
776808
err = mmc_send_app_op_cond(host, 0, &ocr);
777809
if (err)
778810
return err;

drivers/mmc/host/sdhci.c

Lines changed: 179 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ static void sdhci_dumpregs(struct sdhci_host *host)
8383
printk(KERN_DEBUG DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
8484
sdhci_readw(host, SDHCI_COMMAND),
8585
sdhci_readl(host, SDHCI_MAX_CURRENT));
86+
printk(KERN_DEBUG DRIVER_NAME ": Host ctl2: 0x%08x\n",
87+
sdhci_readw(host, SDHCI_HOST_CONTROL2));
8688

8789
if (host->flags & SDHCI_USE_ADMA)
8890
printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
@@ -1323,11 +1325,114 @@ static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
13231325
spin_unlock_irqrestore(&host->lock, flags);
13241326
}
13251327

1328+
static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1329+
struct mmc_ios *ios)
1330+
{
1331+
struct sdhci_host *host;
1332+
u8 pwr;
1333+
u16 clk, ctrl;
1334+
u32 present_state;
1335+
1336+
host = mmc_priv(mmc);
1337+
1338+
/*
1339+
* Signal Voltage Switching is only applicable for Host Controllers
1340+
* v3.00 and above.
1341+
*/
1342+
if (host->version < SDHCI_SPEC_300)
1343+
return 0;
1344+
1345+
/*
1346+
* We first check whether the request is to set signalling voltage
1347+
* to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1348+
*/
1349+
ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1350+
if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1351+
/* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1352+
ctrl &= ~SDHCI_CTRL_VDD_180;
1353+
sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1354+
1355+
/* Wait for 5ms */
1356+
usleep_range(5000, 5500);
1357+
1358+
/* 3.3V regulator output should be stable within 5 ms */
1359+
ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1360+
if (!(ctrl & SDHCI_CTRL_VDD_180))
1361+
return 0;
1362+
else {
1363+
printk(KERN_INFO DRIVER_NAME ": Switching to 3.3V "
1364+
"signalling voltage failed\n");
1365+
return -EIO;
1366+
}
1367+
} else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1368+
(ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1369+
/* Stop SDCLK */
1370+
clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1371+
clk &= ~SDHCI_CLOCK_CARD_EN;
1372+
sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1373+
1374+
/* Check whether DAT[3:0] is 0000 */
1375+
present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1376+
if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1377+
SDHCI_DATA_LVL_SHIFT)) {
1378+
/*
1379+
* Enable 1.8V Signal Enable in the Host Control2
1380+
* register
1381+
*/
1382+
ctrl |= SDHCI_CTRL_VDD_180;
1383+
sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1384+
1385+
/* Wait for 5ms */
1386+
usleep_range(5000, 5500);
1387+
1388+
ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1389+
if (ctrl & SDHCI_CTRL_VDD_180) {
1390+
/* Provide SDCLK again and wait for 1ms*/
1391+
clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1392+
clk |= SDHCI_CLOCK_CARD_EN;
1393+
sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1394+
usleep_range(1000, 1500);
1395+
1396+
/*
1397+
* If DAT[3:0] level is 1111b, then the card
1398+
* was successfully switched to 1.8V signaling.
1399+
*/
1400+
present_state = sdhci_readl(host,
1401+
SDHCI_PRESENT_STATE);
1402+
if ((present_state & SDHCI_DATA_LVL_MASK) ==
1403+
SDHCI_DATA_LVL_MASK)
1404+
return 0;
1405+
}
1406+
}
1407+
1408+
/*
1409+
* If we are here, that means the switch to 1.8V signaling
1410+
* failed. We power cycle the card, and retry initialization
1411+
* sequence by setting S18R to 0.
1412+
*/
1413+
pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1414+
pwr &= ~SDHCI_POWER_ON;
1415+
sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1416+
1417+
/* Wait for 1ms as per the spec */
1418+
usleep_range(1000, 1500);
1419+
pwr |= SDHCI_POWER_ON;
1420+
sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1421+
1422+
printk(KERN_INFO DRIVER_NAME ": Switching to 1.8V signalling "
1423+
"voltage failed, retrying with S18R set to 0\n");
1424+
return -EAGAIN;
1425+
} else
1426+
/* No signal voltage switch required */
1427+
return 0;
1428+
}
1429+
13261430
static const struct mmc_host_ops sdhci_ops = {
13271431
.request = sdhci_request,
13281432
.set_ios = sdhci_set_ios,
13291433
.get_ro = sdhci_get_ro,
13301434
.enable_sdio_irq = sdhci_enable_sdio_irq,
1435+
.start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
13311436
};
13321437

13331438
/*****************************************************************************\
@@ -1808,7 +1913,9 @@ EXPORT_SYMBOL_GPL(sdhci_alloc_host);
18081913
int sdhci_add_host(struct sdhci_host *host)
18091914
{
18101915
struct mmc_host *mmc;
1811-
unsigned int caps, ocr_avail;
1916+
u32 caps[2];
1917+
u32 max_current_caps;
1918+
unsigned int ocr_avail;
18121919
int ret;
18131920

18141921
WARN_ON(host == NULL);
@@ -1831,12 +1938,15 @@ int sdhci_add_host(struct sdhci_host *host)
18311938
host->version);
18321939
}
18331940

1834-
caps = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
1941+
caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
18351942
sdhci_readl(host, SDHCI_CAPABILITIES);
18361943

1944+
caps[1] = (host->version >= SDHCI_SPEC_300) ?
1945+
sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
1946+
18371947
if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
18381948
host->flags |= SDHCI_USE_SDMA;
1839-
else if (!(caps & SDHCI_CAN_DO_SDMA))
1949+
else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
18401950
DBG("Controller doesn't have SDMA capability\n");
18411951
else
18421952
host->flags |= SDHCI_USE_SDMA;
@@ -1847,7 +1957,8 @@ int sdhci_add_host(struct sdhci_host *host)
18471957
host->flags &= ~SDHCI_USE_SDMA;
18481958
}
18491959

1850-
if ((host->version >= SDHCI_SPEC_200) && (caps & SDHCI_CAN_DO_ADMA2))
1960+
if ((host->version >= SDHCI_SPEC_200) &&
1961+
(caps[0] & SDHCI_CAN_DO_ADMA2))
18511962
host->flags |= SDHCI_USE_ADMA;
18521963

18531964
if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
@@ -1897,10 +2008,10 @@ int sdhci_add_host(struct sdhci_host *host)
18972008
}
18982009

18992010
if (host->version >= SDHCI_SPEC_300)
1900-
host->max_clk = (caps & SDHCI_CLOCK_V3_BASE_MASK)
2011+
host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
19012012
>> SDHCI_CLOCK_BASE_SHIFT;
19022013
else
1903-
host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK)
2014+
host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
19042015
>> SDHCI_CLOCK_BASE_SHIFT;
19052016

19062017
host->max_clk *= 1000000;
@@ -1916,7 +2027,7 @@ int sdhci_add_host(struct sdhci_host *host)
19162027
}
19172028

19182029
host->timeout_clk =
1919-
(caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2030+
(caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
19202031
if (host->timeout_clk == 0) {
19212032
if (host->ops->get_timeout_clock) {
19222033
host->timeout_clk = host->ops->get_timeout_clock(host);
@@ -1928,7 +2039,7 @@ int sdhci_add_host(struct sdhci_host *host)
19282039
return -ENODEV;
19292040
}
19302041
}
1931-
if (caps & SDHCI_TIMEOUT_CLK_UNIT)
2042+
if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
19322043
host->timeout_clk *= 1000;
19332044

19342045
/*
@@ -1955,21 +2066,76 @@ int sdhci_add_host(struct sdhci_host *host)
19552066
if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
19562067
mmc->caps |= MMC_CAP_4_BIT_DATA;
19572068

1958-
if (caps & SDHCI_CAN_DO_HISPD)
2069+
if (caps[0] & SDHCI_CAN_DO_HISPD)
19592070
mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
19602071

19612072
if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
19622073
mmc_card_is_removable(mmc))
19632074
mmc->caps |= MMC_CAP_NEEDS_POLL;
19642075

2076+
/* UHS-I mode(s) supported by the host controller. */
2077+
if (host->version >= SDHCI_SPEC_300)
2078+
mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2079+
2080+
/* SDR104 supports also implies SDR50 support */
2081+
if (caps[1] & SDHCI_SUPPORT_SDR104)
2082+
mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2083+
else if (caps[1] & SDHCI_SUPPORT_SDR50)
2084+
mmc->caps |= MMC_CAP_UHS_SDR50;
2085+
2086+
if (caps[1] & SDHCI_SUPPORT_DDR50)
2087+
mmc->caps |= MMC_CAP_UHS_DDR50;
2088+
19652089
ocr_avail = 0;
1966-
if (caps & SDHCI_CAN_VDD_330)
2090+
/*
2091+
* According to SD Host Controller spec v3.00, if the Host System
2092+
* can afford more than 150mA, Host Driver should set XPC to 1. Also
2093+
* the value is meaningful only if Voltage Support in the Capabilities
2094+
* register is set. The actual current value is 4 times the register
2095+
* value.
2096+
*/
2097+
max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
2098+
2099+
if (caps[0] & SDHCI_CAN_VDD_330) {
2100+
int max_current_330;
2101+
19672102
ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
1968-
if (caps & SDHCI_CAN_VDD_300)
2103+
2104+
max_current_330 = ((max_current_caps &
2105+
SDHCI_MAX_CURRENT_330_MASK) >>
2106+
SDHCI_MAX_CURRENT_330_SHIFT) *
2107+
SDHCI_MAX_CURRENT_MULTIPLIER;
2108+
2109+
if (max_current_330 > 150)
2110+
mmc->caps |= MMC_CAP_SET_XPC_330;
2111+
}
2112+
if (caps[0] & SDHCI_CAN_VDD_300) {
2113+
int max_current_300;
2114+
19692115
ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
1970-
if (caps & SDHCI_CAN_VDD_180)
2116+
2117+
max_current_300 = ((max_current_caps &
2118+
SDHCI_MAX_CURRENT_300_MASK) >>
2119+
SDHCI_MAX_CURRENT_300_SHIFT) *
2120+
SDHCI_MAX_CURRENT_MULTIPLIER;
2121+
2122+
if (max_current_300 > 150)
2123+
mmc->caps |= MMC_CAP_SET_XPC_300;
2124+
}
2125+
if (caps[0] & SDHCI_CAN_VDD_180) {
2126+
int max_current_180;
2127+
19712128
ocr_avail |= MMC_VDD_165_195;
19722129

2130+
max_current_180 = ((max_current_caps &
2131+
SDHCI_MAX_CURRENT_180_MASK) >>
2132+
SDHCI_MAX_CURRENT_180_SHIFT) *
2133+
SDHCI_MAX_CURRENT_MULTIPLIER;
2134+
2135+
if (max_current_180 > 150)
2136+
mmc->caps |= MMC_CAP_SET_XPC_180;
2137+
}
2138+
19732139
mmc->ocr_avail = ocr_avail;
19742140
mmc->ocr_avail_sdio = ocr_avail;
19752141
if (host->ocr_avail_sdio)
@@ -2029,7 +2195,7 @@ int sdhci_add_host(struct sdhci_host *host)
20292195
if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
20302196
mmc->max_blk_size = 2;
20312197
} else {
2032-
mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
2198+
mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
20332199
SDHCI_MAX_BLOCK_SHIFT;
20342200
if (mmc->max_blk_size >= 3) {
20352201
printk(KERN_WARNING "%s: Invalid maximum block size, "

0 commit comments

Comments
 (0)