Skip to contentSkip to conversation

mmc-utils: add ability to flush optional eMMC cache

PATCH In review Michele Dionisio 4 participants 4 messages Jul 14, 2025 – Jul 15, 2025 MMC, SD and SDIO

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mmc-utils: add ability to flush optional eMMC cache

The MMC 5.0 instroduce command to flush cache. This feature can be use on embedded device there power is not stable.

---
Changes since v1:
- Remove redundant cover letter
- Add docs/HOWTO.rst
- add check to not flush if cache is disabled

 docs/HOWTO.rst |  5 +++++
 man/mmc.1      |  4 ++++
 mmc.c          |  5 +++++
 mmc.h          |  1 +
 mmc_cmds.c     | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++
 mmc_cmds.h     |  1 +
 6 files changed, 68 insertions(+)
docs/HOWTO.rst +5 -0
index f2f2f35..d454d8c 100644
@@ -107,3 +107,8 @@ Running mmc-utils
     ``mmc rpmb secure-wp-enable <rpmb device> <key file>``
         Disable updating WP related EXT_CSD and CSD fields.
         Applicable only if secure write protect mode is enabled.
+
+
+    ``mmc cache flush <device>``
+        Flush the eMMC cache for <device>.
+        Applicable only if device version >= eMMC5.0.
man/mmc.1 +4 -0
index bccabf3..665fe6d 100644
@@ -109,6 +109,10 @@ NOTE! The cache is an optional feature on devices >= eMMC4.5.
 Disable the eMMC cache feature on <device>.
 NOTE! The cache is an optional feature on devices >= eMMC4.5.
 .TP
+.BR "cache flush <device>"
+Flush the eMMC cache on <device>.
+NOTE! The cache is an optional feature on devices >= eMMC5.0.
+.TP
 .BR "<cmd> --help"
 Show detailed help for a command or subset of commands.
 
mmc.c +5 -0
index 6770a45..fce7eef 100644
@@ -231,6 +231,11 @@ static struct Command commands[] = {
 		"Disable the eMMC cache feature on <device>.\n"
 		"NOTE! The cache is an optional feature on devices >= eMMC4.5.",
 	},
+	{ do_cache_flush, 1,
+	  "cache flush", "<device>\n"
+		"flush the eMMC cache <device>.\n"
+		"NOTE! The cache is an optional feature on devices >= eMMC5.0.",
+	},
 	{ do_read_csd, -1,
 	  "csd read", "<device path>\n"
 		  "Print CSD data from <device path>.\n"
mmc.h +1 -0
index 9fc22ec..2bb346b 100644
@@ -152,6 +152,7 @@
 #define EXT_CSD_DATA_SECTOR_SIZE	61 /* R */
 #define EXT_CSD_EXT_PARTITIONS_ATTRIBUTE_1	53
 #define EXT_CSD_EXT_PARTITIONS_ATTRIBUTE_0	52
+#define EXT_CSD_FLUSH_CACHE     32
 #define EXT_CSD_CACHE_CTRL		33
 #define EXT_CSD_MODE_CONFIG		30
 #define EXT_CSD_MODE_OPERATION_CODES	29	/* W */
mmc_cmds.c +52 -0

58 lines not on this page · raw message

mmc_cmds.h +1 -0
index ce35d3e..f767deb 100644
@@ -50,6 +50,7 @@ int do_rpmb_sec_wp_mode_clear(int nargs, char **argv);
 int do_rpmb_sec_wp_en_read(int nargs, char **argv);
 int do_cache_en(int nargs, char **argv);
 int do_cache_dis(int nargs, char **argv);
+int do_cache_flush(int nargs, char **argv);
 int do_ffu(int nargs, char **argv);
 int do_opt_ffu1(int nargs, char **argv);
 int do_opt_ffu2(int nargs, char **argv);
Signature · 1 line
2.43.0
#3 lore raw
In reply to Michele Dionisio
Quoted 6 lines · Michele Dionisio

The MMC 5.0 instroduce command to flush cache. This feature can be use on embedded device there power is not stable.

Applied for mmc-utils master, thanks!

Kind regards
Uffe

Quoted 148 lines

---
Changes since v1:
- Remove redundant cover letter
- Add docs/HOWTO.rst
- add check to not flush if cache is disabled

 docs/HOWTO.rst |  5 +++++
 man/mmc.1      |  4 ++++
 mmc.c          |  5 +++++
 mmc.h          |  1 +
 mmc_cmds.c     | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++
 mmc_cmds.h     |  1 +
 6 files changed, 68 insertions(+)
docs/HOWTO.rst +5 -0
index f2f2f35..d454d8c 100644
@@ -107,3 +107,8 @@ Running mmc-utils
     ``mmc rpmb secure-wp-enable <rpmb device> <key file>``
         Disable updating WP related EXT_CSD and CSD fields.
         Applicable only if secure write protect mode is enabled.
+
+
+    ``mmc cache flush <device>``
+        Flush the eMMC cache for <device>.
+        Applicable only if device version >= eMMC5.0.
man/mmc.1 +4 -0
index bccabf3..665fe6d 100644
@@ -109,6 +109,10 @@ NOTE! The cache is an optional feature on devices >= eMMC4.5.
 Disable the eMMC cache feature on <device>.
 NOTE! The cache is an optional feature on devices >= eMMC4.5.
 .TP
+.BR "cache flush <device>"
+Flush the eMMC cache on <device>.
+NOTE! The cache is an optional feature on devices >= eMMC5.0.
+.TP
 .BR "<cmd> --help"
 Show detailed help for a command or subset of commands.
 
mmc.c +5 -0
index 6770a45..fce7eef 100644
@@ -231,6 +231,11 @@ static struct Command commands[] = {
                "Disable the eMMC cache feature on <device>.\n"
                "NOTE! The cache is an optional feature on devices >= eMMC4.5.",
        },
+       { do_cache_flush, 1,
+         "cache flush", "<device>\n"
+               "flush the eMMC cache <device>.\n"
+               "NOTE! The cache is an optional feature on devices >= eMMC5.0.",
+       },
        { do_read_csd, -1,
          "csd read", "<device path>\n"
                  "Print CSD data from <device path>.\n"
mmc.h +1 -0
index 9fc22ec..2bb346b 100644
@@ -152,6 +152,7 @@
 #define EXT_CSD_DATA_SECTOR_SIZE       61 /* R */
 #define EXT_CSD_EXT_PARTITIONS_ATTRIBUTE_1     53
 #define EXT_CSD_EXT_PARTITIONS_ATTRIBUTE_0     52
+#define EXT_CSD_FLUSH_CACHE     32
 #define EXT_CSD_CACHE_CTRL             33
 #define EXT_CSD_MODE_CONFIG            30
 #define EXT_CSD_MODE_OPERATION_CODES   29      /* W */
mmc_cmds.c +52 -0
index d56a3ed..16c6b2e 100644
@@ -2865,6 +2865,58 @@ int do_cache_dis(int nargs, char **argv)
        return do_cache_ctrl(0, nargs, argv);
 }
 
+int do_cache_flush(int nargs, char **argv)
+{
+       __u8 ext_csd[512];
+       int fd, ret;
+       char *device;
+
+       device = argv[1];
+
+       fd = open(device, O_RDWR);
+       if (fd < 0) {
+               perror("open");
+               exit(1);
+       }
+
+       ret = read_extcsd(fd, ext_csd);
+       if (ret) {
+               fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
+               exit(1);
+       }
+
+       if (ext_csd[EXT_CSD_REV] < EXT_CSD_REV_V5_0) {
+               fprintf(stderr,
+                       "The CACHE FLUSH option is only availabe on devices >= "
+                       "MMC 5.0 %s\n", device);
+               exit(1);
+       }
+
+       /* If the cache size is zero, this device does not have a cache */
+       if (!(ext_csd[EXT_CSD_CACHE_SIZE_3] ||
+                       ext_csd[EXT_CSD_CACHE_SIZE_2] ||
+                       ext_csd[EXT_CSD_CACHE_SIZE_1] ||
+                       ext_csd[EXT_CSD_CACHE_SIZE_0])) {
+               fprintf(stderr,
+                       "The CACHE option is not available on %s\n",
+                       device);
+               exit(1);
+       }
+
+       if (ext_csd[EXT_CSD_CACHE_CTRL]) {
+               ret = write_extcsd_value(fd, EXT_CSD_FLUSH_CACHE, 1, 0);
+               if (ret) {
+                       fprintf(stderr,
+                               "Could not write 0x%02x to EXT_CSD[%d] in %s\n",
+                               EXT_CSD_FLUSH_CACHE, EXT_CSD_FLUSH_CACHE, device);
+                       exit(1);
+               }
+       }
+
+       close(fd);
+       return 0;
+}
+
 static int erase(int dev_fd, __u32 argin, __u32 start, __u32 end)
 {
        int ret = 0;
mmc_cmds.h +1 -1
index ce35d3e..f767deb 100644
@@ -50,6 +50,7 @@ int do_rpmb_sec_wp_mode_clear(int nargs, char **argv);
 int do_rpmb_sec_wp_en_read(int nargs, char **argv);
 int do_cache_en(int nargs, char **argv);
 int do_cache_dis(int nargs, char **argv);
+int do_cache_flush(int nargs, char **argv);
 int do_ffu(int nargs, char **argv);
 int do_opt_ffu1(int nargs, char **argv);
 int do_opt_ffu2(int nargs, char **argv);
--

2.43.0