spi.c 3.6 KB

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