1 // SPDX-License-Identifier: GPL-2.0+
4 * Freescale QuadSPI driver.
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
11 * Transition to SPI MEM interface:
13 * Boris Brezillon <bbrezillon@kernel.org>
14 * Frieder Schrempf <frieder.schrempf@kontron.de>
15 * Yogesh Gaur <yogeshnarayan.gaur@nxp.com>
16 * Suresh Gupta <suresh.gupta@nxp.com>
18 * Based on the original fsl-quadspi.c SPI NOR driver:
19 * Author: Freescale Semiconductor, Inc.
23 #include <linux/bitops.h>
24 #include <linux/clk.h>
25 #include <linux/completion.h>
26 #include <linux/delay.h>
27 #include <linux/err.h>
28 #include <linux/errno.h>
29 #include <linux/interrupt.h>
31 #include <linux/iopoll.h>
32 #include <linux/jiffies.h>
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/mutex.h>
37 #include <linux/platform_device.h>
38 #include <linux/pm_qos.h>
39 #include <linux/sizes.h>
41 #include <linux/spi/spi.h>
42 #include <linux/spi/spi-mem.h>
45 * The driver only uses one single LUT entry, that is updated on
46 * each call of exec_op(). Index 0 is preset at boot with a basic
47 * read operation, so let's use the last entry (15).
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)
62 #define QUADSPI_IPCR 0x08
63 #define QUADSPI_IPCR_SEQID(x) ((x) << 24)
65 #define QUADSPI_FLSHCR 0x0c
66 #define QUADSPI_FLSHCR_TCSS_MASK GENMASK(3, 0)
67 #define QUADSPI_FLSHCR_TCSH_MASK GENMASK(11, 8)
68 #define QUADSPI_FLSHCR_TDH_MASK GENMASK(17, 16)
70 #define QUADSPI_BUF0CR 0x10
71 #define QUADSPI_BUF1CR 0x14
72 #define QUADSPI_BUF2CR 0x18
73 #define QUADSPI_BUFXCR_INVALID_MSTRID 0xe
75 #define QUADSPI_BUF3CR 0x1c
76 #define QUADSPI_BUF3CR_ALLMST_MASK BIT(31)
77 #define QUADSPI_BUF3CR_ADATSZ(x) ((x) << 8)
78 #define QUADSPI_BUF3CR_ADATSZ_MASK GENMASK(15, 8)
80 #define QUADSPI_BFGENCR 0x20
81 #define QUADSPI_BFGENCR_SEQID(x) ((x) << 12)
83 #define QUADSPI_BUF0IND 0x30
84 #define QUADSPI_BUF1IND 0x34
85 #define QUADSPI_BUF2IND 0x38
86 #define QUADSPI_SFAR 0x100
88 #define QUADSPI_SMPR 0x108
89 #define QUADSPI_SMPR_DDRSMP_MASK GENMASK(18, 16)
90 #define QUADSPI_SMPR_FSDLY_MASK BIT(6)
91 #define QUADSPI_SMPR_FSPHS_MASK BIT(5)
92 #define QUADSPI_SMPR_HSENA_MASK BIT(0)
94 #define QUADSPI_RBCT 0x110
95 #define QUADSPI_RBCT_WMRK_MASK GENMASK(4, 0)
96 #define QUADSPI_RBCT_RXBRD_USEIPS BIT(8)
98 #define QUADSPI_TBDR 0x154
100 #define QUADSPI_SR 0x15c
101 #define QUADSPI_SR_IP_ACC_MASK BIT(1)
102 #define QUADSPI_SR_AHB_ACC_MASK BIT(2)
104 #define QUADSPI_FR 0x160
105 #define QUADSPI_FR_TFF_MASK BIT(0)
107 #define QUADSPI_RSER 0x164
108 #define QUADSPI_RSER_TFIE BIT(0)
110 #define QUADSPI_SPTRCLR 0x16c
111 #define QUADSPI_SPTRCLR_IPPTRC BIT(8)
112 #define QUADSPI_SPTRCLR_BFPTRC BIT(0)
114 #define QUADSPI_SFA1AD 0x180
115 #define QUADSPI_SFA2AD 0x184
116 #define QUADSPI_SFB1AD 0x188
117 #define QUADSPI_SFB2AD 0x18c
118 #define QUADSPI_RBDR(x) (0x200 + ((x) * 4))
120 #define QUADSPI_LUTKEY 0x300
121 #define QUADSPI_LUTKEY_VALUE 0x5AF05AF0
123 #define QUADSPI_LCKCR 0x304
124 #define QUADSPI_LCKER_LOCK BIT(0)
125 #define QUADSPI_LCKER_UNLOCK BIT(1)
127 #define QUADSPI_LUT_BASE 0x310
128 #define QUADSPI_LUT_OFFSET (SEQID_LUT * 4 * 4)
129 #define QUADSPI_LUT_REG(idx) \
130 (QUADSPI_LUT_BASE + QUADSPI_LUT_OFFSET + (idx) * 4)
132 /* Instruction set for the LUT register */
140 #define LUT_FSL_READ 7
141 #define LUT_FSL_WRITE 8
142 #define LUT_JMP_ON_CS 9
143 #define LUT_ADDR_DDR 10
144 #define LUT_MODE_DDR 11
145 #define LUT_MODE2_DDR 12
146 #define LUT_MODE4_DDR 13
147 #define LUT_FSL_READ_DDR 14
148 #define LUT_FSL_WRITE_DDR 15
149 #define LUT_DATA_LEARN 16
152 * The PAD definitions for LUT register.
154 * The pad stands for the number of IO lines [0:3].
155 * For example, the quad read needs four IO lines,
156 * so you should use LUT_PAD(4).
158 #define LUT_PAD(x) (fls(x) - 1)
161 * Macro for constructing the LUT entries with the following
164 * ---------------------------------------------------
165 * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 |
166 * ---------------------------------------------------
168 #define LUT_DEF(idx, ins, pad, opr) \
169 ((((ins) << 10) | ((pad) << 8) | (opr)) << (((idx) % 2) * 16))
171 /* Controller needs driver to swap endianness */
172 #define QUADSPI_QUIRK_SWAP_ENDIAN BIT(0)
174 /* Controller needs 4x internal clock */
175 #define QUADSPI_QUIRK_4X_INT_CLK BIT(1)
178 * TKT253890, the controller needs the driver to fill the txfifo with
179 * 16 bytes at least to trigger a data transfer, even though the extra
180 * data won't be transferred.
182 #define QUADSPI_QUIRK_TKT253890 BIT(2)
184 /* TKT245618, the controller cannot wake up from wait mode */
185 #define QUADSPI_QUIRK_TKT245618 BIT(3)
188 * Controller adds QSPI_AMBA_BASE (base address of the mapped memory)
189 * internally. No need to add it when setting SFXXAD and SFAR registers
191 #define QUADSPI_QUIRK_BASE_INTERNAL BIT(4)
194 * Controller uses TDH bits in register QUADSPI_FLSHCR.
195 * They need to be set in accordance with the DDR/SDR mode.
197 #define QUADSPI_QUIRK_USE_TDH_SETTING BIT(5)
199 struct fsl_qspi_devtype_data
{
203 unsigned int ahb_buf_size
;
208 static const struct fsl_qspi_devtype_data vybrid_data
= {
211 .invalid_mstrid
= QUADSPI_BUFXCR_INVALID_MSTRID
,
212 .ahb_buf_size
= SZ_1K
,
213 .quirks
= QUADSPI_QUIRK_SWAP_ENDIAN
,
214 .little_endian
= true,
217 static const struct fsl_qspi_devtype_data imx6sx_data
= {
220 .invalid_mstrid
= QUADSPI_BUFXCR_INVALID_MSTRID
,
221 .ahb_buf_size
= SZ_1K
,
222 .quirks
= QUADSPI_QUIRK_4X_INT_CLK
| QUADSPI_QUIRK_TKT245618
,
223 .little_endian
= true,
226 static const struct fsl_qspi_devtype_data imx7d_data
= {
229 .invalid_mstrid
= QUADSPI_BUFXCR_INVALID_MSTRID
,
230 .ahb_buf_size
= SZ_1K
,
231 .quirks
= QUADSPI_QUIRK_TKT253890
| QUADSPI_QUIRK_4X_INT_CLK
|
232 QUADSPI_QUIRK_USE_TDH_SETTING
,
233 .little_endian
= true,
236 static const struct fsl_qspi_devtype_data imx6ul_data
= {
239 .invalid_mstrid
= QUADSPI_BUFXCR_INVALID_MSTRID
,
240 .ahb_buf_size
= SZ_1K
,
241 .quirks
= QUADSPI_QUIRK_TKT253890
| QUADSPI_QUIRK_4X_INT_CLK
|
242 QUADSPI_QUIRK_USE_TDH_SETTING
,
243 .little_endian
= true,
246 static const struct fsl_qspi_devtype_data ls1021a_data
= {
249 .invalid_mstrid
= QUADSPI_BUFXCR_INVALID_MSTRID
,
250 .ahb_buf_size
= SZ_1K
,
252 .little_endian
= false,
255 static const struct fsl_qspi_devtype_data ls2080a_data
= {
258 .ahb_buf_size
= SZ_1K
,
259 .invalid_mstrid
= 0x0,
260 .quirks
= QUADSPI_QUIRK_TKT253890
| QUADSPI_QUIRK_BASE_INTERNAL
,
261 .little_endian
= true,
265 void __iomem
*iobase
;
266 void __iomem
*ahb_addr
;
268 struct clk
*clk
, *clk_en
;
271 const struct fsl_qspi_devtype_data
*devtype_data
;
273 struct pm_qos_request pm_qos_req
;
277 static inline int needs_swap_endian(struct fsl_qspi
*q
)
279 return q
->devtype_data
->quirks
& QUADSPI_QUIRK_SWAP_ENDIAN
;
282 static inline int needs_4x_clock(struct fsl_qspi
*q
)
284 return q
->devtype_data
->quirks
& QUADSPI_QUIRK_4X_INT_CLK
;
287 static inline int needs_fill_txfifo(struct fsl_qspi
*q
)
289 return q
->devtype_data
->quirks
& QUADSPI_QUIRK_TKT253890
;
292 static inline int needs_wakeup_wait_mode(struct fsl_qspi
*q
)
294 return q
->devtype_data
->quirks
& QUADSPI_QUIRK_TKT245618
;
297 static inline int needs_amba_base_offset(struct fsl_qspi
*q
)
299 return !(q
->devtype_data
->quirks
& QUADSPI_QUIRK_BASE_INTERNAL
);
302 static inline int needs_tdh_setting(struct fsl_qspi
*q
)
304 return q
->devtype_data
->quirks
& QUADSPI_QUIRK_USE_TDH_SETTING
;
308 * An IC bug makes it necessary to rearrange the 32-bit data.
309 * Later chips, such as IMX6SLX, have fixed this bug.
311 static inline u32
fsl_qspi_endian_xchg(struct fsl_qspi
*q
, u32 a
)
313 return needs_swap_endian(q
) ? __swab32(a
) : a
;
317 * R/W functions for big- or little-endian registers:
318 * The QSPI controller's endianness is independent of
319 * the CPU core's endianness. So far, although the CPU
320 * core is little-endian the QSPI controller can use
321 * big-endian or little-endian.
323 static void qspi_writel(struct fsl_qspi
*q
, u32 val
, void __iomem
*addr
)
325 if (q
->devtype_data
->little_endian
)
326 iowrite32(val
, addr
);
328 iowrite32be(val
, addr
);
331 static u32
qspi_readl(struct fsl_qspi
*q
, void __iomem
*addr
)
333 if (q
->devtype_data
->little_endian
)
334 return ioread32(addr
);
336 return ioread32be(addr
);
339 static irqreturn_t
fsl_qspi_irq_handler(int irq
, void *dev_id
)
341 struct fsl_qspi
*q
= dev_id
;
344 /* clear interrupt */
345 reg
= qspi_readl(q
, q
->iobase
+ QUADSPI_FR
);
346 qspi_writel(q
, reg
, q
->iobase
+ QUADSPI_FR
);
348 if (reg
& QUADSPI_FR_TFF_MASK
)
351 dev_dbg(q
->dev
, "QUADSPI_FR : 0x%.8x:0x%.8x\n", 0, reg
);
355 static int fsl_qspi_check_buswidth(struct fsl_qspi
*q
, u8 width
)
367 static bool fsl_qspi_supports_op(struct spi_mem
*mem
,
368 const struct spi_mem_op
*op
)
370 struct fsl_qspi
*q
= spi_controller_get_devdata(mem
->spi
->controller
);
373 ret
= fsl_qspi_check_buswidth(q
, op
->cmd
.buswidth
);
376 ret
|= fsl_qspi_check_buswidth(q
, op
->addr
.buswidth
);
378 if (op
->dummy
.nbytes
)
379 ret
|= fsl_qspi_check_buswidth(q
, op
->dummy
.buswidth
);
382 ret
|= fsl_qspi_check_buswidth(q
, op
->data
.buswidth
);
388 * The number of instructions needed for the op, needs
389 * to fit into a single LUT entry.
391 if (op
->addr
.nbytes
+
392 (op
->dummy
.nbytes
? 1:0) +
393 (op
->data
.nbytes
? 1:0) > 6)
396 /* Max 64 dummy clock cycles supported */
397 if (op
->dummy
.nbytes
&&
398 (op
->dummy
.nbytes
* 8 / op
->dummy
.buswidth
> 64))
401 /* Max data length, check controller limits and alignment */
402 if (op
->data
.dir
== SPI_MEM_DATA_IN
&&
403 (op
->data
.nbytes
> q
->devtype_data
->ahb_buf_size
||
404 (op
->data
.nbytes
> q
->devtype_data
->rxfifo
- 4 &&
405 !IS_ALIGNED(op
->data
.nbytes
, 8))))
408 if (op
->data
.dir
== SPI_MEM_DATA_OUT
&&
409 op
->data
.nbytes
> q
->devtype_data
->txfifo
)
412 return spi_mem_default_supports_op(mem
, op
);
415 static void fsl_qspi_prepare_lut(struct fsl_qspi
*q
,
416 const struct spi_mem_op
*op
)
418 void __iomem
*base
= q
->iobase
;
422 lutval
[0] |= LUT_DEF(0, LUT_CMD
, LUT_PAD(op
->cmd
.buswidth
),
426 * For some unknown reason, using LUT_ADDR doesn't work in some
427 * cases (at least with only one byte long addresses), so
428 * let's use LUT_MODE to write the address bytes one by one
430 for (i
= 0; i
< op
->addr
.nbytes
; i
++) {
431 u8 addrbyte
= op
->addr
.val
>> (8 * (op
->addr
.nbytes
- i
- 1));
433 lutval
[lutidx
/ 2] |= LUT_DEF(lutidx
, LUT_MODE
,
434 LUT_PAD(op
->addr
.buswidth
),
439 if (op
->dummy
.nbytes
) {
440 lutval
[lutidx
/ 2] |= LUT_DEF(lutidx
, LUT_DUMMY
,
441 LUT_PAD(op
->dummy
.buswidth
),
442 op
->dummy
.nbytes
* 8 /
447 if (op
->data
.nbytes
) {
448 lutval
[lutidx
/ 2] |= LUT_DEF(lutidx
,
449 op
->data
.dir
== SPI_MEM_DATA_IN
?
450 LUT_FSL_READ
: LUT_FSL_WRITE
,
451 LUT_PAD(op
->data
.buswidth
),
456 lutval
[lutidx
/ 2] |= LUT_DEF(lutidx
, LUT_STOP
, 0, 0);
459 qspi_writel(q
, QUADSPI_LUTKEY_VALUE
, q
->iobase
+ QUADSPI_LUTKEY
);
460 qspi_writel(q
, QUADSPI_LCKER_UNLOCK
, q
->iobase
+ QUADSPI_LCKCR
);
463 for (i
= 0; i
< ARRAY_SIZE(lutval
); i
++)
464 qspi_writel(q
, lutval
[i
], base
+ QUADSPI_LUT_REG(i
));
467 qspi_writel(q
, QUADSPI_LUTKEY_VALUE
, q
->iobase
+ QUADSPI_LUTKEY
);
468 qspi_writel(q
, QUADSPI_LCKER_LOCK
, q
->iobase
+ QUADSPI_LCKCR
);
471 static int fsl_qspi_clk_prep_enable(struct fsl_qspi
*q
)
475 ret
= clk_prepare_enable(q
->clk_en
);
479 ret
= clk_prepare_enable(q
->clk
);
481 clk_disable_unprepare(q
->clk_en
);
485 if (needs_wakeup_wait_mode(q
))
486 cpu_latency_qos_add_request(&q
->pm_qos_req
, 0);
491 static void fsl_qspi_clk_disable_unprep(struct fsl_qspi
*q
)
493 if (needs_wakeup_wait_mode(q
))
494 cpu_latency_qos_remove_request(&q
->pm_qos_req
);
496 clk_disable_unprepare(q
->clk
);
497 clk_disable_unprepare(q
->clk_en
);
501 * If we have changed the content of the flash by writing or erasing, or if we
502 * read from flash with a different offset into the page buffer, we need to
503 * invalidate the AHB buffer. If we do not do so, we may read out the wrong
504 * data. The spec tells us reset the AHB domain and Serial Flash domain at
507 static void fsl_qspi_invalidate(struct fsl_qspi
*q
)
511 reg
= qspi_readl(q
, q
->iobase
+ QUADSPI_MCR
);
512 reg
|= QUADSPI_MCR_SWRSTHD_MASK
| QUADSPI_MCR_SWRSTSD_MASK
;
513 qspi_writel(q
, reg
, q
->iobase
+ QUADSPI_MCR
);
516 * The minimum delay : 1 AHB + 2 SFCK clocks.
517 * Delay 1 us is enough.
521 reg
&= ~(QUADSPI_MCR_SWRSTHD_MASK
| QUADSPI_MCR_SWRSTSD_MASK
);
522 qspi_writel(q
, reg
, q
->iobase
+ QUADSPI_MCR
);
525 static void fsl_qspi_select_mem(struct fsl_qspi
*q
, struct spi_device
*spi
)
527 unsigned long rate
= spi
->max_speed_hz
;
530 if (q
->selected
== spi_get_chipselect(spi
, 0))
533 if (needs_4x_clock(q
))
536 fsl_qspi_clk_disable_unprep(q
);
538 ret
= clk_set_rate(q
->clk
, rate
);
542 ret
= fsl_qspi_clk_prep_enable(q
);
546 q
->selected
= spi_get_chipselect(spi
, 0);
548 fsl_qspi_invalidate(q
);
551 static void fsl_qspi_read_ahb(struct fsl_qspi
*q
, const struct spi_mem_op
*op
)
553 memcpy_fromio(op
->data
.buf
.in
,
554 q
->ahb_addr
+ q
->selected
* q
->devtype_data
->ahb_buf_size
,
558 static void fsl_qspi_fill_txfifo(struct fsl_qspi
*q
,
559 const struct spi_mem_op
*op
)
561 void __iomem
*base
= q
->iobase
;
565 for (i
= 0; i
< ALIGN_DOWN(op
->data
.nbytes
, 4); i
+= 4) {
566 memcpy(&val
, op
->data
.buf
.out
+ i
, 4);
567 val
= fsl_qspi_endian_xchg(q
, val
);
568 qspi_writel(q
, val
, base
+ QUADSPI_TBDR
);
571 if (i
< op
->data
.nbytes
) {
572 memcpy(&val
, op
->data
.buf
.out
+ i
, op
->data
.nbytes
- i
);
573 val
= fsl_qspi_endian_xchg(q
, val
);
574 qspi_writel(q
, val
, base
+ QUADSPI_TBDR
);
577 if (needs_fill_txfifo(q
)) {
578 for (i
= op
->data
.nbytes
; i
< 16; i
+= 4)
579 qspi_writel(q
, 0, base
+ QUADSPI_TBDR
);
583 static void fsl_qspi_read_rxfifo(struct fsl_qspi
*q
,
584 const struct spi_mem_op
*op
)
586 void __iomem
*base
= q
->iobase
;
588 u8
*buf
= op
->data
.buf
.in
;
591 for (i
= 0; i
< ALIGN_DOWN(op
->data
.nbytes
, 4); i
+= 4) {
592 val
= qspi_readl(q
, base
+ QUADSPI_RBDR(i
/ 4));
593 val
= fsl_qspi_endian_xchg(q
, val
);
594 memcpy(buf
+ i
, &val
, 4);
597 if (i
< op
->data
.nbytes
) {
598 val
= qspi_readl(q
, base
+ QUADSPI_RBDR(i
/ 4));
599 val
= fsl_qspi_endian_xchg(q
, val
);
600 memcpy(buf
+ i
, &val
, op
->data
.nbytes
- i
);
604 static int fsl_qspi_do_op(struct fsl_qspi
*q
, const struct spi_mem_op
*op
)
606 void __iomem
*base
= q
->iobase
;
609 init_completion(&q
->c
);
612 * Always start the sequence at the same index since we update
613 * the LUT at each exec_op() call. And also specify the DATA
614 * length, since it's has not been specified in the LUT.
616 qspi_writel(q
, op
->data
.nbytes
| QUADSPI_IPCR_SEQID(SEQID_LUT
),
617 base
+ QUADSPI_IPCR
);
619 /* Wait for the interrupt. */
620 if (!wait_for_completion_timeout(&q
->c
, msecs_to_jiffies(1000)))
623 if (!err
&& op
->data
.nbytes
&& op
->data
.dir
== SPI_MEM_DATA_IN
)
624 fsl_qspi_read_rxfifo(q
, op
);
629 static int fsl_qspi_readl_poll_tout(struct fsl_qspi
*q
, void __iomem
*base
,
630 u32 mask
, u32 delay_us
, u32 timeout_us
)
634 if (!q
->devtype_data
->little_endian
)
635 mask
= (u32
)cpu_to_be32(mask
);
637 return readl_poll_timeout(base
, reg
, !(reg
& mask
), delay_us
,
641 static int fsl_qspi_exec_op(struct spi_mem
*mem
, const struct spi_mem_op
*op
)
643 struct fsl_qspi
*q
= spi_controller_get_devdata(mem
->spi
->controller
);
644 void __iomem
*base
= q
->iobase
;
647 int invalid_mstrid
= q
->devtype_data
->invalid_mstrid
;
649 mutex_lock(&q
->lock
);
651 /* wait for the controller being ready */
652 fsl_qspi_readl_poll_tout(q
, base
+ QUADSPI_SR
, (QUADSPI_SR_IP_ACC_MASK
|
653 QUADSPI_SR_AHB_ACC_MASK
), 10, 1000);
655 fsl_qspi_select_mem(q
, mem
->spi
);
657 if (needs_amba_base_offset(q
))
658 addr_offset
= q
->memmap_phy
;
661 q
->selected
* q
->devtype_data
->ahb_buf_size
+ addr_offset
,
662 base
+ QUADSPI_SFAR
);
664 qspi_writel(q
, qspi_readl(q
, base
+ QUADSPI_MCR
) |
665 QUADSPI_MCR_CLR_RXF_MASK
| QUADSPI_MCR_CLR_TXF_MASK
,
668 qspi_writel(q
, QUADSPI_SPTRCLR_BFPTRC
| QUADSPI_SPTRCLR_IPPTRC
,
669 base
+ QUADSPI_SPTRCLR
);
671 qspi_writel(q
, invalid_mstrid
, base
+ QUADSPI_BUF0CR
);
672 qspi_writel(q
, invalid_mstrid
, base
+ QUADSPI_BUF1CR
);
673 qspi_writel(q
, invalid_mstrid
, base
+ QUADSPI_BUF2CR
);
675 fsl_qspi_prepare_lut(q
, op
);
678 * If we have large chunks of data, we read them through the AHB bus
679 * by accessing the mapped memory. In all other cases we use
680 * IP commands to access the flash.
682 if (op
->data
.nbytes
> (q
->devtype_data
->rxfifo
- 4) &&
683 op
->data
.dir
== SPI_MEM_DATA_IN
) {
684 fsl_qspi_read_ahb(q
, op
);
686 qspi_writel(q
, QUADSPI_RBCT_WMRK_MASK
|
687 QUADSPI_RBCT_RXBRD_USEIPS
, base
+ QUADSPI_RBCT
);
689 if (op
->data
.nbytes
&& op
->data
.dir
== SPI_MEM_DATA_OUT
)
690 fsl_qspi_fill_txfifo(q
, op
);
692 err
= fsl_qspi_do_op(q
, op
);
695 /* Invalidate the data in the AHB buffer. */
696 fsl_qspi_invalidate(q
);
698 mutex_unlock(&q
->lock
);
703 static int fsl_qspi_adjust_op_size(struct spi_mem
*mem
, struct spi_mem_op
*op
)
705 struct fsl_qspi
*q
= spi_controller_get_devdata(mem
->spi
->controller
);
707 if (op
->data
.dir
== SPI_MEM_DATA_OUT
) {
708 if (op
->data
.nbytes
> q
->devtype_data
->txfifo
)
709 op
->data
.nbytes
= q
->devtype_data
->txfifo
;
711 if (op
->data
.nbytes
> q
->devtype_data
->ahb_buf_size
)
712 op
->data
.nbytes
= q
->devtype_data
->ahb_buf_size
;
713 else if (op
->data
.nbytes
> (q
->devtype_data
->rxfifo
- 4))
714 op
->data
.nbytes
= ALIGN_DOWN(op
->data
.nbytes
, 8);
720 static int fsl_qspi_default_setup(struct fsl_qspi
*q
)
722 void __iomem
*base
= q
->iobase
;
723 u32 reg
, addr_offset
= 0;
726 /* disable and unprepare clock to avoid glitch pass to controller */
727 fsl_qspi_clk_disable_unprep(q
);
729 /* the default frequency, we will change it later if necessary. */
730 ret
= clk_set_rate(q
->clk
, 66000000);
734 ret
= fsl_qspi_clk_prep_enable(q
);
738 /* Reset the module */
739 qspi_writel(q
, QUADSPI_MCR_SWRSTSD_MASK
| QUADSPI_MCR_SWRSTHD_MASK
,
743 /* Disable the module */
744 qspi_writel(q
, QUADSPI_MCR_MDIS_MASK
| QUADSPI_MCR_RESERVED_MASK
,
748 * Previous boot stages (BootROM, bootloader) might have used DDR
749 * mode and did not clear the TDH bits. As we currently use SDR mode
750 * only, clear the TDH bits if necessary.
752 if (needs_tdh_setting(q
))
753 qspi_writel(q
, qspi_readl(q
, base
+ QUADSPI_FLSHCR
) &
754 ~QUADSPI_FLSHCR_TDH_MASK
,
755 base
+ QUADSPI_FLSHCR
);
757 reg
= qspi_readl(q
, base
+ QUADSPI_SMPR
);
758 qspi_writel(q
, reg
& ~(QUADSPI_SMPR_FSDLY_MASK
759 | QUADSPI_SMPR_FSPHS_MASK
760 | QUADSPI_SMPR_HSENA_MASK
761 | QUADSPI_SMPR_DDRSMP_MASK
), base
+ QUADSPI_SMPR
);
763 /* We only use the buffer3 for AHB read */
764 qspi_writel(q
, 0, base
+ QUADSPI_BUF0IND
);
765 qspi_writel(q
, 0, base
+ QUADSPI_BUF1IND
);
766 qspi_writel(q
, 0, base
+ QUADSPI_BUF2IND
);
768 qspi_writel(q
, QUADSPI_BFGENCR_SEQID(SEQID_LUT
),
769 q
->iobase
+ QUADSPI_BFGENCR
);
770 qspi_writel(q
, QUADSPI_RBCT_WMRK_MASK
, base
+ QUADSPI_RBCT
);
771 qspi_writel(q
, QUADSPI_BUF3CR_ALLMST_MASK
|
772 QUADSPI_BUF3CR_ADATSZ(q
->devtype_data
->ahb_buf_size
/ 8),
773 base
+ QUADSPI_BUF3CR
);
775 if (needs_amba_base_offset(q
))
776 addr_offset
= q
->memmap_phy
;
779 * In HW there can be a maximum of four chips on two buses with
780 * two chip selects on each bus. We use four chip selects in SW
781 * to differentiate between the four chips.
782 * We use ahb_buf_size for each chip and set SFA1AD, SFA2AD, SFB1AD,
783 * SFB2AD accordingly.
785 qspi_writel(q
, q
->devtype_data
->ahb_buf_size
+ addr_offset
,
786 base
+ QUADSPI_SFA1AD
);
787 qspi_writel(q
, q
->devtype_data
->ahb_buf_size
* 2 + addr_offset
,
788 base
+ QUADSPI_SFA2AD
);
789 qspi_writel(q
, q
->devtype_data
->ahb_buf_size
* 3 + addr_offset
,
790 base
+ QUADSPI_SFB1AD
);
791 qspi_writel(q
, q
->devtype_data
->ahb_buf_size
* 4 + addr_offset
,
792 base
+ QUADSPI_SFB2AD
);
796 /* Enable the module */
797 qspi_writel(q
, QUADSPI_MCR_RESERVED_MASK
| QUADSPI_MCR_END_CFG_MASK
,
800 /* clear all interrupt status */
801 qspi_writel(q
, 0xffffffff, q
->iobase
+ QUADSPI_FR
);
803 /* enable the interrupt */
804 qspi_writel(q
, QUADSPI_RSER_TFIE
, q
->iobase
+ QUADSPI_RSER
);
809 static const char *fsl_qspi_get_name(struct spi_mem
*mem
)
811 struct fsl_qspi
*q
= spi_controller_get_devdata(mem
->spi
->controller
);
812 struct device
*dev
= &mem
->spi
->dev
;
816 * In order to keep mtdparts compatible with the old MTD driver at
817 * mtd/spi-nor/fsl-quadspi.c, we set a custom name derived from the
818 * platform_device of the controller.
820 if (of_get_available_child_count(q
->dev
->of_node
) == 1)
821 return dev_name(q
->dev
);
823 name
= devm_kasprintf(dev
, GFP_KERNEL
,
824 "%s-%d", dev_name(q
->dev
),
825 spi_get_chipselect(mem
->spi
, 0));
828 dev_err(dev
, "failed to get memory for custom flash name\n");
829 return ERR_PTR(-ENOMEM
);
835 static const struct spi_controller_mem_ops fsl_qspi_mem_ops
= {
836 .adjust_op_size
= fsl_qspi_adjust_op_size
,
837 .supports_op
= fsl_qspi_supports_op
,
838 .exec_op
= fsl_qspi_exec_op
,
839 .get_name
= fsl_qspi_get_name
,
842 static int fsl_qspi_probe(struct platform_device
*pdev
)
844 struct spi_controller
*ctlr
;
845 struct device
*dev
= &pdev
->dev
;
846 struct device_node
*np
= dev
->of_node
;
847 struct resource
*res
;
851 ctlr
= spi_alloc_host(&pdev
->dev
, sizeof(*q
));
855 ctlr
->mode_bits
= SPI_RX_DUAL
| SPI_RX_QUAD
|
856 SPI_TX_DUAL
| SPI_TX_QUAD
;
858 q
= spi_controller_get_devdata(ctlr
);
860 q
->devtype_data
= of_device_get_match_data(dev
);
861 if (!q
->devtype_data
) {
866 platform_set_drvdata(pdev
, q
);
868 /* find the resources */
869 q
->iobase
= devm_platform_ioremap_resource_byname(pdev
, "QuadSPI");
870 if (IS_ERR(q
->iobase
)) {
871 ret
= PTR_ERR(q
->iobase
);
875 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
881 q
->memmap_phy
= res
->start
;
882 /* Since there are 4 cs, map size required is 4 times ahb_buf_size */
883 q
->ahb_addr
= devm_ioremap(dev
, q
->memmap_phy
,
884 (q
->devtype_data
->ahb_buf_size
* 4));
890 /* find the clocks */
891 q
->clk_en
= devm_clk_get(dev
, "qspi_en");
892 if (IS_ERR(q
->clk_en
)) {
893 ret
= PTR_ERR(q
->clk_en
);
897 q
->clk
= devm_clk_get(dev
, "qspi");
898 if (IS_ERR(q
->clk
)) {
899 ret
= PTR_ERR(q
->clk
);
903 ret
= fsl_qspi_clk_prep_enable(q
);
905 dev_err(dev
, "can not enable the clock\n");
910 ret
= platform_get_irq(pdev
, 0);
912 goto err_disable_clk
;
914 ret
= devm_request_irq(dev
, ret
,
915 fsl_qspi_irq_handler
, 0, pdev
->name
, q
);
917 dev_err(dev
, "failed to request irq: %d\n", ret
);
918 goto err_disable_clk
;
921 mutex_init(&q
->lock
);
924 ctlr
->num_chipselect
= 4;
925 ctlr
->mem_ops
= &fsl_qspi_mem_ops
;
927 fsl_qspi_default_setup(q
);
929 ctlr
->dev
.of_node
= np
;
931 ret
= devm_spi_register_controller(dev
, ctlr
);
933 goto err_destroy_mutex
;
938 mutex_destroy(&q
->lock
);
941 fsl_qspi_clk_disable_unprep(q
);
944 spi_controller_put(ctlr
);
946 dev_err(dev
, "Freescale QuadSPI probe failed\n");
950 static void fsl_qspi_remove(struct platform_device
*pdev
)
952 struct fsl_qspi
*q
= platform_get_drvdata(pdev
);
954 /* disable the hardware */
955 qspi_writel(q
, QUADSPI_MCR_MDIS_MASK
, q
->iobase
+ QUADSPI_MCR
);
956 qspi_writel(q
, 0x0, q
->iobase
+ QUADSPI_RSER
);
958 fsl_qspi_clk_disable_unprep(q
);
960 mutex_destroy(&q
->lock
);
963 static int fsl_qspi_suspend(struct device
*dev
)
968 static int fsl_qspi_resume(struct device
*dev
)
970 struct fsl_qspi
*q
= dev_get_drvdata(dev
);
972 fsl_qspi_default_setup(q
);
977 static const struct of_device_id fsl_qspi_dt_ids
[] = {
978 { .compatible
= "fsl,vf610-qspi", .data
= &vybrid_data
, },
979 { .compatible
= "fsl,imx6sx-qspi", .data
= &imx6sx_data
, },
980 { .compatible
= "fsl,imx7d-qspi", .data
= &imx7d_data
, },
981 { .compatible
= "fsl,imx6ul-qspi", .data
= &imx6ul_data
, },
982 { .compatible
= "fsl,ls1021a-qspi", .data
= &ls1021a_data
, },
983 { .compatible
= "fsl,ls2080a-qspi", .data
= &ls2080a_data
, },
986 MODULE_DEVICE_TABLE(of
, fsl_qspi_dt_ids
);
988 static const struct dev_pm_ops fsl_qspi_pm_ops
= {
989 .suspend
= fsl_qspi_suspend
,
990 .resume
= fsl_qspi_resume
,
993 static struct platform_driver fsl_qspi_driver
= {
995 .name
= "fsl-quadspi",
996 .of_match_table
= fsl_qspi_dt_ids
,
997 .pm
= &fsl_qspi_pm_ops
,
999 .probe
= fsl_qspi_probe
,
1000 .remove
= fsl_qspi_remove
,
1002 module_platform_driver(fsl_qspi_driver
);
1004 MODULE_DESCRIPTION("Freescale QuadSPI Controller Driver");
1005 MODULE_AUTHOR("Freescale Semiconductor Inc.");
1006 MODULE_AUTHOR("Boris Brezillon <bbrezillon@kernel.org>");
1007 MODULE_AUTHOR("Frieder Schrempf <frieder.schrempf@kontron.de>");
1008 MODULE_AUTHOR("Yogesh Gaur <yogeshnarayan.gaur@nxp.com>");
1009 MODULE_AUTHOR("Suresh Gupta <suresh.gupta@nxp.com>");
1010 MODULE_LICENSE("GPL v2");