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