2 * Copyright 2008, Freescale Semiconductor, Inc
5 * Based vaguely on the Linux code
7 * SPDX-License-Identifier: GPL-2.0+
15 #include <linux/math64.h>
16 #include "mmc_private.h"
18 static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
22 int err, start_cmd, end_cmd;
24 if (mmc->high_capacity) {
25 end = start + blkcnt - 1;
27 end = (start + blkcnt - 1) * mmc->write_bl_len;
28 start *= mmc->write_bl_len;
32 start_cmd = SD_CMD_ERASE_WR_BLK_START;
33 end_cmd = SD_CMD_ERASE_WR_BLK_END;
35 start_cmd = MMC_CMD_ERASE_GROUP_START;
36 end_cmd = MMC_CMD_ERASE_GROUP_END;
39 cmd.cmdidx = start_cmd;
41 cmd.resp_type = MMC_RSP_R1;
43 err = mmc_send_cmd(mmc, &cmd, NULL);
50 err = mmc_send_cmd(mmc, &cmd, NULL);
54 cmd.cmdidx = MMC_CMD_ERASE;
55 cmd.cmdarg = MMC_ERASE_ARG;
56 cmd.resp_type = MMC_RSP_R1b;
58 err = mmc_send_cmd(mmc, &cmd, NULL);
65 puts("mmc erase failed\n");
69 unsigned long mmc_berase(struct blk_desc *block_dev, lbaint_t start,
72 int dev_num = block_dev->devnum;
74 u32 start_rem, blkcnt_rem;
75 struct mmc *mmc = find_mmc_device(dev_num);
76 lbaint_t blk = 0, blk_r = 0;
82 err = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_num,
88 * We want to see if the requested start or total block count are
89 * unaligned. We discard the whole numbers and only care about the
92 err = div_u64_rem(start, mmc->erase_grp_size, &start_rem);
93 err = div_u64_rem(blkcnt, mmc->erase_grp_size, &blkcnt_rem);
94 if (start_rem || blkcnt_rem)
95 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
96 "The erase range would be change to "
97 "0x" LBAF "~0x" LBAF "\n\n",
98 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
99 ((start + blkcnt + mmc->erase_grp_size)
100 & ~(mmc->erase_grp_size - 1)) - 1);
102 while (blk < blkcnt) {
103 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
104 mmc->erase_grp_size : (blkcnt - blk);
105 err = mmc_erase_t(mmc, start + blk, blk_r);
111 /* Waiting for the ready status */
112 if (mmc_send_status(mmc, timeout))
119 static ulong mmc_write_blocks(struct mmc *mmc, lbaint_t start,
120 lbaint_t blkcnt, const void *src)
123 struct mmc_data data;
126 if ((start + blkcnt) > mmc_get_blk_desc(mmc)->lba) {
127 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
128 start + blkcnt, mmc_get_blk_desc(mmc)->lba);
134 else if (blkcnt == 1)
135 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
137 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
139 if (mmc->high_capacity)
142 cmd.cmdarg = start * mmc->write_bl_len;
144 cmd.resp_type = MMC_RSP_R1;
147 data.blocks = blkcnt;
148 data.blocksize = mmc->write_bl_len;
149 data.flags = MMC_DATA_WRITE;
151 if (mmc_send_cmd(mmc, &cmd, &data)) {
152 printf("mmc write failed\n");
156 /* SPI multiblock writes terminate using a special
157 * token, not a STOP_TRANSMISSION request.
159 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
160 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
162 cmd.resp_type = MMC_RSP_R1b;
163 if (mmc_send_cmd(mmc, &cmd, NULL)) {
164 printf("mmc fail to send stop cmd\n");
169 /* Waiting for the ready status */
170 if (mmc_send_status(mmc, timeout))
177 ulong mmc_bwrite(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
180 ulong mmc_bwrite(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
185 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
187 int dev_num = block_dev->devnum;
188 lbaint_t cur, blocks_todo = blkcnt;
191 struct mmc *mmc = find_mmc_device(dev_num);
195 err = blk_select_hwpart_devnum(IF_TYPE_MMC, dev_num, block_dev->hwpart);
199 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
203 cur = (blocks_todo > mmc->cfg->b_max) ?
204 mmc->cfg->b_max : blocks_todo;
205 if (mmc_write_blocks(mmc, start, cur, src) != cur)
209 src += cur * mmc->write_bl_len;
210 } while (blocks_todo > 0);