soft_spi.c 4.6 KB

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