blob: 6820c56532c866802c39216adbe62c78bcbec485 [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>
John Rigbya637d7b2012-01-19 10:08:32 -070025#include <config.h>
wdenk71f95112003-06-15 22:40:42 +000026#include <command.h>
wdenk71f95112003-06-15 22:40:42 +000027#include <mmc.h>
28
John Rigbya637d7b2012-01-19 10:08:32 -070029#ifndef CONFIG_SNOWBALL_MMC_HACK
Mike Frysinger02e22c22009-06-14 21:35:21 -040030static int curr_device = -1;
Lei Wenea6ebe22011-05-02 16:26:25 +000031#ifndef CONFIG_GENERIC_MMC
Wolfgang Denk54841ab2010-06-28 22:00:46 +020032int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk71f95112003-06-15 22:40:42 +000033{
Minkyu Kang869f6bf2009-03-30 14:55:51 +090034 int dev;
35
Wolfgang Denk47e26b12010-07-17 01:06:04 +020036 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +000037 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090038
39 if (strcmp(argv[1], "init") == 0) {
40 if (argc == 2) {
41 if (curr_device < 0)
42 dev = 1;
43 else
44 dev = curr_device;
45 } else if (argc == 3) {
46 dev = (int)simple_strtoul(argv[2], NULL, 10);
47 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +000048 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090049 }
50
51 if (mmc_legacy_init(dev) != 0) {
52 puts("No MMC card found\n");
53 return 1;
54 }
55
56 curr_device = dev;
57 printf("mmc%d is available\n", curr_device);
58 } else if (strcmp(argv[1], "device") == 0) {
59 if (argc == 2) {
60 if (curr_device < 0) {
61 puts("No MMC device available\n");
62 return 1;
63 }
64 } else if (argc == 3) {
65 dev = (int)simple_strtoul(argv[2], NULL, 10);
66
67#ifdef CONFIG_SYS_MMC_SET_DEV
68 if (mmc_set_dev(dev) != 0)
69 return 1;
70#endif
71 curr_device = dev;
72 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +000073 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090074 }
75
76 printf("mmc%d is current device\n", curr_device);
77 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +000078 return CMD_RET_USAGE;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090079 }
80
wdenk71f95112003-06-15 22:40:42 +000081 return 0;
82}
83
wdenk0d498392003-07-01 21:06:45 +000084U_BOOT_CMD(
Minkyu Kang869f6bf2009-03-30 14:55:51 +090085 mmc, 3, 1, do_mmc,
86 "MMC sub-system",
87 "init [dev] - init MMC sub system\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +020088 "mmc device [dev] - show or set current device"
wdenkb0fce992003-06-29 21:03:46 +000089);
Dirk Behme3511b4e2009-02-18 19:59:39 +010090#else /* !CONFIG_GENERIC_MMC */
Andy Fleming272cc702008-10-30 16:41:01 -050091
Lei Wen6be95cc2011-06-22 17:03:30 +000092enum mmc_state {
93 MMC_INVALID,
94 MMC_READ,
95 MMC_WRITE,
Lei Wene6f99a52011-06-22 17:03:31 +000096 MMC_ERASE,
Lei Wen6be95cc2011-06-22 17:03:30 +000097};
Andy Fleming272cc702008-10-30 16:41:01 -050098static void print_mmcinfo(struct mmc *mmc)
99{
100 printf("Device: %s\n", mmc->name);
101 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
102 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
103 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
104 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
105 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
106
107 printf("Tran Speed: %d\n", mmc->tran_speed);
108 printf("Rd Block Len: %d\n", mmc->read_bl_len);
109
110 printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
111 (mmc->version >> 4) & 0xf, mmc->version & 0xf);
112
113 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang940e0782011-01-04 01:04:19 +0000114 puts("Capacity: ");
115 print_size(mmc->capacity, "\n");
Andy Fleming272cc702008-10-30 16:41:01 -0500116
117 printf("Bus Width: %d-bit\n", mmc->bus_width);
118}
119
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200120int do_mmcinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500121{
122 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500123
Lei Wenea6ebe22011-05-02 16:26:25 +0000124 if (curr_device < 0) {
125 if (get_mmc_num() > 0)
126 curr_device = 0;
127 else {
128 puts("No MMC device available\n");
129 return 1;
130 }
131 }
Andy Fleming272cc702008-10-30 16:41:01 -0500132
Lei Wenea6ebe22011-05-02 16:26:25 +0000133 mmc = find_mmc_device(curr_device);
Andy Fleming272cc702008-10-30 16:41:01 -0500134
135 if (mmc) {
136 mmc_init(mmc);
137
138 print_mmcinfo(mmc);
Lei Wenea6ebe22011-05-02 16:26:25 +0000139 return 0;
140 } else {
141 printf("no mmc device at slot %x\n", curr_device);
142 return 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500143 }
Andy Fleming272cc702008-10-30 16:41:01 -0500144}
145
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200146U_BOOT_CMD(
Lei Wenea6ebe22011-05-02 16:26:25 +0000147 mmcinfo, 1, 0, do_mmcinfo,
Frans Meulenbroekscc9f6072010-07-31 15:01:52 +0200148 "display MMC info",
Wolfgang Denk2d941de2010-09-10 00:16:19 +0200149 " - device number of the device to dislay info of\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200150 ""
151);
Andy Fleming272cc702008-10-30 16:41:01 -0500152
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200153int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500154{
Lei Wen6be95cc2011-06-22 17:03:30 +0000155 enum mmc_state state;
156
Lei Wenea6ebe22011-05-02 16:26:25 +0000157 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000158 return CMD_RET_USAGE;
Andy Fleming272cc702008-10-30 16:41:01 -0500159
Lei Wenea6ebe22011-05-02 16:26:25 +0000160 if (curr_device < 0) {
161 if (get_mmc_num() > 0)
162 curr_device = 0;
163 else {
164 puts("No MMC device available\n");
165 return 1;
166 }
167 }
Andy Fleming272cc702008-10-30 16:41:01 -0500168
Lei Wenea6ebe22011-05-02 16:26:25 +0000169 if (strcmp(argv[1], "rescan") == 0) {
170 struct mmc *mmc = find_mmc_device(curr_device);
Rabin Vincente85649c2009-04-05 13:30:53 +0530171
Lei Wenea6ebe22011-05-02 16:26:25 +0000172 if (!mmc) {
173 printf("no mmc device at slot %x\n", curr_device);
Lei Wen8f3b9642010-09-13 22:07:28 +0800174 return 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500175 }
176
Lei Wenbc897b12011-05-02 16:26:26 +0000177 mmc->has_init = 0;
Andy Fleming272cc702008-10-30 16:41:01 -0500178
Michael Jones8fd01b82011-07-14 23:09:43 +0000179 if (mmc_init(mmc))
180 return 1;
181 else
182 return 0;
Lei Wenea6ebe22011-05-02 16:26:25 +0000183 } else if (strncmp(argv[1], "part", 4) == 0) {
184 block_dev_desc_t *mmc_dev;
185 struct mmc *mmc = find_mmc_device(curr_device);
186
187 if (!mmc) {
188 printf("no mmc device at slot %x\n", curr_device);
189 return 1;
190 }
191 mmc_init(mmc);
192 mmc_dev = mmc_get_dev(curr_device);
193 if (mmc_dev != NULL &&
194 mmc_dev->type != DEV_TYPE_UNKNOWN) {
195 print_part(mmc_dev);
Andy Fleming272cc702008-10-30 16:41:01 -0500196 return 0;
197 }
Lei Wenea6ebe22011-05-02 16:26:25 +0000198
199 puts("get mmc type error!\n");
Andy Fleming272cc702008-10-30 16:41:01 -0500200 return 1;
Lei Wenea6ebe22011-05-02 16:26:25 +0000201 } else if (strcmp(argv[1], "list") == 0) {
202 print_mmc_devices('\n');
203 return 0;
204 } else if (strcmp(argv[1], "dev") == 0) {
Lei Wenbc897b12011-05-02 16:26:26 +0000205 int dev, part = -1;
Lei Wenea6ebe22011-05-02 16:26:25 +0000206 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500207
Lei Wenea6ebe22011-05-02 16:26:25 +0000208 if (argc == 2)
209 dev = curr_device;
210 else if (argc == 3)
211 dev = simple_strtoul(argv[2], NULL, 10);
Lei Wenbc897b12011-05-02 16:26:26 +0000212 else if (argc == 4) {
213 dev = (int)simple_strtoul(argv[2], NULL, 10);
214 part = (int)simple_strtoul(argv[3], NULL, 10);
215 if (part > PART_ACCESS_MASK) {
216 printf("#part_num shouldn't be larger"
217 " than %d\n", PART_ACCESS_MASK);
218 return 1;
219 }
220 } else
Simon Glass4c12eeb2011-12-10 08:44:01 +0000221 return CMD_RET_USAGE;
Rabin Vincente85649c2009-04-05 13:30:53 +0530222
Lei Wenea6ebe22011-05-02 16:26:25 +0000223 mmc = find_mmc_device(dev);
224 if (!mmc) {
225 printf("no mmc device at slot %x\n", dev);
226 return 1;
227 }
Andy Fleming272cc702008-10-30 16:41:01 -0500228
Lei Wenbc897b12011-05-02 16:26:26 +0000229 mmc_init(mmc);
230 if (part != -1) {
231 int ret;
232 if (mmc->part_config == MMCPART_NOAVAILABLE) {
233 printf("Card doesn't support part_switch\n");
234 return 1;
235 }
236
237 if (part != mmc->part_num) {
238 ret = mmc_switch_part(dev, part);
239 if (!ret)
240 mmc->part_num = part;
241
242 printf("switch to partions #%d, %s\n",
243 part, (!ret) ? "OK" : "ERROR");
244 }
245 }
Lei Wenea6ebe22011-05-02 16:26:25 +0000246 curr_device = dev;
Lei Wenbc897b12011-05-02 16:26:26 +0000247 if (mmc->part_config == MMCPART_NOAVAILABLE)
248 printf("mmc%d is current device\n", curr_device);
249 else
250 printf("mmc%d(part %d) is current device\n",
251 curr_device, mmc->part_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500252
Lei Wenea6ebe22011-05-02 16:26:25 +0000253 return 0;
Lei Wen6be95cc2011-06-22 17:03:30 +0000254 }
255
256 if (strcmp(argv[1], "read") == 0)
257 state = MMC_READ;
258 else if (strcmp(argv[1], "write") == 0)
259 state = MMC_WRITE;
Lei Wene6f99a52011-06-22 17:03:31 +0000260 else if (strcmp(argv[1], "erase") == 0)
261 state = MMC_ERASE;
Lei Wen6be95cc2011-06-22 17:03:30 +0000262 else
263 state = MMC_INVALID;
264
265 if (state != MMC_INVALID) {
266 struct mmc *mmc = find_mmc_device(curr_device);
Lei Wene6f99a52011-06-22 17:03:31 +0000267 int idx = 2;
268 u32 blk, cnt, n;
269 void *addr;
270
271 if (state != MMC_ERASE) {
272 addr = (void *)simple_strtoul(argv[idx], NULL, 16);
273 ++idx;
274 } else
275 addr = 0;
276 blk = simple_strtoul(argv[idx], NULL, 16);
277 cnt = simple_strtoul(argv[idx + 1], NULL, 16);
Andy Fleming272cc702008-10-30 16:41:01 -0500278
Lei Wenea6ebe22011-05-02 16:26:25 +0000279 if (!mmc) {
280 printf("no mmc device at slot %x\n", curr_device);
281 return 1;
282 }
Rabin Vincente85649c2009-04-05 13:30:53 +0530283
Lei Wen6be95cc2011-06-22 17:03:30 +0000284 printf("\nMMC %s: dev # %d, block # %d, count %d ... ",
285 argv[1], curr_device, blk, cnt);
Andy Fleming272cc702008-10-30 16:41:01 -0500286
Lei Wenea6ebe22011-05-02 16:26:25 +0000287 mmc_init(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -0500288
Lei Wen6be95cc2011-06-22 17:03:30 +0000289 switch (state) {
290 case MMC_READ:
291 n = mmc->block_dev.block_read(curr_device, blk,
292 cnt, addr);
293 /* flush cache after read */
294 flush_cache((ulong)addr, cnt * 512); /* FIXME */
295 break;
296 case MMC_WRITE:
297 n = mmc->block_dev.block_write(curr_device, blk,
298 cnt, addr);
299 break;
Lei Wene6f99a52011-06-22 17:03:31 +0000300 case MMC_ERASE:
301 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
302 break;
Lei Wen6be95cc2011-06-22 17:03:30 +0000303 default:
304 BUG();
305 }
Andy Fleming272cc702008-10-30 16:41:01 -0500306
Lei Wen6be95cc2011-06-22 17:03:30 +0000307 printf("%d blocks %s: %s\n",
308 n, argv[1], (n == cnt) ? "OK" : "ERROR");
Lei Wenea6ebe22011-05-02 16:26:25 +0000309 return (n == cnt) ? 0 : 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500310 }
Lei Wenea6ebe22011-05-02 16:26:25 +0000311
Simon Glass4c12eeb2011-12-10 08:44:01 +0000312 return CMD_RET_USAGE;
Andy Fleming272cc702008-10-30 16:41:01 -0500313}
314
315U_BOOT_CMD(
316 mmc, 6, 1, do_mmcops,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400317 "MMC sub system",
Lei Wenea6ebe22011-05-02 16:26:25 +0000318 "read addr blk# cnt\n"
319 "mmc write addr blk# cnt\n"
Lei Wene6f99a52011-06-22 17:03:31 +0000320 "mmc erase blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +0000321 "mmc rescan\n"
322 "mmc part - lists available partition on current mmc device\n"
Lei Wenbc897b12011-05-02 16:26:26 +0000323 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200324 "mmc list - lists available devices");
Dirk Behme3511b4e2009-02-18 19:59:39 +0100325#endif
John Rigbya637d7b2012-01-19 10:08:32 -0700326#endif /* !CONFIG_SNOWBALL_MMC_HACK */