cmd_mmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * (C) Copyright 2003
  3. * Kyle Harris, kharris@nexus-tech.net
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <mmc.h>
  10. static int curr_device = -1;
  11. #ifndef CONFIG_GENERIC_MMC
  12. int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  13. {
  14. int dev;
  15. if (argc < 2)
  16. return CMD_RET_USAGE;
  17. if (strcmp(argv[1], "init") == 0) {
  18. if (argc == 2) {
  19. if (curr_device < 0)
  20. dev = 1;
  21. else
  22. dev = curr_device;
  23. } else if (argc == 3) {
  24. dev = (int)simple_strtoul(argv[2], NULL, 10);
  25. } else {
  26. return CMD_RET_USAGE;
  27. }
  28. if (mmc_legacy_init(dev) != 0) {
  29. puts("No MMC card found\n");
  30. return 1;
  31. }
  32. curr_device = dev;
  33. printf("mmc%d is available\n", curr_device);
  34. } else if (strcmp(argv[1], "device") == 0) {
  35. if (argc == 2) {
  36. if (curr_device < 0) {
  37. puts("No MMC device available\n");
  38. return 1;
  39. }
  40. } else if (argc == 3) {
  41. dev = (int)simple_strtoul(argv[2], NULL, 10);
  42. #ifdef CONFIG_SYS_MMC_SET_DEV
  43. if (mmc_set_dev(dev) != 0)
  44. return 1;
  45. #endif
  46. curr_device = dev;
  47. } else {
  48. return CMD_RET_USAGE;
  49. }
  50. printf("mmc%d is current device\n", curr_device);
  51. } else {
  52. return CMD_RET_USAGE;
  53. }
  54. return 0;
  55. }
  56. U_BOOT_CMD(
  57. mmc, 3, 1, do_mmc,
  58. "MMC sub-system",
  59. "init [dev] - init MMC sub system\n"
  60. "mmc device [dev] - show or set current device"
  61. );
  62. #else /* !CONFIG_GENERIC_MMC */
  63. enum mmc_state {
  64. MMC_INVALID,
  65. MMC_READ,
  66. MMC_WRITE,
  67. MMC_ERASE,
  68. };
  69. static void print_mmcinfo(struct mmc *mmc)
  70. {
  71. printf("Device: %s\n", mmc->cfg->name);
  72. printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
  73. printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
  74. printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
  75. (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
  76. (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
  77. printf("Tran Speed: %d\n", mmc->tran_speed);
  78. printf("Rd Block Len: %d\n", mmc->read_bl_len);
  79. printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
  80. (mmc->version >> 8) & 0xf, mmc->version & 0xff);
  81. printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
  82. puts("Capacity: ");
  83. print_size(mmc->capacity, "\n");
  84. printf("Bus Width: %d-bit\n", mmc->bus_width);
  85. }
  86. static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  87. {
  88. struct mmc *mmc;
  89. if (curr_device < 0) {
  90. if (get_mmc_num() > 0)
  91. curr_device = 0;
  92. else {
  93. puts("No MMC device available\n");
  94. return 1;
  95. }
  96. }
  97. mmc = find_mmc_device(curr_device);
  98. if (mmc) {
  99. mmc_init(mmc);
  100. print_mmcinfo(mmc);
  101. return 0;
  102. } else {
  103. printf("no mmc device at slot %x\n", curr_device);
  104. return 1;
  105. }
  106. }
  107. U_BOOT_CMD(
  108. mmcinfo, 1, 0, do_mmcinfo,
  109. "display MMC info",
  110. "- display info of the current MMC device"
  111. );
  112. static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  113. {
  114. enum mmc_state state;
  115. if (argc < 2)
  116. return CMD_RET_USAGE;
  117. if (curr_device < 0) {
  118. if (get_mmc_num() > 0)
  119. curr_device = 0;
  120. else {
  121. puts("No MMC device available\n");
  122. return 1;
  123. }
  124. }
  125. if (strcmp(argv[1], "rescan") == 0) {
  126. struct mmc *mmc;
  127. if (argc != 2)
  128. return CMD_RET_USAGE;
  129. mmc = find_mmc_device(curr_device);
  130. if (!mmc) {
  131. printf("no mmc device at slot %x\n", curr_device);
  132. return 1;
  133. }
  134. mmc->has_init = 0;
  135. if (mmc_init(mmc))
  136. return 1;
  137. else
  138. return 0;
  139. } else if (strcmp(argv[1], "part") == 0) {
  140. block_dev_desc_t *mmc_dev;
  141. struct mmc *mmc;
  142. if (argc != 2)
  143. return CMD_RET_USAGE;
  144. mmc = find_mmc_device(curr_device);
  145. if (!mmc) {
  146. printf("no mmc device at slot %x\n", curr_device);
  147. return 1;
  148. }
  149. mmc_init(mmc);
  150. mmc_dev = mmc_get_dev(curr_device);
  151. if (mmc_dev != NULL &&
  152. mmc_dev->type != DEV_TYPE_UNKNOWN) {
  153. print_part(mmc_dev);
  154. return 0;
  155. }
  156. puts("get mmc type error!\n");
  157. return 1;
  158. } else if (strcmp(argv[1], "list") == 0) {
  159. if (argc != 2)
  160. return CMD_RET_USAGE;
  161. print_mmc_devices('\n');
  162. return 0;
  163. } else if (strcmp(argv[1], "dev") == 0) {
  164. int dev, part = -1;
  165. struct mmc *mmc;
  166. if (argc == 2)
  167. dev = curr_device;
  168. else if (argc == 3)
  169. dev = simple_strtoul(argv[2], NULL, 10);
  170. else if (argc == 4) {
  171. dev = (int)simple_strtoul(argv[2], NULL, 10);
  172. part = (int)simple_strtoul(argv[3], NULL, 10);
  173. if (part > PART_ACCESS_MASK) {
  174. printf("#part_num shouldn't be larger"
  175. " than %d\n", PART_ACCESS_MASK);
  176. return 1;
  177. }
  178. } else
  179. return CMD_RET_USAGE;
  180. mmc = find_mmc_device(dev);
  181. if (!mmc) {
  182. printf("no mmc device at slot %x\n", dev);
  183. return 1;
  184. }
  185. mmc_init(mmc);
  186. if (part != -1) {
  187. int ret;
  188. if (mmc->part_config == MMCPART_NOAVAILABLE) {
  189. printf("Card doesn't support part_switch\n");
  190. return 1;
  191. }
  192. if (part != mmc->part_num) {
  193. ret = mmc_switch_part(dev, part);
  194. if (!ret)
  195. mmc->part_num = part;
  196. printf("switch to partitions #%d, %s\n",
  197. part, (!ret) ? "OK" : "ERROR");
  198. }
  199. }
  200. curr_device = dev;
  201. if (mmc->part_config == MMCPART_NOAVAILABLE)
  202. printf("mmc%d is current device\n", curr_device);
  203. else
  204. printf("mmc%d(part %d) is current device\n",
  205. curr_device, mmc->part_num);
  206. return 0;
  207. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  208. } else if (strcmp(argv[1], "partconf") == 0) {
  209. int dev;
  210. struct mmc *mmc;
  211. u8 ack, part_num, access;
  212. if (argc == 6) {
  213. dev = simple_strtoul(argv[2], NULL, 10);
  214. ack = simple_strtoul(argv[3], NULL, 10);
  215. part_num = simple_strtoul(argv[4], NULL, 10);
  216. access = simple_strtoul(argv[5], NULL, 10);
  217. } else {
  218. return CMD_RET_USAGE;
  219. }
  220. mmc = find_mmc_device(dev);
  221. if (!mmc) {
  222. printf("no mmc device at slot %x\n", dev);
  223. return 1;
  224. }
  225. if (IS_SD(mmc)) {
  226. puts("PARTITION_CONFIG only exists on eMMC\n");
  227. return 1;
  228. }
  229. /* acknowledge to be sent during boot operation */
  230. return mmc_set_part_conf(mmc, ack, part_num, access);
  231. } else if (strcmp(argv[1], "bootbus") == 0) {
  232. int dev;
  233. struct mmc *mmc;
  234. u8 width, reset, mode;
  235. if (argc == 6) {
  236. dev = simple_strtoul(argv[2], NULL, 10);
  237. width = simple_strtoul(argv[3], NULL, 10);
  238. reset = simple_strtoul(argv[4], NULL, 10);
  239. mode = simple_strtoul(argv[5], NULL, 10);
  240. } else {
  241. return CMD_RET_USAGE;
  242. }
  243. mmc = find_mmc_device(dev);
  244. if (!mmc) {
  245. printf("no mmc device at slot %x\n", dev);
  246. return 1;
  247. }
  248. if (IS_SD(mmc)) {
  249. puts("BOOT_BUS_WIDTH only exists on eMMC\n");
  250. return 1;
  251. }
  252. /* acknowledge to be sent during boot operation */
  253. return mmc_set_boot_bus_width(mmc, width, reset, mode);
  254. } else if (strcmp(argv[1], "bootpart-resize") == 0) {
  255. int dev;
  256. struct mmc *mmc;
  257. u32 bootsize, rpmbsize;
  258. if (argc == 5) {
  259. dev = simple_strtoul(argv[2], NULL, 10);
  260. bootsize = simple_strtoul(argv[3], NULL, 10);
  261. rpmbsize = simple_strtoul(argv[4], NULL, 10);
  262. } else {
  263. return CMD_RET_USAGE;
  264. }
  265. mmc = find_mmc_device(dev);
  266. if (!mmc) {
  267. printf("no mmc device at slot %x\n", dev);
  268. return 1;
  269. }
  270. if (IS_SD(mmc)) {
  271. printf("It is not a EMMC device\n");
  272. return 1;
  273. }
  274. if (0 == mmc_boot_partition_size_change(mmc,
  275. bootsize, rpmbsize)) {
  276. printf("EMMC boot partition Size %d MB\n", bootsize);
  277. printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
  278. return 0;
  279. } else {
  280. printf("EMMC boot partition Size change Failed.\n");
  281. return 1;
  282. }
  283. } else if (strcmp(argv[1], "rst-function") == 0) {
  284. /*
  285. * Set the RST_n_ENABLE bit of RST_n_FUNCTION
  286. * The only valid values are 0x0, 0x1 and 0x2 and writing
  287. * a value of 0x1 or 0x2 sets the value permanently.
  288. */
  289. int dev;
  290. struct mmc *mmc;
  291. u8 enable;
  292. if (argc == 4) {
  293. dev = simple_strtoul(argv[2], NULL, 10);
  294. enable = simple_strtoul(argv[3], NULL, 10);
  295. } else {
  296. return CMD_RET_USAGE;
  297. }
  298. if (enable > 2 || enable < 0) {
  299. puts("Invalid RST_n_ENABLE value\n");
  300. return CMD_RET_USAGE;
  301. }
  302. mmc = find_mmc_device(dev);
  303. if (!mmc) {
  304. printf("no mmc device at slot %x\n", dev);
  305. return 1;
  306. }
  307. if (IS_SD(mmc)) {
  308. puts("RST_n_FUNCTION only exists on eMMC\n");
  309. return 1;
  310. }
  311. return mmc_set_rst_n_function(mmc, enable);
  312. #endif /* CONFIG_SUPPORT_EMMC_BOOT */
  313. }
  314. else if (argc == 3 && strcmp(argv[1], "setdsr") == 0) {
  315. struct mmc *mmc = find_mmc_device(curr_device);
  316. u32 val = simple_strtoul(argv[2], NULL, 16);
  317. int ret;
  318. if (!mmc) {
  319. printf("no mmc device at slot %x\n", curr_device);
  320. return 1;
  321. }
  322. ret = mmc_set_dsr(mmc, val);
  323. printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
  324. if (!ret) {
  325. mmc->has_init = 0;
  326. if (mmc_init(mmc))
  327. return 1;
  328. else
  329. return 0;
  330. }
  331. return ret;
  332. }
  333. state = MMC_INVALID;
  334. if (argc == 5 && strcmp(argv[1], "read") == 0)
  335. state = MMC_READ;
  336. else if (argc == 5 && strcmp(argv[1], "write") == 0)
  337. state = MMC_WRITE;
  338. else if (argc == 4 && strcmp(argv[1], "erase") == 0)
  339. state = MMC_ERASE;
  340. if (state != MMC_INVALID) {
  341. struct mmc *mmc = find_mmc_device(curr_device);
  342. int idx = 2;
  343. u32 blk, cnt, n;
  344. void *addr;
  345. if (state != MMC_ERASE) {
  346. addr = (void *)simple_strtoul(argv[idx], NULL, 16);
  347. ++idx;
  348. } else
  349. addr = NULL;
  350. blk = simple_strtoul(argv[idx], NULL, 16);
  351. cnt = simple_strtoul(argv[idx + 1], NULL, 16);
  352. if (!mmc) {
  353. printf("no mmc device at slot %x\n", curr_device);
  354. return 1;
  355. }
  356. printf("\nMMC %s: dev # %d, block # %d, count %d ... ",
  357. argv[1], curr_device, blk, cnt);
  358. mmc_init(mmc);
  359. if ((state == MMC_WRITE || state == MMC_ERASE)) {
  360. if (mmc_getwp(mmc) == 1) {
  361. printf("Error: card is write protected!\n");
  362. return 1;
  363. }
  364. }
  365. switch (state) {
  366. case MMC_READ:
  367. n = mmc->block_dev.block_read(curr_device, blk,
  368. cnt, addr);
  369. /* flush cache after read */
  370. flush_cache((ulong)addr, cnt * 512); /* FIXME */
  371. break;
  372. case MMC_WRITE:
  373. n = mmc->block_dev.block_write(curr_device, blk,
  374. cnt, addr);
  375. break;
  376. case MMC_ERASE:
  377. n = mmc->block_dev.block_erase(curr_device, blk, cnt);
  378. break;
  379. default:
  380. BUG();
  381. }
  382. printf("%d blocks %s: %s\n",
  383. n, argv[1], (n == cnt) ? "OK" : "ERROR");
  384. return (n == cnt) ? 0 : 1;
  385. }
  386. return CMD_RET_USAGE;
  387. }
  388. U_BOOT_CMD(
  389. mmc, 6, 1, do_mmcops,
  390. "MMC sub system",
  391. "read addr blk# cnt\n"
  392. "mmc write addr blk# cnt\n"
  393. "mmc erase blk# cnt\n"
  394. "mmc rescan\n"
  395. "mmc part - lists available partition on current mmc device\n"
  396. "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
  397. "mmc list - lists available devices\n"
  398. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  399. "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
  400. " - Set the BOOT_BUS_WIDTH field of the specified device\n"
  401. "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
  402. " - Change sizes of boot and RPMB partitions of specified device\n"
  403. "mmc partconf dev boot_ack boot_partition partition_access\n"
  404. " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
  405. "mmc rst-function dev value\n"
  406. " - Change the RST_n_FUNCTION field of the specified device\n"
  407. " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
  408. #endif
  409. "mmc setdsr - set DSR register value\n"
  410. );
  411. #endif /* !CONFIG_GENERIC_MMC */