wdenk | 71f9511 | 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 | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 26 | #include <mmc.h> |
| 27 | |
Mike Frysinger | 02e22c2 | 2009-06-14 21:35:21 -0400 | [diff] [blame] | 28 | static int curr_device = -1; |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 29 | #ifndef CONFIG_GENERIC_MMC |
Wolfgang Denk | 54841ab | 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 | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 31 | { |
Minkyu Kang | 869f6bf | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 32 | int dev; |
| 33 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 34 | if (argc < 2) |
| 35 | return cmd_usage(cmdtp); |
Minkyu Kang | 869f6bf | 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 | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 46 | return cmd_usage(cmdtp); |
Minkyu Kang | 869f6bf | 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 | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 71 | return cmd_usage(cmdtp); |
Minkyu Kang | 869f6bf | 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 | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 76 | return cmd_usage(cmdtp); |
Minkyu Kang | 869f6bf | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 77 | } |
| 78 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 82 | U_BOOT_CMD( |
Minkyu Kang | 869f6bf | 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 | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 86 | "mmc device [dev] - show or set current device" |
wdenk | b0fce99 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 87 | ); |
Dirk Behme | 3511b4e | 2009-02-18 19:59:39 +0100 | [diff] [blame] | 88 | #else /* !CONFIG_GENERIC_MMC */ |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 89 | |
Lei Wen | 6be95cc | 2011-06-22 17:03:30 +0000 | [diff] [blame] | 90 | enum mmc_state { |
| 91 | MMC_INVALID, |
| 92 | MMC_READ, |
| 93 | MMC_WRITE, |
Lei Wen | e6f99a5 | 2011-06-22 17:03:31 +0000 | [diff] [blame^] | 94 | MMC_ERASE, |
Lei Wen | 6be95cc | 2011-06-22 17:03:30 +0000 | [diff] [blame] | 95 | }; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 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"); |
Minkyu Kang | 940e078 | 2011-01-04 01:04:19 +0000 | [diff] [blame] | 112 | puts("Capacity: "); |
| 113 | print_size(mmc->capacity, "\n"); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 114 | |
| 115 | printf("Bus Width: %d-bit\n", mmc->bus_width); |
| 116 | } |
| 117 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 118 | int do_mmcinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 119 | { |
| 120 | struct mmc *mmc; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 121 | |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 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 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 130 | |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 131 | mmc = find_mmc_device(curr_device); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 132 | |
| 133 | if (mmc) { |
| 134 | mmc_init(mmc); |
| 135 | |
| 136 | print_mmcinfo(mmc); |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 137 | return 0; |
| 138 | } else { |
| 139 | printf("no mmc device at slot %x\n", curr_device); |
| 140 | return 1; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 141 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 142 | } |
| 143 | |
Frans Meulenbroeks | 388a29d | 2010-07-31 15:01:53 +0200 | [diff] [blame] | 144 | U_BOOT_CMD( |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 145 | mmcinfo, 1, 0, do_mmcinfo, |
Frans Meulenbroeks | cc9f607 | 2010-07-31 15:01:52 +0200 | [diff] [blame] | 146 | "display MMC info", |
Wolfgang Denk | 2d941de | 2010-09-10 00:16:19 +0200 | [diff] [blame] | 147 | " - device number of the device to dislay info of\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 148 | "" |
| 149 | ); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 150 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 151 | int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 152 | { |
Lei Wen | 6be95cc | 2011-06-22 17:03:30 +0000 | [diff] [blame] | 153 | enum mmc_state state; |
| 154 | |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 155 | if (argc < 2) |
| 156 | return cmd_usage(cmdtp); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 157 | |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 158 | if (curr_device < 0) { |
| 159 | if (get_mmc_num() > 0) |
| 160 | curr_device = 0; |
| 161 | else { |
| 162 | puts("No MMC device available\n"); |
| 163 | return 1; |
| 164 | } |
| 165 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 166 | |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 167 | if (strcmp(argv[1], "rescan") == 0) { |
| 168 | struct mmc *mmc = find_mmc_device(curr_device); |
Rabin Vincent | e85649c | 2009-04-05 13:30:53 +0530 | [diff] [blame] | 169 | |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 170 | if (!mmc) { |
| 171 | printf("no mmc device at slot %x\n", curr_device); |
Lei Wen | 8f3b964 | 2010-09-13 22:07:28 +0800 | [diff] [blame] | 172 | return 1; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 173 | } |
| 174 | |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 175 | mmc->has_init = 0; |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 176 | mmc_init(mmc); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 177 | |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 178 | return 0; |
| 179 | } else if (strncmp(argv[1], "part", 4) == 0) { |
| 180 | block_dev_desc_t *mmc_dev; |
| 181 | struct mmc *mmc = find_mmc_device(curr_device); |
| 182 | |
| 183 | if (!mmc) { |
| 184 | printf("no mmc device at slot %x\n", curr_device); |
| 185 | return 1; |
| 186 | } |
| 187 | mmc_init(mmc); |
| 188 | mmc_dev = mmc_get_dev(curr_device); |
| 189 | if (mmc_dev != NULL && |
| 190 | mmc_dev->type != DEV_TYPE_UNKNOWN) { |
| 191 | print_part(mmc_dev); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 192 | return 0; |
| 193 | } |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 194 | |
| 195 | puts("get mmc type error!\n"); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 196 | return 1; |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 197 | } else if (strcmp(argv[1], "list") == 0) { |
| 198 | print_mmc_devices('\n'); |
| 199 | return 0; |
| 200 | } else if (strcmp(argv[1], "dev") == 0) { |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 201 | int dev, part = -1; |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 202 | struct mmc *mmc; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 203 | |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 204 | if (argc == 2) |
| 205 | dev = curr_device; |
| 206 | else if (argc == 3) |
| 207 | dev = simple_strtoul(argv[2], NULL, 10); |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 208 | else if (argc == 4) { |
| 209 | dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 210 | part = (int)simple_strtoul(argv[3], NULL, 10); |
| 211 | if (part > PART_ACCESS_MASK) { |
| 212 | printf("#part_num shouldn't be larger" |
| 213 | " than %d\n", PART_ACCESS_MASK); |
| 214 | return 1; |
| 215 | } |
| 216 | } else |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 217 | return cmd_usage(cmdtp); |
Rabin Vincent | e85649c | 2009-04-05 13:30:53 +0530 | [diff] [blame] | 218 | |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 219 | mmc = find_mmc_device(dev); |
| 220 | if (!mmc) { |
| 221 | printf("no mmc device at slot %x\n", dev); |
| 222 | return 1; |
| 223 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 224 | |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 225 | mmc_init(mmc); |
| 226 | if (part != -1) { |
| 227 | int ret; |
| 228 | if (mmc->part_config == MMCPART_NOAVAILABLE) { |
| 229 | printf("Card doesn't support part_switch\n"); |
| 230 | return 1; |
| 231 | } |
| 232 | |
| 233 | if (part != mmc->part_num) { |
| 234 | ret = mmc_switch_part(dev, part); |
| 235 | if (!ret) |
| 236 | mmc->part_num = part; |
| 237 | |
| 238 | printf("switch to partions #%d, %s\n", |
| 239 | part, (!ret) ? "OK" : "ERROR"); |
| 240 | } |
| 241 | } |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 242 | curr_device = dev; |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 243 | if (mmc->part_config == MMCPART_NOAVAILABLE) |
| 244 | printf("mmc%d is current device\n", curr_device); |
| 245 | else |
| 246 | printf("mmc%d(part %d) is current device\n", |
| 247 | curr_device, mmc->part_num); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 248 | |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 249 | return 0; |
Lei Wen | 6be95cc | 2011-06-22 17:03:30 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | if (strcmp(argv[1], "read") == 0) |
| 253 | state = MMC_READ; |
| 254 | else if (strcmp(argv[1], "write") == 0) |
| 255 | state = MMC_WRITE; |
Lei Wen | e6f99a5 | 2011-06-22 17:03:31 +0000 | [diff] [blame^] | 256 | else if (strcmp(argv[1], "erase") == 0) |
| 257 | state = MMC_ERASE; |
Lei Wen | 6be95cc | 2011-06-22 17:03:30 +0000 | [diff] [blame] | 258 | else |
| 259 | state = MMC_INVALID; |
| 260 | |
| 261 | if (state != MMC_INVALID) { |
| 262 | struct mmc *mmc = find_mmc_device(curr_device); |
Lei Wen | e6f99a5 | 2011-06-22 17:03:31 +0000 | [diff] [blame^] | 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 = 0; |
| 272 | blk = simple_strtoul(argv[idx], NULL, 16); |
| 273 | cnt = simple_strtoul(argv[idx + 1], NULL, 16); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 274 | |
Lei Wen | ea6ebe2 | 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 | e85649c | 2009-04-05 13:30:53 +0530 | [diff] [blame] | 279 | |
Lei Wen | 6be95cc | 2011-06-22 17:03:30 +0000 | [diff] [blame] | 280 | printf("\nMMC %s: dev # %d, block # %d, count %d ... ", |
| 281 | argv[1], curr_device, blk, cnt); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 282 | |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 283 | mmc_init(mmc); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 284 | |
Lei Wen | 6be95cc | 2011-06-22 17:03:30 +0000 | [diff] [blame] | 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; |
Lei Wen | e6f99a5 | 2011-06-22 17:03:31 +0000 | [diff] [blame^] | 296 | case MMC_ERASE: |
| 297 | n = mmc->block_dev.block_erase(curr_device, blk, cnt); |
| 298 | break; |
Lei Wen | 6be95cc | 2011-06-22 17:03:30 +0000 | [diff] [blame] | 299 | default: |
| 300 | BUG(); |
| 301 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 302 | |
Lei Wen | 6be95cc | 2011-06-22 17:03:30 +0000 | [diff] [blame] | 303 | printf("%d blocks %s: %s\n", |
| 304 | n, argv[1], (n == cnt) ? "OK" : "ERROR"); |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 305 | return (n == cnt) ? 0 : 1; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 306 | } |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 307 | |
| 308 | return cmd_usage(cmdtp); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | U_BOOT_CMD( |
| 312 | mmc, 6, 1, do_mmcops, |
Mike Frysinger | 852dbfd | 2009-03-23 22:27:34 -0400 | [diff] [blame] | 313 | "MMC sub system", |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 314 | "read addr blk# cnt\n" |
| 315 | "mmc write addr blk# cnt\n" |
Lei Wen | e6f99a5 | 2011-06-22 17:03:31 +0000 | [diff] [blame^] | 316 | "mmc erase blk# cnt\n" |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 317 | "mmc rescan\n" |
| 318 | "mmc part - lists available partition on current mmc device\n" |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 319 | "mmc dev [dev] [part] - show or set current mmc device [partition]\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 320 | "mmc list - lists available devices"); |
Dirk Behme | 3511b4e | 2009-02-18 19:59:39 +0100 | [diff] [blame] | 321 | #endif |