Prepare v2024.10
[platform/kernel/u-boot.git] / drivers / spi / cadence_qspi.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (C) 2012
4  * Altera Corporation <www.altera.com>
5  */
6
7 #ifndef __CADENCE_QSPI_H__
8 #define __CADENCE_QSPI_H__
9
10 #include <reset.h>
11 #include <linux/mtd/spi-nor.h>
12 #include <spi-mem.h>
13
14 #define CQSPI_IS_ADDR(cmd_len)          (cmd_len > 1 ? 1 : 0)
15
16 #define CQSPI_NO_DECODER_MAX_CS         4
17 #define CQSPI_DECODER_MAX_CS            16
18 #define CQSPI_READ_CAPTURE_MAX_DELAY    16
19
20 #define CQSPI_REG_POLL_US                       1 /* 1us */
21 #define CQSPI_REG_RETRY                         10000
22 #define CQSPI_POLL_IDLE_RETRY                   3
23
24 /* Transfer mode */
25 #define CQSPI_INST_TYPE_SINGLE                  0
26 #define CQSPI_INST_TYPE_DUAL                    1
27 #define CQSPI_INST_TYPE_QUAD                    2
28 #define CQSPI_INST_TYPE_OCTAL                   3
29
30 #define CQSPI_STIG_DATA_LEN_MAX                 8
31
32 #define CQSPI_DUMMY_CLKS_PER_BYTE               8
33 #define CQSPI_DUMMY_BYTES_MAX                   4
34 #define CQSPI_DUMMY_CLKS_MAX                    31
35
36 #define CMD_4BYTE_FAST_READ                     0x0C
37 #define CMD_4BYTE_OCTAL_READ                    0x7c
38 #define CMD_4BYTE_READ                          0x13
39
40 /****************************************************************************
41  * Controller's configuration and status register (offset from QSPI_BASE)
42  ****************************************************************************/
43 #define CQSPI_REG_CONFIG                        0x00
44 #define CQSPI_REG_CONFIG_ENABLE                 BIT(0)
45 #define CQSPI_REG_CONFIG_CLK_POL                BIT(1)
46 #define CQSPI_REG_CONFIG_CLK_PHA                BIT(2)
47 #define CQSPI_REG_CONFIG_PHY_ENABLE_MASK        BIT(3)
48 #define CQSPI_REG_CONFIG_DIRECT                 BIT(7)
49 #define CQSPI_REG_CONFIG_DECODE                 BIT(9)
50 #define CQSPI_REG_CONFIG_ENBL_DMA               BIT(15)
51 #define CQSPI_REG_CONFIG_XIP_IMM                BIT(18)
52 #define CQSPI_REG_CONFIG_DTR_PROT_EN_MASK       BIT(24)
53 #define CQSPI_REG_CONFIG_CHIPSELECT_LSB         10
54 #define CQSPI_REG_CONFIG_BAUD_LSB               19
55 #define CQSPI_REG_CONFIG_DTR_PROTO              BIT(24)
56 #define CQSPI_REG_CONFIG_DUAL_OPCODE            BIT(30)
57 #define CQSPI_REG_CONFIG_IDLE_LSB               31
58 #define CQSPI_REG_CONFIG_CHIPSELECT_MASK        0xF
59 #define CQSPI_REG_CONFIG_BAUD_MASK              0xF
60
61 #define CQSPI_REG_RD_INSTR                      0x04
62 #define CQSPI_REG_RD_INSTR_OPCODE_LSB           0
63 #define CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB       8
64 #define CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB        12
65 #define CQSPI_REG_RD_INSTR_TYPE_DATA_LSB        16
66 #define CQSPI_REG_RD_INSTR_MODE_EN_LSB          20
67 #define CQSPI_REG_RD_INSTR_DUMMY_LSB            24
68 #define CQSPI_REG_RD_INSTR_TYPE_INSTR_MASK      0x3
69 #define CQSPI_REG_RD_INSTR_TYPE_ADDR_MASK       0x3
70 #define CQSPI_REG_RD_INSTR_TYPE_DATA_MASK       0x3
71 #define CQSPI_REG_RD_INSTR_DUMMY_MASK           0x1F
72
73 #define CQSPI_REG_WR_INSTR                      0x08
74 #define CQSPI_REG_WR_INSTR_OPCODE_LSB           0
75 #define CQSPI_REG_WR_INSTR_TYPE_ADDR_LSB        12
76 #define CQSPI_REG_WR_INSTR_TYPE_DATA_LSB        16
77
78 #define CQSPI_REG_DELAY                         0x0C
79 #define CQSPI_REG_DELAY_TSLCH_LSB               0
80 #define CQSPI_REG_DELAY_TCHSH_LSB               8
81 #define CQSPI_REG_DELAY_TSD2D_LSB               16
82 #define CQSPI_REG_DELAY_TSHSL_LSB               24
83 #define CQSPI_REG_DELAY_TSLCH_MASK              0xFF
84 #define CQSPI_REG_DELAY_TCHSH_MASK              0xFF
85 #define CQSPI_REG_DELAY_TSD2D_MASK              0xFF
86 #define CQSPI_REG_DELAY_TSHSL_MASK              0xFF
87
88 #define CQSPI_REG_RD_DATA_CAPTURE               0x10
89 #define CQSPI_REG_RD_DATA_CAPTURE_BYPASS        BIT(0)
90 #define CQSPI_REG_READCAPTURE_DQS_ENABLE        BIT(8)
91 #define CQSPI_REG_RD_DATA_CAPTURE_DELAY_LSB     1
92 #define CQSPI_REG_RD_DATA_CAPTURE_DELAY_MASK    0xF
93
94 #define CQSPI_REG_SIZE                          0x14
95 #define CQSPI_REG_SIZE_ADDRESS_LSB              0
96 #define CQSPI_REG_SIZE_PAGE_LSB                 4
97 #define CQSPI_REG_SIZE_BLOCK_LSB                16
98 #define CQSPI_REG_SIZE_ADDRESS_MASK             0xF
99 #define CQSPI_REG_SIZE_PAGE_MASK                0xFFF
100 #define CQSPI_REG_SIZE_BLOCK_MASK               0x3F
101
102 #define CQSPI_REG_SRAMPARTITION                 0x18
103 #define CQSPI_REG_INDIRECTTRIGGER               0x1C
104
105 #define CQSPI_REG_REMAP                         0x24
106 #define CQSPI_REG_MODE_BIT                      0x28
107
108 #define CQSPI_REG_SDRAMLEVEL                    0x2C
109 #define CQSPI_REG_SDRAMLEVEL_RD_LSB             0
110 #define CQSPI_REG_SDRAMLEVEL_WR_LSB             16
111 #define CQSPI_REG_SDRAMLEVEL_RD_MASK            0xFFFF
112 #define CQSPI_REG_SDRAMLEVEL_WR_MASK            0xFFFF
113
114 #define CQSPI_REG_WR_COMPLETION_CTRL            0x38
115 #define CQSPI_REG_WR_DISABLE_AUTO_POLL          BIT(14)
116
117 #define CQSPI_REG_IRQSTATUS                     0x40
118 #define CQSPI_REG_IRQMASK                       0x44
119
120 #define CQSPI_REG_INDIRECTRD                    0x60
121 #define CQSPI_REG_INDIRECTRD_START              BIT(0)
122 #define CQSPI_REG_INDIRECTRD_CANCEL             BIT(1)
123 #define CQSPI_REG_INDIRECTRD_INPROGRESS         BIT(2)
124 #define CQSPI_REG_INDIRECTRD_DONE               BIT(5)
125
126 #define CQSPI_REG_INDIRECTRDWATERMARK           0x64
127 #define CQSPI_REG_INDIRECTRDSTARTADDR           0x68
128 #define CQSPI_REG_INDIRECTRDBYTES               0x6C
129
130 #define CQSPI_REG_CMDCTRL                       0x90
131 #define CQSPI_REG_CMDCTRL_EXECUTE               BIT(0)
132 #define CQSPI_REG_CMDCTRL_INPROGRESS            BIT(1)
133 #define CQSPI_REG_CMDCTRL_DUMMY_LSB             7
134 #define CQSPI_REG_CMDCTRL_WR_BYTES_LSB          12
135 #define CQSPI_REG_CMDCTRL_WR_EN_LSB             15
136 #define CQSPI_REG_CMDCTRL_ADD_BYTES_LSB         16
137 #define CQSPI_REG_CMDCTRL_ADDR_EN_LSB           19
138 #define CQSPI_REG_CMDCTRL_RD_BYTES_LSB          20
139 #define CQSPI_REG_CMDCTRL_RD_EN_LSB             23
140 #define CQSPI_REG_CMDCTRL_OPCODE_LSB            24
141 #define CQSPI_REG_CMDCTRL_DUMMY_MASK            0x1F
142 #define CQSPI_REG_CMDCTRL_WR_BYTES_MASK         0x7
143 #define CQSPI_REG_CMDCTRL_ADD_BYTES_MASK        0x3
144 #define CQSPI_REG_CMDCTRL_RD_BYTES_MASK         0x7
145 #define CQSPI_REG_CMDCTRL_OPCODE_MASK           0xFF
146
147 #define CQSPI_REG_INDIRECTWR                    0x70
148 #define CQSPI_REG_INDIRECTWR_START              BIT(0)
149 #define CQSPI_REG_INDIRECTWR_CANCEL             BIT(1)
150 #define CQSPI_REG_INDIRECTWR_INPROGRESS         BIT(2)
151 #define CQSPI_REG_INDIRECTWR_DONE               BIT(5)
152
153 #define CQSPI_REG_INDIRECTWRWATERMARK           0x74
154 #define CQSPI_REG_INDIRECTWRSTARTADDR           0x78
155 #define CQSPI_REG_INDIRECTWRBYTES               0x7C
156
157 #define CQSPI_REG_CMDADDRESS                    0x94
158 #define CQSPI_REG_CMDREADDATALOWER              0xA0
159 #define CQSPI_REG_CMDREADDATAUPPER              0xA4
160 #define CQSPI_REG_CMDWRITEDATALOWER             0xA8
161 #define CQSPI_REG_CMDWRITEDATAUPPER             0xAC
162
163 #define CQSPI_REG_OP_EXT_LOWER                  0xE0
164 #define CQSPI_REG_OP_EXT_READ_LSB               24
165 #define CQSPI_REG_OP_EXT_WRITE_LSB              16
166 #define CQSPI_REG_OP_EXT_STIG_LSB               0
167
168 #define CQSPI_REG_PHY_CONFIG                    0xB4
169 #define CQSPI_REG_PHY_CONFIG_RESET_FLD_MASK     0x40000000
170
171 #define CQSPI_DMA_DST_ADDR_REG                  0x1800
172 #define CQSPI_DMA_DST_SIZE_REG                  0x1804
173 #define CQSPI_DMA_DST_STS_REG                   0x1808
174 #define CQSPI_DMA_DST_CTRL_REG                  0x180C
175 #define CQSPI_DMA_DST_I_STS_REG                 0x1814
176 #define CQSPI_DMA_DST_I_ENBL_REG                0x1818
177 #define CQSPI_DMA_DST_I_DISBL_REG               0x181C
178 #define CQSPI_DMA_DST_CTRL2_REG                 0x1824
179 #define CQSPI_DMA_DST_ADDR_MSB_REG              0x1828
180
181 #define CQSPI_DMA_SRC_RD_ADDR_REG               0x1000
182
183 #define CQSPI_REG_DMA_PERIPH_CFG                0x20
184 #define CQSPI_REG_INDIR_TRIG_ADDR_RANGE         0x80
185 #define CQSPI_DFLT_INDIR_TRIG_ADDR_RANGE        6
186 #define CQSPI_DFLT_DMA_PERIPH_CFG               0x602
187 #define CQSPI_DFLT_DST_CTRL_REG_VAL             0xF43FFA00
188
189 #define CQSPI_DMA_DST_I_STS_DONE                BIT(1)
190 #define CQSPI_DMA_TIMEOUT                       10000000
191
192 #define CQSPI_REG_IS_IDLE(base)                         \
193         ((readl((base) + CQSPI_REG_CONFIG) >>           \
194         CQSPI_REG_CONFIG_IDLE_LSB) & 0x1)
195
196 #define CQSPI_GET_RD_SRAM_LEVEL(reg_base)               \
197         (((readl((reg_base) + CQSPI_REG_SDRAMLEVEL)) >> \
198         CQSPI_REG_SDRAMLEVEL_RD_LSB) & CQSPI_REG_SDRAMLEVEL_RD_MASK)
199
200 #define CQSPI_GET_WR_SRAM_LEVEL(reg_base)               \
201         (((readl((reg_base) + CQSPI_REG_SDRAMLEVEL)) >> \
202         CQSPI_REG_SDRAMLEVEL_WR_LSB) & CQSPI_REG_SDRAMLEVEL_WR_MASK)
203
204 struct cadence_spi_plat {
205         unsigned int    max_hz;
206         void            *regbase;
207         void            *ahbbase;
208         bool            is_decoded_cs;
209         u32             fifo_depth;
210         u32             fifo_width;
211         u32             trigger_address;
212         fdt_addr_t      ahbsize;
213         bool            use_dac_mode;
214         int             read_delay;
215
216         /* Flash parameters */
217         u32             page_size;
218         u32             block_size;
219         u32             tshsl_ns;
220         u32             tsd2d_ns;
221         u32             tchsh_ns;
222         u32             tslch_ns;
223
224         bool            is_dma;
225 };
226
227 struct cadence_spi_priv {
228         unsigned int    ref_clk_hz;
229         unsigned int    max_hz;
230         void            *regbase;
231         void            *ahbbase;
232         unsigned int    fifo_depth;
233         unsigned int    fifo_width;
234         unsigned int    trigger_address;
235         fdt_addr_t      ahbsize;
236         size_t          cmd_len;
237         u8              cmd_buf[32];
238         size_t          data_len;
239
240         int             qspi_is_init;
241         unsigned int    qspi_calibrated_hz;
242         unsigned int    qspi_calibrated_cs;
243         unsigned int    previous_hz;
244         u32             wr_delay;
245         int             read_delay;
246
247         struct reset_ctl_bulk *resets;
248         u32             page_size;
249         u32             block_size;
250         u32             tshsl_ns;
251         u32             tsd2d_ns;
252         u32             tchsh_ns;
253         u32             tslch_ns;
254         u8              edge_mode;
255         u8              dll_mode;
256         bool            extra_dummy;
257         bool            ddr_init;
258         bool            is_decoded_cs;
259         bool            use_dac_mode;
260         bool            is_dma;
261
262         /* Transaction protocol parameters. */
263         u8              inst_width;
264         u8              addr_width;
265         u8              data_width;
266         bool            dtr;
267 };
268
269 /* Functions call declaration */
270 void cadence_qspi_apb_controller_init(struct cadence_spi_priv *priv);
271 void cadence_qspi_apb_controller_enable(void *reg_base_addr);
272 void cadence_qspi_apb_controller_disable(void *reg_base_addr);
273 void cadence_qspi_apb_dac_mode_enable(void *reg_base);
274
275 int cadence_qspi_apb_command_read_setup(struct cadence_spi_priv *priv,
276                                         const struct spi_mem_op *op);
277 int cadence_qspi_apb_command_read(struct cadence_spi_priv *priv,
278                                   const struct spi_mem_op *op);
279 int cadence_qspi_apb_command_write_setup(struct cadence_spi_priv *priv,
280                                          const struct spi_mem_op *op);
281 int cadence_qspi_apb_command_write(struct cadence_spi_priv *priv,
282                                    const struct spi_mem_op *op);
283
284 int cadence_qspi_apb_read_setup(struct cadence_spi_priv *priv,
285                                 const struct spi_mem_op *op);
286 int cadence_qspi_apb_read_execute(struct cadence_spi_priv *priv,
287                                   const struct spi_mem_op *op);
288 int cadence_qspi_apb_write_setup(struct cadence_spi_priv *priv,
289                                  const struct spi_mem_op *op);
290 int cadence_qspi_apb_write_execute(struct cadence_spi_priv *priv,
291                                    const struct spi_mem_op *op);
292
293 void cadence_qspi_apb_chipselect(void *reg_base,
294         unsigned int chip_select, unsigned int decoder_enable);
295 void cadence_qspi_apb_set_clk_mode(void *reg_base, uint mode);
296 void cadence_qspi_apb_config_baudrate_div(void *reg_base,
297         unsigned int ref_clk_hz, unsigned int sclk_hz);
298 void cadence_qspi_apb_delay(void *reg_base,
299         unsigned int ref_clk, unsigned int sclk_hz,
300         unsigned int tshsl_ns, unsigned int tsd2d_ns,
301         unsigned int tchsh_ns, unsigned int tslch_ns);
302 void cadence_qspi_apb_enter_xip(void *reg_base, char xip_dummy);
303 void cadence_qspi_apb_readdata_capture(void *reg_base,
304         unsigned int bypass, unsigned int delay);
305 unsigned int cm_get_qspi_controller_clk_hz(void);
306 int cadence_qspi_apb_dma_read(struct cadence_spi_priv *priv,
307                               const struct spi_mem_op *op);
308 int cadence_qspi_apb_wait_for_dma_cmplt(struct cadence_spi_priv *priv);
309 int cadence_qspi_apb_exec_flash_cmd(void *reg_base, unsigned int reg);
310 int cadence_qspi_versal_flash_reset(struct udevice *dev);
311 ofnode cadence_qspi_get_subnode(struct udevice *dev);
312 void cadence_qspi_apb_enable_linear_mode(bool enable);
313
314 #endif /* __CADENCE_QSPI_H__ */