blob: f7b02b3149b516a5e2e0dd738d8f51a723f62ec1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk71f95112003-06-15 22:40:42 +00002/*
3 * (C) Copyright 2003
4 * Kyle Harris, kharris@nexus-tech.net
wdenk71f95112003-06-15 22:40:42 +00005 */
6
7#include <common.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -06008#include <blk.h>
wdenk71f95112003-06-15 22:40:42 +00009#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070010#include <console.h>
Marek Vasutd5810762020-06-20 14:28:25 +020011#include <memalign.h>
wdenk71f95112003-06-15 22:40:42 +000012#include <mmc.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060013#include <part.h>
Jassi Brar732bc7c2018-04-06 12:05:24 +053014#include <sparse_format.h>
15#include <image-sparse.h>
wdenk71f95112003-06-15 22:40:42 +000016
Mike Frysinger02e22c22009-06-14 21:35:21 -040017static int curr_device = -1;
Andy Fleming272cc702008-10-30 16:41:01 -050018
19static void print_mmcinfo(struct mmc *mmc)
20{
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010021 int i;
22
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020023 printf("Device: %s\n", mmc->cfg->name);
Andy Fleming272cc702008-10-30 16:41:01 -050024 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
Max Merchel84191f72022-02-10 10:16:39 +010025 if (IS_SD(mmc)) {
26 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
27 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
28 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
29 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
30 } else {
31 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xff);
32 printf("Name: %c%c%c%c%c%c \n", mmc->cid[0] & 0xff,
33 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
34 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
35 (mmc->cid[2] >> 24));
36 }
Andy Fleming272cc702008-10-30 16:41:01 -050037
Jean-Jacques Hiblot7a96ec72017-09-21 16:29:56 +020038 printf("Bus Speed: %d\n", mmc->clock);
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +010039#if CONFIG_IS_ENABLED(MMC_VERBOSE)
Marek Vasut41e30dc2019-03-18 04:49:21 +010040 printf("Mode: %s\n", mmc_mode_name(mmc->selected_mode));
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +010041 mmc_dump_capabilities("card capabilities", mmc->card_caps);
42 mmc_dump_capabilities("host capabilities", mmc->host_caps);
43#endif
Andy Fleming272cc702008-10-30 16:41:01 -050044 printf("Rd Block Len: %d\n", mmc->read_bl_len);
45
Pantelis Antoniou4b7cee52015-01-23 12:12:01 +020046 printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
47 EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
48 EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
49 if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
50 printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
51 printf("\n");
Andy Fleming272cc702008-10-30 16:41:01 -050052
53 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang940e0782011-01-04 01:04:19 +000054 puts("Capacity: ");
55 print_size(mmc->capacity, "\n");
Andy Fleming272cc702008-10-30 16:41:01 -050056
Andrew Gabbasov786e8f82014-12-01 06:59:09 -060057 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
58 mmc->ddr_mode ? " DDR" : "");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010059
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +010060#if CONFIG_IS_ENABLED(MMC_WRITE)
Diego Santa Cruzb0361522014-12-23 10:50:26 +010061 puts("Erase Group Size: ");
62 print_size(((u64)mmc->erase_grp_size) << 9, "\n");
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +010063#endif
Diego Santa Cruzb0361522014-12-23 10:50:26 +010064
Diego Santa Cruz525ada22014-12-23 10:50:19 +010065 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +010066 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
Diego Santa Cruzbeb98a12014-12-23 10:50:23 +010067 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
Marek Vasutd5810762020-06-20 14:28:25 +020068 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
69 u8 wp;
Heinrich Schuchardtd5210e42020-03-30 07:24:18 +020070 int ret;
Diego Santa Cruzb0361522014-12-23 10:50:26 +010071
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +010072#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruzb0361522014-12-23 10:50:26 +010073 puts("HC WP Group Size: ");
74 print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +010075#endif
Diego Santa Cruzb0361522014-12-23 10:50:26 +010076
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010077 puts("User Capacity: ");
Diego Santa Cruz9e41a002014-12-23 10:50:33 +010078 print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
79 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
80 puts(" WRREL\n");
81 else
82 putc('\n');
Diego Santa Cruzbeb98a12014-12-23 10:50:23 +010083 if (usr_enh) {
84 puts("User Enhanced Start: ");
85 print_size(mmc->enh_user_start, "\n");
86 puts("User Enhanced Size: ");
87 print_size(mmc->enh_user_size, "\n");
88 }
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010089 puts("Boot Capacity: ");
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +010090 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010091 puts("RPMB Capacity: ");
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +010092 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
Diego Santa Cruzb0361522014-12-23 10:50:26 +010093
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010094 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +010095 bool is_enh = has_enh &&
96 (mmc->part_attr & EXT_CSD_ENH_GP(i));
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010097 if (mmc->capacity_gp[i]) {
Diego Santa Cruzf289fd72014-12-23 10:50:18 +010098 printf("GP%i Capacity: ", i+1);
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +010099 print_size(mmc->capacity_gp[i],
Diego Santa Cruz9e41a002014-12-23 10:50:33 +0100100 is_enh ? " ENH" : "");
101 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
102 puts(" WRREL\n");
103 else
104 putc('\n');
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100105 }
106 }
Heinrich Schuchardtd5210e42020-03-30 07:24:18 +0200107 ret = mmc_send_ext_csd(mmc, ext_csd);
108 if (ret)
109 return;
110 wp = ext_csd[EXT_CSD_BOOT_WP_STATUS];
111 for (i = 0; i < 2; ++i) {
112 printf("Boot area %d is ", i);
113 switch (wp & 3) {
114 case 0:
115 printf("not write protected\n");
116 break;
117 case 1:
118 printf("power on protected\n");
119 break;
120 case 2:
121 printf("permanently protected\n");
122 break;
123 default:
124 printf("in reserved protection state\n");
125 break;
126 }
127 wp >>= 2;
128 }
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100129 }
Andy Fleming272cc702008-10-30 16:41:01 -0500130}
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530131
132static struct mmc *__init_mmc_device(int dev, bool force_init,
133 enum bus_mode speed_mode)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200134{
135 struct mmc *mmc;
136 mmc = find_mmc_device(dev);
137 if (!mmc) {
138 printf("no mmc device at slot %x\n", dev);
139 return NULL;
140 }
Eric Nelsonbcfde7f2016-03-27 12:00:14 -0700141
Marek Vasutd2a08362019-01-03 22:09:43 +0100142 if (!mmc_getcd(mmc))
143 force_init = true;
144
Stephen Warren1ae24a52014-05-23 13:24:45 -0600145 if (force_init)
146 mmc->has_init = 0;
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530147
148 if (IS_ENABLED(CONFIG_MMC_SPEED_MODE_SET))
149 mmc->user_speed_mode = speed_mode;
150
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200151 if (mmc_init(mmc))
152 return NULL;
Marek Vasut1d044d32019-01-03 22:09:44 +0100153
154#ifdef CONFIG_BLOCK_CACHE
155 struct blk_desc *bd = mmc_get_blk_desc(mmc);
156 blkcache_invalidate(bd->if_type, bd->devnum);
157#endif
158
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200159 return mmc;
160}
Simon Glass09140112020-05-10 11:40:03 -0600161
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530162static struct mmc *init_mmc_device(int dev, bool force_init)
163{
164 return __init_mmc_device(dev, force_init, MMC_MODES_END);
165}
166
Simon Glass09140112020-05-10 11:40:03 -0600167static int do_mmcinfo(struct cmd_tbl *cmdtp, int flag, int argc,
168 char *const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500169{
170 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500171
Lei Wenea6ebe22011-05-02 16:26:25 +0000172 if (curr_device < 0) {
173 if (get_mmc_num() > 0)
174 curr_device = 0;
175 else {
176 puts("No MMC device available\n");
177 return 1;
178 }
179 }
Andy Fleming272cc702008-10-30 16:41:01 -0500180
Stephen Warren1ae24a52014-05-23 13:24:45 -0600181 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200182 if (!mmc)
183 return CMD_RET_FAILURE;
Andy Fleming272cc702008-10-30 16:41:01 -0500184
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200185 print_mmcinfo(mmc);
186 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500187}
188
Alex Kiernan5a7b11e2018-05-08 04:43:31 +0000189#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200190static int confirm_key_prog(void)
191{
192 puts("Warning: Programming authentication key can be done only once !\n"
193 " Use this command only if you are sure of what you are doing,\n"
194 "Really perform the key programming? <y/N> ");
195 if (confirm_yesno())
196 return 1;
197
198 puts("Authentication key programming aborted\n");
199 return 0;
200}
Simon Glass09140112020-05-10 11:40:03 -0600201
202static int do_mmcrpmb_key(struct cmd_tbl *cmdtp, int flag,
203 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200204{
205 void *key_addr;
206 struct mmc *mmc = find_mmc_device(curr_device);
207
208 if (argc != 2)
209 return CMD_RET_USAGE;
210
Simon Glass7e5f4602021-07-24 09:03:29 -0600211 key_addr = (void *)hextoul(argv[1], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200212 if (!confirm_key_prog())
213 return CMD_RET_FAILURE;
214 if (mmc_rpmb_set_key(mmc, key_addr)) {
215 printf("ERROR - Key already programmed ?\n");
216 return CMD_RET_FAILURE;
217 }
218 return CMD_RET_SUCCESS;
219}
Simon Glass09140112020-05-10 11:40:03 -0600220
221static int do_mmcrpmb_read(struct cmd_tbl *cmdtp, int flag,
222 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200223{
224 u16 blk, cnt;
225 void *addr;
226 int n;
227 void *key_addr = NULL;
228 struct mmc *mmc = find_mmc_device(curr_device);
229
230 if (argc < 4)
231 return CMD_RET_USAGE;
232
Simon Glass7e5f4602021-07-24 09:03:29 -0600233 addr = (void *)hextoul(argv[1], NULL);
234 blk = hextoul(argv[2], NULL);
235 cnt = hextoul(argv[3], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200236
237 if (argc == 5)
Simon Glass7e5f4602021-07-24 09:03:29 -0600238 key_addr = (void *)hextoul(argv[4], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200239
240 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
241 curr_device, blk, cnt);
242 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
243
244 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
245 if (n != cnt)
246 return CMD_RET_FAILURE;
247 return CMD_RET_SUCCESS;
248}
Simon Glass09140112020-05-10 11:40:03 -0600249
250static int do_mmcrpmb_write(struct cmd_tbl *cmdtp, int flag,
251 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200252{
253 u16 blk, cnt;
254 void *addr;
255 int n;
256 void *key_addr;
257 struct mmc *mmc = find_mmc_device(curr_device);
258
259 if (argc != 5)
260 return CMD_RET_USAGE;
261
Simon Glass7e5f4602021-07-24 09:03:29 -0600262 addr = (void *)hextoul(argv[1], NULL);
263 blk = hextoul(argv[2], NULL);
264 cnt = hextoul(argv[3], NULL);
265 key_addr = (void *)hextoul(argv[4], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200266
267 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
268 curr_device, blk, cnt);
269 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
270
271 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
272 if (n != cnt)
273 return CMD_RET_FAILURE;
274 return CMD_RET_SUCCESS;
275}
Simon Glass09140112020-05-10 11:40:03 -0600276
277static int do_mmcrpmb_counter(struct cmd_tbl *cmdtp, int flag,
278 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200279{
280 unsigned long counter;
281 struct mmc *mmc = find_mmc_device(curr_device);
282
283 if (mmc_rpmb_get_counter(mmc, &counter))
284 return CMD_RET_FAILURE;
285 printf("RPMB Write counter= %lx\n", counter);
286 return CMD_RET_SUCCESS;
287}
288
Simon Glass09140112020-05-10 11:40:03 -0600289static struct cmd_tbl cmd_rpmb[] = {
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200290 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
291 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
292 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
293 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
294};
295
Simon Glass09140112020-05-10 11:40:03 -0600296static int do_mmcrpmb(struct cmd_tbl *cmdtp, int flag,
297 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200298{
Simon Glass09140112020-05-10 11:40:03 -0600299 struct cmd_tbl *cp;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200300 struct mmc *mmc;
301 char original_part;
302 int ret;
303
304 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
305
306 /* Drop the rpmb subcommand */
307 argc--;
308 argv++;
309
310 if (cp == NULL || argc > cp->maxargs)
311 return CMD_RET_USAGE;
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100312 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200313 return CMD_RET_SUCCESS;
314
Stephen Warren1ae24a52014-05-23 13:24:45 -0600315 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200316 if (!mmc)
317 return CMD_RET_FAILURE;
318
319 if (!(mmc->version & MMC_VERSION_MMC)) {
Heinrich Schuchardt71a3e5c2020-03-29 19:26:57 +0000320 printf("It is not an eMMC device\n");
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200321 return CMD_RET_FAILURE;
322 }
323 if (mmc->version < MMC_VERSION_4_41) {
324 printf("RPMB not supported before version 4.41\n");
325 return CMD_RET_FAILURE;
326 }
327 /* Switch to the RPMB partition */
Kever Yang4dc80c82017-06-08 09:20:03 +0800328#ifndef CONFIG_BLK
Marek Vasutb955e422016-05-04 16:35:25 +0200329 original_part = mmc->block_dev.hwpart;
Kever Yang4dc80c82017-06-08 09:20:03 +0800330#else
331 original_part = mmc_get_blk_desc(mmc)->hwpart;
332#endif
Simon Glass69f45cd2016-05-01 13:52:29 -0600333 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, MMC_PART_RPMB) !=
334 0)
Stephen Warren873cc1d2015-12-07 11:38:49 -0700335 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200336 ret = cp->cmd(cmdtp, flag, argc, argv);
337
338 /* Return to original partition */
Simon Glass69f45cd2016-05-01 13:52:29 -0600339 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, original_part) !=
340 0)
Stephen Warren873cc1d2015-12-07 11:38:49 -0700341 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200342 return ret;
343}
344#endif
345
Simon Glass09140112020-05-10 11:40:03 -0600346static int do_mmc_read(struct cmd_tbl *cmdtp, int flag,
347 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200348{
349 struct mmc *mmc;
350 u32 blk, cnt, n;
351 void *addr;
352
353 if (argc != 4)
354 return CMD_RET_USAGE;
355
Simon Glass7e5f4602021-07-24 09:03:29 -0600356 addr = (void *)hextoul(argv[1], NULL);
357 blk = hextoul(argv[2], NULL);
358 cnt = hextoul(argv[3], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200359
Stephen Warren1ae24a52014-05-23 13:24:45 -0600360 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200361 if (!mmc)
362 return CMD_RET_FAILURE;
363
364 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
365 curr_device, blk, cnt);
366
Simon Glassc40fdca2016-05-01 13:52:35 -0600367 n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200368 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
369
370 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
371}
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +0100372
Alex Kiernanc232d142018-05-29 15:30:52 +0000373#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar732bc7c2018-04-06 12:05:24 +0530374static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk,
375 lbaint_t blkcnt, const void *buffer)
376{
377 struct blk_desc *dev_desc = info->priv;
378
379 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
380}
381
382static lbaint_t mmc_sparse_reserve(struct sparse_storage *info,
383 lbaint_t blk, lbaint_t blkcnt)
384{
385 return blkcnt;
386}
387
Simon Glass09140112020-05-10 11:40:03 -0600388static int do_mmc_sparse_write(struct cmd_tbl *cmdtp, int flag,
389 int argc, char *const argv[])
Jassi Brar732bc7c2018-04-06 12:05:24 +0530390{
391 struct sparse_storage sparse;
392 struct blk_desc *dev_desc;
393 struct mmc *mmc;
394 char dest[11];
395 void *addr;
396 u32 blk;
397
398 if (argc != 3)
399 return CMD_RET_USAGE;
400
Simon Glass7e5f4602021-07-24 09:03:29 -0600401 addr = (void *)hextoul(argv[1], NULL);
402 blk = hextoul(argv[2], NULL);
Jassi Brar732bc7c2018-04-06 12:05:24 +0530403
404 if (!is_sparse_image(addr)) {
405 printf("Not a sparse image\n");
406 return CMD_RET_FAILURE;
407 }
408
409 mmc = init_mmc_device(curr_device, false);
410 if (!mmc)
411 return CMD_RET_FAILURE;
412
413 printf("\nMMC Sparse write: dev # %d, block # %d ... ",
414 curr_device, blk);
415
416 if (mmc_getwp(mmc) == 1) {
417 printf("Error: card is write protected!\n");
418 return CMD_RET_FAILURE;
419 }
420
421 dev_desc = mmc_get_blk_desc(mmc);
422 sparse.priv = dev_desc;
423 sparse.blksz = 512;
424 sparse.start = blk;
425 sparse.size = dev_desc->lba - blk;
426 sparse.write = mmc_sparse_write;
427 sparse.reserve = mmc_sparse_reserve;
428 sparse.mssg = NULL;
429 sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz);
430
Alex Kiernanc4ded032018-05-29 15:30:40 +0000431 if (write_sparse_image(&sparse, dest, addr, NULL))
Jassi Brar732bc7c2018-04-06 12:05:24 +0530432 return CMD_RET_FAILURE;
433 else
434 return CMD_RET_SUCCESS;
435}
436#endif
437
Alex Kiernanc232d142018-05-29 15:30:52 +0000438#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glass09140112020-05-10 11:40:03 -0600439static int do_mmc_write(struct cmd_tbl *cmdtp, int flag,
440 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200441{
442 struct mmc *mmc;
443 u32 blk, cnt, n;
444 void *addr;
445
446 if (argc != 4)
447 return CMD_RET_USAGE;
448
Simon Glass7e5f4602021-07-24 09:03:29 -0600449 addr = (void *)hextoul(argv[1], NULL);
450 blk = hextoul(argv[2], NULL);
451 cnt = hextoul(argv[3], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200452
Stephen Warren1ae24a52014-05-23 13:24:45 -0600453 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200454 if (!mmc)
455 return CMD_RET_FAILURE;
456
457 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
458 curr_device, blk, cnt);
459
460 if (mmc_getwp(mmc) == 1) {
461 printf("Error: card is write protected!\n");
462 return CMD_RET_FAILURE;
463 }
Simon Glassc40fdca2016-05-01 13:52:35 -0600464 n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200465 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
466
467 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
468}
Simon Glass09140112020-05-10 11:40:03 -0600469
470static int do_mmc_erase(struct cmd_tbl *cmdtp, int flag,
471 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200472{
473 struct mmc *mmc;
474 u32 blk, cnt, n;
475
476 if (argc != 3)
477 return CMD_RET_USAGE;
478
Simon Glass7e5f4602021-07-24 09:03:29 -0600479 blk = hextoul(argv[1], NULL);
480 cnt = hextoul(argv[2], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200481
Stephen Warren1ae24a52014-05-23 13:24:45 -0600482 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200483 if (!mmc)
484 return CMD_RET_FAILURE;
485
486 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
487 curr_device, blk, cnt);
488
489 if (mmc_getwp(mmc) == 1) {
490 printf("Error: card is write protected!\n");
491 return CMD_RET_FAILURE;
492 }
Simon Glassc40fdca2016-05-01 13:52:35 -0600493 n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200494 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
495
496 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
497}
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +0100498#endif
499
Simon Glass09140112020-05-10 11:40:03 -0600500static int do_mmc_rescan(struct cmd_tbl *cmdtp, int flag,
501 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200502{
503 struct mmc *mmc;
504
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530505 if (argc == 1) {
506 mmc = init_mmc_device(curr_device, true);
507 } else if (argc == 2) {
Heinrich Schuchardt27434702022-04-25 23:11:06 +0200508 enum bus_mode speed_mode;
509
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530510 speed_mode = (int)dectoul(argv[1], NULL);
511 mmc = __init_mmc_device(curr_device, true, speed_mode);
512 } else {
513 return CMD_RET_USAGE;
514 }
515
Stephen Warren941944e2014-05-23 13:24:46 -0600516 if (!mmc)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200517 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200518
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200519 return CMD_RET_SUCCESS;
520}
Simon Glass09140112020-05-10 11:40:03 -0600521
522static int do_mmc_part(struct cmd_tbl *cmdtp, int flag,
523 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200524{
Simon Glass4101f682016-02-29 15:25:34 -0700525 struct blk_desc *mmc_dev;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200526 struct mmc *mmc;
527
Stephen Warren1ae24a52014-05-23 13:24:45 -0600528 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200529 if (!mmc)
530 return CMD_RET_FAILURE;
531
Simon Glass3c457f42016-05-01 11:36:15 -0600532 mmc_dev = blk_get_devnum_by_type(IF_TYPE_MMC, curr_device);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200533 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
Simon Glass3e8bd462016-02-29 15:25:48 -0700534 part_print(mmc_dev);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200535 return CMD_RET_SUCCESS;
536 }
537
538 puts("get mmc type error!\n");
539 return CMD_RET_FAILURE;
540}
Simon Glass09140112020-05-10 11:40:03 -0600541
542static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag,
543 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200544{
Stephen Warren60dc58f2014-05-23 12:48:10 -0600545 int dev, part = 0, ret;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200546 struct mmc *mmc;
547
548 if (argc == 1) {
549 dev = curr_device;
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530550 mmc = init_mmc_device(dev, true);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200551 } else if (argc == 2) {
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530552 dev = (int)dectoul(argv[1], NULL);
553 mmc = init_mmc_device(dev, true);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200554 } else if (argc == 3) {
Simon Glass0b1284e2021-07-24 09:03:30 -0600555 dev = (int)dectoul(argv[1], NULL);
556 part = (int)dectoul(argv[2], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200557 if (part > PART_ACCESS_MASK) {
558 printf("#part_num shouldn't be larger than %d\n",
559 PART_ACCESS_MASK);
560 return CMD_RET_FAILURE;
561 }
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530562 mmc = init_mmc_device(dev, true);
563 } else if (argc == 4) {
Heinrich Schuchardt27434702022-04-25 23:11:06 +0200564 enum bus_mode speed_mode;
565
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530566 dev = (int)dectoul(argv[1], NULL);
567 part = (int)dectoul(argv[2], NULL);
568 if (part > PART_ACCESS_MASK) {
569 printf("#part_num shouldn't be larger than %d\n",
570 PART_ACCESS_MASK);
571 return CMD_RET_FAILURE;
572 }
573 speed_mode = (int)dectoul(argv[3], NULL);
574 mmc = __init_mmc_device(dev, true, speed_mode);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200575 } else {
576 return CMD_RET_USAGE;
577 }
578
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200579 if (!mmc)
580 return CMD_RET_FAILURE;
581
Simon Glass69f45cd2016-05-01 13:52:29 -0600582 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
Stephen Warren60dc58f2014-05-23 12:48:10 -0600583 printf("switch to partitions #%d, %s\n",
584 part, (!ret) ? "OK" : "ERROR");
585 if (ret)
586 return 1;
587
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200588 curr_device = dev;
589 if (mmc->part_config == MMCPART_NOAVAILABLE)
590 printf("mmc%d is current device\n", curr_device);
591 else
592 printf("mmc%d(part %d) is current device\n",
Simon Glassc40fdca2016-05-01 13:52:35 -0600593 curr_device, mmc_get_blk_desc(mmc)->hwpart);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200594
595 return CMD_RET_SUCCESS;
596}
Simon Glass09140112020-05-10 11:40:03 -0600597
598static int do_mmc_list(struct cmd_tbl *cmdtp, int flag,
599 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200600{
601 print_mmc_devices('\n');
602 return CMD_RET_SUCCESS;
603}
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100604
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +0100605#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Marek Vasutf702dc12021-09-15 11:43:13 +0200606static void parse_hwpart_user_enh_size(struct mmc *mmc,
607 struct mmc_hwpart_conf *pconf,
608 char *argv)
609{
Marek Vasut1d4b3b22022-01-17 22:54:29 +0100610 int i, ret;
Marek Vasutf702dc12021-09-15 11:43:13 +0200611
612 pconf->user.enh_size = 0;
613
614 if (!strcmp(argv, "-")) { /* The rest of eMMC */
615 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
616 ret = mmc_send_ext_csd(mmc, ext_csd);
617 if (ret)
618 return;
Marek Vasut1d4b3b22022-01-17 22:54:29 +0100619 /* The enh_size value is in 512B block units */
Marek Vasutf702dc12021-09-15 11:43:13 +0200620 pconf->user.enh_size =
621 ((ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 2] << 16) +
622 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 1] << 8) +
623 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT]) * 1024 *
624 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
625 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
626 pconf->user.enh_size -= pconf->user.enh_start;
Marek Vasut1d4b3b22022-01-17 22:54:29 +0100627 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
628 /*
629 * If the eMMC already has GP partitions set,
630 * subtract their size from the maximum USER
631 * partition size.
632 *
633 * Else, if the command was used to configure new
634 * GP partitions, subtract their size from maximum
635 * USER partition size.
636 */
637 if (mmc->capacity_gp[i]) {
638 /* The capacity_gp is in 1B units */
639 pconf->user.enh_size -= mmc->capacity_gp[i] >> 9;
640 } else if (pconf->gp_part[i].size) {
641 /* The gp_part[].size is in 512B units */
642 pconf->user.enh_size -= pconf->gp_part[i].size;
643 }
644 }
Marek Vasutf702dc12021-09-15 11:43:13 +0200645 } else {
646 pconf->user.enh_size = dectoul(argv, NULL);
647 }
648}
649
650static int parse_hwpart_user(struct mmc *mmc, struct mmc_hwpart_conf *pconf,
Simon Glass09140112020-05-10 11:40:03 -0600651 int argc, char *const argv[])
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100652{
653 int i = 0;
654
655 memset(&pconf->user, 0, sizeof(pconf->user));
656
657 while (i < argc) {
658 if (!strcmp(argv[i], "enh")) {
659 if (i + 2 >= argc)
660 return -1;
661 pconf->user.enh_start =
Simon Glass0b1284e2021-07-24 09:03:30 -0600662 dectoul(argv[i + 1], NULL);
Marek Vasutf702dc12021-09-15 11:43:13 +0200663 parse_hwpart_user_enh_size(mmc, pconf, argv[i + 2]);
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100664 i += 3;
665 } else if (!strcmp(argv[i], "wrrel")) {
666 if (i + 1 >= argc)
667 return -1;
668 pconf->user.wr_rel_change = 1;
669 if (!strcmp(argv[i+1], "on"))
670 pconf->user.wr_rel_set = 1;
671 else if (!strcmp(argv[i+1], "off"))
672 pconf->user.wr_rel_set = 0;
673 else
674 return -1;
675 i += 2;
676 } else {
677 break;
678 }
679 }
680 return i;
681}
682
683static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
Simon Glass09140112020-05-10 11:40:03 -0600684 int argc, char *const argv[])
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100685{
686 int i;
687
688 memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
689
690 if (1 >= argc)
691 return -1;
Simon Glass0b1284e2021-07-24 09:03:30 -0600692 pconf->gp_part[pidx].size = dectoul(argv[0], NULL);
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100693
694 i = 1;
695 while (i < argc) {
696 if (!strcmp(argv[i], "enh")) {
697 pconf->gp_part[pidx].enhanced = 1;
698 i += 1;
699 } else if (!strcmp(argv[i], "wrrel")) {
700 if (i + 1 >= argc)
701 return -1;
702 pconf->gp_part[pidx].wr_rel_change = 1;
703 if (!strcmp(argv[i+1], "on"))
704 pconf->gp_part[pidx].wr_rel_set = 1;
705 else if (!strcmp(argv[i+1], "off"))
706 pconf->gp_part[pidx].wr_rel_set = 0;
707 else
708 return -1;
709 i += 2;
710 } else {
711 break;
712 }
713 }
714 return i;
715}
716
Simon Glass09140112020-05-10 11:40:03 -0600717static int do_mmc_hwpartition(struct cmd_tbl *cmdtp, int flag,
718 int argc, char *const argv[])
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100719{
720 struct mmc *mmc;
721 struct mmc_hwpart_conf pconf = { };
722 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100723 int i, r, pidx;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100724
725 mmc = init_mmc_device(curr_device, false);
726 if (!mmc)
727 return CMD_RET_FAILURE;
728
Jaehoon Chung0d453c82021-09-24 09:23:34 +0900729 if (IS_SD(mmc)) {
730 puts("SD doesn't support partitioning\n");
731 return CMD_RET_FAILURE;
732 }
733
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100734 if (argc < 1)
735 return CMD_RET_USAGE;
736 i = 1;
737 while (i < argc) {
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100738 if (!strcmp(argv[i], "user")) {
739 i++;
Marek Vasutf702dc12021-09-15 11:43:13 +0200740 r = parse_hwpart_user(mmc, &pconf, argc - i, &argv[i]);
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100741 if (r < 0)
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100742 return CMD_RET_USAGE;
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100743 i += r;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100744 } else if (!strncmp(argv[i], "gp", 2) &&
745 strlen(argv[i]) == 3 &&
746 argv[i][2] >= '1' && argv[i][2] <= '4') {
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100747 pidx = argv[i][2] - '1';
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100748 i++;
749 r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
750 if (r < 0)
751 return CMD_RET_USAGE;
752 i += r;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100753 } else if (!strcmp(argv[i], "check")) {
754 mode = MMC_HWPART_CONF_CHECK;
755 i++;
756 } else if (!strcmp(argv[i], "set")) {
757 mode = MMC_HWPART_CONF_SET;
758 i++;
759 } else if (!strcmp(argv[i], "complete")) {
760 mode = MMC_HWPART_CONF_COMPLETE;
761 i++;
762 } else {
763 return CMD_RET_USAGE;
764 }
765 }
766
767 puts("Partition configuration:\n");
768 if (pconf.user.enh_size) {
769 puts("\tUser Enhanced Start: ");
770 print_size(((u64)pconf.user.enh_start) << 9, "\n");
771 puts("\tUser Enhanced Size: ");
772 print_size(((u64)pconf.user.enh_size) << 9, "\n");
773 } else {
774 puts("\tNo enhanced user data area\n");
775 }
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100776 if (pconf.user.wr_rel_change)
777 printf("\tUser partition write reliability: %s\n",
778 pconf.user.wr_rel_set ? "on" : "off");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100779 for (pidx = 0; pidx < 4; pidx++) {
780 if (pconf.gp_part[pidx].size) {
781 printf("\tGP%i Capacity: ", pidx+1);
782 print_size(((u64)pconf.gp_part[pidx].size) << 9,
783 pconf.gp_part[pidx].enhanced ?
784 " ENH\n" : "\n");
785 } else {
786 printf("\tNo GP%i partition\n", pidx+1);
787 }
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100788 if (pconf.gp_part[pidx].wr_rel_change)
789 printf("\tGP%i write reliability: %s\n", pidx+1,
790 pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100791 }
792
793 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
794 if (mode == MMC_HWPART_CONF_COMPLETE)
795 puts("Partitioning successful, "
796 "power-cycle to make effective\n");
797 return CMD_RET_SUCCESS;
798 } else {
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100799 puts("Failed!\n");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100800 return CMD_RET_FAILURE;
801 }
802}
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +0100803#endif
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100804
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200805#ifdef CONFIG_SUPPORT_EMMC_BOOT
Simon Glass09140112020-05-10 11:40:03 -0600806static int do_mmc_bootbus(struct cmd_tbl *cmdtp, int flag,
807 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200808{
809 int dev;
810 struct mmc *mmc;
811 u8 width, reset, mode;
812
813 if (argc != 5)
814 return CMD_RET_USAGE;
Simon Glass0b1284e2021-07-24 09:03:30 -0600815 dev = dectoul(argv[1], NULL);
816 width = dectoul(argv[2], NULL);
817 reset = dectoul(argv[3], NULL);
818 mode = dectoul(argv[4], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200819
Stephen Warren1ae24a52014-05-23 13:24:45 -0600820 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200821 if (!mmc)
822 return CMD_RET_FAILURE;
823
824 if (IS_SD(mmc)) {
825 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
826 return CMD_RET_FAILURE;
827 }
828
Jaehoon Chunge9978b12021-02-26 18:38:20 +0900829 /*
830 * BOOT_BUS_CONDITIONS[177]
831 * BOOT_MODE[4:3]
832 * 0x0 : Use SDR + Backward compatible timing in boot operation
833 * 0x1 : Use SDR + High Speed Timing in boot operation mode
834 * 0x2 : Use DDR in boot operation
835 * RESET_BOOT_BUS_CONDITIONS
836 * 0x0 : Reset bus width to x1, SDR, Backward compatible
837 * 0x1 : Retain BOOT_BUS_WIDTH and BOOT_MODE
838 * BOOT_BUS_WIDTH
839 * 0x0 : x1(sdr) or x4 (ddr) buswidth
840 * 0x1 : x4(sdr/ddr) buswith
841 * 0x2 : x8(sdr/ddr) buswith
842 *
843 */
844 if (width >= 0x3) {
845 printf("boot_bus_width %d is invalid\n", width);
846 return CMD_RET_FAILURE;
847 }
848
849 if (reset >= 0x2) {
850 printf("reset_boot_bus_width %d is invalid\n", reset);
851 return CMD_RET_FAILURE;
852 }
853
854 if (mode >= 0x3) {
855 printf("reset_boot_bus_width %d is invalid\n", mode);
856 return CMD_RET_FAILURE;
857 }
858
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200859 /* acknowledge to be sent during boot operation */
Jaehoon Chunge9978b12021-02-26 18:38:20 +0900860 if (mmc_set_boot_bus_width(mmc, width, reset, mode)) {
861 puts("BOOT_BUS_WIDTH is failed to change.\n");
862 return CMD_RET_FAILURE;
863 }
864
865 printf("Set to BOOT_BUS_WIDTH = 0x%x, RESET = 0x%x, BOOT_MODE = 0x%x\n",
866 width, reset, mode);
867 return CMD_RET_SUCCESS;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200868}
Simon Glass09140112020-05-10 11:40:03 -0600869
870static int do_mmc_boot_resize(struct cmd_tbl *cmdtp, int flag,
871 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200872{
873 int dev;
874 struct mmc *mmc;
875 u32 bootsize, rpmbsize;
876
877 if (argc != 4)
878 return CMD_RET_USAGE;
Simon Glass0b1284e2021-07-24 09:03:30 -0600879 dev = dectoul(argv[1], NULL);
880 bootsize = dectoul(argv[2], NULL);
881 rpmbsize = dectoul(argv[3], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200882
Stephen Warren1ae24a52014-05-23 13:24:45 -0600883 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200884 if (!mmc)
885 return CMD_RET_FAILURE;
886
887 if (IS_SD(mmc)) {
Heinrich Schuchardt71a3e5c2020-03-29 19:26:57 +0000888 printf("It is not an eMMC device\n");
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200889 return CMD_RET_FAILURE;
890 }
891
892 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
893 printf("EMMC boot partition Size change Failed.\n");
894 return CMD_RET_FAILURE;
895 }
896
897 printf("EMMC boot partition Size %d MB\n", bootsize);
898 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
899 return CMD_RET_SUCCESS;
900}
Angelo Dureghellobdb60992017-08-01 14:27:10 +0200901
Reuben Dowle5c2beda2021-05-14 10:15:43 +1200902static int mmc_partconf_print(struct mmc *mmc, const char *varname)
Angelo Dureghellobdb60992017-08-01 14:27:10 +0200903{
904 u8 ack, access, part;
905
906 if (mmc->part_config == MMCPART_NOAVAILABLE) {
907 printf("No part_config info for ver. 0x%x\n", mmc->version);
908 return CMD_RET_FAILURE;
909 }
910
911 access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
912 ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
913 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
914
Reuben Dowle5c2beda2021-05-14 10:15:43 +1200915 if(varname)
916 env_set_hex(varname, part);
917
Angelo Dureghellobdb60992017-08-01 14:27:10 +0200918 printf("EXT_CSD[179], PARTITION_CONFIG:\n"
919 "BOOT_ACK: 0x%x\n"
920 "BOOT_PARTITION_ENABLE: 0x%x\n"
921 "PARTITION_ACCESS: 0x%x\n", ack, part, access);
922
923 return CMD_RET_SUCCESS;
924}
925
Simon Glass09140112020-05-10 11:40:03 -0600926static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag,
927 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200928{
929 int dev;
930 struct mmc *mmc;
931 u8 ack, part_num, access;
932
Reuben Dowle5c2beda2021-05-14 10:15:43 +1200933 if (argc != 2 && argc != 3 && argc != 5)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200934 return CMD_RET_USAGE;
935
Simon Glass0b1284e2021-07-24 09:03:30 -0600936 dev = dectoul(argv[1], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200937
Stephen Warren1ae24a52014-05-23 13:24:45 -0600938 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200939 if (!mmc)
940 return CMD_RET_FAILURE;
941
942 if (IS_SD(mmc)) {
943 puts("PARTITION_CONFIG only exists on eMMC\n");
944 return CMD_RET_FAILURE;
945 }
946
Reuben Dowle5c2beda2021-05-14 10:15:43 +1200947 if (argc == 2 || argc == 3)
948 return mmc_partconf_print(mmc, argc == 3 ? argv[2] : NULL);
Angelo Dureghellobdb60992017-08-01 14:27:10 +0200949
Simon Glass0b1284e2021-07-24 09:03:30 -0600950 ack = dectoul(argv[2], NULL);
951 part_num = dectoul(argv[3], NULL);
952 access = dectoul(argv[4], NULL);
Angelo Dureghellobdb60992017-08-01 14:27:10 +0200953
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200954 /* acknowledge to be sent during boot operation */
955 return mmc_set_part_conf(mmc, ack, part_num, access);
956}
Simon Glass09140112020-05-10 11:40:03 -0600957
958static int do_mmc_rst_func(struct cmd_tbl *cmdtp, int flag,
959 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200960{
961 int dev;
962 struct mmc *mmc;
963 u8 enable;
964
965 /*
966 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
967 * The only valid values are 0x0, 0x1 and 0x2 and writing
968 * a value of 0x1 or 0x2 sets the value permanently.
969 */
970 if (argc != 3)
971 return CMD_RET_USAGE;
972
Simon Glass0b1284e2021-07-24 09:03:30 -0600973 dev = dectoul(argv[1], NULL);
974 enable = dectoul(argv[2], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200975
Peng Fan678e9312015-11-25 17:16:21 +0800976 if (enable > 2) {
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200977 puts("Invalid RST_n_ENABLE value\n");
978 return CMD_RET_USAGE;
979 }
980
Stephen Warren1ae24a52014-05-23 13:24:45 -0600981 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200982 if (!mmc)
983 return CMD_RET_FAILURE;
984
985 if (IS_SD(mmc)) {
986 puts("RST_n_FUNCTION only exists on eMMC\n");
987 return CMD_RET_FAILURE;
988 }
989
990 return mmc_set_rst_n_function(mmc, enable);
991}
992#endif
Simon Glass09140112020-05-10 11:40:03 -0600993static int do_mmc_setdsr(struct cmd_tbl *cmdtp, int flag,
994 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200995{
996 struct mmc *mmc;
997 u32 val;
998 int ret;
999
1000 if (argc != 2)
1001 return CMD_RET_USAGE;
Simon Glass7e5f4602021-07-24 09:03:29 -06001002 val = hextoul(argv[1], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001003
1004 mmc = find_mmc_device(curr_device);
1005 if (!mmc) {
1006 printf("no mmc device at slot %x\n", curr_device);
1007 return CMD_RET_FAILURE;
1008 }
1009 ret = mmc_set_dsr(mmc, val);
1010 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
1011 if (!ret) {
1012 mmc->has_init = 0;
1013 if (mmc_init(mmc))
1014 return CMD_RET_FAILURE;
1015 else
1016 return CMD_RET_SUCCESS;
1017 }
1018 return ret;
1019}
1020
Tomas Melincd3d4882016-11-25 11:01:03 +02001021#ifdef CONFIG_CMD_BKOPS_ENABLE
Simon Glass09140112020-05-10 11:40:03 -06001022static int do_mmc_bkops_enable(struct cmd_tbl *cmdtp, int flag,
1023 int argc, char *const argv[])
Tomas Melincd3d4882016-11-25 11:01:03 +02001024{
1025 int dev;
1026 struct mmc *mmc;
1027
1028 if (argc != 2)
1029 return CMD_RET_USAGE;
1030
Simon Glass0b1284e2021-07-24 09:03:30 -06001031 dev = dectoul(argv[1], NULL);
Tomas Melincd3d4882016-11-25 11:01:03 +02001032
1033 mmc = init_mmc_device(dev, false);
1034 if (!mmc)
1035 return CMD_RET_FAILURE;
1036
1037 if (IS_SD(mmc)) {
1038 puts("BKOPS_EN only exists on eMMC\n");
1039 return CMD_RET_FAILURE;
1040 }
1041
1042 return mmc_set_bkops_enable(mmc);
1043}
1044#endif
1045
Simon Glass09140112020-05-10 11:40:03 -06001046static int do_mmc_boot_wp(struct cmd_tbl *cmdtp, int flag,
Heinrich Schuchardt0469d842020-03-30 07:24:19 +02001047 int argc, char * const argv[])
1048{
1049 int err;
1050 struct mmc *mmc;
Ying-Chun Liu (PaulLiu)23366242022-04-25 21:59:03 +08001051 int part;
Heinrich Schuchardt0469d842020-03-30 07:24:19 +02001052
1053 mmc = init_mmc_device(curr_device, false);
1054 if (!mmc)
1055 return CMD_RET_FAILURE;
1056 if (IS_SD(mmc)) {
1057 printf("It is not an eMMC device\n");
1058 return CMD_RET_FAILURE;
1059 }
Ying-Chun Liu (PaulLiu)23366242022-04-25 21:59:03 +08001060
1061 if (argc == 2) {
1062 part = dectoul(argv[1], NULL);
1063 err = mmc_boot_wp_single_partition(mmc, part);
1064 } else {
1065 err = mmc_boot_wp(mmc);
1066 }
1067
Heinrich Schuchardt0469d842020-03-30 07:24:19 +02001068 if (err)
1069 return CMD_RET_FAILURE;
1070 printf("boot areas protected\n");
1071 return CMD_RET_SUCCESS;
1072}
1073
Simon Glass09140112020-05-10 11:40:03 -06001074static struct cmd_tbl cmd_mmc[] = {
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001075 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
1076 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
Ying-Chun Liu (PaulLiu)23366242022-04-25 21:59:03 +08001077 U_BOOT_CMD_MKENT(wp, 2, 0, do_mmc_boot_wp, "", ""),
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01001078#if CONFIG_IS_ENABLED(MMC_WRITE)
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001079 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
1080 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01001081#endif
Alex Kiernanc232d142018-05-29 15:30:52 +00001082#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
1083 U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
1084#endif
Aswath Govindraju19f7a342021-08-13 23:04:41 +05301085 U_BOOT_CMD_MKENT(rescan, 2, 1, do_mmc_rescan, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001086 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
Aswath Govindraju19f7a342021-08-13 23:04:41 +05301087 U_BOOT_CMD_MKENT(dev, 4, 0, do_mmc_dev, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001088 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +01001089#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz189f9632014-12-23 10:50:32 +01001090 U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +01001091#endif
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001092#ifdef CONFIG_SUPPORT_EMMC_BOOT
1093 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehfa7b8852014-09-25 23:00:16 +08001094 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001095 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
1096 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
1097#endif
Alex Kiernan5a7b11e2018-05-08 04:43:31 +00001098#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001099 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
1100#endif
1101 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
Tomas Melincd3d4882016-11-25 11:01:03 +02001102#ifdef CONFIG_CMD_BKOPS_ENABLE
1103 U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
1104#endif
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001105};
Andy Fleming272cc702008-10-30 16:41:01 -05001106
Simon Glass09140112020-05-10 11:40:03 -06001107static int do_mmcops(struct cmd_tbl *cmdtp, int flag, int argc,
1108 char *const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -05001109{
Simon Glass09140112020-05-10 11:40:03 -06001110 struct cmd_tbl *cp;
Lei Wen6be95cc2011-06-22 17:03:30 +00001111
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001112 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
1113
1114 /* Drop the mmc command */
1115 argc--;
1116 argv++;
1117
1118 if (cp == NULL || argc > cp->maxargs)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001119 return CMD_RET_USAGE;
Boris Brezillon80a48dd2018-12-03 22:54:20 +01001120 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001121 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -05001122
Lei Wenea6ebe22011-05-02 16:26:25 +00001123 if (curr_device < 0) {
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001124 if (get_mmc_num() > 0) {
Lei Wenea6ebe22011-05-02 16:26:25 +00001125 curr_device = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001126 } else {
Lei Wenea6ebe22011-05-02 16:26:25 +00001127 puts("No MMC device available\n");
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001128 return CMD_RET_FAILURE;
Lei Wenea6ebe22011-05-02 16:26:25 +00001129 }
1130 }
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001131 return cp->cmd(cmdtp, flag, argc, argv);
Andy Fleming272cc702008-10-30 16:41:01 -05001132}
1133
1134U_BOOT_CMD(
Diego Santa Cruz189f9632014-12-23 10:50:32 +01001135 mmc, 29, 1, do_mmcops,
Mike Frysinger852dbfd2009-03-23 22:27:34 -04001136 "MMC sub system",
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001137 "info - display info of the current MMC device\n"
1138 "mmc read addr blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +00001139 "mmc write addr blk# cnt\n"
Alex Kiernanc232d142018-05-29 15:30:52 +00001140#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar732bc7c2018-04-06 12:05:24 +05301141 "mmc swrite addr blk#\n"
1142#endif
Lei Wene6f99a52011-06-22 17:03:31 +00001143 "mmc erase blk# cnt\n"
Aswath Govindraju19f7a342021-08-13 23:04:41 +05301144 "mmc rescan [mode]\n"
Lei Wenea6ebe22011-05-02 16:26:25 +00001145 "mmc part - lists available partition on current mmc device\n"
Aswath Govindraju19f7a342021-08-13 23:04:41 +05301146 "mmc dev [dev] [part] [mode] - show or set current mmc device [partition] and set mode\n"
1147 " - the required speed mode is passed as the index from the following list\n"
1148 " [MMC_LEGACY, MMC_HS, SD_HS, MMC_HS_52, MMC_DDR_52, UHS_SDR12, UHS_SDR25,\n"
1149 " UHS_SDR50, UHS_DDR50, UHS_SDR104, MMC_HS_200, MMC_HS_400, MMC_HS_400_ES]\n"
Amar2a91c912013-04-27 11:43:00 +05301150 "mmc list - lists available devices\n"
Ying-Chun Liu (PaulLiu)23366242022-04-25 21:59:03 +08001151 "mmc wp [PART] - power on write protect boot partitions\n"
1152 " arguments:\n"
1153 " PART - [0|1]\n"
1154 " : 0 - first boot partition, 1 - second boot partition\n"
1155 " if not assigned, write protect all boot partitions\n"
Alex Kiernan84593672018-06-11 16:20:09 +00001156#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Jaehoon Chung8ae82c42021-02-26 16:07:18 +09001157 "mmc hwpartition <USER> <GP> <MODE> - does hardware partitioning\n"
Diego Santa Cruzc599f532014-12-23 10:50:30 +01001158 " arguments (sizes in 512-byte blocks):\n"
Jaehoon Chung8ae82c42021-02-26 16:07:18 +09001159 " USER - <user> <enh> <start> <cnt> <wrrel> <{on|off}>\n"
1160 " : sets user data area attributes\n"
1161 " GP - <{gp1|gp2|gp3|gp4}> <cnt> <enh> <wrrel> <{on|off}>\n"
1162 " : general purpose partition\n"
1163 " MODE - <{check|set|complete}>\n"
1164 " : mode, complete set partitioning completed\n"
Diego Santa Cruz189f9632014-12-23 10:50:32 +01001165 " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
1166 " Power cycling is required to initialize partitions after set to complete.\n"
Alex Kiernan84593672018-06-11 16:20:09 +00001167#endif
Amar2a91c912013-04-27 11:43:00 +05301168#ifdef CONFIG_SUPPORT_EMMC_BOOT
Jaehoon Chung1019b192020-12-16 07:24:50 +09001169 "mmc bootbus <dev> <boot_bus_width> <reset_boot_bus_width> <boot_mode>\n"
Tom Rini5a99b9d2014-02-05 10:24:22 -05001170 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rinif1fd9572014-02-05 10:24:20 -05001171 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
1172 " - Change sizes of boot and RPMB partitions of specified device\n"
Reuben Dowle5c2beda2021-05-14 10:15:43 +12001173 "mmc partconf <dev> [[varname] | [<boot_ack> <boot_partition> <partition_access>]]\n"
Angelo Dureghellobdb60992017-08-01 14:27:10 +02001174 " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
Reuben Dowle5c2beda2021-05-14 10:15:43 +12001175 " If showing the bits, optionally store the boot_partition field into varname\n"
Jaehoon Chung1019b192020-12-16 07:24:50 +09001176 "mmc rst-function <dev> <value>\n"
Tom Rini33ace362014-02-07 14:15:20 -05001177 " - Change the RST_n_FUNCTION field of the specified device\n"
1178 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behme3511b4e2009-02-18 19:59:39 +01001179#endif
Alex Kiernan5a7b11e2018-05-08 04:43:31 +00001180#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001181 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
1182 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
1183 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
1184 "mmc rpmb counter - read the value of the write counter\n"
1185#endif
1186 "mmc setdsr <value> - set DSR register value\n"
Tomas Melincd3d4882016-11-25 11:01:03 +02001187#ifdef CONFIG_CMD_BKOPS_ENABLE
1188 "mmc bkops-enable <dev> - enable background operations handshake on device\n"
1189 " WARNING: This is a write-once setting.\n"
1190#endif
Amar2a91c912013-04-27 11:43:00 +05301191 );
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001192
1193/* Old command kept for compatibility. Same as 'mmc info' */
1194U_BOOT_CMD(
1195 mmcinfo, 1, 0, do_mmcinfo,
1196 "display MMC info",
1197 "- display info of the current MMC device"
1198);