mmc_spi.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Command for mmc_spi setup.
  3. *
  4. * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
  5. * Licensed under the GPL-2 or later.
  6. */
  7. #include <common.h>
  8. #include <mmc.h>
  9. #include <spi.h>
  10. #ifndef CONFIG_MMC_SPI_BUS
  11. # define CONFIG_MMC_SPI_BUS 0
  12. #endif
  13. #ifndef CONFIG_MMC_SPI_CS
  14. # define CONFIG_MMC_SPI_CS 1
  15. #endif
  16. /* in SPI mode, MMC speed limit is 20MHz, while SD speed limit is 25MHz */
  17. #ifndef CONFIG_MMC_SPI_SPEED
  18. # define CONFIG_MMC_SPI_SPEED 25000000
  19. #endif
  20. /* MMC and SD specs only seem to care that sampling is on the
  21. * rising edge ... meaning SPI modes 0 or 3. So either SPI mode
  22. * should be legit. We'll use mode 0 since the steady state is 0,
  23. * which is appropriate for hotplugging, unless the platform data
  24. * specify mode 3 (if hardware is not compatible to mode 0).
  25. */
  26. #ifndef CONFIG_MMC_SPI_MODE
  27. # define CONFIG_MMC_SPI_MODE SPI_MODE_0
  28. #endif
  29. static int do_mmc_spi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  30. {
  31. uint bus = CONFIG_MMC_SPI_BUS;
  32. uint cs = CONFIG_MMC_SPI_CS;
  33. uint speed = CONFIG_MMC_SPI_SPEED;
  34. uint mode = CONFIG_MMC_SPI_MODE;
  35. char *endp;
  36. struct mmc *mmc;
  37. if (argc < 2)
  38. goto usage;
  39. cs = simple_strtoul(argv[1], &endp, 0);
  40. if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
  41. goto usage;
  42. if (*endp == ':') {
  43. if (endp[1] == 0)
  44. goto usage;
  45. bus = cs;
  46. cs = simple_strtoul(endp + 1, &endp, 0);
  47. if (*endp != 0)
  48. goto usage;
  49. }
  50. if (argc >= 3) {
  51. speed = simple_strtoul(argv[2], &endp, 0);
  52. if (*argv[2] == 0 || *endp != 0)
  53. goto usage;
  54. }
  55. if (argc >= 4) {
  56. mode = simple_strtoul(argv[3], &endp, 16);
  57. if (*argv[3] == 0 || *endp != 0)
  58. goto usage;
  59. }
  60. if (!spi_cs_is_valid(bus, cs)) {
  61. printf("Invalid SPI bus %u cs %u\n", bus, cs);
  62. return 1;
  63. }
  64. mmc = mmc_spi_init(bus, cs, speed, mode);
  65. if (!mmc) {
  66. printf("Failed to create MMC Device\n");
  67. return 1;
  68. }
  69. printf("%s: %d at %u:%u hz %u mode %u\n", mmc->cfg->name,
  70. mmc->block_dev.devnum, bus, cs, speed, mode);
  71. mmc_init(mmc);
  72. return 0;
  73. usage:
  74. return CMD_RET_USAGE;
  75. }
  76. U_BOOT_CMD(
  77. mmc_spi, 4, 0, do_mmc_spi,
  78. "mmc_spi setup",
  79. "[bus:]cs [hz] [mode] - setup mmc_spi device"
  80. );