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>
21 #include <linux/mtd/mtd.h>
22 #include <linux/of_platform.h>
23 #include <linux/spi/flash.h>
24 #include <linux/mtd/spi-nor.h>
25 #include <linux/spi/spi.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 SST_GLOBAL_PROT_UNLK BIT(10) /* Unlock the Global protection for
85 #define JEDEC_MFR(info) ((info)->id[0])
87 static const struct flash_info *spi_nor_match_id(const char *name);
90 * Read the status register, returning its value in the location
91 * Return the status register value.
92 * Returns negative if error occurred.
94 static int read_sr(struct spi_nor *nor)
99 if (nor->isparallel) {
100 ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val[0], 2);
102 pr_err("error %d reading SR\n", (int) ret);
107 ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val[0], 1);
109 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 if (nor->isparallel) {
128 ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val[0], 2);
130 pr_err("error %d reading FSR\n", ret);
135 ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val[0], 1);
137 pr_err("error %d reading FSR\n", ret);
146 * Read configuration register, returning its value in the
147 * location. Return the configuration register value.
148 * Returns negative if error occured.
150 static int read_cr(struct spi_nor *nor)
155 ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
157 dev_err(nor->dev, "error %d reading CR\n", ret);
165 * Dummy Cycle calculation for different type of read.
166 * It can be used to support more commands with
167 * different dummy cycle requirements.
169 static inline int spi_nor_read_dummy_cycles(struct spi_nor *nor)
171 switch (nor->flash_read) {
183 * Write status register 1 byte
184 * Returns negative if error occurred.
186 static inline int write_sr(struct spi_nor *nor, u8 val)
188 nor->cmd_buf[0] = val;
189 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
193 * Write status Register and configuration register with 2 bytes
194 * The first byte will be written to the status register, while the
195 * second byte will be written to the configuration register.
196 * Return negative if error occured.
198 static int write_sr_cr(struct spi_nor *nor, u16 val)
200 nor->cmd_buf[0] = val & 0xff;
201 nor->cmd_buf[1] = (val >> 8);
203 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 2);
207 * Set write enable latch with Write Enable command.
208 * Returns negative if error occurred.
210 static inline int write_enable(struct spi_nor *nor)
212 return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
216 * Send write disble instruction to the chip.
218 static inline int write_disable(struct spi_nor *nor)
220 return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
223 static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
228 /* Enable/disable 4-byte addressing mode. */
229 static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info,
233 bool need_wren = false;
236 switch (JEDEC_MFR(info)) {
237 case SNOR_MFR_MICRON:
238 /* Some Micron need WREN command; all will accept it */
240 case SNOR_MFR_MACRONIX:
241 case SNOR_MFR_WINBOND:
245 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
246 status = nor->write_reg(nor, cmd, NULL, 0);
253 nor->cmd_buf[0] = enable << 7;
254 return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
259 * read_ear - Get the extended/bank address register value
260 * @nor: Pointer to the flash control structure
262 * This routine reads the Extended/bank address register value
264 * Return: Negative if error occured.
266 static int read_ear(struct spi_nor *nor, struct flash_info *info)
272 /* This is actually Spansion */
273 if (JEDEC_MFR(info) == CFI_MFR_AMD)
274 code = SPINOR_OP_BRRD;
275 /* This is actually Micron */
276 else if (JEDEC_MFR(info) == CFI_MFR_ST)
277 code = SPINOR_OP_RDEAR;
281 ret = nor->read_reg(nor, code, &val, 1);
289 static inline int spi_nor_sr_ready(struct spi_nor *nor)
291 int sr = read_sr(nor);
295 return !(sr & SR_WIP);
298 static inline int spi_nor_fsr_ready(struct spi_nor *nor)
300 int fsr = read_fsr(nor);
304 return fsr & FSR_READY;
307 static int spi_nor_ready(struct spi_nor *nor)
310 sr = spi_nor_sr_ready(nor);
313 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
320 * Service routine to read status register until ready, or timeout occurs.
321 * Returns non-zero if error.
323 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
324 unsigned long timeout_jiffies)
326 unsigned long deadline;
327 int timeout = 0, ret;
329 deadline = jiffies + timeout_jiffies;
332 if (time_after_eq(jiffies, deadline))
335 ret = spi_nor_ready(nor);
344 dev_err(nor->dev, "flash operation timed out\n");
349 static int spi_nor_wait_till_ready(struct spi_nor *nor)
351 return spi_nor_wait_till_ready_with_timeout(nor,
352 DEFAULT_READY_WAIT_JIFFIES);
356 * Update Extended Address/bank selection Register.
357 * Call with flash->lock locked.
359 static int write_ear(struct spi_nor *nor, u32 addr)
364 struct mtd_info *mtd = &nor->mtd;
366 /* Wait until finished previous write command. */
367 if (spi_nor_wait_till_ready(nor))
370 if (mtd->size <= (0x1000000) << nor->shift)
373 addr = addr % (u32) mtd->size;
376 if ((!nor->isstacked) && (ear == nor->curbank))
379 if (nor->isstacked && (mtd->size <= 0x2000000))
382 if (nor->jedec_id == CFI_MFR_AMD)
383 code = SPINOR_OP_BRWR;
384 if (nor->jedec_id == CFI_MFR_ST) {
386 code = SPINOR_OP_WREAR;
388 nor->cmd_buf[0] = ear;
390 ret = nor->write_reg(nor, code, nor->cmd_buf, 1);
400 * Erase the whole flash memory
402 * Returns 0 if successful, non-zero otherwise.
404 static int erase_chip(struct spi_nor *nor)
408 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10));
411 nor->spi->master->flags &= ~SPI_MASTER_U_PAGE;
413 ret = nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0);
417 if (nor->isstacked) {
418 /* Wait until previous write command finished */
419 ret = spi_nor_wait_till_ready(nor);
423 nor->spi->master->flags |= SPI_MASTER_U_PAGE;
425 ret = nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0);
431 static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)
435 mutex_lock(&nor->lock);
438 ret = nor->prepare(nor, ops);
440 dev_err(nor->dev, "failed in the preparation.\n");
441 mutex_unlock(&nor->lock);
448 static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
451 nor->unprepare(nor, ops);
452 mutex_unlock(&nor->lock);
456 * Initiate the erasure of a single sector
458 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
460 u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
464 return nor->erase(nor, addr);
467 * Default implementation, if driver doesn't have a specialized HW
470 for (i = nor->addr_width - 1; i >= 0; i--) {
471 buf[i] = addr & 0xff;
475 return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
479 * Erase an address range on the nor chip. The address range may extend
480 * one or more erase sectors. Return an error is there is a problem erasing.
482 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
484 struct spi_nor *nor = mtd_to_spi_nor(mtd);
485 u32 addr, len, offset;
489 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
490 (long long)instr->len);
492 div_u64_rem(instr->len, mtd->erasesize, &rem);
499 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_ERASE);
503 /* whole-chip erase? */
504 if (len == mtd->size) {
505 unsigned long timeout;
509 if (erase_chip(nor)) {
515 * Scale the timeout linearly with the size of the flash, with
516 * a minimum calibrated to an old 2MB flash. We could try to
517 * pull these from CFI/SFDP, but these values should be good
520 timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES,
521 CHIP_ERASE_2MB_READY_WAIT_JIFFIES *
522 (unsigned long)(mtd->size / SZ_2M));
523 ret = spi_nor_wait_till_ready_with_timeout(nor, timeout);
527 /* REVISIT in some cases we could speed up erasing large regions
528 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
529 * to use "small sector erase", but that's not always optimal.
532 /* "sector"-at-a-time erase */
538 if (nor->isparallel == 1)
541 if (nor->isstacked == 1) {
542 if (offset >= (mtd->size / 2)) {
543 offset = offset - (mtd->size / 2);
544 nor->spi->master->flags |=
547 nor->spi->master->flags &=
551 if (nor->addr_width == 3) {
552 /* Update Extended Address Register */
553 ret = write_ear(nor, offset);
557 ret = spi_nor_wait_till_ready(nor);
563 ret = spi_nor_erase_sector(nor, offset);
567 addr += mtd->erasesize;
568 len -= mtd->erasesize;
570 ret = spi_nor_wait_till_ready(nor);
579 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
581 instr->state = ret ? MTD_ERASE_FAILED : MTD_ERASE_DONE;
582 mtd_erase_callback(instr);
587 static inline uint16_t min_lockable_sectors(struct spi_nor *nor,
590 uint16_t lock_granularity;
593 * Revisit - SST (not used by us) has the same JEDEC ID as micron but
594 * protected area table is similar to that of spansion.
596 lock_granularity = max(1, n_sectors/M25P_MAX_LOCKABLE_SECTORS);
597 if (nor->jedec_id == CFI_MFR_ST) /* Micron */
598 lock_granularity = 1;
600 return lock_granularity;
603 static inline uint32_t get_protected_area_start(struct spi_nor *nor,
609 struct mtd_info *mtd = &nor->mtd;
611 n_sectors = nor->n_sectors;
612 sector_size = nor->sector_size;
613 mtd_size = mtd->size;
615 if (nor->isparallel) {
616 sector_size = (nor->sector_size >> 1);
617 mtd_size = (mtd->size >> 1);
619 if (nor->isstacked) {
620 n_sectors = (nor->n_sectors >> 1);
621 mtd_size = (mtd->size >> 1);
624 return mtd_size - (1<<(lock_bits-1)) *
625 min_lockable_sectors(nor, n_sectors) * sector_size;
628 static uint8_t min_protected_area_including_offset(struct spi_nor *nor,
631 uint8_t lock_bits, lockbits_limit;
634 * Revisit - SST (not used by us) has the same JEDEC ID as micron but
635 * protected area table is similar to that of spansion.
636 * Mircon has 4 block protect bits.
639 if (nor->jedec_id == CFI_MFR_ST) /* Micron */
642 for (lock_bits = 1; lock_bits < lockbits_limit; lock_bits++) {
643 if (offset >= get_protected_area_start(nor, lock_bits))
649 static int write_sr_modify_protection(struct spi_nor *nor, uint8_t status,
652 uint8_t status_new, bp_mask;
655 status_new = status & ~SR_BP_BIT_MASK;
656 bp_mask = (lock_bits << SR_BP_BIT_OFFSET) & SR_BP_BIT_MASK;
659 if (nor->jedec_id == CFI_MFR_ST) {
660 /* To support chips with more than 896 sectors (56MB) */
661 status_new &= ~SR_BP3;
663 /* Protected area starts from top */
664 status_new &= ~SR_BP_TB;
670 status_new |= bp_mask;
674 /* For spansion flashes */
675 if (nor->jedec_id == CFI_MFR_AMD) {
676 val = read_cr(nor) << 8;
678 if (write_sr_cr(nor, val) < 0)
681 if (write_sr(nor, status_new) < 0)
687 static uint8_t bp_bits_from_sr(struct spi_nor *nor, uint8_t status)
691 ret = (((status) & SR_BP_BIT_MASK) >> SR_BP_BIT_OFFSET);
692 if (nor->jedec_id == 0x20)
693 ret |= ((status & SR_BP3) >> (SR_BP_BIT_OFFSET + 1));
699 static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
702 struct mtd_info *mtd = &nor->mtd;
703 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
704 int shift = ffs(mask) - 1;
712 pow = ((sr & mask) ^ mask) >> shift;
713 *len = mtd->size >> pow;
714 if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
717 *ofs = mtd->size - *len;
722 * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
723 * @locked is false); 0 otherwise
725 static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
734 stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
737 /* Requested range is a sub-range of locked range */
738 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
740 /* Requested range does not overlap with locked range */
741 return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
744 static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
747 return stm_check_lock_status_sr(nor, ofs, len, sr, true);
750 static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
753 return stm_check_lock_status_sr(nor, ofs, len, sr, false);
757 * Lock a region of the flash. Compatible with ST Micro and similar flash.
758 * Supports the block protection bits BP{0,1,2} in the status register
759 * (SR). Does not support these features found in newer SR bitfields:
760 * - SEC: sector/block protect - only handle SEC=0 (block protect)
761 * - CMP: complement protect - only support CMP=0 (range is not complemented)
763 * Support for the following is provided conditionally for some flash:
764 * - TB: top/bottom protect
766 * Sample table portion for 8MB flash (Winbond w25q64fw):
768 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
769 * --------------------------------------------------------------------------
770 * X | X | 0 | 0 | 0 | NONE | NONE
771 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
772 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
773 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
774 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
775 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
776 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
777 * X | X | 1 | 1 | 1 | 8 MB | ALL
778 * ------|-------|-------|-------|-------|---------------|-------------------
779 * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
780 * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
781 * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
782 * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
783 * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
784 * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
786 * Returns negative on errors, 0 on success.
788 static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
790 struct mtd_info *mtd = &nor->mtd;
791 int status_old, status_new;
792 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
793 u8 shift = ffs(mask) - 1, pow, val;
795 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
799 ofs = ofs >> nor->shift;
801 status_old = read_sr(nor);
805 /* If nothing in our range is unlocked, we don't need to do anything */
806 if (stm_is_locked_sr(nor, ofs, len, status_old))
809 /* If anything below us is unlocked, we can't use 'bottom' protection */
810 if (!stm_is_locked_sr(nor, 0, ofs, status_old))
811 can_be_bottom = false;
813 /* If anything above us is unlocked, we can't use 'top' protection */
814 if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
818 if (!can_be_bottom && !can_be_top)
821 /* Prefer top, if both are valid */
822 use_top = can_be_top;
824 /* lock_len: length of region that should end up locked */
826 lock_len = mtd->size - ofs;
828 lock_len = ofs + len;
831 * Need smallest pow such that:
833 * 1 / (2^pow) <= (len / size)
835 * so (assuming power-of-2 size) we do:
837 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
839 pow = ilog2(mtd->size) - ilog2(lock_len);
840 val = mask - (pow << shift);
843 /* Don't "lock" with no region! */
847 status_new = (status_old & ~mask & ~SR_TB) | val;
849 /* Disallow further writes if WP pin is asserted */
850 status_new |= SR_SRWD;
855 /* Don't bother if they're the same */
856 if (status_new == status_old)
859 /* Only modify protection if it will not unlock other areas */
860 if ((status_new & mask) < (status_old & mask))
864 ret = write_sr(nor, status_new);
867 return spi_nor_wait_till_ready(nor);
871 * Unlock a region of the flash. See stm_lock() for more info
873 * Returns negative on errors, 0 on success.
875 static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
877 struct mtd_info *mtd = &nor->mtd;
878 int status_old, status_new;
879 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
880 u8 shift = ffs(mask) - 1, pow, val;
882 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
886 ofs = ofs >> nor->shift;
888 status_old = read_sr(nor);
892 /* If nothing in our range is locked, we don't need to do anything */
893 if (stm_is_unlocked_sr(nor, ofs, len, status_old))
896 /* If anything below us is locked, we can't use 'top' protection */
897 if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
900 /* If anything above us is locked, we can't use 'bottom' protection */
901 if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
903 can_be_bottom = false;
905 if (!can_be_bottom && !can_be_top)
908 /* Prefer top, if both are valid */
909 use_top = can_be_top;
911 /* lock_len: length of region that should remain locked */
913 lock_len = mtd->size - (ofs + len);
918 * Need largest pow such that:
920 * 1 / (2^pow) >= (len / size)
922 * so (assuming power-of-2 size) we do:
924 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
926 pow = ilog2(mtd->size) - order_base_2(lock_len);
928 val = 0; /* fully unlocked */
930 val = mask - (pow << shift);
931 /* Some power-of-two sizes are not supported */
936 status_new = (status_old & ~mask & ~SR_TB) | val;
938 /* Don't protect status register if we're fully unlocked */
940 status_new &= ~SR_SRWD;
945 /* Don't bother if they're the same */
946 if (status_new == status_old)
949 /* Only modify protection if it will not lock other areas */
950 if ((status_new & mask) > (status_old & mask))
954 ret = write_sr(nor, status_new);
957 return spi_nor_wait_till_ready(nor);
961 * Check if a region of the flash is (completely) locked. See stm_lock() for
964 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
965 * negative on errors.
967 static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
971 status = read_sr(nor);
975 return stm_is_locked_sr(nor, ofs, len, status);
978 static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
980 struct spi_nor *nor = mtd_to_spi_nor(mtd);
985 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_LOCK);
989 if (nor->isparallel == 1)
990 ofs = ofs >> nor->shift;
992 if (nor->isstacked == 1) {
993 if (ofs >= (mtd->size / 2)) {
994 ofs = ofs - (mtd->size / 2);
995 nor->spi->master->flags |= SPI_MASTER_U_PAGE;
997 nor->spi->master->flags &= ~SPI_MASTER_U_PAGE;
1000 ret = nor->flash_lock(nor, ofs, len);
1001 /* Wait until finished previous command */
1002 ret = spi_nor_wait_till_ready(nor);
1006 status = read_sr(nor);
1008 lock_bits = min_protected_area_including_offset(nor, ofs);
1010 /* Only modify protection if it will not unlock other areas */
1011 if (lock_bits > bp_bits_from_sr(nor, status))
1012 ret = write_sr_modify_protection(nor, status, lock_bits);
1014 dev_err(nor->dev, "trying to unlock already locked area\n");
1016 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
1020 static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1022 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1027 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
1031 if (nor->isparallel == 1)
1032 ofs = ofs >> nor->shift;
1034 if (nor->isstacked == 1) {
1035 if (ofs >= (mtd->size / 2)) {
1036 ofs = ofs - (mtd->size / 2);
1037 nor->spi->master->flags |= SPI_MASTER_U_PAGE;
1039 nor->spi->master->flags &= ~SPI_MASTER_U_PAGE;
1042 ret = nor->flash_unlock(nor, ofs, len);
1043 /* Wait until finished previous command */
1044 ret = spi_nor_wait_till_ready(nor);
1048 status = read_sr(nor);
1050 lock_bits = min_protected_area_including_offset(nor, ofs+len) - 1;
1052 /* Only modify protection if it will not lock other areas */
1053 if (lock_bits < bp_bits_from_sr(nor, status))
1054 ret = write_sr_modify_protection(nor, status, lock_bits);
1056 dev_err(nor->dev, "trying to lock already unlocked area\n");
1058 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
1062 static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1064 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1067 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
1071 ret = nor->flash_is_locked(nor, ofs, len);
1073 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
1077 /* Used when the "_ext_id" is two bytes at most */
1078 #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
1080 ((_jedec_id) >> 16) & 0xff, \
1081 ((_jedec_id) >> 8) & 0xff, \
1082 (_jedec_id) & 0xff, \
1083 ((_ext_id) >> 8) & 0xff, \
1086 .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))), \
1087 .sector_size = (_sector_size), \
1088 .n_sectors = (_n_sectors), \
1092 #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
1094 ((_jedec_id) >> 16) & 0xff, \
1095 ((_jedec_id) >> 8) & 0xff, \
1096 (_jedec_id) & 0xff, \
1097 ((_ext_id) >> 16) & 0xff, \
1098 ((_ext_id) >> 8) & 0xff, \
1102 .sector_size = (_sector_size), \
1103 .n_sectors = (_n_sectors), \
1107 #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
1108 .sector_size = (_sector_size), \
1109 .n_sectors = (_n_sectors), \
1110 .page_size = (_page_size), \
1111 .addr_width = (_addr_width), \
1114 /* NOTE: double check command sets and memory organization when you add
1115 * more nor chips. This current list focusses on newer chips, which
1116 * have been converging on command sets which including JEDEC ID.
1118 * All newly added entries should describe *hardware* and should use SECT_4K
1119 * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage
1120 * scenarios excluding small sectors there is config option that can be
1121 * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS.
1122 * For historical (and compatibility) reasons (before we got above config) some
1123 * old entries may be missing 4K flag.
1125 static const struct flash_info spi_nor_ids[] = {
1126 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
1127 { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
1128 { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
1130 { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
1131 { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
1132 { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
1134 { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
1135 { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
1136 { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
1137 { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
1139 { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
1141 /* EON -- en25xxx */
1142 { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
1143 { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
1144 { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
1145 { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
1146 { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
1147 { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
1148 { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
1149 { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K) },
1152 { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K) },
1155 { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1156 { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1159 { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE) },
1163 "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64,
1164 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1165 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1168 "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128,
1169 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1170 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1173 "gd25lq64c", INFO(0xc86017, 0, 64 * 1024, 128,
1174 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1175 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1178 "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256,
1179 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1180 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1183 /* Intel/Numonyx -- xxxs33b */
1184 { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
1185 { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
1186 { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
1189 { "is25lp256d", INFO(0x9d6019, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_FSR | SPI_NOR_HAS_LOCK) },
1190 { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K) },
1193 { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K) },
1194 { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
1195 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
1196 { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
1197 { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
1198 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, SECT_4K) },
1199 { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) },
1200 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, SECT_4K) },
1201 { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
1202 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
1203 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
1204 { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, 0) },
1205 { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
1206 { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_QUAD_READ) },
1207 { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ) },
1210 { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
1211 { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
1212 { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
1213 { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
1214 { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ | USE_FSR | SPI_NOR_HAS_LOCK) },
1215 { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ | USE_FSR | SPI_NOR_HAS_LOCK) },
1216 { "n25q256a", INFO(0x20bb19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ | USE_FSR| SPI_NOR_HAS_LOCK) },
1217 { "n25q256a13", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_FSR | SPI_NOR_HAS_LOCK) },
1218 { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | SPI_NOR_HAS_LOCK) },
1219 { "n25q512a13", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_FSR | SPI_NOR_HAS_LOCK) },
1220 { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | SPI_NOR_HAS_LOCK) },
1221 { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | SPI_NOR_HAS_LOCK) },
1222 { "n25q00a", INFO(0x20bb21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | SPI_NOR_HAS_LOCK) },
1225 { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
1226 { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
1227 { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
1229 /* Spansion -- single (large) sector size only, at least
1230 * for the chips listed here (without boot sectors).
1232 { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1233 { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1234 { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_HAS_LOCK) },
1235 { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1236 { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1237 { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1238 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, SPI_NOR_HAS_LOCK) },
1239 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, SPI_NOR_HAS_LOCK) },
1240 { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
1241 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_HAS_LOCK) },
1242 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_HAS_LOCK) },
1243 { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
1244 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
1245 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
1246 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
1247 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
1248 { "s25fl004k", INFO(0xef4013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1249 { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1250 { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1251 { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
1252 { "s25fl116k", INFO(0x014015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1253 { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) },
1254 { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
1255 { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ) },
1256 { "sst26wf016B", INFO(0xbf2651, 0, 64 * 1024, 32, SECT_4K | SST_GLOBAL_PROT_UNLK) },
1258 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
1259 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
1260 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
1261 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
1262 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
1263 { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
1264 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
1265 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
1266 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
1267 { "sst25wf020a", INFO(0x621612, 0, 64 * 1024, 4, SECT_4K) },
1268 { "sst25wf040b", INFO(0x621613, 0, 64 * 1024, 8, SECT_4K) },
1269 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
1270 { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
1272 /* ST Microelectronics -- newer production may have feature updates */
1273 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
1274 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
1275 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
1276 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
1277 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
1278 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
1279 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
1280 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
1281 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
1283 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
1284 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
1285 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
1286 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
1287 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
1288 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
1289 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
1290 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
1291 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
1293 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
1294 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
1295 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
1297 { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
1298 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
1299 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
1301 { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
1302 { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
1303 { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
1304 { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
1305 { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
1306 { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
1308 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
1309 { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
1310 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
1311 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
1312 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
1313 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
1314 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
1315 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
1316 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
1318 "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64,
1319 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1320 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1322 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
1323 { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
1325 "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128,
1326 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1327 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1330 "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256,
1331 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1332 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1334 { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
1335 { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
1336 { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K| SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1337 { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K| SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1339 /* Catalyst / On Semiconductor -- non-JEDEC */
1340 { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1341 { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1342 { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1343 { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1344 { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1348 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
1351 u8 id[SPI_NOR_MAX_ID_LEN];
1352 const struct flash_info *info;
1354 tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
1356 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
1357 return ERR_PTR(tmp);
1360 for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
1361 info = &spi_nor_ids[tmp];
1363 if (!memcmp(info->id, id, info->id_len))
1364 return &spi_nor_ids[tmp];
1367 dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
1368 id[0], id[1], id[2]);
1369 return ERR_PTR(-ENODEV);
1372 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
1373 size_t *retlen, u_char *buf)
1375 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1378 u32 stack_shift = 0;
1380 u32 rem_bank_len = 0;
1384 #define OFFSET_16_MB 0x1000000
1386 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
1388 if ((nor->isparallel) && (offset & 1)) {
1389 /* We can hit this case when we use file system like ubifs */
1390 from = (loff_t)(from - 1);
1391 len = (size_t)(len + 1);
1395 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ);
1401 if (nor->addr_width == 3) {
1402 bank = (u32)from / (OFFSET_16_MB << nor->shift);
1403 rem_bank_len = ((OFFSET_16_MB << nor->shift) *
1408 if (nor->isparallel == 1)
1411 if (nor->isstacked == 1) {
1413 if (offset >= (mtd->size / 2)) {
1414 offset = offset - (mtd->size / 2);
1415 nor->spi->master->flags |= SPI_MASTER_U_PAGE;
1417 nor->spi->master->flags &= ~SPI_MASTER_U_PAGE;
1421 /* Die cross over issue is not handled */
1422 if (nor->addr_width == 4) {
1423 rem_bank_len = (mtd->size >> stack_shift) -
1424 (offset << nor->shift);
1426 if (nor->addr_width == 3)
1427 write_ear(nor, offset);
1428 if (len < rem_bank_len)
1431 read_len = rem_bank_len;
1433 /* Wait till previous write/erase is done. */
1434 ret = spi_nor_wait_till_ready(nor);
1438 ret = nor->read(nor, offset, read_len, buf);
1440 /* We shouldn't see 0-length reads */
1448 if (is_ofst_odd == 1) {
1449 memcpy(buf, (buf + 1), (len - 1));
1450 *retlen += (ret - 1);
1461 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
1465 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
1466 size_t *retlen, const u_char *buf)
1468 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1472 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1474 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
1480 nor->sst_write_second = false;
1483 /* Start write from odd address. */
1485 nor->program_opcode = SPINOR_OP_BP;
1487 /* write one byte. */
1488 ret = nor->write(nor, to, 1, buf);
1491 WARN(ret != 1, "While writing 1 byte written %i bytes\n",
1493 ret = spi_nor_wait_till_ready(nor);
1499 /* Write out most of the data here. */
1500 for (; actual < len - 1; actual += 2) {
1501 nor->program_opcode = SPINOR_OP_AAI_WP;
1503 /* write two bytes. */
1504 ret = nor->write(nor, to, 2, buf + actual);
1507 WARN(ret != 2, "While writing 2 bytes written %i bytes\n",
1509 ret = spi_nor_wait_till_ready(nor);
1513 nor->sst_write_second = true;
1515 nor->sst_write_second = false;
1518 ret = spi_nor_wait_till_ready(nor);
1522 /* Write out trailing byte if it exists. */
1523 if (actual != len) {
1526 nor->program_opcode = SPINOR_OP_BP;
1527 ret = nor->write(nor, to, 1, buf + actual);
1530 WARN(ret != 1, "While writing 1 byte written %i bytes\n",
1532 ret = spi_nor_wait_till_ready(nor);
1540 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
1545 * Write an address range to the nor chip. Data must be written in
1546 * FLASH_PAGESIZE chunks. The address range may be any size provided
1547 * it is within the physical boundaries.
1549 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
1550 size_t *retlen, const u_char *buf)
1552 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1553 size_t page_offset, page_remain, i;
1555 u32 offset, stack_shift=0;
1557 u32 rem_bank_len = 0;
1559 #define OFFSET_16_MB 0x1000000
1561 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1562 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
1565 for (i = 0; i < len; ) {
1568 if (nor->addr_width == 3) {
1569 bank = (u32)to / (OFFSET_16_MB << nor->shift);
1570 rem_bank_len = ((OFFSET_16_MB << nor->shift) *
1574 page_offset = ((to + i)) & (nor->page_size - 1);
1578 if (nor->isparallel == 1)
1581 if (nor->isstacked == 1) {
1583 if (offset >= (mtd->size / 2)) {
1584 offset = offset - (mtd->size / 2);
1585 nor->spi->master->flags |= SPI_MASTER_U_PAGE;
1587 nor->spi->master->flags &= ~SPI_MASTER_U_PAGE;
1591 /* Die cross over issue is not handled */
1592 if (nor->addr_width == 4)
1593 rem_bank_len = (mtd->size >> stack_shift) - offset;
1594 if (nor->addr_width == 3)
1595 write_ear(nor, (offset >> nor->shift));
1596 if (len < rem_bank_len) {
1597 page_remain = min_t(size_t,
1598 nor->page_size - page_offset, len - i);
1602 /* the size of data remaining on the first page */
1603 page_remain = rem_bank_len;
1605 ret = spi_nor_wait_till_ready(nor);
1611 ret = nor->write(nor, (offset), page_remain, buf + i);
1616 ret = spi_nor_wait_till_ready(nor);
1621 if (written != page_remain) {
1623 "While writing %zu bytes written %zd bytes\n",
1624 page_remain, written);
1631 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
1635 static int macronix_quad_enable(struct spi_nor *nor)
1644 write_sr(nor, val | SR_QUAD_EN_MX);
1646 if (spi_nor_wait_till_ready(nor))
1650 if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
1651 dev_err(nor->dev, "Macronix Quad bit not set\n");
1658 static int spansion_quad_enable(struct spi_nor *nor)
1661 int quad_en = CR_QUAD_EN_SPAN << 8;
1663 quad_en |= read_sr(nor);
1664 quad_en |= (read_cr(nor) << 8);
1668 ret = write_sr_cr(nor, quad_en);
1671 "error while writing configuration register\n");
1675 /* read back and check it */
1677 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
1678 dev_err(nor->dev, "Spansion Quad bit not set\n");
1685 static int set_quad_mode(struct spi_nor *nor, const struct flash_info *info)
1689 switch (JEDEC_MFR(info)) {
1690 case SNOR_MFR_MACRONIX:
1692 status = macronix_quad_enable(nor);
1694 dev_err(nor->dev, "Macronix quad-read not enabled\n");
1698 case SNOR_MFR_MICRON:
1701 status = spansion_quad_enable(nor);
1703 dev_err(nor->dev, "Spansion quad-read not enabled\n");
1710 static int spi_nor_check(struct spi_nor *nor)
1712 if (!nor->dev || !nor->read || !nor->write ||
1713 !nor->read_reg || !nor->write_reg) {
1714 pr_err("spi-nor: please fill all the necessary fields!\n");
1721 int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
1723 struct flash_info *info = NULL;
1724 struct device *dev = nor->dev;
1725 struct mtd_info *mtd = &nor->mtd;
1726 struct device_node *np = spi_nor_get_flash_node(nor);
1727 struct device_node *np_spi ;
1732 ret = spi_nor_check(nor);
1737 info = (struct flash_info *)spi_nor_match_id(name);
1738 /* Try to auto-detect if chip name wasn't specified or not found */
1740 info = (struct flash_info *)spi_nor_read_id(nor);
1741 if (IS_ERR_OR_NULL(info))
1745 * If caller has specified name of flash model that can normally be
1746 * detected using JEDEC, let's verify it.
1748 if (name && info->id_len) {
1749 const struct flash_info *jinfo;
1751 jinfo = spi_nor_read_id(nor);
1752 if (IS_ERR(jinfo)) {
1753 return PTR_ERR(jinfo);
1754 } else if (jinfo != info) {
1756 * JEDEC knows better, so overwrite platform ID. We
1757 * can't trust partitions any longer, but we'll let
1758 * mtd apply them anyway, since some partitions may be
1759 * marked read-only, and we don't want to lose that
1760 * information, even if it's not 100% accurate.
1762 dev_warn(dev, "found %s, expected %s\n",
1763 jinfo->name, info->name);
1764 info = (struct flash_info *)jinfo;
1768 mutex_init(&nor->lock);
1771 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
1772 * with the software protection bits set
1775 if (JEDEC_MFR(info) == SNOR_MFR_ATMEL ||
1776 JEDEC_MFR(info) == SNOR_MFR_INTEL ||
1777 JEDEC_MFR(info) == SNOR_MFR_SST ||
1778 info->flags & SPI_NOR_HAS_LOCK) {
1781 if (info->flags & SST_GLOBAL_PROT_UNLK) {
1783 /* Unlock global write protection bits */
1784 nor->write_reg(nor, GLOBAL_BLKPROT_UNLK, NULL, 0);
1786 spi_nor_wait_till_ready(nor);
1790 mtd->name = dev_name(dev);
1792 mtd->type = MTD_NORFLASH;
1794 mtd->flags = MTD_CAP_NORFLASH;
1795 mtd->size = info->sector_size * info->n_sectors;
1796 mtd->_erase = spi_nor_erase;
1797 mtd->_read = spi_nor_read;
1799 np_spi = of_get_next_parent(np);
1800 if ((of_property_match_string(np_spi, "compatible",
1801 "xlnx,zynq-qspi-1.0") >= 0) ||
1802 (of_property_match_string(np_spi, "compatible",
1803 "xlnx,zynqmp-qspi-1.0") >= 0)) {
1804 if (of_property_read_u32(np_spi, "is-dual",
1806 /* Default to single if prop not defined */
1809 nor->isparallel = 0;
1814 info->sector_size <<= nor->shift;
1815 info->page_size <<= nor->shift;
1816 mtd->size <<= nor->shift;
1817 nor->isparallel = 1;
1819 nor->spi->master->flags |=
1820 (SPI_MASTER_DATA_STRIPE
1821 | SPI_MASTER_BOTH_CS);
1823 #ifdef CONFIG_SPI_ZYNQ_QSPI_DUAL_STACKED
1827 info->n_sectors <<= 1;
1829 nor->isparallel = 0;
1832 if (of_property_read_u32(np_spi,
1841 info->n_sectors <<= 1;
1843 nor->isparallel = 0;
1848 nor->isparallel = 0;
1855 pr_info("parallel %d stacked %d shift %d mtsize %d\n",
1856 nor->isparallel, nor->isstacked, nor->shift, mtd->size);
1859 /* Default to single */
1862 nor->isparallel = 0;
1865 /* NOR protection support for STmicro/Micron chips and similar */
1866 if (JEDEC_MFR(info) == SNOR_MFR_MICRON ||
1867 info->flags & SPI_NOR_HAS_LOCK) {
1868 nor->flash_lock = stm_lock;
1869 nor->flash_unlock = stm_unlock;
1870 nor->flash_is_locked = stm_is_locked;
1873 if (nor->flash_lock && nor->flash_unlock && nor->flash_is_locked) {
1874 mtd->_lock = spi_nor_lock;
1875 mtd->_unlock = spi_nor_unlock;
1876 mtd->_is_locked = spi_nor_is_locked;
1879 /* sst nor chips use AAI word program */
1880 if (info->flags & SST_WRITE)
1881 mtd->_write = sst_write;
1883 mtd->_write = spi_nor_write;
1885 if (info->flags & USE_FSR)
1886 nor->flags |= SNOR_F_USE_FSR;
1887 if (info->flags & SPI_NOR_HAS_TB)
1888 nor->flags |= SNOR_F_HAS_SR_TB;
1890 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
1891 /* prefer "small sector" erase if possible */
1892 if (info->flags & SECT_4K) {
1893 nor->erase_opcode = SPINOR_OP_BE_4K;
1894 mtd->erasesize = 4096 << nor->shift;
1895 } else if (info->flags & SECT_4K_PMC) {
1896 nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
1897 mtd->erasesize = 4096 << nor->shift;
1901 nor->erase_opcode = SPINOR_OP_SE;
1902 mtd->erasesize = info->sector_size;
1905 if (info->flags & SPI_NOR_NO_ERASE)
1906 mtd->flags |= MTD_NO_ERASE;
1908 nor->jedec_id = info->id[0];
1909 mtd->dev.parent = dev;
1910 nor->page_size = info->page_size;
1911 mtd->writebufsize = nor->page_size;
1914 /* If we were instantiated by DT, use it */
1915 if (of_property_read_bool(np, "m25p,fast-read"))
1916 nor->flash_read = SPI_NOR_FAST;
1918 nor->flash_read = SPI_NOR_NORMAL;
1920 /* If we weren't instantiated by DT, default to fast-read */
1921 nor->flash_read = SPI_NOR_FAST;
1924 /* Some devices cannot do fast-read, no matter what DT tells us */
1925 if (info->flags & SPI_NOR_NO_FR)
1926 nor->flash_read = SPI_NOR_NORMAL;
1928 /* Quad/Dual-read mode takes precedence over fast/normal */
1929 if (mode == SPI_NOR_QUAD && info->flags & SPI_NOR_QUAD_READ) {
1930 ret = set_quad_mode(nor, info);
1932 dev_err(dev, "quad mode not supported\n");
1935 nor->flash_read = SPI_NOR_QUAD;
1936 } else if (mode == SPI_NOR_DUAL && info->flags & SPI_NOR_DUAL_READ) {
1937 nor->flash_read = SPI_NOR_DUAL;
1940 /* Default commands */
1941 switch (nor->flash_read) {
1943 nor->read_opcode = SPINOR_OP_READ_1_1_4;
1946 nor->read_opcode = SPINOR_OP_READ_1_1_2;
1949 nor->read_opcode = SPINOR_OP_READ_FAST;
1951 case SPI_NOR_NORMAL:
1952 nor->read_opcode = SPINOR_OP_READ;
1955 dev_err(dev, "No Read opcode defined\n");
1959 nor->program_opcode = SPINOR_OP_PP;
1961 if (info->addr_width)
1962 nor->addr_width = info->addr_width;
1963 else if (mtd->size > 0x1000000) {
1965 np_spi = of_get_next_parent(np);
1966 if (of_property_match_string(np_spi, "compatible",
1967 "xlnx,zynq-qspi-1.0") >= 0) {
1970 nor->addr_width = 3;
1971 set_4byte(nor, info, 0);
1972 status = read_ear(nor, info);
1974 dev_warn(dev, "failed to read ear reg\n");
1976 nor->curbank = status & EAR_SEGMENT_MASK;
1979 /* enable 4-byte addressing if the device exceeds 16MiB */
1980 nor->addr_width = 4;
1981 if (JEDEC_MFR(info) == SNOR_MFR_SPANSION) {
1982 /* Dedicated 4-byte command set */
1983 switch (nor->flash_read) {
1985 nor->read_opcode = SPINOR_OP_READ4_1_1_4;
1988 nor->read_opcode = SPINOR_OP_READ4_1_1_2;
1991 nor->read_opcode = SPINOR_OP_READ4_FAST;
1993 case SPI_NOR_NORMAL:
1994 nor->read_opcode = SPINOR_OP_READ4;
1997 nor->program_opcode = SPINOR_OP_PP_4B;
1998 /* No small sector erase for 4-byte command set */
1999 nor->erase_opcode = SPINOR_OP_SE_4B;
2000 mtd->erasesize = info->sector_size;
2002 np_spi = of_get_next_parent(np);
2003 if (of_property_match_string(np_spi, "compatible",
2004 "xlnx,xps-spi-2.00.a") >= 0) {
2005 nor->addr_width = 3;
2006 set_4byte(nor, info, 0);
2008 set_4byte(nor, info, 1);
2009 if (nor->isstacked) {
2010 nor->spi->master->flags |=
2012 set_4byte(nor, info, 1);
2013 nor->spi->master->flags &=
2022 nor->addr_width = 3;
2025 if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
2026 dev_err(dev, "address width is too large: %u\n",
2031 nor->read_dummy = spi_nor_read_dummy_cycles(nor);
2033 dev_info(dev, "%s (%lld Kbytes)\n", info->name,
2034 (long long)mtd->size >> 10);
2037 "mtd .name = %s, .size = 0x%llx (%lldMiB), "
2038 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
2039 mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
2040 mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
2042 if (mtd->numeraseregions)
2043 for (i = 0; i < mtd->numeraseregions; i++)
2045 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
2046 ".erasesize = 0x%.8x (%uKiB), "
2047 ".numblocks = %d }\n",
2048 i, (long long)mtd->eraseregions[i].offset,
2049 mtd->eraseregions[i].erasesize,
2050 mtd->eraseregions[i].erasesize / 1024,
2051 mtd->eraseregions[i].numblocks);
2054 EXPORT_SYMBOL_GPL(spi_nor_scan);
2056 static const struct flash_info *spi_nor_match_id(const char *name)
2058 const struct flash_info *id = spi_nor_ids;
2061 if (!strcmp(name, id->name))
2068 MODULE_LICENSE("GPL");
2069 MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
2070 MODULE_AUTHOR("Mike Lavender");
2071 MODULE_DESCRIPTION("framework for SPI NOR");