cmd_mmc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  113. static int boot_part_access(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
  114. {
  115. int err;
  116. err = mmc_boot_part_access(mmc, ack, part_num, access);
  117. if ((err == 0) && (access != 0)) {
  118. printf("\t\t\t!!!Notice!!!\n");
  119. printf("!You must close EMMC boot Partition");
  120. printf("after all images are written\n");
  121. printf("!EMMC boot partition has continuity");
  122. printf("at image writing time.\n");
  123. printf("!So, do not close the boot partition");
  124. printf("before all images are written.\n");
  125. return 0;
  126. } else if ((err == 0) && (access == 0))
  127. return 0;
  128. else if ((err != 0) && (access != 0)) {
  129. printf("EMMC boot partition-%d OPEN Failed.\n", part_num);
  130. return 1;
  131. } else {
  132. printf("EMMC boot partition-%d CLOSE Failed.\n", part_num);
  133. return 1;
  134. }
  135. }
  136. #endif
  137. static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  138. {
  139. enum mmc_state state;
  140. if (argc < 2)
  141. return CMD_RET_USAGE;
  142. if (curr_device < 0) {
  143. if (get_mmc_num() > 0)
  144. curr_device = 0;
  145. else {
  146. puts("No MMC device available\n");
  147. return 1;
  148. }
  149. }
  150. if (strcmp(argv[1], "rescan") == 0) {
  151. struct mmc *mmc;
  152. if (argc != 2)
  153. return CMD_RET_USAGE;
  154. mmc = find_mmc_device(curr_device);
  155. if (!mmc) {
  156. printf("no mmc device at slot %x\n", curr_device);
  157. return 1;
  158. }
  159. mmc->has_init = 0;
  160. if (mmc_init(mmc))
  161. return 1;
  162. else
  163. return 0;
  164. } else if (strncmp(argv[1], "part", 4) == 0) {
  165. block_dev_desc_t *mmc_dev;
  166. struct mmc *mmc;
  167. if (argc != 2)
  168. return CMD_RET_USAGE;
  169. mmc = find_mmc_device(curr_device);
  170. if (!mmc) {
  171. printf("no mmc device at slot %x\n", curr_device);
  172. return 1;
  173. }
  174. mmc_init(mmc);
  175. mmc_dev = mmc_get_dev(curr_device);
  176. if (mmc_dev != NULL &&
  177. mmc_dev->type != DEV_TYPE_UNKNOWN) {
  178. print_part(mmc_dev);
  179. return 0;
  180. }
  181. puts("get mmc type error!\n");
  182. return 1;
  183. } else if (strcmp(argv[1], "list") == 0) {
  184. if (argc != 2)
  185. return CMD_RET_USAGE;
  186. print_mmc_devices('\n');
  187. return 0;
  188. } else if (strcmp(argv[1], "dev") == 0) {
  189. int dev, part = -1;
  190. struct mmc *mmc;
  191. if (argc == 2)
  192. dev = curr_device;
  193. else if (argc == 3)
  194. dev = simple_strtoul(argv[2], NULL, 10);
  195. else if (argc == 4) {
  196. dev = (int)simple_strtoul(argv[2], NULL, 10);
  197. part = (int)simple_strtoul(argv[3], NULL, 10);
  198. if (part > PART_ACCESS_MASK) {
  199. printf("#part_num shouldn't be larger"
  200. " than %d\n", PART_ACCESS_MASK);
  201. return 1;
  202. }
  203. } else
  204. return CMD_RET_USAGE;
  205. mmc = find_mmc_device(dev);
  206. if (!mmc) {
  207. printf("no mmc device at slot %x\n", dev);
  208. return 1;
  209. }
  210. mmc_init(mmc);
  211. if (part != -1) {
  212. int ret;
  213. if (mmc->part_config == MMCPART_NOAVAILABLE) {
  214. printf("Card doesn't support part_switch\n");
  215. return 1;
  216. }
  217. if (part != mmc->part_num) {
  218. ret = mmc_switch_part(dev, part);
  219. if (!ret)
  220. mmc->part_num = part;
  221. printf("switch to partitions #%d, %s\n",
  222. part, (!ret) ? "OK" : "ERROR");
  223. }
  224. }
  225. curr_device = dev;
  226. if (mmc->part_config == MMCPART_NOAVAILABLE)
  227. printf("mmc%d is current device\n", curr_device);
  228. else
  229. printf("mmc%d(part %d) is current device\n",
  230. curr_device, mmc->part_num);
  231. return 0;
  232. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  233. } else if ((strcmp(argv[1], "open") == 0) ||
  234. (strcmp(argv[1], "close") == 0)) {
  235. int dev;
  236. struct mmc *mmc;
  237. u8 part_num, access = 0;
  238. if (argc == 4) {
  239. dev = simple_strtoul(argv[2], NULL, 10);
  240. part_num = simple_strtoul(argv[3], NULL, 10);
  241. } else {
  242. return CMD_RET_USAGE;
  243. }
  244. mmc = find_mmc_device(dev);
  245. if (!mmc) {
  246. printf("no mmc device at slot %x\n", dev);
  247. return 1;
  248. }
  249. if (IS_SD(mmc)) {
  250. printf("SD device cannot be opened/closed\n");
  251. return 1;
  252. }
  253. if ((part_num <= 0) || (part_num > MMC_NUM_BOOT_PARTITION)) {
  254. printf("Invalid boot partition number:\n");
  255. printf("Boot partition number cannot be <= 0\n");
  256. printf("EMMC44 supports only 2 boot partitions\n");
  257. return 1;
  258. }
  259. if (strcmp(argv[1], "open") == 0)
  260. access = part_num; /* enable R/W access to boot part*/
  261. else
  262. access = 0; /* No access to boot partition */
  263. /* acknowledge to be sent during boot operation */
  264. return boot_part_access(mmc, 1, part_num, access);
  265. } else if (strcmp(argv[1], "bootpart") == 0) {
  266. int dev;
  267. struct *mmc;
  268. u32 bootsize, rpmbsize;
  269. if (argc == 5) {
  270. dev = simple_strtoul(argv[2], NULL, 10);
  271. bootsize = simple_strtoul(argv[3], NULL, 10);
  272. rpmbsize = simple_strtoul(argv[4], NULL, 10);
  273. } else {
  274. return CMD_RET_USAGE;
  275. }
  276. mmc = find_mmc_device(dev);
  277. if (!mmc) {
  278. printf("no mmc device at slot %x\n", dev);
  279. return 1;
  280. }
  281. if (IS_SD(mmc)) {
  282. printf("It is not a EMMC device\n");
  283. return 1;
  284. }
  285. if (0 == mmc_boot_partition_size_change(mmc,
  286. bootsize, rpmbsize)) {
  287. printf("EMMC boot partition Size %d MB\n", bootsize);
  288. printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
  289. return 0;
  290. } else {
  291. printf("EMMC boot partition Size change Failed.\n");
  292. return 1;
  293. }
  294. #endif /* CONFIG_SUPPORT_EMMC_BOOT */
  295. }
  296. else if (argc == 3 && strcmp(argv[1], "setdsr") == 0) {
  297. struct mmc *mmc = find_mmc_device(curr_device);
  298. u32 val = simple_strtoul(argv[2], NULL, 16);
  299. int ret;
  300. if (!mmc) {
  301. printf("no mmc device at slot %x\n", curr_device);
  302. return 1;
  303. }
  304. ret = mmc_set_dsr(mmc, val);
  305. printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
  306. if (!ret) {
  307. mmc->has_init = 0;
  308. if (mmc_init(mmc))
  309. return 1;
  310. else
  311. return 0;
  312. }
  313. return ret;
  314. }
  315. state = MMC_INVALID;
  316. if (argc == 5 && strcmp(argv[1], "read") == 0)
  317. state = MMC_READ;
  318. else if (argc == 5 && strcmp(argv[1], "write") == 0)
  319. state = MMC_WRITE;
  320. else if (argc == 4 && strcmp(argv[1], "erase") == 0)
  321. state = MMC_ERASE;
  322. if (state != MMC_INVALID) {
  323. struct mmc *mmc = find_mmc_device(curr_device);
  324. int idx = 2;
  325. u32 blk, cnt, n;
  326. void *addr;
  327. if (state != MMC_ERASE) {
  328. addr = (void *)simple_strtoul(argv[idx], NULL, 16);
  329. ++idx;
  330. } else
  331. addr = NULL;
  332. blk = simple_strtoul(argv[idx], NULL, 16);
  333. cnt = simple_strtoul(argv[idx + 1], NULL, 16);
  334. if (!mmc) {
  335. printf("no mmc device at slot %x\n", curr_device);
  336. return 1;
  337. }
  338. printf("\nMMC %s: dev # %d, block # %d, count %d ... ",
  339. argv[1], curr_device, blk, cnt);
  340. mmc_init(mmc);
  341. if ((state == MMC_WRITE || state == MMC_ERASE)) {
  342. if (mmc_getwp(mmc) == 1) {
  343. printf("Error: card is write protected!\n");
  344. return 1;
  345. }
  346. }
  347. switch (state) {
  348. case MMC_READ:
  349. n = mmc->block_dev.block_read(curr_device, blk,
  350. cnt, addr);
  351. /* flush cache after read */
  352. flush_cache((ulong)addr, cnt * 512); /* FIXME */
  353. break;
  354. case MMC_WRITE:
  355. n = mmc->block_dev.block_write(curr_device, blk,
  356. cnt, addr);
  357. break;
  358. case MMC_ERASE:
  359. n = mmc->block_dev.block_erase(curr_device, blk, cnt);
  360. break;
  361. default:
  362. BUG();
  363. }
  364. printf("%d blocks %s: %s\n",
  365. n, argv[1], (n == cnt) ? "OK" : "ERROR");
  366. return (n == cnt) ? 0 : 1;
  367. }
  368. return CMD_RET_USAGE;
  369. }
  370. U_BOOT_CMD(
  371. mmc, 6, 1, do_mmcops,
  372. "MMC sub system",
  373. "read addr blk# cnt\n"
  374. "mmc write addr blk# cnt\n"
  375. "mmc erase blk# cnt\n"
  376. "mmc rescan\n"
  377. "mmc part - lists available partition on current mmc device\n"
  378. "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
  379. "mmc list - lists available devices\n"
  380. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  381. "mmc open <dev> <boot_partition>\n"
  382. " - Enable boot_part for booting and enable R/W access of boot_part\n"
  383. "mmc close <dev> <boot_partition>\n"
  384. " - Enable boot_part for booting and disable access to boot_part\n"
  385. "mmc bootpart <device num> <boot part size MB> <RPMB part size MB>\n"
  386. " - change sizes of boot and RPMB partitions of specified device\n"
  387. #endif
  388. "mmc setdsr - set DSR register value\n"
  389. );
  390. #endif /* !CONFIG_GENERIC_MMC */