caif_spi_slave.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (C) ST-Ericsson AB 2010
  3. * Author: Daniel Martensson
  4. * License terms: GNU General Public License (GPL) version 2.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/device.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/string.h>
  10. #include <linux/semaphore.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/completion.h>
  13. #include <linux/list.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/delay.h>
  17. #include <linux/sched.h>
  18. #include <linux/debugfs.h>
  19. #include <net/caif/caif_spi.h>
  20. #ifndef CONFIG_CAIF_SPI_SYNC
  21. #define SPI_DATA_POS 0
  22. static inline int forward_to_spi_cmd(struct cfspi *cfspi)
  23. {
  24. return cfspi->rx_cpck_len;
  25. }
  26. #else
  27. #define SPI_DATA_POS SPI_CMD_SZ
  28. static inline int forward_to_spi_cmd(struct cfspi *cfspi)
  29. {
  30. return 0;
  31. }
  32. #endif
  33. int spi_frm_align = 2;
  34. /*
  35. * SPI padding options.
  36. * Warning: must be a base of 2 (& operation used) and can not be zero !
  37. */
  38. int spi_up_head_align = 1 << 1;
  39. int spi_up_tail_align = 1 << 0;
  40. int spi_down_head_align = 1 << 2;
  41. int spi_down_tail_align = 1 << 1;
  42. #ifdef CONFIG_DEBUG_FS
  43. static inline void debugfs_store_prev(struct cfspi *cfspi)
  44. {
  45. /* Store previous command for debugging reasons.*/
  46. cfspi->pcmd = cfspi->cmd;
  47. /* Store previous transfer. */
  48. cfspi->tx_ppck_len = cfspi->tx_cpck_len;
  49. cfspi->rx_ppck_len = cfspi->rx_cpck_len;
  50. }
  51. #else
  52. static inline void debugfs_store_prev(struct cfspi *cfspi)
  53. {
  54. }
  55. #endif
  56. void cfspi_xfer(struct work_struct *work)
  57. {
  58. struct cfspi *cfspi;
  59. u8 *ptr = NULL;
  60. unsigned long flags;
  61. int ret;
  62. cfspi = container_of(work, struct cfspi, work);
  63. /* Initialize state. */
  64. cfspi->cmd = SPI_CMD_EOT;
  65. for (;;) {
  66. cfspi_dbg_state(cfspi, CFSPI_STATE_WAITING);
  67. /* Wait for master talk or transmit event. */
  68. wait_event_interruptible(cfspi->wait,
  69. test_bit(SPI_XFER, &cfspi->state) ||
  70. test_bit(SPI_TERMINATE, &cfspi->state));
  71. if (test_bit(SPI_TERMINATE, &cfspi->state))
  72. return;
  73. #if CFSPI_DBG_PREFILL
  74. /* Prefill buffers for easier debugging. */
  75. memset(cfspi->xfer.va_tx, 0xFF, SPI_DMA_BUF_LEN);
  76. memset(cfspi->xfer.va_rx, 0xFF, SPI_DMA_BUF_LEN);
  77. #endif /* CFSPI_DBG_PREFILL */
  78. cfspi_dbg_state(cfspi, CFSPI_STATE_AWAKE);
  79. /* Check whether we have a committed frame. */
  80. if (cfspi->tx_cpck_len) {
  81. int len;
  82. cfspi_dbg_state(cfspi, CFSPI_STATE_FETCH_PKT);
  83. /* Copy committed SPI frames after the SPI indication. */
  84. ptr = (u8 *) cfspi->xfer.va_tx;
  85. ptr += SPI_IND_SZ;
  86. len = cfspi_xmitfrm(cfspi, ptr, cfspi->tx_cpck_len);
  87. WARN_ON(len != cfspi->tx_cpck_len);
  88. }
  89. cfspi_dbg_state(cfspi, CFSPI_STATE_GET_NEXT);
  90. /* Get length of next frame to commit. */
  91. cfspi->tx_npck_len = cfspi_xmitlen(cfspi);
  92. WARN_ON(cfspi->tx_npck_len > SPI_DMA_BUF_LEN);
  93. /*
  94. * Add indication and length at the beginning of the frame,
  95. * using little endian.
  96. */
  97. ptr = (u8 *) cfspi->xfer.va_tx;
  98. *ptr++ = SPI_CMD_IND;
  99. *ptr++ = (SPI_CMD_IND & 0xFF00) >> 8;
  100. *ptr++ = cfspi->tx_npck_len & 0x00FF;
  101. *ptr++ = (cfspi->tx_npck_len & 0xFF00) >> 8;
  102. /* Calculate length of DMAs. */
  103. cfspi->xfer.tx_dma_len = cfspi->tx_cpck_len + SPI_IND_SZ;
  104. cfspi->xfer.rx_dma_len = cfspi->rx_cpck_len + SPI_CMD_SZ;
  105. /* Add SPI TX frame alignment padding, if necessary. */
  106. if (cfspi->tx_cpck_len &&
  107. (cfspi->xfer.tx_dma_len % spi_frm_align)) {
  108. cfspi->xfer.tx_dma_len += spi_frm_align -
  109. (cfspi->xfer.tx_dma_len % spi_frm_align);
  110. }
  111. /* Add SPI RX frame alignment padding, if necessary. */
  112. if (cfspi->rx_cpck_len &&
  113. (cfspi->xfer.rx_dma_len % spi_frm_align)) {
  114. cfspi->xfer.rx_dma_len += spi_frm_align -
  115. (cfspi->xfer.rx_dma_len % spi_frm_align);
  116. }
  117. cfspi_dbg_state(cfspi, CFSPI_STATE_INIT_XFER);
  118. /* Start transfer. */
  119. ret = cfspi->dev->init_xfer(&cfspi->xfer, cfspi->dev);
  120. WARN_ON(ret);
  121. cfspi_dbg_state(cfspi, CFSPI_STATE_WAIT_ACTIVE);
  122. /*
  123. * TODO: We might be able to make an assumption if this is the
  124. * first loop. Make sure that minimum toggle time is respected.
  125. */
  126. udelay(MIN_TRANSITION_TIME_USEC);
  127. cfspi_dbg_state(cfspi, CFSPI_STATE_SIG_ACTIVE);
  128. /* Signal that we are ready to receive data. */
  129. cfspi->dev->sig_xfer(true, cfspi->dev);
  130. cfspi_dbg_state(cfspi, CFSPI_STATE_WAIT_XFER_DONE);
  131. /* Wait for transfer completion. */
  132. wait_for_completion(&cfspi->comp);
  133. cfspi_dbg_state(cfspi, CFSPI_STATE_XFER_DONE);
  134. if (cfspi->cmd == SPI_CMD_EOT) {
  135. /*
  136. * Clear the master talk bit. A xfer is always at
  137. * least two bursts.
  138. */
  139. clear_bit(SPI_SS_ON, &cfspi->state);
  140. }
  141. cfspi_dbg_state(cfspi, CFSPI_STATE_WAIT_INACTIVE);
  142. /* Make sure that the minimum toggle time is respected. */
  143. if (SPI_XFER_TIME_USEC(cfspi->xfer.tx_dma_len,
  144. cfspi->dev->clk_mhz) <
  145. MIN_TRANSITION_TIME_USEC) {
  146. udelay(MIN_TRANSITION_TIME_USEC -
  147. SPI_XFER_TIME_USEC
  148. (cfspi->xfer.tx_dma_len, cfspi->dev->clk_mhz));
  149. }
  150. cfspi_dbg_state(cfspi, CFSPI_STATE_SIG_INACTIVE);
  151. /* De-assert transfer signal. */
  152. cfspi->dev->sig_xfer(false, cfspi->dev);
  153. /* Check whether we received a CAIF packet. */
  154. if (cfspi->rx_cpck_len) {
  155. int len;
  156. cfspi_dbg_state(cfspi, CFSPI_STATE_DELIVER_PKT);
  157. /* Parse SPI frame. */
  158. ptr = ((u8 *)(cfspi->xfer.va_rx + SPI_DATA_POS));
  159. len = cfspi_rxfrm(cfspi, ptr, cfspi->rx_cpck_len);
  160. WARN_ON(len != cfspi->rx_cpck_len);
  161. }
  162. /* Check the next SPI command and length. */
  163. ptr = (u8 *) cfspi->xfer.va_rx;
  164. ptr += forward_to_spi_cmd(cfspi);
  165. cfspi->cmd = *ptr++;
  166. cfspi->cmd |= ((*ptr++) << 8) & 0xFF00;
  167. cfspi->rx_npck_len = *ptr++;
  168. cfspi->rx_npck_len |= ((*ptr++) << 8) & 0xFF00;
  169. WARN_ON(cfspi->rx_npck_len > SPI_DMA_BUF_LEN);
  170. WARN_ON(cfspi->cmd > SPI_CMD_EOT);
  171. debugfs_store_prev(cfspi);
  172. /* Check whether the master issued an EOT command. */
  173. if (cfspi->cmd == SPI_CMD_EOT) {
  174. /* Reset state. */
  175. cfspi->tx_cpck_len = 0;
  176. cfspi->rx_cpck_len = 0;
  177. } else {
  178. /* Update state. */
  179. cfspi->tx_cpck_len = cfspi->tx_npck_len;
  180. cfspi->rx_cpck_len = cfspi->rx_npck_len;
  181. }
  182. /*
  183. * Check whether we need to clear the xfer bit.
  184. * Spin lock needed for packet insertion.
  185. * Test and clear of different bits
  186. * are not supported.
  187. */
  188. spin_lock_irqsave(&cfspi->lock, flags);
  189. if (cfspi->cmd == SPI_CMD_EOT && !cfspi_xmitlen(cfspi)
  190. && !test_bit(SPI_SS_ON, &cfspi->state))
  191. clear_bit(SPI_XFER, &cfspi->state);
  192. spin_unlock_irqrestore(&cfspi->lock, flags);
  193. }
  194. }
  195. struct platform_driver cfspi_spi_driver = {
  196. .probe = cfspi_spi_probe,
  197. .remove = cfspi_spi_remove,
  198. .driver = {
  199. .name = "cfspi_sspi",
  200. .owner = THIS_MODULE,
  201. },
  202. };