dw_spi.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (C) Cvitek Co., Ltd. 2019-2020. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __DW_SPI_HEADER_H__
  17. #define __DW_SPI_HEADER_H__
  18. #include "stdint.h"
  19. #include "stdbool.h"
  20. #define SPI_REGBASE 0x04180000
  21. #define SPI_REF_CLK 187500000
  22. #define MAX_SPI_NUM 4
  23. #define CVI_DW_SPI_CTRLR0 0x00
  24. #define CVI_DW_SPI_CTRLR1 0x04
  25. #define CVI_DW_SPI_SSIENR 0x08
  26. #define CVI_DW_SPI_MWCR 0x0c
  27. #define CVI_DW_SPI_SER 0x10
  28. #define CVI_DW_SPI_BAUDR 0x14
  29. #define CVI_DW_SPI_TXFTLR 0x18
  30. #define CVI_DW_SPI_RXFTLR 0x1c
  31. #define CVI_DW_SPI_TXFLR 0x20
  32. #define CVI_DW_SPI_RXFLR 0x24
  33. #define CVI_DW_SPI_SR 0x28
  34. #define CVI_DW_SPI_IMR 0x2c
  35. #define CVI_DW_SPI_ISR 0x30
  36. #define CVI_DW_SPI_RISR 0x34
  37. #define CVI_DW_SPI_TXOICR 0x38
  38. #define CVI_DW_SPI_RXOICR 0x3c
  39. #define CVI_DW_SPI_RXUICR 0x40
  40. #define CVI_DW_SPI_MSTICR 0x44
  41. #define CVI_DW_SPI_ICR 0x48
  42. #define CVI_DW_SPI_DMACR 0x4c
  43. #define CVI_DW_SPI_DMATDLR 0x50
  44. #define CVI_DW_SPI_DMARDLR 0x54
  45. #define CVI_DW_SPI_IDR 0x58
  46. #define CVI_DW_SPI_VERSION 0x5c
  47. #define CVI_DW_SPI_DR 0x60
  48. /* Bit fields in CTRLR0 */
  49. #define CVI_SPI_DFS_OFFSET 0
  50. #define CVI_SPI_FRF_OFFSET 4
  51. #define CVI_SPI_FRF_SPI 0x0
  52. #define CVI_SPI_FRF_SSP 0x1
  53. #define CVI_SPI_FRF_MICROWIRE 0x2
  54. #define CVI_SPI_FRF_RESV 0x3
  55. #define CVI_SPI_MODE_OFFSET 6
  56. #define CVI_SPI_SCPH_OFFSET 6
  57. #define CVI_SPI_SCOL_OFFSET 7
  58. #define CVI_SPI_TMOD_OFFSET 8
  59. #define CVI_SPI_TMOD_MASK (0x3 << CVI_SPI_TMOD_OFFSET)
  60. #define CVI_SPI_TMOD_TR 0x0 /* xmit & recv */
  61. #define CVI_SPI_TMOD_TO 0x1 /* xmit only */
  62. #define CVI_SPI_TMOD_RO 0x2 /* recv only */
  63. #define CVI_SPI_TMOD_EPROMREAD 0x3 /* eeprom read mode */
  64. #define CVI_SPI_SLVOE_OFFSET 10
  65. #define CVI_SPI_SRL_OFFSET 11
  66. #define CVI_SPI_CFS_OFFSET 12
  67. /* Bit fields in SR, 7 bits */
  68. #define CVI_SR_MASK 0x7f
  69. #define CVI_SR_BUSY (1 << 0)
  70. #define CVI_SR_TF_NOT_FULL (1 << 1)
  71. #define CVI_SR_TF_EMPT (1 << 2)
  72. #define CVI_SR_RF_NOT_EMPT (1 << 3)
  73. #define CVI_SR_RF_FULL (1 << 4)
  74. #define CVI_SR_TX_ERR (1 << 5)
  75. #define SR_DCOL (1 << 6)
  76. /* Bit fields in ISR, IMR, RISR, 7 bits */
  77. #define CVI_SPI_INT_TXEI (1 << 0)
  78. #define CVI_SPI_INT_TXOI (1 << 1)
  79. #define CVI_SPI_INT_RXUI (1 << 2)
  80. #define CVI_SPI_INT_RXOI (1 << 3)
  81. #define CVI_SPI_INT_RXFI (1 << 4)
  82. #define CVI_SPI_INT_MSTI (1 << 5)
  83. /* Bit fields in DMACR */
  84. #define CVI_SPI_DMA_RDMAE (1 << 0)
  85. #define CVI_SPI_DMA_TDMAE (1 << 1)
  86. /* TX RX interrupt level threshold, max can be 256 */
  87. #define CVI_SPI_INT_THRESHOLD 32
  88. #define BITS_PER_BYTE 8
  89. #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
  90. struct dw_spi {
  91. void *regs;
  92. int irq;
  93. int index;
  94. uint32_t fifo_len; /* depth of the FIFO buffer */
  95. uint16_t num_cs; /* supported slave numbers */
  96. uint32_t speed_hz;
  97. /* Current message transfer state info */
  98. size_t len;
  99. const void *tx;
  100. const void *tx_end;
  101. void *rx;
  102. void *rx_end;
  103. uint32_t rx_len;
  104. uint32_t tx_len;
  105. uint8_t n_bytes; /* current is a 1/2 bytes op */
  106. uint32_t dma_width;
  107. int (*transfer_handler)(struct dw_spi *dws);
  108. /* Bus interface info */
  109. void *priv;
  110. };
  111. struct spi_delay {
  112. #define SPI_DELAY_UNIT_USECS 0
  113. #define SPI_DELAY_UNIT_NSECS 1
  114. #define SPI_DELAY_UNIT_SCK 2
  115. uint16_t value;
  116. uint8_t unit;
  117. };
  118. #define SPI_CPHA 0x01
  119. #define SPI_CPOL 0x02
  120. #define SPI_MODE_0 (0|0)
  121. #define SPI_MODE_1 (0|SPI_CPHA)
  122. #define SPI_MODE_2 (SPI_CPOL|0)
  123. #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
  124. enum transfer_type {
  125. POLL_TRAN = 0,
  126. IRQ_TRAN,
  127. DMA_TRAN,
  128. };
  129. enum dw_ssi_type {
  130. SSI_MOTO_SPI = 0,
  131. SSI_TI_SSP,
  132. SSI_NS_MICROWIRE,
  133. };
  134. #define SPI_FORMAT_CPOL0_CPHA0 0
  135. #define SPI_FORMAT_CPOL0_CPHA1 1
  136. #define SPI_FORMAT_CPOL1_CPHA0 2
  137. #define SPI_FORMAT_CPOL1_CPHA1 3
  138. #ifndef BIT
  139. #define BIT(_n) ( 1 << (_n))
  140. #endif
  141. static void dw_writel(struct dw_spi *dws, uint32_t off, uint32_t val)
  142. {
  143. writel(val, (dws->regs + off));
  144. }
  145. static uint32_t dw_readl(struct dw_spi *dws, uint32_t off)
  146. {
  147. return readl(dws->regs + off);
  148. }
  149. static inline void spi_enable_chip(struct dw_spi *dws, int enable)
  150. {
  151. dw_writel(dws, CVI_DW_SPI_SSIENR, (enable ? 1 : 0));
  152. }
  153. static inline void spi_set_clk(struct dw_spi *dws, uint16_t div)
  154. {
  155. dw_writel(dws, CVI_DW_SPI_BAUDR, div);
  156. }
  157. /* Disable IRQ bits */
  158. static inline void spi_mask_intr(struct dw_spi *dws, uint32_t mask)
  159. {
  160. uint32_t new_mask;
  161. new_mask = dw_readl(dws, CVI_DW_SPI_IMR) & ~mask;
  162. dw_writel(dws, CVI_DW_SPI_IMR, new_mask);
  163. }
  164. static inline uint32_t spi_get_status(struct dw_spi *dws)
  165. {
  166. return dw_readl(dws, CVI_DW_SPI_SR);
  167. }
  168. /* Enable IRQ bits */
  169. static inline void spi_umask_intr(struct dw_spi *dws, uint32_t mask)
  170. {
  171. uint32_t new_mask;
  172. new_mask = dw_readl(dws, CVI_DW_SPI_IMR) | mask;
  173. dw_writel(dws, CVI_DW_SPI_IMR, new_mask);
  174. }
  175. static inline void spi_reset_chip(struct dw_spi *dws)
  176. {
  177. spi_enable_chip(dws, 0);
  178. spi_mask_intr(dws, 0xff);
  179. dw_readl(dws, CVI_DW_SPI_ICR);
  180. dw_writel(dws, CVI_DW_SPI_SER, 0);
  181. spi_enable_chip(dws, 1);
  182. }
  183. static inline void spi_enable_dma(struct dw_spi *dws, uint8_t is_tx, uint8_t op)
  184. {
  185. /* 1: TDMAE, 0: RDMAE */
  186. uint32_t val = dw_readl(dws, CVI_DW_SPI_DMACR);
  187. if (op)
  188. val |= 1 << (!!is_tx);
  189. else
  190. val &= ~(1 << (!!is_tx));
  191. dw_writel(dws, CVI_DW_SPI_DMACR, val);
  192. }
  193. static inline void spi_shutdown_chip(struct dw_spi *dws)
  194. {
  195. spi_enable_chip(dws, 0);
  196. spi_set_clk(dws, 0);
  197. }
  198. void spi_hw_init(struct dw_spi *dws);
  199. void dw_spi_set_controller_mode(struct dw_spi *dws, uint8_t enable_master);
  200. void dw_spi_set_polarity_and_phase(struct dw_spi *dws, uint8_t format);
  201. uint32_t dw_spi_set_clock(struct dw_spi *dws, uint32_t clock_in, uint32_t clock_out);
  202. int dw_spi_set_data_frame_len(struct dw_spi *dws, uint32_t size);
  203. void dw_spi_set_cs(struct dw_spi *dws, bool enable, uint32_t index);
  204. void dw_reader(struct dw_spi *dws);
  205. void dw_writer(struct dw_spi *dws);
  206. void set_tran_mode(struct dw_spi *dws);
  207. void dw_spi_show_regs(struct dw_spi *dws);
  208. int poll_transfer(struct dw_spi *dws);
  209. int dw_spi_check_status(struct dw_spi *dws, bool raw);
  210. #endif