blob: a2fab7ad0a255bdc909a4fc994008315a2ac35d0 [file] [log] [blame]
Michael Walle383fded2019-12-18 00:09:58 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * NXP FlexSPI(FSPI) controller driver.
4 *
5 * Copyright (c) 2019 Michael Walle <michael@walle.cc>
6 * Copyright (c) 2019 NXP
7 *
8 * This driver was originally ported from the linux kernel v5.4-rc3, which had
9 * the following notes:
10 *
11 * FlexSPI is a flexsible SPI host controller which supports two SPI
12 * channels and up to 4 external devices. Each channel supports
13 * Single/Dual/Quad/Octal mode data transfer (1/2/4/8 bidirectional
14 * data lines).
15 *
16 * FlexSPI controller is driven by the LUT(Look-up Table) registers
17 * LUT registers are a look-up-table for sequences of instructions.
18 * A valid sequence consists of four LUT registers.
19 * Maximum 32 LUT sequences can be programmed simultaneously.
20 *
21 * LUTs are being created at run-time based on the commands passed
22 * from the spi-mem framework, thus using single LUT index.
23 *
24 * Software triggered Flash read/write access by IP Bus.
25 *
26 * Memory mapped read access by AHB Bus.
27 *
28 * Based on SPI MEM interface and spi-fsl-qspi.c driver.
29 *
30 * Author:
31 * Yogesh Narayan Gaur <yogeshnarayan.gaur@nxp.com>
32 * Boris Brezillon <bbrezillon@kernel.org>
33 * Frieder Schrempf <frieder.schrempf@kontron.de>
34 */
35
36#include <common.h>
37#include <asm/io.h>
38#include <malloc.h>
39#include <spi.h>
40#include <spi-mem.h>
41#include <dm.h>
42#include <clk.h>
43#include <linux/kernel.h>
44#include <linux/sizes.h>
45#include <linux/iopoll.h>
46#include <linux/bug.h>
47
48/*
49 * The driver only uses one single LUT entry, that is updated on
50 * each call of exec_op(). Index 0 is preset at boot with a basic
51 * read operation, so let's use the last entry (31).
52 */
53#define SEQID_LUT 31
54
55/* Registers used by the driver */
56#define FSPI_MCR0 0x00
57#define FSPI_MCR0_AHB_TIMEOUT(x) ((x) << 24)
58#define FSPI_MCR0_IP_TIMEOUT(x) ((x) << 16)
59#define FSPI_MCR0_LEARN_EN BIT(15)
60#define FSPI_MCR0_SCRFRUN_EN BIT(14)
61#define FSPI_MCR0_OCTCOMB_EN BIT(13)
62#define FSPI_MCR0_DOZE_EN BIT(12)
63#define FSPI_MCR0_HSEN BIT(11)
64#define FSPI_MCR0_SERCLKDIV BIT(8)
65#define FSPI_MCR0_ATDF_EN BIT(7)
66#define FSPI_MCR0_ARDF_EN BIT(6)
67#define FSPI_MCR0_RXCLKSRC(x) ((x) << 4)
68#define FSPI_MCR0_END_CFG(x) ((x) << 2)
69#define FSPI_MCR0_MDIS BIT(1)
70#define FSPI_MCR0_SWRST BIT(0)
71
72#define FSPI_MCR1 0x04
73#define FSPI_MCR1_SEQ_TIMEOUT(x) ((x) << 16)
74#define FSPI_MCR1_AHB_TIMEOUT(x) (x)
75
76#define FSPI_MCR2 0x08
77#define FSPI_MCR2_IDLE_WAIT(x) ((x) << 24)
78#define FSPI_MCR2_SAMEDEVICEEN BIT(15)
79#define FSPI_MCR2_CLRLRPHS BIT(14)
80#define FSPI_MCR2_ABRDATSZ BIT(8)
81#define FSPI_MCR2_ABRLEARN BIT(7)
82#define FSPI_MCR2_ABR_READ BIT(6)
83#define FSPI_MCR2_ABRWRITE BIT(5)
84#define FSPI_MCR2_ABRDUMMY BIT(4)
85#define FSPI_MCR2_ABR_MODE BIT(3)
86#define FSPI_MCR2_ABRCADDR BIT(2)
87#define FSPI_MCR2_ABRRADDR BIT(1)
88#define FSPI_MCR2_ABR_CMD BIT(0)
89
90#define FSPI_AHBCR 0x0c
91#define FSPI_AHBCR_RDADDROPT BIT(6)
92#define FSPI_AHBCR_PREF_EN BIT(5)
93#define FSPI_AHBCR_BUFF_EN BIT(4)
94#define FSPI_AHBCR_CACH_EN BIT(3)
95#define FSPI_AHBCR_CLRTXBUF BIT(2)
96#define FSPI_AHBCR_CLRRXBUF BIT(1)
97#define FSPI_AHBCR_PAR_EN BIT(0)
98
99#define FSPI_INTEN 0x10
100#define FSPI_INTEN_SCLKSBWR BIT(9)
101#define FSPI_INTEN_SCLKSBRD BIT(8)
102#define FSPI_INTEN_DATALRNFL BIT(7)
103#define FSPI_INTEN_IPTXWE BIT(6)
104#define FSPI_INTEN_IPRXWA BIT(5)
105#define FSPI_INTEN_AHBCMDERR BIT(4)
106#define FSPI_INTEN_IPCMDERR BIT(3)
107#define FSPI_INTEN_AHBCMDGE BIT(2)
108#define FSPI_INTEN_IPCMDGE BIT(1)
109#define FSPI_INTEN_IPCMDDONE BIT(0)
110
111#define FSPI_INTR 0x14
112#define FSPI_INTR_SCLKSBWR BIT(9)
113#define FSPI_INTR_SCLKSBRD BIT(8)
114#define FSPI_INTR_DATALRNFL BIT(7)
115#define FSPI_INTR_IPTXWE BIT(6)
116#define FSPI_INTR_IPRXWA BIT(5)
117#define FSPI_INTR_AHBCMDERR BIT(4)
118#define FSPI_INTR_IPCMDERR BIT(3)
119#define FSPI_INTR_AHBCMDGE BIT(2)
120#define FSPI_INTR_IPCMDGE BIT(1)
121#define FSPI_INTR_IPCMDDONE BIT(0)
122
123#define FSPI_LUTKEY 0x18
124#define FSPI_LUTKEY_VALUE 0x5AF05AF0
125
126#define FSPI_LCKCR 0x1C
127
128#define FSPI_LCKER_LOCK 0x1
129#define FSPI_LCKER_UNLOCK 0x2
130
131#define FSPI_BUFXCR_INVALID_MSTRID 0xE
132#define FSPI_AHBRX_BUF0CR0 0x20
133#define FSPI_AHBRX_BUF1CR0 0x24
134#define FSPI_AHBRX_BUF2CR0 0x28
135#define FSPI_AHBRX_BUF3CR0 0x2C
136#define FSPI_AHBRX_BUF4CR0 0x30
137#define FSPI_AHBRX_BUF5CR0 0x34
138#define FSPI_AHBRX_BUF6CR0 0x38
139#define FSPI_AHBRX_BUF7CR0 0x3C
140#define FSPI_AHBRXBUF0CR7_PREF BIT(31)
141
142#define FSPI_AHBRX_BUF0CR1 0x40
143#define FSPI_AHBRX_BUF1CR1 0x44
144#define FSPI_AHBRX_BUF2CR1 0x48
145#define FSPI_AHBRX_BUF3CR1 0x4C
146#define FSPI_AHBRX_BUF4CR1 0x50
147#define FSPI_AHBRX_BUF5CR1 0x54
148#define FSPI_AHBRX_BUF6CR1 0x58
149#define FSPI_AHBRX_BUF7CR1 0x5C
150
151#define FSPI_FLSHA1CR0 0x60
152#define FSPI_FLSHA2CR0 0x64
153#define FSPI_FLSHB1CR0 0x68
154#define FSPI_FLSHB2CR0 0x6C
155#define FSPI_FLSHXCR0_SZ_KB 10
156#define FSPI_FLSHXCR0_SZ(x) ((x) >> FSPI_FLSHXCR0_SZ_KB)
157
158#define FSPI_FLSHA1CR1 0x70
159#define FSPI_FLSHA2CR1 0x74
160#define FSPI_FLSHB1CR1 0x78
161#define FSPI_FLSHB2CR1 0x7C
162#define FSPI_FLSHXCR1_CSINTR(x) ((x) << 16)
163#define FSPI_FLSHXCR1_CAS(x) ((x) << 11)
164#define FSPI_FLSHXCR1_WA BIT(10)
165#define FSPI_FLSHXCR1_TCSH(x) ((x) << 5)
166#define FSPI_FLSHXCR1_TCSS(x) (x)
167
168#define FSPI_FLSHA1CR2 0x80
169#define FSPI_FLSHA2CR2 0x84
170#define FSPI_FLSHB1CR2 0x88
171#define FSPI_FLSHB2CR2 0x8C
172#define FSPI_FLSHXCR2_CLRINSP BIT(24)
173#define FSPI_FLSHXCR2_AWRWAIT BIT(16)
174#define FSPI_FLSHXCR2_AWRSEQN_SHIFT 13
175#define FSPI_FLSHXCR2_AWRSEQI_SHIFT 8
176#define FSPI_FLSHXCR2_ARDSEQN_SHIFT 5
177#define FSPI_FLSHXCR2_ARDSEQI_SHIFT 0
178
179#define FSPI_IPCR0 0xA0
180
181#define FSPI_IPCR1 0xA4
182#define FSPI_IPCR1_IPAREN BIT(31)
183#define FSPI_IPCR1_SEQNUM_SHIFT 24
184#define FSPI_IPCR1_SEQID_SHIFT 16
185#define FSPI_IPCR1_IDATSZ(x) (x)
186
187#define FSPI_IPCMD 0xB0
188#define FSPI_IPCMD_TRG BIT(0)
189
190#define FSPI_DLPR 0xB4
191
192#define FSPI_IPRXFCR 0xB8
193#define FSPI_IPRXFCR_CLR BIT(0)
194#define FSPI_IPRXFCR_DMA_EN BIT(1)
195#define FSPI_IPRXFCR_WMRK(x) ((x) << 2)
196
197#define FSPI_IPTXFCR 0xBC
198#define FSPI_IPTXFCR_CLR BIT(0)
199#define FSPI_IPTXFCR_DMA_EN BIT(1)
200#define FSPI_IPTXFCR_WMRK(x) ((x) << 2)
201
202#define FSPI_DLLACR 0xC0
203#define FSPI_DLLACR_OVRDEN BIT(8)
204
205#define FSPI_DLLBCR 0xC4
206#define FSPI_DLLBCR_OVRDEN BIT(8)
207
208#define FSPI_STS0 0xE0
209#define FSPI_STS0_DLPHB(x) ((x) << 8)
210#define FSPI_STS0_DLPHA(x) ((x) << 4)
211#define FSPI_STS0_CMD_SRC(x) ((x) << 2)
212#define FSPI_STS0_ARB_IDLE BIT(1)
213#define FSPI_STS0_SEQ_IDLE BIT(0)
214
215#define FSPI_STS1 0xE4
216#define FSPI_STS1_IP_ERRCD(x) ((x) << 24)
217#define FSPI_STS1_IP_ERRID(x) ((x) << 16)
218#define FSPI_STS1_AHB_ERRCD(x) ((x) << 8)
219#define FSPI_STS1_AHB_ERRID(x) (x)
220
221#define FSPI_AHBSPNST 0xEC
222#define FSPI_AHBSPNST_DATLFT(x) ((x) << 16)
223#define FSPI_AHBSPNST_BUFID(x) ((x) << 1)
224#define FSPI_AHBSPNST_ACTIVE BIT(0)
225
226#define FSPI_IPRXFSTS 0xF0
227#define FSPI_IPRXFSTS_RDCNTR(x) ((x) << 16)
228#define FSPI_IPRXFSTS_FILL(x) (x)
229
230#define FSPI_IPTXFSTS 0xF4
231#define FSPI_IPTXFSTS_WRCNTR(x) ((x) << 16)
232#define FSPI_IPTXFSTS_FILL(x) (x)
233
234#define FSPI_RFDR 0x100
235#define FSPI_TFDR 0x180
236
237#define FSPI_LUT_BASE 0x200
238#define FSPI_LUT_OFFSET (SEQID_LUT * 4 * 4)
239#define FSPI_LUT_REG(idx) \
240 (FSPI_LUT_BASE + FSPI_LUT_OFFSET + (idx) * 4)
241
242/* register map end */
243
244/* Instruction set for the LUT register. */
245#define LUT_STOP 0x00
246#define LUT_CMD 0x01
247#define LUT_ADDR 0x02
248#define LUT_CADDR_SDR 0x03
249#define LUT_MODE 0x04
250#define LUT_MODE2 0x05
251#define LUT_MODE4 0x06
252#define LUT_MODE8 0x07
253#define LUT_NXP_WRITE 0x08
254#define LUT_NXP_READ 0x09
255#define LUT_LEARN_SDR 0x0A
256#define LUT_DATSZ_SDR 0x0B
257#define LUT_DUMMY 0x0C
258#define LUT_DUMMY_RWDS_SDR 0x0D
259#define LUT_JMP_ON_CS 0x1F
260#define LUT_CMD_DDR 0x21
261#define LUT_ADDR_DDR 0x22
262#define LUT_CADDR_DDR 0x23
263#define LUT_MODE_DDR 0x24
264#define LUT_MODE2_DDR 0x25
265#define LUT_MODE4_DDR 0x26
266#define LUT_MODE8_DDR 0x27
267#define LUT_WRITE_DDR 0x28
268#define LUT_READ_DDR 0x29
269#define LUT_LEARN_DDR 0x2A
270#define LUT_DATSZ_DDR 0x2B
271#define LUT_DUMMY_DDR 0x2C
272#define LUT_DUMMY_RWDS_DDR 0x2D
273
274/*
275 * Calculate number of required PAD bits for LUT register.
276 *
277 * The pad stands for the number of IO lines [0:7].
278 * For example, the octal read needs eight IO lines,
279 * so you should use LUT_PAD(8). This macro
280 * returns 3 i.e. use eight (2^3) IP lines for read.
281 */
282#define LUT_PAD(x) (fls(x) - 1)
283
284/*
285 * Macro for constructing the LUT entries with the following
286 * register layout:
287 *
288 * ---------------------------------------------------
289 * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 |
290 * ---------------------------------------------------
291 */
292#define PAD_SHIFT 8
293#define INSTR_SHIFT 10
294#define OPRND_SHIFT 16
295
296/* Macros for constructing the LUT register. */
297#define LUT_DEF(idx, ins, pad, opr) \
298 ((((ins) << INSTR_SHIFT) | ((pad) << PAD_SHIFT) | \
299 (opr)) << (((idx) % 2) * OPRND_SHIFT))
300
301#define POLL_TOUT 5000
302#define NXP_FSPI_MAX_CHIPSELECT 4
303
304struct nxp_fspi_devtype_data {
305 unsigned int rxfifo;
306 unsigned int txfifo;
307 unsigned int ahb_buf_size;
308 unsigned int quirks;
309 bool little_endian;
310};
311
312static const struct nxp_fspi_devtype_data lx2160a_data = {
313 .rxfifo = SZ_512, /* (64 * 64 bits) */
314 .txfifo = SZ_1K, /* (128 * 64 bits) */
315 .ahb_buf_size = SZ_2K, /* (256 * 64 bits) */
316 .quirks = 0,
317 .little_endian = true, /* little-endian */
318};
319
320struct nxp_fspi {
321 struct udevice *dev;
322 void __iomem *iobase;
323 void __iomem *ahb_addr;
324 u32 memmap_phy;
325 u32 memmap_phy_size;
326 struct clk clk, clk_en;
327 const struct nxp_fspi_devtype_data *devtype_data;
328};
329
330/*
331 * R/W functions for big- or little-endian registers:
332 * The FSPI controller's endianness is independent of
333 * the CPU core's endianness. So far, although the CPU
334 * core is little-endian the FSPI controller can use
335 * big-endian or little-endian.
336 */
337static void fspi_writel(struct nxp_fspi *f, u32 val, void __iomem *addr)
338{
339 if (f->devtype_data->little_endian)
340 out_le32(addr, val);
341 else
342 out_be32(addr, val);
343}
344
345static u32 fspi_readl(struct nxp_fspi *f, void __iomem *addr)
346{
347 if (f->devtype_data->little_endian)
348 return in_le32(addr);
349 else
350 return in_be32(addr);
351}
352
353static int nxp_fspi_check_buswidth(struct nxp_fspi *f, u8 width)
354{
355 switch (width) {
356 case 1:
357 case 2:
358 case 4:
359 case 8:
360 return 0;
361 }
362
363 return -ENOTSUPP;
364}
365
366static bool nxp_fspi_supports_op(struct spi_slave *slave,
367 const struct spi_mem_op *op)
368{
369 struct nxp_fspi *f;
370 struct udevice *bus;
371 int ret;
372
373 bus = slave->dev->parent;
374 f = dev_get_priv(bus);
375
376 ret = nxp_fspi_check_buswidth(f, op->cmd.buswidth);
377
378 if (op->addr.nbytes)
379 ret |= nxp_fspi_check_buswidth(f, op->addr.buswidth);
380
381 if (op->dummy.nbytes)
382 ret |= nxp_fspi_check_buswidth(f, op->dummy.buswidth);
383
384 if (op->data.nbytes)
385 ret |= nxp_fspi_check_buswidth(f, op->data.buswidth);
386
387 if (ret)
388 return false;
389
390 /*
391 * The number of address bytes should be equal to or less than 4 bytes.
392 */
393 if (op->addr.nbytes > 4)
394 return false;
395
396 /*
397 * If requested address value is greater than controller assigned
398 * memory mapped space, return error as it didn't fit in the range
399 * of assigned address space.
400 */
401 if (op->addr.val >= f->memmap_phy_size)
402 return false;
403
404 /* Max 64 dummy clock cycles supported */
405 if (op->dummy.buswidth &&
406 (op->dummy.nbytes * 8 / op->dummy.buswidth > 64))
407 return false;
408
409 /* Max data length, check controller limits and alignment */
410 if (op->data.dir == SPI_MEM_DATA_IN &&
411 (op->data.nbytes > f->devtype_data->ahb_buf_size ||
412 (op->data.nbytes > f->devtype_data->rxfifo - 4 &&
413 !IS_ALIGNED(op->data.nbytes, 8))))
414 return false;
415
416 if (op->data.dir == SPI_MEM_DATA_OUT &&
417 op->data.nbytes > f->devtype_data->txfifo)
418 return false;
419
420 return true;
421}
422
423/* Instead of busy looping invoke readl_poll_timeout functionality. */
424static int fspi_readl_poll_tout(struct nxp_fspi *f, void __iomem *base,
425 u32 mask, u32 delay_us,
426 u32 timeout_us, bool c)
427{
428 u32 reg;
429
430 if (!f->devtype_data->little_endian)
431 mask = (u32)cpu_to_be32(mask);
432
433 if (c)
434 return readl_poll_timeout(base, reg, (reg & mask),
435 timeout_us);
436 else
437 return readl_poll_timeout(base, reg, !(reg & mask),
438 timeout_us);
439}
440
441/*
442 * If the slave device content being changed by Write/Erase, need to
443 * invalidate the AHB buffer. This can be achieved by doing the reset
444 * of controller after setting MCR0[SWRESET] bit.
445 */
446static inline void nxp_fspi_invalid(struct nxp_fspi *f)
447{
448 u32 reg;
449 int ret;
450
451 reg = fspi_readl(f, f->iobase + FSPI_MCR0);
452 fspi_writel(f, reg | FSPI_MCR0_SWRST, f->iobase + FSPI_MCR0);
453
454 /* w1c register, wait unit clear */
455 ret = fspi_readl_poll_tout(f, f->iobase + FSPI_MCR0,
456 FSPI_MCR0_SWRST, 0, POLL_TOUT, false);
457 WARN_ON(ret);
458}
459
460static void nxp_fspi_prepare_lut(struct nxp_fspi *f,
461 const struct spi_mem_op *op)
462{
463 void __iomem *base = f->iobase;
464 u32 lutval[4] = {};
465 int lutidx = 1, i;
466
467 /* cmd */
468 lutval[0] |= LUT_DEF(0, LUT_CMD, LUT_PAD(op->cmd.buswidth),
469 op->cmd.opcode);
470
471 /* addr bytes */
472 if (op->addr.nbytes) {
473 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_ADDR,
474 LUT_PAD(op->addr.buswidth),
475 op->addr.nbytes * 8);
476 lutidx++;
477 }
478
479 /* dummy bytes, if needed */
480 if (op->dummy.nbytes) {
481 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_DUMMY,
482 /*
483 * Due to FlexSPI controller limitation number of PAD for dummy
484 * buswidth needs to be programmed as equal to data buswidth.
485 */
486 LUT_PAD(op->data.buswidth),
487 op->dummy.nbytes * 8 /
488 op->dummy.buswidth);
489 lutidx++;
490 }
491
492 /* read/write data bytes */
493 if (op->data.nbytes) {
494 lutval[lutidx / 2] |= LUT_DEF(lutidx,
495 op->data.dir == SPI_MEM_DATA_IN ?
496 LUT_NXP_READ : LUT_NXP_WRITE,
497 LUT_PAD(op->data.buswidth),
498 0);
499 lutidx++;
500 }
501
502 /* stop condition. */
503 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_STOP, 0, 0);
504
505 /* unlock LUT */
506 fspi_writel(f, FSPI_LUTKEY_VALUE, f->iobase + FSPI_LUTKEY);
507 fspi_writel(f, FSPI_LCKER_UNLOCK, f->iobase + FSPI_LCKCR);
508
509 /* fill LUT */
510 for (i = 0; i < ARRAY_SIZE(lutval); i++)
511 fspi_writel(f, lutval[i], base + FSPI_LUT_REG(i));
512
513 dev_dbg(f->dev, "CMD[%x] lutval[0:%x \t 1:%x \t 2:%x \t 3:%x]\n",
514 op->cmd.opcode, lutval[0], lutval[1], lutval[2], lutval[3]);
515
516 /* lock LUT */
517 fspi_writel(f, FSPI_LUTKEY_VALUE, f->iobase + FSPI_LUTKEY);
518 fspi_writel(f, FSPI_LCKER_LOCK, f->iobase + FSPI_LCKCR);
519}
520
521#if CONFIG_IS_ENABLED(CONFIG_CLK)
522static int nxp_fspi_clk_prep_enable(struct nxp_fspi *f)
523{
524 int ret;
525
526 ret = clk_enable(&f->clk_en);
527 if (ret)
528 return ret;
529
530 ret = clk_enable(&f->clk);
531 if (ret) {
532 clk_disable(&f->clk_en);
533 return ret;
534 }
535
536 return 0;
537}
538
539static void nxp_fspi_clk_disable_unprep(struct nxp_fspi *f)
540{
541 clk_disable(&f->clk);
542 clk_disable(&f->clk_en);
543}
544#endif
545
546/*
547 * In FlexSPI controller, flash access is based on value of FSPI_FLSHXXCR0
548 * register and start base address of the slave device.
549 *
550 * (Higher address)
551 * -------- <-- FLSHB2CR0
552 * | B2 |
553 * | |
554 * B2 start address --> -------- <-- FLSHB1CR0
555 * | B1 |
556 * | |
557 * B1 start address --> -------- <-- FLSHA2CR0
558 * | A2 |
559 * | |
560 * A2 start address --> -------- <-- FLSHA1CR0
561 * | A1 |
562 * | |
563 * A1 start address --> -------- (Lower address)
564 *
565 *
566 * Start base address defines the starting address range for given CS and
567 * FSPI_FLSHXXCR0 defines the size of the slave device connected at given CS.
568 *
569 * But, different targets are having different combinations of number of CS,
570 * some targets only have single CS or two CS covering controller's full
571 * memory mapped space area.
572 * Thus, implementation is being done as independent of the size and number
573 * of the connected slave device.
574 * Assign controller memory mapped space size as the size to the connected
575 * slave device.
576 * Mark FLSHxxCR0 as zero initially and then assign value only to the selected
577 * chip-select Flash configuration register.
578 *
579 * For e.g. to access CS2 (B1), FLSHB1CR0 register would be equal to the
580 * memory mapped size of the controller.
581 * Value for rest of the CS FLSHxxCR0 register would be zero.
582 *
583 */
584static void nxp_fspi_select_mem(struct nxp_fspi *f, int chip_select)
585{
586 u64 size_kb;
587
588 /* Reset FLSHxxCR0 registers */
589 fspi_writel(f, 0, f->iobase + FSPI_FLSHA1CR0);
590 fspi_writel(f, 0, f->iobase + FSPI_FLSHA2CR0);
591 fspi_writel(f, 0, f->iobase + FSPI_FLSHB1CR0);
592 fspi_writel(f, 0, f->iobase + FSPI_FLSHB2CR0);
593
594 /* Assign controller memory mapped space as size, KBytes, of flash. */
595 size_kb = FSPI_FLSHXCR0_SZ(f->memmap_phy_size);
596
597 fspi_writel(f, size_kb, f->iobase + FSPI_FLSHA1CR0 +
598 4 * chip_select);
599
600 dev_dbg(f->dev, "Slave device [CS:%x] selected\n", chip_select);
601}
602
603static void nxp_fspi_read_ahb(struct nxp_fspi *f, const struct spi_mem_op *op)
604{
605 u32 len = op->data.nbytes;
606
607 /* Read out the data directly from the AHB buffer. */
608 memcpy_fromio(op->data.buf.in, (f->ahb_addr + op->addr.val), len);
609}
610
611static void nxp_fspi_fill_txfifo(struct nxp_fspi *f,
612 const struct spi_mem_op *op)
613{
614 void __iomem *base = f->iobase;
615 int i, ret;
616 u8 *buf = (u8 *)op->data.buf.out;
617
618 /* clear the TX FIFO. */
619 fspi_writel(f, FSPI_IPTXFCR_CLR, base + FSPI_IPTXFCR);
620
621 /*
622 * Default value of water mark level is 8 bytes, hence in single
623 * write request controller can write max 8 bytes of data.
624 */
625
626 for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 8); i += 8) {
627 /* Wait for TXFIFO empty */
628 ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
629 FSPI_INTR_IPTXWE, 0,
630 POLL_TOUT, true);
631 WARN_ON(ret);
632
633 fspi_writel(f, *(u32 *)(buf + i), base + FSPI_TFDR);
634 fspi_writel(f, *(u32 *)(buf + i + 4), base + FSPI_TFDR + 4);
635 fspi_writel(f, FSPI_INTR_IPTXWE, base + FSPI_INTR);
636 }
637
638 if (i < op->data.nbytes) {
639 u32 data = 0;
640 int j;
641 /* Wait for TXFIFO empty */
642 ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
643 FSPI_INTR_IPTXWE, 0,
644 POLL_TOUT, true);
645 WARN_ON(ret);
646
647 for (j = 0; j < ALIGN(op->data.nbytes - i, 4); j += 4) {
648 memcpy(&data, buf + i + j, 4);
649 fspi_writel(f, data, base + FSPI_TFDR + j);
650 }
651 fspi_writel(f, FSPI_INTR_IPTXWE, base + FSPI_INTR);
652 }
653}
654
655static void nxp_fspi_read_rxfifo(struct nxp_fspi *f,
656 const struct spi_mem_op *op)
657{
658 void __iomem *base = f->iobase;
659 int i, ret;
660 int len = op->data.nbytes;
661 u8 *buf = (u8 *)op->data.buf.in;
662
663 /*
664 * Default value of water mark level is 8 bytes, hence in single
665 * read request controller can read max 8 bytes of data.
666 */
667 for (i = 0; i < ALIGN_DOWN(len, 8); i += 8) {
668 /* Wait for RXFIFO available */
669 ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
670 FSPI_INTR_IPRXWA, 0,
671 POLL_TOUT, true);
672 WARN_ON(ret);
673
674 *(u32 *)(buf + i) = fspi_readl(f, base + FSPI_RFDR);
675 *(u32 *)(buf + i + 4) = fspi_readl(f, base + FSPI_RFDR + 4);
676 /* move the FIFO pointer */
677 fspi_writel(f, FSPI_INTR_IPRXWA, base + FSPI_INTR);
678 }
679
680 if (i < len) {
681 u32 tmp;
682 int size, j;
683
684 buf = op->data.buf.in + i;
685 /* Wait for RXFIFO available */
686 ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
687 FSPI_INTR_IPRXWA, 0,
688 POLL_TOUT, true);
689 WARN_ON(ret);
690
691 len = op->data.nbytes - i;
692 for (j = 0; j < op->data.nbytes - i; j += 4) {
693 tmp = fspi_readl(f, base + FSPI_RFDR + j);
694 size = min(len, 4);
695 memcpy(buf + j, &tmp, size);
696 len -= size;
697 }
698 }
699
700 /* invalid the RXFIFO */
701 fspi_writel(f, FSPI_IPRXFCR_CLR, base + FSPI_IPRXFCR);
702 /* move the FIFO pointer */
703 fspi_writel(f, FSPI_INTR_IPRXWA, base + FSPI_INTR);
704}
705
706static int nxp_fspi_do_op(struct nxp_fspi *f, const struct spi_mem_op *op)
707{
708 void __iomem *base = f->iobase;
709 int seqnum = 0;
710 int err = 0;
711 u32 reg;
712
713 reg = fspi_readl(f, base + FSPI_IPRXFCR);
714 /* invalid RXFIFO first */
715 reg &= ~FSPI_IPRXFCR_DMA_EN;
716 reg = reg | FSPI_IPRXFCR_CLR;
717 fspi_writel(f, reg, base + FSPI_IPRXFCR);
718
719 fspi_writel(f, op->addr.val, base + FSPI_IPCR0);
720 /*
721 * Always start the sequence at the same index since we update
722 * the LUT at each exec_op() call. And also specify the DATA
723 * length, since it's has not been specified in the LUT.
724 */
725 fspi_writel(f, op->data.nbytes |
726 (SEQID_LUT << FSPI_IPCR1_SEQID_SHIFT) |
727 (seqnum << FSPI_IPCR1_SEQNUM_SHIFT),
728 base + FSPI_IPCR1);
729
730 /* Trigger the LUT now. */
731 fspi_writel(f, FSPI_IPCMD_TRG, base + FSPI_IPCMD);
732
733 /* Wait for the completion. */
734 err = fspi_readl_poll_tout(f, f->iobase + FSPI_STS0,
735 FSPI_STS0_ARB_IDLE, 1, 1000 * 1000, true);
736
737 /* Invoke IP data read, if request is of data read. */
738 if (!err && op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN)
739 nxp_fspi_read_rxfifo(f, op);
740
741 return err;
742}
743
744static int nxp_fspi_exec_op(struct spi_slave *slave,
745 const struct spi_mem_op *op)
746{
747 struct nxp_fspi *f;
748 struct udevice *bus;
749 int err = 0;
750
751 bus = slave->dev->parent;
752 f = dev_get_priv(bus);
753
754 /* Wait for controller being ready. */
755 err = fspi_readl_poll_tout(f, f->iobase + FSPI_STS0,
756 FSPI_STS0_ARB_IDLE, 1, POLL_TOUT, true);
757 WARN_ON(err);
758
759 nxp_fspi_prepare_lut(f, op);
760 /*
761 * If we have large chunks of data, we read them through the AHB bus
762 * by accessing the mapped memory. In all other cases we use
763 * IP commands to access the flash.
764 */
765 if (op->data.nbytes > (f->devtype_data->rxfifo - 4) &&
766 op->data.dir == SPI_MEM_DATA_IN) {
767 nxp_fspi_read_ahb(f, op);
768 } else {
769 if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT)
770 nxp_fspi_fill_txfifo(f, op);
771
772 err = nxp_fspi_do_op(f, op);
773 }
774
775 /* Invalidate the data in the AHB buffer. */
776 nxp_fspi_invalid(f);
777
778 return err;
779}
780
781static int nxp_fspi_adjust_op_size(struct spi_slave *slave,
782 struct spi_mem_op *op)
783{
784 struct nxp_fspi *f;
785 struct udevice *bus;
786
787 bus = slave->dev->parent;
788 f = dev_get_priv(bus);
789
790 if (op->data.dir == SPI_MEM_DATA_OUT) {
791 if (op->data.nbytes > f->devtype_data->txfifo)
792 op->data.nbytes = f->devtype_data->txfifo;
793 } else {
794 if (op->data.nbytes > f->devtype_data->ahb_buf_size)
795 op->data.nbytes = f->devtype_data->ahb_buf_size;
796 else if (op->data.nbytes > (f->devtype_data->rxfifo - 4))
797 op->data.nbytes = ALIGN_DOWN(op->data.nbytes, 8);
798 }
799
800 return 0;
801}
802
803static int nxp_fspi_default_setup(struct nxp_fspi *f)
804{
805 void __iomem *base = f->iobase;
806 int ret, i;
807 u32 reg;
808
809#if CONFIG_IS_ENABLED(CONFIG_CLK)
810 /* disable and unprepare clock to avoid glitch pass to controller */
811 nxp_fspi_clk_disable_unprep(f);
812
813 /* the default frequency, we will change it later if necessary. */
814 ret = clk_set_rate(&f->clk, 20000000);
815 if (ret)
816 return ret;
817
818 ret = nxp_fspi_clk_prep_enable(f);
819 if (ret)
820 return ret;
821#endif
822
823 /* Reset the module */
824 /* w1c register, wait unit clear */
825 ret = fspi_readl_poll_tout(f, f->iobase + FSPI_MCR0,
826 FSPI_MCR0_SWRST, 0, POLL_TOUT, false);
827 WARN_ON(ret);
828
829 /* Disable the module */
830 fspi_writel(f, FSPI_MCR0_MDIS, base + FSPI_MCR0);
831
832 /* Reset the DLL register to default value */
833 fspi_writel(f, FSPI_DLLACR_OVRDEN, base + FSPI_DLLACR);
834 fspi_writel(f, FSPI_DLLBCR_OVRDEN, base + FSPI_DLLBCR);
835
836 /* enable module */
837 fspi_writel(f, FSPI_MCR0_AHB_TIMEOUT(0xFF) | FSPI_MCR0_IP_TIMEOUT(0xFF),
838 base + FSPI_MCR0);
839
840 /*
841 * Disable same device enable bit and configure all slave devices
842 * independently.
843 */
844 reg = fspi_readl(f, f->iobase + FSPI_MCR2);
845 reg = reg & ~(FSPI_MCR2_SAMEDEVICEEN);
846 fspi_writel(f, reg, base + FSPI_MCR2);
847
848 /* AHB configuration for access buffer 0~7. */
849 for (i = 0; i < 7; i++)
850 fspi_writel(f, 0, base + FSPI_AHBRX_BUF0CR0 + 4 * i);
851
852 /*
853 * Set ADATSZ with the maximum AHB buffer size to improve the read
854 * performance.
855 */
856 fspi_writel(f, (f->devtype_data->ahb_buf_size / 8 |
857 FSPI_AHBRXBUF0CR7_PREF), base + FSPI_AHBRX_BUF7CR0);
858
859 /* prefetch and no start address alignment limitation */
860 fspi_writel(f, FSPI_AHBCR_PREF_EN | FSPI_AHBCR_RDADDROPT,
861 base + FSPI_AHBCR);
862
863 /* AHB Read - Set lut sequence ID for all CS. */
864 fspi_writel(f, SEQID_LUT, base + FSPI_FLSHA1CR2);
865 fspi_writel(f, SEQID_LUT, base + FSPI_FLSHA2CR2);
866 fspi_writel(f, SEQID_LUT, base + FSPI_FLSHB1CR2);
867 fspi_writel(f, SEQID_LUT, base + FSPI_FLSHB2CR2);
868
869 return 0;
870}
871
872static int nxp_fspi_probe(struct udevice *bus)
873{
874 struct nxp_fspi *f = dev_get_priv(bus);
875
876 f->devtype_data =
877 (struct nxp_fspi_devtype_data *)dev_get_driver_data(bus);
878 nxp_fspi_default_setup(f);
879
880 return 0;
881}
882
883static int nxp_fspi_claim_bus(struct udevice *dev)
884{
885 struct nxp_fspi *f;
886 struct udevice *bus;
887 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
888
889 bus = dev->parent;
890 f = dev_get_priv(bus);
891
892 nxp_fspi_select_mem(f, slave_plat->cs);
893
894 return 0;
895}
896
897static int nxp_fspi_set_speed(struct udevice *bus, uint speed)
898{
899#if CONFIG_IS_ENABLED(CONFIG_CLK)
900 struct nxp_fspi *f = dev_get_priv(bus);
901 int ret;
902
903 nxp_fspi_clk_disable_unprep(f);
904
905 ret = clk_set_rate(&f->clk, speed);
906 if (ret)
907 return ret;
908
909 ret = nxp_fspi_clk_prep_enable(f);
910 if (ret)
911 return ret;
912#endif
913 return 0;
914}
915
916static int nxp_fspi_set_mode(struct udevice *bus, uint mode)
917{
918 /* Nothing to do */
919 return 0;
920}
921
922static int nxp_fspi_ofdata_to_platdata(struct udevice *bus)
923{
924 struct nxp_fspi *f = dev_get_priv(bus);
925#if CONFIG_IS_ENABLED(CONFIG_CLK)
926 int ret;
927#endif
928
929 fdt_addr_t iobase;
930 fdt_addr_t iobase_size;
931 fdt_addr_t ahb_addr;
932 fdt_addr_t ahb_size;
933
934 f->dev = bus;
935
936 iobase = devfdt_get_addr_size_name(bus, "fspi_base", &iobase_size);
937 if (iobase == FDT_ADDR_T_NONE) {
938 dev_err(bus, "fspi_base regs missing\n");
939 return -ENODEV;
940 }
941 f->iobase = map_physmem(iobase, iobase_size, MAP_NOCACHE);
942
943 ahb_addr = devfdt_get_addr_size_name(bus, "fspi_mmap", &ahb_size);
944 if (ahb_addr == FDT_ADDR_T_NONE) {
945 dev_err(bus, "fspi_mmap regs missing\n");
946 return -ENODEV;
947 }
948 f->ahb_addr = map_physmem(ahb_addr, ahb_size, MAP_NOCACHE);
949 f->memmap_phy_size = ahb_size;
950
951#if CONFIG_IS_ENABLED(CONFIG_CLK)
952 ret = clk_get_by_name(bus, "fspi_en", &f->clk_en);
953 if (ret) {
954 dev_err(bus, "failed to get fspi_en clock\n");
955 return ret;
956 }
957
958 ret = clk_get_by_name(bus, "fspi", &f->clk);
959 if (ret) {
960 dev_err(bus, "failed to get fspi clock\n");
961 return ret;
962 }
963#endif
964
965 dev_dbg(bus, "iobase=<0x%llx>, ahb_addr=<0x%llx>\n", iobase, ahb_addr);
966
967 return 0;
968}
969
970static const struct spi_controller_mem_ops nxp_fspi_mem_ops = {
971 .adjust_op_size = nxp_fspi_adjust_op_size,
972 .supports_op = nxp_fspi_supports_op,
973 .exec_op = nxp_fspi_exec_op,
974};
975
976static const struct dm_spi_ops nxp_fspi_ops = {
977 .claim_bus = nxp_fspi_claim_bus,
978 .set_speed = nxp_fspi_set_speed,
979 .set_mode = nxp_fspi_set_mode,
980 .mem_ops = &nxp_fspi_mem_ops,
981};
982
983static const struct udevice_id nxp_fspi_ids[] = {
984 { .compatible = "nxp,lx2160a-fspi", .data = (ulong)&lx2160a_data, },
985 { }
986};
987
988U_BOOT_DRIVER(nxp_fspi) = {
989 .name = "nxp_fspi",
990 .id = UCLASS_SPI,
991 .of_match = nxp_fspi_ids,
992 .ops = &nxp_fspi_ops,
993 .ofdata_to_platdata = nxp_fspi_ofdata_to_platdata,
994 .priv_auto_alloc_size = sizeof(struct nxp_fspi),
995 .probe = nxp_fspi_probe,
996};