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 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 28 | #ifndef CONFIG_GENERIC_MMC |
Mike Frysinger | 02e22c2 | 2009-06-14 21:35:21 -0400 | [diff] [blame] | 29 | static int curr_device = -1; |
Minkyu Kang | 869f6bf | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 30 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 31 | 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] | 32 | { |
Minkyu Kang | 869f6bf | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 33 | int dev; |
| 34 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 35 | if (argc < 2) |
| 36 | return cmd_usage(cmdtp); |
Minkyu Kang | 869f6bf | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 37 | |
| 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 Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 47 | return cmd_usage(cmdtp); |
Minkyu Kang | 869f6bf | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 48 | } |
| 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 Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 72 | return cmd_usage(cmdtp); |
Minkyu Kang | 869f6bf | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | printf("mmc%d is current device\n", curr_device); |
| 76 | } else { |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 77 | return cmd_usage(cmdtp); |
Minkyu Kang | 869f6bf | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 78 | } |
| 79 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 83 | U_BOOT_CMD( |
Minkyu Kang | 869f6bf | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 84 | mmc, 3, 1, do_mmc, |
| 85 | "MMC sub-system", |
| 86 | "init [dev] - init MMC sub system\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 87 | "mmc device [dev] - show or set current device" |
wdenk | b0fce99 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 88 | ); |
Dirk Behme | 3511b4e | 2009-02-18 19:59:39 +0100 | [diff] [blame] | 89 | #else /* !CONFIG_GENERIC_MMC */ |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 90 | |
| 91 | static 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"); |
Minkyu Kang | 940e078 | 2011-01-04 01:04:19 +0000 | [diff] [blame] | 107 | puts("Capacity: "); |
| 108 | print_size(mmc->capacity, "\n"); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 109 | |
| 110 | printf("Bus Width: %d-bit\n", mmc->bus_width); |
| 111 | } |
| 112 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 113 | 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] | 114 | { |
| 115 | struct mmc *mmc; |
| 116 | int dev_num; |
| 117 | |
| 118 | if (argc < 2) |
| 119 | dev_num = 0; |
| 120 | else |
| 121 | dev_num = simple_strtoul(argv[1], NULL, 0); |
| 122 | |
| 123 | mmc = find_mmc_device(dev_num); |
| 124 | |
| 125 | if (mmc) { |
| 126 | mmc_init(mmc); |
| 127 | |
| 128 | print_mmcinfo(mmc); |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
Frans Meulenbroeks | 388a29d | 2010-07-31 15:01:53 +0200 | [diff] [blame] | 134 | U_BOOT_CMD( |
| 135 | mmcinfo, 2, 0, do_mmcinfo, |
Frans Meulenbroeks | cc9f607 | 2010-07-31 15:01:52 +0200 | [diff] [blame] | 136 | "display MMC info", |
Reinhard Meyer | a9804be | 2010-08-09 17:30:51 +0200 | [diff] [blame] | 137 | "<dev num>\n" |
Wolfgang Denk | 2d941de | 2010-09-10 00:16:19 +0200 | [diff] [blame] | 138 | " - device number of the device to dislay info of\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 139 | "" |
| 140 | ); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 141 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 142 | 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] | 143 | { |
| 144 | int rc = 0; |
| 145 | |
| 146 | switch (argc) { |
| 147 | case 3: |
| 148 | if (strcmp(argv[1], "rescan") == 0) { |
| 149 | int dev = simple_strtoul(argv[2], NULL, 10); |
| 150 | struct mmc *mmc = find_mmc_device(dev); |
| 151 | |
Rabin Vincent | e85649c | 2009-04-05 13:30:53 +0530 | [diff] [blame] | 152 | if (!mmc) |
| 153 | return 1; |
| 154 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 155 | mmc_init(mmc); |
| 156 | |
| 157 | return 0; |
Lei Wen | 8f3b964 | 2010-09-13 22:07:28 +0800 | [diff] [blame] | 158 | } else if (strncmp(argv[1], "part", 4) == 0) { |
| 159 | int dev = simple_strtoul(argv[2], NULL, 10); |
| 160 | block_dev_desc_t *mmc_dev; |
| 161 | struct mmc *mmc = find_mmc_device(dev); |
| 162 | |
| 163 | if (!mmc) { |
| 164 | puts("no mmc devices available\n"); |
| 165 | return 1; |
| 166 | } |
| 167 | mmc_init(mmc); |
| 168 | mmc_dev = mmc_get_dev(dev); |
| 169 | if (mmc_dev != NULL && |
| 170 | mmc_dev->type != DEV_TYPE_UNKNOWN) { |
| 171 | print_part(mmc_dev); |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | puts("get mmc type error!\n"); |
| 176 | return 1; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | case 0: |
| 180 | case 1: |
| 181 | case 4: |
Mike Frysinger | e2fad3f | 2010-07-29 13:40:43 -0400 | [diff] [blame] | 182 | return cmd_usage(cmdtp); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 183 | |
| 184 | case 2: |
| 185 | if (!strcmp(argv[1], "list")) { |
| 186 | print_mmc_devices('\n'); |
| 187 | return 0; |
| 188 | } |
| 189 | return 1; |
| 190 | default: /* at least 5 args */ |
| 191 | if (strcmp(argv[1], "read") == 0) { |
| 192 | int dev = simple_strtoul(argv[2], NULL, 10); |
| 193 | void *addr = (void *)simple_strtoul(argv[3], NULL, 16); |
| 194 | u32 cnt = simple_strtoul(argv[5], NULL, 16); |
| 195 | u32 n; |
| 196 | u32 blk = simple_strtoul(argv[4], NULL, 16); |
| 197 | struct mmc *mmc = find_mmc_device(dev); |
| 198 | |
Rabin Vincent | e85649c | 2009-04-05 13:30:53 +0530 | [diff] [blame] | 199 | if (!mmc) |
| 200 | return 1; |
| 201 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 202 | printf("\nMMC read: dev # %d, block # %d, count %d ... ", |
| 203 | dev, blk, cnt); |
| 204 | |
| 205 | mmc_init(mmc); |
| 206 | |
| 207 | n = mmc->block_dev.block_read(dev, blk, cnt, addr); |
| 208 | |
| 209 | /* flush cache after read */ |
Wolfgang Denk | 1bba30e | 2009-02-19 00:41:08 +0100 | [diff] [blame] | 210 | flush_cache((ulong)addr, cnt * 512); /* FIXME */ |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 211 | |
| 212 | printf("%d blocks read: %s\n", |
| 213 | n, (n==cnt) ? "OK" : "ERROR"); |
| 214 | return (n == cnt) ? 0 : 1; |
| 215 | } else if (strcmp(argv[1], "write") == 0) { |
| 216 | int dev = simple_strtoul(argv[2], NULL, 10); |
| 217 | void *addr = (void *)simple_strtoul(argv[3], NULL, 16); |
| 218 | u32 cnt = simple_strtoul(argv[5], NULL, 16); |
| 219 | u32 n; |
| 220 | struct mmc *mmc = find_mmc_device(dev); |
| 221 | |
| 222 | int blk = simple_strtoul(argv[4], NULL, 16); |
| 223 | |
Rabin Vincent | e85649c | 2009-04-05 13:30:53 +0530 | [diff] [blame] | 224 | if (!mmc) |
| 225 | return 1; |
| 226 | |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 227 | printf("\nMMC write: dev # %d, block # %d, count %d ... ", |
| 228 | dev, blk, cnt); |
| 229 | |
| 230 | mmc_init(mmc); |
| 231 | |
| 232 | n = mmc->block_dev.block_write(dev, blk, cnt, addr); |
| 233 | |
| 234 | printf("%d blocks written: %s\n", |
| 235 | n, (n == cnt) ? "OK" : "ERROR"); |
| 236 | return (n == cnt) ? 0 : 1; |
Mike Frysinger | e2fad3f | 2010-07-29 13:40:43 -0400 | [diff] [blame] | 237 | } else |
| 238 | rc = cmd_usage(cmdtp); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 239 | |
| 240 | return rc; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | U_BOOT_CMD( |
| 245 | mmc, 6, 1, do_mmcops, |
Mike Frysinger | 852dbfd | 2009-03-23 22:27:34 -0400 | [diff] [blame] | 246 | "MMC sub system", |
Rabin Vincent | ac0865f | 2009-04-05 13:30:52 +0530 | [diff] [blame] | 247 | "read <device num> addr blk# cnt\n" |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 248 | "mmc write <device num> addr blk# cnt\n" |
| 249 | "mmc rescan <device num>\n" |
Lei Wen | 8f3b964 | 2010-09-13 22:07:28 +0800 | [diff] [blame] | 250 | "mmc part <device num> - lists available partition on mmc\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 251 | "mmc list - lists available devices"); |
Dirk Behme | 3511b4e | 2009-02-18 19:59:39 +0100 | [diff] [blame] | 252 | #endif |