blob: c50b6d31c9b22a40d67d78f26e812ef59a8eaf3d [file] [log] [blame]
wdenk71f95112003-06-15 22:40:42 +00001/*
2 * (C) Copyright 2003
3 * Kyle Harris, kharris@nexus-tech.net
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <command.h>
wdenk71f95112003-06-15 22:40:42 +000026#include <mmc.h>
27
Mike Frysinger02e22c22009-06-14 21:35:21 -040028static int curr_device = -1;
Lei Wenea6ebe22011-05-02 16:26:25 +000029#ifndef CONFIG_GENERIC_MMC
Wolfgang Denk54841ab2010-06-28 22:00:46 +020030int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk71f95112003-06-15 22:40:42 +000031{
Minkyu Kang869f6bf2009-03-30 14:55:51 +090032 int dev;
33
Wolfgang Denk47e26b12010-07-17 01:06:04 +020034 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +000035 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090036
37 if (strcmp(argv[1], "init") == 0) {
38 if (argc == 2) {
39 if (curr_device < 0)
40 dev = 1;
41 else
42 dev = curr_device;
43 } else if (argc == 3) {
44 dev = (int)simple_strtoul(argv[2], NULL, 10);
45 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +000046 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090047 }
48
49 if (mmc_legacy_init(dev) != 0) {
50 puts("No MMC card found\n");
51 return 1;
52 }
53
54 curr_device = dev;
55 printf("mmc%d is available\n", curr_device);
56 } else if (strcmp(argv[1], "device") == 0) {
57 if (argc == 2) {
58 if (curr_device < 0) {
59 puts("No MMC device available\n");
60 return 1;
61 }
62 } else if (argc == 3) {
63 dev = (int)simple_strtoul(argv[2], NULL, 10);
64
65#ifdef CONFIG_SYS_MMC_SET_DEV
66 if (mmc_set_dev(dev) != 0)
67 return 1;
68#endif
69 curr_device = dev;
70 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +000071 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090072 }
73
74 printf("mmc%d is current device\n", curr_device);
75 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +000076 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090077 }
78
wdenk71f95112003-06-15 22:40:42 +000079 return 0;
80}
81
wdenk0d498392003-07-01 21:06:45 +000082U_BOOT_CMD(
Minkyu Kang869f6bf2009-03-30 14:55:51 +090083 mmc, 3, 1, do_mmc,
84 "MMC sub-system",
85 "init [dev] - init MMC sub system\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +020086 "mmc device [dev] - show or set current device"
wdenkb0fce992003-06-29 21:03:46 +000087);
Dirk Behme3511b4e2009-02-18 19:59:39 +010088#else /* !CONFIG_GENERIC_MMC */
Andy Fleming272cc702008-10-30 16:41:01 -050089
Lei Wen6be95cc2011-06-22 17:03:30 +000090enum mmc_state {
91 MMC_INVALID,
92 MMC_READ,
93 MMC_WRITE,
Lei Wene6f99a52011-06-22 17:03:31 +000094 MMC_ERASE,
Lei Wen6be95cc2011-06-22 17:03:30 +000095};
Andy Fleming272cc702008-10-30 16:41:01 -050096static void print_mmcinfo(struct mmc *mmc)
97{
98 printf("Device: %s\n", mmc->name);
99 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
100 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
101 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
102 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
103 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
104
105 printf("Tran Speed: %d\n", mmc->tran_speed);
106 printf("Rd Block Len: %d\n", mmc->read_bl_len);
107
108 printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
109 (mmc->version >> 4) & 0xf, mmc->version & 0xf);
110
111 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang940e0782011-01-04 01:04:19 +0000112 puts("Capacity: ");
113 print_size(mmc->capacity, "\n");
Andy Fleming272cc702008-10-30 16:41:01 -0500114
115 printf("Bus Width: %d-bit\n", mmc->bus_width);
116}
117
Kim Phillips088f1b12012-10-29 13:34:31 +0000118static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500119{
120 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500121
Lei Wenea6ebe22011-05-02 16:26:25 +0000122 if (curr_device < 0) {
123 if (get_mmc_num() > 0)
124 curr_device = 0;
125 else {
126 puts("No MMC device available\n");
127 return 1;
128 }
129 }
Andy Fleming272cc702008-10-30 16:41:01 -0500130
Lei Wenea6ebe22011-05-02 16:26:25 +0000131 mmc = find_mmc_device(curr_device);
Andy Fleming272cc702008-10-30 16:41:01 -0500132
133 if (mmc) {
134 mmc_init(mmc);
135
136 print_mmcinfo(mmc);
Lei Wenea6ebe22011-05-02 16:26:25 +0000137 return 0;
138 } else {
139 printf("no mmc device at slot %x\n", curr_device);
140 return 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500141 }
Andy Fleming272cc702008-10-30 16:41:01 -0500142}
143
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200144U_BOOT_CMD(
Lei Wenea6ebe22011-05-02 16:26:25 +0000145 mmcinfo, 1, 0, do_mmcinfo,
Frans Meulenbroekscc9f6072010-07-31 15:01:52 +0200146 "display MMC info",
Benoît Thébaudeaud1424ee2012-08-10 09:07:38 +0000147 "- dislay info of the current MMC device"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200148);
Andy Fleming272cc702008-10-30 16:41:01 -0500149
Kim Phillips088f1b12012-10-29 13:34:31 +0000150static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500151{
Lei Wen6be95cc2011-06-22 17:03:30 +0000152 enum mmc_state state;
153
Lei Wenea6ebe22011-05-02 16:26:25 +0000154 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000155 return CMD_RET_USAGE;
Andy Fleming272cc702008-10-30 16:41:01 -0500156
Lei Wenea6ebe22011-05-02 16:26:25 +0000157 if (curr_device < 0) {
158 if (get_mmc_num() > 0)
159 curr_device = 0;
160 else {
161 puts("No MMC device available\n");
162 return 1;
163 }
164 }
Andy Fleming272cc702008-10-30 16:41:01 -0500165
Lei Wenea6ebe22011-05-02 16:26:25 +0000166 if (strcmp(argv[1], "rescan") == 0) {
167 struct mmc *mmc = find_mmc_device(curr_device);
Rabin Vincente85649c2009-04-05 13:30:53 +0530168
Lei Wenea6ebe22011-05-02 16:26:25 +0000169 if (!mmc) {
170 printf("no mmc device at slot %x\n", curr_device);
Lei Wen8f3b9642010-09-13 22:07:28 +0800171 return 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500172 }
173
Lei Wenbc897b12011-05-02 16:26:26 +0000174 mmc->has_init = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500175
Michael Jones8fd01b82011-07-14 23:09:43 +0000176 if (mmc_init(mmc))
177 return 1;
178 else
179 return 0;
Lei Wenea6ebe22011-05-02 16:26:25 +0000180 } else if (strncmp(argv[1], "part", 4) == 0) {
181 block_dev_desc_t *mmc_dev;
182 struct mmc *mmc = find_mmc_device(curr_device);
183
184 if (!mmc) {
185 printf("no mmc device at slot %x\n", curr_device);
186 return 1;
187 }
188 mmc_init(mmc);
189 mmc_dev = mmc_get_dev(curr_device);
190 if (mmc_dev != NULL &&
191 mmc_dev->type != DEV_TYPE_UNKNOWN) {
192 print_part(mmc_dev);
Andy Fleming272cc702008-10-30 16:41:01 -0500193 return 0;
194 }
Lei Wenea6ebe22011-05-02 16:26:25 +0000195
196 puts("get mmc type error!\n");
Andy Fleming272cc702008-10-30 16:41:01 -0500197 return 1;
Lei Wenea6ebe22011-05-02 16:26:25 +0000198 } else if (strcmp(argv[1], "list") == 0) {
199 print_mmc_devices('\n');
200 return 0;
201 } else if (strcmp(argv[1], "dev") == 0) {
Inderpal Singh5b93b272012-12-07 10:34:42 +0530202 int dev, part = -1, bootable = 0;
Lei Wenea6ebe22011-05-02 16:26:25 +0000203 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500204
Lei Wenea6ebe22011-05-02 16:26:25 +0000205 if (argc == 2)
206 dev = curr_device;
207 else if (argc == 3)
208 dev = simple_strtoul(argv[2], NULL, 10);
Lei Wenbc897b12011-05-02 16:26:26 +0000209 else if (argc == 4) {
210 dev = (int)simple_strtoul(argv[2], NULL, 10);
211 part = (int)simple_strtoul(argv[3], NULL, 10);
212 if (part > PART_ACCESS_MASK) {
213 printf("#part_num shouldn't be larger"
214 " than %d\n", PART_ACCESS_MASK);
215 return 1;
216 }
Inderpal Singh5b93b272012-12-07 10:34:42 +0530217 } else if (argc == 5) {
218 dev = (int)simple_strtoul(argv[2], NULL, 10);
219 part = (int)simple_strtoul(argv[3], NULL, 10);
220 if (part > PART_ACCESS_MASK) {
221 printf("#part_num shouldn't be larger"
222 " than %d\n", PART_ACCESS_MASK);
223 return 1;
224 }
225 bootable = (int)simple_strtoul(argv[4], NULL, 10);
226 if (bootable < 0 || bootable > 1) {
227 printf("bootable is boolean variable \n");
228 return 1;
229 }
Lei Wenbc897b12011-05-02 16:26:26 +0000230 } else
Simon Glass4c12eeb2011-12-10 08:44:01 +0000231 return CMD_RET_USAGE;
Rabin Vincente85649c2009-04-05 13:30:53 +0530232
Lei Wenea6ebe22011-05-02 16:26:25 +0000233 mmc = find_mmc_device(dev);
234 if (!mmc) {
235 printf("no mmc device at slot %x\n", dev);
236 return 1;
237 }
Andy Fleming272cc702008-10-30 16:41:01 -0500238
Lei Wenbc897b12011-05-02 16:26:26 +0000239 mmc_init(mmc);
240 if (part != -1) {
241 int ret;
242 if (mmc->part_config == MMCPART_NOAVAILABLE) {
243 printf("Card doesn't support part_switch\n");
244 return 1;
245 }
246
Inderpal Singh5b93b272012-12-07 10:34:42 +0530247 if (bootable) {
248 ret = mmc_part_bootenable(dev, part);
249 if (!ret)
250 mmc->part_num = part;
251 printf("partions #%d, made bootable %s\n",
252 part, (!ret) ? "OK" : "ERROR");
253
254 } else if (part != mmc->part_num) {
Lei Wenbc897b12011-05-02 16:26:26 +0000255 ret = mmc_switch_part(dev, part);
256 if (!ret)
257 mmc->part_num = part;
258
259 printf("switch to partions #%d, %s\n",
260 part, (!ret) ? "OK" : "ERROR");
261 }
262 }
Lei Wenea6ebe22011-05-02 16:26:25 +0000263 curr_device = dev;
Lei Wenbc897b12011-05-02 16:26:26 +0000264 if (mmc->part_config == MMCPART_NOAVAILABLE)
265 printf("mmc%d is current device\n", curr_device);
266 else
267 printf("mmc%d(part %d) is current device\n",
268 curr_device, mmc->part_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500269
Lei Wenea6ebe22011-05-02 16:26:25 +0000270 return 0;
Lei Wen6be95cc2011-06-22 17:03:30 +0000271 }
272
273 if (strcmp(argv[1], "read") == 0)
274 state = MMC_READ;
275 else if (strcmp(argv[1], "write") == 0)
276 state = MMC_WRITE;
Lei Wene6f99a52011-06-22 17:03:31 +0000277 else if (strcmp(argv[1], "erase") == 0)
278 state = MMC_ERASE;
Lei Wen6be95cc2011-06-22 17:03:30 +0000279 else
280 state = MMC_INVALID;
281
282 if (state != MMC_INVALID) {
283 struct mmc *mmc = find_mmc_device(curr_device);
Lei Wene6f99a52011-06-22 17:03:31 +0000284 int idx = 2;
285 u32 blk, cnt, n;
286 void *addr;
287
288 if (state != MMC_ERASE) {
289 addr = (void *)simple_strtoul(argv[idx], NULL, 16);
290 ++idx;
291 } else
Kim Phillips088f1b12012-10-29 13:34:31 +0000292 addr = NULL;
Lei Wene6f99a52011-06-22 17:03:31 +0000293 blk = simple_strtoul(argv[idx], NULL, 16);
294 cnt = simple_strtoul(argv[idx + 1], NULL, 16);
Andy Fleming272cc702008-10-30 16:41:01 -0500295
Lei Wenea6ebe22011-05-02 16:26:25 +0000296 if (!mmc) {
297 printf("no mmc device at slot %x\n", curr_device);
298 return 1;
299 }
Rabin Vincente85649c2009-04-05 13:30:53 +0530300
Lei Wen6be95cc2011-06-22 17:03:30 +0000301 printf("\nMMC %s: dev # %d, block # %d, count %d ... ",
302 argv[1], curr_device, blk, cnt);
Andy Fleming272cc702008-10-30 16:41:01 -0500303
Lei Wenea6ebe22011-05-02 16:26:25 +0000304 mmc_init(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -0500305
Lei Wen6be95cc2011-06-22 17:03:30 +0000306 switch (state) {
307 case MMC_READ:
308 n = mmc->block_dev.block_read(curr_device, blk,
309 cnt, addr);
310 /* flush cache after read */
311 flush_cache((ulong)addr, cnt * 512); /* FIXME */
312 break;
313 case MMC_WRITE:
314 n = mmc->block_dev.block_write(curr_device, blk,
315 cnt, addr);
316 break;
Lei Wene6f99a52011-06-22 17:03:31 +0000317 case MMC_ERASE:
318 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
319 break;
Lei Wen6be95cc2011-06-22 17:03:30 +0000320 default:
321 BUG();
322 }
Andy Fleming272cc702008-10-30 16:41:01 -0500323
Lei Wen6be95cc2011-06-22 17:03:30 +0000324 printf("%d blocks %s: %s\n",
325 n, argv[1], (n == cnt) ? "OK" : "ERROR");
Lei Wenea6ebe22011-05-02 16:26:25 +0000326 return (n == cnt) ? 0 : 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500327 }
Lei Wenea6ebe22011-05-02 16:26:25 +0000328
Simon Glass4c12eeb2011-12-10 08:44:01 +0000329 return CMD_RET_USAGE;
Andy Fleming272cc702008-10-30 16:41:01 -0500330}
331
332U_BOOT_CMD(
333 mmc, 6, 1, do_mmcops,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400334 "MMC sub system",
Lei Wenea6ebe22011-05-02 16:26:25 +0000335 "read addr blk# cnt\n"
336 "mmc write addr blk# cnt\n"
Lei Wene6f99a52011-06-22 17:03:31 +0000337 "mmc erase blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000338 "mmc rescan\n"
339 "mmc part - lists available partition on current mmc device\n"
Inderpal Singh5b93b272012-12-07 10:34:42 +0530340 "mmc dev [dev] [part] [bootable]- show or set current mmc device [partition] [bootable]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200341 "mmc list - lists available devices");
Dirk Behme3511b4e2009-02-18 19:59:39 +0100342#endif