blob: 5340a58be8ee4ff6b83bba60faee4c67c3a72639 [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>
Tom Rini22856382025-05-14 16:46:03 -060011#include <env.h>
Simon Glass75792952024-09-01 16:26:28 -060012#include <mapmem.h>
Marek Vasutdfff8ed2020-06-20 14:28:25 +020013#include <memalign.h>
wdenk7a428cc2003-06-15 22:40:42 +000014#include <mmc.h>
Simon Glass655306c2020-05-10 11:39:58 -060015#include <part.h>
Jassi Brar814a9752018-04-06 12:05:24 +053016#include <sparse_format.h>
17#include <image-sparse.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060018#include <vsprintf.h>
Tim Harvey728cbde2024-05-31 08:36:34 -070019#include <linux/ctype.h>
wdenk7a428cc2003-06-15 22:40:42 +000020
Mike Frysinger0289fe62009-06-14 21:35:21 -040021static int curr_device = -1;
Andy Flemingad347bb2008-10-30 16:41:01 -050022
23static void print_mmcinfo(struct mmc *mmc)
24{
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010025 int i;
26
Pantelis Antoniou2c850462014-03-11 19:34:20 +020027 printf("Device: %s\n", mmc->cfg->name);
Andy Flemingad347bb2008-10-30 16:41:01 -050028 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
Max Merchel1414ada2022-02-10 10:16:39 +010029 if (IS_SD(mmc)) {
30 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
31 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
32 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
33 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
34 } else {
35 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xff);
36 printf("Name: %c%c%c%c%c%c \n", mmc->cid[0] & 0xff,
37 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
38 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
39 (mmc->cid[2] >> 24));
40 }
Andy Flemingad347bb2008-10-30 16:41:01 -050041
Jean-Jacques Hiblotf62caf92017-09-21 16:29:56 +020042 printf("Bus Speed: %d\n", mmc->clock);
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +010043#if CONFIG_IS_ENABLED(MMC_VERBOSE)
Marek Vasutd1e63462019-03-18 04:49:21 +010044 printf("Mode: %s\n", mmc_mode_name(mmc->selected_mode));
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +010045 mmc_dump_capabilities("card capabilities", mmc->card_caps);
46 mmc_dump_capabilities("host capabilities", mmc->host_caps);
47#endif
Andy Flemingad347bb2008-10-30 16:41:01 -050048 printf("Rd Block Len: %d\n", mmc->read_bl_len);
49
Pantelis Antonioua095bfd2015-01-23 12:12:01 +020050 printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
51 EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
52 EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
53 if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
54 printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
55 printf("\n");
Andy Flemingad347bb2008-10-30 16:41:01 -050056
57 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang316f81a2011-01-04 01:04:19 +000058 puts("Capacity: ");
59 print_size(mmc->capacity, "\n");
Andy Flemingad347bb2008-10-30 16:41:01 -050060
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -060061 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
62 mmc->ddr_mode ? " DDR" : "");
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010063
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +010064#if CONFIG_IS_ENABLED(MMC_WRITE)
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010065 puts("Erase Group Size: ");
66 print_size(((u64)mmc->erase_grp_size) << 9, "\n");
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +010067#endif
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010068
Diego Santa Cruz1f69e222014-12-23 10:50:19 +010069 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010070 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
Diego Santa Cruz2b45f582014-12-23 10:50:23 +010071 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
Marek Vasutdfff8ed2020-06-20 14:28:25 +020072 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
73 u8 wp;
Heinrich Schuchardt0c8ffff2020-03-30 07:24:18 +020074 int ret;
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010075
Jean-Jacques Hiblotba54ab82018-01-04 15:23:36 +010076#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010077 puts("HC WP Group Size: ");
78 print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
Jean-Jacques Hiblotba54ab82018-01-04 15:23:36 +010079#endif
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010080
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010081 puts("User Capacity: ");
Diego Santa Cruz37a50b92014-12-23 10:50:33 +010082 print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
83 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
84 puts(" WRREL\n");
85 else
86 putc('\n');
Diego Santa Cruz2b45f582014-12-23 10:50:23 +010087 if (usr_enh) {
88 puts("User Enhanced Start: ");
89 print_size(mmc->enh_user_start, "\n");
90 puts("User Enhanced Size: ");
91 print_size(mmc->enh_user_size, "\n");
92 }
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010093 puts("Boot Capacity: ");
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010094 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010095 puts("RPMB Capacity: ");
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010096 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010097
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010098 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010099 bool is_enh = has_enh &&
100 (mmc->part_attr & EXT_CSD_ENH_GP(i));
Diego Santa Cruz99ca0282014-12-23 10:50:16 +0100101 if (mmc->capacity_gp[i]) {
Diego Santa Cruzd8358432014-12-23 10:50:18 +0100102 printf("GP%i Capacity: ", i+1);
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +0100103 print_size(mmc->capacity_gp[i],
Diego Santa Cruz37a50b92014-12-23 10:50:33 +0100104 is_enh ? " ENH" : "");
105 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
106 puts(" WRREL\n");
107 else
108 putc('\n');
Diego Santa Cruz99ca0282014-12-23 10:50:16 +0100109 }
110 }
Heinrich Schuchardt0c8ffff2020-03-30 07:24:18 +0200111 ret = mmc_send_ext_csd(mmc, ext_csd);
112 if (ret)
113 return;
114 wp = ext_csd[EXT_CSD_BOOT_WP_STATUS];
115 for (i = 0; i < 2; ++i) {
116 printf("Boot area %d is ", i);
117 switch (wp & 3) {
118 case 0:
119 printf("not write protected\n");
120 break;
121 case 1:
122 printf("power on protected\n");
123 break;
124 case 2:
125 printf("permanently protected\n");
126 break;
127 default:
128 printf("in reserved protection state\n");
129 break;
130 }
131 wp >>= 2;
132 }
Diego Santa Cruz99ca0282014-12-23 10:50:16 +0100133 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500134}
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530135
136static struct mmc *__init_mmc_device(int dev, bool force_init,
137 enum bus_mode speed_mode)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200138{
139 struct mmc *mmc;
140 mmc = find_mmc_device(dev);
141 if (!mmc) {
142 printf("no mmc device at slot %x\n", dev);
143 return NULL;
144 }
Eric Nelsonbc181662016-03-27 12:00:14 -0700145
Marek Vasutcb5df5d2019-01-03 22:09:43 +0100146 if (!mmc_getcd(mmc))
147 force_init = true;
148
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600149 if (force_init)
150 mmc->has_init = 0;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530151
152 if (IS_ENABLED(CONFIG_MMC_SPEED_MODE_SET))
153 mmc->user_speed_mode = speed_mode;
154
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200155 if (mmc_init(mmc))
156 return NULL;
Marek Vasut829540f2019-01-03 22:09:44 +0100157
158#ifdef CONFIG_BLOCK_CACHE
159 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Simon Glassfada3f92022-09-17 09:00:09 -0600160 blkcache_invalidate(bd->uclass_id, bd->devnum);
Marek Vasut829540f2019-01-03 22:09:44 +0100161#endif
162
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200163 return mmc;
164}
Simon Glassed38aef2020-05-10 11:40:03 -0600165
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530166static struct mmc *init_mmc_device(int dev, bool force_init)
167{
168 return __init_mmc_device(dev, force_init, MMC_MODES_END);
169}
170
Simon Glassed38aef2020-05-10 11:40:03 -0600171static int do_mmcinfo(struct cmd_tbl *cmdtp, int flag, int argc,
172 char *const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500173{
174 struct mmc *mmc;
Andy Flemingad347bb2008-10-30 16:41:01 -0500175
Lei Wend430d7c2011-05-02 16:26:25 +0000176 if (curr_device < 0) {
177 if (get_mmc_num() > 0)
178 curr_device = 0;
179 else {
180 puts("No MMC device available\n");
Pali Rohár332c2302023-03-22 21:06:53 +0100181 return CMD_RET_FAILURE;
Lei Wend430d7c2011-05-02 16:26:25 +0000182 }
183 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500184
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600185 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200186 if (!mmc)
187 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500188
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200189 print_mmcinfo(mmc);
190 return CMD_RET_SUCCESS;
191}
Andy Flemingad347bb2008-10-30 16:41:01 -0500192
Alex Kiernan60e0f612018-05-08 04:43:31 +0000193#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200194static int confirm_key_prog(void)
195{
196 puts("Warning: Programming authentication key can be done only once !\n"
197 " Use this command only if you are sure of what you are doing,\n"
198 "Really perform the key programming? <y/N> ");
199 if (confirm_yesno())
Lei Wend430d7c2011-05-02 16:26:25 +0000200 return 1;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200201
202 puts("Authentication key programming aborted\n");
203 return 0;
204}
Simon Glassed38aef2020-05-10 11:40:03 -0600205
206static int do_mmcrpmb_key(struct cmd_tbl *cmdtp, int flag,
207 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200208{
209 void *key_addr;
210 struct mmc *mmc = find_mmc_device(curr_device);
211
212 if (argc != 2)
213 return CMD_RET_USAGE;
214
Simon Glass3ff49ec2021-07-24 09:03:29 -0600215 key_addr = (void *)hextoul(argv[1], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200216 if (!confirm_key_prog())
217 return CMD_RET_FAILURE;
218 if (mmc_rpmb_set_key(mmc, key_addr)) {
219 printf("ERROR - Key already programmed ?\n");
220 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500221 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200222 return CMD_RET_SUCCESS;
Andy Flemingad347bb2008-10-30 16:41:01 -0500223}
Simon Glassed38aef2020-05-10 11:40:03 -0600224
225static int do_mmcrpmb_read(struct cmd_tbl *cmdtp, int flag,
226 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200227{
228 u16 blk, cnt;
229 void *addr;
230 int n;
231 void *key_addr = NULL;
232 struct mmc *mmc = find_mmc_device(curr_device);
Andy Flemingad347bb2008-10-30 16:41:01 -0500233
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200234 if (argc < 4)
235 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500236
Simon Glass3ff49ec2021-07-24 09:03:29 -0600237 addr = (void *)hextoul(argv[1], NULL);
238 blk = hextoul(argv[2], NULL);
239 cnt = hextoul(argv[3], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200240
241 if (argc == 5)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600242 key_addr = (void *)hextoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200243
Simon Glass9703a932024-08-22 07:57:51 -0600244 printf("MMC RPMB read: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200245 curr_device, blk, cnt);
246 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
247
248 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
249 if (n != cnt)
250 return CMD_RET_FAILURE;
251 return CMD_RET_SUCCESS;
252}
Simon Glassed38aef2020-05-10 11:40:03 -0600253
254static int do_mmcrpmb_write(struct cmd_tbl *cmdtp, int flag,
255 int argc, char *const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500256{
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200257 u16 blk, cnt;
258 void *addr;
259 int n;
260 void *key_addr;
261 struct mmc *mmc = find_mmc_device(curr_device);
Lei Wen64b2b1e2011-06-22 17:03:30 +0000262
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200263 if (argc != 5)
Simon Glassa06dfc72011-12-10 08:44:01 +0000264 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500265
Simon Glass3ff49ec2021-07-24 09:03:29 -0600266 addr = (void *)hextoul(argv[1], NULL);
267 blk = hextoul(argv[2], NULL);
268 cnt = hextoul(argv[3], NULL);
269 key_addr = (void *)hextoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200270
Simon Glass9703a932024-08-22 07:57:51 -0600271 printf("MMC RPMB write: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200272 curr_device, blk, cnt);
273 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
274
275 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
276 if (n != cnt)
277 return CMD_RET_FAILURE;
278 return CMD_RET_SUCCESS;
279}
Simon Glassed38aef2020-05-10 11:40:03 -0600280
281static int do_mmcrpmb_counter(struct cmd_tbl *cmdtp, int flag,
282 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200283{
284 unsigned long counter;
285 struct mmc *mmc = find_mmc_device(curr_device);
286
287 if (mmc_rpmb_get_counter(mmc, &counter))
288 return CMD_RET_FAILURE;
289 printf("RPMB Write counter= %lx\n", counter);
290 return CMD_RET_SUCCESS;
291}
292
Simon Glassed38aef2020-05-10 11:40:03 -0600293static struct cmd_tbl cmd_rpmb[] = {
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200294 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
295 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
296 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
297 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
298};
299
Simon Glassed38aef2020-05-10 11:40:03 -0600300static int do_mmcrpmb(struct cmd_tbl *cmdtp, int flag,
301 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200302{
Simon Glassed38aef2020-05-10 11:40:03 -0600303 struct cmd_tbl *cp;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200304 struct mmc *mmc;
305 char original_part;
306 int ret;
307
308 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
309
310 /* Drop the rpmb subcommand */
311 argc--;
312 argv++;
313
314 if (cp == NULL || argc > cp->maxargs)
315 return CMD_RET_USAGE;
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100316 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200317 return CMD_RET_SUCCESS;
318
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600319 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200320 if (!mmc)
321 return CMD_RET_FAILURE;
322
323 if (!(mmc->version & MMC_VERSION_MMC)) {
Heinrich Schuchardteeb191c2020-03-29 19:26:57 +0000324 printf("It is not an eMMC device\n");
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200325 return CMD_RET_FAILURE;
Lei Wend430d7c2011-05-02 16:26:25 +0000326 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200327 if (mmc->version < MMC_VERSION_4_41) {
328 printf("RPMB not supported before version 4.41\n");
329 return CMD_RET_FAILURE;
330 }
331 /* Switch to the RPMB partition */
Kever Yangcb5f1932017-06-08 09:20:03 +0800332#ifndef CONFIG_BLK
Marek Vasuta62cf492016-05-04 16:35:25 +0200333 original_part = mmc->block_dev.hwpart;
Kever Yangcb5f1932017-06-08 09:20:03 +0800334#else
335 original_part = mmc_get_blk_desc(mmc)->hwpart;
336#endif
Simon Glassdbfa32c2022-08-11 19:34:59 -0600337 if (blk_select_hwpart_devnum(UCLASS_MMC, curr_device, MMC_PART_RPMB) !=
Simon Glass11f2bb62016-05-01 13:52:29 -0600338 0)
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700339 return CMD_RET_FAILURE;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200340 ret = cp->cmd(cmdtp, flag, argc, argv);
Andy Flemingad347bb2008-10-30 16:41:01 -0500341
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200342 /* Return to original partition */
Simon Glassdbfa32c2022-08-11 19:34:59 -0600343 if (blk_select_hwpart_devnum(UCLASS_MMC, curr_device, original_part) !=
Simon Glass11f2bb62016-05-01 13:52:29 -0600344 0)
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700345 return CMD_RET_FAILURE;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200346 return ret;
347}
348#endif
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530349
Simon Glassed38aef2020-05-10 11:40:03 -0600350static int do_mmc_read(struct cmd_tbl *cmdtp, int flag,
351 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200352{
353 struct mmc *mmc;
354 u32 blk, cnt, n;
Simon Glass75792952024-09-01 16:26:28 -0600355 void *ptr;
Stephen Warren18faca72013-04-01 11:50:28 +0000356
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200357 if (argc != 4)
358 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500359
Simon Glass75792952024-09-01 16:26:28 -0600360 ptr = map_sysmem(hextoul(argv[1], NULL), 0);
Simon Glass3ff49ec2021-07-24 09:03:29 -0600361 blk = hextoul(argv[2], NULL);
362 cnt = hextoul(argv[3], NULL);
Lei Wen61dba932010-09-13 22:07:28 +0800363
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600364 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200365 if (!mmc)
366 return CMD_RET_FAILURE;
Lei Wen61dba932010-09-13 22:07:28 +0800367
Simon Glass9703a932024-08-22 07:57:51 -0600368 printf("MMC read: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200369 curr_device, blk, cnt);
Stephen Warren18faca72013-04-01 11:50:28 +0000370
Simon Glass75792952024-09-01 16:26:28 -0600371 n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, ptr);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200372 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Simon Glass75792952024-09-01 16:26:28 -0600373 unmap_sysmem(ptr);
Lei Wend430d7c2011-05-02 16:26:25 +0000374
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200375 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
376}
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +0100377
Alex Kiernanc568bcb2018-05-29 15:30:52 +0000378#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar814a9752018-04-06 12:05:24 +0530379static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk,
380 lbaint_t blkcnt, const void *buffer)
381{
382 struct blk_desc *dev_desc = info->priv;
383
384 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
385}
386
387static lbaint_t mmc_sparse_reserve(struct sparse_storage *info,
388 lbaint_t blk, lbaint_t blkcnt)
389{
390 return blkcnt;
391}
392
Simon Glassed38aef2020-05-10 11:40:03 -0600393static int do_mmc_sparse_write(struct cmd_tbl *cmdtp, int flag,
394 int argc, char *const argv[])
Jassi Brar814a9752018-04-06 12:05:24 +0530395{
396 struct sparse_storage sparse;
397 struct blk_desc *dev_desc;
398 struct mmc *mmc;
399 char dest[11];
400 void *addr;
401 u32 blk;
402
403 if (argc != 3)
404 return CMD_RET_USAGE;
405
Simon Glass3ff49ec2021-07-24 09:03:29 -0600406 addr = (void *)hextoul(argv[1], NULL);
407 blk = hextoul(argv[2], NULL);
Jassi Brar814a9752018-04-06 12:05:24 +0530408
409 if (!is_sparse_image(addr)) {
410 printf("Not a sparse image\n");
411 return CMD_RET_FAILURE;
412 }
413
414 mmc = init_mmc_device(curr_device, false);
415 if (!mmc)
416 return CMD_RET_FAILURE;
417
Simon Glass9703a932024-08-22 07:57:51 -0600418 printf("MMC Sparse write: dev # %d, block # %d ... ",
Jassi Brar814a9752018-04-06 12:05:24 +0530419 curr_device, blk);
420
421 if (mmc_getwp(mmc) == 1) {
422 printf("Error: card is write protected!\n");
423 return CMD_RET_FAILURE;
424 }
425
426 dev_desc = mmc_get_blk_desc(mmc);
427 sparse.priv = dev_desc;
428 sparse.blksz = 512;
429 sparse.start = blk;
430 sparse.size = dev_desc->lba - blk;
431 sparse.write = mmc_sparse_write;
432 sparse.reserve = mmc_sparse_reserve;
433 sparse.mssg = NULL;
434 sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz);
435
Alex Kiernan27b69de2018-05-29 15:30:40 +0000436 if (write_sparse_image(&sparse, dest, addr, NULL))
Jassi Brar814a9752018-04-06 12:05:24 +0530437 return CMD_RET_FAILURE;
438 else
439 return CMD_RET_SUCCESS;
440}
441#endif
442
Alex Kiernanc568bcb2018-05-29 15:30:52 +0000443#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glassed38aef2020-05-10 11:40:03 -0600444static int do_mmc_write(struct cmd_tbl *cmdtp, int flag,
445 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200446{
447 struct mmc *mmc;
448 u32 blk, cnt, n;
Simon Glass75792952024-09-01 16:26:28 -0600449 void *ptr;
Andy Flemingad347bb2008-10-30 16:41:01 -0500450
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200451 if (argc != 4)
452 return CMD_RET_USAGE;
Lei Wend430d7c2011-05-02 16:26:25 +0000453
Simon Glass75792952024-09-01 16:26:28 -0600454 ptr = map_sysmem(hextoul(argv[1], NULL), 0);
Simon Glass3ff49ec2021-07-24 09:03:29 -0600455 blk = hextoul(argv[2], NULL);
456 cnt = hextoul(argv[3], NULL);
Lei Wend430d7c2011-05-02 16:26:25 +0000457
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600458 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200459 if (!mmc)
460 return CMD_RET_FAILURE;
Lei Wen31b99802011-05-02 16:26:26 +0000461
Simon Glass9703a932024-08-22 07:57:51 -0600462 printf("MMC write: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200463 curr_device, blk, cnt);
Lei Wen31b99802011-05-02 16:26:26 +0000464
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200465 if (mmc_getwp(mmc) == 1) {
466 printf("Error: card is write protected!\n");
467 return CMD_RET_FAILURE;
468 }
Simon Glass75792952024-09-01 16:26:28 -0600469 n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, ptr);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200470 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Simon Glass75792952024-09-01 16:26:28 -0600471 unmap_sysmem(ptr);
Lei Wend430d7c2011-05-02 16:26:25 +0000472
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200473 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
474}
Simon Glassed38aef2020-05-10 11:40:03 -0600475
476static int do_mmc_erase(struct cmd_tbl *cmdtp, int flag,
477 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200478{
479 struct mmc *mmc;
Tomas Paukrtab00d0e2024-09-02 20:49:17 +0200480 struct disk_partition info;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200481 u32 blk, cnt, n;
Tom Rinif8c6f792014-02-05 10:24:21 -0500482
Tomas Paukrtab00d0e2024-09-02 20:49:17 +0200483 if (argc < 2 || argc > 3)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200484 return CMD_RET_USAGE;
Tom Rinif8c6f792014-02-05 10:24:21 -0500485
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600486 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200487 if (!mmc)
488 return CMD_RET_FAILURE;
Tomas Paukrtab00d0e2024-09-02 20:49:17 +0200489
490 if (argc == 3) {
491 blk = hextoul(argv[1], NULL);
492 cnt = hextoul(argv[2], NULL);
493 } else if (part_get_info_by_name(mmc_get_blk_desc(mmc), argv[1], &info) >= 0) {
494 blk = info.start;
495 cnt = info.size;
496 } else {
497 return CMD_RET_FAILURE;
498 }
Tom Rinif8c6f792014-02-05 10:24:21 -0500499
Simon Glass9703a932024-08-22 07:57:51 -0600500 printf("MMC erase: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200501 curr_device, blk, cnt);
Tom Rini4cf854c2014-02-05 10:24:22 -0500502
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200503 if (mmc_getwp(mmc) == 1) {
504 printf("Error: card is write protected!\n");
505 return CMD_RET_FAILURE;
506 }
Simon Glasse5db1152016-05-01 13:52:35 -0600507 n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200508 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Tom Rini4cf854c2014-02-05 10:24:22 -0500509
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200510 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
511}
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +0100512#endif
513
Simon Glassed38aef2020-05-10 11:40:03 -0600514static int do_mmc_rescan(struct cmd_tbl *cmdtp, int flag,
515 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200516{
517 struct mmc *mmc;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530518
519 if (argc == 1) {
520 mmc = init_mmc_device(curr_device, true);
521 } else if (argc == 2) {
Heinrich Schuchardt05c74282022-04-25 23:11:06 +0200522 enum bus_mode speed_mode;
523
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530524 speed_mode = (int)dectoul(argv[1], NULL);
525 mmc = __init_mmc_device(curr_device, true, speed_mode);
526 } else {
527 return CMD_RET_USAGE;
528 }
Tom Rini4cf854c2014-02-05 10:24:22 -0500529
Stephen Warren07ec4e72014-05-23 13:24:46 -0600530 if (!mmc)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200531 return CMD_RET_FAILURE;
Tom Rini4cf854c2014-02-05 10:24:22 -0500532
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200533 return CMD_RET_SUCCESS;
534}
Simon Glassed38aef2020-05-10 11:40:03 -0600535
536static int do_mmc_part(struct cmd_tbl *cmdtp, int flag,
537 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200538{
Simon Glasse3394752016-02-29 15:25:34 -0700539 struct blk_desc *mmc_dev;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200540 struct mmc *mmc;
Amar3ccad532013-04-27 11:43:00 +0530541
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600542 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200543 if (!mmc)
544 return CMD_RET_FAILURE;
Amar3ccad532013-04-27 11:43:00 +0530545
Simon Glassfada3f92022-09-17 09:00:09 -0600546 mmc_dev = blk_get_devnum_by_uclass_id(UCLASS_MMC, curr_device);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200547 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
Simon Glassb89a8442016-02-29 15:25:48 -0700548 part_print(mmc_dev);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200549 return CMD_RET_SUCCESS;
550 }
Amar3ccad532013-04-27 11:43:00 +0530551
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200552 puts("get mmc type error!\n");
553 return CMD_RET_FAILURE;
554}
Simon Glassed38aef2020-05-10 11:40:03 -0600555
556static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag,
557 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200558{
Stephen Warren18549802014-05-23 12:48:10 -0600559 int dev, part = 0, ret;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200560 struct mmc *mmc;
Tom Rini35a3ea12014-02-07 14:15:20 -0500561
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200562 if (argc == 1) {
563 dev = curr_device;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530564 mmc = init_mmc_device(dev, true);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200565 } else if (argc == 2) {
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530566 dev = (int)dectoul(argv[1], NULL);
567 mmc = init_mmc_device(dev, true);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200568 } else if (argc == 3) {
Simon Glassff9b9032021-07-24 09:03:30 -0600569 dev = (int)dectoul(argv[1], NULL);
570 part = (int)dectoul(argv[2], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200571 if (part > PART_ACCESS_MASK) {
572 printf("#part_num shouldn't be larger than %d\n",
573 PART_ACCESS_MASK);
574 return CMD_RET_FAILURE;
Tom Rini35a3ea12014-02-07 14:15:20 -0500575 }
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530576 mmc = init_mmc_device(dev, true);
577 } else if (argc == 4) {
Heinrich Schuchardt05c74282022-04-25 23:11:06 +0200578 enum bus_mode speed_mode;
579
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530580 dev = (int)dectoul(argv[1], NULL);
581 part = (int)dectoul(argv[2], NULL);
582 if (part > PART_ACCESS_MASK) {
583 printf("#part_num shouldn't be larger than %d\n",
584 PART_ACCESS_MASK);
585 return CMD_RET_FAILURE;
586 }
587 speed_mode = (int)dectoul(argv[3], NULL);
588 mmc = __init_mmc_device(dev, true, speed_mode);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200589 } else {
590 return CMD_RET_USAGE;
591 }
Tom Rini35a3ea12014-02-07 14:15:20 -0500592
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200593 if (!mmc)
594 return CMD_RET_FAILURE;
Tom Rini35a3ea12014-02-07 14:15:20 -0500595
Simon Glassdbfa32c2022-08-11 19:34:59 -0600596 ret = blk_select_hwpart_devnum(UCLASS_MMC, dev, part);
Stephen Warren18549802014-05-23 12:48:10 -0600597 printf("switch to partitions #%d, %s\n",
598 part, (!ret) ? "OK" : "ERROR");
599 if (ret)
600 return 1;
601
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200602 curr_device = dev;
603 if (mmc->part_config == MMCPART_NOAVAILABLE)
604 printf("mmc%d is current device\n", curr_device);
605 else
606 printf("mmc%d(part %d) is current device\n",
Simon Glasse5db1152016-05-01 13:52:35 -0600607 curr_device, mmc_get_blk_desc(mmc)->hwpart);
Tom Rini35a3ea12014-02-07 14:15:20 -0500608
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200609 return CMD_RET_SUCCESS;
610}
Simon Glassed38aef2020-05-10 11:40:03 -0600611
612static int do_mmc_list(struct cmd_tbl *cmdtp, int flag,
613 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200614{
615 print_mmc_devices('\n');
616 return CMD_RET_SUCCESS;
617}
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100618
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +0100619#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Marek Vasut94e39952021-09-15 11:43:13 +0200620static void parse_hwpart_user_enh_size(struct mmc *mmc,
621 struct mmc_hwpart_conf *pconf,
622 char *argv)
623{
Marek Vasut30d29662022-01-17 22:54:29 +0100624 int i, ret;
Marek Vasut94e39952021-09-15 11:43:13 +0200625
626 pconf->user.enh_size = 0;
627
628 if (!strcmp(argv, "-")) { /* The rest of eMMC */
629 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
630 ret = mmc_send_ext_csd(mmc, ext_csd);
631 if (ret)
632 return;
Marek Vasut30d29662022-01-17 22:54:29 +0100633 /* The enh_size value is in 512B block units */
Marek Vasut94e39952021-09-15 11:43:13 +0200634 pconf->user.enh_size =
635 ((ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 2] << 16) +
636 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 1] << 8) +
637 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT]) * 1024 *
638 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
639 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
640 pconf->user.enh_size -= pconf->user.enh_start;
Marek Vasut30d29662022-01-17 22:54:29 +0100641 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
642 /*
643 * If the eMMC already has GP partitions set,
644 * subtract their size from the maximum USER
645 * partition size.
646 *
647 * Else, if the command was used to configure new
648 * GP partitions, subtract their size from maximum
649 * USER partition size.
650 */
651 if (mmc->capacity_gp[i]) {
652 /* The capacity_gp is in 1B units */
653 pconf->user.enh_size -= mmc->capacity_gp[i] >> 9;
654 } else if (pconf->gp_part[i].size) {
655 /* The gp_part[].size is in 512B units */
656 pconf->user.enh_size -= pconf->gp_part[i].size;
657 }
658 }
Marek Vasut94e39952021-09-15 11:43:13 +0200659 } else {
660 pconf->user.enh_size = dectoul(argv, NULL);
661 }
662}
663
664static int parse_hwpart_user(struct mmc *mmc, struct mmc_hwpart_conf *pconf,
Simon Glassed38aef2020-05-10 11:40:03 -0600665 int argc, char *const argv[])
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100666{
667 int i = 0;
668
669 memset(&pconf->user, 0, sizeof(pconf->user));
670
671 while (i < argc) {
672 if (!strcmp(argv[i], "enh")) {
673 if (i + 2 >= argc)
674 return -1;
675 pconf->user.enh_start =
Simon Glassff9b9032021-07-24 09:03:30 -0600676 dectoul(argv[i + 1], NULL);
Marek Vasut94e39952021-09-15 11:43:13 +0200677 parse_hwpart_user_enh_size(mmc, pconf, argv[i + 2]);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100678 i += 3;
679 } else if (!strcmp(argv[i], "wrrel")) {
680 if (i + 1 >= argc)
681 return -1;
682 pconf->user.wr_rel_change = 1;
683 if (!strcmp(argv[i+1], "on"))
684 pconf->user.wr_rel_set = 1;
685 else if (!strcmp(argv[i+1], "off"))
686 pconf->user.wr_rel_set = 0;
687 else
688 return -1;
689 i += 2;
690 } else {
691 break;
692 }
693 }
694 return i;
695}
696
697static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
Simon Glassed38aef2020-05-10 11:40:03 -0600698 int argc, char *const argv[])
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100699{
700 int i;
701
702 memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
703
704 if (1 >= argc)
705 return -1;
Simon Glassff9b9032021-07-24 09:03:30 -0600706 pconf->gp_part[pidx].size = dectoul(argv[0], NULL);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100707
708 i = 1;
709 while (i < argc) {
710 if (!strcmp(argv[i], "enh")) {
711 pconf->gp_part[pidx].enhanced = 1;
712 i += 1;
713 } else if (!strcmp(argv[i], "wrrel")) {
714 if (i + 1 >= argc)
715 return -1;
716 pconf->gp_part[pidx].wr_rel_change = 1;
717 if (!strcmp(argv[i+1], "on"))
718 pconf->gp_part[pidx].wr_rel_set = 1;
719 else if (!strcmp(argv[i+1], "off"))
720 pconf->gp_part[pidx].wr_rel_set = 0;
721 else
722 return -1;
723 i += 2;
724 } else {
725 break;
726 }
727 }
728 return i;
729}
730
Simon Glassed38aef2020-05-10 11:40:03 -0600731static int do_mmc_hwpartition(struct cmd_tbl *cmdtp, int flag,
732 int argc, char *const argv[])
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100733{
734 struct mmc *mmc;
735 struct mmc_hwpart_conf pconf = { };
736 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100737 int i, r, pidx;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100738
739 mmc = init_mmc_device(curr_device, false);
740 if (!mmc)
741 return CMD_RET_FAILURE;
742
Jaehoon Chungffbba622021-09-24 09:23:34 +0900743 if (IS_SD(mmc)) {
744 puts("SD doesn't support partitioning\n");
745 return CMD_RET_FAILURE;
746 }
747
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100748 if (argc < 1)
749 return CMD_RET_USAGE;
750 i = 1;
751 while (i < argc) {
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100752 if (!strcmp(argv[i], "user")) {
753 i++;
Marek Vasut94e39952021-09-15 11:43:13 +0200754 r = parse_hwpart_user(mmc, &pconf, argc - i, &argv[i]);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100755 if (r < 0)
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100756 return CMD_RET_USAGE;
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100757 i += r;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100758 } else if (!strncmp(argv[i], "gp", 2) &&
759 strlen(argv[i]) == 3 &&
760 argv[i][2] >= '1' && argv[i][2] <= '4') {
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100761 pidx = argv[i][2] - '1';
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100762 i++;
763 r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
764 if (r < 0)
765 return CMD_RET_USAGE;
766 i += r;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100767 } else if (!strcmp(argv[i], "check")) {
768 mode = MMC_HWPART_CONF_CHECK;
769 i++;
770 } else if (!strcmp(argv[i], "set")) {
771 mode = MMC_HWPART_CONF_SET;
772 i++;
773 } else if (!strcmp(argv[i], "complete")) {
774 mode = MMC_HWPART_CONF_COMPLETE;
775 i++;
776 } else {
777 return CMD_RET_USAGE;
778 }
779 }
780
781 puts("Partition configuration:\n");
782 if (pconf.user.enh_size) {
783 puts("\tUser Enhanced Start: ");
784 print_size(((u64)pconf.user.enh_start) << 9, "\n");
785 puts("\tUser Enhanced Size: ");
786 print_size(((u64)pconf.user.enh_size) << 9, "\n");
787 } else {
788 puts("\tNo enhanced user data area\n");
789 }
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100790 if (pconf.user.wr_rel_change)
791 printf("\tUser partition write reliability: %s\n",
792 pconf.user.wr_rel_set ? "on" : "off");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100793 for (pidx = 0; pidx < 4; pidx++) {
794 if (pconf.gp_part[pidx].size) {
795 printf("\tGP%i Capacity: ", pidx+1);
796 print_size(((u64)pconf.gp_part[pidx].size) << 9,
797 pconf.gp_part[pidx].enhanced ?
798 " ENH\n" : "\n");
799 } else {
800 printf("\tNo GP%i partition\n", pidx+1);
801 }
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100802 if (pconf.gp_part[pidx].wr_rel_change)
803 printf("\tGP%i write reliability: %s\n", pidx+1,
804 pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100805 }
806
807 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
808 if (mode == MMC_HWPART_CONF_COMPLETE)
809 puts("Partitioning successful, "
810 "power-cycle to make effective\n");
811 return CMD_RET_SUCCESS;
812 } else {
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100813 puts("Failed!\n");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100814 return CMD_RET_FAILURE;
815 }
816}
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +0100817#endif
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100818
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200819#ifdef CONFIG_SUPPORT_EMMC_BOOT
Simon Glassed38aef2020-05-10 11:40:03 -0600820static int do_mmc_bootbus(struct cmd_tbl *cmdtp, int flag,
821 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200822{
823 int dev;
824 struct mmc *mmc;
825 u8 width, reset, mode;
826
827 if (argc != 5)
828 return CMD_RET_USAGE;
Simon Glassff9b9032021-07-24 09:03:30 -0600829 dev = dectoul(argv[1], NULL);
830 width = dectoul(argv[2], NULL);
831 reset = dectoul(argv[3], NULL);
832 mode = dectoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200833
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600834 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200835 if (!mmc)
836 return CMD_RET_FAILURE;
837
838 if (IS_SD(mmc)) {
839 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
840 return CMD_RET_FAILURE;
Amar3ccad532013-04-27 11:43:00 +0530841 }
Markus Niebel03951412013-12-16 13:40:46 +0100842
Jaehoon Chung7483dd32021-02-26 18:38:20 +0900843 /*
844 * BOOT_BUS_CONDITIONS[177]
845 * BOOT_MODE[4:3]
846 * 0x0 : Use SDR + Backward compatible timing in boot operation
847 * 0x1 : Use SDR + High Speed Timing in boot operation mode
848 * 0x2 : Use DDR in boot operation
849 * RESET_BOOT_BUS_CONDITIONS
850 * 0x0 : Reset bus width to x1, SDR, Backward compatible
851 * 0x1 : Retain BOOT_BUS_WIDTH and BOOT_MODE
852 * BOOT_BUS_WIDTH
853 * 0x0 : x1(sdr) or x4 (ddr) buswidth
854 * 0x1 : x4(sdr/ddr) buswith
855 * 0x2 : x8(sdr/ddr) buswith
856 *
857 */
858 if (width >= 0x3) {
859 printf("boot_bus_width %d is invalid\n", width);
860 return CMD_RET_FAILURE;
861 }
862
863 if (reset >= 0x2) {
864 printf("reset_boot_bus_width %d is invalid\n", reset);
865 return CMD_RET_FAILURE;
866 }
867
868 if (mode >= 0x3) {
869 printf("reset_boot_bus_width %d is invalid\n", mode);
870 return CMD_RET_FAILURE;
871 }
872
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200873 /* acknowledge to be sent during boot operation */
Jaehoon Chung7483dd32021-02-26 18:38:20 +0900874 if (mmc_set_boot_bus_width(mmc, width, reset, mode)) {
875 puts("BOOT_BUS_WIDTH is failed to change.\n");
876 return CMD_RET_FAILURE;
877 }
878
879 printf("Set to BOOT_BUS_WIDTH = 0x%x, RESET = 0x%x, BOOT_MODE = 0x%x\n",
880 width, reset, mode);
881 return CMD_RET_SUCCESS;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200882}
Simon Glassed38aef2020-05-10 11:40:03 -0600883
884static int do_mmc_boot_resize(struct cmd_tbl *cmdtp, int flag,
885 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200886{
887 int dev;
888 struct mmc *mmc;
889 u32 bootsize, rpmbsize;
Markus Niebel03951412013-12-16 13:40:46 +0100890
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200891 if (argc != 4)
892 return CMD_RET_USAGE;
Simon Glassff9b9032021-07-24 09:03:30 -0600893 dev = dectoul(argv[1], NULL);
894 bootsize = dectoul(argv[2], NULL);
895 rpmbsize = dectoul(argv[3], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200896
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600897 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200898 if (!mmc)
899 return CMD_RET_FAILURE;
900
901 if (IS_SD(mmc)) {
Heinrich Schuchardteeb191c2020-03-29 19:26:57 +0000902 printf("It is not an eMMC device\n");
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200903 return CMD_RET_FAILURE;
Markus Niebel03951412013-12-16 13:40:46 +0100904 }
905
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200906 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
907 printf("EMMC boot partition Size change Failed.\n");
908 return CMD_RET_FAILURE;
909 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500910
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200911 printf("EMMC boot partition Size %d MB\n", bootsize);
912 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
913 return CMD_RET_SUCCESS;
914}
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200915
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200916static int mmc_partconf_print(struct mmc *mmc, const char *varname)
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200917{
918 u8 ack, access, part;
919
920 if (mmc->part_config == MMCPART_NOAVAILABLE) {
921 printf("No part_config info for ver. 0x%x\n", mmc->version);
922 return CMD_RET_FAILURE;
923 }
924
925 access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
926 ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
927 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
928
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200929 if(varname)
930 env_set_hex(varname, part);
931
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200932 printf("EXT_CSD[179], PARTITION_CONFIG:\n"
933 "BOOT_ACK: 0x%x\n"
Tim Harvey728cbde2024-05-31 08:36:34 -0700934 "BOOT_PARTITION_ENABLE: 0x%x (%s)\n"
935 "PARTITION_ACCESS: 0x%x (%s)\n", ack, part, emmc_boot_part_names[part],
936 access, emmc_hwpart_names[access]);
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200937
938 return CMD_RET_SUCCESS;
939}
940
Simon Glassed38aef2020-05-10 11:40:03 -0600941static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag,
942 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200943{
Pali Rohár332c2302023-03-22 21:06:53 +0100944 int ret, dev;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200945 struct mmc *mmc;
946 u8 ack, part_num, access;
Lei Wenea526762011-06-22 17:03:31 +0000947
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200948 if (argc != 2 && argc != 3 && argc != 5)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200949 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500950
Simon Glassff9b9032021-07-24 09:03:30 -0600951 dev = dectoul(argv[1], NULL);
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530952
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600953 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200954 if (!mmc)
955 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500956
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200957 if (IS_SD(mmc)) {
958 puts("PARTITION_CONFIG only exists on eMMC\n");
959 return CMD_RET_FAILURE;
960 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500961
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200962 if (argc == 2 || argc == 3)
Simon Glass793a98e2023-11-18 14:05:20 -0700963 return mmc_partconf_print(mmc, cmd_arg2(argc, argv));
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200964
Tim Harvey728cbde2024-05-31 08:36:34 -0700965 /* BOOT_ACK */
Simon Glassff9b9032021-07-24 09:03:30 -0600966 ack = dectoul(argv[2], NULL);
Tim Harvey728cbde2024-05-31 08:36:34 -0700967 /* BOOT_PARTITION_ENABLE */
968 if (!isdigit(*argv[3])) {
969 for (part_num = ARRAY_SIZE(emmc_boot_part_names) - 1; part_num > 0; part_num--) {
970 if (!strcmp(argv[3], emmc_boot_part_names[part_num]))
971 break;
972 }
973 } else {
974 part_num = dectoul(argv[3], NULL);
975 }
976 /* PARTITION_ACCESS */
977 if (!isdigit(*argv[4])) {
978 for (access = ARRAY_SIZE(emmc_hwpart_names) - 1; access > 0; access--) {
979 if (!strcmp(argv[4], emmc_hwpart_names[access]))
980 break;
981 }
982 } else {
983 access = dectoul(argv[4], NULL);
984 }
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200985
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200986 /* acknowledge to be sent during boot operation */
Pali Rohár332c2302023-03-22 21:06:53 +0100987 ret = mmc_set_part_conf(mmc, ack, part_num, access);
988 if (ret != 0)
989 return CMD_RET_FAILURE;
990
991 return CMD_RET_SUCCESS;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200992}
Simon Glassed38aef2020-05-10 11:40:03 -0600993
994static int do_mmc_rst_func(struct cmd_tbl *cmdtp, int flag,
995 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200996{
Pali Rohár332c2302023-03-22 21:06:53 +0100997 int ret, dev;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200998 struct mmc *mmc;
999 u8 enable;
Nikita Kiryanov020f2612012-12-03 02:19:46 +00001000
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001001 /*
1002 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
1003 * The only valid values are 0x0, 0x1 and 0x2 and writing
1004 * a value of 0x1 or 0x2 sets the value permanently.
1005 */
1006 if (argc != 3)
1007 return CMD_RET_USAGE;
1008
Simon Glassff9b9032021-07-24 09:03:30 -06001009 dev = dectoul(argv[1], NULL);
1010 enable = dectoul(argv[2], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001011
Peng Fanb1e455a2015-11-25 17:16:21 +08001012 if (enable > 2) {
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001013 puts("Invalid RST_n_ENABLE value\n");
1014 return CMD_RET_USAGE;
1015 }
1016
Stephen Warrenb627f4b2014-05-23 13:24:45 -06001017 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001018 if (!mmc)
1019 return CMD_RET_FAILURE;
1020
1021 if (IS_SD(mmc)) {
1022 puts("RST_n_FUNCTION only exists on eMMC\n");
1023 return CMD_RET_FAILURE;
1024 }
1025
Pali Rohár332c2302023-03-22 21:06:53 +01001026 ret = mmc_set_rst_n_function(mmc, enable);
1027 if (ret != 0)
1028 return CMD_RET_FAILURE;
1029
1030 return CMD_RET_SUCCESS;
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001031}
1032#endif
Simon Glassed38aef2020-05-10 11:40:03 -06001033static int do_mmc_setdsr(struct cmd_tbl *cmdtp, int flag,
1034 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001035{
1036 struct mmc *mmc;
1037 u32 val;
1038 int ret;
Andy Flemingad347bb2008-10-30 16:41:01 -05001039
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001040 if (argc != 2)
1041 return CMD_RET_USAGE;
Simon Glass3ff49ec2021-07-24 09:03:29 -06001042 val = hextoul(argv[1], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001043
1044 mmc = find_mmc_device(curr_device);
1045 if (!mmc) {
1046 printf("no mmc device at slot %x\n", curr_device);
1047 return CMD_RET_FAILURE;
1048 }
1049 ret = mmc_set_dsr(mmc, val);
1050 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
1051 if (!ret) {
1052 mmc->has_init = 0;
1053 if (mmc_init(mmc))
1054 return CMD_RET_FAILURE;
1055 else
1056 return CMD_RET_SUCCESS;
Andy Flemingad347bb2008-10-30 16:41:01 -05001057 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001058 return ret;
1059}
Lei Wend430d7c2011-05-02 16:26:25 +00001060
Tomas Melinc17dae52016-11-25 11:01:03 +02001061#ifdef CONFIG_CMD_BKOPS_ENABLE
Marek Vasutefdeed62023-01-05 15:19:08 +01001062static int mmc_bkops_common(char *device, bool autobkops, bool enable)
Tomas Melinc17dae52016-11-25 11:01:03 +02001063{
Tomas Melinc17dae52016-11-25 11:01:03 +02001064 struct mmc *mmc;
Marek Vasutefdeed62023-01-05 15:19:08 +01001065 int dev;
Tomas Melinc17dae52016-11-25 11:01:03 +02001066
Marek Vasutefdeed62023-01-05 15:19:08 +01001067 dev = dectoul(device, NULL);
Tomas Melinc17dae52016-11-25 11:01:03 +02001068
1069 mmc = init_mmc_device(dev, false);
1070 if (!mmc)
1071 return CMD_RET_FAILURE;
1072
1073 if (IS_SD(mmc)) {
1074 puts("BKOPS_EN only exists on eMMC\n");
1075 return CMD_RET_FAILURE;
1076 }
1077
Marek Vasutefdeed62023-01-05 15:19:08 +01001078 return mmc_set_bkops_enable(mmc, autobkops, enable);
1079}
1080
1081static int do_mmc_bkops(struct cmd_tbl *cmdtp, int flag,
1082 int argc, char * const argv[])
1083{
1084 bool autobkops, enable;
1085
1086 if (argc != 4)
1087 return CMD_RET_USAGE;
1088
1089 if (!strcmp(argv[2], "manual"))
1090 autobkops = false;
1091 else if (!strcmp(argv[2], "auto"))
1092 autobkops = true;
1093 else
1094 return CMD_RET_FAILURE;
1095
1096 if (!strcmp(argv[3], "disable"))
1097 enable = false;
1098 else if (!strcmp(argv[3], "enable"))
1099 enable = true;
1100 else
1101 return CMD_RET_FAILURE;
1102
1103 return mmc_bkops_common(argv[1], autobkops, enable);
1104}
1105
1106static int do_mmc_bkops_enable(struct cmd_tbl *cmdtp, int flag,
1107 int argc, char * const argv[])
1108{
1109 if (argc != 2)
1110 return CMD_RET_USAGE;
1111
1112 return mmc_bkops_common(argv[1], false, true);
Tomas Melinc17dae52016-11-25 11:01:03 +02001113}
1114#endif
1115
Simon Glassed38aef2020-05-10 11:40:03 -06001116static int do_mmc_boot_wp(struct cmd_tbl *cmdtp, int flag,
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +02001117 int argc, char * const argv[])
1118{
1119 int err;
1120 struct mmc *mmc;
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001121 int part;
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +02001122
1123 mmc = init_mmc_device(curr_device, false);
1124 if (!mmc)
1125 return CMD_RET_FAILURE;
1126 if (IS_SD(mmc)) {
1127 printf("It is not an eMMC device\n");
1128 return CMD_RET_FAILURE;
1129 }
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001130
1131 if (argc == 2) {
1132 part = dectoul(argv[1], NULL);
1133 err = mmc_boot_wp_single_partition(mmc, part);
1134 } else {
1135 err = mmc_boot_wp(mmc);
1136 }
1137
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +02001138 if (err)
1139 return CMD_RET_FAILURE;
1140 printf("boot areas protected\n");
1141 return CMD_RET_SUCCESS;
1142}
1143
Marek Vasut1f692e32023-10-31 13:20:17 +01001144#if CONFIG_IS_ENABLED(CMD_MMC_REG)
1145static int do_mmc_reg(struct cmd_tbl *cmdtp, int flag,
1146 int argc, char *const argv[])
1147{
1148 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1149 struct mmc *mmc;
1150 int i, ret;
1151 u32 off;
1152
1153 if (argc < 3 || argc > 5)
1154 return CMD_RET_USAGE;
1155
1156 mmc = find_mmc_device(curr_device);
1157 if (!mmc) {
1158 printf("no mmc device at slot %x\n", curr_device);
1159 return CMD_RET_FAILURE;
1160 }
1161
1162 if (IS_SD(mmc)) {
1163 printf("SD registers are not supported\n");
1164 return CMD_RET_FAILURE;
1165 }
1166
1167 off = simple_strtoul(argv[3], NULL, 10);
1168 if (!strcmp(argv[2], "cid")) {
1169 if (off > 3)
1170 return CMD_RET_USAGE;
1171 printf("CID[%i]: 0x%08x\n", off, mmc->cid[off]);
1172 if (argv[4])
1173 env_set_hex(argv[4], mmc->cid[off]);
1174 return CMD_RET_SUCCESS;
1175 }
1176 if (!strcmp(argv[2], "csd")) {
1177 if (off > 3)
1178 return CMD_RET_USAGE;
1179 printf("CSD[%i]: 0x%08x\n", off, mmc->csd[off]);
1180 if (argv[4])
1181 env_set_hex(argv[4], mmc->csd[off]);
1182 return CMD_RET_SUCCESS;
1183 }
1184 if (!strcmp(argv[2], "dsr")) {
1185 printf("DSR: 0x%08x\n", mmc->dsr);
1186 if (argv[4])
1187 env_set_hex(argv[4], mmc->dsr);
1188 return CMD_RET_SUCCESS;
1189 }
1190 if (!strcmp(argv[2], "ocr")) {
1191 printf("OCR: 0x%08x\n", mmc->ocr);
1192 if (argv[4])
1193 env_set_hex(argv[4], mmc->ocr);
1194 return CMD_RET_SUCCESS;
1195 }
1196 if (!strcmp(argv[2], "rca")) {
1197 printf("RCA: 0x%08x\n", mmc->rca);
1198 if (argv[4])
1199 env_set_hex(argv[4], mmc->rca);
1200 return CMD_RET_SUCCESS;
1201 }
1202 if (!strcmp(argv[2], "extcsd") &&
1203 mmc->version >= MMC_VERSION_4_41) {
1204 ret = mmc_send_ext_csd(mmc, ext_csd);
1205 if (ret)
1206 return CMD_RET_FAILURE;
1207 if (!strcmp(argv[3], "all")) {
1208 /* Dump the entire register */
1209 printf("EXT_CSD:");
1210 for (i = 0; i < MMC_MAX_BLOCK_LEN; i++) {
1211 if (!(i % 10))
1212 printf("\n%03i: ", i);
1213 printf(" %02x", ext_csd[i]);
1214 }
1215 printf("\n");
1216 return CMD_RET_SUCCESS;
1217 }
1218 off = simple_strtoul(argv[3], NULL, 10);
1219 if (off > 512)
1220 return CMD_RET_USAGE;
1221 printf("EXT_CSD[%i]: 0x%02x\n", off, ext_csd[off]);
1222 if (argv[4])
1223 env_set_hex(argv[4], ext_csd[off]);
1224 return CMD_RET_SUCCESS;
1225 }
1226
1227 return CMD_RET_FAILURE;
1228}
1229#endif
1230
Simon Glassed38aef2020-05-10 11:40:03 -06001231static struct cmd_tbl cmd_mmc[] = {
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001232 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
1233 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001234 U_BOOT_CMD_MKENT(wp, 2, 0, do_mmc_boot_wp, "", ""),
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01001235#if CONFIG_IS_ENABLED(MMC_WRITE)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001236 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
1237 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01001238#endif
Alex Kiernanc568bcb2018-05-29 15:30:52 +00001239#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
1240 U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
1241#endif
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301242 U_BOOT_CMD_MKENT(rescan, 2, 1, do_mmc_rescan, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001243 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301244 U_BOOT_CMD_MKENT(dev, 4, 0, do_mmc_dev, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001245 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +01001246#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001247 U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +01001248#endif
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001249#ifdef CONFIG_SUPPORT_EMMC_BOOT
1250 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehd49c4aa2014-09-25 23:00:16 +08001251 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001252 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
1253 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
1254#endif
Alex Kiernan60e0f612018-05-08 04:43:31 +00001255#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001256 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
1257#endif
1258 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
Tomas Melinc17dae52016-11-25 11:01:03 +02001259#ifdef CONFIG_CMD_BKOPS_ENABLE
1260 U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
Marek Vasutefdeed62023-01-05 15:19:08 +01001261 U_BOOT_CMD_MKENT(bkops, 4, 0, do_mmc_bkops, "", ""),
Tomas Melinc17dae52016-11-25 11:01:03 +02001262#endif
Marek Vasut1f692e32023-10-31 13:20:17 +01001263#if CONFIG_IS_ENABLED(CMD_MMC_REG)
1264 U_BOOT_CMD_MKENT(reg, 5, 0, do_mmc_reg, "", ""),
1265#endif
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001266};
1267
Simon Glassed38aef2020-05-10 11:40:03 -06001268static int do_mmcops(struct cmd_tbl *cmdtp, int flag, int argc,
1269 char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001270{
Simon Glassed38aef2020-05-10 11:40:03 -06001271 struct cmd_tbl *cp;
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001272
1273 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
1274
1275 /* Drop the mmc command */
1276 argc--;
1277 argv++;
1278
1279 if (cp == NULL || argc > cp->maxargs)
1280 return CMD_RET_USAGE;
Boris Brezillonc71dfd12018-12-03 22:54:20 +01001281 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001282 return CMD_RET_SUCCESS;
1283
1284 if (curr_device < 0) {
1285 if (get_mmc_num() > 0) {
1286 curr_device = 0;
1287 } else {
1288 puts("No MMC device available\n");
1289 return CMD_RET_FAILURE;
1290 }
1291 }
1292 return cp->cmd(cmdtp, flag, argc, argv);
Andy Flemingad347bb2008-10-30 16:41:01 -05001293}
1294
1295U_BOOT_CMD(
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001296 mmc, 29, 1, do_mmcops,
Mike Frysinger27e1e622009-03-23 22:27:34 -04001297 "MMC sub system",
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001298 "info - display info of the current MMC device\n"
1299 "mmc read addr blk# cnt\n"
Lei Wend430d7c2011-05-02 16:26:25 +00001300 "mmc write addr blk# cnt\n"
Alex Kiernanc568bcb2018-05-29 15:30:52 +00001301#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar814a9752018-04-06 12:05:24 +05301302 "mmc swrite addr blk#\n"
1303#endif
Lei Wenea526762011-06-22 17:03:31 +00001304 "mmc erase blk# cnt\n"
Tomas Paukrtab00d0e2024-09-02 20:49:17 +02001305 "mmc erase partname\n"
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301306 "mmc rescan [mode]\n"
Lei Wend430d7c2011-05-02 16:26:25 +00001307 "mmc part - lists available partition on current mmc device\n"
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301308 "mmc dev [dev] [part] [mode] - show or set current mmc device [partition] and set mode\n"
1309 " - the required speed mode is passed as the index from the following list\n"
1310 " [MMC_LEGACY, MMC_HS, SD_HS, MMC_HS_52, MMC_DDR_52, UHS_SDR12, UHS_SDR25,\n"
1311 " UHS_SDR50, UHS_DDR50, UHS_SDR104, MMC_HS_200, MMC_HS_400, MMC_HS_400_ES]\n"
Amar3ccad532013-04-27 11:43:00 +05301312 "mmc list - lists available devices\n"
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001313 "mmc wp [PART] - power on write protect boot partitions\n"
1314 " arguments:\n"
1315 " PART - [0|1]\n"
1316 " : 0 - first boot partition, 1 - second boot partition\n"
1317 " if not assigned, write protect all boot partitions\n"
Alex Kiernan72d80462018-06-11 16:20:09 +00001318#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Jaehoon Chung94a47572021-02-26 16:07:18 +09001319 "mmc hwpartition <USER> <GP> <MODE> - does hardware partitioning\n"
Diego Santa Cruz420fc162014-12-23 10:50:30 +01001320 " arguments (sizes in 512-byte blocks):\n"
Jaehoon Chung94a47572021-02-26 16:07:18 +09001321 " USER - <user> <enh> <start> <cnt> <wrrel> <{on|off}>\n"
1322 " : sets user data area attributes\n"
1323 " GP - <{gp1|gp2|gp3|gp4}> <cnt> <enh> <wrrel> <{on|off}>\n"
1324 " : general purpose partition\n"
1325 " MODE - <{check|set|complete}>\n"
1326 " : mode, complete set partitioning completed\n"
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001327 " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
1328 " Power cycling is required to initialize partitions after set to complete.\n"
Alex Kiernan72d80462018-06-11 16:20:09 +00001329#endif
Amar3ccad532013-04-27 11:43:00 +05301330#ifdef CONFIG_SUPPORT_EMMC_BOOT
Jaehoon Chung8460b912020-12-16 07:24:50 +09001331 "mmc bootbus <dev> <boot_bus_width> <reset_boot_bus_width> <boot_mode>\n"
Tom Rini4cf854c2014-02-05 10:24:22 -05001332 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rini79919462014-02-05 10:24:20 -05001333 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
1334 " - Change sizes of boot and RPMB partitions of specified device\n"
Reuben Dowle55a81ad2021-05-14 10:15:43 +12001335 "mmc partconf <dev> [[varname] | [<boot_ack> <boot_partition> <partition_access>]]\n"
Angelo Dureghellof54f7532017-08-01 14:27:10 +02001336 " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
Reuben Dowle55a81ad2021-05-14 10:15:43 +12001337 " If showing the bits, optionally store the boot_partition field into varname\n"
Jaehoon Chung8460b912020-12-16 07:24:50 +09001338 "mmc rst-function <dev> <value>\n"
Tom Rini35a3ea12014-02-07 14:15:20 -05001339 " - Change the RST_n_FUNCTION field of the specified device\n"
1340 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behmedf8852a2009-02-18 19:59:39 +01001341#endif
Alex Kiernan60e0f612018-05-08 04:43:31 +00001342#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001343 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
1344 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
1345 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
1346 "mmc rpmb counter - read the value of the write counter\n"
1347#endif
1348 "mmc setdsr <value> - set DSR register value\n"
Tomas Melinc17dae52016-11-25 11:01:03 +02001349#ifdef CONFIG_CMD_BKOPS_ENABLE
1350 "mmc bkops-enable <dev> - enable background operations handshake on device\n"
1351 " WARNING: This is a write-once setting.\n"
Marek Vasutefdeed62023-01-05 15:19:08 +01001352 "mmc bkops <dev> [auto|manual] [enable|disable]\n"
1353 " - configure background operations handshake on device\n"
Tomas Melinc17dae52016-11-25 11:01:03 +02001354#endif
Marek Vasut1f692e32023-10-31 13:20:17 +01001355#if CONFIG_IS_ENABLED(CMD_MMC_REG)
1356 "mmc reg read <reg> <offset> [env] - read card register <reg> offset <offset>\n"
1357 " (optionally into [env] variable)\n"
1358 " - reg: cid/csd/dsr/ocr/rca/extcsd\n"
1359 " - offset: for cid/csd [0..3], for extcsd [0..511,all]\n"
1360#endif
Amar3ccad532013-04-27 11:43:00 +05301361 );
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001362
1363/* Old command kept for compatibility. Same as 'mmc info' */
1364U_BOOT_CMD(
1365 mmcinfo, 1, 0, do_mmcinfo,
1366 "display MMC info",
1367 "- display info of the current MMC device"
1368);