blob: faf40cb66498dd11fb149f66c03412b7f942d610 [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 Semincc760f32020-09-20 14:28:53 +03005#include <linux/bits.h>
Serge Seminbdbdf0f2020-05-29 16:11:52 +03006#include <linux/completion.h>
Serge Semin83784492020-05-29 16:12:04 +03007#include <linux/debugfs.h>
Andy Shevchenkoe62a15d2020-05-06 18:30:21 +03008#include <linux/irqreturn.h>
Feng Tange24c7452009-12-14 14:20:22 -08009#include <linux/io.h>
Jiri Slaby46165a3d2011-03-18 10:41:17 +010010#include <linux/scatterlist.h>
Serge Semin64232072020-10-08 02:55:06 +030011#include <linux/spi/spi-mem.h>
Feng Tange24c7452009-12-14 14:20:22 -080012
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070013/* Register offsets */
Wan Ahmad Zainie299cb652020-05-05 21:06:12 +080014#define DW_SPI_CTRLR0 0x00
15#define DW_SPI_CTRLR1 0x04
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070016#define DW_SPI_SSIENR 0x08
17#define DW_SPI_MWCR 0x0c
18#define DW_SPI_SER 0x10
19#define DW_SPI_BAUDR 0x14
Wan Ahmad Zainie299cb652020-05-05 21:06:12 +080020#define DW_SPI_TXFTLR 0x18
21#define DW_SPI_RXFTLR 0x1c
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070022#define DW_SPI_TXFLR 0x20
23#define DW_SPI_RXFLR 0x24
24#define DW_SPI_SR 0x28
25#define DW_SPI_IMR 0x2c
26#define DW_SPI_ISR 0x30
27#define DW_SPI_RISR 0x34
28#define DW_SPI_TXOICR 0x38
29#define DW_SPI_RXOICR 0x3c
30#define DW_SPI_RXUICR 0x40
31#define DW_SPI_MSTICR 0x44
32#define DW_SPI_ICR 0x48
33#define DW_SPI_DMACR 0x4c
34#define DW_SPI_DMATDLR 0x50
35#define DW_SPI_DMARDLR 0x54
36#define DW_SPI_IDR 0x58
37#define DW_SPI_VERSION 0x5c
38#define DW_SPI_DR 0x60
Lars Povlsenbac70b52020-08-24 22:30:05 +020039#define DW_SPI_RX_SAMPLE_DLY 0xf0
Talel Shenharf2d70472018-10-11 14:20:07 +030040#define DW_SPI_CS_OVERRIDE 0xf4
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -070041
Feng Tange24c7452009-12-14 14:20:22 -080042/* Bit fields in CTRLR0 */
43#define SPI_DFS_OFFSET 0
44
45#define SPI_FRF_OFFSET 4
46#define SPI_FRF_SPI 0x0
47#define SPI_FRF_SSP 0x1
48#define SPI_FRF_MICROWIRE 0x2
49#define SPI_FRF_RESV 0x3
50
51#define SPI_MODE_OFFSET 6
52#define SPI_SCPH_OFFSET 6
53#define SPI_SCOL_OFFSET 7
Feng Tange3e55ff2010-09-07 15:52:06 +080054
Feng Tange24c7452009-12-14 14:20:22 -080055#define SPI_TMOD_OFFSET 8
Feng Tange3e55ff2010-09-07 15:52:06 +080056#define SPI_TMOD_MASK (0x3 << SPI_TMOD_OFFSET)
Feng Tange24c7452009-12-14 14:20:22 -080057#define SPI_TMOD_TR 0x0 /* xmit & recv */
58#define SPI_TMOD_TO 0x1 /* xmit only */
59#define SPI_TMOD_RO 0x2 /* recv only */
60#define SPI_TMOD_EPROMREAD 0x3 /* eeprom read mode */
61
62#define SPI_SLVOE_OFFSET 10
63#define SPI_SRL_OFFSET 11
64#define SPI_CFS_OFFSET 12
65
Wan Ahmad Zainiee539f432020-05-05 21:06:14 +080066/* 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
Serge Seminffb7ca52020-09-20 14:28:54 +030075/*
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
Serge Semin64232072020-10-08 02:55:06 +030082/* Bit fields in CTRLR1 */
83#define SPI_NDF_MASK GENMASK(15, 0)
84
Feng Tange24c7452009-12-14 14:20:22 -080085/* Bit fields in SR, 7 bits */
86#define SR_MASK 0x7f /* cover 7 bits */
87#define SR_BUSY (1 << 0)
88#define SR_TF_NOT_FULL (1 << 1)
89#define SR_TF_EMPT (1 << 2)
90#define SR_RF_NOT_EMPT (1 << 3)
91#define SR_RF_FULL (1 << 4)
92#define SR_TX_ERR (1 << 5)
93#define SR_DCOL (1 << 6)
94
95/* Bit fields in ISR, IMR, RISR, 7 bits */
96#define SPI_INT_TXEI (1 << 0)
97#define SPI_INT_TXOI (1 << 1)
98#define SPI_INT_RXUI (1 << 2)
99#define SPI_INT_RXOI (1 << 3)
100#define SPI_INT_RXFI (1 << 4)
101#define SPI_INT_MSTI (1 << 5)
102
Andy Shevchenko15ee3be2014-10-02 16:31:07 +0300103/* Bit fields in DMACR */
104#define SPI_DMA_RDMAE (1 << 0)
105#define SPI_DMA_TDMAE (1 << 1)
106
Serge Semincf75bae2020-10-08 02:55:04 +0300107#define SPI_WAIT_RETRIES 5
Serge Semin64232072020-10-08 02:55:06 +0300108#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)
Serge Semincf75bae2020-10-08 02:55:04 +0300113
Feng Tange24c7452009-12-14 14:20:22 -0800114enum dw_ssi_type {
115 SSI_MOTO_SPI = 0,
116 SSI_TI_SSP,
117 SSI_NS_MICROWIRE,
118};
119
Serge Semincc760f32020-09-20 14:28:53 +0300120/* DW SPI capabilities */
121#define DW_SPI_CAP_CS_OVERRIDE BIT(0)
Serge Seminffb7ca52020-09-20 14:28:54 +0300122#define DW_SPI_CAP_KEEMBAY_MST BIT(1)
Serge Semind6bbd112020-10-08 02:54:51 +0300123#define DW_SPI_CAP_DWC_SSI BIT(2)
Serge Semincc760f32020-09-20 14:28:53 +0300124
Serge Semin3ff60c62020-10-08 02:54:56 +0300125/* Slave spi_transfer/spi_mem_op related */
126struct dw_spi_cfg {
127 u8 tmode;
128 u8 dfs;
129 u32 ndf;
130 u32 freq;
131};
132
Feng Tang7063c0d2010-12-24 13:59:11 +0800133struct dw_spi;
134struct dw_spi_dma_ops {
Andy Shevchenko6370aba2020-05-06 18:30:24 +0300135 int (*dma_init)(struct device *dev, struct dw_spi *dws);
Feng Tang7063c0d2010-12-24 13:59:11 +0800136 void (*dma_exit)(struct dw_spi *dws);
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200137 int (*dma_setup)(struct dw_spi *dws, struct spi_transfer *xfer);
Jarkko Nikula721483e2018-02-01 17:17:29 +0200138 bool (*can_dma)(struct spi_controller *master, struct spi_device *spi,
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200139 struct spi_transfer *xfer);
140 int (*dma_transfer)(struct dw_spi *dws, struct spi_transfer *xfer);
Andy Shevchenko4d5ac1e2015-03-09 16:48:48 +0200141 void (*dma_stop)(struct dw_spi *dws);
Feng Tang7063c0d2010-12-24 13:59:11 +0800142};
143
Feng Tange24c7452009-12-14 14:20:22 -0800144struct dw_spi {
Jarkko Nikula721483e2018-02-01 17:17:29 +0200145 struct spi_controller *master;
Feng Tange24c7452009-12-14 14:20:22 -0800146
147 void __iomem *regs;
148 unsigned long paddr;
Feng Tange24c7452009-12-14 14:20:22 -0800149 int irq;
Feng Tang552e4502010-01-20 13:49:45 -0700150 u32 fifo_len; /* depth of the FIFO buffer */
Serge Semin84ecaf42020-10-08 02:55:07 +0300151 u32 max_mem_freq; /* max mem-ops bus freq */
Feng Tange24c7452009-12-14 14:20:22 -0800152 u32 max_freq; /* max bus freq supported */
153
Serge Semincc760f32020-09-20 14:28:53 +0300154 u32 caps; /* DW SPI capabilities */
155
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200156 u32 reg_io_width; /* DR I/O width in bytes */
Feng Tange24c7452009-12-14 14:20:22 -0800157 u16 bus_num;
158 u16 num_cs; /* supported slave numbers */
Alexandre Belloni62dbbae2018-07-17 16:23:11 +0200159 void (*set_cs)(struct spi_device *spi, bool enable);
Feng Tange24c7452009-12-14 14:20:22 -0800160
Feng Tange24c7452009-12-14 14:20:22 -0800161 /* Current message transfer state info */
Feng Tange24c7452009-12-14 14:20:22 -0800162 void *tx;
Serge Semin8dedbea2020-10-08 02:54:57 +0300163 unsigned int tx_len;
Feng Tange24c7452009-12-14 14:20:22 -0800164 void *rx;
Serge Semin8dedbea2020-10-08 02:54:57 +0300165 unsigned int rx_len;
Serge Semin64232072020-10-08 02:55:06 +0300166 u8 buf[SPI_BUF_SIZE];
Feng Tange24c7452009-12-14 14:20:22 -0800167 int dma_mapped;
Feng Tange24c7452009-12-14 14:20:22 -0800168 u8 n_bytes; /* current is a 1/2 bytes op */
Feng Tange24c7452009-12-14 14:20:22 -0800169 irqreturn_t (*transfer_handler)(struct dw_spi *dws);
Matthias Seidel13b10302016-09-04 02:04:49 +0200170 u32 current_freq; /* frequency in hz */
Lars Povlsenbac70b52020-08-24 22:30:05 +0200171 u32 cur_rx_sample_dly;
172 u32 def_rx_sample_dly_ns;
Feng Tange24c7452009-12-14 14:20:22 -0800173
Serge Semin64232072020-10-08 02:55:06 +0300174 /* Custom memory operations */
175 struct spi_controller_mem_ops mem_ops;
176
Andy Shevchenkof89a6d82015-03-09 16:48:49 +0200177 /* DMA info */
Feng Tange24c7452009-12-14 14:20:22 -0800178 struct dma_chan *txchan;
Serge Semin0b2b6652020-05-29 16:11:56 +0300179 u32 txburst;
Feng Tange24c7452009-12-14 14:20:22 -0800180 struct dma_chan *rxchan;
Serge Semin0b2b6652020-05-29 16:11:56 +0300181 u32 rxburst;
Serge Seminad4fe122020-09-20 14:23:22 +0300182 u32 dma_sg_burst;
Andy Shevchenko30c8eb52014-10-28 18:25:02 +0200183 unsigned long dma_chan_busy;
Feng Tang7063c0d2010-12-24 13:59:11 +0800184 dma_addr_t dma_addr; /* phy address of the Data register */
Julia Lawall4fe338c2015-11-28 15:09:38 +0100185 const struct dw_spi_dma_ops *dma_ops;
Serge Seminbdbdf0f2020-05-29 16:11:52 +0300186 struct completion dma_completion;
Feng Tange24c7452009-12-14 14:20:22 -0800187
Feng Tange24c7452009-12-14 14:20:22 -0800188#ifdef CONFIG_DEBUG_FS
189 struct dentry *debugfs;
Serge Semin83784492020-05-29 16:12:04 +0300190 struct debugfs_regset32 regset;
Feng Tange24c7452009-12-14 14:20:22 -0800191#endif
192};
193
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -0700194static inline u32 dw_readl(struct dw_spi *dws, u32 offset)
195{
196 return __raw_readl(dws->regs + offset);
197}
198
199static inline void dw_writel(struct dw_spi *dws, u32 offset, u32 val)
200{
201 __raw_writel(val, dws->regs + offset);
202}
203
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200204static inline u32 dw_read_io_reg(struct dw_spi *dws, u32 offset)
205{
206 switch (dws->reg_io_width) {
207 case 2:
Serge Semin7e31cea2020-09-20 14:28:51 +0300208 return readw_relaxed(dws->regs + offset);
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200209 case 4:
210 default:
Serge Semin7e31cea2020-09-20 14:28:51 +0300211 return readl_relaxed(dws->regs + offset);
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200212 }
213}
214
215static inline void dw_write_io_reg(struct dw_spi *dws, u32 offset, u32 val)
216{
217 switch (dws->reg_io_width) {
218 case 2:
Serge Semin7e31cea2020-09-20 14:28:51 +0300219 writew_relaxed(val, dws->regs + offset);
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200220 break;
221 case 4:
222 default:
Serge Semin7e31cea2020-09-20 14:28:51 +0300223 writel_relaxed(val, dws->regs + offset);
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200224 break;
225 }
226}
227
Feng Tange24c7452009-12-14 14:20:22 -0800228static inline void spi_enable_chip(struct dw_spi *dws, int enable)
229{
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -0700230 dw_writel(dws, DW_SPI_SSIENR, (enable ? 1 : 0));
Feng Tange24c7452009-12-14 14:20:22 -0800231}
232
233static inline void spi_set_clk(struct dw_spi *dws, u16 div)
234{
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -0700235 dw_writel(dws, DW_SPI_BAUDR, div);
Feng Tange24c7452009-12-14 14:20:22 -0800236}
237
Feng Tange24c7452009-12-14 14:20:22 -0800238/* Disable IRQ bits */
239static inline void spi_mask_intr(struct dw_spi *dws, u32 mask)
240{
241 u32 new_mask;
242
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -0700243 new_mask = dw_readl(dws, DW_SPI_IMR) & ~mask;
244 dw_writel(dws, DW_SPI_IMR, new_mask);
Feng Tange24c7452009-12-14 14:20:22 -0800245}
246
247/* Enable IRQ bits */
248static inline void spi_umask_intr(struct dw_spi *dws, u32 mask)
249{
250 u32 new_mask;
251
H Hartley Sweeten7eb187b2011-09-20 11:06:17 -0700252 new_mask = dw_readl(dws, DW_SPI_IMR) | mask;
253 dw_writel(dws, DW_SPI_IMR, new_mask);
Feng Tange24c7452009-12-14 14:20:22 -0800254}
255
256/*
Serge Seminfbddc982020-10-08 02:55:02 +0300257 * 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.
Andy Shevchenko45746e82015-03-02 14:58:55 +0200260 */
261static inline void spi_reset_chip(struct dw_spi *dws)
262{
263 spi_enable_chip(dws, 0);
264 spi_mask_intr(dws, 0xff);
Serge Semina128f6e2020-09-20 14:28:49 +0300265 dw_readl(dws, DW_SPI_ICR);
Serge Seminfbddc982020-10-08 02:55:02 +0300266 dw_writel(dws, DW_SPI_SER, 0);
Andy Shevchenko45746e82015-03-02 14:58:55 +0200267 spi_enable_chip(dws, 1);
268}
269
Andy Shevchenko1cc3f142015-10-14 23:12:23 +0300270static inline void spi_shutdown_chip(struct dw_spi *dws)
271{
272 spi_enable_chip(dws, 0);
273 spi_set_clk(dws, 0);
274}
275
Alexandre Bellonic79bdbb2018-07-27 21:53:54 +0200276extern void dw_spi_set_cs(struct spi_device *spi, bool enable);
Serge Semin3ff60c62020-10-08 02:54:56 +0300277extern void dw_spi_update_config(struct dw_spi *dws, struct spi_device *spi,
278 struct dw_spi_cfg *cfg);
Serge Seminbf64b662020-10-08 02:55:05 +0300279extern int dw_spi_check_status(struct dw_spi *dws, bool raw);
Baruch Siach04f421e2013-12-30 20:30:44 +0200280extern int dw_spi_add_host(struct device *dev, struct dw_spi *dws);
Feng Tange24c7452009-12-14 14:20:22 -0800281extern void dw_spi_remove_host(struct dw_spi *dws);
282extern int dw_spi_suspend_host(struct dw_spi *dws);
283extern int dw_spi_resume_host(struct dw_spi *dws);
Feng Tang7063c0d2010-12-24 13:59:11 +0800284
Serge Semin6c710c02020-05-29 16:11:59 +0300285#ifdef CONFIG_SPI_DW_DMA
286
Serge Semin57784412020-05-29 16:12:02 +0300287extern void dw_spi_dma_setup_mfld(struct dw_spi *dws);
288extern void dw_spi_dma_setup_generic(struct dw_spi *dws);
Serge Semin6c710c02020-05-29 16:11:59 +0300289
290#else
291
Serge Semin57784412020-05-29 16:12:02 +0300292static inline void dw_spi_dma_setup_mfld(struct dw_spi *dws) {}
293static inline void dw_spi_dma_setup_generic(struct dw_spi *dws) {}
Serge Semin6c710c02020-05-29 16:11:59 +0300294
295#endif /* !CONFIG_SPI_DW_DMA */
Andy Shevchenko37aa8aa2020-05-06 18:30:23 +0300296
Feng Tange24c7452009-12-14 14:20:22 -0800297#endif /* DW_SPI_HEADER_H */