blob: c67ad7624227cb11bfe313660f49af77d11acaf1 [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}
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600123static struct mmc *init_mmc_device(int dev, bool force_init)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200124{
125 struct mmc *mmc;
126 mmc = find_mmc_device(dev);
127 if (!mmc) {
128 printf("no mmc device at slot %x\n", dev);
129 return NULL;
130 }
Eric Nelsonbc181662016-03-27 12:00:14 -0700131
Marek Vasutcb5df5d2019-01-03 22:09:43 +0100132 if (!mmc_getcd(mmc))
133 force_init = true;
134
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600135 if (force_init)
136 mmc->has_init = 0;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200137 if (mmc_init(mmc))
138 return NULL;
Marek Vasut829540f2019-01-03 22:09:44 +0100139
140#ifdef CONFIG_BLOCK_CACHE
141 struct blk_desc *bd = mmc_get_blk_desc(mmc);
142 blkcache_invalidate(bd->if_type, bd->devnum);
143#endif
144
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200145 return mmc;
146}
Simon Glassed38aef2020-05-10 11:40:03 -0600147
148static int do_mmcinfo(struct cmd_tbl *cmdtp, int flag, int argc,
149 char *const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500150{
151 struct mmc *mmc;
Andy Flemingad347bb2008-10-30 16:41:01 -0500152
Lei Wend430d7c2011-05-02 16:26:25 +0000153 if (curr_device < 0) {
154 if (get_mmc_num() > 0)
155 curr_device = 0;
156 else {
157 puts("No MMC device available\n");
158 return 1;
159 }
160 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500161
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600162 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200163 if (!mmc)
164 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500165
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200166 print_mmcinfo(mmc);
167 return CMD_RET_SUCCESS;
168}
Andy Flemingad347bb2008-10-30 16:41:01 -0500169
Alex Kiernan60e0f612018-05-08 04:43:31 +0000170#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200171static int confirm_key_prog(void)
172{
173 puts("Warning: Programming authentication key can be done only once !\n"
174 " Use this command only if you are sure of what you are doing,\n"
175 "Really perform the key programming? <y/N> ");
176 if (confirm_yesno())
Lei Wend430d7c2011-05-02 16:26:25 +0000177 return 1;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200178
179 puts("Authentication key programming aborted\n");
180 return 0;
181}
Simon Glassed38aef2020-05-10 11:40:03 -0600182
183static int do_mmcrpmb_key(struct cmd_tbl *cmdtp, int flag,
184 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200185{
186 void *key_addr;
187 struct mmc *mmc = find_mmc_device(curr_device);
188
189 if (argc != 2)
190 return CMD_RET_USAGE;
191
Simon Glass3ff49ec2021-07-24 09:03:29 -0600192 key_addr = (void *)hextoul(argv[1], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200193 if (!confirm_key_prog())
194 return CMD_RET_FAILURE;
195 if (mmc_rpmb_set_key(mmc, key_addr)) {
196 printf("ERROR - Key already programmed ?\n");
197 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500198 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200199 return CMD_RET_SUCCESS;
Andy Flemingad347bb2008-10-30 16:41:01 -0500200}
Simon Glassed38aef2020-05-10 11:40:03 -0600201
202static int do_mmcrpmb_read(struct cmd_tbl *cmdtp, int flag,
203 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200204{
205 u16 blk, cnt;
206 void *addr;
207 int n;
208 void *key_addr = NULL;
209 struct mmc *mmc = find_mmc_device(curr_device);
Andy Flemingad347bb2008-10-30 16:41:01 -0500210
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200211 if (argc < 4)
212 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500213
Simon Glass3ff49ec2021-07-24 09:03:29 -0600214 addr = (void *)hextoul(argv[1], NULL);
215 blk = hextoul(argv[2], NULL);
216 cnt = hextoul(argv[3], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200217
218 if (argc == 5)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600219 key_addr = (void *)hextoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200220
221 printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
222 curr_device, blk, cnt);
223 n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
224
225 printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
226 if (n != cnt)
227 return CMD_RET_FAILURE;
228 return CMD_RET_SUCCESS;
229}
Simon Glassed38aef2020-05-10 11:40:03 -0600230
231static int do_mmcrpmb_write(struct cmd_tbl *cmdtp, int flag,
232 int argc, char *const argv[])
Andy Flemingad347bb2008-10-30 16:41:01 -0500233{
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200234 u16 blk, cnt;
235 void *addr;
236 int n;
237 void *key_addr;
238 struct mmc *mmc = find_mmc_device(curr_device);
Lei Wen64b2b1e2011-06-22 17:03:30 +0000239
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200240 if (argc != 5)
Simon Glassa06dfc72011-12-10 08:44:01 +0000241 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500242
Simon Glass3ff49ec2021-07-24 09:03:29 -0600243 addr = (void *)hextoul(argv[1], NULL);
244 blk = hextoul(argv[2], NULL);
245 cnt = hextoul(argv[3], NULL);
246 key_addr = (void *)hextoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200247
248 printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
249 curr_device, blk, cnt);
250 n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
251
252 printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
253 if (n != cnt)
254 return CMD_RET_FAILURE;
255 return CMD_RET_SUCCESS;
256}
Simon Glassed38aef2020-05-10 11:40:03 -0600257
258static int do_mmcrpmb_counter(struct cmd_tbl *cmdtp, int flag,
259 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200260{
261 unsigned long counter;
262 struct mmc *mmc = find_mmc_device(curr_device);
263
264 if (mmc_rpmb_get_counter(mmc, &counter))
265 return CMD_RET_FAILURE;
266 printf("RPMB Write counter= %lx\n", counter);
267 return CMD_RET_SUCCESS;
268}
269
Simon Glassed38aef2020-05-10 11:40:03 -0600270static struct cmd_tbl cmd_rpmb[] = {
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200271 U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
272 U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
273 U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
274 U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
275};
276
Simon Glassed38aef2020-05-10 11:40:03 -0600277static int do_mmcrpmb(struct cmd_tbl *cmdtp, int flag,
278 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200279{
Simon Glassed38aef2020-05-10 11:40:03 -0600280 struct cmd_tbl *cp;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200281 struct mmc *mmc;
282 char original_part;
283 int ret;
284
285 cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
286
287 /* Drop the rpmb subcommand */
288 argc--;
289 argv++;
290
291 if (cp == NULL || argc > cp->maxargs)
292 return CMD_RET_USAGE;
Boris Brezillonc71dfd12018-12-03 22:54:20 +0100293 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200294 return CMD_RET_SUCCESS;
295
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600296 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200297 if (!mmc)
298 return CMD_RET_FAILURE;
299
300 if (!(mmc->version & MMC_VERSION_MMC)) {
Heinrich Schuchardteeb191c2020-03-29 19:26:57 +0000301 printf("It is not an eMMC device\n");
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200302 return CMD_RET_FAILURE;
Lei Wend430d7c2011-05-02 16:26:25 +0000303 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200304 if (mmc->version < MMC_VERSION_4_41) {
305 printf("RPMB not supported before version 4.41\n");
306 return CMD_RET_FAILURE;
307 }
308 /* Switch to the RPMB partition */
Kever Yangcb5f1932017-06-08 09:20:03 +0800309#ifndef CONFIG_BLK
Marek Vasuta62cf492016-05-04 16:35:25 +0200310 original_part = mmc->block_dev.hwpart;
Kever Yangcb5f1932017-06-08 09:20:03 +0800311#else
312 original_part = mmc_get_blk_desc(mmc)->hwpart;
313#endif
Simon Glass11f2bb62016-05-01 13:52:29 -0600314 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, MMC_PART_RPMB) !=
315 0)
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700316 return CMD_RET_FAILURE;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200317 ret = cp->cmd(cmdtp, flag, argc, argv);
Andy Flemingad347bb2008-10-30 16:41:01 -0500318
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200319 /* Return to original partition */
Simon Glass11f2bb62016-05-01 13:52:29 -0600320 if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, original_part) !=
321 0)
Stephen Warren1e0f92a2015-12-07 11:38:49 -0700322 return CMD_RET_FAILURE;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200323 return ret;
324}
325#endif
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530326
Simon Glassed38aef2020-05-10 11:40:03 -0600327static int do_mmc_read(struct cmd_tbl *cmdtp, int flag,
328 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200329{
330 struct mmc *mmc;
331 u32 blk, cnt, n;
332 void *addr;
Stephen Warren18faca72013-04-01 11:50:28 +0000333
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200334 if (argc != 4)
335 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500336
Simon Glass3ff49ec2021-07-24 09:03:29 -0600337 addr = (void *)hextoul(argv[1], NULL);
338 blk = hextoul(argv[2], NULL);
339 cnt = hextoul(argv[3], NULL);
Lei Wen61dba932010-09-13 22:07:28 +0800340
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600341 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200342 if (!mmc)
343 return CMD_RET_FAILURE;
Lei Wen61dba932010-09-13 22:07:28 +0800344
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200345 printf("\nMMC read: dev # %d, block # %d, count %d ... ",
346 curr_device, blk, cnt);
Stephen Warren18faca72013-04-01 11:50:28 +0000347
Simon Glasse5db1152016-05-01 13:52:35 -0600348 n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200349 printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Lei Wend430d7c2011-05-02 16:26:25 +0000350
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200351 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
352}
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +0100353
Alex Kiernanc568bcb2018-05-29 15:30:52 +0000354#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar814a9752018-04-06 12:05:24 +0530355static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk,
356 lbaint_t blkcnt, const void *buffer)
357{
358 struct blk_desc *dev_desc = info->priv;
359
360 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
361}
362
363static lbaint_t mmc_sparse_reserve(struct sparse_storage *info,
364 lbaint_t blk, lbaint_t blkcnt)
365{
366 return blkcnt;
367}
368
Simon Glassed38aef2020-05-10 11:40:03 -0600369static int do_mmc_sparse_write(struct cmd_tbl *cmdtp, int flag,
370 int argc, char *const argv[])
Jassi Brar814a9752018-04-06 12:05:24 +0530371{
372 struct sparse_storage sparse;
373 struct blk_desc *dev_desc;
374 struct mmc *mmc;
375 char dest[11];
376 void *addr;
377 u32 blk;
378
379 if (argc != 3)
380 return CMD_RET_USAGE;
381
Simon Glass3ff49ec2021-07-24 09:03:29 -0600382 addr = (void *)hextoul(argv[1], NULL);
383 blk = hextoul(argv[2], NULL);
Jassi Brar814a9752018-04-06 12:05:24 +0530384
385 if (!is_sparse_image(addr)) {
386 printf("Not a sparse image\n");
387 return CMD_RET_FAILURE;
388 }
389
390 mmc = init_mmc_device(curr_device, false);
391 if (!mmc)
392 return CMD_RET_FAILURE;
393
394 printf("\nMMC Sparse write: dev # %d, block # %d ... ",
395 curr_device, blk);
396
397 if (mmc_getwp(mmc) == 1) {
398 printf("Error: card is write protected!\n");
399 return CMD_RET_FAILURE;
400 }
401
402 dev_desc = mmc_get_blk_desc(mmc);
403 sparse.priv = dev_desc;
404 sparse.blksz = 512;
405 sparse.start = blk;
406 sparse.size = dev_desc->lba - blk;
407 sparse.write = mmc_sparse_write;
408 sparse.reserve = mmc_sparse_reserve;
409 sparse.mssg = NULL;
410 sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz);
411
Alex Kiernan27b69de2018-05-29 15:30:40 +0000412 if (write_sparse_image(&sparse, dest, addr, NULL))
Jassi Brar814a9752018-04-06 12:05:24 +0530413 return CMD_RET_FAILURE;
414 else
415 return CMD_RET_SUCCESS;
416}
417#endif
418
Alex Kiernanc568bcb2018-05-29 15:30:52 +0000419#if CONFIG_IS_ENABLED(MMC_WRITE)
Simon Glassed38aef2020-05-10 11:40:03 -0600420static int do_mmc_write(struct cmd_tbl *cmdtp, int flag,
421 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200422{
423 struct mmc *mmc;
424 u32 blk, cnt, n;
425 void *addr;
Andy Flemingad347bb2008-10-30 16:41:01 -0500426
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200427 if (argc != 4)
428 return CMD_RET_USAGE;
Lei Wend430d7c2011-05-02 16:26:25 +0000429
Simon Glass3ff49ec2021-07-24 09:03:29 -0600430 addr = (void *)hextoul(argv[1], NULL);
431 blk = hextoul(argv[2], NULL);
432 cnt = hextoul(argv[3], NULL);
Lei Wend430d7c2011-05-02 16:26:25 +0000433
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600434 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200435 if (!mmc)
436 return CMD_RET_FAILURE;
Lei Wen31b99802011-05-02 16:26:26 +0000437
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200438 printf("\nMMC write: dev # %d, block # %d, count %d ... ",
439 curr_device, blk, cnt);
Lei Wen31b99802011-05-02 16:26:26 +0000440
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200441 if (mmc_getwp(mmc) == 1) {
442 printf("Error: card is write protected!\n");
443 return CMD_RET_FAILURE;
444 }
Simon Glasse5db1152016-05-01 13:52:35 -0600445 n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200446 printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Lei Wend430d7c2011-05-02 16:26:25 +0000447
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200448 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
449}
Simon Glassed38aef2020-05-10 11:40:03 -0600450
451static int do_mmc_erase(struct cmd_tbl *cmdtp, int flag,
452 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200453{
454 struct mmc *mmc;
455 u32 blk, cnt, n;
Tom Rinif8c6f792014-02-05 10:24:21 -0500456
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200457 if (argc != 3)
458 return CMD_RET_USAGE;
Tom Rinif8c6f792014-02-05 10:24:21 -0500459
Simon Glass3ff49ec2021-07-24 09:03:29 -0600460 blk = hextoul(argv[1], NULL);
461 cnt = hextoul(argv[2], NULL);
Amar3ccad532013-04-27 11:43:00 +0530462
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600463 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200464 if (!mmc)
465 return CMD_RET_FAILURE;
Tom Rinif8c6f792014-02-05 10:24:21 -0500466
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200467 printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
468 curr_device, blk, cnt);
Tom Rini4cf854c2014-02-05 10:24:22 -0500469
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200470 if (mmc_getwp(mmc) == 1) {
471 printf("Error: card is write protected!\n");
472 return CMD_RET_FAILURE;
473 }
Simon Glasse5db1152016-05-01 13:52:35 -0600474 n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200475 printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
Tom Rini4cf854c2014-02-05 10:24:22 -0500476
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200477 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
478}
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +0100479#endif
480
Simon Glassed38aef2020-05-10 11:40:03 -0600481static int do_mmc_rescan(struct cmd_tbl *cmdtp, int flag,
482 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200483{
484 struct mmc *mmc;
Tom Rini4cf854c2014-02-05 10:24:22 -0500485
Stephen Warren07ec4e72014-05-23 13:24:46 -0600486 mmc = init_mmc_device(curr_device, true);
487 if (!mmc)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200488 return CMD_RET_FAILURE;
Tom Rini4cf854c2014-02-05 10:24:22 -0500489
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200490 return CMD_RET_SUCCESS;
491}
Simon Glassed38aef2020-05-10 11:40:03 -0600492
493static int do_mmc_part(struct cmd_tbl *cmdtp, int flag,
494 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200495{
Simon Glasse3394752016-02-29 15:25:34 -0700496 struct blk_desc *mmc_dev;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200497 struct mmc *mmc;
Amar3ccad532013-04-27 11:43:00 +0530498
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600499 mmc = init_mmc_device(curr_device, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200500 if (!mmc)
501 return CMD_RET_FAILURE;
Amar3ccad532013-04-27 11:43:00 +0530502
Simon Glassfa255112016-05-01 11:36:15 -0600503 mmc_dev = blk_get_devnum_by_type(IF_TYPE_MMC, curr_device);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200504 if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
Simon Glassb89a8442016-02-29 15:25:48 -0700505 part_print(mmc_dev);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200506 return CMD_RET_SUCCESS;
507 }
Amar3ccad532013-04-27 11:43:00 +0530508
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200509 puts("get mmc type error!\n");
510 return CMD_RET_FAILURE;
511}
Simon Glassed38aef2020-05-10 11:40:03 -0600512
513static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag,
514 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200515{
Stephen Warren18549802014-05-23 12:48:10 -0600516 int dev, part = 0, ret;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200517 struct mmc *mmc;
Tom Rini35a3ea12014-02-07 14:15:20 -0500518
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200519 if (argc == 1) {
520 dev = curr_device;
521 } else if (argc == 2) {
Simon Glassff9b9032021-07-24 09:03:30 -0600522 dev = dectoul(argv[1], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200523 } else if (argc == 3) {
Simon Glassff9b9032021-07-24 09:03:30 -0600524 dev = (int)dectoul(argv[1], NULL);
525 part = (int)dectoul(argv[2], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200526 if (part > PART_ACCESS_MASK) {
527 printf("#part_num shouldn't be larger than %d\n",
528 PART_ACCESS_MASK);
529 return CMD_RET_FAILURE;
Tom Rini35a3ea12014-02-07 14:15:20 -0500530 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200531 } else {
532 return CMD_RET_USAGE;
533 }
Tom Rini35a3ea12014-02-07 14:15:20 -0500534
Stephen Warrencf66e6f2014-05-23 13:24:47 -0600535 mmc = init_mmc_device(dev, true);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200536 if (!mmc)
537 return CMD_RET_FAILURE;
Tom Rini35a3ea12014-02-07 14:15:20 -0500538
Simon Glass11f2bb62016-05-01 13:52:29 -0600539 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
Stephen Warren18549802014-05-23 12:48:10 -0600540 printf("switch to partitions #%d, %s\n",
541 part, (!ret) ? "OK" : "ERROR");
542 if (ret)
543 return 1;
544
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200545 curr_device = dev;
546 if (mmc->part_config == MMCPART_NOAVAILABLE)
547 printf("mmc%d is current device\n", curr_device);
548 else
549 printf("mmc%d(part %d) is current device\n",
Simon Glasse5db1152016-05-01 13:52:35 -0600550 curr_device, mmc_get_blk_desc(mmc)->hwpart);
Tom Rini35a3ea12014-02-07 14:15:20 -0500551
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200552 return CMD_RET_SUCCESS;
553}
Simon Glassed38aef2020-05-10 11:40:03 -0600554
555static int do_mmc_list(struct cmd_tbl *cmdtp, int flag,
556 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200557{
558 print_mmc_devices('\n');
559 return CMD_RET_SUCCESS;
560}
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100561
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +0100562#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100563static int parse_hwpart_user(struct mmc_hwpart_conf *pconf,
Simon Glassed38aef2020-05-10 11:40:03 -0600564 int argc, char *const argv[])
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100565{
566 int i = 0;
567
568 memset(&pconf->user, 0, sizeof(pconf->user));
569
570 while (i < argc) {
571 if (!strcmp(argv[i], "enh")) {
572 if (i + 2 >= argc)
573 return -1;
574 pconf->user.enh_start =
Simon Glassff9b9032021-07-24 09:03:30 -0600575 dectoul(argv[i + 1], NULL);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100576 pconf->user.enh_size =
Simon Glassff9b9032021-07-24 09:03:30 -0600577 dectoul(argv[i + 2], NULL);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100578 i += 3;
579 } else if (!strcmp(argv[i], "wrrel")) {
580 if (i + 1 >= argc)
581 return -1;
582 pconf->user.wr_rel_change = 1;
583 if (!strcmp(argv[i+1], "on"))
584 pconf->user.wr_rel_set = 1;
585 else if (!strcmp(argv[i+1], "off"))
586 pconf->user.wr_rel_set = 0;
587 else
588 return -1;
589 i += 2;
590 } else {
591 break;
592 }
593 }
594 return i;
595}
596
597static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
Simon Glassed38aef2020-05-10 11:40:03 -0600598 int argc, char *const argv[])
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100599{
600 int i;
601
602 memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
603
604 if (1 >= argc)
605 return -1;
Simon Glassff9b9032021-07-24 09:03:30 -0600606 pconf->gp_part[pidx].size = dectoul(argv[0], NULL);
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100607
608 i = 1;
609 while (i < argc) {
610 if (!strcmp(argv[i], "enh")) {
611 pconf->gp_part[pidx].enhanced = 1;
612 i += 1;
613 } else if (!strcmp(argv[i], "wrrel")) {
614 if (i + 1 >= argc)
615 return -1;
616 pconf->gp_part[pidx].wr_rel_change = 1;
617 if (!strcmp(argv[i+1], "on"))
618 pconf->gp_part[pidx].wr_rel_set = 1;
619 else if (!strcmp(argv[i+1], "off"))
620 pconf->gp_part[pidx].wr_rel_set = 0;
621 else
622 return -1;
623 i += 2;
624 } else {
625 break;
626 }
627 }
628 return i;
629}
630
Simon Glassed38aef2020-05-10 11:40:03 -0600631static int do_mmc_hwpartition(struct cmd_tbl *cmdtp, int flag,
632 int argc, char *const argv[])
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100633{
634 struct mmc *mmc;
635 struct mmc_hwpart_conf pconf = { };
636 enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100637 int i, r, pidx;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100638
639 mmc = init_mmc_device(curr_device, false);
640 if (!mmc)
641 return CMD_RET_FAILURE;
642
643 if (argc < 1)
644 return CMD_RET_USAGE;
645 i = 1;
646 while (i < argc) {
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100647 if (!strcmp(argv[i], "user")) {
648 i++;
649 r = parse_hwpart_user(&pconf, argc-i, &argv[i]);
650 if (r < 0)
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100651 return CMD_RET_USAGE;
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100652 i += r;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100653 } else if (!strncmp(argv[i], "gp", 2) &&
654 strlen(argv[i]) == 3 &&
655 argv[i][2] >= '1' && argv[i][2] <= '4') {
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100656 pidx = argv[i][2] - '1';
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100657 i++;
658 r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
659 if (r < 0)
660 return CMD_RET_USAGE;
661 i += r;
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100662 } else if (!strcmp(argv[i], "check")) {
663 mode = MMC_HWPART_CONF_CHECK;
664 i++;
665 } else if (!strcmp(argv[i], "set")) {
666 mode = MMC_HWPART_CONF_SET;
667 i++;
668 } else if (!strcmp(argv[i], "complete")) {
669 mode = MMC_HWPART_CONF_COMPLETE;
670 i++;
671 } else {
672 return CMD_RET_USAGE;
673 }
674 }
675
676 puts("Partition configuration:\n");
677 if (pconf.user.enh_size) {
678 puts("\tUser Enhanced Start: ");
679 print_size(((u64)pconf.user.enh_start) << 9, "\n");
680 puts("\tUser Enhanced Size: ");
681 print_size(((u64)pconf.user.enh_size) << 9, "\n");
682 } else {
683 puts("\tNo enhanced user data area\n");
684 }
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100685 if (pconf.user.wr_rel_change)
686 printf("\tUser partition write reliability: %s\n",
687 pconf.user.wr_rel_set ? "on" : "off");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100688 for (pidx = 0; pidx < 4; pidx++) {
689 if (pconf.gp_part[pidx].size) {
690 printf("\tGP%i Capacity: ", pidx+1);
691 print_size(((u64)pconf.gp_part[pidx].size) << 9,
692 pconf.gp_part[pidx].enhanced ?
693 " ENH\n" : "\n");
694 } else {
695 printf("\tNo GP%i partition\n", pidx+1);
696 }
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100697 if (pconf.gp_part[pidx].wr_rel_change)
698 printf("\tGP%i write reliability: %s\n", pidx+1,
699 pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100700 }
701
702 if (!mmc_hwpart_config(mmc, &pconf, mode)) {
703 if (mode == MMC_HWPART_CONF_COMPLETE)
704 puts("Partitioning successful, "
705 "power-cycle to make effective\n");
706 return CMD_RET_SUCCESS;
707 } else {
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100708 puts("Failed!\n");
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100709 return CMD_RET_FAILURE;
710 }
711}
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +0100712#endif
Diego Santa Cruz420fc162014-12-23 10:50:30 +0100713
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200714#ifdef CONFIG_SUPPORT_EMMC_BOOT
Simon Glassed38aef2020-05-10 11:40:03 -0600715static int do_mmc_bootbus(struct cmd_tbl *cmdtp, int flag,
716 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200717{
718 int dev;
719 struct mmc *mmc;
720 u8 width, reset, mode;
721
722 if (argc != 5)
723 return CMD_RET_USAGE;
Simon Glassff9b9032021-07-24 09:03:30 -0600724 dev = dectoul(argv[1], NULL);
725 width = dectoul(argv[2], NULL);
726 reset = dectoul(argv[3], NULL);
727 mode = dectoul(argv[4], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200728
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600729 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200730 if (!mmc)
731 return CMD_RET_FAILURE;
732
733 if (IS_SD(mmc)) {
734 puts("BOOT_BUS_WIDTH only exists on eMMC\n");
735 return CMD_RET_FAILURE;
Amar3ccad532013-04-27 11:43:00 +0530736 }
Markus Niebel03951412013-12-16 13:40:46 +0100737
Jaehoon Chung7483dd32021-02-26 18:38:20 +0900738 /*
739 * BOOT_BUS_CONDITIONS[177]
740 * BOOT_MODE[4:3]
741 * 0x0 : Use SDR + Backward compatible timing in boot operation
742 * 0x1 : Use SDR + High Speed Timing in boot operation mode
743 * 0x2 : Use DDR in boot operation
744 * RESET_BOOT_BUS_CONDITIONS
745 * 0x0 : Reset bus width to x1, SDR, Backward compatible
746 * 0x1 : Retain BOOT_BUS_WIDTH and BOOT_MODE
747 * BOOT_BUS_WIDTH
748 * 0x0 : x1(sdr) or x4 (ddr) buswidth
749 * 0x1 : x4(sdr/ddr) buswith
750 * 0x2 : x8(sdr/ddr) buswith
751 *
752 */
753 if (width >= 0x3) {
754 printf("boot_bus_width %d is invalid\n", width);
755 return CMD_RET_FAILURE;
756 }
757
758 if (reset >= 0x2) {
759 printf("reset_boot_bus_width %d is invalid\n", reset);
760 return CMD_RET_FAILURE;
761 }
762
763 if (mode >= 0x3) {
764 printf("reset_boot_bus_width %d is invalid\n", mode);
765 return CMD_RET_FAILURE;
766 }
767
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200768 /* acknowledge to be sent during boot operation */
Jaehoon Chung7483dd32021-02-26 18:38:20 +0900769 if (mmc_set_boot_bus_width(mmc, width, reset, mode)) {
770 puts("BOOT_BUS_WIDTH is failed to change.\n");
771 return CMD_RET_FAILURE;
772 }
773
774 printf("Set to BOOT_BUS_WIDTH = 0x%x, RESET = 0x%x, BOOT_MODE = 0x%x\n",
775 width, reset, mode);
776 return CMD_RET_SUCCESS;
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200777}
Simon Glassed38aef2020-05-10 11:40:03 -0600778
779static int do_mmc_boot_resize(struct cmd_tbl *cmdtp, int flag,
780 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200781{
782 int dev;
783 struct mmc *mmc;
784 u32 bootsize, rpmbsize;
Markus Niebel03951412013-12-16 13:40:46 +0100785
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200786 if (argc != 4)
787 return CMD_RET_USAGE;
Simon Glassff9b9032021-07-24 09:03:30 -0600788 dev = dectoul(argv[1], NULL);
789 bootsize = dectoul(argv[2], NULL);
790 rpmbsize = dectoul(argv[3], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200791
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600792 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200793 if (!mmc)
794 return CMD_RET_FAILURE;
795
796 if (IS_SD(mmc)) {
Heinrich Schuchardteeb191c2020-03-29 19:26:57 +0000797 printf("It is not an eMMC device\n");
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200798 return CMD_RET_FAILURE;
Markus Niebel03951412013-12-16 13:40:46 +0100799 }
800
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200801 if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
802 printf("EMMC boot partition Size change Failed.\n");
803 return CMD_RET_FAILURE;
804 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500805
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200806 printf("EMMC boot partition Size %d MB\n", bootsize);
807 printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
808 return CMD_RET_SUCCESS;
809}
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200810
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200811static int mmc_partconf_print(struct mmc *mmc, const char *varname)
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200812{
813 u8 ack, access, part;
814
815 if (mmc->part_config == MMCPART_NOAVAILABLE) {
816 printf("No part_config info for ver. 0x%x\n", mmc->version);
817 return CMD_RET_FAILURE;
818 }
819
820 access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
821 ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
822 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
823
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200824 if(varname)
825 env_set_hex(varname, part);
826
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200827 printf("EXT_CSD[179], PARTITION_CONFIG:\n"
828 "BOOT_ACK: 0x%x\n"
829 "BOOT_PARTITION_ENABLE: 0x%x\n"
830 "PARTITION_ACCESS: 0x%x\n", ack, part, access);
831
832 return CMD_RET_SUCCESS;
833}
834
Simon Glassed38aef2020-05-10 11:40:03 -0600835static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag,
836 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200837{
838 int dev;
839 struct mmc *mmc;
840 u8 ack, part_num, access;
Lei Wenea526762011-06-22 17:03:31 +0000841
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200842 if (argc != 2 && argc != 3 && argc != 5)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200843 return CMD_RET_USAGE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500844
Simon Glassff9b9032021-07-24 09:03:30 -0600845 dev = dectoul(argv[1], NULL);
Rabin Vincent3e448aa2009-04-05 13:30:53 +0530846
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600847 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200848 if (!mmc)
849 return CMD_RET_FAILURE;
Andy Flemingad347bb2008-10-30 16:41:01 -0500850
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200851 if (IS_SD(mmc)) {
852 puts("PARTITION_CONFIG only exists on eMMC\n");
853 return CMD_RET_FAILURE;
854 }
Andy Flemingad347bb2008-10-30 16:41:01 -0500855
Reuben Dowle55a81ad2021-05-14 10:15:43 +1200856 if (argc == 2 || argc == 3)
857 return mmc_partconf_print(mmc, argc == 3 ? argv[2] : NULL);
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200858
Simon Glassff9b9032021-07-24 09:03:30 -0600859 ack = dectoul(argv[2], NULL);
860 part_num = dectoul(argv[3], NULL);
861 access = dectoul(argv[4], NULL);
Angelo Dureghellof54f7532017-08-01 14:27:10 +0200862
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200863 /* acknowledge to be sent during boot operation */
864 return mmc_set_part_conf(mmc, ack, part_num, access);
865}
Simon Glassed38aef2020-05-10 11:40:03 -0600866
867static int do_mmc_rst_func(struct cmd_tbl *cmdtp, int flag,
868 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200869{
870 int dev;
871 struct mmc *mmc;
872 u8 enable;
Nikita Kiryanov020f2612012-12-03 02:19:46 +0000873
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200874 /*
875 * Set the RST_n_ENABLE bit of RST_n_FUNCTION
876 * The only valid values are 0x0, 0x1 and 0x2 and writing
877 * a value of 0x1 or 0x2 sets the value permanently.
878 */
879 if (argc != 3)
880 return CMD_RET_USAGE;
881
Simon Glassff9b9032021-07-24 09:03:30 -0600882 dev = dectoul(argv[1], NULL);
883 enable = dectoul(argv[2], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200884
Peng Fanb1e455a2015-11-25 17:16:21 +0800885 if (enable > 2) {
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200886 puts("Invalid RST_n_ENABLE value\n");
887 return CMD_RET_USAGE;
888 }
889
Stephen Warrenb627f4b2014-05-23 13:24:45 -0600890 mmc = init_mmc_device(dev, false);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200891 if (!mmc)
892 return CMD_RET_FAILURE;
893
894 if (IS_SD(mmc)) {
895 puts("RST_n_FUNCTION only exists on eMMC\n");
896 return CMD_RET_FAILURE;
897 }
898
899 return mmc_set_rst_n_function(mmc, enable);
900}
901#endif
Simon Glassed38aef2020-05-10 11:40:03 -0600902static int do_mmc_setdsr(struct cmd_tbl *cmdtp, int flag,
903 int argc, char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200904{
905 struct mmc *mmc;
906 u32 val;
907 int ret;
Andy Flemingad347bb2008-10-30 16:41:01 -0500908
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200909 if (argc != 2)
910 return CMD_RET_USAGE;
Simon Glass3ff49ec2021-07-24 09:03:29 -0600911 val = hextoul(argv[1], NULL);
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200912
913 mmc = find_mmc_device(curr_device);
914 if (!mmc) {
915 printf("no mmc device at slot %x\n", curr_device);
916 return CMD_RET_FAILURE;
917 }
918 ret = mmc_set_dsr(mmc, val);
919 printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
920 if (!ret) {
921 mmc->has_init = 0;
922 if (mmc_init(mmc))
923 return CMD_RET_FAILURE;
924 else
925 return CMD_RET_SUCCESS;
Andy Flemingad347bb2008-10-30 16:41:01 -0500926 }
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200927 return ret;
928}
Lei Wend430d7c2011-05-02 16:26:25 +0000929
Tomas Melinc17dae52016-11-25 11:01:03 +0200930#ifdef CONFIG_CMD_BKOPS_ENABLE
Simon Glassed38aef2020-05-10 11:40:03 -0600931static int do_mmc_bkops_enable(struct cmd_tbl *cmdtp, int flag,
932 int argc, char *const argv[])
Tomas Melinc17dae52016-11-25 11:01:03 +0200933{
934 int dev;
935 struct mmc *mmc;
936
937 if (argc != 2)
938 return CMD_RET_USAGE;
939
Simon Glassff9b9032021-07-24 09:03:30 -0600940 dev = dectoul(argv[1], NULL);
Tomas Melinc17dae52016-11-25 11:01:03 +0200941
942 mmc = init_mmc_device(dev, false);
943 if (!mmc)
944 return CMD_RET_FAILURE;
945
946 if (IS_SD(mmc)) {
947 puts("BKOPS_EN only exists on eMMC\n");
948 return CMD_RET_FAILURE;
949 }
950
951 return mmc_set_bkops_enable(mmc);
952}
953#endif
954
Simon Glassed38aef2020-05-10 11:40:03 -0600955static int do_mmc_boot_wp(struct cmd_tbl *cmdtp, int flag,
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +0200956 int argc, char * const argv[])
957{
958 int err;
959 struct mmc *mmc;
960
961 mmc = init_mmc_device(curr_device, false);
962 if (!mmc)
963 return CMD_RET_FAILURE;
964 if (IS_SD(mmc)) {
965 printf("It is not an eMMC device\n");
966 return CMD_RET_FAILURE;
967 }
968 err = mmc_boot_wp(mmc);
969 if (err)
970 return CMD_RET_FAILURE;
971 printf("boot areas protected\n");
972 return CMD_RET_SUCCESS;
973}
974
Simon Glassed38aef2020-05-10 11:40:03 -0600975static struct cmd_tbl cmd_mmc[] = {
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200976 U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
977 U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
Heinrich Schuchardt75e5a642020-03-30 07:24:19 +0200978 U_BOOT_CMD_MKENT(wp, 1, 0, do_mmc_boot_wp, "", ""),
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +0100979#if CONFIG_IS_ENABLED(MMC_WRITE)
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200980 U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
981 U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
Jean-Jacques Hiblot27edffe2018-01-04 15:23:34 +0100982#endif
Alex Kiernanc568bcb2018-05-29 15:30:52 +0000983#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
984 U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
985#endif
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200986 U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
987 U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
988 U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
989 U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +0100990#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Diego Santa Cruz58bda792014-12-23 10:50:32 +0100991 U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
Jean-Jacques Hiblot1d7769a2017-11-30 17:44:02 +0100992#endif
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200993#ifdef CONFIG_SUPPORT_EMMC_BOOT
994 U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
Wally Yehd49c4aa2014-09-25 23:00:16 +0800995 U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
Pierre Aubertbcc302c2014-04-24 10:30:08 +0200996 U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
997 U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
998#endif
Alex Kiernan60e0f612018-05-08 04:43:31 +0000999#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001000 U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
1001#endif
1002 U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
Tomas Melinc17dae52016-11-25 11:01:03 +02001003#ifdef CONFIG_CMD_BKOPS_ENABLE
1004 U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
1005#endif
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001006};
1007
Simon Glassed38aef2020-05-10 11:40:03 -06001008static int do_mmcops(struct cmd_tbl *cmdtp, int flag, int argc,
1009 char *const argv[])
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001010{
Simon Glassed38aef2020-05-10 11:40:03 -06001011 struct cmd_tbl *cp;
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001012
1013 cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
1014
1015 /* Drop the mmc command */
1016 argc--;
1017 argv++;
1018
1019 if (cp == NULL || argc > cp->maxargs)
1020 return CMD_RET_USAGE;
Boris Brezillonc71dfd12018-12-03 22:54:20 +01001021 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001022 return CMD_RET_SUCCESS;
1023
1024 if (curr_device < 0) {
1025 if (get_mmc_num() > 0) {
1026 curr_device = 0;
1027 } else {
1028 puts("No MMC device available\n");
1029 return CMD_RET_FAILURE;
1030 }
1031 }
1032 return cp->cmd(cmdtp, flag, argc, argv);
Andy Flemingad347bb2008-10-30 16:41:01 -05001033}
1034
1035U_BOOT_CMD(
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001036 mmc, 29, 1, do_mmcops,
Mike Frysinger27e1e622009-03-23 22:27:34 -04001037 "MMC sub system",
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001038 "info - display info of the current MMC device\n"
1039 "mmc read addr blk# cnt\n"
Lei Wend430d7c2011-05-02 16:26:25 +00001040 "mmc write addr blk# cnt\n"
Alex Kiernanc568bcb2018-05-29 15:30:52 +00001041#if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
Jassi Brar814a9752018-04-06 12:05:24 +05301042 "mmc swrite addr blk#\n"
1043#endif
Lei Wenea526762011-06-22 17:03:31 +00001044 "mmc erase blk# cnt\n"
Lei Wend430d7c2011-05-02 16:26:25 +00001045 "mmc rescan\n"
1046 "mmc part - lists available partition on current mmc device\n"
Lei Wen31b99802011-05-02 16:26:26 +00001047 "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
Amar3ccad532013-04-27 11:43:00 +05301048 "mmc list - lists available devices\n"
Michal Simek21f753c2020-06-05 11:45:12 +02001049 "mmc wp - power on write protect boot partitions\n"
Alex Kiernan72d80462018-06-11 16:20:09 +00001050#if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
Jaehoon Chung94a47572021-02-26 16:07:18 +09001051 "mmc hwpartition <USER> <GP> <MODE> - does hardware partitioning\n"
Diego Santa Cruz420fc162014-12-23 10:50:30 +01001052 " arguments (sizes in 512-byte blocks):\n"
Jaehoon Chung94a47572021-02-26 16:07:18 +09001053 " USER - <user> <enh> <start> <cnt> <wrrel> <{on|off}>\n"
1054 " : sets user data area attributes\n"
1055 " GP - <{gp1|gp2|gp3|gp4}> <cnt> <enh> <wrrel> <{on|off}>\n"
1056 " : general purpose partition\n"
1057 " MODE - <{check|set|complete}>\n"
1058 " : mode, complete set partitioning completed\n"
Diego Santa Cruz58bda792014-12-23 10:50:32 +01001059 " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
1060 " Power cycling is required to initialize partitions after set to complete.\n"
Alex Kiernan72d80462018-06-11 16:20:09 +00001061#endif
Amar3ccad532013-04-27 11:43:00 +05301062#ifdef CONFIG_SUPPORT_EMMC_BOOT
Jaehoon Chung8460b912020-12-16 07:24:50 +09001063 "mmc bootbus <dev> <boot_bus_width> <reset_boot_bus_width> <boot_mode>\n"
Tom Rini4cf854c2014-02-05 10:24:22 -05001064 " - Set the BOOT_BUS_WIDTH field of the specified device\n"
Tom Rini79919462014-02-05 10:24:20 -05001065 "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
1066 " - Change sizes of boot and RPMB partitions of specified device\n"
Reuben Dowle55a81ad2021-05-14 10:15:43 +12001067 "mmc partconf <dev> [[varname] | [<boot_ack> <boot_partition> <partition_access>]]\n"
Angelo Dureghellof54f7532017-08-01 14:27:10 +02001068 " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
Reuben Dowle55a81ad2021-05-14 10:15:43 +12001069 " If showing the bits, optionally store the boot_partition field into varname\n"
Jaehoon Chung8460b912020-12-16 07:24:50 +09001070 "mmc rst-function <dev> <value>\n"
Tom Rini35a3ea12014-02-07 14:15:20 -05001071 " - Change the RST_n_FUNCTION field of the specified device\n"
1072 " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
Dirk Behmedf8852a2009-02-18 19:59:39 +01001073#endif
Alex Kiernan60e0f612018-05-08 04:43:31 +00001074#if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001075 "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
1076 "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
1077 "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
1078 "mmc rpmb counter - read the value of the write counter\n"
1079#endif
1080 "mmc setdsr <value> - set DSR register value\n"
Tomas Melinc17dae52016-11-25 11:01:03 +02001081#ifdef CONFIG_CMD_BKOPS_ENABLE
1082 "mmc bkops-enable <dev> - enable background operations handshake on device\n"
1083 " WARNING: This is a write-once setting.\n"
1084#endif
Amar3ccad532013-04-27 11:43:00 +05301085 );
Pierre Aubertbcc302c2014-04-24 10:30:08 +02001086
1087/* Old command kept for compatibility. Same as 'mmc info' */
1088U_BOOT_CMD(
1089 mmcinfo, 1, 0, do_mmcinfo,
1090 "display MMC info",
1091 "- display info of the current MMC device"
1092);