cmd_mmc.c 21 KB

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