blob: c0c23ee24b66ee28b97ca1c6be9bcb0e07180f00 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk7a428cc2003-06-15 22:40:42 +00002/*
3 * (C) Copyright 2003
4 * Kyle Harris, kharris@nexus-tech.net
wdenk7a428cc2003-06-15 22:40:42 +00005 */
6
Simon Glass655306c2020-05-10 11:39:58 -06007#include <blk.h>
wdenk7a428cc2003-06-15 22:40:42 +00008#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -07009#include <console.h>
Simon Glass1ab16922022-07-31 12:28:48 -060010#include <display_options.h>
Marek Vasutdfff8ed2020-06-20 14:28:25 +020011#include <memalign.h>
wdenk7a428cc2003-06-15 22:40:42 +000012#include <mmc.h>
Simon Glass655306c2020-05-10 11:39:58 -060013#include <part.h>
Jassi Brar814a9752018-04-06 12:05:24 +053014#include <sparse_format.h>
15#include <image-sparse.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060016#include <vsprintf.h>
Tim Harvey728cbde2024-05-31 08:36:34 -070017#include <linux/ctype.h>
wdenk7a428cc2003-06-15 22:40:42 +000018
Mike Frysinger0289fe62009-06-14 21:35:21 -040019static int curr_device = -1;
Andy Flemingad347bb2008-10-30 16:41:01 -050020
21static void print_mmcinfo(struct mmc *mmc)
22{
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010023 int i;
24
Pantelis Antoniou2c850462014-03-11 19:34:20 +020025 printf("Device: %s\n", mmc->cfg->name);
Andy Flemingad347bb2008-10-30 16:41:01 -050026 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
Max Merchel1414ada2022-02-10 10:16:39 +010027 if (IS_SD(mmc)) {
28 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
29 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
30 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
31 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
32 } else {
33 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xff);
34 printf("Name: %c%c%c%c%c%c \n", mmc->cid[0] & 0xff,
35 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
36 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
37 (mmc->cid[2] >> 24));
38 }
Andy Flemingad347bb2008-10-30 16:41:01 -050039
Jean-Jacques Hiblotf62caf92017-09-21 16:29:56 +020040 printf("Bus Speed: %d\n", mmc->clock);
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +010041#if CONFIG_IS_ENABLED(MMC_VERBOSE)
Marek Vasutd1e63462019-03-18 04:49:21 +010042 printf("Mode: %s\n", mmc_mode_name(mmc->selected_mode));
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +010043 mmc_dump_capabilities("card capabilities", mmc->card_caps);
44 mmc_dump_capabilities("host capabilities", mmc->host_caps);
45#endif
Andy Flemingad347bb2008-10-30 16:41:01 -050046 printf("Rd Block Len: %d\n", mmc->read_bl_len);
47
Pantelis Antonioua095bfd2015-01-23 12:12:01 +020048 printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
49 EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
50 EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
51 if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
52 printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
53 printf("\n");
Andy Flemingad347bb2008-10-30 16:41:01 -050054
55 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang316f81a2011-01-04 01:04:19 +000056 puts("Capacity: ");
57 print_size(mmc->capacity, "\n");
Andy Flemingad347bb2008-10-30 16:41:01 -050058
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -060059 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
60 mmc->ddr_mode ? " DDR" : "");
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010061
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +010062#if CONFIG_IS_ENABLED(MMC_WRITE)
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010063 puts("Erase Group Size: ");
64 print_size(((u64)mmc->erase_grp_size) << 9, "\n");
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +010065#endif
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010066
Diego Santa Cruz1f69e222014-12-23 10:50:19 +010067 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010068 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
Diego Santa Cruz2b45f582014-12-23 10:50:23 +010069 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
Marek Vasutdfff8ed2020-06-20 14:28:25 +020070 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
71 u8 wp;
Heinrich Schuchardt0c8ffff2020-03-30 07:24:18 +020072 int ret;
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010073
Jean-Jacques Hiblotba54ab82018-01-04 15:23:36 +010074#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010075 puts("HC WP Group Size: ");
76 print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
Jean-Jacques Hiblotba54ab82018-01-04 15:23:36 +010077#endif
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010078
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010079 puts("User Capacity: ");
Diego Santa Cruz37a50b92014-12-23 10:50:33 +010080 print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
81 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
82 puts(" WRREL\n");
83 else
84 putc('\n');
Diego Santa Cruz2b45f582014-12-23 10:50:23 +010085 if (usr_enh) {
86 puts("User Enhanced Start: ");
87 print_size(mmc->enh_user_start, "\n");
88 puts("User Enhanced Size: ");
89 print_size(mmc->enh_user_size, "\n");
90 }
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010091 puts("Boot Capacity: ");
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010092 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010093 puts("RPMB Capacity: ");
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010094 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010095
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010096 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010097 bool is_enh = has_enh &&
98 (mmc->part_attr & EXT_CSD_ENH_GP(i));
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010099 if (mmc->capacity_gp[i]) {
Diego Santa Cruzd8358432014-12-23 10:50:18 +0100100 printf("GP%i Capacity: ", i+1);
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +0100101 print_size(mmc->capacity_gp[i],
Diego Santa Cruz37a50b92014-12-23 10:50:33 +0100102 is_enh ? " ENH" : "");
103 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
104 puts(" WRREL\n");
105 else
106 putc('\n');
Diego Santa Cruz99ca0282014-12-23 10:50:16 +0100107 }
108 }
Heinrich Schuchardt0c8ffff2020-03-30 07:24:18 +0200109 ret = mmc_send_ext_csd(mmc, ext_csd);
110 if (ret)
111 return;
112 wp = ext_csd[EXT_CSD_BOOT_WP_STATUS];
113 for (i = 0; i < 2; ++i) {
114 printf("Boot area %d is ", i);
115 switch (wp & 3) {
116 case 0:
117 printf("not write protected\n");
118 break;
119 case 1:
120 printf("power on protected\n");
121 break;
122 case 2:
123 printf("permanently protected\n");
124 break;
125 default:
126 printf("in reserved protection state\n");
127 break;
128 }
129 wp >>= 2;
130 }
Diego Santa Cruz99ca0282014-12-23 10:50:16 +0100131 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500132}
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530133
134static struct mmc *__init_mmc_device(int dev, bool force_init,
135 enum bus_mode speed_mode)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200136{
137 struct mmc *mmc;
138 mmc = find_mmc_device(dev);
139 if (!mmc) {
140 printf("no mmc device at slot %x\n", dev);
141 return NULL;
142 }
Eric Nelsonbc181662016-03-27 12:00:14 -0700143
Marek Vasutcb5df5d2019-01-03 22:09:43 +0100144 if (!mmc_getcd(mmc))
145 force_init = true;
146
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600147 if (force_init)
148 mmc->has_init = 0;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530149
150 if (IS_ENABLED(CONFIG_MMC_SPEED_MODE_SET))
151 mmc->user_speed_mode = speed_mode;
152
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200153 if (mmc_init(mmc))
154 return NULL;
Marek Vasut829540f2019-01-03 22:09:44 +0100155
156#ifdef CONFIG_BLOCK_CACHE
157 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Simon Glassfada3f92022-09-17 09:00:09 -0600158 blkcache_invalidate(bd->uclass_id, bd->devnum);
Marek Vasut829540f2019-01-03 22:09:44 +0100159#endif
160
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200161 return mmc;
162}
Simon Glassed38aef2020-05-10 11:40:03 -0600163
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530164static struct mmc *init_mmc_device(int dev, bool force_init)
165{
166 return __init_mmc_device(dev, force_init, MMC_MODES_END);
167}
168
Simon Glassed38aef2020-05-10 11:40:03 -0600169static int do_mmcinfo(struct cmd_tbl *cmdtp, int flag, int argc,
170 char *const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500171{
172 struct mmc *mmc;
Andy Flemingad347bb2008-10-30 16:41:01 -0500173
Lei Wend430d7c2011-05-02 16:26:25 +0000174 if (curr_device < 0) {
175 if (get_mmc_num() > 0)
176 curr_device = 0;
177 else {
178 puts("No MMC device available\n");
Pali Rohár332c2302023-03-22 21:06:53 +0100179 return CMD_RET_FAILURE;
Lei Wend430d7c2011-05-02 16:26:25 +0000180 }
181 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500182
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600183 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200184 if (!mmc)
185 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500186
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200187 print_mmcinfo(mmc);
188 return CMD_RET_SUCCESS;
189}
Andy Flemingad347bb2008-10-30 16:41:01 -0500190
Alex Kiernan60e0f612018-05-08 04:43:31 +0000191#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200192static int confirm_key_prog(void)
193{
194 puts("Warning: Programming authentication key can be done only once !\n"
195 " Use this command only if you are sure of what you are doing,\n"
196 "Really perform the key programming? <y/N> ");
197 if (confirm_yesno())
Lei Wend430d7c2011-05-02 16:26:25 +0000198 return 1;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200199
200 puts("Authentication key programming aborted\n");
201 return 0;
202}
Simon Glassed38aef2020-05-10 11:40:03 -0600203
204static int do_mmcrpmb_key(struct cmd_tbl *cmdtp, int flag,
205 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200206{
207 void *key_addr;
208 struct mmc *mmc = find_mmc_device(curr_device);
209
210 if (argc != 2)
211 return CMD_RET_USAGE;
212
Simon Glass3ff49ec2021-07-24 09:03:29 -0600213 key_addr = (void *)hextoul(argv[1], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200214 if (!confirm_key_prog())
215 return CMD_RET_FAILURE;
216 if (mmc_rpmb_set_key(mmc, key_addr)) {
217 printf("ERROR - Key already programmed ?\n");
218 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500219 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200220 return CMD_RET_SUCCESS;
Andy Flemingad347bb2008-10-30 16:41:01 -0500221}
Simon Glassed38aef2020-05-10 11:40:03 -0600222
223static int do_mmcrpmb_read(struct cmd_tbl *cmdtp, int flag,
224 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200225{
226 u16 blk, cnt;
227 void *addr;
228 int n;
229 void *key_addr = NULL;
230 struct mmc *mmc = find_mmc_device(curr_device);
Andy Flemingad347bb2008-10-30 16:41:01 -0500231
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200232 if (argc < 4)
233 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500234
Simon Glass3ff49ec2021-07-24 09:03:29 -0600235 addr = (void *)hextoul(argv[1], NULL);
236 blk = hextoul(argv[2], NULL);
237 cnt = hextoul(argv[3], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200238
239 if (argc == 5)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600240 key_addr = (void *)hextoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200241
Simon Glass9703a932024-08-22 07:57:51 -0600242 printf("MMC RPMB read: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200243 curr_device, blk, cnt);
244 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
245
246 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
247 if (n != cnt)
248 return CMD_RET_FAILURE;
249 return CMD_RET_SUCCESS;
250}
Simon Glassed38aef2020-05-10 11:40:03 -0600251
252static int do_mmcrpmb_write(struct cmd_tbl *cmdtp, int flag,
253 int argc, char *const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500254{
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200255 u16 blk, cnt;
256 void *addr;
257 int n;
258 void *key_addr;
259 struct mmc *mmc = find_mmc_device(curr_device);
Lei Wen64b2b1e2011-06-22 17:03:30 +0000260
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200261 if (argc != 5)
Simon Glassa06dfc72011-12-10 08:44:01 +0000262 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500263
Simon Glass3ff49ec2021-07-24 09:03:29 -0600264 addr = (void *)hextoul(argv[1], NULL);
265 blk = hextoul(argv[2], NULL);
266 cnt = hextoul(argv[3], NULL);
267 key_addr = (void *)hextoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200268
Simon Glass9703a932024-08-22 07:57:51 -0600269 printf("MMC RPMB write: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200270 curr_device, blk, cnt);
271 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
272
273 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
274 if (n != cnt)
275 return CMD_RET_FAILURE;
276 return CMD_RET_SUCCESS;
277}
Simon Glassed38aef2020-05-10 11:40:03 -0600278
279static int do_mmcrpmb_counter(struct cmd_tbl *cmdtp, int flag,
280 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200281{
282 unsigned long counter;
283 struct mmc *mmc = find_mmc_device(curr_device);
284
285 if (mmc_rpmb_get_counter(mmc, &counter))
286 return CMD_RET_FAILURE;
287 printf("RPMB Write counter= %lx\n", counter);
288 return CMD_RET_SUCCESS;
289}
290
Simon Glassed38aef2020-05-10 11:40:03 -0600291static struct cmd_tbl cmd_rpmb[] = {
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200292 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
293 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
294 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
295 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
296};
297
Simon Glassed38aef2020-05-10 11:40:03 -0600298static int do_mmcrpmb(struct cmd_tbl *cmdtp, int flag,
299 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200300{
Simon Glassed38aef2020-05-10 11:40:03 -0600301 struct cmd_tbl *cp;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200302 struct mmc *mmc;
303 char original_part;
304 int ret;
305
306 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
307
308 /* Drop the rpmb subcommand */
309 argc--;
310 argv++;
311
312 if (cp == NULL || argc > cp->maxargs)
313 return CMD_RET_USAGE;
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100314 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200315 return CMD_RET_SUCCESS;
316
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600317 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200318 if (!mmc)
319 return CMD_RET_FAILURE;
320
321 if (!(mmc->version & MMC_VERSION_MMC)) {
Heinrich Schuchardteeb191c2020-03-29 19:26:57 +0000322 printf("It is not an eMMC device\n");
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200323 return CMD_RET_FAILURE;
Lei Wend430d7c2011-05-02 16:26:25 +0000324 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200325 if (mmc->version < MMC_VERSION_4_41) {
326 printf("RPMB not supported before version 4.41\n");
327 return CMD_RET_FAILURE;
328 }
329 /* Switch to the RPMB partition */
Kever Yangcb5f1932017-06-08 09:20:03 +0800330#ifndef CONFIG_BLK
Marek Vasuta62cf492016-05-04 16:35:25 +0200331 original_part = mmc->block_dev.hwpart;
Kever Yangcb5f1932017-06-08 09:20:03 +0800332#else
333 original_part = mmc_get_blk_desc(mmc)->hwpart;
334#endif
Simon Glassdbfa32c2022-08-11 19:34:59 -0600335 if (blk_select_hwpart_devnum(UCLASS_MMC, curr_device, MMC_PART_RPMB) !=
Simon Glass11f2bb62016-05-01 13:52:29 -0600336 0)
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700337 return CMD_RET_FAILURE;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200338 ret = cp->cmd(cmdtp, flag, argc, argv);
Andy Flemingad347bb2008-10-30 16:41:01 -0500339
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200340 /* Return to original partition */
Simon Glassdbfa32c2022-08-11 19:34:59 -0600341 if (blk_select_hwpart_devnum(UCLASS_MMC, curr_device, original_part) !=
Simon Glass11f2bb62016-05-01 13:52:29 -0600342 0)
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700343 return CMD_RET_FAILURE;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200344 return ret;
345}
346#endif
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530347
Simon Glassed38aef2020-05-10 11:40:03 -0600348static int do_mmc_read(struct cmd_tbl *cmdtp, int flag,
349 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200350{
351 struct mmc *mmc;
352 u32 blk, cnt, n;
353 void *addr;
Stephen Warren18faca72013-04-01 11:50:28 +0000354
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200355 if (argc != 4)
356 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500357
Simon Glass3ff49ec2021-07-24 09:03:29 -0600358 addr = (void *)hextoul(argv[1], NULL);
359 blk = hextoul(argv[2], NULL);
360 cnt = hextoul(argv[3], NULL);
Lei Wen61dba932010-09-13 22:07:28 +0800361
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600362 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200363 if (!mmc)
364 return CMD_RET_FAILURE;
Lei Wen61dba932010-09-13 22:07:28 +0800365
Simon Glass9703a932024-08-22 07:57:51 -0600366 printf("MMC read: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200367 curr_device, blk, cnt);
Stephen Warren18faca72013-04-01 11:50:28 +0000368
Simon Glasse5db1152016-05-01 13:52:35 -0600369 n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200370 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Lei Wend430d7c2011-05-02 16:26:25 +0000371
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200372 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
373}
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +0100374
Alex Kiernanc568bcb2018-05-29 15:30:52 +0000375#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar814a9752018-04-06 12:05:24 +0530376static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk,
377 lbaint_t blkcnt, const void *buffer)
378{
379 struct blk_desc *dev_desc = info->priv;
380
381 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
382}
383
384static lbaint_t mmc_sparse_reserve(struct sparse_storage *info,
385 lbaint_t blk, lbaint_t blkcnt)
386{
387 return blkcnt;
388}
389
Simon Glassed38aef2020-05-10 11:40:03 -0600390static int do_mmc_sparse_write(struct cmd_tbl *cmdtp, int flag,
391 int argc, char *const argv[])
Jassi Brar814a9752018-04-06 12:05:24 +0530392{
393 struct sparse_storage sparse;
394 struct blk_desc *dev_desc;
395 struct mmc *mmc;
396 char dest[11];
397 void *addr;
398 u32 blk;
399
400 if (argc != 3)
401 return CMD_RET_USAGE;
402
Simon Glass3ff49ec2021-07-24 09:03:29 -0600403 addr = (void *)hextoul(argv[1], NULL);
404 blk = hextoul(argv[2], NULL);
Jassi Brar814a9752018-04-06 12:05:24 +0530405
406 if (!is_sparse_image(addr)) {
407 printf("Not a sparse image\n");
408 return CMD_RET_FAILURE;
409 }
410
411 mmc = init_mmc_device(curr_device, false);
412 if (!mmc)
413 return CMD_RET_FAILURE;
414
Simon Glass9703a932024-08-22 07:57:51 -0600415 printf("MMC Sparse write: dev # %d, block # %d ... ",
Jassi Brar814a9752018-04-06 12:05:24 +0530416 curr_device, blk);
417
418 if (mmc_getwp(mmc) == 1) {
419 printf("Error: card is write protected!\n");
420 return CMD_RET_FAILURE;
421 }
422
423 dev_desc = mmc_get_blk_desc(mmc);
424 sparse.priv = dev_desc;
425 sparse.blksz = 512;
426 sparse.start = blk;
427 sparse.size = dev_desc->lba - blk;
428 sparse.write = mmc_sparse_write;
429 sparse.reserve = mmc_sparse_reserve;
430 sparse.mssg = NULL;
431 sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz);
432
Alex Kiernan27b69de2018-05-29 15:30:40 +0000433 if (write_sparse_image(&sparse, dest, addr, NULL))
Jassi Brar814a9752018-04-06 12:05:24 +0530434 return CMD_RET_FAILURE;
435 else
436 return CMD_RET_SUCCESS;
437}
438#endif
439
Alex Kiernanc568bcb2018-05-29 15:30:52 +0000440#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glassed38aef2020-05-10 11:40:03 -0600441static int do_mmc_write(struct cmd_tbl *cmdtp, int flag,
442 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200443{
444 struct mmc *mmc;
445 u32 blk, cnt, n;
446 void *addr;
Andy Flemingad347bb2008-10-30 16:41:01 -0500447
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200448 if (argc != 4)
449 return CMD_RET_USAGE;
Lei Wend430d7c2011-05-02 16:26:25 +0000450
Simon Glass3ff49ec2021-07-24 09:03:29 -0600451 addr = (void *)hextoul(argv[1], NULL);
452 blk = hextoul(argv[2], NULL);
453 cnt = hextoul(argv[3], NULL);
Lei Wend430d7c2011-05-02 16:26:25 +0000454
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600455 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200456 if (!mmc)
457 return CMD_RET_FAILURE;
Lei Wen31b99802011-05-02 16:26:26 +0000458
Simon Glass9703a932024-08-22 07:57:51 -0600459 printf("MMC write: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200460 curr_device, blk, cnt);
Lei Wen31b99802011-05-02 16:26:26 +0000461
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200462 if (mmc_getwp(mmc) == 1) {
463 printf("Error: card is write protected!\n");
464 return CMD_RET_FAILURE;
465 }
Simon Glasse5db1152016-05-01 13:52:35 -0600466 n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200467 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Lei Wend430d7c2011-05-02 16:26:25 +0000468
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200469 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
470}
Simon Glassed38aef2020-05-10 11:40:03 -0600471
472static int do_mmc_erase(struct cmd_tbl *cmdtp, int flag,
473 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200474{
475 struct mmc *mmc;
Tomas Paukrtab00d0e2024-09-02 20:49:17 +0200476 struct disk_partition info;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200477 u32 blk, cnt, n;
Tom Rinif8c6f792014-02-05 10:24:21 -0500478
Tomas Paukrtab00d0e2024-09-02 20:49:17 +0200479 if (argc < 2 || argc > 3)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200480 return CMD_RET_USAGE;
Tom Rinif8c6f792014-02-05 10:24:21 -0500481
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600482 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200483 if (!mmc)
484 return CMD_RET_FAILURE;
Tomas Paukrtab00d0e2024-09-02 20:49:17 +0200485
486 if (argc == 3) {
487 blk = hextoul(argv[1], NULL);
488 cnt = hextoul(argv[2], NULL);
489 } else if (part_get_info_by_name(mmc_get_blk_desc(mmc), argv[1], &info) >= 0) {
490 blk = info.start;
491 cnt = info.size;
492 } else {
493 return CMD_RET_FAILURE;
494 }
Tom Rinif8c6f792014-02-05 10:24:21 -0500495
Simon Glass9703a932024-08-22 07:57:51 -0600496 printf("MMC erase: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200497 curr_device, blk, cnt);
Tom Rini4cf854c2014-02-05 10:24:22 -0500498
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200499 if (mmc_getwp(mmc) == 1) {
500 printf("Error: card is write protected!\n");
501 return CMD_RET_FAILURE;
502 }
Simon Glasse5db1152016-05-01 13:52:35 -0600503 n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200504 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Tom Rini4cf854c2014-02-05 10:24:22 -0500505
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200506 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
507}
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +0100508#endif
509
Simon Glassed38aef2020-05-10 11:40:03 -0600510static int do_mmc_rescan(struct cmd_tbl *cmdtp, int flag,
511 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200512{
513 struct mmc *mmc;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530514
515 if (argc == 1) {
516 mmc = init_mmc_device(curr_device, true);
517 } else if (argc == 2) {
Heinrich Schuchardt05c74282022-04-25 23:11:06 +0200518 enum bus_mode speed_mode;
519
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530520 speed_mode = (int)dectoul(argv[1], NULL);
521 mmc = __init_mmc_device(curr_device, true, speed_mode);
522 } else {
523 return CMD_RET_USAGE;
524 }
Tom Rini4cf854c2014-02-05 10:24:22 -0500525
Stephen Warren07ec4e72014-05-23 13:24:46 -0600526 if (!mmc)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200527 return CMD_RET_FAILURE;
Tom Rini4cf854c2014-02-05 10:24:22 -0500528
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200529 return CMD_RET_SUCCESS;
530}
Simon Glassed38aef2020-05-10 11:40:03 -0600531
532static int do_mmc_part(struct cmd_tbl *cmdtp, int flag,
533 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200534{
Simon Glasse3394752016-02-29 15:25:34 -0700535 struct blk_desc *mmc_dev;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200536 struct mmc *mmc;
Amar3ccad532013-04-27 11:43:00 +0530537
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600538 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200539 if (!mmc)
540 return CMD_RET_FAILURE;
Amar3ccad532013-04-27 11:43:00 +0530541
Simon Glassfada3f92022-09-17 09:00:09 -0600542 mmc_dev = blk_get_devnum_by_uclass_id(UCLASS_MMC, curr_device);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200543 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
Simon Glassb89a8442016-02-29 15:25:48 -0700544 part_print(mmc_dev);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200545 return CMD_RET_SUCCESS;
546 }
Amar3ccad532013-04-27 11:43:00 +0530547
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200548 puts("get mmc type error!\n");
549 return CMD_RET_FAILURE;
550}
Simon Glassed38aef2020-05-10 11:40:03 -0600551
552static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag,
553 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200554{
Stephen Warren18549802014-05-23 12:48:10 -0600555 int dev, part = 0, ret;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200556 struct mmc *mmc;
Tom Rini35a3ea12014-02-07 14:15:20 -0500557
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200558 if (argc == 1) {
559 dev = curr_device;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530560 mmc = init_mmc_device(dev, true);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200561 } else if (argc == 2) {
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530562 dev = (int)dectoul(argv[1], NULL);
563 mmc = init_mmc_device(dev, true);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200564 } else if (argc == 3) {
Simon Glassff9b9032021-07-24 09:03:30 -0600565 dev = (int)dectoul(argv[1], NULL);
566 part = (int)dectoul(argv[2], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200567 if (part > PART_ACCESS_MASK) {
568 printf("#part_num shouldn't be larger than %d\n",
569 PART_ACCESS_MASK);
570 return CMD_RET_FAILURE;
Tom Rini35a3ea12014-02-07 14:15:20 -0500571 }
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530572 mmc = init_mmc_device(dev, true);
573 } else if (argc == 4) {
Heinrich Schuchardt05c74282022-04-25 23:11:06 +0200574 enum bus_mode speed_mode;
575
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530576 dev = (int)dectoul(argv[1], NULL);
577 part = (int)dectoul(argv[2], NULL);
578 if (part > PART_ACCESS_MASK) {
579 printf("#part_num shouldn't be larger than %d\n",
580 PART_ACCESS_MASK);
581 return CMD_RET_FAILURE;
582 }
583 speed_mode = (int)dectoul(argv[3], NULL);
584 mmc = __init_mmc_device(dev, true, speed_mode);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200585 } else {
586 return CMD_RET_USAGE;
587 }
Tom Rini35a3ea12014-02-07 14:15:20 -0500588
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200589 if (!mmc)
590 return CMD_RET_FAILURE;
Tom Rini35a3ea12014-02-07 14:15:20 -0500591
Simon Glassdbfa32c2022-08-11 19:34:59 -0600592 ret = blk_select_hwpart_devnum(UCLASS_MMC, dev, part);
Stephen Warren18549802014-05-23 12:48:10 -0600593 printf("switch to partitions #%d, %s\n",
594 part, (!ret) ? "OK" : "ERROR");
595 if (ret)
596 return 1;
597
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200598 curr_device = dev;
599 if (mmc->part_config == MMCPART_NOAVAILABLE)
600 printf("mmc%d is current device\n", curr_device);
601 else
602 printf("mmc%d(part %d) is current device\n",
Simon Glasse5db1152016-05-01 13:52:35 -0600603 curr_device, mmc_get_blk_desc(mmc)->hwpart);
Tom Rini35a3ea12014-02-07 14:15:20 -0500604
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200605 return CMD_RET_SUCCESS;
606}
Simon Glassed38aef2020-05-10 11:40:03 -0600607
608static int do_mmc_list(struct cmd_tbl *cmdtp, int flag,
609 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200610{
611 print_mmc_devices('\n');
612 return CMD_RET_SUCCESS;
613}
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100614
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +0100615#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Marek Vasut94e39952021-09-15 11:43:13 +0200616static void parse_hwpart_user_enh_size(struct mmc *mmc,
617 struct mmc_hwpart_conf *pconf,
618 char *argv)
619{
Marek Vasut30d29662022-01-17 22:54:29 +0100620 int i, ret;
Marek Vasut94e39952021-09-15 11:43:13 +0200621
622 pconf->user.enh_size = 0;
623
624 if (!strcmp(argv, "-")) { /* The rest of eMMC */
625 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
626 ret = mmc_send_ext_csd(mmc, ext_csd);
627 if (ret)
628 return;
Marek Vasut30d29662022-01-17 22:54:29 +0100629 /* The enh_size value is in 512B block units */
Marek Vasut94e39952021-09-15 11:43:13 +0200630 pconf->user.enh_size =
631 ((ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 2] << 16) +
632 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 1] << 8) +
633 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT]) * 1024 *
634 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
635 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
636 pconf->user.enh_size -= pconf->user.enh_start;
Marek Vasut30d29662022-01-17 22:54:29 +0100637 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
638 /*
639 * If the eMMC already has GP partitions set,
640 * subtract their size from the maximum USER
641 * partition size.
642 *
643 * Else, if the command was used to configure new
644 * GP partitions, subtract their size from maximum
645 * USER partition size.
646 */
647 if (mmc->capacity_gp[i]) {
648 /* The capacity_gp is in 1B units */
649 pconf->user.enh_size -= mmc->capacity_gp[i] >> 9;
650 } else if (pconf->gp_part[i].size) {
651 /* The gp_part[].size is in 512B units */
652 pconf->user.enh_size -= pconf->gp_part[i].size;
653 }
654 }
Marek Vasut94e39952021-09-15 11:43:13 +0200655 } else {
656 pconf->user.enh_size = dectoul(argv, NULL);
657 }
658}
659
660static int parse_hwpart_user(struct mmc *mmc, struct mmc_hwpart_conf *pconf,
Simon Glassed38aef2020-05-10 11:40:03 -0600661 int argc, char *const argv[])
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100662{
663 int i = 0;
664
665 memset(&pconf->user, 0, sizeof(pconf->user));
666
667 while (i < argc) {
668 if (!strcmp(argv[i], "enh")) {
669 if (i + 2 >= argc)
670 return -1;
671 pconf->user.enh_start =
Simon Glassff9b9032021-07-24 09:03:30 -0600672 dectoul(argv[i + 1], NULL);
Marek Vasut94e39952021-09-15 11:43:13 +0200673 parse_hwpart_user_enh_size(mmc, pconf, argv[i + 2]);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100674 i += 3;
675 } else if (!strcmp(argv[i], "wrrel")) {
676 if (i + 1 >= argc)
677 return -1;
678 pconf->user.wr_rel_change = 1;
679 if (!strcmp(argv[i+1], "on"))
680 pconf->user.wr_rel_set = 1;
681 else if (!strcmp(argv[i+1], "off"))
682 pconf->user.wr_rel_set = 0;
683 else
684 return -1;
685 i += 2;
686 } else {
687 break;
688 }
689 }
690 return i;
691}
692
693static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
Simon Glassed38aef2020-05-10 11:40:03 -0600694 int argc, char *const argv[])
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100695{
696 int i;
697
698 memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
699
700 if (1 >= argc)
701 return -1;
Simon Glassff9b9032021-07-24 09:03:30 -0600702 pconf->gp_part[pidx].size = dectoul(argv[0], NULL);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100703
704 i = 1;
705 while (i < argc) {
706 if (!strcmp(argv[i], "enh")) {
707 pconf->gp_part[pidx].enhanced = 1;
708 i += 1;
709 } else if (!strcmp(argv[i], "wrrel")) {
710 if (i + 1 >= argc)
711 return -1;
712 pconf->gp_part[pidx].wr_rel_change = 1;
713 if (!strcmp(argv[i+1], "on"))
714 pconf->gp_part[pidx].wr_rel_set = 1;
715 else if (!strcmp(argv[i+1], "off"))
716 pconf->gp_part[pidx].wr_rel_set = 0;
717 else
718 return -1;
719 i += 2;
720 } else {
721 break;
722 }
723 }
724 return i;
725}
726
Simon Glassed38aef2020-05-10 11:40:03 -0600727static int do_mmc_hwpartition(struct cmd_tbl *cmdtp, int flag,
728 int argc, char *const argv[])
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100729{
730 struct mmc *mmc;
731 struct mmc_hwpart_conf pconf = { };
732 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100733 int i, r, pidx;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100734
735 mmc = init_mmc_device(curr_device, false);
736 if (!mmc)
737 return CMD_RET_FAILURE;
738
Jaehoon Chungffbba622021-09-24 09:23:34 +0900739 if (IS_SD(mmc)) {
740 puts("SD doesn't support partitioning\n");
741 return CMD_RET_FAILURE;
742 }
743
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100744 if (argc < 1)
745 return CMD_RET_USAGE;
746 i = 1;
747 while (i < argc) {
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100748 if (!strcmp(argv[i], "user")) {
749 i++;
Marek Vasut94e39952021-09-15 11:43:13 +0200750 r = parse_hwpart_user(mmc, &pconf, argc - i, &argv[i]);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100751 if (r < 0)
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100752 return CMD_RET_USAGE;
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100753 i += r;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100754 } else if (!strncmp(argv[i], "gp", 2) &&
755 strlen(argv[i]) == 3 &&
756 argv[i][2] >= '1' && argv[i][2] <= '4') {
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100757 pidx = argv[i][2] - '1';
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100758 i++;
759 r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
760 if (r < 0)
761 return CMD_RET_USAGE;
762 i += r;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100763 } else if (!strcmp(argv[i], "check")) {
764 mode = MMC_HWPART_CONF_CHECK;
765 i++;
766 } else if (!strcmp(argv[i], "set")) {
767 mode = MMC_HWPART_CONF_SET;
768 i++;
769 } else if (!strcmp(argv[i], "complete")) {
770 mode = MMC_HWPART_CONF_COMPLETE;
771 i++;
772 } else {
773 return CMD_RET_USAGE;
774 }
775 }
776
777 puts("Partition configuration:\n");
778 if (pconf.user.enh_size) {
779 puts("\tUser Enhanced Start: ");
780 print_size(((u64)pconf.user.enh_start) << 9, "\n");
781 puts("\tUser Enhanced Size: ");
782 print_size(((u64)pconf.user.enh_size) << 9, "\n");
783 } else {
784 puts("\tNo enhanced user data area\n");
785 }
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100786 if (pconf.user.wr_rel_change)
787 printf("\tUser partition write reliability: %s\n",
788 pconf.user.wr_rel_set ? "on" : "off");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100789 for (pidx = 0; pidx < 4; pidx++) {
790 if (pconf.gp_part[pidx].size) {
791 printf("\tGP%i Capacity: ", pidx+1);
792 print_size(((u64)pconf.gp_part[pidx].size) << 9,
793 pconf.gp_part[pidx].enhanced ?
794 " ENH\n" : "\n");
795 } else {
796 printf("\tNo GP%i partition\n", pidx+1);
797 }
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100798 if (pconf.gp_part[pidx].wr_rel_change)
799 printf("\tGP%i write reliability: %s\n", pidx+1,
800 pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100801 }
802
803 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
804 if (mode == MMC_HWPART_CONF_COMPLETE)
805 puts("Partitioning successful, "
806 "power-cycle to make effective\n");
807 return CMD_RET_SUCCESS;
808 } else {
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100809 puts("Failed!\n");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100810 return CMD_RET_FAILURE;
811 }
812}
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +0100813#endif
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100814
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200815#ifdef CONFIG_SUPPORT_EMMC_BOOT
Simon Glassed38aef2020-05-10 11:40:03 -0600816static int do_mmc_bootbus(struct cmd_tbl *cmdtp, int flag,
817 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200818{
819 int dev;
820 struct mmc *mmc;
821 u8 width, reset, mode;
822
823 if (argc != 5)
824 return CMD_RET_USAGE;
Simon Glassff9b9032021-07-24 09:03:30 -0600825 dev = dectoul(argv[1], NULL);
826 width = dectoul(argv[2], NULL);
827 reset = dectoul(argv[3], NULL);
828 mode = dectoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200829
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600830 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200831 if (!mmc)
832 return CMD_RET_FAILURE;
833
834 if (IS_SD(mmc)) {
835 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
836 return CMD_RET_FAILURE;
Amar3ccad532013-04-27 11:43:00 +0530837 }
Markus Niebel03951412013-12-16 13:40:46 +0100838
Jaehoon Chung7483dd32021-02-26 18:38:20 +0900839 /*
840 * BOOT_BUS_CONDITIONS[177]
841 * BOOT_MODE[4:3]
842 * 0x0 : Use SDR + Backward compatible timing in boot operation
843 * 0x1 : Use SDR + High Speed Timing in boot operation mode
844 * 0x2 : Use DDR in boot operation
845 * RESET_BOOT_BUS_CONDITIONS
846 * 0x0 : Reset bus width to x1, SDR, Backward compatible
847 * 0x1 : Retain BOOT_BUS_WIDTH and BOOT_MODE
848 * BOOT_BUS_WIDTH
849 * 0x0 : x1(sdr) or x4 (ddr) buswidth
850 * 0x1 : x4(sdr/ddr) buswith
851 * 0x2 : x8(sdr/ddr) buswith
852 *
853 */
854 if (width >= 0x3) {
855 printf("boot_bus_width %d is invalid\n", width);
856 return CMD_RET_FAILURE;
857 }
858
859 if (reset >= 0x2) {
860 printf("reset_boot_bus_width %d is invalid\n", reset);
861 return CMD_RET_FAILURE;
862 }
863
864 if (mode >= 0x3) {
865 printf("reset_boot_bus_width %d is invalid\n", mode);
866 return CMD_RET_FAILURE;
867 }
868
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200869 /* acknowledge to be sent during boot operation */
Jaehoon Chung7483dd32021-02-26 18:38:20 +0900870 if (mmc_set_boot_bus_width(mmc, width, reset, mode)) {
871 puts("BOOT_BUS_WIDTH is failed to change.\n");
872 return CMD_RET_FAILURE;
873 }
874
875 printf("Set to BOOT_BUS_WIDTH = 0x%x, RESET = 0x%x, BOOT_MODE = 0x%x\n",
876 width, reset, mode);
877 return CMD_RET_SUCCESS;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200878}
Simon Glassed38aef2020-05-10 11:40:03 -0600879
880static int do_mmc_boot_resize(struct cmd_tbl *cmdtp, int flag,
881 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200882{
883 int dev;
884 struct mmc *mmc;
885 u32 bootsize, rpmbsize;
Markus Niebel03951412013-12-16 13:40:46 +0100886
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200887 if (argc != 4)
888 return CMD_RET_USAGE;
Simon Glassff9b9032021-07-24 09:03:30 -0600889 dev = dectoul(argv[1], NULL);
890 bootsize = dectoul(argv[2], NULL);
891 rpmbsize = dectoul(argv[3], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200892
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600893 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200894 if (!mmc)
895 return CMD_RET_FAILURE;
896
897 if (IS_SD(mmc)) {
Heinrich Schuchardteeb191c2020-03-29 19:26:57 +0000898 printf("It is not an eMMC device\n");
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200899 return CMD_RET_FAILURE;
Markus Niebel03951412013-12-16 13:40:46 +0100900 }
901
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200902 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
903 printf("EMMC boot partition Size change Failed.\n");
904 return CMD_RET_FAILURE;
905 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500906
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200907 printf("EMMC boot partition Size %d MB\n", bootsize);
908 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
909 return CMD_RET_SUCCESS;
910}
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200911
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200912static int mmc_partconf_print(struct mmc *mmc, const char *varname)
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200913{
914 u8 ack, access, part;
915
916 if (mmc->part_config == MMCPART_NOAVAILABLE) {
917 printf("No part_config info for ver. 0x%x\n", mmc->version);
918 return CMD_RET_FAILURE;
919 }
920
921 access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
922 ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
923 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
924
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200925 if(varname)
926 env_set_hex(varname, part);
927
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200928 printf("EXT_CSD[179], PARTITION_CONFIG:\n"
929 "BOOT_ACK: 0x%x\n"
Tim Harvey728cbde2024-05-31 08:36:34 -0700930 "BOOT_PARTITION_ENABLE: 0x%x (%s)\n"
931 "PARTITION_ACCESS: 0x%x (%s)\n", ack, part, emmc_boot_part_names[part],
932 access, emmc_hwpart_names[access]);
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200933
934 return CMD_RET_SUCCESS;
935}
936
Simon Glassed38aef2020-05-10 11:40:03 -0600937static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag,
938 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200939{
Pali Rohár332c2302023-03-22 21:06:53 +0100940 int ret, dev;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200941 struct mmc *mmc;
942 u8 ack, part_num, access;
Lei Wenea526762011-06-22 17:03:31 +0000943
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200944 if (argc != 2 && argc != 3 && argc != 5)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200945 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500946
Simon Glassff9b9032021-07-24 09:03:30 -0600947 dev = dectoul(argv[1], NULL);
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530948
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600949 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200950 if (!mmc)
951 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500952
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200953 if (IS_SD(mmc)) {
954 puts("PARTITION_CONFIG only exists on eMMC\n");
955 return CMD_RET_FAILURE;
956 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500957
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200958 if (argc == 2 || argc == 3)
Simon Glass793a98e2023-11-18 14:05:20 -0700959 return mmc_partconf_print(mmc, cmd_arg2(argc, argv));
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200960
Tim Harvey728cbde2024-05-31 08:36:34 -0700961 /* BOOT_ACK */
Simon Glassff9b9032021-07-24 09:03:30 -0600962 ack = dectoul(argv[2], NULL);
Tim Harvey728cbde2024-05-31 08:36:34 -0700963 /* BOOT_PARTITION_ENABLE */
964 if (!isdigit(*argv[3])) {
965 for (part_num = ARRAY_SIZE(emmc_boot_part_names) - 1; part_num > 0; part_num--) {
966 if (!strcmp(argv[3], emmc_boot_part_names[part_num]))
967 break;
968 }
969 } else {
970 part_num = dectoul(argv[3], NULL);
971 }
972 /* PARTITION_ACCESS */
973 if (!isdigit(*argv[4])) {
974 for (access = ARRAY_SIZE(emmc_hwpart_names) - 1; access > 0; access--) {
975 if (!strcmp(argv[4], emmc_hwpart_names[access]))
976 break;
977 }
978 } else {
979 access = dectoul(argv[4], NULL);
980 }
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200981
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200982 /* acknowledge to be sent during boot operation */
Pali Rohár332c2302023-03-22 21:06:53 +0100983 ret = mmc_set_part_conf(mmc, ack, part_num, access);
984 if (ret != 0)
985 return CMD_RET_FAILURE;
986
987 return CMD_RET_SUCCESS;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200988}
Simon Glassed38aef2020-05-10 11:40:03 -0600989
990static int do_mmc_rst_func(struct cmd_tbl *cmdtp, int flag,
991 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200992{
Pali Rohár332c2302023-03-22 21:06:53 +0100993 int ret, dev;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200994 struct mmc *mmc;
995 u8 enable;
Nikita Kiryanov020f2612012-12-03 02:19:46 +0000996
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200997 /*
998 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
999 * The only valid values are 0x0, 0x1 and 0x2 and writing
1000 * a value of 0x1 or 0x2 sets the value permanently.
1001 */
1002 if (argc != 3)
1003 return CMD_RET_USAGE;
1004
Simon Glassff9b9032021-07-24 09:03:30 -06001005 dev = dectoul(argv[1], NULL);
1006 enable = dectoul(argv[2], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001007
Peng Fanb1e455a2015-11-25 17:16:21 +08001008 if (enable > 2) {
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001009 puts("Invalid RST_n_ENABLE value\n");
1010 return CMD_RET_USAGE;
1011 }
1012
Stephen Warrenb627f4b2014-05-23 13:24:45 -06001013 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001014 if (!mmc)
1015 return CMD_RET_FAILURE;
1016
1017 if (IS_SD(mmc)) {
1018 puts("RST_n_FUNCTION only exists on eMMC\n");
1019 return CMD_RET_FAILURE;
1020 }
1021
Pali Rohár332c2302023-03-22 21:06:53 +01001022 ret = mmc_set_rst_n_function(mmc, enable);
1023 if (ret != 0)
1024 return CMD_RET_FAILURE;
1025
1026 return CMD_RET_SUCCESS;
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001027}
1028#endif
Simon Glassed38aef2020-05-10 11:40:03 -06001029static int do_mmc_setdsr(struct cmd_tbl *cmdtp, int flag,
1030 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001031{
1032 struct mmc *mmc;
1033 u32 val;
1034 int ret;
Andy Flemingad347bb2008-10-30 16:41:01 -05001035
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001036 if (argc != 2)
1037 return CMD_RET_USAGE;
Simon Glass3ff49ec2021-07-24 09:03:29 -06001038 val = hextoul(argv[1], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001039
1040 mmc = find_mmc_device(curr_device);
1041 if (!mmc) {
1042 printf("no mmc device at slot %x\n", curr_device);
1043 return CMD_RET_FAILURE;
1044 }
1045 ret = mmc_set_dsr(mmc, val);
1046 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
1047 if (!ret) {
1048 mmc->has_init = 0;
1049 if (mmc_init(mmc))
1050 return CMD_RET_FAILURE;
1051 else
1052 return CMD_RET_SUCCESS;
Andy Flemingad347bb2008-10-30 16:41:01 -05001053 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001054 return ret;
1055}
Lei Wend430d7c2011-05-02 16:26:25 +00001056
Tomas Melinc17dae52016-11-25 11:01:03 +02001057#ifdef CONFIG_CMD_BKOPS_ENABLE
Marek Vasutefdeed62023-01-05 15:19:08 +01001058static int mmc_bkops_common(char *device, bool autobkops, bool enable)
Tomas Melinc17dae52016-11-25 11:01:03 +02001059{
Tomas Melinc17dae52016-11-25 11:01:03 +02001060 struct mmc *mmc;
Marek Vasutefdeed62023-01-05 15:19:08 +01001061 int dev;
Tomas Melinc17dae52016-11-25 11:01:03 +02001062
Marek Vasutefdeed62023-01-05 15:19:08 +01001063 dev = dectoul(device, NULL);
Tomas Melinc17dae52016-11-25 11:01:03 +02001064
1065 mmc = init_mmc_device(dev, false);
1066 if (!mmc)
1067 return CMD_RET_FAILURE;
1068
1069 if (IS_SD(mmc)) {
1070 puts("BKOPS_EN only exists on eMMC\n");
1071 return CMD_RET_FAILURE;
1072 }
1073
Marek Vasutefdeed62023-01-05 15:19:08 +01001074 return mmc_set_bkops_enable(mmc, autobkops, enable);
1075}
1076
1077static int do_mmc_bkops(struct cmd_tbl *cmdtp, int flag,
1078 int argc, char * const argv[])
1079{
1080 bool autobkops, enable;
1081
1082 if (argc != 4)
1083 return CMD_RET_USAGE;
1084
1085 if (!strcmp(argv[2], "manual"))
1086 autobkops = false;
1087 else if (!strcmp(argv[2], "auto"))
1088 autobkops = true;
1089 else
1090 return CMD_RET_FAILURE;
1091
1092 if (!strcmp(argv[3], "disable"))
1093 enable = false;
1094 else if (!strcmp(argv[3], "enable"))
1095 enable = true;
1096 else
1097 return CMD_RET_FAILURE;
1098
1099 return mmc_bkops_common(argv[1], autobkops, enable);
1100}
1101
1102static int do_mmc_bkops_enable(struct cmd_tbl *cmdtp, int flag,
1103 int argc, char * const argv[])
1104{
1105 if (argc != 2)
1106 return CMD_RET_USAGE;
1107
1108 return mmc_bkops_common(argv[1], false, true);
Tomas Melinc17dae52016-11-25 11:01:03 +02001109}
1110#endif
1111
Simon Glassed38aef2020-05-10 11:40:03 -06001112static int do_mmc_boot_wp(struct cmd_tbl *cmdtp, int flag,
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +02001113 int argc, char * const argv[])
1114{
1115 int err;
1116 struct mmc *mmc;
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001117 int part;
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +02001118
1119 mmc = init_mmc_device(curr_device, false);
1120 if (!mmc)
1121 return CMD_RET_FAILURE;
1122 if (IS_SD(mmc)) {
1123 printf("It is not an eMMC device\n");
1124 return CMD_RET_FAILURE;
1125 }
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001126
1127 if (argc == 2) {
1128 part = dectoul(argv[1], NULL);
1129 err = mmc_boot_wp_single_partition(mmc, part);
1130 } else {
1131 err = mmc_boot_wp(mmc);
1132 }
1133
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +02001134 if (err)
1135 return CMD_RET_FAILURE;
1136 printf("boot areas protected\n");
1137 return CMD_RET_SUCCESS;
1138}
1139
Marek Vasut1f692e32023-10-31 13:20:17 +01001140#if CONFIG_IS_ENABLED(CMD_MMC_REG)
1141static int do_mmc_reg(struct cmd_tbl *cmdtp, int flag,
1142 int argc, char *const argv[])
1143{
1144 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1145 struct mmc *mmc;
1146 int i, ret;
1147 u32 off;
1148
1149 if (argc < 3 || argc > 5)
1150 return CMD_RET_USAGE;
1151
1152 mmc = find_mmc_device(curr_device);
1153 if (!mmc) {
1154 printf("no mmc device at slot %x\n", curr_device);
1155 return CMD_RET_FAILURE;
1156 }
1157
1158 if (IS_SD(mmc)) {
1159 printf("SD registers are not supported\n");
1160 return CMD_RET_FAILURE;
1161 }
1162
1163 off = simple_strtoul(argv[3], NULL, 10);
1164 if (!strcmp(argv[2], "cid")) {
1165 if (off > 3)
1166 return CMD_RET_USAGE;
1167 printf("CID[%i]: 0x%08x\n", off, mmc->cid[off]);
1168 if (argv[4])
1169 env_set_hex(argv[4], mmc->cid[off]);
1170 return CMD_RET_SUCCESS;
1171 }
1172 if (!strcmp(argv[2], "csd")) {
1173 if (off > 3)
1174 return CMD_RET_USAGE;
1175 printf("CSD[%i]: 0x%08x\n", off, mmc->csd[off]);
1176 if (argv[4])
1177 env_set_hex(argv[4], mmc->csd[off]);
1178 return CMD_RET_SUCCESS;
1179 }
1180 if (!strcmp(argv[2], "dsr")) {
1181 printf("DSR: 0x%08x\n", mmc->dsr);
1182 if (argv[4])
1183 env_set_hex(argv[4], mmc->dsr);
1184 return CMD_RET_SUCCESS;
1185 }
1186 if (!strcmp(argv[2], "ocr")) {
1187 printf("OCR: 0x%08x\n", mmc->ocr);
1188 if (argv[4])
1189 env_set_hex(argv[4], mmc->ocr);
1190 return CMD_RET_SUCCESS;
1191 }
1192 if (!strcmp(argv[2], "rca")) {
1193 printf("RCA: 0x%08x\n", mmc->rca);
1194 if (argv[4])
1195 env_set_hex(argv[4], mmc->rca);
1196 return CMD_RET_SUCCESS;
1197 }
1198 if (!strcmp(argv[2], "extcsd") &&
1199 mmc->version >= MMC_VERSION_4_41) {
1200 ret = mmc_send_ext_csd(mmc, ext_csd);
1201 if (ret)
1202 return CMD_RET_FAILURE;
1203 if (!strcmp(argv[3], "all")) {
1204 /* Dump the entire register */
1205 printf("EXT_CSD:");
1206 for (i = 0; i < MMC_MAX_BLOCK_LEN; i++) {
1207 if (!(i % 10))
1208 printf("\n%03i: ", i);
1209 printf(" %02x", ext_csd[i]);
1210 }
1211 printf("\n");
1212 return CMD_RET_SUCCESS;
1213 }
1214 off = simple_strtoul(argv[3], NULL, 10);
1215 if (off > 512)
1216 return CMD_RET_USAGE;
1217 printf("EXT_CSD[%i]: 0x%02x\n", off, ext_csd[off]);
1218 if (argv[4])
1219 env_set_hex(argv[4], ext_csd[off]);
1220 return CMD_RET_SUCCESS;
1221 }
1222
1223 return CMD_RET_FAILURE;
1224}
1225#endif
1226
Simon Glassed38aef2020-05-10 11:40:03 -06001227static struct cmd_tbl cmd_mmc[] = {
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001228 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
1229 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001230 U_BOOT_CMD_MKENT(wp, 2, 0, do_mmc_boot_wp, "", ""),
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01001231#if CONFIG_IS_ENABLED(MMC_WRITE)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001232 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
1233 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01001234#endif
Alex Kiernanc568bcb2018-05-29 15:30:52 +00001235#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
1236 U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
1237#endif
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301238 U_BOOT_CMD_MKENT(rescan, 2, 1, do_mmc_rescan, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001239 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301240 U_BOOT_CMD_MKENT(dev, 4, 0, do_mmc_dev, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001241 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +01001242#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001243 U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +01001244#endif
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001245#ifdef CONFIG_SUPPORT_EMMC_BOOT
1246 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehd49c4aa2014-09-25 23:00:16 +08001247 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001248 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
1249 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
1250#endif
Alex Kiernan60e0f612018-05-08 04:43:31 +00001251#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001252 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
1253#endif
1254 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
Tomas Melinc17dae52016-11-25 11:01:03 +02001255#ifdef CONFIG_CMD_BKOPS_ENABLE
1256 U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
Marek Vasutefdeed62023-01-05 15:19:08 +01001257 U_BOOT_CMD_MKENT(bkops, 4, 0, do_mmc_bkops, "", ""),
Tomas Melinc17dae52016-11-25 11:01:03 +02001258#endif
Marek Vasut1f692e32023-10-31 13:20:17 +01001259#if CONFIG_IS_ENABLED(CMD_MMC_REG)
1260 U_BOOT_CMD_MKENT(reg, 5, 0, do_mmc_reg, "", ""),
1261#endif
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001262};
1263
Simon Glassed38aef2020-05-10 11:40:03 -06001264static int do_mmcops(struct cmd_tbl *cmdtp, int flag, int argc,
1265 char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001266{
Simon Glassed38aef2020-05-10 11:40:03 -06001267 struct cmd_tbl *cp;
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001268
1269 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
1270
1271 /* Drop the mmc command */
1272 argc--;
1273 argv++;
1274
1275 if (cp == NULL || argc > cp->maxargs)
1276 return CMD_RET_USAGE;
Boris Brezillonc71dfd12018-12-03 22:54:20 +01001277 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001278 return CMD_RET_SUCCESS;
1279
1280 if (curr_device < 0) {
1281 if (get_mmc_num() > 0) {
1282 curr_device = 0;
1283 } else {
1284 puts("No MMC device available\n");
1285 return CMD_RET_FAILURE;
1286 }
1287 }
1288 return cp->cmd(cmdtp, flag, argc, argv);
Andy Flemingad347bb2008-10-30 16:41:01 -05001289}
1290
1291U_BOOT_CMD(
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001292 mmc, 29, 1, do_mmcops,
Mike Frysinger27e1e622009-03-23 22:27:34 -04001293 "MMC sub system",
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001294 "info - display info of the current MMC device\n"
1295 "mmc read addr blk# cnt\n"
Lei Wend430d7c2011-05-02 16:26:25 +00001296 "mmc write addr blk# cnt\n"
Alex Kiernanc568bcb2018-05-29 15:30:52 +00001297#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar814a9752018-04-06 12:05:24 +05301298 "mmc swrite addr blk#\n"
1299#endif
Lei Wenea526762011-06-22 17:03:31 +00001300 "mmc erase blk# cnt\n"
Tomas Paukrtab00d0e2024-09-02 20:49:17 +02001301 "mmc erase partname\n"
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301302 "mmc rescan [mode]\n"
Lei Wend430d7c2011-05-02 16:26:25 +00001303 "mmc part - lists available partition on current mmc device\n"
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301304 "mmc dev [dev] [part] [mode] - show or set current mmc device [partition] and set mode\n"
1305 " - the required speed mode is passed as the index from the following list\n"
1306 " [MMC_LEGACY, MMC_HS, SD_HS, MMC_HS_52, MMC_DDR_52, UHS_SDR12, UHS_SDR25,\n"
1307 " UHS_SDR50, UHS_DDR50, UHS_SDR104, MMC_HS_200, MMC_HS_400, MMC_HS_400_ES]\n"
Amar3ccad532013-04-27 11:43:00 +05301308 "mmc list - lists available devices\n"
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001309 "mmc wp [PART] - power on write protect boot partitions\n"
1310 " arguments:\n"
1311 " PART - [0|1]\n"
1312 " : 0 - first boot partition, 1 - second boot partition\n"
1313 " if not assigned, write protect all boot partitions\n"
Alex Kiernan72d80462018-06-11 16:20:09 +00001314#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Jaehoon Chung94a47572021-02-26 16:07:18 +09001315 "mmc hwpartition <USER> <GP> <MODE> - does hardware partitioning\n"
Diego Santa Cruz420fc162014-12-23 10:50:30 +01001316 " arguments (sizes in 512-byte blocks):\n"
Jaehoon Chung94a47572021-02-26 16:07:18 +09001317 " USER - <user> <enh> <start> <cnt> <wrrel> <{on|off}>\n"
1318 " : sets user data area attributes\n"
1319 " GP - <{gp1|gp2|gp3|gp4}> <cnt> <enh> <wrrel> <{on|off}>\n"
1320 " : general purpose partition\n"
1321 " MODE - <{check|set|complete}>\n"
1322 " : mode, complete set partitioning completed\n"
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001323 " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
1324 " Power cycling is required to initialize partitions after set to complete.\n"
Alex Kiernan72d80462018-06-11 16:20:09 +00001325#endif
Amar3ccad532013-04-27 11:43:00 +05301326#ifdef CONFIG_SUPPORT_EMMC_BOOT
Jaehoon Chung8460b912020-12-16 07:24:50 +09001327 "mmc bootbus <dev> <boot_bus_width> <reset_boot_bus_width> <boot_mode>\n"
Tom Rini4cf854c2014-02-05 10:24:22 -05001328 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rini79919462014-02-05 10:24:20 -05001329 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
1330 " - Change sizes of boot and RPMB partitions of specified device\n"
Reuben Dowle55a81ad2021-05-14 10:15:43 +12001331 "mmc partconf <dev> [[varname] | [<boot_ack> <boot_partition> <partition_access>]]\n"
Angelo Dureghellof54f7532017-08-01 14:27:10 +02001332 " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
Reuben Dowle55a81ad2021-05-14 10:15:43 +12001333 " If showing the bits, optionally store the boot_partition field into varname\n"
Jaehoon Chung8460b912020-12-16 07:24:50 +09001334 "mmc rst-function <dev> <value>\n"
Tom Rini35a3ea12014-02-07 14:15:20 -05001335 " - Change the RST_n_FUNCTION field of the specified device\n"
1336 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behmedf8852a2009-02-18 19:59:39 +01001337#endif
Alex Kiernan60e0f612018-05-08 04:43:31 +00001338#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001339 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
1340 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
1341 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
1342 "mmc rpmb counter - read the value of the write counter\n"
1343#endif
1344 "mmc setdsr <value> - set DSR register value\n"
Tomas Melinc17dae52016-11-25 11:01:03 +02001345#ifdef CONFIG_CMD_BKOPS_ENABLE
1346 "mmc bkops-enable <dev> - enable background operations handshake on device\n"
1347 " WARNING: This is a write-once setting.\n"
Marek Vasutefdeed62023-01-05 15:19:08 +01001348 "mmc bkops <dev> [auto|manual] [enable|disable]\n"
1349 " - configure background operations handshake on device\n"
Tomas Melinc17dae52016-11-25 11:01:03 +02001350#endif
Marek Vasut1f692e32023-10-31 13:20:17 +01001351#if CONFIG_IS_ENABLED(CMD_MMC_REG)
1352 "mmc reg read <reg> <offset> [env] - read card register <reg> offset <offset>\n"
1353 " (optionally into [env] variable)\n"
1354 " - reg: cid/csd/dsr/ocr/rca/extcsd\n"
1355 " - offset: for cid/csd [0..3], for extcsd [0..511,all]\n"
1356#endif
Amar3ccad532013-04-27 11:43:00 +05301357 );
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001358
1359/* Old command kept for compatibility. Same as 'mmc info' */
1360U_BOOT_CMD(
1361 mmcinfo, 1, 0, do_mmcinfo,
1362 "display MMC info",
1363 "- display info of the current MMC device"
1364);