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

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