1 // SPDX-License-Identifier: GPL-2.0+
4 * Kyle Harris, kharris@nexus-tech.net
14 #include <sparse_format.h>
15 #include <image-sparse.h>
17 static int curr_device = -1;
19 static void print_mmcinfo(struct mmc *mmc)
23 printf("Device: %s\n", mmc->cfg->name);
24 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
25 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
26 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
27 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
28 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
30 printf("Bus Speed: %d\n", mmc->clock);
31 #if CONFIG_IS_ENABLED(MMC_VERBOSE)
32 printf("Mode: %s\n", mmc_mode_name(mmc->selected_mode));
33 mmc_dump_capabilities("card capabilities", mmc->card_caps);
34 mmc_dump_capabilities("host capabilities", mmc->host_caps);
36 printf("Rd Block Len: %d\n", mmc->read_bl_len);
38 printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
39 EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
40 EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
41 if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
42 printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
45 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
47 print_size(mmc->capacity, "\n");
49 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
50 mmc->ddr_mode ? " DDR" : "");
52 #if CONFIG_IS_ENABLED(MMC_WRITE)
53 puts("Erase Group Size: ");
54 print_size(((u64)mmc->erase_grp_size) << 9, "\n");
57 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
58 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
59 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
60 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
64 #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
65 puts("HC WP Group Size: ");
66 print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
69 puts("User Capacity: ");
70 print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
71 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
76 puts("User Enhanced Start: ");
77 print_size(mmc->enh_user_start, "\n");
78 puts("User Enhanced Size: ");
79 print_size(mmc->enh_user_size, "\n");
81 puts("Boot Capacity: ");
82 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
83 puts("RPMB Capacity: ");
84 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
86 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
87 bool is_enh = has_enh &&
88 (mmc->part_attr & EXT_CSD_ENH_GP(i));
89 if (mmc->capacity_gp[i]) {
90 printf("GP%i Capacity: ", i+1);
91 print_size(mmc->capacity_gp[i],
92 is_enh ? " ENH" : "");
93 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
99 ret = mmc_send_ext_csd(mmc, ext_csd);
102 wp = ext_csd[EXT_CSD_BOOT_WP_STATUS];
103 for (i = 0; i < 2; ++i) {
104 printf("Boot area %d is ", i);
107 printf("not write protected\n");
110 printf("power on protected\n");
113 printf("permanently protected\n");
116 printf("in reserved protection state\n");
123 static struct mmc *init_mmc_device(int dev, bool force_init)
126 mmc = find_mmc_device(dev);
128 printf("no mmc device at slot %x\n", dev);
140 #ifdef CONFIG_BLOCK_CACHE
141 struct blk_desc *bd = mmc_get_blk_desc(mmc);
142 blkcache_invalidate(bd->if_type, bd->devnum);
148 static int do_mmcinfo(struct cmd_tbl *cmdtp, int flag, int argc,
153 if (curr_device < 0) {
154 if (get_mmc_num() > 0)
157 puts("No MMC device available\n");
162 mmc = init_mmc_device(curr_device, false);
164 return CMD_RET_FAILURE;
167 return CMD_RET_SUCCESS;
170 #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
171 static int confirm_key_prog(void)
173 puts("Warning: Programming authentication key can be done only once !\n"
174 " Use this command only if you are sure of what you are doing,\n"
175 "Really perform the key programming? <y/N> ");
179 puts("Authentication key programming aborted\n");
183 static int do_mmcrpmb_key(struct cmd_tbl *cmdtp, int flag,
184 int argc, char *const argv[])
187 struct mmc *mmc = find_mmc_device(curr_device);
190 return CMD_RET_USAGE;
192 key_addr = (void *)hextoul(argv[1], NULL);
193 if (!confirm_key_prog())
194 return CMD_RET_FAILURE;
195 if (mmc_rpmb_set_key(mmc, key_addr)) {
196 printf("ERROR - Key already programmed ?\n");
197 return CMD_RET_FAILURE;
199 return CMD_RET_SUCCESS;
202 static int do_mmcrpmb_read(struct cmd_tbl *cmdtp, int flag,
203 int argc, char *const argv[])
208 void *key_addr = NULL;
209 struct mmc *mmc = find_mmc_device(curr_device);
212 return CMD_RET_USAGE;
214 addr = (void *)hextoul(argv[1], NULL);
215 blk = hextoul(argv[2], NULL);
216 cnt = hextoul(argv[3], NULL);
219 key_addr = (void *)hextoul(argv[4], NULL);
221 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
222 curr_device, blk, cnt);
223 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
225 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
227 return CMD_RET_FAILURE;
228 return CMD_RET_SUCCESS;
231 static int do_mmcrpmb_write(struct cmd_tbl *cmdtp, int flag,
232 int argc, char *const argv[])
238 struct mmc *mmc = find_mmc_device(curr_device);
241 return CMD_RET_USAGE;
243 addr = (void *)hextoul(argv[1], NULL);
244 blk = hextoul(argv[2], NULL);
245 cnt = hextoul(argv[3], NULL);
246 key_addr = (void *)hextoul(argv[4], NULL);
248 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
249 curr_device, blk, cnt);
250 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
252 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
254 return CMD_RET_FAILURE;
255 return CMD_RET_SUCCESS;
258 static int do_mmcrpmb_counter(struct cmd_tbl *cmdtp, int flag,
259 int argc, char *const argv[])
261 unsigned long counter;
262 struct mmc *mmc = find_mmc_device(curr_device);
264 if (mmc_rpmb_get_counter(mmc, &counter))
265 return CMD_RET_FAILURE;
266 printf("RPMB Write counter= %lx\n", counter);
267 return CMD_RET_SUCCESS;
270 static struct cmd_tbl cmd_rpmb[] = {
271 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
272 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
273 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
274 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
277 static int do_mmcrpmb(struct cmd_tbl *cmdtp, int flag,
278 int argc, char *const argv[])
285 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
287 /* Drop the rpmb subcommand */
291 if (cp == NULL || argc > cp->maxargs)
292 return CMD_RET_USAGE;
293 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
294 return CMD_RET_SUCCESS;
296 mmc = init_mmc_device(curr_device, false);
298 return CMD_RET_FAILURE;
300 if (!(mmc->version & MMC_VERSION_MMC)) {
301 printf("It is not an eMMC device\n");
302 return CMD_RET_FAILURE;
304 if (mmc->version < MMC_VERSION_4_41) {
305 printf("RPMB not supported before version 4.41\n");
306 return CMD_RET_FAILURE;
308 /* Switch to the RPMB partition */
310 original_part = mmc->block_dev.hwpart;
312 original_part = mmc_get_blk_desc(mmc)->hwpart;
314 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, MMC_PART_RPMB) !=
316 return CMD_RET_FAILURE;
317 ret = cp->cmd(cmdtp, flag, argc, argv);
319 /* Return to original partition */
320 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, original_part) !=
322 return CMD_RET_FAILURE;
327 static int do_mmc_read(struct cmd_tbl *cmdtp, int flag,
328 int argc, char *const argv[])
335 return CMD_RET_USAGE;
337 addr = (void *)hextoul(argv[1], NULL);
338 blk = hextoul(argv[2], NULL);
339 cnt = hextoul(argv[3], NULL);
341 mmc = init_mmc_device(curr_device, false);
343 return CMD_RET_FAILURE;
345 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
346 curr_device, blk, cnt);
348 n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
349 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
351 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
354 #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
355 static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk,
356 lbaint_t blkcnt, const void *buffer)
358 struct blk_desc *dev_desc = info->priv;
360 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
363 static lbaint_t mmc_sparse_reserve(struct sparse_storage *info,
364 lbaint_t blk, lbaint_t blkcnt)
369 static int do_mmc_sparse_write(struct cmd_tbl *cmdtp, int flag,
370 int argc, char *const argv[])
372 struct sparse_storage sparse;
373 struct blk_desc *dev_desc;
380 return CMD_RET_USAGE;
382 addr = (void *)hextoul(argv[1], NULL);
383 blk = hextoul(argv[2], NULL);
385 if (!is_sparse_image(addr)) {
386 printf("Not a sparse image\n");
387 return CMD_RET_FAILURE;
390 mmc = init_mmc_device(curr_device, false);
392 return CMD_RET_FAILURE;
394 printf("\nMMC Sparse write: dev # %d, block # %d ... ",
397 if (mmc_getwp(mmc) == 1) {
398 printf("Error: card is write protected!\n");
399 return CMD_RET_FAILURE;
402 dev_desc = mmc_get_blk_desc(mmc);
403 sparse.priv = dev_desc;
406 sparse.size = dev_desc->lba - blk;
407 sparse.write = mmc_sparse_write;
408 sparse.reserve = mmc_sparse_reserve;
410 sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz);
412 if (write_sparse_image(&sparse, dest, addr, NULL))
413 return CMD_RET_FAILURE;
415 return CMD_RET_SUCCESS;
419 #if CONFIG_IS_ENABLED(MMC_WRITE)
420 static int do_mmc_write(struct cmd_tbl *cmdtp, int flag,
421 int argc, char *const argv[])
428 return CMD_RET_USAGE;
430 addr = (void *)hextoul(argv[1], NULL);
431 blk = hextoul(argv[2], NULL);
432 cnt = hextoul(argv[3], NULL);
434 mmc = init_mmc_device(curr_device, false);
436 return CMD_RET_FAILURE;
438 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
439 curr_device, blk, cnt);
441 if (mmc_getwp(mmc) == 1) {
442 printf("Error: card is write protected!\n");
443 return CMD_RET_FAILURE;
445 n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
446 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
448 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
451 static int do_mmc_erase(struct cmd_tbl *cmdtp, int flag,
452 int argc, char *const argv[])
458 return CMD_RET_USAGE;
460 blk = hextoul(argv[1], NULL);
461 cnt = hextoul(argv[2], NULL);
463 mmc = init_mmc_device(curr_device, false);
465 return CMD_RET_FAILURE;
467 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
468 curr_device, blk, cnt);
470 if (mmc_getwp(mmc) == 1) {
471 printf("Error: card is write protected!\n");
472 return CMD_RET_FAILURE;
474 n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
475 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
477 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
481 static int do_mmc_rescan(struct cmd_tbl *cmdtp, int flag,
482 int argc, char *const argv[])
486 mmc = init_mmc_device(curr_device, true);
488 return CMD_RET_FAILURE;
490 return CMD_RET_SUCCESS;
493 static int do_mmc_part(struct cmd_tbl *cmdtp, int flag,
494 int argc, char *const argv[])
496 struct blk_desc *mmc_dev;
499 mmc = init_mmc_device(curr_device, false);
501 return CMD_RET_FAILURE;
503 mmc_dev = blk_get_devnum_by_type(IF_TYPE_MMC, curr_device);
504 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
506 return CMD_RET_SUCCESS;
509 puts("get mmc type error!\n");
510 return CMD_RET_FAILURE;
513 static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag,
514 int argc, char *const argv[])
516 int dev, part = 0, ret;
521 } else if (argc == 2) {
522 dev = dectoul(argv[1], NULL);
523 } else if (argc == 3) {
524 dev = (int)dectoul(argv[1], NULL);
525 part = (int)dectoul(argv[2], NULL);
526 if (part > PART_ACCESS_MASK) {
527 printf("#part_num shouldn't be larger than %d\n",
529 return CMD_RET_FAILURE;
532 return CMD_RET_USAGE;
535 mmc = init_mmc_device(dev, true);
537 return CMD_RET_FAILURE;
539 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
540 printf("switch to partitions #%d, %s\n",
541 part, (!ret) ? "OK" : "ERROR");
546 if (mmc->part_config == MMCPART_NOAVAILABLE)
547 printf("mmc%d is current device\n", curr_device);
549 printf("mmc%d(part %d) is current device\n",
550 curr_device, mmc_get_blk_desc(mmc)->hwpart);
552 return CMD_RET_SUCCESS;
555 static int do_mmc_list(struct cmd_tbl *cmdtp, int flag,
556 int argc, char *const argv[])
558 print_mmc_devices('\n');
559 return CMD_RET_SUCCESS;
562 #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
563 static int parse_hwpart_user(struct mmc_hwpart_conf *pconf,
564 int argc, char *const argv[])
568 memset(&pconf->user, 0, sizeof(pconf->user));
571 if (!strcmp(argv[i], "enh")) {
574 pconf->user.enh_start =
575 dectoul(argv[i + 1], NULL);
576 pconf->user.enh_size =
577 dectoul(argv[i + 2], NULL);
579 } else if (!strcmp(argv[i], "wrrel")) {
582 pconf->user.wr_rel_change = 1;
583 if (!strcmp(argv[i+1], "on"))
584 pconf->user.wr_rel_set = 1;
585 else if (!strcmp(argv[i+1], "off"))
586 pconf->user.wr_rel_set = 0;
597 static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
598 int argc, char *const argv[])
602 memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
606 pconf->gp_part[pidx].size = dectoul(argv[0], NULL);
610 if (!strcmp(argv[i], "enh")) {
611 pconf->gp_part[pidx].enhanced = 1;
613 } else if (!strcmp(argv[i], "wrrel")) {
616 pconf->gp_part[pidx].wr_rel_change = 1;
617 if (!strcmp(argv[i+1], "on"))
618 pconf->gp_part[pidx].wr_rel_set = 1;
619 else if (!strcmp(argv[i+1], "off"))
620 pconf->gp_part[pidx].wr_rel_set = 0;
631 static int do_mmc_hwpartition(struct cmd_tbl *cmdtp, int flag,
632 int argc, char *const argv[])
635 struct mmc_hwpart_conf pconf = { };
636 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
639 mmc = init_mmc_device(curr_device, false);
641 return CMD_RET_FAILURE;
644 return CMD_RET_USAGE;
647 if (!strcmp(argv[i], "user")) {
649 r = parse_hwpart_user(&pconf, argc-i, &argv[i]);
651 return CMD_RET_USAGE;
653 } else if (!strncmp(argv[i], "gp", 2) &&
654 strlen(argv[i]) == 3 &&
655 argv[i][2] >= '1' && argv[i][2] <= '4') {
656 pidx = argv[i][2] - '1';
658 r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
660 return CMD_RET_USAGE;
662 } else if (!strcmp(argv[i], "check")) {
663 mode = MMC_HWPART_CONF_CHECK;
665 } else if (!strcmp(argv[i], "set")) {
666 mode = MMC_HWPART_CONF_SET;
668 } else if (!strcmp(argv[i], "complete")) {
669 mode = MMC_HWPART_CONF_COMPLETE;
672 return CMD_RET_USAGE;
676 puts("Partition configuration:\n");
677 if (pconf.user.enh_size) {
678 puts("\tUser Enhanced Start: ");
679 print_size(((u64)pconf.user.enh_start) << 9, "\n");
680 puts("\tUser Enhanced Size: ");
681 print_size(((u64)pconf.user.enh_size) << 9, "\n");
683 puts("\tNo enhanced user data area\n");
685 if (pconf.user.wr_rel_change)
686 printf("\tUser partition write reliability: %s\n",
687 pconf.user.wr_rel_set ? "on" : "off");
688 for (pidx = 0; pidx < 4; pidx++) {
689 if (pconf.gp_part[pidx].size) {
690 printf("\tGP%i Capacity: ", pidx+1);
691 print_size(((u64)pconf.gp_part[pidx].size) << 9,
692 pconf.gp_part[pidx].enhanced ?
695 printf("\tNo GP%i partition\n", pidx+1);
697 if (pconf.gp_part[pidx].wr_rel_change)
698 printf("\tGP%i write reliability: %s\n", pidx+1,
699 pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
702 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
703 if (mode == MMC_HWPART_CONF_COMPLETE)
704 puts("Partitioning successful, "
705 "power-cycle to make effective\n");
706 return CMD_RET_SUCCESS;
709 return CMD_RET_FAILURE;
714 #ifdef CONFIG_SUPPORT_EMMC_BOOT
715 static int do_mmc_bootbus(struct cmd_tbl *cmdtp, int flag,
716 int argc, char *const argv[])
720 u8 width, reset, mode;
723 return CMD_RET_USAGE;
724 dev = dectoul(argv[1], NULL);
725 width = dectoul(argv[2], NULL);
726 reset = dectoul(argv[3], NULL);
727 mode = dectoul(argv[4], NULL);
729 mmc = init_mmc_device(dev, false);
731 return CMD_RET_FAILURE;
734 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
735 return CMD_RET_FAILURE;
739 * BOOT_BUS_CONDITIONS[177]
741 * 0x0 : Use SDR + Backward compatible timing in boot operation
742 * 0x1 : Use SDR + High Speed Timing in boot operation mode
743 * 0x2 : Use DDR in boot operation
744 * RESET_BOOT_BUS_CONDITIONS
745 * 0x0 : Reset bus width to x1, SDR, Backward compatible
746 * 0x1 : Retain BOOT_BUS_WIDTH and BOOT_MODE
748 * 0x0 : x1(sdr) or x4 (ddr) buswidth
749 * 0x1 : x4(sdr/ddr) buswith
750 * 0x2 : x8(sdr/ddr) buswith
754 printf("boot_bus_width %d is invalid\n", width);
755 return CMD_RET_FAILURE;
759 printf("reset_boot_bus_width %d is invalid\n", reset);
760 return CMD_RET_FAILURE;
764 printf("reset_boot_bus_width %d is invalid\n", mode);
765 return CMD_RET_FAILURE;
768 /* acknowledge to be sent during boot operation */
769 if (mmc_set_boot_bus_width(mmc, width, reset, mode)) {
770 puts("BOOT_BUS_WIDTH is failed to change.\n");
771 return CMD_RET_FAILURE;
774 printf("Set to BOOT_BUS_WIDTH = 0x%x, RESET = 0x%x, BOOT_MODE = 0x%x\n",
776 return CMD_RET_SUCCESS;
779 static int do_mmc_boot_resize(struct cmd_tbl *cmdtp, int flag,
780 int argc, char *const argv[])
784 u32 bootsize, rpmbsize;
787 return CMD_RET_USAGE;
788 dev = dectoul(argv[1], NULL);
789 bootsize = dectoul(argv[2], NULL);
790 rpmbsize = dectoul(argv[3], NULL);
792 mmc = init_mmc_device(dev, false);
794 return CMD_RET_FAILURE;
797 printf("It is not an eMMC device\n");
798 return CMD_RET_FAILURE;
801 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
802 printf("EMMC boot partition Size change Failed.\n");
803 return CMD_RET_FAILURE;
806 printf("EMMC boot partition Size %d MB\n", bootsize);
807 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
808 return CMD_RET_SUCCESS;
811 static int mmc_partconf_print(struct mmc *mmc, const char *varname)
813 u8 ack, access, part;
815 if (mmc->part_config == MMCPART_NOAVAILABLE) {
816 printf("No part_config info for ver. 0x%x\n", mmc->version);
817 return CMD_RET_FAILURE;
820 access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
821 ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
822 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
825 env_set_hex(varname, part);
827 printf("EXT_CSD[179], PARTITION_CONFIG:\n"
829 "BOOT_PARTITION_ENABLE: 0x%x\n"
830 "PARTITION_ACCESS: 0x%x\n", ack, part, access);
832 return CMD_RET_SUCCESS;
835 static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag,
836 int argc, char *const argv[])
840 u8 ack, part_num, access;
842 if (argc != 2 && argc != 3 && argc != 5)
843 return CMD_RET_USAGE;
845 dev = dectoul(argv[1], NULL);
847 mmc = init_mmc_device(dev, false);
849 return CMD_RET_FAILURE;
852 puts("PARTITION_CONFIG only exists on eMMC\n");
853 return CMD_RET_FAILURE;
856 if (argc == 2 || argc == 3)
857 return mmc_partconf_print(mmc, argc == 3 ? argv[2] : NULL);
859 ack = dectoul(argv[2], NULL);
860 part_num = dectoul(argv[3], NULL);
861 access = dectoul(argv[4], NULL);
863 /* acknowledge to be sent during boot operation */
864 return mmc_set_part_conf(mmc, ack, part_num, access);
867 static int do_mmc_rst_func(struct cmd_tbl *cmdtp, int flag,
868 int argc, char *const argv[])
875 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
876 * The only valid values are 0x0, 0x1 and 0x2 and writing
877 * a value of 0x1 or 0x2 sets the value permanently.
880 return CMD_RET_USAGE;
882 dev = dectoul(argv[1], NULL);
883 enable = dectoul(argv[2], NULL);
886 puts("Invalid RST_n_ENABLE value\n");
887 return CMD_RET_USAGE;
890 mmc = init_mmc_device(dev, false);
892 return CMD_RET_FAILURE;
895 puts("RST_n_FUNCTION only exists on eMMC\n");
896 return CMD_RET_FAILURE;
899 return mmc_set_rst_n_function(mmc, enable);
902 static int do_mmc_setdsr(struct cmd_tbl *cmdtp, int flag,
903 int argc, char *const argv[])
910 return CMD_RET_USAGE;
911 val = hextoul(argv[1], NULL);
913 mmc = find_mmc_device(curr_device);
915 printf("no mmc device at slot %x\n", curr_device);
916 return CMD_RET_FAILURE;
918 ret = mmc_set_dsr(mmc, val);
919 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
923 return CMD_RET_FAILURE;
925 return CMD_RET_SUCCESS;
930 #ifdef CONFIG_CMD_BKOPS_ENABLE
931 static int do_mmc_bkops_enable(struct cmd_tbl *cmdtp, int flag,
932 int argc, char *const argv[])
938 return CMD_RET_USAGE;
940 dev = dectoul(argv[1], NULL);
942 mmc = init_mmc_device(dev, false);
944 return CMD_RET_FAILURE;
947 puts("BKOPS_EN only exists on eMMC\n");
948 return CMD_RET_FAILURE;
951 return mmc_set_bkops_enable(mmc);
955 static int do_mmc_boot_wp(struct cmd_tbl *cmdtp, int flag,
956 int argc, char * const argv[])
961 mmc = init_mmc_device(curr_device, false);
963 return CMD_RET_FAILURE;
965 printf("It is not an eMMC device\n");
966 return CMD_RET_FAILURE;
968 err = mmc_boot_wp(mmc);
970 return CMD_RET_FAILURE;
971 printf("boot areas protected\n");
972 return CMD_RET_SUCCESS;
975 static struct cmd_tbl cmd_mmc[] = {
976 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
977 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
978 U_BOOT_CMD_MKENT(wp, 1, 0, do_mmc_boot_wp, "", ""),
979 #if CONFIG_IS_ENABLED(MMC_WRITE)
980 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
981 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
983 #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
984 U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
986 U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
987 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
988 U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
989 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
990 #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
991 U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
993 #ifdef CONFIG_SUPPORT_EMMC_BOOT
994 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
995 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
996 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
997 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
999 #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
1000 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
1002 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
1003 #ifdef CONFIG_CMD_BKOPS_ENABLE
1004 U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
1008 static int do_mmcops(struct cmd_tbl *cmdtp, int flag, int argc,
1013 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
1015 /* Drop the mmc command */
1019 if (cp == NULL || argc > cp->maxargs)
1020 return CMD_RET_USAGE;
1021 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
1022 return CMD_RET_SUCCESS;
1024 if (curr_device < 0) {
1025 if (get_mmc_num() > 0) {
1028 puts("No MMC device available\n");
1029 return CMD_RET_FAILURE;
1032 return cp->cmd(cmdtp, flag, argc, argv);
1036 mmc, 29, 1, do_mmcops,
1038 "info - display info of the current MMC device\n"
1039 "mmc read addr blk# cnt\n"
1040 "mmc write addr blk# cnt\n"
1041 #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
1042 "mmc swrite addr blk#\n"
1044 "mmc erase blk# cnt\n"
1046 "mmc part - lists available partition on current mmc device\n"
1047 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
1048 "mmc list - lists available devices\n"
1049 "mmc wp - power on write protect boot partitions\n"
1050 #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
1051 "mmc hwpartition <USER> <GP> <MODE> - does hardware partitioning\n"
1052 " arguments (sizes in 512-byte blocks):\n"
1053 " USER - <user> <enh> <start> <cnt> <wrrel> <{on|off}>\n"
1054 " : sets user data area attributes\n"
1055 " GP - <{gp1|gp2|gp3|gp4}> <cnt> <enh> <wrrel> <{on|off}>\n"
1056 " : general purpose partition\n"
1057 " MODE - <{check|set|complete}>\n"
1058 " : mode, complete set partitioning completed\n"
1059 " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
1060 " Power cycling is required to initialize partitions after set to complete.\n"
1062 #ifdef CONFIG_SUPPORT_EMMC_BOOT
1063 "mmc bootbus <dev> <boot_bus_width> <reset_boot_bus_width> <boot_mode>\n"
1064 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
1065 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
1066 " - Change sizes of boot and RPMB partitions of specified device\n"
1067 "mmc partconf <dev> [[varname] | [<boot_ack> <boot_partition> <partition_access>]]\n"
1068 " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
1069 " If showing the bits, optionally store the boot_partition field into varname\n"
1070 "mmc rst-function <dev> <value>\n"
1071 " - Change the RST_n_FUNCTION field of the specified device\n"
1072 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
1074 #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
1075 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
1076 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
1077 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
1078 "mmc rpmb counter - read the value of the write counter\n"
1080 "mmc setdsr <value> - set DSR register value\n"
1081 #ifdef CONFIG_CMD_BKOPS_ENABLE
1082 "mmc bkops-enable <dev> - enable background operations handshake on device\n"
1083 " WARNING: This is a write-once setting.\n"
1087 /* Old command kept for compatibility. Same as 'mmc info' */
1089 mmcinfo, 1, 0, do_mmcinfo,
1091 "- display info of the current MMC device"