blob: 5e84144940789ae924c0deebf1db35cc0e04748b [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>
29#include <asm/io.h>
30#include <asm/errno.h>
Marek Vasutdae51dd2016-04-27 23:18:55 +020031#include <wait_bit.h>
Stefan Roese1c60fe72014-11-07 12:37:49 +010032#include "cadence_qspi.h"
33
34#define CQSPI_REG_POLL_US (1) /* 1us */
35#define CQSPI_REG_RETRY (10000)
36#define CQSPI_POLL_IDLE_RETRY (3)
37
38#define CQSPI_FIFO_WIDTH (4)
39
Stefan Roese1c60fe72014-11-07 12:37:49 +010040#define CQSPI_REG_SRAM_THRESHOLD_WORDS (50)
41
42/* Transfer mode */
43#define CQSPI_INST_TYPE_SINGLE (0)
44#define CQSPI_INST_TYPE_DUAL (1)
45#define CQSPI_INST_TYPE_QUAD (2)
46
47#define CQSPI_STIG_DATA_LEN_MAX (8)
48#define CQSPI_INDIRECTTRIGGER_ADDR_MASK (0xFFFFF)
49
50#define CQSPI_DUMMY_CLKS_PER_BYTE (8)
51#define CQSPI_DUMMY_BYTES_MAX (4)
52
53
54#define CQSPI_REG_SRAM_FILL_THRESHOLD \
55 ((CQSPI_REG_SRAM_SIZE_WORD / 2) * CQSPI_FIFO_WIDTH)
56/****************************************************************************
57 * Controller's configuration and status register (offset from QSPI_BASE)
58 ****************************************************************************/
59#define CQSPI_REG_CONFIG 0x00
60#define CQSPI_REG_CONFIG_CLK_POL_LSB 1
61#define CQSPI_REG_CONFIG_CLK_PHA_LSB 2
Jagan Teki99d71672015-10-23 01:36:06 +053062#define CQSPI_REG_CONFIG_ENABLE_MASK BIT(0)
63#define CQSPI_REG_CONFIG_DIRECT_MASK BIT(7)
64#define CQSPI_REG_CONFIG_DECODE_MASK BIT(9)
65#define CQSPI_REG_CONFIG_XIP_IMM_MASK 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
86
87#define CQSPI_REG_DELAY 0x0C
88#define CQSPI_REG_DELAY_TSLCH_LSB 0
89#define CQSPI_REG_DELAY_TCHSH_LSB 8
90#define CQSPI_REG_DELAY_TSD2D_LSB 16
91#define CQSPI_REG_DELAY_TSHSL_LSB 24
92#define CQSPI_REG_DELAY_TSLCH_MASK 0xFF
93#define CQSPI_REG_DELAY_TCHSH_MASK 0xFF
94#define CQSPI_REG_DELAY_TSD2D_MASK 0xFF
95#define CQSPI_REG_DELAY_TSHSL_MASK 0xFF
96
97#define CQSPI_READLCAPTURE 0x10
98#define CQSPI_READLCAPTURE_BYPASS_LSB 0
99#define CQSPI_READLCAPTURE_DELAY_LSB 1
100#define CQSPI_READLCAPTURE_DELAY_MASK 0xF
101
102#define CQSPI_REG_SIZE 0x14
103#define CQSPI_REG_SIZE_ADDRESS_LSB 0
104#define CQSPI_REG_SIZE_PAGE_LSB 4
105#define CQSPI_REG_SIZE_BLOCK_LSB 16
106#define CQSPI_REG_SIZE_ADDRESS_MASK 0xF
107#define CQSPI_REG_SIZE_PAGE_MASK 0xFFF
108#define CQSPI_REG_SIZE_BLOCK_MASK 0x3F
109
110#define CQSPI_REG_SRAMPARTITION 0x18
111#define CQSPI_REG_INDIRECTTRIGGER 0x1C
112
113#define CQSPI_REG_REMAP 0x24
114#define CQSPI_REG_MODE_BIT 0x28
115
116#define CQSPI_REG_SDRAMLEVEL 0x2C
117#define CQSPI_REG_SDRAMLEVEL_RD_LSB 0
118#define CQSPI_REG_SDRAMLEVEL_WR_LSB 16
119#define CQSPI_REG_SDRAMLEVEL_RD_MASK 0xFFFF
120#define CQSPI_REG_SDRAMLEVEL_WR_MASK 0xFFFF
121
122#define CQSPI_REG_IRQSTATUS 0x40
123#define CQSPI_REG_IRQMASK 0x44
124
125#define CQSPI_REG_INDIRECTRD 0x60
Jagan Teki99d71672015-10-23 01:36:06 +0530126#define CQSPI_REG_INDIRECTRD_START_MASK BIT(0)
127#define CQSPI_REG_INDIRECTRD_CANCEL_MASK BIT(1)
128#define CQSPI_REG_INDIRECTRD_INPROGRESS_MASK BIT(2)
129#define CQSPI_REG_INDIRECTRD_DONE_MASK BIT(5)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100130
131#define CQSPI_REG_INDIRECTRDWATERMARK 0x64
132#define CQSPI_REG_INDIRECTRDSTARTADDR 0x68
133#define CQSPI_REG_INDIRECTRDBYTES 0x6C
134
135#define CQSPI_REG_CMDCTRL 0x90
Jagan Teki99d71672015-10-23 01:36:06 +0530136#define CQSPI_REG_CMDCTRL_EXECUTE_MASK BIT(0)
137#define CQSPI_REG_CMDCTRL_INPROGRESS_MASK BIT(1)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100138#define CQSPI_REG_CMDCTRL_DUMMY_LSB 7
139#define CQSPI_REG_CMDCTRL_WR_BYTES_LSB 12
140#define CQSPI_REG_CMDCTRL_WR_EN_LSB 15
141#define CQSPI_REG_CMDCTRL_ADD_BYTES_LSB 16
142#define CQSPI_REG_CMDCTRL_ADDR_EN_LSB 19
143#define CQSPI_REG_CMDCTRL_RD_BYTES_LSB 20
144#define CQSPI_REG_CMDCTRL_RD_EN_LSB 23
145#define CQSPI_REG_CMDCTRL_OPCODE_LSB 24
146#define CQSPI_REG_CMDCTRL_DUMMY_MASK 0x1F
147#define CQSPI_REG_CMDCTRL_WR_BYTES_MASK 0x7
148#define CQSPI_REG_CMDCTRL_ADD_BYTES_MASK 0x3
149#define CQSPI_REG_CMDCTRL_RD_BYTES_MASK 0x7
150#define CQSPI_REG_CMDCTRL_OPCODE_MASK 0xFF
151
152#define CQSPI_REG_INDIRECTWR 0x70
Jagan Teki99d71672015-10-23 01:36:06 +0530153#define CQSPI_REG_INDIRECTWR_START_MASK BIT(0)
154#define CQSPI_REG_INDIRECTWR_CANCEL_MASK BIT(1)
155#define CQSPI_REG_INDIRECTWR_INPROGRESS_MASK BIT(2)
156#define CQSPI_REG_INDIRECTWR_DONE_MASK BIT(5)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100157
158#define CQSPI_REG_INDIRECTWRWATERMARK 0x74
159#define CQSPI_REG_INDIRECTWRSTARTADDR 0x78
160#define CQSPI_REG_INDIRECTWRBYTES 0x7C
161
162#define CQSPI_REG_CMDADDRESS 0x94
163#define CQSPI_REG_CMDREADDATALOWER 0xA0
164#define CQSPI_REG_CMDREADDATAUPPER 0xA4
165#define CQSPI_REG_CMDWRITEDATALOWER 0xA8
166#define CQSPI_REG_CMDWRITEDATAUPPER 0xAC
167
168#define CQSPI_REG_IS_IDLE(base) \
169 ((readl(base + CQSPI_REG_CONFIG) >> \
170 CQSPI_REG_CONFIG_IDLE_LSB) & 0x1)
171
172#define CQSPI_CAL_DELAY(tdelay_ns, tref_ns, tsclk_ns) \
173 ((((tdelay_ns) - (tsclk_ns)) / (tref_ns)))
174
175#define CQSPI_GET_RD_SRAM_LEVEL(reg_base) \
176 (((readl(reg_base + CQSPI_REG_SDRAMLEVEL)) >> \
177 CQSPI_REG_SDRAMLEVEL_RD_LSB) & CQSPI_REG_SDRAMLEVEL_RD_MASK)
178
179#define CQSPI_GET_WR_SRAM_LEVEL(reg_base) \
180 (((readl(reg_base + CQSPI_REG_SDRAMLEVEL)) >> \
181 CQSPI_REG_SDRAMLEVEL_WR_LSB) & CQSPI_REG_SDRAMLEVEL_WR_MASK)
182
183static unsigned int cadence_qspi_apb_cmd2addr(const unsigned char *addr_buf,
184 unsigned int addr_width)
185{
186 unsigned int addr;
187
188 addr = (addr_buf[0] << 16) | (addr_buf[1] << 8) | addr_buf[2];
189
190 if (addr_width == 4)
191 addr = (addr << 8) | addr_buf[3];
192
193 return addr;
194}
195
196static void cadence_qspi_apb_read_fifo_data(void *dest,
197 const void *src_ahb_addr, unsigned int bytes)
198{
199 unsigned int temp;
200 int remaining = bytes;
201 unsigned int *dest_ptr = (unsigned int *)dest;
202 unsigned int *src_ptr = (unsigned int *)src_ahb_addr;
203
Vikas Manochaf206f712015-07-02 18:29:45 -0700204 while (remaining >= sizeof(dest_ptr)) {
205 *dest_ptr = readl(src_ptr);
206 remaining -= sizeof(src_ptr);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100207 dest_ptr++;
208 }
Vikas Manochaf206f712015-07-02 18:29:45 -0700209 if (remaining) {
210 /* dangling bytes */
211 temp = readl(src_ptr);
212 memcpy(dest_ptr, &temp, remaining);
213 }
Stefan Roese1c60fe72014-11-07 12:37:49 +0100214
215 return;
216}
217
Stefan Roese1c60fe72014-11-07 12:37:49 +0100218/* Read from SRAM FIFO with polling SRAM fill level. */
219static int qspi_read_sram_fifo_poll(const void *reg_base, void *dest_addr,
220 const void *src_addr, unsigned int num_bytes)
221{
222 unsigned int remaining = num_bytes;
223 unsigned int retry;
224 unsigned int sram_level = 0;
225 unsigned char *dest = (unsigned char *)dest_addr;
226
227 while (remaining > 0) {
228 retry = CQSPI_REG_RETRY;
229 while (retry--) {
230 sram_level = CQSPI_GET_RD_SRAM_LEVEL(reg_base);
231 if (sram_level)
232 break;
233 udelay(1);
234 }
235
236 if (!retry) {
237 printf("QSPI: No receive data after polling for %d times\n",
238 CQSPI_REG_RETRY);
239 return -1;
240 }
241
242 sram_level *= CQSPI_FIFO_WIDTH;
243 sram_level = sram_level > remaining ? remaining : sram_level;
244
245 /* Read data from FIFO. */
246 cadence_qspi_apb_read_fifo_data(dest, src_addr, sram_level);
247 dest += sram_level;
248 remaining -= sram_level;
249 udelay(1);
250 }
251 return 0;
252}
253
Stefan Roese1c60fe72014-11-07 12:37:49 +0100254void cadence_qspi_apb_controller_enable(void *reg_base)
255{
256 unsigned int reg;
257 reg = readl(reg_base + CQSPI_REG_CONFIG);
258 reg |= CQSPI_REG_CONFIG_ENABLE_MASK;
259 writel(reg, reg_base + CQSPI_REG_CONFIG);
260 return;
261}
262
263void cadence_qspi_apb_controller_disable(void *reg_base)
264{
265 unsigned int reg;
266 reg = readl(reg_base + CQSPI_REG_CONFIG);
267 reg &= ~CQSPI_REG_CONFIG_ENABLE_MASK;
268 writel(reg, reg_base + CQSPI_REG_CONFIG);
269 return;
270}
271
272/* Return 1 if idle, otherwise return 0 (busy). */
273static unsigned int cadence_qspi_wait_idle(void *reg_base)
274{
275 unsigned int start, count = 0;
276 /* timeout in unit of ms */
277 unsigned int timeout = 5000;
278
279 start = get_timer(0);
280 for ( ; get_timer(start) < timeout ; ) {
281 if (CQSPI_REG_IS_IDLE(reg_base))
282 count++;
283 else
284 count = 0;
285 /*
286 * Ensure the QSPI controller is in true idle state after
287 * reading back the same idle status consecutively
288 */
289 if (count >= CQSPI_POLL_IDLE_RETRY)
290 return 1;
291 }
292
293 /* Timeout, still in busy mode. */
294 printf("QSPI: QSPI is still busy after poll for %d times.\n",
295 CQSPI_REG_RETRY);
296 return 0;
297}
298
299void cadence_qspi_apb_readdata_capture(void *reg_base,
300 unsigned int bypass, unsigned int delay)
301{
302 unsigned int reg;
303 cadence_qspi_apb_controller_disable(reg_base);
304
305 reg = readl(reg_base + CQSPI_READLCAPTURE);
306
307 if (bypass)
308 reg |= (1 << CQSPI_READLCAPTURE_BYPASS_LSB);
309 else
310 reg &= ~(1 << CQSPI_READLCAPTURE_BYPASS_LSB);
311
312 reg &= ~(CQSPI_READLCAPTURE_DELAY_MASK
313 << CQSPI_READLCAPTURE_DELAY_LSB);
314
315 reg |= ((delay & CQSPI_READLCAPTURE_DELAY_MASK)
316 << CQSPI_READLCAPTURE_DELAY_LSB);
317
318 writel(reg, reg_base + CQSPI_READLCAPTURE);
319
320 cadence_qspi_apb_controller_enable(reg_base);
321 return;
322}
323
324void cadence_qspi_apb_config_baudrate_div(void *reg_base,
325 unsigned int ref_clk_hz, unsigned int sclk_hz)
326{
327 unsigned int reg;
328 unsigned int div;
329
330 cadence_qspi_apb_controller_disable(reg_base);
331 reg = readl(reg_base + CQSPI_REG_CONFIG);
332 reg &= ~(CQSPI_REG_CONFIG_BAUD_MASK << CQSPI_REG_CONFIG_BAUD_LSB);
333
334 div = ref_clk_hz / sclk_hz;
335
336 if (div > 32)
337 div = 32;
338
339 /* Check if even number. */
340 if ((div & 1)) {
341 div = (div / 2);
342 } else {
343 if (ref_clk_hz % sclk_hz)
344 /* ensure generated SCLK doesn't exceed user
345 specified sclk_hz */
346 div = (div / 2);
347 else
348 div = (div / 2) - 1;
349 }
350
351 debug("%s: ref_clk %dHz sclk %dHz Div 0x%x\n", __func__,
352 ref_clk_hz, sclk_hz, div);
353
354 div = (div & CQSPI_REG_CONFIG_BAUD_MASK) << CQSPI_REG_CONFIG_BAUD_LSB;
355 reg |= div;
356 writel(reg, reg_base + CQSPI_REG_CONFIG);
357
358 cadence_qspi_apb_controller_enable(reg_base);
359 return;
360}
361
362void cadence_qspi_apb_set_clk_mode(void *reg_base,
363 unsigned int clk_pol, unsigned int clk_pha)
364{
365 unsigned int reg;
366
367 cadence_qspi_apb_controller_disable(reg_base);
368 reg = readl(reg_base + CQSPI_REG_CONFIG);
369 reg &= ~(1 <<
370 (CQSPI_REG_CONFIG_CLK_POL_LSB | CQSPI_REG_CONFIG_CLK_PHA_LSB));
371
372 reg |= ((clk_pol & 0x1) << CQSPI_REG_CONFIG_CLK_POL_LSB);
373 reg |= ((clk_pha & 0x1) << CQSPI_REG_CONFIG_CLK_PHA_LSB);
374
375 writel(reg, reg_base + CQSPI_REG_CONFIG);
376
377 cadence_qspi_apb_controller_enable(reg_base);
378 return;
379}
380
381void cadence_qspi_apb_chipselect(void *reg_base,
382 unsigned int chip_select, unsigned int decoder_enable)
383{
384 unsigned int reg;
385
386 cadence_qspi_apb_controller_disable(reg_base);
387
388 debug("%s : chipselect %d decode %d\n", __func__, chip_select,
389 decoder_enable);
390
391 reg = readl(reg_base + CQSPI_REG_CONFIG);
392 /* docoder */
393 if (decoder_enable) {
394 reg |= CQSPI_REG_CONFIG_DECODE_MASK;
395 } else {
396 reg &= ~CQSPI_REG_CONFIG_DECODE_MASK;
397 /* Convert CS if without decoder.
398 * CS0 to 4b'1110
399 * CS1 to 4b'1101
400 * CS2 to 4b'1011
401 * CS3 to 4b'0111
402 */
403 chip_select = 0xF & ~(1 << chip_select);
404 }
405
406 reg &= ~(CQSPI_REG_CONFIG_CHIPSELECT_MASK
407 << CQSPI_REG_CONFIG_CHIPSELECT_LSB);
408 reg |= (chip_select & CQSPI_REG_CONFIG_CHIPSELECT_MASK)
409 << CQSPI_REG_CONFIG_CHIPSELECT_LSB;
410 writel(reg, reg_base + CQSPI_REG_CONFIG);
411
412 cadence_qspi_apb_controller_enable(reg_base);
413 return;
414}
415
416void cadence_qspi_apb_delay(void *reg_base,
417 unsigned int ref_clk, unsigned int sclk_hz,
418 unsigned int tshsl_ns, unsigned int tsd2d_ns,
419 unsigned int tchsh_ns, unsigned int tslch_ns)
420{
421 unsigned int ref_clk_ns;
422 unsigned int sclk_ns;
423 unsigned int tshsl, tchsh, tslch, tsd2d;
424 unsigned int reg;
425
426 cadence_qspi_apb_controller_disable(reg_base);
427
428 /* Convert to ns. */
429 ref_clk_ns = (1000000000) / ref_clk;
430
431 /* Convert to ns. */
432 sclk_ns = (1000000000) / sclk_hz;
433
434 /* Plus 1 to round up 1 clock cycle. */
435 tshsl = CQSPI_CAL_DELAY(tshsl_ns, ref_clk_ns, sclk_ns) + 1;
436 tchsh = CQSPI_CAL_DELAY(tchsh_ns, ref_clk_ns, sclk_ns) + 1;
437 tslch = CQSPI_CAL_DELAY(tslch_ns, ref_clk_ns, sclk_ns) + 1;
438 tsd2d = CQSPI_CAL_DELAY(tsd2d_ns, ref_clk_ns, sclk_ns) + 1;
439
440 reg = ((tshsl & CQSPI_REG_DELAY_TSHSL_MASK)
441 << CQSPI_REG_DELAY_TSHSL_LSB);
442 reg |= ((tchsh & CQSPI_REG_DELAY_TCHSH_MASK)
443 << CQSPI_REG_DELAY_TCHSH_LSB);
444 reg |= ((tslch & CQSPI_REG_DELAY_TSLCH_MASK)
445 << CQSPI_REG_DELAY_TSLCH_LSB);
446 reg |= ((tsd2d & CQSPI_REG_DELAY_TSD2D_MASK)
447 << CQSPI_REG_DELAY_TSD2D_LSB);
448 writel(reg, reg_base + CQSPI_REG_DELAY);
449
450 cadence_qspi_apb_controller_enable(reg_base);
451 return;
452}
453
454void cadence_qspi_apb_controller_init(struct cadence_spi_platdata *plat)
455{
456 unsigned reg;
457
458 cadence_qspi_apb_controller_disable(plat->regbase);
459
460 /* Configure the device size and address bytes */
461 reg = readl(plat->regbase + CQSPI_REG_SIZE);
462 /* Clear the previous value */
463 reg &= ~(CQSPI_REG_SIZE_PAGE_MASK << CQSPI_REG_SIZE_PAGE_LSB);
464 reg &= ~(CQSPI_REG_SIZE_BLOCK_MASK << CQSPI_REG_SIZE_BLOCK_LSB);
465 reg |= (plat->page_size << CQSPI_REG_SIZE_PAGE_LSB);
466 reg |= (plat->block_size << CQSPI_REG_SIZE_BLOCK_LSB);
467 writel(reg, plat->regbase + CQSPI_REG_SIZE);
468
469 /* Configure the remap address register, no remap */
470 writel(0, plat->regbase + CQSPI_REG_REMAP);
471
Vikas Manocha215cea02015-07-02 18:29:43 -0700472 /* Indirect mode configurations */
Vikas Manocha480f3b52015-07-02 18:29:44 -0700473 writel((plat->sram_size/2), plat->regbase + CQSPI_REG_SRAMPARTITION);
Vikas Manocha215cea02015-07-02 18:29:43 -0700474
Stefan Roese1c60fe72014-11-07 12:37:49 +0100475 /* Disable all interrupts */
476 writel(0, plat->regbase + CQSPI_REG_IRQMASK);
477
478 cadence_qspi_apb_controller_enable(plat->regbase);
479 return;
480}
481
482static int cadence_qspi_apb_exec_flash_cmd(void *reg_base,
483 unsigned int reg)
484{
485 unsigned int retry = CQSPI_REG_RETRY;
486
487 /* Write the CMDCTRL without start execution. */
488 writel(reg, reg_base + CQSPI_REG_CMDCTRL);
489 /* Start execute */
490 reg |= CQSPI_REG_CMDCTRL_EXECUTE_MASK;
491 writel(reg, reg_base + CQSPI_REG_CMDCTRL);
492
493 while (retry--) {
494 reg = readl(reg_base + CQSPI_REG_CMDCTRL);
495 if ((reg & CQSPI_REG_CMDCTRL_INPROGRESS_MASK) == 0)
496 break;
497 udelay(1);
498 }
499
500 if (!retry) {
501 printf("QSPI: flash command execution timeout\n");
502 return -EIO;
503 }
504
505 /* Polling QSPI idle status. */
506 if (!cadence_qspi_wait_idle(reg_base))
507 return -EIO;
508
509 return 0;
510}
511
512/* For command RDID, RDSR. */
513int cadence_qspi_apb_command_read(void *reg_base,
514 unsigned int cmdlen, const u8 *cmdbuf, unsigned int rxlen,
515 u8 *rxbuf)
516{
517 unsigned int reg;
518 unsigned int read_len;
519 int status;
520
521 if (!cmdlen || rxlen > CQSPI_STIG_DATA_LEN_MAX || rxbuf == NULL) {
522 printf("QSPI: Invalid input arguments cmdlen %d rxlen %d\n",
523 cmdlen, rxlen);
524 return -EINVAL;
525 }
526
527 reg = cmdbuf[0] << CQSPI_REG_CMDCTRL_OPCODE_LSB;
528
529 reg |= (0x1 << CQSPI_REG_CMDCTRL_RD_EN_LSB);
530
531 /* 0 means 1 byte. */
532 reg |= (((rxlen - 1) & CQSPI_REG_CMDCTRL_RD_BYTES_MASK)
533 << CQSPI_REG_CMDCTRL_RD_BYTES_LSB);
534 status = cadence_qspi_apb_exec_flash_cmd(reg_base, reg);
535 if (status != 0)
536 return status;
537
538 reg = readl(reg_base + CQSPI_REG_CMDREADDATALOWER);
539
540 /* Put the read value into rx_buf */
541 read_len = (rxlen > 4) ? 4 : rxlen;
542 memcpy(rxbuf, &reg, read_len);
543 rxbuf += read_len;
544
545 if (rxlen > 4) {
546 reg = readl(reg_base + CQSPI_REG_CMDREADDATAUPPER);
547
548 read_len = rxlen - read_len;
549 memcpy(rxbuf, &reg, read_len);
550 }
551 return 0;
552}
553
554/* For commands: WRSR, WREN, WRDI, CHIP_ERASE, BE, etc. */
555int cadence_qspi_apb_command_write(void *reg_base, unsigned int cmdlen,
556 const u8 *cmdbuf, unsigned int txlen, const u8 *txbuf)
557{
558 unsigned int reg = 0;
559 unsigned int addr_value;
560 unsigned int wr_data;
561 unsigned int wr_len;
562
563 if (!cmdlen || cmdlen > 5 || txlen > 8 || cmdbuf == NULL) {
564 printf("QSPI: Invalid input arguments cmdlen %d txlen %d\n",
565 cmdlen, txlen);
566 return -EINVAL;
567 }
568
569 reg |= cmdbuf[0] << CQSPI_REG_CMDCTRL_OPCODE_LSB;
570
571 if (cmdlen == 4 || cmdlen == 5) {
572 /* Command with address */
573 reg |= (0x1 << CQSPI_REG_CMDCTRL_ADDR_EN_LSB);
574 /* Number of bytes to write. */
575 reg |= ((cmdlen - 2) & CQSPI_REG_CMDCTRL_ADD_BYTES_MASK)
576 << CQSPI_REG_CMDCTRL_ADD_BYTES_LSB;
577 /* Get address */
578 addr_value = cadence_qspi_apb_cmd2addr(&cmdbuf[1],
579 cmdlen >= 5 ? 4 : 3);
580
581 writel(addr_value, reg_base + CQSPI_REG_CMDADDRESS);
582 }
583
584 if (txlen) {
585 /* writing data = yes */
586 reg |= (0x1 << CQSPI_REG_CMDCTRL_WR_EN_LSB);
587 reg |= ((txlen - 1) & CQSPI_REG_CMDCTRL_WR_BYTES_MASK)
588 << CQSPI_REG_CMDCTRL_WR_BYTES_LSB;
589
590 wr_len = txlen > 4 ? 4 : txlen;
591 memcpy(&wr_data, txbuf, wr_len);
592 writel(wr_data, reg_base +
593 CQSPI_REG_CMDWRITEDATALOWER);
594
595 if (txlen > 4) {
596 txbuf += wr_len;
597 wr_len = txlen - wr_len;
598 memcpy(&wr_data, txbuf, wr_len);
599 writel(wr_data, reg_base +
600 CQSPI_REG_CMDWRITEDATAUPPER);
601 }
602 }
603
604 /* Execute the command */
605 return cadence_qspi_apb_exec_flash_cmd(reg_base, reg);
606}
607
608/* Opcode + Address (3/4 bytes) + dummy bytes (0-4 bytes) */
609int cadence_qspi_apb_indirect_read_setup(struct cadence_spi_platdata *plat,
610 unsigned int cmdlen, const u8 *cmdbuf)
611{
612 unsigned int reg;
613 unsigned int rd_reg;
614 unsigned int addr_value;
615 unsigned int dummy_clk;
616 unsigned int dummy_bytes;
617 unsigned int addr_bytes;
618
619 /*
620 * Identify addr_byte. All NOR flash device drivers are using fast read
621 * which always expecting 1 dummy byte, 1 cmd byte and 3/4 addr byte.
622 * With that, the length is in value of 5 or 6. Only FRAM chip from
623 * ramtron using normal read (which won't need dummy byte).
624 * Unlikely NOR flash using normal read due to performance issue.
625 */
626 if (cmdlen >= 5)
627 /* to cater fast read where cmd + addr + dummy */
628 addr_bytes = cmdlen - 2;
629 else
630 /* for normal read (only ramtron as of now) */
631 addr_bytes = cmdlen - 1;
632
633 /* Setup the indirect trigger address */
634 writel(((u32)plat->ahbbase & CQSPI_INDIRECTTRIGGER_ADDR_MASK),
635 plat->regbase + CQSPI_REG_INDIRECTTRIGGER);
636
Stefan Roese1c60fe72014-11-07 12:37:49 +0100637 /* Configure the opcode */
638 rd_reg = cmdbuf[0] << CQSPI_REG_RD_INSTR_OPCODE_LSB;
639
640#if (CONFIG_SPI_FLASH_QUAD == 1)
641 /* Instruction and address at DQ0, data at DQ0-3. */
642 rd_reg |= CQSPI_INST_TYPE_QUAD << CQSPI_REG_RD_INSTR_TYPE_DATA_LSB;
643#endif
644
645 /* Get address */
646 addr_value = cadence_qspi_apb_cmd2addr(&cmdbuf[1], addr_bytes);
647 writel(addr_value, plat->regbase + CQSPI_REG_INDIRECTRDSTARTADDR);
648
649 /* The remaining lenght is dummy bytes. */
650 dummy_bytes = cmdlen - addr_bytes - 1;
651 if (dummy_bytes) {
652 if (dummy_bytes > CQSPI_DUMMY_BYTES_MAX)
653 dummy_bytes = CQSPI_DUMMY_BYTES_MAX;
654
655 rd_reg |= (1 << CQSPI_REG_RD_INSTR_MODE_EN_LSB);
656#if defined(CONFIG_SPL_SPI_XIP) && defined(CONFIG_SPL_BUILD)
657 writel(0x0, plat->regbase + CQSPI_REG_MODE_BIT);
658#else
659 writel(0xFF, plat->regbase + CQSPI_REG_MODE_BIT);
660#endif
661
662 /* Convert to clock cycles. */
663 dummy_clk = dummy_bytes * CQSPI_DUMMY_CLKS_PER_BYTE;
664 /* Need to minus the mode byte (8 clocks). */
665 dummy_clk -= CQSPI_DUMMY_CLKS_PER_BYTE;
666
667 if (dummy_clk)
668 rd_reg |= (dummy_clk & CQSPI_REG_RD_INSTR_DUMMY_MASK)
669 << CQSPI_REG_RD_INSTR_DUMMY_LSB;
670 }
671
672 writel(rd_reg, plat->regbase + CQSPI_REG_RD_INSTR);
673
674 /* set device size */
675 reg = readl(plat->regbase + CQSPI_REG_SIZE);
676 reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
677 reg |= (addr_bytes - 1);
678 writel(reg, plat->regbase + CQSPI_REG_SIZE);
679 return 0;
680}
681
682int cadence_qspi_apb_indirect_read_execute(struct cadence_spi_platdata *plat,
683 unsigned int rxlen, u8 *rxbuf)
684{
685 unsigned int reg;
686
687 writel(rxlen, plat->regbase + CQSPI_REG_INDIRECTRDBYTES);
688
689 /* Start the indirect read transfer */
690 writel(CQSPI_REG_INDIRECTRD_START_MASK,
691 plat->regbase + CQSPI_REG_INDIRECTRD);
692
693 if (qspi_read_sram_fifo_poll(plat->regbase, (void *)rxbuf,
694 (const void *)plat->ahbbase, rxlen))
695 goto failrd;
696
697 /* Check flash indirect controller */
698 reg = readl(plat->regbase + CQSPI_REG_INDIRECTRD);
699 if (!(reg & CQSPI_REG_INDIRECTRD_DONE_MASK)) {
700 reg = readl(plat->regbase + CQSPI_REG_INDIRECTRD);
701 printf("QSPI: indirect completion status error with reg 0x%08x\n",
702 reg);
703 goto failrd;
704 }
705
706 /* Clear indirect completion status */
707 writel(CQSPI_REG_INDIRECTRD_DONE_MASK,
708 plat->regbase + CQSPI_REG_INDIRECTRD);
709 return 0;
710
711failrd:
712 /* Cancel the indirect read */
713 writel(CQSPI_REG_INDIRECTRD_CANCEL_MASK,
714 plat->regbase + CQSPI_REG_INDIRECTRD);
715 return -1;
716}
717
718/* Opcode + Address (3/4 bytes) */
719int cadence_qspi_apb_indirect_write_setup(struct cadence_spi_platdata *plat,
720 unsigned int cmdlen, const u8 *cmdbuf)
721{
722 unsigned int reg;
723 unsigned int addr_bytes = cmdlen > 4 ? 4 : 3;
724
725 if (cmdlen < 4 || cmdbuf == NULL) {
726 printf("QSPI: iInvalid input argument, len %d cmdbuf 0x%08x\n",
727 cmdlen, (unsigned int)cmdbuf);
728 return -EINVAL;
729 }
730 /* Setup the indirect trigger address */
731 writel(((u32)plat->ahbbase & CQSPI_INDIRECTTRIGGER_ADDR_MASK),
732 plat->regbase + CQSPI_REG_INDIRECTTRIGGER);
733
Stefan Roese1c60fe72014-11-07 12:37:49 +0100734 /* Configure the opcode */
735 reg = cmdbuf[0] << CQSPI_REG_WR_INSTR_OPCODE_LSB;
736 writel(reg, plat->regbase + CQSPI_REG_WR_INSTR);
737
738 /* Setup write address. */
739 reg = cadence_qspi_apb_cmd2addr(&cmdbuf[1], addr_bytes);
740 writel(reg, plat->regbase + CQSPI_REG_INDIRECTWRSTARTADDR);
741
742 reg = readl(plat->regbase + CQSPI_REG_SIZE);
743 reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
744 reg |= (addr_bytes - 1);
745 writel(reg, plat->regbase + CQSPI_REG_SIZE);
746 return 0;
747}
748
749int cadence_qspi_apb_indirect_write_execute(struct cadence_spi_platdata *plat,
Marek Vasutdae51dd2016-04-27 23:18:55 +0200750 unsigned int n_tx, const u8 *txbuf)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100751{
Marek Vasutdae51dd2016-04-27 23:18:55 +0200752 unsigned int page_size = plat->page_size;
753 unsigned int remaining = n_tx;
754 unsigned int write_bytes;
755 int ret;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100756
757 /* Configure the indirect read transfer bytes */
Marek Vasutdae51dd2016-04-27 23:18:55 +0200758 writel(n_tx, plat->regbase + CQSPI_REG_INDIRECTWRBYTES);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100759
760 /* Start the indirect write transfer */
761 writel(CQSPI_REG_INDIRECTWR_START_MASK,
762 plat->regbase + CQSPI_REG_INDIRECTWR);
763
Marek Vasutdae51dd2016-04-27 23:18:55 +0200764 while (remaining > 0) {
765 write_bytes = remaining > page_size ? page_size : remaining;
766 /* Handle non-4-byte aligned access to avoid data abort. */
767 if (((uintptr_t)txbuf % 4) || (write_bytes % 4))
768 writesb(plat->ahbbase, txbuf, write_bytes);
769 else
770 writesl(plat->ahbbase, txbuf, write_bytes >> 2);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100771
Marek Vasutdae51dd2016-04-27 23:18:55 +0200772 ret = wait_for_bit("QSPI", plat->regbase + CQSPI_REG_SDRAMLEVEL,
773 CQSPI_REG_SDRAMLEVEL_WR_MASK <<
774 CQSPI_REG_SDRAMLEVEL_WR_LSB, 0, 10, 0);
775 if (ret) {
776 printf("Indirect write timed out (%i)\n", ret);
777 goto failwr;
778 }
Stefan Roese1c60fe72014-11-07 12:37:49 +0100779
Marek Vasutdae51dd2016-04-27 23:18:55 +0200780 txbuf += write_bytes;
781 remaining -= write_bytes;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100782 }
783
Marek Vasutdae51dd2016-04-27 23:18:55 +0200784 /* Check indirect done status */
785 ret = wait_for_bit("QSPI", plat->regbase + CQSPI_REG_INDIRECTWR,
786 CQSPI_REG_INDIRECTWR_DONE_MASK, 1, 10, 0);
787 if (ret) {
788 printf("Indirect write completion error (%i)\n", ret);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100789 goto failwr;
790 }
791
792 /* Clear indirect completion status */
793 writel(CQSPI_REG_INDIRECTWR_DONE_MASK,
794 plat->regbase + CQSPI_REG_INDIRECTWR);
795 return 0;
796
797failwr:
798 /* Cancel the indirect write */
799 writel(CQSPI_REG_INDIRECTWR_CANCEL_MASK,
800 plat->regbase + CQSPI_REG_INDIRECTWR);
Marek Vasutdae51dd2016-04-27 23:18:55 +0200801 return ret;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100802}
803
804void cadence_qspi_apb_enter_xip(void *reg_base, char xip_dummy)
805{
806 unsigned int reg;
807
808 /* enter XiP mode immediately and enable direct mode */
809 reg = readl(reg_base + CQSPI_REG_CONFIG);
810 reg |= CQSPI_REG_CONFIG_ENABLE_MASK;
811 reg |= CQSPI_REG_CONFIG_DIRECT_MASK;
812 reg |= CQSPI_REG_CONFIG_XIP_IMM_MASK;
813 writel(reg, reg_base + CQSPI_REG_CONFIG);
814
815 /* keep the XiP mode */
816 writel(xip_dummy, reg_base + CQSPI_REG_MODE_BIT);
817
818 /* Enable mode bit at devrd */
819 reg = readl(reg_base + CQSPI_REG_RD_INSTR);
820 reg |= (1 << CQSPI_REG_RD_INSTR_MODE_EN_LSB);
821 writel(reg, reg_base + CQSPI_REG_RD_INSTR);
822}