1 // SPDX-License-Identifier: GPL-2.0+
4 * Kyle Harris, kharris@nexus-tech.net
11 #include <sparse_format.h>
12 #include <image-sparse.h>
14 static int curr_device = -1;
16 static void print_mmcinfo(struct mmc *mmc)
20 printf("Device: %s\n", mmc->cfg->name);
21 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
22 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
23 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
24 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
25 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
27 printf("Bus Speed: %d\n", mmc->clock);
28 #if CONFIG_IS_ENABLED(MMC_VERBOSE)
29 printf("Mode: %s\n", mmc_mode_name(mmc->selected_mode));
30 mmc_dump_capabilities("card capabilities", mmc->card_caps);
31 mmc_dump_capabilities("host capabilities", mmc->host_caps);
33 printf("Rd Block Len: %d\n", mmc->read_bl_len);
35 printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
36 EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
37 EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
38 if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
39 printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
42 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
44 print_size(mmc->capacity, "\n");
46 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
47 mmc->ddr_mode ? " DDR" : "");
49 #if CONFIG_IS_ENABLED(MMC_WRITE)
50 puts("Erase Group Size: ");
51 print_size(((u64)mmc->erase_grp_size) << 9, "\n");
54 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
55 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
56 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
58 #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
59 puts("HC WP Group Size: ");
60 print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
63 puts("User Capacity: ");
64 print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
65 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
70 puts("User Enhanced Start: ");
71 print_size(mmc->enh_user_start, "\n");
72 puts("User Enhanced Size: ");
73 print_size(mmc->enh_user_size, "\n");
75 puts("Boot Capacity: ");
76 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
77 puts("RPMB Capacity: ");
78 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
80 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
81 bool is_enh = has_enh &&
82 (mmc->part_attr & EXT_CSD_ENH_GP(i));
83 if (mmc->capacity_gp[i]) {
84 printf("GP%i Capacity: ", i+1);
85 print_size(mmc->capacity_gp[i],
86 is_enh ? " ENH" : "");
87 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
95 static struct mmc *init_mmc_device(int dev, bool force_init)
98 mmc = find_mmc_device(dev);
100 printf("no mmc device at slot %x\n", dev);
112 #ifdef CONFIG_BLOCK_CACHE
113 struct blk_desc *bd = mmc_get_blk_desc(mmc);
114 blkcache_invalidate(bd->if_type, bd->devnum);
119 static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
123 if (curr_device < 0) {
124 if (get_mmc_num() > 0)
127 puts("No MMC device available\n");
132 mmc = init_mmc_device(curr_device, false);
134 return CMD_RET_FAILURE;
137 return CMD_RET_SUCCESS;
140 #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
141 static int confirm_key_prog(void)
143 puts("Warning: Programming authentication key can be done only once !\n"
144 " Use this command only if you are sure of what you are doing,\n"
145 "Really perform the key programming? <y/N> ");
149 puts("Authentication key programming aborted\n");
152 static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag,
153 int argc, char * const argv[])
156 struct mmc *mmc = find_mmc_device(curr_device);
159 return CMD_RET_USAGE;
161 key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
162 if (!confirm_key_prog())
163 return CMD_RET_FAILURE;
164 if (mmc_rpmb_set_key(mmc, key_addr)) {
165 printf("ERROR - Key already programmed ?\n");
166 return CMD_RET_FAILURE;
168 return CMD_RET_SUCCESS;
170 static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag,
171 int argc, char * const argv[])
176 void *key_addr = NULL;
177 struct mmc *mmc = find_mmc_device(curr_device);
180 return CMD_RET_USAGE;
182 addr = (void *)simple_strtoul(argv[1], NULL, 16);
183 blk = simple_strtoul(argv[2], NULL, 16);
184 cnt = simple_strtoul(argv[3], NULL, 16);
187 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
189 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
190 curr_device, blk, cnt);
191 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
193 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
195 return CMD_RET_FAILURE;
196 return CMD_RET_SUCCESS;
198 static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag,
199 int argc, char * const argv[])
205 struct mmc *mmc = find_mmc_device(curr_device);
208 return CMD_RET_USAGE;
210 addr = (void *)simple_strtoul(argv[1], NULL, 16);
211 blk = simple_strtoul(argv[2], NULL, 16);
212 cnt = simple_strtoul(argv[3], NULL, 16);
213 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
215 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
216 curr_device, blk, cnt);
217 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
219 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
221 return CMD_RET_FAILURE;
222 return CMD_RET_SUCCESS;
224 static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag,
225 int argc, char * const argv[])
227 unsigned long counter;
228 struct mmc *mmc = find_mmc_device(curr_device);
230 if (mmc_rpmb_get_counter(mmc, &counter))
231 return CMD_RET_FAILURE;
232 printf("RPMB Write counter= %lx\n", counter);
233 return CMD_RET_SUCCESS;
236 static cmd_tbl_t cmd_rpmb[] = {
237 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
238 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
239 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
240 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
243 static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag,
244 int argc, char * const argv[])
251 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
253 /* Drop the rpmb subcommand */
257 if (cp == NULL || argc > cp->maxargs)
258 return CMD_RET_USAGE;
259 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
260 return CMD_RET_SUCCESS;
262 mmc = init_mmc_device(curr_device, false);
264 return CMD_RET_FAILURE;
266 if (!(mmc->version & MMC_VERSION_MMC)) {
267 printf("It is not a EMMC device\n");
268 return CMD_RET_FAILURE;
270 if (mmc->version < MMC_VERSION_4_41) {
271 printf("RPMB not supported before version 4.41\n");
272 return CMD_RET_FAILURE;
274 /* Switch to the RPMB partition */
276 original_part = mmc->block_dev.hwpart;
278 original_part = mmc_get_blk_desc(mmc)->hwpart;
280 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, MMC_PART_RPMB) !=
282 return CMD_RET_FAILURE;
283 ret = cp->cmd(cmdtp, flag, argc, argv);
285 /* Return to original partition */
286 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, original_part) !=
288 return CMD_RET_FAILURE;
293 static int do_mmc_read(cmd_tbl_t *cmdtp, int flag,
294 int argc, char * const argv[])
301 return CMD_RET_USAGE;
303 addr = (void *)simple_strtoul(argv[1], NULL, 16);
304 blk = simple_strtoul(argv[2], NULL, 16);
305 cnt = simple_strtoul(argv[3], NULL, 16);
307 mmc = init_mmc_device(curr_device, false);
309 return CMD_RET_FAILURE;
311 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
312 curr_device, blk, cnt);
314 n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
315 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
317 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
320 #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
321 static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk,
322 lbaint_t blkcnt, const void *buffer)
324 struct blk_desc *dev_desc = info->priv;
326 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
329 static lbaint_t mmc_sparse_reserve(struct sparse_storage *info,
330 lbaint_t blk, lbaint_t blkcnt)
335 static int do_mmc_sparse_write(cmd_tbl_t *cmdtp, int flag,
336 int argc, char * const argv[])
338 struct sparse_storage sparse;
339 struct blk_desc *dev_desc;
346 return CMD_RET_USAGE;
348 addr = (void *)simple_strtoul(argv[1], NULL, 16);
349 blk = simple_strtoul(argv[2], NULL, 16);
351 if (!is_sparse_image(addr)) {
352 printf("Not a sparse image\n");
353 return CMD_RET_FAILURE;
356 mmc = init_mmc_device(curr_device, false);
358 return CMD_RET_FAILURE;
360 printf("\nMMC Sparse write: dev # %d, block # %d ... ",
363 if (mmc_getwp(mmc) == 1) {
364 printf("Error: card is write protected!\n");
365 return CMD_RET_FAILURE;
368 dev_desc = mmc_get_blk_desc(mmc);
369 sparse.priv = dev_desc;
372 sparse.size = dev_desc->lba - blk;
373 sparse.write = mmc_sparse_write;
374 sparse.reserve = mmc_sparse_reserve;
376 sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz);
378 if (write_sparse_image(&sparse, dest, addr, NULL))
379 return CMD_RET_FAILURE;
381 return CMD_RET_SUCCESS;
385 #if CONFIG_IS_ENABLED(MMC_WRITE)
386 static int do_mmc_write(cmd_tbl_t *cmdtp, int flag,
387 int argc, char * const argv[])
394 return CMD_RET_USAGE;
396 addr = (void *)simple_strtoul(argv[1], NULL, 16);
397 blk = simple_strtoul(argv[2], NULL, 16);
398 cnt = simple_strtoul(argv[3], NULL, 16);
400 mmc = init_mmc_device(curr_device, false);
402 return CMD_RET_FAILURE;
404 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
405 curr_device, blk, cnt);
407 if (mmc_getwp(mmc) == 1) {
408 printf("Error: card is write protected!\n");
409 return CMD_RET_FAILURE;
411 n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
412 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
414 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
416 static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,
417 int argc, char * const argv[])
423 return CMD_RET_USAGE;
425 blk = simple_strtoul(argv[1], NULL, 16);
426 cnt = simple_strtoul(argv[2], NULL, 16);
428 mmc = init_mmc_device(curr_device, false);
430 return CMD_RET_FAILURE;
432 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
433 curr_device, blk, cnt);
435 if (mmc_getwp(mmc) == 1) {
436 printf("Error: card is write protected!\n");
437 return CMD_RET_FAILURE;
439 n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
440 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
442 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
446 static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
447 int argc, char * const argv[])
451 mmc = init_mmc_device(curr_device, true);
453 return CMD_RET_FAILURE;
455 return CMD_RET_SUCCESS;
457 static int do_mmc_part(cmd_tbl_t *cmdtp, int flag,
458 int argc, char * const argv[])
460 struct blk_desc *mmc_dev;
463 mmc = init_mmc_device(curr_device, false);
465 return CMD_RET_FAILURE;
467 mmc_dev = blk_get_devnum_by_type(IF_TYPE_MMC, curr_device);
468 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
470 return CMD_RET_SUCCESS;
473 puts("get mmc type error!\n");
474 return CMD_RET_FAILURE;
476 static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag,
477 int argc, char * const argv[])
479 int dev, part = 0, ret;
484 } else if (argc == 2) {
485 dev = simple_strtoul(argv[1], NULL, 10);
486 } else if (argc == 3) {
487 dev = (int)simple_strtoul(argv[1], NULL, 10);
488 part = (int)simple_strtoul(argv[2], NULL, 10);
489 if (part > PART_ACCESS_MASK) {
490 printf("#part_num shouldn't be larger than %d\n",
492 return CMD_RET_FAILURE;
495 return CMD_RET_USAGE;
498 mmc = init_mmc_device(dev, true);
500 return CMD_RET_FAILURE;
502 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
503 printf("switch to partitions #%d, %s\n",
504 part, (!ret) ? "OK" : "ERROR");
509 if (mmc->part_config == MMCPART_NOAVAILABLE)
510 printf("mmc%d is current device\n", curr_device);
512 printf("mmc%d(part %d) is current device\n",
513 curr_device, mmc_get_blk_desc(mmc)->hwpart);
515 return CMD_RET_SUCCESS;
517 static int do_mmc_list(cmd_tbl_t *cmdtp, int flag,
518 int argc, char * const argv[])
520 print_mmc_devices('\n');
521 return CMD_RET_SUCCESS;
524 #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
525 static int parse_hwpart_user(struct mmc_hwpart_conf *pconf,
526 int argc, char * const argv[])
530 memset(&pconf->user, 0, sizeof(pconf->user));
533 if (!strcmp(argv[i], "enh")) {
536 pconf->user.enh_start =
537 simple_strtoul(argv[i+1], NULL, 10);
538 pconf->user.enh_size =
539 simple_strtoul(argv[i+2], NULL, 10);
541 } else if (!strcmp(argv[i], "wrrel")) {
544 pconf->user.wr_rel_change = 1;
545 if (!strcmp(argv[i+1], "on"))
546 pconf->user.wr_rel_set = 1;
547 else if (!strcmp(argv[i+1], "off"))
548 pconf->user.wr_rel_set = 0;
559 static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
560 int argc, char * const argv[])
564 memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
568 pconf->gp_part[pidx].size = simple_strtoul(argv[0], NULL, 10);
572 if (!strcmp(argv[i], "enh")) {
573 pconf->gp_part[pidx].enhanced = 1;
575 } else if (!strcmp(argv[i], "wrrel")) {
578 pconf->gp_part[pidx].wr_rel_change = 1;
579 if (!strcmp(argv[i+1], "on"))
580 pconf->gp_part[pidx].wr_rel_set = 1;
581 else if (!strcmp(argv[i+1], "off"))
582 pconf->gp_part[pidx].wr_rel_set = 0;
593 static int do_mmc_hwpartition(cmd_tbl_t *cmdtp, int flag,
594 int argc, char * const argv[])
597 struct mmc_hwpart_conf pconf = { };
598 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
601 mmc = init_mmc_device(curr_device, false);
603 return CMD_RET_FAILURE;
606 return CMD_RET_USAGE;
609 if (!strcmp(argv[i], "user")) {
611 r = parse_hwpart_user(&pconf, argc-i, &argv[i]);
613 return CMD_RET_USAGE;
615 } else if (!strncmp(argv[i], "gp", 2) &&
616 strlen(argv[i]) == 3 &&
617 argv[i][2] >= '1' && argv[i][2] <= '4') {
618 pidx = argv[i][2] - '1';
620 r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
622 return CMD_RET_USAGE;
624 } else if (!strcmp(argv[i], "check")) {
625 mode = MMC_HWPART_CONF_CHECK;
627 } else if (!strcmp(argv[i], "set")) {
628 mode = MMC_HWPART_CONF_SET;
630 } else if (!strcmp(argv[i], "complete")) {
631 mode = MMC_HWPART_CONF_COMPLETE;
634 return CMD_RET_USAGE;
638 puts("Partition configuration:\n");
639 if (pconf.user.enh_size) {
640 puts("\tUser Enhanced Start: ");
641 print_size(((u64)pconf.user.enh_start) << 9, "\n");
642 puts("\tUser Enhanced Size: ");
643 print_size(((u64)pconf.user.enh_size) << 9, "\n");
645 puts("\tNo enhanced user data area\n");
647 if (pconf.user.wr_rel_change)
648 printf("\tUser partition write reliability: %s\n",
649 pconf.user.wr_rel_set ? "on" : "off");
650 for (pidx = 0; pidx < 4; pidx++) {
651 if (pconf.gp_part[pidx].size) {
652 printf("\tGP%i Capacity: ", pidx+1);
653 print_size(((u64)pconf.gp_part[pidx].size) << 9,
654 pconf.gp_part[pidx].enhanced ?
657 printf("\tNo GP%i partition\n", pidx+1);
659 if (pconf.gp_part[pidx].wr_rel_change)
660 printf("\tGP%i write reliability: %s\n", pidx+1,
661 pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
664 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
665 if (mode == MMC_HWPART_CONF_COMPLETE)
666 puts("Partitioning successful, "
667 "power-cycle to make effective\n");
668 return CMD_RET_SUCCESS;
671 return CMD_RET_FAILURE;
676 #ifdef CONFIG_SUPPORT_EMMC_BOOT
677 static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag,
678 int argc, char * const argv[])
682 u8 width, reset, mode;
685 return CMD_RET_USAGE;
686 dev = simple_strtoul(argv[1], NULL, 10);
687 width = simple_strtoul(argv[2], NULL, 10);
688 reset = simple_strtoul(argv[3], NULL, 10);
689 mode = simple_strtoul(argv[4], NULL, 10);
691 mmc = init_mmc_device(dev, false);
693 return CMD_RET_FAILURE;
696 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
697 return CMD_RET_FAILURE;
700 /* acknowledge to be sent during boot operation */
701 return mmc_set_boot_bus_width(mmc, width, reset, mode);
703 static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag,
704 int argc, char * const argv[])
708 u32 bootsize, rpmbsize;
711 return CMD_RET_USAGE;
712 dev = simple_strtoul(argv[1], NULL, 10);
713 bootsize = simple_strtoul(argv[2], NULL, 10);
714 rpmbsize = simple_strtoul(argv[3], NULL, 10);
716 mmc = init_mmc_device(dev, false);
718 return CMD_RET_FAILURE;
721 printf("It is not a EMMC device\n");
722 return CMD_RET_FAILURE;
725 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
726 printf("EMMC boot partition Size change Failed.\n");
727 return CMD_RET_FAILURE;
730 printf("EMMC boot partition Size %d MB\n", bootsize);
731 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
732 return CMD_RET_SUCCESS;
735 static int mmc_partconf_print(struct mmc *mmc)
737 u8 ack, access, part;
739 if (mmc->part_config == MMCPART_NOAVAILABLE) {
740 printf("No part_config info for ver. 0x%x\n", mmc->version);
741 return CMD_RET_FAILURE;
744 access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
745 ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
746 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
748 printf("EXT_CSD[179], PARTITION_CONFIG:\n"
750 "BOOT_PARTITION_ENABLE: 0x%x\n"
751 "PARTITION_ACCESS: 0x%x\n", ack, part, access);
753 return CMD_RET_SUCCESS;
756 static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag,
757 int argc, char * const argv[])
761 u8 ack, part_num, access;
763 if (argc != 2 && argc != 5)
764 return CMD_RET_USAGE;
766 dev = simple_strtoul(argv[1], NULL, 10);
768 mmc = init_mmc_device(dev, false);
770 return CMD_RET_FAILURE;
773 puts("PARTITION_CONFIG only exists on eMMC\n");
774 return CMD_RET_FAILURE;
778 return mmc_partconf_print(mmc);
780 ack = simple_strtoul(argv[2], NULL, 10);
781 part_num = simple_strtoul(argv[3], NULL, 10);
782 access = simple_strtoul(argv[4], NULL, 10);
784 /* acknowledge to be sent during boot operation */
785 return mmc_set_part_conf(mmc, ack, part_num, access);
787 static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag,
788 int argc, char * const argv[])
795 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
796 * The only valid values are 0x0, 0x1 and 0x2 and writing
797 * a value of 0x1 or 0x2 sets the value permanently.
800 return CMD_RET_USAGE;
802 dev = simple_strtoul(argv[1], NULL, 10);
803 enable = simple_strtoul(argv[2], NULL, 10);
806 puts("Invalid RST_n_ENABLE value\n");
807 return CMD_RET_USAGE;
810 mmc = init_mmc_device(dev, false);
812 return CMD_RET_FAILURE;
815 puts("RST_n_FUNCTION only exists on eMMC\n");
816 return CMD_RET_FAILURE;
819 return mmc_set_rst_n_function(mmc, enable);
822 static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag,
823 int argc, char * const argv[])
830 return CMD_RET_USAGE;
831 val = simple_strtoul(argv[1], NULL, 16);
833 mmc = find_mmc_device(curr_device);
835 printf("no mmc device at slot %x\n", curr_device);
836 return CMD_RET_FAILURE;
838 ret = mmc_set_dsr(mmc, val);
839 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
843 return CMD_RET_FAILURE;
845 return CMD_RET_SUCCESS;
850 #ifdef CONFIG_CMD_BKOPS_ENABLE
851 static int do_mmc_bkops_enable(cmd_tbl_t *cmdtp, int flag,
852 int argc, char * const argv[])
858 return CMD_RET_USAGE;
860 dev = simple_strtoul(argv[1], NULL, 10);
862 mmc = init_mmc_device(dev, false);
864 return CMD_RET_FAILURE;
867 puts("BKOPS_EN only exists on eMMC\n");
868 return CMD_RET_FAILURE;
871 return mmc_set_bkops_enable(mmc);
875 static cmd_tbl_t cmd_mmc[] = {
876 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
877 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
878 #if CONFIG_IS_ENABLED(MMC_WRITE)
879 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
880 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
882 #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
883 U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
885 U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
886 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
887 U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
888 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
889 #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
890 U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
892 #ifdef CONFIG_SUPPORT_EMMC_BOOT
893 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
894 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
895 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
896 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
898 #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
899 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
901 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
902 #ifdef CONFIG_CMD_BKOPS_ENABLE
903 U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
907 static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
911 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
913 /* Drop the mmc command */
917 if (cp == NULL || argc > cp->maxargs)
918 return CMD_RET_USAGE;
919 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
920 return CMD_RET_SUCCESS;
922 if (curr_device < 0) {
923 if (get_mmc_num() > 0) {
926 puts("No MMC device available\n");
927 return CMD_RET_FAILURE;
930 return cp->cmd(cmdtp, flag, argc, argv);
934 mmc, 29, 1, do_mmcops,
936 "info - display info of the current MMC device\n"
937 "mmc read addr blk# cnt\n"
938 "mmc write addr blk# cnt\n"
939 #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
940 "mmc swrite addr blk#\n"
942 "mmc erase blk# cnt\n"
944 "mmc part - lists available partition on current mmc device\n"
945 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
946 "mmc list - lists available devices\n"
947 #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
948 "mmc hwpartition [args...] - does hardware partitioning\n"
949 " arguments (sizes in 512-byte blocks):\n"
950 " [user [enh start cnt] [wrrel {on|off}]] - sets user data area attributes\n"
951 " [gp1|gp2|gp3|gp4 cnt [enh] [wrrel {on|off}]] - general purpose partition\n"
952 " [check|set|complete] - mode, complete set partitioning completed\n"
953 " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
954 " Power cycling is required to initialize partitions after set to complete.\n"
956 #ifdef CONFIG_SUPPORT_EMMC_BOOT
957 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
958 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
959 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
960 " - Change sizes of boot and RPMB partitions of specified device\n"
961 "mmc partconf dev [boot_ack boot_partition partition_access]\n"
962 " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
963 "mmc rst-function dev value\n"
964 " - Change the RST_n_FUNCTION field of the specified device\n"
965 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
967 #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
968 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
969 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
970 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
971 "mmc rpmb counter - read the value of the write counter\n"
973 "mmc setdsr <value> - set DSR register value\n"
974 #ifdef CONFIG_CMD_BKOPS_ENABLE
975 "mmc bkops-enable <dev> - enable background operations handshake on device\n"
976 " WARNING: This is a write-once setting.\n"
980 /* Old command kept for compatibility. Same as 'mmc info' */
982 mmcinfo, 1, 0, do_mmcinfo,
984 "- display info of the current MMC device"