wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 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> |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 26 | #include <mmc.h> |
| 27 | |
Mike Frysinger | 0289fe6 | 2009-06-14 21:35:21 -0400 | [diff] [blame] | 28 | static int curr_device = -1; |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 29 | #ifndef CONFIG_GENERIC_MMC |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 30 | int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 31 | { |
Minkyu Kang | 0f92290 | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 32 | int dev; |
| 33 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 34 | if (argc < 2) |
| 35 | return cmd_usage(cmdtp); |
Minkyu Kang | 0f92290 | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 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 { |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 46 | return cmd_usage(cmdtp); |
Minkyu Kang | 0f92290 | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 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 { |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 71 | return cmd_usage(cmdtp); |
Minkyu Kang | 0f92290 | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | printf("mmc%d is current device\n", curr_device); |
| 75 | } else { |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 76 | return cmd_usage(cmdtp); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 77 | } |
Minkyu Kang | 0f92290 | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 78 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 82 | U_BOOT_CMD( |
Minkyu Kang | 0f92290 | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 83 | mmc, 3, 1, do_mmc, |
| 84 | "MMC sub-system", |
| 85 | "init [dev] - init MMC sub system\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 86 | "mmc device [dev] - show or set current device" |
wdenk | f12e396 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 87 | ); |
Dirk Behme | df8852a | 2009-02-18 19:59:39 +0100 | [diff] [blame] | 88 | #else /* !CONFIG_GENERIC_MMC */ |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 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"); |
Minkyu Kang | 316f81a | 2011-01-04 01:04:19 +0000 | [diff] [blame] | 106 | puts("Capacity: "); |
| 107 | print_size(mmc->capacity, "\n"); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 108 | |
| 109 | printf("Bus Width: %d-bit\n", mmc->bus_width); |
| 110 | } |
| 111 | |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 112 | int do_mmcinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 113 | { |
| 114 | struct mmc *mmc; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 115 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 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 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 124 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 125 | mmc = find_mmc_device(curr_device); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 126 | |
| 127 | if (mmc) { |
| 128 | mmc_init(mmc); |
| 129 | |
| 130 | print_mmcinfo(mmc); |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 131 | return 0; |
| 132 | } else { |
| 133 | printf("no mmc device at slot %x\n", curr_device); |
| 134 | return 1; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 135 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 136 | } |
| 137 | |
Frans Meulenbroeks | 7675a09 | 2010-07-31 15:01:53 +0200 | [diff] [blame] | 138 | U_BOOT_CMD( |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 139 | mmcinfo, 1, 0, do_mmcinfo, |
Frans Meulenbroeks | 0dddc00 | 2010-07-31 15:01:52 +0200 | [diff] [blame] | 140 | "display MMC info", |
Wolfgang Denk | e9e8bab | 2010-09-10 00:16:19 +0200 | [diff] [blame] | 141 | " - device number of the device to dislay info of\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 142 | "" |
| 143 | ); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 144 | |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 145 | int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 146 | { |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 147 | if (argc < 2) |
| 148 | return cmd_usage(cmdtp); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 149 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 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 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 158 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 159 | if (strcmp(argv[1], "rescan") == 0) { |
| 160 | struct mmc *mmc = find_mmc_device(curr_device); |
Rabin Vincent | 3e448aa | 2009-04-05 13:30:53 +0530 | [diff] [blame] | 161 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 162 | if (!mmc) { |
| 163 | printf("no mmc device at slot %x\n", curr_device); |
| 164 | return 1; |
| 165 | } |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 166 | |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 167 | mmc->has_init = 0; |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 168 | mmc_init(mmc); |
Lei Wen | 61dba93 | 2010-09-13 22:07:28 +0800 | [diff] [blame] | 169 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 170 | return 0; |
| 171 | } else if (strncmp(argv[1], "part", 4) == 0) { |
| 172 | block_dev_desc_t *mmc_dev; |
| 173 | struct mmc *mmc = find_mmc_device(curr_device); |
Lei Wen | 61dba93 | 2010-09-13 22:07:28 +0800 | [diff] [blame] | 174 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 175 | if (!mmc) { |
| 176 | printf("no mmc device at slot %x\n", curr_device); |
Lei Wen | 61dba93 | 2010-09-13 22:07:28 +0800 | [diff] [blame] | 177 | return 1; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 178 | } |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 179 | mmc_init(mmc); |
| 180 | mmc_dev = mmc_get_dev(curr_device); |
| 181 | if (mmc_dev != NULL && |
| 182 | mmc_dev->type != DEV_TYPE_UNKNOWN) { |
| 183 | print_part(mmc_dev); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 184 | return 0; |
| 185 | } |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 186 | |
| 187 | puts("get mmc type error!\n"); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 188 | return 1; |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 189 | } else if (strcmp(argv[1], "list") == 0) { |
| 190 | print_mmc_devices('\n'); |
| 191 | return 0; |
| 192 | } else if (strcmp(argv[1], "dev") == 0) { |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 193 | int dev, part = -1; |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 194 | struct mmc *mmc; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 195 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 196 | if (argc == 2) |
| 197 | dev = curr_device; |
| 198 | else if (argc == 3) |
| 199 | dev = simple_strtoul(argv[2], NULL, 10); |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 200 | else if (argc == 4) { |
| 201 | dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 202 | part = (int)simple_strtoul(argv[3], NULL, 10); |
| 203 | if (part > PART_ACCESS_MASK) { |
| 204 | printf("#part_num shouldn't be larger" |
| 205 | " than %d\n", PART_ACCESS_MASK); |
| 206 | return 1; |
| 207 | } |
| 208 | } else |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 209 | return cmd_usage(cmdtp); |
| 210 | |
| 211 | mmc = find_mmc_device(dev); |
| 212 | if (!mmc) { |
| 213 | printf("no mmc device at slot %x\n", dev); |
| 214 | return 1; |
| 215 | } |
| 216 | |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 217 | mmc_init(mmc); |
| 218 | if (part != -1) { |
| 219 | int ret; |
| 220 | if (mmc->part_config == MMCPART_NOAVAILABLE) { |
| 221 | printf("Card doesn't support part_switch\n"); |
| 222 | return 1; |
| 223 | } |
| 224 | |
| 225 | if (part != mmc->part_num) { |
| 226 | ret = mmc_switch_part(dev, part); |
| 227 | if (!ret) |
| 228 | mmc->part_num = part; |
| 229 | |
| 230 | printf("switch to partions #%d, %s\n", |
| 231 | part, (!ret) ? "OK" : "ERROR"); |
| 232 | } |
| 233 | } |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 234 | curr_device = dev; |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 235 | if (mmc->part_config == MMCPART_NOAVAILABLE) |
| 236 | printf("mmc%d is current device\n", curr_device); |
| 237 | else |
| 238 | printf("mmc%d(part %d) is current device\n", |
| 239 | curr_device, mmc->part_num); |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 240 | |
| 241 | return 0; |
| 242 | } else if (strcmp(argv[1], "read") == 0) { |
| 243 | void *addr = (void *)simple_strtoul(argv[2], NULL, 16); |
| 244 | u32 cnt = simple_strtoul(argv[4], NULL, 16); |
| 245 | u32 n; |
| 246 | u32 blk = simple_strtoul(argv[3], NULL, 16); |
| 247 | struct mmc *mmc = find_mmc_device(curr_device); |
| 248 | |
| 249 | if (!mmc) { |
| 250 | printf("no mmc device at slot %x\n", curr_device); |
| 251 | return 1; |
| 252 | } |
Rabin Vincent | 3e448aa | 2009-04-05 13:30:53 +0530 | [diff] [blame] | 253 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 254 | printf("\nMMC read: dev # %d, block # %d, count %d ... ", |
| 255 | curr_device, blk, cnt); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 256 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 257 | mmc_init(mmc); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 258 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 259 | n = mmc->block_dev.block_read(curr_device, blk, cnt, addr); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 260 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 261 | /* flush cache after read */ |
| 262 | flush_cache((ulong)addr, cnt * 512); /* FIXME */ |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 263 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 264 | printf("%d blocks read: %s\n", |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 265 | n, (n==cnt) ? "OK" : "ERROR"); |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 266 | return (n == cnt) ? 0 : 1; |
| 267 | } else if (strcmp(argv[1], "write") == 0) { |
| 268 | void *addr = (void *)simple_strtoul(argv[2], NULL, 16); |
| 269 | u32 cnt = simple_strtoul(argv[4], NULL, 16); |
| 270 | u32 n; |
| 271 | struct mmc *mmc = find_mmc_device(curr_device); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 272 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 273 | int blk = simple_strtoul(argv[3], NULL, 16); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 274 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 275 | if (!mmc) { |
| 276 | printf("no mmc device at slot %x\n", curr_device); |
| 277 | return 1; |
| 278 | } |
Rabin Vincent | 3e448aa | 2009-04-05 13:30:53 +0530 | [diff] [blame] | 279 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 280 | printf("\nMMC write: dev # %d, block # %d, count %d ... ", |
| 281 | curr_device, blk, cnt); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 282 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 283 | mmc_init(mmc); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 284 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 285 | n = mmc->block_dev.block_write(curr_device, blk, cnt, addr); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 286 | |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 287 | printf("%d blocks written: %s\n", |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 288 | n, (n == cnt) ? "OK" : "ERROR"); |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 289 | return (n == cnt) ? 0 : 1; |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 290 | } |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 291 | |
| 292 | return cmd_usage(cmdtp); |
Andy Fleming | ad347bb | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | U_BOOT_CMD( |
| 296 | mmc, 6, 1, do_mmcops, |
Mike Frysinger | 27e1e62 | 2009-03-23 22:27:34 -0400 | [diff] [blame] | 297 | "MMC sub system", |
Lei Wen | d430d7c | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 298 | "read addr blk# cnt\n" |
| 299 | "mmc write addr blk# cnt\n" |
| 300 | "mmc rescan\n" |
| 301 | "mmc part - lists available partition on current mmc device\n" |
Lei Wen | 31b9980 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 302 | "mmc dev [dev] [part] - show or set current mmc device [partition]\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 303 | "mmc list - lists available devices"); |
Dirk Behme | df8852a | 2009-02-18 19:59:39 +0100 | [diff] [blame] | 304 | #endif |