blob: e715f9838f1ff1dcd362d7ef1a8dfef2866ee246 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060027#include <log.h>
Alison Wangc7410e32014-05-06 09:13:01 +080028#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060029#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060030#include <linux/delay.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060031#include <linux/libfdt.h>
32#include <linux/sizes.h>
33#include <linux/iopoll.h>
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080034#include <dm.h>
Kuldeep Singhd8429a12020-02-20 22:57:52 +053035#include <linux/iopoll.h>
36#include <linux/sizes.h>
37#include <linux/err.h>
38#include <spi.h>
39#include <spi-mem.h>
Alison Wangc7410e32014-05-06 09:13:01 +080040
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080041DECLARE_GLOBAL_DATA_PTR;
42
Kuldeep Singhd8429a12020-02-20 22:57:52 +053043/*
44 * The driver only uses one single LUT entry, that is updated on
45 * each call of exec_op(). Index 0 is preset at boot with a basic
46 * read operation, so let's use the last entry (15).
47 */
48#define SEQID_LUT 15
Alison Wangc7410e32014-05-06 09:13:01 +080049
Kuldeep Singhd8429a12020-02-20 22:57:52 +053050/* Registers used by the driver */
51#define QUADSPI_MCR 0x00
52#define QUADSPI_MCR_RESERVED_MASK GENMASK(19, 16)
53#define QUADSPI_MCR_MDIS_MASK BIT(14)
54#define QUADSPI_MCR_CLR_TXF_MASK BIT(11)
55#define QUADSPI_MCR_CLR_RXF_MASK BIT(10)
56#define QUADSPI_MCR_DDR_EN_MASK BIT(7)
57#define QUADSPI_MCR_END_CFG_MASK GENMASK(3, 2)
58#define QUADSPI_MCR_SWRSTHD_MASK BIT(1)
59#define QUADSPI_MCR_SWRSTSD_MASK BIT(0)
Alison Wangc7410e32014-05-06 09:13:01 +080060
Kuldeep Singhd8429a12020-02-20 22:57:52 +053061#define QUADSPI_IPCR 0x08
62#define QUADSPI_IPCR_SEQID(x) ((x) << 24)
63#define QUADSPI_FLSHCR 0x0c
64#define QUADSPI_FLSHCR_TCSS_MASK GENMASK(3, 0)
65#define QUADSPI_FLSHCR_TCSH_MASK GENMASK(11, 8)
66#define QUADSPI_FLSHCR_TDH_MASK GENMASK(17, 16)
Alison Wangc7410e32014-05-06 09:13:01 +080067
Kuldeep Singhd8429a12020-02-20 22:57:52 +053068#define QUADSPI_BUF3CR 0x1c
69#define QUADSPI_BUF3CR_ALLMST_MASK BIT(31)
70#define QUADSPI_BUF3CR_ADATSZ(x) ((x) << 8)
71#define QUADSPI_BUF3CR_ADATSZ_MASK GENMASK(15, 8)
Alison Wangc7410e32014-05-06 09:13:01 +080072
Kuldeep Singhd8429a12020-02-20 22:57:52 +053073#define QUADSPI_BFGENCR 0x20
74#define QUADSPI_BFGENCR_SEQID(x) ((x) << 12)
Peng Fan3a344482015-01-04 17:07:14 +080075
Kuldeep Singhd8429a12020-02-20 22:57:52 +053076#define QUADSPI_BUF0IND 0x30
77#define QUADSPI_BUF1IND 0x34
78#define QUADSPI_BUF2IND 0x38
79#define QUADSPI_SFAR 0x100
Peng Fan3a344482015-01-04 17:07:14 +080080
Kuldeep Singhd8429a12020-02-20 22:57:52 +053081#define QUADSPI_SMPR 0x108
82#define QUADSPI_SMPR_DDRSMP_MASK GENMASK(18, 16)
83#define QUADSPI_SMPR_FSDLY_MASK BIT(6)
84#define QUADSPI_SMPR_FSPHS_MASK BIT(5)
85#define QUADSPI_SMPR_HSENA_MASK BIT(0)
Yuan Yaod7193262016-03-15 14:36:42 +080086
Kuldeep Singhd8429a12020-02-20 22:57:52 +053087#define QUADSPI_RBCT 0x110
88#define QUADSPI_RBCT_WMRK_MASK GENMASK(4, 0)
89#define QUADSPI_RBCT_RXBRD_USEIPS BIT(8)
Alison Wangc7410e32014-05-06 09:13:01 +080090
Kuldeep Singhd8429a12020-02-20 22:57:52 +053091#define QUADSPI_TBDR 0x154
Alison Wangc7410e32014-05-06 09:13:01 +080092
Kuldeep Singhd8429a12020-02-20 22:57:52 +053093#define QUADSPI_SR 0x15c
94#define QUADSPI_SR_IP_ACC_MASK BIT(1)
95#define QUADSPI_SR_AHB_ACC_MASK BIT(2)
Alison Wangc7410e32014-05-06 09:13:01 +080096
Kuldeep Singhd8429a12020-02-20 22:57:52 +053097#define QUADSPI_FR 0x160
98#define QUADSPI_FR_TFF_MASK BIT(0)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +080099
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530100#define QUADSPI_RSER 0x164
101#define QUADSPI_RSER_TFIE BIT(0)
Ye Li007b6042019-08-14 11:31:36 +0000102
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530103#define QUADSPI_SPTRCLR 0x16c
104#define QUADSPI_SPTRCLR_IPPTRC BIT(8)
105#define QUADSPI_SPTRCLR_BFPTRC BIT(0)
Ye Li007b6042019-08-14 11:31:36 +0000106
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530107#define QUADSPI_SFA1AD 0x180
108#define QUADSPI_SFA2AD 0x184
109#define QUADSPI_SFB1AD 0x188
110#define QUADSPI_SFB2AD 0x18c
111#define QUADSPI_RBDR(x) (0x200 + ((x) * 4))
Ye Li007b6042019-08-14 11:31:36 +0000112
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530113#define QUADSPI_LUTKEY 0x300
114#define QUADSPI_LUTKEY_VALUE 0x5AF05AF0
115
116#define QUADSPI_LCKCR 0x304
117#define QUADSPI_LCKER_LOCK BIT(0)
118#define QUADSPI_LCKER_UNLOCK BIT(1)
119
120#define QUADSPI_LUT_BASE 0x310
121#define QUADSPI_LUT_OFFSET (SEQID_LUT * 4 * 4)
122#define QUADSPI_LUT_REG(idx) \
123 (QUADSPI_LUT_BASE + QUADSPI_LUT_OFFSET + (idx) * 4)
124
125/* Instruction set for the LUT register */
126#define LUT_STOP 0
127#define LUT_CMD 1
128#define LUT_ADDR 2
129#define LUT_DUMMY 3
130#define LUT_MODE 4
131#define LUT_MODE2 5
132#define LUT_MODE4 6
133#define LUT_FSL_READ 7
134#define LUT_FSL_WRITE 8
135#define LUT_JMP_ON_CS 9
136#define LUT_ADDR_DDR 10
137#define LUT_MODE_DDR 11
138#define LUT_MODE2_DDR 12
139#define LUT_MODE4_DDR 13
140#define LUT_FSL_READ_DDR 14
141#define LUT_FSL_WRITE_DDR 15
142#define LUT_DATA_LEARN 16
143
144/*
145 * The PAD definitions for LUT register.
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800146 *
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530147 * The pad stands for the number of IO lines [0:3].
148 * For example, the quad read needs four IO lines,
149 * so you should use LUT_PAD(4).
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800150 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530151#define LUT_PAD(x) (fls(x) - 1)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800152
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530153/*
154 * Macro for constructing the LUT entries with the following
155 * register layout:
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800156 *
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530157 * ---------------------------------------------------
158 * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 |
159 * ---------------------------------------------------
160 */
161#define LUT_DEF(idx, ins, pad, opr) \
162 ((((ins) << 10) | ((pad) << 8) | (opr)) << (((idx) % 2) * 16))
163
164/* Controller needs driver to swap endianness */
165#define QUADSPI_QUIRK_SWAP_ENDIAN BIT(0)
166
167/* Controller needs 4x internal clock */
168#define QUADSPI_QUIRK_4X_INT_CLK BIT(1)
169
170/*
171 * TKT253890, the controller needs the driver to fill the txfifo with
172 * 16 bytes at least to trigger a data transfer, even though the extra
173 * data won't be transferred.
174 */
175#define QUADSPI_QUIRK_TKT253890 BIT(2)
176
177/* TKT245618, the controller cannot wake up from wait mode */
178#define QUADSPI_QUIRK_TKT245618 BIT(3)
179
180/*
181 * Controller adds QSPI_AMBA_BASE (base address of the mapped memory)
182 * internally. No need to add it when setting SFXXAD and SFAR registers
183 */
184#define QUADSPI_QUIRK_BASE_INTERNAL BIT(4)
185
186/*
187 * Controller uses TDH bits in register QUADSPI_FLSHCR.
188 * They need to be set in accordance with the DDR/SDR mode.
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800189 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530190#define QUADSPI_QUIRK_USE_TDH_SETTING BIT(5)
191
192struct fsl_qspi_devtype_data {
193 unsigned int rxfifo;
194 unsigned int txfifo;
195 unsigned int ahb_buf_size;
196 unsigned int quirks;
197 bool little_endian;
Alison Wangc7410e32014-05-06 09:13:01 +0800198};
199
Ye Li007b6042019-08-14 11:31:36 +0000200static const struct fsl_qspi_devtype_data vybrid_data = {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530201 .rxfifo = SZ_128,
202 .txfifo = SZ_64,
203 .ahb_buf_size = SZ_1K,
204 .quirks = QUADSPI_QUIRK_SWAP_ENDIAN,
205 .little_endian = true,
Ye Li007b6042019-08-14 11:31:36 +0000206};
207
208static const struct fsl_qspi_devtype_data imx6sx_data = {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530209 .rxfifo = SZ_128,
210 .txfifo = SZ_512,
211 .ahb_buf_size = SZ_1K,
212 .quirks = QUADSPI_QUIRK_4X_INT_CLK | QUADSPI_QUIRK_TKT245618,
213 .little_endian = true,
Ye Li007b6042019-08-14 11:31:36 +0000214};
215
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530216static const struct fsl_qspi_devtype_data imx7d_data = {
217 .rxfifo = SZ_128,
218 .txfifo = SZ_512,
219 .ahb_buf_size = SZ_1K,
220 .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_4X_INT_CLK |
221 QUADSPI_QUIRK_USE_TDH_SETTING,
222 .little_endian = true,
Ye Li007b6042019-08-14 11:31:36 +0000223};
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800224
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530225static const struct fsl_qspi_devtype_data imx6ul_data = {
226 .rxfifo = SZ_128,
227 .txfifo = SZ_512,
228 .ahb_buf_size = SZ_1K,
229 .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_4X_INT_CLK |
230 QUADSPI_QUIRK_USE_TDH_SETTING,
231 .little_endian = true,
Ye Li57f67752019-08-14 11:31:40 +0000232};
233
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530234static const struct fsl_qspi_devtype_data ls1021a_data = {
235 .rxfifo = SZ_128,
236 .txfifo = SZ_64,
237 .ahb_buf_size = SZ_1K,
238 .quirks = 0,
239 .little_endian = false,
240};
241
242static const struct fsl_qspi_devtype_data ls1088a_data = {
243 .rxfifo = SZ_128,
244 .txfifo = SZ_128,
245 .ahb_buf_size = SZ_1K,
246 .quirks = QUADSPI_QUIRK_TKT253890,
247 .little_endian = true,
248};
249
250static const struct fsl_qspi_devtype_data ls2080a_data = {
251 .rxfifo = SZ_128,
252 .txfifo = SZ_64,
253 .ahb_buf_size = SZ_1K,
254 .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_BASE_INTERNAL,
255 .little_endian = true,
256};
257
258struct fsl_qspi {
259 struct udevice *dev;
260 void __iomem *iobase;
261 void __iomem *ahb_addr;
262 u32 memmap_phy;
263 const struct fsl_qspi_devtype_data *devtype_data;
264 int selected;
265};
266
267static inline int needs_swap_endian(struct fsl_qspi *q)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800268{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530269 return q->devtype_data->quirks & QUADSPI_QUIRK_SWAP_ENDIAN;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800270}
271
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530272static inline int needs_4x_clock(struct fsl_qspi *q)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800273{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530274 return q->devtype_data->quirks & QUADSPI_QUIRK_4X_INT_CLK;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800275}
Alison Wangc7410e32014-05-06 09:13:01 +0800276
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530277static inline int needs_fill_txfifo(struct fsl_qspi *q)
Rajat Srivastava234daec2018-03-22 13:30:55 +0530278{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530279 return q->devtype_data->quirks & QUADSPI_QUIRK_TKT253890;
Rajat Srivastava234daec2018-03-22 13:30:55 +0530280}
281
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530282static inline int needs_wakeup_wait_mode(struct fsl_qspi *q)
Alison Wangc7410e32014-05-06 09:13:01 +0800283{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530284 return q->devtype_data->quirks & QUADSPI_QUIRK_TKT245618;
Alison Wangc7410e32014-05-06 09:13:01 +0800285}
286
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530287static inline int needs_amba_base_offset(struct fsl_qspi *q)
Alison Wangc7410e32014-05-06 09:13:01 +0800288{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530289 return !(q->devtype_data->quirks & QUADSPI_QUIRK_BASE_INTERNAL);
290}
Alison Wangc7410e32014-05-06 09:13:01 +0800291
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530292static inline int needs_tdh_setting(struct fsl_qspi *q)
293{
294 return q->devtype_data->quirks & QUADSPI_QUIRK_USE_TDH_SETTING;
295}
Alison Wangc7410e32014-05-06 09:13:01 +0800296
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530297/*
298 * An IC bug makes it necessary to rearrange the 32-bit data.
299 * Later chips, such as IMX6SLX, have fixed this bug.
300 */
301static inline u32 fsl_qspi_endian_xchg(struct fsl_qspi *q, u32 a)
302{
303 return needs_swap_endian(q) ? __swab32(a) : a;
304}
Alison Wangc7410e32014-05-06 09:13:01 +0800305
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530306/*
307 * R/W functions for big- or little-endian registers:
308 * The QSPI controller's endianness is independent of
309 * the CPU core's endianness. So far, although the CPU
310 * core is little-endian the QSPI controller can use
311 * big-endian or little-endian.
312 */
313static void qspi_writel(struct fsl_qspi *q, u32 val, void __iomem *addr)
314{
315 if (q->devtype_data->little_endian)
316 out_le32(addr, val);
Alison Wangc7410e32014-05-06 09:13:01 +0800317 else
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530318 out_be32(addr, val);
319}
Alison Wangc7410e32014-05-06 09:13:01 +0800320
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530321static u32 qspi_readl(struct fsl_qspi *q, void __iomem *addr)
322{
323 if (q->devtype_data->little_endian)
324 return in_le32(addr);
Alison Wangc7410e32014-05-06 09:13:01 +0800325
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530326 return in_be32(addr);
327}
Alison Wangc7410e32014-05-06 09:13:01 +0800328
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530329static int fsl_qspi_check_buswidth(struct fsl_qspi *q, u8 width)
330{
331 switch (width) {
332 case 1:
333 case 2:
334 case 4:
335 return 0;
336 }
Alison Wangc7410e32014-05-06 09:13:01 +0800337
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530338 return -ENOTSUPP;
339}
Alison Wangc7410e32014-05-06 09:13:01 +0800340
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530341static bool fsl_qspi_supports_op(struct spi_slave *slave,
342 const struct spi_mem_op *op)
343{
344 struct fsl_qspi *q = dev_get_priv(slave->dev->parent);
345 int ret;
Alison Wangc7410e32014-05-06 09:13:01 +0800346
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530347 ret = fsl_qspi_check_buswidth(q, op->cmd.buswidth);
Peng Fan3642a872014-12-31 11:01:39 +0800348
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530349 if (op->addr.nbytes)
350 ret |= fsl_qspi_check_buswidth(q, op->addr.buswidth);
Peng Fan3a344482015-01-04 17:07:14 +0800351
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530352 if (op->dummy.nbytes)
353 ret |= fsl_qspi_check_buswidth(q, op->dummy.buswidth);
Peng Fan3a344482015-01-04 17:07:14 +0800354
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530355 if (op->data.nbytes)
356 ret |= fsl_qspi_check_buswidth(q, op->data.buswidth);
Peng Fan3a344482015-01-04 17:07:14 +0800357
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530358 if (ret)
359 return false;
Yuan Yaod7193262016-03-15 14:36:42 +0800360
361 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530362 * The number of instructions needed for the op, needs
363 * to fit into a single LUT entry.
Yuan Yaod7193262016-03-15 14:36:42 +0800364 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530365 if (op->addr.nbytes +
366 (op->dummy.nbytes ? 1 : 0) +
367 (op->data.nbytes ? 1 : 0) > 6)
368 return false;
Yuan Yaod7193262016-03-15 14:36:42 +0800369
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530370 /* Max 64 dummy clock cycles supported */
371 if (op->dummy.nbytes &&
372 (op->dummy.nbytes * 8 / op->dummy.buswidth > 64))
373 return false;
Yuan Yaod7193262016-03-15 14:36:42 +0800374
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530375 /* Max data length, check controller limits and alignment */
376 if (op->data.dir == SPI_MEM_DATA_IN &&
377 (op->data.nbytes > q->devtype_data->ahb_buf_size ||
378 (op->data.nbytes > q->devtype_data->rxfifo - 4 &&
379 !IS_ALIGNED(op->data.nbytes, 8))))
380 return false;
381
382 if (op->data.dir == SPI_MEM_DATA_OUT &&
383 op->data.nbytes > q->devtype_data->txfifo)
384 return false;
385
386 return true;
Alison Wangc7410e32014-05-06 09:13:01 +0800387}
388
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530389static void fsl_qspi_prepare_lut(struct fsl_qspi *q,
390 const struct spi_mem_op *op)
Peng Fan1c5f9662015-01-08 10:40:20 +0800391{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530392 void __iomem *base = q->iobase;
393 u32 lutval[4] = {};
394 int lutidx = 1, i;
Peng Fan1c5f9662015-01-08 10:40:20 +0800395
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530396 lutval[0] |= LUT_DEF(0, LUT_CMD, LUT_PAD(op->cmd.buswidth),
397 op->cmd.opcode);
Peng Fan1c5f9662015-01-08 10:40:20 +0800398
399 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530400 * For some unknown reason, using LUT_ADDR doesn't work in some
401 * cases (at least with only one byte long addresses), so
402 * let's use LUT_MODE to write the address bytes one by one
Peng Fan1c5f9662015-01-08 10:40:20 +0800403 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530404 for (i = 0; i < op->addr.nbytes; i++) {
405 u8 addrbyte = op->addr.val >> (8 * (op->addr.nbytes - i - 1));
Peng Fan1c5f9662015-01-08 10:40:20 +0800406
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530407 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_MODE,
408 LUT_PAD(op->addr.buswidth),
409 addrbyte);
410 lutidx++;
411 }
Peng Fan1c5f9662015-01-08 10:40:20 +0800412
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530413 if (op->dummy.nbytes) {
414 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_DUMMY,
415 LUT_PAD(op->dummy.buswidth),
416 op->dummy.nbytes * 8 /
417 op->dummy.buswidth);
418 lutidx++;
419 }
Peng Fan1c5f9662015-01-08 10:40:20 +0800420
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530421 if (op->data.nbytes) {
422 lutval[lutidx / 2] |= LUT_DEF(lutidx,
423 op->data.dir == SPI_MEM_DATA_IN ?
424 LUT_FSL_READ : LUT_FSL_WRITE,
425 LUT_PAD(op->data.buswidth),
426 0);
427 lutidx++;
428 }
Peng Fan1c5f9662015-01-08 10:40:20 +0800429
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530430 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_STOP, 0, 0);
Peng Fan1c5f9662015-01-08 10:40:20 +0800431
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530432 /* unlock LUT */
433 qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
434 qspi_writel(q, QUADSPI_LCKER_UNLOCK, q->iobase + QUADSPI_LCKCR);
Peng Fan1c5f9662015-01-08 10:40:20 +0800435
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530436 dev_dbg(q->dev, "CMD[%x] lutval[0:%x \t 1:%x \t 2:%x \t 3:%x]\n",
437 op->cmd.opcode, lutval[0], lutval[1], lutval[2], lutval[3]);
Peng Fan1c5f9662015-01-08 10:40:20 +0800438
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530439 /* fill LUT */
440 for (i = 0; i < ARRAY_SIZE(lutval); i++)
441 qspi_writel(q, lutval[i], base + QUADSPI_LUT_REG(i));
Ye Li416d2ec2019-08-14 11:31:27 +0000442
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530443 /* lock LUT */
444 qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
445 qspi_writel(q, QUADSPI_LCKER_LOCK, q->iobase + QUADSPI_LCKCR);
Peng Fan1c5f9662015-01-08 10:40:20 +0800446}
447
448/*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530449 * If we have changed the content of the flash by writing or erasing, or if we
450 * read from flash with a different offset into the page buffer, we need to
451 * invalidate the AHB buffer. If we do not do so, we may read out the wrong
452 * data. The spec tells us reset the AHB domain and Serial Flash domain at
453 * the same time.
Peng Fan1c5f9662015-01-08 10:40:20 +0800454 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530455static void fsl_qspi_invalidate(struct fsl_qspi *q)
Peng Fan1c5f9662015-01-08 10:40:20 +0800456{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530457 u32 reg;
Peng Fan1c5f9662015-01-08 10:40:20 +0800458
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530459 reg = qspi_readl(q, q->iobase + QUADSPI_MCR);
460 reg |= QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK;
461 qspi_writel(q, reg, q->iobase + QUADSPI_MCR);
Peng Fan1c5f9662015-01-08 10:40:20 +0800462
463 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530464 * The minimum delay : 1 AHB + 2 SFCK clocks.
465 * Delay 1 us is enough.
Peng Fan1c5f9662015-01-08 10:40:20 +0800466 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530467 udelay(1);
Peng Fan1c5f9662015-01-08 10:40:20 +0800468
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530469 reg &= ~(QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK);
470 qspi_writel(q, reg, q->iobase + QUADSPI_MCR);
Peng Fan1c5f9662015-01-08 10:40:20 +0800471}
Peng Fan1c5f9662015-01-08 10:40:20 +0800472
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530473static void fsl_qspi_select_mem(struct fsl_qspi *q, struct spi_slave *slave)
Peng Fan3a344482015-01-04 17:07:14 +0800474{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530475 struct dm_spi_slave_platdata *plat =
476 dev_get_parent_platdata(slave->dev);
Peng Fan3a344482015-01-04 17:07:14 +0800477
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530478 if (q->selected == plat->cs)
479 return;
Alexander Stein283eb4a2017-06-01 09:32:19 +0200480
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530481 q->selected = plat->cs;
482 fsl_qspi_invalidate(q);
Peng Fan3a344482015-01-04 17:07:14 +0800483}
Alison Wangc7410e32014-05-06 09:13:01 +0800484
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530485static void fsl_qspi_read_ahb(struct fsl_qspi *q, const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800486{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530487 memcpy_fromio(op->data.buf.in,
488 q->ahb_addr + q->selected * q->devtype_data->ahb_buf_size,
489 op->data.nbytes);
Alison Wangc7410e32014-05-06 09:13:01 +0800490}
491
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530492static void fsl_qspi_fill_txfifo(struct fsl_qspi *q,
493 const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800494{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530495 void __iomem *base = q->iobase;
496 int i;
497 u32 val;
Alison Wangc7410e32014-05-06 09:13:01 +0800498
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530499 for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 4); i += 4) {
500 memcpy(&val, op->data.buf.out + i, 4);
501 val = fsl_qspi_endian_xchg(q, val);
502 qspi_writel(q, val, base + QUADSPI_TBDR);
503 }
Alison Wangc7410e32014-05-06 09:13:01 +0800504
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530505 if (i < op->data.nbytes) {
506 memcpy(&val, op->data.buf.out + i, op->data.nbytes - i);
507 val = fsl_qspi_endian_xchg(q, val);
508 qspi_writel(q, val, base + QUADSPI_TBDR);
Alison Wangc7410e32014-05-06 09:13:01 +0800509 }
510
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530511 if (needs_fill_txfifo(q)) {
512 for (i = op->data.nbytes; i < 16; i += 4)
513 qspi_writel(q, 0, base + QUADSPI_TBDR);
514 }
Alison Wangc7410e32014-05-06 09:13:01 +0800515}
516
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530517static void fsl_qspi_read_rxfifo(struct fsl_qspi *q,
518 const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800519{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530520 void __iomem *base = q->iobase;
521 int i;
522 u8 *buf = op->data.buf.in;
523 u32 val;
Alison Wangc7410e32014-05-06 09:13:01 +0800524
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530525 for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 4); i += 4) {
526 val = qspi_readl(q, base + QUADSPI_RBDR(i / 4));
527 val = fsl_qspi_endian_xchg(q, val);
528 memcpy(buf + i, &val, 4);
Alison Wangc7410e32014-05-06 09:13:01 +0800529 }
530
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530531 if (i < op->data.nbytes) {
532 val = qspi_readl(q, base + QUADSPI_RBDR(i / 4));
533 val = fsl_qspi_endian_xchg(q, val);
534 memcpy(buf + i, &val, op->data.nbytes - i);
Alison Wangc7410e32014-05-06 09:13:01 +0800535 }
Alison Wangc7410e32014-05-06 09:13:01 +0800536}
537
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530538static int fsl_qspi_readl_poll_tout(struct fsl_qspi *q, void __iomem *base,
539 u32 mask, u32 delay_us, u32 timeout_us)
Alison Wangc7410e32014-05-06 09:13:01 +0800540{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530541 u32 reg;
Alexander Stein283eb4a2017-06-01 09:32:19 +0200542
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530543 if (!q->devtype_data->little_endian)
544 mask = (u32)cpu_to_be32(mask);
Alison Wangc7410e32014-05-06 09:13:01 +0800545
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530546 return readl_poll_timeout(base, reg, !(reg & mask), timeout_us);
Alison Wangc7410e32014-05-06 09:13:01 +0800547}
548
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530549static int fsl_qspi_do_op(struct fsl_qspi *q, const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800550{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530551 void __iomem *base = q->iobase;
552 int err = 0;
Alison Wangc7410e32014-05-06 09:13:01 +0800553
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530554 /*
555 * Always start the sequence at the same index since we update
556 * the LUT at each exec_op() call. And also specify the DATA
557 * length, since it's has not been specified in the LUT.
558 */
559 qspi_writel(q, op->data.nbytes | QUADSPI_IPCR_SEQID(SEQID_LUT),
560 base + QUADSPI_IPCR);
Alison Wangc7410e32014-05-06 09:13:01 +0800561
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530562 /* wait for the controller being ready */
563 err = fsl_qspi_readl_poll_tout(q, base + QUADSPI_SR,
564 (QUADSPI_SR_IP_ACC_MASK |
565 QUADSPI_SR_AHB_ACC_MASK),
566 10, 1000);
Alison Wangc7410e32014-05-06 09:13:01 +0800567
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530568 if (!err && op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN)
569 fsl_qspi_read_rxfifo(q, op);
Alison Wangc7410e32014-05-06 09:13:01 +0800570
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530571 return err;
Alison Wangc7410e32014-05-06 09:13:01 +0800572}
573
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530574static int fsl_qspi_exec_op(struct spi_slave *slave,
575 const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800576{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530577 struct fsl_qspi *q = dev_get_priv(slave->dev->parent);
578 void __iomem *base = q->iobase;
579 u32 addr_offset = 0;
580 int err = 0;
Alison Wangc7410e32014-05-06 09:13:01 +0800581
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530582 /* wait for the controller being ready */
583 fsl_qspi_readl_poll_tout(q, base + QUADSPI_SR, (QUADSPI_SR_IP_ACC_MASK |
584 QUADSPI_SR_AHB_ACC_MASK), 10, 1000);
Alexander Stein283eb4a2017-06-01 09:32:19 +0200585
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530586 fsl_qspi_select_mem(q, slave);
Alison Wangc7410e32014-05-06 09:13:01 +0800587
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530588 if (needs_amba_base_offset(q))
589 addr_offset = q->memmap_phy;
Alison Wangc7410e32014-05-06 09:13:01 +0800590
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530591 qspi_writel(q,
592 q->selected * q->devtype_data->ahb_buf_size + addr_offset,
593 base + QUADSPI_SFAR);
Alison Wangc7410e32014-05-06 09:13:01 +0800594
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530595 qspi_writel(q, qspi_readl(q, base + QUADSPI_MCR) |
596 QUADSPI_MCR_CLR_RXF_MASK | QUADSPI_MCR_CLR_TXF_MASK,
597 base + QUADSPI_MCR);
Alison Wangc7410e32014-05-06 09:13:01 +0800598
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530599 qspi_writel(q, QUADSPI_SPTRCLR_BFPTRC | QUADSPI_SPTRCLR_IPPTRC,
600 base + QUADSPI_SPTRCLR);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800601
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530602 fsl_qspi_prepare_lut(q, op);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800603
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530604 /*
605 * If we have large chunks of data, we read them through the AHB bus
606 * by accessing the mapped memory. In all other cases we use
607 * IP commands to access the flash.
608 */
609 if (op->data.nbytes > (q->devtype_data->rxfifo - 4) &&
610 op->data.dir == SPI_MEM_DATA_IN) {
611 fsl_qspi_read_ahb(q, op);
612 } else {
613 qspi_writel(q, QUADSPI_RBCT_WMRK_MASK |
614 QUADSPI_RBCT_RXBRD_USEIPS, base + QUADSPI_RBCT);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800615
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530616 if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT)
617 fsl_qspi_fill_txfifo(q, op);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800618
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530619 err = fsl_qspi_do_op(q, op);
620 }
621
622 /* Invalidate the data in the AHB buffer. */
623 fsl_qspi_invalidate(q);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800624
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530625 return err;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800626}
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800627
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530628static int fsl_qspi_adjust_op_size(struct spi_slave *slave,
629 struct spi_mem_op *op)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800630{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530631 struct fsl_qspi *q = dev_get_priv(slave->dev->parent);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800632
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530633 if (op->data.dir == SPI_MEM_DATA_OUT) {
634 if (op->data.nbytes > q->devtype_data->txfifo)
635 op->data.nbytes = q->devtype_data->txfifo;
636 } else {
637 if (op->data.nbytes > q->devtype_data->ahb_buf_size)
638 op->data.nbytes = q->devtype_data->ahb_buf_size;
639 else if (op->data.nbytes > (q->devtype_data->rxfifo - 4))
640 op->data.nbytes = ALIGN_DOWN(op->data.nbytes, 8);
641 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800642
643 return 0;
644}
645
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530646static int fsl_qspi_default_setup(struct fsl_qspi *q)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800647{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530648 void __iomem *base = q->iobase;
649 u32 reg, addr_offset = 0;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800650
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530651 /* Reset the module */
652 qspi_writel(q, QUADSPI_MCR_SWRSTSD_MASK | QUADSPI_MCR_SWRSTHD_MASK,
653 base + QUADSPI_MCR);
654 udelay(1);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800655
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530656 /* Disable the module */
657 qspi_writel(q, QUADSPI_MCR_MDIS_MASK | QUADSPI_MCR_RESERVED_MASK,
658 base + QUADSPI_MCR);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800659
Yuan Yaoae412392016-03-15 14:36:40 +0800660 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530661 * Previous boot stages (BootROM, bootloader) might have used DDR
662 * mode and did not clear the TDH bits. As we currently use SDR mode
663 * only, clear the TDH bits if necessary.
Yuan Yaoae412392016-03-15 14:36:40 +0800664 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530665 if (needs_tdh_setting(q))
666 qspi_writel(q, qspi_readl(q, base + QUADSPI_FLSHCR) &
667 ~QUADSPI_FLSHCR_TDH_MASK,
668 base + QUADSPI_FLSHCR);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800669
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530670 reg = qspi_readl(q, base + QUADSPI_SMPR);
671 qspi_writel(q, reg & ~(QUADSPI_SMPR_FSDLY_MASK
672 | QUADSPI_SMPR_FSPHS_MASK
673 | QUADSPI_SMPR_HSENA_MASK
674 | QUADSPI_SMPR_DDRSMP_MASK), base + QUADSPI_SMPR);
Ye Li007b6042019-08-14 11:31:36 +0000675
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530676 /* We only use the buffer3 for AHB read */
677 qspi_writel(q, 0, base + QUADSPI_BUF0IND);
678 qspi_writel(q, 0, base + QUADSPI_BUF1IND);
679 qspi_writel(q, 0, base + QUADSPI_BUF2IND);
Suresh Gupta4945b872017-08-30 20:06:33 +0530680
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530681 qspi_writel(q, QUADSPI_BFGENCR_SEQID(SEQID_LUT),
682 q->iobase + QUADSPI_BFGENCR);
683 qspi_writel(q, QUADSPI_RBCT_WMRK_MASK, base + QUADSPI_RBCT);
684 qspi_writel(q, QUADSPI_BUF3CR_ALLMST_MASK |
685 QUADSPI_BUF3CR_ADATSZ(q->devtype_data->ahb_buf_size / 8),
686 base + QUADSPI_BUF3CR);
Suresh Gupta4945b872017-08-30 20:06:33 +0530687
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530688 if (needs_amba_base_offset(q))
689 addr_offset = q->memmap_phy;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800690
Yuan Yaob4bfe102016-03-15 14:36:41 +0800691 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530692 * In HW there can be a maximum of four chips on two buses with
693 * two chip selects on each bus. We use four chip selects in SW
694 * to differentiate between the four chips.
695 * We use ahb_buf_size for each chip and set SFA1AD, SFA2AD, SFB1AD,
696 * SFB2AD accordingly.
Yuan Yaob4bfe102016-03-15 14:36:41 +0800697 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530698 qspi_writel(q, q->devtype_data->ahb_buf_size + addr_offset,
699 base + QUADSPI_SFA1AD);
700 qspi_writel(q, q->devtype_data->ahb_buf_size * 2 + addr_offset,
701 base + QUADSPI_SFA2AD);
702 qspi_writel(q, q->devtype_data->ahb_buf_size * 3 + addr_offset,
703 base + QUADSPI_SFB1AD);
704 qspi_writel(q, q->devtype_data->ahb_buf_size * 4 + addr_offset,
705 base + QUADSPI_SFB2AD);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800706
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530707 q->selected = -1;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800708
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530709 /* Enable the module */
710 qspi_writel(q, QUADSPI_MCR_RESERVED_MASK | QUADSPI_MCR_END_CFG_MASK,
711 base + QUADSPI_MCR);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800712 return 0;
713}
714
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530715static const struct spi_controller_mem_ops fsl_qspi_mem_ops = {
716 .adjust_op_size = fsl_qspi_adjust_op_size,
717 .supports_op = fsl_qspi_supports_op,
718 .exec_op = fsl_qspi_exec_op,
719};
720
721static int fsl_qspi_probe(struct udevice *bus)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800722{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530723 struct dm_spi_bus *dm_bus = bus->uclass_priv;
724 struct fsl_qspi *q = dev_get_priv(bus);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800725 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700726 int node = dev_of_offset(bus);
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530727 struct fdt_resource res;
728 int ret;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800729
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530730 q->dev = bus;
731 q->devtype_data = (struct fsl_qspi_devtype_data *)
732 dev_get_driver_data(bus);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800733
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530734 /* find the resources */
735 ret = fdt_get_named_resource(blob, node, "reg", "reg-names", "QuadSPI",
736 &res);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800737 if (ret) {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530738 dev_err(bus, "Can't get regs base addresses(ret = %d)!\n", ret);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800739 return -ENOMEM;
740 }
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530741
742 q->iobase = map_physmem(res.start, res.end - res.start, MAP_NOCACHE);
743
Yuan Yaoae412392016-03-15 14:36:40 +0800744 ret = fdt_get_named_resource(blob, node, "reg", "reg-names",
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530745 "QuadSPI-memory", &res);
Yuan Yaoae412392016-03-15 14:36:40 +0800746 if (ret) {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530747 dev_err(bus, "Can't get AMBA base addresses(ret = %d)!\n", ret);
Yuan Yaoae412392016-03-15 14:36:40 +0800748 return -ENOMEM;
749 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800750
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530751 q->ahb_addr = map_physmem(res.start, res.end - res.start, MAP_NOCACHE);
752 q->memmap_phy = res.start;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800753
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530754 dm_bus->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
755 66000000);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800756
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530757 fsl_qspi_default_setup(q);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800758
759 return 0;
760}
761
762static int fsl_qspi_xfer(struct udevice *dev, unsigned int bitlen,
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530763 const void *dout, void *din, unsigned long flags)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800764{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530765 return 0;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800766}
767
768static int fsl_qspi_claim_bus(struct udevice *dev)
769{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800770 return 0;
771}
772
773static int fsl_qspi_release_bus(struct udevice *dev)
774{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800775 return 0;
776}
777
778static int fsl_qspi_set_speed(struct udevice *bus, uint speed)
779{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800780 return 0;
781}
782
783static int fsl_qspi_set_mode(struct udevice *bus, uint mode)
784{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800785 return 0;
786}
787
788static const struct dm_spi_ops fsl_qspi_ops = {
789 .claim_bus = fsl_qspi_claim_bus,
790 .release_bus = fsl_qspi_release_bus,
791 .xfer = fsl_qspi_xfer,
792 .set_speed = fsl_qspi_set_speed,
793 .set_mode = fsl_qspi_set_mode,
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530794 .mem_ops = &fsl_qspi_mem_ops,
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800795};
796
797static const struct udevice_id fsl_qspi_ids[] = {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530798 { .compatible = "fsl,vf610-qspi", .data = (ulong)&vybrid_data, },
799 { .compatible = "fsl,imx6sx-qspi", .data = (ulong)&imx6sx_data, },
800 { .compatible = "fsl,imx6ul-qspi", .data = (ulong)&imx6ul_data, },
801 { .compatible = "fsl,imx7d-qspi", .data = (ulong)&imx7d_data, },
802 { .compatible = "fsl,ls1021a-qspi", .data = (ulong)&ls1021a_data, },
803 { .compatible = "fsl,ls1088a-qspi", .data = (ulong)&ls1088a_data, },
804 { .compatible = "fsl,ls2080a-qspi", .data = (ulong)&ls2080a_data, },
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800805 { }
806};
807
808U_BOOT_DRIVER(fsl_qspi) = {
809 .name = "fsl_qspi",
810 .id = UCLASS_SPI,
811 .of_match = fsl_qspi_ids,
812 .ops = &fsl_qspi_ops,
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530813 .priv_auto_alloc_size = sizeof(struct fsl_qspi),
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800814 .probe = fsl_qspi_probe,
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800815};