soft_spi.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <spi.h>
  28. #include <malloc.h>
  29. /*-----------------------------------------------------------------------
  30. * Definitions
  31. */
  32. #ifdef DEBUG_SPI
  33. #define PRINTD(fmt,args...) printf (fmt ,##args)
  34. #else
  35. #define PRINTD(fmt,args...)
  36. #endif
  37. struct soft_spi_slave {
  38. struct spi_slave slave;
  39. unsigned int mode;
  40. };
  41. static inline struct soft_spi_slave *to_soft_spi(struct spi_slave *slave)
  42. {
  43. return container_of(slave, struct soft_spi_slave, slave);
  44. }
  45. /*=====================================================================*/
  46. /* Public Functions */
  47. /*=====================================================================*/
  48. /*-----------------------------------------------------------------------
  49. * Initialization
  50. */
  51. void spi_init (void)
  52. {
  53. #ifdef SPI_INIT
  54. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  55. SPI_INIT;
  56. #endif
  57. }
  58. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  59. unsigned int max_hz, unsigned int mode)
  60. {
  61. struct soft_spi_slave *ss;
  62. if (!spi_cs_is_valid(bus, cs))
  63. return NULL;
  64. ss = malloc(sizeof(struct soft_spi_slave));
  65. if (!ss)
  66. return NULL;
  67. ss->slave.bus = bus;
  68. ss->slave.cs = cs;
  69. ss->mode = mode;
  70. /* TODO: Use max_hz to limit the SCK rate */
  71. return &ss->slave;
  72. }
  73. void spi_free_slave(struct spi_slave *slave)
  74. {
  75. struct soft_spi_slave *ss = to_soft_spi(slave);
  76. free(ss);
  77. }
  78. int spi_claim_bus(struct spi_slave *slave)
  79. {
  80. #ifdef CFG_IMMR
  81. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  82. #endif
  83. struct soft_spi_slave *ss = to_soft_spi(slave);
  84. /*
  85. * Make sure the SPI clock is in idle state as defined for
  86. * this slave.
  87. */
  88. if (ss->mode & SPI_CPOL)
  89. SPI_SCL(1);
  90. else
  91. SPI_SCL(0);
  92. return 0;
  93. }
  94. void spi_release_bus(struct spi_slave *slave)
  95. {
  96. /* Nothing to do */
  97. }
  98. /*-----------------------------------------------------------------------
  99. * SPI transfer
  100. *
  101. * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
  102. * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
  103. *
  104. * The source of the outgoing bits is the "dout" parameter and the
  105. * destination of the input bits is the "din" parameter. Note that "dout"
  106. * and "din" can point to the same memory location, in which case the
  107. * input data overwrites the output data (since both are buffered by
  108. * temporary variables, this is OK).
  109. */
  110. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  111. const void *dout, void *din, unsigned long flags)
  112. {
  113. #ifdef CFG_IMMR
  114. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  115. #endif
  116. struct soft_spi_slave *ss = to_soft_spi(slave);
  117. uchar tmpdin = 0;
  118. uchar tmpdout = 0;
  119. const u8 *txd = dout;
  120. u8 *rxd = din;
  121. int cpol = ss->mode & SPI_CPOL;
  122. int cpha = ss->mode & SPI_CPHA;
  123. unsigned int j;
  124. PRINTD("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
  125. slave->bus, slave->cs, *(uint *)txd, *(uint *)rxd, bitlen);
  126. if (flags & SPI_XFER_BEGIN)
  127. spi_cs_activate(slave);
  128. for(j = 0; j < bitlen; j++) {
  129. /*
  130. * Check if it is time to work on a new byte.
  131. */
  132. if((j % 8) == 0) {
  133. tmpdout = *txd++;
  134. if(j != 0) {
  135. *rxd++ = tmpdin;
  136. }
  137. tmpdin = 0;
  138. }
  139. if (!cpha)
  140. SPI_SCL(!cpol);
  141. SPI_SDA(tmpdout & 0x80);
  142. SPI_DELAY;
  143. if (cpha)
  144. SPI_SCL(!cpol);
  145. else
  146. SPI_SCL(cpol);
  147. tmpdin <<= 1;
  148. tmpdin |= SPI_READ;
  149. tmpdout <<= 1;
  150. SPI_DELAY;
  151. if (cpha)
  152. SPI_SCL(cpol);
  153. }
  154. /*
  155. * If the number of bits isn't a multiple of 8, shift the last
  156. * bits over to left-justify them. Then store the last byte
  157. * read in.
  158. */
  159. if((bitlen % 8) != 0)
  160. tmpdin <<= 8 - (bitlen % 8);
  161. *rxd++ = tmpdin;
  162. if (flags & SPI_XFER_END)
  163. spi_cs_deactivate(slave);
  164. return(0);
  165. }