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