spi_bitbang_txrx.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Mix this utility code with some glue code to get one of several types of
  3. * simple SPI master driver. Two do polled word-at-a-time I/O:
  4. *
  5. * - GPIO/parport bitbangers. Provide chipselect() and txrx_word[](),
  6. * expanding the per-word routines from the inline templates below.
  7. *
  8. * - Drivers for controllers resembling bare shift registers. Provide
  9. * chipselect() and txrx_word[](), with custom setup()/cleanup() methods
  10. * that use your controller's clock and chipselect registers.
  11. *
  12. * Some hardware works well with requests at spi_transfer scope:
  13. *
  14. * - Drivers leveraging smarter hardware, with fifos or DMA; or for half
  15. * duplex (MicroWire) controllers. Provide chipselect() and txrx_bufs(),
  16. * and custom setup()/cleanup() methods.
  17. */
  18. /*
  19. * The code that knows what GPIO pins do what should have declared four
  20. * functions, ideally as inlines, before including this header:
  21. *
  22. * void setsck(struct spi_device *, int is_on);
  23. * void setmosi(struct spi_device *, int is_on);
  24. * int getmiso(struct spi_device *);
  25. * void spidelay(unsigned);
  26. *
  27. * setsck()'s is_on parameter is a zero/nonzero boolean.
  28. *
  29. * setmosi()'s is_on parameter is a zero/nonzero boolean.
  30. *
  31. * getmiso() is required to return 0 or 1 only. Any other value is invalid
  32. * and will result in improper operation.
  33. *
  34. * A non-inlined routine would call bitbang_txrx_*() routines. The
  35. * main loop could easily compile down to a handful of instructions,
  36. * especially if the delay is a NOP (to run at peak speed).
  37. *
  38. * Since this is software, the timings may not be exactly what your board's
  39. * chips need ... there may be several reasons you'd need to tweak timings
  40. * in these routines, not just make to make it faster or slower to match a
  41. * particular CPU clock rate.
  42. */
  43. static inline u32
  44. bitbang_txrx_be_cpha0(struct spi_device *spi,
  45. unsigned nsecs, unsigned cpol, unsigned flags,
  46. u32 word, u8 bits)
  47. {
  48. /* if (cpol == 0) this is SPI_MODE_0; else this is SPI_MODE_2 */
  49. /* clock starts at inactive polarity */
  50. for (word <<= (32 - bits); likely(bits); bits--) {
  51. /* setup MSB (to slave) on trailing edge */
  52. if ((flags & SPI_MASTER_NO_TX) == 0)
  53. setmosi(spi, word & (1 << 31));
  54. spidelay(nsecs); /* T(setup) */
  55. setsck(spi, !cpol);
  56. spidelay(nsecs);
  57. /* sample MSB (from slave) on leading edge */
  58. word <<= 1;
  59. if ((flags & SPI_MASTER_NO_RX) == 0)
  60. word |= getmiso(spi);
  61. setsck(spi, cpol);
  62. }
  63. return word;
  64. }
  65. static inline u32
  66. bitbang_txrx_be_cpha1(struct spi_device *spi,
  67. unsigned nsecs, unsigned cpol, unsigned flags,
  68. u32 word, u8 bits)
  69. {
  70. /* if (cpol == 0) this is SPI_MODE_1; else this is SPI_MODE_3 */
  71. /* clock starts at inactive polarity */
  72. for (word <<= (32 - bits); likely(bits); bits--) {
  73. /* setup MSB (to slave) on leading edge */
  74. setsck(spi, !cpol);
  75. if ((flags & SPI_MASTER_NO_TX) == 0)
  76. setmosi(spi, word & (1 << 31));
  77. spidelay(nsecs); /* T(setup) */
  78. setsck(spi, cpol);
  79. spidelay(nsecs);
  80. /* sample MSB (from slave) on trailing edge */
  81. word <<= 1;
  82. if ((flags & SPI_MASTER_NO_RX) == 0)
  83. word |= getmiso(spi);
  84. }
  85. return word;
  86. }