cmd_mmc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 (strcmp(argv[1], "part") == 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], "partconf") == 0) {
  266. int dev;
  267. struct mmc *mmc;
  268. u8 ack, part_num, access;
  269. if (argc == 6) {
  270. dev = simple_strtoul(argv[2], NULL, 10);
  271. ack = simple_strtoul(argv[3], NULL, 10);
  272. part_num = simple_strtoul(argv[4], NULL, 10);
  273. access = simple_strtoul(argv[5], NULL, 10);
  274. } else {
  275. return CMD_RET_USAGE;
  276. }
  277. mmc = find_mmc_device(dev);
  278. if (!mmc) {
  279. printf("no mmc device at slot %x\n", dev);
  280. return 1;
  281. }
  282. if (IS_SD(mmc)) {
  283. puts("PARTITION_CONFIG only exists on eMMC\n");
  284. return 1;
  285. }
  286. /* acknowledge to be sent during boot operation */
  287. return mmc_set_part_conf(mmc, ack, part_num, access);
  288. } else if (strcmp(argv[1], "bootbus") == 0) {
  289. int dev;
  290. struct mmc *mmc;
  291. u8 width, reset, mode;
  292. if (argc == 6) {
  293. dev = simple_strtoul(argv[2], NULL, 10);
  294. width = simple_strtoul(argv[3], NULL, 10);
  295. reset = simple_strtoul(argv[4], NULL, 10);
  296. mode = simple_strtoul(argv[5], NULL, 10);
  297. } else {
  298. return CMD_RET_USAGE;
  299. }
  300. mmc = find_mmc_device(dev);
  301. if (!mmc) {
  302. printf("no mmc device at slot %x\n", dev);
  303. return 1;
  304. }
  305. if (IS_SD(mmc)) {
  306. puts("BOOT_BUS_WIDTH only exists on eMMC\n");
  307. return 1;
  308. }
  309. /* acknowledge to be sent during boot operation */
  310. return mmc_set_boot_bus_width(mmc, width, reset, mode);
  311. } else if (strcmp(argv[1], "bootpart-resize") == 0) {
  312. int dev;
  313. struct mmc *mmc;
  314. u32 bootsize, rpmbsize;
  315. if (argc == 5) {
  316. dev = simple_strtoul(argv[2], NULL, 10);
  317. bootsize = simple_strtoul(argv[3], NULL, 10);
  318. rpmbsize = simple_strtoul(argv[4], NULL, 10);
  319. } else {
  320. return CMD_RET_USAGE;
  321. }
  322. mmc = find_mmc_device(dev);
  323. if (!mmc) {
  324. printf("no mmc device at slot %x\n", dev);
  325. return 1;
  326. }
  327. if (IS_SD(mmc)) {
  328. printf("It is not a EMMC device\n");
  329. return 1;
  330. }
  331. if (0 == mmc_boot_partition_size_change(mmc,
  332. bootsize, rpmbsize)) {
  333. printf("EMMC boot partition Size %d MB\n", bootsize);
  334. printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
  335. return 0;
  336. } else {
  337. printf("EMMC boot partition Size change Failed.\n");
  338. return 1;
  339. }
  340. #endif /* CONFIG_SUPPORT_EMMC_BOOT */
  341. }
  342. else if (argc == 3 && strcmp(argv[1], "setdsr") == 0) {
  343. struct mmc *mmc = find_mmc_device(curr_device);
  344. u32 val = simple_strtoul(argv[2], NULL, 16);
  345. int ret;
  346. if (!mmc) {
  347. printf("no mmc device at slot %x\n", curr_device);
  348. return 1;
  349. }
  350. ret = mmc_set_dsr(mmc, val);
  351. printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
  352. if (!ret) {
  353. mmc->has_init = 0;
  354. if (mmc_init(mmc))
  355. return 1;
  356. else
  357. return 0;
  358. }
  359. return ret;
  360. }
  361. state = MMC_INVALID;
  362. if (argc == 5 && strcmp(argv[1], "read") == 0)
  363. state = MMC_READ;
  364. else if (argc == 5 && strcmp(argv[1], "write") == 0)
  365. state = MMC_WRITE;
  366. else if (argc == 4 && strcmp(argv[1], "erase") == 0)
  367. state = MMC_ERASE;
  368. if (state != MMC_INVALID) {
  369. struct mmc *mmc = find_mmc_device(curr_device);
  370. int idx = 2;
  371. u32 blk, cnt, n;
  372. void *addr;
  373. if (state != MMC_ERASE) {
  374. addr = (void *)simple_strtoul(argv[idx], NULL, 16);
  375. ++idx;
  376. } else
  377. addr = NULL;
  378. blk = simple_strtoul(argv[idx], NULL, 16);
  379. cnt = simple_strtoul(argv[idx + 1], NULL, 16);
  380. if (!mmc) {
  381. printf("no mmc device at slot %x\n", curr_device);
  382. return 1;
  383. }
  384. printf("\nMMC %s: dev # %d, block # %d, count %d ... ",
  385. argv[1], curr_device, blk, cnt);
  386. mmc_init(mmc);
  387. if ((state == MMC_WRITE || state == MMC_ERASE)) {
  388. if (mmc_getwp(mmc) == 1) {
  389. printf("Error: card is write protected!\n");
  390. return 1;
  391. }
  392. }
  393. switch (state) {
  394. case MMC_READ:
  395. n = mmc->block_dev.block_read(curr_device, blk,
  396. cnt, addr);
  397. /* flush cache after read */
  398. flush_cache((ulong)addr, cnt * 512); /* FIXME */
  399. break;
  400. case MMC_WRITE:
  401. n = mmc->block_dev.block_write(curr_device, blk,
  402. cnt, addr);
  403. break;
  404. case MMC_ERASE:
  405. n = mmc->block_dev.block_erase(curr_device, blk, cnt);
  406. break;
  407. default:
  408. BUG();
  409. }
  410. printf("%d blocks %s: %s\n",
  411. n, argv[1], (n == cnt) ? "OK" : "ERROR");
  412. return (n == cnt) ? 0 : 1;
  413. }
  414. return CMD_RET_USAGE;
  415. }
  416. U_BOOT_CMD(
  417. mmc, 6, 1, do_mmcops,
  418. "MMC sub system",
  419. "read addr blk# cnt\n"
  420. "mmc write addr blk# cnt\n"
  421. "mmc erase blk# cnt\n"
  422. "mmc rescan\n"
  423. "mmc part - lists available partition on current mmc device\n"
  424. "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
  425. "mmc list - lists available devices\n"
  426. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  427. "mmc open <dev> <boot_partition>\n"
  428. " - Enable boot_part for booting and enable R/W access of boot_part\n"
  429. "mmc close <dev> <boot_partition>\n"
  430. " - Enable boot_part for booting and disable access to boot_part\n"
  431. "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
  432. " - Set the BOOT_BUS_WIDTH field of the specified device\n"
  433. "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
  434. " - Change sizes of boot and RPMB partitions of specified device\n"
  435. "mmc partconf dev boot_ack boot_partition partition_access\n"
  436. " - Change the bits of the PARTITION_CONFIG field of the specified device\n"
  437. #endif
  438. "mmc setdsr - set DSR register value\n"
  439. );
  440. #endif /* !CONFIG_GENERIC_MMC */