blob: 96478e45c14039cd88a1a59427d0ec7f1a44d07e [file] [log] [blame]
wdenk71f95112003-06-15 22:40:42 +00001/*
2 * (C) Copyright 2003
3 * Kyle Harris, kharris@nexus-tech.net
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk71f95112003-06-15 22:40:42 +00006 */
7
8#include <common.h>
9#include <command.h>
wdenk71f95112003-06-15 22:40:42 +000010#include <mmc.h>
11
Mike Frysinger02e22c22009-06-14 21:35:21 -040012static int curr_device = -1;
Lei Wenea6ebe22011-05-02 16:26:25 +000013#ifndef CONFIG_GENERIC_MMC
Wolfgang Denk54841ab2010-06-28 22:00:46 +020014int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk71f95112003-06-15 22:40:42 +000015{
Minkyu Kang869f6bf2009-03-30 14:55:51 +090016 int dev;
17
Wolfgang Denk47e26b12010-07-17 01:06:04 +020018 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +000019 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090020
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 {
Simon Glass4c12eeb2011-12-10 08:44:01 +000030 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090031 }
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 {
Simon Glass4c12eeb2011-12-10 08:44:01 +000055 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090056 }
57
58 printf("mmc%d is current device\n", curr_device);
59 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +000060 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090061 }
62
wdenk71f95112003-06-15 22:40:42 +000063 return 0;
64}
65
wdenk0d498392003-07-01 21:06:45 +000066U_BOOT_CMD(
Minkyu Kang869f6bf2009-03-30 14:55:51 +090067 mmc, 3, 1, do_mmc,
68 "MMC sub-system",
69 "init [dev] - init MMC sub system\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +020070 "mmc device [dev] - show or set current device"
wdenkb0fce992003-06-29 21:03:46 +000071);
Dirk Behme3511b4e2009-02-18 19:59:39 +010072#else /* !CONFIG_GENERIC_MMC */
Andy Fleming272cc702008-10-30 16:41:01 -050073
74static void print_mmcinfo(struct mmc *mmc)
75{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020076 printf("Device: %s\n", mmc->cfg->name);
Andy Fleming272cc702008-10-30 16:41:01 -050077 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
78 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
79 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
80 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
81 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
82
83 printf("Tran Speed: %d\n", mmc->tran_speed);
84 printf("Rd Block Len: %d\n", mmc->read_bl_len);
85
86 printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
Jaehoon Chung64f4a612013-01-29 19:31:16 +000087 (mmc->version >> 8) & 0xf, mmc->version & 0xff);
Andy Fleming272cc702008-10-30 16:41:01 -050088
89 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang940e0782011-01-04 01:04:19 +000090 puts("Capacity: ");
91 print_size(mmc->capacity, "\n");
Andy Fleming272cc702008-10-30 16:41:01 -050092
Andrew Gabbasov786e8f82014-12-01 06:59:09 -060093 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
94 mmc->ddr_mode ? " DDR" : "");
Andy Fleming272cc702008-10-30 16:41:01 -050095}
Stephen Warren1ae24a52014-05-23 13:24:45 -060096static struct mmc *init_mmc_device(int dev, bool force_init)
Pierre Aubert1fd93c62014-04-24 10:30:08 +020097{
98 struct mmc *mmc;
99 mmc = find_mmc_device(dev);
100 if (!mmc) {
101 printf("no mmc device at slot %x\n", dev);
102 return NULL;
103 }
Stephen Warren1ae24a52014-05-23 13:24:45 -0600104 if (force_init)
105 mmc->has_init = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200106 if (mmc_init(mmc))
107 return NULL;
108 return mmc;
109}
Kim Phillips088f1b12012-10-29 13:34:31 +0000110static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500111{
112 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500113
Lei Wenea6ebe22011-05-02 16:26:25 +0000114 if (curr_device < 0) {
115 if (get_mmc_num() > 0)
116 curr_device = 0;
117 else {
118 puts("No MMC device available\n");
119 return 1;
120 }
121 }
Andy Fleming272cc702008-10-30 16:41:01 -0500122
Stephen Warren1ae24a52014-05-23 13:24:45 -0600123 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200124 if (!mmc)
125 return CMD_RET_FAILURE;
Andy Fleming272cc702008-10-30 16:41:01 -0500126
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200127 print_mmcinfo(mmc);
128 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500129}
130
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200131#ifdef CONFIG_SUPPORT_EMMC_RPMB
132static int confirm_key_prog(void)
133{
134 puts("Warning: Programming authentication key can be done only once !\n"
135 " Use this command only if you are sure of what you are doing,\n"
136 "Really perform the key programming? <y/N> ");
137 if (confirm_yesno())
138 return 1;
139
140 puts("Authentication key programming aborted\n");
141 return 0;
142}
143static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag,
144 int argc, char * const argv[])
145{
146 void *key_addr;
147 struct mmc *mmc = find_mmc_device(curr_device);
148
149 if (argc != 2)
150 return CMD_RET_USAGE;
151
152 key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
153 if (!confirm_key_prog())
154 return CMD_RET_FAILURE;
155 if (mmc_rpmb_set_key(mmc, key_addr)) {
156 printf("ERROR - Key already programmed ?\n");
157 return CMD_RET_FAILURE;
158 }
159 return CMD_RET_SUCCESS;
160}
161static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag,
162 int argc, char * const argv[])
163{
164 u16 blk, cnt;
165 void *addr;
166 int n;
167 void *key_addr = NULL;
168 struct mmc *mmc = find_mmc_device(curr_device);
169
170 if (argc < 4)
171 return CMD_RET_USAGE;
172
173 addr = (void *)simple_strtoul(argv[1], NULL, 16);
174 blk = simple_strtoul(argv[2], NULL, 16);
175 cnt = simple_strtoul(argv[3], NULL, 16);
176
177 if (argc == 5)
178 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
179
180 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
181 curr_device, blk, cnt);
182 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
183
184 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
185 if (n != cnt)
186 return CMD_RET_FAILURE;
187 return CMD_RET_SUCCESS;
188}
189static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag,
190 int argc, char * const argv[])
191{
192 u16 blk, cnt;
193 void *addr;
194 int n;
195 void *key_addr;
196 struct mmc *mmc = find_mmc_device(curr_device);
197
198 if (argc != 5)
199 return CMD_RET_USAGE;
200
201 addr = (void *)simple_strtoul(argv[1], NULL, 16);
202 blk = simple_strtoul(argv[2], NULL, 16);
203 cnt = simple_strtoul(argv[3], NULL, 16);
204 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
205
206 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
207 curr_device, blk, cnt);
208 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
209
210 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
211 if (n != cnt)
212 return CMD_RET_FAILURE;
213 return CMD_RET_SUCCESS;
214}
215static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag,
216 int argc, char * const argv[])
217{
218 unsigned long counter;
219 struct mmc *mmc = find_mmc_device(curr_device);
220
221 if (mmc_rpmb_get_counter(mmc, &counter))
222 return CMD_RET_FAILURE;
223 printf("RPMB Write counter= %lx\n", counter);
224 return CMD_RET_SUCCESS;
225}
226
227static cmd_tbl_t cmd_rpmb[] = {
228 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
229 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
230 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
231 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
232};
233
234static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag,
235 int argc, char * const argv[])
236{
237 cmd_tbl_t *cp;
238 struct mmc *mmc;
239 char original_part;
240 int ret;
241
242 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
243
244 /* Drop the rpmb subcommand */
245 argc--;
246 argv++;
247
248 if (cp == NULL || argc > cp->maxargs)
249 return CMD_RET_USAGE;
250 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
251 return CMD_RET_SUCCESS;
252
Stephen Warren1ae24a52014-05-23 13:24:45 -0600253 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200254 if (!mmc)
255 return CMD_RET_FAILURE;
256
257 if (!(mmc->version & MMC_VERSION_MMC)) {
258 printf("It is not a EMMC device\n");
259 return CMD_RET_FAILURE;
260 }
261 if (mmc->version < MMC_VERSION_4_41) {
262 printf("RPMB not supported before version 4.41\n");
263 return CMD_RET_FAILURE;
264 }
265 /* Switch to the RPMB partition */
266 original_part = mmc->part_num;
267 if (mmc->part_num != MMC_PART_RPMB) {
268 if (mmc_switch_part(curr_device, MMC_PART_RPMB) != 0)
269 return CMD_RET_FAILURE;
270 mmc->part_num = MMC_PART_RPMB;
271 }
272 ret = cp->cmd(cmdtp, flag, argc, argv);
273
274 /* Return to original partition */
275 if (mmc->part_num != original_part) {
276 if (mmc_switch_part(curr_device, original_part) != 0)
277 return CMD_RET_FAILURE;
278 mmc->part_num = original_part;
279 }
280 return ret;
281}
282#endif
283
284static int do_mmc_read(cmd_tbl_t *cmdtp, int flag,
285 int argc, char * const argv[])
286{
287 struct mmc *mmc;
288 u32 blk, cnt, n;
289 void *addr;
290
291 if (argc != 4)
292 return CMD_RET_USAGE;
293
294 addr = (void *)simple_strtoul(argv[1], NULL, 16);
295 blk = simple_strtoul(argv[2], NULL, 16);
296 cnt = simple_strtoul(argv[3], NULL, 16);
297
Stephen Warren1ae24a52014-05-23 13:24:45 -0600298 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200299 if (!mmc)
300 return CMD_RET_FAILURE;
301
302 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
303 curr_device, blk, cnt);
304
305 n = mmc->block_dev.block_read(curr_device, blk, cnt, addr);
306 /* flush cache after read */
307 flush_cache((ulong)addr, cnt * 512); /* FIXME */
308 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
309
310 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
311}
312static int do_mmc_write(cmd_tbl_t *cmdtp, int flag,
313 int argc, char * const argv[])
314{
315 struct mmc *mmc;
316 u32 blk, cnt, n;
317 void *addr;
318
319 if (argc != 4)
320 return CMD_RET_USAGE;
321
322 addr = (void *)simple_strtoul(argv[1], NULL, 16);
323 blk = simple_strtoul(argv[2], NULL, 16);
324 cnt = simple_strtoul(argv[3], NULL, 16);
325
Stephen Warren1ae24a52014-05-23 13:24:45 -0600326 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200327 if (!mmc)
328 return CMD_RET_FAILURE;
329
330 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
331 curr_device, blk, cnt);
332
333 if (mmc_getwp(mmc) == 1) {
334 printf("Error: card is write protected!\n");
335 return CMD_RET_FAILURE;
336 }
337 n = mmc->block_dev.block_write(curr_device, blk, cnt, addr);
338 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
339
340 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
341}
342static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,
343 int argc, char * const argv[])
344{
345 struct mmc *mmc;
346 u32 blk, cnt, n;
347
348 if (argc != 3)
349 return CMD_RET_USAGE;
350
351 blk = simple_strtoul(argv[1], NULL, 16);
352 cnt = simple_strtoul(argv[2], NULL, 16);
353
Stephen Warren1ae24a52014-05-23 13:24:45 -0600354 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200355 if (!mmc)
356 return CMD_RET_FAILURE;
357
358 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
359 curr_device, blk, cnt);
360
361 if (mmc_getwp(mmc) == 1) {
362 printf("Error: card is write protected!\n");
363 return CMD_RET_FAILURE;
364 }
365 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
366 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
367
368 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
369}
370static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
371 int argc, char * const argv[])
372{
373 struct mmc *mmc;
374
Stephen Warren941944e2014-05-23 13:24:46 -0600375 mmc = init_mmc_device(curr_device, true);
376 if (!mmc)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200377 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200378
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200379 return CMD_RET_SUCCESS;
380}
381static int do_mmc_part(cmd_tbl_t *cmdtp, int flag,
382 int argc, char * const argv[])
383{
384 block_dev_desc_t *mmc_dev;
385 struct mmc *mmc;
386
Stephen Warren1ae24a52014-05-23 13:24:45 -0600387 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200388 if (!mmc)
389 return CMD_RET_FAILURE;
390
391 mmc_dev = mmc_get_dev(curr_device);
392 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
393 print_part(mmc_dev);
394 return CMD_RET_SUCCESS;
395 }
396
397 puts("get mmc type error!\n");
398 return CMD_RET_FAILURE;
399}
400static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag,
401 int argc, char * const argv[])
402{
Stephen Warren60dc58f2014-05-23 12:48:10 -0600403 int dev, part = 0, ret;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200404 struct mmc *mmc;
405
406 if (argc == 1) {
407 dev = curr_device;
408 } else if (argc == 2) {
409 dev = simple_strtoul(argv[1], NULL, 10);
410 } else if (argc == 3) {
411 dev = (int)simple_strtoul(argv[1], NULL, 10);
412 part = (int)simple_strtoul(argv[2], NULL, 10);
413 if (part > PART_ACCESS_MASK) {
414 printf("#part_num shouldn't be larger than %d\n",
415 PART_ACCESS_MASK);
416 return CMD_RET_FAILURE;
417 }
418 } else {
419 return CMD_RET_USAGE;
420 }
421
Stephen Warrena5710922014-05-23 13:24:47 -0600422 mmc = init_mmc_device(dev, true);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200423 if (!mmc)
424 return CMD_RET_FAILURE;
425
Stephen Warren60dc58f2014-05-23 12:48:10 -0600426 ret = mmc_select_hwpart(dev, part);
427 printf("switch to partitions #%d, %s\n",
428 part, (!ret) ? "OK" : "ERROR");
429 if (ret)
430 return 1;
431
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200432 curr_device = dev;
433 if (mmc->part_config == MMCPART_NOAVAILABLE)
434 printf("mmc%d is current device\n", curr_device);
435 else
436 printf("mmc%d(part %d) is current device\n",
437 curr_device, mmc->part_num);
438
439 return CMD_RET_SUCCESS;
440}
441static int do_mmc_list(cmd_tbl_t *cmdtp, int flag,
442 int argc, char * const argv[])
443{
444 print_mmc_devices('\n');
445 return CMD_RET_SUCCESS;
446}
447#ifdef CONFIG_SUPPORT_EMMC_BOOT
448static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag,
449 int argc, char * const argv[])
450{
451 int dev;
452 struct mmc *mmc;
453 u8 width, reset, mode;
454
455 if (argc != 5)
456 return CMD_RET_USAGE;
457 dev = simple_strtoul(argv[1], NULL, 10);
458 width = simple_strtoul(argv[2], NULL, 10);
459 reset = simple_strtoul(argv[3], NULL, 10);
460 mode = simple_strtoul(argv[4], NULL, 10);
461
Stephen Warren1ae24a52014-05-23 13:24:45 -0600462 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200463 if (!mmc)
464 return CMD_RET_FAILURE;
465
466 if (IS_SD(mmc)) {
467 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
468 return CMD_RET_FAILURE;
469 }
470
471 /* acknowledge to be sent during boot operation */
472 return mmc_set_boot_bus_width(mmc, width, reset, mode);
473}
474static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag,
475 int argc, char * const argv[])
476{
477 int dev;
478 struct mmc *mmc;
479 u32 bootsize, rpmbsize;
480
481 if (argc != 4)
482 return CMD_RET_USAGE;
483 dev = simple_strtoul(argv[1], NULL, 10);
484 bootsize = simple_strtoul(argv[2], NULL, 10);
485 rpmbsize = simple_strtoul(argv[3], NULL, 10);
486
Stephen Warren1ae24a52014-05-23 13:24:45 -0600487 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200488 if (!mmc)
489 return CMD_RET_FAILURE;
490
491 if (IS_SD(mmc)) {
492 printf("It is not a EMMC device\n");
493 return CMD_RET_FAILURE;
494 }
495
496 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
497 printf("EMMC boot partition Size change Failed.\n");
498 return CMD_RET_FAILURE;
499 }
500
501 printf("EMMC boot partition Size %d MB\n", bootsize);
502 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
503 return CMD_RET_SUCCESS;
504}
505static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag,
506 int argc, char * const argv[])
507{
508 int dev;
509 struct mmc *mmc;
510 u8 ack, part_num, access;
511
512 if (argc != 5)
513 return CMD_RET_USAGE;
514
515 dev = simple_strtoul(argv[1], NULL, 10);
516 ack = simple_strtoul(argv[2], NULL, 10);
517 part_num = simple_strtoul(argv[3], NULL, 10);
518 access = simple_strtoul(argv[4], NULL, 10);
519
Stephen Warren1ae24a52014-05-23 13:24:45 -0600520 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200521 if (!mmc)
522 return CMD_RET_FAILURE;
523
524 if (IS_SD(mmc)) {
525 puts("PARTITION_CONFIG only exists on eMMC\n");
526 return CMD_RET_FAILURE;
527 }
528
529 /* acknowledge to be sent during boot operation */
530 return mmc_set_part_conf(mmc, ack, part_num, access);
531}
532static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag,
533 int argc, char * const argv[])
534{
535 int dev;
536 struct mmc *mmc;
537 u8 enable;
538
539 /*
540 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
541 * The only valid values are 0x0, 0x1 and 0x2 and writing
542 * a value of 0x1 or 0x2 sets the value permanently.
543 */
544 if (argc != 3)
545 return CMD_RET_USAGE;
546
547 dev = simple_strtoul(argv[1], NULL, 10);
548 enable = simple_strtoul(argv[2], NULL, 10);
549
550 if (enable > 2 || enable < 0) {
551 puts("Invalid RST_n_ENABLE value\n");
552 return CMD_RET_USAGE;
553 }
554
Stephen Warren1ae24a52014-05-23 13:24:45 -0600555 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200556 if (!mmc)
557 return CMD_RET_FAILURE;
558
559 if (IS_SD(mmc)) {
560 puts("RST_n_FUNCTION only exists on eMMC\n");
561 return CMD_RET_FAILURE;
562 }
563
564 return mmc_set_rst_n_function(mmc, enable);
565}
566#endif
567static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag,
568 int argc, char * const argv[])
569{
570 struct mmc *mmc;
571 u32 val;
572 int ret;
573
574 if (argc != 2)
575 return CMD_RET_USAGE;
576 val = simple_strtoul(argv[2], NULL, 16);
577
578 mmc = find_mmc_device(curr_device);
579 if (!mmc) {
580 printf("no mmc device at slot %x\n", curr_device);
581 return CMD_RET_FAILURE;
582 }
583 ret = mmc_set_dsr(mmc, val);
584 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
585 if (!ret) {
586 mmc->has_init = 0;
587 if (mmc_init(mmc))
588 return CMD_RET_FAILURE;
589 else
590 return CMD_RET_SUCCESS;
591 }
592 return ret;
593}
594
595static cmd_tbl_t cmd_mmc[] = {
596 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
597 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
598 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
599 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
600 U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
601 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
602 U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
603 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
604#ifdef CONFIG_SUPPORT_EMMC_BOOT
605 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehfa7b8852014-09-25 23:00:16 +0800606 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200607 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
608 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
609#endif
610#ifdef CONFIG_SUPPORT_EMMC_RPMB
611 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
612#endif
613 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
614};
Andy Fleming272cc702008-10-30 16:41:01 -0500615
Kim Phillips088f1b12012-10-29 13:34:31 +0000616static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500617{
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200618 cmd_tbl_t *cp;
Lei Wen6be95cc2011-06-22 17:03:30 +0000619
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200620 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
621
622 /* Drop the mmc command */
623 argc--;
624 argv++;
625
626 if (cp == NULL || argc > cp->maxargs)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000627 return CMD_RET_USAGE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200628 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
629 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500630
Lei Wenea6ebe22011-05-02 16:26:25 +0000631 if (curr_device < 0) {
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200632 if (get_mmc_num() > 0) {
Lei Wenea6ebe22011-05-02 16:26:25 +0000633 curr_device = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200634 } else {
Lei Wenea6ebe22011-05-02 16:26:25 +0000635 puts("No MMC device available\n");
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200636 return CMD_RET_FAILURE;
Lei Wenea6ebe22011-05-02 16:26:25 +0000637 }
638 }
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200639 return cp->cmd(cmdtp, flag, argc, argv);
Andy Fleming272cc702008-10-30 16:41:01 -0500640}
641
642U_BOOT_CMD(
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200643 mmc, 7, 1, do_mmcops,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400644 "MMC sub system",
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200645 "info - display info of the current MMC device\n"
646 "mmc read addr blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000647 "mmc write addr blk# cnt\n"
Lei Wene6f99a52011-06-22 17:03:31 +0000648 "mmc erase blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000649 "mmc rescan\n"
650 "mmc part - lists available partition on current mmc device\n"
Lei Wenbc897b12011-05-02 16:26:26 +0000651 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
Amar2a91c912013-04-27 11:43:00 +0530652 "mmc list - lists available devices\n"
653#ifdef CONFIG_SUPPORT_EMMC_BOOT
Tom Rini5a99b9d2014-02-05 10:24:22 -0500654 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
655 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rinif1fd9572014-02-05 10:24:20 -0500656 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
657 " - Change sizes of boot and RPMB partitions of specified device\n"
Tom Rini792970b2014-02-05 10:24:21 -0500658 "mmc partconf dev boot_ack boot_partition partition_access\n"
659 " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
Tom Rini33ace362014-02-07 14:15:20 -0500660 "mmc rst-function dev value\n"
661 " - Change the RST_n_FUNCTION field of the specified device\n"
662 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behme3511b4e2009-02-18 19:59:39 +0100663#endif
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200664#ifdef CONFIG_SUPPORT_EMMC_RPMB
665 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
666 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
667 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
668 "mmc rpmb counter - read the value of the write counter\n"
669#endif
670 "mmc setdsr <value> - set DSR register value\n"
Amar2a91c912013-04-27 11:43:00 +0530671 );
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200672
673/* Old command kept for compatibility. Same as 'mmc info' */
674U_BOOT_CMD(
675 mmcinfo, 1, 0, do_mmcinfo,
676 "display MMC info",
677 "- display info of the current MMC device"
678);
679
Amar2a91c912013-04-27 11:43:00 +0530680#endif /* !CONFIG_GENERIC_MMC */