blob: 64e6be7280386918e017e67f5a66672640954e89 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk71f95112003-06-15 22:40:42 +00002/*
3 * (C) Copyright 2003
4 * Kyle Harris, kharris@nexus-tech.net
wdenk71f95112003-06-15 22:40:42 +00005 */
6
7#include <common.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -06008#include <blk.h>
wdenk71f95112003-06-15 22:40:42 +00009#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070010#include <console.h>
Marek Vasutd5810762020-06-20 14:28:25 +020011#include <memalign.h>
wdenk71f95112003-06-15 22:40:42 +000012#include <mmc.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060013#include <part.h>
Jassi Brar732bc7c2018-04-06 12:05:24 +053014#include <sparse_format.h>
15#include <image-sparse.h>
wdenk71f95112003-06-15 22:40:42 +000016
Mike Frysinger02e22c22009-06-14 21:35:21 -040017static int curr_device = -1;
Andy Fleming272cc702008-10-30 16:41:01 -050018
19static void print_mmcinfo(struct mmc *mmc)
20{
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010021 int i;
22
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020023 printf("Device: %s\n", mmc->cfg->name);
Andy Fleming272cc702008-10-30 16:41:01 -050024 printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
25 printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
26 printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
27 (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
28 (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
29
Jean-Jacques Hiblot7a96ec72017-09-21 16:29:56 +020030 printf("Bus Speed: %d\n", mmc->clock);
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +010031#if CONFIG_IS_ENABLED(MMC_VERBOSE)
Marek Vasut41e30dc2019-03-18 04:49:21 +010032 printf("Mode: %s\n", mmc_mode_name(mmc->selected_mode));
Jean-Jacques Hiblot52d241d2017-11-30 17:43:54 +010033 mmc_dump_capabilities("card capabilities", mmc->card_caps);
34 mmc_dump_capabilities("host capabilities", mmc->host_caps);
35#endif
Andy Fleming272cc702008-10-30 16:41:01 -050036 printf("Rd Block Len: %d\n", mmc->read_bl_len);
37
Pantelis Antoniou4b7cee52015-01-23 12:12:01 +020038 printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
39 EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
40 EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
41 if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
42 printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
43 printf("\n");
Andy Fleming272cc702008-10-30 16:41:01 -050044
45 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang940e0782011-01-04 01:04:19 +000046 puts("Capacity: ");
47 print_size(mmc->capacity, "\n");
Andy Fleming272cc702008-10-30 16:41:01 -050048
Andrew Gabbasov786e8f82014-12-01 06:59:09 -060049 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
50 mmc->ddr_mode ? " DDR" : "");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010051
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +010052#if CONFIG_IS_ENABLED(MMC_WRITE)
Diego Santa Cruzb0361522014-12-23 10:50:26 +010053 puts("Erase Group Size: ");
54 print_size(((u64)mmc->erase_grp_size) << 9, "\n");
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +010055#endif
Diego Santa Cruzb0361522014-12-23 10:50:26 +010056
Diego Santa Cruz525ada22014-12-23 10:50:19 +010057 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +010058 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
Diego Santa Cruzbeb98a12014-12-23 10:50:23 +010059 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
Marek Vasutd5810762020-06-20 14:28:25 +020060 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
61 u8 wp;
Heinrich Schuchardtd5210e42020-03-30 07:24:18 +020062 int ret;
Diego Santa Cruzb0361522014-12-23 10:50:26 +010063
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +010064#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruzb0361522014-12-23 10:50:26 +010065 puts("HC WP Group Size: ");
66 print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
Jean-Jacques Hiblotb7a6e2c2018-01-04 15:23:36 +010067#endif
Diego Santa Cruzb0361522014-12-23 10:50:26 +010068
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010069 puts("User Capacity: ");
Diego Santa Cruz9e41a002014-12-23 10:50:33 +010070 print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
71 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
72 puts(" WRREL\n");
73 else
74 putc('\n');
Diego Santa Cruzbeb98a12014-12-23 10:50:23 +010075 if (usr_enh) {
76 puts("User Enhanced Start: ");
77 print_size(mmc->enh_user_start, "\n");
78 puts("User Enhanced Size: ");
79 print_size(mmc->enh_user_size, "\n");
80 }
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010081 puts("Boot Capacity: ");
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +010082 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010083 puts("RPMB Capacity: ");
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +010084 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
Diego Santa Cruzb0361522014-12-23 10:50:26 +010085
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010086 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +010087 bool is_enh = has_enh &&
88 (mmc->part_attr & EXT_CSD_ENH_GP(i));
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010089 if (mmc->capacity_gp[i]) {
Diego Santa Cruzf289fd72014-12-23 10:50:18 +010090 printf("GP%i Capacity: ", i+1);
Diego Santa Cruzc3dbb4f2014-12-23 10:50:17 +010091 print_size(mmc->capacity_gp[i],
Diego Santa Cruz9e41a002014-12-23 10:50:33 +010092 is_enh ? " ENH" : "");
93 if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
94 puts(" WRREL\n");
95 else
96 putc('\n');
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +010097 }
98 }
Heinrich Schuchardtd5210e42020-03-30 07:24:18 +020099 ret = mmc_send_ext_csd(mmc, ext_csd);
100 if (ret)
101 return;
102 wp = ext_csd[EXT_CSD_BOOT_WP_STATUS];
103 for (i = 0; i < 2; ++i) {
104 printf("Boot area %d is ", i);
105 switch (wp & 3) {
106 case 0:
107 printf("not write protected\n");
108 break;
109 case 1:
110 printf("power on protected\n");
111 break;
112 case 2:
113 printf("permanently protected\n");
114 break;
115 default:
116 printf("in reserved protection state\n");
117 break;
118 }
119 wp >>= 2;
120 }
Diego Santa Cruzc5f0d3f2014-12-23 10:50:16 +0100121 }
Andy Fleming272cc702008-10-30 16:41:01 -0500122}
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530123
124static struct mmc *__init_mmc_device(int dev, bool force_init,
125 enum bus_mode speed_mode)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200126{
127 struct mmc *mmc;
128 mmc = find_mmc_device(dev);
129 if (!mmc) {
130 printf("no mmc device at slot %x\n", dev);
131 return NULL;
132 }
Eric Nelsonbcfde7f2016-03-27 12:00:14 -0700133
Marek Vasutd2a08362019-01-03 22:09:43 +0100134 if (!mmc_getcd(mmc))
135 force_init = true;
136
Stephen Warren1ae24a52014-05-23 13:24:45 -0600137 if (force_init)
138 mmc->has_init = 0;
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530139
140 if (IS_ENABLED(CONFIG_MMC_SPEED_MODE_SET))
141 mmc->user_speed_mode = speed_mode;
142
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200143 if (mmc_init(mmc))
144 return NULL;
Marek Vasut1d044d32019-01-03 22:09:44 +0100145
146#ifdef CONFIG_BLOCK_CACHE
147 struct blk_desc *bd = mmc_get_blk_desc(mmc);
148 blkcache_invalidate(bd->if_type, bd->devnum);
149#endif
150
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200151 return mmc;
152}
Simon Glass09140112020-05-10 11:40:03 -0600153
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530154static struct mmc *init_mmc_device(int dev, bool force_init)
155{
156 return __init_mmc_device(dev, force_init, MMC_MODES_END);
157}
158
Simon Glass09140112020-05-10 11:40:03 -0600159static int do_mmcinfo(struct cmd_tbl *cmdtp, int flag, int argc,
160 char *const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -0500161{
162 struct mmc *mmc;
Andy Fleming272cc702008-10-30 16:41:01 -0500163
Lei Wenea6ebe22011-05-02 16:26:25 +0000164 if (curr_device < 0) {
165 if (get_mmc_num() > 0)
166 curr_device = 0;
167 else {
168 puts("No MMC device available\n");
169 return 1;
170 }
171 }
Andy Fleming272cc702008-10-30 16:41:01 -0500172
Stephen Warren1ae24a52014-05-23 13:24:45 -0600173 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200174 if (!mmc)
175 return CMD_RET_FAILURE;
Andy Fleming272cc702008-10-30 16:41:01 -0500176
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200177 print_mmcinfo(mmc);
178 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -0500179}
180
Alex Kiernan5a7b11e2018-05-08 04:43:31 +0000181#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200182static int confirm_key_prog(void)
183{
184 puts("Warning: Programming authentication key can be done only once !\n"
185 " Use this command only if you are sure of what you are doing,\n"
186 "Really perform the key programming? <y/N> ");
187 if (confirm_yesno())
188 return 1;
189
190 puts("Authentication key programming aborted\n");
191 return 0;
192}
Simon Glass09140112020-05-10 11:40:03 -0600193
194static int do_mmcrpmb_key(struct cmd_tbl *cmdtp, int flag,
195 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200196{
197 void *key_addr;
198 struct mmc *mmc = find_mmc_device(curr_device);
199
200 if (argc != 2)
201 return CMD_RET_USAGE;
202
Simon Glass7e5f4602021-07-24 09:03:29 -0600203 key_addr = (void *)hextoul(argv[1], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200204 if (!confirm_key_prog())
205 return CMD_RET_FAILURE;
206 if (mmc_rpmb_set_key(mmc, key_addr)) {
207 printf("ERROR - Key already programmed ?\n");
208 return CMD_RET_FAILURE;
209 }
210 return CMD_RET_SUCCESS;
211}
Simon Glass09140112020-05-10 11:40:03 -0600212
213static int do_mmcrpmb_read(struct cmd_tbl *cmdtp, int flag,
214 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200215{
216 u16 blk, cnt;
217 void *addr;
218 int n;
219 void *key_addr = NULL;
220 struct mmc *mmc = find_mmc_device(curr_device);
221
222 if (argc < 4)
223 return CMD_RET_USAGE;
224
Simon Glass7e5f4602021-07-24 09:03:29 -0600225 addr = (void *)hextoul(argv[1], NULL);
226 blk = hextoul(argv[2], NULL);
227 cnt = hextoul(argv[3], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200228
229 if (argc == 5)
Simon Glass7e5f4602021-07-24 09:03:29 -0600230 key_addr = (void *)hextoul(argv[4], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200231
232 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
233 curr_device, blk, cnt);
234 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
235
236 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
237 if (n != cnt)
238 return CMD_RET_FAILURE;
239 return CMD_RET_SUCCESS;
240}
Simon Glass09140112020-05-10 11:40:03 -0600241
242static int do_mmcrpmb_write(struct cmd_tbl *cmdtp, int flag,
243 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200244{
245 u16 blk, cnt;
246 void *addr;
247 int n;
248 void *key_addr;
249 struct mmc *mmc = find_mmc_device(curr_device);
250
251 if (argc != 5)
252 return CMD_RET_USAGE;
253
Simon Glass7e5f4602021-07-24 09:03:29 -0600254 addr = (void *)hextoul(argv[1], NULL);
255 blk = hextoul(argv[2], NULL);
256 cnt = hextoul(argv[3], NULL);
257 key_addr = (void *)hextoul(argv[4], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200258
259 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
260 curr_device, blk, cnt);
261 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
262
263 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
264 if (n != cnt)
265 return CMD_RET_FAILURE;
266 return CMD_RET_SUCCESS;
267}
Simon Glass09140112020-05-10 11:40:03 -0600268
269static int do_mmcrpmb_counter(struct cmd_tbl *cmdtp, int flag,
270 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200271{
272 unsigned long counter;
273 struct mmc *mmc = find_mmc_device(curr_device);
274
275 if (mmc_rpmb_get_counter(mmc, &counter))
276 return CMD_RET_FAILURE;
277 printf("RPMB Write counter= %lx\n", counter);
278 return CMD_RET_SUCCESS;
279}
280
Simon Glass09140112020-05-10 11:40:03 -0600281static struct cmd_tbl cmd_rpmb[] = {
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200282 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
283 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
284 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
285 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
286};
287
Simon Glass09140112020-05-10 11:40:03 -0600288static int do_mmcrpmb(struct cmd_tbl *cmdtp, int flag,
289 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200290{
Simon Glass09140112020-05-10 11:40:03 -0600291 struct cmd_tbl *cp;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200292 struct mmc *mmc;
293 char original_part;
294 int ret;
295
296 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
297
298 /* Drop the rpmb subcommand */
299 argc--;
300 argv++;
301
302 if (cp == NULL || argc > cp->maxargs)
303 return CMD_RET_USAGE;
Boris Brezillon80a48dd2018-12-03 22:54:20 +0100304 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200305 return CMD_RET_SUCCESS;
306
Stephen Warren1ae24a52014-05-23 13:24:45 -0600307 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200308 if (!mmc)
309 return CMD_RET_FAILURE;
310
311 if (!(mmc->version & MMC_VERSION_MMC)) {
Heinrich Schuchardt71a3e5c2020-03-29 19:26:57 +0000312 printf("It is not an eMMC device\n");
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200313 return CMD_RET_FAILURE;
314 }
315 if (mmc->version < MMC_VERSION_4_41) {
316 printf("RPMB not supported before version 4.41\n");
317 return CMD_RET_FAILURE;
318 }
319 /* Switch to the RPMB partition */
Kever Yang4dc80c82017-06-08 09:20:03 +0800320#ifndef CONFIG_BLK
Marek Vasutb955e422016-05-04 16:35:25 +0200321 original_part = mmc->block_dev.hwpart;
Kever Yang4dc80c82017-06-08 09:20:03 +0800322#else
323 original_part = mmc_get_blk_desc(mmc)->hwpart;
324#endif
Simon Glass69f45cd2016-05-01 13:52:29 -0600325 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, MMC_PART_RPMB) !=
326 0)
Stephen Warren873cc1d2015-12-07 11:38:49 -0700327 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200328 ret = cp->cmd(cmdtp, flag, argc, argv);
329
330 /* Return to original partition */
Simon Glass69f45cd2016-05-01 13:52:29 -0600331 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, original_part) !=
332 0)
Stephen Warren873cc1d2015-12-07 11:38:49 -0700333 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200334 return ret;
335}
336#endif
337
Simon Glass09140112020-05-10 11:40:03 -0600338static int do_mmc_read(struct cmd_tbl *cmdtp, int flag,
339 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200340{
341 struct mmc *mmc;
342 u32 blk, cnt, n;
343 void *addr;
344
345 if (argc != 4)
346 return CMD_RET_USAGE;
347
Simon Glass7e5f4602021-07-24 09:03:29 -0600348 addr = (void *)hextoul(argv[1], NULL);
349 blk = hextoul(argv[2], NULL);
350 cnt = hextoul(argv[3], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200351
Stephen Warren1ae24a52014-05-23 13:24:45 -0600352 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200353 if (!mmc)
354 return CMD_RET_FAILURE;
355
356 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
357 curr_device, blk, cnt);
358
Simon Glassc40fdca2016-05-01 13:52:35 -0600359 n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200360 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
361
362 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
363}
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +0100364
Alex Kiernanc232d142018-05-29 15:30:52 +0000365#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar732bc7c2018-04-06 12:05:24 +0530366static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk,
367 lbaint_t blkcnt, const void *buffer)
368{
369 struct blk_desc *dev_desc = info->priv;
370
371 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
372}
373
374static lbaint_t mmc_sparse_reserve(struct sparse_storage *info,
375 lbaint_t blk, lbaint_t blkcnt)
376{
377 return blkcnt;
378}
379
Simon Glass09140112020-05-10 11:40:03 -0600380static int do_mmc_sparse_write(struct cmd_tbl *cmdtp, int flag,
381 int argc, char *const argv[])
Jassi Brar732bc7c2018-04-06 12:05:24 +0530382{
383 struct sparse_storage sparse;
384 struct blk_desc *dev_desc;
385 struct mmc *mmc;
386 char dest[11];
387 void *addr;
388 u32 blk;
389
390 if (argc != 3)
391 return CMD_RET_USAGE;
392
Simon Glass7e5f4602021-07-24 09:03:29 -0600393 addr = (void *)hextoul(argv[1], NULL);
394 blk = hextoul(argv[2], NULL);
Jassi Brar732bc7c2018-04-06 12:05:24 +0530395
396 if (!is_sparse_image(addr)) {
397 printf("Not a sparse image\n");
398 return CMD_RET_FAILURE;
399 }
400
401 mmc = init_mmc_device(curr_device, false);
402 if (!mmc)
403 return CMD_RET_FAILURE;
404
405 printf("\nMMC Sparse write: dev # %d, block # %d ... ",
406 curr_device, blk);
407
408 if (mmc_getwp(mmc) == 1) {
409 printf("Error: card is write protected!\n");
410 return CMD_RET_FAILURE;
411 }
412
413 dev_desc = mmc_get_blk_desc(mmc);
414 sparse.priv = dev_desc;
415 sparse.blksz = 512;
416 sparse.start = blk;
417 sparse.size = dev_desc->lba - blk;
418 sparse.write = mmc_sparse_write;
419 sparse.reserve = mmc_sparse_reserve;
420 sparse.mssg = NULL;
421 sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz);
422
Alex Kiernanc4ded032018-05-29 15:30:40 +0000423 if (write_sparse_image(&sparse, dest, addr, NULL))
Jassi Brar732bc7c2018-04-06 12:05:24 +0530424 return CMD_RET_FAILURE;
425 else
426 return CMD_RET_SUCCESS;
427}
428#endif
429
Alex Kiernanc232d142018-05-29 15:30:52 +0000430#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glass09140112020-05-10 11:40:03 -0600431static int do_mmc_write(struct cmd_tbl *cmdtp, int flag,
432 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200433{
434 struct mmc *mmc;
435 u32 blk, cnt, n;
436 void *addr;
437
438 if (argc != 4)
439 return CMD_RET_USAGE;
440
Simon Glass7e5f4602021-07-24 09:03:29 -0600441 addr = (void *)hextoul(argv[1], NULL);
442 blk = hextoul(argv[2], NULL);
443 cnt = hextoul(argv[3], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200444
Stephen Warren1ae24a52014-05-23 13:24:45 -0600445 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200446 if (!mmc)
447 return CMD_RET_FAILURE;
448
449 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
450 curr_device, blk, cnt);
451
452 if (mmc_getwp(mmc) == 1) {
453 printf("Error: card is write protected!\n");
454 return CMD_RET_FAILURE;
455 }
Simon Glassc40fdca2016-05-01 13:52:35 -0600456 n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200457 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
458
459 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
460}
Simon Glass09140112020-05-10 11:40:03 -0600461
462static int do_mmc_erase(struct cmd_tbl *cmdtp, int flag,
463 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200464{
465 struct mmc *mmc;
466 u32 blk, cnt, n;
467
468 if (argc != 3)
469 return CMD_RET_USAGE;
470
Simon Glass7e5f4602021-07-24 09:03:29 -0600471 blk = hextoul(argv[1], NULL);
472 cnt = hextoul(argv[2], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200473
Stephen Warren1ae24a52014-05-23 13:24:45 -0600474 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200475 if (!mmc)
476 return CMD_RET_FAILURE;
477
478 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
479 curr_device, blk, cnt);
480
481 if (mmc_getwp(mmc) == 1) {
482 printf("Error: card is write protected!\n");
483 return CMD_RET_FAILURE;
484 }
Simon Glassc40fdca2016-05-01 13:52:35 -0600485 n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200486 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
487
488 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
489}
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +0100490#endif
491
Simon Glass09140112020-05-10 11:40:03 -0600492static int do_mmc_rescan(struct cmd_tbl *cmdtp, int flag,
493 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200494{
495 struct mmc *mmc;
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530496 enum bus_mode speed_mode = MMC_MODES_END;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200497
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530498 if (argc == 1) {
499 mmc = init_mmc_device(curr_device, true);
500 } else if (argc == 2) {
501 speed_mode = (int)dectoul(argv[1], NULL);
502 mmc = __init_mmc_device(curr_device, true, speed_mode);
503 } else {
504 return CMD_RET_USAGE;
505 }
506
Stephen Warren941944e2014-05-23 13:24:46 -0600507 if (!mmc)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200508 return CMD_RET_FAILURE;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200509
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200510 return CMD_RET_SUCCESS;
511}
Simon Glass09140112020-05-10 11:40:03 -0600512
513static int do_mmc_part(struct cmd_tbl *cmdtp, int flag,
514 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200515{
Simon Glass4101f682016-02-29 15:25:34 -0700516 struct blk_desc *mmc_dev;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200517 struct mmc *mmc;
518
Stephen Warren1ae24a52014-05-23 13:24:45 -0600519 mmc = init_mmc_device(curr_device, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200520 if (!mmc)
521 return CMD_RET_FAILURE;
522
Simon Glass3c457f42016-05-01 11:36:15 -0600523 mmc_dev = blk_get_devnum_by_type(IF_TYPE_MMC, curr_device);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200524 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
Simon Glass3e8bd462016-02-29 15:25:48 -0700525 part_print(mmc_dev);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200526 return CMD_RET_SUCCESS;
527 }
528
529 puts("get mmc type error!\n");
530 return CMD_RET_FAILURE;
531}
Simon Glass09140112020-05-10 11:40:03 -0600532
533static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag,
534 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200535{
Stephen Warren60dc58f2014-05-23 12:48:10 -0600536 int dev, part = 0, ret;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200537 struct mmc *mmc;
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530538 enum bus_mode speed_mode = MMC_MODES_END;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200539
540 if (argc == 1) {
541 dev = curr_device;
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530542 mmc = init_mmc_device(dev, true);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200543 } else if (argc == 2) {
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530544 dev = (int)dectoul(argv[1], NULL);
545 mmc = init_mmc_device(dev, true);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200546 } else if (argc == 3) {
Simon Glass0b1284e2021-07-24 09:03:30 -0600547 dev = (int)dectoul(argv[1], NULL);
548 part = (int)dectoul(argv[2], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200549 if (part > PART_ACCESS_MASK) {
550 printf("#part_num shouldn't be larger than %d\n",
551 PART_ACCESS_MASK);
552 return CMD_RET_FAILURE;
553 }
Aswath Govindraju19f7a342021-08-13 23:04:41 +0530554 mmc = init_mmc_device(dev, true);
555 } else if (argc == 4) {
556 dev = (int)dectoul(argv[1], NULL);
557 part = (int)dectoul(argv[2], NULL);
558 if (part > PART_ACCESS_MASK) {
559 printf("#part_num shouldn't be larger than %d\n",
560 PART_ACCESS_MASK);
561 return CMD_RET_FAILURE;
562 }
563 speed_mode = (int)dectoul(argv[3], NULL);
564 mmc = __init_mmc_device(dev, true, speed_mode);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200565 } else {
566 return CMD_RET_USAGE;
567 }
568
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200569 if (!mmc)
570 return CMD_RET_FAILURE;
571
Simon Glass69f45cd2016-05-01 13:52:29 -0600572 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
Stephen Warren60dc58f2014-05-23 12:48:10 -0600573 printf("switch to partitions #%d, %s\n",
574 part, (!ret) ? "OK" : "ERROR");
575 if (ret)
576 return 1;
577
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200578 curr_device = dev;
579 if (mmc->part_config == MMCPART_NOAVAILABLE)
580 printf("mmc%d is current device\n", curr_device);
581 else
582 printf("mmc%d(part %d) is current device\n",
Simon Glassc40fdca2016-05-01 13:52:35 -0600583 curr_device, mmc_get_blk_desc(mmc)->hwpart);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200584
585 return CMD_RET_SUCCESS;
586}
Simon Glass09140112020-05-10 11:40:03 -0600587
588static int do_mmc_list(struct cmd_tbl *cmdtp, int flag,
589 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200590{
591 print_mmc_devices('\n');
592 return CMD_RET_SUCCESS;
593}
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100594
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +0100595#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Marek Vasutf702dc12021-09-15 11:43:13 +0200596static void parse_hwpart_user_enh_size(struct mmc *mmc,
597 struct mmc_hwpart_conf *pconf,
598 char *argv)
599{
600 int ret;
601
602 pconf->user.enh_size = 0;
603
604 if (!strcmp(argv, "-")) { /* The rest of eMMC */
605 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
606 ret = mmc_send_ext_csd(mmc, ext_csd);
607 if (ret)
608 return;
609 /* This value is in 512B block units */
610 pconf->user.enh_size =
611 ((ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 2] << 16) +
612 (ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT + 1] << 8) +
613 ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT]) * 1024 *
614 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
615 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
616 pconf->user.enh_size -= pconf->user.enh_start;
617 } else {
618 pconf->user.enh_size = dectoul(argv, NULL);
619 }
620}
621
622static int parse_hwpart_user(struct mmc *mmc, struct mmc_hwpart_conf *pconf,
Simon Glass09140112020-05-10 11:40:03 -0600623 int argc, char *const argv[])
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100624{
625 int i = 0;
626
627 memset(&pconf->user, 0, sizeof(pconf->user));
628
629 while (i < argc) {
630 if (!strcmp(argv[i], "enh")) {
631 if (i + 2 >= argc)
632 return -1;
633 pconf->user.enh_start =
Simon Glass0b1284e2021-07-24 09:03:30 -0600634 dectoul(argv[i + 1], NULL);
Marek Vasutf702dc12021-09-15 11:43:13 +0200635 parse_hwpart_user_enh_size(mmc, pconf, argv[i + 2]);
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100636 i += 3;
637 } else if (!strcmp(argv[i], "wrrel")) {
638 if (i + 1 >= argc)
639 return -1;
640 pconf->user.wr_rel_change = 1;
641 if (!strcmp(argv[i+1], "on"))
642 pconf->user.wr_rel_set = 1;
643 else if (!strcmp(argv[i+1], "off"))
644 pconf->user.wr_rel_set = 0;
645 else
646 return -1;
647 i += 2;
648 } else {
649 break;
650 }
651 }
652 return i;
653}
654
655static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
Simon Glass09140112020-05-10 11:40:03 -0600656 int argc, char *const argv[])
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100657{
658 int i;
659
660 memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
661
662 if (1 >= argc)
663 return -1;
Simon Glass0b1284e2021-07-24 09:03:30 -0600664 pconf->gp_part[pidx].size = dectoul(argv[0], NULL);
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100665
666 i = 1;
667 while (i < argc) {
668 if (!strcmp(argv[i], "enh")) {
669 pconf->gp_part[pidx].enhanced = 1;
670 i += 1;
671 } else if (!strcmp(argv[i], "wrrel")) {
672 if (i + 1 >= argc)
673 return -1;
674 pconf->gp_part[pidx].wr_rel_change = 1;
675 if (!strcmp(argv[i+1], "on"))
676 pconf->gp_part[pidx].wr_rel_set = 1;
677 else if (!strcmp(argv[i+1], "off"))
678 pconf->gp_part[pidx].wr_rel_set = 0;
679 else
680 return -1;
681 i += 2;
682 } else {
683 break;
684 }
685 }
686 return i;
687}
688
Simon Glass09140112020-05-10 11:40:03 -0600689static int do_mmc_hwpartition(struct cmd_tbl *cmdtp, int flag,
690 int argc, char *const argv[])
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100691{
692 struct mmc *mmc;
693 struct mmc_hwpart_conf pconf = { };
694 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100695 int i, r, pidx;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100696
697 mmc = init_mmc_device(curr_device, false);
698 if (!mmc)
699 return CMD_RET_FAILURE;
700
701 if (argc < 1)
702 return CMD_RET_USAGE;
703 i = 1;
704 while (i < argc) {
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100705 if (!strcmp(argv[i], "user")) {
706 i++;
Marek Vasutf702dc12021-09-15 11:43:13 +0200707 r = parse_hwpart_user(mmc, &pconf, argc - i, &argv[i]);
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100708 if (r < 0)
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100709 return CMD_RET_USAGE;
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100710 i += r;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100711 } else if (!strncmp(argv[i], "gp", 2) &&
712 strlen(argv[i]) == 3 &&
713 argv[i][2] >= '1' && argv[i][2] <= '4') {
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100714 pidx = argv[i][2] - '1';
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100715 i++;
716 r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
717 if (r < 0)
718 return CMD_RET_USAGE;
719 i += r;
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100720 } else if (!strcmp(argv[i], "check")) {
721 mode = MMC_HWPART_CONF_CHECK;
722 i++;
723 } else if (!strcmp(argv[i], "set")) {
724 mode = MMC_HWPART_CONF_SET;
725 i++;
726 } else if (!strcmp(argv[i], "complete")) {
727 mode = MMC_HWPART_CONF_COMPLETE;
728 i++;
729 } else {
730 return CMD_RET_USAGE;
731 }
732 }
733
734 puts("Partition configuration:\n");
735 if (pconf.user.enh_size) {
736 puts("\tUser Enhanced Start: ");
737 print_size(((u64)pconf.user.enh_start) << 9, "\n");
738 puts("\tUser Enhanced Size: ");
739 print_size(((u64)pconf.user.enh_size) << 9, "\n");
740 } else {
741 puts("\tNo enhanced user data area\n");
742 }
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100743 if (pconf.user.wr_rel_change)
744 printf("\tUser partition write reliability: %s\n",
745 pconf.user.wr_rel_set ? "on" : "off");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100746 for (pidx = 0; pidx < 4; pidx++) {
747 if (pconf.gp_part[pidx].size) {
748 printf("\tGP%i Capacity: ", pidx+1);
749 print_size(((u64)pconf.gp_part[pidx].size) << 9,
750 pconf.gp_part[pidx].enhanced ?
751 " ENH\n" : "\n");
752 } else {
753 printf("\tNo GP%i partition\n", pidx+1);
754 }
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100755 if (pconf.gp_part[pidx].wr_rel_change)
756 printf("\tGP%i write reliability: %s\n", pidx+1,
757 pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100758 }
759
760 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
761 if (mode == MMC_HWPART_CONF_COMPLETE)
762 puts("Partitioning successful, "
763 "power-cycle to make effective\n");
764 return CMD_RET_SUCCESS;
765 } else {
Diego Santa Cruz189f9632014-12-23 10:50:32 +0100766 puts("Failed!\n");
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100767 return CMD_RET_FAILURE;
768 }
769}
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +0100770#endif
Diego Santa Cruzc599f532014-12-23 10:50:30 +0100771
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200772#ifdef CONFIG_SUPPORT_EMMC_BOOT
Simon Glass09140112020-05-10 11:40:03 -0600773static int do_mmc_bootbus(struct cmd_tbl *cmdtp, int flag,
774 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200775{
776 int dev;
777 struct mmc *mmc;
778 u8 width, reset, mode;
779
780 if (argc != 5)
781 return CMD_RET_USAGE;
Simon Glass0b1284e2021-07-24 09:03:30 -0600782 dev = dectoul(argv[1], NULL);
783 width = dectoul(argv[2], NULL);
784 reset = dectoul(argv[3], NULL);
785 mode = dectoul(argv[4], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200786
Stephen Warren1ae24a52014-05-23 13:24:45 -0600787 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200788 if (!mmc)
789 return CMD_RET_FAILURE;
790
791 if (IS_SD(mmc)) {
792 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
793 return CMD_RET_FAILURE;
794 }
795
Jaehoon Chunge9978b12021-02-26 18:38:20 +0900796 /*
797 * BOOT_BUS_CONDITIONS[177]
798 * BOOT_MODE[4:3]
799 * 0x0 : Use SDR + Backward compatible timing in boot operation
800 * 0x1 : Use SDR + High Speed Timing in boot operation mode
801 * 0x2 : Use DDR in boot operation
802 * RESET_BOOT_BUS_CONDITIONS
803 * 0x0 : Reset bus width to x1, SDR, Backward compatible
804 * 0x1 : Retain BOOT_BUS_WIDTH and BOOT_MODE
805 * BOOT_BUS_WIDTH
806 * 0x0 : x1(sdr) or x4 (ddr) buswidth
807 * 0x1 : x4(sdr/ddr) buswith
808 * 0x2 : x8(sdr/ddr) buswith
809 *
810 */
811 if (width >= 0x3) {
812 printf("boot_bus_width %d is invalid\n", width);
813 return CMD_RET_FAILURE;
814 }
815
816 if (reset >= 0x2) {
817 printf("reset_boot_bus_width %d is invalid\n", reset);
818 return CMD_RET_FAILURE;
819 }
820
821 if (mode >= 0x3) {
822 printf("reset_boot_bus_width %d is invalid\n", mode);
823 return CMD_RET_FAILURE;
824 }
825
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200826 /* acknowledge to be sent during boot operation */
Jaehoon Chunge9978b12021-02-26 18:38:20 +0900827 if (mmc_set_boot_bus_width(mmc, width, reset, mode)) {
828 puts("BOOT_BUS_WIDTH is failed to change.\n");
829 return CMD_RET_FAILURE;
830 }
831
832 printf("Set to BOOT_BUS_WIDTH = 0x%x, RESET = 0x%x, BOOT_MODE = 0x%x\n",
833 width, reset, mode);
834 return CMD_RET_SUCCESS;
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200835}
Simon Glass09140112020-05-10 11:40:03 -0600836
837static int do_mmc_boot_resize(struct cmd_tbl *cmdtp, int flag,
838 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200839{
840 int dev;
841 struct mmc *mmc;
842 u32 bootsize, rpmbsize;
843
844 if (argc != 4)
845 return CMD_RET_USAGE;
Simon Glass0b1284e2021-07-24 09:03:30 -0600846 dev = dectoul(argv[1], NULL);
847 bootsize = dectoul(argv[2], NULL);
848 rpmbsize = dectoul(argv[3], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200849
Stephen Warren1ae24a52014-05-23 13:24:45 -0600850 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200851 if (!mmc)
852 return CMD_RET_FAILURE;
853
854 if (IS_SD(mmc)) {
Heinrich Schuchardt71a3e5c2020-03-29 19:26:57 +0000855 printf("It is not an eMMC device\n");
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200856 return CMD_RET_FAILURE;
857 }
858
859 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
860 printf("EMMC boot partition Size change Failed.\n");
861 return CMD_RET_FAILURE;
862 }
863
864 printf("EMMC boot partition Size %d MB\n", bootsize);
865 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
866 return CMD_RET_SUCCESS;
867}
Angelo Dureghellobdb60992017-08-01 14:27:10 +0200868
Reuben Dowle5c2beda2021-05-14 10:15:43 +1200869static int mmc_partconf_print(struct mmc *mmc, const char *varname)
Angelo Dureghellobdb60992017-08-01 14:27:10 +0200870{
871 u8 ack, access, part;
872
873 if (mmc->part_config == MMCPART_NOAVAILABLE) {
874 printf("No part_config info for ver. 0x%x\n", mmc->version);
875 return CMD_RET_FAILURE;
876 }
877
878 access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
879 ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
880 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
881
Reuben Dowle5c2beda2021-05-14 10:15:43 +1200882 if(varname)
883 env_set_hex(varname, part);
884
Angelo Dureghellobdb60992017-08-01 14:27:10 +0200885 printf("EXT_CSD[179], PARTITION_CONFIG:\n"
886 "BOOT_ACK: 0x%x\n"
887 "BOOT_PARTITION_ENABLE: 0x%x\n"
888 "PARTITION_ACCESS: 0x%x\n", ack, part, access);
889
890 return CMD_RET_SUCCESS;
891}
892
Simon Glass09140112020-05-10 11:40:03 -0600893static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag,
894 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200895{
896 int dev;
897 struct mmc *mmc;
898 u8 ack, part_num, access;
899
Reuben Dowle5c2beda2021-05-14 10:15:43 +1200900 if (argc != 2 && argc != 3 && argc != 5)
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200901 return CMD_RET_USAGE;
902
Simon Glass0b1284e2021-07-24 09:03:30 -0600903 dev = dectoul(argv[1], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200904
Stephen Warren1ae24a52014-05-23 13:24:45 -0600905 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200906 if (!mmc)
907 return CMD_RET_FAILURE;
908
909 if (IS_SD(mmc)) {
910 puts("PARTITION_CONFIG only exists on eMMC\n");
911 return CMD_RET_FAILURE;
912 }
913
Reuben Dowle5c2beda2021-05-14 10:15:43 +1200914 if (argc == 2 || argc == 3)
915 return mmc_partconf_print(mmc, argc == 3 ? argv[2] : NULL);
Angelo Dureghellobdb60992017-08-01 14:27:10 +0200916
Simon Glass0b1284e2021-07-24 09:03:30 -0600917 ack = dectoul(argv[2], NULL);
918 part_num = dectoul(argv[3], NULL);
919 access = dectoul(argv[4], NULL);
Angelo Dureghellobdb60992017-08-01 14:27:10 +0200920
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200921 /* acknowledge to be sent during boot operation */
922 return mmc_set_part_conf(mmc, ack, part_num, access);
923}
Simon Glass09140112020-05-10 11:40:03 -0600924
925static int do_mmc_rst_func(struct cmd_tbl *cmdtp, int flag,
926 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200927{
928 int dev;
929 struct mmc *mmc;
930 u8 enable;
931
932 /*
933 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
934 * The only valid values are 0x0, 0x1 and 0x2 and writing
935 * a value of 0x1 or 0x2 sets the value permanently.
936 */
937 if (argc != 3)
938 return CMD_RET_USAGE;
939
Simon Glass0b1284e2021-07-24 09:03:30 -0600940 dev = dectoul(argv[1], NULL);
941 enable = dectoul(argv[2], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200942
Peng Fan678e9312015-11-25 17:16:21 +0800943 if (enable > 2) {
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200944 puts("Invalid RST_n_ENABLE value\n");
945 return CMD_RET_USAGE;
946 }
947
Stephen Warren1ae24a52014-05-23 13:24:45 -0600948 mmc = init_mmc_device(dev, false);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200949 if (!mmc)
950 return CMD_RET_FAILURE;
951
952 if (IS_SD(mmc)) {
953 puts("RST_n_FUNCTION only exists on eMMC\n");
954 return CMD_RET_FAILURE;
955 }
956
957 return mmc_set_rst_n_function(mmc, enable);
958}
959#endif
Simon Glass09140112020-05-10 11:40:03 -0600960static int do_mmc_setdsr(struct cmd_tbl *cmdtp, int flag,
961 int argc, char *const argv[])
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200962{
963 struct mmc *mmc;
964 u32 val;
965 int ret;
966
967 if (argc != 2)
968 return CMD_RET_USAGE;
Simon Glass7e5f4602021-07-24 09:03:29 -0600969 val = hextoul(argv[1], NULL);
Pierre Aubert1fd93c62014-04-24 10:30:08 +0200970
971 mmc = find_mmc_device(curr_device);
972 if (!mmc) {
973 printf("no mmc device at slot %x\n", curr_device);
974 return CMD_RET_FAILURE;
975 }
976 ret = mmc_set_dsr(mmc, val);
977 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
978 if (!ret) {
979 mmc->has_init = 0;
980 if (mmc_init(mmc))
981 return CMD_RET_FAILURE;
982 else
983 return CMD_RET_SUCCESS;
984 }
985 return ret;
986}
987
Tomas Melincd3d4882016-11-25 11:01:03 +0200988#ifdef CONFIG_CMD_BKOPS_ENABLE
Simon Glass09140112020-05-10 11:40:03 -0600989static int do_mmc_bkops_enable(struct cmd_tbl *cmdtp, int flag,
990 int argc, char *const argv[])
Tomas Melincd3d4882016-11-25 11:01:03 +0200991{
992 int dev;
993 struct mmc *mmc;
994
995 if (argc != 2)
996 return CMD_RET_USAGE;
997
Simon Glass0b1284e2021-07-24 09:03:30 -0600998 dev = dectoul(argv[1], NULL);
Tomas Melincd3d4882016-11-25 11:01:03 +0200999
1000 mmc = init_mmc_device(dev, false);
1001 if (!mmc)
1002 return CMD_RET_FAILURE;
1003
1004 if (IS_SD(mmc)) {
1005 puts("BKOPS_EN only exists on eMMC\n");
1006 return CMD_RET_FAILURE;
1007 }
1008
1009 return mmc_set_bkops_enable(mmc);
1010}
1011#endif
1012
Simon Glass09140112020-05-10 11:40:03 -06001013static int do_mmc_boot_wp(struct cmd_tbl *cmdtp, int flag,
Heinrich Schuchardt0469d842020-03-30 07:24:19 +02001014 int argc, char * const argv[])
1015{
1016 int err;
1017 struct mmc *mmc;
1018
1019 mmc = init_mmc_device(curr_device, false);
1020 if (!mmc)
1021 return CMD_RET_FAILURE;
1022 if (IS_SD(mmc)) {
1023 printf("It is not an eMMC device\n");
1024 return CMD_RET_FAILURE;
1025 }
1026 err = mmc_boot_wp(mmc);
1027 if (err)
1028 return CMD_RET_FAILURE;
1029 printf("boot areas protected\n");
1030 return CMD_RET_SUCCESS;
1031}
1032
Simon Glass09140112020-05-10 11:40:03 -06001033static struct cmd_tbl cmd_mmc[] = {
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001034 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
1035 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
Heinrich Schuchardt0469d842020-03-30 07:24:19 +02001036 U_BOOT_CMD_MKENT(wp, 1, 0, do_mmc_boot_wp, "", ""),
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01001037#if CONFIG_IS_ENABLED(MMC_WRITE)
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001038 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
1039 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
Jean-Jacques Hiblote6fa5a52018-01-04 15:23:34 +01001040#endif
Alex Kiernanc232d142018-05-29 15:30:52 +00001041#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
1042 U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
1043#endif
Aswath Govindraju19f7a342021-08-13 23:04:41 +05301044 U_BOOT_CMD_MKENT(rescan, 2, 1, do_mmc_rescan, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001045 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
Aswath Govindraju19f7a342021-08-13 23:04:41 +05301046 U_BOOT_CMD_MKENT(dev, 4, 0, do_mmc_dev, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001047 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +01001048#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz189f9632014-12-23 10:50:32 +01001049 U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
Jean-Jacques Hiblotcf177892017-11-30 17:44:02 +01001050#endif
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001051#ifdef CONFIG_SUPPORT_EMMC_BOOT
1052 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehfa7b8852014-09-25 23:00:16 +08001053 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001054 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
1055 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
1056#endif
Alex Kiernan5a7b11e2018-05-08 04:43:31 +00001057#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001058 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
1059#endif
1060 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
Tomas Melincd3d4882016-11-25 11:01:03 +02001061#ifdef CONFIG_CMD_BKOPS_ENABLE
1062 U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
1063#endif
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001064};
Andy Fleming272cc702008-10-30 16:41:01 -05001065
Simon Glass09140112020-05-10 11:40:03 -06001066static int do_mmcops(struct cmd_tbl *cmdtp, int flag, int argc,
1067 char *const argv[])
Andy Fleming272cc702008-10-30 16:41:01 -05001068{
Simon Glass09140112020-05-10 11:40:03 -06001069 struct cmd_tbl *cp;
Lei Wen6be95cc2011-06-22 17:03:30 +00001070
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001071 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
1072
1073 /* Drop the mmc command */
1074 argc--;
1075 argv++;
1076
1077 if (cp == NULL || argc > cp->maxargs)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001078 return CMD_RET_USAGE;
Boris Brezillon80a48dd2018-12-03 22:54:20 +01001079 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001080 return CMD_RET_SUCCESS;
Andy Fleming272cc702008-10-30 16:41:01 -05001081
Lei Wenea6ebe22011-05-02 16:26:25 +00001082 if (curr_device < 0) {
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001083 if (get_mmc_num() > 0) {
Lei Wenea6ebe22011-05-02 16:26:25 +00001084 curr_device = 0;
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001085 } else {
Lei Wenea6ebe22011-05-02 16:26:25 +00001086 puts("No MMC device available\n");
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001087 return CMD_RET_FAILURE;
Lei Wenea6ebe22011-05-02 16:26:25 +00001088 }
1089 }
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001090 return cp->cmd(cmdtp, flag, argc, argv);
Andy Fleming272cc702008-10-30 16:41:01 -05001091}
1092
1093U_BOOT_CMD(
Diego Santa Cruz189f9632014-12-23 10:50:32 +01001094 mmc, 29, 1, do_mmcops,
Mike Frysinger852dbfd2009-03-23 22:27:34 -04001095 "MMC sub system",
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001096 "info - display info of the current MMC device\n"
1097 "mmc read addr blk# cnt\n"
Lei Wenea6ebe22011-05-02 16:26:25 +00001098 "mmc write addr blk# cnt\n"
Alex Kiernanc232d142018-05-29 15:30:52 +00001099#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar732bc7c2018-04-06 12:05:24 +05301100 "mmc swrite addr blk#\n"
1101#endif
Lei Wene6f99a52011-06-22 17:03:31 +00001102 "mmc erase blk# cnt\n"
Aswath Govindraju19f7a342021-08-13 23:04:41 +05301103 "mmc rescan [mode]\n"
Lei Wenea6ebe22011-05-02 16:26:25 +00001104 "mmc part - lists available partition on current mmc device\n"
Aswath Govindraju19f7a342021-08-13 23:04:41 +05301105 "mmc dev [dev] [part] [mode] - show or set current mmc device [partition] and set mode\n"
1106 " - the required speed mode is passed as the index from the following list\n"
1107 " [MMC_LEGACY, MMC_HS, SD_HS, MMC_HS_52, MMC_DDR_52, UHS_SDR12, UHS_SDR25,\n"
1108 " UHS_SDR50, UHS_DDR50, UHS_SDR104, MMC_HS_200, MMC_HS_400, MMC_HS_400_ES]\n"
Amar2a91c912013-04-27 11:43:00 +05301109 "mmc list - lists available devices\n"
Michal Simeka633a802020-06-05 11:45:12 +02001110 "mmc wp - power on write protect boot partitions\n"
Alex Kiernan84593672018-06-11 16:20:09 +00001111#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Jaehoon Chung8ae82c42021-02-26 16:07:18 +09001112 "mmc hwpartition <USER> <GP> <MODE> - does hardware partitioning\n"
Diego Santa Cruzc599f532014-12-23 10:50:30 +01001113 " arguments (sizes in 512-byte blocks):\n"
Jaehoon Chung8ae82c42021-02-26 16:07:18 +09001114 " USER - <user> <enh> <start> <cnt> <wrrel> <{on|off}>\n"
1115 " : sets user data area attributes\n"
1116 " GP - <{gp1|gp2|gp3|gp4}> <cnt> <enh> <wrrel> <{on|off}>\n"
1117 " : general purpose partition\n"
1118 " MODE - <{check|set|complete}>\n"
1119 " : mode, complete set partitioning completed\n"
Diego Santa Cruz189f9632014-12-23 10:50:32 +01001120 " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
1121 " Power cycling is required to initialize partitions after set to complete.\n"
Alex Kiernan84593672018-06-11 16:20:09 +00001122#endif
Amar2a91c912013-04-27 11:43:00 +05301123#ifdef CONFIG_SUPPORT_EMMC_BOOT
Jaehoon Chung1019b192020-12-16 07:24:50 +09001124 "mmc bootbus <dev> <boot_bus_width> <reset_boot_bus_width> <boot_mode>\n"
Tom Rini5a99b9d2014-02-05 10:24:22 -05001125 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rinif1fd9572014-02-05 10:24:20 -05001126 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
1127 " - Change sizes of boot and RPMB partitions of specified device\n"
Reuben Dowle5c2beda2021-05-14 10:15:43 +12001128 "mmc partconf <dev> [[varname] | [<boot_ack> <boot_partition> <partition_access>]]\n"
Angelo Dureghellobdb60992017-08-01 14:27:10 +02001129 " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
Reuben Dowle5c2beda2021-05-14 10:15:43 +12001130 " If showing the bits, optionally store the boot_partition field into varname\n"
Jaehoon Chung1019b192020-12-16 07:24:50 +09001131 "mmc rst-function <dev> <value>\n"
Tom Rini33ace362014-02-07 14:15:20 -05001132 " - Change the RST_n_FUNCTION field of the specified device\n"
1133 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behme3511b4e2009-02-18 19:59:39 +01001134#endif
Alex Kiernan5a7b11e2018-05-08 04:43:31 +00001135#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001136 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
1137 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
1138 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
1139 "mmc rpmb counter - read the value of the write counter\n"
1140#endif
1141 "mmc setdsr <value> - set DSR register value\n"
Tomas Melincd3d4882016-11-25 11:01:03 +02001142#ifdef CONFIG_CMD_BKOPS_ENABLE
1143 "mmc bkops-enable <dev> - enable background operations handshake on device\n"
1144 " WARNING: This is a write-once setting.\n"
1145#endif
Amar2a91c912013-04-27 11:43:00 +05301146 );
Pierre Aubert1fd93c62014-04-24 10:30:08 +02001147
1148/* Old command kept for compatibility. Same as 'mmc info' */
1149U_BOOT_CMD(
1150 mmcinfo, 1, 0, do_mmcinfo,
1151 "display MMC info",
1152 "- display info of the current MMC device"
1153);