Kconfig: Move CONFIG_BOOTSTAGE to Kconfig
[platform/kernel/u-boot.git] / common / cmd_mmc.c
1 /*
2  * (C) Copyright 2003
3  * Kyle Harris, kharris@nexus-tech.net
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9 #include <command.h>
10 #include <mmc.h>
11
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[])
15 {
16         int dev;
17
18         if (argc < 2)
19                 return CMD_RET_USAGE;
20
21         if (strcmp(argv[1], "init") == 0) {
22                 if (argc == 2) {
23                         if (curr_device < 0)
24                                 dev = 1;
25                         else
26                                 dev = curr_device;
27                 } else if (argc == 3) {
28                         dev = (int)simple_strtoul(argv[2], NULL, 10);
29                 } else {
30                         return CMD_RET_USAGE;
31                 }
32
33                 if (mmc_legacy_init(dev) != 0) {
34                         puts("No MMC card found\n");
35                         return 1;
36                 }
37
38                 curr_device = dev;
39                 printf("mmc%d is available\n", curr_device);
40         } else if (strcmp(argv[1], "device") == 0) {
41                 if (argc == 2) {
42                         if (curr_device < 0) {
43                                 puts("No MMC device available\n");
44                                 return 1;
45                         }
46                 } else if (argc == 3) {
47                         dev = (int)simple_strtoul(argv[2], NULL, 10);
48
49 #ifdef CONFIG_SYS_MMC_SET_DEV
50                         if (mmc_set_dev(dev) != 0)
51                                 return 1;
52 #endif
53                         curr_device = dev;
54                 } else {
55                         return CMD_RET_USAGE;
56                 }
57
58                 printf("mmc%d is current device\n", curr_device);
59         } else {
60                 return CMD_RET_USAGE;
61         }
62
63         return 0;
64 }
65
66 U_BOOT_CMD(
67         mmc, 3, 1, do_mmc,
68         "MMC sub-system",
69         "init [dev] - init MMC sub system\n"
70         "mmc device [dev] - show or set current device"
71 );
72 #else /* !CONFIG_GENERIC_MMC */
73
74 static void print_mmcinfo(struct mmc *mmc)
75 {
76         int i;
77
78         printf("Device: %s\n", mmc->cfg->name);
79         printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
80         printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
81         printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
82                         (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
83                         (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
84
85         printf("Tran Speed: %d\n", mmc->tran_speed);
86         printf("Rd Block Len: %d\n", mmc->read_bl_len);
87
88         printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
89                         EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
90                         EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
91         if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
92                 printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
93         printf("\n");
94
95         printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
96         puts("Capacity: ");
97         print_size(mmc->capacity, "\n");
98
99         printf("Bus Width: %d-bit%s\n", mmc->bus_width,
100                         mmc->ddr_mode ? " DDR" : "");
101
102         puts("Erase Group Size: ");
103         print_size(((u64)mmc->erase_grp_size) << 9, "\n");
104
105         if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
106                 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
107                 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
108
109                 puts("HC WP Group Size: ");
110                 print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
111
112                 puts("User Capacity: ");
113                 print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
114                 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
115                         puts(" WRREL\n");
116                 else
117                         putc('\n');
118                 if (usr_enh) {
119                         puts("User Enhanced Start: ");
120                         print_size(mmc->enh_user_start, "\n");
121                         puts("User Enhanced Size: ");
122                         print_size(mmc->enh_user_size, "\n");
123                 }
124                 puts("Boot Capacity: ");
125                 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
126                 puts("RPMB Capacity: ");
127                 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
128
129                 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
130                         bool is_enh = has_enh &&
131                                 (mmc->part_attr & EXT_CSD_ENH_GP(i));
132                         if (mmc->capacity_gp[i]) {
133                                 printf("GP%i Capacity: ", i+1);
134                                 print_size(mmc->capacity_gp[i],
135                                            is_enh ? " ENH" : "");
136                                 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
137                                         puts(" WRREL\n");
138                                 else
139                                         putc('\n');
140                         }
141                 }
142         }
143 }
144 static struct mmc *init_mmc_device(int dev, bool force_init)
145 {
146         struct mmc *mmc;
147         mmc = find_mmc_device(dev);
148         if (!mmc) {
149                 printf("no mmc device at slot %x\n", dev);
150                 return NULL;
151         }
152         if (force_init)
153                 mmc->has_init = 0;
154         if (mmc_init(mmc))
155                 return NULL;
156         return mmc;
157 }
158 static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
159 {
160         struct mmc *mmc;
161
162         if (curr_device < 0) {
163                 if (get_mmc_num() > 0)
164                         curr_device = 0;
165                 else {
166                         puts("No MMC device available\n");
167                         return 1;
168                 }
169         }
170
171         mmc = init_mmc_device(curr_device, false);
172         if (!mmc)
173                 return CMD_RET_FAILURE;
174
175         print_mmcinfo(mmc);
176         return CMD_RET_SUCCESS;
177 }
178
179 #ifdef CONFIG_SUPPORT_EMMC_RPMB
180 static int confirm_key_prog(void)
181 {
182         puts("Warning: Programming authentication key can be done only once !\n"
183              "         Use this command only if you are sure of what you are doing,\n"
184              "Really perform the key programming? <y/N> ");
185         if (confirm_yesno())
186                 return 1;
187
188         puts("Authentication key programming aborted\n");
189         return 0;
190 }
191 static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag,
192                           int argc, char * const argv[])
193 {
194         void *key_addr;
195         struct mmc *mmc = find_mmc_device(curr_device);
196
197         if (argc != 2)
198                 return CMD_RET_USAGE;
199
200         key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
201         if (!confirm_key_prog())
202                 return CMD_RET_FAILURE;
203         if (mmc_rpmb_set_key(mmc, key_addr)) {
204                 printf("ERROR - Key already programmed ?\n");
205                 return CMD_RET_FAILURE;
206         }
207         return CMD_RET_SUCCESS;
208 }
209 static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag,
210                            int argc, char * const argv[])
211 {
212         u16 blk, cnt;
213         void *addr;
214         int n;
215         void *key_addr = NULL;
216         struct mmc *mmc = find_mmc_device(curr_device);
217
218         if (argc < 4)
219                 return CMD_RET_USAGE;
220
221         addr = (void *)simple_strtoul(argv[1], NULL, 16);
222         blk = simple_strtoul(argv[2], NULL, 16);
223         cnt = simple_strtoul(argv[3], NULL, 16);
224
225         if (argc == 5)
226                 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
227
228         printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
229                curr_device, blk, cnt);
230         n =  mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
231
232         printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
233         if (n != cnt)
234                 return CMD_RET_FAILURE;
235         return CMD_RET_SUCCESS;
236 }
237 static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag,
238                             int argc, char * const argv[])
239 {
240         u16 blk, cnt;
241         void *addr;
242         int n;
243         void *key_addr;
244         struct mmc *mmc = find_mmc_device(curr_device);
245
246         if (argc != 5)
247                 return CMD_RET_USAGE;
248
249         addr = (void *)simple_strtoul(argv[1], NULL, 16);
250         blk = simple_strtoul(argv[2], NULL, 16);
251         cnt = simple_strtoul(argv[3], NULL, 16);
252         key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
253
254         printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
255                curr_device, blk, cnt);
256         n =  mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
257
258         printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
259         if (n != cnt)
260                 return CMD_RET_FAILURE;
261         return CMD_RET_SUCCESS;
262 }
263 static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag,
264                               int argc, char * const argv[])
265 {
266         unsigned long counter;
267         struct mmc *mmc = find_mmc_device(curr_device);
268
269         if (mmc_rpmb_get_counter(mmc, &counter))
270                 return CMD_RET_FAILURE;
271         printf("RPMB Write counter= %lx\n", counter);
272         return CMD_RET_SUCCESS;
273 }
274
275 static cmd_tbl_t cmd_rpmb[] = {
276         U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
277         U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
278         U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
279         U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
280 };
281
282 static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag,
283                       int argc, char * const argv[])
284 {
285         cmd_tbl_t *cp;
286         struct mmc *mmc;
287         char original_part;
288         int ret;
289
290         cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
291
292         /* Drop the rpmb subcommand */
293         argc--;
294         argv++;
295
296         if (cp == NULL || argc > cp->maxargs)
297                 return CMD_RET_USAGE;
298         if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
299                 return CMD_RET_SUCCESS;
300
301         mmc = init_mmc_device(curr_device, false);
302         if (!mmc)
303                 return CMD_RET_FAILURE;
304
305         if (!(mmc->version & MMC_VERSION_MMC)) {
306                 printf("It is not a EMMC device\n");
307                 return CMD_RET_FAILURE;
308         }
309         if (mmc->version < MMC_VERSION_4_41) {
310                 printf("RPMB not supported before version 4.41\n");
311                 return CMD_RET_FAILURE;
312         }
313         /* Switch to the RPMB partition */
314         original_part = mmc->part_num;
315         if (mmc->part_num != MMC_PART_RPMB) {
316                 if (mmc_switch_part(curr_device, MMC_PART_RPMB) != 0)
317                         return CMD_RET_FAILURE;
318                 mmc->part_num = MMC_PART_RPMB;
319         }
320         ret = cp->cmd(cmdtp, flag, argc, argv);
321
322         /* Return to original partition */
323         if (mmc->part_num != original_part) {
324                 if (mmc_switch_part(curr_device, original_part) != 0)
325                         return CMD_RET_FAILURE;
326                 mmc->part_num = original_part;
327         }
328         return ret;
329 }
330 #endif
331
332 static int do_mmc_read(cmd_tbl_t *cmdtp, int flag,
333                        int argc, char * const argv[])
334 {
335         struct mmc *mmc;
336         u32 blk, cnt, n;
337         void *addr;
338
339         if (argc != 4)
340                 return CMD_RET_USAGE;
341
342         addr = (void *)simple_strtoul(argv[1], NULL, 16);
343         blk = simple_strtoul(argv[2], NULL, 16);
344         cnt = simple_strtoul(argv[3], NULL, 16);
345
346         mmc = init_mmc_device(curr_device, false);
347         if (!mmc)
348                 return CMD_RET_FAILURE;
349
350         printf("\nMMC read: dev # %d, block # %d, count %d ... ",
351                curr_device, blk, cnt);
352
353         n = mmc->block_dev.block_read(curr_device, blk, cnt, addr);
354         /* flush cache after read */
355         flush_cache((ulong)addr, cnt * 512); /* FIXME */
356         printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
357
358         return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
359 }
360 static int do_mmc_write(cmd_tbl_t *cmdtp, int flag,
361                         int argc, char * const argv[])
362 {
363         struct mmc *mmc;
364         u32 blk, cnt, n;
365         void *addr;
366
367         if (argc != 4)
368                 return CMD_RET_USAGE;
369
370         addr = (void *)simple_strtoul(argv[1], NULL, 16);
371         blk = simple_strtoul(argv[2], NULL, 16);
372         cnt = simple_strtoul(argv[3], NULL, 16);
373
374         mmc = init_mmc_device(curr_device, false);
375         if (!mmc)
376                 return CMD_RET_FAILURE;
377
378         printf("\nMMC write: dev # %d, block # %d, count %d ... ",
379                curr_device, blk, cnt);
380
381         if (mmc_getwp(mmc) == 1) {
382                 printf("Error: card is write protected!\n");
383                 return CMD_RET_FAILURE;
384         }
385         n = mmc->block_dev.block_write(curr_device, blk, cnt, addr);
386         printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
387
388         return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
389 }
390 static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,
391                         int argc, char * const argv[])
392 {
393         struct mmc *mmc;
394         u32 blk, cnt, n;
395
396         if (argc != 3)
397                 return CMD_RET_USAGE;
398
399         blk = simple_strtoul(argv[1], NULL, 16);
400         cnt = simple_strtoul(argv[2], NULL, 16);
401
402         mmc = init_mmc_device(curr_device, false);
403         if (!mmc)
404                 return CMD_RET_FAILURE;
405
406         printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
407                curr_device, blk, cnt);
408
409         if (mmc_getwp(mmc) == 1) {
410                 printf("Error: card is write protected!\n");
411                 return CMD_RET_FAILURE;
412         }
413         n = mmc->block_dev.block_erase(curr_device, blk, cnt);
414         printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
415
416         return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
417 }
418 static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
419                          int argc, char * const argv[])
420 {
421         struct mmc *mmc;
422
423         mmc = init_mmc_device(curr_device, true);
424         if (!mmc)
425                 return CMD_RET_FAILURE;
426
427         return CMD_RET_SUCCESS;
428 }
429 static int do_mmc_part(cmd_tbl_t *cmdtp, int flag,
430                        int argc, char * const argv[])
431 {
432         block_dev_desc_t *mmc_dev;
433         struct mmc *mmc;
434
435         mmc = init_mmc_device(curr_device, false);
436         if (!mmc)
437                 return CMD_RET_FAILURE;
438
439         mmc_dev = mmc_get_dev(curr_device);
440         if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
441                 print_part(mmc_dev);
442                 return CMD_RET_SUCCESS;
443         }
444
445         puts("get mmc type error!\n");
446         return CMD_RET_FAILURE;
447 }
448 static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag,
449                       int argc, char * const argv[])
450 {
451         int dev, part = 0, ret;
452         struct mmc *mmc;
453
454         if (argc == 1) {
455                 dev = curr_device;
456         } else if (argc == 2) {
457                 dev = simple_strtoul(argv[1], NULL, 10);
458         } else if (argc == 3) {
459                 dev = (int)simple_strtoul(argv[1], NULL, 10);
460                 part = (int)simple_strtoul(argv[2], NULL, 10);
461                 if (part > PART_ACCESS_MASK) {
462                         printf("#part_num shouldn't be larger than %d\n",
463                                PART_ACCESS_MASK);
464                         return CMD_RET_FAILURE;
465                 }
466         } else {
467                 return CMD_RET_USAGE;
468         }
469
470         mmc = init_mmc_device(dev, true);
471         if (!mmc)
472                 return CMD_RET_FAILURE;
473
474         ret = mmc_select_hwpart(dev, part);
475         printf("switch to partitions #%d, %s\n",
476                part, (!ret) ? "OK" : "ERROR");
477         if (ret)
478                 return 1;
479
480         curr_device = dev;
481         if (mmc->part_config == MMCPART_NOAVAILABLE)
482                 printf("mmc%d is current device\n", curr_device);
483         else
484                 printf("mmc%d(part %d) is current device\n",
485                        curr_device, mmc->part_num);
486
487         return CMD_RET_SUCCESS;
488 }
489 static int do_mmc_list(cmd_tbl_t *cmdtp, int flag,
490                        int argc, char * const argv[])
491 {
492         print_mmc_devices('\n');
493         return CMD_RET_SUCCESS;
494 }
495
496 static int parse_hwpart_user(struct mmc_hwpart_conf *pconf,
497                              int argc, char * const argv[])
498 {
499         int i = 0;
500
501         memset(&pconf->user, 0, sizeof(pconf->user));
502
503         while (i < argc) {
504                 if (!strcmp(argv[i], "enh")) {
505                         if (i + 2 >= argc)
506                                 return -1;
507                         pconf->user.enh_start =
508                                 simple_strtoul(argv[i+1], NULL, 10);
509                         pconf->user.enh_size =
510                                 simple_strtoul(argv[i+2], NULL, 10);
511                         i += 3;
512                 } else if (!strcmp(argv[i], "wrrel")) {
513                         if (i + 1 >= argc)
514                                 return -1;
515                         pconf->user.wr_rel_change = 1;
516                         if (!strcmp(argv[i+1], "on"))
517                                 pconf->user.wr_rel_set = 1;
518                         else if (!strcmp(argv[i+1], "off"))
519                                 pconf->user.wr_rel_set = 0;
520                         else
521                                 return -1;
522                         i += 2;
523                 } else {
524                         break;
525                 }
526         }
527         return i;
528 }
529
530 static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
531                            int argc, char * const argv[])
532 {
533         int i;
534
535         memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
536
537         if (1 >= argc)
538                 return -1;
539         pconf->gp_part[pidx].size = simple_strtoul(argv[0], NULL, 10);
540
541         i = 1;
542         while (i < argc) {
543                 if (!strcmp(argv[i], "enh")) {
544                         pconf->gp_part[pidx].enhanced = 1;
545                         i += 1;
546                 } else if (!strcmp(argv[i], "wrrel")) {
547                         if (i + 1 >= argc)
548                                 return -1;
549                         pconf->gp_part[pidx].wr_rel_change = 1;
550                         if (!strcmp(argv[i+1], "on"))
551                                 pconf->gp_part[pidx].wr_rel_set = 1;
552                         else if (!strcmp(argv[i+1], "off"))
553                                 pconf->gp_part[pidx].wr_rel_set = 0;
554                         else
555                                 return -1;
556                         i += 2;
557                 } else {
558                         break;
559                 }
560         }
561         return i;
562 }
563
564 static int do_mmc_hwpartition(cmd_tbl_t *cmdtp, int flag,
565                               int argc, char * const argv[])
566 {
567         struct mmc *mmc;
568         struct mmc_hwpart_conf pconf = { };
569         enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
570         int i, r, pidx;
571
572         mmc = init_mmc_device(curr_device, false);
573         if (!mmc)
574                 return CMD_RET_FAILURE;
575
576         if (argc < 1)
577                 return CMD_RET_USAGE;
578         i = 1;
579         while (i < argc) {
580                 if (!strcmp(argv[i], "user")) {
581                         i++;
582                         r = parse_hwpart_user(&pconf, argc-i, &argv[i]);
583                         if (r < 0)
584                                 return CMD_RET_USAGE;
585                         i += r;
586                 } else if (!strncmp(argv[i], "gp", 2) &&
587                            strlen(argv[i]) == 3 &&
588                            argv[i][2] >= '1' && argv[i][2] <= '4') {
589                         pidx = argv[i][2] - '1';
590                         i++;
591                         r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
592                         if (r < 0)
593                                 return CMD_RET_USAGE;
594                         i += r;
595                 } else if (!strcmp(argv[i], "check")) {
596                         mode = MMC_HWPART_CONF_CHECK;
597                         i++;
598                 } else if (!strcmp(argv[i], "set")) {
599                         mode = MMC_HWPART_CONF_SET;
600                         i++;
601                 } else if (!strcmp(argv[i], "complete")) {
602                         mode = MMC_HWPART_CONF_COMPLETE;
603                         i++;
604                 } else {
605                         return CMD_RET_USAGE;
606                 }
607         }
608
609         puts("Partition configuration:\n");
610         if (pconf.user.enh_size) {
611                 puts("\tUser Enhanced Start: ");
612                 print_size(((u64)pconf.user.enh_start) << 9, "\n");
613                 puts("\tUser Enhanced Size: ");
614                 print_size(((u64)pconf.user.enh_size) << 9, "\n");
615         } else {
616                 puts("\tNo enhanced user data area\n");
617         }
618         if (pconf.user.wr_rel_change)
619                 printf("\tUser partition write reliability: %s\n",
620                        pconf.user.wr_rel_set ? "on" : "off");
621         for (pidx = 0; pidx < 4; pidx++) {
622                 if (pconf.gp_part[pidx].size) {
623                         printf("\tGP%i Capacity: ", pidx+1);
624                         print_size(((u64)pconf.gp_part[pidx].size) << 9,
625                                    pconf.gp_part[pidx].enhanced ?
626                                    " ENH\n" : "\n");
627                 } else {
628                         printf("\tNo GP%i partition\n", pidx+1);
629                 }
630                 if (pconf.gp_part[pidx].wr_rel_change)
631                         printf("\tGP%i write reliability: %s\n", pidx+1,
632                                pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
633         }
634
635         if (!mmc_hwpart_config(mmc, &pconf, mode)) {
636                 if (mode == MMC_HWPART_CONF_COMPLETE)
637                         puts("Partitioning successful, "
638                              "power-cycle to make effective\n");
639                 return CMD_RET_SUCCESS;
640         } else {
641                 puts("Failed!\n");
642                 return CMD_RET_FAILURE;
643         }
644 }
645
646 #ifdef CONFIG_SUPPORT_EMMC_BOOT
647 static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag,
648                           int argc, char * const argv[])
649 {
650         int dev;
651         struct mmc *mmc;
652         u8 width, reset, mode;
653
654         if (argc != 5)
655                 return CMD_RET_USAGE;
656         dev = simple_strtoul(argv[1], NULL, 10);
657         width = simple_strtoul(argv[2], NULL, 10);
658         reset = simple_strtoul(argv[3], NULL, 10);
659         mode = simple_strtoul(argv[4], NULL, 10);
660
661         mmc = init_mmc_device(dev, false);
662         if (!mmc)
663                 return CMD_RET_FAILURE;
664
665         if (IS_SD(mmc)) {
666                 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
667                 return CMD_RET_FAILURE;
668         }
669
670         /* acknowledge to be sent during boot operation */
671         return mmc_set_boot_bus_width(mmc, width, reset, mode);
672 }
673 static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag,
674                               int argc, char * const argv[])
675 {
676         int dev;
677         struct mmc *mmc;
678         u32 bootsize, rpmbsize;
679
680         if (argc != 4)
681                 return CMD_RET_USAGE;
682         dev = simple_strtoul(argv[1], NULL, 10);
683         bootsize = simple_strtoul(argv[2], NULL, 10);
684         rpmbsize = simple_strtoul(argv[3], NULL, 10);
685
686         mmc = init_mmc_device(dev, false);
687         if (!mmc)
688                 return CMD_RET_FAILURE;
689
690         if (IS_SD(mmc)) {
691                 printf("It is not a EMMC device\n");
692                 return CMD_RET_FAILURE;
693         }
694
695         if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
696                 printf("EMMC boot partition Size change Failed.\n");
697                 return CMD_RET_FAILURE;
698         }
699
700         printf("EMMC boot partition Size %d MB\n", bootsize);
701         printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
702         return CMD_RET_SUCCESS;
703 }
704 static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag,
705                            int argc, char * const argv[])
706 {
707         int dev;
708         struct mmc *mmc;
709         u8 ack, part_num, access;
710
711         if (argc != 5)
712                 return CMD_RET_USAGE;
713
714         dev = simple_strtoul(argv[1], NULL, 10);
715         ack = simple_strtoul(argv[2], NULL, 10);
716         part_num = simple_strtoul(argv[3], NULL, 10);
717         access = simple_strtoul(argv[4], NULL, 10);
718
719         mmc = init_mmc_device(dev, false);
720         if (!mmc)
721                 return CMD_RET_FAILURE;
722
723         if (IS_SD(mmc)) {
724                 puts("PARTITION_CONFIG only exists on eMMC\n");
725                 return CMD_RET_FAILURE;
726         }
727
728         /* acknowledge to be sent during boot operation */
729         return mmc_set_part_conf(mmc, ack, part_num, access);
730 }
731 static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag,
732                            int argc, char * const argv[])
733 {
734         int dev;
735         struct mmc *mmc;
736         u8 enable;
737
738         /*
739          * Set the RST_n_ENABLE bit of RST_n_FUNCTION
740          * The only valid values are 0x0, 0x1 and 0x2 and writing
741          * a value of 0x1 or 0x2 sets the value permanently.
742          */
743         if (argc != 3)
744                 return CMD_RET_USAGE;
745
746         dev = simple_strtoul(argv[1], NULL, 10);
747         enable = simple_strtoul(argv[2], NULL, 10);
748
749         if (enable > 2 || enable < 0) {
750                 puts("Invalid RST_n_ENABLE value\n");
751                 return CMD_RET_USAGE;
752         }
753
754         mmc = init_mmc_device(dev, false);
755         if (!mmc)
756                 return CMD_RET_FAILURE;
757
758         if (IS_SD(mmc)) {
759                 puts("RST_n_FUNCTION only exists on eMMC\n");
760                 return CMD_RET_FAILURE;
761         }
762
763         return mmc_set_rst_n_function(mmc, enable);
764 }
765 #endif
766 static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag,
767                          int argc, char * const argv[])
768 {
769         struct mmc *mmc;
770         u32 val;
771         int ret;
772
773         if (argc != 2)
774                 return CMD_RET_USAGE;
775         val = simple_strtoul(argv[2], NULL, 16);
776
777         mmc = find_mmc_device(curr_device);
778         if (!mmc) {
779                 printf("no mmc device at slot %x\n", curr_device);
780                 return CMD_RET_FAILURE;
781         }
782         ret = mmc_set_dsr(mmc, val);
783         printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
784         if (!ret) {
785                 mmc->has_init = 0;
786                 if (mmc_init(mmc))
787                         return CMD_RET_FAILURE;
788                 else
789                         return CMD_RET_SUCCESS;
790         }
791         return ret;
792 }
793
794 static cmd_tbl_t cmd_mmc[] = {
795         U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
796         U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
797         U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
798         U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
799         U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
800         U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
801         U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
802         U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
803         U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
804 #ifdef CONFIG_SUPPORT_EMMC_BOOT
805         U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
806         U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
807         U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
808         U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
809 #endif
810 #ifdef CONFIG_SUPPORT_EMMC_RPMB
811         U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
812 #endif
813         U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
814 };
815
816 static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
817 {
818         cmd_tbl_t *cp;
819
820         cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
821
822         /* Drop the mmc command */
823         argc--;
824         argv++;
825
826         if (cp == NULL || argc > cp->maxargs)
827                 return CMD_RET_USAGE;
828         if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
829                 return CMD_RET_SUCCESS;
830
831         if (curr_device < 0) {
832                 if (get_mmc_num() > 0) {
833                         curr_device = 0;
834                 } else {
835                         puts("No MMC device available\n");
836                         return CMD_RET_FAILURE;
837                 }
838         }
839         return cp->cmd(cmdtp, flag, argc, argv);
840 }
841
842 U_BOOT_CMD(
843         mmc, 29, 1, do_mmcops,
844         "MMC sub system",
845         "info - display info of the current MMC device\n"
846         "mmc read addr blk# cnt\n"
847         "mmc write addr blk# cnt\n"
848         "mmc erase blk# cnt\n"
849         "mmc rescan\n"
850         "mmc part - lists available partition on current mmc device\n"
851         "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
852         "mmc list - lists available devices\n"
853         "mmc hwpartition [args...] - does hardware partitioning\n"
854         "  arguments (sizes in 512-byte blocks):\n"
855         "    [user [enh start cnt] [wrrel {on|off}]] - sets user data area attributes\n"
856         "    [gp1|gp2|gp3|gp4 cnt [enh] [wrrel {on|off}]] - general purpose partition\n"
857         "    [check|set|complete] - mode, complete set partitioning completed\n"
858         "  WARNING: Partitioning is a write-once setting once it is set to complete.\n"
859         "  Power cycling is required to initialize partitions after set to complete.\n"
860 #ifdef CONFIG_SUPPORT_EMMC_BOOT
861         "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
862         " - Set the BOOT_BUS_WIDTH field of the specified device\n"
863         "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
864         " - Change sizes of boot and RPMB partitions of specified device\n"
865         "mmc partconf dev boot_ack boot_partition partition_access\n"
866         " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
867         "mmc rst-function dev value\n"
868         " - Change the RST_n_FUNCTION field of the specified device\n"
869         "   WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
870 #endif
871 #ifdef CONFIG_SUPPORT_EMMC_RPMB
872         "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
873         "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
874         "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
875         "mmc rpmb counter - read the value of the write counter\n"
876 #endif
877         "mmc setdsr <value> - set DSR register value\n"
878         );
879
880 /* Old command kept for compatibility. Same as 'mmc info' */
881 U_BOOT_CMD(
882         mmcinfo, 1, 0, do_mmcinfo,
883         "display MMC info",
884         "- display info of the current MMC device"
885 );
886
887 #endif /* !CONFIG_GENERIC_MMC */