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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <errno.h>
  22. #include <assert.h>
  23. #include <timer.h>
  24. #include <gpxe/bitbash.h>
  25. #include <gpxe/spi.h>
  26. /** @file
  27. *
  28. * SPI bit-bashing interface
  29. *
  30. */
  31. /** Delay between SCLK changes and around SS changes */
  32. static void spi_delay ( void ) {
  33. udelay ( SPI_UDELAY );
  34. }
  35. /**
  36. * Select/deselect slave
  37. *
  38. * @v spi SPI bit-bashing interface
  39. * @v slave Slave number
  40. * @v state Slave select state
  41. *
  42. * @c state must be set to zero to select the specified slave, or to
  43. * @c SPI_MODE_SSPOL to deselect the slave.
  44. */
  45. static void spi_bit_set_slave_select ( struct spi_bit_basher *spibit,
  46. unsigned int slave,
  47. unsigned int state ) {
  48. struct bit_basher *basher = &spibit->basher;
  49. state ^= ( spibit->spi.mode & SPI_MODE_SSPOL );
  50. DBG ( "Setting slave %d select %s\n", slave,
  51. ( state ? "high" : "low" ) );
  52. spi_delay();
  53. write_bit ( basher, SPI_BIT_SS ( slave ), state );
  54. spi_delay();
  55. }
  56. /**
  57. * Select slave
  58. *
  59. * @v spi SPI interface
  60. * @v slave Slave number
  61. */
  62. static void spi_bit_select_slave ( struct spi_interface *spi,
  63. unsigned int slave ) {
  64. struct spi_bit_basher *spibit
  65. = container_of ( spi, struct spi_bit_basher, spi );
  66. spibit->slave = slave;
  67. spi_bit_set_slave_select ( spibit, slave, 0 );
  68. }
  69. /**
  70. * Deselect slave
  71. *
  72. * @v spi SPI interface
  73. */
  74. static void spi_bit_deselect_slave ( struct spi_interface *spi ) {
  75. struct spi_bit_basher *spibit
  76. = container_of ( spi, struct spi_bit_basher, spi );
  77. spi_bit_set_slave_select ( spibit, spibit->slave, SPI_MODE_SSPOL );
  78. }
  79. /**
  80. * Transfer bits over SPI bit-bashing interface
  81. *
  82. * @v spi SPI interface
  83. * @v data_out TX data buffer (or NULL)
  84. * @v data_in RX data buffer (or NULL)
  85. * @v len Length of transfer (in @b bits)
  86. *
  87. * This issues @c len clock cycles on the SPI bus, shifting out data
  88. * from the @c data_out buffer to the MOSI line and shifting in data
  89. * from the MISO line to the @c data_in buffer. If @c data_out is
  90. * NULL, then the data sent will be all zeroes. If @c data_in is
  91. * NULL, then the incoming data will be discarded.
  92. */
  93. static void spi_bit_transfer ( struct spi_interface *spi, const void *data_out,
  94. void *data_in, unsigned int len ) {
  95. struct spi_bit_basher *spibit
  96. = container_of ( spi, struct spi_bit_basher, spi );
  97. struct bit_basher *basher = &spibit->basher;
  98. unsigned int sclk = ( ( spi->mode & SPI_MODE_CPOL ) ? 1 : 0 );
  99. unsigned int cpha = ( ( spi->mode & SPI_MODE_CPHA ) ? 1 : 0 );
  100. unsigned int offset;
  101. unsigned int mask;
  102. unsigned int bit;
  103. int step;
  104. DBG ( "Transferring %d bits in mode %x\n", len, spi->mode );
  105. for ( step = ( ( len * 2 ) - 1 ) ; step >= 0 ; step-- ) {
  106. /* Calculate byte offset within data and bit mask */
  107. offset = ( step / 16 );
  108. mask = ( 1 << ( ( step % 16 ) / 2 ) );
  109. /* Shift data in or out */
  110. if ( sclk == cpha ) {
  111. const uint8_t *byte;
  112. /* Shift data out */
  113. if ( data_out ) {
  114. byte = ( data_out + offset );
  115. bit = ( *byte & mask );
  116. } else {
  117. bit = 0;
  118. }
  119. write_bit ( basher, SPI_BIT_MOSI, bit );
  120. } else {
  121. uint8_t *byte;
  122. /* Shift data in */
  123. bit = read_bit ( basher, SPI_BIT_MISO );
  124. if ( data_in ) {
  125. byte = ( data_in + offset );
  126. *byte &= ~mask;
  127. *byte |= ( bit & mask );
  128. }
  129. }
  130. /* Toggle clock line */
  131. spi_delay();
  132. sclk = ~sclk;
  133. write_bit ( basher, SPI_BIT_SCLK, sclk );
  134. }
  135. }
  136. /**
  137. * Initialise SPI bit-bashing interface
  138. *
  139. * @v spibit SPI bit-bashing interface
  140. */
  141. void init_spi_bit_basher ( struct spi_bit_basher *spibit ) {
  142. assert ( &spibit->basher.read != NULL );
  143. assert ( &spibit->basher.write != NULL );
  144. spibit->spi.select_slave = spi_bit_select_slave;
  145. spibit->spi.deselect_slave = spi_bit_deselect_slave;
  146. spibit->spi.transfer = spi_bit_transfer;
  147. }