blob: c023d15e52a8df45a36207efd4ebca1d8e6bd6a4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Paul Burton8d30cc92013-09-09 15:30:26 +01002/*
3 * Copyright 2008, Freescale Semiconductor, Inc
4 * Andy Fleming
5 *
6 * Based vaguely on the Linux code
Paul Burton8d30cc92013-09-09 15:30:26 +01007 */
8
9#include <config.h>
Simon Glass655306c2020-05-10 11:39:58 -060010#include <blk.h>
Simon Glass59bc6f22016-05-01 13:52:41 -060011#include <dm.h>
Paul Burton8d30cc92013-09-09 15:30:26 +010012#include <part.h>
Tom Rini58e31942015-06-11 20:53:31 -040013#include <div64.h>
14#include <linux/math64.h>
Paul Burton8d30cc92013-09-09 15:30:26 +010015#include "mmc_private.h"
16
Loic Poulain7fc98c52023-01-26 10:24:18 +010017static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt, u32 args)
Paul Burton8d30cc92013-09-09 15:30:26 +010018{
19 struct mmc_cmd cmd;
20 ulong end;
21 int err, start_cmd, end_cmd;
22
23 if (mmc->high_capacity) {
24 end = start + blkcnt - 1;
25 } else {
26 end = (start + blkcnt - 1) * mmc->write_bl_len;
27 start *= mmc->write_bl_len;
28 }
29
30 if (IS_SD(mmc)) {
31 start_cmd = SD_CMD_ERASE_WR_BLK_START;
32 end_cmd = SD_CMD_ERASE_WR_BLK_END;
33 } else {
34 start_cmd = MMC_CMD_ERASE_GROUP_START;
35 end_cmd = MMC_CMD_ERASE_GROUP_END;
36 }
37
38 cmd.cmdidx = start_cmd;
39 cmd.cmdarg = start;
40 cmd.resp_type = MMC_RSP_R1;
41
42 err = mmc_send_cmd(mmc, &cmd, NULL);
43 if (err)
44 goto err_out;
45
46 cmd.cmdidx = end_cmd;
47 cmd.cmdarg = end;
48
49 err = mmc_send_cmd(mmc, &cmd, NULL);
50 if (err)
51 goto err_out;
52
53 cmd.cmdidx = MMC_CMD_ERASE;
Loic Poulain7fc98c52023-01-26 10:24:18 +010054 cmd.cmdarg = args ? args : MMC_ERASE_ARG;
Paul Burton8d30cc92013-09-09 15:30:26 +010055 cmd.resp_type = MMC_RSP_R1b;
56
57 err = mmc_send_cmd(mmc, &cmd, NULL);
58 if (err)
59 goto err_out;
60
61 return 0;
62
63err_out:
64 puts("mmc erase failed\n");
65 return err;
66}
67
Ezequiel Garciab8483d42019-01-07 18:13:24 -030068#if CONFIG_IS_ENABLED(BLK)
Simon Glassfcc53c12016-10-01 14:43:17 -060069ulong mmc_berase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt)
70#else
71ulong mmc_berase(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt)
72#endif
Paul Burton8d30cc92013-09-09 15:30:26 +010073{
Ezequiel Garciab8483d42019-01-07 18:13:24 -030074#if CONFIG_IS_ENABLED(BLK)
Simon Glass71fa5b42020-12-03 16:55:18 -070075 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glassfcc53c12016-10-01 14:43:17 -060076#endif
Simon Glass2f26fff2016-02-29 15:25:51 -070077 int dev_num = block_dev->devnum;
Paul Burton8d30cc92013-09-09 15:30:26 +010078 int err = 0;
Loic Poulain7fc98c52023-01-26 10:24:18 +010079 u32 start_rem, blkcnt_rem, erase_args = 0;
Paul Burton8d30cc92013-09-09 15:30:26 +010080 struct mmc *mmc = find_mmc_device(dev_num);
81 lbaint_t blk = 0, blk_r = 0;
Sam Protsenkodb174c62019-08-14 22:52:51 +030082 int timeout_ms = 1000;
Paul Burton8d30cc92013-09-09 15:30:26 +010083
84 if (!mmc)
85 return -1;
86
Simon Glassdbfa32c2022-08-11 19:34:59 -060087 err = blk_select_hwpart_devnum(UCLASS_MMC, dev_num,
Simon Glass11f2bb62016-05-01 13:52:29 -060088 block_dev->hwpart);
Stephen Warren1e0f92a2015-12-07 11:38:49 -070089 if (err < 0)
90 return -1;
91
Tom Rini58e31942015-06-11 20:53:31 -040092 /*
93 * We want to see if the requested start or total block count are
94 * unaligned. We discard the whole numbers and only care about the
95 * remainder.
96 */
97 err = div_u64_rem(start, mmc->erase_grp_size, &start_rem);
98 err = div_u64_rem(blkcnt, mmc->erase_grp_size, &blkcnt_rem);
Loic Poulain7fc98c52023-01-26 10:24:18 +010099 if (start_rem || blkcnt_rem) {
100 if (mmc->can_trim) {
101 /* Trim function applies the erase operation to write
102 * blocks instead of erase groups.
103 */
104 erase_args = MMC_TRIM_ARG;
105 } else {
106 /* The card ignores all LSB's below the erase group
107 * size, rounding down the addess to a erase group
108 * boundary.
109 */
110 printf("\n\nCaution! Your devices Erase group is 0x%x\n"
111 "The erase range would be change to "
112 "0x" LBAF "~0x" LBAF "\n\n",
113 mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
114 ((start + blkcnt + mmc->erase_grp_size - 1)
115 & ~(mmc->erase_grp_size - 1)) - 1);
116 }
117 }
Paul Burton8d30cc92013-09-09 15:30:26 +0100118
119 while (blk < blkcnt) {
Peng Fan32260452016-09-01 11:13:39 +0800120 if (IS_SD(mmc) && mmc->ssr.au) {
121 blk_r = ((blkcnt - blk) > mmc->ssr.au) ?
122 mmc->ssr.au : (blkcnt - blk);
123 } else {
124 blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
125 mmc->erase_grp_size : (blkcnt - blk);
126 }
Loic Poulain7fc98c52023-01-26 10:24:18 +0100127 err = mmc_erase_t(mmc, start + blk, blk_r, erase_args);
Paul Burton8d30cc92013-09-09 15:30:26 +0100128 if (err)
129 break;
130
131 blk += blk_r;
132
133 /* Waiting for the ready status */
Sam Protsenkodb174c62019-08-14 22:52:51 +0300134 if (mmc_poll_for_busy(mmc, timeout_ms))
Paul Burton8d30cc92013-09-09 15:30:26 +0100135 return 0;
136 }
137
138 return blk;
139}
140
141static ulong mmc_write_blocks(struct mmc *mmc, lbaint_t start,
142 lbaint_t blkcnt, const void *src)
143{
144 struct mmc_cmd cmd;
145 struct mmc_data data;
Sam Protsenkodb174c62019-08-14 22:52:51 +0300146 int timeout_ms = 1000;
Paul Burton8d30cc92013-09-09 15:30:26 +0100147
Simon Glasse5db1152016-05-01 13:52:35 -0600148 if ((start + blkcnt) > mmc_get_blk_desc(mmc)->lba) {
Paul Burton8d30cc92013-09-09 15:30:26 +0100149 printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
Simon Glasse5db1152016-05-01 13:52:35 -0600150 start + blkcnt, mmc_get_blk_desc(mmc)->lba);
Paul Burton8d30cc92013-09-09 15:30:26 +0100151 return 0;
152 }
153
154 if (blkcnt == 0)
155 return 0;
156 else if (blkcnt == 1)
157 cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
158 else
159 cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
160
161 if (mmc->high_capacity)
162 cmd.cmdarg = start;
163 else
164 cmd.cmdarg = start * mmc->write_bl_len;
165
166 cmd.resp_type = MMC_RSP_R1;
167
168 data.src = src;
169 data.blocks = blkcnt;
170 data.blocksize = mmc->write_bl_len;
171 data.flags = MMC_DATA_WRITE;
172
173 if (mmc_send_cmd(mmc, &cmd, &data)) {
174 printf("mmc write failed\n");
175 return 0;
176 }
177
178 /* SPI multiblock writes terminate using a special
179 * token, not a STOP_TRANSMISSION request.
180 */
181 if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
182 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
183 cmd.cmdarg = 0;
184 cmd.resp_type = MMC_RSP_R1b;
185 if (mmc_send_cmd(mmc, &cmd, NULL)) {
186 printf("mmc fail to send stop cmd\n");
187 return 0;
188 }
189 }
190
191 /* Waiting for the ready status */
Sam Protsenkodb174c62019-08-14 22:52:51 +0300192 if (mmc_poll_for_busy(mmc, timeout_ms))
Paul Burton8d30cc92013-09-09 15:30:26 +0100193 return 0;
194
195 return blkcnt;
196}
197
Ezequiel Garciab8483d42019-01-07 18:13:24 -0300198#if CONFIG_IS_ENABLED(BLK)
Simon Glass59bc6f22016-05-01 13:52:41 -0600199ulong mmc_bwrite(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
200 const void *src)
201#else
Simon Glasse3394752016-02-29 15:25:34 -0700202ulong mmc_bwrite(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
Stephen Warrene73f2962015-12-07 11:38:48 -0700203 const void *src)
Simon Glass59bc6f22016-05-01 13:52:41 -0600204#endif
Paul Burton8d30cc92013-09-09 15:30:26 +0100205{
Ezequiel Garciab8483d42019-01-07 18:13:24 -0300206#if CONFIG_IS_ENABLED(BLK)
Simon Glass71fa5b42020-12-03 16:55:18 -0700207 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glass59bc6f22016-05-01 13:52:41 -0600208#endif
Simon Glass2f26fff2016-02-29 15:25:51 -0700209 int dev_num = block_dev->devnum;
Paul Burton8d30cc92013-09-09 15:30:26 +0100210 lbaint_t cur, blocks_todo = blkcnt;
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700211 int err;
Paul Burton8d30cc92013-09-09 15:30:26 +0100212
213 struct mmc *mmc = find_mmc_device(dev_num);
214 if (!mmc)
215 return 0;
216
Simon Glassdbfa32c2022-08-11 19:34:59 -0600217 err = blk_select_hwpart_devnum(UCLASS_MMC, dev_num, block_dev->hwpart);
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700218 if (err < 0)
219 return 0;
220
Paul Burton8d30cc92013-09-09 15:30:26 +0100221 if (mmc_set_blocklen(mmc, mmc->write_bl_len))
222 return 0;
223
224 do {
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200225 cur = (blocks_todo > mmc->cfg->b_max) ?
226 mmc->cfg->b_max : blocks_todo;
Paul Burton8d30cc92013-09-09 15:30:26 +0100227 if (mmc_write_blocks(mmc, start, cur, src) != cur)
228 return 0;
229 blocks_todo -= cur;
230 start += cur;
231 src += cur * mmc->write_bl_len;
232 } while (blocks_todo > 0);
233
234 return blkcnt;
235}