blob: 8c03e583e5c58098357f7204340e81b5e481d0dd [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 Cruzc5f0d3f2014-12-23 10:50:16 +0100100 puts("User Capacity: ");
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100101 print_size(mmc->capacity_user,
102 has_enh && (mmc->part_attr & EXT_CSD_ENH_USR) ?
103 " ENH\n" : "\n");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100104 puts("Boot Capacity: ");
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100105 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100106 puts("RPMB Capacity: ");
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100107 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100108 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100109 bool is_enh = has_enh &&
110 (mmc->part_attr & EXT_CSD_ENH_GP(i));
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100111 if (mmc->capacity_gp[i]) {
Diego Santa Cruzf289fd72014-12-23 10:50:18 +0100112 printf("GP%i Capacity: ", i+1);
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100113 print_size(mmc->capacity_gp[i],
114 is_enh ? " ENH\n" : "\n");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100115 }
116 }
117 }
Andy Fleming272cc702008-10-30 16:41:01 -0500118}
Stephen Warren1ae24a52014-05-23 13:24:45 -0600119static struct mmc *init_mmc_device(int dev, bool force_init)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200120{
121 struct mmc *mmc;
122 mmc = find_mmc_device(dev);
123 if (!mmc) {
124 printf("no mmc device at slot %x\n", dev);
125 return NULL;
126 }
Stephen Warren1ae24a52014-05-23 13:24:45 -0600127 if (force_init)
128 mmc->has_init = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200129 if (mmc_init(mmc))
130 return NULL;
131 return mmc;
132}
Kim Phillips088f1b12012-10-29 13:34:31 +0000133static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500134{
135 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500136
Lei Wenea6ebe22011-05-02 16:26:25 +0000137 if (curr_device < 0) {
138 if (get_mmc_num() > 0)
139 curr_device = 0;
140 else {
141 puts("No MMC device available\n");
142 return 1;
143 }
144 }
Andy Fleming272cc702008-10-30 16:41:01 -0500145
Stephen Warren1ae24a52014-05-23 13:24:45 -0600146 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200147 if (!mmc)
148 return CMD_RET_FAILURE;
Andy Fleming272cc702008-10-30 16:41:01 -0500149
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200150 print_mmcinfo(mmc);
151 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500152}
153
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200154#ifdef CONFIG_SUPPORT_EMMC_RPMB
155static int confirm_key_prog(void)
156{
157 puts("Warning: Programming authentication key can be done only once !\n"
158 " Use this command only if you are sure of what you are doing,\n"
159 "Really perform the key programming? <y/N> ");
160 if (confirm_yesno())
161 return 1;
162
163 puts("Authentication key programming aborted\n");
164 return 0;
165}
166static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag,
167 int argc, char * const argv[])
168{
169 void *key_addr;
170 struct mmc *mmc = find_mmc_device(curr_device);
171
172 if (argc != 2)
173 return CMD_RET_USAGE;
174
175 key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
176 if (!confirm_key_prog())
177 return CMD_RET_FAILURE;
178 if (mmc_rpmb_set_key(mmc, key_addr)) {
179 printf("ERROR - Key already programmed ?\n");
180 return CMD_RET_FAILURE;
181 }
182 return CMD_RET_SUCCESS;
183}
184static int do_mmcrpmb_read(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 = NULL;
191 struct mmc *mmc = find_mmc_device(curr_device);
192
193 if (argc < 4)
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
200 if (argc == 5)
201 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
202
203 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
204 curr_device, blk, cnt);
205 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
206
207 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
208 if (n != cnt)
209 return CMD_RET_FAILURE;
210 return CMD_RET_SUCCESS;
211}
212static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag,
213 int argc, char * const argv[])
214{
215 u16 blk, cnt;
216 void *addr;
217 int n;
218 void *key_addr;
219 struct mmc *mmc = find_mmc_device(curr_device);
220
221 if (argc != 5)
222 return CMD_RET_USAGE;
223
224 addr = (void *)simple_strtoul(argv[1], NULL, 16);
225 blk = simple_strtoul(argv[2], NULL, 16);
226 cnt = simple_strtoul(argv[3], NULL, 16);
227 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
228
229 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
230 curr_device, blk, cnt);
231 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
232
233 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
234 if (n != cnt)
235 return CMD_RET_FAILURE;
236 return CMD_RET_SUCCESS;
237}
238static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag,
239 int argc, char * const argv[])
240{
241 unsigned long counter;
242 struct mmc *mmc = find_mmc_device(curr_device);
243
244 if (mmc_rpmb_get_counter(mmc, &counter))
245 return CMD_RET_FAILURE;
246 printf("RPMB Write counter= %lx\n", counter);
247 return CMD_RET_SUCCESS;
248}
249
250static cmd_tbl_t cmd_rpmb[] = {
251 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
252 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
253 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
254 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
255};
256
257static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag,
258 int argc, char * const argv[])
259{
260 cmd_tbl_t *cp;
261 struct mmc *mmc;
262 char original_part;
263 int ret;
264
265 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
266
267 /* Drop the rpmb subcommand */
268 argc--;
269 argv++;
270
271 if (cp == NULL || argc > cp->maxargs)
272 return CMD_RET_USAGE;
273 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
274 return CMD_RET_SUCCESS;
275
Stephen Warren1ae24a52014-05-23 13:24:45 -0600276 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200277 if (!mmc)
278 return CMD_RET_FAILURE;
279
280 if (!(mmc->version & MMC_VERSION_MMC)) {
281 printf("It is not a EMMC device\n");
282 return CMD_RET_FAILURE;
283 }
284 if (mmc->version < MMC_VERSION_4_41) {
285 printf("RPMB not supported before version 4.41\n");
286 return CMD_RET_FAILURE;
287 }
288 /* Switch to the RPMB partition */
289 original_part = mmc->part_num;
290 if (mmc->part_num != MMC_PART_RPMB) {
291 if (mmc_switch_part(curr_device, MMC_PART_RPMB) != 0)
292 return CMD_RET_FAILURE;
293 mmc->part_num = MMC_PART_RPMB;
294 }
295 ret = cp->cmd(cmdtp, flag, argc, argv);
296
297 /* Return to original partition */
298 if (mmc->part_num != original_part) {
299 if (mmc_switch_part(curr_device, original_part) != 0)
300 return CMD_RET_FAILURE;
301 mmc->part_num = original_part;
302 }
303 return ret;
304}
305#endif
306
307static int do_mmc_read(cmd_tbl_t *cmdtp, int flag,
308 int argc, char * const argv[])
309{
310 struct mmc *mmc;
311 u32 blk, cnt, n;
312 void *addr;
313
314 if (argc != 4)
315 return CMD_RET_USAGE;
316
317 addr = (void *)simple_strtoul(argv[1], NULL, 16);
318 blk = simple_strtoul(argv[2], NULL, 16);
319 cnt = simple_strtoul(argv[3], NULL, 16);
320
Stephen Warren1ae24a52014-05-23 13:24:45 -0600321 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200322 if (!mmc)
323 return CMD_RET_FAILURE;
324
325 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
326 curr_device, blk, cnt);
327
328 n = mmc->block_dev.block_read(curr_device, blk, cnt, addr);
329 /* flush cache after read */
330 flush_cache((ulong)addr, cnt * 512); /* FIXME */
331 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
332
333 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
334}
335static int do_mmc_write(cmd_tbl_t *cmdtp, int flag,
336 int argc, char * const argv[])
337{
338 struct mmc *mmc;
339 u32 blk, cnt, n;
340 void *addr;
341
342 if (argc != 4)
343 return CMD_RET_USAGE;
344
345 addr = (void *)simple_strtoul(argv[1], NULL, 16);
346 blk = simple_strtoul(argv[2], NULL, 16);
347 cnt = simple_strtoul(argv[3], NULL, 16);
348
Stephen Warren1ae24a52014-05-23 13:24:45 -0600349 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200350 if (!mmc)
351 return CMD_RET_FAILURE;
352
353 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
354 curr_device, blk, cnt);
355
356 if (mmc_getwp(mmc) == 1) {
357 printf("Error: card is write protected!\n");
358 return CMD_RET_FAILURE;
359 }
360 n = mmc->block_dev.block_write(curr_device, blk, cnt, addr);
361 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
362
363 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
364}
365static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,
366 int argc, char * const argv[])
367{
368 struct mmc *mmc;
369 u32 blk, cnt, n;
370
371 if (argc != 3)
372 return CMD_RET_USAGE;
373
374 blk = simple_strtoul(argv[1], NULL, 16);
375 cnt = simple_strtoul(argv[2], NULL, 16);
376
Stephen Warren1ae24a52014-05-23 13:24:45 -0600377 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200378 if (!mmc)
379 return CMD_RET_FAILURE;
380
381 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
382 curr_device, blk, cnt);
383
384 if (mmc_getwp(mmc) == 1) {
385 printf("Error: card is write protected!\n");
386 return CMD_RET_FAILURE;
387 }
388 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
389 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
390
391 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
392}
393static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
394 int argc, char * const argv[])
395{
396 struct mmc *mmc;
397
Stephen Warren941944e2014-05-23 13:24:46 -0600398 mmc = init_mmc_device(curr_device, true);
399 if (!mmc)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200400 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200401
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200402 return CMD_RET_SUCCESS;
403}
404static int do_mmc_part(cmd_tbl_t *cmdtp, int flag,
405 int argc, char * const argv[])
406{
407 block_dev_desc_t *mmc_dev;
408 struct mmc *mmc;
409
Stephen Warren1ae24a52014-05-23 13:24:45 -0600410 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200411 if (!mmc)
412 return CMD_RET_FAILURE;
413
414 mmc_dev = mmc_get_dev(curr_device);
415 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
416 print_part(mmc_dev);
417 return CMD_RET_SUCCESS;
418 }
419
420 puts("get mmc type error!\n");
421 return CMD_RET_FAILURE;
422}
423static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag,
424 int argc, char * const argv[])
425{
Stephen Warren60dc58f2014-05-23 12:48:10 -0600426 int dev, part = 0, ret;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200427 struct mmc *mmc;
428
429 if (argc == 1) {
430 dev = curr_device;
431 } else if (argc == 2) {
432 dev = simple_strtoul(argv[1], NULL, 10);
433 } else if (argc == 3) {
434 dev = (int)simple_strtoul(argv[1], NULL, 10);
435 part = (int)simple_strtoul(argv[2], NULL, 10);
436 if (part > PART_ACCESS_MASK) {
437 printf("#part_num shouldn't be larger than %d\n",
438 PART_ACCESS_MASK);
439 return CMD_RET_FAILURE;
440 }
441 } else {
442 return CMD_RET_USAGE;
443 }
444
Stephen Warrena5710922014-05-23 13:24:47 -0600445 mmc = init_mmc_device(dev, true);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200446 if (!mmc)
447 return CMD_RET_FAILURE;
448
Stephen Warren60dc58f2014-05-23 12:48:10 -0600449 ret = mmc_select_hwpart(dev, part);
450 printf("switch to partitions #%d, %s\n",
451 part, (!ret) ? "OK" : "ERROR");
452 if (ret)
453 return 1;
454
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200455 curr_device = dev;
456 if (mmc->part_config == MMCPART_NOAVAILABLE)
457 printf("mmc%d is current device\n", curr_device);
458 else
459 printf("mmc%d(part %d) is current device\n",
460 curr_device, mmc->part_num);
461
462 return CMD_RET_SUCCESS;
463}
464static int do_mmc_list(cmd_tbl_t *cmdtp, int flag,
465 int argc, char * const argv[])
466{
467 print_mmc_devices('\n');
468 return CMD_RET_SUCCESS;
469}
470#ifdef CONFIG_SUPPORT_EMMC_BOOT
471static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag,
472 int argc, char * const argv[])
473{
474 int dev;
475 struct mmc *mmc;
476 u8 width, reset, mode;
477
478 if (argc != 5)
479 return CMD_RET_USAGE;
480 dev = simple_strtoul(argv[1], NULL, 10);
481 width = simple_strtoul(argv[2], NULL, 10);
482 reset = simple_strtoul(argv[3], NULL, 10);
483 mode = simple_strtoul(argv[4], NULL, 10);
484
Stephen Warren1ae24a52014-05-23 13:24:45 -0600485 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200486 if (!mmc)
487 return CMD_RET_FAILURE;
488
489 if (IS_SD(mmc)) {
490 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
491 return CMD_RET_FAILURE;
492 }
493
494 /* acknowledge to be sent during boot operation */
495 return mmc_set_boot_bus_width(mmc, width, reset, mode);
496}
497static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag,
498 int argc, char * const argv[])
499{
500 int dev;
501 struct mmc *mmc;
502 u32 bootsize, rpmbsize;
503
504 if (argc != 4)
505 return CMD_RET_USAGE;
506 dev = simple_strtoul(argv[1], NULL, 10);
507 bootsize = simple_strtoul(argv[2], NULL, 10);
508 rpmbsize = simple_strtoul(argv[3], NULL, 10);
509
Stephen Warren1ae24a52014-05-23 13:24:45 -0600510 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200511 if (!mmc)
512 return CMD_RET_FAILURE;
513
514 if (IS_SD(mmc)) {
515 printf("It is not a EMMC device\n");
516 return CMD_RET_FAILURE;
517 }
518
519 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
520 printf("EMMC boot partition Size change Failed.\n");
521 return CMD_RET_FAILURE;
522 }
523
524 printf("EMMC boot partition Size %d MB\n", bootsize);
525 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
526 return CMD_RET_SUCCESS;
527}
528static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag,
529 int argc, char * const argv[])
530{
531 int dev;
532 struct mmc *mmc;
533 u8 ack, part_num, access;
534
535 if (argc != 5)
536 return CMD_RET_USAGE;
537
538 dev = simple_strtoul(argv[1], NULL, 10);
539 ack = simple_strtoul(argv[2], NULL, 10);
540 part_num = simple_strtoul(argv[3], NULL, 10);
541 access = simple_strtoul(argv[4], NULL, 10);
542
Stephen Warren1ae24a52014-05-23 13:24:45 -0600543 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200544 if (!mmc)
545 return CMD_RET_FAILURE;
546
547 if (IS_SD(mmc)) {
548 puts("PARTITION_CONFIG only exists on eMMC\n");
549 return CMD_RET_FAILURE;
550 }
551
552 /* acknowledge to be sent during boot operation */
553 return mmc_set_part_conf(mmc, ack, part_num, access);
554}
555static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag,
556 int argc, char * const argv[])
557{
558 int dev;
559 struct mmc *mmc;
560 u8 enable;
561
562 /*
563 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
564 * The only valid values are 0x0, 0x1 and 0x2 and writing
565 * a value of 0x1 or 0x2 sets the value permanently.
566 */
567 if (argc != 3)
568 return CMD_RET_USAGE;
569
570 dev = simple_strtoul(argv[1], NULL, 10);
571 enable = simple_strtoul(argv[2], NULL, 10);
572
573 if (enable > 2 || enable < 0) {
574 puts("Invalid RST_n_ENABLE value\n");
575 return CMD_RET_USAGE;
576 }
577
Stephen Warren1ae24a52014-05-23 13:24:45 -0600578 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200579 if (!mmc)
580 return CMD_RET_FAILURE;
581
582 if (IS_SD(mmc)) {
583 puts("RST_n_FUNCTION only exists on eMMC\n");
584 return CMD_RET_FAILURE;
585 }
586
587 return mmc_set_rst_n_function(mmc, enable);
588}
589#endif
590static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag,
591 int argc, char * const argv[])
592{
593 struct mmc *mmc;
594 u32 val;
595 int ret;
596
597 if (argc != 2)
598 return CMD_RET_USAGE;
599 val = simple_strtoul(argv[2], NULL, 16);
600
601 mmc = find_mmc_device(curr_device);
602 if (!mmc) {
603 printf("no mmc device at slot %x\n", curr_device);
604 return CMD_RET_FAILURE;
605 }
606 ret = mmc_set_dsr(mmc, val);
607 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
608 if (!ret) {
609 mmc->has_init = 0;
610 if (mmc_init(mmc))
611 return CMD_RET_FAILURE;
612 else
613 return CMD_RET_SUCCESS;
614 }
615 return ret;
616}
617
618static cmd_tbl_t cmd_mmc[] = {
619 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
620 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
621 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
622 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
623 U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
624 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
625 U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
626 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
627#ifdef CONFIG_SUPPORT_EMMC_BOOT
628 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehfa7b8852014-09-25 23:00:16 +0800629 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200630 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
631 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
632#endif
633#ifdef CONFIG_SUPPORT_EMMC_RPMB
634 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
635#endif
636 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
637};
Andy Fleming272cc702008-10-30 16:41:01 -0500638
Kim Phillips088f1b12012-10-29 13:34:31 +0000639static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500640{
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200641 cmd_tbl_t *cp;
Lei Wen6be95cc2011-06-22 17:03:30 +0000642
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200643 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
644
645 /* Drop the mmc command */
646 argc--;
647 argv++;
648
649 if (cp == NULL || argc > cp->maxargs)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000650 return CMD_RET_USAGE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200651 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
652 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500653
Lei Wenea6ebe22011-05-02 16:26:25 +0000654 if (curr_device < 0) {
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200655 if (get_mmc_num() > 0) {
Lei Wenea6ebe22011-05-02 16:26:25 +0000656 curr_device = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200657 } else {
Lei Wenea6ebe22011-05-02 16:26:25 +0000658 puts("No MMC device available\n");
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200659 return CMD_RET_FAILURE;
Lei Wenea6ebe22011-05-02 16:26:25 +0000660 }
661 }
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200662 return cp->cmd(cmdtp, flag, argc, argv);
Andy Fleming272cc702008-10-30 16:41:01 -0500663}
664
665U_BOOT_CMD(
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200666 mmc, 7, 1, do_mmcops,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400667 "MMC sub system",
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200668 "info - display info of the current MMC device\n"
669 "mmc read addr blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000670 "mmc write addr blk# cnt\n"
Lei Wene6f99a52011-06-22 17:03:31 +0000671 "mmc erase blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000672 "mmc rescan\n"
673 "mmc part - lists available partition on current mmc device\n"
Lei Wenbc897b12011-05-02 16:26:26 +0000674 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
Amar2a91c912013-04-27 11:43:00 +0530675 "mmc list - lists available devices\n"
676#ifdef CONFIG_SUPPORT_EMMC_BOOT
Tom Rini5a99b9d2014-02-05 10:24:22 -0500677 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
678 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rinif1fd9572014-02-05 10:24:20 -0500679 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
680 " - Change sizes of boot and RPMB partitions of specified device\n"
Tom Rini792970b2014-02-05 10:24:21 -0500681 "mmc partconf dev boot_ack boot_partition partition_access\n"
682 " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
Tom Rini33ace362014-02-07 14:15:20 -0500683 "mmc rst-function dev value\n"
684 " - Change the RST_n_FUNCTION field of the specified device\n"
685 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behme3511b4e2009-02-18 19:59:39 +0100686#endif
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200687#ifdef CONFIG_SUPPORT_EMMC_RPMB
688 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
689 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
690 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
691 "mmc rpmb counter - read the value of the write counter\n"
692#endif
693 "mmc setdsr <value> - set DSR register value\n"
Amar2a91c912013-04-27 11:43:00 +0530694 );
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200695
696/* Old command kept for compatibility. Same as 'mmc info' */
697U_BOOT_CMD(
698 mmcinfo, 1, 0, do_mmcinfo,
699 "display MMC info",
700 "- display info of the current MMC device"
701);
702
Amar2a91c912013-04-27 11:43:00 +0530703#endif /* !CONFIG_GENERIC_MMC */