blob: b2a058380f69ea271602af6815d9cf0bba5441da [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
Yuan Yaod7193262016-03-15 14:36:42 +080047#define SEQID_WRAR 13
48#define SEQID_RDAR 14
Alison Wangc7410e32014-05-06 09:13:01 +080049
Peng Fanff162342014-12-31 11:01:36 +080050/* QSPI CMD */
51#define QSPI_CMD_PP 0x02 /* Page program (up to 256 bytes) */
52#define QSPI_CMD_RDSR 0x05 /* Read status register */
53#define QSPI_CMD_WREN 0x06 /* Write enable */
54#define QSPI_CMD_FAST_READ 0x0b /* Read data bytes (high frequency) */
Peng Fan3642a872014-12-31 11:01:39 +080055#define QSPI_CMD_BE_4K 0x20 /* 4K erase */
Peng Fanff162342014-12-31 11:01:36 +080056#define QSPI_CMD_CHIP_ERASE 0xc7 /* Erase whole flash chip */
57#define QSPI_CMD_SE 0xd8 /* Sector erase (usually 64KiB) */
58#define QSPI_CMD_RDID 0x9f /* Read JEDEC ID */
Alison Wangc7410e32014-05-06 09:13:01 +080059
Peng Fan3a344482015-01-04 17:07:14 +080060/* Used for Micron, winbond and Macronix flashes */
61#define QSPI_CMD_WREAR 0xc5 /* EAR register write */
62#define QSPI_CMD_RDEAR 0xc8 /* EAR reigster read */
63
64/* Used for Spansion flashes only. */
65#define QSPI_CMD_BRRD 0x16 /* Bank register read */
66#define QSPI_CMD_BRWR 0x17 /* Bank register write */
67
Yuan Yaod7193262016-03-15 14:36:42 +080068/* Used for Spansion S25FS-S family flash only. */
69#define QSPI_CMD_RDAR 0x65 /* Read any device register */
70#define QSPI_CMD_WRAR 0x71 /* Write any device register */
71
Peng Fanff162342014-12-31 11:01:36 +080072/* 4-byte address QSPI CMD - used on Spansion and some Macronix flashes */
73#define QSPI_CMD_FAST_READ_4B 0x0c /* Read data bytes (high frequency) */
74#define QSPI_CMD_PP_4B 0x12 /* Page program (up to 256 bytes) */
75#define QSPI_CMD_SE_4B 0xdc /* Sector erase (usually 64KiB) */
Alison Wangc7410e32014-05-06 09:13:01 +080076
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080077/* fsl_qspi_platdata flags */
Jagan Tekic97ca922015-10-23 01:37:18 +053078#define QSPI_FLAG_REGMAP_ENDIAN_BIG BIT(0)
Alison Wangc7410e32014-05-06 09:13:01 +080079
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080080/* default SCK frequency, unit: HZ */
81#define FSL_QSPI_DEFAULT_SCK_FREQ 50000000
Alison Wangc7410e32014-05-06 09:13:01 +080082
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080083/* QSPI max chipselect signals number */
84#define FSL_QSPI_MAX_CHIPSELECT_NUM 4
85
86#ifdef CONFIG_DM_SPI
87/**
88 * struct fsl_qspi_platdata - platform data for Freescale QSPI
89 *
90 * @flags: Flags for QSPI QSPI_FLAG_...
91 * @speed_hz: Default SCK frequency
92 * @reg_base: Base address of QSPI registers
93 * @amba_base: Base address of QSPI memory mapping
94 * @amba_total_size: size of QSPI memory mapping
95 * @flash_num: Number of active slave devices
96 * @num_chipselect: Number of QSPI chipselect signals
97 */
98struct fsl_qspi_platdata {
99 u32 flags;
100 u32 speed_hz;
Yuan Yaoae412392016-03-15 14:36:40 +0800101 fdt_addr_t reg_base;
102 fdt_addr_t amba_base;
103 fdt_size_t amba_total_size;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800104 u32 flash_num;
105 u32 num_chipselect;
106};
Peng Fan828e4682014-12-31 11:01:38 +0800107#endif
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800108
109/**
110 * struct fsl_qspi_priv - private data for Freescale QSPI
111 *
112 * @flags: Flags for QSPI QSPI_FLAG_...
113 * @bus_clk: QSPI input clk frequency
114 * @speed_hz: Default SCK frequency
115 * @cur_seqid: current LUT table sequence id
116 * @sf_addr: flash access offset
117 * @amba_base: Base address of QSPI memory mapping of every CS
118 * @amba_total_size: size of QSPI memory mapping
119 * @cur_amba_base: Base address of QSPI memory mapping of current CS
120 * @flash_num: Number of active slave devices
121 * @num_chipselect: Number of QSPI chipselect signals
122 * @regs: Point to QSPI register structure for I/O access
123 */
124struct fsl_qspi_priv {
125 u32 flags;
126 u32 bus_clk;
127 u32 speed_hz;
128 u32 cur_seqid;
129 u32 sf_addr;
130 u32 amba_base[FSL_QSPI_MAX_CHIPSELECT_NUM];
131 u32 amba_total_size;
132 u32 cur_amba_base;
133 u32 flash_num;
134 u32 num_chipselect;
135 struct fsl_qspi_regs *regs;
Alison Wangc7410e32014-05-06 09:13:01 +0800136};
137
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800138#ifndef CONFIG_DM_SPI
Alison Wangc7410e32014-05-06 09:13:01 +0800139struct fsl_qspi {
140 struct spi_slave slave;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800141 struct fsl_qspi_priv priv;
Alison Wangc7410e32014-05-06 09:13:01 +0800142};
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800143#endif
144
145static u32 qspi_read32(u32 flags, u32 *addr)
146{
147 return flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ?
148 in_be32(addr) : in_le32(addr);
149}
150
151static void qspi_write32(u32 flags, u32 *addr, u32 val)
152{
153 flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ?
154 out_be32(addr, val) : out_le32(addr, val);
155}
Alison Wangc7410e32014-05-06 09:13:01 +0800156
157/* QSPI support swapping the flash read/write data
158 * in hardware for LS102xA, but not for VF610 */
159static inline u32 qspi_endian_xchg(u32 data)
160{
161#ifdef CONFIG_VF610
162 return swab32(data);
163#else
164 return data;
165#endif
166}
167
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800168static void qspi_set_lut(struct fsl_qspi_priv *priv)
Alison Wangc7410e32014-05-06 09:13:01 +0800169{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800170 struct fsl_qspi_regs *regs = priv->regs;
Alison Wangc7410e32014-05-06 09:13:01 +0800171 u32 lut_base;
172
173 /* Unlock the LUT */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800174 qspi_write32(priv->flags, &regs->lutkey, LUT_KEY_VALUE);
175 qspi_write32(priv->flags, &regs->lckcr, QSPI_LCKCR_UNLOCK);
Alison Wangc7410e32014-05-06 09:13:01 +0800176
177 /* Write Enable */
178 lut_base = SEQID_WREN * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800179 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_WREN) |
Alison Wangc7410e32014-05-06 09:13:01 +0800180 PAD0(LUT_PAD1) | INSTR0(LUT_CMD));
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800181 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
182 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
183 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800184
185 /* Fast Read */
186 lut_base = SEQID_FAST_READ * 4;
Peng Fan3a344482015-01-04 17:07:14 +0800187#ifdef CONFIG_SPI_FLASH_BAR
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) |
Peng Fan3a344482015-01-04 17:07:14 +0800191 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
192#else
Alison Wangc7410e32014-05-06 09:13:01 +0800193 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800194 qspi_write32(priv->flags, &regs->lut[lut_base],
195 OPRND0(QSPI_CMD_FAST_READ) | PAD0(LUT_PAD1) |
196 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
197 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Alison Wangc7410e32014-05-06 09:13:01 +0800198 else
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800199 qspi_write32(priv->flags, &regs->lut[lut_base],
Peng Fanff162342014-12-31 11:01:36 +0800200 OPRND0(QSPI_CMD_FAST_READ_4B) |
201 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) |
202 OPRND1(ADDR32BIT) | PAD1(LUT_PAD1) |
203 INSTR1(LUT_ADDR));
Peng Fan3a344482015-01-04 17:07:14 +0800204#endif
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800205 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
206 OPRND0(8) | PAD0(LUT_PAD1) | INSTR0(LUT_DUMMY) |
207 OPRND1(RX_BUFFER_SIZE) | PAD1(LUT_PAD1) |
208 INSTR1(LUT_READ));
209 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
210 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800211
212 /* Read Status */
213 lut_base = SEQID_RDSR * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800214 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDSR) |
Alison Wangc7410e32014-05-06 09:13:01 +0800215 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
216 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800217 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
218 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
219 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800220
221 /* Erase a sector */
222 lut_base = SEQID_SE * 4;
Peng Fan3a344482015-01-04 17:07:14 +0800223#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800224 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_SE) |
Peng Fan3a344482015-01-04 17:07:14 +0800225 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
226 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
227#else
Alison Wangc7410e32014-05-06 09:13:01 +0800228 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800229 qspi_write32(priv->flags, &regs->lut[lut_base],
230 OPRND0(QSPI_CMD_SE) | PAD0(LUT_PAD1) |
231 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
232 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Alison Wangc7410e32014-05-06 09:13:01 +0800233 else
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800234 qspi_write32(priv->flags, &regs->lut[lut_base],
235 OPRND0(QSPI_CMD_SE_4B) | PAD0(LUT_PAD1) |
236 INSTR0(LUT_CMD) | OPRND1(ADDR32BIT) |
237 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Peng Fan3a344482015-01-04 17:07:14 +0800238#endif
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800239 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
240 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
241 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800242
243 /* Erase the whole chip */
244 lut_base = SEQID_CHIP_ERASE * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800245 qspi_write32(priv->flags, &regs->lut[lut_base],
246 OPRND0(QSPI_CMD_CHIP_ERASE) |
247 PAD0(LUT_PAD1) | INSTR0(LUT_CMD));
248 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
249 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
250 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800251
252 /* Page Program */
253 lut_base = SEQID_PP * 4;
Peng Fan3a344482015-01-04 17:07:14 +0800254#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800255 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_PP) |
Peng Fan3a344482015-01-04 17:07:14 +0800256 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
257 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
258#else
Alison Wangc7410e32014-05-06 09:13:01 +0800259 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800260 qspi_write32(priv->flags, &regs->lut[lut_base],
261 OPRND0(QSPI_CMD_PP) | PAD0(LUT_PAD1) |
262 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
263 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Alison Wangc7410e32014-05-06 09:13:01 +0800264 else
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800265 qspi_write32(priv->flags, &regs->lut[lut_base],
266 OPRND0(QSPI_CMD_PP_4B) | PAD0(LUT_PAD1) |
267 INSTR0(LUT_CMD) | OPRND1(ADDR32BIT) |
268 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
Peng Fan3a344482015-01-04 17:07:14 +0800269#endif
Peng Fan828e4682014-12-31 11:01:38 +0800270#ifdef CONFIG_MX6SX
271 /*
272 * To MX6SX, OPRND0(TX_BUFFER_SIZE) can not work correctly.
273 * So, Use IDATSZ in IPCR to determine the size and here set 0.
274 */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800275 qspi_write32(priv->flags, &regs->lut[lut_base + 1], OPRND0(0) |
Peng Fan828e4682014-12-31 11:01:38 +0800276 PAD0(LUT_PAD1) | INSTR0(LUT_WRITE));
277#else
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800278 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
279 OPRND0(TX_BUFFER_SIZE) |
280 PAD0(LUT_PAD1) | INSTR0(LUT_WRITE));
Peng Fan828e4682014-12-31 11:01:38 +0800281#endif
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800282 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
283 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800284
285 /* READ ID */
286 lut_base = SEQID_RDID * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800287 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDID) |
Alison Wangc7410e32014-05-06 09:13:01 +0800288 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(8) |
289 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800290 qspi_write32(priv->flags, &regs->lut[lut_base + 1], 0);
291 qspi_write32(priv->flags, &regs->lut[lut_base + 2], 0);
292 qspi_write32(priv->flags, &regs->lut[lut_base + 3], 0);
Alison Wangc7410e32014-05-06 09:13:01 +0800293
Peng Fan3642a872014-12-31 11:01:39 +0800294 /* SUB SECTOR 4K ERASE */
295 lut_base = SEQID_BE_4K * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800296 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BE_4K) |
Peng Fan3642a872014-12-31 11:01:39 +0800297 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
298 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
299
Peng Fan3a344482015-01-04 17:07:14 +0800300#ifdef CONFIG_SPI_FLASH_BAR
301 /*
302 * BRRD BRWR RDEAR WREAR are all supported, because it is hard to
303 * dynamically check whether to set BRRD BRWR or RDEAR WREAR during
304 * initialization.
305 */
306 lut_base = SEQID_BRRD * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800307 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BRRD) |
Peng Fan3a344482015-01-04 17:07:14 +0800308 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
309 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
310
311 lut_base = SEQID_BRWR * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800312 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_BRWR) |
Peng Fan3a344482015-01-04 17:07:14 +0800313 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
314 PAD1(LUT_PAD1) | INSTR1(LUT_WRITE));
315
316 lut_base = SEQID_RDEAR * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800317 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_RDEAR) |
Peng Fan3a344482015-01-04 17:07:14 +0800318 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
319 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
320
321 lut_base = SEQID_WREAR * 4;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800322 qspi_write32(priv->flags, &regs->lut[lut_base], OPRND0(QSPI_CMD_WREAR) |
Peng Fan3a344482015-01-04 17:07:14 +0800323 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
324 PAD1(LUT_PAD1) | INSTR1(LUT_WRITE));
325#endif
Yuan Yaod7193262016-03-15 14:36:42 +0800326
327 /*
328 * Read any device register.
329 * Used for Spansion S25FS-S family flash only.
330 */
331 lut_base = SEQID_RDAR * 4;
332 qspi_write32(priv->flags, &regs->lut[lut_base],
333 OPRND0(QSPI_CMD_RDAR) | PAD0(LUT_PAD1) |
334 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
335 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
336 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
337 OPRND0(8) | PAD0(LUT_PAD1) | INSTR0(LUT_DUMMY) |
338 OPRND1(1) | PAD1(LUT_PAD1) |
339 INSTR1(LUT_READ));
340
341 /*
342 * Write any device register.
343 * Used for Spansion S25FS-S family flash only.
344 */
345 lut_base = SEQID_WRAR * 4;
346 qspi_write32(priv->flags, &regs->lut[lut_base],
347 OPRND0(QSPI_CMD_WRAR) | PAD0(LUT_PAD1) |
348 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
349 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
350 qspi_write32(priv->flags, &regs->lut[lut_base + 1],
351 OPRND0(1) | PAD0(LUT_PAD1) | INSTR0(LUT_WRITE));
352
Alison Wangc7410e32014-05-06 09:13:01 +0800353 /* Lock the LUT */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800354 qspi_write32(priv->flags, &regs->lutkey, LUT_KEY_VALUE);
355 qspi_write32(priv->flags, &regs->lckcr, QSPI_LCKCR_LOCK);
Alison Wangc7410e32014-05-06 09:13:01 +0800356}
357
Peng Fan1c5f9662015-01-08 10:40:20 +0800358#if defined(CONFIG_SYS_FSL_QSPI_AHB)
359/*
360 * If we have changed the content of the flash by writing or erasing,
361 * we need to invalidate the AHB buffer. If we do not do so, we may read out
362 * the wrong data. The spec tells us reset the AHB domain and Serial Flash
363 * domain at the same time.
364 */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800365static inline void qspi_ahb_invalid(struct fsl_qspi_priv *priv)
Peng Fan1c5f9662015-01-08 10:40:20 +0800366{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800367 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan1c5f9662015-01-08 10:40:20 +0800368 u32 reg;
369
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800370 reg = qspi_read32(priv->flags, &regs->mcr);
Peng Fan1c5f9662015-01-08 10:40:20 +0800371 reg |= QSPI_MCR_SWRSTHD_MASK | QSPI_MCR_SWRSTSD_MASK;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800372 qspi_write32(priv->flags, &regs->mcr, reg);
Peng Fan1c5f9662015-01-08 10:40:20 +0800373
374 /*
375 * The minimum delay : 1 AHB + 2 SFCK clocks.
376 * Delay 1 us is enough.
377 */
378 udelay(1);
379
380 reg &= ~(QSPI_MCR_SWRSTHD_MASK | QSPI_MCR_SWRSTSD_MASK);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800381 qspi_write32(priv->flags, &regs->mcr, reg);
Peng Fan1c5f9662015-01-08 10:40:20 +0800382}
383
384/* Read out the data from the AHB buffer. */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800385static inline void qspi_ahb_read(struct fsl_qspi_priv *priv, u8 *rxbuf, int len)
Peng Fan1c5f9662015-01-08 10:40:20 +0800386{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800387 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan1c5f9662015-01-08 10:40:20 +0800388 u32 mcr_reg;
Yunhui Cuie4584cc2016-07-13 10:46:27 +0800389 void *rx_addr = NULL;
Peng Fan1c5f9662015-01-08 10:40:20 +0800390
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800391 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
Peng Fan1c5f9662015-01-08 10:40:20 +0800392
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800393 qspi_write32(priv->flags, &regs->mcr,
394 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Peng Fan1c5f9662015-01-08 10:40:20 +0800395 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
396
Yunhui Cuie4584cc2016-07-13 10:46:27 +0800397 rx_addr = (void *)(uintptr_t)(priv->cur_amba_base + priv->sf_addr);
Peng Fan1c5f9662015-01-08 10:40:20 +0800398 /* Read out the data directly from the AHB buffer. */
Yunhui Cuie4584cc2016-07-13 10:46:27 +0800399 memcpy(rxbuf, rx_addr, len);
Peng Fan1c5f9662015-01-08 10:40:20 +0800400
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800401 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Peng Fan1c5f9662015-01-08 10:40:20 +0800402}
403
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800404static void qspi_enable_ddr_mode(struct fsl_qspi_priv *priv)
Peng Fan1c5f9662015-01-08 10:40:20 +0800405{
406 u32 reg, reg2;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800407 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan1c5f9662015-01-08 10:40:20 +0800408
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800409 reg = qspi_read32(priv->flags, &regs->mcr);
Peng Fan1c5f9662015-01-08 10:40:20 +0800410 /* Disable the module */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800411 qspi_write32(priv->flags, &regs->mcr, reg | QSPI_MCR_MDIS_MASK);
Peng Fan1c5f9662015-01-08 10:40:20 +0800412
413 /* Set the Sampling Register for DDR */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800414 reg2 = qspi_read32(priv->flags, &regs->smpr);
Peng Fan1c5f9662015-01-08 10:40:20 +0800415 reg2 &= ~QSPI_SMPR_DDRSMP_MASK;
416 reg2 |= (2 << QSPI_SMPR_DDRSMP_SHIFT);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800417 qspi_write32(priv->flags, &regs->smpr, reg2);
Peng Fan1c5f9662015-01-08 10:40:20 +0800418
419 /* Enable the module again (enable the DDR too) */
420 reg |= QSPI_MCR_DDR_EN_MASK;
421 /* Enable bit 29 for imx6sx */
Jagan Tekic97ca922015-10-23 01:37:18 +0530422 reg |= BIT(29);
Peng Fan1c5f9662015-01-08 10:40:20 +0800423
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800424 qspi_write32(priv->flags, &regs->mcr, reg);
Peng Fan1c5f9662015-01-08 10:40:20 +0800425}
426
427/*
428 * There are two different ways to read out the data from the flash:
429 * the "IP Command Read" and the "AHB Command Read".
430 *
431 * The IC guy suggests we use the "AHB Command Read" which is faster
432 * then the "IP Command Read". (What's more is that there is a bug in
433 * the "IP Command Read" in the Vybrid.)
434 *
435 * After we set up the registers for the "AHB Command Read", we can use
436 * the memcpy to read the data directly. A "missed" access to the buffer
437 * causes the controller to clear the buffer, and use the sequence pointed
438 * by the QUADSPI_BFGENCR[SEQID] to initiate a read from the flash.
439 */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800440static void qspi_init_ahb_read(struct fsl_qspi_priv *priv)
Peng Fan1c5f9662015-01-08 10:40:20 +0800441{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800442 struct fsl_qspi_regs *regs = priv->regs;
443
Peng Fan1c5f9662015-01-08 10:40:20 +0800444 /* AHB configuration for access buffer 0/1/2 .*/
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800445 qspi_write32(priv->flags, &regs->buf0cr, QSPI_BUFXCR_INVALID_MSTRID);
446 qspi_write32(priv->flags, &regs->buf1cr, QSPI_BUFXCR_INVALID_MSTRID);
447 qspi_write32(priv->flags, &regs->buf2cr, QSPI_BUFXCR_INVALID_MSTRID);
448 qspi_write32(priv->flags, &regs->buf3cr, QSPI_BUF3CR_ALLMST_MASK |
Peng Fan1c5f9662015-01-08 10:40:20 +0800449 (0x80 << QSPI_BUF3CR_ADATSZ_SHIFT));
450
451 /* We only use the buffer3 */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800452 qspi_write32(priv->flags, &regs->buf0ind, 0);
453 qspi_write32(priv->flags, &regs->buf1ind, 0);
454 qspi_write32(priv->flags, &regs->buf2ind, 0);
Peng Fan1c5f9662015-01-08 10:40:20 +0800455
456 /*
457 * Set the default lut sequence for AHB Read.
458 * Parallel mode is disabled.
459 */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800460 qspi_write32(priv->flags, &regs->bfgencr,
Peng Fan1c5f9662015-01-08 10:40:20 +0800461 SEQID_FAST_READ << QSPI_BFGENCR_SEQID_SHIFT);
462
463 /*Enable DDR Mode*/
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800464 qspi_enable_ddr_mode(priv);
Peng Fan1c5f9662015-01-08 10:40:20 +0800465}
466#endif
467
Peng Fan3a344482015-01-04 17:07:14 +0800468#ifdef CONFIG_SPI_FLASH_BAR
469/* Bank register read/write, EAR register read/write */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800470static void qspi_op_rdbank(struct fsl_qspi_priv *priv, u8 *rxbuf, u32 len)
Peng Fan3a344482015-01-04 17:07:14 +0800471{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800472 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan3a344482015-01-04 17:07:14 +0800473 u32 reg, mcr_reg, data, seqid;
474
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800475 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
476 qspi_write32(priv->flags, &regs->mcr,
477 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
Peng Fan3a344482015-01-04 17:07:14 +0800478 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800479 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Peng Fan3a344482015-01-04 17:07:14 +0800480
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800481 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
Peng Fan3a344482015-01-04 17:07:14 +0800482
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800483 if (priv->cur_seqid == QSPI_CMD_BRRD)
Peng Fan3a344482015-01-04 17:07:14 +0800484 seqid = SEQID_BRRD;
485 else
486 seqid = SEQID_RDEAR;
487
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800488 qspi_write32(priv->flags, &regs->ipcr,
489 (seqid << QSPI_IPCR_SEQID_SHIFT) | len);
Peng Fan3a344482015-01-04 17:07:14 +0800490
491 /* Wait previous command complete */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800492 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Peng Fan3a344482015-01-04 17:07:14 +0800493 ;
494
495 while (1) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800496 reg = qspi_read32(priv->flags, &regs->rbsr);
Peng Fan3a344482015-01-04 17:07:14 +0800497 if (reg & QSPI_RBSR_RDBFL_MASK) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800498 data = qspi_read32(priv->flags, &regs->rbdr[0]);
Peng Fan3a344482015-01-04 17:07:14 +0800499 data = qspi_endian_xchg(data);
500 memcpy(rxbuf, &data, len);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800501 qspi_write32(priv->flags, &regs->mcr,
502 qspi_read32(priv->flags, &regs->mcr) |
Peng Fan3a344482015-01-04 17:07:14 +0800503 QSPI_MCR_CLR_RXF_MASK);
504 break;
505 }
506 }
507
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800508 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Peng Fan3a344482015-01-04 17:07:14 +0800509}
510#endif
Alison Wangc7410e32014-05-06 09:13:01 +0800511
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800512static void qspi_op_rdid(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len)
Alison Wangc7410e32014-05-06 09:13:01 +0800513{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800514 struct fsl_qspi_regs *regs = priv->regs;
Gong Qianyua5357ca2016-01-26 15:06:40 +0800515 u32 mcr_reg, rbsr_reg, data, size;
516 int i;
Alison Wangc7410e32014-05-06 09:13:01 +0800517
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800518 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
519 qspi_write32(priv->flags, &regs->mcr,
520 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
521 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
522 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Alison Wangc7410e32014-05-06 09:13:01 +0800523
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800524 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
Alison Wangc7410e32014-05-06 09:13:01 +0800525
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800526 qspi_write32(priv->flags, &regs->ipcr,
527 (SEQID_RDID << QSPI_IPCR_SEQID_SHIFT) | 0);
528 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800529 ;
530
531 i = 0;
Gong Qianyua5357ca2016-01-26 15:06:40 +0800532 while ((RX_BUFFER_SIZE >= len) && (len > 0)) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800533 rbsr_reg = qspi_read32(priv->flags, &regs->rbsr);
Alison Wangc7410e32014-05-06 09:13:01 +0800534 if (rbsr_reg & QSPI_RBSR_RDBFL_MASK) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800535 data = qspi_read32(priv->flags, &regs->rbdr[i]);
Alison Wangc7410e32014-05-06 09:13:01 +0800536 data = qspi_endian_xchg(data);
Gong Qianyua5357ca2016-01-26 15:06:40 +0800537 size = (len < 4) ? len : 4;
538 memcpy(rxbuf, &data, size);
539 len -= size;
Alison Wangc7410e32014-05-06 09:13:01 +0800540 rxbuf++;
Alison Wangc7410e32014-05-06 09:13:01 +0800541 i++;
542 }
543 }
544
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800545 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Alison Wangc7410e32014-05-06 09:13:01 +0800546}
547
Peng Fan1c5f9662015-01-08 10:40:20 +0800548/* If not use AHB read, read data from ip interface */
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800549static void qspi_op_read(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len)
Alison Wangc7410e32014-05-06 09:13:01 +0800550{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800551 struct fsl_qspi_regs *regs = priv->regs;
Alison Wangc7410e32014-05-06 09:13:01 +0800552 u32 mcr_reg, data;
553 int i, size;
554 u32 to_or_from;
Yuan Yaod7193262016-03-15 14:36:42 +0800555 u32 seqid;
556
557 if (priv->cur_seqid == QSPI_CMD_RDAR)
558 seqid = SEQID_RDAR;
559 else
560 seqid = SEQID_FAST_READ;
Alison Wangc7410e32014-05-06 09:13:01 +0800561
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800562 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
563 qspi_write32(priv->flags, &regs->mcr,
564 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
565 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
566 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Alison Wangc7410e32014-05-06 09:13:01 +0800567
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800568 to_or_from = priv->sf_addr + priv->cur_amba_base;
Alison Wangc7410e32014-05-06 09:13:01 +0800569
570 while (len > 0) {
Alexander Stein9b2c8a92015-11-04 09:19:10 +0100571 WATCHDOG_RESET();
572
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800573 qspi_write32(priv->flags, &regs->sfar, to_or_from);
Alison Wangc7410e32014-05-06 09:13:01 +0800574
575 size = (len > RX_BUFFER_SIZE) ?
576 RX_BUFFER_SIZE : len;
577
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800578 qspi_write32(priv->flags, &regs->ipcr,
Yuan Yaod7193262016-03-15 14:36:42 +0800579 (seqid << QSPI_IPCR_SEQID_SHIFT) |
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800580 size);
581 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800582 ;
583
584 to_or_from += size;
585 len -= size;
586
587 i = 0;
588 while ((RX_BUFFER_SIZE >= size) && (size > 0)) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800589 data = qspi_read32(priv->flags, &regs->rbdr[i]);
Alison Wangc7410e32014-05-06 09:13:01 +0800590 data = qspi_endian_xchg(data);
Yuan Yaod7193262016-03-15 14:36:42 +0800591 if (size < 4)
592 memcpy(rxbuf, &data, size);
593 else
594 memcpy(rxbuf, &data, 4);
Alison Wangc7410e32014-05-06 09:13:01 +0800595 rxbuf++;
596 size -= 4;
597 i++;
598 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800599 qspi_write32(priv->flags, &regs->mcr,
600 qspi_read32(priv->flags, &regs->mcr) |
601 QSPI_MCR_CLR_RXF_MASK);
Alison Wangc7410e32014-05-06 09:13:01 +0800602 }
603
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800604 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Alison Wangc7410e32014-05-06 09:13:01 +0800605}
606
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800607static void qspi_op_write(struct fsl_qspi_priv *priv, u8 *txbuf, u32 len)
Alison Wangc7410e32014-05-06 09:13:01 +0800608{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800609 struct fsl_qspi_regs *regs = priv->regs;
Peng Fan3a344482015-01-04 17:07:14 +0800610 u32 mcr_reg, data, reg, status_reg, seqid;
Alison Wangc7410e32014-05-06 09:13:01 +0800611 int i, size, tx_size;
612 u32 to_or_from = 0;
613
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800614 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
615 qspi_write32(priv->flags, &regs->mcr,
616 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
617 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
618 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Alison Wangc7410e32014-05-06 09:13:01 +0800619
620 status_reg = 0;
621 while ((status_reg & FLASH_STATUS_WEL) != FLASH_STATUS_WEL) {
Alexander Stein9b2c8a92015-11-04 09:19:10 +0100622 WATCHDOG_RESET();
623
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800624 qspi_write32(priv->flags, &regs->ipcr,
625 (SEQID_WREN << QSPI_IPCR_SEQID_SHIFT) | 0);
626 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800627 ;
628
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800629 qspi_write32(priv->flags, &regs->ipcr,
630 (SEQID_RDSR << QSPI_IPCR_SEQID_SHIFT) | 1);
631 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800632 ;
633
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800634 reg = qspi_read32(priv->flags, &regs->rbsr);
Alison Wangc7410e32014-05-06 09:13:01 +0800635 if (reg & QSPI_RBSR_RDBFL_MASK) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800636 status_reg = qspi_read32(priv->flags, &regs->rbdr[0]);
Alison Wangc7410e32014-05-06 09:13:01 +0800637 status_reg = qspi_endian_xchg(status_reg);
638 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800639 qspi_write32(priv->flags, &regs->mcr,
640 qspi_read32(priv->flags, &regs->mcr) |
641 QSPI_MCR_CLR_RXF_MASK);
Alison Wangc7410e32014-05-06 09:13:01 +0800642 }
643
Peng Fan3a344482015-01-04 17:07:14 +0800644 /* Default is page programming */
645 seqid = SEQID_PP;
Yuan Yaod7193262016-03-15 14:36:42 +0800646 if (priv->cur_seqid == QSPI_CMD_WRAR)
647 seqid = SEQID_WRAR;
Peng Fan3a344482015-01-04 17:07:14 +0800648#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800649 if (priv->cur_seqid == QSPI_CMD_BRWR)
Peng Fan3a344482015-01-04 17:07:14 +0800650 seqid = SEQID_BRWR;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800651 else if (priv->cur_seqid == QSPI_CMD_WREAR)
Peng Fan3a344482015-01-04 17:07:14 +0800652 seqid = SEQID_WREAR;
653#endif
654
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800655 to_or_from = priv->sf_addr + priv->cur_amba_base;
Peng Fan3a344482015-01-04 17:07:14 +0800656
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800657 qspi_write32(priv->flags, &regs->sfar, to_or_from);
Alison Wangc7410e32014-05-06 09:13:01 +0800658
659 tx_size = (len > TX_BUFFER_SIZE) ?
660 TX_BUFFER_SIZE : len;
661
Peng Fan3a344482015-01-04 17:07:14 +0800662 size = tx_size / 4;
Alison Wangc7410e32014-05-06 09:13:01 +0800663 for (i = 0; i < size; i++) {
Peng Fan3a344482015-01-04 17:07:14 +0800664 memcpy(&data, txbuf, 4);
665 data = qspi_endian_xchg(data);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800666 qspi_write32(priv->flags, &regs->tbdr, data);
Peng Fan3a344482015-01-04 17:07:14 +0800667 txbuf += 4;
Alison Wangc7410e32014-05-06 09:13:01 +0800668 }
669
Peng Fan3a344482015-01-04 17:07:14 +0800670 size = tx_size % 4;
671 if (size) {
672 data = 0;
673 memcpy(&data, txbuf, size);
674 data = qspi_endian_xchg(data);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800675 qspi_write32(priv->flags, &regs->tbdr, data);
Peng Fan3a344482015-01-04 17:07:14 +0800676 }
677
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800678 qspi_write32(priv->flags, &regs->ipcr,
679 (seqid << QSPI_IPCR_SEQID_SHIFT) | tx_size);
680 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800681 ;
682
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800683 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Alison Wangc7410e32014-05-06 09:13:01 +0800684}
685
Gong Qianyu4e8630b2016-01-26 15:06:41 +0800686static void qspi_op_rdsr(struct fsl_qspi_priv *priv, void *rxbuf, u32 len)
Alison Wangc7410e32014-05-06 09:13:01 +0800687{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800688 struct fsl_qspi_regs *regs = priv->regs;
Alison Wangc7410e32014-05-06 09:13:01 +0800689 u32 mcr_reg, reg, data;
690
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800691 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
692 qspi_write32(priv->flags, &regs->mcr,
693 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
694 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
695 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Alison Wangc7410e32014-05-06 09:13:01 +0800696
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800697 qspi_write32(priv->flags, &regs->sfar, priv->cur_amba_base);
Alison Wangc7410e32014-05-06 09:13:01 +0800698
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800699 qspi_write32(priv->flags, &regs->ipcr,
700 (SEQID_RDSR << QSPI_IPCR_SEQID_SHIFT) | 0);
701 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800702 ;
703
704 while (1) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800705 reg = qspi_read32(priv->flags, &regs->rbsr);
Alison Wangc7410e32014-05-06 09:13:01 +0800706 if (reg & QSPI_RBSR_RDBFL_MASK) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800707 data = qspi_read32(priv->flags, &regs->rbdr[0]);
Alison Wangc7410e32014-05-06 09:13:01 +0800708 data = qspi_endian_xchg(data);
Gong Qianyu4e8630b2016-01-26 15:06:41 +0800709 memcpy(rxbuf, &data, len);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800710 qspi_write32(priv->flags, &regs->mcr,
711 qspi_read32(priv->flags, &regs->mcr) |
712 QSPI_MCR_CLR_RXF_MASK);
Alison Wangc7410e32014-05-06 09:13:01 +0800713 break;
714 }
715 }
716
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800717 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Alison Wangc7410e32014-05-06 09:13:01 +0800718}
719
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800720static void qspi_op_erase(struct fsl_qspi_priv *priv)
Alison Wangc7410e32014-05-06 09:13:01 +0800721{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800722 struct fsl_qspi_regs *regs = priv->regs;
Alison Wangc7410e32014-05-06 09:13:01 +0800723 u32 mcr_reg;
724 u32 to_or_from = 0;
725
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800726 mcr_reg = qspi_read32(priv->flags, &regs->mcr);
727 qspi_write32(priv->flags, &regs->mcr,
728 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
729 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
730 qspi_write32(priv->flags, &regs->rbct, QSPI_RBCT_RXBRD_USEIPS);
Alison Wangc7410e32014-05-06 09:13:01 +0800731
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800732 to_or_from = priv->sf_addr + priv->cur_amba_base;
733 qspi_write32(priv->flags, &regs->sfar, to_or_from);
Alison Wangc7410e32014-05-06 09:13:01 +0800734
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800735 qspi_write32(priv->flags, &regs->ipcr,
736 (SEQID_WREN << QSPI_IPCR_SEQID_SHIFT) | 0);
737 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800738 ;
739
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800740 if (priv->cur_seqid == QSPI_CMD_SE) {
741 qspi_write32(priv->flags, &regs->ipcr,
Peng Fan3642a872014-12-31 11:01:39 +0800742 (SEQID_SE << QSPI_IPCR_SEQID_SHIFT) | 0);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800743 } else if (priv->cur_seqid == QSPI_CMD_BE_4K) {
744 qspi_write32(priv->flags, &regs->ipcr,
Peng Fan3642a872014-12-31 11:01:39 +0800745 (SEQID_BE_4K << QSPI_IPCR_SEQID_SHIFT) | 0);
746 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800747 while (qspi_read32(priv->flags, &regs->sr) & QSPI_SR_BUSY_MASK)
Alison Wangc7410e32014-05-06 09:13:01 +0800748 ;
749
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800750 qspi_write32(priv->flags, &regs->mcr, mcr_reg);
Alison Wangc7410e32014-05-06 09:13:01 +0800751}
752
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800753int qspi_xfer(struct fsl_qspi_priv *priv, unsigned int bitlen,
Alison Wangc7410e32014-05-06 09:13:01 +0800754 const void *dout, void *din, unsigned long flags)
755{
Alison Wangc7410e32014-05-06 09:13:01 +0800756 u32 bytes = DIV_ROUND_UP(bitlen, 8);
Peng Fan3a344482015-01-04 17:07:14 +0800757 static u32 wr_sfaddr;
Alison Wangc7410e32014-05-06 09:13:01 +0800758 u32 txbuf;
759
760 if (dout) {
Peng Fan3a344482015-01-04 17:07:14 +0800761 if (flags & SPI_XFER_BEGIN) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800762 priv->cur_seqid = *(u8 *)dout;
Peng Fan3a344482015-01-04 17:07:14 +0800763 memcpy(&txbuf, dout, 4);
764 }
Alison Wangc7410e32014-05-06 09:13:01 +0800765
766 if (flags == SPI_XFER_END) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800767 priv->sf_addr = wr_sfaddr;
768 qspi_op_write(priv, (u8 *)dout, bytes);
Alison Wangc7410e32014-05-06 09:13:01 +0800769 return 0;
770 }
771
Yuan Yaod7193262016-03-15 14:36:42 +0800772 if (priv->cur_seqid == QSPI_CMD_FAST_READ ||
773 priv->cur_seqid == QSPI_CMD_RDAR) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800774 priv->sf_addr = swab32(txbuf) & OFFSET_BITS_MASK;
775 } else if ((priv->cur_seqid == QSPI_CMD_SE) ||
776 (priv->cur_seqid == QSPI_CMD_BE_4K)) {
777 priv->sf_addr = swab32(txbuf) & OFFSET_BITS_MASK;
778 qspi_op_erase(priv);
Yuan Yaod7193262016-03-15 14:36:42 +0800779 } else if (priv->cur_seqid == QSPI_CMD_PP ||
780 priv->cur_seqid == QSPI_CMD_WRAR) {
Peng Fan3a344482015-01-04 17:07:14 +0800781 wr_sfaddr = swab32(txbuf) & OFFSET_BITS_MASK;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800782 } else if ((priv->cur_seqid == QSPI_CMD_BRWR) ||
783 (priv->cur_seqid == QSPI_CMD_WREAR)) {
Peng Fan3a344482015-01-04 17:07:14 +0800784#ifdef CONFIG_SPI_FLASH_BAR
Peng Fan3a344482015-01-04 17:07:14 +0800785 wr_sfaddr = 0;
Peng Fan3a344482015-01-04 17:07:14 +0800786#endif
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800787 }
Alison Wangc7410e32014-05-06 09:13:01 +0800788 }
789
790 if (din) {
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800791 if (priv->cur_seqid == QSPI_CMD_FAST_READ) {
Peng Fan1c5f9662015-01-08 10:40:20 +0800792#ifdef CONFIG_SYS_FSL_QSPI_AHB
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800793 qspi_ahb_read(priv, din, bytes);
Peng Fan1c5f9662015-01-08 10:40:20 +0800794#else
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800795 qspi_op_read(priv, din, bytes);
Peng Fan1c5f9662015-01-08 10:40:20 +0800796#endif
Yuan Yaod7193262016-03-15 14:36:42 +0800797 } else if (priv->cur_seqid == QSPI_CMD_RDAR) {
798 qspi_op_read(priv, din, bytes);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800799 } else if (priv->cur_seqid == QSPI_CMD_RDID)
800 qspi_op_rdid(priv, din, bytes);
801 else if (priv->cur_seqid == QSPI_CMD_RDSR)
Gong Qianyu4e8630b2016-01-26 15:06:41 +0800802 qspi_op_rdsr(priv, din, bytes);
Peng Fan3a344482015-01-04 17:07:14 +0800803#ifdef CONFIG_SPI_FLASH_BAR
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800804 else if ((priv->cur_seqid == QSPI_CMD_BRRD) ||
805 (priv->cur_seqid == QSPI_CMD_RDEAR)) {
806 priv->sf_addr = 0;
807 qspi_op_rdbank(priv, din, bytes);
Peng Fan3a344482015-01-04 17:07:14 +0800808 }
809#endif
Alison Wangc7410e32014-05-06 09:13:01 +0800810 }
811
Peng Fan1c5f9662015-01-08 10:40:20 +0800812#ifdef CONFIG_SYS_FSL_QSPI_AHB
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800813 if ((priv->cur_seqid == QSPI_CMD_SE) ||
814 (priv->cur_seqid == QSPI_CMD_PP) ||
815 (priv->cur_seqid == QSPI_CMD_BE_4K) ||
816 (priv->cur_seqid == QSPI_CMD_WREAR) ||
817 (priv->cur_seqid == QSPI_CMD_BRWR))
818 qspi_ahb_invalid(priv);
819#endif
820
821 return 0;
822}
823
824void qspi_module_disable(struct fsl_qspi_priv *priv, u8 disable)
825{
826 u32 mcr_val;
827
828 mcr_val = qspi_read32(priv->flags, &priv->regs->mcr);
829 if (disable)
830 mcr_val |= QSPI_MCR_MDIS_MASK;
831 else
832 mcr_val &= ~QSPI_MCR_MDIS_MASK;
833 qspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
834}
835
836void qspi_cfg_smpr(struct fsl_qspi_priv *priv, u32 clear_bits, u32 set_bits)
837{
838 u32 smpr_val;
839
840 smpr_val = qspi_read32(priv->flags, &priv->regs->smpr);
841 smpr_val &= ~clear_bits;
842 smpr_val |= set_bits;
843 qspi_write32(priv->flags, &priv->regs->smpr, smpr_val);
844}
845#ifndef CONFIG_DM_SPI
846static unsigned long spi_bases[] = {
847 QSPI0_BASE_ADDR,
848#ifdef CONFIG_MX6SX
849 QSPI1_BASE_ADDR,
850#endif
851};
852
853static unsigned long amba_bases[] = {
854 QSPI0_AMBA_BASE,
855#ifdef CONFIG_MX6SX
856 QSPI1_AMBA_BASE,
857#endif
858};
859
860static inline struct fsl_qspi *to_qspi_spi(struct spi_slave *slave)
861{
862 return container_of(slave, struct fsl_qspi, slave);
863}
864
865struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
866 unsigned int max_hz, unsigned int mode)
867{
York Sunb4f57a92016-10-05 13:19:08 -0700868 u32 mcr_val;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800869 struct fsl_qspi *qspi;
870 struct fsl_qspi_regs *regs;
871 u32 total_size;
872
873 if (bus >= ARRAY_SIZE(spi_bases))
874 return NULL;
875
876 if (cs >= FSL_QSPI_FLASH_NUM)
877 return NULL;
878
879 qspi = spi_alloc_slave(struct fsl_qspi, bus, cs);
880 if (!qspi)
881 return NULL;
882
883#ifdef CONFIG_SYS_FSL_QSPI_BE
884 qspi->priv.flags |= QSPI_FLAG_REGMAP_ENDIAN_BIG;
885#endif
886
887 regs = (struct fsl_qspi_regs *)spi_bases[bus];
888 qspi->priv.regs = regs;
889 /*
890 * According cs, use different amba_base to choose the
891 * corresponding flash devices.
892 *
893 * If not, only one flash device is used even if passing
894 * different cs using `sf probe`
895 */
896 qspi->priv.cur_amba_base = amba_bases[bus] + cs * FSL_QSPI_FLASH_SIZE;
897
898 qspi->slave.max_write_size = TX_BUFFER_SIZE;
899
York Sunb4f57a92016-10-05 13:19:08 -0700900 mcr_val = qspi_read32(qspi->priv.flags, &regs->mcr);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800901 qspi_write32(qspi->priv.flags, &regs->mcr,
York Sunb4f57a92016-10-05 13:19:08 -0700902 QSPI_MCR_RESERVED_MASK | QSPI_MCR_MDIS_MASK |
903 (mcr_val & QSPI_MCR_END_CFD_MASK));
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800904
905 qspi_cfg_smpr(&qspi->priv,
906 ~(QSPI_SMPR_FSDLY_MASK | QSPI_SMPR_DDRSMP_MASK |
907 QSPI_SMPR_FSPHS_MASK | QSPI_SMPR_HSENA_MASK), 0);
908
909 total_size = FSL_QSPI_FLASH_SIZE * FSL_QSPI_FLASH_NUM;
910 /*
911 * Any read access to non-implemented addresses will provide
912 * undefined results.
913 *
914 * In case single die flash devices, TOP_ADDR_MEMA2 and
915 * TOP_ADDR_MEMB2 should be initialized/programmed to
916 * TOP_ADDR_MEMA1 and TOP_ADDR_MEMB1 respectively - in effect,
917 * setting the size of these devices to 0. This would ensure
918 * that the complete memory map is assigned to only one flash device.
919 */
920 qspi_write32(qspi->priv.flags, &regs->sfa1ad,
921 FSL_QSPI_FLASH_SIZE | amba_bases[bus]);
922 qspi_write32(qspi->priv.flags, &regs->sfa2ad,
923 FSL_QSPI_FLASH_SIZE | amba_bases[bus]);
924 qspi_write32(qspi->priv.flags, &regs->sfb1ad,
925 total_size | amba_bases[bus]);
926 qspi_write32(qspi->priv.flags, &regs->sfb2ad,
927 total_size | amba_bases[bus]);
928
929 qspi_set_lut(&qspi->priv);
930
931#ifdef CONFIG_SYS_FSL_QSPI_AHB
932 qspi_init_ahb_read(&qspi->priv);
Peng Fan1c5f9662015-01-08 10:40:20 +0800933#endif
934
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800935 qspi_module_disable(&qspi->priv, 0);
936
937 return &qspi->slave;
938}
939
940void spi_free_slave(struct spi_slave *slave)
941{
942 struct fsl_qspi *qspi = to_qspi_spi(slave);
943
944 free(qspi);
945}
946
947int spi_claim_bus(struct spi_slave *slave)
948{
Alison Wangc7410e32014-05-06 09:13:01 +0800949 return 0;
950}
951
952void spi_release_bus(struct spi_slave *slave)
953{
954 /* Nothing to do */
955}
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800956
957int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
958 const void *dout, void *din, unsigned long flags)
959{
960 struct fsl_qspi *qspi = to_qspi_spi(slave);
961
962 return qspi_xfer(&qspi->priv, bitlen, dout, din, flags);
963}
964
965void spi_init(void)
966{
967 /* Nothing to do */
968}
969#else
970static int fsl_qspi_child_pre_probe(struct udevice *dev)
971{
Simon Glassde44acf2015-09-28 23:32:01 -0600972 struct spi_slave *slave = dev_get_parent_priv(dev);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800973
974 slave->max_write_size = TX_BUFFER_SIZE;
975
976 return 0;
977}
978
979static int fsl_qspi_probe(struct udevice *bus)
980{
York Sunb4f57a92016-10-05 13:19:08 -0700981 u32 mcr_val;
Yuan Yaob4bfe102016-03-15 14:36:41 +0800982 u32 amba_size_per_chip;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800983 struct fsl_qspi_platdata *plat = dev_get_platdata(bus);
984 struct fsl_qspi_priv *priv = dev_get_priv(bus);
985 struct dm_spi_bus *dm_spi_bus;
Yuan Yaob4bfe102016-03-15 14:36:41 +0800986 int i;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800987
988 dm_spi_bus = bus->uclass_priv;
989
990 dm_spi_bus->max_hz = plat->speed_hz;
991
Gong Qianyu14b532e2016-01-26 15:06:39 +0800992 priv->regs = (struct fsl_qspi_regs *)(uintptr_t)plat->reg_base;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800993 priv->flags = plat->flags;
994
995 priv->speed_hz = plat->speed_hz;
Yuan Yaoae412392016-03-15 14:36:40 +0800996 /*
997 * QSPI SFADR width is 32bits, the max dest addr is 4GB-1.
998 * AMBA memory zone should be located on the 0~4GB space
999 * even on a 64bits cpu.
1000 */
1001 priv->amba_base[0] = (u32)plat->amba_base;
1002 priv->amba_total_size = (u32)plat->amba_total_size;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001003 priv->flash_num = plat->flash_num;
1004 priv->num_chipselect = plat->num_chipselect;
1005
York Sunb4f57a92016-10-05 13:19:08 -07001006 mcr_val = qspi_read32(priv->flags, &priv->regs->mcr);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001007 qspi_write32(priv->flags, &priv->regs->mcr,
York Sunb4f57a92016-10-05 13:19:08 -07001008 QSPI_MCR_RESERVED_MASK | QSPI_MCR_MDIS_MASK |
1009 (mcr_val & QSPI_MCR_END_CFD_MASK));
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001010
1011 qspi_cfg_smpr(priv, ~(QSPI_SMPR_FSDLY_MASK | QSPI_SMPR_DDRSMP_MASK |
1012 QSPI_SMPR_FSPHS_MASK | QSPI_SMPR_HSENA_MASK), 0);
1013
Yuan Yaob4bfe102016-03-15 14:36:41 +08001014 /*
1015 * Assign AMBA memory zone for every chipselect
1016 * QuadSPI has two channels, every channel has two chipselects.
1017 * If the property 'num-cs' in dts is 2, the AMBA memory will be divided
1018 * into two parts and assign to every channel. This indicate that every
1019 * channel only has one valid chipselect.
1020 * If the property 'num-cs' in dts is 4, the AMBA memory will be divided
1021 * into four parts and assign to every chipselect.
1022 * Every channel will has two valid chipselects.
1023 */
1024 amba_size_per_chip = priv->amba_total_size >>
1025 (priv->num_chipselect >> 1);
1026 for (i = 1 ; i < priv->num_chipselect ; i++)
1027 priv->amba_base[i] =
1028 amba_size_per_chip + priv->amba_base[i - 1];
1029
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001030 /*
1031 * Any read access to non-implemented addresses will provide
1032 * undefined results.
1033 *
1034 * In case single die flash devices, TOP_ADDR_MEMA2 and
1035 * TOP_ADDR_MEMB2 should be initialized/programmed to
1036 * TOP_ADDR_MEMA1 and TOP_ADDR_MEMB1 respectively - in effect,
1037 * setting the size of these devices to 0. This would ensure
1038 * that the complete memory map is assigned to only one flash device.
1039 */
Yuan Yaob4bfe102016-03-15 14:36:41 +08001040 qspi_write32(priv->flags, &priv->regs->sfa1ad, priv->amba_base[1]);
1041 switch (priv->num_chipselect) {
1042 case 2:
1043 qspi_write32(priv->flags, &priv->regs->sfa2ad,
1044 priv->amba_base[1]);
1045 qspi_write32(priv->flags, &priv->regs->sfb1ad,
1046 priv->amba_base[1] + amba_size_per_chip);
1047 qspi_write32(priv->flags, &priv->regs->sfb2ad,
1048 priv->amba_base[1] + amba_size_per_chip);
1049 break;
1050 case 4:
1051 qspi_write32(priv->flags, &priv->regs->sfa2ad,
1052 priv->amba_base[2]);
1053 qspi_write32(priv->flags, &priv->regs->sfb1ad,
1054 priv->amba_base[3]);
1055 qspi_write32(priv->flags, &priv->regs->sfb2ad,
1056 priv->amba_base[3] + amba_size_per_chip);
1057 break;
1058 default:
1059 debug("Error: Unsupported chipselect number %u!\n",
1060 priv->num_chipselect);
1061 qspi_module_disable(priv, 1);
1062 return -EINVAL;
1063 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001064
1065 qspi_set_lut(priv);
1066
1067#ifdef CONFIG_SYS_FSL_QSPI_AHB
1068 qspi_init_ahb_read(priv);
1069#endif
1070
1071 qspi_module_disable(priv, 0);
1072
1073 return 0;
1074}
1075
1076static int fsl_qspi_ofdata_to_platdata(struct udevice *bus)
1077{
Yuan Yaoae412392016-03-15 14:36:40 +08001078 struct fdt_resource res_regs, res_mem;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001079 struct fsl_qspi_platdata *plat = bus->platdata;
1080 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -07001081 int node = dev_of_offset(bus);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001082 int ret, flash_num = 0, subnode;
1083
1084 if (fdtdec_get_bool(blob, node, "big-endian"))
1085 plat->flags |= QSPI_FLAG_REGMAP_ENDIAN_BIG;
1086
Yuan Yaoae412392016-03-15 14:36:40 +08001087 ret = fdt_get_named_resource(blob, node, "reg", "reg-names",
1088 "QuadSPI", &res_regs);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001089 if (ret) {
Yuan Yaoae412392016-03-15 14:36:40 +08001090 debug("Error: can't get regs base addresses(ret = %d)!\n", ret);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001091 return -ENOMEM;
1092 }
Yuan Yaoae412392016-03-15 14:36:40 +08001093 ret = fdt_get_named_resource(blob, node, "reg", "reg-names",
1094 "QuadSPI-memory", &res_mem);
1095 if (ret) {
1096 debug("Error: can't get AMBA base addresses(ret = %d)!\n", ret);
1097 return -ENOMEM;
1098 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001099
1100 /* Count flash numbers */
Simon Glass499c29e2016-10-02 17:59:29 -06001101 fdt_for_each_subnode(subnode, blob, node)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001102 ++flash_num;
1103
1104 if (flash_num == 0) {
1105 debug("Error: Missing flashes!\n");
1106 return -ENODEV;
1107 }
1108
1109 plat->speed_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
1110 FSL_QSPI_DEFAULT_SCK_FREQ);
1111 plat->num_chipselect = fdtdec_get_int(blob, node, "num-cs",
1112 FSL_QSPI_MAX_CHIPSELECT_NUM);
1113
Yuan Yaoae412392016-03-15 14:36:40 +08001114 plat->reg_base = res_regs.start;
1115 plat->amba_base = res_mem.start;
1116 plat->amba_total_size = res_mem.end - res_mem.start + 1;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001117 plat->flash_num = flash_num;
1118
Yuan Yaoae412392016-03-15 14:36:40 +08001119 debug("%s: regs=<0x%llx> <0x%llx, 0x%llx>, max-frequency=%d, endianess=%s\n",
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001120 __func__,
Yuan Yaoae412392016-03-15 14:36:40 +08001121 (u64)plat->reg_base,
1122 (u64)plat->amba_base,
1123 (u64)plat->amba_total_size,
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001124 plat->speed_hz,
1125 plat->flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ? "be" : "le"
1126 );
1127
1128 return 0;
1129}
1130
1131static int fsl_qspi_xfer(struct udevice *dev, unsigned int bitlen,
1132 const void *dout, void *din, unsigned long flags)
1133{
1134 struct fsl_qspi_priv *priv;
1135 struct udevice *bus;
1136
1137 bus = dev->parent;
1138 priv = dev_get_priv(bus);
1139
1140 return qspi_xfer(priv, bitlen, dout, din, flags);
1141}
1142
1143static int fsl_qspi_claim_bus(struct udevice *dev)
1144{
1145 struct fsl_qspi_priv *priv;
1146 struct udevice *bus;
1147 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
1148
1149 bus = dev->parent;
1150 priv = dev_get_priv(bus);
1151
Yuan Yaob4bfe102016-03-15 14:36:41 +08001152 priv->cur_amba_base = priv->amba_base[slave_plat->cs];
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +08001153
1154 qspi_module_disable(priv, 0);
1155
1156 return 0;
1157}
1158
1159static int fsl_qspi_release_bus(struct udevice *dev)
1160{
1161 struct fsl_qspi_priv *priv;
1162 struct udevice *bus;
1163
1164 bus = dev->parent;
1165 priv = dev_get_priv(bus);
1166
1167 qspi_module_disable(priv, 1);
1168
1169 return 0;
1170}
1171
1172static int fsl_qspi_set_speed(struct udevice *bus, uint speed)
1173{
1174 /* Nothing to do */
1175 return 0;
1176}
1177
1178static int fsl_qspi_set_mode(struct udevice *bus, uint mode)
1179{
1180 /* Nothing to do */
1181 return 0;
1182}
1183
1184static const struct dm_spi_ops fsl_qspi_ops = {
1185 .claim_bus = fsl_qspi_claim_bus,
1186 .release_bus = fsl_qspi_release_bus,
1187 .xfer = fsl_qspi_xfer,
1188 .set_speed = fsl_qspi_set_speed,
1189 .set_mode = fsl_qspi_set_mode,
1190};
1191
1192static const struct udevice_id fsl_qspi_ids[] = {
1193 { .compatible = "fsl,vf610-qspi" },
1194 { .compatible = "fsl,imx6sx-qspi" },
1195 { }
1196};
1197
1198U_BOOT_DRIVER(fsl_qspi) = {
1199 .name = "fsl_qspi",
1200 .id = UCLASS_SPI,
1201 .of_match = fsl_qspi_ids,
1202 .ops = &fsl_qspi_ops,
1203 .ofdata_to_platdata = fsl_qspi_ofdata_to_platdata,
1204 .platdata_auto_alloc_size = sizeof(struct fsl_qspi_platdata),
1205 .priv_auto_alloc_size = sizeof(struct fsl_qspi_priv),
1206 .probe = fsl_qspi_probe,
1207 .child_pre_probe = fsl_qspi_child_pre_probe,
1208};
1209#endif