]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
91afd36f | 2 | |
6b57ff6f | 3 | /* |
91afd36f KS |
4 | * Freescale QuadSPI driver. |
5 | * | |
6 | * Copyright (C) 2013 Freescale Semiconductor, Inc. | |
7 | * Copyright (C) 2018 Bootlin | |
8 | * Copyright (C) 2018 exceet electronics GmbH | |
9 | * Copyright (C) 2018 Kontron Electronics GmbH | |
10 | * Copyright 2019-2020 NXP | |
11 | * | |
12 | * This driver is a ported version of Linux Freescale QSPI driver taken from | |
13 | * v5.5-rc1 tag having following information. | |
6b57ff6f | 14 | * |
91afd36f KS |
15 | * Transition to SPI MEM interface: |
16 | * Authors: | |
17 | * Boris Brezillon <[email protected]> | |
18 | * Frieder Schrempf <[email protected]> | |
19 | * Yogesh Gaur <[email protected]> | |
20 | * Suresh Gupta <[email protected]> | |
21 | * | |
22 | * Based on the original fsl-quadspi.c spi-nor driver. | |
23 | * Transition to spi-mem in spi-fsl-qspi.c | |
6b57ff6f AW |
24 | */ |
25 | ||
26 | #include <common.h> | |
f7ae49fc | 27 | #include <log.h> |
6b57ff6f | 28 | #include <asm/io.h> |
cd93d625 | 29 | #include <linux/bitops.h> |
c05ed00a | 30 | #include <linux/delay.h> |
4d72caa5 SG |
31 | #include <linux/libfdt.h> |
32 | #include <linux/sizes.h> | |
33 | #include <linux/iopoll.h> | |
5bc48308 | 34 | #include <dm.h> |
91afd36f KS |
35 | #include <linux/iopoll.h> |
36 | #include <linux/sizes.h> | |
37 | #include <linux/err.h> | |
38 | #include <spi.h> | |
39 | #include <spi-mem.h> | |
6b57ff6f | 40 | |
5bc48308 HW |
41 | DECLARE_GLOBAL_DATA_PTR; |
42 | ||
91afd36f KS |
43 | /* |
44 | * The driver only uses one single LUT entry, that is updated on | |
45 | * each call of exec_op(). Index 0 is preset at boot with a basic | |
46 | * read operation, so let's use the last entry (15). | |
47 | */ | |
48 | #define SEQID_LUT 15 | |
def88bce | 49 | #define SEQID_LUT_AHB 14 |
91afd36f KS |
50 | |
51 | /* Registers used by the driver */ | |
52 | #define QUADSPI_MCR 0x00 | |
53 | #define QUADSPI_MCR_RESERVED_MASK GENMASK(19, 16) | |
54 | #define QUADSPI_MCR_MDIS_MASK BIT(14) | |
55 | #define QUADSPI_MCR_CLR_TXF_MASK BIT(11) | |
56 | #define QUADSPI_MCR_CLR_RXF_MASK BIT(10) | |
57 | #define QUADSPI_MCR_DDR_EN_MASK BIT(7) | |
58 | #define QUADSPI_MCR_END_CFG_MASK GENMASK(3, 2) | |
59 | #define QUADSPI_MCR_SWRSTHD_MASK BIT(1) | |
60 | #define QUADSPI_MCR_SWRSTSD_MASK BIT(0) | |
61 | ||
62 | #define QUADSPI_IPCR 0x08 | |
63 | #define QUADSPI_IPCR_SEQID(x) ((x) << 24) | |
64 | #define QUADSPI_FLSHCR 0x0c | |
65 | #define QUADSPI_FLSHCR_TCSS_MASK GENMASK(3, 0) | |
66 | #define QUADSPI_FLSHCR_TCSH_MASK GENMASK(11, 8) | |
67 | #define QUADSPI_FLSHCR_TDH_MASK GENMASK(17, 16) | |
68 | ||
69 | #define QUADSPI_BUF3CR 0x1c | |
70 | #define QUADSPI_BUF3CR_ALLMST_MASK BIT(31) | |
71 | #define QUADSPI_BUF3CR_ADATSZ(x) ((x) << 8) | |
72 | #define QUADSPI_BUF3CR_ADATSZ_MASK GENMASK(15, 8) | |
73 | ||
74 | #define QUADSPI_BFGENCR 0x20 | |
75 | #define QUADSPI_BFGENCR_SEQID(x) ((x) << 12) | |
76 | ||
77 | #define QUADSPI_BUF0IND 0x30 | |
78 | #define QUADSPI_BUF1IND 0x34 | |
79 | #define QUADSPI_BUF2IND 0x38 | |
80 | #define QUADSPI_SFAR 0x100 | |
81 | ||
82 | #define QUADSPI_SMPR 0x108 | |
83 | #define QUADSPI_SMPR_DDRSMP_MASK GENMASK(18, 16) | |
84 | #define QUADSPI_SMPR_FSDLY_MASK BIT(6) | |
85 | #define QUADSPI_SMPR_FSPHS_MASK BIT(5) | |
86 | #define QUADSPI_SMPR_HSENA_MASK BIT(0) | |
87 | ||
88 | #define QUADSPI_RBCT 0x110 | |
89 | #define QUADSPI_RBCT_WMRK_MASK GENMASK(4, 0) | |
90 | #define QUADSPI_RBCT_RXBRD_USEIPS BIT(8) | |
91 | ||
92 | #define QUADSPI_TBDR 0x154 | |
93 | ||
94 | #define QUADSPI_SR 0x15c | |
95 | #define QUADSPI_SR_IP_ACC_MASK BIT(1) | |
96 | #define QUADSPI_SR_AHB_ACC_MASK BIT(2) | |
97 | ||
98 | #define QUADSPI_FR 0x160 | |
99 | #define QUADSPI_FR_TFF_MASK BIT(0) | |
100 | ||
101 | #define QUADSPI_RSER 0x164 | |
102 | #define QUADSPI_RSER_TFIE BIT(0) | |
103 | ||
104 | #define QUADSPI_SPTRCLR 0x16c | |
105 | #define QUADSPI_SPTRCLR_IPPTRC BIT(8) | |
106 | #define QUADSPI_SPTRCLR_BFPTRC BIT(0) | |
107 | ||
108 | #define QUADSPI_SFA1AD 0x180 | |
109 | #define QUADSPI_SFA2AD 0x184 | |
110 | #define QUADSPI_SFB1AD 0x188 | |
111 | #define QUADSPI_SFB2AD 0x18c | |
112 | #define QUADSPI_RBDR(x) (0x200 + ((x) * 4)) | |
113 | ||
114 | #define QUADSPI_LUTKEY 0x300 | |
115 | #define QUADSPI_LUTKEY_VALUE 0x5AF05AF0 | |
116 | ||
117 | #define QUADSPI_LCKCR 0x304 | |
118 | #define QUADSPI_LCKER_LOCK BIT(0) | |
119 | #define QUADSPI_LCKER_UNLOCK BIT(1) | |
120 | ||
121 | #define QUADSPI_LUT_BASE 0x310 | |
122 | #define QUADSPI_LUT_OFFSET (SEQID_LUT * 4 * 4) | |
123 | #define QUADSPI_LUT_REG(idx) \ | |
124 | (QUADSPI_LUT_BASE + QUADSPI_LUT_OFFSET + (idx) * 4) | |
125 | ||
def88bce YL |
126 | #define QUADSPI_AHB_LUT_OFFSET (SEQID_LUT_AHB * 4 * 4) |
127 | #define QUADSPI_AHB_LUT_REG(idx) \ | |
128 | (QUADSPI_LUT_BASE + QUADSPI_AHB_LUT_OFFSET + (idx) * 4) | |
129 | ||
91afd36f KS |
130 | /* Instruction set for the LUT register */ |
131 | #define LUT_STOP 0 | |
132 | #define LUT_CMD 1 | |
133 | #define LUT_ADDR 2 | |
134 | #define LUT_DUMMY 3 | |
135 | #define LUT_MODE 4 | |
136 | #define LUT_MODE2 5 | |
137 | #define LUT_MODE4 6 | |
138 | #define LUT_FSL_READ 7 | |
139 | #define LUT_FSL_WRITE 8 | |
140 | #define LUT_JMP_ON_CS 9 | |
141 | #define LUT_ADDR_DDR 10 | |
142 | #define LUT_MODE_DDR 11 | |
143 | #define LUT_MODE2_DDR 12 | |
144 | #define LUT_MODE4_DDR 13 | |
145 | #define LUT_FSL_READ_DDR 14 | |
146 | #define LUT_FSL_WRITE_DDR 15 | |
147 | #define LUT_DATA_LEARN 16 | |
148 | ||
149 | /* | |
150 | * The PAD definitions for LUT register. | |
151 | * | |
152 | * The pad stands for the number of IO lines [0:3]. | |
153 | * For example, the quad read needs four IO lines, | |
154 | * so you should use LUT_PAD(4). | |
155 | */ | |
156 | #define LUT_PAD(x) (fls(x) - 1) | |
157 | ||
158 | /* | |
159 | * Macro for constructing the LUT entries with the following | |
160 | * register layout: | |
161 | * | |
162 | * --------------------------------------------------- | |
163 | * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 | | |
164 | * --------------------------------------------------- | |
165 | */ | |
166 | #define LUT_DEF(idx, ins, pad, opr) \ | |
167 | ((((ins) << 10) | ((pad) << 8) | (opr)) << (((idx) % 2) * 16)) | |
168 | ||
169 | /* Controller needs driver to swap endianness */ | |
ce7575a8 YL |
170 | #define QUADSPI_QUIRK_SWAP_ENDIAN BIT(0) |
171 | ||
91afd36f KS |
172 | /* Controller needs 4x internal clock */ |
173 | #define QUADSPI_QUIRK_4X_INT_CLK BIT(1) | |
ce7575a8 | 174 | |
91afd36f KS |
175 | /* |
176 | * TKT253890, the controller needs the driver to fill the txfifo with | |
177 | * 16 bytes at least to trigger a data transfer, even though the extra | |
178 | * data won't be transferred. | |
179 | */ | |
180 | #define QUADSPI_QUIRK_TKT253890 BIT(2) | |
ce7575a8 | 181 | |
91afd36f KS |
182 | /* TKT245618, the controller cannot wake up from wait mode */ |
183 | #define QUADSPI_QUIRK_TKT245618 BIT(3) | |
184 | ||
185 | /* | |
186 | * Controller adds QSPI_AMBA_BASE (base address of the mapped memory) | |
187 | * internally. No need to add it when setting SFXXAD and SFAR registers | |
5bc48308 | 188 | */ |
91afd36f | 189 | #define QUADSPI_QUIRK_BASE_INTERNAL BIT(4) |
5bc48308 | 190 | |
91afd36f KS |
191 | /* |
192 | * Controller uses TDH bits in register QUADSPI_FLSHCR. | |
193 | * They need to be set in accordance with the DDR/SDR mode. | |
5bc48308 | 194 | */ |
91afd36f KS |
195 | #define QUADSPI_QUIRK_USE_TDH_SETTING BIT(5) |
196 | ||
def88bce YL |
197 | /* |
198 | * Controller only has Two CS on flash A, no flash B port | |
199 | */ | |
200 | #define QUADSPI_QUIRK_SINGLE_BUS BIT(6) | |
201 | ||
91afd36f KS |
202 | struct fsl_qspi_devtype_data { |
203 | unsigned int rxfifo; | |
204 | unsigned int txfifo; | |
205 | unsigned int ahb_buf_size; | |
206 | unsigned int quirks; | |
207 | bool little_endian; | |
6b57ff6f AW |
208 | }; |
209 | ||
ce7575a8 | 210 | static const struct fsl_qspi_devtype_data vybrid_data = { |
91afd36f KS |
211 | .rxfifo = SZ_128, |
212 | .txfifo = SZ_64, | |
213 | .ahb_buf_size = SZ_1K, | |
214 | .quirks = QUADSPI_QUIRK_SWAP_ENDIAN, | |
215 | .little_endian = true, | |
ce7575a8 YL |
216 | }; |
217 | ||
218 | static const struct fsl_qspi_devtype_data imx6sx_data = { | |
91afd36f KS |
219 | .rxfifo = SZ_128, |
220 | .txfifo = SZ_512, | |
221 | .ahb_buf_size = SZ_1K, | |
222 | .quirks = QUADSPI_QUIRK_4X_INT_CLK | QUADSPI_QUIRK_TKT245618, | |
223 | .little_endian = true, | |
224 | }; | |
225 | ||
226 | static const struct fsl_qspi_devtype_data imx7d_data = { | |
227 | .rxfifo = SZ_128, | |
228 | .txfifo = SZ_512, | |
229 | .ahb_buf_size = SZ_1K, | |
230 | .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_4X_INT_CLK | | |
231 | QUADSPI_QUIRK_USE_TDH_SETTING, | |
232 | .little_endian = true, | |
ce7575a8 YL |
233 | }; |
234 | ||
91afd36f KS |
235 | static const struct fsl_qspi_devtype_data imx6ul_data = { |
236 | .rxfifo = SZ_128, | |
237 | .txfifo = SZ_512, | |
238 | .ahb_buf_size = SZ_1K, | |
239 | .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_4X_INT_CLK | | |
240 | QUADSPI_QUIRK_USE_TDH_SETTING, | |
241 | .little_endian = true, | |
ce7575a8 | 242 | }; |
5bc48308 | 243 | |
93d6c8f7 YL |
244 | static const struct fsl_qspi_devtype_data imx7ulp_data = { |
245 | .rxfifo = SZ_64, | |
246 | .txfifo = SZ_64, | |
247 | .ahb_buf_size = SZ_128, | |
248 | .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_4X_INT_CLK | | |
def88bce | 249 | QUADSPI_QUIRK_USE_TDH_SETTING | QUADSPI_QUIRK_SINGLE_BUS, |
93d6c8f7 YL |
250 | .little_endian = true, |
251 | }; | |
252 | ||
91afd36f KS |
253 | static const struct fsl_qspi_devtype_data ls1021a_data = { |
254 | .rxfifo = SZ_128, | |
255 | .txfifo = SZ_64, | |
256 | .ahb_buf_size = SZ_1K, | |
257 | .quirks = 0, | |
258 | .little_endian = false, | |
9699fb4d YL |
259 | }; |
260 | ||
91afd36f KS |
261 | static const struct fsl_qspi_devtype_data ls1088a_data = { |
262 | .rxfifo = SZ_128, | |
263 | .txfifo = SZ_128, | |
264 | .ahb_buf_size = SZ_1K, | |
265 | .quirks = QUADSPI_QUIRK_TKT253890, | |
266 | .little_endian = true, | |
267 | }; | |
268 | ||
269 | static const struct fsl_qspi_devtype_data ls2080a_data = { | |
270 | .rxfifo = SZ_128, | |
271 | .txfifo = SZ_64, | |
272 | .ahb_buf_size = SZ_1K, | |
273 | .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_BASE_INTERNAL, | |
274 | .little_endian = true, | |
275 | }; | |
276 | ||
277 | struct fsl_qspi { | |
278 | struct udevice *dev; | |
279 | void __iomem *iobase; | |
280 | void __iomem *ahb_addr; | |
281 | u32 memmap_phy; | |
def88bce | 282 | u32 memmap_size; |
91afd36f KS |
283 | const struct fsl_qspi_devtype_data *devtype_data; |
284 | int selected; | |
285 | }; | |
286 | ||
287 | static inline int needs_swap_endian(struct fsl_qspi *q) | |
5bc48308 | 288 | { |
91afd36f | 289 | return q->devtype_data->quirks & QUADSPI_QUIRK_SWAP_ENDIAN; |
5bc48308 HW |
290 | } |
291 | ||
91afd36f | 292 | static inline int needs_4x_clock(struct fsl_qspi *q) |
5bc48308 | 293 | { |
91afd36f | 294 | return q->devtype_data->quirks & QUADSPI_QUIRK_4X_INT_CLK; |
5bc48308 | 295 | } |
6b57ff6f | 296 | |
91afd36f | 297 | static inline int needs_fill_txfifo(struct fsl_qspi *q) |
1f553564 | 298 | { |
91afd36f | 299 | return q->devtype_data->quirks & QUADSPI_QUIRK_TKT253890; |
1f553564 RS |
300 | } |
301 | ||
91afd36f | 302 | static inline int needs_wakeup_wait_mode(struct fsl_qspi *q) |
6b57ff6f | 303 | { |
91afd36f | 304 | return q->devtype_data->quirks & QUADSPI_QUIRK_TKT245618; |
6b57ff6f AW |
305 | } |
306 | ||
91afd36f | 307 | static inline int needs_amba_base_offset(struct fsl_qspi *q) |
6b57ff6f | 308 | { |
91afd36f KS |
309 | return !(q->devtype_data->quirks & QUADSPI_QUIRK_BASE_INTERNAL); |
310 | } | |
febffe8d | 311 | |
91afd36f KS |
312 | static inline int needs_tdh_setting(struct fsl_qspi *q) |
313 | { | |
314 | return q->devtype_data->quirks & QUADSPI_QUIRK_USE_TDH_SETTING; | |
6b57ff6f AW |
315 | } |
316 | ||
def88bce YL |
317 | static inline int needs_single_bus(struct fsl_qspi *q) |
318 | { | |
319 | return q->devtype_data->quirks & QUADSPI_QUIRK_SINGLE_BUS; | |
320 | } | |
321 | ||
5f7f70c1 | 322 | /* |
91afd36f KS |
323 | * An IC bug makes it necessary to rearrange the 32-bit data. |
324 | * Later chips, such as IMX6SLX, have fixed this bug. | |
5f7f70c1 | 325 | */ |
91afd36f | 326 | static inline u32 fsl_qspi_endian_xchg(struct fsl_qspi *q, u32 a) |
5f7f70c1 | 327 | { |
91afd36f | 328 | return needs_swap_endian(q) ? __swab32(a) : a; |
5f7f70c1 PF |
329 | } |
330 | ||
91afd36f KS |
331 | /* |
332 | * R/W functions for big- or little-endian registers: | |
333 | * The QSPI controller's endianness is independent of | |
334 | * the CPU core's endianness. So far, although the CPU | |
335 | * core is little-endian the QSPI controller can use | |
336 | * big-endian or little-endian. | |
337 | */ | |
338 | static void qspi_writel(struct fsl_qspi *q, u32 val, void __iomem *addr) | |
5f7f70c1 | 339 | { |
91afd36f KS |
340 | if (q->devtype_data->little_endian) |
341 | out_le32(addr, val); | |
342 | else | |
343 | out_be32(addr, val); | |
344 | } | |
5f7f70c1 | 345 | |
91afd36f KS |
346 | static u32 qspi_readl(struct fsl_qspi *q, void __iomem *addr) |
347 | { | |
348 | if (q->devtype_data->little_endian) | |
349 | return in_le32(addr); | |
5f7f70c1 | 350 | |
91afd36f | 351 | return in_be32(addr); |
5f7f70c1 PF |
352 | } |
353 | ||
91afd36f | 354 | static int fsl_qspi_check_buswidth(struct fsl_qspi *q, u8 width) |
5f7f70c1 | 355 | { |
91afd36f KS |
356 | switch (width) { |
357 | case 1: | |
358 | case 2: | |
359 | case 4: | |
360 | return 0; | |
361 | } | |
5f7f70c1 | 362 | |
91afd36f | 363 | return -ENOTSUPP; |
5f7f70c1 PF |
364 | } |
365 | ||
91afd36f KS |
366 | static bool fsl_qspi_supports_op(struct spi_slave *slave, |
367 | const struct spi_mem_op *op) | |
5f7f70c1 | 368 | { |
91afd36f KS |
369 | struct fsl_qspi *q = dev_get_priv(slave->dev->parent); |
370 | int ret; | |
371 | ||
372 | ret = fsl_qspi_check_buswidth(q, op->cmd.buswidth); | |
373 | ||
374 | if (op->addr.nbytes) | |
375 | ret |= fsl_qspi_check_buswidth(q, op->addr.buswidth); | |
376 | ||
377 | if (op->dummy.nbytes) | |
378 | ret |= fsl_qspi_check_buswidth(q, op->dummy.buswidth); | |
5bc48308 | 379 | |
91afd36f KS |
380 | if (op->data.nbytes) |
381 | ret |= fsl_qspi_check_buswidth(q, op->data.buswidth); | |
5f7f70c1 | 382 | |
91afd36f KS |
383 | if (ret) |
384 | return false; | |
5f7f70c1 PF |
385 | |
386 | /* | |
91afd36f KS |
387 | * The number of instructions needed for the op, needs |
388 | * to fit into a single LUT entry. | |
5f7f70c1 | 389 | */ |
91afd36f KS |
390 | if (op->addr.nbytes + |
391 | (op->dummy.nbytes ? 1 : 0) + | |
392 | (op->data.nbytes ? 1 : 0) > 6) | |
393 | return false; | |
394 | ||
395 | /* Max 64 dummy clock cycles supported */ | |
396 | if (op->dummy.nbytes && | |
397 | (op->dummy.nbytes * 8 / op->dummy.buswidth > 64)) | |
398 | return false; | |
399 | ||
400 | /* Max data length, check controller limits and alignment */ | |
401 | if (op->data.dir == SPI_MEM_DATA_IN && | |
402 | (op->data.nbytes > q->devtype_data->ahb_buf_size || | |
403 | (op->data.nbytes > q->devtype_data->rxfifo - 4 && | |
404 | !IS_ALIGNED(op->data.nbytes, 8)))) | |
405 | return false; | |
406 | ||
407 | if (op->data.dir == SPI_MEM_DATA_OUT && | |
408 | op->data.nbytes > q->devtype_data->txfifo) | |
409 | return false; | |
410 | ||
411 | return true; | |
5f7f70c1 | 412 | } |
5f7f70c1 | 413 | |
91afd36f KS |
414 | static void fsl_qspi_prepare_lut(struct fsl_qspi *q, |
415 | const struct spi_mem_op *op) | |
a2358783 | 416 | { |
91afd36f KS |
417 | void __iomem *base = q->iobase; |
418 | u32 lutval[4] = {}; | |
419 | int lutidx = 1, i; | |
a2358783 | 420 | |
91afd36f KS |
421 | lutval[0] |= LUT_DEF(0, LUT_CMD, LUT_PAD(op->cmd.buswidth), |
422 | op->cmd.opcode); | |
a2358783 | 423 | |
def88bce YL |
424 | if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) { |
425 | if (op->addr.nbytes) { | |
426 | lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_ADDR, | |
427 | LUT_PAD(op->addr.buswidth), | |
428 | (op->addr.nbytes == 4) ? 0x20 : 0x18); | |
429 | lutidx++; | |
430 | } | |
431 | } else { | |
432 | /* | |
433 | * For some unknown reason, using LUT_ADDR doesn't work in some | |
434 | * cases (at least with only one byte long addresses), so | |
435 | * let's use LUT_MODE to write the address bytes one by one | |
436 | */ | |
437 | for (i = 0; i < op->addr.nbytes; i++) { | |
438 | u8 addrbyte = op->addr.val >> (8 * (op->addr.nbytes - i - 1)); | |
439 | ||
440 | lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_MODE, | |
441 | LUT_PAD(op->addr.buswidth), | |
442 | addrbyte); | |
443 | lutidx++; | |
444 | } | |
a2358783 PF |
445 | } |
446 | ||
91afd36f KS |
447 | if (op->dummy.nbytes) { |
448 | lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_DUMMY, | |
449 | LUT_PAD(op->dummy.buswidth), | |
450 | op->dummy.nbytes * 8 / | |
451 | op->dummy.buswidth); | |
452 | lutidx++; | |
6b57ff6f AW |
453 | } |
454 | ||
91afd36f KS |
455 | if (op->data.nbytes) { |
456 | lutval[lutidx / 2] |= LUT_DEF(lutidx, | |
457 | op->data.dir == SPI_MEM_DATA_IN ? | |
458 | LUT_FSL_READ : LUT_FSL_WRITE, | |
459 | LUT_PAD(op->data.buswidth), | |
460 | 0); | |
461 | lutidx++; | |
6b57ff6f AW |
462 | } |
463 | ||
91afd36f | 464 | lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_STOP, 0, 0); |
6b57ff6f | 465 | |
91afd36f KS |
466 | /* unlock LUT */ |
467 | qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY); | |
468 | qspi_writel(q, QUADSPI_LCKER_UNLOCK, q->iobase + QUADSPI_LCKCR); | |
469 | ||
470 | dev_dbg(q->dev, "CMD[%x] lutval[0:%x \t 1:%x \t 2:%x \t 3:%x]\n", | |
471 | op->cmd.opcode, lutval[0], lutval[1], lutval[2], lutval[3]); | |
6b57ff6f | 472 | |
91afd36f KS |
473 | /* fill LUT */ |
474 | for (i = 0; i < ARRAY_SIZE(lutval); i++) | |
475 | qspi_writel(q, lutval[i], base + QUADSPI_LUT_REG(i)); | |
a2358783 | 476 | |
def88bce YL |
477 | if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) { |
478 | if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN && | |
479 | op->addr.nbytes) { | |
480 | for (i = 0; i < ARRAY_SIZE(lutval); i++) | |
481 | qspi_writel(q, lutval[i], base + QUADSPI_AHB_LUT_REG(i)); | |
482 | } | |
483 | } | |
484 | ||
91afd36f KS |
485 | /* lock LUT */ |
486 | qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY); | |
487 | qspi_writel(q, QUADSPI_LCKER_LOCK, q->iobase + QUADSPI_LCKCR); | |
488 | } | |
a2358783 | 489 | |
91afd36f KS |
490 | /* |
491 | * If we have changed the content of the flash by writing or erasing, or if we | |
492 | * read from flash with a different offset into the page buffer, we need to | |
493 | * invalidate the AHB buffer. If we do not do so, we may read out the wrong | |
494 | * data. The spec tells us reset the AHB domain and Serial Flash domain at | |
495 | * the same time. | |
496 | */ | |
497 | static void fsl_qspi_invalidate(struct fsl_qspi *q) | |
498 | { | |
499 | u32 reg; | |
6b57ff6f | 500 | |
91afd36f KS |
501 | reg = qspi_readl(q, q->iobase + QUADSPI_MCR); |
502 | reg |= QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK; | |
503 | qspi_writel(q, reg, q->iobase + QUADSPI_MCR); | |
6b57ff6f | 504 | |
10509987 | 505 | /* |
91afd36f KS |
506 | * The minimum delay : 1 AHB + 2 SFCK clocks. |
507 | * Delay 1 us is enough. | |
10509987 | 508 | */ |
91afd36f | 509 | udelay(1); |
6b57ff6f | 510 | |
91afd36f KS |
511 | reg &= ~(QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK); |
512 | qspi_writel(q, reg, q->iobase + QUADSPI_MCR); | |
6b57ff6f AW |
513 | } |
514 | ||
91afd36f | 515 | static void fsl_qspi_select_mem(struct fsl_qspi *q, struct spi_slave *slave) |
6b57ff6f | 516 | { |
91afd36f KS |
517 | struct dm_spi_slave_platdata *plat = |
518 | dev_get_parent_platdata(slave->dev); | |
519 | ||
520 | if (q->selected == plat->cs) | |
521 | return; | |
6b57ff6f | 522 | |
91afd36f KS |
523 | q->selected = plat->cs; |
524 | fsl_qspi_invalidate(q); | |
6b57ff6f AW |
525 | } |
526 | ||
def88bce YL |
527 | static u32 fsl_qspi_memsize_per_cs(struct fsl_qspi *q) |
528 | { | |
529 | if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) { | |
530 | if (needs_single_bus(q)) | |
531 | return q->memmap_size / 2; | |
532 | else | |
533 | return q->memmap_size / 4; | |
534 | } else { | |
535 | return ALIGN(q->devtype_data->ahb_buf_size, 0x400); | |
536 | } | |
537 | } | |
538 | ||
91afd36f | 539 | static void fsl_qspi_read_ahb(struct fsl_qspi *q, const struct spi_mem_op *op) |
6b57ff6f | 540 | { |
def88bce YL |
541 | void __iomem *ahb_read_addr = q->ahb_addr; |
542 | ||
543 | if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) { | |
544 | if (op->addr.nbytes) | |
545 | ahb_read_addr += op->addr.val; | |
546 | } | |
547 | ||
91afd36f | 548 | memcpy_fromio(op->data.buf.in, |
def88bce | 549 | ahb_read_addr + q->selected * fsl_qspi_memsize_per_cs(q), |
91afd36f | 550 | op->data.nbytes); |
6b57ff6f AW |
551 | } |
552 | ||
91afd36f KS |
553 | static void fsl_qspi_fill_txfifo(struct fsl_qspi *q, |
554 | const struct spi_mem_op *op) | |
6b57ff6f | 555 | { |
91afd36f KS |
556 | void __iomem *base = q->iobase; |
557 | int i; | |
558 | u32 val; | |
6b57ff6f | 559 | |
91afd36f KS |
560 | for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 4); i += 4) { |
561 | memcpy(&val, op->data.buf.out + i, 4); | |
562 | val = fsl_qspi_endian_xchg(q, val); | |
563 | qspi_writel(q, val, base + QUADSPI_TBDR); | |
6b57ff6f AW |
564 | } |
565 | ||
91afd36f KS |
566 | if (i < op->data.nbytes) { |
567 | memcpy(&val, op->data.buf.out + i, op->data.nbytes - i); | |
568 | val = fsl_qspi_endian_xchg(q, val); | |
569 | qspi_writel(q, val, base + QUADSPI_TBDR); | |
570 | } | |
5bc48308 | 571 | |
91afd36f KS |
572 | if (needs_fill_txfifo(q)) { |
573 | for (i = op->data.nbytes; i < 16; i += 4) | |
574 | qspi_writel(q, 0, base + QUADSPI_TBDR); | |
575 | } | |
5bc48308 HW |
576 | } |
577 | ||
91afd36f KS |
578 | static void fsl_qspi_read_rxfifo(struct fsl_qspi *q, |
579 | const struct spi_mem_op *op) | |
5bc48308 | 580 | { |
91afd36f KS |
581 | void __iomem *base = q->iobase; |
582 | int i; | |
583 | u8 *buf = op->data.buf.in; | |
584 | u32 val; | |
5bc48308 | 585 | |
91afd36f KS |
586 | for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 4); i += 4) { |
587 | val = qspi_readl(q, base + QUADSPI_RBDR(i / 4)); | |
588 | val = fsl_qspi_endian_xchg(q, val); | |
589 | memcpy(buf + i, &val, 4); | |
590 | } | |
591 | ||
592 | if (i < op->data.nbytes) { | |
593 | val = qspi_readl(q, base + QUADSPI_RBDR(i / 4)); | |
594 | val = fsl_qspi_endian_xchg(q, val); | |
595 | memcpy(buf + i, &val, op->data.nbytes - i); | |
596 | } | |
5bc48308 HW |
597 | } |
598 | ||
91afd36f KS |
599 | static int fsl_qspi_readl_poll_tout(struct fsl_qspi *q, void __iomem *base, |
600 | u32 mask, u32 delay_us, u32 timeout_us) | |
5bc48308 | 601 | { |
91afd36f | 602 | u32 reg; |
5bc48308 | 603 | |
91afd36f KS |
604 | if (!q->devtype_data->little_endian) |
605 | mask = (u32)cpu_to_be32(mask); | |
606 | ||
607 | return readl_poll_timeout(base, reg, !(reg & mask), timeout_us); | |
5bc48308 | 608 | } |
5bc48308 | 609 | |
91afd36f | 610 | static int fsl_qspi_do_op(struct fsl_qspi *q, const struct spi_mem_op *op) |
5bc48308 | 611 | { |
91afd36f KS |
612 | void __iomem *base = q->iobase; |
613 | int err = 0; | |
5bc48308 | 614 | |
91afd36f KS |
615 | /* |
616 | * Always start the sequence at the same index since we update | |
617 | * the LUT at each exec_op() call. And also specify the DATA | |
618 | * length, since it's has not been specified in the LUT. | |
619 | */ | |
620 | qspi_writel(q, op->data.nbytes | QUADSPI_IPCR_SEQID(SEQID_LUT), | |
621 | base + QUADSPI_IPCR); | |
5bc48308 | 622 | |
91afd36f KS |
623 | /* wait for the controller being ready */ |
624 | err = fsl_qspi_readl_poll_tout(q, base + QUADSPI_SR, | |
625 | (QUADSPI_SR_IP_ACC_MASK | | |
626 | QUADSPI_SR_AHB_ACC_MASK), | |
627 | 10, 1000); | |
628 | ||
629 | if (!err && op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN) | |
630 | fsl_qspi_read_rxfifo(q, op); | |
631 | ||
632 | return err; | |
5bc48308 HW |
633 | } |
634 | ||
91afd36f KS |
635 | static int fsl_qspi_exec_op(struct spi_slave *slave, |
636 | const struct spi_mem_op *op) | |
5bc48308 | 637 | { |
91afd36f KS |
638 | struct fsl_qspi *q = dev_get_priv(slave->dev->parent); |
639 | void __iomem *base = q->iobase; | |
640 | u32 addr_offset = 0; | |
641 | int err = 0; | |
5bc48308 | 642 | |
91afd36f KS |
643 | /* wait for the controller being ready */ |
644 | fsl_qspi_readl_poll_tout(q, base + QUADSPI_SR, (QUADSPI_SR_IP_ACC_MASK | | |
645 | QUADSPI_SR_AHB_ACC_MASK), 10, 1000); | |
5bc48308 | 646 | |
91afd36f | 647 | fsl_qspi_select_mem(q, slave); |
5bc48308 | 648 | |
91afd36f KS |
649 | if (needs_amba_base_offset(q)) |
650 | addr_offset = q->memmap_phy; | |
651 | ||
def88bce YL |
652 | if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) { |
653 | if (op->addr.nbytes) | |
654 | addr_offset += op->addr.val; | |
655 | } | |
656 | ||
91afd36f | 657 | qspi_writel(q, |
def88bce | 658 | q->selected * fsl_qspi_memsize_per_cs(q) + addr_offset, |
91afd36f KS |
659 | base + QUADSPI_SFAR); |
660 | ||
661 | qspi_writel(q, qspi_readl(q, base + QUADSPI_MCR) | | |
662 | QUADSPI_MCR_CLR_RXF_MASK | QUADSPI_MCR_CLR_TXF_MASK, | |
663 | base + QUADSPI_MCR); | |
664 | ||
665 | qspi_writel(q, QUADSPI_SPTRCLR_BFPTRC | QUADSPI_SPTRCLR_IPPTRC, | |
666 | base + QUADSPI_SPTRCLR); | |
667 | ||
668 | fsl_qspi_prepare_lut(q, op); | |
5bc48308 | 669 | |
bf9bffa9 | 670 | /* |
91afd36f KS |
671 | * If we have large chunks of data, we read them through the AHB bus |
672 | * by accessing the mapped memory. In all other cases we use | |
673 | * IP commands to access the flash. | |
bf9bffa9 | 674 | */ |
91afd36f KS |
675 | if (op->data.nbytes > (q->devtype_data->rxfifo - 4) && |
676 | op->data.dir == SPI_MEM_DATA_IN) { | |
677 | fsl_qspi_read_ahb(q, op); | |
678 | } else { | |
679 | qspi_writel(q, QUADSPI_RBCT_WMRK_MASK | | |
680 | QUADSPI_RBCT_RXBRD_USEIPS, base + QUADSPI_RBCT); | |
681 | ||
682 | if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT) | |
683 | fsl_qspi_fill_txfifo(q, op); | |
684 | ||
685 | err = fsl_qspi_do_op(q, op); | |
ce7575a8 YL |
686 | } |
687 | ||
91afd36f KS |
688 | /* Invalidate the data in the AHB buffer. */ |
689 | fsl_qspi_invalidate(q); | |
ce7575a8 | 690 | |
91afd36f KS |
691 | return err; |
692 | } | |
1c631da4 | 693 | |
91afd36f KS |
694 | static int fsl_qspi_adjust_op_size(struct spi_slave *slave, |
695 | struct spi_mem_op *op) | |
696 | { | |
697 | struct fsl_qspi *q = dev_get_priv(slave->dev->parent); | |
698 | ||
699 | if (op->data.dir == SPI_MEM_DATA_OUT) { | |
700 | if (op->data.nbytes > q->devtype_data->txfifo) | |
701 | op->data.nbytes = q->devtype_data->txfifo; | |
702 | } else { | |
703 | if (op->data.nbytes > q->devtype_data->ahb_buf_size) | |
704 | op->data.nbytes = q->devtype_data->ahb_buf_size; | |
705 | else if (op->data.nbytes > (q->devtype_data->rxfifo - 4)) | |
706 | op->data.nbytes = ALIGN_DOWN(op->data.nbytes, 8); | |
1c631da4 SG |
707 | } |
708 | ||
91afd36f KS |
709 | return 0; |
710 | } | |
711 | ||
712 | static int fsl_qspi_default_setup(struct fsl_qspi *q) | |
713 | { | |
714 | void __iomem *base = q->iobase; | |
def88bce | 715 | u32 reg, addr_offset = 0, memsize_cs; |
91afd36f KS |
716 | |
717 | /* Reset the module */ | |
718 | qspi_writel(q, QUADSPI_MCR_SWRSTSD_MASK | QUADSPI_MCR_SWRSTHD_MASK, | |
719 | base + QUADSPI_MCR); | |
720 | udelay(1); | |
5bc48308 | 721 | |
91afd36f KS |
722 | /* Disable the module */ |
723 | qspi_writel(q, QUADSPI_MCR_MDIS_MASK | QUADSPI_MCR_RESERVED_MASK, | |
724 | base + QUADSPI_MCR); | |
5bc48308 | 725 | |
4e147418 | 726 | /* |
91afd36f KS |
727 | * Previous boot stages (BootROM, bootloader) might have used DDR |
728 | * mode and did not clear the TDH bits. As we currently use SDR mode | |
729 | * only, clear the TDH bits if necessary. | |
4e147418 | 730 | */ |
91afd36f KS |
731 | if (needs_tdh_setting(q)) |
732 | qspi_writel(q, qspi_readl(q, base + QUADSPI_FLSHCR) & | |
733 | ~QUADSPI_FLSHCR_TDH_MASK, | |
734 | base + QUADSPI_FLSHCR); | |
735 | ||
736 | reg = qspi_readl(q, base + QUADSPI_SMPR); | |
737 | qspi_writel(q, reg & ~(QUADSPI_SMPR_FSDLY_MASK | |
738 | | QUADSPI_SMPR_FSPHS_MASK | |
739 | | QUADSPI_SMPR_HSENA_MASK | |
740 | | QUADSPI_SMPR_DDRSMP_MASK), base + QUADSPI_SMPR); | |
741 | ||
742 | /* We only use the buffer3 for AHB read */ | |
743 | qspi_writel(q, 0, base + QUADSPI_BUF0IND); | |
744 | qspi_writel(q, 0, base + QUADSPI_BUF1IND); | |
745 | qspi_writel(q, 0, base + QUADSPI_BUF2IND); | |
746 | ||
def88bce YL |
747 | if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) |
748 | qspi_writel(q, QUADSPI_BFGENCR_SEQID(SEQID_LUT_AHB), | |
749 | q->iobase + QUADSPI_BFGENCR); | |
750 | else | |
751 | qspi_writel(q, QUADSPI_BFGENCR_SEQID(SEQID_LUT), | |
752 | q->iobase + QUADSPI_BFGENCR); | |
753 | ||
91afd36f KS |
754 | qspi_writel(q, QUADSPI_RBCT_WMRK_MASK, base + QUADSPI_RBCT); |
755 | qspi_writel(q, QUADSPI_BUF3CR_ALLMST_MASK | | |
756 | QUADSPI_BUF3CR_ADATSZ(q->devtype_data->ahb_buf_size / 8), | |
757 | base + QUADSPI_BUF3CR); | |
758 | ||
759 | if (needs_amba_base_offset(q)) | |
760 | addr_offset = q->memmap_phy; | |
4e147418 | 761 | |
5bc48308 | 762 | /* |
91afd36f KS |
763 | * In HW there can be a maximum of four chips on two buses with |
764 | * two chip selects on each bus. We use four chip selects in SW | |
765 | * to differentiate between the four chips. | |
766 | * We use ahb_buf_size for each chip and set SFA1AD, SFA2AD, SFB1AD, | |
767 | * SFB2AD accordingly. | |
5bc48308 | 768 | */ |
def88bce YL |
769 | memsize_cs = fsl_qspi_memsize_per_cs(q); |
770 | qspi_writel(q, memsize_cs + addr_offset, | |
91afd36f | 771 | base + QUADSPI_SFA1AD); |
def88bce | 772 | qspi_writel(q, memsize_cs * 2 + addr_offset, |
91afd36f | 773 | base + QUADSPI_SFA2AD); |
def88bce YL |
774 | if (!needs_single_bus(q)) { |
775 | qspi_writel(q, memsize_cs * 3 + addr_offset, | |
776 | base + QUADSPI_SFB1AD); | |
777 | qspi_writel(q, memsize_cs * 4 + addr_offset, | |
778 | base + QUADSPI_SFB2AD); | |
779 | } | |
91afd36f KS |
780 | |
781 | q->selected = -1; | |
782 | ||
783 | /* Enable the module */ | |
784 | qspi_writel(q, QUADSPI_MCR_RESERVED_MASK | QUADSPI_MCR_END_CFG_MASK, | |
785 | base + QUADSPI_MCR); | |
5bc48308 HW |
786 | return 0; |
787 | } | |
788 | ||
91afd36f KS |
789 | static const struct spi_controller_mem_ops fsl_qspi_mem_ops = { |
790 | .adjust_op_size = fsl_qspi_adjust_op_size, | |
791 | .supports_op = fsl_qspi_supports_op, | |
792 | .exec_op = fsl_qspi_exec_op, | |
793 | }; | |
794 | ||
795 | static int fsl_qspi_probe(struct udevice *bus) | |
5bc48308 | 796 | { |
91afd36f KS |
797 | struct dm_spi_bus *dm_bus = bus->uclass_priv; |
798 | struct fsl_qspi *q = dev_get_priv(bus); | |
5bc48308 | 799 | const void *blob = gd->fdt_blob; |
e160f7d4 | 800 | int node = dev_of_offset(bus); |
91afd36f KS |
801 | struct fdt_resource res; |
802 | int ret; | |
5bc48308 | 803 | |
91afd36f KS |
804 | q->dev = bus; |
805 | q->devtype_data = (struct fsl_qspi_devtype_data *) | |
806 | dev_get_driver_data(bus); | |
5bc48308 | 807 | |
91afd36f KS |
808 | /* find the resources */ |
809 | ret = fdt_get_named_resource(blob, node, "reg", "reg-names", "QuadSPI", | |
810 | &res); | |
bf9bffa9 | 811 | if (ret) { |
91afd36f | 812 | dev_err(bus, "Can't get regs base addresses(ret = %d)!\n", ret); |
bf9bffa9 YY |
813 | return -ENOMEM; |
814 | } | |
91afd36f KS |
815 | |
816 | q->iobase = map_physmem(res.start, res.end - res.start, MAP_NOCACHE); | |
817 | ||
bf9bffa9 | 818 | ret = fdt_get_named_resource(blob, node, "reg", "reg-names", |
91afd36f | 819 | "QuadSPI-memory", &res); |
5bc48308 | 820 | if (ret) { |
91afd36f | 821 | dev_err(bus, "Can't get AMBA base addresses(ret = %d)!\n", ret); |
5bc48308 HW |
822 | return -ENOMEM; |
823 | } | |
824 | ||
91afd36f KS |
825 | q->ahb_addr = map_physmem(res.start, res.end - res.start, MAP_NOCACHE); |
826 | q->memmap_phy = res.start; | |
def88bce | 827 | q->memmap_size = res.end - res.start; |
5bc48308 | 828 | |
91afd36f KS |
829 | dm_bus->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", |
830 | 66000000); | |
5bc48308 | 831 | |
91afd36f | 832 | fsl_qspi_default_setup(q); |
5bc48308 HW |
833 | |
834 | return 0; | |
835 | } | |
836 | ||
837 | static int fsl_qspi_xfer(struct udevice *dev, unsigned int bitlen, | |
91afd36f | 838 | const void *dout, void *din, unsigned long flags) |
5bc48308 | 839 | { |
91afd36f | 840 | return 0; |
5bc48308 HW |
841 | } |
842 | ||
843 | static int fsl_qspi_claim_bus(struct udevice *dev) | |
844 | { | |
5bc48308 HW |
845 | return 0; |
846 | } | |
847 | ||
848 | static int fsl_qspi_release_bus(struct udevice *dev) | |
849 | { | |
5bc48308 HW |
850 | return 0; |
851 | } | |
852 | ||
853 | static int fsl_qspi_set_speed(struct udevice *bus, uint speed) | |
854 | { | |
5bc48308 HW |
855 | return 0; |
856 | } | |
857 | ||
858 | static int fsl_qspi_set_mode(struct udevice *bus, uint mode) | |
859 | { | |
5bc48308 HW |
860 | return 0; |
861 | } | |
862 | ||
863 | static const struct dm_spi_ops fsl_qspi_ops = { | |
864 | .claim_bus = fsl_qspi_claim_bus, | |
865 | .release_bus = fsl_qspi_release_bus, | |
866 | .xfer = fsl_qspi_xfer, | |
867 | .set_speed = fsl_qspi_set_speed, | |
868 | .set_mode = fsl_qspi_set_mode, | |
91afd36f | 869 | .mem_ops = &fsl_qspi_mem_ops, |
5bc48308 HW |
870 | }; |
871 | ||
872 | static const struct udevice_id fsl_qspi_ids[] = { | |
91afd36f KS |
873 | { .compatible = "fsl,vf610-qspi", .data = (ulong)&vybrid_data, }, |
874 | { .compatible = "fsl,imx6sx-qspi", .data = (ulong)&imx6sx_data, }, | |
875 | { .compatible = "fsl,imx6ul-qspi", .data = (ulong)&imx6ul_data, }, | |
876 | { .compatible = "fsl,imx7d-qspi", .data = (ulong)&imx7d_data, }, | |
93d6c8f7 | 877 | { .compatible = "fsl,imx7ulp-qspi", .data = (ulong)&imx7ulp_data, }, |
91afd36f KS |
878 | { .compatible = "fsl,ls1021a-qspi", .data = (ulong)&ls1021a_data, }, |
879 | { .compatible = "fsl,ls1088a-qspi", .data = (ulong)&ls1088a_data, }, | |
880 | { .compatible = "fsl,ls2080a-qspi", .data = (ulong)&ls2080a_data, }, | |
5bc48308 HW |
881 | { } |
882 | }; | |
883 | ||
884 | U_BOOT_DRIVER(fsl_qspi) = { | |
885 | .name = "fsl_qspi", | |
886 | .id = UCLASS_SPI, | |
887 | .of_match = fsl_qspi_ids, | |
888 | .ops = &fsl_qspi_ops, | |
91afd36f | 889 | .priv_auto_alloc_size = sizeof(struct fsl_qspi), |
5bc48308 | 890 | .probe = fsl_qspi_probe, |
5bc48308 | 891 | }; |