Skip to content

Commit

Permalink
sdmmc: Add erase command-38. Support erase/trim/discard/sanitize
Browse files Browse the repository at this point in the history
options.

Erase command (38) for SD cards allows option for erase/dicard/fule
operation at block level and for MMC cards supports option for
discard/trim at block level. When Sanitize is executed only the
portion of data that was unmapped by a Discard command shall be
removed by the Sanitize command.

Unit test cases added to verify ERASE feature in SD/SDSPI mode.
TRIM/DISCARD/SANITIZE tests for eMMC devices.

Closes #7635
Closes #7623
  • Loading branch information
Vamshi Gajjela committed Mar 5, 2022
1 parent 19ddb8b commit ffdbeee
Show file tree
Hide file tree
Showing 12 changed files with 825 additions and 16 deletions.
36 changes: 30 additions & 6 deletions components/driver/include/driver/sdmmc_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@
#define MMC_SET_BLOCK_COUNT 23 /* R1 */
#define MMC_WRITE_BLOCK_SINGLE 24 /* R1 */
#define MMC_WRITE_BLOCK_MULTIPLE 25 /* R1 */
#define MMC_ERASE_GROUP_START 35 /* R1 */
#define MMC_ERASE_GROUP_END 36 /* R1 */
#define MMC_ERASE 38 /* R1B */
#define MMC_APP_CMD 55 /* R1 */

/* SD commands */ /* response type */
#define SD_SEND_RELATIVE_ADDR 3 /* R6 */
#define SD_SEND_SWITCH_FUNC 6 /* R1 */
#define SD_SEND_IF_COND 8 /* R7 */
#define SD_ERASE_GROUP_START 32 /* R1 */
#define SD_ERASE_GROUP_END 33 /* R1 */
#define SD_READ_OCR 58 /* R3 */
#define SD_CRC_ON_OFF 59 /* R1 */

Expand Down Expand Up @@ -141,21 +146,26 @@
#define SD_ARG_BUS_WIDTH_4 2

/* EXT_CSD fields */
#define EXT_CSD_SANITIZE_START 165 /* WO */
#define EXT_CSD_ERASED_MEM_CONT 181 /* RO */
#define EXT_CSD_BUS_WIDTH 183 /* WO */
#define EXT_CSD_HS_TIMING 185 /* R/W */
#define EXT_CSD_POWER_CLASS 187 /* R/W */
#define EXT_CSD_CMD_SET 191 /* R/W */
#define EXT_CSD_REV 192 /* RO */
#define EXT_CSD_STRUCTURE 194 /* RO */
#define EXT_CSD_CARD_TYPE 196 /* RO */
#define EXT_CSD_SEC_COUNT 212 /* RO */
#define EXT_CSD_PWR_CL_26_360 203 /* RO */
#define EXT_CSD_PWR_CL_52_360 202 /* RO */
#define EXT_CSD_PWR_CL_26_195 201 /* RO */
#define EXT_CSD_PWR_CL_52_195 200 /* RO */
#define EXT_CSD_POWER_CLASS 187 /* R/W */
#define EXT_CSD_CMD_SET 191 /* R/W */
#define EXT_CSD_PWR_CL_26_195 201 /* RO */
#define EXT_CSD_PWR_CL_52_360 202 /* RO */
#define EXT_CSD_PWR_CL_26_360 203 /* RO */
#define EXT_CSD_SEC_COUNT 212 /* RO */
#define EXT_CSD_SEC_FEATURE_SUPPORT 231 /* RO */
#define EXT_CSD_S_CMD_SET 504 /* RO */

/* EXT_CSD field definitions */
#define EXT_CSD_REV_1_6 6 /* Revision 1.6 (for MMC v4.5, v4.51) */

#define EXT_CSD_CMD_SET_NORMAL (1U << 0)
#define EXT_CSD_CMD_SET_SECURE (1U << 1)
#define EXT_CSD_CMD_SET_CPSECURE (1U << 2)
Expand Down Expand Up @@ -186,6 +196,12 @@
#define EXT_CSD_CARD_TYPE_52M_V12 0x0b
#define EXT_CSD_CARD_TYPE_52M_V12_18 0x0f

/* EXT_CSD_SEC_FEATURE_SUPPORT */
#define EXT_CSD_SECURE_ER_EN (uint8_t)(1 << 0)
#define EXT_CSD_SEC_BD_BLK_EN (uint8_t)(1 << 2)
#define EXT_CSD_SEC_GB_CL_EN (uint8_t)(1 << 4)
#define EXT_CSD_SEC_SANITIZE (uint8_t)(1 << 6)

/* EXT_CSD MMC */
#define EXT_CSD_MMC_SIZE 512

Expand Down Expand Up @@ -336,6 +352,12 @@
#define SCR_CMD_SUPPORT_CMD20(scr) MMC_RSP_BITS((scr), 32, 1)
#define SCR_RESERVED2(scr) MMC_RSP_BITS((scr), 0, 32)

/* SSR (SD Status Register) */
#define SSR_DAT_BUS_WIDTH(ssr) MMC_RSP_BITS((ssr), 510, 2)
#define SSR_AU_SIZE(ssr) MMC_RSP_BITS((ssr), 428, 4)
#define SSR_DISCARD_SUPPORT(ssr) MMC_RSP_BITS((ssr), 313, 1)
#define SSR_FULE_SUPPORT(ssr) MMC_RSP_BITS((ssr), 312, 1)

/* Max supply current in SWITCH_FUNC response (in mA) */
#define SD_SFUNC_I_MAX(status) (MMC_RSP_BITS((uint32_t *)(status), 496, 16))

Expand Down Expand Up @@ -365,6 +387,8 @@
#define SD_ACCESS_MODE_SDR104 3 /* UHS-I, 208 MHz clock */
#define SD_ACCESS_MODE_DDR50 4 /* UHS-I, 50 MHz clock, DDR */

#define SD_SSR_SIZE 64 /* SD status register */

/**
* @brief Extract up to 32 sequential bits from an array of 32-bit words
*
Expand Down
50 changes: 46 additions & 4 deletions components/driver/include/driver/sdmmc_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,35 @@ typedef struct {

/**
* Decoded values from SD Configuration Register
* Note: When new member is added, update reserved bits accordingly
*/
typedef struct {
int sd_spec; /*!< SD Physical layer specification version, reported by card */
int bus_width; /*!< bus widths supported by card: BIT(0) — 1-bit bus, BIT(2) — 4-bit bus */
uint32_t sd_spec: 4; /*!< SD Physical layer specification version, reported by card */
uint32_t erase_mem_state: 1; /*!< data state on card after erase whether 0 or 1 (card vendor dependent) */
uint32_t bus_width: 4; /*!< bus widths supported by card: BIT(0) — 1-bit bus, BIT(2) — 4-bit bus */
uint32_t reserved: 23; /*!< reserved for future expansion */
uint32_t rsvd_mnf; /*!< reserved for manufacturer usage */
} sdmmc_scr_t;

/**
* Decoded values from SD Status Register
* Note: When new member is added, update reserved bits accordingly
*/
typedef struct {
uint32_t cur_bus_width: 2; /*!< SD current bus width */
uint32_t discard_support: 1; /*!< SD discard feature support */
uint32_t fule_support: 1; /*!< SD FULE (Full User Area Logical Erase) feature support */
uint32_t reserved: 28; /*!< reserved for future expansion */
} sdmmc_ssr_t;

/**
* Decoded values of Extended Card Specific Data
*/
typedef struct {
uint8_t power_class; /*!< Power class used by the card */
uint8_t rev; /*!< Extended CSD Revision */
uint8_t power_class; /*!< Power class used by the card */
uint8_t erase_mem_state; /*!< data state on card after erase whether 0 or 1 (card vendor dependent) */
uint8_t sec_feature; /*!< secure data management features supported by the card */
} sdmmc_ext_csd_t;

/**
Expand Down Expand Up @@ -120,7 +138,7 @@ typedef struct {
#define SCF_WAIT_BUSY 0x2000 /*!< Wait for completion of card busy signal before returning */
/** @endcond */
esp_err_t error; /*!< error returned from transfer */
int timeout_ms; /*!< response timeout, in milliseconds */
uint32_t timeout_ms; /*!< response timeout, in milliseconds */
} sdmmc_command_t;

/**
Expand Down Expand Up @@ -173,6 +191,7 @@ typedef struct {
};
sdmmc_csd_t csd; /*!< decoded CSD (Card-Specific Data) register value */
sdmmc_scr_t scr; /*!< decoded SCR (SD card Configuration Register) value */
sdmmc_ssr_t ssr; /*!< decoded SSR (SD Status Register) value */
sdmmc_ext_csd_t ext_csd; /*!< decoded EXT_CSD (Extended Card Specific Data) register value */
uint16_t rca; /*!< RCA (Relative Card Address) */
uint16_t max_freq_khz; /*!< Maximum frequency, in kHz, supported by the card */
Expand All @@ -185,5 +204,28 @@ typedef struct {
uint32_t reserved : 23; /*!< Reserved for future expansion */
} sdmmc_card_t;

/**
* SD/MMC erase command(38) arguments
* SD:
* ERASE: Erase the write blocks, physical/hard erase.
*
* DISCARD: Card may deallocate the discarded blocks partially or completely.
* After discard operation the previously written data may be partially or
* fully read by the host depending on card implementation.
*
* MMC:
* ERASE: Does TRIM, applies erase operation to write blocks instead of Erase Group.
*
* DISCARD: The Discard function allows the host to identify data that is no
* longer required so that the device can erase the data if necessary during
* background erase events. Applies to write blocks instead of Erase Group
* After discard operation, the original data may be remained partially or
* fully accessible to the host dependent on device.
*
*/
typedef enum {
SDMMC_ERASE_ARG = 0, /*!< Erase operation on SD, Trim operation on MMC */
SDMMC_DISCARD_ARG = 1, /*!< Discard operation for SD/MMC */
} sdmmc_erase_arg_t;

#endif // _SDMMC_TYPES_H_
6 changes: 3 additions & 3 deletions components/driver/sdmmc_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static esp_err_t process_events(sdmmc_event_t evt, sdmmc_command_t* cmd,
static void process_command_response(uint32_t status, sdmmc_command_t* cmd);
static void fill_dma_descriptors(size_t num_desc);
static size_t get_free_descriptors_count(void);
static bool wait_for_busy_cleared(int timeout_ms);
static bool wait_for_busy_cleared(uint32_t timeout_ms);

esp_err_t sdmmc_host_transaction_handler_init(void)
{
Expand Down Expand Up @@ -462,7 +462,7 @@ static esp_err_t process_events(sdmmc_event_t evt, sdmmc_command_t* cmd,
return ESP_OK;
}

static bool wait_for_busy_cleared(int timeout_ms)
static bool wait_for_busy_cleared(uint32_t timeout_ms)
{
if (timeout_ms == 0) {
return !sdmmc_host_card_busy();
Expand All @@ -472,7 +472,7 @@ static bool wait_for_busy_cleared(int timeout_ms)
* can only generate Busy Clear Interrupt for data write commands, and waiting
* for busy clear is mostly needed for other commands such as MMC_SWITCH.
*/
int timeout_ticks = (timeout_ms + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS;
uint32_t timeout_ticks = (timeout_ms + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS;
while (timeout_ticks-- > 0) {
if (!sdmmc_host_card_busy()) {
return true;
Expand Down
78 changes: 78 additions & 0 deletions components/sdmmc/include/sdmmc_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,84 @@ esp_err_t sdmmc_write_sectors(sdmmc_card_t* card, const void* src,
esp_err_t sdmmc_read_sectors(sdmmc_card_t* card, void* dst,
size_t start_sector, size_t sector_count);

/**
* Erase given number of sectors from the SD/MMC card
*
* @note When sdmmc_erase_sectors used with cards in SDSPI mode, it was
* observed that card requires re-init after erase operation.
*
* @param card pointer to card information structure previously initialized
* using sdmmc_card_init
* @param start_sector sector where to start erase
* @param sector_count number of sectors to erase
* @param arg erase command (CMD38) argument
* @return
* - ESP_OK on success
* - One of the error codes from SDMMC host controller
*/
esp_err_t sdmmc_erase_sectors(sdmmc_card_t* card, size_t start_sector,
size_t sector_count, sdmmc_erase_arg_t arg);

/**
* Check if SD/MMC card supports discard
*
* @param card pointer to card information structure previously initialized
* using sdmmc_card_init
* @return
* - ESP_OK if supported by the card/device
* - ESP_FAIL if not supported by the card/device
*/
esp_err_t sdmmc_can_discard(sdmmc_card_t* card);

/**
* Check if SD/MMC card supports trim
*
* @param card pointer to card information structure previously initialized
* using sdmmc_card_init
* @return
* - ESP_OK if supported by the card/device
* - ESP_FAIL if not supported by the card/device
*/
esp_err_t sdmmc_can_trim(sdmmc_card_t* card);

/**
* Check if SD/MMC card supports sanitize
*
* @param card pointer to card information structure previously initialized
* using sdmmc_card_init
* @return
* - ESP_OK if supported by the card/device
* - ESP_FAIL if not supported by the card/device
*/
esp_err_t sdmmc_mmc_can_sanitize(sdmmc_card_t* card);

/**
* Sanitize the data that was unmapped by a Discard command
*
* @note Discard command has to precede sanitize operation. To discard, use
* MMC_DICARD_ARG with sdmmc_erase_sectors argument
*
* @param card pointer to card information structure previously initialized
* using sdmmc_card_init
* @param timeout_ms timeout value in milliseconds required to sanitize the
* selected range of sectors.
* @return
* - ESP_OK on success
* - One of the error codes from SDMMC host controller
*/
esp_err_t sdmmc_mmc_sanitize(sdmmc_card_t* card, uint32_t timeout_ms);

/**
* Erase complete SD/MMC card
*
* @param card pointer to card information structure previously initialized
* using sdmmc_card_init
* @return
* - ESP_OK on success
* - One of the error codes from SDMMC host controller
*/
esp_err_t sdmmc_full_erase(sdmmc_card_t* card);

/**
* Read one byte from an SDIO card using IO_RW_DIRECT (CMD52)
*
Expand Down
Loading

0 comments on commit ffdbeee

Please sign in to comment.