]>
Commit | Line | Data |
---|---|---|
71f95118 WD |
1 | /* |
2 | * (C) Copyright 2003 | |
3 | * Kyle Harris, [email protected] | |
4 | * | |
1a459660 | 5 | * SPDX-License-Identifier: GPL-2.0+ |
71f95118 WD |
6 | */ |
7 | ||
8 | #include <common.h> | |
9 | #include <command.h> | |
71f95118 WD |
10 | #include <mmc.h> |
11 | ||
02e22c2d | 12 | static int curr_device = -1; |
ea6ebe21 | 13 | #ifndef CONFIG_GENERIC_MMC |
54841ab5 | 14 | int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
71f95118 | 15 | { |
869f6bf4 MK |
16 | int dev; |
17 | ||
47e26b1b | 18 | if (argc < 2) |
4c12eeb8 | 19 | return CMD_RET_USAGE; |
869f6bf4 MK |
20 | |
21 | if (strcmp(argv[1], "init") == 0) { | |
22 | if (argc == 2) { | |
23 | if (curr_device < 0) | |
24 | dev = 1; | |
25 | else | |
26 | dev = curr_device; | |
27 | } else if (argc == 3) { | |
28 | dev = (int)simple_strtoul(argv[2], NULL, 10); | |
29 | } else { | |
4c12eeb8 | 30 | return CMD_RET_USAGE; |
869f6bf4 MK |
31 | } |
32 | ||
33 | if (mmc_legacy_init(dev) != 0) { | |
34 | puts("No MMC card found\n"); | |
35 | return 1; | |
36 | } | |
37 | ||
38 | curr_device = dev; | |
39 | printf("mmc%d is available\n", curr_device); | |
40 | } else if (strcmp(argv[1], "device") == 0) { | |
41 | if (argc == 2) { | |
42 | if (curr_device < 0) { | |
43 | puts("No MMC device available\n"); | |
44 | return 1; | |
45 | } | |
46 | } else if (argc == 3) { | |
47 | dev = (int)simple_strtoul(argv[2], NULL, 10); | |
48 | ||
49 | #ifdef CONFIG_SYS_MMC_SET_DEV | |
50 | if (mmc_set_dev(dev) != 0) | |
51 | return 1; | |
52 | #endif | |
53 | curr_device = dev; | |
54 | } else { | |
4c12eeb8 | 55 | return CMD_RET_USAGE; |
869f6bf4 MK |
56 | } |
57 | ||
58 | printf("mmc%d is current device\n", curr_device); | |
59 | } else { | |
4c12eeb8 | 60 | return CMD_RET_USAGE; |
71f95118 | 61 | } |
869f6bf4 | 62 | |
71f95118 WD |
63 | return 0; |
64 | } | |
65 | ||
0d498393 | 66 | U_BOOT_CMD( |
869f6bf4 MK |
67 | mmc, 3, 1, do_mmc, |
68 | "MMC sub-system", | |
69 | "init [dev] - init MMC sub system\n" | |
a89c33db | 70 | "mmc device [dev] - show or set current device" |
b0fce99b | 71 | ); |
3511b4e2 | 72 | #else /* !CONFIG_GENERIC_MMC */ |
272cc70b AF |
73 | |
74 | static void print_mmcinfo(struct mmc *mmc) | |
75 | { | |
c5f0d3f1 DSC |
76 | int i; |
77 | ||
93bfd616 | 78 | printf("Device: %s\n", mmc->cfg->name); |
272cc70b AF |
79 | printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24); |
80 | printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff); | |
81 | printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff, | |
82 | (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff, | |
83 | (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff); | |
84 | ||
85 | printf("Tran Speed: %d\n", mmc->tran_speed); | |
86 | printf("Rd Block Len: %d\n", mmc->read_bl_len); | |
87 | ||
4b7cee53 PA |
88 | printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC", |
89 | EXTRACT_SDMMC_MAJOR_VERSION(mmc->version), | |
90 | EXTRACT_SDMMC_MINOR_VERSION(mmc->version)); | |
91 | if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0) | |
92 | printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version)); | |
93 | printf("\n"); | |
272cc70b AF |
94 | |
95 | printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No"); | |
940e0782 MK |
96 | puts("Capacity: "); |
97 | print_size(mmc->capacity, "\n"); | |
272cc70b | 98 | |
786e8f81 AG |
99 | printf("Bus Width: %d-bit%s\n", mmc->bus_width, |
100 | mmc->ddr_mode ? " DDR" : ""); | |
c5f0d3f1 | 101 | |
b0361526 DSC |
102 | puts("Erase Group Size: "); |
103 | print_size(((u64)mmc->erase_grp_size) << 9, "\n"); | |
104 | ||
525ada21 | 105 | if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) { |
c3dbb4f9 | 106 | bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0; |
beb98a14 | 107 | bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR); |
b0361526 DSC |
108 | |
109 | puts("HC WP Group Size: "); | |
110 | print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n"); | |
111 | ||
c5f0d3f1 | 112 | puts("User Capacity: "); |
9e41a00b DSC |
113 | print_size(mmc->capacity_user, usr_enh ? " ENH" : ""); |
114 | if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR) | |
115 | puts(" WRREL\n"); | |
116 | else | |
117 | putc('\n'); | |
beb98a14 DSC |
118 | if (usr_enh) { |
119 | puts("User Enhanced Start: "); | |
120 | print_size(mmc->enh_user_start, "\n"); | |
121 | puts("User Enhanced Size: "); | |
122 | print_size(mmc->enh_user_size, "\n"); | |
123 | } | |
c5f0d3f1 | 124 | puts("Boot Capacity: "); |
c3dbb4f9 | 125 | print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n"); |
c5f0d3f1 | 126 | puts("RPMB Capacity: "); |
c3dbb4f9 | 127 | print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n"); |
b0361526 | 128 | |
c5f0d3f1 | 129 | for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) { |
c3dbb4f9 DSC |
130 | bool is_enh = has_enh && |
131 | (mmc->part_attr & EXT_CSD_ENH_GP(i)); | |
c5f0d3f1 | 132 | if (mmc->capacity_gp[i]) { |
f289fd73 | 133 | printf("GP%i Capacity: ", i+1); |
c3dbb4f9 | 134 | print_size(mmc->capacity_gp[i], |
9e41a00b DSC |
135 | is_enh ? " ENH" : ""); |
136 | if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i)) | |
137 | puts(" WRREL\n"); | |
138 | else | |
139 | putc('\n'); | |
c5f0d3f1 DSC |
140 | } |
141 | } | |
142 | } | |
272cc70b | 143 | } |
1ae24a50 | 144 | static struct mmc *init_mmc_device(int dev, bool force_init) |
1fd93c6e PA |
145 | { |
146 | struct mmc *mmc; | |
147 | mmc = find_mmc_device(dev); | |
148 | if (!mmc) { | |
149 | printf("no mmc device at slot %x\n", dev); | |
150 | return NULL; | |
151 | } | |
1ae24a50 SW |
152 | if (force_init) |
153 | mmc->has_init = 0; | |
1fd93c6e PA |
154 | if (mmc_init(mmc)) |
155 | return NULL; | |
156 | return mmc; | |
157 | } | |
088f1b19 | 158 | static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
272cc70b AF |
159 | { |
160 | struct mmc *mmc; | |
272cc70b | 161 | |
ea6ebe21 LW |
162 | if (curr_device < 0) { |
163 | if (get_mmc_num() > 0) | |
164 | curr_device = 0; | |
165 | else { | |
166 | puts("No MMC device available\n"); | |
167 | return 1; | |
168 | } | |
169 | } | |
272cc70b | 170 | |
1ae24a50 | 171 | mmc = init_mmc_device(curr_device, false); |
1fd93c6e PA |
172 | if (!mmc) |
173 | return CMD_RET_FAILURE; | |
272cc70b | 174 | |
1fd93c6e PA |
175 | print_mmcinfo(mmc); |
176 | return CMD_RET_SUCCESS; | |
177 | } | |
272cc70b | 178 | |
1fd93c6e PA |
179 | #ifdef CONFIG_SUPPORT_EMMC_RPMB |
180 | static int confirm_key_prog(void) | |
181 | { | |
182 | puts("Warning: Programming authentication key can be done only once !\n" | |
183 | " Use this command only if you are sure of what you are doing,\n" | |
184 | "Really perform the key programming? <y/N> "); | |
185 | if (confirm_yesno()) | |
ea6ebe21 | 186 | return 1; |
1fd93c6e PA |
187 | |
188 | puts("Authentication key programming aborted\n"); | |
189 | return 0; | |
190 | } | |
191 | static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag, | |
192 | int argc, char * const argv[]) | |
193 | { | |
194 | void *key_addr; | |
195 | struct mmc *mmc = find_mmc_device(curr_device); | |
196 | ||
197 | if (argc != 2) | |
198 | return CMD_RET_USAGE; | |
199 | ||
200 | key_addr = (void *)simple_strtoul(argv[1], NULL, 16); | |
201 | if (!confirm_key_prog()) | |
202 | return CMD_RET_FAILURE; | |
203 | if (mmc_rpmb_set_key(mmc, key_addr)) { | |
204 | printf("ERROR - Key already programmed ?\n"); | |
205 | return CMD_RET_FAILURE; | |
272cc70b | 206 | } |
1fd93c6e | 207 | return CMD_RET_SUCCESS; |
272cc70b | 208 | } |
1fd93c6e PA |
209 | static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag, |
210 | int argc, char * const argv[]) | |
211 | { | |
212 | u16 blk, cnt; | |
213 | void *addr; | |
214 | int n; | |
215 | void *key_addr = NULL; | |
216 | struct mmc *mmc = find_mmc_device(curr_device); | |
272cc70b | 217 | |
1fd93c6e PA |
218 | if (argc < 4) |
219 | return CMD_RET_USAGE; | |
272cc70b | 220 | |
1fd93c6e PA |
221 | addr = (void *)simple_strtoul(argv[1], NULL, 16); |
222 | blk = simple_strtoul(argv[2], NULL, 16); | |
223 | cnt = simple_strtoul(argv[3], NULL, 16); | |
224 | ||
225 | if (argc == 5) | |
226 | key_addr = (void *)simple_strtoul(argv[4], NULL, 16); | |
227 | ||
228 | printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ", | |
229 | curr_device, blk, cnt); | |
230 | n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr); | |
231 | ||
232 | printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR"); | |
233 | if (n != cnt) | |
234 | return CMD_RET_FAILURE; | |
235 | return CMD_RET_SUCCESS; | |
236 | } | |
237 | static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag, | |
238 | int argc, char * const argv[]) | |
272cc70b | 239 | { |
1fd93c6e PA |
240 | u16 blk, cnt; |
241 | void *addr; | |
242 | int n; | |
243 | void *key_addr; | |
244 | struct mmc *mmc = find_mmc_device(curr_device); | |
6be95ccf | 245 | |
1fd93c6e | 246 | if (argc != 5) |
4c12eeb8 | 247 | return CMD_RET_USAGE; |
272cc70b | 248 | |
1fd93c6e PA |
249 | addr = (void *)simple_strtoul(argv[1], NULL, 16); |
250 | blk = simple_strtoul(argv[2], NULL, 16); | |
251 | cnt = simple_strtoul(argv[3], NULL, 16); | |
252 | key_addr = (void *)simple_strtoul(argv[4], NULL, 16); | |
253 | ||
254 | printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ", | |
255 | curr_device, blk, cnt); | |
256 | n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr); | |
257 | ||
258 | printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR"); | |
259 | if (n != cnt) | |
260 | return CMD_RET_FAILURE; | |
261 | return CMD_RET_SUCCESS; | |
262 | } | |
263 | static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag, | |
264 | int argc, char * const argv[]) | |
265 | { | |
266 | unsigned long counter; | |
267 | struct mmc *mmc = find_mmc_device(curr_device); | |
268 | ||
269 | if (mmc_rpmb_get_counter(mmc, &counter)) | |
270 | return CMD_RET_FAILURE; | |
271 | printf("RPMB Write counter= %lx\n", counter); | |
272 | return CMD_RET_SUCCESS; | |
273 | } | |
274 | ||
275 | static cmd_tbl_t cmd_rpmb[] = { | |
276 | U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""), | |
277 | U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""), | |
278 | U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""), | |
279 | U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""), | |
280 | }; | |
281 | ||
282 | static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag, | |
283 | int argc, char * const argv[]) | |
284 | { | |
285 | cmd_tbl_t *cp; | |
286 | struct mmc *mmc; | |
287 | char original_part; | |
288 | int ret; | |
289 | ||
290 | cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb)); | |
291 | ||
292 | /* Drop the rpmb subcommand */ | |
293 | argc--; | |
294 | argv++; | |
295 | ||
296 | if (cp == NULL || argc > cp->maxargs) | |
297 | return CMD_RET_USAGE; | |
298 | if (flag == CMD_FLAG_REPEAT && !cp->repeatable) | |
299 | return CMD_RET_SUCCESS; | |
300 | ||
1ae24a50 | 301 | mmc = init_mmc_device(curr_device, false); |
1fd93c6e PA |
302 | if (!mmc) |
303 | return CMD_RET_FAILURE; | |
304 | ||
305 | if (!(mmc->version & MMC_VERSION_MMC)) { | |
306 | printf("It is not a EMMC device\n"); | |
307 | return CMD_RET_FAILURE; | |
308 | } | |
309 | if (mmc->version < MMC_VERSION_4_41) { | |
310 | printf("RPMB not supported before version 4.41\n"); | |
311 | return CMD_RET_FAILURE; | |
ea6ebe21 | 312 | } |
1fd93c6e PA |
313 | /* Switch to the RPMB partition */ |
314 | original_part = mmc->part_num; | |
315 | if (mmc->part_num != MMC_PART_RPMB) { | |
316 | if (mmc_switch_part(curr_device, MMC_PART_RPMB) != 0) | |
317 | return CMD_RET_FAILURE; | |
318 | mmc->part_num = MMC_PART_RPMB; | |
319 | } | |
320 | ret = cp->cmd(cmdtp, flag, argc, argv); | |
272cc70b | 321 | |
1fd93c6e PA |
322 | /* Return to original partition */ |
323 | if (mmc->part_num != original_part) { | |
324 | if (mmc_switch_part(curr_device, original_part) != 0) | |
325 | return CMD_RET_FAILURE; | |
326 | mmc->part_num = original_part; | |
327 | } | |
328 | return ret; | |
329 | } | |
330 | #endif | |
9fd38372 | 331 | |
1fd93c6e PA |
332 | static int do_mmc_read(cmd_tbl_t *cmdtp, int flag, |
333 | int argc, char * const argv[]) | |
334 | { | |
335 | struct mmc *mmc; | |
336 | u32 blk, cnt, n; | |
337 | void *addr; | |
e85649c7 | 338 | |
1fd93c6e PA |
339 | if (argc != 4) |
340 | return CMD_RET_USAGE; | |
ea6ebe21 | 341 | |
1fd93c6e PA |
342 | addr = (void *)simple_strtoul(argv[1], NULL, 16); |
343 | blk = simple_strtoul(argv[2], NULL, 16); | |
344 | cnt = simple_strtoul(argv[3], NULL, 16); | |
272cc70b | 345 | |
1ae24a50 | 346 | mmc = init_mmc_device(curr_device, false); |
1fd93c6e PA |
347 | if (!mmc) |
348 | return CMD_RET_FAILURE; | |
ea6ebe21 | 349 | |
1fd93c6e PA |
350 | printf("\nMMC read: dev # %d, block # %d, count %d ... ", |
351 | curr_device, blk, cnt); | |
9fd38372 | 352 | |
1fd93c6e PA |
353 | n = mmc->block_dev.block_read(curr_device, blk, cnt, addr); |
354 | /* flush cache after read */ | |
355 | flush_cache((ulong)addr, cnt * 512); /* FIXME */ | |
356 | printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR"); | |
8f3b9642 | 357 | |
1fd93c6e PA |
358 | return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE; |
359 | } | |
360 | static int do_mmc_write(cmd_tbl_t *cmdtp, int flag, | |
361 | int argc, char * const argv[]) | |
362 | { | |
363 | struct mmc *mmc; | |
364 | u32 blk, cnt, n; | |
365 | void *addr; | |
8f3b9642 | 366 | |
1fd93c6e PA |
367 | if (argc != 4) |
368 | return CMD_RET_USAGE; | |
272cc70b | 369 | |
1fd93c6e PA |
370 | addr = (void *)simple_strtoul(argv[1], NULL, 16); |
371 | blk = simple_strtoul(argv[2], NULL, 16); | |
372 | cnt = simple_strtoul(argv[3], NULL, 16); | |
bc897b1d | 373 | |
1ae24a50 | 374 | mmc = init_mmc_device(curr_device, false); |
1fd93c6e PA |
375 | if (!mmc) |
376 | return CMD_RET_FAILURE; | |
bc897b1d | 377 | |
1fd93c6e PA |
378 | printf("\nMMC write: dev # %d, block # %d, count %d ... ", |
379 | curr_device, blk, cnt); | |
272cc70b | 380 | |
1fd93c6e PA |
381 | if (mmc_getwp(mmc) == 1) { |
382 | printf("Error: card is write protected!\n"); | |
383 | return CMD_RET_FAILURE; | |
384 | } | |
385 | n = mmc->block_dev.block_write(curr_device, blk, cnt, addr); | |
386 | printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR"); | |
792970b0 | 387 | |
1fd93c6e PA |
388 | return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE; |
389 | } | |
390 | static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag, | |
391 | int argc, char * const argv[]) | |
392 | { | |
393 | struct mmc *mmc; | |
394 | u32 blk, cnt, n; | |
2a91c913 | 395 | |
1fd93c6e PA |
396 | if (argc != 3) |
397 | return CMD_RET_USAGE; | |
792970b0 | 398 | |
1fd93c6e PA |
399 | blk = simple_strtoul(argv[1], NULL, 16); |
400 | cnt = simple_strtoul(argv[2], NULL, 16); | |
5a99b9de | 401 | |
1ae24a50 | 402 | mmc = init_mmc_device(curr_device, false); |
1fd93c6e PA |
403 | if (!mmc) |
404 | return CMD_RET_FAILURE; | |
5a99b9de | 405 | |
1fd93c6e PA |
406 | printf("\nMMC erase: dev # %d, block # %d, count %d ... ", |
407 | curr_device, blk, cnt); | |
5a99b9de | 408 | |
1fd93c6e PA |
409 | if (mmc_getwp(mmc) == 1) { |
410 | printf("Error: card is write protected!\n"); | |
411 | return CMD_RET_FAILURE; | |
412 | } | |
413 | n = mmc->block_dev.block_erase(curr_device, blk, cnt); | |
414 | printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR"); | |
b01e6fe6 | 415 | |
1fd93c6e PA |
416 | return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE; |
417 | } | |
418 | static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag, | |
419 | int argc, char * const argv[]) | |
420 | { | |
421 | struct mmc *mmc; | |
2a91c913 | 422 | |
941944e4 SW |
423 | mmc = init_mmc_device(curr_device, true); |
424 | if (!mmc) | |
1fd93c6e | 425 | return CMD_RET_FAILURE; |
2a91c913 | 426 | |
1fd93c6e PA |
427 | return CMD_RET_SUCCESS; |
428 | } | |
429 | static int do_mmc_part(cmd_tbl_t *cmdtp, int flag, | |
430 | int argc, char * const argv[]) | |
431 | { | |
432 | block_dev_desc_t *mmc_dev; | |
433 | struct mmc *mmc; | |
434 | ||
1ae24a50 | 435 | mmc = init_mmc_device(curr_device, false); |
1fd93c6e PA |
436 | if (!mmc) |
437 | return CMD_RET_FAILURE; | |
438 | ||
439 | mmc_dev = mmc_get_dev(curr_device); | |
440 | if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) { | |
441 | print_part(mmc_dev); | |
442 | return CMD_RET_SUCCESS; | |
443 | } | |
444 | ||
445 | puts("get mmc type error!\n"); | |
446 | return CMD_RET_FAILURE; | |
447 | } | |
448 | static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag, | |
449 | int argc, char * const argv[]) | |
450 | { | |
60dc58f7 | 451 | int dev, part = 0, ret; |
1fd93c6e PA |
452 | struct mmc *mmc; |
453 | ||
454 | if (argc == 1) { | |
455 | dev = curr_device; | |
456 | } else if (argc == 2) { | |
457 | dev = simple_strtoul(argv[1], NULL, 10); | |
458 | } else if (argc == 3) { | |
459 | dev = (int)simple_strtoul(argv[1], NULL, 10); | |
460 | part = (int)simple_strtoul(argv[2], NULL, 10); | |
461 | if (part > PART_ACCESS_MASK) { | |
462 | printf("#part_num shouldn't be larger than %d\n", | |
463 | PART_ACCESS_MASK); | |
464 | return CMD_RET_FAILURE; | |
33ace362 | 465 | } |
1fd93c6e PA |
466 | } else { |
467 | return CMD_RET_USAGE; | |
468 | } | |
33ace362 | 469 | |
a5710920 | 470 | mmc = init_mmc_device(dev, true); |
1fd93c6e PA |
471 | if (!mmc) |
472 | return CMD_RET_FAILURE; | |
473 | ||
60dc58f7 SW |
474 | ret = mmc_select_hwpart(dev, part); |
475 | printf("switch to partitions #%d, %s\n", | |
476 | part, (!ret) ? "OK" : "ERROR"); | |
477 | if (ret) | |
478 | return 1; | |
479 | ||
1fd93c6e PA |
480 | curr_device = dev; |
481 | if (mmc->part_config == MMCPART_NOAVAILABLE) | |
482 | printf("mmc%d is current device\n", curr_device); | |
483 | else | |
484 | printf("mmc%d(part %d) is current device\n", | |
485 | curr_device, mmc->part_num); | |
33ace362 | 486 | |
1fd93c6e PA |
487 | return CMD_RET_SUCCESS; |
488 | } | |
489 | static int do_mmc_list(cmd_tbl_t *cmdtp, int flag, | |
490 | int argc, char * const argv[]) | |
491 | { | |
492 | print_mmc_devices('\n'); | |
493 | return CMD_RET_SUCCESS; | |
494 | } | |
c599f53b | 495 | |
189f963a DSC |
496 | static int parse_hwpart_user(struct mmc_hwpart_conf *pconf, |
497 | int argc, char * const argv[]) | |
498 | { | |
499 | int i = 0; | |
500 | ||
501 | memset(&pconf->user, 0, sizeof(pconf->user)); | |
502 | ||
503 | while (i < argc) { | |
504 | if (!strcmp(argv[i], "enh")) { | |
505 | if (i + 2 >= argc) | |
506 | return -1; | |
507 | pconf->user.enh_start = | |
508 | simple_strtoul(argv[i+1], NULL, 10); | |
509 | pconf->user.enh_size = | |
510 | simple_strtoul(argv[i+2], NULL, 10); | |
511 | i += 3; | |
512 | } else if (!strcmp(argv[i], "wrrel")) { | |
513 | if (i + 1 >= argc) | |
514 | return -1; | |
515 | pconf->user.wr_rel_change = 1; | |
516 | if (!strcmp(argv[i+1], "on")) | |
517 | pconf->user.wr_rel_set = 1; | |
518 | else if (!strcmp(argv[i+1], "off")) | |
519 | pconf->user.wr_rel_set = 0; | |
520 | else | |
521 | return -1; | |
522 | i += 2; | |
523 | } else { | |
524 | break; | |
525 | } | |
526 | } | |
527 | return i; | |
528 | } | |
529 | ||
530 | static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx, | |
531 | int argc, char * const argv[]) | |
532 | { | |
533 | int i; | |
534 | ||
535 | memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx])); | |
536 | ||
537 | if (1 >= argc) | |
538 | return -1; | |
539 | pconf->gp_part[pidx].size = simple_strtoul(argv[0], NULL, 10); | |
540 | ||
541 | i = 1; | |
542 | while (i < argc) { | |
543 | if (!strcmp(argv[i], "enh")) { | |
544 | pconf->gp_part[pidx].enhanced = 1; | |
545 | i += 1; | |
546 | } else if (!strcmp(argv[i], "wrrel")) { | |
547 | if (i + 1 >= argc) | |
548 | return -1; | |
549 | pconf->gp_part[pidx].wr_rel_change = 1; | |
550 | if (!strcmp(argv[i+1], "on")) | |
551 | pconf->gp_part[pidx].wr_rel_set = 1; | |
552 | else if (!strcmp(argv[i+1], "off")) | |
553 | pconf->gp_part[pidx].wr_rel_set = 0; | |
554 | else | |
555 | return -1; | |
556 | i += 2; | |
557 | } else { | |
558 | break; | |
559 | } | |
560 | } | |
561 | return i; | |
562 | } | |
563 | ||
c599f53b DSC |
564 | static int do_mmc_hwpartition(cmd_tbl_t *cmdtp, int flag, |
565 | int argc, char * const argv[]) | |
566 | { | |
567 | struct mmc *mmc; | |
568 | struct mmc_hwpart_conf pconf = { }; | |
569 | enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK; | |
189f963a | 570 | int i, r, pidx; |
c599f53b DSC |
571 | |
572 | mmc = init_mmc_device(curr_device, false); | |
573 | if (!mmc) | |
574 | return CMD_RET_FAILURE; | |
575 | ||
576 | if (argc < 1) | |
577 | return CMD_RET_USAGE; | |
578 | i = 1; | |
579 | while (i < argc) { | |
189f963a DSC |
580 | if (!strcmp(argv[i], "user")) { |
581 | i++; | |
582 | r = parse_hwpart_user(&pconf, argc-i, &argv[i]); | |
583 | if (r < 0) | |
c599f53b | 584 | return CMD_RET_USAGE; |
189f963a | 585 | i += r; |
c599f53b DSC |
586 | } else if (!strncmp(argv[i], "gp", 2) && |
587 | strlen(argv[i]) == 3 && | |
588 | argv[i][2] >= '1' && argv[i][2] <= '4') { | |
c599f53b | 589 | pidx = argv[i][2] - '1'; |
189f963a DSC |
590 | i++; |
591 | r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]); | |
592 | if (r < 0) | |
593 | return CMD_RET_USAGE; | |
594 | i += r; | |
c599f53b DSC |
595 | } else if (!strcmp(argv[i], "check")) { |
596 | mode = MMC_HWPART_CONF_CHECK; | |
597 | i++; | |
598 | } else if (!strcmp(argv[i], "set")) { | |
599 | mode = MMC_HWPART_CONF_SET; | |
600 | i++; | |
601 | } else if (!strcmp(argv[i], "complete")) { | |
602 | mode = MMC_HWPART_CONF_COMPLETE; | |
603 | i++; | |
604 | } else { | |
605 | return CMD_RET_USAGE; | |
606 | } | |
607 | } | |
608 | ||
609 | puts("Partition configuration:\n"); | |
610 | if (pconf.user.enh_size) { | |
611 | puts("\tUser Enhanced Start: "); | |
612 | print_size(((u64)pconf.user.enh_start) << 9, "\n"); | |
613 | puts("\tUser Enhanced Size: "); | |
614 | print_size(((u64)pconf.user.enh_size) << 9, "\n"); | |
615 | } else { | |
616 | puts("\tNo enhanced user data area\n"); | |
617 | } | |
189f963a DSC |
618 | if (pconf.user.wr_rel_change) |
619 | printf("\tUser partition write reliability: %s\n", | |
620 | pconf.user.wr_rel_set ? "on" : "off"); | |
c599f53b DSC |
621 | for (pidx = 0; pidx < 4; pidx++) { |
622 | if (pconf.gp_part[pidx].size) { | |
623 | printf("\tGP%i Capacity: ", pidx+1); | |
624 | print_size(((u64)pconf.gp_part[pidx].size) << 9, | |
625 | pconf.gp_part[pidx].enhanced ? | |
626 | " ENH\n" : "\n"); | |
627 | } else { | |
628 | printf("\tNo GP%i partition\n", pidx+1); | |
629 | } | |
189f963a DSC |
630 | if (pconf.gp_part[pidx].wr_rel_change) |
631 | printf("\tGP%i write reliability: %s\n", pidx+1, | |
632 | pconf.gp_part[pidx].wr_rel_set ? "on" : "off"); | |
c599f53b DSC |
633 | } |
634 | ||
635 | if (!mmc_hwpart_config(mmc, &pconf, mode)) { | |
636 | if (mode == MMC_HWPART_CONF_COMPLETE) | |
637 | puts("Partitioning successful, " | |
638 | "power-cycle to make effective\n"); | |
639 | return CMD_RET_SUCCESS; | |
640 | } else { | |
189f963a | 641 | puts("Failed!\n"); |
c599f53b DSC |
642 | return CMD_RET_FAILURE; |
643 | } | |
644 | } | |
645 | ||
1fd93c6e PA |
646 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
647 | static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag, | |
648 | int argc, char * const argv[]) | |
649 | { | |
650 | int dev; | |
651 | struct mmc *mmc; | |
652 | u8 width, reset, mode; | |
653 | ||
654 | if (argc != 5) | |
655 | return CMD_RET_USAGE; | |
656 | dev = simple_strtoul(argv[1], NULL, 10); | |
657 | width = simple_strtoul(argv[2], NULL, 10); | |
658 | reset = simple_strtoul(argv[3], NULL, 10); | |
659 | mode = simple_strtoul(argv[4], NULL, 10); | |
660 | ||
1ae24a50 | 661 | mmc = init_mmc_device(dev, false); |
1fd93c6e PA |
662 | if (!mmc) |
663 | return CMD_RET_FAILURE; | |
664 | ||
665 | if (IS_SD(mmc)) { | |
666 | puts("BOOT_BUS_WIDTH only exists on eMMC\n"); | |
667 | return CMD_RET_FAILURE; | |
2a91c913 | 668 | } |
ab71188c | 669 | |
1fd93c6e PA |
670 | /* acknowledge to be sent during boot operation */ |
671 | return mmc_set_boot_bus_width(mmc, width, reset, mode); | |
672 | } | |
673 | static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag, | |
674 | int argc, char * const argv[]) | |
675 | { | |
676 | int dev; | |
677 | struct mmc *mmc; | |
678 | u32 bootsize, rpmbsize; | |
ab71188c | 679 | |
1fd93c6e PA |
680 | if (argc != 4) |
681 | return CMD_RET_USAGE; | |
682 | dev = simple_strtoul(argv[1], NULL, 10); | |
683 | bootsize = simple_strtoul(argv[2], NULL, 10); | |
684 | rpmbsize = simple_strtoul(argv[3], NULL, 10); | |
685 | ||
1ae24a50 | 686 | mmc = init_mmc_device(dev, false); |
1fd93c6e PA |
687 | if (!mmc) |
688 | return CMD_RET_FAILURE; | |
689 | ||
690 | if (IS_SD(mmc)) { | |
691 | printf("It is not a EMMC device\n"); | |
692 | return CMD_RET_FAILURE; | |
ab71188c MN |
693 | } |
694 | ||
1fd93c6e PA |
695 | if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) { |
696 | printf("EMMC boot partition Size change Failed.\n"); | |
697 | return CMD_RET_FAILURE; | |
698 | } | |
e85649c7 | 699 | |
1fd93c6e PA |
700 | printf("EMMC boot partition Size %d MB\n", bootsize); |
701 | printf("EMMC RPMB partition Size %d MB\n", rpmbsize); | |
702 | return CMD_RET_SUCCESS; | |
703 | } | |
704 | static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag, | |
705 | int argc, char * const argv[]) | |
706 | { | |
707 | int dev; | |
708 | struct mmc *mmc; | |
709 | u8 ack, part_num, access; | |
272cc70b | 710 | |
1fd93c6e PA |
711 | if (argc != 5) |
712 | return CMD_RET_USAGE; | |
272cc70b | 713 | |
1fd93c6e PA |
714 | dev = simple_strtoul(argv[1], NULL, 10); |
715 | ack = simple_strtoul(argv[2], NULL, 10); | |
716 | part_num = simple_strtoul(argv[3], NULL, 10); | |
717 | access = simple_strtoul(argv[4], NULL, 10); | |
d23d8d7e | 718 | |
1ae24a50 | 719 | mmc = init_mmc_device(dev, false); |
1fd93c6e PA |
720 | if (!mmc) |
721 | return CMD_RET_FAILURE; | |
722 | ||
723 | if (IS_SD(mmc)) { | |
724 | puts("PARTITION_CONFIG only exists on eMMC\n"); | |
725 | return CMD_RET_FAILURE; | |
726 | } | |
727 | ||
728 | /* acknowledge to be sent during boot operation */ | |
729 | return mmc_set_part_conf(mmc, ack, part_num, access); | |
730 | } | |
731 | static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag, | |
732 | int argc, char * const argv[]) | |
733 | { | |
734 | int dev; | |
735 | struct mmc *mmc; | |
736 | u8 enable; | |
737 | ||
738 | /* | |
739 | * Set the RST_n_ENABLE bit of RST_n_FUNCTION | |
740 | * The only valid values are 0x0, 0x1 and 0x2 and writing | |
741 | * a value of 0x1 or 0x2 sets the value permanently. | |
742 | */ | |
743 | if (argc != 3) | |
744 | return CMD_RET_USAGE; | |
745 | ||
746 | dev = simple_strtoul(argv[1], NULL, 10); | |
747 | enable = simple_strtoul(argv[2], NULL, 10); | |
748 | ||
749 | if (enable > 2 || enable < 0) { | |
750 | puts("Invalid RST_n_ENABLE value\n"); | |
751 | return CMD_RET_USAGE; | |
752 | } | |
753 | ||
1ae24a50 | 754 | mmc = init_mmc_device(dev, false); |
1fd93c6e PA |
755 | if (!mmc) |
756 | return CMD_RET_FAILURE; | |
757 | ||
758 | if (IS_SD(mmc)) { | |
759 | puts("RST_n_FUNCTION only exists on eMMC\n"); | |
760 | return CMD_RET_FAILURE; | |
761 | } | |
272cc70b | 762 | |
1fd93c6e PA |
763 | return mmc_set_rst_n_function(mmc, enable); |
764 | } | |
765 | #endif | |
766 | static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag, | |
767 | int argc, char * const argv[]) | |
768 | { | |
769 | struct mmc *mmc; | |
770 | u32 val; | |
771 | int ret; | |
772 | ||
773 | if (argc != 2) | |
774 | return CMD_RET_USAGE; | |
775 | val = simple_strtoul(argv[2], NULL, 16); | |
776 | ||
777 | mmc = find_mmc_device(curr_device); | |
778 | if (!mmc) { | |
779 | printf("no mmc device at slot %x\n", curr_device); | |
780 | return CMD_RET_FAILURE; | |
781 | } | |
782 | ret = mmc_set_dsr(mmc, val); | |
783 | printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR"); | |
784 | if (!ret) { | |
785 | mmc->has_init = 0; | |
786 | if (mmc_init(mmc)) | |
787 | return CMD_RET_FAILURE; | |
788 | else | |
789 | return CMD_RET_SUCCESS; | |
272cc70b | 790 | } |
1fd93c6e PA |
791 | return ret; |
792 | } | |
793 | ||
794 | static cmd_tbl_t cmd_mmc[] = { | |
795 | U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""), | |
796 | U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""), | |
797 | U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""), | |
798 | U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""), | |
799 | U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""), | |
800 | U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""), | |
801 | U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""), | |
802 | U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""), | |
189f963a | 803 | U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""), |
1fd93c6e PA |
804 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
805 | U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""), | |
fa7b8851 | 806 | U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""), |
1fd93c6e PA |
807 | U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""), |
808 | U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""), | |
809 | #endif | |
810 | #ifdef CONFIG_SUPPORT_EMMC_RPMB | |
811 | U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""), | |
812 | #endif | |
813 | U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""), | |
814 | }; | |
815 | ||
816 | static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
817 | { | |
818 | cmd_tbl_t *cp; | |
819 | ||
820 | cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc)); | |
821 | ||
822 | /* Drop the mmc command */ | |
823 | argc--; | |
824 | argv++; | |
825 | ||
826 | if (cp == NULL || argc > cp->maxargs) | |
827 | return CMD_RET_USAGE; | |
828 | if (flag == CMD_FLAG_REPEAT && !cp->repeatable) | |
829 | return CMD_RET_SUCCESS; | |
ea6ebe21 | 830 | |
1fd93c6e PA |
831 | if (curr_device < 0) { |
832 | if (get_mmc_num() > 0) { | |
833 | curr_device = 0; | |
834 | } else { | |
835 | puts("No MMC device available\n"); | |
836 | return CMD_RET_FAILURE; | |
837 | } | |
838 | } | |
839 | return cp->cmd(cmdtp, flag, argc, argv); | |
272cc70b AF |
840 | } |
841 | ||
842 | U_BOOT_CMD( | |
189f963a | 843 | mmc, 29, 1, do_mmcops, |
852dbfdd | 844 | "MMC sub system", |
1fd93c6e PA |
845 | "info - display info of the current MMC device\n" |
846 | "mmc read addr blk# cnt\n" | |
ea6ebe21 | 847 | "mmc write addr blk# cnt\n" |
e6f99a56 | 848 | "mmc erase blk# cnt\n" |
ea6ebe21 LW |
849 | "mmc rescan\n" |
850 | "mmc part - lists available partition on current mmc device\n" | |
bc897b1d | 851 | "mmc dev [dev] [part] - show or set current mmc device [partition]\n" |
2a91c913 | 852 | "mmc list - lists available devices\n" |
c599f53b DSC |
853 | "mmc hwpartition [args...] - does hardware partitioning\n" |
854 | " arguments (sizes in 512-byte blocks):\n" | |
189f963a DSC |
855 | " [user [enh start cnt] [wrrel {on|off}]] - sets user data area attributes\n" |
856 | " [gp1|gp2|gp3|gp4 cnt [enh] [wrrel {on|off}]] - general purpose partition\n" | |
c599f53b | 857 | " [check|set|complete] - mode, complete set partitioning completed\n" |
189f963a DSC |
858 | " WARNING: Partitioning is a write-once setting once it is set to complete.\n" |
859 | " Power cycling is required to initialize partitions after set to complete.\n" | |
2a91c913 | 860 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
5a99b9de TR |
861 | "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n" |
862 | " - Set the BOOT_BUS_WIDTH field of the specified device\n" | |
f1fd957e TR |
863 | "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n" |
864 | " - Change sizes of boot and RPMB partitions of specified device\n" | |
792970b0 TR |
865 | "mmc partconf dev boot_ack boot_partition partition_access\n" |
866 | " - Change the bits of the PARTITION_CONFIG field of the specified device\n" | |
33ace362 TR |
867 | "mmc rst-function dev value\n" |
868 | " - Change the RST_n_FUNCTION field of the specified device\n" | |
869 | " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n" | |
3511b4e2 | 870 | #endif |
1fd93c6e PA |
871 | #ifdef CONFIG_SUPPORT_EMMC_RPMB |
872 | "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n" | |
873 | "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n" | |
874 | "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n" | |
875 | "mmc rpmb counter - read the value of the write counter\n" | |
876 | #endif | |
877 | "mmc setdsr <value> - set DSR register value\n" | |
2a91c913 | 878 | ); |
1fd93c6e PA |
879 | |
880 | /* Old command kept for compatibility. Same as 'mmc info' */ | |
881 | U_BOOT_CMD( | |
882 | mmcinfo, 1, 0, do_mmcinfo, | |
883 | "display MMC info", | |
884 | "- display info of the current MMC device" | |
885 | ); | |
886 | ||
2a91c913 | 887 | #endif /* !CONFIG_GENERIC_MMC */ |