blob: 1335e3d344c4eb48f1227e53e519f2ef4b45c9e5 [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
Pantelis Antoniou4b7cee52015-01-23 12:12:01 +020088 printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
89 EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
90 EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
91 if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
92 printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
93 printf("\n");
Andy Fleming272cc702008-10-30 16:41:01 -050094
95 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang940e0782011-01-04 01:04:19 +000096 puts("Capacity: ");
97 print_size(mmc->capacity, "\n");
Andy Fleming272cc702008-10-30 16:41:01 -050098
Andrew Gabbasov786e8f82014-12-01 06:59:09 -060099 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
100 mmc->ddr_mode ? " DDR" : "");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100101
Diego Santa Cruzb0361522014-12-23 10:50:26 +0100102 puts("Erase Group Size: ");
103 print_size(((u64)mmc->erase_grp_size) << 9, "\n");
104
Diego Santa Cruz525ada22014-12-23 10:50:19 +0100105 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100106 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
Diego Santa Cruzbeb98a12014-12-23 10:50:23 +0100107 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
Diego Santa Cruzb0361522014-12-23 10:50:26 +0100108
109 puts("HC WP Group Size: ");
110 print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
111
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100112 puts("User Capacity: ");
Diego Santa Cruz9e41a002014-12-23 10:50:33 +0100113 print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
114 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
115 puts(" WRREL\n");
116 else
117 putc('\n');
Diego Santa Cruzbeb98a12014-12-23 10:50:23 +0100118 if (usr_enh) {
119 puts("User Enhanced Start: ");
120 print_size(mmc->enh_user_start, "\n");
121 puts("User Enhanced Size: ");
122 print_size(mmc->enh_user_size, "\n");
123 }
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100124 puts("Boot Capacity: ");
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100125 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100126 puts("RPMB Capacity: ");
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100127 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
Diego Santa Cruzb0361522014-12-23 10:50:26 +0100128
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100129 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100130 bool is_enh = has_enh &&
131 (mmc->part_attr & EXT_CSD_ENH_GP(i));
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100132 if (mmc->capacity_gp[i]) {
Diego Santa Cruzf289fd72014-12-23 10:50:18 +0100133 printf("GP%i Capacity: ", i+1);
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100134 print_size(mmc->capacity_gp[i],
Diego Santa Cruz9e41a002014-12-23 10:50:33 +0100135 is_enh ? " ENH" : "");
136 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
137 puts(" WRREL\n");
138 else
139 putc('\n');
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100140 }
141 }
142 }
Andy Fleming272cc702008-10-30 16:41:01 -0500143}
Stephen Warren1ae24a52014-05-23 13:24:45 -0600144static struct mmc *init_mmc_device(int dev, bool force_init)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200145{
146 struct mmc *mmc;
147 mmc = find_mmc_device(dev);
148 if (!mmc) {
149 printf("no mmc device at slot %x\n", dev);
150 return NULL;
151 }
Stephen Warren1ae24a52014-05-23 13:24:45 -0600152 if (force_init)
153 mmc->has_init = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200154 if (mmc_init(mmc))
155 return NULL;
156 return mmc;
157}
Kim Phillips088f1b12012-10-29 13:34:31 +0000158static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500159{
160 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500161
Lei Wenea6ebe22011-05-02 16:26:25 +0000162 if (curr_device < 0) {
163 if (get_mmc_num() > 0)
164 curr_device = 0;
165 else {
166 puts("No MMC device available\n");
167 return 1;
168 }
169 }
Andy Fleming272cc702008-10-30 16:41:01 -0500170
Stephen Warren1ae24a52014-05-23 13:24:45 -0600171 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200172 if (!mmc)
173 return CMD_RET_FAILURE;
Andy Fleming272cc702008-10-30 16:41:01 -0500174
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200175 print_mmcinfo(mmc);
176 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500177}
178
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200179#ifdef CONFIG_SUPPORT_EMMC_RPMB
180static int confirm_key_prog(void)
181{
182 puts("Warning: Programming authentication key can be done only once !\n"
183 " Use this command only if you are sure of what you are doing,\n"
184 "Really perform the key programming? <y/N> ");
185 if (confirm_yesno())
186 return 1;
187
188 puts("Authentication key programming aborted\n");
189 return 0;
190}
191static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag,
192 int argc, char * const argv[])
193{
194 void *key_addr;
195 struct mmc *mmc = find_mmc_device(curr_device);
196
197 if (argc != 2)
198 return CMD_RET_USAGE;
199
200 key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
201 if (!confirm_key_prog())
202 return CMD_RET_FAILURE;
203 if (mmc_rpmb_set_key(mmc, key_addr)) {
204 printf("ERROR - Key already programmed ?\n");
205 return CMD_RET_FAILURE;
206 }
207 return CMD_RET_SUCCESS;
208}
209static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag,
210 int argc, char * const argv[])
211{
212 u16 blk, cnt;
213 void *addr;
214 int n;
215 void *key_addr = NULL;
216 struct mmc *mmc = find_mmc_device(curr_device);
217
218 if (argc < 4)
219 return CMD_RET_USAGE;
220
221 addr = (void *)simple_strtoul(argv[1], NULL, 16);
222 blk = simple_strtoul(argv[2], NULL, 16);
223 cnt = simple_strtoul(argv[3], NULL, 16);
224
225 if (argc == 5)
226 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
227
228 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
229 curr_device, blk, cnt);
230 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
231
232 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
233 if (n != cnt)
234 return CMD_RET_FAILURE;
235 return CMD_RET_SUCCESS;
236}
237static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag,
238 int argc, char * const argv[])
239{
240 u16 blk, cnt;
241 void *addr;
242 int n;
243 void *key_addr;
244 struct mmc *mmc = find_mmc_device(curr_device);
245
246 if (argc != 5)
247 return CMD_RET_USAGE;
248
249 addr = (void *)simple_strtoul(argv[1], NULL, 16);
250 blk = simple_strtoul(argv[2], NULL, 16);
251 cnt = simple_strtoul(argv[3], NULL, 16);
252 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
253
254 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
255 curr_device, blk, cnt);
256 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
257
258 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
259 if (n != cnt)
260 return CMD_RET_FAILURE;
261 return CMD_RET_SUCCESS;
262}
263static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag,
264 int argc, char * const argv[])
265{
266 unsigned long counter;
267 struct mmc *mmc = find_mmc_device(curr_device);
268
269 if (mmc_rpmb_get_counter(mmc, &counter))
270 return CMD_RET_FAILURE;
271 printf("RPMB Write counter= %lx\n", counter);
272 return CMD_RET_SUCCESS;
273}
274
275static cmd_tbl_t cmd_rpmb[] = {
276 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
277 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
278 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
279 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
280};
281
282static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag,
283 int argc, char * const argv[])
284{
285 cmd_tbl_t *cp;
286 struct mmc *mmc;
287 char original_part;
288 int ret;
289
290 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
291
292 /* Drop the rpmb subcommand */
293 argc--;
294 argv++;
295
296 if (cp == NULL || argc > cp->maxargs)
297 return CMD_RET_USAGE;
298 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
299 return CMD_RET_SUCCESS;
300
Stephen Warren1ae24a52014-05-23 13:24:45 -0600301 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200302 if (!mmc)
303 return CMD_RET_FAILURE;
304
305 if (!(mmc->version & MMC_VERSION_MMC)) {
306 printf("It is not a EMMC device\n");
307 return CMD_RET_FAILURE;
308 }
309 if (mmc->version < MMC_VERSION_4_41) {
310 printf("RPMB not supported before version 4.41\n");
311 return CMD_RET_FAILURE;
312 }
313 /* Switch to the RPMB partition */
314 original_part = mmc->part_num;
315 if (mmc->part_num != MMC_PART_RPMB) {
316 if (mmc_switch_part(curr_device, MMC_PART_RPMB) != 0)
317 return CMD_RET_FAILURE;
318 mmc->part_num = MMC_PART_RPMB;
319 }
320 ret = cp->cmd(cmdtp, flag, argc, argv);
321
322 /* Return to original partition */
323 if (mmc->part_num != original_part) {
324 if (mmc_switch_part(curr_device, original_part) != 0)
325 return CMD_RET_FAILURE;
326 mmc->part_num = original_part;
327 }
328 return ret;
329}
330#endif
331
332static int do_mmc_read(cmd_tbl_t *cmdtp, int flag,
333 int argc, char * const argv[])
334{
335 struct mmc *mmc;
336 u32 blk, cnt, n;
337 void *addr;
338
339 if (argc != 4)
340 return CMD_RET_USAGE;
341
342 addr = (void *)simple_strtoul(argv[1], NULL, 16);
343 blk = simple_strtoul(argv[2], NULL, 16);
344 cnt = simple_strtoul(argv[3], NULL, 16);
345
Stephen Warren1ae24a52014-05-23 13:24:45 -0600346 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200347 if (!mmc)
348 return CMD_RET_FAILURE;
349
350 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
351 curr_device, blk, cnt);
352
353 n = mmc->block_dev.block_read(curr_device, blk, cnt, addr);
354 /* flush cache after read */
355 flush_cache((ulong)addr, cnt * 512); /* FIXME */
356 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
357
358 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
359}
360static int do_mmc_write(cmd_tbl_t *cmdtp, int flag,
361 int argc, char * const argv[])
362{
363 struct mmc *mmc;
364 u32 blk, cnt, n;
365 void *addr;
366
367 if (argc != 4)
368 return CMD_RET_USAGE;
369
370 addr = (void *)simple_strtoul(argv[1], NULL, 16);
371 blk = simple_strtoul(argv[2], NULL, 16);
372 cnt = simple_strtoul(argv[3], NULL, 16);
373
Stephen Warren1ae24a52014-05-23 13:24:45 -0600374 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200375 if (!mmc)
376 return CMD_RET_FAILURE;
377
378 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
379 curr_device, blk, cnt);
380
381 if (mmc_getwp(mmc) == 1) {
382 printf("Error: card is write protected!\n");
383 return CMD_RET_FAILURE;
384 }
385 n = mmc->block_dev.block_write(curr_device, blk, cnt, addr);
386 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
387
388 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
389}
390static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,
391 int argc, char * const argv[])
392{
393 struct mmc *mmc;
394 u32 blk, cnt, n;
395
396 if (argc != 3)
397 return CMD_RET_USAGE;
398
399 blk = simple_strtoul(argv[1], NULL, 16);
400 cnt = simple_strtoul(argv[2], NULL, 16);
401
Stephen Warren1ae24a52014-05-23 13:24:45 -0600402 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200403 if (!mmc)
404 return CMD_RET_FAILURE;
405
406 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
407 curr_device, blk, cnt);
408
409 if (mmc_getwp(mmc) == 1) {
410 printf("Error: card is write protected!\n");
411 return CMD_RET_FAILURE;
412 }
413 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
414 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
415
416 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
417}
418static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
419 int argc, char * const argv[])
420{
421 struct mmc *mmc;
422
Stephen Warren941944e2014-05-23 13:24:46 -0600423 mmc = init_mmc_device(curr_device, true);
424 if (!mmc)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200425 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200426
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200427 return CMD_RET_SUCCESS;
428}
429static int do_mmc_part(cmd_tbl_t *cmdtp, int flag,
430 int argc, char * const argv[])
431{
432 block_dev_desc_t *mmc_dev;
433 struct mmc *mmc;
434
Stephen Warren1ae24a52014-05-23 13:24:45 -0600435 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200436 if (!mmc)
437 return CMD_RET_FAILURE;
438
439 mmc_dev = mmc_get_dev(curr_device);
440 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
441 print_part(mmc_dev);
442 return CMD_RET_SUCCESS;
443 }
444
445 puts("get mmc type error!\n");
446 return CMD_RET_FAILURE;
447}
448static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag,
449 int argc, char * const argv[])
450{
Stephen Warren60dc58f2014-05-23 12:48:10 -0600451 int dev, part = 0, ret;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200452 struct mmc *mmc;
453
454 if (argc == 1) {
455 dev = curr_device;
456 } else if (argc == 2) {
457 dev = simple_strtoul(argv[1], NULL, 10);
458 } else if (argc == 3) {
459 dev = (int)simple_strtoul(argv[1], NULL, 10);
460 part = (int)simple_strtoul(argv[2], NULL, 10);
461 if (part > PART_ACCESS_MASK) {
462 printf("#part_num shouldn't be larger than %d\n",
463 PART_ACCESS_MASK);
464 return CMD_RET_FAILURE;
465 }
466 } else {
467 return CMD_RET_USAGE;
468 }
469
Stephen Warrena5710922014-05-23 13:24:47 -0600470 mmc = init_mmc_device(dev, true);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200471 if (!mmc)
472 return CMD_RET_FAILURE;
473
Stephen Warren60dc58f2014-05-23 12:48:10 -0600474 ret = mmc_select_hwpart(dev, part);
475 printf("switch to partitions #%d, %s\n",
476 part, (!ret) ? "OK" : "ERROR");
477 if (ret)
478 return 1;
479
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200480 curr_device = dev;
481 if (mmc->part_config == MMCPART_NOAVAILABLE)
482 printf("mmc%d is current device\n", curr_device);
483 else
484 printf("mmc%d(part %d) is current device\n",
485 curr_device, mmc->part_num);
486
487 return CMD_RET_SUCCESS;
488}
489static int do_mmc_list(cmd_tbl_t *cmdtp, int flag,
490 int argc, char * const argv[])
491{
492 print_mmc_devices('\n');
493 return CMD_RET_SUCCESS;
494}
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100495
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100496static int parse_hwpart_user(struct mmc_hwpart_conf *pconf,
497 int argc, char * const argv[])
498{
499 int i = 0;
500
501 memset(&pconf->user, 0, sizeof(pconf->user));
502
503 while (i < argc) {
504 if (!strcmp(argv[i], "enh")) {
505 if (i + 2 >= argc)
506 return -1;
507 pconf->user.enh_start =
508 simple_strtoul(argv[i+1], NULL, 10);
509 pconf->user.enh_size =
510 simple_strtoul(argv[i+2], NULL, 10);
511 i += 3;
512 } else if (!strcmp(argv[i], "wrrel")) {
513 if (i + 1 >= argc)
514 return -1;
515 pconf->user.wr_rel_change = 1;
516 if (!strcmp(argv[i+1], "on"))
517 pconf->user.wr_rel_set = 1;
518 else if (!strcmp(argv[i+1], "off"))
519 pconf->user.wr_rel_set = 0;
520 else
521 return -1;
522 i += 2;
523 } else {
524 break;
525 }
526 }
527 return i;
528}
529
530static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
531 int argc, char * const argv[])
532{
533 int i;
534
535 memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
536
537 if (1 >= argc)
538 return -1;
539 pconf->gp_part[pidx].size = simple_strtoul(argv[0], NULL, 10);
540
541 i = 1;
542 while (i < argc) {
543 if (!strcmp(argv[i], "enh")) {
544 pconf->gp_part[pidx].enhanced = 1;
545 i += 1;
546 } else if (!strcmp(argv[i], "wrrel")) {
547 if (i + 1 >= argc)
548 return -1;
549 pconf->gp_part[pidx].wr_rel_change = 1;
550 if (!strcmp(argv[i+1], "on"))
551 pconf->gp_part[pidx].wr_rel_set = 1;
552 else if (!strcmp(argv[i+1], "off"))
553 pconf->gp_part[pidx].wr_rel_set = 0;
554 else
555 return -1;
556 i += 2;
557 } else {
558 break;
559 }
560 }
561 return i;
562}
563
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100564static int do_mmc_hwpartition(cmd_tbl_t *cmdtp, int flag,
565 int argc, char * const argv[])
566{
567 struct mmc *mmc;
568 struct mmc_hwpart_conf pconf = { };
569 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100570 int i, r, pidx;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100571
572 mmc = init_mmc_device(curr_device, false);
573 if (!mmc)
574 return CMD_RET_FAILURE;
575
576 if (argc < 1)
577 return CMD_RET_USAGE;
578 i = 1;
579 while (i < argc) {
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100580 if (!strcmp(argv[i], "user")) {
581 i++;
582 r = parse_hwpart_user(&pconf, argc-i, &argv[i]);
583 if (r < 0)
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100584 return CMD_RET_USAGE;
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100585 i += r;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100586 } else if (!strncmp(argv[i], "gp", 2) &&
587 strlen(argv[i]) == 3 &&
588 argv[i][2] >= '1' && argv[i][2] <= '4') {
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100589 pidx = argv[i][2] - '1';
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100590 i++;
591 r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
592 if (r < 0)
593 return CMD_RET_USAGE;
594 i += r;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100595 } else if (!strcmp(argv[i], "check")) {
596 mode = MMC_HWPART_CONF_CHECK;
597 i++;
598 } else if (!strcmp(argv[i], "set")) {
599 mode = MMC_HWPART_CONF_SET;
600 i++;
601 } else if (!strcmp(argv[i], "complete")) {
602 mode = MMC_HWPART_CONF_COMPLETE;
603 i++;
604 } else {
605 return CMD_RET_USAGE;
606 }
607 }
608
609 puts("Partition configuration:\n");
610 if (pconf.user.enh_size) {
611 puts("\tUser Enhanced Start: ");
612 print_size(((u64)pconf.user.enh_start) << 9, "\n");
613 puts("\tUser Enhanced Size: ");
614 print_size(((u64)pconf.user.enh_size) << 9, "\n");
615 } else {
616 puts("\tNo enhanced user data area\n");
617 }
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100618 if (pconf.user.wr_rel_change)
619 printf("\tUser partition write reliability: %s\n",
620 pconf.user.wr_rel_set ? "on" : "off");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100621 for (pidx = 0; pidx < 4; pidx++) {
622 if (pconf.gp_part[pidx].size) {
623 printf("\tGP%i Capacity: ", pidx+1);
624 print_size(((u64)pconf.gp_part[pidx].size) << 9,
625 pconf.gp_part[pidx].enhanced ?
626 " ENH\n" : "\n");
627 } else {
628 printf("\tNo GP%i partition\n", pidx+1);
629 }
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100630 if (pconf.gp_part[pidx].wr_rel_change)
631 printf("\tGP%i write reliability: %s\n", pidx+1,
632 pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100633 }
634
635 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
636 if (mode == MMC_HWPART_CONF_COMPLETE)
637 puts("Partitioning successful, "
638 "power-cycle to make effective\n");
639 return CMD_RET_SUCCESS;
640 } else {
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100641 puts("Failed!\n");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100642 return CMD_RET_FAILURE;
643 }
644}
645
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200646#ifdef CONFIG_SUPPORT_EMMC_BOOT
647static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag,
648 int argc, char * const argv[])
649{
650 int dev;
651 struct mmc *mmc;
652 u8 width, reset, mode;
653
654 if (argc != 5)
655 return CMD_RET_USAGE;
656 dev = simple_strtoul(argv[1], NULL, 10);
657 width = simple_strtoul(argv[2], NULL, 10);
658 reset = simple_strtoul(argv[3], NULL, 10);
659 mode = simple_strtoul(argv[4], NULL, 10);
660
Stephen Warren1ae24a52014-05-23 13:24:45 -0600661 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200662 if (!mmc)
663 return CMD_RET_FAILURE;
664
665 if (IS_SD(mmc)) {
666 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
667 return CMD_RET_FAILURE;
668 }
669
670 /* acknowledge to be sent during boot operation */
671 return mmc_set_boot_bus_width(mmc, width, reset, mode);
672}
673static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag,
674 int argc, char * const argv[])
675{
676 int dev;
677 struct mmc *mmc;
678 u32 bootsize, rpmbsize;
679
680 if (argc != 4)
681 return CMD_RET_USAGE;
682 dev = simple_strtoul(argv[1], NULL, 10);
683 bootsize = simple_strtoul(argv[2], NULL, 10);
684 rpmbsize = simple_strtoul(argv[3], NULL, 10);
685
Stephen Warren1ae24a52014-05-23 13:24:45 -0600686 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200687 if (!mmc)
688 return CMD_RET_FAILURE;
689
690 if (IS_SD(mmc)) {
691 printf("It is not a EMMC device\n");
692 return CMD_RET_FAILURE;
693 }
694
695 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
696 printf("EMMC boot partition Size change Failed.\n");
697 return CMD_RET_FAILURE;
698 }
699
700 printf("EMMC boot partition Size %d MB\n", bootsize);
701 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
702 return CMD_RET_SUCCESS;
703}
704static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag,
705 int argc, char * const argv[])
706{
707 int dev;
708 struct mmc *mmc;
709 u8 ack, part_num, access;
710
711 if (argc != 5)
712 return CMD_RET_USAGE;
713
714 dev = simple_strtoul(argv[1], NULL, 10);
715 ack = simple_strtoul(argv[2], NULL, 10);
716 part_num = simple_strtoul(argv[3], NULL, 10);
717 access = simple_strtoul(argv[4], NULL, 10);
718
Stephen Warren1ae24a52014-05-23 13:24:45 -0600719 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200720 if (!mmc)
721 return CMD_RET_FAILURE;
722
723 if (IS_SD(mmc)) {
724 puts("PARTITION_CONFIG only exists on eMMC\n");
725 return CMD_RET_FAILURE;
726 }
727
728 /* acknowledge to be sent during boot operation */
729 return mmc_set_part_conf(mmc, ack, part_num, access);
730}
731static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag,
732 int argc, char * const argv[])
733{
734 int dev;
735 struct mmc *mmc;
736 u8 enable;
737
738 /*
739 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
740 * The only valid values are 0x0, 0x1 and 0x2 and writing
741 * a value of 0x1 or 0x2 sets the value permanently.
742 */
743 if (argc != 3)
744 return CMD_RET_USAGE;
745
746 dev = simple_strtoul(argv[1], NULL, 10);
747 enable = simple_strtoul(argv[2], NULL, 10);
748
749 if (enable > 2 || enable < 0) {
750 puts("Invalid RST_n_ENABLE value\n");
751 return CMD_RET_USAGE;
752 }
753
Stephen Warren1ae24a52014-05-23 13:24:45 -0600754 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200755 if (!mmc)
756 return CMD_RET_FAILURE;
757
758 if (IS_SD(mmc)) {
759 puts("RST_n_FUNCTION only exists on eMMC\n");
760 return CMD_RET_FAILURE;
761 }
762
763 return mmc_set_rst_n_function(mmc, enable);
764}
765#endif
766static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag,
767 int argc, char * const argv[])
768{
769 struct mmc *mmc;
770 u32 val;
771 int ret;
772
773 if (argc != 2)
774 return CMD_RET_USAGE;
775 val = simple_strtoul(argv[2], NULL, 16);
776
777 mmc = find_mmc_device(curr_device);
778 if (!mmc) {
779 printf("no mmc device at slot %x\n", curr_device);
780 return CMD_RET_FAILURE;
781 }
782 ret = mmc_set_dsr(mmc, val);
783 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
784 if (!ret) {
785 mmc->has_init = 0;
786 if (mmc_init(mmc))
787 return CMD_RET_FAILURE;
788 else
789 return CMD_RET_SUCCESS;
790 }
791 return ret;
792}
793
794static cmd_tbl_t cmd_mmc[] = {
795 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
796 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
797 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
798 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
799 U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
800 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
801 U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
802 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100803 U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200804#ifdef CONFIG_SUPPORT_EMMC_BOOT
805 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehfa7b8852014-09-25 23:00:16 +0800806 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200807 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
808 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
809#endif
810#ifdef CONFIG_SUPPORT_EMMC_RPMB
811 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
812#endif
813 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
814};
Andy Fleming272cc702008-10-30 16:41:01 -0500815
Kim Phillips088f1b12012-10-29 13:34:31 +0000816static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500817{
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200818 cmd_tbl_t *cp;
Lei Wen6be95cc2011-06-22 17:03:30 +0000819
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200820 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
821
822 /* Drop the mmc command */
823 argc--;
824 argv++;
825
826 if (cp == NULL || argc > cp->maxargs)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000827 return CMD_RET_USAGE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200828 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
829 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500830
Lei Wenea6ebe22011-05-02 16:26:25 +0000831 if (curr_device < 0) {
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200832 if (get_mmc_num() > 0) {
Lei Wenea6ebe22011-05-02 16:26:25 +0000833 curr_device = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200834 } else {
Lei Wenea6ebe22011-05-02 16:26:25 +0000835 puts("No MMC device available\n");
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200836 return CMD_RET_FAILURE;
Lei Wenea6ebe22011-05-02 16:26:25 +0000837 }
838 }
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200839 return cp->cmd(cmdtp, flag, argc, argv);
Andy Fleming272cc702008-10-30 16:41:01 -0500840}
841
842U_BOOT_CMD(
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100843 mmc, 29, 1, do_mmcops,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400844 "MMC sub system",
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200845 "info - display info of the current MMC device\n"
846 "mmc read addr blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000847 "mmc write addr blk# cnt\n"
Lei Wene6f99a52011-06-22 17:03:31 +0000848 "mmc erase blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000849 "mmc rescan\n"
850 "mmc part - lists available partition on current mmc device\n"
Lei Wenbc897b12011-05-02 16:26:26 +0000851 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
Amar2a91c912013-04-27 11:43:00 +0530852 "mmc list - lists available devices\n"
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100853 "mmc hwpartition [args...] - does hardware partitioning\n"
854 " arguments (sizes in 512-byte blocks):\n"
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100855 " [user [enh start cnt] [wrrel {on|off}]] - sets user data area attributes\n"
856 " [gp1|gp2|gp3|gp4 cnt [enh] [wrrel {on|off}]] - general purpose partition\n"
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100857 " [check|set|complete] - mode, complete set partitioning completed\n"
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100858 " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
859 " Power cycling is required to initialize partitions after set to complete.\n"
Amar2a91c912013-04-27 11:43:00 +0530860#ifdef CONFIG_SUPPORT_EMMC_BOOT
Tom Rini5a99b9d2014-02-05 10:24:22 -0500861 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
862 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rinif1fd9572014-02-05 10:24:20 -0500863 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
864 " - Change sizes of boot and RPMB partitions of specified device\n"
Tom Rini792970b2014-02-05 10:24:21 -0500865 "mmc partconf dev boot_ack boot_partition partition_access\n"
866 " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
Tom Rini33ace362014-02-07 14:15:20 -0500867 "mmc rst-function dev value\n"
868 " - Change the RST_n_FUNCTION field of the specified device\n"
869 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behme3511b4e2009-02-18 19:59:39 +0100870#endif
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200871#ifdef CONFIG_SUPPORT_EMMC_RPMB
872 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
873 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
874 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
875 "mmc rpmb counter - read the value of the write counter\n"
876#endif
877 "mmc setdsr <value> - set DSR register value\n"
Amar2a91c912013-04-27 11:43:00 +0530878 );
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200879
880/* Old command kept for compatibility. Same as 'mmc info' */
881U_BOOT_CMD(
882 mmcinfo, 1, 0, do_mmcinfo,
883 "display MMC info",
884 "- display info of the current MMC device"
885);
886
Amar2a91c912013-04-27 11:43:00 +0530887#endif /* !CONFIG_GENERIC_MMC */