1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * MultiMediaCard (MMC), eMMC and Secure Digital (SD) erase support code.
4 * This code is controller independent.
10 uint64_t storage_block_erase(struct storage_media
*media
, uint64_t start
,
13 struct mmc_command cmd
;
14 struct sd_mmc_ctrlr
*ctrlr
= media
->ctrlr
;
16 if (storage_block_setup(media
, start
, count
, 0) == 0)
19 cmd
.cmdidx
= MMC_CMD_ERASE_GROUP_START
;
20 cmd
.resp_type
= CARD_RSP_R1
;
24 if (ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
))
27 cmd
.cmdidx
= MMC_CMD_ERASE_GROUP_END
;
28 cmd
.cmdarg
= start
+ count
- 1;
29 cmd
.resp_type
= CARD_RSP_R1
;
32 if (ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
))
35 cmd
.cmdidx
= MMC_CMD_ERASE
;
36 cmd
.cmdarg
= MMC_TRIM_ARG
; /* just unmap blocks */
37 cmd
.resp_type
= CARD_RSP_R1
;
40 if (ctrlr
->send_cmd(ctrlr
, &cmd
, NULL
))
45 * Timeout for TRIM operation on one erase group is defined as:
46 * TRIM timeout = 300ms x TRIM_MULT
48 * This timeout is expressed in units of 100us to sd_mmc_send_status.
50 * Hence, timeout_per_erase_block = TRIM timeout * 1000us/100us;
52 size_t timeout_per_erase_block
= (media
->trim_mult
* 300) * 10;
55 erase_blocks
= ALIGN_UP(count
, media
->erase_blocks
)
56 / media
->erase_blocks
;
58 while (erase_blocks
) {
60 * To avoid overflow of timeout value, loop in calls to
61 * sd_mmc_send_status for erase_blocks number of times.
63 err
= sd_mmc_send_status(media
, timeout_per_erase_block
);
65 /* Send status successful, erase action complete. */
72 /* Total timeout done. Still status not successful. */
74 sd_mmc_error("TRIM operation not successful within timeout.\n");