caif_spi_slave.c 6.4 KB

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