1 // SPDX-License-Identifier: GPL-2.0+
2
3 /*
4 * NXP FlexSPI(FSPI) controller driver.
5 *
6 * Copyright 2019-2020 NXP
7 * Copyright 2020 Puresoftware Ltd.
8 *
9 * FlexSPI is a flexsible SPI host controller which supports two SPI
10 * channels and up to 4 external devices. Each channel supports
11 * Single/Dual/Quad/Octal mode data transfer (1/2/4/8 bidirectional
12 * data lines).
13 *
14 * FlexSPI controller is driven by the LUT(Look-up Table) registers
15 * LUT registers are a look-up-table for sequences of instructions.
16 * A valid sequence consists of four LUT registers.
17 * Maximum 32 LUT sequences can be programmed simultaneously.
18 *
19 * LUTs are being created at run-time based on the commands passed
20 * from the spi-mem framework, thus using single LUT index.
21 *
22 * Software triggered Flash read/write access by IP Bus.
23 *
24 * Memory mapped read access by AHB Bus.
25 *
26 * Based on SPI MEM interface and spi-fsl-qspi.c driver.
27 *
28 * Author:
29 * Yogesh Narayan Gaur <yogeshnarayan.gaur@nxp.com>
30 * Boris Brezillon <bbrezillon@kernel.org>
31 * Frieder Schrempf <frieder.schrempf@kontron.de>
32 */
33
34 #include <linux/acpi.h>
35 #include <linux/bitops.h>
36 #include <linux/bitfield.h>
37 #include <linux/clk.h>
38 #include <linux/completion.h>
39 #include <linux/delay.h>
40 #include <linux/err.h>
41 #include <linux/errno.h>
42 #include <linux/interrupt.h>
43 #include <linux/io.h>
44 #include <linux/iopoll.h>
45 #include <linux/jiffies.h>
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/mutex.h>
49 #include <linux/of.h>
50 #include <linux/platform_device.h>
51 #include <linux/pinctrl/consumer.h>
52 #include <linux/pm_runtime.h>
53 #include <linux/pm_qos.h>
54 #include <linux/regmap.h>
55 #include <linux/sizes.h>
56 #include <linux/sys_soc.h>
57
58 #include <linux/mfd/syscon.h>
59 #include <linux/spi/spi.h>
60 #include <linux/spi/spi-mem.h>
61
62 /* runtime pm timeout */
63 #define FSPI_RPM_TIMEOUT 50 /* 50ms */
64
65 /* Registers used by the driver */
66 #define FSPI_MCR0 0x00
67 #define FSPI_MCR0_AHB_TIMEOUT(x) ((x) << 24)
68 #define FSPI_MCR0_IP_TIMEOUT(x) ((x) << 16)
69 #define FSPI_MCR0_LEARN_EN BIT(15)
70 #define FSPI_MCR0_SCRFRUN_EN BIT(14)
71 #define FSPI_MCR0_OCTCOMB_EN BIT(13)
72 #define FSPI_MCR0_DOZE_EN BIT(12)
73 #define FSPI_MCR0_HSEN BIT(11)
74 #define FSPI_MCR0_SERCLKDIV BIT(8)
75 #define FSPI_MCR0_ATDF_EN BIT(7)
76 #define FSPI_MCR0_ARDF_EN BIT(6)
77 #define FSPI_MCR0_RXCLKSRC(x) ((x) << 4)
78 #define FSPI_MCR0_END_CFG(x) ((x) << 2)
79 #define FSPI_MCR0_MDIS BIT(1)
80 #define FSPI_MCR0_SWRST BIT(0)
81
82 #define FSPI_MCR1 0x04
83 #define FSPI_MCR1_SEQ_TIMEOUT(x) ((x) << 16)
84 #define FSPI_MCR1_AHB_TIMEOUT(x) (x)
85
86 #define FSPI_MCR2 0x08
87 #define FSPI_MCR2_IDLE_WAIT(x) ((x) << 24)
88 #define FSPI_MCR2_SAMEDEVICEEN BIT(15)
89 #define FSPI_MCR2_CLRLRPHS BIT(14)
90 #define FSPI_MCR2_ABRDATSZ BIT(8)
91 #define FSPI_MCR2_ABRLEARN BIT(7)
92 #define FSPI_MCR2_ABR_READ BIT(6)
93 #define FSPI_MCR2_ABRWRITE BIT(5)
94 #define FSPI_MCR2_ABRDUMMY BIT(4)
95 #define FSPI_MCR2_ABR_MODE BIT(3)
96 #define FSPI_MCR2_ABRCADDR BIT(2)
97 #define FSPI_MCR2_ABRRADDR BIT(1)
98 #define FSPI_MCR2_ABR_CMD BIT(0)
99
100 #define FSPI_AHBCR 0x0c
101 #define FSPI_AHBCR_RDADDROPT BIT(6)
102 #define FSPI_AHBCR_PREF_EN BIT(5)
103 #define FSPI_AHBCR_BUFF_EN BIT(4)
104 #define FSPI_AHBCR_CACH_EN BIT(3)
105 #define FSPI_AHBCR_CLRTXBUF BIT(2)
106 #define FSPI_AHBCR_CLRRXBUF BIT(1)
107 #define FSPI_AHBCR_PAR_EN BIT(0)
108
109 #define FSPI_INTEN 0x10
110 #define FSPI_INTEN_SCLKSBWR BIT(9)
111 #define FSPI_INTEN_SCLKSBRD BIT(8)
112 #define FSPI_INTEN_DATALRNFL BIT(7)
113 #define FSPI_INTEN_IPTXWE BIT(6)
114 #define FSPI_INTEN_IPRXWA BIT(5)
115 #define FSPI_INTEN_AHBCMDERR BIT(4)
116 #define FSPI_INTEN_IPCMDERR BIT(3)
117 #define FSPI_INTEN_AHBCMDGE BIT(2)
118 #define FSPI_INTEN_IPCMDGE BIT(1)
119 #define FSPI_INTEN_IPCMDDONE BIT(0)
120
121 #define FSPI_INTR 0x14
122 #define FSPI_INTR_SCLKSBWR BIT(9)
123 #define FSPI_INTR_SCLKSBRD BIT(8)
124 #define FSPI_INTR_DATALRNFL BIT(7)
125 #define FSPI_INTR_IPTXWE BIT(6)
126 #define FSPI_INTR_IPRXWA BIT(5)
127 #define FSPI_INTR_AHBCMDERR BIT(4)
128 #define FSPI_INTR_IPCMDERR BIT(3)
129 #define FSPI_INTR_AHBCMDGE BIT(2)
130 #define FSPI_INTR_IPCMDGE BIT(1)
131 #define FSPI_INTR_IPCMDDONE BIT(0)
132
133 #define FSPI_LUTKEY 0x18
134 #define FSPI_LUTKEY_VALUE 0x5AF05AF0
135
136 #define FSPI_LCKCR 0x1C
137
138 #define FSPI_LCKER_LOCK 0x1
139 #define FSPI_LCKER_UNLOCK 0x2
140
141 #define FSPI_BUFXCR_INVALID_MSTRID 0xE
142 #define FSPI_AHBRX_BUF0CR0 0x20
143 #define FSPI_AHBRX_BUF1CR0 0x24
144 #define FSPI_AHBRX_BUF2CR0 0x28
145 #define FSPI_AHBRX_BUF3CR0 0x2C
146 #define FSPI_AHBRX_BUF4CR0 0x30
147 #define FSPI_AHBRX_BUF5CR0 0x34
148 #define FSPI_AHBRX_BUF6CR0 0x38
149 #define FSPI_AHBRX_BUF7CR0 0x3C
150 #define FSPI_AHBRXBUF0CR7_PREF BIT(31)
151
152 #define FSPI_AHBRX_BUF0CR1 0x40
153 #define FSPI_AHBRX_BUF1CR1 0x44
154 #define FSPI_AHBRX_BUF2CR1 0x48
155 #define FSPI_AHBRX_BUF3CR1 0x4C
156 #define FSPI_AHBRX_BUF4CR1 0x50
157 #define FSPI_AHBRX_BUF5CR1 0x54
158 #define FSPI_AHBRX_BUF6CR1 0x58
159 #define FSPI_AHBRX_BUF7CR1 0x5C
160
161 #define FSPI_FLSHA1CR0 0x60
162 #define FSPI_FLSHA2CR0 0x64
163 #define FSPI_FLSHB1CR0 0x68
164 #define FSPI_FLSHB2CR0 0x6C
165 #define FSPI_FLSHXCR0_SZ_KB 10
166 #define FSPI_FLSHXCR0_SZ(x) ((x) >> FSPI_FLSHXCR0_SZ_KB)
167
168 #define FSPI_FLSHA1CR1 0x70
169 #define FSPI_FLSHA2CR1 0x74
170 #define FSPI_FLSHB1CR1 0x78
171 #define FSPI_FLSHB2CR1 0x7C
172 #define FSPI_FLSHXCR1_CSINTR(x) ((x) << 16)
173 #define FSPI_FLSHXCR1_CAS(x) ((x) << 11)
174 #define FSPI_FLSHXCR1_WA BIT(10)
175 #define FSPI_FLSHXCR1_TCSH(x) ((x) << 5)
176 #define FSPI_FLSHXCR1_TCSS(x) (x)
177
178 #define FSPI_FLSHA1CR2 0x80
179 #define FSPI_FLSHA2CR2 0x84
180 #define FSPI_FLSHB1CR2 0x88
181 #define FSPI_FLSHB2CR2 0x8C
182 #define FSPI_FLSHXCR2_CLRINSP BIT(24)
183 #define FSPI_FLSHXCR2_AWRWAIT BIT(16)
184 #define FSPI_FLSHXCR2_AWRSEQN_SHIFT 13
185 #define FSPI_FLSHXCR2_AWRSEQI_SHIFT 8
186 #define FSPI_FLSHXCR2_ARDSEQN_SHIFT 5
187 #define FSPI_FLSHXCR2_ARDSEQI_SHIFT 0
188
189 #define FSPI_IPCR0 0xA0
190
191 #define FSPI_IPCR1 0xA4
192 #define FSPI_IPCR1_IPAREN BIT(31)
193 #define FSPI_IPCR1_SEQNUM_SHIFT 24
194 #define FSPI_IPCR1_SEQID_SHIFT 16
195 #define FSPI_IPCR1_IDATSZ(x) (x)
196
197 #define FSPI_IPCMD 0xB0
198 #define FSPI_IPCMD_TRG BIT(0)
199
200 #define FSPI_DLPR 0xB4
201
202 #define FSPI_IPRXFCR 0xB8
203 #define FSPI_IPRXFCR_CLR BIT(0)
204 #define FSPI_IPRXFCR_DMA_EN BIT(1)
205 #define FSPI_IPRXFCR_WMRK(x) ((x) << 2)
206
207 #define FSPI_IPTXFCR 0xBC
208 #define FSPI_IPTXFCR_CLR BIT(0)
209 #define FSPI_IPTXFCR_DMA_EN BIT(1)
210 #define FSPI_IPTXFCR_WMRK(x) ((x) << 2)
211
212 #define FSPI_DLLACR 0xC0
213 #define FSPI_DLLACR_OVRDEN BIT(8)
214 #define FSPI_DLLACR_SLVDLY(x) ((x) << 3)
215 #define FSPI_DLLACR_DLLRESET BIT(1)
216 #define FSPI_DLLACR_DLLEN BIT(0)
217
218 #define FSPI_DLLBCR 0xC4
219 #define FSPI_DLLBCR_OVRDEN BIT(8)
220 #define FSPI_DLLBCR_SLVDLY(x) ((x) << 3)
221 #define FSPI_DLLBCR_DLLRESET BIT(1)
222 #define FSPI_DLLBCR_DLLEN BIT(0)
223
224 #define FSPI_STS0 0xE0
225 #define FSPI_STS0_DLPHB(x) ((x) << 8)
226 #define FSPI_STS0_DLPHA(x) ((x) << 4)
227 #define FSPI_STS0_CMD_SRC(x) ((x) << 2)
228 #define FSPI_STS0_ARB_IDLE BIT(1)
229 #define FSPI_STS0_SEQ_IDLE BIT(0)
230
231 #define FSPI_STS1 0xE4
232 #define FSPI_STS1_IP_ERRCD(x) ((x) << 24)
233 #define FSPI_STS1_IP_ERRID(x) ((x) << 16)
234 #define FSPI_STS1_AHB_ERRCD(x) ((x) << 8)
235 #define FSPI_STS1_AHB_ERRID(x) (x)
236
237 #define FSPI_STS2 0xE8
238 #define FSPI_STS2_BREFLOCK BIT(17)
239 #define FSPI_STS2_BSLVLOCK BIT(16)
240 #define FSPI_STS2_AREFLOCK BIT(1)
241 #define FSPI_STS2_ASLVLOCK BIT(0)
242 #define FSPI_STS2_AB_LOCK (FSPI_STS2_BREFLOCK | \
243 FSPI_STS2_BSLVLOCK | \
244 FSPI_STS2_AREFLOCK | \
245 FSPI_STS2_ASLVLOCK)
246
247 #define FSPI_AHBSPNST 0xEC
248 #define FSPI_AHBSPNST_DATLFT(x) ((x) << 16)
249 #define FSPI_AHBSPNST_BUFID(x) ((x) << 1)
250 #define FSPI_AHBSPNST_ACTIVE BIT(0)
251
252 #define FSPI_IPRXFSTS 0xF0
253 #define FSPI_IPRXFSTS_RDCNTR(x) ((x) << 16)
254 #define FSPI_IPRXFSTS_FILL(x) (x)
255
256 #define FSPI_IPTXFSTS 0xF4
257 #define FSPI_IPTXFSTS_WRCNTR(x) ((x) << 16)
258 #define FSPI_IPTXFSTS_FILL(x) (x)
259
260 #define FSPI_RFDR 0x100
261 #define FSPI_TFDR 0x180
262
263 #define FSPI_LUT_BASE 0x200
264
265 /* register map end */
266
267 /* Instruction set for the LUT register. */
268 #define LUT_STOP 0x00
269 #define LUT_CMD 0x01
270 #define LUT_ADDR 0x02
271 #define LUT_CADDR_SDR 0x03
272 #define LUT_MODE 0x04
273 #define LUT_MODE2 0x05
274 #define LUT_MODE4 0x06
275 #define LUT_MODE8 0x07
276 #define LUT_NXP_WRITE 0x08
277 #define LUT_NXP_READ 0x09
278 #define LUT_LEARN_SDR 0x0A
279 #define LUT_DATSZ_SDR 0x0B
280 #define LUT_DUMMY 0x0C
281 #define LUT_DUMMY_RWDS_SDR 0x0D
282 #define LUT_JMP_ON_CS 0x1F
283 #define LUT_CMD_DDR 0x21
284 #define LUT_ADDR_DDR 0x22
285 #define LUT_CADDR_DDR 0x23
286 #define LUT_MODE_DDR 0x24
287 #define LUT_MODE2_DDR 0x25
288 #define LUT_MODE4_DDR 0x26
289 #define LUT_MODE8_DDR 0x27
290 #define LUT_WRITE_DDR 0x28
291 #define LUT_READ_DDR 0x29
292 #define LUT_LEARN_DDR 0x2A
293 #define LUT_DATSZ_DDR 0x2B
294 #define LUT_DUMMY_DDR 0x2C
295 #define LUT_DUMMY_RWDS_DDR 0x2D
296
297 /*
298 * Calculate number of required PAD bits for LUT register.
299 *
300 * The pad stands for the number of IO lines [0:7].
301 * For example, the octal read needs eight IO lines,
302 * so you should use LUT_PAD(8). This macro
303 * returns 3 i.e. use eight (2^3) IP lines for read.
304 */
305 #define LUT_PAD(x) (fls(x) - 1)
306
307 /*
308 * Macro for constructing the LUT entries with the following
309 * register layout:
310 *
311 * ---------------------------------------------------
312 * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 |
313 * ---------------------------------------------------
314 */
315 #define PAD_SHIFT 8
316 #define INSTR_SHIFT 10
317 #define OPRND_SHIFT 16
318
319 /* Macros for constructing the LUT register. */
320 #define LUT_DEF(idx, ins, pad, opr) \
321 ((((ins) << INSTR_SHIFT) | ((pad) << PAD_SHIFT) | \
322 (opr)) << (((idx) % 2) * OPRND_SHIFT))
323
324 #define POLL_TOUT 5000
325 #define NXP_FSPI_MAX_CHIPSELECT 4
326 #define NXP_FSPI_MIN_IOMAP SZ_4M
327
328 #define DCFG_RCWSR1 0x100
329 #define SYS_PLL_RAT GENMASK(6, 2)
330
331 /* Access flash memory using IP bus only */
332 #define FSPI_QUIRK_USE_IP_ONLY BIT(0)
333 /* Disable DTR */
334 #define FSPI_QUIRK_DISABLE_DTR BIT(1)
335
336 struct nxp_fspi_devtype_data {
337 unsigned int rxfifo;
338 unsigned int txfifo;
339 unsigned int ahb_buf_size;
340 unsigned int quirks;
341 unsigned int lut_num;
342 bool little_endian;
343 /*
344 * The max clock rate (Hz) that FlexSPI can output to the device
345 * in SDR mode (RXCLKSRC=0). Defaults to 66MHz if zero.
346 * Some SoCs (e.g. LX2160A) support up to 100MHz in SDR mode.
347 */
348 unsigned long max_sdr_rate;
349 /*
350 * The max clock rate (Hz) that FlexSPI can output to the device
351 * in DTR mode (RXCLKSRC=3). Defaults to 166MHz if zero.
352 * Some SoCs (e.g. i.MX95, i.MX8QM, i.MX8DXL) support up to 200MHz.
353 */
354 unsigned long max_dtr_rate;
355 };
356
357 static struct nxp_fspi_devtype_data lx2160a_data = {
358 .rxfifo = SZ_512, /* (64 * 64 bits) */
359 .txfifo = SZ_1K, /* (128 * 64 bits) */
360 .ahb_buf_size = SZ_2K, /* (256 * 64 bits) */
361 .quirks = FSPI_QUIRK_DISABLE_DTR,
362 .lut_num = 32,
363 .little_endian = true, /* little-endian */
364 /*
365 * LX2160ACEC: SDR RXCLKSRC=0 max 100MHz, DTR disabled via quirk.
366 */
367 .max_sdr_rate = 100000000,
368 };
369
370 static struct nxp_fspi_devtype_data imx8mm_data = {
371 .rxfifo = SZ_512, /* (64 * 64 bits) */
372 .txfifo = SZ_1K, /* (128 * 64 bits) */
373 .ahb_buf_size = SZ_2K, /* (256 * 64 bits) */
374 .quirks = 0,
375 .lut_num = 32,
376 .little_endian = true, /* little-endian */
377 /* IMX8MMCEC §3.9.10: SDR RXCLKSRC=0 max 66MHz, DDR RXCLKSRC=3 max 166MHz */
378 .max_sdr_rate = 66000000,
379 .max_dtr_rate = 166000000,
380 };
381
382 static struct nxp_fspi_devtype_data imx8mp_data = {
383 .rxfifo = SZ_512, /* (64 * 64 bits) */
384 .txfifo = SZ_1K, /* (128 * 64 bits) */
385 .ahb_buf_size = SZ_2K, /* (256 * 64 bits) */
386 .quirks = 0,
387 .lut_num = 32,
388 .little_endian = true, /* little-endian */
389 /* IMX8MPCEC: SDR RXCLKSRC=0 max 66MHz, DDR RXCLKSRC=3 max 166MHz */
390 .max_sdr_rate = 66000000,
391 .max_dtr_rate = 166000000,
392 };
393
394 static struct nxp_fspi_devtype_data imx8qxp_data = {
395 .rxfifo = SZ_512, /* (64 * 64 bits) */
396 .txfifo = SZ_1K, /* (128 * 64 bits) */
397 .ahb_buf_size = SZ_2K, /* (256 * 64 bits) */
398 .quirks = 0,
399 .lut_num = 32,
400 .little_endian = true, /* little-endian */
401 /*
402 * IMX8QXPCEC: SDR RXCLKSRC=0 max 60MHz, DDR RXCLKSRC=3 max 200MHz.
403 * i.MX8QM and i.MX8DXL share the same FlexSPI IP and limits.
404 */
405 .max_sdr_rate = 60000000,
406 .max_dtr_rate = 200000000,
407 };
408
409 static struct nxp_fspi_devtype_data imx8dxl_data = {
410 .rxfifo = SZ_512, /* (64 * 64 bits) */
411 .txfifo = SZ_1K, /* (128 * 64 bits) */
412 .ahb_buf_size = SZ_2K, /* (256 * 64 bits) */
413 .quirks = FSPI_QUIRK_USE_IP_ONLY,
414 .lut_num = 32,
415 .little_endian = true, /* little-endian */
416 /*
417 * IMX8DXLCEC (i.MX 8XLite): SDR RXCLKSRC=0 max 60MHz,
418 * DDR RXCLKSRC=3 max 200MHz.
419 */
420 .max_sdr_rate = 60000000,
421 .max_dtr_rate = 200000000,
422 };
423
424 static struct nxp_fspi_devtype_data imx8ulp_data = {
425 .rxfifo = SZ_512, /* (64 * 64 bits) */
426 .txfifo = SZ_1K, /* (128 * 64 bits) */
427 .ahb_buf_size = SZ_2K, /* (256 * 64 bits) */
428 .quirks = 0,
429 .lut_num = 16,
430 .little_endian = true, /* little-endian */
431 /*
432 * IMX8ULPCEC §7.3.1, Normal Drive (ND, 1.0V) mode:
433 * SDR RXCLKSRC=0 max 60MHz, DDR RXCLKSRC=3 max 166MHz.
434 * Note: Overdrive (OD, 1.05V) allows up to 180MHz DTR
435 * but is not the default use case.
436 */
437 .max_sdr_rate = 60000000,
438 .max_dtr_rate = 166000000,
439 };
440
441 static struct nxp_fspi_devtype_data imx95_data = {
442 .rxfifo = SZ_512, /* (64 * 64 bits) */
443 .txfifo = SZ_1K, /* (128 * 64 bits) */
444 .ahb_buf_size = SZ_2K, /* (256 * 64 bits) */
445 .quirks = 0,
446 .lut_num = 32,
447 .little_endian = true, /* little-endian */
448 /*
449 * IMX95CEC Rev.8 §4.11.7: SDR RXCLKSRC=0 max 66MHz,
450 * DDR RXCLKSRC=3 max 200MHz (Nominal/Overdrive mode).
451 */
452 .max_sdr_rate = 66000000,
453 .max_dtr_rate = 200000000,
454 };
455
456 struct nxp_fspi {
457 void __iomem *iobase;
458 void __iomem *ahb_addr;
459 u32 memmap_phy;
460 u32 memmap_phy_size;
461 u32 memmap_start;
462 u32 memmap_len;
463 struct clk *clk, *clk_en;
464 struct device *dev;
465 struct completion c;
466 struct nxp_fspi_devtype_data *devtype_data;
467 struct mutex lock;
468 struct pm_qos_request pm_qos_req;
469 int selected;
470 #define FSPI_NEED_INIT BIT(0)
471 #define FSPI_DTR_MODE BIT(1)
472 int flags;
473 /* save the previous operation clock rate */
474 unsigned long pre_op_rate;
475 /* the max clock rate fspi output to device */
476 unsigned long max_rate;
477 };
478
needs_ip_only(struct nxp_fspi * f)479 static inline int needs_ip_only(struct nxp_fspi *f)
480 {
481 return f->devtype_data->quirks & FSPI_QUIRK_USE_IP_ONLY;
482 }
483
484 /*
485 * R/W functions for big- or little-endian registers:
486 * The FSPI controller's endianness is independent of
487 * the CPU core's endianness. So far, although the CPU
488 * core is little-endian the FSPI controller can use
489 * big-endian or little-endian.
490 */
fspi_writel(struct nxp_fspi * f,u32 val,void __iomem * addr)491 static void fspi_writel(struct nxp_fspi *f, u32 val, void __iomem *addr)
492 {
493 if (f->devtype_data->little_endian)
494 iowrite32(val, addr);
495 else
496 iowrite32be(val, addr);
497 }
498
fspi_readl(struct nxp_fspi * f,void __iomem * addr)499 static u32 fspi_readl(struct nxp_fspi *f, void __iomem *addr)
500 {
501 if (f->devtype_data->little_endian)
502 return ioread32(addr);
503 else
504 return ioread32be(addr);
505 }
506
nxp_fspi_irq_handler(int irq,void * dev_id)507 static irqreturn_t nxp_fspi_irq_handler(int irq, void *dev_id)
508 {
509 struct nxp_fspi *f = dev_id;
510 u32 reg;
511
512 /* clear interrupt */
513 reg = fspi_readl(f, f->iobase + FSPI_INTR);
514 fspi_writel(f, FSPI_INTR_IPCMDDONE, f->iobase + FSPI_INTR);
515
516 if (reg & FSPI_INTR_IPCMDDONE)
517 complete(&f->c);
518
519 return IRQ_HANDLED;
520 }
521
nxp_fspi_check_buswidth(struct nxp_fspi * f,u8 width)522 static int nxp_fspi_check_buswidth(struct nxp_fspi *f, u8 width)
523 {
524 switch (width) {
525 case 1:
526 case 2:
527 case 4:
528 case 8:
529 return 0;
530 }
531
532 return -ENOTSUPP;
533 }
534
nxp_fspi_supports_op(struct spi_mem * mem,const struct spi_mem_op * op)535 static bool nxp_fspi_supports_op(struct spi_mem *mem,
536 const struct spi_mem_op *op)
537 {
538 struct nxp_fspi *f = spi_controller_get_devdata(mem->spi->controller);
539 int ret;
540
541 ret = nxp_fspi_check_buswidth(f, op->cmd.buswidth);
542
543 if (op->addr.nbytes)
544 ret |= nxp_fspi_check_buswidth(f, op->addr.buswidth);
545
546 if (op->dummy.nbytes)
547 ret |= nxp_fspi_check_buswidth(f, op->dummy.buswidth);
548
549 if (op->data.nbytes)
550 ret |= nxp_fspi_check_buswidth(f, op->data.buswidth);
551
552 if (ret)
553 return false;
554
555 /*
556 * The number of address bytes should be equal to or less than 4 bytes.
557 */
558 if (op->addr.nbytes > 4)
559 return false;
560
561 /*
562 * If requested address value is greater than controller assigned
563 * memory mapped space, return error as it didn't fit in the range
564 * of assigned address space.
565 */
566 if (op->addr.val >= f->memmap_phy_size)
567 return false;
568
569 /* Max 64 dummy clock cycles supported */
570 if (op->dummy.buswidth &&
571 (op->dummy.nbytes * 8 / op->dummy.buswidth > 64))
572 return false;
573
574 /* Max data length, check controller limits and alignment */
575 if (op->data.dir == SPI_MEM_DATA_IN &&
576 (op->data.nbytes > f->devtype_data->ahb_buf_size ||
577 (op->data.nbytes > f->devtype_data->rxfifo - 4 &&
578 !IS_ALIGNED(op->data.nbytes, 8))))
579 return false;
580
581 if (op->data.dir == SPI_MEM_DATA_OUT &&
582 op->data.nbytes > f->devtype_data->txfifo)
583 return false;
584
585 return spi_mem_default_supports_op(mem, op);
586 }
587
588 /* Instead of busy looping invoke readl_poll_timeout functionality. */
fspi_readl_poll_tout(struct nxp_fspi * f,void __iomem * base,u32 mask,u32 delay_us,u32 timeout_us,bool c)589 static int fspi_readl_poll_tout(struct nxp_fspi *f, void __iomem *base,
590 u32 mask, u32 delay_us,
591 u32 timeout_us, bool c)
592 {
593 u32 reg;
594
595 if (!f->devtype_data->little_endian)
596 mask = (u32)cpu_to_be32(mask);
597
598 if (c)
599 return readl_poll_timeout(base, reg, (reg & mask),
600 delay_us, timeout_us);
601 else
602 return readl_poll_timeout(base, reg, !(reg & mask),
603 delay_us, timeout_us);
604 }
605
606 /*
607 * If the target device content being changed by Write/Erase, need to
608 * invalidate the AHB buffer. This can be achieved by doing the reset
609 * of controller after setting MCR0[SWRESET] bit.
610 */
nxp_fspi_invalid(struct nxp_fspi * f)611 static inline void nxp_fspi_invalid(struct nxp_fspi *f)
612 {
613 u32 reg;
614 int ret;
615
616 reg = fspi_readl(f, f->iobase + FSPI_MCR0);
617 fspi_writel(f, reg | FSPI_MCR0_SWRST, f->iobase + FSPI_MCR0);
618
619 /* w1c register, wait unit clear */
620 ret = fspi_readl_poll_tout(f, f->iobase + FSPI_MCR0,
621 FSPI_MCR0_SWRST, 0, POLL_TOUT, false);
622 WARN_ON(ret);
623 }
624
nxp_fspi_prepare_lut(struct nxp_fspi * f,const struct spi_mem_op * op)625 static void nxp_fspi_prepare_lut(struct nxp_fspi *f,
626 const struct spi_mem_op *op)
627 {
628 void __iomem *base = f->iobase;
629 u32 lutval[4] = {};
630 int lutidx = 1, i;
631 u32 lut_offset = (f->devtype_data->lut_num - 1) * 4 * 4;
632 u32 target_lut_reg;
633
634 /* cmd */
635 if (op->cmd.dtr) {
636 lutval[0] |= LUT_DEF(0, LUT_CMD_DDR, LUT_PAD(op->cmd.buswidth),
637 op->cmd.opcode >> 8);
638 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_CMD_DDR,
639 LUT_PAD(op->cmd.buswidth),
640 op->cmd.opcode & 0xFF);
641 lutidx++;
642 } else {
643 lutval[0] |= LUT_DEF(0, LUT_CMD, LUT_PAD(op->cmd.buswidth),
644 op->cmd.opcode);
645 }
646
647 /* addr bytes */
648 if (op->addr.nbytes) {
649 lutval[lutidx / 2] |= LUT_DEF(lutidx, op->addr.dtr ? LUT_ADDR_DDR : LUT_ADDR,
650 LUT_PAD(op->addr.buswidth),
651 op->addr.nbytes * 8);
652 lutidx++;
653 }
654
655 /* dummy bytes, if needed */
656 if (op->dummy.nbytes) {
657 lutval[lutidx / 2] |= LUT_DEF(lutidx, op->dummy.dtr ? LUT_DUMMY_DDR : LUT_DUMMY,
658 /*
659 * Due to FlexSPI controller limitation number of PAD for dummy
660 * buswidth needs to be programmed as equal to data buswidth.
661 */
662 LUT_PAD(op->data.buswidth),
663 op->dummy.nbytes * 8 /
664 op->dummy.buswidth);
665 lutidx++;
666 }
667
668 /* read/write data bytes */
669 if (op->data.nbytes) {
670 lutval[lutidx / 2] |= LUT_DEF(lutidx,
671 op->data.dir == SPI_MEM_DATA_IN ?
672 (op->data.dtr ? LUT_READ_DDR : LUT_NXP_READ) :
673 (op->data.dtr ? LUT_WRITE_DDR : LUT_NXP_WRITE),
674 LUT_PAD(op->data.buswidth),
675 0);
676 lutidx++;
677 }
678
679 /* stop condition. */
680 lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_STOP, 0, 0);
681
682 /* unlock LUT */
683 fspi_writel(f, FSPI_LUTKEY_VALUE, f->iobase + FSPI_LUTKEY);
684 fspi_writel(f, FSPI_LCKER_UNLOCK, f->iobase + FSPI_LCKCR);
685
686 /* fill LUT */
687 for (i = 0; i < ARRAY_SIZE(lutval); i++) {
688 target_lut_reg = FSPI_LUT_BASE + lut_offset + i * 4;
689 fspi_writel(f, lutval[i], base + target_lut_reg);
690 }
691
692 dev_dbg(f->dev, "CMD[%02x] lutval[0:%08x 1:%08x 2:%08x 3:%08x], size: 0x%08x\n",
693 op->cmd.opcode, lutval[0], lutval[1], lutval[2], lutval[3], op->data.nbytes);
694
695 /* lock LUT */
696 fspi_writel(f, FSPI_LUTKEY_VALUE, f->iobase + FSPI_LUTKEY);
697 fspi_writel(f, FSPI_LCKER_LOCK, f->iobase + FSPI_LCKCR);
698 }
699
nxp_fspi_clk_prep_enable(struct nxp_fspi * f)700 static int nxp_fspi_clk_prep_enable(struct nxp_fspi *f)
701 {
702 int ret;
703
704 if (is_acpi_node(dev_fwnode(f->dev)))
705 return 0;
706
707 ret = clk_prepare_enable(f->clk_en);
708 if (ret)
709 return ret;
710
711 ret = clk_prepare_enable(f->clk);
712 if (ret) {
713 clk_disable_unprepare(f->clk_en);
714 return ret;
715 }
716
717 return 0;
718 }
719
nxp_fspi_clk_disable_unprep(struct nxp_fspi * f)720 static void nxp_fspi_clk_disable_unprep(struct nxp_fspi *f)
721 {
722 if (is_acpi_node(dev_fwnode(f->dev)))
723 return;
724
725 clk_disable_unprepare(f->clk);
726 clk_disable_unprepare(f->clk_en);
727
728 return;
729 }
730
731 /*
732 * Sample Clock source selection for Flash Reading
733 * Four modes defined by fspi:
734 * mode 0: Dummy Read strobe generated by FlexSPI Controller
735 * and loopback internally
736 * mode 1: Dummy Read strobe generated by FlexSPI Controller
737 * and loopback from DQS pad
738 * mode 2: Reserved
739 * mode 3: Flash provided Read strobe and input from DQS pad
740 *
741 * fspi default use mode 0 after reset
742 */
nxp_fspi_select_rx_sample_clk_source(struct nxp_fspi * f,bool op_is_dtr)743 static void nxp_fspi_select_rx_sample_clk_source(struct nxp_fspi *f,
744 bool op_is_dtr)
745 {
746 u32 reg;
747
748 /*
749 * For 8D-8D-8D mode, need to use mode 3 (Flash provided Read
750 * strobe and input from DQS pad), otherwise read operaton may
751 * meet issue.
752 * This mode require flash device connect the DQS pad on board.
753 * For other modes, still use mode 0, keep align with before.
754 * spi_nor_suspend will disable 8D-8D-8D mode, also need to
755 * change the mode back to mode 0.
756 */
757 reg = fspi_readl(f, f->iobase + FSPI_MCR0);
758 if (op_is_dtr) {
759 reg |= FSPI_MCR0_RXCLKSRC(3);
760 /*
761 * Use the SoC-specific DTR max rate if provided, otherwise
762 * fall back to 166MHz (limit from IMX8MN datasheet §3.9.9).
763 */
764 f->max_rate = f->devtype_data->max_dtr_rate ?
765 f->devtype_data->max_dtr_rate : 166000000;
766 } else { /*select mode 0 */
767 reg &= ~FSPI_MCR0_RXCLKSRC(3);
768 /*
769 * Use the SoC-specific SDR max rate if provided, otherwise
770 * fall back to 66MHz (limit from IMX8MN datasheet §3.9.9).
771 */
772 f->max_rate = f->devtype_data->max_sdr_rate ?
773 f->devtype_data->max_sdr_rate : 66000000;
774 }
775 fspi_writel(f, reg, f->iobase + FSPI_MCR0);
776 }
777
nxp_fspi_dll_calibration(struct nxp_fspi * f)778 static void nxp_fspi_dll_calibration(struct nxp_fspi *f)
779 {
780 int ret;
781
782 /* Reset the DLL, set the DLLRESET to 1 and then set to 0 */
783 fspi_writel(f, FSPI_DLLACR_DLLRESET, f->iobase + FSPI_DLLACR);
784 fspi_writel(f, FSPI_DLLBCR_DLLRESET, f->iobase + FSPI_DLLBCR);
785 fspi_writel(f, 0, f->iobase + FSPI_DLLACR);
786 fspi_writel(f, 0, f->iobase + FSPI_DLLBCR);
787
788 /*
789 * Enable the DLL calibration mode.
790 * The delay target for slave delay line is:
791 * ((SLVDLYTARGET+1) * 1/32 * clock cycle of reference clock.
792 * When clock rate > 100MHz, recommend SLVDLYTARGET is 0xF, which
793 * means half of clock cycle of reference clock.
794 */
795 fspi_writel(f, FSPI_DLLACR_DLLEN | FSPI_DLLACR_SLVDLY(0xF),
796 f->iobase + FSPI_DLLACR);
797 fspi_writel(f, FSPI_DLLBCR_DLLEN | FSPI_DLLBCR_SLVDLY(0xF),
798 f->iobase + FSPI_DLLBCR);
799
800 /* Wait to get REF/SLV lock */
801 ret = fspi_readl_poll_tout(f, f->iobase + FSPI_STS2, FSPI_STS2_AB_LOCK,
802 0, POLL_TOUT, true);
803 if (ret)
804 dev_warn(f->dev, "DLL lock failed, please fix it!\n");
805
806 /*
807 * For ERR050272, DLL lock status bit is not accurate,
808 * wait for 4us more as a workaround.
809 */
810 udelay(4);
811 }
812
813 /*
814 * Config the DLL register to default value, enable the target clock delay
815 * line delay cell override mode, and use 1 fixed delay cell in DLL delay
816 * chain, this is the suggested setting when clock rate < 100MHz.
817 */
nxp_fspi_dll_override(struct nxp_fspi * f)818 static void nxp_fspi_dll_override(struct nxp_fspi *f)
819 {
820 fspi_writel(f, FSPI_DLLACR_OVRDEN, f->iobase + FSPI_DLLACR);
821 fspi_writel(f, FSPI_DLLBCR_OVRDEN, f->iobase + FSPI_DLLBCR);
822 }
823
824 /*
825 * In FlexSPI controller, flash access is based on value of FSPI_FLSHXXCR0
826 * register and start base address of the target device.
827 *
828 * (Higher address)
829 * -------- <-- FLSHB2CR0
830 * | B2 |
831 * | |
832 * B2 start address --> -------- <-- FLSHB1CR0
833 * | B1 |
834 * | |
835 * B1 start address --> -------- <-- FLSHA2CR0
836 * | A2 |
837 * | |
838 * A2 start address --> -------- <-- FLSHA1CR0
839 * | A1 |
840 * | |
841 * A1 start address --> -------- (Lower address)
842 *
843 *
844 * Start base address defines the starting address range for given CS and
845 * FSPI_FLSHXXCR0 defines the size of the target device connected at given CS.
846 *
847 * But, different targets are having different combinations of number of CS,
848 * some targets only have single CS or two CS covering controller's full
849 * memory mapped space area.
850 * Thus, implementation is being done as independent of the size and number
851 * of the connected target device.
852 * Assign controller memory mapped space size as the size to the connected
853 * target device.
854 * Mark FLSHxxCR0 as zero initially and then assign value only to the selected
855 * chip-select Flash configuration register.
856 *
857 * For e.g. to access CS2 (B1), FLSHB1CR0 register would be equal to the
858 * memory mapped size of the controller.
859 * Value for rest of the CS FLSHxxCR0 register would be zero.
860 *
861 */
nxp_fspi_select_mem(struct nxp_fspi * f,struct spi_device * spi,const struct spi_mem_op * op)862 static int nxp_fspi_select_mem(struct nxp_fspi *f, struct spi_device *spi,
863 const struct spi_mem_op *op)
864 {
865 /* flexspi only support one DTR mode: 8D-8D-8D */
866 bool op_is_dtr = op->cmd.dtr && op->addr.dtr && op->dummy.dtr && op->data.dtr;
867 unsigned long rate = op->max_freq;
868 int ret;
869 uint64_t size_kb;
870 u32 reg;
871
872 /*
873 * Return when following condition all meet,
874 * 1, if previously selected target device is same as current
875 * requested target device.
876 * 2, the DTR or STR mode do not change.
877 * 3, previous operation max rate equals current one.
878 *
879 * For other case, need to re-config.
880 */
881 if ((f->selected == spi_get_chipselect(spi, 0)) &&
882 (!!(f->flags & FSPI_DTR_MODE) == op_is_dtr) &&
883 (f->pre_op_rate == op->max_freq))
884 return 0;
885
886 /* Reset FLSHxxCR0 registers */
887 fspi_writel(f, 0, f->iobase + FSPI_FLSHA1CR0);
888 fspi_writel(f, 0, f->iobase + FSPI_FLSHA2CR0);
889 fspi_writel(f, 0, f->iobase + FSPI_FLSHB1CR0);
890 fspi_writel(f, 0, f->iobase + FSPI_FLSHB2CR0);
891
892 /* Assign controller memory mapped space as size, KBytes, of flash. */
893 size_kb = FSPI_FLSHXCR0_SZ(f->memmap_phy_size);
894
895 fspi_writel(f, size_kb, f->iobase + FSPI_FLSHA1CR0 +
896 4 * spi_get_chipselect(spi, 0));
897
898 dev_dbg(f->dev, "Target device [CS:%x] selected\n", spi_get_chipselect(spi, 0));
899
900 /*
901 * Per the FlexSPI reference manual (initialization sequence), MCR0
902 * should be configured while the module is in stop mode (MCR0[MDIS] = 1).
903 * Enter stop mode before reconfiguring the RX sample clock source, then
904 * exit stop mode afterwards.
905 */
906 reg = fspi_readl(f, f->iobase + FSPI_MCR0);
907 fspi_writel(f, reg | FSPI_MCR0_MDIS, f->iobase + FSPI_MCR0);
908
909 nxp_fspi_select_rx_sample_clk_source(f, op_is_dtr);
910 rate = min(f->max_rate, op->max_freq);
911
912 if (op_is_dtr) {
913 f->flags |= FSPI_DTR_MODE;
914 /* For DTR mode, flexspi will default div 2 and output to device.
915 * so here to config the root clock to 2 * device rate.
916 */
917 rate = rate * 2;
918 } else {
919 f->flags &= ~FSPI_DTR_MODE;
920 }
921
922 nxp_fspi_clk_disable_unprep(f);
923
924 ret = clk_set_rate(f->clk, rate);
925 if (ret) {
926 /*
927 * clk_set_rate() failed with the clocks already disabled.
928 * Re-enable them so the enable count matches what the caller's
929 * pm_runtime_put() (runtime_suspend) will drop.
930 */
931 nxp_fspi_clk_prep_enable(f);
932 return ret;
933 }
934
935 ret = nxp_fspi_clk_prep_enable(f);
936 if (ret)
937 return ret;
938
939 /*
940 * Exit stop mode before running the DLL calibration. The DLL
941 * reference clock is derived from the module clock domain, which is
942 * gated while MCR0[MDIS] = 1. Calibrating the DLL in stop mode leaves
943 * the STS2 lock bits stuck at 0 (the DLL never runs), so clear MDIS
944 * first and let the module and reference clock run.
945 */
946 reg = fspi_readl(f, f->iobase + FSPI_MCR0);
947 fspi_writel(f, reg & ~FSPI_MCR0_MDIS, f->iobase + FSPI_MCR0);
948
949 /*
950 * If clock rate > 100MHz, then switch from DLL override mode to
951 * DLL calibration mode.
952 */
953 if (rate > 100000000)
954 nxp_fspi_dll_calibration(f);
955 else
956 nxp_fspi_dll_override(f);
957
958 f->pre_op_rate = op->max_freq;
959
960 f->selected = spi_get_chipselect(spi, 0);
961
962 return 0;
963 }
964
nxp_fspi_read_ahb(struct nxp_fspi * f,const struct spi_mem_op * op)965 static int nxp_fspi_read_ahb(struct nxp_fspi *f, const struct spi_mem_op *op)
966 {
967 u32 start = op->addr.val;
968 u32 len = op->data.nbytes;
969
970 /* if necessary, ioremap before AHB read */
971 if ((!f->ahb_addr) || start < f->memmap_start ||
972 start + len > f->memmap_start + f->memmap_len) {
973 if (f->ahb_addr)
974 iounmap(f->ahb_addr);
975
976 f->memmap_start = start;
977 f->memmap_len = max_t(u32, len, NXP_FSPI_MIN_IOMAP);
978
979 f->ahb_addr = ioremap(f->memmap_phy + f->memmap_start,
980 f->memmap_len);
981
982 if (!f->ahb_addr) {
983 dev_err(f->dev, "failed to alloc memory\n");
984 return -ENOMEM;
985 }
986 }
987
988 /* Read out the data directly from the AHB buffer. */
989 memcpy_fromio(op->data.buf.in,
990 f->ahb_addr + start - f->memmap_start, len);
991
992 return 0;
993 }
994
nxp_fspi_fill_txfifo(struct nxp_fspi * f,const struct spi_mem_op * op)995 static void nxp_fspi_fill_txfifo(struct nxp_fspi *f,
996 const struct spi_mem_op *op)
997 {
998 void __iomem *base = f->iobase;
999 int i, ret;
1000 u8 *buf = (u8 *) op->data.buf.out;
1001
1002 /* clear the TX FIFO. */
1003 fspi_writel(f, FSPI_IPTXFCR_CLR, base + FSPI_IPTXFCR);
1004
1005 /*
1006 * Default value of water mark level is 8 bytes, hence in single
1007 * write request controller can write max 8 bytes of data.
1008 */
1009
1010 for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 8); i += 8) {
1011 /* Wait for TXFIFO empty */
1012 ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
1013 FSPI_INTR_IPTXWE, 0,
1014 POLL_TOUT, true);
1015 WARN_ON(ret);
1016
1017 fspi_writel(f, *(u32 *) (buf + i), base + FSPI_TFDR);
1018 fspi_writel(f, *(u32 *) (buf + i + 4), base + FSPI_TFDR + 4);
1019 fspi_writel(f, FSPI_INTR_IPTXWE, base + FSPI_INTR);
1020 }
1021
1022 if (i < op->data.nbytes) {
1023 u32 data = 0;
1024 int j;
1025 int remaining = op->data.nbytes - i;
1026 /* Wait for TXFIFO empty */
1027 ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
1028 FSPI_INTR_IPTXWE, 0,
1029 POLL_TOUT, true);
1030 WARN_ON(ret);
1031
1032 for (j = 0; j < ALIGN(remaining, 4); j += 4) {
1033 memcpy(&data, buf + i + j, min_t(int, 4, remaining - j));
1034 fspi_writel(f, data, base + FSPI_TFDR + j);
1035 }
1036 fspi_writel(f, FSPI_INTR_IPTXWE, base + FSPI_INTR);
1037 }
1038 }
1039
nxp_fspi_read_rxfifo(struct nxp_fspi * f,const struct spi_mem_op * op)1040 static void nxp_fspi_read_rxfifo(struct nxp_fspi *f,
1041 const struct spi_mem_op *op)
1042 {
1043 void __iomem *base = f->iobase;
1044 int i, ret;
1045 int len = op->data.nbytes;
1046 u8 *buf = (u8 *) op->data.buf.in;
1047
1048 /*
1049 * Default value of water mark level is 8 bytes, hence in single
1050 * read request controller can read max 8 bytes of data.
1051 */
1052 for (i = 0; i < ALIGN_DOWN(len, 8); i += 8) {
1053 /* Wait for RXFIFO available */
1054 ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
1055 FSPI_INTR_IPRXWA, 0,
1056 POLL_TOUT, true);
1057 WARN_ON(ret);
1058
1059 *(u32 *)(buf + i) = fspi_readl(f, base + FSPI_RFDR);
1060 *(u32 *)(buf + i + 4) = fspi_readl(f, base + FSPI_RFDR + 4);
1061 /* move the FIFO pointer */
1062 fspi_writel(f, FSPI_INTR_IPRXWA, base + FSPI_INTR);
1063 }
1064
1065 if (i < len) {
1066 u32 tmp;
1067 int size, j;
1068
1069 buf = op->data.buf.in + i;
1070 /* Wait for RXFIFO available */
1071 ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
1072 FSPI_INTR_IPRXWA, 0,
1073 POLL_TOUT, true);
1074 WARN_ON(ret);
1075
1076 len = op->data.nbytes - i;
1077 for (j = 0; j < op->data.nbytes - i; j += 4) {
1078 tmp = fspi_readl(f, base + FSPI_RFDR + j);
1079 size = min(len, 4);
1080 memcpy(buf + j, &tmp, size);
1081 len -= size;
1082 }
1083 }
1084
1085 /* invalid the RXFIFO */
1086 fspi_writel(f, FSPI_IPRXFCR_CLR, base + FSPI_IPRXFCR);
1087 /* move the FIFO pointer */
1088 fspi_writel(f, FSPI_INTR_IPRXWA, base + FSPI_INTR);
1089 }
1090
nxp_fspi_do_op(struct nxp_fspi * f,const struct spi_mem_op * op)1091 static int nxp_fspi_do_op(struct nxp_fspi *f, const struct spi_mem_op *op)
1092 {
1093 void __iomem *base = f->iobase;
1094 int seqnum = 0;
1095 int err = 0;
1096 u32 reg, seqid_lut;
1097
1098 reg = fspi_readl(f, base + FSPI_IPRXFCR);
1099 /* invalid RXFIFO first */
1100 reg &= ~FSPI_IPRXFCR_DMA_EN;
1101 reg = reg | FSPI_IPRXFCR_CLR;
1102 fspi_writel(f, reg, base + FSPI_IPRXFCR);
1103
1104 reinit_completion(&f->c);
1105
1106 fspi_writel(f, op->addr.val, base + FSPI_IPCR0);
1107 /*
1108 * Always start the sequence at the same index since we update
1109 * the LUT at each exec_op() call. And also specify the DATA
1110 * length, since it's has not been specified in the LUT.
1111 */
1112 seqid_lut = f->devtype_data->lut_num - 1;
1113 fspi_writel(f, op->data.nbytes |
1114 (seqid_lut << FSPI_IPCR1_SEQID_SHIFT) |
1115 (seqnum << FSPI_IPCR1_SEQNUM_SHIFT),
1116 base + FSPI_IPCR1);
1117
1118 /* Trigger the LUT now. */
1119 fspi_writel(f, FSPI_IPCMD_TRG, base + FSPI_IPCMD);
1120
1121 /* Wait for the interrupt. */
1122 if (!wait_for_completion_timeout(&f->c, msecs_to_jiffies(1000)))
1123 err = -ETIMEDOUT;
1124
1125 /* Invoke IP data read, if request is of data read. */
1126 if (!err && op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN)
1127 nxp_fspi_read_rxfifo(f, op);
1128
1129 return err;
1130 }
1131
nxp_fspi_exec_op(struct spi_mem * mem,const struct spi_mem_op * op)1132 static int nxp_fspi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
1133 {
1134 struct nxp_fspi *f = spi_controller_get_devdata(mem->spi->controller);
1135 int err = 0;
1136
1137 guard(mutex)(&f->lock);
1138
1139 err = pm_runtime_get_sync(f->dev);
1140 if (err < 0) {
1141 dev_err(f->dev, "Failed to enable clock %d\n", __LINE__);
1142 return err;
1143 }
1144
1145 /* Wait for controller being ready. */
1146 err = fspi_readl_poll_tout(f, f->iobase + FSPI_STS0,
1147 FSPI_STS0_ARB_IDLE, 1, POLL_TOUT, true);
1148 WARN_ON(err);
1149
1150 err = nxp_fspi_select_mem(f, mem->spi, op);
1151 if (err) {
1152 /*
1153 * On failure the FlexSPI clock may be left disabled, so avoid
1154 * any further register access (which would trigger a synchronous
1155 * external abort) and bail out.
1156 */
1157 pm_runtime_put_autosuspend(f->dev);
1158 return err;
1159 }
1160
1161 nxp_fspi_prepare_lut(f, op);
1162 /*
1163 * If we have large chunks of data, we read them through the AHB bus by
1164 * accessing the mapped memory. In all other cases we use IP commands
1165 * to access the flash. Read via AHB bus may be corrupted due to
1166 * existence of an errata and therefore discard AHB read in such cases.
1167 */
1168 if (op->data.nbytes > (f->devtype_data->rxfifo - 4) &&
1169 op->data.dir == SPI_MEM_DATA_IN &&
1170 !needs_ip_only(f)) {
1171 err = nxp_fspi_read_ahb(f, op);
1172 } else {
1173 if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT)
1174 nxp_fspi_fill_txfifo(f, op);
1175
1176 err = nxp_fspi_do_op(f, op);
1177 }
1178
1179 /* Invalidate the data in the AHB buffer. */
1180 nxp_fspi_invalid(f);
1181
1182 pm_runtime_put_autosuspend(f->dev);
1183
1184 return err;
1185 }
1186
nxp_fspi_adjust_op_size(struct spi_mem * mem,struct spi_mem_op * op)1187 static int nxp_fspi_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
1188 {
1189 struct nxp_fspi *f = spi_controller_get_devdata(mem->spi->controller);
1190
1191 if (op->data.dir == SPI_MEM_DATA_OUT) {
1192 if (op->data.nbytes > f->devtype_data->txfifo)
1193 op->data.nbytes = f->devtype_data->txfifo;
1194 } else {
1195 if (op->data.nbytes > f->devtype_data->ahb_buf_size)
1196 op->data.nbytes = f->devtype_data->ahb_buf_size;
1197 else if (op->data.nbytes > (f->devtype_data->rxfifo - 4))
1198 op->data.nbytes = ALIGN_DOWN(op->data.nbytes, 8);
1199 }
1200
1201 /* Limit data bytes to RX FIFO in case of IP read only */
1202 if (op->data.dir == SPI_MEM_DATA_IN &&
1203 needs_ip_only(f) &&
1204 op->data.nbytes > f->devtype_data->rxfifo)
1205 op->data.nbytes = f->devtype_data->rxfifo;
1206
1207 return 0;
1208 }
1209
erratum_err050568(struct nxp_fspi * f)1210 static void erratum_err050568(struct nxp_fspi *f)
1211 {
1212 static const struct soc_device_attribute ls1028a_soc_attr[] = {
1213 { .family = "QorIQ LS1028A" },
1214 { /* sentinel */ }
1215 };
1216 struct regmap *map;
1217 u32 val, sys_pll_ratio;
1218 int ret;
1219
1220 /* Check for LS1028A family */
1221 if (!soc_device_match(ls1028a_soc_attr)) {
1222 dev_dbg(f->dev, "Errata applicable only for LS1028A\n");
1223 return;
1224 }
1225
1226 map = syscon_regmap_lookup_by_compatible("fsl,ls1028a-dcfg");
1227 if (IS_ERR(map)) {
1228 dev_err(f->dev, "No syscon regmap\n");
1229 goto err;
1230 }
1231
1232 ret = regmap_read(map, DCFG_RCWSR1, &val);
1233 if (ret < 0)
1234 goto err;
1235
1236 sys_pll_ratio = FIELD_GET(SYS_PLL_RAT, val);
1237 dev_dbg(f->dev, "val: 0x%08x, sys_pll_ratio: %d\n", val, sys_pll_ratio);
1238
1239 /* Use IP bus only if platform clock is 300MHz */
1240 if (sys_pll_ratio == 3)
1241 f->devtype_data->quirks |= FSPI_QUIRK_USE_IP_ONLY;
1242
1243 return;
1244
1245 err:
1246 dev_err(f->dev, "Errata cannot be executed. Read via IP bus may not work\n");
1247 }
1248
nxp_fspi_default_setup(struct nxp_fspi * f)1249 static int nxp_fspi_default_setup(struct nxp_fspi *f)
1250 {
1251 void __iomem *base = f->iobase;
1252 int ret, i;
1253 u32 reg, seqid_lut;
1254
1255 /* disable and unprepare clock to avoid glitch pass to controller */
1256 nxp_fspi_clk_disable_unprep(f);
1257
1258 /* the default frequency, we will change it later if necessary. */
1259 ret = clk_set_rate(f->clk, 20000000);
1260 if (ret)
1261 return ret;
1262
1263 ret = nxp_fspi_clk_prep_enable(f);
1264 if (ret)
1265 return ret;
1266
1267 /*
1268 * ERR050568: Flash access by FlexSPI AHB command may not work with
1269 * platform frequency equal to 300 MHz on LS1028A.
1270 * LS1028A reuses LX2160A compatible entry. Make errata applicable for
1271 * Layerscape LS1028A platform.
1272 */
1273 if (of_device_is_compatible(f->dev->of_node, "nxp,lx2160a-fspi"))
1274 erratum_err050568(f);
1275
1276 /* Reset the module */
1277 /* w1c register, wait unit clear */
1278 ret = fspi_readl_poll_tout(f, f->iobase + FSPI_MCR0,
1279 FSPI_MCR0_SWRST, 0, POLL_TOUT, false);
1280 WARN_ON(ret);
1281
1282 /* Disable the module */
1283 fspi_writel(f, FSPI_MCR0_MDIS, base + FSPI_MCR0);
1284
1285 nxp_fspi_dll_override(f);
1286
1287 /* enable module */
1288 fspi_writel(f, FSPI_MCR0_AHB_TIMEOUT(0xFF) |
1289 FSPI_MCR0_IP_TIMEOUT(0xFF) | (u32) FSPI_MCR0_OCTCOMB_EN,
1290 base + FSPI_MCR0);
1291
1292 /*
1293 * Disable same device enable bit and configure all target devices
1294 * independently.
1295 */
1296 reg = fspi_readl(f, f->iobase + FSPI_MCR2);
1297 reg = reg & ~(FSPI_MCR2_SAMEDEVICEEN);
1298 fspi_writel(f, reg, base + FSPI_MCR2);
1299
1300 /* AHB configuration for access buffer 0~7. */
1301 for (i = 0; i < 7; i++)
1302 fspi_writel(f, 0, base + FSPI_AHBRX_BUF0CR0 + 4 * i);
1303
1304 /*
1305 * Set ADATSZ with the maximum AHB buffer size to improve the read
1306 * performance.
1307 */
1308 fspi_writel(f, (f->devtype_data->ahb_buf_size / 8 |
1309 FSPI_AHBRXBUF0CR7_PREF), base + FSPI_AHBRX_BUF7CR0);
1310
1311 /* prefetch and no start address alignment limitation */
1312 fspi_writel(f, FSPI_AHBCR_PREF_EN | FSPI_AHBCR_RDADDROPT,
1313 base + FSPI_AHBCR);
1314
1315 /* Reset the FLSHxCR1 registers. */
1316 reg = FSPI_FLSHXCR1_TCSH(0x3) | FSPI_FLSHXCR1_TCSS(0x3);
1317 fspi_writel(f, reg, base + FSPI_FLSHA1CR1);
1318 fspi_writel(f, reg, base + FSPI_FLSHA2CR1);
1319 fspi_writel(f, reg, base + FSPI_FLSHB1CR1);
1320 fspi_writel(f, reg, base + FSPI_FLSHB2CR1);
1321
1322 /*
1323 * The driver only uses one single LUT entry, that is updated on
1324 * each call of exec_op(). Index 0 is preset at boot with a basic
1325 * read operation, so let's use the last entry.
1326 */
1327 seqid_lut = f->devtype_data->lut_num - 1;
1328 /* AHB Read - Set lut sequence ID for all CS. */
1329 fspi_writel(f, seqid_lut, base + FSPI_FLSHA1CR2);
1330 fspi_writel(f, seqid_lut, base + FSPI_FLSHA2CR2);
1331 fspi_writel(f, seqid_lut, base + FSPI_FLSHB1CR2);
1332 fspi_writel(f, seqid_lut, base + FSPI_FLSHB2CR2);
1333
1334 f->selected = -1;
1335
1336 /* enable the interrupt */
1337 fspi_writel(f, FSPI_INTEN_IPCMDDONE, base + FSPI_INTEN);
1338
1339 return 0;
1340 }
1341
nxp_fspi_get_name(struct spi_mem * mem)1342 static const char *nxp_fspi_get_name(struct spi_mem *mem)
1343 {
1344 struct nxp_fspi *f = spi_controller_get_devdata(mem->spi->controller);
1345 struct device *dev = &mem->spi->dev;
1346 const char *name;
1347
1348 // Set custom name derived from the platform_device of the controller.
1349 if (of_get_available_child_count(f->dev->of_node) == 1)
1350 return dev_name(f->dev);
1351
1352 name = devm_kasprintf(dev, GFP_KERNEL,
1353 "%s-%d", dev_name(f->dev),
1354 spi_get_chipselect(mem->spi, 0));
1355
1356 if (!name) {
1357 dev_err(dev, "failed to get memory for custom flash name\n");
1358 return ERR_PTR(-ENOMEM);
1359 }
1360
1361 return name;
1362 }
1363
1364 static const struct spi_controller_mem_ops nxp_fspi_mem_ops = {
1365 .adjust_op_size = nxp_fspi_adjust_op_size,
1366 .supports_op = nxp_fspi_supports_op,
1367 .exec_op = nxp_fspi_exec_op,
1368 .get_name = nxp_fspi_get_name,
1369 };
1370
1371 static const struct spi_controller_mem_caps nxp_fspi_mem_caps = {
1372 .dtr = true,
1373 .swap16 = false,
1374 .per_op_freq = true,
1375 };
1376
1377 static const struct spi_controller_mem_caps nxp_fspi_mem_caps_disable_dtr = {
1378 .dtr = false,
1379 .per_op_freq = true,
1380 };
1381
nxp_fspi_cleanup(void * data)1382 static void nxp_fspi_cleanup(void *data)
1383 {
1384 struct nxp_fspi *f = data;
1385
1386 /* enable clock first since there is register access */
1387 pm_runtime_get_sync(f->dev);
1388
1389 /* disable the hardware */
1390 fspi_writel(f, FSPI_MCR0_MDIS, f->iobase + FSPI_MCR0);
1391
1392 pm_runtime_disable(f->dev);
1393 pm_runtime_put_noidle(f->dev);
1394 nxp_fspi_clk_disable_unprep(f);
1395
1396 if (f->ahb_addr)
1397 iounmap(f->ahb_addr);
1398 }
1399
nxp_fspi_probe(struct platform_device * pdev)1400 static int nxp_fspi_probe(struct platform_device *pdev)
1401 {
1402 struct spi_controller *ctlr;
1403 struct device *dev = &pdev->dev;
1404 struct fwnode_handle *fwnode = dev_fwnode(dev);
1405 struct resource *res;
1406 struct nxp_fspi *f;
1407 int ret, irq;
1408 u32 reg;
1409
1410 ctlr = devm_spi_alloc_host(&pdev->dev, sizeof(*f));
1411 if (!ctlr)
1412 return -ENOMEM;
1413
1414 ctlr->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL |
1415 SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL;
1416
1417 f = spi_controller_get_devdata(ctlr);
1418 f->dev = dev;
1419 f->devtype_data = (struct nxp_fspi_devtype_data *)device_get_match_data(dev);
1420 if (!f->devtype_data)
1421 return -ENODEV;
1422
1423 platform_set_drvdata(pdev, f);
1424
1425 /* find the resources - configuration register address space */
1426 if (is_acpi_node(fwnode))
1427 f->iobase = devm_platform_ioremap_resource(pdev, 0);
1428 else
1429 f->iobase = devm_platform_ioremap_resource_byname(pdev, "fspi_base");
1430 if (IS_ERR(f->iobase))
1431 return PTR_ERR(f->iobase);
1432
1433 /* find the resources - controller memory mapped space */
1434 if (is_acpi_node(fwnode))
1435 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1436 else
1437 res = platform_get_resource_byname(pdev,
1438 IORESOURCE_MEM, "fspi_mmap");
1439 if (!res)
1440 return -ENODEV;
1441
1442 /* assign memory mapped starting address and mapped size. */
1443 f->memmap_phy = res->start;
1444 f->memmap_phy_size = resource_size(res);
1445
1446 /* find the clocks */
1447 if (is_of_node(fwnode)) {
1448 f->clk_en = devm_clk_get(dev, "fspi_en");
1449 if (IS_ERR(f->clk_en))
1450 return PTR_ERR(f->clk_en);
1451
1452 f->clk = devm_clk_get(dev, "fspi");
1453 if (IS_ERR(f->clk))
1454 return PTR_ERR(f->clk);
1455 }
1456
1457 /* find the irq */
1458 irq = platform_get_irq(pdev, 0);
1459 if (irq < 0)
1460 return dev_err_probe(dev, irq, "Failed to get irq source");
1461
1462 pm_runtime_enable(dev);
1463 pm_runtime_set_autosuspend_delay(dev, FSPI_RPM_TIMEOUT);
1464 pm_runtime_use_autosuspend(dev);
1465
1466 /* enable clock */
1467 ret = pm_runtime_resume_and_get(f->dev);
1468 if (ret < 0) {
1469 ret = dev_err_probe(dev, ret, "Failed to enable clock");
1470 goto err_disable_pm;
1471 }
1472
1473 /* Clear potential interrupts */
1474 reg = fspi_readl(f, f->iobase + FSPI_INTR);
1475 if (reg)
1476 fspi_writel(f, reg, f->iobase + FSPI_INTR);
1477
1478 nxp_fspi_default_setup(f);
1479
1480 ret = pm_runtime_put_sync(dev);
1481 if (ret < 0) {
1482 ret = dev_err_probe(dev, ret, "Failed to disable clock");
1483 goto err_disable_pm;
1484 }
1485
1486 init_completion(&f->c);
1487 ret = devm_request_irq(dev, irq,
1488 nxp_fspi_irq_handler, 0, pdev->name, f);
1489 if (ret) {
1490 ret = dev_err_probe(dev, ret, "Failed to request irq\n");
1491 goto err_disable_pm;
1492 }
1493
1494 ret = devm_mutex_init(dev, &f->lock);
1495 if (ret) {
1496 ret = dev_err_probe(dev, ret, "Failed to initialize lock\n");
1497 goto err_disable_pm;
1498 }
1499
1500 ctlr->bus_num = -1;
1501 ctlr->num_chipselect = NXP_FSPI_MAX_CHIPSELECT;
1502 ctlr->mem_ops = &nxp_fspi_mem_ops;
1503
1504 if (f->devtype_data->quirks & FSPI_QUIRK_DISABLE_DTR)
1505 ctlr->mem_caps = &nxp_fspi_mem_caps_disable_dtr;
1506 else
1507 ctlr->mem_caps = &nxp_fspi_mem_caps;
1508
1509 ret = devm_add_action_or_reset(dev, nxp_fspi_cleanup, f);
1510 if (ret)
1511 return ret;
1512
1513 return devm_spi_register_controller(&pdev->dev, ctlr);
1514
1515 err_disable_pm:
1516 pm_runtime_dont_use_autosuspend(dev);
1517 pm_runtime_disable(dev);
1518 return ret;
1519 }
1520
nxp_fspi_runtime_suspend(struct device * dev)1521 static int nxp_fspi_runtime_suspend(struct device *dev)
1522 {
1523 struct nxp_fspi *f = dev_get_drvdata(dev);
1524
1525 nxp_fspi_clk_disable_unprep(f);
1526
1527 return 0;
1528 }
1529
nxp_fspi_runtime_resume(struct device * dev)1530 static int nxp_fspi_runtime_resume(struct device *dev)
1531 {
1532 struct nxp_fspi *f = dev_get_drvdata(dev);
1533 int ret;
1534
1535 ret = nxp_fspi_clk_prep_enable(f);
1536 if (ret)
1537 return ret;
1538
1539 if (f->flags & FSPI_NEED_INIT) {
1540 nxp_fspi_default_setup(f);
1541 ret = pinctrl_pm_select_default_state(dev);
1542 if (ret)
1543 dev_err(dev, "select flexspi default pinctrl failed!\n");
1544 f->flags &= ~FSPI_NEED_INIT;
1545 }
1546
1547 return ret;
1548 }
1549
nxp_fspi_suspend(struct device * dev)1550 static int nxp_fspi_suspend(struct device *dev)
1551 {
1552 struct nxp_fspi *f = dev_get_drvdata(dev);
1553 int ret;
1554
1555 ret = pinctrl_pm_select_sleep_state(dev);
1556 if (ret) {
1557 dev_err(dev, "select flexspi sleep pinctrl failed!\n");
1558 return ret;
1559 }
1560
1561 f->flags |= FSPI_NEED_INIT;
1562
1563 return pm_runtime_force_suspend(dev);
1564 }
1565
1566 static const struct dev_pm_ops nxp_fspi_pm_ops = {
1567 RUNTIME_PM_OPS(nxp_fspi_runtime_suspend, nxp_fspi_runtime_resume, NULL)
1568 SYSTEM_SLEEP_PM_OPS(nxp_fspi_suspend, pm_runtime_force_resume)
1569 };
1570
1571 static const struct of_device_id nxp_fspi_dt_ids[] = {
1572 { .compatible = "nxp,lx2160a-fspi", .data = (void *)&lx2160a_data, },
1573 { .compatible = "nxp,imx8mm-fspi", .data = (void *)&imx8mm_data, },
1574 { .compatible = "nxp,imx8mp-fspi", .data = (void *)&imx8mp_data, },
1575 { .compatible = "nxp,imx8qxp-fspi", .data = (void *)&imx8qxp_data, },
1576 { .compatible = "nxp,imx8dxl-fspi", .data = (void *)&imx8dxl_data, },
1577 { .compatible = "nxp,imx8ulp-fspi", .data = (void *)&imx8ulp_data, },
1578 { .compatible = "nxp,imx95-fspi", .data = (void *)&imx95_data, },
1579 { /* sentinel */ }
1580 };
1581 MODULE_DEVICE_TABLE(of, nxp_fspi_dt_ids);
1582
1583 #ifdef CONFIG_ACPI
1584 static const struct acpi_device_id nxp_fspi_acpi_ids[] = {
1585 { "NXP0009", .driver_data = (kernel_ulong_t)&lx2160a_data, },
1586 {}
1587 };
1588 MODULE_DEVICE_TABLE(acpi, nxp_fspi_acpi_ids);
1589 #endif
1590
1591 static struct platform_driver nxp_fspi_driver = {
1592 .driver = {
1593 .name = "nxp-fspi",
1594 .of_match_table = nxp_fspi_dt_ids,
1595 .acpi_match_table = ACPI_PTR(nxp_fspi_acpi_ids),
1596 .pm = pm_ptr(&nxp_fspi_pm_ops),
1597 },
1598 .probe = nxp_fspi_probe,
1599 };
1600 module_platform_driver(nxp_fspi_driver);
1601
1602 MODULE_DESCRIPTION("NXP FSPI Controller Driver");
1603 MODULE_AUTHOR("NXP Semiconductor");
1604 MODULE_AUTHOR("Yogesh Narayan Gaur <yogeshnarayan.gaur@nxp.com>");
1605 MODULE_AUTHOR("Boris Brezillon <bbrezillon@kernel.org>");
1606 MODULE_AUTHOR("Frieder Schrempf <frieder.schrempf@kontron.de>");
1607 MODULE_LICENSE("GPL v2");
1608