Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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