]>
Commit | Line | Data |
---|---|---|
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 | 28 | static int curr_device = -1; |
ea6ebe21 | 29 | #ifndef CONFIG_GENERIC_MMC |
54841ab5 | 30 | int 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 | 82 | U_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 |
90 | enum mmc_state { |
91 | MMC_INVALID, | |
92 | MMC_READ, | |
93 | MMC_WRITE, | |
e6f99a56 | 94 | MMC_ERASE, |
6be95ccf | 95 | }; |
272cc70b AF |
96 | static 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", | |
64f4a619 | 109 | (mmc->version >> 8) & 0xf, mmc->version & 0xff); |
272cc70b AF |
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 | 118 | static 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 | 144 | U_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 | |
2a91c913 A |
150 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
151 | static int boot_part_access(struct mmc *mmc, u8 ack, u8 part_num, u8 access) | |
152 | { | |
153 | int err; | |
154 | err = mmc_boot_part_access(mmc, ack, part_num, access); | |
155 | ||
156 | if ((err == 0) && (access != 0)) { | |
157 | printf("\t\t\t!!!Notice!!!\n"); | |
158 | ||
159 | printf("!You must close EMMC boot Partition"); | |
160 | printf("after all images are written\n"); | |
161 | ||
162 | printf("!EMMC boot partition has continuity"); | |
163 | printf("at image writing time.\n"); | |
164 | ||
165 | printf("!So, do not close the boot partition"); | |
166 | printf("before all images are written.\n"); | |
167 | return 0; | |
168 | } else if ((err == 0) && (access == 0)) | |
169 | return 0; | |
170 | else if ((err != 0) && (access != 0)) { | |
171 | printf("EMMC boot partition-%d OPEN Failed.\n", part_num); | |
172 | return 1; | |
173 | } else { | |
174 | printf("EMMC boot partition-%d CLOSE Failed.\n", part_num); | |
175 | return 1; | |
176 | } | |
177 | } | |
178 | #endif | |
179 | ||
088f1b19 | 180 | static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
272cc70b | 181 | { |
6be95ccf LW |
182 | enum mmc_state state; |
183 | ||
ea6ebe21 | 184 | if (argc < 2) |
4c12eeb8 | 185 | return CMD_RET_USAGE; |
272cc70b | 186 | |
ea6ebe21 LW |
187 | if (curr_device < 0) { |
188 | if (get_mmc_num() > 0) | |
189 | curr_device = 0; | |
190 | else { | |
191 | puts("No MMC device available\n"); | |
192 | return 1; | |
193 | } | |
194 | } | |
272cc70b | 195 | |
ea6ebe21 | 196 | if (strcmp(argv[1], "rescan") == 0) { |
9fd38372 SW |
197 | struct mmc *mmc; |
198 | ||
199 | if (argc != 2) | |
200 | return CMD_RET_USAGE; | |
e85649c7 | 201 | |
9fd38372 | 202 | mmc = find_mmc_device(curr_device); |
ea6ebe21 LW |
203 | if (!mmc) { |
204 | printf("no mmc device at slot %x\n", curr_device); | |
205 | return 1; | |
206 | } | |
207 | ||
bc897b1d | 208 | mmc->has_init = 0; |
272cc70b | 209 | |
8fd01b8f MJ |
210 | if (mmc_init(mmc)) |
211 | return 1; | |
212 | else | |
213 | return 0; | |
ea6ebe21 LW |
214 | } else if (strncmp(argv[1], "part", 4) == 0) { |
215 | block_dev_desc_t *mmc_dev; | |
9fd38372 | 216 | struct mmc *mmc; |
ea6ebe21 | 217 | |
9fd38372 SW |
218 | if (argc != 2) |
219 | return CMD_RET_USAGE; | |
220 | ||
221 | mmc = find_mmc_device(curr_device); | |
ea6ebe21 LW |
222 | if (!mmc) { |
223 | printf("no mmc device at slot %x\n", curr_device); | |
224 | return 1; | |
225 | } | |
226 | mmc_init(mmc); | |
227 | mmc_dev = mmc_get_dev(curr_device); | |
228 | if (mmc_dev != NULL && | |
229 | mmc_dev->type != DEV_TYPE_UNKNOWN) { | |
230 | print_part(mmc_dev); | |
272cc70b | 231 | return 0; |
ea6ebe21 | 232 | } |
8f3b9642 | 233 | |
ea6ebe21 LW |
234 | puts("get mmc type error!\n"); |
235 | return 1; | |
236 | } else if (strcmp(argv[1], "list") == 0) { | |
9fd38372 SW |
237 | if (argc != 2) |
238 | return CMD_RET_USAGE; | |
ea6ebe21 LW |
239 | print_mmc_devices('\n'); |
240 | return 0; | |
241 | } else if (strcmp(argv[1], "dev") == 0) { | |
bc897b1d | 242 | int dev, part = -1; |
ea6ebe21 LW |
243 | struct mmc *mmc; |
244 | ||
245 | if (argc == 2) | |
246 | dev = curr_device; | |
247 | else if (argc == 3) | |
248 | dev = simple_strtoul(argv[2], NULL, 10); | |
bc897b1d LW |
249 | else if (argc == 4) { |
250 | dev = (int)simple_strtoul(argv[2], NULL, 10); | |
251 | part = (int)simple_strtoul(argv[3], NULL, 10); | |
252 | if (part > PART_ACCESS_MASK) { | |
253 | printf("#part_num shouldn't be larger" | |
254 | " than %d\n", PART_ACCESS_MASK); | |
255 | return 1; | |
256 | } | |
257 | } else | |
4c12eeb8 | 258 | return CMD_RET_USAGE; |
8f3b9642 | 259 | |
ea6ebe21 LW |
260 | mmc = find_mmc_device(dev); |
261 | if (!mmc) { | |
262 | printf("no mmc device at slot %x\n", dev); | |
8f3b9642 | 263 | return 1; |
272cc70b AF |
264 | } |
265 | ||
bc897b1d LW |
266 | mmc_init(mmc); |
267 | if (part != -1) { | |
268 | int ret; | |
269 | if (mmc->part_config == MMCPART_NOAVAILABLE) { | |
270 | printf("Card doesn't support part_switch\n"); | |
271 | return 1; | |
272 | } | |
273 | ||
274 | if (part != mmc->part_num) { | |
275 | ret = mmc_switch_part(dev, part); | |
276 | if (!ret) | |
277 | mmc->part_num = part; | |
278 | ||
279 | printf("switch to partions #%d, %s\n", | |
280 | part, (!ret) ? "OK" : "ERROR"); | |
281 | } | |
282 | } | |
ea6ebe21 | 283 | curr_device = dev; |
bc897b1d LW |
284 | if (mmc->part_config == MMCPART_NOAVAILABLE) |
285 | printf("mmc%d is current device\n", curr_device); | |
286 | else | |
287 | printf("mmc%d(part %d) is current device\n", | |
288 | curr_device, mmc->part_num); | |
272cc70b | 289 | |
ea6ebe21 | 290 | return 0; |
2a91c913 A |
291 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
292 | } else if ((strcmp(argv[1], "open") == 0) || | |
293 | (strcmp(argv[1], "close") == 0)) { | |
294 | int dev; | |
295 | struct mmc *mmc; | |
296 | u8 part_num, access = 0; | |
297 | ||
298 | if (argc == 4) { | |
299 | dev = simple_strtoul(argv[2], NULL, 10); | |
300 | part_num = simple_strtoul(argv[3], NULL, 10); | |
301 | } else { | |
302 | return CMD_RET_USAGE; | |
303 | } | |
304 | ||
305 | mmc = find_mmc_device(dev); | |
306 | if (!mmc) { | |
307 | printf("no mmc device at slot %x\n", dev); | |
308 | return 1; | |
309 | } | |
272cc70b | 310 | |
2a91c913 A |
311 | if (IS_SD(mmc)) { |
312 | printf("SD device cannot be opened/closed\n"); | |
313 | return 1; | |
314 | } | |
315 | ||
316 | if ((part_num <= 0) || (part_num > MMC_NUM_BOOT_PARTITION)) { | |
317 | printf("Invalid boot partition number:\n"); | |
318 | printf("Boot partition number cannot be <= 0\n"); | |
319 | printf("EMMC44 supports only 2 boot partitions\n"); | |
320 | return 1; | |
321 | } | |
322 | ||
323 | if (strcmp(argv[1], "open") == 0) | |
324 | access = part_num; /* enable R/W access to boot part*/ | |
325 | else | |
326 | access = 0; /* No access to boot partition */ | |
327 | ||
328 | /* acknowledge to be sent during boot operation */ | |
329 | return boot_part_access(mmc, 1, part_num, access); | |
330 | ||
331 | } else if (strcmp(argv[1], "bootpart") == 0) { | |
332 | int dev; | |
333 | dev = simple_strtoul(argv[2], NULL, 10); | |
334 | ||
335 | u32 bootsize = simple_strtoul(argv[3], NULL, 10); | |
336 | u32 rpmbsize = simple_strtoul(argv[4], NULL, 10); | |
337 | struct mmc *mmc = find_mmc_device(dev); | |
338 | if (!mmc) { | |
339 | printf("no mmc device at slot %x\n", dev); | |
340 | return 1; | |
341 | } | |
342 | ||
343 | if (IS_SD(mmc)) { | |
344 | printf("It is not a EMMC device\n"); | |
345 | return 1; | |
346 | } | |
347 | ||
348 | if (0 == mmc_boot_partition_size_change(mmc, | |
349 | bootsize, rpmbsize)) { | |
350 | printf("EMMC boot partition Size %d MB\n", bootsize); | |
351 | printf("EMMC RPMB partition Size %d MB\n", rpmbsize); | |
352 | return 0; | |
353 | } else { | |
354 | printf("EMMC boot partition Size change Failed.\n"); | |
355 | return 1; | |
356 | } | |
357 | #endif /* CONFIG_SUPPORT_EMMC_BOOT */ | |
358 | } | |
ed80c931 TH |
359 | state = MMC_INVALID; |
360 | if (argc == 5 && strcmp(argv[1], "read") == 0) | |
6be95ccf | 361 | state = MMC_READ; |
ed80c931 | 362 | else if (argc == 5 && strcmp(argv[1], "write") == 0) |
6be95ccf | 363 | state = MMC_WRITE; |
ed80c931 | 364 | else if (argc == 4 && strcmp(argv[1], "erase") == 0) |
e6f99a56 | 365 | state = MMC_ERASE; |
272cc70b | 366 | |
6be95ccf LW |
367 | if (state != MMC_INVALID) { |
368 | struct mmc *mmc = find_mmc_device(curr_device); | |
e6f99a56 LW |
369 | int idx = 2; |
370 | u32 blk, cnt, n; | |
371 | void *addr; | |
372 | ||
373 | if (state != MMC_ERASE) { | |
374 | addr = (void *)simple_strtoul(argv[idx], NULL, 16); | |
375 | ++idx; | |
376 | } else | |
088f1b19 | 377 | addr = NULL; |
e6f99a56 LW |
378 | blk = simple_strtoul(argv[idx], NULL, 16); |
379 | cnt = simple_strtoul(argv[idx + 1], NULL, 16); | |
272cc70b | 380 | |
ea6ebe21 LW |
381 | if (!mmc) { |
382 | printf("no mmc device at slot %x\n", curr_device); | |
383 | return 1; | |
384 | } | |
e85649c7 | 385 | |
6be95ccf LW |
386 | printf("\nMMC %s: dev # %d, block # %d, count %d ... ", |
387 | argv[1], curr_device, blk, cnt); | |
272cc70b | 388 | |
ea6ebe21 | 389 | mmc_init(mmc); |
272cc70b | 390 | |
d23d8d7e NK |
391 | if ((state == MMC_WRITE || state == MMC_ERASE)) { |
392 | if (mmc_getwp(mmc) == 1) { | |
393 | printf("Error: card is write protected!\n"); | |
394 | return 1; | |
395 | } | |
396 | } | |
397 | ||
6be95ccf LW |
398 | switch (state) { |
399 | case MMC_READ: | |
400 | n = mmc->block_dev.block_read(curr_device, blk, | |
401 | cnt, addr); | |
402 | /* flush cache after read */ | |
403 | flush_cache((ulong)addr, cnt * 512); /* FIXME */ | |
404 | break; | |
405 | case MMC_WRITE: | |
406 | n = mmc->block_dev.block_write(curr_device, blk, | |
407 | cnt, addr); | |
408 | break; | |
e6f99a56 LW |
409 | case MMC_ERASE: |
410 | n = mmc->block_dev.block_erase(curr_device, blk, cnt); | |
411 | break; | |
6be95ccf LW |
412 | default: |
413 | BUG(); | |
414 | } | |
272cc70b | 415 | |
6be95ccf LW |
416 | printf("%d blocks %s: %s\n", |
417 | n, argv[1], (n == cnt) ? "OK" : "ERROR"); | |
ea6ebe21 | 418 | return (n == cnt) ? 0 : 1; |
272cc70b | 419 | } |
ea6ebe21 | 420 | |
4c12eeb8 | 421 | return CMD_RET_USAGE; |
272cc70b AF |
422 | } |
423 | ||
424 | U_BOOT_CMD( | |
425 | mmc, 6, 1, do_mmcops, | |
852dbfdd | 426 | "MMC sub system", |
ea6ebe21 LW |
427 | "read addr blk# cnt\n" |
428 | "mmc write addr blk# cnt\n" | |
e6f99a56 | 429 | "mmc erase blk# cnt\n" |
ea6ebe21 LW |
430 | "mmc rescan\n" |
431 | "mmc part - lists available partition on current mmc device\n" | |
bc897b1d | 432 | "mmc dev [dev] [part] - show or set current mmc device [partition]\n" |
2a91c913 A |
433 | "mmc list - lists available devices\n" |
434 | #ifdef CONFIG_SUPPORT_EMMC_BOOT | |
435 | "mmc open <dev> <boot_partition>\n" | |
436 | " - Enable boot_part for booting and enable R/W access of boot_part\n" | |
437 | "mmc close <dev> <boot_partition>\n" | |
438 | " - Enable boot_part for booting and disable access to boot_part\n" | |
439 | "mmc bootpart <device num> <boot part size MB> <RPMB part size MB>\n" | |
440 | " - change sizes of boot and RPMB partions of specified device\n" | |
3511b4e2 | 441 | #endif |
2a91c913 A |
442 | ); |
443 | #endif /* !CONFIG_GENERIC_MMC */ |