Skip to content

Commit b513ea2

Browse files
amd-anathcjb
authored andcommitted
mmc: sd: add support for tuning during uhs initialization
Host Controller needs tuning during initialization to operate SDR50 and SDR104 UHS-I cards. Whether SDR50 mode actually needs tuning is indicated by bit 45 of the Host Controller Capabilities register. A new command CMD19 has been defined in the Physical Layer spec v3.01 to request the card to send tuning pattern. We enable Buffer Read Ready interrupt at the very begining of tuning procedure, because that is the only interrupt generated by the Host Controller during tuning. We program the block size to 64 in the Block Size register. We make sure that DMA Enable and Multi Block Select in the Transfer Mode register are set to 0 before actually sending CMD19. The tuning block is sent by the card to the Host Controller using DAT lines, so we set Data Present Select (bit 5) in the Command register. The Host Controller is responsible for doing the verfication of tuning block sent by the card at the hardware level. After sending CMD19, we wait for Buffer Read Ready interrupt. In case we don't receive an interrupt after the specified timeout value, we fall back on fixed sampling clock by setting Execute Tuning (bit 6) and Sampling Clock Select (bit 7) of Host Control2 register to 0. Before exiting the tuning procedure, we disable Buffer Read Ready interrupt and re-enable other interrupts. Tested by Zhangfei Gao with a Toshiba uhs card and general hs card, on mmp2 in SDMA mode. Signed-off-by: Arindam Nath <arindam.nath@amd.com> Reviewed-by: Philip Rakity <prakity@marvell.com> Tested-by: Philip Rakity <prakity@marvell.com> Acked-by: Zhangfei Gao <zhangfei.gao@marvell.com> Signed-off-by: Chris Ball <cjb@laptop.org>
1 parent 3a30351 commit b513ea2

6 files changed

Lines changed: 182 additions & 1 deletion

File tree

‎drivers/mmc/core/sd.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,12 @@ static int mmc_sd_init_uhs_card(struct mmc_card *card)
618618

619619
/* Set current limit for the card */
620620
err = sd_set_current_limit(card, status);
621+
if (err)
622+
goto out;
623+
624+
/* SPI mode doesn't define CMD19 */
625+
if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning)
626+
err = card->host->ops->execute_tuning(card->host);
621627

622628
out:
623629
kfree(status);

‎drivers/mmc/host/sdhci.c‎

Lines changed: 167 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#define SDHCI_USE_LEDS_CLASS
3939
#endif
4040

41+
#define MAX_TUNING_LOOP 40
42+
4143
static unsigned int debug_quirks = 0;
4244

4345
static void sdhci_finish_data(struct sdhci_host *);
@@ -968,7 +970,9 @@ static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
968970
flags |= SDHCI_CMD_CRC;
969971
if (cmd->flags & MMC_RSP_OPCODE)
970972
flags |= SDHCI_CMD_INDEX;
971-
if (cmd->data)
973+
974+
/* CMD19 is special in that the Data Present Select should be set */
975+
if (cmd->data || (cmd->opcode == MMC_SEND_TUNING_BLOCK))
972976
flags |= SDHCI_CMD_DATA;
973977

974978
sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
@@ -1501,12 +1505,157 @@ static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
15011505
return 0;
15021506
}
15031507

1508+
static int sdhci_execute_tuning(struct mmc_host *mmc)
1509+
{
1510+
struct sdhci_host *host;
1511+
u16 ctrl;
1512+
u32 ier;
1513+
int tuning_loop_counter = MAX_TUNING_LOOP;
1514+
unsigned long timeout;
1515+
int err = 0;
1516+
1517+
host = mmc_priv(mmc);
1518+
1519+
disable_irq(host->irq);
1520+
spin_lock(&host->lock);
1521+
1522+
ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1523+
1524+
/*
1525+
* Host Controller needs tuning only in case of SDR104 mode
1526+
* and for SDR50 mode when Use Tuning for SDR50 is set in
1527+
* Capabilities register.
1528+
*/
1529+
if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
1530+
(((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1531+
(host->flags & SDHCI_SDR50_NEEDS_TUNING)))
1532+
ctrl |= SDHCI_CTRL_EXEC_TUNING;
1533+
else {
1534+
spin_unlock(&host->lock);
1535+
enable_irq(host->irq);
1536+
return 0;
1537+
}
1538+
1539+
sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1540+
1541+
/*
1542+
* As per the Host Controller spec v3.00, tuning command
1543+
* generates Buffer Read Ready interrupt, so enable that.
1544+
*
1545+
* Note: The spec clearly says that when tuning sequence
1546+
* is being performed, the controller does not generate
1547+
* interrupts other than Buffer Read Ready interrupt. But
1548+
* to make sure we don't hit a controller bug, we _only_
1549+
* enable Buffer Read Ready interrupt here.
1550+
*/
1551+
ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1552+
sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1553+
1554+
/*
1555+
* Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1556+
* of loops reaches 40 times or a timeout of 150ms occurs.
1557+
*/
1558+
timeout = 150;
1559+
do {
1560+
struct mmc_command cmd = {0};
1561+
struct mmc_request mrq = {0};
1562+
1563+
if (!tuning_loop_counter && !timeout)
1564+
break;
1565+
1566+
cmd.opcode = MMC_SEND_TUNING_BLOCK;
1567+
cmd.arg = 0;
1568+
cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1569+
cmd.retries = 0;
1570+
cmd.data = NULL;
1571+
cmd.error = 0;
1572+
1573+
mrq.cmd = &cmd;
1574+
host->mrq = &mrq;
1575+
1576+
/*
1577+
* In response to CMD19, the card sends 64 bytes of tuning
1578+
* block to the Host Controller. So we set the block size
1579+
* to 64 here.
1580+
*/
1581+
sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
1582+
1583+
/*
1584+
* The tuning block is sent by the card to the host controller.
1585+
* So we set the TRNS_READ bit in the Transfer Mode register.
1586+
* This also takes care of setting DMA Enable and Multi Block
1587+
* Select in the same register to 0.
1588+
*/
1589+
sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1590+
1591+
sdhci_send_command(host, &cmd);
1592+
1593+
host->cmd = NULL;
1594+
host->mrq = NULL;
1595+
1596+
spin_unlock(&host->lock);
1597+
enable_irq(host->irq);
1598+
1599+
/* Wait for Buffer Read Ready interrupt */
1600+
wait_event_interruptible_timeout(host->buf_ready_int,
1601+
(host->tuning_done == 1),
1602+
msecs_to_jiffies(50));
1603+
disable_irq(host->irq);
1604+
spin_lock(&host->lock);
1605+
1606+
if (!host->tuning_done) {
1607+
printk(KERN_INFO DRIVER_NAME ": Timeout waiting for "
1608+
"Buffer Read Ready interrupt during tuning "
1609+
"procedure, falling back to fixed sampling "
1610+
"clock\n");
1611+
ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1612+
ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1613+
ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1614+
sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1615+
1616+
err = -EIO;
1617+
goto out;
1618+
}
1619+
1620+
host->tuning_done = 0;
1621+
1622+
ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1623+
tuning_loop_counter--;
1624+
timeout--;
1625+
mdelay(1);
1626+
} while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1627+
1628+
/*
1629+
* The Host Driver has exhausted the maximum number of loops allowed,
1630+
* so use fixed sampling frequency.
1631+
*/
1632+
if (!tuning_loop_counter || !timeout) {
1633+
ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1634+
sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1635+
} else {
1636+
if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
1637+
printk(KERN_INFO DRIVER_NAME ": Tuning procedure"
1638+
" failed, falling back to fixed sampling"
1639+
" clock\n");
1640+
err = -EIO;
1641+
}
1642+
}
1643+
1644+
out:
1645+
sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1646+
spin_unlock(&host->lock);
1647+
enable_irq(host->irq);
1648+
1649+
return err;
1650+
}
1651+
15041652
static const struct mmc_host_ops sdhci_ops = {
15051653
.request = sdhci_request,
15061654
.set_ios = sdhci_set_ios,
15071655
.get_ro = sdhci_get_ro,
15081656
.enable_sdio_irq = sdhci_enable_sdio_irq,
15091657
.start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
1658+
.execute_tuning = sdhci_execute_tuning,
15101659
};
15111660

15121661
/*****************************************************************************\
@@ -1724,6 +1873,16 @@ static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
17241873
{
17251874
BUG_ON(intmask == 0);
17261875

1876+
/* CMD19 generates _only_ Buffer Read Ready interrupt */
1877+
if (intmask & SDHCI_INT_DATA_AVAIL) {
1878+
if (SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)) ==
1879+
MMC_SEND_TUNING_BLOCK) {
1880+
host->tuning_done = 1;
1881+
wake_up(&host->buf_ready_int);
1882+
return;
1883+
}
1884+
}
1885+
17271886
if (!host->data) {
17281887
/*
17291888
* The "data complete" interrupt is also used to
@@ -2160,6 +2319,10 @@ int sdhci_add_host(struct sdhci_host *host)
21602319
if (caps[1] & SDHCI_SUPPORT_DDR50)
21612320
mmc->caps |= MMC_CAP_UHS_DDR50;
21622321

2322+
/* Does the host needs tuning for SDR50? */
2323+
if (caps[1] & SDHCI_USE_SDR50_TUNING)
2324+
host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2325+
21632326
/* Driver Type(s) (A, C, D) supported by the host */
21642327
if (caps[1] & SDHCI_DRIVER_TYPE_A)
21652328
mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
@@ -2313,6 +2476,9 @@ int sdhci_add_host(struct sdhci_host *host)
23132476

23142477
setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
23152478

2479+
if (host->version >= SDHCI_SPEC_300)
2480+
init_waitqueue_head(&host->buf_ready_int);
2481+
23162482
ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
23172483
mmc_hostname(mmc), host);
23182484
if (ret)

‎drivers/mmc/host/sdhci.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@
161161
#define SDHCI_CTRL_DRV_TYPE_A 0x0010
162162
#define SDHCI_CTRL_DRV_TYPE_C 0x0020
163163
#define SDHCI_CTRL_DRV_TYPE_D 0x0030
164+
#define SDHCI_CTRL_EXEC_TUNING 0x0040
165+
#define SDHCI_CTRL_TUNED_CLK 0x0080
164166
#define SDHCI_CTRL_PRESET_VAL_ENABLE 0x8000
165167

166168
#define SDHCI_CAPABILITIES 0x40
@@ -188,6 +190,7 @@
188190
#define SDHCI_DRIVER_TYPE_A 0x00000010
189191
#define SDHCI_DRIVER_TYPE_C 0x00000020
190192
#define SDHCI_DRIVER_TYPE_D 0x00000040
193+
#define SDHCI_USE_SDR50_TUNING 0x00002000
191194

192195
#define SDHCI_CAPABILITIES_1 0x44
193196

‎include/linux/mmc/host.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ struct mmc_host_ops {
136136
void (*init_card)(struct mmc_host *host, struct mmc_card *card);
137137

138138
int (*start_signal_voltage_switch)(struct mmc_host *host, struct mmc_ios *ios);
139+
int (*execute_tuning)(struct mmc_host *host);
139140
};
140141

141142
struct mmc_card;

‎include/linux/mmc/mmc.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#define MMC_SET_BLOCKLEN 16 /* ac [31:0] block len R1 */
5151
#define MMC_READ_SINGLE_BLOCK 17 /* adtc [31:0] data addr R1 */
5252
#define MMC_READ_MULTIPLE_BLOCK 18 /* adtc [31:0] data addr R1 */
53+
#define MMC_SEND_TUNING_BLOCK 19 /* adtc R1 */
5354

5455
/* class 3 */
5556
#define MMC_WRITE_DAT_UNTIL_STOP 20 /* adtc [31:0] data addr R1 */

‎include/linux/mmc/sdhci.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ struct sdhci_host {
111111
#define SDHCI_USE_ADMA (1<<1) /* Host is ADMA capable */
112112
#define SDHCI_REQ_USE_DMA (1<<2) /* Use DMA for this req. */
113113
#define SDHCI_DEVICE_DEAD (1<<3) /* Device unresponsive */
114+
#define SDHCI_SDR50_NEEDS_TUNING (1<<4) /* SDR50 needs tuning */
114115

115116
unsigned int version; /* SDHCI spec. version */
116117

@@ -147,6 +148,9 @@ struct sdhci_host {
147148
unsigned int ocr_avail_sd;
148149
unsigned int ocr_avail_mmc;
149150

151+
wait_queue_head_t buf_ready_int; /* Waitqueue for Buffer Read Ready interrupt */
152+
unsigned int tuning_done; /* Condition flag set when CMD19 succeeds */
153+
150154
unsigned long private[0] ____cacheline_aligned;
151155
};
152156
#endif /* __SDHCI_H */

0 commit comments

Comments
 (0)