cmd_mmc.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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->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. #endif /* CONFIG_SUPPORT_EMMC_BOOT */
  284. }
  285. else if (argc == 3 && strcmp(argv[1], "setdsr") == 0) {
  286. struct mmc *mmc = find_mmc_device(curr_device);
  287. u32 val = simple_strtoul(argv[2], NULL, 16);
  288. int ret;
  289. if (!mmc) {
  290. printf("no mmc device at slot %x\n", curr_device);
  291. return 1;
  292. }
  293. ret = mmc_set_dsr(mmc, val);
  294. printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
  295. if (!ret) {
  296. mmc->has_init = 0;
  297. if (mmc_init(mmc))
  298. return 1;
  299. else
  300. return 0;
  301. }
  302. return ret;
  303. }
  304. state = MMC_INVALID;
  305. if (argc == 5 && strcmp(argv[1], "read") == 0)
  306. state = MMC_READ;
  307. else if (argc == 5 && strcmp(argv[1], "write") == 0)
  308. state = MMC_WRITE;
  309. else if (argc == 4 && strcmp(argv[1], "erase") == 0)
  310. state = MMC_ERASE;
  311. if (state != MMC_INVALID) {
  312. struct mmc *mmc = find_mmc_device(curr_device);
  313. int idx = 2;
  314. u32 blk, cnt, n;
  315. void *addr;
  316. if (state != MMC_ERASE) {
  317. addr = (void *)simple_strtoul(argv[idx], NULL, 16);
  318. ++idx;
  319. } else
  320. addr = NULL;
  321. blk = simple_strtoul(argv[idx], NULL, 16);
  322. cnt = simple_strtoul(argv[idx + 1], NULL, 16);
  323. if (!mmc) {
  324. printf("no mmc device at slot %x\n", curr_device);
  325. return 1;
  326. }
  327. printf("\nMMC %s: dev # %d, block # %d, count %d ... ",
  328. argv[1], curr_device, blk, cnt);
  329. mmc_init(mmc);
  330. if ((state == MMC_WRITE || state == MMC_ERASE)) {
  331. if (mmc_getwp(mmc) == 1) {
  332. printf("Error: card is write protected!\n");
  333. return 1;
  334. }
  335. }
  336. switch (state) {
  337. case MMC_READ:
  338. n = mmc->block_dev.block_read(curr_device, blk,
  339. cnt, addr);
  340. /* flush cache after read */
  341. flush_cache((ulong)addr, cnt * 512); /* FIXME */
  342. break;
  343. case MMC_WRITE:
  344. n = mmc->block_dev.block_write(curr_device, blk,
  345. cnt, addr);
  346. break;
  347. case MMC_ERASE:
  348. n = mmc->block_dev.block_erase(curr_device, blk, cnt);
  349. break;
  350. default:
  351. BUG();
  352. }
  353. printf("%d blocks %s: %s\n",
  354. n, argv[1], (n == cnt) ? "OK" : "ERROR");
  355. return (n == cnt) ? 0 : 1;
  356. }
  357. return CMD_RET_USAGE;
  358. }
  359. U_BOOT_CMD(
  360. mmc, 6, 1, do_mmcops,
  361. "MMC sub system",
  362. "read addr blk# cnt\n"
  363. "mmc write addr blk# cnt\n"
  364. "mmc erase blk# cnt\n"
  365. "mmc rescan\n"
  366. "mmc part - lists available partition on current mmc device\n"
  367. "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
  368. "mmc list - lists available devices\n"
  369. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  370. "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
  371. " - Set the BOOT_BUS_WIDTH field of the specified device\n"
  372. "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
  373. " - Change sizes of boot and RPMB partitions of specified device\n"
  374. "mmc partconf dev boot_ack boot_partition partition_access\n"
  375. " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
  376. #endif
  377. "mmc setdsr - set DSR register value\n"
  378. );
  379. #endif /* !CONFIG_GENERIC_MMC */