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) },
1096 /* ST Microelectronics -- newer production may have feature updates */
1097 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
1098 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
1099 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
1100 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
1101 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
1102 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
1103 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
1104 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
1105 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
1107 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
1108 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
1109 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
1110 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
1111 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
1112 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
1113 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
1114 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
1115 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
1117 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
1118 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
1119 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
1121 { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
1122 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
1123 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
1125 { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
1126 { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
1127 { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
1128 { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
1129 { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
1130 { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
1132 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
1133 { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
1134 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
1135 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
1136 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
1137 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
1138 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
1139 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
1140 { "w25q20cl", INFO(0xef4012, 0, 64 * 1024, 4, SECT_4K) },
1141 { "w25q20bw", INFO(0xef5012, 0, 64 * 1024, 4, SECT_4K) },
1142 { "w25q20ew", INFO(0xef6012, 0, 64 * 1024, 4, SECT_4K) },
1143 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
1145 "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64,
1146 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1147 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1149 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
1150 { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
1152 "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128,
1153 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1154 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1157 "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256,
1158 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1159 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1161 { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
1162 { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
1163 { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
1164 { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1165 { "w25m512jv", INFO(0xef7119, 0, 64 * 1024, 1024,
1166 SECT_4K | SPI_NOR_QUAD_READ | SPI_NOR_DUAL_READ) },
1168 /* Catalyst / On Semiconductor -- non-JEDEC */
1169 { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1170 { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1171 { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1172 { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1173 { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1175 /* Xilinx S3AN Internal Flash */
1176 { "3S50AN", S3AN_INFO(0x1f2200, 64, 264) },
1177 { "3S200AN", S3AN_INFO(0x1f2400, 256, 264) },
1178 { "3S400AN", S3AN_INFO(0x1f2400, 256, 264) },
1179 { "3S700AN", S3AN_INFO(0x1f2500, 512, 264) },
1180 { "3S1400AN", S3AN_INFO(0x1f2600, 512, 528) },
1184 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
1187 u8 id[SPI_NOR_MAX_ID_LEN];
1188 const struct flash_info *info;
1190 tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
1192 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
1193 return ERR_PTR(tmp);
1196 for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
1197 info = &spi_nor_ids[tmp];
1199 if (!memcmp(info->id, id, info->id_len))
1200 return &spi_nor_ids[tmp];
1203 dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
1204 id[0], id[1], id[2]);
1205 return ERR_PTR(-ENODEV);
1208 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
1209 size_t *retlen, u_char *buf)
1211 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1214 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
1216 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ);
1223 if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
1224 addr = spi_nor_s3an_addr_convert(nor, addr);
1226 ret = nor->read(nor, addr, len, buf);
1228 /* We shouldn't see 0-length reads */
1244 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
1248 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
1249 size_t *retlen, const u_char *buf)
1251 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1255 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1257 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
1263 nor->sst_write_second = false;
1266 /* Start write from odd address. */
1268 nor->program_opcode = SPINOR_OP_BP;
1270 /* write one byte. */
1271 ret = nor->write(nor, to, 1, buf);
1274 WARN(ret != 1, "While writing 1 byte written %i bytes\n",
1276 ret = spi_nor_wait_till_ready(nor);
1282 /* Write out most of the data here. */
1283 for (; actual < len - 1; actual += 2) {
1284 nor->program_opcode = SPINOR_OP_AAI_WP;
1286 /* write two bytes. */
1287 ret = nor->write(nor, to, 2, buf + actual);
1290 WARN(ret != 2, "While writing 2 bytes written %i bytes\n",
1292 ret = spi_nor_wait_till_ready(nor);
1296 nor->sst_write_second = true;
1298 nor->sst_write_second = false;
1301 ret = spi_nor_wait_till_ready(nor);
1305 /* Write out trailing byte if it exists. */
1306 if (actual != len) {
1309 nor->program_opcode = SPINOR_OP_BP;
1310 ret = nor->write(nor, to, 1, buf + actual);
1313 WARN(ret != 1, "While writing 1 byte written %i bytes\n",
1315 ret = spi_nor_wait_till_ready(nor);
1323 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
1328 * Write an address range to the nor chip. Data must be written in
1329 * FLASH_PAGESIZE chunks. The address range may be any size provided
1330 * it is within the physical boundaries.
1332 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
1333 size_t *retlen, const u_char *buf)
1335 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1336 size_t page_offset, page_remain, i;
1339 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1341 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
1345 for (i = 0; i < len; ) {
1347 loff_t addr = to + i;
1350 * If page_size is a power of two, the offset can be quickly
1351 * calculated with an AND operation. On the other cases we
1352 * need to do a modulus operation (more expensive).
1353 * Power of two numbers have only one bit set and we can use
1354 * the instruction hweight32 to detect if we need to do a
1355 * modulus (do_div()) or not.
1357 if (hweight32(nor->page_size) == 1) {
1358 page_offset = addr & (nor->page_size - 1);
1360 uint64_t aux = addr;
1362 page_offset = do_div(aux, nor->page_size);
1364 /* the size of data remaining on the first page */
1365 page_remain = min_t(size_t,
1366 nor->page_size - page_offset, len - i);
1368 if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
1369 addr = spi_nor_s3an_addr_convert(nor, addr);
1372 ret = nor->write(nor, addr, page_remain, buf + i);
1377 ret = spi_nor_wait_till_ready(nor);
1382 if (written != page_remain) {
1384 "While writing %zu bytes written %zd bytes\n",
1385 page_remain, written);
1392 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
1397 * macronix_quad_enable() - set QE bit in Status Register.
1398 * @nor: pointer to a 'struct spi_nor'
1400 * Set the Quad Enable (QE) bit in the Status Register.
1402 * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
1404 * Return: 0 on success, -errno otherwise.
1406 static int macronix_quad_enable(struct spi_nor *nor)
1413 if (val & SR_QUAD_EN_MX)
1418 write_sr(nor, val | SR_QUAD_EN_MX);
1420 ret = spi_nor_wait_till_ready(nor);
1425 if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
1426 dev_err(nor->dev, "Macronix Quad bit not set\n");
1434 * Write status Register and configuration register with 2 bytes
1435 * The first byte will be written to the status register, while the
1436 * second byte will be written to the configuration register.
1437 * Return negative if error occurred.
1439 static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
1445 ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
1448 "error while writing configuration register\n");
1452 ret = spi_nor_wait_till_ready(nor);
1455 "timeout while writing configuration register\n");
1463 * spansion_quad_enable() - set QE bit in Configuraiton Register.
1464 * @nor: pointer to a 'struct spi_nor'
1466 * Set the Quad Enable (QE) bit in the Configuration Register.
1467 * This function is kept for legacy purpose because it has been used for a
1468 * long time without anybody complaining but it should be considered as
1469 * deprecated and maybe buggy.
1470 * First, this function doesn't care about the previous values of the Status
1471 * and Configuration Registers when it sets the QE bit (bit 1) in the
1472 * Configuration Register: all other bits are cleared, which may have unwanted
1473 * side effects like removing some block protections.
1474 * Secondly, it uses the Read Configuration Register (35h) instruction though
1475 * some very old and few memories don't support this instruction. If a pull-up
1476 * resistor is present on the MISO/IO1 line, we might still be able to pass the
1477 * "read back" test because the QSPI memory doesn't recognize the command,
1478 * so leaves the MISO/IO1 line state unchanged, hence read_cr() returns 0xFF.
1480 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1483 * Return: 0 on success, -errno otherwise.
1485 static int spansion_quad_enable(struct spi_nor *nor)
1487 u8 sr_cr[2] = {0, CR_QUAD_EN_SPAN};
1490 ret = write_sr_cr(nor, sr_cr);
1494 /* read back and check it */
1496 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
1497 dev_err(nor->dev, "Spansion Quad bit not set\n");
1505 * spansion_no_read_cr_quad_enable() - set QE bit in Configuration Register.
1506 * @nor: pointer to a 'struct spi_nor'
1508 * Set the Quad Enable (QE) bit in the Configuration Register.
1509 * This function should be used with QSPI memories not supporting the Read
1510 * Configuration Register (35h) instruction.
1512 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1515 * Return: 0 on success, -errno otherwise.
1517 static int spansion_no_read_cr_quad_enable(struct spi_nor *nor)
1522 /* Keep the current value of the Status Register. */
1525 dev_err(nor->dev, "error while reading status register\n");
1529 sr_cr[1] = CR_QUAD_EN_SPAN;
1531 return write_sr_cr(nor, sr_cr);
1535 * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
1536 * @nor: pointer to a 'struct spi_nor'
1538 * Set the Quad Enable (QE) bit in the Configuration Register.
1539 * This function should be used with QSPI memories supporting the Read
1540 * Configuration Register (35h) instruction.
1542 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1545 * Return: 0 on success, -errno otherwise.
1547 static int spansion_read_cr_quad_enable(struct spi_nor *nor)
1549 struct device *dev = nor->dev;
1553 /* Check current Quad Enable bit value. */
1556 dev_err(dev, "error while reading configuration register\n");
1560 if (ret & CR_QUAD_EN_SPAN)
1563 sr_cr[1] = ret | CR_QUAD_EN_SPAN;
1565 /* Keep the current value of the Status Register. */
1568 dev_err(dev, "error while reading status register\n");
1573 ret = write_sr_cr(nor, sr_cr);
1577 /* Read back and check it. */
1579 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
1580 dev_err(nor->dev, "Spansion Quad bit not set\n");
1588 * sr2_bit7_quad_enable() - set QE bit in Status Register 2.
1589 * @nor: pointer to a 'struct spi_nor'
1591 * Set the Quad Enable (QE) bit in the Status Register 2.
1593 * This is one of the procedures to set the QE bit described in the SFDP
1594 * (JESD216 rev B) specification but no manufacturer using this procedure has
1595 * been identified yet, hence the name of the function.
1597 * Return: 0 on success, -errno otherwise.
1599 static int sr2_bit7_quad_enable(struct spi_nor *nor)
1604 /* Check current Quad Enable bit value. */
1605 ret = nor->read_reg(nor, SPINOR_OP_RDSR2, &sr2, 1);
1608 if (sr2 & SR2_QUAD_EN_BIT7)
1611 /* Update the Quad Enable bit. */
1612 sr2 |= SR2_QUAD_EN_BIT7;
1616 ret = nor->write_reg(nor, SPINOR_OP_WRSR2, &sr2, 1);
1618 dev_err(nor->dev, "error while writing status register 2\n");
1622 ret = spi_nor_wait_till_ready(nor);
1624 dev_err(nor->dev, "timeout while writing status register 2\n");
1628 /* Read back and check it. */
1629 ret = nor->read_reg(nor, SPINOR_OP_RDSR2, &sr2, 1);
1630 if (!(ret > 0 && (sr2 & SR2_QUAD_EN_BIT7))) {
1631 dev_err(nor->dev, "SR2 Quad bit not set\n");
1638 static int spi_nor_check(struct spi_nor *nor)
1640 if (!nor->dev || !nor->read || !nor->write ||
1641 !nor->read_reg || !nor->write_reg) {
1642 pr_err("spi-nor: please fill all the necessary fields!\n");
1649 static int s3an_nor_scan(const struct flash_info *info, struct spi_nor *nor)
1654 ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);
1656 dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret);
1660 nor->erase_opcode = SPINOR_OP_XSE;
1661 nor->program_opcode = SPINOR_OP_XPP;
1662 nor->read_opcode = SPINOR_OP_READ;
1663 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
1666 * This flashes have a page size of 264 or 528 bytes (known as
1667 * Default addressing mode). It can be changed to a more standard
1668 * Power of two mode where the page size is 256/512. This comes
1669 * with a price: there is 3% less of space, the data is corrupted
1670 * and the page size cannot be changed back to default addressing
1673 * The current addressing mode can be read from the XRDSR register
1674 * and should not be changed, because is a destructive operation.
1676 if (val & XSR_PAGESIZE) {
1677 /* Flash in Power of 2 mode */
1678 nor->page_size = (nor->page_size == 264) ? 256 : 512;
1679 nor->mtd.writebufsize = nor->page_size;
1680 nor->mtd.size = 8 * nor->page_size * info->n_sectors;
1681 nor->mtd.erasesize = 8 * nor->page_size;
1683 /* Flash in Default addressing mode */
1684 nor->flags |= SNOR_F_S3AN_ADDR_DEFAULT;
1690 struct spi_nor_read_command {
1694 enum spi_nor_protocol proto;
1697 struct spi_nor_pp_command {
1699 enum spi_nor_protocol proto;
1702 enum spi_nor_read_command_index {
1705 SNOR_CMD_READ_1_1_1_DTR,
1708 SNOR_CMD_READ_1_1_2,
1709 SNOR_CMD_READ_1_2_2,
1710 SNOR_CMD_READ_2_2_2,
1711 SNOR_CMD_READ_1_2_2_DTR,
1714 SNOR_CMD_READ_1_1_4,
1715 SNOR_CMD_READ_1_4_4,
1716 SNOR_CMD_READ_4_4_4,
1717 SNOR_CMD_READ_1_4_4_DTR,
1720 SNOR_CMD_READ_1_1_8,
1721 SNOR_CMD_READ_1_8_8,
1722 SNOR_CMD_READ_8_8_8,
1723 SNOR_CMD_READ_1_8_8_DTR,
1728 enum spi_nor_pp_command_index {
1744 struct spi_nor_flash_parameter {
1748 struct spi_nor_hwcaps hwcaps;
1749 struct spi_nor_read_command reads[SNOR_CMD_READ_MAX];
1750 struct spi_nor_pp_command page_programs[SNOR_CMD_PP_MAX];
1752 int (*quad_enable)(struct spi_nor *nor);
1756 spi_nor_set_read_settings(struct spi_nor_read_command *read,
1760 enum spi_nor_protocol proto)
1762 read->num_mode_clocks = num_mode_clocks;
1763 read->num_wait_states = num_wait_states;
1764 read->opcode = opcode;
1765 read->proto = proto;
1769 spi_nor_set_pp_settings(struct spi_nor_pp_command *pp,
1771 enum spi_nor_protocol proto)
1773 pp->opcode = opcode;
1778 * Serial Flash Discoverable Parameters (SFDP) parsing.
1782 * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
1783 * @nor: pointer to a 'struct spi_nor'
1784 * @addr: offset in the SFDP area to start reading data from
1785 * @len: number of bytes to read
1786 * @buf: buffer where the SFDP data are copied into
1788 * Whatever the actual numbers of bytes for address and dummy cycles are
1789 * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
1790 * followed by a 3-byte address and 8 dummy clock cycles.
1792 * Return: 0 on success, -errno otherwise.
1794 static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr,
1795 size_t len, void *buf)
1797 u8 addr_width, read_opcode, read_dummy;
1800 read_opcode = nor->read_opcode;
1801 addr_width = nor->addr_width;
1802 read_dummy = nor->read_dummy;
1804 nor->read_opcode = SPINOR_OP_RDSFDP;
1805 nor->addr_width = 3;
1806 nor->read_dummy = 8;
1809 ret = nor->read(nor, addr, len, (u8 *)buf);
1810 if (!ret || ret > len) {
1824 nor->read_opcode = read_opcode;
1825 nor->addr_width = addr_width;
1826 nor->read_dummy = read_dummy;
1831 struct sfdp_parameter_header {
1835 u8 length; /* in double words */
1836 u8 parameter_table_pointer[3]; /* byte address */
1840 #define SFDP_PARAM_HEADER_ID(p) (((p)->id_msb << 8) | (p)->id_lsb)
1841 #define SFDP_PARAM_HEADER_PTP(p) \
1842 (((p)->parameter_table_pointer[2] << 16) | \
1843 ((p)->parameter_table_pointer[1] << 8) | \
1844 ((p)->parameter_table_pointer[0] << 0))
1846 #define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
1847 #define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
1849 #define SFDP_SIGNATURE 0x50444653U
1850 #define SFDP_JESD216_MAJOR 1
1851 #define SFDP_JESD216_MINOR 0
1852 #define SFDP_JESD216A_MINOR 5
1853 #define SFDP_JESD216B_MINOR 6
1855 struct sfdp_header {
1856 u32 signature; /* Ox50444653U <=> "SFDP" */
1859 u8 nph; /* 0-base number of parameter headers */
1862 /* Basic Flash Parameter Table. */
1863 struct sfdp_parameter_header bfpt_header;
1866 /* Basic Flash Parameter Table */
1869 * JESD216 rev B defines a Basic Flash Parameter Table of 16 DWORDs.
1870 * They are indexed from 1 but C arrays are indexed from 0.
1872 #define BFPT_DWORD(i) ((i) - 1)
1873 #define BFPT_DWORD_MAX 16
1875 /* The first version of JESB216 defined only 9 DWORDs. */
1876 #define BFPT_DWORD_MAX_JESD216 9
1879 #define BFPT_DWORD1_FAST_READ_1_1_2 BIT(16)
1880 #define BFPT_DWORD1_ADDRESS_BYTES_MASK GENMASK(18, 17)
1881 #define BFPT_DWORD1_ADDRESS_BYTES_3_ONLY (0x0UL << 17)
1882 #define BFPT_DWORD1_ADDRESS_BYTES_3_OR_4 (0x1UL << 17)
1883 #define BFPT_DWORD1_ADDRESS_BYTES_4_ONLY (0x2UL << 17)
1884 #define BFPT_DWORD1_DTR BIT(19)
1885 #define BFPT_DWORD1_FAST_READ_1_2_2 BIT(20)
1886 #define BFPT_DWORD1_FAST_READ_1_4_4 BIT(21)
1887 #define BFPT_DWORD1_FAST_READ_1_1_4 BIT(22)
1890 #define BFPT_DWORD5_FAST_READ_2_2_2 BIT(0)
1891 #define BFPT_DWORD5_FAST_READ_4_4_4 BIT(4)
1894 #define BFPT_DWORD11_PAGE_SIZE_SHIFT 4
1895 #define BFPT_DWORD11_PAGE_SIZE_MASK GENMASK(7, 4)
1900 * (from JESD216 rev B)
1901 * Quad Enable Requirements (QER):
1902 * - 000b: Device does not have a QE bit. Device detects 1-1-4 and 1-4-4
1903 * reads based on instruction. DQ3/HOLD# functions are hold during
1904 * instruction phase.
1905 * - 001b: QE is bit 1 of status register 2. It is set via Write Status with
1906 * two data bytes where bit 1 of the second byte is one.
1908 * Writing only one byte to the status register has the side-effect of
1909 * clearing status register 2, including the QE bit. The 100b code is
1910 * used if writing one byte to the status register does not modify
1911 * status register 2.
1912 * - 010b: QE is bit 6 of status register 1. It is set via Write Status with
1913 * one data byte where bit 6 is one.
1915 * - 011b: QE is bit 7 of status register 2. It is set via Write status
1916 * register 2 instruction 3Eh with one data byte where bit 7 is one.
1918 * The status register 2 is read using instruction 3Fh.
1919 * - 100b: QE is bit 1 of status register 2. It is set via Write Status with
1920 * two data bytes where bit 1 of the second byte is one.
1922 * In contrast to the 001b code, writing one byte to the status
1923 * register does not modify status register 2.
1924 * - 101b: QE is bit 1 of status register 2. Status register 1 is read using
1925 * Read Status instruction 05h. Status register2 is read using
1926 * instruction 35h. QE is set via Writ Status instruction 01h with
1927 * two data bytes where bit 1 of the second byte is one.
1930 #define BFPT_DWORD15_QER_MASK GENMASK(22, 20)
1931 #define BFPT_DWORD15_QER_NONE (0x0UL << 20) /* Micron */
1932 #define BFPT_DWORD15_QER_SR2_BIT1_BUGGY (0x1UL << 20)
1933 #define BFPT_DWORD15_QER_SR1_BIT6 (0x2UL << 20) /* Macronix */
1934 #define BFPT_DWORD15_QER_SR2_BIT7 (0x3UL << 20)
1935 #define BFPT_DWORD15_QER_SR2_BIT1_NO_RD (0x4UL << 20)
1936 #define BFPT_DWORD15_QER_SR2_BIT1 (0x5UL << 20) /* Spansion */
1939 u32 dwords[BFPT_DWORD_MAX];
1942 /* Fast Read settings. */
1945 spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read,
1947 enum spi_nor_protocol proto)
1949 read->num_mode_clocks = (half >> 5) & 0x07;
1950 read->num_wait_states = (half >> 0) & 0x1f;
1951 read->opcode = (half >> 8) & 0xff;
1952 read->proto = proto;
1955 struct sfdp_bfpt_read {
1956 /* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
1960 * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us
1961 * whether the Fast Read x-y-z command is supported.
1963 u32 supported_dword;
1967 * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
1968 * encodes the op code, the number of mode clocks and the number of wait
1969 * states to be used by Fast Read x-y-z command.
1974 /* The SPI protocol for this Fast Read x-y-z command. */
1975 enum spi_nor_protocol proto;
1978 static const struct sfdp_bfpt_read sfdp_bfpt_reads[] = {
1979 /* Fast Read 1-1-2 */
1981 SNOR_HWCAPS_READ_1_1_2,
1982 BFPT_DWORD(1), BIT(16), /* Supported bit */
1983 BFPT_DWORD(4), 0, /* Settings */
1987 /* Fast Read 1-2-2 */
1989 SNOR_HWCAPS_READ_1_2_2,
1990 BFPT_DWORD(1), BIT(20), /* Supported bit */
1991 BFPT_DWORD(4), 16, /* Settings */
1995 /* Fast Read 2-2-2 */
1997 SNOR_HWCAPS_READ_2_2_2,
1998 BFPT_DWORD(5), BIT(0), /* Supported bit */
1999 BFPT_DWORD(6), 16, /* Settings */
2003 /* Fast Read 1-1-4 */
2005 SNOR_HWCAPS_READ_1_1_4,
2006 BFPT_DWORD(1), BIT(22), /* Supported bit */
2007 BFPT_DWORD(3), 16, /* Settings */
2011 /* Fast Read 1-4-4 */
2013 SNOR_HWCAPS_READ_1_4_4,
2014 BFPT_DWORD(1), BIT(21), /* Supported bit */
2015 BFPT_DWORD(3), 0, /* Settings */
2019 /* Fast Read 4-4-4 */
2021 SNOR_HWCAPS_READ_4_4_4,
2022 BFPT_DWORD(5), BIT(4), /* Supported bit */
2023 BFPT_DWORD(7), 16, /* Settings */
2028 struct sfdp_bfpt_erase {
2030 * The half-word at offset <shift> in DWORD <dwoard> encodes the
2031 * op code and erase sector size to be used by Sector Erase commands.
2037 static const struct sfdp_bfpt_erase sfdp_bfpt_erases[] = {
2038 /* Erase Type 1 in DWORD8 bits[15:0] */
2041 /* Erase Type 2 in DWORD8 bits[31:16] */
2042 {BFPT_DWORD(8), 16},
2044 /* Erase Type 3 in DWORD9 bits[15:0] */
2047 /* Erase Type 4 in DWORD9 bits[31:16] */
2048 {BFPT_DWORD(9), 16},
2051 static int spi_nor_hwcaps_read2cmd(u32 hwcaps);
2054 * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
2055 * @nor: pointer to a 'struct spi_nor'
2056 * @bfpt_header: pointer to the 'struct sfdp_parameter_header' describing
2057 * the Basic Flash Parameter Table length and version
2058 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
2061 * The Basic Flash Parameter Table is the main and only mandatory table as
2062 * defined by the SFDP (JESD216) specification.
2063 * It provides us with the total size (memory density) of the data array and
2064 * the number of address bytes for Fast Read, Page Program and Sector Erase
2066 * For Fast READ commands, it also gives the number of mode clock cycles and
2067 * wait states (regrouped in the number of dummy clock cycles) for each
2068 * supported instruction op code.
2069 * For Page Program, the page size is now available since JESD216 rev A, however
2070 * the supported instruction op codes are still not provided.
2071 * For Sector Erase commands, this table stores the supported instruction op
2072 * codes and the associated sector sizes.
2073 * Finally, the Quad Enable Requirements (QER) are also available since JESD216
2074 * rev A. The QER bits encode the manufacturer dependent procedure to be
2075 * executed to set the Quad Enable (QE) bit in some internal register of the
2076 * Quad SPI memory. Indeed the QE bit, when it exists, must be set before
2077 * sending any Quad SPI command to the memory. Actually, setting the QE bit
2078 * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2
2079 * and IO3 hence enabling 4 (Quad) I/O lines.
2081 * Return: 0 on success, -errno otherwise.
2083 static int spi_nor_parse_bfpt(struct spi_nor *nor,
2084 const struct sfdp_parameter_header *bfpt_header,
2085 struct spi_nor_flash_parameter *params)
2087 struct mtd_info *mtd = &nor->mtd;
2088 struct sfdp_bfpt bfpt;
2094 /* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */
2095 if (bfpt_header->length < BFPT_DWORD_MAX_JESD216)
2098 /* Read the Basic Flash Parameter Table. */
2099 len = min_t(size_t, sizeof(bfpt),
2100 bfpt_header->length * sizeof(u32));
2101 addr = SFDP_PARAM_HEADER_PTP(bfpt_header);
2102 memset(&bfpt, 0, sizeof(bfpt));
2103 err = spi_nor_read_sfdp(nor, addr, len, &bfpt);
2107 /* Fix endianness of the BFPT DWORDs. */
2108 for (i = 0; i < BFPT_DWORD_MAX; i++)
2109 bfpt.dwords[i] = le32_to_cpu(bfpt.dwords[i]);
2111 /* Number of address bytes. */
2112 switch (bfpt.dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) {
2113 case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY:
2114 nor->addr_width = 3;
2117 case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY:
2118 nor->addr_width = 4;
2125 /* Flash Memory Density (in bits). */
2126 params->size = bfpt.dwords[BFPT_DWORD(2)];
2127 if (params->size & BIT(31)) {
2128 params->size &= ~BIT(31);
2129 params->size = 1ULL << params->size;
2133 params->size >>= 3; /* Convert to bytes. */
2135 /* Fast Read settings. */
2136 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_reads); i++) {
2137 const struct sfdp_bfpt_read *rd = &sfdp_bfpt_reads[i];
2138 struct spi_nor_read_command *read;
2140 if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) {
2141 params->hwcaps.mask &= ~rd->hwcaps;
2145 params->hwcaps.mask |= rd->hwcaps;
2146 cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps);
2147 read = ¶ms->reads[cmd];
2148 half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift;
2149 spi_nor_set_read_settings_from_bfpt(read, half, rd->proto);
2152 /* Sector Erase settings. */
2153 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_erases); i++) {
2154 const struct sfdp_bfpt_erase *er = &sfdp_bfpt_erases[i];
2158 half = bfpt.dwords[er->dword] >> er->shift;
2159 erasesize = half & 0xff;
2161 /* erasesize == 0 means this Erase Type is not supported. */
2165 erasesize = 1U << erasesize;
2166 opcode = (half >> 8) & 0xff;
2167 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
2168 if (erasesize == SZ_4K) {
2169 nor->erase_opcode = opcode;
2170 mtd->erasesize = erasesize;
2174 if (!mtd->erasesize || mtd->erasesize < erasesize) {
2175 nor->erase_opcode = opcode;
2176 mtd->erasesize = erasesize;
2180 /* Stop here if not JESD216 rev A or later. */
2181 if (bfpt_header->length < BFPT_DWORD_MAX)
2184 /* Page size: this field specifies 'N' so the page size = 2^N bytes. */
2185 params->page_size = bfpt.dwords[BFPT_DWORD(11)];
2186 params->page_size &= BFPT_DWORD11_PAGE_SIZE_MASK;
2187 params->page_size >>= BFPT_DWORD11_PAGE_SIZE_SHIFT;
2188 params->page_size = 1U << params->page_size;
2190 /* Quad Enable Requirements. */
2191 switch (bfpt.dwords[BFPT_DWORD(15)] & BFPT_DWORD15_QER_MASK) {
2192 case BFPT_DWORD15_QER_NONE:
2193 params->quad_enable = NULL;
2196 case BFPT_DWORD15_QER_SR2_BIT1_BUGGY:
2197 case BFPT_DWORD15_QER_SR2_BIT1_NO_RD:
2198 params->quad_enable = spansion_no_read_cr_quad_enable;
2201 case BFPT_DWORD15_QER_SR1_BIT6:
2202 params->quad_enable = macronix_quad_enable;
2205 case BFPT_DWORD15_QER_SR2_BIT7:
2206 params->quad_enable = sr2_bit7_quad_enable;
2209 case BFPT_DWORD15_QER_SR2_BIT1:
2210 params->quad_enable = spansion_read_cr_quad_enable;
2221 * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
2222 * @nor: pointer to a 'struct spi_nor'
2223 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
2226 * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
2227 * specification. This is a standard which tends to supported by almost all
2228 * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
2229 * runtime the main parameters needed to perform basic SPI flash operations such
2230 * as Fast Read, Page Program or Sector Erase commands.
2232 * Return: 0 on success, -errno otherwise.
2234 static int spi_nor_parse_sfdp(struct spi_nor *nor,
2235 struct spi_nor_flash_parameter *params)
2237 const struct sfdp_parameter_header *param_header, *bfpt_header;
2238 struct sfdp_parameter_header *param_headers = NULL;
2239 struct sfdp_header header;
2240 struct device *dev = nor->dev;
2244 /* Get the SFDP header. */
2245 err = spi_nor_read_sfdp(nor, 0, sizeof(header), &header);
2249 /* Check the SFDP header version. */
2250 if (le32_to_cpu(header.signature) != SFDP_SIGNATURE ||
2251 header.major != SFDP_JESD216_MAJOR ||
2252 header.minor < SFDP_JESD216_MINOR)
2256 * Verify that the first and only mandatory parameter header is a
2257 * Basic Flash Parameter Table header as specified in JESD216.
2259 bfpt_header = &header.bfpt_header;
2260 if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID ||
2261 bfpt_header->major != SFDP_JESD216_MAJOR)
2265 * Allocate memory then read all parameter headers with a single
2266 * Read SFDP command. These parameter headers will actually be parsed
2267 * twice: a first time to get the latest revision of the basic flash
2268 * parameter table, then a second time to handle the supported optional
2270 * Hence we read the parameter headers once for all to reduce the
2271 * processing time. Also we use kmalloc() instead of devm_kmalloc()
2272 * because we don't need to keep these parameter headers: the allocated
2273 * memory is always released with kfree() before exiting this function.
2276 psize = header.nph * sizeof(*param_headers);
2278 param_headers = kmalloc(psize, GFP_KERNEL);
2282 err = spi_nor_read_sfdp(nor, sizeof(header),
2283 psize, param_headers);
2285 dev_err(dev, "failed to read SFDP parameter headers\n");
2291 * Check other parameter headers to get the latest revision of
2292 * the basic flash parameter table.
2294 for (i = 0; i < header.nph; i++) {
2295 param_header = ¶m_headers[i];
2297 if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID &&
2298 param_header->major == SFDP_JESD216_MAJOR &&
2299 (param_header->minor > bfpt_header->minor ||
2300 (param_header->minor == bfpt_header->minor &&
2301 param_header->length > bfpt_header->length)))
2302 bfpt_header = param_header;
2305 err = spi_nor_parse_bfpt(nor, bfpt_header, params);
2309 /* Parse other parameter headers. */
2310 for (i = 0; i < header.nph; i++) {
2311 param_header = ¶m_headers[i];
2313 switch (SFDP_PARAM_HEADER_ID(param_header)) {
2314 case SFDP_SECTOR_MAP_ID:
2315 dev_info(dev, "non-uniform erase sector maps are not supported yet.\n");
2327 kfree(param_headers);
2331 static int spi_nor_init_params(struct spi_nor *nor,
2332 const struct flash_info *info,
2333 struct spi_nor_flash_parameter *params)
2335 /* Set legacy flash parameters as default. */
2336 memset(params, 0, sizeof(*params));
2338 /* Set SPI NOR sizes. */
2339 params->size = info->sector_size * info->n_sectors;
2340 params->page_size = info->page_size;
2342 /* (Fast) Read settings. */
2343 params->hwcaps.mask |= SNOR_HWCAPS_READ;
2344 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ],
2345 0, 0, SPINOR_OP_READ,
2348 if (!(info->flags & SPI_NOR_NO_FR)) {
2349 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
2350 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_FAST],
2351 0, 8, SPINOR_OP_READ_FAST,
2355 if (info->flags & SPI_NOR_DUAL_READ) {
2356 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
2357 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_2],
2358 0, 8, SPINOR_OP_READ_1_1_2,
2362 if (info->flags & SPI_NOR_QUAD_READ) {
2363 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
2364 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_4],
2365 0, 8, SPINOR_OP_READ_1_1_4,
2369 /* Page Program settings. */
2370 params->hwcaps.mask |= SNOR_HWCAPS_PP;
2371 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP],
2372 SPINOR_OP_PP, SNOR_PROTO_1_1_1);
2374 /* Select the procedure to set the Quad Enable bit. */
2375 if (params->hwcaps.mask & (SNOR_HWCAPS_READ_QUAD |
2376 SNOR_HWCAPS_PP_QUAD)) {
2377 switch (JEDEC_MFR(info)) {
2378 case SNOR_MFR_MACRONIX:
2379 params->quad_enable = macronix_quad_enable;
2382 case SNOR_MFR_MICRON:
2386 /* Kept only for backward compatibility purpose. */
2387 params->quad_enable = spansion_quad_enable;
2392 /* Override the parameters with data read from SFDP tables. */
2393 nor->addr_width = 0;
2394 nor->mtd.erasesize = 0;
2395 if ((info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)) &&
2396 !(info->flags & SPI_NOR_SKIP_SFDP)) {
2397 struct spi_nor_flash_parameter sfdp_params;
2399 memcpy(&sfdp_params, params, sizeof(sfdp_params));
2400 if (spi_nor_parse_sfdp(nor, &sfdp_params)) {
2401 nor->addr_width = 0;
2402 nor->mtd.erasesize = 0;
2404 memcpy(params, &sfdp_params, sizeof(*params));
2411 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
2415 for (i = 0; i < size; i++)
2416 if (table[i][0] == (int)hwcaps)
2422 static int spi_nor_hwcaps_read2cmd(u32 hwcaps)
2424 static const int hwcaps_read2cmd[][2] = {
2425 { SNOR_HWCAPS_READ, SNOR_CMD_READ },
2426 { SNOR_HWCAPS_READ_FAST, SNOR_CMD_READ_FAST },
2427 { SNOR_HWCAPS_READ_1_1_1_DTR, SNOR_CMD_READ_1_1_1_DTR },
2428 { SNOR_HWCAPS_READ_1_1_2, SNOR_CMD_READ_1_1_2 },
2429 { SNOR_HWCAPS_READ_1_2_2, SNOR_CMD_READ_1_2_2 },
2430 { SNOR_HWCAPS_READ_2_2_2, SNOR_CMD_READ_2_2_2 },
2431 { SNOR_HWCAPS_READ_1_2_2_DTR, SNOR_CMD_READ_1_2_2_DTR },
2432 { SNOR_HWCAPS_READ_1_1_4, SNOR_CMD_READ_1_1_4 },
2433 { SNOR_HWCAPS_READ_1_4_4, SNOR_CMD_READ_1_4_4 },
2434 { SNOR_HWCAPS_READ_4_4_4, SNOR_CMD_READ_4_4_4 },
2435 { SNOR_HWCAPS_READ_1_4_4_DTR, SNOR_CMD_READ_1_4_4_DTR },
2436 { SNOR_HWCAPS_READ_1_1_8, SNOR_CMD_READ_1_1_8 },
2437 { SNOR_HWCAPS_READ_1_8_8, SNOR_CMD_READ_1_8_8 },
2438 { SNOR_HWCAPS_READ_8_8_8, SNOR_CMD_READ_8_8_8 },
2439 { SNOR_HWCAPS_READ_1_8_8_DTR, SNOR_CMD_READ_1_8_8_DTR },
2442 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
2443 ARRAY_SIZE(hwcaps_read2cmd));
2446 static int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
2448 static const int hwcaps_pp2cmd[][2] = {
2449 { SNOR_HWCAPS_PP, SNOR_CMD_PP },
2450 { SNOR_HWCAPS_PP_1_1_4, SNOR_CMD_PP_1_1_4 },
2451 { SNOR_HWCAPS_PP_1_4_4, SNOR_CMD_PP_1_4_4 },
2452 { SNOR_HWCAPS_PP_4_4_4, SNOR_CMD_PP_4_4_4 },
2453 { SNOR_HWCAPS_PP_1_1_8, SNOR_CMD_PP_1_1_8 },
2454 { SNOR_HWCAPS_PP_1_8_8, SNOR_CMD_PP_1_8_8 },
2455 { SNOR_HWCAPS_PP_8_8_8, SNOR_CMD_PP_8_8_8 },
2458 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
2459 ARRAY_SIZE(hwcaps_pp2cmd));
2462 static int spi_nor_select_read(struct spi_nor *nor,
2463 const struct spi_nor_flash_parameter *params,
2466 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
2467 const struct spi_nor_read_command *read;
2472 cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
2476 read = ¶ms->reads[cmd];
2477 nor->read_opcode = read->opcode;
2478 nor->read_proto = read->proto;
2481 * In the spi-nor framework, we don't need to make the difference
2482 * between mode clock cycles and wait state clock cycles.
2483 * Indeed, the value of the mode clock cycles is used by a QSPI
2484 * flash memory to know whether it should enter or leave its 0-4-4
2485 * (Continuous Read / XIP) mode.
2486 * eXecution In Place is out of the scope of the mtd sub-system.
2487 * Hence we choose to merge both mode and wait state clock cycles
2488 * into the so called dummy clock cycles.
2490 nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
2494 static int spi_nor_select_pp(struct spi_nor *nor,
2495 const struct spi_nor_flash_parameter *params,
2498 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
2499 const struct spi_nor_pp_command *pp;
2504 cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
2508 pp = ¶ms->page_programs[cmd];
2509 nor->program_opcode = pp->opcode;
2510 nor->write_proto = pp->proto;
2514 static int spi_nor_select_erase(struct spi_nor *nor,
2515 const struct flash_info *info)
2517 struct mtd_info *mtd = &nor->mtd;
2519 /* Do nothing if already configured from SFDP. */
2523 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
2524 /* prefer "small sector" erase if possible */
2525 if (info->flags & SECT_4K) {
2526 nor->erase_opcode = SPINOR_OP_BE_4K;
2527 mtd->erasesize = 4096;
2528 } else if (info->flags & SECT_4K_PMC) {
2529 nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
2530 mtd->erasesize = 4096;
2534 nor->erase_opcode = SPINOR_OP_SE;
2535 mtd->erasesize = info->sector_size;
2540 static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
2541 const struct spi_nor_flash_parameter *params,
2542 const struct spi_nor_hwcaps *hwcaps)
2544 u32 ignored_mask, shared_mask;
2545 bool enable_quad_io;
2549 * Keep only the hardware capabilities supported by both the SPI
2550 * controller and the SPI flash memory.
2552 shared_mask = hwcaps->mask & params->hwcaps.mask;
2554 /* SPI n-n-n protocols are not supported yet. */
2555 ignored_mask = (SNOR_HWCAPS_READ_2_2_2 |
2556 SNOR_HWCAPS_READ_4_4_4 |
2557 SNOR_HWCAPS_READ_8_8_8 |
2558 SNOR_HWCAPS_PP_4_4_4 |
2559 SNOR_HWCAPS_PP_8_8_8);
2560 if (shared_mask & ignored_mask) {
2562 "SPI n-n-n protocols are not supported yet.\n");
2563 shared_mask &= ~ignored_mask;
2566 /* Select the (Fast) Read command. */
2567 err = spi_nor_select_read(nor, params, shared_mask);
2570 "can't select read settings supported by both the SPI controller and memory.\n");
2574 /* Select the Page Program command. */
2575 err = spi_nor_select_pp(nor, params, shared_mask);
2578 "can't select write settings supported by both the SPI controller and memory.\n");
2582 /* Select the Sector Erase command. */
2583 err = spi_nor_select_erase(nor, info);
2586 "can't select erase settings supported by both the SPI controller and memory.\n");
2590 /* Enable Quad I/O if needed. */
2591 enable_quad_io = (spi_nor_get_protocol_width(nor->read_proto) == 4 ||
2592 spi_nor_get_protocol_width(nor->write_proto) == 4);
2593 if (enable_quad_io && params->quad_enable) {
2594 err = params->quad_enable(nor);
2596 dev_err(nor->dev, "quad mode not supported\n");
2604 int spi_nor_scan(struct spi_nor *nor, const char *name,
2605 const struct spi_nor_hwcaps *hwcaps)
2607 struct spi_nor_flash_parameter params;
2608 const struct flash_info *info = NULL;
2609 struct device *dev = nor->dev;
2610 struct mtd_info *mtd = &nor->mtd;
2611 struct device_node *np = spi_nor_get_flash_node(nor);
2615 ret = spi_nor_check(nor);
2619 /* Reset SPI protocol for all commands. */
2620 nor->reg_proto = SNOR_PROTO_1_1_1;
2621 nor->read_proto = SNOR_PROTO_1_1_1;
2622 nor->write_proto = SNOR_PROTO_1_1_1;
2625 info = spi_nor_match_id(name);
2626 /* Try to auto-detect if chip name wasn't specified or not found */
2628 info = spi_nor_read_id(nor);
2629 if (IS_ERR_OR_NULL(info))
2633 * If caller has specified name of flash model that can normally be
2634 * detected using JEDEC, let's verify it.
2636 if (name && info->id_len) {
2637 const struct flash_info *jinfo;
2639 jinfo = spi_nor_read_id(nor);
2640 if (IS_ERR(jinfo)) {
2641 return PTR_ERR(jinfo);
2642 } else if (jinfo != info) {
2644 * JEDEC knows better, so overwrite platform ID. We
2645 * can't trust partitions any longer, but we'll let
2646 * mtd apply them anyway, since some partitions may be
2647 * marked read-only, and we don't want to lose that
2648 * information, even if it's not 100% accurate.
2650 dev_warn(dev, "found %s, expected %s\n",
2651 jinfo->name, info->name);
2656 mutex_init(&nor->lock);
2659 * Make sure the XSR_RDY flag is set before calling
2660 * spi_nor_wait_till_ready(). Xilinx S3AN share MFR
2661 * with Atmel spi-nor
2663 if (info->flags & SPI_S3AN)
2664 nor->flags |= SNOR_F_READY_XSR_RDY;
2666 /* Parse the Serial Flash Discoverable Parameters table. */
2667 ret = spi_nor_init_params(nor, info, ¶ms);
2672 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
2673 * with the software protection bits set
2676 if (JEDEC_MFR(info) == SNOR_MFR_ATMEL ||
2677 JEDEC_MFR(info) == SNOR_MFR_INTEL ||
2678 JEDEC_MFR(info) == SNOR_MFR_SST ||
2679 info->flags & SPI_NOR_HAS_LOCK) {
2682 spi_nor_wait_till_ready(nor);
2686 mtd->name = dev_name(dev);
2688 mtd->type = MTD_NORFLASH;
2690 mtd->flags = MTD_CAP_NORFLASH;
2691 mtd->size = params.size;
2692 mtd->_erase = spi_nor_erase;
2693 mtd->_read = spi_nor_read;
2695 /* NOR protection support for STmicro/Micron chips and similar */
2696 if (JEDEC_MFR(info) == SNOR_MFR_MICRON ||
2697 info->flags & SPI_NOR_HAS_LOCK) {
2698 nor->flash_lock = stm_lock;
2699 nor->flash_unlock = stm_unlock;
2700 nor->flash_is_locked = stm_is_locked;
2703 if (nor->flash_lock && nor->flash_unlock && nor->flash_is_locked) {
2704 mtd->_lock = spi_nor_lock;
2705 mtd->_unlock = spi_nor_unlock;
2706 mtd->_is_locked = spi_nor_is_locked;
2709 /* sst nor chips use AAI word program */
2710 if (info->flags & SST_WRITE)
2711 mtd->_write = sst_write;
2713 mtd->_write = spi_nor_write;
2715 if (info->flags & USE_FSR)
2716 nor->flags |= SNOR_F_USE_FSR;
2717 if (info->flags & SPI_NOR_HAS_TB)
2718 nor->flags |= SNOR_F_HAS_SR_TB;
2719 if (info->flags & NO_CHIP_ERASE)
2720 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
2721 if (info->flags & USE_CLSR)
2722 nor->flags |= SNOR_F_USE_CLSR;
2724 if (info->flags & SPI_NOR_NO_ERASE)
2725 mtd->flags |= MTD_NO_ERASE;
2727 mtd->dev.parent = dev;
2728 nor->page_size = params.page_size;
2729 mtd->writebufsize = nor->page_size;
2732 /* If we were instantiated by DT, use it */
2733 if (of_property_read_bool(np, "m25p,fast-read"))
2734 params.hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
2736 params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
2738 /* If we weren't instantiated by DT, default to fast-read */
2739 params.hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
2742 /* Some devices cannot do fast-read, no matter what DT tells us */
2743 if (info->flags & SPI_NOR_NO_FR)
2744 params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
2747 * Configure the SPI memory:
2748 * - select op codes for (Fast) Read, Page Program and Sector Erase.
2749 * - set the number of dummy cycles (mode cycles + wait states).
2750 * - set the SPI protocols for register and memory accesses.
2751 * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
2753 ret = spi_nor_setup(nor, info, ¶ms, hwcaps);
2757 if (nor->addr_width) {
2758 /* already configured from SFDP */
2759 } else if (info->addr_width) {
2760 nor->addr_width = info->addr_width;
2761 } else if (mtd->size > 0x1000000) {
2762 /* enable 4-byte addressing if the device exceeds 16MiB */
2763 nor->addr_width = 4;
2764 if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
2765 info->flags & SPI_NOR_4B_OPCODES)
2766 spi_nor_set_4byte_opcodes(nor, info);
2768 set_4byte(nor, info, 1);
2770 nor->addr_width = 3;
2773 if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
2774 dev_err(dev, "address width is too large: %u\n",
2779 if (info->flags & SPI_S3AN) {
2780 ret = s3an_nor_scan(info, nor);
2785 dev_info(dev, "%s (%lld Kbytes)\n", info->name,
2786 (long long)mtd->size >> 10);
2789 "mtd .name = %s, .size = 0x%llx (%lldMiB), "
2790 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
2791 mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
2792 mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
2794 if (mtd->numeraseregions)
2795 for (i = 0; i < mtd->numeraseregions; i++)
2797 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
2798 ".erasesize = 0x%.8x (%uKiB), "
2799 ".numblocks = %d }\n",
2800 i, (long long)mtd->eraseregions[i].offset,
2801 mtd->eraseregions[i].erasesize,
2802 mtd->eraseregions[i].erasesize / 1024,
2803 mtd->eraseregions[i].numblocks);
2806 EXPORT_SYMBOL_GPL(spi_nor_scan);
2808 static const struct flash_info *spi_nor_match_id(const char *name)
2810 const struct flash_info *id = spi_nor_ids;
2813 if (!strcmp(name, id->name))
2820 MODULE_LICENSE("GPL");
2821 MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
2822 MODULE_AUTHOR("Mike Lavender");
2823 MODULE_DESCRIPTION("framework for SPI NOR");