blob: fe7899ec793c4492d3d0f6f21762dd4b5b265529 [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>
Simon Glass75792952024-09-01 16:26:28 -060011#include <mapmem.h>
Marek Vasutdfff8ed2020-06-20 14:28:25 +020012#include <memalign.h>
wdenk7a428cc2003-06-15 22:40:42 +000013#include <mmc.h>
Simon Glass655306c2020-05-10 11:39:58 -060014#include <part.h>
Jassi Brar814a9752018-04-06 12:05:24 +053015#include <sparse_format.h>
16#include <image-sparse.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060017#include <vsprintf.h>
Tim Harvey728cbde2024-05-31 08:36:34 -070018#include <linux/ctype.h>
wdenk7a428cc2003-06-15 22:40:42 +000019
Mike Frysinger0289fe62009-06-14 21:35:21 -040020static int curr_device = -1;
Andy Flemingad347bb2008-10-30 16:41:01 -050021
22static void print_mmcinfo(struct mmc *mmc)
23{
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010024 int i;
25
Pantelis Antoniou2c850462014-03-11 19:34:20 +020026 printf("Device: %s\n", mmc->cfg->name);
Andy Flemingad347bb2008-10-30 16:41:01 -050027 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
Max Merchel1414ada2022-02-10 10:16:39 +010028 if (IS_SD(mmc)) {
29 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
30 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
31 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
32 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
33 } else {
34 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xff);
35 printf("Name: %c%c%c%c%c%c \n", mmc->cid[0] & 0xff,
36 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
37 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
38 (mmc->cid[2] >> 24));
39 }
Andy Flemingad347bb2008-10-30 16:41:01 -050040
Jean-Jacques Hiblotf62caf92017-09-21 16:29:56 +020041 printf("Bus Speed: %d\n", mmc->clock);
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +010042#if CONFIG_IS_ENABLED(MMC_VERBOSE)
Marek Vasutd1e63462019-03-18 04:49:21 +010043 printf("Mode: %s\n", mmc_mode_name(mmc->selected_mode));
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +010044 mmc_dump_capabilities("card capabilities", mmc->card_caps);
45 mmc_dump_capabilities("host capabilities", mmc->host_caps);
46#endif
Andy Flemingad347bb2008-10-30 16:41:01 -050047 printf("Rd Block Len: %d\n", mmc->read_bl_len);
48
Pantelis Antonioua095bfd2015-01-23 12:12:01 +020049 printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
50 EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
51 EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
52 if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
53 printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
54 printf("\n");
Andy Flemingad347bb2008-10-30 16:41:01 -050055
56 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang316f81a2011-01-04 01:04:19 +000057 puts("Capacity: ");
58 print_size(mmc->capacity, "\n");
Andy Flemingad347bb2008-10-30 16:41:01 -050059
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -060060 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
61 mmc->ddr_mode ? " DDR" : "");
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010062
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +010063#if CONFIG_IS_ENABLED(MMC_WRITE)
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010064 puts("Erase Group Size: ");
65 print_size(((u64)mmc->erase_grp_size) << 9, "\n");
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +010066#endif
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010067
Diego Santa Cruz1f69e222014-12-23 10:50:19 +010068 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010069 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
Diego Santa Cruz2b45f582014-12-23 10:50:23 +010070 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
Marek Vasutdfff8ed2020-06-20 14:28:25 +020071 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
72 u8 wp;
Heinrich Schuchardt0c8ffff2020-03-30 07:24:18 +020073 int ret;
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010074
Jean-Jacques Hiblotba54ab82018-01-04 15:23:36 +010075#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010076 puts("HC WP Group Size: ");
77 print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
Jean-Jacques Hiblotba54ab82018-01-04 15:23:36 +010078#endif
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010079
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010080 puts("User Capacity: ");
Diego Santa Cruz37a50b92014-12-23 10:50:33 +010081 print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
82 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
83 puts(" WRREL\n");
84 else
85 putc('\n');
Diego Santa Cruz2b45f582014-12-23 10:50:23 +010086 if (usr_enh) {
87 puts("User Enhanced Start: ");
88 print_size(mmc->enh_user_start, "\n");
89 puts("User Enhanced Size: ");
90 print_size(mmc->enh_user_size, "\n");
91 }
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010092 puts("Boot Capacity: ");
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010093 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010094 puts("RPMB Capacity: ");
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010095 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010096
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010097 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010098 bool is_enh = has_enh &&
99 (mmc->part_attr & EXT_CSD_ENH_GP(i));
Diego Santa Cruz99ca0282014-12-23 10:50:16 +0100100 if (mmc->capacity_gp[i]) {
Diego Santa Cruzd8358432014-12-23 10:50:18 +0100101 printf("GP%i Capacity: ", i+1);
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +0100102 print_size(mmc->capacity_gp[i],
Diego Santa Cruz37a50b92014-12-23 10:50:33 +0100103 is_enh ? " ENH" : "");
104 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
105 puts(" WRREL\n");
106 else
107 putc('\n');
Diego Santa Cruz99ca0282014-12-23 10:50:16 +0100108 }
109 }
Heinrich Schuchardt0c8ffff2020-03-30 07:24:18 +0200110 ret = mmc_send_ext_csd(mmc, ext_csd);
111 if (ret)
112 return;
113 wp = ext_csd[EXT_CSD_BOOT_WP_STATUS];
114 for (i = 0; i < 2; ++i) {
115 printf("Boot area %d is ", i);
116 switch (wp & 3) {
117 case 0:
118 printf("not write protected\n");
119 break;
120 case 1:
121 printf("power on protected\n");
122 break;
123 case 2:
124 printf("permanently protected\n");
125 break;
126 default:
127 printf("in reserved protection state\n");
128 break;
129 }
130 wp >>= 2;
131 }
Diego Santa Cruz99ca0282014-12-23 10:50:16 +0100132 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500133}
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530134
135static struct mmc *__init_mmc_device(int dev, bool force_init,
136 enum bus_mode speed_mode)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200137{
138 struct mmc *mmc;
139 mmc = find_mmc_device(dev);
140 if (!mmc) {
141 printf("no mmc device at slot %x\n", dev);
142 return NULL;
143 }
Eric Nelsonbc181662016-03-27 12:00:14 -0700144
Marek Vasutcb5df5d2019-01-03 22:09:43 +0100145 if (!mmc_getcd(mmc))
146 force_init = true;
147
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600148 if (force_init)
149 mmc->has_init = 0;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530150
151 if (IS_ENABLED(CONFIG_MMC_SPEED_MODE_SET))
152 mmc->user_speed_mode = speed_mode;
153
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200154 if (mmc_init(mmc))
155 return NULL;
Marek Vasut829540f2019-01-03 22:09:44 +0100156
157#ifdef CONFIG_BLOCK_CACHE
158 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Simon Glassfada3f92022-09-17 09:00:09 -0600159 blkcache_invalidate(bd->uclass_id, bd->devnum);
Marek Vasut829540f2019-01-03 22:09:44 +0100160#endif
161
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200162 return mmc;
163}
Simon Glassed38aef2020-05-10 11:40:03 -0600164
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530165static struct mmc *init_mmc_device(int dev, bool force_init)
166{
167 return __init_mmc_device(dev, force_init, MMC_MODES_END);
168}
169
Simon Glassed38aef2020-05-10 11:40:03 -0600170static int do_mmcinfo(struct cmd_tbl *cmdtp, int flag, int argc,
171 char *const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500172{
173 struct mmc *mmc;
Andy Flemingad347bb2008-10-30 16:41:01 -0500174
Lei Wend430d7c2011-05-02 16:26:25 +0000175 if (curr_device < 0) {
176 if (get_mmc_num() > 0)
177 curr_device = 0;
178 else {
179 puts("No MMC device available\n");
Pali Rohár332c2302023-03-22 21:06:53 +0100180 return CMD_RET_FAILURE;
Lei Wend430d7c2011-05-02 16:26:25 +0000181 }
182 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500183
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600184 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200185 if (!mmc)
186 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500187
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200188 print_mmcinfo(mmc);
189 return CMD_RET_SUCCESS;
190}
Andy Flemingad347bb2008-10-30 16:41:01 -0500191
Alex Kiernan60e0f612018-05-08 04:43:31 +0000192#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200193static int confirm_key_prog(void)
194{
195 puts("Warning: Programming authentication key can be done only once !\n"
196 " Use this command only if you are sure of what you are doing,\n"
197 "Really perform the key programming? <y/N> ");
198 if (confirm_yesno())
Lei Wend430d7c2011-05-02 16:26:25 +0000199 return 1;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200200
201 puts("Authentication key programming aborted\n");
202 return 0;
203}
Simon Glassed38aef2020-05-10 11:40:03 -0600204
205static int do_mmcrpmb_key(struct cmd_tbl *cmdtp, int flag,
206 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200207{
208 void *key_addr;
209 struct mmc *mmc = find_mmc_device(curr_device);
210
211 if (argc != 2)
212 return CMD_RET_USAGE;
213
Simon Glass3ff49ec2021-07-24 09:03:29 -0600214 key_addr = (void *)hextoul(argv[1], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200215 if (!confirm_key_prog())
216 return CMD_RET_FAILURE;
217 if (mmc_rpmb_set_key(mmc, key_addr)) {
218 printf("ERROR - Key already programmed ?\n");
219 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500220 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200221 return CMD_RET_SUCCESS;
Andy Flemingad347bb2008-10-30 16:41:01 -0500222}
Simon Glassed38aef2020-05-10 11:40:03 -0600223
224static int do_mmcrpmb_read(struct cmd_tbl *cmdtp, int flag,
225 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200226{
227 u16 blk, cnt;
228 void *addr;
229 int n;
230 void *key_addr = NULL;
231 struct mmc *mmc = find_mmc_device(curr_device);
Andy Flemingad347bb2008-10-30 16:41:01 -0500232
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200233 if (argc < 4)
234 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500235
Simon Glass3ff49ec2021-07-24 09:03:29 -0600236 addr = (void *)hextoul(argv[1], NULL);
237 blk = hextoul(argv[2], NULL);
238 cnt = hextoul(argv[3], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200239
240 if (argc == 5)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600241 key_addr = (void *)hextoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200242
Simon Glass9703a932024-08-22 07:57:51 -0600243 printf("MMC RPMB read: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200244 curr_device, blk, cnt);
245 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
246
247 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
248 if (n != cnt)
249 return CMD_RET_FAILURE;
250 return CMD_RET_SUCCESS;
251}
Simon Glassed38aef2020-05-10 11:40:03 -0600252
253static int do_mmcrpmb_write(struct cmd_tbl *cmdtp, int flag,
254 int argc, char *const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500255{
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200256 u16 blk, cnt;
257 void *addr;
258 int n;
259 void *key_addr;
260 struct mmc *mmc = find_mmc_device(curr_device);
Lei Wen64b2b1e2011-06-22 17:03:30 +0000261
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200262 if (argc != 5)
Simon Glassa06dfc72011-12-10 08:44:01 +0000263 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500264
Simon Glass3ff49ec2021-07-24 09:03:29 -0600265 addr = (void *)hextoul(argv[1], NULL);
266 blk = hextoul(argv[2], NULL);
267 cnt = hextoul(argv[3], NULL);
268 key_addr = (void *)hextoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200269
Simon Glass9703a932024-08-22 07:57:51 -0600270 printf("MMC RPMB write: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200271 curr_device, blk, cnt);
272 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
273
274 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
275 if (n != cnt)
276 return CMD_RET_FAILURE;
277 return CMD_RET_SUCCESS;
278}
Simon Glassed38aef2020-05-10 11:40:03 -0600279
280static int do_mmcrpmb_counter(struct cmd_tbl *cmdtp, int flag,
281 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200282{
283 unsigned long counter;
284 struct mmc *mmc = find_mmc_device(curr_device);
285
286 if (mmc_rpmb_get_counter(mmc, &counter))
287 return CMD_RET_FAILURE;
288 printf("RPMB Write counter= %lx\n", counter);
289 return CMD_RET_SUCCESS;
290}
291
Simon Glassed38aef2020-05-10 11:40:03 -0600292static struct cmd_tbl cmd_rpmb[] = {
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200293 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
294 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
295 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
296 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
297};
298
Simon Glassed38aef2020-05-10 11:40:03 -0600299static int do_mmcrpmb(struct cmd_tbl *cmdtp, int flag,
300 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200301{
Simon Glassed38aef2020-05-10 11:40:03 -0600302 struct cmd_tbl *cp;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200303 struct mmc *mmc;
304 char original_part;
305 int ret;
306
307 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
308
309 /* Drop the rpmb subcommand */
310 argc--;
311 argv++;
312
313 if (cp == NULL || argc > cp->maxargs)
314 return CMD_RET_USAGE;
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100315 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200316 return CMD_RET_SUCCESS;
317
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600318 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200319 if (!mmc)
320 return CMD_RET_FAILURE;
321
322 if (!(mmc->version & MMC_VERSION_MMC)) {
Heinrich Schuchardteeb191c2020-03-29 19:26:57 +0000323 printf("It is not an eMMC device\n");
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200324 return CMD_RET_FAILURE;
Lei Wend430d7c2011-05-02 16:26:25 +0000325 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200326 if (mmc->version < MMC_VERSION_4_41) {
327 printf("RPMB not supported before version 4.41\n");
328 return CMD_RET_FAILURE;
329 }
330 /* Switch to the RPMB partition */
Kever Yangcb5f1932017-06-08 09:20:03 +0800331#ifndef CONFIG_BLK
Marek Vasuta62cf492016-05-04 16:35:25 +0200332 original_part = mmc->block_dev.hwpart;
Kever Yangcb5f1932017-06-08 09:20:03 +0800333#else
334 original_part = mmc_get_blk_desc(mmc)->hwpart;
335#endif
Simon Glassdbfa32c2022-08-11 19:34:59 -0600336 if (blk_select_hwpart_devnum(UCLASS_MMC, curr_device, MMC_PART_RPMB) !=
Simon Glass11f2bb62016-05-01 13:52:29 -0600337 0)
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700338 return CMD_RET_FAILURE;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200339 ret = cp->cmd(cmdtp, flag, argc, argv);
Andy Flemingad347bb2008-10-30 16:41:01 -0500340
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200341 /* Return to original partition */
Simon Glassdbfa32c2022-08-11 19:34:59 -0600342 if (blk_select_hwpart_devnum(UCLASS_MMC, curr_device, original_part) !=
Simon Glass11f2bb62016-05-01 13:52:29 -0600343 0)
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700344 return CMD_RET_FAILURE;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200345 return ret;
346}
347#endif
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530348
Simon Glassed38aef2020-05-10 11:40:03 -0600349static int do_mmc_read(struct cmd_tbl *cmdtp, int flag,
350 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200351{
352 struct mmc *mmc;
353 u32 blk, cnt, n;
Simon Glass75792952024-09-01 16:26:28 -0600354 void *ptr;
Stephen Warren18faca72013-04-01 11:50:28 +0000355
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200356 if (argc != 4)
357 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500358
Simon Glass75792952024-09-01 16:26:28 -0600359 ptr = map_sysmem(hextoul(argv[1], NULL), 0);
Simon Glass3ff49ec2021-07-24 09:03:29 -0600360 blk = hextoul(argv[2], NULL);
361 cnt = hextoul(argv[3], NULL);
Lei Wen61dba932010-09-13 22:07:28 +0800362
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600363 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200364 if (!mmc)
365 return CMD_RET_FAILURE;
Lei Wen61dba932010-09-13 22:07:28 +0800366
Simon Glass9703a932024-08-22 07:57:51 -0600367 printf("MMC read: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200368 curr_device, blk, cnt);
Stephen Warren18faca72013-04-01 11:50:28 +0000369
Simon Glass75792952024-09-01 16:26:28 -0600370 n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, ptr);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200371 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Simon Glass75792952024-09-01 16:26:28 -0600372 unmap_sysmem(ptr);
Lei Wend430d7c2011-05-02 16:26:25 +0000373
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200374 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
375}
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +0100376
Alex Kiernanc568bcb2018-05-29 15:30:52 +0000377#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar814a9752018-04-06 12:05:24 +0530378static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk,
379 lbaint_t blkcnt, const void *buffer)
380{
381 struct blk_desc *dev_desc = info->priv;
382
383 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
384}
385
386static lbaint_t mmc_sparse_reserve(struct sparse_storage *info,
387 lbaint_t blk, lbaint_t blkcnt)
388{
389 return blkcnt;
390}
391
Simon Glassed38aef2020-05-10 11:40:03 -0600392static int do_mmc_sparse_write(struct cmd_tbl *cmdtp, int flag,
393 int argc, char *const argv[])
Jassi Brar814a9752018-04-06 12:05:24 +0530394{
395 struct sparse_storage sparse;
396 struct blk_desc *dev_desc;
397 struct mmc *mmc;
398 char dest[11];
399 void *addr;
400 u32 blk;
401
402 if (argc != 3)
403 return CMD_RET_USAGE;
404
Simon Glass3ff49ec2021-07-24 09:03:29 -0600405 addr = (void *)hextoul(argv[1], NULL);
406 blk = hextoul(argv[2], NULL);
Jassi Brar814a9752018-04-06 12:05:24 +0530407
408 if (!is_sparse_image(addr)) {
409 printf("Not a sparse image\n");
410 return CMD_RET_FAILURE;
411 }
412
413 mmc = init_mmc_device(curr_device, false);
414 if (!mmc)
415 return CMD_RET_FAILURE;
416
Simon Glass9703a932024-08-22 07:57:51 -0600417 printf("MMC Sparse write: dev # %d, block # %d ... ",
Jassi Brar814a9752018-04-06 12:05:24 +0530418 curr_device, blk);
419
420 if (mmc_getwp(mmc) == 1) {
421 printf("Error: card is write protected!\n");
422 return CMD_RET_FAILURE;
423 }
424
425 dev_desc = mmc_get_blk_desc(mmc);
426 sparse.priv = dev_desc;
427 sparse.blksz = 512;
428 sparse.start = blk;
429 sparse.size = dev_desc->lba - blk;
430 sparse.write = mmc_sparse_write;
431 sparse.reserve = mmc_sparse_reserve;
432 sparse.mssg = NULL;
433 sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz);
434
Alex Kiernan27b69de2018-05-29 15:30:40 +0000435 if (write_sparse_image(&sparse, dest, addr, NULL))
Jassi Brar814a9752018-04-06 12:05:24 +0530436 return CMD_RET_FAILURE;
437 else
438 return CMD_RET_SUCCESS;
439}
440#endif
441
Alex Kiernanc568bcb2018-05-29 15:30:52 +0000442#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glassed38aef2020-05-10 11:40:03 -0600443static int do_mmc_write(struct cmd_tbl *cmdtp, int flag,
444 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200445{
446 struct mmc *mmc;
447 u32 blk, cnt, n;
Simon Glass75792952024-09-01 16:26:28 -0600448 void *ptr;
Andy Flemingad347bb2008-10-30 16:41:01 -0500449
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200450 if (argc != 4)
451 return CMD_RET_USAGE;
Lei Wend430d7c2011-05-02 16:26:25 +0000452
Simon Glass75792952024-09-01 16:26:28 -0600453 ptr = map_sysmem(hextoul(argv[1], NULL), 0);
Simon Glass3ff49ec2021-07-24 09:03:29 -0600454 blk = hextoul(argv[2], NULL);
455 cnt = hextoul(argv[3], NULL);
Lei Wend430d7c2011-05-02 16:26:25 +0000456
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600457 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200458 if (!mmc)
459 return CMD_RET_FAILURE;
Lei Wen31b99802011-05-02 16:26:26 +0000460
Simon Glass9703a932024-08-22 07:57:51 -0600461 printf("MMC write: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200462 curr_device, blk, cnt);
Lei Wen31b99802011-05-02 16:26:26 +0000463
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200464 if (mmc_getwp(mmc) == 1) {
465 printf("Error: card is write protected!\n");
466 return CMD_RET_FAILURE;
467 }
Simon Glass75792952024-09-01 16:26:28 -0600468 n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, ptr);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200469 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Simon Glass75792952024-09-01 16:26:28 -0600470 unmap_sysmem(ptr);
Lei Wend430d7c2011-05-02 16:26:25 +0000471
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200472 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
473}
Simon Glassed38aef2020-05-10 11:40:03 -0600474
475static int do_mmc_erase(struct cmd_tbl *cmdtp, int flag,
476 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200477{
478 struct mmc *mmc;
Tomas Paukrtab00d0e2024-09-02 20:49:17 +0200479 struct disk_partition info;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200480 u32 blk, cnt, n;
Tom Rinif8c6f792014-02-05 10:24:21 -0500481
Tomas Paukrtab00d0e2024-09-02 20:49:17 +0200482 if (argc < 2 || argc > 3)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200483 return CMD_RET_USAGE;
Tom Rinif8c6f792014-02-05 10:24:21 -0500484
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600485 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200486 if (!mmc)
487 return CMD_RET_FAILURE;
Tomas Paukrtab00d0e2024-09-02 20:49:17 +0200488
489 if (argc == 3) {
490 blk = hextoul(argv[1], NULL);
491 cnt = hextoul(argv[2], NULL);
492 } else if (part_get_info_by_name(mmc_get_blk_desc(mmc), argv[1], &info) >= 0) {
493 blk = info.start;
494 cnt = info.size;
495 } else {
496 return CMD_RET_FAILURE;
497 }
Tom Rinif8c6f792014-02-05 10:24:21 -0500498
Simon Glass9703a932024-08-22 07:57:51 -0600499 printf("MMC erase: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200500 curr_device, blk, cnt);
Tom Rini4cf854c2014-02-05 10:24:22 -0500501
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200502 if (mmc_getwp(mmc) == 1) {
503 printf("Error: card is write protected!\n");
504 return CMD_RET_FAILURE;
505 }
Simon Glasse5db1152016-05-01 13:52:35 -0600506 n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200507 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Tom Rini4cf854c2014-02-05 10:24:22 -0500508
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200509 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
510}
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +0100511#endif
512
Simon Glassed38aef2020-05-10 11:40:03 -0600513static int do_mmc_rescan(struct cmd_tbl *cmdtp, int flag,
514 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200515{
516 struct mmc *mmc;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530517
518 if (argc == 1) {
519 mmc = init_mmc_device(curr_device, true);
520 } else if (argc == 2) {
Heinrich Schuchardt05c74282022-04-25 23:11:06 +0200521 enum bus_mode speed_mode;
522
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530523 speed_mode = (int)dectoul(argv[1], NULL);
524 mmc = __init_mmc_device(curr_device, true, speed_mode);
525 } else {
526 return CMD_RET_USAGE;
527 }
Tom Rini4cf854c2014-02-05 10:24:22 -0500528
Stephen Warren07ec4e72014-05-23 13:24:46 -0600529 if (!mmc)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200530 return CMD_RET_FAILURE;
Tom Rini4cf854c2014-02-05 10:24:22 -0500531
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200532 return CMD_RET_SUCCESS;
533}
Simon Glassed38aef2020-05-10 11:40:03 -0600534
535static int do_mmc_part(struct cmd_tbl *cmdtp, int flag,
536 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200537{
Simon Glasse3394752016-02-29 15:25:34 -0700538 struct blk_desc *mmc_dev;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200539 struct mmc *mmc;
Amar3ccad532013-04-27 11:43:00 +0530540
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600541 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200542 if (!mmc)
543 return CMD_RET_FAILURE;
Amar3ccad532013-04-27 11:43:00 +0530544
Simon Glassfada3f92022-09-17 09:00:09 -0600545 mmc_dev = blk_get_devnum_by_uclass_id(UCLASS_MMC, curr_device);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200546 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
Simon Glassb89a8442016-02-29 15:25:48 -0700547 part_print(mmc_dev);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200548 return CMD_RET_SUCCESS;
549 }
Amar3ccad532013-04-27 11:43:00 +0530550
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200551 puts("get mmc type error!\n");
552 return CMD_RET_FAILURE;
553}
Simon Glassed38aef2020-05-10 11:40:03 -0600554
555static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag,
556 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200557{
Stephen Warren18549802014-05-23 12:48:10 -0600558 int dev, part = 0, ret;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200559 struct mmc *mmc;
Tom Rini35a3ea12014-02-07 14:15:20 -0500560
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200561 if (argc == 1) {
562 dev = curr_device;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530563 mmc = init_mmc_device(dev, true);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200564 } else if (argc == 2) {
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530565 dev = (int)dectoul(argv[1], NULL);
566 mmc = init_mmc_device(dev, true);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200567 } else if (argc == 3) {
Simon Glassff9b9032021-07-24 09:03:30 -0600568 dev = (int)dectoul(argv[1], NULL);
569 part = (int)dectoul(argv[2], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200570 if (part > PART_ACCESS_MASK) {
571 printf("#part_num shouldn't be larger than %d\n",
572 PART_ACCESS_MASK);
573 return CMD_RET_FAILURE;
Tom Rini35a3ea12014-02-07 14:15:20 -0500574 }
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530575 mmc = init_mmc_device(dev, true);
576 } else if (argc == 4) {
Heinrich Schuchardt05c74282022-04-25 23:11:06 +0200577 enum bus_mode speed_mode;
578
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530579 dev = (int)dectoul(argv[1], NULL);
580 part = (int)dectoul(argv[2], NULL);
581 if (part > PART_ACCESS_MASK) {
582 printf("#part_num shouldn't be larger than %d\n",
583 PART_ACCESS_MASK);
584 return CMD_RET_FAILURE;
585 }
586 speed_mode = (int)dectoul(argv[3], NULL);
587 mmc = __init_mmc_device(dev, true, speed_mode);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200588 } else {
589 return CMD_RET_USAGE;
590 }
Tom Rini35a3ea12014-02-07 14:15:20 -0500591
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200592 if (!mmc)
593 return CMD_RET_FAILURE;
Tom Rini35a3ea12014-02-07 14:15:20 -0500594
Simon Glassdbfa32c2022-08-11 19:34:59 -0600595 ret = blk_select_hwpart_devnum(UCLASS_MMC, dev, part);
Stephen Warren18549802014-05-23 12:48:10 -0600596 printf("switch to partitions #%d, %s\n",
597 part, (!ret) ? "OK" : "ERROR");
598 if (ret)
599 return 1;
600
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200601 curr_device = dev;
602 if (mmc->part_config == MMCPART_NOAVAILABLE)
603 printf("mmc%d is current device\n", curr_device);
604 else
605 printf("mmc%d(part %d) is current device\n",
Simon Glasse5db1152016-05-01 13:52:35 -0600606 curr_device, mmc_get_blk_desc(mmc)->hwpart);
Tom Rini35a3ea12014-02-07 14:15:20 -0500607
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200608 return CMD_RET_SUCCESS;
609}
Simon Glassed38aef2020-05-10 11:40:03 -0600610
611static int do_mmc_list(struct cmd_tbl *cmdtp, int flag,
612 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200613{
614 print_mmc_devices('\n');
615 return CMD_RET_SUCCESS;
616}
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100617
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +0100618#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Marek Vasut94e39952021-09-15 11:43:13 +0200619static void parse_hwpart_user_enh_size(struct mmc *mmc,
620 struct mmc_hwpart_conf *pconf,
621 char *argv)
622{
Marek Vasut30d29662022-01-17 22:54:29 +0100623 int i, ret;
Marek Vasut94e39952021-09-15 11:43:13 +0200624
625 pconf->user.enh_size = 0;
626
627 if (!strcmp(argv, "-")) { /* The rest of eMMC */
628 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
629 ret = mmc_send_ext_csd(mmc, ext_csd);
630 if (ret)
631 return;
Marek Vasut30d29662022-01-17 22:54:29 +0100632 /* The enh_size value is in 512B block units */
Marek Vasut94e39952021-09-15 11:43:13 +0200633 pconf->user.enh_size =
634 ((ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 2] << 16) +
635 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 1] << 8) +
636 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT]) * 1024 *
637 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
638 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
639 pconf->user.enh_size -= pconf->user.enh_start;
Marek Vasut30d29662022-01-17 22:54:29 +0100640 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
641 /*
642 * If the eMMC already has GP partitions set,
643 * subtract their size from the maximum USER
644 * partition size.
645 *
646 * Else, if the command was used to configure new
647 * GP partitions, subtract their size from maximum
648 * USER partition size.
649 */
650 if (mmc->capacity_gp[i]) {
651 /* The capacity_gp is in 1B units */
652 pconf->user.enh_size -= mmc->capacity_gp[i] >> 9;
653 } else if (pconf->gp_part[i].size) {
654 /* The gp_part[].size is in 512B units */
655 pconf->user.enh_size -= pconf->gp_part[i].size;
656 }
657 }
Marek Vasut94e39952021-09-15 11:43:13 +0200658 } else {
659 pconf->user.enh_size = dectoul(argv, NULL);
660 }
661}
662
663static int parse_hwpart_user(struct mmc *mmc, struct mmc_hwpart_conf *pconf,
Simon Glassed38aef2020-05-10 11:40:03 -0600664 int argc, char *const argv[])
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100665{
666 int i = 0;
667
668 memset(&pconf->user, 0, sizeof(pconf->user));
669
670 while (i < argc) {
671 if (!strcmp(argv[i], "enh")) {
672 if (i + 2 >= argc)
673 return -1;
674 pconf->user.enh_start =
Simon Glassff9b9032021-07-24 09:03:30 -0600675 dectoul(argv[i + 1], NULL);
Marek Vasut94e39952021-09-15 11:43:13 +0200676 parse_hwpart_user_enh_size(mmc, pconf, argv[i + 2]);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100677 i += 3;
678 } else if (!strcmp(argv[i], "wrrel")) {
679 if (i + 1 >= argc)
680 return -1;
681 pconf->user.wr_rel_change = 1;
682 if (!strcmp(argv[i+1], "on"))
683 pconf->user.wr_rel_set = 1;
684 else if (!strcmp(argv[i+1], "off"))
685 pconf->user.wr_rel_set = 0;
686 else
687 return -1;
688 i += 2;
689 } else {
690 break;
691 }
692 }
693 return i;
694}
695
696static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
Simon Glassed38aef2020-05-10 11:40:03 -0600697 int argc, char *const argv[])
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100698{
699 int i;
700
701 memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
702
703 if (1 >= argc)
704 return -1;
Simon Glassff9b9032021-07-24 09:03:30 -0600705 pconf->gp_part[pidx].size = dectoul(argv[0], NULL);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100706
707 i = 1;
708 while (i < argc) {
709 if (!strcmp(argv[i], "enh")) {
710 pconf->gp_part[pidx].enhanced = 1;
711 i += 1;
712 } else if (!strcmp(argv[i], "wrrel")) {
713 if (i + 1 >= argc)
714 return -1;
715 pconf->gp_part[pidx].wr_rel_change = 1;
716 if (!strcmp(argv[i+1], "on"))
717 pconf->gp_part[pidx].wr_rel_set = 1;
718 else if (!strcmp(argv[i+1], "off"))
719 pconf->gp_part[pidx].wr_rel_set = 0;
720 else
721 return -1;
722 i += 2;
723 } else {
724 break;
725 }
726 }
727 return i;
728}
729
Simon Glassed38aef2020-05-10 11:40:03 -0600730static int do_mmc_hwpartition(struct cmd_tbl *cmdtp, int flag,
731 int argc, char *const argv[])
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100732{
733 struct mmc *mmc;
734 struct mmc_hwpart_conf pconf = { };
735 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100736 int i, r, pidx;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100737
738 mmc = init_mmc_device(curr_device, false);
739 if (!mmc)
740 return CMD_RET_FAILURE;
741
Jaehoon Chungffbba622021-09-24 09:23:34 +0900742 if (IS_SD(mmc)) {
743 puts("SD doesn't support partitioning\n");
744 return CMD_RET_FAILURE;
745 }
746
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100747 if (argc < 1)
748 return CMD_RET_USAGE;
749 i = 1;
750 while (i < argc) {
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100751 if (!strcmp(argv[i], "user")) {
752 i++;
Marek Vasut94e39952021-09-15 11:43:13 +0200753 r = parse_hwpart_user(mmc, &pconf, argc - i, &argv[i]);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100754 if (r < 0)
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100755 return CMD_RET_USAGE;
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100756 i += r;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100757 } else if (!strncmp(argv[i], "gp", 2) &&
758 strlen(argv[i]) == 3 &&
759 argv[i][2] >= '1' && argv[i][2] <= '4') {
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100760 pidx = argv[i][2] - '1';
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100761 i++;
762 r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
763 if (r < 0)
764 return CMD_RET_USAGE;
765 i += r;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100766 } else if (!strcmp(argv[i], "check")) {
767 mode = MMC_HWPART_CONF_CHECK;
768 i++;
769 } else if (!strcmp(argv[i], "set")) {
770 mode = MMC_HWPART_CONF_SET;
771 i++;
772 } else if (!strcmp(argv[i], "complete")) {
773 mode = MMC_HWPART_CONF_COMPLETE;
774 i++;
775 } else {
776 return CMD_RET_USAGE;
777 }
778 }
779
780 puts("Partition configuration:\n");
781 if (pconf.user.enh_size) {
782 puts("\tUser Enhanced Start: ");
783 print_size(((u64)pconf.user.enh_start) << 9, "\n");
784 puts("\tUser Enhanced Size: ");
785 print_size(((u64)pconf.user.enh_size) << 9, "\n");
786 } else {
787 puts("\tNo enhanced user data area\n");
788 }
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100789 if (pconf.user.wr_rel_change)
790 printf("\tUser partition write reliability: %s\n",
791 pconf.user.wr_rel_set ? "on" : "off");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100792 for (pidx = 0; pidx < 4; pidx++) {
793 if (pconf.gp_part[pidx].size) {
794 printf("\tGP%i Capacity: ", pidx+1);
795 print_size(((u64)pconf.gp_part[pidx].size) << 9,
796 pconf.gp_part[pidx].enhanced ?
797 " ENH\n" : "\n");
798 } else {
799 printf("\tNo GP%i partition\n", pidx+1);
800 }
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100801 if (pconf.gp_part[pidx].wr_rel_change)
802 printf("\tGP%i write reliability: %s\n", pidx+1,
803 pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100804 }
805
806 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
807 if (mode == MMC_HWPART_CONF_COMPLETE)
808 puts("Partitioning successful, "
809 "power-cycle to make effective\n");
810 return CMD_RET_SUCCESS;
811 } else {
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100812 puts("Failed!\n");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100813 return CMD_RET_FAILURE;
814 }
815}
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +0100816#endif
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100817
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200818#ifdef CONFIG_SUPPORT_EMMC_BOOT
Simon Glassed38aef2020-05-10 11:40:03 -0600819static int do_mmc_bootbus(struct cmd_tbl *cmdtp, int flag,
820 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200821{
822 int dev;
823 struct mmc *mmc;
824 u8 width, reset, mode;
825
826 if (argc != 5)
827 return CMD_RET_USAGE;
Simon Glassff9b9032021-07-24 09:03:30 -0600828 dev = dectoul(argv[1], NULL);
829 width = dectoul(argv[2], NULL);
830 reset = dectoul(argv[3], NULL);
831 mode = dectoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200832
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600833 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200834 if (!mmc)
835 return CMD_RET_FAILURE;
836
837 if (IS_SD(mmc)) {
838 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
839 return CMD_RET_FAILURE;
Amar3ccad532013-04-27 11:43:00 +0530840 }
Markus Niebel03951412013-12-16 13:40:46 +0100841
Jaehoon Chung7483dd32021-02-26 18:38:20 +0900842 /*
843 * BOOT_BUS_CONDITIONS[177]
844 * BOOT_MODE[4:3]
845 * 0x0 : Use SDR + Backward compatible timing in boot operation
846 * 0x1 : Use SDR + High Speed Timing in boot operation mode
847 * 0x2 : Use DDR in boot operation
848 * RESET_BOOT_BUS_CONDITIONS
849 * 0x0 : Reset bus width to x1, SDR, Backward compatible
850 * 0x1 : Retain BOOT_BUS_WIDTH and BOOT_MODE
851 * BOOT_BUS_WIDTH
852 * 0x0 : x1(sdr) or x4 (ddr) buswidth
853 * 0x1 : x4(sdr/ddr) buswith
854 * 0x2 : x8(sdr/ddr) buswith
855 *
856 */
857 if (width >= 0x3) {
858 printf("boot_bus_width %d is invalid\n", width);
859 return CMD_RET_FAILURE;
860 }
861
862 if (reset >= 0x2) {
863 printf("reset_boot_bus_width %d is invalid\n", reset);
864 return CMD_RET_FAILURE;
865 }
866
867 if (mode >= 0x3) {
868 printf("reset_boot_bus_width %d is invalid\n", mode);
869 return CMD_RET_FAILURE;
870 }
871
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200872 /* acknowledge to be sent during boot operation */
Jaehoon Chung7483dd32021-02-26 18:38:20 +0900873 if (mmc_set_boot_bus_width(mmc, width, reset, mode)) {
874 puts("BOOT_BUS_WIDTH is failed to change.\n");
875 return CMD_RET_FAILURE;
876 }
877
878 printf("Set to BOOT_BUS_WIDTH = 0x%x, RESET = 0x%x, BOOT_MODE = 0x%x\n",
879 width, reset, mode);
880 return CMD_RET_SUCCESS;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200881}
Simon Glassed38aef2020-05-10 11:40:03 -0600882
883static int do_mmc_boot_resize(struct cmd_tbl *cmdtp, int flag,
884 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200885{
886 int dev;
887 struct mmc *mmc;
888 u32 bootsize, rpmbsize;
Markus Niebel03951412013-12-16 13:40:46 +0100889
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200890 if (argc != 4)
891 return CMD_RET_USAGE;
Simon Glassff9b9032021-07-24 09:03:30 -0600892 dev = dectoul(argv[1], NULL);
893 bootsize = dectoul(argv[2], NULL);
894 rpmbsize = dectoul(argv[3], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200895
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600896 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200897 if (!mmc)
898 return CMD_RET_FAILURE;
899
900 if (IS_SD(mmc)) {
Heinrich Schuchardteeb191c2020-03-29 19:26:57 +0000901 printf("It is not an eMMC device\n");
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200902 return CMD_RET_FAILURE;
Markus Niebel03951412013-12-16 13:40:46 +0100903 }
904
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200905 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
906 printf("EMMC boot partition Size change Failed.\n");
907 return CMD_RET_FAILURE;
908 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500909
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200910 printf("EMMC boot partition Size %d MB\n", bootsize);
911 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
912 return CMD_RET_SUCCESS;
913}
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200914
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200915static int mmc_partconf_print(struct mmc *mmc, const char *varname)
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200916{
917 u8 ack, access, part;
918
919 if (mmc->part_config == MMCPART_NOAVAILABLE) {
920 printf("No part_config info for ver. 0x%x\n", mmc->version);
921 return CMD_RET_FAILURE;
922 }
923
924 access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
925 ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
926 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
927
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200928 if(varname)
929 env_set_hex(varname, part);
930
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200931 printf("EXT_CSD[179], PARTITION_CONFIG:\n"
932 "BOOT_ACK: 0x%x\n"
Tim Harvey728cbde2024-05-31 08:36:34 -0700933 "BOOT_PARTITION_ENABLE: 0x%x (%s)\n"
934 "PARTITION_ACCESS: 0x%x (%s)\n", ack, part, emmc_boot_part_names[part],
935 access, emmc_hwpart_names[access]);
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200936
937 return CMD_RET_SUCCESS;
938}
939
Simon Glassed38aef2020-05-10 11:40:03 -0600940static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag,
941 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200942{
Pali Rohár332c2302023-03-22 21:06:53 +0100943 int ret, dev;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200944 struct mmc *mmc;
945 u8 ack, part_num, access;
Lei Wenea526762011-06-22 17:03:31 +0000946
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200947 if (argc != 2 && argc != 3 && argc != 5)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200948 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500949
Simon Glassff9b9032021-07-24 09:03:30 -0600950 dev = dectoul(argv[1], NULL);
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530951
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600952 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200953 if (!mmc)
954 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500955
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200956 if (IS_SD(mmc)) {
957 puts("PARTITION_CONFIG only exists on eMMC\n");
958 return CMD_RET_FAILURE;
959 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500960
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200961 if (argc == 2 || argc == 3)
Simon Glass793a98e2023-11-18 14:05:20 -0700962 return mmc_partconf_print(mmc, cmd_arg2(argc, argv));
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200963
Tim Harvey728cbde2024-05-31 08:36:34 -0700964 /* BOOT_ACK */
Simon Glassff9b9032021-07-24 09:03:30 -0600965 ack = dectoul(argv[2], NULL);
Tim Harvey728cbde2024-05-31 08:36:34 -0700966 /* BOOT_PARTITION_ENABLE */
967 if (!isdigit(*argv[3])) {
968 for (part_num = ARRAY_SIZE(emmc_boot_part_names) - 1; part_num > 0; part_num--) {
969 if (!strcmp(argv[3], emmc_boot_part_names[part_num]))
970 break;
971 }
972 } else {
973 part_num = dectoul(argv[3], NULL);
974 }
975 /* PARTITION_ACCESS */
976 if (!isdigit(*argv[4])) {
977 for (access = ARRAY_SIZE(emmc_hwpart_names) - 1; access > 0; access--) {
978 if (!strcmp(argv[4], emmc_hwpart_names[access]))
979 break;
980 }
981 } else {
982 access = dectoul(argv[4], NULL);
983 }
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200984
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200985 /* acknowledge to be sent during boot operation */
Pali Rohár332c2302023-03-22 21:06:53 +0100986 ret = mmc_set_part_conf(mmc, ack, part_num, access);
987 if (ret != 0)
988 return CMD_RET_FAILURE;
989
990 return CMD_RET_SUCCESS;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200991}
Simon Glassed38aef2020-05-10 11:40:03 -0600992
993static int do_mmc_rst_func(struct cmd_tbl *cmdtp, int flag,
994 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200995{
Pali Rohár332c2302023-03-22 21:06:53 +0100996 int ret, dev;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200997 struct mmc *mmc;
998 u8 enable;
Nikita Kiryanov020f2612012-12-03 02:19:46 +0000999
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001000 /*
1001 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
1002 * The only valid values are 0x0, 0x1 and 0x2 and writing
1003 * a value of 0x1 or 0x2 sets the value permanently.
1004 */
1005 if (argc != 3)
1006 return CMD_RET_USAGE;
1007
Simon Glassff9b9032021-07-24 09:03:30 -06001008 dev = dectoul(argv[1], NULL);
1009 enable = dectoul(argv[2], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001010
Peng Fanb1e455a2015-11-25 17:16:21 +08001011 if (enable > 2) {
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001012 puts("Invalid RST_n_ENABLE value\n");
1013 return CMD_RET_USAGE;
1014 }
1015
Stephen Warrenb627f4b2014-05-23 13:24:45 -06001016 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001017 if (!mmc)
1018 return CMD_RET_FAILURE;
1019
1020 if (IS_SD(mmc)) {
1021 puts("RST_n_FUNCTION only exists on eMMC\n");
1022 return CMD_RET_FAILURE;
1023 }
1024
Pali Rohár332c2302023-03-22 21:06:53 +01001025 ret = mmc_set_rst_n_function(mmc, enable);
1026 if (ret != 0)
1027 return CMD_RET_FAILURE;
1028
1029 return CMD_RET_SUCCESS;
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001030}
1031#endif
Simon Glassed38aef2020-05-10 11:40:03 -06001032static int do_mmc_setdsr(struct cmd_tbl *cmdtp, int flag,
1033 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001034{
1035 struct mmc *mmc;
1036 u32 val;
1037 int ret;
Andy Flemingad347bb2008-10-30 16:41:01 -05001038
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001039 if (argc != 2)
1040 return CMD_RET_USAGE;
Simon Glass3ff49ec2021-07-24 09:03:29 -06001041 val = hextoul(argv[1], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001042
1043 mmc = find_mmc_device(curr_device);
1044 if (!mmc) {
1045 printf("no mmc device at slot %x\n", curr_device);
1046 return CMD_RET_FAILURE;
1047 }
1048 ret = mmc_set_dsr(mmc, val);
1049 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
1050 if (!ret) {
1051 mmc->has_init = 0;
1052 if (mmc_init(mmc))
1053 return CMD_RET_FAILURE;
1054 else
1055 return CMD_RET_SUCCESS;
Andy Flemingad347bb2008-10-30 16:41:01 -05001056 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001057 return ret;
1058}
Lei Wend430d7c2011-05-02 16:26:25 +00001059
Tomas Melinc17dae52016-11-25 11:01:03 +02001060#ifdef CONFIG_CMD_BKOPS_ENABLE
Marek Vasutefdeed62023-01-05 15:19:08 +01001061static int mmc_bkops_common(char *device, bool autobkops, bool enable)
Tomas Melinc17dae52016-11-25 11:01:03 +02001062{
Tomas Melinc17dae52016-11-25 11:01:03 +02001063 struct mmc *mmc;
Marek Vasutefdeed62023-01-05 15:19:08 +01001064 int dev;
Tomas Melinc17dae52016-11-25 11:01:03 +02001065
Marek Vasutefdeed62023-01-05 15:19:08 +01001066 dev = dectoul(device, NULL);
Tomas Melinc17dae52016-11-25 11:01:03 +02001067
1068 mmc = init_mmc_device(dev, false);
1069 if (!mmc)
1070 return CMD_RET_FAILURE;
1071
1072 if (IS_SD(mmc)) {
1073 puts("BKOPS_EN only exists on eMMC\n");
1074 return CMD_RET_FAILURE;
1075 }
1076
Marek Vasutefdeed62023-01-05 15:19:08 +01001077 return mmc_set_bkops_enable(mmc, autobkops, enable);
1078}
1079
1080static int do_mmc_bkops(struct cmd_tbl *cmdtp, int flag,
1081 int argc, char * const argv[])
1082{
1083 bool autobkops, enable;
1084
1085 if (argc != 4)
1086 return CMD_RET_USAGE;
1087
1088 if (!strcmp(argv[2], "manual"))
1089 autobkops = false;
1090 else if (!strcmp(argv[2], "auto"))
1091 autobkops = true;
1092 else
1093 return CMD_RET_FAILURE;
1094
1095 if (!strcmp(argv[3], "disable"))
1096 enable = false;
1097 else if (!strcmp(argv[3], "enable"))
1098 enable = true;
1099 else
1100 return CMD_RET_FAILURE;
1101
1102 return mmc_bkops_common(argv[1], autobkops, enable);
1103}
1104
1105static int do_mmc_bkops_enable(struct cmd_tbl *cmdtp, int flag,
1106 int argc, char * const argv[])
1107{
1108 if (argc != 2)
1109 return CMD_RET_USAGE;
1110
1111 return mmc_bkops_common(argv[1], false, true);
Tomas Melinc17dae52016-11-25 11:01:03 +02001112}
1113#endif
1114
Simon Glassed38aef2020-05-10 11:40:03 -06001115static int do_mmc_boot_wp(struct cmd_tbl *cmdtp, int flag,
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +02001116 int argc, char * const argv[])
1117{
1118 int err;
1119 struct mmc *mmc;
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001120 int part;
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +02001121
1122 mmc = init_mmc_device(curr_device, false);
1123 if (!mmc)
1124 return CMD_RET_FAILURE;
1125 if (IS_SD(mmc)) {
1126 printf("It is not an eMMC device\n");
1127 return CMD_RET_FAILURE;
1128 }
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001129
1130 if (argc == 2) {
1131 part = dectoul(argv[1], NULL);
1132 err = mmc_boot_wp_single_partition(mmc, part);
1133 } else {
1134 err = mmc_boot_wp(mmc);
1135 }
1136
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +02001137 if (err)
1138 return CMD_RET_FAILURE;
1139 printf("boot areas protected\n");
1140 return CMD_RET_SUCCESS;
1141}
1142
Marek Vasut1f692e32023-10-31 13:20:17 +01001143#if CONFIG_IS_ENABLED(CMD_MMC_REG)
1144static int do_mmc_reg(struct cmd_tbl *cmdtp, int flag,
1145 int argc, char *const argv[])
1146{
1147 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1148 struct mmc *mmc;
1149 int i, ret;
1150 u32 off;
1151
1152 if (argc < 3 || argc > 5)
1153 return CMD_RET_USAGE;
1154
1155 mmc = find_mmc_device(curr_device);
1156 if (!mmc) {
1157 printf("no mmc device at slot %x\n", curr_device);
1158 return CMD_RET_FAILURE;
1159 }
1160
1161 if (IS_SD(mmc)) {
1162 printf("SD registers are not supported\n");
1163 return CMD_RET_FAILURE;
1164 }
1165
1166 off = simple_strtoul(argv[3], NULL, 10);
1167 if (!strcmp(argv[2], "cid")) {
1168 if (off > 3)
1169 return CMD_RET_USAGE;
1170 printf("CID[%i]: 0x%08x\n", off, mmc->cid[off]);
1171 if (argv[4])
1172 env_set_hex(argv[4], mmc->cid[off]);
1173 return CMD_RET_SUCCESS;
1174 }
1175 if (!strcmp(argv[2], "csd")) {
1176 if (off > 3)
1177 return CMD_RET_USAGE;
1178 printf("CSD[%i]: 0x%08x\n", off, mmc->csd[off]);
1179 if (argv[4])
1180 env_set_hex(argv[4], mmc->csd[off]);
1181 return CMD_RET_SUCCESS;
1182 }
1183 if (!strcmp(argv[2], "dsr")) {
1184 printf("DSR: 0x%08x\n", mmc->dsr);
1185 if (argv[4])
1186 env_set_hex(argv[4], mmc->dsr);
1187 return CMD_RET_SUCCESS;
1188 }
1189 if (!strcmp(argv[2], "ocr")) {
1190 printf("OCR: 0x%08x\n", mmc->ocr);
1191 if (argv[4])
1192 env_set_hex(argv[4], mmc->ocr);
1193 return CMD_RET_SUCCESS;
1194 }
1195 if (!strcmp(argv[2], "rca")) {
1196 printf("RCA: 0x%08x\n", mmc->rca);
1197 if (argv[4])
1198 env_set_hex(argv[4], mmc->rca);
1199 return CMD_RET_SUCCESS;
1200 }
1201 if (!strcmp(argv[2], "extcsd") &&
1202 mmc->version >= MMC_VERSION_4_41) {
1203 ret = mmc_send_ext_csd(mmc, ext_csd);
1204 if (ret)
1205 return CMD_RET_FAILURE;
1206 if (!strcmp(argv[3], "all")) {
1207 /* Dump the entire register */
1208 printf("EXT_CSD:");
1209 for (i = 0; i < MMC_MAX_BLOCK_LEN; i++) {
1210 if (!(i % 10))
1211 printf("\n%03i: ", i);
1212 printf(" %02x", ext_csd[i]);
1213 }
1214 printf("\n");
1215 return CMD_RET_SUCCESS;
1216 }
1217 off = simple_strtoul(argv[3], NULL, 10);
1218 if (off > 512)
1219 return CMD_RET_USAGE;
1220 printf("EXT_CSD[%i]: 0x%02x\n", off, ext_csd[off]);
1221 if (argv[4])
1222 env_set_hex(argv[4], ext_csd[off]);
1223 return CMD_RET_SUCCESS;
1224 }
1225
1226 return CMD_RET_FAILURE;
1227}
1228#endif
1229
Simon Glassed38aef2020-05-10 11:40:03 -06001230static struct cmd_tbl cmd_mmc[] = {
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001231 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
1232 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001233 U_BOOT_CMD_MKENT(wp, 2, 0, do_mmc_boot_wp, "", ""),
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01001234#if CONFIG_IS_ENABLED(MMC_WRITE)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001235 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
1236 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01001237#endif
Alex Kiernanc568bcb2018-05-29 15:30:52 +00001238#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
1239 U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
1240#endif
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301241 U_BOOT_CMD_MKENT(rescan, 2, 1, do_mmc_rescan, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001242 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301243 U_BOOT_CMD_MKENT(dev, 4, 0, do_mmc_dev, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001244 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +01001245#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001246 U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +01001247#endif
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001248#ifdef CONFIG_SUPPORT_EMMC_BOOT
1249 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehd49c4aa2014-09-25 23:00:16 +08001250 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001251 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
1252 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
1253#endif
Alex Kiernan60e0f612018-05-08 04:43:31 +00001254#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001255 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
1256#endif
1257 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
Tomas Melinc17dae52016-11-25 11:01:03 +02001258#ifdef CONFIG_CMD_BKOPS_ENABLE
1259 U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
Marek Vasutefdeed62023-01-05 15:19:08 +01001260 U_BOOT_CMD_MKENT(bkops, 4, 0, do_mmc_bkops, "", ""),
Tomas Melinc17dae52016-11-25 11:01:03 +02001261#endif
Marek Vasut1f692e32023-10-31 13:20:17 +01001262#if CONFIG_IS_ENABLED(CMD_MMC_REG)
1263 U_BOOT_CMD_MKENT(reg, 5, 0, do_mmc_reg, "", ""),
1264#endif
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001265};
1266
Simon Glassed38aef2020-05-10 11:40:03 -06001267static int do_mmcops(struct cmd_tbl *cmdtp, int flag, int argc,
1268 char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001269{
Simon Glassed38aef2020-05-10 11:40:03 -06001270 struct cmd_tbl *cp;
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001271
1272 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
1273
1274 /* Drop the mmc command */
1275 argc--;
1276 argv++;
1277
1278 if (cp == NULL || argc > cp->maxargs)
1279 return CMD_RET_USAGE;
Boris Brezillonc71dfd12018-12-03 22:54:20 +01001280 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001281 return CMD_RET_SUCCESS;
1282
1283 if (curr_device < 0) {
1284 if (get_mmc_num() > 0) {
1285 curr_device = 0;
1286 } else {
1287 puts("No MMC device available\n");
1288 return CMD_RET_FAILURE;
1289 }
1290 }
1291 return cp->cmd(cmdtp, flag, argc, argv);
Andy Flemingad347bb2008-10-30 16:41:01 -05001292}
1293
1294U_BOOT_CMD(
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001295 mmc, 29, 1, do_mmcops,
Mike Frysinger27e1e622009-03-23 22:27:34 -04001296 "MMC sub system",
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001297 "info - display info of the current MMC device\n"
1298 "mmc read addr blk# cnt\n"
Lei Wend430d7c2011-05-02 16:26:25 +00001299 "mmc write addr blk# cnt\n"
Alex Kiernanc568bcb2018-05-29 15:30:52 +00001300#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar814a9752018-04-06 12:05:24 +05301301 "mmc swrite addr blk#\n"
1302#endif
Lei Wenea526762011-06-22 17:03:31 +00001303 "mmc erase blk# cnt\n"
Tomas Paukrtab00d0e2024-09-02 20:49:17 +02001304 "mmc erase partname\n"
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301305 "mmc rescan [mode]\n"
Lei Wend430d7c2011-05-02 16:26:25 +00001306 "mmc part - lists available partition on current mmc device\n"
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301307 "mmc dev [dev] [part] [mode] - show or set current mmc device [partition] and set mode\n"
1308 " - the required speed mode is passed as the index from the following list\n"
1309 " [MMC_LEGACY, MMC_HS, SD_HS, MMC_HS_52, MMC_DDR_52, UHS_SDR12, UHS_SDR25,\n"
1310 " UHS_SDR50, UHS_DDR50, UHS_SDR104, MMC_HS_200, MMC_HS_400, MMC_HS_400_ES]\n"
Amar3ccad532013-04-27 11:43:00 +05301311 "mmc list - lists available devices\n"
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001312 "mmc wp [PART] - power on write protect boot partitions\n"
1313 " arguments:\n"
1314 " PART - [0|1]\n"
1315 " : 0 - first boot partition, 1 - second boot partition\n"
1316 " if not assigned, write protect all boot partitions\n"
Alex Kiernan72d80462018-06-11 16:20:09 +00001317#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Jaehoon Chung94a47572021-02-26 16:07:18 +09001318 "mmc hwpartition <USER> <GP> <MODE> - does hardware partitioning\n"
Diego Santa Cruz420fc162014-12-23 10:50:30 +01001319 " arguments (sizes in 512-byte blocks):\n"
Jaehoon Chung94a47572021-02-26 16:07:18 +09001320 " USER - <user> <enh> <start> <cnt> <wrrel> <{on|off}>\n"
1321 " : sets user data area attributes\n"
1322 " GP - <{gp1|gp2|gp3|gp4}> <cnt> <enh> <wrrel> <{on|off}>\n"
1323 " : general purpose partition\n"
1324 " MODE - <{check|set|complete}>\n"
1325 " : mode, complete set partitioning completed\n"
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001326 " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
1327 " Power cycling is required to initialize partitions after set to complete.\n"
Alex Kiernan72d80462018-06-11 16:20:09 +00001328#endif
Amar3ccad532013-04-27 11:43:00 +05301329#ifdef CONFIG_SUPPORT_EMMC_BOOT
Jaehoon Chung8460b912020-12-16 07:24:50 +09001330 "mmc bootbus <dev> <boot_bus_width> <reset_boot_bus_width> <boot_mode>\n"
Tom Rini4cf854c2014-02-05 10:24:22 -05001331 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rini79919462014-02-05 10:24:20 -05001332 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
1333 " - Change sizes of boot and RPMB partitions of specified device\n"
Reuben Dowle55a81ad2021-05-14 10:15:43 +12001334 "mmc partconf <dev> [[varname] | [<boot_ack> <boot_partition> <partition_access>]]\n"
Angelo Dureghellof54f7532017-08-01 14:27:10 +02001335 " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
Reuben Dowle55a81ad2021-05-14 10:15:43 +12001336 " If showing the bits, optionally store the boot_partition field into varname\n"
Jaehoon Chung8460b912020-12-16 07:24:50 +09001337 "mmc rst-function <dev> <value>\n"
Tom Rini35a3ea12014-02-07 14:15:20 -05001338 " - Change the RST_n_FUNCTION field of the specified device\n"
1339 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behmedf8852a2009-02-18 19:59:39 +01001340#endif
Alex Kiernan60e0f612018-05-08 04:43:31 +00001341#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001342 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
1343 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
1344 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
1345 "mmc rpmb counter - read the value of the write counter\n"
1346#endif
1347 "mmc setdsr <value> - set DSR register value\n"
Tomas Melinc17dae52016-11-25 11:01:03 +02001348#ifdef CONFIG_CMD_BKOPS_ENABLE
1349 "mmc bkops-enable <dev> - enable background operations handshake on device\n"
1350 " WARNING: This is a write-once setting.\n"
Marek Vasutefdeed62023-01-05 15:19:08 +01001351 "mmc bkops <dev> [auto|manual] [enable|disable]\n"
1352 " - configure background operations handshake on device\n"
Tomas Melinc17dae52016-11-25 11:01:03 +02001353#endif
Marek Vasut1f692e32023-10-31 13:20:17 +01001354#if CONFIG_IS_ENABLED(CMD_MMC_REG)
1355 "mmc reg read <reg> <offset> [env] - read card register <reg> offset <offset>\n"
1356 " (optionally into [env] variable)\n"
1357 " - reg: cid/csd/dsr/ocr/rca/extcsd\n"
1358 " - offset: for cid/csd [0..3], for extcsd [0..511,all]\n"
1359#endif
Amar3ccad532013-04-27 11:43:00 +05301360 );
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001361
1362/* Old command kept for compatibility. Same as 'mmc info' */
1363U_BOOT_CMD(
1364 mmcinfo, 1, 0, do_mmcinfo,
1365 "display MMC info",
1366 "- display info of the current MMC device"
1367);