mmc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2003
  4. * Kyle Harris, kharris@nexus-tech.net
  5. */
  6. #include <common.h>
  7. #include <blk.h>
  8. #include <command.h>
  9. #include <console.h>
  10. #include <memalign.h>
  11. #include <mmc.h>
  12. #include <part.h>
  13. #include <sparse_format.h>
  14. #include <image-sparse.h>
  15. static int curr_device = -1;
  16. static void print_mmcinfo(struct mmc *mmc)
  17. {
  18. int i;
  19. printf("Device: %s\n", mmc->cfg->name);
  20. printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
  21. printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
  22. printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
  23. (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
  24. (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
  25. printf("Bus Speed: %d\n", mmc->clock);
  26. #if CONFIG_IS_ENABLED(MMC_VERBOSE)
  27. printf("Mode: %s\n", mmc_mode_name(mmc->selected_mode));
  28. mmc_dump_capabilities("card capabilities", mmc->card_caps);
  29. mmc_dump_capabilities("host capabilities", mmc->host_caps);
  30. #endif
  31. printf("Rd Block Len: %d\n", mmc->read_bl_len);
  32. printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
  33. EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
  34. EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
  35. if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
  36. printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
  37. printf("\n");
  38. printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
  39. puts("Capacity: ");
  40. print_size(mmc->capacity, "\n");
  41. printf("Bus Width: %d-bit%s\n", mmc->bus_width,
  42. mmc->ddr_mode ? " DDR" : "");
  43. #if CONFIG_IS_ENABLED(MMC_WRITE)
  44. puts("Erase Group Size: ");
  45. print_size(((u64)mmc->erase_grp_size) << 9, "\n");
  46. #endif
  47. if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
  48. bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
  49. bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
  50. ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
  51. u8 wp;
  52. int ret;
  53. #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
  54. puts("HC WP Group Size: ");
  55. print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
  56. #endif
  57. puts("User Capacity: ");
  58. print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
  59. if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
  60. puts(" WRREL\n");
  61. else
  62. putc('\n');
  63. if (usr_enh) {
  64. puts("User Enhanced Start: ");
  65. print_size(mmc->enh_user_start, "\n");
  66. puts("User Enhanced Size: ");
  67. print_size(mmc->enh_user_size, "\n");
  68. }
  69. puts("Boot Capacity: ");
  70. print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
  71. puts("RPMB Capacity: ");
  72. print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
  73. for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
  74. bool is_enh = has_enh &&
  75. (mmc->part_attr & EXT_CSD_ENH_GP(i));
  76. if (mmc->capacity_gp[i]) {
  77. printf("GP%i Capacity: ", i+1);
  78. print_size(mmc->capacity_gp[i],
  79. is_enh ? " ENH" : "");
  80. if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
  81. puts(" WRREL\n");
  82. else
  83. putc('\n');
  84. }
  85. }
  86. ret = mmc_send_ext_csd(mmc, ext_csd);
  87. if (ret)
  88. return;
  89. wp = ext_csd[EXT_CSD_BOOT_WP_STATUS];
  90. for (i = 0; i < 2; ++i) {
  91. printf("Boot area %d is ", i);
  92. switch (wp & 3) {
  93. case 0:
  94. printf("not write protected\n");
  95. break;
  96. case 1:
  97. printf("power on protected\n");
  98. break;
  99. case 2:
  100. printf("permanently protected\n");
  101. break;
  102. default:
  103. printf("in reserved protection state\n");
  104. break;
  105. }
  106. wp >>= 2;
  107. }
  108. }
  109. }
  110. static struct mmc *__init_mmc_device(int dev, bool force_init,
  111. enum bus_mode speed_mode)
  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 (!mmc_getcd(mmc))
  120. force_init = true;
  121. if (force_init)
  122. mmc->has_init = 0;
  123. if (IS_ENABLED(CONFIG_MMC_SPEED_MODE_SET))
  124. mmc->user_speed_mode = speed_mode;
  125. if (mmc_init(mmc))
  126. return NULL;
  127. #ifdef CONFIG_BLOCK_CACHE
  128. struct blk_desc *bd = mmc_get_blk_desc(mmc);
  129. blkcache_invalidate(bd->if_type, bd->devnum);
  130. #endif
  131. return mmc;
  132. }
  133. static struct mmc *init_mmc_device(int dev, bool force_init)
  134. {
  135. return __init_mmc_device(dev, force_init, MMC_MODES_END);
  136. }
  137. static int do_mmcinfo(struct cmd_tbl *cmdtp, int flag, int argc,
  138. 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. #if CONFIG_IS_ENABLED(CMD_MMC_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(struct cmd_tbl *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 *)hextoul(argv[1], NULL);
  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(struct cmd_tbl *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 *)hextoul(argv[1], NULL);
  193. blk = hextoul(argv[2], NULL);
  194. cnt = hextoul(argv[3], NULL);
  195. if (argc == 5)
  196. key_addr = (void *)hextoul(argv[4], NULL);
  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(struct cmd_tbl *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 *)hextoul(argv[1], NULL);
  216. blk = hextoul(argv[2], NULL);
  217. cnt = hextoul(argv[3], NULL);
  218. key_addr = (void *)hextoul(argv[4], NULL);
  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(struct cmd_tbl *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 struct cmd_tbl 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(struct cmd_tbl *cmdtp, int flag,
  244. int argc, char *const argv[])
  245. {
  246. struct cmd_tbl *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 && !cmd_is_repeatable(cp))
  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 an 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. #ifndef CONFIG_BLK
  271. original_part = mmc->block_dev.hwpart;
  272. #else
  273. original_part = mmc_get_blk_desc(mmc)->hwpart;
  274. #endif
  275. if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, MMC_PART_RPMB) !=
  276. 0)
  277. return CMD_RET_FAILURE;
  278. ret = cp->cmd(cmdtp, flag, argc, argv);
  279. /* Return to original partition */
  280. if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, original_part) !=
  281. 0)
  282. return CMD_RET_FAILURE;
  283. return ret;
  284. }
  285. #endif
  286. static int do_mmc_read(struct cmd_tbl *cmdtp, int flag,
  287. int argc, char *const argv[])
  288. {
  289. struct mmc *mmc;
  290. u32 blk, cnt, n;
  291. void *addr;
  292. if (argc != 4)
  293. return CMD_RET_USAGE;
  294. addr = (void *)hextoul(argv[1], NULL);
  295. blk = hextoul(argv[2], NULL);
  296. cnt = hextoul(argv[3], NULL);
  297. mmc = init_mmc_device(curr_device, false);
  298. if (!mmc)
  299. return CMD_RET_FAILURE;
  300. printf("\nMMC read: dev # %d, block # %d, count %d ... ",
  301. curr_device, blk, cnt);
  302. n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
  303. printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
  304. return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
  305. }
  306. #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
  307. static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk,
  308. lbaint_t blkcnt, const void *buffer)
  309. {
  310. struct blk_desc *dev_desc = info->priv;
  311. return blk_dwrite(dev_desc, blk, blkcnt, buffer);
  312. }
  313. static lbaint_t mmc_sparse_reserve(struct sparse_storage *info,
  314. lbaint_t blk, lbaint_t blkcnt)
  315. {
  316. return blkcnt;
  317. }
  318. static int do_mmc_sparse_write(struct cmd_tbl *cmdtp, int flag,
  319. int argc, char *const argv[])
  320. {
  321. struct sparse_storage sparse;
  322. struct blk_desc *dev_desc;
  323. struct mmc *mmc;
  324. char dest[11];
  325. void *addr;
  326. u32 blk;
  327. if (argc != 3)
  328. return CMD_RET_USAGE;
  329. addr = (void *)hextoul(argv[1], NULL);
  330. blk = hextoul(argv[2], NULL);
  331. if (!is_sparse_image(addr)) {
  332. printf("Not a sparse image\n");
  333. return CMD_RET_FAILURE;
  334. }
  335. mmc = init_mmc_device(curr_device, false);
  336. if (!mmc)
  337. return CMD_RET_FAILURE;
  338. printf("\nMMC Sparse write: dev # %d, block # %d ... ",
  339. curr_device, blk);
  340. if (mmc_getwp(mmc) == 1) {
  341. printf("Error: card is write protected!\n");
  342. return CMD_RET_FAILURE;
  343. }
  344. dev_desc = mmc_get_blk_desc(mmc);
  345. sparse.priv = dev_desc;
  346. sparse.blksz = 512;
  347. sparse.start = blk;
  348. sparse.size = dev_desc->lba - blk;
  349. sparse.write = mmc_sparse_write;
  350. sparse.reserve = mmc_sparse_reserve;
  351. sparse.mssg = NULL;
  352. sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz);
  353. if (write_sparse_image(&sparse, dest, addr, NULL))
  354. return CMD_RET_FAILURE;
  355. else
  356. return CMD_RET_SUCCESS;
  357. }
  358. #endif
  359. #if CONFIG_IS_ENABLED(MMC_WRITE)
  360. static int do_mmc_write(struct cmd_tbl *cmdtp, int flag,
  361. int argc, char *const argv[])
  362. {
  363. struct mmc *mmc;
  364. u32 blk, cnt, n;
  365. void *addr;
  366. if (argc != 4)
  367. return CMD_RET_USAGE;
  368. addr = (void *)hextoul(argv[1], NULL);
  369. blk = hextoul(argv[2], NULL);
  370. cnt = hextoul(argv[3], NULL);
  371. mmc = init_mmc_device(curr_device, false);
  372. if (!mmc)
  373. return CMD_RET_FAILURE;
  374. printf("\nMMC write: dev # %d, block # %d, count %d ... ",
  375. curr_device, blk, cnt);
  376. if (mmc_getwp(mmc) == 1) {
  377. printf("Error: card is write protected!\n");
  378. return CMD_RET_FAILURE;
  379. }
  380. n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
  381. printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
  382. return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
  383. }
  384. static int do_mmc_erase(struct cmd_tbl *cmdtp, int flag,
  385. int argc, char *const argv[])
  386. {
  387. struct mmc *mmc;
  388. u32 blk, cnt, n;
  389. if (argc != 3)
  390. return CMD_RET_USAGE;
  391. blk = hextoul(argv[1], NULL);
  392. cnt = hextoul(argv[2], NULL);
  393. mmc = init_mmc_device(curr_device, false);
  394. if (!mmc)
  395. return CMD_RET_FAILURE;
  396. printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
  397. curr_device, blk, cnt);
  398. if (mmc_getwp(mmc) == 1) {
  399. printf("Error: card is write protected!\n");
  400. return CMD_RET_FAILURE;
  401. }
  402. n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
  403. printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
  404. return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
  405. }
  406. #endif
  407. static int do_mmc_rescan(struct cmd_tbl *cmdtp, int flag,
  408. int argc, char *const argv[])
  409. {
  410. struct mmc *mmc;
  411. enum bus_mode speed_mode = MMC_MODES_END;
  412. if (argc == 1) {
  413. mmc = init_mmc_device(curr_device, true);
  414. } else if (argc == 2) {
  415. speed_mode = (int)dectoul(argv[1], NULL);
  416. mmc = __init_mmc_device(curr_device, true, speed_mode);
  417. } else {
  418. return CMD_RET_USAGE;
  419. }
  420. if (!mmc)
  421. return CMD_RET_FAILURE;
  422. return CMD_RET_SUCCESS;
  423. }
  424. static int do_mmc_part(struct cmd_tbl *cmdtp, int flag,
  425. int argc, char *const argv[])
  426. {
  427. struct blk_desc *mmc_dev;
  428. struct mmc *mmc;
  429. mmc = init_mmc_device(curr_device, false);
  430. if (!mmc)
  431. return CMD_RET_FAILURE;
  432. mmc_dev = blk_get_devnum_by_type(IF_TYPE_MMC, curr_device);
  433. if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
  434. part_print(mmc_dev);
  435. return CMD_RET_SUCCESS;
  436. }
  437. puts("get mmc type error!\n");
  438. return CMD_RET_FAILURE;
  439. }
  440. static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag,
  441. int argc, char *const argv[])
  442. {
  443. int dev, part = 0, ret;
  444. struct mmc *mmc;
  445. enum bus_mode speed_mode = MMC_MODES_END;
  446. if (argc == 1) {
  447. dev = curr_device;
  448. mmc = init_mmc_device(dev, true);
  449. } else if (argc == 2) {
  450. dev = (int)dectoul(argv[1], NULL);
  451. mmc = init_mmc_device(dev, true);
  452. } else if (argc == 3) {
  453. dev = (int)dectoul(argv[1], NULL);
  454. part = (int)dectoul(argv[2], NULL);
  455. if (part > PART_ACCESS_MASK) {
  456. printf("#part_num shouldn't be larger than %d\n",
  457. PART_ACCESS_MASK);
  458. return CMD_RET_FAILURE;
  459. }
  460. mmc = init_mmc_device(dev, true);
  461. } else if (argc == 4) {
  462. dev = (int)dectoul(argv[1], NULL);
  463. part = (int)dectoul(argv[2], NULL);
  464. if (part > PART_ACCESS_MASK) {
  465. printf("#part_num shouldn't be larger than %d\n",
  466. PART_ACCESS_MASK);
  467. return CMD_RET_FAILURE;
  468. }
  469. speed_mode = (int)dectoul(argv[3], NULL);
  470. mmc = __init_mmc_device(dev, true, speed_mode);
  471. } else {
  472. return CMD_RET_USAGE;
  473. }
  474. if (!mmc)
  475. return CMD_RET_FAILURE;
  476. ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
  477. printf("switch to partitions #%d, %s\n",
  478. part, (!ret) ? "OK" : "ERROR");
  479. if (ret)
  480. return 1;
  481. curr_device = dev;
  482. if (mmc->part_config == MMCPART_NOAVAILABLE)
  483. printf("mmc%d is current device\n", curr_device);
  484. else
  485. printf("mmc%d(part %d) is current device\n",
  486. curr_device, mmc_get_blk_desc(mmc)->hwpart);
  487. return CMD_RET_SUCCESS;
  488. }
  489. static int do_mmc_list(struct cmd_tbl *cmdtp, int flag,
  490. int argc, char *const argv[])
  491. {
  492. print_mmc_devices('\n');
  493. return CMD_RET_SUCCESS;
  494. }
  495. #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
  496. static int parse_hwpart_user(struct mmc_hwpart_conf *pconf,
  497. int argc, char *const argv[])
  498. {
  499. int i = 0;
  500. memset(&pconf->user, 0, sizeof(pconf->user));
  501. while (i < argc) {
  502. if (!strcmp(argv[i], "enh")) {
  503. if (i + 2 >= argc)
  504. return -1;
  505. pconf->user.enh_start =
  506. dectoul(argv[i + 1], NULL);
  507. pconf->user.enh_size =
  508. dectoul(argv[i + 2], NULL);
  509. i += 3;
  510. } else if (!strcmp(argv[i], "wrrel")) {
  511. if (i + 1 >= argc)
  512. return -1;
  513. pconf->user.wr_rel_change = 1;
  514. if (!strcmp(argv[i+1], "on"))
  515. pconf->user.wr_rel_set = 1;
  516. else if (!strcmp(argv[i+1], "off"))
  517. pconf->user.wr_rel_set = 0;
  518. else
  519. return -1;
  520. i += 2;
  521. } else {
  522. break;
  523. }
  524. }
  525. return i;
  526. }
  527. static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
  528. int argc, char *const argv[])
  529. {
  530. int i;
  531. memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
  532. if (1 >= argc)
  533. return -1;
  534. pconf->gp_part[pidx].size = dectoul(argv[0], NULL);
  535. i = 1;
  536. while (i < argc) {
  537. if (!strcmp(argv[i], "enh")) {
  538. pconf->gp_part[pidx].enhanced = 1;
  539. i += 1;
  540. } else if (!strcmp(argv[i], "wrrel")) {
  541. if (i + 1 >= argc)
  542. return -1;
  543. pconf->gp_part[pidx].wr_rel_change = 1;
  544. if (!strcmp(argv[i+1], "on"))
  545. pconf->gp_part[pidx].wr_rel_set = 1;
  546. else if (!strcmp(argv[i+1], "off"))
  547. pconf->gp_part[pidx].wr_rel_set = 0;
  548. else
  549. return -1;
  550. i += 2;
  551. } else {
  552. break;
  553. }
  554. }
  555. return i;
  556. }
  557. static int do_mmc_hwpartition(struct cmd_tbl *cmdtp, int flag,
  558. int argc, char *const argv[])
  559. {
  560. struct mmc *mmc;
  561. struct mmc_hwpart_conf pconf = { };
  562. enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
  563. int i, r, pidx;
  564. mmc = init_mmc_device(curr_device, false);
  565. if (!mmc)
  566. return CMD_RET_FAILURE;
  567. if (argc < 1)
  568. return CMD_RET_USAGE;
  569. i = 1;
  570. while (i < argc) {
  571. if (!strcmp(argv[i], "user")) {
  572. i++;
  573. r = parse_hwpart_user(&pconf, argc-i, &argv[i]);
  574. if (r < 0)
  575. return CMD_RET_USAGE;
  576. i += r;
  577. } else if (!strncmp(argv[i], "gp", 2) &&
  578. strlen(argv[i]) == 3 &&
  579. argv[i][2] >= '1' && argv[i][2] <= '4') {
  580. pidx = argv[i][2] - '1';
  581. i++;
  582. r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
  583. if (r < 0)
  584. return CMD_RET_USAGE;
  585. i += r;
  586. } else if (!strcmp(argv[i], "check")) {
  587. mode = MMC_HWPART_CONF_CHECK;
  588. i++;
  589. } else if (!strcmp(argv[i], "set")) {
  590. mode = MMC_HWPART_CONF_SET;
  591. i++;
  592. } else if (!strcmp(argv[i], "complete")) {
  593. mode = MMC_HWPART_CONF_COMPLETE;
  594. i++;
  595. } else {
  596. return CMD_RET_USAGE;
  597. }
  598. }
  599. puts("Partition configuration:\n");
  600. if (pconf.user.enh_size) {
  601. puts("\tUser Enhanced Start: ");
  602. print_size(((u64)pconf.user.enh_start) << 9, "\n");
  603. puts("\tUser Enhanced Size: ");
  604. print_size(((u64)pconf.user.enh_size) << 9, "\n");
  605. } else {
  606. puts("\tNo enhanced user data area\n");
  607. }
  608. if (pconf.user.wr_rel_change)
  609. printf("\tUser partition write reliability: %s\n",
  610. pconf.user.wr_rel_set ? "on" : "off");
  611. for (pidx = 0; pidx < 4; pidx++) {
  612. if (pconf.gp_part[pidx].size) {
  613. printf("\tGP%i Capacity: ", pidx+1);
  614. print_size(((u64)pconf.gp_part[pidx].size) << 9,
  615. pconf.gp_part[pidx].enhanced ?
  616. " ENH\n" : "\n");
  617. } else {
  618. printf("\tNo GP%i partition\n", pidx+1);
  619. }
  620. if (pconf.gp_part[pidx].wr_rel_change)
  621. printf("\tGP%i write reliability: %s\n", pidx+1,
  622. pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
  623. }
  624. if (!mmc_hwpart_config(mmc, &pconf, mode)) {
  625. if (mode == MMC_HWPART_CONF_COMPLETE)
  626. puts("Partitioning successful, "
  627. "power-cycle to make effective\n");
  628. return CMD_RET_SUCCESS;
  629. } else {
  630. puts("Failed!\n");
  631. return CMD_RET_FAILURE;
  632. }
  633. }
  634. #endif
  635. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  636. static int do_mmc_bootbus(struct cmd_tbl *cmdtp, int flag,
  637. int argc, char *const argv[])
  638. {
  639. int dev;
  640. struct mmc *mmc;
  641. u8 width, reset, mode;
  642. if (argc != 5)
  643. return CMD_RET_USAGE;
  644. dev = dectoul(argv[1], NULL);
  645. width = dectoul(argv[2], NULL);
  646. reset = dectoul(argv[3], NULL);
  647. mode = dectoul(argv[4], NULL);
  648. mmc = init_mmc_device(dev, false);
  649. if (!mmc)
  650. return CMD_RET_FAILURE;
  651. if (IS_SD(mmc)) {
  652. puts("BOOT_BUS_WIDTH only exists on eMMC\n");
  653. return CMD_RET_FAILURE;
  654. }
  655. /*
  656. * BOOT_BUS_CONDITIONS[177]
  657. * BOOT_MODE[4:3]
  658. * 0x0 : Use SDR + Backward compatible timing in boot operation
  659. * 0x1 : Use SDR + High Speed Timing in boot operation mode
  660. * 0x2 : Use DDR in boot operation
  661. * RESET_BOOT_BUS_CONDITIONS
  662. * 0x0 : Reset bus width to x1, SDR, Backward compatible
  663. * 0x1 : Retain BOOT_BUS_WIDTH and BOOT_MODE
  664. * BOOT_BUS_WIDTH
  665. * 0x0 : x1(sdr) or x4 (ddr) buswidth
  666. * 0x1 : x4(sdr/ddr) buswith
  667. * 0x2 : x8(sdr/ddr) buswith
  668. *
  669. */
  670. if (width >= 0x3) {
  671. printf("boot_bus_width %d is invalid\n", width);
  672. return CMD_RET_FAILURE;
  673. }
  674. if (reset >= 0x2) {
  675. printf("reset_boot_bus_width %d is invalid\n", reset);
  676. return CMD_RET_FAILURE;
  677. }
  678. if (mode >= 0x3) {
  679. printf("reset_boot_bus_width %d is invalid\n", mode);
  680. return CMD_RET_FAILURE;
  681. }
  682. /* acknowledge to be sent during boot operation */
  683. if (mmc_set_boot_bus_width(mmc, width, reset, mode)) {
  684. puts("BOOT_BUS_WIDTH is failed to change.\n");
  685. return CMD_RET_FAILURE;
  686. }
  687. printf("Set to BOOT_BUS_WIDTH = 0x%x, RESET = 0x%x, BOOT_MODE = 0x%x\n",
  688. width, reset, mode);
  689. return CMD_RET_SUCCESS;
  690. }
  691. static int do_mmc_boot_resize(struct cmd_tbl *cmdtp, int flag,
  692. int argc, char *const argv[])
  693. {
  694. int dev;
  695. struct mmc *mmc;
  696. u32 bootsize, rpmbsize;
  697. if (argc != 4)
  698. return CMD_RET_USAGE;
  699. dev = dectoul(argv[1], NULL);
  700. bootsize = dectoul(argv[2], NULL);
  701. rpmbsize = dectoul(argv[3], NULL);
  702. mmc = init_mmc_device(dev, false);
  703. if (!mmc)
  704. return CMD_RET_FAILURE;
  705. if (IS_SD(mmc)) {
  706. printf("It is not an eMMC device\n");
  707. return CMD_RET_FAILURE;
  708. }
  709. if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
  710. printf("EMMC boot partition Size change Failed.\n");
  711. return CMD_RET_FAILURE;
  712. }
  713. printf("EMMC boot partition Size %d MB\n", bootsize);
  714. printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
  715. return CMD_RET_SUCCESS;
  716. }
  717. static int mmc_partconf_print(struct mmc *mmc, const char *varname)
  718. {
  719. u8 ack, access, part;
  720. if (mmc->part_config == MMCPART_NOAVAILABLE) {
  721. printf("No part_config info for ver. 0x%x\n", mmc->version);
  722. return CMD_RET_FAILURE;
  723. }
  724. access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
  725. ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
  726. part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
  727. if(varname)
  728. env_set_hex(varname, part);
  729. printf("EXT_CSD[179], PARTITION_CONFIG:\n"
  730. "BOOT_ACK: 0x%x\n"
  731. "BOOT_PARTITION_ENABLE: 0x%x\n"
  732. "PARTITION_ACCESS: 0x%x\n", ack, part, access);
  733. return CMD_RET_SUCCESS;
  734. }
  735. static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag,
  736. int argc, char *const argv[])
  737. {
  738. int dev;
  739. struct mmc *mmc;
  740. u8 ack, part_num, access;
  741. if (argc != 2 && argc != 3 && argc != 5)
  742. return CMD_RET_USAGE;
  743. dev = dectoul(argv[1], NULL);
  744. mmc = init_mmc_device(dev, false);
  745. if (!mmc)
  746. return CMD_RET_FAILURE;
  747. if (IS_SD(mmc)) {
  748. puts("PARTITION_CONFIG only exists on eMMC\n");
  749. return CMD_RET_FAILURE;
  750. }
  751. if (argc == 2 || argc == 3)
  752. return mmc_partconf_print(mmc, argc == 3 ? argv[2] : NULL);
  753. ack = dectoul(argv[2], NULL);
  754. part_num = dectoul(argv[3], NULL);
  755. access = dectoul(argv[4], NULL);
  756. /* acknowledge to be sent during boot operation */
  757. return mmc_set_part_conf(mmc, ack, part_num, access);
  758. }
  759. static int do_mmc_rst_func(struct cmd_tbl *cmdtp, int flag,
  760. int argc, char *const argv[])
  761. {
  762. int dev;
  763. struct mmc *mmc;
  764. u8 enable;
  765. /*
  766. * Set the RST_n_ENABLE bit of RST_n_FUNCTION
  767. * The only valid values are 0x0, 0x1 and 0x2 and writing
  768. * a value of 0x1 or 0x2 sets the value permanently.
  769. */
  770. if (argc != 3)
  771. return CMD_RET_USAGE;
  772. dev = dectoul(argv[1], NULL);
  773. enable = dectoul(argv[2], NULL);
  774. if (enable > 2) {
  775. puts("Invalid RST_n_ENABLE value\n");
  776. return CMD_RET_USAGE;
  777. }
  778. mmc = init_mmc_device(dev, false);
  779. if (!mmc)
  780. return CMD_RET_FAILURE;
  781. if (IS_SD(mmc)) {
  782. puts("RST_n_FUNCTION only exists on eMMC\n");
  783. return CMD_RET_FAILURE;
  784. }
  785. return mmc_set_rst_n_function(mmc, enable);
  786. }
  787. #endif
  788. static int do_mmc_setdsr(struct cmd_tbl *cmdtp, int flag,
  789. int argc, char *const argv[])
  790. {
  791. struct mmc *mmc;
  792. u32 val;
  793. int ret;
  794. if (argc != 2)
  795. return CMD_RET_USAGE;
  796. val = hextoul(argv[1], NULL);
  797. mmc = find_mmc_device(curr_device);
  798. if (!mmc) {
  799. printf("no mmc device at slot %x\n", curr_device);
  800. return CMD_RET_FAILURE;
  801. }
  802. ret = mmc_set_dsr(mmc, val);
  803. printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
  804. if (!ret) {
  805. mmc->has_init = 0;
  806. if (mmc_init(mmc))
  807. return CMD_RET_FAILURE;
  808. else
  809. return CMD_RET_SUCCESS;
  810. }
  811. return ret;
  812. }
  813. #ifdef CONFIG_CMD_BKOPS_ENABLE
  814. static int do_mmc_bkops_enable(struct cmd_tbl *cmdtp, int flag,
  815. int argc, char *const argv[])
  816. {
  817. int dev;
  818. struct mmc *mmc;
  819. if (argc != 2)
  820. return CMD_RET_USAGE;
  821. dev = dectoul(argv[1], NULL);
  822. mmc = init_mmc_device(dev, false);
  823. if (!mmc)
  824. return CMD_RET_FAILURE;
  825. if (IS_SD(mmc)) {
  826. puts("BKOPS_EN only exists on eMMC\n");
  827. return CMD_RET_FAILURE;
  828. }
  829. return mmc_set_bkops_enable(mmc);
  830. }
  831. #endif
  832. static int do_mmc_boot_wp(struct cmd_tbl *cmdtp, int flag,
  833. int argc, char * const argv[])
  834. {
  835. int err;
  836. struct mmc *mmc;
  837. mmc = init_mmc_device(curr_device, false);
  838. if (!mmc)
  839. return CMD_RET_FAILURE;
  840. if (IS_SD(mmc)) {
  841. printf("It is not an eMMC device\n");
  842. return CMD_RET_FAILURE;
  843. }
  844. err = mmc_boot_wp(mmc);
  845. if (err)
  846. return CMD_RET_FAILURE;
  847. printf("boot areas protected\n");
  848. return CMD_RET_SUCCESS;
  849. }
  850. static struct cmd_tbl cmd_mmc[] = {
  851. U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
  852. U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
  853. U_BOOT_CMD_MKENT(wp, 1, 0, do_mmc_boot_wp, "", ""),
  854. #if CONFIG_IS_ENABLED(MMC_WRITE)
  855. U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
  856. U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
  857. #endif
  858. #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
  859. U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
  860. #endif
  861. U_BOOT_CMD_MKENT(rescan, 2, 1, do_mmc_rescan, "", ""),
  862. U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
  863. U_BOOT_CMD_MKENT(dev, 4, 0, do_mmc_dev, "", ""),
  864. U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
  865. #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
  866. U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
  867. #endif
  868. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  869. U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
  870. U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
  871. U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
  872. U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
  873. #endif
  874. #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
  875. U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
  876. #endif
  877. U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
  878. #ifdef CONFIG_CMD_BKOPS_ENABLE
  879. U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
  880. #endif
  881. };
  882. static int do_mmcops(struct cmd_tbl *cmdtp, int flag, int argc,
  883. char *const argv[])
  884. {
  885. struct cmd_tbl *cp;
  886. cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
  887. /* Drop the mmc command */
  888. argc--;
  889. argv++;
  890. if (cp == NULL || argc > cp->maxargs)
  891. return CMD_RET_USAGE;
  892. if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
  893. return CMD_RET_SUCCESS;
  894. if (curr_device < 0) {
  895. if (get_mmc_num() > 0) {
  896. curr_device = 0;
  897. } else {
  898. puts("No MMC device available\n");
  899. return CMD_RET_FAILURE;
  900. }
  901. }
  902. return cp->cmd(cmdtp, flag, argc, argv);
  903. }
  904. U_BOOT_CMD(
  905. mmc, 29, 1, do_mmcops,
  906. "MMC sub system",
  907. "info - display info of the current MMC device\n"
  908. "mmc read addr blk# cnt\n"
  909. "mmc write addr blk# cnt\n"
  910. #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
  911. "mmc swrite addr blk#\n"
  912. #endif
  913. "mmc erase blk# cnt\n"
  914. "mmc rescan [mode]\n"
  915. "mmc part - lists available partition on current mmc device\n"
  916. "mmc dev [dev] [part] [mode] - show or set current mmc device [partition] and set mode\n"
  917. " - the required speed mode is passed as the index from the following list\n"
  918. " [MMC_LEGACY, MMC_HS, SD_HS, MMC_HS_52, MMC_DDR_52, UHS_SDR12, UHS_SDR25,\n"
  919. " UHS_SDR50, UHS_DDR50, UHS_SDR104, MMC_HS_200, MMC_HS_400, MMC_HS_400_ES]\n"
  920. "mmc list - lists available devices\n"
  921. "mmc wp - power on write protect boot partitions\n"
  922. #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
  923. "mmc hwpartition <USER> <GP> <MODE> - does hardware partitioning\n"
  924. " arguments (sizes in 512-byte blocks):\n"
  925. " USER - <user> <enh> <start> <cnt> <wrrel> <{on|off}>\n"
  926. " : sets user data area attributes\n"
  927. " GP - <{gp1|gp2|gp3|gp4}> <cnt> <enh> <wrrel> <{on|off}>\n"
  928. " : general purpose partition\n"
  929. " MODE - <{check|set|complete}>\n"
  930. " : mode, complete set partitioning completed\n"
  931. " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
  932. " Power cycling is required to initialize partitions after set to complete.\n"
  933. #endif
  934. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  935. "mmc bootbus <dev> <boot_bus_width> <reset_boot_bus_width> <boot_mode>\n"
  936. " - Set the BOOT_BUS_WIDTH field of the specified device\n"
  937. "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
  938. " - Change sizes of boot and RPMB partitions of specified device\n"
  939. "mmc partconf <dev> [[varname] | [<boot_ack> <boot_partition> <partition_access>]]\n"
  940. " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
  941. " If showing the bits, optionally store the boot_partition field into varname\n"
  942. "mmc rst-function <dev> <value>\n"
  943. " - Change the RST_n_FUNCTION field of the specified device\n"
  944. " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
  945. #endif
  946. #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
  947. "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
  948. "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
  949. "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
  950. "mmc rpmb counter - read the value of the write counter\n"
  951. #endif
  952. "mmc setdsr <value> - set DSR register value\n"
  953. #ifdef CONFIG_CMD_BKOPS_ENABLE
  954. "mmc bkops-enable <dev> - enable background operations handshake on device\n"
  955. " WARNING: This is a write-once setting.\n"
  956. #endif
  957. );
  958. /* Old command kept for compatibility. Same as 'mmc info' */
  959. U_BOOT_CMD(
  960. mmcinfo, 1, 0, do_mmcinfo,
  961. "display MMC info",
  962. "- display info of the current MMC device"
  963. );