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>
26 /* Define max times to check status register before we give up. */
29 * For everything but full-chip erase; probably could be much smaller, but kept
30 * around for safety for now
32 #define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ)
35 * For full-chip erase, calibrated to a 2MB flash (M25P16); should be scaled up
38 #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
40 #define SPI_NOR_MAX_ID_LEN 6
46 * This array stores the ID bytes.
47 * The first three bytes are the JEDIC ID.
48 * JEDEC ID zero means "no ID" (mostly older chips).
50 u8 id[SPI_NOR_MAX_ID_LEN];
53 /* The size listed here is what works with SPINOR_OP_SE, which isn't
54 * necessarily called a "sector" by the vendor.
63 #define SECT_4K 0x01 /* SPINOR_OP_BE_4K works uniformly */
64 #define SPI_NOR_NO_ERASE 0x02 /* No erase command needed */
65 #define SST_WRITE 0x04 /* use SST byte programming */
66 #define SPI_NOR_NO_FR 0x08 /* Can't do fastread */
67 #define SECT_4K_PMC 0x10 /* SPINOR_OP_BE_4K_PMC works uniformly */
68 #define SPI_NOR_DUAL_READ 0x20 /* Flash supports Dual Read */
69 #define SPI_NOR_QUAD_READ 0x40 /* Flash supports Quad Read */
70 #define USE_FSR 0x80 /* use flag status register */
73 #define JEDEC_MFR(info) ((info)->id[0])
75 static const struct flash_info *spi_nor_match_id(const char *name);
78 * Read the status register, returning its value in the location
79 * Return the status register value.
80 * Returns negative if error occurred.
82 static int read_sr(struct spi_nor *nor)
87 ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
89 pr_err("error %d reading SR\n", (int) ret);
97 * Read the flag status register, returning its value in the location
98 * Return the status register value.
99 * Returns negative if error occurred.
101 static int read_fsr(struct spi_nor *nor)
106 ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
108 pr_err("error %d reading FSR\n", ret);
116 * Read configuration register, returning its value in the
117 * location. Return the configuration register value.
118 * Returns negative if error occured.
120 static int read_cr(struct spi_nor *nor)
125 ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
127 dev_err(nor->dev, "error %d reading CR\n", ret);
135 * Dummy Cycle calculation for different type of read.
136 * It can be used to support more commands with
137 * different dummy cycle requirements.
139 static inline int spi_nor_read_dummy_cycles(struct spi_nor *nor)
141 switch (nor->flash_read) {
153 * Write status register 1 byte
154 * Returns negative if error occurred.
156 static inline int write_sr(struct spi_nor *nor, u8 val)
158 nor->cmd_buf[0] = val;
159 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
163 * Set write enable latch with Write Enable command.
164 * Returns negative if error occurred.
166 static inline int write_enable(struct spi_nor *nor)
168 return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
172 * Send write disble instruction to the chip.
174 static inline int write_disable(struct spi_nor *nor)
176 return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
179 static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
184 /* Enable/disable 4-byte addressing mode. */
185 static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info,
189 bool need_wren = false;
192 switch (JEDEC_MFR(info)) {
193 case SNOR_MFR_MICRON:
194 /* Some Micron need WREN command; all will accept it */
196 case SNOR_MFR_MACRONIX:
197 case SNOR_MFR_WINBOND:
201 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
202 status = nor->write_reg(nor, cmd, NULL, 0);
209 nor->cmd_buf[0] = enable << 7;
210 return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
213 static inline int spi_nor_sr_ready(struct spi_nor *nor)
215 int sr = read_sr(nor);
219 return !(sr & SR_WIP);
222 static inline int spi_nor_fsr_ready(struct spi_nor *nor)
224 int fsr = read_fsr(nor);
228 return fsr & FSR_READY;
231 static int spi_nor_ready(struct spi_nor *nor)
234 sr = spi_nor_sr_ready(nor);
237 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
244 * Service routine to read status register until ready, or timeout occurs.
245 * Returns non-zero if error.
247 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
248 unsigned long timeout_jiffies)
250 unsigned long deadline;
251 int timeout = 0, ret;
253 deadline = jiffies + timeout_jiffies;
256 if (time_after_eq(jiffies, deadline))
259 ret = spi_nor_ready(nor);
268 dev_err(nor->dev, "flash operation timed out\n");
273 static int spi_nor_wait_till_ready(struct spi_nor *nor)
275 return spi_nor_wait_till_ready_with_timeout(nor,
276 DEFAULT_READY_WAIT_JIFFIES);
280 * Erase the whole flash memory
282 * Returns 0 if successful, non-zero otherwise.
284 static int erase_chip(struct spi_nor *nor)
286 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10));
288 return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0);
291 static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)
295 mutex_lock(&nor->lock);
298 ret = nor->prepare(nor, ops);
300 dev_err(nor->dev, "failed in the preparation.\n");
301 mutex_unlock(&nor->lock);
308 static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
311 nor->unprepare(nor, ops);
312 mutex_unlock(&nor->lock);
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);
326 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
327 (long long)instr->len);
329 div_u64_rem(instr->len, mtd->erasesize, &rem);
336 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_ERASE);
340 /* whole-chip erase? */
341 if (len == mtd->size) {
342 unsigned long timeout;
346 if (erase_chip(nor)) {
352 * Scale the timeout linearly with the size of the flash, with
353 * a minimum calibrated to an old 2MB flash. We could try to
354 * pull these from CFI/SFDP, but these values should be good
357 timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES,
358 CHIP_ERASE_2MB_READY_WAIT_JIFFIES *
359 (unsigned long)(mtd->size / SZ_2M));
360 ret = spi_nor_wait_till_ready_with_timeout(nor, timeout);
364 /* REVISIT in some cases we could speed up erasing large regions
365 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
366 * to use "small sector erase", but that's not always optimal.
369 /* "sector"-at-a-time erase */
374 if (nor->erase(nor, addr)) {
379 addr += mtd->erasesize;
380 len -= mtd->erasesize;
382 ret = spi_nor_wait_till_ready(nor);
390 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
392 instr->state = MTD_ERASE_DONE;
393 mtd_erase_callback(instr);
398 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
399 instr->state = MTD_ERASE_FAILED;
403 static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
406 struct mtd_info *mtd = &nor->mtd;
407 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
408 int shift = ffs(mask) - 1;
416 pow = ((sr & mask) ^ mask) >> shift;
417 *len = mtd->size >> pow;
418 *ofs = mtd->size - *len;
423 * Return 1 if the entire region is locked, 0 otherwise
425 static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
431 stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
433 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
437 * Lock a region of the flash. Compatible with ST Micro and similar flash.
438 * Supports only the block protection bits BP{0,1,2} in the status register
439 * (SR). Does not support these features found in newer SR bitfields:
440 * - TB: top/bottom protect - only handle TB=0 (top protect)
441 * - SEC: sector/block protect - only handle SEC=0 (block protect)
442 * - CMP: complement protect - only support CMP=0 (range is not complemented)
444 * Sample table portion for 8MB flash (Winbond w25q64fw):
446 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
447 * --------------------------------------------------------------------------
448 * X | X | 0 | 0 | 0 | NONE | NONE
449 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
450 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
451 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
452 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
453 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
454 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
455 * X | X | 1 | 1 | 1 | 8 MB | ALL
457 * Returns negative on errors, 0 on success.
459 static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
461 struct mtd_info *mtd = &nor->mtd;
462 u8 status_old, status_new;
463 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
464 u8 shift = ffs(mask) - 1, pow, val;
466 status_old = read_sr(nor);
468 /* SPI NOR always locks to the end */
469 if (ofs + len != mtd->size) {
470 /* Does combined region extend to end? */
471 if (!stm_is_locked_sr(nor, ofs + len, mtd->size - ofs - len,
474 len = mtd->size - ofs;
478 * Need smallest pow such that:
480 * 1 / (2^pow) <= (len / size)
482 * so (assuming power-of-2 size) we do:
484 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
486 pow = ilog2(mtd->size) - ilog2(len);
487 val = mask - (pow << shift);
490 /* Don't "lock" with no region! */
494 status_new = (status_old & ~mask) | val;
496 /* Only modify protection if it will not unlock other areas */
497 if ((status_new & mask) <= (status_old & mask))
501 return write_sr(nor, status_new);
505 * Unlock a region of the flash. See stm_lock() for more info
507 * Returns negative on errors, 0 on success.
509 static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
511 struct mtd_info *mtd = &nor->mtd;
512 uint8_t status_old, status_new;
513 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
514 u8 shift = ffs(mask) - 1, pow, val;
516 status_old = read_sr(nor);
518 /* Cannot unlock; would unlock larger region than requested */
519 if (stm_is_locked_sr(nor, status_old, ofs - mtd->erasesize,
524 * Need largest pow such that:
526 * 1 / (2^pow) >= (len / size)
528 * so (assuming power-of-2 size) we do:
530 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
532 pow = ilog2(mtd->size) - order_base_2(mtd->size - (ofs + len));
533 if (ofs + len == mtd->size) {
534 val = 0; /* fully unlocked */
536 val = mask - (pow << shift);
537 /* Some power-of-two sizes are not supported */
542 status_new = (status_old & ~mask) | val;
544 /* Only modify protection if it will not lock other areas */
545 if ((status_new & mask) >= (status_old & mask))
549 return write_sr(nor, status_new);
552 static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
554 struct spi_nor *nor = mtd_to_spi_nor(mtd);
557 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_LOCK);
561 ret = nor->flash_lock(nor, ofs, len);
563 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
567 static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
569 struct spi_nor *nor = mtd_to_spi_nor(mtd);
572 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
576 ret = nor->flash_unlock(nor, ofs, len);
578 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
582 /* Used when the "_ext_id" is two bytes at most */
583 #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
585 ((_jedec_id) >> 16) & 0xff, \
586 ((_jedec_id) >> 8) & 0xff, \
587 (_jedec_id) & 0xff, \
588 ((_ext_id) >> 8) & 0xff, \
591 .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))), \
592 .sector_size = (_sector_size), \
593 .n_sectors = (_n_sectors), \
597 #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
599 ((_jedec_id) >> 16) & 0xff, \
600 ((_jedec_id) >> 8) & 0xff, \
601 (_jedec_id) & 0xff, \
602 ((_ext_id) >> 16) & 0xff, \
603 ((_ext_id) >> 8) & 0xff, \
607 .sector_size = (_sector_size), \
608 .n_sectors = (_n_sectors), \
612 #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
613 .sector_size = (_sector_size), \
614 .n_sectors = (_n_sectors), \
615 .page_size = (_page_size), \
616 .addr_width = (_addr_width), \
619 /* NOTE: double check command sets and memory organization when you add
620 * more nor chips. This current list focusses on newer chips, which
621 * have been converging on command sets which including JEDEC ID.
623 * All newly added entries should describe *hardware* and should use SECT_4K
624 * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage
625 * scenarios excluding small sectors there is config option that can be
626 * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS.
627 * For historical (and compatibility) reasons (before we got above config) some
628 * old entries may be missing 4K flag.
630 static const struct flash_info spi_nor_ids[] = {
631 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
632 { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
633 { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
635 { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
636 { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
637 { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
639 { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
640 { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
641 { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
642 { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
644 { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
647 { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
648 { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
649 { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
650 { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
651 { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
652 { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
653 { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
654 { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K) },
657 { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K) },
660 { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
661 { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
664 { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE) },
667 { "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64, SECT_4K) },
668 { "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128, SECT_4K) },
669 { "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256, SECT_4K) },
671 /* Intel/Numonyx -- xxxs33b */
672 { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
673 { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
674 { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
677 { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K) },
680 { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K) },
681 { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
682 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
683 { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
684 { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
685 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
686 { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) },
687 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
688 { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
689 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
690 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
691 { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, 0) },
692 { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
693 { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_QUAD_READ) },
694 { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ) },
697 { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
698 { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
699 { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
700 { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
701 { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
702 { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SPI_NOR_QUAD_READ) },
703 { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ) },
704 { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
705 { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
706 { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
709 { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
710 { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
711 { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
713 /* Spansion -- single (large) sector size only, at least
714 * for the chips listed here (without boot sectors).
716 { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
717 { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
718 { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, 0) },
719 { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
720 { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
721 { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
722 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
723 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
724 { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
725 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
726 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
727 { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
728 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
729 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
730 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
731 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
732 { "s25fl004k", INFO(0xef4013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
733 { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
734 { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
735 { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
736 { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) },
737 { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
738 { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ) },
740 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
741 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
742 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
743 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
744 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
745 { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
746 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
747 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
748 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
749 { "sst25wf020a", INFO(0x621612, 0, 64 * 1024, 4, SECT_4K) },
750 { "sst25wf040b", INFO(0x621613, 0, 64 * 1024, 8, SECT_4K) },
751 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
752 { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
754 /* ST Microelectronics -- newer production may have feature updates */
755 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
756 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
757 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
758 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
759 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
760 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
761 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
762 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
763 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
765 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
766 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
767 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
768 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
769 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
770 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
771 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
772 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
773 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
775 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
776 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
777 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
779 { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
780 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
781 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
783 { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
784 { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
785 { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
786 { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
787 { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
788 { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
790 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
791 { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
792 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
793 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
794 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
795 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
796 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
797 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
798 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
799 { "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64, SECT_4K) },
800 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
801 { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
802 { "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128, SECT_4K) },
803 { "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
804 { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
805 { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
806 { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
807 { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K) },
809 /* Catalyst / On Semiconductor -- non-JEDEC */
810 { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
811 { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
812 { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
813 { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
814 { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
818 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
821 u8 id[SPI_NOR_MAX_ID_LEN];
822 const struct flash_info *info;
824 tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
826 dev_dbg(nor->dev, " error %d reading JEDEC ID\n", tmp);
830 for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
831 info = &spi_nor_ids[tmp];
833 if (!memcmp(info->id, id, info->id_len))
834 return &spi_nor_ids[tmp];
837 dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %2x, %2x\n",
838 id[0], id[1], id[2]);
839 return ERR_PTR(-ENODEV);
842 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
843 size_t *retlen, u_char *buf)
845 struct spi_nor *nor = mtd_to_spi_nor(mtd);
848 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
850 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ);
854 ret = nor->read(nor, from, len, retlen, buf);
856 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
860 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
861 size_t *retlen, const u_char *buf)
863 struct spi_nor *nor = mtd_to_spi_nor(mtd);
867 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
869 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
875 nor->sst_write_second = false;
878 /* Start write from odd address. */
880 nor->program_opcode = SPINOR_OP_BP;
882 /* write one byte. */
883 nor->write(nor, to, 1, retlen, buf);
884 ret = spi_nor_wait_till_ready(nor);
890 /* Write out most of the data here. */
891 for (; actual < len - 1; actual += 2) {
892 nor->program_opcode = SPINOR_OP_AAI_WP;
894 /* write two bytes. */
895 nor->write(nor, to, 2, retlen, buf + actual);
896 ret = spi_nor_wait_till_ready(nor);
900 nor->sst_write_second = true;
902 nor->sst_write_second = false;
905 ret = spi_nor_wait_till_ready(nor);
909 /* Write out trailing byte if it exists. */
913 nor->program_opcode = SPINOR_OP_BP;
914 nor->write(nor, to, 1, retlen, buf + actual);
916 ret = spi_nor_wait_till_ready(nor);
922 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
927 * Write an address range to the nor chip. Data must be written in
928 * FLASH_PAGESIZE chunks. The address range may be any size provided
929 * it is within the physical boundaries.
931 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
932 size_t *retlen, const u_char *buf)
934 struct spi_nor *nor = mtd_to_spi_nor(mtd);
935 u32 page_offset, page_size, i;
938 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
940 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
946 page_offset = to & (nor->page_size - 1);
948 /* do all the bytes fit onto one page? */
949 if (page_offset + len <= nor->page_size) {
950 nor->write(nor, to, len, retlen, buf);
952 /* the size of data remaining on the first page */
953 page_size = nor->page_size - page_offset;
954 nor->write(nor, to, page_size, retlen, buf);
956 /* write everything in nor->page_size chunks */
957 for (i = page_size; i < len; i += page_size) {
959 if (page_size > nor->page_size)
960 page_size = nor->page_size;
962 ret = spi_nor_wait_till_ready(nor);
968 nor->write(nor, to + i, page_size, retlen, buf + i);
972 ret = spi_nor_wait_till_ready(nor);
974 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
978 static int macronix_quad_enable(struct spi_nor *nor)
985 write_sr(nor, val | SR_QUAD_EN_MX);
987 if (spi_nor_wait_till_ready(nor))
991 if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
992 dev_err(nor->dev, "Macronix Quad bit not set\n");
1000 * Write status Register and configuration register with 2 bytes
1001 * The first byte will be written to the status register, while the
1002 * second byte will be written to the configuration register.
1003 * Return negative if error occured.
1005 static int write_sr_cr(struct spi_nor *nor, u16 val)
1007 nor->cmd_buf[0] = val & 0xff;
1008 nor->cmd_buf[1] = (val >> 8);
1010 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 2);
1013 static int spansion_quad_enable(struct spi_nor *nor)
1016 int quad_en = CR_QUAD_EN_SPAN << 8;
1020 ret = write_sr_cr(nor, quad_en);
1023 "error while writing configuration register\n");
1027 /* read back and check it */
1029 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
1030 dev_err(nor->dev, "Spansion Quad bit not set\n");
1037 static int micron_quad_enable(struct spi_nor *nor)
1042 ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &val, 1);
1044 dev_err(nor->dev, "error %d reading EVCR\n", ret);
1050 /* set EVCR, enable quad I/O */
1051 nor->cmd_buf[0] = val & ~EVCR_QUAD_EN_MICRON;
1052 ret = nor->write_reg(nor, SPINOR_OP_WD_EVCR, nor->cmd_buf, 1);
1054 dev_err(nor->dev, "error while writing EVCR register\n");
1058 ret = spi_nor_wait_till_ready(nor);
1062 /* read EVCR and check it */
1063 ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &val, 1);
1065 dev_err(nor->dev, "error %d reading EVCR\n", ret);
1068 if (val & EVCR_QUAD_EN_MICRON) {
1069 dev_err(nor->dev, "Micron EVCR Quad bit not clear\n");
1076 static int set_quad_mode(struct spi_nor *nor, const struct flash_info *info)
1080 switch (JEDEC_MFR(info)) {
1081 case SNOR_MFR_MACRONIX:
1082 status = macronix_quad_enable(nor);
1084 dev_err(nor->dev, "Macronix quad-read not enabled\n");
1088 case SNOR_MFR_MICRON:
1089 status = micron_quad_enable(nor);
1091 dev_err(nor->dev, "Micron quad-read not enabled\n");
1096 status = spansion_quad_enable(nor);
1098 dev_err(nor->dev, "Spansion quad-read not enabled\n");
1105 static int spi_nor_check(struct spi_nor *nor)
1107 if (!nor->dev || !nor->read || !nor->write ||
1108 !nor->read_reg || !nor->write_reg || !nor->erase) {
1109 pr_err("spi-nor: please fill all the necessary fields!\n");
1116 int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
1118 const struct flash_info *info = NULL;
1119 struct device *dev = nor->dev;
1120 struct mtd_info *mtd = &nor->mtd;
1121 struct device_node *np = nor->flash_node;
1125 ret = spi_nor_check(nor);
1130 info = spi_nor_match_id(name);
1131 /* Try to auto-detect if chip name wasn't specified or not found */
1133 info = spi_nor_read_id(nor);
1134 if (IS_ERR_OR_NULL(info))
1138 * If caller has specified name of flash model that can normally be
1139 * detected using JEDEC, let's verify it.
1141 if (name && info->id_len) {
1142 const struct flash_info *jinfo;
1144 jinfo = spi_nor_read_id(nor);
1145 if (IS_ERR(jinfo)) {
1146 return PTR_ERR(jinfo);
1147 } else if (jinfo != info) {
1149 * JEDEC knows better, so overwrite platform ID. We
1150 * can't trust partitions any longer, but we'll let
1151 * mtd apply them anyway, since some partitions may be
1152 * marked read-only, and we don't want to lose that
1153 * information, even if it's not 100% accurate.
1155 dev_warn(dev, "found %s, expected %s\n",
1156 jinfo->name, info->name);
1161 mutex_init(&nor->lock);
1164 * Atmel, SST and Intel/Numonyx serial nor tend to power
1165 * up with the software protection bits set
1168 if (JEDEC_MFR(info) == SNOR_MFR_ATMEL ||
1169 JEDEC_MFR(info) == SNOR_MFR_INTEL ||
1170 JEDEC_MFR(info) == SNOR_MFR_SST) {
1176 mtd->name = dev_name(dev);
1178 mtd->type = MTD_NORFLASH;
1180 mtd->flags = MTD_CAP_NORFLASH;
1181 mtd->size = info->sector_size * info->n_sectors;
1182 mtd->_erase = spi_nor_erase;
1183 mtd->_read = spi_nor_read;
1185 /* NOR protection support for STmicro/Micron chips */
1186 if (JEDEC_MFR(info) == SNOR_MFR_MICRON) {
1187 nor->flash_lock = stm_lock;
1188 nor->flash_unlock = stm_unlock;
1191 if (nor->flash_lock && nor->flash_unlock) {
1192 mtd->_lock = spi_nor_lock;
1193 mtd->_unlock = spi_nor_unlock;
1196 /* sst nor chips use AAI word program */
1197 if (info->flags & SST_WRITE)
1198 mtd->_write = sst_write;
1200 mtd->_write = spi_nor_write;
1202 if (info->flags & USE_FSR)
1203 nor->flags |= SNOR_F_USE_FSR;
1205 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
1206 /* prefer "small sector" erase if possible */
1207 if (info->flags & SECT_4K) {
1208 nor->erase_opcode = SPINOR_OP_BE_4K;
1209 mtd->erasesize = 4096;
1210 } else if (info->flags & SECT_4K_PMC) {
1211 nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
1212 mtd->erasesize = 4096;
1216 nor->erase_opcode = SPINOR_OP_SE;
1217 mtd->erasesize = info->sector_size;
1220 if (info->flags & SPI_NOR_NO_ERASE)
1221 mtd->flags |= MTD_NO_ERASE;
1223 mtd->dev.parent = dev;
1224 nor->page_size = info->page_size;
1225 mtd->writebufsize = nor->page_size;
1228 /* If we were instantiated by DT, use it */
1229 if (of_property_read_bool(np, "m25p,fast-read"))
1230 nor->flash_read = SPI_NOR_FAST;
1232 nor->flash_read = SPI_NOR_NORMAL;
1234 /* If we weren't instantiated by DT, default to fast-read */
1235 nor->flash_read = SPI_NOR_FAST;
1238 /* Some devices cannot do fast-read, no matter what DT tells us */
1239 if (info->flags & SPI_NOR_NO_FR)
1240 nor->flash_read = SPI_NOR_NORMAL;
1242 /* Quad/Dual-read mode takes precedence over fast/normal */
1243 if (mode == SPI_NOR_QUAD && info->flags & SPI_NOR_QUAD_READ) {
1244 ret = set_quad_mode(nor, info);
1246 dev_err(dev, "quad mode not supported\n");
1249 nor->flash_read = SPI_NOR_QUAD;
1250 } else if (mode == SPI_NOR_DUAL && info->flags & SPI_NOR_DUAL_READ) {
1251 nor->flash_read = SPI_NOR_DUAL;
1254 /* Default commands */
1255 switch (nor->flash_read) {
1257 nor->read_opcode = SPINOR_OP_READ_1_1_4;
1260 nor->read_opcode = SPINOR_OP_READ_1_1_2;
1263 nor->read_opcode = SPINOR_OP_READ_FAST;
1265 case SPI_NOR_NORMAL:
1266 nor->read_opcode = SPINOR_OP_READ;
1269 dev_err(dev, "No Read opcode defined\n");
1273 nor->program_opcode = SPINOR_OP_PP;
1275 if (info->addr_width)
1276 nor->addr_width = info->addr_width;
1277 else if (mtd->size > 0x1000000) {
1278 /* enable 4-byte addressing if the device exceeds 16MiB */
1279 nor->addr_width = 4;
1280 if (JEDEC_MFR(info) == SNOR_MFR_SPANSION) {
1281 /* Dedicated 4-byte command set */
1282 switch (nor->flash_read) {
1284 nor->read_opcode = SPINOR_OP_READ4_1_1_4;
1287 nor->read_opcode = SPINOR_OP_READ4_1_1_2;
1290 nor->read_opcode = SPINOR_OP_READ4_FAST;
1292 case SPI_NOR_NORMAL:
1293 nor->read_opcode = SPINOR_OP_READ4;
1296 nor->program_opcode = SPINOR_OP_PP_4B;
1297 /* No small sector erase for 4-byte command set */
1298 nor->erase_opcode = SPINOR_OP_SE_4B;
1299 mtd->erasesize = info->sector_size;
1301 set_4byte(nor, info, 1);
1303 nor->addr_width = 3;
1306 nor->read_dummy = spi_nor_read_dummy_cycles(nor);
1308 dev_info(dev, "%s (%lld Kbytes)\n", info->name,
1309 (long long)mtd->size >> 10);
1312 "mtd .name = %s, .size = 0x%llx (%lldMiB), "
1313 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
1314 mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
1315 mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
1317 if (mtd->numeraseregions)
1318 for (i = 0; i < mtd->numeraseregions; i++)
1320 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
1321 ".erasesize = 0x%.8x (%uKiB), "
1322 ".numblocks = %d }\n",
1323 i, (long long)mtd->eraseregions[i].offset,
1324 mtd->eraseregions[i].erasesize,
1325 mtd->eraseregions[i].erasesize / 1024,
1326 mtd->eraseregions[i].numblocks);
1329 EXPORT_SYMBOL_GPL(spi_nor_scan);
1331 static const struct flash_info *spi_nor_match_id(const char *name)
1333 const struct flash_info *id = spi_nor_ids;
1336 if (!strcmp(name, id->name))
1343 MODULE_LICENSE("GPL");
1344 MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
1345 MODULE_AUTHOR("Mike Lavender");
1346 MODULE_DESCRIPTION("framework for SPI NOR");