]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/cmd_mmc.c
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%s\n", mmc
->bus_width
,
94 mmc
->ddr_mode
? " DDR" : "");
96 static struct mmc
*init_mmc_device(int dev
, bool force_init
)
99 mmc
= find_mmc_device(dev
);
101 printf("no mmc device at slot %x\n", dev
);
110 static int do_mmcinfo(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
114 if (curr_device
< 0) {
115 if (get_mmc_num() > 0)
118 puts("No MMC device available\n");
123 mmc
= init_mmc_device(curr_device
, false);
125 return CMD_RET_FAILURE
;
128 return CMD_RET_SUCCESS
;
131 #ifdef CONFIG_SUPPORT_EMMC_RPMB
132 static int confirm_key_prog(void)
134 puts("Warning: Programming authentication key can be done only once !\n"
135 " Use this command only if you are sure of what you are doing,\n"
136 "Really perform the key programming? <y/N> ");
140 puts("Authentication key programming aborted\n");
143 static int do_mmcrpmb_key(cmd_tbl_t
*cmdtp
, int flag
,
144 int argc
, char * const argv
[])
147 struct mmc
*mmc
= find_mmc_device(curr_device
);
150 return CMD_RET_USAGE
;
152 key_addr
= (void *)simple_strtoul(argv
[1], NULL
, 16);
153 if (!confirm_key_prog())
154 return CMD_RET_FAILURE
;
155 if (mmc_rpmb_set_key(mmc
, key_addr
)) {
156 printf("ERROR - Key already programmed ?\n");
157 return CMD_RET_FAILURE
;
159 return CMD_RET_SUCCESS
;
161 static int do_mmcrpmb_read(cmd_tbl_t
*cmdtp
, int flag
,
162 int argc
, char * const argv
[])
167 void *key_addr
= NULL
;
168 struct mmc
*mmc
= find_mmc_device(curr_device
);
171 return CMD_RET_USAGE
;
173 addr
= (void *)simple_strtoul(argv
[1], NULL
, 16);
174 blk
= simple_strtoul(argv
[2], NULL
, 16);
175 cnt
= simple_strtoul(argv
[3], NULL
, 16);
178 key_addr
= (void *)simple_strtoul(argv
[4], NULL
, 16);
180 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
181 curr_device
, blk
, cnt
);
182 n
= mmc_rpmb_read(mmc
, addr
, blk
, cnt
, key_addr
);
184 printf("%d RPMB blocks read: %s\n", n
, (n
== cnt
) ? "OK" : "ERROR");
186 return CMD_RET_FAILURE
;
187 return CMD_RET_SUCCESS
;
189 static int do_mmcrpmb_write(cmd_tbl_t
*cmdtp
, int flag
,
190 int argc
, char * const argv
[])
196 struct mmc
*mmc
= find_mmc_device(curr_device
);
199 return CMD_RET_USAGE
;
201 addr
= (void *)simple_strtoul(argv
[1], NULL
, 16);
202 blk
= simple_strtoul(argv
[2], NULL
, 16);
203 cnt
= simple_strtoul(argv
[3], NULL
, 16);
204 key_addr
= (void *)simple_strtoul(argv
[4], NULL
, 16);
206 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
207 curr_device
, blk
, cnt
);
208 n
= mmc_rpmb_write(mmc
, addr
, blk
, cnt
, key_addr
);
210 printf("%d RPMB blocks written: %s\n", n
, (n
== cnt
) ? "OK" : "ERROR");
212 return CMD_RET_FAILURE
;
213 return CMD_RET_SUCCESS
;
215 static int do_mmcrpmb_counter(cmd_tbl_t
*cmdtp
, int flag
,
216 int argc
, char * const argv
[])
218 unsigned long counter
;
219 struct mmc
*mmc
= find_mmc_device(curr_device
);
221 if (mmc_rpmb_get_counter(mmc
, &counter
))
222 return CMD_RET_FAILURE
;
223 printf("RPMB Write counter= %lx\n", counter
);
224 return CMD_RET_SUCCESS
;
227 static cmd_tbl_t cmd_rpmb
[] = {
228 U_BOOT_CMD_MKENT(key
, 2, 0, do_mmcrpmb_key
, "", ""),
229 U_BOOT_CMD_MKENT(read
, 5, 1, do_mmcrpmb_read
, "", ""),
230 U_BOOT_CMD_MKENT(write
, 5, 0, do_mmcrpmb_write
, "", ""),
231 U_BOOT_CMD_MKENT(counter
, 1, 1, do_mmcrpmb_counter
, "", ""),
234 static int do_mmcrpmb(cmd_tbl_t
*cmdtp
, int flag
,
235 int argc
, char * const argv
[])
242 cp
= find_cmd_tbl(argv
[1], cmd_rpmb
, ARRAY_SIZE(cmd_rpmb
));
244 /* Drop the rpmb subcommand */
248 if (cp
== NULL
|| argc
> cp
->maxargs
)
249 return CMD_RET_USAGE
;
250 if (flag
== CMD_FLAG_REPEAT
&& !cp
->repeatable
)
251 return CMD_RET_SUCCESS
;
253 mmc
= init_mmc_device(curr_device
, false);
255 return CMD_RET_FAILURE
;
257 if (!(mmc
->version
& MMC_VERSION_MMC
)) {
258 printf("It is not a EMMC device\n");
259 return CMD_RET_FAILURE
;
261 if (mmc
->version
< MMC_VERSION_4_41
) {
262 printf("RPMB not supported before version 4.41\n");
263 return CMD_RET_FAILURE
;
265 /* Switch to the RPMB partition */
266 original_part
= mmc
->part_num
;
267 if (mmc
->part_num
!= MMC_PART_RPMB
) {
268 if (mmc_switch_part(curr_device
, MMC_PART_RPMB
) != 0)
269 return CMD_RET_FAILURE
;
270 mmc
->part_num
= MMC_PART_RPMB
;
272 ret
= cp
->cmd(cmdtp
, flag
, argc
, argv
);
274 /* Return to original partition */
275 if (mmc
->part_num
!= original_part
) {
276 if (mmc_switch_part(curr_device
, original_part
) != 0)
277 return CMD_RET_FAILURE
;
278 mmc
->part_num
= original_part
;
284 static int do_mmc_read(cmd_tbl_t
*cmdtp
, int flag
,
285 int argc
, char * const argv
[])
292 return CMD_RET_USAGE
;
294 addr
= (void *)simple_strtoul(argv
[1], NULL
, 16);
295 blk
= simple_strtoul(argv
[2], NULL
, 16);
296 cnt
= simple_strtoul(argv
[3], NULL
, 16);
298 mmc
= init_mmc_device(curr_device
, false);
300 return CMD_RET_FAILURE
;
302 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
303 curr_device
, blk
, cnt
);
305 n
= mmc
->block_dev
.block_read(curr_device
, blk
, cnt
, addr
);
306 /* flush cache after read */
307 flush_cache((ulong
)addr
, cnt
* 512); /* FIXME */
308 printf("%d blocks read: %s\n", n
, (n
== cnt
) ? "OK" : "ERROR");
310 return (n
== cnt
) ? CMD_RET_SUCCESS
: CMD_RET_FAILURE
;
312 static int do_mmc_write(cmd_tbl_t
*cmdtp
, int flag
,
313 int argc
, char * const argv
[])
320 return CMD_RET_USAGE
;
322 addr
= (void *)simple_strtoul(argv
[1], NULL
, 16);
323 blk
= simple_strtoul(argv
[2], NULL
, 16);
324 cnt
= simple_strtoul(argv
[3], NULL
, 16);
326 mmc
= init_mmc_device(curr_device
, false);
328 return CMD_RET_FAILURE
;
330 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
331 curr_device
, blk
, cnt
);
333 if (mmc_getwp(mmc
) == 1) {
334 printf("Error: card is write protected!\n");
335 return CMD_RET_FAILURE
;
337 n
= mmc
->block_dev
.block_write(curr_device
, blk
, cnt
, addr
);
338 printf("%d blocks written: %s\n", n
, (n
== cnt
) ? "OK" : "ERROR");
340 return (n
== cnt
) ? CMD_RET_SUCCESS
: CMD_RET_FAILURE
;
342 static int do_mmc_erase(cmd_tbl_t
*cmdtp
, int flag
,
343 int argc
, char * const argv
[])
349 return CMD_RET_USAGE
;
351 blk
= simple_strtoul(argv
[1], NULL
, 16);
352 cnt
= simple_strtoul(argv
[2], NULL
, 16);
354 mmc
= init_mmc_device(curr_device
, false);
356 return CMD_RET_FAILURE
;
358 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
359 curr_device
, blk
, cnt
);
361 if (mmc_getwp(mmc
) == 1) {
362 printf("Error: card is write protected!\n");
363 return CMD_RET_FAILURE
;
365 n
= mmc
->block_dev
.block_erase(curr_device
, blk
, cnt
);
366 printf("%d blocks erased: %s\n", n
, (n
== cnt
) ? "OK" : "ERROR");
368 return (n
== cnt
) ? CMD_RET_SUCCESS
: CMD_RET_FAILURE
;
370 static int do_mmc_rescan(cmd_tbl_t
*cmdtp
, int flag
,
371 int argc
, char * const argv
[])
375 mmc
= init_mmc_device(curr_device
, true);
377 return CMD_RET_FAILURE
;
379 return CMD_RET_SUCCESS
;
381 static int do_mmc_part(cmd_tbl_t
*cmdtp
, int flag
,
382 int argc
, char * const argv
[])
384 block_dev_desc_t
*mmc_dev
;
387 mmc
= init_mmc_device(curr_device
, false);
389 return CMD_RET_FAILURE
;
391 mmc_dev
= mmc_get_dev(curr_device
);
392 if (mmc_dev
!= NULL
&& mmc_dev
->type
!= DEV_TYPE_UNKNOWN
) {
394 return CMD_RET_SUCCESS
;
397 puts("get mmc type error!\n");
398 return CMD_RET_FAILURE
;
400 static int do_mmc_dev(cmd_tbl_t
*cmdtp
, int flag
,
401 int argc
, char * const argv
[])
403 int dev
, part
= 0, ret
;
408 } else if (argc
== 2) {
409 dev
= simple_strtoul(argv
[1], NULL
, 10);
410 } else if (argc
== 3) {
411 dev
= (int)simple_strtoul(argv
[1], NULL
, 10);
412 part
= (int)simple_strtoul(argv
[2], NULL
, 10);
413 if (part
> PART_ACCESS_MASK
) {
414 printf("#part_num shouldn't be larger than %d\n",
416 return CMD_RET_FAILURE
;
419 return CMD_RET_USAGE
;
422 mmc
= init_mmc_device(dev
, true);
424 return CMD_RET_FAILURE
;
426 ret
= mmc_select_hwpart(dev
, part
);
427 printf("switch to partitions #%d, %s\n",
428 part
, (!ret
) ? "OK" : "ERROR");
433 if (mmc
->part_config
== MMCPART_NOAVAILABLE
)
434 printf("mmc%d is current device\n", curr_device
);
436 printf("mmc%d(part %d) is current device\n",
437 curr_device
, mmc
->part_num
);
439 return CMD_RET_SUCCESS
;
441 static int do_mmc_list(cmd_tbl_t
*cmdtp
, int flag
,
442 int argc
, char * const argv
[])
444 print_mmc_devices('\n');
445 return CMD_RET_SUCCESS
;
447 #ifdef CONFIG_SUPPORT_EMMC_BOOT
448 static int do_mmc_bootbus(cmd_tbl_t
*cmdtp
, int flag
,
449 int argc
, char * const argv
[])
453 u8 width
, reset
, mode
;
456 return CMD_RET_USAGE
;
457 dev
= simple_strtoul(argv
[1], NULL
, 10);
458 width
= simple_strtoul(argv
[2], NULL
, 10);
459 reset
= simple_strtoul(argv
[3], NULL
, 10);
460 mode
= simple_strtoul(argv
[4], NULL
, 10);
462 mmc
= init_mmc_device(dev
, false);
464 return CMD_RET_FAILURE
;
467 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
468 return CMD_RET_FAILURE
;
471 /* acknowledge to be sent during boot operation */
472 return mmc_set_boot_bus_width(mmc
, width
, reset
, mode
);
474 static int do_mmc_boot_resize(cmd_tbl_t
*cmdtp
, int flag
,
475 int argc
, char * const argv
[])
479 u32 bootsize
, rpmbsize
;
482 return CMD_RET_USAGE
;
483 dev
= simple_strtoul(argv
[1], NULL
, 10);
484 bootsize
= simple_strtoul(argv
[2], NULL
, 10);
485 rpmbsize
= simple_strtoul(argv
[3], NULL
, 10);
487 mmc
= init_mmc_device(dev
, false);
489 return CMD_RET_FAILURE
;
492 printf("It is not a EMMC device\n");
493 return CMD_RET_FAILURE
;
496 if (mmc_boot_partition_size_change(mmc
, bootsize
, rpmbsize
)) {
497 printf("EMMC boot partition Size change Failed.\n");
498 return CMD_RET_FAILURE
;
501 printf("EMMC boot partition Size %d MB\n", bootsize
);
502 printf("EMMC RPMB partition Size %d MB\n", rpmbsize
);
503 return CMD_RET_SUCCESS
;
505 static int do_mmc_partconf(cmd_tbl_t
*cmdtp
, int flag
,
506 int argc
, char * const argv
[])
510 u8 ack
, part_num
, access
;
513 return CMD_RET_USAGE
;
515 dev
= simple_strtoul(argv
[1], NULL
, 10);
516 ack
= simple_strtoul(argv
[2], NULL
, 10);
517 part_num
= simple_strtoul(argv
[3], NULL
, 10);
518 access
= simple_strtoul(argv
[4], NULL
, 10);
520 mmc
= init_mmc_device(dev
, false);
522 return CMD_RET_FAILURE
;
525 puts("PARTITION_CONFIG only exists on eMMC\n");
526 return CMD_RET_FAILURE
;
529 /* acknowledge to be sent during boot operation */
530 return mmc_set_part_conf(mmc
, ack
, part_num
, access
);
532 static int do_mmc_rst_func(cmd_tbl_t
*cmdtp
, int flag
,
533 int argc
, char * const argv
[])
540 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
541 * The only valid values are 0x0, 0x1 and 0x2 and writing
542 * a value of 0x1 or 0x2 sets the value permanently.
545 return CMD_RET_USAGE
;
547 dev
= simple_strtoul(argv
[1], NULL
, 10);
548 enable
= simple_strtoul(argv
[2], NULL
, 10);
550 if (enable
> 2 || enable
< 0) {
551 puts("Invalid RST_n_ENABLE value\n");
552 return CMD_RET_USAGE
;
555 mmc
= init_mmc_device(dev
, false);
557 return CMD_RET_FAILURE
;
560 puts("RST_n_FUNCTION only exists on eMMC\n");
561 return CMD_RET_FAILURE
;
564 return mmc_set_rst_n_function(mmc
, enable
);
567 static int do_mmc_setdsr(cmd_tbl_t
*cmdtp
, int flag
,
568 int argc
, char * const argv
[])
575 return CMD_RET_USAGE
;
576 val
= simple_strtoul(argv
[2], NULL
, 16);
578 mmc
= find_mmc_device(curr_device
);
580 printf("no mmc device at slot %x\n", curr_device
);
581 return CMD_RET_FAILURE
;
583 ret
= mmc_set_dsr(mmc
, val
);
584 printf("set dsr %s\n", (!ret
) ? "OK, force rescan" : "ERROR");
588 return CMD_RET_FAILURE
;
590 return CMD_RET_SUCCESS
;
595 static cmd_tbl_t cmd_mmc
[] = {
596 U_BOOT_CMD_MKENT(info
, 1, 0, do_mmcinfo
, "", ""),
597 U_BOOT_CMD_MKENT(read
, 4, 1, do_mmc_read
, "", ""),
598 U_BOOT_CMD_MKENT(write
, 4, 0, do_mmc_write
, "", ""),
599 U_BOOT_CMD_MKENT(erase
, 3, 0, do_mmc_erase
, "", ""),
600 U_BOOT_CMD_MKENT(rescan
, 1, 1, do_mmc_rescan
, "", ""),
601 U_BOOT_CMD_MKENT(part
, 1, 1, do_mmc_part
, "", ""),
602 U_BOOT_CMD_MKENT(dev
, 3, 0, do_mmc_dev
, "", ""),
603 U_BOOT_CMD_MKENT(list
, 1, 1, do_mmc_list
, "", ""),
604 #ifdef CONFIG_SUPPORT_EMMC_BOOT
605 U_BOOT_CMD_MKENT(bootbus
, 5, 0, do_mmc_bootbus
, "", ""),
606 U_BOOT_CMD_MKENT(bootpart
-resize
, 4, 0, do_mmc_boot_resize
, "", ""),
607 U_BOOT_CMD_MKENT(partconf
, 5, 0, do_mmc_partconf
, "", ""),
608 U_BOOT_CMD_MKENT(rst
-function
, 3, 0, do_mmc_rst_func
, "", ""),
610 #ifdef CONFIG_SUPPORT_EMMC_RPMB
611 U_BOOT_CMD_MKENT(rpmb
, CONFIG_SYS_MAXARGS
, 1, do_mmcrpmb
, "", ""),
613 U_BOOT_CMD_MKENT(setdsr
, 2, 0, do_mmc_setdsr
, "", ""),
616 static int do_mmcops(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
620 cp
= find_cmd_tbl(argv
[1], cmd_mmc
, ARRAY_SIZE(cmd_mmc
));
622 /* Drop the mmc command */
626 if (cp
== NULL
|| argc
> cp
->maxargs
)
627 return CMD_RET_USAGE
;
628 if (flag
== CMD_FLAG_REPEAT
&& !cp
->repeatable
)
629 return CMD_RET_SUCCESS
;
631 if (curr_device
< 0) {
632 if (get_mmc_num() > 0) {
635 puts("No MMC device available\n");
636 return CMD_RET_FAILURE
;
639 return cp
->cmd(cmdtp
, flag
, argc
, argv
);
643 mmc
, 7, 1, do_mmcops
,
645 "info - display info of the current MMC device\n"
646 "mmc read addr blk# cnt\n"
647 "mmc write addr blk# cnt\n"
648 "mmc erase blk# cnt\n"
650 "mmc part - lists available partition on current mmc device\n"
651 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
652 "mmc list - lists available devices\n"
653 #ifdef CONFIG_SUPPORT_EMMC_BOOT
654 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
655 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
656 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
657 " - Change sizes of boot and RPMB partitions of specified device\n"
658 "mmc partconf dev boot_ack boot_partition partition_access\n"
659 " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
660 "mmc rst-function dev value\n"
661 " - Change the RST_n_FUNCTION field of the specified device\n"
662 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
664 #ifdef CONFIG_SUPPORT_EMMC_RPMB
665 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
666 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
667 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
668 "mmc rpmb counter - read the value of the write counter\n"
670 "mmc setdsr <value> - set DSR register value\n"
673 /* Old command kept for compatibility. Same as 'mmc info' */
675 mmcinfo
, 1, 0, do_mmcinfo
,
677 "- display info of the current MMC device"
680 #endif /* !CONFIG_GENERIC_MMC */