spi.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. if (!str)
  46. return -ENOMEM;
  47. ret = spi_get_bus_and_cs(bus, cs, 1000000, mode, "spi_generic_drv",
  48. str, &dev, &slave);
  49. if (ret)
  50. return ret;
  51. #else
  52. slave = spi_setup_slave(bus, cs, 1000000, mode);
  53. if (!slave) {
  54. printf("Invalid device %d:%d\n", bus, cs);
  55. return -EINVAL;
  56. }
  57. #endif
  58. ret = spi_claim_bus(slave);
  59. if (ret)
  60. goto done;
  61. ret = spi_xfer(slave, bitlen, dout, din,
  62. SPI_XFER_BEGIN | SPI_XFER_END);
  63. #ifndef CONFIG_DM_SPI
  64. /* We don't get an error code in this case */
  65. if (ret)
  66. ret = -EIO;
  67. #endif
  68. if (ret) {
  69. printf("Error %d during SPI transaction\n", ret);
  70. } else {
  71. int j;
  72. for (j = 0; j < ((bitlen + 7) / 8); j++)
  73. printf("%02X", din[j]);
  74. printf("\n");
  75. }
  76. done:
  77. spi_release_bus(slave);
  78. #ifndef CONFIG_DM_SPI
  79. spi_free_slave(slave);
  80. #endif
  81. return ret;
  82. }
  83. /*
  84. * SPI read/write
  85. *
  86. * Syntax:
  87. * spi {dev} {num_bits} {dout}
  88. * {dev} is the device number for controlling chip select (see TBD)
  89. * {num_bits} is the number of bits to send & receive (base 10)
  90. * {dout} is a hexadecimal string of data to send
  91. * The command prints out the hexadecimal string received via SPI.
  92. */
  93. int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  94. {
  95. char *cp = 0;
  96. uchar tmp;
  97. int j;
  98. /*
  99. * We use the last specified parameters, unless new ones are
  100. * entered.
  101. */
  102. if ((flag & CMD_FLAG_REPEAT) == 0)
  103. {
  104. if (argc >= 2) {
  105. mode = CONFIG_DEFAULT_SPI_MODE;
  106. bus = simple_strtoul(argv[1], &cp, 10);
  107. if (*cp == ':') {
  108. cs = simple_strtoul(cp+1, &cp, 10);
  109. } else {
  110. cs = bus;
  111. bus = CONFIG_DEFAULT_SPI_BUS;
  112. }
  113. if (*cp == '.')
  114. mode = simple_strtoul(cp+1, NULL, 10);
  115. }
  116. if (argc >= 3)
  117. bitlen = simple_strtoul(argv[2], NULL, 10);
  118. if (argc >= 4) {
  119. cp = argv[3];
  120. for(j = 0; *cp; j++, cp++) {
  121. tmp = *cp - '0';
  122. if(tmp > 9)
  123. tmp -= ('A' - '0') - 10;
  124. if(tmp > 15)
  125. tmp -= ('a' - 'A');
  126. if(tmp > 15) {
  127. printf("Hex conversion error on %c\n", *cp);
  128. return 1;
  129. }
  130. if((j % 2) == 0)
  131. dout[j / 2] = (tmp << 4);
  132. else
  133. dout[j / 2] |= tmp;
  134. }
  135. }
  136. }
  137. if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) {
  138. printf("Invalid bitlen %d\n", bitlen);
  139. return 1;
  140. }
  141. if (do_spi_xfer(bus, cs))
  142. return 1;
  143. return 0;
  144. }
  145. /***************************************************/
  146. U_BOOT_CMD(
  147. sspi, 5, 1, do_spi,
  148. "SPI utility command",
  149. "[<bus>:]<cs>[.<mode>] <bit_len> <dout> - Send and receive bits\n"
  150. "<bus> - Identifies the SPI bus\n"
  151. "<cs> - Identifies the chip select\n"
  152. "<mode> - Identifies the SPI mode to use\n"
  153. "<bit_len> - Number of bits to send (base 10)\n"
  154. "<dout> - Hexadecimal string that gets sent"
  155. );