blob: 8a0a53cb372034b5ebb1ef39d094f135d6bb4fb3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kuldeep Singhd8429a12020-02-20 22:57:52 +05302
Alison Wangc7410e32014-05-06 09:13:01 +08003/*
Kuldeep Singhd8429a12020-02-20 22:57:52 +05304 * Freescale QuadSPI driver.
5 *
6 * Copyright (C) 2013 Freescale Semiconductor, Inc.
7 * Copyright (C) 2018 Bootlin
8 * Copyright (C) 2018 exceet electronics GmbH
9 * Copyright (C) 2018 Kontron Electronics GmbH
10 * Copyright 2019-2020 NXP
Alison Wangc7410e32014-05-06 09:13:01 +080011 *
Kuldeep Singhd8429a12020-02-20 22:57:52 +053012 * This driver is a ported version of Linux Freescale QSPI driver taken from
13 * v5.5-rc1 tag having following information.
14 *
15 * Transition to SPI MEM interface:
16 * Authors:
17 * Boris Brezillon <bbrezillon@kernel.org>
18 * Frieder Schrempf <frieder.schrempf@kontron.de>
19 * Yogesh Gaur <yogeshnarayan.gaur@nxp.com>
20 * Suresh Gupta <suresh.gupta@nxp.com>
21 *
22 * Based on the original fsl-quadspi.c spi-nor driver.
23 * Transition to spi-mem in spi-fsl-qspi.c
Alison Wangc7410e32014-05-06 09:13:01 +080024 */
25
Sean Andersona89b9432020-10-04 21:39:50 -040026#include <dm.h>
27#include <dm/device_compat.h>
Simon Glass0f2af882020-05-10 11:40:05 -060028#include <log.h>
Sean Andersona89b9432020-10-04 21:39:50 -040029#include <spi.h>
30#include <spi-mem.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060031#include <asm/global_data.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060032#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060033#include <linux/delay.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060034#include <linux/libfdt.h>
35#include <linux/sizes.h>
36#include <linux/iopoll.h>
Kuldeep Singhd8429a12020-02-20 22:57:52 +053037#include <linux/iopoll.h>
38#include <linux/sizes.h>
39#include <linux/err.h>
Sean Andersona89b9432020-10-04 21:39:50 -040040#include <asm/io.h>
Alison Wangc7410e32014-05-06 09:13:01 +080041
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080042DECLARE_GLOBAL_DATA_PTR;
43
Kuldeep Singhd8429a12020-02-20 22:57:52 +053044/*
45 * The driver only uses one single LUT entry, that is updated on
46 * each call of exec_op(). Index 0 is preset at boot with a basic
47 * read operation, so let's use the last entry (15).
48 */
49#define SEQID_LUT 15
Ye Lid7e3c9a2020-06-09 00:59:06 -070050#define SEQID_LUT_AHB 14
Alison Wangc7410e32014-05-06 09:13:01 +080051
Kuldeep Singhd8429a12020-02-20 22:57:52 +053052/* Registers used by the driver */
53#define QUADSPI_MCR 0x00
54#define QUADSPI_MCR_RESERVED_MASK GENMASK(19, 16)
55#define QUADSPI_MCR_MDIS_MASK BIT(14)
56#define QUADSPI_MCR_CLR_TXF_MASK BIT(11)
57#define QUADSPI_MCR_CLR_RXF_MASK BIT(10)
58#define QUADSPI_MCR_DDR_EN_MASK BIT(7)
59#define QUADSPI_MCR_END_CFG_MASK GENMASK(3, 2)
60#define QUADSPI_MCR_SWRSTHD_MASK BIT(1)
61#define QUADSPI_MCR_SWRSTSD_MASK BIT(0)
Alison Wangc7410e32014-05-06 09:13:01 +080062
Kuldeep Singhd8429a12020-02-20 22:57:52 +053063#define QUADSPI_IPCR 0x08
64#define QUADSPI_IPCR_SEQID(x) ((x) << 24)
65#define QUADSPI_FLSHCR 0x0c
66#define QUADSPI_FLSHCR_TCSS_MASK GENMASK(3, 0)
67#define QUADSPI_FLSHCR_TCSH_MASK GENMASK(11, 8)
68#define QUADSPI_FLSHCR_TDH_MASK GENMASK(17, 16)
Alison Wangc7410e32014-05-06 09:13:01 +080069
Kuldeep Singhd8429a12020-02-20 22:57:52 +053070#define QUADSPI_BUF3CR 0x1c
71#define QUADSPI_BUF3CR_ALLMST_MASK BIT(31)
72#define QUADSPI_BUF3CR_ADATSZ(x) ((x) << 8)
73#define QUADSPI_BUF3CR_ADATSZ_MASK GENMASK(15, 8)
Alison Wangc7410e32014-05-06 09:13:01 +080074
Kuldeep Singhd8429a12020-02-20 22:57:52 +053075#define QUADSPI_BFGENCR 0x20
76#define QUADSPI_BFGENCR_SEQID(x) ((x) << 12)
Peng Fan3a344482015-01-04 17:07:14 +080077
Kuldeep Singhd8429a12020-02-20 22:57:52 +053078#define QUADSPI_BUF0IND 0x30
79#define QUADSPI_BUF1IND 0x34
80#define QUADSPI_BUF2IND 0x38
81#define QUADSPI_SFAR 0x100
Peng Fan3a344482015-01-04 17:07:14 +080082
Kuldeep Singhd8429a12020-02-20 22:57:52 +053083#define QUADSPI_SMPR 0x108
84#define QUADSPI_SMPR_DDRSMP_MASK GENMASK(18, 16)
85#define QUADSPI_SMPR_FSDLY_MASK BIT(6)
86#define QUADSPI_SMPR_FSPHS_MASK BIT(5)
87#define QUADSPI_SMPR_HSENA_MASK BIT(0)
Yuan Yaod7193262016-03-15 14:36:42 +080088
Kuldeep Singhd8429a12020-02-20 22:57:52 +053089#define QUADSPI_RBCT 0x110
90#define QUADSPI_RBCT_WMRK_MASK GENMASK(4, 0)
91#define QUADSPI_RBCT_RXBRD_USEIPS BIT(8)
Alison Wangc7410e32014-05-06 09:13:01 +080092
Kuldeep Singhd8429a12020-02-20 22:57:52 +053093#define QUADSPI_TBDR 0x154
Alison Wangc7410e32014-05-06 09:13:01 +080094
Kuldeep Singhd8429a12020-02-20 22:57:52 +053095#define QUADSPI_SR 0x15c
96#define QUADSPI_SR_IP_ACC_MASK BIT(1)
97#define QUADSPI_SR_AHB_ACC_MASK BIT(2)
Alison Wangc7410e32014-05-06 09:13:01 +080098
Kuldeep Singhd8429a12020-02-20 22:57:52 +053099#define QUADSPI_FR 0x160
100#define QUADSPI_FR_TFF_MASK BIT(0)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800101
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530102#define QUADSPI_RSER 0x164
103#define QUADSPI_RSER_TFIE BIT(0)
Ye Li007b6042019-08-14 11:31:36 +0000104
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530105#define QUADSPI_SPTRCLR 0x16c
106#define QUADSPI_SPTRCLR_IPPTRC BIT(8)
107#define QUADSPI_SPTRCLR_BFPTRC BIT(0)
Ye Li007b6042019-08-14 11:31:36 +0000108
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530109#define QUADSPI_SFA1AD 0x180
110#define QUADSPI_SFA2AD 0x184
111#define QUADSPI_SFB1AD 0x188
112#define QUADSPI_SFB2AD 0x18c
113#define QUADSPI_RBDR(x) (0x200 + ((x) * 4))
Ye Li007b6042019-08-14 11:31:36 +0000114
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530115#define QUADSPI_LUTKEY 0x300
116#define QUADSPI_LUTKEY_VALUE 0x5AF05AF0
117
118#define QUADSPI_LCKCR 0x304
119#define QUADSPI_LCKER_LOCK BIT(0)
120#define QUADSPI_LCKER_UNLOCK BIT(1)
121
122#define QUADSPI_LUT_BASE 0x310
123#define QUADSPI_LUT_OFFSET (SEQID_LUT * 4 * 4)
124#define QUADSPI_LUT_REG(idx) \
125 (QUADSPI_LUT_BASE + QUADSPI_LUT_OFFSET + (idx) * 4)
126
Ye Lid7e3c9a2020-06-09 00:59:06 -0700127#define QUADSPI_AHB_LUT_OFFSET (SEQID_LUT_AHB * 4 * 4)
128#define QUADSPI_AHB_LUT_REG(idx) \
129 (QUADSPI_LUT_BASE + QUADSPI_AHB_LUT_OFFSET + (idx) * 4)
130
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530131/* Instruction set for the LUT register */
132#define LUT_STOP 0
133#define LUT_CMD 1
134#define LUT_ADDR 2
135#define LUT_DUMMY 3
136#define LUT_MODE 4
137#define LUT_MODE2 5
138#define LUT_MODE4 6
139#define LUT_FSL_READ 7
140#define LUT_FSL_WRITE 8
141#define LUT_JMP_ON_CS 9
142#define LUT_ADDR_DDR 10
143#define LUT_MODE_DDR 11
144#define LUT_MODE2_DDR 12
145#define LUT_MODE4_DDR 13
146#define LUT_FSL_READ_DDR 14
147#define LUT_FSL_WRITE_DDR 15
148#define LUT_DATA_LEARN 16
149
150/*
151 * The PAD definitions for LUT register.
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800152 *
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530153 * The pad stands for the number of IO lines [0:3].
154 * For example, the quad read needs four IO lines,
155 * so you should use LUT_PAD(4).
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800156 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530157#define LUT_PAD(x) (fls(x) - 1)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800158
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530159/*
160 * Macro for constructing the LUT entries with the following
161 * register layout:
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800162 *
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530163 * ---------------------------------------------------
164 * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 |
165 * ---------------------------------------------------
166 */
167#define LUT_DEF(idx, ins, pad, opr) \
168 ((((ins) << 10) | ((pad) << 8) | (opr)) << (((idx) % 2) * 16))
169
170/* Controller needs driver to swap endianness */
171#define QUADSPI_QUIRK_SWAP_ENDIAN BIT(0)
172
173/* Controller needs 4x internal clock */
174#define QUADSPI_QUIRK_4X_INT_CLK BIT(1)
175
176/*
177 * TKT253890, the controller needs the driver to fill the txfifo with
178 * 16 bytes at least to trigger a data transfer, even though the extra
179 * data won't be transferred.
180 */
181#define QUADSPI_QUIRK_TKT253890 BIT(2)
182
183/* TKT245618, the controller cannot wake up from wait mode */
184#define QUADSPI_QUIRK_TKT245618 BIT(3)
185
186/*
187 * Controller adds QSPI_AMBA_BASE (base address of the mapped memory)
188 * internally. No need to add it when setting SFXXAD and SFAR registers
189 */
190#define QUADSPI_QUIRK_BASE_INTERNAL BIT(4)
191
192/*
193 * Controller uses TDH bits in register QUADSPI_FLSHCR.
194 * They need to be set in accordance with the DDR/SDR mode.
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800195 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530196#define QUADSPI_QUIRK_USE_TDH_SETTING BIT(5)
197
Ye Lid7e3c9a2020-06-09 00:59:06 -0700198/*
199 * Controller only has Two CS on flash A, no flash B port
200 */
201#define QUADSPI_QUIRK_SINGLE_BUS BIT(6)
202
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530203struct fsl_qspi_devtype_data {
204 unsigned int rxfifo;
205 unsigned int txfifo;
206 unsigned int ahb_buf_size;
207 unsigned int quirks;
208 bool little_endian;
Alison Wangc7410e32014-05-06 09:13:01 +0800209};
210
Ye Li007b6042019-08-14 11:31:36 +0000211static const struct fsl_qspi_devtype_data vybrid_data = {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530212 .rxfifo = SZ_128,
213 .txfifo = SZ_64,
214 .ahb_buf_size = SZ_1K,
215 .quirks = QUADSPI_QUIRK_SWAP_ENDIAN,
216 .little_endian = true,
Ye Li007b6042019-08-14 11:31:36 +0000217};
218
219static const struct fsl_qspi_devtype_data imx6sx_data = {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530220 .rxfifo = SZ_128,
221 .txfifo = SZ_512,
222 .ahb_buf_size = SZ_1K,
223 .quirks = QUADSPI_QUIRK_4X_INT_CLK | QUADSPI_QUIRK_TKT245618,
224 .little_endian = true,
Ye Li007b6042019-08-14 11:31:36 +0000225};
226
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530227static const struct fsl_qspi_devtype_data imx7d_data = {
228 .rxfifo = SZ_128,
229 .txfifo = SZ_512,
230 .ahb_buf_size = SZ_1K,
231 .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_4X_INT_CLK |
232 QUADSPI_QUIRK_USE_TDH_SETTING,
233 .little_endian = true,
Ye Li007b6042019-08-14 11:31:36 +0000234};
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800235
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530236static const struct fsl_qspi_devtype_data imx6ul_data = {
237 .rxfifo = SZ_128,
238 .txfifo = SZ_512,
239 .ahb_buf_size = SZ_1K,
240 .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_4X_INT_CLK |
241 QUADSPI_QUIRK_USE_TDH_SETTING,
242 .little_endian = true,
Ye Li57f67752019-08-14 11:31:40 +0000243};
244
Ye Lie4d39a02020-06-09 00:59:05 -0700245static const struct fsl_qspi_devtype_data imx7ulp_data = {
246 .rxfifo = SZ_64,
247 .txfifo = SZ_64,
248 .ahb_buf_size = SZ_128,
249 .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_4X_INT_CLK |
Ye Lid7e3c9a2020-06-09 00:59:06 -0700250 QUADSPI_QUIRK_USE_TDH_SETTING | QUADSPI_QUIRK_SINGLE_BUS,
Ye Lie4d39a02020-06-09 00:59:05 -0700251 .little_endian = true,
252};
253
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530254static const struct fsl_qspi_devtype_data ls1021a_data = {
255 .rxfifo = SZ_128,
256 .txfifo = SZ_64,
257 .ahb_buf_size = SZ_1K,
258 .quirks = 0,
259 .little_endian = false,
260};
261
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530262static const struct fsl_qspi_devtype_data ls2080a_data = {
263 .rxfifo = SZ_128,
264 .txfifo = SZ_64,
265 .ahb_buf_size = SZ_1K,
266 .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_BASE_INTERNAL,
267 .little_endian = true,
268};
269
270struct fsl_qspi {
271 struct udevice *dev;
272 void __iomem *iobase;
273 void __iomem *ahb_addr;
274 u32 memmap_phy;
Ye Lid7e3c9a2020-06-09 00:59:06 -0700275 u32 memmap_size;
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530276 const struct fsl_qspi_devtype_data *devtype_data;
277 int selected;
278};
279
280static inline int needs_swap_endian(struct fsl_qspi *q)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800281{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530282 return q->devtype_data->quirks & QUADSPI_QUIRK_SWAP_ENDIAN;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800283}
284
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530285static inline int needs_4x_clock(struct fsl_qspi *q)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800286{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530287 return q->devtype_data->quirks & QUADSPI_QUIRK_4X_INT_CLK;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800288}
Alison Wangc7410e32014-05-06 09:13:01 +0800289
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530290static inline int needs_fill_txfifo(struct fsl_qspi *q)
Rajat Srivastava234daec2018-03-22 13:30:55 +0530291{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530292 return q->devtype_data->quirks & QUADSPI_QUIRK_TKT253890;
Rajat Srivastava234daec2018-03-22 13:30:55 +0530293}
294
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530295static inline int needs_wakeup_wait_mode(struct fsl_qspi *q)
Alison Wangc7410e32014-05-06 09:13:01 +0800296{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530297 return q->devtype_data->quirks & QUADSPI_QUIRK_TKT245618;
Alison Wangc7410e32014-05-06 09:13:01 +0800298}
299
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530300static inline int needs_amba_base_offset(struct fsl_qspi *q)
Alison Wangc7410e32014-05-06 09:13:01 +0800301{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530302 return !(q->devtype_data->quirks & QUADSPI_QUIRK_BASE_INTERNAL);
303}
Alison Wangc7410e32014-05-06 09:13:01 +0800304
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530305static inline int needs_tdh_setting(struct fsl_qspi *q)
306{
307 return q->devtype_data->quirks & QUADSPI_QUIRK_USE_TDH_SETTING;
308}
Alison Wangc7410e32014-05-06 09:13:01 +0800309
Ye Lid7e3c9a2020-06-09 00:59:06 -0700310static inline int needs_single_bus(struct fsl_qspi *q)
311{
312 return q->devtype_data->quirks & QUADSPI_QUIRK_SINGLE_BUS;
313}
314
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530315/*
316 * An IC bug makes it necessary to rearrange the 32-bit data.
317 * Later chips, such as IMX6SLX, have fixed this bug.
318 */
319static inline u32 fsl_qspi_endian_xchg(struct fsl_qspi *q, u32 a)
320{
321 return needs_swap_endian(q) ? __swab32(a) : a;
322}
Alison Wangc7410e32014-05-06 09:13:01 +0800323
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530324/*
325 * R/W functions for big- or little-endian registers:
326 * The QSPI controller's endianness is independent of
327 * the CPU core's endianness. So far, although the CPU
328 * core is little-endian the QSPI controller can use
329 * big-endian or little-endian.
330 */
331static void qspi_writel(struct fsl_qspi *q, u32 val, void __iomem *addr)
332{
333 if (q->devtype_data->little_endian)
334 out_le32(addr, val);
Alison Wangc7410e32014-05-06 09:13:01 +0800335 else
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530336 out_be32(addr, val);
337}
Alison Wangc7410e32014-05-06 09:13:01 +0800338
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530339static u32 qspi_readl(struct fsl_qspi *q, void __iomem *addr)
340{
341 if (q->devtype_data->little_endian)
342 return in_le32(addr);
Alison Wangc7410e32014-05-06 09:13:01 +0800343
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530344 return in_be32(addr);
345}
Alison Wangc7410e32014-05-06 09:13:01 +0800346
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530347static int fsl_qspi_check_buswidth(struct fsl_qspi *q, u8 width)
348{
349 switch (width) {
350 case 1:
351 case 2:
352 case 4:
353 return 0;
354 }
Alison Wangc7410e32014-05-06 09:13:01 +0800355
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530356 return -ENOTSUPP;
357}
Alison Wangc7410e32014-05-06 09:13:01 +0800358
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530359static bool fsl_qspi_supports_op(struct spi_slave *slave,
360 const struct spi_mem_op *op)
361{
362 struct fsl_qspi *q = dev_get_priv(slave->dev->parent);
363 int ret;
Alison Wangc7410e32014-05-06 09:13:01 +0800364
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530365 ret = fsl_qspi_check_buswidth(q, op->cmd.buswidth);
Peng Fan3642a872014-12-31 11:01:39 +0800366
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530367 if (op->addr.nbytes)
368 ret |= fsl_qspi_check_buswidth(q, op->addr.buswidth);
Peng Fan3a344482015-01-04 17:07:14 +0800369
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530370 if (op->dummy.nbytes)
371 ret |= fsl_qspi_check_buswidth(q, op->dummy.buswidth);
Peng Fan3a344482015-01-04 17:07:14 +0800372
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530373 if (op->data.nbytes)
374 ret |= fsl_qspi_check_buswidth(q, op->data.buswidth);
Peng Fan3a344482015-01-04 17:07:14 +0800375
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530376 if (ret)
377 return false;
Yuan Yaod7193262016-03-15 14:36:42 +0800378
379 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530380 * The number of instructions needed for the op, needs
381 * to fit into a single LUT entry.
Yuan Yaod7193262016-03-15 14:36:42 +0800382 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530383 if (op->addr.nbytes +
384 (op->dummy.nbytes ? 1 : 0) +
385 (op->data.nbytes ? 1 : 0) > 6)
386 return false;
Yuan Yaod7193262016-03-15 14:36:42 +0800387
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530388 /* Max 64 dummy clock cycles supported */
389 if (op->dummy.nbytes &&
390 (op->dummy.nbytes * 8 / op->dummy.buswidth > 64))
391 return false;
Yuan Yaod7193262016-03-15 14:36:42 +0800392
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530393 /* Max data length, check controller limits and alignment */
394 if (op->data.dir == SPI_MEM_DATA_IN &&
395 (op->data.nbytes > q->devtype_data->ahb_buf_size ||
396 (op->data.nbytes > q->devtype_data->rxfifo - 4 &&
397 !IS_ALIGNED(op->data.nbytes, 8))))
398 return false;
399
400 if (op->data.dir == SPI_MEM_DATA_OUT &&
401 op->data.nbytes > q->devtype_data->txfifo)
402 return false;
403
Mathew McBride148dba42021-01-25 03:55:21 +0000404 return spi_mem_default_supports_op(slave, op);
Alison Wangc7410e32014-05-06 09:13:01 +0800405}
406
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530407static void fsl_qspi_prepare_lut(struct fsl_qspi *q,
408 const struct spi_mem_op *op)
Peng Fan1c5f9662015-01-08 10:40:20 +0800409{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530410 void __iomem *base = q->iobase;
411 u32 lutval[4] = {};
412 int lutidx = 1, i;
Peng Fan1c5f9662015-01-08 10:40:20 +0800413
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530414 lutval[0] |= LUT_DEF(0, LUT_CMD, LUT_PAD(op->cmd.buswidth),
415 op->cmd.opcode);
Peng Fan1c5f9662015-01-08 10:40:20 +0800416
Ye Lid7e3c9a2020-06-09 00:59:06 -0700417 if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) {
418 if (op->addr.nbytes) {
419 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_ADDR,
420 LUT_PAD(op->addr.buswidth),
421 (op->addr.nbytes == 4) ? 0x20 : 0x18);
422 lutidx++;
423 }
424 } else {
425 /*
426 * For some unknown reason, using LUT_ADDR doesn't work in some
427 * cases (at least with only one byte long addresses), so
428 * let's use LUT_MODE to write the address bytes one by one
429 */
430 for (i = 0; i < op->addr.nbytes; i++) {
431 u8 addrbyte = op->addr.val >> (8 * (op->addr.nbytes - i - 1));
Peng Fan1c5f9662015-01-08 10:40:20 +0800432
Ye Lid7e3c9a2020-06-09 00:59:06 -0700433 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_MODE,
434 LUT_PAD(op->addr.buswidth),
435 addrbyte);
436 lutidx++;
437 }
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530438 }
Peng Fan1c5f9662015-01-08 10:40:20 +0800439
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530440 if (op->dummy.nbytes) {
441 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_DUMMY,
442 LUT_PAD(op->dummy.buswidth),
443 op->dummy.nbytes * 8 /
444 op->dummy.buswidth);
445 lutidx++;
446 }
Peng Fan1c5f9662015-01-08 10:40:20 +0800447
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530448 if (op->data.nbytes) {
449 lutval[lutidx / 2] |= LUT_DEF(lutidx,
450 op->data.dir == SPI_MEM_DATA_IN ?
451 LUT_FSL_READ : LUT_FSL_WRITE,
452 LUT_PAD(op->data.buswidth),
453 0);
454 lutidx++;
455 }
Peng Fan1c5f9662015-01-08 10:40:20 +0800456
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530457 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_STOP, 0, 0);
Peng Fan1c5f9662015-01-08 10:40:20 +0800458
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530459 /* unlock LUT */
460 qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
461 qspi_writel(q, QUADSPI_LCKER_UNLOCK, q->iobase + QUADSPI_LCKCR);
Peng Fan1c5f9662015-01-08 10:40:20 +0800462
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530463 dev_dbg(q->dev, "CMD[%x] lutval[0:%x \t 1:%x \t 2:%x \t 3:%x]\n",
464 op->cmd.opcode, lutval[0], lutval[1], lutval[2], lutval[3]);
Peng Fan1c5f9662015-01-08 10:40:20 +0800465
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530466 /* fill LUT */
467 for (i = 0; i < ARRAY_SIZE(lutval); i++)
468 qspi_writel(q, lutval[i], base + QUADSPI_LUT_REG(i));
Ye Li416d2ec2019-08-14 11:31:27 +0000469
Ye Lid7e3c9a2020-06-09 00:59:06 -0700470 if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) {
471 if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN &&
472 op->addr.nbytes) {
473 for (i = 0; i < ARRAY_SIZE(lutval); i++)
474 qspi_writel(q, lutval[i], base + QUADSPI_AHB_LUT_REG(i));
475 }
476 }
477
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530478 /* lock LUT */
479 qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
480 qspi_writel(q, QUADSPI_LCKER_LOCK, q->iobase + QUADSPI_LCKCR);
Peng Fan1c5f9662015-01-08 10:40:20 +0800481}
482
483/*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530484 * If we have changed the content of the flash by writing or erasing, or if we
485 * read from flash with a different offset into the page buffer, we need to
486 * invalidate the AHB buffer. If we do not do so, we may read out the wrong
487 * data. The spec tells us reset the AHB domain and Serial Flash domain at
488 * the same time.
Peng Fan1c5f9662015-01-08 10:40:20 +0800489 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530490static void fsl_qspi_invalidate(struct fsl_qspi *q)
Peng Fan1c5f9662015-01-08 10:40:20 +0800491{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530492 u32 reg;
Peng Fan1c5f9662015-01-08 10:40:20 +0800493
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530494 reg = qspi_readl(q, q->iobase + QUADSPI_MCR);
495 reg |= QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK;
496 qspi_writel(q, reg, q->iobase + QUADSPI_MCR);
Peng Fan1c5f9662015-01-08 10:40:20 +0800497
498 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530499 * The minimum delay : 1 AHB + 2 SFCK clocks.
500 * Delay 1 us is enough.
Peng Fan1c5f9662015-01-08 10:40:20 +0800501 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530502 udelay(1);
Peng Fan1c5f9662015-01-08 10:40:20 +0800503
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530504 reg &= ~(QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK);
505 qspi_writel(q, reg, q->iobase + QUADSPI_MCR);
Peng Fan1c5f9662015-01-08 10:40:20 +0800506}
Peng Fan1c5f9662015-01-08 10:40:20 +0800507
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530508static void fsl_qspi_select_mem(struct fsl_qspi *q, struct spi_slave *slave)
Peng Fan3a344482015-01-04 17:07:14 +0800509{
Simon Glassb75b15b2020-12-03 16:55:23 -0700510 struct dm_spi_slave_plat *plat =
Simon Glass71fa5b42020-12-03 16:55:18 -0700511 dev_get_parent_plat(slave->dev);
Peng Fan3a344482015-01-04 17:07:14 +0800512
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530513 if (q->selected == plat->cs)
514 return;
Alexander Stein283eb4a2017-06-01 09:32:19 +0200515
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530516 q->selected = plat->cs;
517 fsl_qspi_invalidate(q);
Peng Fan3a344482015-01-04 17:07:14 +0800518}
Alison Wangc7410e32014-05-06 09:13:01 +0800519
Ye Lid7e3c9a2020-06-09 00:59:06 -0700520static u32 fsl_qspi_memsize_per_cs(struct fsl_qspi *q)
521{
522 if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) {
523 if (needs_single_bus(q))
524 return q->memmap_size / 2;
525 else
526 return q->memmap_size / 4;
527 } else {
528 return ALIGN(q->devtype_data->ahb_buf_size, 0x400);
529 }
530}
531
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530532static void fsl_qspi_read_ahb(struct fsl_qspi *q, const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800533{
Ye Lid7e3c9a2020-06-09 00:59:06 -0700534 void __iomem *ahb_read_addr = q->ahb_addr;
535
536 if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) {
537 if (op->addr.nbytes)
538 ahb_read_addr += op->addr.val;
539 }
540
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530541 memcpy_fromio(op->data.buf.in,
Ye Lid7e3c9a2020-06-09 00:59:06 -0700542 ahb_read_addr + q->selected * fsl_qspi_memsize_per_cs(q),
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530543 op->data.nbytes);
Alison Wangc7410e32014-05-06 09:13:01 +0800544}
545
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530546static void fsl_qspi_fill_txfifo(struct fsl_qspi *q,
547 const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800548{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530549 void __iomem *base = q->iobase;
550 int i;
551 u32 val;
Alison Wangc7410e32014-05-06 09:13:01 +0800552
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530553 for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 4); i += 4) {
554 memcpy(&val, op->data.buf.out + i, 4);
555 val = fsl_qspi_endian_xchg(q, val);
556 qspi_writel(q, val, base + QUADSPI_TBDR);
557 }
Alison Wangc7410e32014-05-06 09:13:01 +0800558
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530559 if (i < op->data.nbytes) {
560 memcpy(&val, op->data.buf.out + i, op->data.nbytes - i);
561 val = fsl_qspi_endian_xchg(q, val);
562 qspi_writel(q, val, base + QUADSPI_TBDR);
Alison Wangc7410e32014-05-06 09:13:01 +0800563 }
564
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530565 if (needs_fill_txfifo(q)) {
566 for (i = op->data.nbytes; i < 16; i += 4)
567 qspi_writel(q, 0, base + QUADSPI_TBDR);
568 }
Alison Wangc7410e32014-05-06 09:13:01 +0800569}
570
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530571static void fsl_qspi_read_rxfifo(struct fsl_qspi *q,
572 const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800573{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530574 void __iomem *base = q->iobase;
575 int i;
576 u8 *buf = op->data.buf.in;
577 u32 val;
Alison Wangc7410e32014-05-06 09:13:01 +0800578
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530579 for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 4); i += 4) {
580 val = qspi_readl(q, base + QUADSPI_RBDR(i / 4));
581 val = fsl_qspi_endian_xchg(q, val);
582 memcpy(buf + i, &val, 4);
Alison Wangc7410e32014-05-06 09:13:01 +0800583 }
584
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530585 if (i < op->data.nbytes) {
586 val = qspi_readl(q, base + QUADSPI_RBDR(i / 4));
587 val = fsl_qspi_endian_xchg(q, val);
588 memcpy(buf + i, &val, op->data.nbytes - i);
Alison Wangc7410e32014-05-06 09:13:01 +0800589 }
Alison Wangc7410e32014-05-06 09:13:01 +0800590}
591
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530592static int fsl_qspi_readl_poll_tout(struct fsl_qspi *q, void __iomem *base,
593 u32 mask, u32 delay_us, u32 timeout_us)
Alison Wangc7410e32014-05-06 09:13:01 +0800594{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530595 u32 reg;
Alexander Stein283eb4a2017-06-01 09:32:19 +0200596
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530597 if (!q->devtype_data->little_endian)
598 mask = (u32)cpu_to_be32(mask);
Alison Wangc7410e32014-05-06 09:13:01 +0800599
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530600 return readl_poll_timeout(base, reg, !(reg & mask), timeout_us);
Alison Wangc7410e32014-05-06 09:13:01 +0800601}
602
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530603static int fsl_qspi_do_op(struct fsl_qspi *q, const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800604{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530605 void __iomem *base = q->iobase;
606 int err = 0;
Alison Wangc7410e32014-05-06 09:13:01 +0800607
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530608 /*
609 * Always start the sequence at the same index since we update
610 * the LUT at each exec_op() call. And also specify the DATA
611 * length, since it's has not been specified in the LUT.
612 */
613 qspi_writel(q, op->data.nbytes | QUADSPI_IPCR_SEQID(SEQID_LUT),
614 base + QUADSPI_IPCR);
Alison Wangc7410e32014-05-06 09:13:01 +0800615
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530616 /* wait for the controller being ready */
617 err = fsl_qspi_readl_poll_tout(q, base + QUADSPI_SR,
618 (QUADSPI_SR_IP_ACC_MASK |
619 QUADSPI_SR_AHB_ACC_MASK),
620 10, 1000);
Alison Wangc7410e32014-05-06 09:13:01 +0800621
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530622 if (!err && op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN)
623 fsl_qspi_read_rxfifo(q, op);
Alison Wangc7410e32014-05-06 09:13:01 +0800624
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530625 return err;
Alison Wangc7410e32014-05-06 09:13:01 +0800626}
627
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530628static int fsl_qspi_exec_op(struct spi_slave *slave,
629 const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800630{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530631 struct fsl_qspi *q = dev_get_priv(slave->dev->parent);
632 void __iomem *base = q->iobase;
633 u32 addr_offset = 0;
634 int err = 0;
Alison Wangc7410e32014-05-06 09:13:01 +0800635
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530636 /* wait for the controller being ready */
637 fsl_qspi_readl_poll_tout(q, base + QUADSPI_SR, (QUADSPI_SR_IP_ACC_MASK |
638 QUADSPI_SR_AHB_ACC_MASK), 10, 1000);
Alexander Stein283eb4a2017-06-01 09:32:19 +0200639
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530640 fsl_qspi_select_mem(q, slave);
Alison Wangc7410e32014-05-06 09:13:01 +0800641
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530642 if (needs_amba_base_offset(q))
643 addr_offset = q->memmap_phy;
Alison Wangc7410e32014-05-06 09:13:01 +0800644
Ye Lid7e3c9a2020-06-09 00:59:06 -0700645 if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) {
646 if (op->addr.nbytes)
647 addr_offset += op->addr.val;
648 }
649
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530650 qspi_writel(q,
Ye Lid7e3c9a2020-06-09 00:59:06 -0700651 q->selected * fsl_qspi_memsize_per_cs(q) + addr_offset,
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530652 base + QUADSPI_SFAR);
Alison Wangc7410e32014-05-06 09:13:01 +0800653
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530654 qspi_writel(q, qspi_readl(q, base + QUADSPI_MCR) |
655 QUADSPI_MCR_CLR_RXF_MASK | QUADSPI_MCR_CLR_TXF_MASK,
656 base + QUADSPI_MCR);
Alison Wangc7410e32014-05-06 09:13:01 +0800657
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530658 qspi_writel(q, QUADSPI_SPTRCLR_BFPTRC | QUADSPI_SPTRCLR_IPPTRC,
659 base + QUADSPI_SPTRCLR);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800660
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530661 fsl_qspi_prepare_lut(q, op);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800662
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530663 /*
664 * If we have large chunks of data, we read them through the AHB bus
665 * by accessing the mapped memory. In all other cases we use
666 * IP commands to access the flash.
667 */
668 if (op->data.nbytes > (q->devtype_data->rxfifo - 4) &&
669 op->data.dir == SPI_MEM_DATA_IN) {
670 fsl_qspi_read_ahb(q, op);
671 } else {
672 qspi_writel(q, QUADSPI_RBCT_WMRK_MASK |
673 QUADSPI_RBCT_RXBRD_USEIPS, base + QUADSPI_RBCT);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800674
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530675 if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT)
676 fsl_qspi_fill_txfifo(q, op);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800677
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530678 err = fsl_qspi_do_op(q, op);
679 }
680
681 /* Invalidate the data in the AHB buffer. */
682 fsl_qspi_invalidate(q);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800683
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530684 return err;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800685}
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800686
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530687static int fsl_qspi_adjust_op_size(struct spi_slave *slave,
688 struct spi_mem_op *op)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800689{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530690 struct fsl_qspi *q = dev_get_priv(slave->dev->parent);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800691
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530692 if (op->data.dir == SPI_MEM_DATA_OUT) {
693 if (op->data.nbytes > q->devtype_data->txfifo)
694 op->data.nbytes = q->devtype_data->txfifo;
695 } else {
696 if (op->data.nbytes > q->devtype_data->ahb_buf_size)
697 op->data.nbytes = q->devtype_data->ahb_buf_size;
698 else if (op->data.nbytes > (q->devtype_data->rxfifo - 4))
699 op->data.nbytes = ALIGN_DOWN(op->data.nbytes, 8);
700 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800701
702 return 0;
703}
704
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530705static int fsl_qspi_default_setup(struct fsl_qspi *q)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800706{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530707 void __iomem *base = q->iobase;
Ye Lid7e3c9a2020-06-09 00:59:06 -0700708 u32 reg, addr_offset = 0, memsize_cs;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800709
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530710 /* Reset the module */
711 qspi_writel(q, QUADSPI_MCR_SWRSTSD_MASK | QUADSPI_MCR_SWRSTHD_MASK,
712 base + QUADSPI_MCR);
713 udelay(1);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800714
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530715 /* Disable the module */
716 qspi_writel(q, QUADSPI_MCR_MDIS_MASK | QUADSPI_MCR_RESERVED_MASK,
717 base + QUADSPI_MCR);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800718
Yuan Yaoae412392016-03-15 14:36:40 +0800719 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530720 * Previous boot stages (BootROM, bootloader) might have used DDR
721 * mode and did not clear the TDH bits. As we currently use SDR mode
722 * only, clear the TDH bits if necessary.
Yuan Yaoae412392016-03-15 14:36:40 +0800723 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530724 if (needs_tdh_setting(q))
725 qspi_writel(q, qspi_readl(q, base + QUADSPI_FLSHCR) &
726 ~QUADSPI_FLSHCR_TDH_MASK,
727 base + QUADSPI_FLSHCR);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800728
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530729 reg = qspi_readl(q, base + QUADSPI_SMPR);
730 qspi_writel(q, reg & ~(QUADSPI_SMPR_FSDLY_MASK
731 | QUADSPI_SMPR_FSPHS_MASK
732 | QUADSPI_SMPR_HSENA_MASK
733 | QUADSPI_SMPR_DDRSMP_MASK), base + QUADSPI_SMPR);
Ye Li007b6042019-08-14 11:31:36 +0000734
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530735 /* We only use the buffer3 for AHB read */
736 qspi_writel(q, 0, base + QUADSPI_BUF0IND);
737 qspi_writel(q, 0, base + QUADSPI_BUF1IND);
738 qspi_writel(q, 0, base + QUADSPI_BUF2IND);
Suresh Gupta4945b872017-08-30 20:06:33 +0530739
Ye Lid7e3c9a2020-06-09 00:59:06 -0700740 if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP))
741 qspi_writel(q, QUADSPI_BFGENCR_SEQID(SEQID_LUT_AHB),
742 q->iobase + QUADSPI_BFGENCR);
743 else
744 qspi_writel(q, QUADSPI_BFGENCR_SEQID(SEQID_LUT),
745 q->iobase + QUADSPI_BFGENCR);
746
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530747 qspi_writel(q, QUADSPI_RBCT_WMRK_MASK, base + QUADSPI_RBCT);
748 qspi_writel(q, QUADSPI_BUF3CR_ALLMST_MASK |
749 QUADSPI_BUF3CR_ADATSZ(q->devtype_data->ahb_buf_size / 8),
750 base + QUADSPI_BUF3CR);
Suresh Gupta4945b872017-08-30 20:06:33 +0530751
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530752 if (needs_amba_base_offset(q))
753 addr_offset = q->memmap_phy;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800754
Yuan Yaob4bfe102016-03-15 14:36:41 +0800755 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530756 * In HW there can be a maximum of four chips on two buses with
757 * two chip selects on each bus. We use four chip selects in SW
758 * to differentiate between the four chips.
759 * We use ahb_buf_size for each chip and set SFA1AD, SFA2AD, SFB1AD,
760 * SFB2AD accordingly.
Yuan Yaob4bfe102016-03-15 14:36:41 +0800761 */
Ye Lid7e3c9a2020-06-09 00:59:06 -0700762 memsize_cs = fsl_qspi_memsize_per_cs(q);
763 qspi_writel(q, memsize_cs + addr_offset,
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530764 base + QUADSPI_SFA1AD);
Ye Lid7e3c9a2020-06-09 00:59:06 -0700765 qspi_writel(q, memsize_cs * 2 + addr_offset,
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530766 base + QUADSPI_SFA2AD);
Ye Lid7e3c9a2020-06-09 00:59:06 -0700767 if (!needs_single_bus(q)) {
768 qspi_writel(q, memsize_cs * 3 + addr_offset,
769 base + QUADSPI_SFB1AD);
770 qspi_writel(q, memsize_cs * 4 + addr_offset,
771 base + QUADSPI_SFB2AD);
772 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800773
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530774 q->selected = -1;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800775
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530776 /* Enable the module */
777 qspi_writel(q, QUADSPI_MCR_RESERVED_MASK | QUADSPI_MCR_END_CFG_MASK,
778 base + QUADSPI_MCR);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800779 return 0;
780}
781
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530782static const struct spi_controller_mem_ops fsl_qspi_mem_ops = {
783 .adjust_op_size = fsl_qspi_adjust_op_size,
784 .supports_op = fsl_qspi_supports_op,
785 .exec_op = fsl_qspi_exec_op,
786};
787
788static int fsl_qspi_probe(struct udevice *bus)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800789{
Simon Glass95588622020-12-22 19:30:28 -0700790 struct dm_spi_bus *dm_bus = dev_get_uclass_priv(bus);
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530791 struct fsl_qspi *q = dev_get_priv(bus);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800792 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700793 int node = dev_of_offset(bus);
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530794 struct fdt_resource res;
795 int ret;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800796
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530797 q->dev = bus;
798 q->devtype_data = (struct fsl_qspi_devtype_data *)
799 dev_get_driver_data(bus);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800800
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530801 /* find the resources */
802 ret = fdt_get_named_resource(blob, node, "reg", "reg-names", "QuadSPI",
803 &res);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800804 if (ret) {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530805 dev_err(bus, "Can't get regs base addresses(ret = %d)!\n", ret);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800806 return -ENOMEM;
807 }
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530808
809 q->iobase = map_physmem(res.start, res.end - res.start, MAP_NOCACHE);
810
Yuan Yaoae412392016-03-15 14:36:40 +0800811 ret = fdt_get_named_resource(blob, node, "reg", "reg-names",
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530812 "QuadSPI-memory", &res);
Yuan Yaoae412392016-03-15 14:36:40 +0800813 if (ret) {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530814 dev_err(bus, "Can't get AMBA base addresses(ret = %d)!\n", ret);
Yuan Yaoae412392016-03-15 14:36:40 +0800815 return -ENOMEM;
816 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800817
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530818 q->ahb_addr = map_physmem(res.start, res.end - res.start, MAP_NOCACHE);
819 q->memmap_phy = res.start;
Ye Lid7e3c9a2020-06-09 00:59:06 -0700820 q->memmap_size = res.end - res.start;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800821
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530822 dm_bus->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
823 66000000);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800824
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530825 fsl_qspi_default_setup(q);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800826
827 return 0;
828}
829
830static int fsl_qspi_xfer(struct udevice *dev, unsigned int bitlen,
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530831 const void *dout, void *din, unsigned long flags)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800832{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530833 return 0;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800834}
835
836static int fsl_qspi_claim_bus(struct udevice *dev)
837{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800838 return 0;
839}
840
841static int fsl_qspi_release_bus(struct udevice *dev)
842{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800843 return 0;
844}
845
846static int fsl_qspi_set_speed(struct udevice *bus, uint speed)
847{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800848 return 0;
849}
850
851static int fsl_qspi_set_mode(struct udevice *bus, uint mode)
852{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800853 return 0;
854}
855
856static const struct dm_spi_ops fsl_qspi_ops = {
857 .claim_bus = fsl_qspi_claim_bus,
858 .release_bus = fsl_qspi_release_bus,
859 .xfer = fsl_qspi_xfer,
860 .set_speed = fsl_qspi_set_speed,
861 .set_mode = fsl_qspi_set_mode,
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530862 .mem_ops = &fsl_qspi_mem_ops,
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800863};
864
865static const struct udevice_id fsl_qspi_ids[] = {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530866 { .compatible = "fsl,vf610-qspi", .data = (ulong)&vybrid_data, },
867 { .compatible = "fsl,imx6sx-qspi", .data = (ulong)&imx6sx_data, },
868 { .compatible = "fsl,imx6ul-qspi", .data = (ulong)&imx6ul_data, },
869 { .compatible = "fsl,imx7d-qspi", .data = (ulong)&imx7d_data, },
Ye Lie4d39a02020-06-09 00:59:05 -0700870 { .compatible = "fsl,imx7ulp-qspi", .data = (ulong)&imx7ulp_data, },
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530871 { .compatible = "fsl,ls1021a-qspi", .data = (ulong)&ls1021a_data, },
Mathew McBrideb4c53962021-01-25 03:55:22 +0000872 { .compatible = "fsl,ls1088a-qspi", .data = (ulong)&ls2080a_data, },
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530873 { .compatible = "fsl,ls2080a-qspi", .data = (ulong)&ls2080a_data, },
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800874 { }
875};
876
877U_BOOT_DRIVER(fsl_qspi) = {
878 .name = "fsl_qspi",
879 .id = UCLASS_SPI,
880 .of_match = fsl_qspi_ids,
881 .ops = &fsl_qspi_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700882 .priv_auto = sizeof(struct fsl_qspi),
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800883 .probe = fsl_qspi_probe,
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800884};