blob: eb4a547a9707939c71ad5396fe2fe386d9912084 [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 }
Eric Nelsonbcfde7f2016-03-27 12:00:14 -0700153
Stephen Warren1ae24a52014-05-23 13:24:45 -0600154 if (force_init)
155 mmc->has_init = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200156 if (mmc_init(mmc))
157 return NULL;
158 return mmc;
159}
Kim Phillips088f1b12012-10-29 13:34:31 +0000160static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500161{
162 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500163
Lei Wenea6ebe22011-05-02 16:26:25 +0000164 if (curr_device < 0) {
165 if (get_mmc_num() > 0)
166 curr_device = 0;
167 else {
168 puts("No MMC device available\n");
169 return 1;
170 }
171 }
Andy Fleming272cc702008-10-30 16:41:01 -0500172
Stephen Warren1ae24a52014-05-23 13:24:45 -0600173 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200174 if (!mmc)
175 return CMD_RET_FAILURE;
Andy Fleming272cc702008-10-30 16:41:01 -0500176
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200177 print_mmcinfo(mmc);
178 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500179}
180
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200181#ifdef CONFIG_SUPPORT_EMMC_RPMB
182static int confirm_key_prog(void)
183{
184 puts("Warning: Programming authentication key can be done only once !\n"
185 " Use this command only if you are sure of what you are doing,\n"
186 "Really perform the key programming? <y/N> ");
187 if (confirm_yesno())
188 return 1;
189
190 puts("Authentication key programming aborted\n");
191 return 0;
192}
193static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag,
194 int argc, char * const argv[])
195{
196 void *key_addr;
197 struct mmc *mmc = find_mmc_device(curr_device);
198
199 if (argc != 2)
200 return CMD_RET_USAGE;
201
202 key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
203 if (!confirm_key_prog())
204 return CMD_RET_FAILURE;
205 if (mmc_rpmb_set_key(mmc, key_addr)) {
206 printf("ERROR - Key already programmed ?\n");
207 return CMD_RET_FAILURE;
208 }
209 return CMD_RET_SUCCESS;
210}
211static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag,
212 int argc, char * const argv[])
213{
214 u16 blk, cnt;
215 void *addr;
216 int n;
217 void *key_addr = NULL;
218 struct mmc *mmc = find_mmc_device(curr_device);
219
220 if (argc < 4)
221 return CMD_RET_USAGE;
222
223 addr = (void *)simple_strtoul(argv[1], NULL, 16);
224 blk = simple_strtoul(argv[2], NULL, 16);
225 cnt = simple_strtoul(argv[3], NULL, 16);
226
227 if (argc == 5)
228 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
229
230 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
231 curr_device, blk, cnt);
232 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
233
234 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
235 if (n != cnt)
236 return CMD_RET_FAILURE;
237 return CMD_RET_SUCCESS;
238}
239static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag,
240 int argc, char * const argv[])
241{
242 u16 blk, cnt;
243 void *addr;
244 int n;
245 void *key_addr;
246 struct mmc *mmc = find_mmc_device(curr_device);
247
248 if (argc != 5)
249 return CMD_RET_USAGE;
250
251 addr = (void *)simple_strtoul(argv[1], NULL, 16);
252 blk = simple_strtoul(argv[2], NULL, 16);
253 cnt = simple_strtoul(argv[3], NULL, 16);
254 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
255
256 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
257 curr_device, blk, cnt);
258 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
259
260 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
261 if (n != cnt)
262 return CMD_RET_FAILURE;
263 return CMD_RET_SUCCESS;
264}
265static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag,
266 int argc, char * const argv[])
267{
268 unsigned long counter;
269 struct mmc *mmc = find_mmc_device(curr_device);
270
271 if (mmc_rpmb_get_counter(mmc, &counter))
272 return CMD_RET_FAILURE;
273 printf("RPMB Write counter= %lx\n", counter);
274 return CMD_RET_SUCCESS;
275}
276
277static cmd_tbl_t cmd_rpmb[] = {
278 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
279 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
280 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
281 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
282};
283
284static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag,
285 int argc, char * const argv[])
286{
287 cmd_tbl_t *cp;
288 struct mmc *mmc;
289 char original_part;
290 int ret;
291
292 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
293
294 /* Drop the rpmb subcommand */
295 argc--;
296 argv++;
297
298 if (cp == NULL || argc > cp->maxargs)
299 return CMD_RET_USAGE;
300 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
301 return CMD_RET_SUCCESS;
302
Stephen Warren1ae24a52014-05-23 13:24:45 -0600303 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200304 if (!mmc)
305 return CMD_RET_FAILURE;
306
307 if (!(mmc->version & MMC_VERSION_MMC)) {
308 printf("It is not a EMMC device\n");
309 return CMD_RET_FAILURE;
310 }
311 if (mmc->version < MMC_VERSION_4_41) {
312 printf("RPMB not supported before version 4.41\n");
313 return CMD_RET_FAILURE;
314 }
315 /* Switch to the RPMB partition */
Marek Vasutb955e422016-05-04 16:35:25 +0200316 original_part = mmc->block_dev.hwpart;
Simon Glass69f45cd2016-05-01 13:52:29 -0600317 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, MMC_PART_RPMB) !=
318 0)
Stephen Warren873cc1d2015-12-07 11:38:49 -0700319 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200320 ret = cp->cmd(cmdtp, flag, argc, argv);
321
322 /* Return to original partition */
Simon Glass69f45cd2016-05-01 13:52:29 -0600323 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, original_part) !=
324 0)
Stephen Warren873cc1d2015-12-07 11:38:49 -0700325 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200326 return ret;
327}
328#endif
329
330static int do_mmc_read(cmd_tbl_t *cmdtp, int flag,
331 int argc, char * const argv[])
332{
333 struct mmc *mmc;
334 u32 blk, cnt, n;
335 void *addr;
336
337 if (argc != 4)
338 return CMD_RET_USAGE;
339
340 addr = (void *)simple_strtoul(argv[1], NULL, 16);
341 blk = simple_strtoul(argv[2], NULL, 16);
342 cnt = simple_strtoul(argv[3], NULL, 16);
343
Stephen Warren1ae24a52014-05-23 13:24:45 -0600344 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200345 if (!mmc)
346 return CMD_RET_FAILURE;
347
348 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
349 curr_device, blk, cnt);
350
Simon Glassc40fdca2016-05-01 13:52:35 -0600351 n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200352 /* flush cache after read */
353 flush_cache((ulong)addr, cnt * 512); /* FIXME */
354 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
355
356 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
357}
358static int do_mmc_write(cmd_tbl_t *cmdtp, int flag,
359 int argc, char * const argv[])
360{
361 struct mmc *mmc;
362 u32 blk, cnt, n;
363 void *addr;
364
365 if (argc != 4)
366 return CMD_RET_USAGE;
367
368 addr = (void *)simple_strtoul(argv[1], NULL, 16);
369 blk = simple_strtoul(argv[2], NULL, 16);
370 cnt = simple_strtoul(argv[3], NULL, 16);
371
Stephen Warren1ae24a52014-05-23 13:24:45 -0600372 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200373 if (!mmc)
374 return CMD_RET_FAILURE;
375
376 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
377 curr_device, blk, cnt);
378
379 if (mmc_getwp(mmc) == 1) {
380 printf("Error: card is write protected!\n");
381 return CMD_RET_FAILURE;
382 }
Simon Glassc40fdca2016-05-01 13:52:35 -0600383 n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200384 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
385
386 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
387}
388static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,
389 int argc, char * const argv[])
390{
391 struct mmc *mmc;
392 u32 blk, cnt, n;
393
394 if (argc != 3)
395 return CMD_RET_USAGE;
396
397 blk = simple_strtoul(argv[1], NULL, 16);
398 cnt = simple_strtoul(argv[2], NULL, 16);
399
Stephen Warren1ae24a52014-05-23 13:24:45 -0600400 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200401 if (!mmc)
402 return CMD_RET_FAILURE;
403
404 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
405 curr_device, blk, cnt);
406
407 if (mmc_getwp(mmc) == 1) {
408 printf("Error: card is write protected!\n");
409 return CMD_RET_FAILURE;
410 }
Simon Glassc40fdca2016-05-01 13:52:35 -0600411 n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200412 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
413
414 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
415}
416static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
417 int argc, char * const argv[])
418{
419 struct mmc *mmc;
420
Stephen Warren941944e2014-05-23 13:24:46 -0600421 mmc = init_mmc_device(curr_device, true);
422 if (!mmc)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200423 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200424
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200425 return CMD_RET_SUCCESS;
426}
427static int do_mmc_part(cmd_tbl_t *cmdtp, int flag,
428 int argc, char * const argv[])
429{
Simon Glass4101f682016-02-29 15:25:34 -0700430 struct blk_desc *mmc_dev;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200431 struct mmc *mmc;
432
Stephen Warren1ae24a52014-05-23 13:24:45 -0600433 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200434 if (!mmc)
435 return CMD_RET_FAILURE;
436
Simon Glass3c457f42016-05-01 11:36:15 -0600437 mmc_dev = blk_get_devnum_by_type(IF_TYPE_MMC, curr_device);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200438 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
Simon Glass3e8bd462016-02-29 15:25:48 -0700439 part_print(mmc_dev);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200440 return CMD_RET_SUCCESS;
441 }
442
443 puts("get mmc type error!\n");
444 return CMD_RET_FAILURE;
445}
446static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag,
447 int argc, char * const argv[])
448{
Stephen Warren60dc58f2014-05-23 12:48:10 -0600449 int dev, part = 0, ret;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200450 struct mmc *mmc;
451
452 if (argc == 1) {
453 dev = curr_device;
454 } else if (argc == 2) {
455 dev = simple_strtoul(argv[1], NULL, 10);
456 } else if (argc == 3) {
457 dev = (int)simple_strtoul(argv[1], NULL, 10);
458 part = (int)simple_strtoul(argv[2], NULL, 10);
459 if (part > PART_ACCESS_MASK) {
460 printf("#part_num shouldn't be larger than %d\n",
461 PART_ACCESS_MASK);
462 return CMD_RET_FAILURE;
463 }
464 } else {
465 return CMD_RET_USAGE;
466 }
467
Stephen Warrena5710922014-05-23 13:24:47 -0600468 mmc = init_mmc_device(dev, true);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200469 if (!mmc)
470 return CMD_RET_FAILURE;
471
Simon Glass69f45cd2016-05-01 13:52:29 -0600472 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
Stephen Warren60dc58f2014-05-23 12:48:10 -0600473 printf("switch to partitions #%d, %s\n",
474 part, (!ret) ? "OK" : "ERROR");
475 if (ret)
476 return 1;
477
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200478 curr_device = dev;
479 if (mmc->part_config == MMCPART_NOAVAILABLE)
480 printf("mmc%d is current device\n", curr_device);
481 else
482 printf("mmc%d(part %d) is current device\n",
Simon Glassc40fdca2016-05-01 13:52:35 -0600483 curr_device, mmc_get_blk_desc(mmc)->hwpart);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200484
485 return CMD_RET_SUCCESS;
486}
487static int do_mmc_list(cmd_tbl_t *cmdtp, int flag,
488 int argc, char * const argv[])
489{
490 print_mmc_devices('\n');
491 return CMD_RET_SUCCESS;
492}
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100493
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100494static int parse_hwpart_user(struct mmc_hwpart_conf *pconf,
495 int argc, char * const argv[])
496{
497 int i = 0;
498
499 memset(&pconf->user, 0, sizeof(pconf->user));
500
501 while (i < argc) {
502 if (!strcmp(argv[i], "enh")) {
503 if (i + 2 >= argc)
504 return -1;
505 pconf->user.enh_start =
506 simple_strtoul(argv[i+1], NULL, 10);
507 pconf->user.enh_size =
508 simple_strtoul(argv[i+2], NULL, 10);
509 i += 3;
510 } else if (!strcmp(argv[i], "wrrel")) {
511 if (i + 1 >= argc)
512 return -1;
513 pconf->user.wr_rel_change = 1;
514 if (!strcmp(argv[i+1], "on"))
515 pconf->user.wr_rel_set = 1;
516 else if (!strcmp(argv[i+1], "off"))
517 pconf->user.wr_rel_set = 0;
518 else
519 return -1;
520 i += 2;
521 } else {
522 break;
523 }
524 }
525 return i;
526}
527
528static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
529 int argc, char * const argv[])
530{
531 int i;
532
533 memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
534
535 if (1 >= argc)
536 return -1;
537 pconf->gp_part[pidx].size = simple_strtoul(argv[0], NULL, 10);
538
539 i = 1;
540 while (i < argc) {
541 if (!strcmp(argv[i], "enh")) {
542 pconf->gp_part[pidx].enhanced = 1;
543 i += 1;
544 } else if (!strcmp(argv[i], "wrrel")) {
545 if (i + 1 >= argc)
546 return -1;
547 pconf->gp_part[pidx].wr_rel_change = 1;
548 if (!strcmp(argv[i+1], "on"))
549 pconf->gp_part[pidx].wr_rel_set = 1;
550 else if (!strcmp(argv[i+1], "off"))
551 pconf->gp_part[pidx].wr_rel_set = 0;
552 else
553 return -1;
554 i += 2;
555 } else {
556 break;
557 }
558 }
559 return i;
560}
561
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100562static int do_mmc_hwpartition(cmd_tbl_t *cmdtp, int flag,
563 int argc, char * const argv[])
564{
565 struct mmc *mmc;
566 struct mmc_hwpart_conf pconf = { };
567 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100568 int i, r, pidx;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100569
570 mmc = init_mmc_device(curr_device, false);
571 if (!mmc)
572 return CMD_RET_FAILURE;
573
574 if (argc < 1)
575 return CMD_RET_USAGE;
576 i = 1;
577 while (i < argc) {
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100578 if (!strcmp(argv[i], "user")) {
579 i++;
580 r = parse_hwpart_user(&pconf, argc-i, &argv[i]);
581 if (r < 0)
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100582 return CMD_RET_USAGE;
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100583 i += r;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100584 } else if (!strncmp(argv[i], "gp", 2) &&
585 strlen(argv[i]) == 3 &&
586 argv[i][2] >= '1' && argv[i][2] <= '4') {
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100587 pidx = argv[i][2] - '1';
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100588 i++;
589 r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
590 if (r < 0)
591 return CMD_RET_USAGE;
592 i += r;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100593 } else if (!strcmp(argv[i], "check")) {
594 mode = MMC_HWPART_CONF_CHECK;
595 i++;
596 } else if (!strcmp(argv[i], "set")) {
597 mode = MMC_HWPART_CONF_SET;
598 i++;
599 } else if (!strcmp(argv[i], "complete")) {
600 mode = MMC_HWPART_CONF_COMPLETE;
601 i++;
602 } else {
603 return CMD_RET_USAGE;
604 }
605 }
606
607 puts("Partition configuration:\n");
608 if (pconf.user.enh_size) {
609 puts("\tUser Enhanced Start: ");
610 print_size(((u64)pconf.user.enh_start) << 9, "\n");
611 puts("\tUser Enhanced Size: ");
612 print_size(((u64)pconf.user.enh_size) << 9, "\n");
613 } else {
614 puts("\tNo enhanced user data area\n");
615 }
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100616 if (pconf.user.wr_rel_change)
617 printf("\tUser partition write reliability: %s\n",
618 pconf.user.wr_rel_set ? "on" : "off");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100619 for (pidx = 0; pidx < 4; pidx++) {
620 if (pconf.gp_part[pidx].size) {
621 printf("\tGP%i Capacity: ", pidx+1);
622 print_size(((u64)pconf.gp_part[pidx].size) << 9,
623 pconf.gp_part[pidx].enhanced ?
624 " ENH\n" : "\n");
625 } else {
626 printf("\tNo GP%i partition\n", pidx+1);
627 }
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100628 if (pconf.gp_part[pidx].wr_rel_change)
629 printf("\tGP%i write reliability: %s\n", pidx+1,
630 pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100631 }
632
633 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
634 if (mode == MMC_HWPART_CONF_COMPLETE)
635 puts("Partitioning successful, "
636 "power-cycle to make effective\n");
637 return CMD_RET_SUCCESS;
638 } else {
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100639 puts("Failed!\n");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100640 return CMD_RET_FAILURE;
641 }
642}
643
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200644#ifdef CONFIG_SUPPORT_EMMC_BOOT
645static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag,
646 int argc, char * const argv[])
647{
648 int dev;
649 struct mmc *mmc;
650 u8 width, reset, mode;
651
652 if (argc != 5)
653 return CMD_RET_USAGE;
654 dev = simple_strtoul(argv[1], NULL, 10);
655 width = simple_strtoul(argv[2], NULL, 10);
656 reset = simple_strtoul(argv[3], NULL, 10);
657 mode = simple_strtoul(argv[4], NULL, 10);
658
Stephen Warren1ae24a52014-05-23 13:24:45 -0600659 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200660 if (!mmc)
661 return CMD_RET_FAILURE;
662
663 if (IS_SD(mmc)) {
664 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
665 return CMD_RET_FAILURE;
666 }
667
668 /* acknowledge to be sent during boot operation */
669 return mmc_set_boot_bus_width(mmc, width, reset, mode);
670}
671static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag,
672 int argc, char * const argv[])
673{
674 int dev;
675 struct mmc *mmc;
676 u32 bootsize, rpmbsize;
677
678 if (argc != 4)
679 return CMD_RET_USAGE;
680 dev = simple_strtoul(argv[1], NULL, 10);
681 bootsize = simple_strtoul(argv[2], NULL, 10);
682 rpmbsize = simple_strtoul(argv[3], NULL, 10);
683
Stephen Warren1ae24a52014-05-23 13:24:45 -0600684 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200685 if (!mmc)
686 return CMD_RET_FAILURE;
687
688 if (IS_SD(mmc)) {
689 printf("It is not a EMMC device\n");
690 return CMD_RET_FAILURE;
691 }
692
693 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
694 printf("EMMC boot partition Size change Failed.\n");
695 return CMD_RET_FAILURE;
696 }
697
698 printf("EMMC boot partition Size %d MB\n", bootsize);
699 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
700 return CMD_RET_SUCCESS;
701}
702static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag,
703 int argc, char * const argv[])
704{
705 int dev;
706 struct mmc *mmc;
707 u8 ack, part_num, access;
708
709 if (argc != 5)
710 return CMD_RET_USAGE;
711
712 dev = simple_strtoul(argv[1], NULL, 10);
713 ack = simple_strtoul(argv[2], NULL, 10);
714 part_num = simple_strtoul(argv[3], NULL, 10);
715 access = simple_strtoul(argv[4], NULL, 10);
716
Stephen Warren1ae24a52014-05-23 13:24:45 -0600717 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200718 if (!mmc)
719 return CMD_RET_FAILURE;
720
721 if (IS_SD(mmc)) {
722 puts("PARTITION_CONFIG only exists on eMMC\n");
723 return CMD_RET_FAILURE;
724 }
725
726 /* acknowledge to be sent during boot operation */
727 return mmc_set_part_conf(mmc, ack, part_num, access);
728}
729static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag,
730 int argc, char * const argv[])
731{
732 int dev;
733 struct mmc *mmc;
734 u8 enable;
735
736 /*
737 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
738 * The only valid values are 0x0, 0x1 and 0x2 and writing
739 * a value of 0x1 or 0x2 sets the value permanently.
740 */
741 if (argc != 3)
742 return CMD_RET_USAGE;
743
744 dev = simple_strtoul(argv[1], NULL, 10);
745 enable = simple_strtoul(argv[2], NULL, 10);
746
Peng Fan678e9312015-11-25 17:16:21 +0800747 if (enable > 2) {
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200748 puts("Invalid RST_n_ENABLE value\n");
749 return CMD_RET_USAGE;
750 }
751
Stephen Warren1ae24a52014-05-23 13:24:45 -0600752 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200753 if (!mmc)
754 return CMD_RET_FAILURE;
755
756 if (IS_SD(mmc)) {
757 puts("RST_n_FUNCTION only exists on eMMC\n");
758 return CMD_RET_FAILURE;
759 }
760
761 return mmc_set_rst_n_function(mmc, enable);
762}
763#endif
764static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag,
765 int argc, char * const argv[])
766{
767 struct mmc *mmc;
768 u32 val;
769 int ret;
770
771 if (argc != 2)
772 return CMD_RET_USAGE;
773 val = simple_strtoul(argv[2], NULL, 16);
774
775 mmc = find_mmc_device(curr_device);
776 if (!mmc) {
777 printf("no mmc device at slot %x\n", curr_device);
778 return CMD_RET_FAILURE;
779 }
780 ret = mmc_set_dsr(mmc, val);
781 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
782 if (!ret) {
783 mmc->has_init = 0;
784 if (mmc_init(mmc))
785 return CMD_RET_FAILURE;
786 else
787 return CMD_RET_SUCCESS;
788 }
789 return ret;
790}
791
792static cmd_tbl_t cmd_mmc[] = {
793 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
794 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
795 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
796 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
797 U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
798 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
799 U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
800 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100801 U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200802#ifdef CONFIG_SUPPORT_EMMC_BOOT
803 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehfa7b8852014-09-25 23:00:16 +0800804 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200805 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
806 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
807#endif
808#ifdef CONFIG_SUPPORT_EMMC_RPMB
809 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
810#endif
811 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
812};
Andy Fleming272cc702008-10-30 16:41:01 -0500813
Kim Phillips088f1b12012-10-29 13:34:31 +0000814static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500815{
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200816 cmd_tbl_t *cp;
Lei Wen6be95cc2011-06-22 17:03:30 +0000817
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200818 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
819
820 /* Drop the mmc command */
821 argc--;
822 argv++;
823
824 if (cp == NULL || argc > cp->maxargs)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000825 return CMD_RET_USAGE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200826 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
827 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500828
Lei Wenea6ebe22011-05-02 16:26:25 +0000829 if (curr_device < 0) {
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200830 if (get_mmc_num() > 0) {
Lei Wenea6ebe22011-05-02 16:26:25 +0000831 curr_device = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200832 } else {
Lei Wenea6ebe22011-05-02 16:26:25 +0000833 puts("No MMC device available\n");
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200834 return CMD_RET_FAILURE;
Lei Wenea6ebe22011-05-02 16:26:25 +0000835 }
836 }
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200837 return cp->cmd(cmdtp, flag, argc, argv);
Andy Fleming272cc702008-10-30 16:41:01 -0500838}
839
840U_BOOT_CMD(
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100841 mmc, 29, 1, do_mmcops,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400842 "MMC sub system",
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200843 "info - display info of the current MMC device\n"
844 "mmc read addr blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000845 "mmc write addr blk# cnt\n"
Lei Wene6f99a52011-06-22 17:03:31 +0000846 "mmc erase blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000847 "mmc rescan\n"
848 "mmc part - lists available partition on current mmc device\n"
Lei Wenbc897b12011-05-02 16:26:26 +0000849 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
Amar2a91c912013-04-27 11:43:00 +0530850 "mmc list - lists available devices\n"
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100851 "mmc hwpartition [args...] - does hardware partitioning\n"
852 " arguments (sizes in 512-byte blocks):\n"
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100853 " [user [enh start cnt] [wrrel {on|off}]] - sets user data area attributes\n"
854 " [gp1|gp2|gp3|gp4 cnt [enh] [wrrel {on|off}]] - general purpose partition\n"
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100855 " [check|set|complete] - mode, complete set partitioning completed\n"
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100856 " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
857 " Power cycling is required to initialize partitions after set to complete.\n"
Amar2a91c912013-04-27 11:43:00 +0530858#ifdef CONFIG_SUPPORT_EMMC_BOOT
Tom Rini5a99b9d2014-02-05 10:24:22 -0500859 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
860 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rinif1fd9572014-02-05 10:24:20 -0500861 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
862 " - Change sizes of boot and RPMB partitions of specified device\n"
Tom Rini792970b2014-02-05 10:24:21 -0500863 "mmc partconf dev boot_ack boot_partition partition_access\n"
864 " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
Tom Rini33ace362014-02-07 14:15:20 -0500865 "mmc rst-function dev value\n"
866 " - Change the RST_n_FUNCTION field of the specified device\n"
867 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behme3511b4e2009-02-18 19:59:39 +0100868#endif
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200869#ifdef CONFIG_SUPPORT_EMMC_RPMB
870 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
871 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
872 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
873 "mmc rpmb counter - read the value of the write counter\n"
874#endif
875 "mmc setdsr <value> - set DSR register value\n"
Amar2a91c912013-04-27 11:43:00 +0530876 );
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200877
878/* Old command kept for compatibility. Same as 'mmc info' */
879U_BOOT_CMD(
880 mmcinfo, 1, 0, do_mmcinfo,
881 "display MMC info",
882 "- display info of the current MMC device"
883);
884
Amar2a91c912013-04-27 11:43:00 +0530885#endif /* !CONFIG_GENERIC_MMC */