cmd_mmc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 *argv[])
  29. {
  30. int dev;
  31. if (argc < 2) {
  32. cmd_usage(cmdtp);
  33. return 1;
  34. }
  35. if (strcmp(argv[1], "init") == 0) {
  36. if (argc == 2) {
  37. if (curr_device < 0)
  38. dev = 1;
  39. else
  40. dev = curr_device;
  41. } else if (argc == 3) {
  42. dev = (int)simple_strtoul(argv[2], NULL, 10);
  43. } else {
  44. cmd_usage(cmdtp);
  45. return 1;
  46. }
  47. if (mmc_legacy_init(dev) != 0) {
  48. puts("No MMC card found\n");
  49. return 1;
  50. }
  51. curr_device = dev;
  52. printf("mmc%d is available\n", curr_device);
  53. } else if (strcmp(argv[1], "device") == 0) {
  54. if (argc == 2) {
  55. if (curr_device < 0) {
  56. puts("No MMC device available\n");
  57. return 1;
  58. }
  59. } else if (argc == 3) {
  60. dev = (int)simple_strtoul(argv[2], NULL, 10);
  61. #ifdef CONFIG_SYS_MMC_SET_DEV
  62. if (mmc_set_dev(dev) != 0)
  63. return 1;
  64. #endif
  65. curr_device = dev;
  66. } else {
  67. cmd_usage(cmdtp);
  68. return 1;
  69. }
  70. printf("mmc%d is current device\n", curr_device);
  71. } else {
  72. cmd_usage(cmdtp);
  73. return 1;
  74. }
  75. return 0;
  76. }
  77. U_BOOT_CMD(
  78. mmc, 3, 1, do_mmc,
  79. "MMC sub-system",
  80. "init [dev] - init MMC sub system\n"
  81. "mmc device [dev] - show or set current device"
  82. );
  83. #else /* !CONFIG_GENERIC_MMC */
  84. static void print_mmcinfo(struct mmc *mmc)
  85. {
  86. printf("Device: %s\n", mmc->name);
  87. printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
  88. printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
  89. printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
  90. (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
  91. (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
  92. printf("Tran Speed: %d\n", mmc->tran_speed);
  93. printf("Rd Block Len: %d\n", mmc->read_bl_len);
  94. printf("%s version %d.%d\n", IS_SD(mmc) ? "SD" : "MMC",
  95. (mmc->version >> 4) & 0xf, mmc->version & 0xf);
  96. printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
  97. printf("Capacity: %lld\n", mmc->capacity);
  98. printf("Bus Width: %d-bit\n", mmc->bus_width);
  99. }
  100. int do_mmcinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  101. {
  102. struct mmc *mmc;
  103. int dev_num;
  104. if (argc < 2)
  105. dev_num = 0;
  106. else
  107. dev_num = simple_strtoul(argv[1], NULL, 0);
  108. mmc = find_mmc_device(dev_num);
  109. if (mmc) {
  110. mmc_init(mmc);
  111. print_mmcinfo(mmc);
  112. }
  113. return 0;
  114. }
  115. U_BOOT_CMD(mmcinfo, 2, 0, do_mmcinfo,
  116. "mmcinfo <dev num>-- display MMC info\n",
  117. ""
  118. );
  119. int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  120. {
  121. int rc = 0;
  122. switch (argc) {
  123. case 3:
  124. if (strcmp(argv[1], "rescan") == 0) {
  125. int dev = simple_strtoul(argv[2], NULL, 10);
  126. struct mmc *mmc = find_mmc_device(dev);
  127. if (!mmc)
  128. return 1;
  129. mmc_init(mmc);
  130. return 0;
  131. }
  132. case 0:
  133. case 1:
  134. case 4:
  135. printf("Usage:\n%s\n", cmdtp->usage);
  136. return 1;
  137. case 2:
  138. if (!strcmp(argv[1], "list")) {
  139. print_mmc_devices('\n');
  140. return 0;
  141. }
  142. return 1;
  143. default: /* at least 5 args */
  144. if (strcmp(argv[1], "read") == 0) {
  145. int dev = simple_strtoul(argv[2], NULL, 10);
  146. void *addr = (void *)simple_strtoul(argv[3], NULL, 16);
  147. u32 cnt = simple_strtoul(argv[5], NULL, 16);
  148. u32 n;
  149. u32 blk = simple_strtoul(argv[4], NULL, 16);
  150. struct mmc *mmc = find_mmc_device(dev);
  151. if (!mmc)
  152. return 1;
  153. printf("\nMMC read: dev # %d, block # %d, count %d ... ",
  154. dev, blk, cnt);
  155. mmc_init(mmc);
  156. n = mmc->block_dev.block_read(dev, blk, cnt, addr);
  157. /* flush cache after read */
  158. flush_cache((ulong)addr, cnt * 512); /* FIXME */
  159. printf("%d blocks read: %s\n",
  160. n, (n==cnt) ? "OK" : "ERROR");
  161. return (n == cnt) ? 0 : 1;
  162. } else if (strcmp(argv[1], "write") == 0) {
  163. int dev = simple_strtoul(argv[2], NULL, 10);
  164. void *addr = (void *)simple_strtoul(argv[3], NULL, 16);
  165. u32 cnt = simple_strtoul(argv[5], NULL, 16);
  166. u32 n;
  167. struct mmc *mmc = find_mmc_device(dev);
  168. int blk = simple_strtoul(argv[4], NULL, 16);
  169. if (!mmc)
  170. return 1;
  171. printf("\nMMC write: dev # %d, block # %d, count %d ... ",
  172. dev, blk, cnt);
  173. mmc_init(mmc);
  174. n = mmc->block_dev.block_write(dev, blk, cnt, addr);
  175. printf("%d blocks written: %s\n",
  176. n, (n == cnt) ? "OK" : "ERROR");
  177. return (n == cnt) ? 0 : 1;
  178. } else {
  179. printf("Usage:\n%s\n", cmdtp->usage);
  180. rc = 1;
  181. }
  182. return rc;
  183. }
  184. }
  185. U_BOOT_CMD(
  186. mmc, 6, 1, do_mmcops,
  187. "MMC sub system",
  188. "read <device num> addr blk# cnt\n"
  189. "mmc write <device num> addr blk# cnt\n"
  190. "mmc rescan <device num>\n"
  191. "mmc list - lists available devices");
  192. #endif