blob: 128f95877f26ca0b0f72af4e543031a3a7833664 [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>
Sean Andersona89b9432020-10-04 21:39:50 -040027#include <dm.h>
28#include <dm/device_compat.h>
Simon Glass0f2af882020-05-10 11:40:05 -060029#include <log.h>
Sean Andersona89b9432020-10-04 21:39:50 -040030#include <spi.h>
31#include <spi-mem.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
262static const struct fsl_qspi_devtype_data ls1088a_data = {
263 .rxfifo = SZ_128,
264 .txfifo = SZ_128,
265 .ahb_buf_size = SZ_1K,
266 .quirks = QUADSPI_QUIRK_TKT253890,
267 .little_endian = true,
268};
269
270static const struct fsl_qspi_devtype_data ls2080a_data = {
271 .rxfifo = SZ_128,
272 .txfifo = SZ_64,
273 .ahb_buf_size = SZ_1K,
274 .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_BASE_INTERNAL,
275 .little_endian = true,
276};
277
278struct fsl_qspi {
279 struct udevice *dev;
280 void __iomem *iobase;
281 void __iomem *ahb_addr;
282 u32 memmap_phy;
Ye Lid7e3c9a2020-06-09 00:59:06 -0700283 u32 memmap_size;
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530284 const struct fsl_qspi_devtype_data *devtype_data;
285 int selected;
286};
287
288static inline int needs_swap_endian(struct fsl_qspi *q)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800289{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530290 return q->devtype_data->quirks & QUADSPI_QUIRK_SWAP_ENDIAN;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800291}
292
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530293static inline int needs_4x_clock(struct fsl_qspi *q)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800294{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530295 return q->devtype_data->quirks & QUADSPI_QUIRK_4X_INT_CLK;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800296}
Alison Wangc7410e32014-05-06 09:13:01 +0800297
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530298static inline int needs_fill_txfifo(struct fsl_qspi *q)
Rajat Srivastava234daec2018-03-22 13:30:55 +0530299{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530300 return q->devtype_data->quirks & QUADSPI_QUIRK_TKT253890;
Rajat Srivastava234daec2018-03-22 13:30:55 +0530301}
302
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530303static inline int needs_wakeup_wait_mode(struct fsl_qspi *q)
Alison Wangc7410e32014-05-06 09:13:01 +0800304{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530305 return q->devtype_data->quirks & QUADSPI_QUIRK_TKT245618;
Alison Wangc7410e32014-05-06 09:13:01 +0800306}
307
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530308static inline int needs_amba_base_offset(struct fsl_qspi *q)
Alison Wangc7410e32014-05-06 09:13:01 +0800309{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530310 return !(q->devtype_data->quirks & QUADSPI_QUIRK_BASE_INTERNAL);
311}
Alison Wangc7410e32014-05-06 09:13:01 +0800312
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530313static inline int needs_tdh_setting(struct fsl_qspi *q)
314{
315 return q->devtype_data->quirks & QUADSPI_QUIRK_USE_TDH_SETTING;
316}
Alison Wangc7410e32014-05-06 09:13:01 +0800317
Ye Lid7e3c9a2020-06-09 00:59:06 -0700318static inline int needs_single_bus(struct fsl_qspi *q)
319{
320 return q->devtype_data->quirks & QUADSPI_QUIRK_SINGLE_BUS;
321}
322
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530323/*
324 * An IC bug makes it necessary to rearrange the 32-bit data.
325 * Later chips, such as IMX6SLX, have fixed this bug.
326 */
327static inline u32 fsl_qspi_endian_xchg(struct fsl_qspi *q, u32 a)
328{
329 return needs_swap_endian(q) ? __swab32(a) : a;
330}
Alison Wangc7410e32014-05-06 09:13:01 +0800331
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530332/*
333 * R/W functions for big- or little-endian registers:
334 * The QSPI controller's endianness is independent of
335 * the CPU core's endianness. So far, although the CPU
336 * core is little-endian the QSPI controller can use
337 * big-endian or little-endian.
338 */
339static void qspi_writel(struct fsl_qspi *q, u32 val, void __iomem *addr)
340{
341 if (q->devtype_data->little_endian)
342 out_le32(addr, val);
Alison Wangc7410e32014-05-06 09:13:01 +0800343 else
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530344 out_be32(addr, val);
345}
Alison Wangc7410e32014-05-06 09:13:01 +0800346
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530347static u32 qspi_readl(struct fsl_qspi *q, void __iomem *addr)
348{
349 if (q->devtype_data->little_endian)
350 return in_le32(addr);
Alison Wangc7410e32014-05-06 09:13:01 +0800351
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530352 return in_be32(addr);
353}
Alison Wangc7410e32014-05-06 09:13:01 +0800354
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530355static int fsl_qspi_check_buswidth(struct fsl_qspi *q, u8 width)
356{
357 switch (width) {
358 case 1:
359 case 2:
360 case 4:
361 return 0;
362 }
Alison Wangc7410e32014-05-06 09:13:01 +0800363
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530364 return -ENOTSUPP;
365}
Alison Wangc7410e32014-05-06 09:13:01 +0800366
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530367static bool fsl_qspi_supports_op(struct spi_slave *slave,
368 const struct spi_mem_op *op)
369{
370 struct fsl_qspi *q = dev_get_priv(slave->dev->parent);
371 int ret;
Alison Wangc7410e32014-05-06 09:13:01 +0800372
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530373 ret = fsl_qspi_check_buswidth(q, op->cmd.buswidth);
Peng Fan3642a872014-12-31 11:01:39 +0800374
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530375 if (op->addr.nbytes)
376 ret |= fsl_qspi_check_buswidth(q, op->addr.buswidth);
Peng Fan3a344482015-01-04 17:07:14 +0800377
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530378 if (op->dummy.nbytes)
379 ret |= fsl_qspi_check_buswidth(q, op->dummy.buswidth);
Peng Fan3a344482015-01-04 17:07:14 +0800380
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530381 if (op->data.nbytes)
382 ret |= fsl_qspi_check_buswidth(q, op->data.buswidth);
Peng Fan3a344482015-01-04 17:07:14 +0800383
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530384 if (ret)
385 return false;
Yuan Yaod7193262016-03-15 14:36:42 +0800386
387 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530388 * The number of instructions needed for the op, needs
389 * to fit into a single LUT entry.
Yuan Yaod7193262016-03-15 14:36:42 +0800390 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530391 if (op->addr.nbytes +
392 (op->dummy.nbytes ? 1 : 0) +
393 (op->data.nbytes ? 1 : 0) > 6)
394 return false;
Yuan Yaod7193262016-03-15 14:36:42 +0800395
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530396 /* Max 64 dummy clock cycles supported */
397 if (op->dummy.nbytes &&
398 (op->dummy.nbytes * 8 / op->dummy.buswidth > 64))
399 return false;
Yuan Yaod7193262016-03-15 14:36:42 +0800400
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530401 /* Max data length, check controller limits and alignment */
402 if (op->data.dir == SPI_MEM_DATA_IN &&
403 (op->data.nbytes > q->devtype_data->ahb_buf_size ||
404 (op->data.nbytes > q->devtype_data->rxfifo - 4 &&
405 !IS_ALIGNED(op->data.nbytes, 8))))
406 return false;
407
408 if (op->data.dir == SPI_MEM_DATA_OUT &&
409 op->data.nbytes > q->devtype_data->txfifo)
410 return false;
411
412 return true;
Alison Wangc7410e32014-05-06 09:13:01 +0800413}
414
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530415static void fsl_qspi_prepare_lut(struct fsl_qspi *q,
416 const struct spi_mem_op *op)
Peng Fan1c5f9662015-01-08 10:40:20 +0800417{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530418 void __iomem *base = q->iobase;
419 u32 lutval[4] = {};
420 int lutidx = 1, i;
Peng Fan1c5f9662015-01-08 10:40:20 +0800421
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530422 lutval[0] |= LUT_DEF(0, LUT_CMD, LUT_PAD(op->cmd.buswidth),
423 op->cmd.opcode);
Peng Fan1c5f9662015-01-08 10:40:20 +0800424
Ye Lid7e3c9a2020-06-09 00:59:06 -0700425 if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) {
426 if (op->addr.nbytes) {
427 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_ADDR,
428 LUT_PAD(op->addr.buswidth),
429 (op->addr.nbytes == 4) ? 0x20 : 0x18);
430 lutidx++;
431 }
432 } else {
433 /*
434 * For some unknown reason, using LUT_ADDR doesn't work in some
435 * cases (at least with only one byte long addresses), so
436 * let's use LUT_MODE to write the address bytes one by one
437 */
438 for (i = 0; i < op->addr.nbytes; i++) {
439 u8 addrbyte = op->addr.val >> (8 * (op->addr.nbytes - i - 1));
Peng Fan1c5f9662015-01-08 10:40:20 +0800440
Ye Lid7e3c9a2020-06-09 00:59:06 -0700441 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_MODE,
442 LUT_PAD(op->addr.buswidth),
443 addrbyte);
444 lutidx++;
445 }
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530446 }
Peng Fan1c5f9662015-01-08 10:40:20 +0800447
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530448 if (op->dummy.nbytes) {
449 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_DUMMY,
450 LUT_PAD(op->dummy.buswidth),
451 op->dummy.nbytes * 8 /
452 op->dummy.buswidth);
453 lutidx++;
454 }
Peng Fan1c5f9662015-01-08 10:40:20 +0800455
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530456 if (op->data.nbytes) {
457 lutval[lutidx / 2] |= LUT_DEF(lutidx,
458 op->data.dir == SPI_MEM_DATA_IN ?
459 LUT_FSL_READ : LUT_FSL_WRITE,
460 LUT_PAD(op->data.buswidth),
461 0);
462 lutidx++;
463 }
Peng Fan1c5f9662015-01-08 10:40:20 +0800464
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530465 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_STOP, 0, 0);
Peng Fan1c5f9662015-01-08 10:40:20 +0800466
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530467 /* unlock LUT */
468 qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
469 qspi_writel(q, QUADSPI_LCKER_UNLOCK, q->iobase + QUADSPI_LCKCR);
Peng Fan1c5f9662015-01-08 10:40:20 +0800470
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530471 dev_dbg(q->dev, "CMD[%x] lutval[0:%x \t 1:%x \t 2:%x \t 3:%x]\n",
472 op->cmd.opcode, lutval[0], lutval[1], lutval[2], lutval[3]);
Peng Fan1c5f9662015-01-08 10:40:20 +0800473
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530474 /* fill LUT */
475 for (i = 0; i < ARRAY_SIZE(lutval); i++)
476 qspi_writel(q, lutval[i], base + QUADSPI_LUT_REG(i));
Ye Li416d2ec2019-08-14 11:31:27 +0000477
Ye Lid7e3c9a2020-06-09 00:59:06 -0700478 if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) {
479 if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN &&
480 op->addr.nbytes) {
481 for (i = 0; i < ARRAY_SIZE(lutval); i++)
482 qspi_writel(q, lutval[i], base + QUADSPI_AHB_LUT_REG(i));
483 }
484 }
485
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530486 /* lock LUT */
487 qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
488 qspi_writel(q, QUADSPI_LCKER_LOCK, q->iobase + QUADSPI_LCKCR);
Peng Fan1c5f9662015-01-08 10:40:20 +0800489}
490
491/*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530492 * If we have changed the content of the flash by writing or erasing, or if we
493 * read from flash with a different offset into the page buffer, we need to
494 * invalidate the AHB buffer. If we do not do so, we may read out the wrong
495 * data. The spec tells us reset the AHB domain and Serial Flash domain at
496 * the same time.
Peng Fan1c5f9662015-01-08 10:40:20 +0800497 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530498static void fsl_qspi_invalidate(struct fsl_qspi *q)
Peng Fan1c5f9662015-01-08 10:40:20 +0800499{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530500 u32 reg;
Peng Fan1c5f9662015-01-08 10:40:20 +0800501
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530502 reg = qspi_readl(q, q->iobase + QUADSPI_MCR);
503 reg |= QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK;
504 qspi_writel(q, reg, q->iobase + QUADSPI_MCR);
Peng Fan1c5f9662015-01-08 10:40:20 +0800505
506 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530507 * The minimum delay : 1 AHB + 2 SFCK clocks.
508 * Delay 1 us is enough.
Peng Fan1c5f9662015-01-08 10:40:20 +0800509 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530510 udelay(1);
Peng Fan1c5f9662015-01-08 10:40:20 +0800511
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530512 reg &= ~(QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK);
513 qspi_writel(q, reg, q->iobase + QUADSPI_MCR);
Peng Fan1c5f9662015-01-08 10:40:20 +0800514}
Peng Fan1c5f9662015-01-08 10:40:20 +0800515
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530516static void fsl_qspi_select_mem(struct fsl_qspi *q, struct spi_slave *slave)
Peng Fan3a344482015-01-04 17:07:14 +0800517{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530518 struct dm_spi_slave_platdata *plat =
519 dev_get_parent_platdata(slave->dev);
Peng Fan3a344482015-01-04 17:07:14 +0800520
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530521 if (q->selected == plat->cs)
522 return;
Alexander Stein283eb4a2017-06-01 09:32:19 +0200523
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530524 q->selected = plat->cs;
525 fsl_qspi_invalidate(q);
Peng Fan3a344482015-01-04 17:07:14 +0800526}
Alison Wangc7410e32014-05-06 09:13:01 +0800527
Ye Lid7e3c9a2020-06-09 00:59:06 -0700528static u32 fsl_qspi_memsize_per_cs(struct fsl_qspi *q)
529{
530 if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) {
531 if (needs_single_bus(q))
532 return q->memmap_size / 2;
533 else
534 return q->memmap_size / 4;
535 } else {
536 return ALIGN(q->devtype_data->ahb_buf_size, 0x400);
537 }
538}
539
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530540static void fsl_qspi_read_ahb(struct fsl_qspi *q, const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800541{
Ye Lid7e3c9a2020-06-09 00:59:06 -0700542 void __iomem *ahb_read_addr = q->ahb_addr;
543
544 if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) {
545 if (op->addr.nbytes)
546 ahb_read_addr += op->addr.val;
547 }
548
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530549 memcpy_fromio(op->data.buf.in,
Ye Lid7e3c9a2020-06-09 00:59:06 -0700550 ahb_read_addr + q->selected * fsl_qspi_memsize_per_cs(q),
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530551 op->data.nbytes);
Alison Wangc7410e32014-05-06 09:13:01 +0800552}
553
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530554static void fsl_qspi_fill_txfifo(struct fsl_qspi *q,
555 const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800556{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530557 void __iomem *base = q->iobase;
558 int i;
559 u32 val;
Alison Wangc7410e32014-05-06 09:13:01 +0800560
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530561 for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 4); i += 4) {
562 memcpy(&val, op->data.buf.out + i, 4);
563 val = fsl_qspi_endian_xchg(q, val);
564 qspi_writel(q, val, base + QUADSPI_TBDR);
565 }
Alison Wangc7410e32014-05-06 09:13:01 +0800566
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530567 if (i < op->data.nbytes) {
568 memcpy(&val, op->data.buf.out + i, op->data.nbytes - i);
569 val = fsl_qspi_endian_xchg(q, val);
570 qspi_writel(q, val, base + QUADSPI_TBDR);
Alison Wangc7410e32014-05-06 09:13:01 +0800571 }
572
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530573 if (needs_fill_txfifo(q)) {
574 for (i = op->data.nbytes; i < 16; i += 4)
575 qspi_writel(q, 0, base + QUADSPI_TBDR);
576 }
Alison Wangc7410e32014-05-06 09:13:01 +0800577}
578
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530579static void fsl_qspi_read_rxfifo(struct fsl_qspi *q,
580 const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800581{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530582 void __iomem *base = q->iobase;
583 int i;
584 u8 *buf = op->data.buf.in;
585 u32 val;
Alison Wangc7410e32014-05-06 09:13:01 +0800586
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530587 for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 4); i += 4) {
588 val = qspi_readl(q, base + QUADSPI_RBDR(i / 4));
589 val = fsl_qspi_endian_xchg(q, val);
590 memcpy(buf + i, &val, 4);
Alison Wangc7410e32014-05-06 09:13:01 +0800591 }
592
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530593 if (i < op->data.nbytes) {
594 val = qspi_readl(q, base + QUADSPI_RBDR(i / 4));
595 val = fsl_qspi_endian_xchg(q, val);
596 memcpy(buf + i, &val, op->data.nbytes - i);
Alison Wangc7410e32014-05-06 09:13:01 +0800597 }
Alison Wangc7410e32014-05-06 09:13:01 +0800598}
599
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530600static int fsl_qspi_readl_poll_tout(struct fsl_qspi *q, void __iomem *base,
601 u32 mask, u32 delay_us, u32 timeout_us)
Alison Wangc7410e32014-05-06 09:13:01 +0800602{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530603 u32 reg;
Alexander Stein283eb4a2017-06-01 09:32:19 +0200604
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530605 if (!q->devtype_data->little_endian)
606 mask = (u32)cpu_to_be32(mask);
Alison Wangc7410e32014-05-06 09:13:01 +0800607
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530608 return readl_poll_timeout(base, reg, !(reg & mask), timeout_us);
Alison Wangc7410e32014-05-06 09:13:01 +0800609}
610
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530611static int fsl_qspi_do_op(struct fsl_qspi *q, const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800612{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530613 void __iomem *base = q->iobase;
614 int err = 0;
Alison Wangc7410e32014-05-06 09:13:01 +0800615
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530616 /*
617 * Always start the sequence at the same index since we update
618 * the LUT at each exec_op() call. And also specify the DATA
619 * length, since it's has not been specified in the LUT.
620 */
621 qspi_writel(q, op->data.nbytes | QUADSPI_IPCR_SEQID(SEQID_LUT),
622 base + QUADSPI_IPCR);
Alison Wangc7410e32014-05-06 09:13:01 +0800623
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530624 /* wait for the controller being ready */
625 err = fsl_qspi_readl_poll_tout(q, base + QUADSPI_SR,
626 (QUADSPI_SR_IP_ACC_MASK |
627 QUADSPI_SR_AHB_ACC_MASK),
628 10, 1000);
Alison Wangc7410e32014-05-06 09:13:01 +0800629
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530630 if (!err && op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN)
631 fsl_qspi_read_rxfifo(q, op);
Alison Wangc7410e32014-05-06 09:13:01 +0800632
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530633 return err;
Alison Wangc7410e32014-05-06 09:13:01 +0800634}
635
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530636static int fsl_qspi_exec_op(struct spi_slave *slave,
637 const struct spi_mem_op *op)
Alison Wangc7410e32014-05-06 09:13:01 +0800638{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530639 struct fsl_qspi *q = dev_get_priv(slave->dev->parent);
640 void __iomem *base = q->iobase;
641 u32 addr_offset = 0;
642 int err = 0;
Alison Wangc7410e32014-05-06 09:13:01 +0800643
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530644 /* wait for the controller being ready */
645 fsl_qspi_readl_poll_tout(q, base + QUADSPI_SR, (QUADSPI_SR_IP_ACC_MASK |
646 QUADSPI_SR_AHB_ACC_MASK), 10, 1000);
Alexander Stein283eb4a2017-06-01 09:32:19 +0200647
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530648 fsl_qspi_select_mem(q, slave);
Alison Wangc7410e32014-05-06 09:13:01 +0800649
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530650 if (needs_amba_base_offset(q))
651 addr_offset = q->memmap_phy;
Alison Wangc7410e32014-05-06 09:13:01 +0800652
Ye Lid7e3c9a2020-06-09 00:59:06 -0700653 if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) {
654 if (op->addr.nbytes)
655 addr_offset += op->addr.val;
656 }
657
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530658 qspi_writel(q,
Ye Lid7e3c9a2020-06-09 00:59:06 -0700659 q->selected * fsl_qspi_memsize_per_cs(q) + addr_offset,
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530660 base + QUADSPI_SFAR);
Alison Wangc7410e32014-05-06 09:13:01 +0800661
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530662 qspi_writel(q, qspi_readl(q, base + QUADSPI_MCR) |
663 QUADSPI_MCR_CLR_RXF_MASK | QUADSPI_MCR_CLR_TXF_MASK,
664 base + QUADSPI_MCR);
Alison Wangc7410e32014-05-06 09:13:01 +0800665
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530666 qspi_writel(q, QUADSPI_SPTRCLR_BFPTRC | QUADSPI_SPTRCLR_IPPTRC,
667 base + QUADSPI_SPTRCLR);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800668
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530669 fsl_qspi_prepare_lut(q, op);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800670
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530671 /*
672 * If we have large chunks of data, we read them through the AHB bus
673 * by accessing the mapped memory. In all other cases we use
674 * IP commands to access the flash.
675 */
676 if (op->data.nbytes > (q->devtype_data->rxfifo - 4) &&
677 op->data.dir == SPI_MEM_DATA_IN) {
678 fsl_qspi_read_ahb(q, op);
679 } else {
680 qspi_writel(q, QUADSPI_RBCT_WMRK_MASK |
681 QUADSPI_RBCT_RXBRD_USEIPS, base + QUADSPI_RBCT);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800682
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530683 if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT)
684 fsl_qspi_fill_txfifo(q, op);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800685
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530686 err = fsl_qspi_do_op(q, op);
687 }
688
689 /* Invalidate the data in the AHB buffer. */
690 fsl_qspi_invalidate(q);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800691
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530692 return err;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800693}
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800694
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530695static int fsl_qspi_adjust_op_size(struct spi_slave *slave,
696 struct spi_mem_op *op)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800697{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530698 struct fsl_qspi *q = dev_get_priv(slave->dev->parent);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800699
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530700 if (op->data.dir == SPI_MEM_DATA_OUT) {
701 if (op->data.nbytes > q->devtype_data->txfifo)
702 op->data.nbytes = q->devtype_data->txfifo;
703 } else {
704 if (op->data.nbytes > q->devtype_data->ahb_buf_size)
705 op->data.nbytes = q->devtype_data->ahb_buf_size;
706 else if (op->data.nbytes > (q->devtype_data->rxfifo - 4))
707 op->data.nbytes = ALIGN_DOWN(op->data.nbytes, 8);
708 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800709
710 return 0;
711}
712
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530713static int fsl_qspi_default_setup(struct fsl_qspi *q)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800714{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530715 void __iomem *base = q->iobase;
Ye Lid7e3c9a2020-06-09 00:59:06 -0700716 u32 reg, addr_offset = 0, memsize_cs;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800717
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530718 /* Reset the module */
719 qspi_writel(q, QUADSPI_MCR_SWRSTSD_MASK | QUADSPI_MCR_SWRSTHD_MASK,
720 base + QUADSPI_MCR);
721 udelay(1);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800722
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530723 /* Disable the module */
724 qspi_writel(q, QUADSPI_MCR_MDIS_MASK | QUADSPI_MCR_RESERVED_MASK,
725 base + QUADSPI_MCR);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800726
Yuan Yaoae412392016-03-15 14:36:40 +0800727 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530728 * Previous boot stages (BootROM, bootloader) might have used DDR
729 * mode and did not clear the TDH bits. As we currently use SDR mode
730 * only, clear the TDH bits if necessary.
Yuan Yaoae412392016-03-15 14:36:40 +0800731 */
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530732 if (needs_tdh_setting(q))
733 qspi_writel(q, qspi_readl(q, base + QUADSPI_FLSHCR) &
734 ~QUADSPI_FLSHCR_TDH_MASK,
735 base + QUADSPI_FLSHCR);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800736
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530737 reg = qspi_readl(q, base + QUADSPI_SMPR);
738 qspi_writel(q, reg & ~(QUADSPI_SMPR_FSDLY_MASK
739 | QUADSPI_SMPR_FSPHS_MASK
740 | QUADSPI_SMPR_HSENA_MASK
741 | QUADSPI_SMPR_DDRSMP_MASK), base + QUADSPI_SMPR);
Ye Li007b6042019-08-14 11:31:36 +0000742
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530743 /* We only use the buffer3 for AHB read */
744 qspi_writel(q, 0, base + QUADSPI_BUF0IND);
745 qspi_writel(q, 0, base + QUADSPI_BUF1IND);
746 qspi_writel(q, 0, base + QUADSPI_BUF2IND);
Suresh Gupta4945b872017-08-30 20:06:33 +0530747
Ye Lid7e3c9a2020-06-09 00:59:06 -0700748 if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP))
749 qspi_writel(q, QUADSPI_BFGENCR_SEQID(SEQID_LUT_AHB),
750 q->iobase + QUADSPI_BFGENCR);
751 else
752 qspi_writel(q, QUADSPI_BFGENCR_SEQID(SEQID_LUT),
753 q->iobase + QUADSPI_BFGENCR);
754
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530755 qspi_writel(q, QUADSPI_RBCT_WMRK_MASK, base + QUADSPI_RBCT);
756 qspi_writel(q, QUADSPI_BUF3CR_ALLMST_MASK |
757 QUADSPI_BUF3CR_ADATSZ(q->devtype_data->ahb_buf_size / 8),
758 base + QUADSPI_BUF3CR);
Suresh Gupta4945b872017-08-30 20:06:33 +0530759
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530760 if (needs_amba_base_offset(q))
761 addr_offset = q->memmap_phy;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800762
Yuan Yaob4bfe102016-03-15 14:36:41 +0800763 /*
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530764 * In HW there can be a maximum of four chips on two buses with
765 * two chip selects on each bus. We use four chip selects in SW
766 * to differentiate between the four chips.
767 * We use ahb_buf_size for each chip and set SFA1AD, SFA2AD, SFB1AD,
768 * SFB2AD accordingly.
Yuan Yaob4bfe102016-03-15 14:36:41 +0800769 */
Ye Lid7e3c9a2020-06-09 00:59:06 -0700770 memsize_cs = fsl_qspi_memsize_per_cs(q);
771 qspi_writel(q, memsize_cs + addr_offset,
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530772 base + QUADSPI_SFA1AD);
Ye Lid7e3c9a2020-06-09 00:59:06 -0700773 qspi_writel(q, memsize_cs * 2 + addr_offset,
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530774 base + QUADSPI_SFA2AD);
Ye Lid7e3c9a2020-06-09 00:59:06 -0700775 if (!needs_single_bus(q)) {
776 qspi_writel(q, memsize_cs * 3 + addr_offset,
777 base + QUADSPI_SFB1AD);
778 qspi_writel(q, memsize_cs * 4 + addr_offset,
779 base + QUADSPI_SFB2AD);
780 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800781
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530782 q->selected = -1;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800783
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530784 /* Enable the module */
785 qspi_writel(q, QUADSPI_MCR_RESERVED_MASK | QUADSPI_MCR_END_CFG_MASK,
786 base + QUADSPI_MCR);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800787 return 0;
788}
789
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530790static const struct spi_controller_mem_ops fsl_qspi_mem_ops = {
791 .adjust_op_size = fsl_qspi_adjust_op_size,
792 .supports_op = fsl_qspi_supports_op,
793 .exec_op = fsl_qspi_exec_op,
794};
795
796static int fsl_qspi_probe(struct udevice *bus)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800797{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530798 struct dm_spi_bus *dm_bus = bus->uclass_priv;
799 struct fsl_qspi *q = dev_get_priv(bus);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800800 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700801 int node = dev_of_offset(bus);
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530802 struct fdt_resource res;
803 int ret;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800804
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530805 q->dev = bus;
806 q->devtype_data = (struct fsl_qspi_devtype_data *)
807 dev_get_driver_data(bus);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800808
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530809 /* find the resources */
810 ret = fdt_get_named_resource(blob, node, "reg", "reg-names", "QuadSPI",
811 &res);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800812 if (ret) {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530813 dev_err(bus, "Can't get regs base addresses(ret = %d)!\n", ret);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800814 return -ENOMEM;
815 }
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530816
817 q->iobase = map_physmem(res.start, res.end - res.start, MAP_NOCACHE);
818
Yuan Yaoae412392016-03-15 14:36:40 +0800819 ret = fdt_get_named_resource(blob, node, "reg", "reg-names",
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530820 "QuadSPI-memory", &res);
Yuan Yaoae412392016-03-15 14:36:40 +0800821 if (ret) {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530822 dev_err(bus, "Can't get AMBA base addresses(ret = %d)!\n", ret);
Yuan Yaoae412392016-03-15 14:36:40 +0800823 return -ENOMEM;
824 }
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800825
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530826 q->ahb_addr = map_physmem(res.start, res.end - res.start, MAP_NOCACHE);
827 q->memmap_phy = res.start;
Ye Lid7e3c9a2020-06-09 00:59:06 -0700828 q->memmap_size = res.end - res.start;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800829
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530830 dm_bus->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
831 66000000);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800832
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530833 fsl_qspi_default_setup(q);
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800834
835 return 0;
836}
837
838static int fsl_qspi_xfer(struct udevice *dev, unsigned int bitlen,
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530839 const void *dout, void *din, unsigned long flags)
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800840{
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530841 return 0;
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800842}
843
844static int fsl_qspi_claim_bus(struct udevice *dev)
845{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800846 return 0;
847}
848
849static int fsl_qspi_release_bus(struct udevice *dev)
850{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800851 return 0;
852}
853
854static int fsl_qspi_set_speed(struct udevice *bus, uint speed)
855{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800856 return 0;
857}
858
859static int fsl_qspi_set_mode(struct udevice *bus, uint mode)
860{
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800861 return 0;
862}
863
864static const struct dm_spi_ops fsl_qspi_ops = {
865 .claim_bus = fsl_qspi_claim_bus,
866 .release_bus = fsl_qspi_release_bus,
867 .xfer = fsl_qspi_xfer,
868 .set_speed = fsl_qspi_set_speed,
869 .set_mode = fsl_qspi_set_mode,
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530870 .mem_ops = &fsl_qspi_mem_ops,
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800871};
872
873static const struct udevice_id fsl_qspi_ids[] = {
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530874 { .compatible = "fsl,vf610-qspi", .data = (ulong)&vybrid_data, },
875 { .compatible = "fsl,imx6sx-qspi", .data = (ulong)&imx6sx_data, },
876 { .compatible = "fsl,imx6ul-qspi", .data = (ulong)&imx6ul_data, },
877 { .compatible = "fsl,imx7d-qspi", .data = (ulong)&imx7d_data, },
Ye Lie4d39a02020-06-09 00:59:05 -0700878 { .compatible = "fsl,imx7ulp-qspi", .data = (ulong)&imx7ulp_data, },
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530879 { .compatible = "fsl,ls1021a-qspi", .data = (ulong)&ls1021a_data, },
880 { .compatible = "fsl,ls1088a-qspi", .data = (ulong)&ls1088a_data, },
881 { .compatible = "fsl,ls2080a-qspi", .data = (ulong)&ls2080a_data, },
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800882 { }
883};
884
885U_BOOT_DRIVER(fsl_qspi) = {
886 .name = "fsl_qspi",
887 .id = UCLASS_SPI,
888 .of_match = fsl_qspi_ids,
889 .ops = &fsl_qspi_ops,
Kuldeep Singhd8429a12020-02-20 22:57:52 +0530890 .priv_auto_alloc_size = sizeof(struct fsl_qspi),
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800891 .probe = fsl_qspi_probe,
Haikun.Wang@freescale.com221f2e12015-04-01 11:10:40 +0800892};