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 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
99 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
100 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
101 puts("User Capacity: ");
102 print_size(mmc->capacity_user, usr_enh ? " ENH\n" : "\n");
104 puts("User Enhanced Start: ");
105 print_size(mmc->enh_user_start, "\n");
106 puts("User Enhanced Size: ");
107 print_size(mmc->enh_user_size, "\n");
109 puts("Boot Capacity: ");
110 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
111 puts("RPMB Capacity: ");
112 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
113 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
114 bool is_enh = has_enh &&
115 (mmc->part_attr & EXT_CSD_ENH_GP(i));
116 if (mmc->capacity_gp[i]) {
117 printf("GP%i Capacity: ", i+1);
118 print_size(mmc->capacity_gp[i],
119 is_enh ? " ENH\n" : "\n");
124 static struct mmc *init_mmc_device(int dev, bool force_init)
127 mmc = find_mmc_device(dev);
129 printf("no mmc device at slot %x\n", dev);
138 static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
142 if (curr_device < 0) {
143 if (get_mmc_num() > 0)
146 puts("No MMC device available\n");
151 mmc = init_mmc_device(curr_device, false);
153 return CMD_RET_FAILURE;
156 return CMD_RET_SUCCESS;
159 #ifdef CONFIG_SUPPORT_EMMC_RPMB
160 static int confirm_key_prog(void)
162 puts("Warning: Programming authentication key can be done only once !\n"
163 " Use this command only if you are sure of what you are doing,\n"
164 "Really perform the key programming? <y/N> ");
168 puts("Authentication key programming aborted\n");
171 static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag,
172 int argc, char * const argv[])
175 struct mmc *mmc = find_mmc_device(curr_device);
178 return CMD_RET_USAGE;
180 key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
181 if (!confirm_key_prog())
182 return CMD_RET_FAILURE;
183 if (mmc_rpmb_set_key(mmc, key_addr)) {
184 printf("ERROR - Key already programmed ?\n");
185 return CMD_RET_FAILURE;
187 return CMD_RET_SUCCESS;
189 static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag,
190 int argc, char * const argv[])
195 void *key_addr = NULL;
196 struct mmc *mmc = find_mmc_device(curr_device);
199 return CMD_RET_USAGE;
201 addr = (void *)simple_strtoul(argv[1], NULL, 16);
202 blk = simple_strtoul(argv[2], NULL, 16);
203 cnt = simple_strtoul(argv[3], NULL, 16);
206 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
208 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
209 curr_device, blk, cnt);
210 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
212 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
214 return CMD_RET_FAILURE;
215 return CMD_RET_SUCCESS;
217 static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag,
218 int argc, char * const argv[])
224 struct mmc *mmc = find_mmc_device(curr_device);
227 return CMD_RET_USAGE;
229 addr = (void *)simple_strtoul(argv[1], NULL, 16);
230 blk = simple_strtoul(argv[2], NULL, 16);
231 cnt = simple_strtoul(argv[3], NULL, 16);
232 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
234 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
235 curr_device, blk, cnt);
236 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
238 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
240 return CMD_RET_FAILURE;
241 return CMD_RET_SUCCESS;
243 static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag,
244 int argc, char * const argv[])
246 unsigned long counter;
247 struct mmc *mmc = find_mmc_device(curr_device);
249 if (mmc_rpmb_get_counter(mmc, &counter))
250 return CMD_RET_FAILURE;
251 printf("RPMB Write counter= %lx\n", counter);
252 return CMD_RET_SUCCESS;
255 static cmd_tbl_t cmd_rpmb[] = {
256 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
257 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
258 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
259 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
262 static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag,
263 int argc, char * const argv[])
270 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
272 /* Drop the rpmb subcommand */
276 if (cp == NULL || argc > cp->maxargs)
277 return CMD_RET_USAGE;
278 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
279 return CMD_RET_SUCCESS;
281 mmc = init_mmc_device(curr_device, false);
283 return CMD_RET_FAILURE;
285 if (!(mmc->version & MMC_VERSION_MMC)) {
286 printf("It is not a EMMC device\n");
287 return CMD_RET_FAILURE;
289 if (mmc->version < MMC_VERSION_4_41) {
290 printf("RPMB not supported before version 4.41\n");
291 return CMD_RET_FAILURE;
293 /* Switch to the RPMB partition */
294 original_part = mmc->part_num;
295 if (mmc->part_num != MMC_PART_RPMB) {
296 if (mmc_switch_part(curr_device, MMC_PART_RPMB) != 0)
297 return CMD_RET_FAILURE;
298 mmc->part_num = MMC_PART_RPMB;
300 ret = cp->cmd(cmdtp, flag, argc, argv);
302 /* Return to original partition */
303 if (mmc->part_num != original_part) {
304 if (mmc_switch_part(curr_device, original_part) != 0)
305 return CMD_RET_FAILURE;
306 mmc->part_num = original_part;
312 static int do_mmc_read(cmd_tbl_t *cmdtp, int flag,
313 int argc, char * const argv[])
320 return CMD_RET_USAGE;
322 addr = (void *)simple_strtoul(argv[1], NULL, 16);
323 blk = simple_strtoul(argv[2], NULL, 16);
324 cnt = simple_strtoul(argv[3], NULL, 16);
326 mmc = init_mmc_device(curr_device, false);
328 return CMD_RET_FAILURE;
330 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
331 curr_device, blk, cnt);
333 n = mmc->block_dev.block_read(curr_device, blk, cnt, addr);
334 /* flush cache after read */
335 flush_cache((ulong)addr, cnt * 512); /* FIXME */
336 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
338 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
340 static int do_mmc_write(cmd_tbl_t *cmdtp, int flag,
341 int argc, char * const argv[])
348 return CMD_RET_USAGE;
350 addr = (void *)simple_strtoul(argv[1], NULL, 16);
351 blk = simple_strtoul(argv[2], NULL, 16);
352 cnt = simple_strtoul(argv[3], NULL, 16);
354 mmc = init_mmc_device(curr_device, false);
356 return CMD_RET_FAILURE;
358 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
359 curr_device, blk, cnt);
361 if (mmc_getwp(mmc) == 1) {
362 printf("Error: card is write protected!\n");
363 return CMD_RET_FAILURE;
365 n = mmc->block_dev.block_write(curr_device, blk, cnt, addr);
366 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
368 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
370 static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,
371 int argc, char * const argv[])
377 return CMD_RET_USAGE;
379 blk = simple_strtoul(argv[1], NULL, 16);
380 cnt = simple_strtoul(argv[2], NULL, 16);
382 mmc = init_mmc_device(curr_device, false);
384 return CMD_RET_FAILURE;
386 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
387 curr_device, blk, cnt);
389 if (mmc_getwp(mmc) == 1) {
390 printf("Error: card is write protected!\n");
391 return CMD_RET_FAILURE;
393 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
394 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
396 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
398 static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
399 int argc, char * const argv[])
403 mmc = init_mmc_device(curr_device, true);
405 return CMD_RET_FAILURE;
407 return CMD_RET_SUCCESS;
409 static int do_mmc_part(cmd_tbl_t *cmdtp, int flag,
410 int argc, char * const argv[])
412 block_dev_desc_t *mmc_dev;
415 mmc = init_mmc_device(curr_device, false);
417 return CMD_RET_FAILURE;
419 mmc_dev = mmc_get_dev(curr_device);
420 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
422 return CMD_RET_SUCCESS;
425 puts("get mmc type error!\n");
426 return CMD_RET_FAILURE;
428 static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag,
429 int argc, char * const argv[])
431 int dev, part = 0, ret;
436 } else if (argc == 2) {
437 dev = simple_strtoul(argv[1], NULL, 10);
438 } else if (argc == 3) {
439 dev = (int)simple_strtoul(argv[1], NULL, 10);
440 part = (int)simple_strtoul(argv[2], NULL, 10);
441 if (part > PART_ACCESS_MASK) {
442 printf("#part_num shouldn't be larger than %d\n",
444 return CMD_RET_FAILURE;
447 return CMD_RET_USAGE;
450 mmc = init_mmc_device(dev, true);
452 return CMD_RET_FAILURE;
454 ret = mmc_select_hwpart(dev, part);
455 printf("switch to partitions #%d, %s\n",
456 part, (!ret) ? "OK" : "ERROR");
461 if (mmc->part_config == MMCPART_NOAVAILABLE)
462 printf("mmc%d is current device\n", curr_device);
464 printf("mmc%d(part %d) is current device\n",
465 curr_device, mmc->part_num);
467 return CMD_RET_SUCCESS;
469 static int do_mmc_list(cmd_tbl_t *cmdtp, int flag,
470 int argc, char * const argv[])
472 print_mmc_devices('\n');
473 return CMD_RET_SUCCESS;
475 #ifdef CONFIG_SUPPORT_EMMC_BOOT
476 static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag,
477 int argc, char * const argv[])
481 u8 width, reset, mode;
484 return CMD_RET_USAGE;
485 dev = simple_strtoul(argv[1], NULL, 10);
486 width = simple_strtoul(argv[2], NULL, 10);
487 reset = simple_strtoul(argv[3], NULL, 10);
488 mode = simple_strtoul(argv[4], NULL, 10);
490 mmc = init_mmc_device(dev, false);
492 return CMD_RET_FAILURE;
495 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
496 return CMD_RET_FAILURE;
499 /* acknowledge to be sent during boot operation */
500 return mmc_set_boot_bus_width(mmc, width, reset, mode);
502 static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag,
503 int argc, char * const argv[])
507 u32 bootsize, rpmbsize;
510 return CMD_RET_USAGE;
511 dev = simple_strtoul(argv[1], NULL, 10);
512 bootsize = simple_strtoul(argv[2], NULL, 10);
513 rpmbsize = simple_strtoul(argv[3], NULL, 10);
515 mmc = init_mmc_device(dev, false);
517 return CMD_RET_FAILURE;
520 printf("It is not a EMMC device\n");
521 return CMD_RET_FAILURE;
524 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
525 printf("EMMC boot partition Size change Failed.\n");
526 return CMD_RET_FAILURE;
529 printf("EMMC boot partition Size %d MB\n", bootsize);
530 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
531 return CMD_RET_SUCCESS;
533 static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag,
534 int argc, char * const argv[])
538 u8 ack, part_num, access;
541 return CMD_RET_USAGE;
543 dev = simple_strtoul(argv[1], NULL, 10);
544 ack = simple_strtoul(argv[2], NULL, 10);
545 part_num = simple_strtoul(argv[3], NULL, 10);
546 access = simple_strtoul(argv[4], NULL, 10);
548 mmc = init_mmc_device(dev, false);
550 return CMD_RET_FAILURE;
553 puts("PARTITION_CONFIG only exists on eMMC\n");
554 return CMD_RET_FAILURE;
557 /* acknowledge to be sent during boot operation */
558 return mmc_set_part_conf(mmc, ack, part_num, access);
560 static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag,
561 int argc, char * const argv[])
568 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
569 * The only valid values are 0x0, 0x1 and 0x2 and writing
570 * a value of 0x1 or 0x2 sets the value permanently.
573 return CMD_RET_USAGE;
575 dev = simple_strtoul(argv[1], NULL, 10);
576 enable = simple_strtoul(argv[2], NULL, 10);
578 if (enable > 2 || enable < 0) {
579 puts("Invalid RST_n_ENABLE value\n");
580 return CMD_RET_USAGE;
583 mmc = init_mmc_device(dev, false);
585 return CMD_RET_FAILURE;
588 puts("RST_n_FUNCTION only exists on eMMC\n");
589 return CMD_RET_FAILURE;
592 return mmc_set_rst_n_function(mmc, enable);
595 static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag,
596 int argc, char * const argv[])
603 return CMD_RET_USAGE;
604 val = simple_strtoul(argv[2], NULL, 16);
606 mmc = find_mmc_device(curr_device);
608 printf("no mmc device at slot %x\n", curr_device);
609 return CMD_RET_FAILURE;
611 ret = mmc_set_dsr(mmc, val);
612 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
616 return CMD_RET_FAILURE;
618 return CMD_RET_SUCCESS;
623 static cmd_tbl_t cmd_mmc[] = {
624 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
625 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
626 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
627 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
628 U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
629 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
630 U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
631 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
632 #ifdef CONFIG_SUPPORT_EMMC_BOOT
633 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
634 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
635 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
636 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
638 #ifdef CONFIG_SUPPORT_EMMC_RPMB
639 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
641 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
644 static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
648 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
650 /* Drop the mmc command */
654 if (cp == NULL || argc > cp->maxargs)
655 return CMD_RET_USAGE;
656 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
657 return CMD_RET_SUCCESS;
659 if (curr_device < 0) {
660 if (get_mmc_num() > 0) {
663 puts("No MMC device available\n");
664 return CMD_RET_FAILURE;
667 return cp->cmd(cmdtp, flag, argc, argv);
671 mmc, 7, 1, do_mmcops,
673 "info - display info of the current MMC device\n"
674 "mmc read addr blk# cnt\n"
675 "mmc write addr blk# cnt\n"
676 "mmc erase blk# cnt\n"
678 "mmc part - lists available partition on current mmc device\n"
679 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
680 "mmc list - lists available devices\n"
681 #ifdef CONFIG_SUPPORT_EMMC_BOOT
682 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
683 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
684 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
685 " - Change sizes of boot and RPMB partitions of specified device\n"
686 "mmc partconf dev boot_ack boot_partition partition_access\n"
687 " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
688 "mmc rst-function dev value\n"
689 " - Change the RST_n_FUNCTION field of the specified device\n"
690 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
692 #ifdef CONFIG_SUPPORT_EMMC_RPMB
693 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
694 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
695 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
696 "mmc rpmb counter - read the value of the write counter\n"
698 "mmc setdsr <value> - set DSR register value\n"
701 /* Old command kept for compatibility. Same as 'mmc info' */
703 mmcinfo, 1, 0, do_mmcinfo,
705 "- display info of the current MMC device"
708 #endif /* !CONFIG_GENERIC_MMC */