blob: a6458037e229b7aa1d69a5602f821db3df5380ab [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)
35 return cmd_usage(cmdtp);
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 {
Wolfgang Denk47e26b12010-07-17 01:06:04 +020046 return cmd_usage(cmdtp);
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 {
Wolfgang Denk47e26b12010-07-17 01:06:04 +020071 return cmd_usage(cmdtp);
Minkyu Kang869f6bf2009-03-30 14:55:51 +090072 }
73
74 printf("mmc%d is current device\n", curr_device);
75 } else {
Wolfgang Denk47e26b12010-07-17 01:06:04 +020076 return cmd_usage(cmdtp);
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,
94};
Andy Fleming272cc702008-10-30 16:41:01 -050095static void print_mmcinfo(struct mmc *mmc)
96{
97 printf("Device: %s\n", mmc->name);
98 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
99 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
100 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
101 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
102 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
103
104 printf("Tran Speed: %d\n", mmc->tran_speed);
105 printf("Rd Block Len: %d\n", mmc->read_bl_len);
106
107 printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
108 (mmc->version >> 4) & 0xf, mmc->version & 0xf);
109
110 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang940e0782011-01-04 01:04:19 +0000111 puts("Capacity: ");
112 print_size(mmc->capacity, "\n");
Andy Fleming272cc702008-10-30 16:41:01 -0500113
114 printf("Bus Width: %d-bit\n", mmc->bus_width);
115}
116
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200117int do_mmcinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500118{
119 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500120
Lei Wenea6ebe22011-05-02 16:26:25 +0000121 if (curr_device < 0) {
122 if (get_mmc_num() > 0)
123 curr_device = 0;
124 else {
125 puts("No MMC device available\n");
126 return 1;
127 }
128 }
Andy Fleming272cc702008-10-30 16:41:01 -0500129
Lei Wenea6ebe22011-05-02 16:26:25 +0000130 mmc = find_mmc_device(curr_device);
Andy Fleming272cc702008-10-30 16:41:01 -0500131
132 if (mmc) {
133 mmc_init(mmc);
134
135 print_mmcinfo(mmc);
Lei Wenea6ebe22011-05-02 16:26:25 +0000136 return 0;
137 } else {
138 printf("no mmc device at slot %x\n", curr_device);
139 return 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500140 }
Andy Fleming272cc702008-10-30 16:41:01 -0500141}
142
Frans Meulenbroeks388a29d2010-07-31 15:01:53 +0200143U_BOOT_CMD(
Lei Wenea6ebe22011-05-02 16:26:25 +0000144 mmcinfo, 1, 0, do_mmcinfo,
Frans Meulenbroekscc9f6072010-07-31 15:01:52 +0200145 "display MMC info",
Wolfgang Denk2d941de2010-09-10 00:16:19 +0200146 " - device number of the device to dislay info of\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200147 ""
148);
Andy Fleming272cc702008-10-30 16:41:01 -0500149
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200150int 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)
155 return cmd_usage(cmdtp);
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;
Lei Wenea6ebe22011-05-02 16:26:25 +0000175 mmc_init(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -0500176
Lei Wenea6ebe22011-05-02 16:26:25 +0000177 return 0;
178 } else if (strncmp(argv[1], "part", 4) == 0) {
179 block_dev_desc_t *mmc_dev;
180 struct mmc *mmc = find_mmc_device(curr_device);
181
182 if (!mmc) {
183 printf("no mmc device at slot %x\n", curr_device);
184 return 1;
185 }
186 mmc_init(mmc);
187 mmc_dev = mmc_get_dev(curr_device);
188 if (mmc_dev != NULL &&
189 mmc_dev->type != DEV_TYPE_UNKNOWN) {
190 print_part(mmc_dev);
Andy Fleming272cc702008-10-30 16:41:01 -0500191 return 0;
192 }
Lei Wenea6ebe22011-05-02 16:26:25 +0000193
194 puts("get mmc type error!\n");
Andy Fleming272cc702008-10-30 16:41:01 -0500195 return 1;
Lei Wenea6ebe22011-05-02 16:26:25 +0000196 } else if (strcmp(argv[1], "list") == 0) {
197 print_mmc_devices('\n');
198 return 0;
199 } else if (strcmp(argv[1], "dev") == 0) {
Lei Wenbc897b12011-05-02 16:26:26 +0000200 int dev, part = -1;
Lei Wenea6ebe22011-05-02 16:26:25 +0000201 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500202
Lei Wenea6ebe22011-05-02 16:26:25 +0000203 if (argc == 2)
204 dev = curr_device;
205 else if (argc == 3)
206 dev = simple_strtoul(argv[2], NULL, 10);
Lei Wenbc897b12011-05-02 16:26:26 +0000207 else if (argc == 4) {
208 dev = (int)simple_strtoul(argv[2], NULL, 10);
209 part = (int)simple_strtoul(argv[3], NULL, 10);
210 if (part > PART_ACCESS_MASK) {
211 printf("#part_num shouldn't be larger"
212 " than %d\n", PART_ACCESS_MASK);
213 return 1;
214 }
215 } else
Lei Wenea6ebe22011-05-02 16:26:25 +0000216 return cmd_usage(cmdtp);
Rabin Vincente85649c2009-04-05 13:30:53 +0530217
Lei Wenea6ebe22011-05-02 16:26:25 +0000218 mmc = find_mmc_device(dev);
219 if (!mmc) {
220 printf("no mmc device at slot %x\n", dev);
221 return 1;
222 }
Andy Fleming272cc702008-10-30 16:41:01 -0500223
Lei Wenbc897b12011-05-02 16:26:26 +0000224 mmc_init(mmc);
225 if (part != -1) {
226 int ret;
227 if (mmc->part_config == MMCPART_NOAVAILABLE) {
228 printf("Card doesn't support part_switch\n");
229 return 1;
230 }
231
232 if (part != mmc->part_num) {
233 ret = mmc_switch_part(dev, part);
234 if (!ret)
235 mmc->part_num = part;
236
237 printf("switch to partions #%d, %s\n",
238 part, (!ret) ? "OK" : "ERROR");
239 }
240 }
Lei Wenea6ebe22011-05-02 16:26:25 +0000241 curr_device = dev;
Lei Wenbc897b12011-05-02 16:26:26 +0000242 if (mmc->part_config == MMCPART_NOAVAILABLE)
243 printf("mmc%d is current device\n", curr_device);
244 else
245 printf("mmc%d(part %d) is current device\n",
246 curr_device, mmc->part_num);
Andy Fleming272cc702008-10-30 16:41:01 -0500247
Lei Wenea6ebe22011-05-02 16:26:25 +0000248 return 0;
Lei Wen6be95cc2011-06-22 17:03:30 +0000249 }
250
251 if (strcmp(argv[1], "read") == 0)
252 state = MMC_READ;
253 else if (strcmp(argv[1], "write") == 0)
254 state = MMC_WRITE;
255 else
256 state = MMC_INVALID;
257
258 if (state != MMC_INVALID) {
259 struct mmc *mmc = find_mmc_device(curr_device);
Lei Wenea6ebe22011-05-02 16:26:25 +0000260 void *addr = (void *)simple_strtoul(argv[2], NULL, 16);
Lei Wenea6ebe22011-05-02 16:26:25 +0000261 u32 blk = simple_strtoul(argv[3], NULL, 16);
Lei Wenea6ebe22011-05-02 16:26:25 +0000262 u32 cnt = simple_strtoul(argv[4], NULL, 16);
263 u32 n;
Andy Fleming272cc702008-10-30 16:41:01 -0500264
Lei Wenea6ebe22011-05-02 16:26:25 +0000265 if (!mmc) {
266 printf("no mmc device at slot %x\n", curr_device);
267 return 1;
268 }
Rabin Vincente85649c2009-04-05 13:30:53 +0530269
Lei Wen6be95cc2011-06-22 17:03:30 +0000270 printf("\nMMC %s: dev # %d, block # %d, count %d ... ",
271 argv[1], curr_device, blk, cnt);
Andy Fleming272cc702008-10-30 16:41:01 -0500272
Lei Wenea6ebe22011-05-02 16:26:25 +0000273 mmc_init(mmc);
Andy Fleming272cc702008-10-30 16:41:01 -0500274
Lei Wen6be95cc2011-06-22 17:03:30 +0000275 switch (state) {
276 case MMC_READ:
277 n = mmc->block_dev.block_read(curr_device, blk,
278 cnt, addr);
279 /* flush cache after read */
280 flush_cache((ulong)addr, cnt * 512); /* FIXME */
281 break;
282 case MMC_WRITE:
283 n = mmc->block_dev.block_write(curr_device, blk,
284 cnt, addr);
285 break;
286 default:
287 BUG();
288 }
Andy Fleming272cc702008-10-30 16:41:01 -0500289
Lei Wen6be95cc2011-06-22 17:03:30 +0000290 printf("%d blocks %s: %s\n",
291 n, argv[1], (n == cnt) ? "OK" : "ERROR");
Lei Wenea6ebe22011-05-02 16:26:25 +0000292 return (n == cnt) ? 0 : 1;
Andy Fleming272cc702008-10-30 16:41:01 -0500293 }
Lei Wenea6ebe22011-05-02 16:26:25 +0000294
295 return cmd_usage(cmdtp);
Andy Fleming272cc702008-10-30 16:41:01 -0500296}
297
298U_BOOT_CMD(
299 mmc, 6, 1, do_mmcops,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400300 "MMC sub system",
Lei Wenea6ebe22011-05-02 16:26:25 +0000301 "read addr blk# cnt\n"
302 "mmc write addr blk# cnt\n"
303 "mmc rescan\n"
304 "mmc part - lists available partition on current mmc device\n"
Lei Wenbc897b12011-05-02 16:26:26 +0000305 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200306 "mmc list - lists available devices");
Dirk Behme3511b4e2009-02-18 19:59:39 +0100307#endif