blob: 7244a90f4dcebb3f1d415505a936963d7625b15b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk7a428cc2003-06-15 22:40:42 +00002/*
3 * (C) Copyright 2003
4 * Kyle Harris, kharris@nexus-tech.net
wdenk7a428cc2003-06-15 22:40:42 +00005 */
6
Simon Glass655306c2020-05-10 11:39:58 -06007#include <blk.h>
wdenk7a428cc2003-06-15 22:40:42 +00008#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -07009#include <console.h>
Simon Glass1ab16922022-07-31 12:28:48 -060010#include <display_options.h>
Marek Vasutdfff8ed2020-06-20 14:28:25 +020011#include <memalign.h>
wdenk7a428cc2003-06-15 22:40:42 +000012#include <mmc.h>
Simon Glass655306c2020-05-10 11:39:58 -060013#include <part.h>
Jassi Brar814a9752018-04-06 12:05:24 +053014#include <sparse_format.h>
15#include <image-sparse.h>
Tom Rini5c1444f2024-04-27 08:10:59 -060016#include <vsprintf.h>
wdenk7a428cc2003-06-15 22:40:42 +000017
Mike Frysinger0289fe62009-06-14 21:35:21 -040018static int curr_device = -1;
Andy Flemingad347bb2008-10-30 16:41:01 -050019
20static void print_mmcinfo(struct mmc *mmc)
21{
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010022 int i;
23
Pantelis Antoniou2c850462014-03-11 19:34:20 +020024 printf("Device: %s\n", mmc->cfg->name);
Andy Flemingad347bb2008-10-30 16:41:01 -050025 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
Max Merchel1414ada2022-02-10 10:16:39 +010026 if (IS_SD(mmc)) {
27 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
28 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
29 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
30 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
31 } else {
32 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xff);
33 printf("Name: %c%c%c%c%c%c \n", mmc->cid[0] & 0xff,
34 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
35 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
36 (mmc->cid[2] >> 24));
37 }
Andy Flemingad347bb2008-10-30 16:41:01 -050038
Jean-Jacques Hiblotf62caf92017-09-21 16:29:56 +020039 printf("Bus Speed: %d\n", mmc->clock);
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +010040#if CONFIG_IS_ENABLED(MMC_VERBOSE)
Marek Vasutd1e63462019-03-18 04:49:21 +010041 printf("Mode: %s\n", mmc_mode_name(mmc->selected_mode));
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +010042 mmc_dump_capabilities("card capabilities", mmc->card_caps);
43 mmc_dump_capabilities("host capabilities", mmc->host_caps);
44#endif
Andy Flemingad347bb2008-10-30 16:41:01 -050045 printf("Rd Block Len: %d\n", mmc->read_bl_len);
46
Pantelis Antonioua095bfd2015-01-23 12:12:01 +020047 printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
48 EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
49 EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
50 if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
51 printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
52 printf("\n");
Andy Flemingad347bb2008-10-30 16:41:01 -050053
54 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang316f81a2011-01-04 01:04:19 +000055 puts("Capacity: ");
56 print_size(mmc->capacity, "\n");
Andy Flemingad347bb2008-10-30 16:41:01 -050057
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -060058 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
59 mmc->ddr_mode ? " DDR" : "");
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010060
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +010061#if CONFIG_IS_ENABLED(MMC_WRITE)
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010062 puts("Erase Group Size: ");
63 print_size(((u64)mmc->erase_grp_size) << 9, "\n");
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +010064#endif
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010065
Diego Santa Cruz1f69e222014-12-23 10:50:19 +010066 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010067 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
Diego Santa Cruz2b45f582014-12-23 10:50:23 +010068 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
Marek Vasutdfff8ed2020-06-20 14:28:25 +020069 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
70 u8 wp;
Heinrich Schuchardt0c8ffff2020-03-30 07:24:18 +020071 int ret;
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010072
Jean-Jacques Hiblotba54ab82018-01-04 15:23:36 +010073#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010074 puts("HC WP Group Size: ");
75 print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
Jean-Jacques Hiblotba54ab82018-01-04 15:23:36 +010076#endif
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010077
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010078 puts("User Capacity: ");
Diego Santa Cruz37a50b92014-12-23 10:50:33 +010079 print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
80 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
81 puts(" WRREL\n");
82 else
83 putc('\n');
Diego Santa Cruz2b45f582014-12-23 10:50:23 +010084 if (usr_enh) {
85 puts("User Enhanced Start: ");
86 print_size(mmc->enh_user_start, "\n");
87 puts("User Enhanced Size: ");
88 print_size(mmc->enh_user_size, "\n");
89 }
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010090 puts("Boot Capacity: ");
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010091 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010092 puts("RPMB Capacity: ");
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010093 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010094
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010095 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010096 bool is_enh = has_enh &&
97 (mmc->part_attr & EXT_CSD_ENH_GP(i));
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010098 if (mmc->capacity_gp[i]) {
Diego Santa Cruzd8358432014-12-23 10:50:18 +010099 printf("GP%i Capacity: ", i+1);
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +0100100 print_size(mmc->capacity_gp[i],
Diego Santa Cruz37a50b92014-12-23 10:50:33 +0100101 is_enh ? " ENH" : "");
102 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
103 puts(" WRREL\n");
104 else
105 putc('\n');
Diego Santa Cruz99ca0282014-12-23 10:50:16 +0100106 }
107 }
Heinrich Schuchardt0c8ffff2020-03-30 07:24:18 +0200108 ret = mmc_send_ext_csd(mmc, ext_csd);
109 if (ret)
110 return;
111 wp = ext_csd[EXT_CSD_BOOT_WP_STATUS];
112 for (i = 0; i < 2; ++i) {
113 printf("Boot area %d is ", i);
114 switch (wp & 3) {
115 case 0:
116 printf("not write protected\n");
117 break;
118 case 1:
119 printf("power on protected\n");
120 break;
121 case 2:
122 printf("permanently protected\n");
123 break;
124 default:
125 printf("in reserved protection state\n");
126 break;
127 }
128 wp >>= 2;
129 }
Diego Santa Cruz99ca0282014-12-23 10:50:16 +0100130 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500131}
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530132
133static struct mmc *__init_mmc_device(int dev, bool force_init,
134 enum bus_mode speed_mode)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200135{
136 struct mmc *mmc;
137 mmc = find_mmc_device(dev);
138 if (!mmc) {
139 printf("no mmc device at slot %x\n", dev);
140 return NULL;
141 }
Eric Nelsonbc181662016-03-27 12:00:14 -0700142
Marek Vasutcb5df5d2019-01-03 22:09:43 +0100143 if (!mmc_getcd(mmc))
144 force_init = true;
145
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600146 if (force_init)
147 mmc->has_init = 0;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530148
149 if (IS_ENABLED(CONFIG_MMC_SPEED_MODE_SET))
150 mmc->user_speed_mode = speed_mode;
151
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200152 if (mmc_init(mmc))
153 return NULL;
Marek Vasut829540f2019-01-03 22:09:44 +0100154
155#ifdef CONFIG_BLOCK_CACHE
156 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Simon Glassfada3f92022-09-17 09:00:09 -0600157 blkcache_invalidate(bd->uclass_id, bd->devnum);
Marek Vasut829540f2019-01-03 22:09:44 +0100158#endif
159
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200160 return mmc;
161}
Simon Glassed38aef2020-05-10 11:40:03 -0600162
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530163static struct mmc *init_mmc_device(int dev, bool force_init)
164{
165 return __init_mmc_device(dev, force_init, MMC_MODES_END);
166}
167
Simon Glassed38aef2020-05-10 11:40:03 -0600168static int do_mmcinfo(struct cmd_tbl *cmdtp, int flag, int argc,
169 char *const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500170{
171 struct mmc *mmc;
Andy Flemingad347bb2008-10-30 16:41:01 -0500172
Lei Wend430d7c2011-05-02 16:26:25 +0000173 if (curr_device < 0) {
174 if (get_mmc_num() > 0)
175 curr_device = 0;
176 else {
177 puts("No MMC device available\n");
Pali Rohár332c2302023-03-22 21:06:53 +0100178 return CMD_RET_FAILURE;
Lei Wend430d7c2011-05-02 16:26:25 +0000179 }
180 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500181
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600182 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200183 if (!mmc)
184 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500185
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200186 print_mmcinfo(mmc);
187 return CMD_RET_SUCCESS;
188}
Andy Flemingad347bb2008-10-30 16:41:01 -0500189
Alex Kiernan60e0f612018-05-08 04:43:31 +0000190#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200191static int confirm_key_prog(void)
192{
193 puts("Warning: Programming authentication key can be done only once !\n"
194 " Use this command only if you are sure of what you are doing,\n"
195 "Really perform the key programming? <y/N> ");
196 if (confirm_yesno())
Lei Wend430d7c2011-05-02 16:26:25 +0000197 return 1;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200198
199 puts("Authentication key programming aborted\n");
200 return 0;
201}
Simon Glassed38aef2020-05-10 11:40:03 -0600202
203static int do_mmcrpmb_key(struct cmd_tbl *cmdtp, int flag,
204 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200205{
206 void *key_addr;
207 struct mmc *mmc = find_mmc_device(curr_device);
208
209 if (argc != 2)
210 return CMD_RET_USAGE;
211
Simon Glass3ff49ec2021-07-24 09:03:29 -0600212 key_addr = (void *)hextoul(argv[1], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200213 if (!confirm_key_prog())
214 return CMD_RET_FAILURE;
215 if (mmc_rpmb_set_key(mmc, key_addr)) {
216 printf("ERROR - Key already programmed ?\n");
217 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500218 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200219 return CMD_RET_SUCCESS;
Andy Flemingad347bb2008-10-30 16:41:01 -0500220}
Simon Glassed38aef2020-05-10 11:40:03 -0600221
222static int do_mmcrpmb_read(struct cmd_tbl *cmdtp, int flag,
223 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200224{
225 u16 blk, cnt;
226 void *addr;
227 int n;
228 void *key_addr = NULL;
229 struct mmc *mmc = find_mmc_device(curr_device);
Andy Flemingad347bb2008-10-30 16:41:01 -0500230
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200231 if (argc < 4)
232 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500233
Simon Glass3ff49ec2021-07-24 09:03:29 -0600234 addr = (void *)hextoul(argv[1], NULL);
235 blk = hextoul(argv[2], NULL);
236 cnt = hextoul(argv[3], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200237
238 if (argc == 5)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600239 key_addr = (void *)hextoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200240
241 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
242 curr_device, blk, cnt);
243 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
244
245 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
246 if (n != cnt)
247 return CMD_RET_FAILURE;
248 return CMD_RET_SUCCESS;
249}
Simon Glassed38aef2020-05-10 11:40:03 -0600250
251static int do_mmcrpmb_write(struct cmd_tbl *cmdtp, int flag,
252 int argc, char *const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500253{
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200254 u16 blk, cnt;
255 void *addr;
256 int n;
257 void *key_addr;
258 struct mmc *mmc = find_mmc_device(curr_device);
Lei Wen64b2b1e2011-06-22 17:03:30 +0000259
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200260 if (argc != 5)
Simon Glassa06dfc72011-12-10 08:44:01 +0000261 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500262
Simon Glass3ff49ec2021-07-24 09:03:29 -0600263 addr = (void *)hextoul(argv[1], NULL);
264 blk = hextoul(argv[2], NULL);
265 cnt = hextoul(argv[3], NULL);
266 key_addr = (void *)hextoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200267
268 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
269 curr_device, blk, cnt);
270 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
271
272 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
273 if (n != cnt)
274 return CMD_RET_FAILURE;
275 return CMD_RET_SUCCESS;
276}
Simon Glassed38aef2020-05-10 11:40:03 -0600277
278static int do_mmcrpmb_counter(struct cmd_tbl *cmdtp, int flag,
279 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200280{
281 unsigned long counter;
282 struct mmc *mmc = find_mmc_device(curr_device);
283
284 if (mmc_rpmb_get_counter(mmc, &counter))
285 return CMD_RET_FAILURE;
286 printf("RPMB Write counter= %lx\n", counter);
287 return CMD_RET_SUCCESS;
288}
289
Simon Glassed38aef2020-05-10 11:40:03 -0600290static struct cmd_tbl cmd_rpmb[] = {
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200291 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
292 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
293 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
294 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
295};
296
Simon Glassed38aef2020-05-10 11:40:03 -0600297static int do_mmcrpmb(struct cmd_tbl *cmdtp, int flag,
298 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200299{
Simon Glassed38aef2020-05-10 11:40:03 -0600300 struct cmd_tbl *cp;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200301 struct mmc *mmc;
302 char original_part;
303 int ret;
304
305 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
306
307 /* Drop the rpmb subcommand */
308 argc--;
309 argv++;
310
311 if (cp == NULL || argc > cp->maxargs)
312 return CMD_RET_USAGE;
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100313 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200314 return CMD_RET_SUCCESS;
315
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600316 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200317 if (!mmc)
318 return CMD_RET_FAILURE;
319
320 if (!(mmc->version & MMC_VERSION_MMC)) {
Heinrich Schuchardteeb191c2020-03-29 19:26:57 +0000321 printf("It is not an eMMC device\n");
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200322 return CMD_RET_FAILURE;
Lei Wend430d7c2011-05-02 16:26:25 +0000323 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200324 if (mmc->version < MMC_VERSION_4_41) {
325 printf("RPMB not supported before version 4.41\n");
326 return CMD_RET_FAILURE;
327 }
328 /* Switch to the RPMB partition */
Kever Yangcb5f1932017-06-08 09:20:03 +0800329#ifndef CONFIG_BLK
Marek Vasuta62cf492016-05-04 16:35:25 +0200330 original_part = mmc->block_dev.hwpart;
Kever Yangcb5f1932017-06-08 09:20:03 +0800331#else
332 original_part = mmc_get_blk_desc(mmc)->hwpart;
333#endif
Simon Glassdbfa32c2022-08-11 19:34:59 -0600334 if (blk_select_hwpart_devnum(UCLASS_MMC, curr_device, MMC_PART_RPMB) !=
Simon Glass11f2bb62016-05-01 13:52:29 -0600335 0)
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700336 return CMD_RET_FAILURE;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200337 ret = cp->cmd(cmdtp, flag, argc, argv);
Andy Flemingad347bb2008-10-30 16:41:01 -0500338
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200339 /* Return to original partition */
Simon Glassdbfa32c2022-08-11 19:34:59 -0600340 if (blk_select_hwpart_devnum(UCLASS_MMC, curr_device, original_part) !=
Simon Glass11f2bb62016-05-01 13:52:29 -0600341 0)
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700342 return CMD_RET_FAILURE;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200343 return ret;
344}
345#endif
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530346
Simon Glassed38aef2020-05-10 11:40:03 -0600347static int do_mmc_read(struct cmd_tbl *cmdtp, int flag,
348 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200349{
350 struct mmc *mmc;
351 u32 blk, cnt, n;
352 void *addr;
Stephen Warren18faca72013-04-01 11:50:28 +0000353
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200354 if (argc != 4)
355 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500356
Simon Glass3ff49ec2021-07-24 09:03:29 -0600357 addr = (void *)hextoul(argv[1], NULL);
358 blk = hextoul(argv[2], NULL);
359 cnt = hextoul(argv[3], NULL);
Lei Wen61dba932010-09-13 22:07:28 +0800360
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600361 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200362 if (!mmc)
363 return CMD_RET_FAILURE;
Lei Wen61dba932010-09-13 22:07:28 +0800364
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200365 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
366 curr_device, blk, cnt);
Stephen Warren18faca72013-04-01 11:50:28 +0000367
Simon Glasse5db1152016-05-01 13:52:35 -0600368 n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200369 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Lei Wend430d7c2011-05-02 16:26:25 +0000370
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200371 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
372}
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +0100373
Alex Kiernanc568bcb2018-05-29 15:30:52 +0000374#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar814a9752018-04-06 12:05:24 +0530375static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk,
376 lbaint_t blkcnt, const void *buffer)
377{
378 struct blk_desc *dev_desc = info->priv;
379
380 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
381}
382
383static lbaint_t mmc_sparse_reserve(struct sparse_storage *info,
384 lbaint_t blk, lbaint_t blkcnt)
385{
386 return blkcnt;
387}
388
Simon Glassed38aef2020-05-10 11:40:03 -0600389static int do_mmc_sparse_write(struct cmd_tbl *cmdtp, int flag,
390 int argc, char *const argv[])
Jassi Brar814a9752018-04-06 12:05:24 +0530391{
392 struct sparse_storage sparse;
393 struct blk_desc *dev_desc;
394 struct mmc *mmc;
395 char dest[11];
396 void *addr;
397 u32 blk;
398
399 if (argc != 3)
400 return CMD_RET_USAGE;
401
Simon Glass3ff49ec2021-07-24 09:03:29 -0600402 addr = (void *)hextoul(argv[1], NULL);
403 blk = hextoul(argv[2], NULL);
Jassi Brar814a9752018-04-06 12:05:24 +0530404
405 if (!is_sparse_image(addr)) {
406 printf("Not a sparse image\n");
407 return CMD_RET_FAILURE;
408 }
409
410 mmc = init_mmc_device(curr_device, false);
411 if (!mmc)
412 return CMD_RET_FAILURE;
413
414 printf("\nMMC Sparse write: dev # %d, block # %d ... ",
415 curr_device, blk);
416
417 if (mmc_getwp(mmc) == 1) {
418 printf("Error: card is write protected!\n");
419 return CMD_RET_FAILURE;
420 }
421
422 dev_desc = mmc_get_blk_desc(mmc);
423 sparse.priv = dev_desc;
424 sparse.blksz = 512;
425 sparse.start = blk;
426 sparse.size = dev_desc->lba - blk;
427 sparse.write = mmc_sparse_write;
428 sparse.reserve = mmc_sparse_reserve;
429 sparse.mssg = NULL;
430 sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz);
431
Alex Kiernan27b69de2018-05-29 15:30:40 +0000432 if (write_sparse_image(&sparse, dest, addr, NULL))
Jassi Brar814a9752018-04-06 12:05:24 +0530433 return CMD_RET_FAILURE;
434 else
435 return CMD_RET_SUCCESS;
436}
437#endif
438
Alex Kiernanc568bcb2018-05-29 15:30:52 +0000439#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glassed38aef2020-05-10 11:40:03 -0600440static int do_mmc_write(struct cmd_tbl *cmdtp, int flag,
441 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200442{
443 struct mmc *mmc;
444 u32 blk, cnt, n;
445 void *addr;
Andy Flemingad347bb2008-10-30 16:41:01 -0500446
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200447 if (argc != 4)
448 return CMD_RET_USAGE;
Lei Wend430d7c2011-05-02 16:26:25 +0000449
Simon Glass3ff49ec2021-07-24 09:03:29 -0600450 addr = (void *)hextoul(argv[1], NULL);
451 blk = hextoul(argv[2], NULL);
452 cnt = hextoul(argv[3], NULL);
Lei Wend430d7c2011-05-02 16:26:25 +0000453
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600454 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200455 if (!mmc)
456 return CMD_RET_FAILURE;
Lei Wen31b99802011-05-02 16:26:26 +0000457
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200458 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
459 curr_device, blk, cnt);
Lei Wen31b99802011-05-02 16:26:26 +0000460
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200461 if (mmc_getwp(mmc) == 1) {
462 printf("Error: card is write protected!\n");
463 return CMD_RET_FAILURE;
464 }
Simon Glasse5db1152016-05-01 13:52:35 -0600465 n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200466 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Lei Wend430d7c2011-05-02 16:26:25 +0000467
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200468 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
469}
Simon Glassed38aef2020-05-10 11:40:03 -0600470
471static int do_mmc_erase(struct cmd_tbl *cmdtp, int flag,
472 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200473{
474 struct mmc *mmc;
475 u32 blk, cnt, n;
Tom Rinif8c6f792014-02-05 10:24:21 -0500476
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200477 if (argc != 3)
478 return CMD_RET_USAGE;
Tom Rinif8c6f792014-02-05 10:24:21 -0500479
Simon Glass3ff49ec2021-07-24 09:03:29 -0600480 blk = hextoul(argv[1], NULL);
481 cnt = hextoul(argv[2], NULL);
Amar3ccad532013-04-27 11:43:00 +0530482
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600483 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200484 if (!mmc)
485 return CMD_RET_FAILURE;
Tom Rinif8c6f792014-02-05 10:24:21 -0500486
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200487 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
488 curr_device, blk, cnt);
Tom Rini4cf854c2014-02-05 10:24:22 -0500489
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200490 if (mmc_getwp(mmc) == 1) {
491 printf("Error: card is write protected!\n");
492 return CMD_RET_FAILURE;
493 }
Simon Glasse5db1152016-05-01 13:52:35 -0600494 n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200495 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Tom Rini4cf854c2014-02-05 10:24:22 -0500496
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200497 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
498}
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +0100499#endif
500
Simon Glassed38aef2020-05-10 11:40:03 -0600501static int do_mmc_rescan(struct cmd_tbl *cmdtp, int flag,
502 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200503{
504 struct mmc *mmc;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530505
506 if (argc == 1) {
507 mmc = init_mmc_device(curr_device, true);
508 } else if (argc == 2) {
Heinrich Schuchardt05c74282022-04-25 23:11:06 +0200509 enum bus_mode speed_mode;
510
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530511 speed_mode = (int)dectoul(argv[1], NULL);
512 mmc = __init_mmc_device(curr_device, true, speed_mode);
513 } else {
514 return CMD_RET_USAGE;
515 }
Tom Rini4cf854c2014-02-05 10:24:22 -0500516
Stephen Warren07ec4e72014-05-23 13:24:46 -0600517 if (!mmc)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200518 return CMD_RET_FAILURE;
Tom Rini4cf854c2014-02-05 10:24:22 -0500519
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200520 return CMD_RET_SUCCESS;
521}
Simon Glassed38aef2020-05-10 11:40:03 -0600522
523static int do_mmc_part(struct cmd_tbl *cmdtp, int flag,
524 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200525{
Simon Glasse3394752016-02-29 15:25:34 -0700526 struct blk_desc *mmc_dev;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200527 struct mmc *mmc;
Amar3ccad532013-04-27 11:43:00 +0530528
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600529 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200530 if (!mmc)
531 return CMD_RET_FAILURE;
Amar3ccad532013-04-27 11:43:00 +0530532
Simon Glassfada3f92022-09-17 09:00:09 -0600533 mmc_dev = blk_get_devnum_by_uclass_id(UCLASS_MMC, curr_device);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200534 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
Simon Glassb89a8442016-02-29 15:25:48 -0700535 part_print(mmc_dev);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200536 return CMD_RET_SUCCESS;
537 }
Amar3ccad532013-04-27 11:43:00 +0530538
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200539 puts("get mmc type error!\n");
540 return CMD_RET_FAILURE;
541}
Simon Glassed38aef2020-05-10 11:40:03 -0600542
543static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag,
544 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200545{
Stephen Warren18549802014-05-23 12:48:10 -0600546 int dev, part = 0, ret;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200547 struct mmc *mmc;
Tom Rini35a3ea12014-02-07 14:15:20 -0500548
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200549 if (argc == 1) {
550 dev = curr_device;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530551 mmc = init_mmc_device(dev, true);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200552 } else if (argc == 2) {
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530553 dev = (int)dectoul(argv[1], NULL);
554 mmc = init_mmc_device(dev, true);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200555 } else if (argc == 3) {
Simon Glassff9b9032021-07-24 09:03:30 -0600556 dev = (int)dectoul(argv[1], NULL);
557 part = (int)dectoul(argv[2], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200558 if (part > PART_ACCESS_MASK) {
559 printf("#part_num shouldn't be larger than %d\n",
560 PART_ACCESS_MASK);
561 return CMD_RET_FAILURE;
Tom Rini35a3ea12014-02-07 14:15:20 -0500562 }
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530563 mmc = init_mmc_device(dev, true);
564 } else if (argc == 4) {
Heinrich Schuchardt05c74282022-04-25 23:11:06 +0200565 enum bus_mode speed_mode;
566
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530567 dev = (int)dectoul(argv[1], NULL);
568 part = (int)dectoul(argv[2], NULL);
569 if (part > PART_ACCESS_MASK) {
570 printf("#part_num shouldn't be larger than %d\n",
571 PART_ACCESS_MASK);
572 return CMD_RET_FAILURE;
573 }
574 speed_mode = (int)dectoul(argv[3], NULL);
575 mmc = __init_mmc_device(dev, true, speed_mode);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200576 } else {
577 return CMD_RET_USAGE;
578 }
Tom Rini35a3ea12014-02-07 14:15:20 -0500579
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200580 if (!mmc)
581 return CMD_RET_FAILURE;
Tom Rini35a3ea12014-02-07 14:15:20 -0500582
Simon Glassdbfa32c2022-08-11 19:34:59 -0600583 ret = blk_select_hwpart_devnum(UCLASS_MMC, dev, part);
Stephen Warren18549802014-05-23 12:48:10 -0600584 printf("switch to partitions #%d, %s\n",
585 part, (!ret) ? "OK" : "ERROR");
586 if (ret)
587 return 1;
588
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200589 curr_device = dev;
590 if (mmc->part_config == MMCPART_NOAVAILABLE)
591 printf("mmc%d is current device\n", curr_device);
592 else
593 printf("mmc%d(part %d) is current device\n",
Simon Glasse5db1152016-05-01 13:52:35 -0600594 curr_device, mmc_get_blk_desc(mmc)->hwpart);
Tom Rini35a3ea12014-02-07 14:15:20 -0500595
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200596 return CMD_RET_SUCCESS;
597}
Simon Glassed38aef2020-05-10 11:40:03 -0600598
599static int do_mmc_list(struct cmd_tbl *cmdtp, int flag,
600 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200601{
602 print_mmc_devices('\n');
603 return CMD_RET_SUCCESS;
604}
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100605
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +0100606#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Marek Vasut94e39952021-09-15 11:43:13 +0200607static void parse_hwpart_user_enh_size(struct mmc *mmc,
608 struct mmc_hwpart_conf *pconf,
609 char *argv)
610{
Marek Vasut30d29662022-01-17 22:54:29 +0100611 int i, ret;
Marek Vasut94e39952021-09-15 11:43:13 +0200612
613 pconf->user.enh_size = 0;
614
615 if (!strcmp(argv, "-")) { /* The rest of eMMC */
616 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
617 ret = mmc_send_ext_csd(mmc, ext_csd);
618 if (ret)
619 return;
Marek Vasut30d29662022-01-17 22:54:29 +0100620 /* The enh_size value is in 512B block units */
Marek Vasut94e39952021-09-15 11:43:13 +0200621 pconf->user.enh_size =
622 ((ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 2] << 16) +
623 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 1] << 8) +
624 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT]) * 1024 *
625 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
626 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
627 pconf->user.enh_size -= pconf->user.enh_start;
Marek Vasut30d29662022-01-17 22:54:29 +0100628 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
629 /*
630 * If the eMMC already has GP partitions set,
631 * subtract their size from the maximum USER
632 * partition size.
633 *
634 * Else, if the command was used to configure new
635 * GP partitions, subtract their size from maximum
636 * USER partition size.
637 */
638 if (mmc->capacity_gp[i]) {
639 /* The capacity_gp is in 1B units */
640 pconf->user.enh_size -= mmc->capacity_gp[i] >> 9;
641 } else if (pconf->gp_part[i].size) {
642 /* The gp_part[].size is in 512B units */
643 pconf->user.enh_size -= pconf->gp_part[i].size;
644 }
645 }
Marek Vasut94e39952021-09-15 11:43:13 +0200646 } else {
647 pconf->user.enh_size = dectoul(argv, NULL);
648 }
649}
650
651static int parse_hwpart_user(struct mmc *mmc, struct mmc_hwpart_conf *pconf,
Simon Glassed38aef2020-05-10 11:40:03 -0600652 int argc, char *const argv[])
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100653{
654 int i = 0;
655
656 memset(&pconf->user, 0, sizeof(pconf->user));
657
658 while (i < argc) {
659 if (!strcmp(argv[i], "enh")) {
660 if (i + 2 >= argc)
661 return -1;
662 pconf->user.enh_start =
Simon Glassff9b9032021-07-24 09:03:30 -0600663 dectoul(argv[i + 1], NULL);
Marek Vasut94e39952021-09-15 11:43:13 +0200664 parse_hwpart_user_enh_size(mmc, pconf, argv[i + 2]);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100665 i += 3;
666 } else if (!strcmp(argv[i], "wrrel")) {
667 if (i + 1 >= argc)
668 return -1;
669 pconf->user.wr_rel_change = 1;
670 if (!strcmp(argv[i+1], "on"))
671 pconf->user.wr_rel_set = 1;
672 else if (!strcmp(argv[i+1], "off"))
673 pconf->user.wr_rel_set = 0;
674 else
675 return -1;
676 i += 2;
677 } else {
678 break;
679 }
680 }
681 return i;
682}
683
684static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
Simon Glassed38aef2020-05-10 11:40:03 -0600685 int argc, char *const argv[])
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100686{
687 int i;
688
689 memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
690
691 if (1 >= argc)
692 return -1;
Simon Glassff9b9032021-07-24 09:03:30 -0600693 pconf->gp_part[pidx].size = dectoul(argv[0], NULL);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100694
695 i = 1;
696 while (i < argc) {
697 if (!strcmp(argv[i], "enh")) {
698 pconf->gp_part[pidx].enhanced = 1;
699 i += 1;
700 } else if (!strcmp(argv[i], "wrrel")) {
701 if (i + 1 >= argc)
702 return -1;
703 pconf->gp_part[pidx].wr_rel_change = 1;
704 if (!strcmp(argv[i+1], "on"))
705 pconf->gp_part[pidx].wr_rel_set = 1;
706 else if (!strcmp(argv[i+1], "off"))
707 pconf->gp_part[pidx].wr_rel_set = 0;
708 else
709 return -1;
710 i += 2;
711 } else {
712 break;
713 }
714 }
715 return i;
716}
717
Simon Glassed38aef2020-05-10 11:40:03 -0600718static int do_mmc_hwpartition(struct cmd_tbl *cmdtp, int flag,
719 int argc, char *const argv[])
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100720{
721 struct mmc *mmc;
722 struct mmc_hwpart_conf pconf = { };
723 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100724 int i, r, pidx;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100725
726 mmc = init_mmc_device(curr_device, false);
727 if (!mmc)
728 return CMD_RET_FAILURE;
729
Jaehoon Chungffbba622021-09-24 09:23:34 +0900730 if (IS_SD(mmc)) {
731 puts("SD doesn't support partitioning\n");
732 return CMD_RET_FAILURE;
733 }
734
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100735 if (argc < 1)
736 return CMD_RET_USAGE;
737 i = 1;
738 while (i < argc) {
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100739 if (!strcmp(argv[i], "user")) {
740 i++;
Marek Vasut94e39952021-09-15 11:43:13 +0200741 r = parse_hwpart_user(mmc, &pconf, argc - i, &argv[i]);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100742 if (r < 0)
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100743 return CMD_RET_USAGE;
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100744 i += r;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100745 } else if (!strncmp(argv[i], "gp", 2) &&
746 strlen(argv[i]) == 3 &&
747 argv[i][2] >= '1' && argv[i][2] <= '4') {
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100748 pidx = argv[i][2] - '1';
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100749 i++;
750 r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
751 if (r < 0)
752 return CMD_RET_USAGE;
753 i += r;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100754 } else if (!strcmp(argv[i], "check")) {
755 mode = MMC_HWPART_CONF_CHECK;
756 i++;
757 } else if (!strcmp(argv[i], "set")) {
758 mode = MMC_HWPART_CONF_SET;
759 i++;
760 } else if (!strcmp(argv[i], "complete")) {
761 mode = MMC_HWPART_CONF_COMPLETE;
762 i++;
763 } else {
764 return CMD_RET_USAGE;
765 }
766 }
767
768 puts("Partition configuration:\n");
769 if (pconf.user.enh_size) {
770 puts("\tUser Enhanced Start: ");
771 print_size(((u64)pconf.user.enh_start) << 9, "\n");
772 puts("\tUser Enhanced Size: ");
773 print_size(((u64)pconf.user.enh_size) << 9, "\n");
774 } else {
775 puts("\tNo enhanced user data area\n");
776 }
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100777 if (pconf.user.wr_rel_change)
778 printf("\tUser partition write reliability: %s\n",
779 pconf.user.wr_rel_set ? "on" : "off");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100780 for (pidx = 0; pidx < 4; pidx++) {
781 if (pconf.gp_part[pidx].size) {
782 printf("\tGP%i Capacity: ", pidx+1);
783 print_size(((u64)pconf.gp_part[pidx].size) << 9,
784 pconf.gp_part[pidx].enhanced ?
785 " ENH\n" : "\n");
786 } else {
787 printf("\tNo GP%i partition\n", pidx+1);
788 }
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100789 if (pconf.gp_part[pidx].wr_rel_change)
790 printf("\tGP%i write reliability: %s\n", pidx+1,
791 pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100792 }
793
794 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
795 if (mode == MMC_HWPART_CONF_COMPLETE)
796 puts("Partitioning successful, "
797 "power-cycle to make effective\n");
798 return CMD_RET_SUCCESS;
799 } else {
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100800 puts("Failed!\n");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100801 return CMD_RET_FAILURE;
802 }
803}
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +0100804#endif
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100805
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200806#ifdef CONFIG_SUPPORT_EMMC_BOOT
Simon Glassed38aef2020-05-10 11:40:03 -0600807static int do_mmc_bootbus(struct cmd_tbl *cmdtp, int flag,
808 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200809{
810 int dev;
811 struct mmc *mmc;
812 u8 width, reset, mode;
813
814 if (argc != 5)
815 return CMD_RET_USAGE;
Simon Glassff9b9032021-07-24 09:03:30 -0600816 dev = dectoul(argv[1], NULL);
817 width = dectoul(argv[2], NULL);
818 reset = dectoul(argv[3], NULL);
819 mode = dectoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200820
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600821 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200822 if (!mmc)
823 return CMD_RET_FAILURE;
824
825 if (IS_SD(mmc)) {
826 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
827 return CMD_RET_FAILURE;
Amar3ccad532013-04-27 11:43:00 +0530828 }
Markus Niebel03951412013-12-16 13:40:46 +0100829
Jaehoon Chung7483dd32021-02-26 18:38:20 +0900830 /*
831 * BOOT_BUS_CONDITIONS[177]
832 * BOOT_MODE[4:3]
833 * 0x0 : Use SDR + Backward compatible timing in boot operation
834 * 0x1 : Use SDR + High Speed Timing in boot operation mode
835 * 0x2 : Use DDR in boot operation
836 * RESET_BOOT_BUS_CONDITIONS
837 * 0x0 : Reset bus width to x1, SDR, Backward compatible
838 * 0x1 : Retain BOOT_BUS_WIDTH and BOOT_MODE
839 * BOOT_BUS_WIDTH
840 * 0x0 : x1(sdr) or x4 (ddr) buswidth
841 * 0x1 : x4(sdr/ddr) buswith
842 * 0x2 : x8(sdr/ddr) buswith
843 *
844 */
845 if (width >= 0x3) {
846 printf("boot_bus_width %d is invalid\n", width);
847 return CMD_RET_FAILURE;
848 }
849
850 if (reset >= 0x2) {
851 printf("reset_boot_bus_width %d is invalid\n", reset);
852 return CMD_RET_FAILURE;
853 }
854
855 if (mode >= 0x3) {
856 printf("reset_boot_bus_width %d is invalid\n", mode);
857 return CMD_RET_FAILURE;
858 }
859
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200860 /* acknowledge to be sent during boot operation */
Jaehoon Chung7483dd32021-02-26 18:38:20 +0900861 if (mmc_set_boot_bus_width(mmc, width, reset, mode)) {
862 puts("BOOT_BUS_WIDTH is failed to change.\n");
863 return CMD_RET_FAILURE;
864 }
865
866 printf("Set to BOOT_BUS_WIDTH = 0x%x, RESET = 0x%x, BOOT_MODE = 0x%x\n",
867 width, reset, mode);
868 return CMD_RET_SUCCESS;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200869}
Simon Glassed38aef2020-05-10 11:40:03 -0600870
871static int do_mmc_boot_resize(struct cmd_tbl *cmdtp, int flag,
872 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200873{
874 int dev;
875 struct mmc *mmc;
876 u32 bootsize, rpmbsize;
Markus Niebel03951412013-12-16 13:40:46 +0100877
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200878 if (argc != 4)
879 return CMD_RET_USAGE;
Simon Glassff9b9032021-07-24 09:03:30 -0600880 dev = dectoul(argv[1], NULL);
881 bootsize = dectoul(argv[2], NULL);
882 rpmbsize = dectoul(argv[3], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200883
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600884 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200885 if (!mmc)
886 return CMD_RET_FAILURE;
887
888 if (IS_SD(mmc)) {
Heinrich Schuchardteeb191c2020-03-29 19:26:57 +0000889 printf("It is not an eMMC device\n");
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200890 return CMD_RET_FAILURE;
Markus Niebel03951412013-12-16 13:40:46 +0100891 }
892
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200893 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
894 printf("EMMC boot partition Size change Failed.\n");
895 return CMD_RET_FAILURE;
896 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500897
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200898 printf("EMMC boot partition Size %d MB\n", bootsize);
899 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
900 return CMD_RET_SUCCESS;
901}
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200902
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200903static int mmc_partconf_print(struct mmc *mmc, const char *varname)
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200904{
905 u8 ack, access, part;
906
907 if (mmc->part_config == MMCPART_NOAVAILABLE) {
908 printf("No part_config info for ver. 0x%x\n", mmc->version);
909 return CMD_RET_FAILURE;
910 }
911
912 access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
913 ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
914 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
915
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200916 if(varname)
917 env_set_hex(varname, part);
918
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200919 printf("EXT_CSD[179], PARTITION_CONFIG:\n"
920 "BOOT_ACK: 0x%x\n"
921 "BOOT_PARTITION_ENABLE: 0x%x\n"
922 "PARTITION_ACCESS: 0x%x\n", ack, part, access);
923
924 return CMD_RET_SUCCESS;
925}
926
Simon Glassed38aef2020-05-10 11:40:03 -0600927static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag,
928 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200929{
Pali Rohár332c2302023-03-22 21:06:53 +0100930 int ret, dev;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200931 struct mmc *mmc;
932 u8 ack, part_num, access;
Lei Wenea526762011-06-22 17:03:31 +0000933
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200934 if (argc != 2 && argc != 3 && argc != 5)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200935 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500936
Simon Glassff9b9032021-07-24 09:03:30 -0600937 dev = dectoul(argv[1], NULL);
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530938
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600939 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200940 if (!mmc)
941 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500942
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200943 if (IS_SD(mmc)) {
944 puts("PARTITION_CONFIG only exists on eMMC\n");
945 return CMD_RET_FAILURE;
946 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500947
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200948 if (argc == 2 || argc == 3)
Simon Glass793a98e2023-11-18 14:05:20 -0700949 return mmc_partconf_print(mmc, cmd_arg2(argc, argv));
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200950
Simon Glassff9b9032021-07-24 09:03:30 -0600951 ack = dectoul(argv[2], NULL);
952 part_num = dectoul(argv[3], NULL);
953 access = dectoul(argv[4], NULL);
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200954
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200955 /* acknowledge to be sent during boot operation */
Pali Rohár332c2302023-03-22 21:06:53 +0100956 ret = mmc_set_part_conf(mmc, ack, part_num, access);
957 if (ret != 0)
958 return CMD_RET_FAILURE;
959
960 return CMD_RET_SUCCESS;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200961}
Simon Glassed38aef2020-05-10 11:40:03 -0600962
963static int do_mmc_rst_func(struct cmd_tbl *cmdtp, int flag,
964 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200965{
Pali Rohár332c2302023-03-22 21:06:53 +0100966 int ret, dev;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200967 struct mmc *mmc;
968 u8 enable;
Nikita Kiryanov020f2612012-12-03 02:19:46 +0000969
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200970 /*
971 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
972 * The only valid values are 0x0, 0x1 and 0x2 and writing
973 * a value of 0x1 or 0x2 sets the value permanently.
974 */
975 if (argc != 3)
976 return CMD_RET_USAGE;
977
Simon Glassff9b9032021-07-24 09:03:30 -0600978 dev = dectoul(argv[1], NULL);
979 enable = dectoul(argv[2], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200980
Peng Fanb1e455a2015-11-25 17:16:21 +0800981 if (enable > 2) {
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200982 puts("Invalid RST_n_ENABLE value\n");
983 return CMD_RET_USAGE;
984 }
985
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600986 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200987 if (!mmc)
988 return CMD_RET_FAILURE;
989
990 if (IS_SD(mmc)) {
991 puts("RST_n_FUNCTION only exists on eMMC\n");
992 return CMD_RET_FAILURE;
993 }
994
Pali Rohár332c2302023-03-22 21:06:53 +0100995 ret = mmc_set_rst_n_function(mmc, enable);
996 if (ret != 0)
997 return CMD_RET_FAILURE;
998
999 return CMD_RET_SUCCESS;
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001000}
1001#endif
Simon Glassed38aef2020-05-10 11:40:03 -06001002static int do_mmc_setdsr(struct cmd_tbl *cmdtp, int flag,
1003 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001004{
1005 struct mmc *mmc;
1006 u32 val;
1007 int ret;
Andy Flemingad347bb2008-10-30 16:41:01 -05001008
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001009 if (argc != 2)
1010 return CMD_RET_USAGE;
Simon Glass3ff49ec2021-07-24 09:03:29 -06001011 val = hextoul(argv[1], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001012
1013 mmc = find_mmc_device(curr_device);
1014 if (!mmc) {
1015 printf("no mmc device at slot %x\n", curr_device);
1016 return CMD_RET_FAILURE;
1017 }
1018 ret = mmc_set_dsr(mmc, val);
1019 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
1020 if (!ret) {
1021 mmc->has_init = 0;
1022 if (mmc_init(mmc))
1023 return CMD_RET_FAILURE;
1024 else
1025 return CMD_RET_SUCCESS;
Andy Flemingad347bb2008-10-30 16:41:01 -05001026 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001027 return ret;
1028}
Lei Wend430d7c2011-05-02 16:26:25 +00001029
Tomas Melinc17dae52016-11-25 11:01:03 +02001030#ifdef CONFIG_CMD_BKOPS_ENABLE
Marek Vasutefdeed62023-01-05 15:19:08 +01001031static int mmc_bkops_common(char *device, bool autobkops, bool enable)
Tomas Melinc17dae52016-11-25 11:01:03 +02001032{
Tomas Melinc17dae52016-11-25 11:01:03 +02001033 struct mmc *mmc;
Marek Vasutefdeed62023-01-05 15:19:08 +01001034 int dev;
Tomas Melinc17dae52016-11-25 11:01:03 +02001035
Marek Vasutefdeed62023-01-05 15:19:08 +01001036 dev = dectoul(device, NULL);
Tomas Melinc17dae52016-11-25 11:01:03 +02001037
1038 mmc = init_mmc_device(dev, false);
1039 if (!mmc)
1040 return CMD_RET_FAILURE;
1041
1042 if (IS_SD(mmc)) {
1043 puts("BKOPS_EN only exists on eMMC\n");
1044 return CMD_RET_FAILURE;
1045 }
1046
Marek Vasutefdeed62023-01-05 15:19:08 +01001047 return mmc_set_bkops_enable(mmc, autobkops, enable);
1048}
1049
1050static int do_mmc_bkops(struct cmd_tbl *cmdtp, int flag,
1051 int argc, char * const argv[])
1052{
1053 bool autobkops, enable;
1054
1055 if (argc != 4)
1056 return CMD_RET_USAGE;
1057
1058 if (!strcmp(argv[2], "manual"))
1059 autobkops = false;
1060 else if (!strcmp(argv[2], "auto"))
1061 autobkops = true;
1062 else
1063 return CMD_RET_FAILURE;
1064
1065 if (!strcmp(argv[3], "disable"))
1066 enable = false;
1067 else if (!strcmp(argv[3], "enable"))
1068 enable = true;
1069 else
1070 return CMD_RET_FAILURE;
1071
1072 return mmc_bkops_common(argv[1], autobkops, enable);
1073}
1074
1075static int do_mmc_bkops_enable(struct cmd_tbl *cmdtp, int flag,
1076 int argc, char * const argv[])
1077{
1078 if (argc != 2)
1079 return CMD_RET_USAGE;
1080
1081 return mmc_bkops_common(argv[1], false, true);
Tomas Melinc17dae52016-11-25 11:01:03 +02001082}
1083#endif
1084
Simon Glassed38aef2020-05-10 11:40:03 -06001085static int do_mmc_boot_wp(struct cmd_tbl *cmdtp, int flag,
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +02001086 int argc, char * const argv[])
1087{
1088 int err;
1089 struct mmc *mmc;
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001090 int part;
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +02001091
1092 mmc = init_mmc_device(curr_device, false);
1093 if (!mmc)
1094 return CMD_RET_FAILURE;
1095 if (IS_SD(mmc)) {
1096 printf("It is not an eMMC device\n");
1097 return CMD_RET_FAILURE;
1098 }
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001099
1100 if (argc == 2) {
1101 part = dectoul(argv[1], NULL);
1102 err = mmc_boot_wp_single_partition(mmc, part);
1103 } else {
1104 err = mmc_boot_wp(mmc);
1105 }
1106
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +02001107 if (err)
1108 return CMD_RET_FAILURE;
1109 printf("boot areas protected\n");
1110 return CMD_RET_SUCCESS;
1111}
1112
Marek Vasut1f692e32023-10-31 13:20:17 +01001113#if CONFIG_IS_ENABLED(CMD_MMC_REG)
1114static int do_mmc_reg(struct cmd_tbl *cmdtp, int flag,
1115 int argc, char *const argv[])
1116{
1117 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1118 struct mmc *mmc;
1119 int i, ret;
1120 u32 off;
1121
1122 if (argc < 3 || argc > 5)
1123 return CMD_RET_USAGE;
1124
1125 mmc = find_mmc_device(curr_device);
1126 if (!mmc) {
1127 printf("no mmc device at slot %x\n", curr_device);
1128 return CMD_RET_FAILURE;
1129 }
1130
1131 if (IS_SD(mmc)) {
1132 printf("SD registers are not supported\n");
1133 return CMD_RET_FAILURE;
1134 }
1135
1136 off = simple_strtoul(argv[3], NULL, 10);
1137 if (!strcmp(argv[2], "cid")) {
1138 if (off > 3)
1139 return CMD_RET_USAGE;
1140 printf("CID[%i]: 0x%08x\n", off, mmc->cid[off]);
1141 if (argv[4])
1142 env_set_hex(argv[4], mmc->cid[off]);
1143 return CMD_RET_SUCCESS;
1144 }
1145 if (!strcmp(argv[2], "csd")) {
1146 if (off > 3)
1147 return CMD_RET_USAGE;
1148 printf("CSD[%i]: 0x%08x\n", off, mmc->csd[off]);
1149 if (argv[4])
1150 env_set_hex(argv[4], mmc->csd[off]);
1151 return CMD_RET_SUCCESS;
1152 }
1153 if (!strcmp(argv[2], "dsr")) {
1154 printf("DSR: 0x%08x\n", mmc->dsr);
1155 if (argv[4])
1156 env_set_hex(argv[4], mmc->dsr);
1157 return CMD_RET_SUCCESS;
1158 }
1159 if (!strcmp(argv[2], "ocr")) {
1160 printf("OCR: 0x%08x\n", mmc->ocr);
1161 if (argv[4])
1162 env_set_hex(argv[4], mmc->ocr);
1163 return CMD_RET_SUCCESS;
1164 }
1165 if (!strcmp(argv[2], "rca")) {
1166 printf("RCA: 0x%08x\n", mmc->rca);
1167 if (argv[4])
1168 env_set_hex(argv[4], mmc->rca);
1169 return CMD_RET_SUCCESS;
1170 }
1171 if (!strcmp(argv[2], "extcsd") &&
1172 mmc->version >= MMC_VERSION_4_41) {
1173 ret = mmc_send_ext_csd(mmc, ext_csd);
1174 if (ret)
1175 return CMD_RET_FAILURE;
1176 if (!strcmp(argv[3], "all")) {
1177 /* Dump the entire register */
1178 printf("EXT_CSD:");
1179 for (i = 0; i < MMC_MAX_BLOCK_LEN; i++) {
1180 if (!(i % 10))
1181 printf("\n%03i: ", i);
1182 printf(" %02x", ext_csd[i]);
1183 }
1184 printf("\n");
1185 return CMD_RET_SUCCESS;
1186 }
1187 off = simple_strtoul(argv[3], NULL, 10);
1188 if (off > 512)
1189 return CMD_RET_USAGE;
1190 printf("EXT_CSD[%i]: 0x%02x\n", off, ext_csd[off]);
1191 if (argv[4])
1192 env_set_hex(argv[4], ext_csd[off]);
1193 return CMD_RET_SUCCESS;
1194 }
1195
1196 return CMD_RET_FAILURE;
1197}
1198#endif
1199
Simon Glassed38aef2020-05-10 11:40:03 -06001200static struct cmd_tbl cmd_mmc[] = {
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001201 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
1202 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001203 U_BOOT_CMD_MKENT(wp, 2, 0, do_mmc_boot_wp, "", ""),
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01001204#if CONFIG_IS_ENABLED(MMC_WRITE)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001205 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
1206 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01001207#endif
Alex Kiernanc568bcb2018-05-29 15:30:52 +00001208#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
1209 U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
1210#endif
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301211 U_BOOT_CMD_MKENT(rescan, 2, 1, do_mmc_rescan, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001212 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301213 U_BOOT_CMD_MKENT(dev, 4, 0, do_mmc_dev, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001214 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +01001215#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001216 U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +01001217#endif
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001218#ifdef CONFIG_SUPPORT_EMMC_BOOT
1219 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehd49c4aa2014-09-25 23:00:16 +08001220 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001221 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
1222 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
1223#endif
Alex Kiernan60e0f612018-05-08 04:43:31 +00001224#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001225 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
1226#endif
1227 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
Tomas Melinc17dae52016-11-25 11:01:03 +02001228#ifdef CONFIG_CMD_BKOPS_ENABLE
1229 U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
Marek Vasutefdeed62023-01-05 15:19:08 +01001230 U_BOOT_CMD_MKENT(bkops, 4, 0, do_mmc_bkops, "", ""),
Tomas Melinc17dae52016-11-25 11:01:03 +02001231#endif
Marek Vasut1f692e32023-10-31 13:20:17 +01001232#if CONFIG_IS_ENABLED(CMD_MMC_REG)
1233 U_BOOT_CMD_MKENT(reg, 5, 0, do_mmc_reg, "", ""),
1234#endif
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001235};
1236
Simon Glassed38aef2020-05-10 11:40:03 -06001237static int do_mmcops(struct cmd_tbl *cmdtp, int flag, int argc,
1238 char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001239{
Simon Glassed38aef2020-05-10 11:40:03 -06001240 struct cmd_tbl *cp;
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001241
1242 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
1243
1244 /* Drop the mmc command */
1245 argc--;
1246 argv++;
1247
1248 if (cp == NULL || argc > cp->maxargs)
1249 return CMD_RET_USAGE;
Boris Brezillonc71dfd12018-12-03 22:54:20 +01001250 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001251 return CMD_RET_SUCCESS;
1252
1253 if (curr_device < 0) {
1254 if (get_mmc_num() > 0) {
1255 curr_device = 0;
1256 } else {
1257 puts("No MMC device available\n");
1258 return CMD_RET_FAILURE;
1259 }
1260 }
1261 return cp->cmd(cmdtp, flag, argc, argv);
Andy Flemingad347bb2008-10-30 16:41:01 -05001262}
1263
1264U_BOOT_CMD(
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001265 mmc, 29, 1, do_mmcops,
Mike Frysinger27e1e622009-03-23 22:27:34 -04001266 "MMC sub system",
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001267 "info - display info of the current MMC device\n"
1268 "mmc read addr blk# cnt\n"
Lei Wend430d7c2011-05-02 16:26:25 +00001269 "mmc write addr blk# cnt\n"
Alex Kiernanc568bcb2018-05-29 15:30:52 +00001270#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar814a9752018-04-06 12:05:24 +05301271 "mmc swrite addr blk#\n"
1272#endif
Lei Wenea526762011-06-22 17:03:31 +00001273 "mmc erase blk# cnt\n"
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301274 "mmc rescan [mode]\n"
Lei Wend430d7c2011-05-02 16:26:25 +00001275 "mmc part - lists available partition on current mmc device\n"
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301276 "mmc dev [dev] [part] [mode] - show or set current mmc device [partition] and set mode\n"
1277 " - the required speed mode is passed as the index from the following list\n"
1278 " [MMC_LEGACY, MMC_HS, SD_HS, MMC_HS_52, MMC_DDR_52, UHS_SDR12, UHS_SDR25,\n"
1279 " UHS_SDR50, UHS_DDR50, UHS_SDR104, MMC_HS_200, MMC_HS_400, MMC_HS_400_ES]\n"
Amar3ccad532013-04-27 11:43:00 +05301280 "mmc list - lists available devices\n"
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001281 "mmc wp [PART] - power on write protect boot partitions\n"
1282 " arguments:\n"
1283 " PART - [0|1]\n"
1284 " : 0 - first boot partition, 1 - second boot partition\n"
1285 " if not assigned, write protect all boot partitions\n"
Alex Kiernan72d80462018-06-11 16:20:09 +00001286#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Jaehoon Chung94a47572021-02-26 16:07:18 +09001287 "mmc hwpartition <USER> <GP> <MODE> - does hardware partitioning\n"
Diego Santa Cruz420fc162014-12-23 10:50:30 +01001288 " arguments (sizes in 512-byte blocks):\n"
Jaehoon Chung94a47572021-02-26 16:07:18 +09001289 " USER - <user> <enh> <start> <cnt> <wrrel> <{on|off}>\n"
1290 " : sets user data area attributes\n"
1291 " GP - <{gp1|gp2|gp3|gp4}> <cnt> <enh> <wrrel> <{on|off}>\n"
1292 " : general purpose partition\n"
1293 " MODE - <{check|set|complete}>\n"
1294 " : mode, complete set partitioning completed\n"
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001295 " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
1296 " Power cycling is required to initialize partitions after set to complete.\n"
Alex Kiernan72d80462018-06-11 16:20:09 +00001297#endif
Amar3ccad532013-04-27 11:43:00 +05301298#ifdef CONFIG_SUPPORT_EMMC_BOOT
Jaehoon Chung8460b912020-12-16 07:24:50 +09001299 "mmc bootbus <dev> <boot_bus_width> <reset_boot_bus_width> <boot_mode>\n"
Tom Rini4cf854c2014-02-05 10:24:22 -05001300 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rini79919462014-02-05 10:24:20 -05001301 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
1302 " - Change sizes of boot and RPMB partitions of specified device\n"
Reuben Dowle55a81ad2021-05-14 10:15:43 +12001303 "mmc partconf <dev> [[varname] | [<boot_ack> <boot_partition> <partition_access>]]\n"
Angelo Dureghellof54f7532017-08-01 14:27:10 +02001304 " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
Reuben Dowle55a81ad2021-05-14 10:15:43 +12001305 " If showing the bits, optionally store the boot_partition field into varname\n"
Jaehoon Chung8460b912020-12-16 07:24:50 +09001306 "mmc rst-function <dev> <value>\n"
Tom Rini35a3ea12014-02-07 14:15:20 -05001307 " - Change the RST_n_FUNCTION field of the specified device\n"
1308 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behmedf8852a2009-02-18 19:59:39 +01001309#endif
Alex Kiernan60e0f612018-05-08 04:43:31 +00001310#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001311 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
1312 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
1313 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
1314 "mmc rpmb counter - read the value of the write counter\n"
1315#endif
1316 "mmc setdsr <value> - set DSR register value\n"
Tomas Melinc17dae52016-11-25 11:01:03 +02001317#ifdef CONFIG_CMD_BKOPS_ENABLE
1318 "mmc bkops-enable <dev> - enable background operations handshake on device\n"
1319 " WARNING: This is a write-once setting.\n"
Marek Vasutefdeed62023-01-05 15:19:08 +01001320 "mmc bkops <dev> [auto|manual] [enable|disable]\n"
1321 " - configure background operations handshake on device\n"
Tomas Melinc17dae52016-11-25 11:01:03 +02001322#endif
Marek Vasut1f692e32023-10-31 13:20:17 +01001323#if CONFIG_IS_ENABLED(CMD_MMC_REG)
1324 "mmc reg read <reg> <offset> [env] - read card register <reg> offset <offset>\n"
1325 " (optionally into [env] variable)\n"
1326 " - reg: cid/csd/dsr/ocr/rca/extcsd\n"
1327 " - offset: for cid/csd [0..3], for extcsd [0..511,all]\n"
1328#endif
Amar3ccad532013-04-27 11:43:00 +05301329 );
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001330
1331/* Old command kept for compatibility. Same as 'mmc info' */
1332U_BOOT_CMD(
1333 mmcinfo, 1, 0, do_mmcinfo,
1334 "display MMC info",
1335 "- display info of the current MMC device"
1336);