2 * Copyright 2013-2015 Freescale Semiconductor, Inc.
4 * Freescale Quad Serial Peripheral Interface (QSPI) driver
6 * SPDX-License-Identifier: GPL-2.0+
13 #include <linux/sizes.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 #define RX_BUFFER_SIZE 0x80
23 #define TX_BUFFER_SIZE 0x200
25 #define TX_BUFFER_SIZE 0x40
28 #define OFFSET_BITS_MASK GENMASK(23, 0)
30 #define FLASH_STATUS_WEL 0x02
34 #define SEQID_FAST_READ 2
37 #define SEQID_CHIP_ERASE 5
41 #ifdef CONFIG_SPI_FLASH_BAR
44 #define SEQID_RDEAR 11
45 #define SEQID_WREAR 12
49 #define QSPI_CMD_PP 0x02 /* Page program (up to 256 bytes) */
50 #define QSPI_CMD_RDSR 0x05 /* Read status register */
51 #define QSPI_CMD_WREN 0x06 /* Write enable */
52 #define QSPI_CMD_FAST_READ 0x0b /* Read data bytes (high frequency) */
53 #define QSPI_CMD_BE_4K 0x20 /* 4K erase */
54 #define QSPI_CMD_CHIP_ERASE 0xc7 /* Erase whole flash chip */
55 #define QSPI_CMD_SE 0xd8 /* Sector erase (usually 64KiB) */
56 #define QSPI_CMD_RDID 0x9f /* Read JEDEC ID */
58 /* Used for Micron, winbond and Macronix flashes */
59 #define QSPI_CMD_WREAR 0xc5 /* EAR register write */
60 #define QSPI_CMD_RDEAR 0xc8 /* EAR reigster read */
62 /* Used for Spansion flashes only. */
63 #define QSPI_CMD_BRRD 0x16 /* Bank register read */
64 #define QSPI_CMD_BRWR 0x17 /* Bank register write */
66 /* 4-byte address QSPI CMD - used on Spansion and some Macronix flashes */
67 #define QSPI_CMD_FAST_READ_4B 0x0c /* Read data bytes (high frequency) */
68 #define QSPI_CMD_PP_4B 0x12 /* Page program (up to 256 bytes) */
69 #define QSPI_CMD_SE_4B 0xdc /* Sector erase (usually 64KiB) */
71 /* fsl_qspi_platdata flags */
72 #define QSPI_FLAG_REGMAP_ENDIAN_BIG BIT(0)
74 /* default SCK frequency, unit: HZ */
75 #define FSL_QSPI_DEFAULT_SCK_FREQ 50000000
77 /* QSPI max chipselect signals number */
78 #define FSL_QSPI_MAX_CHIPSELECT_NUM 4
82 * struct fsl_qspi_platdata - platform data for Freescale QSPI
84 * @flags: Flags for QSPI QSPI_FLAG_...
85 * @speed_hz: Default SCK frequency
86 * @reg_base: Base address of QSPI registers
87 * @amba_base: Base address of QSPI memory mapping
88 * @amba_total_size: size of QSPI memory mapping
89 * @flash_num: Number of active slave devices
90 * @num_chipselect: Number of QSPI chipselect signals
92 struct fsl_qspi_platdata {
104 * struct fsl_qspi_priv - private data for Freescale QSPI
106 * @flags: Flags for QSPI QSPI_FLAG_...
107 * @bus_clk: QSPI input clk frequency
108 * @speed_hz: Default SCK frequency
109 * @cur_seqid: current LUT table sequence id
110 * @sf_addr: flash access offset
111 * @amba_base: Base address of QSPI memory mapping of every CS
112 * @amba_total_size: size of QSPI memory mapping
113 * @cur_amba_base: Base address of QSPI memory mapping of current CS
114 * @flash_num: Number of active slave devices
115 * @num_chipselect: Number of QSPI chipselect signals
116 * @regs: Point to QSPI register structure for I/O access
118 struct fsl_qspi_priv {
124 u32 amba_base[FSL_QSPI_MAX_CHIPSELECT_NUM];
129 struct fsl_qspi_regs *regs;
132 #ifndef CONFIG_DM_SPI
134 struct spi_slave slave;
135 struct fsl_qspi_priv priv;
139 static u32 qspi_read32(u32 flags, u32 *addr)
141 return flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ?
142 in_be32(addr) : in_le32(addr);
145 static void qspi_write32(u32 flags, u32 *addr, u32 val)
147 flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ?
148 out_be32(addr, val) : out_le32(addr, val);
151 /* QSPI support swapping the flash read/write data
152 * in hardware for LS102xA, but not for VF610 */
153 static inline u32 qspi_endian_xchg(u32 data)
162 static void qspi_set_lut(struct fsl_qspi_priv *priv)
164 struct fsl_qspi_regs *regs = priv->regs;
168 qspi_write32(priv->flags, ®s->lutkey, LUT_KEY_VALUE);
169 qspi_write32(priv->flags, ®s->lckcr, QSPI_LCKCR_UNLOCK);
172 lut_base = SEQID_WREN * 4;
173 qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_WREN) |
174 PAD0(LUT_PAD1) | INSTR0(LUT_CMD));
175 qspi_write32(priv->flags, ®s->lut[lut_base + 1], 0);
176 qspi_write32(priv->flags, ®s->lut[lut_base + 2], 0);
177 qspi_write32(priv->flags, ®s->lut[lut_base + 3], 0);
180 lut_base = SEQID_FAST_READ * 4;
181 #ifdef CONFIG_SPI_FLASH_BAR
182 qspi_write32(priv->flags, ®s->lut[lut_base],
183 OPRND0(QSPI_CMD_FAST_READ) | PAD0(LUT_PAD1) |
184 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
185 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
187 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
188 qspi_write32(priv->flags, ®s->lut[lut_base],
189 OPRND0(QSPI_CMD_FAST_READ) | PAD0(LUT_PAD1) |
190 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
191 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
193 qspi_write32(priv->flags, ®s->lut[lut_base],
194 OPRND0(QSPI_CMD_FAST_READ_4B) |
195 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) |
196 OPRND1(ADDR32BIT) | PAD1(LUT_PAD1) |
199 qspi_write32(priv->flags, ®s->lut[lut_base + 1],
200 OPRND0(8) | PAD0(LUT_PAD1) | INSTR0(LUT_DUMMY) |
201 OPRND1(RX_BUFFER_SIZE) | PAD1(LUT_PAD1) |
203 qspi_write32(priv->flags, ®s->lut[lut_base + 2], 0);
204 qspi_write32(priv->flags, ®s->lut[lut_base + 3], 0);
207 lut_base = SEQID_RDSR * 4;
208 qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_RDSR) |
209 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
210 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
211 qspi_write32(priv->flags, ®s->lut[lut_base + 1], 0);
212 qspi_write32(priv->flags, ®s->lut[lut_base + 2], 0);
213 qspi_write32(priv->flags, ®s->lut[lut_base + 3], 0);
216 lut_base = SEQID_SE * 4;
217 #ifdef CONFIG_SPI_FLASH_BAR
218 qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_SE) |
219 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
220 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
222 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
223 qspi_write32(priv->flags, ®s->lut[lut_base],
224 OPRND0(QSPI_CMD_SE) | PAD0(LUT_PAD1) |
225 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
226 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
228 qspi_write32(priv->flags, ®s->lut[lut_base],
229 OPRND0(QSPI_CMD_SE_4B) | PAD0(LUT_PAD1) |
230 INSTR0(LUT_CMD) | OPRND1(ADDR32BIT) |
231 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
233 qspi_write32(priv->flags, ®s->lut[lut_base + 1], 0);
234 qspi_write32(priv->flags, ®s->lut[lut_base + 2], 0);
235 qspi_write32(priv->flags, ®s->lut[lut_base + 3], 0);
237 /* Erase the whole chip */
238 lut_base = SEQID_CHIP_ERASE * 4;
239 qspi_write32(priv->flags, ®s->lut[lut_base],
240 OPRND0(QSPI_CMD_CHIP_ERASE) |
241 PAD0(LUT_PAD1) | INSTR0(LUT_CMD));
242 qspi_write32(priv->flags, ®s->lut[lut_base + 1], 0);
243 qspi_write32(priv->flags, ®s->lut[lut_base + 2], 0);
244 qspi_write32(priv->flags, ®s->lut[lut_base + 3], 0);
247 lut_base = SEQID_PP * 4;
248 #ifdef CONFIG_SPI_FLASH_BAR
249 qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_PP) |
250 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
251 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
253 if (FSL_QSPI_FLASH_SIZE <= SZ_16M)
254 qspi_write32(priv->flags, ®s->lut[lut_base],
255 OPRND0(QSPI_CMD_PP) | PAD0(LUT_PAD1) |
256 INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
257 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
259 qspi_write32(priv->flags, ®s->lut[lut_base],
260 OPRND0(QSPI_CMD_PP_4B) | PAD0(LUT_PAD1) |
261 INSTR0(LUT_CMD) | OPRND1(ADDR32BIT) |
262 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
266 * To MX6SX, OPRND0(TX_BUFFER_SIZE) can not work correctly.
267 * So, Use IDATSZ in IPCR to determine the size and here set 0.
269 qspi_write32(priv->flags, ®s->lut[lut_base + 1], OPRND0(0) |
270 PAD0(LUT_PAD1) | INSTR0(LUT_WRITE));
272 qspi_write32(priv->flags, ®s->lut[lut_base + 1],
273 OPRND0(TX_BUFFER_SIZE) |
274 PAD0(LUT_PAD1) | INSTR0(LUT_WRITE));
276 qspi_write32(priv->flags, ®s->lut[lut_base + 2], 0);
277 qspi_write32(priv->flags, ®s->lut[lut_base + 3], 0);
280 lut_base = SEQID_RDID * 4;
281 qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_RDID) |
282 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(8) |
283 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
284 qspi_write32(priv->flags, ®s->lut[lut_base + 1], 0);
285 qspi_write32(priv->flags, ®s->lut[lut_base + 2], 0);
286 qspi_write32(priv->flags, ®s->lut[lut_base + 3], 0);
288 /* SUB SECTOR 4K ERASE */
289 lut_base = SEQID_BE_4K * 4;
290 qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_BE_4K) |
291 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) |
292 PAD1(LUT_PAD1) | INSTR1(LUT_ADDR));
294 #ifdef CONFIG_SPI_FLASH_BAR
296 * BRRD BRWR RDEAR WREAR are all supported, because it is hard to
297 * dynamically check whether to set BRRD BRWR or RDEAR WREAR during
300 lut_base = SEQID_BRRD * 4;
301 qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_BRRD) |
302 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
303 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
305 lut_base = SEQID_BRWR * 4;
306 qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_BRWR) |
307 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
308 PAD1(LUT_PAD1) | INSTR1(LUT_WRITE));
310 lut_base = SEQID_RDEAR * 4;
311 qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_RDEAR) |
312 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
313 PAD1(LUT_PAD1) | INSTR1(LUT_READ));
315 lut_base = SEQID_WREAR * 4;
316 qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_WREAR) |
317 PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) |
318 PAD1(LUT_PAD1) | INSTR1(LUT_WRITE));
321 qspi_write32(priv->flags, ®s->lutkey, LUT_KEY_VALUE);
322 qspi_write32(priv->flags, ®s->lckcr, QSPI_LCKCR_LOCK);
325 #if defined(CONFIG_SYS_FSL_QSPI_AHB)
327 * If we have changed the content of the flash by writing or erasing,
328 * we need to invalidate the AHB buffer. If we do not do so, we may read out
329 * the wrong data. The spec tells us reset the AHB domain and Serial Flash
330 * domain at the same time.
332 static inline void qspi_ahb_invalid(struct fsl_qspi_priv *priv)
334 struct fsl_qspi_regs *regs = priv->regs;
337 reg = qspi_read32(priv->flags, ®s->mcr);
338 reg |= QSPI_MCR_SWRSTHD_MASK | QSPI_MCR_SWRSTSD_MASK;
339 qspi_write32(priv->flags, ®s->mcr, reg);
342 * The minimum delay : 1 AHB + 2 SFCK clocks.
343 * Delay 1 us is enough.
347 reg &= ~(QSPI_MCR_SWRSTHD_MASK | QSPI_MCR_SWRSTSD_MASK);
348 qspi_write32(priv->flags, ®s->mcr, reg);
351 /* Read out the data from the AHB buffer. */
352 static inline void qspi_ahb_read(struct fsl_qspi_priv *priv, u8 *rxbuf, int len)
354 struct fsl_qspi_regs *regs = priv->regs;
357 mcr_reg = qspi_read32(priv->flags, ®s->mcr);
359 qspi_write32(priv->flags, ®s->mcr,
360 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
361 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
363 /* Read out the data directly from the AHB buffer. */
364 memcpy(rxbuf, (u8 *)(priv->cur_amba_base + priv->sf_addr), len);
366 qspi_write32(priv->flags, ®s->mcr, mcr_reg);
369 static void qspi_enable_ddr_mode(struct fsl_qspi_priv *priv)
372 struct fsl_qspi_regs *regs = priv->regs;
374 reg = qspi_read32(priv->flags, ®s->mcr);
375 /* Disable the module */
376 qspi_write32(priv->flags, ®s->mcr, reg | QSPI_MCR_MDIS_MASK);
378 /* Set the Sampling Register for DDR */
379 reg2 = qspi_read32(priv->flags, ®s->smpr);
380 reg2 &= ~QSPI_SMPR_DDRSMP_MASK;
381 reg2 |= (2 << QSPI_SMPR_DDRSMP_SHIFT);
382 qspi_write32(priv->flags, ®s->smpr, reg2);
384 /* Enable the module again (enable the DDR too) */
385 reg |= QSPI_MCR_DDR_EN_MASK;
386 /* Enable bit 29 for imx6sx */
389 qspi_write32(priv->flags, ®s->mcr, reg);
393 * There are two different ways to read out the data from the flash:
394 * the "IP Command Read" and the "AHB Command Read".
396 * The IC guy suggests we use the "AHB Command Read" which is faster
397 * then the "IP Command Read". (What's more is that there is a bug in
398 * the "IP Command Read" in the Vybrid.)
400 * After we set up the registers for the "AHB Command Read", we can use
401 * the memcpy to read the data directly. A "missed" access to the buffer
402 * causes the controller to clear the buffer, and use the sequence pointed
403 * by the QUADSPI_BFGENCR[SEQID] to initiate a read from the flash.
405 static void qspi_init_ahb_read(struct fsl_qspi_priv *priv)
407 struct fsl_qspi_regs *regs = priv->regs;
409 /* AHB configuration for access buffer 0/1/2 .*/
410 qspi_write32(priv->flags, ®s->buf0cr, QSPI_BUFXCR_INVALID_MSTRID);
411 qspi_write32(priv->flags, ®s->buf1cr, QSPI_BUFXCR_INVALID_MSTRID);
412 qspi_write32(priv->flags, ®s->buf2cr, QSPI_BUFXCR_INVALID_MSTRID);
413 qspi_write32(priv->flags, ®s->buf3cr, QSPI_BUF3CR_ALLMST_MASK |
414 (0x80 << QSPI_BUF3CR_ADATSZ_SHIFT));
416 /* We only use the buffer3 */
417 qspi_write32(priv->flags, ®s->buf0ind, 0);
418 qspi_write32(priv->flags, ®s->buf1ind, 0);
419 qspi_write32(priv->flags, ®s->buf2ind, 0);
422 * Set the default lut sequence for AHB Read.
423 * Parallel mode is disabled.
425 qspi_write32(priv->flags, ®s->bfgencr,
426 SEQID_FAST_READ << QSPI_BFGENCR_SEQID_SHIFT);
429 qspi_enable_ddr_mode(priv);
433 #ifdef CONFIG_SPI_FLASH_BAR
434 /* Bank register read/write, EAR register read/write */
435 static void qspi_op_rdbank(struct fsl_qspi_priv *priv, u8 *rxbuf, u32 len)
437 struct fsl_qspi_regs *regs = priv->regs;
438 u32 reg, mcr_reg, data, seqid;
440 mcr_reg = qspi_read32(priv->flags, ®s->mcr);
441 qspi_write32(priv->flags, ®s->mcr,
442 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
443 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
444 qspi_write32(priv->flags, ®s->rbct, QSPI_RBCT_RXBRD_USEIPS);
446 qspi_write32(priv->flags, ®s->sfar, priv->cur_amba_base);
448 if (priv->cur_seqid == QSPI_CMD_BRRD)
453 qspi_write32(priv->flags, ®s->ipcr,
454 (seqid << QSPI_IPCR_SEQID_SHIFT) | len);
456 /* Wait previous command complete */
457 while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK)
461 reg = qspi_read32(priv->flags, ®s->rbsr);
462 if (reg & QSPI_RBSR_RDBFL_MASK) {
463 data = qspi_read32(priv->flags, ®s->rbdr[0]);
464 data = qspi_endian_xchg(data);
465 memcpy(rxbuf, &data, len);
466 qspi_write32(priv->flags, ®s->mcr,
467 qspi_read32(priv->flags, ®s->mcr) |
468 QSPI_MCR_CLR_RXF_MASK);
473 qspi_write32(priv->flags, ®s->mcr, mcr_reg);
477 static void qspi_op_rdid(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len)
479 struct fsl_qspi_regs *regs = priv->regs;
480 u32 mcr_reg, rbsr_reg, data;
483 mcr_reg = qspi_read32(priv->flags, ®s->mcr);
484 qspi_write32(priv->flags, ®s->mcr,
485 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
486 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
487 qspi_write32(priv->flags, ®s->rbct, QSPI_RBCT_RXBRD_USEIPS);
489 qspi_write32(priv->flags, ®s->sfar, priv->cur_amba_base);
491 qspi_write32(priv->flags, ®s->ipcr,
492 (SEQID_RDID << QSPI_IPCR_SEQID_SHIFT) | 0);
493 while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK)
498 while ((RX_BUFFER_SIZE >= size) && (size > 0)) {
499 rbsr_reg = qspi_read32(priv->flags, ®s->rbsr);
500 if (rbsr_reg & QSPI_RBSR_RDBFL_MASK) {
501 data = qspi_read32(priv->flags, ®s->rbdr[i]);
502 data = qspi_endian_xchg(data);
503 memcpy(rxbuf, &data, 4);
510 qspi_write32(priv->flags, ®s->mcr, mcr_reg);
513 #ifndef CONFIG_SYS_FSL_QSPI_AHB
514 /* If not use AHB read, read data from ip interface */
515 static void qspi_op_read(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len)
517 struct fsl_qspi_regs *regs = priv->regs;
522 mcr_reg = qspi_read32(priv->flags, ®s->mcr);
523 qspi_write32(priv->flags, ®s->mcr,
524 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
525 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
526 qspi_write32(priv->flags, ®s->rbct, QSPI_RBCT_RXBRD_USEIPS);
528 to_or_from = priv->sf_addr + priv->cur_amba_base;
533 qspi_write32(priv->flags, ®s->sfar, to_or_from);
535 size = (len > RX_BUFFER_SIZE) ?
536 RX_BUFFER_SIZE : len;
538 qspi_write32(priv->flags, ®s->ipcr,
539 (SEQID_FAST_READ << QSPI_IPCR_SEQID_SHIFT) |
541 while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK)
548 while ((RX_BUFFER_SIZE >= size) && (size > 0)) {
549 data = qspi_read32(priv->flags, ®s->rbdr[i]);
550 data = qspi_endian_xchg(data);
551 memcpy(rxbuf, &data, 4);
556 qspi_write32(priv->flags, ®s->mcr,
557 qspi_read32(priv->flags, ®s->mcr) |
558 QSPI_MCR_CLR_RXF_MASK);
561 qspi_write32(priv->flags, ®s->mcr, mcr_reg);
565 static void qspi_op_write(struct fsl_qspi_priv *priv, u8 *txbuf, u32 len)
567 struct fsl_qspi_regs *regs = priv->regs;
568 u32 mcr_reg, data, reg, status_reg, seqid;
569 int i, size, tx_size;
572 mcr_reg = qspi_read32(priv->flags, ®s->mcr);
573 qspi_write32(priv->flags, ®s->mcr,
574 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
575 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
576 qspi_write32(priv->flags, ®s->rbct, QSPI_RBCT_RXBRD_USEIPS);
579 while ((status_reg & FLASH_STATUS_WEL) != FLASH_STATUS_WEL) {
582 qspi_write32(priv->flags, ®s->ipcr,
583 (SEQID_WREN << QSPI_IPCR_SEQID_SHIFT) | 0);
584 while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK)
587 qspi_write32(priv->flags, ®s->ipcr,
588 (SEQID_RDSR << QSPI_IPCR_SEQID_SHIFT) | 1);
589 while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK)
592 reg = qspi_read32(priv->flags, ®s->rbsr);
593 if (reg & QSPI_RBSR_RDBFL_MASK) {
594 status_reg = qspi_read32(priv->flags, ®s->rbdr[0]);
595 status_reg = qspi_endian_xchg(status_reg);
597 qspi_write32(priv->flags, ®s->mcr,
598 qspi_read32(priv->flags, ®s->mcr) |
599 QSPI_MCR_CLR_RXF_MASK);
602 /* Default is page programming */
604 #ifdef CONFIG_SPI_FLASH_BAR
605 if (priv->cur_seqid == QSPI_CMD_BRWR)
607 else if (priv->cur_seqid == QSPI_CMD_WREAR)
611 to_or_from = priv->sf_addr + priv->cur_amba_base;
613 qspi_write32(priv->flags, ®s->sfar, to_or_from);
615 tx_size = (len > TX_BUFFER_SIZE) ?
616 TX_BUFFER_SIZE : len;
619 for (i = 0; i < size; i++) {
620 memcpy(&data, txbuf, 4);
621 data = qspi_endian_xchg(data);
622 qspi_write32(priv->flags, ®s->tbdr, data);
629 memcpy(&data, txbuf, size);
630 data = qspi_endian_xchg(data);
631 qspi_write32(priv->flags, ®s->tbdr, data);
634 qspi_write32(priv->flags, ®s->ipcr,
635 (seqid << QSPI_IPCR_SEQID_SHIFT) | tx_size);
636 while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK)
639 qspi_write32(priv->flags, ®s->mcr, mcr_reg);
642 static void qspi_op_rdsr(struct fsl_qspi_priv *priv, u32 *rxbuf)
644 struct fsl_qspi_regs *regs = priv->regs;
645 u32 mcr_reg, reg, data;
647 mcr_reg = qspi_read32(priv->flags, ®s->mcr);
648 qspi_write32(priv->flags, ®s->mcr,
649 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
650 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
651 qspi_write32(priv->flags, ®s->rbct, QSPI_RBCT_RXBRD_USEIPS);
653 qspi_write32(priv->flags, ®s->sfar, priv->cur_amba_base);
655 qspi_write32(priv->flags, ®s->ipcr,
656 (SEQID_RDSR << QSPI_IPCR_SEQID_SHIFT) | 0);
657 while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK)
661 reg = qspi_read32(priv->flags, ®s->rbsr);
662 if (reg & QSPI_RBSR_RDBFL_MASK) {
663 data = qspi_read32(priv->flags, ®s->rbdr[0]);
664 data = qspi_endian_xchg(data);
665 memcpy(rxbuf, &data, 4);
666 qspi_write32(priv->flags, ®s->mcr,
667 qspi_read32(priv->flags, ®s->mcr) |
668 QSPI_MCR_CLR_RXF_MASK);
673 qspi_write32(priv->flags, ®s->mcr, mcr_reg);
676 static void qspi_op_erase(struct fsl_qspi_priv *priv)
678 struct fsl_qspi_regs *regs = priv->regs;
682 mcr_reg = qspi_read32(priv->flags, ®s->mcr);
683 qspi_write32(priv->flags, ®s->mcr,
684 QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK |
685 QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE);
686 qspi_write32(priv->flags, ®s->rbct, QSPI_RBCT_RXBRD_USEIPS);
688 to_or_from = priv->sf_addr + priv->cur_amba_base;
689 qspi_write32(priv->flags, ®s->sfar, to_or_from);
691 qspi_write32(priv->flags, ®s->ipcr,
692 (SEQID_WREN << QSPI_IPCR_SEQID_SHIFT) | 0);
693 while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK)
696 if (priv->cur_seqid == QSPI_CMD_SE) {
697 qspi_write32(priv->flags, ®s->ipcr,
698 (SEQID_SE << QSPI_IPCR_SEQID_SHIFT) | 0);
699 } else if (priv->cur_seqid == QSPI_CMD_BE_4K) {
700 qspi_write32(priv->flags, ®s->ipcr,
701 (SEQID_BE_4K << QSPI_IPCR_SEQID_SHIFT) | 0);
703 while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK)
706 qspi_write32(priv->flags, ®s->mcr, mcr_reg);
709 int qspi_xfer(struct fsl_qspi_priv *priv, unsigned int bitlen,
710 const void *dout, void *din, unsigned long flags)
712 u32 bytes = DIV_ROUND_UP(bitlen, 8);
713 static u32 wr_sfaddr;
717 if (flags & SPI_XFER_BEGIN) {
718 priv->cur_seqid = *(u8 *)dout;
719 memcpy(&txbuf, dout, 4);
722 if (flags == SPI_XFER_END) {
723 priv->sf_addr = wr_sfaddr;
724 qspi_op_write(priv, (u8 *)dout, bytes);
728 if (priv->cur_seqid == QSPI_CMD_FAST_READ) {
729 priv->sf_addr = swab32(txbuf) & OFFSET_BITS_MASK;
730 } else if ((priv->cur_seqid == QSPI_CMD_SE) ||
731 (priv->cur_seqid == QSPI_CMD_BE_4K)) {
732 priv->sf_addr = swab32(txbuf) & OFFSET_BITS_MASK;
734 } else if (priv->cur_seqid == QSPI_CMD_PP) {
735 wr_sfaddr = swab32(txbuf) & OFFSET_BITS_MASK;
736 } else if ((priv->cur_seqid == QSPI_CMD_BRWR) ||
737 (priv->cur_seqid == QSPI_CMD_WREAR)) {
738 #ifdef CONFIG_SPI_FLASH_BAR
745 if (priv->cur_seqid == QSPI_CMD_FAST_READ) {
746 #ifdef CONFIG_SYS_FSL_QSPI_AHB
747 qspi_ahb_read(priv, din, bytes);
749 qspi_op_read(priv, din, bytes);
751 } else if (priv->cur_seqid == QSPI_CMD_RDID)
752 qspi_op_rdid(priv, din, bytes);
753 else if (priv->cur_seqid == QSPI_CMD_RDSR)
754 qspi_op_rdsr(priv, din);
755 #ifdef CONFIG_SPI_FLASH_BAR
756 else if ((priv->cur_seqid == QSPI_CMD_BRRD) ||
757 (priv->cur_seqid == QSPI_CMD_RDEAR)) {
759 qspi_op_rdbank(priv, din, bytes);
764 #ifdef CONFIG_SYS_FSL_QSPI_AHB
765 if ((priv->cur_seqid == QSPI_CMD_SE) ||
766 (priv->cur_seqid == QSPI_CMD_PP) ||
767 (priv->cur_seqid == QSPI_CMD_BE_4K) ||
768 (priv->cur_seqid == QSPI_CMD_WREAR) ||
769 (priv->cur_seqid == QSPI_CMD_BRWR))
770 qspi_ahb_invalid(priv);
776 void qspi_module_disable(struct fsl_qspi_priv *priv, u8 disable)
780 mcr_val = qspi_read32(priv->flags, &priv->regs->mcr);
782 mcr_val |= QSPI_MCR_MDIS_MASK;
784 mcr_val &= ~QSPI_MCR_MDIS_MASK;
785 qspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
788 void qspi_cfg_smpr(struct fsl_qspi_priv *priv, u32 clear_bits, u32 set_bits)
792 smpr_val = qspi_read32(priv->flags, &priv->regs->smpr);
793 smpr_val &= ~clear_bits;
794 smpr_val |= set_bits;
795 qspi_write32(priv->flags, &priv->regs->smpr, smpr_val);
797 #ifndef CONFIG_DM_SPI
798 static unsigned long spi_bases[] = {
805 static unsigned long amba_bases[] = {
812 static inline struct fsl_qspi *to_qspi_spi(struct spi_slave *slave)
814 return container_of(slave, struct fsl_qspi, slave);
817 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
818 unsigned int max_hz, unsigned int mode)
820 struct fsl_qspi *qspi;
821 struct fsl_qspi_regs *regs;
824 if (bus >= ARRAY_SIZE(spi_bases))
827 if (cs >= FSL_QSPI_FLASH_NUM)
830 qspi = spi_alloc_slave(struct fsl_qspi, bus, cs);
834 #ifdef CONFIG_SYS_FSL_QSPI_BE
835 qspi->priv.flags |= QSPI_FLAG_REGMAP_ENDIAN_BIG;
838 regs = (struct fsl_qspi_regs *)spi_bases[bus];
839 qspi->priv.regs = regs;
841 * According cs, use different amba_base to choose the
842 * corresponding flash devices.
844 * If not, only one flash device is used even if passing
845 * different cs using `sf probe`
847 qspi->priv.cur_amba_base = amba_bases[bus] + cs * FSL_QSPI_FLASH_SIZE;
849 qspi->slave.max_write_size = TX_BUFFER_SIZE;
851 qspi_write32(qspi->priv.flags, ®s->mcr,
852 QSPI_MCR_RESERVED_MASK | QSPI_MCR_MDIS_MASK);
854 qspi_cfg_smpr(&qspi->priv,
855 ~(QSPI_SMPR_FSDLY_MASK | QSPI_SMPR_DDRSMP_MASK |
856 QSPI_SMPR_FSPHS_MASK | QSPI_SMPR_HSENA_MASK), 0);
858 total_size = FSL_QSPI_FLASH_SIZE * FSL_QSPI_FLASH_NUM;
860 * Any read access to non-implemented addresses will provide
863 * In case single die flash devices, TOP_ADDR_MEMA2 and
864 * TOP_ADDR_MEMB2 should be initialized/programmed to
865 * TOP_ADDR_MEMA1 and TOP_ADDR_MEMB1 respectively - in effect,
866 * setting the size of these devices to 0. This would ensure
867 * that the complete memory map is assigned to only one flash device.
869 qspi_write32(qspi->priv.flags, ®s->sfa1ad,
870 FSL_QSPI_FLASH_SIZE | amba_bases[bus]);
871 qspi_write32(qspi->priv.flags, ®s->sfa2ad,
872 FSL_QSPI_FLASH_SIZE | amba_bases[bus]);
873 qspi_write32(qspi->priv.flags, ®s->sfb1ad,
874 total_size | amba_bases[bus]);
875 qspi_write32(qspi->priv.flags, ®s->sfb2ad,
876 total_size | amba_bases[bus]);
878 qspi_set_lut(&qspi->priv);
880 #ifdef CONFIG_SYS_FSL_QSPI_AHB
881 qspi_init_ahb_read(&qspi->priv);
884 qspi_module_disable(&qspi->priv, 0);
889 void spi_free_slave(struct spi_slave *slave)
891 struct fsl_qspi *qspi = to_qspi_spi(slave);
896 int spi_claim_bus(struct spi_slave *slave)
901 void spi_release_bus(struct spi_slave *slave)
906 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
907 const void *dout, void *din, unsigned long flags)
909 struct fsl_qspi *qspi = to_qspi_spi(slave);
911 return qspi_xfer(&qspi->priv, bitlen, dout, din, flags);
919 static int fsl_qspi_child_pre_probe(struct udevice *dev)
921 struct spi_slave *slave = dev_get_parent_priv(dev);
923 slave->max_write_size = TX_BUFFER_SIZE;
928 static int fsl_qspi_probe(struct udevice *bus)
931 struct fsl_qspi_platdata *plat = dev_get_platdata(bus);
932 struct fsl_qspi_priv *priv = dev_get_priv(bus);
933 struct dm_spi_bus *dm_spi_bus;
935 dm_spi_bus = bus->uclass_priv;
937 dm_spi_bus->max_hz = plat->speed_hz;
939 priv->regs = (struct fsl_qspi_regs *)(uintptr_t)plat->reg_base;
940 priv->flags = plat->flags;
942 priv->speed_hz = plat->speed_hz;
943 priv->amba_base[0] = plat->amba_base;
944 priv->amba_total_size = plat->amba_total_size;
945 priv->flash_num = plat->flash_num;
946 priv->num_chipselect = plat->num_chipselect;
948 qspi_write32(priv->flags, &priv->regs->mcr,
949 QSPI_MCR_RESERVED_MASK | QSPI_MCR_MDIS_MASK);
951 qspi_cfg_smpr(priv, ~(QSPI_SMPR_FSDLY_MASK | QSPI_SMPR_DDRSMP_MASK |
952 QSPI_SMPR_FSPHS_MASK | QSPI_SMPR_HSENA_MASK), 0);
954 total_size = FSL_QSPI_FLASH_SIZE * FSL_QSPI_FLASH_NUM;
956 * Any read access to non-implemented addresses will provide
959 * In case single die flash devices, TOP_ADDR_MEMA2 and
960 * TOP_ADDR_MEMB2 should be initialized/programmed to
961 * TOP_ADDR_MEMA1 and TOP_ADDR_MEMB1 respectively - in effect,
962 * setting the size of these devices to 0. This would ensure
963 * that the complete memory map is assigned to only one flash device.
965 qspi_write32(priv->flags, &priv->regs->sfa1ad,
966 FSL_QSPI_FLASH_SIZE | priv->amba_base[0]);
967 qspi_write32(priv->flags, &priv->regs->sfa2ad,
968 FSL_QSPI_FLASH_SIZE | priv->amba_base[0]);
969 qspi_write32(priv->flags, &priv->regs->sfb1ad,
970 total_size | priv->amba_base[0]);
971 qspi_write32(priv->flags, &priv->regs->sfb2ad,
972 total_size | priv->amba_base[0]);
976 #ifdef CONFIG_SYS_FSL_QSPI_AHB
977 qspi_init_ahb_read(priv);
980 qspi_module_disable(priv, 0);
985 static int fsl_qspi_ofdata_to_platdata(struct udevice *bus)
991 struct fsl_qspi_platdata *plat = bus->platdata;
992 const void *blob = gd->fdt_blob;
993 int node = bus->of_offset;
994 int ret, flash_num = 0, subnode;
996 if (fdtdec_get_bool(blob, node, "big-endian"))
997 plat->flags |= QSPI_FLAG_REGMAP_ENDIAN_BIG;
999 ret = fdtdec_get_int_array(blob, node, "reg", (u32 *)regs_data,
1000 sizeof(regs_data)/sizeof(u32));
1002 debug("Error: can't get base addresses (ret = %d)!\n", ret);
1006 /* Count flash numbers */
1007 fdt_for_each_subnode(blob, subnode, node)
1010 if (flash_num == 0) {
1011 debug("Error: Missing flashes!\n");
1015 plat->speed_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
1016 FSL_QSPI_DEFAULT_SCK_FREQ);
1017 plat->num_chipselect = fdtdec_get_int(blob, node, "num-cs",
1018 FSL_QSPI_MAX_CHIPSELECT_NUM);
1020 plat->reg_base = regs_data[0].addr;
1021 plat->amba_base = regs_data[1].addr;
1022 plat->amba_total_size = regs_data[1].size;
1023 plat->flash_num = flash_num;
1025 debug("%s: regs=<0x%x> <0x%x, 0x%x>, max-frequency=%d, endianess=%s\n",
1029 plat->amba_total_size,
1031 plat->flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ? "be" : "le"
1037 static int fsl_qspi_xfer(struct udevice *dev, unsigned int bitlen,
1038 const void *dout, void *din, unsigned long flags)
1040 struct fsl_qspi_priv *priv;
1041 struct udevice *bus;
1044 priv = dev_get_priv(bus);
1046 return qspi_xfer(priv, bitlen, dout, din, flags);
1049 static int fsl_qspi_claim_bus(struct udevice *dev)
1051 struct fsl_qspi_priv *priv;
1052 struct udevice *bus;
1053 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
1056 priv = dev_get_priv(bus);
1058 priv->cur_amba_base =
1059 priv->amba_base[0] + FSL_QSPI_FLASH_SIZE * slave_plat->cs;
1061 qspi_module_disable(priv, 0);
1066 static int fsl_qspi_release_bus(struct udevice *dev)
1068 struct fsl_qspi_priv *priv;
1069 struct udevice *bus;
1072 priv = dev_get_priv(bus);
1074 qspi_module_disable(priv, 1);
1079 static int fsl_qspi_set_speed(struct udevice *bus, uint speed)
1085 static int fsl_qspi_set_mode(struct udevice *bus, uint mode)
1091 static const struct dm_spi_ops fsl_qspi_ops = {
1092 .claim_bus = fsl_qspi_claim_bus,
1093 .release_bus = fsl_qspi_release_bus,
1094 .xfer = fsl_qspi_xfer,
1095 .set_speed = fsl_qspi_set_speed,
1096 .set_mode = fsl_qspi_set_mode,
1099 static const struct udevice_id fsl_qspi_ids[] = {
1100 { .compatible = "fsl,vf610-qspi" },
1101 { .compatible = "fsl,imx6sx-qspi" },
1105 U_BOOT_DRIVER(fsl_qspi) = {
1108 .of_match = fsl_qspi_ids,
1109 .ops = &fsl_qspi_ops,
1110 .ofdata_to_platdata = fsl_qspi_ofdata_to_platdata,
1111 .platdata_auto_alloc_size = sizeof(struct fsl_qspi_platdata),
1112 .priv_auto_alloc_size = sizeof(struct fsl_qspi_priv),
1113 .probe = fsl_qspi_probe,
1114 .child_pre_probe = fsl_qspi_child_pre_probe,