2 * Based on m25p80.c, by Mike Lavender (mike@steroidmicros.com), with
3 * influence from lart.c (Abraham Van Der Merwe) and mtd_dataflash.c
5 * Copyright (C) 2005, Intec Automation Inc.
6 * Copyright (C) 2014, Freescale Semiconductor, Inc.
8 * This code is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/mutex.h>
18 #include <linux/math64.h>
19 #include <linux/sizes.h>
20 #include <linux/slab.h>
22 #include <linux/mtd/mtd.h>
23 #include <linux/of_platform.h>
24 #include <linux/spi/flash.h>
25 #include <linux/mtd/spi-nor.h>
27 /* Define max times to check status register before we give up. */
30 * For everything but full-chip erase; probably could be much smaller, but kept
31 * around for safety for now
33 #define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ)
36 * For full-chip erase, calibrated to a 2MB flash (M25P16); should be scaled up
39 #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
41 #define SPI_NOR_MAX_ID_LEN 6
42 #define SPI_NOR_MAX_ADDR_WIDTH 4
48 * This array stores the ID bytes.
49 * The first three bytes are the JEDIC ID.
50 * JEDEC ID zero means "no ID" (mostly older chips).
52 u8 id
[SPI_NOR_MAX_ID_LEN
];
55 /* The size listed here is what works with SPINOR_OP_SE, which isn't
56 * necessarily called a "sector" by the vendor.
65 #define SECT_4K BIT(0) /* SPINOR_OP_BE_4K works uniformly */
66 #define SPI_NOR_NO_ERASE BIT(1) /* No erase command needed */
67 #define SST_WRITE BIT(2) /* use SST byte programming */
68 #define SPI_NOR_NO_FR BIT(3) /* Can't do fastread */
69 #define SECT_4K_PMC BIT(4) /* SPINOR_OP_BE_4K_PMC works uniformly */
70 #define SPI_NOR_DUAL_READ BIT(5) /* Flash supports Dual Read */
71 #define SPI_NOR_QUAD_READ BIT(6) /* Flash supports Quad Read */
72 #define USE_FSR BIT(7) /* use flag status register */
73 #define SPI_NOR_HAS_LOCK BIT(8) /* Flash supports lock/unlock via SR */
74 #define SPI_NOR_HAS_TB BIT(9) /*
75 * Flash SR has Top/Bottom (TB) protect
76 * bit. Must be used with
79 #define SPI_S3AN BIT(10) /*
80 * Xilinx Spartan 3AN In-System Flash
81 * (MFR cannot be used for probing
82 * because it has the same value as
85 #define SPI_NOR_4B_OPCODES BIT(11) /*
86 * Use dedicated 4byte address op codes
87 * to support memory size above 128Mib.
89 #define NO_CHIP_ERASE BIT(12) /* Chip does not support chip erase */
90 #define SPI_NOR_SKIP_SFDP BIT(13) /* Skip parsing of SFDP tables */
91 #define USE_CLSR BIT(14) /* use CLSR command */
94 #define JEDEC_MFR(info) ((info)->id[0])
96 static const struct flash_info
*spi_nor_match_id(const char *name
);
99 * Read the status register, returning its value in the location
100 * Return the status register value.
101 * Returns negative if error occurred.
103 static int read_sr(struct spi_nor
*nor
)
108 ret
= nor
->read_reg(nor
, SPINOR_OP_RDSR
, &val
, 1);
110 pr_err("error %d reading SR\n", (int) ret
);
118 * Read the flag status register, returning its value in the location
119 * Return the status register value.
120 * Returns negative if error occurred.
122 static int read_fsr(struct spi_nor
*nor
)
127 ret
= nor
->read_reg(nor
, SPINOR_OP_RDFSR
, &val
, 1);
129 pr_err("error %d reading FSR\n", ret
);
137 * Read configuration register, returning its value in the
138 * location. Return the configuration register value.
139 * Returns negative if error occurred.
141 static int read_cr(struct spi_nor
*nor
)
146 ret
= nor
->read_reg(nor
, SPINOR_OP_RDCR
, &val
, 1);
148 dev_err(nor
->dev
, "error %d reading CR\n", ret
);
156 * Write status register 1 byte
157 * Returns negative if error occurred.
159 static inline int write_sr(struct spi_nor
*nor
, u8 val
)
161 nor
->cmd_buf
[0] = val
;
162 return nor
->write_reg(nor
, SPINOR_OP_WRSR
, nor
->cmd_buf
, 1);
166 * Set write enable latch with Write Enable command.
167 * Returns negative if error occurred.
169 static inline int write_enable(struct spi_nor
*nor
)
171 return nor
->write_reg(nor
, SPINOR_OP_WREN
, NULL
, 0);
175 * Send write disable instruction to the chip.
177 static inline int write_disable(struct spi_nor
*nor
)
179 return nor
->write_reg(nor
, SPINOR_OP_WRDI
, NULL
, 0);
182 static inline struct spi_nor
*mtd_to_spi_nor(struct mtd_info
*mtd
)
188 static u8
spi_nor_convert_opcode(u8 opcode
, const u8 table
[][2], size_t size
)
192 for (i
= 0; i
< size
; i
++)
193 if (table
[i
][0] == opcode
)
196 /* No conversion found, keep input op code. */
200 static inline u8
spi_nor_convert_3to4_read(u8 opcode
)
202 static const u8 spi_nor_3to4_read
[][2] = {
203 { SPINOR_OP_READ
, SPINOR_OP_READ_4B
},
204 { SPINOR_OP_READ_FAST
, SPINOR_OP_READ_FAST_4B
},
205 { SPINOR_OP_READ_1_1_2
, SPINOR_OP_READ_1_1_2_4B
},
206 { SPINOR_OP_READ_1_2_2
, SPINOR_OP_READ_1_2_2_4B
},
207 { SPINOR_OP_READ_1_1_4
, SPINOR_OP_READ_1_1_4_4B
},
208 { SPINOR_OP_READ_1_4_4
, SPINOR_OP_READ_1_4_4_4B
},
210 { SPINOR_OP_READ_1_1_1_DTR
, SPINOR_OP_READ_1_1_1_DTR_4B
},
211 { SPINOR_OP_READ_1_2_2_DTR
, SPINOR_OP_READ_1_2_2_DTR_4B
},
212 { SPINOR_OP_READ_1_4_4_DTR
, SPINOR_OP_READ_1_4_4_DTR_4B
},
215 return spi_nor_convert_opcode(opcode
, spi_nor_3to4_read
,
216 ARRAY_SIZE(spi_nor_3to4_read
));
219 static inline u8
spi_nor_convert_3to4_program(u8 opcode
)
221 static const u8 spi_nor_3to4_program
[][2] = {
222 { SPINOR_OP_PP
, SPINOR_OP_PP_4B
},
223 { SPINOR_OP_PP_1_1_4
, SPINOR_OP_PP_1_1_4_4B
},
224 { SPINOR_OP_PP_1_4_4
, SPINOR_OP_PP_1_4_4_4B
},
227 return spi_nor_convert_opcode(opcode
, spi_nor_3to4_program
,
228 ARRAY_SIZE(spi_nor_3to4_program
));
231 static inline u8
spi_nor_convert_3to4_erase(u8 opcode
)
233 static const u8 spi_nor_3to4_erase
[][2] = {
234 { SPINOR_OP_BE_4K
, SPINOR_OP_BE_4K_4B
},
235 { SPINOR_OP_BE_32K
, SPINOR_OP_BE_32K_4B
},
236 { SPINOR_OP_SE
, SPINOR_OP_SE_4B
},
239 return spi_nor_convert_opcode(opcode
, spi_nor_3to4_erase
,
240 ARRAY_SIZE(spi_nor_3to4_erase
));
243 static void spi_nor_set_4byte_opcodes(struct spi_nor
*nor
,
244 const struct flash_info
*info
)
246 /* Do some manufacturer fixups first */
247 switch (JEDEC_MFR(info
)) {
248 case SNOR_MFR_SPANSION
:
249 /* No small sector erase for 4-byte command set */
250 nor
->erase_opcode
= SPINOR_OP_SE
;
251 nor
->mtd
.erasesize
= info
->sector_size
;
258 nor
->read_opcode
= spi_nor_convert_3to4_read(nor
->read_opcode
);
259 nor
->program_opcode
= spi_nor_convert_3to4_program(nor
->program_opcode
);
260 nor
->erase_opcode
= spi_nor_convert_3to4_erase(nor
->erase_opcode
);
263 /* Enable/disable 4-byte addressing mode. */
264 static inline int set_4byte(struct spi_nor
*nor
, const struct flash_info
*info
,
268 bool need_wren
= false;
271 switch (JEDEC_MFR(info
)) {
272 case SNOR_MFR_MICRON
:
273 /* Some Micron need WREN command; all will accept it */
275 case SNOR_MFR_MACRONIX
:
276 case SNOR_MFR_WINBOND
:
280 cmd
= enable
? SPINOR_OP_EN4B
: SPINOR_OP_EX4B
;
281 status
= nor
->write_reg(nor
, cmd
, NULL
, 0);
288 nor
->cmd_buf
[0] = enable
<< 7;
289 return nor
->write_reg(nor
, SPINOR_OP_BRWR
, nor
->cmd_buf
, 1);
293 static int s3an_sr_ready(struct spi_nor
*nor
)
298 ret
= nor
->read_reg(nor
, SPINOR_OP_XRDSR
, &val
, 1);
300 dev_err(nor
->dev
, "error %d reading XRDSR\n", (int) ret
);
304 return !!(val
& XSR_RDY
);
307 static inline int spi_nor_sr_ready(struct spi_nor
*nor
)
309 int sr
= read_sr(nor
);
313 if (nor
->flags
& SNOR_F_USE_CLSR
&& sr
& (SR_E_ERR
| SR_P_ERR
)) {
315 dev_err(nor
->dev
, "Erase Error occurred\n");
317 dev_err(nor
->dev
, "Programming Error occurred\n");
319 nor
->write_reg(nor
, SPINOR_OP_CLSR
, NULL
, 0);
323 return !(sr
& SR_WIP
);
326 static inline int spi_nor_fsr_ready(struct spi_nor
*nor
)
328 int fsr
= read_fsr(nor
);
332 return fsr
& FSR_READY
;
335 static int spi_nor_ready(struct spi_nor
*nor
)
339 if (nor
->flags
& SNOR_F_READY_XSR_RDY
)
340 sr
= s3an_sr_ready(nor
);
342 sr
= spi_nor_sr_ready(nor
);
345 fsr
= nor
->flags
& SNOR_F_USE_FSR
? spi_nor_fsr_ready(nor
) : 1;
352 * Service routine to read status register until ready, or timeout occurs.
353 * Returns non-zero if error.
355 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor
*nor
,
356 unsigned long timeout_jiffies
)
358 unsigned long deadline
;
359 int timeout
= 0, ret
;
361 deadline
= jiffies
+ timeout_jiffies
;
364 if (time_after_eq(jiffies
, deadline
))
367 ret
= spi_nor_ready(nor
);
376 dev_err(nor
->dev
, "flash operation timed out\n");
381 static int spi_nor_wait_till_ready(struct spi_nor
*nor
)
383 return spi_nor_wait_till_ready_with_timeout(nor
,
384 DEFAULT_READY_WAIT_JIFFIES
);
388 * Erase the whole flash memory
390 * Returns 0 if successful, non-zero otherwise.
392 static int erase_chip(struct spi_nor
*nor
)
394 dev_dbg(nor
->dev
, " %lldKiB\n", (long long)(nor
->mtd
.size
>> 10));
396 return nor
->write_reg(nor
, SPINOR_OP_CHIP_ERASE
, NULL
, 0);
399 static int spi_nor_lock_and_prep(struct spi_nor
*nor
, enum spi_nor_ops ops
)
403 mutex_lock(&nor
->lock
);
406 ret
= nor
->prepare(nor
, ops
);
408 dev_err(nor
->dev
, "failed in the preparation.\n");
409 mutex_unlock(&nor
->lock
);
416 static void spi_nor_unlock_and_unprep(struct spi_nor
*nor
, enum spi_nor_ops ops
)
419 nor
->unprepare(nor
, ops
);
420 mutex_unlock(&nor
->lock
);
424 * This code converts an address to the Default Address Mode, that has non
425 * power of two page sizes. We must support this mode because it is the default
426 * mode supported by Xilinx tools, it can access the whole flash area and
427 * changing over to the Power-of-two mode is irreversible and corrupts the
429 * Addr can safely be unsigned int, the biggest S3AN device is smaller than
432 static loff_t
spi_nor_s3an_addr_convert(struct spi_nor
*nor
, unsigned int addr
)
437 offset
= addr
% nor
->page_size
;
438 page
= addr
/ nor
->page_size
;
439 page
<<= (nor
->page_size
> 512) ? 10 : 9;
441 return page
| offset
;
445 * Initiate the erasure of a single sector
447 static int spi_nor_erase_sector(struct spi_nor
*nor
, u32 addr
)
449 u8 buf
[SPI_NOR_MAX_ADDR_WIDTH
];
452 if (nor
->flags
& SNOR_F_S3AN_ADDR_DEFAULT
)
453 addr
= spi_nor_s3an_addr_convert(nor
, addr
);
456 return nor
->erase(nor
, addr
);
459 * Default implementation, if driver doesn't have a specialized HW
462 for (i
= nor
->addr_width
- 1; i
>= 0; i
--) {
463 buf
[i
] = addr
& 0xff;
467 return nor
->write_reg(nor
, nor
->erase_opcode
, buf
, nor
->addr_width
);
471 * Erase an address range on the nor chip. The address range may extend
472 * one or more erase sectors. Return an error is there is a problem erasing.
474 static int spi_nor_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
476 struct spi_nor
*nor
= mtd_to_spi_nor(mtd
);
481 dev_dbg(nor
->dev
, "at 0x%llx, len %lld\n", (long long)instr
->addr
,
482 (long long)instr
->len
);
484 div_u64_rem(instr
->len
, mtd
->erasesize
, &rem
);
491 ret
= spi_nor_lock_and_prep(nor
, SPI_NOR_OPS_ERASE
);
495 /* whole-chip erase? */
496 if (len
== mtd
->size
&& !(nor
->flags
& SNOR_F_NO_OP_CHIP_ERASE
)) {
497 unsigned long timeout
;
501 if (erase_chip(nor
)) {
507 * Scale the timeout linearly with the size of the flash, with
508 * a minimum calibrated to an old 2MB flash. We could try to
509 * pull these from CFI/SFDP, but these values should be good
512 timeout
= max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES
,
513 CHIP_ERASE_2MB_READY_WAIT_JIFFIES
*
514 (unsigned long)(mtd
->size
/ SZ_2M
));
515 ret
= spi_nor_wait_till_ready_with_timeout(nor
, timeout
);
519 /* REVISIT in some cases we could speed up erasing large regions
520 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
521 * to use "small sector erase", but that's not always optimal.
524 /* "sector"-at-a-time erase */
529 ret
= spi_nor_erase_sector(nor
, addr
);
533 addr
+= mtd
->erasesize
;
534 len
-= mtd
->erasesize
;
536 ret
= spi_nor_wait_till_ready(nor
);
545 spi_nor_unlock_and_unprep(nor
, SPI_NOR_OPS_ERASE
);
547 instr
->state
= ret
? MTD_ERASE_FAILED
: MTD_ERASE_DONE
;
548 mtd_erase_callback(instr
);
553 static void stm_get_locked_range(struct spi_nor
*nor
, u8 sr
, loff_t
*ofs
,
556 struct mtd_info
*mtd
= &nor
->mtd
;
557 u8 mask
= SR_BP2
| SR_BP1
| SR_BP0
;
558 int shift
= ffs(mask
) - 1;
566 pow
= ((sr
& mask
) ^ mask
) >> shift
;
567 *len
= mtd
->size
>> pow
;
568 if (nor
->flags
& SNOR_F_HAS_SR_TB
&& sr
& SR_TB
)
571 *ofs
= mtd
->size
- *len
;
576 * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
577 * @locked is false); 0 otherwise
579 static int stm_check_lock_status_sr(struct spi_nor
*nor
, loff_t ofs
, uint64_t len
,
588 stm_get_locked_range(nor
, sr
, &lock_offs
, &lock_len
);
591 /* Requested range is a sub-range of locked range */
592 return (ofs
+ len
<= lock_offs
+ lock_len
) && (ofs
>= lock_offs
);
594 /* Requested range does not overlap with locked range */
595 return (ofs
>= lock_offs
+ lock_len
) || (ofs
+ len
<= lock_offs
);
598 static int stm_is_locked_sr(struct spi_nor
*nor
, loff_t ofs
, uint64_t len
,
601 return stm_check_lock_status_sr(nor
, ofs
, len
, sr
, true);
604 static int stm_is_unlocked_sr(struct spi_nor
*nor
, loff_t ofs
, uint64_t len
,
607 return stm_check_lock_status_sr(nor
, ofs
, len
, sr
, false);
611 * Lock a region of the flash. Compatible with ST Micro and similar flash.
612 * Supports the block protection bits BP{0,1,2} in the status register
613 * (SR). Does not support these features found in newer SR bitfields:
614 * - SEC: sector/block protect - only handle SEC=0 (block protect)
615 * - CMP: complement protect - only support CMP=0 (range is not complemented)
617 * Support for the following is provided conditionally for some flash:
618 * - TB: top/bottom protect
620 * Sample table portion for 8MB flash (Winbond w25q64fw):
622 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
623 * --------------------------------------------------------------------------
624 * X | X | 0 | 0 | 0 | NONE | NONE
625 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
626 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
627 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
628 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
629 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
630 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
631 * X | X | 1 | 1 | 1 | 8 MB | ALL
632 * ------|-------|-------|-------|-------|---------------|-------------------
633 * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
634 * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
635 * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
636 * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
637 * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
638 * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
640 * Returns negative on errors, 0 on success.
642 static int stm_lock(struct spi_nor
*nor
, loff_t ofs
, uint64_t len
)
644 struct mtd_info
*mtd
= &nor
->mtd
;
645 int status_old
, status_new
;
646 u8 mask
= SR_BP2
| SR_BP1
| SR_BP0
;
647 u8 shift
= ffs(mask
) - 1, pow
, val
;
649 bool can_be_top
= true, can_be_bottom
= nor
->flags
& SNOR_F_HAS_SR_TB
;
653 status_old
= read_sr(nor
);
657 /* If nothing in our range is unlocked, we don't need to do anything */
658 if (stm_is_locked_sr(nor
, ofs
, len
, status_old
))
661 /* If anything below us is unlocked, we can't use 'bottom' protection */
662 if (!stm_is_locked_sr(nor
, 0, ofs
, status_old
))
663 can_be_bottom
= false;
665 /* If anything above us is unlocked, we can't use 'top' protection */
666 if (!stm_is_locked_sr(nor
, ofs
+ len
, mtd
->size
- (ofs
+ len
),
670 if (!can_be_bottom
&& !can_be_top
)
673 /* Prefer top, if both are valid */
674 use_top
= can_be_top
;
676 /* lock_len: length of region that should end up locked */
678 lock_len
= mtd
->size
- ofs
;
680 lock_len
= ofs
+ len
;
683 * Need smallest pow such that:
685 * 1 / (2^pow) <= (len / size)
687 * so (assuming power-of-2 size) we do:
689 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
691 pow
= ilog2(mtd
->size
) - ilog2(lock_len
);
692 val
= mask
- (pow
<< shift
);
695 /* Don't "lock" with no region! */
699 status_new
= (status_old
& ~mask
& ~SR_TB
) | val
;
701 /* Disallow further writes if WP pin is asserted */
702 status_new
|= SR_SRWD
;
707 /* Don't bother if they're the same */
708 if (status_new
== status_old
)
711 /* Only modify protection if it will not unlock other areas */
712 if ((status_new
& mask
) < (status_old
& mask
))
716 ret
= write_sr(nor
, status_new
);
719 return spi_nor_wait_till_ready(nor
);
723 * Unlock a region of the flash. See stm_lock() for more info
725 * Returns negative on errors, 0 on success.
727 static int stm_unlock(struct spi_nor
*nor
, loff_t ofs
, uint64_t len
)
729 struct mtd_info
*mtd
= &nor
->mtd
;
730 int status_old
, status_new
;
731 u8 mask
= SR_BP2
| SR_BP1
| SR_BP0
;
732 u8 shift
= ffs(mask
) - 1, pow
, val
;
734 bool can_be_top
= true, can_be_bottom
= nor
->flags
& SNOR_F_HAS_SR_TB
;
738 status_old
= read_sr(nor
);
742 /* If nothing in our range is locked, we don't need to do anything */
743 if (stm_is_unlocked_sr(nor
, ofs
, len
, status_old
))
746 /* If anything below us is locked, we can't use 'top' protection */
747 if (!stm_is_unlocked_sr(nor
, 0, ofs
, status_old
))
750 /* If anything above us is locked, we can't use 'bottom' protection */
751 if (!stm_is_unlocked_sr(nor
, ofs
+ len
, mtd
->size
- (ofs
+ len
),
753 can_be_bottom
= false;
755 if (!can_be_bottom
&& !can_be_top
)
758 /* Prefer top, if both are valid */
759 use_top
= can_be_top
;
761 /* lock_len: length of region that should remain locked */
763 lock_len
= mtd
->size
- (ofs
+ len
);
768 * Need largest pow such that:
770 * 1 / (2^pow) >= (len / size)
772 * so (assuming power-of-2 size) we do:
774 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
776 pow
= ilog2(mtd
->size
) - order_base_2(lock_len
);
778 val
= 0; /* fully unlocked */
780 val
= mask
- (pow
<< shift
);
781 /* Some power-of-two sizes are not supported */
786 status_new
= (status_old
& ~mask
& ~SR_TB
) | val
;
788 /* Don't protect status register if we're fully unlocked */
790 status_new
&= ~SR_SRWD
;
795 /* Don't bother if they're the same */
796 if (status_new
== status_old
)
799 /* Only modify protection if it will not lock other areas */
800 if ((status_new
& mask
) > (status_old
& mask
))
804 ret
= write_sr(nor
, status_new
);
807 return spi_nor_wait_till_ready(nor
);
811 * Check if a region of the flash is (completely) locked. See stm_lock() for
814 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
815 * negative on errors.
817 static int stm_is_locked(struct spi_nor
*nor
, loff_t ofs
, uint64_t len
)
821 status
= read_sr(nor
);
825 return stm_is_locked_sr(nor
, ofs
, len
, status
);
828 static int spi_nor_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
830 struct spi_nor
*nor
= mtd_to_spi_nor(mtd
);
833 ret
= spi_nor_lock_and_prep(nor
, SPI_NOR_OPS_LOCK
);
837 ret
= nor
->flash_lock(nor
, ofs
, len
);
839 spi_nor_unlock_and_unprep(nor
, SPI_NOR_OPS_UNLOCK
);
843 static int spi_nor_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
845 struct spi_nor
*nor
= mtd_to_spi_nor(mtd
);
848 ret
= spi_nor_lock_and_prep(nor
, SPI_NOR_OPS_UNLOCK
);
852 ret
= nor
->flash_unlock(nor
, ofs
, len
);
854 spi_nor_unlock_and_unprep(nor
, SPI_NOR_OPS_LOCK
);
858 static int spi_nor_is_locked(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
860 struct spi_nor
*nor
= mtd_to_spi_nor(mtd
);
863 ret
= spi_nor_lock_and_prep(nor
, SPI_NOR_OPS_UNLOCK
);
867 ret
= nor
->flash_is_locked(nor
, ofs
, len
);
869 spi_nor_unlock_and_unprep(nor
, SPI_NOR_OPS_LOCK
);
873 /* Used when the "_ext_id" is two bytes at most */
874 #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
876 ((_jedec_id) >> 16) & 0xff, \
877 ((_jedec_id) >> 8) & 0xff, \
878 (_jedec_id) & 0xff, \
879 ((_ext_id) >> 8) & 0xff, \
882 .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))), \
883 .sector_size = (_sector_size), \
884 .n_sectors = (_n_sectors), \
888 #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
890 ((_jedec_id) >> 16) & 0xff, \
891 ((_jedec_id) >> 8) & 0xff, \
892 (_jedec_id) & 0xff, \
893 ((_ext_id) >> 16) & 0xff, \
894 ((_ext_id) >> 8) & 0xff, \
898 .sector_size = (_sector_size), \
899 .n_sectors = (_n_sectors), \
903 #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
904 .sector_size = (_sector_size), \
905 .n_sectors = (_n_sectors), \
906 .page_size = (_page_size), \
907 .addr_width = (_addr_width), \
910 #define S3AN_INFO(_jedec_id, _n_sectors, _page_size) \
912 ((_jedec_id) >> 16) & 0xff, \
913 ((_jedec_id) >> 8) & 0xff, \
917 .sector_size = (8*_page_size), \
918 .n_sectors = (_n_sectors), \
919 .page_size = _page_size, \
921 .flags = SPI_NOR_NO_FR | SPI_S3AN,
923 /* NOTE: double check command sets and memory organization when you add
924 * more nor chips. This current list focusses on newer chips, which
925 * have been converging on command sets which including JEDEC ID.
927 * All newly added entries should describe *hardware* and should use SECT_4K
928 * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage
929 * scenarios excluding small sectors there is config option that can be
930 * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS.
931 * For historical (and compatibility) reasons (before we got above config) some
932 * old entries may be missing 4K flag.
934 static const struct flash_info spi_nor_ids
[] = {
935 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
936 { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K
) },
937 { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K
) },
939 { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K
) },
940 { "at25df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K
) },
941 { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K
) },
942 { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K
) },
944 { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K
) },
945 { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K
) },
946 { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K
) },
947 { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K
) },
949 { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K
) },
952 { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K
) },
953 { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
954 { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
955 { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
956 { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K
) },
957 { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
958 { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
959 { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K
) },
962 { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K
| SPI_NOR_HAS_LOCK
) },
963 { "f25l32qa", INFO(0x8c4116, 0, 64 * 1024, 64, SECT_4K
| SPI_NOR_HAS_LOCK
) },
964 { "f25l64qa", INFO(0x8c4117, 0, 64 * 1024, 128, SECT_4K
| SPI_NOR_HAS_LOCK
) },
967 { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE
| SPI_NOR_NO_FR
) },
968 { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE
| SPI_NOR_NO_FR
) },
969 { "mr25h40", CAT25_INFO(512 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE
| SPI_NOR_NO_FR
) },
972 { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE
) },
976 "gd25q16", INFO(0xc84015, 0, 64 * 1024, 32,
977 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
978 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
981 "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64,
982 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
983 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
986 "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128,
987 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
988 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
991 "gd25lq64c", INFO(0xc86017, 0, 64 * 1024, 128,
992 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
993 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
996 "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256,
997 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
998 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
1001 /* Intel/Numonyx -- xxxs33b */
1002 { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
1003 { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
1004 { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
1007 { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K
) },
1010 { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K
) },
1011 { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K
) },
1012 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K
) },
1013 { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
1014 { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K
) },
1015 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, SECT_4K
) },
1016 { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K
) },
1017 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, SECT_4K
) },
1018 { "mx25u2033e", INFO(0xc22532, 0, 64 * 1024, 4, SECT_4K
) },
1019 { "mx25u4035", INFO(0xc22533, 0, 64 * 1024, 8, SECT_4K
) },
1020 { "mx25u8035", INFO(0xc22534, 0, 64 * 1024, 16, SECT_4K
) },
1021 { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K
) },
1022 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
1023 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
1024 { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
1025 { "mx25u25635f", INFO(0xc22539, 0, 64 * 1024, 512, SECT_4K
| SPI_NOR_4B_OPCODES
) },
1026 { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
1027 { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
1028 { "mx66u51235f", INFO(0xc2253a, 0, 64 * 1024, 1024, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| SPI_NOR_4B_OPCODES
) },
1029 { "mx66l1g45g", INFO(0xc2201b, 0, 64 * 1024, 2048, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
1030 { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ
) },
1033 { "n25q016a", INFO(0x20bb15, 0, 64 * 1024, 32, SECT_4K
| SPI_NOR_QUAD_READ
) },
1034 { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ
) },
1035 { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ
) },
1036 { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K
| SPI_NOR_QUAD_READ
) },
1037 { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K
| SPI_NOR_QUAD_READ
) },
1038 { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SECT_4K
| SPI_NOR_QUAD_READ
) },
1039 { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SECT_4K
| SPI_NOR_QUAD_READ
) },
1040 { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
1041 { "n25q256ax1", INFO(0x20bb19, 0, 64 * 1024, 512, SECT_4K
| SPI_NOR_QUAD_READ
) },
1042 { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K
| USE_FSR
| SPI_NOR_QUAD_READ
) },
1043 { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K
| USE_FSR
| SPI_NOR_QUAD_READ
) },
1044 { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K
| USE_FSR
| SPI_NOR_QUAD_READ
| NO_CHIP_ERASE
) },
1045 { "n25q00a", INFO(0x20bb21, 0, 64 * 1024, 2048, SECT_4K
| USE_FSR
| SPI_NOR_QUAD_READ
| NO_CHIP_ERASE
) },
1048 { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC
) },
1049 { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC
) },
1050 { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K
) },
1052 /* Spansion -- single (large) sector size only, at least
1053 * for the chips listed here (without boot sectors).
1055 { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
1056 { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
1057 { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, USE_CLSR
) },
1058 { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| USE_CLSR
) },
1059 { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| USE_CLSR
) },
1060 { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
1061 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
1062 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
1063 { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| USE_CLSR
) },
1064 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| USE_CLSR
) },
1065 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| USE_CLSR
) },
1066 { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
1067 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
1068 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
1069 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
1070 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
1071 { "s25fl004k", INFO(0xef4013, 0, 64 * 1024, 8, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
1072 { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
1073 { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
1074 { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K
) },
1075 { "s25fl116k", INFO(0x014015, 0, 64 * 1024, 32, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
1076 { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K
) },
1077 { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K
) },
1078 { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K
| SPI_NOR_DUAL_READ
) },
1079 { "s25fl208k", INFO(0x014014, 0, 64 * 1024, 16, SECT_4K
| SPI_NOR_DUAL_READ
) },
1080 { "s25fl064l", INFO(0x016017, 0, 64 * 1024, 128, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
| SPI_NOR_4B_OPCODES
) },
1082 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
1083 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K
| SST_WRITE
) },
1084 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K
| SST_WRITE
) },
1085 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K
| SST_WRITE
) },
1086 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K
| SST_WRITE
) },
1087 { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K
) },
1088 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K
| SST_WRITE
) },
1089 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K
| SST_WRITE
) },
1090 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K
| SST_WRITE
) },
1091 { "sst25wf020a", INFO(0x621612, 0, 64 * 1024, 4, SECT_4K
) },
1092 { "sst25wf040b", INFO(0x621613, 0, 64 * 1024, 8, SECT_4K
) },
1093 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K
| SST_WRITE
) },
1094 { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K
| SST_WRITE
) },
1095 { "sst26vf064b", INFO(0xbf2643, 0, 64 * 1024, 128, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
1097 /* ST Microelectronics -- newer production may have feature updates */
1098 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
1099 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
1100 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
1101 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
1102 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
1103 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
1104 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
1105 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
1106 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
1108 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
1109 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
1110 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
1111 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
1112 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
1113 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
1114 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
1115 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
1116 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
1118 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
1119 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
1120 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
1122 { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
1123 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
1124 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K
) },
1126 { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K
) },
1127 { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K
) },
1128 { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K
) },
1129 { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K
) },
1130 { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
1131 { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
1133 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
1134 { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K
) },
1135 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K
) },
1136 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K
) },
1137 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K
) },
1138 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K
) },
1139 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K
) },
1140 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K
) },
1141 { "w25q20cl", INFO(0xef4012, 0, 64 * 1024, 4, SECT_4K
) },
1142 { "w25q20bw", INFO(0xef5012, 0, 64 * 1024, 4, SECT_4K
) },
1143 { "w25q20ew", INFO(0xef6012, 0, 64 * 1024, 4, SECT_4K
) },
1144 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K
) },
1146 "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64,
1147 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
1148 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
1150 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K
) },
1151 { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K
) },
1153 "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128,
1154 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
1155 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
1158 "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256,
1159 SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
|
1160 SPI_NOR_HAS_LOCK
| SPI_NOR_HAS_TB
)
1162 { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K
) },
1163 { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K
) },
1164 { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K
) },
1165 { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K
| SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
) },
1166 { "w25m512jv", INFO(0xef7119, 0, 64 * 1024, 1024,
1167 SECT_4K
| SPI_NOR_QUAD_READ
| SPI_NOR_DUAL_READ
) },
1169 /* Catalyst / On Semiconductor -- non-JEDEC */
1170 { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE
| SPI_NOR_NO_FR
) },
1171 { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE
| SPI_NOR_NO_FR
) },
1172 { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE
| SPI_NOR_NO_FR
) },
1173 { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE
| SPI_NOR_NO_FR
) },
1174 { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE
| SPI_NOR_NO_FR
) },
1176 /* Xilinx S3AN Internal Flash */
1177 { "3S50AN", S3AN_INFO(0x1f2200, 64, 264) },
1178 { "3S200AN", S3AN_INFO(0x1f2400, 256, 264) },
1179 { "3S400AN", S3AN_INFO(0x1f2400, 256, 264) },
1180 { "3S700AN", S3AN_INFO(0x1f2500, 512, 264) },
1181 { "3S1400AN", S3AN_INFO(0x1f2600, 512, 528) },
1185 static const struct flash_info
*spi_nor_read_id(struct spi_nor
*nor
)
1188 u8 id
[SPI_NOR_MAX_ID_LEN
];
1189 const struct flash_info
*info
;
1191 tmp
= nor
->read_reg(nor
, SPINOR_OP_RDID
, id
, SPI_NOR_MAX_ID_LEN
);
1193 dev_dbg(nor
->dev
, "error %d reading JEDEC ID\n", tmp
);
1194 return ERR_PTR(tmp
);
1197 for (tmp
= 0; tmp
< ARRAY_SIZE(spi_nor_ids
) - 1; tmp
++) {
1198 info
= &spi_nor_ids
[tmp
];
1200 if (!memcmp(info
->id
, id
, info
->id_len
))
1201 return &spi_nor_ids
[tmp
];
1204 dev_err(nor
->dev
, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
1205 id
[0], id
[1], id
[2]);
1206 return ERR_PTR(-ENODEV
);
1209 static int spi_nor_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
1210 size_t *retlen
, u_char
*buf
)
1212 struct spi_nor
*nor
= mtd_to_spi_nor(mtd
);
1215 dev_dbg(nor
->dev
, "from 0x%08x, len %zd\n", (u32
)from
, len
);
1217 ret
= spi_nor_lock_and_prep(nor
, SPI_NOR_OPS_READ
);
1224 if (nor
->flags
& SNOR_F_S3AN_ADDR_DEFAULT
)
1225 addr
= spi_nor_s3an_addr_convert(nor
, addr
);
1227 ret
= nor
->read(nor
, addr
, len
, buf
);
1229 /* We shouldn't see 0-length reads */
1245 spi_nor_unlock_and_unprep(nor
, SPI_NOR_OPS_READ
);
1249 static int sst_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
1250 size_t *retlen
, const u_char
*buf
)
1252 struct spi_nor
*nor
= mtd_to_spi_nor(mtd
);
1256 dev_dbg(nor
->dev
, "to 0x%08x, len %zd\n", (u32
)to
, len
);
1258 ret
= spi_nor_lock_and_prep(nor
, SPI_NOR_OPS_WRITE
);
1264 nor
->sst_write_second
= false;
1267 /* Start write from odd address. */
1269 nor
->program_opcode
= SPINOR_OP_BP
;
1271 /* write one byte. */
1272 ret
= nor
->write(nor
, to
, 1, buf
);
1275 WARN(ret
!= 1, "While writing 1 byte written %i bytes\n",
1277 ret
= spi_nor_wait_till_ready(nor
);
1283 /* Write out most of the data here. */
1284 for (; actual
< len
- 1; actual
+= 2) {
1285 nor
->program_opcode
= SPINOR_OP_AAI_WP
;
1287 /* write two bytes. */
1288 ret
= nor
->write(nor
, to
, 2, buf
+ actual
);
1291 WARN(ret
!= 2, "While writing 2 bytes written %i bytes\n",
1293 ret
= spi_nor_wait_till_ready(nor
);
1297 nor
->sst_write_second
= true;
1299 nor
->sst_write_second
= false;
1302 ret
= spi_nor_wait_till_ready(nor
);
1306 /* Write out trailing byte if it exists. */
1307 if (actual
!= len
) {
1310 nor
->program_opcode
= SPINOR_OP_BP
;
1311 ret
= nor
->write(nor
, to
, 1, buf
+ actual
);
1314 WARN(ret
!= 1, "While writing 1 byte written %i bytes\n",
1316 ret
= spi_nor_wait_till_ready(nor
);
1324 spi_nor_unlock_and_unprep(nor
, SPI_NOR_OPS_WRITE
);
1329 * Write an address range to the nor chip. Data must be written in
1330 * FLASH_PAGESIZE chunks. The address range may be any size provided
1331 * it is within the physical boundaries.
1333 static int spi_nor_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
1334 size_t *retlen
, const u_char
*buf
)
1336 struct spi_nor
*nor
= mtd_to_spi_nor(mtd
);
1337 size_t page_offset
, page_remain
, i
;
1340 dev_dbg(nor
->dev
, "to 0x%08x, len %zd\n", (u32
)to
, len
);
1342 ret
= spi_nor_lock_and_prep(nor
, SPI_NOR_OPS_WRITE
);
1346 for (i
= 0; i
< len
; ) {
1348 loff_t addr
= to
+ i
;
1351 * If page_size is a power of two, the offset can be quickly
1352 * calculated with an AND operation. On the other cases we
1353 * need to do a modulus operation (more expensive).
1354 * Power of two numbers have only one bit set and we can use
1355 * the instruction hweight32 to detect if we need to do a
1356 * modulus (do_div()) or not.
1358 if (hweight32(nor
->page_size
) == 1) {
1359 page_offset
= addr
& (nor
->page_size
- 1);
1361 uint64_t aux
= addr
;
1363 page_offset
= do_div(aux
, nor
->page_size
);
1365 /* the size of data remaining on the first page */
1366 page_remain
= min_t(size_t,
1367 nor
->page_size
- page_offset
, len
- i
);
1369 if (nor
->flags
& SNOR_F_S3AN_ADDR_DEFAULT
)
1370 addr
= spi_nor_s3an_addr_convert(nor
, addr
);
1373 ret
= nor
->write(nor
, addr
, page_remain
, buf
+ i
);
1378 ret
= spi_nor_wait_till_ready(nor
);
1383 if (written
!= page_remain
) {
1385 "While writing %zu bytes written %zd bytes\n",
1386 page_remain
, written
);
1393 spi_nor_unlock_and_unprep(nor
, SPI_NOR_OPS_WRITE
);
1398 * macronix_quad_enable() - set QE bit in Status Register.
1399 * @nor: pointer to a 'struct spi_nor'
1401 * Set the Quad Enable (QE) bit in the Status Register.
1403 * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
1405 * Return: 0 on success, -errno otherwise.
1407 static int macronix_quad_enable(struct spi_nor
*nor
)
1414 if (val
& SR_QUAD_EN_MX
)
1419 write_sr(nor
, val
| SR_QUAD_EN_MX
);
1421 ret
= spi_nor_wait_till_ready(nor
);
1426 if (!(ret
> 0 && (ret
& SR_QUAD_EN_MX
))) {
1427 dev_err(nor
->dev
, "Macronix Quad bit not set\n");
1435 * Write status Register and configuration register with 2 bytes
1436 * The first byte will be written to the status register, while the
1437 * second byte will be written to the configuration register.
1438 * Return negative if error occurred.
1440 static int write_sr_cr(struct spi_nor
*nor
, u8
*sr_cr
)
1446 ret
= nor
->write_reg(nor
, SPINOR_OP_WRSR
, sr_cr
, 2);
1449 "error while writing configuration register\n");
1453 ret
= spi_nor_wait_till_ready(nor
);
1456 "timeout while writing configuration register\n");
1464 * spansion_quad_enable() - set QE bit in Configuraiton Register.
1465 * @nor: pointer to a 'struct spi_nor'
1467 * Set the Quad Enable (QE) bit in the Configuration Register.
1468 * This function is kept for legacy purpose because it has been used for a
1469 * long time without anybody complaining but it should be considered as
1470 * deprecated and maybe buggy.
1471 * First, this function doesn't care about the previous values of the Status
1472 * and Configuration Registers when it sets the QE bit (bit 1) in the
1473 * Configuration Register: all other bits are cleared, which may have unwanted
1474 * side effects like removing some block protections.
1475 * Secondly, it uses the Read Configuration Register (35h) instruction though
1476 * some very old and few memories don't support this instruction. If a pull-up
1477 * resistor is present on the MISO/IO1 line, we might still be able to pass the
1478 * "read back" test because the QSPI memory doesn't recognize the command,
1479 * so leaves the MISO/IO1 line state unchanged, hence read_cr() returns 0xFF.
1481 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1484 * Return: 0 on success, -errno otherwise.
1486 static int spansion_quad_enable(struct spi_nor
*nor
)
1488 u8 sr_cr
[2] = {0, CR_QUAD_EN_SPAN
};
1491 ret
= write_sr_cr(nor
, sr_cr
);
1495 /* read back and check it */
1497 if (!(ret
> 0 && (ret
& CR_QUAD_EN_SPAN
))) {
1498 dev_err(nor
->dev
, "Spansion Quad bit not set\n");
1506 * spansion_no_read_cr_quad_enable() - set QE bit in Configuration Register.
1507 * @nor: pointer to a 'struct spi_nor'
1509 * Set the Quad Enable (QE) bit in the Configuration Register.
1510 * This function should be used with QSPI memories not supporting the Read
1511 * Configuration Register (35h) instruction.
1513 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1516 * Return: 0 on success, -errno otherwise.
1518 static int spansion_no_read_cr_quad_enable(struct spi_nor
*nor
)
1523 /* Keep the current value of the Status Register. */
1526 dev_err(nor
->dev
, "error while reading status register\n");
1530 sr_cr
[1] = CR_QUAD_EN_SPAN
;
1532 return write_sr_cr(nor
, sr_cr
);
1536 * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
1537 * @nor: pointer to a 'struct spi_nor'
1539 * Set the Quad Enable (QE) bit in the Configuration Register.
1540 * This function should be used with QSPI memories supporting the Read
1541 * Configuration Register (35h) instruction.
1543 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1546 * Return: 0 on success, -errno otherwise.
1548 static int spansion_read_cr_quad_enable(struct spi_nor
*nor
)
1550 struct device
*dev
= nor
->dev
;
1554 /* Check current Quad Enable bit value. */
1557 dev_err(dev
, "error while reading configuration register\n");
1561 if (ret
& CR_QUAD_EN_SPAN
)
1564 sr_cr
[1] = ret
| CR_QUAD_EN_SPAN
;
1566 /* Keep the current value of the Status Register. */
1569 dev_err(dev
, "error while reading status register\n");
1574 ret
= write_sr_cr(nor
, sr_cr
);
1578 /* Read back and check it. */
1580 if (!(ret
> 0 && (ret
& CR_QUAD_EN_SPAN
))) {
1581 dev_err(nor
->dev
, "Spansion Quad bit not set\n");
1589 * sr2_bit7_quad_enable() - set QE bit in Status Register 2.
1590 * @nor: pointer to a 'struct spi_nor'
1592 * Set the Quad Enable (QE) bit in the Status Register 2.
1594 * This is one of the procedures to set the QE bit described in the SFDP
1595 * (JESD216 rev B) specification but no manufacturer using this procedure has
1596 * been identified yet, hence the name of the function.
1598 * Return: 0 on success, -errno otherwise.
1600 static int sr2_bit7_quad_enable(struct spi_nor
*nor
)
1605 /* Check current Quad Enable bit value. */
1606 ret
= nor
->read_reg(nor
, SPINOR_OP_RDSR2
, &sr2
, 1);
1609 if (sr2
& SR2_QUAD_EN_BIT7
)
1612 /* Update the Quad Enable bit. */
1613 sr2
|= SR2_QUAD_EN_BIT7
;
1617 ret
= nor
->write_reg(nor
, SPINOR_OP_WRSR2
, &sr2
, 1);
1619 dev_err(nor
->dev
, "error while writing status register 2\n");
1623 ret
= spi_nor_wait_till_ready(nor
);
1625 dev_err(nor
->dev
, "timeout while writing status register 2\n");
1629 /* Read back and check it. */
1630 ret
= nor
->read_reg(nor
, SPINOR_OP_RDSR2
, &sr2
, 1);
1631 if (!(ret
> 0 && (sr2
& SR2_QUAD_EN_BIT7
))) {
1632 dev_err(nor
->dev
, "SR2 Quad bit not set\n");
1639 static int spi_nor_check(struct spi_nor
*nor
)
1641 if (!nor
->dev
|| !nor
->read
|| !nor
->write
||
1642 !nor
->read_reg
|| !nor
->write_reg
) {
1643 pr_err("spi-nor: please fill all the necessary fields!\n");
1650 static int s3an_nor_scan(const struct flash_info
*info
, struct spi_nor
*nor
)
1655 ret
= nor
->read_reg(nor
, SPINOR_OP_XRDSR
, &val
, 1);
1657 dev_err(nor
->dev
, "error %d reading XRDSR\n", (int) ret
);
1661 nor
->erase_opcode
= SPINOR_OP_XSE
;
1662 nor
->program_opcode
= SPINOR_OP_XPP
;
1663 nor
->read_opcode
= SPINOR_OP_READ
;
1664 nor
->flags
|= SNOR_F_NO_OP_CHIP_ERASE
;
1667 * This flashes have a page size of 264 or 528 bytes (known as
1668 * Default addressing mode). It can be changed to a more standard
1669 * Power of two mode where the page size is 256/512. This comes
1670 * with a price: there is 3% less of space, the data is corrupted
1671 * and the page size cannot be changed back to default addressing
1674 * The current addressing mode can be read from the XRDSR register
1675 * and should not be changed, because is a destructive operation.
1677 if (val
& XSR_PAGESIZE
) {
1678 /* Flash in Power of 2 mode */
1679 nor
->page_size
= (nor
->page_size
== 264) ? 256 : 512;
1680 nor
->mtd
.writebufsize
= nor
->page_size
;
1681 nor
->mtd
.size
= 8 * nor
->page_size
* info
->n_sectors
;
1682 nor
->mtd
.erasesize
= 8 * nor
->page_size
;
1684 /* Flash in Default addressing mode */
1685 nor
->flags
|= SNOR_F_S3AN_ADDR_DEFAULT
;
1691 struct spi_nor_read_command
{
1695 enum spi_nor_protocol proto
;
1698 struct spi_nor_pp_command
{
1700 enum spi_nor_protocol proto
;
1703 enum spi_nor_read_command_index
{
1706 SNOR_CMD_READ_1_1_1_DTR
,
1709 SNOR_CMD_READ_1_1_2
,
1710 SNOR_CMD_READ_1_2_2
,
1711 SNOR_CMD_READ_2_2_2
,
1712 SNOR_CMD_READ_1_2_2_DTR
,
1715 SNOR_CMD_READ_1_1_4
,
1716 SNOR_CMD_READ_1_4_4
,
1717 SNOR_CMD_READ_4_4_4
,
1718 SNOR_CMD_READ_1_4_4_DTR
,
1721 SNOR_CMD_READ_1_1_8
,
1722 SNOR_CMD_READ_1_8_8
,
1723 SNOR_CMD_READ_8_8_8
,
1724 SNOR_CMD_READ_1_8_8_DTR
,
1729 enum spi_nor_pp_command_index
{
1745 struct spi_nor_flash_parameter
{
1749 struct spi_nor_hwcaps hwcaps
;
1750 struct spi_nor_read_command reads
[SNOR_CMD_READ_MAX
];
1751 struct spi_nor_pp_command page_programs
[SNOR_CMD_PP_MAX
];
1753 int (*quad_enable
)(struct spi_nor
*nor
);
1757 spi_nor_set_read_settings(struct spi_nor_read_command
*read
,
1761 enum spi_nor_protocol proto
)
1763 read
->num_mode_clocks
= num_mode_clocks
;
1764 read
->num_wait_states
= num_wait_states
;
1765 read
->opcode
= opcode
;
1766 read
->proto
= proto
;
1770 spi_nor_set_pp_settings(struct spi_nor_pp_command
*pp
,
1772 enum spi_nor_protocol proto
)
1774 pp
->opcode
= opcode
;
1779 * Serial Flash Discoverable Parameters (SFDP) parsing.
1783 * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
1784 * @nor: pointer to a 'struct spi_nor'
1785 * @addr: offset in the SFDP area to start reading data from
1786 * @len: number of bytes to read
1787 * @buf: buffer where the SFDP data are copied into (dma-safe memory)
1789 * Whatever the actual numbers of bytes for address and dummy cycles are
1790 * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
1791 * followed by a 3-byte address and 8 dummy clock cycles.
1793 * Return: 0 on success, -errno otherwise.
1795 static int spi_nor_read_sfdp(struct spi_nor
*nor
, u32 addr
,
1796 size_t len
, void *buf
)
1798 u8 addr_width
, read_opcode
, read_dummy
;
1801 read_opcode
= nor
->read_opcode
;
1802 addr_width
= nor
->addr_width
;
1803 read_dummy
= nor
->read_dummy
;
1805 nor
->read_opcode
= SPINOR_OP_RDSFDP
;
1806 nor
->addr_width
= 3;
1807 nor
->read_dummy
= 8;
1810 ret
= nor
->read(nor
, addr
, len
, (u8
*)buf
);
1811 if (!ret
|| ret
> len
) {
1825 nor
->read_opcode
= read_opcode
;
1826 nor
->addr_width
= addr_width
;
1827 nor
->read_dummy
= read_dummy
;
1833 * spi_nor_read_sfdp_dma_unsafe() - read Serial Flash Discoverable Parameters.
1834 * @nor: pointer to a 'struct spi_nor'
1835 * @addr: offset in the SFDP area to start reading data from
1836 * @len: number of bytes to read
1837 * @buf: buffer where the SFDP data are copied into
1839 * Wrap spi_nor_read_sfdp() using a kmalloc'ed bounce buffer as @buf is now not
1840 * guaranteed to be dma-safe.
1842 * Return: -ENOMEM if kmalloc() fails, the return code of spi_nor_read_sfdp()
1845 static int spi_nor_read_sfdp_dma_unsafe(struct spi_nor
*nor
, u32 addr
,
1846 size_t len
, void *buf
)
1851 dma_safe_buf
= kmalloc(len
, GFP_KERNEL
);
1855 ret
= spi_nor_read_sfdp(nor
, addr
, len
, dma_safe_buf
);
1856 memcpy(buf
, dma_safe_buf
, len
);
1857 kfree(dma_safe_buf
);
1862 struct sfdp_parameter_header
{
1866 u8 length
; /* in double words */
1867 u8 parameter_table_pointer
[3]; /* byte address */
1871 #define SFDP_PARAM_HEADER_ID(p) (((p)->id_msb << 8) | (p)->id_lsb)
1872 #define SFDP_PARAM_HEADER_PTP(p) \
1873 (((p)->parameter_table_pointer[2] << 16) | \
1874 ((p)->parameter_table_pointer[1] << 8) | \
1875 ((p)->parameter_table_pointer[0] << 0))
1877 #define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
1878 #define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
1880 #define SFDP_SIGNATURE 0x50444653U
1881 #define SFDP_JESD216_MAJOR 1
1882 #define SFDP_JESD216_MINOR 0
1883 #define SFDP_JESD216A_MINOR 5
1884 #define SFDP_JESD216B_MINOR 6
1886 struct sfdp_header
{
1887 u32 signature
; /* Ox50444653U <=> "SFDP" */
1890 u8 nph
; /* 0-base number of parameter headers */
1893 /* Basic Flash Parameter Table. */
1894 struct sfdp_parameter_header bfpt_header
;
1897 /* Basic Flash Parameter Table */
1900 * JESD216 rev B defines a Basic Flash Parameter Table of 16 DWORDs.
1901 * They are indexed from 1 but C arrays are indexed from 0.
1903 #define BFPT_DWORD(i) ((i) - 1)
1904 #define BFPT_DWORD_MAX 16
1906 /* The first version of JESB216 defined only 9 DWORDs. */
1907 #define BFPT_DWORD_MAX_JESD216 9
1910 #define BFPT_DWORD1_FAST_READ_1_1_2 BIT(16)
1911 #define BFPT_DWORD1_ADDRESS_BYTES_MASK GENMASK(18, 17)
1912 #define BFPT_DWORD1_ADDRESS_BYTES_3_ONLY (0x0UL << 17)
1913 #define BFPT_DWORD1_ADDRESS_BYTES_3_OR_4 (0x1UL << 17)
1914 #define BFPT_DWORD1_ADDRESS_BYTES_4_ONLY (0x2UL << 17)
1915 #define BFPT_DWORD1_DTR BIT(19)
1916 #define BFPT_DWORD1_FAST_READ_1_2_2 BIT(20)
1917 #define BFPT_DWORD1_FAST_READ_1_4_4 BIT(21)
1918 #define BFPT_DWORD1_FAST_READ_1_1_4 BIT(22)
1921 #define BFPT_DWORD5_FAST_READ_2_2_2 BIT(0)
1922 #define BFPT_DWORD5_FAST_READ_4_4_4 BIT(4)
1925 #define BFPT_DWORD11_PAGE_SIZE_SHIFT 4
1926 #define BFPT_DWORD11_PAGE_SIZE_MASK GENMASK(7, 4)
1931 * (from JESD216 rev B)
1932 * Quad Enable Requirements (QER):
1933 * - 000b: Device does not have a QE bit. Device detects 1-1-4 and 1-4-4
1934 * reads based on instruction. DQ3/HOLD# functions are hold during
1935 * instruction phase.
1936 * - 001b: QE is bit 1 of status register 2. It is set via Write Status with
1937 * two data bytes where bit 1 of the second byte is one.
1939 * Writing only one byte to the status register has the side-effect of
1940 * clearing status register 2, including the QE bit. The 100b code is
1941 * used if writing one byte to the status register does not modify
1942 * status register 2.
1943 * - 010b: QE is bit 6 of status register 1. It is set via Write Status with
1944 * one data byte where bit 6 is one.
1946 * - 011b: QE is bit 7 of status register 2. It is set via Write status
1947 * register 2 instruction 3Eh with one data byte where bit 7 is one.
1949 * The status register 2 is read using instruction 3Fh.
1950 * - 100b: QE is bit 1 of status register 2. It is set via Write Status with
1951 * two data bytes where bit 1 of the second byte is one.
1953 * In contrast to the 001b code, writing one byte to the status
1954 * register does not modify status register 2.
1955 * - 101b: QE is bit 1 of status register 2. Status register 1 is read using
1956 * Read Status instruction 05h. Status register2 is read using
1957 * instruction 35h. QE is set via Writ Status instruction 01h with
1958 * two data bytes where bit 1 of the second byte is one.
1961 #define BFPT_DWORD15_QER_MASK GENMASK(22, 20)
1962 #define BFPT_DWORD15_QER_NONE (0x0UL << 20) /* Micron */
1963 #define BFPT_DWORD15_QER_SR2_BIT1_BUGGY (0x1UL << 20)
1964 #define BFPT_DWORD15_QER_SR1_BIT6 (0x2UL << 20) /* Macronix */
1965 #define BFPT_DWORD15_QER_SR2_BIT7 (0x3UL << 20)
1966 #define BFPT_DWORD15_QER_SR2_BIT1_NO_RD (0x4UL << 20)
1967 #define BFPT_DWORD15_QER_SR2_BIT1 (0x5UL << 20) /* Spansion */
1970 u32 dwords
[BFPT_DWORD_MAX
];
1973 /* Fast Read settings. */
1976 spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command
*read
,
1978 enum spi_nor_protocol proto
)
1980 read
->num_mode_clocks
= (half
>> 5) & 0x07;
1981 read
->num_wait_states
= (half
>> 0) & 0x1f;
1982 read
->opcode
= (half
>> 8) & 0xff;
1983 read
->proto
= proto
;
1986 struct sfdp_bfpt_read
{
1987 /* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
1991 * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us
1992 * whether the Fast Read x-y-z command is supported.
1994 u32 supported_dword
;
1998 * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
1999 * encodes the op code, the number of mode clocks and the number of wait
2000 * states to be used by Fast Read x-y-z command.
2005 /* The SPI protocol for this Fast Read x-y-z command. */
2006 enum spi_nor_protocol proto
;
2009 static const struct sfdp_bfpt_read sfdp_bfpt_reads
[] = {
2010 /* Fast Read 1-1-2 */
2012 SNOR_HWCAPS_READ_1_1_2
,
2013 BFPT_DWORD(1), BIT(16), /* Supported bit */
2014 BFPT_DWORD(4), 0, /* Settings */
2018 /* Fast Read 1-2-2 */
2020 SNOR_HWCAPS_READ_1_2_2
,
2021 BFPT_DWORD(1), BIT(20), /* Supported bit */
2022 BFPT_DWORD(4), 16, /* Settings */
2026 /* Fast Read 2-2-2 */
2028 SNOR_HWCAPS_READ_2_2_2
,
2029 BFPT_DWORD(5), BIT(0), /* Supported bit */
2030 BFPT_DWORD(6), 16, /* Settings */
2034 /* Fast Read 1-1-4 */
2036 SNOR_HWCAPS_READ_1_1_4
,
2037 BFPT_DWORD(1), BIT(22), /* Supported bit */
2038 BFPT_DWORD(3), 16, /* Settings */
2042 /* Fast Read 1-4-4 */
2044 SNOR_HWCAPS_READ_1_4_4
,
2045 BFPT_DWORD(1), BIT(21), /* Supported bit */
2046 BFPT_DWORD(3), 0, /* Settings */
2050 /* Fast Read 4-4-4 */
2052 SNOR_HWCAPS_READ_4_4_4
,
2053 BFPT_DWORD(5), BIT(4), /* Supported bit */
2054 BFPT_DWORD(7), 16, /* Settings */
2059 struct sfdp_bfpt_erase
{
2061 * The half-word at offset <shift> in DWORD <dwoard> encodes the
2062 * op code and erase sector size to be used by Sector Erase commands.
2068 static const struct sfdp_bfpt_erase sfdp_bfpt_erases
[] = {
2069 /* Erase Type 1 in DWORD8 bits[15:0] */
2072 /* Erase Type 2 in DWORD8 bits[31:16] */
2073 {BFPT_DWORD(8), 16},
2075 /* Erase Type 3 in DWORD9 bits[15:0] */
2078 /* Erase Type 4 in DWORD9 bits[31:16] */
2079 {BFPT_DWORD(9), 16},
2082 static int spi_nor_hwcaps_read2cmd(u32 hwcaps
);
2085 * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
2086 * @nor: pointer to a 'struct spi_nor'
2087 * @bfpt_header: pointer to the 'struct sfdp_parameter_header' describing
2088 * the Basic Flash Parameter Table length and version
2089 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
2092 * The Basic Flash Parameter Table is the main and only mandatory table as
2093 * defined by the SFDP (JESD216) specification.
2094 * It provides us with the total size (memory density) of the data array and
2095 * the number of address bytes for Fast Read, Page Program and Sector Erase
2097 * For Fast READ commands, it also gives the number of mode clock cycles and
2098 * wait states (regrouped in the number of dummy clock cycles) for each
2099 * supported instruction op code.
2100 * For Page Program, the page size is now available since JESD216 rev A, however
2101 * the supported instruction op codes are still not provided.
2102 * For Sector Erase commands, this table stores the supported instruction op
2103 * codes and the associated sector sizes.
2104 * Finally, the Quad Enable Requirements (QER) are also available since JESD216
2105 * rev A. The QER bits encode the manufacturer dependent procedure to be
2106 * executed to set the Quad Enable (QE) bit in some internal register of the
2107 * Quad SPI memory. Indeed the QE bit, when it exists, must be set before
2108 * sending any Quad SPI command to the memory. Actually, setting the QE bit
2109 * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2
2110 * and IO3 hence enabling 4 (Quad) I/O lines.
2112 * Return: 0 on success, -errno otherwise.
2114 static int spi_nor_parse_bfpt(struct spi_nor
*nor
,
2115 const struct sfdp_parameter_header
*bfpt_header
,
2116 struct spi_nor_flash_parameter
*params
)
2118 struct mtd_info
*mtd
= &nor
->mtd
;
2119 struct sfdp_bfpt bfpt
;
2125 /* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */
2126 if (bfpt_header
->length
< BFPT_DWORD_MAX_JESD216
)
2129 /* Read the Basic Flash Parameter Table. */
2130 len
= min_t(size_t, sizeof(bfpt
),
2131 bfpt_header
->length
* sizeof(u32
));
2132 addr
= SFDP_PARAM_HEADER_PTP(bfpt_header
);
2133 memset(&bfpt
, 0, sizeof(bfpt
));
2134 err
= spi_nor_read_sfdp_dma_unsafe(nor
, addr
, len
, &bfpt
);
2138 /* Fix endianness of the BFPT DWORDs. */
2139 for (i
= 0; i
< BFPT_DWORD_MAX
; i
++)
2140 bfpt
.dwords
[i
] = le32_to_cpu(bfpt
.dwords
[i
]);
2142 /* Number of address bytes. */
2143 switch (bfpt
.dwords
[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK
) {
2144 case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY
:
2145 nor
->addr_width
= 3;
2148 case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY
:
2149 nor
->addr_width
= 4;
2156 /* Flash Memory Density (in bits). */
2157 params
->size
= bfpt
.dwords
[BFPT_DWORD(2)];
2158 if (params
->size
& BIT(31)) {
2159 params
->size
&= ~BIT(31);
2162 * Prevent overflows on params->size. Anyway, a NOR of 2^64
2163 * bits is unlikely to exist so this error probably means
2164 * the BFPT we are reading is corrupted/wrong.
2166 if (params
->size
> 63)
2169 params
->size
= 1ULL << params
->size
;
2173 params
->size
>>= 3; /* Convert to bytes. */
2175 /* Fast Read settings. */
2176 for (i
= 0; i
< ARRAY_SIZE(sfdp_bfpt_reads
); i
++) {
2177 const struct sfdp_bfpt_read
*rd
= &sfdp_bfpt_reads
[i
];
2178 struct spi_nor_read_command
*read
;
2180 if (!(bfpt
.dwords
[rd
->supported_dword
] & rd
->supported_bit
)) {
2181 params
->hwcaps
.mask
&= ~rd
->hwcaps
;
2185 params
->hwcaps
.mask
|= rd
->hwcaps
;
2186 cmd
= spi_nor_hwcaps_read2cmd(rd
->hwcaps
);
2187 read
= ¶ms
->reads
[cmd
];
2188 half
= bfpt
.dwords
[rd
->settings_dword
] >> rd
->settings_shift
;
2189 spi_nor_set_read_settings_from_bfpt(read
, half
, rd
->proto
);
2192 /* Sector Erase settings. */
2193 for (i
= 0; i
< ARRAY_SIZE(sfdp_bfpt_erases
); i
++) {
2194 const struct sfdp_bfpt_erase
*er
= &sfdp_bfpt_erases
[i
];
2198 half
= bfpt
.dwords
[er
->dword
] >> er
->shift
;
2199 erasesize
= half
& 0xff;
2201 /* erasesize == 0 means this Erase Type is not supported. */
2205 erasesize
= 1U << erasesize
;
2206 opcode
= (half
>> 8) & 0xff;
2207 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
2208 if (erasesize
== SZ_4K
) {
2209 nor
->erase_opcode
= opcode
;
2210 mtd
->erasesize
= erasesize
;
2214 if (!mtd
->erasesize
|| mtd
->erasesize
< erasesize
) {
2215 nor
->erase_opcode
= opcode
;
2216 mtd
->erasesize
= erasesize
;
2220 /* Stop here if not JESD216 rev A or later. */
2221 if (bfpt_header
->length
< BFPT_DWORD_MAX
)
2224 /* Page size: this field specifies 'N' so the page size = 2^N bytes. */
2225 params
->page_size
= bfpt
.dwords
[BFPT_DWORD(11)];
2226 params
->page_size
&= BFPT_DWORD11_PAGE_SIZE_MASK
;
2227 params
->page_size
>>= BFPT_DWORD11_PAGE_SIZE_SHIFT
;
2228 params
->page_size
= 1U << params
->page_size
;
2230 /* Quad Enable Requirements. */
2231 switch (bfpt
.dwords
[BFPT_DWORD(15)] & BFPT_DWORD15_QER_MASK
) {
2232 case BFPT_DWORD15_QER_NONE
:
2233 params
->quad_enable
= NULL
;
2236 case BFPT_DWORD15_QER_SR2_BIT1_BUGGY
:
2237 case BFPT_DWORD15_QER_SR2_BIT1_NO_RD
:
2238 params
->quad_enable
= spansion_no_read_cr_quad_enable
;
2241 case BFPT_DWORD15_QER_SR1_BIT6
:
2242 params
->quad_enable
= macronix_quad_enable
;
2245 case BFPT_DWORD15_QER_SR2_BIT7
:
2246 params
->quad_enable
= sr2_bit7_quad_enable
;
2249 case BFPT_DWORD15_QER_SR2_BIT1
:
2250 params
->quad_enable
= spansion_read_cr_quad_enable
;
2261 * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
2262 * @nor: pointer to a 'struct spi_nor'
2263 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
2266 * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
2267 * specification. This is a standard which tends to supported by almost all
2268 * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
2269 * runtime the main parameters needed to perform basic SPI flash operations such
2270 * as Fast Read, Page Program or Sector Erase commands.
2272 * Return: 0 on success, -errno otherwise.
2274 static int spi_nor_parse_sfdp(struct spi_nor
*nor
,
2275 struct spi_nor_flash_parameter
*params
)
2277 const struct sfdp_parameter_header
*param_header
, *bfpt_header
;
2278 struct sfdp_parameter_header
*param_headers
= NULL
;
2279 struct sfdp_header header
;
2280 struct device
*dev
= nor
->dev
;
2284 /* Get the SFDP header. */
2285 err
= spi_nor_read_sfdp_dma_unsafe(nor
, 0, sizeof(header
), &header
);
2289 /* Check the SFDP header version. */
2290 if (le32_to_cpu(header
.signature
) != SFDP_SIGNATURE
||
2291 header
.major
!= SFDP_JESD216_MAJOR
||
2292 header
.minor
< SFDP_JESD216_MINOR
)
2296 * Verify that the first and only mandatory parameter header is a
2297 * Basic Flash Parameter Table header as specified in JESD216.
2299 bfpt_header
= &header
.bfpt_header
;
2300 if (SFDP_PARAM_HEADER_ID(bfpt_header
) != SFDP_BFPT_ID
||
2301 bfpt_header
->major
!= SFDP_JESD216_MAJOR
)
2305 * Allocate memory then read all parameter headers with a single
2306 * Read SFDP command. These parameter headers will actually be parsed
2307 * twice: a first time to get the latest revision of the basic flash
2308 * parameter table, then a second time to handle the supported optional
2310 * Hence we read the parameter headers once for all to reduce the
2311 * processing time. Also we use kmalloc() instead of devm_kmalloc()
2312 * because we don't need to keep these parameter headers: the allocated
2313 * memory is always released with kfree() before exiting this function.
2316 psize
= header
.nph
* sizeof(*param_headers
);
2318 param_headers
= kmalloc(psize
, GFP_KERNEL
);
2322 err
= spi_nor_read_sfdp(nor
, sizeof(header
),
2323 psize
, param_headers
);
2325 dev_err(dev
, "failed to read SFDP parameter headers\n");
2331 * Check other parameter headers to get the latest revision of
2332 * the basic flash parameter table.
2334 for (i
= 0; i
< header
.nph
; i
++) {
2335 param_header
= ¶m_headers
[i
];
2337 if (SFDP_PARAM_HEADER_ID(param_header
) == SFDP_BFPT_ID
&&
2338 param_header
->major
== SFDP_JESD216_MAJOR
&&
2339 (param_header
->minor
> bfpt_header
->minor
||
2340 (param_header
->minor
== bfpt_header
->minor
&&
2341 param_header
->length
> bfpt_header
->length
)))
2342 bfpt_header
= param_header
;
2345 err
= spi_nor_parse_bfpt(nor
, bfpt_header
, params
);
2349 /* Parse other parameter headers. */
2350 for (i
= 0; i
< header
.nph
; i
++) {
2351 param_header
= ¶m_headers
[i
];
2353 switch (SFDP_PARAM_HEADER_ID(param_header
)) {
2354 case SFDP_SECTOR_MAP_ID
:
2355 dev_info(dev
, "non-uniform erase sector maps are not supported yet.\n");
2367 kfree(param_headers
);
2371 static int spi_nor_init_params(struct spi_nor
*nor
,
2372 const struct flash_info
*info
,
2373 struct spi_nor_flash_parameter
*params
)
2375 /* Set legacy flash parameters as default. */
2376 memset(params
, 0, sizeof(*params
));
2378 /* Set SPI NOR sizes. */
2379 params
->size
= info
->sector_size
* info
->n_sectors
;
2380 params
->page_size
= info
->page_size
;
2382 /* (Fast) Read settings. */
2383 params
->hwcaps
.mask
|= SNOR_HWCAPS_READ
;
2384 spi_nor_set_read_settings(¶ms
->reads
[SNOR_CMD_READ
],
2385 0, 0, SPINOR_OP_READ
,
2388 if (!(info
->flags
& SPI_NOR_NO_FR
)) {
2389 params
->hwcaps
.mask
|= SNOR_HWCAPS_READ_FAST
;
2390 spi_nor_set_read_settings(¶ms
->reads
[SNOR_CMD_READ_FAST
],
2391 0, 8, SPINOR_OP_READ_FAST
,
2395 if (info
->flags
& SPI_NOR_DUAL_READ
) {
2396 params
->hwcaps
.mask
|= SNOR_HWCAPS_READ_1_1_2
;
2397 spi_nor_set_read_settings(¶ms
->reads
[SNOR_CMD_READ_1_1_2
],
2398 0, 8, SPINOR_OP_READ_1_1_2
,
2402 if (info
->flags
& SPI_NOR_QUAD_READ
) {
2403 params
->hwcaps
.mask
|= SNOR_HWCAPS_READ_1_1_4
;
2404 spi_nor_set_read_settings(¶ms
->reads
[SNOR_CMD_READ_1_1_4
],
2405 0, 8, SPINOR_OP_READ_1_1_4
,
2409 /* Page Program settings. */
2410 params
->hwcaps
.mask
|= SNOR_HWCAPS_PP
;
2411 spi_nor_set_pp_settings(¶ms
->page_programs
[SNOR_CMD_PP
],
2412 SPINOR_OP_PP
, SNOR_PROTO_1_1_1
);
2414 /* Select the procedure to set the Quad Enable bit. */
2415 if (params
->hwcaps
.mask
& (SNOR_HWCAPS_READ_QUAD
|
2416 SNOR_HWCAPS_PP_QUAD
)) {
2417 switch (JEDEC_MFR(info
)) {
2418 case SNOR_MFR_MACRONIX
:
2419 params
->quad_enable
= macronix_quad_enable
;
2422 case SNOR_MFR_MICRON
:
2426 /* Kept only for backward compatibility purpose. */
2427 params
->quad_enable
= spansion_quad_enable
;
2432 /* Override the parameters with data read from SFDP tables. */
2433 nor
->addr_width
= 0;
2434 nor
->mtd
.erasesize
= 0;
2435 if ((info
->flags
& (SPI_NOR_DUAL_READ
| SPI_NOR_QUAD_READ
)) &&
2436 !(info
->flags
& SPI_NOR_SKIP_SFDP
)) {
2437 struct spi_nor_flash_parameter sfdp_params
;
2439 memcpy(&sfdp_params
, params
, sizeof(sfdp_params
));
2440 if (spi_nor_parse_sfdp(nor
, &sfdp_params
)) {
2441 nor
->addr_width
= 0;
2442 nor
->mtd
.erasesize
= 0;
2444 memcpy(params
, &sfdp_params
, sizeof(*params
));
2451 static int spi_nor_hwcaps2cmd(u32 hwcaps
, const int table
[][2], size_t size
)
2455 for (i
= 0; i
< size
; i
++)
2456 if (table
[i
][0] == (int)hwcaps
)
2462 static int spi_nor_hwcaps_read2cmd(u32 hwcaps
)
2464 static const int hwcaps_read2cmd
[][2] = {
2465 { SNOR_HWCAPS_READ
, SNOR_CMD_READ
},
2466 { SNOR_HWCAPS_READ_FAST
, SNOR_CMD_READ_FAST
},
2467 { SNOR_HWCAPS_READ_1_1_1_DTR
, SNOR_CMD_READ_1_1_1_DTR
},
2468 { SNOR_HWCAPS_READ_1_1_2
, SNOR_CMD_READ_1_1_2
},
2469 { SNOR_HWCAPS_READ_1_2_2
, SNOR_CMD_READ_1_2_2
},
2470 { SNOR_HWCAPS_READ_2_2_2
, SNOR_CMD_READ_2_2_2
},
2471 { SNOR_HWCAPS_READ_1_2_2_DTR
, SNOR_CMD_READ_1_2_2_DTR
},
2472 { SNOR_HWCAPS_READ_1_1_4
, SNOR_CMD_READ_1_1_4
},
2473 { SNOR_HWCAPS_READ_1_4_4
, SNOR_CMD_READ_1_4_4
},
2474 { SNOR_HWCAPS_READ_4_4_4
, SNOR_CMD_READ_4_4_4
},
2475 { SNOR_HWCAPS_READ_1_4_4_DTR
, SNOR_CMD_READ_1_4_4_DTR
},
2476 { SNOR_HWCAPS_READ_1_1_8
, SNOR_CMD_READ_1_1_8
},
2477 { SNOR_HWCAPS_READ_1_8_8
, SNOR_CMD_READ_1_8_8
},
2478 { SNOR_HWCAPS_READ_8_8_8
, SNOR_CMD_READ_8_8_8
},
2479 { SNOR_HWCAPS_READ_1_8_8_DTR
, SNOR_CMD_READ_1_8_8_DTR
},
2482 return spi_nor_hwcaps2cmd(hwcaps
, hwcaps_read2cmd
,
2483 ARRAY_SIZE(hwcaps_read2cmd
));
2486 static int spi_nor_hwcaps_pp2cmd(u32 hwcaps
)
2488 static const int hwcaps_pp2cmd
[][2] = {
2489 { SNOR_HWCAPS_PP
, SNOR_CMD_PP
},
2490 { SNOR_HWCAPS_PP_1_1_4
, SNOR_CMD_PP_1_1_4
},
2491 { SNOR_HWCAPS_PP_1_4_4
, SNOR_CMD_PP_1_4_4
},
2492 { SNOR_HWCAPS_PP_4_4_4
, SNOR_CMD_PP_4_4_4
},
2493 { SNOR_HWCAPS_PP_1_1_8
, SNOR_CMD_PP_1_1_8
},
2494 { SNOR_HWCAPS_PP_1_8_8
, SNOR_CMD_PP_1_8_8
},
2495 { SNOR_HWCAPS_PP_8_8_8
, SNOR_CMD_PP_8_8_8
},
2498 return spi_nor_hwcaps2cmd(hwcaps
, hwcaps_pp2cmd
,
2499 ARRAY_SIZE(hwcaps_pp2cmd
));
2502 static int spi_nor_select_read(struct spi_nor
*nor
,
2503 const struct spi_nor_flash_parameter
*params
,
2506 int cmd
, best_match
= fls(shared_hwcaps
& SNOR_HWCAPS_READ_MASK
) - 1;
2507 const struct spi_nor_read_command
*read
;
2512 cmd
= spi_nor_hwcaps_read2cmd(BIT(best_match
));
2516 read
= ¶ms
->reads
[cmd
];
2517 nor
->read_opcode
= read
->opcode
;
2518 nor
->read_proto
= read
->proto
;
2521 * In the spi-nor framework, we don't need to make the difference
2522 * between mode clock cycles and wait state clock cycles.
2523 * Indeed, the value of the mode clock cycles is used by a QSPI
2524 * flash memory to know whether it should enter or leave its 0-4-4
2525 * (Continuous Read / XIP) mode.
2526 * eXecution In Place is out of the scope of the mtd sub-system.
2527 * Hence we choose to merge both mode and wait state clock cycles
2528 * into the so called dummy clock cycles.
2530 nor
->read_dummy
= read
->num_mode_clocks
+ read
->num_wait_states
;
2534 static int spi_nor_select_pp(struct spi_nor
*nor
,
2535 const struct spi_nor_flash_parameter
*params
,
2538 int cmd
, best_match
= fls(shared_hwcaps
& SNOR_HWCAPS_PP_MASK
) - 1;
2539 const struct spi_nor_pp_command
*pp
;
2544 cmd
= spi_nor_hwcaps_pp2cmd(BIT(best_match
));
2548 pp
= ¶ms
->page_programs
[cmd
];
2549 nor
->program_opcode
= pp
->opcode
;
2550 nor
->write_proto
= pp
->proto
;
2554 static int spi_nor_select_erase(struct spi_nor
*nor
,
2555 const struct flash_info
*info
)
2557 struct mtd_info
*mtd
= &nor
->mtd
;
2559 /* Do nothing if already configured from SFDP. */
2563 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
2564 /* prefer "small sector" erase if possible */
2565 if (info
->flags
& SECT_4K
) {
2566 nor
->erase_opcode
= SPINOR_OP_BE_4K
;
2567 mtd
->erasesize
= 4096;
2568 } else if (info
->flags
& SECT_4K_PMC
) {
2569 nor
->erase_opcode
= SPINOR_OP_BE_4K_PMC
;
2570 mtd
->erasesize
= 4096;
2574 nor
->erase_opcode
= SPINOR_OP_SE
;
2575 mtd
->erasesize
= info
->sector_size
;
2580 static int spi_nor_setup(struct spi_nor
*nor
, const struct flash_info
*info
,
2581 const struct spi_nor_flash_parameter
*params
,
2582 const struct spi_nor_hwcaps
*hwcaps
)
2584 u32 ignored_mask
, shared_mask
;
2585 bool enable_quad_io
;
2589 * Keep only the hardware capabilities supported by both the SPI
2590 * controller and the SPI flash memory.
2592 shared_mask
= hwcaps
->mask
& params
->hwcaps
.mask
;
2594 /* SPI n-n-n protocols are not supported yet. */
2595 ignored_mask
= (SNOR_HWCAPS_READ_2_2_2
|
2596 SNOR_HWCAPS_READ_4_4_4
|
2597 SNOR_HWCAPS_READ_8_8_8
|
2598 SNOR_HWCAPS_PP_4_4_4
|
2599 SNOR_HWCAPS_PP_8_8_8
);
2600 if (shared_mask
& ignored_mask
) {
2602 "SPI n-n-n protocols are not supported yet.\n");
2603 shared_mask
&= ~ignored_mask
;
2606 /* Select the (Fast) Read command. */
2607 err
= spi_nor_select_read(nor
, params
, shared_mask
);
2610 "can't select read settings supported by both the SPI controller and memory.\n");
2614 /* Select the Page Program command. */
2615 err
= spi_nor_select_pp(nor
, params
, shared_mask
);
2618 "can't select write settings supported by both the SPI controller and memory.\n");
2622 /* Select the Sector Erase command. */
2623 err
= spi_nor_select_erase(nor
, info
);
2626 "can't select erase settings supported by both the SPI controller and memory.\n");
2630 /* Enable Quad I/O if needed. */
2631 enable_quad_io
= (spi_nor_get_protocol_width(nor
->read_proto
) == 4 ||
2632 spi_nor_get_protocol_width(nor
->write_proto
) == 4);
2633 if (enable_quad_io
&& params
->quad_enable
) {
2634 err
= params
->quad_enable(nor
);
2636 dev_err(nor
->dev
, "quad mode not supported\n");
2644 int spi_nor_scan(struct spi_nor
*nor
, const char *name
,
2645 const struct spi_nor_hwcaps
*hwcaps
)
2647 struct spi_nor_flash_parameter params
;
2648 const struct flash_info
*info
= NULL
;
2649 struct device
*dev
= nor
->dev
;
2650 struct mtd_info
*mtd
= &nor
->mtd
;
2651 struct device_node
*np
= spi_nor_get_flash_node(nor
);
2655 ret
= spi_nor_check(nor
);
2659 /* Reset SPI protocol for all commands. */
2660 nor
->reg_proto
= SNOR_PROTO_1_1_1
;
2661 nor
->read_proto
= SNOR_PROTO_1_1_1
;
2662 nor
->write_proto
= SNOR_PROTO_1_1_1
;
2665 info
= spi_nor_match_id(name
);
2666 /* Try to auto-detect if chip name wasn't specified or not found */
2668 info
= spi_nor_read_id(nor
);
2669 if (IS_ERR_OR_NULL(info
))
2673 * If caller has specified name of flash model that can normally be
2674 * detected using JEDEC, let's verify it.
2676 if (name
&& info
->id_len
) {
2677 const struct flash_info
*jinfo
;
2679 jinfo
= spi_nor_read_id(nor
);
2680 if (IS_ERR(jinfo
)) {
2681 return PTR_ERR(jinfo
);
2682 } else if (jinfo
!= info
) {
2684 * JEDEC knows better, so overwrite platform ID. We
2685 * can't trust partitions any longer, but we'll let
2686 * mtd apply them anyway, since some partitions may be
2687 * marked read-only, and we don't want to lose that
2688 * information, even if it's not 100% accurate.
2690 dev_warn(dev
, "found %s, expected %s\n",
2691 jinfo
->name
, info
->name
);
2696 mutex_init(&nor
->lock
);
2699 * Make sure the XSR_RDY flag is set before calling
2700 * spi_nor_wait_till_ready(). Xilinx S3AN share MFR
2701 * with Atmel spi-nor
2703 if (info
->flags
& SPI_S3AN
)
2704 nor
->flags
|= SNOR_F_READY_XSR_RDY
;
2706 /* Parse the Serial Flash Discoverable Parameters table. */
2707 ret
= spi_nor_init_params(nor
, info
, ¶ms
);
2712 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
2713 * with the software protection bits set
2716 if (JEDEC_MFR(info
) == SNOR_MFR_ATMEL
||
2717 JEDEC_MFR(info
) == SNOR_MFR_INTEL
||
2718 JEDEC_MFR(info
) == SNOR_MFR_SST
||
2719 info
->flags
& SPI_NOR_HAS_LOCK
) {
2722 spi_nor_wait_till_ready(nor
);
2726 mtd
->name
= dev_name(dev
);
2728 mtd
->type
= MTD_NORFLASH
;
2730 mtd
->flags
= MTD_CAP_NORFLASH
;
2731 mtd
->size
= params
.size
;
2732 mtd
->_erase
= spi_nor_erase
;
2733 mtd
->_read
= spi_nor_read
;
2735 /* NOR protection support for STmicro/Micron chips and similar */
2736 if (JEDEC_MFR(info
) == SNOR_MFR_MICRON
||
2737 info
->flags
& SPI_NOR_HAS_LOCK
) {
2738 nor
->flash_lock
= stm_lock
;
2739 nor
->flash_unlock
= stm_unlock
;
2740 nor
->flash_is_locked
= stm_is_locked
;
2743 if (nor
->flash_lock
&& nor
->flash_unlock
&& nor
->flash_is_locked
) {
2744 mtd
->_lock
= spi_nor_lock
;
2745 mtd
->_unlock
= spi_nor_unlock
;
2746 mtd
->_is_locked
= spi_nor_is_locked
;
2749 /* sst nor chips use AAI word program */
2750 if (info
->flags
& SST_WRITE
)
2751 mtd
->_write
= sst_write
;
2753 mtd
->_write
= spi_nor_write
;
2755 if (info
->flags
& USE_FSR
)
2756 nor
->flags
|= SNOR_F_USE_FSR
;
2757 if (info
->flags
& SPI_NOR_HAS_TB
)
2758 nor
->flags
|= SNOR_F_HAS_SR_TB
;
2759 if (info
->flags
& NO_CHIP_ERASE
)
2760 nor
->flags
|= SNOR_F_NO_OP_CHIP_ERASE
;
2761 if (info
->flags
& USE_CLSR
)
2762 nor
->flags
|= SNOR_F_USE_CLSR
;
2764 if (info
->flags
& SPI_NOR_NO_ERASE
)
2765 mtd
->flags
|= MTD_NO_ERASE
;
2767 mtd
->dev
.parent
= dev
;
2768 nor
->page_size
= params
.page_size
;
2769 mtd
->writebufsize
= nor
->page_size
;
2772 /* If we were instantiated by DT, use it */
2773 if (of_property_read_bool(np
, "m25p,fast-read"))
2774 params
.hwcaps
.mask
|= SNOR_HWCAPS_READ_FAST
;
2776 params
.hwcaps
.mask
&= ~SNOR_HWCAPS_READ_FAST
;
2778 /* If we weren't instantiated by DT, default to fast-read */
2779 params
.hwcaps
.mask
|= SNOR_HWCAPS_READ_FAST
;
2782 /* Some devices cannot do fast-read, no matter what DT tells us */
2783 if (info
->flags
& SPI_NOR_NO_FR
)
2784 params
.hwcaps
.mask
&= ~SNOR_HWCAPS_READ_FAST
;
2787 * Configure the SPI memory:
2788 * - select op codes for (Fast) Read, Page Program and Sector Erase.
2789 * - set the number of dummy cycles (mode cycles + wait states).
2790 * - set the SPI protocols for register and memory accesses.
2791 * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
2793 ret
= spi_nor_setup(nor
, info
, ¶ms
, hwcaps
);
2797 if (nor
->addr_width
) {
2798 /* already configured from SFDP */
2799 } else if (info
->addr_width
) {
2800 nor
->addr_width
= info
->addr_width
;
2801 } else if (mtd
->size
> 0x1000000) {
2802 /* enable 4-byte addressing if the device exceeds 16MiB */
2803 nor
->addr_width
= 4;
2804 if (JEDEC_MFR(info
) == SNOR_MFR_SPANSION
||
2805 info
->flags
& SPI_NOR_4B_OPCODES
)
2806 spi_nor_set_4byte_opcodes(nor
, info
);
2808 set_4byte(nor
, info
, 1);
2810 nor
->addr_width
= 3;
2813 if (nor
->addr_width
> SPI_NOR_MAX_ADDR_WIDTH
) {
2814 dev_err(dev
, "address width is too large: %u\n",
2819 if (info
->flags
& SPI_S3AN
) {
2820 ret
= s3an_nor_scan(info
, nor
);
2825 dev_info(dev
, "%s (%lld Kbytes)\n", info
->name
,
2826 (long long)mtd
->size
>> 10);
2829 "mtd .name = %s, .size = 0x%llx (%lldMiB), "
2830 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
2831 mtd
->name
, (long long)mtd
->size
, (long long)(mtd
->size
>> 20),
2832 mtd
->erasesize
, mtd
->erasesize
/ 1024, mtd
->numeraseregions
);
2834 if (mtd
->numeraseregions
)
2835 for (i
= 0; i
< mtd
->numeraseregions
; i
++)
2837 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
2838 ".erasesize = 0x%.8x (%uKiB), "
2839 ".numblocks = %d }\n",
2840 i
, (long long)mtd
->eraseregions
[i
].offset
,
2841 mtd
->eraseregions
[i
].erasesize
,
2842 mtd
->eraseregions
[i
].erasesize
/ 1024,
2843 mtd
->eraseregions
[i
].numblocks
);
2846 EXPORT_SYMBOL_GPL(spi_nor_scan
);
2848 static const struct flash_info
*spi_nor_match_id(const char *name
)
2850 const struct flash_info
*id
= spi_nor_ids
;
2853 if (!strcmp(name
, id
->name
))
2860 MODULE_LICENSE("GPL");
2861 MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
2862 MODULE_AUTHOR("Mike Lavender");
2863 MODULE_DESCRIPTION("framework for SPI NOR");