spi.c 3.6 KB

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