wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Kyle Harris, kharris@nexus-tech.net |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 10 | #include <mmc.h> |
| 11 | |
Mike Frysinger | 02e22c2 | 2009-06-14 21:35:21 -0400 | [diff] [blame] | 12 | static int curr_device = -1; |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 13 | #ifndef CONFIG_GENERIC_MMC |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 14 | 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] | 15 | { |
Minkyu Kang | 869f6bf | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 16 | int dev; |
| 17 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 18 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 19 | return CMD_RET_USAGE; |
Minkyu Kang | 869f6bf | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 20 | |
| 21 | if (strcmp(argv[1], "init") == 0) { |
| 22 | if (argc == 2) { |
| 23 | if (curr_device < 0) |
| 24 | dev = 1; |
| 25 | else |
| 26 | dev = curr_device; |
| 27 | } else if (argc == 3) { |
| 28 | dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 29 | } else { |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 30 | return CMD_RET_USAGE; |
Minkyu Kang | 869f6bf | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | if (mmc_legacy_init(dev) != 0) { |
| 34 | puts("No MMC card found\n"); |
| 35 | return 1; |
| 36 | } |
| 37 | |
| 38 | curr_device = dev; |
| 39 | printf("mmc%d is available\n", curr_device); |
| 40 | } else if (strcmp(argv[1], "device") == 0) { |
| 41 | if (argc == 2) { |
| 42 | if (curr_device < 0) { |
| 43 | puts("No MMC device available\n"); |
| 44 | return 1; |
| 45 | } |
| 46 | } else if (argc == 3) { |
| 47 | dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 48 | |
| 49 | #ifdef CONFIG_SYS_MMC_SET_DEV |
| 50 | if (mmc_set_dev(dev) != 0) |
| 51 | return 1; |
| 52 | #endif |
| 53 | curr_device = dev; |
| 54 | } else { |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 55 | return CMD_RET_USAGE; |
Minkyu Kang | 869f6bf | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | printf("mmc%d is current device\n", curr_device); |
| 59 | } else { |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 60 | return CMD_RET_USAGE; |
Minkyu Kang | 869f6bf | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 61 | } |
| 62 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 66 | U_BOOT_CMD( |
Minkyu Kang | 869f6bf | 2009-03-30 14:55:51 +0900 | [diff] [blame] | 67 | mmc, 3, 1, do_mmc, |
| 68 | "MMC sub-system", |
| 69 | "init [dev] - init MMC sub system\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 70 | "mmc device [dev] - show or set current device" |
wdenk | b0fce99 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 71 | ); |
Dirk Behme | 3511b4e | 2009-02-18 19:59:39 +0100 | [diff] [blame] | 72 | #else /* !CONFIG_GENERIC_MMC */ |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 73 | |
| 74 | static void print_mmcinfo(struct mmc *mmc) |
| 75 | { |
Diego Santa Cruz | c5f0d3f | 2014-12-23 10:50:16 +0100 | [diff] [blame] | 76 | int i; |
| 77 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 78 | printf("Device: %s\n", mmc->cfg->name); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 79 | printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24); |
| 80 | printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff); |
| 81 | printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff, |
| 82 | (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, |
| 83 | (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff); |
| 84 | |
| 85 | printf("Tran Speed: %d\n", mmc->tran_speed); |
| 86 | printf("Rd Block Len: %d\n", mmc->read_bl_len); |
| 87 | |
| 88 | printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC", |
Jaehoon Chung | 64f4a61 | 2013-01-29 19:31:16 +0000 | [diff] [blame] | 89 | (mmc->version >> 8) & 0xf, mmc->version & 0xff); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 90 | |
| 91 | printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No"); |
Minkyu Kang | 940e078 | 2011-01-04 01:04:19 +0000 | [diff] [blame] | 92 | puts("Capacity: "); |
| 93 | print_size(mmc->capacity, "\n"); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 94 | |
Andrew Gabbasov | 786e8f8 | 2014-12-01 06:59:09 -0600 | [diff] [blame] | 95 | printf("Bus Width: %d-bit%s\n", mmc->bus_width, |
| 96 | mmc->ddr_mode ? " DDR" : ""); |
Diego Santa Cruz | c5f0d3f | 2014-12-23 10:50:16 +0100 | [diff] [blame] | 97 | |
Diego Santa Cruz | 525ada2 | 2014-12-23 10:50:19 +0100 | [diff] [blame] | 98 | if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) { |
Diego Santa Cruz | c3dbb4f | 2014-12-23 10:50:17 +0100 | [diff] [blame] | 99 | bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0; |
Diego Santa Cruz | c5f0d3f | 2014-12-23 10:50:16 +0100 | [diff] [blame] | 100 | puts("User Capacity: "); |
Diego Santa Cruz | c3dbb4f | 2014-12-23 10:50:17 +0100 | [diff] [blame] | 101 | print_size(mmc->capacity_user, |
| 102 | has_enh && (mmc->part_attr & EXT_CSD_ENH_USR) ? |
| 103 | " ENH\n" : "\n"); |
Diego Santa Cruz | c5f0d3f | 2014-12-23 10:50:16 +0100 | [diff] [blame] | 104 | puts("Boot Capacity: "); |
Diego Santa Cruz | c3dbb4f | 2014-12-23 10:50:17 +0100 | [diff] [blame] | 105 | print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n"); |
Diego Santa Cruz | c5f0d3f | 2014-12-23 10:50:16 +0100 | [diff] [blame] | 106 | puts("RPMB Capacity: "); |
Diego Santa Cruz | c3dbb4f | 2014-12-23 10:50:17 +0100 | [diff] [blame] | 107 | print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n"); |
Diego Santa Cruz | c5f0d3f | 2014-12-23 10:50:16 +0100 | [diff] [blame] | 108 | for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) { |
Diego Santa Cruz | c3dbb4f | 2014-12-23 10:50:17 +0100 | [diff] [blame] | 109 | bool is_enh = has_enh && |
| 110 | (mmc->part_attr & EXT_CSD_ENH_GP(i)); |
Diego Santa Cruz | c5f0d3f | 2014-12-23 10:50:16 +0100 | [diff] [blame] | 111 | if (mmc->capacity_gp[i]) { |
Diego Santa Cruz | f289fd7 | 2014-12-23 10:50:18 +0100 | [diff] [blame] | 112 | printf("GP%i Capacity: ", i+1); |
Diego Santa Cruz | c3dbb4f | 2014-12-23 10:50:17 +0100 | [diff] [blame] | 113 | print_size(mmc->capacity_gp[i], |
| 114 | is_enh ? " ENH\n" : "\n"); |
Diego Santa Cruz | c5f0d3f | 2014-12-23 10:50:16 +0100 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 118 | } |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 119 | static struct mmc *init_mmc_device(int dev, bool force_init) |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 120 | { |
| 121 | struct mmc *mmc; |
| 122 | mmc = find_mmc_device(dev); |
| 123 | if (!mmc) { |
| 124 | printf("no mmc device at slot %x\n", dev); |
| 125 | return NULL; |
| 126 | } |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 127 | if (force_init) |
| 128 | mmc->has_init = 0; |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 129 | if (mmc_init(mmc)) |
| 130 | return NULL; |
| 131 | return mmc; |
| 132 | } |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 133 | static 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] | 134 | { |
| 135 | struct mmc *mmc; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 136 | |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 137 | if (curr_device < 0) { |
| 138 | if (get_mmc_num() > 0) |
| 139 | curr_device = 0; |
| 140 | else { |
| 141 | puts("No MMC device available\n"); |
| 142 | return 1; |
| 143 | } |
| 144 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 145 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 146 | mmc = init_mmc_device(curr_device, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 147 | if (!mmc) |
| 148 | return CMD_RET_FAILURE; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 149 | |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 150 | print_mmcinfo(mmc); |
| 151 | return CMD_RET_SUCCESS; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 154 | #ifdef CONFIG_SUPPORT_EMMC_RPMB |
| 155 | static int confirm_key_prog(void) |
| 156 | { |
| 157 | puts("Warning: Programming authentication key can be done only once !\n" |
| 158 | " Use this command only if you are sure of what you are doing,\n" |
| 159 | "Really perform the key programming? <y/N> "); |
| 160 | if (confirm_yesno()) |
| 161 | return 1; |
| 162 | |
| 163 | puts("Authentication key programming aborted\n"); |
| 164 | return 0; |
| 165 | } |
| 166 | static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag, |
| 167 | int argc, char * const argv[]) |
| 168 | { |
| 169 | void *key_addr; |
| 170 | struct mmc *mmc = find_mmc_device(curr_device); |
| 171 | |
| 172 | if (argc != 2) |
| 173 | return CMD_RET_USAGE; |
| 174 | |
| 175 | key_addr = (void *)simple_strtoul(argv[1], NULL, 16); |
| 176 | if (!confirm_key_prog()) |
| 177 | return CMD_RET_FAILURE; |
| 178 | if (mmc_rpmb_set_key(mmc, key_addr)) { |
| 179 | printf("ERROR - Key already programmed ?\n"); |
| 180 | return CMD_RET_FAILURE; |
| 181 | } |
| 182 | return CMD_RET_SUCCESS; |
| 183 | } |
| 184 | static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag, |
| 185 | int argc, char * const argv[]) |
| 186 | { |
| 187 | u16 blk, cnt; |
| 188 | void *addr; |
| 189 | int n; |
| 190 | void *key_addr = NULL; |
| 191 | struct mmc *mmc = find_mmc_device(curr_device); |
| 192 | |
| 193 | if (argc < 4) |
| 194 | return CMD_RET_USAGE; |
| 195 | |
| 196 | addr = (void *)simple_strtoul(argv[1], NULL, 16); |
| 197 | blk = simple_strtoul(argv[2], NULL, 16); |
| 198 | cnt = simple_strtoul(argv[3], NULL, 16); |
| 199 | |
| 200 | if (argc == 5) |
| 201 | key_addr = (void *)simple_strtoul(argv[4], NULL, 16); |
| 202 | |
| 203 | printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ", |
| 204 | curr_device, blk, cnt); |
| 205 | n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr); |
| 206 | |
| 207 | printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR"); |
| 208 | if (n != cnt) |
| 209 | return CMD_RET_FAILURE; |
| 210 | return CMD_RET_SUCCESS; |
| 211 | } |
| 212 | static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag, |
| 213 | int argc, char * const argv[]) |
| 214 | { |
| 215 | u16 blk, cnt; |
| 216 | void *addr; |
| 217 | int n; |
| 218 | void *key_addr; |
| 219 | struct mmc *mmc = find_mmc_device(curr_device); |
| 220 | |
| 221 | if (argc != 5) |
| 222 | return CMD_RET_USAGE; |
| 223 | |
| 224 | addr = (void *)simple_strtoul(argv[1], NULL, 16); |
| 225 | blk = simple_strtoul(argv[2], NULL, 16); |
| 226 | cnt = simple_strtoul(argv[3], NULL, 16); |
| 227 | key_addr = (void *)simple_strtoul(argv[4], NULL, 16); |
| 228 | |
| 229 | printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ", |
| 230 | curr_device, blk, cnt); |
| 231 | n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr); |
| 232 | |
| 233 | printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR"); |
| 234 | if (n != cnt) |
| 235 | return CMD_RET_FAILURE; |
| 236 | return CMD_RET_SUCCESS; |
| 237 | } |
| 238 | static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag, |
| 239 | int argc, char * const argv[]) |
| 240 | { |
| 241 | unsigned long counter; |
| 242 | struct mmc *mmc = find_mmc_device(curr_device); |
| 243 | |
| 244 | if (mmc_rpmb_get_counter(mmc, &counter)) |
| 245 | return CMD_RET_FAILURE; |
| 246 | printf("RPMB Write counter= %lx\n", counter); |
| 247 | return CMD_RET_SUCCESS; |
| 248 | } |
| 249 | |
| 250 | static cmd_tbl_t cmd_rpmb[] = { |
| 251 | U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""), |
| 252 | U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""), |
| 253 | U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""), |
| 254 | U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""), |
| 255 | }; |
| 256 | |
| 257 | static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag, |
| 258 | int argc, char * const argv[]) |
| 259 | { |
| 260 | cmd_tbl_t *cp; |
| 261 | struct mmc *mmc; |
| 262 | char original_part; |
| 263 | int ret; |
| 264 | |
| 265 | cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb)); |
| 266 | |
| 267 | /* Drop the rpmb subcommand */ |
| 268 | argc--; |
| 269 | argv++; |
| 270 | |
| 271 | if (cp == NULL || argc > cp->maxargs) |
| 272 | return CMD_RET_USAGE; |
| 273 | if (flag == CMD_FLAG_REPEAT && !cp->repeatable) |
| 274 | return CMD_RET_SUCCESS; |
| 275 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 276 | mmc = init_mmc_device(curr_device, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 277 | if (!mmc) |
| 278 | return CMD_RET_FAILURE; |
| 279 | |
| 280 | if (!(mmc->version & MMC_VERSION_MMC)) { |
| 281 | printf("It is not a EMMC device\n"); |
| 282 | return CMD_RET_FAILURE; |
| 283 | } |
| 284 | if (mmc->version < MMC_VERSION_4_41) { |
| 285 | printf("RPMB not supported before version 4.41\n"); |
| 286 | return CMD_RET_FAILURE; |
| 287 | } |
| 288 | /* Switch to the RPMB partition */ |
| 289 | original_part = mmc->part_num; |
| 290 | if (mmc->part_num != MMC_PART_RPMB) { |
| 291 | if (mmc_switch_part(curr_device, MMC_PART_RPMB) != 0) |
| 292 | return CMD_RET_FAILURE; |
| 293 | mmc->part_num = MMC_PART_RPMB; |
| 294 | } |
| 295 | ret = cp->cmd(cmdtp, flag, argc, argv); |
| 296 | |
| 297 | /* Return to original partition */ |
| 298 | if (mmc->part_num != original_part) { |
| 299 | if (mmc_switch_part(curr_device, original_part) != 0) |
| 300 | return CMD_RET_FAILURE; |
| 301 | mmc->part_num = original_part; |
| 302 | } |
| 303 | return ret; |
| 304 | } |
| 305 | #endif |
| 306 | |
| 307 | static int do_mmc_read(cmd_tbl_t *cmdtp, int flag, |
| 308 | int argc, char * const argv[]) |
| 309 | { |
| 310 | struct mmc *mmc; |
| 311 | u32 blk, cnt, n; |
| 312 | void *addr; |
| 313 | |
| 314 | if (argc != 4) |
| 315 | return CMD_RET_USAGE; |
| 316 | |
| 317 | addr = (void *)simple_strtoul(argv[1], NULL, 16); |
| 318 | blk = simple_strtoul(argv[2], NULL, 16); |
| 319 | cnt = simple_strtoul(argv[3], NULL, 16); |
| 320 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 321 | mmc = init_mmc_device(curr_device, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 322 | if (!mmc) |
| 323 | return CMD_RET_FAILURE; |
| 324 | |
| 325 | printf("\nMMC read: dev # %d, block # %d, count %d ... ", |
| 326 | curr_device, blk, cnt); |
| 327 | |
| 328 | n = mmc->block_dev.block_read(curr_device, blk, cnt, addr); |
| 329 | /* flush cache after read */ |
| 330 | flush_cache((ulong)addr, cnt * 512); /* FIXME */ |
| 331 | printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR"); |
| 332 | |
| 333 | return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE; |
| 334 | } |
| 335 | static int do_mmc_write(cmd_tbl_t *cmdtp, int flag, |
| 336 | int argc, char * const argv[]) |
| 337 | { |
| 338 | struct mmc *mmc; |
| 339 | u32 blk, cnt, n; |
| 340 | void *addr; |
| 341 | |
| 342 | if (argc != 4) |
| 343 | return CMD_RET_USAGE; |
| 344 | |
| 345 | addr = (void *)simple_strtoul(argv[1], NULL, 16); |
| 346 | blk = simple_strtoul(argv[2], NULL, 16); |
| 347 | cnt = simple_strtoul(argv[3], NULL, 16); |
| 348 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 349 | mmc = init_mmc_device(curr_device, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 350 | if (!mmc) |
| 351 | return CMD_RET_FAILURE; |
| 352 | |
| 353 | printf("\nMMC write: dev # %d, block # %d, count %d ... ", |
| 354 | curr_device, blk, cnt); |
| 355 | |
| 356 | if (mmc_getwp(mmc) == 1) { |
| 357 | printf("Error: card is write protected!\n"); |
| 358 | return CMD_RET_FAILURE; |
| 359 | } |
| 360 | n = mmc->block_dev.block_write(curr_device, blk, cnt, addr); |
| 361 | printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR"); |
| 362 | |
| 363 | return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE; |
| 364 | } |
| 365 | static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag, |
| 366 | int argc, char * const argv[]) |
| 367 | { |
| 368 | struct mmc *mmc; |
| 369 | u32 blk, cnt, n; |
| 370 | |
| 371 | if (argc != 3) |
| 372 | return CMD_RET_USAGE; |
| 373 | |
| 374 | blk = simple_strtoul(argv[1], NULL, 16); |
| 375 | cnt = simple_strtoul(argv[2], NULL, 16); |
| 376 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 377 | mmc = init_mmc_device(curr_device, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 378 | if (!mmc) |
| 379 | return CMD_RET_FAILURE; |
| 380 | |
| 381 | printf("\nMMC erase: dev # %d, block # %d, count %d ... ", |
| 382 | curr_device, blk, cnt); |
| 383 | |
| 384 | if (mmc_getwp(mmc) == 1) { |
| 385 | printf("Error: card is write protected!\n"); |
| 386 | return CMD_RET_FAILURE; |
| 387 | } |
| 388 | n = mmc->block_dev.block_erase(curr_device, blk, cnt); |
| 389 | printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR"); |
| 390 | |
| 391 | return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE; |
| 392 | } |
| 393 | static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag, |
| 394 | int argc, char * const argv[]) |
| 395 | { |
| 396 | struct mmc *mmc; |
| 397 | |
Stephen Warren | 941944e | 2014-05-23 13:24:46 -0600 | [diff] [blame] | 398 | mmc = init_mmc_device(curr_device, true); |
| 399 | if (!mmc) |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 400 | return CMD_RET_FAILURE; |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 401 | |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 402 | return CMD_RET_SUCCESS; |
| 403 | } |
| 404 | static int do_mmc_part(cmd_tbl_t *cmdtp, int flag, |
| 405 | int argc, char * const argv[]) |
| 406 | { |
| 407 | block_dev_desc_t *mmc_dev; |
| 408 | struct mmc *mmc; |
| 409 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 410 | mmc = init_mmc_device(curr_device, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 411 | if (!mmc) |
| 412 | return CMD_RET_FAILURE; |
| 413 | |
| 414 | mmc_dev = mmc_get_dev(curr_device); |
| 415 | if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) { |
| 416 | print_part(mmc_dev); |
| 417 | return CMD_RET_SUCCESS; |
| 418 | } |
| 419 | |
| 420 | puts("get mmc type error!\n"); |
| 421 | return CMD_RET_FAILURE; |
| 422 | } |
| 423 | static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag, |
| 424 | int argc, char * const argv[]) |
| 425 | { |
Stephen Warren | 60dc58f | 2014-05-23 12:48:10 -0600 | [diff] [blame] | 426 | int dev, part = 0, ret; |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 427 | struct mmc *mmc; |
| 428 | |
| 429 | if (argc == 1) { |
| 430 | dev = curr_device; |
| 431 | } else if (argc == 2) { |
| 432 | dev = simple_strtoul(argv[1], NULL, 10); |
| 433 | } else if (argc == 3) { |
| 434 | dev = (int)simple_strtoul(argv[1], NULL, 10); |
| 435 | part = (int)simple_strtoul(argv[2], NULL, 10); |
| 436 | if (part > PART_ACCESS_MASK) { |
| 437 | printf("#part_num shouldn't be larger than %d\n", |
| 438 | PART_ACCESS_MASK); |
| 439 | return CMD_RET_FAILURE; |
| 440 | } |
| 441 | } else { |
| 442 | return CMD_RET_USAGE; |
| 443 | } |
| 444 | |
Stephen Warren | a571092 | 2014-05-23 13:24:47 -0600 | [diff] [blame] | 445 | mmc = init_mmc_device(dev, true); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 446 | if (!mmc) |
| 447 | return CMD_RET_FAILURE; |
| 448 | |
Stephen Warren | 60dc58f | 2014-05-23 12:48:10 -0600 | [diff] [blame] | 449 | ret = mmc_select_hwpart(dev, part); |
| 450 | printf("switch to partitions #%d, %s\n", |
| 451 | part, (!ret) ? "OK" : "ERROR"); |
| 452 | if (ret) |
| 453 | return 1; |
| 454 | |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 455 | curr_device = dev; |
| 456 | if (mmc->part_config == MMCPART_NOAVAILABLE) |
| 457 | printf("mmc%d is current device\n", curr_device); |
| 458 | else |
| 459 | printf("mmc%d(part %d) is current device\n", |
| 460 | curr_device, mmc->part_num); |
| 461 | |
| 462 | return CMD_RET_SUCCESS; |
| 463 | } |
| 464 | static int do_mmc_list(cmd_tbl_t *cmdtp, int flag, |
| 465 | int argc, char * const argv[]) |
| 466 | { |
| 467 | print_mmc_devices('\n'); |
| 468 | return CMD_RET_SUCCESS; |
| 469 | } |
| 470 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
| 471 | static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag, |
| 472 | int argc, char * const argv[]) |
| 473 | { |
| 474 | int dev; |
| 475 | struct mmc *mmc; |
| 476 | u8 width, reset, mode; |
| 477 | |
| 478 | if (argc != 5) |
| 479 | return CMD_RET_USAGE; |
| 480 | dev = simple_strtoul(argv[1], NULL, 10); |
| 481 | width = simple_strtoul(argv[2], NULL, 10); |
| 482 | reset = simple_strtoul(argv[3], NULL, 10); |
| 483 | mode = simple_strtoul(argv[4], NULL, 10); |
| 484 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 485 | mmc = init_mmc_device(dev, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 486 | if (!mmc) |
| 487 | return CMD_RET_FAILURE; |
| 488 | |
| 489 | if (IS_SD(mmc)) { |
| 490 | puts("BOOT_BUS_WIDTH only exists on eMMC\n"); |
| 491 | return CMD_RET_FAILURE; |
| 492 | } |
| 493 | |
| 494 | /* acknowledge to be sent during boot operation */ |
| 495 | return mmc_set_boot_bus_width(mmc, width, reset, mode); |
| 496 | } |
| 497 | static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag, |
| 498 | int argc, char * const argv[]) |
| 499 | { |
| 500 | int dev; |
| 501 | struct mmc *mmc; |
| 502 | u32 bootsize, rpmbsize; |
| 503 | |
| 504 | if (argc != 4) |
| 505 | return CMD_RET_USAGE; |
| 506 | dev = simple_strtoul(argv[1], NULL, 10); |
| 507 | bootsize = simple_strtoul(argv[2], NULL, 10); |
| 508 | rpmbsize = simple_strtoul(argv[3], NULL, 10); |
| 509 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 510 | mmc = init_mmc_device(dev, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 511 | if (!mmc) |
| 512 | return CMD_RET_FAILURE; |
| 513 | |
| 514 | if (IS_SD(mmc)) { |
| 515 | printf("It is not a EMMC device\n"); |
| 516 | return CMD_RET_FAILURE; |
| 517 | } |
| 518 | |
| 519 | if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) { |
| 520 | printf("EMMC boot partition Size change Failed.\n"); |
| 521 | return CMD_RET_FAILURE; |
| 522 | } |
| 523 | |
| 524 | printf("EMMC boot partition Size %d MB\n", bootsize); |
| 525 | printf("EMMC RPMB partition Size %d MB\n", rpmbsize); |
| 526 | return CMD_RET_SUCCESS; |
| 527 | } |
| 528 | static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag, |
| 529 | int argc, char * const argv[]) |
| 530 | { |
| 531 | int dev; |
| 532 | struct mmc *mmc; |
| 533 | u8 ack, part_num, access; |
| 534 | |
| 535 | if (argc != 5) |
| 536 | return CMD_RET_USAGE; |
| 537 | |
| 538 | dev = simple_strtoul(argv[1], NULL, 10); |
| 539 | ack = simple_strtoul(argv[2], NULL, 10); |
| 540 | part_num = simple_strtoul(argv[3], NULL, 10); |
| 541 | access = simple_strtoul(argv[4], NULL, 10); |
| 542 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 543 | mmc = init_mmc_device(dev, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 544 | if (!mmc) |
| 545 | return CMD_RET_FAILURE; |
| 546 | |
| 547 | if (IS_SD(mmc)) { |
| 548 | puts("PARTITION_CONFIG only exists on eMMC\n"); |
| 549 | return CMD_RET_FAILURE; |
| 550 | } |
| 551 | |
| 552 | /* acknowledge to be sent during boot operation */ |
| 553 | return mmc_set_part_conf(mmc, ack, part_num, access); |
| 554 | } |
| 555 | static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag, |
| 556 | int argc, char * const argv[]) |
| 557 | { |
| 558 | int dev; |
| 559 | struct mmc *mmc; |
| 560 | u8 enable; |
| 561 | |
| 562 | /* |
| 563 | * Set the RST_n_ENABLE bit of RST_n_FUNCTION |
| 564 | * The only valid values are 0x0, 0x1 and 0x2 and writing |
| 565 | * a value of 0x1 or 0x2 sets the value permanently. |
| 566 | */ |
| 567 | if (argc != 3) |
| 568 | return CMD_RET_USAGE; |
| 569 | |
| 570 | dev = simple_strtoul(argv[1], NULL, 10); |
| 571 | enable = simple_strtoul(argv[2], NULL, 10); |
| 572 | |
| 573 | if (enable > 2 || enable < 0) { |
| 574 | puts("Invalid RST_n_ENABLE value\n"); |
| 575 | return CMD_RET_USAGE; |
| 576 | } |
| 577 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 578 | mmc = init_mmc_device(dev, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 579 | if (!mmc) |
| 580 | return CMD_RET_FAILURE; |
| 581 | |
| 582 | if (IS_SD(mmc)) { |
| 583 | puts("RST_n_FUNCTION only exists on eMMC\n"); |
| 584 | return CMD_RET_FAILURE; |
| 585 | } |
| 586 | |
| 587 | return mmc_set_rst_n_function(mmc, enable); |
| 588 | } |
| 589 | #endif |
| 590 | static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag, |
| 591 | int argc, char * const argv[]) |
| 592 | { |
| 593 | struct mmc *mmc; |
| 594 | u32 val; |
| 595 | int ret; |
| 596 | |
| 597 | if (argc != 2) |
| 598 | return CMD_RET_USAGE; |
| 599 | val = simple_strtoul(argv[2], NULL, 16); |
| 600 | |
| 601 | mmc = find_mmc_device(curr_device); |
| 602 | if (!mmc) { |
| 603 | printf("no mmc device at slot %x\n", curr_device); |
| 604 | return CMD_RET_FAILURE; |
| 605 | } |
| 606 | ret = mmc_set_dsr(mmc, val); |
| 607 | printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR"); |
| 608 | if (!ret) { |
| 609 | mmc->has_init = 0; |
| 610 | if (mmc_init(mmc)) |
| 611 | return CMD_RET_FAILURE; |
| 612 | else |
| 613 | return CMD_RET_SUCCESS; |
| 614 | } |
| 615 | return ret; |
| 616 | } |
| 617 | |
| 618 | static cmd_tbl_t cmd_mmc[] = { |
| 619 | U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""), |
| 620 | U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""), |
| 621 | U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""), |
| 622 | U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""), |
| 623 | U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""), |
| 624 | U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""), |
| 625 | U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""), |
| 626 | U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""), |
| 627 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
| 628 | U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""), |
Wally Yeh | fa7b885 | 2014-09-25 23:00:16 +0800 | [diff] [blame] | 629 | U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""), |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 630 | U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""), |
| 631 | U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""), |
| 632 | #endif |
| 633 | #ifdef CONFIG_SUPPORT_EMMC_RPMB |
| 634 | U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""), |
| 635 | #endif |
| 636 | U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""), |
| 637 | }; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 638 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 639 | static 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] | 640 | { |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 641 | cmd_tbl_t *cp; |
Lei Wen | 6be95cc | 2011-06-22 17:03:30 +0000 | [diff] [blame] | 642 | |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 643 | cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc)); |
| 644 | |
| 645 | /* Drop the mmc command */ |
| 646 | argc--; |
| 647 | argv++; |
| 648 | |
| 649 | if (cp == NULL || argc > cp->maxargs) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 650 | return CMD_RET_USAGE; |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 651 | if (flag == CMD_FLAG_REPEAT && !cp->repeatable) |
| 652 | return CMD_RET_SUCCESS; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 653 | |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 654 | if (curr_device < 0) { |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 655 | if (get_mmc_num() > 0) { |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 656 | curr_device = 0; |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 657 | } else { |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 658 | puts("No MMC device available\n"); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 659 | return CMD_RET_FAILURE; |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 660 | } |
| 661 | } |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 662 | return cp->cmd(cmdtp, flag, argc, argv); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | U_BOOT_CMD( |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 666 | mmc, 7, 1, do_mmcops, |
Mike Frysinger | 852dbfd | 2009-03-23 22:27:34 -0400 | [diff] [blame] | 667 | "MMC sub system", |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 668 | "info - display info of the current MMC device\n" |
| 669 | "mmc read addr blk# cnt\n" |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 670 | "mmc write addr blk# cnt\n" |
Lei Wen | e6f99a5 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 671 | "mmc erase blk# cnt\n" |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 672 | "mmc rescan\n" |
| 673 | "mmc part - lists available partition on current mmc device\n" |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 674 | "mmc dev [dev] [part] - show or set current mmc device [partition]\n" |
Amar | 2a91c91 | 2013-04-27 11:43:00 +0530 | [diff] [blame] | 675 | "mmc list - lists available devices\n" |
| 676 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
Tom Rini | 5a99b9d | 2014-02-05 10:24:22 -0500 | [diff] [blame] | 677 | "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n" |
| 678 | " - Set the BOOT_BUS_WIDTH field of the specified device\n" |
Tom Rini | f1fd957 | 2014-02-05 10:24:20 -0500 | [diff] [blame] | 679 | "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n" |
| 680 | " - Change sizes of boot and RPMB partitions of specified device\n" |
Tom Rini | 792970b | 2014-02-05 10:24:21 -0500 | [diff] [blame] | 681 | "mmc partconf dev boot_ack boot_partition partition_access\n" |
| 682 | " - Change the bits of the PARTITION_CONFIG field of the specified device\n" |
Tom Rini | 33ace36 | 2014-02-07 14:15:20 -0500 | [diff] [blame] | 683 | "mmc rst-function dev value\n" |
| 684 | " - Change the RST_n_FUNCTION field of the specified device\n" |
| 685 | " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n" |
Dirk Behme | 3511b4e | 2009-02-18 19:59:39 +0100 | [diff] [blame] | 686 | #endif |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 687 | #ifdef CONFIG_SUPPORT_EMMC_RPMB |
| 688 | "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n" |
| 689 | "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n" |
| 690 | "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n" |
| 691 | "mmc rpmb counter - read the value of the write counter\n" |
| 692 | #endif |
| 693 | "mmc setdsr <value> - set DSR register value\n" |
Amar | 2a91c91 | 2013-04-27 11:43:00 +0530 | [diff] [blame] | 694 | ); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 695 | |
| 696 | /* Old command kept for compatibility. Same as 'mmc info' */ |
| 697 | U_BOOT_CMD( |
| 698 | mmcinfo, 1, 0, do_mmcinfo, |
| 699 | "display MMC info", |
| 700 | "- display info of the current MMC device" |
| 701 | ); |
| 702 | |
Amar | 2a91c91 | 2013-04-27 11:43:00 +0530 | [diff] [blame] | 703 | #endif /* !CONFIG_GENERIC_MMC */ |