blob: 97e5d91859c9fc9e2ec200fcc128c4c0d1a4b5a6 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk71f95112003-06-15 22:40:42 +00002/*
3 * (C) Copyright 2003
4 * Kyle Harris, kharris@nexus-tech.net
wdenk71f95112003-06-15 22:40:42 +00005 */
6
7#include <common.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -06008#include <blk.h>
wdenk71f95112003-06-15 22:40:42 +00009#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070010#include <console.h>
wdenk71f95112003-06-15 22:40:42 +000011#include <mmc.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060012#include <part.h>
Jassi Brar732bc7c2018-04-06 12:05:24 +053013#include <sparse_format.h>
14#include <image-sparse.h>
wdenk71f95112003-06-15 22:40:42 +000015
Mike Frysinger02e22c22009-06-14 21:35:21 -040016static int curr_device = -1;
Andy Fleming272cc702008-10-30 16:41:01 -050017
18static void print_mmcinfo(struct mmc *mmc)
19{
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010020 int i;
21
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020022 printf("Device: %s\n", mmc->cfg->name);
Andy Fleming272cc702008-10-30 16:41:01 -050023 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
24 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
25 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
26 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
27 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
28
Jean-Jacques Hiblot7a96ec72017-09-21 16:29:56 +020029 printf("Bus Speed: %d\n", mmc->clock);
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +010030#if CONFIG_IS_ENABLED(MMC_VERBOSE)
Marek Vasut41e30dc2019-03-18 04:49:21 +010031 printf("Mode: %s\n", mmc_mode_name(mmc->selected_mode));
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +010032 mmc_dump_capabilities("card capabilities", mmc->card_caps);
33 mmc_dump_capabilities("host capabilities", mmc->host_caps);
34#endif
Andy Fleming272cc702008-10-30 16:41:01 -050035 printf("Rd Block Len: %d\n", mmc->read_bl_len);
36
Pantelis Antoniou4b7cee52015-01-23 12:12:01 +020037 printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
38 EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
39 EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
40 if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
41 printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
42 printf("\n");
Andy Fleming272cc702008-10-30 16:41:01 -050043
44 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang940e0782011-01-04 01:04:19 +000045 puts("Capacity: ");
46 print_size(mmc->capacity, "\n");
Andy Fleming272cc702008-10-30 16:41:01 -050047
Andrew Gabbasov786e8f82014-12-01 06:59:09 -060048 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
49 mmc->ddr_mode ? " DDR" : "");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010050
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +010051#if CONFIG_IS_ENABLED(MMC_WRITE)
Diego Santa Cruzb0361522014-12-23 10:50:26 +010052 puts("Erase Group Size: ");
53 print_size(((u64)mmc->erase_grp_size) << 9, "\n");
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +010054#endif
Diego Santa Cruzb0361522014-12-23 10:50:26 +010055
Diego Santa Cruz525ada22014-12-23 10:50:19 +010056 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +010057 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
Diego Santa Cruzbeb98a12014-12-23 10:50:23 +010058 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
Heinrich Schuchardtd5210e42020-03-30 07:24:18 +020059 u8 wp, ext_csd[MMC_MAX_BLOCK_LEN];
60 int ret;
Diego Santa Cruzb0361522014-12-23 10:50:26 +010061
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +010062#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruzb0361522014-12-23 10:50:26 +010063 puts("HC WP Group Size: ");
64 print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +010065#endif
Diego Santa Cruzb0361522014-12-23 10:50:26 +010066
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010067 puts("User Capacity: ");
Diego Santa Cruz9e41a002014-12-23 10:50:33 +010068 print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
69 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
70 puts(" WRREL\n");
71 else
72 putc('\n');
Diego Santa Cruzbeb98a12014-12-23 10:50:23 +010073 if (usr_enh) {
74 puts("User Enhanced Start: ");
75 print_size(mmc->enh_user_start, "\n");
76 puts("User Enhanced Size: ");
77 print_size(mmc->enh_user_size, "\n");
78 }
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010079 puts("Boot Capacity: ");
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +010080 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010081 puts("RPMB Capacity: ");
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +010082 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
Diego Santa Cruzb0361522014-12-23 10:50:26 +010083
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010084 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +010085 bool is_enh = has_enh &&
86 (mmc->part_attr & EXT_CSD_ENH_GP(i));
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010087 if (mmc->capacity_gp[i]) {
Diego Santa Cruzf289fd72014-12-23 10:50:18 +010088 printf("GP%i Capacity: ", i+1);
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +010089 print_size(mmc->capacity_gp[i],
Diego Santa Cruz9e41a002014-12-23 10:50:33 +010090 is_enh ? " ENH" : "");
91 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
92 puts(" WRREL\n");
93 else
94 putc('\n');
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010095 }
96 }
Heinrich Schuchardtd5210e42020-03-30 07:24:18 +020097 ret = mmc_send_ext_csd(mmc, ext_csd);
98 if (ret)
99 return;
100 wp = ext_csd[EXT_CSD_BOOT_WP_STATUS];
101 for (i = 0; i < 2; ++i) {
102 printf("Boot area %d is ", i);
103 switch (wp & 3) {
104 case 0:
105 printf("not write protected\n");
106 break;
107 case 1:
108 printf("power on protected\n");
109 break;
110 case 2:
111 printf("permanently protected\n");
112 break;
113 default:
114 printf("in reserved protection state\n");
115 break;
116 }
117 wp >>= 2;
118 }
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100119 }
Andy Fleming272cc702008-10-30 16:41:01 -0500120}
Stephen Warren1ae24a52014-05-23 13:24:45 -0600121static struct mmc *init_mmc_device(int dev, bool force_init)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200122{
123 struct mmc *mmc;
124 mmc = find_mmc_device(dev);
125 if (!mmc) {
126 printf("no mmc device at slot %x\n", dev);
127 return NULL;
128 }
Eric Nelsonbcfde7f2016-03-27 12:00:14 -0700129
Marek Vasutd2a08362019-01-03 22:09:43 +0100130 if (!mmc_getcd(mmc))
131 force_init = true;
132
Stephen Warren1ae24a52014-05-23 13:24:45 -0600133 if (force_init)
134 mmc->has_init = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200135 if (mmc_init(mmc))
136 return NULL;
Marek Vasut1d044d32019-01-03 22:09:44 +0100137
138#ifdef CONFIG_BLOCK_CACHE
139 struct blk_desc *bd = mmc_get_blk_desc(mmc);
140 blkcache_invalidate(bd->if_type, bd->devnum);
141#endif
142
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200143 return mmc;
144}
Simon Glass09140112020-05-10 11:40:03 -0600145
146static int do_mmcinfo(struct cmd_tbl *cmdtp, int flag, int argc,
147 char *const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500148{
149 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500150
Lei Wenea6ebe22011-05-02 16:26:25 +0000151 if (curr_device < 0) {
152 if (get_mmc_num() > 0)
153 curr_device = 0;
154 else {
155 puts("No MMC device available\n");
156 return 1;
157 }
158 }
Andy Fleming272cc702008-10-30 16:41:01 -0500159
Stephen Warren1ae24a52014-05-23 13:24:45 -0600160 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200161 if (!mmc)
162 return CMD_RET_FAILURE;
Andy Fleming272cc702008-10-30 16:41:01 -0500163
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200164 print_mmcinfo(mmc);
165 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500166}
167
Alex Kiernan5a7b11e2018-05-08 04:43:31 +0000168#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200169static int confirm_key_prog(void)
170{
171 puts("Warning: Programming authentication key can be done only once !\n"
172 " Use this command only if you are sure of what you are doing,\n"
173 "Really perform the key programming? <y/N> ");
174 if (confirm_yesno())
175 return 1;
176
177 puts("Authentication key programming aborted\n");
178 return 0;
179}
Simon Glass09140112020-05-10 11:40:03 -0600180
181static int do_mmcrpmb_key(struct cmd_tbl *cmdtp, int flag,
182 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200183{
184 void *key_addr;
185 struct mmc *mmc = find_mmc_device(curr_device);
186
187 if (argc != 2)
188 return CMD_RET_USAGE;
189
190 key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
191 if (!confirm_key_prog())
192 return CMD_RET_FAILURE;
193 if (mmc_rpmb_set_key(mmc, key_addr)) {
194 printf("ERROR - Key already programmed ?\n");
195 return CMD_RET_FAILURE;
196 }
197 return CMD_RET_SUCCESS;
198}
Simon Glass09140112020-05-10 11:40:03 -0600199
200static int do_mmcrpmb_read(struct cmd_tbl *cmdtp, int flag,
201 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200202{
203 u16 blk, cnt;
204 void *addr;
205 int n;
206 void *key_addr = NULL;
207 struct mmc *mmc = find_mmc_device(curr_device);
208
209 if (argc < 4)
210 return CMD_RET_USAGE;
211
212 addr = (void *)simple_strtoul(argv[1], NULL, 16);
213 blk = simple_strtoul(argv[2], NULL, 16);
214 cnt = simple_strtoul(argv[3], NULL, 16);
215
216 if (argc == 5)
217 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
218
219 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
220 curr_device, blk, cnt);
221 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
222
223 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
224 if (n != cnt)
225 return CMD_RET_FAILURE;
226 return CMD_RET_SUCCESS;
227}
Simon Glass09140112020-05-10 11:40:03 -0600228
229static int do_mmcrpmb_write(struct cmd_tbl *cmdtp, int flag,
230 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200231{
232 u16 blk, cnt;
233 void *addr;
234 int n;
235 void *key_addr;
236 struct mmc *mmc = find_mmc_device(curr_device);
237
238 if (argc != 5)
239 return CMD_RET_USAGE;
240
241 addr = (void *)simple_strtoul(argv[1], NULL, 16);
242 blk = simple_strtoul(argv[2], NULL, 16);
243 cnt = simple_strtoul(argv[3], NULL, 16);
244 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
245
246 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
247 curr_device, blk, cnt);
248 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
249
250 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
251 if (n != cnt)
252 return CMD_RET_FAILURE;
253 return CMD_RET_SUCCESS;
254}
Simon Glass09140112020-05-10 11:40:03 -0600255
256static int do_mmcrpmb_counter(struct cmd_tbl *cmdtp, int flag,
257 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200258{
259 unsigned long counter;
260 struct mmc *mmc = find_mmc_device(curr_device);
261
262 if (mmc_rpmb_get_counter(mmc, &counter))
263 return CMD_RET_FAILURE;
264 printf("RPMB Write counter= %lx\n", counter);
265 return CMD_RET_SUCCESS;
266}
267
Simon Glass09140112020-05-10 11:40:03 -0600268static struct cmd_tbl cmd_rpmb[] = {
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200269 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
270 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
271 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
272 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
273};
274
Simon Glass09140112020-05-10 11:40:03 -0600275static int do_mmcrpmb(struct cmd_tbl *cmdtp, int flag,
276 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200277{
Simon Glass09140112020-05-10 11:40:03 -0600278 struct cmd_tbl *cp;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200279 struct mmc *mmc;
280 char original_part;
281 int ret;
282
283 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
284
285 /* Drop the rpmb subcommand */
286 argc--;
287 argv++;
288
289 if (cp == NULL || argc > cp->maxargs)
290 return CMD_RET_USAGE;
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100291 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200292 return CMD_RET_SUCCESS;
293
Stephen Warren1ae24a52014-05-23 13:24:45 -0600294 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200295 if (!mmc)
296 return CMD_RET_FAILURE;
297
298 if (!(mmc->version & MMC_VERSION_MMC)) {
Heinrich Schuchardt71a3e5c2020-03-29 19:26:57 +0000299 printf("It is not an eMMC device\n");
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200300 return CMD_RET_FAILURE;
301 }
302 if (mmc->version < MMC_VERSION_4_41) {
303 printf("RPMB not supported before version 4.41\n");
304 return CMD_RET_FAILURE;
305 }
306 /* Switch to the RPMB partition */
Kever Yang4dc80c82017-06-08 09:20:03 +0800307#ifndef CONFIG_BLK
Marek Vasutb955e422016-05-04 16:35:25 +0200308 original_part = mmc->block_dev.hwpart;
Kever Yang4dc80c82017-06-08 09:20:03 +0800309#else
310 original_part = mmc_get_blk_desc(mmc)->hwpart;
311#endif
Simon Glass69f45cd2016-05-01 13:52:29 -0600312 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, MMC_PART_RPMB) !=
313 0)
Stephen Warren873cc1d2015-12-07 11:38:49 -0700314 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200315 ret = cp->cmd(cmdtp, flag, argc, argv);
316
317 /* Return to original partition */
Simon Glass69f45cd2016-05-01 13:52:29 -0600318 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, original_part) !=
319 0)
Stephen Warren873cc1d2015-12-07 11:38:49 -0700320 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200321 return ret;
322}
323#endif
324
Simon Glass09140112020-05-10 11:40:03 -0600325static int do_mmc_read(struct cmd_tbl *cmdtp, int flag,
326 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200327{
328 struct mmc *mmc;
329 u32 blk, cnt, n;
330 void *addr;
331
332 if (argc != 4)
333 return CMD_RET_USAGE;
334
335 addr = (void *)simple_strtoul(argv[1], NULL, 16);
336 blk = simple_strtoul(argv[2], NULL, 16);
337 cnt = simple_strtoul(argv[3], NULL, 16);
338
Stephen Warren1ae24a52014-05-23 13:24:45 -0600339 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200340 if (!mmc)
341 return CMD_RET_FAILURE;
342
343 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
344 curr_device, blk, cnt);
345
Simon Glassc40fdca2016-05-01 13:52:35 -0600346 n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200347 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
348
349 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
350}
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +0100351
Alex Kiernanc232d142018-05-29 15:30:52 +0000352#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar732bc7c2018-04-06 12:05:24 +0530353static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk,
354 lbaint_t blkcnt, const void *buffer)
355{
356 struct blk_desc *dev_desc = info->priv;
357
358 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
359}
360
361static lbaint_t mmc_sparse_reserve(struct sparse_storage *info,
362 lbaint_t blk, lbaint_t blkcnt)
363{
364 return blkcnt;
365}
366
Simon Glass09140112020-05-10 11:40:03 -0600367static int do_mmc_sparse_write(struct cmd_tbl *cmdtp, int flag,
368 int argc, char *const argv[])
Jassi Brar732bc7c2018-04-06 12:05:24 +0530369{
370 struct sparse_storage sparse;
371 struct blk_desc *dev_desc;
372 struct mmc *mmc;
373 char dest[11];
374 void *addr;
375 u32 blk;
376
377 if (argc != 3)
378 return CMD_RET_USAGE;
379
380 addr = (void *)simple_strtoul(argv[1], NULL, 16);
381 blk = simple_strtoul(argv[2], NULL, 16);
382
383 if (!is_sparse_image(addr)) {
384 printf("Not a sparse image\n");
385 return CMD_RET_FAILURE;
386 }
387
388 mmc = init_mmc_device(curr_device, false);
389 if (!mmc)
390 return CMD_RET_FAILURE;
391
392 printf("\nMMC Sparse write: dev # %d, block # %d ... ",
393 curr_device, blk);
394
395 if (mmc_getwp(mmc) == 1) {
396 printf("Error: card is write protected!\n");
397 return CMD_RET_FAILURE;
398 }
399
400 dev_desc = mmc_get_blk_desc(mmc);
401 sparse.priv = dev_desc;
402 sparse.blksz = 512;
403 sparse.start = blk;
404 sparse.size = dev_desc->lba - blk;
405 sparse.write = mmc_sparse_write;
406 sparse.reserve = mmc_sparse_reserve;
407 sparse.mssg = NULL;
408 sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz);
409
Alex Kiernanc4ded032018-05-29 15:30:40 +0000410 if (write_sparse_image(&sparse, dest, addr, NULL))
Jassi Brar732bc7c2018-04-06 12:05:24 +0530411 return CMD_RET_FAILURE;
412 else
413 return CMD_RET_SUCCESS;
414}
415#endif
416
Alex Kiernanc232d142018-05-29 15:30:52 +0000417#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glass09140112020-05-10 11:40:03 -0600418static int do_mmc_write(struct cmd_tbl *cmdtp, int flag,
419 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200420{
421 struct mmc *mmc;
422 u32 blk, cnt, n;
423 void *addr;
424
425 if (argc != 4)
426 return CMD_RET_USAGE;
427
428 addr = (void *)simple_strtoul(argv[1], NULL, 16);
429 blk = simple_strtoul(argv[2], NULL, 16);
430 cnt = simple_strtoul(argv[3], NULL, 16);
431
Stephen Warren1ae24a52014-05-23 13:24:45 -0600432 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200433 if (!mmc)
434 return CMD_RET_FAILURE;
435
436 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
437 curr_device, blk, cnt);
438
439 if (mmc_getwp(mmc) == 1) {
440 printf("Error: card is write protected!\n");
441 return CMD_RET_FAILURE;
442 }
Simon Glassc40fdca2016-05-01 13:52:35 -0600443 n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200444 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
445
446 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
447}
Simon Glass09140112020-05-10 11:40:03 -0600448
449static int do_mmc_erase(struct cmd_tbl *cmdtp, int flag,
450 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200451{
452 struct mmc *mmc;
453 u32 blk, cnt, n;
454
455 if (argc != 3)
456 return CMD_RET_USAGE;
457
458 blk = simple_strtoul(argv[1], NULL, 16);
459 cnt = simple_strtoul(argv[2], NULL, 16);
460
Stephen Warren1ae24a52014-05-23 13:24:45 -0600461 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200462 if (!mmc)
463 return CMD_RET_FAILURE;
464
465 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
466 curr_device, blk, cnt);
467
468 if (mmc_getwp(mmc) == 1) {
469 printf("Error: card is write protected!\n");
470 return CMD_RET_FAILURE;
471 }
Simon Glassc40fdca2016-05-01 13:52:35 -0600472 n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200473 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
474
475 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
476}
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +0100477#endif
478
Simon Glass09140112020-05-10 11:40:03 -0600479static int do_mmc_rescan(struct cmd_tbl *cmdtp, int flag,
480 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200481{
482 struct mmc *mmc;
483
Stephen Warren941944e2014-05-23 13:24:46 -0600484 mmc = init_mmc_device(curr_device, true);
485 if (!mmc)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200486 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200487
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200488 return CMD_RET_SUCCESS;
489}
Simon Glass09140112020-05-10 11:40:03 -0600490
491static int do_mmc_part(struct cmd_tbl *cmdtp, int flag,
492 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200493{
Simon Glass4101f682016-02-29 15:25:34 -0700494 struct blk_desc *mmc_dev;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200495 struct mmc *mmc;
496
Stephen Warren1ae24a52014-05-23 13:24:45 -0600497 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200498 if (!mmc)
499 return CMD_RET_FAILURE;
500
Simon Glass3c457f42016-05-01 11:36:15 -0600501 mmc_dev = blk_get_devnum_by_type(IF_TYPE_MMC, curr_device);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200502 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
Simon Glass3e8bd462016-02-29 15:25:48 -0700503 part_print(mmc_dev);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200504 return CMD_RET_SUCCESS;
505 }
506
507 puts("get mmc type error!\n");
508 return CMD_RET_FAILURE;
509}
Simon Glass09140112020-05-10 11:40:03 -0600510
511static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag,
512 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200513{
Stephen Warren60dc58f2014-05-23 12:48:10 -0600514 int dev, part = 0, ret;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200515 struct mmc *mmc;
516
517 if (argc == 1) {
518 dev = curr_device;
519 } else if (argc == 2) {
520 dev = simple_strtoul(argv[1], NULL, 10);
521 } else if (argc == 3) {
522 dev = (int)simple_strtoul(argv[1], NULL, 10);
523 part = (int)simple_strtoul(argv[2], NULL, 10);
524 if (part > PART_ACCESS_MASK) {
525 printf("#part_num shouldn't be larger than %d\n",
526 PART_ACCESS_MASK);
527 return CMD_RET_FAILURE;
528 }
529 } else {
530 return CMD_RET_USAGE;
531 }
532
Stephen Warrena5710922014-05-23 13:24:47 -0600533 mmc = init_mmc_device(dev, true);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200534 if (!mmc)
535 return CMD_RET_FAILURE;
536
Simon Glass69f45cd2016-05-01 13:52:29 -0600537 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
Stephen Warren60dc58f2014-05-23 12:48:10 -0600538 printf("switch to partitions #%d, %s\n",
539 part, (!ret) ? "OK" : "ERROR");
540 if (ret)
541 return 1;
542
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200543 curr_device = dev;
544 if (mmc->part_config == MMCPART_NOAVAILABLE)
545 printf("mmc%d is current device\n", curr_device);
546 else
547 printf("mmc%d(part %d) is current device\n",
Simon Glassc40fdca2016-05-01 13:52:35 -0600548 curr_device, mmc_get_blk_desc(mmc)->hwpart);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200549
550 return CMD_RET_SUCCESS;
551}
Simon Glass09140112020-05-10 11:40:03 -0600552
553static int do_mmc_list(struct cmd_tbl *cmdtp, int flag,
554 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200555{
556 print_mmc_devices('\n');
557 return CMD_RET_SUCCESS;
558}
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100559
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +0100560#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100561static int parse_hwpart_user(struct mmc_hwpart_conf *pconf,
Simon Glass09140112020-05-10 11:40:03 -0600562 int argc, char *const argv[])
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100563{
564 int i = 0;
565
566 memset(&pconf->user, 0, sizeof(pconf->user));
567
568 while (i < argc) {
569 if (!strcmp(argv[i], "enh")) {
570 if (i + 2 >= argc)
571 return -1;
572 pconf->user.enh_start =
573 simple_strtoul(argv[i+1], NULL, 10);
574 pconf->user.enh_size =
575 simple_strtoul(argv[i+2], NULL, 10);
576 i += 3;
577 } else if (!strcmp(argv[i], "wrrel")) {
578 if (i + 1 >= argc)
579 return -1;
580 pconf->user.wr_rel_change = 1;
581 if (!strcmp(argv[i+1], "on"))
582 pconf->user.wr_rel_set = 1;
583 else if (!strcmp(argv[i+1], "off"))
584 pconf->user.wr_rel_set = 0;
585 else
586 return -1;
587 i += 2;
588 } else {
589 break;
590 }
591 }
592 return i;
593}
594
595static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
Simon Glass09140112020-05-10 11:40:03 -0600596 int argc, char *const argv[])
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100597{
598 int i;
599
600 memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
601
602 if (1 >= argc)
603 return -1;
604 pconf->gp_part[pidx].size = simple_strtoul(argv[0], NULL, 10);
605
606 i = 1;
607 while (i < argc) {
608 if (!strcmp(argv[i], "enh")) {
609 pconf->gp_part[pidx].enhanced = 1;
610 i += 1;
611 } else if (!strcmp(argv[i], "wrrel")) {
612 if (i + 1 >= argc)
613 return -1;
614 pconf->gp_part[pidx].wr_rel_change = 1;
615 if (!strcmp(argv[i+1], "on"))
616 pconf->gp_part[pidx].wr_rel_set = 1;
617 else if (!strcmp(argv[i+1], "off"))
618 pconf->gp_part[pidx].wr_rel_set = 0;
619 else
620 return -1;
621 i += 2;
622 } else {
623 break;
624 }
625 }
626 return i;
627}
628
Simon Glass09140112020-05-10 11:40:03 -0600629static int do_mmc_hwpartition(struct cmd_tbl *cmdtp, int flag,
630 int argc, char *const argv[])
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100631{
632 struct mmc *mmc;
633 struct mmc_hwpart_conf pconf = { };
634 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100635 int i, r, pidx;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100636
637 mmc = init_mmc_device(curr_device, false);
638 if (!mmc)
639 return CMD_RET_FAILURE;
640
641 if (argc < 1)
642 return CMD_RET_USAGE;
643 i = 1;
644 while (i < argc) {
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100645 if (!strcmp(argv[i], "user")) {
646 i++;
647 r = parse_hwpart_user(&pconf, argc-i, &argv[i]);
648 if (r < 0)
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100649 return CMD_RET_USAGE;
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100650 i += r;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100651 } else if (!strncmp(argv[i], "gp", 2) &&
652 strlen(argv[i]) == 3 &&
653 argv[i][2] >= '1' && argv[i][2] <= '4') {
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100654 pidx = argv[i][2] - '1';
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100655 i++;
656 r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
657 if (r < 0)
658 return CMD_RET_USAGE;
659 i += r;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100660 } else if (!strcmp(argv[i], "check")) {
661 mode = MMC_HWPART_CONF_CHECK;
662 i++;
663 } else if (!strcmp(argv[i], "set")) {
664 mode = MMC_HWPART_CONF_SET;
665 i++;
666 } else if (!strcmp(argv[i], "complete")) {
667 mode = MMC_HWPART_CONF_COMPLETE;
668 i++;
669 } else {
670 return CMD_RET_USAGE;
671 }
672 }
673
674 puts("Partition configuration:\n");
675 if (pconf.user.enh_size) {
676 puts("\tUser Enhanced Start: ");
677 print_size(((u64)pconf.user.enh_start) << 9, "\n");
678 puts("\tUser Enhanced Size: ");
679 print_size(((u64)pconf.user.enh_size) << 9, "\n");
680 } else {
681 puts("\tNo enhanced user data area\n");
682 }
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100683 if (pconf.user.wr_rel_change)
684 printf("\tUser partition write reliability: %s\n",
685 pconf.user.wr_rel_set ? "on" : "off");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100686 for (pidx = 0; pidx < 4; pidx++) {
687 if (pconf.gp_part[pidx].size) {
688 printf("\tGP%i Capacity: ", pidx+1);
689 print_size(((u64)pconf.gp_part[pidx].size) << 9,
690 pconf.gp_part[pidx].enhanced ?
691 " ENH\n" : "\n");
692 } else {
693 printf("\tNo GP%i partition\n", pidx+1);
694 }
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100695 if (pconf.gp_part[pidx].wr_rel_change)
696 printf("\tGP%i write reliability: %s\n", pidx+1,
697 pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100698 }
699
700 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
701 if (mode == MMC_HWPART_CONF_COMPLETE)
702 puts("Partitioning successful, "
703 "power-cycle to make effective\n");
704 return CMD_RET_SUCCESS;
705 } else {
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100706 puts("Failed!\n");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100707 return CMD_RET_FAILURE;
708 }
709}
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +0100710#endif
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100711
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200712#ifdef CONFIG_SUPPORT_EMMC_BOOT
Simon Glass09140112020-05-10 11:40:03 -0600713static int do_mmc_bootbus(struct cmd_tbl *cmdtp, int flag,
714 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200715{
716 int dev;
717 struct mmc *mmc;
718 u8 width, reset, mode;
719
720 if (argc != 5)
721 return CMD_RET_USAGE;
722 dev = simple_strtoul(argv[1], NULL, 10);
723 width = simple_strtoul(argv[2], NULL, 10);
724 reset = simple_strtoul(argv[3], NULL, 10);
725 mode = simple_strtoul(argv[4], NULL, 10);
726
Stephen Warren1ae24a52014-05-23 13:24:45 -0600727 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200728 if (!mmc)
729 return CMD_RET_FAILURE;
730
731 if (IS_SD(mmc)) {
732 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
733 return CMD_RET_FAILURE;
734 }
735
736 /* acknowledge to be sent during boot operation */
737 return mmc_set_boot_bus_width(mmc, width, reset, mode);
738}
Simon Glass09140112020-05-10 11:40:03 -0600739
740static int do_mmc_boot_resize(struct cmd_tbl *cmdtp, int flag,
741 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200742{
743 int dev;
744 struct mmc *mmc;
745 u32 bootsize, rpmbsize;
746
747 if (argc != 4)
748 return CMD_RET_USAGE;
749 dev = simple_strtoul(argv[1], NULL, 10);
750 bootsize = simple_strtoul(argv[2], NULL, 10);
751 rpmbsize = simple_strtoul(argv[3], NULL, 10);
752
Stephen Warren1ae24a52014-05-23 13:24:45 -0600753 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200754 if (!mmc)
755 return CMD_RET_FAILURE;
756
757 if (IS_SD(mmc)) {
Heinrich Schuchardt71a3e5c2020-03-29 19:26:57 +0000758 printf("It is not an eMMC device\n");
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200759 return CMD_RET_FAILURE;
760 }
761
762 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
763 printf("EMMC boot partition Size change Failed.\n");
764 return CMD_RET_FAILURE;
765 }
766
767 printf("EMMC boot partition Size %d MB\n", bootsize);
768 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
769 return CMD_RET_SUCCESS;
770}
Angelo Dureghellobdb60992017-08-01 14:27:10 +0200771
772static int mmc_partconf_print(struct mmc *mmc)
773{
774 u8 ack, access, part;
775
776 if (mmc->part_config == MMCPART_NOAVAILABLE) {
777 printf("No part_config info for ver. 0x%x\n", mmc->version);
778 return CMD_RET_FAILURE;
779 }
780
781 access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
782 ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
783 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
784
785 printf("EXT_CSD[179], PARTITION_CONFIG:\n"
786 "BOOT_ACK: 0x%x\n"
787 "BOOT_PARTITION_ENABLE: 0x%x\n"
788 "PARTITION_ACCESS: 0x%x\n", ack, part, access);
789
790 return CMD_RET_SUCCESS;
791}
792
Simon Glass09140112020-05-10 11:40:03 -0600793static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag,
794 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200795{
796 int dev;
797 struct mmc *mmc;
798 u8 ack, part_num, access;
799
Angelo Dureghellobdb60992017-08-01 14:27:10 +0200800 if (argc != 2 && argc != 5)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200801 return CMD_RET_USAGE;
802
803 dev = simple_strtoul(argv[1], NULL, 10);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200804
Stephen Warren1ae24a52014-05-23 13:24:45 -0600805 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200806 if (!mmc)
807 return CMD_RET_FAILURE;
808
809 if (IS_SD(mmc)) {
810 puts("PARTITION_CONFIG only exists on eMMC\n");
811 return CMD_RET_FAILURE;
812 }
813
Angelo Dureghellobdb60992017-08-01 14:27:10 +0200814 if (argc == 2)
815 return mmc_partconf_print(mmc);
816
817 ack = simple_strtoul(argv[2], NULL, 10);
818 part_num = simple_strtoul(argv[3], NULL, 10);
819 access = simple_strtoul(argv[4], NULL, 10);
820
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200821 /* acknowledge to be sent during boot operation */
822 return mmc_set_part_conf(mmc, ack, part_num, access);
823}
Simon Glass09140112020-05-10 11:40:03 -0600824
825static int do_mmc_rst_func(struct cmd_tbl *cmdtp, int flag,
826 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200827{
828 int dev;
829 struct mmc *mmc;
830 u8 enable;
831
832 /*
833 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
834 * The only valid values are 0x0, 0x1 and 0x2 and writing
835 * a value of 0x1 or 0x2 sets the value permanently.
836 */
837 if (argc != 3)
838 return CMD_RET_USAGE;
839
840 dev = simple_strtoul(argv[1], NULL, 10);
841 enable = simple_strtoul(argv[2], NULL, 10);
842
Peng Fan678e9312015-11-25 17:16:21 +0800843 if (enable > 2) {
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200844 puts("Invalid RST_n_ENABLE value\n");
845 return CMD_RET_USAGE;
846 }
847
Stephen Warren1ae24a52014-05-23 13:24:45 -0600848 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200849 if (!mmc)
850 return CMD_RET_FAILURE;
851
852 if (IS_SD(mmc)) {
853 puts("RST_n_FUNCTION only exists on eMMC\n");
854 return CMD_RET_FAILURE;
855 }
856
857 return mmc_set_rst_n_function(mmc, enable);
858}
859#endif
Simon Glass09140112020-05-10 11:40:03 -0600860static int do_mmc_setdsr(struct cmd_tbl *cmdtp, int flag,
861 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200862{
863 struct mmc *mmc;
864 u32 val;
865 int ret;
866
867 if (argc != 2)
868 return CMD_RET_USAGE;
Markus Niebel84c1dfe2017-02-03 15:26:36 +0100869 val = simple_strtoul(argv[1], NULL, 16);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200870
871 mmc = find_mmc_device(curr_device);
872 if (!mmc) {
873 printf("no mmc device at slot %x\n", curr_device);
874 return CMD_RET_FAILURE;
875 }
876 ret = mmc_set_dsr(mmc, val);
877 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
878 if (!ret) {
879 mmc->has_init = 0;
880 if (mmc_init(mmc))
881 return CMD_RET_FAILURE;
882 else
883 return CMD_RET_SUCCESS;
884 }
885 return ret;
886}
887
Tomas Melincd3d4882016-11-25 11:01:03 +0200888#ifdef CONFIG_CMD_BKOPS_ENABLE
Simon Glass09140112020-05-10 11:40:03 -0600889static int do_mmc_bkops_enable(struct cmd_tbl *cmdtp, int flag,
890 int argc, char *const argv[])
Tomas Melincd3d4882016-11-25 11:01:03 +0200891{
892 int dev;
893 struct mmc *mmc;
894
895 if (argc != 2)
896 return CMD_RET_USAGE;
897
898 dev = simple_strtoul(argv[1], NULL, 10);
899
900 mmc = init_mmc_device(dev, false);
901 if (!mmc)
902 return CMD_RET_FAILURE;
903
904 if (IS_SD(mmc)) {
905 puts("BKOPS_EN only exists on eMMC\n");
906 return CMD_RET_FAILURE;
907 }
908
909 return mmc_set_bkops_enable(mmc);
910}
911#endif
912
Simon Glass09140112020-05-10 11:40:03 -0600913static int do_mmc_boot_wp(struct cmd_tbl *cmdtp, int flag,
Heinrich Schuchardt0469d842020-03-30 07:24:19 +0200914 int argc, char * const argv[])
915{
916 int err;
917 struct mmc *mmc;
918
919 mmc = init_mmc_device(curr_device, false);
920 if (!mmc)
921 return CMD_RET_FAILURE;
922 if (IS_SD(mmc)) {
923 printf("It is not an eMMC device\n");
924 return CMD_RET_FAILURE;
925 }
926 err = mmc_boot_wp(mmc);
927 if (err)
928 return CMD_RET_FAILURE;
929 printf("boot areas protected\n");
930 return CMD_RET_SUCCESS;
931}
932
Simon Glass09140112020-05-10 11:40:03 -0600933static struct cmd_tbl cmd_mmc[] = {
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200934 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
935 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
Heinrich Schuchardt0469d842020-03-30 07:24:19 +0200936 U_BOOT_CMD_MKENT(wp, 1, 0, do_mmc_boot_wp, "", ""),
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +0100937#if CONFIG_IS_ENABLED(MMC_WRITE)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200938 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
939 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +0100940#endif
Alex Kiernanc232d142018-05-29 15:30:52 +0000941#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
942 U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
943#endif
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200944 U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
945 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
946 U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
947 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +0100948#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100949 U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +0100950#endif
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200951#ifdef CONFIG_SUPPORT_EMMC_BOOT
952 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehfa7b8852014-09-25 23:00:16 +0800953 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200954 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
955 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
956#endif
Alex Kiernan5a7b11e2018-05-08 04:43:31 +0000957#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200958 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
959#endif
960 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
Tomas Melincd3d4882016-11-25 11:01:03 +0200961#ifdef CONFIG_CMD_BKOPS_ENABLE
962 U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
963#endif
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200964};
Andy Fleming272cc702008-10-30 16:41:01 -0500965
Simon Glass09140112020-05-10 11:40:03 -0600966static int do_mmcops(struct cmd_tbl *cmdtp, int flag, int argc,
967 char *const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500968{
Simon Glass09140112020-05-10 11:40:03 -0600969 struct cmd_tbl *cp;
Lei Wen6be95cc2011-06-22 17:03:30 +0000970
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200971 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
972
973 /* Drop the mmc command */
974 argc--;
975 argv++;
976
977 if (cp == NULL || argc > cp->maxargs)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000978 return CMD_RET_USAGE;
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100979 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200980 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500981
Lei Wenea6ebe22011-05-02 16:26:25 +0000982 if (curr_device < 0) {
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200983 if (get_mmc_num() > 0) {
Lei Wenea6ebe22011-05-02 16:26:25 +0000984 curr_device = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200985 } else {
Lei Wenea6ebe22011-05-02 16:26:25 +0000986 puts("No MMC device available\n");
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200987 return CMD_RET_FAILURE;
Lei Wenea6ebe22011-05-02 16:26:25 +0000988 }
989 }
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200990 return cp->cmd(cmdtp, flag, argc, argv);
Andy Fleming272cc702008-10-30 16:41:01 -0500991}
992
993U_BOOT_CMD(
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100994 mmc, 29, 1, do_mmcops,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400995 "MMC sub system",
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200996 "info - display info of the current MMC device\n"
997 "mmc read addr blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000998 "mmc write addr blk# cnt\n"
Alex Kiernanc232d142018-05-29 15:30:52 +0000999#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar732bc7c2018-04-06 12:05:24 +05301000 "mmc swrite addr blk#\n"
1001#endif
Lei Wene6f99a52011-06-22 17:03:31 +00001002 "mmc erase blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +00001003 "mmc rescan\n"
1004 "mmc part - lists available partition on current mmc device\n"
Lei Wenbc897b12011-05-02 16:26:26 +00001005 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
Amar2a91c912013-04-27 11:43:00 +05301006 "mmc list - lists available devices\n"
Heinrich Schuchardt0469d842020-03-30 07:24:19 +02001007 "mmc wp - power on write protect booot partitions\n"
Alex Kiernan84593672018-06-11 16:20:09 +00001008#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruzc599f532014-12-23 10:50:30 +01001009 "mmc hwpartition [args...] - does hardware partitioning\n"
1010 " arguments (sizes in 512-byte blocks):\n"
Diego Santa Cruz189f9632014-12-23 10:50:32 +01001011 " [user [enh start cnt] [wrrel {on|off}]] - sets user data area attributes\n"
1012 " [gp1|gp2|gp3|gp4 cnt [enh] [wrrel {on|off}]] - general purpose partition\n"
Diego Santa Cruzc599f532014-12-23 10:50:30 +01001013 " [check|set|complete] - mode, complete set partitioning completed\n"
Diego Santa Cruz189f9632014-12-23 10:50:32 +01001014 " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
1015 " Power cycling is required to initialize partitions after set to complete.\n"
Alex Kiernan84593672018-06-11 16:20:09 +00001016#endif
Amar2a91c912013-04-27 11:43:00 +05301017#ifdef CONFIG_SUPPORT_EMMC_BOOT
Tom Rini5a99b9d2014-02-05 10:24:22 -05001018 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
1019 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rinif1fd9572014-02-05 10:24:20 -05001020 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
1021 " - Change sizes of boot and RPMB partitions of specified device\n"
Angelo Dureghellobdb60992017-08-01 14:27:10 +02001022 "mmc partconf dev [boot_ack boot_partition partition_access]\n"
1023 " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
Tom Rini33ace362014-02-07 14:15:20 -05001024 "mmc rst-function dev value\n"
1025 " - Change the RST_n_FUNCTION field of the specified device\n"
1026 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behme3511b4e2009-02-18 19:59:39 +01001027#endif
Alex Kiernan5a7b11e2018-05-08 04:43:31 +00001028#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001029 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
1030 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
1031 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
1032 "mmc rpmb counter - read the value of the write counter\n"
1033#endif
1034 "mmc setdsr <value> - set DSR register value\n"
Tomas Melincd3d4882016-11-25 11:01:03 +02001035#ifdef CONFIG_CMD_BKOPS_ENABLE
1036 "mmc bkops-enable <dev> - enable background operations handshake on device\n"
1037 " WARNING: This is a write-once setting.\n"
1038#endif
Amar2a91c912013-04-27 11:43:00 +05301039 );
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001040
1041/* Old command kept for compatibility. Same as 'mmc info' */
1042U_BOOT_CMD(
1043 mmcinfo, 1, 0, do_mmcinfo,
1044 "display MMC info",
1045 "- display info of the current MMC device"
1046);