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>
29 #include <linux/mutex.h>
32 #define QUADSPI_MCR 0x00
33 #define QUADSPI_MCR_RESERVED_SHIFT 16
34 #define QUADSPI_MCR_RESERVED_MASK (0xF << QUADSPI_MCR_RESERVED_SHIFT)
35 #define QUADSPI_MCR_MDIS_SHIFT 14
36 #define QUADSPI_MCR_MDIS_MASK (1 << QUADSPI_MCR_MDIS_SHIFT)
37 #define QUADSPI_MCR_CLR_TXF_SHIFT 11
38 #define QUADSPI_MCR_CLR_TXF_MASK (1 << QUADSPI_MCR_CLR_TXF_SHIFT)
39 #define QUADSPI_MCR_CLR_RXF_SHIFT 10
40 #define QUADSPI_MCR_CLR_RXF_MASK (1 << QUADSPI_MCR_CLR_RXF_SHIFT)
41 #define QUADSPI_MCR_DDR_EN_SHIFT 7
42 #define QUADSPI_MCR_DDR_EN_MASK (1 << QUADSPI_MCR_DDR_EN_SHIFT)
43 #define QUADSPI_MCR_END_CFG_SHIFT 2
44 #define QUADSPI_MCR_END_CFG_MASK (3 << QUADSPI_MCR_END_CFG_SHIFT)
45 #define QUADSPI_MCR_SWRSTHD_SHIFT 1
46 #define QUADSPI_MCR_SWRSTHD_MASK (1 << QUADSPI_MCR_SWRSTHD_SHIFT)
47 #define QUADSPI_MCR_SWRSTSD_SHIFT 0
48 #define QUADSPI_MCR_SWRSTSD_MASK (1 << QUADSPI_MCR_SWRSTSD_SHIFT)
50 #define QUADSPI_IPCR 0x08
51 #define QUADSPI_IPCR_SEQID_SHIFT 24
52 #define QUADSPI_IPCR_SEQID_MASK (0xF << QUADSPI_IPCR_SEQID_SHIFT)
54 #define QUADSPI_BUF0CR 0x10
55 #define QUADSPI_BUF1CR 0x14
56 #define QUADSPI_BUF2CR 0x18
57 #define QUADSPI_BUFXCR_INVALID_MSTRID 0xe
59 #define QUADSPI_BUF3CR 0x1c
60 #define QUADSPI_BUF3CR_ALLMST_SHIFT 31
61 #define QUADSPI_BUF3CR_ALLMST_MASK (1 << QUADSPI_BUF3CR_ALLMST_SHIFT)
62 #define QUADSPI_BUF3CR_ADATSZ_SHIFT 8
63 #define QUADSPI_BUF3CR_ADATSZ_MASK (0xFF << QUADSPI_BUF3CR_ADATSZ_SHIFT)
65 #define QUADSPI_BFGENCR 0x20
66 #define QUADSPI_BFGENCR_PAR_EN_SHIFT 16
67 #define QUADSPI_BFGENCR_PAR_EN_MASK (1 << (QUADSPI_BFGENCR_PAR_EN_SHIFT))
68 #define QUADSPI_BFGENCR_SEQID_SHIFT 12
69 #define QUADSPI_BFGENCR_SEQID_MASK (0xF << QUADSPI_BFGENCR_SEQID_SHIFT)
71 #define QUADSPI_BUF0IND 0x30
72 #define QUADSPI_BUF1IND 0x34
73 #define QUADSPI_BUF2IND 0x38
74 #define QUADSPI_SFAR 0x100
76 #define QUADSPI_SMPR 0x108
77 #define QUADSPI_SMPR_DDRSMP_SHIFT 16
78 #define QUADSPI_SMPR_DDRSMP_MASK (7 << QUADSPI_SMPR_DDRSMP_SHIFT)
79 #define QUADSPI_SMPR_FSDLY_SHIFT 6
80 #define QUADSPI_SMPR_FSDLY_MASK (1 << QUADSPI_SMPR_FSDLY_SHIFT)
81 #define QUADSPI_SMPR_FSPHS_SHIFT 5
82 #define QUADSPI_SMPR_FSPHS_MASK (1 << QUADSPI_SMPR_FSPHS_SHIFT)
83 #define QUADSPI_SMPR_HSENA_SHIFT 0
84 #define QUADSPI_SMPR_HSENA_MASK (1 << QUADSPI_SMPR_HSENA_SHIFT)
86 #define QUADSPI_RBSR 0x10c
87 #define QUADSPI_RBSR_RDBFL_SHIFT 8
88 #define QUADSPI_RBSR_RDBFL_MASK (0x3F << QUADSPI_RBSR_RDBFL_SHIFT)
90 #define QUADSPI_RBCT 0x110
91 #define QUADSPI_RBCT_WMRK_MASK 0x1F
92 #define QUADSPI_RBCT_RXBRD_SHIFT 8
93 #define QUADSPI_RBCT_RXBRD_USEIPS (0x1 << QUADSPI_RBCT_RXBRD_SHIFT)
95 #define QUADSPI_TBSR 0x150
96 #define QUADSPI_TBDR 0x154
97 #define QUADSPI_SR 0x15c
98 #define QUADSPI_SR_IP_ACC_SHIFT 1
99 #define QUADSPI_SR_IP_ACC_MASK (0x1 << QUADSPI_SR_IP_ACC_SHIFT)
100 #define QUADSPI_SR_AHB_ACC_SHIFT 2
101 #define QUADSPI_SR_AHB_ACC_MASK (0x1 << QUADSPI_SR_AHB_ACC_SHIFT)
103 #define QUADSPI_FR 0x160
104 #define QUADSPI_FR_TFF_MASK 0x1
106 #define QUADSPI_SFA1AD 0x180
107 #define QUADSPI_SFA2AD 0x184
108 #define QUADSPI_SFB1AD 0x188
109 #define QUADSPI_SFB2AD 0x18c
110 #define QUADSPI_RBDR 0x200
112 #define QUADSPI_LUTKEY 0x300
113 #define QUADSPI_LUTKEY_VALUE 0x5AF05AF0
115 #define QUADSPI_LCKCR 0x304
116 #define QUADSPI_LCKER_LOCK 0x1
117 #define QUADSPI_LCKER_UNLOCK 0x2
119 #define QUADSPI_RSER 0x164
120 #define QUADSPI_RSER_TFIE (0x1 << 0)
122 #define QUADSPI_LUT_BASE 0x310
125 * The definition of the LUT register shows below:
127 * ---------------------------------------------------
128 * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 |
129 * ---------------------------------------------------
131 #define OPRND0_SHIFT 0
133 #define INSTR0_SHIFT 10
134 #define OPRND1_SHIFT 16
136 /* Instruction set for the LUT register. */
146 #define LUT_JMP_ON_CS 9
147 #define LUT_ADDR_DDR 10
148 #define LUT_MODE_DDR 11
149 #define LUT_MODE2_DDR 12
150 #define LUT_MODE4_DDR 13
151 #define LUT_READ_DDR 14
152 #define LUT_WRITE_DDR 15
153 #define LUT_DATA_LEARN 16
156 * The PAD definitions for LUT register.
158 * The pad stands for the lines number of IO[0:3].
159 * For example, the Quad read need four IO lines, so you should
160 * set LUT_PAD4 which means we use four IO lines.
166 /* Oprands for the LUT register. */
167 #define ADDR24BIT 0x18
168 #define ADDR32BIT 0x20
170 /* Macros for constructing the LUT register. */
171 #define LUT0(ins, pad, opr) \
172 (((opr) << OPRND0_SHIFT) | ((LUT_##pad) << PAD0_SHIFT) | \
173 ((LUT_##ins) << INSTR0_SHIFT))
175 #define LUT1(ins, pad, opr) (LUT0(ins, pad, opr) << OPRND1_SHIFT)
177 /* other macros for LUT register. */
178 #define QUADSPI_LUT(x) (QUADSPI_LUT_BASE + (x) * 4)
179 #define QUADSPI_LUT_NUM 64
181 /* SEQID -- we can have 16 seqids at most. */
182 #define SEQID_QUAD_READ 0
187 #define SEQID_CHIP_ERASE 5
192 #define SEQID_EN4B 10
193 #define SEQID_BRWR 11
195 #define QUADSPI_MIN_IOMAP SZ_4M
197 enum fsl_qspi_devtype {
202 struct fsl_qspi_devtype_data {
203 enum fsl_qspi_devtype devtype;
209 static struct fsl_qspi_devtype_data vybrid_data = {
210 .devtype = FSL_QUADSPI_VYBRID,
216 static struct fsl_qspi_devtype_data imx6sx_data = {
217 .devtype = FSL_QUADSPI_IMX6SX,
223 #define FSL_QSPI_MAX_CHIP 4
225 struct mtd_info mtd[FSL_QSPI_MAX_CHIP];
226 struct spi_nor nor[FSL_QSPI_MAX_CHIP];
227 void __iomem *iobase;
228 void __iomem *ahb_addr;
232 struct clk *clk, *clk_en;
235 struct fsl_qspi_devtype_data *devtype_data;
239 unsigned int chip_base_addr; /* We may support two chips. */
240 bool has_second_chip;
244 static inline int is_vybrid_qspi(struct fsl_qspi *q)
246 return q->devtype_data->devtype == FSL_QUADSPI_VYBRID;
249 static inline int is_imx6sx_qspi(struct fsl_qspi *q)
251 return q->devtype_data->devtype == FSL_QUADSPI_IMX6SX;
255 * An IC bug makes us to re-arrange the 32-bit data.
256 * The following chips, such as IMX6SLX, have fixed this bug.
258 static inline u32 fsl_qspi_endian_xchg(struct fsl_qspi *q, u32 a)
260 return is_vybrid_qspi(q) ? __swab32(a) : a;
263 static inline void fsl_qspi_unlock_lut(struct fsl_qspi *q)
265 writel(QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
266 writel(QUADSPI_LCKER_UNLOCK, q->iobase + QUADSPI_LCKCR);
269 static inline void fsl_qspi_lock_lut(struct fsl_qspi *q)
271 writel(QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
272 writel(QUADSPI_LCKER_LOCK, q->iobase + QUADSPI_LCKCR);
275 static irqreturn_t fsl_qspi_irq_handler(int irq, void *dev_id)
277 struct fsl_qspi *q = dev_id;
280 /* clear interrupt */
281 reg = readl(q->iobase + QUADSPI_FR);
282 writel(reg, q->iobase + QUADSPI_FR);
284 if (reg & QUADSPI_FR_TFF_MASK)
287 dev_dbg(q->dev, "QUADSPI_FR : 0x%.8x:0x%.8x\n", q->chip_base_addr, reg);
291 static void fsl_qspi_init_lut(struct fsl_qspi *q)
293 void __iomem *base = q->iobase;
294 int rxfifo = q->devtype_data->rxfifo;
296 u8 cmd, addrlen, dummy;
299 fsl_qspi_unlock_lut(q);
301 /* Clear all the LUT table */
302 for (i = 0; i < QUADSPI_LUT_NUM; i++)
303 writel(0, base + QUADSPI_LUT_BASE + i * 4);
306 lut_base = SEQID_QUAD_READ * 4;
308 if (q->nor_size <= SZ_16M) {
309 cmd = SPINOR_OP_READ_1_1_4;
313 /* use the 4-byte address */
314 cmd = SPINOR_OP_READ_1_1_4;
319 writel(LUT0(CMD, PAD1, cmd) | LUT1(ADDR, PAD1, addrlen),
320 base + QUADSPI_LUT(lut_base));
321 writel(LUT0(DUMMY, PAD1, dummy) | LUT1(READ, PAD4, rxfifo),
322 base + QUADSPI_LUT(lut_base + 1));
325 lut_base = SEQID_WREN * 4;
326 writel(LUT0(CMD, PAD1, SPINOR_OP_WREN), base + QUADSPI_LUT(lut_base));
329 lut_base = SEQID_PP * 4;
331 if (q->nor_size <= SZ_16M) {
335 /* use the 4-byte address */
340 writel(LUT0(CMD, PAD1, cmd) | LUT1(ADDR, PAD1, addrlen),
341 base + QUADSPI_LUT(lut_base));
342 writel(LUT0(WRITE, PAD1, 0), base + QUADSPI_LUT(lut_base + 1));
345 lut_base = SEQID_RDSR * 4;
346 writel(LUT0(CMD, PAD1, SPINOR_OP_RDSR) | LUT1(READ, PAD1, 0x1),
347 base + QUADSPI_LUT(lut_base));
350 lut_base = SEQID_SE * 4;
352 if (q->nor_size <= SZ_16M) {
356 /* use the 4-byte address */
361 writel(LUT0(CMD, PAD1, cmd) | LUT1(ADDR, PAD1, addrlen),
362 base + QUADSPI_LUT(lut_base));
364 /* Erase the whole chip */
365 lut_base = SEQID_CHIP_ERASE * 4;
366 writel(LUT0(CMD, PAD1, SPINOR_OP_CHIP_ERASE),
367 base + QUADSPI_LUT(lut_base));
370 lut_base = SEQID_RDID * 4;
371 writel(LUT0(CMD, PAD1, SPINOR_OP_RDID) | LUT1(READ, PAD1, 0x8),
372 base + QUADSPI_LUT(lut_base));
375 lut_base = SEQID_WRSR * 4;
376 writel(LUT0(CMD, PAD1, SPINOR_OP_WRSR) | LUT1(WRITE, PAD1, 0x2),
377 base + QUADSPI_LUT(lut_base));
379 /* Read Configuration Register */
380 lut_base = SEQID_RDCR * 4;
381 writel(LUT0(CMD, PAD1, SPINOR_OP_RDCR) | LUT1(READ, PAD1, 0x1),
382 base + QUADSPI_LUT(lut_base));
385 lut_base = SEQID_WRDI * 4;
386 writel(LUT0(CMD, PAD1, SPINOR_OP_WRDI), base + QUADSPI_LUT(lut_base));
388 /* Enter 4 Byte Mode (Micron) */
389 lut_base = SEQID_EN4B * 4;
390 writel(LUT0(CMD, PAD1, SPINOR_OP_EN4B), base + QUADSPI_LUT(lut_base));
392 /* Enter 4 Byte Mode (Spansion) */
393 lut_base = SEQID_BRWR * 4;
394 writel(LUT0(CMD, PAD1, SPINOR_OP_BRWR), base + QUADSPI_LUT(lut_base));
396 fsl_qspi_lock_lut(q);
399 /* Get the SEQID for the command */
400 static int fsl_qspi_get_seqid(struct fsl_qspi *q, u8 cmd)
403 case SPINOR_OP_READ_1_1_4:
404 return SEQID_QUAD_READ;
413 case SPINOR_OP_CHIP_ERASE:
414 return SEQID_CHIP_ERASE;
428 dev_err(q->dev, "Unsupported cmd 0x%.2x\n", cmd);
435 fsl_qspi_runcmd(struct fsl_qspi *q, u8 cmd, unsigned int addr, int len)
437 void __iomem *base = q->iobase;
442 init_completion(&q->c);
443 dev_dbg(q->dev, "to 0x%.8x:0x%.8x, len:%d, cmd:%.2x\n",
444 q->chip_base_addr, addr, len, cmd);
447 reg = readl(base + QUADSPI_MCR);
449 writel(q->memmap_phy + q->chip_base_addr + addr, base + QUADSPI_SFAR);
450 writel(QUADSPI_RBCT_WMRK_MASK | QUADSPI_RBCT_RXBRD_USEIPS,
451 base + QUADSPI_RBCT);
452 writel(reg | QUADSPI_MCR_CLR_RXF_MASK, base + QUADSPI_MCR);
455 reg2 = readl(base + QUADSPI_SR);
456 if (reg2 & (QUADSPI_SR_IP_ACC_MASK | QUADSPI_SR_AHB_ACC_MASK)) {
458 dev_dbg(q->dev, "The controller is busy, 0x%x\n", reg2);
464 /* trigger the LUT now */
465 seqid = fsl_qspi_get_seqid(q, cmd);
466 writel((seqid << QUADSPI_IPCR_SEQID_SHIFT) | len, base + QUADSPI_IPCR);
468 /* Wait for the interrupt. */
469 if (!wait_for_completion_timeout(&q->c, msecs_to_jiffies(1000))) {
471 "cmd 0x%.2x timeout, addr@%.8x, FR:0x%.8x, SR:0x%.8x\n",
472 cmd, addr, readl(base + QUADSPI_FR),
473 readl(base + QUADSPI_SR));
479 /* restore the MCR */
480 writel(reg, base + QUADSPI_MCR);
485 /* Read out the data from the QUADSPI_RBDR buffer registers. */
486 static void fsl_qspi_read_data(struct fsl_qspi *q, int len, u8 *rxbuf)
492 tmp = readl(q->iobase + QUADSPI_RBDR + i * 4);
493 tmp = fsl_qspi_endian_xchg(q, tmp);
494 dev_dbg(q->dev, "chip addr:0x%.8x, rcv:0x%.8x\n",
495 q->chip_base_addr, tmp);
498 *((u32 *)rxbuf) = tmp;
501 memcpy(rxbuf, &tmp, len);
511 * If we have changed the content of the flash by writing or erasing,
512 * we need to invalidate the AHB buffer. If we do not do so, we may read out
513 * the wrong data. The spec tells us reset the AHB domain and Serial Flash
514 * domain at the same time.
516 static inline void fsl_qspi_invalid(struct fsl_qspi *q)
520 reg = readl(q->iobase + QUADSPI_MCR);
521 reg |= QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK;
522 writel(reg, q->iobase + QUADSPI_MCR);
525 * The minimum delay : 1 AHB + 2 SFCK clocks.
526 * Delay 1 us is enough.
530 reg &= ~(QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK);
531 writel(reg, q->iobase + QUADSPI_MCR);
534 static int fsl_qspi_nor_write(struct fsl_qspi *q, struct spi_nor *nor,
535 u8 opcode, unsigned int to, u32 *txbuf,
536 unsigned count, size_t *retlen)
541 dev_dbg(q->dev, "to 0x%.8x:0x%.8x, len : %d\n",
542 q->chip_base_addr, to, count);
544 /* clear the TX FIFO. */
545 tmp = readl(q->iobase + QUADSPI_MCR);
546 writel(tmp | QUADSPI_MCR_CLR_TXF_MASK, q->iobase + QUADSPI_MCR);
548 /* fill the TX data to the FIFO */
549 for (j = 0, i = ((count + 3) / 4); j < i; j++) {
550 tmp = fsl_qspi_endian_xchg(q, *txbuf);
551 writel(tmp, q->iobase + QUADSPI_TBDR);
556 ret = fsl_qspi_runcmd(q, opcode, to, count);
558 if (ret == 0 && retlen)
564 static void fsl_qspi_set_map_addr(struct fsl_qspi *q)
566 int nor_size = q->nor_size;
567 void __iomem *base = q->iobase;
569 writel(nor_size + q->memmap_phy, base + QUADSPI_SFA1AD);
570 writel(nor_size * 2 + q->memmap_phy, base + QUADSPI_SFA2AD);
571 writel(nor_size * 3 + q->memmap_phy, base + QUADSPI_SFB1AD);
572 writel(nor_size * 4 + q->memmap_phy, base + QUADSPI_SFB2AD);
576 * There are two different ways to read out the data from the flash:
577 * the "IP Command Read" and the "AHB Command Read".
579 * The IC guy suggests we use the "AHB Command Read" which is faster
580 * then the "IP Command Read". (What's more is that there is a bug in
581 * the "IP Command Read" in the Vybrid.)
583 * After we set up the registers for the "AHB Command Read", we can use
584 * the memcpy to read the data directly. A "missed" access to the buffer
585 * causes the controller to clear the buffer, and use the sequence pointed
586 * by the QUADSPI_BFGENCR[SEQID] to initiate a read from the flash.
588 static void fsl_qspi_init_abh_read(struct fsl_qspi *q)
590 void __iomem *base = q->iobase;
593 /* AHB configuration for access buffer 0/1/2 .*/
594 writel(QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF0CR);
595 writel(QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF1CR);
596 writel(QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF2CR);
598 * Set ADATSZ with the maximum AHB buffer size to improve the
601 writel(QUADSPI_BUF3CR_ALLMST_MASK | ((q->devtype_data->ahb_buf_size / 8)
602 << QUADSPI_BUF3CR_ADATSZ_SHIFT), base + QUADSPI_BUF3CR);
604 /* We only use the buffer3 */
605 writel(0, base + QUADSPI_BUF0IND);
606 writel(0, base + QUADSPI_BUF1IND);
607 writel(0, base + QUADSPI_BUF2IND);
609 /* Set the default lut sequence for AHB Read. */
610 seqid = fsl_qspi_get_seqid(q, q->nor[0].read_opcode);
611 writel(seqid << QUADSPI_BFGENCR_SEQID_SHIFT,
612 q->iobase + QUADSPI_BFGENCR);
615 /* We use this function to do some basic init for spi_nor_scan(). */
616 static int fsl_qspi_nor_setup(struct fsl_qspi *q)
618 void __iomem *base = q->iobase;
622 /* the default frequency, we will change it in the future.*/
623 ret = clk_set_rate(q->clk, 66000000);
627 /* Init the LUT table. */
628 fsl_qspi_init_lut(q);
630 /* Disable the module */
631 writel(QUADSPI_MCR_MDIS_MASK | QUADSPI_MCR_RESERVED_MASK,
634 reg = readl(base + QUADSPI_SMPR);
635 writel(reg & ~(QUADSPI_SMPR_FSDLY_MASK
636 | QUADSPI_SMPR_FSPHS_MASK
637 | QUADSPI_SMPR_HSENA_MASK
638 | QUADSPI_SMPR_DDRSMP_MASK), base + QUADSPI_SMPR);
640 /* Enable the module */
641 writel(QUADSPI_MCR_RESERVED_MASK | QUADSPI_MCR_END_CFG_MASK,
644 /* enable the interrupt */
645 writel(QUADSPI_RSER_TFIE, q->iobase + QUADSPI_RSER);
650 static int fsl_qspi_nor_setup_last(struct fsl_qspi *q)
652 unsigned long rate = q->clk_rate;
655 if (is_imx6sx_qspi(q))
658 ret = clk_set_rate(q->clk, rate);
662 /* Init the LUT table again. */
663 fsl_qspi_init_lut(q);
665 /* Init for AHB read */
666 fsl_qspi_init_abh_read(q);
671 static const struct of_device_id fsl_qspi_dt_ids[] = {
672 { .compatible = "fsl,vf610-qspi", .data = (void *)&vybrid_data, },
673 { .compatible = "fsl,imx6sx-qspi", .data = (void *)&imx6sx_data, },
676 MODULE_DEVICE_TABLE(of, fsl_qspi_dt_ids);
678 static void fsl_qspi_set_base_addr(struct fsl_qspi *q, struct spi_nor *nor)
680 q->chip_base_addr = q->nor_size * (nor - q->nor);
683 static int fsl_qspi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
686 struct fsl_qspi *q = nor->priv;
688 ret = fsl_qspi_runcmd(q, opcode, 0, len);
692 fsl_qspi_read_data(q, len, buf);
696 static int fsl_qspi_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len,
699 struct fsl_qspi *q = nor->priv;
703 ret = fsl_qspi_runcmd(q, opcode, 0, 1);
707 if (opcode == SPINOR_OP_CHIP_ERASE)
710 } else if (len > 0) {
711 ret = fsl_qspi_nor_write(q, nor, opcode, 0,
712 (u32 *)buf, len, NULL);
714 dev_err(q->dev, "invalid cmd %d\n", opcode);
721 static void fsl_qspi_write(struct spi_nor *nor, loff_t to,
722 size_t len, size_t *retlen, const u_char *buf)
724 struct fsl_qspi *q = nor->priv;
726 fsl_qspi_nor_write(q, nor, nor->program_opcode, to,
727 (u32 *)buf, len, retlen);
729 /* invalid the data in the AHB buffer. */
733 static int fsl_qspi_read(struct spi_nor *nor, loff_t from,
734 size_t len, size_t *retlen, u_char *buf)
736 struct fsl_qspi *q = nor->priv;
737 u8 cmd = nor->read_opcode;
739 /* if necessary,ioremap buffer before AHB read, */
741 q->memmap_offs = q->chip_base_addr + from;
742 q->memmap_len = len > QUADSPI_MIN_IOMAP ? len : QUADSPI_MIN_IOMAP;
744 q->ahb_addr = ioremap_nocache(
745 q->memmap_phy + q->memmap_offs,
748 dev_err(q->dev, "ioremap failed\n");
751 /* ioremap if the data requested is out of range */
752 } else if (q->chip_base_addr + from < q->memmap_offs
753 || q->chip_base_addr + from + len >
754 q->memmap_offs + q->memmap_len) {
755 iounmap(q->ahb_addr);
757 q->memmap_offs = q->chip_base_addr + from;
758 q->memmap_len = len > QUADSPI_MIN_IOMAP ? len : QUADSPI_MIN_IOMAP;
759 q->ahb_addr = ioremap_nocache(
760 q->memmap_phy + q->memmap_offs,
763 dev_err(q->dev, "ioremap failed\n");
768 dev_dbg(q->dev, "cmd [%x],read from 0x%p, len:%d\n",
769 cmd, q->ahb_addr + q->chip_base_addr + from - q->memmap_offs,
772 /* Read out the data directly from the AHB buffer.*/
773 memcpy(buf, q->ahb_addr + q->chip_base_addr + from - q->memmap_offs,
780 static int fsl_qspi_erase(struct spi_nor *nor, loff_t offs)
782 struct fsl_qspi *q = nor->priv;
785 dev_dbg(nor->dev, "%dKiB at 0x%08x:0x%08x\n",
786 nor->mtd->erasesize / 1024, q->chip_base_addr, (u32)offs);
788 ret = fsl_qspi_runcmd(q, nor->erase_opcode, offs, 0);
796 static int fsl_qspi_prep(struct spi_nor *nor, enum spi_nor_ops ops)
798 struct fsl_qspi *q = nor->priv;
801 mutex_lock(&q->lock);
802 ret = clk_enable(q->clk_en);
806 ret = clk_enable(q->clk);
810 fsl_qspi_set_base_addr(q, nor);
814 clk_disable(q->clk_en);
816 mutex_unlock(&q->lock);
821 static void fsl_qspi_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
823 struct fsl_qspi *q = nor->priv;
826 clk_disable(q->clk_en);
827 mutex_unlock(&q->lock);
830 static int fsl_qspi_probe(struct platform_device *pdev)
832 struct device_node *np = pdev->dev.of_node;
833 struct mtd_part_parser_data ppdata;
834 struct device *dev = &pdev->dev;
836 struct resource *res;
838 struct mtd_info *mtd;
840 const struct of_device_id *of_id =
841 of_match_device(fsl_qspi_dt_ids, &pdev->dev);
843 q = devm_kzalloc(dev, sizeof(*q), GFP_KERNEL);
847 q->nor_num = of_get_child_count(dev->of_node);
848 if (!q->nor_num || q->nor_num > FSL_QSPI_MAX_CHIP)
851 /* find the resources */
852 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "QuadSPI");
853 q->iobase = devm_ioremap_resource(dev, res);
854 if (IS_ERR(q->iobase))
855 return PTR_ERR(q->iobase);
857 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
859 if (!devm_request_mem_region(dev, res->start, resource_size(res),
861 dev_err(dev, "can't request region for resource %pR\n", res);
865 q->memmap_phy = res->start;
867 /* find the clocks */
868 q->clk_en = devm_clk_get(dev, "qspi_en");
869 if (IS_ERR(q->clk_en))
870 return PTR_ERR(q->clk_en);
872 q->clk = devm_clk_get(dev, "qspi");
874 return PTR_ERR(q->clk);
876 ret = clk_prepare_enable(q->clk_en);
878 dev_err(dev, "cannot enable the qspi_en clock: %d\n", ret);
882 ret = clk_prepare_enable(q->clk);
884 dev_err(dev, "cannot enable the qspi clock: %d\n", ret);
889 ret = platform_get_irq(pdev, 0);
891 dev_err(dev, "failed to get the irq: %d\n", ret);
895 ret = devm_request_irq(dev, ret,
896 fsl_qspi_irq_handler, 0, pdev->name, q);
898 dev_err(dev, "failed to request irq: %d\n", ret);
903 q->devtype_data = (struct fsl_qspi_devtype_data *)of_id->data;
904 platform_set_drvdata(pdev, q);
906 ret = fsl_qspi_nor_setup(q);
910 if (of_get_property(np, "fsl,qspi-has-second-chip", NULL))
911 q->has_second_chip = true;
913 mutex_init(&q->lock);
915 /* iterate the subnodes. */
916 for_each_available_child_of_node(dev->of_node, np) {
920 if (!q->has_second_chip)
932 nor->read_reg = fsl_qspi_read_reg;
933 nor->write_reg = fsl_qspi_write_reg;
934 nor->read = fsl_qspi_read;
935 nor->write = fsl_qspi_write;
936 nor->erase = fsl_qspi_erase;
938 nor->prepare = fsl_qspi_prep;
939 nor->unprepare = fsl_qspi_unprep;
941 ret = of_modalias_node(np, modalias, sizeof(modalias));
945 ret = of_property_read_u32(np, "spi-max-frequency",
950 /* set the chip address for READID */
951 fsl_qspi_set_base_addr(q, nor);
953 ret = spi_nor_scan(nor, modalias, SPI_NOR_QUAD);
958 ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
962 /* Set the correct NOR size now. */
963 if (q->nor_size == 0) {
964 q->nor_size = mtd->size;
966 /* Map the SPI NOR to accessiable address */
967 fsl_qspi_set_map_addr(q);
971 * The TX FIFO is 64 bytes in the Vybrid, but the Page Program
972 * may writes 265 bytes per time. The write is working in the
973 * unit of the TX FIFO, not in the unit of the SPI NOR's page
976 * So shrink the spi_nor->page_size if it is larger then the
979 if (nor->page_size > q->devtype_data->txfifo)
980 nor->page_size = q->devtype_data->txfifo;
985 /* finish the rest init. */
986 ret = fsl_qspi_nor_setup_last(q);
988 goto last_init_failed;
991 clk_disable(q->clk_en);
995 for (i = 0; i < q->nor_num; i++) {
997 if (!q->has_second_chip)
999 mtd_device_unregister(&q->mtd[i]);
1002 mutex_destroy(&q->lock);
1004 clk_disable_unprepare(q->clk);
1006 clk_disable_unprepare(q->clk_en);
1010 static int fsl_qspi_remove(struct platform_device *pdev)
1012 struct fsl_qspi *q = platform_get_drvdata(pdev);
1015 for (i = 0; i < q->nor_num; i++) {
1016 /* skip the holes */
1017 if (!q->has_second_chip)
1019 mtd_device_unregister(&q->mtd[i]);
1022 /* disable the hardware */
1023 writel(QUADSPI_MCR_MDIS_MASK, q->iobase + QUADSPI_MCR);
1024 writel(0x0, q->iobase + QUADSPI_RSER);
1026 mutex_destroy(&q->lock);
1027 clk_unprepare(q->clk);
1028 clk_unprepare(q->clk_en);
1031 iounmap(q->ahb_addr);
1036 static int fsl_qspi_suspend(struct platform_device *pdev, pm_message_t state)
1041 static int fsl_qspi_resume(struct platform_device *pdev)
1043 struct fsl_qspi *q = platform_get_drvdata(pdev);
1045 fsl_qspi_nor_setup(q);
1046 fsl_qspi_set_map_addr(q);
1047 fsl_qspi_nor_setup_last(q);
1052 static struct platform_driver fsl_qspi_driver = {
1054 .name = "fsl-quadspi",
1055 .bus = &platform_bus_type,
1056 .of_match_table = fsl_qspi_dt_ids,
1058 .probe = fsl_qspi_probe,
1059 .remove = fsl_qspi_remove,
1060 .suspend = fsl_qspi_suspend,
1061 .resume = fsl_qspi_resume,
1063 module_platform_driver(fsl_qspi_driver);
1065 MODULE_DESCRIPTION("Freescale QuadSPI Controller Driver");
1066 MODULE_AUTHOR("Freescale Semiconductor Inc.");
1067 MODULE_LICENSE("GPL v2");