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