blob: 8397232d7107c08aa30582c37400345cb6e1e992 [file] [log] [blame]
wdenk71f95112003-06-15 22:40:42 +00001/*
2 * (C) Copyright 2003
3 * Kyle Harris, kharris@nexus-tech.net
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk71f95112003-06-15 22:40:42 +00006 */
7
8#include <common.h>
9#include <command.h>
wdenk71f95112003-06-15 22:40:42 +000010#include <mmc.h>
11
Mike Frysinger02e22c22009-06-14 21:35:21 -040012static int curr_device = -1;
Lei Wenea6ebe22011-05-02 16:26:25 +000013#ifndef CONFIG_GENERIC_MMC
Wolfgang Denk54841ab2010-06-28 22:00:46 +020014int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk71f95112003-06-15 22:40:42 +000015{
Minkyu Kang869f6bf2009-03-30 14:55:51 +090016 int dev;
17
Wolfgang Denk47e26b12010-07-17 01:06:04 +020018 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +000019 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090020
21 if (strcmp(argv[1], "init") == 0) {
22 if (argc == 2) {
23 if (curr_device < 0)
24 dev = 1;
25 else
26 dev = curr_device;
27 } else if (argc == 3) {
28 dev = (int)simple_strtoul(argv[2], NULL, 10);
29 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +000030 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090031 }
32
33 if (mmc_legacy_init(dev) != 0) {
34 puts("No MMC card found\n");
35 return 1;
36 }
37
38 curr_device = dev;
39 printf("mmc%d is available\n", curr_device);
40 } else if (strcmp(argv[1], "device") == 0) {
41 if (argc == 2) {
42 if (curr_device < 0) {
43 puts("No MMC device available\n");
44 return 1;
45 }
46 } else if (argc == 3) {
47 dev = (int)simple_strtoul(argv[2], NULL, 10);
48
49#ifdef CONFIG_SYS_MMC_SET_DEV
50 if (mmc_set_dev(dev) != 0)
51 return 1;
52#endif
53 curr_device = dev;
54 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +000055 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090056 }
57
58 printf("mmc%d is current device\n", curr_device);
59 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +000060 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090061 }
62
wdenk71f95112003-06-15 22:40:42 +000063 return 0;
64}
65
wdenk0d498392003-07-01 21:06:45 +000066U_BOOT_CMD(
Minkyu Kang869f6bf2009-03-30 14:55:51 +090067 mmc, 3, 1, do_mmc,
68 "MMC sub-system",
69 "init [dev] - init MMC sub system\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +020070 "mmc device [dev] - show or set current device"
wdenkb0fce992003-06-29 21:03:46 +000071);
Dirk Behme3511b4e2009-02-18 19:59:39 +010072#else /* !CONFIG_GENERIC_MMC */
Andy Fleming272cc702008-10-30 16:41:01 -050073
74static void print_mmcinfo(struct mmc *mmc)
75{
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010076 int i;
77
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020078 printf("Device: %s\n", mmc->cfg->name);
Andy Fleming272cc702008-10-30 16:41:01 -050079 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
80 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
81 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
82 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
83 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
84
85 printf("Tran Speed: %d\n", mmc->tran_speed);
86 printf("Rd Block Len: %d\n", mmc->read_bl_len);
87
88 printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
Jaehoon Chung64f4a612013-01-29 19:31:16 +000089 (mmc->version >> 8) & 0xf, mmc->version & 0xff);
Andy Fleming272cc702008-10-30 16:41:01 -050090
91 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang940e0782011-01-04 01:04:19 +000092 puts("Capacity: ");
93 print_size(mmc->capacity, "\n");
Andy Fleming272cc702008-10-30 16:41:01 -050094
Andrew Gabbasov786e8f82014-12-01 06:59:09 -060095 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
96 mmc->ddr_mode ? " DDR" : "");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010097
Diego Santa Cruzb0361522014-12-23 10:50:26 +010098 puts("Erase Group Size: ");
99 print_size(((u64)mmc->erase_grp_size) << 9, "\n");
100
Diego Santa Cruz525ada22014-12-23 10:50:19 +0100101 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100102 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
Diego Santa Cruzbeb98a12014-12-23 10:50:23 +0100103 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
Diego Santa Cruzb0361522014-12-23 10:50:26 +0100104
105 puts("HC WP Group Size: ");
106 print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
107
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100108 puts("User Capacity: ");
Diego Santa Cruzbeb98a12014-12-23 10:50:23 +0100109 print_size(mmc->capacity_user, usr_enh ? " ENH\n" : "\n");
110 if (usr_enh) {
111 puts("User Enhanced Start: ");
112 print_size(mmc->enh_user_start, "\n");
113 puts("User Enhanced Size: ");
114 print_size(mmc->enh_user_size, "\n");
115 }
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100116 puts("Boot Capacity: ");
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100117 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100118 puts("RPMB Capacity: ");
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100119 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
Diego Santa Cruzb0361522014-12-23 10:50:26 +0100120
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100121 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100122 bool is_enh = has_enh &&
123 (mmc->part_attr & EXT_CSD_ENH_GP(i));
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100124 if (mmc->capacity_gp[i]) {
Diego Santa Cruzf289fd72014-12-23 10:50:18 +0100125 printf("GP%i Capacity: ", i+1);
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +0100126 print_size(mmc->capacity_gp[i],
127 is_enh ? " ENH\n" : "\n");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100128 }
129 }
130 }
Andy Fleming272cc702008-10-30 16:41:01 -0500131}
Stephen Warren1ae24a52014-05-23 13:24:45 -0600132static struct mmc *init_mmc_device(int dev, bool force_init)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200133{
134 struct mmc *mmc;
135 mmc = find_mmc_device(dev);
136 if (!mmc) {
137 printf("no mmc device at slot %x\n", dev);
138 return NULL;
139 }
Stephen Warren1ae24a52014-05-23 13:24:45 -0600140 if (force_init)
141 mmc->has_init = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200142 if (mmc_init(mmc))
143 return NULL;
144 return mmc;
145}
Kim Phillips088f1b12012-10-29 13:34:31 +0000146static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500147{
148 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500149
Lei Wenea6ebe22011-05-02 16:26:25 +0000150 if (curr_device < 0) {
151 if (get_mmc_num() > 0)
152 curr_device = 0;
153 else {
154 puts("No MMC device available\n");
155 return 1;
156 }
157 }
Andy Fleming272cc702008-10-30 16:41:01 -0500158
Stephen Warren1ae24a52014-05-23 13:24:45 -0600159 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200160 if (!mmc)
161 return CMD_RET_FAILURE;
Andy Fleming272cc702008-10-30 16:41:01 -0500162
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200163 print_mmcinfo(mmc);
164 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500165}
166
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200167#ifdef CONFIG_SUPPORT_EMMC_RPMB
168static int confirm_key_prog(void)
169{
170 puts("Warning: Programming authentication key can be done only once !\n"
171 " Use this command only if you are sure of what you are doing,\n"
172 "Really perform the key programming? <y/N> ");
173 if (confirm_yesno())
174 return 1;
175
176 puts("Authentication key programming aborted\n");
177 return 0;
178}
179static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag,
180 int argc, char * const argv[])
181{
182 void *key_addr;
183 struct mmc *mmc = find_mmc_device(curr_device);
184
185 if (argc != 2)
186 return CMD_RET_USAGE;
187
188 key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
189 if (!confirm_key_prog())
190 return CMD_RET_FAILURE;
191 if (mmc_rpmb_set_key(mmc, key_addr)) {
192 printf("ERROR - Key already programmed ?\n");
193 return CMD_RET_FAILURE;
194 }
195 return CMD_RET_SUCCESS;
196}
197static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag,
198 int argc, char * const argv[])
199{
200 u16 blk, cnt;
201 void *addr;
202 int n;
203 void *key_addr = NULL;
204 struct mmc *mmc = find_mmc_device(curr_device);
205
206 if (argc < 4)
207 return CMD_RET_USAGE;
208
209 addr = (void *)simple_strtoul(argv[1], NULL, 16);
210 blk = simple_strtoul(argv[2], NULL, 16);
211 cnt = simple_strtoul(argv[3], NULL, 16);
212
213 if (argc == 5)
214 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
215
216 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
217 curr_device, blk, cnt);
218 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
219
220 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
221 if (n != cnt)
222 return CMD_RET_FAILURE;
223 return CMD_RET_SUCCESS;
224}
225static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag,
226 int argc, char * const argv[])
227{
228 u16 blk, cnt;
229 void *addr;
230 int n;
231 void *key_addr;
232 struct mmc *mmc = find_mmc_device(curr_device);
233
234 if (argc != 5)
235 return CMD_RET_USAGE;
236
237 addr = (void *)simple_strtoul(argv[1], NULL, 16);
238 blk = simple_strtoul(argv[2], NULL, 16);
239 cnt = simple_strtoul(argv[3], NULL, 16);
240 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
241
242 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
243 curr_device, blk, cnt);
244 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
245
246 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
247 if (n != cnt)
248 return CMD_RET_FAILURE;
249 return CMD_RET_SUCCESS;
250}
251static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag,
252 int argc, char * const argv[])
253{
254 unsigned long counter;
255 struct mmc *mmc = find_mmc_device(curr_device);
256
257 if (mmc_rpmb_get_counter(mmc, &counter))
258 return CMD_RET_FAILURE;
259 printf("RPMB Write counter= %lx\n", counter);
260 return CMD_RET_SUCCESS;
261}
262
263static cmd_tbl_t cmd_rpmb[] = {
264 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
265 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
266 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
267 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
268};
269
270static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag,
271 int argc, char * const argv[])
272{
273 cmd_tbl_t *cp;
274 struct mmc *mmc;
275 char original_part;
276 int ret;
277
278 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
279
280 /* Drop the rpmb subcommand */
281 argc--;
282 argv++;
283
284 if (cp == NULL || argc > cp->maxargs)
285 return CMD_RET_USAGE;
286 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
287 return CMD_RET_SUCCESS;
288
Stephen Warren1ae24a52014-05-23 13:24:45 -0600289 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200290 if (!mmc)
291 return CMD_RET_FAILURE;
292
293 if (!(mmc->version & MMC_VERSION_MMC)) {
294 printf("It is not a EMMC device\n");
295 return CMD_RET_FAILURE;
296 }
297 if (mmc->version < MMC_VERSION_4_41) {
298 printf("RPMB not supported before version 4.41\n");
299 return CMD_RET_FAILURE;
300 }
301 /* Switch to the RPMB partition */
302 original_part = mmc->part_num;
303 if (mmc->part_num != MMC_PART_RPMB) {
304 if (mmc_switch_part(curr_device, MMC_PART_RPMB) != 0)
305 return CMD_RET_FAILURE;
306 mmc->part_num = MMC_PART_RPMB;
307 }
308 ret = cp->cmd(cmdtp, flag, argc, argv);
309
310 /* Return to original partition */
311 if (mmc->part_num != original_part) {
312 if (mmc_switch_part(curr_device, original_part) != 0)
313 return CMD_RET_FAILURE;
314 mmc->part_num = original_part;
315 }
316 return ret;
317}
318#endif
319
320static int do_mmc_read(cmd_tbl_t *cmdtp, int flag,
321 int argc, char * const argv[])
322{
323 struct mmc *mmc;
324 u32 blk, cnt, n;
325 void *addr;
326
327 if (argc != 4)
328 return CMD_RET_USAGE;
329
330 addr = (void *)simple_strtoul(argv[1], NULL, 16);
331 blk = simple_strtoul(argv[2], NULL, 16);
332 cnt = simple_strtoul(argv[3], NULL, 16);
333
Stephen Warren1ae24a52014-05-23 13:24:45 -0600334 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200335 if (!mmc)
336 return CMD_RET_FAILURE;
337
338 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
339 curr_device, blk, cnt);
340
341 n = mmc->block_dev.block_read(curr_device, blk, cnt, addr);
342 /* flush cache after read */
343 flush_cache((ulong)addr, cnt * 512); /* FIXME */
344 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
345
346 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
347}
348static int do_mmc_write(cmd_tbl_t *cmdtp, int flag,
349 int argc, char * const argv[])
350{
351 struct mmc *mmc;
352 u32 blk, cnt, n;
353 void *addr;
354
355 if (argc != 4)
356 return CMD_RET_USAGE;
357
358 addr = (void *)simple_strtoul(argv[1], NULL, 16);
359 blk = simple_strtoul(argv[2], NULL, 16);
360 cnt = simple_strtoul(argv[3], NULL, 16);
361
Stephen Warren1ae24a52014-05-23 13:24:45 -0600362 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200363 if (!mmc)
364 return CMD_RET_FAILURE;
365
366 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
367 curr_device, blk, cnt);
368
369 if (mmc_getwp(mmc) == 1) {
370 printf("Error: card is write protected!\n");
371 return CMD_RET_FAILURE;
372 }
373 n = mmc->block_dev.block_write(curr_device, blk, cnt, addr);
374 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
375
376 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
377}
378static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,
379 int argc, char * const argv[])
380{
381 struct mmc *mmc;
382 u32 blk, cnt, n;
383
384 if (argc != 3)
385 return CMD_RET_USAGE;
386
387 blk = simple_strtoul(argv[1], NULL, 16);
388 cnt = simple_strtoul(argv[2], NULL, 16);
389
Stephen Warren1ae24a52014-05-23 13:24:45 -0600390 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200391 if (!mmc)
392 return CMD_RET_FAILURE;
393
394 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
395 curr_device, blk, cnt);
396
397 if (mmc_getwp(mmc) == 1) {
398 printf("Error: card is write protected!\n");
399 return CMD_RET_FAILURE;
400 }
401 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
402 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
403
404 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
405}
406static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
407 int argc, char * const argv[])
408{
409 struct mmc *mmc;
410
Stephen Warren941944e2014-05-23 13:24:46 -0600411 mmc = init_mmc_device(curr_device, true);
412 if (!mmc)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200413 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200414
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200415 return CMD_RET_SUCCESS;
416}
417static int do_mmc_part(cmd_tbl_t *cmdtp, int flag,
418 int argc, char * const argv[])
419{
420 block_dev_desc_t *mmc_dev;
421 struct mmc *mmc;
422
Stephen Warren1ae24a52014-05-23 13:24:45 -0600423 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200424 if (!mmc)
425 return CMD_RET_FAILURE;
426
427 mmc_dev = mmc_get_dev(curr_device);
428 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
429 print_part(mmc_dev);
430 return CMD_RET_SUCCESS;
431 }
432
433 puts("get mmc type error!\n");
434 return CMD_RET_FAILURE;
435}
436static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag,
437 int argc, char * const argv[])
438{
Stephen Warren60dc58f2014-05-23 12:48:10 -0600439 int dev, part = 0, ret;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200440 struct mmc *mmc;
441
442 if (argc == 1) {
443 dev = curr_device;
444 } else if (argc == 2) {
445 dev = simple_strtoul(argv[1], NULL, 10);
446 } else if (argc == 3) {
447 dev = (int)simple_strtoul(argv[1], NULL, 10);
448 part = (int)simple_strtoul(argv[2], NULL, 10);
449 if (part > PART_ACCESS_MASK) {
450 printf("#part_num shouldn't be larger than %d\n",
451 PART_ACCESS_MASK);
452 return CMD_RET_FAILURE;
453 }
454 } else {
455 return CMD_RET_USAGE;
456 }
457
Stephen Warrena5710922014-05-23 13:24:47 -0600458 mmc = init_mmc_device(dev, true);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200459 if (!mmc)
460 return CMD_RET_FAILURE;
461
Stephen Warren60dc58f2014-05-23 12:48:10 -0600462 ret = mmc_select_hwpart(dev, part);
463 printf("switch to partitions #%d, %s\n",
464 part, (!ret) ? "OK" : "ERROR");
465 if (ret)
466 return 1;
467
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200468 curr_device = dev;
469 if (mmc->part_config == MMCPART_NOAVAILABLE)
470 printf("mmc%d is current device\n", curr_device);
471 else
472 printf("mmc%d(part %d) is current device\n",
473 curr_device, mmc->part_num);
474
475 return CMD_RET_SUCCESS;
476}
477static int do_mmc_list(cmd_tbl_t *cmdtp, int flag,
478 int argc, char * const argv[])
479{
480 print_mmc_devices('\n');
481 return CMD_RET_SUCCESS;
482}
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100483
484static int do_mmc_hwpartition(cmd_tbl_t *cmdtp, int flag,
485 int argc, char * const argv[])
486{
487 struct mmc *mmc;
488 struct mmc_hwpart_conf pconf = { };
489 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
490 int i, pidx;
491
492 mmc = init_mmc_device(curr_device, false);
493 if (!mmc)
494 return CMD_RET_FAILURE;
495
496 if (argc < 1)
497 return CMD_RET_USAGE;
498 i = 1;
499 while (i < argc) {
500 if (!strcmp(argv[i], "userenh")) {
501 if (i + 2 >= argc)
502 return CMD_RET_USAGE;
503 memset(&pconf.user, 0, sizeof(pconf.user));
504 pconf.user.enh_start =
505 simple_strtoul(argv[i+1], NULL, 10);
506 pconf.user.enh_size =
507 simple_strtoul(argv[i+2], NULL, 10);
508 i += 3;
509 } else if (!strncmp(argv[i], "gp", 2) &&
510 strlen(argv[i]) == 3 &&
511 argv[i][2] >= '1' && argv[i][2] <= '4') {
512 if (i + 1 >= argc)
513 return CMD_RET_USAGE;
514 pidx = argv[i][2] - '1';
515 memset(&pconf.gp_part[pidx], 0,
516 sizeof(pconf.gp_part[pidx]));
517 pconf.gp_part[pidx].size =
518 simple_strtoul(argv[i+1], NULL, 10);
519 i += 2;
520 if (i < argc && !strcmp(argv[i], "enh")) {
521 pconf.gp_part[pidx].enhanced = 1;
522 i++;
523 }
524 } else if (!strcmp(argv[i], "check")) {
525 mode = MMC_HWPART_CONF_CHECK;
526 i++;
527 } else if (!strcmp(argv[i], "set")) {
528 mode = MMC_HWPART_CONF_SET;
529 i++;
530 } else if (!strcmp(argv[i], "complete")) {
531 mode = MMC_HWPART_CONF_COMPLETE;
532 i++;
533 } else {
534 return CMD_RET_USAGE;
535 }
536 }
537
538 puts("Partition configuration:\n");
539 if (pconf.user.enh_size) {
540 puts("\tUser Enhanced Start: ");
541 print_size(((u64)pconf.user.enh_start) << 9, "\n");
542 puts("\tUser Enhanced Size: ");
543 print_size(((u64)pconf.user.enh_size) << 9, "\n");
544 } else {
545 puts("\tNo enhanced user data area\n");
546 }
547 for (pidx = 0; pidx < 4; pidx++) {
548 if (pconf.gp_part[pidx].size) {
549 printf("\tGP%i Capacity: ", pidx+1);
550 print_size(((u64)pconf.gp_part[pidx].size) << 9,
551 pconf.gp_part[pidx].enhanced ?
552 " ENH\n" : "\n");
553 } else {
554 printf("\tNo GP%i partition\n", pidx+1);
555 }
556 }
557
558 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
559 if (mode == MMC_HWPART_CONF_COMPLETE)
560 puts("Partitioning successful, "
561 "power-cycle to make effective\n");
562 return CMD_RET_SUCCESS;
563 } else {
564 return CMD_RET_FAILURE;
565 }
566}
567
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200568#ifdef CONFIG_SUPPORT_EMMC_BOOT
569static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag,
570 int argc, char * const argv[])
571{
572 int dev;
573 struct mmc *mmc;
574 u8 width, reset, mode;
575
576 if (argc != 5)
577 return CMD_RET_USAGE;
578 dev = simple_strtoul(argv[1], NULL, 10);
579 width = simple_strtoul(argv[2], NULL, 10);
580 reset = simple_strtoul(argv[3], NULL, 10);
581 mode = simple_strtoul(argv[4], NULL, 10);
582
Stephen Warren1ae24a52014-05-23 13:24:45 -0600583 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200584 if (!mmc)
585 return CMD_RET_FAILURE;
586
587 if (IS_SD(mmc)) {
588 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
589 return CMD_RET_FAILURE;
590 }
591
592 /* acknowledge to be sent during boot operation */
593 return mmc_set_boot_bus_width(mmc, width, reset, mode);
594}
595static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag,
596 int argc, char * const argv[])
597{
598 int dev;
599 struct mmc *mmc;
600 u32 bootsize, rpmbsize;
601
602 if (argc != 4)
603 return CMD_RET_USAGE;
604 dev = simple_strtoul(argv[1], NULL, 10);
605 bootsize = simple_strtoul(argv[2], NULL, 10);
606 rpmbsize = simple_strtoul(argv[3], NULL, 10);
607
Stephen Warren1ae24a52014-05-23 13:24:45 -0600608 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200609 if (!mmc)
610 return CMD_RET_FAILURE;
611
612 if (IS_SD(mmc)) {
613 printf("It is not a EMMC device\n");
614 return CMD_RET_FAILURE;
615 }
616
617 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
618 printf("EMMC boot partition Size change Failed.\n");
619 return CMD_RET_FAILURE;
620 }
621
622 printf("EMMC boot partition Size %d MB\n", bootsize);
623 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
624 return CMD_RET_SUCCESS;
625}
626static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag,
627 int argc, char * const argv[])
628{
629 int dev;
630 struct mmc *mmc;
631 u8 ack, part_num, access;
632
633 if (argc != 5)
634 return CMD_RET_USAGE;
635
636 dev = simple_strtoul(argv[1], NULL, 10);
637 ack = simple_strtoul(argv[2], NULL, 10);
638 part_num = simple_strtoul(argv[3], NULL, 10);
639 access = simple_strtoul(argv[4], NULL, 10);
640
Stephen Warren1ae24a52014-05-23 13:24:45 -0600641 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200642 if (!mmc)
643 return CMD_RET_FAILURE;
644
645 if (IS_SD(mmc)) {
646 puts("PARTITION_CONFIG only exists on eMMC\n");
647 return CMD_RET_FAILURE;
648 }
649
650 /* acknowledge to be sent during boot operation */
651 return mmc_set_part_conf(mmc, ack, part_num, access);
652}
653static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag,
654 int argc, char * const argv[])
655{
656 int dev;
657 struct mmc *mmc;
658 u8 enable;
659
660 /*
661 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
662 * The only valid values are 0x0, 0x1 and 0x2 and writing
663 * a value of 0x1 or 0x2 sets the value permanently.
664 */
665 if (argc != 3)
666 return CMD_RET_USAGE;
667
668 dev = simple_strtoul(argv[1], NULL, 10);
669 enable = simple_strtoul(argv[2], NULL, 10);
670
671 if (enable > 2 || enable < 0) {
672 puts("Invalid RST_n_ENABLE value\n");
673 return CMD_RET_USAGE;
674 }
675
Stephen Warren1ae24a52014-05-23 13:24:45 -0600676 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200677 if (!mmc)
678 return CMD_RET_FAILURE;
679
680 if (IS_SD(mmc)) {
681 puts("RST_n_FUNCTION only exists on eMMC\n");
682 return CMD_RET_FAILURE;
683 }
684
685 return mmc_set_rst_n_function(mmc, enable);
686}
687#endif
688static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag,
689 int argc, char * const argv[])
690{
691 struct mmc *mmc;
692 u32 val;
693 int ret;
694
695 if (argc != 2)
696 return CMD_RET_USAGE;
697 val = simple_strtoul(argv[2], NULL, 16);
698
699 mmc = find_mmc_device(curr_device);
700 if (!mmc) {
701 printf("no mmc device at slot %x\n", curr_device);
702 return CMD_RET_FAILURE;
703 }
704 ret = mmc_set_dsr(mmc, val);
705 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
706 if (!ret) {
707 mmc->has_init = 0;
708 if (mmc_init(mmc))
709 return CMD_RET_FAILURE;
710 else
711 return CMD_RET_SUCCESS;
712 }
713 return ret;
714}
715
716static cmd_tbl_t cmd_mmc[] = {
717 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
718 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
719 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
720 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
721 U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
722 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
723 U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
724 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100725 U_BOOT_CMD_MKENT(hwpartition, 17, 0, do_mmc_hwpartition, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200726#ifdef CONFIG_SUPPORT_EMMC_BOOT
727 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehfa7b8852014-09-25 23:00:16 +0800728 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200729 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
730 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
731#endif
732#ifdef CONFIG_SUPPORT_EMMC_RPMB
733 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
734#endif
735 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
736};
Andy Fleming272cc702008-10-30 16:41:01 -0500737
Kim Phillips088f1b12012-10-29 13:34:31 +0000738static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500739{
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200740 cmd_tbl_t *cp;
Lei Wen6be95cc2011-06-22 17:03:30 +0000741
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200742 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
743
744 /* Drop the mmc command */
745 argc--;
746 argv++;
747
748 if (cp == NULL || argc > cp->maxargs)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000749 return CMD_RET_USAGE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200750 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
751 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500752
Lei Wenea6ebe22011-05-02 16:26:25 +0000753 if (curr_device < 0) {
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200754 if (get_mmc_num() > 0) {
Lei Wenea6ebe22011-05-02 16:26:25 +0000755 curr_device = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200756 } else {
Lei Wenea6ebe22011-05-02 16:26:25 +0000757 puts("No MMC device available\n");
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200758 return CMD_RET_FAILURE;
Lei Wenea6ebe22011-05-02 16:26:25 +0000759 }
760 }
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200761 return cp->cmd(cmdtp, flag, argc, argv);
Andy Fleming272cc702008-10-30 16:41:01 -0500762}
763
764U_BOOT_CMD(
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100765 mmc, 18, 1, do_mmcops,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400766 "MMC sub system",
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200767 "info - display info of the current MMC device\n"
768 "mmc read addr blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000769 "mmc write addr blk# cnt\n"
Lei Wene6f99a52011-06-22 17:03:31 +0000770 "mmc erase blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000771 "mmc rescan\n"
772 "mmc part - lists available partition on current mmc device\n"
Lei Wenbc897b12011-05-02 16:26:26 +0000773 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
Amar2a91c912013-04-27 11:43:00 +0530774 "mmc list - lists available devices\n"
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100775 "mmc hwpartition [args...] - does hardware partitioning\n"
776 " arguments (sizes in 512-byte blocks):\n"
777 " [userenh start cnt] - sets enhanced user data area\n"
778 " [gp1|gp2|gp3|gp4 cnt [enh]] - general purpose partition\n"
779 " [check|set|complete] - mode, complete set partitioning completed\n"
Amar2a91c912013-04-27 11:43:00 +0530780#ifdef CONFIG_SUPPORT_EMMC_BOOT
Tom Rini5a99b9d2014-02-05 10:24:22 -0500781 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
782 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rinif1fd9572014-02-05 10:24:20 -0500783 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
784 " - Change sizes of boot and RPMB partitions of specified device\n"
Tom Rini792970b2014-02-05 10:24:21 -0500785 "mmc partconf dev boot_ack boot_partition partition_access\n"
786 " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
Tom Rini33ace362014-02-07 14:15:20 -0500787 "mmc rst-function dev value\n"
788 " - Change the RST_n_FUNCTION field of the specified device\n"
789 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behme3511b4e2009-02-18 19:59:39 +0100790#endif
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200791#ifdef CONFIG_SUPPORT_EMMC_RPMB
792 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
793 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
794 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
795 "mmc rpmb counter - read the value of the write counter\n"
796#endif
797 "mmc setdsr <value> - set DSR register value\n"
Amar2a91c912013-04-27 11:43:00 +0530798 );
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200799
800/* Old command kept for compatibility. Same as 'mmc info' */
801U_BOOT_CMD(
802 mmcinfo, 1, 0, do_mmcinfo,
803 "display MMC info",
804 "- display info of the current MMC device"
805);
806
Amar2a91c912013-04-27 11:43:00 +0530807#endif /* !CONFIG_GENERIC_MMC */