soft_spi_legacy.c 3.8 KB

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