3 * Kyle Harris, kharris@nexus-tech.net
5 * SPDX-License-Identifier: GPL-2.0+
12 static int curr_device = -1;
13 #ifndef CONFIG_GENERIC_MMC
14 int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
21 if (strcmp(argv[1], "init") == 0) {
27 } else if (argc == 3) {
28 dev = (int)simple_strtoul(argv[2], NULL, 10);
33 if (mmc_legacy_init(dev) != 0) {
34 puts("No MMC card found\n");
39 printf("mmc%d is available\n", curr_device);
40 } else if (strcmp(argv[1], "device") == 0) {
42 if (curr_device < 0) {
43 puts("No MMC device available\n");
46 } else if (argc == 3) {
47 dev = (int)simple_strtoul(argv[2], NULL, 10);
49 #ifdef CONFIG_SYS_MMC_SET_DEV
50 if (mmc_set_dev(dev) != 0)
58 printf("mmc%d is current device\n", curr_device);
69 "init [dev] - init MMC sub system\n"
70 "mmc device [dev] - show or set current device"
72 #else /* !CONFIG_GENERIC_MMC */
74 static void print_mmcinfo(struct mmc *mmc)
76 printf("Device: %s\n", mmc->cfg->name);
77 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
78 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
79 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
80 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
81 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
83 printf("Tran Speed: %d\n", mmc->tran_speed);
84 printf("Rd Block Len: %d\n", mmc->read_bl_len);
86 printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
87 (mmc->version >> 8) & 0xf, mmc->version & 0xff);
89 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
91 print_size(mmc->capacity, "\n");
93 printf("Bus Width: %d-bit\n", mmc->bus_width);
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);
109 static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
113 if (curr_device < 0) {
114 if (get_mmc_num() > 0)
117 puts("No MMC device available\n");
122 mmc = init_mmc_device(curr_device, false);
124 return CMD_RET_FAILURE;
127 return CMD_RET_SUCCESS;
130 #ifdef CONFIG_SUPPORT_EMMC_RPMB
131 static int confirm_key_prog(void)
133 puts("Warning: Programming authentication key can be done only once !\n"
134 " Use this command only if you are sure of what you are doing,\n"
135 "Really perform the key programming? <y/N> ");
139 puts("Authentication key programming aborted\n");
142 static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag,
143 int argc, char * const argv[])
146 struct mmc *mmc = find_mmc_device(curr_device);
149 return CMD_RET_USAGE;
151 key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
152 if (!confirm_key_prog())
153 return CMD_RET_FAILURE;
154 if (mmc_rpmb_set_key(mmc, key_addr)) {
155 printf("ERROR - Key already programmed ?\n");
156 return CMD_RET_FAILURE;
158 return CMD_RET_SUCCESS;
160 static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag,
161 int argc, char * const argv[])
166 void *key_addr = NULL;
167 struct mmc *mmc = find_mmc_device(curr_device);
170 return CMD_RET_USAGE;
172 addr = (void *)simple_strtoul(argv[1], NULL, 16);
173 blk = simple_strtoul(argv[2], NULL, 16);
174 cnt = simple_strtoul(argv[3], NULL, 16);
177 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
179 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
180 curr_device, blk, cnt);
181 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
183 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
185 return CMD_RET_FAILURE;
186 return CMD_RET_SUCCESS;
188 static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag,
189 int argc, char * const argv[])
195 struct mmc *mmc = find_mmc_device(curr_device);
198 return CMD_RET_USAGE;
200 addr = (void *)simple_strtoul(argv[1], NULL, 16);
201 blk = simple_strtoul(argv[2], NULL, 16);
202 cnt = simple_strtoul(argv[3], NULL, 16);
203 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
205 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
206 curr_device, blk, cnt);
207 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
209 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
211 return CMD_RET_FAILURE;
212 return CMD_RET_SUCCESS;
214 static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag,
215 int argc, char * const argv[])
217 unsigned long counter;
218 struct mmc *mmc = find_mmc_device(curr_device);
220 if (mmc_rpmb_get_counter(mmc, &counter))
221 return CMD_RET_FAILURE;
222 printf("RPMB Write counter= %lx\n", counter);
223 return CMD_RET_SUCCESS;
226 static cmd_tbl_t cmd_rpmb[] = {
227 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
228 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
229 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
230 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
233 static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag,
234 int argc, char * const argv[])
241 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
243 /* Drop the rpmb subcommand */
247 if (cp == NULL || argc > cp->maxargs)
248 return CMD_RET_USAGE;
249 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
250 return CMD_RET_SUCCESS;
252 mmc = init_mmc_device(curr_device, false);
254 return CMD_RET_FAILURE;
256 if (!(mmc->version & MMC_VERSION_MMC)) {
257 printf("It is not a EMMC device\n");
258 return CMD_RET_FAILURE;
260 if (mmc->version < MMC_VERSION_4_41) {
261 printf("RPMB not supported before version 4.41\n");
262 return CMD_RET_FAILURE;
264 /* Switch to the RPMB partition */
265 original_part = mmc->part_num;
266 if (mmc->part_num != MMC_PART_RPMB) {
267 if (mmc_switch_part(curr_device, MMC_PART_RPMB) != 0)
268 return CMD_RET_FAILURE;
269 mmc->part_num = MMC_PART_RPMB;
271 ret = cp->cmd(cmdtp, flag, argc, argv);
273 /* Return to original partition */
274 if (mmc->part_num != original_part) {
275 if (mmc_switch_part(curr_device, original_part) != 0)
276 return CMD_RET_FAILURE;
277 mmc->part_num = original_part;
283 static int do_mmc_read(cmd_tbl_t *cmdtp, int flag,
284 int argc, char * const argv[])
291 return CMD_RET_USAGE;
293 addr = (void *)simple_strtoul(argv[1], NULL, 16);
294 blk = simple_strtoul(argv[2], NULL, 16);
295 cnt = simple_strtoul(argv[3], NULL, 16);
297 mmc = init_mmc_device(curr_device, false);
299 return CMD_RET_FAILURE;
301 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
302 curr_device, blk, cnt);
304 n = mmc->block_dev.block_read(curr_device, blk, cnt, addr);
305 /* flush cache after read */
306 flush_cache((ulong)addr, cnt * 512); /* FIXME */
307 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
309 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
311 static int do_mmc_write(cmd_tbl_t *cmdtp, int flag,
312 int argc, char * const argv[])
319 return CMD_RET_USAGE;
321 addr = (void *)simple_strtoul(argv[1], NULL, 16);
322 blk = simple_strtoul(argv[2], NULL, 16);
323 cnt = simple_strtoul(argv[3], NULL, 16);
325 mmc = init_mmc_device(curr_device, false);
327 return CMD_RET_FAILURE;
329 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
330 curr_device, blk, cnt);
332 if (mmc_getwp(mmc) == 1) {
333 printf("Error: card is write protected!\n");
334 return CMD_RET_FAILURE;
336 n = mmc->block_dev.block_write(curr_device, blk, cnt, addr);
337 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
339 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
341 static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,
342 int argc, char * const argv[])
348 return CMD_RET_USAGE;
350 blk = simple_strtoul(argv[1], NULL, 16);
351 cnt = simple_strtoul(argv[2], NULL, 16);
353 mmc = init_mmc_device(curr_device, false);
355 return CMD_RET_FAILURE;
357 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
358 curr_device, blk, cnt);
360 if (mmc_getwp(mmc) == 1) {
361 printf("Error: card is write protected!\n");
362 return CMD_RET_FAILURE;
364 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
365 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
367 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
369 static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
370 int argc, char * const argv[])
374 mmc = find_mmc_device(curr_device);
376 printf("no mmc device at slot %x\n", curr_device);
377 return CMD_RET_FAILURE;
383 return CMD_RET_FAILURE;
384 return CMD_RET_SUCCESS;
386 static int do_mmc_part(cmd_tbl_t *cmdtp, int flag,
387 int argc, char * const argv[])
389 block_dev_desc_t *mmc_dev;
392 mmc = init_mmc_device(curr_device, false);
394 return CMD_RET_FAILURE;
396 mmc_dev = mmc_get_dev(curr_device);
397 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
399 return CMD_RET_SUCCESS;
402 puts("get mmc type error!\n");
403 return CMD_RET_FAILURE;
405 static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag,
406 int argc, char * const argv[])
408 int dev, part = 0, ret;
413 } else if (argc == 2) {
414 dev = simple_strtoul(argv[1], NULL, 10);
415 } else if (argc == 3) {
416 dev = (int)simple_strtoul(argv[1], NULL, 10);
417 part = (int)simple_strtoul(argv[2], NULL, 10);
418 if (part > PART_ACCESS_MASK) {
419 printf("#part_num shouldn't be larger than %d\n",
421 return CMD_RET_FAILURE;
424 return CMD_RET_USAGE;
427 mmc = init_mmc_device(dev, false);
429 return CMD_RET_FAILURE;
431 ret = mmc_select_hwpart(dev, part);
432 printf("switch to partitions #%d, %s\n",
433 part, (!ret) ? "OK" : "ERROR");
438 if (mmc->part_config == MMCPART_NOAVAILABLE)
439 printf("mmc%d is current device\n", curr_device);
441 printf("mmc%d(part %d) is current device\n",
442 curr_device, mmc->part_num);
444 return CMD_RET_SUCCESS;
446 static int do_mmc_list(cmd_tbl_t *cmdtp, int flag,
447 int argc, char * const argv[])
449 print_mmc_devices('\n');
450 return CMD_RET_SUCCESS;
452 #ifdef CONFIG_SUPPORT_EMMC_BOOT
453 static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag,
454 int argc, char * const argv[])
458 u8 width, reset, mode;
461 return CMD_RET_USAGE;
462 dev = simple_strtoul(argv[1], NULL, 10);
463 width = simple_strtoul(argv[2], NULL, 10);
464 reset = simple_strtoul(argv[3], NULL, 10);
465 mode = simple_strtoul(argv[4], NULL, 10);
467 mmc = init_mmc_device(dev, false);
469 return CMD_RET_FAILURE;
472 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
473 return CMD_RET_FAILURE;
476 /* acknowledge to be sent during boot operation */
477 return mmc_set_boot_bus_width(mmc, width, reset, mode);
479 static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag,
480 int argc, char * const argv[])
484 u32 bootsize, rpmbsize;
487 return CMD_RET_USAGE;
488 dev = simple_strtoul(argv[1], NULL, 10);
489 bootsize = simple_strtoul(argv[2], NULL, 10);
490 rpmbsize = simple_strtoul(argv[3], NULL, 10);
492 mmc = init_mmc_device(dev, false);
494 return CMD_RET_FAILURE;
497 printf("It is not a EMMC device\n");
498 return CMD_RET_FAILURE;
501 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
502 printf("EMMC boot partition Size change Failed.\n");
503 return CMD_RET_FAILURE;
506 printf("EMMC boot partition Size %d MB\n", bootsize);
507 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
508 return CMD_RET_SUCCESS;
510 static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag,
511 int argc, char * const argv[])
515 u8 ack, part_num, access;
518 return CMD_RET_USAGE;
520 dev = simple_strtoul(argv[1], NULL, 10);
521 ack = simple_strtoul(argv[2], NULL, 10);
522 part_num = simple_strtoul(argv[3], NULL, 10);
523 access = simple_strtoul(argv[4], NULL, 10);
525 mmc = init_mmc_device(dev, false);
527 return CMD_RET_FAILURE;
530 puts("PARTITION_CONFIG only exists on eMMC\n");
531 return CMD_RET_FAILURE;
534 /* acknowledge to be sent during boot operation */
535 return mmc_set_part_conf(mmc, ack, part_num, access);
537 static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag,
538 int argc, char * const argv[])
545 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
546 * The only valid values are 0x0, 0x1 and 0x2 and writing
547 * a value of 0x1 or 0x2 sets the value permanently.
550 return CMD_RET_USAGE;
552 dev = simple_strtoul(argv[1], NULL, 10);
553 enable = simple_strtoul(argv[2], NULL, 10);
555 if (enable > 2 || enable < 0) {
556 puts("Invalid RST_n_ENABLE value\n");
557 return CMD_RET_USAGE;
560 mmc = init_mmc_device(dev, false);
562 return CMD_RET_FAILURE;
565 puts("RST_n_FUNCTION only exists on eMMC\n");
566 return CMD_RET_FAILURE;
569 return mmc_set_rst_n_function(mmc, enable);
572 static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag,
573 int argc, char * const argv[])
580 return CMD_RET_USAGE;
581 val = simple_strtoul(argv[2], NULL, 16);
583 mmc = find_mmc_device(curr_device);
585 printf("no mmc device at slot %x\n", curr_device);
586 return CMD_RET_FAILURE;
588 ret = mmc_set_dsr(mmc, val);
589 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
593 return CMD_RET_FAILURE;
595 return CMD_RET_SUCCESS;
600 static cmd_tbl_t cmd_mmc[] = {
601 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
602 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
603 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
604 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
605 U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
606 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
607 U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
608 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
609 #ifdef CONFIG_SUPPORT_EMMC_BOOT
610 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
611 U_BOOT_CMD_MKENT(bootpart-resize, 3, 0, do_mmc_boot_resize, "", ""),
612 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
613 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
615 #ifdef CONFIG_SUPPORT_EMMC_RPMB
616 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
618 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
621 static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
625 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
627 /* Drop the mmc command */
631 if (cp == NULL || argc > cp->maxargs)
632 return CMD_RET_USAGE;
633 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
634 return CMD_RET_SUCCESS;
636 if (curr_device < 0) {
637 if (get_mmc_num() > 0) {
640 puts("No MMC device available\n");
641 return CMD_RET_FAILURE;
644 return cp->cmd(cmdtp, flag, argc, argv);
648 mmc, 7, 1, do_mmcops,
650 "info - display info of the current MMC device\n"
651 "mmc read addr blk# cnt\n"
652 "mmc write addr blk# cnt\n"
653 "mmc erase blk# cnt\n"
655 "mmc part - lists available partition on current mmc device\n"
656 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
657 "mmc list - lists available devices\n"
658 #ifdef CONFIG_SUPPORT_EMMC_BOOT
659 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
660 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
661 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
662 " - Change sizes of boot and RPMB partitions of specified device\n"
663 "mmc partconf dev boot_ack boot_partition partition_access\n"
664 " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
665 "mmc rst-function dev value\n"
666 " - Change the RST_n_FUNCTION field of the specified device\n"
667 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
669 #ifdef CONFIG_SUPPORT_EMMC_RPMB
670 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
671 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
672 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
673 "mmc rpmb counter - read the value of the write counter\n"
675 "mmc setdsr <value> - set DSR register value\n"
678 /* Old command kept for compatibility. Same as 'mmc info' */
680 mmcinfo, 1, 0, do_mmcinfo,
682 "- display info of the current MMC device"
685 #endif /* !CONFIG_GENERIC_MMC */