mmc.c 22 KB

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