spi.c 3.5 KB

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