spi-dw.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef DW_SPI_HEADER_H
  3. #define DW_SPI_HEADER_H
  4. #include <linux/io.h>
  5. #include <linux/scatterlist.h>
  6. #include <linux/gpio.h>
  7. /* Register offsets */
  8. #define DW_SPI_CTRL0 0x00
  9. #define DW_SPI_CTRL1 0x04
  10. #define DW_SPI_SSIENR 0x08
  11. #define DW_SPI_MWCR 0x0c
  12. #define DW_SPI_SER 0x10
  13. #define DW_SPI_BAUDR 0x14
  14. #define DW_SPI_TXFLTR 0x18
  15. #define DW_SPI_RXFLTR 0x1c
  16. #define DW_SPI_TXFLR 0x20
  17. #define DW_SPI_RXFLR 0x24
  18. #define DW_SPI_SR 0x28
  19. #define DW_SPI_IMR 0x2c
  20. #define DW_SPI_ISR 0x30
  21. #define DW_SPI_RISR 0x34
  22. #define DW_SPI_TXOICR 0x38
  23. #define DW_SPI_RXOICR 0x3c
  24. #define DW_SPI_RXUICR 0x40
  25. #define DW_SPI_MSTICR 0x44
  26. #define DW_SPI_ICR 0x48
  27. #define DW_SPI_DMACR 0x4c
  28. #define DW_SPI_DMATDLR 0x50
  29. #define DW_SPI_DMARDLR 0x54
  30. #define DW_SPI_IDR 0x58
  31. #define DW_SPI_VERSION 0x5c
  32. #define DW_SPI_DR 0x60
  33. /* Bit fields in CTRLR0 */
  34. #define SPI_DFS_OFFSET 0
  35. #define SPI_FRF_OFFSET 4
  36. #define SPI_FRF_SPI 0x0
  37. #define SPI_FRF_SSP 0x1
  38. #define SPI_FRF_MICROWIRE 0x2
  39. #define SPI_FRF_RESV 0x3
  40. #define SPI_MODE_OFFSET 6
  41. #define SPI_SCPH_OFFSET 6
  42. #define SPI_SCOL_OFFSET 7
  43. #define SPI_TMOD_OFFSET 8
  44. #define SPI_TMOD_MASK (0x3 << SPI_TMOD_OFFSET)
  45. #define SPI_TMOD_TR 0x0 /* xmit & recv */
  46. #define SPI_TMOD_TO 0x1 /* xmit only */
  47. #define SPI_TMOD_RO 0x2 /* recv only */
  48. #define SPI_TMOD_EPROMREAD 0x3 /* eeprom read mode */
  49. #define SPI_SLVOE_OFFSET 10
  50. #define SPI_SRL_OFFSET 11
  51. #define SPI_CFS_OFFSET 12
  52. /* Bit fields in SR, 7 bits */
  53. #define SR_MASK 0x7f /* cover 7 bits */
  54. #define SR_BUSY (1 << 0)
  55. #define SR_TF_NOT_FULL (1 << 1)
  56. #define SR_TF_EMPT (1 << 2)
  57. #define SR_RF_NOT_EMPT (1 << 3)
  58. #define SR_RF_FULL (1 << 4)
  59. #define SR_TX_ERR (1 << 5)
  60. #define SR_DCOL (1 << 6)
  61. /* Bit fields in ISR, IMR, RISR, 7 bits */
  62. #define SPI_INT_TXEI (1 << 0)
  63. #define SPI_INT_TXOI (1 << 1)
  64. #define SPI_INT_RXUI (1 << 2)
  65. #define SPI_INT_RXOI (1 << 3)
  66. #define SPI_INT_RXFI (1 << 4)
  67. #define SPI_INT_MSTI (1 << 5)
  68. /* Bit fields in DMACR */
  69. #define SPI_DMA_RDMAE (1 << 0)
  70. #define SPI_DMA_TDMAE (1 << 1)
  71. /* TX RX interrupt level threshold, max can be 256 */
  72. #define SPI_INT_THRESHOLD 32
  73. enum dw_ssi_type {
  74. SSI_MOTO_SPI = 0,
  75. SSI_TI_SSP,
  76. SSI_NS_MICROWIRE,
  77. };
  78. struct dw_spi;
  79. struct dw_spi_dma_ops {
  80. int (*dma_init)(struct dw_spi *dws);
  81. void (*dma_exit)(struct dw_spi *dws);
  82. int (*dma_setup)(struct dw_spi *dws, struct spi_transfer *xfer);
  83. bool (*can_dma)(struct spi_controller *master, struct spi_device *spi,
  84. struct spi_transfer *xfer);
  85. int (*dma_transfer)(struct dw_spi *dws, struct spi_transfer *xfer);
  86. void (*dma_stop)(struct dw_spi *dws);
  87. };
  88. struct dw_spi {
  89. struct spi_controller *master;
  90. enum dw_ssi_type type;
  91. void __iomem *regs;
  92. unsigned long paddr;
  93. int irq;
  94. u32 fifo_len; /* depth of the FIFO buffer */
  95. u32 max_freq; /* max bus freq supported */
  96. u32 reg_io_width; /* DR I/O width in bytes */
  97. u16 bus_num;
  98. u16 num_cs; /* supported slave numbers */
  99. void (*set_cs)(struct spi_device *spi, bool enable);
  100. /* Current message transfer state info */
  101. size_t len;
  102. void *tx;
  103. void *tx_end;
  104. spinlock_t buf_lock;
  105. void *rx;
  106. void *rx_end;
  107. int dma_mapped;
  108. u8 n_bytes; /* current is a 1/2 bytes op */
  109. u32 dma_width;
  110. irqreturn_t (*transfer_handler)(struct dw_spi *dws);
  111. u32 current_freq; /* frequency in hz */
  112. /* DMA info */
  113. int dma_inited;
  114. struct dma_chan *txchan;
  115. struct dma_chan *rxchan;
  116. unsigned long dma_chan_busy;
  117. dma_addr_t dma_addr; /* phy address of the Data register */
  118. const struct dw_spi_dma_ops *dma_ops;
  119. void *dma_tx;
  120. void *dma_rx;
  121. /* Bus interface info */
  122. void *priv;
  123. #ifdef CONFIG_DEBUG_FS
  124. struct dentry *debugfs;
  125. #endif
  126. };
  127. static inline u32 dw_readl(struct dw_spi *dws, u32 offset)
  128. {
  129. return __raw_readl(dws->regs + offset);
  130. }
  131. static inline u16 dw_readw(struct dw_spi *dws, u32 offset)
  132. {
  133. return __raw_readw(dws->regs + offset);
  134. }
  135. static inline void dw_writel(struct dw_spi *dws, u32 offset, u32 val)
  136. {
  137. __raw_writel(val, dws->regs + offset);
  138. }
  139. static inline void dw_writew(struct dw_spi *dws, u32 offset, u16 val)
  140. {
  141. __raw_writew(val, dws->regs + offset);
  142. }
  143. static inline u32 dw_read_io_reg(struct dw_spi *dws, u32 offset)
  144. {
  145. switch (dws->reg_io_width) {
  146. case 2:
  147. return dw_readw(dws, offset);
  148. case 4:
  149. default:
  150. return dw_readl(dws, offset);
  151. }
  152. }
  153. static inline void dw_write_io_reg(struct dw_spi *dws, u32 offset, u32 val)
  154. {
  155. switch (dws->reg_io_width) {
  156. case 2:
  157. dw_writew(dws, offset, val);
  158. break;
  159. case 4:
  160. default:
  161. dw_writel(dws, offset, val);
  162. break;
  163. }
  164. }
  165. static inline void spi_enable_chip(struct dw_spi *dws, int enable)
  166. {
  167. dw_writel(dws, DW_SPI_SSIENR, (enable ? 1 : 0));
  168. }
  169. static inline void spi_set_clk(struct dw_spi *dws, u16 div)
  170. {
  171. dw_writel(dws, DW_SPI_BAUDR, div);
  172. }
  173. /* Disable IRQ bits */
  174. static inline void spi_mask_intr(struct dw_spi *dws, u32 mask)
  175. {
  176. u32 new_mask;
  177. new_mask = dw_readl(dws, DW_SPI_IMR) & ~mask;
  178. dw_writel(dws, DW_SPI_IMR, new_mask);
  179. }
  180. /* Enable IRQ bits */
  181. static inline void spi_umask_intr(struct dw_spi *dws, u32 mask)
  182. {
  183. u32 new_mask;
  184. new_mask = dw_readl(dws, DW_SPI_IMR) | mask;
  185. dw_writel(dws, DW_SPI_IMR, new_mask);
  186. }
  187. /*
  188. * This does disable the SPI controller, interrupts, and re-enable the
  189. * controller back. Transmit and receive FIFO buffers are cleared when the
  190. * device is disabled.
  191. */
  192. static inline void spi_reset_chip(struct dw_spi *dws)
  193. {
  194. spi_enable_chip(dws, 0);
  195. spi_mask_intr(dws, 0xff);
  196. spi_enable_chip(dws, 1);
  197. }
  198. static inline void spi_shutdown_chip(struct dw_spi *dws)
  199. {
  200. spi_enable_chip(dws, 0);
  201. spi_set_clk(dws, 0);
  202. }
  203. /*
  204. * Each SPI slave device to work with dw_api controller should
  205. * has such a structure claiming its working mode (poll or PIO/DMA),
  206. * which can be save in the "controller_data" member of the
  207. * struct spi_device.
  208. */
  209. struct dw_spi_chip {
  210. u8 poll_mode; /* 1 for controller polling mode */
  211. u8 type; /* SPI/SSP/MicroWire */
  212. void (*cs_control)(u32 command);
  213. };
  214. extern void dw_spi_set_cs(struct spi_device *spi, bool enable);
  215. extern int dw_spi_add_host(struct device *dev, struct dw_spi *dws);
  216. extern void dw_spi_remove_host(struct dw_spi *dws);
  217. extern int dw_spi_suspend_host(struct dw_spi *dws);
  218. extern int dw_spi_resume_host(struct dw_spi *dws);
  219. /* platform related setup */
  220. extern int dw_spi_mid_init(struct dw_spi *dws); /* Intel MID platforms */
  221. #endif /* DW_SPI_HEADER_H */