mmc: fsl_esdhc: clean up DM and non-DM code
[oweals/u-boot.git] / drivers / mmc / omap_hsmmc.c
index 4d171f457ec4b6da574724faee2c54f9eeb190b8..bade129aea9d75dd72c3ff82462280a636baea1d 100644 (file)
@@ -47,6 +47,7 @@
 #endif
 #include <dm.h>
 #include <power/regulator.h>
+#include <thermal.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -83,7 +84,6 @@ struct omap_hsmmc_data {
 #if CONFIG_IS_ENABLED(DM_MMC)
        struct gpio_desc cd_gpio;       /* Change Detect GPIO */
        struct gpio_desc wp_gpio;       /* Write Protect GPIO */
-       bool cd_inverted;
 #else
        int cd_gpio;
        int wp_gpio;
@@ -216,6 +216,10 @@ static unsigned char mmc_board_init(struct mmc *mmc)
        /* for cairo board, we need to set up 1.8 Volt bias level on MMC1 */
        pbias_lite &= ~PBIASLITEVMODE0;
 #endif
+#ifdef CONFIG_TARGET_OMAP3_LOGIC
+       /* For Logic PD board, 1.8V bias to go enable gpio127 for mmc_cd */
+       pbias_lite &= ~PBIASLITEVMODE1;
+#endif
 #ifdef CONFIG_MMC_OMAP36XX_PINS
        if (get_cpu_family() == CPU_OMAP36XX) {
                /* Disable extended drain IO before changing PBIAS */
@@ -260,7 +264,7 @@ static unsigned char mmc_board_init(struct mmc *mmc)
        !CONFIG_IS_ENABLED(DM_REGULATOR)
        /* PBIAS config needed for MMC1 only */
        if (mmc_get_blk_desc(mmc)->devnum == 0)
-               vmmc_pbias_config(LDO_VOLT_3V0);
+               vmmc_pbias_config(LDO_VOLT_3V3);
 #endif
 
        return 0;
@@ -414,7 +418,7 @@ static void omap_hsmmc_conf_bus_power(struct mmc *mmc, uint signal_voltage)
 
        switch (signal_voltage) {
        case MMC_SIGNAL_VOLTAGE_330:
-               hctl |= SDVS_3V0;
+               hctl |= SDVS_3V3;
                break;
        case MMC_SIGNAL_VOLTAGE_180:
                hctl |= SDVS_1V8;
@@ -426,8 +430,7 @@ static void omap_hsmmc_conf_bus_power(struct mmc *mmc, uint signal_voltage)
        writel(ac12, &mmc_base->ac12);
 }
 
-#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
-static int omap_hsmmc_wait_dat0(struct udevice *dev, int state, int timeout)
+static int omap_hsmmc_wait_dat0(struct udevice *dev, int state, int timeout_us)
 {
        int ret = -ETIMEDOUT;
        u32 con;
@@ -439,8 +442,8 @@ static int omap_hsmmc_wait_dat0(struct udevice *dev, int state, int timeout)
        con = readl(&mmc_base->con);
        writel(con | CON_CLKEXTFREE | CON_PADEN, &mmc_base->con);
 
-       timeout = DIV_ROUND_UP(timeout, 10); /* check every 10 us. */
-       while (timeout--)       {
+       timeout_us = DIV_ROUND_UP(timeout_us, 10); /* check every 10 us. */
+       while (timeout_us--) {
                dat0_high = !!(readl(&mmc_base->pstate) & PSTATE_DLEV_DAT0);
                if (dat0_high == target_dat0_high) {
                        ret = 0;
@@ -452,7 +455,6 @@ static int omap_hsmmc_wait_dat0(struct udevice *dev, int state, int timeout)
 
        return ret;
 }
-#endif
 
 #if CONFIG_IS_ENABLED(MMC_IO_VOLTAGE)
 #if CONFIG_IS_ENABLED(DM_REGULATOR)
@@ -467,21 +469,21 @@ static int omap_hsmmc_set_io_regulator(struct mmc *mmc, int mV)
                return 0;
 
        /* Disable PBIAS */
-       ret = regulator_set_enable(priv->pbias_supply, false);
-       if (ret && ret != -ENOSYS)
+       ret = regulator_set_enable_if_allowed(priv->pbias_supply, false);
+       if (ret)
                return ret;
 
        /* Turn off IO voltage */
-       ret = regulator_set_enable(mmc->vqmmc_supply, false);
-       if (ret && ret != -ENOSYS)
+       ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, false);
+       if (ret)
                return ret;
        /* Program a new IO voltage value */
        ret = regulator_set_value(mmc->vqmmc_supply, uV);
        if (ret)
                return ret;
        /* Turn on IO voltage */
-       ret = regulator_set_enable(mmc->vqmmc_supply, true);
-       if (ret && ret != -ENOSYS)
+       ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, true);
+       if (ret)
                return ret;
 
        /* Program PBIAS voltage*/
@@ -489,8 +491,8 @@ static int omap_hsmmc_set_io_regulator(struct mmc *mmc, int mV)
        if (ret && ret != -ENOSYS)
                return ret;
        /* Enable PBIAS */
-       ret = regulator_set_enable(priv->pbias_supply, true);
-       if (ret && ret != -ENOSYS)
+       ret = regulator_set_enable_if_allowed(priv->pbias_supply, true);
+       if (ret)
                return ret;
 
        return 0;
@@ -510,10 +512,9 @@ static int omap_hsmmc_set_signal_voltage(struct mmc *mmc)
                return -EINVAL;
 
        if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
-               /* Use 3.0V rather than 3.3V */
-               mv = 3000;
-               capa_mask = VS30_3V0SUP;
-               palmas_ldo_volt = LDO_VOLT_3V0;
+               mv = 3300;
+               capa_mask = VS33_3V3SUP;
+               palmas_ldo_volt = LDO_VOLT_3V3;
        } else if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
                capa_mask = VS18_1V8SUP;
                palmas_ldo_volt = LDO_VOLT_1V8;
@@ -552,13 +553,13 @@ static uint32_t omap_hsmmc_set_capabilities(struct mmc *mmc)
        val = readl(&mmc_base->capa);
 
        if (priv->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
-               val |= (VS30_3V0SUP | VS18_1V8SUP);
+               val |= (VS33_3V3SUP | VS18_1V8SUP);
        } else if (priv->controller_flags & OMAP_HSMMC_NO_1_8_V) {
-               val |= VS30_3V0SUP;
+               val |= VS33_3V3SUP;
                val &= ~VS18_1V8SUP;
        } else {
                val |= VS18_1V8SUP;
-               val &= ~VS30_3V0SUP;
+               val &= ~VS33_3V3SUP;
        }
 
        writel(val, &mmc_base->capa);
@@ -619,6 +620,10 @@ static int omap_hsmmc_execute_tuning(struct udevice *dev, uint opcode)
        u32 phase_delay = 0;
        u32 start_window = 0, max_window = 0;
        u32 length = 0, max_len = 0;
+       bool single_point_failure = false;
+       struct udevice *thermal_dev;
+       int temperature;
+       int i;
 
        mmc_base = priv->base_addr;
        val = readl(&mmc_base->capa2);
@@ -629,9 +634,25 @@ static int omap_hsmmc_execute_tuning(struct udevice *dev, uint opcode)
              ((mmc->selected_mode == UHS_SDR50) && (val & CAPA2_TSDR50))))
                return 0;
 
+       ret = uclass_first_device(UCLASS_THERMAL, &thermal_dev);
+       if (ret) {
+               printf("Couldn't get thermal device for tuning\n");
+               return ret;
+       }
+       ret = thermal_get_temp(thermal_dev, &temperature);
+       if (ret) {
+               printf("Couldn't get temperature for tuning\n");
+               return ret;
+       }
        val = readl(&mmc_base->dll);
        val |= DLL_SWT;
        writel(val, &mmc_base->dll);
+
+       /*
+        * Stage 1: Search for a maximum pass window ignoring any
+        * any single point failures. If the tuning value ends up
+        * near it, move away from it in stage 2 below
+        */
        while (phase_delay <= MAX_PHASE_DELAY) {
                omap_hsmmc_set_dll(mmc, phase_delay);
 
@@ -640,10 +661,16 @@ static int omap_hsmmc_execute_tuning(struct udevice *dev, uint opcode)
                if (cur_match) {
                        if (prev_match) {
                                length++;
+                       } else if (single_point_failure) {
+                               /* ignore single point failure */
+                               length++;
+                               single_point_failure = false;
                        } else {
                                start_window = phase_delay;
                                length = 1;
                        }
+               } else {
+                       single_point_failure = prev_match;
                }
 
                if (length > max_len) {
@@ -665,8 +692,71 @@ static int omap_hsmmc_execute_tuning(struct udevice *dev, uint opcode)
                ret = -EIO;
                goto tuning_error;
        }
+       /*
+        * Assign tuning value as a ratio of maximum pass window based
+        * on temperature
+        */
+       if (temperature < -20000)
+               phase_delay = min(max_window + 4 * max_len - 24,
+                                 max_window +
+                                 DIV_ROUND_UP(13 * max_len, 16) * 4);
+       else if (temperature < 20000)
+               phase_delay = max_window + DIV_ROUND_UP(9 * max_len, 16) * 4;
+       else if (temperature < 40000)
+               phase_delay = max_window + DIV_ROUND_UP(8 * max_len, 16) * 4;
+       else if (temperature < 70000)
+               phase_delay = max_window + DIV_ROUND_UP(7 * max_len, 16) * 4;
+       else if (temperature < 90000)
+               phase_delay = max_window + DIV_ROUND_UP(5 * max_len, 16) * 4;
+       else if (temperature < 120000)
+               phase_delay = max_window + DIV_ROUND_UP(4 * max_len, 16) * 4;
+       else
+               phase_delay = max_window + DIV_ROUND_UP(3 * max_len, 16) * 4;
+
+       /*
+        * Stage 2: Search for a single point failure near the chosen tuning
+        * value in two steps. First in the +3 to +10 range and then in the
+        * +2 to -10 range. If found, move away from it in the appropriate
+        * direction by the appropriate amount depending on the temperature.
+        */
+       for (i = 3; i <= 10; i++) {
+               omap_hsmmc_set_dll(mmc, phase_delay + i);
+               if (mmc_send_tuning(mmc, opcode, NULL)) {
+                       if (temperature < 10000)
+                               phase_delay += i + 6;
+                       else if (temperature < 20000)
+                               phase_delay += i - 12;
+                       else if (temperature < 70000)
+                               phase_delay += i - 8;
+                       else if (temperature < 90000)
+                               phase_delay += i - 6;
+                       else
+                               phase_delay += i - 6;
+
+                       goto single_failure_found;
+               }
+       }
+
+       for (i = 2; i >= -10; i--) {
+               omap_hsmmc_set_dll(mmc, phase_delay + i);
+               if (mmc_send_tuning(mmc, opcode, NULL)) {
+                       if (temperature < 10000)
+                               phase_delay += i + 12;
+                       else if (temperature < 20000)
+                               phase_delay += i + 8;
+                       else if (temperature < 70000)
+                               phase_delay += i + 8;
+                       else if (temperature < 90000)
+                               phase_delay += i + 10;
+                       else
+                               phase_delay += i + 12;
+
+                       goto single_failure_found;
+               }
+       }
+
+single_failure_found:
 
-       phase_delay = max_window + 4 * ((3 * max_len) >> 2);
        omap_hsmmc_set_dll(mmc, phase_delay);
 
        mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
@@ -683,14 +773,6 @@ tuning_error:
        return ret;
 }
 #endif
-
-static void omap_hsmmc_send_init_stream(struct udevice *dev)
-{
-       struct omap_hsmmc_data *priv = dev_get_priv(dev);
-       struct hsmmc *mmc_base = priv->base_addr;
-
-       mmc_init_stream(mmc_base);
-}
 #endif
 
 static void mmc_enable_irq(struct mmc *mmc, struct mmc_cmd *cmd)
@@ -749,11 +831,11 @@ static int omap_hsmmc_init_setup(struct mmc *mmc)
 
 #if CONFIG_IS_ENABLED(DM_MMC)
        reg_val = omap_hsmmc_set_capabilities(mmc);
-       omap_hsmmc_conf_bus_power(mmc, (reg_val & VS30_3V0SUP) ?
+       omap_hsmmc_conf_bus_power(mmc, (reg_val & VS33_3V3SUP) ?
                          MMC_SIGNAL_VOLTAGE_330 : MMC_SIGNAL_VOLTAGE_180);
 #else
        writel(DTW_1_BITMODE | SDBP_PWROFF | SDVS_3V0, &mmc_base->hctl);
-       writel(readl(&mmc_base->capa) | VS30_3V0SUP | VS18_1V8SUP,
+       writel(readl(&mmc_base->capa) | VS33_3V3SUP | VS18_1V8SUP,
                &mmc_base->capa);
 #endif
 
@@ -973,18 +1055,17 @@ static int omap_hsmmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
                if (get_timer(0) - start > MAX_RETRY_MS) {
                        printf("%s: timedout waiting on cmd inhibit to clear\n",
                                        __func__);
+                       mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
+                       mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
                        return -ETIMEDOUT;
                }
        }
        writel(0xFFFFFFFF, &mmc_base->stat);
-       start = get_timer(0);
-       while (readl(&mmc_base->stat)) {
-               if (get_timer(0) - start > MAX_RETRY_MS) {
-                       printf("%s: timedout waiting for STAT (%x) to clear\n",
-                               __func__, readl(&mmc_base->stat));
-                       return -ETIMEDOUT;
-               }
+       if (readl(&mmc_base->stat)) {
+               mmc_reset_controller_fsm(mmc_base, SYSCTL_SRD);
+               mmc_reset_controller_fsm(mmc_base, SYSCTL_SRC);
        }
+
        /*
         * CMDREG
         * CMDIDX[13:8] : Command index
@@ -1364,17 +1445,15 @@ static int omap_hsmmc_set_ios(struct udevice *dev)
 #if CONFIG_IS_ENABLED(DM_MMC)
 static int omap_hsmmc_getcd(struct udevice *dev)
 {
-       struct omap_hsmmc_data *priv = dev_get_priv(dev);
        int value = -1;
 #if CONFIG_IS_ENABLED(DM_GPIO)
+       struct omap_hsmmc_data *priv = dev_get_priv(dev);
        value = dm_gpio_get_value(&priv->cd_gpio);
 #endif
        /* if no CD return as 1 */
        if (value < 0)
                return 1;
 
-       if (priv->cd_inverted)
-               return !value;
        return value;
 }
 
@@ -1432,10 +1511,7 @@ static const struct dm_mmc_ops omap_hsmmc_ops = {
 #ifdef MMC_SUPPORTS_TUNING
        .execute_tuning = omap_hsmmc_execute_tuning,
 #endif
-       .send_init_stream       = omap_hsmmc_send_init_stream,
-#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
        .wait_dat0      = omap_hsmmc_wait_dat0,
-#endif
 };
 #else
 static const struct mmc_ops omap_hsmmc_ops = {
@@ -1856,10 +1932,6 @@ static int omap_hsmmc_ofdata_to_platdata(struct udevice *dev)
        }
 #endif
 
-#ifdef OMAP_HSMMC_USE_GPIO
-       plat->cd_inverted = fdtdec_get_bool(fdt, node, "cd-inverted");
-#endif
-
        return 0;
 }
 #endif
@@ -1888,9 +1960,6 @@ static int omap_hsmmc_probe(struct udevice *dev)
        priv->base_addr = plat->base_addr;
        priv->controller_flags = plat->controller_flags;
        priv->hw_rev = plat->hw_rev;
-#ifdef OMAP_HSMMC_USE_GPIO
-       priv->cd_inverted = plat->cd_inverted;
-#endif
 
 #ifdef CONFIG_BLK
        mmc = plat->mmc;
@@ -1959,6 +2028,8 @@ U_BOOT_DRIVER(omap_hsmmc) = {
        .ops = &omap_hsmmc_ops,
        .probe  = omap_hsmmc_probe,
        .priv_auto_alloc_size = sizeof(struct omap_hsmmc_data),
+#if !CONFIG_IS_ENABLED(OF_CONTROL)
        .flags  = DM_FLAG_PRE_RELOC,
+#endif
 };
 #endif