mmc: Downgrade SD/MMC from UHS/HS200/HS400 modes before boot
[oweals/u-boot.git] / drivers / mmc / mmc-uclass.c
index bfda942d46d4c3a22f1d1ebfe06f78e001196834..a9c8f335c142de56df370660986347652a51c66c 100644 (file)
@@ -1,8 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (C) 2015 Google, Inc
  * Written by Simon Glass <sjg@chromium.org>
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
@@ -12,8 +11,6 @@
 #include <dm/lists.h>
 #include "mmc_private.h"
 
-DECLARE_GLOBAL_DATA_PTR;
-
 int dm_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
                    struct mmc_data *data)
 {
@@ -63,6 +60,7 @@ void mmc_send_init_stream(struct mmc *mmc)
        dm_mmc_send_init_stream(mmc->dev);
 }
 
+#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT)
 int dm_mmc_wait_dat0(struct udevice *dev, int state, int timeout)
 {
        struct dm_mmc_ops *ops = mmc_get_ops(dev);
@@ -76,6 +74,7 @@ int mmc_wait_dat0(struct mmc *mmc, int state, int timeout)
 {
        return dm_mmc_wait_dat0(mmc->dev, state, timeout);
 }
+#endif
 
 int dm_mmc_get_wp(struct udevice *dev)
 {
@@ -105,6 +104,7 @@ int mmc_getcd(struct mmc *mmc)
        return dm_mmc_get_cd(mmc->dev);
 }
 
+#ifdef MMC_SUPPORTS_TUNING
 int dm_mmc_execute_tuning(struct udevice *dev, uint opcode)
 {
        struct dm_mmc_ops *ops = mmc_get_ops(dev);
@@ -118,6 +118,7 @@ int mmc_execute_tuning(struct mmc *mmc, uint opcode)
 {
        return dm_mmc_execute_tuning(mmc->dev, opcode);
 }
+#endif
 
 int mmc_of_parse(struct udevice *dev, struct mmc_config *cfg)
 {
@@ -136,13 +137,12 @@ int mmc_of_parse(struct udevice *dev, struct mmc_config *cfg)
                cfg->host_caps |= MMC_MODE_1BIT;
                break;
        default:
-               debug("warning: %s invalid bus-width property. using 1-bit\n",
-                     dev_read_name(dev));
-               cfg->host_caps |= MMC_MODE_1BIT;
-               break;
+               dev_err(dev, "Invalid \"bus-width\" value %u!\n", val);
+               return -EINVAL;
        }
 
-       cfg->f_max = dev_read_u32_default(dev, "max-frequency", 52000000);
+       /* f_max is obtained from the optional "max-frequency" property */
+       dev_read_u32(dev, "max-frequency", &cfg->f_max);
 
        if (dev_read_bool(dev, "cap-sd-highspeed"))
                cfg->host_caps |= MMC_CAP(SD_HS);
@@ -166,6 +166,10 @@ int mmc_of_parse(struct udevice *dev, struct mmc_config *cfg)
                cfg->host_caps |= MMC_CAP(MMC_HS_200);
        if (dev_read_bool(dev, "mmc-hs200-1_2v"))
                cfg->host_caps |= MMC_CAP(MMC_HS_200);
+       if (dev_read_bool(dev, "mmc-hs400-1_8v"))
+               cfg->host_caps |= MMC_CAP(MMC_HS_400);
+       if (dev_read_bool(dev, "mmc-hs400-1_2v"))
+               cfg->host_caps |= MMC_CAP(MMC_HS_400);
 
        return 0;
 }
@@ -364,9 +368,22 @@ static int mmc_blk_probe(struct udevice *dev)
        return 0;
 }
 
+#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
+    CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
+    CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
+static int mmc_blk_remove(struct udevice *dev)
+{
+       struct udevice *mmc_dev = dev_get_parent(dev);
+       struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc_dev);
+       struct mmc *mmc = upriv->mmc;
+
+       return mmc_deinit(mmc);
+}
+#endif
+
 static const struct blk_ops mmc_blk_ops = {
        .read   = mmc_bread,
-#ifndef CONFIG_SPL_BUILD
+#if CONFIG_IS_ENABLED(MMC_WRITE)
        .write  = mmc_bwrite,
        .erase  = mmc_berase,
 #endif
@@ -378,6 +395,12 @@ U_BOOT_DRIVER(mmc_blk) = {
        .id             = UCLASS_BLK,
        .ops            = &mmc_blk_ops,
        .probe          = mmc_blk_probe,
+#if CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) || \
+    CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) || \
+    CONFIG_IS_ENABLED(MMC_HS400_SUPPORT)
+       .remove         = mmc_blk_remove,
+       .flags          = DM_FLAG_OS_PREPARE,
+#endif
 };
 #endif /* CONFIG_BLK */