caif_spi_slave.c 6.5 KB

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