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 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #include <string.h>
  23. #include <byteswap.h>
  24. #include <errno.h>
  25. #include <assert.h>
  26. #include <unistd.h>
  27. #include <ipxe/bitbash.h>
  28. #include <ipxe/spi_bit.h>
  29. /** @file
  30. *
  31. * SPI bit-bashing interface
  32. *
  33. */
  34. /** Delay between SCLK changes and around SS changes */
  35. static void spi_bit_delay ( void ) {
  36. udelay ( SPI_BIT_UDELAY );
  37. }
  38. /** Chip select line will be asserted */
  39. #define SELECT_SLAVE 0
  40. /** Chip select line will be deasserted */
  41. #define DESELECT_SLAVE SPI_MODE_SSPOL
  42. /**
  43. * Select/deselect slave
  44. *
  45. * @v spibit SPI bit-bashing interface
  46. * @v slave Slave number
  47. * @v state Slave select state
  48. *
  49. * @c state must be @c SELECT_SLAVE or @c DESELECT_SLAVE.
  50. */
  51. static void spi_bit_set_slave_select ( struct spi_bit_basher *spibit,
  52. unsigned int slave,
  53. unsigned int state ) {
  54. struct bit_basher *basher = &spibit->basher;
  55. state ^= ( spibit->bus.mode & SPI_MODE_SSPOL );
  56. DBGC2 ( spibit, "SPIBIT %p setting slave %d select %s\n",
  57. spibit, slave, ( state ? "high" : "low" ) );
  58. spi_bit_delay();
  59. write_bit ( basher, SPI_BIT_SS ( slave ), state );
  60. spi_bit_delay();
  61. }
  62. /**
  63. * Transfer bits over SPI bit-bashing bus
  64. *
  65. * @v bus SPI bus
  66. * @v data_out TX data buffer (or NULL)
  67. * @v data_in RX data buffer (or NULL)
  68. * @v len Length of transfer (in @b bits)
  69. * @v endianness Endianness of this data transfer
  70. *
  71. * This issues @c len clock cycles on the SPI bus, shifting out data
  72. * from the @c data_out buffer to the MOSI line and shifting in data
  73. * from the MISO line to the @c data_in buffer. If @c data_out is
  74. * NULL, then the data sent will be all zeroes. If @c data_in is
  75. * NULL, then the incoming data will be discarded.
  76. */
  77. static void spi_bit_transfer ( struct spi_bit_basher *spibit,
  78. const void *data_out, void *data_in,
  79. unsigned int len, int endianness ) {
  80. struct spi_bus *bus = &spibit->bus;
  81. struct bit_basher *basher = &spibit->basher;
  82. unsigned int sclk = ( ( bus->mode & SPI_MODE_CPOL ) ? 1 : 0 );
  83. unsigned int cpha = ( ( bus->mode & SPI_MODE_CPHA ) ? 1 : 0 );
  84. unsigned int bit_offset;
  85. unsigned int byte_offset;
  86. unsigned int byte_mask;
  87. unsigned int bit;
  88. unsigned int step;
  89. DBGC2 ( spibit, "SPIBIT %p transferring %d bits in mode %#x\n",
  90. spibit, len, bus->mode );
  91. for ( step = 0 ; step < ( len * 2 ) ; step++ ) {
  92. /* Calculate byte offset and byte mask */
  93. bit_offset = ( ( endianness == SPI_BIT_BIG_ENDIAN ) ?
  94. ( len - ( step / 2 ) - 1 ) : ( step / 2 ) );
  95. byte_offset = ( bit_offset / 8 );
  96. byte_mask = ( 1 << ( bit_offset % 8 ) );
  97. /* Shift data in or out */
  98. if ( sclk == cpha ) {
  99. const uint8_t *byte;
  100. /* Shift data out */
  101. if ( data_out ) {
  102. byte = ( data_out + byte_offset );
  103. bit = ( *byte & byte_mask );
  104. DBGCP ( spibit, "SPIBIT %p wrote bit %d\n",
  105. spibit, ( bit ? 1 : 0 ) );
  106. } else {
  107. bit = 0;
  108. }
  109. write_bit ( basher, SPI_BIT_MOSI, bit );
  110. } else {
  111. uint8_t *byte;
  112. /* Shift data in */
  113. bit = read_bit ( basher, SPI_BIT_MISO );
  114. if ( data_in ) {
  115. DBGCP ( spibit, "SPIBIT %p read bit %d\n",
  116. spibit, ( bit ? 1 : 0 ) );
  117. byte = ( data_in + byte_offset );
  118. *byte &= ~byte_mask;
  119. *byte |= ( bit & byte_mask );
  120. }
  121. }
  122. /* Toggle clock line */
  123. spi_bit_delay();
  124. sclk ^= 1;
  125. write_bit ( basher, SPI_BIT_SCLK, sclk );
  126. }
  127. }
  128. /**
  129. * Read/write data via SPI bit-bashing bus
  130. *
  131. * @v bus SPI bus
  132. * @v device SPI device
  133. * @v command Command
  134. * @v address Address to read/write (<0 for no address)
  135. * @v data_out TX data buffer (or NULL)
  136. * @v data_in RX data buffer (or NULL)
  137. * @v len Length of transfer
  138. * @ret rc Return status code
  139. */
  140. static int spi_bit_rw ( struct spi_bus *bus, struct spi_device *device,
  141. unsigned int command, int address,
  142. const void *data_out, void *data_in, size_t len ) {
  143. struct spi_bit_basher *spibit
  144. = container_of ( bus, struct spi_bit_basher, bus );
  145. uint32_t tmp_command;
  146. uint32_t tmp_address;
  147. uint32_t tmp_address_detect;
  148. /* Open bit-bashing interface */
  149. open_bit ( &spibit->basher );
  150. /* Deassert chip select to reset specified slave */
  151. spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
  152. /* Set clock line to idle state */
  153. write_bit ( &spibit->basher, SPI_BIT_SCLK,
  154. ( bus->mode & SPI_MODE_CPOL ) );
  155. /* Assert chip select on specified slave */
  156. spi_bit_set_slave_select ( spibit, device->slave, SELECT_SLAVE );
  157. /* Transmit command */
  158. assert ( device->command_len <= ( 8 * sizeof ( tmp_command ) ) );
  159. tmp_command = cpu_to_le32 ( command );
  160. spi_bit_transfer ( spibit, &tmp_command, NULL, device->command_len,
  161. SPI_BIT_BIG_ENDIAN );
  162. /* Transmit address, if present */
  163. if ( address >= 0 ) {
  164. assert ( device->address_len <= ( 8 * sizeof ( tmp_address )));
  165. tmp_address = cpu_to_le32 ( address );
  166. if ( device->address_len == SPI_AUTODETECT_ADDRESS_LEN ) {
  167. /* Autodetect address length. This relies on
  168. * the device responding with a dummy zero
  169. * data bit before the first real data bit.
  170. */
  171. DBGC ( spibit, "SPIBIT %p autodetecting device "
  172. "address length\n", spibit );
  173. assert ( address == 0 );
  174. device->address_len = 0;
  175. do {
  176. spi_bit_transfer ( spibit, &tmp_address,
  177. &tmp_address_detect, 1,
  178. SPI_BIT_BIG_ENDIAN );
  179. device->address_len++;
  180. } while ( le32_to_cpu ( tmp_address_detect ) & 1 );
  181. DBGC ( spibit, "SPIBIT %p autodetected device address "
  182. "length %d\n", spibit, device->address_len );
  183. } else {
  184. spi_bit_transfer ( spibit, &tmp_address, NULL,
  185. device->address_len,
  186. SPI_BIT_BIG_ENDIAN );
  187. }
  188. }
  189. /* Transmit/receive data */
  190. spi_bit_transfer ( spibit, data_out, data_in, ( len * 8 ),
  191. spibit->endianness );
  192. /* Deassert chip select on specified slave */
  193. spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
  194. /* Close bit-bashing interface */
  195. close_bit ( &spibit->basher );
  196. return 0;
  197. }
  198. /**
  199. * Initialise SPI bit-bashing interface
  200. *
  201. * @v spibit SPI bit-bashing interface
  202. */
  203. void init_spi_bit_basher ( struct spi_bit_basher *spibit ) {
  204. assert ( &spibit->basher.op->read != NULL );
  205. assert ( &spibit->basher.op->write != NULL );
  206. spibit->bus.rw = spi_bit_rw;
  207. }