blob: dfc1ec850e20a939bcecf24c1d377da84d034fd7 [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>
Simon Glass24b852a2015-11-08 23:47:45 -070010#include <console.h>
wdenk71f95112003-06-15 22:40:42 +000011#include <mmc.h>
12
Mike Frysinger02e22c22009-06-14 21:35:21 -040013static int curr_device = -1;
Lei Wenea6ebe22011-05-02 16:26:25 +000014#ifndef CONFIG_GENERIC_MMC
Wolfgang Denk54841ab2010-06-28 22:00:46 +020015int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk71f95112003-06-15 22:40:42 +000016{
Minkyu Kang869f6bf2009-03-30 14:55:51 +090017 int dev;
18
Wolfgang Denk47e26b12010-07-17 01:06:04 +020019 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +000020 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090021
22 if (strcmp(argv[1], "init") == 0) {
23 if (argc == 2) {
24 if (curr_device < 0)
25 dev = 1;
26 else
27 dev = curr_device;
28 } else if (argc == 3) {
29 dev = (int)simple_strtoul(argv[2], NULL, 10);
30 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +000031 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090032 }
33
34 if (mmc_legacy_init(dev) != 0) {
35 puts("No MMC card found\n");
36 return 1;
37 }
38
39 curr_device = dev;
40 printf("mmc%d is available\n", curr_device);
41 } else if (strcmp(argv[1], "device") == 0) {
42 if (argc == 2) {
43 if (curr_device < 0) {
44 puts("No MMC device available\n");
45 return 1;
46 }
47 } else if (argc == 3) {
48 dev = (int)simple_strtoul(argv[2], NULL, 10);
49
50#ifdef CONFIG_SYS_MMC_SET_DEV
51 if (mmc_set_dev(dev) != 0)
52 return 1;
53#endif
54 curr_device = dev;
55 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +000056 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090057 }
58
59 printf("mmc%d is current device\n", curr_device);
60 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +000061 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090062 }
63
wdenk71f95112003-06-15 22:40:42 +000064 return 0;
65}
66
wdenk0d498392003-07-01 21:06:45 +000067U_BOOT_CMD(
Minkyu Kang869f6bf2009-03-30 14:55:51 +090068 mmc, 3, 1, do_mmc,
69 "MMC sub-system",
70 "init [dev] - init MMC sub system\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +020071 "mmc device [dev] - show or set current device"
wdenkb0fce992003-06-29 21:03:46 +000072);
Dirk Behme3511b4e2009-02-18 19:59:39 +010073#else /* !CONFIG_GENERIC_MMC */
Andy Fleming272cc702008-10-30 16:41:01 -050074
75static void print_mmcinfo(struct mmc *mmc)
76{
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010077 int i;
78
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020079 printf("Device: %s\n", mmc->cfg->name);
Andy Fleming272cc702008-10-30 16:41:01 -050080 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
81 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
82 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
83 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
84 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
85
86 printf("Tran Speed: %d\n", mmc->tran_speed);
87 printf("Rd Block Len: %d\n", mmc->read_bl_len);
88
Pantelis Antoniou4b7cee52015-01-23 12:12:01 +020089 printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
90 EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
91 EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
92 if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
93 printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
94 printf("\n");
Andy Fleming272cc702008-10-30 16:41:01 -050095
96 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang940e0782011-01-04 01:04:19 +000097 puts("Capacity: ");
98 print_size(mmc->capacity, "\n");
Andy Fleming272cc702008-10-30 16:41:01 -050099
Andrew Gabbasov786e8f82014-12-01 06:59:09 -0600100 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
101 mmc->ddr_mode ? " DDR" : "");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100102
Diego Santa Cruzb0361522014-12-23 10:50:26 +0100103 puts("Erase Group Size: ");
104 print_size(((u64)mmc->erase_grp_size) << 9, "\n");
105
Diego Santa Cruz525ada22014-12-23 10:50:19 +0100106 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100107 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
Diego Santa Cruzbeb98a12014-12-23 10:50:23 +0100108 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
Diego Santa Cruzb0361522014-12-23 10:50:26 +0100109
110 puts("HC WP Group Size: ");
111 print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
112
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100113 puts("User Capacity: ");
Diego Santa Cruz9e41a002014-12-23 10:50:33 +0100114 print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
115 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
116 puts(" WRREL\n");
117 else
118 putc('\n');
Diego Santa Cruzbeb98a12014-12-23 10:50:23 +0100119 if (usr_enh) {
120 puts("User Enhanced Start: ");
121 print_size(mmc->enh_user_start, "\n");
122 puts("User Enhanced Size: ");
123 print_size(mmc->enh_user_size, "\n");
124 }
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100125 puts("Boot Capacity: ");
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100126 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100127 puts("RPMB Capacity: ");
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100128 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
Diego Santa Cruzb0361522014-12-23 10:50:26 +0100129
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100130 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100131 bool is_enh = has_enh &&
132 (mmc->part_attr & EXT_CSD_ENH_GP(i));
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100133 if (mmc->capacity_gp[i]) {
Diego Santa Cruzf289fd72014-12-23 10:50:18 +0100134 printf("GP%i Capacity: ", i+1);
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100135 print_size(mmc->capacity_gp[i],
Diego Santa Cruz9e41a002014-12-23 10:50:33 +0100136 is_enh ? " ENH" : "");
137 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
138 puts(" WRREL\n");
139 else
140 putc('\n');
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100141 }
142 }
143 }
Andy Fleming272cc702008-10-30 16:41:01 -0500144}
Stephen Warren1ae24a52014-05-23 13:24:45 -0600145static struct mmc *init_mmc_device(int dev, bool force_init)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200146{
147 struct mmc *mmc;
148 mmc = find_mmc_device(dev);
149 if (!mmc) {
150 printf("no mmc device at slot %x\n", dev);
151 return NULL;
152 }
Stephen Warren1ae24a52014-05-23 13:24:45 -0600153 if (force_init)
154 mmc->has_init = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200155 if (mmc_init(mmc))
156 return NULL;
157 return mmc;
158}
Kim Phillips088f1b12012-10-29 13:34:31 +0000159static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500160{
161 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500162
Lei Wenea6ebe22011-05-02 16:26:25 +0000163 if (curr_device < 0) {
164 if (get_mmc_num() > 0)
165 curr_device = 0;
166 else {
167 puts("No MMC device available\n");
168 return 1;
169 }
170 }
Andy Fleming272cc702008-10-30 16:41:01 -0500171
Stephen Warren1ae24a52014-05-23 13:24:45 -0600172 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200173 if (!mmc)
174 return CMD_RET_FAILURE;
Andy Fleming272cc702008-10-30 16:41:01 -0500175
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200176 print_mmcinfo(mmc);
177 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500178}
179
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200180#ifdef CONFIG_SUPPORT_EMMC_RPMB
181static int confirm_key_prog(void)
182{
183 puts("Warning: Programming authentication key can be done only once !\n"
184 " Use this command only if you are sure of what you are doing,\n"
185 "Really perform the key programming? <y/N> ");
186 if (confirm_yesno())
187 return 1;
188
189 puts("Authentication key programming aborted\n");
190 return 0;
191}
192static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag,
193 int argc, char * const argv[])
194{
195 void *key_addr;
196 struct mmc *mmc = find_mmc_device(curr_device);
197
198 if (argc != 2)
199 return CMD_RET_USAGE;
200
201 key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
202 if (!confirm_key_prog())
203 return CMD_RET_FAILURE;
204 if (mmc_rpmb_set_key(mmc, key_addr)) {
205 printf("ERROR - Key already programmed ?\n");
206 return CMD_RET_FAILURE;
207 }
208 return CMD_RET_SUCCESS;
209}
210static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag,
211 int argc, char * const argv[])
212{
213 u16 blk, cnt;
214 void *addr;
215 int n;
216 void *key_addr = NULL;
217 struct mmc *mmc = find_mmc_device(curr_device);
218
219 if (argc < 4)
220 return CMD_RET_USAGE;
221
222 addr = (void *)simple_strtoul(argv[1], NULL, 16);
223 blk = simple_strtoul(argv[2], NULL, 16);
224 cnt = simple_strtoul(argv[3], NULL, 16);
225
226 if (argc == 5)
227 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
228
229 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
230 curr_device, blk, cnt);
231 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
232
233 printf("%d RPMB blocks read: %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_write(cmd_tbl_t *cmdtp, int flag,
239 int argc, char * const argv[])
240{
241 u16 blk, cnt;
242 void *addr;
243 int n;
244 void *key_addr;
245 struct mmc *mmc = find_mmc_device(curr_device);
246
247 if (argc != 5)
248 return CMD_RET_USAGE;
249
250 addr = (void *)simple_strtoul(argv[1], NULL, 16);
251 blk = simple_strtoul(argv[2], NULL, 16);
252 cnt = simple_strtoul(argv[3], NULL, 16);
253 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
254
255 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
256 curr_device, blk, cnt);
257 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
258
259 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
260 if (n != cnt)
261 return CMD_RET_FAILURE;
262 return CMD_RET_SUCCESS;
263}
264static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag,
265 int argc, char * const argv[])
266{
267 unsigned long counter;
268 struct mmc *mmc = find_mmc_device(curr_device);
269
270 if (mmc_rpmb_get_counter(mmc, &counter))
271 return CMD_RET_FAILURE;
272 printf("RPMB Write counter= %lx\n", counter);
273 return CMD_RET_SUCCESS;
274}
275
276static cmd_tbl_t cmd_rpmb[] = {
277 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
278 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
279 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
280 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
281};
282
283static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag,
284 int argc, char * const argv[])
285{
286 cmd_tbl_t *cp;
287 struct mmc *mmc;
288 char original_part;
289 int ret;
290
291 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
292
293 /* Drop the rpmb subcommand */
294 argc--;
295 argv++;
296
297 if (cp == NULL || argc > cp->maxargs)
298 return CMD_RET_USAGE;
299 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
300 return CMD_RET_SUCCESS;
301
Stephen Warren1ae24a52014-05-23 13:24:45 -0600302 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200303 if (!mmc)
304 return CMD_RET_FAILURE;
305
306 if (!(mmc->version & MMC_VERSION_MMC)) {
307 printf("It is not a EMMC device\n");
308 return CMD_RET_FAILURE;
309 }
310 if (mmc->version < MMC_VERSION_4_41) {
311 printf("RPMB not supported before version 4.41\n");
312 return CMD_RET_FAILURE;
313 }
314 /* Switch to the RPMB partition */
315 original_part = mmc->part_num;
316 if (mmc->part_num != MMC_PART_RPMB) {
317 if (mmc_switch_part(curr_device, MMC_PART_RPMB) != 0)
318 return CMD_RET_FAILURE;
319 mmc->part_num = MMC_PART_RPMB;
320 }
321 ret = cp->cmd(cmdtp, flag, argc, argv);
322
323 /* Return to original partition */
324 if (mmc->part_num != original_part) {
325 if (mmc_switch_part(curr_device, original_part) != 0)
326 return CMD_RET_FAILURE;
327 mmc->part_num = original_part;
328 }
329 return ret;
330}
331#endif
332
333static int do_mmc_read(cmd_tbl_t *cmdtp, int flag,
334 int argc, char * const argv[])
335{
336 struct mmc *mmc;
337 u32 blk, cnt, n;
338 void *addr;
339
340 if (argc != 4)
341 return CMD_RET_USAGE;
342
343 addr = (void *)simple_strtoul(argv[1], NULL, 16);
344 blk = simple_strtoul(argv[2], NULL, 16);
345 cnt = simple_strtoul(argv[3], NULL, 16);
346
Stephen Warren1ae24a52014-05-23 13:24:45 -0600347 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200348 if (!mmc)
349 return CMD_RET_FAILURE;
350
351 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
352 curr_device, blk, cnt);
353
354 n = mmc->block_dev.block_read(curr_device, blk, cnt, addr);
355 /* flush cache after read */
356 flush_cache((ulong)addr, cnt * 512); /* FIXME */
357 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
358
359 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
360}
361static int do_mmc_write(cmd_tbl_t *cmdtp, int flag,
362 int argc, char * const argv[])
363{
364 struct mmc *mmc;
365 u32 blk, cnt, n;
366 void *addr;
367
368 if (argc != 4)
369 return CMD_RET_USAGE;
370
371 addr = (void *)simple_strtoul(argv[1], NULL, 16);
372 blk = simple_strtoul(argv[2], NULL, 16);
373 cnt = simple_strtoul(argv[3], NULL, 16);
374
Stephen Warren1ae24a52014-05-23 13:24:45 -0600375 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200376 if (!mmc)
377 return CMD_RET_FAILURE;
378
379 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
380 curr_device, blk, cnt);
381
382 if (mmc_getwp(mmc) == 1) {
383 printf("Error: card is write protected!\n");
384 return CMD_RET_FAILURE;
385 }
386 n = mmc->block_dev.block_write(curr_device, blk, cnt, addr);
387 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
388
389 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
390}
391static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,
392 int argc, char * const argv[])
393{
394 struct mmc *mmc;
395 u32 blk, cnt, n;
396
397 if (argc != 3)
398 return CMD_RET_USAGE;
399
400 blk = simple_strtoul(argv[1], NULL, 16);
401 cnt = simple_strtoul(argv[2], NULL, 16);
402
Stephen Warren1ae24a52014-05-23 13:24:45 -0600403 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200404 if (!mmc)
405 return CMD_RET_FAILURE;
406
407 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
408 curr_device, blk, cnt);
409
410 if (mmc_getwp(mmc) == 1) {
411 printf("Error: card is write protected!\n");
412 return CMD_RET_FAILURE;
413 }
414 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
415 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
416
417 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
418}
419static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
420 int argc, char * const argv[])
421{
422 struct mmc *mmc;
423
Stephen Warren941944e2014-05-23 13:24:46 -0600424 mmc = init_mmc_device(curr_device, true);
425 if (!mmc)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200426 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200427
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200428 return CMD_RET_SUCCESS;
429}
430static int do_mmc_part(cmd_tbl_t *cmdtp, int flag,
431 int argc, char * const argv[])
432{
433 block_dev_desc_t *mmc_dev;
434 struct mmc *mmc;
435
Stephen Warren1ae24a52014-05-23 13:24:45 -0600436 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200437 if (!mmc)
438 return CMD_RET_FAILURE;
439
440 mmc_dev = mmc_get_dev(curr_device);
441 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
442 print_part(mmc_dev);
443 return CMD_RET_SUCCESS;
444 }
445
446 puts("get mmc type error!\n");
447 return CMD_RET_FAILURE;
448}
449static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag,
450 int argc, char * const argv[])
451{
Stephen Warren60dc58f2014-05-23 12:48:10 -0600452 int dev, part = 0, ret;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200453 struct mmc *mmc;
454
455 if (argc == 1) {
456 dev = curr_device;
457 } else if (argc == 2) {
458 dev = simple_strtoul(argv[1], NULL, 10);
459 } else if (argc == 3) {
460 dev = (int)simple_strtoul(argv[1], NULL, 10);
461 part = (int)simple_strtoul(argv[2], NULL, 10);
462 if (part > PART_ACCESS_MASK) {
463 printf("#part_num shouldn't be larger than %d\n",
464 PART_ACCESS_MASK);
465 return CMD_RET_FAILURE;
466 }
467 } else {
468 return CMD_RET_USAGE;
469 }
470
Stephen Warrena5710922014-05-23 13:24:47 -0600471 mmc = init_mmc_device(dev, true);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200472 if (!mmc)
473 return CMD_RET_FAILURE;
474
Stephen Warren60dc58f2014-05-23 12:48:10 -0600475 ret = mmc_select_hwpart(dev, part);
476 printf("switch to partitions #%d, %s\n",
477 part, (!ret) ? "OK" : "ERROR");
478 if (ret)
479 return 1;
480
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200481 curr_device = dev;
482 if (mmc->part_config == MMCPART_NOAVAILABLE)
483 printf("mmc%d is current device\n", curr_device);
484 else
485 printf("mmc%d(part %d) is current device\n",
486 curr_device, mmc->part_num);
487
488 return CMD_RET_SUCCESS;
489}
490static int do_mmc_list(cmd_tbl_t *cmdtp, int flag,
491 int argc, char * const argv[])
492{
493 print_mmc_devices('\n');
494 return CMD_RET_SUCCESS;
495}
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100496
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100497static int parse_hwpart_user(struct mmc_hwpart_conf *pconf,
498 int argc, char * const argv[])
499{
500 int i = 0;
501
502 memset(&pconf->user, 0, sizeof(pconf->user));
503
504 while (i < argc) {
505 if (!strcmp(argv[i], "enh")) {
506 if (i + 2 >= argc)
507 return -1;
508 pconf->user.enh_start =
509 simple_strtoul(argv[i+1], NULL, 10);
510 pconf->user.enh_size =
511 simple_strtoul(argv[i+2], NULL, 10);
512 i += 3;
513 } else if (!strcmp(argv[i], "wrrel")) {
514 if (i + 1 >= argc)
515 return -1;
516 pconf->user.wr_rel_change = 1;
517 if (!strcmp(argv[i+1], "on"))
518 pconf->user.wr_rel_set = 1;
519 else if (!strcmp(argv[i+1], "off"))
520 pconf->user.wr_rel_set = 0;
521 else
522 return -1;
523 i += 2;
524 } else {
525 break;
526 }
527 }
528 return i;
529}
530
531static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
532 int argc, char * const argv[])
533{
534 int i;
535
536 memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
537
538 if (1 >= argc)
539 return -1;
540 pconf->gp_part[pidx].size = simple_strtoul(argv[0], NULL, 10);
541
542 i = 1;
543 while (i < argc) {
544 if (!strcmp(argv[i], "enh")) {
545 pconf->gp_part[pidx].enhanced = 1;
546 i += 1;
547 } else if (!strcmp(argv[i], "wrrel")) {
548 if (i + 1 >= argc)
549 return -1;
550 pconf->gp_part[pidx].wr_rel_change = 1;
551 if (!strcmp(argv[i+1], "on"))
552 pconf->gp_part[pidx].wr_rel_set = 1;
553 else if (!strcmp(argv[i+1], "off"))
554 pconf->gp_part[pidx].wr_rel_set = 0;
555 else
556 return -1;
557 i += 2;
558 } else {
559 break;
560 }
561 }
562 return i;
563}
564
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100565static int do_mmc_hwpartition(cmd_tbl_t *cmdtp, int flag,
566 int argc, char * const argv[])
567{
568 struct mmc *mmc;
569 struct mmc_hwpart_conf pconf = { };
570 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100571 int i, r, pidx;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100572
573 mmc = init_mmc_device(curr_device, false);
574 if (!mmc)
575 return CMD_RET_FAILURE;
576
577 if (argc < 1)
578 return CMD_RET_USAGE;
579 i = 1;
580 while (i < argc) {
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100581 if (!strcmp(argv[i], "user")) {
582 i++;
583 r = parse_hwpart_user(&pconf, argc-i, &argv[i]);
584 if (r < 0)
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100585 return CMD_RET_USAGE;
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100586 i += r;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100587 } else if (!strncmp(argv[i], "gp", 2) &&
588 strlen(argv[i]) == 3 &&
589 argv[i][2] >= '1' && argv[i][2] <= '4') {
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100590 pidx = argv[i][2] - '1';
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100591 i++;
592 r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
593 if (r < 0)
594 return CMD_RET_USAGE;
595 i += r;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100596 } else if (!strcmp(argv[i], "check")) {
597 mode = MMC_HWPART_CONF_CHECK;
598 i++;
599 } else if (!strcmp(argv[i], "set")) {
600 mode = MMC_HWPART_CONF_SET;
601 i++;
602 } else if (!strcmp(argv[i], "complete")) {
603 mode = MMC_HWPART_CONF_COMPLETE;
604 i++;
605 } else {
606 return CMD_RET_USAGE;
607 }
608 }
609
610 puts("Partition configuration:\n");
611 if (pconf.user.enh_size) {
612 puts("\tUser Enhanced Start: ");
613 print_size(((u64)pconf.user.enh_start) << 9, "\n");
614 puts("\tUser Enhanced Size: ");
615 print_size(((u64)pconf.user.enh_size) << 9, "\n");
616 } else {
617 puts("\tNo enhanced user data area\n");
618 }
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100619 if (pconf.user.wr_rel_change)
620 printf("\tUser partition write reliability: %s\n",
621 pconf.user.wr_rel_set ? "on" : "off");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100622 for (pidx = 0; pidx < 4; pidx++) {
623 if (pconf.gp_part[pidx].size) {
624 printf("\tGP%i Capacity: ", pidx+1);
625 print_size(((u64)pconf.gp_part[pidx].size) << 9,
626 pconf.gp_part[pidx].enhanced ?
627 " ENH\n" : "\n");
628 } else {
629 printf("\tNo GP%i partition\n", pidx+1);
630 }
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100631 if (pconf.gp_part[pidx].wr_rel_change)
632 printf("\tGP%i write reliability: %s\n", pidx+1,
633 pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100634 }
635
636 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
637 if (mode == MMC_HWPART_CONF_COMPLETE)
638 puts("Partitioning successful, "
639 "power-cycle to make effective\n");
640 return CMD_RET_SUCCESS;
641 } else {
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100642 puts("Failed!\n");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100643 return CMD_RET_FAILURE;
644 }
645}
646
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200647#ifdef CONFIG_SUPPORT_EMMC_BOOT
648static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag,
649 int argc, char * const argv[])
650{
651 int dev;
652 struct mmc *mmc;
653 u8 width, reset, mode;
654
655 if (argc != 5)
656 return CMD_RET_USAGE;
657 dev = simple_strtoul(argv[1], NULL, 10);
658 width = simple_strtoul(argv[2], NULL, 10);
659 reset = simple_strtoul(argv[3], NULL, 10);
660 mode = simple_strtoul(argv[4], NULL, 10);
661
Stephen Warren1ae24a52014-05-23 13:24:45 -0600662 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200663 if (!mmc)
664 return CMD_RET_FAILURE;
665
666 if (IS_SD(mmc)) {
667 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
668 return CMD_RET_FAILURE;
669 }
670
671 /* acknowledge to be sent during boot operation */
672 return mmc_set_boot_bus_width(mmc, width, reset, mode);
673}
674static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag,
675 int argc, char * const argv[])
676{
677 int dev;
678 struct mmc *mmc;
679 u32 bootsize, rpmbsize;
680
681 if (argc != 4)
682 return CMD_RET_USAGE;
683 dev = simple_strtoul(argv[1], NULL, 10);
684 bootsize = simple_strtoul(argv[2], NULL, 10);
685 rpmbsize = simple_strtoul(argv[3], NULL, 10);
686
Stephen Warren1ae24a52014-05-23 13:24:45 -0600687 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200688 if (!mmc)
689 return CMD_RET_FAILURE;
690
691 if (IS_SD(mmc)) {
692 printf("It is not a EMMC device\n");
693 return CMD_RET_FAILURE;
694 }
695
696 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
697 printf("EMMC boot partition Size change Failed.\n");
698 return CMD_RET_FAILURE;
699 }
700
701 printf("EMMC boot partition Size %d MB\n", bootsize);
702 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
703 return CMD_RET_SUCCESS;
704}
705static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag,
706 int argc, char * const argv[])
707{
708 int dev;
709 struct mmc *mmc;
710 u8 ack, part_num, access;
711
712 if (argc != 5)
713 return CMD_RET_USAGE;
714
715 dev = simple_strtoul(argv[1], NULL, 10);
716 ack = simple_strtoul(argv[2], NULL, 10);
717 part_num = simple_strtoul(argv[3], NULL, 10);
718 access = simple_strtoul(argv[4], NULL, 10);
719
Stephen Warren1ae24a52014-05-23 13:24:45 -0600720 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200721 if (!mmc)
722 return CMD_RET_FAILURE;
723
724 if (IS_SD(mmc)) {
725 puts("PARTITION_CONFIG only exists on eMMC\n");
726 return CMD_RET_FAILURE;
727 }
728
729 /* acknowledge to be sent during boot operation */
730 return mmc_set_part_conf(mmc, ack, part_num, access);
731}
732static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag,
733 int argc, char * const argv[])
734{
735 int dev;
736 struct mmc *mmc;
737 u8 enable;
738
739 /*
740 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
741 * The only valid values are 0x0, 0x1 and 0x2 and writing
742 * a value of 0x1 or 0x2 sets the value permanently.
743 */
744 if (argc != 3)
745 return CMD_RET_USAGE;
746
747 dev = simple_strtoul(argv[1], NULL, 10);
748 enable = simple_strtoul(argv[2], NULL, 10);
749
750 if (enable > 2 || enable < 0) {
751 puts("Invalid RST_n_ENABLE value\n");
752 return CMD_RET_USAGE;
753 }
754
Stephen Warren1ae24a52014-05-23 13:24:45 -0600755 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200756 if (!mmc)
757 return CMD_RET_FAILURE;
758
759 if (IS_SD(mmc)) {
760 puts("RST_n_FUNCTION only exists on eMMC\n");
761 return CMD_RET_FAILURE;
762 }
763
764 return mmc_set_rst_n_function(mmc, enable);
765}
766#endif
767static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag,
768 int argc, char * const argv[])
769{
770 struct mmc *mmc;
771 u32 val;
772 int ret;
773
774 if (argc != 2)
775 return CMD_RET_USAGE;
776 val = simple_strtoul(argv[2], NULL, 16);
777
778 mmc = find_mmc_device(curr_device);
779 if (!mmc) {
780 printf("no mmc device at slot %x\n", curr_device);
781 return CMD_RET_FAILURE;
782 }
783 ret = mmc_set_dsr(mmc, val);
784 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
785 if (!ret) {
786 mmc->has_init = 0;
787 if (mmc_init(mmc))
788 return CMD_RET_FAILURE;
789 else
790 return CMD_RET_SUCCESS;
791 }
792 return ret;
793}
794
795static cmd_tbl_t cmd_mmc[] = {
796 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
797 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
798 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
799 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
800 U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
801 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
802 U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
803 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100804 U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200805#ifdef CONFIG_SUPPORT_EMMC_BOOT
806 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehfa7b8852014-09-25 23:00:16 +0800807 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200808 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
809 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
810#endif
811#ifdef CONFIG_SUPPORT_EMMC_RPMB
812 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
813#endif
814 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
815};
Andy Fleming272cc702008-10-30 16:41:01 -0500816
Kim Phillips088f1b12012-10-29 13:34:31 +0000817static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500818{
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200819 cmd_tbl_t *cp;
Lei Wen6be95cc2011-06-22 17:03:30 +0000820
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200821 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
822
823 /* Drop the mmc command */
824 argc--;
825 argv++;
826
827 if (cp == NULL || argc > cp->maxargs)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000828 return CMD_RET_USAGE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200829 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
830 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500831
Lei Wenea6ebe22011-05-02 16:26:25 +0000832 if (curr_device < 0) {
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200833 if (get_mmc_num() > 0) {
Lei Wenea6ebe22011-05-02 16:26:25 +0000834 curr_device = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200835 } else {
Lei Wenea6ebe22011-05-02 16:26:25 +0000836 puts("No MMC device available\n");
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200837 return CMD_RET_FAILURE;
Lei Wenea6ebe22011-05-02 16:26:25 +0000838 }
839 }
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200840 return cp->cmd(cmdtp, flag, argc, argv);
Andy Fleming272cc702008-10-30 16:41:01 -0500841}
842
843U_BOOT_CMD(
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100844 mmc, 29, 1, do_mmcops,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400845 "MMC sub system",
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200846 "info - display info of the current MMC device\n"
847 "mmc read addr blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000848 "mmc write addr blk# cnt\n"
Lei Wene6f99a52011-06-22 17:03:31 +0000849 "mmc erase blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000850 "mmc rescan\n"
851 "mmc part - lists available partition on current mmc device\n"
Lei Wenbc897b12011-05-02 16:26:26 +0000852 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
Amar2a91c912013-04-27 11:43:00 +0530853 "mmc list - lists available devices\n"
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100854 "mmc hwpartition [args...] - does hardware partitioning\n"
855 " arguments (sizes in 512-byte blocks):\n"
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100856 " [user [enh start cnt] [wrrel {on|off}]] - sets user data area attributes\n"
857 " [gp1|gp2|gp3|gp4 cnt [enh] [wrrel {on|off}]] - general purpose partition\n"
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100858 " [check|set|complete] - mode, complete set partitioning completed\n"
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100859 " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
860 " Power cycling is required to initialize partitions after set to complete.\n"
Amar2a91c912013-04-27 11:43:00 +0530861#ifdef CONFIG_SUPPORT_EMMC_BOOT
Tom Rini5a99b9d2014-02-05 10:24:22 -0500862 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
863 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rinif1fd9572014-02-05 10:24:20 -0500864 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
865 " - Change sizes of boot and RPMB partitions of specified device\n"
Tom Rini792970b2014-02-05 10:24:21 -0500866 "mmc partconf dev boot_ack boot_partition partition_access\n"
867 " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
Tom Rini33ace362014-02-07 14:15:20 -0500868 "mmc rst-function dev value\n"
869 " - Change the RST_n_FUNCTION field of the specified device\n"
870 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behme3511b4e2009-02-18 19:59:39 +0100871#endif
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200872#ifdef CONFIG_SUPPORT_EMMC_RPMB
873 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
874 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
875 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
876 "mmc rpmb counter - read the value of the write counter\n"
877#endif
878 "mmc setdsr <value> - set DSR register value\n"
Amar2a91c912013-04-27 11:43:00 +0530879 );
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200880
881/* Old command kept for compatibility. Same as 'mmc info' */
882U_BOOT_CMD(
883 mmcinfo, 1, 0, do_mmcinfo,
884 "display MMC info",
885 "- display info of the current MMC device"
886);
887
Amar2a91c912013-04-27 11:43:00 +0530888#endif /* !CONFIG_GENERIC_MMC */