cmd_mmc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #ifndef CONFIG_GENERIC_MMC
  27. static int curr_device = -1;
  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_usage(cmdtp);
  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_usage(cmdtp);
  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_usage(cmdtp);
  65. }
  66. printf("mmc%d is current device\n", curr_device);
  67. } else {
  68. return cmd_usage(cmdtp);
  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. static void print_mmcinfo(struct mmc *mmc)
  80. {
  81. printf("Device: %s\n", mmc->name);
  82. printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
  83. printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
  84. printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
  85. (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
  86. (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
  87. printf("Tran Speed: %d\n", mmc->tran_speed);
  88. printf("Rd Block Len: %d\n", mmc->read_bl_len);
  89. printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
  90. (mmc->version >> 4) & 0xf, mmc->version & 0xf);
  91. printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
  92. printf("Capacity: %lld\n", mmc->capacity);
  93. printf("Bus Width: %d-bit\n", mmc->bus_width);
  94. }
  95. int do_mmcinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  96. {
  97. struct mmc *mmc;
  98. int dev_num;
  99. if (argc < 2)
  100. dev_num = 0;
  101. else
  102. dev_num = simple_strtoul(argv[1], NULL, 0);
  103. mmc = find_mmc_device(dev_num);
  104. if (mmc) {
  105. mmc_init(mmc);
  106. print_mmcinfo(mmc);
  107. }
  108. return 0;
  109. }
  110. U_BOOT_CMD(mmcinfo, 2, 0, do_mmcinfo,
  111. "mmcinfo <dev num>-- display MMC info",
  112. ""
  113. );
  114. int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  115. {
  116. int rc = 0;
  117. switch (argc) {
  118. case 3:
  119. if (strcmp(argv[1], "rescan") == 0) {
  120. int dev = simple_strtoul(argv[2], NULL, 10);
  121. struct mmc *mmc = find_mmc_device(dev);
  122. if (!mmc)
  123. return 1;
  124. mmc_init(mmc);
  125. return 0;
  126. }
  127. case 0:
  128. case 1:
  129. case 4:
  130. printf("Usage:\n%s\n", cmdtp->usage);
  131. return 1;
  132. case 2:
  133. if (!strcmp(argv[1], "list")) {
  134. print_mmc_devices('\n');
  135. return 0;
  136. }
  137. return 1;
  138. default: /* at least 5 args */
  139. if (strcmp(argv[1], "read") == 0) {
  140. int dev = simple_strtoul(argv[2], NULL, 10);
  141. void *addr = (void *)simple_strtoul(argv[3], NULL, 16);
  142. u32 cnt = simple_strtoul(argv[5], NULL, 16);
  143. u32 n;
  144. u32 blk = simple_strtoul(argv[4], NULL, 16);
  145. struct mmc *mmc = find_mmc_device(dev);
  146. if (!mmc)
  147. return 1;
  148. printf("\nMMC read: dev # %d, block # %d, count %d ... ",
  149. dev, blk, cnt);
  150. mmc_init(mmc);
  151. n = mmc->block_dev.block_read(dev, blk, cnt, addr);
  152. /* flush cache after read */
  153. flush_cache((ulong)addr, cnt * 512); /* FIXME */
  154. printf("%d blocks read: %s\n",
  155. n, (n==cnt) ? "OK" : "ERROR");
  156. return (n == cnt) ? 0 : 1;
  157. } else if (strcmp(argv[1], "write") == 0) {
  158. int dev = simple_strtoul(argv[2], NULL, 10);
  159. void *addr = (void *)simple_strtoul(argv[3], NULL, 16);
  160. u32 cnt = simple_strtoul(argv[5], NULL, 16);
  161. u32 n;
  162. struct mmc *mmc = find_mmc_device(dev);
  163. int blk = simple_strtoul(argv[4], NULL, 16);
  164. if (!mmc)
  165. return 1;
  166. printf("\nMMC write: dev # %d, block # %d, count %d ... ",
  167. dev, blk, cnt);
  168. mmc_init(mmc);
  169. n = mmc->block_dev.block_write(dev, blk, cnt, addr);
  170. printf("%d blocks written: %s\n",
  171. n, (n == cnt) ? "OK" : "ERROR");
  172. return (n == cnt) ? 0 : 1;
  173. } else {
  174. printf("Usage:\n%s\n", cmdtp->usage);
  175. rc = 1;
  176. }
  177. return rc;
  178. }
  179. }
  180. U_BOOT_CMD(
  181. mmc, 6, 1, do_mmcops,
  182. "MMC sub system",
  183. "read <device num> addr blk# cnt\n"
  184. "mmc write <device num> addr blk# cnt\n"
  185. "mmc rescan <device num>\n"
  186. "mmc list - lists available devices");
  187. #endif