1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2003
4  * Kyle Harris, kharris@nexus-tech.net
5  */
6 
7 #include <blk.h>
8 #include <command.h>
9 #include <console.h>
10 #include <display_options.h>
11 #include <env.h>
12 #include <mapmem.h>
13 #include <memalign.h>
14 #include <mmc.h>
15 #include <part.h>
16 #include <sparse_format.h>
17 #include <image-sparse.h>
18 #include <vsprintf.h>
19 #include <linux/ctype.h>
20 
21 static int curr_device = -1;
22 
print_mmcinfo(struct mmc * mmc)23 static void print_mmcinfo(struct mmc *mmc)
24 {
25 	int i;
26 
27 	printf("Device: %s\n", mmc->cfg->name);
28 	printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
29 	if (IS_SD(mmc)) {
30 		printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
31 		printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
32 		(mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
33 		(mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
34 	} else {
35 		printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xff);
36 		printf("Name: %c%c%c%c%c%c \n", mmc->cid[0] & 0xff,
37 		(mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
38 		(mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
39 		(mmc->cid[2] >> 24));
40 	}
41 
42 	printf("Bus Speed: %d\n", mmc->clock);
43 #if CONFIG_IS_ENABLED(MMC_VERBOSE)
44 	printf("Mode: %s\n", mmc_mode_name(mmc->selected_mode));
45 	mmc_dump_capabilities("card capabilities", mmc->card_caps);
46 	mmc_dump_capabilities("host capabilities", mmc->host_caps);
47 #endif
48 	printf("Rd Block Len: %d\n", mmc->read_bl_len);
49 
50 	printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
51 			EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
52 			EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
53 	if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
54 		printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
55 	printf("\n");
56 
57 	printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
58 	puts("Capacity: ");
59 	print_size(mmc->capacity, "\n");
60 
61 	printf("Bus Width: %d-bit%s\n", mmc->bus_width,
62 			mmc->ddr_mode ? " DDR" : "");
63 
64 #if CONFIG_IS_ENABLED(MMC_WRITE)
65 	puts("Erase Group Size: ");
66 	print_size(((u64)mmc->erase_grp_size) << 9, "\n");
67 #endif
68 
69 	if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
70 		bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
71 		bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
72 		ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
73 		u8 wp;
74 		int ret;
75 
76 #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
77 		puts("HC WP Group Size: ");
78 		print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
79 #endif
80 
81 		puts("User Capacity: ");
82 		print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
83 		if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
84 			puts(" WRREL\n");
85 		else
86 			putc('\n');
87 		if (usr_enh) {
88 			puts("User Enhanced Start: ");
89 			print_size(mmc->enh_user_start, "\n");
90 			puts("User Enhanced Size: ");
91 			print_size(mmc->enh_user_size, "\n");
92 		}
93 		puts("Boot Capacity: ");
94 		print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
95 		puts("RPMB Capacity: ");
96 		print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
97 
98 		for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
99 			bool is_enh = has_enh &&
100 				(mmc->part_attr & EXT_CSD_ENH_GP(i));
101 			if (mmc->capacity_gp[i]) {
102 				printf("GP%i Capacity: ", i+1);
103 				print_size(mmc->capacity_gp[i],
104 					   is_enh ? " ENH" : "");
105 				if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
106 					puts(" WRREL\n");
107 				else
108 					putc('\n');
109 			}
110 		}
111 		ret = mmc_send_ext_csd(mmc, ext_csd);
112 		if (ret)
113 			return;
114 		wp = ext_csd[EXT_CSD_BOOT_WP_STATUS];
115 		for (i = 0; i < 2; ++i) {
116 			printf("Boot area %d is ", i);
117 			switch (wp & 3) {
118 			case 0:
119 				printf("not write protected\n");
120 				break;
121 			case 1:
122 				printf("power on protected\n");
123 				break;
124 			case 2:
125 				printf("permanently protected\n");
126 				break;
127 			default:
128 				printf("in reserved protection state\n");
129 				break;
130 			}
131 			wp >>= 2;
132 		}
133 	}
134 }
135 
__init_mmc_device(int dev,bool force_init,enum bus_mode speed_mode)136 static struct mmc *__init_mmc_device(int dev, bool force_init,
137 				     enum bus_mode speed_mode)
138 {
139 	struct mmc *mmc;
140 	mmc = find_mmc_device(dev);
141 	if (!mmc) {
142 		printf("no mmc device at slot %x\n", dev);
143 		return NULL;
144 	}
145 
146 	if (!mmc_getcd(mmc))
147 		force_init = true;
148 
149 	if (force_init)
150 		mmc->has_init = 0;
151 
152 	if (IS_ENABLED(CONFIG_MMC_SPEED_MODE_SET))
153 		mmc->user_speed_mode = speed_mode;
154 
155 	if (mmc_init(mmc))
156 		return NULL;
157 
158 #ifdef CONFIG_BLOCK_CACHE
159 	struct blk_desc *bd = mmc_get_blk_desc(mmc);
160 	blkcache_invalidate(bd->uclass_id, bd->devnum);
161 #endif
162 
163 	return mmc;
164 }
165 
init_mmc_device(int dev,bool force_init)166 static struct mmc *init_mmc_device(int dev, bool force_init)
167 {
168 	return __init_mmc_device(dev, force_init, MMC_MODES_END);
169 }
170 
do_mmcinfo(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])171 static int do_mmcinfo(struct cmd_tbl *cmdtp, int flag, int argc,
172 		      char *const argv[])
173 {
174 	struct mmc *mmc;
175 
176 	if (curr_device < 0) {
177 		if (get_mmc_num() > 0)
178 			curr_device = 0;
179 		else {
180 			puts("No MMC device available\n");
181 			return CMD_RET_FAILURE;
182 		}
183 	}
184 
185 	mmc = init_mmc_device(curr_device, false);
186 	if (!mmc)
187 		return CMD_RET_FAILURE;
188 
189 	print_mmcinfo(mmc);
190 	return CMD_RET_SUCCESS;
191 }
192 
193 #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
confirm_key_prog(void)194 static int confirm_key_prog(void)
195 {
196 	puts("Warning: Programming authentication key can be done only once !\n"
197 	     "         Use this command only if you are sure of what you are doing,\n"
198 	     "Really perform the key programming? <y/N> ");
199 	if (confirm_yesno())
200 		return 1;
201 
202 	puts("Authentication key programming aborted\n");
203 	return 0;
204 }
205 
do_mmcrpmb_key(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])206 static int do_mmcrpmb_key(struct cmd_tbl *cmdtp, int flag,
207 			  int argc, char *const argv[])
208 {
209 	void *key_addr;
210 	struct mmc *mmc = find_mmc_device(curr_device);
211 
212 	if (argc != 2)
213 		return CMD_RET_USAGE;
214 
215 	key_addr = (void *)hextoul(argv[1], NULL);
216 	if (!confirm_key_prog())
217 		return CMD_RET_FAILURE;
218 	if (mmc_rpmb_set_key(mmc, key_addr)) {
219 		printf("ERROR - Key already programmed ?\n");
220 		return CMD_RET_FAILURE;
221 	}
222 	return CMD_RET_SUCCESS;
223 }
224 
do_mmcrpmb_read(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])225 static int do_mmcrpmb_read(struct cmd_tbl *cmdtp, int flag,
226 			   int argc, char *const argv[])
227 {
228 	u16 blk, cnt;
229 	void *addr;
230 	int n;
231 	void *key_addr = NULL;
232 	struct mmc *mmc = find_mmc_device(curr_device);
233 
234 	if (argc < 4)
235 		return CMD_RET_USAGE;
236 
237 	addr = (void *)hextoul(argv[1], NULL);
238 	blk = hextoul(argv[2], NULL);
239 	cnt = hextoul(argv[3], NULL);
240 
241 	if (argc == 5)
242 		key_addr = (void *)hextoul(argv[4], NULL);
243 
244 	printf("MMC RPMB read: dev # %d, block # %d, count %d ... ",
245 	       curr_device, blk, cnt);
246 	n =  mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
247 
248 	printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
249 	if (n != cnt)
250 		return CMD_RET_FAILURE;
251 	return CMD_RET_SUCCESS;
252 }
253 
do_mmcrpmb_write(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])254 static int do_mmcrpmb_write(struct cmd_tbl *cmdtp, int flag,
255 			    int argc, char *const argv[])
256 {
257 	u16 blk, cnt;
258 	void *addr;
259 	int n;
260 	void *key_addr;
261 	struct mmc *mmc = find_mmc_device(curr_device);
262 
263 	if (argc != 5)
264 		return CMD_RET_USAGE;
265 
266 	addr = (void *)hextoul(argv[1], NULL);
267 	blk = hextoul(argv[2], NULL);
268 	cnt = hextoul(argv[3], NULL);
269 	key_addr = (void *)hextoul(argv[4], NULL);
270 
271 	printf("MMC RPMB write: dev # %d, block # %d, count %d ... ",
272 	       curr_device, blk, cnt);
273 	n =  mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
274 
275 	printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
276 	if (n != cnt)
277 		return CMD_RET_FAILURE;
278 	return CMD_RET_SUCCESS;
279 }
280 
do_mmcrpmb_counter(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])281 static int do_mmcrpmb_counter(struct cmd_tbl *cmdtp, int flag,
282 			      int argc, char *const argv[])
283 {
284 	unsigned long counter;
285 	struct mmc *mmc = find_mmc_device(curr_device);
286 
287 	if (mmc_rpmb_get_counter(mmc, &counter))
288 		return CMD_RET_FAILURE;
289 	printf("RPMB Write counter= %lx\n", counter);
290 	return CMD_RET_SUCCESS;
291 }
292 
293 static struct cmd_tbl cmd_rpmb[] = {
294 	U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
295 	U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
296 	U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
297 	U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
298 };
299 
do_mmcrpmb(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])300 static int do_mmcrpmb(struct cmd_tbl *cmdtp, int flag,
301 		      int argc, char *const argv[])
302 {
303 	struct cmd_tbl *cp;
304 	struct mmc *mmc;
305 	char original_part;
306 	int ret;
307 
308 	cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
309 
310 	/* Drop the rpmb subcommand */
311 	argc--;
312 	argv++;
313 
314 	if (cp == NULL || argc > cp->maxargs)
315 		return CMD_RET_USAGE;
316 	if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
317 		return CMD_RET_SUCCESS;
318 
319 	mmc = init_mmc_device(curr_device, false);
320 	if (!mmc)
321 		return CMD_RET_FAILURE;
322 
323 	if (!(mmc->version & MMC_VERSION_MMC)) {
324 		printf("It is not an eMMC device\n");
325 		return CMD_RET_FAILURE;
326 	}
327 	if (mmc->version < MMC_VERSION_4_41) {
328 		printf("RPMB not supported before version 4.41\n");
329 		return CMD_RET_FAILURE;
330 	}
331 	/* Switch to the RPMB partition */
332 #ifndef CONFIG_BLK
333 	original_part = mmc->block_dev.hwpart;
334 #else
335 	original_part = mmc_get_blk_desc(mmc)->hwpart;
336 #endif
337 	if (blk_select_hwpart_devnum(UCLASS_MMC, curr_device, MMC_PART_RPMB) !=
338 	    0)
339 		return CMD_RET_FAILURE;
340 	ret = cp->cmd(cmdtp, flag, argc, argv);
341 
342 	/* Return to original partition */
343 	if (blk_select_hwpart_devnum(UCLASS_MMC, curr_device, original_part) !=
344 	    0)
345 		return CMD_RET_FAILURE;
346 	return ret;
347 }
348 #endif
349 
do_mmc_read(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])350 static int do_mmc_read(struct cmd_tbl *cmdtp, int flag,
351 		       int argc, char *const argv[])
352 {
353 	struct mmc *mmc;
354 	u32 blk, cnt, n;
355 	void *ptr;
356 
357 	if (argc != 4)
358 		return CMD_RET_USAGE;
359 
360 	ptr = map_sysmem(hextoul(argv[1], NULL), 0);
361 	blk = hextoul(argv[2], NULL);
362 	cnt = hextoul(argv[3], NULL);
363 
364 	mmc = init_mmc_device(curr_device, false);
365 	if (!mmc)
366 		return CMD_RET_FAILURE;
367 
368 	printf("MMC read: dev # %d, block # %d, count %d ... ",
369 	       curr_device, blk, cnt);
370 
371 	n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, ptr);
372 	printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
373 	unmap_sysmem(ptr);
374 
375 	return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
376 }
377 
378 #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
mmc_sparse_write(struct sparse_storage * info,lbaint_t blk,lbaint_t blkcnt,const void * buffer)379 static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk,
380 				 lbaint_t blkcnt, const void *buffer)
381 {
382 	struct blk_desc *dev_desc = info->priv;
383 
384 	return blk_dwrite(dev_desc, blk, blkcnt, buffer);
385 }
386 
mmc_sparse_reserve(struct sparse_storage * info,lbaint_t blk,lbaint_t blkcnt)387 static lbaint_t mmc_sparse_reserve(struct sparse_storage *info,
388 				   lbaint_t blk, lbaint_t blkcnt)
389 {
390 	return blkcnt;
391 }
392 
do_mmc_sparse_write(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])393 static int do_mmc_sparse_write(struct cmd_tbl *cmdtp, int flag,
394 			       int argc, char *const argv[])
395 {
396 	struct sparse_storage sparse;
397 	struct blk_desc *dev_desc;
398 	struct mmc *mmc;
399 	char dest[11];
400 	void *addr;
401 	u32 blk;
402 
403 	if (argc != 3)
404 		return CMD_RET_USAGE;
405 
406 	addr = (void *)hextoul(argv[1], NULL);
407 	blk = hextoul(argv[2], NULL);
408 
409 	if (!is_sparse_image(addr)) {
410 		printf("Not a sparse image\n");
411 		return CMD_RET_FAILURE;
412 	}
413 
414 	mmc = init_mmc_device(curr_device, false);
415 	if (!mmc)
416 		return CMD_RET_FAILURE;
417 
418 	printf("MMC Sparse write: dev # %d, block # %d ... ",
419 	       curr_device, blk);
420 
421 	if (mmc_getwp(mmc) == 1) {
422 		printf("Error: card is write protected!\n");
423 		return CMD_RET_FAILURE;
424 	}
425 
426 	dev_desc = mmc_get_blk_desc(mmc);
427 	sparse.priv = dev_desc;
428 	sparse.blksz = 512;
429 	sparse.start = blk;
430 	sparse.size = dev_desc->lba - blk;
431 	sparse.write = mmc_sparse_write;
432 	sparse.reserve = mmc_sparse_reserve;
433 	sparse.mssg = NULL;
434 	sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz);
435 
436 	if (write_sparse_image(&sparse, dest, addr, NULL))
437 		return CMD_RET_FAILURE;
438 	else
439 		return CMD_RET_SUCCESS;
440 }
441 #endif
442 
443 #if CONFIG_IS_ENABLED(MMC_WRITE)
do_mmc_write(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])444 static int do_mmc_write(struct cmd_tbl *cmdtp, int flag,
445 			int argc, char *const argv[])
446 {
447 	struct mmc *mmc;
448 	u32 blk, cnt, n;
449 	void *ptr;
450 
451 	if (argc != 4)
452 		return CMD_RET_USAGE;
453 
454 	ptr = map_sysmem(hextoul(argv[1], NULL), 0);
455 	blk = hextoul(argv[2], NULL);
456 	cnt = hextoul(argv[3], NULL);
457 
458 	mmc = init_mmc_device(curr_device, false);
459 	if (!mmc)
460 		return CMD_RET_FAILURE;
461 
462 	printf("MMC write: dev # %d, block # %d, count %d ... ",
463 	       curr_device, blk, cnt);
464 
465 	if (mmc_getwp(mmc) == 1) {
466 		printf("Error: card is write protected!\n");
467 		return CMD_RET_FAILURE;
468 	}
469 	n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, ptr);
470 	printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
471 	unmap_sysmem(ptr);
472 
473 	return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
474 }
475 
do_mmc_erase(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])476 static int do_mmc_erase(struct cmd_tbl *cmdtp, int flag,
477 			int argc, char *const argv[])
478 {
479 	struct mmc *mmc;
480 	struct disk_partition info;
481 	u32 blk, cnt, n;
482 
483 	if (argc < 2 || argc > 3)
484 		return CMD_RET_USAGE;
485 
486 	mmc = init_mmc_device(curr_device, false);
487 	if (!mmc)
488 		return CMD_RET_FAILURE;
489 
490 	if (argc == 3) {
491 		blk = hextoul(argv[1], NULL);
492 		cnt = hextoul(argv[2], NULL);
493 	} else if (part_get_info_by_name(mmc_get_blk_desc(mmc), argv[1], &info) >= 0) {
494 		blk = info.start;
495 		cnt = info.size;
496 	} else {
497 		return CMD_RET_FAILURE;
498 	}
499 
500 	printf("MMC erase: dev # %d, block # %d, count %d ... ",
501 	       curr_device, blk, cnt);
502 
503 	if (mmc_getwp(mmc) == 1) {
504 		printf("Error: card is write protected!\n");
505 		return CMD_RET_FAILURE;
506 	}
507 	n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
508 	printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
509 
510 	return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
511 }
512 #endif
513 
do_mmc_rescan(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])514 static int do_mmc_rescan(struct cmd_tbl *cmdtp, int flag,
515 			 int argc, char *const argv[])
516 {
517 	struct mmc *mmc;
518 
519 	if (argc == 1) {
520 		mmc = init_mmc_device(curr_device, true);
521 	} else if (argc == 2) {
522 		enum bus_mode speed_mode;
523 
524 		speed_mode = (int)dectoul(argv[1], NULL);
525 		mmc = __init_mmc_device(curr_device, true, speed_mode);
526 	} else {
527 		return CMD_RET_USAGE;
528 	}
529 
530 	if (!mmc)
531 		return CMD_RET_FAILURE;
532 
533 	return CMD_RET_SUCCESS;
534 }
535 
do_mmc_part(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])536 static int do_mmc_part(struct cmd_tbl *cmdtp, int flag,
537 		       int argc, char *const argv[])
538 {
539 	struct blk_desc *mmc_dev;
540 	struct mmc *mmc;
541 
542 	mmc = init_mmc_device(curr_device, false);
543 	if (!mmc)
544 		return CMD_RET_FAILURE;
545 
546 	mmc_dev = blk_get_devnum_by_uclass_id(UCLASS_MMC, curr_device);
547 	if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
548 		part_print(mmc_dev);
549 		return CMD_RET_SUCCESS;
550 	}
551 
552 	puts("get mmc type error!\n");
553 	return CMD_RET_FAILURE;
554 }
555 
do_mmc_dev(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])556 static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag,
557 		      int argc, char *const argv[])
558 {
559 	int dev, part = 0, ret;
560 	struct mmc *mmc;
561 
562 	if (argc == 1) {
563 		dev = curr_device;
564 		mmc = init_mmc_device(dev, true);
565 	} else if (argc == 2) {
566 		dev = (int)dectoul(argv[1], NULL);
567 		mmc = init_mmc_device(dev, true);
568 	} else if (argc == 3) {
569 		dev = (int)dectoul(argv[1], NULL);
570 		part = (int)dectoul(argv[2], NULL);
571 		if (part > PART_ACCESS_MASK) {
572 			printf("#part_num shouldn't be larger than %d\n",
573 			       PART_ACCESS_MASK);
574 			return CMD_RET_FAILURE;
575 		}
576 		mmc = init_mmc_device(dev, true);
577 	} else if (argc == 4) {
578 		enum bus_mode speed_mode;
579 
580 		dev = (int)dectoul(argv[1], NULL);
581 		part = (int)dectoul(argv[2], NULL);
582 		if (part > PART_ACCESS_MASK) {
583 			printf("#part_num shouldn't be larger than %d\n",
584 			       PART_ACCESS_MASK);
585 			return CMD_RET_FAILURE;
586 		}
587 		speed_mode = (int)dectoul(argv[3], NULL);
588 		mmc = __init_mmc_device(dev, true, speed_mode);
589 	} else {
590 		return CMD_RET_USAGE;
591 	}
592 
593 	if (!mmc)
594 		return CMD_RET_FAILURE;
595 
596 	ret = blk_select_hwpart_devnum(UCLASS_MMC, dev, part);
597 	printf("switch to partitions #%d, %s\n",
598 	       part, (!ret) ? "OK" : "ERROR");
599 	if (ret)
600 		return 1;
601 
602 	curr_device = dev;
603 	if (mmc->part_config == MMCPART_NOAVAILABLE)
604 		printf("mmc%d is current device\n", curr_device);
605 	else
606 		printf("mmc%d(part %d) is current device\n",
607 		       curr_device, mmc_get_blk_desc(mmc)->hwpart);
608 
609 	return CMD_RET_SUCCESS;
610 }
611 
do_mmc_list(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])612 static int do_mmc_list(struct cmd_tbl *cmdtp, int flag,
613 		       int argc, char *const argv[])
614 {
615 	print_mmc_devices('\n');
616 	return CMD_RET_SUCCESS;
617 }
618 
619 #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
parse_hwpart_user_enh_size(struct mmc * mmc,struct mmc_hwpart_conf * pconf,char * argv)620 static void parse_hwpart_user_enh_size(struct mmc *mmc,
621 				       struct mmc_hwpart_conf *pconf,
622 				       char *argv)
623 {
624 	int i, ret;
625 
626 	pconf->user.enh_size = 0;
627 
628 	if (!strcmp(argv, "-"))	{ /* The rest of eMMC */
629 		ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
630 		ret = mmc_send_ext_csd(mmc, ext_csd);
631 		if (ret)
632 			return;
633 		/* The enh_size value is in 512B block units */
634 		pconf->user.enh_size =
635 			((ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 2] << 16) +
636 			(ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 1] << 8) +
637 			ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT]) * 1024 *
638 			ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
639 			ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
640 		pconf->user.enh_size -= pconf->user.enh_start;
641 		for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
642 			/*
643 			 * If the eMMC already has GP partitions set,
644 			 * subtract their size from the maximum USER
645 			 * partition size.
646 			 *
647 			 * Else, if the command was used to configure new
648 			 * GP partitions, subtract their size from maximum
649 			 * USER partition size.
650 			 */
651 			if (mmc->capacity_gp[i]) {
652 				/* The capacity_gp is in 1B units */
653 				pconf->user.enh_size -= mmc->capacity_gp[i] >> 9;
654 			} else if (pconf->gp_part[i].size) {
655 				/* The gp_part[].size is in 512B units */
656 				pconf->user.enh_size -= pconf->gp_part[i].size;
657 			}
658 		}
659 	} else {
660 		pconf->user.enh_size = dectoul(argv, NULL);
661 	}
662 }
663 
parse_hwpart_user(struct mmc * mmc,struct mmc_hwpart_conf * pconf,int argc,char * const argv[])664 static int parse_hwpart_user(struct mmc *mmc, struct mmc_hwpart_conf *pconf,
665 			     int argc, char *const argv[])
666 {
667 	int i = 0;
668 
669 	memset(&pconf->user, 0, sizeof(pconf->user));
670 
671 	while (i < argc) {
672 		if (!strcmp(argv[i], "enh")) {
673 			if (i + 2 >= argc)
674 				return -1;
675 			pconf->user.enh_start =
676 				dectoul(argv[i + 1], NULL);
677 			parse_hwpart_user_enh_size(mmc, pconf, argv[i + 2]);
678 			i += 3;
679 		} else if (!strcmp(argv[i], "wrrel")) {
680 			if (i + 1 >= argc)
681 				return -1;
682 			pconf->user.wr_rel_change = 1;
683 			if (!strcmp(argv[i+1], "on"))
684 				pconf->user.wr_rel_set = 1;
685 			else if (!strcmp(argv[i+1], "off"))
686 				pconf->user.wr_rel_set = 0;
687 			else
688 				return -1;
689 			i += 2;
690 		} else {
691 			break;
692 		}
693 	}
694 	return i;
695 }
696 
parse_hwpart_gp(struct mmc_hwpart_conf * pconf,int pidx,int argc,char * const argv[])697 static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
698 			   int argc, char *const argv[])
699 {
700 	int i;
701 
702 	memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
703 
704 	if (1 >= argc)
705 		return -1;
706 	pconf->gp_part[pidx].size = dectoul(argv[0], NULL);
707 
708 	i = 1;
709 	while (i < argc) {
710 		if (!strcmp(argv[i], "enh")) {
711 			pconf->gp_part[pidx].enhanced = 1;
712 			i += 1;
713 		} else if (!strcmp(argv[i], "wrrel")) {
714 			if (i + 1 >= argc)
715 				return -1;
716 			pconf->gp_part[pidx].wr_rel_change = 1;
717 			if (!strcmp(argv[i+1], "on"))
718 				pconf->gp_part[pidx].wr_rel_set = 1;
719 			else if (!strcmp(argv[i+1], "off"))
720 				pconf->gp_part[pidx].wr_rel_set = 0;
721 			else
722 				return -1;
723 			i += 2;
724 		} else {
725 			break;
726 		}
727 	}
728 	return i;
729 }
730 
do_mmc_hwpartition(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])731 static int do_mmc_hwpartition(struct cmd_tbl *cmdtp, int flag,
732 			      int argc, char *const argv[])
733 {
734 	struct mmc *mmc;
735 	struct mmc_hwpart_conf pconf = { };
736 	enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
737 	int i, r, pidx;
738 
739 	mmc = init_mmc_device(curr_device, false);
740 	if (!mmc)
741 		return CMD_RET_FAILURE;
742 
743 	if (IS_SD(mmc)) {
744 		puts("SD doesn't support partitioning\n");
745 		return CMD_RET_FAILURE;
746 	}
747 
748 	if (argc < 1)
749 		return CMD_RET_USAGE;
750 	i = 1;
751 	while (i < argc) {
752 		if (!strcmp(argv[i], "user")) {
753 			i++;
754 			r = parse_hwpart_user(mmc, &pconf, argc - i, &argv[i]);
755 			if (r < 0)
756 				return CMD_RET_USAGE;
757 			i += r;
758 		} else if (!strncmp(argv[i], "gp", 2) &&
759 			   strlen(argv[i]) == 3 &&
760 			   argv[i][2] >= '1' && argv[i][2] <= '4') {
761 			pidx = argv[i][2] - '1';
762 			i++;
763 			r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
764 			if (r < 0)
765 				return CMD_RET_USAGE;
766 			i += r;
767 		} else if (!strcmp(argv[i], "check")) {
768 			mode = MMC_HWPART_CONF_CHECK;
769 			i++;
770 		} else if (!strcmp(argv[i], "set")) {
771 			mode = MMC_HWPART_CONF_SET;
772 			i++;
773 		} else if (!strcmp(argv[i], "complete")) {
774 			mode = MMC_HWPART_CONF_COMPLETE;
775 			i++;
776 		} else {
777 			return CMD_RET_USAGE;
778 		}
779 	}
780 
781 	puts("Partition configuration:\n");
782 	if (pconf.user.enh_size) {
783 		puts("\tUser Enhanced Start: ");
784 		print_size(((u64)pconf.user.enh_start) << 9, "\n");
785 		puts("\tUser Enhanced Size: ");
786 		print_size(((u64)pconf.user.enh_size) << 9, "\n");
787 	} else {
788 		puts("\tNo enhanced user data area\n");
789 	}
790 	if (pconf.user.wr_rel_change)
791 		printf("\tUser partition write reliability: %s\n",
792 		       pconf.user.wr_rel_set ? "on" : "off");
793 	for (pidx = 0; pidx < 4; pidx++) {
794 		if (pconf.gp_part[pidx].size) {
795 			printf("\tGP%i Capacity: ", pidx+1);
796 			print_size(((u64)pconf.gp_part[pidx].size) << 9,
797 				   pconf.gp_part[pidx].enhanced ?
798 				   " ENH\n" : "\n");
799 		} else {
800 			printf("\tNo GP%i partition\n", pidx+1);
801 		}
802 		if (pconf.gp_part[pidx].wr_rel_change)
803 			printf("\tGP%i write reliability: %s\n", pidx+1,
804 			       pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
805 	}
806 
807 	if (!mmc_hwpart_config(mmc, &pconf, mode)) {
808 		if (mode == MMC_HWPART_CONF_COMPLETE)
809 			puts("Partitioning successful, "
810 			     "power-cycle to make effective\n");
811 		return CMD_RET_SUCCESS;
812 	} else {
813 		puts("Failed!\n");
814 		return CMD_RET_FAILURE;
815 	}
816 }
817 #endif
818 
819 #ifdef CONFIG_SUPPORT_EMMC_BOOT
do_mmc_bootbus(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])820 static int do_mmc_bootbus(struct cmd_tbl *cmdtp, int flag,
821 			  int argc, char *const argv[])
822 {
823 	int dev;
824 	struct mmc *mmc;
825 	u8 width, reset, mode;
826 
827 	if (argc != 5)
828 		return CMD_RET_USAGE;
829 	dev = dectoul(argv[1], NULL);
830 	width = dectoul(argv[2], NULL);
831 	reset = dectoul(argv[3], NULL);
832 	mode = dectoul(argv[4], NULL);
833 
834 	mmc = init_mmc_device(dev, false);
835 	if (!mmc)
836 		return CMD_RET_FAILURE;
837 
838 	if (IS_SD(mmc)) {
839 		puts("BOOT_BUS_WIDTH only exists on eMMC\n");
840 		return CMD_RET_FAILURE;
841 	}
842 
843 	/*
844 	 * BOOT_BUS_CONDITIONS[177]
845 	 * BOOT_MODE[4:3]
846 	 * 0x0 : Use SDR + Backward compatible timing in boot operation
847 	 * 0x1 : Use SDR + High Speed Timing in boot operation mode
848 	 * 0x2 : Use DDR in boot operation
849 	 * RESET_BOOT_BUS_CONDITIONS
850 	 * 0x0 : Reset bus width to x1, SDR, Backward compatible
851 	 * 0x1 : Retain BOOT_BUS_WIDTH and BOOT_MODE
852 	 * BOOT_BUS_WIDTH
853 	 * 0x0 : x1(sdr) or x4 (ddr) buswidth
854 	 * 0x1 : x4(sdr/ddr) buswith
855 	 * 0x2 : x8(sdr/ddr) buswith
856 	 *
857 	 */
858 	if (width >= 0x3) {
859 		printf("boot_bus_width %d is invalid\n", width);
860 		return CMD_RET_FAILURE;
861 	}
862 
863 	if (reset >= 0x2) {
864 		printf("reset_boot_bus_width %d is invalid\n", reset);
865 		return CMD_RET_FAILURE;
866 	}
867 
868 	if (mode >= 0x3) {
869 		printf("reset_boot_bus_width %d is invalid\n", mode);
870 		return CMD_RET_FAILURE;
871 	}
872 
873 	/* acknowledge to be sent during boot operation */
874 	if (mmc_set_boot_bus_width(mmc, width, reset, mode)) {
875 		puts("BOOT_BUS_WIDTH is failed to change.\n");
876 		return CMD_RET_FAILURE;
877 	}
878 
879 	printf("Set to BOOT_BUS_WIDTH = 0x%x, RESET = 0x%x, BOOT_MODE = 0x%x\n",
880 			width, reset, mode);
881 	return CMD_RET_SUCCESS;
882 }
883 
do_mmc_boot_resize(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])884 static int do_mmc_boot_resize(struct cmd_tbl *cmdtp, int flag,
885 			      int argc, char *const argv[])
886 {
887 	int dev;
888 	struct mmc *mmc;
889 	u32 bootsize, rpmbsize;
890 
891 	if (argc != 4)
892 		return CMD_RET_USAGE;
893 	dev = dectoul(argv[1], NULL);
894 	bootsize = dectoul(argv[2], NULL);
895 	rpmbsize = dectoul(argv[3], NULL);
896 
897 	mmc = init_mmc_device(dev, false);
898 	if (!mmc)
899 		return CMD_RET_FAILURE;
900 
901 	if (IS_SD(mmc)) {
902 		printf("It is not an eMMC device\n");
903 		return CMD_RET_FAILURE;
904 	}
905 
906 	if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
907 		printf("EMMC boot partition Size change Failed.\n");
908 		return CMD_RET_FAILURE;
909 	}
910 
911 	printf("EMMC boot partition Size %d MB\n", bootsize);
912 	printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
913 	return CMD_RET_SUCCESS;
914 }
915 
mmc_partconf_print(struct mmc * mmc,const char * varname)916 static int mmc_partconf_print(struct mmc *mmc, const char *varname)
917 {
918 	u8 ack, access, part;
919 
920 	if (mmc->part_config == MMCPART_NOAVAILABLE) {
921 		printf("No part_config info for ver. 0x%x\n", mmc->version);
922 		return CMD_RET_FAILURE;
923 	}
924 
925 	access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
926 	ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
927 	part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
928 
929 	if(varname)
930 		env_set_hex(varname, part);
931 
932 	printf("EXT_CSD[179], PARTITION_CONFIG:\n"
933 		"BOOT_ACK: 0x%x\n"
934 		"BOOT_PARTITION_ENABLE: 0x%x (%s)\n"
935 		"PARTITION_ACCESS: 0x%x (%s)\n", ack, part, emmc_boot_part_names[part],
936 		access, emmc_hwpart_names[access]);
937 
938 	return CMD_RET_SUCCESS;
939 }
940 
do_mmc_partconf(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])941 static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag,
942 			   int argc, char *const argv[])
943 {
944 	int ret, dev;
945 	struct mmc *mmc;
946 	u8 ack, part_num, access;
947 
948 	if (argc != 2 && argc != 3 && argc != 5)
949 		return CMD_RET_USAGE;
950 
951 	dev = dectoul(argv[1], NULL);
952 
953 	mmc = init_mmc_device(dev, false);
954 	if (!mmc)
955 		return CMD_RET_FAILURE;
956 
957 	if (IS_SD(mmc)) {
958 		puts("PARTITION_CONFIG only exists on eMMC\n");
959 		return CMD_RET_FAILURE;
960 	}
961 
962 	if (argc == 2 || argc == 3)
963 		return mmc_partconf_print(mmc, cmd_arg2(argc, argv));
964 
965 	/* BOOT_ACK */
966 	ack = dectoul(argv[2], NULL);
967 	/* BOOT_PARTITION_ENABLE */
968 	if (!isdigit(*argv[3])) {
969 		for (part_num = ARRAY_SIZE(emmc_boot_part_names) - 1; part_num > 0; part_num--) {
970 			if (!strcmp(argv[3], emmc_boot_part_names[part_num]))
971 				break;
972 		}
973 	} else {
974 		part_num = dectoul(argv[3], NULL);
975 	}
976 	/* PARTITION_ACCESS */
977 	if (!isdigit(*argv[4])) {
978 		for (access = ARRAY_SIZE(emmc_hwpart_names) - 1; access > 0; access--) {
979 			if (!strcmp(argv[4], emmc_hwpart_names[access]))
980 				break;
981 		}
982 	} else {
983 		access = dectoul(argv[4], NULL);
984 	}
985 
986 	/* acknowledge to be sent during boot operation */
987 	ret = mmc_set_part_conf(mmc, ack, part_num, access);
988 	if (ret != 0)
989 		return CMD_RET_FAILURE;
990 
991 	return CMD_RET_SUCCESS;
992 }
993 
do_mmc_rst_func(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])994 static int do_mmc_rst_func(struct cmd_tbl *cmdtp, int flag,
995 			   int argc, char *const argv[])
996 {
997 	int ret, dev;
998 	struct mmc *mmc;
999 	u8 enable;
1000 
1001 	/*
1002 	 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
1003 	 * The only valid values are 0x0, 0x1 and 0x2 and writing
1004 	 * a value of 0x1 or 0x2 sets the value permanently.
1005 	 */
1006 	if (argc != 3)
1007 		return CMD_RET_USAGE;
1008 
1009 	dev = dectoul(argv[1], NULL);
1010 	enable = dectoul(argv[2], NULL);
1011 
1012 	if (enable > 2) {
1013 		puts("Invalid RST_n_ENABLE value\n");
1014 		return CMD_RET_USAGE;
1015 	}
1016 
1017 	mmc = init_mmc_device(dev, false);
1018 	if (!mmc)
1019 		return CMD_RET_FAILURE;
1020 
1021 	if (IS_SD(mmc)) {
1022 		puts("RST_n_FUNCTION only exists on eMMC\n");
1023 		return CMD_RET_FAILURE;
1024 	}
1025 
1026 	ret = mmc_set_rst_n_function(mmc, enable);
1027 	if (ret != 0)
1028 		return CMD_RET_FAILURE;
1029 
1030 	return CMD_RET_SUCCESS;
1031 }
1032 #endif
do_mmc_setdsr(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])1033 static int do_mmc_setdsr(struct cmd_tbl *cmdtp, int flag,
1034 			 int argc, char *const argv[])
1035 {
1036 	struct mmc *mmc;
1037 	u32 val;
1038 	int ret;
1039 
1040 	if (argc != 2)
1041 		return CMD_RET_USAGE;
1042 	val = hextoul(argv[1], NULL);
1043 
1044 	mmc = find_mmc_device(curr_device);
1045 	if (!mmc) {
1046 		printf("no mmc device at slot %x\n", curr_device);
1047 		return CMD_RET_FAILURE;
1048 	}
1049 	ret = mmc_set_dsr(mmc, val);
1050 	printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
1051 	if (!ret) {
1052 		mmc->has_init = 0;
1053 		if (mmc_init(mmc))
1054 			return CMD_RET_FAILURE;
1055 		else
1056 			return CMD_RET_SUCCESS;
1057 	}
1058 	return ret;
1059 }
1060 
1061 #ifdef CONFIG_CMD_BKOPS_ENABLE
mmc_bkops_common(char * device,bool autobkops,bool enable)1062 static int mmc_bkops_common(char *device, bool autobkops, bool enable)
1063 {
1064 	struct mmc *mmc;
1065 	int dev;
1066 
1067 	dev = dectoul(device, NULL);
1068 
1069 	mmc = init_mmc_device(dev, false);
1070 	if (!mmc)
1071 		return CMD_RET_FAILURE;
1072 
1073 	if (IS_SD(mmc)) {
1074 		puts("BKOPS_EN only exists on eMMC\n");
1075 		return CMD_RET_FAILURE;
1076 	}
1077 
1078 	return mmc_set_bkops_enable(mmc, autobkops, enable);
1079 }
1080 
do_mmc_bkops(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])1081 static int do_mmc_bkops(struct cmd_tbl *cmdtp, int flag,
1082 			int argc, char * const argv[])
1083 {
1084 	bool autobkops, enable;
1085 
1086 	if (argc != 4)
1087 		return CMD_RET_USAGE;
1088 
1089 	if (!strcmp(argv[2], "manual"))
1090 		autobkops = false;
1091 	else if (!strcmp(argv[2], "auto"))
1092 		autobkops = true;
1093 	else
1094 		return CMD_RET_FAILURE;
1095 
1096 	if (!strcmp(argv[3], "disable"))
1097 		enable = false;
1098 	else if (!strcmp(argv[3], "enable"))
1099 		enable = true;
1100 	else
1101 		return CMD_RET_FAILURE;
1102 
1103 	return mmc_bkops_common(argv[1], autobkops, enable);
1104 }
1105 
do_mmc_bkops_enable(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])1106 static int do_mmc_bkops_enable(struct cmd_tbl *cmdtp, int flag,
1107 			       int argc, char * const argv[])
1108 {
1109 	if (argc != 2)
1110 		return CMD_RET_USAGE;
1111 
1112 	return mmc_bkops_common(argv[1], false, true);
1113 }
1114 #endif
1115 
do_mmc_boot_wp(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])1116 static int do_mmc_boot_wp(struct cmd_tbl *cmdtp, int flag,
1117 			  int argc, char * const argv[])
1118 {
1119 	int err;
1120 	struct mmc *mmc;
1121 	int part;
1122 
1123 	mmc = init_mmc_device(curr_device, false);
1124 	if (!mmc)
1125 		return CMD_RET_FAILURE;
1126 	if (IS_SD(mmc)) {
1127 		printf("It is not an eMMC device\n");
1128 		return CMD_RET_FAILURE;
1129 	}
1130 
1131 	if (argc == 2) {
1132 		part = dectoul(argv[1], NULL);
1133 		err = mmc_boot_wp_single_partition(mmc, part);
1134 	} else {
1135 		err = mmc_boot_wp(mmc);
1136 	}
1137 
1138 	if (err)
1139 		return CMD_RET_FAILURE;
1140 	printf("boot areas protected\n");
1141 	return CMD_RET_SUCCESS;
1142 }
1143 
1144 #if CONFIG_IS_ENABLED(CMD_MMC_REG)
do_mmc_reg(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])1145 static int do_mmc_reg(struct cmd_tbl *cmdtp, int flag,
1146 		      int argc, char *const argv[])
1147 {
1148 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1149 	struct mmc *mmc;
1150 	int i, ret;
1151 	u32 off;
1152 
1153 	if (argc < 3 || argc > 5)
1154 		return CMD_RET_USAGE;
1155 
1156 	mmc = find_mmc_device(curr_device);
1157 	if (!mmc) {
1158 		printf("no mmc device at slot %x\n", curr_device);
1159 		return CMD_RET_FAILURE;
1160 	}
1161 
1162 	if (IS_SD(mmc)) {
1163 		printf("SD registers are not supported\n");
1164 		return CMD_RET_FAILURE;
1165 	}
1166 
1167 	off = simple_strtoul(argv[3], NULL, 10);
1168 	if (!strcmp(argv[2], "cid")) {
1169 		if (off > 3)
1170 			return CMD_RET_USAGE;
1171 		printf("CID[%i]: 0x%08x\n", off, mmc->cid[off]);
1172 		if (argv[4])
1173 			env_set_hex(argv[4], mmc->cid[off]);
1174 		return CMD_RET_SUCCESS;
1175 	}
1176 	if (!strcmp(argv[2], "csd")) {
1177 		if (off > 3)
1178 			return CMD_RET_USAGE;
1179 		printf("CSD[%i]: 0x%08x\n", off, mmc->csd[off]);
1180 		if (argv[4])
1181 			env_set_hex(argv[4], mmc->csd[off]);
1182 		return CMD_RET_SUCCESS;
1183 	}
1184 	if (!strcmp(argv[2], "dsr")) {
1185 		printf("DSR: 0x%08x\n", mmc->dsr);
1186 		if (argv[4])
1187 			env_set_hex(argv[4], mmc->dsr);
1188 		return CMD_RET_SUCCESS;
1189 	}
1190 	if (!strcmp(argv[2], "ocr")) {
1191 		printf("OCR: 0x%08x\n", mmc->ocr);
1192 		if (argv[4])
1193 			env_set_hex(argv[4], mmc->ocr);
1194 		return CMD_RET_SUCCESS;
1195 	}
1196 	if (!strcmp(argv[2], "rca")) {
1197 		printf("RCA: 0x%08x\n", mmc->rca);
1198 		if (argv[4])
1199 			env_set_hex(argv[4], mmc->rca);
1200 		return CMD_RET_SUCCESS;
1201 	}
1202 	if (!strcmp(argv[2], "extcsd") &&
1203 	    mmc->version >= MMC_VERSION_4_41) {
1204 		ret = mmc_send_ext_csd(mmc, ext_csd);
1205 		if (ret)
1206 			return CMD_RET_FAILURE;
1207 		if (!strcmp(argv[3], "all")) {
1208 			/* Dump the entire register */
1209 			printf("EXT_CSD:");
1210 			for (i = 0; i < MMC_MAX_BLOCK_LEN; i++) {
1211 				if (!(i % 10))
1212 					printf("\n%03i: ", i);
1213 				printf(" %02x", ext_csd[i]);
1214 			}
1215 			printf("\n");
1216 			return CMD_RET_SUCCESS;
1217 		}
1218 		off = simple_strtoul(argv[3], NULL, 10);
1219 		if (off > 512)
1220 			return CMD_RET_USAGE;
1221 		printf("EXT_CSD[%i]: 0x%02x\n", off, ext_csd[off]);
1222 		if (argv[4])
1223 			env_set_hex(argv[4], ext_csd[off]);
1224 		return CMD_RET_SUCCESS;
1225 	}
1226 
1227 	return CMD_RET_FAILURE;
1228 }
1229 #endif
1230 
1231 static struct cmd_tbl cmd_mmc[] = {
1232 	U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
1233 	U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
1234 	U_BOOT_CMD_MKENT(wp, 2, 0, do_mmc_boot_wp, "", ""),
1235 #if CONFIG_IS_ENABLED(MMC_WRITE)
1236 	U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
1237 	U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
1238 #endif
1239 #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
1240 	U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
1241 #endif
1242 	U_BOOT_CMD_MKENT(rescan, 2, 1, do_mmc_rescan, "", ""),
1243 	U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
1244 	U_BOOT_CMD_MKENT(dev, 4, 0, do_mmc_dev, "", ""),
1245 	U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
1246 #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
1247 	U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
1248 #endif
1249 #ifdef CONFIG_SUPPORT_EMMC_BOOT
1250 	U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
1251 	U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
1252 	U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
1253 	U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
1254 #endif
1255 #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
1256 	U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
1257 #endif
1258 	U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
1259 #ifdef CONFIG_CMD_BKOPS_ENABLE
1260 	U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
1261 	U_BOOT_CMD_MKENT(bkops, 4, 0, do_mmc_bkops, "", ""),
1262 #endif
1263 #if CONFIG_IS_ENABLED(CMD_MMC_REG)
1264 	U_BOOT_CMD_MKENT(reg, 5, 0, do_mmc_reg, "", ""),
1265 #endif
1266 };
1267 
do_mmcops(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])1268 static int do_mmcops(struct cmd_tbl *cmdtp, int flag, int argc,
1269 		     char *const argv[])
1270 {
1271 	struct cmd_tbl *cp;
1272 
1273 	cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
1274 
1275 	/* Drop the mmc command */
1276 	argc--;
1277 	argv++;
1278 
1279 	if (cp == NULL || argc > cp->maxargs)
1280 		return CMD_RET_USAGE;
1281 	if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
1282 		return CMD_RET_SUCCESS;
1283 
1284 	if (curr_device < 0) {
1285 		if (get_mmc_num() > 0) {
1286 			curr_device = 0;
1287 		} else {
1288 			puts("No MMC device available\n");
1289 			return CMD_RET_FAILURE;
1290 		}
1291 	}
1292 	return cp->cmd(cmdtp, flag, argc, argv);
1293 }
1294 
1295 U_BOOT_CMD(
1296 	mmc, 29, 1, do_mmcops,
1297 	"MMC sub system",
1298 	"info - display info of the current MMC device\n"
1299 	"mmc read addr blk# cnt\n"
1300 	"mmc write addr blk# cnt\n"
1301 #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
1302 	"mmc swrite addr blk#\n"
1303 #endif
1304 	"mmc erase blk# cnt\n"
1305 	"mmc erase partname\n"
1306 	"mmc rescan [mode]\n"
1307 	"mmc part - lists available partition on current mmc device\n"
1308 	"mmc dev [dev] [part] [mode] - show or set current mmc device [partition] and set mode\n"
1309 	"  - the required speed mode is passed as the index from the following list\n"
1310 	"    [MMC_LEGACY, MMC_HS, SD_HS, MMC_HS_52, MMC_DDR_52, UHS_SDR12, UHS_SDR25,\n"
1311 	"    UHS_SDR50, UHS_DDR50, UHS_SDR104, MMC_HS_200, MMC_HS_400, MMC_HS_400_ES]\n"
1312 	"mmc list - lists available devices\n"
1313 	"mmc wp [PART] - power on write protect boot partitions\n"
1314 	"  arguments:\n"
1315 	"   PART - [0|1]\n"
1316 	"       : 0 - first boot partition, 1 - second boot partition\n"
1317 	"         if not assigned, write protect all boot partitions\n"
1318 #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
1319 	"mmc hwpartition <USER> <GP> <MODE> - does hardware partitioning\n"
1320 	"  arguments (sizes in 512-byte blocks):\n"
1321 	"   USER - <user> <enh> <start> <cnt> <wrrel> <{on|off}>\n"
1322 	"	: sets user data area attributes\n"
1323 	"   GP - <{gp1|gp2|gp3|gp4}> <cnt> <enh> <wrrel> <{on|off}>\n"
1324 	"	: general purpose partition\n"
1325 	"   MODE - <{check|set|complete}>\n"
1326 	"	: mode, complete set partitioning completed\n"
1327 	"  WARNING: Partitioning is a write-once setting once it is set to complete.\n"
1328 	"  Power cycling is required to initialize partitions after set to complete.\n"
1329 #endif
1330 #ifdef CONFIG_SUPPORT_EMMC_BOOT
1331 	"mmc bootbus <dev> <boot_bus_width> <reset_boot_bus_width> <boot_mode>\n"
1332 	" - Set the BOOT_BUS_WIDTH field of the specified device\n"
1333 	"mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
1334 	" - Change sizes of boot and RPMB partitions of specified device\n"
1335 	"mmc partconf <dev> [[varname] | [<boot_ack> <boot_partition> <partition_access>]]\n"
1336 	" - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
1337 	"   If showing the bits, optionally store the boot_partition field into varname\n"
1338 	"mmc rst-function <dev> <value>\n"
1339 	" - Change the RST_n_FUNCTION field of the specified device\n"
1340 	"   WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
1341 #endif
1342 #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
1343 	"mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
1344 	"mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
1345 	"mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
1346 	"mmc rpmb counter - read the value of the write counter\n"
1347 #endif
1348 	"mmc setdsr <value> - set DSR register value\n"
1349 #ifdef CONFIG_CMD_BKOPS_ENABLE
1350 	"mmc bkops-enable <dev> - enable background operations handshake on device\n"
1351 	"   WARNING: This is a write-once setting.\n"
1352 	"mmc bkops <dev> [auto|manual] [enable|disable]\n"
1353 	" - configure background operations handshake on device\n"
1354 #endif
1355 #if CONFIG_IS_ENABLED(CMD_MMC_REG)
1356 	"mmc reg read <reg> <offset> [env] - read card register <reg> offset <offset>\n"
1357 	"                                    (optionally into [env] variable)\n"
1358 	" - reg: cid/csd/dsr/ocr/rca/extcsd\n"
1359 	" - offset: for cid/csd [0..3], for extcsd [0..511,all]\n"
1360 #endif
1361 	);
1362 
1363 /* Old command kept for compatibility. Same as 'mmc info' */
1364 U_BOOT_CMD(
1365 	mmcinfo, 1, 0, do_mmcinfo,
1366 	"display MMC info",
1367 	"- display info of the current MMC device"
1368 );
1369