blob: 8f13c22d9baba16c2ab6a975a129901987618b33 [file] [log] [blame]
wdenk7a428cc2003-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>
wdenk7a428cc2003-06-15 22:40:42 +000026#include <mmc.h>
27
Mike Frysinger0289fe62009-06-14 21:35:21 -040028static int curr_device = -1;
Lei Wend430d7c2011-05-02 16:26:25 +000029#ifndef CONFIG_GENERIC_MMC
Wolfgang Denk6262d0212010-06-28 22:00:46 +020030int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk7a428cc2003-06-15 22:40:42 +000031{
Minkyu Kang0f922902009-03-30 14:55:51 +090032 int dev;
33
Wolfgang Denk3b683112010-07-17 01:06:04 +020034 if (argc < 2)
35 return cmd_usage(cmdtp);
Minkyu Kang0f922902009-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 {
Wolfgang Denk3b683112010-07-17 01:06:04 +020046 return cmd_usage(cmdtp);
Minkyu Kang0f922902009-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 {
Wolfgang Denk3b683112010-07-17 01:06:04 +020071 return cmd_usage(cmdtp);
Minkyu Kang0f922902009-03-30 14:55:51 +090072 }
73
74 printf("mmc%d is current device\n", curr_device);
75 } else {
Wolfgang Denk3b683112010-07-17 01:06:04 +020076 return cmd_usage(cmdtp);
wdenk7a428cc2003-06-15 22:40:42 +000077 }
Minkyu Kang0f922902009-03-30 14:55:51 +090078
wdenk7a428cc2003-06-15 22:40:42 +000079 return 0;
80}
81
wdenkf287a242003-07-01 21:06:45 +000082U_BOOT_CMD(
Minkyu Kang0f922902009-03-30 14:55:51 +090083 mmc, 3, 1, do_mmc,
84 "MMC sub-system",
85 "init [dev] - init MMC sub system\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +020086 "mmc device [dev] - show or set current device"
wdenkf12e3962003-06-29 21:03:46 +000087);
Dirk Behmedf8852a2009-02-18 19:59:39 +010088#else /* !CONFIG_GENERIC_MMC */
Andy Flemingad347bb2008-10-30 16:41:01 -050089
Lei Wen64b2b1e2011-06-22 17:03:30 +000090enum mmc_state {
91 MMC_INVALID,
92 MMC_READ,
93 MMC_WRITE,
Lei Wenea526762011-06-22 17:03:31 +000094 MMC_ERASE,
Lei Wen64b2b1e2011-06-22 17:03:30 +000095};
Andy Flemingad347bb2008-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 Kang316f81a2011-01-04 01:04:19 +0000112 puts("Capacity: ");
113 print_size(mmc->capacity, "\n");
Andy Flemingad347bb2008-10-30 16:41:01 -0500114
115 printf("Bus Width: %d-bit\n", mmc->bus_width);
116}
117
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200118int do_mmcinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500119{
120 struct mmc *mmc;
Andy Flemingad347bb2008-10-30 16:41:01 -0500121
Lei Wend430d7c2011-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 Flemingad347bb2008-10-30 16:41:01 -0500130
Lei Wend430d7c2011-05-02 16:26:25 +0000131 mmc = find_mmc_device(curr_device);
Andy Flemingad347bb2008-10-30 16:41:01 -0500132
133 if (mmc) {
134 mmc_init(mmc);
135
136 print_mmcinfo(mmc);
Lei Wend430d7c2011-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 Flemingad347bb2008-10-30 16:41:01 -0500141 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500142}
143
Frans Meulenbroeks7675a092010-07-31 15:01:53 +0200144U_BOOT_CMD(
Lei Wend430d7c2011-05-02 16:26:25 +0000145 mmcinfo, 1, 0, do_mmcinfo,
Frans Meulenbroeks0dddc002010-07-31 15:01:52 +0200146 "display MMC info",
Wolfgang Denke9e8bab2010-09-10 00:16:19 +0200147 " - device number of the device to dislay info of\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200148 ""
149);
Andy Flemingad347bb2008-10-30 16:41:01 -0500150
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200151int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500152{
Lei Wen64b2b1e2011-06-22 17:03:30 +0000153 enum mmc_state state;
154
Lei Wend430d7c2011-05-02 16:26:25 +0000155 if (argc < 2)
156 return cmd_usage(cmdtp);
Andy Flemingad347bb2008-10-30 16:41:01 -0500157
Lei Wend430d7c2011-05-02 16:26:25 +0000158 if (curr_device < 0) {
159 if (get_mmc_num() > 0)
160 curr_device = 0;
161 else {
162 puts("No MMC device available\n");
163 return 1;
164 }
165 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500166
Lei Wend430d7c2011-05-02 16:26:25 +0000167 if (strcmp(argv[1], "rescan") == 0) {
168 struct mmc *mmc = find_mmc_device(curr_device);
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530169
Lei Wend430d7c2011-05-02 16:26:25 +0000170 if (!mmc) {
171 printf("no mmc device at slot %x\n", curr_device);
172 return 1;
173 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500174
Lei Wen31b99802011-05-02 16:26:26 +0000175 mmc->has_init = 0;
Lei Wen61dba932010-09-13 22:07:28 +0800176
Michael Jones67a8bd82011-07-14 23:09:43 +0000177 if (mmc_init(mmc))
178 return 1;
179 else
180 return 0;
Lei Wend430d7c2011-05-02 16:26:25 +0000181 } else if (strncmp(argv[1], "part", 4) == 0) {
182 block_dev_desc_t *mmc_dev;
183 struct mmc *mmc = find_mmc_device(curr_device);
Lei Wen61dba932010-09-13 22:07:28 +0800184
Lei Wend430d7c2011-05-02 16:26:25 +0000185 if (!mmc) {
186 printf("no mmc device at slot %x\n", curr_device);
Lei Wen61dba932010-09-13 22:07:28 +0800187 return 1;
Andy Flemingad347bb2008-10-30 16:41:01 -0500188 }
Lei Wend430d7c2011-05-02 16:26:25 +0000189 mmc_init(mmc);
190 mmc_dev = mmc_get_dev(curr_device);
191 if (mmc_dev != NULL &&
192 mmc_dev->type != DEV_TYPE_UNKNOWN) {
193 print_part(mmc_dev);
Andy Flemingad347bb2008-10-30 16:41:01 -0500194 return 0;
195 }
Lei Wend430d7c2011-05-02 16:26:25 +0000196
197 puts("get mmc type error!\n");
Andy Flemingad347bb2008-10-30 16:41:01 -0500198 return 1;
Lei Wend430d7c2011-05-02 16:26:25 +0000199 } else if (strcmp(argv[1], "list") == 0) {
200 print_mmc_devices('\n');
201 return 0;
202 } else if (strcmp(argv[1], "dev") == 0) {
Lei Wen31b99802011-05-02 16:26:26 +0000203 int dev, part = -1;
Lei Wend430d7c2011-05-02 16:26:25 +0000204 struct mmc *mmc;
Andy Flemingad347bb2008-10-30 16:41:01 -0500205
Lei Wend430d7c2011-05-02 16:26:25 +0000206 if (argc == 2)
207 dev = curr_device;
208 else if (argc == 3)
209 dev = simple_strtoul(argv[2], NULL, 10);
Lei Wen31b99802011-05-02 16:26:26 +0000210 else if (argc == 4) {
211 dev = (int)simple_strtoul(argv[2], NULL, 10);
212 part = (int)simple_strtoul(argv[3], NULL, 10);
213 if (part > PART_ACCESS_MASK) {
214 printf("#part_num shouldn't be larger"
215 " than %d\n", PART_ACCESS_MASK);
216 return 1;
217 }
218 } else
Lei Wend430d7c2011-05-02 16:26:25 +0000219 return cmd_usage(cmdtp);
220
221 mmc = find_mmc_device(dev);
222 if (!mmc) {
223 printf("no mmc device at slot %x\n", dev);
224 return 1;
225 }
226
Lei Wen31b99802011-05-02 16:26:26 +0000227 mmc_init(mmc);
228 if (part != -1) {
229 int ret;
230 if (mmc->part_config == MMCPART_NOAVAILABLE) {
231 printf("Card doesn't support part_switch\n");
232 return 1;
233 }
234
235 if (part != mmc->part_num) {
236 ret = mmc_switch_part(dev, part);
237 if (!ret)
238 mmc->part_num = part;
239
240 printf("switch to partions #%d, %s\n",
241 part, (!ret) ? "OK" : "ERROR");
242 }
243 }
Lei Wend430d7c2011-05-02 16:26:25 +0000244 curr_device = dev;
Lei Wen31b99802011-05-02 16:26:26 +0000245 if (mmc->part_config == MMCPART_NOAVAILABLE)
246 printf("mmc%d is current device\n", curr_device);
247 else
248 printf("mmc%d(part %d) is current device\n",
249 curr_device, mmc->part_num);
Lei Wend430d7c2011-05-02 16:26:25 +0000250
251 return 0;
Lei Wen64b2b1e2011-06-22 17:03:30 +0000252 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500253
Lei Wen64b2b1e2011-06-22 17:03:30 +0000254 if (strcmp(argv[1], "read") == 0)
255 state = MMC_READ;
256 else if (strcmp(argv[1], "write") == 0)
257 state = MMC_WRITE;
Lei Wenea526762011-06-22 17:03:31 +0000258 else if (strcmp(argv[1], "erase") == 0)
259 state = MMC_ERASE;
Lei Wen64b2b1e2011-06-22 17:03:30 +0000260 else
261 state = MMC_INVALID;
Andy Flemingad347bb2008-10-30 16:41:01 -0500262
Lei Wen64b2b1e2011-06-22 17:03:30 +0000263 if (state != MMC_INVALID) {
264 struct mmc *mmc = find_mmc_device(curr_device);
Lei Wenea526762011-06-22 17:03:31 +0000265 int idx = 2;
266 u32 blk, cnt, n;
267 void *addr;
268
269 if (state != MMC_ERASE) {
270 addr = (void *)simple_strtoul(argv[idx], NULL, 16);
271 ++idx;
272 } else
273 addr = 0;
274 blk = simple_strtoul(argv[idx], NULL, 16);
275 cnt = simple_strtoul(argv[idx + 1], NULL, 16);
Andy Flemingad347bb2008-10-30 16:41:01 -0500276
Lei Wend430d7c2011-05-02 16:26:25 +0000277 if (!mmc) {
278 printf("no mmc device at slot %x\n", curr_device);
279 return 1;
280 }
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530281
Lei Wen64b2b1e2011-06-22 17:03:30 +0000282 printf("\nMMC %s: dev # %d, block # %d, count %d ... ",
283 argv[1], curr_device, blk, cnt);
Andy Flemingad347bb2008-10-30 16:41:01 -0500284
Lei Wend430d7c2011-05-02 16:26:25 +0000285 mmc_init(mmc);
Andy Flemingad347bb2008-10-30 16:41:01 -0500286
Lei Wen64b2b1e2011-06-22 17:03:30 +0000287 switch (state) {
288 case MMC_READ:
289 n = mmc->block_dev.block_read(curr_device, blk,
290 cnt, addr);
291 /* flush cache after read */
292 flush_cache((ulong)addr, cnt * 512); /* FIXME */
293 break;
294 case MMC_WRITE:
295 n = mmc->block_dev.block_write(curr_device, blk,
296 cnt, addr);
297 break;
Lei Wenea526762011-06-22 17:03:31 +0000298 case MMC_ERASE:
299 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
300 break;
Lei Wen64b2b1e2011-06-22 17:03:30 +0000301 default:
302 BUG();
303 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500304
Lei Wen64b2b1e2011-06-22 17:03:30 +0000305 printf("%d blocks %s: %s\n",
306 n, argv[1], (n == cnt) ? "OK" : "ERROR");
Lei Wend430d7c2011-05-02 16:26:25 +0000307 return (n == cnt) ? 0 : 1;
Andy Flemingad347bb2008-10-30 16:41:01 -0500308 }
Lei Wend430d7c2011-05-02 16:26:25 +0000309
310 return cmd_usage(cmdtp);
Andy Flemingad347bb2008-10-30 16:41:01 -0500311}
312
313U_BOOT_CMD(
314 mmc, 6, 1, do_mmcops,
Mike Frysinger27e1e622009-03-23 22:27:34 -0400315 "MMC sub system",
Lei Wend430d7c2011-05-02 16:26:25 +0000316 "read addr blk# cnt\n"
317 "mmc write addr blk# cnt\n"
Lei Wenea526762011-06-22 17:03:31 +0000318 "mmc erase blk# cnt\n"
Lei Wend430d7c2011-05-02 16:26:25 +0000319 "mmc rescan\n"
320 "mmc part - lists available partition on current mmc device\n"
Lei Wen31b99802011-05-02 16:26:26 +0000321 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200322 "mmc list - lists available devices");
Dirk Behmedf8852a2009-02-18 19:59:39 +0100323#endif