blob: 19f2eb418d6a9e3d9ac3dde2dfa468331009feff [file] [log] [blame]
wdenk71f95112003-06-15 22:40:42 +00001/*
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>
wdenk71f95112003-06-15 22:40:42 +000026#include <mmc.h>
27
Andy Fleming272cc702008-10-30 16:41:01 -050028#ifndef CONFIG_GENERIC_MMC
Mike Frysinger02e22c22009-06-14 21:35:21 -040029static int curr_device = -1;
Minkyu Kang869f6bf2009-03-30 14:55:51 +090030
Wolfgang Denk54841ab2010-06-28 22:00:46 +020031int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk71f95112003-06-15 22:40:42 +000032{
Minkyu Kang869f6bf2009-03-30 14:55:51 +090033 int dev;
34
Wolfgang Denk47e26b12010-07-17 01:06:04 +020035 if (argc < 2)
36 return cmd_usage(cmdtp);
Minkyu Kang869f6bf2009-03-30 14:55:51 +090037
38 if (strcmp(argv[1], "init") == 0) {
39 if (argc == 2) {
40 if (curr_device < 0)
41 dev = 1;
42 else
43 dev = curr_device;
44 } else if (argc == 3) {
45 dev = (int)simple_strtoul(argv[2], NULL, 10);
46 } else {
Wolfgang Denk47e26b12010-07-17 01:06:04 +020047 return cmd_usage(cmdtp);
Minkyu Kang869f6bf2009-03-30 14:55:51 +090048 }
49
50 if (mmc_legacy_init(dev) != 0) {
51 puts("No MMC card found\n");
52 return 1;
53 }
54
55 curr_device = dev;
56 printf("mmc%d is available\n", curr_device);
57 } else if (strcmp(argv[1], "device") == 0) {
58 if (argc == 2) {
59 if (curr_device < 0) {
60 puts("No MMC device available\n");
61 return 1;
62 }
63 } else if (argc == 3) {
64 dev = (int)simple_strtoul(argv[2], NULL, 10);
65
66#ifdef CONFIG_SYS_MMC_SET_DEV
67 if (mmc_set_dev(dev) != 0)
68 return 1;
69#endif
70 curr_device = dev;
71 } else {
Wolfgang Denk47e26b12010-07-17 01:06:04 +020072 return cmd_usage(cmdtp);
Minkyu Kang869f6bf2009-03-30 14:55:51 +090073 }
74
75 printf("mmc%d is current device\n", curr_device);
76 } else {
Wolfgang Denk47e26b12010-07-17 01:06:04 +020077 return cmd_usage(cmdtp);
Minkyu Kang869f6bf2009-03-30 14:55:51 +090078 }
79
wdenk71f95112003-06-15 22:40:42 +000080 return 0;
81}
82
wdenk0d498392003-07-01 21:06:45 +000083U_BOOT_CMD(
Minkyu Kang869f6bf2009-03-30 14:55:51 +090084 mmc, 3, 1, do_mmc,
85 "MMC sub-system",
86 "init [dev] - init MMC sub system\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +020087 "mmc device [dev] - show or set current device"
wdenkb0fce992003-06-29 21:03:46 +000088);
Dirk Behme3511b4e2009-02-18 19:59:39 +010089#else /* !CONFIG_GENERIC_MMC */
Andy Fleming272cc702008-10-30 16:41:01 -050090
91static void print_mmcinfo(struct mmc *mmc)
92{
93 printf("Device: %s\n", mmc->name);
94 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
95 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
96 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
97 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
98 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
99
100 printf("Tran Speed: %d\n", mmc->tran_speed);
101 printf("Rd Block Len: %d\n", mmc->read_bl_len);
102
103 printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
104 (mmc->version >> 4) & 0xf, mmc->version & 0xf);
105
106 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
107 printf("Capacity: %lld\n", mmc->capacity);
108
109 printf("Bus Width: %d-bit\n", mmc->bus_width);
110}
111
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200112int do_mmcinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500113{
114 struct mmc *mmc;
115 int dev_num;
116
117 if (argc < 2)
118 dev_num = 0;
119 else
120 dev_num = simple_strtoul(argv[1], NULL, 0);
121
122 mmc = find_mmc_device(dev_num);
123
124 if (mmc) {
125 mmc_init(mmc);
126
127 print_mmcinfo(mmc);
128 }
129
130 return 0;
131}
132
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200133U_BOOT_CMD(mmcinfo, 2, 0, do_mmcinfo,
Frans Meulenbroekscc9f6072010-07-31 15:01:52 +0200134 "display MMC info",
135 "<dev num>\n
136 " - device number of the device to dislay info of\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200137 ""
138);
Andy Fleming272cc702008-10-30 16:41:01 -0500139
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200140int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500141{
142 int rc = 0;
143
144 switch (argc) {
145 case 3:
146 if (strcmp(argv[1], "rescan") == 0) {
147 int dev = simple_strtoul(argv[2], NULL, 10);
148 struct mmc *mmc = find_mmc_device(dev);
149
Rabin Vincente85649c2009-04-05 13:30:53 +0530150 if (!mmc)
151 return 1;
152
Andy Fleming272cc702008-10-30 16:41:01 -0500153 mmc_init(mmc);
154
155 return 0;
156 }
157
158 case 0:
159 case 1:
160 case 4:
161 printf("Usage:\n%s\n", cmdtp->usage);
162 return 1;
163
164 case 2:
165 if (!strcmp(argv[1], "list")) {
166 print_mmc_devices('\n');
167 return 0;
168 }
169 return 1;
170 default: /* at least 5 args */
171 if (strcmp(argv[1], "read") == 0) {
172 int dev = simple_strtoul(argv[2], NULL, 10);
173 void *addr = (void *)simple_strtoul(argv[3], NULL, 16);
174 u32 cnt = simple_strtoul(argv[5], NULL, 16);
175 u32 n;
176 u32 blk = simple_strtoul(argv[4], NULL, 16);
177 struct mmc *mmc = find_mmc_device(dev);
178
Rabin Vincente85649c2009-04-05 13:30:53 +0530179 if (!mmc)
180 return 1;
181
Andy Fleming272cc702008-10-30 16:41:01 -0500182 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
183 dev, blk, cnt);
184
185 mmc_init(mmc);
186
187 n = mmc->block_dev.block_read(dev, blk, cnt, addr);
188
189 /* flush cache after read */
Wolfgang Denk1bba30e2009-02-19 00:41:08 +0100190 flush_cache((ulong)addr, cnt * 512); /* FIXME */
Andy Fleming272cc702008-10-30 16:41:01 -0500191
192 printf("%d blocks read: %s\n",
193 n, (n==cnt) ? "OK" : "ERROR");
194 return (n == cnt) ? 0 : 1;
195 } else if (strcmp(argv[1], "write") == 0) {
196 int dev = simple_strtoul(argv[2], NULL, 10);
197 void *addr = (void *)simple_strtoul(argv[3], NULL, 16);
198 u32 cnt = simple_strtoul(argv[5], NULL, 16);
199 u32 n;
200 struct mmc *mmc = find_mmc_device(dev);
201
202 int blk = simple_strtoul(argv[4], NULL, 16);
203
Rabin Vincente85649c2009-04-05 13:30:53 +0530204 if (!mmc)
205 return 1;
206
Andy Fleming272cc702008-10-30 16:41:01 -0500207 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
208 dev, blk, cnt);
209
210 mmc_init(mmc);
211
212 n = mmc->block_dev.block_write(dev, blk, cnt, addr);
213
214 printf("%d blocks written: %s\n",
215 n, (n == cnt) ? "OK" : "ERROR");
216 return (n == cnt) ? 0 : 1;
217 } else {
218 printf("Usage:\n%s\n", cmdtp->usage);
219 rc = 1;
220 }
221
222 return rc;
223 }
224}
225
226U_BOOT_CMD(
227 mmc, 6, 1, do_mmcops,
Mike Frysinger852dbfd2009-03-23 22:27:34 -0400228 "MMC sub system",
Rabin Vincentac0865f2009-04-05 13:30:52 +0530229 "read <device num> addr blk# cnt\n"
Andy Fleming272cc702008-10-30 16:41:01 -0500230 "mmc write <device num> addr blk# cnt\n"
231 "mmc rescan <device num>\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200232 "mmc list - lists available devices");
Dirk Behme3511b4e2009-02-18 19:59:39 +0100233#endif