net: smc91x: ACPI Enable lan91x adapters
[linux/fpc-iii.git] / drivers / mtd / spi-nor / spi-nor.c
blobc52e45594bfd6e78775f6250314c9cf26a049e2a
1 /*
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
36 * for larger flash
38 #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
40 #define SPI_NOR_MAX_ID_LEN 6
41 #define SPI_NOR_MAX_ADDR_WIDTH 4
43 struct flash_info {
44 char *name;
47 * This array stores the ID bytes.
48 * The first three bytes are the JEDIC ID.
49 * JEDEC ID zero means "no ID" (mostly older chips).
51 u8 id[SPI_NOR_MAX_ID_LEN];
52 u8 id_len;
54 /* The size listed here is what works with SPINOR_OP_SE, which isn't
55 * necessarily called a "sector" by the vendor.
57 unsigned sector_size;
58 u16 n_sectors;
60 u16 page_size;
61 u16 addr_width;
63 u16 flags;
64 #define SECT_4K BIT(0) /* SPINOR_OP_BE_4K works uniformly */
65 #define SPI_NOR_NO_ERASE BIT(1) /* No erase command needed */
66 #define SST_WRITE BIT(2) /* use SST byte programming */
67 #define SPI_NOR_NO_FR BIT(3) /* Can't do fastread */
68 #define SECT_4K_PMC BIT(4) /* SPINOR_OP_BE_4K_PMC works uniformly */
69 #define SPI_NOR_DUAL_READ BIT(5) /* Flash supports Dual Read */
70 #define SPI_NOR_QUAD_READ BIT(6) /* Flash supports Quad Read */
71 #define USE_FSR BIT(7) /* use flag status register */
72 #define SPI_NOR_HAS_LOCK BIT(8) /* Flash supports lock/unlock via SR */
73 #define SPI_NOR_HAS_TB BIT(9) /*
74 * Flash SR has Top/Bottom (TB) protect
75 * bit. Must be used with
76 * SPI_NOR_HAS_LOCK.
80 #define JEDEC_MFR(info) ((info)->id[0])
82 static const struct flash_info *spi_nor_match_id(const char *name);
85 * Read the status register, returning its value in the location
86 * Return the status register value.
87 * Returns negative if error occurred.
89 static int read_sr(struct spi_nor *nor)
91 int ret;
92 u8 val;
94 ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
95 if (ret < 0) {
96 pr_err("error %d reading SR\n", (int) ret);
97 return ret;
100 return val;
104 * Read the flag status register, returning its value in the location
105 * Return the status register value.
106 * Returns negative if error occurred.
108 static int read_fsr(struct spi_nor *nor)
110 int ret;
111 u8 val;
113 ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
114 if (ret < 0) {
115 pr_err("error %d reading FSR\n", ret);
116 return ret;
119 return val;
123 * Read configuration register, returning its value in the
124 * location. Return the configuration register value.
125 * Returns negative if error occured.
127 static int read_cr(struct spi_nor *nor)
129 int ret;
130 u8 val;
132 ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
133 if (ret < 0) {
134 dev_err(nor->dev, "error %d reading CR\n", ret);
135 return ret;
138 return val;
142 * Dummy Cycle calculation for different type of read.
143 * It can be used to support more commands with
144 * different dummy cycle requirements.
146 static inline int spi_nor_read_dummy_cycles(struct spi_nor *nor)
148 switch (nor->flash_read) {
149 case SPI_NOR_FAST:
150 case SPI_NOR_DUAL:
151 case SPI_NOR_QUAD:
152 return 8;
153 case SPI_NOR_NORMAL:
154 return 0;
156 return 0;
160 * Write status register 1 byte
161 * Returns negative if error occurred.
163 static inline int write_sr(struct spi_nor *nor, u8 val)
165 nor->cmd_buf[0] = val;
166 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
170 * Set write enable latch with Write Enable command.
171 * Returns negative if error occurred.
173 static inline int write_enable(struct spi_nor *nor)
175 return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
179 * Send write disble instruction to the chip.
181 static inline int write_disable(struct spi_nor *nor)
183 return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
186 static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
188 return mtd->priv;
191 /* Enable/disable 4-byte addressing mode. */
192 static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info,
193 int enable)
195 int status;
196 bool need_wren = false;
197 u8 cmd;
199 switch (JEDEC_MFR(info)) {
200 case SNOR_MFR_MICRON:
201 /* Some Micron need WREN command; all will accept it */
202 need_wren = true;
203 case SNOR_MFR_MACRONIX:
204 case SNOR_MFR_WINBOND:
205 if (need_wren)
206 write_enable(nor);
208 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
209 status = nor->write_reg(nor, cmd, NULL, 0);
210 if (need_wren)
211 write_disable(nor);
213 return status;
214 default:
215 /* Spansion style */
216 nor->cmd_buf[0] = enable << 7;
217 return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
220 static inline int spi_nor_sr_ready(struct spi_nor *nor)
222 int sr = read_sr(nor);
223 if (sr < 0)
224 return sr;
225 else
226 return !(sr & SR_WIP);
229 static inline int spi_nor_fsr_ready(struct spi_nor *nor)
231 int fsr = read_fsr(nor);
232 if (fsr < 0)
233 return fsr;
234 else
235 return fsr & FSR_READY;
238 static int spi_nor_ready(struct spi_nor *nor)
240 int sr, fsr;
241 sr = spi_nor_sr_ready(nor);
242 if (sr < 0)
243 return sr;
244 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
245 if (fsr < 0)
246 return fsr;
247 return sr && fsr;
251 * Service routine to read status register until ready, or timeout occurs.
252 * Returns non-zero if error.
254 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
255 unsigned long timeout_jiffies)
257 unsigned long deadline;
258 int timeout = 0, ret;
260 deadline = jiffies + timeout_jiffies;
262 while (!timeout) {
263 if (time_after_eq(jiffies, deadline))
264 timeout = 1;
266 ret = spi_nor_ready(nor);
267 if (ret < 0)
268 return ret;
269 if (ret)
270 return 0;
272 cond_resched();
275 dev_err(nor->dev, "flash operation timed out\n");
277 return -ETIMEDOUT;
280 static int spi_nor_wait_till_ready(struct spi_nor *nor)
282 return spi_nor_wait_till_ready_with_timeout(nor,
283 DEFAULT_READY_WAIT_JIFFIES);
287 * Erase the whole flash memory
289 * Returns 0 if successful, non-zero otherwise.
291 static int erase_chip(struct spi_nor *nor)
293 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10));
295 return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0);
298 static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)
300 int ret = 0;
302 mutex_lock(&nor->lock);
304 if (nor->prepare) {
305 ret = nor->prepare(nor, ops);
306 if (ret) {
307 dev_err(nor->dev, "failed in the preparation.\n");
308 mutex_unlock(&nor->lock);
309 return ret;
312 return ret;
315 static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
317 if (nor->unprepare)
318 nor->unprepare(nor, ops);
319 mutex_unlock(&nor->lock);
323 * Initiate the erasure of a single sector
325 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
327 u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
328 int i;
330 if (nor->erase)
331 return nor->erase(nor, addr);
334 * Default implementation, if driver doesn't have a specialized HW
335 * control
337 for (i = nor->addr_width - 1; i >= 0; i--) {
338 buf[i] = addr & 0xff;
339 addr >>= 8;
342 return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
346 * Erase an address range on the nor chip. The address range may extend
347 * one or more erase sectors. Return an error is there is a problem erasing.
349 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
351 struct spi_nor *nor = mtd_to_spi_nor(mtd);
352 u32 addr, len;
353 uint32_t rem;
354 int ret;
356 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
357 (long long)instr->len);
359 div_u64_rem(instr->len, mtd->erasesize, &rem);
360 if (rem)
361 return -EINVAL;
363 addr = instr->addr;
364 len = instr->len;
366 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_ERASE);
367 if (ret)
368 return ret;
370 /* whole-chip erase? */
371 if (len == mtd->size) {
372 unsigned long timeout;
374 write_enable(nor);
376 if (erase_chip(nor)) {
377 ret = -EIO;
378 goto erase_err;
382 * Scale the timeout linearly with the size of the flash, with
383 * a minimum calibrated to an old 2MB flash. We could try to
384 * pull these from CFI/SFDP, but these values should be good
385 * enough for now.
387 timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES,
388 CHIP_ERASE_2MB_READY_WAIT_JIFFIES *
389 (unsigned long)(mtd->size / SZ_2M));
390 ret = spi_nor_wait_till_ready_with_timeout(nor, timeout);
391 if (ret)
392 goto erase_err;
394 /* REVISIT in some cases we could speed up erasing large regions
395 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
396 * to use "small sector erase", but that's not always optimal.
399 /* "sector"-at-a-time erase */
400 } else {
401 while (len) {
402 write_enable(nor);
404 ret = spi_nor_erase_sector(nor, addr);
405 if (ret)
406 goto erase_err;
408 addr += mtd->erasesize;
409 len -= mtd->erasesize;
411 ret = spi_nor_wait_till_ready(nor);
412 if (ret)
413 goto erase_err;
417 write_disable(nor);
419 erase_err:
420 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
422 instr->state = ret ? MTD_ERASE_FAILED : MTD_ERASE_DONE;
423 mtd_erase_callback(instr);
425 return ret;
428 static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
429 uint64_t *len)
431 struct mtd_info *mtd = &nor->mtd;
432 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
433 int shift = ffs(mask) - 1;
434 int pow;
436 if (!(sr & mask)) {
437 /* No protection */
438 *ofs = 0;
439 *len = 0;
440 } else {
441 pow = ((sr & mask) ^ mask) >> shift;
442 *len = mtd->size >> pow;
443 if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
444 *ofs = 0;
445 else
446 *ofs = mtd->size - *len;
451 * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
452 * @locked is false); 0 otherwise
454 static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
455 u8 sr, bool locked)
457 loff_t lock_offs;
458 uint64_t lock_len;
460 if (!len)
461 return 1;
463 stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
465 if (locked)
466 /* Requested range is a sub-range of locked range */
467 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
468 else
469 /* Requested range does not overlap with locked range */
470 return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
473 static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
474 u8 sr)
476 return stm_check_lock_status_sr(nor, ofs, len, sr, true);
479 static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
480 u8 sr)
482 return stm_check_lock_status_sr(nor, ofs, len, sr, false);
486 * Lock a region of the flash. Compatible with ST Micro and similar flash.
487 * Supports the block protection bits BP{0,1,2} in the status register
488 * (SR). Does not support these features found in newer SR bitfields:
489 * - SEC: sector/block protect - only handle SEC=0 (block protect)
490 * - CMP: complement protect - only support CMP=0 (range is not complemented)
492 * Support for the following is provided conditionally for some flash:
493 * - TB: top/bottom protect
495 * Sample table portion for 8MB flash (Winbond w25q64fw):
497 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
498 * --------------------------------------------------------------------------
499 * X | X | 0 | 0 | 0 | NONE | NONE
500 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
501 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
502 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
503 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
504 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
505 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
506 * X | X | 1 | 1 | 1 | 8 MB | ALL
507 * ------|-------|-------|-------|-------|---------------|-------------------
508 * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
509 * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
510 * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
511 * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
512 * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
513 * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
515 * Returns negative on errors, 0 on success.
517 static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
519 struct mtd_info *mtd = &nor->mtd;
520 int status_old, status_new;
521 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
522 u8 shift = ffs(mask) - 1, pow, val;
523 loff_t lock_len;
524 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
525 bool use_top;
526 int ret;
528 status_old = read_sr(nor);
529 if (status_old < 0)
530 return status_old;
532 /* If nothing in our range is unlocked, we don't need to do anything */
533 if (stm_is_locked_sr(nor, ofs, len, status_old))
534 return 0;
536 /* If anything below us is unlocked, we can't use 'bottom' protection */
537 if (!stm_is_locked_sr(nor, 0, ofs, status_old))
538 can_be_bottom = false;
540 /* If anything above us is unlocked, we can't use 'top' protection */
541 if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
542 status_old))
543 can_be_top = false;
545 if (!can_be_bottom && !can_be_top)
546 return -EINVAL;
548 /* Prefer top, if both are valid */
549 use_top = can_be_top;
551 /* lock_len: length of region that should end up locked */
552 if (use_top)
553 lock_len = mtd->size - ofs;
554 else
555 lock_len = ofs + len;
558 * Need smallest pow such that:
560 * 1 / (2^pow) <= (len / size)
562 * so (assuming power-of-2 size) we do:
564 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
566 pow = ilog2(mtd->size) - ilog2(lock_len);
567 val = mask - (pow << shift);
568 if (val & ~mask)
569 return -EINVAL;
570 /* Don't "lock" with no region! */
571 if (!(val & mask))
572 return -EINVAL;
574 status_new = (status_old & ~mask & ~SR_TB) | val;
576 /* Disallow further writes if WP pin is asserted */
577 status_new |= SR_SRWD;
579 if (!use_top)
580 status_new |= SR_TB;
582 /* Don't bother if they're the same */
583 if (status_new == status_old)
584 return 0;
586 /* Only modify protection if it will not unlock other areas */
587 if ((status_new & mask) < (status_old & mask))
588 return -EINVAL;
590 write_enable(nor);
591 ret = write_sr(nor, status_new);
592 if (ret)
593 return ret;
594 return spi_nor_wait_till_ready(nor);
598 * Unlock a region of the flash. See stm_lock() for more info
600 * Returns negative on errors, 0 on success.
602 static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
604 struct mtd_info *mtd = &nor->mtd;
605 int status_old, status_new;
606 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
607 u8 shift = ffs(mask) - 1, pow, val;
608 loff_t lock_len;
609 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
610 bool use_top;
611 int ret;
613 status_old = read_sr(nor);
614 if (status_old < 0)
615 return status_old;
617 /* If nothing in our range is locked, we don't need to do anything */
618 if (stm_is_unlocked_sr(nor, ofs, len, status_old))
619 return 0;
621 /* If anything below us is locked, we can't use 'top' protection */
622 if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
623 can_be_top = false;
625 /* If anything above us is locked, we can't use 'bottom' protection */
626 if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
627 status_old))
628 can_be_bottom = false;
630 if (!can_be_bottom && !can_be_top)
631 return -EINVAL;
633 /* Prefer top, if both are valid */
634 use_top = can_be_top;
636 /* lock_len: length of region that should remain locked */
637 if (use_top)
638 lock_len = mtd->size - (ofs + len);
639 else
640 lock_len = ofs;
643 * Need largest pow such that:
645 * 1 / (2^pow) >= (len / size)
647 * so (assuming power-of-2 size) we do:
649 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
651 pow = ilog2(mtd->size) - order_base_2(lock_len);
652 if (lock_len == 0) {
653 val = 0; /* fully unlocked */
654 } else {
655 val = mask - (pow << shift);
656 /* Some power-of-two sizes are not supported */
657 if (val & ~mask)
658 return -EINVAL;
661 status_new = (status_old & ~mask & ~SR_TB) | val;
663 /* Don't protect status register if we're fully unlocked */
664 if (lock_len == mtd->size)
665 status_new &= ~SR_SRWD;
667 if (!use_top)
668 status_new |= SR_TB;
670 /* Don't bother if they're the same */
671 if (status_new == status_old)
672 return 0;
674 /* Only modify protection if it will not lock other areas */
675 if ((status_new & mask) > (status_old & mask))
676 return -EINVAL;
678 write_enable(nor);
679 ret = write_sr(nor, status_new);
680 if (ret)
681 return ret;
682 return spi_nor_wait_till_ready(nor);
686 * Check if a region of the flash is (completely) locked. See stm_lock() for
687 * more info.
689 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
690 * negative on errors.
692 static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
694 int status;
696 status = read_sr(nor);
697 if (status < 0)
698 return status;
700 return stm_is_locked_sr(nor, ofs, len, status);
703 static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
705 struct spi_nor *nor = mtd_to_spi_nor(mtd);
706 int ret;
708 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_LOCK);
709 if (ret)
710 return ret;
712 ret = nor->flash_lock(nor, ofs, len);
714 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
715 return ret;
718 static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
720 struct spi_nor *nor = mtd_to_spi_nor(mtd);
721 int ret;
723 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
724 if (ret)
725 return ret;
727 ret = nor->flash_unlock(nor, ofs, len);
729 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
730 return ret;
733 static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
735 struct spi_nor *nor = mtd_to_spi_nor(mtd);
736 int ret;
738 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
739 if (ret)
740 return ret;
742 ret = nor->flash_is_locked(nor, ofs, len);
744 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
745 return ret;
748 /* Used when the "_ext_id" is two bytes at most */
749 #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
750 .id = { \
751 ((_jedec_id) >> 16) & 0xff, \
752 ((_jedec_id) >> 8) & 0xff, \
753 (_jedec_id) & 0xff, \
754 ((_ext_id) >> 8) & 0xff, \
755 (_ext_id) & 0xff, \
756 }, \
757 .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))), \
758 .sector_size = (_sector_size), \
759 .n_sectors = (_n_sectors), \
760 .page_size = 256, \
761 .flags = (_flags),
763 #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
764 .id = { \
765 ((_jedec_id) >> 16) & 0xff, \
766 ((_jedec_id) >> 8) & 0xff, \
767 (_jedec_id) & 0xff, \
768 ((_ext_id) >> 16) & 0xff, \
769 ((_ext_id) >> 8) & 0xff, \
770 (_ext_id) & 0xff, \
771 }, \
772 .id_len = 6, \
773 .sector_size = (_sector_size), \
774 .n_sectors = (_n_sectors), \
775 .page_size = 256, \
776 .flags = (_flags),
778 #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
779 .sector_size = (_sector_size), \
780 .n_sectors = (_n_sectors), \
781 .page_size = (_page_size), \
782 .addr_width = (_addr_width), \
783 .flags = (_flags),
785 /* NOTE: double check command sets and memory organization when you add
786 * more nor chips. This current list focusses on newer chips, which
787 * have been converging on command sets which including JEDEC ID.
789 * All newly added entries should describe *hardware* and should use SECT_4K
790 * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage
791 * scenarios excluding small sectors there is config option that can be
792 * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS.
793 * For historical (and compatibility) reasons (before we got above config) some
794 * old entries may be missing 4K flag.
796 static const struct flash_info spi_nor_ids[] = {
797 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
798 { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
799 { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
801 { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
802 { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
803 { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
805 { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
806 { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
807 { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
808 { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
810 { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
812 /* EON -- en25xxx */
813 { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
814 { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
815 { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
816 { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
817 { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
818 { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
819 { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
820 { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K) },
822 /* ESMT */
823 { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K) },
825 /* Everspin */
826 { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
827 { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
829 /* Fujitsu */
830 { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE) },
832 /* GigaDevice */
833 { "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64, SECT_4K) },
834 { "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128, SECT_4K) },
835 { "gd25lq64c", INFO(0xc86017, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
836 { "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256, SECT_4K) },
838 /* Intel/Numonyx -- xxxs33b */
839 { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
840 { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
841 { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
843 /* ISSI */
844 { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K) },
846 /* Macronix */
847 { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K) },
848 { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
849 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
850 { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
851 { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
852 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, SECT_4K) },
853 { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) },
854 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, SECT_4K) },
855 { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
856 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
857 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
858 { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, 0) },
859 { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
860 { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_QUAD_READ) },
861 { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ) },
863 /* Micron */
864 { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
865 { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
866 { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
867 { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
868 { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
869 { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
870 { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ) },
871 { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
872 { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
873 { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
875 /* PMC */
876 { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
877 { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
878 { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
880 /* Spansion -- single (large) sector size only, at least
881 * for the chips listed here (without boot sectors).
883 { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
884 { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
885 { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, 0) },
886 { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
887 { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
888 { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
889 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
890 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
891 { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
892 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
893 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
894 { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
895 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
896 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
897 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
898 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
899 { "s25fl004k", INFO(0xef4013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
900 { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
901 { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
902 { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
903 { "s25fl116k", INFO(0x014015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
904 { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) },
905 { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
906 { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ) },
908 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
909 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
910 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
911 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
912 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
913 { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
914 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
915 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
916 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
917 { "sst25wf020a", INFO(0x621612, 0, 64 * 1024, 4, SECT_4K) },
918 { "sst25wf040b", INFO(0x621613, 0, 64 * 1024, 8, SECT_4K) },
919 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
920 { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
922 /* ST Microelectronics -- newer production may have feature updates */
923 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
924 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
925 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
926 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
927 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
928 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
929 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
930 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
931 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
933 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
934 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
935 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
936 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
937 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
938 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
939 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
940 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
941 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
943 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
944 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
945 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
947 { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
948 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
949 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
951 { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
952 { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
953 { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
954 { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
955 { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
956 { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
958 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
959 { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
960 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
961 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
962 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
963 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
964 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
965 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
966 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
968 "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64,
969 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
970 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
972 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
973 { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
975 "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128,
976 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
977 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
980 "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256,
981 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
982 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
984 { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
985 { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
986 { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
987 { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K) },
989 /* Catalyst / On Semiconductor -- non-JEDEC */
990 { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
991 { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
992 { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
993 { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
994 { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
995 { },
998 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
1000 int tmp;
1001 u8 id[SPI_NOR_MAX_ID_LEN];
1002 const struct flash_info *info;
1004 tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
1005 if (tmp < 0) {
1006 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
1007 return ERR_PTR(tmp);
1010 for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
1011 info = &spi_nor_ids[tmp];
1012 if (info->id_len) {
1013 if (!memcmp(info->id, id, info->id_len))
1014 return &spi_nor_ids[tmp];
1017 dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
1018 id[0], id[1], id[2]);
1019 return ERR_PTR(-ENODEV);
1022 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
1023 size_t *retlen, u_char *buf)
1025 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1026 int ret;
1028 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
1030 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ);
1031 if (ret)
1032 return ret;
1034 ret = nor->read(nor, from, len, retlen, buf);
1036 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
1037 return ret;
1040 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
1041 size_t *retlen, const u_char *buf)
1043 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1044 size_t actual;
1045 int ret;
1047 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1049 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
1050 if (ret)
1051 return ret;
1053 write_enable(nor);
1055 nor->sst_write_second = false;
1057 actual = to % 2;
1058 /* Start write from odd address. */
1059 if (actual) {
1060 nor->program_opcode = SPINOR_OP_BP;
1062 /* write one byte. */
1063 nor->write(nor, to, 1, retlen, buf);
1064 ret = spi_nor_wait_till_ready(nor);
1065 if (ret)
1066 goto time_out;
1068 to += actual;
1070 /* Write out most of the data here. */
1071 for (; actual < len - 1; actual += 2) {
1072 nor->program_opcode = SPINOR_OP_AAI_WP;
1074 /* write two bytes. */
1075 nor->write(nor, to, 2, retlen, buf + actual);
1076 ret = spi_nor_wait_till_ready(nor);
1077 if (ret)
1078 goto time_out;
1079 to += 2;
1080 nor->sst_write_second = true;
1082 nor->sst_write_second = false;
1084 write_disable(nor);
1085 ret = spi_nor_wait_till_ready(nor);
1086 if (ret)
1087 goto time_out;
1089 /* Write out trailing byte if it exists. */
1090 if (actual != len) {
1091 write_enable(nor);
1093 nor->program_opcode = SPINOR_OP_BP;
1094 nor->write(nor, to, 1, retlen, buf + actual);
1096 ret = spi_nor_wait_till_ready(nor);
1097 if (ret)
1098 goto time_out;
1099 write_disable(nor);
1101 time_out:
1102 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
1103 return ret;
1107 * Write an address range to the nor chip. Data must be written in
1108 * FLASH_PAGESIZE chunks. The address range may be any size provided
1109 * it is within the physical boundaries.
1111 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
1112 size_t *retlen, const u_char *buf)
1114 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1115 u32 page_offset, page_size, i;
1116 int ret;
1118 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1120 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
1121 if (ret)
1122 return ret;
1124 write_enable(nor);
1126 page_offset = to & (nor->page_size - 1);
1128 /* do all the bytes fit onto one page? */
1129 if (page_offset + len <= nor->page_size) {
1130 nor->write(nor, to, len, retlen, buf);
1131 } else {
1132 /* the size of data remaining on the first page */
1133 page_size = nor->page_size - page_offset;
1134 nor->write(nor, to, page_size, retlen, buf);
1136 /* write everything in nor->page_size chunks */
1137 for (i = page_size; i < len; i += page_size) {
1138 page_size = len - i;
1139 if (page_size > nor->page_size)
1140 page_size = nor->page_size;
1142 ret = spi_nor_wait_till_ready(nor);
1143 if (ret)
1144 goto write_err;
1146 write_enable(nor);
1148 nor->write(nor, to + i, page_size, retlen, buf + i);
1152 ret = spi_nor_wait_till_ready(nor);
1153 write_err:
1154 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
1155 return ret;
1158 static int macronix_quad_enable(struct spi_nor *nor)
1160 int ret, val;
1162 val = read_sr(nor);
1163 if (val < 0)
1164 return val;
1165 write_enable(nor);
1167 write_sr(nor, val | SR_QUAD_EN_MX);
1169 if (spi_nor_wait_till_ready(nor))
1170 return 1;
1172 ret = read_sr(nor);
1173 if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
1174 dev_err(nor->dev, "Macronix Quad bit not set\n");
1175 return -EINVAL;
1178 return 0;
1182 * Write status Register and configuration register with 2 bytes
1183 * The first byte will be written to the status register, while the
1184 * second byte will be written to the configuration register.
1185 * Return negative if error occured.
1187 static int write_sr_cr(struct spi_nor *nor, u16 val)
1189 nor->cmd_buf[0] = val & 0xff;
1190 nor->cmd_buf[1] = (val >> 8);
1192 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 2);
1195 static int spansion_quad_enable(struct spi_nor *nor)
1197 int ret;
1198 int quad_en = CR_QUAD_EN_SPAN << 8;
1200 write_enable(nor);
1202 ret = write_sr_cr(nor, quad_en);
1203 if (ret < 0) {
1204 dev_err(nor->dev,
1205 "error while writing configuration register\n");
1206 return -EINVAL;
1209 /* read back and check it */
1210 ret = read_cr(nor);
1211 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
1212 dev_err(nor->dev, "Spansion Quad bit not set\n");
1213 return -EINVAL;
1216 return 0;
1219 static int set_quad_mode(struct spi_nor *nor, const struct flash_info *info)
1221 int status;
1223 switch (JEDEC_MFR(info)) {
1224 case SNOR_MFR_MACRONIX:
1225 status = macronix_quad_enable(nor);
1226 if (status) {
1227 dev_err(nor->dev, "Macronix quad-read not enabled\n");
1228 return -EINVAL;
1230 return status;
1231 case SNOR_MFR_MICRON:
1232 return 0;
1233 default:
1234 status = spansion_quad_enable(nor);
1235 if (status) {
1236 dev_err(nor->dev, "Spansion quad-read not enabled\n");
1237 return -EINVAL;
1239 return status;
1243 static int spi_nor_check(struct spi_nor *nor)
1245 if (!nor->dev || !nor->read || !nor->write ||
1246 !nor->read_reg || !nor->write_reg) {
1247 pr_err("spi-nor: please fill all the necessary fields!\n");
1248 return -EINVAL;
1251 return 0;
1254 int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
1256 const struct flash_info *info = NULL;
1257 struct device *dev = nor->dev;
1258 struct mtd_info *mtd = &nor->mtd;
1259 struct device_node *np = spi_nor_get_flash_node(nor);
1260 int ret;
1261 int i;
1263 ret = spi_nor_check(nor);
1264 if (ret)
1265 return ret;
1267 if (name)
1268 info = spi_nor_match_id(name);
1269 /* Try to auto-detect if chip name wasn't specified or not found */
1270 if (!info)
1271 info = spi_nor_read_id(nor);
1272 if (IS_ERR_OR_NULL(info))
1273 return -ENOENT;
1276 * If caller has specified name of flash model that can normally be
1277 * detected using JEDEC, let's verify it.
1279 if (name && info->id_len) {
1280 const struct flash_info *jinfo;
1282 jinfo = spi_nor_read_id(nor);
1283 if (IS_ERR(jinfo)) {
1284 return PTR_ERR(jinfo);
1285 } else if (jinfo != info) {
1287 * JEDEC knows better, so overwrite platform ID. We
1288 * can't trust partitions any longer, but we'll let
1289 * mtd apply them anyway, since some partitions may be
1290 * marked read-only, and we don't want to lose that
1291 * information, even if it's not 100% accurate.
1293 dev_warn(dev, "found %s, expected %s\n",
1294 jinfo->name, info->name);
1295 info = jinfo;
1299 mutex_init(&nor->lock);
1302 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
1303 * with the software protection bits set
1306 if (JEDEC_MFR(info) == SNOR_MFR_ATMEL ||
1307 JEDEC_MFR(info) == SNOR_MFR_INTEL ||
1308 JEDEC_MFR(info) == SNOR_MFR_SST ||
1309 info->flags & SPI_NOR_HAS_LOCK) {
1310 write_enable(nor);
1311 write_sr(nor, 0);
1312 spi_nor_wait_till_ready(nor);
1315 if (!mtd->name)
1316 mtd->name = dev_name(dev);
1317 mtd->priv = nor;
1318 mtd->type = MTD_NORFLASH;
1319 mtd->writesize = 1;
1320 mtd->flags = MTD_CAP_NORFLASH;
1321 mtd->size = info->sector_size * info->n_sectors;
1322 mtd->_erase = spi_nor_erase;
1323 mtd->_read = spi_nor_read;
1325 /* NOR protection support for STmicro/Micron chips and similar */
1326 if (JEDEC_MFR(info) == SNOR_MFR_MICRON ||
1327 info->flags & SPI_NOR_HAS_LOCK) {
1328 nor->flash_lock = stm_lock;
1329 nor->flash_unlock = stm_unlock;
1330 nor->flash_is_locked = stm_is_locked;
1333 if (nor->flash_lock && nor->flash_unlock && nor->flash_is_locked) {
1334 mtd->_lock = spi_nor_lock;
1335 mtd->_unlock = spi_nor_unlock;
1336 mtd->_is_locked = spi_nor_is_locked;
1339 /* sst nor chips use AAI word program */
1340 if (info->flags & SST_WRITE)
1341 mtd->_write = sst_write;
1342 else
1343 mtd->_write = spi_nor_write;
1345 if (info->flags & USE_FSR)
1346 nor->flags |= SNOR_F_USE_FSR;
1347 if (info->flags & SPI_NOR_HAS_TB)
1348 nor->flags |= SNOR_F_HAS_SR_TB;
1350 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
1351 /* prefer "small sector" erase if possible */
1352 if (info->flags & SECT_4K) {
1353 nor->erase_opcode = SPINOR_OP_BE_4K;
1354 mtd->erasesize = 4096;
1355 } else if (info->flags & SECT_4K_PMC) {
1356 nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
1357 mtd->erasesize = 4096;
1358 } else
1359 #endif
1361 nor->erase_opcode = SPINOR_OP_SE;
1362 mtd->erasesize = info->sector_size;
1365 if (info->flags & SPI_NOR_NO_ERASE)
1366 mtd->flags |= MTD_NO_ERASE;
1368 mtd->dev.parent = dev;
1369 nor->page_size = info->page_size;
1370 mtd->writebufsize = nor->page_size;
1372 if (np) {
1373 /* If we were instantiated by DT, use it */
1374 if (of_property_read_bool(np, "m25p,fast-read"))
1375 nor->flash_read = SPI_NOR_FAST;
1376 else
1377 nor->flash_read = SPI_NOR_NORMAL;
1378 } else {
1379 /* If we weren't instantiated by DT, default to fast-read */
1380 nor->flash_read = SPI_NOR_FAST;
1383 /* Some devices cannot do fast-read, no matter what DT tells us */
1384 if (info->flags & SPI_NOR_NO_FR)
1385 nor->flash_read = SPI_NOR_NORMAL;
1387 /* Quad/Dual-read mode takes precedence over fast/normal */
1388 if (mode == SPI_NOR_QUAD && info->flags & SPI_NOR_QUAD_READ) {
1389 ret = set_quad_mode(nor, info);
1390 if (ret) {
1391 dev_err(dev, "quad mode not supported\n");
1392 return ret;
1394 nor->flash_read = SPI_NOR_QUAD;
1395 } else if (mode == SPI_NOR_DUAL && info->flags & SPI_NOR_DUAL_READ) {
1396 nor->flash_read = SPI_NOR_DUAL;
1399 /* Default commands */
1400 switch (nor->flash_read) {
1401 case SPI_NOR_QUAD:
1402 nor->read_opcode = SPINOR_OP_READ_1_1_4;
1403 break;
1404 case SPI_NOR_DUAL:
1405 nor->read_opcode = SPINOR_OP_READ_1_1_2;
1406 break;
1407 case SPI_NOR_FAST:
1408 nor->read_opcode = SPINOR_OP_READ_FAST;
1409 break;
1410 case SPI_NOR_NORMAL:
1411 nor->read_opcode = SPINOR_OP_READ;
1412 break;
1413 default:
1414 dev_err(dev, "No Read opcode defined\n");
1415 return -EINVAL;
1418 nor->program_opcode = SPINOR_OP_PP;
1420 if (info->addr_width)
1421 nor->addr_width = info->addr_width;
1422 else if (mtd->size > 0x1000000) {
1423 /* enable 4-byte addressing if the device exceeds 16MiB */
1424 nor->addr_width = 4;
1425 if (JEDEC_MFR(info) == SNOR_MFR_SPANSION) {
1426 /* Dedicated 4-byte command set */
1427 switch (nor->flash_read) {
1428 case SPI_NOR_QUAD:
1429 nor->read_opcode = SPINOR_OP_READ4_1_1_4;
1430 break;
1431 case SPI_NOR_DUAL:
1432 nor->read_opcode = SPINOR_OP_READ4_1_1_2;
1433 break;
1434 case SPI_NOR_FAST:
1435 nor->read_opcode = SPINOR_OP_READ4_FAST;
1436 break;
1437 case SPI_NOR_NORMAL:
1438 nor->read_opcode = SPINOR_OP_READ4;
1439 break;
1441 nor->program_opcode = SPINOR_OP_PP_4B;
1442 /* No small sector erase for 4-byte command set */
1443 nor->erase_opcode = SPINOR_OP_SE_4B;
1444 mtd->erasesize = info->sector_size;
1445 } else
1446 set_4byte(nor, info, 1);
1447 } else {
1448 nor->addr_width = 3;
1451 if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
1452 dev_err(dev, "address width is too large: %u\n",
1453 nor->addr_width);
1454 return -EINVAL;
1457 nor->read_dummy = spi_nor_read_dummy_cycles(nor);
1459 dev_info(dev, "%s (%lld Kbytes)\n", info->name,
1460 (long long)mtd->size >> 10);
1462 dev_dbg(dev,
1463 "mtd .name = %s, .size = 0x%llx (%lldMiB), "
1464 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
1465 mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
1466 mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
1468 if (mtd->numeraseregions)
1469 for (i = 0; i < mtd->numeraseregions; i++)
1470 dev_dbg(dev,
1471 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
1472 ".erasesize = 0x%.8x (%uKiB), "
1473 ".numblocks = %d }\n",
1474 i, (long long)mtd->eraseregions[i].offset,
1475 mtd->eraseregions[i].erasesize,
1476 mtd->eraseregions[i].erasesize / 1024,
1477 mtd->eraseregions[i].numblocks);
1478 return 0;
1480 EXPORT_SYMBOL_GPL(spi_nor_scan);
1482 static const struct flash_info *spi_nor_match_id(const char *name)
1484 const struct flash_info *id = spi_nor_ids;
1486 while (id->name) {
1487 if (!strcmp(name, id->name))
1488 return id;
1489 id++;
1491 return NULL;
1494 MODULE_LICENSE("GPL");
1495 MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
1496 MODULE_AUTHOR("Mike Lavender");
1497 MODULE_DESCRIPTION("framework for SPI NOR");