dw_spi.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #ifndef DW_SPI_HEADER_H
  2. #define DW_SPI_HEADER_H
  3. #include <linux/io.h>
  4. #include <linux/scatterlist.h>
  5. /* Bit fields in CTRLR0 */
  6. #define SPI_DFS_OFFSET 0
  7. #define SPI_FRF_OFFSET 4
  8. #define SPI_FRF_SPI 0x0
  9. #define SPI_FRF_SSP 0x1
  10. #define SPI_FRF_MICROWIRE 0x2
  11. #define SPI_FRF_RESV 0x3
  12. #define SPI_MODE_OFFSET 6
  13. #define SPI_SCPH_OFFSET 6
  14. #define SPI_SCOL_OFFSET 7
  15. #define SPI_TMOD_OFFSET 8
  16. #define SPI_TMOD_MASK (0x3 << SPI_TMOD_OFFSET)
  17. #define SPI_TMOD_TR 0x0 /* xmit & recv */
  18. #define SPI_TMOD_TO 0x1 /* xmit only */
  19. #define SPI_TMOD_RO 0x2 /* recv only */
  20. #define SPI_TMOD_EPROMREAD 0x3 /* eeprom read mode */
  21. #define SPI_SLVOE_OFFSET 10
  22. #define SPI_SRL_OFFSET 11
  23. #define SPI_CFS_OFFSET 12
  24. /* Bit fields in SR, 7 bits */
  25. #define SR_MASK 0x7f /* cover 7 bits */
  26. #define SR_BUSY (1 << 0)
  27. #define SR_TF_NOT_FULL (1 << 1)
  28. #define SR_TF_EMPT (1 << 2)
  29. #define SR_RF_NOT_EMPT (1 << 3)
  30. #define SR_RF_FULL (1 << 4)
  31. #define SR_TX_ERR (1 << 5)
  32. #define SR_DCOL (1 << 6)
  33. /* Bit fields in ISR, IMR, RISR, 7 bits */
  34. #define SPI_INT_TXEI (1 << 0)
  35. #define SPI_INT_TXOI (1 << 1)
  36. #define SPI_INT_RXUI (1 << 2)
  37. #define SPI_INT_RXOI (1 << 3)
  38. #define SPI_INT_RXFI (1 << 4)
  39. #define SPI_INT_MSTI (1 << 5)
  40. /* TX RX interrupt level threshold, max can be 256 */
  41. #define SPI_INT_THRESHOLD 32
  42. enum dw_ssi_type {
  43. SSI_MOTO_SPI = 0,
  44. SSI_TI_SSP,
  45. SSI_NS_MICROWIRE,
  46. };
  47. struct dw_spi_reg {
  48. u32 ctrl0;
  49. u32 ctrl1;
  50. u32 ssienr;
  51. u32 mwcr;
  52. u32 ser;
  53. u32 baudr;
  54. u32 txfltr;
  55. u32 rxfltr;
  56. u32 txflr;
  57. u32 rxflr;
  58. u32 sr;
  59. u32 imr;
  60. u32 isr;
  61. u32 risr;
  62. u32 txoicr;
  63. u32 rxoicr;
  64. u32 rxuicr;
  65. u32 msticr;
  66. u32 icr;
  67. u32 dmacr;
  68. u32 dmatdlr;
  69. u32 dmardlr;
  70. u32 idr;
  71. u32 version;
  72. u32 dr; /* Currently oper as 32 bits,
  73. though only low 16 bits matters */
  74. } __packed;
  75. struct dw_spi;
  76. struct dw_spi_dma_ops {
  77. int (*dma_init)(struct dw_spi *dws);
  78. void (*dma_exit)(struct dw_spi *dws);
  79. int (*dma_transfer)(struct dw_spi *dws, int cs_change);
  80. };
  81. struct dw_spi {
  82. struct spi_master *master;
  83. struct spi_device *cur_dev;
  84. struct device *parent_dev;
  85. enum dw_ssi_type type;
  86. void __iomem *regs;
  87. unsigned long paddr;
  88. u32 iolen;
  89. int irq;
  90. u32 fifo_len; /* depth of the FIFO buffer */
  91. u32 max_freq; /* max bus freq supported */
  92. u16 bus_num;
  93. u16 num_cs; /* supported slave numbers */
  94. /* Driver message queue */
  95. struct workqueue_struct *workqueue;
  96. struct work_struct pump_messages;
  97. spinlock_t lock;
  98. struct list_head queue;
  99. int busy;
  100. int run;
  101. /* Message Transfer pump */
  102. struct tasklet_struct pump_transfers;
  103. /* Current message transfer state info */
  104. struct spi_message *cur_msg;
  105. struct spi_transfer *cur_transfer;
  106. struct chip_data *cur_chip;
  107. struct chip_data *prev_chip;
  108. size_t len;
  109. void *tx;
  110. void *tx_end;
  111. void *rx;
  112. void *rx_end;
  113. int dma_mapped;
  114. dma_addr_t rx_dma;
  115. dma_addr_t tx_dma;
  116. size_t rx_map_len;
  117. size_t tx_map_len;
  118. u8 n_bytes; /* current is a 1/2 bytes op */
  119. u8 max_bits_per_word; /* maxim is 16b */
  120. u32 dma_width;
  121. int cs_change;
  122. irqreturn_t (*transfer_handler)(struct dw_spi *dws);
  123. void (*cs_control)(u32 command);
  124. /* Dma info */
  125. int dma_inited;
  126. struct dma_chan *txchan;
  127. struct scatterlist tx_sgl;
  128. struct dma_chan *rxchan;
  129. struct scatterlist rx_sgl;
  130. int dma_chan_done;
  131. struct device *dma_dev;
  132. dma_addr_t dma_addr; /* phy address of the Data register */
  133. struct dw_spi_dma_ops *dma_ops;
  134. void *dma_priv; /* platform relate info */
  135. struct pci_dev *dmac;
  136. /* Bus interface info */
  137. void *priv;
  138. #ifdef CONFIG_DEBUG_FS
  139. struct dentry *debugfs;
  140. #endif
  141. };
  142. #define dw_readl(dw, name) \
  143. __raw_readl(&(((struct dw_spi_reg *)dw->regs)->name))
  144. #define dw_writel(dw, name, val) \
  145. __raw_writel((val), &(((struct dw_spi_reg *)dw->regs)->name))
  146. #define dw_readw(dw, name) \
  147. __raw_readw(&(((struct dw_spi_reg *)dw->regs)->name))
  148. #define dw_writew(dw, name, val) \
  149. __raw_writew((val), &(((struct dw_spi_reg *)dw->regs)->name))
  150. static inline void spi_enable_chip(struct dw_spi *dws, int enable)
  151. {
  152. dw_writel(dws, ssienr, (enable ? 1 : 0));
  153. }
  154. static inline void spi_set_clk(struct dw_spi *dws, u16 div)
  155. {
  156. dw_writel(dws, baudr, div);
  157. }
  158. static inline void spi_chip_sel(struct dw_spi *dws, u16 cs)
  159. {
  160. if (cs > dws->num_cs)
  161. return;
  162. if (dws->cs_control)
  163. dws->cs_control(1);
  164. dw_writel(dws, ser, 1 << cs);
  165. }
  166. /* Disable IRQ bits */
  167. static inline void spi_mask_intr(struct dw_spi *dws, u32 mask)
  168. {
  169. u32 new_mask;
  170. new_mask = dw_readl(dws, imr) & ~mask;
  171. dw_writel(dws, imr, new_mask);
  172. }
  173. /* Enable IRQ bits */
  174. static inline void spi_umask_intr(struct dw_spi *dws, u32 mask)
  175. {
  176. u32 new_mask;
  177. new_mask = dw_readl(dws, imr) | mask;
  178. dw_writel(dws, imr, new_mask);
  179. }
  180. /*
  181. * Each SPI slave device to work with dw_api controller should
  182. * has such a structure claiming its working mode (PIO/DMA etc),
  183. * which can be save in the "controller_data" member of the
  184. * struct spi_device
  185. */
  186. struct dw_spi_chip {
  187. u8 poll_mode; /* 0 for contoller polling mode */
  188. u8 type; /* SPI/SSP/Micrwire */
  189. u8 enable_dma;
  190. void (*cs_control)(u32 command);
  191. };
  192. extern int dw_spi_add_host(struct dw_spi *dws);
  193. extern void dw_spi_remove_host(struct dw_spi *dws);
  194. extern int dw_spi_suspend_host(struct dw_spi *dws);
  195. extern int dw_spi_resume_host(struct dw_spi *dws);
  196. extern void dw_spi_xfer_done(struct dw_spi *dws);
  197. /* platform related setup */
  198. extern int dw_spi_mid_init(struct dw_spi *dws); /* Intel MID platforms */
  199. #endif /* DW_SPI_HEADER_H */