Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stddef.h>
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <byteswap.h>
  22. #include <errno.h>
  23. #include <assert.h>
  24. #include <timer.h>
  25. #include <gpxe/bitbash.h>
  26. #include <gpxe/spi_bit.h>
  27. /** @file
  28. *
  29. * SPI bit-bashing interface
  30. *
  31. */
  32. /** Delay between SCLK changes and around SS changes */
  33. static void spi_bit_delay ( void ) {
  34. udelay ( SPI_BIT_UDELAY );
  35. }
  36. /** Chip select line will be asserted */
  37. #define SELECT_SLAVE 0
  38. /** Chip select line will be deasserted */
  39. #define DESELECT_SLAVE SPI_MODE_SSPOL
  40. /**
  41. * Select/deselect slave
  42. *
  43. * @v spibit SPI bit-bashing interface
  44. * @v slave Slave number
  45. * @v state Slave select state
  46. *
  47. * @c state must be @c SELECT_SLAVE or @c DESELECT_SLAVE.
  48. */
  49. static void spi_bit_set_slave_select ( struct spi_bit_basher *spibit,
  50. unsigned int slave,
  51. unsigned int state ) {
  52. struct bit_basher *basher = &spibit->basher;
  53. state ^= ( spibit->bus.mode & SPI_MODE_SSPOL );
  54. DBG ( "Setting slave %d select %s\n", slave,
  55. ( state ? "high" : "low" ) );
  56. spi_bit_delay();
  57. write_bit ( basher, SPI_BIT_SS ( slave ), state );
  58. spi_bit_delay();
  59. }
  60. /**
  61. * Transfer bits over SPI bit-bashing bus
  62. *
  63. * @v bus SPI bus
  64. * @v data_out TX data buffer (or NULL)
  65. * @v data_in RX data buffer (or NULL)
  66. * @v len Length of transfer (in @b bits)
  67. *
  68. * This issues @c len clock cycles on the SPI bus, shifting out data
  69. * from the @c data_out buffer to the MOSI line and shifting in data
  70. * from the MISO line to the @c data_in buffer. If @c data_out is
  71. * NULL, then the data sent will be all zeroes. If @c data_in is
  72. * NULL, then the incoming data will be discarded.
  73. */
  74. static void spi_bit_transfer ( struct spi_bit_basher *spibit,
  75. const void *data_out, void *data_in,
  76. unsigned int len ) {
  77. struct spi_bus *bus = &spibit->bus;
  78. struct bit_basher *basher = &spibit->basher;
  79. unsigned int sclk = ( ( bus->mode & SPI_MODE_CPOL ) ? 1 : 0 );
  80. unsigned int cpha = ( ( bus->mode & SPI_MODE_CPHA ) ? 1 : 0 );
  81. unsigned int offset;
  82. unsigned int mask;
  83. unsigned int bit;
  84. int step;
  85. DBG ( "Transferring %d bits in mode %x\n", len, bus->mode );
  86. for ( step = ( ( len * 2 ) - 1 ) ; step >= 0 ; step-- ) {
  87. /* Calculate byte offset within data and bit mask */
  88. offset = ( step / 16 );
  89. mask = ( 1 << ( ( step % 16 ) / 2 ) );
  90. /* Shift data in or out */
  91. if ( sclk == cpha ) {
  92. const uint8_t *byte;
  93. /* Shift data out */
  94. if ( data_out ) {
  95. byte = ( data_out + offset );
  96. bit = ( *byte & mask );
  97. } else {
  98. bit = 0;
  99. }
  100. write_bit ( basher, SPI_BIT_MOSI, bit );
  101. } else {
  102. uint8_t *byte;
  103. /* Shift data in */
  104. bit = read_bit ( basher, SPI_BIT_MISO );
  105. if ( data_in ) {
  106. byte = ( data_in + offset );
  107. *byte &= ~mask;
  108. *byte |= ( bit & mask );
  109. }
  110. }
  111. /* Toggle clock line */
  112. spi_bit_delay();
  113. sclk = ~sclk;
  114. write_bit ( basher, SPI_BIT_SCLK, sclk );
  115. }
  116. }
  117. /**
  118. * Read/write data via SPI bit-bashing bus
  119. *
  120. * @v bus SPI bus
  121. * @v device SPI device
  122. * @v command Command
  123. * @v address Address to read/write (<0 for no address)
  124. * @v data_out TX data buffer (or NULL)
  125. * @v data_in RX data buffer (or NULL)
  126. * @v len Length of transfer
  127. * @ret rc Return status code
  128. */
  129. static int spi_bit_rw ( struct spi_bus *bus, struct spi_device *device,
  130. unsigned int command, int address,
  131. const void *data_out, void *data_in, size_t len ) {
  132. struct spi_bit_basher *spibit
  133. = container_of ( bus, struct spi_bit_basher, bus );
  134. uint32_t tmp;
  135. /* Assert chip select on specified slave */
  136. spi_bit_set_slave_select ( spibit, device->slave, SELECT_SLAVE );
  137. /* Transmit command */
  138. assert ( device->command_len <= ( 8 * sizeof ( tmp ) ) );
  139. tmp = cpu_to_le32 ( command );
  140. spi_bit_transfer ( spibit, &tmp, NULL, device->command_len );
  141. /* Transmit address, if present */
  142. if ( address >= 0 ) {
  143. assert ( device->address_len <= ( 8 * sizeof ( tmp ) ) );
  144. tmp = cpu_to_le32 ( address );
  145. spi_bit_transfer ( spibit, &tmp, NULL, device->address_len );
  146. }
  147. /* Transmit/receive data */
  148. spi_bit_transfer ( spibit, data_out, data_in, ( len * 8 ) );
  149. /* Deassert chip select on specified slave */
  150. spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
  151. return 0;
  152. }
  153. /**
  154. * Initialise SPI bit-bashing interface
  155. *
  156. * @v spibit SPI bit-bashing interface
  157. */
  158. void init_spi_bit_basher ( struct spi_bit_basher *spibit ) {
  159. assert ( &spibit->basher.op->read != NULL );
  160. assert ( &spibit->basher.op->write != NULL );
  161. spibit->bus.rw = spi_bit_rw;
  162. }