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