soft_spi.c 4.5 KB

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