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");
124 static struct mmc *__init_mmc_device(int dev, bool force_init,
125 enum bus_mode speed_mode)
128 mmc = find_mmc_device(dev);
130 printf("no mmc device at slot %x\n", dev);
140 if (IS_ENABLED(CONFIG_MMC_SPEED_MODE_SET))
141 mmc->user_speed_mode = speed_mode;
146 #ifdef CONFIG_BLOCK_CACHE
147 struct blk_desc *bd = mmc_get_blk_desc(mmc);
148 blkcache_invalidate(bd->if_type, bd->devnum);
154 static struct mmc *init_mmc_device(int dev, bool force_init)
156 return __init_mmc_device(dev, force_init, MMC_MODES_END);
159 static int do_mmcinfo(struct cmd_tbl *cmdtp, int flag, int argc,
164 if (curr_device < 0) {
165 if (get_mmc_num() > 0)
168 puts("No MMC device available\n");
173 mmc = init_mmc_device(curr_device, false);
175 return CMD_RET_FAILURE;
178 return CMD_RET_SUCCESS;
181 #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
182 static int confirm_key_prog(void)
184 puts("Warning: Programming authentication key can be done only once !\n"
185 " Use this command only if you are sure of what you are doing,\n"
186 "Really perform the key programming? <y/N> ");
190 puts("Authentication key programming aborted\n");
194 static int do_mmcrpmb_key(struct cmd_tbl *cmdtp, int flag,
195 int argc, char *const argv[])
198 struct mmc *mmc = find_mmc_device(curr_device);
201 return CMD_RET_USAGE;
203 key_addr = (void *)hextoul(argv[1], NULL);
204 if (!confirm_key_prog())
205 return CMD_RET_FAILURE;
206 if (mmc_rpmb_set_key(mmc, key_addr)) {
207 printf("ERROR - Key already programmed ?\n");
208 return CMD_RET_FAILURE;
210 return CMD_RET_SUCCESS;
213 static int do_mmcrpmb_read(struct cmd_tbl *cmdtp, int flag,
214 int argc, char *const argv[])
219 void *key_addr = NULL;
220 struct mmc *mmc = find_mmc_device(curr_device);
223 return CMD_RET_USAGE;
225 addr = (void *)hextoul(argv[1], NULL);
226 blk = hextoul(argv[2], NULL);
227 cnt = hextoul(argv[3], NULL);
230 key_addr = (void *)hextoul(argv[4], NULL);
232 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
233 curr_device, blk, cnt);
234 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
236 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
238 return CMD_RET_FAILURE;
239 return CMD_RET_SUCCESS;
242 static int do_mmcrpmb_write(struct cmd_tbl *cmdtp, int flag,
243 int argc, char *const argv[])
249 struct mmc *mmc = find_mmc_device(curr_device);
252 return CMD_RET_USAGE;
254 addr = (void *)hextoul(argv[1], NULL);
255 blk = hextoul(argv[2], NULL);
256 cnt = hextoul(argv[3], NULL);
257 key_addr = (void *)hextoul(argv[4], NULL);
259 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
260 curr_device, blk, cnt);
261 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
263 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
265 return CMD_RET_FAILURE;
266 return CMD_RET_SUCCESS;
269 static int do_mmcrpmb_counter(struct cmd_tbl *cmdtp, int flag,
270 int argc, char *const argv[])
272 unsigned long counter;
273 struct mmc *mmc = find_mmc_device(curr_device);
275 if (mmc_rpmb_get_counter(mmc, &counter))
276 return CMD_RET_FAILURE;
277 printf("RPMB Write counter= %lx\n", counter);
278 return CMD_RET_SUCCESS;
281 static struct cmd_tbl cmd_rpmb[] = {
282 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
283 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
284 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
285 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
288 static int do_mmcrpmb(struct cmd_tbl *cmdtp, int flag,
289 int argc, char *const argv[])
296 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
298 /* Drop the rpmb subcommand */
302 if (cp == NULL || argc > cp->maxargs)
303 return CMD_RET_USAGE;
304 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
305 return CMD_RET_SUCCESS;
307 mmc = init_mmc_device(curr_device, false);
309 return CMD_RET_FAILURE;
311 if (!(mmc->version & MMC_VERSION_MMC)) {
312 printf("It is not an eMMC device\n");
313 return CMD_RET_FAILURE;
315 if (mmc->version < MMC_VERSION_4_41) {
316 printf("RPMB not supported before version 4.41\n");
317 return CMD_RET_FAILURE;
319 /* Switch to the RPMB partition */
321 original_part = mmc->block_dev.hwpart;
323 original_part = mmc_get_blk_desc(mmc)->hwpart;
325 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, MMC_PART_RPMB) !=
327 return CMD_RET_FAILURE;
328 ret = cp->cmd(cmdtp, flag, argc, argv);
330 /* Return to original partition */
331 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, original_part) !=
333 return CMD_RET_FAILURE;
338 static int do_mmc_read(struct cmd_tbl *cmdtp, int flag,
339 int argc, char *const argv[])
346 return CMD_RET_USAGE;
348 addr = (void *)hextoul(argv[1], NULL);
349 blk = hextoul(argv[2], NULL);
350 cnt = hextoul(argv[3], NULL);
352 mmc = init_mmc_device(curr_device, false);
354 return CMD_RET_FAILURE;
356 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
357 curr_device, blk, cnt);
359 n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
360 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
362 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
365 #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
366 static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk,
367 lbaint_t blkcnt, const void *buffer)
369 struct blk_desc *dev_desc = info->priv;
371 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
374 static lbaint_t mmc_sparse_reserve(struct sparse_storage *info,
375 lbaint_t blk, lbaint_t blkcnt)
380 static int do_mmc_sparse_write(struct cmd_tbl *cmdtp, int flag,
381 int argc, char *const argv[])
383 struct sparse_storage sparse;
384 struct blk_desc *dev_desc;
391 return CMD_RET_USAGE;
393 addr = (void *)hextoul(argv[1], NULL);
394 blk = hextoul(argv[2], NULL);
396 if (!is_sparse_image(addr)) {
397 printf("Not a sparse image\n");
398 return CMD_RET_FAILURE;
401 mmc = init_mmc_device(curr_device, false);
403 return CMD_RET_FAILURE;
405 printf("\nMMC Sparse write: dev # %d, block # %d ... ",
408 if (mmc_getwp(mmc) == 1) {
409 printf("Error: card is write protected!\n");
410 return CMD_RET_FAILURE;
413 dev_desc = mmc_get_blk_desc(mmc);
414 sparse.priv = dev_desc;
417 sparse.size = dev_desc->lba - blk;
418 sparse.write = mmc_sparse_write;
419 sparse.reserve = mmc_sparse_reserve;
421 sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz);
423 if (write_sparse_image(&sparse, dest, addr, NULL))
424 return CMD_RET_FAILURE;
426 return CMD_RET_SUCCESS;
430 #if CONFIG_IS_ENABLED(MMC_WRITE)
431 static int do_mmc_write(struct cmd_tbl *cmdtp, int flag,
432 int argc, char *const argv[])
439 return CMD_RET_USAGE;
441 addr = (void *)hextoul(argv[1], NULL);
442 blk = hextoul(argv[2], NULL);
443 cnt = hextoul(argv[3], NULL);
445 mmc = init_mmc_device(curr_device, false);
447 return CMD_RET_FAILURE;
449 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
450 curr_device, blk, cnt);
452 if (mmc_getwp(mmc) == 1) {
453 printf("Error: card is write protected!\n");
454 return CMD_RET_FAILURE;
456 n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
457 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
459 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
462 static int do_mmc_erase(struct cmd_tbl *cmdtp, int flag,
463 int argc, char *const argv[])
469 return CMD_RET_USAGE;
471 blk = hextoul(argv[1], NULL);
472 cnt = hextoul(argv[2], NULL);
474 mmc = init_mmc_device(curr_device, false);
476 return CMD_RET_FAILURE;
478 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
479 curr_device, blk, cnt);
481 if (mmc_getwp(mmc) == 1) {
482 printf("Error: card is write protected!\n");
483 return CMD_RET_FAILURE;
485 n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
486 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
488 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
492 static int do_mmc_rescan(struct cmd_tbl *cmdtp, int flag,
493 int argc, char *const argv[])
496 enum bus_mode speed_mode = MMC_MODES_END;
499 mmc = init_mmc_device(curr_device, true);
500 } else if (argc == 2) {
501 speed_mode = (int)dectoul(argv[1], NULL);
502 mmc = __init_mmc_device(curr_device, true, speed_mode);
504 return CMD_RET_USAGE;
508 return CMD_RET_FAILURE;
510 return CMD_RET_SUCCESS;
513 static int do_mmc_part(struct cmd_tbl *cmdtp, int flag,
514 int argc, char *const argv[])
516 struct blk_desc *mmc_dev;
519 mmc = init_mmc_device(curr_device, false);
521 return CMD_RET_FAILURE;
523 mmc_dev = blk_get_devnum_by_type(IF_TYPE_MMC, curr_device);
524 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
526 return CMD_RET_SUCCESS;
529 puts("get mmc type error!\n");
530 return CMD_RET_FAILURE;
533 static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag,
534 int argc, char *const argv[])
536 int dev, part = 0, ret;
538 enum bus_mode speed_mode = MMC_MODES_END;
542 mmc = init_mmc_device(dev, true);
543 } else if (argc == 2) {
544 dev = (int)dectoul(argv[1], NULL);
545 mmc = init_mmc_device(dev, true);
546 } else if (argc == 3) {
547 dev = (int)dectoul(argv[1], NULL);
548 part = (int)dectoul(argv[2], NULL);
549 if (part > PART_ACCESS_MASK) {
550 printf("#part_num shouldn't be larger than %d\n",
552 return CMD_RET_FAILURE;
554 mmc = init_mmc_device(dev, true);
555 } else if (argc == 4) {
556 dev = (int)dectoul(argv[1], NULL);
557 part = (int)dectoul(argv[2], NULL);
558 if (part > PART_ACCESS_MASK) {
559 printf("#part_num shouldn't be larger than %d\n",
561 return CMD_RET_FAILURE;
563 speed_mode = (int)dectoul(argv[3], NULL);
564 mmc = __init_mmc_device(dev, true, speed_mode);
566 return CMD_RET_USAGE;
570 return CMD_RET_FAILURE;
572 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
573 printf("switch to partitions #%d, %s\n",
574 part, (!ret) ? "OK" : "ERROR");
579 if (mmc->part_config == MMCPART_NOAVAILABLE)
580 printf("mmc%d is current device\n", curr_device);
582 printf("mmc%d(part %d) is current device\n",
583 curr_device, mmc_get_blk_desc(mmc)->hwpart);
585 return CMD_RET_SUCCESS;
588 static int do_mmc_list(struct cmd_tbl *cmdtp, int flag,
589 int argc, char *const argv[])
591 print_mmc_devices('\n');
592 return CMD_RET_SUCCESS;
595 #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
596 static void parse_hwpart_user_enh_size(struct mmc *mmc,
597 struct mmc_hwpart_conf *pconf,
602 pconf->user.enh_size = 0;
604 if (!strcmp(argv, "-")) { /* The rest of eMMC */
605 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
606 ret = mmc_send_ext_csd(mmc, ext_csd);
609 /* This value is in 512B block units */
610 pconf->user.enh_size =
611 ((ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 2] << 16) +
612 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 1] << 8) +
613 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT]) * 1024 *
614 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
615 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
616 pconf->user.enh_size -= pconf->user.enh_start;
618 pconf->user.enh_size = dectoul(argv, NULL);
622 static int parse_hwpart_user(struct mmc *mmc, struct mmc_hwpart_conf *pconf,
623 int argc, char *const argv[])
627 memset(&pconf->user, 0, sizeof(pconf->user));
630 if (!strcmp(argv[i], "enh")) {
633 pconf->user.enh_start =
634 dectoul(argv[i + 1], NULL);
635 parse_hwpart_user_enh_size(mmc, pconf, argv[i + 2]);
637 } else if (!strcmp(argv[i], "wrrel")) {
640 pconf->user.wr_rel_change = 1;
641 if (!strcmp(argv[i+1], "on"))
642 pconf->user.wr_rel_set = 1;
643 else if (!strcmp(argv[i+1], "off"))
644 pconf->user.wr_rel_set = 0;
655 static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
656 int argc, char *const argv[])
660 memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
664 pconf->gp_part[pidx].size = dectoul(argv[0], NULL);
668 if (!strcmp(argv[i], "enh")) {
669 pconf->gp_part[pidx].enhanced = 1;
671 } else if (!strcmp(argv[i], "wrrel")) {
674 pconf->gp_part[pidx].wr_rel_change = 1;
675 if (!strcmp(argv[i+1], "on"))
676 pconf->gp_part[pidx].wr_rel_set = 1;
677 else if (!strcmp(argv[i+1], "off"))
678 pconf->gp_part[pidx].wr_rel_set = 0;
689 static int do_mmc_hwpartition(struct cmd_tbl *cmdtp, int flag,
690 int argc, char *const argv[])
693 struct mmc_hwpart_conf pconf = { };
694 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
697 mmc = init_mmc_device(curr_device, false);
699 return CMD_RET_FAILURE;
702 return CMD_RET_USAGE;
705 if (!strcmp(argv[i], "user")) {
707 r = parse_hwpart_user(mmc, &pconf, argc - i, &argv[i]);
709 return CMD_RET_USAGE;
711 } else if (!strncmp(argv[i], "gp", 2) &&
712 strlen(argv[i]) == 3 &&
713 argv[i][2] >= '1' && argv[i][2] <= '4') {
714 pidx = argv[i][2] - '1';
716 r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
718 return CMD_RET_USAGE;
720 } else if (!strcmp(argv[i], "check")) {
721 mode = MMC_HWPART_CONF_CHECK;
723 } else if (!strcmp(argv[i], "set")) {
724 mode = MMC_HWPART_CONF_SET;
726 } else if (!strcmp(argv[i], "complete")) {
727 mode = MMC_HWPART_CONF_COMPLETE;
730 return CMD_RET_USAGE;
734 puts("Partition configuration:\n");
735 if (pconf.user.enh_size) {
736 puts("\tUser Enhanced Start: ");
737 print_size(((u64)pconf.user.enh_start) << 9, "\n");
738 puts("\tUser Enhanced Size: ");
739 print_size(((u64)pconf.user.enh_size) << 9, "\n");
741 puts("\tNo enhanced user data area\n");
743 if (pconf.user.wr_rel_change)
744 printf("\tUser partition write reliability: %s\n",
745 pconf.user.wr_rel_set ? "on" : "off");
746 for (pidx = 0; pidx < 4; pidx++) {
747 if (pconf.gp_part[pidx].size) {
748 printf("\tGP%i Capacity: ", pidx+1);
749 print_size(((u64)pconf.gp_part[pidx].size) << 9,
750 pconf.gp_part[pidx].enhanced ?
753 printf("\tNo GP%i partition\n", pidx+1);
755 if (pconf.gp_part[pidx].wr_rel_change)
756 printf("\tGP%i write reliability: %s\n", pidx+1,
757 pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
760 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
761 if (mode == MMC_HWPART_CONF_COMPLETE)
762 puts("Partitioning successful, "
763 "power-cycle to make effective\n");
764 return CMD_RET_SUCCESS;
767 return CMD_RET_FAILURE;
772 #ifdef CONFIG_SUPPORT_EMMC_BOOT
773 static int do_mmc_bootbus(struct cmd_tbl *cmdtp, int flag,
774 int argc, char *const argv[])
778 u8 width, reset, mode;
781 return CMD_RET_USAGE;
782 dev = dectoul(argv[1], NULL);
783 width = dectoul(argv[2], NULL);
784 reset = dectoul(argv[3], NULL);
785 mode = dectoul(argv[4], NULL);
787 mmc = init_mmc_device(dev, false);
789 return CMD_RET_FAILURE;
792 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
793 return CMD_RET_FAILURE;
797 * BOOT_BUS_CONDITIONS[177]
799 * 0x0 : Use SDR + Backward compatible timing in boot operation
800 * 0x1 : Use SDR + High Speed Timing in boot operation mode
801 * 0x2 : Use DDR in boot operation
802 * RESET_BOOT_BUS_CONDITIONS
803 * 0x0 : Reset bus width to x1, SDR, Backward compatible
804 * 0x1 : Retain BOOT_BUS_WIDTH and BOOT_MODE
806 * 0x0 : x1(sdr) or x4 (ddr) buswidth
807 * 0x1 : x4(sdr/ddr) buswith
808 * 0x2 : x8(sdr/ddr) buswith
812 printf("boot_bus_width %d is invalid\n", width);
813 return CMD_RET_FAILURE;
817 printf("reset_boot_bus_width %d is invalid\n", reset);
818 return CMD_RET_FAILURE;
822 printf("reset_boot_bus_width %d is invalid\n", mode);
823 return CMD_RET_FAILURE;
826 /* acknowledge to be sent during boot operation */
827 if (mmc_set_boot_bus_width(mmc, width, reset, mode)) {
828 puts("BOOT_BUS_WIDTH is failed to change.\n");
829 return CMD_RET_FAILURE;
832 printf("Set to BOOT_BUS_WIDTH = 0x%x, RESET = 0x%x, BOOT_MODE = 0x%x\n",
834 return CMD_RET_SUCCESS;
837 static int do_mmc_boot_resize(struct cmd_tbl *cmdtp, int flag,
838 int argc, char *const argv[])
842 u32 bootsize, rpmbsize;
845 return CMD_RET_USAGE;
846 dev = dectoul(argv[1], NULL);
847 bootsize = dectoul(argv[2], NULL);
848 rpmbsize = dectoul(argv[3], NULL);
850 mmc = init_mmc_device(dev, false);
852 return CMD_RET_FAILURE;
855 printf("It is not an eMMC device\n");
856 return CMD_RET_FAILURE;
859 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
860 printf("EMMC boot partition Size change Failed.\n");
861 return CMD_RET_FAILURE;
864 printf("EMMC boot partition Size %d MB\n", bootsize);
865 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
866 return CMD_RET_SUCCESS;
869 static int mmc_partconf_print(struct mmc *mmc, const char *varname)
871 u8 ack, access, part;
873 if (mmc->part_config == MMCPART_NOAVAILABLE) {
874 printf("No part_config info for ver. 0x%x\n", mmc->version);
875 return CMD_RET_FAILURE;
878 access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
879 ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
880 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
883 env_set_hex(varname, part);
885 printf("EXT_CSD[179], PARTITION_CONFIG:\n"
887 "BOOT_PARTITION_ENABLE: 0x%x\n"
888 "PARTITION_ACCESS: 0x%x\n", ack, part, access);
890 return CMD_RET_SUCCESS;
893 static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag,
894 int argc, char *const argv[])
898 u8 ack, part_num, access;
900 if (argc != 2 && argc != 3 && argc != 5)
901 return CMD_RET_USAGE;
903 dev = dectoul(argv[1], NULL);
905 mmc = init_mmc_device(dev, false);
907 return CMD_RET_FAILURE;
910 puts("PARTITION_CONFIG only exists on eMMC\n");
911 return CMD_RET_FAILURE;
914 if (argc == 2 || argc == 3)
915 return mmc_partconf_print(mmc, argc == 3 ? argv[2] : NULL);
917 ack = dectoul(argv[2], NULL);
918 part_num = dectoul(argv[3], NULL);
919 access = dectoul(argv[4], NULL);
921 /* acknowledge to be sent during boot operation */
922 return mmc_set_part_conf(mmc, ack, part_num, access);
925 static int do_mmc_rst_func(struct cmd_tbl *cmdtp, int flag,
926 int argc, char *const argv[])
933 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
934 * The only valid values are 0x0, 0x1 and 0x2 and writing
935 * a value of 0x1 or 0x2 sets the value permanently.
938 return CMD_RET_USAGE;
940 dev = dectoul(argv[1], NULL);
941 enable = dectoul(argv[2], NULL);
944 puts("Invalid RST_n_ENABLE value\n");
945 return CMD_RET_USAGE;
948 mmc = init_mmc_device(dev, false);
950 return CMD_RET_FAILURE;
953 puts("RST_n_FUNCTION only exists on eMMC\n");
954 return CMD_RET_FAILURE;
957 return mmc_set_rst_n_function(mmc, enable);
960 static int do_mmc_setdsr(struct cmd_tbl *cmdtp, int flag,
961 int argc, char *const argv[])
968 return CMD_RET_USAGE;
969 val = hextoul(argv[1], NULL);
971 mmc = find_mmc_device(curr_device);
973 printf("no mmc device at slot %x\n", curr_device);
974 return CMD_RET_FAILURE;
976 ret = mmc_set_dsr(mmc, val);
977 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
981 return CMD_RET_FAILURE;
983 return CMD_RET_SUCCESS;
988 #ifdef CONFIG_CMD_BKOPS_ENABLE
989 static int do_mmc_bkops_enable(struct cmd_tbl *cmdtp, int flag,
990 int argc, char *const argv[])
996 return CMD_RET_USAGE;
998 dev = dectoul(argv[1], NULL);
1000 mmc = init_mmc_device(dev, false);
1002 return CMD_RET_FAILURE;
1005 puts("BKOPS_EN only exists on eMMC\n");
1006 return CMD_RET_FAILURE;
1009 return mmc_set_bkops_enable(mmc);
1013 static int do_mmc_boot_wp(struct cmd_tbl *cmdtp, int flag,
1014 int argc, char * const argv[])
1019 mmc = init_mmc_device(curr_device, false);
1021 return CMD_RET_FAILURE;
1023 printf("It is not an eMMC device\n");
1024 return CMD_RET_FAILURE;
1026 err = mmc_boot_wp(mmc);
1028 return CMD_RET_FAILURE;
1029 printf("boot areas protected\n");
1030 return CMD_RET_SUCCESS;
1033 static struct cmd_tbl cmd_mmc[] = {
1034 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
1035 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
1036 U_BOOT_CMD_MKENT(wp, 1, 0, do_mmc_boot_wp, "", ""),
1037 #if CONFIG_IS_ENABLED(MMC_WRITE)
1038 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
1039 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
1041 #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
1042 U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
1044 U_BOOT_CMD_MKENT(rescan, 2, 1, do_mmc_rescan, "", ""),
1045 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
1046 U_BOOT_CMD_MKENT(dev, 4, 0, do_mmc_dev, "", ""),
1047 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
1048 #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
1049 U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
1051 #ifdef CONFIG_SUPPORT_EMMC_BOOT
1052 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
1053 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
1054 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
1055 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
1057 #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
1058 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
1060 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
1061 #ifdef CONFIG_CMD_BKOPS_ENABLE
1062 U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
1066 static int do_mmcops(struct cmd_tbl *cmdtp, int flag, int argc,
1071 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
1073 /* Drop the mmc command */
1077 if (cp == NULL || argc > cp->maxargs)
1078 return CMD_RET_USAGE;
1079 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
1080 return CMD_RET_SUCCESS;
1082 if (curr_device < 0) {
1083 if (get_mmc_num() > 0) {
1086 puts("No MMC device available\n");
1087 return CMD_RET_FAILURE;
1090 return cp->cmd(cmdtp, flag, argc, argv);
1094 mmc, 29, 1, do_mmcops,
1096 "info - display info of the current MMC device\n"
1097 "mmc read addr blk# cnt\n"
1098 "mmc write addr blk# cnt\n"
1099 #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
1100 "mmc swrite addr blk#\n"
1102 "mmc erase blk# cnt\n"
1103 "mmc rescan [mode]\n"
1104 "mmc part - lists available partition on current mmc device\n"
1105 "mmc dev [dev] [part] [mode] - show or set current mmc device [partition] and set mode\n"
1106 " - the required speed mode is passed as the index from the following list\n"
1107 " [MMC_LEGACY, MMC_HS, SD_HS, MMC_HS_52, MMC_DDR_52, UHS_SDR12, UHS_SDR25,\n"
1108 " UHS_SDR50, UHS_DDR50, UHS_SDR104, MMC_HS_200, MMC_HS_400, MMC_HS_400_ES]\n"
1109 "mmc list - lists available devices\n"
1110 "mmc wp - power on write protect boot partitions\n"
1111 #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
1112 "mmc hwpartition <USER> <GP> <MODE> - does hardware partitioning\n"
1113 " arguments (sizes in 512-byte blocks):\n"
1114 " USER - <user> <enh> <start> <cnt> <wrrel> <{on|off}>\n"
1115 " : sets user data area attributes\n"
1116 " GP - <{gp1|gp2|gp3|gp4}> <cnt> <enh> <wrrel> <{on|off}>\n"
1117 " : general purpose partition\n"
1118 " MODE - <{check|set|complete}>\n"
1119 " : mode, complete set partitioning completed\n"
1120 " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
1121 " Power cycling is required to initialize partitions after set to complete.\n"
1123 #ifdef CONFIG_SUPPORT_EMMC_BOOT
1124 "mmc bootbus <dev> <boot_bus_width> <reset_boot_bus_width> <boot_mode>\n"
1125 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
1126 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
1127 " - Change sizes of boot and RPMB partitions of specified device\n"
1128 "mmc partconf <dev> [[varname] | [<boot_ack> <boot_partition> <partition_access>]]\n"
1129 " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
1130 " If showing the bits, optionally store the boot_partition field into varname\n"
1131 "mmc rst-function <dev> <value>\n"
1132 " - Change the RST_n_FUNCTION field of the specified device\n"
1133 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
1135 #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
1136 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
1137 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
1138 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
1139 "mmc rpmb counter - read the value of the write counter\n"
1141 "mmc setdsr <value> - set DSR register value\n"
1142 #ifdef CONFIG_CMD_BKOPS_ENABLE
1143 "mmc bkops-enable <dev> - enable background operations handshake on device\n"
1144 " WARNING: This is a write-once setting.\n"
1148 /* Old command kept for compatibility. Same as 'mmc info' */
1150 mmcinfo, 1, 0, do_mmcinfo,
1152 "- display info of the current MMC device"