soft_spi_legacy.c 3.8 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. * Influenced by code from:
  7. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  8. */
  9. #include <common.h>
  10. #include <spi.h>
  11. #include <malloc.h>
  12. /*-----------------------------------------------------------------------
  13. * Definitions
  14. */
  15. #ifdef DEBUG_SPI
  16. #define PRINTD(fmt,args...) printf (fmt ,##args)
  17. #else
  18. #define PRINTD(fmt,args...)
  19. #endif
  20. struct soft_spi_slave {
  21. struct spi_slave slave;
  22. unsigned int mode;
  23. };
  24. static inline struct soft_spi_slave *to_soft_spi(struct spi_slave *slave)
  25. {
  26. return container_of(slave, struct soft_spi_slave, slave);
  27. }
  28. /*=====================================================================*/
  29. /* Public Functions */
  30. /*=====================================================================*/
  31. /*-----------------------------------------------------------------------
  32. * Initialization
  33. */
  34. void spi_init (void)
  35. {
  36. }
  37. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  38. unsigned int max_hz, unsigned int mode)
  39. {
  40. struct soft_spi_slave *ss;
  41. if (!spi_cs_is_valid(bus, cs))
  42. return NULL;
  43. ss = spi_alloc_slave(struct soft_spi_slave, bus, cs);
  44. if (!ss)
  45. return NULL;
  46. ss->mode = mode;
  47. /* TODO: Use max_hz to limit the SCK rate */
  48. return &ss->slave;
  49. }
  50. void spi_free_slave(struct spi_slave *slave)
  51. {
  52. struct soft_spi_slave *ss = to_soft_spi(slave);
  53. free(ss);
  54. }
  55. int spi_claim_bus(struct spi_slave *slave)
  56. {
  57. #ifdef CONFIG_SYS_IMMR
  58. volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  59. #endif
  60. struct soft_spi_slave *ss = to_soft_spi(slave);
  61. /*
  62. * Make sure the SPI clock is in idle state as defined for
  63. * this slave.
  64. */
  65. if (ss->mode & SPI_CPOL)
  66. SPI_SCL(1);
  67. else
  68. SPI_SCL(0);
  69. return 0;
  70. }
  71. void spi_release_bus(struct spi_slave *slave)
  72. {
  73. /* Nothing to do */
  74. }
  75. /*-----------------------------------------------------------------------
  76. * SPI transfer
  77. *
  78. * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
  79. * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
  80. *
  81. * The source of the outgoing bits is the "dout" parameter and the
  82. * destination of the input bits is the "din" parameter. Note that "dout"
  83. * and "din" can point to the same memory location, in which case the
  84. * input data overwrites the output data (since both are buffered by
  85. * temporary variables, this is OK).
  86. */
  87. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  88. const void *dout, void *din, unsigned long flags)
  89. {
  90. #ifdef CONFIG_SYS_IMMR
  91. volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  92. #endif
  93. struct soft_spi_slave *ss = to_soft_spi(slave);
  94. uchar tmpdin = 0;
  95. uchar tmpdout = 0;
  96. const u8 *txd = dout;
  97. u8 *rxd = din;
  98. int cpol = ss->mode & SPI_CPOL;
  99. int cpha = ss->mode & SPI_CPHA;
  100. unsigned int j;
  101. PRINTD("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
  102. slave->bus, slave->cs, *(uint *)txd, *(uint *)rxd, bitlen);
  103. if (flags & SPI_XFER_BEGIN)
  104. spi_cs_activate(slave);
  105. for(j = 0; j < bitlen; j++) {
  106. /*
  107. * Check if it is time to work on a new byte.
  108. */
  109. if ((j % 8) == 0) {
  110. if (txd)
  111. tmpdout = *txd++;
  112. else
  113. tmpdout = 0;
  114. if(j != 0) {
  115. if (rxd)
  116. *rxd++ = tmpdin;
  117. }
  118. tmpdin = 0;
  119. }
  120. if (!cpha)
  121. SPI_SCL(!cpol);
  122. SPI_SDA(tmpdout & 0x80);
  123. SPI_DELAY;
  124. if (cpha)
  125. SPI_SCL(!cpol);
  126. else
  127. SPI_SCL(cpol);
  128. tmpdin <<= 1;
  129. tmpdin |= SPI_READ;
  130. tmpdout <<= 1;
  131. SPI_DELAY;
  132. if (cpha)
  133. SPI_SCL(cpol);
  134. }
  135. /*
  136. * If the number of bits isn't a multiple of 8, shift the last
  137. * bits over to left-justify them. Then store the last byte
  138. * read in.
  139. */
  140. if (rxd) {
  141. if ((bitlen % 8) != 0)
  142. tmpdin <<= 8 - (bitlen % 8);
  143. *rxd++ = tmpdin;
  144. }
  145. if (flags & SPI_XFER_END)
  146. spi_cs_deactivate(slave);
  147. return(0);
  148. }