blob: 40b2c8c0c2a292d14fb67e8701fa7d80daa5f94e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Feng Tange24c7452009-12-14 14:20:22 -08002#ifndef DW_SPI_HEADER_H
3#define DW_SPI_HEADER_H
Feng Tang7063c0d2010-12-24 13:59:11 +08004
Serge Seminbdbdf0f2020-05-29 16:11:52 +03005#include <linux/completion.h>
Serge Semin83784492020-05-29 16:12:04 +03006#include <linux/debugfs.h>
Andy Shevchenkoe62a15d2020-05-06 18:30:21 +03007#include <linux/irqreturn.h>
Feng Tange24c7452009-12-14 14:20:22 -08008#include <linux/io.h>
Jiri Slaby46165a3d2011-03-18 10:41:17 +01009#include <linux/scatterlist.h>
Feng Tange24c7452009-12-14 14:20:22 -080010
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070011/* Register offsets */
Wan Ahmad Zainie299cb652020-05-05 21:06:12 +080012#define DW_SPI_CTRLR0 0x00
13#define DW_SPI_CTRLR1 0x04
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070014#define DW_SPI_SSIENR 0x08
15#define DW_SPI_MWCR 0x0c
16#define DW_SPI_SER 0x10
17#define DW_SPI_BAUDR 0x14
Wan Ahmad Zainie299cb652020-05-05 21:06:12 +080018#define DW_SPI_TXFTLR 0x18
19#define DW_SPI_RXFTLR 0x1c
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070020#define DW_SPI_TXFLR 0x20
21#define DW_SPI_RXFLR 0x24
22#define DW_SPI_SR 0x28
23#define DW_SPI_IMR 0x2c
24#define DW_SPI_ISR 0x30
25#define DW_SPI_RISR 0x34
26#define DW_SPI_TXOICR 0x38
27#define DW_SPI_RXOICR 0x3c
28#define DW_SPI_RXUICR 0x40
29#define DW_SPI_MSTICR 0x44
30#define DW_SPI_ICR 0x48
31#define DW_SPI_DMACR 0x4c
32#define DW_SPI_DMATDLR 0x50
33#define DW_SPI_DMARDLR 0x54
34#define DW_SPI_IDR 0x58
35#define DW_SPI_VERSION 0x5c
36#define DW_SPI_DR 0x60
Lars Povlsenbac70b52020-08-24 22:30:05 +020037#define DW_SPI_RX_SAMPLE_DLY 0xf0
Talel Shenharf2d70472018-10-11 14:20:07 +030038#define DW_SPI_CS_OVERRIDE 0xf4
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070039
Feng Tange24c7452009-12-14 14:20:22 -080040/* Bit fields in CTRLR0 */
41#define SPI_DFS_OFFSET 0
42
43#define SPI_FRF_OFFSET 4
44#define SPI_FRF_SPI 0x0
45#define SPI_FRF_SSP 0x1
46#define SPI_FRF_MICROWIRE 0x2
47#define SPI_FRF_RESV 0x3
48
49#define SPI_MODE_OFFSET 6
50#define SPI_SCPH_OFFSET 6
51#define SPI_SCOL_OFFSET 7
Feng Tange3e55ff2010-09-07 15:52:06 +080052
Feng Tange24c7452009-12-14 14:20:22 -080053#define SPI_TMOD_OFFSET 8
Feng Tange3e55ff2010-09-07 15:52:06 +080054#define SPI_TMOD_MASK (0x3 << SPI_TMOD_OFFSET)
Feng Tange24c7452009-12-14 14:20:22 -080055#define SPI_TMOD_TR 0x0 /* xmit & recv */
56#define SPI_TMOD_TO 0x1 /* xmit only */
57#define SPI_TMOD_RO 0x2 /* recv only */
58#define SPI_TMOD_EPROMREAD 0x3 /* eeprom read mode */
59
60#define SPI_SLVOE_OFFSET 10
61#define SPI_SRL_OFFSET 11
62#define SPI_CFS_OFFSET 12
63
Wan Ahmad Zainiee539f432020-05-05 21:06:14 +080064/* Bit fields in CTRLR0 based on DWC_ssi_databook.pdf v1.01a */
65#define DWC_SSI_CTRLR0_SRL_OFFSET 13
66#define DWC_SSI_CTRLR0_TMOD_OFFSET 10
67#define DWC_SSI_CTRLR0_TMOD_MASK GENMASK(11, 10)
68#define DWC_SSI_CTRLR0_SCPOL_OFFSET 9
69#define DWC_SSI_CTRLR0_SCPH_OFFSET 8
70#define DWC_SSI_CTRLR0_FRF_OFFSET 6
71#define DWC_SSI_CTRLR0_DFS_OFFSET 0
72
Feng Tange24c7452009-12-14 14:20:22 -080073/* Bit fields in SR, 7 bits */
74#define SR_MASK 0x7f /* cover 7 bits */
75#define SR_BUSY (1 << 0)
76#define SR_TF_NOT_FULL (1 << 1)
77#define SR_TF_EMPT (1 << 2)
78#define SR_RF_NOT_EMPT (1 << 3)
79#define SR_RF_FULL (1 << 4)
80#define SR_TX_ERR (1 << 5)
81#define SR_DCOL (1 << 6)
82
83/* Bit fields in ISR, IMR, RISR, 7 bits */
84#define SPI_INT_TXEI (1 << 0)
85#define SPI_INT_TXOI (1 << 1)
86#define SPI_INT_RXUI (1 << 2)
87#define SPI_INT_RXOI (1 << 3)
88#define SPI_INT_RXFI (1 << 4)
89#define SPI_INT_MSTI (1 << 5)
90
Andy Shevchenko15ee3be2014-10-02 16:31:07 +030091/* Bit fields in DMACR */
92#define SPI_DMA_RDMAE (1 << 0)
93#define SPI_DMA_TDMAE (1 << 1)
94
Feng Tange24c7452009-12-14 14:20:22 -080095enum dw_ssi_type {
96 SSI_MOTO_SPI = 0,
97 SSI_TI_SSP,
98 SSI_NS_MICROWIRE,
99};
100
Feng Tang7063c0d2010-12-24 13:59:11 +0800101struct dw_spi;
102struct dw_spi_dma_ops {
Andy Shevchenko6370aba2020-05-06 18:30:24 +0300103 int (*dma_init)(struct device *dev, struct dw_spi *dws);
Feng Tang7063c0d2010-12-24 13:59:11 +0800104 void (*dma_exit)(struct dw_spi *dws);
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200105 int (*dma_setup)(struct dw_spi *dws, struct spi_transfer *xfer);
Jarkko Nikula721483e2018-02-01 17:17:29 +0200106 bool (*can_dma)(struct spi_controller *master, struct spi_device *spi,
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200107 struct spi_transfer *xfer);
108 int (*dma_transfer)(struct dw_spi *dws, struct spi_transfer *xfer);
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200109 void (*dma_stop)(struct dw_spi *dws);
Feng Tang7063c0d2010-12-24 13:59:11 +0800110};
111
Feng Tange24c7452009-12-14 14:20:22 -0800112struct dw_spi {
Jarkko Nikula721483e2018-02-01 17:17:29 +0200113 struct spi_controller *master;
Feng Tange24c7452009-12-14 14:20:22 -0800114 enum dw_ssi_type type;
115
116 void __iomem *regs;
117 unsigned long paddr;
Feng Tange24c7452009-12-14 14:20:22 -0800118 int irq;
Feng Tang552e4502010-01-20 13:49:45 -0700119 u32 fifo_len; /* depth of the FIFO buffer */
Feng Tange24c7452009-12-14 14:20:22 -0800120 u32 max_freq; /* max bus freq supported */
121
Talel Shenharf2d70472018-10-11 14:20:07 +0300122 int cs_override;
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200123 u32 reg_io_width; /* DR I/O width in bytes */
Feng Tange24c7452009-12-14 14:20:22 -0800124 u16 bus_num;
125 u16 num_cs; /* supported slave numbers */
Alexandre Belloni62dbbae2018-07-17 16:23:11 +0200126 void (*set_cs)(struct spi_device *spi, bool enable);
Wan Ahmad Zainiec4eadee2020-05-05 21:06:13 +0800127 u32 (*update_cr0)(struct spi_controller *master, struct spi_device *spi,
128 struct spi_transfer *transfer);
Feng Tange24c7452009-12-14 14:20:22 -0800129
Feng Tange24c7452009-12-14 14:20:22 -0800130 /* Current message transfer state info */
Feng Tange24c7452009-12-14 14:20:22 -0800131 size_t len;
132 void *tx;
133 void *tx_end;
wuxu.wu19b61392020-01-01 11:39:41 +0800134 spinlock_t buf_lock;
Feng Tange24c7452009-12-14 14:20:22 -0800135 void *rx;
136 void *rx_end;
137 int dma_mapped;
Feng Tange24c7452009-12-14 14:20:22 -0800138 u8 n_bytes; /* current is a 1/2 bytes op */
Feng Tange24c7452009-12-14 14:20:22 -0800139 irqreturn_t (*transfer_handler)(struct dw_spi *dws);
Matthias Seidel13b10302016-09-04 02:04:49 +0200140 u32 current_freq; /* frequency in hz */
Lars Povlsenbac70b52020-08-24 22:30:05 +0200141 u32 cur_rx_sample_dly;
142 u32 def_rx_sample_dly_ns;
Feng Tange24c7452009-12-14 14:20:22 -0800143
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200144 /* DMA info */
Feng Tange24c7452009-12-14 14:20:22 -0800145 struct dma_chan *txchan;
Serge Semin0b2b6652020-05-29 16:11:56 +0300146 u32 txburst;
Feng Tange24c7452009-12-14 14:20:22 -0800147 struct dma_chan *rxchan;
Serge Semin0b2b6652020-05-29 16:11:56 +0300148 u32 rxburst;
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200149 unsigned long dma_chan_busy;
Feng Tang7063c0d2010-12-24 13:59:11 +0800150 dma_addr_t dma_addr; /* phy address of the Data register */
Julia Lawall4fe338c2015-11-28 15:09:38 +0100151 const struct dw_spi_dma_ops *dma_ops;
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300152 struct completion dma_completion;
Feng Tange24c7452009-12-14 14:20:22 -0800153
Feng Tange24c7452009-12-14 14:20:22 -0800154#ifdef CONFIG_DEBUG_FS
155 struct dentry *debugfs;
Serge Semin83784492020-05-29 16:12:04 +0300156 struct debugfs_regset32 regset;
Feng Tange24c7452009-12-14 14:20:22 -0800157#endif
158};
159
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -0700160static inline u32 dw_readl(struct dw_spi *dws, u32 offset)
161{
162 return __raw_readl(dws->regs + offset);
163}
164
165static inline void dw_writel(struct dw_spi *dws, u32 offset, u32 val)
166{
167 __raw_writel(val, dws->regs + offset);
168}
169
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200170static inline u32 dw_read_io_reg(struct dw_spi *dws, u32 offset)
171{
172 switch (dws->reg_io_width) {
173 case 2:
Serge Semin7e31cea2020-09-20 14:28:51 +0300174 return readw_relaxed(dws->regs + offset);
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200175 case 4:
176 default:
Serge Semin7e31cea2020-09-20 14:28:51 +0300177 return readl_relaxed(dws->regs + offset);
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200178 }
179}
180
181static inline void dw_write_io_reg(struct dw_spi *dws, u32 offset, u32 val)
182{
183 switch (dws->reg_io_width) {
184 case 2:
Serge Semin7e31cea2020-09-20 14:28:51 +0300185 writew_relaxed(val, dws->regs + offset);
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200186 break;
187 case 4:
188 default:
Serge Semin7e31cea2020-09-20 14:28:51 +0300189 writel_relaxed(val, dws->regs + offset);
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200190 break;
191 }
192}
193
Feng Tange24c7452009-12-14 14:20:22 -0800194static inline void spi_enable_chip(struct dw_spi *dws, int enable)
195{
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -0700196 dw_writel(dws, DW_SPI_SSIENR, (enable ? 1 : 0));
Feng Tange24c7452009-12-14 14:20:22 -0800197}
198
199static inline void spi_set_clk(struct dw_spi *dws, u16 div)
200{
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -0700201 dw_writel(dws, DW_SPI_BAUDR, div);
Feng Tange24c7452009-12-14 14:20:22 -0800202}
203
Feng Tange24c7452009-12-14 14:20:22 -0800204/* Disable IRQ bits */
205static inline void spi_mask_intr(struct dw_spi *dws, u32 mask)
206{
207 u32 new_mask;
208
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -0700209 new_mask = dw_readl(dws, DW_SPI_IMR) & ~mask;
210 dw_writel(dws, DW_SPI_IMR, new_mask);
Feng Tange24c7452009-12-14 14:20:22 -0800211}
212
213/* Enable IRQ bits */
214static inline void spi_umask_intr(struct dw_spi *dws, u32 mask)
215{
216 u32 new_mask;
217
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -0700218 new_mask = dw_readl(dws, DW_SPI_IMR) | mask;
219 dw_writel(dws, DW_SPI_IMR, new_mask);
Feng Tange24c7452009-12-14 14:20:22 -0800220}
221
222/*
Serge Semina128f6e2020-09-20 14:28:49 +0300223 * This disables the SPI controller, interrupts, clears the interrupts status,
224 * and re-enable the controller back. Transmit and receive FIFO buffers are
225 * cleared when the device is disabled.
Andy Shevchenko45746e82015-03-02 14:58:55 +0200226 */
227static inline void spi_reset_chip(struct dw_spi *dws)
228{
229 spi_enable_chip(dws, 0);
230 spi_mask_intr(dws, 0xff);
Serge Semina128f6e2020-09-20 14:28:49 +0300231 dw_readl(dws, DW_SPI_ICR);
Andy Shevchenko45746e82015-03-02 14:58:55 +0200232 spi_enable_chip(dws, 1);
233}
234
Andy Shevchenko1cc3f142015-10-14 23:12:23 +0300235static inline void spi_shutdown_chip(struct dw_spi *dws)
236{
237 spi_enable_chip(dws, 0);
238 spi_set_clk(dws, 0);
239}
240
Alexandre Bellonic79bdbb2018-07-27 21:53:54 +0200241extern void dw_spi_set_cs(struct spi_device *spi, bool enable);
Baruch Siach04f421e2013-12-30 20:30:44 +0200242extern int dw_spi_add_host(struct device *dev, struct dw_spi *dws);
Feng Tange24c7452009-12-14 14:20:22 -0800243extern void dw_spi_remove_host(struct dw_spi *dws);
244extern int dw_spi_suspend_host(struct dw_spi *dws);
245extern int dw_spi_resume_host(struct dw_spi *dws);
Wan Ahmad Zainiec4eadee2020-05-05 21:06:13 +0800246extern u32 dw_spi_update_cr0(struct spi_controller *master,
247 struct spi_device *spi,
248 struct spi_transfer *transfer);
Wan Ahmad Zainiee539f432020-05-05 21:06:14 +0800249extern u32 dw_spi_update_cr0_v1_01a(struct spi_controller *master,
250 struct spi_device *spi,
251 struct spi_transfer *transfer);
Feng Tang7063c0d2010-12-24 13:59:11 +0800252
Serge Semin6c710c02020-05-29 16:11:59 +0300253#ifdef CONFIG_SPI_DW_DMA
254
Serge Semin57784412020-05-29 16:12:02 +0300255extern void dw_spi_dma_setup_mfld(struct dw_spi *dws);
256extern void dw_spi_dma_setup_generic(struct dw_spi *dws);
Serge Semin6c710c02020-05-29 16:11:59 +0300257
258#else
259
Serge Semin57784412020-05-29 16:12:02 +0300260static inline void dw_spi_dma_setup_mfld(struct dw_spi *dws) {}
261static inline void dw_spi_dma_setup_generic(struct dw_spi *dws) {}
Serge Semin6c710c02020-05-29 16:11:59 +0300262
263#endif /* !CONFIG_SPI_DW_DMA */
Andy Shevchenko37aa8aa2020-05-06 18:30:23 +0300264
Feng Tange24c7452009-12-14 14:20:22 -0800265#endif /* DW_SPI_HEADER_H */