cmd_mmc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  129. {
  130. enum mmc_state state;
  131. if (argc < 2)
  132. return CMD_RET_USAGE;
  133. if (curr_device < 0) {
  134. if (get_mmc_num() > 0)
  135. curr_device = 0;
  136. else {
  137. puts("No MMC device available\n");
  138. return 1;
  139. }
  140. }
  141. if (strcmp(argv[1], "rescan") == 0) {
  142. struct mmc *mmc;
  143. if (argc != 2)
  144. return CMD_RET_USAGE;
  145. mmc = find_mmc_device(curr_device);
  146. if (!mmc) {
  147. printf("no mmc device at slot %x\n", curr_device);
  148. return 1;
  149. }
  150. mmc->has_init = 0;
  151. if (mmc_init(mmc))
  152. return 1;
  153. else
  154. return 0;
  155. } else if (strncmp(argv[1], "part", 4) == 0) {
  156. block_dev_desc_t *mmc_dev;
  157. struct mmc *mmc;
  158. if (argc != 2)
  159. return CMD_RET_USAGE;
  160. mmc = find_mmc_device(curr_device);
  161. if (!mmc) {
  162. printf("no mmc device at slot %x\n", curr_device);
  163. return 1;
  164. }
  165. mmc_init(mmc);
  166. mmc_dev = mmc_get_dev(curr_device);
  167. if (mmc_dev != NULL &&
  168. mmc_dev->type != DEV_TYPE_UNKNOWN) {
  169. print_part(mmc_dev);
  170. return 0;
  171. }
  172. puts("get mmc type error!\n");
  173. return 1;
  174. } else if (strcmp(argv[1], "list") == 0) {
  175. if (argc != 2)
  176. return CMD_RET_USAGE;
  177. print_mmc_devices('\n');
  178. return 0;
  179. } else if (strcmp(argv[1], "dev") == 0) {
  180. int dev, part = -1;
  181. struct mmc *mmc;
  182. if (argc == 2)
  183. dev = curr_device;
  184. else if (argc == 3)
  185. dev = simple_strtoul(argv[2], NULL, 10);
  186. else if (argc == 4) {
  187. dev = (int)simple_strtoul(argv[2], NULL, 10);
  188. part = (int)simple_strtoul(argv[3], NULL, 10);
  189. if (part > PART_ACCESS_MASK) {
  190. printf("#part_num shouldn't be larger"
  191. " than %d\n", PART_ACCESS_MASK);
  192. return 1;
  193. }
  194. } else
  195. return CMD_RET_USAGE;
  196. mmc = find_mmc_device(dev);
  197. if (!mmc) {
  198. printf("no mmc device at slot %x\n", dev);
  199. return 1;
  200. }
  201. mmc_init(mmc);
  202. if (part != -1) {
  203. int ret;
  204. if (mmc->part_config == MMCPART_NOAVAILABLE) {
  205. printf("Card doesn't support part_switch\n");
  206. return 1;
  207. }
  208. if (part != mmc->part_num) {
  209. ret = mmc_switch_part(dev, part);
  210. if (!ret)
  211. mmc->part_num = part;
  212. printf("switch to partions #%d, %s\n",
  213. part, (!ret) ? "OK" : "ERROR");
  214. }
  215. }
  216. curr_device = dev;
  217. if (mmc->part_config == MMCPART_NOAVAILABLE)
  218. printf("mmc%d is current device\n", curr_device);
  219. else
  220. printf("mmc%d(part %d) is current device\n",
  221. curr_device, mmc->part_num);
  222. return 0;
  223. }
  224. state = MMC_INVALID;
  225. if (argc == 5 && strcmp(argv[1], "read") == 0)
  226. state = MMC_READ;
  227. else if (argc == 5 && strcmp(argv[1], "write") == 0)
  228. state = MMC_WRITE;
  229. else if (argc == 4 && strcmp(argv[1], "erase") == 0)
  230. state = MMC_ERASE;
  231. if (state != MMC_INVALID) {
  232. struct mmc *mmc = find_mmc_device(curr_device);
  233. int idx = 2;
  234. u32 blk, cnt, n;
  235. void *addr;
  236. if (state != MMC_ERASE) {
  237. addr = (void *)simple_strtoul(argv[idx], NULL, 16);
  238. ++idx;
  239. } else
  240. addr = NULL;
  241. blk = simple_strtoul(argv[idx], NULL, 16);
  242. cnt = simple_strtoul(argv[idx + 1], NULL, 16);
  243. if (!mmc) {
  244. printf("no mmc device at slot %x\n", curr_device);
  245. return 1;
  246. }
  247. printf("\nMMC %s: dev # %d, block # %d, count %d ... ",
  248. argv[1], curr_device, blk, cnt);
  249. mmc_init(mmc);
  250. if ((state == MMC_WRITE || state == MMC_ERASE)) {
  251. if (mmc_getwp(mmc) == 1) {
  252. printf("Error: card is write protected!\n");
  253. return 1;
  254. }
  255. }
  256. switch (state) {
  257. case MMC_READ:
  258. n = mmc->block_dev.block_read(curr_device, blk,
  259. cnt, addr);
  260. /* flush cache after read */
  261. flush_cache((ulong)addr, cnt * 512); /* FIXME */
  262. break;
  263. case MMC_WRITE:
  264. n = mmc->block_dev.block_write(curr_device, blk,
  265. cnt, addr);
  266. break;
  267. case MMC_ERASE:
  268. n = mmc->block_dev.block_erase(curr_device, blk, cnt);
  269. break;
  270. default:
  271. BUG();
  272. }
  273. printf("%d blocks %s: %s\n",
  274. n, argv[1], (n == cnt) ? "OK" : "ERROR");
  275. return (n == cnt) ? 0 : 1;
  276. }
  277. return CMD_RET_USAGE;
  278. }
  279. U_BOOT_CMD(
  280. mmc, 6, 1, do_mmcops,
  281. "MMC sub system",
  282. "read addr blk# cnt\n"
  283. "mmc write addr blk# cnt\n"
  284. "mmc erase blk# cnt\n"
  285. "mmc rescan\n"
  286. "mmc part - lists available partition on current mmc device\n"
  287. "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
  288. "mmc list - lists available devices");
  289. #endif