blob: 9a841c25d3d12f0c997e727d86d9fb54ae8c08fb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk7a428cc2003-06-15 22:40:42 +00002/*
3 * (C) Copyright 2003
4 * Kyle Harris, kharris@nexus-tech.net
wdenk7a428cc2003-06-15 22:40:42 +00005 */
6
Simon Glass655306c2020-05-10 11:39:58 -06007#include <blk.h>
wdenk7a428cc2003-06-15 22:40:42 +00008#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -07009#include <console.h>
Simon Glass1ab16922022-07-31 12:28:48 -060010#include <display_options.h>
Marek Vasutdfff8ed2020-06-20 14:28:25 +020011#include <memalign.h>
wdenk7a428cc2003-06-15 22:40:42 +000012#include <mmc.h>
Simon Glass655306c2020-05-10 11:39:58 -060013#include <part.h>
Jassi Brar814a9752018-04-06 12:05:24 +053014#include <sparse_format.h>
15#include <image-sparse.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060016#include <vsprintf.h>
Tim Harvey728cbde2024-05-31 08:36:34 -070017#include <linux/ctype.h>
wdenk7a428cc2003-06-15 22:40:42 +000018
Mike Frysinger0289fe62009-06-14 21:35:21 -040019static int curr_device = -1;
Andy Flemingad347bb2008-10-30 16:41:01 -050020
21static void print_mmcinfo(struct mmc *mmc)
22{
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010023 int i;
24
Pantelis Antoniou2c850462014-03-11 19:34:20 +020025 printf("Device: %s\n", mmc->cfg->name);
Andy Flemingad347bb2008-10-30 16:41:01 -050026 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
Max Merchel1414ada2022-02-10 10:16:39 +010027 if (IS_SD(mmc)) {
28 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
29 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
30 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
31 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
32 } else {
33 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xff);
34 printf("Name: %c%c%c%c%c%c \n", mmc->cid[0] & 0xff,
35 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
36 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
37 (mmc->cid[2] >> 24));
38 }
Andy Flemingad347bb2008-10-30 16:41:01 -050039
Jean-Jacques Hiblotf62caf92017-09-21 16:29:56 +020040 printf("Bus Speed: %d\n", mmc->clock);
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +010041#if CONFIG_IS_ENABLED(MMC_VERBOSE)
Marek Vasutd1e63462019-03-18 04:49:21 +010042 printf("Mode: %s\n", mmc_mode_name(mmc->selected_mode));
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +010043 mmc_dump_capabilities("card capabilities", mmc->card_caps);
44 mmc_dump_capabilities("host capabilities", mmc->host_caps);
45#endif
Andy Flemingad347bb2008-10-30 16:41:01 -050046 printf("Rd Block Len: %d\n", mmc->read_bl_len);
47
Pantelis Antonioua095bfd2015-01-23 12:12:01 +020048 printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
49 EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
50 EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
51 if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
52 printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
53 printf("\n");
Andy Flemingad347bb2008-10-30 16:41:01 -050054
55 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang316f81a2011-01-04 01:04:19 +000056 puts("Capacity: ");
57 print_size(mmc->capacity, "\n");
Andy Flemingad347bb2008-10-30 16:41:01 -050058
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -060059 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
60 mmc->ddr_mode ? " DDR" : "");
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010061
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +010062#if CONFIG_IS_ENABLED(MMC_WRITE)
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010063 puts("Erase Group Size: ");
64 print_size(((u64)mmc->erase_grp_size) << 9, "\n");
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +010065#endif
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010066
Diego Santa Cruz1f69e222014-12-23 10:50:19 +010067 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010068 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
Diego Santa Cruz2b45f582014-12-23 10:50:23 +010069 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
Marek Vasutdfff8ed2020-06-20 14:28:25 +020070 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
71 u8 wp;
Heinrich Schuchardt0c8ffff2020-03-30 07:24:18 +020072 int ret;
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010073
Jean-Jacques Hiblotba54ab82018-01-04 15:23:36 +010074#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010075 puts("HC WP Group Size: ");
76 print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
Jean-Jacques Hiblotba54ab82018-01-04 15:23:36 +010077#endif
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010078
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010079 puts("User Capacity: ");
Diego Santa Cruz37a50b92014-12-23 10:50:33 +010080 print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
81 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
82 puts(" WRREL\n");
83 else
84 putc('\n');
Diego Santa Cruz2b45f582014-12-23 10:50:23 +010085 if (usr_enh) {
86 puts("User Enhanced Start: ");
87 print_size(mmc->enh_user_start, "\n");
88 puts("User Enhanced Size: ");
89 print_size(mmc->enh_user_size, "\n");
90 }
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010091 puts("Boot Capacity: ");
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010092 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010093 puts("RPMB Capacity: ");
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010094 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010095
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010096 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010097 bool is_enh = has_enh &&
98 (mmc->part_attr & EXT_CSD_ENH_GP(i));
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010099 if (mmc->capacity_gp[i]) {
Diego Santa Cruzd8358432014-12-23 10:50:18 +0100100 printf("GP%i Capacity: ", i+1);
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +0100101 print_size(mmc->capacity_gp[i],
Diego Santa Cruz37a50b92014-12-23 10:50:33 +0100102 is_enh ? " ENH" : "");
103 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
104 puts(" WRREL\n");
105 else
106 putc('\n');
Diego Santa Cruz99ca0282014-12-23 10:50:16 +0100107 }
108 }
Heinrich Schuchardt0c8ffff2020-03-30 07:24:18 +0200109 ret = mmc_send_ext_csd(mmc, ext_csd);
110 if (ret)
111 return;
112 wp = ext_csd[EXT_CSD_BOOT_WP_STATUS];
113 for (i = 0; i < 2; ++i) {
114 printf("Boot area %d is ", i);
115 switch (wp & 3) {
116 case 0:
117 printf("not write protected\n");
118 break;
119 case 1:
120 printf("power on protected\n");
121 break;
122 case 2:
123 printf("permanently protected\n");
124 break;
125 default:
126 printf("in reserved protection state\n");
127 break;
128 }
129 wp >>= 2;
130 }
Diego Santa Cruz99ca0282014-12-23 10:50:16 +0100131 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500132}
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530133
134static struct mmc *__init_mmc_device(int dev, bool force_init,
135 enum bus_mode speed_mode)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200136{
137 struct mmc *mmc;
138 mmc = find_mmc_device(dev);
139 if (!mmc) {
140 printf("no mmc device at slot %x\n", dev);
141 return NULL;
142 }
Eric Nelsonbc181662016-03-27 12:00:14 -0700143
Marek Vasutcb5df5d2019-01-03 22:09:43 +0100144 if (!mmc_getcd(mmc))
145 force_init = true;
146
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600147 if (force_init)
148 mmc->has_init = 0;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530149
150 if (IS_ENABLED(CONFIG_MMC_SPEED_MODE_SET))
151 mmc->user_speed_mode = speed_mode;
152
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200153 if (mmc_init(mmc))
154 return NULL;
Marek Vasut829540f2019-01-03 22:09:44 +0100155
156#ifdef CONFIG_BLOCK_CACHE
157 struct blk_desc *bd = mmc_get_blk_desc(mmc);
Simon Glassfada3f92022-09-17 09:00:09 -0600158 blkcache_invalidate(bd->uclass_id, bd->devnum);
Marek Vasut829540f2019-01-03 22:09:44 +0100159#endif
160
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200161 return mmc;
162}
Simon Glassed38aef2020-05-10 11:40:03 -0600163
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530164static struct mmc *init_mmc_device(int dev, bool force_init)
165{
166 return __init_mmc_device(dev, force_init, MMC_MODES_END);
167}
168
Simon Glassed38aef2020-05-10 11:40:03 -0600169static int do_mmcinfo(struct cmd_tbl *cmdtp, int flag, int argc,
170 char *const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500171{
172 struct mmc *mmc;
Andy Flemingad347bb2008-10-30 16:41:01 -0500173
Lei Wend430d7c2011-05-02 16:26:25 +0000174 if (curr_device < 0) {
175 if (get_mmc_num() > 0)
176 curr_device = 0;
177 else {
178 puts("No MMC device available\n");
Pali Rohár332c2302023-03-22 21:06:53 +0100179 return CMD_RET_FAILURE;
Lei Wend430d7c2011-05-02 16:26:25 +0000180 }
181 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500182
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600183 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200184 if (!mmc)
185 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500186
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200187 print_mmcinfo(mmc);
188 return CMD_RET_SUCCESS;
189}
Andy Flemingad347bb2008-10-30 16:41:01 -0500190
Alex Kiernan60e0f612018-05-08 04:43:31 +0000191#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200192static int confirm_key_prog(void)
193{
194 puts("Warning: Programming authentication key can be done only once !\n"
195 " Use this command only if you are sure of what you are doing,\n"
196 "Really perform the key programming? <y/N> ");
197 if (confirm_yesno())
Lei Wend430d7c2011-05-02 16:26:25 +0000198 return 1;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200199
200 puts("Authentication key programming aborted\n");
201 return 0;
202}
Simon Glassed38aef2020-05-10 11:40:03 -0600203
204static int do_mmcrpmb_key(struct cmd_tbl *cmdtp, int flag,
205 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200206{
207 void *key_addr;
208 struct mmc *mmc = find_mmc_device(curr_device);
209
210 if (argc != 2)
211 return CMD_RET_USAGE;
212
Simon Glass3ff49ec2021-07-24 09:03:29 -0600213 key_addr = (void *)hextoul(argv[1], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200214 if (!confirm_key_prog())
215 return CMD_RET_FAILURE;
216 if (mmc_rpmb_set_key(mmc, key_addr)) {
217 printf("ERROR - Key already programmed ?\n");
218 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500219 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200220 return CMD_RET_SUCCESS;
Andy Flemingad347bb2008-10-30 16:41:01 -0500221}
Simon Glassed38aef2020-05-10 11:40:03 -0600222
223static int do_mmcrpmb_read(struct cmd_tbl *cmdtp, int flag,
224 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200225{
226 u16 blk, cnt;
227 void *addr;
228 int n;
229 void *key_addr = NULL;
230 struct mmc *mmc = find_mmc_device(curr_device);
Andy Flemingad347bb2008-10-30 16:41:01 -0500231
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200232 if (argc < 4)
233 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500234
Simon Glass3ff49ec2021-07-24 09:03:29 -0600235 addr = (void *)hextoul(argv[1], NULL);
236 blk = hextoul(argv[2], NULL);
237 cnt = hextoul(argv[3], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200238
239 if (argc == 5)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600240 key_addr = (void *)hextoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200241
Simon Glass9703a932024-08-22 07:57:51 -0600242 printf("MMC RPMB read: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200243 curr_device, blk, cnt);
244 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
245
246 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
247 if (n != cnt)
248 return CMD_RET_FAILURE;
249 return CMD_RET_SUCCESS;
250}
Simon Glassed38aef2020-05-10 11:40:03 -0600251
252static int do_mmcrpmb_write(struct cmd_tbl *cmdtp, int flag,
253 int argc, char *const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500254{
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200255 u16 blk, cnt;
256 void *addr;
257 int n;
258 void *key_addr;
259 struct mmc *mmc = find_mmc_device(curr_device);
Lei Wen64b2b1e2011-06-22 17:03:30 +0000260
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200261 if (argc != 5)
Simon Glassa06dfc72011-12-10 08:44:01 +0000262 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500263
Simon Glass3ff49ec2021-07-24 09:03:29 -0600264 addr = (void *)hextoul(argv[1], NULL);
265 blk = hextoul(argv[2], NULL);
266 cnt = hextoul(argv[3], NULL);
267 key_addr = (void *)hextoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200268
Simon Glass9703a932024-08-22 07:57:51 -0600269 printf("MMC RPMB write: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200270 curr_device, blk, cnt);
271 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
272
273 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
274 if (n != cnt)
275 return CMD_RET_FAILURE;
276 return CMD_RET_SUCCESS;
277}
Simon Glassed38aef2020-05-10 11:40:03 -0600278
279static int do_mmcrpmb_counter(struct cmd_tbl *cmdtp, int flag,
280 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200281{
282 unsigned long counter;
283 struct mmc *mmc = find_mmc_device(curr_device);
284
285 if (mmc_rpmb_get_counter(mmc, &counter))
286 return CMD_RET_FAILURE;
287 printf("RPMB Write counter= %lx\n", counter);
288 return CMD_RET_SUCCESS;
289}
290
Simon Glassed38aef2020-05-10 11:40:03 -0600291static struct cmd_tbl cmd_rpmb[] = {
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200292 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
293 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
294 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
295 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
296};
297
Simon Glassed38aef2020-05-10 11:40:03 -0600298static int do_mmcrpmb(struct cmd_tbl *cmdtp, int flag,
299 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200300{
Simon Glassed38aef2020-05-10 11:40:03 -0600301 struct cmd_tbl *cp;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200302 struct mmc *mmc;
303 char original_part;
304 int ret;
305
306 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
307
308 /* Drop the rpmb subcommand */
309 argc--;
310 argv++;
311
312 if (cp == NULL || argc > cp->maxargs)
313 return CMD_RET_USAGE;
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100314 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200315 return CMD_RET_SUCCESS;
316
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600317 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200318 if (!mmc)
319 return CMD_RET_FAILURE;
320
321 if (!(mmc->version & MMC_VERSION_MMC)) {
Heinrich Schuchardteeb191c2020-03-29 19:26:57 +0000322 printf("It is not an eMMC device\n");
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200323 return CMD_RET_FAILURE;
Lei Wend430d7c2011-05-02 16:26:25 +0000324 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200325 if (mmc->version < MMC_VERSION_4_41) {
326 printf("RPMB not supported before version 4.41\n");
327 return CMD_RET_FAILURE;
328 }
329 /* Switch to the RPMB partition */
Kever Yangcb5f1932017-06-08 09:20:03 +0800330#ifndef CONFIG_BLK
Marek Vasuta62cf492016-05-04 16:35:25 +0200331 original_part = mmc->block_dev.hwpart;
Kever Yangcb5f1932017-06-08 09:20:03 +0800332#else
333 original_part = mmc_get_blk_desc(mmc)->hwpart;
334#endif
Simon Glassdbfa32c2022-08-11 19:34:59 -0600335 if (blk_select_hwpart_devnum(UCLASS_MMC, curr_device, MMC_PART_RPMB) !=
Simon Glass11f2bb62016-05-01 13:52:29 -0600336 0)
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700337 return CMD_RET_FAILURE;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200338 ret = cp->cmd(cmdtp, flag, argc, argv);
Andy Flemingad347bb2008-10-30 16:41:01 -0500339
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200340 /* Return to original partition */
Simon Glassdbfa32c2022-08-11 19:34:59 -0600341 if (blk_select_hwpart_devnum(UCLASS_MMC, curr_device, original_part) !=
Simon Glass11f2bb62016-05-01 13:52:29 -0600342 0)
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700343 return CMD_RET_FAILURE;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200344 return ret;
345}
346#endif
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530347
Simon Glassed38aef2020-05-10 11:40:03 -0600348static int do_mmc_read(struct cmd_tbl *cmdtp, int flag,
349 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200350{
351 struct mmc *mmc;
352 u32 blk, cnt, n;
353 void *addr;
Stephen Warren18faca72013-04-01 11:50:28 +0000354
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200355 if (argc != 4)
356 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500357
Simon Glass3ff49ec2021-07-24 09:03:29 -0600358 addr = (void *)hextoul(argv[1], NULL);
359 blk = hextoul(argv[2], NULL);
360 cnt = hextoul(argv[3], NULL);
Lei Wen61dba932010-09-13 22:07:28 +0800361
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600362 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200363 if (!mmc)
364 return CMD_RET_FAILURE;
Lei Wen61dba932010-09-13 22:07:28 +0800365
Simon Glass9703a932024-08-22 07:57:51 -0600366 printf("MMC read: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200367 curr_device, blk, cnt);
Stephen Warren18faca72013-04-01 11:50:28 +0000368
Simon Glasse5db1152016-05-01 13:52:35 -0600369 n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200370 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Lei Wend430d7c2011-05-02 16:26:25 +0000371
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200372 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
373}
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +0100374
Alex Kiernanc568bcb2018-05-29 15:30:52 +0000375#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar814a9752018-04-06 12:05:24 +0530376static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk,
377 lbaint_t blkcnt, const void *buffer)
378{
379 struct blk_desc *dev_desc = info->priv;
380
381 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
382}
383
384static lbaint_t mmc_sparse_reserve(struct sparse_storage *info,
385 lbaint_t blk, lbaint_t blkcnt)
386{
387 return blkcnt;
388}
389
Simon Glassed38aef2020-05-10 11:40:03 -0600390static int do_mmc_sparse_write(struct cmd_tbl *cmdtp, int flag,
391 int argc, char *const argv[])
Jassi Brar814a9752018-04-06 12:05:24 +0530392{
393 struct sparse_storage sparse;
394 struct blk_desc *dev_desc;
395 struct mmc *mmc;
396 char dest[11];
397 void *addr;
398 u32 blk;
399
400 if (argc != 3)
401 return CMD_RET_USAGE;
402
Simon Glass3ff49ec2021-07-24 09:03:29 -0600403 addr = (void *)hextoul(argv[1], NULL);
404 blk = hextoul(argv[2], NULL);
Jassi Brar814a9752018-04-06 12:05:24 +0530405
406 if (!is_sparse_image(addr)) {
407 printf("Not a sparse image\n");
408 return CMD_RET_FAILURE;
409 }
410
411 mmc = init_mmc_device(curr_device, false);
412 if (!mmc)
413 return CMD_RET_FAILURE;
414
Simon Glass9703a932024-08-22 07:57:51 -0600415 printf("MMC Sparse write: dev # %d, block # %d ... ",
Jassi Brar814a9752018-04-06 12:05:24 +0530416 curr_device, blk);
417
418 if (mmc_getwp(mmc) == 1) {
419 printf("Error: card is write protected!\n");
420 return CMD_RET_FAILURE;
421 }
422
423 dev_desc = mmc_get_blk_desc(mmc);
424 sparse.priv = dev_desc;
425 sparse.blksz = 512;
426 sparse.start = blk;
427 sparse.size = dev_desc->lba - blk;
428 sparse.write = mmc_sparse_write;
429 sparse.reserve = mmc_sparse_reserve;
430 sparse.mssg = NULL;
431 sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz);
432
Alex Kiernan27b69de2018-05-29 15:30:40 +0000433 if (write_sparse_image(&sparse, dest, addr, NULL))
Jassi Brar814a9752018-04-06 12:05:24 +0530434 return CMD_RET_FAILURE;
435 else
436 return CMD_RET_SUCCESS;
437}
438#endif
439
Alex Kiernanc568bcb2018-05-29 15:30:52 +0000440#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glassed38aef2020-05-10 11:40:03 -0600441static int do_mmc_write(struct cmd_tbl *cmdtp, int flag,
442 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200443{
444 struct mmc *mmc;
445 u32 blk, cnt, n;
446 void *addr;
Andy Flemingad347bb2008-10-30 16:41:01 -0500447
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200448 if (argc != 4)
449 return CMD_RET_USAGE;
Lei Wend430d7c2011-05-02 16:26:25 +0000450
Simon Glass3ff49ec2021-07-24 09:03:29 -0600451 addr = (void *)hextoul(argv[1], NULL);
452 blk = hextoul(argv[2], NULL);
453 cnt = hextoul(argv[3], NULL);
Lei Wend430d7c2011-05-02 16:26:25 +0000454
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600455 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200456 if (!mmc)
457 return CMD_RET_FAILURE;
Lei Wen31b99802011-05-02 16:26:26 +0000458
Simon Glass9703a932024-08-22 07:57:51 -0600459 printf("MMC write: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200460 curr_device, blk, cnt);
Lei Wen31b99802011-05-02 16:26:26 +0000461
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200462 if (mmc_getwp(mmc) == 1) {
463 printf("Error: card is write protected!\n");
464 return CMD_RET_FAILURE;
465 }
Simon Glasse5db1152016-05-01 13:52:35 -0600466 n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200467 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Lei Wend430d7c2011-05-02 16:26:25 +0000468
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200469 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
470}
Simon Glassed38aef2020-05-10 11:40:03 -0600471
472static int do_mmc_erase(struct cmd_tbl *cmdtp, int flag,
473 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200474{
475 struct mmc *mmc;
476 u32 blk, cnt, n;
Tom Rinif8c6f792014-02-05 10:24:21 -0500477
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200478 if (argc != 3)
479 return CMD_RET_USAGE;
Tom Rinif8c6f792014-02-05 10:24:21 -0500480
Simon Glass3ff49ec2021-07-24 09:03:29 -0600481 blk = hextoul(argv[1], NULL);
482 cnt = hextoul(argv[2], NULL);
Amar3ccad532013-04-27 11:43:00 +0530483
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600484 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200485 if (!mmc)
486 return CMD_RET_FAILURE;
Tom Rinif8c6f792014-02-05 10:24:21 -0500487
Simon Glass9703a932024-08-22 07:57:51 -0600488 printf("MMC erase: dev # %d, block # %d, count %d ... ",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200489 curr_device, blk, cnt);
Tom Rini4cf854c2014-02-05 10:24:22 -0500490
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200491 if (mmc_getwp(mmc) == 1) {
492 printf("Error: card is write protected!\n");
493 return CMD_RET_FAILURE;
494 }
Simon Glasse5db1152016-05-01 13:52:35 -0600495 n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200496 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Tom Rini4cf854c2014-02-05 10:24:22 -0500497
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200498 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
499}
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +0100500#endif
501
Simon Glassed38aef2020-05-10 11:40:03 -0600502static int do_mmc_rescan(struct cmd_tbl *cmdtp, int flag,
503 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200504{
505 struct mmc *mmc;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530506
507 if (argc == 1) {
508 mmc = init_mmc_device(curr_device, true);
509 } else if (argc == 2) {
Heinrich Schuchardt05c74282022-04-25 23:11:06 +0200510 enum bus_mode speed_mode;
511
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530512 speed_mode = (int)dectoul(argv[1], NULL);
513 mmc = __init_mmc_device(curr_device, true, speed_mode);
514 } else {
515 return CMD_RET_USAGE;
516 }
Tom Rini4cf854c2014-02-05 10:24:22 -0500517
Stephen Warren07ec4e72014-05-23 13:24:46 -0600518 if (!mmc)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200519 return CMD_RET_FAILURE;
Tom Rini4cf854c2014-02-05 10:24:22 -0500520
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200521 return CMD_RET_SUCCESS;
522}
Simon Glassed38aef2020-05-10 11:40:03 -0600523
524static int do_mmc_part(struct cmd_tbl *cmdtp, int flag,
525 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200526{
Simon Glasse3394752016-02-29 15:25:34 -0700527 struct blk_desc *mmc_dev;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200528 struct mmc *mmc;
Amar3ccad532013-04-27 11:43:00 +0530529
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600530 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200531 if (!mmc)
532 return CMD_RET_FAILURE;
Amar3ccad532013-04-27 11:43:00 +0530533
Simon Glassfada3f92022-09-17 09:00:09 -0600534 mmc_dev = blk_get_devnum_by_uclass_id(UCLASS_MMC, curr_device);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200535 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
Simon Glassb89a8442016-02-29 15:25:48 -0700536 part_print(mmc_dev);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200537 return CMD_RET_SUCCESS;
538 }
Amar3ccad532013-04-27 11:43:00 +0530539
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200540 puts("get mmc type error!\n");
541 return CMD_RET_FAILURE;
542}
Simon Glassed38aef2020-05-10 11:40:03 -0600543
544static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag,
545 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200546{
Stephen Warren18549802014-05-23 12:48:10 -0600547 int dev, part = 0, ret;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200548 struct mmc *mmc;
Tom Rini35a3ea12014-02-07 14:15:20 -0500549
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200550 if (argc == 1) {
551 dev = curr_device;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530552 mmc = init_mmc_device(dev, true);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200553 } else if (argc == 2) {
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530554 dev = (int)dectoul(argv[1], NULL);
555 mmc = init_mmc_device(dev, true);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200556 } else if (argc == 3) {
Simon Glassff9b9032021-07-24 09:03:30 -0600557 dev = (int)dectoul(argv[1], NULL);
558 part = (int)dectoul(argv[2], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200559 if (part > PART_ACCESS_MASK) {
560 printf("#part_num shouldn't be larger than %d\n",
561 PART_ACCESS_MASK);
562 return CMD_RET_FAILURE;
Tom Rini35a3ea12014-02-07 14:15:20 -0500563 }
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530564 mmc = init_mmc_device(dev, true);
565 } else if (argc == 4) {
Heinrich Schuchardt05c74282022-04-25 23:11:06 +0200566 enum bus_mode speed_mode;
567
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530568 dev = (int)dectoul(argv[1], NULL);
569 part = (int)dectoul(argv[2], NULL);
570 if (part > PART_ACCESS_MASK) {
571 printf("#part_num shouldn't be larger than %d\n",
572 PART_ACCESS_MASK);
573 return CMD_RET_FAILURE;
574 }
575 speed_mode = (int)dectoul(argv[3], NULL);
576 mmc = __init_mmc_device(dev, true, speed_mode);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200577 } else {
578 return CMD_RET_USAGE;
579 }
Tom Rini35a3ea12014-02-07 14:15:20 -0500580
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200581 if (!mmc)
582 return CMD_RET_FAILURE;
Tom Rini35a3ea12014-02-07 14:15:20 -0500583
Simon Glassdbfa32c2022-08-11 19:34:59 -0600584 ret = blk_select_hwpart_devnum(UCLASS_MMC, dev, part);
Stephen Warren18549802014-05-23 12:48:10 -0600585 printf("switch to partitions #%d, %s\n",
586 part, (!ret) ? "OK" : "ERROR");
587 if (ret)
588 return 1;
589
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200590 curr_device = dev;
591 if (mmc->part_config == MMCPART_NOAVAILABLE)
592 printf("mmc%d is current device\n", curr_device);
593 else
594 printf("mmc%d(part %d) is current device\n",
Simon Glasse5db1152016-05-01 13:52:35 -0600595 curr_device, mmc_get_blk_desc(mmc)->hwpart);
Tom Rini35a3ea12014-02-07 14:15:20 -0500596
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200597 return CMD_RET_SUCCESS;
598}
Simon Glassed38aef2020-05-10 11:40:03 -0600599
600static int do_mmc_list(struct cmd_tbl *cmdtp, int flag,
601 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200602{
603 print_mmc_devices('\n');
604 return CMD_RET_SUCCESS;
605}
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100606
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +0100607#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Marek Vasut94e39952021-09-15 11:43:13 +0200608static void parse_hwpart_user_enh_size(struct mmc *mmc,
609 struct mmc_hwpart_conf *pconf,
610 char *argv)
611{
Marek Vasut30d29662022-01-17 22:54:29 +0100612 int i, ret;
Marek Vasut94e39952021-09-15 11:43:13 +0200613
614 pconf->user.enh_size = 0;
615
616 if (!strcmp(argv, "-")) { /* The rest of eMMC */
617 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
618 ret = mmc_send_ext_csd(mmc, ext_csd);
619 if (ret)
620 return;
Marek Vasut30d29662022-01-17 22:54:29 +0100621 /* The enh_size value is in 512B block units */
Marek Vasut94e39952021-09-15 11:43:13 +0200622 pconf->user.enh_size =
623 ((ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 2] << 16) +
624 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 1] << 8) +
625 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT]) * 1024 *
626 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
627 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
628 pconf->user.enh_size -= pconf->user.enh_start;
Marek Vasut30d29662022-01-17 22:54:29 +0100629 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
630 /*
631 * If the eMMC already has GP partitions set,
632 * subtract their size from the maximum USER
633 * partition size.
634 *
635 * Else, if the command was used to configure new
636 * GP partitions, subtract their size from maximum
637 * USER partition size.
638 */
639 if (mmc->capacity_gp[i]) {
640 /* The capacity_gp is in 1B units */
641 pconf->user.enh_size -= mmc->capacity_gp[i] >> 9;
642 } else if (pconf->gp_part[i].size) {
643 /* The gp_part[].size is in 512B units */
644 pconf->user.enh_size -= pconf->gp_part[i].size;
645 }
646 }
Marek Vasut94e39952021-09-15 11:43:13 +0200647 } else {
648 pconf->user.enh_size = dectoul(argv, NULL);
649 }
650}
651
652static int parse_hwpart_user(struct mmc *mmc, struct mmc_hwpart_conf *pconf,
Simon Glassed38aef2020-05-10 11:40:03 -0600653 int argc, char *const argv[])
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100654{
655 int i = 0;
656
657 memset(&pconf->user, 0, sizeof(pconf->user));
658
659 while (i < argc) {
660 if (!strcmp(argv[i], "enh")) {
661 if (i + 2 >= argc)
662 return -1;
663 pconf->user.enh_start =
Simon Glassff9b9032021-07-24 09:03:30 -0600664 dectoul(argv[i + 1], NULL);
Marek Vasut94e39952021-09-15 11:43:13 +0200665 parse_hwpart_user_enh_size(mmc, pconf, argv[i + 2]);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100666 i += 3;
667 } else if (!strcmp(argv[i], "wrrel")) {
668 if (i + 1 >= argc)
669 return -1;
670 pconf->user.wr_rel_change = 1;
671 if (!strcmp(argv[i+1], "on"))
672 pconf->user.wr_rel_set = 1;
673 else if (!strcmp(argv[i+1], "off"))
674 pconf->user.wr_rel_set = 0;
675 else
676 return -1;
677 i += 2;
678 } else {
679 break;
680 }
681 }
682 return i;
683}
684
685static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
Simon Glassed38aef2020-05-10 11:40:03 -0600686 int argc, char *const argv[])
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100687{
688 int i;
689
690 memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
691
692 if (1 >= argc)
693 return -1;
Simon Glassff9b9032021-07-24 09:03:30 -0600694 pconf->gp_part[pidx].size = dectoul(argv[0], NULL);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100695
696 i = 1;
697 while (i < argc) {
698 if (!strcmp(argv[i], "enh")) {
699 pconf->gp_part[pidx].enhanced = 1;
700 i += 1;
701 } else if (!strcmp(argv[i], "wrrel")) {
702 if (i + 1 >= argc)
703 return -1;
704 pconf->gp_part[pidx].wr_rel_change = 1;
705 if (!strcmp(argv[i+1], "on"))
706 pconf->gp_part[pidx].wr_rel_set = 1;
707 else if (!strcmp(argv[i+1], "off"))
708 pconf->gp_part[pidx].wr_rel_set = 0;
709 else
710 return -1;
711 i += 2;
712 } else {
713 break;
714 }
715 }
716 return i;
717}
718
Simon Glassed38aef2020-05-10 11:40:03 -0600719static int do_mmc_hwpartition(struct cmd_tbl *cmdtp, int flag,
720 int argc, char *const argv[])
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100721{
722 struct mmc *mmc;
723 struct mmc_hwpart_conf pconf = { };
724 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100725 int i, r, pidx;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100726
727 mmc = init_mmc_device(curr_device, false);
728 if (!mmc)
729 return CMD_RET_FAILURE;
730
Jaehoon Chungffbba622021-09-24 09:23:34 +0900731 if (IS_SD(mmc)) {
732 puts("SD doesn't support partitioning\n");
733 return CMD_RET_FAILURE;
734 }
735
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100736 if (argc < 1)
737 return CMD_RET_USAGE;
738 i = 1;
739 while (i < argc) {
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100740 if (!strcmp(argv[i], "user")) {
741 i++;
Marek Vasut94e39952021-09-15 11:43:13 +0200742 r = parse_hwpart_user(mmc, &pconf, argc - i, &argv[i]);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100743 if (r < 0)
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100744 return CMD_RET_USAGE;
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100745 i += r;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100746 } else if (!strncmp(argv[i], "gp", 2) &&
747 strlen(argv[i]) == 3 &&
748 argv[i][2] >= '1' && argv[i][2] <= '4') {
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100749 pidx = argv[i][2] - '1';
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100750 i++;
751 r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
752 if (r < 0)
753 return CMD_RET_USAGE;
754 i += r;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100755 } else if (!strcmp(argv[i], "check")) {
756 mode = MMC_HWPART_CONF_CHECK;
757 i++;
758 } else if (!strcmp(argv[i], "set")) {
759 mode = MMC_HWPART_CONF_SET;
760 i++;
761 } else if (!strcmp(argv[i], "complete")) {
762 mode = MMC_HWPART_CONF_COMPLETE;
763 i++;
764 } else {
765 return CMD_RET_USAGE;
766 }
767 }
768
769 puts("Partition configuration:\n");
770 if (pconf.user.enh_size) {
771 puts("\tUser Enhanced Start: ");
772 print_size(((u64)pconf.user.enh_start) << 9, "\n");
773 puts("\tUser Enhanced Size: ");
774 print_size(((u64)pconf.user.enh_size) << 9, "\n");
775 } else {
776 puts("\tNo enhanced user data area\n");
777 }
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100778 if (pconf.user.wr_rel_change)
779 printf("\tUser partition write reliability: %s\n",
780 pconf.user.wr_rel_set ? "on" : "off");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100781 for (pidx = 0; pidx < 4; pidx++) {
782 if (pconf.gp_part[pidx].size) {
783 printf("\tGP%i Capacity: ", pidx+1);
784 print_size(((u64)pconf.gp_part[pidx].size) << 9,
785 pconf.gp_part[pidx].enhanced ?
786 " ENH\n" : "\n");
787 } else {
788 printf("\tNo GP%i partition\n", pidx+1);
789 }
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100790 if (pconf.gp_part[pidx].wr_rel_change)
791 printf("\tGP%i write reliability: %s\n", pidx+1,
792 pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100793 }
794
795 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
796 if (mode == MMC_HWPART_CONF_COMPLETE)
797 puts("Partitioning successful, "
798 "power-cycle to make effective\n");
799 return CMD_RET_SUCCESS;
800 } else {
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100801 puts("Failed!\n");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100802 return CMD_RET_FAILURE;
803 }
804}
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +0100805#endif
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100806
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200807#ifdef CONFIG_SUPPORT_EMMC_BOOT
Simon Glassed38aef2020-05-10 11:40:03 -0600808static int do_mmc_bootbus(struct cmd_tbl *cmdtp, int flag,
809 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200810{
811 int dev;
812 struct mmc *mmc;
813 u8 width, reset, mode;
814
815 if (argc != 5)
816 return CMD_RET_USAGE;
Simon Glassff9b9032021-07-24 09:03:30 -0600817 dev = dectoul(argv[1], NULL);
818 width = dectoul(argv[2], NULL);
819 reset = dectoul(argv[3], NULL);
820 mode = dectoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200821
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600822 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200823 if (!mmc)
824 return CMD_RET_FAILURE;
825
826 if (IS_SD(mmc)) {
827 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
828 return CMD_RET_FAILURE;
Amar3ccad532013-04-27 11:43:00 +0530829 }
Markus Niebel03951412013-12-16 13:40:46 +0100830
Jaehoon Chung7483dd32021-02-26 18:38:20 +0900831 /*
832 * BOOT_BUS_CONDITIONS[177]
833 * BOOT_MODE[4:3]
834 * 0x0 : Use SDR + Backward compatible timing in boot operation
835 * 0x1 : Use SDR + High Speed Timing in boot operation mode
836 * 0x2 : Use DDR in boot operation
837 * RESET_BOOT_BUS_CONDITIONS
838 * 0x0 : Reset bus width to x1, SDR, Backward compatible
839 * 0x1 : Retain BOOT_BUS_WIDTH and BOOT_MODE
840 * BOOT_BUS_WIDTH
841 * 0x0 : x1(sdr) or x4 (ddr) buswidth
842 * 0x1 : x4(sdr/ddr) buswith
843 * 0x2 : x8(sdr/ddr) buswith
844 *
845 */
846 if (width >= 0x3) {
847 printf("boot_bus_width %d is invalid\n", width);
848 return CMD_RET_FAILURE;
849 }
850
851 if (reset >= 0x2) {
852 printf("reset_boot_bus_width %d is invalid\n", reset);
853 return CMD_RET_FAILURE;
854 }
855
856 if (mode >= 0x3) {
857 printf("reset_boot_bus_width %d is invalid\n", mode);
858 return CMD_RET_FAILURE;
859 }
860
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200861 /* acknowledge to be sent during boot operation */
Jaehoon Chung7483dd32021-02-26 18:38:20 +0900862 if (mmc_set_boot_bus_width(mmc, width, reset, mode)) {
863 puts("BOOT_BUS_WIDTH is failed to change.\n");
864 return CMD_RET_FAILURE;
865 }
866
867 printf("Set to BOOT_BUS_WIDTH = 0x%x, RESET = 0x%x, BOOT_MODE = 0x%x\n",
868 width, reset, mode);
869 return CMD_RET_SUCCESS;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200870}
Simon Glassed38aef2020-05-10 11:40:03 -0600871
872static int do_mmc_boot_resize(struct cmd_tbl *cmdtp, int flag,
873 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200874{
875 int dev;
876 struct mmc *mmc;
877 u32 bootsize, rpmbsize;
Markus Niebel03951412013-12-16 13:40:46 +0100878
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200879 if (argc != 4)
880 return CMD_RET_USAGE;
Simon Glassff9b9032021-07-24 09:03:30 -0600881 dev = dectoul(argv[1], NULL);
882 bootsize = dectoul(argv[2], NULL);
883 rpmbsize = dectoul(argv[3], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200884
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600885 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200886 if (!mmc)
887 return CMD_RET_FAILURE;
888
889 if (IS_SD(mmc)) {
Heinrich Schuchardteeb191c2020-03-29 19:26:57 +0000890 printf("It is not an eMMC device\n");
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200891 return CMD_RET_FAILURE;
Markus Niebel03951412013-12-16 13:40:46 +0100892 }
893
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200894 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
895 printf("EMMC boot partition Size change Failed.\n");
896 return CMD_RET_FAILURE;
897 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500898
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200899 printf("EMMC boot partition Size %d MB\n", bootsize);
900 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
901 return CMD_RET_SUCCESS;
902}
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200903
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200904static int mmc_partconf_print(struct mmc *mmc, const char *varname)
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200905{
906 u8 ack, access, part;
907
908 if (mmc->part_config == MMCPART_NOAVAILABLE) {
909 printf("No part_config info for ver. 0x%x\n", mmc->version);
910 return CMD_RET_FAILURE;
911 }
912
913 access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
914 ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
915 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
916
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200917 if(varname)
918 env_set_hex(varname, part);
919
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200920 printf("EXT_CSD[179], PARTITION_CONFIG:\n"
921 "BOOT_ACK: 0x%x\n"
Tim Harvey728cbde2024-05-31 08:36:34 -0700922 "BOOT_PARTITION_ENABLE: 0x%x (%s)\n"
923 "PARTITION_ACCESS: 0x%x (%s)\n", ack, part, emmc_boot_part_names[part],
924 access, emmc_hwpart_names[access]);
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200925
926 return CMD_RET_SUCCESS;
927}
928
Simon Glassed38aef2020-05-10 11:40:03 -0600929static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag,
930 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200931{
Pali Rohár332c2302023-03-22 21:06:53 +0100932 int ret, dev;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200933 struct mmc *mmc;
934 u8 ack, part_num, access;
Lei Wenea526762011-06-22 17:03:31 +0000935
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200936 if (argc != 2 && argc != 3 && argc != 5)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200937 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500938
Simon Glassff9b9032021-07-24 09:03:30 -0600939 dev = dectoul(argv[1], NULL);
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530940
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600941 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200942 if (!mmc)
943 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500944
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200945 if (IS_SD(mmc)) {
946 puts("PARTITION_CONFIG only exists on eMMC\n");
947 return CMD_RET_FAILURE;
948 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500949
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200950 if (argc == 2 || argc == 3)
Simon Glass793a98e2023-11-18 14:05:20 -0700951 return mmc_partconf_print(mmc, cmd_arg2(argc, argv));
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200952
Tim Harvey728cbde2024-05-31 08:36:34 -0700953 /* BOOT_ACK */
Simon Glassff9b9032021-07-24 09:03:30 -0600954 ack = dectoul(argv[2], NULL);
Tim Harvey728cbde2024-05-31 08:36:34 -0700955 /* BOOT_PARTITION_ENABLE */
956 if (!isdigit(*argv[3])) {
957 for (part_num = ARRAY_SIZE(emmc_boot_part_names) - 1; part_num > 0; part_num--) {
958 if (!strcmp(argv[3], emmc_boot_part_names[part_num]))
959 break;
960 }
961 } else {
962 part_num = dectoul(argv[3], NULL);
963 }
964 /* PARTITION_ACCESS */
965 if (!isdigit(*argv[4])) {
966 for (access = ARRAY_SIZE(emmc_hwpart_names) - 1; access > 0; access--) {
967 if (!strcmp(argv[4], emmc_hwpart_names[access]))
968 break;
969 }
970 } else {
971 access = dectoul(argv[4], NULL);
972 }
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200973
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200974 /* acknowledge to be sent during boot operation */
Pali Rohár332c2302023-03-22 21:06:53 +0100975 ret = mmc_set_part_conf(mmc, ack, part_num, access);
976 if (ret != 0)
977 return CMD_RET_FAILURE;
978
979 return CMD_RET_SUCCESS;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200980}
Simon Glassed38aef2020-05-10 11:40:03 -0600981
982static int do_mmc_rst_func(struct cmd_tbl *cmdtp, int flag,
983 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200984{
Pali Rohár332c2302023-03-22 21:06:53 +0100985 int ret, dev;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200986 struct mmc *mmc;
987 u8 enable;
Nikita Kiryanov020f2612012-12-03 02:19:46 +0000988
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200989 /*
990 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
991 * The only valid values are 0x0, 0x1 and 0x2 and writing
992 * a value of 0x1 or 0x2 sets the value permanently.
993 */
994 if (argc != 3)
995 return CMD_RET_USAGE;
996
Simon Glassff9b9032021-07-24 09:03:30 -0600997 dev = dectoul(argv[1], NULL);
998 enable = dectoul(argv[2], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200999
Peng Fanb1e455a2015-11-25 17:16:21 +08001000 if (enable > 2) {
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001001 puts("Invalid RST_n_ENABLE value\n");
1002 return CMD_RET_USAGE;
1003 }
1004
Stephen Warrenb627f4b2014-05-23 13:24:45 -06001005 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001006 if (!mmc)
1007 return CMD_RET_FAILURE;
1008
1009 if (IS_SD(mmc)) {
1010 puts("RST_n_FUNCTION only exists on eMMC\n");
1011 return CMD_RET_FAILURE;
1012 }
1013
Pali Rohár332c2302023-03-22 21:06:53 +01001014 ret = mmc_set_rst_n_function(mmc, enable);
1015 if (ret != 0)
1016 return CMD_RET_FAILURE;
1017
1018 return CMD_RET_SUCCESS;
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001019}
1020#endif
Simon Glassed38aef2020-05-10 11:40:03 -06001021static int do_mmc_setdsr(struct cmd_tbl *cmdtp, int flag,
1022 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001023{
1024 struct mmc *mmc;
1025 u32 val;
1026 int ret;
Andy Flemingad347bb2008-10-30 16:41:01 -05001027
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001028 if (argc != 2)
1029 return CMD_RET_USAGE;
Simon Glass3ff49ec2021-07-24 09:03:29 -06001030 val = hextoul(argv[1], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001031
1032 mmc = find_mmc_device(curr_device);
1033 if (!mmc) {
1034 printf("no mmc device at slot %x\n", curr_device);
1035 return CMD_RET_FAILURE;
1036 }
1037 ret = mmc_set_dsr(mmc, val);
1038 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
1039 if (!ret) {
1040 mmc->has_init = 0;
1041 if (mmc_init(mmc))
1042 return CMD_RET_FAILURE;
1043 else
1044 return CMD_RET_SUCCESS;
Andy Flemingad347bb2008-10-30 16:41:01 -05001045 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001046 return ret;
1047}
Lei Wend430d7c2011-05-02 16:26:25 +00001048
Tomas Melinc17dae52016-11-25 11:01:03 +02001049#ifdef CONFIG_CMD_BKOPS_ENABLE
Marek Vasutefdeed62023-01-05 15:19:08 +01001050static int mmc_bkops_common(char *device, bool autobkops, bool enable)
Tomas Melinc17dae52016-11-25 11:01:03 +02001051{
Tomas Melinc17dae52016-11-25 11:01:03 +02001052 struct mmc *mmc;
Marek Vasutefdeed62023-01-05 15:19:08 +01001053 int dev;
Tomas Melinc17dae52016-11-25 11:01:03 +02001054
Marek Vasutefdeed62023-01-05 15:19:08 +01001055 dev = dectoul(device, NULL);
Tomas Melinc17dae52016-11-25 11:01:03 +02001056
1057 mmc = init_mmc_device(dev, false);
1058 if (!mmc)
1059 return CMD_RET_FAILURE;
1060
1061 if (IS_SD(mmc)) {
1062 puts("BKOPS_EN only exists on eMMC\n");
1063 return CMD_RET_FAILURE;
1064 }
1065
Marek Vasutefdeed62023-01-05 15:19:08 +01001066 return mmc_set_bkops_enable(mmc, autobkops, enable);
1067}
1068
1069static int do_mmc_bkops(struct cmd_tbl *cmdtp, int flag,
1070 int argc, char * const argv[])
1071{
1072 bool autobkops, enable;
1073
1074 if (argc != 4)
1075 return CMD_RET_USAGE;
1076
1077 if (!strcmp(argv[2], "manual"))
1078 autobkops = false;
1079 else if (!strcmp(argv[2], "auto"))
1080 autobkops = true;
1081 else
1082 return CMD_RET_FAILURE;
1083
1084 if (!strcmp(argv[3], "disable"))
1085 enable = false;
1086 else if (!strcmp(argv[3], "enable"))
1087 enable = true;
1088 else
1089 return CMD_RET_FAILURE;
1090
1091 return mmc_bkops_common(argv[1], autobkops, enable);
1092}
1093
1094static int do_mmc_bkops_enable(struct cmd_tbl *cmdtp, int flag,
1095 int argc, char * const argv[])
1096{
1097 if (argc != 2)
1098 return CMD_RET_USAGE;
1099
1100 return mmc_bkops_common(argv[1], false, true);
Tomas Melinc17dae52016-11-25 11:01:03 +02001101}
1102#endif
1103
Simon Glassed38aef2020-05-10 11:40:03 -06001104static int do_mmc_boot_wp(struct cmd_tbl *cmdtp, int flag,
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +02001105 int argc, char * const argv[])
1106{
1107 int err;
1108 struct mmc *mmc;
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001109 int part;
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +02001110
1111 mmc = init_mmc_device(curr_device, false);
1112 if (!mmc)
1113 return CMD_RET_FAILURE;
1114 if (IS_SD(mmc)) {
1115 printf("It is not an eMMC device\n");
1116 return CMD_RET_FAILURE;
1117 }
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001118
1119 if (argc == 2) {
1120 part = dectoul(argv[1], NULL);
1121 err = mmc_boot_wp_single_partition(mmc, part);
1122 } else {
1123 err = mmc_boot_wp(mmc);
1124 }
1125
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +02001126 if (err)
1127 return CMD_RET_FAILURE;
1128 printf("boot areas protected\n");
1129 return CMD_RET_SUCCESS;
1130}
1131
Marek Vasut1f692e32023-10-31 13:20:17 +01001132#if CONFIG_IS_ENABLED(CMD_MMC_REG)
1133static int do_mmc_reg(struct cmd_tbl *cmdtp, int flag,
1134 int argc, char *const argv[])
1135{
1136 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1137 struct mmc *mmc;
1138 int i, ret;
1139 u32 off;
1140
1141 if (argc < 3 || argc > 5)
1142 return CMD_RET_USAGE;
1143
1144 mmc = find_mmc_device(curr_device);
1145 if (!mmc) {
1146 printf("no mmc device at slot %x\n", curr_device);
1147 return CMD_RET_FAILURE;
1148 }
1149
1150 if (IS_SD(mmc)) {
1151 printf("SD registers are not supported\n");
1152 return CMD_RET_FAILURE;
1153 }
1154
1155 off = simple_strtoul(argv[3], NULL, 10);
1156 if (!strcmp(argv[2], "cid")) {
1157 if (off > 3)
1158 return CMD_RET_USAGE;
1159 printf("CID[%i]: 0x%08x\n", off, mmc->cid[off]);
1160 if (argv[4])
1161 env_set_hex(argv[4], mmc->cid[off]);
1162 return CMD_RET_SUCCESS;
1163 }
1164 if (!strcmp(argv[2], "csd")) {
1165 if (off > 3)
1166 return CMD_RET_USAGE;
1167 printf("CSD[%i]: 0x%08x\n", off, mmc->csd[off]);
1168 if (argv[4])
1169 env_set_hex(argv[4], mmc->csd[off]);
1170 return CMD_RET_SUCCESS;
1171 }
1172 if (!strcmp(argv[2], "dsr")) {
1173 printf("DSR: 0x%08x\n", mmc->dsr);
1174 if (argv[4])
1175 env_set_hex(argv[4], mmc->dsr);
1176 return CMD_RET_SUCCESS;
1177 }
1178 if (!strcmp(argv[2], "ocr")) {
1179 printf("OCR: 0x%08x\n", mmc->ocr);
1180 if (argv[4])
1181 env_set_hex(argv[4], mmc->ocr);
1182 return CMD_RET_SUCCESS;
1183 }
1184 if (!strcmp(argv[2], "rca")) {
1185 printf("RCA: 0x%08x\n", mmc->rca);
1186 if (argv[4])
1187 env_set_hex(argv[4], mmc->rca);
1188 return CMD_RET_SUCCESS;
1189 }
1190 if (!strcmp(argv[2], "extcsd") &&
1191 mmc->version >= MMC_VERSION_4_41) {
1192 ret = mmc_send_ext_csd(mmc, ext_csd);
1193 if (ret)
1194 return CMD_RET_FAILURE;
1195 if (!strcmp(argv[3], "all")) {
1196 /* Dump the entire register */
1197 printf("EXT_CSD:");
1198 for (i = 0; i < MMC_MAX_BLOCK_LEN; i++) {
1199 if (!(i % 10))
1200 printf("\n%03i: ", i);
1201 printf(" %02x", ext_csd[i]);
1202 }
1203 printf("\n");
1204 return CMD_RET_SUCCESS;
1205 }
1206 off = simple_strtoul(argv[3], NULL, 10);
1207 if (off > 512)
1208 return CMD_RET_USAGE;
1209 printf("EXT_CSD[%i]: 0x%02x\n", off, ext_csd[off]);
1210 if (argv[4])
1211 env_set_hex(argv[4], ext_csd[off]);
1212 return CMD_RET_SUCCESS;
1213 }
1214
1215 return CMD_RET_FAILURE;
1216}
1217#endif
1218
Simon Glassed38aef2020-05-10 11:40:03 -06001219static struct cmd_tbl cmd_mmc[] = {
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001220 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
1221 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001222 U_BOOT_CMD_MKENT(wp, 2, 0, do_mmc_boot_wp, "", ""),
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01001223#if CONFIG_IS_ENABLED(MMC_WRITE)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001224 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
1225 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01001226#endif
Alex Kiernanc568bcb2018-05-29 15:30:52 +00001227#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
1228 U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
1229#endif
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301230 U_BOOT_CMD_MKENT(rescan, 2, 1, do_mmc_rescan, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001231 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301232 U_BOOT_CMD_MKENT(dev, 4, 0, do_mmc_dev, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001233 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +01001234#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001235 U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +01001236#endif
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001237#ifdef CONFIG_SUPPORT_EMMC_BOOT
1238 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehd49c4aa2014-09-25 23:00:16 +08001239 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001240 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
1241 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
1242#endif
Alex Kiernan60e0f612018-05-08 04:43:31 +00001243#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001244 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
1245#endif
1246 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
Tomas Melinc17dae52016-11-25 11:01:03 +02001247#ifdef CONFIG_CMD_BKOPS_ENABLE
1248 U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
Marek Vasutefdeed62023-01-05 15:19:08 +01001249 U_BOOT_CMD_MKENT(bkops, 4, 0, do_mmc_bkops, "", ""),
Tomas Melinc17dae52016-11-25 11:01:03 +02001250#endif
Marek Vasut1f692e32023-10-31 13:20:17 +01001251#if CONFIG_IS_ENABLED(CMD_MMC_REG)
1252 U_BOOT_CMD_MKENT(reg, 5, 0, do_mmc_reg, "", ""),
1253#endif
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001254};
1255
Simon Glassed38aef2020-05-10 11:40:03 -06001256static int do_mmcops(struct cmd_tbl *cmdtp, int flag, int argc,
1257 char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001258{
Simon Glassed38aef2020-05-10 11:40:03 -06001259 struct cmd_tbl *cp;
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001260
1261 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
1262
1263 /* Drop the mmc command */
1264 argc--;
1265 argv++;
1266
1267 if (cp == NULL || argc > cp->maxargs)
1268 return CMD_RET_USAGE;
Boris Brezillonc71dfd12018-12-03 22:54:20 +01001269 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001270 return CMD_RET_SUCCESS;
1271
1272 if (curr_device < 0) {
1273 if (get_mmc_num() > 0) {
1274 curr_device = 0;
1275 } else {
1276 puts("No MMC device available\n");
1277 return CMD_RET_FAILURE;
1278 }
1279 }
1280 return cp->cmd(cmdtp, flag, argc, argv);
Andy Flemingad347bb2008-10-30 16:41:01 -05001281}
1282
1283U_BOOT_CMD(
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001284 mmc, 29, 1, do_mmcops,
Mike Frysinger27e1e622009-03-23 22:27:34 -04001285 "MMC sub system",
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001286 "info - display info of the current MMC device\n"
1287 "mmc read addr blk# cnt\n"
Lei Wend430d7c2011-05-02 16:26:25 +00001288 "mmc write addr blk# cnt\n"
Alex Kiernanc568bcb2018-05-29 15:30:52 +00001289#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar814a9752018-04-06 12:05:24 +05301290 "mmc swrite addr blk#\n"
1291#endif
Lei Wenea526762011-06-22 17:03:31 +00001292 "mmc erase blk# cnt\n"
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301293 "mmc rescan [mode]\n"
Lei Wend430d7c2011-05-02 16:26:25 +00001294 "mmc part - lists available partition on current mmc device\n"
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301295 "mmc dev [dev] [part] [mode] - show or set current mmc device [partition] and set mode\n"
1296 " - the required speed mode is passed as the index from the following list\n"
1297 " [MMC_LEGACY, MMC_HS, SD_HS, MMC_HS_52, MMC_DDR_52, UHS_SDR12, UHS_SDR25,\n"
1298 " UHS_SDR50, UHS_DDR50, UHS_SDR104, MMC_HS_200, MMC_HS_400, MMC_HS_400_ES]\n"
Amar3ccad532013-04-27 11:43:00 +05301299 "mmc list - lists available devices\n"
Ying-Chun Liu (PaulLiu)f0388122022-04-25 21:59:03 +08001300 "mmc wp [PART] - power on write protect boot partitions\n"
1301 " arguments:\n"
1302 " PART - [0|1]\n"
1303 " : 0 - first boot partition, 1 - second boot partition\n"
1304 " if not assigned, write protect all boot partitions\n"
Alex Kiernan72d80462018-06-11 16:20:09 +00001305#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Jaehoon Chung94a47572021-02-26 16:07:18 +09001306 "mmc hwpartition <USER> <GP> <MODE> - does hardware partitioning\n"
Diego Santa Cruz420fc162014-12-23 10:50:30 +01001307 " arguments (sizes in 512-byte blocks):\n"
Jaehoon Chung94a47572021-02-26 16:07:18 +09001308 " USER - <user> <enh> <start> <cnt> <wrrel> <{on|off}>\n"
1309 " : sets user data area attributes\n"
1310 " GP - <{gp1|gp2|gp3|gp4}> <cnt> <enh> <wrrel> <{on|off}>\n"
1311 " : general purpose partition\n"
1312 " MODE - <{check|set|complete}>\n"
1313 " : mode, complete set partitioning completed\n"
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001314 " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
1315 " Power cycling is required to initialize partitions after set to complete.\n"
Alex Kiernan72d80462018-06-11 16:20:09 +00001316#endif
Amar3ccad532013-04-27 11:43:00 +05301317#ifdef CONFIG_SUPPORT_EMMC_BOOT
Jaehoon Chung8460b912020-12-16 07:24:50 +09001318 "mmc bootbus <dev> <boot_bus_width> <reset_boot_bus_width> <boot_mode>\n"
Tom Rini4cf854c2014-02-05 10:24:22 -05001319 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rini79919462014-02-05 10:24:20 -05001320 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
1321 " - Change sizes of boot and RPMB partitions of specified device\n"
Reuben Dowle55a81ad2021-05-14 10:15:43 +12001322 "mmc partconf <dev> [[varname] | [<boot_ack> <boot_partition> <partition_access>]]\n"
Angelo Dureghellof54f7532017-08-01 14:27:10 +02001323 " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
Reuben Dowle55a81ad2021-05-14 10:15:43 +12001324 " If showing the bits, optionally store the boot_partition field into varname\n"
Jaehoon Chung8460b912020-12-16 07:24:50 +09001325 "mmc rst-function <dev> <value>\n"
Tom Rini35a3ea12014-02-07 14:15:20 -05001326 " - Change the RST_n_FUNCTION field of the specified device\n"
1327 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behmedf8852a2009-02-18 19:59:39 +01001328#endif
Alex Kiernan60e0f612018-05-08 04:43:31 +00001329#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001330 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
1331 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
1332 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
1333 "mmc rpmb counter - read the value of the write counter\n"
1334#endif
1335 "mmc setdsr <value> - set DSR register value\n"
Tomas Melinc17dae52016-11-25 11:01:03 +02001336#ifdef CONFIG_CMD_BKOPS_ENABLE
1337 "mmc bkops-enable <dev> - enable background operations handshake on device\n"
1338 " WARNING: This is a write-once setting.\n"
Marek Vasutefdeed62023-01-05 15:19:08 +01001339 "mmc bkops <dev> [auto|manual] [enable|disable]\n"
1340 " - configure background operations handshake on device\n"
Tomas Melinc17dae52016-11-25 11:01:03 +02001341#endif
Marek Vasut1f692e32023-10-31 13:20:17 +01001342#if CONFIG_IS_ENABLED(CMD_MMC_REG)
1343 "mmc reg read <reg> <offset> [env] - read card register <reg> offset <offset>\n"
1344 " (optionally into [env] variable)\n"
1345 " - reg: cid/csd/dsr/ocr/rca/extcsd\n"
1346 " - offset: for cid/csd [0..3], for extcsd [0..511,all]\n"
1347#endif
Amar3ccad532013-04-27 11:43:00 +05301348 );
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001349
1350/* Old command kept for compatibility. Same as 'mmc info' */
1351U_BOOT_CMD(
1352 mmcinfo, 1, 0, do_mmcinfo,
1353 "display MMC info",
1354 "- display info of the current MMC device"
1355);