1 // SPDX-License-Identifier: GPL-2.0
3 * Based on m25p80.c, by Mike Lavender (mike@steroidmicros.com), with
4 * influence from lart.c (Abraham Van Der Merwe) and mtd_dataflash.c
6 * Copyright (C) 2005, Intec Automation Inc.
7 * Copyright (C) 2014, Freescale Semiconductor, Inc.
9 * Synced from Linux v4.19
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/log2.h>
16 #include <linux/math64.h>
17 #include <linux/sizes.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/spi-nor.h>
24 /* Define max times to check status register before we give up. */
27 * For everything but full-chip erase; probably could be much smaller, but kept
28 * around for safety for now
31 #define HZ CONFIG_SYS_HZ
33 #define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ)
35 #define SPI_NOR_MAX_ID_LEN 6
36 #define SPI_NOR_MAX_ADDR_WIDTH 4
42 * This array stores the ID bytes.
43 * The first three bytes are the JEDIC ID.
44 * JEDEC ID zero means "no ID" (mostly older chips).
46 u8 id[SPI_NOR_MAX_ID_LEN];
49 /* The size listed here is what works with SPINOR_OP_SE, which isn't
50 * necessarily called a "sector" by the vendor.
52 unsigned int sector_size;
59 #define SECT_4K BIT(0) /* SPINOR_OP_BE_4K works uniformly */
60 #define SPI_NOR_NO_ERASE BIT(1) /* No erase command needed */
61 #define SST_WRITE BIT(2) /* use SST byte programming */
62 #define SPI_NOR_NO_FR BIT(3) /* Can't do fastread */
63 #define SECT_4K_PMC BIT(4) /* SPINOR_OP_BE_4K_PMC works uniformly */
64 #define SPI_NOR_DUAL_READ BIT(5) /* Flash supports Dual Read */
65 #define SPI_NOR_QUAD_READ BIT(6) /* Flash supports Quad Read */
66 #define USE_FSR BIT(7) /* use flag status register */
67 #define SPI_NOR_HAS_LOCK BIT(8) /* Flash supports lock/unlock via SR */
68 #define SPI_NOR_HAS_TB BIT(9) /*
69 * Flash SR has Top/Bottom (TB) protect
70 * bit. Must be used with
73 #define SPI_S3AN BIT(10) /*
74 * Xilinx Spartan 3AN In-System Flash
75 * (MFR cannot be used for probing
76 * because it has the same value as
79 #define SPI_NOR_4B_OPCODES BIT(11) /*
80 * Use dedicated 4byte address op codes
81 * to support memory size above 128Mib.
83 #define NO_CHIP_ERASE BIT(12) /* Chip does not support chip erase */
84 #define USE_CLSR BIT(14) /* use CLSR command */
86 int (*quad_enable)(struct spi_nor *nor);
89 #define JEDEC_MFR(info) ((info)->id[0])
91 static int spi_nor_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len)
96 static int spi_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
101 static ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
107 static ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
114 * Read the status register, returning its value in the location
115 * Return the status register value.
116 * Returns negative if error occurred.
118 static int read_sr(struct spi_nor *nor)
123 ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
125 pr_debug("error %d reading SR\n", (int)ret);
133 * Read the flag status register, returning its value in the location
134 * Return the status register value.
135 * Returns negative if error occurred.
137 static int read_fsr(struct spi_nor *nor)
142 ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
144 pr_debug("error %d reading FSR\n", ret);
152 * Read configuration register, returning its value in the
153 * location. Return the configuration register value.
154 * Returns negative if error occurred.
156 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
157 static int read_cr(struct spi_nor *nor)
162 ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
164 dev_dbg(nor->dev, "error %d reading CR\n", ret);
173 * Write status register 1 byte
174 * Returns negative if error occurred.
176 static int write_sr(struct spi_nor *nor, u8 val)
178 nor->cmd_buf[0] = val;
179 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
183 * Set write enable latch with Write Enable command.
184 * Returns negative if error occurred.
186 static int write_enable(struct spi_nor *nor)
188 return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
192 * Send write disable instruction to the chip.
194 static int write_disable(struct spi_nor *nor)
196 return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
199 static struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
204 static int spi_nor_sr_ready(struct spi_nor *nor)
206 int sr = read_sr(nor);
211 if (nor->flags & SNOR_F_USE_CLSR && sr & (SR_E_ERR | SR_P_ERR)) {
213 dev_dbg(nor->dev, "Erase Error occurred\n");
215 dev_dbg(nor->dev, "Programming Error occurred\n");
217 nor->write_reg(nor, SPINOR_OP_CLSR, NULL, 0);
221 return !(sr & SR_WIP);
224 static int spi_nor_fsr_ready(struct spi_nor *nor)
226 int fsr = read_fsr(nor);
231 if (fsr & (FSR_E_ERR | FSR_P_ERR)) {
233 dev_dbg(nor->dev, "Erase operation failed.\n");
235 dev_dbg(nor->dev, "Program operation failed.\n");
237 if (fsr & FSR_PT_ERR)
239 "Attempted to modify a protected sector.\n");
241 nor->write_reg(nor, SPINOR_OP_CLFSR, NULL, 0);
245 return fsr & FSR_READY;
248 static int spi_nor_ready(struct spi_nor *nor)
252 sr = spi_nor_sr_ready(nor);
255 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
262 * Service routine to read status register until ready, or timeout occurs.
263 * Returns non-zero if error.
265 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
266 unsigned long timeout)
268 unsigned long timebase;
271 timebase = get_timer(0);
273 while (get_timer(timebase) < timeout) {
274 ret = spi_nor_ready(nor);
281 dev_err(nor->dev, "flash operation timed out\n");
286 static int spi_nor_wait_till_ready(struct spi_nor *nor)
288 return spi_nor_wait_till_ready_with_timeout(nor,
289 DEFAULT_READY_WAIT_JIFFIES);
293 * Initiate the erasure of a single sector
295 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
297 u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
301 return nor->erase(nor, addr);
304 * Default implementation, if driver doesn't have a specialized HW
307 for (i = nor->addr_width - 1; i >= 0; i--) {
308 buf[i] = addr & 0xff;
312 return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
316 * Erase an address range on the nor chip. The address range may extend
317 * one or more erase sectors. Return an error is there is a problem erasing.
319 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
321 struct spi_nor *nor = mtd_to_spi_nor(mtd);
325 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
326 (long long)instr->len);
328 div_u64_rem(instr->len, mtd->erasesize, &rem);
338 ret = spi_nor_erase_sector(nor, addr);
342 addr += mtd->erasesize;
343 len -= mtd->erasesize;
345 ret = spi_nor_wait_till_ready(nor);
356 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
357 /* Write status register and ensure bits in mask match written values */
358 static int write_sr_and_check(struct spi_nor *nor, u8 status_new, u8 mask)
363 ret = write_sr(nor, status_new);
367 ret = spi_nor_wait_till_ready(nor);
375 return ((ret & mask) != (status_new & mask)) ? -EIO : 0;
378 static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
381 struct mtd_info *mtd = &nor->mtd;
382 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
383 int shift = ffs(mask) - 1;
391 pow = ((sr & mask) ^ mask) >> shift;
392 *len = mtd->size >> pow;
393 if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
396 *ofs = mtd->size - *len;
401 * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
402 * @locked is false); 0 otherwise
404 static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, u64 len,
413 stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
416 /* Requested range is a sub-range of locked range */
417 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
419 /* Requested range does not overlap with locked range */
420 return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
423 static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
426 return stm_check_lock_status_sr(nor, ofs, len, sr, true);
429 static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
432 return stm_check_lock_status_sr(nor, ofs, len, sr, false);
436 * Lock a region of the flash. Compatible with ST Micro and similar flash.
437 * Supports the block protection bits BP{0,1,2} in the status register
438 * (SR). Does not support these features found in newer SR bitfields:
439 * - SEC: sector/block protect - only handle SEC=0 (block protect)
440 * - CMP: complement protect - only support CMP=0 (range is not complemented)
442 * Support for the following is provided conditionally for some flash:
443 * - TB: top/bottom protect
445 * Sample table portion for 8MB flash (Winbond w25q64fw):
447 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
448 * --------------------------------------------------------------------------
449 * X | X | 0 | 0 | 0 | NONE | NONE
450 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
451 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
452 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
453 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
454 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
455 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
456 * X | X | 1 | 1 | 1 | 8 MB | ALL
457 * ------|-------|-------|-------|-------|---------------|-------------------
458 * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
459 * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
460 * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
461 * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
462 * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
463 * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
465 * Returns negative on errors, 0 on success.
467 static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
469 struct mtd_info *mtd = &nor->mtd;
470 int status_old, status_new;
471 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
472 u8 shift = ffs(mask) - 1, pow, val;
474 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
477 status_old = read_sr(nor);
481 /* If nothing in our range is unlocked, we don't need to do anything */
482 if (stm_is_locked_sr(nor, ofs, len, status_old))
485 /* If anything below us is unlocked, we can't use 'bottom' protection */
486 if (!stm_is_locked_sr(nor, 0, ofs, status_old))
487 can_be_bottom = false;
489 /* If anything above us is unlocked, we can't use 'top' protection */
490 if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
494 if (!can_be_bottom && !can_be_top)
497 /* Prefer top, if both are valid */
498 use_top = can_be_top;
500 /* lock_len: length of region that should end up locked */
502 lock_len = mtd->size - ofs;
504 lock_len = ofs + len;
507 * Need smallest pow such that:
509 * 1 / (2^pow) <= (len / size)
511 * so (assuming power-of-2 size) we do:
513 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
515 pow = ilog2(mtd->size) - ilog2(lock_len);
516 val = mask - (pow << shift);
519 /* Don't "lock" with no region! */
523 status_new = (status_old & ~mask & ~SR_TB) | val;
525 /* Disallow further writes if WP pin is asserted */
526 status_new |= SR_SRWD;
531 /* Don't bother if they're the same */
532 if (status_new == status_old)
535 /* Only modify protection if it will not unlock other areas */
536 if ((status_new & mask) < (status_old & mask))
539 return write_sr_and_check(nor, status_new, mask);
543 * Unlock a region of the flash. See stm_lock() for more info
545 * Returns negative on errors, 0 on success.
547 static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
549 struct mtd_info *mtd = &nor->mtd;
550 int status_old, status_new;
551 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
552 u8 shift = ffs(mask) - 1, pow, val;
554 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
557 status_old = read_sr(nor);
561 /* If nothing in our range is locked, we don't need to do anything */
562 if (stm_is_unlocked_sr(nor, ofs, len, status_old))
565 /* If anything below us is locked, we can't use 'top' protection */
566 if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
569 /* If anything above us is locked, we can't use 'bottom' protection */
570 if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
572 can_be_bottom = false;
574 if (!can_be_bottom && !can_be_top)
577 /* Prefer top, if both are valid */
578 use_top = can_be_top;
580 /* lock_len: length of region that should remain locked */
582 lock_len = mtd->size - (ofs + len);
587 * Need largest pow such that:
589 * 1 / (2^pow) >= (len / size)
591 * so (assuming power-of-2 size) we do:
593 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
595 pow = ilog2(mtd->size) - order_base_2(lock_len);
597 val = 0; /* fully unlocked */
599 val = mask - (pow << shift);
600 /* Some power-of-two sizes are not supported */
605 status_new = (status_old & ~mask & ~SR_TB) | val;
607 /* Don't protect status register if we're fully unlocked */
609 status_new &= ~SR_SRWD;
614 /* Don't bother if they're the same */
615 if (status_new == status_old)
618 /* Only modify protection if it will not lock other areas */
619 if ((status_new & mask) > (status_old & mask))
622 return write_sr_and_check(nor, status_new, mask);
626 * Check if a region of the flash is (completely) locked. See stm_lock() for
629 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
630 * negative on errors.
632 static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
636 status = read_sr(nor);
640 return stm_is_locked_sr(nor, ofs, len, status);
642 #endif /* CONFIG_SPI_FLASH_STMICRO */
644 /* Used when the "_ext_id" is two bytes at most */
645 #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
647 ((_jedec_id) >> 16) & 0xff, \
648 ((_jedec_id) >> 8) & 0xff, \
649 (_jedec_id) & 0xff, \
650 ((_ext_id) >> 8) & 0xff, \
653 .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))), \
654 .sector_size = (_sector_size), \
655 .n_sectors = (_n_sectors), \
659 #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
661 ((_jedec_id) >> 16) & 0xff, \
662 ((_jedec_id) >> 8) & 0xff, \
663 (_jedec_id) & 0xff, \
664 ((_ext_id) >> 16) & 0xff, \
665 ((_ext_id) >> 8) & 0xff, \
669 .sector_size = (_sector_size), \
670 .n_sectors = (_n_sectors), \
674 /* NOTE: double check command sets and memory organization when you add
675 * more nor chips. This current list focusses on newer chips, which
676 * have been converging on command sets which including JEDEC ID.
678 * All newly added entries should describe *hardware* and should use SECT_4K
679 * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage
680 * scenarios excluding small sectors there is config option that can be
681 * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS.
682 * For historical (and compatibility) reasons (before we got above config) some
683 * old entries may be missing 4K flag.
685 const struct flash_info spi_nor_ids[] = {
686 #ifdef CONFIG_SPI_FLASH_ATMEL /* ATMEL */
687 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
688 { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
689 { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
691 { "at45db011d", INFO(0x1f2200, 0, 64 * 1024, 4, SECT_4K) },
692 { "at45db021d", INFO(0x1f2300, 0, 64 * 1024, 8, SECT_4K) },
693 { "at45db041d", INFO(0x1f2400, 0, 64 * 1024, 8, SECT_4K) },
694 { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
695 { "at45db161d", INFO(0x1f2600, 0, 64 * 1024, 32, SECT_4K) },
696 { "at45db321d", INFO(0x1f2700, 0, 64 * 1024, 64, SECT_4K) },
697 { "at45db641d", INFO(0x1f2800, 0, 64 * 1024, 128, SECT_4K) },
698 { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
700 #ifdef CONFIG_SPI_FLASH_EON /* EON */
702 { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
703 { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
704 { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
705 { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K) },
707 #ifdef CONFIG_SPI_FLASH_GIGADEVICE /* GIGADEVICE */
710 "gd25q16", INFO(0xc84015, 0, 64 * 1024, 32,
711 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
712 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
715 "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64,
716 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
717 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
720 "gd25lq32", INFO(0xc86016, 0, 64 * 1024, 64,
721 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
722 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
725 "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128,
726 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
727 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
730 #ifdef CONFIG_SPI_FLASH_ISSI /* ISSI */
732 { "is25lq040b", INFO(0x9d4013, 0, 64 * 1024, 8,
733 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
734 { "is25lp032", INFO(0x9d6016, 0, 64 * 1024, 64, 0) },
735 { "is25lp064", INFO(0x9d6017, 0, 64 * 1024, 128, 0) },
736 { "is25lp128", INFO(0x9d6018, 0, 64 * 1024, 256,
737 SECT_4K | SPI_NOR_DUAL_READ) },
738 { "is25lp256", INFO(0x9d6019, 0, 64 * 1024, 512,
739 SECT_4K | SPI_NOR_DUAL_READ) },
740 { "is25wp032", INFO(0x9d7016, 0, 64 * 1024, 64,
741 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
742 { "is25wp064", INFO(0x9d7017, 0, 64 * 1024, 128,
743 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
744 { "is25wp128", INFO(0x9d7018, 0, 64 * 1024, 256,
745 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
747 #ifdef CONFIG_SPI_FLASH_MACRONIX /* MACRONIX */
749 { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
750 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
751 { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
752 { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
753 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, SECT_4K) },
754 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, SECT_4K) },
755 { "mx25u2033e", INFO(0xc22532, 0, 64 * 1024, 4, SECT_4K) },
756 { "mx25u1635e", INFO(0xc22535, 0, 64 * 1024, 32, SECT_4K) },
757 { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
758 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
759 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
760 { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
761 { "mx25u25635f", INFO(0xc22539, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_4B_OPCODES) },
762 { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
763 { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
764 { "mx66u51235f", INFO(0xc2253a, 0, 64 * 1024, 1024, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
765 { "mx66l1g45g", INFO(0xc2201b, 0, 64 * 1024, 2048, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
766 { "mx25l1633e", INFO(0xc22415, 0, 64 * 1024, 32, SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES | SECT_4K) },
769 #ifdef CONFIG_SPI_FLASH_STMICRO /* STMICRO */
771 { "n25q016a", INFO(0x20bb15, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_QUAD_READ) },
772 { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
773 { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
774 { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
775 { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
776 { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
777 { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
778 { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
779 { "n25q256ax1", INFO(0x20bb19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ) },
780 { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
781 { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
782 { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
783 { "n25q00a", INFO(0x20bb21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
784 { "mt25qu02g", INFO(0x20bb22, 0, 64 * 1024, 4096, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
786 #ifdef CONFIG_SPI_FLASH_SPANSION /* SPANSION */
787 /* Spansion/Cypress -- single (large) sector size only, at least
788 * for the chips listed here (without boot sectors).
790 { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
791 { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
792 { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, USE_CLSR) },
793 { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
794 { "s25fl512s", INFO6(0x010220, 0x4d0081, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
795 { "s25fl512s_256k", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
796 { "s25fl512s_64k", INFO(0x010220, 0x4d01, 64 * 1024, 1024, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
797 { "s25fl512s_512k", INFO(0x010220, 0x4f00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
798 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
799 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
800 { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
801 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
802 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
803 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
804 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
805 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
806 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
807 { "s25fl116k", INFO(0x014015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
808 { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
809 { "s25fl208k", INFO(0x014014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ) },
810 { "s25fl128l", INFO(0x016018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
812 #ifdef CONFIG_SPI_FLASH_SST /* SST */
813 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
814 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
815 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
816 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
817 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
818 { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
819 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
820 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
821 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
822 { "sst25wf020a", INFO(0x621612, 0, 64 * 1024, 4, SECT_4K) },
823 { "sst25wf040b", INFO(0x621613, 0, 64 * 1024, 8, SECT_4K) },
824 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
825 { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
826 { "sst26vf064b", INFO(0xbf2643, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
827 { "sst26wf016", INFO(0xbf2651, 0, 64 * 1024, 32, SECT_4K) },
828 { "sst26wf032", INFO(0xbf2622, 0, 64 * 1024, 64, SECT_4K) },
829 { "sst26wf064", INFO(0xbf2643, 0, 64 * 1024, 128, SECT_4K) },
831 #ifdef CONFIG_SPI_FLASH_STMICRO /* STMICRO */
832 /* ST Microelectronics -- newer production may have feature updates */
833 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
834 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
835 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
836 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
837 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
838 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
839 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
840 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
841 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
842 { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
843 { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
845 #ifdef CONFIG_SPI_FLASH_WINBOND /* WINBOND */
846 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
847 { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
848 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
849 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
850 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
851 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
852 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
854 "w25q16dw", INFO(0xef6015, 0, 64 * 1024, 32,
855 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
856 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
858 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
859 { "w25q20cl", INFO(0xef4012, 0, 64 * 1024, 4, SECT_4K) },
860 { "w25q20bw", INFO(0xef5012, 0, 64 * 1024, 4, SECT_4K) },
861 { "w25q20ew", INFO(0xef6012, 0, 64 * 1024, 4, SECT_4K) },
862 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
864 "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64,
865 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
866 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
869 "w25q32jv", INFO(0xef7016, 0, 64 * 1024, 64,
870 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
871 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
873 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
874 { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
876 "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128,
877 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
878 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
881 "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256,
882 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
883 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
885 { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
886 { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
887 { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
888 { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
889 { "w25m512jv", INFO(0xef7119, 0, 64 * 1024, 1024,
890 SECT_4K | SPI_NOR_QUAD_READ | SPI_NOR_DUAL_READ) },
892 #ifdef CONFIG_SPI_FLASH_XMC
893 /* XMC (Wuhan Xinxin Semiconductor Manufacturing Corp.) */
894 { "XM25QH64A", INFO(0x207017, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
895 { "XM25QH128A", INFO(0x207018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
900 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
903 u8 id[SPI_NOR_MAX_ID_LEN];
904 const struct flash_info *info;
906 if (!ARRAY_SIZE(spi_nor_ids))
907 return ERR_PTR(-ENODEV);
909 tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
911 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
915 for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
916 info = &spi_nor_ids[tmp];
918 if (!memcmp(info->id, id, info->id_len))
919 return &spi_nor_ids[tmp];
922 dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
923 id[0], id[1], id[2]);
924 return ERR_PTR(-ENODEV);
927 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
928 size_t *retlen, u_char *buf)
930 struct spi_nor *nor = mtd_to_spi_nor(mtd);
933 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
938 ret = nor->read(nor, addr, len, buf);
940 /* We shouldn't see 0-length reads */
958 #ifdef CONFIG_SPI_FLASH_SST
959 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
960 size_t *retlen, const u_char *buf)
962 struct spi_nor *nor = mtd_to_spi_nor(mtd);
966 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
970 nor->sst_write_second = false;
973 /* Start write from odd address. */
975 nor->program_opcode = SPINOR_OP_BP;
977 /* write one byte. */
978 ret = nor->write(nor, to, 1, buf);
981 ret = spi_nor_wait_till_ready(nor);
987 /* Write out most of the data here. */
988 for (; actual < len - 1; actual += 2) {
989 nor->program_opcode = SPINOR_OP_AAI_WP;
991 /* write two bytes. */
992 ret = nor->write(nor, to, 2, buf + actual);
995 ret = spi_nor_wait_till_ready(nor);
999 nor->sst_write_second = true;
1001 nor->sst_write_second = false;
1004 ret = spi_nor_wait_till_ready(nor);
1008 /* Write out trailing byte if it exists. */
1009 if (actual != len) {
1012 nor->program_opcode = SPINOR_OP_BP;
1013 ret = nor->write(nor, to, 1, buf + actual);
1016 ret = spi_nor_wait_till_ready(nor);
1028 * Write an address range to the nor chip. Data must be written in
1029 * FLASH_PAGESIZE chunks. The address range may be any size provided
1030 * it is within the physical boundaries.
1032 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
1033 size_t *retlen, const u_char *buf)
1035 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1036 size_t page_offset, page_remain, i;
1039 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1041 for (i = 0; i < len; ) {
1043 loff_t addr = to + i;
1046 * If page_size is a power of two, the offset can be quickly
1047 * calculated with an AND operation. On the other cases we
1048 * need to do a modulus operation (more expensive).
1049 * Power of two numbers have only one bit set and we can use
1050 * the instruction hweight32 to detect if we need to do a
1051 * modulus (do_div()) or not.
1053 if (hweight32(nor->page_size) == 1) {
1054 page_offset = addr & (nor->page_size - 1);
1058 page_offset = do_div(aux, nor->page_size);
1060 /* the size of data remaining on the first page */
1061 page_remain = min_t(size_t,
1062 nor->page_size - page_offset, len - i);
1065 ret = nor->write(nor, addr, page_remain, buf + i);
1070 ret = spi_nor_wait_till_ready(nor);
1075 if (written != page_remain) {
1085 #ifdef CONFIG_SPI_FLASH_MACRONIX
1087 * macronix_quad_enable() - set QE bit in Status Register.
1088 * @nor: pointer to a 'struct spi_nor'
1090 * Set the Quad Enable (QE) bit in the Status Register.
1092 * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
1094 * Return: 0 on success, -errno otherwise.
1096 static int macronix_quad_enable(struct spi_nor *nor)
1103 if (val & SR_QUAD_EN_MX)
1108 write_sr(nor, val | SR_QUAD_EN_MX);
1110 ret = spi_nor_wait_till_ready(nor);
1115 if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
1116 dev_err(nor->dev, "Macronix Quad bit not set\n");
1124 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
1126 * Write status Register and configuration register with 2 bytes
1127 * The first byte will be written to the status register, while the
1128 * second byte will be written to the configuration register.
1129 * Return negative if error occurred.
1131 static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
1137 ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
1140 "error while writing configuration register\n");
1144 ret = spi_nor_wait_till_ready(nor);
1147 "timeout while writing configuration register\n");
1155 * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
1156 * @nor: pointer to a 'struct spi_nor'
1158 * Set the Quad Enable (QE) bit in the Configuration Register.
1159 * This function should be used with QSPI memories supporting the Read
1160 * Configuration Register (35h) instruction.
1162 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1165 * Return: 0 on success, -errno otherwise.
1167 static int spansion_read_cr_quad_enable(struct spi_nor *nor)
1172 /* Check current Quad Enable bit value. */
1175 dev_dbg(dev, "error while reading configuration register\n");
1179 if (ret & CR_QUAD_EN_SPAN)
1182 sr_cr[1] = ret | CR_QUAD_EN_SPAN;
1184 /* Keep the current value of the Status Register. */
1187 dev_dbg(dev, "error while reading status register\n");
1192 ret = write_sr_cr(nor, sr_cr);
1196 /* Read back and check it. */
1198 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
1199 dev_dbg(nor->dev, "Spansion Quad bit not set\n");
1205 #endif /* CONFIG_SPI_FLASH_SPANSION */
1207 struct spi_nor_read_command {
1211 enum spi_nor_protocol proto;
1214 struct spi_nor_pp_command {
1216 enum spi_nor_protocol proto;
1219 enum spi_nor_read_command_index {
1222 SNOR_CMD_READ_1_1_1_DTR,
1225 SNOR_CMD_READ_1_1_2,
1226 SNOR_CMD_READ_1_2_2,
1227 SNOR_CMD_READ_2_2_2,
1228 SNOR_CMD_READ_1_2_2_DTR,
1231 SNOR_CMD_READ_1_1_4,
1232 SNOR_CMD_READ_1_4_4,
1233 SNOR_CMD_READ_4_4_4,
1234 SNOR_CMD_READ_1_4_4_DTR,
1237 SNOR_CMD_READ_1_1_8,
1238 SNOR_CMD_READ_1_8_8,
1239 SNOR_CMD_READ_8_8_8,
1240 SNOR_CMD_READ_1_8_8_DTR,
1245 enum spi_nor_pp_command_index {
1261 struct spi_nor_flash_parameter {
1265 struct spi_nor_hwcaps hwcaps;
1266 struct spi_nor_read_command reads[SNOR_CMD_READ_MAX];
1267 struct spi_nor_pp_command page_programs[SNOR_CMD_PP_MAX];
1269 int (*quad_enable)(struct spi_nor *nor);
1273 spi_nor_set_read_settings(struct spi_nor_read_command *read,
1277 enum spi_nor_protocol proto)
1279 read->num_mode_clocks = num_mode_clocks;
1280 read->num_wait_states = num_wait_states;
1281 read->opcode = opcode;
1282 read->proto = proto;
1286 spi_nor_set_pp_settings(struct spi_nor_pp_command *pp,
1288 enum spi_nor_protocol proto)
1290 pp->opcode = opcode;
1294 static int spi_nor_init_params(struct spi_nor *nor,
1295 const struct flash_info *info,
1296 struct spi_nor_flash_parameter *params)
1298 /* Set legacy flash parameters as default. */
1299 memset(params, 0, sizeof(*params));
1301 /* Set SPI NOR sizes. */
1302 params->size = info->sector_size * info->n_sectors;
1303 params->page_size = info->page_size;
1305 /* (Fast) Read settings. */
1306 params->hwcaps.mask |= SNOR_HWCAPS_READ;
1307 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ],
1308 0, 0, SPINOR_OP_READ,
1311 if (!(info->flags & SPI_NOR_NO_FR)) {
1312 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
1313 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_FAST],
1314 0, 8, SPINOR_OP_READ_FAST,
1318 if (info->flags & SPI_NOR_DUAL_READ) {
1319 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
1320 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_2],
1321 0, 8, SPINOR_OP_READ_1_1_2,
1325 if (info->flags & SPI_NOR_QUAD_READ) {
1326 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
1327 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_4],
1328 0, 8, SPINOR_OP_READ_1_1_4,
1332 /* Page Program settings. */
1333 params->hwcaps.mask |= SNOR_HWCAPS_PP;
1334 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP],
1335 SPINOR_OP_PP, SNOR_PROTO_1_1_1);
1337 if (info->flags & SPI_NOR_QUAD_READ) {
1338 params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4;
1339 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP_1_1_4],
1340 SPINOR_OP_PP_1_1_4, SNOR_PROTO_1_1_4);
1343 /* Select the procedure to set the Quad Enable bit. */
1344 if (params->hwcaps.mask & (SNOR_HWCAPS_READ_QUAD |
1345 SNOR_HWCAPS_PP_QUAD)) {
1346 switch (JEDEC_MFR(info)) {
1347 #ifdef CONFIG_SPI_FLASH_MACRONIX
1348 case SNOR_MFR_MACRONIX:
1349 params->quad_enable = macronix_quad_enable;
1353 case SNOR_MFR_MICRON:
1357 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
1358 /* Kept only for backward compatibility purpose. */
1359 params->quad_enable = spansion_read_cr_quad_enable;
1366 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
1370 for (i = 0; i < size; i++)
1371 if (table[i][0] == (int)hwcaps)
1377 static int spi_nor_hwcaps_read2cmd(u32 hwcaps)
1379 static const int hwcaps_read2cmd[][2] = {
1380 { SNOR_HWCAPS_READ, SNOR_CMD_READ },
1381 { SNOR_HWCAPS_READ_FAST, SNOR_CMD_READ_FAST },
1382 { SNOR_HWCAPS_READ_1_1_1_DTR, SNOR_CMD_READ_1_1_1_DTR },
1383 { SNOR_HWCAPS_READ_1_1_2, SNOR_CMD_READ_1_1_2 },
1384 { SNOR_HWCAPS_READ_1_2_2, SNOR_CMD_READ_1_2_2 },
1385 { SNOR_HWCAPS_READ_2_2_2, SNOR_CMD_READ_2_2_2 },
1386 { SNOR_HWCAPS_READ_1_2_2_DTR, SNOR_CMD_READ_1_2_2_DTR },
1387 { SNOR_HWCAPS_READ_1_1_4, SNOR_CMD_READ_1_1_4 },
1388 { SNOR_HWCAPS_READ_1_4_4, SNOR_CMD_READ_1_4_4 },
1389 { SNOR_HWCAPS_READ_4_4_4, SNOR_CMD_READ_4_4_4 },
1390 { SNOR_HWCAPS_READ_1_4_4_DTR, SNOR_CMD_READ_1_4_4_DTR },
1391 { SNOR_HWCAPS_READ_1_1_8, SNOR_CMD_READ_1_1_8 },
1392 { SNOR_HWCAPS_READ_1_8_8, SNOR_CMD_READ_1_8_8 },
1393 { SNOR_HWCAPS_READ_8_8_8, SNOR_CMD_READ_8_8_8 },
1394 { SNOR_HWCAPS_READ_1_8_8_DTR, SNOR_CMD_READ_1_8_8_DTR },
1397 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
1398 ARRAY_SIZE(hwcaps_read2cmd));
1401 static int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
1403 static const int hwcaps_pp2cmd[][2] = {
1404 { SNOR_HWCAPS_PP, SNOR_CMD_PP },
1405 { SNOR_HWCAPS_PP_1_1_4, SNOR_CMD_PP_1_1_4 },
1406 { SNOR_HWCAPS_PP_1_4_4, SNOR_CMD_PP_1_4_4 },
1407 { SNOR_HWCAPS_PP_4_4_4, SNOR_CMD_PP_4_4_4 },
1408 { SNOR_HWCAPS_PP_1_1_8, SNOR_CMD_PP_1_1_8 },
1409 { SNOR_HWCAPS_PP_1_8_8, SNOR_CMD_PP_1_8_8 },
1410 { SNOR_HWCAPS_PP_8_8_8, SNOR_CMD_PP_8_8_8 },
1413 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
1414 ARRAY_SIZE(hwcaps_pp2cmd));
1417 static int spi_nor_select_read(struct spi_nor *nor,
1418 const struct spi_nor_flash_parameter *params,
1421 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
1422 const struct spi_nor_read_command *read;
1427 cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
1431 read = ¶ms->reads[cmd];
1432 nor->read_opcode = read->opcode;
1433 nor->read_proto = read->proto;
1436 * In the spi-nor framework, we don't need to make the difference
1437 * between mode clock cycles and wait state clock cycles.
1438 * Indeed, the value of the mode clock cycles is used by a QSPI
1439 * flash memory to know whether it should enter or leave its 0-4-4
1440 * (Continuous Read / XIP) mode.
1441 * eXecution In Place is out of the scope of the mtd sub-system.
1442 * Hence we choose to merge both mode and wait state clock cycles
1443 * into the so called dummy clock cycles.
1445 nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
1449 static int spi_nor_select_pp(struct spi_nor *nor,
1450 const struct spi_nor_flash_parameter *params,
1453 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
1454 const struct spi_nor_pp_command *pp;
1459 cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
1463 pp = ¶ms->page_programs[cmd];
1464 nor->program_opcode = pp->opcode;
1465 nor->write_proto = pp->proto;
1469 static int spi_nor_select_erase(struct spi_nor *nor,
1470 const struct flash_info *info)
1472 struct mtd_info *mtd = &nor->mtd;
1474 #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
1475 /* prefer "small sector" erase if possible */
1476 if (info->flags & SECT_4K) {
1477 nor->erase_opcode = SPINOR_OP_BE_4K;
1478 mtd->erasesize = 4096;
1479 } else if (info->flags & SECT_4K_PMC) {
1480 nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
1481 mtd->erasesize = 4096;
1485 nor->erase_opcode = SPINOR_OP_SE;
1486 mtd->erasesize = info->sector_size;
1491 static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
1492 const struct spi_nor_flash_parameter *params,
1493 const struct spi_nor_hwcaps *hwcaps)
1495 u32 ignored_mask, shared_mask;
1496 bool enable_quad_io;
1500 * Keep only the hardware capabilities supported by both the SPI
1501 * controller and the SPI flash memory.
1503 shared_mask = hwcaps->mask & params->hwcaps.mask;
1505 /* SPI n-n-n protocols are not supported yet. */
1506 ignored_mask = (SNOR_HWCAPS_READ_2_2_2 |
1507 SNOR_HWCAPS_READ_4_4_4 |
1508 SNOR_HWCAPS_READ_8_8_8 |
1509 SNOR_HWCAPS_PP_4_4_4 |
1510 SNOR_HWCAPS_PP_8_8_8);
1511 if (shared_mask & ignored_mask) {
1513 "SPI n-n-n protocols are not supported yet.\n");
1514 shared_mask &= ~ignored_mask;
1517 /* Select the (Fast) Read command. */
1518 err = spi_nor_select_read(nor, params, shared_mask);
1521 "can't select read settings supported by both the SPI controller and memory.\n");
1525 /* Select the Page Program command. */
1526 err = spi_nor_select_pp(nor, params, shared_mask);
1529 "can't select write settings supported by both the SPI controller and memory.\n");
1533 /* Select the Sector Erase command. */
1534 err = spi_nor_select_erase(nor, info);
1537 "can't select erase settings supported by both the SPI controller and memory.\n");
1541 /* Enable Quad I/O if needed. */
1542 enable_quad_io = (spi_nor_get_protocol_width(nor->read_proto) == 4 ||
1543 spi_nor_get_protocol_width(nor->write_proto) == 4);
1544 if (enable_quad_io && params->quad_enable)
1545 nor->quad_enable = params->quad_enable;
1547 nor->quad_enable = NULL;
1552 static int spi_nor_init(struct spi_nor *nor)
1557 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
1558 * with the software protection bits set
1560 if (JEDEC_MFR(nor->info) == SNOR_MFR_ATMEL ||
1561 JEDEC_MFR(nor->info) == SNOR_MFR_INTEL ||
1562 JEDEC_MFR(nor->info) == SNOR_MFR_SST ||
1563 nor->info->flags & SPI_NOR_HAS_LOCK) {
1566 spi_nor_wait_till_ready(nor);
1569 if (nor->quad_enable) {
1570 err = nor->quad_enable(nor);
1572 dev_dbg(nor->dev, "quad mode not supported\n");
1580 int spi_nor_scan(struct spi_nor *nor)
1582 struct spi_nor_flash_parameter params;
1583 const struct flash_info *info = NULL;
1584 struct mtd_info *mtd = &nor->mtd;
1585 struct spi_nor_hwcaps hwcaps = {
1586 .mask = SNOR_HWCAPS_READ |
1587 SNOR_HWCAPS_READ_FAST |
1590 struct spi_slave *spi = nor->spi;
1593 /* Reset SPI protocol for all commands. */
1594 nor->reg_proto = SNOR_PROTO_1_1_1;
1595 nor->read_proto = SNOR_PROTO_1_1_1;
1596 nor->write_proto = SNOR_PROTO_1_1_1;
1597 nor->read = spi_nor_read_data;
1598 nor->write = spi_nor_write_data;
1599 nor->read_reg = spi_nor_read_reg;
1600 nor->write_reg = spi_nor_write_reg;
1602 if (spi->mode & SPI_RX_QUAD) {
1603 hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
1605 if (spi->mode & SPI_TX_QUAD)
1606 hwcaps.mask |= (SNOR_HWCAPS_READ_1_4_4 |
1607 SNOR_HWCAPS_PP_1_1_4 |
1608 SNOR_HWCAPS_PP_1_4_4);
1609 } else if (spi->mode & SPI_RX_DUAL) {
1610 hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
1612 if (spi->mode & SPI_TX_DUAL)
1613 hwcaps.mask |= SNOR_HWCAPS_READ_1_2_2;
1616 info = spi_nor_read_id(nor);
1617 if (IS_ERR_OR_NULL(info))
1620 ret = spi_nor_init_params(nor, info, ¶ms);
1625 mtd->name = info->name;
1627 mtd->type = MTD_NORFLASH;
1629 mtd->flags = MTD_CAP_NORFLASH;
1630 mtd->size = params.size;
1631 mtd->_erase = spi_nor_erase;
1632 mtd->_read = spi_nor_read;
1634 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
1635 /* NOR protection support for STmicro/Micron chips and similar */
1636 if (JEDEC_MFR(info) == SNOR_MFR_ST ||
1637 JEDEC_MFR(info) == SNOR_MFR_MICRON ||
1638 JEDEC_MFR(info) == SNOR_MFR_SST ||
1639 info->flags & SPI_NOR_HAS_LOCK) {
1640 nor->flash_lock = stm_lock;
1641 nor->flash_unlock = stm_unlock;
1642 nor->flash_is_locked = stm_is_locked;
1646 #ifdef CONFIG_SPI_FLASH_SST
1647 /* sst nor chips use AAI word program */
1648 if (info->flags & SST_WRITE)
1649 mtd->_write = sst_write;
1652 mtd->_write = spi_nor_write;
1654 if (info->flags & USE_FSR)
1655 nor->flags |= SNOR_F_USE_FSR;
1656 if (info->flags & SPI_NOR_HAS_TB)
1657 nor->flags |= SNOR_F_HAS_SR_TB;
1658 if (info->flags & NO_CHIP_ERASE)
1659 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
1660 if (info->flags & USE_CLSR)
1661 nor->flags |= SNOR_F_USE_CLSR;
1663 if (info->flags & SPI_NOR_NO_ERASE)
1664 mtd->flags |= MTD_NO_ERASE;
1666 nor->page_size = params.page_size;
1667 mtd->writebufsize = nor->page_size;
1669 /* Some devices cannot do fast-read, no matter what DT tells us */
1670 if ((info->flags & SPI_NOR_NO_FR) || (spi->mode & SPI_RX_SLOW))
1671 params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
1674 * Configure the SPI memory:
1675 * - select op codes for (Fast) Read, Page Program and Sector Erase.
1676 * - set the number of dummy cycles (mode cycles + wait states).
1677 * - set the SPI protocols for register and memory accesses.
1678 * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
1680 ret = spi_nor_setup(nor, info, ¶ms, &hwcaps);
1684 if (info->addr_width) {
1685 nor->addr_width = info->addr_width;
1687 nor->addr_width = 3;
1690 if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
1691 dev_dbg(dev, "address width is too large: %u\n",
1696 /* Send all the required SPI flash commands to initialize device */
1698 ret = spi_nor_init(nor);
1702 nor->name = mtd->name;
1703 nor->size = mtd->size;
1704 nor->erase_size = mtd->erasesize;
1705 nor->sector_size = mtd->erasesize;
1707 #ifndef CONFIG_SPL_BUILD
1708 printf("SF: Detected %s with page size ", nor->name);
1709 print_size(nor->page_size, ", erase size ");
1710 print_size(nor->erase_size, ", total ");
1711 print_size(nor->size, "");