blob: 693474a287134cbd9f011b7b578ec916aa3d4d80 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Stefan Roese1c60fe72014-11-07 12:37:49 +01002/*
3 * Copyright (C) 2012
4 * Altera Corporation <www.altera.com>
Stefan Roese1c60fe72014-11-07 12:37:49 +01005 */
6
7#ifndef __CADENCE_QSPI_H__
8#define __CADENCE_QSPI_H__
9
Simon Goldschmidt46e56a42019-03-01 20:12:35 +010010#include <reset.h>
T Karthik Reddy8cc7e7e2022-05-12 04:05:31 -060011#include <linux/mtd/spi-nor.h>
12#include <spi-mem.h>
Simon Goldschmidt46e56a42019-03-01 20:12:35 +010013
Stefan Roese1c60fe72014-11-07 12:37:49 +010014#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
T Karthik Reddy8cc7e7e2022-05-12 04:05:31 -060020#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
Tejas Bhumkare56bc922024-01-28 12:07:46 +053036#define CMD_4BYTE_FAST_READ 0x0C
37#define CMD_4BYTE_OCTAL_READ 0x7c
38#define CMD_4BYTE_READ 0x13
39
T Karthik Reddy8cc7e7e2022-05-12 04:05:31 -060040/****************************************************************************
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
Simon Glassb75b15b2020-12-03 16:55:23 -0700204struct cadence_spi_plat {
Stefan Roese1c60fe72014-11-07 12:37:49 +0100205 unsigned int max_hz;
206 void *regbase;
207 void *ahbbase;
Jason Rush1b4df5e2018-01-23 17:13:09 -0600208 bool is_decoded_cs;
209 u32 fifo_depth;
210 u32 fifo_width;
211 u32 trigger_address;
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530212 fdt_addr_t ahbsize;
213 bool use_dac_mode;
Pratyush Yadav8e0be9e2021-06-26 00:47:07 +0530214 int read_delay;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100215
Jason Rush1b4df5e2018-01-23 17:13:09 -0600216 /* Flash parameters */
Stefan Roese1c60fe72014-11-07 12:37:49 +0100217 u32 page_size;
218 u32 block_size;
219 u32 tshsl_ns;
220 u32 tsd2d_ns;
221 u32 tchsh_ns;
222 u32 tslch_ns;
Pratyush Yadave1814ad2021-06-26 00:47:09 +0530223
T Karthik Reddy73701e72022-05-12 04:05:32 -0600224 bool is_dma;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100225};
226
227struct cadence_spi_priv {
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600228 unsigned int ref_clk_hz;
229 unsigned int max_hz;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100230 void *regbase;
231 void *ahbbase;
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600232 unsigned int fifo_depth;
233 unsigned int fifo_width;
234 unsigned int trigger_address;
235 fdt_addr_t ahbsize;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100236 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;
Chin Liang See36431f92015-10-17 08:31:55 -0500243 unsigned int previous_hz;
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600244 u32 wr_delay;
245 int read_delay;
Simon Goldschmidt46e56a42019-03-01 20:12:35 +0100246
Christian Gmeinerd560a672022-02-22 17:23:25 +0100247 struct reset_ctl_bulk *resets;
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600248 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;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100267};
268
269/* Functions call declaration */
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600270void cadence_qspi_apb_controller_init(struct cadence_spi_priv *priv);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100271void cadence_qspi_apb_controller_enable(void *reg_base_addr);
272void cadence_qspi_apb_controller_disable(void *reg_base_addr);
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530273void cadence_qspi_apb_dac_mode_enable(void *reg_base);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100274
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600275int cadence_qspi_apb_command_read_setup(struct cadence_spi_priv *priv,
Pratyush Yadave1814ad2021-06-26 00:47:09 +0530276 const struct spi_mem_op *op);
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600277int cadence_qspi_apb_command_read(struct cadence_spi_priv *priv,
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530278 const struct spi_mem_op *op);
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600279int cadence_qspi_apb_command_write_setup(struct cadence_spi_priv *priv,
Pratyush Yadave1814ad2021-06-26 00:47:09 +0530280 const struct spi_mem_op *op);
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600281int cadence_qspi_apb_command_write(struct cadence_spi_priv *priv,
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530282 const struct spi_mem_op *op);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100283
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600284int cadence_qspi_apb_read_setup(struct cadence_spi_priv *priv,
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530285 const struct spi_mem_op *op);
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600286int cadence_qspi_apb_read_execute(struct cadence_spi_priv *priv,
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530287 const struct spi_mem_op *op);
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600288int cadence_qspi_apb_write_setup(struct cadence_spi_priv *priv,
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530289 const struct spi_mem_op *op);
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600290int cadence_qspi_apb_write_execute(struct cadence_spi_priv *priv,
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530291 const struct spi_mem_op *op);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100292
293void cadence_qspi_apb_chipselect(void *reg_base,
294 unsigned int chip_select, unsigned int decoder_enable);
Phil Edworthyeef2edc2016-11-29 12:58:31 +0000295void cadence_qspi_apb_set_clk_mode(void *reg_base, uint mode);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100296void cadence_qspi_apb_config_baudrate_div(void *reg_base,
297 unsigned int ref_clk_hz, unsigned int sclk_hz);
298void 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);
302void cadence_qspi_apb_enter_xip(void *reg_base, char xip_dummy);
303void cadence_qspi_apb_readdata_capture(void *reg_base,
304 unsigned int bypass, unsigned int delay);
Tom Rini3fb5b2f2022-03-30 18:07:23 -0400305unsigned int cm_get_qspi_controller_clk_hz(void);
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600306int cadence_qspi_apb_dma_read(struct cadence_spi_priv *priv,
T Karthik Reddy73701e72022-05-12 04:05:32 -0600307 const struct spi_mem_op *op);
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600308int cadence_qspi_apb_wait_for_dma_cmplt(struct cadence_spi_priv *priv);
T Karthik Reddy73701e72022-05-12 04:05:32 -0600309int cadence_qspi_apb_exec_flash_cmd(void *reg_base, unsigned int reg);
T Karthik Reddy3d71b2d2022-05-12 04:05:33 -0600310int cadence_qspi_versal_flash_reset(struct udevice *dev);
Udit Kumar88f53042023-09-12 15:20:35 +0530311ofnode cadence_qspi_get_subnode(struct udevice *dev);
T Karthik Reddy3b49fbf2022-05-12 04:05:34 -0600312void cadence_qspi_apb_enable_linear_mode(bool enable);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100313
314#endif /* __CADENCE_QSPI_H__ */