]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_mmc.c
e266d4fa43ffc047134b58ce56ec07ea87ff65db
[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 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_usage(cmdtp);
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_usage(cmdtp);
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_usage(cmdtp);
72                 }
73
74                 printf("mmc%d is current device\n", curr_device);
75         } else {
76                 return cmd_usage(cmdtp);
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 static void print_mmcinfo(struct mmc *mmc)
91 {
92         printf("Device: %s\n", mmc->name);
93         printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
94         printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
95         printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
96                         (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
97                         (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
98
99         printf("Tran Speed: %d\n", mmc->tran_speed);
100         printf("Rd Block Len: %d\n", mmc->read_bl_len);
101
102         printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
103                         (mmc->version >> 4) & 0xf, mmc->version & 0xf);
104
105         printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
106         puts("Capacity: ");
107         print_size(mmc->capacity, "\n");
108
109         printf("Bus Width: %d-bit\n", mmc->bus_width);
110 }
111
112 int do_mmcinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
113 {
114         struct mmc *mmc;
115
116         if (curr_device < 0) {
117                 if (get_mmc_num() > 0)
118                         curr_device = 0;
119                 else {
120                         puts("No MMC device available\n");
121                         return 1;
122                 }
123         }
124
125         mmc = find_mmc_device(curr_device);
126
127         if (mmc) {
128                 mmc_init(mmc);
129
130                 print_mmcinfo(mmc);
131                 return 0;
132         } else {
133                 printf("no mmc device at slot %x\n", curr_device);
134                 return 1;
135         }
136 }
137
138 U_BOOT_CMD(
139         mmcinfo, 1, 0, do_mmcinfo,
140         "display MMC info",
141         "    - device number of the device to dislay info of\n"
142         ""
143 );
144
145 int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
146 {
147         if (argc < 2)
148                 return cmd_usage(cmdtp);
149
150         if (curr_device < 0) {
151                 if (get_mmc_num() > 0)
152                         curr_device = 0;
153                 else {
154                         puts("No MMC device available\n");
155                         return 1;
156                 }
157         }
158
159         if (strcmp(argv[1], "rescan") == 0) {
160                 struct mmc *mmc = find_mmc_device(curr_device);
161
162                 if (!mmc) {
163                         printf("no mmc device at slot %x\n", curr_device);
164                         return 1;
165                 }
166
167                 mmc_init(mmc);
168
169                 return 0;
170         } else if (strncmp(argv[1], "part", 4) == 0) {
171                 block_dev_desc_t *mmc_dev;
172                 struct mmc *mmc = find_mmc_device(curr_device);
173
174                 if (!mmc) {
175                         printf("no mmc device at slot %x\n", curr_device);
176                         return 1;
177                 }
178                 mmc_init(mmc);
179                 mmc_dev = mmc_get_dev(curr_device);
180                 if (mmc_dev != NULL &&
181                                 mmc_dev->type != DEV_TYPE_UNKNOWN) {
182                         print_part(mmc_dev);
183                         return 0;
184                 }
185
186                 puts("get mmc type error!\n");
187                 return 1;
188         } else if (strcmp(argv[1], "list") == 0) {
189                 print_mmc_devices('\n');
190                 return 0;
191         } else if (strcmp(argv[1], "dev") == 0) {
192                 int dev;
193                 struct mmc *mmc;
194
195                 if (argc == 2)
196                         dev = curr_device;
197                 else if (argc == 3)
198                         dev = simple_strtoul(argv[2], NULL, 10);
199                 else
200                         return cmd_usage(cmdtp);
201
202                 mmc = find_mmc_device(dev);
203                 if (!mmc) {
204                         printf("no mmc device at slot %x\n", dev);
205                         return 1;
206                 }
207
208                 curr_device = dev;
209                 printf("mmc%d is current device\n", curr_device);
210
211                 return 0;
212         } else if (strcmp(argv[1], "read") == 0) {
213                 void *addr = (void *)simple_strtoul(argv[2], NULL, 16);
214                 u32 cnt = simple_strtoul(argv[4], NULL, 16);
215                 u32 n;
216                 u32 blk = simple_strtoul(argv[3], NULL, 16);
217                 struct mmc *mmc = find_mmc_device(curr_device);
218
219                 if (!mmc) {
220                         printf("no mmc device at slot %x\n", curr_device);
221                         return 1;
222                 }
223
224                 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
225                                 curr_device, blk, cnt);
226
227                 mmc_init(mmc);
228
229                 n = mmc->block_dev.block_read(curr_device, blk, cnt, addr);
230
231                 /* flush cache after read */
232                 flush_cache((ulong)addr, cnt * 512); /* FIXME */
233
234                 printf("%d blocks read: %s\n",
235                                 n, (n==cnt) ? "OK" : "ERROR");
236                 return (n == cnt) ? 0 : 1;
237         } else if (strcmp(argv[1], "write") == 0) {
238                 void *addr = (void *)simple_strtoul(argv[2], NULL, 16);
239                 u32 cnt = simple_strtoul(argv[4], NULL, 16);
240                 u32 n;
241                 struct mmc *mmc = find_mmc_device(curr_device);
242
243                 int blk = simple_strtoul(argv[3], NULL, 16);
244
245                 if (!mmc) {
246                         printf("no mmc device at slot %x\n", curr_device);
247                         return 1;
248                 }
249
250                 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
251                                 curr_device, blk, cnt);
252
253                 mmc_init(mmc);
254
255                 n = mmc->block_dev.block_write(curr_device, blk, cnt, addr);
256
257                 printf("%d blocks written: %s\n",
258                                 n, (n == cnt) ? "OK" : "ERROR");
259                 return (n == cnt) ? 0 : 1;
260         }
261
262         return cmd_usage(cmdtp);
263 }
264
265 U_BOOT_CMD(
266         mmc, 6, 1, do_mmcops,
267         "MMC sub system",
268         "read addr blk# cnt\n"
269         "mmc write addr blk# cnt\n"
270         "mmc rescan\n"
271         "mmc part - lists available partition on current mmc device\n"
272         "mmc dev [dev] - show or set current mmc device\n"
273         "mmc list - lists available devices");
274 #endif