]> Git Repo - J-u-boot.git/blame - drivers/spi/fsl_qspi.c
spi: fsl_qspi: Assign AMBA mem according CS num in dts
[J-u-boot.git] / drivers / spi / fsl_qspi.c
CommitLineData
6b57ff6f 1/*
5bc48308 2 * Copyright 2013-2015 Freescale Semiconductor, Inc.
6b57ff6f
AW
3 *
4 * Freescale Quad Serial Peripheral Interface (QSPI) driver
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
10#include <malloc.h>
11#include <spi.h>
12#include <asm/io.h>
13#include <linux/sizes.h>
5bc48308
HW
14#include <dm.h>
15#include <errno.h>
beedbc2e 16#include <watchdog.h>
6b57ff6f
AW
17#include "fsl_qspi.h"
18
5bc48308
HW
19DECLARE_GLOBAL_DATA_PTR;
20
6b57ff6f 21#define RX_BUFFER_SIZE 0x80
b93ab2ee
PF
22#ifdef CONFIG_MX6SX
23#define TX_BUFFER_SIZE 0x200
24#else
6b57ff6f 25#define TX_BUFFER_SIZE 0x40
b93ab2ee 26#endif
6b57ff6f 27
8770413f 28#define OFFSET_BITS_MASK GENMASK(23, 0)
6b57ff6f
AW
29
30#define FLASH_STATUS_WEL 0x02
31
32/* SEQID */
33#define SEQID_WREN 1
34#define SEQID_FAST_READ 2
35#define SEQID_RDSR 3
36#define SEQID_SE 4
37#define SEQID_CHIP_ERASE 5
38#define SEQID_PP 6
39#define SEQID_RDID 7
ba4dc8ab 40#define SEQID_BE_4K 8
a2358783
PF
41#ifdef CONFIG_SPI_FLASH_BAR
42#define SEQID_BRRD 9
43#define SEQID_BRWR 10
44#define SEQID_RDEAR 11
45#define SEQID_WREAR 12
46#endif
6b57ff6f 47
53e3db7f
PF
48/* QSPI CMD */
49#define QSPI_CMD_PP 0x02 /* Page program (up to 256 bytes) */
50#define QSPI_CMD_RDSR 0x05 /* Read status register */
51#define QSPI_CMD_WREN 0x06 /* Write enable */
52#define QSPI_CMD_FAST_READ 0x0b /* Read data bytes (high frequency) */
ba4dc8ab 53#define QSPI_CMD_BE_4K 0x20 /* 4K erase */
53e3db7f
PF
54#define QSPI_CMD_CHIP_ERASE 0xc7 /* Erase whole flash chip */
55#define QSPI_CMD_SE 0xd8 /* Sector erase (usually 64KiB) */
56#define QSPI_CMD_RDID 0x9f /* Read JEDEC ID */
57
a2358783
PF
58/* Used for Micron, winbond and Macronix flashes */
59#define QSPI_CMD_WREAR 0xc5 /* EAR register write */
60#define QSPI_CMD_RDEAR 0xc8 /* EAR reigster read */
61
62/* Used for Spansion flashes only. */
63#define QSPI_CMD_BRRD 0x16 /* Bank register read */
64#define QSPI_CMD_BRWR 0x17 /* Bank register write */
65
53e3db7f
PF
66/* 4-byte address QSPI CMD - used on Spansion and some Macronix flashes */
67#define QSPI_CMD_FAST_READ_4B 0x0c /* Read data bytes (high frequency) */
68#define QSPI_CMD_PP_4B 0x12 /* Page program (up to 256 bytes) */
69#define QSPI_CMD_SE_4B 0xdc /* Sector erase (usually 64KiB) */
6b57ff6f 70
5bc48308 71/* fsl_qspi_platdata flags */
29e6abd9 72#define QSPI_FLAG_REGMAP_ENDIAN_BIG BIT(0)
6b57ff6f 73
5bc48308
HW
74/* default SCK frequency, unit: HZ */
75#define FSL_QSPI_DEFAULT_SCK_FREQ 50000000
6b57ff6f 76
5bc48308
HW
77/* QSPI max chipselect signals number */
78#define FSL_QSPI_MAX_CHIPSELECT_NUM 4
79
80#ifdef CONFIG_DM_SPI
81/**
82 * struct fsl_qspi_platdata - platform data for Freescale QSPI
83 *
84 * @flags: Flags for QSPI QSPI_FLAG_...
85 * @speed_hz: Default SCK frequency
86 * @reg_base: Base address of QSPI registers
87 * @amba_base: Base address of QSPI memory mapping
88 * @amba_total_size: size of QSPI memory mapping
89 * @flash_num: Number of active slave devices
90 * @num_chipselect: Number of QSPI chipselect signals
91 */
92struct fsl_qspi_platdata {
93 u32 flags;
94 u32 speed_hz;
bf9bffa9
YY
95 fdt_addr_t reg_base;
96 fdt_addr_t amba_base;
97 fdt_size_t amba_total_size;
5bc48308
HW
98 u32 flash_num;
99 u32 num_chipselect;
100};
b93ab2ee 101#endif
5bc48308
HW
102
103/**
104 * struct fsl_qspi_priv - private data for Freescale QSPI
105 *
106 * @flags: Flags for QSPI QSPI_FLAG_...
107 * @bus_clk: QSPI input clk frequency
108 * @speed_hz: Default SCK frequency
109 * @cur_seqid: current LUT table sequence id
110 * @sf_addr: flash access offset
111 * @amba_base: Base address of QSPI memory mapping of every CS
112 * @amba_total_size: size of QSPI memory mapping
113 * @cur_amba_base: Base address of QSPI memory mapping of current CS
114 * @flash_num: Number of active slave devices
115 * @num_chipselect: Number of QSPI chipselect signals
116 * @regs: Point to QSPI register structure for I/O access
117 */
118struct fsl_qspi_priv {
119 u32 flags;
120 u32 bus_clk;
121 u32 speed_hz;
122 u32 cur_seqid;
123 u32 sf_addr;
124 u32 amba_base[FSL_QSPI_MAX_CHIPSELECT_NUM];
125 u32 amba_total_size;
126 u32 cur_amba_base;
127 u32 flash_num;
128 u32 num_chipselect;
129 struct fsl_qspi_regs *regs;
6b57ff6f
AW
130};
131
5bc48308 132#ifndef CONFIG_DM_SPI
6b57ff6f
AW
133struct fsl_qspi {
134 struct spi_slave slave;
5bc48308 135 struct fsl_qspi_priv priv;
6b57ff6f 136};
5bc48308
HW
137#endif
138
139static u32 qspi_read32(u32 flags, u32 *addr)
140{
141 return flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ?
142 in_be32(addr) : in_le32(addr);
143}
144
145static void qspi_write32(u32 flags, u32 *addr, u32 val)
146{
147 flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ?
148 out_be32(addr, val) : out_le32(addr, val);
149}
6b57ff6f
AW
150
151/* QSPI support swapping the flash read/write data
152 * in hardware for LS102xA, but not for VF610 */
153static inline u32 qspi_endian_xchg(u32 data)
154{
155#ifdef CONFIG_VF610
156 return swab32(data);
157#else
158 return data;
159#endif
160}
161
5bc48308 162static void qspi_set_lut(struct fsl_qspi_priv *priv)
6b57ff6f 163{
5bc48308 164 struct fsl_qspi_regs *regs = priv->regs;
6b57ff6f
AW
165 u32 lut_base;
166
167 /* Unlock the LUT */
5bc48308
HW
168 qspi_write32(priv->flags, &regs->lutkey, LUT_KEY_VALUE);
169 qspi_write32(priv->flags, &regs->lckcr, QSPI_LCKCR_UNLOCK);
6b57ff6f
AW
170
171 /* Write Enable */
172 lut_base = SEQID_WREN * 4;
5bc48308 173 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_WREN) |
6b57ff6f 174 PAD0(LUT_PAD1) | INSTR0(LUT_CMD));
5bc48308
HW
175 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
176 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
177 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
6b57ff6f
AW
178
179 /* Fast Read */
180 lut_base = SEQID_FAST_READ * 4;
a2358783 181#ifdef CONFIG_SPI_FLASH_BAR
5bc48308
HW
182 qspi_write32(priv->flags, &regs->lut[lut_base],
183 OPRND0(QSPI_CMD_FAST_READ) | PAD0(LUT_PAD1) |
184 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
a2358783
PF
185 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
186#else
6b57ff6f 187 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
5bc48308
HW
188 qspi_write32(priv->flags, &regs->lut[lut_base],
189 OPRND0(QSPI_CMD_FAST_READ) | PAD0(LUT_PAD1) |
190 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
191 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
6b57ff6f 192 else
5bc48308 193 qspi_write32(priv->flags, &regs->lut[lut_base],
53e3db7f
PF
194 OPRND0(QSPI_CMD_FAST_READ_4B) |
195 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) |
196 OPRND1(ADDR32BIT) | PAD1(LUT_PAD1) |
197 INSTR1(LUT_ADDR));
a2358783 198#endif
5bc48308
HW
199 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
200 OPRND0(8) | PAD0(LUT_PAD1) | INSTR0(LUT_DUMMY) |
201 OPRND1(RX_BUFFER_SIZE) | PAD1(LUT_PAD1) |
202 INSTR1(LUT_READ));
203 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
204 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
6b57ff6f
AW
205
206 /* Read Status */
207 lut_base = SEQID_RDSR * 4;
5bc48308 208 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDSR) |
6b57ff6f
AW
209 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
210 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
5bc48308
HW
211 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
212 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
213 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
6b57ff6f
AW
214
215 /* Erase a sector */
216 lut_base = SEQID_SE * 4;
a2358783 217#ifdef CONFIG_SPI_FLASH_BAR
5bc48308 218 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_SE) |
a2358783
PF
219 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
220 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
221#else
6b57ff6f 222 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
5bc48308
HW
223 qspi_write32(priv->flags, &regs->lut[lut_base],
224 OPRND0(QSPI_CMD_SE) | PAD0(LUT_PAD1) |
225 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
226 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
6b57ff6f 227 else
5bc48308
HW
228 qspi_write32(priv->flags, &regs->lut[lut_base],
229 OPRND0(QSPI_CMD_SE_4B) | PAD0(LUT_PAD1) |
230 INSTR0(LUT_CMD) | OPRND1(ADDR32BIT) |
231 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
a2358783 232#endif
5bc48308
HW
233 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
234 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
235 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
6b57ff6f
AW
236
237 /* Erase the whole chip */
238 lut_base = SEQID_CHIP_ERASE * 4;
5bc48308
HW
239 qspi_write32(priv->flags, &regs->lut[lut_base],
240 OPRND0(QSPI_CMD_CHIP_ERASE) |
241 PAD0(LUT_PAD1) | INSTR0(LUT_CMD));
242 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
243 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
244 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
6b57ff6f
AW
245
246 /* Page Program */
247 lut_base = SEQID_PP * 4;
a2358783 248#ifdef CONFIG_SPI_FLASH_BAR
5bc48308 249 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_PP) |
a2358783
PF
250 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
251 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
252#else
6b57ff6f 253 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
5bc48308
HW
254 qspi_write32(priv->flags, &regs->lut[lut_base],
255 OPRND0(QSPI_CMD_PP) | PAD0(LUT_PAD1) |
256 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
257 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
6b57ff6f 258 else
5bc48308
HW
259 qspi_write32(priv->flags, &regs->lut[lut_base],
260 OPRND0(QSPI_CMD_PP_4B) | PAD0(LUT_PAD1) |
261 INSTR0(LUT_CMD) | OPRND1(ADDR32BIT) |
262 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
a2358783 263#endif
b93ab2ee
PF
264#ifdef CONFIG_MX6SX
265 /*
266 * To MX6SX, OPRND0(TX_BUFFER_SIZE) can not work correctly.
267 * So, Use IDATSZ in IPCR to determine the size and here set 0.
268 */
5bc48308 269 qspi_write32(priv->flags, &regs->lut[lut_base + 1], OPRND0(0) |
b93ab2ee
PF
270 PAD0(LUT_PAD1) | INSTR0(LUT_WRITE));
271#else
5bc48308
HW
272 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
273 OPRND0(TX_BUFFER_SIZE) |
274 PAD0(LUT_PAD1) | INSTR0(LUT_WRITE));
b93ab2ee 275#endif
5bc48308
HW
276 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
277 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
6b57ff6f
AW
278
279 /* READ ID */
280 lut_base = SEQID_RDID * 4;
5bc48308 281 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDID) |
6b57ff6f
AW
282 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(8) |
283 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
5bc48308
HW
284 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
285 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
286 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
6b57ff6f 287
ba4dc8ab
PF
288 /* SUB SECTOR 4K ERASE */
289 lut_base = SEQID_BE_4K * 4;
5bc48308 290 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BE_4K) |
ba4dc8ab
PF
291 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
292 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
293
a2358783
PF
294#ifdef CONFIG_SPI_FLASH_BAR
295 /*
296 * BRRD BRWR RDEAR WREAR are all supported, because it is hard to
297 * dynamically check whether to set BRRD BRWR or RDEAR WREAR during
298 * initialization.
299 */
300 lut_base = SEQID_BRRD * 4;
5bc48308 301 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BRRD) |
a2358783
PF
302 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
303 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
304
305 lut_base = SEQID_BRWR * 4;
5bc48308 306 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BRWR) |
a2358783
PF
307 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
308 PAD1(LUT_PAD1) | INSTR1(LUT_WRITE));
309
310 lut_base = SEQID_RDEAR * 4;
5bc48308 311 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDEAR) |
a2358783
PF
312 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
313 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
314
315 lut_base = SEQID_WREAR * 4;
5bc48308 316 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_WREAR) |
a2358783
PF
317 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
318 PAD1(LUT_PAD1) | INSTR1(LUT_WRITE));
319#endif
6b57ff6f 320 /* Lock the LUT */
5bc48308
HW
321 qspi_write32(priv->flags, &regs->lutkey, LUT_KEY_VALUE);
322 qspi_write32(priv->flags, &regs->lckcr, QSPI_LCKCR_LOCK);
6b57ff6f
AW
323}
324
5f7f70c1
PF
325#if defined(CONFIG_SYS_FSL_QSPI_AHB)
326/*
327 * If we have changed the content of the flash by writing or erasing,
328 * we need to invalidate the AHB buffer. If we do not do so, we may read out
329 * the wrong data. The spec tells us reset the AHB domain and Serial Flash
330 * domain at the same time.
331 */
5bc48308 332static inline void qspi_ahb_invalid(struct fsl_qspi_priv *priv)
5f7f70c1 333{
5bc48308 334 struct fsl_qspi_regs *regs = priv->regs;
5f7f70c1
PF
335 u32 reg;
336
5bc48308 337 reg = qspi_read32(priv->flags, &regs->mcr);
5f7f70c1 338 reg |= QSPI_MCR_SWRSTHD_MASK | QSPI_MCR_SWRSTSD_MASK;
5bc48308 339 qspi_write32(priv->flags, &regs->mcr, reg);
5f7f70c1
PF
340
341 /*
342 * The minimum delay : 1 AHB + 2 SFCK clocks.
343 * Delay 1 us is enough.
344 */
345 udelay(1);
346
347 reg &= ~(QSPI_MCR_SWRSTHD_MASK | QSPI_MCR_SWRSTSD_MASK);
5bc48308 348 qspi_write32(priv->flags, &regs->mcr, reg);
5f7f70c1
PF
349}
350
351/* Read out the data from the AHB buffer. */
5bc48308 352static inline void qspi_ahb_read(struct fsl_qspi_priv *priv, u8 *rxbuf, int len)
5f7f70c1 353{
5bc48308 354 struct fsl_qspi_regs *regs = priv->regs;
5f7f70c1
PF
355 u32 mcr_reg;
356
5bc48308 357 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
5f7f70c1 358
5bc48308
HW
359 qspi_write32(priv->flags, &regs->mcr,
360 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
5f7f70c1
PF
361 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
362
363 /* Read out the data directly from the AHB buffer. */
5bc48308 364 memcpy(rxbuf, (u8 *)(priv->cur_amba_base + priv->sf_addr), len);
5f7f70c1 365
5bc48308 366 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
5f7f70c1
PF
367}
368
5bc48308 369static void qspi_enable_ddr_mode(struct fsl_qspi_priv *priv)
5f7f70c1
PF
370{
371 u32 reg, reg2;
5bc48308 372 struct fsl_qspi_regs *regs = priv->regs;
5f7f70c1 373
5bc48308 374 reg = qspi_read32(priv->flags, &regs->mcr);
5f7f70c1 375 /* Disable the module */
5bc48308 376 qspi_write32(priv->flags, &regs->mcr, reg | QSPI_MCR_MDIS_MASK);
5f7f70c1
PF
377
378 /* Set the Sampling Register for DDR */
5bc48308 379 reg2 = qspi_read32(priv->flags, &regs->smpr);
5f7f70c1
PF
380 reg2 &= ~QSPI_SMPR_DDRSMP_MASK;
381 reg2 |= (2 << QSPI_SMPR_DDRSMP_SHIFT);
5bc48308 382 qspi_write32(priv->flags, &regs->smpr, reg2);
5f7f70c1
PF
383
384 /* Enable the module again (enable the DDR too) */
385 reg |= QSPI_MCR_DDR_EN_MASK;
386 /* Enable bit 29 for imx6sx */
29e6abd9 387 reg |= BIT(29);
5f7f70c1 388
5bc48308 389 qspi_write32(priv->flags, &regs->mcr, reg);
5f7f70c1
PF
390}
391
392/*
393 * There are two different ways to read out the data from the flash:
394 * the "IP Command Read" and the "AHB Command Read".
395 *
396 * The IC guy suggests we use the "AHB Command Read" which is faster
397 * then the "IP Command Read". (What's more is that there is a bug in
398 * the "IP Command Read" in the Vybrid.)
399 *
400 * After we set up the registers for the "AHB Command Read", we can use
401 * the memcpy to read the data directly. A "missed" access to the buffer
402 * causes the controller to clear the buffer, and use the sequence pointed
403 * by the QUADSPI_BFGENCR[SEQID] to initiate a read from the flash.
404 */
5bc48308 405static void qspi_init_ahb_read(struct fsl_qspi_priv *priv)
5f7f70c1 406{
5bc48308
HW
407 struct fsl_qspi_regs *regs = priv->regs;
408
5f7f70c1 409 /* AHB configuration for access buffer 0/1/2 .*/
5bc48308
HW
410 qspi_write32(priv->flags, &regs->buf0cr, QSPI_BUFXCR_INVALID_MSTRID);
411 qspi_write32(priv->flags, &regs->buf1cr, QSPI_BUFXCR_INVALID_MSTRID);
412 qspi_write32(priv->flags, &regs->buf2cr, QSPI_BUFXCR_INVALID_MSTRID);
413 qspi_write32(priv->flags, &regs->buf3cr, QSPI_BUF3CR_ALLMST_MASK |
5f7f70c1
PF
414 (0x80 << QSPI_BUF3CR_ADATSZ_SHIFT));
415
416 /* We only use the buffer3 */
5bc48308
HW
417 qspi_write32(priv->flags, &regs->buf0ind, 0);
418 qspi_write32(priv->flags, &regs->buf1ind, 0);
419 qspi_write32(priv->flags, &regs->buf2ind, 0);
5f7f70c1
PF
420
421 /*
422 * Set the default lut sequence for AHB Read.
423 * Parallel mode is disabled.
424 */
5bc48308 425 qspi_write32(priv->flags, &regs->bfgencr,
5f7f70c1
PF
426 SEQID_FAST_READ << QSPI_BFGENCR_SEQID_SHIFT);
427
428 /*Enable DDR Mode*/
5bc48308 429 qspi_enable_ddr_mode(priv);
5f7f70c1
PF
430}
431#endif
432
a2358783
PF
433#ifdef CONFIG_SPI_FLASH_BAR
434/* Bank register read/write, EAR register read/write */
5bc48308 435static void qspi_op_rdbank(struct fsl_qspi_priv *priv, u8 *rxbuf, u32 len)
a2358783 436{
5bc48308 437 struct fsl_qspi_regs *regs = priv->regs;
a2358783
PF
438 u32 reg, mcr_reg, data, seqid;
439
5bc48308
HW
440 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
441 qspi_write32(priv->flags, &regs->mcr,
442 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
a2358783 443 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
5bc48308 444 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
a2358783 445
5bc48308 446 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
a2358783 447
5bc48308 448 if (priv->cur_seqid == QSPI_CMD_BRRD)
a2358783
PF
449 seqid = SEQID_BRRD;
450 else
451 seqid = SEQID_RDEAR;
452
5bc48308
HW
453 qspi_write32(priv->flags, &regs->ipcr,
454 (seqid << QSPI_IPCR_SEQID_SHIFT) | len);
a2358783
PF
455
456 /* Wait previous command complete */
5bc48308 457 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
a2358783
PF
458 ;
459
460 while (1) {
5bc48308 461 reg = qspi_read32(priv->flags, &regs->rbsr);
a2358783 462 if (reg & QSPI_RBSR_RDBFL_MASK) {
5bc48308 463 data = qspi_read32(priv->flags, &regs->rbdr[0]);
a2358783
PF
464 data = qspi_endian_xchg(data);
465 memcpy(rxbuf, &data, len);
5bc48308
HW
466 qspi_write32(priv->flags, &regs->mcr,
467 qspi_read32(priv->flags, &regs->mcr) |
a2358783
PF
468 QSPI_MCR_CLR_RXF_MASK);
469 break;
470 }
471 }
472
5bc48308 473 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
a2358783
PF
474}
475#endif
476
5bc48308 477static void qspi_op_rdid(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len)
6b57ff6f 478{
5bc48308 479 struct fsl_qspi_regs *regs = priv->regs;
5207014d
GQ
480 u32 mcr_reg, rbsr_reg, data, size;
481 int i;
6b57ff6f 482
5bc48308
HW
483 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
484 qspi_write32(priv->flags, &regs->mcr,
485 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
486 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
487 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
6b57ff6f 488
5bc48308 489 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
6b57ff6f 490
5bc48308
HW
491 qspi_write32(priv->flags, &regs->ipcr,
492 (SEQID_RDID << QSPI_IPCR_SEQID_SHIFT) | 0);
493 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
6b57ff6f
AW
494 ;
495
496 i = 0;
5207014d 497 while ((RX_BUFFER_SIZE >= len) && (len > 0)) {
5bc48308 498 rbsr_reg = qspi_read32(priv->flags, &regs->rbsr);
6b57ff6f 499 if (rbsr_reg & QSPI_RBSR_RDBFL_MASK) {
5bc48308 500 data = qspi_read32(priv->flags, &regs->rbdr[i]);
6b57ff6f 501 data = qspi_endian_xchg(data);
5207014d
GQ
502 size = (len < 4) ? len : 4;
503 memcpy(rxbuf, &data, size);
504 len -= size;
6b57ff6f 505 rxbuf++;
6b57ff6f
AW
506 i++;
507 }
508 }
509
5bc48308 510 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
6b57ff6f
AW
511}
512
5f7f70c1
PF
513#ifndef CONFIG_SYS_FSL_QSPI_AHB
514/* If not use AHB read, read data from ip interface */
5bc48308 515static void qspi_op_read(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len)
6b57ff6f 516{
5bc48308 517 struct fsl_qspi_regs *regs = priv->regs;
6b57ff6f
AW
518 u32 mcr_reg, data;
519 int i, size;
520 u32 to_or_from;
521
5bc48308
HW
522 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
523 qspi_write32(priv->flags, &regs->mcr,
524 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
525 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
526 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
6b57ff6f 527
5bc48308 528 to_or_from = priv->sf_addr + priv->cur_amba_base;
6b57ff6f
AW
529
530 while (len > 0) {
beedbc2e
AS
531 WATCHDOG_RESET();
532
5bc48308 533 qspi_write32(priv->flags, &regs->sfar, to_or_from);
6b57ff6f
AW
534
535 size = (len > RX_BUFFER_SIZE) ?
536 RX_BUFFER_SIZE : len;
537
5bc48308
HW
538 qspi_write32(priv->flags, &regs->ipcr,
539 (SEQID_FAST_READ << QSPI_IPCR_SEQID_SHIFT) |
540 size);
541 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
6b57ff6f
AW
542 ;
543
544 to_or_from += size;
545 len -= size;
546
547 i = 0;
548 while ((RX_BUFFER_SIZE >= size) && (size > 0)) {
5bc48308 549 data = qspi_read32(priv->flags, &regs->rbdr[i]);
6b57ff6f
AW
550 data = qspi_endian_xchg(data);
551 memcpy(rxbuf, &data, 4);
552 rxbuf++;
553 size -= 4;
554 i++;
555 }
5bc48308
HW
556 qspi_write32(priv->flags, &regs->mcr,
557 qspi_read32(priv->flags, &regs->mcr) |
558 QSPI_MCR_CLR_RXF_MASK);
6b57ff6f
AW
559 }
560
5bc48308 561 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
6b57ff6f 562}
5f7f70c1 563#endif
6b57ff6f 564
5bc48308 565static void qspi_op_write(struct fsl_qspi_priv *priv, u8 *txbuf, u32 len)
6b57ff6f 566{
5bc48308 567 struct fsl_qspi_regs *regs = priv->regs;
a2358783 568 u32 mcr_reg, data, reg, status_reg, seqid;
6b57ff6f
AW
569 int i, size, tx_size;
570 u32 to_or_from = 0;
571
5bc48308
HW
572 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
573 qspi_write32(priv->flags, &regs->mcr,
574 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
575 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
576 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
6b57ff6f
AW
577
578 status_reg = 0;
579 while ((status_reg & FLASH_STATUS_WEL) != FLASH_STATUS_WEL) {
beedbc2e
AS
580 WATCHDOG_RESET();
581
5bc48308
HW
582 qspi_write32(priv->flags, &regs->ipcr,
583 (SEQID_WREN << QSPI_IPCR_SEQID_SHIFT) | 0);
584 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
6b57ff6f
AW
585 ;
586
5bc48308
HW
587 qspi_write32(priv->flags, &regs->ipcr,
588 (SEQID_RDSR << QSPI_IPCR_SEQID_SHIFT) | 1);
589 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
6b57ff6f
AW
590 ;
591
5bc48308 592 reg = qspi_read32(priv->flags, &regs->rbsr);
6b57ff6f 593 if (reg & QSPI_RBSR_RDBFL_MASK) {
5bc48308 594 status_reg = qspi_read32(priv->flags, &regs->rbdr[0]);
6b57ff6f
AW
595 status_reg = qspi_endian_xchg(status_reg);
596 }
5bc48308
HW
597 qspi_write32(priv->flags, &regs->mcr,
598 qspi_read32(priv->flags, &regs->mcr) |
599 QSPI_MCR_CLR_RXF_MASK);
6b57ff6f
AW
600 }
601
a2358783
PF
602 /* Default is page programming */
603 seqid = SEQID_PP;
604#ifdef CONFIG_SPI_FLASH_BAR
5bc48308 605 if (priv->cur_seqid == QSPI_CMD_BRWR)
a2358783 606 seqid = SEQID_BRWR;
5bc48308 607 else if (priv->cur_seqid == QSPI_CMD_WREAR)
a2358783
PF
608 seqid = SEQID_WREAR;
609#endif
610
5bc48308 611 to_or_from = priv->sf_addr + priv->cur_amba_base;
a2358783 612
5bc48308 613 qspi_write32(priv->flags, &regs->sfar, to_or_from);
6b57ff6f
AW
614
615 tx_size = (len > TX_BUFFER_SIZE) ?
616 TX_BUFFER_SIZE : len;
617
a2358783 618 size = tx_size / 4;
6b57ff6f 619 for (i = 0; i < size; i++) {
a2358783
PF
620 memcpy(&data, txbuf, 4);
621 data = qspi_endian_xchg(data);
5bc48308 622 qspi_write32(priv->flags, &regs->tbdr, data);
a2358783 623 txbuf += 4;
6b57ff6f
AW
624 }
625
a2358783
PF
626 size = tx_size % 4;
627 if (size) {
628 data = 0;
629 memcpy(&data, txbuf, size);
630 data = qspi_endian_xchg(data);
5bc48308 631 qspi_write32(priv->flags, &regs->tbdr, data);
a2358783
PF
632 }
633
5bc48308
HW
634 qspi_write32(priv->flags, &regs->ipcr,
635 (seqid << QSPI_IPCR_SEQID_SHIFT) | tx_size);
636 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
6b57ff6f
AW
637 ;
638
5bc48308 639 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
6b57ff6f
AW
640}
641
940d2b89 642static void qspi_op_rdsr(struct fsl_qspi_priv *priv, void *rxbuf, u32 len)
6b57ff6f 643{
5bc48308 644 struct fsl_qspi_regs *regs = priv->regs;
6b57ff6f
AW
645 u32 mcr_reg, reg, data;
646
5bc48308
HW
647 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
648 qspi_write32(priv->flags, &regs->mcr,
649 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
650 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
651 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
6b57ff6f 652
5bc48308 653 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
6b57ff6f 654
5bc48308
HW
655 qspi_write32(priv->flags, &regs->ipcr,
656 (SEQID_RDSR << QSPI_IPCR_SEQID_SHIFT) | 0);
657 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
6b57ff6f
AW
658 ;
659
660 while (1) {
5bc48308 661 reg = qspi_read32(priv->flags, &regs->rbsr);
6b57ff6f 662 if (reg & QSPI_RBSR_RDBFL_MASK) {
5bc48308 663 data = qspi_read32(priv->flags, &regs->rbdr[0]);
6b57ff6f 664 data = qspi_endian_xchg(data);
940d2b89 665 memcpy(rxbuf, &data, len);
5bc48308
HW
666 qspi_write32(priv->flags, &regs->mcr,
667 qspi_read32(priv->flags, &regs->mcr) |
668 QSPI_MCR_CLR_RXF_MASK);
6b57ff6f
AW
669 break;
670 }
671 }
672
5bc48308 673 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
6b57ff6f
AW
674}
675
5bc48308 676static void qspi_op_erase(struct fsl_qspi_priv *priv)
6b57ff6f 677{
5bc48308 678 struct fsl_qspi_regs *regs = priv->regs;
6b57ff6f
AW
679 u32 mcr_reg;
680 u32 to_or_from = 0;
681
5bc48308
HW
682 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
683 qspi_write32(priv->flags, &regs->mcr,
684 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
685 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
686 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
6b57ff6f 687
5bc48308
HW
688 to_or_from = priv->sf_addr + priv->cur_amba_base;
689 qspi_write32(priv->flags, &regs->sfar, to_or_from);
6b57ff6f 690
5bc48308
HW
691 qspi_write32(priv->flags, &regs->ipcr,
692 (SEQID_WREN << QSPI_IPCR_SEQID_SHIFT) | 0);
693 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
6b57ff6f
AW
694 ;
695
5bc48308
HW
696 if (priv->cur_seqid == QSPI_CMD_SE) {
697 qspi_write32(priv->flags, &regs->ipcr,
ba4dc8ab 698 (SEQID_SE << QSPI_IPCR_SEQID_SHIFT) | 0);
5bc48308
HW
699 } else if (priv->cur_seqid == QSPI_CMD_BE_4K) {
700 qspi_write32(priv->flags, &regs->ipcr,
ba4dc8ab
PF
701 (SEQID_BE_4K << QSPI_IPCR_SEQID_SHIFT) | 0);
702 }
5bc48308 703 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
6b57ff6f
AW
704 ;
705
5bc48308 706 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
6b57ff6f
AW
707}
708
5bc48308 709int qspi_xfer(struct fsl_qspi_priv *priv, unsigned int bitlen,
6b57ff6f
AW
710 const void *dout, void *din, unsigned long flags)
711{
6b57ff6f 712 u32 bytes = DIV_ROUND_UP(bitlen, 8);
a2358783 713 static u32 wr_sfaddr;
6b57ff6f
AW
714 u32 txbuf;
715
716 if (dout) {
a2358783 717 if (flags & SPI_XFER_BEGIN) {
5bc48308 718 priv->cur_seqid = *(u8 *)dout;
a2358783
PF
719 memcpy(&txbuf, dout, 4);
720 }
6b57ff6f
AW
721
722 if (flags == SPI_XFER_END) {
5bc48308
HW
723 priv->sf_addr = wr_sfaddr;
724 qspi_op_write(priv, (u8 *)dout, bytes);
6b57ff6f
AW
725 return 0;
726 }
727
5bc48308
HW
728 if (priv->cur_seqid == QSPI_CMD_FAST_READ) {
729 priv->sf_addr = swab32(txbuf) & OFFSET_BITS_MASK;
730 } else if ((priv->cur_seqid == QSPI_CMD_SE) ||
731 (priv->cur_seqid == QSPI_CMD_BE_4K)) {
732 priv->sf_addr = swab32(txbuf) & OFFSET_BITS_MASK;
733 qspi_op_erase(priv);
734 } else if (priv->cur_seqid == QSPI_CMD_PP) {
a2358783 735 wr_sfaddr = swab32(txbuf) & OFFSET_BITS_MASK;
5bc48308
HW
736 } else if ((priv->cur_seqid == QSPI_CMD_BRWR) ||
737 (priv->cur_seqid == QSPI_CMD_WREAR)) {
a2358783 738#ifdef CONFIG_SPI_FLASH_BAR
a2358783 739 wr_sfaddr = 0;
a2358783 740#endif
5bc48308 741 }
6b57ff6f
AW
742 }
743
744 if (din) {
5bc48308 745 if (priv->cur_seqid == QSPI_CMD_FAST_READ) {
5f7f70c1 746#ifdef CONFIG_SYS_FSL_QSPI_AHB
5bc48308 747 qspi_ahb_read(priv, din, bytes);
5f7f70c1 748#else
5bc48308 749 qspi_op_read(priv, din, bytes);
5f7f70c1 750#endif
5bc48308
HW
751 } else if (priv->cur_seqid == QSPI_CMD_RDID)
752 qspi_op_rdid(priv, din, bytes);
753 else if (priv->cur_seqid == QSPI_CMD_RDSR)
940d2b89 754 qspi_op_rdsr(priv, din, bytes);
a2358783 755#ifdef CONFIG_SPI_FLASH_BAR
5bc48308
HW
756 else if ((priv->cur_seqid == QSPI_CMD_BRRD) ||
757 (priv->cur_seqid == QSPI_CMD_RDEAR)) {
758 priv->sf_addr = 0;
759 qspi_op_rdbank(priv, din, bytes);
a2358783
PF
760 }
761#endif
6b57ff6f
AW
762 }
763
5f7f70c1 764#ifdef CONFIG_SYS_FSL_QSPI_AHB
5bc48308
HW
765 if ((priv->cur_seqid == QSPI_CMD_SE) ||
766 (priv->cur_seqid == QSPI_CMD_PP) ||
767 (priv->cur_seqid == QSPI_CMD_BE_4K) ||
768 (priv->cur_seqid == QSPI_CMD_WREAR) ||
769 (priv->cur_seqid == QSPI_CMD_BRWR))
770 qspi_ahb_invalid(priv);
771#endif
772
773 return 0;
774}
775
776void qspi_module_disable(struct fsl_qspi_priv *priv, u8 disable)
777{
778 u32 mcr_val;
779
780 mcr_val = qspi_read32(priv->flags, &priv->regs->mcr);
781 if (disable)
782 mcr_val |= QSPI_MCR_MDIS_MASK;
783 else
784 mcr_val &= ~QSPI_MCR_MDIS_MASK;
785 qspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
786}
787
788void qspi_cfg_smpr(struct fsl_qspi_priv *priv, u32 clear_bits, u32 set_bits)
789{
790 u32 smpr_val;
791
792 smpr_val = qspi_read32(priv->flags, &priv->regs->smpr);
793 smpr_val &= ~clear_bits;
794 smpr_val |= set_bits;
795 qspi_write32(priv->flags, &priv->regs->smpr, smpr_val);
796}
797#ifndef CONFIG_DM_SPI
798static unsigned long spi_bases[] = {
799 QSPI0_BASE_ADDR,
800#ifdef CONFIG_MX6SX
801 QSPI1_BASE_ADDR,
802#endif
803};
804
805static unsigned long amba_bases[] = {
806 QSPI0_AMBA_BASE,
807#ifdef CONFIG_MX6SX
808 QSPI1_AMBA_BASE,
809#endif
810};
811
812static inline struct fsl_qspi *to_qspi_spi(struct spi_slave *slave)
813{
814 return container_of(slave, struct fsl_qspi, slave);
815}
816
817struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
818 unsigned int max_hz, unsigned int mode)
819{
820 struct fsl_qspi *qspi;
821 struct fsl_qspi_regs *regs;
822 u32 total_size;
823
824 if (bus >= ARRAY_SIZE(spi_bases))
825 return NULL;
826
827 if (cs >= FSL_QSPI_FLASH_NUM)
828 return NULL;
829
830 qspi = spi_alloc_slave(struct fsl_qspi, bus, cs);
831 if (!qspi)
832 return NULL;
833
834#ifdef CONFIG_SYS_FSL_QSPI_BE
835 qspi->priv.flags |= QSPI_FLAG_REGMAP_ENDIAN_BIG;
836#endif
837
838 regs = (struct fsl_qspi_regs *)spi_bases[bus];
839 qspi->priv.regs = regs;
840 /*
841 * According cs, use different amba_base to choose the
842 * corresponding flash devices.
843 *
844 * If not, only one flash device is used even if passing
845 * different cs using `sf probe`
846 */
847 qspi->priv.cur_amba_base = amba_bases[bus] + cs * FSL_QSPI_FLASH_SIZE;
848
849 qspi->slave.max_write_size = TX_BUFFER_SIZE;
850
851 qspi_write32(qspi->priv.flags, &regs->mcr,
852 QSPI_MCR_RESERVED_MASK | QSPI_MCR_MDIS_MASK);
853
854 qspi_cfg_smpr(&qspi->priv,
855 ~(QSPI_SMPR_FSDLY_MASK | QSPI_SMPR_DDRSMP_MASK |
856 QSPI_SMPR_FSPHS_MASK | QSPI_SMPR_HSENA_MASK), 0);
857
858 total_size = FSL_QSPI_FLASH_SIZE * FSL_QSPI_FLASH_NUM;
859 /*
860 * Any read access to non-implemented addresses will provide
861 * undefined results.
862 *
863 * In case single die flash devices, TOP_ADDR_MEMA2 and
864 * TOP_ADDR_MEMB2 should be initialized/programmed to
865 * TOP_ADDR_MEMA1 and TOP_ADDR_MEMB1 respectively - in effect,
866 * setting the size of these devices to 0. This would ensure
867 * that the complete memory map is assigned to only one flash device.
868 */
869 qspi_write32(qspi->priv.flags, &regs->sfa1ad,
870 FSL_QSPI_FLASH_SIZE | amba_bases[bus]);
871 qspi_write32(qspi->priv.flags, &regs->sfa2ad,
872 FSL_QSPI_FLASH_SIZE | amba_bases[bus]);
873 qspi_write32(qspi->priv.flags, &regs->sfb1ad,
874 total_size | amba_bases[bus]);
875 qspi_write32(qspi->priv.flags, &regs->sfb2ad,
876 total_size | amba_bases[bus]);
877
878 qspi_set_lut(&qspi->priv);
879
880#ifdef CONFIG_SYS_FSL_QSPI_AHB
881 qspi_init_ahb_read(&qspi->priv);
5f7f70c1
PF
882#endif
883
5bc48308
HW
884 qspi_module_disable(&qspi->priv, 0);
885
886 return &qspi->slave;
887}
888
889void spi_free_slave(struct spi_slave *slave)
890{
891 struct fsl_qspi *qspi = to_qspi_spi(slave);
892
893 free(qspi);
894}
895
896int spi_claim_bus(struct spi_slave *slave)
897{
6b57ff6f
AW
898 return 0;
899}
900
901void spi_release_bus(struct spi_slave *slave)
902{
903 /* Nothing to do */
904}
5bc48308
HW
905
906int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
907 const void *dout, void *din, unsigned long flags)
908{
909 struct fsl_qspi *qspi = to_qspi_spi(slave);
910
911 return qspi_xfer(&qspi->priv, bitlen, dout, din, flags);
912}
913
914void spi_init(void)
915{
916 /* Nothing to do */
917}
918#else
919static int fsl_qspi_child_pre_probe(struct udevice *dev)
920{
bcbe3d15 921 struct spi_slave *slave = dev_get_parent_priv(dev);
5bc48308
HW
922
923 slave->max_write_size = TX_BUFFER_SIZE;
924
925 return 0;
926}
927
928static int fsl_qspi_probe(struct udevice *bus)
929{
4e147418 930 u32 amba_size_per_chip;
5bc48308
HW
931 struct fsl_qspi_platdata *plat = dev_get_platdata(bus);
932 struct fsl_qspi_priv *priv = dev_get_priv(bus);
933 struct dm_spi_bus *dm_spi_bus;
4e147418 934 int i;
5bc48308
HW
935
936 dm_spi_bus = bus->uclass_priv;
937
938 dm_spi_bus->max_hz = plat->speed_hz;
939
c2a4cb17 940 priv->regs = (struct fsl_qspi_regs *)(uintptr_t)plat->reg_base;
5bc48308
HW
941 priv->flags = plat->flags;
942
943 priv->speed_hz = plat->speed_hz;
bf9bffa9
YY
944 /*
945 * QSPI SFADR width is 32bits, the max dest addr is 4GB-1.
946 * AMBA memory zone should be located on the 0~4GB space
947 * even on a 64bits cpu.
948 */
949 priv->amba_base[0] = (u32)plat->amba_base;
950 priv->amba_total_size = (u32)plat->amba_total_size;
5bc48308
HW
951 priv->flash_num = plat->flash_num;
952 priv->num_chipselect = plat->num_chipselect;
953
954 qspi_write32(priv->flags, &priv->regs->mcr,
955 QSPI_MCR_RESERVED_MASK | QSPI_MCR_MDIS_MASK);
956
957 qspi_cfg_smpr(priv, ~(QSPI_SMPR_FSDLY_MASK | QSPI_SMPR_DDRSMP_MASK |
958 QSPI_SMPR_FSPHS_MASK | QSPI_SMPR_HSENA_MASK), 0);
959
4e147418
YY
960 /*
961 * Assign AMBA memory zone for every chipselect
962 * QuadSPI has two channels, every channel has two chipselects.
963 * If the property 'num-cs' in dts is 2, the AMBA memory will be divided
964 * into two parts and assign to every channel. This indicate that every
965 * channel only has one valid chipselect.
966 * If the property 'num-cs' in dts is 4, the AMBA memory will be divided
967 * into four parts and assign to every chipselect.
968 * Every channel will has two valid chipselects.
969 */
970 amba_size_per_chip = priv->amba_total_size >>
971 (priv->num_chipselect >> 1);
972 for (i = 1 ; i < priv->num_chipselect ; i++)
973 priv->amba_base[i] =
974 amba_size_per_chip + priv->amba_base[i - 1];
975
5bc48308
HW
976 /*
977 * Any read access to non-implemented addresses will provide
978 * undefined results.
979 *
980 * In case single die flash devices, TOP_ADDR_MEMA2 and
981 * TOP_ADDR_MEMB2 should be initialized/programmed to
982 * TOP_ADDR_MEMA1 and TOP_ADDR_MEMB1 respectively - in effect,
983 * setting the size of these devices to 0. This would ensure
984 * that the complete memory map is assigned to only one flash device.
985 */
4e147418
YY
986 qspi_write32(priv->flags, &priv->regs->sfa1ad, priv->amba_base[1]);
987 switch (priv->num_chipselect) {
988 case 2:
989 qspi_write32(priv->flags, &priv->regs->sfa2ad,
990 priv->amba_base[1]);
991 qspi_write32(priv->flags, &priv->regs->sfb1ad,
992 priv->amba_base[1] + amba_size_per_chip);
993 qspi_write32(priv->flags, &priv->regs->sfb2ad,
994 priv->amba_base[1] + amba_size_per_chip);
995 break;
996 case 4:
997 qspi_write32(priv->flags, &priv->regs->sfa2ad,
998 priv->amba_base[2]);
999 qspi_write32(priv->flags, &priv->regs->sfb1ad,
1000 priv->amba_base[3]);
1001 qspi_write32(priv->flags, &priv->regs->sfb2ad,
1002 priv->amba_base[3] + amba_size_per_chip);
1003 break;
1004 default:
1005 debug("Error: Unsupported chipselect number %u!\n",
1006 priv->num_chipselect);
1007 qspi_module_disable(priv, 1);
1008 return -EINVAL;
1009 }
5bc48308
HW
1010
1011 qspi_set_lut(priv);
1012
1013#ifdef CONFIG_SYS_FSL_QSPI_AHB
1014 qspi_init_ahb_read(priv);
1015#endif
1016
1017 qspi_module_disable(priv, 0);
1018
1019 return 0;
1020}
1021
1022static int fsl_qspi_ofdata_to_platdata(struct udevice *bus)
1023{
bf9bffa9 1024 struct fdt_resource res_regs, res_mem;
5bc48308
HW
1025 struct fsl_qspi_platdata *plat = bus->platdata;
1026 const void *blob = gd->fdt_blob;
1027 int node = bus->of_offset;
1028 int ret, flash_num = 0, subnode;
1029
1030 if (fdtdec_get_bool(blob, node, "big-endian"))
1031 plat->flags |= QSPI_FLAG_REGMAP_ENDIAN_BIG;
1032
bf9bffa9
YY
1033 ret = fdt_get_named_resource(blob, node, "reg", "reg-names",
1034 "QuadSPI", &res_regs);
1035 if (ret) {
1036 debug("Error: can't get regs base addresses(ret = %d)!\n", ret);
1037 return -ENOMEM;
1038 }
1039 ret = fdt_get_named_resource(blob, node, "reg", "reg-names",
1040 "QuadSPI-memory", &res_mem);
5bc48308 1041 if (ret) {
bf9bffa9 1042 debug("Error: can't get AMBA base addresses(ret = %d)!\n", ret);
5bc48308
HW
1043 return -ENOMEM;
1044 }
1045
1046 /* Count flash numbers */
1047 fdt_for_each_subnode(blob, subnode, node)
1048 ++flash_num;
1049
1050 if (flash_num == 0) {
1051 debug("Error: Missing flashes!\n");
1052 return -ENODEV;
1053 }
1054
1055 plat->speed_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
1056 FSL_QSPI_DEFAULT_SCK_FREQ);
1057 plat->num_chipselect = fdtdec_get_int(blob, node, "num-cs",
1058 FSL_QSPI_MAX_CHIPSELECT_NUM);
1059
bf9bffa9
YY
1060 plat->reg_base = res_regs.start;
1061 plat->amba_base = res_mem.start;
1062 plat->amba_total_size = res_mem.end - res_mem.start + 1;
5bc48308
HW
1063 plat->flash_num = flash_num;
1064
bf9bffa9 1065 debug("%s: regs=<0x%llx> <0x%llx, 0x%llx>, max-frequency=%d, endianess=%s\n",
5bc48308 1066 __func__,
bf9bffa9
YY
1067 (u64)plat->reg_base,
1068 (u64)plat->amba_base,
1069 (u64)plat->amba_total_size,
5bc48308
HW
1070 plat->speed_hz,
1071 plat->flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ? "be" : "le"
1072 );
1073
1074 return 0;
1075}
1076
1077static int fsl_qspi_xfer(struct udevice *dev, unsigned int bitlen,
1078 const void *dout, void *din, unsigned long flags)
1079{
1080 struct fsl_qspi_priv *priv;
1081 struct udevice *bus;
1082
1083 bus = dev->parent;
1084 priv = dev_get_priv(bus);
1085
1086 return qspi_xfer(priv, bitlen, dout, din, flags);
1087}
1088
1089static int fsl_qspi_claim_bus(struct udevice *dev)
1090{
1091 struct fsl_qspi_priv *priv;
1092 struct udevice *bus;
1093 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
1094
1095 bus = dev->parent;
1096 priv = dev_get_priv(bus);
1097
4e147418 1098 priv->cur_amba_base = priv->amba_base[slave_plat->cs];
5bc48308
HW
1099
1100 qspi_module_disable(priv, 0);
1101
1102 return 0;
1103}
1104
1105static int fsl_qspi_release_bus(struct udevice *dev)
1106{
1107 struct fsl_qspi_priv *priv;
1108 struct udevice *bus;
1109
1110 bus = dev->parent;
1111 priv = dev_get_priv(bus);
1112
1113 qspi_module_disable(priv, 1);
1114
1115 return 0;
1116}
1117
1118static int fsl_qspi_set_speed(struct udevice *bus, uint speed)
1119{
1120 /* Nothing to do */
1121 return 0;
1122}
1123
1124static int fsl_qspi_set_mode(struct udevice *bus, uint mode)
1125{
1126 /* Nothing to do */
1127 return 0;
1128}
1129
1130static const struct dm_spi_ops fsl_qspi_ops = {
1131 .claim_bus = fsl_qspi_claim_bus,
1132 .release_bus = fsl_qspi_release_bus,
1133 .xfer = fsl_qspi_xfer,
1134 .set_speed = fsl_qspi_set_speed,
1135 .set_mode = fsl_qspi_set_mode,
1136};
1137
1138static const struct udevice_id fsl_qspi_ids[] = {
1139 { .compatible = "fsl,vf610-qspi" },
1140 { .compatible = "fsl,imx6sx-qspi" },
1141 { }
1142};
1143
1144U_BOOT_DRIVER(fsl_qspi) = {
1145 .name = "fsl_qspi",
1146 .id = UCLASS_SPI,
1147 .of_match = fsl_qspi_ids,
1148 .ops = &fsl_qspi_ops,
1149 .ofdata_to_platdata = fsl_qspi_ofdata_to_platdata,
1150 .platdata_auto_alloc_size = sizeof(struct fsl_qspi_platdata),
1151 .priv_auto_alloc_size = sizeof(struct fsl_qspi_priv),
1152 .probe = fsl_qspi_probe,
1153 .child_pre_probe = fsl_qspi_child_pre_probe,
1154};
1155#endif
This page took 0.357486 seconds and 4 git commands to generate.