]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_mmc.c
applied patches from Freescale and Ka-Ro
[karo-tx-uboot.git] / common / cmd_mmc.c
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>
26 #include <mmc.h>
27
28 #ifndef CONFIG_GENERIC_MMC
29 static int curr_device = -1;
30
31 int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
32 {
33         int dev;
34
35         if (argc < 2) {
36                 cmd_usage(cmdtp);
37                 return 1;
38         }
39
40         if (strcmp(argv[1], "init") == 0) {
41                 if (argc == 2) {
42                         if (curr_device < 0)
43                                 dev = 1;
44                         else
45                                 dev = curr_device;
46                 } else if (argc == 3) {
47                         dev = (int)simple_strtoul(argv[2], NULL, 10);
48                 } else {
49                         cmd_usage(cmdtp);
50                         return 1;
51                 }
52
53                 if (mmc_legacy_init(dev) != 0) {
54                         puts("No MMC card found\n");
55                         return 1;
56                 }
57
58                 curr_device = dev;
59                 printf("mmc%d is available\n", curr_device);
60         } else if (strcmp(argv[1], "device") == 0) {
61                 if (argc == 2) {
62                         if (curr_device < 0) {
63                                 puts("No MMC device available\n");
64                                 return 1;
65                         }
66                 } else if (argc == 3) {
67                         dev = (int)simple_strtoul(argv[2], NULL, 10);
68
69 #ifdef CONFIG_SYS_MMC_SET_DEV
70                         if (mmc_set_dev(dev) != 0)
71                                 return 1;
72 #endif
73                         curr_device = dev;
74                 } else {
75                         cmd_usage(cmdtp);
76                         return 1;
77                 }
78
79                 printf("mmc%d is current device\n", curr_device);
80         } else {
81                 cmd_usage(cmdtp);
82                 return 1;
83         }
84
85         return 0;
86 }
87
88 U_BOOT_CMD(
89         mmc, 3, 1, do_mmc,
90         "MMC sub-system",
91         "init [dev] - init MMC sub system\n"
92         "mmc device [dev] - show or set current device"
93 );
94 #else /* !CONFIG_GENERIC_MMC */
95
96 #ifdef CONFIG_BOOT_PARTITION_ACCESS
97 #define MMC_PARTITION_SWITCH(mmc, part, enable_boot) \
98         do { \
99                 if (IS_SD(mmc)) {       \
100                         if (part > 1)   {\
101                                 printf( \
102                                 "\nError: SD partition can only be 0 or 1\n");\
103                                 return 1;       \
104                         }       \
105                         if (sd_switch_partition(mmc, part) < 0) {       \
106                                 if (part > 0) { \
107                                         printf("\nError: Unable to switch SD "\
108                                         "partition\n");\
109                                         return 1;       \
110                                 }       \
111                         }       \
112                 } else {        \
113                         if (mmc_switch_partition(mmc, part, enable_boot) \
114                                 < 0) {  \
115                                 printf("Error: Fail to switch " \
116                                         "partition to %d\n", part);     \
117                                 return 1;       \
118                         }       \
119                 } \
120         } while (0)
121 #endif
122
123 static void print_mmcinfo(struct mmc *mmc)
124 {
125         printf("Device: %s\n", mmc->name);
126         printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
127         printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
128         printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
129                         (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
130                         (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
131
132         printf("Tran Speed: %d\n", mmc->tran_speed);
133         printf("Rd Block Len: %d\n", mmc->read_bl_len);
134
135         printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
136                         (mmc->version >> 4) & 0xf, mmc->version & 0xf);
137
138         printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
139         printf("Capacity: %lld\n", mmc->capacity);
140
141 #ifdef CONFIG_EMMC_DDR_MODE
142         if (mmc->bus_width == EMMC_MODE_4BIT_DDR ||
143                 mmc->bus_width == EMMC_MODE_8BIT_DDR)
144                 printf("Bus Width: %d-bit DDR\n", (mmc->bus_width >> 8));
145         else
146 #endif
147         printf("Bus Width: %d-bit\n", mmc->bus_width);
148 #ifdef CONFIG_BOOT_PARTITION_ACCESS
149         if (mmc->boot_size_mult == 0) {
150                 printf("Boot Partition Size: %s\n", "No boot partition available");
151         } else {
152                 printf("Boot Partition Size: %5dKB\n", mmc->boot_size_mult * 128);
153
154                 printf("Current Partition for boot: ");
155                 switch (mmc->boot_config & EXT_CSD_BOOT_PARTITION_ENABLE_MASK) {
156                 case EXT_CSD_BOOT_PARTITION_DISABLE:
157                         printf("Not bootable\n");
158                         break;
159                 case EXT_CSD_BOOT_PARTITION_PART1:
160                         printf("Boot partition 1\n");
161                         break;
162                 case EXT_CSD_BOOT_PARTITION_PART2:
163                         printf("Boot partition 2\n");
164                         break;
165                 case EXT_CSD_BOOT_PARTITION_USER:
166                         printf("User area\n");
167                         break;
168                 default:
169                         printf("Unknown\n");
170                         break;
171                 }
172         }
173 #endif
174 }
175
176 int do_mmcinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
177 {
178         struct mmc *mmc;
179         int dev_num;
180
181         if (argc < 2)
182                 dev_num = 0;
183         else
184                 dev_num = simple_strtoul(argv[1], NULL, 0);
185
186         mmc = find_mmc_device(dev_num);
187
188         if (mmc) {
189                 if (mmc_init(mmc))
190                         puts("MMC card init failed!\n");
191                 else
192                         print_mmcinfo(mmc);
193         }
194
195         return 0;
196 }
197
198 U_BOOT_CMD(mmcinfo, 2, 0, do_mmcinfo,
199         "mmcinfo <dev num>-- display MMC info",
200         ""
201 );
202
203 int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
204 {
205         int rc = 0;
206 #ifdef CONFIG_BOOT_PARTITION_ACCESS
207         u32 part = 0;
208 #endif
209
210         switch (argc) {
211         case 3:
212                 if (strcmp(argv[1], "rescan") == 0) {
213                         int dev = simple_strtoul(argv[2], NULL, 10);
214                         struct mmc *mmc = find_mmc_device(dev);
215
216                         if (!mmc)
217                                 return 1;
218
219                         mmc_init(mmc);
220
221                         return 0;
222                 }
223
224         case 0:
225         case 1:
226         case 4:
227                 printf("Usage:\n%s\n", cmdtp->usage);
228                 return 1;
229
230         case 2:
231                 if (!strcmp(argv[1], "list")) {
232                         print_mmc_devices('\n');
233                         return 0;
234                 }
235                 return 1;
236 #ifdef CONFIG_BOOT_PARTITION_ACCESS
237         case 7: /* Fall through */
238                 part = simple_strtoul(argv[6], NULL, 10);
239 #endif
240         default: /* at least 5 args */
241                 if (strcmp(argv[1], "read") == 0) {
242                         int dev = simple_strtoul(argv[2], NULL, 10);
243                         void *addr = (void *)simple_strtoul(argv[3], NULL, 16);
244                         u32 cnt = simple_strtoul(argv[5], NULL, 16);
245                         u32 n;
246                         u32 blk = simple_strtoul(argv[4], NULL, 16);
247
248                         struct mmc *mmc = find_mmc_device(dev);
249
250                         if (!mmc)
251                                 return 1;
252
253 #ifdef CONFIG_BOOT_PARTITION_ACCESS
254                         printf("\nMMC read: dev # %d, block # %d, "
255                                 "count %d partition # %d ... \n",
256                                 dev, blk, cnt, part);
257 #else
258                         printf("\nMMC read: dev # %d, block # %d,"
259                                 "count %d ... \n", dev, blk, cnt);
260 #endif
261
262                         mmc_init(mmc);
263
264 #ifdef CONFIG_BOOT_PARTITION_ACCESS
265                         if (((mmc->boot_config &
266                                 EXT_CSD_BOOT_PARTITION_ACCESS_MASK) != part)
267                                 || IS_SD(mmc)) {
268                                 /*
269                                  * After mmc_init, we now know whether
270                                  * this is a eSD/eMMC which support boot
271                                  * partition
272                                  */
273                                 MMC_PARTITION_SWITCH(mmc, part, 0);
274                         }
275 #endif
276
277                         n = mmc->block_dev.block_read(dev, blk, cnt, addr);
278
279                         /* flush cache after read */
280                         flush_cache((ulong)addr, cnt * 512); /* FIXME */
281
282                         printf("%d blocks read: %s\n",
283                                 n, (n==cnt) ? "OK" : "ERROR");
284                         return (n == cnt) ? 0 : 1;
285                 } else if (strcmp(argv[1], "write") == 0) {
286                         int dev = simple_strtoul(argv[2], NULL, 10);
287                         void *addr = (void *)simple_strtoul(argv[3], NULL, 16);
288                         u32 cnt = simple_strtoul(argv[5], NULL, 16);
289                         u32 n;
290
291                         struct mmc *mmc = find_mmc_device(dev);
292
293                         int blk = simple_strtoul(argv[4], NULL, 16);
294
295                         if (!mmc)
296                                 return 1;
297
298 #ifdef CONFIG_BOOT_PARTITION_ACCESS
299                         printf("\nMMC write: dev # %d, block # %d, "
300                                 "count %d, partition # %d ... \n",
301                                 dev, blk, cnt, part);
302 #else
303                         printf("\nMMC write: dev # %d, block # %d, "
304                                 "count %d ... \n",
305                                 dev, blk, cnt);
306 #endif
307
308                         mmc_init(mmc);
309
310 #ifdef CONFIG_BOOT_PARTITION_ACCESS
311                         if (((mmc->boot_config &
312                                 EXT_CSD_BOOT_PARTITION_ACCESS_MASK) != part)
313                                 || IS_SD(mmc)) {
314                                 /*
315                                  * After mmc_init, we now know whether this is a
316                                  * eSD/eMMC which support boot partition
317                                  */
318                                 MMC_PARTITION_SWITCH(mmc, part, 1);
319                         }
320 #endif
321
322                         n = mmc->block_dev.block_write(dev, blk, cnt, addr);
323
324                         printf("%d blocks written: %s\n",
325                                 n, (n == cnt) ? "OK" : "ERROR");
326                         return (n == cnt) ? 0 : 1;
327                 } else {
328                         printf("Usage:\n%s\n", cmdtp->usage);
329                         rc = 1;
330                 }
331
332                 return rc;
333         }
334 }
335
336 #ifndef CONFIG_BOOT_PARTITION_ACCESS
337 U_BOOT_CMD(
338         mmc, 6, 1, do_mmcops,
339         "MMC sub system",
340         "mmc read <device num> addr blk# cnt\n"
341         "mmc write <device num> addr blk# cnt\n"
342         "mmc rescan <device num>\n"
343         "mmc list - lists available devices");
344 #else
345 U_BOOT_CMD(
346         mmc, 7, 1, do_mmcops,
347         "MMC sub system",
348         "mmc read <device num> addr blk# cnt [partition]\n"
349         "mmc write <device num> addr blk# cnt [partition]\n"
350         "mmc rescan <device num>\n"
351         "mmc list - lists available devices");
352 #endif
353 #endif
354