You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

spi_bit.c 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <unistd.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. * @v endianness Endianness of this data transfer
  68. *
  69. * This issues @c len clock cycles on the SPI bus, shifting out data
  70. * from the @c data_out buffer to the MOSI line and shifting in data
  71. * from the MISO line to the @c data_in buffer. If @c data_out is
  72. * NULL, then the data sent will be all zeroes. If @c data_in is
  73. * NULL, then the incoming data will be discarded.
  74. */
  75. static void spi_bit_transfer ( struct spi_bit_basher *spibit,
  76. const void *data_out, void *data_in,
  77. unsigned int len, int endianness ) {
  78. struct spi_bus *bus = &spibit->bus;
  79. struct bit_basher *basher = &spibit->basher;
  80. unsigned int sclk = ( ( bus->mode & SPI_MODE_CPOL ) ? 1 : 0 );
  81. unsigned int cpha = ( ( bus->mode & SPI_MODE_CPHA ) ? 1 : 0 );
  82. unsigned int bit_offset;
  83. unsigned int byte_offset;
  84. unsigned int byte_mask;
  85. unsigned int bit;
  86. unsigned int step;
  87. DBG ( "Transferring %d bits in mode %x\n", len, bus->mode );
  88. for ( step = 0 ; step < ( len * 2 ) ; step++ ) {
  89. /* Calculate byte offset and byte mask */
  90. bit_offset = ( ( endianness == SPI_BIT_BIG_ENDIAN ) ?
  91. ( len - ( step / 2 ) - 1 ) : ( step / 2 ) );
  92. byte_offset = ( bit_offset / 8 );
  93. byte_mask = ( 1 << ( bit_offset % 8 ) );
  94. /* Shift data in or out */
  95. if ( sclk == cpha ) {
  96. const uint8_t *byte;
  97. /* Shift data out */
  98. if ( data_out ) {
  99. byte = ( data_out + byte_offset );
  100. bit = ( *byte & byte_mask );
  101. } else {
  102. bit = 0;
  103. }
  104. write_bit ( basher, SPI_BIT_MOSI, bit );
  105. } else {
  106. uint8_t *byte;
  107. /* Shift data in */
  108. bit = read_bit ( basher, SPI_BIT_MISO );
  109. if ( data_in ) {
  110. byte = ( data_in + byte_offset );
  111. *byte &= ~byte_mask;
  112. *byte |= ( bit & byte_mask );
  113. }
  114. }
  115. /* Toggle clock line */
  116. spi_bit_delay();
  117. sclk = ~sclk;
  118. write_bit ( basher, SPI_BIT_SCLK, sclk );
  119. }
  120. }
  121. /**
  122. * Read/write data via SPI bit-bashing bus
  123. *
  124. * @v bus SPI bus
  125. * @v device SPI device
  126. * @v command Command
  127. * @v address Address to read/write (<0 for no address)
  128. * @v data_out TX data buffer (or NULL)
  129. * @v data_in RX data buffer (or NULL)
  130. * @v len Length of transfer
  131. * @ret rc Return status code
  132. */
  133. static int spi_bit_rw ( struct spi_bus *bus, struct spi_device *device,
  134. unsigned int command, int address,
  135. const void *data_out, void *data_in, size_t len ) {
  136. struct spi_bit_basher *spibit
  137. = container_of ( bus, struct spi_bit_basher, bus );
  138. uint32_t tmp;
  139. /* Set clock line to idle state */
  140. write_bit ( &spibit->basher, SPI_BIT_SCLK,
  141. ( bus->mode & SPI_MODE_CPOL ) );
  142. /* Assert chip select on specified slave */
  143. spi_bit_set_slave_select ( spibit, device->slave, SELECT_SLAVE );
  144. /* Transmit command */
  145. assert ( device->command_len <= ( 8 * sizeof ( tmp ) ) );
  146. tmp = cpu_to_le32 ( command );
  147. spi_bit_transfer ( spibit, &tmp, NULL, device->command_len,
  148. SPI_BIT_BIG_ENDIAN );
  149. /* Transmit address, if present */
  150. if ( address >= 0 ) {
  151. assert ( device->address_len <= ( 8 * sizeof ( tmp ) ) );
  152. tmp = cpu_to_le32 ( address );
  153. spi_bit_transfer ( spibit, &tmp, NULL, device->address_len,
  154. SPI_BIT_BIG_ENDIAN );
  155. }
  156. /* Transmit/receive data */
  157. spi_bit_transfer ( spibit, data_out, data_in, ( len * 8 ),
  158. spibit->endianness );
  159. /* Deassert chip select on specified slave */
  160. spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
  161. return 0;
  162. }
  163. /**
  164. * Initialise SPI bit-bashing interface
  165. *
  166. * @v spibit SPI bit-bashing interface
  167. */
  168. void init_spi_bit_basher ( struct spi_bit_basher *spibit ) {
  169. assert ( &spibit->basher.op->read != NULL );
  170. assert ( &spibit->basher.op->write != NULL );
  171. spibit->bus.rw = spi_bit_rw;
  172. }