xref: /openbmc/u-boot/cmd/mmc.c (revision bdb60996)
12e192b24SSimon Glass /*
22e192b24SSimon Glass  * (C) Copyright 2003
32e192b24SSimon Glass  * Kyle Harris, kharris@nexus-tech.net
42e192b24SSimon Glass  *
52e192b24SSimon Glass  * SPDX-License-Identifier:	GPL-2.0+
62e192b24SSimon Glass  */
72e192b24SSimon Glass 
82e192b24SSimon Glass #include <common.h>
92e192b24SSimon Glass #include <command.h>
102e192b24SSimon Glass #include <console.h>
112e192b24SSimon Glass #include <mmc.h>
122e192b24SSimon Glass 
132e192b24SSimon Glass static int curr_device = -1;
142e192b24SSimon Glass 
152e192b24SSimon Glass static void print_mmcinfo(struct mmc *mmc)
162e192b24SSimon Glass {
172e192b24SSimon Glass 	int i;
182e192b24SSimon Glass 
192e192b24SSimon Glass 	printf("Device: %s\n", mmc->cfg->name);
202e192b24SSimon Glass 	printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
212e192b24SSimon Glass 	printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
222e192b24SSimon Glass 	printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
232e192b24SSimon Glass 			(mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
242e192b24SSimon Glass 			(mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
252e192b24SSimon Glass 
262e192b24SSimon Glass 	printf("Tran Speed: %d\n", mmc->tran_speed);
272e192b24SSimon Glass 	printf("Rd Block Len: %d\n", mmc->read_bl_len);
282e192b24SSimon Glass 
292e192b24SSimon Glass 	printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
302e192b24SSimon Glass 			EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
312e192b24SSimon Glass 			EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
322e192b24SSimon Glass 	if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
332e192b24SSimon Glass 		printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
342e192b24SSimon Glass 	printf("\n");
352e192b24SSimon Glass 
362e192b24SSimon Glass 	printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
372e192b24SSimon Glass 	puts("Capacity: ");
382e192b24SSimon Glass 	print_size(mmc->capacity, "\n");
392e192b24SSimon Glass 
402e192b24SSimon Glass 	printf("Bus Width: %d-bit%s\n", mmc->bus_width,
412e192b24SSimon Glass 			mmc->ddr_mode ? " DDR" : "");
422e192b24SSimon Glass 
432e192b24SSimon Glass 	puts("Erase Group Size: ");
442e192b24SSimon Glass 	print_size(((u64)mmc->erase_grp_size) << 9, "\n");
452e192b24SSimon Glass 
462e192b24SSimon Glass 	if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
472e192b24SSimon Glass 		bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
482e192b24SSimon Glass 		bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
492e192b24SSimon Glass 
502e192b24SSimon Glass 		puts("HC WP Group Size: ");
512e192b24SSimon Glass 		print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
522e192b24SSimon Glass 
532e192b24SSimon Glass 		puts("User Capacity: ");
542e192b24SSimon Glass 		print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
552e192b24SSimon Glass 		if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
562e192b24SSimon Glass 			puts(" WRREL\n");
572e192b24SSimon Glass 		else
582e192b24SSimon Glass 			putc('\n');
592e192b24SSimon Glass 		if (usr_enh) {
602e192b24SSimon Glass 			puts("User Enhanced Start: ");
612e192b24SSimon Glass 			print_size(mmc->enh_user_start, "\n");
622e192b24SSimon Glass 			puts("User Enhanced Size: ");
632e192b24SSimon Glass 			print_size(mmc->enh_user_size, "\n");
642e192b24SSimon Glass 		}
652e192b24SSimon Glass 		puts("Boot Capacity: ");
662e192b24SSimon Glass 		print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
672e192b24SSimon Glass 		puts("RPMB Capacity: ");
682e192b24SSimon Glass 		print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
692e192b24SSimon Glass 
702e192b24SSimon Glass 		for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
712e192b24SSimon Glass 			bool is_enh = has_enh &&
722e192b24SSimon Glass 				(mmc->part_attr & EXT_CSD_ENH_GP(i));
732e192b24SSimon Glass 			if (mmc->capacity_gp[i]) {
742e192b24SSimon Glass 				printf("GP%i Capacity: ", i+1);
752e192b24SSimon Glass 				print_size(mmc->capacity_gp[i],
762e192b24SSimon Glass 					   is_enh ? " ENH" : "");
772e192b24SSimon Glass 				if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
782e192b24SSimon Glass 					puts(" WRREL\n");
792e192b24SSimon Glass 				else
802e192b24SSimon Glass 					putc('\n');
812e192b24SSimon Glass 			}
822e192b24SSimon Glass 		}
832e192b24SSimon Glass 	}
842e192b24SSimon Glass }
852e192b24SSimon Glass static struct mmc *init_mmc_device(int dev, bool force_init)
862e192b24SSimon Glass {
872e192b24SSimon Glass 	struct mmc *mmc;
882e192b24SSimon Glass 	mmc = find_mmc_device(dev);
892e192b24SSimon Glass 	if (!mmc) {
902e192b24SSimon Glass 		printf("no mmc device at slot %x\n", dev);
912e192b24SSimon Glass 		return NULL;
922e192b24SSimon Glass 	}
93bcfde7ffSEric Nelson 
942e192b24SSimon Glass 	if (force_init)
952e192b24SSimon Glass 		mmc->has_init = 0;
962e192b24SSimon Glass 	if (mmc_init(mmc))
972e192b24SSimon Glass 		return NULL;
982e192b24SSimon Glass 	return mmc;
992e192b24SSimon Glass }
1002e192b24SSimon Glass static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1012e192b24SSimon Glass {
1022e192b24SSimon Glass 	struct mmc *mmc;
1032e192b24SSimon Glass 
1042e192b24SSimon Glass 	if (curr_device < 0) {
1052e192b24SSimon Glass 		if (get_mmc_num() > 0)
1062e192b24SSimon Glass 			curr_device = 0;
1072e192b24SSimon Glass 		else {
1082e192b24SSimon Glass 			puts("No MMC device available\n");
1092e192b24SSimon Glass 			return 1;
1102e192b24SSimon Glass 		}
1112e192b24SSimon Glass 	}
1122e192b24SSimon Glass 
1132e192b24SSimon Glass 	mmc = init_mmc_device(curr_device, false);
1142e192b24SSimon Glass 	if (!mmc)
1152e192b24SSimon Glass 		return CMD_RET_FAILURE;
1162e192b24SSimon Glass 
1172e192b24SSimon Glass 	print_mmcinfo(mmc);
1182e192b24SSimon Glass 	return CMD_RET_SUCCESS;
1192e192b24SSimon Glass }
1202e192b24SSimon Glass 
1212e192b24SSimon Glass #ifdef CONFIG_SUPPORT_EMMC_RPMB
1222e192b24SSimon Glass static int confirm_key_prog(void)
1232e192b24SSimon Glass {
1242e192b24SSimon Glass 	puts("Warning: Programming authentication key can be done only once !\n"
1252e192b24SSimon Glass 	     "         Use this command only if you are sure of what you are doing,\n"
1262e192b24SSimon Glass 	     "Really perform the key programming? <y/N> ");
1272e192b24SSimon Glass 	if (confirm_yesno())
1282e192b24SSimon Glass 		return 1;
1292e192b24SSimon Glass 
1302e192b24SSimon Glass 	puts("Authentication key programming aborted\n");
1312e192b24SSimon Glass 	return 0;
1322e192b24SSimon Glass }
1332e192b24SSimon Glass static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag,
1342e192b24SSimon Glass 			  int argc, char * const argv[])
1352e192b24SSimon Glass {
1362e192b24SSimon Glass 	void *key_addr;
1372e192b24SSimon Glass 	struct mmc *mmc = find_mmc_device(curr_device);
1382e192b24SSimon Glass 
1392e192b24SSimon Glass 	if (argc != 2)
1402e192b24SSimon Glass 		return CMD_RET_USAGE;
1412e192b24SSimon Glass 
1422e192b24SSimon Glass 	key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
1432e192b24SSimon Glass 	if (!confirm_key_prog())
1442e192b24SSimon Glass 		return CMD_RET_FAILURE;
1452e192b24SSimon Glass 	if (mmc_rpmb_set_key(mmc, key_addr)) {
1462e192b24SSimon Glass 		printf("ERROR - Key already programmed ?\n");
1472e192b24SSimon Glass 		return CMD_RET_FAILURE;
1482e192b24SSimon Glass 	}
1492e192b24SSimon Glass 	return CMD_RET_SUCCESS;
1502e192b24SSimon Glass }
1512e192b24SSimon Glass static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag,
1522e192b24SSimon Glass 			   int argc, char * const argv[])
1532e192b24SSimon Glass {
1542e192b24SSimon Glass 	u16 blk, cnt;
1552e192b24SSimon Glass 	void *addr;
1562e192b24SSimon Glass 	int n;
1572e192b24SSimon Glass 	void *key_addr = NULL;
1582e192b24SSimon Glass 	struct mmc *mmc = find_mmc_device(curr_device);
1592e192b24SSimon Glass 
1602e192b24SSimon Glass 	if (argc < 4)
1612e192b24SSimon Glass 		return CMD_RET_USAGE;
1622e192b24SSimon Glass 
1632e192b24SSimon Glass 	addr = (void *)simple_strtoul(argv[1], NULL, 16);
1642e192b24SSimon Glass 	blk = simple_strtoul(argv[2], NULL, 16);
1652e192b24SSimon Glass 	cnt = simple_strtoul(argv[3], NULL, 16);
1662e192b24SSimon Glass 
1672e192b24SSimon Glass 	if (argc == 5)
1682e192b24SSimon Glass 		key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
1692e192b24SSimon Glass 
1702e192b24SSimon Glass 	printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
1712e192b24SSimon Glass 	       curr_device, blk, cnt);
1722e192b24SSimon Glass 	n =  mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
1732e192b24SSimon Glass 
1742e192b24SSimon Glass 	printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
1752e192b24SSimon Glass 	if (n != cnt)
1762e192b24SSimon Glass 		return CMD_RET_FAILURE;
1772e192b24SSimon Glass 	return CMD_RET_SUCCESS;
1782e192b24SSimon Glass }
1792e192b24SSimon Glass static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag,
1802e192b24SSimon Glass 			    int argc, char * const argv[])
1812e192b24SSimon Glass {
1822e192b24SSimon Glass 	u16 blk, cnt;
1832e192b24SSimon Glass 	void *addr;
1842e192b24SSimon Glass 	int n;
1852e192b24SSimon Glass 	void *key_addr;
1862e192b24SSimon Glass 	struct mmc *mmc = find_mmc_device(curr_device);
1872e192b24SSimon Glass 
1882e192b24SSimon Glass 	if (argc != 5)
1892e192b24SSimon Glass 		return CMD_RET_USAGE;
1902e192b24SSimon Glass 
1912e192b24SSimon Glass 	addr = (void *)simple_strtoul(argv[1], NULL, 16);
1922e192b24SSimon Glass 	blk = simple_strtoul(argv[2], NULL, 16);
1932e192b24SSimon Glass 	cnt = simple_strtoul(argv[3], NULL, 16);
1942e192b24SSimon Glass 	key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
1952e192b24SSimon Glass 
1962e192b24SSimon Glass 	printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
1972e192b24SSimon Glass 	       curr_device, blk, cnt);
1982e192b24SSimon Glass 	n =  mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
1992e192b24SSimon Glass 
2002e192b24SSimon Glass 	printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
2012e192b24SSimon Glass 	if (n != cnt)
2022e192b24SSimon Glass 		return CMD_RET_FAILURE;
2032e192b24SSimon Glass 	return CMD_RET_SUCCESS;
2042e192b24SSimon Glass }
2052e192b24SSimon Glass static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag,
2062e192b24SSimon Glass 			      int argc, char * const argv[])
2072e192b24SSimon Glass {
2082e192b24SSimon Glass 	unsigned long counter;
2092e192b24SSimon Glass 	struct mmc *mmc = find_mmc_device(curr_device);
2102e192b24SSimon Glass 
2112e192b24SSimon Glass 	if (mmc_rpmb_get_counter(mmc, &counter))
2122e192b24SSimon Glass 		return CMD_RET_FAILURE;
2132e192b24SSimon Glass 	printf("RPMB Write counter= %lx\n", counter);
2142e192b24SSimon Glass 	return CMD_RET_SUCCESS;
2152e192b24SSimon Glass }
2162e192b24SSimon Glass 
2172e192b24SSimon Glass static cmd_tbl_t cmd_rpmb[] = {
2182e192b24SSimon Glass 	U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
2192e192b24SSimon Glass 	U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
2202e192b24SSimon Glass 	U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
2212e192b24SSimon Glass 	U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
2222e192b24SSimon Glass };
2232e192b24SSimon Glass 
2242e192b24SSimon Glass static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag,
2252e192b24SSimon Glass 		      int argc, char * const argv[])
2262e192b24SSimon Glass {
2272e192b24SSimon Glass 	cmd_tbl_t *cp;
2282e192b24SSimon Glass 	struct mmc *mmc;
2292e192b24SSimon Glass 	char original_part;
2302e192b24SSimon Glass 	int ret;
2312e192b24SSimon Glass 
2322e192b24SSimon Glass 	cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
2332e192b24SSimon Glass 
2342e192b24SSimon Glass 	/* Drop the rpmb subcommand */
2352e192b24SSimon Glass 	argc--;
2362e192b24SSimon Glass 	argv++;
2372e192b24SSimon Glass 
2382e192b24SSimon Glass 	if (cp == NULL || argc > cp->maxargs)
2392e192b24SSimon Glass 		return CMD_RET_USAGE;
2402e192b24SSimon Glass 	if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
2412e192b24SSimon Glass 		return CMD_RET_SUCCESS;
2422e192b24SSimon Glass 
2432e192b24SSimon Glass 	mmc = init_mmc_device(curr_device, false);
2442e192b24SSimon Glass 	if (!mmc)
2452e192b24SSimon Glass 		return CMD_RET_FAILURE;
2462e192b24SSimon Glass 
2472e192b24SSimon Glass 	if (!(mmc->version & MMC_VERSION_MMC)) {
2482e192b24SSimon Glass 		printf("It is not a EMMC device\n");
2492e192b24SSimon Glass 		return CMD_RET_FAILURE;
2502e192b24SSimon Glass 	}
2512e192b24SSimon Glass 	if (mmc->version < MMC_VERSION_4_41) {
2522e192b24SSimon Glass 		printf("RPMB not supported before version 4.41\n");
2532e192b24SSimon Glass 		return CMD_RET_FAILURE;
2542e192b24SSimon Glass 	}
2552e192b24SSimon Glass 	/* Switch to the RPMB partition */
2564dc80c87SKever Yang #ifndef CONFIG_BLK
257b955e42bSMarek Vasut 	original_part = mmc->block_dev.hwpart;
2584dc80c87SKever Yang #else
2594dc80c87SKever Yang 	original_part = mmc_get_blk_desc(mmc)->hwpart;
2604dc80c87SKever Yang #endif
26169f45cd5SSimon Glass 	if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, MMC_PART_RPMB) !=
26269f45cd5SSimon Glass 	    0)
2632e192b24SSimon Glass 		return CMD_RET_FAILURE;
2642e192b24SSimon Glass 	ret = cp->cmd(cmdtp, flag, argc, argv);
2652e192b24SSimon Glass 
2662e192b24SSimon Glass 	/* Return to original partition */
26769f45cd5SSimon Glass 	if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, original_part) !=
26869f45cd5SSimon Glass 	    0)
2692e192b24SSimon Glass 		return CMD_RET_FAILURE;
2702e192b24SSimon Glass 	return ret;
2712e192b24SSimon Glass }
2722e192b24SSimon Glass #endif
2732e192b24SSimon Glass 
2742e192b24SSimon Glass static int do_mmc_read(cmd_tbl_t *cmdtp, int flag,
2752e192b24SSimon Glass 		       int argc, char * const argv[])
2762e192b24SSimon Glass {
2772e192b24SSimon Glass 	struct mmc *mmc;
2782e192b24SSimon Glass 	u32 blk, cnt, n;
2792e192b24SSimon Glass 	void *addr;
2802e192b24SSimon Glass 
2812e192b24SSimon Glass 	if (argc != 4)
2822e192b24SSimon Glass 		return CMD_RET_USAGE;
2832e192b24SSimon Glass 
2842e192b24SSimon Glass 	addr = (void *)simple_strtoul(argv[1], NULL, 16);
2852e192b24SSimon Glass 	blk = simple_strtoul(argv[2], NULL, 16);
2862e192b24SSimon Glass 	cnt = simple_strtoul(argv[3], NULL, 16);
2872e192b24SSimon Glass 
2882e192b24SSimon Glass 	mmc = init_mmc_device(curr_device, false);
2892e192b24SSimon Glass 	if (!mmc)
2902e192b24SSimon Glass 		return CMD_RET_FAILURE;
2912e192b24SSimon Glass 
2922e192b24SSimon Glass 	printf("\nMMC read: dev # %d, block # %d, count %d ... ",
2932e192b24SSimon Glass 	       curr_device, blk, cnt);
2942e192b24SSimon Glass 
295c40fdca6SSimon Glass 	n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
2962e192b24SSimon Glass 	/* flush cache after read */
2972e192b24SSimon Glass 	flush_cache((ulong)addr, cnt * 512); /* FIXME */
2982e192b24SSimon Glass 	printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
2992e192b24SSimon Glass 
3002e192b24SSimon Glass 	return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
3012e192b24SSimon Glass }
3022e192b24SSimon Glass static int do_mmc_write(cmd_tbl_t *cmdtp, int flag,
3032e192b24SSimon Glass 			int argc, char * const argv[])
3042e192b24SSimon Glass {
3052e192b24SSimon Glass 	struct mmc *mmc;
3062e192b24SSimon Glass 	u32 blk, cnt, n;
3072e192b24SSimon Glass 	void *addr;
3082e192b24SSimon Glass 
3092e192b24SSimon Glass 	if (argc != 4)
3102e192b24SSimon Glass 		return CMD_RET_USAGE;
3112e192b24SSimon Glass 
3122e192b24SSimon Glass 	addr = (void *)simple_strtoul(argv[1], NULL, 16);
3132e192b24SSimon Glass 	blk = simple_strtoul(argv[2], NULL, 16);
3142e192b24SSimon Glass 	cnt = simple_strtoul(argv[3], NULL, 16);
3152e192b24SSimon Glass 
3162e192b24SSimon Glass 	mmc = init_mmc_device(curr_device, false);
3172e192b24SSimon Glass 	if (!mmc)
3182e192b24SSimon Glass 		return CMD_RET_FAILURE;
3192e192b24SSimon Glass 
3202e192b24SSimon Glass 	printf("\nMMC write: dev # %d, block # %d, count %d ... ",
3212e192b24SSimon Glass 	       curr_device, blk, cnt);
3222e192b24SSimon Glass 
3232e192b24SSimon Glass 	if (mmc_getwp(mmc) == 1) {
3242e192b24SSimon Glass 		printf("Error: card is write protected!\n");
3252e192b24SSimon Glass 		return CMD_RET_FAILURE;
3262e192b24SSimon Glass 	}
327c40fdca6SSimon Glass 	n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
3282e192b24SSimon Glass 	printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
3292e192b24SSimon Glass 
3302e192b24SSimon Glass 	return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
3312e192b24SSimon Glass }
3322e192b24SSimon Glass static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,
3332e192b24SSimon Glass 			int argc, char * const argv[])
3342e192b24SSimon Glass {
3352e192b24SSimon Glass 	struct mmc *mmc;
3362e192b24SSimon Glass 	u32 blk, cnt, n;
3372e192b24SSimon Glass 
3382e192b24SSimon Glass 	if (argc != 3)
3392e192b24SSimon Glass 		return CMD_RET_USAGE;
3402e192b24SSimon Glass 
3412e192b24SSimon Glass 	blk = simple_strtoul(argv[1], NULL, 16);
3422e192b24SSimon Glass 	cnt = simple_strtoul(argv[2], NULL, 16);
3432e192b24SSimon Glass 
3442e192b24SSimon Glass 	mmc = init_mmc_device(curr_device, false);
3452e192b24SSimon Glass 	if (!mmc)
3462e192b24SSimon Glass 		return CMD_RET_FAILURE;
3472e192b24SSimon Glass 
3482e192b24SSimon Glass 	printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
3492e192b24SSimon Glass 	       curr_device, blk, cnt);
3502e192b24SSimon Glass 
3512e192b24SSimon Glass 	if (mmc_getwp(mmc) == 1) {
3522e192b24SSimon Glass 		printf("Error: card is write protected!\n");
3532e192b24SSimon Glass 		return CMD_RET_FAILURE;
3542e192b24SSimon Glass 	}
355c40fdca6SSimon Glass 	n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
3562e192b24SSimon Glass 	printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
3572e192b24SSimon Glass 
3582e192b24SSimon Glass 	return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
3592e192b24SSimon Glass }
3602e192b24SSimon Glass static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
3612e192b24SSimon Glass 			 int argc, char * const argv[])
3622e192b24SSimon Glass {
3632e192b24SSimon Glass 	struct mmc *mmc;
3642e192b24SSimon Glass 
3652e192b24SSimon Glass 	mmc = init_mmc_device(curr_device, true);
3662e192b24SSimon Glass 	if (!mmc)
3672e192b24SSimon Glass 		return CMD_RET_FAILURE;
3682e192b24SSimon Glass 
3692e192b24SSimon Glass 	return CMD_RET_SUCCESS;
3702e192b24SSimon Glass }
3712e192b24SSimon Glass static int do_mmc_part(cmd_tbl_t *cmdtp, int flag,
3722e192b24SSimon Glass 		       int argc, char * const argv[])
3732e192b24SSimon Glass {
3744101f687SSimon Glass 	struct blk_desc *mmc_dev;
3752e192b24SSimon Glass 	struct mmc *mmc;
3762e192b24SSimon Glass 
3772e192b24SSimon Glass 	mmc = init_mmc_device(curr_device, false);
3782e192b24SSimon Glass 	if (!mmc)
3792e192b24SSimon Glass 		return CMD_RET_FAILURE;
3802e192b24SSimon Glass 
3813c457f4dSSimon Glass 	mmc_dev = blk_get_devnum_by_type(IF_TYPE_MMC, curr_device);
3822e192b24SSimon Glass 	if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
3833e8bd469SSimon Glass 		part_print(mmc_dev);
3842e192b24SSimon Glass 		return CMD_RET_SUCCESS;
3852e192b24SSimon Glass 	}
3862e192b24SSimon Glass 
3872e192b24SSimon Glass 	puts("get mmc type error!\n");
3882e192b24SSimon Glass 	return CMD_RET_FAILURE;
3892e192b24SSimon Glass }
3902e192b24SSimon Glass static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag,
3912e192b24SSimon Glass 		      int argc, char * const argv[])
3922e192b24SSimon Glass {
3932e192b24SSimon Glass 	int dev, part = 0, ret;
3942e192b24SSimon Glass 	struct mmc *mmc;
3952e192b24SSimon Glass 
3962e192b24SSimon Glass 	if (argc == 1) {
3972e192b24SSimon Glass 		dev = curr_device;
3982e192b24SSimon Glass 	} else if (argc == 2) {
3992e192b24SSimon Glass 		dev = simple_strtoul(argv[1], NULL, 10);
4002e192b24SSimon Glass 	} else if (argc == 3) {
4012e192b24SSimon Glass 		dev = (int)simple_strtoul(argv[1], NULL, 10);
4022e192b24SSimon Glass 		part = (int)simple_strtoul(argv[2], NULL, 10);
4032e192b24SSimon Glass 		if (part > PART_ACCESS_MASK) {
4042e192b24SSimon Glass 			printf("#part_num shouldn't be larger than %d\n",
4052e192b24SSimon Glass 			       PART_ACCESS_MASK);
4062e192b24SSimon Glass 			return CMD_RET_FAILURE;
4072e192b24SSimon Glass 		}
4082e192b24SSimon Glass 	} else {
4092e192b24SSimon Glass 		return CMD_RET_USAGE;
4102e192b24SSimon Glass 	}
4112e192b24SSimon Glass 
4122e192b24SSimon Glass 	mmc = init_mmc_device(dev, true);
4132e192b24SSimon Glass 	if (!mmc)
4142e192b24SSimon Glass 		return CMD_RET_FAILURE;
4152e192b24SSimon Glass 
41669f45cd5SSimon Glass 	ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
4172e192b24SSimon Glass 	printf("switch to partitions #%d, %s\n",
4182e192b24SSimon Glass 	       part, (!ret) ? "OK" : "ERROR");
4192e192b24SSimon Glass 	if (ret)
4202e192b24SSimon Glass 		return 1;
4212e192b24SSimon Glass 
4222e192b24SSimon Glass 	curr_device = dev;
4232e192b24SSimon Glass 	if (mmc->part_config == MMCPART_NOAVAILABLE)
4242e192b24SSimon Glass 		printf("mmc%d is current device\n", curr_device);
4252e192b24SSimon Glass 	else
4262e192b24SSimon Glass 		printf("mmc%d(part %d) is current device\n",
427c40fdca6SSimon Glass 		       curr_device, mmc_get_blk_desc(mmc)->hwpart);
4282e192b24SSimon Glass 
4292e192b24SSimon Glass 	return CMD_RET_SUCCESS;
4302e192b24SSimon Glass }
4312e192b24SSimon Glass static int do_mmc_list(cmd_tbl_t *cmdtp, int flag,
4322e192b24SSimon Glass 		       int argc, char * const argv[])
4332e192b24SSimon Glass {
4342e192b24SSimon Glass 	print_mmc_devices('\n');
4352e192b24SSimon Glass 	return CMD_RET_SUCCESS;
4362e192b24SSimon Glass }
4372e192b24SSimon Glass 
4382e192b24SSimon Glass static int parse_hwpart_user(struct mmc_hwpart_conf *pconf,
4392e192b24SSimon Glass 			     int argc, char * const argv[])
4402e192b24SSimon Glass {
4412e192b24SSimon Glass 	int i = 0;
4422e192b24SSimon Glass 
4432e192b24SSimon Glass 	memset(&pconf->user, 0, sizeof(pconf->user));
4442e192b24SSimon Glass 
4452e192b24SSimon Glass 	while (i < argc) {
4462e192b24SSimon Glass 		if (!strcmp(argv[i], "enh")) {
4472e192b24SSimon Glass 			if (i + 2 >= argc)
4482e192b24SSimon Glass 				return -1;
4492e192b24SSimon Glass 			pconf->user.enh_start =
4502e192b24SSimon Glass 				simple_strtoul(argv[i+1], NULL, 10);
4512e192b24SSimon Glass 			pconf->user.enh_size =
4522e192b24SSimon Glass 				simple_strtoul(argv[i+2], NULL, 10);
4532e192b24SSimon Glass 			i += 3;
4542e192b24SSimon Glass 		} else if (!strcmp(argv[i], "wrrel")) {
4552e192b24SSimon Glass 			if (i + 1 >= argc)
4562e192b24SSimon Glass 				return -1;
4572e192b24SSimon Glass 			pconf->user.wr_rel_change = 1;
4582e192b24SSimon Glass 			if (!strcmp(argv[i+1], "on"))
4592e192b24SSimon Glass 				pconf->user.wr_rel_set = 1;
4602e192b24SSimon Glass 			else if (!strcmp(argv[i+1], "off"))
4612e192b24SSimon Glass 				pconf->user.wr_rel_set = 0;
4622e192b24SSimon Glass 			else
4632e192b24SSimon Glass 				return -1;
4642e192b24SSimon Glass 			i += 2;
4652e192b24SSimon Glass 		} else {
4662e192b24SSimon Glass 			break;
4672e192b24SSimon Glass 		}
4682e192b24SSimon Glass 	}
4692e192b24SSimon Glass 	return i;
4702e192b24SSimon Glass }
4712e192b24SSimon Glass 
4722e192b24SSimon Glass static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
4732e192b24SSimon Glass 			   int argc, char * const argv[])
4742e192b24SSimon Glass {
4752e192b24SSimon Glass 	int i;
4762e192b24SSimon Glass 
4772e192b24SSimon Glass 	memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
4782e192b24SSimon Glass 
4792e192b24SSimon Glass 	if (1 >= argc)
4802e192b24SSimon Glass 		return -1;
4812e192b24SSimon Glass 	pconf->gp_part[pidx].size = simple_strtoul(argv[0], NULL, 10);
4822e192b24SSimon Glass 
4832e192b24SSimon Glass 	i = 1;
4842e192b24SSimon Glass 	while (i < argc) {
4852e192b24SSimon Glass 		if (!strcmp(argv[i], "enh")) {
4862e192b24SSimon Glass 			pconf->gp_part[pidx].enhanced = 1;
4872e192b24SSimon Glass 			i += 1;
4882e192b24SSimon Glass 		} else if (!strcmp(argv[i], "wrrel")) {
4892e192b24SSimon Glass 			if (i + 1 >= argc)
4902e192b24SSimon Glass 				return -1;
4912e192b24SSimon Glass 			pconf->gp_part[pidx].wr_rel_change = 1;
4922e192b24SSimon Glass 			if (!strcmp(argv[i+1], "on"))
4932e192b24SSimon Glass 				pconf->gp_part[pidx].wr_rel_set = 1;
4942e192b24SSimon Glass 			else if (!strcmp(argv[i+1], "off"))
4952e192b24SSimon Glass 				pconf->gp_part[pidx].wr_rel_set = 0;
4962e192b24SSimon Glass 			else
4972e192b24SSimon Glass 				return -1;
4982e192b24SSimon Glass 			i += 2;
4992e192b24SSimon Glass 		} else {
5002e192b24SSimon Glass 			break;
5012e192b24SSimon Glass 		}
5022e192b24SSimon Glass 	}
5032e192b24SSimon Glass 	return i;
5042e192b24SSimon Glass }
5052e192b24SSimon Glass 
5062e192b24SSimon Glass static int do_mmc_hwpartition(cmd_tbl_t *cmdtp, int flag,
5072e192b24SSimon Glass 			      int argc, char * const argv[])
5082e192b24SSimon Glass {
5092e192b24SSimon Glass 	struct mmc *mmc;
5102e192b24SSimon Glass 	struct mmc_hwpart_conf pconf = { };
5112e192b24SSimon Glass 	enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
5122e192b24SSimon Glass 	int i, r, pidx;
5132e192b24SSimon Glass 
5142e192b24SSimon Glass 	mmc = init_mmc_device(curr_device, false);
5152e192b24SSimon Glass 	if (!mmc)
5162e192b24SSimon Glass 		return CMD_RET_FAILURE;
5172e192b24SSimon Glass 
5182e192b24SSimon Glass 	if (argc < 1)
5192e192b24SSimon Glass 		return CMD_RET_USAGE;
5202e192b24SSimon Glass 	i = 1;
5212e192b24SSimon Glass 	while (i < argc) {
5222e192b24SSimon Glass 		if (!strcmp(argv[i], "user")) {
5232e192b24SSimon Glass 			i++;
5242e192b24SSimon Glass 			r = parse_hwpart_user(&pconf, argc-i, &argv[i]);
5252e192b24SSimon Glass 			if (r < 0)
5262e192b24SSimon Glass 				return CMD_RET_USAGE;
5272e192b24SSimon Glass 			i += r;
5282e192b24SSimon Glass 		} else if (!strncmp(argv[i], "gp", 2) &&
5292e192b24SSimon Glass 			   strlen(argv[i]) == 3 &&
5302e192b24SSimon Glass 			   argv[i][2] >= '1' && argv[i][2] <= '4') {
5312e192b24SSimon Glass 			pidx = argv[i][2] - '1';
5322e192b24SSimon Glass 			i++;
5332e192b24SSimon Glass 			r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
5342e192b24SSimon Glass 			if (r < 0)
5352e192b24SSimon Glass 				return CMD_RET_USAGE;
5362e192b24SSimon Glass 			i += r;
5372e192b24SSimon Glass 		} else if (!strcmp(argv[i], "check")) {
5382e192b24SSimon Glass 			mode = MMC_HWPART_CONF_CHECK;
5392e192b24SSimon Glass 			i++;
5402e192b24SSimon Glass 		} else if (!strcmp(argv[i], "set")) {
5412e192b24SSimon Glass 			mode = MMC_HWPART_CONF_SET;
5422e192b24SSimon Glass 			i++;
5432e192b24SSimon Glass 		} else if (!strcmp(argv[i], "complete")) {
5442e192b24SSimon Glass 			mode = MMC_HWPART_CONF_COMPLETE;
5452e192b24SSimon Glass 			i++;
5462e192b24SSimon Glass 		} else {
5472e192b24SSimon Glass 			return CMD_RET_USAGE;
5482e192b24SSimon Glass 		}
5492e192b24SSimon Glass 	}
5502e192b24SSimon Glass 
5512e192b24SSimon Glass 	puts("Partition configuration:\n");
5522e192b24SSimon Glass 	if (pconf.user.enh_size) {
5532e192b24SSimon Glass 		puts("\tUser Enhanced Start: ");
5542e192b24SSimon Glass 		print_size(((u64)pconf.user.enh_start) << 9, "\n");
5552e192b24SSimon Glass 		puts("\tUser Enhanced Size: ");
5562e192b24SSimon Glass 		print_size(((u64)pconf.user.enh_size) << 9, "\n");
5572e192b24SSimon Glass 	} else {
5582e192b24SSimon Glass 		puts("\tNo enhanced user data area\n");
5592e192b24SSimon Glass 	}
5602e192b24SSimon Glass 	if (pconf.user.wr_rel_change)
5612e192b24SSimon Glass 		printf("\tUser partition write reliability: %s\n",
5622e192b24SSimon Glass 		       pconf.user.wr_rel_set ? "on" : "off");
5632e192b24SSimon Glass 	for (pidx = 0; pidx < 4; pidx++) {
5642e192b24SSimon Glass 		if (pconf.gp_part[pidx].size) {
5652e192b24SSimon Glass 			printf("\tGP%i Capacity: ", pidx+1);
5662e192b24SSimon Glass 			print_size(((u64)pconf.gp_part[pidx].size) << 9,
5672e192b24SSimon Glass 				   pconf.gp_part[pidx].enhanced ?
5682e192b24SSimon Glass 				   " ENH\n" : "\n");
5692e192b24SSimon Glass 		} else {
5702e192b24SSimon Glass 			printf("\tNo GP%i partition\n", pidx+1);
5712e192b24SSimon Glass 		}
5722e192b24SSimon Glass 		if (pconf.gp_part[pidx].wr_rel_change)
5732e192b24SSimon Glass 			printf("\tGP%i write reliability: %s\n", pidx+1,
5742e192b24SSimon Glass 			       pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
5752e192b24SSimon Glass 	}
5762e192b24SSimon Glass 
5772e192b24SSimon Glass 	if (!mmc_hwpart_config(mmc, &pconf, mode)) {
5782e192b24SSimon Glass 		if (mode == MMC_HWPART_CONF_COMPLETE)
5792e192b24SSimon Glass 			puts("Partitioning successful, "
5802e192b24SSimon Glass 			     "power-cycle to make effective\n");
5812e192b24SSimon Glass 		return CMD_RET_SUCCESS;
5822e192b24SSimon Glass 	} else {
5832e192b24SSimon Glass 		puts("Failed!\n");
5842e192b24SSimon Glass 		return CMD_RET_FAILURE;
5852e192b24SSimon Glass 	}
5862e192b24SSimon Glass }
5872e192b24SSimon Glass 
5882e192b24SSimon Glass #ifdef CONFIG_SUPPORT_EMMC_BOOT
5892e192b24SSimon Glass static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag,
5902e192b24SSimon Glass 			  int argc, char * const argv[])
5912e192b24SSimon Glass {
5922e192b24SSimon Glass 	int dev;
5932e192b24SSimon Glass 	struct mmc *mmc;
5942e192b24SSimon Glass 	u8 width, reset, mode;
5952e192b24SSimon Glass 
5962e192b24SSimon Glass 	if (argc != 5)
5972e192b24SSimon Glass 		return CMD_RET_USAGE;
5982e192b24SSimon Glass 	dev = simple_strtoul(argv[1], NULL, 10);
5992e192b24SSimon Glass 	width = simple_strtoul(argv[2], NULL, 10);
6002e192b24SSimon Glass 	reset = simple_strtoul(argv[3], NULL, 10);
6012e192b24SSimon Glass 	mode = simple_strtoul(argv[4], NULL, 10);
6022e192b24SSimon Glass 
6032e192b24SSimon Glass 	mmc = init_mmc_device(dev, false);
6042e192b24SSimon Glass 	if (!mmc)
6052e192b24SSimon Glass 		return CMD_RET_FAILURE;
6062e192b24SSimon Glass 
6072e192b24SSimon Glass 	if (IS_SD(mmc)) {
6082e192b24SSimon Glass 		puts("BOOT_BUS_WIDTH only exists on eMMC\n");
6092e192b24SSimon Glass 		return CMD_RET_FAILURE;
6102e192b24SSimon Glass 	}
6112e192b24SSimon Glass 
6122e192b24SSimon Glass 	/* acknowledge to be sent during boot operation */
6132e192b24SSimon Glass 	return mmc_set_boot_bus_width(mmc, width, reset, mode);
6142e192b24SSimon Glass }
6152e192b24SSimon Glass static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag,
6162e192b24SSimon Glass 			      int argc, char * const argv[])
6172e192b24SSimon Glass {
6182e192b24SSimon Glass 	int dev;
6192e192b24SSimon Glass 	struct mmc *mmc;
6202e192b24SSimon Glass 	u32 bootsize, rpmbsize;
6212e192b24SSimon Glass 
6222e192b24SSimon Glass 	if (argc != 4)
6232e192b24SSimon Glass 		return CMD_RET_USAGE;
6242e192b24SSimon Glass 	dev = simple_strtoul(argv[1], NULL, 10);
6252e192b24SSimon Glass 	bootsize = simple_strtoul(argv[2], NULL, 10);
6262e192b24SSimon Glass 	rpmbsize = simple_strtoul(argv[3], NULL, 10);
6272e192b24SSimon Glass 
6282e192b24SSimon Glass 	mmc = init_mmc_device(dev, false);
6292e192b24SSimon Glass 	if (!mmc)
6302e192b24SSimon Glass 		return CMD_RET_FAILURE;
6312e192b24SSimon Glass 
6322e192b24SSimon Glass 	if (IS_SD(mmc)) {
6332e192b24SSimon Glass 		printf("It is not a EMMC device\n");
6342e192b24SSimon Glass 		return CMD_RET_FAILURE;
6352e192b24SSimon Glass 	}
6362e192b24SSimon Glass 
6372e192b24SSimon Glass 	if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
6382e192b24SSimon Glass 		printf("EMMC boot partition Size change Failed.\n");
6392e192b24SSimon Glass 		return CMD_RET_FAILURE;
6402e192b24SSimon Glass 	}
6412e192b24SSimon Glass 
6422e192b24SSimon Glass 	printf("EMMC boot partition Size %d MB\n", bootsize);
6432e192b24SSimon Glass 	printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
6442e192b24SSimon Glass 	return CMD_RET_SUCCESS;
6452e192b24SSimon Glass }
646*bdb60996SAngelo Dureghello 
647*bdb60996SAngelo Dureghello static int mmc_partconf_print(struct mmc *mmc)
648*bdb60996SAngelo Dureghello {
649*bdb60996SAngelo Dureghello 	u8 ack, access, part;
650*bdb60996SAngelo Dureghello 
651*bdb60996SAngelo Dureghello 	if (mmc->part_config == MMCPART_NOAVAILABLE) {
652*bdb60996SAngelo Dureghello 		printf("No part_config info for ver. 0x%x\n", mmc->version);
653*bdb60996SAngelo Dureghello 		return CMD_RET_FAILURE;
654*bdb60996SAngelo Dureghello 	}
655*bdb60996SAngelo Dureghello 
656*bdb60996SAngelo Dureghello 	access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
657*bdb60996SAngelo Dureghello 	ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
658*bdb60996SAngelo Dureghello 	part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
659*bdb60996SAngelo Dureghello 
660*bdb60996SAngelo Dureghello 	printf("EXT_CSD[179], PARTITION_CONFIG:\n"
661*bdb60996SAngelo Dureghello 		"BOOT_ACK: 0x%x\n"
662*bdb60996SAngelo Dureghello 		"BOOT_PARTITION_ENABLE: 0x%x\n"
663*bdb60996SAngelo Dureghello 		"PARTITION_ACCESS: 0x%x\n", ack, part, access);
664*bdb60996SAngelo Dureghello 
665*bdb60996SAngelo Dureghello 	return CMD_RET_SUCCESS;
666*bdb60996SAngelo Dureghello }
667*bdb60996SAngelo Dureghello 
6682e192b24SSimon Glass static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag,
6692e192b24SSimon Glass 			   int argc, char * const argv[])
6702e192b24SSimon Glass {
6712e192b24SSimon Glass 	int dev;
6722e192b24SSimon Glass 	struct mmc *mmc;
6732e192b24SSimon Glass 	u8 ack, part_num, access;
6742e192b24SSimon Glass 
675*bdb60996SAngelo Dureghello 	if (argc != 2 && argc != 5)
6762e192b24SSimon Glass 		return CMD_RET_USAGE;
6772e192b24SSimon Glass 
6782e192b24SSimon Glass 	dev = simple_strtoul(argv[1], NULL, 10);
6792e192b24SSimon Glass 
6802e192b24SSimon Glass 	mmc = init_mmc_device(dev, false);
6812e192b24SSimon Glass 	if (!mmc)
6822e192b24SSimon Glass 		return CMD_RET_FAILURE;
6832e192b24SSimon Glass 
6842e192b24SSimon Glass 	if (IS_SD(mmc)) {
6852e192b24SSimon Glass 		puts("PARTITION_CONFIG only exists on eMMC\n");
6862e192b24SSimon Glass 		return CMD_RET_FAILURE;
6872e192b24SSimon Glass 	}
6882e192b24SSimon Glass 
689*bdb60996SAngelo Dureghello 	if (argc == 2)
690*bdb60996SAngelo Dureghello 		return mmc_partconf_print(mmc);
691*bdb60996SAngelo Dureghello 
692*bdb60996SAngelo Dureghello 	ack = simple_strtoul(argv[2], NULL, 10);
693*bdb60996SAngelo Dureghello 	part_num = simple_strtoul(argv[3], NULL, 10);
694*bdb60996SAngelo Dureghello 	access = simple_strtoul(argv[4], NULL, 10);
695*bdb60996SAngelo Dureghello 
6962e192b24SSimon Glass 	/* acknowledge to be sent during boot operation */
6972e192b24SSimon Glass 	return mmc_set_part_conf(mmc, ack, part_num, access);
6982e192b24SSimon Glass }
6992e192b24SSimon Glass static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag,
7002e192b24SSimon Glass 			   int argc, char * const argv[])
7012e192b24SSimon Glass {
7022e192b24SSimon Glass 	int dev;
7032e192b24SSimon Glass 	struct mmc *mmc;
7042e192b24SSimon Glass 	u8 enable;
7052e192b24SSimon Glass 
7062e192b24SSimon Glass 	/*
7072e192b24SSimon Glass 	 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
7082e192b24SSimon Glass 	 * The only valid values are 0x0, 0x1 and 0x2 and writing
7092e192b24SSimon Glass 	 * a value of 0x1 or 0x2 sets the value permanently.
7102e192b24SSimon Glass 	 */
7112e192b24SSimon Glass 	if (argc != 3)
7122e192b24SSimon Glass 		return CMD_RET_USAGE;
7132e192b24SSimon Glass 
7142e192b24SSimon Glass 	dev = simple_strtoul(argv[1], NULL, 10);
7152e192b24SSimon Glass 	enable = simple_strtoul(argv[2], NULL, 10);
7162e192b24SSimon Glass 
7172e192b24SSimon Glass 	if (enable > 2) {
7182e192b24SSimon Glass 		puts("Invalid RST_n_ENABLE value\n");
7192e192b24SSimon Glass 		return CMD_RET_USAGE;
7202e192b24SSimon Glass 	}
7212e192b24SSimon Glass 
7222e192b24SSimon Glass 	mmc = init_mmc_device(dev, false);
7232e192b24SSimon Glass 	if (!mmc)
7242e192b24SSimon Glass 		return CMD_RET_FAILURE;
7252e192b24SSimon Glass 
7262e192b24SSimon Glass 	if (IS_SD(mmc)) {
7272e192b24SSimon Glass 		puts("RST_n_FUNCTION only exists on eMMC\n");
7282e192b24SSimon Glass 		return CMD_RET_FAILURE;
7292e192b24SSimon Glass 	}
7302e192b24SSimon Glass 
7312e192b24SSimon Glass 	return mmc_set_rst_n_function(mmc, enable);
7322e192b24SSimon Glass }
7332e192b24SSimon Glass #endif
7342e192b24SSimon Glass static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag,
7352e192b24SSimon Glass 			 int argc, char * const argv[])
7362e192b24SSimon Glass {
7372e192b24SSimon Glass 	struct mmc *mmc;
7382e192b24SSimon Glass 	u32 val;
7392e192b24SSimon Glass 	int ret;
7402e192b24SSimon Glass 
7412e192b24SSimon Glass 	if (argc != 2)
7422e192b24SSimon Glass 		return CMD_RET_USAGE;
74384c1dfe4SMarkus Niebel 	val = simple_strtoul(argv[1], NULL, 16);
7442e192b24SSimon Glass 
7452e192b24SSimon Glass 	mmc = find_mmc_device(curr_device);
7462e192b24SSimon Glass 	if (!mmc) {
7472e192b24SSimon Glass 		printf("no mmc device at slot %x\n", curr_device);
7482e192b24SSimon Glass 		return CMD_RET_FAILURE;
7492e192b24SSimon Glass 	}
7502e192b24SSimon Glass 	ret = mmc_set_dsr(mmc, val);
7512e192b24SSimon Glass 	printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
7522e192b24SSimon Glass 	if (!ret) {
7532e192b24SSimon Glass 		mmc->has_init = 0;
7542e192b24SSimon Glass 		if (mmc_init(mmc))
7552e192b24SSimon Glass 			return CMD_RET_FAILURE;
7562e192b24SSimon Glass 		else
7572e192b24SSimon Glass 			return CMD_RET_SUCCESS;
7582e192b24SSimon Glass 	}
7592e192b24SSimon Glass 	return ret;
7602e192b24SSimon Glass }
7612e192b24SSimon Glass 
762cd3d4880STomas Melin #ifdef CONFIG_CMD_BKOPS_ENABLE
763cd3d4880STomas Melin static int do_mmc_bkops_enable(cmd_tbl_t *cmdtp, int flag,
764cd3d4880STomas Melin 				   int argc, char * const argv[])
765cd3d4880STomas Melin {
766cd3d4880STomas Melin 	int dev;
767cd3d4880STomas Melin 	struct mmc *mmc;
768cd3d4880STomas Melin 
769cd3d4880STomas Melin 	if (argc != 2)
770cd3d4880STomas Melin 		return CMD_RET_USAGE;
771cd3d4880STomas Melin 
772cd3d4880STomas Melin 	dev = simple_strtoul(argv[1], NULL, 10);
773cd3d4880STomas Melin 
774cd3d4880STomas Melin 	mmc = init_mmc_device(dev, false);
775cd3d4880STomas Melin 	if (!mmc)
776cd3d4880STomas Melin 		return CMD_RET_FAILURE;
777cd3d4880STomas Melin 
778cd3d4880STomas Melin 	if (IS_SD(mmc)) {
779cd3d4880STomas Melin 		puts("BKOPS_EN only exists on eMMC\n");
780cd3d4880STomas Melin 		return CMD_RET_FAILURE;
781cd3d4880STomas Melin 	}
782cd3d4880STomas Melin 
783cd3d4880STomas Melin 	return mmc_set_bkops_enable(mmc);
784cd3d4880STomas Melin }
785cd3d4880STomas Melin #endif
786cd3d4880STomas Melin 
7872e192b24SSimon Glass static cmd_tbl_t cmd_mmc[] = {
7882e192b24SSimon Glass 	U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
7892e192b24SSimon Glass 	U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
7902e192b24SSimon Glass 	U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
7912e192b24SSimon Glass 	U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
7922e192b24SSimon Glass 	U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
7932e192b24SSimon Glass 	U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
7942e192b24SSimon Glass 	U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
7952e192b24SSimon Glass 	U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
7962e192b24SSimon Glass 	U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
7972e192b24SSimon Glass #ifdef CONFIG_SUPPORT_EMMC_BOOT
7982e192b24SSimon Glass 	U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
7992e192b24SSimon Glass 	U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
8002e192b24SSimon Glass 	U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
8012e192b24SSimon Glass 	U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
8022e192b24SSimon Glass #endif
8032e192b24SSimon Glass #ifdef CONFIG_SUPPORT_EMMC_RPMB
8042e192b24SSimon Glass 	U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
8052e192b24SSimon Glass #endif
8062e192b24SSimon Glass 	U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
807cd3d4880STomas Melin #ifdef CONFIG_CMD_BKOPS_ENABLE
808cd3d4880STomas Melin 	U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
809cd3d4880STomas Melin #endif
8102e192b24SSimon Glass };
8112e192b24SSimon Glass 
8122e192b24SSimon Glass static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
8132e192b24SSimon Glass {
8142e192b24SSimon Glass 	cmd_tbl_t *cp;
8152e192b24SSimon Glass 
8162e192b24SSimon Glass 	cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
8172e192b24SSimon Glass 
8182e192b24SSimon Glass 	/* Drop the mmc command */
8192e192b24SSimon Glass 	argc--;
8202e192b24SSimon Glass 	argv++;
8212e192b24SSimon Glass 
8222e192b24SSimon Glass 	if (cp == NULL || argc > cp->maxargs)
8232e192b24SSimon Glass 		return CMD_RET_USAGE;
8242e192b24SSimon Glass 	if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
8252e192b24SSimon Glass 		return CMD_RET_SUCCESS;
8262e192b24SSimon Glass 
8272e192b24SSimon Glass 	if (curr_device < 0) {
8282e192b24SSimon Glass 		if (get_mmc_num() > 0) {
8292e192b24SSimon Glass 			curr_device = 0;
8302e192b24SSimon Glass 		} else {
8312e192b24SSimon Glass 			puts("No MMC device available\n");
8322e192b24SSimon Glass 			return CMD_RET_FAILURE;
8332e192b24SSimon Glass 		}
8342e192b24SSimon Glass 	}
8352e192b24SSimon Glass 	return cp->cmd(cmdtp, flag, argc, argv);
8362e192b24SSimon Glass }
8372e192b24SSimon Glass 
8382e192b24SSimon Glass U_BOOT_CMD(
8392e192b24SSimon Glass 	mmc, 29, 1, do_mmcops,
8402e192b24SSimon Glass 	"MMC sub system",
8412e192b24SSimon Glass 	"info - display info of the current MMC device\n"
8422e192b24SSimon Glass 	"mmc read addr blk# cnt\n"
8432e192b24SSimon Glass 	"mmc write addr blk# cnt\n"
8442e192b24SSimon Glass 	"mmc erase blk# cnt\n"
8452e192b24SSimon Glass 	"mmc rescan\n"
8462e192b24SSimon Glass 	"mmc part - lists available partition on current mmc device\n"
8472e192b24SSimon Glass 	"mmc dev [dev] [part] - show or set current mmc device [partition]\n"
8482e192b24SSimon Glass 	"mmc list - lists available devices\n"
8492e192b24SSimon Glass 	"mmc hwpartition [args...] - does hardware partitioning\n"
8502e192b24SSimon Glass 	"  arguments (sizes in 512-byte blocks):\n"
8512e192b24SSimon Glass 	"    [user [enh start cnt] [wrrel {on|off}]] - sets user data area attributes\n"
8522e192b24SSimon Glass 	"    [gp1|gp2|gp3|gp4 cnt [enh] [wrrel {on|off}]] - general purpose partition\n"
8532e192b24SSimon Glass 	"    [check|set|complete] - mode, complete set partitioning completed\n"
8542e192b24SSimon Glass 	"  WARNING: Partitioning is a write-once setting once it is set to complete.\n"
8552e192b24SSimon Glass 	"  Power cycling is required to initialize partitions after set to complete.\n"
8562e192b24SSimon Glass #ifdef CONFIG_SUPPORT_EMMC_BOOT
8572e192b24SSimon Glass 	"mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
8582e192b24SSimon Glass 	" - Set the BOOT_BUS_WIDTH field of the specified device\n"
8592e192b24SSimon Glass 	"mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
8602e192b24SSimon Glass 	" - Change sizes of boot and RPMB partitions of specified device\n"
861*bdb60996SAngelo Dureghello 	"mmc partconf dev [boot_ack boot_partition partition_access]\n"
862*bdb60996SAngelo Dureghello 	" - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
8632e192b24SSimon Glass 	"mmc rst-function dev value\n"
8642e192b24SSimon Glass 	" - Change the RST_n_FUNCTION field of the specified device\n"
8652e192b24SSimon Glass 	"   WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
8662e192b24SSimon Glass #endif
8672e192b24SSimon Glass #ifdef CONFIG_SUPPORT_EMMC_RPMB
8682e192b24SSimon Glass 	"mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
8692e192b24SSimon Glass 	"mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
8702e192b24SSimon Glass 	"mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
8712e192b24SSimon Glass 	"mmc rpmb counter - read the value of the write counter\n"
8722e192b24SSimon Glass #endif
8732e192b24SSimon Glass 	"mmc setdsr <value> - set DSR register value\n"
874cd3d4880STomas Melin #ifdef CONFIG_CMD_BKOPS_ENABLE
875cd3d4880STomas Melin 	"mmc bkops-enable <dev> - enable background operations handshake on device\n"
876cd3d4880STomas Melin 	"   WARNING: This is a write-once setting.\n"
877cd3d4880STomas Melin #endif
8782e192b24SSimon Glass 	);
8792e192b24SSimon Glass 
8802e192b24SSimon Glass /* Old command kept for compatibility. Same as 'mmc info' */
8812e192b24SSimon Glass U_BOOT_CMD(
8822e192b24SSimon Glass 	mmcinfo, 1, 0, do_mmcinfo,
8832e192b24SSimon Glass 	"display MMC info",
8842e192b24SSimon Glass 	"- display info of the current MMC device"
8852e192b24SSimon Glass );
886