blob: 5706569834104f6fb7302a1ce71edf5fb69f5bfa [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Alison Wang6b57ff62014-05-06 09:13:01 +08002/*
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08003 * Copyright 2013-2015 Freescale Semiconductor, Inc.
Alison Wang6b57ff62014-05-06 09:13:01 +08004 *
5 * Freescale Quad Serial Peripheral Interface (QSPI) driver
Alison Wang6b57ff62014-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 Schaefer733391e2019-07-01 17:37:35 +020013#include <linux/iopoll.h>
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +080014#include <dm.h>
15#include <errno.h>
Alexander Steinbeedbc22015-11-04 09:19:10 +010016#include <watchdog.h>
Suresh Gupta1c631da2017-08-30 20:06:33 +053017#include <wait_bit.h>
Alison Wang6b57ff62014-05-06 09:13:01 +080018#include "fsl_qspi.h"
19
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +080020DECLARE_GLOBAL_DATA_PTR;
21
Alison Wang6b57ff62014-05-06 09:13:01 +080022#define RX_BUFFER_SIZE 0x80
Peng Fanafe8e1b2018-01-03 08:52:02 +080023#if defined(CONFIG_MX6SX) || defined(CONFIG_MX6UL) || \
24 defined(CONFIG_MX6ULL) || defined(CONFIG_MX7D)
Peng Fanb93ab2e2014-12-31 11:01:38 +080025#define TX_BUFFER_SIZE 0x200
26#else
Alison Wang6b57ff62014-05-06 09:13:01 +080027#define TX_BUFFER_SIZE 0x40
Peng Fanb93ab2e2014-12-31 11:01:38 +080028#endif
Alison Wang6b57ff62014-05-06 09:13:01 +080029
Gong Qianyu87704132015-12-14 18:26:25 +080030#define OFFSET_BITS_MASK GENMASK(23, 0)
Alison Wang6b57ff62014-05-06 09:13:01 +080031
32#define FLASH_STATUS_WEL 0x02
33
34/* SEQID */
35#define SEQID_WREN 1
36#define SEQID_FAST_READ 2
37#define SEQID_RDSR 3
38#define SEQID_SE 4
39#define SEQID_CHIP_ERASE 5
40#define SEQID_PP 6
41#define SEQID_RDID 7
Peng Fanba4dc8a2014-12-31 11:01:39 +080042#define SEQID_BE_4K 8
Peng Fana2358782015-01-04 17:07:14 +080043#ifdef CONFIG_SPI_FLASH_BAR
44#define SEQID_BRRD 9
45#define SEQID_BRWR 10
46#define SEQID_RDEAR 11
47#define SEQID_WREAR 12
48#endif
Yuan Yaofebffe82016-03-15 14:36:42 +080049#define SEQID_WRAR 13
50#define SEQID_RDAR 14
Alison Wang6b57ff62014-05-06 09:13:01 +080051
Peng Fan53e3db72014-12-31 11:01:36 +080052/* QSPI CMD */
53#define QSPI_CMD_PP 0x02 /* Page program (up to 256 bytes) */
54#define QSPI_CMD_RDSR 0x05 /* Read status register */
55#define QSPI_CMD_WREN 0x06 /* Write enable */
56#define QSPI_CMD_FAST_READ 0x0b /* Read data bytes (high frequency) */
Peng Fanba4dc8a2014-12-31 11:01:39 +080057#define QSPI_CMD_BE_4K 0x20 /* 4K erase */
Peng Fan53e3db72014-12-31 11:01:36 +080058#define QSPI_CMD_CHIP_ERASE 0xc7 /* Erase whole flash chip */
59#define QSPI_CMD_SE 0xd8 /* Sector erase (usually 64KiB) */
60#define QSPI_CMD_RDID 0x9f /* Read JEDEC ID */
Alison Wang6b57ff62014-05-06 09:13:01 +080061
Peng Fana2358782015-01-04 17:07:14 +080062/* Used for Micron, winbond and Macronix flashes */
63#define QSPI_CMD_WREAR 0xc5 /* EAR register write */
64#define QSPI_CMD_RDEAR 0xc8 /* EAR reigster read */
65
66/* Used for Spansion flashes only. */
67#define QSPI_CMD_BRRD 0x16 /* Bank register read */
68#define QSPI_CMD_BRWR 0x17 /* Bank register write */
69
Yuan Yaofebffe82016-03-15 14:36:42 +080070/* Used for Spansion S25FS-S family flash only. */
71#define QSPI_CMD_RDAR 0x65 /* Read any device register */
72#define QSPI_CMD_WRAR 0x71 /* Write any device register */
73
Peng Fan53e3db72014-12-31 11:01:36 +080074/* 4-byte address QSPI CMD - used on Spansion and some Macronix flashes */
75#define QSPI_CMD_FAST_READ_4B 0x0c /* Read data bytes (high frequency) */
76#define QSPI_CMD_PP_4B 0x12 /* Page program (up to 256 bytes) */
77#define QSPI_CMD_SE_4B 0xdc /* Sector erase (usually 64KiB) */
Alison Wang6b57ff62014-05-06 09:13:01 +080078
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +080079/* fsl_qspi_platdata flags */
Jagan Teki29e6abd2015-10-23 01:37:18 +053080#define QSPI_FLAG_REGMAP_ENDIAN_BIG BIT(0)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +080081
82/* default SCK frequency, unit: HZ */
83#define FSL_QSPI_DEFAULT_SCK_FREQ 50000000
84
85/* QSPI max chipselect signals number */
86#define FSL_QSPI_MAX_CHIPSELECT_NUM 4
87
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +080088/**
89 * struct fsl_qspi_platdata - platform data for Freescale QSPI
90 *
91 * @flags: Flags for QSPI QSPI_FLAG_...
92 * @speed_hz: Default SCK frequency
93 * @reg_base: Base address of QSPI registers
94 * @amba_base: Base address of QSPI memory mapping
95 * @amba_total_size: size of QSPI memory mapping
96 * @flash_num: Number of active slave devices
97 * @num_chipselect: Number of QSPI chipselect signals
98 */
99struct fsl_qspi_platdata {
100 u32 flags;
101 u32 speed_hz;
Yuan Yaobf9bffa2016-03-15 14:36:40 +0800102 fdt_addr_t reg_base;
103 fdt_addr_t amba_base;
104 fdt_size_t amba_total_size;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800105 u32 flash_num;
106 u32 num_chipselect;
107};
Alison Wang6b57ff62014-05-06 09:13:01 +0800108
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800109/**
110 * struct fsl_qspi_priv - private data for Freescale QSPI
111 *
112 * @flags: Flags for QSPI QSPI_FLAG_...
113 * @bus_clk: QSPI input clk frequency
114 * @speed_hz: Default SCK frequency
115 * @cur_seqid: current LUT table sequence id
116 * @sf_addr: flash access offset
117 * @amba_base: Base address of QSPI memory mapping of every CS
118 * @amba_total_size: size of QSPI memory mapping
119 * @cur_amba_base: Base address of QSPI memory mapping of current CS
120 * @flash_num: Number of active slave devices
121 * @num_chipselect: Number of QSPI chipselect signals
122 * @regs: Point to QSPI register structure for I/O access
123 */
124struct fsl_qspi_priv {
125 u32 flags;
126 u32 bus_clk;
127 u32 speed_hz;
128 u32 cur_seqid;
129 u32 sf_addr;
130 u32 amba_base[FSL_QSPI_MAX_CHIPSELECT_NUM];
131 u32 amba_total_size;
132 u32 cur_amba_base;
133 u32 flash_num;
134 u32 num_chipselect;
135 struct fsl_qspi_regs *regs;
Alison Wang6b57ff62014-05-06 09:13:01 +0800136};
137
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800138
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}
Alison Wang6b57ff62014-05-06 09:13:01 +0800150
Rajat Srivastava1f553562018-03-22 13:30:55 +0530151static inline int is_controller_busy(const struct fsl_qspi_priv *priv)
152{
153 u32 val;
Thomas Schaefer733391e2019-07-01 17:37:35 +0200154 u32 mask = QSPI_SR_BUSY_MASK | QSPI_SR_AHB_ACC_MASK |
155 QSPI_SR_IP_ACC_MASK;
Rajat Srivastava1f553562018-03-22 13:30:55 +0530156
Thomas Schaefer733391e2019-07-01 17:37:35 +0200157 if (priv->flags & QSPI_FLAG_REGMAP_ENDIAN_BIG)
158 mask = (u32)cpu_to_be32(mask);
Rajat Srivastava1f553562018-03-22 13:30:55 +0530159
Thomas Schaefer733391e2019-07-01 17:37:35 +0200160 return readl_poll_timeout(&priv->regs->sr, val, !(val & mask), 1000);
Rajat Srivastava1f553562018-03-22 13:30:55 +0530161}
162
Alison Wang6b57ff62014-05-06 09:13:01 +0800163/* QSPI support swapping the flash read/write data
164 * in hardware for LS102xA, but not for VF610 */
165static inline u32 qspi_endian_xchg(u32 data)
166{
167#ifdef CONFIG_VF610
168 return swab32(data);
169#else
170 return data;
171#endif
172}
173
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800174static void qspi_set_lut(struct fsl_qspi_priv *priv)
Alison Wang6b57ff62014-05-06 09:13:01 +0800175{
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800176 struct fsl_qspi_regs *regs = priv->regs;
Alison Wang6b57ff62014-05-06 09:13:01 +0800177 u32 lut_base;
178
179 /* Unlock the LUT */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800180 qspi_write32(priv->flags, &regs->lutkey, LUT_KEY_VALUE);
181 qspi_write32(priv->flags, &regs->lckcr, QSPI_LCKCR_UNLOCK);
Alison Wang6b57ff62014-05-06 09:13:01 +0800182
183 /* Write Enable */
184 lut_base = SEQID_WREN * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800185 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_WREN) |
Alison Wang6b57ff62014-05-06 09:13:01 +0800186 PAD0(LUT_PAD1) | INSTR0(LUT_CMD));
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800187 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
188 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
189 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wang6b57ff62014-05-06 09:13:01 +0800190
191 /* Fast Read */
192 lut_base = SEQID_FAST_READ * 4;
Peng Fana2358782015-01-04 17:07:14 +0800193#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800194 qspi_write32(priv->flags, &regs->lut[lut_base],
195 OPRND0(QSPI_CMD_FAST_READ) | PAD0(LUT_PAD1) |
196 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
Peng Fana2358782015-01-04 17:07:14 +0800197 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
198#else
Alison Wang6b57ff62014-05-06 09:13:01 +0800199 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800200 qspi_write32(priv->flags, &regs->lut[lut_base],
201 OPRND0(QSPI_CMD_FAST_READ) | PAD0(LUT_PAD1) |
202 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
203 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Alison Wang6b57ff62014-05-06 09:13:01 +0800204 else
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800205 qspi_write32(priv->flags, &regs->lut[lut_base],
Peng Fan53e3db72014-12-31 11:01:36 +0800206 OPRND0(QSPI_CMD_FAST_READ_4B) |
207 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) |
208 OPRND1(ADDR32BIT) | PAD1(LUT_PAD1) |
209 INSTR1(LUT_ADDR));
Peng Fana2358782015-01-04 17:07:14 +0800210#endif
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800211 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
212 OPRND0(8) | PAD0(LUT_PAD1) | INSTR0(LUT_DUMMY) |
213 OPRND1(RX_BUFFER_SIZE) | PAD1(LUT_PAD1) |
214 INSTR1(LUT_READ));
215 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
216 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wang6b57ff62014-05-06 09:13:01 +0800217
218 /* Read Status */
219 lut_base = SEQID_RDSR * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800220 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDSR) |
Alison Wang6b57ff62014-05-06 09:13:01 +0800221 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
222 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800223 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
224 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
225 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wang6b57ff62014-05-06 09:13:01 +0800226
227 /* Erase a sector */
228 lut_base = SEQID_SE * 4;
Peng Fana2358782015-01-04 17:07:14 +0800229#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800230 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_SE) |
Peng Fana2358782015-01-04 17:07:14 +0800231 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
232 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
233#else
Alison Wang6b57ff62014-05-06 09:13:01 +0800234 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800235 qspi_write32(priv->flags, &regs->lut[lut_base],
236 OPRND0(QSPI_CMD_SE) | PAD0(LUT_PAD1) |
237 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
238 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Alison Wang6b57ff62014-05-06 09:13:01 +0800239 else
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800240 qspi_write32(priv->flags, &regs->lut[lut_base],
241 OPRND0(QSPI_CMD_SE_4B) | PAD0(LUT_PAD1) |
242 INSTR0(LUT_CMD) | OPRND1(ADDR32BIT) |
243 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Peng Fana2358782015-01-04 17:07:14 +0800244#endif
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800245 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
246 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
247 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wang6b57ff62014-05-06 09:13:01 +0800248
249 /* Erase the whole chip */
250 lut_base = SEQID_CHIP_ERASE * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800251 qspi_write32(priv->flags, &regs->lut[lut_base],
252 OPRND0(QSPI_CMD_CHIP_ERASE) |
253 PAD0(LUT_PAD1) | INSTR0(LUT_CMD));
254 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
255 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
256 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wang6b57ff62014-05-06 09:13:01 +0800257
258 /* Page Program */
259 lut_base = SEQID_PP * 4;
Peng Fana2358782015-01-04 17:07:14 +0800260#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800261 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_PP) |
Peng Fana2358782015-01-04 17:07:14 +0800262 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
263 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
264#else
Alison Wang6b57ff62014-05-06 09:13:01 +0800265 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800266 qspi_write32(priv->flags, &regs->lut[lut_base],
267 OPRND0(QSPI_CMD_PP) | PAD0(LUT_PAD1) |
268 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
269 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Alison Wang6b57ff62014-05-06 09:13:01 +0800270 else
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800271 qspi_write32(priv->flags, &regs->lut[lut_base],
272 OPRND0(QSPI_CMD_PP_4B) | PAD0(LUT_PAD1) |
273 INSTR0(LUT_CMD) | OPRND1(ADDR32BIT) |
274 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Peng Fana2358782015-01-04 17:07:14 +0800275#endif
Ye Lib866b922019-08-14 11:31:31 +0000276 /* Use IDATSZ in IPCR to determine the size and here set 0. */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800277 qspi_write32(priv->flags, &regs->lut[lut_base + 1], OPRND0(0) |
Peng Fanb93ab2e2014-12-31 11:01:38 +0800278 PAD0(LUT_PAD1) | INSTR0(LUT_WRITE));
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800279 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
280 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wang6b57ff62014-05-06 09:13:01 +0800281
282 /* READ ID */
283 lut_base = SEQID_RDID * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800284 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDID) |
Alison Wang6b57ff62014-05-06 09:13:01 +0800285 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(8) |
286 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800287 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
288 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
289 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wang6b57ff62014-05-06 09:13:01 +0800290
Peng Fanba4dc8a2014-12-31 11:01:39 +0800291 /* SUB SECTOR 4K ERASE */
292 lut_base = SEQID_BE_4K * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800293 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BE_4K) |
Peng Fanba4dc8a2014-12-31 11:01:39 +0800294 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
295 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
296
Peng Fana2358782015-01-04 17:07:14 +0800297#ifdef CONFIG_SPI_FLASH_BAR
298 /*
299 * BRRD BRWR RDEAR WREAR are all supported, because it is hard to
300 * dynamically check whether to set BRRD BRWR or RDEAR WREAR during
301 * initialization.
302 */
303 lut_base = SEQID_BRRD * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800304 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BRRD) |
Peng Fana2358782015-01-04 17:07:14 +0800305 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
306 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
307
308 lut_base = SEQID_BRWR * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800309 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BRWR) |
Peng Fana2358782015-01-04 17:07:14 +0800310 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
311 PAD1(LUT_PAD1) | INSTR1(LUT_WRITE));
312
313 lut_base = SEQID_RDEAR * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800314 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDEAR) |
Peng Fana2358782015-01-04 17:07:14 +0800315 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
316 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
317
318 lut_base = SEQID_WREAR * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800319 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_WREAR) |
Peng Fana2358782015-01-04 17:07:14 +0800320 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
321 PAD1(LUT_PAD1) | INSTR1(LUT_WRITE));
322#endif
Yuan Yaofebffe82016-03-15 14:36:42 +0800323
324 /*
325 * Read any device register.
326 * Used for Spansion S25FS-S family flash only.
327 */
328 lut_base = SEQID_RDAR * 4;
329 qspi_write32(priv->flags, &regs->lut[lut_base],
330 OPRND0(QSPI_CMD_RDAR) | PAD0(LUT_PAD1) |
331 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
332 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
333 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
334 OPRND0(8) | PAD0(LUT_PAD1) | INSTR0(LUT_DUMMY) |
335 OPRND1(1) | PAD1(LUT_PAD1) |
336 INSTR1(LUT_READ));
337
338 /*
339 * Write any device register.
340 * Used for Spansion S25FS-S family flash only.
341 */
342 lut_base = SEQID_WRAR * 4;
343 qspi_write32(priv->flags, &regs->lut[lut_base],
344 OPRND0(QSPI_CMD_WRAR) | PAD0(LUT_PAD1) |
345 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
346 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
347 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
348 OPRND0(1) | PAD0(LUT_PAD1) | INSTR0(LUT_WRITE));
349
Alison Wang6b57ff62014-05-06 09:13:01 +0800350 /* Lock the LUT */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800351 qspi_write32(priv->flags, &regs->lutkey, LUT_KEY_VALUE);
352 qspi_write32(priv->flags, &regs->lckcr, QSPI_LCKCR_LOCK);
Alison Wang6b57ff62014-05-06 09:13:01 +0800353}
354
Peng Fan5f7f70c2015-01-08 10:40:20 +0800355#if defined(CONFIG_SYS_FSL_QSPI_AHB)
356/*
357 * If we have changed the content of the flash by writing or erasing,
358 * we need to invalidate the AHB buffer. If we do not do so, we may read out
359 * the wrong data. The spec tells us reset the AHB domain and Serial Flash
360 * domain at the same time.
361 */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800362static inline void qspi_ahb_invalid(struct fsl_qspi_priv *priv)
Peng Fan5f7f70c2015-01-08 10:40:20 +0800363{
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800364 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan5f7f70c2015-01-08 10:40:20 +0800365 u32 reg;
366
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800367 reg = qspi_read32(priv->flags, &regs->mcr);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800368 reg |= QSPI_MCR_SWRSTHD_MASK | QSPI_MCR_SWRSTSD_MASK;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800369 qspi_write32(priv->flags, &regs->mcr, reg);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800370
371 /*
372 * The minimum delay : 1 AHB + 2 SFCK clocks.
373 * Delay 1 us is enough.
374 */
375 udelay(1);
376
377 reg &= ~(QSPI_MCR_SWRSTHD_MASK | QSPI_MCR_SWRSTSD_MASK);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800378 qspi_write32(priv->flags, &regs->mcr, reg);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800379}
380
381/* Read out the data from the AHB buffer. */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800382static inline void qspi_ahb_read(struct fsl_qspi_priv *priv, u8 *rxbuf, int len)
Peng Fan5f7f70c2015-01-08 10:40:20 +0800383{
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800384 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan5f7f70c2015-01-08 10:40:20 +0800385 u32 mcr_reg;
Heinrich Schuchardt4bcd88a2018-03-18 12:47:20 +0100386 void *rx_addr;
Peng Fan5f7f70c2015-01-08 10:40:20 +0800387
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800388 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800389
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800390 qspi_write32(priv->flags, &regs->mcr,
391 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Ye Li79495762019-08-14 11:31:27 +0000392 mcr_reg);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800393
Yunhui Cui04e5c6d2016-07-13 10:46:27 +0800394 rx_addr = (void *)(uintptr_t)(priv->cur_amba_base + priv->sf_addr);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800395 /* Read out the data directly from the AHB buffer. */
Yunhui Cui04e5c6d2016-07-13 10:46:27 +0800396 memcpy(rxbuf, rx_addr, len);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800397
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800398 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800399}
400
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800401static void qspi_enable_ddr_mode(struct fsl_qspi_priv *priv)
Peng Fan5f7f70c2015-01-08 10:40:20 +0800402{
403 u32 reg, reg2;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800404 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan5f7f70c2015-01-08 10:40:20 +0800405
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800406 reg = qspi_read32(priv->flags, &regs->mcr);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800407 /* Disable the module */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800408 qspi_write32(priv->flags, &regs->mcr, reg | QSPI_MCR_MDIS_MASK);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800409
410 /* Set the Sampling Register for DDR */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800411 reg2 = qspi_read32(priv->flags, &regs->smpr);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800412 reg2 &= ~QSPI_SMPR_DDRSMP_MASK;
413 reg2 |= (2 << QSPI_SMPR_DDRSMP_SHIFT);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800414 qspi_write32(priv->flags, &regs->smpr, reg2);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800415
416 /* Enable the module again (enable the DDR too) */
417 reg |= QSPI_MCR_DDR_EN_MASK;
418 /* Enable bit 29 for imx6sx */
Jagan Teki29e6abd2015-10-23 01:37:18 +0530419 reg |= BIT(29);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800420
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800421 qspi_write32(priv->flags, &regs->mcr, reg);
Ye Li79495762019-08-14 11:31:27 +0000422
423 /* Enable the TDH to 1 for some platforms like imx6ul, imx7d, etc
424 * These two bits are reserved on other platforms
425 */
426 reg = qspi_read32(priv->flags, &regs->flshcr);
427 reg &= ~(BIT(17));
428 reg |= BIT(16);
429 qspi_write32(priv->flags, &regs->flshcr, reg);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800430}
431
432/*
433 * There are two different ways to read out the data from the flash:
434 * the "IP Command Read" and the "AHB Command Read".
435 *
436 * The IC guy suggests we use the "AHB Command Read" which is faster
437 * then the "IP Command Read". (What's more is that there is a bug in
438 * the "IP Command Read" in the Vybrid.)
439 *
440 * After we set up the registers for the "AHB Command Read", we can use
441 * the memcpy to read the data directly. A "missed" access to the buffer
442 * causes the controller to clear the buffer, and use the sequence pointed
443 * by the QUADSPI_BFGENCR[SEQID] to initiate a read from the flash.
444 */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800445static void qspi_init_ahb_read(struct fsl_qspi_priv *priv)
Peng Fan5f7f70c2015-01-08 10:40:20 +0800446{
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800447 struct fsl_qspi_regs *regs = priv->regs;
448
Peng Fan5f7f70c2015-01-08 10:40:20 +0800449 /* AHB configuration for access buffer 0/1/2 .*/
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800450 qspi_write32(priv->flags, &regs->buf0cr, QSPI_BUFXCR_INVALID_MSTRID);
451 qspi_write32(priv->flags, &regs->buf1cr, QSPI_BUFXCR_INVALID_MSTRID);
452 qspi_write32(priv->flags, &regs->buf2cr, QSPI_BUFXCR_INVALID_MSTRID);
453 qspi_write32(priv->flags, &regs->buf3cr, QSPI_BUF3CR_ALLMST_MASK |
Peng Fan5f7f70c2015-01-08 10:40:20 +0800454 (0x80 << QSPI_BUF3CR_ADATSZ_SHIFT));
455
456 /* We only use the buffer3 */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800457 qspi_write32(priv->flags, &regs->buf0ind, 0);
458 qspi_write32(priv->flags, &regs->buf1ind, 0);
459 qspi_write32(priv->flags, &regs->buf2ind, 0);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800460
461 /*
462 * Set the default lut sequence for AHB Read.
463 * Parallel mode is disabled.
464 */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800465 qspi_write32(priv->flags, &regs->bfgencr,
Peng Fan5f7f70c2015-01-08 10:40:20 +0800466 SEQID_FAST_READ << QSPI_BFGENCR_SEQID_SHIFT);
467
468 /*Enable DDR Mode*/
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800469 qspi_enable_ddr_mode(priv);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800470}
471#endif
472
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800473#ifdef CONFIG_SPI_FLASH_BAR
474/* Bank register read/write, EAR register read/write */
475static void qspi_op_rdbank(struct fsl_qspi_priv *priv, u8 *rxbuf, u32 len)
Alison Wang6b57ff62014-05-06 09:13:01 +0800476{
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800477 struct fsl_qspi_regs *regs = priv->regs;
478 u32 reg, mcr_reg, data, seqid;
479
480 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
481 qspi_write32(priv->flags, &regs->mcr,
482 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Ye Li79495762019-08-14 11:31:27 +0000483 mcr_reg);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800484 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
485
486 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
487
488 if (priv->cur_seqid == QSPI_CMD_BRRD)
489 seqid = SEQID_BRRD;
490 else
491 seqid = SEQID_RDEAR;
492
493 qspi_write32(priv->flags, &regs->ipcr,
494 (seqid << QSPI_IPCR_SEQID_SHIFT) | len);
495
496 /* Wait previous command complete */
497 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
498 ;
499
500 while (1) {
Alexander Stein4df24f22017-06-01 09:32:19 +0200501 WATCHDOG_RESET();
502
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800503 reg = qspi_read32(priv->flags, &regs->rbsr);
504 if (reg & QSPI_RBSR_RDBFL_MASK) {
505 data = qspi_read32(priv->flags, &regs->rbdr[0]);
506 data = qspi_endian_xchg(data);
507 memcpy(rxbuf, &data, len);
508 qspi_write32(priv->flags, &regs->mcr,
509 qspi_read32(priv->flags, &regs->mcr) |
510 QSPI_MCR_CLR_RXF_MASK);
511 break;
512 }
513 }
514
515 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
516}
517#endif
518
519static void qspi_op_rdid(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len)
520{
521 struct fsl_qspi_regs *regs = priv->regs;
Gong Qianyu52070142016-01-26 15:06:40 +0800522 u32 mcr_reg, rbsr_reg, data, size;
523 int i;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800524
525 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
526 qspi_write32(priv->flags, &regs->mcr,
527 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Ye Li79495762019-08-14 11:31:27 +0000528 mcr_reg);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800529 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
530
531 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
532
533 qspi_write32(priv->flags, &regs->ipcr,
534 (SEQID_RDID << QSPI_IPCR_SEQID_SHIFT) | 0);
535 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
536 ;
537
538 i = 0;
Gong Qianyu52070142016-01-26 15:06:40 +0800539 while ((RX_BUFFER_SIZE >= len) && (len > 0)) {
Alexander Stein4df24f22017-06-01 09:32:19 +0200540 WATCHDOG_RESET();
541
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800542 rbsr_reg = qspi_read32(priv->flags, &regs->rbsr);
543 if (rbsr_reg & QSPI_RBSR_RDBFL_MASK) {
544 data = qspi_read32(priv->flags, &regs->rbdr[i]);
545 data = qspi_endian_xchg(data);
Gong Qianyu52070142016-01-26 15:06:40 +0800546 size = (len < 4) ? len : 4;
547 memcpy(rxbuf, &data, size);
548 len -= size;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800549 rxbuf++;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800550 i++;
551 }
552 }
553
554 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
555}
556
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800557/* If not use AHB read, read data from ip interface */
558static void qspi_op_read(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len)
559{
560 struct fsl_qspi_regs *regs = priv->regs;
561 u32 mcr_reg, data;
562 int i, size;
563 u32 to_or_from;
Yuan Yaofebffe82016-03-15 14:36:42 +0800564 u32 seqid;
565
566 if (priv->cur_seqid == QSPI_CMD_RDAR)
567 seqid = SEQID_RDAR;
568 else
569 seqid = SEQID_FAST_READ;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800570
571 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
572 qspi_write32(priv->flags, &regs->mcr,
573 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Ye Li79495762019-08-14 11:31:27 +0000574 mcr_reg);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800575 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
576
577 to_or_from = priv->sf_addr + priv->cur_amba_base;
578
579 while (len > 0) {
Alexander Steinbeedbc22015-11-04 09:19:10 +0100580 WATCHDOG_RESET();
581
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800582 qspi_write32(priv->flags, &regs->sfar, to_or_from);
583
584 size = (len > RX_BUFFER_SIZE) ?
585 RX_BUFFER_SIZE : len;
586
587 qspi_write32(priv->flags, &regs->ipcr,
Yuan Yaofebffe82016-03-15 14:36:42 +0800588 (seqid << QSPI_IPCR_SEQID_SHIFT) |
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800589 size);
590 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
591 ;
592
593 to_or_from += size;
594 len -= size;
595
596 i = 0;
597 while ((RX_BUFFER_SIZE >= size) && (size > 0)) {
598 data = qspi_read32(priv->flags, &regs->rbdr[i]);
599 data = qspi_endian_xchg(data);
Yuan Yaofebffe82016-03-15 14:36:42 +0800600 if (size < 4)
601 memcpy(rxbuf, &data, size);
602 else
603 memcpy(rxbuf, &data, 4);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800604 rxbuf++;
605 size -= 4;
606 i++;
607 }
608 qspi_write32(priv->flags, &regs->mcr,
609 qspi_read32(priv->flags, &regs->mcr) |
610 QSPI_MCR_CLR_RXF_MASK);
611 }
612
613 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
614}
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800615
616static void qspi_op_write(struct fsl_qspi_priv *priv, u8 *txbuf, u32 len)
617{
618 struct fsl_qspi_regs *regs = priv->regs;
619 u32 mcr_reg, data, reg, status_reg, seqid;
620 int i, size, tx_size;
621 u32 to_or_from = 0;
622
623 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
624 qspi_write32(priv->flags, &regs->mcr,
625 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Ye Li79495762019-08-14 11:31:27 +0000626 mcr_reg);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800627 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
628
629 status_reg = 0;
630 while ((status_reg & FLASH_STATUS_WEL) != FLASH_STATUS_WEL) {
Alexander Steinbeedbc22015-11-04 09:19:10 +0100631 WATCHDOG_RESET();
632
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800633 qspi_write32(priv->flags, &regs->ipcr,
634 (SEQID_WREN << QSPI_IPCR_SEQID_SHIFT) | 0);
635 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
636 ;
637
638 qspi_write32(priv->flags, &regs->ipcr,
639 (SEQID_RDSR << QSPI_IPCR_SEQID_SHIFT) | 1);
640 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
641 ;
642
643 reg = qspi_read32(priv->flags, &regs->rbsr);
644 if (reg & QSPI_RBSR_RDBFL_MASK) {
645 status_reg = qspi_read32(priv->flags, &regs->rbdr[0]);
646 status_reg = qspi_endian_xchg(status_reg);
647 }
648 qspi_write32(priv->flags, &regs->mcr,
649 qspi_read32(priv->flags, &regs->mcr) |
650 QSPI_MCR_CLR_RXF_MASK);
651 }
652
653 /* Default is page programming */
654 seqid = SEQID_PP;
Yuan Yaofebffe82016-03-15 14:36:42 +0800655 if (priv->cur_seqid == QSPI_CMD_WRAR)
656 seqid = SEQID_WRAR;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800657#ifdef CONFIG_SPI_FLASH_BAR
658 if (priv->cur_seqid == QSPI_CMD_BRWR)
659 seqid = SEQID_BRWR;
660 else if (priv->cur_seqid == QSPI_CMD_WREAR)
661 seqid = SEQID_WREAR;
662#endif
663
664 to_or_from = priv->sf_addr + priv->cur_amba_base;
665
666 qspi_write32(priv->flags, &regs->sfar, to_or_from);
667
668 tx_size = (len > TX_BUFFER_SIZE) ?
669 TX_BUFFER_SIZE : len;
670
Suresh Gupta10509982017-06-05 14:37:20 +0530671 size = tx_size / 16;
672 /*
673 * There must be atleast 128bit data
674 * available in TX FIFO for any pop operation
675 */
676 if (tx_size % 16)
677 size++;
678 for (i = 0; i < size * 4; i++) {
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800679 memcpy(&data, txbuf, 4);
680 data = qspi_endian_xchg(data);
681 qspi_write32(priv->flags, &regs->tbdr, data);
682 txbuf += 4;
683 }
684
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800685 qspi_write32(priv->flags, &regs->ipcr,
686 (seqid << QSPI_IPCR_SEQID_SHIFT) | tx_size);
687 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
688 ;
689
690 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
691}
692
Gong Qianyu940d2b82016-01-26 15:06:41 +0800693static void qspi_op_rdsr(struct fsl_qspi_priv *priv, void *rxbuf, u32 len)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800694{
695 struct fsl_qspi_regs *regs = priv->regs;
696 u32 mcr_reg, reg, data;
697
698 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
699 qspi_write32(priv->flags, &regs->mcr,
700 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Ye Li79495762019-08-14 11:31:27 +0000701 mcr_reg);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800702 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
703
704 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
705
706 qspi_write32(priv->flags, &regs->ipcr,
707 (SEQID_RDSR << QSPI_IPCR_SEQID_SHIFT) | 0);
708 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
709 ;
710
711 while (1) {
Alexander Stein4df24f22017-06-01 09:32:19 +0200712 WATCHDOG_RESET();
713
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800714 reg = qspi_read32(priv->flags, &regs->rbsr);
715 if (reg & QSPI_RBSR_RDBFL_MASK) {
716 data = qspi_read32(priv->flags, &regs->rbdr[0]);
717 data = qspi_endian_xchg(data);
Gong Qianyu940d2b82016-01-26 15:06:41 +0800718 memcpy(rxbuf, &data, len);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800719 qspi_write32(priv->flags, &regs->mcr,
720 qspi_read32(priv->flags, &regs->mcr) |
721 QSPI_MCR_CLR_RXF_MASK);
722 break;
723 }
724 }
725
726 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
727}
728
729static void qspi_op_erase(struct fsl_qspi_priv *priv)
730{
731 struct fsl_qspi_regs *regs = priv->regs;
732 u32 mcr_reg;
733 u32 to_or_from = 0;
734
735 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
736 qspi_write32(priv->flags, &regs->mcr,
737 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Ye Li79495762019-08-14 11:31:27 +0000738 mcr_reg);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800739 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
740
741 to_or_from = priv->sf_addr + priv->cur_amba_base;
742 qspi_write32(priv->flags, &regs->sfar, to_or_from);
743
744 qspi_write32(priv->flags, &regs->ipcr,
745 (SEQID_WREN << QSPI_IPCR_SEQID_SHIFT) | 0);
746 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
747 ;
748
749 if (priv->cur_seqid == QSPI_CMD_SE) {
750 qspi_write32(priv->flags, &regs->ipcr,
751 (SEQID_SE << QSPI_IPCR_SEQID_SHIFT) | 0);
752 } else if (priv->cur_seqid == QSPI_CMD_BE_4K) {
753 qspi_write32(priv->flags, &regs->ipcr,
754 (SEQID_BE_4K << QSPI_IPCR_SEQID_SHIFT) | 0);
755 }
756 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
757 ;
758
759 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
760}
761
762int qspi_xfer(struct fsl_qspi_priv *priv, unsigned int bitlen,
763 const void *dout, void *din, unsigned long flags)
764{
765 u32 bytes = DIV_ROUND_UP(bitlen, 8);
766 static u32 wr_sfaddr;
767 u32 txbuf;
768
Alexander Stein4df24f22017-06-01 09:32:19 +0200769 WATCHDOG_RESET();
770
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800771 if (dout) {
772 if (flags & SPI_XFER_BEGIN) {
773 priv->cur_seqid = *(u8 *)dout;
774 memcpy(&txbuf, dout, 4);
775 }
776
777 if (flags == SPI_XFER_END) {
778 priv->sf_addr = wr_sfaddr;
779 qspi_op_write(priv, (u8 *)dout, bytes);
780 return 0;
781 }
782
Yuan Yaofebffe82016-03-15 14:36:42 +0800783 if (priv->cur_seqid == QSPI_CMD_FAST_READ ||
784 priv->cur_seqid == QSPI_CMD_RDAR) {
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800785 priv->sf_addr = swab32(txbuf) & OFFSET_BITS_MASK;
786 } else if ((priv->cur_seqid == QSPI_CMD_SE) ||
787 (priv->cur_seqid == QSPI_CMD_BE_4K)) {
788 priv->sf_addr = swab32(txbuf) & OFFSET_BITS_MASK;
789 qspi_op_erase(priv);
Yuan Yaofebffe82016-03-15 14:36:42 +0800790 } else if (priv->cur_seqid == QSPI_CMD_PP ||
791 priv->cur_seqid == QSPI_CMD_WRAR) {
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800792 wr_sfaddr = swab32(txbuf) & OFFSET_BITS_MASK;
793 } else if ((priv->cur_seqid == QSPI_CMD_BRWR) ||
794 (priv->cur_seqid == QSPI_CMD_WREAR)) {
795#ifdef CONFIG_SPI_FLASH_BAR
796 wr_sfaddr = 0;
797#endif
798 }
799 }
800
801 if (din) {
802 if (priv->cur_seqid == QSPI_CMD_FAST_READ) {
803#ifdef CONFIG_SYS_FSL_QSPI_AHB
804 qspi_ahb_read(priv, din, bytes);
805#else
806 qspi_op_read(priv, din, bytes);
807#endif
Yuan Yaofebffe82016-03-15 14:36:42 +0800808 } else if (priv->cur_seqid == QSPI_CMD_RDAR) {
809 qspi_op_read(priv, din, bytes);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800810 } else if (priv->cur_seqid == QSPI_CMD_RDID)
811 qspi_op_rdid(priv, din, bytes);
812 else if (priv->cur_seqid == QSPI_CMD_RDSR)
Gong Qianyu940d2b82016-01-26 15:06:41 +0800813 qspi_op_rdsr(priv, din, bytes);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800814#ifdef CONFIG_SPI_FLASH_BAR
815 else if ((priv->cur_seqid == QSPI_CMD_BRRD) ||
816 (priv->cur_seqid == QSPI_CMD_RDEAR)) {
817 priv->sf_addr = 0;
818 qspi_op_rdbank(priv, din, bytes);
819 }
820#endif
821 }
822
823#ifdef CONFIG_SYS_FSL_QSPI_AHB
824 if ((priv->cur_seqid == QSPI_CMD_SE) ||
825 (priv->cur_seqid == QSPI_CMD_PP) ||
826 (priv->cur_seqid == QSPI_CMD_BE_4K) ||
827 (priv->cur_seqid == QSPI_CMD_WREAR) ||
828 (priv->cur_seqid == QSPI_CMD_BRWR))
829 qspi_ahb_invalid(priv);
830#endif
831
832 return 0;
833}
834
835void qspi_module_disable(struct fsl_qspi_priv *priv, u8 disable)
836{
837 u32 mcr_val;
838
839 mcr_val = qspi_read32(priv->flags, &priv->regs->mcr);
840 if (disable)
841 mcr_val |= QSPI_MCR_MDIS_MASK;
842 else
843 mcr_val &= ~QSPI_MCR_MDIS_MASK;
844 qspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
845}
846
847void qspi_cfg_smpr(struct fsl_qspi_priv *priv, u32 clear_bits, u32 set_bits)
848{
849 u32 smpr_val;
850
851 smpr_val = qspi_read32(priv->flags, &priv->regs->smpr);
852 smpr_val &= ~clear_bits;
853 smpr_val |= set_bits;
854 qspi_write32(priv->flags, &priv->regs->smpr, smpr_val);
855}
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800856
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800857static int fsl_qspi_child_pre_probe(struct udevice *dev)
858{
Simon Glassbcbe3d12015-09-28 23:32:01 -0600859 struct spi_slave *slave = dev_get_parent_priv(dev);
Alison Wang6b57ff62014-05-06 09:13:01 +0800860
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800861 slave->max_write_size = TX_BUFFER_SIZE;
Peng Fan5f7f70c2015-01-08 10:40:20 +0800862
Alison Wang6b57ff62014-05-06 09:13:01 +0800863 return 0;
864}
865
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800866static int fsl_qspi_probe(struct udevice *bus)
867{
Yuan Yao4e147412016-03-15 14:36:41 +0800868 u32 amba_size_per_chip;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800869 struct fsl_qspi_platdata *plat = dev_get_platdata(bus);
870 struct fsl_qspi_priv *priv = dev_get_priv(bus);
871 struct dm_spi_bus *dm_spi_bus;
Suresh Gupta1c631da2017-08-30 20:06:33 +0530872 int i, ret;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800873
874 dm_spi_bus = bus->uclass_priv;
875
876 dm_spi_bus->max_hz = plat->speed_hz;
877
Gong Qianyuc2a4cb12016-01-26 15:06:39 +0800878 priv->regs = (struct fsl_qspi_regs *)(uintptr_t)plat->reg_base;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800879 priv->flags = plat->flags;
880
881 priv->speed_hz = plat->speed_hz;
Yuan Yaobf9bffa2016-03-15 14:36:40 +0800882 /*
883 * QSPI SFADR width is 32bits, the max dest addr is 4GB-1.
884 * AMBA memory zone should be located on the 0~4GB space
885 * even on a 64bits cpu.
886 */
887 priv->amba_base[0] = (u32)plat->amba_base;
888 priv->amba_total_size = (u32)plat->amba_total_size;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800889 priv->flash_num = plat->flash_num;
890 priv->num_chipselect = plat->num_chipselect;
891
Suresh Gupta1c631da2017-08-30 20:06:33 +0530892 /* make sure controller is not busy anywhere */
Rajat Srivastava1f553562018-03-22 13:30:55 +0530893 ret = is_controller_busy(priv);
Suresh Gupta1c631da2017-08-30 20:06:33 +0530894
895 if (ret) {
896 debug("ERROR : The controller is busy\n");
897 return ret;
898 }
899
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800900 qspi_write32(priv->flags, &priv->regs->mcr,
York Sun3c6b1762016-10-05 13:19:08 -0700901 QSPI_MCR_RESERVED_MASK | QSPI_MCR_MDIS_MASK |
Ye Li79495762019-08-14 11:31:27 +0000902 QSPI_MCR_END_CFD_LE);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800903
904 qspi_cfg_smpr(priv, ~(QSPI_SMPR_FSDLY_MASK | QSPI_SMPR_DDRSMP_MASK |
905 QSPI_SMPR_FSPHS_MASK | QSPI_SMPR_HSENA_MASK), 0);
906
Yuan Yao4e147412016-03-15 14:36:41 +0800907 /*
908 * Assign AMBA memory zone for every chipselect
909 * QuadSPI has two channels, every channel has two chipselects.
910 * If the property 'num-cs' in dts is 2, the AMBA memory will be divided
911 * into two parts and assign to every channel. This indicate that every
912 * channel only has one valid chipselect.
913 * If the property 'num-cs' in dts is 4, the AMBA memory will be divided
914 * into four parts and assign to every chipselect.
915 * Every channel will has two valid chipselects.
916 */
917 amba_size_per_chip = priv->amba_total_size >>
918 (priv->num_chipselect >> 1);
919 for (i = 1 ; i < priv->num_chipselect ; i++)
920 priv->amba_base[i] =
921 amba_size_per_chip + priv->amba_base[i - 1];
922
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800923 /*
924 * Any read access to non-implemented addresses will provide
925 * undefined results.
926 *
927 * In case single die flash devices, TOP_ADDR_MEMA2 and
928 * TOP_ADDR_MEMB2 should be initialized/programmed to
929 * TOP_ADDR_MEMA1 and TOP_ADDR_MEMB1 respectively - in effect,
930 * setting the size of these devices to 0. This would ensure
931 * that the complete memory map is assigned to only one flash device.
932 */
Suresh Gupta38a5c572017-02-21 14:26:46 +0530933 qspi_write32(priv->flags, &priv->regs->sfa1ad,
934 priv->amba_base[0] + amba_size_per_chip);
Yuan Yao4e147412016-03-15 14:36:41 +0800935 switch (priv->num_chipselect) {
Suresh Gupta38a5c572017-02-21 14:26:46 +0530936 case 1:
937 break;
Yuan Yao4e147412016-03-15 14:36:41 +0800938 case 2:
939 qspi_write32(priv->flags, &priv->regs->sfa2ad,
940 priv->amba_base[1]);
941 qspi_write32(priv->flags, &priv->regs->sfb1ad,
942 priv->amba_base[1] + amba_size_per_chip);
943 qspi_write32(priv->flags, &priv->regs->sfb2ad,
944 priv->amba_base[1] + amba_size_per_chip);
945 break;
946 case 4:
947 qspi_write32(priv->flags, &priv->regs->sfa2ad,
948 priv->amba_base[2]);
949 qspi_write32(priv->flags, &priv->regs->sfb1ad,
950 priv->amba_base[3]);
951 qspi_write32(priv->flags, &priv->regs->sfb2ad,
952 priv->amba_base[3] + amba_size_per_chip);
953 break;
954 default:
955 debug("Error: Unsupported chipselect number %u!\n",
956 priv->num_chipselect);
957 qspi_module_disable(priv, 1);
958 return -EINVAL;
959 }
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800960
961 qspi_set_lut(priv);
962
963#ifdef CONFIG_SYS_FSL_QSPI_AHB
964 qspi_init_ahb_read(priv);
965#endif
966
967 qspi_module_disable(priv, 0);
968
969 return 0;
970}
971
972static int fsl_qspi_ofdata_to_platdata(struct udevice *bus)
973{
Yuan Yaobf9bffa2016-03-15 14:36:40 +0800974 struct fdt_resource res_regs, res_mem;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800975 struct fsl_qspi_platdata *plat = bus->platdata;
976 const void *blob = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -0700977 int node = dev_of_offset(bus);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800978 int ret, flash_num = 0, subnode;
979
980 if (fdtdec_get_bool(blob, node, "big-endian"))
981 plat->flags |= QSPI_FLAG_REGMAP_ENDIAN_BIG;
982
Yuan Yaobf9bffa2016-03-15 14:36:40 +0800983 ret = fdt_get_named_resource(blob, node, "reg", "reg-names",
984 "QuadSPI", &res_regs);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800985 if (ret) {
Yuan Yaobf9bffa2016-03-15 14:36:40 +0800986 debug("Error: can't get regs base addresses(ret = %d)!\n", ret);
987 return -ENOMEM;
988 }
989 ret = fdt_get_named_resource(blob, node, "reg", "reg-names",
990 "QuadSPI-memory", &res_mem);
991 if (ret) {
992 debug("Error: can't get AMBA base addresses(ret = %d)!\n", ret);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800993 return -ENOMEM;
994 }
995
996 /* Count flash numbers */
Simon Glassdf87e6b2016-10-02 17:59:29 -0600997 fdt_for_each_subnode(subnode, blob, node)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800998 ++flash_num;
999
1000 if (flash_num == 0) {
1001 debug("Error: Missing flashes!\n");
1002 return -ENODEV;
1003 }
1004
1005 plat->speed_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
1006 FSL_QSPI_DEFAULT_SCK_FREQ);
1007 plat->num_chipselect = fdtdec_get_int(blob, node, "num-cs",
1008 FSL_QSPI_MAX_CHIPSELECT_NUM);
1009
Yuan Yaobf9bffa2016-03-15 14:36:40 +08001010 plat->reg_base = res_regs.start;
1011 plat->amba_base = res_mem.start;
1012 plat->amba_total_size = res_mem.end - res_mem.start + 1;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001013 plat->flash_num = flash_num;
1014
Yuan Yaobf9bffa2016-03-15 14:36:40 +08001015 debug("%s: regs=<0x%llx> <0x%llx, 0x%llx>, max-frequency=%d, endianess=%s\n",
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001016 __func__,
Yuan Yaobf9bffa2016-03-15 14:36:40 +08001017 (u64)plat->reg_base,
1018 (u64)plat->amba_base,
1019 (u64)plat->amba_total_size,
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001020 plat->speed_hz,
1021 plat->flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ? "be" : "le"
1022 );
1023
1024 return 0;
1025}
1026
1027static int fsl_qspi_xfer(struct udevice *dev, unsigned int bitlen,
1028 const void *dout, void *din, unsigned long flags)
1029{
1030 struct fsl_qspi_priv *priv;
1031 struct udevice *bus;
1032
1033 bus = dev->parent;
1034 priv = dev_get_priv(bus);
1035
1036 return qspi_xfer(priv, bitlen, dout, din, flags);
1037}
1038
1039static int fsl_qspi_claim_bus(struct udevice *dev)
1040{
1041 struct fsl_qspi_priv *priv;
1042 struct udevice *bus;
1043 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
Suresh Gupta1c631da2017-08-30 20:06:33 +05301044 int ret;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001045
1046 bus = dev->parent;
1047 priv = dev_get_priv(bus);
1048
Suresh Gupta1c631da2017-08-30 20:06:33 +05301049 /* make sure controller is not busy anywhere */
Rajat Srivastava1f553562018-03-22 13:30:55 +05301050 ret = is_controller_busy(priv);
Suresh Gupta1c631da2017-08-30 20:06:33 +05301051
1052 if (ret) {
1053 debug("ERROR : The controller is busy\n");
1054 return ret;
1055 }
1056
Yuan Yao4e147412016-03-15 14:36:41 +08001057 priv->cur_amba_base = priv->amba_base[slave_plat->cs];
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001058
1059 qspi_module_disable(priv, 0);
1060
1061 return 0;
1062}
1063
1064static int fsl_qspi_release_bus(struct udevice *dev)
1065{
1066 struct fsl_qspi_priv *priv;
1067 struct udevice *bus;
1068
1069 bus = dev->parent;
1070 priv = dev_get_priv(bus);
1071
1072 qspi_module_disable(priv, 1);
1073
1074 return 0;
1075}
1076
1077static int fsl_qspi_set_speed(struct udevice *bus, uint speed)
Alison Wang6b57ff62014-05-06 09:13:01 +08001078{
1079 /* Nothing to do */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001080 return 0;
Alison Wang6b57ff62014-05-06 09:13:01 +08001081}
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001082
1083static int fsl_qspi_set_mode(struct udevice *bus, uint mode)
1084{
1085 /* Nothing to do */
1086 return 0;
1087}
1088
1089static const struct dm_spi_ops fsl_qspi_ops = {
1090 .claim_bus = fsl_qspi_claim_bus,
1091 .release_bus = fsl_qspi_release_bus,
1092 .xfer = fsl_qspi_xfer,
1093 .set_speed = fsl_qspi_set_speed,
1094 .set_mode = fsl_qspi_set_mode,
1095};
1096
1097static const struct udevice_id fsl_qspi_ids[] = {
1098 { .compatible = "fsl,vf610-qspi" },
1099 { .compatible = "fsl,imx6sx-qspi" },
Peng Fanafe8e1b2018-01-03 08:52:02 +08001100 { .compatible = "fsl,imx6ul-qspi" },
1101 { .compatible = "fsl,imx7d-qspi" },
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001102 { }
1103};
1104
1105U_BOOT_DRIVER(fsl_qspi) = {
1106 .name = "fsl_qspi",
1107 .id = UCLASS_SPI,
1108 .of_match = fsl_qspi_ids,
1109 .ops = &fsl_qspi_ops,
1110 .ofdata_to_platdata = fsl_qspi_ofdata_to_platdata,
1111 .platdata_auto_alloc_size = sizeof(struct fsl_qspi_platdata),
1112 .priv_auto_alloc_size = sizeof(struct fsl_qspi_priv),
1113 .probe = fsl_qspi_probe,
1114 .child_pre_probe = fsl_qspi_child_pre_probe,
1115};