3 * Kyle Harris, kharris@nexus-tech.net
5 * SPDX-License-Identifier: GPL-2.0+
12 static int curr_device = -1;
13 #ifndef CONFIG_GENERIC_MMC
14 int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
21 if (strcmp(argv[1], "init") == 0) {
27 } else if (argc == 3) {
28 dev = (int)simple_strtoul(argv[2], NULL, 10);
33 if (mmc_legacy_init(dev) != 0) {
34 puts("No MMC card found\n");
39 printf("mmc%d is available\n", curr_device);
40 } else if (strcmp(argv[1], "device") == 0) {
42 if (curr_device < 0) {
43 puts("No MMC device available\n");
46 } else if (argc == 3) {
47 dev = (int)simple_strtoul(argv[2], NULL, 10);
49 #ifdef CONFIG_SYS_MMC_SET_DEV
50 if (mmc_set_dev(dev) != 0)
58 printf("mmc%d is current device\n", curr_device);
69 "init [dev] - init MMC sub system\n"
70 "mmc device [dev] - show or set current device"
72 #else /* !CONFIG_GENERIC_MMC */
74 static void print_mmcinfo(struct mmc *mmc)
78 printf("Device: %s\n", mmc->cfg->name);
79 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
80 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
81 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
82 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
83 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
85 printf("Tran Speed: %d\n", mmc->tran_speed);
86 printf("Rd Block Len: %d\n", mmc->read_bl_len);
88 printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
89 (mmc->version >> 8) & 0xf, mmc->version & 0xff);
91 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
93 print_size(mmc->capacity, "\n");
95 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
96 mmc->ddr_mode ? " DDR" : "");
98 puts("Erase Group Size: ");
99 print_size(((u64)mmc->erase_grp_size) << 9, "\n");
101 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
102 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
103 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
105 puts("HC WP Group Size: ");
106 print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
108 puts("User Capacity: ");
109 print_size(mmc->capacity_user, usr_enh ? " ENH\n" : "\n");
111 puts("User Enhanced Start: ");
112 print_size(mmc->enh_user_start, "\n");
113 puts("User Enhanced Size: ");
114 print_size(mmc->enh_user_size, "\n");
116 puts("Boot Capacity: ");
117 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
118 puts("RPMB Capacity: ");
119 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
121 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
122 bool is_enh = has_enh &&
123 (mmc->part_attr & EXT_CSD_ENH_GP(i));
124 if (mmc->capacity_gp[i]) {
125 printf("GP%i Capacity: ", i+1);
126 print_size(mmc->capacity_gp[i],
127 is_enh ? " ENH\n" : "\n");
132 static struct mmc *init_mmc_device(int dev, bool force_init)
135 mmc = find_mmc_device(dev);
137 printf("no mmc device at slot %x\n", dev);
146 static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
150 if (curr_device < 0) {
151 if (get_mmc_num() > 0)
154 puts("No MMC device available\n");
159 mmc = init_mmc_device(curr_device, false);
161 return CMD_RET_FAILURE;
164 return CMD_RET_SUCCESS;
167 #ifdef CONFIG_SUPPORT_EMMC_RPMB
168 static int confirm_key_prog(void)
170 puts("Warning: Programming authentication key can be done only once !\n"
171 " Use this command only if you are sure of what you are doing,\n"
172 "Really perform the key programming? <y/N> ");
176 puts("Authentication key programming aborted\n");
179 static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag,
180 int argc, char * const argv[])
183 struct mmc *mmc = find_mmc_device(curr_device);
186 return CMD_RET_USAGE;
188 key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
189 if (!confirm_key_prog())
190 return CMD_RET_FAILURE;
191 if (mmc_rpmb_set_key(mmc, key_addr)) {
192 printf("ERROR - Key already programmed ?\n");
193 return CMD_RET_FAILURE;
195 return CMD_RET_SUCCESS;
197 static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag,
198 int argc, char * const argv[])
203 void *key_addr = NULL;
204 struct mmc *mmc = find_mmc_device(curr_device);
207 return CMD_RET_USAGE;
209 addr = (void *)simple_strtoul(argv[1], NULL, 16);
210 blk = simple_strtoul(argv[2], NULL, 16);
211 cnt = simple_strtoul(argv[3], NULL, 16);
214 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
216 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
217 curr_device, blk, cnt);
218 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
220 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
222 return CMD_RET_FAILURE;
223 return CMD_RET_SUCCESS;
225 static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag,
226 int argc, char * const argv[])
232 struct mmc *mmc = find_mmc_device(curr_device);
235 return CMD_RET_USAGE;
237 addr = (void *)simple_strtoul(argv[1], NULL, 16);
238 blk = simple_strtoul(argv[2], NULL, 16);
239 cnt = simple_strtoul(argv[3], NULL, 16);
240 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
242 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
243 curr_device, blk, cnt);
244 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
246 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
248 return CMD_RET_FAILURE;
249 return CMD_RET_SUCCESS;
251 static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag,
252 int argc, char * const argv[])
254 unsigned long counter;
255 struct mmc *mmc = find_mmc_device(curr_device);
257 if (mmc_rpmb_get_counter(mmc, &counter))
258 return CMD_RET_FAILURE;
259 printf("RPMB Write counter= %lx\n", counter);
260 return CMD_RET_SUCCESS;
263 static cmd_tbl_t cmd_rpmb[] = {
264 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
265 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
266 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
267 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
270 static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag,
271 int argc, char * const argv[])
278 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
280 /* Drop the rpmb subcommand */
284 if (cp == NULL || argc > cp->maxargs)
285 return CMD_RET_USAGE;
286 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
287 return CMD_RET_SUCCESS;
289 mmc = init_mmc_device(curr_device, false);
291 return CMD_RET_FAILURE;
293 if (!(mmc->version & MMC_VERSION_MMC)) {
294 printf("It is not a EMMC device\n");
295 return CMD_RET_FAILURE;
297 if (mmc->version < MMC_VERSION_4_41) {
298 printf("RPMB not supported before version 4.41\n");
299 return CMD_RET_FAILURE;
301 /* Switch to the RPMB partition */
302 original_part = mmc->part_num;
303 if (mmc->part_num != MMC_PART_RPMB) {
304 if (mmc_switch_part(curr_device, MMC_PART_RPMB) != 0)
305 return CMD_RET_FAILURE;
306 mmc->part_num = MMC_PART_RPMB;
308 ret = cp->cmd(cmdtp, flag, argc, argv);
310 /* Return to original partition */
311 if (mmc->part_num != original_part) {
312 if (mmc_switch_part(curr_device, original_part) != 0)
313 return CMD_RET_FAILURE;
314 mmc->part_num = original_part;
320 static int do_mmc_read(cmd_tbl_t *cmdtp, int flag,
321 int argc, char * const argv[])
328 return CMD_RET_USAGE;
330 addr = (void *)simple_strtoul(argv[1], NULL, 16);
331 blk = simple_strtoul(argv[2], NULL, 16);
332 cnt = simple_strtoul(argv[3], NULL, 16);
334 mmc = init_mmc_device(curr_device, false);
336 return CMD_RET_FAILURE;
338 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
339 curr_device, blk, cnt);
341 n = mmc->block_dev.block_read(curr_device, blk, cnt, addr);
342 /* flush cache after read */
343 flush_cache((ulong)addr, cnt * 512); /* FIXME */
344 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
346 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
348 static int do_mmc_write(cmd_tbl_t *cmdtp, int flag,
349 int argc, char * const argv[])
356 return CMD_RET_USAGE;
358 addr = (void *)simple_strtoul(argv[1], NULL, 16);
359 blk = simple_strtoul(argv[2], NULL, 16);
360 cnt = simple_strtoul(argv[3], NULL, 16);
362 mmc = init_mmc_device(curr_device, false);
364 return CMD_RET_FAILURE;
366 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
367 curr_device, blk, cnt);
369 if (mmc_getwp(mmc) == 1) {
370 printf("Error: card is write protected!\n");
371 return CMD_RET_FAILURE;
373 n = mmc->block_dev.block_write(curr_device, blk, cnt, addr);
374 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
376 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
378 static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,
379 int argc, char * const argv[])
385 return CMD_RET_USAGE;
387 blk = simple_strtoul(argv[1], NULL, 16);
388 cnt = simple_strtoul(argv[2], NULL, 16);
390 mmc = init_mmc_device(curr_device, false);
392 return CMD_RET_FAILURE;
394 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
395 curr_device, blk, cnt);
397 if (mmc_getwp(mmc) == 1) {
398 printf("Error: card is write protected!\n");
399 return CMD_RET_FAILURE;
401 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
402 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
404 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
406 static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
407 int argc, char * const argv[])
411 mmc = init_mmc_device(curr_device, true);
413 return CMD_RET_FAILURE;
415 return CMD_RET_SUCCESS;
417 static int do_mmc_part(cmd_tbl_t *cmdtp, int flag,
418 int argc, char * const argv[])
420 block_dev_desc_t *mmc_dev;
423 mmc = init_mmc_device(curr_device, false);
425 return CMD_RET_FAILURE;
427 mmc_dev = mmc_get_dev(curr_device);
428 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
430 return CMD_RET_SUCCESS;
433 puts("get mmc type error!\n");
434 return CMD_RET_FAILURE;
436 static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag,
437 int argc, char * const argv[])
439 int dev, part = 0, ret;
444 } else if (argc == 2) {
445 dev = simple_strtoul(argv[1], NULL, 10);
446 } else if (argc == 3) {
447 dev = (int)simple_strtoul(argv[1], NULL, 10);
448 part = (int)simple_strtoul(argv[2], NULL, 10);
449 if (part > PART_ACCESS_MASK) {
450 printf("#part_num shouldn't be larger than %d\n",
452 return CMD_RET_FAILURE;
455 return CMD_RET_USAGE;
458 mmc = init_mmc_device(dev, true);
460 return CMD_RET_FAILURE;
462 ret = mmc_select_hwpart(dev, part);
463 printf("switch to partitions #%d, %s\n",
464 part, (!ret) ? "OK" : "ERROR");
469 if (mmc->part_config == MMCPART_NOAVAILABLE)
470 printf("mmc%d is current device\n", curr_device);
472 printf("mmc%d(part %d) is current device\n",
473 curr_device, mmc->part_num);
475 return CMD_RET_SUCCESS;
477 static int do_mmc_list(cmd_tbl_t *cmdtp, int flag,
478 int argc, char * const argv[])
480 print_mmc_devices('\n');
481 return CMD_RET_SUCCESS;
483 #ifdef CONFIG_SUPPORT_EMMC_BOOT
484 static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag,
485 int argc, char * const argv[])
489 u8 width, reset, mode;
492 return CMD_RET_USAGE;
493 dev = simple_strtoul(argv[1], NULL, 10);
494 width = simple_strtoul(argv[2], NULL, 10);
495 reset = simple_strtoul(argv[3], NULL, 10);
496 mode = simple_strtoul(argv[4], NULL, 10);
498 mmc = init_mmc_device(dev, false);
500 return CMD_RET_FAILURE;
503 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
504 return CMD_RET_FAILURE;
507 /* acknowledge to be sent during boot operation */
508 return mmc_set_boot_bus_width(mmc, width, reset, mode);
510 static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag,
511 int argc, char * const argv[])
515 u32 bootsize, rpmbsize;
518 return CMD_RET_USAGE;
519 dev = simple_strtoul(argv[1], NULL, 10);
520 bootsize = simple_strtoul(argv[2], NULL, 10);
521 rpmbsize = simple_strtoul(argv[3], NULL, 10);
523 mmc = init_mmc_device(dev, false);
525 return CMD_RET_FAILURE;
528 printf("It is not a EMMC device\n");
529 return CMD_RET_FAILURE;
532 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
533 printf("EMMC boot partition Size change Failed.\n");
534 return CMD_RET_FAILURE;
537 printf("EMMC boot partition Size %d MB\n", bootsize);
538 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
539 return CMD_RET_SUCCESS;
541 static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag,
542 int argc, char * const argv[])
546 u8 ack, part_num, access;
549 return CMD_RET_USAGE;
551 dev = simple_strtoul(argv[1], NULL, 10);
552 ack = simple_strtoul(argv[2], NULL, 10);
553 part_num = simple_strtoul(argv[3], NULL, 10);
554 access = simple_strtoul(argv[4], NULL, 10);
556 mmc = init_mmc_device(dev, false);
558 return CMD_RET_FAILURE;
561 puts("PARTITION_CONFIG only exists on eMMC\n");
562 return CMD_RET_FAILURE;
565 /* acknowledge to be sent during boot operation */
566 return mmc_set_part_conf(mmc, ack, part_num, access);
568 static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag,
569 int argc, char * const argv[])
576 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
577 * The only valid values are 0x0, 0x1 and 0x2 and writing
578 * a value of 0x1 or 0x2 sets the value permanently.
581 return CMD_RET_USAGE;
583 dev = simple_strtoul(argv[1], NULL, 10);
584 enable = simple_strtoul(argv[2], NULL, 10);
586 if (enable > 2 || enable < 0) {
587 puts("Invalid RST_n_ENABLE value\n");
588 return CMD_RET_USAGE;
591 mmc = init_mmc_device(dev, false);
593 return CMD_RET_FAILURE;
596 puts("RST_n_FUNCTION only exists on eMMC\n");
597 return CMD_RET_FAILURE;
600 return mmc_set_rst_n_function(mmc, enable);
603 static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag,
604 int argc, char * const argv[])
611 return CMD_RET_USAGE;
612 val = simple_strtoul(argv[2], NULL, 16);
614 mmc = find_mmc_device(curr_device);
616 printf("no mmc device at slot %x\n", curr_device);
617 return CMD_RET_FAILURE;
619 ret = mmc_set_dsr(mmc, val);
620 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
624 return CMD_RET_FAILURE;
626 return CMD_RET_SUCCESS;
631 static cmd_tbl_t cmd_mmc[] = {
632 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
633 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
634 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
635 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
636 U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
637 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
638 U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
639 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
640 #ifdef CONFIG_SUPPORT_EMMC_BOOT
641 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
642 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
643 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
644 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
646 #ifdef CONFIG_SUPPORT_EMMC_RPMB
647 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
649 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
652 static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
656 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
658 /* Drop the mmc command */
662 if (cp == NULL || argc > cp->maxargs)
663 return CMD_RET_USAGE;
664 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
665 return CMD_RET_SUCCESS;
667 if (curr_device < 0) {
668 if (get_mmc_num() > 0) {
671 puts("No MMC device available\n");
672 return CMD_RET_FAILURE;
675 return cp->cmd(cmdtp, flag, argc, argv);
679 mmc, 7, 1, do_mmcops,
681 "info - display info of the current MMC device\n"
682 "mmc read addr blk# cnt\n"
683 "mmc write addr blk# cnt\n"
684 "mmc erase blk# cnt\n"
686 "mmc part - lists available partition on current mmc device\n"
687 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
688 "mmc list - lists available devices\n"
689 #ifdef CONFIG_SUPPORT_EMMC_BOOT
690 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
691 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
692 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
693 " - Change sizes of boot and RPMB partitions of specified device\n"
694 "mmc partconf dev boot_ack boot_partition partition_access\n"
695 " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
696 "mmc rst-function dev value\n"
697 " - Change the RST_n_FUNCTION field of the specified device\n"
698 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
700 #ifdef CONFIG_SUPPORT_EMMC_RPMB
701 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
702 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
703 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
704 "mmc rpmb counter - read the value of the write counter\n"
706 "mmc setdsr <value> - set DSR register value\n"
709 /* Old command kept for compatibility. Same as 'mmc info' */
711 mmcinfo, 1, 0, do_mmcinfo,
713 "- display info of the current MMC device"
716 #endif /* !CONFIG_GENERIC_MMC */