]> git.ipfire.org Git - people/ms/u-boot.git/blame - cmd/mmc.c
mmc: dump card and host capabilities if debug is enabled
[people/ms/u-boot.git] / cmd / mmc.c
CommitLineData
71f95118
WD
1/*
2 * (C) Copyright 2003
3 * Kyle Harris, kharris@nexus-tech.net
4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
71f95118
WD
6 */
7
8#include <common.h>
9#include <command.h>
24b852a7 10#include <console.h>
71f95118
WD
11#include <mmc.h>
12
02e22c2d 13static int curr_device = -1;
272cc70b
AF
14
15static void print_mmcinfo(struct mmc *mmc)
16{
c5f0d3f1
DSC
17 int i;
18
93bfd616 19 printf("Device: %s\n", mmc->cfg->name);
272cc70b
AF
20 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
21 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
22 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
23 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
24 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
25
7a96ec74 26 printf("Bus Speed: %d\n", mmc->clock);
52d241df 27#if CONFIG_IS_ENABLED(MMC_VERBOSE)
7a96ec74 28 printf("Mode : %s\n", mmc_mode_name(mmc->selected_mode));
52d241df
JJH
29 mmc_dump_capabilities("card capabilities", mmc->card_caps);
30 mmc_dump_capabilities("host capabilities", mmc->host_caps);
31#endif
272cc70b
AF
32 printf("Rd Block Len: %d\n", mmc->read_bl_len);
33
4b7cee53
PA
34 printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
35 EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
36 EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
37 if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
38 printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
39 printf("\n");
272cc70b
AF
40
41 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
940e0782
MK
42 puts("Capacity: ");
43 print_size(mmc->capacity, "\n");
272cc70b 44
786e8f81
AG
45 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
46 mmc->ddr_mode ? " DDR" : "");
c5f0d3f1 47
b0361526
DSC
48 puts("Erase Group Size: ");
49 print_size(((u64)mmc->erase_grp_size) << 9, "\n");
50
525ada21 51 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
c3dbb4f9 52 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
beb98a14 53 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
b0361526
DSC
54
55 puts("HC WP Group Size: ");
56 print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
57
c5f0d3f1 58 puts("User Capacity: ");
9e41a00b
DSC
59 print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
60 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
61 puts(" WRREL\n");
62 else
63 putc('\n');
beb98a14
DSC
64 if (usr_enh) {
65 puts("User Enhanced Start: ");
66 print_size(mmc->enh_user_start, "\n");
67 puts("User Enhanced Size: ");
68 print_size(mmc->enh_user_size, "\n");
69 }
c5f0d3f1 70 puts("Boot Capacity: ");
c3dbb4f9 71 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
c5f0d3f1 72 puts("RPMB Capacity: ");
c3dbb4f9 73 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
b0361526 74
c5f0d3f1 75 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
c3dbb4f9
DSC
76 bool is_enh = has_enh &&
77 (mmc->part_attr & EXT_CSD_ENH_GP(i));
c5f0d3f1 78 if (mmc->capacity_gp[i]) {
f289fd73 79 printf("GP%i Capacity: ", i+1);
c3dbb4f9 80 print_size(mmc->capacity_gp[i],
9e41a00b
DSC
81 is_enh ? " ENH" : "");
82 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
83 puts(" WRREL\n");
84 else
85 putc('\n');
c5f0d3f1
DSC
86 }
87 }
88 }
272cc70b 89}
1ae24a50 90static struct mmc *init_mmc_device(int dev, bool force_init)
1fd93c6e
PA
91{
92 struct mmc *mmc;
93 mmc = find_mmc_device(dev);
94 if (!mmc) {
95 printf("no mmc device at slot %x\n", dev);
96 return NULL;
97 }
bcfde7ff 98
1ae24a50
SW
99 if (force_init)
100 mmc->has_init = 0;
1fd93c6e
PA
101 if (mmc_init(mmc))
102 return NULL;
103 return mmc;
104}
088f1b19 105static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
272cc70b
AF
106{
107 struct mmc *mmc;
272cc70b 108
ea6ebe21
LW
109 if (curr_device < 0) {
110 if (get_mmc_num() > 0)
111 curr_device = 0;
112 else {
113 puts("No MMC device available\n");
114 return 1;
115 }
116 }
272cc70b 117
1ae24a50 118 mmc = init_mmc_device(curr_device, false);
1fd93c6e
PA
119 if (!mmc)
120 return CMD_RET_FAILURE;
272cc70b 121
1fd93c6e
PA
122 print_mmcinfo(mmc);
123 return CMD_RET_SUCCESS;
124}
272cc70b 125
1fd93c6e
PA
126#ifdef CONFIG_SUPPORT_EMMC_RPMB
127static int confirm_key_prog(void)
128{
129 puts("Warning: Programming authentication key can be done only once !\n"
130 " Use this command only if you are sure of what you are doing,\n"
131 "Really perform the key programming? <y/N> ");
132 if (confirm_yesno())
ea6ebe21 133 return 1;
1fd93c6e
PA
134
135 puts("Authentication key programming aborted\n");
136 return 0;
137}
138static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag,
139 int argc, char * const argv[])
140{
141 void *key_addr;
142 struct mmc *mmc = find_mmc_device(curr_device);
143
144 if (argc != 2)
145 return CMD_RET_USAGE;
146
147 key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
148 if (!confirm_key_prog())
149 return CMD_RET_FAILURE;
150 if (mmc_rpmb_set_key(mmc, key_addr)) {
151 printf("ERROR - Key already programmed ?\n");
152 return CMD_RET_FAILURE;
272cc70b 153 }
1fd93c6e 154 return CMD_RET_SUCCESS;
272cc70b 155}
1fd93c6e
PA
156static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag,
157 int argc, char * const argv[])
158{
159 u16 blk, cnt;
160 void *addr;
161 int n;
162 void *key_addr = NULL;
163 struct mmc *mmc = find_mmc_device(curr_device);
272cc70b 164
1fd93c6e
PA
165 if (argc < 4)
166 return CMD_RET_USAGE;
272cc70b 167
1fd93c6e
PA
168 addr = (void *)simple_strtoul(argv[1], NULL, 16);
169 blk = simple_strtoul(argv[2], NULL, 16);
170 cnt = simple_strtoul(argv[3], NULL, 16);
171
172 if (argc == 5)
173 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
174
175 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
176 curr_device, blk, cnt);
177 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
178
179 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
180 if (n != cnt)
181 return CMD_RET_FAILURE;
182 return CMD_RET_SUCCESS;
183}
184static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag,
185 int argc, char * const argv[])
272cc70b 186{
1fd93c6e
PA
187 u16 blk, cnt;
188 void *addr;
189 int n;
190 void *key_addr;
191 struct mmc *mmc = find_mmc_device(curr_device);
6be95ccf 192
1fd93c6e 193 if (argc != 5)
4c12eeb8 194 return CMD_RET_USAGE;
272cc70b 195
1fd93c6e
PA
196 addr = (void *)simple_strtoul(argv[1], NULL, 16);
197 blk = simple_strtoul(argv[2], NULL, 16);
198 cnt = simple_strtoul(argv[3], NULL, 16);
199 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
200
201 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
202 curr_device, blk, cnt);
203 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
204
205 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
206 if (n != cnt)
207 return CMD_RET_FAILURE;
208 return CMD_RET_SUCCESS;
209}
210static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag,
211 int argc, char * const argv[])
212{
213 unsigned long counter;
214 struct mmc *mmc = find_mmc_device(curr_device);
215
216 if (mmc_rpmb_get_counter(mmc, &counter))
217 return CMD_RET_FAILURE;
218 printf("RPMB Write counter= %lx\n", counter);
219 return CMD_RET_SUCCESS;
220}
221
222static cmd_tbl_t cmd_rpmb[] = {
223 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
224 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
225 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
226 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
227};
228
229static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag,
230 int argc, char * const argv[])
231{
232 cmd_tbl_t *cp;
233 struct mmc *mmc;
234 char original_part;
235 int ret;
236
237 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
238
239 /* Drop the rpmb subcommand */
240 argc--;
241 argv++;
242
243 if (cp == NULL || argc > cp->maxargs)
244 return CMD_RET_USAGE;
245 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
246 return CMD_RET_SUCCESS;
247
1ae24a50 248 mmc = init_mmc_device(curr_device, false);
1fd93c6e
PA
249 if (!mmc)
250 return CMD_RET_FAILURE;
251
252 if (!(mmc->version & MMC_VERSION_MMC)) {
253 printf("It is not a EMMC device\n");
254 return CMD_RET_FAILURE;
255 }
256 if (mmc->version < MMC_VERSION_4_41) {
257 printf("RPMB not supported before version 4.41\n");
258 return CMD_RET_FAILURE;
ea6ebe21 259 }
1fd93c6e 260 /* Switch to the RPMB partition */
4dc80c87 261#ifndef CONFIG_BLK
b955e42b 262 original_part = mmc->block_dev.hwpart;
4dc80c87
KY
263#else
264 original_part = mmc_get_blk_desc(mmc)->hwpart;
265#endif
69f45cd5
SG
266 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, MMC_PART_RPMB) !=
267 0)
873cc1d7 268 return CMD_RET_FAILURE;
1fd93c6e 269 ret = cp->cmd(cmdtp, flag, argc, argv);
272cc70b 270
1fd93c6e 271 /* Return to original partition */
69f45cd5
SG
272 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, original_part) !=
273 0)
873cc1d7 274 return CMD_RET_FAILURE;
1fd93c6e
PA
275 return ret;
276}
277#endif
9fd38372 278
1fd93c6e
PA
279static int do_mmc_read(cmd_tbl_t *cmdtp, int flag,
280 int argc, char * const argv[])
281{
282 struct mmc *mmc;
283 u32 blk, cnt, n;
284 void *addr;
e85649c7 285
1fd93c6e
PA
286 if (argc != 4)
287 return CMD_RET_USAGE;
ea6ebe21 288
1fd93c6e
PA
289 addr = (void *)simple_strtoul(argv[1], NULL, 16);
290 blk = simple_strtoul(argv[2], NULL, 16);
291 cnt = simple_strtoul(argv[3], NULL, 16);
272cc70b 292
1ae24a50 293 mmc = init_mmc_device(curr_device, false);
1fd93c6e
PA
294 if (!mmc)
295 return CMD_RET_FAILURE;
ea6ebe21 296
1fd93c6e
PA
297 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
298 curr_device, blk, cnt);
9fd38372 299
c40fdca6 300 n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
1fd93c6e 301 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
8f3b9642 302
1fd93c6e
PA
303 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
304}
305static int do_mmc_write(cmd_tbl_t *cmdtp, int flag,
306 int argc, char * const argv[])
307{
308 struct mmc *mmc;
309 u32 blk, cnt, n;
310 void *addr;
8f3b9642 311
1fd93c6e
PA
312 if (argc != 4)
313 return CMD_RET_USAGE;
272cc70b 314
1fd93c6e
PA
315 addr = (void *)simple_strtoul(argv[1], NULL, 16);
316 blk = simple_strtoul(argv[2], NULL, 16);
317 cnt = simple_strtoul(argv[3], NULL, 16);
bc897b1d 318
1ae24a50 319 mmc = init_mmc_device(curr_device, false);
1fd93c6e
PA
320 if (!mmc)
321 return CMD_RET_FAILURE;
bc897b1d 322
1fd93c6e
PA
323 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
324 curr_device, blk, cnt);
272cc70b 325
1fd93c6e
PA
326 if (mmc_getwp(mmc) == 1) {
327 printf("Error: card is write protected!\n");
328 return CMD_RET_FAILURE;
329 }
c40fdca6 330 n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
1fd93c6e 331 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
792970b0 332
1fd93c6e
PA
333 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
334}
335static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,
336 int argc, char * const argv[])
337{
338 struct mmc *mmc;
339 u32 blk, cnt, n;
2a91c913 340
1fd93c6e
PA
341 if (argc != 3)
342 return CMD_RET_USAGE;
792970b0 343
1fd93c6e
PA
344 blk = simple_strtoul(argv[1], NULL, 16);
345 cnt = simple_strtoul(argv[2], NULL, 16);
5a99b9de 346
1ae24a50 347 mmc = init_mmc_device(curr_device, false);
1fd93c6e
PA
348 if (!mmc)
349 return CMD_RET_FAILURE;
5a99b9de 350
1fd93c6e
PA
351 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
352 curr_device, blk, cnt);
5a99b9de 353
1fd93c6e
PA
354 if (mmc_getwp(mmc) == 1) {
355 printf("Error: card is write protected!\n");
356 return CMD_RET_FAILURE;
357 }
c40fdca6 358 n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
1fd93c6e 359 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
b01e6fe6 360
1fd93c6e
PA
361 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
362}
363static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
364 int argc, char * const argv[])
365{
366 struct mmc *mmc;
2a91c913 367
941944e4
SW
368 mmc = init_mmc_device(curr_device, true);
369 if (!mmc)
1fd93c6e 370 return CMD_RET_FAILURE;
2a91c913 371
1fd93c6e
PA
372 return CMD_RET_SUCCESS;
373}
374static int do_mmc_part(cmd_tbl_t *cmdtp, int flag,
375 int argc, char * const argv[])
376{
4101f687 377 struct blk_desc *mmc_dev;
1fd93c6e
PA
378 struct mmc *mmc;
379
1ae24a50 380 mmc = init_mmc_device(curr_device, false);
1fd93c6e
PA
381 if (!mmc)
382 return CMD_RET_FAILURE;
383
3c457f4d 384 mmc_dev = blk_get_devnum_by_type(IF_TYPE_MMC, curr_device);
1fd93c6e 385 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
3e8bd469 386 part_print(mmc_dev);
1fd93c6e
PA
387 return CMD_RET_SUCCESS;
388 }
389
390 puts("get mmc type error!\n");
391 return CMD_RET_FAILURE;
392}
393static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag,
394 int argc, char * const argv[])
395{
60dc58f7 396 int dev, part = 0, ret;
1fd93c6e
PA
397 struct mmc *mmc;
398
399 if (argc == 1) {
400 dev = curr_device;
401 } else if (argc == 2) {
402 dev = simple_strtoul(argv[1], NULL, 10);
403 } else if (argc == 3) {
404 dev = (int)simple_strtoul(argv[1], NULL, 10);
405 part = (int)simple_strtoul(argv[2], NULL, 10);
406 if (part > PART_ACCESS_MASK) {
407 printf("#part_num shouldn't be larger than %d\n",
408 PART_ACCESS_MASK);
409 return CMD_RET_FAILURE;
33ace362 410 }
1fd93c6e
PA
411 } else {
412 return CMD_RET_USAGE;
413 }
33ace362 414
a5710920 415 mmc = init_mmc_device(dev, true);
1fd93c6e
PA
416 if (!mmc)
417 return CMD_RET_FAILURE;
418
69f45cd5 419 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
60dc58f7
SW
420 printf("switch to partitions #%d, %s\n",
421 part, (!ret) ? "OK" : "ERROR");
422 if (ret)
423 return 1;
424
1fd93c6e
PA
425 curr_device = dev;
426 if (mmc->part_config == MMCPART_NOAVAILABLE)
427 printf("mmc%d is current device\n", curr_device);
428 else
429 printf("mmc%d(part %d) is current device\n",
c40fdca6 430 curr_device, mmc_get_blk_desc(mmc)->hwpart);
33ace362 431
1fd93c6e
PA
432 return CMD_RET_SUCCESS;
433}
434static int do_mmc_list(cmd_tbl_t *cmdtp, int flag,
435 int argc, char * const argv[])
436{
437 print_mmc_devices('\n');
438 return CMD_RET_SUCCESS;
439}
c599f53b 440
189f963a
DSC
441static int parse_hwpart_user(struct mmc_hwpart_conf *pconf,
442 int argc, char * const argv[])
443{
444 int i = 0;
445
446 memset(&pconf->user, 0, sizeof(pconf->user));
447
448 while (i < argc) {
449 if (!strcmp(argv[i], "enh")) {
450 if (i + 2 >= argc)
451 return -1;
452 pconf->user.enh_start =
453 simple_strtoul(argv[i+1], NULL, 10);
454 pconf->user.enh_size =
455 simple_strtoul(argv[i+2], NULL, 10);
456 i += 3;
457 } else if (!strcmp(argv[i], "wrrel")) {
458 if (i + 1 >= argc)
459 return -1;
460 pconf->user.wr_rel_change = 1;
461 if (!strcmp(argv[i+1], "on"))
462 pconf->user.wr_rel_set = 1;
463 else if (!strcmp(argv[i+1], "off"))
464 pconf->user.wr_rel_set = 0;
465 else
466 return -1;
467 i += 2;
468 } else {
469 break;
470 }
471 }
472 return i;
473}
474
475static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
476 int argc, char * const argv[])
477{
478 int i;
479
480 memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
481
482 if (1 >= argc)
483 return -1;
484 pconf->gp_part[pidx].size = simple_strtoul(argv[0], NULL, 10);
485
486 i = 1;
487 while (i < argc) {
488 if (!strcmp(argv[i], "enh")) {
489 pconf->gp_part[pidx].enhanced = 1;
490 i += 1;
491 } else if (!strcmp(argv[i], "wrrel")) {
492 if (i + 1 >= argc)
493 return -1;
494 pconf->gp_part[pidx].wr_rel_change = 1;
495 if (!strcmp(argv[i+1], "on"))
496 pconf->gp_part[pidx].wr_rel_set = 1;
497 else if (!strcmp(argv[i+1], "off"))
498 pconf->gp_part[pidx].wr_rel_set = 0;
499 else
500 return -1;
501 i += 2;
502 } else {
503 break;
504 }
505 }
506 return i;
507}
508
c599f53b
DSC
509static int do_mmc_hwpartition(cmd_tbl_t *cmdtp, int flag,
510 int argc, char * const argv[])
511{
512 struct mmc *mmc;
513 struct mmc_hwpart_conf pconf = { };
514 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
189f963a 515 int i, r, pidx;
c599f53b
DSC
516
517 mmc = init_mmc_device(curr_device, false);
518 if (!mmc)
519 return CMD_RET_FAILURE;
520
521 if (argc < 1)
522 return CMD_RET_USAGE;
523 i = 1;
524 while (i < argc) {
189f963a
DSC
525 if (!strcmp(argv[i], "user")) {
526 i++;
527 r = parse_hwpart_user(&pconf, argc-i, &argv[i]);
528 if (r < 0)
c599f53b 529 return CMD_RET_USAGE;
189f963a 530 i += r;
c599f53b
DSC
531 } else if (!strncmp(argv[i], "gp", 2) &&
532 strlen(argv[i]) == 3 &&
533 argv[i][2] >= '1' && argv[i][2] <= '4') {
c599f53b 534 pidx = argv[i][2] - '1';
189f963a
DSC
535 i++;
536 r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
537 if (r < 0)
538 return CMD_RET_USAGE;
539 i += r;
c599f53b
DSC
540 } else if (!strcmp(argv[i], "check")) {
541 mode = MMC_HWPART_CONF_CHECK;
542 i++;
543 } else if (!strcmp(argv[i], "set")) {
544 mode = MMC_HWPART_CONF_SET;
545 i++;
546 } else if (!strcmp(argv[i], "complete")) {
547 mode = MMC_HWPART_CONF_COMPLETE;
548 i++;
549 } else {
550 return CMD_RET_USAGE;
551 }
552 }
553
554 puts("Partition configuration:\n");
555 if (pconf.user.enh_size) {
556 puts("\tUser Enhanced Start: ");
557 print_size(((u64)pconf.user.enh_start) << 9, "\n");
558 puts("\tUser Enhanced Size: ");
559 print_size(((u64)pconf.user.enh_size) << 9, "\n");
560 } else {
561 puts("\tNo enhanced user data area\n");
562 }
189f963a
DSC
563 if (pconf.user.wr_rel_change)
564 printf("\tUser partition write reliability: %s\n",
565 pconf.user.wr_rel_set ? "on" : "off");
c599f53b
DSC
566 for (pidx = 0; pidx < 4; pidx++) {
567 if (pconf.gp_part[pidx].size) {
568 printf("\tGP%i Capacity: ", pidx+1);
569 print_size(((u64)pconf.gp_part[pidx].size) << 9,
570 pconf.gp_part[pidx].enhanced ?
571 " ENH\n" : "\n");
572 } else {
573 printf("\tNo GP%i partition\n", pidx+1);
574 }
189f963a
DSC
575 if (pconf.gp_part[pidx].wr_rel_change)
576 printf("\tGP%i write reliability: %s\n", pidx+1,
577 pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
c599f53b
DSC
578 }
579
580 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
581 if (mode == MMC_HWPART_CONF_COMPLETE)
582 puts("Partitioning successful, "
583 "power-cycle to make effective\n");
584 return CMD_RET_SUCCESS;
585 } else {
189f963a 586 puts("Failed!\n");
c599f53b
DSC
587 return CMD_RET_FAILURE;
588 }
589}
590
1fd93c6e
PA
591#ifdef CONFIG_SUPPORT_EMMC_BOOT
592static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag,
593 int argc, char * const argv[])
594{
595 int dev;
596 struct mmc *mmc;
597 u8 width, reset, mode;
598
599 if (argc != 5)
600 return CMD_RET_USAGE;
601 dev = simple_strtoul(argv[1], NULL, 10);
602 width = simple_strtoul(argv[2], NULL, 10);
603 reset = simple_strtoul(argv[3], NULL, 10);
604 mode = simple_strtoul(argv[4], NULL, 10);
605
1ae24a50 606 mmc = init_mmc_device(dev, false);
1fd93c6e
PA
607 if (!mmc)
608 return CMD_RET_FAILURE;
609
610 if (IS_SD(mmc)) {
611 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
612 return CMD_RET_FAILURE;
2a91c913 613 }
ab71188c 614
1fd93c6e
PA
615 /* acknowledge to be sent during boot operation */
616 return mmc_set_boot_bus_width(mmc, width, reset, mode);
617}
618static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag,
619 int argc, char * const argv[])
620{
621 int dev;
622 struct mmc *mmc;
623 u32 bootsize, rpmbsize;
ab71188c 624
1fd93c6e
PA
625 if (argc != 4)
626 return CMD_RET_USAGE;
627 dev = simple_strtoul(argv[1], NULL, 10);
628 bootsize = simple_strtoul(argv[2], NULL, 10);
629 rpmbsize = simple_strtoul(argv[3], NULL, 10);
630
1ae24a50 631 mmc = init_mmc_device(dev, false);
1fd93c6e
PA
632 if (!mmc)
633 return CMD_RET_FAILURE;
634
635 if (IS_SD(mmc)) {
636 printf("It is not a EMMC device\n");
637 return CMD_RET_FAILURE;
ab71188c
MN
638 }
639
1fd93c6e
PA
640 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
641 printf("EMMC boot partition Size change Failed.\n");
642 return CMD_RET_FAILURE;
643 }
e85649c7 644
1fd93c6e
PA
645 printf("EMMC boot partition Size %d MB\n", bootsize);
646 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
647 return CMD_RET_SUCCESS;
648}
bdb60996
AD
649
650static int mmc_partconf_print(struct mmc *mmc)
651{
652 u8 ack, access, part;
653
654 if (mmc->part_config == MMCPART_NOAVAILABLE) {
655 printf("No part_config info for ver. 0x%x\n", mmc->version);
656 return CMD_RET_FAILURE;
657 }
658
659 access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
660 ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
661 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
662
663 printf("EXT_CSD[179], PARTITION_CONFIG:\n"
664 "BOOT_ACK: 0x%x\n"
665 "BOOT_PARTITION_ENABLE: 0x%x\n"
666 "PARTITION_ACCESS: 0x%x\n", ack, part, access);
667
668 return CMD_RET_SUCCESS;
669}
670
1fd93c6e
PA
671static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag,
672 int argc, char * const argv[])
673{
674 int dev;
675 struct mmc *mmc;
676 u8 ack, part_num, access;
272cc70b 677
bdb60996 678 if (argc != 2 && argc != 5)
1fd93c6e 679 return CMD_RET_USAGE;
272cc70b 680
1fd93c6e 681 dev = simple_strtoul(argv[1], NULL, 10);
d23d8d7e 682
1ae24a50 683 mmc = init_mmc_device(dev, false);
1fd93c6e
PA
684 if (!mmc)
685 return CMD_RET_FAILURE;
686
687 if (IS_SD(mmc)) {
688 puts("PARTITION_CONFIG only exists on eMMC\n");
689 return CMD_RET_FAILURE;
690 }
691
bdb60996
AD
692 if (argc == 2)
693 return mmc_partconf_print(mmc);
694
695 ack = simple_strtoul(argv[2], NULL, 10);
696 part_num = simple_strtoul(argv[3], NULL, 10);
697 access = simple_strtoul(argv[4], NULL, 10);
698
1fd93c6e
PA
699 /* acknowledge to be sent during boot operation */
700 return mmc_set_part_conf(mmc, ack, part_num, access);
701}
702static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag,
703 int argc, char * const argv[])
704{
705 int dev;
706 struct mmc *mmc;
707 u8 enable;
708
709 /*
710 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
711 * The only valid values are 0x0, 0x1 and 0x2 and writing
712 * a value of 0x1 or 0x2 sets the value permanently.
713 */
714 if (argc != 3)
715 return CMD_RET_USAGE;
716
717 dev = simple_strtoul(argv[1], NULL, 10);
718 enable = simple_strtoul(argv[2], NULL, 10);
719
678e9316 720 if (enable > 2) {
1fd93c6e
PA
721 puts("Invalid RST_n_ENABLE value\n");
722 return CMD_RET_USAGE;
723 }
724
1ae24a50 725 mmc = init_mmc_device(dev, false);
1fd93c6e
PA
726 if (!mmc)
727 return CMD_RET_FAILURE;
728
729 if (IS_SD(mmc)) {
730 puts("RST_n_FUNCTION only exists on eMMC\n");
731 return CMD_RET_FAILURE;
732 }
272cc70b 733
1fd93c6e
PA
734 return mmc_set_rst_n_function(mmc, enable);
735}
736#endif
737static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag,
738 int argc, char * const argv[])
739{
740 struct mmc *mmc;
741 u32 val;
742 int ret;
743
744 if (argc != 2)
745 return CMD_RET_USAGE;
84c1dfe4 746 val = simple_strtoul(argv[1], NULL, 16);
1fd93c6e
PA
747
748 mmc = find_mmc_device(curr_device);
749 if (!mmc) {
750 printf("no mmc device at slot %x\n", curr_device);
751 return CMD_RET_FAILURE;
752 }
753 ret = mmc_set_dsr(mmc, val);
754 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
755 if (!ret) {
756 mmc->has_init = 0;
757 if (mmc_init(mmc))
758 return CMD_RET_FAILURE;
759 else
760 return CMD_RET_SUCCESS;
272cc70b 761 }
1fd93c6e
PA
762 return ret;
763}
764
cd3d4880
TM
765#ifdef CONFIG_CMD_BKOPS_ENABLE
766static int do_mmc_bkops_enable(cmd_tbl_t *cmdtp, int flag,
767 int argc, char * const argv[])
768{
769 int dev;
770 struct mmc *mmc;
771
772 if (argc != 2)
773 return CMD_RET_USAGE;
774
775 dev = simple_strtoul(argv[1], NULL, 10);
776
777 mmc = init_mmc_device(dev, false);
778 if (!mmc)
779 return CMD_RET_FAILURE;
780
781 if (IS_SD(mmc)) {
782 puts("BKOPS_EN only exists on eMMC\n");
783 return CMD_RET_FAILURE;
784 }
785
786 return mmc_set_bkops_enable(mmc);
787}
788#endif
789
1fd93c6e
PA
790static cmd_tbl_t cmd_mmc[] = {
791 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
792 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
793 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
794 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
795 U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
796 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
797 U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
798 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
189f963a 799 U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
1fd93c6e
PA
800#ifdef CONFIG_SUPPORT_EMMC_BOOT
801 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
fa7b8851 802 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
1fd93c6e
PA
803 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
804 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
805#endif
806#ifdef CONFIG_SUPPORT_EMMC_RPMB
807 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
808#endif
809 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
cd3d4880
TM
810#ifdef CONFIG_CMD_BKOPS_ENABLE
811 U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
812#endif
1fd93c6e
PA
813};
814
815static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
816{
817 cmd_tbl_t *cp;
818
819 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
820
821 /* Drop the mmc command */
822 argc--;
823 argv++;
824
825 if (cp == NULL || argc > cp->maxargs)
826 return CMD_RET_USAGE;
827 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
828 return CMD_RET_SUCCESS;
ea6ebe21 829
1fd93c6e
PA
830 if (curr_device < 0) {
831 if (get_mmc_num() > 0) {
832 curr_device = 0;
833 } else {
834 puts("No MMC device available\n");
835 return CMD_RET_FAILURE;
836 }
837 }
838 return cp->cmd(cmdtp, flag, argc, argv);
272cc70b
AF
839}
840
841U_BOOT_CMD(
189f963a 842 mmc, 29, 1, do_mmcops,
852dbfdd 843 "MMC sub system",
1fd93c6e
PA
844 "info - display info of the current MMC device\n"
845 "mmc read addr blk# cnt\n"
ea6ebe21 846 "mmc write addr blk# cnt\n"
e6f99a56 847 "mmc erase blk# cnt\n"
ea6ebe21
LW
848 "mmc rescan\n"
849 "mmc part - lists available partition on current mmc device\n"
bc897b1d 850 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
2a91c913 851 "mmc list - lists available devices\n"
c599f53b
DSC
852 "mmc hwpartition [args...] - does hardware partitioning\n"
853 " arguments (sizes in 512-byte blocks):\n"
189f963a
DSC
854 " [user [enh start cnt] [wrrel {on|off}]] - sets user data area attributes\n"
855 " [gp1|gp2|gp3|gp4 cnt [enh] [wrrel {on|off}]] - general purpose partition\n"
c599f53b 856 " [check|set|complete] - mode, complete set partitioning completed\n"
189f963a
DSC
857 " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
858 " Power cycling is required to initialize partitions after set to complete.\n"
2a91c913 859#ifdef CONFIG_SUPPORT_EMMC_BOOT
5a99b9de
TR
860 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
861 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
f1fd957e
TR
862 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
863 " - Change sizes of boot and RPMB partitions of specified device\n"
bdb60996
AD
864 "mmc partconf dev [boot_ack boot_partition partition_access]\n"
865 " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
33ace362
TR
866 "mmc rst-function dev value\n"
867 " - Change the RST_n_FUNCTION field of the specified device\n"
868 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
3511b4e2 869#endif
1fd93c6e
PA
870#ifdef CONFIG_SUPPORT_EMMC_RPMB
871 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
872 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
873 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
874 "mmc rpmb counter - read the value of the write counter\n"
875#endif
876 "mmc setdsr <value> - set DSR register value\n"
cd3d4880
TM
877#ifdef CONFIG_CMD_BKOPS_ENABLE
878 "mmc bkops-enable <dev> - enable background operations handshake on device\n"
879 " WARNING: This is a write-once setting.\n"
880#endif
2a91c913 881 );
1fd93c6e
PA
882
883/* Old command kept for compatibility. Same as 'mmc info' */
884U_BOOT_CMD(
885 mmcinfo, 1, 0, do_mmcinfo,
886 "display MMC info",
887 "- display info of the current MMC device"
888);