]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_mmc.c
printenv: Correct out-of-memory condition check.
[people/ms/u-boot.git] / common / cmd_mmc.c
CommitLineData
71f95118
WD
1/*
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>
71f95118
WD
26#include <mmc.h>
27
02e22c2d 28static int curr_device = -1;
ea6ebe21 29#ifndef CONFIG_GENERIC_MMC
54841ab5 30int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
71f95118 31{
869f6bf4
MK
32 int dev;
33
47e26b1b 34 if (argc < 2)
4c12eeb8 35 return CMD_RET_USAGE;
869f6bf4
MK
36
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 {
4c12eeb8 46 return CMD_RET_USAGE;
869f6bf4
MK
47 }
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 {
4c12eeb8 71 return CMD_RET_USAGE;
869f6bf4
MK
72 }
73
74 printf("mmc%d is current device\n", curr_device);
75 } else {
4c12eeb8 76 return CMD_RET_USAGE;
71f95118 77 }
869f6bf4 78
71f95118
WD
79 return 0;
80}
81
0d498393 82U_BOOT_CMD(
869f6bf4
MK
83 mmc, 3, 1, do_mmc,
84 "MMC sub-system",
85 "init [dev] - init MMC sub system\n"
a89c33db 86 "mmc device [dev] - show or set current device"
b0fce99b 87);
3511b4e2 88#else /* !CONFIG_GENERIC_MMC */
272cc70b 89
6be95ccf
LW
90enum mmc_state {
91 MMC_INVALID,
92 MMC_READ,
93 MMC_WRITE,
e6f99a56 94 MMC_ERASE,
6be95ccf 95};
272cc70b
AF
96static 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");
940e0782
MK
112 puts("Capacity: ");
113 print_size(mmc->capacity, "\n");
272cc70b
AF
114
115 printf("Bus Width: %d-bit\n", mmc->bus_width);
116}
117
088f1b19 118static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
272cc70b
AF
119{
120 struct mmc *mmc;
272cc70b 121
ea6ebe21
LW
122 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 }
272cc70b 130
ea6ebe21 131 mmc = find_mmc_device(curr_device);
272cc70b
AF
132
133 if (mmc) {
134 mmc_init(mmc);
135
136 print_mmcinfo(mmc);
ea6ebe21
LW
137 return 0;
138 } else {
139 printf("no mmc device at slot %x\n", curr_device);
140 return 1;
272cc70b 141 }
272cc70b
AF
142}
143
388a29d0 144U_BOOT_CMD(
ea6ebe21 145 mmcinfo, 1, 0, do_mmcinfo,
cc9f607b 146 "display MMC info",
59ddead1 147 "- display info of the current MMC device"
a89c33db 148);
272cc70b 149
088f1b19 150static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
272cc70b 151{
6be95ccf
LW
152 enum mmc_state state;
153
ea6ebe21 154 if (argc < 2)
4c12eeb8 155 return CMD_RET_USAGE;
272cc70b 156
ea6ebe21
LW
157 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 }
272cc70b 165
ea6ebe21 166 if (strcmp(argv[1], "rescan") == 0) {
9fd38372
SW
167 struct mmc *mmc;
168
169 if (argc != 2)
170 return CMD_RET_USAGE;
e85649c7 171
9fd38372 172 mmc = find_mmc_device(curr_device);
ea6ebe21
LW
173 if (!mmc) {
174 printf("no mmc device at slot %x\n", curr_device);
175 return 1;
176 }
177
bc897b1d 178 mmc->has_init = 0;
272cc70b 179
8fd01b8f
MJ
180 if (mmc_init(mmc))
181 return 1;
182 else
183 return 0;
ea6ebe21
LW
184 } else if (strncmp(argv[1], "part", 4) == 0) {
185 block_dev_desc_t *mmc_dev;
9fd38372 186 struct mmc *mmc;
ea6ebe21 187
9fd38372
SW
188 if (argc != 2)
189 return CMD_RET_USAGE;
190
191 mmc = find_mmc_device(curr_device);
ea6ebe21
LW
192 if (!mmc) {
193 printf("no mmc device at slot %x\n", curr_device);
194 return 1;
195 }
196 mmc_init(mmc);
197 mmc_dev = mmc_get_dev(curr_device);
198 if (mmc_dev != NULL &&
199 mmc_dev->type != DEV_TYPE_UNKNOWN) {
200 print_part(mmc_dev);
272cc70b 201 return 0;
ea6ebe21 202 }
8f3b9642 203
ea6ebe21
LW
204 puts("get mmc type error!\n");
205 return 1;
206 } else if (strcmp(argv[1], "list") == 0) {
9fd38372
SW
207 if (argc != 2)
208 return CMD_RET_USAGE;
ea6ebe21
LW
209 print_mmc_devices('\n');
210 return 0;
211 } else if (strcmp(argv[1], "dev") == 0) {
bc897b1d 212 int dev, part = -1;
ea6ebe21
LW
213 struct mmc *mmc;
214
215 if (argc == 2)
216 dev = curr_device;
217 else if (argc == 3)
218 dev = simple_strtoul(argv[2], NULL, 10);
bc897b1d
LW
219 else if (argc == 4) {
220 dev = (int)simple_strtoul(argv[2], NULL, 10);
221 part = (int)simple_strtoul(argv[3], NULL, 10);
222 if (part > PART_ACCESS_MASK) {
223 printf("#part_num shouldn't be larger"
224 " than %d\n", PART_ACCESS_MASK);
225 return 1;
226 }
227 } else
4c12eeb8 228 return CMD_RET_USAGE;
8f3b9642 229
ea6ebe21
LW
230 mmc = find_mmc_device(dev);
231 if (!mmc) {
232 printf("no mmc device at slot %x\n", dev);
8f3b9642 233 return 1;
272cc70b
AF
234 }
235
bc897b1d
LW
236 mmc_init(mmc);
237 if (part != -1) {
238 int ret;
239 if (mmc->part_config == MMCPART_NOAVAILABLE) {
240 printf("Card doesn't support part_switch\n");
241 return 1;
242 }
243
244 if (part != mmc->part_num) {
245 ret = mmc_switch_part(dev, part);
246 if (!ret)
247 mmc->part_num = part;
248
249 printf("switch to partions #%d, %s\n",
250 part, (!ret) ? "OK" : "ERROR");
251 }
252 }
ea6ebe21 253 curr_device = dev;
bc897b1d
LW
254 if (mmc->part_config == MMCPART_NOAVAILABLE)
255 printf("mmc%d is current device\n", curr_device);
256 else
257 printf("mmc%d(part %d) is current device\n",
258 curr_device, mmc->part_num);
272cc70b 259
ea6ebe21 260 return 0;
6be95ccf 261 }
272cc70b 262
ed80c931
TH
263 state = MMC_INVALID;
264 if (argc == 5 && strcmp(argv[1], "read") == 0)
6be95ccf 265 state = MMC_READ;
ed80c931 266 else if (argc == 5 && strcmp(argv[1], "write") == 0)
6be95ccf 267 state = MMC_WRITE;
ed80c931 268 else if (argc == 4 && strcmp(argv[1], "erase") == 0)
e6f99a56 269 state = MMC_ERASE;
272cc70b 270
6be95ccf
LW
271 if (state != MMC_INVALID) {
272 struct mmc *mmc = find_mmc_device(curr_device);
e6f99a56
LW
273 int idx = 2;
274 u32 blk, cnt, n;
275 void *addr;
276
277 if (state != MMC_ERASE) {
278 addr = (void *)simple_strtoul(argv[idx], NULL, 16);
279 ++idx;
280 } else
088f1b19 281 addr = NULL;
e6f99a56
LW
282 blk = simple_strtoul(argv[idx], NULL, 16);
283 cnt = simple_strtoul(argv[idx + 1], NULL, 16);
272cc70b 284
ea6ebe21
LW
285 if (!mmc) {
286 printf("no mmc device at slot %x\n", curr_device);
287 return 1;
288 }
e85649c7 289
6be95ccf
LW
290 printf("\nMMC %s: dev # %d, block # %d, count %d ... ",
291 argv[1], curr_device, blk, cnt);
272cc70b 292
ea6ebe21 293 mmc_init(mmc);
272cc70b 294
d23d8d7e
NK
295 if ((state == MMC_WRITE || state == MMC_ERASE)) {
296 if (mmc_getwp(mmc) == 1) {
297 printf("Error: card is write protected!\n");
298 return 1;
299 }
300 }
301
6be95ccf
LW
302 switch (state) {
303 case MMC_READ:
304 n = mmc->block_dev.block_read(curr_device, blk,
305 cnt, addr);
306 /* flush cache after read */
307 flush_cache((ulong)addr, cnt * 512); /* FIXME */
308 break;
309 case MMC_WRITE:
310 n = mmc->block_dev.block_write(curr_device, blk,
311 cnt, addr);
312 break;
e6f99a56
LW
313 case MMC_ERASE:
314 n = mmc->block_dev.block_erase(curr_device, blk, cnt);
315 break;
6be95ccf
LW
316 default:
317 BUG();
318 }
272cc70b 319
6be95ccf
LW
320 printf("%d blocks %s: %s\n",
321 n, argv[1], (n == cnt) ? "OK" : "ERROR");
ea6ebe21 322 return (n == cnt) ? 0 : 1;
272cc70b 323 }
ea6ebe21 324
4c12eeb8 325 return CMD_RET_USAGE;
272cc70b
AF
326}
327
328U_BOOT_CMD(
329 mmc, 6, 1, do_mmcops,
852dbfdd 330 "MMC sub system",
ea6ebe21
LW
331 "read addr blk# cnt\n"
332 "mmc write addr blk# cnt\n"
e6f99a56 333 "mmc erase blk# cnt\n"
ea6ebe21
LW
334 "mmc rescan\n"
335 "mmc part - lists available partition on current mmc device\n"
bc897b1d 336 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
a89c33db 337 "mmc list - lists available devices");
3511b4e2 338#endif