| .. | .. |
|---|
| 2 | 2 | #ifndef DW_SPI_HEADER_H |
|---|
| 3 | 3 | #define DW_SPI_HEADER_H |
|---|
| 4 | 4 | |
|---|
| 5 | +#include <linux/bits.h> |
|---|
| 6 | +#include <linux/completion.h> |
|---|
| 7 | +#include <linux/debugfs.h> |
|---|
| 8 | +#include <linux/irqreturn.h> |
|---|
| 5 | 9 | #include <linux/io.h> |
|---|
| 6 | 10 | #include <linux/scatterlist.h> |
|---|
| 7 | | -#include <linux/gpio.h> |
|---|
| 11 | +#include <linux/spi/spi-mem.h> |
|---|
| 8 | 12 | |
|---|
| 9 | 13 | /* Register offsets */ |
|---|
| 10 | | -#define DW_SPI_CTRL0 0x00 |
|---|
| 11 | | -#define DW_SPI_CTRL1 0x04 |
|---|
| 14 | +#define DW_SPI_CTRLR0 0x00 |
|---|
| 15 | +#define DW_SPI_CTRLR1 0x04 |
|---|
| 12 | 16 | #define DW_SPI_SSIENR 0x08 |
|---|
| 13 | 17 | #define DW_SPI_MWCR 0x0c |
|---|
| 14 | 18 | #define DW_SPI_SER 0x10 |
|---|
| 15 | 19 | #define DW_SPI_BAUDR 0x14 |
|---|
| 16 | | -#define DW_SPI_TXFLTR 0x18 |
|---|
| 17 | | -#define DW_SPI_RXFLTR 0x1c |
|---|
| 20 | +#define DW_SPI_TXFTLR 0x18 |
|---|
| 21 | +#define DW_SPI_RXFTLR 0x1c |
|---|
| 18 | 22 | #define DW_SPI_TXFLR 0x20 |
|---|
| 19 | 23 | #define DW_SPI_RXFLR 0x24 |
|---|
| 20 | 24 | #define DW_SPI_SR 0x28 |
|---|
| .. | .. |
|---|
| 32 | 36 | #define DW_SPI_IDR 0x58 |
|---|
| 33 | 37 | #define DW_SPI_VERSION 0x5c |
|---|
| 34 | 38 | #define DW_SPI_DR 0x60 |
|---|
| 39 | +#define DW_SPI_RX_SAMPLE_DLY 0xf0 |
|---|
| 40 | +#define DW_SPI_CS_OVERRIDE 0xf4 |
|---|
| 35 | 41 | |
|---|
| 36 | 42 | /* Bit fields in CTRLR0 */ |
|---|
| 37 | 43 | #define SPI_DFS_OFFSET 0 |
|---|
| .. | .. |
|---|
| 57 | 63 | #define SPI_SRL_OFFSET 11 |
|---|
| 58 | 64 | #define SPI_CFS_OFFSET 12 |
|---|
| 59 | 65 | |
|---|
| 66 | +/* Bit fields in CTRLR0 based on DWC_ssi_databook.pdf v1.01a */ |
|---|
| 67 | +#define DWC_SSI_CTRLR0_SRL_OFFSET 13 |
|---|
| 68 | +#define DWC_SSI_CTRLR0_TMOD_OFFSET 10 |
|---|
| 69 | +#define DWC_SSI_CTRLR0_TMOD_MASK GENMASK(11, 10) |
|---|
| 70 | +#define DWC_SSI_CTRLR0_SCPOL_OFFSET 9 |
|---|
| 71 | +#define DWC_SSI_CTRLR0_SCPH_OFFSET 8 |
|---|
| 72 | +#define DWC_SSI_CTRLR0_FRF_OFFSET 6 |
|---|
| 73 | +#define DWC_SSI_CTRLR0_DFS_OFFSET 0 |
|---|
| 74 | + |
|---|
| 75 | +/* |
|---|
| 76 | + * For Keem Bay, CTRLR0[31] is used to select controller mode. |
|---|
| 77 | + * 0: SSI is slave |
|---|
| 78 | + * 1: SSI is master |
|---|
| 79 | + */ |
|---|
| 80 | +#define DWC_SSI_CTRLR0_KEEMBAY_MST BIT(31) |
|---|
| 81 | + |
|---|
| 82 | +/* Bit fields in CTRLR1 */ |
|---|
| 83 | +#define SPI_NDF_MASK GENMASK(15, 0) |
|---|
| 84 | + |
|---|
| 60 | 85 | /* Bit fields in SR, 7 bits */ |
|---|
| 61 | 86 | #define SR_MASK 0x7f /* cover 7 bits */ |
|---|
| 62 | 87 | #define SR_BUSY (1 << 0) |
|---|
| .. | .. |
|---|
| 79 | 104 | #define SPI_DMA_RDMAE (1 << 0) |
|---|
| 80 | 105 | #define SPI_DMA_TDMAE (1 << 1) |
|---|
| 81 | 106 | |
|---|
| 82 | | -/* TX RX interrupt level threshold, max can be 256 */ |
|---|
| 83 | | -#define SPI_INT_THRESHOLD 32 |
|---|
| 107 | +#define SPI_WAIT_RETRIES 5 |
|---|
| 108 | +#define SPI_BUF_SIZE \ |
|---|
| 109 | + (sizeof_field(struct spi_mem_op, cmd.opcode) + \ |
|---|
| 110 | + sizeof_field(struct spi_mem_op, addr.val) + 256) |
|---|
| 111 | +#define SPI_GET_BYTE(_val, _idx) \ |
|---|
| 112 | + ((_val) >> (BITS_PER_BYTE * (_idx)) & 0xff) |
|---|
| 84 | 113 | |
|---|
| 85 | 114 | enum dw_ssi_type { |
|---|
| 86 | 115 | SSI_MOTO_SPI = 0, |
|---|
| .. | .. |
|---|
| 88 | 117 | SSI_NS_MICROWIRE, |
|---|
| 89 | 118 | }; |
|---|
| 90 | 119 | |
|---|
| 120 | +/* DW SPI capabilities */ |
|---|
| 121 | +#define DW_SPI_CAP_CS_OVERRIDE BIT(0) |
|---|
| 122 | +#define DW_SPI_CAP_KEEMBAY_MST BIT(1) |
|---|
| 123 | +#define DW_SPI_CAP_DWC_SSI BIT(2) |
|---|
| 124 | + |
|---|
| 125 | +/* Slave spi_transfer/spi_mem_op related */ |
|---|
| 126 | +struct dw_spi_cfg { |
|---|
| 127 | + u8 tmode; |
|---|
| 128 | + u8 dfs; |
|---|
| 129 | + u32 ndf; |
|---|
| 130 | + u32 freq; |
|---|
| 131 | +}; |
|---|
| 132 | + |
|---|
| 91 | 133 | struct dw_spi; |
|---|
| 92 | 134 | struct dw_spi_dma_ops { |
|---|
| 93 | | - int (*dma_init)(struct dw_spi *dws); |
|---|
| 135 | + int (*dma_init)(struct device *dev, struct dw_spi *dws); |
|---|
| 94 | 136 | void (*dma_exit)(struct dw_spi *dws); |
|---|
| 95 | 137 | int (*dma_setup)(struct dw_spi *dws, struct spi_transfer *xfer); |
|---|
| 96 | 138 | bool (*can_dma)(struct spi_controller *master, struct spi_device *spi, |
|---|
| .. | .. |
|---|
| 101 | 143 | |
|---|
| 102 | 144 | struct dw_spi { |
|---|
| 103 | 145 | struct spi_controller *master; |
|---|
| 104 | | - enum dw_ssi_type type; |
|---|
| 105 | 146 | |
|---|
| 106 | 147 | void __iomem *regs; |
|---|
| 107 | 148 | unsigned long paddr; |
|---|
| 108 | 149 | int irq; |
|---|
| 109 | 150 | u32 fifo_len; /* depth of the FIFO buffer */ |
|---|
| 151 | + u32 max_mem_freq; /* max mem-ops bus freq */ |
|---|
| 110 | 152 | u32 max_freq; /* max bus freq supported */ |
|---|
| 153 | + |
|---|
| 154 | + u32 caps; /* DW SPI capabilities */ |
|---|
| 111 | 155 | |
|---|
| 112 | 156 | u32 reg_io_width; /* DR I/O width in bytes */ |
|---|
| 113 | 157 | u16 bus_num; |
|---|
| .. | .. |
|---|
| 115 | 159 | void (*set_cs)(struct spi_device *spi, bool enable); |
|---|
| 116 | 160 | |
|---|
| 117 | 161 | /* Current message transfer state info */ |
|---|
| 118 | | - size_t len; |
|---|
| 119 | 162 | void *tx; |
|---|
| 120 | | - void *tx_end; |
|---|
| 121 | | - spinlock_t buf_lock; |
|---|
| 163 | + unsigned int tx_len; |
|---|
| 122 | 164 | void *rx; |
|---|
| 123 | | - void *rx_end; |
|---|
| 165 | + unsigned int rx_len; |
|---|
| 166 | + u8 buf[SPI_BUF_SIZE]; |
|---|
| 124 | 167 | int dma_mapped; |
|---|
| 125 | 168 | u8 n_bytes; /* current is a 1/2 bytes op */ |
|---|
| 126 | | - u32 dma_width; |
|---|
| 127 | 169 | irqreturn_t (*transfer_handler)(struct dw_spi *dws); |
|---|
| 128 | 170 | u32 current_freq; /* frequency in hz */ |
|---|
| 171 | + u32 cur_rx_sample_dly; |
|---|
| 172 | + u32 def_rx_sample_dly_ns; |
|---|
| 173 | + |
|---|
| 174 | + /* Custom memory operations */ |
|---|
| 175 | + struct spi_controller_mem_ops mem_ops; |
|---|
| 129 | 176 | |
|---|
| 130 | 177 | /* DMA info */ |
|---|
| 131 | | - int dma_inited; |
|---|
| 132 | 178 | struct dma_chan *txchan; |
|---|
| 179 | + u32 txburst; |
|---|
| 133 | 180 | struct dma_chan *rxchan; |
|---|
| 181 | + u32 rxburst; |
|---|
| 182 | + u32 dma_sg_burst; |
|---|
| 134 | 183 | unsigned long dma_chan_busy; |
|---|
| 135 | 184 | dma_addr_t dma_addr; /* phy address of the Data register */ |
|---|
| 136 | 185 | const struct dw_spi_dma_ops *dma_ops; |
|---|
| 137 | | - void *dma_tx; |
|---|
| 138 | | - void *dma_rx; |
|---|
| 186 | + struct completion dma_completion; |
|---|
| 139 | 187 | |
|---|
| 140 | | - /* Bus interface info */ |
|---|
| 141 | | - void *priv; |
|---|
| 142 | 188 | #ifdef CONFIG_DEBUG_FS |
|---|
| 143 | 189 | struct dentry *debugfs; |
|---|
| 190 | + struct debugfs_regset32 regset; |
|---|
| 144 | 191 | #endif |
|---|
| 145 | 192 | }; |
|---|
| 146 | 193 | |
|---|
| .. | .. |
|---|
| 149 | 196 | return __raw_readl(dws->regs + offset); |
|---|
| 150 | 197 | } |
|---|
| 151 | 198 | |
|---|
| 152 | | -static inline u16 dw_readw(struct dw_spi *dws, u32 offset) |
|---|
| 153 | | -{ |
|---|
| 154 | | - return __raw_readw(dws->regs + offset); |
|---|
| 155 | | -} |
|---|
| 156 | | - |
|---|
| 157 | 199 | static inline void dw_writel(struct dw_spi *dws, u32 offset, u32 val) |
|---|
| 158 | 200 | { |
|---|
| 159 | 201 | __raw_writel(val, dws->regs + offset); |
|---|
| 160 | | -} |
|---|
| 161 | | - |
|---|
| 162 | | -static inline void dw_writew(struct dw_spi *dws, u32 offset, u16 val) |
|---|
| 163 | | -{ |
|---|
| 164 | | - __raw_writew(val, dws->regs + offset); |
|---|
| 165 | 202 | } |
|---|
| 166 | 203 | |
|---|
| 167 | 204 | static inline u32 dw_read_io_reg(struct dw_spi *dws, u32 offset) |
|---|
| 168 | 205 | { |
|---|
| 169 | 206 | switch (dws->reg_io_width) { |
|---|
| 170 | 207 | case 2: |
|---|
| 171 | | - return dw_readw(dws, offset); |
|---|
| 208 | + return readw_relaxed(dws->regs + offset); |
|---|
| 172 | 209 | case 4: |
|---|
| 173 | 210 | default: |
|---|
| 174 | | - return dw_readl(dws, offset); |
|---|
| 211 | + return readl_relaxed(dws->regs + offset); |
|---|
| 175 | 212 | } |
|---|
| 176 | 213 | } |
|---|
| 177 | 214 | |
|---|
| .. | .. |
|---|
| 179 | 216 | { |
|---|
| 180 | 217 | switch (dws->reg_io_width) { |
|---|
| 181 | 218 | case 2: |
|---|
| 182 | | - dw_writew(dws, offset, val); |
|---|
| 219 | + writew_relaxed(val, dws->regs + offset); |
|---|
| 183 | 220 | break; |
|---|
| 184 | 221 | case 4: |
|---|
| 185 | 222 | default: |
|---|
| 186 | | - dw_writel(dws, offset, val); |
|---|
| 223 | + writel_relaxed(val, dws->regs + offset); |
|---|
| 187 | 224 | break; |
|---|
| 188 | 225 | } |
|---|
| 189 | 226 | } |
|---|
| .. | .. |
|---|
| 217 | 254 | } |
|---|
| 218 | 255 | |
|---|
| 219 | 256 | /* |
|---|
| 220 | | - * This does disable the SPI controller, interrupts, and re-enable the |
|---|
| 221 | | - * controller back. Transmit and receive FIFO buffers are cleared when the |
|---|
| 222 | | - * device is disabled. |
|---|
| 257 | + * This disables the SPI controller, interrupts, clears the interrupts status |
|---|
| 258 | + * and CS, then re-enables the controller back. Transmit and receive FIFO |
|---|
| 259 | + * buffers are cleared when the device is disabled. |
|---|
| 223 | 260 | */ |
|---|
| 224 | 261 | static inline void spi_reset_chip(struct dw_spi *dws) |
|---|
| 225 | 262 | { |
|---|
| 226 | 263 | spi_enable_chip(dws, 0); |
|---|
| 227 | 264 | spi_mask_intr(dws, 0xff); |
|---|
| 265 | + dw_readl(dws, DW_SPI_ICR); |
|---|
| 266 | + dw_writel(dws, DW_SPI_SER, 0); |
|---|
| 228 | 267 | spi_enable_chip(dws, 1); |
|---|
| 229 | 268 | } |
|---|
| 230 | 269 | |
|---|
| .. | .. |
|---|
| 234 | 273 | spi_set_clk(dws, 0); |
|---|
| 235 | 274 | } |
|---|
| 236 | 275 | |
|---|
| 237 | | -/* |
|---|
| 238 | | - * Each SPI slave device to work with dw_api controller should |
|---|
| 239 | | - * has such a structure claiming its working mode (poll or PIO/DMA), |
|---|
| 240 | | - * which can be save in the "controller_data" member of the |
|---|
| 241 | | - * struct spi_device. |
|---|
| 242 | | - */ |
|---|
| 243 | | -struct dw_spi_chip { |
|---|
| 244 | | - u8 poll_mode; /* 1 for controller polling mode */ |
|---|
| 245 | | - u8 type; /* SPI/SSP/MicroWire */ |
|---|
| 246 | | - void (*cs_control)(u32 command); |
|---|
| 247 | | -}; |
|---|
| 248 | | - |
|---|
| 249 | 276 | extern void dw_spi_set_cs(struct spi_device *spi, bool enable); |
|---|
| 277 | +extern void dw_spi_update_config(struct dw_spi *dws, struct spi_device *spi, |
|---|
| 278 | + struct dw_spi_cfg *cfg); |
|---|
| 279 | +extern int dw_spi_check_status(struct dw_spi *dws, bool raw); |
|---|
| 250 | 280 | extern int dw_spi_add_host(struct device *dev, struct dw_spi *dws); |
|---|
| 251 | 281 | extern void dw_spi_remove_host(struct dw_spi *dws); |
|---|
| 252 | 282 | extern int dw_spi_suspend_host(struct dw_spi *dws); |
|---|
| 253 | 283 | extern int dw_spi_resume_host(struct dw_spi *dws); |
|---|
| 254 | 284 | |
|---|
| 255 | | -/* platform related setup */ |
|---|
| 256 | | -extern int dw_spi_mid_init(struct dw_spi *dws); /* Intel MID platforms */ |
|---|
| 285 | +#ifdef CONFIG_SPI_DW_DMA |
|---|
| 286 | + |
|---|
| 287 | +extern void dw_spi_dma_setup_mfld(struct dw_spi *dws); |
|---|
| 288 | +extern void dw_spi_dma_setup_generic(struct dw_spi *dws); |
|---|
| 289 | + |
|---|
| 290 | +#else |
|---|
| 291 | + |
|---|
| 292 | +static inline void dw_spi_dma_setup_mfld(struct dw_spi *dws) {} |
|---|
| 293 | +static inline void dw_spi_dma_setup_generic(struct dw_spi *dws) {} |
|---|
| 294 | + |
|---|
| 295 | +#endif /* !CONFIG_SPI_DW_DMA */ |
|---|
| 296 | + |
|---|
| 257 | 297 | #endif /* DW_SPI_HEADER_H */ |
|---|