mmc.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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 *)simple_strtoul(argv[1], NULL, 16);
  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 *)simple_strtoul(argv[1], NULL, 16);
  186. blk = simple_strtoul(argv[2], NULL, 16);
  187. cnt = simple_strtoul(argv[3], NULL, 16);
  188. if (argc == 5)
  189. key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
  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 *)simple_strtoul(argv[1], NULL, 16);
  209. blk = simple_strtoul(argv[2], NULL, 16);
  210. cnt = simple_strtoul(argv[3], NULL, 16);
  211. key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
  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 *)simple_strtoul(argv[1], NULL, 16);
  288. blk = simple_strtoul(argv[2], NULL, 16);
  289. cnt = simple_strtoul(argv[3], NULL, 16);
  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 *)simple_strtoul(argv[1], NULL, 16);
  323. blk = simple_strtoul(argv[2], NULL, 16);
  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 *)simple_strtoul(argv[1], NULL, 16);
  362. blk = simple_strtoul(argv[2], NULL, 16);
  363. cnt = simple_strtoul(argv[3], NULL, 16);
  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 = simple_strtoul(argv[1], NULL, 16);
  385. cnt = simple_strtoul(argv[2], NULL, 16);
  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 = simple_strtoul(argv[1], NULL, 10);
  434. } else if (argc == 3) {
  435. dev = (int)simple_strtoul(argv[1], NULL, 10);
  436. part = (int)simple_strtoul(argv[2], NULL, 10);
  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. simple_strtoul(argv[i+1], NULL, 10);
  479. pconf->user.enh_size =
  480. simple_strtoul(argv[i+2], NULL, 10);
  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 = simple_strtoul(argv[0], NULL, 10);
  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 = simple_strtoul(argv[1], NULL, 10);
  617. width = simple_strtoul(argv[2], NULL, 10);
  618. reset = simple_strtoul(argv[3], NULL, 10);
  619. mode = simple_strtoul(argv[4], NULL, 10);
  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 = simple_strtoul(argv[1], NULL, 10);
  672. bootsize = simple_strtoul(argv[2], NULL, 10);
  673. rpmbsize = simple_strtoul(argv[3], NULL, 10);
  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)
  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. printf("EXT_CSD[179], PARTITION_CONFIG:\n"
  700. "BOOT_ACK: 0x%x\n"
  701. "BOOT_PARTITION_ENABLE: 0x%x\n"
  702. "PARTITION_ACCESS: 0x%x\n", ack, part, access);
  703. return CMD_RET_SUCCESS;
  704. }
  705. static int do_mmc_partconf(struct cmd_tbl *cmdtp, int flag,
  706. int argc, char *const argv[])
  707. {
  708. int dev;
  709. struct mmc *mmc;
  710. u8 ack, part_num, access;
  711. if (argc != 2 && argc != 5)
  712. return CMD_RET_USAGE;
  713. dev = simple_strtoul(argv[1], NULL, 10);
  714. mmc = init_mmc_device(dev, false);
  715. if (!mmc)
  716. return CMD_RET_FAILURE;
  717. if (IS_SD(mmc)) {
  718. puts("PARTITION_CONFIG only exists on eMMC\n");
  719. return CMD_RET_FAILURE;
  720. }
  721. if (argc == 2)
  722. return mmc_partconf_print(mmc);
  723. ack = simple_strtoul(argv[2], NULL, 10);
  724. part_num = simple_strtoul(argv[3], NULL, 10);
  725. access = simple_strtoul(argv[4], NULL, 10);
  726. /* acknowledge to be sent during boot operation */
  727. return mmc_set_part_conf(mmc, ack, part_num, access);
  728. }
  729. static int do_mmc_rst_func(struct cmd_tbl *cmdtp, int flag,
  730. int argc, char *const argv[])
  731. {
  732. int dev;
  733. struct mmc *mmc;
  734. u8 enable;
  735. /*
  736. * Set the RST_n_ENABLE bit of RST_n_FUNCTION
  737. * The only valid values are 0x0, 0x1 and 0x2 and writing
  738. * a value of 0x1 or 0x2 sets the value permanently.
  739. */
  740. if (argc != 3)
  741. return CMD_RET_USAGE;
  742. dev = simple_strtoul(argv[1], NULL, 10);
  743. enable = simple_strtoul(argv[2], NULL, 10);
  744. if (enable > 2) {
  745. puts("Invalid RST_n_ENABLE value\n");
  746. return CMD_RET_USAGE;
  747. }
  748. mmc = init_mmc_device(dev, false);
  749. if (!mmc)
  750. return CMD_RET_FAILURE;
  751. if (IS_SD(mmc)) {
  752. puts("RST_n_FUNCTION only exists on eMMC\n");
  753. return CMD_RET_FAILURE;
  754. }
  755. return mmc_set_rst_n_function(mmc, enable);
  756. }
  757. #endif
  758. static int do_mmc_setdsr(struct cmd_tbl *cmdtp, int flag,
  759. int argc, char *const argv[])
  760. {
  761. struct mmc *mmc;
  762. u32 val;
  763. int ret;
  764. if (argc != 2)
  765. return CMD_RET_USAGE;
  766. val = simple_strtoul(argv[1], NULL, 16);
  767. mmc = find_mmc_device(curr_device);
  768. if (!mmc) {
  769. printf("no mmc device at slot %x\n", curr_device);
  770. return CMD_RET_FAILURE;
  771. }
  772. ret = mmc_set_dsr(mmc, val);
  773. printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
  774. if (!ret) {
  775. mmc->has_init = 0;
  776. if (mmc_init(mmc))
  777. return CMD_RET_FAILURE;
  778. else
  779. return CMD_RET_SUCCESS;
  780. }
  781. return ret;
  782. }
  783. #ifdef CONFIG_CMD_BKOPS_ENABLE
  784. static int do_mmc_bkops_enable(struct cmd_tbl *cmdtp, int flag,
  785. int argc, char *const argv[])
  786. {
  787. int dev;
  788. struct mmc *mmc;
  789. if (argc != 2)
  790. return CMD_RET_USAGE;
  791. dev = simple_strtoul(argv[1], NULL, 10);
  792. mmc = init_mmc_device(dev, false);
  793. if (!mmc)
  794. return CMD_RET_FAILURE;
  795. if (IS_SD(mmc)) {
  796. puts("BKOPS_EN only exists on eMMC\n");
  797. return CMD_RET_FAILURE;
  798. }
  799. return mmc_set_bkops_enable(mmc);
  800. }
  801. #endif
  802. static int do_mmc_boot_wp(struct cmd_tbl *cmdtp, int flag,
  803. int argc, char * const argv[])
  804. {
  805. int err;
  806. struct mmc *mmc;
  807. mmc = init_mmc_device(curr_device, false);
  808. if (!mmc)
  809. return CMD_RET_FAILURE;
  810. if (IS_SD(mmc)) {
  811. printf("It is not an eMMC device\n");
  812. return CMD_RET_FAILURE;
  813. }
  814. err = mmc_boot_wp(mmc);
  815. if (err)
  816. return CMD_RET_FAILURE;
  817. printf("boot areas protected\n");
  818. return CMD_RET_SUCCESS;
  819. }
  820. static struct cmd_tbl cmd_mmc[] = {
  821. U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
  822. U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
  823. U_BOOT_CMD_MKENT(wp, 1, 0, do_mmc_boot_wp, "", ""),
  824. #if CONFIG_IS_ENABLED(MMC_WRITE)
  825. U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
  826. U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
  827. #endif
  828. #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
  829. U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
  830. #endif
  831. U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
  832. U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
  833. U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
  834. U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
  835. #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
  836. U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
  837. #endif
  838. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  839. U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
  840. U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
  841. U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
  842. U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
  843. #endif
  844. #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
  845. U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
  846. #endif
  847. U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
  848. #ifdef CONFIG_CMD_BKOPS_ENABLE
  849. U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
  850. #endif
  851. };
  852. static int do_mmcops(struct cmd_tbl *cmdtp, int flag, int argc,
  853. char *const argv[])
  854. {
  855. struct cmd_tbl *cp;
  856. cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
  857. /* Drop the mmc command */
  858. argc--;
  859. argv++;
  860. if (cp == NULL || argc > cp->maxargs)
  861. return CMD_RET_USAGE;
  862. if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
  863. return CMD_RET_SUCCESS;
  864. if (curr_device < 0) {
  865. if (get_mmc_num() > 0) {
  866. curr_device = 0;
  867. } else {
  868. puts("No MMC device available\n");
  869. return CMD_RET_FAILURE;
  870. }
  871. }
  872. return cp->cmd(cmdtp, flag, argc, argv);
  873. }
  874. U_BOOT_CMD(
  875. mmc, 29, 1, do_mmcops,
  876. "MMC sub system",
  877. "info - display info of the current MMC device\n"
  878. "mmc read addr blk# cnt\n"
  879. "mmc write addr blk# cnt\n"
  880. #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
  881. "mmc swrite addr blk#\n"
  882. #endif
  883. "mmc erase blk# cnt\n"
  884. "mmc rescan\n"
  885. "mmc part - lists available partition on current mmc device\n"
  886. "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
  887. "mmc list - lists available devices\n"
  888. "mmc wp - power on write protect boot partitions\n"
  889. #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
  890. "mmc hwpartition <USER> <GP> <MODE> - does hardware partitioning\n"
  891. " arguments (sizes in 512-byte blocks):\n"
  892. " USER - <user> <enh> <start> <cnt> <wrrel> <{on|off}>\n"
  893. " : sets user data area attributes\n"
  894. " GP - <{gp1|gp2|gp3|gp4}> <cnt> <enh> <wrrel> <{on|off}>\n"
  895. " : general purpose partition\n"
  896. " MODE - <{check|set|complete}>\n"
  897. " : mode, complete set partitioning completed\n"
  898. " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
  899. " Power cycling is required to initialize partitions after set to complete.\n"
  900. #endif
  901. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  902. "mmc bootbus <dev> <boot_bus_width> <reset_boot_bus_width> <boot_mode>\n"
  903. " - Set the BOOT_BUS_WIDTH field of the specified device\n"
  904. "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
  905. " - Change sizes of boot and RPMB partitions of specified device\n"
  906. "mmc partconf <dev> [boot_ack boot_partition partition_access]\n"
  907. " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
  908. "mmc rst-function <dev> <value>\n"
  909. " - Change the RST_n_FUNCTION field of the specified device\n"
  910. " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
  911. #endif
  912. #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
  913. "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
  914. "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
  915. "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
  916. "mmc rpmb counter - read the value of the write counter\n"
  917. #endif
  918. "mmc setdsr <value> - set DSR register value\n"
  919. #ifdef CONFIG_CMD_BKOPS_ENABLE
  920. "mmc bkops-enable <dev> - enable background operations handshake on device\n"
  921. " WARNING: This is a write-once setting.\n"
  922. #endif
  923. );
  924. /* Old command kept for compatibility. Same as 'mmc info' */
  925. U_BOOT_CMD(
  926. mmcinfo, 1, 0, do_mmcinfo,
  927. "display MMC info",
  928. "- display info of the current MMC device"
  929. );