Merge branch 'master' of git://www.denx.de/git/u-boot-mpc85xx
[kernel/u-boot.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 static int curr_device = -1;
29 #ifndef CONFIG_GENERIC_MMC
30 int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
31 {
32         int dev;
33
34         if (argc < 2)
35                 return CMD_RET_USAGE;
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 {
46                         return CMD_RET_USAGE;
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 {
71                         return CMD_RET_USAGE;
72                 }
73
74                 printf("mmc%d is current device\n", curr_device);
75         } else {
76                 return CMD_RET_USAGE;
77         }
78
79         return 0;
80 }
81
82 U_BOOT_CMD(
83         mmc, 3, 1, do_mmc,
84         "MMC sub-system",
85         "init [dev] - init MMC sub system\n"
86         "mmc device [dev] - show or set current device"
87 );
88 #else /* !CONFIG_GENERIC_MMC */
89
90 enum mmc_state {
91         MMC_INVALID,
92         MMC_READ,
93         MMC_WRITE,
94         MMC_ERASE,
95 };
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",
109                         (mmc->version >> 4) & 0xf, mmc->version & 0xf);
110
111         printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
112         puts("Capacity: ");
113         print_size(mmc->capacity, "\n");
114
115         printf("Bus Width: %d-bit\n", mmc->bus_width);
116 }
117
118 static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
119 {
120         struct mmc *mmc;
121
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         }
130
131         mmc = find_mmc_device(curr_device);
132
133         if (mmc) {
134                 mmc_init(mmc);
135
136                 print_mmcinfo(mmc);
137                 return 0;
138         } else {
139                 printf("no mmc device at slot %x\n", curr_device);
140                 return 1;
141         }
142 }
143
144 U_BOOT_CMD(
145         mmcinfo, 1, 0, do_mmcinfo,
146         "display MMC info",
147         "- display info of the current MMC device"
148 );
149
150 static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
151 {
152         enum mmc_state state;
153
154         if (argc < 2)
155                 return CMD_RET_USAGE;
156
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         }
165
166         if (strcmp(argv[1], "rescan") == 0) {
167                 struct mmc *mmc = find_mmc_device(curr_device);
168
169                 if (!mmc) {
170                         printf("no mmc device at slot %x\n", curr_device);
171                         return 1;
172                 }
173
174                 mmc->has_init = 0;
175
176                 if (mmc_init(mmc))
177                         return 1;
178                 else
179                         return 0;
180         } else if (strncmp(argv[1], "part", 4) == 0) {
181                 block_dev_desc_t *mmc_dev;
182                 struct mmc *mmc = find_mmc_device(curr_device);
183
184                 if (!mmc) {
185                         printf("no mmc device at slot %x\n", curr_device);
186                         return 1;
187                 }
188                 mmc_init(mmc);
189                 mmc_dev = mmc_get_dev(curr_device);
190                 if (mmc_dev != NULL &&
191                                 mmc_dev->type != DEV_TYPE_UNKNOWN) {
192                         print_part(mmc_dev);
193                         return 0;
194                 }
195
196                 puts("get mmc type error!\n");
197                 return 1;
198         } else if (strcmp(argv[1], "list") == 0) {
199                 print_mmc_devices('\n');
200                 return 0;
201         } else if (strcmp(argv[1], "dev") == 0) {
202                 int dev, part = -1;
203                 struct mmc *mmc;
204
205                 if (argc == 2)
206                         dev = curr_device;
207                 else if (argc == 3)
208                         dev = simple_strtoul(argv[2], NULL, 10);
209                 else if (argc == 4) {
210                         dev = (int)simple_strtoul(argv[2], NULL, 10);
211                         part = (int)simple_strtoul(argv[3], NULL, 10);
212                         if (part > PART_ACCESS_MASK) {
213                                 printf("#part_num shouldn't be larger"
214                                         " than %d\n", PART_ACCESS_MASK);
215                                 return 1;
216                         }
217                 } else
218                         return CMD_RET_USAGE;
219
220                 mmc = find_mmc_device(dev);
221                 if (!mmc) {
222                         printf("no mmc device at slot %x\n", dev);
223                         return 1;
224                 }
225
226                 mmc_init(mmc);
227                 if (part != -1) {
228                         int ret;
229                         if (mmc->part_config == MMCPART_NOAVAILABLE) {
230                                 printf("Card doesn't support part_switch\n");
231                                 return 1;
232                         }
233
234                         if (part != mmc->part_num) {
235                                 ret = mmc_switch_part(dev, part);
236                                 if (!ret)
237                                         mmc->part_num = part;
238
239                                 printf("switch to partions #%d, %s\n",
240                                                 part, (!ret) ? "OK" : "ERROR");
241                         }
242                 }
243                 curr_device = dev;
244                 if (mmc->part_config == MMCPART_NOAVAILABLE)
245                         printf("mmc%d is current device\n", curr_device);
246                 else
247                         printf("mmc%d(part %d) is current device\n",
248                                 curr_device, mmc->part_num);
249
250                 return 0;
251         }
252
253         state = MMC_INVALID;
254         if (argc == 5 && strcmp(argv[1], "read") == 0)
255                 state = MMC_READ;
256         else if (argc == 5 && strcmp(argv[1], "write") == 0)
257                 state = MMC_WRITE;
258         else if (argc == 4 && strcmp(argv[1], "erase") == 0)
259                 state = MMC_ERASE;
260
261         if (state != MMC_INVALID) {
262                 struct mmc *mmc = find_mmc_device(curr_device);
263                 int idx = 2;
264                 u32 blk, cnt, n;
265                 void *addr;
266
267                 if (state != MMC_ERASE) {
268                         addr = (void *)simple_strtoul(argv[idx], NULL, 16);
269                         ++idx;
270                 } else
271                         addr = NULL;
272                 blk = simple_strtoul(argv[idx], NULL, 16);
273                 cnt = simple_strtoul(argv[idx + 1], NULL, 16);
274
275                 if (!mmc) {
276                         printf("no mmc device at slot %x\n", curr_device);
277                         return 1;
278                 }
279
280                 printf("\nMMC %s: dev # %d, block # %d, count %d ... ",
281                                 argv[1], curr_device, blk, cnt);
282
283                 mmc_init(mmc);
284
285                 switch (state) {
286                 case MMC_READ:
287                         n = mmc->block_dev.block_read(curr_device, blk,
288                                                       cnt, addr);
289                         /* flush cache after read */
290                         flush_cache((ulong)addr, cnt * 512); /* FIXME */
291                         break;
292                 case MMC_WRITE:
293                         n = mmc->block_dev.block_write(curr_device, blk,
294                                                       cnt, addr);
295                         break;
296                 case MMC_ERASE:
297                         n = mmc->block_dev.block_erase(curr_device, blk, cnt);
298                         break;
299                 default:
300                         BUG();
301                 }
302
303                 printf("%d blocks %s: %s\n",
304                                 n, argv[1], (n == cnt) ? "OK" : "ERROR");
305                 return (n == cnt) ? 0 : 1;
306         }
307
308         return CMD_RET_USAGE;
309 }
310
311 U_BOOT_CMD(
312         mmc, 6, 1, do_mmcops,
313         "MMC sub system",
314         "read addr blk# cnt\n"
315         "mmc write addr blk# cnt\n"
316         "mmc erase blk# cnt\n"
317         "mmc rescan\n"
318         "mmc part - lists available partition on current mmc device\n"
319         "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
320         "mmc list - lists available devices");
321 #endif