blob: e715f9838f1ff1dcd362d7ef1a8dfef2866ee246 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kuldeep Singh91afd362020-02-20 22:57:52 +05302
Alison Wang6b57ff62014-05-06 09:13:01 +08003/*
Kuldeep Singh91afd362020-02-20 22:57:52 +05304 * Freescale QuadSPI driver.
Alison Wang6b57ff62014-05-06 09:13:01 +08005 *
Kuldeep Singh91afd362020-02-20 22:57:52 +05306 * 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
11 *
12 * 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 Wang6b57ff62014-05-06 09:13:01 +080024 */
25
26#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060027#include <log.h>
Alison Wang6b57ff62014-05-06 09:13:01 +080028#include <asm/io.h>
Simon Glasscd93d622020-05-10 11:40:13 -060029#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060030#include <linux/delay.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060031#include <linux/libfdt.h>
32#include <linux/sizes.h>
33#include <linux/iopoll.h>
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +080034#include <dm.h>
Kuldeep Singh91afd362020-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 Wang6b57ff62014-05-06 09:13:01 +080040
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +080041DECLARE_GLOBAL_DATA_PTR;
42
Kuldeep Singh91afd362020-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 Wang6b57ff62014-05-06 09:13:01 +080049
Kuldeep Singh91afd362020-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 Wang6b57ff62014-05-06 09:13:01 +080060
Kuldeep Singh91afd362020-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 Wang6b57ff62014-05-06 09:13:01 +080067
Kuldeep Singh91afd362020-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 Wang6b57ff62014-05-06 09:13:01 +080072
Kuldeep Singh91afd362020-02-20 22:57:52 +053073#define QUADSPI_BFGENCR 0x20
74#define QUADSPI_BFGENCR_SEQID(x) ((x) << 12)
Peng Fana2358782015-01-04 17:07:14 +080075
Kuldeep Singh91afd362020-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 Fana2358782015-01-04 17:07:14 +080080
Kuldeep Singh91afd362020-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 Yaofebffe82016-03-15 14:36:42 +080086
Kuldeep Singh91afd362020-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 Wang6b57ff62014-05-06 09:13:01 +080090
Kuldeep Singh91afd362020-02-20 22:57:52 +053091#define QUADSPI_TBDR 0x154
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +080092
Kuldeep Singh91afd362020-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)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +080096
Kuldeep Singh91afd362020-02-20 22:57:52 +053097#define QUADSPI_FR 0x160
98#define QUADSPI_FR_TFF_MASK BIT(0)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +080099
Kuldeep Singh91afd362020-02-20 22:57:52 +0530100#define QUADSPI_RSER 0x164
101#define QUADSPI_RSER_TFIE BIT(0)
102
103#define QUADSPI_SPTRCLR 0x16c
104#define QUADSPI_SPTRCLR_IPPTRC BIT(8)
105#define QUADSPI_SPTRCLR_BFPTRC BIT(0)
106
107#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))
112
113#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.
146 *
147 * 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).
150 */
151#define LUT_PAD(x) (fls(x) - 1)
152
153/*
154 * Macro for constructing the LUT entries with the following
155 * register layout:
156 *
157 * ---------------------------------------------------
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 */
Ye Lice7575a2019-08-14 11:31:36 +0000165#define QUADSPI_QUIRK_SWAP_ENDIAN BIT(0)
166
Kuldeep Singh91afd362020-02-20 22:57:52 +0530167/* 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.
189 */
190#define QUADSPI_QUIRK_USE_TDH_SETTING BIT(5)
Ye Lice7575a2019-08-14 11:31:36 +0000191
192struct fsl_qspi_devtype_data {
Kuldeep Singh91afd362020-02-20 22:57:52 +0530193 unsigned int rxfifo;
194 unsigned int txfifo;
195 unsigned int ahb_buf_size;
196 unsigned int quirks;
197 bool little_endian;
Alison Wang6b57ff62014-05-06 09:13:01 +0800198};
199
Ye Lice7575a2019-08-14 11:31:36 +0000200static const struct fsl_qspi_devtype_data vybrid_data = {
Kuldeep Singh91afd362020-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 Lice7575a2019-08-14 11:31:36 +0000206};
207
208static const struct fsl_qspi_devtype_data imx6sx_data = {
Kuldeep Singh91afd362020-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 Lice7575a2019-08-14 11:31:36 +0000214};
215
Kuldeep Singh91afd362020-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 Lice7575a2019-08-14 11:31:36 +0000223};
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800224
Kuldeep Singh91afd362020-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 Li9699fb42019-08-14 11:31:40 +0000232};
233
Kuldeep Singh91afd362020-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.com5bc48302015-04-01 11:10:40 +0800268{
Kuldeep Singh91afd362020-02-20 22:57:52 +0530269 return q->devtype_data->quirks & QUADSPI_QUIRK_SWAP_ENDIAN;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800270}
271
Kuldeep Singh91afd362020-02-20 22:57:52 +0530272static inline int needs_4x_clock(struct fsl_qspi *q)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800273{
Kuldeep Singh91afd362020-02-20 22:57:52 +0530274 return q->devtype_data->quirks & QUADSPI_QUIRK_4X_INT_CLK;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800275}
Alison Wang6b57ff62014-05-06 09:13:01 +0800276
Kuldeep Singh91afd362020-02-20 22:57:52 +0530277static inline int needs_fill_txfifo(struct fsl_qspi *q)
Rajat Srivastava1f553562018-03-22 13:30:55 +0530278{
Kuldeep Singh91afd362020-02-20 22:57:52 +0530279 return q->devtype_data->quirks & QUADSPI_QUIRK_TKT253890;
Rajat Srivastava1f553562018-03-22 13:30:55 +0530280}
281
Kuldeep Singh91afd362020-02-20 22:57:52 +0530282static inline int needs_wakeup_wait_mode(struct fsl_qspi *q)
Alison Wang6b57ff62014-05-06 09:13:01 +0800283{
Kuldeep Singh91afd362020-02-20 22:57:52 +0530284 return q->devtype_data->quirks & QUADSPI_QUIRK_TKT245618;
Alison Wang6b57ff62014-05-06 09:13:01 +0800285}
286
Kuldeep Singh91afd362020-02-20 22:57:52 +0530287static inline int needs_amba_base_offset(struct fsl_qspi *q)
Alison Wang6b57ff62014-05-06 09:13:01 +0800288{
Kuldeep Singh91afd362020-02-20 22:57:52 +0530289 return !(q->devtype_data->quirks & QUADSPI_QUIRK_BASE_INTERNAL);
Alison Wang6b57ff62014-05-06 09:13:01 +0800290}
291
Kuldeep Singh91afd362020-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}
296
Peng Fan5f7f70c2015-01-08 10:40:20 +0800297/*
Kuldeep Singh91afd362020-02-20 22:57:52 +0530298 * An IC bug makes it necessary to rearrange the 32-bit data.
299 * Later chips, such as IMX6SLX, have fixed this bug.
Peng Fan5f7f70c2015-01-08 10:40:20 +0800300 */
Kuldeep Singh91afd362020-02-20 22:57:52 +0530301static inline u32 fsl_qspi_endian_xchg(struct fsl_qspi *q, u32 a)
Peng Fan5f7f70c2015-01-08 10:40:20 +0800302{
Kuldeep Singh91afd362020-02-20 22:57:52 +0530303 return needs_swap_endian(q) ? __swab32(a) : a;
304}
305
306/*
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);
317 else
318 out_be32(addr, val);
319}
320
321static u32 qspi_readl(struct fsl_qspi *q, void __iomem *addr)
322{
323 if (q->devtype_data->little_endian)
324 return in_le32(addr);
325
326 return in_be32(addr);
327}
328
329static 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 }
337
338 return -ENOTSUPP;
339}
340
341static 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;
346
347 ret = fsl_qspi_check_buswidth(q, op->cmd.buswidth);
348
349 if (op->addr.nbytes)
350 ret |= fsl_qspi_check_buswidth(q, op->addr.buswidth);
351
352 if (op->dummy.nbytes)
353 ret |= fsl_qspi_check_buswidth(q, op->dummy.buswidth);
354
355 if (op->data.nbytes)
356 ret |= fsl_qspi_check_buswidth(q, op->data.buswidth);
357
358 if (ret)
359 return false;
360
361 /*
362 * The number of instructions needed for the op, needs
363 * to fit into a single LUT entry.
364 */
365 if (op->addr.nbytes +
366 (op->dummy.nbytes ? 1 : 0) +
367 (op->data.nbytes ? 1 : 0) > 6)
368 return false;
369
370 /* Max 64 dummy clock cycles supported */
371 if (op->dummy.nbytes &&
372 (op->dummy.nbytes * 8 / op->dummy.buswidth > 64))
373 return false;
374
375 /* 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;
387}
388
389static void fsl_qspi_prepare_lut(struct fsl_qspi *q,
390 const struct spi_mem_op *op)
391{
392 void __iomem *base = q->iobase;
393 u32 lutval[4] = {};
394 int lutidx = 1, i;
395
396 lutval[0] |= LUT_DEF(0, LUT_CMD, LUT_PAD(op->cmd.buswidth),
397 op->cmd.opcode);
398
399 /*
400 * 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
403 */
404 for (i = 0; i < op->addr.nbytes; i++) {
405 u8 addrbyte = op->addr.val >> (8 * (op->addr.nbytes - i - 1));
406
407 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_MODE,
408 LUT_PAD(op->addr.buswidth),
409 addrbyte);
410 lutidx++;
411 }
412
413 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 }
420
421 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 }
429
430 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_STOP, 0, 0);
431
432 /* unlock LUT */
433 qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
434 qspi_writel(q, QUADSPI_LCKER_UNLOCK, q->iobase + QUADSPI_LCKCR);
435
436 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]);
438
439 /* fill LUT */
440 for (i = 0; i < ARRAY_SIZE(lutval); i++)
441 qspi_writel(q, lutval[i], base + QUADSPI_LUT_REG(i));
442
443 /* lock LUT */
444 qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
445 qspi_writel(q, QUADSPI_LCKER_LOCK, q->iobase + QUADSPI_LCKCR);
446}
447
448/*
449 * 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.
454 */
455static void fsl_qspi_invalidate(struct fsl_qspi *q)
456{
Peng Fan5f7f70c2015-01-08 10:40:20 +0800457 u32 reg;
458
Kuldeep Singh91afd362020-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 Fan5f7f70c2015-01-08 10:40:20 +0800462
463 /*
464 * The minimum delay : 1 AHB + 2 SFCK clocks.
465 * Delay 1 us is enough.
466 */
467 udelay(1);
468
Kuldeep Singh91afd362020-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 Fan5f7f70c2015-01-08 10:40:20 +0800471}
472
Kuldeep Singh91afd362020-02-20 22:57:52 +0530473static void fsl_qspi_select_mem(struct fsl_qspi *q, struct spi_slave *slave)
Peng Fan5f7f70c2015-01-08 10:40:20 +0800474{
Kuldeep Singh91afd362020-02-20 22:57:52 +0530475 struct dm_spi_slave_platdata *plat =
476 dev_get_parent_platdata(slave->dev);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800477
Kuldeep Singh91afd362020-02-20 22:57:52 +0530478 if (q->selected == plat->cs)
479 return;
Peng Fan5f7f70c2015-01-08 10:40:20 +0800480
Kuldeep Singh91afd362020-02-20 22:57:52 +0530481 q->selected = plat->cs;
482 fsl_qspi_invalidate(q);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800483}
484
Kuldeep Singh91afd362020-02-20 22:57:52 +0530485static void fsl_qspi_read_ahb(struct fsl_qspi *q, const struct spi_mem_op *op)
Peng Fan5f7f70c2015-01-08 10:40:20 +0800486{
Kuldeep Singh91afd362020-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);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800490}
491
Kuldeep Singh91afd362020-02-20 22:57:52 +0530492static void fsl_qspi_fill_txfifo(struct fsl_qspi *q,
493 const struct spi_mem_op *op)
Peng Fan5f7f70c2015-01-08 10:40:20 +0800494{
Kuldeep Singh91afd362020-02-20 22:57:52 +0530495 void __iomem *base = q->iobase;
Gong Qianyu52070142016-01-26 15:06:40 +0800496 int i;
Kuldeep Singh91afd362020-02-20 22:57:52 +0530497 u32 val;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800498
Kuldeep Singh91afd362020-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);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800503 }
504
Kuldeep Singh91afd362020-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);
509 }
510
511 if (needs_fill_txfifo(q)) {
512 for (i = op->data.nbytes; i < 16; i += 4)
513 qspi_writel(q, 0, base + QUADSPI_TBDR);
514 }
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800515}
516
Kuldeep Singh91afd362020-02-20 22:57:52 +0530517static void fsl_qspi_read_rxfifo(struct fsl_qspi *q,
518 const struct spi_mem_op *op)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800519{
Kuldeep Singh91afd362020-02-20 22:57:52 +0530520 void __iomem *base = q->iobase;
521 int i;
522 u8 *buf = op->data.buf.in;
523 u32 val;
Yuan Yaofebffe82016-03-15 14:36:42 +0800524
Kuldeep Singh91afd362020-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);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800529 }
530
Kuldeep Singh91afd362020-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);
535 }
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800536}
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800537
Kuldeep Singh91afd362020-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)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800540{
Kuldeep Singh91afd362020-02-20 22:57:52 +0530541 u32 reg;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800542
Kuldeep Singh91afd362020-02-20 22:57:52 +0530543 if (!q->devtype_data->little_endian)
544 mask = (u32)cpu_to_be32(mask);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800545
Kuldeep Singh91afd362020-02-20 22:57:52 +0530546 return readl_poll_timeout(base, reg, !(reg & mask), timeout_us);
547}
Alexander Steinbeedbc22015-11-04 09:19:10 +0100548
Kuldeep Singh91afd362020-02-20 22:57:52 +0530549static int fsl_qspi_do_op(struct fsl_qspi *q, const struct spi_mem_op *op)
550{
551 void __iomem *base = q->iobase;
552 int err = 0;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800553
Suresh Gupta10509982017-06-05 14:37:20 +0530554 /*
Kuldeep Singh91afd362020-02-20 22:57:52 +0530555 * 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.
Suresh Gupta10509982017-06-05 14:37:20 +0530558 */
Kuldeep Singh91afd362020-02-20 22:57:52 +0530559 qspi_writel(q, op->data.nbytes | QUADSPI_IPCR_SEQID(SEQID_LUT),
560 base + QUADSPI_IPCR);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800561
Kuldeep Singh91afd362020-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);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800567
Kuldeep Singh91afd362020-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);
570
571 return err;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800572}
573
Kuldeep Singh91afd362020-02-20 22:57:52 +0530574static int fsl_qspi_exec_op(struct spi_slave *slave,
575 const struct spi_mem_op *op)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800576{
Kuldeep Singh91afd362020-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;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800581
Kuldeep Singh91afd362020-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);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800585
Kuldeep Singh91afd362020-02-20 22:57:52 +0530586 fsl_qspi_select_mem(q, slave);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800587
Kuldeep Singh91afd362020-02-20 22:57:52 +0530588 if (needs_amba_base_offset(q))
589 addr_offset = q->memmap_phy;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800590
Kuldeep Singh91afd362020-02-20 22:57:52 +0530591 qspi_writel(q,
592 q->selected * q->devtype_data->ahb_buf_size + addr_offset,
593 base + QUADSPI_SFAR);
Alexander Stein4df24f22017-06-01 09:32:19 +0200594
Kuldeep Singh91afd362020-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);
598
599 qspi_writel(q, QUADSPI_SPTRCLR_BFPTRC | QUADSPI_SPTRCLR_IPPTRC,
600 base + QUADSPI_SPTRCLR);
601
602 fsl_qspi_prepare_lut(q, op);
603
604 /*
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);
615
616 if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT)
617 fsl_qspi_fill_txfifo(q, op);
618
619 err = fsl_qspi_do_op(q, op);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800620 }
621
Kuldeep Singh91afd362020-02-20 22:57:52 +0530622 /* Invalidate the data in the AHB buffer. */
623 fsl_qspi_invalidate(q);
624
625 return err;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800626}
627
Kuldeep Singh91afd362020-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.com5bc48302015-04-01 11:10:40 +0800630{
Kuldeep Singh91afd362020-02-20 22:57:52 +0530631 struct fsl_qspi *q = dev_get_priv(slave->dev->parent);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800632
Kuldeep Singh91afd362020-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);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800641 }
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800642
643 return 0;
644}
645
Kuldeep Singh91afd362020-02-20 22:57:52 +0530646static int fsl_qspi_default_setup(struct fsl_qspi *q)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800647{
Kuldeep Singh91afd362020-02-20 22:57:52 +0530648 void __iomem *base = q->iobase;
649 u32 reg, addr_offset = 0;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800650
Kuldeep Singh91afd362020-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.com5bc48302015-04-01 11:10:40 +0800655
Kuldeep Singh91afd362020-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.com5bc48302015-04-01 11:10:40 +0800659
Kuldeep Singh91afd362020-02-20 22:57:52 +0530660 /*
661 * 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.
664 */
665 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.com5bc48302015-04-01 11:10:40 +0800669
Kuldeep Singh91afd362020-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);
Alison Wang6b57ff62014-05-06 09:13:01 +0800675
Kuldeep Singh91afd362020-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);
Peng Fan5f7f70c2015-01-08 10:40:20 +0800680
Kuldeep Singh91afd362020-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);
687
688 if (needs_amba_base_offset(q))
689 addr_offset = q->memmap_phy;
690
691 /*
692 * 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.
697 */
698 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);
706
707 q->selected = -1;
708
709 /* Enable the module */
710 qspi_writel(q, QUADSPI_MCR_RESERVED_MASK | QUADSPI_MCR_END_CFG_MASK,
711 base + QUADSPI_MCR);
Alison Wang6b57ff62014-05-06 09:13:01 +0800712 return 0;
713}
714
Kuldeep Singh91afd362020-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
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800721static int fsl_qspi_probe(struct udevice *bus)
722{
Kuldeep Singh91afd362020-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.com5bc48302015-04-01 11:10:40 +0800725 const void *blob = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -0700726 int node = dev_of_offset(bus);
Kuldeep Singh91afd362020-02-20 22:57:52 +0530727 struct fdt_resource res;
728 int ret;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800729
Kuldeep Singh91afd362020-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.com5bc48302015-04-01 11:10:40 +0800733
Kuldeep Singh91afd362020-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.com5bc48302015-04-01 11:10:40 +0800737 if (ret) {
Kuldeep Singh91afd362020-02-20 22:57:52 +0530738 dev_err(bus, "Can't get regs base addresses(ret = %d)!\n", ret);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800739 return -ENOMEM;
740 }
741
Kuldeep Singh91afd362020-02-20 22:57:52 +0530742 q->iobase = map_physmem(res.start, res.end - res.start, MAP_NOCACHE);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800743
Kuldeep Singh91afd362020-02-20 22:57:52 +0530744 ret = fdt_get_named_resource(blob, node, "reg", "reg-names",
745 "QuadSPI-memory", &res);
746 if (ret) {
747 dev_err(bus, "Can't get AMBA base addresses(ret = %d)!\n", ret);
748 return -ENOMEM;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800749 }
750
Kuldeep Singh91afd362020-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.com5bc48302015-04-01 11:10:40 +0800753
Kuldeep Singh91afd362020-02-20 22:57:52 +0530754 dm_bus->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
755 66000000);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800756
Kuldeep Singh91afd362020-02-20 22:57:52 +0530757 fsl_qspi_default_setup(q);
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800758
759 return 0;
760}
761
762static int fsl_qspi_xfer(struct udevice *dev, unsigned int bitlen,
Kuldeep Singh91afd362020-02-20 22:57:52 +0530763 const void *dout, void *din, unsigned long flags)
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800764{
Kuldeep Singh91afd362020-02-20 22:57:52 +0530765 return 0;
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800766}
767
768static int fsl_qspi_claim_bus(struct udevice *dev)
769{
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800770 return 0;
771}
772
773static int fsl_qspi_release_bus(struct udevice *dev)
774{
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800775 return 0;
776}
777
778static int fsl_qspi_set_speed(struct udevice *bus, uint speed)
Alison Wang6b57ff62014-05-06 09:13:01 +0800779{
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800780 return 0;
Alison Wang6b57ff62014-05-06 09:13:01 +0800781}
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800782
783static int fsl_qspi_set_mode(struct udevice *bus, uint mode)
784{
Haikun.Wang@freescale.com5bc48302015-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 Singh91afd362020-02-20 22:57:52 +0530794 .mem_ops = &fsl_qspi_mem_ops,
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800795};
796
797static const struct udevice_id fsl_qspi_ids[] = {
Kuldeep Singh91afd362020-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.com5bc48302015-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 Singh91afd362020-02-20 22:57:52 +0530813 .priv_auto_alloc_size = sizeof(struct fsl_qspi),
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800814 .probe = fsl_qspi_probe,
Haikun.Wang@freescale.com5bc48302015-04-01 11:10:40 +0800815};