watchdog/core: Rename some softlockup_* functions
[linux/fpc-iii.git] / drivers / mtd / spi-nor / spi-nor.c
blobcf1d4a15e10a63394b0410f4349e711f602c6c5d
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>
20 #include <linux/slab.h>
22 #include <linux/mtd/mtd.h>
23 #include <linux/of_platform.h>
24 #include <linux/spi/flash.h>
25 #include <linux/mtd/spi-nor.h>
27 /* Define max times to check status register before we give up. */
30 * For everything but full-chip erase; probably could be much smaller, but kept
31 * around for safety for now
33 #define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ)
36 * For full-chip erase, calibrated to a 2MB flash (M25P16); should be scaled up
37 * for larger flash
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
44 struct flash_info {
45 char *name;
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];
53 u8 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.
58 unsigned sector_size;
59 u16 n_sectors;
61 u16 page_size;
62 u16 addr_width;
64 u16 flags;
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
77 * SPI_NOR_HAS_LOCK.
79 #define SPI_S3AN BIT(10) /*
80 * Xilinx Spartan 3AN In-System Flash
81 * (MFR cannot be used for probing
82 * because it has the same value as
83 * ATMEL flashes)
85 #define SPI_NOR_4B_OPCODES BIT(11) /*
86 * Use dedicated 4byte address op codes
87 * to support memory size above 128Mib.
89 #define NO_CHIP_ERASE BIT(12) /* Chip does not support chip erase */
90 #define SPI_NOR_SKIP_SFDP BIT(13) /* Skip parsing of SFDP tables */
91 #define USE_CLSR BIT(14) /* use CLSR command */
94 #define JEDEC_MFR(info) ((info)->id[0])
96 static const struct flash_info *spi_nor_match_id(const char *name);
99 * Read the status register, returning its value in the location
100 * Return the status register value.
101 * Returns negative if error occurred.
103 static int read_sr(struct spi_nor *nor)
105 int ret;
106 u8 val;
108 ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
109 if (ret < 0) {
110 pr_err("error %d reading SR\n", (int) ret);
111 return ret;
114 return val;
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)
124 int ret;
125 u8 val;
127 ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
128 if (ret < 0) {
129 pr_err("error %d reading FSR\n", ret);
130 return ret;
133 return val;
137 * Read configuration register, returning its value in the
138 * location. Return the configuration register value.
139 * Returns negative if error occurred.
141 static int read_cr(struct spi_nor *nor)
143 int ret;
144 u8 val;
146 ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
147 if (ret < 0) {
148 dev_err(nor->dev, "error %d reading CR\n", ret);
149 return ret;
152 return val;
156 * Write status register 1 byte
157 * Returns negative if error occurred.
159 static inline int write_sr(struct spi_nor *nor, u8 val)
161 nor->cmd_buf[0] = val;
162 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
166 * Set write enable latch with Write Enable command.
167 * Returns negative if error occurred.
169 static inline int write_enable(struct spi_nor *nor)
171 return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
175 * Send write disable instruction to the chip.
177 static inline int write_disable(struct spi_nor *nor)
179 return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
182 static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
184 return mtd->priv;
188 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
190 size_t i;
192 for (i = 0; i < size; i++)
193 if (table[i][0] == opcode)
194 return table[i][1];
196 /* No conversion found, keep input op code. */
197 return opcode;
200 static inline u8 spi_nor_convert_3to4_read(u8 opcode)
202 static const u8 spi_nor_3to4_read[][2] = {
203 { SPINOR_OP_READ, SPINOR_OP_READ_4B },
204 { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B },
205 { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
206 { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
207 { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
208 { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
210 { SPINOR_OP_READ_1_1_1_DTR, SPINOR_OP_READ_1_1_1_DTR_4B },
211 { SPINOR_OP_READ_1_2_2_DTR, SPINOR_OP_READ_1_2_2_DTR_4B },
212 { SPINOR_OP_READ_1_4_4_DTR, SPINOR_OP_READ_1_4_4_DTR_4B },
215 return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
216 ARRAY_SIZE(spi_nor_3to4_read));
219 static inline u8 spi_nor_convert_3to4_program(u8 opcode)
221 static const u8 spi_nor_3to4_program[][2] = {
222 { SPINOR_OP_PP, SPINOR_OP_PP_4B },
223 { SPINOR_OP_PP_1_1_4, SPINOR_OP_PP_1_1_4_4B },
224 { SPINOR_OP_PP_1_4_4, SPINOR_OP_PP_1_4_4_4B },
227 return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
228 ARRAY_SIZE(spi_nor_3to4_program));
231 static inline u8 spi_nor_convert_3to4_erase(u8 opcode)
233 static const u8 spi_nor_3to4_erase[][2] = {
234 { SPINOR_OP_BE_4K, SPINOR_OP_BE_4K_4B },
235 { SPINOR_OP_BE_32K, SPINOR_OP_BE_32K_4B },
236 { SPINOR_OP_SE, SPINOR_OP_SE_4B },
239 return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
240 ARRAY_SIZE(spi_nor_3to4_erase));
243 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor,
244 const struct flash_info *info)
246 /* Do some manufacturer fixups first */
247 switch (JEDEC_MFR(info)) {
248 case SNOR_MFR_SPANSION:
249 /* No small sector erase for 4-byte command set */
250 nor->erase_opcode = SPINOR_OP_SE;
251 nor->mtd.erasesize = info->sector_size;
252 break;
254 default:
255 break;
258 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
259 nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
260 nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
263 /* Enable/disable 4-byte addressing mode. */
264 static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info,
265 int enable)
267 int status;
268 bool need_wren = false;
269 u8 cmd;
271 switch (JEDEC_MFR(info)) {
272 case SNOR_MFR_MICRON:
273 /* Some Micron need WREN command; all will accept it */
274 need_wren = true;
275 case SNOR_MFR_MACRONIX:
276 case SNOR_MFR_WINBOND:
277 if (need_wren)
278 write_enable(nor);
280 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
281 status = nor->write_reg(nor, cmd, NULL, 0);
282 if (need_wren)
283 write_disable(nor);
285 return status;
286 default:
287 /* Spansion style */
288 nor->cmd_buf[0] = enable << 7;
289 return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
293 static int s3an_sr_ready(struct spi_nor *nor)
295 int ret;
296 u8 val;
298 ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);
299 if (ret < 0) {
300 dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret);
301 return ret;
304 return !!(val & XSR_RDY);
307 static inline int spi_nor_sr_ready(struct spi_nor *nor)
309 int sr = read_sr(nor);
310 if (sr < 0)
311 return sr;
313 if (nor->flags & SNOR_F_USE_CLSR && sr & (SR_E_ERR | SR_P_ERR)) {
314 if (sr & SR_E_ERR)
315 dev_err(nor->dev, "Erase Error occurred\n");
316 else
317 dev_err(nor->dev, "Programming Error occurred\n");
319 nor->write_reg(nor, SPINOR_OP_CLSR, NULL, 0);
320 return -EIO;
323 return !(sr & SR_WIP);
326 static inline int spi_nor_fsr_ready(struct spi_nor *nor)
328 int fsr = read_fsr(nor);
329 if (fsr < 0)
330 return fsr;
331 else
332 return fsr & FSR_READY;
335 static int spi_nor_ready(struct spi_nor *nor)
337 int sr, fsr;
339 if (nor->flags & SNOR_F_READY_XSR_RDY)
340 sr = s3an_sr_ready(nor);
341 else
342 sr = spi_nor_sr_ready(nor);
343 if (sr < 0)
344 return sr;
345 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
346 if (fsr < 0)
347 return fsr;
348 return sr && fsr;
352 * Service routine to read status register until ready, or timeout occurs.
353 * Returns non-zero if error.
355 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
356 unsigned long timeout_jiffies)
358 unsigned long deadline;
359 int timeout = 0, ret;
361 deadline = jiffies + timeout_jiffies;
363 while (!timeout) {
364 if (time_after_eq(jiffies, deadline))
365 timeout = 1;
367 ret = spi_nor_ready(nor);
368 if (ret < 0)
369 return ret;
370 if (ret)
371 return 0;
373 cond_resched();
376 dev_err(nor->dev, "flash operation timed out\n");
378 return -ETIMEDOUT;
381 static int spi_nor_wait_till_ready(struct spi_nor *nor)
383 return spi_nor_wait_till_ready_with_timeout(nor,
384 DEFAULT_READY_WAIT_JIFFIES);
388 * Erase the whole flash memory
390 * Returns 0 if successful, non-zero otherwise.
392 static int erase_chip(struct spi_nor *nor)
394 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10));
396 return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0);
399 static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)
401 int ret = 0;
403 mutex_lock(&nor->lock);
405 if (nor->prepare) {
406 ret = nor->prepare(nor, ops);
407 if (ret) {
408 dev_err(nor->dev, "failed in the preparation.\n");
409 mutex_unlock(&nor->lock);
410 return ret;
413 return ret;
416 static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
418 if (nor->unprepare)
419 nor->unprepare(nor, ops);
420 mutex_unlock(&nor->lock);
424 * This code converts an address to the Default Address Mode, that has non
425 * power of two page sizes. We must support this mode because it is the default
426 * mode supported by Xilinx tools, it can access the whole flash area and
427 * changing over to the Power-of-two mode is irreversible and corrupts the
428 * original data.
429 * Addr can safely be unsigned int, the biggest S3AN device is smaller than
430 * 4 MiB.
432 static loff_t spi_nor_s3an_addr_convert(struct spi_nor *nor, unsigned int addr)
434 unsigned int offset;
435 unsigned int page;
437 offset = addr % nor->page_size;
438 page = addr / nor->page_size;
439 page <<= (nor->page_size > 512) ? 10 : 9;
441 return page | offset;
445 * Initiate the erasure of a single sector
447 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
449 u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
450 int i;
452 if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
453 addr = spi_nor_s3an_addr_convert(nor, addr);
455 if (nor->erase)
456 return nor->erase(nor, addr);
459 * Default implementation, if driver doesn't have a specialized HW
460 * control
462 for (i = nor->addr_width - 1; i >= 0; i--) {
463 buf[i] = addr & 0xff;
464 addr >>= 8;
467 return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
471 * Erase an address range on the nor chip. The address range may extend
472 * one or more erase sectors. Return an error is there is a problem erasing.
474 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
476 struct spi_nor *nor = mtd_to_spi_nor(mtd);
477 u32 addr, len;
478 uint32_t rem;
479 int ret;
481 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
482 (long long)instr->len);
484 div_u64_rem(instr->len, mtd->erasesize, &rem);
485 if (rem)
486 return -EINVAL;
488 addr = instr->addr;
489 len = instr->len;
491 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_ERASE);
492 if (ret)
493 return ret;
495 /* whole-chip erase? */
496 if (len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) {
497 unsigned long timeout;
499 write_enable(nor);
501 if (erase_chip(nor)) {
502 ret = -EIO;
503 goto erase_err;
507 * Scale the timeout linearly with the size of the flash, with
508 * a minimum calibrated to an old 2MB flash. We could try to
509 * pull these from CFI/SFDP, but these values should be good
510 * enough for now.
512 timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES,
513 CHIP_ERASE_2MB_READY_WAIT_JIFFIES *
514 (unsigned long)(mtd->size / SZ_2M));
515 ret = spi_nor_wait_till_ready_with_timeout(nor, timeout);
516 if (ret)
517 goto erase_err;
519 /* REVISIT in some cases we could speed up erasing large regions
520 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
521 * to use "small sector erase", but that's not always optimal.
524 /* "sector"-at-a-time erase */
525 } else {
526 while (len) {
527 write_enable(nor);
529 ret = spi_nor_erase_sector(nor, addr);
530 if (ret)
531 goto erase_err;
533 addr += mtd->erasesize;
534 len -= mtd->erasesize;
536 ret = spi_nor_wait_till_ready(nor);
537 if (ret)
538 goto erase_err;
542 write_disable(nor);
544 erase_err:
545 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
547 instr->state = ret ? MTD_ERASE_FAILED : MTD_ERASE_DONE;
548 mtd_erase_callback(instr);
550 return ret;
553 static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
554 uint64_t *len)
556 struct mtd_info *mtd = &nor->mtd;
557 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
558 int shift = ffs(mask) - 1;
559 int pow;
561 if (!(sr & mask)) {
562 /* No protection */
563 *ofs = 0;
564 *len = 0;
565 } else {
566 pow = ((sr & mask) ^ mask) >> shift;
567 *len = mtd->size >> pow;
568 if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
569 *ofs = 0;
570 else
571 *ofs = mtd->size - *len;
576 * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
577 * @locked is false); 0 otherwise
579 static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
580 u8 sr, bool locked)
582 loff_t lock_offs;
583 uint64_t lock_len;
585 if (!len)
586 return 1;
588 stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
590 if (locked)
591 /* Requested range is a sub-range of locked range */
592 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
593 else
594 /* Requested range does not overlap with locked range */
595 return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
598 static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
599 u8 sr)
601 return stm_check_lock_status_sr(nor, ofs, len, sr, true);
604 static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
605 u8 sr)
607 return stm_check_lock_status_sr(nor, ofs, len, sr, false);
611 * Lock a region of the flash. Compatible with ST Micro and similar flash.
612 * Supports the block protection bits BP{0,1,2} in the status register
613 * (SR). Does not support these features found in newer SR bitfields:
614 * - SEC: sector/block protect - only handle SEC=0 (block protect)
615 * - CMP: complement protect - only support CMP=0 (range is not complemented)
617 * Support for the following is provided conditionally for some flash:
618 * - TB: top/bottom protect
620 * Sample table portion for 8MB flash (Winbond w25q64fw):
622 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
623 * --------------------------------------------------------------------------
624 * X | X | 0 | 0 | 0 | NONE | NONE
625 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
626 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
627 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
628 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
629 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
630 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
631 * X | X | 1 | 1 | 1 | 8 MB | ALL
632 * ------|-------|-------|-------|-------|---------------|-------------------
633 * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
634 * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
635 * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
636 * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
637 * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
638 * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
640 * Returns negative on errors, 0 on success.
642 static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
644 struct mtd_info *mtd = &nor->mtd;
645 int status_old, status_new;
646 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
647 u8 shift = ffs(mask) - 1, pow, val;
648 loff_t lock_len;
649 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
650 bool use_top;
651 int ret;
653 status_old = read_sr(nor);
654 if (status_old < 0)
655 return status_old;
657 /* If nothing in our range is unlocked, we don't need to do anything */
658 if (stm_is_locked_sr(nor, ofs, len, status_old))
659 return 0;
661 /* If anything below us is unlocked, we can't use 'bottom' protection */
662 if (!stm_is_locked_sr(nor, 0, ofs, status_old))
663 can_be_bottom = false;
665 /* If anything above us is unlocked, we can't use 'top' protection */
666 if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
667 status_old))
668 can_be_top = false;
670 if (!can_be_bottom && !can_be_top)
671 return -EINVAL;
673 /* Prefer top, if both are valid */
674 use_top = can_be_top;
676 /* lock_len: length of region that should end up locked */
677 if (use_top)
678 lock_len = mtd->size - ofs;
679 else
680 lock_len = ofs + len;
683 * Need smallest pow such that:
685 * 1 / (2^pow) <= (len / size)
687 * so (assuming power-of-2 size) we do:
689 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
691 pow = ilog2(mtd->size) - ilog2(lock_len);
692 val = mask - (pow << shift);
693 if (val & ~mask)
694 return -EINVAL;
695 /* Don't "lock" with no region! */
696 if (!(val & mask))
697 return -EINVAL;
699 status_new = (status_old & ~mask & ~SR_TB) | val;
701 /* Disallow further writes if WP pin is asserted */
702 status_new |= SR_SRWD;
704 if (!use_top)
705 status_new |= SR_TB;
707 /* Don't bother if they're the same */
708 if (status_new == status_old)
709 return 0;
711 /* Only modify protection if it will not unlock other areas */
712 if ((status_new & mask) < (status_old & mask))
713 return -EINVAL;
715 write_enable(nor);
716 ret = write_sr(nor, status_new);
717 if (ret)
718 return ret;
719 return spi_nor_wait_till_ready(nor);
723 * Unlock a region of the flash. See stm_lock() for more info
725 * Returns negative on errors, 0 on success.
727 static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
729 struct mtd_info *mtd = &nor->mtd;
730 int status_old, status_new;
731 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
732 u8 shift = ffs(mask) - 1, pow, val;
733 loff_t lock_len;
734 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
735 bool use_top;
736 int ret;
738 status_old = read_sr(nor);
739 if (status_old < 0)
740 return status_old;
742 /* If nothing in our range is locked, we don't need to do anything */
743 if (stm_is_unlocked_sr(nor, ofs, len, status_old))
744 return 0;
746 /* If anything below us is locked, we can't use 'top' protection */
747 if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
748 can_be_top = false;
750 /* If anything above us is locked, we can't use 'bottom' protection */
751 if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
752 status_old))
753 can_be_bottom = false;
755 if (!can_be_bottom && !can_be_top)
756 return -EINVAL;
758 /* Prefer top, if both are valid */
759 use_top = can_be_top;
761 /* lock_len: length of region that should remain locked */
762 if (use_top)
763 lock_len = mtd->size - (ofs + len);
764 else
765 lock_len = ofs;
768 * Need largest pow such that:
770 * 1 / (2^pow) >= (len / size)
772 * so (assuming power-of-2 size) we do:
774 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
776 pow = ilog2(mtd->size) - order_base_2(lock_len);
777 if (lock_len == 0) {
778 val = 0; /* fully unlocked */
779 } else {
780 val = mask - (pow << shift);
781 /* Some power-of-two sizes are not supported */
782 if (val & ~mask)
783 return -EINVAL;
786 status_new = (status_old & ~mask & ~SR_TB) | val;
788 /* Don't protect status register if we're fully unlocked */
789 if (lock_len == 0)
790 status_new &= ~SR_SRWD;
792 if (!use_top)
793 status_new |= SR_TB;
795 /* Don't bother if they're the same */
796 if (status_new == status_old)
797 return 0;
799 /* Only modify protection if it will not lock other areas */
800 if ((status_new & mask) > (status_old & mask))
801 return -EINVAL;
803 write_enable(nor);
804 ret = write_sr(nor, status_new);
805 if (ret)
806 return ret;
807 return spi_nor_wait_till_ready(nor);
811 * Check if a region of the flash is (completely) locked. See stm_lock() for
812 * more info.
814 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
815 * negative on errors.
817 static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
819 int status;
821 status = read_sr(nor);
822 if (status < 0)
823 return status;
825 return stm_is_locked_sr(nor, ofs, len, status);
828 static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
830 struct spi_nor *nor = mtd_to_spi_nor(mtd);
831 int ret;
833 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_LOCK);
834 if (ret)
835 return ret;
837 ret = nor->flash_lock(nor, ofs, len);
839 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
840 return ret;
843 static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
845 struct spi_nor *nor = mtd_to_spi_nor(mtd);
846 int ret;
848 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
849 if (ret)
850 return ret;
852 ret = nor->flash_unlock(nor, ofs, len);
854 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
855 return ret;
858 static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
860 struct spi_nor *nor = mtd_to_spi_nor(mtd);
861 int ret;
863 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
864 if (ret)
865 return ret;
867 ret = nor->flash_is_locked(nor, ofs, len);
869 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
870 return ret;
873 /* Used when the "_ext_id" is two bytes at most */
874 #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
875 .id = { \
876 ((_jedec_id) >> 16) & 0xff, \
877 ((_jedec_id) >> 8) & 0xff, \
878 (_jedec_id) & 0xff, \
879 ((_ext_id) >> 8) & 0xff, \
880 (_ext_id) & 0xff, \
881 }, \
882 .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))), \
883 .sector_size = (_sector_size), \
884 .n_sectors = (_n_sectors), \
885 .page_size = 256, \
886 .flags = (_flags),
888 #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
889 .id = { \
890 ((_jedec_id) >> 16) & 0xff, \
891 ((_jedec_id) >> 8) & 0xff, \
892 (_jedec_id) & 0xff, \
893 ((_ext_id) >> 16) & 0xff, \
894 ((_ext_id) >> 8) & 0xff, \
895 (_ext_id) & 0xff, \
896 }, \
897 .id_len = 6, \
898 .sector_size = (_sector_size), \
899 .n_sectors = (_n_sectors), \
900 .page_size = 256, \
901 .flags = (_flags),
903 #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
904 .sector_size = (_sector_size), \
905 .n_sectors = (_n_sectors), \
906 .page_size = (_page_size), \
907 .addr_width = (_addr_width), \
908 .flags = (_flags),
910 #define S3AN_INFO(_jedec_id, _n_sectors, _page_size) \
911 .id = { \
912 ((_jedec_id) >> 16) & 0xff, \
913 ((_jedec_id) >> 8) & 0xff, \
914 (_jedec_id) & 0xff \
915 }, \
916 .id_len = 3, \
917 .sector_size = (8*_page_size), \
918 .n_sectors = (_n_sectors), \
919 .page_size = _page_size, \
920 .addr_width = 3, \
921 .flags = SPI_NOR_NO_FR | SPI_S3AN,
923 /* NOTE: double check command sets and memory organization when you add
924 * more nor chips. This current list focusses on newer chips, which
925 * have been converging on command sets which including JEDEC ID.
927 * All newly added entries should describe *hardware* and should use SECT_4K
928 * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage
929 * scenarios excluding small sectors there is config option that can be
930 * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS.
931 * For historical (and compatibility) reasons (before we got above config) some
932 * old entries may be missing 4K flag.
934 static const struct flash_info spi_nor_ids[] = {
935 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
936 { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
937 { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
939 { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
940 { "at25df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
941 { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
942 { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
944 { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
945 { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
946 { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
947 { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
949 { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
951 /* EON -- en25xxx */
952 { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
953 { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
954 { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
955 { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
956 { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
957 { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
958 { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
959 { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K) },
961 /* ESMT */
962 { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K | SPI_NOR_HAS_LOCK) },
963 { "f25l32qa", INFO(0x8c4116, 0, 64 * 1024, 64, SECT_4K | SPI_NOR_HAS_LOCK) },
964 { "f25l64qa", INFO(0x8c4117, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_HAS_LOCK) },
966 /* Everspin */
967 { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
968 { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
969 { "mr25h40", CAT25_INFO(512 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
971 /* Fujitsu */
972 { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE) },
974 /* GigaDevice */
976 "gd25q16", INFO(0xc84015, 0, 64 * 1024, 32,
977 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
978 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
981 "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64,
982 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
983 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
986 "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128,
987 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
988 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
991 "gd25lq64c", INFO(0xc86017, 0, 64 * 1024, 128,
992 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
993 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
996 "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256,
997 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
998 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1001 /* Intel/Numonyx -- xxxs33b */
1002 { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
1003 { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
1004 { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
1006 /* ISSI */
1007 { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K) },
1009 /* Macronix */
1010 { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K) },
1011 { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
1012 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
1013 { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
1014 { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
1015 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, SECT_4K) },
1016 { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) },
1017 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, SECT_4K) },
1018 { "mx25u2033e", INFO(0xc22532, 0, 64 * 1024, 4, SECT_4K) },
1019 { "mx25u4035", INFO(0xc22533, 0, 64 * 1024, 8, SECT_4K) },
1020 { "mx25u8035", INFO(0xc22534, 0, 64 * 1024, 16, SECT_4K) },
1021 { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
1022 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
1023 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
1024 { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1025 { "mx25u25635f", INFO(0xc22539, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_4B_OPCODES) },
1026 { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
1027 { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1028 { "mx66u51235f", INFO(0xc2253a, 0, 64 * 1024, 1024, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
1029 { "mx66l1g45g", INFO(0xc2201b, 0, 64 * 1024, 2048, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1030 { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ) },
1032 /* Micron */
1033 { "n25q016a", INFO(0x20bb15, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_QUAD_READ) },
1034 { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
1035 { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
1036 { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
1037 { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
1038 { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
1039 { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
1040 { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1041 { "n25q256ax1", INFO(0x20bb19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ) },
1042 { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
1043 { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
1044 { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
1045 { "n25q00a", INFO(0x20bb21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
1047 /* PMC */
1048 { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
1049 { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
1050 { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
1052 /* Spansion -- single (large) sector size only, at least
1053 * for the chips listed here (without boot sectors).
1055 { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1056 { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1057 { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, USE_CLSR) },
1058 { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
1059 { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
1060 { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
1061 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
1062 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
1063 { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
1064 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
1065 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
1066 { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
1067 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
1068 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
1069 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
1070 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
1071 { "s25fl004k", INFO(0xef4013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1072 { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1073 { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1074 { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
1075 { "s25fl116k", INFO(0x014015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1076 { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) },
1077 { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
1078 { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ) },
1079 { "s25fl208k", INFO(0x014014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ) },
1080 { "s25fl064l", INFO(0x016017, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
1082 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
1083 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
1084 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
1085 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
1086 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
1087 { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
1088 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
1089 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
1090 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
1091 { "sst25wf020a", INFO(0x621612, 0, 64 * 1024, 4, SECT_4K) },
1092 { "sst25wf040b", INFO(0x621613, 0, 64 * 1024, 8, SECT_4K) },
1093 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
1094 { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
1095 { "sst26vf064b", INFO(0xbf2643, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1097 /* ST Microelectronics -- newer production may have feature updates */
1098 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
1099 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
1100 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
1101 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
1102 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
1103 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
1104 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
1105 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
1106 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
1108 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
1109 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
1110 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
1111 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
1112 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
1113 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
1114 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
1115 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
1116 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
1118 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
1119 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
1120 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
1122 { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
1123 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
1124 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
1126 { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
1127 { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
1128 { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
1129 { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
1130 { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
1131 { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
1133 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
1134 { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
1135 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
1136 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
1137 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
1138 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
1139 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
1140 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
1141 { "w25q20cl", INFO(0xef4012, 0, 64 * 1024, 4, SECT_4K) },
1142 { "w25q20bw", INFO(0xef5012, 0, 64 * 1024, 4, SECT_4K) },
1143 { "w25q20ew", INFO(0xef6012, 0, 64 * 1024, 4, SECT_4K) },
1144 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
1146 "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64,
1147 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1148 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1150 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
1151 { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
1153 "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128,
1154 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1155 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1158 "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256,
1159 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1160 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1162 { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
1163 { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
1164 { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
1165 { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1166 { "w25m512jv", INFO(0xef7119, 0, 64 * 1024, 1024,
1167 SECT_4K | SPI_NOR_QUAD_READ | SPI_NOR_DUAL_READ) },
1169 /* Catalyst / On Semiconductor -- non-JEDEC */
1170 { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1171 { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1172 { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1173 { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1174 { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1176 /* Xilinx S3AN Internal Flash */
1177 { "3S50AN", S3AN_INFO(0x1f2200, 64, 264) },
1178 { "3S200AN", S3AN_INFO(0x1f2400, 256, 264) },
1179 { "3S400AN", S3AN_INFO(0x1f2400, 256, 264) },
1180 { "3S700AN", S3AN_INFO(0x1f2500, 512, 264) },
1181 { "3S1400AN", S3AN_INFO(0x1f2600, 512, 528) },
1182 { },
1185 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
1187 int tmp;
1188 u8 id[SPI_NOR_MAX_ID_LEN];
1189 const struct flash_info *info;
1191 tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
1192 if (tmp < 0) {
1193 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
1194 return ERR_PTR(tmp);
1197 for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
1198 info = &spi_nor_ids[tmp];
1199 if (info->id_len) {
1200 if (!memcmp(info->id, id, info->id_len))
1201 return &spi_nor_ids[tmp];
1204 dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
1205 id[0], id[1], id[2]);
1206 return ERR_PTR(-ENODEV);
1209 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
1210 size_t *retlen, u_char *buf)
1212 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1213 int ret;
1215 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
1217 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ);
1218 if (ret)
1219 return ret;
1221 while (len) {
1222 loff_t addr = from;
1224 if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
1225 addr = spi_nor_s3an_addr_convert(nor, addr);
1227 ret = nor->read(nor, addr, len, buf);
1228 if (ret == 0) {
1229 /* We shouldn't see 0-length reads */
1230 ret = -EIO;
1231 goto read_err;
1233 if (ret < 0)
1234 goto read_err;
1236 WARN_ON(ret > len);
1237 *retlen += ret;
1238 buf += ret;
1239 from += ret;
1240 len -= ret;
1242 ret = 0;
1244 read_err:
1245 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
1246 return ret;
1249 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
1250 size_t *retlen, const u_char *buf)
1252 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1253 size_t actual;
1254 int ret;
1256 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1258 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
1259 if (ret)
1260 return ret;
1262 write_enable(nor);
1264 nor->sst_write_second = false;
1266 actual = to % 2;
1267 /* Start write from odd address. */
1268 if (actual) {
1269 nor->program_opcode = SPINOR_OP_BP;
1271 /* write one byte. */
1272 ret = nor->write(nor, to, 1, buf);
1273 if (ret < 0)
1274 goto sst_write_err;
1275 WARN(ret != 1, "While writing 1 byte written %i bytes\n",
1276 (int)ret);
1277 ret = spi_nor_wait_till_ready(nor);
1278 if (ret)
1279 goto sst_write_err;
1281 to += actual;
1283 /* Write out most of the data here. */
1284 for (; actual < len - 1; actual += 2) {
1285 nor->program_opcode = SPINOR_OP_AAI_WP;
1287 /* write two bytes. */
1288 ret = nor->write(nor, to, 2, buf + actual);
1289 if (ret < 0)
1290 goto sst_write_err;
1291 WARN(ret != 2, "While writing 2 bytes written %i bytes\n",
1292 (int)ret);
1293 ret = spi_nor_wait_till_ready(nor);
1294 if (ret)
1295 goto sst_write_err;
1296 to += 2;
1297 nor->sst_write_second = true;
1299 nor->sst_write_second = false;
1301 write_disable(nor);
1302 ret = spi_nor_wait_till_ready(nor);
1303 if (ret)
1304 goto sst_write_err;
1306 /* Write out trailing byte if it exists. */
1307 if (actual != len) {
1308 write_enable(nor);
1310 nor->program_opcode = SPINOR_OP_BP;
1311 ret = nor->write(nor, to, 1, buf + actual);
1312 if (ret < 0)
1313 goto sst_write_err;
1314 WARN(ret != 1, "While writing 1 byte written %i bytes\n",
1315 (int)ret);
1316 ret = spi_nor_wait_till_ready(nor);
1317 if (ret)
1318 goto sst_write_err;
1319 write_disable(nor);
1320 actual += 1;
1322 sst_write_err:
1323 *retlen += actual;
1324 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
1325 return ret;
1329 * Write an address range to the nor chip. Data must be written in
1330 * FLASH_PAGESIZE chunks. The address range may be any size provided
1331 * it is within the physical boundaries.
1333 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
1334 size_t *retlen, const u_char *buf)
1336 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1337 size_t page_offset, page_remain, i;
1338 ssize_t ret;
1340 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1342 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
1343 if (ret)
1344 return ret;
1346 for (i = 0; i < len; ) {
1347 ssize_t written;
1348 loff_t addr = to + i;
1351 * If page_size is a power of two, the offset can be quickly
1352 * calculated with an AND operation. On the other cases we
1353 * need to do a modulus operation (more expensive).
1354 * Power of two numbers have only one bit set and we can use
1355 * the instruction hweight32 to detect if we need to do a
1356 * modulus (do_div()) or not.
1358 if (hweight32(nor->page_size) == 1) {
1359 page_offset = addr & (nor->page_size - 1);
1360 } else {
1361 uint64_t aux = addr;
1363 page_offset = do_div(aux, nor->page_size);
1365 /* the size of data remaining on the first page */
1366 page_remain = min_t(size_t,
1367 nor->page_size - page_offset, len - i);
1369 if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
1370 addr = spi_nor_s3an_addr_convert(nor, addr);
1372 write_enable(nor);
1373 ret = nor->write(nor, addr, page_remain, buf + i);
1374 if (ret < 0)
1375 goto write_err;
1376 written = ret;
1378 ret = spi_nor_wait_till_ready(nor);
1379 if (ret)
1380 goto write_err;
1381 *retlen += written;
1382 i += written;
1383 if (written != page_remain) {
1384 dev_err(nor->dev,
1385 "While writing %zu bytes written %zd bytes\n",
1386 page_remain, written);
1387 ret = -EIO;
1388 goto write_err;
1392 write_err:
1393 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
1394 return ret;
1398 * macronix_quad_enable() - set QE bit in Status Register.
1399 * @nor: pointer to a 'struct spi_nor'
1401 * Set the Quad Enable (QE) bit in the Status Register.
1403 * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
1405 * Return: 0 on success, -errno otherwise.
1407 static int macronix_quad_enable(struct spi_nor *nor)
1409 int ret, val;
1411 val = read_sr(nor);
1412 if (val < 0)
1413 return val;
1414 if (val & SR_QUAD_EN_MX)
1415 return 0;
1417 write_enable(nor);
1419 write_sr(nor, val | SR_QUAD_EN_MX);
1421 ret = spi_nor_wait_till_ready(nor);
1422 if (ret)
1423 return ret;
1425 ret = read_sr(nor);
1426 if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
1427 dev_err(nor->dev, "Macronix Quad bit not set\n");
1428 return -EINVAL;
1431 return 0;
1435 * Write status Register and configuration register with 2 bytes
1436 * The first byte will be written to the status register, while the
1437 * second byte will be written to the configuration register.
1438 * Return negative if error occurred.
1440 static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
1442 int ret;
1444 write_enable(nor);
1446 ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
1447 if (ret < 0) {
1448 dev_err(nor->dev,
1449 "error while writing configuration register\n");
1450 return -EINVAL;
1453 ret = spi_nor_wait_till_ready(nor);
1454 if (ret) {
1455 dev_err(nor->dev,
1456 "timeout while writing configuration register\n");
1457 return ret;
1460 return 0;
1464 * spansion_quad_enable() - set QE bit in Configuraiton Register.
1465 * @nor: pointer to a 'struct spi_nor'
1467 * Set the Quad Enable (QE) bit in the Configuration Register.
1468 * This function is kept for legacy purpose because it has been used for a
1469 * long time without anybody complaining but it should be considered as
1470 * deprecated and maybe buggy.
1471 * First, this function doesn't care about the previous values of the Status
1472 * and Configuration Registers when it sets the QE bit (bit 1) in the
1473 * Configuration Register: all other bits are cleared, which may have unwanted
1474 * side effects like removing some block protections.
1475 * Secondly, it uses the Read Configuration Register (35h) instruction though
1476 * some very old and few memories don't support this instruction. If a pull-up
1477 * resistor is present on the MISO/IO1 line, we might still be able to pass the
1478 * "read back" test because the QSPI memory doesn't recognize the command,
1479 * so leaves the MISO/IO1 line state unchanged, hence read_cr() returns 0xFF.
1481 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1482 * memories.
1484 * Return: 0 on success, -errno otherwise.
1486 static int spansion_quad_enable(struct spi_nor *nor)
1488 u8 sr_cr[2] = {0, CR_QUAD_EN_SPAN};
1489 int ret;
1491 ret = write_sr_cr(nor, sr_cr);
1492 if (ret)
1493 return ret;
1495 /* read back and check it */
1496 ret = read_cr(nor);
1497 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
1498 dev_err(nor->dev, "Spansion Quad bit not set\n");
1499 return -EINVAL;
1502 return 0;
1506 * spansion_no_read_cr_quad_enable() - set QE bit in Configuration Register.
1507 * @nor: pointer to a 'struct spi_nor'
1509 * Set the Quad Enable (QE) bit in the Configuration Register.
1510 * This function should be used with QSPI memories not supporting the Read
1511 * Configuration Register (35h) instruction.
1513 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1514 * memories.
1516 * Return: 0 on success, -errno otherwise.
1518 static int spansion_no_read_cr_quad_enable(struct spi_nor *nor)
1520 u8 sr_cr[2];
1521 int ret;
1523 /* Keep the current value of the Status Register. */
1524 ret = read_sr(nor);
1525 if (ret < 0) {
1526 dev_err(nor->dev, "error while reading status register\n");
1527 return -EINVAL;
1529 sr_cr[0] = ret;
1530 sr_cr[1] = CR_QUAD_EN_SPAN;
1532 return write_sr_cr(nor, sr_cr);
1536 * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
1537 * @nor: pointer to a 'struct spi_nor'
1539 * Set the Quad Enable (QE) bit in the Configuration Register.
1540 * This function should be used with QSPI memories supporting the Read
1541 * Configuration Register (35h) instruction.
1543 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1544 * memories.
1546 * Return: 0 on success, -errno otherwise.
1548 static int spansion_read_cr_quad_enable(struct spi_nor *nor)
1550 struct device *dev = nor->dev;
1551 u8 sr_cr[2];
1552 int ret;
1554 /* Check current Quad Enable bit value. */
1555 ret = read_cr(nor);
1556 if (ret < 0) {
1557 dev_err(dev, "error while reading configuration register\n");
1558 return -EINVAL;
1561 if (ret & CR_QUAD_EN_SPAN)
1562 return 0;
1564 sr_cr[1] = ret | CR_QUAD_EN_SPAN;
1566 /* Keep the current value of the Status Register. */
1567 ret = read_sr(nor);
1568 if (ret < 0) {
1569 dev_err(dev, "error while reading status register\n");
1570 return -EINVAL;
1572 sr_cr[0] = ret;
1574 ret = write_sr_cr(nor, sr_cr);
1575 if (ret)
1576 return ret;
1578 /* Read back and check it. */
1579 ret = read_cr(nor);
1580 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
1581 dev_err(nor->dev, "Spansion Quad bit not set\n");
1582 return -EINVAL;
1585 return 0;
1589 * sr2_bit7_quad_enable() - set QE bit in Status Register 2.
1590 * @nor: pointer to a 'struct spi_nor'
1592 * Set the Quad Enable (QE) bit in the Status Register 2.
1594 * This is one of the procedures to set the QE bit described in the SFDP
1595 * (JESD216 rev B) specification but no manufacturer using this procedure has
1596 * been identified yet, hence the name of the function.
1598 * Return: 0 on success, -errno otherwise.
1600 static int sr2_bit7_quad_enable(struct spi_nor *nor)
1602 u8 sr2;
1603 int ret;
1605 /* Check current Quad Enable bit value. */
1606 ret = nor->read_reg(nor, SPINOR_OP_RDSR2, &sr2, 1);
1607 if (ret)
1608 return ret;
1609 if (sr2 & SR2_QUAD_EN_BIT7)
1610 return 0;
1612 /* Update the Quad Enable bit. */
1613 sr2 |= SR2_QUAD_EN_BIT7;
1615 write_enable(nor);
1617 ret = nor->write_reg(nor, SPINOR_OP_WRSR2, &sr2, 1);
1618 if (ret < 0) {
1619 dev_err(nor->dev, "error while writing status register 2\n");
1620 return -EINVAL;
1623 ret = spi_nor_wait_till_ready(nor);
1624 if (ret < 0) {
1625 dev_err(nor->dev, "timeout while writing status register 2\n");
1626 return ret;
1629 /* Read back and check it. */
1630 ret = nor->read_reg(nor, SPINOR_OP_RDSR2, &sr2, 1);
1631 if (!(ret > 0 && (sr2 & SR2_QUAD_EN_BIT7))) {
1632 dev_err(nor->dev, "SR2 Quad bit not set\n");
1633 return -EINVAL;
1636 return 0;
1639 static int spi_nor_check(struct spi_nor *nor)
1641 if (!nor->dev || !nor->read || !nor->write ||
1642 !nor->read_reg || !nor->write_reg) {
1643 pr_err("spi-nor: please fill all the necessary fields!\n");
1644 return -EINVAL;
1647 return 0;
1650 static int s3an_nor_scan(const struct flash_info *info, struct spi_nor *nor)
1652 int ret;
1653 u8 val;
1655 ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);
1656 if (ret < 0) {
1657 dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret);
1658 return ret;
1661 nor->erase_opcode = SPINOR_OP_XSE;
1662 nor->program_opcode = SPINOR_OP_XPP;
1663 nor->read_opcode = SPINOR_OP_READ;
1664 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
1667 * This flashes have a page size of 264 or 528 bytes (known as
1668 * Default addressing mode). It can be changed to a more standard
1669 * Power of two mode where the page size is 256/512. This comes
1670 * with a price: there is 3% less of space, the data is corrupted
1671 * and the page size cannot be changed back to default addressing
1672 * mode.
1674 * The current addressing mode can be read from the XRDSR register
1675 * and should not be changed, because is a destructive operation.
1677 if (val & XSR_PAGESIZE) {
1678 /* Flash in Power of 2 mode */
1679 nor->page_size = (nor->page_size == 264) ? 256 : 512;
1680 nor->mtd.writebufsize = nor->page_size;
1681 nor->mtd.size = 8 * nor->page_size * info->n_sectors;
1682 nor->mtd.erasesize = 8 * nor->page_size;
1683 } else {
1684 /* Flash in Default addressing mode */
1685 nor->flags |= SNOR_F_S3AN_ADDR_DEFAULT;
1688 return 0;
1691 struct spi_nor_read_command {
1692 u8 num_mode_clocks;
1693 u8 num_wait_states;
1694 u8 opcode;
1695 enum spi_nor_protocol proto;
1698 struct spi_nor_pp_command {
1699 u8 opcode;
1700 enum spi_nor_protocol proto;
1703 enum spi_nor_read_command_index {
1704 SNOR_CMD_READ,
1705 SNOR_CMD_READ_FAST,
1706 SNOR_CMD_READ_1_1_1_DTR,
1708 /* Dual SPI */
1709 SNOR_CMD_READ_1_1_2,
1710 SNOR_CMD_READ_1_2_2,
1711 SNOR_CMD_READ_2_2_2,
1712 SNOR_CMD_READ_1_2_2_DTR,
1714 /* Quad SPI */
1715 SNOR_CMD_READ_1_1_4,
1716 SNOR_CMD_READ_1_4_4,
1717 SNOR_CMD_READ_4_4_4,
1718 SNOR_CMD_READ_1_4_4_DTR,
1720 /* Octo SPI */
1721 SNOR_CMD_READ_1_1_8,
1722 SNOR_CMD_READ_1_8_8,
1723 SNOR_CMD_READ_8_8_8,
1724 SNOR_CMD_READ_1_8_8_DTR,
1726 SNOR_CMD_READ_MAX
1729 enum spi_nor_pp_command_index {
1730 SNOR_CMD_PP,
1732 /* Quad SPI */
1733 SNOR_CMD_PP_1_1_4,
1734 SNOR_CMD_PP_1_4_4,
1735 SNOR_CMD_PP_4_4_4,
1737 /* Octo SPI */
1738 SNOR_CMD_PP_1_1_8,
1739 SNOR_CMD_PP_1_8_8,
1740 SNOR_CMD_PP_8_8_8,
1742 SNOR_CMD_PP_MAX
1745 struct spi_nor_flash_parameter {
1746 u64 size;
1747 u32 page_size;
1749 struct spi_nor_hwcaps hwcaps;
1750 struct spi_nor_read_command reads[SNOR_CMD_READ_MAX];
1751 struct spi_nor_pp_command page_programs[SNOR_CMD_PP_MAX];
1753 int (*quad_enable)(struct spi_nor *nor);
1756 static void
1757 spi_nor_set_read_settings(struct spi_nor_read_command *read,
1758 u8 num_mode_clocks,
1759 u8 num_wait_states,
1760 u8 opcode,
1761 enum spi_nor_protocol proto)
1763 read->num_mode_clocks = num_mode_clocks;
1764 read->num_wait_states = num_wait_states;
1765 read->opcode = opcode;
1766 read->proto = proto;
1769 static void
1770 spi_nor_set_pp_settings(struct spi_nor_pp_command *pp,
1771 u8 opcode,
1772 enum spi_nor_protocol proto)
1774 pp->opcode = opcode;
1775 pp->proto = proto;
1779 * Serial Flash Discoverable Parameters (SFDP) parsing.
1783 * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
1784 * @nor: pointer to a 'struct spi_nor'
1785 * @addr: offset in the SFDP area to start reading data from
1786 * @len: number of bytes to read
1787 * @buf: buffer where the SFDP data are copied into
1789 * Whatever the actual numbers of bytes for address and dummy cycles are
1790 * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
1791 * followed by a 3-byte address and 8 dummy clock cycles.
1793 * Return: 0 on success, -errno otherwise.
1795 static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr,
1796 size_t len, void *buf)
1798 u8 addr_width, read_opcode, read_dummy;
1799 int ret;
1801 read_opcode = nor->read_opcode;
1802 addr_width = nor->addr_width;
1803 read_dummy = nor->read_dummy;
1805 nor->read_opcode = SPINOR_OP_RDSFDP;
1806 nor->addr_width = 3;
1807 nor->read_dummy = 8;
1809 while (len) {
1810 ret = nor->read(nor, addr, len, (u8 *)buf);
1811 if (!ret || ret > len) {
1812 ret = -EIO;
1813 goto read_err;
1815 if (ret < 0)
1816 goto read_err;
1818 buf += ret;
1819 addr += ret;
1820 len -= ret;
1822 ret = 0;
1824 read_err:
1825 nor->read_opcode = read_opcode;
1826 nor->addr_width = addr_width;
1827 nor->read_dummy = read_dummy;
1829 return ret;
1832 struct sfdp_parameter_header {
1833 u8 id_lsb;
1834 u8 minor;
1835 u8 major;
1836 u8 length; /* in double words */
1837 u8 parameter_table_pointer[3]; /* byte address */
1838 u8 id_msb;
1841 #define SFDP_PARAM_HEADER_ID(p) (((p)->id_msb << 8) | (p)->id_lsb)
1842 #define SFDP_PARAM_HEADER_PTP(p) \
1843 (((p)->parameter_table_pointer[2] << 16) | \
1844 ((p)->parameter_table_pointer[1] << 8) | \
1845 ((p)->parameter_table_pointer[0] << 0))
1847 #define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
1848 #define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
1850 #define SFDP_SIGNATURE 0x50444653U
1851 #define SFDP_JESD216_MAJOR 1
1852 #define SFDP_JESD216_MINOR 0
1853 #define SFDP_JESD216A_MINOR 5
1854 #define SFDP_JESD216B_MINOR 6
1856 struct sfdp_header {
1857 u32 signature; /* Ox50444653U <=> "SFDP" */
1858 u8 minor;
1859 u8 major;
1860 u8 nph; /* 0-base number of parameter headers */
1861 u8 unused;
1863 /* Basic Flash Parameter Table. */
1864 struct sfdp_parameter_header bfpt_header;
1867 /* Basic Flash Parameter Table */
1870 * JESD216 rev B defines a Basic Flash Parameter Table of 16 DWORDs.
1871 * They are indexed from 1 but C arrays are indexed from 0.
1873 #define BFPT_DWORD(i) ((i) - 1)
1874 #define BFPT_DWORD_MAX 16
1876 /* The first version of JESB216 defined only 9 DWORDs. */
1877 #define BFPT_DWORD_MAX_JESD216 9
1879 /* 1st DWORD. */
1880 #define BFPT_DWORD1_FAST_READ_1_1_2 BIT(16)
1881 #define BFPT_DWORD1_ADDRESS_BYTES_MASK GENMASK(18, 17)
1882 #define BFPT_DWORD1_ADDRESS_BYTES_3_ONLY (0x0UL << 17)
1883 #define BFPT_DWORD1_ADDRESS_BYTES_3_OR_4 (0x1UL << 17)
1884 #define BFPT_DWORD1_ADDRESS_BYTES_4_ONLY (0x2UL << 17)
1885 #define BFPT_DWORD1_DTR BIT(19)
1886 #define BFPT_DWORD1_FAST_READ_1_2_2 BIT(20)
1887 #define BFPT_DWORD1_FAST_READ_1_4_4 BIT(21)
1888 #define BFPT_DWORD1_FAST_READ_1_1_4 BIT(22)
1890 /* 5th DWORD. */
1891 #define BFPT_DWORD5_FAST_READ_2_2_2 BIT(0)
1892 #define BFPT_DWORD5_FAST_READ_4_4_4 BIT(4)
1894 /* 11th DWORD. */
1895 #define BFPT_DWORD11_PAGE_SIZE_SHIFT 4
1896 #define BFPT_DWORD11_PAGE_SIZE_MASK GENMASK(7, 4)
1898 /* 15th DWORD. */
1901 * (from JESD216 rev B)
1902 * Quad Enable Requirements (QER):
1903 * - 000b: Device does not have a QE bit. Device detects 1-1-4 and 1-4-4
1904 * reads based on instruction. DQ3/HOLD# functions are hold during
1905 * instruction phase.
1906 * - 001b: QE is bit 1 of status register 2. It is set via Write Status with
1907 * two data bytes where bit 1 of the second byte is one.
1908 * [...]
1909 * Writing only one byte to the status register has the side-effect of
1910 * clearing status register 2, including the QE bit. The 100b code is
1911 * used if writing one byte to the status register does not modify
1912 * status register 2.
1913 * - 010b: QE is bit 6 of status register 1. It is set via Write Status with
1914 * one data byte where bit 6 is one.
1915 * [...]
1916 * - 011b: QE is bit 7 of status register 2. It is set via Write status
1917 * register 2 instruction 3Eh with one data byte where bit 7 is one.
1918 * [...]
1919 * The status register 2 is read using instruction 3Fh.
1920 * - 100b: QE is bit 1 of status register 2. It is set via Write Status with
1921 * two data bytes where bit 1 of the second byte is one.
1922 * [...]
1923 * In contrast to the 001b code, writing one byte to the status
1924 * register does not modify status register 2.
1925 * - 101b: QE is bit 1 of status register 2. Status register 1 is read using
1926 * Read Status instruction 05h. Status register2 is read using
1927 * instruction 35h. QE is set via Writ Status instruction 01h with
1928 * two data bytes where bit 1 of the second byte is one.
1929 * [...]
1931 #define BFPT_DWORD15_QER_MASK GENMASK(22, 20)
1932 #define BFPT_DWORD15_QER_NONE (0x0UL << 20) /* Micron */
1933 #define BFPT_DWORD15_QER_SR2_BIT1_BUGGY (0x1UL << 20)
1934 #define BFPT_DWORD15_QER_SR1_BIT6 (0x2UL << 20) /* Macronix */
1935 #define BFPT_DWORD15_QER_SR2_BIT7 (0x3UL << 20)
1936 #define BFPT_DWORD15_QER_SR2_BIT1_NO_RD (0x4UL << 20)
1937 #define BFPT_DWORD15_QER_SR2_BIT1 (0x5UL << 20) /* Spansion */
1939 struct sfdp_bfpt {
1940 u32 dwords[BFPT_DWORD_MAX];
1943 /* Fast Read settings. */
1945 static inline void
1946 spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read,
1947 u16 half,
1948 enum spi_nor_protocol proto)
1950 read->num_mode_clocks = (half >> 5) & 0x07;
1951 read->num_wait_states = (half >> 0) & 0x1f;
1952 read->opcode = (half >> 8) & 0xff;
1953 read->proto = proto;
1956 struct sfdp_bfpt_read {
1957 /* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
1958 u32 hwcaps;
1961 * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us
1962 * whether the Fast Read x-y-z command is supported.
1964 u32 supported_dword;
1965 u32 supported_bit;
1968 * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
1969 * encodes the op code, the number of mode clocks and the number of wait
1970 * states to be used by Fast Read x-y-z command.
1972 u32 settings_dword;
1973 u32 settings_shift;
1975 /* The SPI protocol for this Fast Read x-y-z command. */
1976 enum spi_nor_protocol proto;
1979 static const struct sfdp_bfpt_read sfdp_bfpt_reads[] = {
1980 /* Fast Read 1-1-2 */
1982 SNOR_HWCAPS_READ_1_1_2,
1983 BFPT_DWORD(1), BIT(16), /* Supported bit */
1984 BFPT_DWORD(4), 0, /* Settings */
1985 SNOR_PROTO_1_1_2,
1988 /* Fast Read 1-2-2 */
1990 SNOR_HWCAPS_READ_1_2_2,
1991 BFPT_DWORD(1), BIT(20), /* Supported bit */
1992 BFPT_DWORD(4), 16, /* Settings */
1993 SNOR_PROTO_1_2_2,
1996 /* Fast Read 2-2-2 */
1998 SNOR_HWCAPS_READ_2_2_2,
1999 BFPT_DWORD(5), BIT(0), /* Supported bit */
2000 BFPT_DWORD(6), 16, /* Settings */
2001 SNOR_PROTO_2_2_2,
2004 /* Fast Read 1-1-4 */
2006 SNOR_HWCAPS_READ_1_1_4,
2007 BFPT_DWORD(1), BIT(22), /* Supported bit */
2008 BFPT_DWORD(3), 16, /* Settings */
2009 SNOR_PROTO_1_1_4,
2012 /* Fast Read 1-4-4 */
2014 SNOR_HWCAPS_READ_1_4_4,
2015 BFPT_DWORD(1), BIT(21), /* Supported bit */
2016 BFPT_DWORD(3), 0, /* Settings */
2017 SNOR_PROTO_1_4_4,
2020 /* Fast Read 4-4-4 */
2022 SNOR_HWCAPS_READ_4_4_4,
2023 BFPT_DWORD(5), BIT(4), /* Supported bit */
2024 BFPT_DWORD(7), 16, /* Settings */
2025 SNOR_PROTO_4_4_4,
2029 struct sfdp_bfpt_erase {
2031 * The half-word at offset <shift> in DWORD <dwoard> encodes the
2032 * op code and erase sector size to be used by Sector Erase commands.
2034 u32 dword;
2035 u32 shift;
2038 static const struct sfdp_bfpt_erase sfdp_bfpt_erases[] = {
2039 /* Erase Type 1 in DWORD8 bits[15:0] */
2040 {BFPT_DWORD(8), 0},
2042 /* Erase Type 2 in DWORD8 bits[31:16] */
2043 {BFPT_DWORD(8), 16},
2045 /* Erase Type 3 in DWORD9 bits[15:0] */
2046 {BFPT_DWORD(9), 0},
2048 /* Erase Type 4 in DWORD9 bits[31:16] */
2049 {BFPT_DWORD(9), 16},
2052 static int spi_nor_hwcaps_read2cmd(u32 hwcaps);
2055 * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
2056 * @nor: pointer to a 'struct spi_nor'
2057 * @bfpt_header: pointer to the 'struct sfdp_parameter_header' describing
2058 * the Basic Flash Parameter Table length and version
2059 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
2060 * filled
2062 * The Basic Flash Parameter Table is the main and only mandatory table as
2063 * defined by the SFDP (JESD216) specification.
2064 * It provides us with the total size (memory density) of the data array and
2065 * the number of address bytes for Fast Read, Page Program and Sector Erase
2066 * commands.
2067 * For Fast READ commands, it also gives the number of mode clock cycles and
2068 * wait states (regrouped in the number of dummy clock cycles) for each
2069 * supported instruction op code.
2070 * For Page Program, the page size is now available since JESD216 rev A, however
2071 * the supported instruction op codes are still not provided.
2072 * For Sector Erase commands, this table stores the supported instruction op
2073 * codes and the associated sector sizes.
2074 * Finally, the Quad Enable Requirements (QER) are also available since JESD216
2075 * rev A. The QER bits encode the manufacturer dependent procedure to be
2076 * executed to set the Quad Enable (QE) bit in some internal register of the
2077 * Quad SPI memory. Indeed the QE bit, when it exists, must be set before
2078 * sending any Quad SPI command to the memory. Actually, setting the QE bit
2079 * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2
2080 * and IO3 hence enabling 4 (Quad) I/O lines.
2082 * Return: 0 on success, -errno otherwise.
2084 static int spi_nor_parse_bfpt(struct spi_nor *nor,
2085 const struct sfdp_parameter_header *bfpt_header,
2086 struct spi_nor_flash_parameter *params)
2088 struct mtd_info *mtd = &nor->mtd;
2089 struct sfdp_bfpt bfpt;
2090 size_t len;
2091 int i, cmd, err;
2092 u32 addr;
2093 u16 half;
2095 /* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */
2096 if (bfpt_header->length < BFPT_DWORD_MAX_JESD216)
2097 return -EINVAL;
2099 /* Read the Basic Flash Parameter Table. */
2100 len = min_t(size_t, sizeof(bfpt),
2101 bfpt_header->length * sizeof(u32));
2102 addr = SFDP_PARAM_HEADER_PTP(bfpt_header);
2103 memset(&bfpt, 0, sizeof(bfpt));
2104 err = spi_nor_read_sfdp(nor, addr, len, &bfpt);
2105 if (err < 0)
2106 return err;
2108 /* Fix endianness of the BFPT DWORDs. */
2109 for (i = 0; i < BFPT_DWORD_MAX; i++)
2110 bfpt.dwords[i] = le32_to_cpu(bfpt.dwords[i]);
2112 /* Number of address bytes. */
2113 switch (bfpt.dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) {
2114 case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY:
2115 nor->addr_width = 3;
2116 break;
2118 case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY:
2119 nor->addr_width = 4;
2120 break;
2122 default:
2123 break;
2126 /* Flash Memory Density (in bits). */
2127 params->size = bfpt.dwords[BFPT_DWORD(2)];
2128 if (params->size & BIT(31)) {
2129 params->size &= ~BIT(31);
2130 params->size = 1ULL << params->size;
2131 } else {
2132 params->size++;
2134 params->size >>= 3; /* Convert to bytes. */
2136 /* Fast Read settings. */
2137 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_reads); i++) {
2138 const struct sfdp_bfpt_read *rd = &sfdp_bfpt_reads[i];
2139 struct spi_nor_read_command *read;
2141 if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) {
2142 params->hwcaps.mask &= ~rd->hwcaps;
2143 continue;
2146 params->hwcaps.mask |= rd->hwcaps;
2147 cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps);
2148 read = &params->reads[cmd];
2149 half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift;
2150 spi_nor_set_read_settings_from_bfpt(read, half, rd->proto);
2153 /* Sector Erase settings. */
2154 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_erases); i++) {
2155 const struct sfdp_bfpt_erase *er = &sfdp_bfpt_erases[i];
2156 u32 erasesize;
2157 u8 opcode;
2159 half = bfpt.dwords[er->dword] >> er->shift;
2160 erasesize = half & 0xff;
2162 /* erasesize == 0 means this Erase Type is not supported. */
2163 if (!erasesize)
2164 continue;
2166 erasesize = 1U << erasesize;
2167 opcode = (half >> 8) & 0xff;
2168 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
2169 if (erasesize == SZ_4K) {
2170 nor->erase_opcode = opcode;
2171 mtd->erasesize = erasesize;
2172 break;
2174 #endif
2175 if (!mtd->erasesize || mtd->erasesize < erasesize) {
2176 nor->erase_opcode = opcode;
2177 mtd->erasesize = erasesize;
2181 /* Stop here if not JESD216 rev A or later. */
2182 if (bfpt_header->length < BFPT_DWORD_MAX)
2183 return 0;
2185 /* Page size: this field specifies 'N' so the page size = 2^N bytes. */
2186 params->page_size = bfpt.dwords[BFPT_DWORD(11)];
2187 params->page_size &= BFPT_DWORD11_PAGE_SIZE_MASK;
2188 params->page_size >>= BFPT_DWORD11_PAGE_SIZE_SHIFT;
2189 params->page_size = 1U << params->page_size;
2191 /* Quad Enable Requirements. */
2192 switch (bfpt.dwords[BFPT_DWORD(15)] & BFPT_DWORD15_QER_MASK) {
2193 case BFPT_DWORD15_QER_NONE:
2194 params->quad_enable = NULL;
2195 break;
2197 case BFPT_DWORD15_QER_SR2_BIT1_BUGGY:
2198 case BFPT_DWORD15_QER_SR2_BIT1_NO_RD:
2199 params->quad_enable = spansion_no_read_cr_quad_enable;
2200 break;
2202 case BFPT_DWORD15_QER_SR1_BIT6:
2203 params->quad_enable = macronix_quad_enable;
2204 break;
2206 case BFPT_DWORD15_QER_SR2_BIT7:
2207 params->quad_enable = sr2_bit7_quad_enable;
2208 break;
2210 case BFPT_DWORD15_QER_SR2_BIT1:
2211 params->quad_enable = spansion_read_cr_quad_enable;
2212 break;
2214 default:
2215 return -EINVAL;
2218 return 0;
2222 * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
2223 * @nor: pointer to a 'struct spi_nor'
2224 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
2225 * filled
2227 * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
2228 * specification. This is a standard which tends to supported by almost all
2229 * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
2230 * runtime the main parameters needed to perform basic SPI flash operations such
2231 * as Fast Read, Page Program or Sector Erase commands.
2233 * Return: 0 on success, -errno otherwise.
2235 static int spi_nor_parse_sfdp(struct spi_nor *nor,
2236 struct spi_nor_flash_parameter *params)
2238 const struct sfdp_parameter_header *param_header, *bfpt_header;
2239 struct sfdp_parameter_header *param_headers = NULL;
2240 struct sfdp_header header;
2241 struct device *dev = nor->dev;
2242 size_t psize;
2243 int i, err;
2245 /* Get the SFDP header. */
2246 err = spi_nor_read_sfdp(nor, 0, sizeof(header), &header);
2247 if (err < 0)
2248 return err;
2250 /* Check the SFDP header version. */
2251 if (le32_to_cpu(header.signature) != SFDP_SIGNATURE ||
2252 header.major != SFDP_JESD216_MAJOR ||
2253 header.minor < SFDP_JESD216_MINOR)
2254 return -EINVAL;
2257 * Verify that the first and only mandatory parameter header is a
2258 * Basic Flash Parameter Table header as specified in JESD216.
2260 bfpt_header = &header.bfpt_header;
2261 if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID ||
2262 bfpt_header->major != SFDP_JESD216_MAJOR)
2263 return -EINVAL;
2266 * Allocate memory then read all parameter headers with a single
2267 * Read SFDP command. These parameter headers will actually be parsed
2268 * twice: a first time to get the latest revision of the basic flash
2269 * parameter table, then a second time to handle the supported optional
2270 * tables.
2271 * Hence we read the parameter headers once for all to reduce the
2272 * processing time. Also we use kmalloc() instead of devm_kmalloc()
2273 * because we don't need to keep these parameter headers: the allocated
2274 * memory is always released with kfree() before exiting this function.
2276 if (header.nph) {
2277 psize = header.nph * sizeof(*param_headers);
2279 param_headers = kmalloc(psize, GFP_KERNEL);
2280 if (!param_headers)
2281 return -ENOMEM;
2283 err = spi_nor_read_sfdp(nor, sizeof(header),
2284 psize, param_headers);
2285 if (err < 0) {
2286 dev_err(dev, "failed to read SFDP parameter headers\n");
2287 goto exit;
2292 * Check other parameter headers to get the latest revision of
2293 * the basic flash parameter table.
2295 for (i = 0; i < header.nph; i++) {
2296 param_header = &param_headers[i];
2298 if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID &&
2299 param_header->major == SFDP_JESD216_MAJOR &&
2300 (param_header->minor > bfpt_header->minor ||
2301 (param_header->minor == bfpt_header->minor &&
2302 param_header->length > bfpt_header->length)))
2303 bfpt_header = param_header;
2306 err = spi_nor_parse_bfpt(nor, bfpt_header, params);
2307 if (err)
2308 goto exit;
2310 /* Parse other parameter headers. */
2311 for (i = 0; i < header.nph; i++) {
2312 param_header = &param_headers[i];
2314 switch (SFDP_PARAM_HEADER_ID(param_header)) {
2315 case SFDP_SECTOR_MAP_ID:
2316 dev_info(dev, "non-uniform erase sector maps are not supported yet.\n");
2317 break;
2319 default:
2320 break;
2323 if (err)
2324 goto exit;
2327 exit:
2328 kfree(param_headers);
2329 return err;
2332 static int spi_nor_init_params(struct spi_nor *nor,
2333 const struct flash_info *info,
2334 struct spi_nor_flash_parameter *params)
2336 /* Set legacy flash parameters as default. */
2337 memset(params, 0, sizeof(*params));
2339 /* Set SPI NOR sizes. */
2340 params->size = info->sector_size * info->n_sectors;
2341 params->page_size = info->page_size;
2343 /* (Fast) Read settings. */
2344 params->hwcaps.mask |= SNOR_HWCAPS_READ;
2345 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ],
2346 0, 0, SPINOR_OP_READ,
2347 SNOR_PROTO_1_1_1);
2349 if (!(info->flags & SPI_NOR_NO_FR)) {
2350 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
2351 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST],
2352 0, 8, SPINOR_OP_READ_FAST,
2353 SNOR_PROTO_1_1_1);
2356 if (info->flags & SPI_NOR_DUAL_READ) {
2357 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
2358 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_2],
2359 0, 8, SPINOR_OP_READ_1_1_2,
2360 SNOR_PROTO_1_1_2);
2363 if (info->flags & SPI_NOR_QUAD_READ) {
2364 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
2365 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_4],
2366 0, 8, SPINOR_OP_READ_1_1_4,
2367 SNOR_PROTO_1_1_4);
2370 /* Page Program settings. */
2371 params->hwcaps.mask |= SNOR_HWCAPS_PP;
2372 spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP],
2373 SPINOR_OP_PP, SNOR_PROTO_1_1_1);
2375 /* Select the procedure to set the Quad Enable bit. */
2376 if (params->hwcaps.mask & (SNOR_HWCAPS_READ_QUAD |
2377 SNOR_HWCAPS_PP_QUAD)) {
2378 switch (JEDEC_MFR(info)) {
2379 case SNOR_MFR_MACRONIX:
2380 params->quad_enable = macronix_quad_enable;
2381 break;
2383 case SNOR_MFR_MICRON:
2384 break;
2386 default:
2387 /* Kept only for backward compatibility purpose. */
2388 params->quad_enable = spansion_quad_enable;
2389 break;
2393 /* Override the parameters with data read from SFDP tables. */
2394 nor->addr_width = 0;
2395 nor->mtd.erasesize = 0;
2396 if ((info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)) &&
2397 !(info->flags & SPI_NOR_SKIP_SFDP)) {
2398 struct spi_nor_flash_parameter sfdp_params;
2400 memcpy(&sfdp_params, params, sizeof(sfdp_params));
2401 if (spi_nor_parse_sfdp(nor, &sfdp_params)) {
2402 nor->addr_width = 0;
2403 nor->mtd.erasesize = 0;
2404 } else {
2405 memcpy(params, &sfdp_params, sizeof(*params));
2409 return 0;
2412 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
2414 size_t i;
2416 for (i = 0; i < size; i++)
2417 if (table[i][0] == (int)hwcaps)
2418 return table[i][1];
2420 return -EINVAL;
2423 static int spi_nor_hwcaps_read2cmd(u32 hwcaps)
2425 static const int hwcaps_read2cmd[][2] = {
2426 { SNOR_HWCAPS_READ, SNOR_CMD_READ },
2427 { SNOR_HWCAPS_READ_FAST, SNOR_CMD_READ_FAST },
2428 { SNOR_HWCAPS_READ_1_1_1_DTR, SNOR_CMD_READ_1_1_1_DTR },
2429 { SNOR_HWCAPS_READ_1_1_2, SNOR_CMD_READ_1_1_2 },
2430 { SNOR_HWCAPS_READ_1_2_2, SNOR_CMD_READ_1_2_2 },
2431 { SNOR_HWCAPS_READ_2_2_2, SNOR_CMD_READ_2_2_2 },
2432 { SNOR_HWCAPS_READ_1_2_2_DTR, SNOR_CMD_READ_1_2_2_DTR },
2433 { SNOR_HWCAPS_READ_1_1_4, SNOR_CMD_READ_1_1_4 },
2434 { SNOR_HWCAPS_READ_1_4_4, SNOR_CMD_READ_1_4_4 },
2435 { SNOR_HWCAPS_READ_4_4_4, SNOR_CMD_READ_4_4_4 },
2436 { SNOR_HWCAPS_READ_1_4_4_DTR, SNOR_CMD_READ_1_4_4_DTR },
2437 { SNOR_HWCAPS_READ_1_1_8, SNOR_CMD_READ_1_1_8 },
2438 { SNOR_HWCAPS_READ_1_8_8, SNOR_CMD_READ_1_8_8 },
2439 { SNOR_HWCAPS_READ_8_8_8, SNOR_CMD_READ_8_8_8 },
2440 { SNOR_HWCAPS_READ_1_8_8_DTR, SNOR_CMD_READ_1_8_8_DTR },
2443 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
2444 ARRAY_SIZE(hwcaps_read2cmd));
2447 static int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
2449 static const int hwcaps_pp2cmd[][2] = {
2450 { SNOR_HWCAPS_PP, SNOR_CMD_PP },
2451 { SNOR_HWCAPS_PP_1_1_4, SNOR_CMD_PP_1_1_4 },
2452 { SNOR_HWCAPS_PP_1_4_4, SNOR_CMD_PP_1_4_4 },
2453 { SNOR_HWCAPS_PP_4_4_4, SNOR_CMD_PP_4_4_4 },
2454 { SNOR_HWCAPS_PP_1_1_8, SNOR_CMD_PP_1_1_8 },
2455 { SNOR_HWCAPS_PP_1_8_8, SNOR_CMD_PP_1_8_8 },
2456 { SNOR_HWCAPS_PP_8_8_8, SNOR_CMD_PP_8_8_8 },
2459 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
2460 ARRAY_SIZE(hwcaps_pp2cmd));
2463 static int spi_nor_select_read(struct spi_nor *nor,
2464 const struct spi_nor_flash_parameter *params,
2465 u32 shared_hwcaps)
2467 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
2468 const struct spi_nor_read_command *read;
2470 if (best_match < 0)
2471 return -EINVAL;
2473 cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
2474 if (cmd < 0)
2475 return -EINVAL;
2477 read = &params->reads[cmd];
2478 nor->read_opcode = read->opcode;
2479 nor->read_proto = read->proto;
2482 * In the spi-nor framework, we don't need to make the difference
2483 * between mode clock cycles and wait state clock cycles.
2484 * Indeed, the value of the mode clock cycles is used by a QSPI
2485 * flash memory to know whether it should enter or leave its 0-4-4
2486 * (Continuous Read / XIP) mode.
2487 * eXecution In Place is out of the scope of the mtd sub-system.
2488 * Hence we choose to merge both mode and wait state clock cycles
2489 * into the so called dummy clock cycles.
2491 nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
2492 return 0;
2495 static int spi_nor_select_pp(struct spi_nor *nor,
2496 const struct spi_nor_flash_parameter *params,
2497 u32 shared_hwcaps)
2499 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
2500 const struct spi_nor_pp_command *pp;
2502 if (best_match < 0)
2503 return -EINVAL;
2505 cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
2506 if (cmd < 0)
2507 return -EINVAL;
2509 pp = &params->page_programs[cmd];
2510 nor->program_opcode = pp->opcode;
2511 nor->write_proto = pp->proto;
2512 return 0;
2515 static int spi_nor_select_erase(struct spi_nor *nor,
2516 const struct flash_info *info)
2518 struct mtd_info *mtd = &nor->mtd;
2520 /* Do nothing if already configured from SFDP. */
2521 if (mtd->erasesize)
2522 return 0;
2524 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
2525 /* prefer "small sector" erase if possible */
2526 if (info->flags & SECT_4K) {
2527 nor->erase_opcode = SPINOR_OP_BE_4K;
2528 mtd->erasesize = 4096;
2529 } else if (info->flags & SECT_4K_PMC) {
2530 nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
2531 mtd->erasesize = 4096;
2532 } else
2533 #endif
2535 nor->erase_opcode = SPINOR_OP_SE;
2536 mtd->erasesize = info->sector_size;
2538 return 0;
2541 static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
2542 const struct spi_nor_flash_parameter *params,
2543 const struct spi_nor_hwcaps *hwcaps)
2545 u32 ignored_mask, shared_mask;
2546 bool enable_quad_io;
2547 int err;
2550 * Keep only the hardware capabilities supported by both the SPI
2551 * controller and the SPI flash memory.
2553 shared_mask = hwcaps->mask & params->hwcaps.mask;
2555 /* SPI n-n-n protocols are not supported yet. */
2556 ignored_mask = (SNOR_HWCAPS_READ_2_2_2 |
2557 SNOR_HWCAPS_READ_4_4_4 |
2558 SNOR_HWCAPS_READ_8_8_8 |
2559 SNOR_HWCAPS_PP_4_4_4 |
2560 SNOR_HWCAPS_PP_8_8_8);
2561 if (shared_mask & ignored_mask) {
2562 dev_dbg(nor->dev,
2563 "SPI n-n-n protocols are not supported yet.\n");
2564 shared_mask &= ~ignored_mask;
2567 /* Select the (Fast) Read command. */
2568 err = spi_nor_select_read(nor, params, shared_mask);
2569 if (err) {
2570 dev_err(nor->dev,
2571 "can't select read settings supported by both the SPI controller and memory.\n");
2572 return err;
2575 /* Select the Page Program command. */
2576 err = spi_nor_select_pp(nor, params, shared_mask);
2577 if (err) {
2578 dev_err(nor->dev,
2579 "can't select write settings supported by both the SPI controller and memory.\n");
2580 return err;
2583 /* Select the Sector Erase command. */
2584 err = spi_nor_select_erase(nor, info);
2585 if (err) {
2586 dev_err(nor->dev,
2587 "can't select erase settings supported by both the SPI controller and memory.\n");
2588 return err;
2591 /* Enable Quad I/O if needed. */
2592 enable_quad_io = (spi_nor_get_protocol_width(nor->read_proto) == 4 ||
2593 spi_nor_get_protocol_width(nor->write_proto) == 4);
2594 if (enable_quad_io && params->quad_enable) {
2595 err = params->quad_enable(nor);
2596 if (err) {
2597 dev_err(nor->dev, "quad mode not supported\n");
2598 return err;
2602 return 0;
2605 int spi_nor_scan(struct spi_nor *nor, const char *name,
2606 const struct spi_nor_hwcaps *hwcaps)
2608 struct spi_nor_flash_parameter params;
2609 const struct flash_info *info = NULL;
2610 struct device *dev = nor->dev;
2611 struct mtd_info *mtd = &nor->mtd;
2612 struct device_node *np = spi_nor_get_flash_node(nor);
2613 int ret;
2614 int i;
2616 ret = spi_nor_check(nor);
2617 if (ret)
2618 return ret;
2620 /* Reset SPI protocol for all commands. */
2621 nor->reg_proto = SNOR_PROTO_1_1_1;
2622 nor->read_proto = SNOR_PROTO_1_1_1;
2623 nor->write_proto = SNOR_PROTO_1_1_1;
2625 if (name)
2626 info = spi_nor_match_id(name);
2627 /* Try to auto-detect if chip name wasn't specified or not found */
2628 if (!info)
2629 info = spi_nor_read_id(nor);
2630 if (IS_ERR_OR_NULL(info))
2631 return -ENOENT;
2634 * If caller has specified name of flash model that can normally be
2635 * detected using JEDEC, let's verify it.
2637 if (name && info->id_len) {
2638 const struct flash_info *jinfo;
2640 jinfo = spi_nor_read_id(nor);
2641 if (IS_ERR(jinfo)) {
2642 return PTR_ERR(jinfo);
2643 } else if (jinfo != info) {
2645 * JEDEC knows better, so overwrite platform ID. We
2646 * can't trust partitions any longer, but we'll let
2647 * mtd apply them anyway, since some partitions may be
2648 * marked read-only, and we don't want to lose that
2649 * information, even if it's not 100% accurate.
2651 dev_warn(dev, "found %s, expected %s\n",
2652 jinfo->name, info->name);
2653 info = jinfo;
2657 mutex_init(&nor->lock);
2660 * Make sure the XSR_RDY flag is set before calling
2661 * spi_nor_wait_till_ready(). Xilinx S3AN share MFR
2662 * with Atmel spi-nor
2664 if (info->flags & SPI_S3AN)
2665 nor->flags |= SNOR_F_READY_XSR_RDY;
2667 /* Parse the Serial Flash Discoverable Parameters table. */
2668 ret = spi_nor_init_params(nor, info, &params);
2669 if (ret)
2670 return ret;
2673 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
2674 * with the software protection bits set
2677 if (JEDEC_MFR(info) == SNOR_MFR_ATMEL ||
2678 JEDEC_MFR(info) == SNOR_MFR_INTEL ||
2679 JEDEC_MFR(info) == SNOR_MFR_SST ||
2680 info->flags & SPI_NOR_HAS_LOCK) {
2681 write_enable(nor);
2682 write_sr(nor, 0);
2683 spi_nor_wait_till_ready(nor);
2686 if (!mtd->name)
2687 mtd->name = dev_name(dev);
2688 mtd->priv = nor;
2689 mtd->type = MTD_NORFLASH;
2690 mtd->writesize = 1;
2691 mtd->flags = MTD_CAP_NORFLASH;
2692 mtd->size = params.size;
2693 mtd->_erase = spi_nor_erase;
2694 mtd->_read = spi_nor_read;
2696 /* NOR protection support for STmicro/Micron chips and similar */
2697 if (JEDEC_MFR(info) == SNOR_MFR_MICRON ||
2698 info->flags & SPI_NOR_HAS_LOCK) {
2699 nor->flash_lock = stm_lock;
2700 nor->flash_unlock = stm_unlock;
2701 nor->flash_is_locked = stm_is_locked;
2704 if (nor->flash_lock && nor->flash_unlock && nor->flash_is_locked) {
2705 mtd->_lock = spi_nor_lock;
2706 mtd->_unlock = spi_nor_unlock;
2707 mtd->_is_locked = spi_nor_is_locked;
2710 /* sst nor chips use AAI word program */
2711 if (info->flags & SST_WRITE)
2712 mtd->_write = sst_write;
2713 else
2714 mtd->_write = spi_nor_write;
2716 if (info->flags & USE_FSR)
2717 nor->flags |= SNOR_F_USE_FSR;
2718 if (info->flags & SPI_NOR_HAS_TB)
2719 nor->flags |= SNOR_F_HAS_SR_TB;
2720 if (info->flags & NO_CHIP_ERASE)
2721 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
2722 if (info->flags & USE_CLSR)
2723 nor->flags |= SNOR_F_USE_CLSR;
2725 if (info->flags & SPI_NOR_NO_ERASE)
2726 mtd->flags |= MTD_NO_ERASE;
2728 mtd->dev.parent = dev;
2729 nor->page_size = params.page_size;
2730 mtd->writebufsize = nor->page_size;
2732 if (np) {
2733 /* If we were instantiated by DT, use it */
2734 if (of_property_read_bool(np, "m25p,fast-read"))
2735 params.hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
2736 else
2737 params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
2738 } else {
2739 /* If we weren't instantiated by DT, default to fast-read */
2740 params.hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
2743 /* Some devices cannot do fast-read, no matter what DT tells us */
2744 if (info->flags & SPI_NOR_NO_FR)
2745 params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
2748 * Configure the SPI memory:
2749 * - select op codes for (Fast) Read, Page Program and Sector Erase.
2750 * - set the number of dummy cycles (mode cycles + wait states).
2751 * - set the SPI protocols for register and memory accesses.
2752 * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
2754 ret = spi_nor_setup(nor, info, &params, hwcaps);
2755 if (ret)
2756 return ret;
2758 if (nor->addr_width) {
2759 /* already configured from SFDP */
2760 } else if (info->addr_width) {
2761 nor->addr_width = info->addr_width;
2762 } else if (mtd->size > 0x1000000) {
2763 /* enable 4-byte addressing if the device exceeds 16MiB */
2764 nor->addr_width = 4;
2765 if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
2766 info->flags & SPI_NOR_4B_OPCODES)
2767 spi_nor_set_4byte_opcodes(nor, info);
2768 else
2769 set_4byte(nor, info, 1);
2770 } else {
2771 nor->addr_width = 3;
2774 if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
2775 dev_err(dev, "address width is too large: %u\n",
2776 nor->addr_width);
2777 return -EINVAL;
2780 if (info->flags & SPI_S3AN) {
2781 ret = s3an_nor_scan(info, nor);
2782 if (ret)
2783 return ret;
2786 dev_info(dev, "%s (%lld Kbytes)\n", info->name,
2787 (long long)mtd->size >> 10);
2789 dev_dbg(dev,
2790 "mtd .name = %s, .size = 0x%llx (%lldMiB), "
2791 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
2792 mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
2793 mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
2795 if (mtd->numeraseregions)
2796 for (i = 0; i < mtd->numeraseregions; i++)
2797 dev_dbg(dev,
2798 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
2799 ".erasesize = 0x%.8x (%uKiB), "
2800 ".numblocks = %d }\n",
2801 i, (long long)mtd->eraseregions[i].offset,
2802 mtd->eraseregions[i].erasesize,
2803 mtd->eraseregions[i].erasesize / 1024,
2804 mtd->eraseregions[i].numblocks);
2805 return 0;
2807 EXPORT_SYMBOL_GPL(spi_nor_scan);
2809 static const struct flash_info *spi_nor_match_id(const char *name)
2811 const struct flash_info *id = spi_nor_ids;
2813 while (id->name) {
2814 if (!strcmp(name, id->name))
2815 return id;
2816 id++;
2818 return NULL;
2821 MODULE_LICENSE("GPL");
2822 MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
2823 MODULE_AUTHOR("Mike Lavender");
2824 MODULE_DESCRIPTION("framework for SPI NOR");