Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2003 |
| 4 | * Kyle Harris, kharris@nexus-tech.net |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 9 | #include <console.h> |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 10 | #include <mmc.h> |
Jassi Brar | 732bc7c | 2018-04-06 12:05:24 +0530 | [diff] [blame] | 11 | #include <sparse_format.h> |
| 12 | #include <image-sparse.h> |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 13 | |
Mike Frysinger | 02e22c2 | 2009-06-14 21:35:21 -0400 | [diff] [blame] | 14 | static int curr_device = -1; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 15 | |
| 16 | static void print_mmcinfo(struct mmc *mmc) |
| 17 | { |
Diego Santa Cruz | c5f0d3f | 2014-12-23 10:50:16 +0100 | [diff] [blame] | 18 | int i; |
| 19 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 20 | printf("Device: %s\n", mmc->cfg->name); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 21 | printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24); |
| 22 | printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff); |
| 23 | printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff, |
| 24 | (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, |
| 25 | (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff); |
| 26 | |
Jean-Jacques Hiblot | 7a96ec7 | 2017-09-21 16:29:56 +0200 | [diff] [blame] | 27 | printf("Bus Speed: %d\n", mmc->clock); |
Jean-Jacques Hiblot | 52d241d | 2017-11-30 17:43:54 +0100 | [diff] [blame] | 28 | #if CONFIG_IS_ENABLED(MMC_VERBOSE) |
Marek Vasut | 41e30dc | 2019-03-18 04:49:21 +0100 | [diff] [blame] | 29 | printf("Mode: %s\n", mmc_mode_name(mmc->selected_mode)); |
Jean-Jacques Hiblot | 52d241d | 2017-11-30 17:43:54 +0100 | [diff] [blame] | 30 | mmc_dump_capabilities("card capabilities", mmc->card_caps); |
| 31 | mmc_dump_capabilities("host capabilities", mmc->host_caps); |
| 32 | #endif |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 33 | printf("Rd Block Len: %d\n", mmc->read_bl_len); |
| 34 | |
Pantelis Antoniou | 4b7cee5 | 2015-01-23 12:12:01 +0200 | [diff] [blame] | 35 | printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC", |
| 36 | EXTRACT_SDMMC_MAJOR_VERSION(mmc->version), |
| 37 | EXTRACT_SDMMC_MINOR_VERSION(mmc->version)); |
| 38 | if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0) |
| 39 | printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version)); |
| 40 | printf("\n"); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 41 | |
| 42 | printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No"); |
Minkyu Kang | 940e078 | 2011-01-04 01:04:19 +0000 | [diff] [blame] | 43 | puts("Capacity: "); |
| 44 | print_size(mmc->capacity, "\n"); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 45 | |
Andrew Gabbasov | 786e8f8 | 2014-12-01 06:59:09 -0600 | [diff] [blame] | 46 | printf("Bus Width: %d-bit%s\n", mmc->bus_width, |
| 47 | mmc->ddr_mode ? " DDR" : ""); |
Diego Santa Cruz | c5f0d3f | 2014-12-23 10:50:16 +0100 | [diff] [blame] | 48 | |
Jean-Jacques Hiblot | e6fa5a5 | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 49 | #if CONFIG_IS_ENABLED(MMC_WRITE) |
Diego Santa Cruz | b036152 | 2014-12-23 10:50:26 +0100 | [diff] [blame] | 50 | puts("Erase Group Size: "); |
| 51 | print_size(((u64)mmc->erase_grp_size) << 9, "\n"); |
Jean-Jacques Hiblot | e6fa5a5 | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 52 | #endif |
Diego Santa Cruz | b036152 | 2014-12-23 10:50:26 +0100 | [diff] [blame] | 53 | |
Diego Santa Cruz | 525ada2 | 2014-12-23 10:50:19 +0100 | [diff] [blame] | 54 | if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) { |
Diego Santa Cruz | c3dbb4f | 2014-12-23 10:50:17 +0100 | [diff] [blame] | 55 | bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0; |
Diego Santa Cruz | beb98a1 | 2014-12-23 10:50:23 +0100 | [diff] [blame] | 56 | bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR); |
Diego Santa Cruz | b036152 | 2014-12-23 10:50:26 +0100 | [diff] [blame] | 57 | |
Jean-Jacques Hiblot | b7a6e2c | 2018-01-04 15:23:36 +0100 | [diff] [blame] | 58 | #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING) |
Diego Santa Cruz | b036152 | 2014-12-23 10:50:26 +0100 | [diff] [blame] | 59 | puts("HC WP Group Size: "); |
| 60 | print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n"); |
Jean-Jacques Hiblot | b7a6e2c | 2018-01-04 15:23:36 +0100 | [diff] [blame] | 61 | #endif |
Diego Santa Cruz | b036152 | 2014-12-23 10:50:26 +0100 | [diff] [blame] | 62 | |
Diego Santa Cruz | c5f0d3f | 2014-12-23 10:50:16 +0100 | [diff] [blame] | 63 | puts("User Capacity: "); |
Diego Santa Cruz | 9e41a00 | 2014-12-23 10:50:33 +0100 | [diff] [blame] | 64 | print_size(mmc->capacity_user, usr_enh ? " ENH" : ""); |
| 65 | if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR) |
| 66 | puts(" WRREL\n"); |
| 67 | else |
| 68 | putc('\n'); |
Diego Santa Cruz | beb98a1 | 2014-12-23 10:50:23 +0100 | [diff] [blame] | 69 | if (usr_enh) { |
| 70 | puts("User Enhanced Start: "); |
| 71 | print_size(mmc->enh_user_start, "\n"); |
| 72 | puts("User Enhanced Size: "); |
| 73 | print_size(mmc->enh_user_size, "\n"); |
| 74 | } |
Diego Santa Cruz | c5f0d3f | 2014-12-23 10:50:16 +0100 | [diff] [blame] | 75 | puts("Boot Capacity: "); |
Diego Santa Cruz | c3dbb4f | 2014-12-23 10:50:17 +0100 | [diff] [blame] | 76 | print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n"); |
Diego Santa Cruz | c5f0d3f | 2014-12-23 10:50:16 +0100 | [diff] [blame] | 77 | puts("RPMB Capacity: "); |
Diego Santa Cruz | c3dbb4f | 2014-12-23 10:50:17 +0100 | [diff] [blame] | 78 | print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n"); |
Diego Santa Cruz | b036152 | 2014-12-23 10:50:26 +0100 | [diff] [blame] | 79 | |
Diego Santa Cruz | c5f0d3f | 2014-12-23 10:50:16 +0100 | [diff] [blame] | 80 | for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) { |
Diego Santa Cruz | c3dbb4f | 2014-12-23 10:50:17 +0100 | [diff] [blame] | 81 | bool is_enh = has_enh && |
| 82 | (mmc->part_attr & EXT_CSD_ENH_GP(i)); |
Diego Santa Cruz | c5f0d3f | 2014-12-23 10:50:16 +0100 | [diff] [blame] | 83 | if (mmc->capacity_gp[i]) { |
Diego Santa Cruz | f289fd7 | 2014-12-23 10:50:18 +0100 | [diff] [blame] | 84 | printf("GP%i Capacity: ", i+1); |
Diego Santa Cruz | c3dbb4f | 2014-12-23 10:50:17 +0100 | [diff] [blame] | 85 | print_size(mmc->capacity_gp[i], |
Diego Santa Cruz | 9e41a00 | 2014-12-23 10:50:33 +0100 | [diff] [blame] | 86 | is_enh ? " ENH" : ""); |
| 87 | if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i)) |
| 88 | puts(" WRREL\n"); |
| 89 | else |
| 90 | putc('\n'); |
Diego Santa Cruz | c5f0d3f | 2014-12-23 10:50:16 +0100 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 94 | } |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 95 | static struct mmc *init_mmc_device(int dev, bool force_init) |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 96 | { |
| 97 | struct mmc *mmc; |
| 98 | mmc = find_mmc_device(dev); |
| 99 | if (!mmc) { |
| 100 | printf("no mmc device at slot %x\n", dev); |
| 101 | return NULL; |
| 102 | } |
Eric Nelson | bcfde7f | 2016-03-27 12:00:14 -0700 | [diff] [blame] | 103 | |
Marek Vasut | d2a0836 | 2019-01-03 22:09:43 +0100 | [diff] [blame] | 104 | if (!mmc_getcd(mmc)) |
| 105 | force_init = true; |
| 106 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 107 | if (force_init) |
| 108 | mmc->has_init = 0; |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 109 | if (mmc_init(mmc)) |
| 110 | return NULL; |
Marek Vasut | 1d044d3 | 2019-01-03 22:09:44 +0100 | [diff] [blame] | 111 | |
| 112 | #ifdef CONFIG_BLOCK_CACHE |
| 113 | struct blk_desc *bd = mmc_get_blk_desc(mmc); |
| 114 | blkcache_invalidate(bd->if_type, bd->devnum); |
| 115 | #endif |
| 116 | |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 117 | return mmc; |
| 118 | } |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 119 | 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] | 120 | { |
| 121 | struct mmc *mmc; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 122 | |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 123 | if (curr_device < 0) { |
| 124 | if (get_mmc_num() > 0) |
| 125 | curr_device = 0; |
| 126 | else { |
| 127 | puts("No MMC device available\n"); |
| 128 | return 1; |
| 129 | } |
| 130 | } |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 131 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 132 | mmc = init_mmc_device(curr_device, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 133 | if (!mmc) |
| 134 | return CMD_RET_FAILURE; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 135 | |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 136 | print_mmcinfo(mmc); |
| 137 | return CMD_RET_SUCCESS; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Alex Kiernan | 5a7b11e | 2018-05-08 04:43:31 +0000 | [diff] [blame] | 140 | #if CONFIG_IS_ENABLED(CMD_MMC_RPMB) |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 141 | static int confirm_key_prog(void) |
| 142 | { |
| 143 | puts("Warning: Programming authentication key can be done only once !\n" |
| 144 | " Use this command only if you are sure of what you are doing,\n" |
| 145 | "Really perform the key programming? <y/N> "); |
| 146 | if (confirm_yesno()) |
| 147 | return 1; |
| 148 | |
| 149 | puts("Authentication key programming aborted\n"); |
| 150 | return 0; |
| 151 | } |
| 152 | static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag, |
| 153 | int argc, char * const argv[]) |
| 154 | { |
| 155 | void *key_addr; |
| 156 | struct mmc *mmc = find_mmc_device(curr_device); |
| 157 | |
| 158 | if (argc != 2) |
| 159 | return CMD_RET_USAGE; |
| 160 | |
| 161 | key_addr = (void *)simple_strtoul(argv[1], NULL, 16); |
| 162 | if (!confirm_key_prog()) |
| 163 | return CMD_RET_FAILURE; |
| 164 | if (mmc_rpmb_set_key(mmc, key_addr)) { |
| 165 | printf("ERROR - Key already programmed ?\n"); |
| 166 | return CMD_RET_FAILURE; |
| 167 | } |
| 168 | return CMD_RET_SUCCESS; |
| 169 | } |
| 170 | static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag, |
| 171 | int argc, char * const argv[]) |
| 172 | { |
| 173 | u16 blk, cnt; |
| 174 | void *addr; |
| 175 | int n; |
| 176 | void *key_addr = NULL; |
| 177 | struct mmc *mmc = find_mmc_device(curr_device); |
| 178 | |
| 179 | if (argc < 4) |
| 180 | return CMD_RET_USAGE; |
| 181 | |
| 182 | addr = (void *)simple_strtoul(argv[1], NULL, 16); |
| 183 | blk = simple_strtoul(argv[2], NULL, 16); |
| 184 | cnt = simple_strtoul(argv[3], NULL, 16); |
| 185 | |
| 186 | if (argc == 5) |
| 187 | key_addr = (void *)simple_strtoul(argv[4], NULL, 16); |
| 188 | |
| 189 | printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ", |
| 190 | curr_device, blk, cnt); |
| 191 | n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr); |
| 192 | |
| 193 | printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR"); |
| 194 | if (n != cnt) |
| 195 | return CMD_RET_FAILURE; |
| 196 | return CMD_RET_SUCCESS; |
| 197 | } |
| 198 | static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag, |
| 199 | int argc, char * const argv[]) |
| 200 | { |
| 201 | u16 blk, cnt; |
| 202 | void *addr; |
| 203 | int n; |
| 204 | void *key_addr; |
| 205 | struct mmc *mmc = find_mmc_device(curr_device); |
| 206 | |
| 207 | if (argc != 5) |
| 208 | return CMD_RET_USAGE; |
| 209 | |
| 210 | addr = (void *)simple_strtoul(argv[1], NULL, 16); |
| 211 | blk = simple_strtoul(argv[2], NULL, 16); |
| 212 | cnt = simple_strtoul(argv[3], NULL, 16); |
| 213 | key_addr = (void *)simple_strtoul(argv[4], NULL, 16); |
| 214 | |
| 215 | printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ", |
| 216 | curr_device, blk, cnt); |
| 217 | n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr); |
| 218 | |
| 219 | printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR"); |
| 220 | if (n != cnt) |
| 221 | return CMD_RET_FAILURE; |
| 222 | return CMD_RET_SUCCESS; |
| 223 | } |
| 224 | static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag, |
| 225 | int argc, char * const argv[]) |
| 226 | { |
| 227 | unsigned long counter; |
| 228 | struct mmc *mmc = find_mmc_device(curr_device); |
| 229 | |
| 230 | if (mmc_rpmb_get_counter(mmc, &counter)) |
| 231 | return CMD_RET_FAILURE; |
| 232 | printf("RPMB Write counter= %lx\n", counter); |
| 233 | return CMD_RET_SUCCESS; |
| 234 | } |
| 235 | |
| 236 | static cmd_tbl_t cmd_rpmb[] = { |
| 237 | U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""), |
| 238 | U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""), |
| 239 | U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""), |
| 240 | U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""), |
| 241 | }; |
| 242 | |
| 243 | static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag, |
| 244 | int argc, char * const argv[]) |
| 245 | { |
| 246 | cmd_tbl_t *cp; |
| 247 | struct mmc *mmc; |
| 248 | char original_part; |
| 249 | int ret; |
| 250 | |
| 251 | cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb)); |
| 252 | |
| 253 | /* Drop the rpmb subcommand */ |
| 254 | argc--; |
| 255 | argv++; |
| 256 | |
| 257 | if (cp == NULL || argc > cp->maxargs) |
| 258 | return CMD_RET_USAGE; |
Boris Brezillon | 80a48dd | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 259 | if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp)) |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 260 | return CMD_RET_SUCCESS; |
| 261 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 262 | mmc = init_mmc_device(curr_device, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 263 | if (!mmc) |
| 264 | return CMD_RET_FAILURE; |
| 265 | |
| 266 | if (!(mmc->version & MMC_VERSION_MMC)) { |
Heinrich Schuchardt | 71a3e5c | 2020-03-29 19:26:57 +0000 | [diff] [blame] | 267 | printf("It is not an eMMC device\n"); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 268 | return CMD_RET_FAILURE; |
| 269 | } |
| 270 | if (mmc->version < MMC_VERSION_4_41) { |
| 271 | printf("RPMB not supported before version 4.41\n"); |
| 272 | return CMD_RET_FAILURE; |
| 273 | } |
| 274 | /* Switch to the RPMB partition */ |
Kever Yang | 4dc80c8 | 2017-06-08 09:20:03 +0800 | [diff] [blame] | 275 | #ifndef CONFIG_BLK |
Marek Vasut | b955e42 | 2016-05-04 16:35:25 +0200 | [diff] [blame] | 276 | original_part = mmc->block_dev.hwpart; |
Kever Yang | 4dc80c8 | 2017-06-08 09:20:03 +0800 | [diff] [blame] | 277 | #else |
| 278 | original_part = mmc_get_blk_desc(mmc)->hwpart; |
| 279 | #endif |
Simon Glass | 69f45cd | 2016-05-01 13:52:29 -0600 | [diff] [blame] | 280 | if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, MMC_PART_RPMB) != |
| 281 | 0) |
Stephen Warren | 873cc1d | 2015-12-07 11:38:49 -0700 | [diff] [blame] | 282 | return CMD_RET_FAILURE; |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 283 | ret = cp->cmd(cmdtp, flag, argc, argv); |
| 284 | |
| 285 | /* Return to original partition */ |
Simon Glass | 69f45cd | 2016-05-01 13:52:29 -0600 | [diff] [blame] | 286 | if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, original_part) != |
| 287 | 0) |
Stephen Warren | 873cc1d | 2015-12-07 11:38:49 -0700 | [diff] [blame] | 288 | return CMD_RET_FAILURE; |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 289 | return ret; |
| 290 | } |
| 291 | #endif |
| 292 | |
| 293 | static int do_mmc_read(cmd_tbl_t *cmdtp, int flag, |
| 294 | int argc, char * const argv[]) |
| 295 | { |
| 296 | struct mmc *mmc; |
| 297 | u32 blk, cnt, n; |
| 298 | void *addr; |
| 299 | |
| 300 | if (argc != 4) |
| 301 | return CMD_RET_USAGE; |
| 302 | |
| 303 | addr = (void *)simple_strtoul(argv[1], NULL, 16); |
| 304 | blk = simple_strtoul(argv[2], NULL, 16); |
| 305 | cnt = simple_strtoul(argv[3], NULL, 16); |
| 306 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 307 | mmc = init_mmc_device(curr_device, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 308 | if (!mmc) |
| 309 | return CMD_RET_FAILURE; |
| 310 | |
| 311 | printf("\nMMC read: dev # %d, block # %d, count %d ... ", |
| 312 | curr_device, blk, cnt); |
| 313 | |
Simon Glass | c40fdca | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 314 | n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 315 | printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR"); |
| 316 | |
| 317 | return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE; |
| 318 | } |
Jean-Jacques Hiblot | e6fa5a5 | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 319 | |
Alex Kiernan | c232d14 | 2018-05-29 15:30:52 +0000 | [diff] [blame] | 320 | #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE) |
Jassi Brar | 732bc7c | 2018-04-06 12:05:24 +0530 | [diff] [blame] | 321 | static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk, |
| 322 | lbaint_t blkcnt, const void *buffer) |
| 323 | { |
| 324 | struct blk_desc *dev_desc = info->priv; |
| 325 | |
| 326 | return blk_dwrite(dev_desc, blk, blkcnt, buffer); |
| 327 | } |
| 328 | |
| 329 | static lbaint_t mmc_sparse_reserve(struct sparse_storage *info, |
| 330 | lbaint_t blk, lbaint_t blkcnt) |
| 331 | { |
| 332 | return blkcnt; |
| 333 | } |
| 334 | |
| 335 | static int do_mmc_sparse_write(cmd_tbl_t *cmdtp, int flag, |
| 336 | int argc, char * const argv[]) |
| 337 | { |
| 338 | struct sparse_storage sparse; |
| 339 | struct blk_desc *dev_desc; |
| 340 | struct mmc *mmc; |
| 341 | char dest[11]; |
| 342 | void *addr; |
| 343 | u32 blk; |
| 344 | |
| 345 | if (argc != 3) |
| 346 | return CMD_RET_USAGE; |
| 347 | |
| 348 | addr = (void *)simple_strtoul(argv[1], NULL, 16); |
| 349 | blk = simple_strtoul(argv[2], NULL, 16); |
| 350 | |
| 351 | if (!is_sparse_image(addr)) { |
| 352 | printf("Not a sparse image\n"); |
| 353 | return CMD_RET_FAILURE; |
| 354 | } |
| 355 | |
| 356 | mmc = init_mmc_device(curr_device, false); |
| 357 | if (!mmc) |
| 358 | return CMD_RET_FAILURE; |
| 359 | |
| 360 | printf("\nMMC Sparse write: dev # %d, block # %d ... ", |
| 361 | curr_device, blk); |
| 362 | |
| 363 | if (mmc_getwp(mmc) == 1) { |
| 364 | printf("Error: card is write protected!\n"); |
| 365 | return CMD_RET_FAILURE; |
| 366 | } |
| 367 | |
| 368 | dev_desc = mmc_get_blk_desc(mmc); |
| 369 | sparse.priv = dev_desc; |
| 370 | sparse.blksz = 512; |
| 371 | sparse.start = blk; |
| 372 | sparse.size = dev_desc->lba - blk; |
| 373 | sparse.write = mmc_sparse_write; |
| 374 | sparse.reserve = mmc_sparse_reserve; |
| 375 | sparse.mssg = NULL; |
| 376 | sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz); |
| 377 | |
Alex Kiernan | c4ded03 | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 378 | if (write_sparse_image(&sparse, dest, addr, NULL)) |
Jassi Brar | 732bc7c | 2018-04-06 12:05:24 +0530 | [diff] [blame] | 379 | return CMD_RET_FAILURE; |
| 380 | else |
| 381 | return CMD_RET_SUCCESS; |
| 382 | } |
| 383 | #endif |
| 384 | |
Alex Kiernan | c232d14 | 2018-05-29 15:30:52 +0000 | [diff] [blame] | 385 | #if CONFIG_IS_ENABLED(MMC_WRITE) |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 386 | static int do_mmc_write(cmd_tbl_t *cmdtp, int flag, |
| 387 | int argc, char * const argv[]) |
| 388 | { |
| 389 | struct mmc *mmc; |
| 390 | u32 blk, cnt, n; |
| 391 | void *addr; |
| 392 | |
| 393 | if (argc != 4) |
| 394 | return CMD_RET_USAGE; |
| 395 | |
| 396 | addr = (void *)simple_strtoul(argv[1], NULL, 16); |
| 397 | blk = simple_strtoul(argv[2], NULL, 16); |
| 398 | cnt = simple_strtoul(argv[3], NULL, 16); |
| 399 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 400 | mmc = init_mmc_device(curr_device, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 401 | if (!mmc) |
| 402 | return CMD_RET_FAILURE; |
| 403 | |
| 404 | printf("\nMMC write: dev # %d, block # %d, count %d ... ", |
| 405 | curr_device, blk, cnt); |
| 406 | |
| 407 | if (mmc_getwp(mmc) == 1) { |
| 408 | printf("Error: card is write protected!\n"); |
| 409 | return CMD_RET_FAILURE; |
| 410 | } |
Simon Glass | c40fdca | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 411 | n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 412 | printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR"); |
| 413 | |
| 414 | return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE; |
| 415 | } |
| 416 | static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag, |
| 417 | int argc, char * const argv[]) |
| 418 | { |
| 419 | struct mmc *mmc; |
| 420 | u32 blk, cnt, n; |
| 421 | |
| 422 | if (argc != 3) |
| 423 | return CMD_RET_USAGE; |
| 424 | |
| 425 | blk = simple_strtoul(argv[1], NULL, 16); |
| 426 | cnt = simple_strtoul(argv[2], NULL, 16); |
| 427 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 428 | mmc = init_mmc_device(curr_device, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 429 | if (!mmc) |
| 430 | return CMD_RET_FAILURE; |
| 431 | |
| 432 | printf("\nMMC erase: dev # %d, block # %d, count %d ... ", |
| 433 | curr_device, blk, cnt); |
| 434 | |
| 435 | if (mmc_getwp(mmc) == 1) { |
| 436 | printf("Error: card is write protected!\n"); |
| 437 | return CMD_RET_FAILURE; |
| 438 | } |
Simon Glass | c40fdca | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 439 | n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 440 | printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR"); |
| 441 | |
| 442 | return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE; |
| 443 | } |
Jean-Jacques Hiblot | e6fa5a5 | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 444 | #endif |
| 445 | |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 446 | static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag, |
| 447 | int argc, char * const argv[]) |
| 448 | { |
| 449 | struct mmc *mmc; |
| 450 | |
Stephen Warren | 941944e | 2014-05-23 13:24:46 -0600 | [diff] [blame] | 451 | mmc = init_mmc_device(curr_device, true); |
| 452 | if (!mmc) |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 453 | return CMD_RET_FAILURE; |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 454 | |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 455 | return CMD_RET_SUCCESS; |
| 456 | } |
| 457 | static int do_mmc_part(cmd_tbl_t *cmdtp, int flag, |
| 458 | int argc, char * const argv[]) |
| 459 | { |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 460 | struct blk_desc *mmc_dev; |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 461 | struct mmc *mmc; |
| 462 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 463 | mmc = init_mmc_device(curr_device, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 464 | if (!mmc) |
| 465 | return CMD_RET_FAILURE; |
| 466 | |
Simon Glass | 3c457f4 | 2016-05-01 11:36:15 -0600 | [diff] [blame] | 467 | mmc_dev = blk_get_devnum_by_type(IF_TYPE_MMC, curr_device); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 468 | if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) { |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 469 | part_print(mmc_dev); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 470 | return CMD_RET_SUCCESS; |
| 471 | } |
| 472 | |
| 473 | puts("get mmc type error!\n"); |
| 474 | return CMD_RET_FAILURE; |
| 475 | } |
| 476 | static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag, |
| 477 | int argc, char * const argv[]) |
| 478 | { |
Stephen Warren | 60dc58f | 2014-05-23 12:48:10 -0600 | [diff] [blame] | 479 | int dev, part = 0, ret; |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 480 | struct mmc *mmc; |
| 481 | |
| 482 | if (argc == 1) { |
| 483 | dev = curr_device; |
| 484 | } else if (argc == 2) { |
| 485 | dev = simple_strtoul(argv[1], NULL, 10); |
| 486 | } else if (argc == 3) { |
| 487 | dev = (int)simple_strtoul(argv[1], NULL, 10); |
| 488 | part = (int)simple_strtoul(argv[2], NULL, 10); |
| 489 | if (part > PART_ACCESS_MASK) { |
| 490 | printf("#part_num shouldn't be larger than %d\n", |
| 491 | PART_ACCESS_MASK); |
| 492 | return CMD_RET_FAILURE; |
| 493 | } |
| 494 | } else { |
| 495 | return CMD_RET_USAGE; |
| 496 | } |
| 497 | |
Stephen Warren | a571092 | 2014-05-23 13:24:47 -0600 | [diff] [blame] | 498 | mmc = init_mmc_device(dev, true); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 499 | if (!mmc) |
| 500 | return CMD_RET_FAILURE; |
| 501 | |
Simon Glass | 69f45cd | 2016-05-01 13:52:29 -0600 | [diff] [blame] | 502 | ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part); |
Stephen Warren | 60dc58f | 2014-05-23 12:48:10 -0600 | [diff] [blame] | 503 | printf("switch to partitions #%d, %s\n", |
| 504 | part, (!ret) ? "OK" : "ERROR"); |
| 505 | if (ret) |
| 506 | return 1; |
| 507 | |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 508 | curr_device = dev; |
| 509 | if (mmc->part_config == MMCPART_NOAVAILABLE) |
| 510 | printf("mmc%d is current device\n", curr_device); |
| 511 | else |
| 512 | printf("mmc%d(part %d) is current device\n", |
Simon Glass | c40fdca | 2016-05-01 13:52:35 -0600 | [diff] [blame] | 513 | curr_device, mmc_get_blk_desc(mmc)->hwpart); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 514 | |
| 515 | return CMD_RET_SUCCESS; |
| 516 | } |
| 517 | static int do_mmc_list(cmd_tbl_t *cmdtp, int flag, |
| 518 | int argc, char * const argv[]) |
| 519 | { |
| 520 | print_mmc_devices('\n'); |
| 521 | return CMD_RET_SUCCESS; |
| 522 | } |
Diego Santa Cruz | c599f53 | 2014-12-23 10:50:30 +0100 | [diff] [blame] | 523 | |
Jean-Jacques Hiblot | cf17789 | 2017-11-30 17:44:02 +0100 | [diff] [blame] | 524 | #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING) |
Diego Santa Cruz | 189f963 | 2014-12-23 10:50:32 +0100 | [diff] [blame] | 525 | static int parse_hwpart_user(struct mmc_hwpart_conf *pconf, |
| 526 | int argc, char * const argv[]) |
| 527 | { |
| 528 | int i = 0; |
| 529 | |
| 530 | memset(&pconf->user, 0, sizeof(pconf->user)); |
| 531 | |
| 532 | while (i < argc) { |
| 533 | if (!strcmp(argv[i], "enh")) { |
| 534 | if (i + 2 >= argc) |
| 535 | return -1; |
| 536 | pconf->user.enh_start = |
| 537 | simple_strtoul(argv[i+1], NULL, 10); |
| 538 | pconf->user.enh_size = |
| 539 | simple_strtoul(argv[i+2], NULL, 10); |
| 540 | i += 3; |
| 541 | } else if (!strcmp(argv[i], "wrrel")) { |
| 542 | if (i + 1 >= argc) |
| 543 | return -1; |
| 544 | pconf->user.wr_rel_change = 1; |
| 545 | if (!strcmp(argv[i+1], "on")) |
| 546 | pconf->user.wr_rel_set = 1; |
| 547 | else if (!strcmp(argv[i+1], "off")) |
| 548 | pconf->user.wr_rel_set = 0; |
| 549 | else |
| 550 | return -1; |
| 551 | i += 2; |
| 552 | } else { |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | return i; |
| 557 | } |
| 558 | |
| 559 | static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx, |
| 560 | int argc, char * const argv[]) |
| 561 | { |
| 562 | int i; |
| 563 | |
| 564 | memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx])); |
| 565 | |
| 566 | if (1 >= argc) |
| 567 | return -1; |
| 568 | pconf->gp_part[pidx].size = simple_strtoul(argv[0], NULL, 10); |
| 569 | |
| 570 | i = 1; |
| 571 | while (i < argc) { |
| 572 | if (!strcmp(argv[i], "enh")) { |
| 573 | pconf->gp_part[pidx].enhanced = 1; |
| 574 | i += 1; |
| 575 | } else if (!strcmp(argv[i], "wrrel")) { |
| 576 | if (i + 1 >= argc) |
| 577 | return -1; |
| 578 | pconf->gp_part[pidx].wr_rel_change = 1; |
| 579 | if (!strcmp(argv[i+1], "on")) |
| 580 | pconf->gp_part[pidx].wr_rel_set = 1; |
| 581 | else if (!strcmp(argv[i+1], "off")) |
| 582 | pconf->gp_part[pidx].wr_rel_set = 0; |
| 583 | else |
| 584 | return -1; |
| 585 | i += 2; |
| 586 | } else { |
| 587 | break; |
| 588 | } |
| 589 | } |
| 590 | return i; |
| 591 | } |
| 592 | |
Diego Santa Cruz | c599f53 | 2014-12-23 10:50:30 +0100 | [diff] [blame] | 593 | static int do_mmc_hwpartition(cmd_tbl_t *cmdtp, int flag, |
| 594 | int argc, char * const argv[]) |
| 595 | { |
| 596 | struct mmc *mmc; |
| 597 | struct mmc_hwpart_conf pconf = { }; |
| 598 | enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK; |
Diego Santa Cruz | 189f963 | 2014-12-23 10:50:32 +0100 | [diff] [blame] | 599 | int i, r, pidx; |
Diego Santa Cruz | c599f53 | 2014-12-23 10:50:30 +0100 | [diff] [blame] | 600 | |
| 601 | mmc = init_mmc_device(curr_device, false); |
| 602 | if (!mmc) |
| 603 | return CMD_RET_FAILURE; |
| 604 | |
| 605 | if (argc < 1) |
| 606 | return CMD_RET_USAGE; |
| 607 | i = 1; |
| 608 | while (i < argc) { |
Diego Santa Cruz | 189f963 | 2014-12-23 10:50:32 +0100 | [diff] [blame] | 609 | if (!strcmp(argv[i], "user")) { |
| 610 | i++; |
| 611 | r = parse_hwpart_user(&pconf, argc-i, &argv[i]); |
| 612 | if (r < 0) |
Diego Santa Cruz | c599f53 | 2014-12-23 10:50:30 +0100 | [diff] [blame] | 613 | return CMD_RET_USAGE; |
Diego Santa Cruz | 189f963 | 2014-12-23 10:50:32 +0100 | [diff] [blame] | 614 | i += r; |
Diego Santa Cruz | c599f53 | 2014-12-23 10:50:30 +0100 | [diff] [blame] | 615 | } else if (!strncmp(argv[i], "gp", 2) && |
| 616 | strlen(argv[i]) == 3 && |
| 617 | argv[i][2] >= '1' && argv[i][2] <= '4') { |
Diego Santa Cruz | c599f53 | 2014-12-23 10:50:30 +0100 | [diff] [blame] | 618 | pidx = argv[i][2] - '1'; |
Diego Santa Cruz | 189f963 | 2014-12-23 10:50:32 +0100 | [diff] [blame] | 619 | i++; |
| 620 | r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]); |
| 621 | if (r < 0) |
| 622 | return CMD_RET_USAGE; |
| 623 | i += r; |
Diego Santa Cruz | c599f53 | 2014-12-23 10:50:30 +0100 | [diff] [blame] | 624 | } else if (!strcmp(argv[i], "check")) { |
| 625 | mode = MMC_HWPART_CONF_CHECK; |
| 626 | i++; |
| 627 | } else if (!strcmp(argv[i], "set")) { |
| 628 | mode = MMC_HWPART_CONF_SET; |
| 629 | i++; |
| 630 | } else if (!strcmp(argv[i], "complete")) { |
| 631 | mode = MMC_HWPART_CONF_COMPLETE; |
| 632 | i++; |
| 633 | } else { |
| 634 | return CMD_RET_USAGE; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | puts("Partition configuration:\n"); |
| 639 | if (pconf.user.enh_size) { |
| 640 | puts("\tUser Enhanced Start: "); |
| 641 | print_size(((u64)pconf.user.enh_start) << 9, "\n"); |
| 642 | puts("\tUser Enhanced Size: "); |
| 643 | print_size(((u64)pconf.user.enh_size) << 9, "\n"); |
| 644 | } else { |
| 645 | puts("\tNo enhanced user data area\n"); |
| 646 | } |
Diego Santa Cruz | 189f963 | 2014-12-23 10:50:32 +0100 | [diff] [blame] | 647 | if (pconf.user.wr_rel_change) |
| 648 | printf("\tUser partition write reliability: %s\n", |
| 649 | pconf.user.wr_rel_set ? "on" : "off"); |
Diego Santa Cruz | c599f53 | 2014-12-23 10:50:30 +0100 | [diff] [blame] | 650 | for (pidx = 0; pidx < 4; pidx++) { |
| 651 | if (pconf.gp_part[pidx].size) { |
| 652 | printf("\tGP%i Capacity: ", pidx+1); |
| 653 | print_size(((u64)pconf.gp_part[pidx].size) << 9, |
| 654 | pconf.gp_part[pidx].enhanced ? |
| 655 | " ENH\n" : "\n"); |
| 656 | } else { |
| 657 | printf("\tNo GP%i partition\n", pidx+1); |
| 658 | } |
Diego Santa Cruz | 189f963 | 2014-12-23 10:50:32 +0100 | [diff] [blame] | 659 | if (pconf.gp_part[pidx].wr_rel_change) |
| 660 | printf("\tGP%i write reliability: %s\n", pidx+1, |
| 661 | pconf.gp_part[pidx].wr_rel_set ? "on" : "off"); |
Diego Santa Cruz | c599f53 | 2014-12-23 10:50:30 +0100 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | if (!mmc_hwpart_config(mmc, &pconf, mode)) { |
| 665 | if (mode == MMC_HWPART_CONF_COMPLETE) |
| 666 | puts("Partitioning successful, " |
| 667 | "power-cycle to make effective\n"); |
| 668 | return CMD_RET_SUCCESS; |
| 669 | } else { |
Diego Santa Cruz | 189f963 | 2014-12-23 10:50:32 +0100 | [diff] [blame] | 670 | puts("Failed!\n"); |
Diego Santa Cruz | c599f53 | 2014-12-23 10:50:30 +0100 | [diff] [blame] | 671 | return CMD_RET_FAILURE; |
| 672 | } |
| 673 | } |
Jean-Jacques Hiblot | cf17789 | 2017-11-30 17:44:02 +0100 | [diff] [blame] | 674 | #endif |
Diego Santa Cruz | c599f53 | 2014-12-23 10:50:30 +0100 | [diff] [blame] | 675 | |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 676 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
| 677 | static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag, |
| 678 | int argc, char * const argv[]) |
| 679 | { |
| 680 | int dev; |
| 681 | struct mmc *mmc; |
| 682 | u8 width, reset, mode; |
| 683 | |
| 684 | if (argc != 5) |
| 685 | return CMD_RET_USAGE; |
| 686 | dev = simple_strtoul(argv[1], NULL, 10); |
| 687 | width = simple_strtoul(argv[2], NULL, 10); |
| 688 | reset = simple_strtoul(argv[3], NULL, 10); |
| 689 | mode = simple_strtoul(argv[4], NULL, 10); |
| 690 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 691 | mmc = init_mmc_device(dev, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 692 | if (!mmc) |
| 693 | return CMD_RET_FAILURE; |
| 694 | |
| 695 | if (IS_SD(mmc)) { |
| 696 | puts("BOOT_BUS_WIDTH only exists on eMMC\n"); |
| 697 | return CMD_RET_FAILURE; |
| 698 | } |
| 699 | |
| 700 | /* acknowledge to be sent during boot operation */ |
| 701 | return mmc_set_boot_bus_width(mmc, width, reset, mode); |
| 702 | } |
| 703 | static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag, |
| 704 | int argc, char * const argv[]) |
| 705 | { |
| 706 | int dev; |
| 707 | struct mmc *mmc; |
| 708 | u32 bootsize, rpmbsize; |
| 709 | |
| 710 | if (argc != 4) |
| 711 | return CMD_RET_USAGE; |
| 712 | dev = simple_strtoul(argv[1], NULL, 10); |
| 713 | bootsize = simple_strtoul(argv[2], NULL, 10); |
| 714 | rpmbsize = simple_strtoul(argv[3], NULL, 10); |
| 715 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 716 | mmc = init_mmc_device(dev, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 717 | if (!mmc) |
| 718 | return CMD_RET_FAILURE; |
| 719 | |
| 720 | if (IS_SD(mmc)) { |
Heinrich Schuchardt | 71a3e5c | 2020-03-29 19:26:57 +0000 | [diff] [blame] | 721 | printf("It is not an eMMC device\n"); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 722 | return CMD_RET_FAILURE; |
| 723 | } |
| 724 | |
| 725 | if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) { |
| 726 | printf("EMMC boot partition Size change Failed.\n"); |
| 727 | return CMD_RET_FAILURE; |
| 728 | } |
| 729 | |
| 730 | printf("EMMC boot partition Size %d MB\n", bootsize); |
| 731 | printf("EMMC RPMB partition Size %d MB\n", rpmbsize); |
| 732 | return CMD_RET_SUCCESS; |
| 733 | } |
Angelo Dureghello | bdb6099 | 2017-08-01 14:27:10 +0200 | [diff] [blame] | 734 | |
| 735 | static int mmc_partconf_print(struct mmc *mmc) |
| 736 | { |
| 737 | u8 ack, access, part; |
| 738 | |
| 739 | if (mmc->part_config == MMCPART_NOAVAILABLE) { |
| 740 | printf("No part_config info for ver. 0x%x\n", mmc->version); |
| 741 | return CMD_RET_FAILURE; |
| 742 | } |
| 743 | |
| 744 | access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config); |
| 745 | ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config); |
| 746 | part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config); |
| 747 | |
| 748 | printf("EXT_CSD[179], PARTITION_CONFIG:\n" |
| 749 | "BOOT_ACK: 0x%x\n" |
| 750 | "BOOT_PARTITION_ENABLE: 0x%x\n" |
| 751 | "PARTITION_ACCESS: 0x%x\n", ack, part, access); |
| 752 | |
| 753 | return CMD_RET_SUCCESS; |
| 754 | } |
| 755 | |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 756 | static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag, |
| 757 | int argc, char * const argv[]) |
| 758 | { |
| 759 | int dev; |
| 760 | struct mmc *mmc; |
| 761 | u8 ack, part_num, access; |
| 762 | |
Angelo Dureghello | bdb6099 | 2017-08-01 14:27:10 +0200 | [diff] [blame] | 763 | if (argc != 2 && argc != 5) |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 764 | return CMD_RET_USAGE; |
| 765 | |
| 766 | dev = simple_strtoul(argv[1], NULL, 10); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 767 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 768 | mmc = init_mmc_device(dev, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 769 | if (!mmc) |
| 770 | return CMD_RET_FAILURE; |
| 771 | |
| 772 | if (IS_SD(mmc)) { |
| 773 | puts("PARTITION_CONFIG only exists on eMMC\n"); |
| 774 | return CMD_RET_FAILURE; |
| 775 | } |
| 776 | |
Angelo Dureghello | bdb6099 | 2017-08-01 14:27:10 +0200 | [diff] [blame] | 777 | if (argc == 2) |
| 778 | return mmc_partconf_print(mmc); |
| 779 | |
| 780 | ack = simple_strtoul(argv[2], NULL, 10); |
| 781 | part_num = simple_strtoul(argv[3], NULL, 10); |
| 782 | access = simple_strtoul(argv[4], NULL, 10); |
| 783 | |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 784 | /* acknowledge to be sent during boot operation */ |
| 785 | return mmc_set_part_conf(mmc, ack, part_num, access); |
| 786 | } |
| 787 | static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag, |
| 788 | int argc, char * const argv[]) |
| 789 | { |
| 790 | int dev; |
| 791 | struct mmc *mmc; |
| 792 | u8 enable; |
| 793 | |
| 794 | /* |
| 795 | * Set the RST_n_ENABLE bit of RST_n_FUNCTION |
| 796 | * The only valid values are 0x0, 0x1 and 0x2 and writing |
| 797 | * a value of 0x1 or 0x2 sets the value permanently. |
| 798 | */ |
| 799 | if (argc != 3) |
| 800 | return CMD_RET_USAGE; |
| 801 | |
| 802 | dev = simple_strtoul(argv[1], NULL, 10); |
| 803 | enable = simple_strtoul(argv[2], NULL, 10); |
| 804 | |
Peng Fan | 678e931 | 2015-11-25 17:16:21 +0800 | [diff] [blame] | 805 | if (enable > 2) { |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 806 | puts("Invalid RST_n_ENABLE value\n"); |
| 807 | return CMD_RET_USAGE; |
| 808 | } |
| 809 | |
Stephen Warren | 1ae24a5 | 2014-05-23 13:24:45 -0600 | [diff] [blame] | 810 | mmc = init_mmc_device(dev, false); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 811 | if (!mmc) |
| 812 | return CMD_RET_FAILURE; |
| 813 | |
| 814 | if (IS_SD(mmc)) { |
| 815 | puts("RST_n_FUNCTION only exists on eMMC\n"); |
| 816 | return CMD_RET_FAILURE; |
| 817 | } |
| 818 | |
| 819 | return mmc_set_rst_n_function(mmc, enable); |
| 820 | } |
| 821 | #endif |
| 822 | static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag, |
| 823 | int argc, char * const argv[]) |
| 824 | { |
| 825 | struct mmc *mmc; |
| 826 | u32 val; |
| 827 | int ret; |
| 828 | |
| 829 | if (argc != 2) |
| 830 | return CMD_RET_USAGE; |
Markus Niebel | 84c1dfe | 2017-02-03 15:26:36 +0100 | [diff] [blame] | 831 | val = simple_strtoul(argv[1], NULL, 16); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 832 | |
| 833 | mmc = find_mmc_device(curr_device); |
| 834 | if (!mmc) { |
| 835 | printf("no mmc device at slot %x\n", curr_device); |
| 836 | return CMD_RET_FAILURE; |
| 837 | } |
| 838 | ret = mmc_set_dsr(mmc, val); |
| 839 | printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR"); |
| 840 | if (!ret) { |
| 841 | mmc->has_init = 0; |
| 842 | if (mmc_init(mmc)) |
| 843 | return CMD_RET_FAILURE; |
| 844 | else |
| 845 | return CMD_RET_SUCCESS; |
| 846 | } |
| 847 | return ret; |
| 848 | } |
| 849 | |
Tomas Melin | cd3d488 | 2016-11-25 11:01:03 +0200 | [diff] [blame] | 850 | #ifdef CONFIG_CMD_BKOPS_ENABLE |
| 851 | static int do_mmc_bkops_enable(cmd_tbl_t *cmdtp, int flag, |
| 852 | int argc, char * const argv[]) |
| 853 | { |
| 854 | int dev; |
| 855 | struct mmc *mmc; |
| 856 | |
| 857 | if (argc != 2) |
| 858 | return CMD_RET_USAGE; |
| 859 | |
| 860 | dev = simple_strtoul(argv[1], NULL, 10); |
| 861 | |
| 862 | mmc = init_mmc_device(dev, false); |
| 863 | if (!mmc) |
| 864 | return CMD_RET_FAILURE; |
| 865 | |
| 866 | if (IS_SD(mmc)) { |
| 867 | puts("BKOPS_EN only exists on eMMC\n"); |
| 868 | return CMD_RET_FAILURE; |
| 869 | } |
| 870 | |
| 871 | return mmc_set_bkops_enable(mmc); |
| 872 | } |
| 873 | #endif |
| 874 | |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 875 | static cmd_tbl_t cmd_mmc[] = { |
| 876 | U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""), |
| 877 | U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""), |
Jean-Jacques Hiblot | e6fa5a5 | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 878 | #if CONFIG_IS_ENABLED(MMC_WRITE) |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 879 | U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""), |
| 880 | U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""), |
Jean-Jacques Hiblot | e6fa5a5 | 2018-01-04 15:23:34 +0100 | [diff] [blame] | 881 | #endif |
Alex Kiernan | c232d14 | 2018-05-29 15:30:52 +0000 | [diff] [blame] | 882 | #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE) |
| 883 | U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""), |
| 884 | #endif |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 885 | U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""), |
| 886 | U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""), |
| 887 | U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""), |
| 888 | U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""), |
Jean-Jacques Hiblot | cf17789 | 2017-11-30 17:44:02 +0100 | [diff] [blame] | 889 | #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING) |
Diego Santa Cruz | 189f963 | 2014-12-23 10:50:32 +0100 | [diff] [blame] | 890 | U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""), |
Jean-Jacques Hiblot | cf17789 | 2017-11-30 17:44:02 +0100 | [diff] [blame] | 891 | #endif |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 892 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
| 893 | U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""), |
Wally Yeh | fa7b885 | 2014-09-25 23:00:16 +0800 | [diff] [blame] | 894 | U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""), |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 895 | U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""), |
| 896 | U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""), |
| 897 | #endif |
Alex Kiernan | 5a7b11e | 2018-05-08 04:43:31 +0000 | [diff] [blame] | 898 | #if CONFIG_IS_ENABLED(CMD_MMC_RPMB) |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 899 | U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""), |
| 900 | #endif |
| 901 | U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""), |
Tomas Melin | cd3d488 | 2016-11-25 11:01:03 +0200 | [diff] [blame] | 902 | #ifdef CONFIG_CMD_BKOPS_ENABLE |
| 903 | U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""), |
| 904 | #endif |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 905 | }; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 906 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 907 | 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] | 908 | { |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 909 | cmd_tbl_t *cp; |
Lei Wen | 6be95cc | 2011-06-22 17:03:30 +0000 | [diff] [blame] | 910 | |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 911 | cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc)); |
| 912 | |
| 913 | /* Drop the mmc command */ |
| 914 | argc--; |
| 915 | argv++; |
| 916 | |
| 917 | if (cp == NULL || argc > cp->maxargs) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 918 | return CMD_RET_USAGE; |
Boris Brezillon | 80a48dd | 2018-12-03 22:54:20 +0100 | [diff] [blame] | 919 | if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp)) |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 920 | return CMD_RET_SUCCESS; |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 921 | |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 922 | if (curr_device < 0) { |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 923 | if (get_mmc_num() > 0) { |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 924 | curr_device = 0; |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 925 | } else { |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 926 | puts("No MMC device available\n"); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 927 | return CMD_RET_FAILURE; |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 928 | } |
| 929 | } |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 930 | return cp->cmd(cmdtp, flag, argc, argv); |
Andy Fleming | 272cc70 | 2008-10-30 16:41:01 -0500 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | U_BOOT_CMD( |
Diego Santa Cruz | 189f963 | 2014-12-23 10:50:32 +0100 | [diff] [blame] | 934 | mmc, 29, 1, do_mmcops, |
Mike Frysinger | 852dbfd | 2009-03-23 22:27:34 -0400 | [diff] [blame] | 935 | "MMC sub system", |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 936 | "info - display info of the current MMC device\n" |
| 937 | "mmc read addr blk# cnt\n" |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 938 | "mmc write addr blk# cnt\n" |
Alex Kiernan | c232d14 | 2018-05-29 15:30:52 +0000 | [diff] [blame] | 939 | #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE) |
Jassi Brar | 732bc7c | 2018-04-06 12:05:24 +0530 | [diff] [blame] | 940 | "mmc swrite addr blk#\n" |
| 941 | #endif |
Lei Wen | e6f99a5 | 2011-06-22 17:03:31 +0000 | [diff] [blame] | 942 | "mmc erase blk# cnt\n" |
Lei Wen | ea6ebe2 | 2011-05-02 16:26:25 +0000 | [diff] [blame] | 943 | "mmc rescan\n" |
| 944 | "mmc part - lists available partition on current mmc device\n" |
Lei Wen | bc897b1 | 2011-05-02 16:26:26 +0000 | [diff] [blame] | 945 | "mmc dev [dev] [part] - show or set current mmc device [partition]\n" |
Amar | 2a91c91 | 2013-04-27 11:43:00 +0530 | [diff] [blame] | 946 | "mmc list - lists available devices\n" |
Alex Kiernan | 8459367 | 2018-06-11 16:20:09 +0000 | [diff] [blame] | 947 | #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING) |
Diego Santa Cruz | c599f53 | 2014-12-23 10:50:30 +0100 | [diff] [blame] | 948 | "mmc hwpartition [args...] - does hardware partitioning\n" |
| 949 | " arguments (sizes in 512-byte blocks):\n" |
Diego Santa Cruz | 189f963 | 2014-12-23 10:50:32 +0100 | [diff] [blame] | 950 | " [user [enh start cnt] [wrrel {on|off}]] - sets user data area attributes\n" |
| 951 | " [gp1|gp2|gp3|gp4 cnt [enh] [wrrel {on|off}]] - general purpose partition\n" |
Diego Santa Cruz | c599f53 | 2014-12-23 10:50:30 +0100 | [diff] [blame] | 952 | " [check|set|complete] - mode, complete set partitioning completed\n" |
Diego Santa Cruz | 189f963 | 2014-12-23 10:50:32 +0100 | [diff] [blame] | 953 | " WARNING: Partitioning is a write-once setting once it is set to complete.\n" |
| 954 | " Power cycling is required to initialize partitions after set to complete.\n" |
Alex Kiernan | 8459367 | 2018-06-11 16:20:09 +0000 | [diff] [blame] | 955 | #endif |
Amar | 2a91c91 | 2013-04-27 11:43:00 +0530 | [diff] [blame] | 956 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
Tom Rini | 5a99b9d | 2014-02-05 10:24:22 -0500 | [diff] [blame] | 957 | "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n" |
| 958 | " - Set the BOOT_BUS_WIDTH field of the specified device\n" |
Tom Rini | f1fd957 | 2014-02-05 10:24:20 -0500 | [diff] [blame] | 959 | "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n" |
| 960 | " - Change sizes of boot and RPMB partitions of specified device\n" |
Angelo Dureghello | bdb6099 | 2017-08-01 14:27:10 +0200 | [diff] [blame] | 961 | "mmc partconf dev [boot_ack boot_partition partition_access]\n" |
| 962 | " - Show or 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] | 963 | "mmc rst-function dev value\n" |
| 964 | " - Change the RST_n_FUNCTION field of the specified device\n" |
| 965 | " 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] | 966 | #endif |
Alex Kiernan | 5a7b11e | 2018-05-08 04:43:31 +0000 | [diff] [blame] | 967 | #if CONFIG_IS_ENABLED(CMD_MMC_RPMB) |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 968 | "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n" |
| 969 | "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n" |
| 970 | "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n" |
| 971 | "mmc rpmb counter - read the value of the write counter\n" |
| 972 | #endif |
| 973 | "mmc setdsr <value> - set DSR register value\n" |
Tomas Melin | cd3d488 | 2016-11-25 11:01:03 +0200 | [diff] [blame] | 974 | #ifdef CONFIG_CMD_BKOPS_ENABLE |
| 975 | "mmc bkops-enable <dev> - enable background operations handshake on device\n" |
| 976 | " WARNING: This is a write-once setting.\n" |
| 977 | #endif |
Amar | 2a91c91 | 2013-04-27 11:43:00 +0530 | [diff] [blame] | 978 | ); |
Pierre Aubert | 1fd93c6 | 2014-04-24 10:30:08 +0200 | [diff] [blame] | 979 | |
| 980 | /* Old command kept for compatibility. Same as 'mmc info' */ |
| 981 | U_BOOT_CMD( |
| 982 | mmcinfo, 1, 0, do_mmcinfo, |
| 983 | "display MMC info", |
| 984 | "- display info of the current MMC device" |
| 985 | ); |