blob: f9675f75a40121be76c805833a0aa45895f76221 [file] [log] [blame]
Stefan Roese1c60fe72014-11-07 12:37:49 +01001/*
2 * Copyright (C) 2012 Altera Corporation <www.altera.com>
3 * All rights reserved.
4 *
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.
15 *
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.
26 */
27
28#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060029#include <log.h>
Stefan Roese1c60fe72014-11-07 12:37:49 +010030#include <asm/io.h>
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +053031#include <dma.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060032#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060033#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090034#include <linux/errno.h>
Marek Vasutdae51dd2016-04-27 23:18:55 +020035#include <wait_bit.h>
Vignesh R4ca60192016-07-06 10:20:56 +053036#include <spi.h>
Vignesh Raghavendra27516a32020-01-27 10:36:39 +053037#include <spi-mem.h>
Vignesh Rad4bd8a2018-01-24 10:44:07 +053038#include <malloc.h>
Stefan Roese1c60fe72014-11-07 12:37:49 +010039#include "cadence_qspi.h"
40
Phil Edworthy3a5ae122016-11-29 12:58:30 +000041#define CQSPI_REG_POLL_US 1 /* 1us */
42#define CQSPI_REG_RETRY 10000
43#define CQSPI_POLL_IDLE_RETRY 3
Stefan Roese1c60fe72014-11-07 12:37:49 +010044
Stefan Roese1c60fe72014-11-07 12:37:49 +010045/* Transfer mode */
Phil Edworthy3a5ae122016-11-29 12:58:30 +000046#define CQSPI_INST_TYPE_SINGLE 0
47#define CQSPI_INST_TYPE_DUAL 1
48#define CQSPI_INST_TYPE_QUAD 2
Vignesh Raghavendra68f82662019-12-05 15:46:06 +053049#define CQSPI_INST_TYPE_OCTAL 3
Stefan Roese1c60fe72014-11-07 12:37:49 +010050
Phil Edworthy3a5ae122016-11-29 12:58:30 +000051#define CQSPI_STIG_DATA_LEN_MAX 8
Stefan Roese1c60fe72014-11-07 12:37:49 +010052
Phil Edworthy3a5ae122016-11-29 12:58:30 +000053#define CQSPI_DUMMY_CLKS_PER_BYTE 8
54#define CQSPI_DUMMY_BYTES_MAX 4
Stefan Roese1c60fe72014-11-07 12:37:49 +010055
Stefan Roese1c60fe72014-11-07 12:37:49 +010056/****************************************************************************
57 * Controller's configuration and status register (offset from QSPI_BASE)
58 ****************************************************************************/
59#define CQSPI_REG_CONFIG 0x00
Phil Edworthy3a5ae122016-11-29 12:58:30 +000060#define CQSPI_REG_CONFIG_ENABLE BIT(0)
Phil Edworthydd18c6f2016-11-29 12:58:29 +000061#define CQSPI_REG_CONFIG_CLK_POL BIT(1)
62#define CQSPI_REG_CONFIG_CLK_PHA BIT(2)
Phil Edworthy3a5ae122016-11-29 12:58:30 +000063#define CQSPI_REG_CONFIG_DIRECT BIT(7)
64#define CQSPI_REG_CONFIG_DECODE BIT(9)
65#define CQSPI_REG_CONFIG_XIP_IMM BIT(18)
Stefan Roese1c60fe72014-11-07 12:37:49 +010066#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
71
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
83
84#define CQSPI_REG_WR_INSTR 0x08
85#define CQSPI_REG_WR_INSTR_OPCODE_LSB 0
Ley Foon Tan08987992019-02-27 13:36:14 +080086#define CQSPI_REG_WR_INSTR_TYPE_DATA_LSB 16
Stefan Roese1c60fe72014-11-07 12:37:49 +010087
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
97
Phil Edworthydd18c6f2016-11-29 12:58:29 +000098#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
Stefan Roese1c60fe72014-11-07 12:37:49 +0100102
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
110
111#define CQSPI_REG_SRAMPARTITION 0x18
112#define CQSPI_REG_INDIRECTTRIGGER 0x1C
113
114#define CQSPI_REG_REMAP 0x24
115#define CQSPI_REG_MODE_BIT 0x28
116
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
122
123#define CQSPI_REG_IRQSTATUS 0x40
124#define CQSPI_REG_IRQMASK 0x44
125
126#define CQSPI_REG_INDIRECTRD 0x60
Phil Edworthy3a5ae122016-11-29 12:58:30 +0000127#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)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100131
132#define CQSPI_REG_INDIRECTRDWATERMARK 0x64
133#define CQSPI_REG_INDIRECTRDSTARTADDR 0x68
134#define CQSPI_REG_INDIRECTRDBYTES 0x6C
135
136#define CQSPI_REG_CMDCTRL 0x90
Phil Edworthy3a5ae122016-11-29 12:58:30 +0000137#define CQSPI_REG_CMDCTRL_EXECUTE BIT(0)
138#define CQSPI_REG_CMDCTRL_INPROGRESS BIT(1)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100139#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
152
153#define CQSPI_REG_INDIRECTWR 0x70
Phil Edworthy3a5ae122016-11-29 12:58:30 +0000154#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)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100158
159#define CQSPI_REG_INDIRECTWRWATERMARK 0x74
160#define CQSPI_REG_INDIRECTWRSTARTADDR 0x78
161#define CQSPI_REG_INDIRECTWRBYTES 0x7C
162
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
168
169#define CQSPI_REG_IS_IDLE(base) \
170 ((readl(base + CQSPI_REG_CONFIG) >> \
171 CQSPI_REG_CONFIG_IDLE_LSB) & 0x1)
172
Stefan Roese1c60fe72014-11-07 12:37:49 +0100173#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)
176
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)
180
Stefan Roese1c60fe72014-11-07 12:37:49 +0100181void cadence_qspi_apb_controller_enable(void *reg_base)
182{
183 unsigned int reg;
184 reg = readl(reg_base + CQSPI_REG_CONFIG);
Phil Edworthy3a5ae122016-11-29 12:58:30 +0000185 reg |= CQSPI_REG_CONFIG_ENABLE;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100186 writel(reg, reg_base + CQSPI_REG_CONFIG);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100187}
188
189void cadence_qspi_apb_controller_disable(void *reg_base)
190{
191 unsigned int reg;
192 reg = readl(reg_base + CQSPI_REG_CONFIG);
Phil Edworthy3a5ae122016-11-29 12:58:30 +0000193 reg &= ~CQSPI_REG_CONFIG_ENABLE;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100194 writel(reg, reg_base + CQSPI_REG_CONFIG);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100195}
196
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530197void cadence_qspi_apb_dac_mode_enable(void *reg_base)
198{
199 unsigned int reg;
200
201 reg = readl(reg_base + CQSPI_REG_CONFIG);
202 reg |= CQSPI_REG_CONFIG_DIRECT;
203 writel(reg, reg_base + CQSPI_REG_CONFIG);
204}
205
Stefan Roese1c60fe72014-11-07 12:37:49 +0100206/* Return 1 if idle, otherwise return 0 (busy). */
207static unsigned int cadence_qspi_wait_idle(void *reg_base)
208{
209 unsigned int start, count = 0;
210 /* timeout in unit of ms */
211 unsigned int timeout = 5000;
212
213 start = get_timer(0);
214 for ( ; get_timer(start) < timeout ; ) {
215 if (CQSPI_REG_IS_IDLE(reg_base))
216 count++;
217 else
218 count = 0;
219 /*
220 * Ensure the QSPI controller is in true idle state after
221 * reading back the same idle status consecutively
222 */
223 if (count >= CQSPI_POLL_IDLE_RETRY)
224 return 1;
225 }
226
227 /* Timeout, still in busy mode. */
228 printf("QSPI: QSPI is still busy after poll for %d times.\n",
229 CQSPI_REG_RETRY);
230 return 0;
231}
232
233void cadence_qspi_apb_readdata_capture(void *reg_base,
234 unsigned int bypass, unsigned int delay)
235{
236 unsigned int reg;
237 cadence_qspi_apb_controller_disable(reg_base);
238
Phil Edworthydd18c6f2016-11-29 12:58:29 +0000239 reg = readl(reg_base + CQSPI_REG_RD_DATA_CAPTURE);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100240
241 if (bypass)
Phil Edworthydd18c6f2016-11-29 12:58:29 +0000242 reg |= CQSPI_REG_RD_DATA_CAPTURE_BYPASS;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100243 else
Phil Edworthydd18c6f2016-11-29 12:58:29 +0000244 reg &= ~CQSPI_REG_RD_DATA_CAPTURE_BYPASS;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100245
Phil Edworthydd18c6f2016-11-29 12:58:29 +0000246 reg &= ~(CQSPI_REG_RD_DATA_CAPTURE_DELAY_MASK
247 << CQSPI_REG_RD_DATA_CAPTURE_DELAY_LSB);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100248
Phil Edworthydd18c6f2016-11-29 12:58:29 +0000249 reg |= (delay & CQSPI_REG_RD_DATA_CAPTURE_DELAY_MASK)
250 << CQSPI_REG_RD_DATA_CAPTURE_DELAY_LSB;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100251
Phil Edworthydd18c6f2016-11-29 12:58:29 +0000252 writel(reg, reg_base + CQSPI_REG_RD_DATA_CAPTURE);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100253
254 cadence_qspi_apb_controller_enable(reg_base);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100255}
256
257void cadence_qspi_apb_config_baudrate_div(void *reg_base,
258 unsigned int ref_clk_hz, unsigned int sclk_hz)
259{
260 unsigned int reg;
261 unsigned int div;
262
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);
266
Phil Edworthy8f24a442016-11-29 12:58:27 +0000267 /*
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.
271 */
272 div = DIV_ROUND_UP(ref_clk_hz, sclk_hz * 2) - 1;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100273
Chin Liang See91b2c192016-08-07 22:50:40 +0800274 /* 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;
277
Phil Edworthy67824ad2016-11-29 12:58:28 +0000278 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)));
280
Chin Liang See91b2c192016-08-07 22:50:40 +0800281 reg |= (div << CQSPI_REG_CONFIG_BAUD_LSB);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100282 writel(reg, reg_base + CQSPI_REG_CONFIG);
283
284 cadence_qspi_apb_controller_enable(reg_base);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100285}
286
Phil Edworthyeef2edc2016-11-29 12:58:31 +0000287void cadence_qspi_apb_set_clk_mode(void *reg_base, uint mode)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100288{
289 unsigned int reg;
290
291 cadence_qspi_apb_controller_disable(reg_base);
292 reg = readl(reg_base + CQSPI_REG_CONFIG);
Phil Edworthydd18c6f2016-11-29 12:58:29 +0000293 reg &= ~(CQSPI_REG_CONFIG_CLK_POL | CQSPI_REG_CONFIG_CLK_PHA);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100294
Phil Edworthyeef2edc2016-11-29 12:58:31 +0000295 if (mode & SPI_CPOL)
Phil Edworthydd18c6f2016-11-29 12:58:29 +0000296 reg |= CQSPI_REG_CONFIG_CLK_POL;
Phil Edworthyeef2edc2016-11-29 12:58:31 +0000297 if (mode & SPI_CPHA)
Phil Edworthydd18c6f2016-11-29 12:58:29 +0000298 reg |= CQSPI_REG_CONFIG_CLK_PHA;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100299
300 writel(reg, reg_base + CQSPI_REG_CONFIG);
301
302 cadence_qspi_apb_controller_enable(reg_base);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100303}
304
305void cadence_qspi_apb_chipselect(void *reg_base,
306 unsigned int chip_select, unsigned int decoder_enable)
307{
308 unsigned int reg;
309
310 cadence_qspi_apb_controller_disable(reg_base);
311
312 debug("%s : chipselect %d decode %d\n", __func__, chip_select,
313 decoder_enable);
314
315 reg = readl(reg_base + CQSPI_REG_CONFIG);
316 /* docoder */
317 if (decoder_enable) {
Phil Edworthy3a5ae122016-11-29 12:58:30 +0000318 reg |= CQSPI_REG_CONFIG_DECODE;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100319 } else {
Phil Edworthy3a5ae122016-11-29 12:58:30 +0000320 reg &= ~CQSPI_REG_CONFIG_DECODE;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100321 /* Convert CS if without decoder.
322 * CS0 to 4b'1110
323 * CS1 to 4b'1101
324 * CS2 to 4b'1011
325 * CS3 to 4b'0111
326 */
327 chip_select = 0xF & ~(1 << chip_select);
328 }
329
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);
335
336 cadence_qspi_apb_controller_enable(reg_base);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100337}
338
339void 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)
343{
344 unsigned int ref_clk_ns;
345 unsigned int sclk_ns;
346 unsigned int tshsl, tchsh, tslch, tsd2d;
347 unsigned int reg;
348
349 cadence_qspi_apb_controller_disable(reg_base);
350
351 /* Convert to ns. */
Phil Edworthy1fdd9232016-11-29 12:58:33 +0000352 ref_clk_ns = DIV_ROUND_UP(1000000000, ref_clk);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100353
354 /* Convert to ns. */
Phil Edworthy1fdd9232016-11-29 12:58:33 +0000355 sclk_ns = DIV_ROUND_UP(1000000000, sclk_hz);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100356
Phil Edworthy1fdd9232016-11-29 12:58:33 +0000357 /* 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);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100366
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);
376
377 cadence_qspi_apb_controller_enable(reg_base);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100378}
379
380void cadence_qspi_apb_controller_init(struct cadence_spi_platdata *plat)
381{
382 unsigned reg;
383
384 cadence_qspi_apb_controller_disable(plat->regbase);
385
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);
394
395 /* Configure the remap address register, no remap */
396 writel(0, plat->regbase + CQSPI_REG_REMAP);
397
Vikas Manocha215cea02015-07-02 18:29:43 -0700398 /* Indirect mode configurations */
Jason Rush1b4df5e2018-01-23 17:13:09 -0600399 writel(plat->fifo_depth / 2, plat->regbase + CQSPI_REG_SRAMPARTITION);
Vikas Manocha215cea02015-07-02 18:29:43 -0700400
Stefan Roese1c60fe72014-11-07 12:37:49 +0100401 /* Disable all interrupts */
402 writel(0, plat->regbase + CQSPI_REG_IRQMASK);
403
404 cadence_qspi_apb_controller_enable(plat->regbase);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100405}
406
407static int cadence_qspi_apb_exec_flash_cmd(void *reg_base,
408 unsigned int reg)
409{
410 unsigned int retry = CQSPI_REG_RETRY;
411
412 /* Write the CMDCTRL without start execution. */
413 writel(reg, reg_base + CQSPI_REG_CMDCTRL);
414 /* Start execute */
Phil Edworthy3a5ae122016-11-29 12:58:30 +0000415 reg |= CQSPI_REG_CMDCTRL_EXECUTE;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100416 writel(reg, reg_base + CQSPI_REG_CMDCTRL);
417
418 while (retry--) {
419 reg = readl(reg_base + CQSPI_REG_CMDCTRL);
Phil Edworthy3a5ae122016-11-29 12:58:30 +0000420 if ((reg & CQSPI_REG_CMDCTRL_INPROGRESS) == 0)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100421 break;
422 udelay(1);
423 }
424
425 if (!retry) {
426 printf("QSPI: flash command execution timeout\n");
427 return -EIO;
428 }
429
430 /* Polling QSPI idle status. */
431 if (!cadence_qspi_wait_idle(reg_base))
432 return -EIO;
433
434 return 0;
435}
436
437/* For command RDID, RDSR. */
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530438int cadence_qspi_apb_command_read(void *reg_base, const struct spi_mem_op *op)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100439{
440 unsigned int reg;
441 unsigned int read_len;
442 int status;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530443 unsigned int rxlen = op->data.nbytes;
444 void *rxbuf = op->data.buf.in;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100445
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530446 if (rxlen > CQSPI_STIG_DATA_LEN_MAX || !rxbuf) {
447 printf("QSPI: Invalid input arguments rxlen %u\n", rxlen);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100448 return -EINVAL;
449 }
450
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530451 reg = op->cmd.opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100452
453 reg |= (0x1 << CQSPI_REG_CMDCTRL_RD_EN_LSB);
454
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);
459 if (status != 0)
460 return status;
461
462 reg = readl(reg_base + CQSPI_REG_CMDREADDATALOWER);
463
464 /* Put the read value into rx_buf */
465 read_len = (rxlen > 4) ? 4 : rxlen;
466 memcpy(rxbuf, &reg, read_len);
467 rxbuf += read_len;
468
469 if (rxlen > 4) {
470 reg = readl(reg_base + CQSPI_REG_CMDREADDATAUPPER);
471
472 read_len = rxlen - read_len;
473 memcpy(rxbuf, &reg, read_len);
474 }
475 return 0;
476}
477
478/* For commands: WRSR, WREN, WRDI, CHIP_ERASE, BE, etc. */
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530479int cadence_qspi_apb_command_write(void *reg_base, const struct spi_mem_op *op)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100480{
481 unsigned int reg = 0;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100482 unsigned int wr_data;
483 unsigned int wr_len;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530484 unsigned int txlen = op->data.nbytes;
485 const void *txbuf = op->data.buf.out;
486 u32 addr;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100487
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530488 /* Reorder address to SPI bus order if only transferring address */
489 if (!txlen) {
490 addr = cpu_to_be32(op->addr.val);
491 if (op->addr.nbytes == 3)
492 addr >>= 8;
493 txbuf = &addr;
494 txlen = op->addr.nbytes;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100495 }
496
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530497 if (txlen > CQSPI_STIG_DATA_LEN_MAX) {
498 printf("QSPI: Invalid input arguments txlen %u\n", txlen);
499 return -EINVAL;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100500 }
501
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530502 reg |= op->cmd.opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB;
503
Stefan Roese1c60fe72014-11-07 12:37:49 +0100504 if (txlen) {
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;
509
510 wr_len = txlen > 4 ? 4 : txlen;
511 memcpy(&wr_data, txbuf, wr_len);
512 writel(wr_data, reg_base +
513 CQSPI_REG_CMDWRITEDATALOWER);
514
515 if (txlen > 4) {
516 txbuf += wr_len;
517 wr_len = txlen - wr_len;
518 memcpy(&wr_data, txbuf, wr_len);
519 writel(wr_data, reg_base +
520 CQSPI_REG_CMDWRITEDATAUPPER);
521 }
522 }
523
524 /* Execute the command */
525 return cadence_qspi_apb_exec_flash_cmd(reg_base, reg);
526}
527
528/* Opcode + Address (3/4 bytes) + dummy bytes (0-4 bytes) */
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530529int cadence_qspi_apb_read_setup(struct cadence_spi_platdata *plat,
530 const struct spi_mem_op *op)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100531{
532 unsigned int reg;
533 unsigned int rd_reg;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100534 unsigned int dummy_clk;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530535 unsigned int dummy_bytes = op->dummy.nbytes;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100536
537 /* Setup the indirect trigger address */
Jason Rush1b4df5e2018-01-23 17:13:09 -0600538 writel(plat->trigger_address,
Stefan Roese1c60fe72014-11-07 12:37:49 +0100539 plat->regbase + CQSPI_REG_INDIRECTTRIGGER);
540
Stefan Roese1c60fe72014-11-07 12:37:49 +0100541 /* Configure the opcode */
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530542 rd_reg = op->cmd.opcode << CQSPI_REG_RD_INSTR_OPCODE_LSB;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100543
Vignesh Raghavendra68f82662019-12-05 15:46:06 +0530544 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)
Vignesh R4ca60192016-07-06 10:20:56 +0530548 /* Instruction and address at DQ0, data at DQ0-3. */
549 rd_reg |= CQSPI_INST_TYPE_QUAD << CQSPI_REG_RD_INSTR_TYPE_DATA_LSB;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100550
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530551 writel(op->addr.val, plat->regbase + CQSPI_REG_INDIRECTRDSTARTADDR);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100552
Stefan Roese1c60fe72014-11-07 12:37:49 +0100553 if (dummy_bytes) {
554 if (dummy_bytes > CQSPI_DUMMY_BYTES_MAX)
555 dummy_bytes = CQSPI_DUMMY_BYTES_MAX;
556
Stefan Roese1c60fe72014-11-07 12:37:49 +0100557 /* Convert to clock cycles. */
558 dummy_clk = dummy_bytes * CQSPI_DUMMY_CLKS_PER_BYTE;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100559
560 if (dummy_clk)
561 rd_reg |= (dummy_clk & CQSPI_REG_RD_INSTR_DUMMY_MASK)
562 << CQSPI_REG_RD_INSTR_DUMMY_LSB;
563 }
564
565 writel(rd_reg, plat->regbase + CQSPI_REG_RD_INSTR);
566
567 /* set device size */
568 reg = readl(plat->regbase + CQSPI_REG_SIZE);
569 reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530570 reg |= (op->addr.nbytes - 1);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100571 writel(reg, plat->regbase + CQSPI_REG_SIZE);
572 return 0;
573}
574
Marek Vasut8c177432016-04-27 23:38:05 +0200575static u32 cadence_qspi_get_rd_sram_level(struct cadence_spi_platdata *plat)
576{
577 u32 reg = readl(plat->regbase + CQSPI_REG_SDRAMLEVEL);
578 reg >>= CQSPI_REG_SDRAMLEVEL_RD_LSB;
579 return reg & CQSPI_REG_SDRAMLEVEL_RD_MASK;
580}
581
582static int cadence_qspi_wait_for_data(struct cadence_spi_platdata *plat)
583{
584 unsigned int timeout = 10000;
585 u32 reg;
586
587 while (timeout--) {
588 reg = cadence_qspi_get_rd_sram_level(plat);
589 if (reg)
590 return reg;
591 udelay(1);
592 }
593
594 return -ETIMEDOUT;
595}
596
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530597static int
598cadence_qspi_apb_indirect_read_execute(struct cadence_spi_platdata *plat,
599 unsigned int n_rx, u8 *rxbuf)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100600{
Marek Vasut8c177432016-04-27 23:38:05 +0200601 unsigned int remaining = n_rx;
602 unsigned int bytes_to_read = 0;
603 int ret;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100604
Marek Vasut8c177432016-04-27 23:38:05 +0200605 writel(n_rx, plat->regbase + CQSPI_REG_INDIRECTRDBYTES);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100606
607 /* Start the indirect read transfer */
Phil Edworthy3a5ae122016-11-29 12:58:30 +0000608 writel(CQSPI_REG_INDIRECTRD_START,
Stefan Roese1c60fe72014-11-07 12:37:49 +0100609 plat->regbase + CQSPI_REG_INDIRECTRD);
610
Marek Vasut8c177432016-04-27 23:38:05 +0200611 while (remaining > 0) {
612 ret = cadence_qspi_wait_for_data(plat);
613 if (ret < 0) {
614 printf("Indirect write timed out (%i)\n", ret);
615 goto failrd;
616 }
Stefan Roese1c60fe72014-11-07 12:37:49 +0100617
Marek Vasut8c177432016-04-27 23:38:05 +0200618 bytes_to_read = ret;
619
620 while (bytes_to_read != 0) {
Jason Rush1b4df5e2018-01-23 17:13:09 -0600621 bytes_to_read *= plat->fifo_width;
Marek Vasut8c177432016-04-27 23:38:05 +0200622 bytes_to_read = bytes_to_read > remaining ?
623 remaining : bytes_to_read;
Goldschmidt Simon16cbd092018-01-24 10:44:05 +0530624 /*
625 * Handle non-4-byte aligned access to avoid
626 * data abort.
627 */
628 if (((uintptr_t)rxbuf % 4) || (bytes_to_read % 4))
629 readsb(plat->ahbbase, rxbuf, bytes_to_read);
630 else
631 readsl(plat->ahbbase, rxbuf,
632 bytes_to_read >> 2);
633 rxbuf += bytes_to_read;
Marek Vasut8c177432016-04-27 23:38:05 +0200634 remaining -= bytes_to_read;
635 bytes_to_read = cadence_qspi_get_rd_sram_level(plat);
636 }
637 }
638
639 /* Check indirect done status */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100640 ret = wait_for_bit_le32(plat->regbase + CQSPI_REG_INDIRECTRD,
641 CQSPI_REG_INDIRECTRD_DONE, 1, 10, 0);
Marek Vasut8c177432016-04-27 23:38:05 +0200642 if (ret) {
643 printf("Indirect read completion error (%i)\n", ret);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100644 goto failrd;
645 }
646
647 /* Clear indirect completion status */
Phil Edworthy3a5ae122016-11-29 12:58:30 +0000648 writel(CQSPI_REG_INDIRECTRD_DONE,
Stefan Roese1c60fe72014-11-07 12:37:49 +0100649 plat->regbase + CQSPI_REG_INDIRECTRD);
Marek Vasut8c177432016-04-27 23:38:05 +0200650
Stefan Roese1c60fe72014-11-07 12:37:49 +0100651 return 0;
652
653failrd:
654 /* Cancel the indirect read */
Phil Edworthy3a5ae122016-11-29 12:58:30 +0000655 writel(CQSPI_REG_INDIRECTRD_CANCEL,
Stefan Roese1c60fe72014-11-07 12:37:49 +0100656 plat->regbase + CQSPI_REG_INDIRECTRD);
Marek Vasut8c177432016-04-27 23:38:05 +0200657 return ret;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100658}
659
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530660int cadence_qspi_apb_read_execute(struct cadence_spi_platdata *plat,
661 const struct spi_mem_op *op)
662{
Vignesh Raghavendra68f82662019-12-05 15:46:06 +0530663 u64 from = op->addr.val;
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530664 void *buf = op->data.buf.in;
665 size_t len = op->data.nbytes;
666
667 if (plat->use_dac_mode && (from + len < plat->ahbsize)) {
668 if (len < 256 ||
669 dma_memcpy(buf, plat->ahbbase + from, len) < 0) {
670 memcpy_fromio(buf, plat->ahbbase + from, len);
671 }
672 if (!cadence_qspi_wait_idle(plat->regbase))
673 return -EIO;
674 return 0;
675 }
676
677 return cadence_qspi_apb_indirect_read_execute(plat, len, buf);
678}
679
Stefan Roese1c60fe72014-11-07 12:37:49 +0100680/* Opcode + Address (3/4 bytes) */
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530681int cadence_qspi_apb_write_setup(struct cadence_spi_platdata *plat,
682 const struct spi_mem_op *op)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100683{
684 unsigned int reg;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100685
Stefan Roese1c60fe72014-11-07 12:37:49 +0100686 /* Setup the indirect trigger address */
Jason Rush1b4df5e2018-01-23 17:13:09 -0600687 writel(plat->trigger_address,
Stefan Roese1c60fe72014-11-07 12:37:49 +0100688 plat->regbase + CQSPI_REG_INDIRECTTRIGGER);
689
Stefan Roese1c60fe72014-11-07 12:37:49 +0100690 /* Configure the opcode */
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530691 reg = op->cmd.opcode << CQSPI_REG_WR_INSTR_OPCODE_LSB;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100692 writel(reg, plat->regbase + CQSPI_REG_WR_INSTR);
693
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530694 writel(op->addr.val, plat->regbase + CQSPI_REG_INDIRECTWRSTARTADDR);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100695
696 reg = readl(plat->regbase + CQSPI_REG_SIZE);
697 reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530698 reg |= (op->addr.nbytes - 1);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100699 writel(reg, plat->regbase + CQSPI_REG_SIZE);
700 return 0;
701}
702
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530703static int
704cadence_qspi_apb_indirect_write_execute(struct cadence_spi_platdata *plat,
705 unsigned int n_tx, const u8 *txbuf)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100706{
Marek Vasutdae51dd2016-04-27 23:18:55 +0200707 unsigned int page_size = plat->page_size;
708 unsigned int remaining = n_tx;
Vignesh Rad4bd8a2018-01-24 10:44:07 +0530709 const u8 *bb_txbuf = txbuf;
710 void *bounce_buf = NULL;
Marek Vasutdae51dd2016-04-27 23:18:55 +0200711 unsigned int write_bytes;
712 int ret;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100713
Vignesh Rad4bd8a2018-01-24 10:44:07 +0530714 /*
715 * Use bounce buffer for non 32 bit aligned txbuf to avoid data
716 * aborts
717 */
718 if ((uintptr_t)txbuf % 4) {
719 bounce_buf = malloc(n_tx);
720 if (!bounce_buf)
721 return -ENOMEM;
722 memcpy(bounce_buf, txbuf, n_tx);
723 bb_txbuf = bounce_buf;
724 }
725
Stefan Roese1c60fe72014-11-07 12:37:49 +0100726 /* Configure the indirect read transfer bytes */
Marek Vasutdae51dd2016-04-27 23:18:55 +0200727 writel(n_tx, plat->regbase + CQSPI_REG_INDIRECTWRBYTES);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100728
729 /* Start the indirect write transfer */
Phil Edworthy3a5ae122016-11-29 12:58:30 +0000730 writel(CQSPI_REG_INDIRECTWR_START,
Stefan Roese1c60fe72014-11-07 12:37:49 +0100731 plat->regbase + CQSPI_REG_INDIRECTWR);
732
Marek Vasutdae51dd2016-04-27 23:18:55 +0200733 while (remaining > 0) {
734 write_bytes = remaining > page_size ? page_size : remaining;
Vignesh Rad4bd8a2018-01-24 10:44:07 +0530735 writesl(plat->ahbbase, bb_txbuf, write_bytes >> 2);
736 if (write_bytes % 4)
737 writesb(plat->ahbbase,
738 bb_txbuf + rounddown(write_bytes, 4),
739 write_bytes % 4);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100740
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100741 ret = wait_for_bit_le32(plat->regbase + CQSPI_REG_SDRAMLEVEL,
742 CQSPI_REG_SDRAMLEVEL_WR_MASK <<
743 CQSPI_REG_SDRAMLEVEL_WR_LSB, 0, 10, 0);
Marek Vasutdae51dd2016-04-27 23:18:55 +0200744 if (ret) {
745 printf("Indirect write timed out (%i)\n", ret);
746 goto failwr;
747 }
Stefan Roese1c60fe72014-11-07 12:37:49 +0100748
Vignesh Rad4bd8a2018-01-24 10:44:07 +0530749 bb_txbuf += write_bytes;
Marek Vasutdae51dd2016-04-27 23:18:55 +0200750 remaining -= write_bytes;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100751 }
752
Marek Vasutdae51dd2016-04-27 23:18:55 +0200753 /* Check indirect done status */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100754 ret = wait_for_bit_le32(plat->regbase + CQSPI_REG_INDIRECTWR,
755 CQSPI_REG_INDIRECTWR_DONE, 1, 10, 0);
Marek Vasutdae51dd2016-04-27 23:18:55 +0200756 if (ret) {
757 printf("Indirect write completion error (%i)\n", ret);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100758 goto failwr;
759 }
760
761 /* Clear indirect completion status */
Phil Edworthy3a5ae122016-11-29 12:58:30 +0000762 writel(CQSPI_REG_INDIRECTWR_DONE,
Stefan Roese1c60fe72014-11-07 12:37:49 +0100763 plat->regbase + CQSPI_REG_INDIRECTWR);
Vignesh Rad4bd8a2018-01-24 10:44:07 +0530764 if (bounce_buf)
765 free(bounce_buf);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100766 return 0;
767
768failwr:
769 /* Cancel the indirect write */
Phil Edworthy3a5ae122016-11-29 12:58:30 +0000770 writel(CQSPI_REG_INDIRECTWR_CANCEL,
Stefan Roese1c60fe72014-11-07 12:37:49 +0100771 plat->regbase + CQSPI_REG_INDIRECTWR);
Vignesh Rad4bd8a2018-01-24 10:44:07 +0530772 if (bounce_buf)
773 free(bounce_buf);
Marek Vasutdae51dd2016-04-27 23:18:55 +0200774 return ret;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100775}
776
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530777int cadence_qspi_apb_write_execute(struct cadence_spi_platdata *plat,
778 const struct spi_mem_op *op)
779{
780 u32 to = op->addr.val;
781 const void *buf = op->data.buf.out;
782 size_t len = op->data.nbytes;
783
784 if (plat->use_dac_mode && (to + len < plat->ahbsize)) {
785 memcpy_toio(plat->ahbbase + to, buf, len);
786 if (!cadence_qspi_wait_idle(plat->regbase))
787 return -EIO;
788 return 0;
789 }
790
791 return cadence_qspi_apb_indirect_write_execute(plat, len, buf);
792}
793
Stefan Roese1c60fe72014-11-07 12:37:49 +0100794void cadence_qspi_apb_enter_xip(void *reg_base, char xip_dummy)
795{
796 unsigned int reg;
797
798 /* enter XiP mode immediately and enable direct mode */
799 reg = readl(reg_base + CQSPI_REG_CONFIG);
Phil Edworthy3a5ae122016-11-29 12:58:30 +0000800 reg |= CQSPI_REG_CONFIG_ENABLE;
801 reg |= CQSPI_REG_CONFIG_DIRECT;
802 reg |= CQSPI_REG_CONFIG_XIP_IMM;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100803 writel(reg, reg_base + CQSPI_REG_CONFIG);
804
805 /* keep the XiP mode */
806 writel(xip_dummy, reg_base + CQSPI_REG_MODE_BIT);
807
808 /* Enable mode bit at devrd */
809 reg = readl(reg_base + CQSPI_REG_RD_INSTR);
810 reg |= (1 << CQSPI_REG_RD_INSTR_MODE_EN_LSB);
811 writel(reg, reg_base + CQSPI_REG_RD_INSTR);
812}