2 * Freescale QuadSPI driver.
4 * Copyright (C) 2013 Freescale Semiconductor, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/errno.h>
15 #include <linux/platform_device.h>
16 #include <linux/sched.h>
17 #include <linux/delay.h>
19 #include <linux/clk.h>
20 #include <linux/err.h>
22 #include <linux/of_device.h>
23 #include <linux/timer.h>
24 #include <linux/jiffies.h>
25 #include <linux/completion.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/mtd/partitions.h>
28 #include <linux/mtd/spi-nor.h>
31 #define QUADSPI_MCR 0x00
32 #define QUADSPI_MCR_RESERVED_SHIFT 16
33 #define QUADSPI_MCR_RESERVED_MASK (0xF << QUADSPI_MCR_RESERVED_SHIFT)
34 #define QUADSPI_MCR_MDIS_SHIFT 14
35 #define QUADSPI_MCR_MDIS_MASK (1 << QUADSPI_MCR_MDIS_SHIFT)
36 #define QUADSPI_MCR_CLR_TXF_SHIFT 11
37 #define QUADSPI_MCR_CLR_TXF_MASK (1 << QUADSPI_MCR_CLR_TXF_SHIFT)
38 #define QUADSPI_MCR_CLR_RXF_SHIFT 10
39 #define QUADSPI_MCR_CLR_RXF_MASK (1 << QUADSPI_MCR_CLR_RXF_SHIFT)
40 #define QUADSPI_MCR_DDR_EN_SHIFT 7
41 #define QUADSPI_MCR_DDR_EN_MASK (1 << QUADSPI_MCR_DDR_EN_SHIFT)
42 #define QUADSPI_MCR_END_CFG_SHIFT 2
43 #define QUADSPI_MCR_END_CFG_MASK (3 << QUADSPI_MCR_END_CFG_SHIFT)
44 #define QUADSPI_MCR_SWRSTHD_SHIFT 1
45 #define QUADSPI_MCR_SWRSTHD_MASK (1 << QUADSPI_MCR_SWRSTHD_SHIFT)
46 #define QUADSPI_MCR_SWRSTSD_SHIFT 0
47 #define QUADSPI_MCR_SWRSTSD_MASK (1 << QUADSPI_MCR_SWRSTSD_SHIFT)
49 #define QUADSPI_IPCR 0x08
50 #define QUADSPI_IPCR_SEQID_SHIFT 24
51 #define QUADSPI_IPCR_SEQID_MASK (0xF << QUADSPI_IPCR_SEQID_SHIFT)
53 #define QUADSPI_BUF0CR 0x10
54 #define QUADSPI_BUF1CR 0x14
55 #define QUADSPI_BUF2CR 0x18
56 #define QUADSPI_BUFXCR_INVALID_MSTRID 0xe
58 #define QUADSPI_BUF3CR 0x1c
59 #define QUADSPI_BUF3CR_ALLMST_SHIFT 31
60 #define QUADSPI_BUF3CR_ALLMST_MASK (1 << QUADSPI_BUF3CR_ALLMST_SHIFT)
61 #define QUADSPI_BUF3CR_ADATSZ_SHIFT 8
62 #define QUADSPI_BUF3CR_ADATSZ_MASK (0xFF << QUADSPI_BUF3CR_ADATSZ_SHIFT)
64 #define QUADSPI_BFGENCR 0x20
65 #define QUADSPI_BFGENCR_PAR_EN_SHIFT 16
66 #define QUADSPI_BFGENCR_PAR_EN_MASK (1 << (QUADSPI_BFGENCR_PAR_EN_SHIFT))
67 #define QUADSPI_BFGENCR_SEQID_SHIFT 12
68 #define QUADSPI_BFGENCR_SEQID_MASK (0xF << QUADSPI_BFGENCR_SEQID_SHIFT)
70 #define QUADSPI_BUF0IND 0x30
71 #define QUADSPI_BUF1IND 0x34
72 #define QUADSPI_BUF2IND 0x38
73 #define QUADSPI_SFAR 0x100
75 #define QUADSPI_SMPR 0x108
76 #define QUADSPI_SMPR_DDRSMP_SHIFT 16
77 #define QUADSPI_SMPR_DDRSMP_MASK (7 << QUADSPI_SMPR_DDRSMP_SHIFT)
78 #define QUADSPI_SMPR_FSDLY_SHIFT 6
79 #define QUADSPI_SMPR_FSDLY_MASK (1 << QUADSPI_SMPR_FSDLY_SHIFT)
80 #define QUADSPI_SMPR_FSPHS_SHIFT 5
81 #define QUADSPI_SMPR_FSPHS_MASK (1 << QUADSPI_SMPR_FSPHS_SHIFT)
82 #define QUADSPI_SMPR_HSENA_SHIFT 0
83 #define QUADSPI_SMPR_HSENA_MASK (1 << QUADSPI_SMPR_HSENA_SHIFT)
85 #define QUADSPI_RBSR 0x10c
86 #define QUADSPI_RBSR_RDBFL_SHIFT 8
87 #define QUADSPI_RBSR_RDBFL_MASK (0x3F << QUADSPI_RBSR_RDBFL_SHIFT)
89 #define QUADSPI_RBCT 0x110
90 #define QUADSPI_RBCT_WMRK_MASK 0x1F
91 #define QUADSPI_RBCT_RXBRD_SHIFT 8
92 #define QUADSPI_RBCT_RXBRD_USEIPS (0x1 << QUADSPI_RBCT_RXBRD_SHIFT)
94 #define QUADSPI_TBSR 0x150
95 #define QUADSPI_TBDR 0x154
96 #define QUADSPI_SR 0x15c
97 #define QUADSPI_SR_IP_ACC_SHIFT 1
98 #define QUADSPI_SR_IP_ACC_MASK (0x1 << QUADSPI_SR_IP_ACC_SHIFT)
99 #define QUADSPI_SR_AHB_ACC_SHIFT 2
100 #define QUADSPI_SR_AHB_ACC_MASK (0x1 << QUADSPI_SR_AHB_ACC_SHIFT)
102 #define QUADSPI_FR 0x160
103 #define QUADSPI_FR_TFF_MASK 0x1
105 #define QUADSPI_SFA1AD 0x180
106 #define QUADSPI_SFA2AD 0x184
107 #define QUADSPI_SFB1AD 0x188
108 #define QUADSPI_SFB2AD 0x18c
109 #define QUADSPI_RBDR 0x200
111 #define QUADSPI_LUTKEY 0x300
112 #define QUADSPI_LUTKEY_VALUE 0x5AF05AF0
114 #define QUADSPI_LCKCR 0x304
115 #define QUADSPI_LCKER_LOCK 0x1
116 #define QUADSPI_LCKER_UNLOCK 0x2
118 #define QUADSPI_RSER 0x164
119 #define QUADSPI_RSER_TFIE (0x1 << 0)
121 #define QUADSPI_LUT_BASE 0x310
124 * The definition of the LUT register shows below:
126 * ---------------------------------------------------
127 * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 |
128 * ---------------------------------------------------
130 #define OPRND0_SHIFT 0
132 #define INSTR0_SHIFT 10
133 #define OPRND1_SHIFT 16
135 /* Instruction set for the LUT register. */
145 #define LUT_JMP_ON_CS 9
146 #define LUT_ADDR_DDR 10
147 #define LUT_MODE_DDR 11
148 #define LUT_MODE2_DDR 12
149 #define LUT_MODE4_DDR 13
150 #define LUT_READ_DDR 14
151 #define LUT_WRITE_DDR 15
152 #define LUT_DATA_LEARN 16
155 * The PAD definitions for LUT register.
157 * The pad stands for the lines number of IO[0:3].
158 * For example, the Quad read need four IO lines, so you should
159 * set LUT_PAD4 which means we use four IO lines.
165 /* Oprands for the LUT register. */
166 #define ADDR24BIT 0x18
167 #define ADDR32BIT 0x20
169 /* Macros for constructing the LUT register. */
170 #define LUT0(ins, pad, opr) \
171 (((opr) << OPRND0_SHIFT) | ((LUT_##pad) << PAD0_SHIFT) | \
172 ((LUT_##ins) << INSTR0_SHIFT))
174 #define LUT1(ins, pad, opr) (LUT0(ins, pad, opr) << OPRND1_SHIFT)
176 /* other macros for LUT register. */
177 #define QUADSPI_LUT(x) (QUADSPI_LUT_BASE + (x) * 4)
178 #define QUADSPI_LUT_NUM 64
180 /* SEQID -- we can have 16 seqids at most. */
181 #define SEQID_QUAD_READ 0
186 #define SEQID_CHIP_ERASE 5
191 #define SEQID_EN4B 10
192 #define SEQID_BRWR 11
194 enum fsl_qspi_devtype
{
199 struct fsl_qspi_devtype_data
{
200 enum fsl_qspi_devtype devtype
;
206 static struct fsl_qspi_devtype_data vybrid_data
= {
207 .devtype
= FSL_QUADSPI_VYBRID
,
213 static struct fsl_qspi_devtype_data imx6sx_data
= {
214 .devtype
= FSL_QUADSPI_IMX6SX
,
220 #define FSL_QSPI_MAX_CHIP 4
222 struct mtd_info mtd
[FSL_QSPI_MAX_CHIP
];
223 struct spi_nor nor
[FSL_QSPI_MAX_CHIP
];
224 void __iomem
*iobase
;
225 void __iomem
*ahb_base
; /* Used when read from AHB bus */
227 struct clk
*clk
, *clk_en
;
230 struct fsl_qspi_devtype_data
*devtype_data
;
234 unsigned int chip_base_addr
; /* We may support two chips. */
235 bool has_second_chip
;
238 static inline int is_vybrid_qspi(struct fsl_qspi
*q
)
240 return q
->devtype_data
->devtype
== FSL_QUADSPI_VYBRID
;
243 static inline int is_imx6sx_qspi(struct fsl_qspi
*q
)
245 return q
->devtype_data
->devtype
== FSL_QUADSPI_IMX6SX
;
249 * An IC bug makes us to re-arrange the 32-bit data.
250 * The following chips, such as IMX6SLX, have fixed this bug.
252 static inline u32
fsl_qspi_endian_xchg(struct fsl_qspi
*q
, u32 a
)
254 return is_vybrid_qspi(q
) ? __swab32(a
) : a
;
257 static inline void fsl_qspi_unlock_lut(struct fsl_qspi
*q
)
259 writel(QUADSPI_LUTKEY_VALUE
, q
->iobase
+ QUADSPI_LUTKEY
);
260 writel(QUADSPI_LCKER_UNLOCK
, q
->iobase
+ QUADSPI_LCKCR
);
263 static inline void fsl_qspi_lock_lut(struct fsl_qspi
*q
)
265 writel(QUADSPI_LUTKEY_VALUE
, q
->iobase
+ QUADSPI_LUTKEY
);
266 writel(QUADSPI_LCKER_LOCK
, q
->iobase
+ QUADSPI_LCKCR
);
269 static irqreturn_t
fsl_qspi_irq_handler(int irq
, void *dev_id
)
271 struct fsl_qspi
*q
= dev_id
;
274 /* clear interrupt */
275 reg
= readl(q
->iobase
+ QUADSPI_FR
);
276 writel(reg
, q
->iobase
+ QUADSPI_FR
);
278 if (reg
& QUADSPI_FR_TFF_MASK
)
281 dev_dbg(q
->dev
, "QUADSPI_FR : 0x%.8x:0x%.8x\n", q
->chip_base_addr
, reg
);
285 static void fsl_qspi_init_lut(struct fsl_qspi
*q
)
287 void __iomem
*base
= q
->iobase
;
288 int rxfifo
= q
->devtype_data
->rxfifo
;
290 u8 cmd
, addrlen
, dummy
;
293 fsl_qspi_unlock_lut(q
);
295 /* Clear all the LUT table */
296 for (i
= 0; i
< QUADSPI_LUT_NUM
; i
++)
297 writel(0, base
+ QUADSPI_LUT_BASE
+ i
* 4);
300 lut_base
= SEQID_QUAD_READ
* 4;
302 if (q
->nor_size
<= SZ_16M
) {
303 cmd
= SPINOR_OP_READ_1_1_4
;
307 /* use the 4-byte address */
308 cmd
= SPINOR_OP_READ_1_1_4
;
313 writel(LUT0(CMD
, PAD1
, cmd
) | LUT1(ADDR
, PAD1
, addrlen
),
314 base
+ QUADSPI_LUT(lut_base
));
315 writel(LUT0(DUMMY
, PAD1
, dummy
) | LUT1(READ
, PAD4
, rxfifo
),
316 base
+ QUADSPI_LUT(lut_base
+ 1));
319 lut_base
= SEQID_WREN
* 4;
320 writel(LUT0(CMD
, PAD1
, SPINOR_OP_WREN
), base
+ QUADSPI_LUT(lut_base
));
323 lut_base
= SEQID_PP
* 4;
325 if (q
->nor_size
<= SZ_16M
) {
329 /* use the 4-byte address */
334 writel(LUT0(CMD
, PAD1
, cmd
) | LUT1(ADDR
, PAD1
, addrlen
),
335 base
+ QUADSPI_LUT(lut_base
));
336 writel(LUT0(WRITE
, PAD1
, 0), base
+ QUADSPI_LUT(lut_base
+ 1));
339 lut_base
= SEQID_RDSR
* 4;
340 writel(LUT0(CMD
, PAD1
, SPINOR_OP_RDSR
) | LUT1(READ
, PAD1
, 0x1),
341 base
+ QUADSPI_LUT(lut_base
));
344 lut_base
= SEQID_SE
* 4;
346 if (q
->nor_size
<= SZ_16M
) {
350 /* use the 4-byte address */
355 writel(LUT0(CMD
, PAD1
, cmd
) | LUT1(ADDR
, PAD1
, addrlen
),
356 base
+ QUADSPI_LUT(lut_base
));
358 /* Erase the whole chip */
359 lut_base
= SEQID_CHIP_ERASE
* 4;
360 writel(LUT0(CMD
, PAD1
, SPINOR_OP_CHIP_ERASE
),
361 base
+ QUADSPI_LUT(lut_base
));
364 lut_base
= SEQID_RDID
* 4;
365 writel(LUT0(CMD
, PAD1
, SPINOR_OP_RDID
) | LUT1(READ
, PAD1
, 0x8),
366 base
+ QUADSPI_LUT(lut_base
));
369 lut_base
= SEQID_WRSR
* 4;
370 writel(LUT0(CMD
, PAD1
, SPINOR_OP_WRSR
) | LUT1(WRITE
, PAD1
, 0x2),
371 base
+ QUADSPI_LUT(lut_base
));
373 /* Read Configuration Register */
374 lut_base
= SEQID_RDCR
* 4;
375 writel(LUT0(CMD
, PAD1
, SPINOR_OP_RDCR
) | LUT1(READ
, PAD1
, 0x1),
376 base
+ QUADSPI_LUT(lut_base
));
379 lut_base
= SEQID_WRDI
* 4;
380 writel(LUT0(CMD
, PAD1
, SPINOR_OP_WRDI
), base
+ QUADSPI_LUT(lut_base
));
382 /* Enter 4 Byte Mode (Micron) */
383 lut_base
= SEQID_EN4B
* 4;
384 writel(LUT0(CMD
, PAD1
, SPINOR_OP_EN4B
), base
+ QUADSPI_LUT(lut_base
));
386 /* Enter 4 Byte Mode (Spansion) */
387 lut_base
= SEQID_BRWR
* 4;
388 writel(LUT0(CMD
, PAD1
, SPINOR_OP_BRWR
), base
+ QUADSPI_LUT(lut_base
));
390 fsl_qspi_lock_lut(q
);
393 /* Get the SEQID for the command */
394 static int fsl_qspi_get_seqid(struct fsl_qspi
*q
, u8 cmd
)
397 case SPINOR_OP_READ_1_1_4
:
398 return SEQID_QUAD_READ
;
407 case SPINOR_OP_CHIP_ERASE
:
408 return SEQID_CHIP_ERASE
;
422 dev_err(q
->dev
, "Unsupported cmd 0x%.2x\n", cmd
);
429 fsl_qspi_runcmd(struct fsl_qspi
*q
, u8 cmd
, unsigned int addr
, int len
)
431 void __iomem
*base
= q
->iobase
;
436 init_completion(&q
->c
);
437 dev_dbg(q
->dev
, "to 0x%.8x:0x%.8x, len:%d, cmd:%.2x\n",
438 q
->chip_base_addr
, addr
, len
, cmd
);
441 reg
= readl(base
+ QUADSPI_MCR
);
443 writel(q
->memmap_phy
+ q
->chip_base_addr
+ addr
, base
+ QUADSPI_SFAR
);
444 writel(QUADSPI_RBCT_WMRK_MASK
| QUADSPI_RBCT_RXBRD_USEIPS
,
445 base
+ QUADSPI_RBCT
);
446 writel(reg
| QUADSPI_MCR_CLR_RXF_MASK
, base
+ QUADSPI_MCR
);
449 reg2
= readl(base
+ QUADSPI_SR
);
450 if (reg2
& (QUADSPI_SR_IP_ACC_MASK
| QUADSPI_SR_AHB_ACC_MASK
)) {
452 dev_dbg(q
->dev
, "The controller is busy, 0x%x\n", reg2
);
458 /* trigger the LUT now */
459 seqid
= fsl_qspi_get_seqid(q
, cmd
);
460 writel((seqid
<< QUADSPI_IPCR_SEQID_SHIFT
) | len
, base
+ QUADSPI_IPCR
);
462 /* Wait for the interrupt. */
463 if (!wait_for_completion_timeout(&q
->c
, msecs_to_jiffies(1000))) {
465 "cmd 0x%.2x timeout, addr@%.8x, FR:0x%.8x, SR:0x%.8x\n",
466 cmd
, addr
, readl(base
+ QUADSPI_FR
),
467 readl(base
+ QUADSPI_SR
));
473 /* restore the MCR */
474 writel(reg
, base
+ QUADSPI_MCR
);
479 /* Read out the data from the QUADSPI_RBDR buffer registers. */
480 static void fsl_qspi_read_data(struct fsl_qspi
*q
, int len
, u8
*rxbuf
)
486 tmp
= readl(q
->iobase
+ QUADSPI_RBDR
+ i
* 4);
487 tmp
= fsl_qspi_endian_xchg(q
, tmp
);
488 dev_dbg(q
->dev
, "chip addr:0x%.8x, rcv:0x%.8x\n",
489 q
->chip_base_addr
, tmp
);
492 *((u32
*)rxbuf
) = tmp
;
495 memcpy(rxbuf
, &tmp
, len
);
505 * If we have changed the content of the flash by writing or erasing,
506 * we need to invalidate the AHB buffer. If we do not do so, we may read out
507 * the wrong data. The spec tells us reset the AHB domain and Serial Flash
508 * domain at the same time.
510 static inline void fsl_qspi_invalid(struct fsl_qspi
*q
)
514 reg
= readl(q
->iobase
+ QUADSPI_MCR
);
515 reg
|= QUADSPI_MCR_SWRSTHD_MASK
| QUADSPI_MCR_SWRSTSD_MASK
;
516 writel(reg
, q
->iobase
+ QUADSPI_MCR
);
519 * The minimum delay : 1 AHB + 2 SFCK clocks.
520 * Delay 1 us is enough.
524 reg
&= ~(QUADSPI_MCR_SWRSTHD_MASK
| QUADSPI_MCR_SWRSTSD_MASK
);
525 writel(reg
, q
->iobase
+ QUADSPI_MCR
);
528 static int fsl_qspi_nor_write(struct fsl_qspi
*q
, struct spi_nor
*nor
,
529 u8 opcode
, unsigned int to
, u32
*txbuf
,
530 unsigned count
, size_t *retlen
)
535 dev_dbg(q
->dev
, "to 0x%.8x:0x%.8x, len : %d\n",
536 q
->chip_base_addr
, to
, count
);
538 /* clear the TX FIFO. */
539 tmp
= readl(q
->iobase
+ QUADSPI_MCR
);
540 writel(tmp
| QUADSPI_MCR_CLR_RXF_MASK
, q
->iobase
+ QUADSPI_MCR
);
542 /* fill the TX data to the FIFO */
543 for (j
= 0, i
= ((count
+ 3) / 4); j
< i
; j
++) {
544 tmp
= fsl_qspi_endian_xchg(q
, *txbuf
);
545 writel(tmp
, q
->iobase
+ QUADSPI_TBDR
);
550 ret
= fsl_qspi_runcmd(q
, opcode
, to
, count
);
552 if (ret
== 0 && retlen
)
558 static void fsl_qspi_set_map_addr(struct fsl_qspi
*q
)
560 int nor_size
= q
->nor_size
;
561 void __iomem
*base
= q
->iobase
;
563 writel(nor_size
+ q
->memmap_phy
, base
+ QUADSPI_SFA1AD
);
564 writel(nor_size
* 2 + q
->memmap_phy
, base
+ QUADSPI_SFA2AD
);
565 writel(nor_size
* 3 + q
->memmap_phy
, base
+ QUADSPI_SFB1AD
);
566 writel(nor_size
* 4 + q
->memmap_phy
, base
+ QUADSPI_SFB2AD
);
570 * There are two different ways to read out the data from the flash:
571 * the "IP Command Read" and the "AHB Command Read".
573 * The IC guy suggests we use the "AHB Command Read" which is faster
574 * then the "IP Command Read". (What's more is that there is a bug in
575 * the "IP Command Read" in the Vybrid.)
577 * After we set up the registers for the "AHB Command Read", we can use
578 * the memcpy to read the data directly. A "missed" access to the buffer
579 * causes the controller to clear the buffer, and use the sequence pointed
580 * by the QUADSPI_BFGENCR[SEQID] to initiate a read from the flash.
582 static void fsl_qspi_init_abh_read(struct fsl_qspi
*q
)
584 void __iomem
*base
= q
->iobase
;
587 /* AHB configuration for access buffer 0/1/2 .*/
588 writel(QUADSPI_BUFXCR_INVALID_MSTRID
, base
+ QUADSPI_BUF0CR
);
589 writel(QUADSPI_BUFXCR_INVALID_MSTRID
, base
+ QUADSPI_BUF1CR
);
590 writel(QUADSPI_BUFXCR_INVALID_MSTRID
, base
+ QUADSPI_BUF2CR
);
592 * Set ADATSZ with the maximum AHB buffer size to improve the
595 writel(QUADSPI_BUF3CR_ALLMST_MASK
| ((q
->devtype_data
->ahb_buf_size
/ 8)
596 << QUADSPI_BUF3CR_ADATSZ_SHIFT
), base
+ QUADSPI_BUF3CR
);
598 /* We only use the buffer3 */
599 writel(0, base
+ QUADSPI_BUF0IND
);
600 writel(0, base
+ QUADSPI_BUF1IND
);
601 writel(0, base
+ QUADSPI_BUF2IND
);
603 /* Set the default lut sequence for AHB Read. */
604 seqid
= fsl_qspi_get_seqid(q
, q
->nor
[0].read_opcode
);
605 writel(seqid
<< QUADSPI_BFGENCR_SEQID_SHIFT
,
606 q
->iobase
+ QUADSPI_BFGENCR
);
609 /* We use this function to do some basic init for spi_nor_scan(). */
610 static int fsl_qspi_nor_setup(struct fsl_qspi
*q
)
612 void __iomem
*base
= q
->iobase
;
616 /* the default frequency, we will change it in the future.*/
617 ret
= clk_set_rate(q
->clk
, 66000000);
621 /* Init the LUT table. */
622 fsl_qspi_init_lut(q
);
624 /* Disable the module */
625 writel(QUADSPI_MCR_MDIS_MASK
| QUADSPI_MCR_RESERVED_MASK
,
628 reg
= readl(base
+ QUADSPI_SMPR
);
629 writel(reg
& ~(QUADSPI_SMPR_FSDLY_MASK
630 | QUADSPI_SMPR_FSPHS_MASK
631 | QUADSPI_SMPR_HSENA_MASK
632 | QUADSPI_SMPR_DDRSMP_MASK
), base
+ QUADSPI_SMPR
);
634 /* Enable the module */
635 writel(QUADSPI_MCR_RESERVED_MASK
| QUADSPI_MCR_END_CFG_MASK
,
638 /* enable the interrupt */
639 writel(QUADSPI_RSER_TFIE
, q
->iobase
+ QUADSPI_RSER
);
644 static int fsl_qspi_nor_setup_last(struct fsl_qspi
*q
)
646 unsigned long rate
= q
->clk_rate
;
649 if (is_imx6sx_qspi(q
))
652 ret
= clk_set_rate(q
->clk
, rate
);
656 /* Init the LUT table again. */
657 fsl_qspi_init_lut(q
);
659 /* Init for AHB read */
660 fsl_qspi_init_abh_read(q
);
665 static struct of_device_id fsl_qspi_dt_ids
[] = {
666 { .compatible
= "fsl,vf610-qspi", .data
= (void *)&vybrid_data
, },
667 { .compatible
= "fsl,imx6sx-qspi", .data
= (void *)&imx6sx_data
, },
670 MODULE_DEVICE_TABLE(of
, fsl_qspi_dt_ids
);
672 static void fsl_qspi_set_base_addr(struct fsl_qspi
*q
, struct spi_nor
*nor
)
674 q
->chip_base_addr
= q
->nor_size
* (nor
- q
->nor
);
677 static int fsl_qspi_read_reg(struct spi_nor
*nor
, u8 opcode
, u8
*buf
, int len
)
680 struct fsl_qspi
*q
= nor
->priv
;
682 ret
= fsl_qspi_runcmd(q
, opcode
, 0, len
);
686 fsl_qspi_read_data(q
, len
, buf
);
690 static int fsl_qspi_write_reg(struct spi_nor
*nor
, u8 opcode
, u8
*buf
, int len
,
693 struct fsl_qspi
*q
= nor
->priv
;
697 ret
= fsl_qspi_runcmd(q
, opcode
, 0, 1);
701 if (opcode
== SPINOR_OP_CHIP_ERASE
)
704 } else if (len
> 0) {
705 ret
= fsl_qspi_nor_write(q
, nor
, opcode
, 0,
706 (u32
*)buf
, len
, NULL
);
708 dev_err(q
->dev
, "invalid cmd %d\n", opcode
);
715 static void fsl_qspi_write(struct spi_nor
*nor
, loff_t to
,
716 size_t len
, size_t *retlen
, const u_char
*buf
)
718 struct fsl_qspi
*q
= nor
->priv
;
720 fsl_qspi_nor_write(q
, nor
, nor
->program_opcode
, to
,
721 (u32
*)buf
, len
, retlen
);
723 /* invalid the data in the AHB buffer. */
727 static int fsl_qspi_read(struct spi_nor
*nor
, loff_t from
,
728 size_t len
, size_t *retlen
, u_char
*buf
)
730 struct fsl_qspi
*q
= nor
->priv
;
731 u8 cmd
= nor
->read_opcode
;
733 dev_dbg(q
->dev
, "cmd [%x],read from (0x%p, 0x%.8x, 0x%.8x),len:%d\n",
734 cmd
, q
->ahb_base
, q
->chip_base_addr
, (unsigned int)from
, len
);
736 /* Read out the data directly from the AHB buffer.*/
737 memcpy(buf
, q
->ahb_base
+ q
->chip_base_addr
+ from
, len
);
743 static int fsl_qspi_erase(struct spi_nor
*nor
, loff_t offs
)
745 struct fsl_qspi
*q
= nor
->priv
;
748 dev_dbg(nor
->dev
, "%dKiB at 0x%08x:0x%08x\n",
749 nor
->mtd
->erasesize
/ 1024, q
->chip_base_addr
, (u32
)offs
);
751 ret
= fsl_qspi_runcmd(q
, nor
->erase_opcode
, offs
, 0);
759 static int fsl_qspi_prep(struct spi_nor
*nor
, enum spi_nor_ops ops
)
761 struct fsl_qspi
*q
= nor
->priv
;
764 ret
= clk_enable(q
->clk_en
);
768 ret
= clk_enable(q
->clk
);
770 clk_disable(q
->clk_en
);
774 fsl_qspi_set_base_addr(q
, nor
);
778 static void fsl_qspi_unprep(struct spi_nor
*nor
, enum spi_nor_ops ops
)
780 struct fsl_qspi
*q
= nor
->priv
;
783 clk_disable(q
->clk_en
);
786 static int fsl_qspi_probe(struct platform_device
*pdev
)
788 struct device_node
*np
= pdev
->dev
.of_node
;
789 struct mtd_part_parser_data ppdata
;
790 struct device
*dev
= &pdev
->dev
;
792 struct resource
*res
;
794 struct mtd_info
*mtd
;
796 const struct of_device_id
*of_id
=
797 of_match_device(fsl_qspi_dt_ids
, &pdev
->dev
);
799 q
= devm_kzalloc(dev
, sizeof(*q
), GFP_KERNEL
);
803 q
->nor_num
= of_get_child_count(dev
->of_node
);
804 if (!q
->nor_num
|| q
->nor_num
> FSL_QSPI_MAX_CHIP
)
807 /* find the resources */
808 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "QuadSPI");
809 q
->iobase
= devm_ioremap_resource(dev
, res
);
810 if (IS_ERR(q
->iobase
))
811 return PTR_ERR(q
->iobase
);
813 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
815 q
->ahb_base
= devm_ioremap_resource(dev
, res
);
816 if (IS_ERR(q
->ahb_base
))
817 return PTR_ERR(q
->ahb_base
);
819 q
->memmap_phy
= res
->start
;
821 /* find the clocks */
822 q
->clk_en
= devm_clk_get(dev
, "qspi_en");
823 if (IS_ERR(q
->clk_en
))
824 return PTR_ERR(q
->clk_en
);
826 q
->clk
= devm_clk_get(dev
, "qspi");
828 return PTR_ERR(q
->clk
);
830 ret
= clk_prepare_enable(q
->clk_en
);
832 dev_err(dev
, "cannot enable the qspi_en clock: %d\n", ret
);
836 ret
= clk_prepare_enable(q
->clk
);
838 dev_err(dev
, "cannot enable the qspi clock: %d\n", ret
);
843 ret
= platform_get_irq(pdev
, 0);
845 dev_err(dev
, "failed to get the irq: %d\n", ret
);
849 ret
= devm_request_irq(dev
, ret
,
850 fsl_qspi_irq_handler
, 0, pdev
->name
, q
);
852 dev_err(dev
, "failed to request irq: %d\n", ret
);
857 q
->devtype_data
= (struct fsl_qspi_devtype_data
*)of_id
->data
;
858 platform_set_drvdata(pdev
, q
);
860 ret
= fsl_qspi_nor_setup(q
);
864 if (of_get_property(np
, "fsl,qspi-has-second-chip", NULL
))
865 q
->has_second_chip
= true;
867 /* iterate the subnodes. */
868 for_each_available_child_of_node(dev
->of_node
, np
) {
872 if (!q
->has_second_chip
)
884 nor
->read_reg
= fsl_qspi_read_reg
;
885 nor
->write_reg
= fsl_qspi_write_reg
;
886 nor
->read
= fsl_qspi_read
;
887 nor
->write
= fsl_qspi_write
;
888 nor
->erase
= fsl_qspi_erase
;
890 nor
->prepare
= fsl_qspi_prep
;
891 nor
->unprepare
= fsl_qspi_unprep
;
893 ret
= of_modalias_node(np
, modalias
, sizeof(modalias
));
897 ret
= of_property_read_u32(np
, "spi-max-frequency",
902 /* set the chip address for READID */
903 fsl_qspi_set_base_addr(q
, nor
);
905 ret
= spi_nor_scan(nor
, modalias
, SPI_NOR_QUAD
);
910 ret
= mtd_device_parse_register(mtd
, NULL
, &ppdata
, NULL
, 0);
914 /* Set the correct NOR size now. */
915 if (q
->nor_size
== 0) {
916 q
->nor_size
= mtd
->size
;
918 /* Map the SPI NOR to accessiable address */
919 fsl_qspi_set_map_addr(q
);
923 * The TX FIFO is 64 bytes in the Vybrid, but the Page Program
924 * may writes 265 bytes per time. The write is working in the
925 * unit of the TX FIFO, not in the unit of the SPI NOR's page
928 * So shrink the spi_nor->page_size if it is larger then the
931 if (nor
->page_size
> q
->devtype_data
->txfifo
)
932 nor
->page_size
= q
->devtype_data
->txfifo
;
937 /* finish the rest init. */
938 ret
= fsl_qspi_nor_setup_last(q
);
940 goto last_init_failed
;
943 clk_disable(q
->clk_en
);
947 for (i
= 0; i
< q
->nor_num
; i
++) {
949 if (!q
->has_second_chip
)
951 mtd_device_unregister(&q
->mtd
[i
]);
954 clk_disable_unprepare(q
->clk
);
956 clk_disable_unprepare(q
->clk_en
);
960 static int fsl_qspi_remove(struct platform_device
*pdev
)
962 struct fsl_qspi
*q
= platform_get_drvdata(pdev
);
965 for (i
= 0; i
< q
->nor_num
; i
++) {
967 if (!q
->has_second_chip
)
969 mtd_device_unregister(&q
->mtd
[i
]);
972 /* disable the hardware */
973 writel(QUADSPI_MCR_MDIS_MASK
, q
->iobase
+ QUADSPI_MCR
);
974 writel(0x0, q
->iobase
+ QUADSPI_RSER
);
976 clk_unprepare(q
->clk
);
977 clk_unprepare(q
->clk_en
);
981 static int fsl_qspi_suspend(struct platform_device
*pdev
, pm_message_t state
)
986 static int fsl_qspi_resume(struct platform_device
*pdev
)
988 struct fsl_qspi
*q
= platform_get_drvdata(pdev
);
990 fsl_qspi_nor_setup(q
);
991 fsl_qspi_set_map_addr(q
);
992 fsl_qspi_nor_setup_last(q
);
997 static struct platform_driver fsl_qspi_driver
= {
999 .name
= "fsl-quadspi",
1000 .bus
= &platform_bus_type
,
1001 .of_match_table
= fsl_qspi_dt_ids
,
1003 .probe
= fsl_qspi_probe
,
1004 .remove
= fsl_qspi_remove
,
1005 .suspend
= fsl_qspi_suspend
,
1006 .resume
= fsl_qspi_resume
,
1008 module_platform_driver(fsl_qspi_driver
);
1010 MODULE_DESCRIPTION("Freescale QuadSPI Controller Driver");
1011 MODULE_AUTHOR("Freescale Semiconductor Inc.");
1012 MODULE_LICENSE("GPL v2");