cmd_mmc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * (C) Copyright 2003
  3. * Kyle Harris, kharris@nexus-tech.net
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <mmc.h>
  26. static int curr_device = -1;
  27. #ifndef CONFIG_GENERIC_MMC
  28. int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  29. {
  30. int dev;
  31. if (argc < 2)
  32. return CMD_RET_USAGE;
  33. if (strcmp(argv[1], "init") == 0) {
  34. if (argc == 2) {
  35. if (curr_device < 0)
  36. dev = 1;
  37. else
  38. dev = curr_device;
  39. } else if (argc == 3) {
  40. dev = (int)simple_strtoul(argv[2], NULL, 10);
  41. } else {
  42. return CMD_RET_USAGE;
  43. }
  44. if (mmc_legacy_init(dev) != 0) {
  45. puts("No MMC card found\n");
  46. return 1;
  47. }
  48. curr_device = dev;
  49. printf("mmc%d is available\n", curr_device);
  50. } else if (strcmp(argv[1], "device") == 0) {
  51. if (argc == 2) {
  52. if (curr_device < 0) {
  53. puts("No MMC device available\n");
  54. return 1;
  55. }
  56. } else if (argc == 3) {
  57. dev = (int)simple_strtoul(argv[2], NULL, 10);
  58. #ifdef CONFIG_SYS_MMC_SET_DEV
  59. if (mmc_set_dev(dev) != 0)
  60. return 1;
  61. #endif
  62. curr_device = dev;
  63. } else {
  64. return CMD_RET_USAGE;
  65. }
  66. printf("mmc%d is current device\n", curr_device);
  67. } else {
  68. return CMD_RET_USAGE;
  69. }
  70. return 0;
  71. }
  72. U_BOOT_CMD(
  73. mmc, 3, 1, do_mmc,
  74. "MMC sub-system",
  75. "init [dev] - init MMC sub system\n"
  76. "mmc device [dev] - show or set current device"
  77. );
  78. #else /* !CONFIG_GENERIC_MMC */
  79. enum mmc_state {
  80. MMC_INVALID,
  81. MMC_READ,
  82. MMC_WRITE,
  83. MMC_ERASE,
  84. };
  85. static void print_mmcinfo(struct mmc *mmc)
  86. {
  87. printf("Device: %s\n", mmc->name);
  88. printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
  89. printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
  90. printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
  91. (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
  92. (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
  93. printf("Tran Speed: %d\n", mmc->tran_speed);
  94. printf("Rd Block Len: %d\n", mmc->read_bl_len);
  95. printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
  96. (mmc->version >> 8) & 0xf, mmc->version & 0xff);
  97. printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
  98. puts("Capacity: ");
  99. print_size(mmc->capacity, "\n");
  100. printf("Bus Width: %d-bit\n", mmc->bus_width);
  101. }
  102. static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  103. {
  104. struct mmc *mmc;
  105. if (curr_device < 0) {
  106. if (get_mmc_num() > 0)
  107. curr_device = 0;
  108. else {
  109. puts("No MMC device available\n");
  110. return 1;
  111. }
  112. }
  113. mmc = find_mmc_device(curr_device);
  114. if (mmc) {
  115. mmc_init(mmc);
  116. print_mmcinfo(mmc);
  117. return 0;
  118. } else {
  119. printf("no mmc device at slot %x\n", curr_device);
  120. return 1;
  121. }
  122. }
  123. U_BOOT_CMD(
  124. mmcinfo, 1, 0, do_mmcinfo,
  125. "display MMC info",
  126. "- display info of the current MMC device"
  127. );
  128. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  129. static int boot_part_access(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
  130. {
  131. int err;
  132. err = mmc_boot_part_access(mmc, ack, part_num, access);
  133. if ((err == 0) && (access != 0)) {
  134. printf("\t\t\t!!!Notice!!!\n");
  135. printf("!You must close EMMC boot Partition");
  136. printf("after all images are written\n");
  137. printf("!EMMC boot partition has continuity");
  138. printf("at image writing time.\n");
  139. printf("!So, do not close the boot partition");
  140. printf("before all images are written.\n");
  141. return 0;
  142. } else if ((err == 0) && (access == 0))
  143. return 0;
  144. else if ((err != 0) && (access != 0)) {
  145. printf("EMMC boot partition-%d OPEN Failed.\n", part_num);
  146. return 1;
  147. } else {
  148. printf("EMMC boot partition-%d CLOSE Failed.\n", part_num);
  149. return 1;
  150. }
  151. }
  152. #endif
  153. static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  154. {
  155. enum mmc_state state;
  156. if (argc < 2)
  157. return CMD_RET_USAGE;
  158. if (curr_device < 0) {
  159. if (get_mmc_num() > 0)
  160. curr_device = 0;
  161. else {
  162. puts("No MMC device available\n");
  163. return 1;
  164. }
  165. }
  166. if (strcmp(argv[1], "rescan") == 0) {
  167. struct mmc *mmc;
  168. if (argc != 2)
  169. return CMD_RET_USAGE;
  170. mmc = find_mmc_device(curr_device);
  171. if (!mmc) {
  172. printf("no mmc device at slot %x\n", curr_device);
  173. return 1;
  174. }
  175. mmc->has_init = 0;
  176. if (mmc_init(mmc))
  177. return 1;
  178. else
  179. return 0;
  180. } else if (strncmp(argv[1], "part", 4) == 0) {
  181. block_dev_desc_t *mmc_dev;
  182. struct mmc *mmc;
  183. if (argc != 2)
  184. return CMD_RET_USAGE;
  185. mmc = find_mmc_device(curr_device);
  186. if (!mmc) {
  187. printf("no mmc device at slot %x\n", curr_device);
  188. return 1;
  189. }
  190. mmc_init(mmc);
  191. mmc_dev = mmc_get_dev(curr_device);
  192. if (mmc_dev != NULL &&
  193. mmc_dev->type != DEV_TYPE_UNKNOWN) {
  194. print_part(mmc_dev);
  195. return 0;
  196. }
  197. puts("get mmc type error!\n");
  198. return 1;
  199. } else if (strcmp(argv[1], "list") == 0) {
  200. if (argc != 2)
  201. return CMD_RET_USAGE;
  202. print_mmc_devices('\n');
  203. return 0;
  204. } else if (strcmp(argv[1], "dev") == 0) {
  205. int dev, part = -1;
  206. struct mmc *mmc;
  207. if (argc == 2)
  208. dev = curr_device;
  209. else if (argc == 3)
  210. dev = simple_strtoul(argv[2], NULL, 10);
  211. else if (argc == 4) {
  212. dev = (int)simple_strtoul(argv[2], NULL, 10);
  213. part = (int)simple_strtoul(argv[3], NULL, 10);
  214. if (part > PART_ACCESS_MASK) {
  215. printf("#part_num shouldn't be larger"
  216. " than %d\n", PART_ACCESS_MASK);
  217. return 1;
  218. }
  219. } else
  220. return CMD_RET_USAGE;
  221. mmc = find_mmc_device(dev);
  222. if (!mmc) {
  223. printf("no mmc device at slot %x\n", dev);
  224. return 1;
  225. }
  226. mmc_init(mmc);
  227. if (part != -1) {
  228. int ret;
  229. if (mmc->part_config == MMCPART_NOAVAILABLE) {
  230. printf("Card doesn't support part_switch\n");
  231. return 1;
  232. }
  233. if (part != mmc->part_num) {
  234. ret = mmc_switch_part(dev, part);
  235. if (!ret)
  236. mmc->part_num = part;
  237. printf("switch to partions #%d, %s\n",
  238. part, (!ret) ? "OK" : "ERROR");
  239. }
  240. }
  241. curr_device = dev;
  242. if (mmc->part_config == MMCPART_NOAVAILABLE)
  243. printf("mmc%d is current device\n", curr_device);
  244. else
  245. printf("mmc%d(part %d) is current device\n",
  246. curr_device, mmc->part_num);
  247. return 0;
  248. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  249. } else if ((strcmp(argv[1], "open") == 0) ||
  250. (strcmp(argv[1], "close") == 0)) {
  251. int dev;
  252. struct mmc *mmc;
  253. u8 part_num, access = 0;
  254. if (argc == 4) {
  255. dev = simple_strtoul(argv[2], NULL, 10);
  256. part_num = simple_strtoul(argv[3], NULL, 10);
  257. } else {
  258. return CMD_RET_USAGE;
  259. }
  260. mmc = find_mmc_device(dev);
  261. if (!mmc) {
  262. printf("no mmc device at slot %x\n", dev);
  263. return 1;
  264. }
  265. if (IS_SD(mmc)) {
  266. printf("SD device cannot be opened/closed\n");
  267. return 1;
  268. }
  269. if ((part_num <= 0) || (part_num > MMC_NUM_BOOT_PARTITION)) {
  270. printf("Invalid boot partition number:\n");
  271. printf("Boot partition number cannot be <= 0\n");
  272. printf("EMMC44 supports only 2 boot partitions\n");
  273. return 1;
  274. }
  275. if (strcmp(argv[1], "open") == 0)
  276. access = part_num; /* enable R/W access to boot part*/
  277. else
  278. access = 0; /* No access to boot partition */
  279. /* acknowledge to be sent during boot operation */
  280. return boot_part_access(mmc, 1, part_num, access);
  281. } else if (strcmp(argv[1], "bootpart") == 0) {
  282. int dev;
  283. dev = simple_strtoul(argv[2], NULL, 10);
  284. u32 bootsize = simple_strtoul(argv[3], NULL, 10);
  285. u32 rpmbsize = simple_strtoul(argv[4], NULL, 10);
  286. struct mmc *mmc = find_mmc_device(dev);
  287. if (!mmc) {
  288. printf("no mmc device at slot %x\n", dev);
  289. return 1;
  290. }
  291. if (IS_SD(mmc)) {
  292. printf("It is not a EMMC device\n");
  293. return 1;
  294. }
  295. if (0 == mmc_boot_partition_size_change(mmc,
  296. bootsize, rpmbsize)) {
  297. printf("EMMC boot partition Size %d MB\n", bootsize);
  298. printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
  299. return 0;
  300. } else {
  301. printf("EMMC boot partition Size change Failed.\n");
  302. return 1;
  303. }
  304. #endif /* CONFIG_SUPPORT_EMMC_BOOT */
  305. }
  306. state = MMC_INVALID;
  307. if (argc == 5 && strcmp(argv[1], "read") == 0)
  308. state = MMC_READ;
  309. else if (argc == 5 && strcmp(argv[1], "write") == 0)
  310. state = MMC_WRITE;
  311. else if (argc == 4 && strcmp(argv[1], "erase") == 0)
  312. state = MMC_ERASE;
  313. if (state != MMC_INVALID) {
  314. struct mmc *mmc = find_mmc_device(curr_device);
  315. int idx = 2;
  316. u32 blk, cnt, n;
  317. void *addr;
  318. if (state != MMC_ERASE) {
  319. addr = (void *)simple_strtoul(argv[idx], NULL, 16);
  320. ++idx;
  321. } else
  322. addr = NULL;
  323. blk = simple_strtoul(argv[idx], NULL, 16);
  324. cnt = simple_strtoul(argv[idx + 1], NULL, 16);
  325. if (!mmc) {
  326. printf("no mmc device at slot %x\n", curr_device);
  327. return 1;
  328. }
  329. printf("\nMMC %s: dev # %d, block # %d, count %d ... ",
  330. argv[1], curr_device, blk, cnt);
  331. mmc_init(mmc);
  332. if ((state == MMC_WRITE || state == MMC_ERASE)) {
  333. if (mmc_getwp(mmc) == 1) {
  334. printf("Error: card is write protected!\n");
  335. return 1;
  336. }
  337. }
  338. switch (state) {
  339. case MMC_READ:
  340. n = mmc->block_dev.block_read(curr_device, blk,
  341. cnt, addr);
  342. /* flush cache after read */
  343. flush_cache((ulong)addr, cnt * 512); /* FIXME */
  344. break;
  345. case MMC_WRITE:
  346. n = mmc->block_dev.block_write(curr_device, blk,
  347. cnt, addr);
  348. break;
  349. case MMC_ERASE:
  350. n = mmc->block_dev.block_erase(curr_device, blk, cnt);
  351. break;
  352. default:
  353. BUG();
  354. }
  355. printf("%d blocks %s: %s\n",
  356. n, argv[1], (n == cnt) ? "OK" : "ERROR");
  357. return (n == cnt) ? 0 : 1;
  358. }
  359. return CMD_RET_USAGE;
  360. }
  361. U_BOOT_CMD(
  362. mmc, 6, 1, do_mmcops,
  363. "MMC sub system",
  364. "read addr blk# cnt\n"
  365. "mmc write addr blk# cnt\n"
  366. "mmc erase blk# cnt\n"
  367. "mmc rescan\n"
  368. "mmc part - lists available partition on current mmc device\n"
  369. "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
  370. "mmc list - lists available devices\n"
  371. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  372. "mmc open <dev> <boot_partition>\n"
  373. " - Enable boot_part for booting and enable R/W access of boot_part\n"
  374. "mmc close <dev> <boot_partition>\n"
  375. " - Enable boot_part for booting and disable access to boot_part\n"
  376. "mmc bootpart <device num> <boot part size MB> <RPMB part size MB>\n"
  377. " - change sizes of boot and RPMB partions of specified device\n"
  378. #endif
  379. );
  380. #endif /* !CONFIG_GENERIC_MMC */