blob: f1e30d0cf64b615c88a43c447a1e4e4f6a80e2d4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk7a428cc2003-06-15 22:40:42 +00002/*
3 * (C) Copyright 2003
4 * Kyle Harris, kharris@nexus-tech.net
wdenk7a428cc2003-06-15 22:40:42 +00005 */
6
7#include <common.h>
Simon Glass655306c2020-05-10 11:39:58 -06008#include <blk.h>
wdenk7a428cc2003-06-15 22:40:42 +00009#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070010#include <console.h>
Marek Vasutdfff8ed2020-06-20 14:28:25 +020011#include <memalign.h>
wdenk7a428cc2003-06-15 22:40:42 +000012#include <mmc.h>
Simon Glass655306c2020-05-10 11:39:58 -060013#include <part.h>
Jassi Brar814a9752018-04-06 12:05:24 +053014#include <sparse_format.h>
15#include <image-sparse.h>
wdenk7a428cc2003-06-15 22:40:42 +000016
Mike Frysinger0289fe62009-06-14 21:35:21 -040017static int curr_device = -1;
Andy Flemingad347bb2008-10-30 16:41:01 -050018
19static void print_mmcinfo(struct mmc *mmc)
20{
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010021 int i;
22
Pantelis Antoniou2c850462014-03-11 19:34:20 +020023 printf("Device: %s\n", mmc->cfg->name);
Andy Flemingad347bb2008-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 Hiblotf62caf92017-09-21 16:29:56 +020030 printf("Bus Speed: %d\n", mmc->clock);
Jean-Jacques Hiblot93c31d12017-11-30 17:43:54 +010031#if CONFIG_IS_ENABLED(MMC_VERBOSE)
Marek Vasutd1e63462019-03-18 04:49:21 +010032 printf("Mode: %s\n", mmc_mode_name(mmc->selected_mode));
Jean-Jacques Hiblot93c31d12017-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 Flemingad347bb2008-10-30 16:41:01 -050036 printf("Rd Block Len: %d\n", mmc->read_bl_len);
37
Pantelis Antonioua095bfd2015-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 Flemingad347bb2008-10-30 16:41:01 -050044
45 printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
Minkyu Kang316f81a2011-01-04 01:04:19 +000046 puts("Capacity: ");
47 print_size(mmc->capacity, "\n");
Andy Flemingad347bb2008-10-30 16:41:01 -050048
Andrew Gabbasov9fc2a412014-12-01 06:59:09 -060049 printf("Bus Width: %d-bit%s\n", mmc->bus_width,
50 mmc->ddr_mode ? " DDR" : "");
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010051
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +010052#if CONFIG_IS_ENABLED(MMC_WRITE)
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010053 puts("Erase Group Size: ");
54 print_size(((u64)mmc->erase_grp_size) << 9, "\n");
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +010055#endif
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010056
Diego Santa Cruz1f69e222014-12-23 10:50:19 +010057 if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010058 bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
Diego Santa Cruz2b45f582014-12-23 10:50:23 +010059 bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
Marek Vasutdfff8ed2020-06-20 14:28:25 +020060 ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
61 u8 wp;
Heinrich Schuchardt0c8ffff2020-03-30 07:24:18 +020062 int ret;
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010063
Jean-Jacques Hiblotba54ab82018-01-04 15:23:36 +010064#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010065 puts("HC WP Group Size: ");
66 print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
Jean-Jacques Hiblotba54ab82018-01-04 15:23:36 +010067#endif
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010068
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010069 puts("User Capacity: ");
Diego Santa Cruz37a50b92014-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 Cruz2b45f582014-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 Cruz99ca0282014-12-23 10:50:16 +010081 puts("Boot Capacity: ");
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010082 print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010083 puts("RPMB Capacity: ");
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010084 print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
Diego Santa Cruz173c79e2014-12-23 10:50:26 +010085
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010086 for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010087 bool is_enh = has_enh &&
88 (mmc->part_attr & EXT_CSD_ENH_GP(i));
Diego Santa Cruz99ca0282014-12-23 10:50:16 +010089 if (mmc->capacity_gp[i]) {
Diego Santa Cruzd8358432014-12-23 10:50:18 +010090 printf("GP%i Capacity: ", i+1);
Diego Santa Cruzc145f9e2014-12-23 10:50:17 +010091 print_size(mmc->capacity_gp[i],
Diego Santa Cruz37a50b92014-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 Cruz99ca0282014-12-23 10:50:16 +010097 }
98 }
Heinrich Schuchardt0c8ffff2020-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 Cruz99ca0282014-12-23 10:50:16 +0100121 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500122}
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530123
124static struct mmc *__init_mmc_device(int dev, bool force_init,
125 enum bus_mode speed_mode)
Pierre Aubertbcc302c2014-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 Nelsonbc181662016-03-27 12:00:14 -0700133
Marek Vasutcb5df5d2019-01-03 22:09:43 +0100134 if (!mmc_getcd(mmc))
135 force_init = true;
136
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600137 if (force_init)
138 mmc->has_init = 0;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530139
140 if (IS_ENABLED(CONFIG_MMC_SPEED_MODE_SET))
141 mmc->user_speed_mode = speed_mode;
142
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200143 if (mmc_init(mmc))
144 return NULL;
Marek Vasut829540f2019-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 Aubertbcc302c2014-04-24 10:30:08 +0200151 return mmc;
152}
Simon Glassed38aef2020-05-10 11:40:03 -0600153
Aswath Govindrajubb5b9fe2021-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 Glassed38aef2020-05-10 11:40:03 -0600159static int do_mmcinfo(struct cmd_tbl *cmdtp, int flag, int argc,
160 char *const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500161{
162 struct mmc *mmc;
Andy Flemingad347bb2008-10-30 16:41:01 -0500163
Lei Wend430d7c2011-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 Flemingad347bb2008-10-30 16:41:01 -0500172
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600173 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200174 if (!mmc)
175 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500176
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200177 print_mmcinfo(mmc);
178 return CMD_RET_SUCCESS;
179}
Andy Flemingad347bb2008-10-30 16:41:01 -0500180
Alex Kiernan60e0f612018-05-08 04:43:31 +0000181#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-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())
Lei Wend430d7c2011-05-02 16:26:25 +0000188 return 1;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200189
190 puts("Authentication key programming aborted\n");
191 return 0;
192}
Simon Glassed38aef2020-05-10 11:40:03 -0600193
194static int do_mmcrpmb_key(struct cmd_tbl *cmdtp, int flag,
195 int argc, char *const argv[])
Pierre Aubertbcc302c2014-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 Glass3ff49ec2021-07-24 09:03:29 -0600203 key_addr = (void *)hextoul(argv[1], NULL);
Pierre Aubertbcc302c2014-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;
Andy Flemingad347bb2008-10-30 16:41:01 -0500209 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200210 return CMD_RET_SUCCESS;
Andy Flemingad347bb2008-10-30 16:41:01 -0500211}
Simon Glassed38aef2020-05-10 11:40:03 -0600212
213static int do_mmcrpmb_read(struct cmd_tbl *cmdtp, int flag,
214 int argc, char *const argv[])
Pierre Aubertbcc302c2014-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);
Andy Flemingad347bb2008-10-30 16:41:01 -0500221
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200222 if (argc < 4)
223 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500224
Simon Glass3ff49ec2021-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 Aubertbcc302c2014-04-24 10:30:08 +0200228
229 if (argc == 5)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600230 key_addr = (void *)hextoul(argv[4], NULL);
Pierre Aubertbcc302c2014-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 Glassed38aef2020-05-10 11:40:03 -0600241
242static int do_mmcrpmb_write(struct cmd_tbl *cmdtp, int flag,
243 int argc, char *const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500244{
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200245 u16 blk, cnt;
246 void *addr;
247 int n;
248 void *key_addr;
249 struct mmc *mmc = find_mmc_device(curr_device);
Lei Wen64b2b1e2011-06-22 17:03:30 +0000250
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200251 if (argc != 5)
Simon Glassa06dfc72011-12-10 08:44:01 +0000252 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500253
Simon Glass3ff49ec2021-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 Aubertbcc302c2014-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 Glassed38aef2020-05-10 11:40:03 -0600268
269static int do_mmcrpmb_counter(struct cmd_tbl *cmdtp, int flag,
270 int argc, char *const argv[])
Pierre Aubertbcc302c2014-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 Glassed38aef2020-05-10 11:40:03 -0600281static struct cmd_tbl cmd_rpmb[] = {
Pierre Aubertbcc302c2014-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 Glassed38aef2020-05-10 11:40:03 -0600288static int do_mmcrpmb(struct cmd_tbl *cmdtp, int flag,
289 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200290{
Simon Glassed38aef2020-05-10 11:40:03 -0600291 struct cmd_tbl *cp;
Pierre Aubertbcc302c2014-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 Brezillonc71dfd12018-12-03 22:54:20 +0100304 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200305 return CMD_RET_SUCCESS;
306
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600307 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200308 if (!mmc)
309 return CMD_RET_FAILURE;
310
311 if (!(mmc->version & MMC_VERSION_MMC)) {
Heinrich Schuchardteeb191c2020-03-29 19:26:57 +0000312 printf("It is not an eMMC device\n");
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200313 return CMD_RET_FAILURE;
Lei Wend430d7c2011-05-02 16:26:25 +0000314 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200315 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 Yangcb5f1932017-06-08 09:20:03 +0800320#ifndef CONFIG_BLK
Marek Vasuta62cf492016-05-04 16:35:25 +0200321 original_part = mmc->block_dev.hwpart;
Kever Yangcb5f1932017-06-08 09:20:03 +0800322#else
323 original_part = mmc_get_blk_desc(mmc)->hwpart;
324#endif
Simon Glass11f2bb62016-05-01 13:52:29 -0600325 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, MMC_PART_RPMB) !=
326 0)
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700327 return CMD_RET_FAILURE;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200328 ret = cp->cmd(cmdtp, flag, argc, argv);
Andy Flemingad347bb2008-10-30 16:41:01 -0500329
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200330 /* Return to original partition */
Simon Glass11f2bb62016-05-01 13:52:29 -0600331 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, original_part) !=
332 0)
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700333 return CMD_RET_FAILURE;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200334 return ret;
335}
336#endif
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530337
Simon Glassed38aef2020-05-10 11:40:03 -0600338static int do_mmc_read(struct cmd_tbl *cmdtp, int flag,
339 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200340{
341 struct mmc *mmc;
342 u32 blk, cnt, n;
343 void *addr;
Stephen Warren18faca72013-04-01 11:50:28 +0000344
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200345 if (argc != 4)
346 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500347
Simon Glass3ff49ec2021-07-24 09:03:29 -0600348 addr = (void *)hextoul(argv[1], NULL);
349 blk = hextoul(argv[2], NULL);
350 cnt = hextoul(argv[3], NULL);
Lei Wen61dba932010-09-13 22:07:28 +0800351
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600352 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200353 if (!mmc)
354 return CMD_RET_FAILURE;
Lei Wen61dba932010-09-13 22:07:28 +0800355
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200356 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
357 curr_device, blk, cnt);
Stephen Warren18faca72013-04-01 11:50:28 +0000358
Simon Glasse5db1152016-05-01 13:52:35 -0600359 n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200360 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Lei Wend430d7c2011-05-02 16:26:25 +0000361
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200362 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
363}
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +0100364
Alex Kiernanc568bcb2018-05-29 15:30:52 +0000365#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar814a9752018-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 Glassed38aef2020-05-10 11:40:03 -0600380static int do_mmc_sparse_write(struct cmd_tbl *cmdtp, int flag,
381 int argc, char *const argv[])
Jassi Brar814a9752018-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 Glass3ff49ec2021-07-24 09:03:29 -0600393 addr = (void *)hextoul(argv[1], NULL);
394 blk = hextoul(argv[2], NULL);
Jassi Brar814a9752018-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 Kiernan27b69de2018-05-29 15:30:40 +0000423 if (write_sparse_image(&sparse, dest, addr, NULL))
Jassi Brar814a9752018-04-06 12:05:24 +0530424 return CMD_RET_FAILURE;
425 else
426 return CMD_RET_SUCCESS;
427}
428#endif
429
Alex Kiernanc568bcb2018-05-29 15:30:52 +0000430#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glassed38aef2020-05-10 11:40:03 -0600431static int do_mmc_write(struct cmd_tbl *cmdtp, int flag,
432 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200433{
434 struct mmc *mmc;
435 u32 blk, cnt, n;
436 void *addr;
Andy Flemingad347bb2008-10-30 16:41:01 -0500437
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200438 if (argc != 4)
439 return CMD_RET_USAGE;
Lei Wend430d7c2011-05-02 16:26:25 +0000440
Simon Glass3ff49ec2021-07-24 09:03:29 -0600441 addr = (void *)hextoul(argv[1], NULL);
442 blk = hextoul(argv[2], NULL);
443 cnt = hextoul(argv[3], NULL);
Lei Wend430d7c2011-05-02 16:26:25 +0000444
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600445 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200446 if (!mmc)
447 return CMD_RET_FAILURE;
Lei Wen31b99802011-05-02 16:26:26 +0000448
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200449 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
450 curr_device, blk, cnt);
Lei Wen31b99802011-05-02 16:26:26 +0000451
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200452 if (mmc_getwp(mmc) == 1) {
453 printf("Error: card is write protected!\n");
454 return CMD_RET_FAILURE;
455 }
Simon Glasse5db1152016-05-01 13:52:35 -0600456 n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200457 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Lei Wend430d7c2011-05-02 16:26:25 +0000458
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200459 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
460}
Simon Glassed38aef2020-05-10 11:40:03 -0600461
462static int do_mmc_erase(struct cmd_tbl *cmdtp, int flag,
463 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200464{
465 struct mmc *mmc;
466 u32 blk, cnt, n;
Tom Rinif8c6f792014-02-05 10:24:21 -0500467
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200468 if (argc != 3)
469 return CMD_RET_USAGE;
Tom Rinif8c6f792014-02-05 10:24:21 -0500470
Simon Glass3ff49ec2021-07-24 09:03:29 -0600471 blk = hextoul(argv[1], NULL);
472 cnt = hextoul(argv[2], NULL);
Amar3ccad532013-04-27 11:43:00 +0530473
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600474 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200475 if (!mmc)
476 return CMD_RET_FAILURE;
Tom Rinif8c6f792014-02-05 10:24:21 -0500477
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200478 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
479 curr_device, blk, cnt);
Tom Rini4cf854c2014-02-05 10:24:22 -0500480
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200481 if (mmc_getwp(mmc) == 1) {
482 printf("Error: card is write protected!\n");
483 return CMD_RET_FAILURE;
484 }
Simon Glasse5db1152016-05-01 13:52:35 -0600485 n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200486 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Tom Rini4cf854c2014-02-05 10:24:22 -0500487
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200488 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
489}
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +0100490#endif
491
Simon Glassed38aef2020-05-10 11:40:03 -0600492static int do_mmc_rescan(struct cmd_tbl *cmdtp, int flag,
493 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200494{
495 struct mmc *mmc;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530496 enum bus_mode speed_mode = MMC_MODES_END;
497
498 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 }
Tom Rini4cf854c2014-02-05 10:24:22 -0500506
Stephen Warren07ec4e72014-05-23 13:24:46 -0600507 if (!mmc)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200508 return CMD_RET_FAILURE;
Tom Rini4cf854c2014-02-05 10:24:22 -0500509
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200510 return CMD_RET_SUCCESS;
511}
Simon Glassed38aef2020-05-10 11:40:03 -0600512
513static int do_mmc_part(struct cmd_tbl *cmdtp, int flag,
514 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200515{
Simon Glasse3394752016-02-29 15:25:34 -0700516 struct blk_desc *mmc_dev;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200517 struct mmc *mmc;
Amar3ccad532013-04-27 11:43:00 +0530518
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600519 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200520 if (!mmc)
521 return CMD_RET_FAILURE;
Amar3ccad532013-04-27 11:43:00 +0530522
Simon Glassfa255112016-05-01 11:36:15 -0600523 mmc_dev = blk_get_devnum_by_type(IF_TYPE_MMC, curr_device);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200524 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
Simon Glassb89a8442016-02-29 15:25:48 -0700525 part_print(mmc_dev);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200526 return CMD_RET_SUCCESS;
527 }
Amar3ccad532013-04-27 11:43:00 +0530528
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200529 puts("get mmc type error!\n");
530 return CMD_RET_FAILURE;
531}
Simon Glassed38aef2020-05-10 11:40:03 -0600532
533static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag,
534 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200535{
Stephen Warren18549802014-05-23 12:48:10 -0600536 int dev, part = 0, ret;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200537 struct mmc *mmc;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530538 enum bus_mode speed_mode = MMC_MODES_END;
Tom Rini35a3ea12014-02-07 14:15:20 -0500539
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200540 if (argc == 1) {
541 dev = curr_device;
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530542 mmc = init_mmc_device(dev, true);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200543 } else if (argc == 2) {
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +0530544 dev = (int)dectoul(argv[1], NULL);
545 mmc = init_mmc_device(dev, true);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200546 } else if (argc == 3) {
Simon Glassff9b9032021-07-24 09:03:30 -0600547 dev = (int)dectoul(argv[1], NULL);
548 part = (int)dectoul(argv[2], NULL);
Pierre Aubertbcc302c2014-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;
Tom Rini35a3ea12014-02-07 14:15:20 -0500553 }
Aswath Govindrajubb5b9fe2021-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 Aubertbcc302c2014-04-24 10:30:08 +0200565 } else {
566 return CMD_RET_USAGE;
567 }
Tom Rini35a3ea12014-02-07 14:15:20 -0500568
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200569 if (!mmc)
570 return CMD_RET_FAILURE;
Tom Rini35a3ea12014-02-07 14:15:20 -0500571
Simon Glass11f2bb62016-05-01 13:52:29 -0600572 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
Stephen Warren18549802014-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 Aubertbcc302c2014-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 Glasse5db1152016-05-01 13:52:35 -0600583 curr_device, mmc_get_blk_desc(mmc)->hwpart);
Tom Rini35a3ea12014-02-07 14:15:20 -0500584
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200585 return CMD_RET_SUCCESS;
586}
Simon Glassed38aef2020-05-10 11:40:03 -0600587
588static int do_mmc_list(struct cmd_tbl *cmdtp, int flag,
589 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200590{
591 print_mmc_devices('\n');
592 return CMD_RET_SUCCESS;
593}
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100594
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +0100595#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100596static int parse_hwpart_user(struct mmc_hwpart_conf *pconf,
Simon Glassed38aef2020-05-10 11:40:03 -0600597 int argc, char *const argv[])
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100598{
599 int i = 0;
600
601 memset(&pconf->user, 0, sizeof(pconf->user));
602
603 while (i < argc) {
604 if (!strcmp(argv[i], "enh")) {
605 if (i + 2 >= argc)
606 return -1;
607 pconf->user.enh_start =
Simon Glassff9b9032021-07-24 09:03:30 -0600608 dectoul(argv[i + 1], NULL);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100609 pconf->user.enh_size =
Simon Glassff9b9032021-07-24 09:03:30 -0600610 dectoul(argv[i + 2], NULL);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100611 i += 3;
612 } else if (!strcmp(argv[i], "wrrel")) {
613 if (i + 1 >= argc)
614 return -1;
615 pconf->user.wr_rel_change = 1;
616 if (!strcmp(argv[i+1], "on"))
617 pconf->user.wr_rel_set = 1;
618 else if (!strcmp(argv[i+1], "off"))
619 pconf->user.wr_rel_set = 0;
620 else
621 return -1;
622 i += 2;
623 } else {
624 break;
625 }
626 }
627 return i;
628}
629
630static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
Simon Glassed38aef2020-05-10 11:40:03 -0600631 int argc, char *const argv[])
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100632{
633 int i;
634
635 memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
636
637 if (1 >= argc)
638 return -1;
Simon Glassff9b9032021-07-24 09:03:30 -0600639 pconf->gp_part[pidx].size = dectoul(argv[0], NULL);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100640
641 i = 1;
642 while (i < argc) {
643 if (!strcmp(argv[i], "enh")) {
644 pconf->gp_part[pidx].enhanced = 1;
645 i += 1;
646 } else if (!strcmp(argv[i], "wrrel")) {
647 if (i + 1 >= argc)
648 return -1;
649 pconf->gp_part[pidx].wr_rel_change = 1;
650 if (!strcmp(argv[i+1], "on"))
651 pconf->gp_part[pidx].wr_rel_set = 1;
652 else if (!strcmp(argv[i+1], "off"))
653 pconf->gp_part[pidx].wr_rel_set = 0;
654 else
655 return -1;
656 i += 2;
657 } else {
658 break;
659 }
660 }
661 return i;
662}
663
Simon Glassed38aef2020-05-10 11:40:03 -0600664static int do_mmc_hwpartition(struct cmd_tbl *cmdtp, int flag,
665 int argc, char *const argv[])
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100666{
667 struct mmc *mmc;
668 struct mmc_hwpart_conf pconf = { };
669 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100670 int i, r, pidx;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100671
672 mmc = init_mmc_device(curr_device, false);
673 if (!mmc)
674 return CMD_RET_FAILURE;
675
676 if (argc < 1)
677 return CMD_RET_USAGE;
678 i = 1;
679 while (i < argc) {
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100680 if (!strcmp(argv[i], "user")) {
681 i++;
682 r = parse_hwpart_user(&pconf, argc-i, &argv[i]);
683 if (r < 0)
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100684 return CMD_RET_USAGE;
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100685 i += r;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100686 } else if (!strncmp(argv[i], "gp", 2) &&
687 strlen(argv[i]) == 3 &&
688 argv[i][2] >= '1' && argv[i][2] <= '4') {
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100689 pidx = argv[i][2] - '1';
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100690 i++;
691 r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
692 if (r < 0)
693 return CMD_RET_USAGE;
694 i += r;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100695 } else if (!strcmp(argv[i], "check")) {
696 mode = MMC_HWPART_CONF_CHECK;
697 i++;
698 } else if (!strcmp(argv[i], "set")) {
699 mode = MMC_HWPART_CONF_SET;
700 i++;
701 } else if (!strcmp(argv[i], "complete")) {
702 mode = MMC_HWPART_CONF_COMPLETE;
703 i++;
704 } else {
705 return CMD_RET_USAGE;
706 }
707 }
708
709 puts("Partition configuration:\n");
710 if (pconf.user.enh_size) {
711 puts("\tUser Enhanced Start: ");
712 print_size(((u64)pconf.user.enh_start) << 9, "\n");
713 puts("\tUser Enhanced Size: ");
714 print_size(((u64)pconf.user.enh_size) << 9, "\n");
715 } else {
716 puts("\tNo enhanced user data area\n");
717 }
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100718 if (pconf.user.wr_rel_change)
719 printf("\tUser partition write reliability: %s\n",
720 pconf.user.wr_rel_set ? "on" : "off");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100721 for (pidx = 0; pidx < 4; pidx++) {
722 if (pconf.gp_part[pidx].size) {
723 printf("\tGP%i Capacity: ", pidx+1);
724 print_size(((u64)pconf.gp_part[pidx].size) << 9,
725 pconf.gp_part[pidx].enhanced ?
726 " ENH\n" : "\n");
727 } else {
728 printf("\tNo GP%i partition\n", pidx+1);
729 }
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100730 if (pconf.gp_part[pidx].wr_rel_change)
731 printf("\tGP%i write reliability: %s\n", pidx+1,
732 pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100733 }
734
735 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
736 if (mode == MMC_HWPART_CONF_COMPLETE)
737 puts("Partitioning successful, "
738 "power-cycle to make effective\n");
739 return CMD_RET_SUCCESS;
740 } else {
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100741 puts("Failed!\n");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100742 return CMD_RET_FAILURE;
743 }
744}
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +0100745#endif
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100746
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200747#ifdef CONFIG_SUPPORT_EMMC_BOOT
Simon Glassed38aef2020-05-10 11:40:03 -0600748static int do_mmc_bootbus(struct cmd_tbl *cmdtp, int flag,
749 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200750{
751 int dev;
752 struct mmc *mmc;
753 u8 width, reset, mode;
754
755 if (argc != 5)
756 return CMD_RET_USAGE;
Simon Glassff9b9032021-07-24 09:03:30 -0600757 dev = dectoul(argv[1], NULL);
758 width = dectoul(argv[2], NULL);
759 reset = dectoul(argv[3], NULL);
760 mode = dectoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200761
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600762 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200763 if (!mmc)
764 return CMD_RET_FAILURE;
765
766 if (IS_SD(mmc)) {
767 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
768 return CMD_RET_FAILURE;
Amar3ccad532013-04-27 11:43:00 +0530769 }
Markus Niebel03951412013-12-16 13:40:46 +0100770
Jaehoon Chung7483dd32021-02-26 18:38:20 +0900771 /*
772 * BOOT_BUS_CONDITIONS[177]
773 * BOOT_MODE[4:3]
774 * 0x0 : Use SDR + Backward compatible timing in boot operation
775 * 0x1 : Use SDR + High Speed Timing in boot operation mode
776 * 0x2 : Use DDR in boot operation
777 * RESET_BOOT_BUS_CONDITIONS
778 * 0x0 : Reset bus width to x1, SDR, Backward compatible
779 * 0x1 : Retain BOOT_BUS_WIDTH and BOOT_MODE
780 * BOOT_BUS_WIDTH
781 * 0x0 : x1(sdr) or x4 (ddr) buswidth
782 * 0x1 : x4(sdr/ddr) buswith
783 * 0x2 : x8(sdr/ddr) buswith
784 *
785 */
786 if (width >= 0x3) {
787 printf("boot_bus_width %d is invalid\n", width);
788 return CMD_RET_FAILURE;
789 }
790
791 if (reset >= 0x2) {
792 printf("reset_boot_bus_width %d is invalid\n", reset);
793 return CMD_RET_FAILURE;
794 }
795
796 if (mode >= 0x3) {
797 printf("reset_boot_bus_width %d is invalid\n", mode);
798 return CMD_RET_FAILURE;
799 }
800
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200801 /* acknowledge to be sent during boot operation */
Jaehoon Chung7483dd32021-02-26 18:38:20 +0900802 if (mmc_set_boot_bus_width(mmc, width, reset, mode)) {
803 puts("BOOT_BUS_WIDTH is failed to change.\n");
804 return CMD_RET_FAILURE;
805 }
806
807 printf("Set to BOOT_BUS_WIDTH = 0x%x, RESET = 0x%x, BOOT_MODE = 0x%x\n",
808 width, reset, mode);
809 return CMD_RET_SUCCESS;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200810}
Simon Glassed38aef2020-05-10 11:40:03 -0600811
812static int do_mmc_boot_resize(struct cmd_tbl *cmdtp, int flag,
813 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200814{
815 int dev;
816 struct mmc *mmc;
817 u32 bootsize, rpmbsize;
Markus Niebel03951412013-12-16 13:40:46 +0100818
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200819 if (argc != 4)
820 return CMD_RET_USAGE;
Simon Glassff9b9032021-07-24 09:03:30 -0600821 dev = dectoul(argv[1], NULL);
822 bootsize = dectoul(argv[2], NULL);
823 rpmbsize = dectoul(argv[3], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200824
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600825 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200826 if (!mmc)
827 return CMD_RET_FAILURE;
828
829 if (IS_SD(mmc)) {
Heinrich Schuchardteeb191c2020-03-29 19:26:57 +0000830 printf("It is not an eMMC device\n");
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200831 return CMD_RET_FAILURE;
Markus Niebel03951412013-12-16 13:40:46 +0100832 }
833
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200834 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
835 printf("EMMC boot partition Size change Failed.\n");
836 return CMD_RET_FAILURE;
837 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500838
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200839 printf("EMMC boot partition Size %d MB\n", bootsize);
840 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
841 return CMD_RET_SUCCESS;
842}
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200843
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200844static int mmc_partconf_print(struct mmc *mmc, const char *varname)
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200845{
846 u8 ack, access, part;
847
848 if (mmc->part_config == MMCPART_NOAVAILABLE) {
849 printf("No part_config info for ver. 0x%x\n", mmc->version);
850 return CMD_RET_FAILURE;
851 }
852
853 access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
854 ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
855 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
856
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200857 if(varname)
858 env_set_hex(varname, part);
859
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200860 printf("EXT_CSD[179], PARTITION_CONFIG:\n"
861 "BOOT_ACK: 0x%x\n"
862 "BOOT_PARTITION_ENABLE: 0x%x\n"
863 "PARTITION_ACCESS: 0x%x\n", ack, part, access);
864
865 return CMD_RET_SUCCESS;
866}
867
Simon Glassed38aef2020-05-10 11:40:03 -0600868static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag,
869 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200870{
871 int dev;
872 struct mmc *mmc;
873 u8 ack, part_num, access;
Lei Wenea526762011-06-22 17:03:31 +0000874
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200875 if (argc != 2 && argc != 3 && argc != 5)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200876 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500877
Simon Glassff9b9032021-07-24 09:03:30 -0600878 dev = dectoul(argv[1], NULL);
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530879
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600880 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200881 if (!mmc)
882 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500883
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200884 if (IS_SD(mmc)) {
885 puts("PARTITION_CONFIG only exists on eMMC\n");
886 return CMD_RET_FAILURE;
887 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500888
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200889 if (argc == 2 || argc == 3)
890 return mmc_partconf_print(mmc, argc == 3 ? argv[2] : NULL);
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200891
Simon Glassff9b9032021-07-24 09:03:30 -0600892 ack = dectoul(argv[2], NULL);
893 part_num = dectoul(argv[3], NULL);
894 access = dectoul(argv[4], NULL);
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200895
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200896 /* acknowledge to be sent during boot operation */
897 return mmc_set_part_conf(mmc, ack, part_num, access);
898}
Simon Glassed38aef2020-05-10 11:40:03 -0600899
900static int do_mmc_rst_func(struct cmd_tbl *cmdtp, int flag,
901 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200902{
903 int dev;
904 struct mmc *mmc;
905 u8 enable;
Nikita Kiryanov020f2612012-12-03 02:19:46 +0000906
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200907 /*
908 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
909 * The only valid values are 0x0, 0x1 and 0x2 and writing
910 * a value of 0x1 or 0x2 sets the value permanently.
911 */
912 if (argc != 3)
913 return CMD_RET_USAGE;
914
Simon Glassff9b9032021-07-24 09:03:30 -0600915 dev = dectoul(argv[1], NULL);
916 enable = dectoul(argv[2], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200917
Peng Fanb1e455a2015-11-25 17:16:21 +0800918 if (enable > 2) {
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200919 puts("Invalid RST_n_ENABLE value\n");
920 return CMD_RET_USAGE;
921 }
922
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600923 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200924 if (!mmc)
925 return CMD_RET_FAILURE;
926
927 if (IS_SD(mmc)) {
928 puts("RST_n_FUNCTION only exists on eMMC\n");
929 return CMD_RET_FAILURE;
930 }
931
932 return mmc_set_rst_n_function(mmc, enable);
933}
934#endif
Simon Glassed38aef2020-05-10 11:40:03 -0600935static int do_mmc_setdsr(struct cmd_tbl *cmdtp, int flag,
936 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200937{
938 struct mmc *mmc;
939 u32 val;
940 int ret;
Andy Flemingad347bb2008-10-30 16:41:01 -0500941
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200942 if (argc != 2)
943 return CMD_RET_USAGE;
Simon Glass3ff49ec2021-07-24 09:03:29 -0600944 val = hextoul(argv[1], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200945
946 mmc = find_mmc_device(curr_device);
947 if (!mmc) {
948 printf("no mmc device at slot %x\n", curr_device);
949 return CMD_RET_FAILURE;
950 }
951 ret = mmc_set_dsr(mmc, val);
952 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
953 if (!ret) {
954 mmc->has_init = 0;
955 if (mmc_init(mmc))
956 return CMD_RET_FAILURE;
957 else
958 return CMD_RET_SUCCESS;
Andy Flemingad347bb2008-10-30 16:41:01 -0500959 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200960 return ret;
961}
Lei Wend430d7c2011-05-02 16:26:25 +0000962
Tomas Melinc17dae52016-11-25 11:01:03 +0200963#ifdef CONFIG_CMD_BKOPS_ENABLE
Simon Glassed38aef2020-05-10 11:40:03 -0600964static int do_mmc_bkops_enable(struct cmd_tbl *cmdtp, int flag,
965 int argc, char *const argv[])
Tomas Melinc17dae52016-11-25 11:01:03 +0200966{
967 int dev;
968 struct mmc *mmc;
969
970 if (argc != 2)
971 return CMD_RET_USAGE;
972
Simon Glassff9b9032021-07-24 09:03:30 -0600973 dev = dectoul(argv[1], NULL);
Tomas Melinc17dae52016-11-25 11:01:03 +0200974
975 mmc = init_mmc_device(dev, false);
976 if (!mmc)
977 return CMD_RET_FAILURE;
978
979 if (IS_SD(mmc)) {
980 puts("BKOPS_EN only exists on eMMC\n");
981 return CMD_RET_FAILURE;
982 }
983
984 return mmc_set_bkops_enable(mmc);
985}
986#endif
987
Simon Glassed38aef2020-05-10 11:40:03 -0600988static int do_mmc_boot_wp(struct cmd_tbl *cmdtp, int flag,
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +0200989 int argc, char * const argv[])
990{
991 int err;
992 struct mmc *mmc;
993
994 mmc = init_mmc_device(curr_device, false);
995 if (!mmc)
996 return CMD_RET_FAILURE;
997 if (IS_SD(mmc)) {
998 printf("It is not an eMMC device\n");
999 return CMD_RET_FAILURE;
1000 }
1001 err = mmc_boot_wp(mmc);
1002 if (err)
1003 return CMD_RET_FAILURE;
1004 printf("boot areas protected\n");
1005 return CMD_RET_SUCCESS;
1006}
1007
Simon Glassed38aef2020-05-10 11:40:03 -06001008static struct cmd_tbl cmd_mmc[] = {
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001009 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
1010 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +02001011 U_BOOT_CMD_MKENT(wp, 1, 0, do_mmc_boot_wp, "", ""),
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01001012#if CONFIG_IS_ENABLED(MMC_WRITE)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001013 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
1014 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +01001015#endif
Alex Kiernanc568bcb2018-05-29 15:30:52 +00001016#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
1017 U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
1018#endif
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301019 U_BOOT_CMD_MKENT(rescan, 2, 1, do_mmc_rescan, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001020 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301021 U_BOOT_CMD_MKENT(dev, 4, 0, do_mmc_dev, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001022 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +01001023#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001024 U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +01001025#endif
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001026#ifdef CONFIG_SUPPORT_EMMC_BOOT
1027 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehd49c4aa2014-09-25 23:00:16 +08001028 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001029 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
1030 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
1031#endif
Alex Kiernan60e0f612018-05-08 04:43:31 +00001032#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001033 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
1034#endif
1035 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
Tomas Melinc17dae52016-11-25 11:01:03 +02001036#ifdef CONFIG_CMD_BKOPS_ENABLE
1037 U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
1038#endif
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001039};
1040
Simon Glassed38aef2020-05-10 11:40:03 -06001041static int do_mmcops(struct cmd_tbl *cmdtp, int flag, int argc,
1042 char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001043{
Simon Glassed38aef2020-05-10 11:40:03 -06001044 struct cmd_tbl *cp;
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001045
1046 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
1047
1048 /* Drop the mmc command */
1049 argc--;
1050 argv++;
1051
1052 if (cp == NULL || argc > cp->maxargs)
1053 return CMD_RET_USAGE;
Boris Brezillonc71dfd12018-12-03 22:54:20 +01001054 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001055 return CMD_RET_SUCCESS;
1056
1057 if (curr_device < 0) {
1058 if (get_mmc_num() > 0) {
1059 curr_device = 0;
1060 } else {
1061 puts("No MMC device available\n");
1062 return CMD_RET_FAILURE;
1063 }
1064 }
1065 return cp->cmd(cmdtp, flag, argc, argv);
Andy Flemingad347bb2008-10-30 16:41:01 -05001066}
1067
1068U_BOOT_CMD(
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001069 mmc, 29, 1, do_mmcops,
Mike Frysinger27e1e622009-03-23 22:27:34 -04001070 "MMC sub system",
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001071 "info - display info of the current MMC device\n"
1072 "mmc read addr blk# cnt\n"
Lei Wend430d7c2011-05-02 16:26:25 +00001073 "mmc write addr blk# cnt\n"
Alex Kiernanc568bcb2018-05-29 15:30:52 +00001074#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar814a9752018-04-06 12:05:24 +05301075 "mmc swrite addr blk#\n"
1076#endif
Lei Wenea526762011-06-22 17:03:31 +00001077 "mmc erase blk# cnt\n"
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301078 "mmc rescan [mode]\n"
Lei Wend430d7c2011-05-02 16:26:25 +00001079 "mmc part - lists available partition on current mmc device\n"
Aswath Govindrajubb5b9fe2021-08-13 23:04:41 +05301080 "mmc dev [dev] [part] [mode] - show or set current mmc device [partition] and set mode\n"
1081 " - the required speed mode is passed as the index from the following list\n"
1082 " [MMC_LEGACY, MMC_HS, SD_HS, MMC_HS_52, MMC_DDR_52, UHS_SDR12, UHS_SDR25,\n"
1083 " UHS_SDR50, UHS_DDR50, UHS_SDR104, MMC_HS_200, MMC_HS_400, MMC_HS_400_ES]\n"
Amar3ccad532013-04-27 11:43:00 +05301084 "mmc list - lists available devices\n"
Michal Simek21f753c2020-06-05 11:45:12 +02001085 "mmc wp - power on write protect boot partitions\n"
Alex Kiernan72d80462018-06-11 16:20:09 +00001086#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Jaehoon Chung94a47572021-02-26 16:07:18 +09001087 "mmc hwpartition <USER> <GP> <MODE> - does hardware partitioning\n"
Diego Santa Cruz420fc162014-12-23 10:50:30 +01001088 " arguments (sizes in 512-byte blocks):\n"
Jaehoon Chung94a47572021-02-26 16:07:18 +09001089 " USER - <user> <enh> <start> <cnt> <wrrel> <{on|off}>\n"
1090 " : sets user data area attributes\n"
1091 " GP - <{gp1|gp2|gp3|gp4}> <cnt> <enh> <wrrel> <{on|off}>\n"
1092 " : general purpose partition\n"
1093 " MODE - <{check|set|complete}>\n"
1094 " : mode, complete set partitioning completed\n"
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001095 " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
1096 " Power cycling is required to initialize partitions after set to complete.\n"
Alex Kiernan72d80462018-06-11 16:20:09 +00001097#endif
Amar3ccad532013-04-27 11:43:00 +05301098#ifdef CONFIG_SUPPORT_EMMC_BOOT
Jaehoon Chung8460b912020-12-16 07:24:50 +09001099 "mmc bootbus <dev> <boot_bus_width> <reset_boot_bus_width> <boot_mode>\n"
Tom Rini4cf854c2014-02-05 10:24:22 -05001100 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rini79919462014-02-05 10:24:20 -05001101 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
1102 " - Change sizes of boot and RPMB partitions of specified device\n"
Reuben Dowle55a81ad2021-05-14 10:15:43 +12001103 "mmc partconf <dev> [[varname] | [<boot_ack> <boot_partition> <partition_access>]]\n"
Angelo Dureghellof54f7532017-08-01 14:27:10 +02001104 " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
Reuben Dowle55a81ad2021-05-14 10:15:43 +12001105 " If showing the bits, optionally store the boot_partition field into varname\n"
Jaehoon Chung8460b912020-12-16 07:24:50 +09001106 "mmc rst-function <dev> <value>\n"
Tom Rini35a3ea12014-02-07 14:15:20 -05001107 " - Change the RST_n_FUNCTION field of the specified device\n"
1108 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behmedf8852a2009-02-18 19:59:39 +01001109#endif
Alex Kiernan60e0f612018-05-08 04:43:31 +00001110#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001111 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
1112 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
1113 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
1114 "mmc rpmb counter - read the value of the write counter\n"
1115#endif
1116 "mmc setdsr <value> - set DSR register value\n"
Tomas Melinc17dae52016-11-25 11:01:03 +02001117#ifdef CONFIG_CMD_BKOPS_ENABLE
1118 "mmc bkops-enable <dev> - enable background operations handshake on device\n"
1119 " WARNING: This is a write-once setting.\n"
1120#endif
Amar3ccad532013-04-27 11:43:00 +05301121 );
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001122
1123/* Old command kept for compatibility. Same as 'mmc info' */
1124U_BOOT_CMD(
1125 mmcinfo, 1, 0, do_mmcinfo,
1126 "display MMC info",
1127 "- display info of the current MMC device"
1128);