blob: 8e2a09df3661293caa4fdd3a01c030a1bced73bf [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Alison Wangc7410e32014-05-06 09:13:01 +08002/*
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08003 * Copyright 2013-2015 Freescale Semiconductor, Inc.
Alison Wangc7410e32014-05-06 09:13:01 +08004 *
5 * Freescale Quad Serial Peripheral Interface (QSPI) driver
Alison Wangc7410e32014-05-06 09:13:01 +08006 */
7
8#include <common.h>
9#include <malloc.h>
10#include <spi.h>
11#include <asm/io.h>
12#include <linux/sizes.h>
Thomas Schaeferf751d852019-07-01 17:37:35 +020013#include <linux/iopoll.h>
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080014#include <dm.h>
15#include <errno.h>
Alexander Stein9b2c8a92015-11-04 09:19:10 +010016#include <watchdog.h>
Suresh Gupta4945b872017-08-30 20:06:33 +053017#include <wait_bit.h>
Alison Wangc7410e32014-05-06 09:13:01 +080018#include "fsl_qspi.h"
19
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080020DECLARE_GLOBAL_DATA_PTR;
21
Gong Qianyu44f65f82015-12-14 18:26:25 +080022#define OFFSET_BITS_MASK GENMASK(23, 0)
Alison Wangc7410e32014-05-06 09:13:01 +080023
24#define FLASH_STATUS_WEL 0x02
25
26/* SEQID */
27#define SEQID_WREN 1
28#define SEQID_FAST_READ 2
29#define SEQID_RDSR 3
30#define SEQID_SE 4
31#define SEQID_CHIP_ERASE 5
32#define SEQID_PP 6
33#define SEQID_RDID 7
Peng Fan3642a872014-12-31 11:01:39 +080034#define SEQID_BE_4K 8
Peng Fan3a344482015-01-04 17:07:14 +080035#ifdef CONFIG_SPI_FLASH_BAR
36#define SEQID_BRRD 9
37#define SEQID_BRWR 10
38#define SEQID_RDEAR 11
39#define SEQID_WREAR 12
40#endif
Yuan Yaod7193262016-03-15 14:36:42 +080041#define SEQID_WRAR 13
42#define SEQID_RDAR 14
Alison Wangc7410e32014-05-06 09:13:01 +080043
Peng Fanff162342014-12-31 11:01:36 +080044/* QSPI CMD */
45#define QSPI_CMD_PP 0x02 /* Page program (up to 256 bytes) */
46#define QSPI_CMD_RDSR 0x05 /* Read status register */
47#define QSPI_CMD_WREN 0x06 /* Write enable */
48#define QSPI_CMD_FAST_READ 0x0b /* Read data bytes (high frequency) */
Peng Fan3642a872014-12-31 11:01:39 +080049#define QSPI_CMD_BE_4K 0x20 /* 4K erase */
Peng Fanff162342014-12-31 11:01:36 +080050#define QSPI_CMD_CHIP_ERASE 0xc7 /* Erase whole flash chip */
51#define QSPI_CMD_SE 0xd8 /* Sector erase (usually 64KiB) */
52#define QSPI_CMD_RDID 0x9f /* Read JEDEC ID */
Alison Wangc7410e32014-05-06 09:13:01 +080053
Peng Fan3a344482015-01-04 17:07:14 +080054/* Used for Micron, winbond and Macronix flashes */
55#define QSPI_CMD_WREAR 0xc5 /* EAR register write */
56#define QSPI_CMD_RDEAR 0xc8 /* EAR reigster read */
57
58/* Used for Spansion flashes only. */
59#define QSPI_CMD_BRRD 0x16 /* Bank register read */
60#define QSPI_CMD_BRWR 0x17 /* Bank register write */
61
Yuan Yaod7193262016-03-15 14:36:42 +080062/* Used for Spansion S25FS-S family flash only. */
63#define QSPI_CMD_RDAR 0x65 /* Read any device register */
64#define QSPI_CMD_WRAR 0x71 /* Write any device register */
65
Peng Fanff162342014-12-31 11:01:36 +080066/* 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) */
Alison Wangc7410e32014-05-06 09:13:01 +080070
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080071/* fsl_qspi_platdata flags */
Jagan Tekic97ca922015-10-23 01:37:18 +053072#define QSPI_FLAG_REGMAP_ENDIAN_BIG BIT(0)
Alison Wangc7410e32014-05-06 09:13:01 +080073
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080074/* default SCK frequency, unit: HZ */
75#define FSL_QSPI_DEFAULT_SCK_FREQ 50000000
Alison Wangc7410e32014-05-06 09:13:01 +080076
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080077/* QSPI max chipselect signals number */
78#define FSL_QSPI_MAX_CHIPSELECT_NUM 4
79
Ye Li007b6042019-08-14 11:31:36 +000080/* Controller needs driver to swap endian */
81#define QUADSPI_QUIRK_SWAP_ENDIAN BIT(0)
82
83enum fsl_qspi_devtype {
84 FSL_QUADSPI_VYBRID,
85 FSL_QUADSPI_IMX6SX,
86 FSL_QUADSPI_IMX6UL_7D,
Ye Li57f67752019-08-14 11:31:40 +000087 FSL_QUADSPI_IMX7ULP,
Ye Li007b6042019-08-14 11:31:36 +000088};
89
90struct fsl_qspi_devtype_data {
91 enum fsl_qspi_devtype devtype;
92 u32 rxfifo;
93 u32 txfifo;
94 u32 ahb_buf_size;
95 u32 driver_data;
96};
97
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080098/**
99 * struct fsl_qspi_platdata - platform data for Freescale QSPI
100 *
101 * @flags: Flags for QSPI QSPI_FLAG_...
102 * @speed_hz: Default SCK frequency
103 * @reg_base: Base address of QSPI registers
104 * @amba_base: Base address of QSPI memory mapping
105 * @amba_total_size: size of QSPI memory mapping
106 * @flash_num: Number of active slave devices
107 * @num_chipselect: Number of QSPI chipselect signals
108 */
109struct fsl_qspi_platdata {
110 u32 flags;
111 u32 speed_hz;
Yuan Yaoae412392016-03-15 14:36:40 +0800112 fdt_addr_t reg_base;
113 fdt_addr_t amba_base;
114 fdt_size_t amba_total_size;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800115 u32 flash_num;
116 u32 num_chipselect;
117};
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800118
119/**
120 * struct fsl_qspi_priv - private data for Freescale QSPI
121 *
122 * @flags: Flags for QSPI QSPI_FLAG_...
123 * @bus_clk: QSPI input clk frequency
124 * @speed_hz: Default SCK frequency
125 * @cur_seqid: current LUT table sequence id
126 * @sf_addr: flash access offset
127 * @amba_base: Base address of QSPI memory mapping of every CS
128 * @amba_total_size: size of QSPI memory mapping
129 * @cur_amba_base: Base address of QSPI memory mapping of current CS
130 * @flash_num: Number of active slave devices
131 * @num_chipselect: Number of QSPI chipselect signals
132 * @regs: Point to QSPI register structure for I/O access
133 */
134struct fsl_qspi_priv {
135 u32 flags;
136 u32 bus_clk;
137 u32 speed_hz;
138 u32 cur_seqid;
139 u32 sf_addr;
140 u32 amba_base[FSL_QSPI_MAX_CHIPSELECT_NUM];
141 u32 amba_total_size;
142 u32 cur_amba_base;
143 u32 flash_num;
144 u32 num_chipselect;
145 struct fsl_qspi_regs *regs;
Ye Li007b6042019-08-14 11:31:36 +0000146 struct fsl_qspi_devtype_data *devtype_data;
Alison Wangc7410e32014-05-06 09:13:01 +0800147};
148
Ye Li007b6042019-08-14 11:31:36 +0000149static const struct fsl_qspi_devtype_data vybrid_data = {
150 .devtype = FSL_QUADSPI_VYBRID,
151 .rxfifo = 128,
152 .txfifo = 64,
153 .ahb_buf_size = 1024,
154 .driver_data = QUADSPI_QUIRK_SWAP_ENDIAN,
155};
156
157static const struct fsl_qspi_devtype_data imx6sx_data = {
158 .devtype = FSL_QUADSPI_IMX6SX,
159 .rxfifo = 128,
160 .txfifo = 512,
161 .ahb_buf_size = 1024,
162 .driver_data = 0,
163};
164
165static const struct fsl_qspi_devtype_data imx6ul_7d_data = {
166 .devtype = FSL_QUADSPI_IMX6UL_7D,
167 .rxfifo = 128,
168 .txfifo = 512,
169 .ahb_buf_size = 1024,
170 .driver_data = 0,
171};
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800172
Ye Li57f67752019-08-14 11:31:40 +0000173static const struct fsl_qspi_devtype_data imx7ulp_data = {
174 .devtype = FSL_QUADSPI_IMX7ULP,
175 .rxfifo = 64,
176 .txfifo = 64,
177 .ahb_buf_size = 128,
178 .driver_data = 0,
179};
180
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800181static u32 qspi_read32(u32 flags, u32 *addr)
182{
183 return flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ?
184 in_be32(addr) : in_le32(addr);
185}
186
187static void qspi_write32(u32 flags, u32 *addr, u32 val)
188{
189 flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ?
190 out_be32(addr, val) : out_le32(addr, val);
191}
Alison Wangc7410e32014-05-06 09:13:01 +0800192
Rajat Srivastava234daec2018-03-22 13:30:55 +0530193static inline int is_controller_busy(const struct fsl_qspi_priv *priv)
194{
195 u32 val;
Thomas Schaeferf751d852019-07-01 17:37:35 +0200196 u32 mask = QSPI_SR_BUSY_MASK | QSPI_SR_AHB_ACC_MASK |
197 QSPI_SR_IP_ACC_MASK;
Rajat Srivastava234daec2018-03-22 13:30:55 +0530198
Thomas Schaeferf751d852019-07-01 17:37:35 +0200199 if (priv->flags & QSPI_FLAG_REGMAP_ENDIAN_BIG)
200 mask = (u32)cpu_to_be32(mask);
Rajat Srivastava234daec2018-03-22 13:30:55 +0530201
Thomas Schaeferf751d852019-07-01 17:37:35 +0200202 return readl_poll_timeout(&priv->regs->sr, val, !(val & mask), 1000);
Rajat Srivastava234daec2018-03-22 13:30:55 +0530203}
204
Alison Wangc7410e32014-05-06 09:13:01 +0800205/* QSPI support swapping the flash read/write data
206 * in hardware for LS102xA, but not for VF610 */
Ye Li007b6042019-08-14 11:31:36 +0000207static inline u32 qspi_endian_xchg(struct fsl_qspi_priv *priv, u32 data)
Alison Wangc7410e32014-05-06 09:13:01 +0800208{
Ye Li007b6042019-08-14 11:31:36 +0000209 if (priv->devtype_data->driver_data & QUADSPI_QUIRK_SWAP_ENDIAN)
210 return swab32(data);
211 else
212 return data;
Alison Wangc7410e32014-05-06 09:13:01 +0800213}
214
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800215static void qspi_set_lut(struct fsl_qspi_priv *priv)
Alison Wangc7410e32014-05-06 09:13:01 +0800216{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800217 struct fsl_qspi_regs *regs = priv->regs;
Alison Wangc7410e32014-05-06 09:13:01 +0800218 u32 lut_base;
219
220 /* Unlock the LUT */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800221 qspi_write32(priv->flags, &regs->lutkey, LUT_KEY_VALUE);
222 qspi_write32(priv->flags, &regs->lckcr, QSPI_LCKCR_UNLOCK);
Alison Wangc7410e32014-05-06 09:13:01 +0800223
224 /* Write Enable */
225 lut_base = SEQID_WREN * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800226 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_WREN) |
Alison Wangc7410e32014-05-06 09:13:01 +0800227 PAD0(LUT_PAD1) | INSTR0(LUT_CMD));
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800228 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
229 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
230 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800231
232 /* Fast Read */
233 lut_base = SEQID_FAST_READ * 4;
Peng Fan3a344482015-01-04 17:07:14 +0800234#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800235 qspi_write32(priv->flags, &regs->lut[lut_base],
236 OPRND0(QSPI_CMD_FAST_READ) | PAD0(LUT_PAD1) |
237 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
Peng Fan3a344482015-01-04 17:07:14 +0800238 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
239#else
Alison Wangc7410e32014-05-06 09:13:01 +0800240 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800241 qspi_write32(priv->flags, &regs->lut[lut_base],
242 OPRND0(QSPI_CMD_FAST_READ) | PAD0(LUT_PAD1) |
243 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
244 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Alison Wangc7410e32014-05-06 09:13:01 +0800245 else
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800246 qspi_write32(priv->flags, &regs->lut[lut_base],
Peng Fanff162342014-12-31 11:01:36 +0800247 OPRND0(QSPI_CMD_FAST_READ_4B) |
248 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) |
249 OPRND1(ADDR32BIT) | PAD1(LUT_PAD1) |
250 INSTR1(LUT_ADDR));
Peng Fan3a344482015-01-04 17:07:14 +0800251#endif
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800252 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
253 OPRND0(8) | PAD0(LUT_PAD1) | INSTR0(LUT_DUMMY) |
Ye Li007b6042019-08-14 11:31:36 +0000254 OPRND1(priv->devtype_data->rxfifo) | PAD1(LUT_PAD1) |
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800255 INSTR1(LUT_READ));
256 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
257 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800258
259 /* Read Status */
260 lut_base = SEQID_RDSR * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800261 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDSR) |
Alison Wangc7410e32014-05-06 09:13:01 +0800262 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
263 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800264 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
265 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
266 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800267
268 /* Erase a sector */
269 lut_base = SEQID_SE * 4;
Peng Fan3a344482015-01-04 17:07:14 +0800270#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800271 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_SE) |
Peng Fan3a344482015-01-04 17:07:14 +0800272 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
273 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
274#else
Alison Wangc7410e32014-05-06 09:13:01 +0800275 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800276 qspi_write32(priv->flags, &regs->lut[lut_base],
277 OPRND0(QSPI_CMD_SE) | PAD0(LUT_PAD1) |
278 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
279 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Alison Wangc7410e32014-05-06 09:13:01 +0800280 else
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800281 qspi_write32(priv->flags, &regs->lut[lut_base],
282 OPRND0(QSPI_CMD_SE_4B) | PAD0(LUT_PAD1) |
283 INSTR0(LUT_CMD) | OPRND1(ADDR32BIT) |
284 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Peng Fan3a344482015-01-04 17:07:14 +0800285#endif
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800286 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
287 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
288 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800289
290 /* Erase the whole chip */
291 lut_base = SEQID_CHIP_ERASE * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800292 qspi_write32(priv->flags, &regs->lut[lut_base],
293 OPRND0(QSPI_CMD_CHIP_ERASE) |
294 PAD0(LUT_PAD1) | INSTR0(LUT_CMD));
295 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
296 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
297 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800298
299 /* Page Program */
300 lut_base = SEQID_PP * 4;
Peng Fan3a344482015-01-04 17:07:14 +0800301#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800302 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_PP) |
Peng Fan3a344482015-01-04 17:07:14 +0800303 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
304 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
305#else
Alison Wangc7410e32014-05-06 09:13:01 +0800306 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800307 qspi_write32(priv->flags, &regs->lut[lut_base],
308 OPRND0(QSPI_CMD_PP) | PAD0(LUT_PAD1) |
309 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
310 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Alison Wangc7410e32014-05-06 09:13:01 +0800311 else
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800312 qspi_write32(priv->flags, &regs->lut[lut_base],
313 OPRND0(QSPI_CMD_PP_4B) | PAD0(LUT_PAD1) |
314 INSTR0(LUT_CMD) | OPRND1(ADDR32BIT) |
315 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Peng Fan3a344482015-01-04 17:07:14 +0800316#endif
Ye Lice561f52019-08-14 11:31:31 +0000317 /* Use IDATSZ in IPCR to determine the size and here set 0. */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800318 qspi_write32(priv->flags, &regs->lut[lut_base + 1], OPRND0(0) |
Peng Fan828e4682014-12-31 11:01:38 +0800319 PAD0(LUT_PAD1) | INSTR0(LUT_WRITE));
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800320 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
321 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800322
323 /* READ ID */
324 lut_base = SEQID_RDID * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800325 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDID) |
Alison Wangc7410e32014-05-06 09:13:01 +0800326 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(8) |
327 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800328 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
329 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
330 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800331
Peng Fan3642a872014-12-31 11:01:39 +0800332 /* SUB SECTOR 4K ERASE */
333 lut_base = SEQID_BE_4K * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800334 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BE_4K) |
Peng Fan3642a872014-12-31 11:01:39 +0800335 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
336 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
337
Peng Fan3a344482015-01-04 17:07:14 +0800338#ifdef CONFIG_SPI_FLASH_BAR
339 /*
340 * BRRD BRWR RDEAR WREAR are all supported, because it is hard to
341 * dynamically check whether to set BRRD BRWR or RDEAR WREAR during
342 * initialization.
343 */
344 lut_base = SEQID_BRRD * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800345 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BRRD) |
Peng Fan3a344482015-01-04 17:07:14 +0800346 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
347 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
348
349 lut_base = SEQID_BRWR * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800350 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BRWR) |
Peng Fan3a344482015-01-04 17:07:14 +0800351 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
352 PAD1(LUT_PAD1) | INSTR1(LUT_WRITE));
353
354 lut_base = SEQID_RDEAR * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800355 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDEAR) |
Peng Fan3a344482015-01-04 17:07:14 +0800356 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
357 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
358
359 lut_base = SEQID_WREAR * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800360 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_WREAR) |
Peng Fan3a344482015-01-04 17:07:14 +0800361 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
362 PAD1(LUT_PAD1) | INSTR1(LUT_WRITE));
363#endif
Yuan Yaod7193262016-03-15 14:36:42 +0800364
365 /*
366 * Read any device register.
367 * Used for Spansion S25FS-S family flash only.
368 */
369 lut_base = SEQID_RDAR * 4;
370 qspi_write32(priv->flags, &regs->lut[lut_base],
371 OPRND0(QSPI_CMD_RDAR) | PAD0(LUT_PAD1) |
372 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
373 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
374 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
375 OPRND0(8) | PAD0(LUT_PAD1) | INSTR0(LUT_DUMMY) |
376 OPRND1(1) | PAD1(LUT_PAD1) |
377 INSTR1(LUT_READ));
378
379 /*
380 * Write any device register.
381 * Used for Spansion S25FS-S family flash only.
382 */
383 lut_base = SEQID_WRAR * 4;
384 qspi_write32(priv->flags, &regs->lut[lut_base],
385 OPRND0(QSPI_CMD_WRAR) | PAD0(LUT_PAD1) |
386 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
387 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
388 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
389 OPRND0(1) | PAD0(LUT_PAD1) | INSTR0(LUT_WRITE));
390
Alison Wangc7410e32014-05-06 09:13:01 +0800391 /* Lock the LUT */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800392 qspi_write32(priv->flags, &regs->lutkey, LUT_KEY_VALUE);
393 qspi_write32(priv->flags, &regs->lckcr, QSPI_LCKCR_LOCK);
Alison Wangc7410e32014-05-06 09:13:01 +0800394}
395
Peng Fan1c5f9662015-01-08 10:40:20 +0800396#if defined(CONFIG_SYS_FSL_QSPI_AHB)
397/*
398 * If we have changed the content of the flash by writing or erasing,
399 * we need to invalidate the AHB buffer. If we do not do so, we may read out
400 * the wrong data. The spec tells us reset the AHB domain and Serial Flash
401 * domain at the same time.
402 */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800403static inline void qspi_ahb_invalid(struct fsl_qspi_priv *priv)
Peng Fan1c5f9662015-01-08 10:40:20 +0800404{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800405 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan1c5f9662015-01-08 10:40:20 +0800406 u32 reg;
407
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800408 reg = qspi_read32(priv->flags, &regs->mcr);
Peng Fan1c5f9662015-01-08 10:40:20 +0800409 reg |= QSPI_MCR_SWRSTHD_MASK | QSPI_MCR_SWRSTSD_MASK;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800410 qspi_write32(priv->flags, &regs->mcr, reg);
Peng Fan1c5f9662015-01-08 10:40:20 +0800411
412 /*
413 * The minimum delay : 1 AHB + 2 SFCK clocks.
414 * Delay 1 us is enough.
415 */
416 udelay(1);
417
418 reg &= ~(QSPI_MCR_SWRSTHD_MASK | QSPI_MCR_SWRSTSD_MASK);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800419 qspi_write32(priv->flags, &regs->mcr, reg);
Peng Fan1c5f9662015-01-08 10:40:20 +0800420}
421
422/* Read out the data from the AHB buffer. */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800423static inline void qspi_ahb_read(struct fsl_qspi_priv *priv, u8 *rxbuf, int len)
Peng Fan1c5f9662015-01-08 10:40:20 +0800424{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800425 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan1c5f9662015-01-08 10:40:20 +0800426 u32 mcr_reg;
Heinrich Schuchardt950e75d2018-03-18 12:47:20 +0100427 void *rx_addr;
Peng Fan1c5f9662015-01-08 10:40:20 +0800428
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800429 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
Peng Fan1c5f9662015-01-08 10:40:20 +0800430
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800431 qspi_write32(priv->flags, &regs->mcr,
432 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Ye Li416d2ec2019-08-14 11:31:27 +0000433 mcr_reg);
Peng Fan1c5f9662015-01-08 10:40:20 +0800434
Yunhui Cuie4584cc2016-07-13 10:46:27 +0800435 rx_addr = (void *)(uintptr_t)(priv->cur_amba_base + priv->sf_addr);
Peng Fan1c5f9662015-01-08 10:40:20 +0800436 /* Read out the data directly from the AHB buffer. */
Yunhui Cuie4584cc2016-07-13 10:46:27 +0800437 memcpy(rxbuf, rx_addr, len);
Peng Fan1c5f9662015-01-08 10:40:20 +0800438
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800439 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Peng Fan1c5f9662015-01-08 10:40:20 +0800440}
441
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800442static void qspi_enable_ddr_mode(struct fsl_qspi_priv *priv)
Peng Fan1c5f9662015-01-08 10:40:20 +0800443{
444 u32 reg, reg2;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800445 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan1c5f9662015-01-08 10:40:20 +0800446
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800447 reg = qspi_read32(priv->flags, &regs->mcr);
Peng Fan1c5f9662015-01-08 10:40:20 +0800448 /* Disable the module */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800449 qspi_write32(priv->flags, &regs->mcr, reg | QSPI_MCR_MDIS_MASK);
Peng Fan1c5f9662015-01-08 10:40:20 +0800450
451 /* Set the Sampling Register for DDR */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800452 reg2 = qspi_read32(priv->flags, &regs->smpr);
Peng Fan1c5f9662015-01-08 10:40:20 +0800453 reg2 &= ~QSPI_SMPR_DDRSMP_MASK;
454 reg2 |= (2 << QSPI_SMPR_DDRSMP_SHIFT);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800455 qspi_write32(priv->flags, &regs->smpr, reg2);
Peng Fan1c5f9662015-01-08 10:40:20 +0800456
457 /* Enable the module again (enable the DDR too) */
458 reg |= QSPI_MCR_DDR_EN_MASK;
459 /* Enable bit 29 for imx6sx */
Jagan Tekic97ca922015-10-23 01:37:18 +0530460 reg |= BIT(29);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800461 qspi_write32(priv->flags, &regs->mcr, reg);
Ye Li416d2ec2019-08-14 11:31:27 +0000462
463 /* Enable the TDH to 1 for some platforms like imx6ul, imx7d, etc
464 * These two bits are reserved on other platforms
465 */
466 reg = qspi_read32(priv->flags, &regs->flshcr);
467 reg &= ~(BIT(17));
468 reg |= BIT(16);
469 qspi_write32(priv->flags, &regs->flshcr, reg);
Peng Fan1c5f9662015-01-08 10:40:20 +0800470}
471
472/*
473 * There are two different ways to read out the data from the flash:
474 * the "IP Command Read" and the "AHB Command Read".
475 *
476 * The IC guy suggests we use the "AHB Command Read" which is faster
477 * then the "IP Command Read". (What's more is that there is a bug in
478 * the "IP Command Read" in the Vybrid.)
479 *
480 * After we set up the registers for the "AHB Command Read", we can use
481 * the memcpy to read the data directly. A "missed" access to the buffer
482 * causes the controller to clear the buffer, and use the sequence pointed
483 * by the QUADSPI_BFGENCR[SEQID] to initiate a read from the flash.
484 */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800485static void qspi_init_ahb_read(struct fsl_qspi_priv *priv)
Peng Fan1c5f9662015-01-08 10:40:20 +0800486{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800487 struct fsl_qspi_regs *regs = priv->regs;
488
Peng Fan1c5f9662015-01-08 10:40:20 +0800489 /* AHB configuration for access buffer 0/1/2 .*/
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800490 qspi_write32(priv->flags, &regs->buf0cr, QSPI_BUFXCR_INVALID_MSTRID);
491 qspi_write32(priv->flags, &regs->buf1cr, QSPI_BUFXCR_INVALID_MSTRID);
492 qspi_write32(priv->flags, &regs->buf2cr, QSPI_BUFXCR_INVALID_MSTRID);
493 qspi_write32(priv->flags, &regs->buf3cr, QSPI_BUF3CR_ALLMST_MASK |
Ye Li007b6042019-08-14 11:31:36 +0000494 ((priv->devtype_data->ahb_buf_size >> 3) << QSPI_BUF3CR_ADATSZ_SHIFT));
Peng Fan1c5f9662015-01-08 10:40:20 +0800495
496 /* We only use the buffer3 */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800497 qspi_write32(priv->flags, &regs->buf0ind, 0);
498 qspi_write32(priv->flags, &regs->buf1ind, 0);
499 qspi_write32(priv->flags, &regs->buf2ind, 0);
Peng Fan1c5f9662015-01-08 10:40:20 +0800500
501 /*
502 * Set the default lut sequence for AHB Read.
503 * Parallel mode is disabled.
504 */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800505 qspi_write32(priv->flags, &regs->bfgencr,
Peng Fan1c5f9662015-01-08 10:40:20 +0800506 SEQID_FAST_READ << QSPI_BFGENCR_SEQID_SHIFT);
507
508 /*Enable DDR Mode*/
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800509 qspi_enable_ddr_mode(priv);
Peng Fan1c5f9662015-01-08 10:40:20 +0800510}
511#endif
512
Peng Fan3a344482015-01-04 17:07:14 +0800513#ifdef CONFIG_SPI_FLASH_BAR
514/* Bank register read/write, EAR register read/write */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800515static void qspi_op_rdbank(struct fsl_qspi_priv *priv, u8 *rxbuf, u32 len)
Peng Fan3a344482015-01-04 17:07:14 +0800516{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800517 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan3a344482015-01-04 17:07:14 +0800518 u32 reg, mcr_reg, data, seqid;
519
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800520 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
521 qspi_write32(priv->flags, &regs->mcr,
522 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Ye Li416d2ec2019-08-14 11:31:27 +0000523 mcr_reg);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800524 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Peng Fan3a344482015-01-04 17:07:14 +0800525
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800526 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
Peng Fan3a344482015-01-04 17:07:14 +0800527
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800528 if (priv->cur_seqid == QSPI_CMD_BRRD)
Peng Fan3a344482015-01-04 17:07:14 +0800529 seqid = SEQID_BRRD;
530 else
531 seqid = SEQID_RDEAR;
532
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800533 qspi_write32(priv->flags, &regs->ipcr,
534 (seqid << QSPI_IPCR_SEQID_SHIFT) | len);
Peng Fan3a344482015-01-04 17:07:14 +0800535
536 /* Wait previous command complete */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800537 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Peng Fan3a344482015-01-04 17:07:14 +0800538 ;
539
540 while (1) {
Alexander Stein283eb4a2017-06-01 09:32:19 +0200541 WATCHDOG_RESET();
542
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800543 reg = qspi_read32(priv->flags, &regs->rbsr);
Peng Fan3a344482015-01-04 17:07:14 +0800544 if (reg & QSPI_RBSR_RDBFL_MASK) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800545 data = qspi_read32(priv->flags, &regs->rbdr[0]);
Ye Li007b6042019-08-14 11:31:36 +0000546 data = qspi_endian_xchg(priv, data);
Peng Fan3a344482015-01-04 17:07:14 +0800547 memcpy(rxbuf, &data, len);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800548 qspi_write32(priv->flags, &regs->mcr,
549 qspi_read32(priv->flags, &regs->mcr) |
Peng Fan3a344482015-01-04 17:07:14 +0800550 QSPI_MCR_CLR_RXF_MASK);
551 break;
552 }
553 }
554
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800555 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Peng Fan3a344482015-01-04 17:07:14 +0800556}
557#endif
Alison Wangc7410e32014-05-06 09:13:01 +0800558
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800559static void qspi_op_rdid(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len)
Alison Wangc7410e32014-05-06 09:13:01 +0800560{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800561 struct fsl_qspi_regs *regs = priv->regs;
Gong Qianyua5357ca2016-01-26 15:06:40 +0800562 u32 mcr_reg, rbsr_reg, data, size;
563 int i;
Alison Wangc7410e32014-05-06 09:13:01 +0800564
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800565 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
566 qspi_write32(priv->flags, &regs->mcr,
567 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Ye Li416d2ec2019-08-14 11:31:27 +0000568 mcr_reg);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800569 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Alison Wangc7410e32014-05-06 09:13:01 +0800570
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800571 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
Alison Wangc7410e32014-05-06 09:13:01 +0800572
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800573 qspi_write32(priv->flags, &regs->ipcr,
574 (SEQID_RDID << QSPI_IPCR_SEQID_SHIFT) | 0);
575 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800576 ;
577
578 i = 0;
Ye Li007b6042019-08-14 11:31:36 +0000579 while ((priv->devtype_data->rxfifo >= len) && (len > 0)) {
Alexander Stein283eb4a2017-06-01 09:32:19 +0200580 WATCHDOG_RESET();
581
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800582 rbsr_reg = qspi_read32(priv->flags, &regs->rbsr);
Alison Wangc7410e32014-05-06 09:13:01 +0800583 if (rbsr_reg & QSPI_RBSR_RDBFL_MASK) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800584 data = qspi_read32(priv->flags, &regs->rbdr[i]);
Ye Li007b6042019-08-14 11:31:36 +0000585 data = qspi_endian_xchg(priv, data);
Gong Qianyua5357ca2016-01-26 15:06:40 +0800586 size = (len < 4) ? len : 4;
587 memcpy(rxbuf, &data, size);
588 len -= size;
Alison Wangc7410e32014-05-06 09:13:01 +0800589 rxbuf++;
Alison Wangc7410e32014-05-06 09:13:01 +0800590 i++;
591 }
592 }
593
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800594 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Alison Wangc7410e32014-05-06 09:13:01 +0800595}
596
Peng Fan1c5f9662015-01-08 10:40:20 +0800597/* If not use AHB read, read data from ip interface */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800598static void qspi_op_read(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len)
Alison Wangc7410e32014-05-06 09:13:01 +0800599{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800600 struct fsl_qspi_regs *regs = priv->regs;
Alison Wangc7410e32014-05-06 09:13:01 +0800601 u32 mcr_reg, data;
602 int i, size;
603 u32 to_or_from;
Yuan Yaod7193262016-03-15 14:36:42 +0800604 u32 seqid;
605
606 if (priv->cur_seqid == QSPI_CMD_RDAR)
607 seqid = SEQID_RDAR;
608 else
609 seqid = SEQID_FAST_READ;
Alison Wangc7410e32014-05-06 09:13:01 +0800610
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800611 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
612 qspi_write32(priv->flags, &regs->mcr,
613 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Ye Li416d2ec2019-08-14 11:31:27 +0000614 mcr_reg);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800615 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Alison Wangc7410e32014-05-06 09:13:01 +0800616
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800617 to_or_from = priv->sf_addr + priv->cur_amba_base;
Alison Wangc7410e32014-05-06 09:13:01 +0800618
619 while (len > 0) {
Alexander Stein9b2c8a92015-11-04 09:19:10 +0100620 WATCHDOG_RESET();
621
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800622 qspi_write32(priv->flags, &regs->sfar, to_or_from);
Alison Wangc7410e32014-05-06 09:13:01 +0800623
Ye Li007b6042019-08-14 11:31:36 +0000624 size = (len > priv->devtype_data->rxfifo) ?
625 priv->devtype_data->rxfifo : len;
Alison Wangc7410e32014-05-06 09:13:01 +0800626
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800627 qspi_write32(priv->flags, &regs->ipcr,
Yuan Yaod7193262016-03-15 14:36:42 +0800628 (seqid << QSPI_IPCR_SEQID_SHIFT) |
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800629 size);
630 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800631 ;
632
633 to_or_from += size;
634 len -= size;
635
636 i = 0;
Ye Li007b6042019-08-14 11:31:36 +0000637 while ((priv->devtype_data->rxfifo >= size) && (size > 0)) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800638 data = qspi_read32(priv->flags, &regs->rbdr[i]);
Ye Li007b6042019-08-14 11:31:36 +0000639 data = qspi_endian_xchg(priv, data);
Yuan Yaod7193262016-03-15 14:36:42 +0800640 if (size < 4)
641 memcpy(rxbuf, &data, size);
642 else
643 memcpy(rxbuf, &data, 4);
Alison Wangc7410e32014-05-06 09:13:01 +0800644 rxbuf++;
645 size -= 4;
646 i++;
647 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800648 qspi_write32(priv->flags, &regs->mcr,
649 qspi_read32(priv->flags, &regs->mcr) |
650 QSPI_MCR_CLR_RXF_MASK);
Alison Wangc7410e32014-05-06 09:13:01 +0800651 }
652
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800653 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Alison Wangc7410e32014-05-06 09:13:01 +0800654}
655
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800656static void qspi_op_write(struct fsl_qspi_priv *priv, u8 *txbuf, u32 len)
Alison Wangc7410e32014-05-06 09:13:01 +0800657{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800658 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan3a344482015-01-04 17:07:14 +0800659 u32 mcr_reg, data, reg, status_reg, seqid;
Alison Wangc7410e32014-05-06 09:13:01 +0800660 int i, size, tx_size;
661 u32 to_or_from = 0;
662
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800663 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
664 qspi_write32(priv->flags, &regs->mcr,
665 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Ye Li416d2ec2019-08-14 11:31:27 +0000666 mcr_reg);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800667 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Alison Wangc7410e32014-05-06 09:13:01 +0800668
669 status_reg = 0;
670 while ((status_reg & FLASH_STATUS_WEL) != FLASH_STATUS_WEL) {
Alexander Stein9b2c8a92015-11-04 09:19:10 +0100671 WATCHDOG_RESET();
672
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800673 qspi_write32(priv->flags, &regs->ipcr,
674 (SEQID_WREN << QSPI_IPCR_SEQID_SHIFT) | 0);
675 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800676 ;
677
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800678 qspi_write32(priv->flags, &regs->ipcr,
679 (SEQID_RDSR << QSPI_IPCR_SEQID_SHIFT) | 1);
680 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800681 ;
682
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800683 reg = qspi_read32(priv->flags, &regs->rbsr);
Alison Wangc7410e32014-05-06 09:13:01 +0800684 if (reg & QSPI_RBSR_RDBFL_MASK) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800685 status_reg = qspi_read32(priv->flags, &regs->rbdr[0]);
Ye Li007b6042019-08-14 11:31:36 +0000686 status_reg = qspi_endian_xchg(priv, status_reg);
Alison Wangc7410e32014-05-06 09:13:01 +0800687 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800688 qspi_write32(priv->flags, &regs->mcr,
689 qspi_read32(priv->flags, &regs->mcr) |
690 QSPI_MCR_CLR_RXF_MASK);
Alison Wangc7410e32014-05-06 09:13:01 +0800691 }
692
Peng Fan3a344482015-01-04 17:07:14 +0800693 /* Default is page programming */
694 seqid = SEQID_PP;
Yuan Yaod7193262016-03-15 14:36:42 +0800695 if (priv->cur_seqid == QSPI_CMD_WRAR)
696 seqid = SEQID_WRAR;
Peng Fan3a344482015-01-04 17:07:14 +0800697#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800698 if (priv->cur_seqid == QSPI_CMD_BRWR)
Peng Fan3a344482015-01-04 17:07:14 +0800699 seqid = SEQID_BRWR;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800700 else if (priv->cur_seqid == QSPI_CMD_WREAR)
Peng Fan3a344482015-01-04 17:07:14 +0800701 seqid = SEQID_WREAR;
702#endif
703
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800704 to_or_from = priv->sf_addr + priv->cur_amba_base;
Peng Fan3a344482015-01-04 17:07:14 +0800705
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800706 qspi_write32(priv->flags, &regs->sfar, to_or_from);
Alison Wangc7410e32014-05-06 09:13:01 +0800707
Ye Li007b6042019-08-14 11:31:36 +0000708 tx_size = (len > priv->devtype_data->txfifo) ?
709 priv->devtype_data->txfifo : len;
Alison Wangc7410e32014-05-06 09:13:01 +0800710
Suresh Gupta14ae0f92017-06-05 14:37:20 +0530711 size = tx_size / 16;
712 /*
713 * There must be atleast 128bit data
714 * available in TX FIFO for any pop operation
715 */
716 if (tx_size % 16)
717 size++;
718 for (i = 0; i < size * 4; i++) {
Peng Fan3a344482015-01-04 17:07:14 +0800719 memcpy(&data, txbuf, 4);
Ye Li007b6042019-08-14 11:31:36 +0000720 data = qspi_endian_xchg(priv, data);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800721 qspi_write32(priv->flags, &regs->tbdr, data);
Peng Fan3a344482015-01-04 17:07:14 +0800722 txbuf += 4;
Alison Wangc7410e32014-05-06 09:13:01 +0800723 }
724
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800725 qspi_write32(priv->flags, &regs->ipcr,
726 (seqid << QSPI_IPCR_SEQID_SHIFT) | tx_size);
727 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800728 ;
729
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800730 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Alison Wangc7410e32014-05-06 09:13:01 +0800731}
732
Gong Qianyu4e8630b2016-01-26 15:06:41 +0800733static void qspi_op_rdsr(struct fsl_qspi_priv *priv, void *rxbuf, u32 len)
Alison Wangc7410e32014-05-06 09:13:01 +0800734{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800735 struct fsl_qspi_regs *regs = priv->regs;
Alison Wangc7410e32014-05-06 09:13:01 +0800736 u32 mcr_reg, reg, data;
737
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800738 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
739 qspi_write32(priv->flags, &regs->mcr,
740 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Ye Li416d2ec2019-08-14 11:31:27 +0000741 mcr_reg);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800742 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Alison Wangc7410e32014-05-06 09:13:01 +0800743
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800744 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
Alison Wangc7410e32014-05-06 09:13:01 +0800745
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800746 qspi_write32(priv->flags, &regs->ipcr,
747 (SEQID_RDSR << QSPI_IPCR_SEQID_SHIFT) | 0);
748 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800749 ;
750
751 while (1) {
Alexander Stein283eb4a2017-06-01 09:32:19 +0200752 WATCHDOG_RESET();
753
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800754 reg = qspi_read32(priv->flags, &regs->rbsr);
Alison Wangc7410e32014-05-06 09:13:01 +0800755 if (reg & QSPI_RBSR_RDBFL_MASK) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800756 data = qspi_read32(priv->flags, &regs->rbdr[0]);
Ye Li007b6042019-08-14 11:31:36 +0000757 data = qspi_endian_xchg(priv, data);
Gong Qianyu4e8630b2016-01-26 15:06:41 +0800758 memcpy(rxbuf, &data, len);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800759 qspi_write32(priv->flags, &regs->mcr,
760 qspi_read32(priv->flags, &regs->mcr) |
761 QSPI_MCR_CLR_RXF_MASK);
Alison Wangc7410e32014-05-06 09:13:01 +0800762 break;
763 }
764 }
765
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800766 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Alison Wangc7410e32014-05-06 09:13:01 +0800767}
768
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800769static void qspi_op_erase(struct fsl_qspi_priv *priv)
Alison Wangc7410e32014-05-06 09:13:01 +0800770{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800771 struct fsl_qspi_regs *regs = priv->regs;
Alison Wangc7410e32014-05-06 09:13:01 +0800772 u32 mcr_reg;
773 u32 to_or_from = 0;
774
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800775 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
776 qspi_write32(priv->flags, &regs->mcr,
777 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Ye Li416d2ec2019-08-14 11:31:27 +0000778 mcr_reg);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800779 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Alison Wangc7410e32014-05-06 09:13:01 +0800780
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800781 to_or_from = priv->sf_addr + priv->cur_amba_base;
782 qspi_write32(priv->flags, &regs->sfar, to_or_from);
Alison Wangc7410e32014-05-06 09:13:01 +0800783
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800784 qspi_write32(priv->flags, &regs->ipcr,
785 (SEQID_WREN << QSPI_IPCR_SEQID_SHIFT) | 0);
786 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800787 ;
788
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800789 if (priv->cur_seqid == QSPI_CMD_SE) {
790 qspi_write32(priv->flags, &regs->ipcr,
Peng Fan3642a872014-12-31 11:01:39 +0800791 (SEQID_SE << QSPI_IPCR_SEQID_SHIFT) | 0);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800792 } else if (priv->cur_seqid == QSPI_CMD_BE_4K) {
793 qspi_write32(priv->flags, &regs->ipcr,
Peng Fan3642a872014-12-31 11:01:39 +0800794 (SEQID_BE_4K << QSPI_IPCR_SEQID_SHIFT) | 0);
795 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800796 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800797 ;
798
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800799 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Alison Wangc7410e32014-05-06 09:13:01 +0800800}
801
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800802int qspi_xfer(struct fsl_qspi_priv *priv, unsigned int bitlen,
Alison Wangc7410e32014-05-06 09:13:01 +0800803 const void *dout, void *din, unsigned long flags)
804{
Alison Wangc7410e32014-05-06 09:13:01 +0800805 u32 bytes = DIV_ROUND_UP(bitlen, 8);
Peng Fan3a344482015-01-04 17:07:14 +0800806 static u32 wr_sfaddr;
Alison Wangc7410e32014-05-06 09:13:01 +0800807 u32 txbuf;
808
Alexander Stein283eb4a2017-06-01 09:32:19 +0200809 WATCHDOG_RESET();
810
Alison Wangc7410e32014-05-06 09:13:01 +0800811 if (dout) {
Peng Fan3a344482015-01-04 17:07:14 +0800812 if (flags & SPI_XFER_BEGIN) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800813 priv->cur_seqid = *(u8 *)dout;
Peng Fan3a344482015-01-04 17:07:14 +0800814 memcpy(&txbuf, dout, 4);
815 }
Alison Wangc7410e32014-05-06 09:13:01 +0800816
817 if (flags == SPI_XFER_END) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800818 priv->sf_addr = wr_sfaddr;
819 qspi_op_write(priv, (u8 *)dout, bytes);
Alison Wangc7410e32014-05-06 09:13:01 +0800820 return 0;
821 }
822
Yuan Yaod7193262016-03-15 14:36:42 +0800823 if (priv->cur_seqid == QSPI_CMD_FAST_READ ||
824 priv->cur_seqid == QSPI_CMD_RDAR) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800825 priv->sf_addr = swab32(txbuf) & OFFSET_BITS_MASK;
826 } else if ((priv->cur_seqid == QSPI_CMD_SE) ||
827 (priv->cur_seqid == QSPI_CMD_BE_4K)) {
828 priv->sf_addr = swab32(txbuf) & OFFSET_BITS_MASK;
829 qspi_op_erase(priv);
Yuan Yaod7193262016-03-15 14:36:42 +0800830 } else if (priv->cur_seqid == QSPI_CMD_PP ||
831 priv->cur_seqid == QSPI_CMD_WRAR) {
Peng Fan3a344482015-01-04 17:07:14 +0800832 wr_sfaddr = swab32(txbuf) & OFFSET_BITS_MASK;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800833 } else if ((priv->cur_seqid == QSPI_CMD_BRWR) ||
834 (priv->cur_seqid == QSPI_CMD_WREAR)) {
Peng Fan3a344482015-01-04 17:07:14 +0800835#ifdef CONFIG_SPI_FLASH_BAR
Peng Fan3a344482015-01-04 17:07:14 +0800836 wr_sfaddr = 0;
Peng Fan3a344482015-01-04 17:07:14 +0800837#endif
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800838 }
Alison Wangc7410e32014-05-06 09:13:01 +0800839 }
840
841 if (din) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800842 if (priv->cur_seqid == QSPI_CMD_FAST_READ) {
Peng Fan1c5f9662015-01-08 10:40:20 +0800843#ifdef CONFIG_SYS_FSL_QSPI_AHB
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800844 qspi_ahb_read(priv, din, bytes);
Peng Fan1c5f9662015-01-08 10:40:20 +0800845#else
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800846 qspi_op_read(priv, din, bytes);
Peng Fan1c5f9662015-01-08 10:40:20 +0800847#endif
Yuan Yaod7193262016-03-15 14:36:42 +0800848 } else if (priv->cur_seqid == QSPI_CMD_RDAR) {
849 qspi_op_read(priv, din, bytes);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800850 } else if (priv->cur_seqid == QSPI_CMD_RDID)
851 qspi_op_rdid(priv, din, bytes);
852 else if (priv->cur_seqid == QSPI_CMD_RDSR)
Gong Qianyu4e8630b2016-01-26 15:06:41 +0800853 qspi_op_rdsr(priv, din, bytes);
Peng Fan3a344482015-01-04 17:07:14 +0800854#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800855 else if ((priv->cur_seqid == QSPI_CMD_BRRD) ||
856 (priv->cur_seqid == QSPI_CMD_RDEAR)) {
857 priv->sf_addr = 0;
858 qspi_op_rdbank(priv, din, bytes);
Peng Fan3a344482015-01-04 17:07:14 +0800859 }
860#endif
Alison Wangc7410e32014-05-06 09:13:01 +0800861 }
862
Peng Fan1c5f9662015-01-08 10:40:20 +0800863#ifdef CONFIG_SYS_FSL_QSPI_AHB
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800864 if ((priv->cur_seqid == QSPI_CMD_SE) ||
865 (priv->cur_seqid == QSPI_CMD_PP) ||
866 (priv->cur_seqid == QSPI_CMD_BE_4K) ||
867 (priv->cur_seqid == QSPI_CMD_WREAR) ||
868 (priv->cur_seqid == QSPI_CMD_BRWR))
869 qspi_ahb_invalid(priv);
870#endif
871
872 return 0;
873}
874
875void qspi_module_disable(struct fsl_qspi_priv *priv, u8 disable)
876{
877 u32 mcr_val;
878
879 mcr_val = qspi_read32(priv->flags, &priv->regs->mcr);
880 if (disable)
881 mcr_val |= QSPI_MCR_MDIS_MASK;
882 else
883 mcr_val &= ~QSPI_MCR_MDIS_MASK;
884 qspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
885}
886
887void qspi_cfg_smpr(struct fsl_qspi_priv *priv, u32 clear_bits, u32 set_bits)
888{
889 u32 smpr_val;
890
891 smpr_val = qspi_read32(priv->flags, &priv->regs->smpr);
892 smpr_val &= ~clear_bits;
893 smpr_val |= set_bits;
894 qspi_write32(priv->flags, &priv->regs->smpr, smpr_val);
895}
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800896
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800897static int fsl_qspi_child_pre_probe(struct udevice *dev)
898{
Simon Glassde44acf2015-09-28 23:32:01 -0600899 struct spi_slave *slave = dev_get_parent_priv(dev);
Ye Li007b6042019-08-14 11:31:36 +0000900 struct fsl_qspi_priv *priv = dev_get_priv(dev_get_parent(dev));
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800901
Ye Li007b6042019-08-14 11:31:36 +0000902 slave->max_write_size = priv->devtype_data->txfifo;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800903
904 return 0;
905}
906
907static int fsl_qspi_probe(struct udevice *bus)
908{
Yuan Yaob4bfe102016-03-15 14:36:41 +0800909 u32 amba_size_per_chip;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800910 struct fsl_qspi_platdata *plat = dev_get_platdata(bus);
911 struct fsl_qspi_priv *priv = dev_get_priv(bus);
912 struct dm_spi_bus *dm_spi_bus;
Suresh Gupta4945b872017-08-30 20:06:33 +0530913 int i, ret;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800914
915 dm_spi_bus = bus->uclass_priv;
916
917 dm_spi_bus->max_hz = plat->speed_hz;
918
Gong Qianyu14b532e2016-01-26 15:06:39 +0800919 priv->regs = (struct fsl_qspi_regs *)(uintptr_t)plat->reg_base;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800920 priv->flags = plat->flags;
921
922 priv->speed_hz = plat->speed_hz;
Yuan Yaoae412392016-03-15 14:36:40 +0800923 /*
924 * QSPI SFADR width is 32bits, the max dest addr is 4GB-1.
925 * AMBA memory zone should be located on the 0~4GB space
926 * even on a 64bits cpu.
927 */
928 priv->amba_base[0] = (u32)plat->amba_base;
929 priv->amba_total_size = (u32)plat->amba_total_size;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800930 priv->flash_num = plat->flash_num;
931 priv->num_chipselect = plat->num_chipselect;
932
Ye Li007b6042019-08-14 11:31:36 +0000933 priv->devtype_data = (struct fsl_qspi_devtype_data *)dev_get_driver_data(bus);
934 if (!priv->devtype_data) {
935 printf("ERROR : No devtype_data found\n");
936 return -ENODEV;
937 }
938
939 debug("devtype=%d, txfifo=%d, rxfifo=%d, ahb=%d, data=0x%x\n",
940 priv->devtype_data->devtype,
941 priv->devtype_data->txfifo,
942 priv->devtype_data->rxfifo,
943 priv->devtype_data->ahb_buf_size,
944 priv->devtype_data->driver_data);
945
Suresh Gupta4945b872017-08-30 20:06:33 +0530946 /* make sure controller is not busy anywhere */
Rajat Srivastava234daec2018-03-22 13:30:55 +0530947 ret = is_controller_busy(priv);
Suresh Gupta4945b872017-08-30 20:06:33 +0530948
949 if (ret) {
950 debug("ERROR : The controller is busy\n");
951 return ret;
952 }
953
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800954 qspi_write32(priv->flags, &priv->regs->mcr,
York Sunb4f57a92016-10-05 13:19:08 -0700955 QSPI_MCR_RESERVED_MASK | QSPI_MCR_MDIS_MASK |
Ye Li416d2ec2019-08-14 11:31:27 +0000956 QSPI_MCR_END_CFD_LE);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800957
958 qspi_cfg_smpr(priv, ~(QSPI_SMPR_FSDLY_MASK | QSPI_SMPR_DDRSMP_MASK |
959 QSPI_SMPR_FSPHS_MASK | QSPI_SMPR_HSENA_MASK), 0);
960
Yuan Yaob4bfe102016-03-15 14:36:41 +0800961 /*
962 * Assign AMBA memory zone for every chipselect
963 * QuadSPI has two channels, every channel has two chipselects.
964 * If the property 'num-cs' in dts is 2, the AMBA memory will be divided
965 * into two parts and assign to every channel. This indicate that every
966 * channel only has one valid chipselect.
967 * If the property 'num-cs' in dts is 4, the AMBA memory will be divided
968 * into four parts and assign to every chipselect.
969 * Every channel will has two valid chipselects.
970 */
971 amba_size_per_chip = priv->amba_total_size >>
972 (priv->num_chipselect >> 1);
973 for (i = 1 ; i < priv->num_chipselect ; i++)
974 priv->amba_base[i] =
975 amba_size_per_chip + priv->amba_base[i - 1];
976
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800977 /*
978 * Any read access to non-implemented addresses will provide
979 * undefined results.
980 *
981 * In case single die flash devices, TOP_ADDR_MEMA2 and
982 * TOP_ADDR_MEMB2 should be initialized/programmed to
983 * TOP_ADDR_MEMA1 and TOP_ADDR_MEMB1 respectively - in effect,
984 * setting the size of these devices to 0. This would ensure
985 * that the complete memory map is assigned to only one flash device.
986 */
Suresh Gupta095f78e2017-02-21 14:26:46 +0530987 qspi_write32(priv->flags, &priv->regs->sfa1ad,
988 priv->amba_base[0] + amba_size_per_chip);
Yuan Yaob4bfe102016-03-15 14:36:41 +0800989 switch (priv->num_chipselect) {
Suresh Gupta095f78e2017-02-21 14:26:46 +0530990 case 1:
991 break;
Yuan Yaob4bfe102016-03-15 14:36:41 +0800992 case 2:
993 qspi_write32(priv->flags, &priv->regs->sfa2ad,
994 priv->amba_base[1]);
995 qspi_write32(priv->flags, &priv->regs->sfb1ad,
996 priv->amba_base[1] + amba_size_per_chip);
997 qspi_write32(priv->flags, &priv->regs->sfb2ad,
998 priv->amba_base[1] + amba_size_per_chip);
999 break;
1000 case 4:
1001 qspi_write32(priv->flags, &priv->regs->sfa2ad,
1002 priv->amba_base[2]);
1003 qspi_write32(priv->flags, &priv->regs->sfb1ad,
1004 priv->amba_base[3]);
1005 qspi_write32(priv->flags, &priv->regs->sfb2ad,
1006 priv->amba_base[3] + amba_size_per_chip);
1007 break;
1008 default:
1009 debug("Error: Unsupported chipselect number %u!\n",
1010 priv->num_chipselect);
1011 qspi_module_disable(priv, 1);
1012 return -EINVAL;
1013 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001014
1015 qspi_set_lut(priv);
1016
1017#ifdef CONFIG_SYS_FSL_QSPI_AHB
1018 qspi_init_ahb_read(priv);
1019#endif
1020
1021 qspi_module_disable(priv, 0);
1022
1023 return 0;
1024}
1025
1026static int fsl_qspi_ofdata_to_platdata(struct udevice *bus)
1027{
Yuan Yaoae412392016-03-15 14:36:40 +08001028 struct fdt_resource res_regs, res_mem;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001029 struct fsl_qspi_platdata *plat = bus->platdata;
1030 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -07001031 int node = dev_of_offset(bus);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001032 int ret, flash_num = 0, subnode;
1033
1034 if (fdtdec_get_bool(blob, node, "big-endian"))
1035 plat->flags |= QSPI_FLAG_REGMAP_ENDIAN_BIG;
1036
Yuan Yaoae412392016-03-15 14:36:40 +08001037 ret = fdt_get_named_resource(blob, node, "reg", "reg-names",
1038 "QuadSPI", &res_regs);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001039 if (ret) {
Yuan Yaoae412392016-03-15 14:36:40 +08001040 debug("Error: can't get regs base addresses(ret = %d)!\n", ret);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001041 return -ENOMEM;
1042 }
Yuan Yaoae412392016-03-15 14:36:40 +08001043 ret = fdt_get_named_resource(blob, node, "reg", "reg-names",
1044 "QuadSPI-memory", &res_mem);
1045 if (ret) {
1046 debug("Error: can't get AMBA base addresses(ret = %d)!\n", ret);
1047 return -ENOMEM;
1048 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001049
1050 /* Count flash numbers */
Simon Glass499c29e2016-10-02 17:59:29 -06001051 fdt_for_each_subnode(subnode, blob, node)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001052 ++flash_num;
1053
1054 if (flash_num == 0) {
1055 debug("Error: Missing flashes!\n");
1056 return -ENODEV;
1057 }
1058
1059 plat->speed_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
1060 FSL_QSPI_DEFAULT_SCK_FREQ);
1061 plat->num_chipselect = fdtdec_get_int(blob, node, "num-cs",
1062 FSL_QSPI_MAX_CHIPSELECT_NUM);
1063
Yuan Yaoae412392016-03-15 14:36:40 +08001064 plat->reg_base = res_regs.start;
1065 plat->amba_base = res_mem.start;
1066 plat->amba_total_size = res_mem.end - res_mem.start + 1;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001067 plat->flash_num = flash_num;
1068
Yuan Yaoae412392016-03-15 14:36:40 +08001069 debug("%s: regs=<0x%llx> <0x%llx, 0x%llx>, max-frequency=%d, endianess=%s\n",
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001070 __func__,
Yuan Yaoae412392016-03-15 14:36:40 +08001071 (u64)plat->reg_base,
1072 (u64)plat->amba_base,
1073 (u64)plat->amba_total_size,
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001074 plat->speed_hz,
1075 plat->flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ? "be" : "le"
1076 );
1077
1078 return 0;
1079}
1080
1081static int fsl_qspi_xfer(struct udevice *dev, unsigned int bitlen,
1082 const void *dout, void *din, unsigned long flags)
1083{
1084 struct fsl_qspi_priv *priv;
1085 struct udevice *bus;
1086
1087 bus = dev->parent;
1088 priv = dev_get_priv(bus);
1089
1090 return qspi_xfer(priv, bitlen, dout, din, flags);
1091}
1092
1093static int fsl_qspi_claim_bus(struct udevice *dev)
1094{
1095 struct fsl_qspi_priv *priv;
1096 struct udevice *bus;
1097 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
Suresh Gupta4945b872017-08-30 20:06:33 +05301098 int ret;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001099
1100 bus = dev->parent;
1101 priv = dev_get_priv(bus);
1102
Suresh Gupta4945b872017-08-30 20:06:33 +05301103 /* make sure controller is not busy anywhere */
Rajat Srivastava234daec2018-03-22 13:30:55 +05301104 ret = is_controller_busy(priv);
Suresh Gupta4945b872017-08-30 20:06:33 +05301105
1106 if (ret) {
1107 debug("ERROR : The controller is busy\n");
1108 return ret;
1109 }
1110
Yuan Yaob4bfe102016-03-15 14:36:41 +08001111 priv->cur_amba_base = priv->amba_base[slave_plat->cs];
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001112
1113 qspi_module_disable(priv, 0);
1114
1115 return 0;
1116}
1117
1118static int fsl_qspi_release_bus(struct udevice *dev)
1119{
1120 struct fsl_qspi_priv *priv;
1121 struct udevice *bus;
1122
1123 bus = dev->parent;
1124 priv = dev_get_priv(bus);
1125
1126 qspi_module_disable(priv, 1);
1127
1128 return 0;
1129}
1130
1131static int fsl_qspi_set_speed(struct udevice *bus, uint speed)
1132{
1133 /* Nothing to do */
1134 return 0;
1135}
1136
1137static int fsl_qspi_set_mode(struct udevice *bus, uint mode)
1138{
1139 /* Nothing to do */
1140 return 0;
1141}
1142
1143static const struct dm_spi_ops fsl_qspi_ops = {
1144 .claim_bus = fsl_qspi_claim_bus,
1145 .release_bus = fsl_qspi_release_bus,
1146 .xfer = fsl_qspi_xfer,
1147 .set_speed = fsl_qspi_set_speed,
1148 .set_mode = fsl_qspi_set_mode,
1149};
1150
1151static const struct udevice_id fsl_qspi_ids[] = {
Ye Li007b6042019-08-14 11:31:36 +00001152 { .compatible = "fsl,vf610-qspi", .data = (ulong)&vybrid_data },
1153 { .compatible = "fsl,imx6sx-qspi", .data = (ulong)&imx6sx_data },
1154 { .compatible = "fsl,imx6ul-qspi", .data = (ulong)&imx6ul_7d_data },
1155 { .compatible = "fsl,imx7d-qspi", .data = (ulong)&imx6ul_7d_data },
Ye Li57f67752019-08-14 11:31:40 +00001156 { .compatible = "fsl,imx7ulp-qspi", .data = (ulong)&imx7ulp_data },
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001157 { }
1158};
1159
1160U_BOOT_DRIVER(fsl_qspi) = {
1161 .name = "fsl_qspi",
1162 .id = UCLASS_SPI,
1163 .of_match = fsl_qspi_ids,
1164 .ops = &fsl_qspi_ops,
1165 .ofdata_to_platdata = fsl_qspi_ofdata_to_platdata,
1166 .platdata_auto_alloc_size = sizeof(struct fsl_qspi_platdata),
1167 .priv_auto_alloc_size = sizeof(struct fsl_qspi_priv),
1168 .probe = fsl_qspi_probe,
1169 .child_pre_probe = fsl_qspi_child_pre_probe,
1170};