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)
98 mmc = find_mmc_device(dev);
100 printf("no mmc device at slot %x\n", dev);
107 static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
111 if (curr_device < 0) {
112 if (get_mmc_num() > 0)
115 puts("No MMC device available\n");
120 mmc = init_mmc_device(curr_device);
122 return CMD_RET_FAILURE;
125 return CMD_RET_SUCCESS;
128 #ifdef CONFIG_SUPPORT_EMMC_RPMB
129 static int confirm_key_prog(void)
131 puts("Warning: Programming authentication key can be done only once !\n"
132 " Use this command only if you are sure of what you are doing,\n"
133 "Really perform the key programming? <y/N> ");
137 puts("Authentication key programming aborted\n");
140 static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag,
141 int argc, char * const argv[])
144 struct mmc *mmc = find_mmc_device(curr_device);
147 return CMD_RET_USAGE;
149 key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
150 if (!confirm_key_prog())
151 return CMD_RET_FAILURE;
152 if (mmc_rpmb_set_key(mmc, key_addr)) {
153 printf("ERROR - Key already programmed ?\n");
154 return CMD_RET_FAILURE;
156 return CMD_RET_SUCCESS;
158 static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag,
159 int argc, char * const argv[])
164 void *key_addr = NULL;
165 struct mmc *mmc = find_mmc_device(curr_device);
168 return CMD_RET_USAGE;
170 addr = (void *)simple_strtoul(argv[1], NULL, 16);
171 blk = simple_strtoul(argv[2], NULL, 16);
172 cnt = simple_strtoul(argv[3], NULL, 16);
175 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
177 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
178 curr_device, blk, cnt);
179 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
181 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
183 return CMD_RET_FAILURE;
184 return CMD_RET_SUCCESS;
186 static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag,
187 int argc, char * const argv[])
193 struct mmc *mmc = find_mmc_device(curr_device);
196 return CMD_RET_USAGE;
198 addr = (void *)simple_strtoul(argv[1], NULL, 16);
199 blk = simple_strtoul(argv[2], NULL, 16);
200 cnt = simple_strtoul(argv[3], NULL, 16);
201 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
203 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
204 curr_device, blk, cnt);
205 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
207 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
209 return CMD_RET_FAILURE;
210 return CMD_RET_SUCCESS;
212 static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag,
213 int argc, char * const argv[])
215 unsigned long counter;
216 struct mmc *mmc = find_mmc_device(curr_device);
218 if (mmc_rpmb_get_counter(mmc, &counter))
219 return CMD_RET_FAILURE;
220 printf("RPMB Write counter= %lx\n", counter);
221 return CMD_RET_SUCCESS;
224 static cmd_tbl_t cmd_rpmb[] = {
225 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
226 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
227 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
228 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
231 static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag,
232 int argc, char * const argv[])
239 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
241 /* Drop the rpmb subcommand */
245 if (cp == NULL || argc > cp->maxargs)
246 return CMD_RET_USAGE;
247 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
248 return CMD_RET_SUCCESS;
250 mmc = init_mmc_device(curr_device);
252 return CMD_RET_FAILURE;
254 if (!(mmc->version & MMC_VERSION_MMC)) {
255 printf("It is not a EMMC device\n");
256 return CMD_RET_FAILURE;
258 if (mmc->version < MMC_VERSION_4_41) {
259 printf("RPMB not supported before version 4.41\n");
260 return CMD_RET_FAILURE;
262 /* Switch to the RPMB partition */
263 original_part = mmc->part_num;
264 if (mmc->part_num != MMC_PART_RPMB) {
265 if (mmc_switch_part(curr_device, MMC_PART_RPMB) != 0)
266 return CMD_RET_FAILURE;
267 mmc->part_num = MMC_PART_RPMB;
269 ret = cp->cmd(cmdtp, flag, argc, argv);
271 /* Return to original partition */
272 if (mmc->part_num != original_part) {
273 if (mmc_switch_part(curr_device, original_part) != 0)
274 return CMD_RET_FAILURE;
275 mmc->part_num = original_part;
281 static int do_mmc_read(cmd_tbl_t *cmdtp, int flag,
282 int argc, char * const argv[])
289 return CMD_RET_USAGE;
291 addr = (void *)simple_strtoul(argv[1], NULL, 16);
292 blk = simple_strtoul(argv[2], NULL, 16);
293 cnt = simple_strtoul(argv[3], NULL, 16);
295 mmc = init_mmc_device(curr_device);
297 return CMD_RET_FAILURE;
299 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
300 curr_device, blk, cnt);
302 n = mmc->block_dev.block_read(curr_device, blk, cnt, addr);
303 /* flush cache after read */
304 flush_cache((ulong)addr, cnt * 512); /* FIXME */
305 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
307 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
309 static int do_mmc_write(cmd_tbl_t *cmdtp, int flag,
310 int argc, char * const argv[])
317 return CMD_RET_USAGE;
319 addr = (void *)simple_strtoul(argv[1], NULL, 16);
320 blk = simple_strtoul(argv[2], NULL, 16);
321 cnt = simple_strtoul(argv[3], NULL, 16);
323 mmc = init_mmc_device(curr_device);
325 return CMD_RET_FAILURE;
327 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
328 curr_device, blk, cnt);
330 if (mmc_getwp(mmc) == 1) {
331 printf("Error: card is write protected!\n");
332 return CMD_RET_FAILURE;
334 n = mmc->block_dev.block_write(curr_device, blk, cnt, addr);
335 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
337 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
339 static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,
340 int argc, char * const argv[])
346 return CMD_RET_USAGE;
348 blk = simple_strtoul(argv[1], NULL, 16);
349 cnt = simple_strtoul(argv[2], NULL, 16);
351 mmc = init_mmc_device(curr_device);
353 return CMD_RET_FAILURE;
355 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
356 curr_device, blk, cnt);
358 if (mmc_getwp(mmc) == 1) {
359 printf("Error: card is write protected!\n");
360 return CMD_RET_FAILURE;
362 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
363 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
365 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
367 static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
368 int argc, char * const argv[])
372 mmc = find_mmc_device(curr_device);
374 printf("no mmc device at slot %x\n", curr_device);
375 return CMD_RET_FAILURE;
381 return CMD_RET_FAILURE;
382 return CMD_RET_SUCCESS;
384 static int do_mmc_part(cmd_tbl_t *cmdtp, int flag,
385 int argc, char * const argv[])
387 block_dev_desc_t *mmc_dev;
390 mmc = init_mmc_device(curr_device);
392 return CMD_RET_FAILURE;
394 mmc_dev = mmc_get_dev(curr_device);
395 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
397 return CMD_RET_SUCCESS;
400 puts("get mmc type error!\n");
401 return CMD_RET_FAILURE;
403 static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag,
404 int argc, char * const argv[])
411 } else if (argc == 2) {
412 dev = simple_strtoul(argv[1], NULL, 10);
413 } else if (argc == 3) {
414 dev = (int)simple_strtoul(argv[1], NULL, 10);
415 part = (int)simple_strtoul(argv[2], NULL, 10);
416 if (part > PART_ACCESS_MASK) {
417 printf("#part_num shouldn't be larger than %d\n",
419 return CMD_RET_FAILURE;
422 return CMD_RET_USAGE;
425 mmc = init_mmc_device(dev);
427 return CMD_RET_FAILURE;
431 if (mmc->part_config == MMCPART_NOAVAILABLE) {
432 printf("Card doesn't support part_switch\n");
433 return CMD_RET_FAILURE;
436 if (part != mmc->part_num) {
437 ret = mmc_switch_part(dev, part);
439 mmc->part_num = part;
441 printf("switch to partitions #%d, %s\n",
442 part, (!ret) ? "OK" : "ERROR");
446 if (mmc->part_config == MMCPART_NOAVAILABLE)
447 printf("mmc%d is current device\n", curr_device);
449 printf("mmc%d(part %d) is current device\n",
450 curr_device, mmc->part_num);
452 return CMD_RET_SUCCESS;
454 static int do_mmc_list(cmd_tbl_t *cmdtp, int flag,
455 int argc, char * const argv[])
457 print_mmc_devices('\n');
458 return CMD_RET_SUCCESS;
460 #ifdef CONFIG_SUPPORT_EMMC_BOOT
461 static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag,
462 int argc, char * const argv[])
466 u8 width, reset, mode;
469 return CMD_RET_USAGE;
470 dev = simple_strtoul(argv[1], NULL, 10);
471 width = simple_strtoul(argv[2], NULL, 10);
472 reset = simple_strtoul(argv[3], NULL, 10);
473 mode = simple_strtoul(argv[4], NULL, 10);
475 mmc = init_mmc_device(dev);
477 return CMD_RET_FAILURE;
480 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
481 return CMD_RET_FAILURE;
484 /* acknowledge to be sent during boot operation */
485 return mmc_set_boot_bus_width(mmc, width, reset, mode);
487 static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag,
488 int argc, char * const argv[])
492 u32 bootsize, rpmbsize;
495 return CMD_RET_USAGE;
496 dev = simple_strtoul(argv[1], NULL, 10);
497 bootsize = simple_strtoul(argv[2], NULL, 10);
498 rpmbsize = simple_strtoul(argv[3], NULL, 10);
500 mmc = init_mmc_device(dev);
502 return CMD_RET_FAILURE;
505 printf("It is not a EMMC device\n");
506 return CMD_RET_FAILURE;
509 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
510 printf("EMMC boot partition Size change Failed.\n");
511 return CMD_RET_FAILURE;
514 printf("EMMC boot partition Size %d MB\n", bootsize);
515 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
516 return CMD_RET_SUCCESS;
518 static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag,
519 int argc, char * const argv[])
523 u8 ack, part_num, access;
526 return CMD_RET_USAGE;
528 dev = simple_strtoul(argv[1], NULL, 10);
529 ack = simple_strtoul(argv[2], NULL, 10);
530 part_num = simple_strtoul(argv[3], NULL, 10);
531 access = simple_strtoul(argv[4], NULL, 10);
533 mmc = init_mmc_device(dev);
535 return CMD_RET_FAILURE;
538 puts("PARTITION_CONFIG only exists on eMMC\n");
539 return CMD_RET_FAILURE;
542 /* acknowledge to be sent during boot operation */
543 return mmc_set_part_conf(mmc, ack, part_num, access);
545 static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag,
546 int argc, char * const argv[])
553 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
554 * The only valid values are 0x0, 0x1 and 0x2 and writing
555 * a value of 0x1 or 0x2 sets the value permanently.
558 return CMD_RET_USAGE;
560 dev = simple_strtoul(argv[1], NULL, 10);
561 enable = simple_strtoul(argv[2], NULL, 10);
563 if (enable > 2 || enable < 0) {
564 puts("Invalid RST_n_ENABLE value\n");
565 return CMD_RET_USAGE;
568 mmc = init_mmc_device(dev);
570 return CMD_RET_FAILURE;
573 puts("RST_n_FUNCTION only exists on eMMC\n");
574 return CMD_RET_FAILURE;
577 return mmc_set_rst_n_function(mmc, enable);
580 static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag,
581 int argc, char * const argv[])
588 return CMD_RET_USAGE;
589 val = simple_strtoul(argv[2], NULL, 16);
591 mmc = find_mmc_device(curr_device);
593 printf("no mmc device at slot %x\n", curr_device);
594 return CMD_RET_FAILURE;
596 ret = mmc_set_dsr(mmc, val);
597 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
601 return CMD_RET_FAILURE;
603 return CMD_RET_SUCCESS;
608 static cmd_tbl_t cmd_mmc[] = {
609 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
610 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
611 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
612 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
613 U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
614 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
615 U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
616 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
617 #ifdef CONFIG_SUPPORT_EMMC_BOOT
618 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
619 U_BOOT_CMD_MKENT(bootpart-resize, 3, 0, do_mmc_boot_resize, "", ""),
620 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
621 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
623 #ifdef CONFIG_SUPPORT_EMMC_RPMB
624 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
626 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
629 static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
633 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
635 /* Drop the mmc command */
639 if (cp == NULL || argc > cp->maxargs)
640 return CMD_RET_USAGE;
641 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
642 return CMD_RET_SUCCESS;
644 if (curr_device < 0) {
645 if (get_mmc_num() > 0) {
648 puts("No MMC device available\n");
649 return CMD_RET_FAILURE;
652 return cp->cmd(cmdtp, flag, argc, argv);
656 mmc, 7, 1, do_mmcops,
658 "info - display info of the current MMC device\n"
659 "mmc read addr blk# cnt\n"
660 "mmc write addr blk# cnt\n"
661 "mmc erase blk# cnt\n"
663 "mmc part - lists available partition on current mmc device\n"
664 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
665 "mmc list - lists available devices\n"
666 #ifdef CONFIG_SUPPORT_EMMC_BOOT
667 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
668 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
669 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
670 " - Change sizes of boot and RPMB partitions of specified device\n"
671 "mmc partconf dev boot_ack boot_partition partition_access\n"
672 " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
673 "mmc rst-function dev value\n"
674 " - Change the RST_n_FUNCTION field of the specified device\n"
675 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
677 #ifdef CONFIG_SUPPORT_EMMC_RPMB
678 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
679 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
680 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
681 "mmc rpmb counter - read the value of the write counter\n"
683 "mmc setdsr <value> - set DSR register value\n"
686 /* Old command kept for compatibility. Same as 'mmc info' */
688 mmcinfo, 1, 0, do_mmcinfo,
690 "- display info of the current MMC device"
693 #endif /* !CONFIG_GENERIC_MMC */