blob: ee2c8b67dc91f6bf5bb46f7c02d33166324aa41e [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
26#include <common.h>
Alison Wangc7410e32014-05-06 09:13:01 +080027#include <asm/io.h>
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080028#include <dm.h>
Kuldeep Singhd8429a12020-02-20 22:57:52 +053029#include <linux/iopoll.h>
30#include <linux/sizes.h>
31#include <linux/err.h>
32#include <spi.h>
33#include <spi-mem.h>
Alison Wangc7410e32014-05-06 09:13:01 +080034
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080035DECLARE_GLOBAL_DATA_PTR;
36
Kuldeep Singhd8429a12020-02-20 22:57:52 +053037/*
38 * The driver only uses one single LUT entry, that is updated on
39 * each call of exec_op(). Index 0 is preset at boot with a basic
40 * read operation, so let's use the last entry (15).
41 */
42#define SEQID_LUT 15
Alison Wangc7410e32014-05-06 09:13:01 +080043
Kuldeep Singhd8429a12020-02-20 22:57:52 +053044/* Registers used by the driver */
45#define QUADSPI_MCR 0x00
46#define QUADSPI_MCR_RESERVED_MASK GENMASK(19, 16)
47#define QUADSPI_MCR_MDIS_MASK BIT(14)
48#define QUADSPI_MCR_CLR_TXF_MASK BIT(11)
49#define QUADSPI_MCR_CLR_RXF_MASK BIT(10)
50#define QUADSPI_MCR_DDR_EN_MASK BIT(7)
51#define QUADSPI_MCR_END_CFG_MASK GENMASK(3, 2)
52#define QUADSPI_MCR_SWRSTHD_MASK BIT(1)
53#define QUADSPI_MCR_SWRSTSD_MASK BIT(0)
Alison Wangc7410e32014-05-06 09:13:01 +080054
Kuldeep Singhd8429a12020-02-20 22:57:52 +053055#define QUADSPI_IPCR 0x08
56#define QUADSPI_IPCR_SEQID(x) ((x) << 24)
57#define QUADSPI_FLSHCR 0x0c
58#define QUADSPI_FLSHCR_TCSS_MASK GENMASK(3, 0)
59#define QUADSPI_FLSHCR_TCSH_MASK GENMASK(11, 8)
60#define QUADSPI_FLSHCR_TDH_MASK GENMASK(17, 16)
Alison Wangc7410e32014-05-06 09:13:01 +080061
Kuldeep Singhd8429a12020-02-20 22:57:52 +053062#define QUADSPI_BUF3CR 0x1c
63#define QUADSPI_BUF3CR_ALLMST_MASK BIT(31)
64#define QUADSPI_BUF3CR_ADATSZ(x) ((x) << 8)
65#define QUADSPI_BUF3CR_ADATSZ_MASK GENMASK(15, 8)
Alison Wangc7410e32014-05-06 09:13:01 +080066
Kuldeep Singhd8429a12020-02-20 22:57:52 +053067#define QUADSPI_BFGENCR 0x20
68#define QUADSPI_BFGENCR_SEQID(x) ((x) << 12)
Peng Fan3a344482015-01-04 17:07:14 +080069
Kuldeep Singhd8429a12020-02-20 22:57:52 +053070#define QUADSPI_BUF0IND 0x30
71#define QUADSPI_BUF1IND 0x34
72#define QUADSPI_BUF2IND 0x38
73#define QUADSPI_SFAR 0x100
Peng Fan3a344482015-01-04 17:07:14 +080074
Kuldeep Singhd8429a12020-02-20 22:57:52 +053075#define QUADSPI_SMPR 0x108
76#define QUADSPI_SMPR_DDRSMP_MASK GENMASK(18, 16)
77#define QUADSPI_SMPR_FSDLY_MASK BIT(6)
78#define QUADSPI_SMPR_FSPHS_MASK BIT(5)
79#define QUADSPI_SMPR_HSENA_MASK BIT(0)
Yuan Yaod7193262016-03-15 14:36:42 +080080
Kuldeep Singhd8429a12020-02-20 22:57:52 +053081#define QUADSPI_RBCT 0x110
82#define QUADSPI_RBCT_WMRK_MASK GENMASK(4, 0)
83#define QUADSPI_RBCT_RXBRD_USEIPS BIT(8)
Alison Wangc7410e32014-05-06 09:13:01 +080084
Kuldeep Singhd8429a12020-02-20 22:57:52 +053085#define QUADSPI_TBDR 0x154
Alison Wangc7410e32014-05-06 09:13:01 +080086
Kuldeep Singhd8429a12020-02-20 22:57:52 +053087#define QUADSPI_SR 0x15c
88#define QUADSPI_SR_IP_ACC_MASK BIT(1)
89#define QUADSPI_SR_AHB_ACC_MASK BIT(2)
Alison Wangc7410e32014-05-06 09:13:01 +080090
Kuldeep Singhd8429a12020-02-20 22:57:52 +053091#define QUADSPI_FR 0x160
92#define QUADSPI_FR_TFF_MASK BIT(0)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080093
Kuldeep Singhd8429a12020-02-20 22:57:52 +053094#define QUADSPI_RSER 0x164
95#define QUADSPI_RSER_TFIE BIT(0)
Ye Li007b6042019-08-14 11:31:36 +000096
Kuldeep Singhd8429a12020-02-20 22:57:52 +053097#define QUADSPI_SPTRCLR 0x16c
98#define QUADSPI_SPTRCLR_IPPTRC BIT(8)
99#define QUADSPI_SPTRCLR_BFPTRC BIT(0)
Ye Li007b6042019-08-14 11:31:36 +0000100
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530101#define QUADSPI_SFA1AD 0x180
102#define QUADSPI_SFA2AD 0x184
103#define QUADSPI_SFB1AD 0x188
104#define QUADSPI_SFB2AD 0x18c
105#define QUADSPI_RBDR(x) (0x200 + ((x) * 4))
Ye Li007b6042019-08-14 11:31:36 +0000106
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530107#define QUADSPI_LUTKEY 0x300
108#define QUADSPI_LUTKEY_VALUE 0x5AF05AF0
109
110#define QUADSPI_LCKCR 0x304
111#define QUADSPI_LCKER_LOCK BIT(0)
112#define QUADSPI_LCKER_UNLOCK BIT(1)
113
114#define QUADSPI_LUT_BASE 0x310
115#define QUADSPI_LUT_OFFSET (SEQID_LUT * 4 * 4)
116#define QUADSPI_LUT_REG(idx) \
117 (QUADSPI_LUT_BASE + QUADSPI_LUT_OFFSET + (idx) * 4)
118
119/* Instruction set for the LUT register */
120#define LUT_STOP 0
121#define LUT_CMD 1
122#define LUT_ADDR 2
123#define LUT_DUMMY 3
124#define LUT_MODE 4
125#define LUT_MODE2 5
126#define LUT_MODE4 6
127#define LUT_FSL_READ 7
128#define LUT_FSL_WRITE 8
129#define LUT_JMP_ON_CS 9
130#define LUT_ADDR_DDR 10
131#define LUT_MODE_DDR 11
132#define LUT_MODE2_DDR 12
133#define LUT_MODE4_DDR 13
134#define LUT_FSL_READ_DDR 14
135#define LUT_FSL_WRITE_DDR 15
136#define LUT_DATA_LEARN 16
137
138/*
139 * The PAD definitions for LUT register.
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800140 *
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530141 * The pad stands for the number of IO lines [0:3].
142 * For example, the quad read needs four IO lines,
143 * so you should use LUT_PAD(4).
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800144 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530145#define LUT_PAD(x) (fls(x) - 1)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800146
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530147/*
148 * Macro for constructing the LUT entries with the following
149 * register layout:
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800150 *
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530151 * ---------------------------------------------------
152 * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 |
153 * ---------------------------------------------------
154 */
155#define LUT_DEF(idx, ins, pad, opr) \
156 ((((ins) << 10) | ((pad) << 8) | (opr)) << (((idx) % 2) * 16))
157
158/* Controller needs driver to swap endianness */
159#define QUADSPI_QUIRK_SWAP_ENDIAN BIT(0)
160
161/* Controller needs 4x internal clock */
162#define QUADSPI_QUIRK_4X_INT_CLK BIT(1)
163
164/*
165 * TKT253890, the controller needs the driver to fill the txfifo with
166 * 16 bytes at least to trigger a data transfer, even though the extra
167 * data won't be transferred.
168 */
169#define QUADSPI_QUIRK_TKT253890 BIT(2)
170
171/* TKT245618, the controller cannot wake up from wait mode */
172#define QUADSPI_QUIRK_TKT245618 BIT(3)
173
174/*
175 * Controller adds QSPI_AMBA_BASE (base address of the mapped memory)
176 * internally. No need to add it when setting SFXXAD and SFAR registers
177 */
178#define QUADSPI_QUIRK_BASE_INTERNAL BIT(4)
179
180/*
181 * Controller uses TDH bits in register QUADSPI_FLSHCR.
182 * They need to be set in accordance with the DDR/SDR mode.
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800183 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530184#define QUADSPI_QUIRK_USE_TDH_SETTING BIT(5)
185
186struct fsl_qspi_devtype_data {
187 unsigned int rxfifo;
188 unsigned int txfifo;
189 unsigned int ahb_buf_size;
190 unsigned int quirks;
191 bool little_endian;
Alison Wangc7410e32014-05-06 09:13:01 +0800192};
193
Ye Li007b6042019-08-14 11:31:36 +0000194static const struct fsl_qspi_devtype_data vybrid_data = {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530195 .rxfifo = SZ_128,
196 .txfifo = SZ_64,
197 .ahb_buf_size = SZ_1K,
198 .quirks = QUADSPI_QUIRK_SWAP_ENDIAN,
199 .little_endian = true,
Ye Li007b6042019-08-14 11:31:36 +0000200};
201
202static const struct fsl_qspi_devtype_data imx6sx_data = {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530203 .rxfifo = SZ_128,
204 .txfifo = SZ_512,
205 .ahb_buf_size = SZ_1K,
206 .quirks = QUADSPI_QUIRK_4X_INT_CLK | QUADSPI_QUIRK_TKT245618,
207 .little_endian = true,
Ye Li007b6042019-08-14 11:31:36 +0000208};
209
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530210static const struct fsl_qspi_devtype_data imx7d_data = {
211 .rxfifo = SZ_128,
212 .txfifo = SZ_512,
213 .ahb_buf_size = SZ_1K,
214 .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_4X_INT_CLK |
215 QUADSPI_QUIRK_USE_TDH_SETTING,
216 .little_endian = true,
Ye Li007b6042019-08-14 11:31:36 +0000217};
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800218
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530219static const struct fsl_qspi_devtype_data imx6ul_data = {
220 .rxfifo = SZ_128,
221 .txfifo = SZ_512,
222 .ahb_buf_size = SZ_1K,
223 .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_4X_INT_CLK |
224 QUADSPI_QUIRK_USE_TDH_SETTING,
225 .little_endian = true,
Ye Li57f67752019-08-14 11:31:40 +0000226};
227
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530228static const struct fsl_qspi_devtype_data ls1021a_data = {
229 .rxfifo = SZ_128,
230 .txfifo = SZ_64,
231 .ahb_buf_size = SZ_1K,
232 .quirks = 0,
233 .little_endian = false,
234};
235
236static const struct fsl_qspi_devtype_data ls1088a_data = {
237 .rxfifo = SZ_128,
238 .txfifo = SZ_128,
239 .ahb_buf_size = SZ_1K,
240 .quirks = QUADSPI_QUIRK_TKT253890,
241 .little_endian = true,
242};
243
244static const struct fsl_qspi_devtype_data ls2080a_data = {
245 .rxfifo = SZ_128,
246 .txfifo = SZ_64,
247 .ahb_buf_size = SZ_1K,
248 .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_BASE_INTERNAL,
249 .little_endian = true,
250};
251
252struct fsl_qspi {
253 struct udevice *dev;
254 void __iomem *iobase;
255 void __iomem *ahb_addr;
256 u32 memmap_phy;
257 const struct fsl_qspi_devtype_data *devtype_data;
258 int selected;
259};
260
261static inline int needs_swap_endian(struct fsl_qspi *q)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800262{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530263 return q->devtype_data->quirks & QUADSPI_QUIRK_SWAP_ENDIAN;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800264}
265
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530266static inline int needs_4x_clock(struct fsl_qspi *q)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800267{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530268 return q->devtype_data->quirks & QUADSPI_QUIRK_4X_INT_CLK;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800269}
Alison Wangc7410e32014-05-06 09:13:01 +0800270
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530271static inline int needs_fill_txfifo(struct fsl_qspi *q)
Rajat Srivastava234daec2018-03-22 13:30:55 +0530272{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530273 return q->devtype_data->quirks & QUADSPI_QUIRK_TKT253890;
Rajat Srivastava234daec2018-03-22 13:30:55 +0530274}
275
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530276static inline int needs_wakeup_wait_mode(struct fsl_qspi *q)
Alison Wangc7410e32014-05-06 09:13:01 +0800277{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530278 return q->devtype_data->quirks & QUADSPI_QUIRK_TKT245618;
Alison Wangc7410e32014-05-06 09:13:01 +0800279}
280
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530281static inline int needs_amba_base_offset(struct fsl_qspi *q)
Alison Wangc7410e32014-05-06 09:13:01 +0800282{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530283 return !(q->devtype_data->quirks & QUADSPI_QUIRK_BASE_INTERNAL);
284}
Alison Wangc7410e32014-05-06 09:13:01 +0800285
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530286static inline int needs_tdh_setting(struct fsl_qspi *q)
287{
288 return q->devtype_data->quirks & QUADSPI_QUIRK_USE_TDH_SETTING;
289}
Alison Wangc7410e32014-05-06 09:13:01 +0800290
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530291/*
292 * An IC bug makes it necessary to rearrange the 32-bit data.
293 * Later chips, such as IMX6SLX, have fixed this bug.
294 */
295static inline u32 fsl_qspi_endian_xchg(struct fsl_qspi *q, u32 a)
296{
297 return needs_swap_endian(q) ? __swab32(a) : a;
298}
Alison Wangc7410e32014-05-06 09:13:01 +0800299
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530300/*
301 * R/W functions for big- or little-endian registers:
302 * The QSPI controller's endianness is independent of
303 * the CPU core's endianness. So far, although the CPU
304 * core is little-endian the QSPI controller can use
305 * big-endian or little-endian.
306 */
307static void qspi_writel(struct fsl_qspi *q, u32 val, void __iomem *addr)
308{
309 if (q->devtype_data->little_endian)
310 out_le32(addr, val);
Alison Wangc7410e32014-05-06 09:13:01 +0800311 else
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530312 out_be32(addr, val);
313}
Alison Wangc7410e32014-05-06 09:13:01 +0800314
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530315static u32 qspi_readl(struct fsl_qspi *q, void __iomem *addr)
316{
317 if (q->devtype_data->little_endian)
318 return in_le32(addr);
Alison Wangc7410e32014-05-06 09:13:01 +0800319
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530320 return in_be32(addr);
321}
Alison Wangc7410e32014-05-06 09:13:01 +0800322
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530323static int fsl_qspi_check_buswidth(struct fsl_qspi *q, u8 width)
324{
325 switch (width) {
326 case 1:
327 case 2:
328 case 4:
329 return 0;
330 }
Alison Wangc7410e32014-05-06 09:13:01 +0800331
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530332 return -ENOTSUPP;
333}
Alison Wangc7410e32014-05-06 09:13:01 +0800334
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530335static bool fsl_qspi_supports_op(struct spi_slave *slave,
336 const struct spi_mem_op *op)
337{
338 struct fsl_qspi *q = dev_get_priv(slave->dev->parent);
339 int ret;
Alison Wangc7410e32014-05-06 09:13:01 +0800340
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530341 ret = fsl_qspi_check_buswidth(q, op->cmd.buswidth);
Peng Fan3642a872014-12-31 11:01:39 +0800342
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530343 if (op->addr.nbytes)
344 ret |= fsl_qspi_check_buswidth(q, op->addr.buswidth);
Peng Fan3a344482015-01-04 17:07:14 +0800345
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530346 if (op->dummy.nbytes)
347 ret |= fsl_qspi_check_buswidth(q, op->dummy.buswidth);
Peng Fan3a344482015-01-04 17:07:14 +0800348
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530349 if (op->data.nbytes)
350 ret |= fsl_qspi_check_buswidth(q, op->data.buswidth);
Peng Fan3a344482015-01-04 17:07:14 +0800351
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530352 if (ret)
353 return false;
Yuan Yaod7193262016-03-15 14:36:42 +0800354
355 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530356 * The number of instructions needed for the op, needs
357 * to fit into a single LUT entry.
Yuan Yaod7193262016-03-15 14:36:42 +0800358 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530359 if (op->addr.nbytes +
360 (op->dummy.nbytes ? 1 : 0) +
361 (op->data.nbytes ? 1 : 0) > 6)
362 return false;
Yuan Yaod7193262016-03-15 14:36:42 +0800363
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530364 /* Max 64 dummy clock cycles supported */
365 if (op->dummy.nbytes &&
366 (op->dummy.nbytes * 8 / op->dummy.buswidth > 64))
367 return false;
Yuan Yaod7193262016-03-15 14:36:42 +0800368
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530369 /* Max data length, check controller limits and alignment */
370 if (op->data.dir == SPI_MEM_DATA_IN &&
371 (op->data.nbytes > q->devtype_data->ahb_buf_size ||
372 (op->data.nbytes > q->devtype_data->rxfifo - 4 &&
373 !IS_ALIGNED(op->data.nbytes, 8))))
374 return false;
375
376 if (op->data.dir == SPI_MEM_DATA_OUT &&
377 op->data.nbytes > q->devtype_data->txfifo)
378 return false;
379
380 return true;
Alison Wangc7410e32014-05-06 09:13:01 +0800381}
382
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530383static void fsl_qspi_prepare_lut(struct fsl_qspi *q,
384 const struct spi_mem_op *op)
Peng Fan1c5f9662015-01-08 10:40:20 +0800385{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530386 void __iomem *base = q->iobase;
387 u32 lutval[4] = {};
388 int lutidx = 1, i;
Peng Fan1c5f9662015-01-08 10:40:20 +0800389
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530390 lutval[0] |= LUT_DEF(0, LUT_CMD, LUT_PAD(op->cmd.buswidth),
391 op->cmd.opcode);
Peng Fan1c5f9662015-01-08 10:40:20 +0800392
393 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530394 * For some unknown reason, using LUT_ADDR doesn't work in some
395 * cases (at least with only one byte long addresses), so
396 * let's use LUT_MODE to write the address bytes one by one
Peng Fan1c5f9662015-01-08 10:40:20 +0800397 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530398 for (i = 0; i < op->addr.nbytes; i++) {
399 u8 addrbyte = op->addr.val >> (8 * (op->addr.nbytes - i - 1));
Peng Fan1c5f9662015-01-08 10:40:20 +0800400
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530401 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_MODE,
402 LUT_PAD(op->addr.buswidth),
403 addrbyte);
404 lutidx++;
405 }
Peng Fan1c5f9662015-01-08 10:40:20 +0800406
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530407 if (op->dummy.nbytes) {
408 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_DUMMY,
409 LUT_PAD(op->dummy.buswidth),
410 op->dummy.nbytes * 8 /
411 op->dummy.buswidth);
412 lutidx++;
413 }
Peng Fan1c5f9662015-01-08 10:40:20 +0800414
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530415 if (op->data.nbytes) {
416 lutval[lutidx / 2] |= LUT_DEF(lutidx,
417 op->data.dir == SPI_MEM_DATA_IN ?
418 LUT_FSL_READ : LUT_FSL_WRITE,
419 LUT_PAD(op->data.buswidth),
420 0);
421 lutidx++;
422 }
Peng Fan1c5f9662015-01-08 10:40:20 +0800423
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530424 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_STOP, 0, 0);
Peng Fan1c5f9662015-01-08 10:40:20 +0800425
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530426 /* unlock LUT */
427 qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
428 qspi_writel(q, QUADSPI_LCKER_UNLOCK, q->iobase + QUADSPI_LCKCR);
Peng Fan1c5f9662015-01-08 10:40:20 +0800429
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530430 dev_dbg(q->dev, "CMD[%x] lutval[0:%x \t 1:%x \t 2:%x \t 3:%x]\n",
431 op->cmd.opcode, lutval[0], lutval[1], lutval[2], lutval[3]);
Peng Fan1c5f9662015-01-08 10:40:20 +0800432
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530433 /* fill LUT */
434 for (i = 0; i < ARRAY_SIZE(lutval); i++)
435 qspi_writel(q, lutval[i], base + QUADSPI_LUT_REG(i));
Ye Li416d2ec2019-08-14 11:31:27 +0000436
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530437 /* lock LUT */
438 qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
439 qspi_writel(q, QUADSPI_LCKER_LOCK, q->iobase + QUADSPI_LCKCR);
Peng Fan1c5f9662015-01-08 10:40:20 +0800440}
441
442/*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530443 * If we have changed the content of the flash by writing or erasing, or if we
444 * read from flash with a different offset into the page buffer, we need to
445 * invalidate the AHB buffer. If we do not do so, we may read out the wrong
446 * data. The spec tells us reset the AHB domain and Serial Flash domain at
447 * the same time.
Peng Fan1c5f9662015-01-08 10:40:20 +0800448 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530449static void fsl_qspi_invalidate(struct fsl_qspi *q)
Peng Fan1c5f9662015-01-08 10:40:20 +0800450{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530451 u32 reg;
Peng Fan1c5f9662015-01-08 10:40:20 +0800452
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530453 reg = qspi_readl(q, q->iobase + QUADSPI_MCR);
454 reg |= QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK;
455 qspi_writel(q, reg, q->iobase + QUADSPI_MCR);
Peng Fan1c5f9662015-01-08 10:40:20 +0800456
457 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530458 * The minimum delay : 1 AHB + 2 SFCK clocks.
459 * Delay 1 us is enough.
Peng Fan1c5f9662015-01-08 10:40:20 +0800460 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530461 udelay(1);
Peng Fan1c5f9662015-01-08 10:40:20 +0800462
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530463 reg &= ~(QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK);
464 qspi_writel(q, reg, q->iobase + QUADSPI_MCR);
Peng Fan1c5f9662015-01-08 10:40:20 +0800465}
Peng Fan1c5f9662015-01-08 10:40:20 +0800466
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530467static void fsl_qspi_select_mem(struct fsl_qspi *q, struct spi_slave *slave)
Peng Fan3a344482015-01-04 17:07:14 +0800468{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530469 struct dm_spi_slave_platdata *plat =
470 dev_get_parent_platdata(slave->dev);
Peng Fan3a344482015-01-04 17:07:14 +0800471
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530472 if (q->selected == plat->cs)
473 return;
Alexander Stein283eb4a2017-06-01 09:32:19 +0200474
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530475 q->selected = plat->cs;
476 fsl_qspi_invalidate(q);
Peng Fan3a344482015-01-04 17:07:14 +0800477}
Alison Wangc7410e32014-05-06 09:13:01 +0800478
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530479static void fsl_qspi_read_ahb(struct fsl_qspi *q, const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800480{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530481 memcpy_fromio(op->data.buf.in,
482 q->ahb_addr + q->selected * q->devtype_data->ahb_buf_size,
483 op->data.nbytes);
Alison Wangc7410e32014-05-06 09:13:01 +0800484}
485
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530486static void fsl_qspi_fill_txfifo(struct fsl_qspi *q,
487 const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800488{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530489 void __iomem *base = q->iobase;
490 int i;
491 u32 val;
Alison Wangc7410e32014-05-06 09:13:01 +0800492
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530493 for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 4); i += 4) {
494 memcpy(&val, op->data.buf.out + i, 4);
495 val = fsl_qspi_endian_xchg(q, val);
496 qspi_writel(q, val, base + QUADSPI_TBDR);
497 }
Alison Wangc7410e32014-05-06 09:13:01 +0800498
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530499 if (i < op->data.nbytes) {
500 memcpy(&val, op->data.buf.out + i, op->data.nbytes - i);
501 val = fsl_qspi_endian_xchg(q, val);
502 qspi_writel(q, val, base + QUADSPI_TBDR);
Alison Wangc7410e32014-05-06 09:13:01 +0800503 }
504
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530505 if (needs_fill_txfifo(q)) {
506 for (i = op->data.nbytes; i < 16; i += 4)
507 qspi_writel(q, 0, base + QUADSPI_TBDR);
508 }
Alison Wangc7410e32014-05-06 09:13:01 +0800509}
510
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530511static void fsl_qspi_read_rxfifo(struct fsl_qspi *q,
512 const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800513{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530514 void __iomem *base = q->iobase;
515 int i;
516 u8 *buf = op->data.buf.in;
517 u32 val;
Alison Wangc7410e32014-05-06 09:13:01 +0800518
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530519 for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 4); i += 4) {
520 val = qspi_readl(q, base + QUADSPI_RBDR(i / 4));
521 val = fsl_qspi_endian_xchg(q, val);
522 memcpy(buf + i, &val, 4);
Alison Wangc7410e32014-05-06 09:13:01 +0800523 }
524
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530525 if (i < op->data.nbytes) {
526 val = qspi_readl(q, base + QUADSPI_RBDR(i / 4));
527 val = fsl_qspi_endian_xchg(q, val);
528 memcpy(buf + i, &val, op->data.nbytes - i);
Alison Wangc7410e32014-05-06 09:13:01 +0800529 }
Alison Wangc7410e32014-05-06 09:13:01 +0800530}
531
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530532static int fsl_qspi_readl_poll_tout(struct fsl_qspi *q, void __iomem *base,
533 u32 mask, u32 delay_us, u32 timeout_us)
Alison Wangc7410e32014-05-06 09:13:01 +0800534{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530535 u32 reg;
Alexander Stein283eb4a2017-06-01 09:32:19 +0200536
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530537 if (!q->devtype_data->little_endian)
538 mask = (u32)cpu_to_be32(mask);
Alison Wangc7410e32014-05-06 09:13:01 +0800539
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530540 return readl_poll_timeout(base, reg, !(reg & mask), timeout_us);
Alison Wangc7410e32014-05-06 09:13:01 +0800541}
542
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530543static int fsl_qspi_do_op(struct fsl_qspi *q, const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800544{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530545 void __iomem *base = q->iobase;
546 int err = 0;
Alison Wangc7410e32014-05-06 09:13:01 +0800547
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530548 /*
549 * Always start the sequence at the same index since we update
550 * the LUT at each exec_op() call. And also specify the DATA
551 * length, since it's has not been specified in the LUT.
552 */
553 qspi_writel(q, op->data.nbytes | QUADSPI_IPCR_SEQID(SEQID_LUT),
554 base + QUADSPI_IPCR);
Alison Wangc7410e32014-05-06 09:13:01 +0800555
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530556 /* wait for the controller being ready */
557 err = fsl_qspi_readl_poll_tout(q, base + QUADSPI_SR,
558 (QUADSPI_SR_IP_ACC_MASK |
559 QUADSPI_SR_AHB_ACC_MASK),
560 10, 1000);
Alison Wangc7410e32014-05-06 09:13:01 +0800561
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530562 if (!err && op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN)
563 fsl_qspi_read_rxfifo(q, op);
Alison Wangc7410e32014-05-06 09:13:01 +0800564
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530565 return err;
Alison Wangc7410e32014-05-06 09:13:01 +0800566}
567
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530568static int fsl_qspi_exec_op(struct spi_slave *slave,
569 const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800570{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530571 struct fsl_qspi *q = dev_get_priv(slave->dev->parent);
572 void __iomem *base = q->iobase;
573 u32 addr_offset = 0;
574 int err = 0;
Alison Wangc7410e32014-05-06 09:13:01 +0800575
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530576 /* wait for the controller being ready */
577 fsl_qspi_readl_poll_tout(q, base + QUADSPI_SR, (QUADSPI_SR_IP_ACC_MASK |
578 QUADSPI_SR_AHB_ACC_MASK), 10, 1000);
Alexander Stein283eb4a2017-06-01 09:32:19 +0200579
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530580 fsl_qspi_select_mem(q, slave);
Alison Wangc7410e32014-05-06 09:13:01 +0800581
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530582 if (needs_amba_base_offset(q))
583 addr_offset = q->memmap_phy;
Alison Wangc7410e32014-05-06 09:13:01 +0800584
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530585 qspi_writel(q,
586 q->selected * q->devtype_data->ahb_buf_size + addr_offset,
587 base + QUADSPI_SFAR);
Alison Wangc7410e32014-05-06 09:13:01 +0800588
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530589 qspi_writel(q, qspi_readl(q, base + QUADSPI_MCR) |
590 QUADSPI_MCR_CLR_RXF_MASK | QUADSPI_MCR_CLR_TXF_MASK,
591 base + QUADSPI_MCR);
Alison Wangc7410e32014-05-06 09:13:01 +0800592
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530593 qspi_writel(q, QUADSPI_SPTRCLR_BFPTRC | QUADSPI_SPTRCLR_IPPTRC,
594 base + QUADSPI_SPTRCLR);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800595
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530596 fsl_qspi_prepare_lut(q, op);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800597
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530598 /*
599 * If we have large chunks of data, we read them through the AHB bus
600 * by accessing the mapped memory. In all other cases we use
601 * IP commands to access the flash.
602 */
603 if (op->data.nbytes > (q->devtype_data->rxfifo - 4) &&
604 op->data.dir == SPI_MEM_DATA_IN) {
605 fsl_qspi_read_ahb(q, op);
606 } else {
607 qspi_writel(q, QUADSPI_RBCT_WMRK_MASK |
608 QUADSPI_RBCT_RXBRD_USEIPS, base + QUADSPI_RBCT);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800609
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530610 if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT)
611 fsl_qspi_fill_txfifo(q, op);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800612
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530613 err = fsl_qspi_do_op(q, op);
614 }
615
616 /* Invalidate the data in the AHB buffer. */
617 fsl_qspi_invalidate(q);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800618
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530619 return err;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800620}
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800621
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530622static int fsl_qspi_adjust_op_size(struct spi_slave *slave,
623 struct spi_mem_op *op)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800624{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530625 struct fsl_qspi *q = dev_get_priv(slave->dev->parent);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800626
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530627 if (op->data.dir == SPI_MEM_DATA_OUT) {
628 if (op->data.nbytes > q->devtype_data->txfifo)
629 op->data.nbytes = q->devtype_data->txfifo;
630 } else {
631 if (op->data.nbytes > q->devtype_data->ahb_buf_size)
632 op->data.nbytes = q->devtype_data->ahb_buf_size;
633 else if (op->data.nbytes > (q->devtype_data->rxfifo - 4))
634 op->data.nbytes = ALIGN_DOWN(op->data.nbytes, 8);
635 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800636
637 return 0;
638}
639
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530640static int fsl_qspi_default_setup(struct fsl_qspi *q)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800641{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530642 void __iomem *base = q->iobase;
643 u32 reg, addr_offset = 0;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800644
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530645 /* Reset the module */
646 qspi_writel(q, QUADSPI_MCR_SWRSTSD_MASK | QUADSPI_MCR_SWRSTHD_MASK,
647 base + QUADSPI_MCR);
648 udelay(1);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800649
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530650 /* Disable the module */
651 qspi_writel(q, QUADSPI_MCR_MDIS_MASK | QUADSPI_MCR_RESERVED_MASK,
652 base + QUADSPI_MCR);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800653
Yuan Yaoae412392016-03-15 14:36:40 +0800654 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530655 * Previous boot stages (BootROM, bootloader) might have used DDR
656 * mode and did not clear the TDH bits. As we currently use SDR mode
657 * only, clear the TDH bits if necessary.
Yuan Yaoae412392016-03-15 14:36:40 +0800658 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530659 if (needs_tdh_setting(q))
660 qspi_writel(q, qspi_readl(q, base + QUADSPI_FLSHCR) &
661 ~QUADSPI_FLSHCR_TDH_MASK,
662 base + QUADSPI_FLSHCR);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800663
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530664 reg = qspi_readl(q, base + QUADSPI_SMPR);
665 qspi_writel(q, reg & ~(QUADSPI_SMPR_FSDLY_MASK
666 | QUADSPI_SMPR_FSPHS_MASK
667 | QUADSPI_SMPR_HSENA_MASK
668 | QUADSPI_SMPR_DDRSMP_MASK), base + QUADSPI_SMPR);
Ye Li007b6042019-08-14 11:31:36 +0000669
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530670 /* We only use the buffer3 for AHB read */
671 qspi_writel(q, 0, base + QUADSPI_BUF0IND);
672 qspi_writel(q, 0, base + QUADSPI_BUF1IND);
673 qspi_writel(q, 0, base + QUADSPI_BUF2IND);
Suresh Gupta4945b872017-08-30 20:06:33 +0530674
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530675 qspi_writel(q, QUADSPI_BFGENCR_SEQID(SEQID_LUT),
676 q->iobase + QUADSPI_BFGENCR);
677 qspi_writel(q, QUADSPI_RBCT_WMRK_MASK, base + QUADSPI_RBCT);
678 qspi_writel(q, QUADSPI_BUF3CR_ALLMST_MASK |
679 QUADSPI_BUF3CR_ADATSZ(q->devtype_data->ahb_buf_size / 8),
680 base + QUADSPI_BUF3CR);
Suresh Gupta4945b872017-08-30 20:06:33 +0530681
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530682 if (needs_amba_base_offset(q))
683 addr_offset = q->memmap_phy;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800684
Yuan Yaob4bfe102016-03-15 14:36:41 +0800685 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530686 * In HW there can be a maximum of four chips on two buses with
687 * two chip selects on each bus. We use four chip selects in SW
688 * to differentiate between the four chips.
689 * We use ahb_buf_size for each chip and set SFA1AD, SFA2AD, SFB1AD,
690 * SFB2AD accordingly.
Yuan Yaob4bfe102016-03-15 14:36:41 +0800691 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530692 qspi_writel(q, q->devtype_data->ahb_buf_size + addr_offset,
693 base + QUADSPI_SFA1AD);
694 qspi_writel(q, q->devtype_data->ahb_buf_size * 2 + addr_offset,
695 base + QUADSPI_SFA2AD);
696 qspi_writel(q, q->devtype_data->ahb_buf_size * 3 + addr_offset,
697 base + QUADSPI_SFB1AD);
698 qspi_writel(q, q->devtype_data->ahb_buf_size * 4 + addr_offset,
699 base + QUADSPI_SFB2AD);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800700
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530701 q->selected = -1;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800702
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530703 /* Enable the module */
704 qspi_writel(q, QUADSPI_MCR_RESERVED_MASK | QUADSPI_MCR_END_CFG_MASK,
705 base + QUADSPI_MCR);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800706 return 0;
707}
708
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530709static const struct spi_controller_mem_ops fsl_qspi_mem_ops = {
710 .adjust_op_size = fsl_qspi_adjust_op_size,
711 .supports_op = fsl_qspi_supports_op,
712 .exec_op = fsl_qspi_exec_op,
713};
714
715static int fsl_qspi_probe(struct udevice *bus)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800716{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530717 struct dm_spi_bus *dm_bus = bus->uclass_priv;
718 struct fsl_qspi *q = dev_get_priv(bus);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800719 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700720 int node = dev_of_offset(bus);
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530721 struct fdt_resource res;
722 int ret;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800723
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530724 q->dev = bus;
725 q->devtype_data = (struct fsl_qspi_devtype_data *)
726 dev_get_driver_data(bus);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800727
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530728 /* find the resources */
729 ret = fdt_get_named_resource(blob, node, "reg", "reg-names", "QuadSPI",
730 &res);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800731 if (ret) {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530732 dev_err(bus, "Can't get regs base addresses(ret = %d)!\n", ret);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800733 return -ENOMEM;
734 }
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530735
736 q->iobase = map_physmem(res.start, res.end - res.start, MAP_NOCACHE);
737
Yuan Yaoae412392016-03-15 14:36:40 +0800738 ret = fdt_get_named_resource(blob, node, "reg", "reg-names",
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530739 "QuadSPI-memory", &res);
Yuan Yaoae412392016-03-15 14:36:40 +0800740 if (ret) {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530741 dev_err(bus, "Can't get AMBA base addresses(ret = %d)!\n", ret);
Yuan Yaoae412392016-03-15 14:36:40 +0800742 return -ENOMEM;
743 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800744
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530745 q->ahb_addr = map_physmem(res.start, res.end - res.start, MAP_NOCACHE);
746 q->memmap_phy = res.start;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800747
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530748 dm_bus->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
749 66000000);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800750
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530751 fsl_qspi_default_setup(q);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800752
753 return 0;
754}
755
756static int fsl_qspi_xfer(struct udevice *dev, unsigned int bitlen,
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530757 const void *dout, void *din, unsigned long flags)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800758{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530759 return 0;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800760}
761
762static int fsl_qspi_claim_bus(struct udevice *dev)
763{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800764 return 0;
765}
766
767static int fsl_qspi_release_bus(struct udevice *dev)
768{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800769 return 0;
770}
771
772static int fsl_qspi_set_speed(struct udevice *bus, uint speed)
773{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800774 return 0;
775}
776
777static int fsl_qspi_set_mode(struct udevice *bus, uint mode)
778{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800779 return 0;
780}
781
782static const struct dm_spi_ops fsl_qspi_ops = {
783 .claim_bus = fsl_qspi_claim_bus,
784 .release_bus = fsl_qspi_release_bus,
785 .xfer = fsl_qspi_xfer,
786 .set_speed = fsl_qspi_set_speed,
787 .set_mode = fsl_qspi_set_mode,
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530788 .mem_ops = &fsl_qspi_mem_ops,
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800789};
790
791static const struct udevice_id fsl_qspi_ids[] = {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530792 { .compatible = "fsl,vf610-qspi", .data = (ulong)&vybrid_data, },
793 { .compatible = "fsl,imx6sx-qspi", .data = (ulong)&imx6sx_data, },
794 { .compatible = "fsl,imx6ul-qspi", .data = (ulong)&imx6ul_data, },
795 { .compatible = "fsl,imx7d-qspi", .data = (ulong)&imx7d_data, },
796 { .compatible = "fsl,ls1021a-qspi", .data = (ulong)&ls1021a_data, },
797 { .compatible = "fsl,ls1088a-qspi", .data = (ulong)&ls1088a_data, },
798 { .compatible = "fsl,ls2080a-qspi", .data = (ulong)&ls2080a_data, },
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800799 { }
800};
801
802U_BOOT_DRIVER(fsl_qspi) = {
803 .name = "fsl_qspi",
804 .id = UCLASS_SPI,
805 .of_match = fsl_qspi_ids,
806 .ops = &fsl_qspi_ops,
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530807 .priv_auto_alloc_size = sizeof(struct fsl_qspi),
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800808 .probe = fsl_qspi_probe,
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800809};