caif_spi_slave.c 6.4 KB

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