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_BYTES_MAX 4
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_IDLE_LSB 31
69 #define CQSPI_REG_CONFIG_CHIPSELECT_MASK 0xF
70 #define CQSPI_REG_CONFIG_BAUD_MASK 0xF
72 #define CQSPI_REG_RD_INSTR 0x04
73 #define CQSPI_REG_RD_INSTR_OPCODE_LSB 0
74 #define CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB 8
75 #define CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB 12
76 #define CQSPI_REG_RD_INSTR_TYPE_DATA_LSB 16
77 #define CQSPI_REG_RD_INSTR_MODE_EN_LSB 20
78 #define CQSPI_REG_RD_INSTR_DUMMY_LSB 24
79 #define CQSPI_REG_RD_INSTR_TYPE_INSTR_MASK 0x3
80 #define CQSPI_REG_RD_INSTR_TYPE_ADDR_MASK 0x3
81 #define CQSPI_REG_RD_INSTR_TYPE_DATA_MASK 0x3
82 #define CQSPI_REG_RD_INSTR_DUMMY_MASK 0x1F
84 #define CQSPI_REG_WR_INSTR 0x08
85 #define CQSPI_REG_WR_INSTR_OPCODE_LSB 0
86 #define CQSPI_REG_WR_INSTR_TYPE_DATA_LSB 16
88 #define CQSPI_REG_DELAY 0x0C
89 #define CQSPI_REG_DELAY_TSLCH_LSB 0
90 #define CQSPI_REG_DELAY_TCHSH_LSB 8
91 #define CQSPI_REG_DELAY_TSD2D_LSB 16
92 #define CQSPI_REG_DELAY_TSHSL_LSB 24
93 #define CQSPI_REG_DELAY_TSLCH_MASK 0xFF
94 #define CQSPI_REG_DELAY_TCHSH_MASK 0xFF
95 #define CQSPI_REG_DELAY_TSD2D_MASK 0xFF
96 #define CQSPI_REG_DELAY_TSHSL_MASK 0xFF
98 #define CQSPI_REG_RD_DATA_CAPTURE 0x10
99 #define CQSPI_REG_RD_DATA_CAPTURE_BYPASS BIT(0)
100 #define CQSPI_REG_RD_DATA_CAPTURE_DELAY_LSB 1
101 #define CQSPI_REG_RD_DATA_CAPTURE_DELAY_MASK 0xF
103 #define CQSPI_REG_SIZE 0x14
104 #define CQSPI_REG_SIZE_ADDRESS_LSB 0
105 #define CQSPI_REG_SIZE_PAGE_LSB 4
106 #define CQSPI_REG_SIZE_BLOCK_LSB 16
107 #define CQSPI_REG_SIZE_ADDRESS_MASK 0xF
108 #define CQSPI_REG_SIZE_PAGE_MASK 0xFFF
109 #define CQSPI_REG_SIZE_BLOCK_MASK 0x3F
111 #define CQSPI_REG_SRAMPARTITION 0x18
112 #define CQSPI_REG_INDIRECTTRIGGER 0x1C
114 #define CQSPI_REG_REMAP 0x24
115 #define CQSPI_REG_MODE_BIT 0x28
117 #define CQSPI_REG_SDRAMLEVEL 0x2C
118 #define CQSPI_REG_SDRAMLEVEL_RD_LSB 0
119 #define CQSPI_REG_SDRAMLEVEL_WR_LSB 16
120 #define CQSPI_REG_SDRAMLEVEL_RD_MASK 0xFFFF
121 #define CQSPI_REG_SDRAMLEVEL_WR_MASK 0xFFFF
123 #define CQSPI_REG_IRQSTATUS 0x40
124 #define CQSPI_REG_IRQMASK 0x44
126 #define CQSPI_REG_INDIRECTRD 0x60
127 #define CQSPI_REG_INDIRECTRD_START BIT(0)
128 #define CQSPI_REG_INDIRECTRD_CANCEL BIT(1)
129 #define CQSPI_REG_INDIRECTRD_INPROGRESS BIT(2)
130 #define CQSPI_REG_INDIRECTRD_DONE BIT(5)
132 #define CQSPI_REG_INDIRECTRDWATERMARK 0x64
133 #define CQSPI_REG_INDIRECTRDSTARTADDR 0x68
134 #define CQSPI_REG_INDIRECTRDBYTES 0x6C
136 #define CQSPI_REG_CMDCTRL 0x90
137 #define CQSPI_REG_CMDCTRL_EXECUTE BIT(0)
138 #define CQSPI_REG_CMDCTRL_INPROGRESS BIT(1)
139 #define CQSPI_REG_CMDCTRL_DUMMY_LSB 7
140 #define CQSPI_REG_CMDCTRL_WR_BYTES_LSB 12
141 #define CQSPI_REG_CMDCTRL_WR_EN_LSB 15
142 #define CQSPI_REG_CMDCTRL_ADD_BYTES_LSB 16
143 #define CQSPI_REG_CMDCTRL_ADDR_EN_LSB 19
144 #define CQSPI_REG_CMDCTRL_RD_BYTES_LSB 20
145 #define CQSPI_REG_CMDCTRL_RD_EN_LSB 23
146 #define CQSPI_REG_CMDCTRL_OPCODE_LSB 24
147 #define CQSPI_REG_CMDCTRL_DUMMY_MASK 0x1F
148 #define CQSPI_REG_CMDCTRL_WR_BYTES_MASK 0x7
149 #define CQSPI_REG_CMDCTRL_ADD_BYTES_MASK 0x3
150 #define CQSPI_REG_CMDCTRL_RD_BYTES_MASK 0x7
151 #define CQSPI_REG_CMDCTRL_OPCODE_MASK 0xFF
153 #define CQSPI_REG_INDIRECTWR 0x70
154 #define CQSPI_REG_INDIRECTWR_START BIT(0)
155 #define CQSPI_REG_INDIRECTWR_CANCEL BIT(1)
156 #define CQSPI_REG_INDIRECTWR_INPROGRESS BIT(2)
157 #define CQSPI_REG_INDIRECTWR_DONE BIT(5)
159 #define CQSPI_REG_INDIRECTWRWATERMARK 0x74
160 #define CQSPI_REG_INDIRECTWRSTARTADDR 0x78
161 #define CQSPI_REG_INDIRECTWRBYTES 0x7C
163 #define CQSPI_REG_CMDADDRESS 0x94
164 #define CQSPI_REG_CMDREADDATALOWER 0xA0
165 #define CQSPI_REG_CMDREADDATAUPPER 0xA4
166 #define CQSPI_REG_CMDWRITEDATALOWER 0xA8
167 #define CQSPI_REG_CMDWRITEDATAUPPER 0xAC
169 #define CQSPI_REG_IS_IDLE(base) \
170 ((readl(base + CQSPI_REG_CONFIG) >> \
171 CQSPI_REG_CONFIG_IDLE_LSB) & 0x1)
173 #define CQSPI_GET_RD_SRAM_LEVEL(reg_base) \
174 (((readl(reg_base + CQSPI_REG_SDRAMLEVEL)) >> \
175 CQSPI_REG_SDRAMLEVEL_RD_LSB) & CQSPI_REG_SDRAMLEVEL_RD_MASK)
177 #define CQSPI_GET_WR_SRAM_LEVEL(reg_base) \
178 (((readl(reg_base + CQSPI_REG_SDRAMLEVEL)) >> \
179 CQSPI_REG_SDRAMLEVEL_WR_LSB) & CQSPI_REG_SDRAMLEVEL_WR_MASK)
181 void cadence_qspi_apb_controller_enable(void *reg_base)
184 reg = readl(reg_base + CQSPI_REG_CONFIG);
185 reg |= CQSPI_REG_CONFIG_ENABLE;
186 writel(reg, reg_base + CQSPI_REG_CONFIG);
189 void cadence_qspi_apb_controller_disable(void *reg_base)
192 reg = readl(reg_base + CQSPI_REG_CONFIG);
193 reg &= ~CQSPI_REG_CONFIG_ENABLE;
194 writel(reg, reg_base + CQSPI_REG_CONFIG);
197 void cadence_qspi_apb_dac_mode_enable(void *reg_base)
201 reg = readl(reg_base + CQSPI_REG_CONFIG);
202 reg |= CQSPI_REG_CONFIG_DIRECT;
203 writel(reg, reg_base + CQSPI_REG_CONFIG);
206 /* Return 1 if idle, otherwise return 0 (busy). */
207 static unsigned int cadence_qspi_wait_idle(void *reg_base)
209 unsigned int start, count = 0;
210 /* timeout in unit of ms */
211 unsigned int timeout = 5000;
213 start = get_timer(0);
214 for ( ; get_timer(start) < timeout ; ) {
215 if (CQSPI_REG_IS_IDLE(reg_base))
220 * Ensure the QSPI controller is in true idle state after
221 * reading back the same idle status consecutively
223 if (count >= CQSPI_POLL_IDLE_RETRY)
227 /* Timeout, still in busy mode. */
228 printf("QSPI: QSPI is still busy after poll for %d times.\n",
233 void cadence_qspi_apb_readdata_capture(void *reg_base,
234 unsigned int bypass, unsigned int delay)
237 cadence_qspi_apb_controller_disable(reg_base);
239 reg = readl(reg_base + CQSPI_REG_RD_DATA_CAPTURE);
242 reg |= CQSPI_REG_RD_DATA_CAPTURE_BYPASS;
244 reg &= ~CQSPI_REG_RD_DATA_CAPTURE_BYPASS;
246 reg &= ~(CQSPI_REG_RD_DATA_CAPTURE_DELAY_MASK
247 << CQSPI_REG_RD_DATA_CAPTURE_DELAY_LSB);
249 reg |= (delay & CQSPI_REG_RD_DATA_CAPTURE_DELAY_MASK)
250 << CQSPI_REG_RD_DATA_CAPTURE_DELAY_LSB;
252 writel(reg, reg_base + CQSPI_REG_RD_DATA_CAPTURE);
254 cadence_qspi_apb_controller_enable(reg_base);
257 void cadence_qspi_apb_config_baudrate_div(void *reg_base,
258 unsigned int ref_clk_hz, unsigned int sclk_hz)
263 cadence_qspi_apb_controller_disable(reg_base);
264 reg = readl(reg_base + CQSPI_REG_CONFIG);
265 reg &= ~(CQSPI_REG_CONFIG_BAUD_MASK << CQSPI_REG_CONFIG_BAUD_LSB);
268 * The baud_div field in the config reg is 4 bits, and the ref clock is
269 * divided by 2 * (baud_div + 1). Round up the divider to ensure the
270 * SPI clock rate is less than or equal to the requested clock rate.
272 div = DIV_ROUND_UP(ref_clk_hz, sclk_hz * 2) - 1;
274 /* ensure the baud rate doesn't exceed the max value */
275 if (div > CQSPI_REG_CONFIG_BAUD_MASK)
276 div = CQSPI_REG_CONFIG_BAUD_MASK;
278 debug("%s: ref_clk %dHz sclk %dHz Div 0x%x, actual %dHz\n", __func__,
279 ref_clk_hz, sclk_hz, div, ref_clk_hz / (2 * (div + 1)));
281 reg |= (div << CQSPI_REG_CONFIG_BAUD_LSB);
282 writel(reg, reg_base + CQSPI_REG_CONFIG);
284 cadence_qspi_apb_controller_enable(reg_base);
287 void cadence_qspi_apb_set_clk_mode(void *reg_base, uint mode)
291 cadence_qspi_apb_controller_disable(reg_base);
292 reg = readl(reg_base + CQSPI_REG_CONFIG);
293 reg &= ~(CQSPI_REG_CONFIG_CLK_POL | CQSPI_REG_CONFIG_CLK_PHA);
296 reg |= CQSPI_REG_CONFIG_CLK_POL;
298 reg |= CQSPI_REG_CONFIG_CLK_PHA;
300 writel(reg, reg_base + CQSPI_REG_CONFIG);
302 cadence_qspi_apb_controller_enable(reg_base);
305 void cadence_qspi_apb_chipselect(void *reg_base,
306 unsigned int chip_select, unsigned int decoder_enable)
310 cadence_qspi_apb_controller_disable(reg_base);
312 debug("%s : chipselect %d decode %d\n", __func__, chip_select,
315 reg = readl(reg_base + CQSPI_REG_CONFIG);
317 if (decoder_enable) {
318 reg |= CQSPI_REG_CONFIG_DECODE;
320 reg &= ~CQSPI_REG_CONFIG_DECODE;
321 /* Convert CS if without decoder.
327 chip_select = 0xF & ~(1 << chip_select);
330 reg &= ~(CQSPI_REG_CONFIG_CHIPSELECT_MASK
331 << CQSPI_REG_CONFIG_CHIPSELECT_LSB);
332 reg |= (chip_select & CQSPI_REG_CONFIG_CHIPSELECT_MASK)
333 << CQSPI_REG_CONFIG_CHIPSELECT_LSB;
334 writel(reg, reg_base + CQSPI_REG_CONFIG);
336 cadence_qspi_apb_controller_enable(reg_base);
339 void cadence_qspi_apb_delay(void *reg_base,
340 unsigned int ref_clk, unsigned int sclk_hz,
341 unsigned int tshsl_ns, unsigned int tsd2d_ns,
342 unsigned int tchsh_ns, unsigned int tslch_ns)
344 unsigned int ref_clk_ns;
345 unsigned int sclk_ns;
346 unsigned int tshsl, tchsh, tslch, tsd2d;
349 cadence_qspi_apb_controller_disable(reg_base);
352 ref_clk_ns = DIV_ROUND_UP(1000000000, ref_clk);
355 sclk_ns = DIV_ROUND_UP(1000000000, sclk_hz);
357 /* The controller adds additional delay to that programmed in the reg */
358 if (tshsl_ns >= sclk_ns + ref_clk_ns)
359 tshsl_ns -= sclk_ns + ref_clk_ns;
360 if (tchsh_ns >= sclk_ns + 3 * ref_clk_ns)
361 tchsh_ns -= sclk_ns + 3 * ref_clk_ns;
362 tshsl = DIV_ROUND_UP(tshsl_ns, ref_clk_ns);
363 tchsh = DIV_ROUND_UP(tchsh_ns, ref_clk_ns);
364 tslch = DIV_ROUND_UP(tslch_ns, ref_clk_ns);
365 tsd2d = DIV_ROUND_UP(tsd2d_ns, ref_clk_ns);
367 reg = ((tshsl & CQSPI_REG_DELAY_TSHSL_MASK)
368 << CQSPI_REG_DELAY_TSHSL_LSB);
369 reg |= ((tchsh & CQSPI_REG_DELAY_TCHSH_MASK)
370 << CQSPI_REG_DELAY_TCHSH_LSB);
371 reg |= ((tslch & CQSPI_REG_DELAY_TSLCH_MASK)
372 << CQSPI_REG_DELAY_TSLCH_LSB);
373 reg |= ((tsd2d & CQSPI_REG_DELAY_TSD2D_MASK)
374 << CQSPI_REG_DELAY_TSD2D_LSB);
375 writel(reg, reg_base + CQSPI_REG_DELAY);
377 cadence_qspi_apb_controller_enable(reg_base);
380 void cadence_qspi_apb_controller_init(struct cadence_spi_plat *plat)
384 cadence_qspi_apb_controller_disable(plat->regbase);
386 /* Configure the device size and address bytes */
387 reg = readl(plat->regbase + CQSPI_REG_SIZE);
388 /* Clear the previous value */
389 reg &= ~(CQSPI_REG_SIZE_PAGE_MASK << CQSPI_REG_SIZE_PAGE_LSB);
390 reg &= ~(CQSPI_REG_SIZE_BLOCK_MASK << CQSPI_REG_SIZE_BLOCK_LSB);
391 reg |= (plat->page_size << CQSPI_REG_SIZE_PAGE_LSB);
392 reg |= (plat->block_size << CQSPI_REG_SIZE_BLOCK_LSB);
393 writel(reg, plat->regbase + CQSPI_REG_SIZE);
395 /* Configure the remap address register, no remap */
396 writel(0, plat->regbase + CQSPI_REG_REMAP);
398 /* Indirect mode configurations */
399 writel(plat->fifo_depth / 2, plat->regbase + CQSPI_REG_SRAMPARTITION);
401 /* Disable all interrupts */
402 writel(0, plat->regbase + CQSPI_REG_IRQMASK);
404 cadence_qspi_apb_controller_enable(plat->regbase);
407 static int cadence_qspi_apb_exec_flash_cmd(void *reg_base,
410 unsigned int retry = CQSPI_REG_RETRY;
412 /* Write the CMDCTRL without start execution. */
413 writel(reg, reg_base + CQSPI_REG_CMDCTRL);
415 reg |= CQSPI_REG_CMDCTRL_EXECUTE;
416 writel(reg, reg_base + CQSPI_REG_CMDCTRL);
419 reg = readl(reg_base + CQSPI_REG_CMDCTRL);
420 if ((reg & CQSPI_REG_CMDCTRL_INPROGRESS) == 0)
426 printf("QSPI: flash command execution timeout\n");
430 /* Polling QSPI idle status. */
431 if (!cadence_qspi_wait_idle(reg_base))
437 /* For command RDID, RDSR. */
438 int cadence_qspi_apb_command_read(void *reg_base, const struct spi_mem_op *op)
441 unsigned int read_len;
443 unsigned int rxlen = op->data.nbytes;
444 void *rxbuf = op->data.buf.in;
446 if (rxlen > CQSPI_STIG_DATA_LEN_MAX || !rxbuf) {
447 printf("QSPI: Invalid input arguments rxlen %u\n", rxlen);
451 reg = op->cmd.opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB;
453 reg |= (0x1 << CQSPI_REG_CMDCTRL_RD_EN_LSB);
455 /* 0 means 1 byte. */
456 reg |= (((rxlen - 1) & CQSPI_REG_CMDCTRL_RD_BYTES_MASK)
457 << CQSPI_REG_CMDCTRL_RD_BYTES_LSB);
458 status = cadence_qspi_apb_exec_flash_cmd(reg_base, reg);
462 reg = readl(reg_base + CQSPI_REG_CMDREADDATALOWER);
464 /* Put the read value into rx_buf */
465 read_len = (rxlen > 4) ? 4 : rxlen;
466 memcpy(rxbuf, ®, read_len);
470 reg = readl(reg_base + CQSPI_REG_CMDREADDATAUPPER);
472 read_len = rxlen - read_len;
473 memcpy(rxbuf, ®, read_len);
478 /* For commands: WRSR, WREN, WRDI, CHIP_ERASE, BE, etc. */
479 int cadence_qspi_apb_command_write(void *reg_base, const struct spi_mem_op *op)
481 unsigned int reg = 0;
482 unsigned int wr_data;
484 unsigned int txlen = op->data.nbytes;
485 const void *txbuf = op->data.buf.out;
488 /* Reorder address to SPI bus order if only transferring address */
490 addr = cpu_to_be32(op->addr.val);
491 if (op->addr.nbytes == 3)
494 txlen = op->addr.nbytes;
497 if (txlen > CQSPI_STIG_DATA_LEN_MAX) {
498 printf("QSPI: Invalid input arguments txlen %u\n", txlen);
502 reg |= op->cmd.opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB;
505 /* writing data = yes */
506 reg |= (0x1 << CQSPI_REG_CMDCTRL_WR_EN_LSB);
507 reg |= ((txlen - 1) & CQSPI_REG_CMDCTRL_WR_BYTES_MASK)
508 << CQSPI_REG_CMDCTRL_WR_BYTES_LSB;
510 wr_len = txlen > 4 ? 4 : txlen;
511 memcpy(&wr_data, txbuf, wr_len);
512 writel(wr_data, reg_base +
513 CQSPI_REG_CMDWRITEDATALOWER);
517 wr_len = txlen - wr_len;
518 memcpy(&wr_data, txbuf, wr_len);
519 writel(wr_data, reg_base +
520 CQSPI_REG_CMDWRITEDATAUPPER);
524 /* Execute the command */
525 return cadence_qspi_apb_exec_flash_cmd(reg_base, reg);
528 /* Opcode + Address (3/4 bytes) + dummy bytes (0-4 bytes) */
529 int cadence_qspi_apb_read_setup(struct cadence_spi_plat *plat,
530 const struct spi_mem_op *op)
534 unsigned int dummy_clk;
535 unsigned int dummy_bytes = op->dummy.nbytes;
537 /* Setup the indirect trigger address */
538 writel(plat->trigger_address,
539 plat->regbase + CQSPI_REG_INDIRECTTRIGGER);
541 /* Configure the opcode */
542 rd_reg = op->cmd.opcode << CQSPI_REG_RD_INSTR_OPCODE_LSB;
544 if (op->data.buswidth == 8)
545 /* Instruction and address at DQ0, data at DQ0-7. */
546 rd_reg |= CQSPI_INST_TYPE_OCTAL << CQSPI_REG_RD_INSTR_TYPE_DATA_LSB;
547 else if (op->data.buswidth == 4)
548 /* Instruction and address at DQ0, data at DQ0-3. */
549 rd_reg |= CQSPI_INST_TYPE_QUAD << CQSPI_REG_RD_INSTR_TYPE_DATA_LSB;
551 writel(op->addr.val, plat->regbase + CQSPI_REG_INDIRECTRDSTARTADDR);
554 if (dummy_bytes > CQSPI_DUMMY_BYTES_MAX)
555 dummy_bytes = CQSPI_DUMMY_BYTES_MAX;
557 /* Convert to clock cycles. */
558 dummy_clk = dummy_bytes * CQSPI_DUMMY_CLKS_PER_BYTE;
561 rd_reg |= (dummy_clk & CQSPI_REG_RD_INSTR_DUMMY_MASK)
562 << CQSPI_REG_RD_INSTR_DUMMY_LSB;
565 writel(rd_reg, plat->regbase + CQSPI_REG_RD_INSTR);
567 /* set device size */
568 reg = readl(plat->regbase + CQSPI_REG_SIZE);
569 reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
570 reg |= (op->addr.nbytes - 1);
571 writel(reg, plat->regbase + CQSPI_REG_SIZE);
575 static u32 cadence_qspi_get_rd_sram_level(struct cadence_spi_plat *plat)
577 u32 reg = readl(plat->regbase + CQSPI_REG_SDRAMLEVEL);
578 reg >>= CQSPI_REG_SDRAMLEVEL_RD_LSB;
579 return reg & CQSPI_REG_SDRAMLEVEL_RD_MASK;
582 static int cadence_qspi_wait_for_data(struct cadence_spi_plat *plat)
584 unsigned int timeout = 10000;
588 reg = cadence_qspi_get_rd_sram_level(plat);
598 cadence_qspi_apb_indirect_read_execute(struct cadence_spi_plat *plat,
599 unsigned int n_rx, u8 *rxbuf)
601 unsigned int remaining = n_rx;
602 unsigned int bytes_to_read = 0;
605 writel(n_rx, plat->regbase + CQSPI_REG_INDIRECTRDBYTES);
607 /* Start the indirect read transfer */
608 writel(CQSPI_REG_INDIRECTRD_START,
609 plat->regbase + CQSPI_REG_INDIRECTRD);
611 while (remaining > 0) {
612 ret = cadence_qspi_wait_for_data(plat);
614 printf("Indirect write timed out (%i)\n", ret);
620 while (bytes_to_read != 0) {
621 bytes_to_read *= plat->fifo_width;
622 bytes_to_read = bytes_to_read > remaining ?
623 remaining : bytes_to_read;
625 * Handle non-4-byte aligned access to avoid
628 if (((uintptr_t)rxbuf % 4) || (bytes_to_read % 4))
629 readsb(plat->ahbbase, rxbuf, bytes_to_read);
631 readsl(plat->ahbbase, rxbuf,
633 rxbuf += bytes_to_read;
634 remaining -= bytes_to_read;
635 bytes_to_read = cadence_qspi_get_rd_sram_level(plat);
639 /* Check indirect done status */
640 ret = wait_for_bit_le32(plat->regbase + CQSPI_REG_INDIRECTRD,
641 CQSPI_REG_INDIRECTRD_DONE, 1, 10, 0);
643 printf("Indirect read completion error (%i)\n", ret);
647 /* Clear indirect completion status */
648 writel(CQSPI_REG_INDIRECTRD_DONE,
649 plat->regbase + CQSPI_REG_INDIRECTRD);
654 /* Cancel the indirect read */
655 writel(CQSPI_REG_INDIRECTRD_CANCEL,
656 plat->regbase + CQSPI_REG_INDIRECTRD);
660 int cadence_qspi_apb_read_execute(struct cadence_spi_plat *plat,
661 const struct spi_mem_op *op)
663 u64 from = op->addr.val;
664 void *buf = op->data.buf.in;
665 size_t len = op->data.nbytes;
667 if (plat->use_dac_mode && (from + len < plat->ahbsize)) {
669 dma_memcpy(buf, plat->ahbbase + from, len) < 0) {
670 memcpy_fromio(buf, plat->ahbbase + from, len);
672 if (!cadence_qspi_wait_idle(plat->regbase))
677 return cadence_qspi_apb_indirect_read_execute(plat, len, buf);
680 /* Opcode + Address (3/4 bytes) */
681 int cadence_qspi_apb_write_setup(struct cadence_spi_plat *plat,
682 const struct spi_mem_op *op)
686 /* Setup the indirect trigger address */
687 writel(plat->trigger_address,
688 plat->regbase + CQSPI_REG_INDIRECTTRIGGER);
690 /* Configure the opcode */
691 reg = op->cmd.opcode << CQSPI_REG_WR_INSTR_OPCODE_LSB;
692 writel(reg, plat->regbase + CQSPI_REG_WR_INSTR);
694 writel(op->addr.val, plat->regbase + CQSPI_REG_INDIRECTWRSTARTADDR);
696 reg = readl(plat->regbase + CQSPI_REG_SIZE);
697 reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
698 reg |= (op->addr.nbytes - 1);
699 writel(reg, plat->regbase + CQSPI_REG_SIZE);
704 cadence_qspi_apb_indirect_write_execute(struct cadence_spi_plat *plat,
705 unsigned int n_tx, const u8 *txbuf)
707 unsigned int page_size = plat->page_size;
708 unsigned int remaining = n_tx;
709 const u8 *bb_txbuf = txbuf;
710 void *bounce_buf = NULL;
711 unsigned int write_bytes;
715 * Use bounce buffer for non 32 bit aligned txbuf to avoid data
718 if ((uintptr_t)txbuf % 4) {
719 bounce_buf = malloc(n_tx);
722 memcpy(bounce_buf, txbuf, n_tx);
723 bb_txbuf = bounce_buf;
726 /* Configure the indirect read transfer bytes */
727 writel(n_tx, plat->regbase + CQSPI_REG_INDIRECTWRBYTES);
729 /* Start the indirect write transfer */
730 writel(CQSPI_REG_INDIRECTWR_START,
731 plat->regbase + CQSPI_REG_INDIRECTWR);
734 * Some delay is required for the above bit to be internally
735 * synchronized by the QSPI module.
737 ndelay(plat->wr_delay);
739 while (remaining > 0) {
740 write_bytes = remaining > page_size ? page_size : remaining;
741 writesl(plat->ahbbase, bb_txbuf, write_bytes >> 2);
743 writesb(plat->ahbbase,
744 bb_txbuf + rounddown(write_bytes, 4),
747 ret = wait_for_bit_le32(plat->regbase + CQSPI_REG_SDRAMLEVEL,
748 CQSPI_REG_SDRAMLEVEL_WR_MASK <<
749 CQSPI_REG_SDRAMLEVEL_WR_LSB, 0, 10, 0);
751 printf("Indirect write timed out (%i)\n", ret);
755 bb_txbuf += write_bytes;
756 remaining -= write_bytes;
759 /* Check indirect done status */
760 ret = wait_for_bit_le32(plat->regbase + CQSPI_REG_INDIRECTWR,
761 CQSPI_REG_INDIRECTWR_DONE, 1, 10, 0);
763 printf("Indirect write completion error (%i)\n", ret);
767 /* Clear indirect completion status */
768 writel(CQSPI_REG_INDIRECTWR_DONE,
769 plat->regbase + CQSPI_REG_INDIRECTWR);
775 /* Cancel the indirect write */
776 writel(CQSPI_REG_INDIRECTWR_CANCEL,
777 plat->regbase + CQSPI_REG_INDIRECTWR);
783 int cadence_qspi_apb_write_execute(struct cadence_spi_plat *plat,
784 const struct spi_mem_op *op)
786 u32 to = op->addr.val;
787 const void *buf = op->data.buf.out;
788 size_t len = op->data.nbytes;
790 if (plat->use_dac_mode && (to + len < plat->ahbsize)) {
791 memcpy_toio(plat->ahbbase + to, buf, len);
792 if (!cadence_qspi_wait_idle(plat->regbase))
797 return cadence_qspi_apb_indirect_write_execute(plat, len, buf);
800 void cadence_qspi_apb_enter_xip(void *reg_base, char xip_dummy)
804 /* enter XiP mode immediately and enable direct mode */
805 reg = readl(reg_base + CQSPI_REG_CONFIG);
806 reg |= CQSPI_REG_CONFIG_ENABLE;
807 reg |= CQSPI_REG_CONFIG_DIRECT;
808 reg |= CQSPI_REG_CONFIG_XIP_IMM;
809 writel(reg, reg_base + CQSPI_REG_CONFIG);
811 /* keep the XiP mode */
812 writel(xip_dummy, reg_base + CQSPI_REG_MODE_BIT);
814 /* Enable mode bit at devrd */
815 reg = readl(reg_base + CQSPI_REG_RD_INSTR);
816 reg |= (1 << CQSPI_REG_RD_INSTR_MODE_EN_LSB);
817 writel(reg, reg_base + CQSPI_REG_RD_INSTR);