blob: bcb03eaa90a4004376eabba24f8797ba0806e834 [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{
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010076 int i;
77
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020078 printf("Device: %s\n", mmc->cfg->name);
Andy Fleming272cc702008-10-30 16:41:01 -050079 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",
Jaehoon Chung64f4a612013-01-29 19:31:16 +000089 (mmc->version >> 8) & 0xf, mmc->version & 0xff);
Andy Fleming272cc702008-10-30 16:41:01 -050090
91 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang940e0782011-01-04 01:04:19 +000092 puts("Capacity: ");
93 print_size(mmc->capacity, "\n");
Andy Fleming272cc702008-10-30 16:41:01 -050094
Andrew Gabbasov786e8f82014-12-01 06:59:09 -060095 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
96 mmc->ddr_mode ? " DDR" : "");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010097
Diego Santa Cruz525ada22014-12-23 10:50:19 +010098 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +010099 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
Diego Santa Cruzbeb98a12014-12-23 10:50:23 +0100100 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100101 puts("User Capacity: ");
Diego Santa Cruzbeb98a12014-12-23 10:50:23 +0100102 print_size(mmc->capacity_user, usr_enh ? " ENH\n" : "\n");
103 if (usr_enh) {
104 puts("User Enhanced Start: ");
105 print_size(mmc->enh_user_start, "\n");
106 puts("User Enhanced Size: ");
107 print_size(mmc->enh_user_size, "\n");
108 }
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100109 puts("Boot Capacity: ");
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100110 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100111 puts("RPMB Capacity: ");
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100112 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100113 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100114 bool is_enh = has_enh &&
115 (mmc->part_attr & EXT_CSD_ENH_GP(i));
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100116 if (mmc->capacity_gp[i]) {
Diego Santa Cruzf289fd72014-12-23 10:50:18 +0100117 printf("GP%i Capacity: ", i+1);
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100118 print_size(mmc->capacity_gp[i],
119 is_enh ? " ENH\n" : "\n");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100120 }
121 }
122 }
Andy Fleming272cc702008-10-30 16:41:01 -0500123}
Stephen Warren1ae24a52014-05-23 13:24:45 -0600124static struct mmc *init_mmc_device(int dev, bool force_init)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200125{
126 struct mmc *mmc;
127 mmc = find_mmc_device(dev);
128 if (!mmc) {
129 printf("no mmc device at slot %x\n", dev);
130 return NULL;
131 }
Stephen Warren1ae24a52014-05-23 13:24:45 -0600132 if (force_init)
133 mmc->has_init = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200134 if (mmc_init(mmc))
135 return NULL;
136 return mmc;
137}
Kim Phillips088f1b12012-10-29 13:34:31 +0000138static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500139{
140 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500141
Lei Wenea6ebe22011-05-02 16:26:25 +0000142 if (curr_device < 0) {
143 if (get_mmc_num() > 0)
144 curr_device = 0;
145 else {
146 puts("No MMC device available\n");
147 return 1;
148 }
149 }
Andy Fleming272cc702008-10-30 16:41:01 -0500150
Stephen Warren1ae24a52014-05-23 13:24:45 -0600151 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200152 if (!mmc)
153 return CMD_RET_FAILURE;
Andy Fleming272cc702008-10-30 16:41:01 -0500154
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200155 print_mmcinfo(mmc);
156 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500157}
158
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200159#ifdef CONFIG_SUPPORT_EMMC_RPMB
160static int confirm_key_prog(void)
161{
162 puts("Warning: Programming authentication key can be done only once !\n"
163 " Use this command only if you are sure of what you are doing,\n"
164 "Really perform the key programming? <y/N> ");
165 if (confirm_yesno())
166 return 1;
167
168 puts("Authentication key programming aborted\n");
169 return 0;
170}
171static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag,
172 int argc, char * const argv[])
173{
174 void *key_addr;
175 struct mmc *mmc = find_mmc_device(curr_device);
176
177 if (argc != 2)
178 return CMD_RET_USAGE;
179
180 key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
181 if (!confirm_key_prog())
182 return CMD_RET_FAILURE;
183 if (mmc_rpmb_set_key(mmc, key_addr)) {
184 printf("ERROR - Key already programmed ?\n");
185 return CMD_RET_FAILURE;
186 }
187 return CMD_RET_SUCCESS;
188}
189static int do_mmcrpmb_read(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 = NULL;
196 struct mmc *mmc = find_mmc_device(curr_device);
197
198 if (argc < 4)
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
205 if (argc == 5)
206 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
207
208 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
209 curr_device, blk, cnt);
210 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
211
212 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
213 if (n != cnt)
214 return CMD_RET_FAILURE;
215 return CMD_RET_SUCCESS;
216}
217static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag,
218 int argc, char * const argv[])
219{
220 u16 blk, cnt;
221 void *addr;
222 int n;
223 void *key_addr;
224 struct mmc *mmc = find_mmc_device(curr_device);
225
226 if (argc != 5)
227 return CMD_RET_USAGE;
228
229 addr = (void *)simple_strtoul(argv[1], NULL, 16);
230 blk = simple_strtoul(argv[2], NULL, 16);
231 cnt = simple_strtoul(argv[3], NULL, 16);
232 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
233
234 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
235 curr_device, blk, cnt);
236 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
237
238 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
239 if (n != cnt)
240 return CMD_RET_FAILURE;
241 return CMD_RET_SUCCESS;
242}
243static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag,
244 int argc, char * const argv[])
245{
246 unsigned long counter;
247 struct mmc *mmc = find_mmc_device(curr_device);
248
249 if (mmc_rpmb_get_counter(mmc, &counter))
250 return CMD_RET_FAILURE;
251 printf("RPMB Write counter= %lx\n", counter);
252 return CMD_RET_SUCCESS;
253}
254
255static cmd_tbl_t cmd_rpmb[] = {
256 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
257 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
258 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
259 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
260};
261
262static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag,
263 int argc, char * const argv[])
264{
265 cmd_tbl_t *cp;
266 struct mmc *mmc;
267 char original_part;
268 int ret;
269
270 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
271
272 /* Drop the rpmb subcommand */
273 argc--;
274 argv++;
275
276 if (cp == NULL || argc > cp->maxargs)
277 return CMD_RET_USAGE;
278 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
279 return CMD_RET_SUCCESS;
280
Stephen Warren1ae24a52014-05-23 13:24:45 -0600281 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200282 if (!mmc)
283 return CMD_RET_FAILURE;
284
285 if (!(mmc->version & MMC_VERSION_MMC)) {
286 printf("It is not a EMMC device\n");
287 return CMD_RET_FAILURE;
288 }
289 if (mmc->version < MMC_VERSION_4_41) {
290 printf("RPMB not supported before version 4.41\n");
291 return CMD_RET_FAILURE;
292 }
293 /* Switch to the RPMB partition */
294 original_part = mmc->part_num;
295 if (mmc->part_num != MMC_PART_RPMB) {
296 if (mmc_switch_part(curr_device, MMC_PART_RPMB) != 0)
297 return CMD_RET_FAILURE;
298 mmc->part_num = MMC_PART_RPMB;
299 }
300 ret = cp->cmd(cmdtp, flag, argc, argv);
301
302 /* Return to original partition */
303 if (mmc->part_num != original_part) {
304 if (mmc_switch_part(curr_device, original_part) != 0)
305 return CMD_RET_FAILURE;
306 mmc->part_num = original_part;
307 }
308 return ret;
309}
310#endif
311
312static int do_mmc_read(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 read: dev # %d, block # %d, count %d ... ",
331 curr_device, blk, cnt);
332
333 n = mmc->block_dev.block_read(curr_device, blk, cnt, addr);
334 /* flush cache after read */
335 flush_cache((ulong)addr, cnt * 512); /* FIXME */
336 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
337
338 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
339}
340static int do_mmc_write(cmd_tbl_t *cmdtp, int flag,
341 int argc, char * const argv[])
342{
343 struct mmc *mmc;
344 u32 blk, cnt, n;
345 void *addr;
346
347 if (argc != 4)
348 return CMD_RET_USAGE;
349
350 addr = (void *)simple_strtoul(argv[1], NULL, 16);
351 blk = simple_strtoul(argv[2], NULL, 16);
352 cnt = simple_strtoul(argv[3], 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 write: 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_write(curr_device, blk, cnt, addr);
366 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
367
368 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
369}
370static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,
371 int argc, char * const argv[])
372{
373 struct mmc *mmc;
374 u32 blk, cnt, n;
375
376 if (argc != 3)
377 return CMD_RET_USAGE;
378
379 blk = simple_strtoul(argv[1], NULL, 16);
380 cnt = simple_strtoul(argv[2], NULL, 16);
381
Stephen Warren1ae24a52014-05-23 13:24:45 -0600382 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200383 if (!mmc)
384 return CMD_RET_FAILURE;
385
386 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
387 curr_device, blk, cnt);
388
389 if (mmc_getwp(mmc) == 1) {
390 printf("Error: card is write protected!\n");
391 return CMD_RET_FAILURE;
392 }
393 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
394 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
395
396 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
397}
398static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
399 int argc, char * const argv[])
400{
401 struct mmc *mmc;
402
Stephen Warren941944e2014-05-23 13:24:46 -0600403 mmc = init_mmc_device(curr_device, true);
404 if (!mmc)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200405 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200406
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200407 return CMD_RET_SUCCESS;
408}
409static int do_mmc_part(cmd_tbl_t *cmdtp, int flag,
410 int argc, char * const argv[])
411{
412 block_dev_desc_t *mmc_dev;
413 struct mmc *mmc;
414
Stephen Warren1ae24a52014-05-23 13:24:45 -0600415 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200416 if (!mmc)
417 return CMD_RET_FAILURE;
418
419 mmc_dev = mmc_get_dev(curr_device);
420 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
421 print_part(mmc_dev);
422 return CMD_RET_SUCCESS;
423 }
424
425 puts("get mmc type error!\n");
426 return CMD_RET_FAILURE;
427}
428static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag,
429 int argc, char * const argv[])
430{
Stephen Warren60dc58f2014-05-23 12:48:10 -0600431 int dev, part = 0, ret;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200432 struct mmc *mmc;
433
434 if (argc == 1) {
435 dev = curr_device;
436 } else if (argc == 2) {
437 dev = simple_strtoul(argv[1], NULL, 10);
438 } else if (argc == 3) {
439 dev = (int)simple_strtoul(argv[1], NULL, 10);
440 part = (int)simple_strtoul(argv[2], NULL, 10);
441 if (part > PART_ACCESS_MASK) {
442 printf("#part_num shouldn't be larger than %d\n",
443 PART_ACCESS_MASK);
444 return CMD_RET_FAILURE;
445 }
446 } else {
447 return CMD_RET_USAGE;
448 }
449
Stephen Warrena5710922014-05-23 13:24:47 -0600450 mmc = init_mmc_device(dev, true);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200451 if (!mmc)
452 return CMD_RET_FAILURE;
453
Stephen Warren60dc58f2014-05-23 12:48:10 -0600454 ret = mmc_select_hwpart(dev, part);
455 printf("switch to partitions #%d, %s\n",
456 part, (!ret) ? "OK" : "ERROR");
457 if (ret)
458 return 1;
459
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200460 curr_device = dev;
461 if (mmc->part_config == MMCPART_NOAVAILABLE)
462 printf("mmc%d is current device\n", curr_device);
463 else
464 printf("mmc%d(part %d) is current device\n",
465 curr_device, mmc->part_num);
466
467 return CMD_RET_SUCCESS;
468}
469static int do_mmc_list(cmd_tbl_t *cmdtp, int flag,
470 int argc, char * const argv[])
471{
472 print_mmc_devices('\n');
473 return CMD_RET_SUCCESS;
474}
475#ifdef CONFIG_SUPPORT_EMMC_BOOT
476static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag,
477 int argc, char * const argv[])
478{
479 int dev;
480 struct mmc *mmc;
481 u8 width, reset, mode;
482
483 if (argc != 5)
484 return CMD_RET_USAGE;
485 dev = simple_strtoul(argv[1], NULL, 10);
486 width = simple_strtoul(argv[2], NULL, 10);
487 reset = simple_strtoul(argv[3], NULL, 10);
488 mode = simple_strtoul(argv[4], NULL, 10);
489
Stephen Warren1ae24a52014-05-23 13:24:45 -0600490 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200491 if (!mmc)
492 return CMD_RET_FAILURE;
493
494 if (IS_SD(mmc)) {
495 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
496 return CMD_RET_FAILURE;
497 }
498
499 /* acknowledge to be sent during boot operation */
500 return mmc_set_boot_bus_width(mmc, width, reset, mode);
501}
502static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag,
503 int argc, char * const argv[])
504{
505 int dev;
506 struct mmc *mmc;
507 u32 bootsize, rpmbsize;
508
509 if (argc != 4)
510 return CMD_RET_USAGE;
511 dev = simple_strtoul(argv[1], NULL, 10);
512 bootsize = simple_strtoul(argv[2], NULL, 10);
513 rpmbsize = simple_strtoul(argv[3], NULL, 10);
514
Stephen Warren1ae24a52014-05-23 13:24:45 -0600515 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200516 if (!mmc)
517 return CMD_RET_FAILURE;
518
519 if (IS_SD(mmc)) {
520 printf("It is not a EMMC device\n");
521 return CMD_RET_FAILURE;
522 }
523
524 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
525 printf("EMMC boot partition Size change Failed.\n");
526 return CMD_RET_FAILURE;
527 }
528
529 printf("EMMC boot partition Size %d MB\n", bootsize);
530 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
531 return CMD_RET_SUCCESS;
532}
533static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag,
534 int argc, char * const argv[])
535{
536 int dev;
537 struct mmc *mmc;
538 u8 ack, part_num, access;
539
540 if (argc != 5)
541 return CMD_RET_USAGE;
542
543 dev = simple_strtoul(argv[1], NULL, 10);
544 ack = simple_strtoul(argv[2], NULL, 10);
545 part_num = simple_strtoul(argv[3], NULL, 10);
546 access = simple_strtoul(argv[4], NULL, 10);
547
Stephen Warren1ae24a52014-05-23 13:24:45 -0600548 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200549 if (!mmc)
550 return CMD_RET_FAILURE;
551
552 if (IS_SD(mmc)) {
553 puts("PARTITION_CONFIG only exists on eMMC\n");
554 return CMD_RET_FAILURE;
555 }
556
557 /* acknowledge to be sent during boot operation */
558 return mmc_set_part_conf(mmc, ack, part_num, access);
559}
560static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag,
561 int argc, char * const argv[])
562{
563 int dev;
564 struct mmc *mmc;
565 u8 enable;
566
567 /*
568 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
569 * The only valid values are 0x0, 0x1 and 0x2 and writing
570 * a value of 0x1 or 0x2 sets the value permanently.
571 */
572 if (argc != 3)
573 return CMD_RET_USAGE;
574
575 dev = simple_strtoul(argv[1], NULL, 10);
576 enable = simple_strtoul(argv[2], NULL, 10);
577
578 if (enable > 2 || enable < 0) {
579 puts("Invalid RST_n_ENABLE value\n");
580 return CMD_RET_USAGE;
581 }
582
Stephen Warren1ae24a52014-05-23 13:24:45 -0600583 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200584 if (!mmc)
585 return CMD_RET_FAILURE;
586
587 if (IS_SD(mmc)) {
588 puts("RST_n_FUNCTION only exists on eMMC\n");
589 return CMD_RET_FAILURE;
590 }
591
592 return mmc_set_rst_n_function(mmc, enable);
593}
594#endif
595static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag,
596 int argc, char * const argv[])
597{
598 struct mmc *mmc;
599 u32 val;
600 int ret;
601
602 if (argc != 2)
603 return CMD_RET_USAGE;
604 val = simple_strtoul(argv[2], NULL, 16);
605
606 mmc = find_mmc_device(curr_device);
607 if (!mmc) {
608 printf("no mmc device at slot %x\n", curr_device);
609 return CMD_RET_FAILURE;
610 }
611 ret = mmc_set_dsr(mmc, val);
612 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
613 if (!ret) {
614 mmc->has_init = 0;
615 if (mmc_init(mmc))
616 return CMD_RET_FAILURE;
617 else
618 return CMD_RET_SUCCESS;
619 }
620 return ret;
621}
622
623static cmd_tbl_t cmd_mmc[] = {
624 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
625 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
626 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
627 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
628 U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
629 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
630 U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
631 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
632#ifdef CONFIG_SUPPORT_EMMC_BOOT
633 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehfa7b8852014-09-25 23:00:16 +0800634 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200635 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
636 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
637#endif
638#ifdef CONFIG_SUPPORT_EMMC_RPMB
639 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
640#endif
641 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
642};
Andy Fleming272cc702008-10-30 16:41:01 -0500643
Kim Phillips088f1b12012-10-29 13:34:31 +0000644static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500645{
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200646 cmd_tbl_t *cp;
Lei Wen6be95cc2011-06-22 17:03:30 +0000647
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200648 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
649
650 /* Drop the mmc command */
651 argc--;
652 argv++;
653
654 if (cp == NULL || argc > cp->maxargs)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000655 return CMD_RET_USAGE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200656 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
657 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500658
Lei Wenea6ebe22011-05-02 16:26:25 +0000659 if (curr_device < 0) {
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200660 if (get_mmc_num() > 0) {
Lei Wenea6ebe22011-05-02 16:26:25 +0000661 curr_device = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200662 } else {
Lei Wenea6ebe22011-05-02 16:26:25 +0000663 puts("No MMC device available\n");
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200664 return CMD_RET_FAILURE;
Lei Wenea6ebe22011-05-02 16:26:25 +0000665 }
666 }
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200667 return cp->cmd(cmdtp, flag, argc, argv);
Andy Fleming272cc702008-10-30 16:41:01 -0500668}
669
670U_BOOT_CMD(
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200671 mmc, 7, 1, do_mmcops,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400672 "MMC sub system",
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200673 "info - display info of the current MMC device\n"
674 "mmc read addr blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000675 "mmc write addr blk# cnt\n"
Lei Wene6f99a52011-06-22 17:03:31 +0000676 "mmc erase blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000677 "mmc rescan\n"
678 "mmc part - lists available partition on current mmc device\n"
Lei Wenbc897b12011-05-02 16:26:26 +0000679 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
Amar2a91c912013-04-27 11:43:00 +0530680 "mmc list - lists available devices\n"
681#ifdef CONFIG_SUPPORT_EMMC_BOOT
Tom Rini5a99b9d2014-02-05 10:24:22 -0500682 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
683 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rinif1fd9572014-02-05 10:24:20 -0500684 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
685 " - Change sizes of boot and RPMB partitions of specified device\n"
Tom Rini792970b2014-02-05 10:24:21 -0500686 "mmc partconf dev boot_ack boot_partition partition_access\n"
687 " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
Tom Rini33ace362014-02-07 14:15:20 -0500688 "mmc rst-function dev value\n"
689 " - Change the RST_n_FUNCTION field of the specified device\n"
690 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behme3511b4e2009-02-18 19:59:39 +0100691#endif
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200692#ifdef CONFIG_SUPPORT_EMMC_RPMB
693 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
694 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
695 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
696 "mmc rpmb counter - read the value of the write counter\n"
697#endif
698 "mmc setdsr <value> - set DSR register value\n"
Amar2a91c912013-04-27 11:43:00 +0530699 );
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200700
701/* Old command kept for compatibility. Same as 'mmc info' */
702U_BOOT_CMD(
703 mmcinfo, 1, 0, do_mmcinfo,
704 "display MMC info",
705 "- display info of the current MMC device"
706);
707
Amar2a91c912013-04-27 11:43:00 +0530708#endif /* !CONFIG_GENERIC_MMC */