blob: 1cdeb443fcc5474498832bb1ebf7269b4f54319a [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
Amar2a91c912013-04-27 11:43:00 +0530134#ifdef CONFIG_SUPPORT_EMMC_BOOT
135static int boot_part_access(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
136{
137 int err;
138 err = mmc_boot_part_access(mmc, ack, part_num, access);
139
140 if ((err == 0) && (access != 0)) {
141 printf("\t\t\t!!!Notice!!!\n");
142
143 printf("!You must close EMMC boot Partition");
144 printf("after all images are written\n");
145
146 printf("!EMMC boot partition has continuity");
147 printf("at image writing time.\n");
148
149 printf("!So, do not close the boot partition");
150 printf("before all images are written.\n");
151 return 0;
152 } else if ((err == 0) && (access == 0))
153 return 0;
154 else if ((err != 0) && (access != 0)) {
155 printf("EMMC boot partition-%d OPEN Failed.\n", part_num);
156 return 1;
157 } else {
158 printf("EMMC boot partition-%d CLOSE Failed.\n", part_num);
159 return 1;
160 }
161}
162#endif
163
Kim Phillips088f1b12012-10-29 13:34:31 +0000164static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500165{
Lei Wen6be95cc2011-06-22 17:03:30 +0000166 enum mmc_state state;
167
Lei Wenea6ebe22011-05-02 16:26:25 +0000168 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000169 return CMD_RET_USAGE;
Andy Fleming272cc702008-10-30 16:41:01 -0500170
Lei Wenea6ebe22011-05-02 16:26:25 +0000171 if (curr_device < 0) {
172 if (get_mmc_num() > 0)
173 curr_device = 0;
174 else {
175 puts("No MMC device available\n");
176 return 1;
177 }
178 }
Andy Fleming272cc702008-10-30 16:41:01 -0500179
Lei Wenea6ebe22011-05-02 16:26:25 +0000180 if (strcmp(argv[1], "rescan") == 0) {
Stephen Warren9fd38372013-04-01 11:50:28 +0000181 struct mmc *mmc;
Rabin Vincente85649c2009-04-05 13:30:53 +0530182
Stephen Warren9fd38372013-04-01 11:50:28 +0000183 if (argc != 2)
184 return CMD_RET_USAGE;
185
186 mmc = find_mmc_device(curr_device);
Lei Wenea6ebe22011-05-02 16:26:25 +0000187 if (!mmc) {
188 printf("no mmc device at slot %x\n", curr_device);
Lei Wen8f3b9642010-09-13 22:07:28 +0800189 return 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500190 }
191
Lei Wenbc897b12011-05-02 16:26:26 +0000192 mmc->has_init = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500193
Michael Jones8fd01b82011-07-14 23:09:43 +0000194 if (mmc_init(mmc))
195 return 1;
196 else
197 return 0;
Lei Wenea6ebe22011-05-02 16:26:25 +0000198 } else if (strncmp(argv[1], "part", 4) == 0) {
199 block_dev_desc_t *mmc_dev;
Stephen Warren9fd38372013-04-01 11:50:28 +0000200 struct mmc *mmc;
Lei Wenea6ebe22011-05-02 16:26:25 +0000201
Stephen Warren9fd38372013-04-01 11:50:28 +0000202 if (argc != 2)
203 return CMD_RET_USAGE;
204
205 mmc = find_mmc_device(curr_device);
Lei Wenea6ebe22011-05-02 16:26:25 +0000206 if (!mmc) {
207 printf("no mmc device at slot %x\n", curr_device);
208 return 1;
209 }
210 mmc_init(mmc);
211 mmc_dev = mmc_get_dev(curr_device);
212 if (mmc_dev != NULL &&
213 mmc_dev->type != DEV_TYPE_UNKNOWN) {
214 print_part(mmc_dev);
Andy Fleming272cc702008-10-30 16:41:01 -0500215 return 0;
216 }
Lei Wenea6ebe22011-05-02 16:26:25 +0000217
218 puts("get mmc type error!\n");
Andy Fleming272cc702008-10-30 16:41:01 -0500219 return 1;
Lei Wenea6ebe22011-05-02 16:26:25 +0000220 } else if (strcmp(argv[1], "list") == 0) {
Stephen Warren9fd38372013-04-01 11:50:28 +0000221 if (argc != 2)
222 return CMD_RET_USAGE;
Lei Wenea6ebe22011-05-02 16:26:25 +0000223 print_mmc_devices('\n');
224 return 0;
225 } else if (strcmp(argv[1], "dev") == 0) {
Lei Wenbc897b12011-05-02 16:26:26 +0000226 int dev, part = -1;
Lei Wenea6ebe22011-05-02 16:26:25 +0000227 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500228
Lei Wenea6ebe22011-05-02 16:26:25 +0000229 if (argc == 2)
230 dev = curr_device;
231 else if (argc == 3)
232 dev = simple_strtoul(argv[2], NULL, 10);
Lei Wenbc897b12011-05-02 16:26:26 +0000233 else if (argc == 4) {
234 dev = (int)simple_strtoul(argv[2], NULL, 10);
235 part = (int)simple_strtoul(argv[3], NULL, 10);
236 if (part > PART_ACCESS_MASK) {
237 printf("#part_num shouldn't be larger"
238 " than %d\n", PART_ACCESS_MASK);
239 return 1;
240 }
241 } else
Simon Glass4c12eeb2011-12-10 08:44:01 +0000242 return CMD_RET_USAGE;
Rabin Vincente85649c2009-04-05 13:30:53 +0530243
Lei Wenea6ebe22011-05-02 16:26:25 +0000244 mmc = find_mmc_device(dev);
245 if (!mmc) {
246 printf("no mmc device at slot %x\n", dev);
247 return 1;
248 }
Andy Fleming272cc702008-10-30 16:41:01 -0500249
Lei Wenbc897b12011-05-02 16:26:26 +0000250 mmc_init(mmc);
251 if (part != -1) {
252 int ret;
253 if (mmc->part_config == MMCPART_NOAVAILABLE) {
254 printf("Card doesn't support part_switch\n");
255 return 1;
256 }
257
258 if (part != mmc->part_num) {
259 ret = mmc_switch_part(dev, part);
260 if (!ret)
261 mmc->part_num = part;
262
263 printf("switch to partions #%d, %s\n",
264 part, (!ret) ? "OK" : "ERROR");
265 }
266 }
Lei Wenea6ebe22011-05-02 16:26:25 +0000267 curr_device = dev;
Lei Wenbc897b12011-05-02 16:26:26 +0000268 if (mmc->part_config == MMCPART_NOAVAILABLE)
269 printf("mmc%d is current device\n", curr_device);
270 else
271 printf("mmc%d(part %d) is current device\n",
272 curr_device, mmc->part_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500273
Lei Wenea6ebe22011-05-02 16:26:25 +0000274 return 0;
Amar2a91c912013-04-27 11:43:00 +0530275#ifdef CONFIG_SUPPORT_EMMC_BOOT
276 } else if ((strcmp(argv[1], "open") == 0) ||
277 (strcmp(argv[1], "close") == 0)) {
278 int dev;
279 struct mmc *mmc;
280 u8 part_num, access = 0;
Lei Wen6be95cc2011-06-22 17:03:30 +0000281
Amar2a91c912013-04-27 11:43:00 +0530282 if (argc == 4) {
283 dev = simple_strtoul(argv[2], NULL, 10);
284 part_num = simple_strtoul(argv[3], NULL, 10);
285 } else {
286 return CMD_RET_USAGE;
287 }
288
289 mmc = find_mmc_device(dev);
290 if (!mmc) {
291 printf("no mmc device at slot %x\n", dev);
292 return 1;
293 }
294
295 if (IS_SD(mmc)) {
296 printf("SD device cannot be opened/closed\n");
297 return 1;
298 }
299
300 if ((part_num <= 0) || (part_num > MMC_NUM_BOOT_PARTITION)) {
301 printf("Invalid boot partition number:\n");
302 printf("Boot partition number cannot be <= 0\n");
303 printf("EMMC44 supports only 2 boot partitions\n");
304 return 1;
305 }
306
307 if (strcmp(argv[1], "open") == 0)
308 access = part_num; /* enable R/W access to boot part*/
309 else
310 access = 0; /* No access to boot partition */
311
312 /* acknowledge to be sent during boot operation */
313 return boot_part_access(mmc, 1, part_num, access);
314
315 } else if (strcmp(argv[1], "bootpart") == 0) {
316 int dev;
317 dev = simple_strtoul(argv[2], NULL, 10);
318
319 u32 bootsize = simple_strtoul(argv[3], NULL, 10);
320 u32 rpmbsize = simple_strtoul(argv[4], NULL, 10);
321 struct mmc *mmc = find_mmc_device(dev);
322 if (!mmc) {
323 printf("no mmc device at slot %x\n", dev);
324 return 1;
325 }
326
327 if (IS_SD(mmc)) {
328 printf("It is not a EMMC device\n");
329 return 1;
330 }
331
332 if (0 == mmc_boot_partition_size_change(mmc,
333 bootsize, rpmbsize)) {
334 printf("EMMC boot partition Size %d MB\n", bootsize);
335 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
336 return 0;
337 } else {
338 printf("EMMC boot partition Size change Failed.\n");
339 return 1;
340 }
341#endif /* CONFIG_SUPPORT_EMMC_BOOT */
342 }
Taylor Hutted80c932012-11-22 09:13:00 +0000343 state = MMC_INVALID;
344 if (argc == 5 && strcmp(argv[1], "read") == 0)
Lei Wen6be95cc2011-06-22 17:03:30 +0000345 state = MMC_READ;
Taylor Hutted80c932012-11-22 09:13:00 +0000346 else if (argc == 5 && strcmp(argv[1], "write") == 0)
Lei Wen6be95cc2011-06-22 17:03:30 +0000347 state = MMC_WRITE;
Taylor Hutted80c932012-11-22 09:13:00 +0000348 else if (argc == 4 && strcmp(argv[1], "erase") == 0)
Lei Wene6f99a52011-06-22 17:03:31 +0000349 state = MMC_ERASE;
Lei Wen6be95cc2011-06-22 17:03:30 +0000350
351 if (state != MMC_INVALID) {
352 struct mmc *mmc = find_mmc_device(curr_device);
Lei Wene6f99a52011-06-22 17:03:31 +0000353 int idx = 2;
354 u32 blk, cnt, n;
355 void *addr;
356
357 if (state != MMC_ERASE) {
358 addr = (void *)simple_strtoul(argv[idx], NULL, 16);
359 ++idx;
360 } else
Kim Phillips088f1b12012-10-29 13:34:31 +0000361 addr = NULL;
Lei Wene6f99a52011-06-22 17:03:31 +0000362 blk = simple_strtoul(argv[idx], NULL, 16);
363 cnt = simple_strtoul(argv[idx + 1], NULL, 16);
Andy Fleming272cc702008-10-30 16:41:01 -0500364
Lei Wenea6ebe22011-05-02 16:26:25 +0000365 if (!mmc) {
366 printf("no mmc device at slot %x\n", curr_device);
367 return 1;
368 }
Rabin Vincente85649c2009-04-05 13:30:53 +0530369
Lei Wen6be95cc2011-06-22 17:03:30 +0000370 printf("\nMMC %s: dev # %d, block # %d, count %d ... ",
371 argv[1], curr_device, blk, cnt);
Andy Fleming272cc702008-10-30 16:41:01 -0500372
Lei Wenea6ebe22011-05-02 16:26:25 +0000373 mmc_init(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -0500374
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +0000375 if ((state == MMC_WRITE || state == MMC_ERASE)) {
376 if (mmc_getwp(mmc) == 1) {
377 printf("Error: card is write protected!\n");
378 return 1;
379 }
380 }
381
Lei Wen6be95cc2011-06-22 17:03:30 +0000382 switch (state) {
383 case MMC_READ:
384 n = mmc->block_dev.block_read(curr_device, blk,
385 cnt, addr);
386 /* flush cache after read */
387 flush_cache((ulong)addr, cnt * 512); /* FIXME */
388 break;
389 case MMC_WRITE:
390 n = mmc->block_dev.block_write(curr_device, blk,
391 cnt, addr);
392 break;
Lei Wene6f99a52011-06-22 17:03:31 +0000393 case MMC_ERASE:
394 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
395 break;
Lei Wen6be95cc2011-06-22 17:03:30 +0000396 default:
397 BUG();
398 }
Andy Fleming272cc702008-10-30 16:41:01 -0500399
Lei Wen6be95cc2011-06-22 17:03:30 +0000400 printf("%d blocks %s: %s\n",
401 n, argv[1], (n == cnt) ? "OK" : "ERROR");
Lei Wenea6ebe22011-05-02 16:26:25 +0000402 return (n == cnt) ? 0 : 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500403 }
Lei Wenea6ebe22011-05-02 16:26:25 +0000404
Simon Glass4c12eeb2011-12-10 08:44:01 +0000405 return CMD_RET_USAGE;
Andy Fleming272cc702008-10-30 16:41:01 -0500406}
407
408U_BOOT_CMD(
409 mmc, 6, 1, do_mmcops,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400410 "MMC sub system",
Lei Wenea6ebe22011-05-02 16:26:25 +0000411 "read addr blk# cnt\n"
412 "mmc write addr blk# cnt\n"
Lei Wene6f99a52011-06-22 17:03:31 +0000413 "mmc erase blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000414 "mmc rescan\n"
415 "mmc part - lists available partition on current mmc device\n"
Lei Wenbc897b12011-05-02 16:26:26 +0000416 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
Amar2a91c912013-04-27 11:43:00 +0530417 "mmc list - lists available devices\n"
418#ifdef CONFIG_SUPPORT_EMMC_BOOT
419 "mmc open <dev> <boot_partition>\n"
420 " - Enable boot_part for booting and enable R/W access of boot_part\n"
421 "mmc close <dev> <boot_partition>\n"
422 " - Enable boot_part for booting and disable access to boot_part\n"
423 "mmc bootpart <device num> <boot part size MB> <RPMB part size MB>\n"
424 " - change sizes of boot and RPMB partions of specified device\n"
Dirk Behme3511b4e2009-02-18 19:59:39 +0100425#endif
Amar2a91c912013-04-27 11:43:00 +0530426 );
427#endif /* !CONFIG_GENERIC_MMC */