blob: 542b6cfe355b3e3212f7cb59d5c49792e1333e4c [file] [log] [blame]
Alison Wangc7410e32014-05-06 09:13:01 +08001/*
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08002 * Copyright 2013-2015 Freescale Semiconductor, Inc.
Alison Wangc7410e32014-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.com221f2e12015-04-01 11:10:40 +080014#include <dm.h>
15#include <errno.h>
Alexander Stein9b2c8a92015-11-04 09:19:10 +010016#include <watchdog.h>
Alison Wangc7410e32014-05-06 09:13:01 +080017#include "fsl_qspi.h"
18
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080019DECLARE_GLOBAL_DATA_PTR;
20
Alison Wangc7410e32014-05-06 09:13:01 +080021#define RX_BUFFER_SIZE 0x80
Peng Fan828e4682014-12-31 11:01:38 +080022#ifdef CONFIG_MX6SX
23#define TX_BUFFER_SIZE 0x200
24#else
Alison Wangc7410e32014-05-06 09:13:01 +080025#define TX_BUFFER_SIZE 0x40
Peng Fan828e4682014-12-31 11:01:38 +080026#endif
Alison Wangc7410e32014-05-06 09:13:01 +080027
Gong Qianyu44f65f82015-12-14 18:26:25 +080028#define OFFSET_BITS_MASK GENMASK(23, 0)
Alison Wangc7410e32014-05-06 09:13:01 +080029
30#define FLASH_STATUS_WEL 0x02
31
32/* SEQID */
33#define SEQID_WREN 1
34#define SEQID_FAST_READ 2
35#define SEQID_RDSR 3
36#define SEQID_SE 4
37#define SEQID_CHIP_ERASE 5
38#define SEQID_PP 6
39#define SEQID_RDID 7
Peng Fan3642a872014-12-31 11:01:39 +080040#define SEQID_BE_4K 8
Peng Fan3a344482015-01-04 17:07:14 +080041#ifdef CONFIG_SPI_FLASH_BAR
42#define SEQID_BRRD 9
43#define SEQID_BRWR 10
44#define SEQID_RDEAR 11
45#define SEQID_WREAR 12
46#endif
Alison Wangc7410e32014-05-06 09:13:01 +080047
Peng Fanff162342014-12-31 11:01:36 +080048/* QSPI CMD */
49#define QSPI_CMD_PP 0x02 /* Page program (up to 256 bytes) */
50#define QSPI_CMD_RDSR 0x05 /* Read status register */
51#define QSPI_CMD_WREN 0x06 /* Write enable */
52#define QSPI_CMD_FAST_READ 0x0b /* Read data bytes (high frequency) */
Peng Fan3642a872014-12-31 11:01:39 +080053#define QSPI_CMD_BE_4K 0x20 /* 4K erase */
Peng Fanff162342014-12-31 11:01:36 +080054#define QSPI_CMD_CHIP_ERASE 0xc7 /* Erase whole flash chip */
55#define QSPI_CMD_SE 0xd8 /* Sector erase (usually 64KiB) */
56#define QSPI_CMD_RDID 0x9f /* Read JEDEC ID */
Alison Wangc7410e32014-05-06 09:13:01 +080057
Peng Fan3a344482015-01-04 17:07:14 +080058/* Used for Micron, winbond and Macronix flashes */
59#define QSPI_CMD_WREAR 0xc5 /* EAR register write */
60#define QSPI_CMD_RDEAR 0xc8 /* EAR reigster read */
61
62/* Used for Spansion flashes only. */
63#define QSPI_CMD_BRRD 0x16 /* Bank register read */
64#define QSPI_CMD_BRWR 0x17 /* Bank register write */
65
Peng Fanff162342014-12-31 11:01:36 +080066/* 4-byte address QSPI CMD - used on Spansion and some Macronix flashes */
67#define QSPI_CMD_FAST_READ_4B 0x0c /* Read data bytes (high frequency) */
68#define QSPI_CMD_PP_4B 0x12 /* Page program (up to 256 bytes) */
69#define QSPI_CMD_SE_4B 0xdc /* Sector erase (usually 64KiB) */
Alison Wangc7410e32014-05-06 09:13:01 +080070
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080071/* fsl_qspi_platdata flags */
Jagan Tekic97ca922015-10-23 01:37:18 +053072#define QSPI_FLAG_REGMAP_ENDIAN_BIG BIT(0)
Alison Wangc7410e32014-05-06 09:13:01 +080073
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080074/* default SCK frequency, unit: HZ */
75#define FSL_QSPI_DEFAULT_SCK_FREQ 50000000
Alison Wangc7410e32014-05-06 09:13:01 +080076
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080077/* QSPI max chipselect signals number */
78#define FSL_QSPI_MAX_CHIPSELECT_NUM 4
79
80#ifdef CONFIG_DM_SPI
81/**
82 * struct fsl_qspi_platdata - platform data for Freescale QSPI
83 *
84 * @flags: Flags for QSPI QSPI_FLAG_...
85 * @speed_hz: Default SCK frequency
86 * @reg_base: Base address of QSPI registers
87 * @amba_base: Base address of QSPI memory mapping
88 * @amba_total_size: size of QSPI memory mapping
89 * @flash_num: Number of active slave devices
90 * @num_chipselect: Number of QSPI chipselect signals
91 */
92struct fsl_qspi_platdata {
93 u32 flags;
94 u32 speed_hz;
95 u32 reg_base;
96 u32 amba_base;
97 u32 amba_total_size;
98 u32 flash_num;
99 u32 num_chipselect;
100};
Peng Fan828e4682014-12-31 11:01:38 +0800101#endif
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800102
103/**
104 * struct fsl_qspi_priv - private data for Freescale QSPI
105 *
106 * @flags: Flags for QSPI QSPI_FLAG_...
107 * @bus_clk: QSPI input clk frequency
108 * @speed_hz: Default SCK frequency
109 * @cur_seqid: current LUT table sequence id
110 * @sf_addr: flash access offset
111 * @amba_base: Base address of QSPI memory mapping of every CS
112 * @amba_total_size: size of QSPI memory mapping
113 * @cur_amba_base: Base address of QSPI memory mapping of current CS
114 * @flash_num: Number of active slave devices
115 * @num_chipselect: Number of QSPI chipselect signals
116 * @regs: Point to QSPI register structure for I/O access
117 */
118struct fsl_qspi_priv {
119 u32 flags;
120 u32 bus_clk;
121 u32 speed_hz;
122 u32 cur_seqid;
123 u32 sf_addr;
124 u32 amba_base[FSL_QSPI_MAX_CHIPSELECT_NUM];
125 u32 amba_total_size;
126 u32 cur_amba_base;
127 u32 flash_num;
128 u32 num_chipselect;
129 struct fsl_qspi_regs *regs;
Alison Wangc7410e32014-05-06 09:13:01 +0800130};
131
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800132#ifndef CONFIG_DM_SPI
Alison Wangc7410e32014-05-06 09:13:01 +0800133struct fsl_qspi {
134 struct spi_slave slave;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800135 struct fsl_qspi_priv priv;
Alison Wangc7410e32014-05-06 09:13:01 +0800136};
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800137#endif
138
139static u32 qspi_read32(u32 flags, u32 *addr)
140{
141 return flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ?
142 in_be32(addr) : in_le32(addr);
143}
144
145static void qspi_write32(u32 flags, u32 *addr, u32 val)
146{
147 flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ?
148 out_be32(addr, val) : out_le32(addr, val);
149}
Alison Wangc7410e32014-05-06 09:13:01 +0800150
151/* QSPI support swapping the flash read/write data
152 * in hardware for LS102xA, but not for VF610 */
153static inline u32 qspi_endian_xchg(u32 data)
154{
155#ifdef CONFIG_VF610
156 return swab32(data);
157#else
158 return data;
159#endif
160}
161
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800162static void qspi_set_lut(struct fsl_qspi_priv *priv)
Alison Wangc7410e32014-05-06 09:13:01 +0800163{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800164 struct fsl_qspi_regs *regs = priv->regs;
Alison Wangc7410e32014-05-06 09:13:01 +0800165 u32 lut_base;
166
167 /* Unlock the LUT */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800168 qspi_write32(priv->flags, &regs->lutkey, LUT_KEY_VALUE);
169 qspi_write32(priv->flags, &regs->lckcr, QSPI_LCKCR_UNLOCK);
Alison Wangc7410e32014-05-06 09:13:01 +0800170
171 /* Write Enable */
172 lut_base = SEQID_WREN * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800173 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_WREN) |
Alison Wangc7410e32014-05-06 09:13:01 +0800174 PAD0(LUT_PAD1) | INSTR0(LUT_CMD));
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800175 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
176 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
177 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800178
179 /* Fast Read */
180 lut_base = SEQID_FAST_READ * 4;
Peng Fan3a344482015-01-04 17:07:14 +0800181#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800182 qspi_write32(priv->flags, &regs->lut[lut_base],
183 OPRND0(QSPI_CMD_FAST_READ) | PAD0(LUT_PAD1) |
184 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
Peng Fan3a344482015-01-04 17:07:14 +0800185 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
186#else
Alison Wangc7410e32014-05-06 09:13:01 +0800187 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800188 qspi_write32(priv->flags, &regs->lut[lut_base],
189 OPRND0(QSPI_CMD_FAST_READ) | PAD0(LUT_PAD1) |
190 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
191 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Alison Wangc7410e32014-05-06 09:13:01 +0800192 else
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800193 qspi_write32(priv->flags, &regs->lut[lut_base],
Peng Fanff162342014-12-31 11:01:36 +0800194 OPRND0(QSPI_CMD_FAST_READ_4B) |
195 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) |
196 OPRND1(ADDR32BIT) | PAD1(LUT_PAD1) |
197 INSTR1(LUT_ADDR));
Peng Fan3a344482015-01-04 17:07:14 +0800198#endif
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800199 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
200 OPRND0(8) | PAD0(LUT_PAD1) | INSTR0(LUT_DUMMY) |
201 OPRND1(RX_BUFFER_SIZE) | PAD1(LUT_PAD1) |
202 INSTR1(LUT_READ));
203 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
204 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800205
206 /* Read Status */
207 lut_base = SEQID_RDSR * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800208 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDSR) |
Alison Wangc7410e32014-05-06 09:13:01 +0800209 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
210 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800211 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
212 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
213 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800214
215 /* Erase a sector */
216 lut_base = SEQID_SE * 4;
Peng Fan3a344482015-01-04 17:07:14 +0800217#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800218 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_SE) |
Peng Fan3a344482015-01-04 17:07:14 +0800219 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
220 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
221#else
Alison Wangc7410e32014-05-06 09:13:01 +0800222 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800223 qspi_write32(priv->flags, &regs->lut[lut_base],
224 OPRND0(QSPI_CMD_SE) | PAD0(LUT_PAD1) |
225 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
226 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Alison Wangc7410e32014-05-06 09:13:01 +0800227 else
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800228 qspi_write32(priv->flags, &regs->lut[lut_base],
229 OPRND0(QSPI_CMD_SE_4B) | PAD0(LUT_PAD1) |
230 INSTR0(LUT_CMD) | OPRND1(ADDR32BIT) |
231 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Peng Fan3a344482015-01-04 17:07:14 +0800232#endif
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800233 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
234 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
235 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800236
237 /* Erase the whole chip */
238 lut_base = SEQID_CHIP_ERASE * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800239 qspi_write32(priv->flags, &regs->lut[lut_base],
240 OPRND0(QSPI_CMD_CHIP_ERASE) |
241 PAD0(LUT_PAD1) | INSTR0(LUT_CMD));
242 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
243 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
244 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800245
246 /* Page Program */
247 lut_base = SEQID_PP * 4;
Peng Fan3a344482015-01-04 17:07:14 +0800248#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800249 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_PP) |
Peng Fan3a344482015-01-04 17:07:14 +0800250 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
251 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
252#else
Alison Wangc7410e32014-05-06 09:13:01 +0800253 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800254 qspi_write32(priv->flags, &regs->lut[lut_base],
255 OPRND0(QSPI_CMD_PP) | PAD0(LUT_PAD1) |
256 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
257 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Alison Wangc7410e32014-05-06 09:13:01 +0800258 else
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800259 qspi_write32(priv->flags, &regs->lut[lut_base],
260 OPRND0(QSPI_CMD_PP_4B) | PAD0(LUT_PAD1) |
261 INSTR0(LUT_CMD) | OPRND1(ADDR32BIT) |
262 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Peng Fan3a344482015-01-04 17:07:14 +0800263#endif
Peng Fan828e4682014-12-31 11:01:38 +0800264#ifdef CONFIG_MX6SX
265 /*
266 * To MX6SX, OPRND0(TX_BUFFER_SIZE) can not work correctly.
267 * So, Use IDATSZ in IPCR to determine the size and here set 0.
268 */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800269 qspi_write32(priv->flags, &regs->lut[lut_base + 1], OPRND0(0) |
Peng Fan828e4682014-12-31 11:01:38 +0800270 PAD0(LUT_PAD1) | INSTR0(LUT_WRITE));
271#else
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800272 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
273 OPRND0(TX_BUFFER_SIZE) |
274 PAD0(LUT_PAD1) | INSTR0(LUT_WRITE));
Peng Fan828e4682014-12-31 11:01:38 +0800275#endif
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800276 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
277 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800278
279 /* READ ID */
280 lut_base = SEQID_RDID * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800281 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDID) |
Alison Wangc7410e32014-05-06 09:13:01 +0800282 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(8) |
283 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800284 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
285 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
286 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800287
Peng Fan3642a872014-12-31 11:01:39 +0800288 /* SUB SECTOR 4K ERASE */
289 lut_base = SEQID_BE_4K * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800290 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BE_4K) |
Peng Fan3642a872014-12-31 11:01:39 +0800291 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
292 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
293
Peng Fan3a344482015-01-04 17:07:14 +0800294#ifdef CONFIG_SPI_FLASH_BAR
295 /*
296 * BRRD BRWR RDEAR WREAR are all supported, because it is hard to
297 * dynamically check whether to set BRRD BRWR or RDEAR WREAR during
298 * initialization.
299 */
300 lut_base = SEQID_BRRD * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800301 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BRRD) |
Peng Fan3a344482015-01-04 17:07:14 +0800302 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
303 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
304
305 lut_base = SEQID_BRWR * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800306 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BRWR) |
Peng Fan3a344482015-01-04 17:07:14 +0800307 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
308 PAD1(LUT_PAD1) | INSTR1(LUT_WRITE));
309
310 lut_base = SEQID_RDEAR * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800311 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDEAR) |
Peng Fan3a344482015-01-04 17:07:14 +0800312 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
313 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
314
315 lut_base = SEQID_WREAR * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800316 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_WREAR) |
Peng Fan3a344482015-01-04 17:07:14 +0800317 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
318 PAD1(LUT_PAD1) | INSTR1(LUT_WRITE));
319#endif
Alison Wangc7410e32014-05-06 09:13:01 +0800320 /* Lock the LUT */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800321 qspi_write32(priv->flags, &regs->lutkey, LUT_KEY_VALUE);
322 qspi_write32(priv->flags, &regs->lckcr, QSPI_LCKCR_LOCK);
Alison Wangc7410e32014-05-06 09:13:01 +0800323}
324
Peng Fan1c5f9662015-01-08 10:40:20 +0800325#if defined(CONFIG_SYS_FSL_QSPI_AHB)
326/*
327 * If we have changed the content of the flash by writing or erasing,
328 * we need to invalidate the AHB buffer. If we do not do so, we may read out
329 * the wrong data. The spec tells us reset the AHB domain and Serial Flash
330 * domain at the same time.
331 */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800332static inline void qspi_ahb_invalid(struct fsl_qspi_priv *priv)
Peng Fan1c5f9662015-01-08 10:40:20 +0800333{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800334 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan1c5f9662015-01-08 10:40:20 +0800335 u32 reg;
336
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800337 reg = qspi_read32(priv->flags, &regs->mcr);
Peng Fan1c5f9662015-01-08 10:40:20 +0800338 reg |= QSPI_MCR_SWRSTHD_MASK | QSPI_MCR_SWRSTSD_MASK;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800339 qspi_write32(priv->flags, &regs->mcr, reg);
Peng Fan1c5f9662015-01-08 10:40:20 +0800340
341 /*
342 * The minimum delay : 1 AHB + 2 SFCK clocks.
343 * Delay 1 us is enough.
344 */
345 udelay(1);
346
347 reg &= ~(QSPI_MCR_SWRSTHD_MASK | QSPI_MCR_SWRSTSD_MASK);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800348 qspi_write32(priv->flags, &regs->mcr, reg);
Peng Fan1c5f9662015-01-08 10:40:20 +0800349}
350
351/* Read out the data from the AHB buffer. */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800352static inline void qspi_ahb_read(struct fsl_qspi_priv *priv, u8 *rxbuf, int len)
Peng Fan1c5f9662015-01-08 10:40:20 +0800353{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800354 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan1c5f9662015-01-08 10:40:20 +0800355 u32 mcr_reg;
356
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800357 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
Peng Fan1c5f9662015-01-08 10:40:20 +0800358
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800359 qspi_write32(priv->flags, &regs->mcr,
360 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Peng Fan1c5f9662015-01-08 10:40:20 +0800361 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
362
363 /* Read out the data directly from the AHB buffer. */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800364 memcpy(rxbuf, (u8 *)(priv->cur_amba_base + priv->sf_addr), len);
Peng Fan1c5f9662015-01-08 10:40:20 +0800365
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800366 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Peng Fan1c5f9662015-01-08 10:40:20 +0800367}
368
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800369static void qspi_enable_ddr_mode(struct fsl_qspi_priv *priv)
Peng Fan1c5f9662015-01-08 10:40:20 +0800370{
371 u32 reg, reg2;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800372 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan1c5f9662015-01-08 10:40:20 +0800373
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800374 reg = qspi_read32(priv->flags, &regs->mcr);
Peng Fan1c5f9662015-01-08 10:40:20 +0800375 /* Disable the module */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800376 qspi_write32(priv->flags, &regs->mcr, reg | QSPI_MCR_MDIS_MASK);
Peng Fan1c5f9662015-01-08 10:40:20 +0800377
378 /* Set the Sampling Register for DDR */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800379 reg2 = qspi_read32(priv->flags, &regs->smpr);
Peng Fan1c5f9662015-01-08 10:40:20 +0800380 reg2 &= ~QSPI_SMPR_DDRSMP_MASK;
381 reg2 |= (2 << QSPI_SMPR_DDRSMP_SHIFT);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800382 qspi_write32(priv->flags, &regs->smpr, reg2);
Peng Fan1c5f9662015-01-08 10:40:20 +0800383
384 /* Enable the module again (enable the DDR too) */
385 reg |= QSPI_MCR_DDR_EN_MASK;
386 /* Enable bit 29 for imx6sx */
Jagan Tekic97ca922015-10-23 01:37:18 +0530387 reg |= BIT(29);
Peng Fan1c5f9662015-01-08 10:40:20 +0800388
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800389 qspi_write32(priv->flags, &regs->mcr, reg);
Peng Fan1c5f9662015-01-08 10:40:20 +0800390}
391
392/*
393 * There are two different ways to read out the data from the flash:
394 * the "IP Command Read" and the "AHB Command Read".
395 *
396 * The IC guy suggests we use the "AHB Command Read" which is faster
397 * then the "IP Command Read". (What's more is that there is a bug in
398 * the "IP Command Read" in the Vybrid.)
399 *
400 * After we set up the registers for the "AHB Command Read", we can use
401 * the memcpy to read the data directly. A "missed" access to the buffer
402 * causes the controller to clear the buffer, and use the sequence pointed
403 * by the QUADSPI_BFGENCR[SEQID] to initiate a read from the flash.
404 */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800405static void qspi_init_ahb_read(struct fsl_qspi_priv *priv)
Peng Fan1c5f9662015-01-08 10:40:20 +0800406{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800407 struct fsl_qspi_regs *regs = priv->regs;
408
Peng Fan1c5f9662015-01-08 10:40:20 +0800409 /* AHB configuration for access buffer 0/1/2 .*/
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800410 qspi_write32(priv->flags, &regs->buf0cr, QSPI_BUFXCR_INVALID_MSTRID);
411 qspi_write32(priv->flags, &regs->buf1cr, QSPI_BUFXCR_INVALID_MSTRID);
412 qspi_write32(priv->flags, &regs->buf2cr, QSPI_BUFXCR_INVALID_MSTRID);
413 qspi_write32(priv->flags, &regs->buf3cr, QSPI_BUF3CR_ALLMST_MASK |
Peng Fan1c5f9662015-01-08 10:40:20 +0800414 (0x80 << QSPI_BUF3CR_ADATSZ_SHIFT));
415
416 /* We only use the buffer3 */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800417 qspi_write32(priv->flags, &regs->buf0ind, 0);
418 qspi_write32(priv->flags, &regs->buf1ind, 0);
419 qspi_write32(priv->flags, &regs->buf2ind, 0);
Peng Fan1c5f9662015-01-08 10:40:20 +0800420
421 /*
422 * Set the default lut sequence for AHB Read.
423 * Parallel mode is disabled.
424 */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800425 qspi_write32(priv->flags, &regs->bfgencr,
Peng Fan1c5f9662015-01-08 10:40:20 +0800426 SEQID_FAST_READ << QSPI_BFGENCR_SEQID_SHIFT);
427
428 /*Enable DDR Mode*/
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800429 qspi_enable_ddr_mode(priv);
Peng Fan1c5f9662015-01-08 10:40:20 +0800430}
431#endif
432
Peng Fan3a344482015-01-04 17:07:14 +0800433#ifdef CONFIG_SPI_FLASH_BAR
434/* Bank register read/write, EAR register read/write */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800435static void qspi_op_rdbank(struct fsl_qspi_priv *priv, u8 *rxbuf, u32 len)
Peng Fan3a344482015-01-04 17:07:14 +0800436{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800437 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan3a344482015-01-04 17:07:14 +0800438 u32 reg, mcr_reg, data, seqid;
439
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800440 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
441 qspi_write32(priv->flags, &regs->mcr,
442 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Peng Fan3a344482015-01-04 17:07:14 +0800443 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800444 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Peng Fan3a344482015-01-04 17:07:14 +0800445
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800446 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
Peng Fan3a344482015-01-04 17:07:14 +0800447
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800448 if (priv->cur_seqid == QSPI_CMD_BRRD)
Peng Fan3a344482015-01-04 17:07:14 +0800449 seqid = SEQID_BRRD;
450 else
451 seqid = SEQID_RDEAR;
452
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800453 qspi_write32(priv->flags, &regs->ipcr,
454 (seqid << QSPI_IPCR_SEQID_SHIFT) | len);
Peng Fan3a344482015-01-04 17:07:14 +0800455
456 /* Wait previous command complete */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800457 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Peng Fan3a344482015-01-04 17:07:14 +0800458 ;
459
460 while (1) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800461 reg = qspi_read32(priv->flags, &regs->rbsr);
Peng Fan3a344482015-01-04 17:07:14 +0800462 if (reg & QSPI_RBSR_RDBFL_MASK) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800463 data = qspi_read32(priv->flags, &regs->rbdr[0]);
Peng Fan3a344482015-01-04 17:07:14 +0800464 data = qspi_endian_xchg(data);
465 memcpy(rxbuf, &data, len);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800466 qspi_write32(priv->flags, &regs->mcr,
467 qspi_read32(priv->flags, &regs->mcr) |
Peng Fan3a344482015-01-04 17:07:14 +0800468 QSPI_MCR_CLR_RXF_MASK);
469 break;
470 }
471 }
472
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800473 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Peng Fan3a344482015-01-04 17:07:14 +0800474}
475#endif
Alison Wangc7410e32014-05-06 09:13:01 +0800476
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800477static void qspi_op_rdid(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len)
Alison Wangc7410e32014-05-06 09:13:01 +0800478{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800479 struct fsl_qspi_regs *regs = priv->regs;
Alison Wangc7410e32014-05-06 09:13:01 +0800480 u32 mcr_reg, rbsr_reg, data;
481 int i, size;
482
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800483 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
484 qspi_write32(priv->flags, &regs->mcr,
485 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
486 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
487 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Alison Wangc7410e32014-05-06 09:13:01 +0800488
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800489 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
Alison Wangc7410e32014-05-06 09:13:01 +0800490
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800491 qspi_write32(priv->flags, &regs->ipcr,
492 (SEQID_RDID << QSPI_IPCR_SEQID_SHIFT) | 0);
493 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800494 ;
495
496 i = 0;
497 size = len;
498 while ((RX_BUFFER_SIZE >= size) && (size > 0)) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800499 rbsr_reg = qspi_read32(priv->flags, &regs->rbsr);
Alison Wangc7410e32014-05-06 09:13:01 +0800500 if (rbsr_reg & QSPI_RBSR_RDBFL_MASK) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800501 data = qspi_read32(priv->flags, &regs->rbdr[i]);
Alison Wangc7410e32014-05-06 09:13:01 +0800502 data = qspi_endian_xchg(data);
503 memcpy(rxbuf, &data, 4);
504 rxbuf++;
505 size -= 4;
506 i++;
507 }
508 }
509
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800510 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Alison Wangc7410e32014-05-06 09:13:01 +0800511}
512
Peng Fan1c5f9662015-01-08 10:40:20 +0800513#ifndef CONFIG_SYS_FSL_QSPI_AHB
514/* If not use AHB read, read data from ip interface */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800515static void qspi_op_read(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len)
Alison Wangc7410e32014-05-06 09:13:01 +0800516{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800517 struct fsl_qspi_regs *regs = priv->regs;
Alison Wangc7410e32014-05-06 09:13:01 +0800518 u32 mcr_reg, data;
519 int i, size;
520 u32 to_or_from;
521
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800522 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
523 qspi_write32(priv->flags, &regs->mcr,
524 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
525 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
526 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Alison Wangc7410e32014-05-06 09:13:01 +0800527
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800528 to_or_from = priv->sf_addr + priv->cur_amba_base;
Alison Wangc7410e32014-05-06 09:13:01 +0800529
530 while (len > 0) {
Alexander Stein9b2c8a92015-11-04 09:19:10 +0100531 WATCHDOG_RESET();
532
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800533 qspi_write32(priv->flags, &regs->sfar, to_or_from);
Alison Wangc7410e32014-05-06 09:13:01 +0800534
535 size = (len > RX_BUFFER_SIZE) ?
536 RX_BUFFER_SIZE : len;
537
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800538 qspi_write32(priv->flags, &regs->ipcr,
539 (SEQID_FAST_READ << QSPI_IPCR_SEQID_SHIFT) |
540 size);
541 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800542 ;
543
544 to_or_from += size;
545 len -= size;
546
547 i = 0;
548 while ((RX_BUFFER_SIZE >= size) && (size > 0)) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800549 data = qspi_read32(priv->flags, &regs->rbdr[i]);
Alison Wangc7410e32014-05-06 09:13:01 +0800550 data = qspi_endian_xchg(data);
551 memcpy(rxbuf, &data, 4);
552 rxbuf++;
553 size -= 4;
554 i++;
555 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800556 qspi_write32(priv->flags, &regs->mcr,
557 qspi_read32(priv->flags, &regs->mcr) |
558 QSPI_MCR_CLR_RXF_MASK);
Alison Wangc7410e32014-05-06 09:13:01 +0800559 }
560
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800561 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Alison Wangc7410e32014-05-06 09:13:01 +0800562}
Peng Fan1c5f9662015-01-08 10:40:20 +0800563#endif
Alison Wangc7410e32014-05-06 09:13:01 +0800564
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800565static void qspi_op_write(struct fsl_qspi_priv *priv, u8 *txbuf, u32 len)
Alison Wangc7410e32014-05-06 09:13:01 +0800566{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800567 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan3a344482015-01-04 17:07:14 +0800568 u32 mcr_reg, data, reg, status_reg, seqid;
Alison Wangc7410e32014-05-06 09:13:01 +0800569 int i, size, tx_size;
570 u32 to_or_from = 0;
571
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800572 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
573 qspi_write32(priv->flags, &regs->mcr,
574 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
575 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
576 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Alison Wangc7410e32014-05-06 09:13:01 +0800577
578 status_reg = 0;
579 while ((status_reg & FLASH_STATUS_WEL) != FLASH_STATUS_WEL) {
Alexander Stein9b2c8a92015-11-04 09:19:10 +0100580 WATCHDOG_RESET();
581
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800582 qspi_write32(priv->flags, &regs->ipcr,
583 (SEQID_WREN << QSPI_IPCR_SEQID_SHIFT) | 0);
584 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800585 ;
586
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800587 qspi_write32(priv->flags, &regs->ipcr,
588 (SEQID_RDSR << QSPI_IPCR_SEQID_SHIFT) | 1);
589 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800590 ;
591
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800592 reg = qspi_read32(priv->flags, &regs->rbsr);
Alison Wangc7410e32014-05-06 09:13:01 +0800593 if (reg & QSPI_RBSR_RDBFL_MASK) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800594 status_reg = qspi_read32(priv->flags, &regs->rbdr[0]);
Alison Wangc7410e32014-05-06 09:13:01 +0800595 status_reg = qspi_endian_xchg(status_reg);
596 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800597 qspi_write32(priv->flags, &regs->mcr,
598 qspi_read32(priv->flags, &regs->mcr) |
599 QSPI_MCR_CLR_RXF_MASK);
Alison Wangc7410e32014-05-06 09:13:01 +0800600 }
601
Peng Fan3a344482015-01-04 17:07:14 +0800602 /* Default is page programming */
603 seqid = SEQID_PP;
604#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800605 if (priv->cur_seqid == QSPI_CMD_BRWR)
Peng Fan3a344482015-01-04 17:07:14 +0800606 seqid = SEQID_BRWR;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800607 else if (priv->cur_seqid == QSPI_CMD_WREAR)
Peng Fan3a344482015-01-04 17:07:14 +0800608 seqid = SEQID_WREAR;
609#endif
610
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800611 to_or_from = priv->sf_addr + priv->cur_amba_base;
Peng Fan3a344482015-01-04 17:07:14 +0800612
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800613 qspi_write32(priv->flags, &regs->sfar, to_or_from);
Alison Wangc7410e32014-05-06 09:13:01 +0800614
615 tx_size = (len > TX_BUFFER_SIZE) ?
616 TX_BUFFER_SIZE : len;
617
Peng Fan3a344482015-01-04 17:07:14 +0800618 size = tx_size / 4;
Alison Wangc7410e32014-05-06 09:13:01 +0800619 for (i = 0; i < size; i++) {
Peng Fan3a344482015-01-04 17:07:14 +0800620 memcpy(&data, txbuf, 4);
621 data = qspi_endian_xchg(data);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800622 qspi_write32(priv->flags, &regs->tbdr, data);
Peng Fan3a344482015-01-04 17:07:14 +0800623 txbuf += 4;
Alison Wangc7410e32014-05-06 09:13:01 +0800624 }
625
Peng Fan3a344482015-01-04 17:07:14 +0800626 size = tx_size % 4;
627 if (size) {
628 data = 0;
629 memcpy(&data, txbuf, size);
630 data = qspi_endian_xchg(data);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800631 qspi_write32(priv->flags, &regs->tbdr, data);
Peng Fan3a344482015-01-04 17:07:14 +0800632 }
633
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800634 qspi_write32(priv->flags, &regs->ipcr,
635 (seqid << QSPI_IPCR_SEQID_SHIFT) | tx_size);
636 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800637 ;
638
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800639 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Alison Wangc7410e32014-05-06 09:13:01 +0800640}
641
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800642static void qspi_op_rdsr(struct fsl_qspi_priv *priv, u32 *rxbuf)
Alison Wangc7410e32014-05-06 09:13:01 +0800643{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800644 struct fsl_qspi_regs *regs = priv->regs;
Alison Wangc7410e32014-05-06 09:13:01 +0800645 u32 mcr_reg, reg, data;
646
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800647 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
648 qspi_write32(priv->flags, &regs->mcr,
649 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
650 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
651 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Alison Wangc7410e32014-05-06 09:13:01 +0800652
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800653 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
Alison Wangc7410e32014-05-06 09:13:01 +0800654
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800655 qspi_write32(priv->flags, &regs->ipcr,
656 (SEQID_RDSR << QSPI_IPCR_SEQID_SHIFT) | 0);
657 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800658 ;
659
660 while (1) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800661 reg = qspi_read32(priv->flags, &regs->rbsr);
Alison Wangc7410e32014-05-06 09:13:01 +0800662 if (reg & QSPI_RBSR_RDBFL_MASK) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800663 data = qspi_read32(priv->flags, &regs->rbdr[0]);
Alison Wangc7410e32014-05-06 09:13:01 +0800664 data = qspi_endian_xchg(data);
665 memcpy(rxbuf, &data, 4);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800666 qspi_write32(priv->flags, &regs->mcr,
667 qspi_read32(priv->flags, &regs->mcr) |
668 QSPI_MCR_CLR_RXF_MASK);
Alison Wangc7410e32014-05-06 09:13:01 +0800669 break;
670 }
671 }
672
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800673 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Alison Wangc7410e32014-05-06 09:13:01 +0800674}
675
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800676static void qspi_op_erase(struct fsl_qspi_priv *priv)
Alison Wangc7410e32014-05-06 09:13:01 +0800677{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800678 struct fsl_qspi_regs *regs = priv->regs;
Alison Wangc7410e32014-05-06 09:13:01 +0800679 u32 mcr_reg;
680 u32 to_or_from = 0;
681
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800682 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
683 qspi_write32(priv->flags, &regs->mcr,
684 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
685 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
686 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Alison Wangc7410e32014-05-06 09:13:01 +0800687
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800688 to_or_from = priv->sf_addr + priv->cur_amba_base;
689 qspi_write32(priv->flags, &regs->sfar, to_or_from);
Alison Wangc7410e32014-05-06 09:13:01 +0800690
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800691 qspi_write32(priv->flags, &regs->ipcr,
692 (SEQID_WREN << QSPI_IPCR_SEQID_SHIFT) | 0);
693 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800694 ;
695
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800696 if (priv->cur_seqid == QSPI_CMD_SE) {
697 qspi_write32(priv->flags, &regs->ipcr,
Peng Fan3642a872014-12-31 11:01:39 +0800698 (SEQID_SE << QSPI_IPCR_SEQID_SHIFT) | 0);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800699 } else if (priv->cur_seqid == QSPI_CMD_BE_4K) {
700 qspi_write32(priv->flags, &regs->ipcr,
Peng Fan3642a872014-12-31 11:01:39 +0800701 (SEQID_BE_4K << QSPI_IPCR_SEQID_SHIFT) | 0);
702 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800703 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800704 ;
705
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800706 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Alison Wangc7410e32014-05-06 09:13:01 +0800707}
708
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800709int qspi_xfer(struct fsl_qspi_priv *priv, unsigned int bitlen,
Alison Wangc7410e32014-05-06 09:13:01 +0800710 const void *dout, void *din, unsigned long flags)
711{
Alison Wangc7410e32014-05-06 09:13:01 +0800712 u32 bytes = DIV_ROUND_UP(bitlen, 8);
Peng Fan3a344482015-01-04 17:07:14 +0800713 static u32 wr_sfaddr;
Alison Wangc7410e32014-05-06 09:13:01 +0800714 u32 txbuf;
715
716 if (dout) {
Peng Fan3a344482015-01-04 17:07:14 +0800717 if (flags & SPI_XFER_BEGIN) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800718 priv->cur_seqid = *(u8 *)dout;
Peng Fan3a344482015-01-04 17:07:14 +0800719 memcpy(&txbuf, dout, 4);
720 }
Alison Wangc7410e32014-05-06 09:13:01 +0800721
722 if (flags == SPI_XFER_END) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800723 priv->sf_addr = wr_sfaddr;
724 qspi_op_write(priv, (u8 *)dout, bytes);
Alison Wangc7410e32014-05-06 09:13:01 +0800725 return 0;
726 }
727
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800728 if (priv->cur_seqid == QSPI_CMD_FAST_READ) {
729 priv->sf_addr = swab32(txbuf) & OFFSET_BITS_MASK;
730 } else if ((priv->cur_seqid == QSPI_CMD_SE) ||
731 (priv->cur_seqid == QSPI_CMD_BE_4K)) {
732 priv->sf_addr = swab32(txbuf) & OFFSET_BITS_MASK;
733 qspi_op_erase(priv);
734 } else if (priv->cur_seqid == QSPI_CMD_PP) {
Peng Fan3a344482015-01-04 17:07:14 +0800735 wr_sfaddr = swab32(txbuf) & OFFSET_BITS_MASK;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800736 } else if ((priv->cur_seqid == QSPI_CMD_BRWR) ||
737 (priv->cur_seqid == QSPI_CMD_WREAR)) {
Peng Fan3a344482015-01-04 17:07:14 +0800738#ifdef CONFIG_SPI_FLASH_BAR
Peng Fan3a344482015-01-04 17:07:14 +0800739 wr_sfaddr = 0;
Peng Fan3a344482015-01-04 17:07:14 +0800740#endif
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800741 }
Alison Wangc7410e32014-05-06 09:13:01 +0800742 }
743
744 if (din) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800745 if (priv->cur_seqid == QSPI_CMD_FAST_READ) {
Peng Fan1c5f9662015-01-08 10:40:20 +0800746#ifdef CONFIG_SYS_FSL_QSPI_AHB
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800747 qspi_ahb_read(priv, din, bytes);
Peng Fan1c5f9662015-01-08 10:40:20 +0800748#else
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800749 qspi_op_read(priv, din, bytes);
Peng Fan1c5f9662015-01-08 10:40:20 +0800750#endif
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800751 } else if (priv->cur_seqid == QSPI_CMD_RDID)
752 qspi_op_rdid(priv, din, bytes);
753 else if (priv->cur_seqid == QSPI_CMD_RDSR)
754 qspi_op_rdsr(priv, din);
Peng Fan3a344482015-01-04 17:07:14 +0800755#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800756 else if ((priv->cur_seqid == QSPI_CMD_BRRD) ||
757 (priv->cur_seqid == QSPI_CMD_RDEAR)) {
758 priv->sf_addr = 0;
759 qspi_op_rdbank(priv, din, bytes);
Peng Fan3a344482015-01-04 17:07:14 +0800760 }
761#endif
Alison Wangc7410e32014-05-06 09:13:01 +0800762 }
763
Peng Fan1c5f9662015-01-08 10:40:20 +0800764#ifdef CONFIG_SYS_FSL_QSPI_AHB
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800765 if ((priv->cur_seqid == QSPI_CMD_SE) ||
766 (priv->cur_seqid == QSPI_CMD_PP) ||
767 (priv->cur_seqid == QSPI_CMD_BE_4K) ||
768 (priv->cur_seqid == QSPI_CMD_WREAR) ||
769 (priv->cur_seqid == QSPI_CMD_BRWR))
770 qspi_ahb_invalid(priv);
771#endif
772
773 return 0;
774}
775
776void qspi_module_disable(struct fsl_qspi_priv *priv, u8 disable)
777{
778 u32 mcr_val;
779
780 mcr_val = qspi_read32(priv->flags, &priv->regs->mcr);
781 if (disable)
782 mcr_val |= QSPI_MCR_MDIS_MASK;
783 else
784 mcr_val &= ~QSPI_MCR_MDIS_MASK;
785 qspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
786}
787
788void qspi_cfg_smpr(struct fsl_qspi_priv *priv, u32 clear_bits, u32 set_bits)
789{
790 u32 smpr_val;
791
792 smpr_val = qspi_read32(priv->flags, &priv->regs->smpr);
793 smpr_val &= ~clear_bits;
794 smpr_val |= set_bits;
795 qspi_write32(priv->flags, &priv->regs->smpr, smpr_val);
796}
797#ifndef CONFIG_DM_SPI
798static unsigned long spi_bases[] = {
799 QSPI0_BASE_ADDR,
800#ifdef CONFIG_MX6SX
801 QSPI1_BASE_ADDR,
802#endif
803};
804
805static unsigned long amba_bases[] = {
806 QSPI0_AMBA_BASE,
807#ifdef CONFIG_MX6SX
808 QSPI1_AMBA_BASE,
809#endif
810};
811
812static inline struct fsl_qspi *to_qspi_spi(struct spi_slave *slave)
813{
814 return container_of(slave, struct fsl_qspi, slave);
815}
816
817struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
818 unsigned int max_hz, unsigned int mode)
819{
820 struct fsl_qspi *qspi;
821 struct fsl_qspi_regs *regs;
822 u32 total_size;
823
824 if (bus >= ARRAY_SIZE(spi_bases))
825 return NULL;
826
827 if (cs >= FSL_QSPI_FLASH_NUM)
828 return NULL;
829
830 qspi = spi_alloc_slave(struct fsl_qspi, bus, cs);
831 if (!qspi)
832 return NULL;
833
834#ifdef CONFIG_SYS_FSL_QSPI_BE
835 qspi->priv.flags |= QSPI_FLAG_REGMAP_ENDIAN_BIG;
836#endif
837
838 regs = (struct fsl_qspi_regs *)spi_bases[bus];
839 qspi->priv.regs = regs;
840 /*
841 * According cs, use different amba_base to choose the
842 * corresponding flash devices.
843 *
844 * If not, only one flash device is used even if passing
845 * different cs using `sf probe`
846 */
847 qspi->priv.cur_amba_base = amba_bases[bus] + cs * FSL_QSPI_FLASH_SIZE;
848
849 qspi->slave.max_write_size = TX_BUFFER_SIZE;
850
851 qspi_write32(qspi->priv.flags, &regs->mcr,
852 QSPI_MCR_RESERVED_MASK | QSPI_MCR_MDIS_MASK);
853
854 qspi_cfg_smpr(&qspi->priv,
855 ~(QSPI_SMPR_FSDLY_MASK | QSPI_SMPR_DDRSMP_MASK |
856 QSPI_SMPR_FSPHS_MASK | QSPI_SMPR_HSENA_MASK), 0);
857
858 total_size = FSL_QSPI_FLASH_SIZE * FSL_QSPI_FLASH_NUM;
859 /*
860 * Any read access to non-implemented addresses will provide
861 * undefined results.
862 *
863 * In case single die flash devices, TOP_ADDR_MEMA2 and
864 * TOP_ADDR_MEMB2 should be initialized/programmed to
865 * TOP_ADDR_MEMA1 and TOP_ADDR_MEMB1 respectively - in effect,
866 * setting the size of these devices to 0. This would ensure
867 * that the complete memory map is assigned to only one flash device.
868 */
869 qspi_write32(qspi->priv.flags, &regs->sfa1ad,
870 FSL_QSPI_FLASH_SIZE | amba_bases[bus]);
871 qspi_write32(qspi->priv.flags, &regs->sfa2ad,
872 FSL_QSPI_FLASH_SIZE | amba_bases[bus]);
873 qspi_write32(qspi->priv.flags, &regs->sfb1ad,
874 total_size | amba_bases[bus]);
875 qspi_write32(qspi->priv.flags, &regs->sfb2ad,
876 total_size | amba_bases[bus]);
877
878 qspi_set_lut(&qspi->priv);
879
880#ifdef CONFIG_SYS_FSL_QSPI_AHB
881 qspi_init_ahb_read(&qspi->priv);
Peng Fan1c5f9662015-01-08 10:40:20 +0800882#endif
883
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800884 qspi_module_disable(&qspi->priv, 0);
885
886 return &qspi->slave;
887}
888
889void spi_free_slave(struct spi_slave *slave)
890{
891 struct fsl_qspi *qspi = to_qspi_spi(slave);
892
893 free(qspi);
894}
895
896int spi_claim_bus(struct spi_slave *slave)
897{
Alison Wangc7410e32014-05-06 09:13:01 +0800898 return 0;
899}
900
901void spi_release_bus(struct spi_slave *slave)
902{
903 /* Nothing to do */
904}
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800905
906int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
907 const void *dout, void *din, unsigned long flags)
908{
909 struct fsl_qspi *qspi = to_qspi_spi(slave);
910
911 return qspi_xfer(&qspi->priv, bitlen, dout, din, flags);
912}
913
914void spi_init(void)
915{
916 /* Nothing to do */
917}
918#else
919static int fsl_qspi_child_pre_probe(struct udevice *dev)
920{
Simon Glassde44acf2015-09-28 23:32:01 -0600921 struct spi_slave *slave = dev_get_parent_priv(dev);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800922
923 slave->max_write_size = TX_BUFFER_SIZE;
924
925 return 0;
926}
927
928static int fsl_qspi_probe(struct udevice *bus)
929{
930 u32 total_size;
931 struct fsl_qspi_platdata *plat = dev_get_platdata(bus);
932 struct fsl_qspi_priv *priv = dev_get_priv(bus);
933 struct dm_spi_bus *dm_spi_bus;
934
935 dm_spi_bus = bus->uclass_priv;
936
937 dm_spi_bus->max_hz = plat->speed_hz;
938
939 priv->regs = (struct fsl_qspi_regs *)plat->reg_base;
940 priv->flags = plat->flags;
941
942 priv->speed_hz = plat->speed_hz;
943 priv->amba_base[0] = plat->amba_base;
944 priv->amba_total_size = plat->amba_total_size;
945 priv->flash_num = plat->flash_num;
946 priv->num_chipselect = plat->num_chipselect;
947
948 qspi_write32(priv->flags, &priv->regs->mcr,
949 QSPI_MCR_RESERVED_MASK | QSPI_MCR_MDIS_MASK);
950
951 qspi_cfg_smpr(priv, ~(QSPI_SMPR_FSDLY_MASK | QSPI_SMPR_DDRSMP_MASK |
952 QSPI_SMPR_FSPHS_MASK | QSPI_SMPR_HSENA_MASK), 0);
953
954 total_size = FSL_QSPI_FLASH_SIZE * FSL_QSPI_FLASH_NUM;
955 /*
956 * Any read access to non-implemented addresses will provide
957 * undefined results.
958 *
959 * In case single die flash devices, TOP_ADDR_MEMA2 and
960 * TOP_ADDR_MEMB2 should be initialized/programmed to
961 * TOP_ADDR_MEMA1 and TOP_ADDR_MEMB1 respectively - in effect,
962 * setting the size of these devices to 0. This would ensure
963 * that the complete memory map is assigned to only one flash device.
964 */
965 qspi_write32(priv->flags, &priv->regs->sfa1ad,
966 FSL_QSPI_FLASH_SIZE | priv->amba_base[0]);
967 qspi_write32(priv->flags, &priv->regs->sfa2ad,
968 FSL_QSPI_FLASH_SIZE | priv->amba_base[0]);
969 qspi_write32(priv->flags, &priv->regs->sfb1ad,
970 total_size | priv->amba_base[0]);
971 qspi_write32(priv->flags, &priv->regs->sfb2ad,
972 total_size | priv->amba_base[0]);
973
974 qspi_set_lut(priv);
975
976#ifdef CONFIG_SYS_FSL_QSPI_AHB
977 qspi_init_ahb_read(priv);
978#endif
979
980 qspi_module_disable(priv, 0);
981
982 return 0;
983}
984
985static int fsl_qspi_ofdata_to_platdata(struct udevice *bus)
986{
987 struct reg_data {
988 u32 addr;
989 u32 size;
990 } regs_data[2];
991 struct fsl_qspi_platdata *plat = bus->platdata;
992 const void *blob = gd->fdt_blob;
993 int node = bus->of_offset;
994 int ret, flash_num = 0, subnode;
995
996 if (fdtdec_get_bool(blob, node, "big-endian"))
997 plat->flags |= QSPI_FLAG_REGMAP_ENDIAN_BIG;
998
999 ret = fdtdec_get_int_array(blob, node, "reg", (u32 *)regs_data,
1000 sizeof(regs_data)/sizeof(u32));
1001 if (ret) {
1002 debug("Error: can't get base addresses (ret = %d)!\n", ret);
1003 return -ENOMEM;
1004 }
1005
1006 /* Count flash numbers */
1007 fdt_for_each_subnode(blob, subnode, node)
1008 ++flash_num;
1009
1010 if (flash_num == 0) {
1011 debug("Error: Missing flashes!\n");
1012 return -ENODEV;
1013 }
1014
1015 plat->speed_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
1016 FSL_QSPI_DEFAULT_SCK_FREQ);
1017 plat->num_chipselect = fdtdec_get_int(blob, node, "num-cs",
1018 FSL_QSPI_MAX_CHIPSELECT_NUM);
1019
1020 plat->reg_base = regs_data[0].addr;
1021 plat->amba_base = regs_data[1].addr;
1022 plat->amba_total_size = regs_data[1].size;
1023 plat->flash_num = flash_num;
1024
1025 debug("%s: regs=<0x%x> <0x%x, 0x%x>, max-frequency=%d, endianess=%s\n",
1026 __func__,
1027 plat->reg_base,
1028 plat->amba_base,
1029 plat->amba_total_size,
1030 plat->speed_hz,
1031 plat->flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ? "be" : "le"
1032 );
1033
1034 return 0;
1035}
1036
1037static int fsl_qspi_xfer(struct udevice *dev, unsigned int bitlen,
1038 const void *dout, void *din, unsigned long flags)
1039{
1040 struct fsl_qspi_priv *priv;
1041 struct udevice *bus;
1042
1043 bus = dev->parent;
1044 priv = dev_get_priv(bus);
1045
1046 return qspi_xfer(priv, bitlen, dout, din, flags);
1047}
1048
1049static int fsl_qspi_claim_bus(struct udevice *dev)
1050{
1051 struct fsl_qspi_priv *priv;
1052 struct udevice *bus;
1053 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
1054
1055 bus = dev->parent;
1056 priv = dev_get_priv(bus);
1057
1058 priv->cur_amba_base =
1059 priv->amba_base[0] + FSL_QSPI_FLASH_SIZE * slave_plat->cs;
1060
1061 qspi_module_disable(priv, 0);
1062
1063 return 0;
1064}
1065
1066static int fsl_qspi_release_bus(struct udevice *dev)
1067{
1068 struct fsl_qspi_priv *priv;
1069 struct udevice *bus;
1070
1071 bus = dev->parent;
1072 priv = dev_get_priv(bus);
1073
1074 qspi_module_disable(priv, 1);
1075
1076 return 0;
1077}
1078
1079static int fsl_qspi_set_speed(struct udevice *bus, uint speed)
1080{
1081 /* Nothing to do */
1082 return 0;
1083}
1084
1085static int fsl_qspi_set_mode(struct udevice *bus, uint mode)
1086{
1087 /* Nothing to do */
1088 return 0;
1089}
1090
1091static const struct dm_spi_ops fsl_qspi_ops = {
1092 .claim_bus = fsl_qspi_claim_bus,
1093 .release_bus = fsl_qspi_release_bus,
1094 .xfer = fsl_qspi_xfer,
1095 .set_speed = fsl_qspi_set_speed,
1096 .set_mode = fsl_qspi_set_mode,
1097};
1098
1099static const struct udevice_id fsl_qspi_ids[] = {
1100 { .compatible = "fsl,vf610-qspi" },
1101 { .compatible = "fsl,imx6sx-qspi" },
1102 { }
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};
1116#endif