spi-bitbang-txrx.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Mix this utility code with some glue code to get one of several types of
  4. * simple SPI master driver. Two do polled word-at-a-time I/O:
  5. *
  6. * - GPIO/parport bitbangers. Provide chipselect() and txrx_word[](),
  7. * expanding the per-word routines from the inline templates below.
  8. *
  9. * - Drivers for controllers resembling bare shift registers. Provide
  10. * chipselect() and txrx_word[](), with custom setup()/cleanup() methods
  11. * that use your controller's clock and chipselect registers.
  12. *
  13. * Some hardware works well with requests at spi_transfer scope:
  14. *
  15. * - Drivers leveraging smarter hardware, with fifos or DMA; or for half
  16. * duplex (MicroWire) controllers. Provide chipselect() and txrx_bufs(),
  17. * and custom setup()/cleanup() methods.
  18. */
  19. /*
  20. * The code that knows what GPIO pins do what should have declared four
  21. * functions, ideally as inlines, before including this header:
  22. *
  23. * void setsck(struct spi_device *, int is_on);
  24. * void setmosi(struct spi_device *, int is_on);
  25. * int getmiso(struct spi_device *);
  26. * void spidelay(unsigned);
  27. *
  28. * setsck()'s is_on parameter is a zero/nonzero boolean.
  29. *
  30. * setmosi()'s is_on parameter is a zero/nonzero boolean.
  31. *
  32. * getmiso() is required to return 0 or 1 only. Any other value is invalid
  33. * and will result in improper operation.
  34. *
  35. * A non-inlined routine would call bitbang_txrx_*() routines. The
  36. * main loop could easily compile down to a handful of instructions,
  37. * especially if the delay is a NOP (to run at peak speed).
  38. *
  39. * Since this is software, the timings may not be exactly what your board's
  40. * chips need ... there may be several reasons you'd need to tweak timings
  41. * in these routines, not just to make it faster or slower to match a
  42. * particular CPU clock rate.
  43. *
  44. * ToDo: Maybe the bitrev macros can be used to improve the code?
  45. */
  46. static inline u32
  47. bitbang_txrx_be_cpha0(struct spi_device *spi,
  48. unsigned nsecs, unsigned cpol, unsigned flags,
  49. u32 word, u8 bits)
  50. {
  51. /* if (cpol == 0) this is SPI_MODE_0; else this is SPI_MODE_2 */
  52. u32 oldbit = (!(word & (1<<(bits-1)))) << 31;
  53. /* clock starts at inactive polarity */
  54. for (word <<= (32 - bits); likely(bits); bits--) {
  55. /* setup MSB (to slave) on trailing edge */
  56. if ((flags & SPI_MASTER_NO_TX) == 0) {
  57. if ((word & (1 << 31)) != oldbit) {
  58. setmosi(spi, word & (1 << 31));
  59. oldbit = word & (1 << 31);
  60. }
  61. }
  62. spidelay(nsecs); /* T(setup) */
  63. setsck(spi, !cpol);
  64. spidelay(nsecs);
  65. /* sample MSB (from slave) on leading edge */
  66. word <<= 1;
  67. if ((flags & SPI_MASTER_NO_RX) == 0)
  68. word |= getmiso(spi);
  69. setsck(spi, cpol);
  70. }
  71. return word;
  72. }
  73. static inline u32
  74. bitbang_txrx_be_cpha1(struct spi_device *spi,
  75. unsigned nsecs, unsigned cpol, unsigned flags,
  76. u32 word, u8 bits)
  77. {
  78. /* if (cpol == 0) this is SPI_MODE_1; else this is SPI_MODE_3 */
  79. u32 oldbit = (!(word & (1<<(bits-1)))) << 31;
  80. /* clock starts at inactive polarity */
  81. for (word <<= (32 - bits); likely(bits); bits--) {
  82. /* setup MSB (to slave) on leading edge */
  83. setsck(spi, !cpol);
  84. if ((flags & SPI_MASTER_NO_TX) == 0) {
  85. if ((word & (1 << 31)) != oldbit) {
  86. setmosi(spi, word & (1 << 31));
  87. oldbit = word & (1 << 31);
  88. }
  89. }
  90. spidelay(nsecs); /* T(setup) */
  91. setsck(spi, cpol);
  92. spidelay(nsecs);
  93. /* sample MSB (from slave) on trailing edge */
  94. word <<= 1;
  95. if ((flags & SPI_MASTER_NO_RX) == 0)
  96. word |= getmiso(spi);
  97. }
  98. return word;
  99. }
  100. static inline u32
  101. bitbang_txrx_le_cpha0(struct spi_device *spi,
  102. unsigned int nsecs, unsigned int cpol, unsigned int flags,
  103. u32 word, u8 bits)
  104. {
  105. /* if (cpol == 0) this is SPI_MODE_0; else this is SPI_MODE_2 */
  106. u8 rxbit = bits - 1;
  107. u32 oldbit = !(word & 1);
  108. /* clock starts at inactive polarity */
  109. for (; likely(bits); bits--) {
  110. /* setup LSB (to slave) on trailing edge */
  111. if ((flags & SPI_MASTER_NO_TX) == 0) {
  112. if ((word & 1) != oldbit) {
  113. setmosi(spi, word & 1);
  114. oldbit = word & 1;
  115. }
  116. }
  117. spidelay(nsecs); /* T(setup) */
  118. setsck(spi, !cpol);
  119. spidelay(nsecs);
  120. /* sample LSB (from slave) on leading edge */
  121. word >>= 1;
  122. if ((flags & SPI_MASTER_NO_RX) == 0)
  123. word |= getmiso(spi) << rxbit;
  124. setsck(spi, cpol);
  125. }
  126. return word;
  127. }
  128. static inline u32
  129. bitbang_txrx_le_cpha1(struct spi_device *spi,
  130. unsigned int nsecs, unsigned int cpol, unsigned int flags,
  131. u32 word, u8 bits)
  132. {
  133. /* if (cpol == 0) this is SPI_MODE_1; else this is SPI_MODE_3 */
  134. u8 rxbit = bits - 1;
  135. u32 oldbit = !(word & 1);
  136. /* clock starts at inactive polarity */
  137. for (; likely(bits); bits--) {
  138. /* setup LSB (to slave) on leading edge */
  139. setsck(spi, !cpol);
  140. if ((flags & SPI_MASTER_NO_TX) == 0) {
  141. if ((word & 1) != oldbit) {
  142. setmosi(spi, word & 1);
  143. oldbit = word & 1;
  144. }
  145. }
  146. spidelay(nsecs); /* T(setup) */
  147. setsck(spi, cpol);
  148. spidelay(nsecs);
  149. /* sample LSB (from slave) on trailing edge */
  150. word >>= 1;
  151. if ((flags & SPI_MASTER_NO_RX) == 0)
  152. word |= getmiso(spi) << rxbit;
  153. }
  154. return word;
  155. }