blob: 2d51927060280bfc0289f00b5514131075829dd7 [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
Lei Wen6be95cc2011-06-22 17:03:30 +000074enum mmc_state {
75 MMC_INVALID,
76 MMC_READ,
77 MMC_WRITE,
Lei Wene6f99a52011-06-22 17:03:31 +000078 MMC_ERASE,
Lei Wen6be95cc2011-06-22 17:03:30 +000079};
Andy Fleming272cc702008-10-30 16:41:01 -050080static void print_mmcinfo(struct mmc *mmc)
81{
82 printf("Device: %s\n", mmc->name);
83 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
84 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
85 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
86 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
87 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
88
89 printf("Tran Speed: %d\n", mmc->tran_speed);
90 printf("Rd Block Len: %d\n", mmc->read_bl_len);
91
92 printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
Jaehoon Chung64f4a612013-01-29 19:31:16 +000093 (mmc->version >> 8) & 0xf, mmc->version & 0xff);
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
99 printf("Bus Width: %d-bit\n", mmc->bus_width);
100}
101
Kim Phillips088f1b12012-10-29 13:34:31 +0000102static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500103{
104 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500105
Lei Wenea6ebe22011-05-02 16:26:25 +0000106 if (curr_device < 0) {
107 if (get_mmc_num() > 0)
108 curr_device = 0;
109 else {
110 puts("No MMC device available\n");
111 return 1;
112 }
113 }
Andy Fleming272cc702008-10-30 16:41:01 -0500114
Lei Wenea6ebe22011-05-02 16:26:25 +0000115 mmc = find_mmc_device(curr_device);
Andy Fleming272cc702008-10-30 16:41:01 -0500116
117 if (mmc) {
118 mmc_init(mmc);
119
120 print_mmcinfo(mmc);
Lei Wenea6ebe22011-05-02 16:26:25 +0000121 return 0;
122 } else {
123 printf("no mmc device at slot %x\n", curr_device);
124 return 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500125 }
Andy Fleming272cc702008-10-30 16:41:01 -0500126}
127
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200128U_BOOT_CMD(
Lei Wenea6ebe22011-05-02 16:26:25 +0000129 mmcinfo, 1, 0, do_mmcinfo,
Frans Meulenbroekscc9f6072010-07-31 15:01:52 +0200130 "display MMC info",
Robert P. J. Day59ddead2012-11-11 03:24:44 +0000131 "- display info of the current MMC device"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200132);
Andy Fleming272cc702008-10-30 16:41:01 -0500133
Kim Phillips088f1b12012-10-29 13:34:31 +0000134static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500135{
Lei Wen6be95cc2011-06-22 17:03:30 +0000136 enum mmc_state state;
137
Lei Wenea6ebe22011-05-02 16:26:25 +0000138 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000139 return CMD_RET_USAGE;
Andy Fleming272cc702008-10-30 16:41:01 -0500140
Lei Wenea6ebe22011-05-02 16:26:25 +0000141 if (curr_device < 0) {
142 if (get_mmc_num() > 0)
143 curr_device = 0;
144 else {
145 puts("No MMC device available\n");
146 return 1;
147 }
148 }
Andy Fleming272cc702008-10-30 16:41:01 -0500149
Lei Wenea6ebe22011-05-02 16:26:25 +0000150 if (strcmp(argv[1], "rescan") == 0) {
Stephen Warren9fd38372013-04-01 11:50:28 +0000151 struct mmc *mmc;
Rabin Vincente85649c2009-04-05 13:30:53 +0530152
Stephen Warren9fd38372013-04-01 11:50:28 +0000153 if (argc != 2)
154 return CMD_RET_USAGE;
155
156 mmc = find_mmc_device(curr_device);
Lei Wenea6ebe22011-05-02 16:26:25 +0000157 if (!mmc) {
158 printf("no mmc device at slot %x\n", curr_device);
Lei Wen8f3b9642010-09-13 22:07:28 +0800159 return 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500160 }
161
Lei Wenbc897b12011-05-02 16:26:26 +0000162 mmc->has_init = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500163
Michael Jones8fd01b82011-07-14 23:09:43 +0000164 if (mmc_init(mmc))
165 return 1;
166 else
167 return 0;
Tom Rini792970b2014-02-05 10:24:21 -0500168 } else if (strcmp(argv[1], "part") == 0) {
Lei Wenea6ebe22011-05-02 16:26:25 +0000169 block_dev_desc_t *mmc_dev;
Stephen Warren9fd38372013-04-01 11:50:28 +0000170 struct mmc *mmc;
Lei Wenea6ebe22011-05-02 16:26:25 +0000171
Stephen Warren9fd38372013-04-01 11:50:28 +0000172 if (argc != 2)
173 return CMD_RET_USAGE;
174
175 mmc = find_mmc_device(curr_device);
Lei Wenea6ebe22011-05-02 16:26:25 +0000176 if (!mmc) {
177 printf("no mmc device at slot %x\n", curr_device);
178 return 1;
179 }
180 mmc_init(mmc);
181 mmc_dev = mmc_get_dev(curr_device);
182 if (mmc_dev != NULL &&
183 mmc_dev->type != DEV_TYPE_UNKNOWN) {
184 print_part(mmc_dev);
Andy Fleming272cc702008-10-30 16:41:01 -0500185 return 0;
186 }
Lei Wenea6ebe22011-05-02 16:26:25 +0000187
188 puts("get mmc type error!\n");
Andy Fleming272cc702008-10-30 16:41:01 -0500189 return 1;
Lei Wenea6ebe22011-05-02 16:26:25 +0000190 } else if (strcmp(argv[1], "list") == 0) {
Stephen Warren9fd38372013-04-01 11:50:28 +0000191 if (argc != 2)
192 return CMD_RET_USAGE;
Lei Wenea6ebe22011-05-02 16:26:25 +0000193 print_mmc_devices('\n');
194 return 0;
195 } else if (strcmp(argv[1], "dev") == 0) {
Lei Wenbc897b12011-05-02 16:26:26 +0000196 int dev, part = -1;
Lei Wenea6ebe22011-05-02 16:26:25 +0000197 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500198
Lei Wenea6ebe22011-05-02 16:26:25 +0000199 if (argc == 2)
200 dev = curr_device;
201 else if (argc == 3)
202 dev = simple_strtoul(argv[2], NULL, 10);
Lei Wenbc897b12011-05-02 16:26:26 +0000203 else if (argc == 4) {
204 dev = (int)simple_strtoul(argv[2], NULL, 10);
205 part = (int)simple_strtoul(argv[3], NULL, 10);
206 if (part > PART_ACCESS_MASK) {
207 printf("#part_num shouldn't be larger"
208 " than %d\n", PART_ACCESS_MASK);
209 return 1;
210 }
211 } else
Simon Glass4c12eeb2011-12-10 08:44:01 +0000212 return CMD_RET_USAGE;
Rabin Vincente85649c2009-04-05 13:30:53 +0530213
Lei Wenea6ebe22011-05-02 16:26:25 +0000214 mmc = find_mmc_device(dev);
215 if (!mmc) {
216 printf("no mmc device at slot %x\n", dev);
217 return 1;
218 }
Andy Fleming272cc702008-10-30 16:41:01 -0500219
Lei Wenbc897b12011-05-02 16:26:26 +0000220 mmc_init(mmc);
221 if (part != -1) {
222 int ret;
223 if (mmc->part_config == MMCPART_NOAVAILABLE) {
224 printf("Card doesn't support part_switch\n");
225 return 1;
226 }
227
228 if (part != mmc->part_num) {
229 ret = mmc_switch_part(dev, part);
230 if (!ret)
231 mmc->part_num = part;
232
Robert P. J. Day1f8b5462013-08-21 11:39:19 -0400233 printf("switch to partitions #%d, %s\n",
Lei Wenbc897b12011-05-02 16:26:26 +0000234 part, (!ret) ? "OK" : "ERROR");
235 }
236 }
Lei Wenea6ebe22011-05-02 16:26:25 +0000237 curr_device = dev;
Lei Wenbc897b12011-05-02 16:26:26 +0000238 if (mmc->part_config == MMCPART_NOAVAILABLE)
239 printf("mmc%d is current device\n", curr_device);
240 else
241 printf("mmc%d(part %d) is current device\n",
242 curr_device, mmc->part_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500243
Lei Wenea6ebe22011-05-02 16:26:25 +0000244 return 0;
Amar2a91c912013-04-27 11:43:00 +0530245#ifdef CONFIG_SUPPORT_EMMC_BOOT
Tom Rini792970b2014-02-05 10:24:21 -0500246 } else if (strcmp(argv[1], "partconf") == 0) {
247 int dev;
248 struct mmc *mmc;
249 u8 ack, part_num, access;
Amar2a91c912013-04-27 11:43:00 +0530250
Tom Rini792970b2014-02-05 10:24:21 -0500251 if (argc == 6) {
252 dev = simple_strtoul(argv[2], NULL, 10);
253 ack = simple_strtoul(argv[3], NULL, 10);
254 part_num = simple_strtoul(argv[4], NULL, 10);
255 access = simple_strtoul(argv[5], NULL, 10);
256 } else {
257 return CMD_RET_USAGE;
258 }
259
260 mmc = find_mmc_device(dev);
261 if (!mmc) {
262 printf("no mmc device at slot %x\n", dev);
263 return 1;
264 }
265
266 if (IS_SD(mmc)) {
267 puts("PARTITION_CONFIG only exists on eMMC\n");
268 return 1;
269 }
270
271 /* acknowledge to be sent during boot operation */
272 return mmc_set_part_conf(mmc, ack, part_num, access);
Tom Rini5a99b9d2014-02-05 10:24:22 -0500273 } else if (strcmp(argv[1], "bootbus") == 0) {
274 int dev;
275 struct mmc *mmc;
276 u8 width, reset, mode;
277
278 if (argc == 6) {
279 dev = simple_strtoul(argv[2], NULL, 10);
280 width = simple_strtoul(argv[3], NULL, 10);
281 reset = simple_strtoul(argv[4], NULL, 10);
282 mode = simple_strtoul(argv[5], NULL, 10);
283 } else {
284 return CMD_RET_USAGE;
285 }
286
287 mmc = find_mmc_device(dev);
288 if (!mmc) {
289 printf("no mmc device at slot %x\n", dev);
290 return 1;
291 }
292
293 if (IS_SD(mmc)) {
294 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
295 return 1;
296 }
297
298 /* acknowledge to be sent during boot operation */
299 return mmc_set_boot_bus_width(mmc, width, reset, mode);
Tom Rinif1fd9572014-02-05 10:24:20 -0500300 } else if (strcmp(argv[1], "bootpart-resize") == 0) {
Amar2a91c912013-04-27 11:43:00 +0530301 int dev;
Tom Rinif1fd9572014-02-05 10:24:20 -0500302 struct mmc *mmc;
Tom Rinib01e6fe2014-02-05 10:24:19 -0500303 u32 bootsize, rpmbsize;
Amar2a91c912013-04-27 11:43:00 +0530304
Tom Rinib01e6fe2014-02-05 10:24:19 -0500305 if (argc == 5) {
306 dev = simple_strtoul(argv[2], NULL, 10);
307 bootsize = simple_strtoul(argv[3], NULL, 10);
308 rpmbsize = simple_strtoul(argv[4], NULL, 10);
309 } else {
310 return CMD_RET_USAGE;
311 }
312
313 mmc = find_mmc_device(dev);
Amar2a91c912013-04-27 11:43:00 +0530314 if (!mmc) {
315 printf("no mmc device at slot %x\n", dev);
316 return 1;
317 }
318
319 if (IS_SD(mmc)) {
320 printf("It is not a EMMC device\n");
321 return 1;
322 }
323
324 if (0 == mmc_boot_partition_size_change(mmc,
325 bootsize, rpmbsize)) {
326 printf("EMMC boot partition Size %d MB\n", bootsize);
327 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
328 return 0;
329 } else {
330 printf("EMMC boot partition Size change Failed.\n");
331 return 1;
332 }
333#endif /* CONFIG_SUPPORT_EMMC_BOOT */
334 }
Markus Niebelab711882013-12-16 13:40:46 +0100335
336 else if (argc == 3 && strcmp(argv[1], "setdsr") == 0) {
337 struct mmc *mmc = find_mmc_device(curr_device);
338 u32 val = simple_strtoul(argv[2], NULL, 16);
339 int ret;
340
341 if (!mmc) {
342 printf("no mmc device at slot %x\n", curr_device);
343 return 1;
344 }
345 ret = mmc_set_dsr(mmc, val);
346 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
347 if (!ret) {
348 mmc->has_init = 0;
349 if (mmc_init(mmc))
350 return 1;
351 else
352 return 0;
353 }
354 return ret;
355 }
356
Taylor Hutted80c932012-11-22 09:13:00 +0000357 state = MMC_INVALID;
358 if (argc == 5 && strcmp(argv[1], "read") == 0)
Lei Wen6be95cc2011-06-22 17:03:30 +0000359 state = MMC_READ;
Taylor Hutted80c932012-11-22 09:13:00 +0000360 else if (argc == 5 && strcmp(argv[1], "write") == 0)
Lei Wen6be95cc2011-06-22 17:03:30 +0000361 state = MMC_WRITE;
Taylor Hutted80c932012-11-22 09:13:00 +0000362 else if (argc == 4 && strcmp(argv[1], "erase") == 0)
Lei Wene6f99a52011-06-22 17:03:31 +0000363 state = MMC_ERASE;
Lei Wen6be95cc2011-06-22 17:03:30 +0000364
365 if (state != MMC_INVALID) {
366 struct mmc *mmc = find_mmc_device(curr_device);
Lei Wene6f99a52011-06-22 17:03:31 +0000367 int idx = 2;
368 u32 blk, cnt, n;
369 void *addr;
370
371 if (state != MMC_ERASE) {
372 addr = (void *)simple_strtoul(argv[idx], NULL, 16);
373 ++idx;
374 } else
Kim Phillips088f1b12012-10-29 13:34:31 +0000375 addr = NULL;
Lei Wene6f99a52011-06-22 17:03:31 +0000376 blk = simple_strtoul(argv[idx], NULL, 16);
377 cnt = simple_strtoul(argv[idx + 1], NULL, 16);
Andy Fleming272cc702008-10-30 16:41:01 -0500378
Lei Wenea6ebe22011-05-02 16:26:25 +0000379 if (!mmc) {
380 printf("no mmc device at slot %x\n", curr_device);
381 return 1;
382 }
Rabin Vincente85649c2009-04-05 13:30:53 +0530383
Lei Wen6be95cc2011-06-22 17:03:30 +0000384 printf("\nMMC %s: dev # %d, block # %d, count %d ... ",
385 argv[1], curr_device, blk, cnt);
Andy Fleming272cc702008-10-30 16:41:01 -0500386
Lei Wenea6ebe22011-05-02 16:26:25 +0000387 mmc_init(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -0500388
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +0000389 if ((state == MMC_WRITE || state == MMC_ERASE)) {
390 if (mmc_getwp(mmc) == 1) {
391 printf("Error: card is write protected!\n");
392 return 1;
393 }
394 }
395
Lei Wen6be95cc2011-06-22 17:03:30 +0000396 switch (state) {
397 case MMC_READ:
398 n = mmc->block_dev.block_read(curr_device, blk,
399 cnt, addr);
400 /* flush cache after read */
401 flush_cache((ulong)addr, cnt * 512); /* FIXME */
402 break;
403 case MMC_WRITE:
404 n = mmc->block_dev.block_write(curr_device, blk,
405 cnt, addr);
406 break;
Lei Wene6f99a52011-06-22 17:03:31 +0000407 case MMC_ERASE:
408 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
409 break;
Lei Wen6be95cc2011-06-22 17:03:30 +0000410 default:
411 BUG();
412 }
Andy Fleming272cc702008-10-30 16:41:01 -0500413
Lei Wen6be95cc2011-06-22 17:03:30 +0000414 printf("%d blocks %s: %s\n",
415 n, argv[1], (n == cnt) ? "OK" : "ERROR");
Lei Wenea6ebe22011-05-02 16:26:25 +0000416 return (n == cnt) ? 0 : 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500417 }
Lei Wenea6ebe22011-05-02 16:26:25 +0000418
Simon Glass4c12eeb2011-12-10 08:44:01 +0000419 return CMD_RET_USAGE;
Andy Fleming272cc702008-10-30 16:41:01 -0500420}
421
422U_BOOT_CMD(
423 mmc, 6, 1, do_mmcops,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400424 "MMC sub system",
Lei Wenea6ebe22011-05-02 16:26:25 +0000425 "read addr blk# cnt\n"
426 "mmc write addr blk# cnt\n"
Lei Wene6f99a52011-06-22 17:03:31 +0000427 "mmc erase blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000428 "mmc rescan\n"
429 "mmc part - lists available partition on current mmc device\n"
Lei Wenbc897b12011-05-02 16:26:26 +0000430 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
Amar2a91c912013-04-27 11:43:00 +0530431 "mmc list - lists available devices\n"
432#ifdef CONFIG_SUPPORT_EMMC_BOOT
Tom Rini5a99b9d2014-02-05 10:24:22 -0500433 "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
434 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rinif1fd9572014-02-05 10:24:20 -0500435 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
436 " - Change sizes of boot and RPMB partitions of specified device\n"
Tom Rini792970b2014-02-05 10:24:21 -0500437 "mmc partconf dev boot_ack boot_partition partition_access\n"
438 " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
Dirk Behme3511b4e2009-02-18 19:59:39 +0100439#endif
Markus Niebelab711882013-12-16 13:40:46 +0100440 "mmc setdsr - set DSR register value\n"
Amar2a91c912013-04-27 11:43:00 +0530441 );
442#endif /* !CONFIG_GENERIC_MMC */