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