[PATCH v3] mmc: Poll CD in case cyclic framework is enabled

Marek Vasut marek.vasut+renesas at mailbox.org
Sun Dec 10 16:02:58 CET 2023


In case the cyclic framework is enabled, poll the card detect of already
initialized cards and deinitialize them in case they are removed. Since
the card initialization is a longer process and card initialization is
done on first access to an uninitialized card anyway, avoid initializing
newly detected uninitialized cards in the cyclic callback.

Signed-off-by: Marek Vasut <marek.vasut+renesas at mailbox.org>
---
Cc: Jaehoon Chung <jh80.chung at samsung.com>
Cc: Peng Fan <peng.fan at nxp.com>
Cc: Simon Glass <sjg at chromium.org>
---
V2: Move the cyclic registration/unregistration into mmc init/deinit
V3: Replace if (CONFIG_IS_ENABLED(CYCLIC)...) with #if as the former
    does not work with structure members
---
 drivers/mmc/mmc.c | 36 ++++++++++++++++++++++++++++++++++++
 include/mmc.h     |  5 +++++
 2 files changed, 41 insertions(+)

diff --git a/drivers/mmc/mmc.c b/drivers/mmc/mmc.c
index eb5010c1465..a5686dbc12e 100644
--- a/drivers/mmc/mmc.c
+++ b/drivers/mmc/mmc.c
@@ -2985,6 +2985,22 @@ static int mmc_complete_init(struct mmc *mmc)
 	return err;
 }
 
+#if CONFIG_IS_ENABLED(CYCLIC)
+static void mmc_cyclic_cd_poll(void *ctx)
+{
+	struct mmc *m = ctx;
+
+	if (!m->has_init)
+		return;
+
+	if (mmc_getcd(m))
+		return;
+
+	mmc_deinit(m);
+	m->has_init = 0;
+}
+#endif
+
 int mmc_init(struct mmc *mmc)
 {
 	int err = 0;
@@ -3007,6 +3023,19 @@ int mmc_init(struct mmc *mmc)
 	if (err)
 		pr_info("%s: %d, time %lu\n", __func__, err, get_timer(start));
 
+#if CONFIG_IS_ENABLED(CYCLIC)
+	if (!mmc->cyclic) {
+		/* Register cyclic function for card detect polling */
+		mmc->cyclic = cyclic_register(mmc_cyclic_cd_poll, 100 * 1000,
+					      mmc->cfg->name, mmc);
+		if (!mmc->cyclic) {
+			printf("Failed to register %s CD poll function\n",
+				mmc->cfg->name);
+			err = -EINVAL;
+		}
+	}
+#endif
+
 	return err;
 }
 
@@ -3014,6 +3043,13 @@ int mmc_deinit(struct mmc *mmc)
 {
 	u32 caps_filtered;
 
+#if CONFIG_IS_ENABLED(CYCLIC)
+	if (mmc->cyclic) {
+		cyclic_unregister(mmc->cyclic);
+		mmc->cyclic = NULL;
+	}
+#endif
+
 	if (!CONFIG_IS_ENABLED(MMC_UHS_SUPPORT) &&
 	    !CONFIG_IS_ENABLED(MMC_HS200_SUPPORT) &&
 	    !CONFIG_IS_ENABLED(MMC_HS400_SUPPORT))
diff --git a/include/mmc.h b/include/mmc.h
index 1022db3ffa7..4256d5567ef 100644
--- a/include/mmc.h
+++ b/include/mmc.h
@@ -14,6 +14,7 @@
 #include <linux/sizes.h>
 #include <linux/compiler.h>
 #include <linux/dma-direction.h>
+#include <cyclic.h>
 #include <part.h>
 
 struct bd_info;
@@ -739,6 +740,10 @@ struct mmc {
 	u8 hs400_tuning;
 
 	enum bus_mode user_speed_mode; /* input speed mode from user */
+
+#if CONFIG_IS_ENABLED(CYCLIC)
+	struct cyclic_info	*cyclic;
+#endif
 };
 
 #if CONFIG_IS_ENABLED(DM_MMC)
-- 
2.42.0



More information about the U-Boot mailing list