soft_spi.c 3.8 KB

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