spi-dw.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #ifndef DW_SPI_HEADER_H
  2. #define DW_SPI_HEADER_H
  3. #include <linux/io.h>
  4. #include <linux/scatterlist.h>
  5. /* Register offsets */
  6. #define DW_SPI_CTRL0 0x00
  7. #define DW_SPI_CTRL1 0x04
  8. #define DW_SPI_SSIENR 0x08
  9. #define DW_SPI_MWCR 0x0c
  10. #define DW_SPI_SER 0x10
  11. #define DW_SPI_BAUDR 0x14
  12. #define DW_SPI_TXFLTR 0x18
  13. #define DW_SPI_RXFLTR 0x1c
  14. #define DW_SPI_TXFLR 0x20
  15. #define DW_SPI_RXFLR 0x24
  16. #define DW_SPI_SR 0x28
  17. #define DW_SPI_IMR 0x2c
  18. #define DW_SPI_ISR 0x30
  19. #define DW_SPI_RISR 0x34
  20. #define DW_SPI_TXOICR 0x38
  21. #define DW_SPI_RXOICR 0x3c
  22. #define DW_SPI_RXUICR 0x40
  23. #define DW_SPI_MSTICR 0x44
  24. #define DW_SPI_ICR 0x48
  25. #define DW_SPI_DMACR 0x4c
  26. #define DW_SPI_DMATDLR 0x50
  27. #define DW_SPI_DMARDLR 0x54
  28. #define DW_SPI_IDR 0x58
  29. #define DW_SPI_VERSION 0x5c
  30. #define DW_SPI_DR 0x60
  31. /* Bit fields in CTRLR0 */
  32. #define SPI_DFS_OFFSET 0
  33. #define SPI_FRF_OFFSET 4
  34. #define SPI_FRF_SPI 0x0
  35. #define SPI_FRF_SSP 0x1
  36. #define SPI_FRF_MICROWIRE 0x2
  37. #define SPI_FRF_RESV 0x3
  38. #define SPI_MODE_OFFSET 6
  39. #define SPI_SCPH_OFFSET 6
  40. #define SPI_SCOL_OFFSET 7
  41. #define SPI_TMOD_OFFSET 8
  42. #define SPI_TMOD_MASK (0x3 << SPI_TMOD_OFFSET)
  43. #define SPI_TMOD_TR 0x0 /* xmit & recv */
  44. #define SPI_TMOD_TO 0x1 /* xmit only */
  45. #define SPI_TMOD_RO 0x2 /* recv only */
  46. #define SPI_TMOD_EPROMREAD 0x3 /* eeprom read mode */
  47. #define SPI_SLVOE_OFFSET 10
  48. #define SPI_SRL_OFFSET 11
  49. #define SPI_CFS_OFFSET 12
  50. /* Bit fields in SR, 7 bits */
  51. #define SR_MASK 0x7f /* cover 7 bits */
  52. #define SR_BUSY (1 << 0)
  53. #define SR_TF_NOT_FULL (1 << 1)
  54. #define SR_TF_EMPT (1 << 2)
  55. #define SR_RF_NOT_EMPT (1 << 3)
  56. #define SR_RF_FULL (1 << 4)
  57. #define SR_TX_ERR (1 << 5)
  58. #define SR_DCOL (1 << 6)
  59. /* Bit fields in ISR, IMR, RISR, 7 bits */
  60. #define SPI_INT_TXEI (1 << 0)
  61. #define SPI_INT_TXOI (1 << 1)
  62. #define SPI_INT_RXUI (1 << 2)
  63. #define SPI_INT_RXOI (1 << 3)
  64. #define SPI_INT_RXFI (1 << 4)
  65. #define SPI_INT_MSTI (1 << 5)
  66. /* TX RX interrupt level threshold, max can be 256 */
  67. #define SPI_INT_THRESHOLD 32
  68. enum dw_ssi_type {
  69. SSI_MOTO_SPI = 0,
  70. SSI_TI_SSP,
  71. SSI_NS_MICROWIRE,
  72. };
  73. struct dw_spi;
  74. struct dw_spi_dma_ops {
  75. int (*dma_init)(struct dw_spi *dws);
  76. void (*dma_exit)(struct dw_spi *dws);
  77. int (*dma_transfer)(struct dw_spi *dws, int cs_change);
  78. };
  79. struct dw_spi {
  80. struct spi_master *master;
  81. struct spi_device *cur_dev;
  82. struct device *parent_dev;
  83. enum dw_ssi_type type;
  84. char name[16];
  85. void __iomem *regs;
  86. unsigned long paddr;
  87. u32 iolen;
  88. int irq;
  89. u32 fifo_len; /* depth of the FIFO buffer */
  90. u32 max_freq; /* max bus freq supported */
  91. u16 bus_num;
  92. u16 num_cs; /* supported slave numbers */
  93. /* Driver message queue */
  94. struct workqueue_struct *workqueue;
  95. struct work_struct pump_messages;
  96. spinlock_t lock;
  97. struct list_head queue;
  98. int busy;
  99. int run;
  100. /* Message Transfer pump */
  101. struct tasklet_struct pump_transfers;
  102. /* Current message transfer state info */
  103. struct spi_message *cur_msg;
  104. struct spi_transfer *cur_transfer;
  105. struct chip_data *cur_chip;
  106. struct chip_data *prev_chip;
  107. size_t len;
  108. void *tx;
  109. void *tx_end;
  110. void *rx;
  111. void *rx_end;
  112. int dma_mapped;
  113. dma_addr_t rx_dma;
  114. dma_addr_t tx_dma;
  115. size_t rx_map_len;
  116. size_t tx_map_len;
  117. u8 n_bytes; /* current is a 1/2 bytes op */
  118. u8 max_bits_per_word; /* maxim is 16b */
  119. u32 dma_width;
  120. int cs_change;
  121. irqreturn_t (*transfer_handler)(struct dw_spi *dws);
  122. void (*cs_control)(u32 command);
  123. /* Dma info */
  124. int dma_inited;
  125. struct dma_chan *txchan;
  126. struct scatterlist tx_sgl;
  127. struct dma_chan *rxchan;
  128. struct scatterlist rx_sgl;
  129. int dma_chan_done;
  130. struct device *dma_dev;
  131. dma_addr_t dma_addr; /* phy address of the Data register */
  132. struct dw_spi_dma_ops *dma_ops;
  133. void *dma_priv; /* platform relate info */
  134. struct pci_dev *dmac;
  135. /* Bus interface info */
  136. void *priv;
  137. #ifdef CONFIG_DEBUG_FS
  138. struct dentry *debugfs;
  139. #endif
  140. };
  141. static inline u32 dw_readl(struct dw_spi *dws, u32 offset)
  142. {
  143. return __raw_readl(dws->regs + offset);
  144. }
  145. static inline void dw_writel(struct dw_spi *dws, u32 offset, u32 val)
  146. {
  147. __raw_writel(val, dws->regs + offset);
  148. }
  149. static inline u16 dw_readw(struct dw_spi *dws, u32 offset)
  150. {
  151. return __raw_readw(dws->regs + offset);
  152. }
  153. static inline void dw_writew(struct dw_spi *dws, u32 offset, u16 val)
  154. {
  155. __raw_writew(val, dws->regs + offset);
  156. }
  157. static inline void spi_enable_chip(struct dw_spi *dws, int enable)
  158. {
  159. dw_writel(dws, DW_SPI_SSIENR, (enable ? 1 : 0));
  160. }
  161. static inline void spi_set_clk(struct dw_spi *dws, u16 div)
  162. {
  163. dw_writel(dws, DW_SPI_BAUDR, div);
  164. }
  165. static inline void spi_chip_sel(struct dw_spi *dws, u16 cs)
  166. {
  167. if (cs > dws->num_cs)
  168. return;
  169. if (dws->cs_control)
  170. dws->cs_control(1);
  171. dw_writel(dws, DW_SPI_SER, 1 << cs);
  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. * Each SPI slave device to work with dw_api controller should
  189. * has such a structure claiming its working mode (PIO/DMA etc),
  190. * which can be save in the "controller_data" member of the
  191. * struct spi_device
  192. */
  193. struct dw_spi_chip {
  194. u8 poll_mode; /* 0 for contoller polling mode */
  195. u8 type; /* SPI/SSP/Micrwire */
  196. u8 enable_dma;
  197. void (*cs_control)(u32 command);
  198. };
  199. extern int dw_spi_add_host(struct dw_spi *dws);
  200. extern void dw_spi_remove_host(struct dw_spi *dws);
  201. extern int dw_spi_suspend_host(struct dw_spi *dws);
  202. extern int dw_spi_resume_host(struct dw_spi *dws);
  203. extern void dw_spi_xfer_done(struct dw_spi *dws);
  204. /* platform related setup */
  205. extern int dw_spi_mid_init(struct dw_spi *dws); /* Intel MID platforms */
  206. #endif /* DW_SPI_HEADER_H */