mmc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. {
  112. struct mmc *mmc;
  113. mmc = find_mmc_device(dev);
  114. if (!mmc) {
  115. printf("no mmc device at slot %x\n", dev);
  116. return NULL;
  117. }
  118. if (!mmc_getcd(mmc))
  119. force_init = true;
  120. if (force_init)
  121. mmc->has_init = 0;
  122. if (mmc_init(mmc))
  123. return NULL;
  124. #ifdef CONFIG_BLOCK_CACHE
  125. struct blk_desc *bd = mmc_get_blk_desc(mmc);
  126. blkcache_invalidate(bd->if_type, bd->devnum);
  127. #endif
  128. return mmc;
  129. }
  130. static int do_mmcinfo(struct cmd_tbl *cmdtp, int flag, int argc,
  131. char *const argv[])
  132. {
  133. struct mmc *mmc;
  134. if (curr_device < 0) {
  135. if (get_mmc_num() > 0)
  136. curr_device = 0;
  137. else {
  138. puts("No MMC device available\n");
  139. return 1;
  140. }
  141. }
  142. mmc = init_mmc_device(curr_device, false);
  143. if (!mmc)
  144. return CMD_RET_FAILURE;
  145. print_mmcinfo(mmc);
  146. return CMD_RET_SUCCESS;
  147. }
  148. #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
  149. static int confirm_key_prog(void)
  150. {
  151. puts("Warning: Programming authentication key can be done only once !\n"
  152. " Use this command only if you are sure of what you are doing,\n"
  153. "Really perform the key programming? <y/N> ");
  154. if (confirm_yesno())
  155. return 1;
  156. puts("Authentication key programming aborted\n");
  157. return 0;
  158. }
  159. static int do_mmcrpmb_key(struct cmd_tbl *cmdtp, int flag,
  160. int argc, char *const argv[])
  161. {
  162. void *key_addr;
  163. struct mmc *mmc = find_mmc_device(curr_device);
  164. if (argc != 2)
  165. return CMD_RET_USAGE;
  166. key_addr = (void *)hextoul(argv[1], NULL);
  167. if (!confirm_key_prog())
  168. return CMD_RET_FAILURE;
  169. if (mmc_rpmb_set_key(mmc, key_addr)) {
  170. printf("ERROR - Key already programmed ?\n");
  171. return CMD_RET_FAILURE;
  172. }
  173. return CMD_RET_SUCCESS;
  174. }
  175. static int do_mmcrpmb_read(struct cmd_tbl *cmdtp, int flag,
  176. int argc, char *const argv[])
  177. {
  178. u16 blk, cnt;
  179. void *addr;
  180. int n;
  181. void *key_addr = NULL;
  182. struct mmc *mmc = find_mmc_device(curr_device);
  183. if (argc < 4)
  184. return CMD_RET_USAGE;
  185. addr = (void *)hextoul(argv[1], NULL);
  186. blk = hextoul(argv[2], NULL);
  187. cnt = hextoul(argv[3], NULL);
  188. if (argc == 5)
  189. key_addr = (void *)hextoul(argv[4], NULL);
  190. printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
  191. curr_device, blk, cnt);
  192. n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
  193. printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
  194. if (n != cnt)
  195. return CMD_RET_FAILURE;
  196. return CMD_RET_SUCCESS;
  197. }
  198. static int do_mmcrpmb_write(struct cmd_tbl *cmdtp, int flag,
  199. int argc, char *const argv[])
  200. {
  201. u16 blk, cnt;
  202. void *addr;
  203. int n;
  204. void *key_addr;
  205. struct mmc *mmc = find_mmc_device(curr_device);
  206. if (argc != 5)
  207. return CMD_RET_USAGE;
  208. addr = (void *)hextoul(argv[1], NULL);
  209. blk = hextoul(argv[2], NULL);
  210. cnt = hextoul(argv[3], NULL);
  211. key_addr = (void *)hextoul(argv[4], NULL);
  212. printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
  213. curr_device, blk, cnt);
  214. n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
  215. printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
  216. if (n != cnt)
  217. return CMD_RET_FAILURE;
  218. return CMD_RET_SUCCESS;
  219. }
  220. static int do_mmcrpmb_counter(struct cmd_tbl *cmdtp, int flag,
  221. int argc, char *const argv[])
  222. {
  223. unsigned long counter;
  224. struct mmc *mmc = find_mmc_device(curr_device);
  225. if (mmc_rpmb_get_counter(mmc, &counter))
  226. return CMD_RET_FAILURE;
  227. printf("RPMB Write counter= %lx\n", counter);
  228. return CMD_RET_SUCCESS;
  229. }
  230. static struct cmd_tbl cmd_rpmb[] = {
  231. U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
  232. U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
  233. U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
  234. U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
  235. };
  236. static int do_mmcrpmb(struct cmd_tbl *cmdtp, int flag,
  237. int argc, char *const argv[])
  238. {
  239. struct cmd_tbl *cp;
  240. struct mmc *mmc;
  241. char original_part;
  242. int ret;
  243. cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
  244. /* Drop the rpmb subcommand */
  245. argc--;
  246. argv++;
  247. if (cp == NULL || argc > cp->maxargs)
  248. return CMD_RET_USAGE;
  249. if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
  250. return CMD_RET_SUCCESS;
  251. mmc = init_mmc_device(curr_device, false);
  252. if (!mmc)
  253. return CMD_RET_FAILURE;
  254. if (!(mmc->version & MMC_VERSION_MMC)) {
  255. printf("It is not an eMMC device\n");
  256. return CMD_RET_FAILURE;
  257. }
  258. if (mmc->version < MMC_VERSION_4_41) {
  259. printf("RPMB not supported before version 4.41\n");
  260. return CMD_RET_FAILURE;
  261. }
  262. /* Switch to the RPMB partition */
  263. #ifndef CONFIG_BLK
  264. original_part = mmc->block_dev.hwpart;
  265. #else
  266. original_part = mmc_get_blk_desc(mmc)->hwpart;
  267. #endif
  268. if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, MMC_PART_RPMB) !=
  269. 0)
  270. return CMD_RET_FAILURE;
  271. ret = cp->cmd(cmdtp, flag, argc, argv);
  272. /* Return to original partition */
  273. if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, original_part) !=
  274. 0)
  275. return CMD_RET_FAILURE;
  276. return ret;
  277. }
  278. #endif
  279. static int do_mmc_read(struct cmd_tbl *cmdtp, int flag,
  280. int argc, char *const argv[])
  281. {
  282. struct mmc *mmc;
  283. u32 blk, cnt, n;
  284. void *addr;
  285. if (argc != 4)
  286. return CMD_RET_USAGE;
  287. addr = (void *)hextoul(argv[1], NULL);
  288. blk = hextoul(argv[2], NULL);
  289. cnt = hextoul(argv[3], NULL);
  290. mmc = init_mmc_device(curr_device, false);
  291. if (!mmc)
  292. return CMD_RET_FAILURE;
  293. printf("\nMMC read: dev # %d, block # %d, count %d ... ",
  294. curr_device, blk, cnt);
  295. n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
  296. printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
  297. return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
  298. }
  299. #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
  300. static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk,
  301. lbaint_t blkcnt, const void *buffer)
  302. {
  303. struct blk_desc *dev_desc = info->priv;
  304. return blk_dwrite(dev_desc, blk, blkcnt, buffer);
  305. }
  306. static lbaint_t mmc_sparse_reserve(struct sparse_storage *info,
  307. lbaint_t blk, lbaint_t blkcnt)
  308. {
  309. return blkcnt;
  310. }
  311. static int do_mmc_sparse_write(struct cmd_tbl *cmdtp, int flag,
  312. int argc, char *const argv[])
  313. {
  314. struct sparse_storage sparse;
  315. struct blk_desc *dev_desc;
  316. struct mmc *mmc;
  317. char dest[11];
  318. void *addr;
  319. u32 blk;
  320. if (argc != 3)
  321. return CMD_RET_USAGE;
  322. addr = (void *)hextoul(argv[1], NULL);
  323. blk = hextoul(argv[2], NULL);
  324. if (!is_sparse_image(addr)) {
  325. printf("Not a sparse image\n");
  326. return CMD_RET_FAILURE;
  327. }
  328. mmc = init_mmc_device(curr_device, false);
  329. if (!mmc)
  330. return CMD_RET_FAILURE;
  331. printf("\nMMC Sparse write: dev # %d, block # %d ... ",
  332. curr_device, blk);
  333. if (mmc_getwp(mmc) == 1) {
  334. printf("Error: card is write protected!\n");
  335. return CMD_RET_FAILURE;
  336. }
  337. dev_desc = mmc_get_blk_desc(mmc);
  338. sparse.priv = dev_desc;
  339. sparse.blksz = 512;
  340. sparse.start = blk;
  341. sparse.size = dev_desc->lba - blk;
  342. sparse.write = mmc_sparse_write;
  343. sparse.reserve = mmc_sparse_reserve;
  344. sparse.mssg = NULL;
  345. sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz);
  346. if (write_sparse_image(&sparse, dest, addr, NULL))
  347. return CMD_RET_FAILURE;
  348. else
  349. return CMD_RET_SUCCESS;
  350. }
  351. #endif
  352. #if CONFIG_IS_ENABLED(MMC_WRITE)
  353. static int do_mmc_write(struct cmd_tbl *cmdtp, int flag,
  354. int argc, char *const argv[])
  355. {
  356. struct mmc *mmc;
  357. u32 blk, cnt, n;
  358. void *addr;
  359. if (argc != 4)
  360. return CMD_RET_USAGE;
  361. addr = (void *)hextoul(argv[1], NULL);
  362. blk = hextoul(argv[2], NULL);
  363. cnt = hextoul(argv[3], NULL);
  364. mmc = init_mmc_device(curr_device, false);
  365. if (!mmc)
  366. return CMD_RET_FAILURE;
  367. printf("\nMMC write: dev # %d, block # %d, count %d ... ",
  368. curr_device, blk, cnt);
  369. if (mmc_getwp(mmc) == 1) {
  370. printf("Error: card is write protected!\n");
  371. return CMD_RET_FAILURE;
  372. }
  373. n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
  374. printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
  375. return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
  376. }
  377. static int do_mmc_erase(struct cmd_tbl *cmdtp, int flag,
  378. int argc, char *const argv[])
  379. {
  380. struct mmc *mmc;
  381. u32 blk, cnt, n;
  382. if (argc != 3)
  383. return CMD_RET_USAGE;
  384. blk = hextoul(argv[1], NULL);
  385. cnt = hextoul(argv[2], NULL);
  386. mmc = init_mmc_device(curr_device, false);
  387. if (!mmc)
  388. return CMD_RET_FAILURE;
  389. printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
  390. curr_device, blk, cnt);
  391. if (mmc_getwp(mmc) == 1) {
  392. printf("Error: card is write protected!\n");
  393. return CMD_RET_FAILURE;
  394. }
  395. n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
  396. printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
  397. return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
  398. }
  399. #endif
  400. static int do_mmc_rescan(struct cmd_tbl *cmdtp, int flag,
  401. int argc, char *const argv[])
  402. {
  403. struct mmc *mmc;
  404. mmc = init_mmc_device(curr_device, true);
  405. if (!mmc)
  406. return CMD_RET_FAILURE;
  407. return CMD_RET_SUCCESS;
  408. }
  409. static int do_mmc_part(struct cmd_tbl *cmdtp, int flag,
  410. int argc, char *const argv[])
  411. {
  412. struct blk_desc *mmc_dev;
  413. struct mmc *mmc;
  414. mmc = init_mmc_device(curr_device, false);
  415. if (!mmc)
  416. return CMD_RET_FAILURE;
  417. mmc_dev = blk_get_devnum_by_type(IF_TYPE_MMC, curr_device);
  418. if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
  419. part_print(mmc_dev);
  420. return CMD_RET_SUCCESS;
  421. }
  422. puts("get mmc type error!\n");
  423. return CMD_RET_FAILURE;
  424. }
  425. static int do_mmc_dev(struct cmd_tbl *cmdtp, int flag,
  426. int argc, char *const argv[])
  427. {
  428. int dev, part = 0, ret;
  429. struct mmc *mmc;
  430. if (argc == 1) {
  431. dev = curr_device;
  432. } else if (argc == 2) {
  433. dev = dectoul(argv[1], NULL);
  434. } else if (argc == 3) {
  435. dev = (int)dectoul(argv[1], NULL);
  436. part = (int)dectoul(argv[2], NULL);
  437. if (part > PART_ACCESS_MASK) {
  438. printf("#part_num shouldn't be larger than %d\n",
  439. PART_ACCESS_MASK);
  440. return CMD_RET_FAILURE;
  441. }
  442. } else {
  443. return CMD_RET_USAGE;
  444. }
  445. mmc = init_mmc_device(dev, true);
  446. if (!mmc)
  447. return CMD_RET_FAILURE;
  448. ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
  449. printf("switch to partitions #%d, %s\n",
  450. part, (!ret) ? "OK" : "ERROR");
  451. if (ret)
  452. return 1;
  453. curr_device = dev;
  454. if (mmc->part_config == MMCPART_NOAVAILABLE)
  455. printf("mmc%d is current device\n", curr_device);
  456. else
  457. printf("mmc%d(part %d) is current device\n",
  458. curr_device, mmc_get_blk_desc(mmc)->hwpart);
  459. return CMD_RET_SUCCESS;
  460. }
  461. static int do_mmc_list(struct cmd_tbl *cmdtp, int flag,
  462. int argc, char *const argv[])
  463. {
  464. print_mmc_devices('\n');
  465. return CMD_RET_SUCCESS;
  466. }
  467. #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
  468. static int parse_hwpart_user(struct mmc_hwpart_conf *pconf,
  469. int argc, char *const argv[])
  470. {
  471. int i = 0;
  472. memset(&pconf->user, 0, sizeof(pconf->user));
  473. while (i < argc) {
  474. if (!strcmp(argv[i], "enh")) {
  475. if (i + 2 >= argc)
  476. return -1;
  477. pconf->user.enh_start =
  478. dectoul(argv[i + 1], NULL);
  479. pconf->user.enh_size =
  480. dectoul(argv[i + 2], NULL);
  481. i += 3;
  482. } else if (!strcmp(argv[i], "wrrel")) {
  483. if (i + 1 >= argc)
  484. return -1;
  485. pconf->user.wr_rel_change = 1;
  486. if (!strcmp(argv[i+1], "on"))
  487. pconf->user.wr_rel_set = 1;
  488. else if (!strcmp(argv[i+1], "off"))
  489. pconf->user.wr_rel_set = 0;
  490. else
  491. return -1;
  492. i += 2;
  493. } else {
  494. break;
  495. }
  496. }
  497. return i;
  498. }
  499. static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
  500. int argc, char *const argv[])
  501. {
  502. int i;
  503. memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
  504. if (1 >= argc)
  505. return -1;
  506. pconf->gp_part[pidx].size = dectoul(argv[0], NULL);
  507. i = 1;
  508. while (i < argc) {
  509. if (!strcmp(argv[i], "enh")) {
  510. pconf->gp_part[pidx].enhanced = 1;
  511. i += 1;
  512. } else if (!strcmp(argv[i], "wrrel")) {
  513. if (i + 1 >= argc)
  514. return -1;
  515. pconf->gp_part[pidx].wr_rel_change = 1;
  516. if (!strcmp(argv[i+1], "on"))
  517. pconf->gp_part[pidx].wr_rel_set = 1;
  518. else if (!strcmp(argv[i+1], "off"))
  519. pconf->gp_part[pidx].wr_rel_set = 0;
  520. else
  521. return -1;
  522. i += 2;
  523. } else {
  524. break;
  525. }
  526. }
  527. return i;
  528. }
  529. static int do_mmc_hwpartition(struct cmd_tbl *cmdtp, int flag,
  530. int argc, char *const argv[])
  531. {
  532. struct mmc *mmc;
  533. struct mmc_hwpart_conf pconf = { };
  534. enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
  535. int i, r, pidx;
  536. mmc = init_mmc_device(curr_device, false);
  537. if (!mmc)
  538. return CMD_RET_FAILURE;
  539. if (argc < 1)
  540. return CMD_RET_USAGE;
  541. i = 1;
  542. while (i < argc) {
  543. if (!strcmp(argv[i], "user")) {
  544. i++;
  545. r = parse_hwpart_user(&pconf, argc-i, &argv[i]);
  546. if (r < 0)
  547. return CMD_RET_USAGE;
  548. i += r;
  549. } else if (!strncmp(argv[i], "gp", 2) &&
  550. strlen(argv[i]) == 3 &&
  551. argv[i][2] >= '1' && argv[i][2] <= '4') {
  552. pidx = argv[i][2] - '1';
  553. i++;
  554. r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
  555. if (r < 0)
  556. return CMD_RET_USAGE;
  557. i += r;
  558. } else if (!strcmp(argv[i], "check")) {
  559. mode = MMC_HWPART_CONF_CHECK;
  560. i++;
  561. } else if (!strcmp(argv[i], "set")) {
  562. mode = MMC_HWPART_CONF_SET;
  563. i++;
  564. } else if (!strcmp(argv[i], "complete")) {
  565. mode = MMC_HWPART_CONF_COMPLETE;
  566. i++;
  567. } else {
  568. return CMD_RET_USAGE;
  569. }
  570. }
  571. puts("Partition configuration:\n");
  572. if (pconf.user.enh_size) {
  573. puts("\tUser Enhanced Start: ");
  574. print_size(((u64)pconf.user.enh_start) << 9, "\n");
  575. puts("\tUser Enhanced Size: ");
  576. print_size(((u64)pconf.user.enh_size) << 9, "\n");
  577. } else {
  578. puts("\tNo enhanced user data area\n");
  579. }
  580. if (pconf.user.wr_rel_change)
  581. printf("\tUser partition write reliability: %s\n",
  582. pconf.user.wr_rel_set ? "on" : "off");
  583. for (pidx = 0; pidx < 4; pidx++) {
  584. if (pconf.gp_part[pidx].size) {
  585. printf("\tGP%i Capacity: ", pidx+1);
  586. print_size(((u64)pconf.gp_part[pidx].size) << 9,
  587. pconf.gp_part[pidx].enhanced ?
  588. " ENH\n" : "\n");
  589. } else {
  590. printf("\tNo GP%i partition\n", pidx+1);
  591. }
  592. if (pconf.gp_part[pidx].wr_rel_change)
  593. printf("\tGP%i write reliability: %s\n", pidx+1,
  594. pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
  595. }
  596. if (!mmc_hwpart_config(mmc, &pconf, mode)) {
  597. if (mode == MMC_HWPART_CONF_COMPLETE)
  598. puts("Partitioning successful, "
  599. "power-cycle to make effective\n");
  600. return CMD_RET_SUCCESS;
  601. } else {
  602. puts("Failed!\n");
  603. return CMD_RET_FAILURE;
  604. }
  605. }
  606. #endif
  607. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  608. static int do_mmc_bootbus(struct cmd_tbl *cmdtp, int flag,
  609. int argc, char *const argv[])
  610. {
  611. int dev;
  612. struct mmc *mmc;
  613. u8 width, reset, mode;
  614. if (argc != 5)
  615. return CMD_RET_USAGE;
  616. dev = dectoul(argv[1], NULL);
  617. width = dectoul(argv[2], NULL);
  618. reset = dectoul(argv[3], NULL);
  619. mode = dectoul(argv[4], NULL);
  620. mmc = init_mmc_device(dev, false);
  621. if (!mmc)
  622. return CMD_RET_FAILURE;
  623. if (IS_SD(mmc)) {
  624. puts("BOOT_BUS_WIDTH only exists on eMMC\n");
  625. return CMD_RET_FAILURE;
  626. }
  627. /*
  628. * BOOT_BUS_CONDITIONS[177]
  629. * BOOT_MODE[4:3]
  630. * 0x0 : Use SDR + Backward compatible timing in boot operation
  631. * 0x1 : Use SDR + High Speed Timing in boot operation mode
  632. * 0x2 : Use DDR in boot operation
  633. * RESET_BOOT_BUS_CONDITIONS
  634. * 0x0 : Reset bus width to x1, SDR, Backward compatible
  635. * 0x1 : Retain BOOT_BUS_WIDTH and BOOT_MODE
  636. * BOOT_BUS_WIDTH
  637. * 0x0 : x1(sdr) or x4 (ddr) buswidth
  638. * 0x1 : x4(sdr/ddr) buswith
  639. * 0x2 : x8(sdr/ddr) buswith
  640. *
  641. */
  642. if (width >= 0x3) {
  643. printf("boot_bus_width %d is invalid\n", width);
  644. return CMD_RET_FAILURE;
  645. }
  646. if (reset >= 0x2) {
  647. printf("reset_boot_bus_width %d is invalid\n", reset);
  648. return CMD_RET_FAILURE;
  649. }
  650. if (mode >= 0x3) {
  651. printf("reset_boot_bus_width %d is invalid\n", mode);
  652. return CMD_RET_FAILURE;
  653. }
  654. /* acknowledge to be sent during boot operation */
  655. if (mmc_set_boot_bus_width(mmc, width, reset, mode)) {
  656. puts("BOOT_BUS_WIDTH is failed to change.\n");
  657. return CMD_RET_FAILURE;
  658. }
  659. printf("Set to BOOT_BUS_WIDTH = 0x%x, RESET = 0x%x, BOOT_MODE = 0x%x\n",
  660. width, reset, mode);
  661. return CMD_RET_SUCCESS;
  662. }
  663. static int do_mmc_boot_resize(struct cmd_tbl *cmdtp, int flag,
  664. int argc, char *const argv[])
  665. {
  666. int dev;
  667. struct mmc *mmc;
  668. u32 bootsize, rpmbsize;
  669. if (argc != 4)
  670. return CMD_RET_USAGE;
  671. dev = dectoul(argv[1], NULL);
  672. bootsize = dectoul(argv[2], NULL);
  673. rpmbsize = dectoul(argv[3], NULL);
  674. mmc = init_mmc_device(dev, false);
  675. if (!mmc)
  676. return CMD_RET_FAILURE;
  677. if (IS_SD(mmc)) {
  678. printf("It is not an eMMC device\n");
  679. return CMD_RET_FAILURE;
  680. }
  681. if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
  682. printf("EMMC boot partition Size change Failed.\n");
  683. return CMD_RET_FAILURE;
  684. }
  685. printf("EMMC boot partition Size %d MB\n", bootsize);
  686. printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
  687. return CMD_RET_SUCCESS;
  688. }
  689. static int mmc_partconf_print(struct mmc *mmc, const char *varname)
  690. {
  691. u8 ack, access, part;
  692. if (mmc->part_config == MMCPART_NOAVAILABLE) {
  693. printf("No part_config info for ver. 0x%x\n", mmc->version);
  694. return CMD_RET_FAILURE;
  695. }
  696. access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
  697. ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
  698. part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
  699. if(varname)
  700. env_set_hex(varname, part);
  701. printf("EXT_CSD[179], PARTITION_CONFIG:\n"
  702. "BOOT_ACK: 0x%x\n"
  703. "BOOT_PARTITION_ENABLE: 0x%x\n"
  704. "PARTITION_ACCESS: 0x%x\n", ack, part, access);
  705. return CMD_RET_SUCCESS;
  706. }
  707. static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag,
  708. int argc, char *const argv[])
  709. {
  710. int dev;
  711. struct mmc *mmc;
  712. u8 ack, part_num, access;
  713. if (argc != 2 && argc != 3 && argc != 5)
  714. return CMD_RET_USAGE;
  715. dev = dectoul(argv[1], NULL);
  716. mmc = init_mmc_device(dev, false);
  717. if (!mmc)
  718. return CMD_RET_FAILURE;
  719. if (IS_SD(mmc)) {
  720. puts("PARTITION_CONFIG only exists on eMMC\n");
  721. return CMD_RET_FAILURE;
  722. }
  723. if (argc == 2 || argc == 3)
  724. return mmc_partconf_print(mmc, argc == 3 ? argv[2] : NULL);
  725. ack = dectoul(argv[2], NULL);
  726. part_num = dectoul(argv[3], NULL);
  727. access = dectoul(argv[4], NULL);
  728. /* acknowledge to be sent during boot operation */
  729. return mmc_set_part_conf(mmc, ack, part_num, access);
  730. }
  731. static int do_mmc_rst_func(struct cmd_tbl *cmdtp, int flag,
  732. int argc, char *const argv[])
  733. {
  734. int dev;
  735. struct mmc *mmc;
  736. u8 enable;
  737. /*
  738. * Set the RST_n_ENABLE bit of RST_n_FUNCTION
  739. * The only valid values are 0x0, 0x1 and 0x2 and writing
  740. * a value of 0x1 or 0x2 sets the value permanently.
  741. */
  742. if (argc != 3)
  743. return CMD_RET_USAGE;
  744. dev = dectoul(argv[1], NULL);
  745. enable = dectoul(argv[2], NULL);
  746. if (enable > 2) {
  747. puts("Invalid RST_n_ENABLE value\n");
  748. return CMD_RET_USAGE;
  749. }
  750. mmc = init_mmc_device(dev, false);
  751. if (!mmc)
  752. return CMD_RET_FAILURE;
  753. if (IS_SD(mmc)) {
  754. puts("RST_n_FUNCTION only exists on eMMC\n");
  755. return CMD_RET_FAILURE;
  756. }
  757. return mmc_set_rst_n_function(mmc, enable);
  758. }
  759. #endif
  760. static int do_mmc_setdsr(struct cmd_tbl *cmdtp, int flag,
  761. int argc, char *const argv[])
  762. {
  763. struct mmc *mmc;
  764. u32 val;
  765. int ret;
  766. if (argc != 2)
  767. return CMD_RET_USAGE;
  768. val = hextoul(argv[1], NULL);
  769. mmc = find_mmc_device(curr_device);
  770. if (!mmc) {
  771. printf("no mmc device at slot %x\n", curr_device);
  772. return CMD_RET_FAILURE;
  773. }
  774. ret = mmc_set_dsr(mmc, val);
  775. printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
  776. if (!ret) {
  777. mmc->has_init = 0;
  778. if (mmc_init(mmc))
  779. return CMD_RET_FAILURE;
  780. else
  781. return CMD_RET_SUCCESS;
  782. }
  783. return ret;
  784. }
  785. #ifdef CONFIG_CMD_BKOPS_ENABLE
  786. static int do_mmc_bkops_enable(struct cmd_tbl *cmdtp, int flag,
  787. int argc, char *const argv[])
  788. {
  789. int dev;
  790. struct mmc *mmc;
  791. if (argc != 2)
  792. return CMD_RET_USAGE;
  793. dev = dectoul(argv[1], NULL);
  794. mmc = init_mmc_device(dev, false);
  795. if (!mmc)
  796. return CMD_RET_FAILURE;
  797. if (IS_SD(mmc)) {
  798. puts("BKOPS_EN only exists on eMMC\n");
  799. return CMD_RET_FAILURE;
  800. }
  801. return mmc_set_bkops_enable(mmc);
  802. }
  803. #endif
  804. static int do_mmc_boot_wp(struct cmd_tbl *cmdtp, int flag,
  805. int argc, char * const argv[])
  806. {
  807. int err;
  808. struct mmc *mmc;
  809. mmc = init_mmc_device(curr_device, false);
  810. if (!mmc)
  811. return CMD_RET_FAILURE;
  812. if (IS_SD(mmc)) {
  813. printf("It is not an eMMC device\n");
  814. return CMD_RET_FAILURE;
  815. }
  816. err = mmc_boot_wp(mmc);
  817. if (err)
  818. return CMD_RET_FAILURE;
  819. printf("boot areas protected\n");
  820. return CMD_RET_SUCCESS;
  821. }
  822. static struct cmd_tbl cmd_mmc[] = {
  823. U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
  824. U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
  825. U_BOOT_CMD_MKENT(wp, 1, 0, do_mmc_boot_wp, "", ""),
  826. #if CONFIG_IS_ENABLED(MMC_WRITE)
  827. U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
  828. U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
  829. #endif
  830. #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
  831. U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
  832. #endif
  833. U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
  834. U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
  835. U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
  836. U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
  837. #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
  838. U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
  839. #endif
  840. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  841. U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
  842. U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
  843. U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
  844. U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
  845. #endif
  846. #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
  847. U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
  848. #endif
  849. U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
  850. #ifdef CONFIG_CMD_BKOPS_ENABLE
  851. U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
  852. #endif
  853. };
  854. static int do_mmcops(struct cmd_tbl *cmdtp, int flag, int argc,
  855. char *const argv[])
  856. {
  857. struct cmd_tbl *cp;
  858. cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
  859. /* Drop the mmc command */
  860. argc--;
  861. argv++;
  862. if (cp == NULL || argc > cp->maxargs)
  863. return CMD_RET_USAGE;
  864. if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
  865. return CMD_RET_SUCCESS;
  866. if (curr_device < 0) {
  867. if (get_mmc_num() > 0) {
  868. curr_device = 0;
  869. } else {
  870. puts("No MMC device available\n");
  871. return CMD_RET_FAILURE;
  872. }
  873. }
  874. return cp->cmd(cmdtp, flag, argc, argv);
  875. }
  876. U_BOOT_CMD(
  877. mmc, 29, 1, do_mmcops,
  878. "MMC sub system",
  879. "info - display info of the current MMC device\n"
  880. "mmc read addr blk# cnt\n"
  881. "mmc write addr blk# cnt\n"
  882. #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
  883. "mmc swrite addr blk#\n"
  884. #endif
  885. "mmc erase blk# cnt\n"
  886. "mmc rescan\n"
  887. "mmc part - lists available partition on current mmc device\n"
  888. "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
  889. "mmc list - lists available devices\n"
  890. "mmc wp - power on write protect boot partitions\n"
  891. #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
  892. "mmc hwpartition <USER> <GP> <MODE> - does hardware partitioning\n"
  893. " arguments (sizes in 512-byte blocks):\n"
  894. " USER - <user> <enh> <start> <cnt> <wrrel> <{on|off}>\n"
  895. " : sets user data area attributes\n"
  896. " GP - <{gp1|gp2|gp3|gp4}> <cnt> <enh> <wrrel> <{on|off}>\n"
  897. " : general purpose partition\n"
  898. " MODE - <{check|set|complete}>\n"
  899. " : mode, complete set partitioning completed\n"
  900. " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
  901. " Power cycling is required to initialize partitions after set to complete.\n"
  902. #endif
  903. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  904. "mmc bootbus <dev> <boot_bus_width> <reset_boot_bus_width> <boot_mode>\n"
  905. " - Set the BOOT_BUS_WIDTH field of the specified device\n"
  906. "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
  907. " - Change sizes of boot and RPMB partitions of specified device\n"
  908. "mmc partconf <dev> [[varname] | [<boot_ack> <boot_partition> <partition_access>]]\n"
  909. " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
  910. " If showing the bits, optionally store the boot_partition field into varname\n"
  911. "mmc rst-function <dev> <value>\n"
  912. " - Change the RST_n_FUNCTION field of the specified device\n"
  913. " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
  914. #endif
  915. #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
  916. "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
  917. "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
  918. "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
  919. "mmc rpmb counter - read the value of the write counter\n"
  920. #endif
  921. "mmc setdsr <value> - set DSR register value\n"
  922. #ifdef CONFIG_CMD_BKOPS_ENABLE
  923. "mmc bkops-enable <dev> - enable background operations handshake on device\n"
  924. " WARNING: This is a write-once setting.\n"
  925. #endif
  926. );
  927. /* Old command kept for compatibility. Same as 'mmc info' */
  928. U_BOOT_CMD(
  929. mmcinfo, 1, 0, do_mmcinfo,
  930. "display MMC info",
  931. "- display info of the current MMC device"
  932. );