aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAl Cooper <alcooperx@gmail.com>2014-05-09 11:34:07 -0400
committerChris Ball <chris@printf.net>2014-05-22 08:40:46 -0400
commit7ce45e950624bee09381c3fd727f06262bcb73e2 (patch)
tree5146a958f4bce89a9e5313800d321965b97266d3
parent69f5bf38f93c78faeb93d51dc41adf51e13fe78d (diff)
mmc: sdhci: SD tuning is broken for some controllers
The SD Host Controller spec states that the SD Host Controller can request that the driver send up to 40 CMD19's while doing tuning and that the total time the card spends responding must be < 150ms. The sdhci_execute_tuning() function in sdhci.c that loops through sending the CMD19's has multiple bugs. First it sets a "timeout" variable to 150 and a loop counter variable to 40. It then decrements both variables by 1 at the end of each loop. It tries to handle violations of the count and time by doing a break when BOTH variables are equal to zero, which can never happen because they we set to different values and decremented by 1 at the same time. The timeout variable is not based on time at all and is totally useless. The routine also considers a loop counter of zero to be an error which means that any controller that requests the max of 40 CMD19s will cause tuning to fail and be disabled. I've fixed these issues by allowing up to 40 CMD19's and I've removed any attempt to handle the 150ms time limit. Removing timeout checking seems safe here because each CMD19 is timeout protected and the max loop counters insures we don't loop forever. Adding timeout checking would not be as simple as snapping the time at the loop start and checking for 150ms to pass because the loop queues the CMD19's and uses events to wait for completion so the time would include all the normal scheduler latencies. Signed-off-by: Al Cooper <alcooperx@gmail.com> Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org> Signed-off-by: Chris Ball <chris@printf.net>
-rw-r--r--drivers/mmc/host/sdhci.c24
1 files changed, 9 insertions, 15 deletions
diff --git a/drivers/mmc/host/sdhci.c b/drivers/mmc/host/sdhci.c
index 447eef8217c7..47055f3f01b8 100644
--- a/drivers/mmc/host/sdhci.c
+++ b/drivers/mmc/host/sdhci.c
@@ -1823,7 +1823,6 @@ static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1823 struct sdhci_host *host = mmc_priv(mmc); 1823 struct sdhci_host *host = mmc_priv(mmc);
1824 u16 ctrl; 1824 u16 ctrl;
1825 int tuning_loop_counter = MAX_TUNING_LOOP; 1825 int tuning_loop_counter = MAX_TUNING_LOOP;
1826 unsigned long timeout;
1827 int err = 0; 1826 int err = 0;
1828 unsigned long flags; 1827 unsigned long flags;
1829 1828
@@ -1882,14 +1881,10 @@ static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1882 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number 1881 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1883 * of loops reaches 40 times or a timeout of 150ms occurs. 1882 * of loops reaches 40 times or a timeout of 150ms occurs.
1884 */ 1883 */
1885 timeout = 150;
1886 do { 1884 do {
1887 struct mmc_command cmd = {0}; 1885 struct mmc_command cmd = {0};
1888 struct mmc_request mrq = {NULL}; 1886 struct mmc_request mrq = {NULL};
1889 1887
1890 if (!tuning_loop_counter && !timeout)
1891 break;
1892
1893 cmd.opcode = opcode; 1888 cmd.opcode = opcode;
1894 cmd.arg = 0; 1889 cmd.arg = 0;
1895 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1890 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
@@ -1897,6 +1892,9 @@ static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1897 cmd.data = NULL; 1892 cmd.data = NULL;
1898 cmd.error = 0; 1893 cmd.error = 0;
1899 1894
1895 if (tuning_loop_counter-- == 0)
1896 break;
1897
1900 mrq.cmd = &cmd; 1898 mrq.cmd = &cmd;
1901 host->mrq = &mrq; 1899 host->mrq = &mrq;
1902 1900
@@ -1954,8 +1952,6 @@ static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1954 host->tuning_done = 0; 1952 host->tuning_done = 0;
1955 1953
1956 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); 1954 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1957 tuning_loop_counter--;
1958 timeout--;
1959 1955
1960 /* eMMC spec does not require a delay between tuning cycles */ 1956 /* eMMC spec does not require a delay between tuning cycles */
1961 if (opcode == MMC_SEND_TUNING_BLOCK) 1957 if (opcode == MMC_SEND_TUNING_BLOCK)
@@ -1966,17 +1962,15 @@ static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1966 * The Host Driver has exhausted the maximum number of loops allowed, 1962 * The Host Driver has exhausted the maximum number of loops allowed,
1967 * so use fixed sampling frequency. 1963 * so use fixed sampling frequency.
1968 */ 1964 */
1969 if (!tuning_loop_counter || !timeout) { 1965 if (tuning_loop_counter < 0) {
1970 ctrl &= ~SDHCI_CTRL_TUNED_CLK; 1966 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1971 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2); 1967 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1968 }
1969 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
1970 pr_info(DRIVER_NAME ": Tuning procedure"
1971 " failed, falling back to fixed sampling"
1972 " clock\n");
1972 err = -EIO; 1973 err = -EIO;
1973 } else {
1974 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
1975 pr_info(DRIVER_NAME ": Tuning procedure"
1976 " failed, falling back to fixed sampling"
1977 " clock\n");
1978 err = -EIO;
1979 }
1980 } 1974 }
1981 1975
1982out: 1976out: