spi-dw.h 6.1 KB

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