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