blob: 96478e45c14039cd88a1a59427d0ec7f1a44d07e [file] [log] [blame]
wdenk7a428cc2003-06-15 22:40:42 +00001/*
2 * (C) Copyright 2003
3 * Kyle Harris, kharris@nexus-tech.net
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk7a428cc2003-06-15 22:40:42 +00006 */
7
8#include <common.h>
9#include <command.h>
wdenk7a428cc2003-06-15 22:40:42 +000010#include <mmc.h>
11
Mike Frysinger0289fe62009-06-14 21:35:21 -040012static int curr_device = -1;
Lei Wend430d7c2011-05-02 16:26:25 +000013#ifndef CONFIG_GENERIC_MMC
Wolfgang Denk6262d0212010-06-28 22:00:46 +020014int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk7a428cc2003-06-15 22:40:42 +000015{
Minkyu Kang0f922902009-03-30 14:55:51 +090016 int dev;
17
Wolfgang Denk3b683112010-07-17 01:06:04 +020018 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +000019 return CMD_RET_USAGE;
Minkyu Kang0f922902009-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 Glassa06dfc72011-12-10 08:44:01 +000030 return CMD_RET_USAGE;
Minkyu Kang0f922902009-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 Glassa06dfc72011-12-10 08:44:01 +000055 return CMD_RET_USAGE;
Minkyu Kang0f922902009-03-30 14:55:51 +090056 }
57
58 printf("mmc%d is current device\n", curr_device);
59 } else {
Simon Glassa06dfc72011-12-10 08:44:01 +000060 return CMD_RET_USAGE;
wdenk7a428cc2003-06-15 22:40:42 +000061 }
Minkyu Kang0f922902009-03-30 14:55:51 +090062
wdenk7a428cc2003-06-15 22:40:42 +000063 return 0;
64}
65
wdenkf287a242003-07-01 21:06:45 +000066U_BOOT_CMD(
Minkyu Kang0f922902009-03-30 14:55:51 +090067 mmc, 3, 1, do_mmc,
68 "MMC sub-system",
69 "init [dev] - init MMC sub system\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +020070 "mmc device [dev] - show or set current device"
wdenkf12e3962003-06-29 21:03:46 +000071);
Dirk Behmedf8852a2009-02-18 19:59:39 +010072#else /* !CONFIG_GENERIC_MMC */
Andy Flemingad347bb2008-10-30 16:41:01 -050073
74static void print_mmcinfo(struct mmc *mmc)
75{
Pantelis Antoniou2c850462014-03-11 19:34:20 +020076 printf("Device: %s\n", mmc->cfg->name);
Andy Flemingad347bb2008-10-30 16:41:01 -050077 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
78 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
79 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
80 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
81 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
82
83 printf("Tran Speed: %d\n", mmc->tran_speed);
84 printf("Rd Block Len: %d\n", mmc->read_bl_len);
85
86 printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
Jaehoon Chung6108ef62013-01-29 19:31:16 +000087 (mmc->version >> 8) & 0xf, mmc->version & 0xff);
Andy Flemingad347bb2008-10-30 16:41:01 -050088
89 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang316f81a2011-01-04 01:04:19 +000090 puts("Capacity: ");
91 print_size(mmc->capacity, "\n");
Andy Flemingad347bb2008-10-30 16:41:01 -050092
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -060093 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
94 mmc->ddr_mode ? " DDR" : "");
Andy Flemingad347bb2008-10-30 16:41:01 -050095}
Stephen Warrenb627f4b2014-05-23 13:24:45 -060096static struct mmc *init_mmc_device(int dev, bool force_init)
Pierre Aubertbcc302c2014-04-24 10:30:08 +020097{
98 struct mmc *mmc;
99 mmc = find_mmc_device(dev);
100 if (!mmc) {
101 printf("no mmc device at slot %x\n", dev);
102 return NULL;
103 }
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600104 if (force_init)
105 mmc->has_init = 0;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200106 if (mmc_init(mmc))
107 return NULL;
108 return mmc;
109}
Kim Phillipsdc00a682012-10-29 13:34:31 +0000110static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500111{
112 struct mmc *mmc;
Andy Flemingad347bb2008-10-30 16:41:01 -0500113
Lei Wend430d7c2011-05-02 16:26:25 +0000114 if (curr_device < 0) {
115 if (get_mmc_num() > 0)
116 curr_device = 0;
117 else {
118 puts("No MMC device available\n");
119 return 1;
120 }
121 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500122
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600123 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200124 if (!mmc)
125 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500126
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200127 print_mmcinfo(mmc);
128 return CMD_RET_SUCCESS;
129}
Andy Flemingad347bb2008-10-30 16:41:01 -0500130
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200131#ifdef CONFIG_SUPPORT_EMMC_RPMB
132static int confirm_key_prog(void)
133{
134 puts("Warning: Programming authentication key can be done only once !\n"
135 " Use this command only if you are sure of what you are doing,\n"
136 "Really perform the key programming? <y/N> ");
137 if (confirm_yesno())
Lei Wend430d7c2011-05-02 16:26:25 +0000138 return 1;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200139
140 puts("Authentication key programming aborted\n");
141 return 0;
142}
143static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag,
144 int argc, char * const argv[])
145{
146 void *key_addr;
147 struct mmc *mmc = find_mmc_device(curr_device);
148
149 if (argc != 2)
150 return CMD_RET_USAGE;
151
152 key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
153 if (!confirm_key_prog())
154 return CMD_RET_FAILURE;
155 if (mmc_rpmb_set_key(mmc, key_addr)) {
156 printf("ERROR - Key already programmed ?\n");
157 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500158 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200159 return CMD_RET_SUCCESS;
Andy Flemingad347bb2008-10-30 16:41:01 -0500160}
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200161static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag,
162 int argc, char * const argv[])
163{
164 u16 blk, cnt;
165 void *addr;
166 int n;
167 void *key_addr = NULL;
168 struct mmc *mmc = find_mmc_device(curr_device);
Andy Flemingad347bb2008-10-30 16:41:01 -0500169
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200170 if (argc < 4)
171 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500172
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200173 addr = (void *)simple_strtoul(argv[1], NULL, 16);
174 blk = simple_strtoul(argv[2], NULL, 16);
175 cnt = simple_strtoul(argv[3], NULL, 16);
176
177 if (argc == 5)
178 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
179
180 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
181 curr_device, blk, cnt);
182 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
183
184 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
185 if (n != cnt)
186 return CMD_RET_FAILURE;
187 return CMD_RET_SUCCESS;
188}
189static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag,
190 int argc, char * const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500191{
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200192 u16 blk, cnt;
193 void *addr;
194 int n;
195 void *key_addr;
196 struct mmc *mmc = find_mmc_device(curr_device);
Lei Wen64b2b1e2011-06-22 17:03:30 +0000197
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200198 if (argc != 5)
Simon Glassa06dfc72011-12-10 08:44:01 +0000199 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500200
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200201 addr = (void *)simple_strtoul(argv[1], NULL, 16);
202 blk = simple_strtoul(argv[2], NULL, 16);
203 cnt = simple_strtoul(argv[3], NULL, 16);
204 key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
205
206 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
207 curr_device, blk, cnt);
208 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
209
210 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
211 if (n != cnt)
212 return CMD_RET_FAILURE;
213 return CMD_RET_SUCCESS;
214}
215static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag,
216 int argc, char * const argv[])
217{
218 unsigned long counter;
219 struct mmc *mmc = find_mmc_device(curr_device);
220
221 if (mmc_rpmb_get_counter(mmc, &counter))
222 return CMD_RET_FAILURE;
223 printf("RPMB Write counter= %lx\n", counter);
224 return CMD_RET_SUCCESS;
225}
226
227static cmd_tbl_t cmd_rpmb[] = {
228 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
229 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
230 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
231 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
232};
233
234static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag,
235 int argc, char * const argv[])
236{
237 cmd_tbl_t *cp;
238 struct mmc *mmc;
239 char original_part;
240 int ret;
241
242 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
243
244 /* Drop the rpmb subcommand */
245 argc--;
246 argv++;
247
248 if (cp == NULL || argc > cp->maxargs)
249 return CMD_RET_USAGE;
250 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
251 return CMD_RET_SUCCESS;
252
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600253 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200254 if (!mmc)
255 return CMD_RET_FAILURE;
256
257 if (!(mmc->version & MMC_VERSION_MMC)) {
258 printf("It is not a EMMC device\n");
259 return CMD_RET_FAILURE;
Lei Wend430d7c2011-05-02 16:26:25 +0000260 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200261 if (mmc->version < MMC_VERSION_4_41) {
262 printf("RPMB not supported before version 4.41\n");
263 return CMD_RET_FAILURE;
264 }
265 /* Switch to the RPMB partition */
266 original_part = mmc->part_num;
267 if (mmc->part_num != MMC_PART_RPMB) {
268 if (mmc_switch_part(curr_device, MMC_PART_RPMB) != 0)
269 return CMD_RET_FAILURE;
270 mmc->part_num = MMC_PART_RPMB;
271 }
272 ret = cp->cmd(cmdtp, flag, argc, argv);
Andy Flemingad347bb2008-10-30 16:41:01 -0500273
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200274 /* Return to original partition */
275 if (mmc->part_num != original_part) {
276 if (mmc_switch_part(curr_device, original_part) != 0)
277 return CMD_RET_FAILURE;
278 mmc->part_num = original_part;
279 }
280 return ret;
281}
282#endif
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530283
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200284static int do_mmc_read(cmd_tbl_t *cmdtp, int flag,
285 int argc, char * const argv[])
286{
287 struct mmc *mmc;
288 u32 blk, cnt, n;
289 void *addr;
Stephen Warren18faca72013-04-01 11:50:28 +0000290
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200291 if (argc != 4)
292 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500293
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200294 addr = (void *)simple_strtoul(argv[1], NULL, 16);
295 blk = simple_strtoul(argv[2], NULL, 16);
296 cnt = simple_strtoul(argv[3], NULL, 16);
Lei Wen61dba932010-09-13 22:07:28 +0800297
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600298 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200299 if (!mmc)
300 return CMD_RET_FAILURE;
Lei Wen61dba932010-09-13 22:07:28 +0800301
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200302 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
303 curr_device, blk, cnt);
Stephen Warren18faca72013-04-01 11:50:28 +0000304
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200305 n = mmc->block_dev.block_read(curr_device, blk, cnt, addr);
306 /* flush cache after read */
307 flush_cache((ulong)addr, cnt * 512); /* FIXME */
308 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Lei Wend430d7c2011-05-02 16:26:25 +0000309
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200310 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
311}
312static int do_mmc_write(cmd_tbl_t *cmdtp, int flag,
313 int argc, char * const argv[])
314{
315 struct mmc *mmc;
316 u32 blk, cnt, n;
317 void *addr;
Andy Flemingad347bb2008-10-30 16:41:01 -0500318
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200319 if (argc != 4)
320 return CMD_RET_USAGE;
Lei Wend430d7c2011-05-02 16:26:25 +0000321
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200322 addr = (void *)simple_strtoul(argv[1], NULL, 16);
323 blk = simple_strtoul(argv[2], NULL, 16);
324 cnt = simple_strtoul(argv[3], NULL, 16);
Lei Wend430d7c2011-05-02 16:26:25 +0000325
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600326 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200327 if (!mmc)
328 return CMD_RET_FAILURE;
Lei Wen31b99802011-05-02 16:26:26 +0000329
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200330 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
331 curr_device, blk, cnt);
Lei Wen31b99802011-05-02 16:26:26 +0000332
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200333 if (mmc_getwp(mmc) == 1) {
334 printf("Error: card is write protected!\n");
335 return CMD_RET_FAILURE;
336 }
337 n = mmc->block_dev.block_write(curr_device, blk, cnt, addr);
338 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Lei Wend430d7c2011-05-02 16:26:25 +0000339
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200340 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
341}
342static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,
343 int argc, char * const argv[])
344{
345 struct mmc *mmc;
346 u32 blk, cnt, n;
Tom Rinif8c6f792014-02-05 10:24:21 -0500347
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200348 if (argc != 3)
349 return CMD_RET_USAGE;
Tom Rinif8c6f792014-02-05 10:24:21 -0500350
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200351 blk = simple_strtoul(argv[1], NULL, 16);
352 cnt = simple_strtoul(argv[2], NULL, 16);
Amar3ccad532013-04-27 11:43:00 +0530353
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600354 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200355 if (!mmc)
356 return CMD_RET_FAILURE;
Tom Rinif8c6f792014-02-05 10:24:21 -0500357
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200358 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
359 curr_device, blk, cnt);
Tom Rini4cf854c2014-02-05 10:24:22 -0500360
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200361 if (mmc_getwp(mmc) == 1) {
362 printf("Error: card is write protected!\n");
363 return CMD_RET_FAILURE;
364 }
365 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
366 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Tom Rini4cf854c2014-02-05 10:24:22 -0500367
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200368 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
369}
370static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
371 int argc, char * const argv[])
372{
373 struct mmc *mmc;
Tom Rini4cf854c2014-02-05 10:24:22 -0500374
Stephen Warren07ec4e72014-05-23 13:24:46 -0600375 mmc = init_mmc_device(curr_device, true);
376 if (!mmc)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200377 return CMD_RET_FAILURE;
Tom Rini4cf854c2014-02-05 10:24:22 -0500378
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200379 return CMD_RET_SUCCESS;
380}
381static int do_mmc_part(cmd_tbl_t *cmdtp, int flag,
382 int argc, char * const argv[])
383{
384 block_dev_desc_t *mmc_dev;
385 struct mmc *mmc;
Amar3ccad532013-04-27 11:43:00 +0530386
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600387 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200388 if (!mmc)
389 return CMD_RET_FAILURE;
Amar3ccad532013-04-27 11:43:00 +0530390
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200391 mmc_dev = mmc_get_dev(curr_device);
392 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
393 print_part(mmc_dev);
394 return CMD_RET_SUCCESS;
395 }
Amar3ccad532013-04-27 11:43:00 +0530396
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200397 puts("get mmc type error!\n");
398 return CMD_RET_FAILURE;
399}
400static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag,
401 int argc, char * const argv[])
402{
Stephen Warren18549802014-05-23 12:48:10 -0600403 int dev, part = 0, ret;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200404 struct mmc *mmc;
Tom Rini35a3ea12014-02-07 14:15:20 -0500405
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200406 if (argc == 1) {
407 dev = curr_device;
408 } else if (argc == 2) {
409 dev = simple_strtoul(argv[1], NULL, 10);
410 } else if (argc == 3) {
411 dev = (int)simple_strtoul(argv[1], NULL, 10);
412 part = (int)simple_strtoul(argv[2], NULL, 10);
413 if (part > PART_ACCESS_MASK) {
414 printf("#part_num shouldn't be larger than %d\n",
415 PART_ACCESS_MASK);
416 return CMD_RET_FAILURE;
Tom Rini35a3ea12014-02-07 14:15:20 -0500417 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200418 } else {
419 return CMD_RET_USAGE;
420 }
Tom Rini35a3ea12014-02-07 14:15:20 -0500421
Stephen Warrencf66e6f2014-05-23 13:24:47 -0600422 mmc = init_mmc_device(dev, true);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200423 if (!mmc)
424 return CMD_RET_FAILURE;
Tom Rini35a3ea12014-02-07 14:15:20 -0500425
Stephen Warren18549802014-05-23 12:48:10 -0600426 ret = mmc_select_hwpart(dev, part);
427 printf("switch to partitions #%d, %s\n",
428 part, (!ret) ? "OK" : "ERROR");
429 if (ret)
430 return 1;
431
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200432 curr_device = dev;
433 if (mmc->part_config == MMCPART_NOAVAILABLE)
434 printf("mmc%d is current device\n", curr_device);
435 else
436 printf("mmc%d(part %d) is current device\n",
437 curr_device, mmc->part_num);
Tom Rini35a3ea12014-02-07 14:15:20 -0500438
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200439 return CMD_RET_SUCCESS;
440}
441static int do_mmc_list(cmd_tbl_t *cmdtp, int flag,
442 int argc, char * const argv[])
443{
444 print_mmc_devices('\n');
445 return CMD_RET_SUCCESS;
446}
447#ifdef CONFIG_SUPPORT_EMMC_BOOT
448static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag,
449 int argc, char * const argv[])
450{
451 int dev;
452 struct mmc *mmc;
453 u8 width, reset, mode;
454
455 if (argc != 5)
456 return CMD_RET_USAGE;
457 dev = simple_strtoul(argv[1], NULL, 10);
458 width = simple_strtoul(argv[2], NULL, 10);
459 reset = simple_strtoul(argv[3], NULL, 10);
460 mode = simple_strtoul(argv[4], NULL, 10);
461
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600462 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200463 if (!mmc)
464 return CMD_RET_FAILURE;
465
466 if (IS_SD(mmc)) {
467 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
468 return CMD_RET_FAILURE;
Amar3ccad532013-04-27 11:43:00 +0530469 }
Markus Niebel03951412013-12-16 13:40:46 +0100470
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200471 /* acknowledge to be sent during boot operation */
472 return mmc_set_boot_bus_width(mmc, width, reset, mode);
473}
474static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag,
475 int argc, char * const argv[])
476{
477 int dev;
478 struct mmc *mmc;
479 u32 bootsize, rpmbsize;
Markus Niebel03951412013-12-16 13:40:46 +0100480
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200481 if (argc != 4)
482 return CMD_RET_USAGE;
483 dev = simple_strtoul(argv[1], NULL, 10);
484 bootsize = simple_strtoul(argv[2], NULL, 10);
485 rpmbsize = simple_strtoul(argv[3], NULL, 10);
486
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600487 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200488 if (!mmc)
489 return CMD_RET_FAILURE;
490
491 if (IS_SD(mmc)) {
492 printf("It is not a EMMC device\n");
493 return CMD_RET_FAILURE;
Markus Niebel03951412013-12-16 13:40:46 +0100494 }
495
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200496 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
497 printf("EMMC boot partition Size change Failed.\n");
498 return CMD_RET_FAILURE;
499 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500500
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200501 printf("EMMC boot partition Size %d MB\n", bootsize);
502 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
503 return CMD_RET_SUCCESS;
504}
505static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag,
506 int argc, char * const argv[])
507{
508 int dev;
509 struct mmc *mmc;
510 u8 ack, part_num, access;
Lei Wenea526762011-06-22 17:03:31 +0000511
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200512 if (argc != 5)
513 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500514
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200515 dev = simple_strtoul(argv[1], NULL, 10);
516 ack = simple_strtoul(argv[2], NULL, 10);
517 part_num = simple_strtoul(argv[3], NULL, 10);
518 access = simple_strtoul(argv[4], NULL, 10);
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530519
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600520 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200521 if (!mmc)
522 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500523
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200524 if (IS_SD(mmc)) {
525 puts("PARTITION_CONFIG only exists on eMMC\n");
526 return CMD_RET_FAILURE;
527 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500528
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200529 /* acknowledge to be sent during boot operation */
530 return mmc_set_part_conf(mmc, ack, part_num, access);
531}
532static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag,
533 int argc, char * const argv[])
534{
535 int dev;
536 struct mmc *mmc;
537 u8 enable;
Nikita Kiryanov020f2612012-12-03 02:19:46 +0000538
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200539 /*
540 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
541 * The only valid values are 0x0, 0x1 and 0x2 and writing
542 * a value of 0x1 or 0x2 sets the value permanently.
543 */
544 if (argc != 3)
545 return CMD_RET_USAGE;
546
547 dev = simple_strtoul(argv[1], NULL, 10);
548 enable = simple_strtoul(argv[2], NULL, 10);
549
550 if (enable > 2 || enable < 0) {
551 puts("Invalid RST_n_ENABLE value\n");
552 return CMD_RET_USAGE;
553 }
554
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600555 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200556 if (!mmc)
557 return CMD_RET_FAILURE;
558
559 if (IS_SD(mmc)) {
560 puts("RST_n_FUNCTION only exists on eMMC\n");
561 return CMD_RET_FAILURE;
562 }
563
564 return mmc_set_rst_n_function(mmc, enable);
565}
566#endif
567static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag,
568 int argc, char * const argv[])
569{
570 struct mmc *mmc;
571 u32 val;
572 int ret;
Andy Flemingad347bb2008-10-30 16:41:01 -0500573
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200574 if (argc != 2)
575 return CMD_RET_USAGE;
576 val = simple_strtoul(argv[2], NULL, 16);
577
578 mmc = find_mmc_device(curr_device);
579 if (!mmc) {
580 printf("no mmc device at slot %x\n", curr_device);
581 return CMD_RET_FAILURE;
582 }
583 ret = mmc_set_dsr(mmc, val);
584 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
585 if (!ret) {
586 mmc->has_init = 0;
587 if (mmc_init(mmc))
588 return CMD_RET_FAILURE;
589 else
590 return CMD_RET_SUCCESS;
Andy Flemingad347bb2008-10-30 16:41:01 -0500591 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200592 return ret;
593}
Lei Wend430d7c2011-05-02 16:26:25 +0000594
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200595static cmd_tbl_t cmd_mmc[] = {
596 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
597 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
598 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
599 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
600 U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
601 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
602 U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
603 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
604#ifdef CONFIG_SUPPORT_EMMC_BOOT
605 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehd49c4aa2014-09-25 23:00:16 +0800606 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200607 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
608 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
609#endif
610#ifdef CONFIG_SUPPORT_EMMC_RPMB
611 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
612#endif
613 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
614};
615
616static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
617{
618 cmd_tbl_t *cp;
619
620 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
621
622 /* Drop the mmc command */
623 argc--;
624 argv++;
625
626 if (cp == NULL || argc > cp->maxargs)
627 return CMD_RET_USAGE;
628 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
629 return CMD_RET_SUCCESS;
630
631 if (curr_device < 0) {
632 if (get_mmc_num() > 0) {
633 curr_device = 0;
634 } else {
635 puts("No MMC device available\n");
636 return CMD_RET_FAILURE;
637 }
638 }
639 return cp->cmd(cmdtp, flag, argc, argv);
Andy Flemingad347bb2008-10-30 16:41:01 -0500640}
641
642U_BOOT_CMD(
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200643 mmc, 7, 1, do_mmcops,
Mike Frysinger27e1e622009-03-23 22:27:34 -0400644 "MMC sub system",
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200645 "info - display info of the current MMC device\n"
646 "mmc read addr blk# cnt\n"
Lei Wend430d7c2011-05-02 16:26:25 +0000647 "mmc write addr blk# cnt\n"
Lei Wenea526762011-06-22 17:03:31 +0000648 "mmc erase blk# cnt\n"
Lei Wend430d7c2011-05-02 16:26:25 +0000649 "mmc rescan\n"
650 "mmc part - lists available partition on current mmc device\n"
Lei Wen31b99802011-05-02 16:26:26 +0000651 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
Amar3ccad532013-04-27 11:43:00 +0530652 "mmc list - lists available devices\n"
653#ifdef CONFIG_SUPPORT_EMMC_BOOT
Tom Rini4cf854c2014-02-05 10:24:22 -0500654 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
655 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rini79919462014-02-05 10:24:20 -0500656 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
657 " - Change sizes of boot and RPMB partitions of specified device\n"
Tom Rinif8c6f792014-02-05 10:24:21 -0500658 "mmc partconf dev boot_ack boot_partition partition_access\n"
659 " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
Tom Rini35a3ea12014-02-07 14:15:20 -0500660 "mmc rst-function dev value\n"
661 " - Change the RST_n_FUNCTION field of the specified device\n"
662 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behmedf8852a2009-02-18 19:59:39 +0100663#endif
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200664#ifdef CONFIG_SUPPORT_EMMC_RPMB
665 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
666 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
667 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
668 "mmc rpmb counter - read the value of the write counter\n"
669#endif
670 "mmc setdsr <value> - set DSR register value\n"
Amar3ccad532013-04-27 11:43:00 +0530671 );
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200672
673/* Old command kept for compatibility. Same as 'mmc info' */
674U_BOOT_CMD(
675 mmcinfo, 1, 0, do_mmcinfo,
676 "display MMC info",
677 "- display info of the current MMC device"
678);
679
Amar3ccad532013-04-27 11:43:00 +0530680#endif /* !CONFIG_GENERIC_MMC */