caif_spi_slave.c 6.5 KB

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