spi-dw.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __SPI_DW_H__
  3. #define __SPI_DW_H__
  4. #include <linux/bits.h>
  5. #include <linux/completion.h>
  6. #include <linux/debugfs.h>
  7. #include <linux/irqreturn.h>
  8. #include <linux/io.h>
  9. #include <linux/scatterlist.h>
  10. #include <linux/spi/spi-mem.h>
  11. #include <linux/bitfield.h>
  12. /* Synopsys DW SSI IP-core virtual IDs */
  13. #define DW_PSSI_ID 0
  14. #define DW_HSSI_ID 1
  15. /* Synopsys DW SSI component versions (FourCC sequence) */
  16. #define DW_HSSI_102A 0x3130322a
  17. /* DW SSI IP-core ID and version check helpers */
  18. #define dw_spi_ip_is(_dws, _ip) \
  19. ((_dws)->ip == DW_ ## _ip ## _ID)
  20. #define __dw_spi_ver_cmp(_dws, _ip, _ver, _op) \
  21. (dw_spi_ip_is(_dws, _ip) && (_dws)->ver _op DW_ ## _ip ## _ ## _ver)
  22. #define dw_spi_ver_is(_dws, _ip, _ver) __dw_spi_ver_cmp(_dws, _ip, _ver, ==)
  23. #define dw_spi_ver_is_ge(_dws, _ip, _ver) __dw_spi_ver_cmp(_dws, _ip, _ver, >=)
  24. /* DW SPI controller capabilities */
  25. #define DW_SPI_CAP_CS_OVERRIDE BIT(0)
  26. #define DW_SPI_CAP_DFS32 BIT(1)
  27. /* Register offsets (Generic for both DWC APB SSI and DWC SSI IP-cores) */
  28. #define DW_SPI_CTRLR0 0x00
  29. #define DW_SPI_CTRLR1 0x04
  30. #define DW_SPI_SSIENR 0x08
  31. #define DW_SPI_MWCR 0x0c
  32. #define DW_SPI_SER 0x10
  33. #define DW_SPI_BAUDR 0x14
  34. #define DW_SPI_TXFTLR 0x18
  35. #define DW_SPI_RXFTLR 0x1c
  36. #define DW_SPI_TXFLR 0x20
  37. #define DW_SPI_RXFLR 0x24
  38. #define DW_SPI_SR 0x28
  39. #define DW_SPI_IMR 0x2c
  40. #define DW_SPI_ISR 0x30
  41. #define DW_SPI_RISR 0x34
  42. #define DW_SPI_TXOICR 0x38
  43. #define DW_SPI_RXOICR 0x3c
  44. #define DW_SPI_RXUICR 0x40
  45. #define DW_SPI_MSTICR 0x44
  46. #define DW_SPI_ICR 0x48
  47. #define DW_SPI_DMACR 0x4c
  48. #define DW_SPI_DMATDLR 0x50
  49. #define DW_SPI_DMARDLR 0x54
  50. #define DW_SPI_IDR 0x58
  51. #define DW_SPI_VERSION 0x5c
  52. #define DW_SPI_DR 0x60
  53. #define DW_SPI_RX_SAMPLE_DLY 0xf0
  54. #define DW_SPI_CS_OVERRIDE 0xf4
  55. /* Bit fields in CTRLR0 (DWC APB SSI) */
  56. #define DW_PSSI_CTRLR0_DFS_MASK GENMASK(3, 0)
  57. #define DW_PSSI_CTRLR0_DFS32_MASK GENMASK(20, 16)
  58. #define DW_PSSI_CTRLR0_FRF_MASK GENMASK(5, 4)
  59. #define DW_SPI_CTRLR0_FRF_MOTO_SPI 0x0
  60. #define DW_SPI_CTRLR0_FRF_TI_SSP 0x1
  61. #define DW_SPI_CTRLR0_FRF_NS_MICROWIRE 0x2
  62. #define DW_SPI_CTRLR0_FRF_RESV 0x3
  63. #define DW_PSSI_CTRLR0_MODE_MASK GENMASK(7, 6)
  64. #define DW_PSSI_CTRLR0_SCPHA BIT(6)
  65. #define DW_PSSI_CTRLR0_SCPOL BIT(7)
  66. #define DW_PSSI_CTRLR0_TMOD_MASK GENMASK(9, 8)
  67. #define DW_SPI_CTRLR0_TMOD_TR 0x0 /* xmit & recv */
  68. #define DW_SPI_CTRLR0_TMOD_TO 0x1 /* xmit only */
  69. #define DW_SPI_CTRLR0_TMOD_RO 0x2 /* recv only */
  70. #define DW_SPI_CTRLR0_TMOD_EPROMREAD 0x3 /* eeprom read mode */
  71. #define DW_PSSI_CTRLR0_SLV_OE BIT(10)
  72. #define DW_PSSI_CTRLR0_SRL BIT(11)
  73. #define DW_PSSI_CTRLR0_CFS BIT(12)
  74. /* Bit fields in CTRLR0 (DWC SSI with AHB interface) */
  75. #define DW_HSSI_CTRLR0_DFS_MASK GENMASK(4, 0)
  76. #define DW_HSSI_CTRLR0_FRF_MASK GENMASK(7, 6)
  77. #define DW_HSSI_CTRLR0_SCPHA BIT(8)
  78. #define DW_HSSI_CTRLR0_SCPOL BIT(9)
  79. #define DW_HSSI_CTRLR0_TMOD_MASK GENMASK(11, 10)
  80. #define DW_HSSI_CTRLR0_SRL BIT(13)
  81. #define DW_HSSI_CTRLR0_MST BIT(31)
  82. /* Bit fields in CTRLR1 */
  83. #define DW_SPI_NDF_MASK GENMASK(15, 0)
  84. /* Bit fields in SR, 7 bits */
  85. #define DW_SPI_SR_MASK GENMASK(6, 0)
  86. #define DW_SPI_SR_BUSY BIT(0)
  87. #define DW_SPI_SR_TF_NOT_FULL BIT(1)
  88. #define DW_SPI_SR_TF_EMPT BIT(2)
  89. #define DW_SPI_SR_RF_NOT_EMPT BIT(3)
  90. #define DW_SPI_SR_RF_FULL BIT(4)
  91. #define DW_SPI_SR_TX_ERR BIT(5)
  92. #define DW_SPI_SR_DCOL BIT(6)
  93. /* Bit fields in ISR, IMR, RISR, 7 bits */
  94. #define DW_SPI_INT_MASK GENMASK(5, 0)
  95. #define DW_SPI_INT_TXEI BIT(0)
  96. #define DW_SPI_INT_TXOI BIT(1)
  97. #define DW_SPI_INT_RXUI BIT(2)
  98. #define DW_SPI_INT_RXOI BIT(3)
  99. #define DW_SPI_INT_RXFI BIT(4)
  100. #define DW_SPI_INT_MSTI BIT(5)
  101. /* Bit fields in DMACR */
  102. #define DW_SPI_DMACR_RDMAE BIT(0)
  103. #define DW_SPI_DMACR_TDMAE BIT(1)
  104. /* Mem/DMA operations helpers */
  105. #define DW_SPI_WAIT_RETRIES 5
  106. #define DW_SPI_BUF_SIZE \
  107. (sizeof_field(struct spi_mem_op, cmd.opcode) + \
  108. sizeof_field(struct spi_mem_op, addr.val) + 256)
  109. #define DW_SPI_GET_BYTE(_val, _idx) \
  110. ((_val) >> (BITS_PER_BYTE * (_idx)) & 0xff)
  111. /* Slave spi_transfer/spi_mem_op related */
  112. struct dw_spi_cfg {
  113. u8 tmode;
  114. u8 dfs;
  115. u32 ndf;
  116. u32 freq;
  117. };
  118. struct dw_spi;
  119. struct dw_spi_dma_ops {
  120. int (*dma_init)(struct device *dev, struct dw_spi *dws);
  121. void (*dma_exit)(struct dw_spi *dws);
  122. int (*dma_setup)(struct dw_spi *dws, struct spi_transfer *xfer);
  123. bool (*can_dma)(struct spi_controller *master, struct spi_device *spi,
  124. struct spi_transfer *xfer);
  125. int (*dma_transfer)(struct dw_spi *dws, struct spi_transfer *xfer);
  126. void (*dma_stop)(struct dw_spi *dws);
  127. };
  128. struct dw_spi {
  129. struct spi_controller *master;
  130. u32 ip; /* Synopsys DW SSI IP-core ID */
  131. u32 ver; /* Synopsys component version */
  132. u32 caps; /* DW SPI capabilities */
  133. void __iomem *regs;
  134. unsigned long paddr;
  135. int irq;
  136. u32 fifo_len; /* depth of the FIFO buffer */
  137. unsigned int dfs_offset; /* CTRLR0 DFS field offset */
  138. u32 max_mem_freq; /* max mem-ops bus freq */
  139. u32 max_freq; /* max bus freq supported */
  140. u32 reg_io_width; /* DR I/O width in bytes */
  141. u16 bus_num;
  142. u16 num_cs; /* supported slave numbers */
  143. void (*set_cs)(struct spi_device *spi, bool enable);
  144. /* Current message transfer state info */
  145. void *tx;
  146. unsigned int tx_len;
  147. void *rx;
  148. unsigned int rx_len;
  149. u8 buf[DW_SPI_BUF_SIZE];
  150. int dma_mapped;
  151. u8 n_bytes; /* current is a 1/2 bytes op */
  152. irqreturn_t (*transfer_handler)(struct dw_spi *dws);
  153. u32 current_freq; /* frequency in hz */
  154. u32 cur_rx_sample_dly;
  155. u32 def_rx_sample_dly_ns;
  156. /* Custom memory operations */
  157. struct spi_controller_mem_ops mem_ops;
  158. /* DMA info */
  159. struct dma_chan *txchan;
  160. u32 txburst;
  161. struct dma_chan *rxchan;
  162. u32 rxburst;
  163. u32 dma_sg_burst;
  164. unsigned long dma_chan_busy;
  165. dma_addr_t dma_addr; /* phy address of the Data register */
  166. const struct dw_spi_dma_ops *dma_ops;
  167. struct completion dma_completion;
  168. #ifdef CONFIG_DEBUG_FS
  169. struct dentry *debugfs;
  170. struct debugfs_regset32 regset;
  171. #endif
  172. };
  173. static inline u32 dw_readl(struct dw_spi *dws, u32 offset)
  174. {
  175. return __raw_readl(dws->regs + offset);
  176. }
  177. static inline void dw_writel(struct dw_spi *dws, u32 offset, u32 val)
  178. {
  179. __raw_writel(val, dws->regs + offset);
  180. }
  181. static inline u32 dw_read_io_reg(struct dw_spi *dws, u32 offset)
  182. {
  183. switch (dws->reg_io_width) {
  184. case 2:
  185. return readw_relaxed(dws->regs + offset);
  186. case 4:
  187. default:
  188. return readl_relaxed(dws->regs + offset);
  189. }
  190. }
  191. static inline void dw_write_io_reg(struct dw_spi *dws, u32 offset, u32 val)
  192. {
  193. switch (dws->reg_io_width) {
  194. case 2:
  195. writew_relaxed(val, dws->regs + offset);
  196. break;
  197. case 4:
  198. default:
  199. writel_relaxed(val, dws->regs + offset);
  200. break;
  201. }
  202. }
  203. static inline void dw_spi_enable_chip(struct dw_spi *dws, int enable)
  204. {
  205. dw_writel(dws, DW_SPI_SSIENR, (enable ? 1 : 0));
  206. }
  207. static inline void dw_spi_set_clk(struct dw_spi *dws, u16 div)
  208. {
  209. dw_writel(dws, DW_SPI_BAUDR, div);
  210. }
  211. /* Disable IRQ bits */
  212. static inline void dw_spi_mask_intr(struct dw_spi *dws, u32 mask)
  213. {
  214. u32 new_mask;
  215. new_mask = dw_readl(dws, DW_SPI_IMR) & ~mask;
  216. dw_writel(dws, DW_SPI_IMR, new_mask);
  217. }
  218. /* Enable IRQ bits */
  219. static inline void dw_spi_umask_intr(struct dw_spi *dws, u32 mask)
  220. {
  221. u32 new_mask;
  222. new_mask = dw_readl(dws, DW_SPI_IMR) | mask;
  223. dw_writel(dws, DW_SPI_IMR, new_mask);
  224. }
  225. /*
  226. * This disables the SPI controller, interrupts, clears the interrupts status
  227. * and CS, then re-enables the controller back. Transmit and receive FIFO
  228. * buffers are cleared when the device is disabled.
  229. */
  230. static inline void dw_spi_reset_chip(struct dw_spi *dws)
  231. {
  232. dw_spi_enable_chip(dws, 0);
  233. dw_spi_mask_intr(dws, 0xff);
  234. dw_readl(dws, DW_SPI_ICR);
  235. dw_writel(dws, DW_SPI_SER, 0);
  236. dw_spi_enable_chip(dws, 1);
  237. }
  238. static inline void dw_spi_shutdown_chip(struct dw_spi *dws)
  239. {
  240. dw_spi_enable_chip(dws, 0);
  241. dw_spi_set_clk(dws, 0);
  242. }
  243. extern void dw_spi_set_cs(struct spi_device *spi, bool enable);
  244. extern void dw_spi_update_config(struct dw_spi *dws, struct spi_device *spi,
  245. struct dw_spi_cfg *cfg);
  246. extern int dw_spi_check_status(struct dw_spi *dws, bool raw);
  247. extern int dw_spi_add_host(struct device *dev, struct dw_spi *dws);
  248. extern void dw_spi_remove_host(struct dw_spi *dws);
  249. extern int dw_spi_suspend_host(struct dw_spi *dws);
  250. extern int dw_spi_resume_host(struct dw_spi *dws);
  251. #ifdef CONFIG_SPI_DW_DMA
  252. extern void dw_spi_dma_setup_mfld(struct dw_spi *dws);
  253. extern void dw_spi_dma_setup_generic(struct dw_spi *dws);
  254. #else
  255. static inline void dw_spi_dma_setup_mfld(struct dw_spi *dws) {}
  256. static inline void dw_spi_dma_setup_generic(struct dw_spi *dws) {}
  257. #endif /* !CONFIG_SPI_DW_DMA */
  258. #endif /* __SPI_DW_H__ */