2 * Copyright 2008, Freescale Semiconductor, Inc
5 * Based vaguely on the Linux code
7 * SPDX-License-Identifier: GPL-2.0+
14 #include <linux/math64.h>
15 #include "mmc_private.h"
17 static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
21 int err, start_cmd, end_cmd;
23 if (mmc->high_capacity) {
24 end = start + blkcnt - 1;
26 end = (start + blkcnt - 1) * mmc->write_bl_len;
27 start *= mmc->write_bl_len;
31 start_cmd = SD_CMD_ERASE_WR_BLK_START;
32 end_cmd = SD_CMD_ERASE_WR_BLK_END;
34 start_cmd = MMC_CMD_ERASE_GROUP_START;
35 end_cmd = MMC_CMD_ERASE_GROUP_END;
38 cmd.cmdidx = start_cmd;
40 cmd.resp_type = MMC_RSP_R1;
42 err = mmc_send_cmd(mmc, &cmd, NULL);
49 err = mmc_send_cmd(mmc, &cmd, NULL);
53 cmd.cmdidx = MMC_CMD_ERASE;
54 cmd.cmdarg = MMC_ERASE_ARG;
55 cmd.resp_type = MMC_RSP_R1b;
57 err = mmc_send_cmd(mmc, &cmd, NULL);
64 puts("mmc erase failed\n");
68 unsigned long mmc_berase(struct blk_desc *block_dev, lbaint_t start,
71 int dev_num = block_dev->devnum;
73 u32 start_rem, blkcnt_rem;
74 struct mmc *mmc = find_mmc_device(dev_num);
75 lbaint_t blk = 0, blk_r = 0;
81 err = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_num,
87 * We want to see if the requested start or total block count are
88 * unaligned. We discard the whole numbers and only care about the
91 err = div_u64_rem(start, mmc->erase_grp_size, &start_rem);
92 err = div_u64_rem(blkcnt, mmc->erase_grp_size, &blkcnt_rem);
93 if (start_rem || blkcnt_rem)
94 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
95 "The erase range would be change to "
96 "0x" LBAF "~0x" LBAF "\n\n",
97 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
98 ((start + blkcnt + mmc->erase_grp_size)
99 & ~(mmc->erase_grp_size - 1)) - 1);
101 while (blk < blkcnt) {
102 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
103 mmc->erase_grp_size : (blkcnt - blk);
104 err = mmc_erase_t(mmc, start + blk, blk_r);
110 /* Waiting for the ready status */
111 if (mmc_send_status(mmc, timeout))
118 static ulong mmc_write_blocks(struct mmc *mmc, lbaint_t start,
119 lbaint_t blkcnt, const void *src)
122 struct mmc_data data;
125 if ((start + blkcnt) > mmc_get_blk_desc(mmc)->lba) {
126 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
127 start + blkcnt, mmc_get_blk_desc(mmc)->lba);
133 else if (blkcnt == 1)
134 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
136 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
138 if (mmc->high_capacity)
141 cmd.cmdarg = start * mmc->write_bl_len;
143 cmd.resp_type = MMC_RSP_R1;
146 data.blocks = blkcnt;
147 data.blocksize = mmc->write_bl_len;
148 data.flags = MMC_DATA_WRITE;
150 if (mmc_send_cmd(mmc, &cmd, &data)) {
151 printf("mmc write failed\n");
155 /* SPI multiblock writes terminate using a special
156 * token, not a STOP_TRANSMISSION request.
158 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
159 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
161 cmd.resp_type = MMC_RSP_R1b;
162 if (mmc_send_cmd(mmc, &cmd, NULL)) {
163 printf("mmc fail to send stop cmd\n");
168 /* Waiting for the ready status */
169 if (mmc_send_status(mmc, timeout))
175 ulong mmc_bwrite(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
178 int dev_num = block_dev->devnum;
179 lbaint_t cur, blocks_todo = blkcnt;
182 struct mmc *mmc = find_mmc_device(dev_num);
186 err = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_num, block_dev->hwpart);
190 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
194 cur = (blocks_todo > mmc->cfg->b_max) ?
195 mmc->cfg->b_max : blocks_todo;
196 if (mmc_write_blocks(mmc, start, cur, src) != cur)
200 src += cur * mmc->write_bl_len;
201 } while (blocks_todo > 0);