blob: 5dc69a6865762a562bf64115f899e489748976cd [file] [log] [blame]
Alison Wang6b57ff62014-05-06 09:13:01 +08001/*
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08002 * Copyright 2013-2015 Freescale Semiconductor, Inc.
Alison Wang6b57ff62014-05-06 09:13:01 +08003 *
4 * Freescale Quad Serial Peripheral Interface (QSPI) driver
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
10#include <malloc.h>
11#include <spi.h>
12#include <asm/io.h>
13#include <linux/sizes.h>
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
88#ifdef CONFIG_DM_SPI
89/**
90 * struct fsl_qspi_platdata - platform data for Freescale QSPI
91 *
92 * @flags: Flags for QSPI QSPI_FLAG_...
93 * @speed_hz: Default SCK frequency
94 * @reg_base: Base address of QSPI registers
95 * @amba_base: Base address of QSPI memory mapping
96 * @amba_total_size: size of QSPI memory mapping
97 * @flash_num: Number of active slave devices
98 * @num_chipselect: Number of QSPI chipselect signals
99 */
100struct fsl_qspi_platdata {
101 u32 flags;
102 u32 speed_hz;
Yuan Yaobf9bffa2016-03-15 14:36:40 +0800103 fdt_addr_t reg_base;
104 fdt_addr_t amba_base;
105 fdt_size_t amba_total_size;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800106 u32 flash_num;
107 u32 num_chipselect;
108};
Alison Wang6b57ff62014-05-06 09:13:01 +0800109#endif
110
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800111/**
112 * struct fsl_qspi_priv - private data for Freescale QSPI
113 *
114 * @flags: Flags for QSPI QSPI_FLAG_...
115 * @bus_clk: QSPI input clk frequency
116 * @speed_hz: Default SCK frequency
117 * @cur_seqid: current LUT table sequence id
118 * @sf_addr: flash access offset
119 * @amba_base: Base address of QSPI memory mapping of every CS
120 * @amba_total_size: size of QSPI memory mapping
121 * @cur_amba_base: Base address of QSPI memory mapping of current CS
122 * @flash_num: Number of active slave devices
123 * @num_chipselect: Number of QSPI chipselect signals
124 * @regs: Point to QSPI register structure for I/O access
125 */
126struct fsl_qspi_priv {
127 u32 flags;
128 u32 bus_clk;
129 u32 speed_hz;
130 u32 cur_seqid;
131 u32 sf_addr;
132 u32 amba_base[FSL_QSPI_MAX_CHIPSELECT_NUM];
133 u32 amba_total_size;
134 u32 cur_amba_base;
135 u32 flash_num;
136 u32 num_chipselect;
137 struct fsl_qspi_regs *regs;
Alison Wang6b57ff62014-05-06 09:13:01 +0800138};
139
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800140#ifndef CONFIG_DM_SPI
Alison Wang6b57ff62014-05-06 09:13:01 +0800141struct fsl_qspi {
142 struct spi_slave slave;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800143 struct fsl_qspi_priv priv;
Alison Wang6b57ff62014-05-06 09:13:01 +0800144};
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800145#endif
146
147static u32 qspi_read32(u32 flags, u32 *addr)
148{
149 return flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ?
150 in_be32(addr) : in_le32(addr);
151}
152
153static void qspi_write32(u32 flags, u32 *addr, u32 val)
154{
155 flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ?
156 out_be32(addr, val) : out_le32(addr, val);
157}
Alison Wang6b57ff62014-05-06 09:13:01 +0800158
159/* QSPI support swapping the flash read/write data
160 * in hardware for LS102xA, but not for VF610 */
161static inline u32 qspi_endian_xchg(u32 data)
162{
163#ifdef CONFIG_VF610
164 return swab32(data);
165#else
166 return data;
167#endif
168}
169
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800170static void qspi_set_lut(struct fsl_qspi_priv *priv)
Alison Wang6b57ff62014-05-06 09:13:01 +0800171{
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800172 struct fsl_qspi_regs *regs = priv->regs;
Alison Wang6b57ff62014-05-06 09:13:01 +0800173 u32 lut_base;
174
175 /* Unlock the LUT */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800176 qspi_write32(priv->flags, &regs->lutkey, LUT_KEY_VALUE);
177 qspi_write32(priv->flags, &regs->lckcr, QSPI_LCKCR_UNLOCK);
Alison Wang6b57ff62014-05-06 09:13:01 +0800178
179 /* Write Enable */
180 lut_base = SEQID_WREN * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800181 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_WREN) |
Alison Wang6b57ff62014-05-06 09:13:01 +0800182 PAD0(LUT_PAD1) | INSTR0(LUT_CMD));
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800183 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
184 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
185 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wang6b57ff62014-05-06 09:13:01 +0800186
187 /* Fast Read */
188 lut_base = SEQID_FAST_READ * 4;
Peng Fana2358782015-01-04 17:07:14 +0800189#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800190 qspi_write32(priv->flags, &regs->lut[lut_base],
191 OPRND0(QSPI_CMD_FAST_READ) | PAD0(LUT_PAD1) |
192 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
Peng Fana2358782015-01-04 17:07:14 +0800193 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
194#else
Alison Wang6b57ff62014-05-06 09:13:01 +0800195 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800196 qspi_write32(priv->flags, &regs->lut[lut_base],
197 OPRND0(QSPI_CMD_FAST_READ) | PAD0(LUT_PAD1) |
198 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
199 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Alison Wang6b57ff62014-05-06 09:13:01 +0800200 else
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800201 qspi_write32(priv->flags, &regs->lut[lut_base],
Peng Fan53e3db72014-12-31 11:01:36 +0800202 OPRND0(QSPI_CMD_FAST_READ_4B) |
203 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) |
204 OPRND1(ADDR32BIT) | PAD1(LUT_PAD1) |
205 INSTR1(LUT_ADDR));
Peng Fana2358782015-01-04 17:07:14 +0800206#endif
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800207 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
208 OPRND0(8) | PAD0(LUT_PAD1) | INSTR0(LUT_DUMMY) |
209 OPRND1(RX_BUFFER_SIZE) | PAD1(LUT_PAD1) |
210 INSTR1(LUT_READ));
211 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
212 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wang6b57ff62014-05-06 09:13:01 +0800213
214 /* Read Status */
215 lut_base = SEQID_RDSR * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800216 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDSR) |
Alison Wang6b57ff62014-05-06 09:13:01 +0800217 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
218 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800219 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
220 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
221 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wang6b57ff62014-05-06 09:13:01 +0800222
223 /* Erase a sector */
224 lut_base = SEQID_SE * 4;
Peng Fana2358782015-01-04 17:07:14 +0800225#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800226 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_SE) |
Peng Fana2358782015-01-04 17:07:14 +0800227 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
228 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
229#else
Alison Wang6b57ff62014-05-06 09:13:01 +0800230 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800231 qspi_write32(priv->flags, &regs->lut[lut_base],
232 OPRND0(QSPI_CMD_SE) | PAD0(LUT_PAD1) |
233 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
234 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Alison Wang6b57ff62014-05-06 09:13:01 +0800235 else
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800236 qspi_write32(priv->flags, &regs->lut[lut_base],
237 OPRND0(QSPI_CMD_SE_4B) | PAD0(LUT_PAD1) |
238 INSTR0(LUT_CMD) | OPRND1(ADDR32BIT) |
239 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Peng Fana2358782015-01-04 17:07:14 +0800240#endif
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800241 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
242 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
243 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wang6b57ff62014-05-06 09:13:01 +0800244
245 /* Erase the whole chip */
246 lut_base = SEQID_CHIP_ERASE * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800247 qspi_write32(priv->flags, &regs->lut[lut_base],
248 OPRND0(QSPI_CMD_CHIP_ERASE) |
249 PAD0(LUT_PAD1) | INSTR0(LUT_CMD));
250 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
251 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
252 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wang6b57ff62014-05-06 09:13:01 +0800253
254 /* Page Program */
255 lut_base = SEQID_PP * 4;
Peng Fana2358782015-01-04 17:07:14 +0800256#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800257 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_PP) |
Peng Fana2358782015-01-04 17:07:14 +0800258 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
259 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
260#else
Alison Wang6b57ff62014-05-06 09:13:01 +0800261 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800262 qspi_write32(priv->flags, &regs->lut[lut_base],
263 OPRND0(QSPI_CMD_PP) | PAD0(LUT_PAD1) |
264 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
265 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Alison Wang6b57ff62014-05-06 09:13:01 +0800266 else
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800267 qspi_write32(priv->flags, &regs->lut[lut_base],
268 OPRND0(QSPI_CMD_PP_4B) | PAD0(LUT_PAD1) |
269 INSTR0(LUT_CMD) | OPRND1(ADDR32BIT) |
270 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Peng Fana2358782015-01-04 17:07:14 +0800271#endif
Peng Fanafe8e1b2018-01-03 08:52:02 +0800272#if defined(CONFIG_MX6SX) || defined(CONFIG_MX6UL) || \
273 defined(CONFIG_MX6ULL) || defined(CONFIG_MX7D)
Peng Fanb93ab2e2014-12-31 11:01:38 +0800274 /*
275 * To MX6SX, OPRND0(TX_BUFFER_SIZE) can not work correctly.
276 * So, Use IDATSZ in IPCR to determine the size and here set 0.
277 */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800278 qspi_write32(priv->flags, &regs->lut[lut_base + 1], OPRND0(0) |
Peng Fanb93ab2e2014-12-31 11:01:38 +0800279 PAD0(LUT_PAD1) | INSTR0(LUT_WRITE));
280#else
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800281 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
282 OPRND0(TX_BUFFER_SIZE) |
283 PAD0(LUT_PAD1) | INSTR0(LUT_WRITE));
Peng Fanb93ab2e2014-12-31 11:01:38 +0800284#endif
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800285 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
286 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wang6b57ff62014-05-06 09:13:01 +0800287
288 /* READ ID */
289 lut_base = SEQID_RDID * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800290 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDID) |
Alison Wang6b57ff62014-05-06 09:13:01 +0800291 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(8) |
292 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800293 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
294 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
295 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wang6b57ff62014-05-06 09:13:01 +0800296
Peng Fanba4dc8a2014-12-31 11:01:39 +0800297 /* SUB SECTOR 4K ERASE */
298 lut_base = SEQID_BE_4K * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800299 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BE_4K) |
Peng Fanba4dc8a2014-12-31 11:01:39 +0800300 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
301 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
302
Peng Fana2358782015-01-04 17:07:14 +0800303#ifdef CONFIG_SPI_FLASH_BAR
304 /*
305 * BRRD BRWR RDEAR WREAR are all supported, because it is hard to
306 * dynamically check whether to set BRRD BRWR or RDEAR WREAR during
307 * initialization.
308 */
309 lut_base = SEQID_BRRD * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800310 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BRRD) |
Peng Fana2358782015-01-04 17:07:14 +0800311 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
312 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
313
314 lut_base = SEQID_BRWR * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800315 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BRWR) |
Peng Fana2358782015-01-04 17:07:14 +0800316 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
317 PAD1(LUT_PAD1) | INSTR1(LUT_WRITE));
318
319 lut_base = SEQID_RDEAR * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800320 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDEAR) |
Peng Fana2358782015-01-04 17:07:14 +0800321 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
322 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
323
324 lut_base = SEQID_WREAR * 4;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800325 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_WREAR) |
Peng Fana2358782015-01-04 17:07:14 +0800326 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
327 PAD1(LUT_PAD1) | INSTR1(LUT_WRITE));
328#endif
Yuan Yaofebffe82016-03-15 14:36:42 +0800329
330 /*
331 * Read any device register.
332 * Used for Spansion S25FS-S family flash only.
333 */
334 lut_base = SEQID_RDAR * 4;
335 qspi_write32(priv->flags, &regs->lut[lut_base],
336 OPRND0(QSPI_CMD_RDAR) | PAD0(LUT_PAD1) |
337 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
338 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
339 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
340 OPRND0(8) | PAD0(LUT_PAD1) | INSTR0(LUT_DUMMY) |
341 OPRND1(1) | PAD1(LUT_PAD1) |
342 INSTR1(LUT_READ));
343
344 /*
345 * Write any device register.
346 * Used for Spansion S25FS-S family flash only.
347 */
348 lut_base = SEQID_WRAR * 4;
349 qspi_write32(priv->flags, &regs->lut[lut_base],
350 OPRND0(QSPI_CMD_WRAR) | PAD0(LUT_PAD1) |
351 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
352 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
353 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
354 OPRND0(1) | PAD0(LUT_PAD1) | INSTR0(LUT_WRITE));
355
Alison Wang6b57ff62014-05-06 09:13:01 +0800356 /* Lock the LUT */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800357 qspi_write32(priv->flags, &regs->lutkey, LUT_KEY_VALUE);
358 qspi_write32(priv->flags, &regs->lckcr, QSPI_LCKCR_LOCK);
Alison Wang6b57ff62014-05-06 09:13:01 +0800359}
360
Peng Fan5f7f70c2015-01-08 10:40:20 +0800361#if defined(CONFIG_SYS_FSL_QSPI_AHB)
362/*
363 * If we have changed the content of the flash by writing or erasing,
364 * we need to invalidate the AHB buffer. If we do not do so, we may read out
365 * the wrong data. The spec tells us reset the AHB domain and Serial Flash
366 * domain at the same time.
367 */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800368static inline void qspi_ahb_invalid(struct fsl_qspi_priv *priv)
Peng Fan5f7f70c2015-01-08 10:40:20 +0800369{
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800370 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan5f7f70c2015-01-08 10:40:20 +0800371 u32 reg;
372
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800373 reg = qspi_read32(priv->flags, &regs->mcr);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800374 reg |= QSPI_MCR_SWRSTHD_MASK | QSPI_MCR_SWRSTSD_MASK;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800375 qspi_write32(priv->flags, &regs->mcr, reg);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800376
377 /*
378 * The minimum delay : 1 AHB + 2 SFCK clocks.
379 * Delay 1 us is enough.
380 */
381 udelay(1);
382
383 reg &= ~(QSPI_MCR_SWRSTHD_MASK | QSPI_MCR_SWRSTSD_MASK);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800384 qspi_write32(priv->flags, &regs->mcr, reg);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800385}
386
387/* Read out the data from the AHB buffer. */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800388static inline void qspi_ahb_read(struct fsl_qspi_priv *priv, u8 *rxbuf, int len)
Peng Fan5f7f70c2015-01-08 10:40:20 +0800389{
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800390 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan5f7f70c2015-01-08 10:40:20 +0800391 u32 mcr_reg;
Yunhui Cui04e5c6d2016-07-13 10:46:27 +0800392 void *rx_addr = NULL;
Peng Fan5f7f70c2015-01-08 10:40:20 +0800393
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800394 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800395
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800396 qspi_write32(priv->flags, &regs->mcr,
397 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Peng Fan5f7f70c2015-01-08 10:40:20 +0800398 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
399
Yunhui Cui04e5c6d2016-07-13 10:46:27 +0800400 rx_addr = (void *)(uintptr_t)(priv->cur_amba_base + priv->sf_addr);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800401 /* Read out the data directly from the AHB buffer. */
Yunhui Cui04e5c6d2016-07-13 10:46:27 +0800402 memcpy(rxbuf, rx_addr, len);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800403
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800404 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800405}
406
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800407static void qspi_enable_ddr_mode(struct fsl_qspi_priv *priv)
Peng Fan5f7f70c2015-01-08 10:40:20 +0800408{
409 u32 reg, reg2;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800410 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan5f7f70c2015-01-08 10:40:20 +0800411
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800412 reg = qspi_read32(priv->flags, &regs->mcr);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800413 /* Disable the module */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800414 qspi_write32(priv->flags, &regs->mcr, reg | QSPI_MCR_MDIS_MASK);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800415
416 /* Set the Sampling Register for DDR */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800417 reg2 = qspi_read32(priv->flags, &regs->smpr);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800418 reg2 &= ~QSPI_SMPR_DDRSMP_MASK;
419 reg2 |= (2 << QSPI_SMPR_DDRSMP_SHIFT);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800420 qspi_write32(priv->flags, &regs->smpr, reg2);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800421
422 /* Enable the module again (enable the DDR too) */
423 reg |= QSPI_MCR_DDR_EN_MASK;
424 /* Enable bit 29 for imx6sx */
Jagan Teki29e6abd2015-10-23 01:37:18 +0530425 reg |= BIT(29);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800426
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800427 qspi_write32(priv->flags, &regs->mcr, reg);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800428}
429
430/*
431 * There are two different ways to read out the data from the flash:
432 * the "IP Command Read" and the "AHB Command Read".
433 *
434 * The IC guy suggests we use the "AHB Command Read" which is faster
435 * then the "IP Command Read". (What's more is that there is a bug in
436 * the "IP Command Read" in the Vybrid.)
437 *
438 * After we set up the registers for the "AHB Command Read", we can use
439 * the memcpy to read the data directly. A "missed" access to the buffer
440 * causes the controller to clear the buffer, and use the sequence pointed
441 * by the QUADSPI_BFGENCR[SEQID] to initiate a read from the flash.
442 */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800443static void qspi_init_ahb_read(struct fsl_qspi_priv *priv)
Peng Fan5f7f70c2015-01-08 10:40:20 +0800444{
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800445 struct fsl_qspi_regs *regs = priv->regs;
446
Peng Fan5f7f70c2015-01-08 10:40:20 +0800447 /* AHB configuration for access buffer 0/1/2 .*/
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800448 qspi_write32(priv->flags, &regs->buf0cr, QSPI_BUFXCR_INVALID_MSTRID);
449 qspi_write32(priv->flags, &regs->buf1cr, QSPI_BUFXCR_INVALID_MSTRID);
450 qspi_write32(priv->flags, &regs->buf2cr, QSPI_BUFXCR_INVALID_MSTRID);
451 qspi_write32(priv->flags, &regs->buf3cr, QSPI_BUF3CR_ALLMST_MASK |
Peng Fan5f7f70c2015-01-08 10:40:20 +0800452 (0x80 << QSPI_BUF3CR_ADATSZ_SHIFT));
453
454 /* We only use the buffer3 */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800455 qspi_write32(priv->flags, &regs->buf0ind, 0);
456 qspi_write32(priv->flags, &regs->buf1ind, 0);
457 qspi_write32(priv->flags, &regs->buf2ind, 0);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800458
459 /*
460 * Set the default lut sequence for AHB Read.
461 * Parallel mode is disabled.
462 */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800463 qspi_write32(priv->flags, &regs->bfgencr,
Peng Fan5f7f70c2015-01-08 10:40:20 +0800464 SEQID_FAST_READ << QSPI_BFGENCR_SEQID_SHIFT);
465
466 /*Enable DDR Mode*/
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800467 qspi_enable_ddr_mode(priv);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800468}
469#endif
470
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800471#ifdef CONFIG_SPI_FLASH_BAR
472/* Bank register read/write, EAR register read/write */
473static void qspi_op_rdbank(struct fsl_qspi_priv *priv, u8 *rxbuf, u32 len)
Alison Wang6b57ff62014-05-06 09:13:01 +0800474{
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800475 struct fsl_qspi_regs *regs = priv->regs;
476 u32 reg, mcr_reg, data, seqid;
477
478 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
479 qspi_write32(priv->flags, &regs->mcr,
480 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
481 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
482 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
483
484 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
485
486 if (priv->cur_seqid == QSPI_CMD_BRRD)
487 seqid = SEQID_BRRD;
488 else
489 seqid = SEQID_RDEAR;
490
491 qspi_write32(priv->flags, &regs->ipcr,
492 (seqid << QSPI_IPCR_SEQID_SHIFT) | len);
493
494 /* Wait previous command complete */
495 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
496 ;
497
498 while (1) {
Alexander Stein4df24f22017-06-01 09:32:19 +0200499 WATCHDOG_RESET();
500
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800501 reg = qspi_read32(priv->flags, &regs->rbsr);
502 if (reg & QSPI_RBSR_RDBFL_MASK) {
503 data = qspi_read32(priv->flags, &regs->rbdr[0]);
504 data = qspi_endian_xchg(data);
505 memcpy(rxbuf, &data, len);
506 qspi_write32(priv->flags, &regs->mcr,
507 qspi_read32(priv->flags, &regs->mcr) |
508 QSPI_MCR_CLR_RXF_MASK);
509 break;
510 }
511 }
512
513 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
514}
515#endif
516
517static void qspi_op_rdid(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len)
518{
519 struct fsl_qspi_regs *regs = priv->regs;
Gong Qianyu52070142016-01-26 15:06:40 +0800520 u32 mcr_reg, rbsr_reg, data, size;
521 int i;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800522
523 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
524 qspi_write32(priv->flags, &regs->mcr,
525 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
526 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
527 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
528
529 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
530
531 qspi_write32(priv->flags, &regs->ipcr,
532 (SEQID_RDID << QSPI_IPCR_SEQID_SHIFT) | 0);
533 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
534 ;
535
536 i = 0;
Gong Qianyu52070142016-01-26 15:06:40 +0800537 while ((RX_BUFFER_SIZE >= len) && (len > 0)) {
Alexander Stein4df24f22017-06-01 09:32:19 +0200538 WATCHDOG_RESET();
539
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800540 rbsr_reg = qspi_read32(priv->flags, &regs->rbsr);
541 if (rbsr_reg & QSPI_RBSR_RDBFL_MASK) {
542 data = qspi_read32(priv->flags, &regs->rbdr[i]);
543 data = qspi_endian_xchg(data);
Gong Qianyu52070142016-01-26 15:06:40 +0800544 size = (len < 4) ? len : 4;
545 memcpy(rxbuf, &data, size);
546 len -= size;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800547 rxbuf++;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800548 i++;
549 }
550 }
551
552 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
553}
554
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800555/* If not use AHB read, read data from ip interface */
556static void qspi_op_read(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len)
557{
558 struct fsl_qspi_regs *regs = priv->regs;
559 u32 mcr_reg, data;
560 int i, size;
561 u32 to_or_from;
Yuan Yaofebffe82016-03-15 14:36:42 +0800562 u32 seqid;
563
564 if (priv->cur_seqid == QSPI_CMD_RDAR)
565 seqid = SEQID_RDAR;
566 else
567 seqid = SEQID_FAST_READ;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800568
569 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
570 qspi_write32(priv->flags, &regs->mcr,
571 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
572 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
573 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
574
575 to_or_from = priv->sf_addr + priv->cur_amba_base;
576
577 while (len > 0) {
Alexander Steinbeedbc22015-11-04 09:19:10 +0100578 WATCHDOG_RESET();
579
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800580 qspi_write32(priv->flags, &regs->sfar, to_or_from);
581
582 size = (len > RX_BUFFER_SIZE) ?
583 RX_BUFFER_SIZE : len;
584
585 qspi_write32(priv->flags, &regs->ipcr,
Yuan Yaofebffe82016-03-15 14:36:42 +0800586 (seqid << QSPI_IPCR_SEQID_SHIFT) |
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800587 size);
588 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
589 ;
590
591 to_or_from += size;
592 len -= size;
593
594 i = 0;
595 while ((RX_BUFFER_SIZE >= size) && (size > 0)) {
596 data = qspi_read32(priv->flags, &regs->rbdr[i]);
597 data = qspi_endian_xchg(data);
Yuan Yaofebffe82016-03-15 14:36:42 +0800598 if (size < 4)
599 memcpy(rxbuf, &data, size);
600 else
601 memcpy(rxbuf, &data, 4);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800602 rxbuf++;
603 size -= 4;
604 i++;
605 }
606 qspi_write32(priv->flags, &regs->mcr,
607 qspi_read32(priv->flags, &regs->mcr) |
608 QSPI_MCR_CLR_RXF_MASK);
609 }
610
611 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
612}
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800613
614static void qspi_op_write(struct fsl_qspi_priv *priv, u8 *txbuf, u32 len)
615{
616 struct fsl_qspi_regs *regs = priv->regs;
617 u32 mcr_reg, data, reg, status_reg, seqid;
618 int i, size, tx_size;
619 u32 to_or_from = 0;
620
621 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
622 qspi_write32(priv->flags, &regs->mcr,
623 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
624 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
625 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
626
627 status_reg = 0;
628 while ((status_reg & FLASH_STATUS_WEL) != FLASH_STATUS_WEL) {
Alexander Steinbeedbc22015-11-04 09:19:10 +0100629 WATCHDOG_RESET();
630
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800631 qspi_write32(priv->flags, &regs->ipcr,
632 (SEQID_WREN << QSPI_IPCR_SEQID_SHIFT) | 0);
633 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
634 ;
635
636 qspi_write32(priv->flags, &regs->ipcr,
637 (SEQID_RDSR << QSPI_IPCR_SEQID_SHIFT) | 1);
638 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
639 ;
640
641 reg = qspi_read32(priv->flags, &regs->rbsr);
642 if (reg & QSPI_RBSR_RDBFL_MASK) {
643 status_reg = qspi_read32(priv->flags, &regs->rbdr[0]);
644 status_reg = qspi_endian_xchg(status_reg);
645 }
646 qspi_write32(priv->flags, &regs->mcr,
647 qspi_read32(priv->flags, &regs->mcr) |
648 QSPI_MCR_CLR_RXF_MASK);
649 }
650
651 /* Default is page programming */
652 seqid = SEQID_PP;
Yuan Yaofebffe82016-03-15 14:36:42 +0800653 if (priv->cur_seqid == QSPI_CMD_WRAR)
654 seqid = SEQID_WRAR;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800655#ifdef CONFIG_SPI_FLASH_BAR
656 if (priv->cur_seqid == QSPI_CMD_BRWR)
657 seqid = SEQID_BRWR;
658 else if (priv->cur_seqid == QSPI_CMD_WREAR)
659 seqid = SEQID_WREAR;
660#endif
661
662 to_or_from = priv->sf_addr + priv->cur_amba_base;
663
664 qspi_write32(priv->flags, &regs->sfar, to_or_from);
665
666 tx_size = (len > TX_BUFFER_SIZE) ?
667 TX_BUFFER_SIZE : len;
668
Suresh Gupta10509982017-06-05 14:37:20 +0530669 size = tx_size / 16;
670 /*
671 * There must be atleast 128bit data
672 * available in TX FIFO for any pop operation
673 */
674 if (tx_size % 16)
675 size++;
676 for (i = 0; i < size * 4; i++) {
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800677 memcpy(&data, txbuf, 4);
678 data = qspi_endian_xchg(data);
679 qspi_write32(priv->flags, &regs->tbdr, data);
680 txbuf += 4;
681 }
682
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800683 qspi_write32(priv->flags, &regs->ipcr,
684 (seqid << QSPI_IPCR_SEQID_SHIFT) | tx_size);
685 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
686 ;
687
688 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
689}
690
Gong Qianyu940d2b82016-01-26 15:06:41 +0800691static void qspi_op_rdsr(struct fsl_qspi_priv *priv, void *rxbuf, u32 len)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800692{
693 struct fsl_qspi_regs *regs = priv->regs;
694 u32 mcr_reg, reg, data;
695
696 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
697 qspi_write32(priv->flags, &regs->mcr,
698 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
699 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
700 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
701
702 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
703
704 qspi_write32(priv->flags, &regs->ipcr,
705 (SEQID_RDSR << QSPI_IPCR_SEQID_SHIFT) | 0);
706 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
707 ;
708
709 while (1) {
Alexander Stein4df24f22017-06-01 09:32:19 +0200710 WATCHDOG_RESET();
711
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800712 reg = qspi_read32(priv->flags, &regs->rbsr);
713 if (reg & QSPI_RBSR_RDBFL_MASK) {
714 data = qspi_read32(priv->flags, &regs->rbdr[0]);
715 data = qspi_endian_xchg(data);
Gong Qianyu940d2b82016-01-26 15:06:41 +0800716 memcpy(rxbuf, &data, len);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800717 qspi_write32(priv->flags, &regs->mcr,
718 qspi_read32(priv->flags, &regs->mcr) |
719 QSPI_MCR_CLR_RXF_MASK);
720 break;
721 }
722 }
723
724 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
725}
726
727static void qspi_op_erase(struct fsl_qspi_priv *priv)
728{
729 struct fsl_qspi_regs *regs = priv->regs;
730 u32 mcr_reg;
731 u32 to_or_from = 0;
732
733 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
734 qspi_write32(priv->flags, &regs->mcr,
735 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
736 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
737 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
738
739 to_or_from = priv->sf_addr + priv->cur_amba_base;
740 qspi_write32(priv->flags, &regs->sfar, to_or_from);
741
742 qspi_write32(priv->flags, &regs->ipcr,
743 (SEQID_WREN << QSPI_IPCR_SEQID_SHIFT) | 0);
744 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
745 ;
746
747 if (priv->cur_seqid == QSPI_CMD_SE) {
748 qspi_write32(priv->flags, &regs->ipcr,
749 (SEQID_SE << QSPI_IPCR_SEQID_SHIFT) | 0);
750 } else if (priv->cur_seqid == QSPI_CMD_BE_4K) {
751 qspi_write32(priv->flags, &regs->ipcr,
752 (SEQID_BE_4K << QSPI_IPCR_SEQID_SHIFT) | 0);
753 }
754 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
755 ;
756
757 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
758}
759
760int qspi_xfer(struct fsl_qspi_priv *priv, unsigned int bitlen,
761 const void *dout, void *din, unsigned long flags)
762{
763 u32 bytes = DIV_ROUND_UP(bitlen, 8);
764 static u32 wr_sfaddr;
765 u32 txbuf;
766
Alexander Stein4df24f22017-06-01 09:32:19 +0200767 WATCHDOG_RESET();
768
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800769 if (dout) {
770 if (flags & SPI_XFER_BEGIN) {
771 priv->cur_seqid = *(u8 *)dout;
772 memcpy(&txbuf, dout, 4);
773 }
774
775 if (flags == SPI_XFER_END) {
776 priv->sf_addr = wr_sfaddr;
777 qspi_op_write(priv, (u8 *)dout, bytes);
778 return 0;
779 }
780
Yuan Yaofebffe82016-03-15 14:36:42 +0800781 if (priv->cur_seqid == QSPI_CMD_FAST_READ ||
782 priv->cur_seqid == QSPI_CMD_RDAR) {
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800783 priv->sf_addr = swab32(txbuf) & OFFSET_BITS_MASK;
784 } else if ((priv->cur_seqid == QSPI_CMD_SE) ||
785 (priv->cur_seqid == QSPI_CMD_BE_4K)) {
786 priv->sf_addr = swab32(txbuf) & OFFSET_BITS_MASK;
787 qspi_op_erase(priv);
Yuan Yaofebffe82016-03-15 14:36:42 +0800788 } else if (priv->cur_seqid == QSPI_CMD_PP ||
789 priv->cur_seqid == QSPI_CMD_WRAR) {
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800790 wr_sfaddr = swab32(txbuf) & OFFSET_BITS_MASK;
791 } else if ((priv->cur_seqid == QSPI_CMD_BRWR) ||
792 (priv->cur_seqid == QSPI_CMD_WREAR)) {
793#ifdef CONFIG_SPI_FLASH_BAR
794 wr_sfaddr = 0;
795#endif
796 }
797 }
798
799 if (din) {
800 if (priv->cur_seqid == QSPI_CMD_FAST_READ) {
801#ifdef CONFIG_SYS_FSL_QSPI_AHB
802 qspi_ahb_read(priv, din, bytes);
803#else
804 qspi_op_read(priv, din, bytes);
805#endif
Yuan Yaofebffe82016-03-15 14:36:42 +0800806 } else if (priv->cur_seqid == QSPI_CMD_RDAR) {
807 qspi_op_read(priv, din, bytes);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800808 } else if (priv->cur_seqid == QSPI_CMD_RDID)
809 qspi_op_rdid(priv, din, bytes);
810 else if (priv->cur_seqid == QSPI_CMD_RDSR)
Gong Qianyu940d2b82016-01-26 15:06:41 +0800811 qspi_op_rdsr(priv, din, bytes);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800812#ifdef CONFIG_SPI_FLASH_BAR
813 else if ((priv->cur_seqid == QSPI_CMD_BRRD) ||
814 (priv->cur_seqid == QSPI_CMD_RDEAR)) {
815 priv->sf_addr = 0;
816 qspi_op_rdbank(priv, din, bytes);
817 }
818#endif
819 }
820
821#ifdef CONFIG_SYS_FSL_QSPI_AHB
822 if ((priv->cur_seqid == QSPI_CMD_SE) ||
823 (priv->cur_seqid == QSPI_CMD_PP) ||
824 (priv->cur_seqid == QSPI_CMD_BE_4K) ||
825 (priv->cur_seqid == QSPI_CMD_WREAR) ||
826 (priv->cur_seqid == QSPI_CMD_BRWR))
827 qspi_ahb_invalid(priv);
828#endif
829
830 return 0;
831}
832
833void qspi_module_disable(struct fsl_qspi_priv *priv, u8 disable)
834{
835 u32 mcr_val;
836
837 mcr_val = qspi_read32(priv->flags, &priv->regs->mcr);
838 if (disable)
839 mcr_val |= QSPI_MCR_MDIS_MASK;
840 else
841 mcr_val &= ~QSPI_MCR_MDIS_MASK;
842 qspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
843}
844
845void qspi_cfg_smpr(struct fsl_qspi_priv *priv, u32 clear_bits, u32 set_bits)
846{
847 u32 smpr_val;
848
849 smpr_val = qspi_read32(priv->flags, &priv->regs->smpr);
850 smpr_val &= ~clear_bits;
851 smpr_val |= set_bits;
852 qspi_write32(priv->flags, &priv->regs->smpr, smpr_val);
853}
854#ifndef CONFIG_DM_SPI
855static unsigned long spi_bases[] = {
856 QSPI0_BASE_ADDR,
857#ifdef CONFIG_MX6SX
858 QSPI1_BASE_ADDR,
859#endif
860};
861
862static unsigned long amba_bases[] = {
863 QSPI0_AMBA_BASE,
864#ifdef CONFIG_MX6SX
865 QSPI1_AMBA_BASE,
866#endif
867};
868
869static inline struct fsl_qspi *to_qspi_spi(struct spi_slave *slave)
870{
871 return container_of(slave, struct fsl_qspi, slave);
Alison Wang6b57ff62014-05-06 09:13:01 +0800872}
873
874struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
875 unsigned int max_hz, unsigned int mode)
876{
York Sun3c6b1762016-10-05 13:19:08 -0700877 u32 mcr_val;
Alison Wang6b57ff62014-05-06 09:13:01 +0800878 struct fsl_qspi *qspi;
879 struct fsl_qspi_regs *regs;
Peng Fan5f7f70c2015-01-08 10:40:20 +0800880 u32 total_size;
Alison Wang6b57ff62014-05-06 09:13:01 +0800881
882 if (bus >= ARRAY_SIZE(spi_bases))
883 return NULL;
884
Peng Faned0c81c2014-12-31 11:01:37 +0800885 if (cs >= FSL_QSPI_FLASH_NUM)
886 return NULL;
887
Alison Wang6b57ff62014-05-06 09:13:01 +0800888 qspi = spi_alloc_slave(struct fsl_qspi, bus, cs);
889 if (!qspi)
890 return NULL;
891
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800892#ifdef CONFIG_SYS_FSL_QSPI_BE
893 qspi->priv.flags |= QSPI_FLAG_REGMAP_ENDIAN_BIG;
894#endif
895
896 regs = (struct fsl_qspi_regs *)spi_bases[bus];
897 qspi->priv.regs = regs;
Peng Faned0c81c2014-12-31 11:01:37 +0800898 /*
899 * According cs, use different amba_base to choose the
900 * corresponding flash devices.
901 *
902 * If not, only one flash device is used even if passing
903 * different cs using `sf probe`
904 */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800905 qspi->priv.cur_amba_base = amba_bases[bus] + cs * FSL_QSPI_FLASH_SIZE;
Alison Wang6b57ff62014-05-06 09:13:01 +0800906
907 qspi->slave.max_write_size = TX_BUFFER_SIZE;
908
York Sun3c6b1762016-10-05 13:19:08 -0700909 mcr_val = qspi_read32(qspi->priv.flags, &regs->mcr);
Peng Fanafe8e1b2018-01-03 08:52:02 +0800910
911 /* Set endianness to LE for i.mx */
912 if (IS_ENABLED(CONFIG_MX6) || IS_ENABLED(CONFIG_MX7))
913 mcr_val = QSPI_MCR_END_CFD_LE;
914
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800915 qspi_write32(qspi->priv.flags, &regs->mcr,
York Sun3c6b1762016-10-05 13:19:08 -0700916 QSPI_MCR_RESERVED_MASK | QSPI_MCR_MDIS_MASK |
917 (mcr_val & QSPI_MCR_END_CFD_MASK));
Alison Wang6b57ff62014-05-06 09:13:01 +0800918
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800919 qspi_cfg_smpr(&qspi->priv,
920 ~(QSPI_SMPR_FSDLY_MASK | QSPI_SMPR_DDRSMP_MASK |
921 QSPI_SMPR_FSPHS_MASK | QSPI_SMPR_HSENA_MASK), 0);
Alison Wang6b57ff62014-05-06 09:13:01 +0800922
923 total_size = FSL_QSPI_FLASH_SIZE * FSL_QSPI_FLASH_NUM;
Peng Faned0c81c2014-12-31 11:01:37 +0800924 /*
925 * Any read access to non-implemented addresses will provide
926 * undefined results.
927 *
928 * In case single die flash devices, TOP_ADDR_MEMA2 and
929 * TOP_ADDR_MEMB2 should be initialized/programmed to
930 * TOP_ADDR_MEMA1 and TOP_ADDR_MEMB1 respectively - in effect,
931 * setting the size of these devices to 0. This would ensure
932 * that the complete memory map is assigned to only one flash device.
933 */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800934 qspi_write32(qspi->priv.flags, &regs->sfa1ad,
935 FSL_QSPI_FLASH_SIZE | amba_bases[bus]);
936 qspi_write32(qspi->priv.flags, &regs->sfa2ad,
937 FSL_QSPI_FLASH_SIZE | amba_bases[bus]);
938 qspi_write32(qspi->priv.flags, &regs->sfb1ad,
939 total_size | amba_bases[bus]);
940 qspi_write32(qspi->priv.flags, &regs->sfb2ad,
941 total_size | amba_bases[bus]);
Alison Wang6b57ff62014-05-06 09:13:01 +0800942
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800943 qspi_set_lut(&qspi->priv);
Alison Wang6b57ff62014-05-06 09:13:01 +0800944
Peng Fan5f7f70c2015-01-08 10:40:20 +0800945#ifdef CONFIG_SYS_FSL_QSPI_AHB
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800946 qspi_init_ahb_read(&qspi->priv);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800947#endif
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800948
949 qspi_module_disable(&qspi->priv, 0);
950
Alison Wang6b57ff62014-05-06 09:13:01 +0800951 return &qspi->slave;
952}
953
954void spi_free_slave(struct spi_slave *slave)
955{
956 struct fsl_qspi *qspi = to_qspi_spi(slave);
957
958 free(qspi);
959}
960
961int spi_claim_bus(struct spi_slave *slave)
962{
963 return 0;
964}
965
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800966void spi_release_bus(struct spi_slave *slave)
Peng Fana2358782015-01-04 17:07:14 +0800967{
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800968 /* Nothing to do */
Alison Wang6b57ff62014-05-06 09:13:01 +0800969}
970
971int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
972 const void *dout, void *din, unsigned long flags)
973{
974 struct fsl_qspi *qspi = to_qspi_spi(slave);
Alison Wang6b57ff62014-05-06 09:13:01 +0800975
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800976 return qspi_xfer(&qspi->priv, bitlen, dout, din, flags);
977}
Alison Wang6b57ff62014-05-06 09:13:01 +0800978
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800979void spi_init(void)
980{
981 /* Nothing to do */
982}
Peng Fan5f7f70c2015-01-08 10:40:20 +0800983#else
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800984static int fsl_qspi_child_pre_probe(struct udevice *dev)
985{
Simon Glassbcbe3d12015-09-28 23:32:01 -0600986 struct spi_slave *slave = dev_get_parent_priv(dev);
Alison Wang6b57ff62014-05-06 09:13:01 +0800987
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800988 slave->max_write_size = TX_BUFFER_SIZE;
Peng Fan5f7f70c2015-01-08 10:40:20 +0800989
Alison Wang6b57ff62014-05-06 09:13:01 +0800990 return 0;
991}
992
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800993static int fsl_qspi_probe(struct udevice *bus)
994{
York Sun3c6b1762016-10-05 13:19:08 -0700995 u32 mcr_val;
Yuan Yao4e147412016-03-15 14:36:41 +0800996 u32 amba_size_per_chip;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800997 struct fsl_qspi_platdata *plat = dev_get_platdata(bus);
998 struct fsl_qspi_priv *priv = dev_get_priv(bus);
999 struct dm_spi_bus *dm_spi_bus;
Suresh Gupta1c631da2017-08-30 20:06:33 +05301000 int i, ret;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001001
1002 dm_spi_bus = bus->uclass_priv;
1003
1004 dm_spi_bus->max_hz = plat->speed_hz;
1005
Gong Qianyuc2a4cb12016-01-26 15:06:39 +08001006 priv->regs = (struct fsl_qspi_regs *)(uintptr_t)plat->reg_base;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001007 priv->flags = plat->flags;
1008
1009 priv->speed_hz = plat->speed_hz;
Yuan Yaobf9bffa2016-03-15 14:36:40 +08001010 /*
1011 * QSPI SFADR width is 32bits, the max dest addr is 4GB-1.
1012 * AMBA memory zone should be located on the 0~4GB space
1013 * even on a 64bits cpu.
1014 */
1015 priv->amba_base[0] = (u32)plat->amba_base;
1016 priv->amba_total_size = (u32)plat->amba_total_size;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001017 priv->flash_num = plat->flash_num;
1018 priv->num_chipselect = plat->num_chipselect;
1019
Suresh Gupta1c631da2017-08-30 20:06:33 +05301020 /* make sure controller is not busy anywhere */
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +01001021 ret = wait_for_bit_le32(&priv->regs->sr,
1022 QSPI_SR_BUSY_MASK |
1023 QSPI_SR_AHB_ACC_MASK |
1024 QSPI_SR_IP_ACC_MASK,
1025 false, 100, false);
Suresh Gupta1c631da2017-08-30 20:06:33 +05301026
1027 if (ret) {
1028 debug("ERROR : The controller is busy\n");
1029 return ret;
1030 }
1031
York Sun3c6b1762016-10-05 13:19:08 -07001032 mcr_val = qspi_read32(priv->flags, &priv->regs->mcr);
Peng Fanafe8e1b2018-01-03 08:52:02 +08001033
1034 /* Set endianness to LE for i.mx */
1035 if (IS_ENABLED(CONFIG_MX6) || IS_ENABLED(CONFIG_MX7))
1036 mcr_val = QSPI_MCR_END_CFD_LE;
1037
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001038 qspi_write32(priv->flags, &priv->regs->mcr,
York Sun3c6b1762016-10-05 13:19:08 -07001039 QSPI_MCR_RESERVED_MASK | QSPI_MCR_MDIS_MASK |
1040 (mcr_val & QSPI_MCR_END_CFD_MASK));
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001041
1042 qspi_cfg_smpr(priv, ~(QSPI_SMPR_FSDLY_MASK | QSPI_SMPR_DDRSMP_MASK |
1043 QSPI_SMPR_FSPHS_MASK | QSPI_SMPR_HSENA_MASK), 0);
1044
Yuan Yao4e147412016-03-15 14:36:41 +08001045 /*
1046 * Assign AMBA memory zone for every chipselect
1047 * QuadSPI has two channels, every channel has two chipselects.
1048 * If the property 'num-cs' in dts is 2, the AMBA memory will be divided
1049 * into two parts and assign to every channel. This indicate that every
1050 * channel only has one valid chipselect.
1051 * If the property 'num-cs' in dts is 4, the AMBA memory will be divided
1052 * into four parts and assign to every chipselect.
1053 * Every channel will has two valid chipselects.
1054 */
1055 amba_size_per_chip = priv->amba_total_size >>
1056 (priv->num_chipselect >> 1);
1057 for (i = 1 ; i < priv->num_chipselect ; i++)
1058 priv->amba_base[i] =
1059 amba_size_per_chip + priv->amba_base[i - 1];
1060
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001061 /*
1062 * Any read access to non-implemented addresses will provide
1063 * undefined results.
1064 *
1065 * In case single die flash devices, TOP_ADDR_MEMA2 and
1066 * TOP_ADDR_MEMB2 should be initialized/programmed to
1067 * TOP_ADDR_MEMA1 and TOP_ADDR_MEMB1 respectively - in effect,
1068 * setting the size of these devices to 0. This would ensure
1069 * that the complete memory map is assigned to only one flash device.
1070 */
Suresh Gupta38a5c572017-02-21 14:26:46 +05301071 qspi_write32(priv->flags, &priv->regs->sfa1ad,
1072 priv->amba_base[0] + amba_size_per_chip);
Yuan Yao4e147412016-03-15 14:36:41 +08001073 switch (priv->num_chipselect) {
Suresh Gupta38a5c572017-02-21 14:26:46 +05301074 case 1:
1075 break;
Yuan Yao4e147412016-03-15 14:36:41 +08001076 case 2:
1077 qspi_write32(priv->flags, &priv->regs->sfa2ad,
1078 priv->amba_base[1]);
1079 qspi_write32(priv->flags, &priv->regs->sfb1ad,
1080 priv->amba_base[1] + amba_size_per_chip);
1081 qspi_write32(priv->flags, &priv->regs->sfb2ad,
1082 priv->amba_base[1] + amba_size_per_chip);
1083 break;
1084 case 4:
1085 qspi_write32(priv->flags, &priv->regs->sfa2ad,
1086 priv->amba_base[2]);
1087 qspi_write32(priv->flags, &priv->regs->sfb1ad,
1088 priv->amba_base[3]);
1089 qspi_write32(priv->flags, &priv->regs->sfb2ad,
1090 priv->amba_base[3] + amba_size_per_chip);
1091 break;
1092 default:
1093 debug("Error: Unsupported chipselect number %u!\n",
1094 priv->num_chipselect);
1095 qspi_module_disable(priv, 1);
1096 return -EINVAL;
1097 }
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001098
1099 qspi_set_lut(priv);
1100
1101#ifdef CONFIG_SYS_FSL_QSPI_AHB
1102 qspi_init_ahb_read(priv);
1103#endif
1104
1105 qspi_module_disable(priv, 0);
1106
1107 return 0;
1108}
1109
1110static int fsl_qspi_ofdata_to_platdata(struct udevice *bus)
1111{
Yuan Yaobf9bffa2016-03-15 14:36:40 +08001112 struct fdt_resource res_regs, res_mem;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001113 struct fsl_qspi_platdata *plat = bus->platdata;
1114 const void *blob = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -07001115 int node = dev_of_offset(bus);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001116 int ret, flash_num = 0, subnode;
1117
1118 if (fdtdec_get_bool(blob, node, "big-endian"))
1119 plat->flags |= QSPI_FLAG_REGMAP_ENDIAN_BIG;
1120
Yuan Yaobf9bffa2016-03-15 14:36:40 +08001121 ret = fdt_get_named_resource(blob, node, "reg", "reg-names",
1122 "QuadSPI", &res_regs);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001123 if (ret) {
Yuan Yaobf9bffa2016-03-15 14:36:40 +08001124 debug("Error: can't get regs base addresses(ret = %d)!\n", ret);
1125 return -ENOMEM;
1126 }
1127 ret = fdt_get_named_resource(blob, node, "reg", "reg-names",
1128 "QuadSPI-memory", &res_mem);
1129 if (ret) {
1130 debug("Error: can't get AMBA base addresses(ret = %d)!\n", ret);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001131 return -ENOMEM;
1132 }
1133
1134 /* Count flash numbers */
Simon Glassdf87e6b2016-10-02 17:59:29 -06001135 fdt_for_each_subnode(subnode, blob, node)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001136 ++flash_num;
1137
1138 if (flash_num == 0) {
1139 debug("Error: Missing flashes!\n");
1140 return -ENODEV;
1141 }
1142
1143 plat->speed_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
1144 FSL_QSPI_DEFAULT_SCK_FREQ);
1145 plat->num_chipselect = fdtdec_get_int(blob, node, "num-cs",
1146 FSL_QSPI_MAX_CHIPSELECT_NUM);
1147
Yuan Yaobf9bffa2016-03-15 14:36:40 +08001148 plat->reg_base = res_regs.start;
1149 plat->amba_base = res_mem.start;
1150 plat->amba_total_size = res_mem.end - res_mem.start + 1;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001151 plat->flash_num = flash_num;
1152
Yuan Yaobf9bffa2016-03-15 14:36:40 +08001153 debug("%s: regs=<0x%llx> <0x%llx, 0x%llx>, max-frequency=%d, endianess=%s\n",
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001154 __func__,
Yuan Yaobf9bffa2016-03-15 14:36:40 +08001155 (u64)plat->reg_base,
1156 (u64)plat->amba_base,
1157 (u64)plat->amba_total_size,
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001158 plat->speed_hz,
1159 plat->flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ? "be" : "le"
1160 );
1161
1162 return 0;
1163}
1164
1165static int fsl_qspi_xfer(struct udevice *dev, unsigned int bitlen,
1166 const void *dout, void *din, unsigned long flags)
1167{
1168 struct fsl_qspi_priv *priv;
1169 struct udevice *bus;
1170
1171 bus = dev->parent;
1172 priv = dev_get_priv(bus);
1173
1174 return qspi_xfer(priv, bitlen, dout, din, flags);
1175}
1176
1177static int fsl_qspi_claim_bus(struct udevice *dev)
1178{
1179 struct fsl_qspi_priv *priv;
1180 struct udevice *bus;
1181 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
Suresh Gupta1c631da2017-08-30 20:06:33 +05301182 int ret;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001183
1184 bus = dev->parent;
1185 priv = dev_get_priv(bus);
1186
Suresh Gupta1c631da2017-08-30 20:06:33 +05301187 /* make sure controller is not busy anywhere */
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +01001188 ret = wait_for_bit_le32(&priv->regs->sr,
1189 QSPI_SR_BUSY_MASK |
1190 QSPI_SR_AHB_ACC_MASK |
1191 QSPI_SR_IP_ACC_MASK,
1192 false, 100, false);
Suresh Gupta1c631da2017-08-30 20:06:33 +05301193
1194 if (ret) {
1195 debug("ERROR : The controller is busy\n");
1196 return ret;
1197 }
1198
Yuan Yao4e147412016-03-15 14:36:41 +08001199 priv->cur_amba_base = priv->amba_base[slave_plat->cs];
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001200
1201 qspi_module_disable(priv, 0);
1202
1203 return 0;
1204}
1205
1206static int fsl_qspi_release_bus(struct udevice *dev)
1207{
1208 struct fsl_qspi_priv *priv;
1209 struct udevice *bus;
1210
1211 bus = dev->parent;
1212 priv = dev_get_priv(bus);
1213
1214 qspi_module_disable(priv, 1);
1215
1216 return 0;
1217}
1218
1219static int fsl_qspi_set_speed(struct udevice *bus, uint speed)
Alison Wang6b57ff62014-05-06 09:13:01 +08001220{
1221 /* Nothing to do */
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001222 return 0;
Alison Wang6b57ff62014-05-06 09:13:01 +08001223}
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001224
1225static int fsl_qspi_set_mode(struct udevice *bus, uint mode)
1226{
1227 /* Nothing to do */
1228 return 0;
1229}
1230
1231static const struct dm_spi_ops fsl_qspi_ops = {
1232 .claim_bus = fsl_qspi_claim_bus,
1233 .release_bus = fsl_qspi_release_bus,
1234 .xfer = fsl_qspi_xfer,
1235 .set_speed = fsl_qspi_set_speed,
1236 .set_mode = fsl_qspi_set_mode,
1237};
1238
1239static const struct udevice_id fsl_qspi_ids[] = {
1240 { .compatible = "fsl,vf610-qspi" },
1241 { .compatible = "fsl,imx6sx-qspi" },
Peng Fanafe8e1b2018-01-03 08:52:02 +08001242 { .compatible = "fsl,imx6ul-qspi" },
1243 { .compatible = "fsl,imx7d-qspi" },
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +08001244 { }
1245};
1246
1247U_BOOT_DRIVER(fsl_qspi) = {
1248 .name = "fsl_qspi",
1249 .id = UCLASS_SPI,
1250 .of_match = fsl_qspi_ids,
1251 .ops = &fsl_qspi_ops,
1252 .ofdata_to_platdata = fsl_qspi_ofdata_to_platdata,
1253 .platdata_auto_alloc_size = sizeof(struct fsl_qspi_platdata),
1254 .priv_auto_alloc_size = sizeof(struct fsl_qspi_priv),
1255 .probe = fsl_qspi_probe,
1256 .child_pre_probe = fsl_qspi_child_pre_probe,
1257};
1258#endif