2 * Copyright (C) 2012 Altera Corporation <www.altera.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * - Neither the name of the Altera Corporation nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL ALTERA CORPORATION BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <linux/bitops.h>
33 #include <linux/delay.h>
34 #include <linux/errno.h>
39 #include "cadence_qspi.h"
41 #define CQSPI_REG_POLL_US 1 /* 1us */
42 #define CQSPI_REG_RETRY 10000
43 #define CQSPI_POLL_IDLE_RETRY 3
46 #define CQSPI_INST_TYPE_SINGLE 0
47 #define CQSPI_INST_TYPE_DUAL 1
48 #define CQSPI_INST_TYPE_QUAD 2
49 #define CQSPI_INST_TYPE_OCTAL 3
51 #define CQSPI_STIG_DATA_LEN_MAX 8
53 #define CQSPI_DUMMY_CLKS_PER_BYTE 8
54 #define CQSPI_DUMMY_CLKS_MAX 31
56 /****************************************************************************
57 * Controller's configuration and status register (offset from QSPI_BASE)
58 ****************************************************************************/
59 #define CQSPI_REG_CONFIG 0x00
60 #define CQSPI_REG_CONFIG_ENABLE BIT(0)
61 #define CQSPI_REG_CONFIG_CLK_POL BIT(1)
62 #define CQSPI_REG_CONFIG_CLK_PHA BIT(2)
63 #define CQSPI_REG_CONFIG_DIRECT BIT(7)
64 #define CQSPI_REG_CONFIG_DECODE BIT(9)
65 #define CQSPI_REG_CONFIG_XIP_IMM BIT(18)
66 #define CQSPI_REG_CONFIG_CHIPSELECT_LSB 10
67 #define CQSPI_REG_CONFIG_BAUD_LSB 19
68 #define CQSPI_REG_CONFIG_DTR_PROTO BIT(24)
69 #define CQSPI_REG_CONFIG_DUAL_OPCODE BIT(30)
70 #define CQSPI_REG_CONFIG_IDLE_LSB 31
71 #define CQSPI_REG_CONFIG_CHIPSELECT_MASK 0xF
72 #define CQSPI_REG_CONFIG_BAUD_MASK 0xF
74 #define CQSPI_REG_RD_INSTR 0x04
75 #define CQSPI_REG_RD_INSTR_OPCODE_LSB 0
76 #define CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB 8
77 #define CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB 12
78 #define CQSPI_REG_RD_INSTR_TYPE_DATA_LSB 16
79 #define CQSPI_REG_RD_INSTR_MODE_EN_LSB 20
80 #define CQSPI_REG_RD_INSTR_DUMMY_LSB 24
81 #define CQSPI_REG_RD_INSTR_TYPE_INSTR_MASK 0x3
82 #define CQSPI_REG_RD_INSTR_TYPE_ADDR_MASK 0x3
83 #define CQSPI_REG_RD_INSTR_TYPE_DATA_MASK 0x3
84 #define CQSPI_REG_RD_INSTR_DUMMY_MASK 0x1F
86 #define CQSPI_REG_WR_INSTR 0x08
87 #define CQSPI_REG_WR_INSTR_OPCODE_LSB 0
88 #define CQSPI_REG_WR_INSTR_TYPE_ADDR_LSB 12
89 #define CQSPI_REG_WR_INSTR_TYPE_DATA_LSB 16
91 #define CQSPI_REG_DELAY 0x0C
92 #define CQSPI_REG_DELAY_TSLCH_LSB 0
93 #define CQSPI_REG_DELAY_TCHSH_LSB 8
94 #define CQSPI_REG_DELAY_TSD2D_LSB 16
95 #define CQSPI_REG_DELAY_TSHSL_LSB 24
96 #define CQSPI_REG_DELAY_TSLCH_MASK 0xFF
97 #define CQSPI_REG_DELAY_TCHSH_MASK 0xFF
98 #define CQSPI_REG_DELAY_TSD2D_MASK 0xFF
99 #define CQSPI_REG_DELAY_TSHSL_MASK 0xFF
101 #define CQSPI_REG_RD_DATA_CAPTURE 0x10
102 #define CQSPI_REG_RD_DATA_CAPTURE_BYPASS BIT(0)
103 #define CQSPI_REG_RD_DATA_CAPTURE_DELAY_LSB 1
104 #define CQSPI_REG_RD_DATA_CAPTURE_DELAY_MASK 0xF
106 #define CQSPI_REG_SIZE 0x14
107 #define CQSPI_REG_SIZE_ADDRESS_LSB 0
108 #define CQSPI_REG_SIZE_PAGE_LSB 4
109 #define CQSPI_REG_SIZE_BLOCK_LSB 16
110 #define CQSPI_REG_SIZE_ADDRESS_MASK 0xF
111 #define CQSPI_REG_SIZE_PAGE_MASK 0xFFF
112 #define CQSPI_REG_SIZE_BLOCK_MASK 0x3F
114 #define CQSPI_REG_SRAMPARTITION 0x18
115 #define CQSPI_REG_INDIRECTTRIGGER 0x1C
117 #define CQSPI_REG_REMAP 0x24
118 #define CQSPI_REG_MODE_BIT 0x28
120 #define CQSPI_REG_SDRAMLEVEL 0x2C
121 #define CQSPI_REG_SDRAMLEVEL_RD_LSB 0
122 #define CQSPI_REG_SDRAMLEVEL_WR_LSB 16
123 #define CQSPI_REG_SDRAMLEVEL_RD_MASK 0xFFFF
124 #define CQSPI_REG_SDRAMLEVEL_WR_MASK 0xFFFF
126 #define CQSPI_REG_WR_COMPLETION_CTRL 0x38
127 #define CQSPI_REG_WR_DISABLE_AUTO_POLL BIT(14)
129 #define CQSPI_REG_IRQSTATUS 0x40
130 #define CQSPI_REG_IRQMASK 0x44
132 #define CQSPI_REG_INDIRECTRD 0x60
133 #define CQSPI_REG_INDIRECTRD_START BIT(0)
134 #define CQSPI_REG_INDIRECTRD_CANCEL BIT(1)
135 #define CQSPI_REG_INDIRECTRD_INPROGRESS BIT(2)
136 #define CQSPI_REG_INDIRECTRD_DONE BIT(5)
138 #define CQSPI_REG_INDIRECTRDWATERMARK 0x64
139 #define CQSPI_REG_INDIRECTRDSTARTADDR 0x68
140 #define CQSPI_REG_INDIRECTRDBYTES 0x6C
142 #define CQSPI_REG_CMDCTRL 0x90
143 #define CQSPI_REG_CMDCTRL_EXECUTE BIT(0)
144 #define CQSPI_REG_CMDCTRL_INPROGRESS BIT(1)
145 #define CQSPI_REG_CMDCTRL_DUMMY_LSB 7
146 #define CQSPI_REG_CMDCTRL_WR_BYTES_LSB 12
147 #define CQSPI_REG_CMDCTRL_WR_EN_LSB 15
148 #define CQSPI_REG_CMDCTRL_ADD_BYTES_LSB 16
149 #define CQSPI_REG_CMDCTRL_ADDR_EN_LSB 19
150 #define CQSPI_REG_CMDCTRL_RD_BYTES_LSB 20
151 #define CQSPI_REG_CMDCTRL_RD_EN_LSB 23
152 #define CQSPI_REG_CMDCTRL_OPCODE_LSB 24
153 #define CQSPI_REG_CMDCTRL_DUMMY_MASK 0x1F
154 #define CQSPI_REG_CMDCTRL_WR_BYTES_MASK 0x7
155 #define CQSPI_REG_CMDCTRL_ADD_BYTES_MASK 0x3
156 #define CQSPI_REG_CMDCTRL_RD_BYTES_MASK 0x7
157 #define CQSPI_REG_CMDCTRL_OPCODE_MASK 0xFF
159 #define CQSPI_REG_INDIRECTWR 0x70
160 #define CQSPI_REG_INDIRECTWR_START BIT(0)
161 #define CQSPI_REG_INDIRECTWR_CANCEL BIT(1)
162 #define CQSPI_REG_INDIRECTWR_INPROGRESS BIT(2)
163 #define CQSPI_REG_INDIRECTWR_DONE BIT(5)
165 #define CQSPI_REG_INDIRECTWRWATERMARK 0x74
166 #define CQSPI_REG_INDIRECTWRSTARTADDR 0x78
167 #define CQSPI_REG_INDIRECTWRBYTES 0x7C
169 #define CQSPI_REG_CMDADDRESS 0x94
170 #define CQSPI_REG_CMDREADDATALOWER 0xA0
171 #define CQSPI_REG_CMDREADDATAUPPER 0xA4
172 #define CQSPI_REG_CMDWRITEDATALOWER 0xA8
173 #define CQSPI_REG_CMDWRITEDATAUPPER 0xAC
175 #define CQSPI_REG_OP_EXT_LOWER 0xE0
176 #define CQSPI_REG_OP_EXT_READ_LSB 24
177 #define CQSPI_REG_OP_EXT_WRITE_LSB 16
178 #define CQSPI_REG_OP_EXT_STIG_LSB 0
180 #define CQSPI_REG_IS_IDLE(base) \
181 ((readl(base + CQSPI_REG_CONFIG) >> \
182 CQSPI_REG_CONFIG_IDLE_LSB) & 0x1)
184 #define CQSPI_GET_RD_SRAM_LEVEL(reg_base) \
185 (((readl(reg_base + CQSPI_REG_SDRAMLEVEL)) >> \
186 CQSPI_REG_SDRAMLEVEL_RD_LSB) & CQSPI_REG_SDRAMLEVEL_RD_MASK)
188 #define CQSPI_GET_WR_SRAM_LEVEL(reg_base) \
189 (((readl(reg_base + CQSPI_REG_SDRAMLEVEL)) >> \
190 CQSPI_REG_SDRAMLEVEL_WR_LSB) & CQSPI_REG_SDRAMLEVEL_WR_MASK)
192 void cadence_qspi_apb_controller_enable(void *reg_base)
195 reg = readl(reg_base + CQSPI_REG_CONFIG);
196 reg |= CQSPI_REG_CONFIG_ENABLE;
197 writel(reg, reg_base + CQSPI_REG_CONFIG);
200 void cadence_qspi_apb_controller_disable(void *reg_base)
203 reg = readl(reg_base + CQSPI_REG_CONFIG);
204 reg &= ~CQSPI_REG_CONFIG_ENABLE;
205 writel(reg, reg_base + CQSPI_REG_CONFIG);
208 void cadence_qspi_apb_dac_mode_enable(void *reg_base)
212 reg = readl(reg_base + CQSPI_REG_CONFIG);
213 reg |= CQSPI_REG_CONFIG_DIRECT;
214 writel(reg, reg_base + CQSPI_REG_CONFIG);
217 static unsigned int cadence_qspi_calc_dummy(const struct spi_mem_op *op,
220 unsigned int dummy_clk;
222 dummy_clk = op->dummy.nbytes * (8 / op->dummy.buswidth);
229 static u32 cadence_qspi_calc_rdreg(struct cadence_spi_plat *plat)
233 rdreg |= plat->inst_width << CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB;
234 rdreg |= plat->addr_width << CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB;
235 rdreg |= plat->data_width << CQSPI_REG_RD_INSTR_TYPE_DATA_LSB;
240 static int cadence_qspi_buswidth_to_inst_type(u8 buswidth)
245 return CQSPI_INST_TYPE_SINGLE;
248 return CQSPI_INST_TYPE_DUAL;
251 return CQSPI_INST_TYPE_QUAD;
254 return CQSPI_INST_TYPE_OCTAL;
261 static int cadence_qspi_set_protocol(struct cadence_spi_plat *plat,
262 const struct spi_mem_op *op)
266 plat->dtr = op->data.dtr && op->cmd.dtr && op->addr.dtr;
268 ret = cadence_qspi_buswidth_to_inst_type(op->cmd.buswidth);
271 plat->inst_width = ret;
273 ret = cadence_qspi_buswidth_to_inst_type(op->addr.buswidth);
276 plat->addr_width = ret;
278 ret = cadence_qspi_buswidth_to_inst_type(op->data.buswidth);
281 plat->data_width = ret;
286 /* Return 1 if idle, otherwise return 0 (busy). */
287 static unsigned int cadence_qspi_wait_idle(void *reg_base)
289 unsigned int start, count = 0;
290 /* timeout in unit of ms */
291 unsigned int timeout = 5000;
293 start = get_timer(0);
294 for ( ; get_timer(start) < timeout ; ) {
295 if (CQSPI_REG_IS_IDLE(reg_base))
300 * Ensure the QSPI controller is in true idle state after
301 * reading back the same idle status consecutively
303 if (count >= CQSPI_POLL_IDLE_RETRY)
307 /* Timeout, still in busy mode. */
308 printf("QSPI: QSPI is still busy after poll for %d times.\n",
313 void cadence_qspi_apb_readdata_capture(void *reg_base,
314 unsigned int bypass, unsigned int delay)
317 cadence_qspi_apb_controller_disable(reg_base);
319 reg = readl(reg_base + CQSPI_REG_RD_DATA_CAPTURE);
322 reg |= CQSPI_REG_RD_DATA_CAPTURE_BYPASS;
324 reg &= ~CQSPI_REG_RD_DATA_CAPTURE_BYPASS;
326 reg &= ~(CQSPI_REG_RD_DATA_CAPTURE_DELAY_MASK
327 << CQSPI_REG_RD_DATA_CAPTURE_DELAY_LSB);
329 reg |= (delay & CQSPI_REG_RD_DATA_CAPTURE_DELAY_MASK)
330 << CQSPI_REG_RD_DATA_CAPTURE_DELAY_LSB;
332 writel(reg, reg_base + CQSPI_REG_RD_DATA_CAPTURE);
334 cadence_qspi_apb_controller_enable(reg_base);
337 void cadence_qspi_apb_config_baudrate_div(void *reg_base,
338 unsigned int ref_clk_hz, unsigned int sclk_hz)
343 cadence_qspi_apb_controller_disable(reg_base);
344 reg = readl(reg_base + CQSPI_REG_CONFIG);
345 reg &= ~(CQSPI_REG_CONFIG_BAUD_MASK << CQSPI_REG_CONFIG_BAUD_LSB);
348 * The baud_div field in the config reg is 4 bits, and the ref clock is
349 * divided by 2 * (baud_div + 1). Round up the divider to ensure the
350 * SPI clock rate is less than or equal to the requested clock rate.
352 div = DIV_ROUND_UP(ref_clk_hz, sclk_hz * 2) - 1;
354 /* ensure the baud rate doesn't exceed the max value */
355 if (div > CQSPI_REG_CONFIG_BAUD_MASK)
356 div = CQSPI_REG_CONFIG_BAUD_MASK;
358 debug("%s: ref_clk %dHz sclk %dHz Div 0x%x, actual %dHz\n", __func__,
359 ref_clk_hz, sclk_hz, div, ref_clk_hz / (2 * (div + 1)));
361 reg |= (div << CQSPI_REG_CONFIG_BAUD_LSB);
362 writel(reg, reg_base + CQSPI_REG_CONFIG);
364 cadence_qspi_apb_controller_enable(reg_base);
367 void cadence_qspi_apb_set_clk_mode(void *reg_base, uint mode)
371 cadence_qspi_apb_controller_disable(reg_base);
372 reg = readl(reg_base + CQSPI_REG_CONFIG);
373 reg &= ~(CQSPI_REG_CONFIG_CLK_POL | CQSPI_REG_CONFIG_CLK_PHA);
376 reg |= CQSPI_REG_CONFIG_CLK_POL;
378 reg |= CQSPI_REG_CONFIG_CLK_PHA;
380 writel(reg, reg_base + CQSPI_REG_CONFIG);
382 cadence_qspi_apb_controller_enable(reg_base);
385 void cadence_qspi_apb_chipselect(void *reg_base,
386 unsigned int chip_select, unsigned int decoder_enable)
390 cadence_qspi_apb_controller_disable(reg_base);
392 debug("%s : chipselect %d decode %d\n", __func__, chip_select,
395 reg = readl(reg_base + CQSPI_REG_CONFIG);
397 if (decoder_enable) {
398 reg |= CQSPI_REG_CONFIG_DECODE;
400 reg &= ~CQSPI_REG_CONFIG_DECODE;
401 /* Convert CS if without decoder.
407 chip_select = 0xF & ~(1 << chip_select);
410 reg &= ~(CQSPI_REG_CONFIG_CHIPSELECT_MASK
411 << CQSPI_REG_CONFIG_CHIPSELECT_LSB);
412 reg |= (chip_select & CQSPI_REG_CONFIG_CHIPSELECT_MASK)
413 << CQSPI_REG_CONFIG_CHIPSELECT_LSB;
414 writel(reg, reg_base + CQSPI_REG_CONFIG);
416 cadence_qspi_apb_controller_enable(reg_base);
419 void cadence_qspi_apb_delay(void *reg_base,
420 unsigned int ref_clk, unsigned int sclk_hz,
421 unsigned int tshsl_ns, unsigned int tsd2d_ns,
422 unsigned int tchsh_ns, unsigned int tslch_ns)
424 unsigned int ref_clk_ns;
425 unsigned int sclk_ns;
426 unsigned int tshsl, tchsh, tslch, tsd2d;
429 cadence_qspi_apb_controller_disable(reg_base);
432 ref_clk_ns = DIV_ROUND_UP(1000000000, ref_clk);
435 sclk_ns = DIV_ROUND_UP(1000000000, sclk_hz);
437 /* The controller adds additional delay to that programmed in the reg */
438 if (tshsl_ns >= sclk_ns + ref_clk_ns)
439 tshsl_ns -= sclk_ns + ref_clk_ns;
440 if (tchsh_ns >= sclk_ns + 3 * ref_clk_ns)
441 tchsh_ns -= sclk_ns + 3 * ref_clk_ns;
442 tshsl = DIV_ROUND_UP(tshsl_ns, ref_clk_ns);
443 tchsh = DIV_ROUND_UP(tchsh_ns, ref_clk_ns);
444 tslch = DIV_ROUND_UP(tslch_ns, ref_clk_ns);
445 tsd2d = DIV_ROUND_UP(tsd2d_ns, ref_clk_ns);
447 reg = ((tshsl & CQSPI_REG_DELAY_TSHSL_MASK)
448 << CQSPI_REG_DELAY_TSHSL_LSB);
449 reg |= ((tchsh & CQSPI_REG_DELAY_TCHSH_MASK)
450 << CQSPI_REG_DELAY_TCHSH_LSB);
451 reg |= ((tslch & CQSPI_REG_DELAY_TSLCH_MASK)
452 << CQSPI_REG_DELAY_TSLCH_LSB);
453 reg |= ((tsd2d & CQSPI_REG_DELAY_TSD2D_MASK)
454 << CQSPI_REG_DELAY_TSD2D_LSB);
455 writel(reg, reg_base + CQSPI_REG_DELAY);
457 cadence_qspi_apb_controller_enable(reg_base);
460 void cadence_qspi_apb_controller_init(struct cadence_spi_plat *plat)
464 cadence_qspi_apb_controller_disable(plat->regbase);
466 /* Configure the device size and address bytes */
467 reg = readl(plat->regbase + CQSPI_REG_SIZE);
468 /* Clear the previous value */
469 reg &= ~(CQSPI_REG_SIZE_PAGE_MASK << CQSPI_REG_SIZE_PAGE_LSB);
470 reg &= ~(CQSPI_REG_SIZE_BLOCK_MASK << CQSPI_REG_SIZE_BLOCK_LSB);
471 reg |= (plat->page_size << CQSPI_REG_SIZE_PAGE_LSB);
472 reg |= (plat->block_size << CQSPI_REG_SIZE_BLOCK_LSB);
473 writel(reg, plat->regbase + CQSPI_REG_SIZE);
475 /* Configure the remap address register, no remap */
476 writel(0, plat->regbase + CQSPI_REG_REMAP);
478 /* Indirect mode configurations */
479 writel(plat->fifo_depth / 2, plat->regbase + CQSPI_REG_SRAMPARTITION);
481 /* Disable all interrupts */
482 writel(0, plat->regbase + CQSPI_REG_IRQMASK);
484 cadence_qspi_apb_controller_enable(plat->regbase);
487 static int cadence_qspi_apb_exec_flash_cmd(void *reg_base,
490 unsigned int retry = CQSPI_REG_RETRY;
492 /* Write the CMDCTRL without start execution. */
493 writel(reg, reg_base + CQSPI_REG_CMDCTRL);
495 reg |= CQSPI_REG_CMDCTRL_EXECUTE;
496 writel(reg, reg_base + CQSPI_REG_CMDCTRL);
499 reg = readl(reg_base + CQSPI_REG_CMDCTRL);
500 if ((reg & CQSPI_REG_CMDCTRL_INPROGRESS) == 0)
506 printf("QSPI: flash command execution timeout\n");
510 /* Polling QSPI idle status. */
511 if (!cadence_qspi_wait_idle(reg_base))
517 static int cadence_qspi_setup_opcode_ext(struct cadence_spi_plat *plat,
518 const struct spi_mem_op *op,
524 if (op->cmd.nbytes != 2)
527 /* Opcode extension is the LSB. */
528 ext = op->cmd.opcode & 0xff;
530 reg = readl(plat->regbase + CQSPI_REG_OP_EXT_LOWER);
531 reg &= ~(0xff << shift);
533 writel(reg, plat->regbase + CQSPI_REG_OP_EXT_LOWER);
538 static int cadence_qspi_enable_dtr(struct cadence_spi_plat *plat,
539 const struct spi_mem_op *op,
546 reg = readl(plat->regbase + CQSPI_REG_CONFIG);
549 reg |= CQSPI_REG_CONFIG_DTR_PROTO;
550 reg |= CQSPI_REG_CONFIG_DUAL_OPCODE;
552 /* Set up command opcode extension. */
553 ret = cadence_qspi_setup_opcode_ext(plat, op, shift);
557 reg &= ~CQSPI_REG_CONFIG_DTR_PROTO;
558 reg &= ~CQSPI_REG_CONFIG_DUAL_OPCODE;
561 writel(reg, plat->regbase + CQSPI_REG_CONFIG);
566 int cadence_qspi_apb_command_read_setup(struct cadence_spi_plat *plat,
567 const struct spi_mem_op *op)
572 ret = cadence_qspi_set_protocol(plat, op);
576 ret = cadence_qspi_enable_dtr(plat, op, CQSPI_REG_OP_EXT_STIG_LSB,
581 reg = cadence_qspi_calc_rdreg(plat);
582 writel(reg, plat->regbase + CQSPI_REG_RD_INSTR);
587 /* For command RDID, RDSR. */
588 int cadence_qspi_apb_command_read(struct cadence_spi_plat *plat,
589 const struct spi_mem_op *op)
591 void *reg_base = plat->regbase;
593 unsigned int read_len;
595 unsigned int rxlen = op->data.nbytes;
596 void *rxbuf = op->data.buf.in;
597 unsigned int dummy_clk;
600 if (rxlen > CQSPI_STIG_DATA_LEN_MAX || !rxbuf) {
601 printf("QSPI: Invalid input arguments rxlen %u\n", rxlen);
606 opcode = op->cmd.opcode >> 8;
608 opcode = op->cmd.opcode;
610 reg = opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB;
612 /* Set up dummy cycles. */
613 dummy_clk = cadence_qspi_calc_dummy(op, plat->dtr);
614 if (dummy_clk > CQSPI_DUMMY_CLKS_MAX)
618 reg |= (dummy_clk & CQSPI_REG_CMDCTRL_DUMMY_MASK)
619 << CQSPI_REG_CMDCTRL_DUMMY_LSB;
621 reg |= (0x1 << CQSPI_REG_CMDCTRL_RD_EN_LSB);
623 /* 0 means 1 byte. */
624 reg |= (((rxlen - 1) & CQSPI_REG_CMDCTRL_RD_BYTES_MASK)
625 << CQSPI_REG_CMDCTRL_RD_BYTES_LSB);
626 status = cadence_qspi_apb_exec_flash_cmd(reg_base, reg);
630 reg = readl(reg_base + CQSPI_REG_CMDREADDATALOWER);
632 /* Put the read value into rx_buf */
633 read_len = (rxlen > 4) ? 4 : rxlen;
634 memcpy(rxbuf, ®, read_len);
638 reg = readl(reg_base + CQSPI_REG_CMDREADDATAUPPER);
640 read_len = rxlen - read_len;
641 memcpy(rxbuf, ®, read_len);
646 int cadence_qspi_apb_command_write_setup(struct cadence_spi_plat *plat,
647 const struct spi_mem_op *op)
652 ret = cadence_qspi_set_protocol(plat, op);
656 ret = cadence_qspi_enable_dtr(plat, op, CQSPI_REG_OP_EXT_STIG_LSB,
661 reg = cadence_qspi_calc_rdreg(plat);
662 writel(reg, plat->regbase + CQSPI_REG_RD_INSTR);
667 /* For commands: WRSR, WREN, WRDI, CHIP_ERASE, BE, etc. */
668 int cadence_qspi_apb_command_write(struct cadence_spi_plat *plat,
669 const struct spi_mem_op *op)
671 unsigned int reg = 0;
672 unsigned int wr_data;
674 unsigned int txlen = op->data.nbytes;
675 const void *txbuf = op->data.buf.out;
676 void *reg_base = plat->regbase;
680 /* Reorder address to SPI bus order if only transferring address */
682 addr = cpu_to_be32(op->addr.val);
683 if (op->addr.nbytes == 3)
686 txlen = op->addr.nbytes;
689 if (txlen > CQSPI_STIG_DATA_LEN_MAX) {
690 printf("QSPI: Invalid input arguments txlen %u\n", txlen);
695 opcode = op->cmd.opcode >> 8;
697 opcode = op->cmd.opcode;
699 reg |= opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB;
702 /* writing data = yes */
703 reg |= (0x1 << CQSPI_REG_CMDCTRL_WR_EN_LSB);
704 reg |= ((txlen - 1) & CQSPI_REG_CMDCTRL_WR_BYTES_MASK)
705 << CQSPI_REG_CMDCTRL_WR_BYTES_LSB;
707 wr_len = txlen > 4 ? 4 : txlen;
708 memcpy(&wr_data, txbuf, wr_len);
709 writel(wr_data, reg_base +
710 CQSPI_REG_CMDWRITEDATALOWER);
714 wr_len = txlen - wr_len;
715 memcpy(&wr_data, txbuf, wr_len);
716 writel(wr_data, reg_base +
717 CQSPI_REG_CMDWRITEDATAUPPER);
721 /* Execute the command */
722 return cadence_qspi_apb_exec_flash_cmd(reg_base, reg);
725 /* Opcode + Address (3/4 bytes) + dummy bytes (0-4 bytes) */
726 int cadence_qspi_apb_read_setup(struct cadence_spi_plat *plat,
727 const struct spi_mem_op *op)
731 unsigned int dummy_clk;
732 unsigned int dummy_bytes = op->dummy.nbytes;
736 ret = cadence_qspi_set_protocol(plat, op);
740 ret = cadence_qspi_enable_dtr(plat, op, CQSPI_REG_OP_EXT_READ_LSB,
745 /* Setup the indirect trigger address */
746 writel(plat->trigger_address,
747 plat->regbase + CQSPI_REG_INDIRECTTRIGGER);
749 /* Configure the opcode */
751 opcode = op->cmd.opcode >> 8;
753 opcode = op->cmd.opcode;
755 rd_reg = opcode << CQSPI_REG_RD_INSTR_OPCODE_LSB;
756 rd_reg |= cadence_qspi_calc_rdreg(plat);
758 writel(op->addr.val, plat->regbase + CQSPI_REG_INDIRECTRDSTARTADDR);
761 /* Convert to clock cycles. */
762 dummy_clk = cadence_qspi_calc_dummy(op, plat->dtr);
764 if (dummy_clk > CQSPI_DUMMY_CLKS_MAX)
768 rd_reg |= (dummy_clk & CQSPI_REG_RD_INSTR_DUMMY_MASK)
769 << CQSPI_REG_RD_INSTR_DUMMY_LSB;
772 writel(rd_reg, plat->regbase + CQSPI_REG_RD_INSTR);
774 /* set device size */
775 reg = readl(plat->regbase + CQSPI_REG_SIZE);
776 reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
777 reg |= (op->addr.nbytes - 1);
778 writel(reg, plat->regbase + CQSPI_REG_SIZE);
782 static u32 cadence_qspi_get_rd_sram_level(struct cadence_spi_plat *plat)
784 u32 reg = readl(plat->regbase + CQSPI_REG_SDRAMLEVEL);
785 reg >>= CQSPI_REG_SDRAMLEVEL_RD_LSB;
786 return reg & CQSPI_REG_SDRAMLEVEL_RD_MASK;
789 static int cadence_qspi_wait_for_data(struct cadence_spi_plat *plat)
791 unsigned int timeout = 10000;
795 reg = cadence_qspi_get_rd_sram_level(plat);
805 cadence_qspi_apb_indirect_read_execute(struct cadence_spi_plat *plat,
806 unsigned int n_rx, u8 *rxbuf)
808 unsigned int remaining = n_rx;
809 unsigned int bytes_to_read = 0;
812 writel(n_rx, plat->regbase + CQSPI_REG_INDIRECTRDBYTES);
814 /* Start the indirect read transfer */
815 writel(CQSPI_REG_INDIRECTRD_START,
816 plat->regbase + CQSPI_REG_INDIRECTRD);
818 while (remaining > 0) {
819 ret = cadence_qspi_wait_for_data(plat);
821 printf("Indirect write timed out (%i)\n", ret);
827 while (bytes_to_read != 0) {
828 bytes_to_read *= plat->fifo_width;
829 bytes_to_read = bytes_to_read > remaining ?
830 remaining : bytes_to_read;
832 * Handle non-4-byte aligned access to avoid
835 if (((uintptr_t)rxbuf % 4) || (bytes_to_read % 4))
836 readsb(plat->ahbbase, rxbuf, bytes_to_read);
838 readsl(plat->ahbbase, rxbuf,
840 rxbuf += bytes_to_read;
841 remaining -= bytes_to_read;
842 bytes_to_read = cadence_qspi_get_rd_sram_level(plat);
846 /* Check indirect done status */
847 ret = wait_for_bit_le32(plat->regbase + CQSPI_REG_INDIRECTRD,
848 CQSPI_REG_INDIRECTRD_DONE, 1, 10, 0);
850 printf("Indirect read completion error (%i)\n", ret);
854 /* Clear indirect completion status */
855 writel(CQSPI_REG_INDIRECTRD_DONE,
856 plat->regbase + CQSPI_REG_INDIRECTRD);
861 /* Cancel the indirect read */
862 writel(CQSPI_REG_INDIRECTRD_CANCEL,
863 plat->regbase + CQSPI_REG_INDIRECTRD);
867 int cadence_qspi_apb_read_execute(struct cadence_spi_plat *plat,
868 const struct spi_mem_op *op)
870 u64 from = op->addr.val;
871 void *buf = op->data.buf.in;
872 size_t len = op->data.nbytes;
874 if (plat->use_dac_mode && (from + len < plat->ahbsize)) {
876 dma_memcpy(buf, plat->ahbbase + from, len) < 0) {
877 memcpy_fromio(buf, plat->ahbbase + from, len);
879 if (!cadence_qspi_wait_idle(plat->regbase))
884 return cadence_qspi_apb_indirect_read_execute(plat, len, buf);
887 /* Opcode + Address (3/4 bytes) */
888 int cadence_qspi_apb_write_setup(struct cadence_spi_plat *plat,
889 const struct spi_mem_op *op)
895 ret = cadence_qspi_set_protocol(plat, op);
899 ret = cadence_qspi_enable_dtr(plat, op, CQSPI_REG_OP_EXT_WRITE_LSB,
904 /* Setup the indirect trigger address */
905 writel(plat->trigger_address,
906 plat->regbase + CQSPI_REG_INDIRECTTRIGGER);
908 /* Configure the opcode */
910 opcode = op->cmd.opcode >> 8;
912 opcode = op->cmd.opcode;
914 reg = opcode << CQSPI_REG_WR_INSTR_OPCODE_LSB;
915 reg |= plat->data_width << CQSPI_REG_WR_INSTR_TYPE_DATA_LSB;
916 reg |= plat->addr_width << CQSPI_REG_WR_INSTR_TYPE_ADDR_LSB;
917 writel(reg, plat->regbase + CQSPI_REG_WR_INSTR);
919 reg = cadence_qspi_calc_rdreg(plat);
920 writel(reg, plat->regbase + CQSPI_REG_RD_INSTR);
922 writel(op->addr.val, plat->regbase + CQSPI_REG_INDIRECTWRSTARTADDR);
926 * Some flashes like the cypress Semper flash expect a 4-byte
927 * dummy address with the Read SR command in DTR mode, but this
928 * controller does not support sending address with the Read SR
929 * command. So, disable write completion polling on the
930 * controller's side. spi-nor will take care of polling the
933 reg = readl(plat->regbase + CQSPI_REG_WR_COMPLETION_CTRL);
934 reg |= CQSPI_REG_WR_DISABLE_AUTO_POLL;
935 writel(reg, plat->regbase + CQSPI_REG_WR_COMPLETION_CTRL);
938 reg = readl(plat->regbase + CQSPI_REG_SIZE);
939 reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
940 reg |= (op->addr.nbytes - 1);
941 writel(reg, plat->regbase + CQSPI_REG_SIZE);
946 cadence_qspi_apb_indirect_write_execute(struct cadence_spi_plat *plat,
947 unsigned int n_tx, const u8 *txbuf)
949 unsigned int page_size = plat->page_size;
950 unsigned int remaining = n_tx;
951 const u8 *bb_txbuf = txbuf;
952 void *bounce_buf = NULL;
953 unsigned int write_bytes;
957 * Use bounce buffer for non 32 bit aligned txbuf to avoid data
960 if ((uintptr_t)txbuf % 4) {
961 bounce_buf = malloc(n_tx);
964 memcpy(bounce_buf, txbuf, n_tx);
965 bb_txbuf = bounce_buf;
968 /* Configure the indirect read transfer bytes */
969 writel(n_tx, plat->regbase + CQSPI_REG_INDIRECTWRBYTES);
971 /* Start the indirect write transfer */
972 writel(CQSPI_REG_INDIRECTWR_START,
973 plat->regbase + CQSPI_REG_INDIRECTWR);
976 * Some delay is required for the above bit to be internally
977 * synchronized by the QSPI module.
979 ndelay(plat->wr_delay);
981 while (remaining > 0) {
982 write_bytes = remaining > page_size ? page_size : remaining;
983 writesl(plat->ahbbase, bb_txbuf, write_bytes >> 2);
985 writesb(plat->ahbbase,
986 bb_txbuf + rounddown(write_bytes, 4),
989 ret = wait_for_bit_le32(plat->regbase + CQSPI_REG_SDRAMLEVEL,
990 CQSPI_REG_SDRAMLEVEL_WR_MASK <<
991 CQSPI_REG_SDRAMLEVEL_WR_LSB, 0, 10, 0);
993 printf("Indirect write timed out (%i)\n", ret);
997 bb_txbuf += write_bytes;
998 remaining -= write_bytes;
1001 /* Check indirect done status */
1002 ret = wait_for_bit_le32(plat->regbase + CQSPI_REG_INDIRECTWR,
1003 CQSPI_REG_INDIRECTWR_DONE, 1, 10, 0);
1005 printf("Indirect write completion error (%i)\n", ret);
1009 /* Clear indirect completion status */
1010 writel(CQSPI_REG_INDIRECTWR_DONE,
1011 plat->regbase + CQSPI_REG_INDIRECTWR);
1017 /* Cancel the indirect write */
1018 writel(CQSPI_REG_INDIRECTWR_CANCEL,
1019 plat->regbase + CQSPI_REG_INDIRECTWR);
1025 int cadence_qspi_apb_write_execute(struct cadence_spi_plat *plat,
1026 const struct spi_mem_op *op)
1028 u32 to = op->addr.val;
1029 const void *buf = op->data.buf.out;
1030 size_t len = op->data.nbytes;
1033 * Some flashes like the Cypress Semper flash expect a dummy 4-byte
1034 * address (all 0s) with the read status register command in DTR mode.
1035 * But this controller does not support sending dummy address bytes to
1036 * the flash when it is polling the write completion register in DTR
1037 * mode. So, we can not use direct mode when in DTR mode for writing
1040 if (!plat->dtr && plat->use_dac_mode && (to + len < plat->ahbsize)) {
1041 memcpy_toio(plat->ahbbase + to, buf, len);
1042 if (!cadence_qspi_wait_idle(plat->regbase))
1047 return cadence_qspi_apb_indirect_write_execute(plat, len, buf);
1050 void cadence_qspi_apb_enter_xip(void *reg_base, char xip_dummy)
1054 /* enter XiP mode immediately and enable direct mode */
1055 reg = readl(reg_base + CQSPI_REG_CONFIG);
1056 reg |= CQSPI_REG_CONFIG_ENABLE;
1057 reg |= CQSPI_REG_CONFIG_DIRECT;
1058 reg |= CQSPI_REG_CONFIG_XIP_IMM;
1059 writel(reg, reg_base + CQSPI_REG_CONFIG);
1061 /* keep the XiP mode */
1062 writel(xip_dummy, reg_base + CQSPI_REG_MODE_BIT);
1064 /* Enable mode bit at devrd */
1065 reg = readl(reg_base + CQSPI_REG_RD_INSTR);
1066 reg |= (1 << CQSPI_REG_RD_INSTR_MODE_EN_LSB);
1067 writel(reg, reg_base + CQSPI_REG_RD_INSTR);