cmd_spi.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * (C) Copyright 2002
  3. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * SPI Read/Write Utilities
  9. */
  10. #include <common.h>
  11. #include <command.h>
  12. #include <dm.h>
  13. #include <errno.h>
  14. #include <spi.h>
  15. /*-----------------------------------------------------------------------
  16. * Definitions
  17. */
  18. #ifndef MAX_SPI_BYTES
  19. # define MAX_SPI_BYTES 32 /* Maximum number of bytes we can handle */
  20. #endif
  21. #ifndef CONFIG_DEFAULT_SPI_BUS
  22. # define CONFIG_DEFAULT_SPI_BUS 0
  23. #endif
  24. #ifndef CONFIG_DEFAULT_SPI_MODE
  25. # define CONFIG_DEFAULT_SPI_MODE SPI_MODE_0
  26. #endif
  27. /*
  28. * Values from last command.
  29. */
  30. static unsigned int bus;
  31. static unsigned int cs;
  32. static unsigned int mode;
  33. static int bitlen;
  34. static uchar dout[MAX_SPI_BYTES];
  35. static uchar din[MAX_SPI_BYTES];
  36. static int do_spi_xfer(int bus, int cs)
  37. {
  38. struct spi_slave *slave;
  39. int ret = 0;
  40. #ifdef CONFIG_DM_SPI
  41. char name[30], *str;
  42. struct udevice *dev;
  43. snprintf(name, sizeof(name), "generic_%d:%d", bus, cs);
  44. str = strdup(name);
  45. ret = spi_get_bus_and_cs(bus, cs, 1000000, mode, "spi_generic_drv",
  46. str, &dev, &slave);
  47. if (ret)
  48. return ret;
  49. #else
  50. slave = spi_setup_slave(bus, cs, 1000000, mode);
  51. if (!slave) {
  52. printf("Invalid device %d:%d\n", bus, cs);
  53. return -EINVAL;
  54. }
  55. #endif
  56. ret = spi_claim_bus(slave);
  57. if (ret)
  58. goto done;
  59. ret = spi_xfer(slave, bitlen, dout, din,
  60. SPI_XFER_BEGIN | SPI_XFER_END);
  61. #ifndef CONFIG_DM_SPI
  62. /* We don't get an error code in this case */
  63. if (ret)
  64. ret = -EIO;
  65. #endif
  66. if (ret) {
  67. printf("Error %d during SPI transaction\n", ret);
  68. } else {
  69. int j;
  70. for (j = 0; j < ((bitlen + 7) / 8); j++)
  71. printf("%02X", din[j]);
  72. printf("\n");
  73. }
  74. done:
  75. spi_release_bus(slave);
  76. #ifndef CONFIG_DM_SPI
  77. spi_free_slave(slave);
  78. #endif
  79. return ret;
  80. }
  81. /*
  82. * SPI read/write
  83. *
  84. * Syntax:
  85. * spi {dev} {num_bits} {dout}
  86. * {dev} is the device number for controlling chip select (see TBD)
  87. * {num_bits} is the number of bits to send & receive (base 10)
  88. * {dout} is a hexadecimal string of data to send
  89. * The command prints out the hexadecimal string received via SPI.
  90. */
  91. int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  92. {
  93. char *cp = 0;
  94. uchar tmp;
  95. int j;
  96. /*
  97. * We use the last specified parameters, unless new ones are
  98. * entered.
  99. */
  100. if ((flag & CMD_FLAG_REPEAT) == 0)
  101. {
  102. if (argc >= 2) {
  103. mode = CONFIG_DEFAULT_SPI_MODE;
  104. bus = simple_strtoul(argv[1], &cp, 10);
  105. if (*cp == ':') {
  106. cs = simple_strtoul(cp+1, &cp, 10);
  107. } else {
  108. cs = bus;
  109. bus = CONFIG_DEFAULT_SPI_BUS;
  110. }
  111. if (*cp == '.')
  112. mode = simple_strtoul(cp+1, NULL, 10);
  113. }
  114. if (argc >= 3)
  115. bitlen = simple_strtoul(argv[2], NULL, 10);
  116. if (argc >= 4) {
  117. cp = argv[3];
  118. for(j = 0; *cp; j++, cp++) {
  119. tmp = *cp - '0';
  120. if(tmp > 9)
  121. tmp -= ('A' - '0') - 10;
  122. if(tmp > 15)
  123. tmp -= ('a' - 'A');
  124. if(tmp > 15) {
  125. printf("Hex conversion error on %c\n", *cp);
  126. return 1;
  127. }
  128. if((j % 2) == 0)
  129. dout[j / 2] = (tmp << 4);
  130. else
  131. dout[j / 2] |= tmp;
  132. }
  133. }
  134. }
  135. if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) {
  136. printf("Invalid bitlen %d\n", bitlen);
  137. return 1;
  138. }
  139. if (do_spi_xfer(bus, cs))
  140. return 1;
  141. return 0;
  142. }
  143. /***************************************************/
  144. U_BOOT_CMD(
  145. sspi, 5, 1, do_spi,
  146. "SPI utility command",
  147. "[<bus>:]<cs>[.<mode>] <bit_len> <dout> - Send and receive bits\n"
  148. "<bus> - Identifies the SPI bus\n"
  149. "<cs> - Identifies the chip select\n"
  150. "<mode> - Identifies the SPI mode to use\n"
  151. "<bit_len> - Number of bits to send (base 10)\n"
  152. "<dout> - Hexadecimal string that gets sent"
  153. );