1 // SPDX-License-Identifier: GPL-2.0
3 * Based on m25p80.c, by Mike Lavender (mike@steroidmicros.com), with
4 * influence from lart.c (Abraham Van Der Merwe) and mtd_dataflash.c
6 * Copyright (C) 2005, Intec Automation Inc.
7 * Copyright (C) 2014, Freescale Semiconductor, Inc.
10 #include <linux/err.h>
11 #include <linux/errno.h>
12 #include <linux/module.h>
13 #include <linux/device.h>
14 #include <linux/mutex.h>
15 #include <linux/math64.h>
16 #include <linux/sizes.h>
17 #include <linux/slab.h>
18 #include <linux/sort.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/of_platform.h>
22 #include <linux/sched/task_stack.h>
23 #include <linux/spi/flash.h>
24 #include <linux/mtd/spi-nor.h>
26 /* Define max times to check status register before we give up. */
29 * For everything but full-chip erase; probably could be much smaller, but kept
30 * around for safety for now
32 #define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ)
35 * For full-chip erase, calibrated to a 2MB flash (M25P16); should be scaled up
38 #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
40 #define SPI_NOR_MAX_ID_LEN 6
41 #define SPI_NOR_MAX_ADDR_WIDTH 4
43 struct sfdp_parameter_header {
47 u8 length; /* in double words */
48 u8 parameter_table_pointer[3]; /* byte address */
52 #define SFDP_PARAM_HEADER_ID(p) (((p)->id_msb << 8) | (p)->id_lsb)
53 #define SFDP_PARAM_HEADER_PTP(p) \
54 (((p)->parameter_table_pointer[2] << 16) | \
55 ((p)->parameter_table_pointer[1] << 8) | \
56 ((p)->parameter_table_pointer[0] << 0))
58 #define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
59 #define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
60 #define SFDP_4BAIT_ID 0xff84 /* 4-byte Address Instruction Table */
62 #define SFDP_SIGNATURE 0x50444653U
63 #define SFDP_JESD216_MAJOR 1
64 #define SFDP_JESD216_MINOR 0
65 #define SFDP_JESD216A_MINOR 5
66 #define SFDP_JESD216B_MINOR 6
69 u32 signature; /* Ox50444653U <=> "SFDP" */
72 u8 nph; /* 0-base number of parameter headers */
75 /* Basic Flash Parameter Table. */
76 struct sfdp_parameter_header bfpt_header;
79 /* Basic Flash Parameter Table */
82 * JESD216 rev B defines a Basic Flash Parameter Table of 16 DWORDs.
83 * They are indexed from 1 but C arrays are indexed from 0.
85 #define BFPT_DWORD(i) ((i) - 1)
86 #define BFPT_DWORD_MAX 16
88 /* The first version of JESB216 defined only 9 DWORDs. */
89 #define BFPT_DWORD_MAX_JESD216 9
92 #define BFPT_DWORD1_FAST_READ_1_1_2 BIT(16)
93 #define BFPT_DWORD1_ADDRESS_BYTES_MASK GENMASK(18, 17)
94 #define BFPT_DWORD1_ADDRESS_BYTES_3_ONLY (0x0UL << 17)
95 #define BFPT_DWORD1_ADDRESS_BYTES_3_OR_4 (0x1UL << 17)
96 #define BFPT_DWORD1_ADDRESS_BYTES_4_ONLY (0x2UL << 17)
97 #define BFPT_DWORD1_DTR BIT(19)
98 #define BFPT_DWORD1_FAST_READ_1_2_2 BIT(20)
99 #define BFPT_DWORD1_FAST_READ_1_4_4 BIT(21)
100 #define BFPT_DWORD1_FAST_READ_1_1_4 BIT(22)
103 #define BFPT_DWORD5_FAST_READ_2_2_2 BIT(0)
104 #define BFPT_DWORD5_FAST_READ_4_4_4 BIT(4)
107 #define BFPT_DWORD11_PAGE_SIZE_SHIFT 4
108 #define BFPT_DWORD11_PAGE_SIZE_MASK GENMASK(7, 4)
113 * (from JESD216 rev B)
114 * Quad Enable Requirements (QER):
115 * - 000b: Device does not have a QE bit. Device detects 1-1-4 and 1-4-4
116 * reads based on instruction. DQ3/HOLD# functions are hold during
118 * - 001b: QE is bit 1 of status register 2. It is set via Write Status with
119 * two data bytes where bit 1 of the second byte is one.
121 * Writing only one byte to the status register has the side-effect of
122 * clearing status register 2, including the QE bit. The 100b code is
123 * used if writing one byte to the status register does not modify
125 * - 010b: QE is bit 6 of status register 1. It is set via Write Status with
126 * one data byte where bit 6 is one.
128 * - 011b: QE is bit 7 of status register 2. It is set via Write status
129 * register 2 instruction 3Eh with one data byte where bit 7 is one.
131 * The status register 2 is read using instruction 3Fh.
132 * - 100b: QE is bit 1 of status register 2. It is set via Write Status with
133 * two data bytes where bit 1 of the second byte is one.
135 * In contrast to the 001b code, writing one byte to the status
136 * register does not modify status register 2.
137 * - 101b: QE is bit 1 of status register 2. Status register 1 is read using
138 * Read Status instruction 05h. Status register2 is read using
139 * instruction 35h. QE is set via Write Status instruction 01h with
140 * two data bytes where bit 1 of the second byte is one.
143 #define BFPT_DWORD15_QER_MASK GENMASK(22, 20)
144 #define BFPT_DWORD15_QER_NONE (0x0UL << 20) /* Micron */
145 #define BFPT_DWORD15_QER_SR2_BIT1_BUGGY (0x1UL << 20)
146 #define BFPT_DWORD15_QER_SR1_BIT6 (0x2UL << 20) /* Macronix */
147 #define BFPT_DWORD15_QER_SR2_BIT7 (0x3UL << 20)
148 #define BFPT_DWORD15_QER_SR2_BIT1_NO_RD (0x4UL << 20)
149 #define BFPT_DWORD15_QER_SR2_BIT1 (0x5UL << 20) /* Spansion */
152 u32 dwords[BFPT_DWORD_MAX];
156 * struct spi_nor_fixups - SPI NOR fixup hooks
157 * @default_init: called after default flash parameters init. Used to tweak
158 * flash parameters when information provided by the flash_info
159 * table is incomplete or wrong.
160 * @post_bfpt: called after the BFPT table has been parsed
161 * @post_sfdp: called after SFDP has been parsed (is also called for SPI NORs
162 * that do not support RDSFDP). Typically used to tweak various
163 * parameters that could not be extracted by other means (i.e.
164 * when information provided by the SFDP/flash_info tables are
165 * incomplete or wrong).
167 * Those hooks can be used to tweak the SPI NOR configuration when the SFDP
168 * table is broken or not available.
170 struct spi_nor_fixups {
171 void (*default_init)(struct spi_nor *nor);
172 int (*post_bfpt)(struct spi_nor *nor,
173 const struct sfdp_parameter_header *bfpt_header,
174 const struct sfdp_bfpt *bfpt,
175 struct spi_nor_flash_parameter *params);
176 void (*post_sfdp)(struct spi_nor *nor);
183 * This array stores the ID bytes.
184 * The first three bytes are the JEDIC ID.
185 * JEDEC ID zero means "no ID" (mostly older chips).
187 u8 id[SPI_NOR_MAX_ID_LEN];
190 /* The size listed here is what works with SPINOR_OP_SE, which isn't
191 * necessarily called a "sector" by the vendor.
193 unsigned sector_size;
200 #define SECT_4K BIT(0) /* SPINOR_OP_BE_4K works uniformly */
201 #define SPI_NOR_NO_ERASE BIT(1) /* No erase command needed */
202 #define SST_WRITE BIT(2) /* use SST byte programming */
203 #define SPI_NOR_NO_FR BIT(3) /* Can't do fastread */
204 #define SECT_4K_PMC BIT(4) /* SPINOR_OP_BE_4K_PMC works uniformly */
205 #define SPI_NOR_DUAL_READ BIT(5) /* Flash supports Dual Read */
206 #define SPI_NOR_QUAD_READ BIT(6) /* Flash supports Quad Read */
207 #define USE_FSR BIT(7) /* use flag status register */
208 #define SPI_NOR_HAS_LOCK BIT(8) /* Flash supports lock/unlock via SR */
209 #define SPI_NOR_HAS_TB BIT(9) /*
210 * Flash SR has Top/Bottom (TB) protect
211 * bit. Must be used with
214 #define SPI_NOR_XSR_RDY BIT(10) /*
215 * S3AN flashes have specific opcode to
216 * read the status register.
217 * Flags SPI_NOR_XSR_RDY and SPI_S3AN
218 * use the same bit as one implies the
219 * other, but we will get rid of
222 #define SPI_S3AN BIT(10) /*
223 * Xilinx Spartan 3AN In-System Flash
224 * (MFR cannot be used for probing
225 * because it has the same value as
228 #define SPI_NOR_4B_OPCODES BIT(11) /*
229 * Use dedicated 4byte address op codes
230 * to support memory size above 128Mib.
232 #define NO_CHIP_ERASE BIT(12) /* Chip does not support chip erase */
233 #define SPI_NOR_SKIP_SFDP BIT(13) /* Skip parsing of SFDP tables */
234 #define USE_CLSR BIT(14) /* use CLSR command */
235 #define SPI_NOR_OCTAL_READ BIT(15) /* Flash supports Octal Read */
237 /* Part specific fixup hooks. */
238 const struct spi_nor_fixups *fixups;
241 #define JEDEC_MFR(info) ((info)->id[0])
244 * spi_nor_spimem_xfer_data() - helper function to read/write data to
245 * flash's memory region
246 * @nor: pointer to 'struct spi_nor'
247 * @op: pointer to 'struct spi_mem_op' template for transfer
249 * Return: number of bytes transferred on success, -errno otherwise
251 static ssize_t spi_nor_spimem_xfer_data(struct spi_nor *nor,
252 struct spi_mem_op *op)
254 bool usebouncebuf = false;
259 if (op->data.dir == SPI_MEM_DATA_IN)
260 buf = op->data.buf.in;
262 buf = op->data.buf.out;
264 if (object_is_on_stack(buf) || !virt_addr_valid(buf))
268 if (op->data.nbytes > nor->bouncebuf_size)
269 op->data.nbytes = nor->bouncebuf_size;
271 if (op->data.dir == SPI_MEM_DATA_IN) {
272 rdbuf = op->data.buf.in;
273 op->data.buf.in = nor->bouncebuf;
275 op->data.buf.out = nor->bouncebuf;
276 memcpy(nor->bouncebuf, buf,
281 ret = spi_mem_adjust_op_size(nor->spimem, op);
285 ret = spi_mem_exec_op(nor->spimem, op);
289 if (usebouncebuf && op->data.dir == SPI_MEM_DATA_IN)
290 memcpy(rdbuf, nor->bouncebuf, op->data.nbytes);
292 return op->data.nbytes;
296 * spi_nor_spimem_read_data() - read data from flash's memory region via
298 * @nor: pointer to 'struct spi_nor'
299 * @from: offset to read from
300 * @len: number of bytes to read
301 * @buf: pointer to dst buffer
303 * Return: number of bytes read successfully, -errno otherwise
305 static ssize_t spi_nor_spimem_read_data(struct spi_nor *nor, loff_t from,
308 struct spi_mem_op op =
309 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
310 SPI_MEM_OP_ADDR(nor->addr_width, from, 1),
311 SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
312 SPI_MEM_OP_DATA_IN(len, buf, 1));
314 /* get transfer protocols. */
315 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
316 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);
317 op.dummy.buswidth = op.addr.buswidth;
318 op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
320 /* convert the dummy cycles to the number of bytes */
321 op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
323 return spi_nor_spimem_xfer_data(nor, &op);
327 * spi_nor_read_data() - read data from flash memory
328 * @nor: pointer to 'struct spi_nor'
329 * @from: offset to read from
330 * @len: number of bytes to read
331 * @buf: pointer to dst buffer
333 * Return: number of bytes read successfully, -errno otherwise
335 static ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
339 return spi_nor_spimem_read_data(nor, from, len, buf);
341 return nor->controller_ops->read(nor, from, len, buf);
345 * spi_nor_spimem_write_data() - write data to flash memory via
347 * @nor: pointer to 'struct spi_nor'
348 * @to: offset to write to
349 * @len: number of bytes to write
350 * @buf: pointer to src buffer
352 * Return: number of bytes written successfully, -errno otherwise
354 static ssize_t spi_nor_spimem_write_data(struct spi_nor *nor, loff_t to,
355 size_t len, const u8 *buf)
357 struct spi_mem_op op =
358 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1),
359 SPI_MEM_OP_ADDR(nor->addr_width, to, 1),
361 SPI_MEM_OP_DATA_OUT(len, buf, 1));
363 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto);
364 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto);
365 op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto);
367 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
370 return spi_nor_spimem_xfer_data(nor, &op);
374 * spi_nor_write_data() - write data to flash memory
375 * @nor: pointer to 'struct spi_nor'
376 * @to: offset to write to
377 * @len: number of bytes to write
378 * @buf: pointer to src buffer
380 * Return: number of bytes written successfully, -errno otherwise
382 static ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
386 return spi_nor_spimem_write_data(nor, to, len, buf);
388 return nor->controller_ops->write(nor, to, len, buf);
392 * spi_nor_write_enable() - Set write enable latch with Write Enable command.
393 * @nor: pointer to 'struct spi_nor'.
395 * Return: 0 on success, -errno otherwise.
397 static int spi_nor_write_enable(struct spi_nor *nor)
402 struct spi_mem_op op =
403 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WREN, 1),
408 ret = spi_mem_exec_op(nor->spimem, &op);
410 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WREN,
415 dev_dbg(nor->dev, "error %d on Write Enable\n", ret);
421 * spi_nor_write_disable() - Send Write Disable instruction to the chip.
422 * @nor: pointer to 'struct spi_nor'.
424 * Return: 0 on success, -errno otherwise.
426 static int spi_nor_write_disable(struct spi_nor *nor)
431 struct spi_mem_op op =
432 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRDI, 1),
437 ret = spi_mem_exec_op(nor->spimem, &op);
439 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WRDI,
444 dev_dbg(nor->dev, "error %d on Write Disable\n", ret);
450 * spi_nor_read_sr() - Read the Status Register.
451 * @nor: pointer to 'struct spi_nor'.
452 * @sr: pointer to a DMA-able buffer where the value of the
453 * Status Register will be written.
455 * Return: 0 on success, -errno otherwise.
457 static int spi_nor_read_sr(struct spi_nor *nor, u8 *sr)
462 struct spi_mem_op op =
463 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDSR, 1),
466 SPI_MEM_OP_DATA_IN(1, sr, 1));
468 ret = spi_mem_exec_op(nor->spimem, &op);
470 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDSR,
475 dev_dbg(nor->dev, "error %d reading SR\n", ret);
481 * spi_nor_read_fsr() - Read the Flag Status Register.
482 * @nor: pointer to 'struct spi_nor'
483 * @fsr: pointer to a DMA-able buffer where the value of the
484 * Flag Status Register will be written.
486 * Return: 0 on success, -errno otherwise.
488 static int spi_nor_read_fsr(struct spi_nor *nor, u8 *fsr)
493 struct spi_mem_op op =
494 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDFSR, 1),
497 SPI_MEM_OP_DATA_IN(1, fsr, 1));
499 ret = spi_mem_exec_op(nor->spimem, &op);
501 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDFSR,
506 dev_dbg(nor->dev, "error %d reading FSR\n", ret);
512 * spi_nor_read_cr() - Read the Configuration Register using the
513 * SPINOR_OP_RDCR (35h) command.
514 * @nor: pointer to 'struct spi_nor'
515 * @cr: pointer to a DMA-able buffer where the value of the
516 * Configuration Register will be written.
518 * Return: 0 on success, -errno otherwise.
520 static int spi_nor_read_cr(struct spi_nor *nor, u8 *cr)
525 struct spi_mem_op op =
526 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDCR, 1),
529 SPI_MEM_OP_DATA_IN(1, cr, 1));
531 ret = spi_mem_exec_op(nor->spimem, &op);
533 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDCR, cr, 1);
537 dev_dbg(nor->dev, "error %d reading CR\n", ret);
543 * macronix_set_4byte() - Set 4-byte address mode for Macronix flashes.
544 * @nor: pointer to 'struct spi_nor'.
545 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
548 * Return: 0 on success, -errno otherwise.
550 static int macronix_set_4byte(struct spi_nor *nor, bool enable)
555 struct spi_mem_op op =
556 SPI_MEM_OP(SPI_MEM_OP_CMD(enable ?
564 ret = spi_mem_exec_op(nor->spimem, &op);
566 ret = nor->controller_ops->write_reg(nor,
567 enable ? SPINOR_OP_EN4B :
573 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret);
579 * st_micron_set_4byte() - Set 4-byte address mode for ST and Micron flashes.
580 * @nor: pointer to 'struct spi_nor'.
581 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
584 * Return: 0 on success, -errno otherwise.
586 static int st_micron_set_4byte(struct spi_nor *nor, bool enable)
590 ret = spi_nor_write_enable(nor);
594 ret = macronix_set_4byte(nor, enable);
598 return spi_nor_write_disable(nor);
602 * spansion_set_4byte() - Set 4-byte address mode for Spansion flashes.
603 * @nor: pointer to 'struct spi_nor'.
604 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
607 * Return: 0 on success, -errno otherwise.
609 static int spansion_set_4byte(struct spi_nor *nor, bool enable)
613 nor->bouncebuf[0] = enable << 7;
616 struct spi_mem_op op =
617 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_BRWR, 1),
620 SPI_MEM_OP_DATA_OUT(1, nor->bouncebuf, 1));
622 ret = spi_mem_exec_op(nor->spimem, &op);
624 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_BRWR,
629 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret);
635 * spi_nor_write_ear() - Write Extended Address Register.
636 * @nor: pointer to 'struct spi_nor'.
637 * @ear: value to write to the Extended Address Register.
639 * Return: 0 on success, -errno otherwise.
641 static int spi_nor_write_ear(struct spi_nor *nor, u8 ear)
645 nor->bouncebuf[0] = ear;
648 struct spi_mem_op op =
649 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WREAR, 1),
652 SPI_MEM_OP_DATA_OUT(1, nor->bouncebuf, 1));
654 ret = spi_mem_exec_op(nor->spimem, &op);
656 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WREAR,
661 dev_dbg(nor->dev, "error %d writing EAR\n", ret);
667 * winbond_set_4byte() - Set 4-byte address mode for Winbond flashes.
668 * @nor: pointer to 'struct spi_nor'.
669 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte
672 * Return: 0 on success, -errno otherwise.
674 static int winbond_set_4byte(struct spi_nor *nor, bool enable)
678 ret = macronix_set_4byte(nor, enable);
683 * On Winbond W25Q256FV, leaving 4byte mode causes the Extended Address
684 * Register to be set to 1, so all 3-byte-address reads come from the
685 * second 16M. We must clear the register to enable normal behavior.
687 ret = spi_nor_write_enable(nor);
691 ret = spi_nor_write_ear(nor, 0);
695 return spi_nor_write_disable(nor);
699 * spi_nor_xread_sr() - Read the Status Register on S3AN flashes.
700 * @nor: pointer to 'struct spi_nor'.
701 * @sr: pointer to a DMA-able buffer where the value of the
702 * Status Register will be written.
704 * Return: 0 on success, -errno otherwise.
706 static int spi_nor_xread_sr(struct spi_nor *nor, u8 *sr)
711 struct spi_mem_op op =
712 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_XRDSR, 1),
715 SPI_MEM_OP_DATA_IN(1, sr, 1));
717 ret = spi_mem_exec_op(nor->spimem, &op);
719 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_XRDSR,
724 dev_dbg(nor->dev, "error %d reading XRDSR\n", ret);
730 * s3an_sr_ready() - Query the Status Register of the S3AN flash to see if the
731 * flash is ready for new commands.
732 * @nor: pointer to 'struct spi_nor'.
734 * Return: 0 on success, -errno otherwise.
736 static int s3an_sr_ready(struct spi_nor *nor)
740 ret = spi_nor_xread_sr(nor, nor->bouncebuf);
744 return !!(nor->bouncebuf[0] & XSR_RDY);
748 * spi_nor_clear_sr() - Clear the Status Register.
749 * @nor: pointer to 'struct spi_nor'.
751 static void spi_nor_clear_sr(struct spi_nor *nor)
756 struct spi_mem_op op =
757 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_CLSR, 1),
762 ret = spi_mem_exec_op(nor->spimem, &op);
764 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_CLSR,
769 dev_dbg(nor->dev, "error %d clearing SR\n", ret);
773 * spi_nor_sr_ready() - Query the Status Register to see if the flash is ready
775 * @nor: pointer to 'struct spi_nor'.
777 * Return: 0 on success, -errno otherwise.
779 static int spi_nor_sr_ready(struct spi_nor *nor)
781 int ret = spi_nor_read_sr(nor, nor->bouncebuf);
786 if (nor->flags & SNOR_F_USE_CLSR &&
787 nor->bouncebuf[0] & (SR_E_ERR | SR_P_ERR)) {
788 if (nor->bouncebuf[0] & SR_E_ERR)
789 dev_err(nor->dev, "Erase Error occurred\n");
791 dev_err(nor->dev, "Programming Error occurred\n");
793 spi_nor_clear_sr(nor);
797 return !(nor->bouncebuf[0] & SR_WIP);
801 * spi_nor_clear_fsr() - Clear the Flag Status Register.
802 * @nor: pointer to 'struct spi_nor'.
804 static void spi_nor_clear_fsr(struct spi_nor *nor)
809 struct spi_mem_op op =
810 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_CLFSR, 1),
815 ret = spi_mem_exec_op(nor->spimem, &op);
817 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_CLFSR,
822 dev_dbg(nor->dev, "error %d clearing FSR\n", ret);
826 * spi_nor_fsr_ready() - Query the Flag Status Register to see if the flash is
827 * ready for new commands.
828 * @nor: pointer to 'struct spi_nor'.
830 * Return: 0 on success, -errno otherwise.
832 static int spi_nor_fsr_ready(struct spi_nor *nor)
834 int ret = spi_nor_read_fsr(nor, nor->bouncebuf);
839 if (nor->bouncebuf[0] & (FSR_E_ERR | FSR_P_ERR)) {
840 if (nor->bouncebuf[0] & FSR_E_ERR)
841 dev_err(nor->dev, "Erase operation failed.\n");
843 dev_err(nor->dev, "Program operation failed.\n");
845 if (nor->bouncebuf[0] & FSR_PT_ERR)
847 "Attempted to modify a protected sector.\n");
849 spi_nor_clear_fsr(nor);
853 return nor->bouncebuf[0] & FSR_READY;
857 * spi_nor_ready() - Query the flash to see if it is ready for new commands.
858 * @nor: pointer to 'struct spi_nor'.
860 * Return: 0 on success, -errno otherwise.
862 static int spi_nor_ready(struct spi_nor *nor)
866 if (nor->flags & SNOR_F_READY_XSR_RDY)
867 sr = s3an_sr_ready(nor);
869 sr = spi_nor_sr_ready(nor);
872 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
879 * spi_nor_wait_till_ready_with_timeout() - Service routine to read the
880 * Status Register until ready, or timeout occurs.
881 * @nor: pointer to "struct spi_nor".
882 * @timeout_jiffies: jiffies to wait until timeout.
884 * Return: 0 on success, -errno otherwise.
886 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
887 unsigned long timeout_jiffies)
889 unsigned long deadline;
890 int timeout = 0, ret;
892 deadline = jiffies + timeout_jiffies;
895 if (time_after_eq(jiffies, deadline))
898 ret = spi_nor_ready(nor);
907 dev_dbg(nor->dev, "flash operation timed out\n");
913 * spi_nor_wait_till_ready() - Wait for a predefined amount of time for the
914 * flash to be ready, or timeout occurs.
915 * @nor: pointer to "struct spi_nor".
917 * Return: 0 on success, -errno otherwise.
919 static int spi_nor_wait_till_ready(struct spi_nor *nor)
921 return spi_nor_wait_till_ready_with_timeout(nor,
922 DEFAULT_READY_WAIT_JIFFIES);
926 * spi_nor_write_sr() - Write the Status Register.
927 * @nor: pointer to 'struct spi_nor'.
928 * @sr: pointer to DMA-able buffer to write to the Status Register.
929 * @len: number of bytes to write to the Status Register.
931 * Return: 0 on success, -errno otherwise.
933 static int spi_nor_write_sr(struct spi_nor *nor, const u8 *sr, size_t len)
937 ret = spi_nor_write_enable(nor);
942 struct spi_mem_op op =
943 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRSR, 1),
946 SPI_MEM_OP_DATA_OUT(len, sr, 1));
948 ret = spi_mem_exec_op(nor->spimem, &op);
950 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WRSR,
955 dev_dbg(nor->dev, "error %d writing SR\n", ret);
959 return spi_nor_wait_till_ready(nor);
963 * spi_nor_write_sr1_and_check() - Write one byte to the Status Register 1 and
964 * ensure that the byte written match the received value.
965 * @nor: pointer to a 'struct spi_nor'.
966 * @sr1: byte value to be written to the Status Register.
968 * Return: 0 on success, -errno otherwise.
970 static int spi_nor_write_sr1_and_check(struct spi_nor *nor, u8 sr1)
974 nor->bouncebuf[0] = sr1;
976 ret = spi_nor_write_sr(nor, nor->bouncebuf, 1);
980 ret = spi_nor_read_sr(nor, nor->bouncebuf);
984 if (nor->bouncebuf[0] != sr1) {
985 dev_dbg(nor->dev, "SR1: read back test failed\n");
993 * spi_nor_write_16bit_sr_and_check() - Write the Status Register 1 and the
994 * Status Register 2 in one shot. Ensure that the byte written in the Status
995 * Register 1 match the received value, and that the 16-bit Write did not
996 * affect what was already in the Status Register 2.
997 * @nor: pointer to a 'struct spi_nor'.
998 * @sr1: byte value to be written to the Status Register 1.
1000 * Return: 0 on success, -errno otherwise.
1002 static int spi_nor_write_16bit_sr_and_check(struct spi_nor *nor, u8 sr1)
1005 u8 *sr_cr = nor->bouncebuf;
1008 /* Make sure we don't overwrite the contents of Status Register 2. */
1009 if (!(nor->flags & SNOR_F_NO_READ_CR)) {
1010 ret = spi_nor_read_cr(nor, &sr_cr[1]);
1013 } else if (nor->params.quad_enable) {
1015 * If the Status Register 2 Read command (35h) is not
1016 * supported, we should at least be sure we don't
1017 * change the value of the SR2 Quad Enable bit.
1019 * We can safely assume that when the Quad Enable method is
1020 * set, the value of the QE bit is one, as a consequence of the
1021 * nor->params.quad_enable() call.
1023 * We can safely assume that the Quad Enable bit is present in
1024 * the Status Register 2 at BIT(1). According to the JESD216
1025 * revB standard, BFPT DWORDS[15], bits 22:20, the 16-bit
1026 * Write Status (01h) command is available just for the cases
1027 * in which the QE bit is described in SR2 at BIT(1).
1029 sr_cr[1] = CR_QUAD_EN_SPAN;
1036 ret = spi_nor_write_sr(nor, sr_cr, 2);
1040 if (nor->flags & SNOR_F_NO_READ_CR)
1043 cr_written = sr_cr[1];
1045 ret = spi_nor_read_cr(nor, &sr_cr[1]);
1049 if (cr_written != sr_cr[1]) {
1050 dev_dbg(nor->dev, "CR: read back test failed\n");
1058 * spi_nor_write_sr_and_check() - Write the Status Register 1 and ensure that
1059 * the byte written match the received value without affecting other bits in the
1060 * Status Register 1 and 2.
1061 * @nor: pointer to a 'struct spi_nor'.
1062 * @sr1: byte value to be written to the Status Register.
1064 * Return: 0 on success, -errno otherwise.
1066 static int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 sr1)
1068 if (nor->flags & SNOR_F_HAS_16BIT_SR)
1069 return spi_nor_write_16bit_sr_and_check(nor, sr1);
1071 return spi_nor_write_sr1_and_check(nor, sr1);
1075 * spi_nor_write_sr2() - Write the Status Register 2 using the
1076 * SPINOR_OP_WRSR2 (3eh) command.
1077 * @nor: pointer to 'struct spi_nor'.
1078 * @sr2: pointer to DMA-able buffer to write to the Status Register 2.
1080 * Return: 0 on success, -errno otherwise.
1082 static int spi_nor_write_sr2(struct spi_nor *nor, const u8 *sr2)
1086 ret = spi_nor_write_enable(nor);
1091 struct spi_mem_op op =
1092 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRSR2, 1),
1094 SPI_MEM_OP_NO_DUMMY,
1095 SPI_MEM_OP_DATA_OUT(1, sr2, 1));
1097 ret = spi_mem_exec_op(nor->spimem, &op);
1099 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_WRSR2,
1104 dev_dbg(nor->dev, "error %d writing SR2\n", ret);
1108 return spi_nor_wait_till_ready(nor);
1112 * spi_nor_read_sr2() - Read the Status Register 2 using the
1113 * SPINOR_OP_RDSR2 (3fh) command.
1114 * @nor: pointer to 'struct spi_nor'.
1115 * @sr2: pointer to DMA-able buffer where the value of the
1116 * Status Register 2 will be written.
1118 * Return: 0 on success, -errno otherwise.
1120 static int spi_nor_read_sr2(struct spi_nor *nor, u8 *sr2)
1125 struct spi_mem_op op =
1126 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDSR2, 1),
1128 SPI_MEM_OP_NO_DUMMY,
1129 SPI_MEM_OP_DATA_IN(1, sr2, 1));
1131 ret = spi_mem_exec_op(nor->spimem, &op);
1133 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDSR2,
1138 dev_dbg(nor->dev, "error %d reading SR2\n", ret);
1144 * spi_nor_erase_chip() - Erase the entire flash memory.
1145 * @nor: pointer to 'struct spi_nor'.
1147 * Return: 0 on success, -errno otherwise.
1149 static int spi_nor_erase_chip(struct spi_nor *nor)
1153 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10));
1156 struct spi_mem_op op =
1157 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_CHIP_ERASE, 1),
1159 SPI_MEM_OP_NO_DUMMY,
1160 SPI_MEM_OP_NO_DATA);
1162 ret = spi_mem_exec_op(nor->spimem, &op);
1164 ret = nor->controller_ops->write_reg(nor, SPINOR_OP_CHIP_ERASE,
1169 dev_dbg(nor->dev, "error %d erasing chip\n", ret);
1174 static struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
1179 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
1183 for (i = 0; i < size; i++)
1184 if (table[i][0] == opcode)
1187 /* No conversion found, keep input op code. */
1191 static u8 spi_nor_convert_3to4_read(u8 opcode)
1193 static const u8 spi_nor_3to4_read[][2] = {
1194 { SPINOR_OP_READ, SPINOR_OP_READ_4B },
1195 { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B },
1196 { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
1197 { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
1198 { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
1199 { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
1200 { SPINOR_OP_READ_1_1_8, SPINOR_OP_READ_1_1_8_4B },
1201 { SPINOR_OP_READ_1_8_8, SPINOR_OP_READ_1_8_8_4B },
1203 { SPINOR_OP_READ_1_1_1_DTR, SPINOR_OP_READ_1_1_1_DTR_4B },
1204 { SPINOR_OP_READ_1_2_2_DTR, SPINOR_OP_READ_1_2_2_DTR_4B },
1205 { SPINOR_OP_READ_1_4_4_DTR, SPINOR_OP_READ_1_4_4_DTR_4B },
1208 return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
1209 ARRAY_SIZE(spi_nor_3to4_read));
1212 static u8 spi_nor_convert_3to4_program(u8 opcode)
1214 static const u8 spi_nor_3to4_program[][2] = {
1215 { SPINOR_OP_PP, SPINOR_OP_PP_4B },
1216 { SPINOR_OP_PP_1_1_4, SPINOR_OP_PP_1_1_4_4B },
1217 { SPINOR_OP_PP_1_4_4, SPINOR_OP_PP_1_4_4_4B },
1218 { SPINOR_OP_PP_1_1_8, SPINOR_OP_PP_1_1_8_4B },
1219 { SPINOR_OP_PP_1_8_8, SPINOR_OP_PP_1_8_8_4B },
1222 return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
1223 ARRAY_SIZE(spi_nor_3to4_program));
1226 static u8 spi_nor_convert_3to4_erase(u8 opcode)
1228 static const u8 spi_nor_3to4_erase[][2] = {
1229 { SPINOR_OP_BE_4K, SPINOR_OP_BE_4K_4B },
1230 { SPINOR_OP_BE_32K, SPINOR_OP_BE_32K_4B },
1231 { SPINOR_OP_SE, SPINOR_OP_SE_4B },
1234 return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
1235 ARRAY_SIZE(spi_nor_3to4_erase));
1238 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor)
1240 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
1241 nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
1242 nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
1244 if (!spi_nor_has_uniform_erase(nor)) {
1245 struct spi_nor_erase_map *map = &nor->params.erase_map;
1246 struct spi_nor_erase_type *erase;
1249 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
1250 erase = &map->erase_type[i];
1252 spi_nor_convert_3to4_erase(erase->opcode);
1257 static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)
1261 mutex_lock(&nor->lock);
1263 if (nor->controller_ops && nor->controller_ops->prepare) {
1264 ret = nor->controller_ops->prepare(nor, ops);
1266 mutex_unlock(&nor->lock);
1273 static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
1275 if (nor->controller_ops && nor->controller_ops->unprepare)
1276 nor->controller_ops->unprepare(nor, ops);
1277 mutex_unlock(&nor->lock);
1281 * This code converts an address to the Default Address Mode, that has non
1282 * power of two page sizes. We must support this mode because it is the default
1283 * mode supported by Xilinx tools, it can access the whole flash area and
1284 * changing over to the Power-of-two mode is irreversible and corrupts the
1286 * Addr can safely be unsigned int, the biggest S3AN device is smaller than
1289 static u32 s3an_convert_addr(struct spi_nor *nor, u32 addr)
1293 offset = addr % nor->page_size;
1294 page = addr / nor->page_size;
1295 page <<= (nor->page_size > 512) ? 10 : 9;
1297 return page | offset;
1300 static u32 spi_nor_convert_addr(struct spi_nor *nor, loff_t addr)
1302 if (!nor->params.convert_addr)
1305 return nor->params.convert_addr(nor, addr);
1309 * Initiate the erasure of a single sector
1311 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
1315 addr = spi_nor_convert_addr(nor, addr);
1317 if (nor->controller_ops && nor->controller_ops->erase)
1318 return nor->controller_ops->erase(nor, addr);
1321 struct spi_mem_op op =
1322 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 1),
1323 SPI_MEM_OP_ADDR(nor->addr_width, addr, 1),
1324 SPI_MEM_OP_NO_DUMMY,
1325 SPI_MEM_OP_NO_DATA);
1327 return spi_mem_exec_op(nor->spimem, &op);
1331 * Default implementation, if driver doesn't have a specialized HW
1334 for (i = nor->addr_width - 1; i >= 0; i--) {
1335 nor->bouncebuf[i] = addr & 0xff;
1339 return nor->controller_ops->write_reg(nor, nor->erase_opcode,
1340 nor->bouncebuf, nor->addr_width);
1344 * spi_nor_div_by_erase_size() - calculate remainder and update new dividend
1345 * @erase: pointer to a structure that describes a SPI NOR erase type
1346 * @dividend: dividend value
1347 * @remainder: pointer to u32 remainder (will be updated)
1349 * Return: the result of the division
1351 static u64 spi_nor_div_by_erase_size(const struct spi_nor_erase_type *erase,
1352 u64 dividend, u32 *remainder)
1354 /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
1355 *remainder = (u32)dividend & erase->size_mask;
1356 return dividend >> erase->size_shift;
1360 * spi_nor_find_best_erase_type() - find the best erase type for the given
1361 * offset in the serial flash memory and the
1362 * number of bytes to erase. The region in
1363 * which the address fits is expected to be
1365 * @map: the erase map of the SPI NOR
1366 * @region: pointer to a structure that describes a SPI NOR erase region
1367 * @addr: offset in the serial flash memory
1368 * @len: number of bytes to erase
1370 * Return: a pointer to the best fitted erase type, NULL otherwise.
1372 static const struct spi_nor_erase_type *
1373 spi_nor_find_best_erase_type(const struct spi_nor_erase_map *map,
1374 const struct spi_nor_erase_region *region,
1377 const struct spi_nor_erase_type *erase;
1380 u8 erase_mask = region->offset & SNOR_ERASE_TYPE_MASK;
1383 * Erase types are ordered by size, with the smallest erase type at
1386 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
1387 /* Does the erase region support the tested erase type? */
1388 if (!(erase_mask & BIT(i)))
1391 erase = &map->erase_type[i];
1393 /* Don't erase more than what the user has asked for. */
1394 if (erase->size > len)
1397 /* Alignment is not mandatory for overlaid regions */
1398 if (region->offset & SNOR_OVERLAID_REGION)
1401 spi_nor_div_by_erase_size(erase, addr, &rem);
1412 * spi_nor_region_next() - get the next spi nor region
1413 * @region: pointer to a structure that describes a SPI NOR erase region
1415 * Return: the next spi nor region or NULL if last region.
1417 static struct spi_nor_erase_region *
1418 spi_nor_region_next(struct spi_nor_erase_region *region)
1420 if (spi_nor_region_is_last(region))
1427 * spi_nor_find_erase_region() - find the region of the serial flash memory in
1428 * which the offset fits
1429 * @map: the erase map of the SPI NOR
1430 * @addr: offset in the serial flash memory
1432 * Return: a pointer to the spi_nor_erase_region struct, ERR_PTR(-errno)
1435 static struct spi_nor_erase_region *
1436 spi_nor_find_erase_region(const struct spi_nor_erase_map *map, u64 addr)
1438 struct spi_nor_erase_region *region = map->regions;
1439 u64 region_start = region->offset & ~SNOR_ERASE_FLAGS_MASK;
1440 u64 region_end = region_start + region->size;
1442 while (addr < region_start || addr >= region_end) {
1443 region = spi_nor_region_next(region);
1445 return ERR_PTR(-EINVAL);
1447 region_start = region->offset & ~SNOR_ERASE_FLAGS_MASK;
1448 region_end = region_start + region->size;
1455 * spi_nor_init_erase_cmd() - initialize an erase command
1456 * @region: pointer to a structure that describes a SPI NOR erase region
1457 * @erase: pointer to a structure that describes a SPI NOR erase type
1459 * Return: the pointer to the allocated erase command, ERR_PTR(-errno)
1462 static struct spi_nor_erase_command *
1463 spi_nor_init_erase_cmd(const struct spi_nor_erase_region *region,
1464 const struct spi_nor_erase_type *erase)
1466 struct spi_nor_erase_command *cmd;
1468 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
1470 return ERR_PTR(-ENOMEM);
1472 INIT_LIST_HEAD(&cmd->list);
1473 cmd->opcode = erase->opcode;
1476 if (region->offset & SNOR_OVERLAID_REGION)
1477 cmd->size = region->size;
1479 cmd->size = erase->size;
1485 * spi_nor_destroy_erase_cmd_list() - destroy erase command list
1486 * @erase_list: list of erase commands
1488 static void spi_nor_destroy_erase_cmd_list(struct list_head *erase_list)
1490 struct spi_nor_erase_command *cmd, *next;
1492 list_for_each_entry_safe(cmd, next, erase_list, list) {
1493 list_del(&cmd->list);
1499 * spi_nor_init_erase_cmd_list() - initialize erase command list
1500 * @nor: pointer to a 'struct spi_nor'
1501 * @erase_list: list of erase commands to be executed once we validate that the
1502 * erase can be performed
1503 * @addr: offset in the serial flash memory
1504 * @len: number of bytes to erase
1506 * Builds the list of best fitted erase commands and verifies if the erase can
1509 * Return: 0 on success, -errno otherwise.
1511 static int spi_nor_init_erase_cmd_list(struct spi_nor *nor,
1512 struct list_head *erase_list,
1515 const struct spi_nor_erase_map *map = &nor->params.erase_map;
1516 const struct spi_nor_erase_type *erase, *prev_erase = NULL;
1517 struct spi_nor_erase_region *region;
1518 struct spi_nor_erase_command *cmd = NULL;
1522 region = spi_nor_find_erase_region(map, addr);
1524 return PTR_ERR(region);
1526 region_end = spi_nor_region_end(region);
1529 erase = spi_nor_find_best_erase_type(map, region, addr, len);
1531 goto destroy_erase_cmd_list;
1533 if (prev_erase != erase ||
1534 region->offset & SNOR_OVERLAID_REGION) {
1535 cmd = spi_nor_init_erase_cmd(region, erase);
1538 goto destroy_erase_cmd_list;
1541 list_add_tail(&cmd->list, erase_list);
1549 if (len && addr >= region_end) {
1550 region = spi_nor_region_next(region);
1552 goto destroy_erase_cmd_list;
1553 region_end = spi_nor_region_end(region);
1561 destroy_erase_cmd_list:
1562 spi_nor_destroy_erase_cmd_list(erase_list);
1567 * spi_nor_erase_multi_sectors() - perform a non-uniform erase
1568 * @nor: pointer to a 'struct spi_nor'
1569 * @addr: offset in the serial flash memory
1570 * @len: number of bytes to erase
1572 * Build a list of best fitted erase commands and execute it once we validate
1573 * that the erase can be performed.
1575 * Return: 0 on success, -errno otherwise.
1577 static int spi_nor_erase_multi_sectors(struct spi_nor *nor, u64 addr, u32 len)
1579 LIST_HEAD(erase_list);
1580 struct spi_nor_erase_command *cmd, *next;
1583 ret = spi_nor_init_erase_cmd_list(nor, &erase_list, addr, len);
1587 list_for_each_entry_safe(cmd, next, &erase_list, list) {
1588 nor->erase_opcode = cmd->opcode;
1589 while (cmd->count) {
1590 ret = spi_nor_write_enable(nor);
1592 goto destroy_erase_cmd_list;
1594 ret = spi_nor_erase_sector(nor, addr);
1596 goto destroy_erase_cmd_list;
1601 ret = spi_nor_wait_till_ready(nor);
1603 goto destroy_erase_cmd_list;
1605 list_del(&cmd->list);
1611 destroy_erase_cmd_list:
1612 spi_nor_destroy_erase_cmd_list(&erase_list);
1617 * Erase an address range on the nor chip. The address range may extend
1618 * one or more erase sectors. Return an error is there is a problem erasing.
1620 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
1622 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1627 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
1628 (long long)instr->len);
1630 if (spi_nor_has_uniform_erase(nor)) {
1631 div_u64_rem(instr->len, mtd->erasesize, &rem);
1639 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_ERASE);
1643 /* whole-chip erase? */
1644 if (len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) {
1645 unsigned long timeout;
1647 ret = spi_nor_write_enable(nor);
1651 ret = spi_nor_erase_chip(nor);
1656 * Scale the timeout linearly with the size of the flash, with
1657 * a minimum calibrated to an old 2MB flash. We could try to
1658 * pull these from CFI/SFDP, but these values should be good
1661 timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES,
1662 CHIP_ERASE_2MB_READY_WAIT_JIFFIES *
1663 (unsigned long)(mtd->size / SZ_2M));
1664 ret = spi_nor_wait_till_ready_with_timeout(nor, timeout);
1668 /* REVISIT in some cases we could speed up erasing large regions
1669 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
1670 * to use "small sector erase", but that's not always optimal.
1673 /* "sector"-at-a-time erase */
1674 } else if (spi_nor_has_uniform_erase(nor)) {
1676 ret = spi_nor_write_enable(nor);
1680 ret = spi_nor_erase_sector(nor, addr);
1684 addr += mtd->erasesize;
1685 len -= mtd->erasesize;
1687 ret = spi_nor_wait_till_ready(nor);
1692 /* erase multiple sectors */
1694 ret = spi_nor_erase_multi_sectors(nor, addr, len);
1699 ret = spi_nor_write_disable(nor);
1702 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
1707 static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
1710 struct mtd_info *mtd = &nor->mtd;
1711 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1712 int shift = ffs(mask) - 1;
1720 pow = ((sr & mask) ^ mask) >> shift;
1721 *len = mtd->size >> pow;
1722 if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
1725 *ofs = mtd->size - *len;
1730 * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
1731 * @locked is false); 0 otherwise
1733 static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
1742 stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
1745 /* Requested range is a sub-range of locked range */
1746 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
1748 /* Requested range does not overlap with locked range */
1749 return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
1752 static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
1755 return stm_check_lock_status_sr(nor, ofs, len, sr, true);
1758 static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
1761 return stm_check_lock_status_sr(nor, ofs, len, sr, false);
1765 * Lock a region of the flash. Compatible with ST Micro and similar flash.
1766 * Supports the block protection bits BP{0,1,2} in the status register
1767 * (SR). Does not support these features found in newer SR bitfields:
1768 * - SEC: sector/block protect - only handle SEC=0 (block protect)
1769 * - CMP: complement protect - only support CMP=0 (range is not complemented)
1771 * Support for the following is provided conditionally for some flash:
1772 * - TB: top/bottom protect
1774 * Sample table portion for 8MB flash (Winbond w25q64fw):
1776 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
1777 * --------------------------------------------------------------------------
1778 * X | X | 0 | 0 | 0 | NONE | NONE
1779 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
1780 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
1781 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
1782 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
1783 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
1784 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
1785 * X | X | 1 | 1 | 1 | 8 MB | ALL
1786 * ------|-------|-------|-------|-------|---------------|-------------------
1787 * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
1788 * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
1789 * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
1790 * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
1791 * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
1792 * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
1794 * Returns negative on errors, 0 on success.
1796 static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1798 struct mtd_info *mtd = &nor->mtd;
1799 int ret, status_old, status_new;
1800 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1801 u8 shift = ffs(mask) - 1, pow, val;
1803 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
1806 ret = spi_nor_read_sr(nor, nor->bouncebuf);
1810 status_old = nor->bouncebuf[0];
1812 /* If nothing in our range is unlocked, we don't need to do anything */
1813 if (stm_is_locked_sr(nor, ofs, len, status_old))
1816 /* If anything below us is unlocked, we can't use 'bottom' protection */
1817 if (!stm_is_locked_sr(nor, 0, ofs, status_old))
1818 can_be_bottom = false;
1820 /* If anything above us is unlocked, we can't use 'top' protection */
1821 if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
1825 if (!can_be_bottom && !can_be_top)
1828 /* Prefer top, if both are valid */
1829 use_top = can_be_top;
1831 /* lock_len: length of region that should end up locked */
1833 lock_len = mtd->size - ofs;
1835 lock_len = ofs + len;
1838 * Need smallest pow such that:
1840 * 1 / (2^pow) <= (len / size)
1842 * so (assuming power-of-2 size) we do:
1844 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
1846 pow = ilog2(mtd->size) - ilog2(lock_len);
1847 val = mask - (pow << shift);
1850 /* Don't "lock" with no region! */
1854 status_new = (status_old & ~mask & ~SR_TB) | val;
1856 /* Disallow further writes if WP pin is asserted */
1857 status_new |= SR_SRWD;
1860 status_new |= SR_TB;
1862 /* Don't bother if they're the same */
1863 if (status_new == status_old)
1866 /* Only modify protection if it will not unlock other areas */
1867 if ((status_new & mask) < (status_old & mask))
1870 return spi_nor_write_sr_and_check(nor, status_new);
1874 * Unlock a region of the flash. See stm_lock() for more info
1876 * Returns negative on errors, 0 on success.
1878 static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1880 struct mtd_info *mtd = &nor->mtd;
1881 int ret, status_old, status_new;
1882 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1883 u8 shift = ffs(mask) - 1, pow, val;
1885 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
1888 ret = spi_nor_read_sr(nor, nor->bouncebuf);
1892 status_old = nor->bouncebuf[0];
1894 /* If nothing in our range is locked, we don't need to do anything */
1895 if (stm_is_unlocked_sr(nor, ofs, len, status_old))
1898 /* If anything below us is locked, we can't use 'top' protection */
1899 if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
1902 /* If anything above us is locked, we can't use 'bottom' protection */
1903 if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
1905 can_be_bottom = false;
1907 if (!can_be_bottom && !can_be_top)
1910 /* Prefer top, if both are valid */
1911 use_top = can_be_top;
1913 /* lock_len: length of region that should remain locked */
1915 lock_len = mtd->size - (ofs + len);
1920 * Need largest pow such that:
1922 * 1 / (2^pow) >= (len / size)
1924 * so (assuming power-of-2 size) we do:
1926 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
1928 pow = ilog2(mtd->size) - order_base_2(lock_len);
1929 if (lock_len == 0) {
1930 val = 0; /* fully unlocked */
1932 val = mask - (pow << shift);
1933 /* Some power-of-two sizes are not supported */
1938 status_new = (status_old & ~mask & ~SR_TB) | val;
1940 /* Don't protect status register if we're fully unlocked */
1942 status_new &= ~SR_SRWD;
1945 status_new |= SR_TB;
1947 /* Don't bother if they're the same */
1948 if (status_new == status_old)
1951 /* Only modify protection if it will not lock other areas */
1952 if ((status_new & mask) > (status_old & mask))
1955 return spi_nor_write_sr_and_check(nor, status_new);
1959 * Check if a region of the flash is (completely) locked. See stm_lock() for
1962 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
1963 * negative on errors.
1965 static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
1969 ret = spi_nor_read_sr(nor, nor->bouncebuf);
1973 return stm_is_locked_sr(nor, ofs, len, nor->bouncebuf[0]);
1976 static const struct spi_nor_locking_ops stm_locking_ops = {
1978 .unlock = stm_unlock,
1979 .is_locked = stm_is_locked,
1982 static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1984 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1987 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_LOCK);
1991 ret = nor->params.locking_ops->lock(nor, ofs, len);
1993 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
1997 static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1999 struct spi_nor *nor = mtd_to_spi_nor(mtd);
2002 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
2006 ret = nor->params.locking_ops->unlock(nor, ofs, len);
2008 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
2012 static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2014 struct spi_nor *nor = mtd_to_spi_nor(mtd);
2017 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
2021 ret = nor->params.locking_ops->is_locked(nor, ofs, len);
2023 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
2028 * macronix_quad_enable() - set QE bit in Status Register.
2029 * @nor: pointer to a 'struct spi_nor'
2031 * Set the Quad Enable (QE) bit in the Status Register.
2033 * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
2035 * Return: 0 on success, -errno otherwise.
2037 static int macronix_quad_enable(struct spi_nor *nor)
2041 ret = spi_nor_read_sr(nor, nor->bouncebuf);
2045 if (nor->bouncebuf[0] & SR_QUAD_EN_MX)
2048 nor->bouncebuf[0] |= SR_QUAD_EN_MX;
2050 ret = spi_nor_write_sr(nor, nor->bouncebuf, 1);
2054 ret = spi_nor_read_sr(nor, nor->bouncebuf);
2058 if (!(nor->bouncebuf[0] & SR_QUAD_EN_MX)) {
2059 dev_dbg(nor->dev, "Macronix Quad bit not set\n");
2067 * spansion_no_read_cr_quad_enable() - set QE bit in Configuration Register.
2068 * @nor: pointer to a 'struct spi_nor'
2070 * Set the Quad Enable (QE) bit in the Configuration Register.
2071 * This function should be used with QSPI memories not supporting the Read
2072 * Configuration Register (35h) instruction.
2074 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
2077 * Return: 0 on success, -errno otherwise.
2079 static int spansion_no_read_cr_quad_enable(struct spi_nor *nor)
2081 u8 *sr_cr = nor->bouncebuf;
2084 /* Keep the current value of the Status Register. */
2085 ret = spi_nor_read_sr(nor, sr_cr);
2089 sr_cr[1] = CR_QUAD_EN_SPAN;
2091 return spi_nor_write_sr(nor, sr_cr, 2);
2095 * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
2096 * @nor: pointer to a 'struct spi_nor'
2098 * Set the Quad Enable (QE) bit in the Configuration Register.
2099 * This function should be used with QSPI memories supporting the Read
2100 * Configuration Register (35h) instruction.
2102 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
2105 * Return: 0 on success, -errno otherwise.
2107 static int spansion_read_cr_quad_enable(struct spi_nor *nor)
2109 u8 *sr_cr = nor->bouncebuf;
2112 /* Check current Quad Enable bit value. */
2113 ret = spi_nor_read_cr(nor, &sr_cr[1]);
2117 if (sr_cr[1] & CR_QUAD_EN_SPAN)
2120 sr_cr[1] |= CR_QUAD_EN_SPAN;
2122 /* Keep the current value of the Status Register. */
2123 ret = spi_nor_read_sr(nor, sr_cr);
2127 ret = spi_nor_write_sr(nor, sr_cr, 2);
2131 /* Read back and check it. */
2132 ret = spi_nor_read_cr(nor, &sr_cr[1]);
2136 if (!(sr_cr[1] & CR_QUAD_EN_SPAN)) {
2137 dev_dbg(nor->dev, "Spansion Quad bit not set\n");
2145 * sr2_bit7_quad_enable() - set QE bit in Status Register 2.
2146 * @nor: pointer to a 'struct spi_nor'
2148 * Set the Quad Enable (QE) bit in the Status Register 2.
2150 * This is one of the procedures to set the QE bit described in the SFDP
2151 * (JESD216 rev B) specification but no manufacturer using this procedure has
2152 * been identified yet, hence the name of the function.
2154 * Return: 0 on success, -errno otherwise.
2156 static int sr2_bit7_quad_enable(struct spi_nor *nor)
2158 u8 *sr2 = nor->bouncebuf;
2161 /* Check current Quad Enable bit value. */
2162 ret = spi_nor_read_sr2(nor, sr2);
2165 if (*sr2 & SR2_QUAD_EN_BIT7)
2168 /* Update the Quad Enable bit. */
2169 *sr2 |= SR2_QUAD_EN_BIT7;
2171 ret = spi_nor_write_sr2(nor, sr2);
2175 /* Read back and check it. */
2176 ret = spi_nor_read_sr2(nor, sr2);
2180 if (!(*sr2 & SR2_QUAD_EN_BIT7)) {
2181 dev_dbg(nor->dev, "SR2 Quad bit not set\n");
2188 /* Used when the "_ext_id" is two bytes at most */
2189 #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
2191 ((_jedec_id) >> 16) & 0xff, \
2192 ((_jedec_id) >> 8) & 0xff, \
2193 (_jedec_id) & 0xff, \
2194 ((_ext_id) >> 8) & 0xff, \
2197 .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))), \
2198 .sector_size = (_sector_size), \
2199 .n_sectors = (_n_sectors), \
2203 #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
2205 ((_jedec_id) >> 16) & 0xff, \
2206 ((_jedec_id) >> 8) & 0xff, \
2207 (_jedec_id) & 0xff, \
2208 ((_ext_id) >> 16) & 0xff, \
2209 ((_ext_id) >> 8) & 0xff, \
2213 .sector_size = (_sector_size), \
2214 .n_sectors = (_n_sectors), \
2218 #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
2219 .sector_size = (_sector_size), \
2220 .n_sectors = (_n_sectors), \
2221 .page_size = (_page_size), \
2222 .addr_width = (_addr_width), \
2225 #define S3AN_INFO(_jedec_id, _n_sectors, _page_size) \
2227 ((_jedec_id) >> 16) & 0xff, \
2228 ((_jedec_id) >> 8) & 0xff, \
2229 (_jedec_id) & 0xff \
2232 .sector_size = (8*_page_size), \
2233 .n_sectors = (_n_sectors), \
2234 .page_size = _page_size, \
2236 .flags = SPI_NOR_NO_FR | SPI_S3AN,
2239 is25lp256_post_bfpt_fixups(struct spi_nor *nor,
2240 const struct sfdp_parameter_header *bfpt_header,
2241 const struct sfdp_bfpt *bfpt,
2242 struct spi_nor_flash_parameter *params)
2245 * IS25LP256 supports 4B opcodes, but the BFPT advertises a
2246 * BFPT_DWORD1_ADDRESS_BYTES_3_ONLY address width.
2247 * Overwrite the address width advertised by the BFPT.
2249 if ((bfpt->dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) ==
2250 BFPT_DWORD1_ADDRESS_BYTES_3_ONLY)
2251 nor->addr_width = 4;
2256 static struct spi_nor_fixups is25lp256_fixups = {
2257 .post_bfpt = is25lp256_post_bfpt_fixups,
2261 mx25l25635_post_bfpt_fixups(struct spi_nor *nor,
2262 const struct sfdp_parameter_header *bfpt_header,
2263 const struct sfdp_bfpt *bfpt,
2264 struct spi_nor_flash_parameter *params)
2267 * MX25L25635F supports 4B opcodes but MX25L25635E does not.
2268 * Unfortunately, Macronix has re-used the same JEDEC ID for both
2269 * variants which prevents us from defining a new entry in the parts
2271 * We need a way to differentiate MX25L25635E and MX25L25635F, and it
2272 * seems that the F version advertises support for Fast Read 4-4-4 in
2275 if (bfpt->dwords[BFPT_DWORD(5)] & BFPT_DWORD5_FAST_READ_4_4_4)
2276 nor->flags |= SNOR_F_4B_OPCODES;
2281 static struct spi_nor_fixups mx25l25635_fixups = {
2282 .post_bfpt = mx25l25635_post_bfpt_fixups,
2285 static void gd25q256_default_init(struct spi_nor *nor)
2288 * Some manufacturer like GigaDevice may use different
2289 * bit to set QE on different memories, so the MFR can't
2290 * indicate the quad_enable method for this case, we need
2291 * to set it in the default_init fixup hook.
2293 nor->params.quad_enable = macronix_quad_enable;
2296 static struct spi_nor_fixups gd25q256_fixups = {
2297 .default_init = gd25q256_default_init,
2300 /* NOTE: double check command sets and memory organization when you add
2301 * more nor chips. This current list focusses on newer chips, which
2302 * have been converging on command sets which including JEDEC ID.
2304 * All newly added entries should describe *hardware* and should use SECT_4K
2305 * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage
2306 * scenarios excluding small sectors there is config option that can be
2307 * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS.
2308 * For historical (and compatibility) reasons (before we got above config) some
2309 * old entries may be missing 4K flag.
2311 static const struct flash_info spi_nor_ids[] = {
2312 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
2313 { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
2314 { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
2316 { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
2317 { "at25df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
2318 { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
2319 { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
2321 { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
2322 { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
2323 { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
2324 { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
2326 { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
2328 /* EON -- en25xxx */
2329 { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
2330 { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
2331 { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
2332 { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
2333 { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
2334 { "en25q80a", INFO(0x1c3014, 0, 64 * 1024, 16,
2335 SECT_4K | SPI_NOR_DUAL_READ) },
2336 { "en25qh16", INFO(0x1c7015, 0, 64 * 1024, 32,
2337 SECT_4K | SPI_NOR_DUAL_READ) },
2338 { "en25qh32", INFO(0x1c7016, 0, 64 * 1024, 64, 0) },
2339 { "en25qh64", INFO(0x1c7017, 0, 64 * 1024, 128,
2340 SECT_4K | SPI_NOR_DUAL_READ) },
2341 { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
2342 { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
2343 { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K) },
2346 { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K | SPI_NOR_HAS_LOCK) },
2347 { "f25l32qa", INFO(0x8c4116, 0, 64 * 1024, 64, SECT_4K | SPI_NOR_HAS_LOCK) },
2348 { "f25l64qa", INFO(0x8c4117, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_HAS_LOCK) },
2351 { "mr25h128", CAT25_INFO( 16 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2352 { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2353 { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2354 { "mr25h40", CAT25_INFO(512 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2357 { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE) },
2361 "gd25q16", INFO(0xc84015, 0, 64 * 1024, 32,
2362 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2363 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2366 "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64,
2367 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2368 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2371 "gd25lq32", INFO(0xc86016, 0, 64 * 1024, 64,
2372 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2373 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2376 "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128,
2377 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2378 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2381 "gd25lq64c", INFO(0xc86017, 0, 64 * 1024, 128,
2382 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2383 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2386 "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256,
2387 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2388 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2391 "gd25q256", INFO(0xc84019, 0, 64 * 1024, 512,
2392 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2393 SPI_NOR_4B_OPCODES | SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2394 .fixups = &gd25q256_fixups,
2397 /* Intel/Numonyx -- xxxs33b */
2398 { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
2399 { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
2400 { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
2403 { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K) },
2404 { "is25lq040b", INFO(0x9d4013, 0, 64 * 1024, 8,
2405 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2406 { "is25lp016d", INFO(0x9d6015, 0, 64 * 1024, 32,
2407 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2408 { "is25lp080d", INFO(0x9d6014, 0, 64 * 1024, 16,
2409 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2410 { "is25lp032", INFO(0x9d6016, 0, 64 * 1024, 64,
2411 SECT_4K | SPI_NOR_DUAL_READ) },
2412 { "is25lp064", INFO(0x9d6017, 0, 64 * 1024, 128,
2413 SECT_4K | SPI_NOR_DUAL_READ) },
2414 { "is25lp128", INFO(0x9d6018, 0, 64 * 1024, 256,
2415 SECT_4K | SPI_NOR_DUAL_READ) },
2416 { "is25lp256", INFO(0x9d6019, 0, 64 * 1024, 512,
2417 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2419 .fixups = &is25lp256_fixups },
2420 { "is25wp032", INFO(0x9d7016, 0, 64 * 1024, 64,
2421 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2422 { "is25wp064", INFO(0x9d7017, 0, 64 * 1024, 128,
2423 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2424 { "is25wp128", INFO(0x9d7018, 0, 64 * 1024, 256,
2425 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2428 { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K) },
2429 { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
2430 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
2431 { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
2432 { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
2433 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, SECT_4K) },
2434 { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) },
2435 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, SECT_4K) },
2436 { "mx25u2033e", INFO(0xc22532, 0, 64 * 1024, 4, SECT_4K) },
2437 { "mx25u3235f", INFO(0xc22536, 0, 64 * 1024, 64,
2438 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2439 { "mx25u4035", INFO(0xc22533, 0, 64 * 1024, 8, SECT_4K) },
2440 { "mx25u8035", INFO(0xc22534, 0, 64 * 1024, 16, SECT_4K) },
2441 { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
2442 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
2443 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
2444 { "mx25u12835f", INFO(0xc22538, 0, 64 * 1024, 256,
2445 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2446 { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512,
2447 SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)
2448 .fixups = &mx25l25635_fixups },
2449 { "mx25u25635f", INFO(0xc22539, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_4B_OPCODES) },
2450 { "mx25v8035f", INFO(0xc22314, 0, 64 * 1024, 16,
2451 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2452 { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
2453 { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
2454 { "mx66u51235f", INFO(0xc2253a, 0, 64 * 1024, 1024, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
2455 { "mx66l1g45g", INFO(0xc2201b, 0, 64 * 1024, 2048, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2456 { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ) },
2458 /* Micron <--> ST Micro */
2459 { "n25q016a", INFO(0x20bb15, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_QUAD_READ) },
2460 { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
2461 { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
2462 { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
2463 { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
2464 { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
2465 { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
2466 { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2467 { "n25q256ax1", INFO(0x20bb19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ) },
2468 { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
2469 { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
2470 { "n25q00a", INFO(0x20bb21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
2471 { "mt25ql02g", INFO(0x20ba22, 0, 64 * 1024, 4096,
2472 SECT_4K | USE_FSR | SPI_NOR_QUAD_READ |
2474 { "mt25qu512a (n25q512a)", INFO(0x20bb20, 0, 64 * 1024, 1024,
2475 SECT_4K | USE_FSR | SPI_NOR_DUAL_READ |
2477 SPI_NOR_4B_OPCODES) },
2478 { "mt25qu02g", INFO(0x20bb22, 0, 64 * 1024, 4096, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
2482 "mt35xu512aba", INFO(0x2c5b1a, 0, 128 * 1024, 512,
2483 SECT_4K | USE_FSR | SPI_NOR_OCTAL_READ |
2486 { "mt35xu02g", INFO(0x2c5b1c, 0, 128 * 1024, 2048,
2487 SECT_4K | USE_FSR | SPI_NOR_OCTAL_READ |
2488 SPI_NOR_4B_OPCODES) },
2491 { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
2492 { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
2493 { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
2495 /* Spansion/Cypress -- single (large) sector size only, at least
2496 * for the chips listed here (without boot sectors).
2498 { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2499 { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2500 { "s25fl128s0", INFO6(0x012018, 0x4d0080, 256 * 1024, 64,
2501 SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
2502 { "s25fl128s1", INFO6(0x012018, 0x4d0180, 64 * 1024, 256,
2503 SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
2504 { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, USE_CLSR) },
2505 { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
2506 { "s25fl512s", INFO6(0x010220, 0x4d0080, 256 * 1024, 256,
2507 SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2508 SPI_NOR_HAS_LOCK | USE_CLSR) },
2509 { "s25fs512s", INFO6(0x010220, 0x4d0081, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
2510 { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
2511 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
2512 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
2513 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
2514 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
2515 { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
2516 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
2517 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
2518 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
2519 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
2520 { "s25fl004k", INFO(0xef4013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2521 { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2522 { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2523 { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
2524 { "s25fl116k", INFO(0x014015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2525 { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) },
2526 { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
2527 { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ) },
2528 { "s25fl208k", INFO(0x014014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ) },
2529 { "s25fl064l", INFO(0x016017, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
2530 { "s25fl128l", INFO(0x016018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
2531 { "s25fl256l", INFO(0x016019, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
2533 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
2534 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
2535 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
2536 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
2537 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
2538 { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
2539 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
2540 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
2541 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
2542 { "sst25wf020a", INFO(0x621612, 0, 64 * 1024, 4, SECT_4K) },
2543 { "sst25wf040b", INFO(0x621613, 0, 64 * 1024, 8, SECT_4K) },
2544 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
2545 { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
2546 { "sst26wf016b", INFO(0xbf2651, 0, 64 * 1024, 32, SECT_4K |
2547 SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2548 { "sst26vf064b", INFO(0xbf2643, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2550 /* ST Microelectronics -- newer production may have feature updates */
2551 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
2552 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
2553 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
2554 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
2555 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
2556 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
2557 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
2558 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
2559 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
2561 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
2562 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
2563 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
2564 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
2565 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
2566 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
2567 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
2568 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
2569 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
2571 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
2572 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
2573 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
2575 { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
2576 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
2577 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
2579 { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
2580 { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
2581 { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
2582 { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
2583 { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
2584 { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
2586 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
2587 { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
2588 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
2589 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
2590 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
2591 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
2592 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
2594 "w25q16dw", INFO(0xef6015, 0, 64 * 1024, 32,
2595 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2596 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2598 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
2600 "w25q16jv-im/jm", INFO(0xef7015, 0, 64 * 1024, 32,
2601 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2602 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2604 { "w25q20cl", INFO(0xef4012, 0, 64 * 1024, 4, SECT_4K) },
2605 { "w25q20bw", INFO(0xef5012, 0, 64 * 1024, 4, SECT_4K) },
2606 { "w25q20ew", INFO(0xef6012, 0, 64 * 1024, 4, SECT_4K) },
2607 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
2609 "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64,
2610 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2611 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2614 "w25q32jv", INFO(0xef7016, 0, 64 * 1024, 64,
2615 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2616 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2618 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
2619 { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
2621 "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128,
2622 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2623 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2626 "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256,
2627 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2628 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2631 "w25q128jv", INFO(0xef7018, 0, 64 * 1024, 256,
2632 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2633 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
2635 { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
2636 { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
2637 { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
2638 { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2639 { "w25q256jvm", INFO(0xef7019, 0, 64 * 1024, 512,
2640 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2641 { "w25m512jv", INFO(0xef7119, 0, 64 * 1024, 1024,
2642 SECT_4K | SPI_NOR_QUAD_READ | SPI_NOR_DUAL_READ) },
2644 /* Catalyst / On Semiconductor -- non-JEDEC */
2645 { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2646 { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2647 { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2648 { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2649 { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
2651 /* Xilinx S3AN Internal Flash */
2652 { "3S50AN", S3AN_INFO(0x1f2200, 64, 264) },
2653 { "3S200AN", S3AN_INFO(0x1f2400, 256, 264) },
2654 { "3S400AN", S3AN_INFO(0x1f2400, 256, 264) },
2655 { "3S700AN", S3AN_INFO(0x1f2500, 512, 264) },
2656 { "3S1400AN", S3AN_INFO(0x1f2600, 512, 528) },
2658 /* XMC (Wuhan Xinxin Semiconductor Manufacturing Corp.) */
2659 { "XM25QH64A", INFO(0x207017, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2660 { "XM25QH128A", INFO(0x207018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
2664 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
2667 u8 *id = nor->bouncebuf;
2668 const struct flash_info *info;
2671 struct spi_mem_op op =
2672 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDID, 1),
2674 SPI_MEM_OP_NO_DUMMY,
2675 SPI_MEM_OP_DATA_IN(SPI_NOR_MAX_ID_LEN, id, 1));
2677 tmp = spi_mem_exec_op(nor->spimem, &op);
2679 tmp = nor->controller_ops->read_reg(nor, SPINOR_OP_RDID, id,
2680 SPI_NOR_MAX_ID_LEN);
2683 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
2684 return ERR_PTR(tmp);
2687 for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
2688 info = &spi_nor_ids[tmp];
2690 if (!memcmp(info->id, id, info->id_len))
2691 return &spi_nor_ids[tmp];
2694 dev_err(nor->dev, "unrecognized JEDEC id bytes: %*ph\n",
2695 SPI_NOR_MAX_ID_LEN, id);
2696 return ERR_PTR(-ENODEV);
2699 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
2700 size_t *retlen, u_char *buf)
2702 struct spi_nor *nor = mtd_to_spi_nor(mtd);
2705 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
2707 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ);
2714 addr = spi_nor_convert_addr(nor, addr);
2716 ret = spi_nor_read_data(nor, addr, len, buf);
2718 /* We shouldn't see 0-length reads */
2734 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
2738 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
2739 size_t *retlen, const u_char *buf)
2741 struct spi_nor *nor = mtd_to_spi_nor(mtd);
2745 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
2747 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
2751 ret = spi_nor_write_enable(nor);
2755 nor->sst_write_second = false;
2757 /* Start write from odd address. */
2759 nor->program_opcode = SPINOR_OP_BP;
2761 /* write one byte. */
2762 ret = spi_nor_write_data(nor, to, 1, buf);
2765 WARN(ret != 1, "While writing 1 byte written %i bytes\n", ret);
2766 ret = spi_nor_wait_till_ready(nor);
2774 /* Write out most of the data here. */
2775 for (; actual < len - 1; actual += 2) {
2776 nor->program_opcode = SPINOR_OP_AAI_WP;
2778 /* write two bytes. */
2779 ret = spi_nor_write_data(nor, to, 2, buf + actual);
2782 WARN(ret != 2, "While writing 2 bytes written %i bytes\n", ret);
2783 ret = spi_nor_wait_till_ready(nor);
2787 nor->sst_write_second = true;
2789 nor->sst_write_second = false;
2791 ret = spi_nor_write_disable(nor);
2795 ret = spi_nor_wait_till_ready(nor);
2799 /* Write out trailing byte if it exists. */
2800 if (actual != len) {
2801 ret = spi_nor_write_enable(nor);
2805 nor->program_opcode = SPINOR_OP_BP;
2806 ret = spi_nor_write_data(nor, to, 1, buf + actual);
2809 WARN(ret != 1, "While writing 1 byte written %i bytes\n", ret);
2810 ret = spi_nor_wait_till_ready(nor);
2816 ret = spi_nor_write_disable(nor);
2820 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
2825 * Write an address range to the nor chip. Data must be written in
2826 * FLASH_PAGESIZE chunks. The address range may be any size provided
2827 * it is within the physical boundaries.
2829 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
2830 size_t *retlen, const u_char *buf)
2832 struct spi_nor *nor = mtd_to_spi_nor(mtd);
2833 size_t page_offset, page_remain, i;
2836 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
2838 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
2842 for (i = 0; i < len; ) {
2844 loff_t addr = to + i;
2847 * If page_size is a power of two, the offset can be quickly
2848 * calculated with an AND operation. On the other cases we
2849 * need to do a modulus operation (more expensive).
2850 * Power of two numbers have only one bit set and we can use
2851 * the instruction hweight32 to detect if we need to do a
2852 * modulus (do_div()) or not.
2854 if (hweight32(nor->page_size) == 1) {
2855 page_offset = addr & (nor->page_size - 1);
2857 uint64_t aux = addr;
2859 page_offset = do_div(aux, nor->page_size);
2861 /* the size of data remaining on the first page */
2862 page_remain = min_t(size_t,
2863 nor->page_size - page_offset, len - i);
2865 addr = spi_nor_convert_addr(nor, addr);
2867 ret = spi_nor_write_enable(nor);
2871 ret = spi_nor_write_data(nor, addr, page_remain, buf + i);
2876 ret = spi_nor_wait_till_ready(nor);
2884 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
2888 static int spi_nor_check(struct spi_nor *nor)
2891 (!nor->spimem && nor->controller_ops &&
2892 (!nor->controller_ops->read ||
2893 !nor->controller_ops->write ||
2894 !nor->controller_ops->read_reg ||
2895 !nor->controller_ops->write_reg))) {
2896 pr_err("spi-nor: please fill all the necessary fields!\n");
2903 static int s3an_nor_setup(struct spi_nor *nor,
2904 const struct spi_nor_hwcaps *hwcaps)
2908 ret = spi_nor_xread_sr(nor, nor->bouncebuf);
2912 nor->erase_opcode = SPINOR_OP_XSE;
2913 nor->program_opcode = SPINOR_OP_XPP;
2914 nor->read_opcode = SPINOR_OP_READ;
2915 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
2918 * This flashes have a page size of 264 or 528 bytes (known as
2919 * Default addressing mode). It can be changed to a more standard
2920 * Power of two mode where the page size is 256/512. This comes
2921 * with a price: there is 3% less of space, the data is corrupted
2922 * and the page size cannot be changed back to default addressing
2925 * The current addressing mode can be read from the XRDSR register
2926 * and should not be changed, because is a destructive operation.
2928 if (nor->bouncebuf[0] & XSR_PAGESIZE) {
2929 /* Flash in Power of 2 mode */
2930 nor->page_size = (nor->page_size == 264) ? 256 : 512;
2931 nor->mtd.writebufsize = nor->page_size;
2932 nor->mtd.size = 8 * nor->page_size * nor->info->n_sectors;
2933 nor->mtd.erasesize = 8 * nor->page_size;
2935 /* Flash in Default addressing mode */
2936 nor->params.convert_addr = s3an_convert_addr;
2937 nor->mtd.erasesize = nor->info->sector_size;
2944 spi_nor_set_read_settings(struct spi_nor_read_command *read,
2948 enum spi_nor_protocol proto)
2950 read->num_mode_clocks = num_mode_clocks;
2951 read->num_wait_states = num_wait_states;
2952 read->opcode = opcode;
2953 read->proto = proto;
2957 spi_nor_set_pp_settings(struct spi_nor_pp_command *pp,
2959 enum spi_nor_protocol proto)
2961 pp->opcode = opcode;
2965 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
2969 for (i = 0; i < size; i++)
2970 if (table[i][0] == (int)hwcaps)
2976 static int spi_nor_hwcaps_read2cmd(u32 hwcaps)
2978 static const int hwcaps_read2cmd[][2] = {
2979 { SNOR_HWCAPS_READ, SNOR_CMD_READ },
2980 { SNOR_HWCAPS_READ_FAST, SNOR_CMD_READ_FAST },
2981 { SNOR_HWCAPS_READ_1_1_1_DTR, SNOR_CMD_READ_1_1_1_DTR },
2982 { SNOR_HWCAPS_READ_1_1_2, SNOR_CMD_READ_1_1_2 },
2983 { SNOR_HWCAPS_READ_1_2_2, SNOR_CMD_READ_1_2_2 },
2984 { SNOR_HWCAPS_READ_2_2_2, SNOR_CMD_READ_2_2_2 },
2985 { SNOR_HWCAPS_READ_1_2_2_DTR, SNOR_CMD_READ_1_2_2_DTR },
2986 { SNOR_HWCAPS_READ_1_1_4, SNOR_CMD_READ_1_1_4 },
2987 { SNOR_HWCAPS_READ_1_4_4, SNOR_CMD_READ_1_4_4 },
2988 { SNOR_HWCAPS_READ_4_4_4, SNOR_CMD_READ_4_4_4 },
2989 { SNOR_HWCAPS_READ_1_4_4_DTR, SNOR_CMD_READ_1_4_4_DTR },
2990 { SNOR_HWCAPS_READ_1_1_8, SNOR_CMD_READ_1_1_8 },
2991 { SNOR_HWCAPS_READ_1_8_8, SNOR_CMD_READ_1_8_8 },
2992 { SNOR_HWCAPS_READ_8_8_8, SNOR_CMD_READ_8_8_8 },
2993 { SNOR_HWCAPS_READ_1_8_8_DTR, SNOR_CMD_READ_1_8_8_DTR },
2996 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
2997 ARRAY_SIZE(hwcaps_read2cmd));
3000 static int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
3002 static const int hwcaps_pp2cmd[][2] = {
3003 { SNOR_HWCAPS_PP, SNOR_CMD_PP },
3004 { SNOR_HWCAPS_PP_1_1_4, SNOR_CMD_PP_1_1_4 },
3005 { SNOR_HWCAPS_PP_1_4_4, SNOR_CMD_PP_1_4_4 },
3006 { SNOR_HWCAPS_PP_4_4_4, SNOR_CMD_PP_4_4_4 },
3007 { SNOR_HWCAPS_PP_1_1_8, SNOR_CMD_PP_1_1_8 },
3008 { SNOR_HWCAPS_PP_1_8_8, SNOR_CMD_PP_1_8_8 },
3009 { SNOR_HWCAPS_PP_8_8_8, SNOR_CMD_PP_8_8_8 },
3012 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
3013 ARRAY_SIZE(hwcaps_pp2cmd));
3017 * Serial Flash Discoverable Parameters (SFDP) parsing.
3021 * spi_nor_read_raw() - raw read of serial flash memory. read_opcode,
3022 * addr_width and read_dummy members of the struct spi_nor
3023 * should be previously
3025 * @nor: pointer to a 'struct spi_nor'
3026 * @addr: offset in the serial flash memory
3027 * @len: number of bytes to read
3028 * @buf: buffer where the data is copied into (dma-safe memory)
3030 * Return: 0 on success, -errno otherwise.
3032 static int spi_nor_read_raw(struct spi_nor *nor, u32 addr, size_t len, u8 *buf)
3037 ret = spi_nor_read_data(nor, addr, len, buf);
3040 if (!ret || ret > len)
3051 * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
3052 * @nor: pointer to a 'struct spi_nor'
3053 * @addr: offset in the SFDP area to start reading data from
3054 * @len: number of bytes to read
3055 * @buf: buffer where the SFDP data are copied into (dma-safe memory)
3057 * Whatever the actual numbers of bytes for address and dummy cycles are
3058 * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
3059 * followed by a 3-byte address and 8 dummy clock cycles.
3061 * Return: 0 on success, -errno otherwise.
3063 static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr,
3064 size_t len, void *buf)
3066 u8 addr_width, read_opcode, read_dummy;
3069 read_opcode = nor->read_opcode;
3070 addr_width = nor->addr_width;
3071 read_dummy = nor->read_dummy;
3073 nor->read_opcode = SPINOR_OP_RDSFDP;
3074 nor->addr_width = 3;
3075 nor->read_dummy = 8;
3077 ret = spi_nor_read_raw(nor, addr, len, buf);
3079 nor->read_opcode = read_opcode;
3080 nor->addr_width = addr_width;
3081 nor->read_dummy = read_dummy;
3087 * spi_nor_spimem_check_op - check if the operation is supported
3089 *@nor: pointer to a 'struct spi_nor'
3090 *@op: pointer to op template to be checked
3092 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
3094 static int spi_nor_spimem_check_op(struct spi_nor *nor,
3095 struct spi_mem_op *op)
3098 * First test with 4 address bytes. The opcode itself might
3099 * be a 3B addressing opcode but we don't care, because
3100 * SPI controller implementation should not check the opcode,
3101 * but just the sequence.
3103 op->addr.nbytes = 4;
3104 if (!spi_mem_supports_op(nor->spimem, op)) {
3105 if (nor->mtd.size > SZ_16M)
3108 /* If flash size <= 16MB, 3 address bytes are sufficient */
3109 op->addr.nbytes = 3;
3110 if (!spi_mem_supports_op(nor->spimem, op))
3118 * spi_nor_spimem_check_readop - check if the read op is supported
3120 *@nor: pointer to a 'struct spi_nor'
3121 *@read: pointer to op template to be checked
3123 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
3125 static int spi_nor_spimem_check_readop(struct spi_nor *nor,
3126 const struct spi_nor_read_command *read)
3128 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(read->opcode, 1),
3129 SPI_MEM_OP_ADDR(3, 0, 1),
3130 SPI_MEM_OP_DUMMY(0, 1),
3131 SPI_MEM_OP_DATA_IN(0, NULL, 1));
3133 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(read->proto);
3134 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(read->proto);
3135 op.data.buswidth = spi_nor_get_protocol_data_nbits(read->proto);
3136 op.dummy.buswidth = op.addr.buswidth;
3137 op.dummy.nbytes = (read->num_mode_clocks + read->num_wait_states) *
3138 op.dummy.buswidth / 8;
3140 return spi_nor_spimem_check_op(nor, &op);
3144 * spi_nor_spimem_check_pp - check if the page program op is supported
3146 *@nor: pointer to a 'struct spi_nor'
3147 *@pp: pointer to op template to be checked
3149 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
3151 static int spi_nor_spimem_check_pp(struct spi_nor *nor,
3152 const struct spi_nor_pp_command *pp)
3154 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(pp->opcode, 1),
3155 SPI_MEM_OP_ADDR(3, 0, 1),
3156 SPI_MEM_OP_NO_DUMMY,
3157 SPI_MEM_OP_DATA_OUT(0, NULL, 1));
3159 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(pp->proto);
3160 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(pp->proto);
3161 op.data.buswidth = spi_nor_get_protocol_data_nbits(pp->proto);
3163 return spi_nor_spimem_check_op(nor, &op);
3167 * spi_nor_spimem_adjust_hwcaps - Find optimal Read/Write protocol
3168 * based on SPI controller capabilities
3169 * @nor: pointer to a 'struct spi_nor'
3170 * @hwcaps: pointer to resulting capabilities after adjusting
3171 * according to controller and flash's capability
3174 spi_nor_spimem_adjust_hwcaps(struct spi_nor *nor, u32 *hwcaps)
3176 struct spi_nor_flash_parameter *params = &nor->params;
3179 /* DTR modes are not supported yet, mask them all. */
3180 *hwcaps &= ~SNOR_HWCAPS_DTR;
3182 /* X-X-X modes are not supported yet, mask them all. */
3183 *hwcaps &= ~SNOR_HWCAPS_X_X_X;
3185 for (cap = 0; cap < sizeof(*hwcaps) * BITS_PER_BYTE; cap++) {
3188 if (!(*hwcaps & BIT(cap)))
3191 rdidx = spi_nor_hwcaps_read2cmd(BIT(cap));
3193 spi_nor_spimem_check_readop(nor, ¶ms->reads[rdidx]))
3194 *hwcaps &= ~BIT(cap);
3196 ppidx = spi_nor_hwcaps_pp2cmd(BIT(cap));
3200 if (spi_nor_spimem_check_pp(nor,
3201 ¶ms->page_programs[ppidx]))
3202 *hwcaps &= ~BIT(cap);
3207 * spi_nor_read_sfdp_dma_unsafe() - read Serial Flash Discoverable Parameters.
3208 * @nor: pointer to a 'struct spi_nor'
3209 * @addr: offset in the SFDP area to start reading data from
3210 * @len: number of bytes to read
3211 * @buf: buffer where the SFDP data are copied into
3213 * Wrap spi_nor_read_sfdp() using a kmalloc'ed bounce buffer as @buf is now not
3214 * guaranteed to be dma-safe.
3216 * Return: -ENOMEM if kmalloc() fails, the return code of spi_nor_read_sfdp()
3219 static int spi_nor_read_sfdp_dma_unsafe(struct spi_nor *nor, u32 addr,
3220 size_t len, void *buf)
3225 dma_safe_buf = kmalloc(len, GFP_KERNEL);
3229 ret = spi_nor_read_sfdp(nor, addr, len, dma_safe_buf);
3230 memcpy(buf, dma_safe_buf, len);
3231 kfree(dma_safe_buf);
3236 /* Fast Read settings. */
3239 spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read,
3241 enum spi_nor_protocol proto)
3243 read->num_mode_clocks = (half >> 5) & 0x07;
3244 read->num_wait_states = (half >> 0) & 0x1f;
3245 read->opcode = (half >> 8) & 0xff;
3246 read->proto = proto;
3249 struct sfdp_bfpt_read {
3250 /* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
3254 * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us
3255 * whether the Fast Read x-y-z command is supported.
3257 u32 supported_dword;
3261 * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
3262 * encodes the op code, the number of mode clocks and the number of wait
3263 * states to be used by Fast Read x-y-z command.
3268 /* The SPI protocol for this Fast Read x-y-z command. */
3269 enum spi_nor_protocol proto;
3272 static const struct sfdp_bfpt_read sfdp_bfpt_reads[] = {
3273 /* Fast Read 1-1-2 */
3275 SNOR_HWCAPS_READ_1_1_2,
3276 BFPT_DWORD(1), BIT(16), /* Supported bit */
3277 BFPT_DWORD(4), 0, /* Settings */
3281 /* Fast Read 1-2-2 */
3283 SNOR_HWCAPS_READ_1_2_2,
3284 BFPT_DWORD(1), BIT(20), /* Supported bit */
3285 BFPT_DWORD(4), 16, /* Settings */
3289 /* Fast Read 2-2-2 */
3291 SNOR_HWCAPS_READ_2_2_2,
3292 BFPT_DWORD(5), BIT(0), /* Supported bit */
3293 BFPT_DWORD(6), 16, /* Settings */
3297 /* Fast Read 1-1-4 */
3299 SNOR_HWCAPS_READ_1_1_4,
3300 BFPT_DWORD(1), BIT(22), /* Supported bit */
3301 BFPT_DWORD(3), 16, /* Settings */
3305 /* Fast Read 1-4-4 */
3307 SNOR_HWCAPS_READ_1_4_4,
3308 BFPT_DWORD(1), BIT(21), /* Supported bit */
3309 BFPT_DWORD(3), 0, /* Settings */
3313 /* Fast Read 4-4-4 */
3315 SNOR_HWCAPS_READ_4_4_4,
3316 BFPT_DWORD(5), BIT(4), /* Supported bit */
3317 BFPT_DWORD(7), 16, /* Settings */
3322 struct sfdp_bfpt_erase {
3324 * The half-word at offset <shift> in DWORD <dwoard> encodes the
3325 * op code and erase sector size to be used by Sector Erase commands.
3331 static const struct sfdp_bfpt_erase sfdp_bfpt_erases[] = {
3332 /* Erase Type 1 in DWORD8 bits[15:0] */
3335 /* Erase Type 2 in DWORD8 bits[31:16] */
3336 {BFPT_DWORD(8), 16},
3338 /* Erase Type 3 in DWORD9 bits[15:0] */
3341 /* Erase Type 4 in DWORD9 bits[31:16] */
3342 {BFPT_DWORD(9), 16},
3346 * spi_nor_set_erase_type() - set a SPI NOR erase type
3347 * @erase: pointer to a structure that describes a SPI NOR erase type
3348 * @size: the size of the sector/block erased by the erase type
3349 * @opcode: the SPI command op code to erase the sector/block
3351 static void spi_nor_set_erase_type(struct spi_nor_erase_type *erase,
3352 u32 size, u8 opcode)
3355 erase->opcode = opcode;
3356 /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
3357 erase->size_shift = ffs(erase->size) - 1;
3358 erase->size_mask = (1 << erase->size_shift) - 1;
3362 * spi_nor_set_erase_settings_from_bfpt() - set erase type settings from BFPT
3363 * @erase: pointer to a structure that describes a SPI NOR erase type
3364 * @size: the size of the sector/block erased by the erase type
3365 * @opcode: the SPI command op code to erase the sector/block
3366 * @i: erase type index as sorted in the Basic Flash Parameter Table
3368 * The supported Erase Types will be sorted at init in ascending order, with
3369 * the smallest Erase Type size being the first member in the erase_type array
3370 * of the spi_nor_erase_map structure. Save the Erase Type index as sorted in
3371 * the Basic Flash Parameter Table since it will be used later on to
3372 * synchronize with the supported Erase Types defined in SFDP optional tables.
3375 spi_nor_set_erase_settings_from_bfpt(struct spi_nor_erase_type *erase,
3376 u32 size, u8 opcode, u8 i)
3379 spi_nor_set_erase_type(erase, size, opcode);
3383 * spi_nor_map_cmp_erase_type() - compare the map's erase types by size
3384 * @l: member in the left half of the map's erase_type array
3385 * @r: member in the right half of the map's erase_type array
3387 * Comparison function used in the sort() call to sort in ascending order the
3388 * map's erase types, the smallest erase type size being the first member in the
3389 * sorted erase_type array.
3391 * Return: the result of @l->size - @r->size
3393 static int spi_nor_map_cmp_erase_type(const void *l, const void *r)
3395 const struct spi_nor_erase_type *left = l, *right = r;
3397 return left->size - right->size;
3401 * spi_nor_sort_erase_mask() - sort erase mask
3402 * @map: the erase map of the SPI NOR
3403 * @erase_mask: the erase type mask to be sorted
3405 * Replicate the sort done for the map's erase types in BFPT: sort the erase
3406 * mask in ascending order with the smallest erase type size starting from
3407 * BIT(0) in the sorted erase mask.
3409 * Return: sorted erase mask.
3411 static u8 spi_nor_sort_erase_mask(struct spi_nor_erase_map *map, u8 erase_mask)
3413 struct spi_nor_erase_type *erase_type = map->erase_type;
3415 u8 sorted_erase_mask = 0;
3420 /* Replicate the sort done for the map's erase types. */
3421 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++)
3422 if (erase_type[i].size && erase_mask & BIT(erase_type[i].idx))
3423 sorted_erase_mask |= BIT(i);
3425 return sorted_erase_mask;
3429 * spi_nor_regions_sort_erase_types() - sort erase types in each region
3430 * @map: the erase map of the SPI NOR
3432 * Function assumes that the erase types defined in the erase map are already
3433 * sorted in ascending order, with the smallest erase type size being the first
3434 * member in the erase_type array. It replicates the sort done for the map's
3435 * erase types. Each region's erase bitmask will indicate which erase types are
3436 * supported from the sorted erase types defined in the erase map.
3437 * Sort the all region's erase type at init in order to speed up the process of
3438 * finding the best erase command at runtime.
3440 static void spi_nor_regions_sort_erase_types(struct spi_nor_erase_map *map)
3442 struct spi_nor_erase_region *region = map->regions;
3443 u8 region_erase_mask, sorted_erase_mask;
3446 region_erase_mask = region->offset & SNOR_ERASE_TYPE_MASK;
3448 sorted_erase_mask = spi_nor_sort_erase_mask(map,
3451 /* Overwrite erase mask. */
3452 region->offset = (region->offset & ~SNOR_ERASE_TYPE_MASK) |
3455 region = spi_nor_region_next(region);
3460 * spi_nor_init_uniform_erase_map() - Initialize uniform erase map
3461 * @map: the erase map of the SPI NOR
3462 * @erase_mask: bitmask encoding erase types that can erase the entire
3464 * @flash_size: the spi nor flash memory size
3466 static void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map,
3467 u8 erase_mask, u64 flash_size)
3469 /* Offset 0 with erase_mask and SNOR_LAST_REGION bit set */
3470 map->uniform_region.offset = (erase_mask & SNOR_ERASE_TYPE_MASK) |
3472 map->uniform_region.size = flash_size;
3473 map->regions = &map->uniform_region;
3474 map->uniform_erase_type = erase_mask;
3478 spi_nor_post_bfpt_fixups(struct spi_nor *nor,
3479 const struct sfdp_parameter_header *bfpt_header,
3480 const struct sfdp_bfpt *bfpt,
3481 struct spi_nor_flash_parameter *params)
3483 if (nor->info->fixups && nor->info->fixups->post_bfpt)
3484 return nor->info->fixups->post_bfpt(nor, bfpt_header, bfpt,
3491 * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
3492 * @nor: pointer to a 'struct spi_nor'
3493 * @bfpt_header: pointer to the 'struct sfdp_parameter_header' describing
3494 * the Basic Flash Parameter Table length and version
3495 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
3498 * The Basic Flash Parameter Table is the main and only mandatory table as
3499 * defined by the SFDP (JESD216) specification.
3500 * It provides us with the total size (memory density) of the data array and
3501 * the number of address bytes for Fast Read, Page Program and Sector Erase
3503 * For Fast READ commands, it also gives the number of mode clock cycles and
3504 * wait states (regrouped in the number of dummy clock cycles) for each
3505 * supported instruction op code.
3506 * For Page Program, the page size is now available since JESD216 rev A, however
3507 * the supported instruction op codes are still not provided.
3508 * For Sector Erase commands, this table stores the supported instruction op
3509 * codes and the associated sector sizes.
3510 * Finally, the Quad Enable Requirements (QER) are also available since JESD216
3511 * rev A. The QER bits encode the manufacturer dependent procedure to be
3512 * executed to set the Quad Enable (QE) bit in some internal register of the
3513 * Quad SPI memory. Indeed the QE bit, when it exists, must be set before
3514 * sending any Quad SPI command to the memory. Actually, setting the QE bit
3515 * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2
3516 * and IO3 hence enabling 4 (Quad) I/O lines.
3518 * Return: 0 on success, -errno otherwise.
3520 static int spi_nor_parse_bfpt(struct spi_nor *nor,
3521 const struct sfdp_parameter_header *bfpt_header,
3522 struct spi_nor_flash_parameter *params)
3524 struct spi_nor_erase_map *map = ¶ms->erase_map;
3525 struct spi_nor_erase_type *erase_type = map->erase_type;
3526 struct sfdp_bfpt bfpt;
3533 /* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */
3534 if (bfpt_header->length < BFPT_DWORD_MAX_JESD216)
3537 /* Read the Basic Flash Parameter Table. */
3538 len = min_t(size_t, sizeof(bfpt),
3539 bfpt_header->length * sizeof(u32));
3540 addr = SFDP_PARAM_HEADER_PTP(bfpt_header);
3541 memset(&bfpt, 0, sizeof(bfpt));
3542 err = spi_nor_read_sfdp_dma_unsafe(nor, addr, len, &bfpt);
3546 /* Fix endianness of the BFPT DWORDs. */
3547 for (i = 0; i < BFPT_DWORD_MAX; i++)
3548 bfpt.dwords[i] = le32_to_cpu(bfpt.dwords[i]);
3550 /* Number of address bytes. */
3551 switch (bfpt.dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) {
3552 case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY:
3553 nor->addr_width = 3;
3556 case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY:
3557 nor->addr_width = 4;
3564 /* Flash Memory Density (in bits). */
3565 params->size = bfpt.dwords[BFPT_DWORD(2)];
3566 if (params->size & BIT(31)) {
3567 params->size &= ~BIT(31);
3570 * Prevent overflows on params->size. Anyway, a NOR of 2^64
3571 * bits is unlikely to exist so this error probably means
3572 * the BFPT we are reading is corrupted/wrong.
3574 if (params->size > 63)
3577 params->size = 1ULL << params->size;
3581 params->size >>= 3; /* Convert to bytes. */
3583 /* Fast Read settings. */
3584 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_reads); i++) {
3585 const struct sfdp_bfpt_read *rd = &sfdp_bfpt_reads[i];
3586 struct spi_nor_read_command *read;
3588 if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) {
3589 params->hwcaps.mask &= ~rd->hwcaps;
3593 params->hwcaps.mask |= rd->hwcaps;
3594 cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps);
3595 read = ¶ms->reads[cmd];
3596 half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift;
3597 spi_nor_set_read_settings_from_bfpt(read, half, rd->proto);
3601 * Sector Erase settings. Reinitialize the uniform erase map using the
3602 * Erase Types defined in the bfpt table.
3605 memset(¶ms->erase_map, 0, sizeof(params->erase_map));
3606 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_erases); i++) {
3607 const struct sfdp_bfpt_erase *er = &sfdp_bfpt_erases[i];
3611 half = bfpt.dwords[er->dword] >> er->shift;
3612 erasesize = half & 0xff;
3614 /* erasesize == 0 means this Erase Type is not supported. */
3618 erasesize = 1U << erasesize;
3619 opcode = (half >> 8) & 0xff;
3620 erase_mask |= BIT(i);
3621 spi_nor_set_erase_settings_from_bfpt(&erase_type[i], erasesize,
3624 spi_nor_init_uniform_erase_map(map, erase_mask, params->size);
3626 * Sort all the map's Erase Types in ascending order with the smallest
3627 * erase size being the first member in the erase_type array.
3629 sort(erase_type, SNOR_ERASE_TYPE_MAX, sizeof(erase_type[0]),
3630 spi_nor_map_cmp_erase_type, NULL);
3632 * Sort the erase types in the uniform region in order to update the
3633 * uniform_erase_type bitmask. The bitmask will be used later on when
3634 * selecting the uniform erase.
3636 spi_nor_regions_sort_erase_types(map);
3637 map->uniform_erase_type = map->uniform_region.offset &
3638 SNOR_ERASE_TYPE_MASK;
3640 /* Stop here if not JESD216 rev A or later. */
3641 if (bfpt_header->length < BFPT_DWORD_MAX)
3642 return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt,
3645 /* Page size: this field specifies 'N' so the page size = 2^N bytes. */
3646 params->page_size = bfpt.dwords[BFPT_DWORD(11)];
3647 params->page_size &= BFPT_DWORD11_PAGE_SIZE_MASK;
3648 params->page_size >>= BFPT_DWORD11_PAGE_SIZE_SHIFT;
3649 params->page_size = 1U << params->page_size;
3651 /* Quad Enable Requirements. */
3652 switch (bfpt.dwords[BFPT_DWORD(15)] & BFPT_DWORD15_QER_MASK) {
3653 case BFPT_DWORD15_QER_NONE:
3654 params->quad_enable = NULL;
3657 case BFPT_DWORD15_QER_SR2_BIT1_BUGGY:
3659 * Writing only one byte to the Status Register has the
3660 * side-effect of clearing Status Register 2.
3662 case BFPT_DWORD15_QER_SR2_BIT1_NO_RD:
3664 * Read Configuration Register (35h) instruction is not
3667 nor->flags |= SNOR_F_HAS_16BIT_SR | SNOR_F_NO_READ_CR;
3668 params->quad_enable = spansion_no_read_cr_quad_enable;
3671 case BFPT_DWORD15_QER_SR1_BIT6:
3672 nor->flags &= ~SNOR_F_HAS_16BIT_SR;
3673 params->quad_enable = macronix_quad_enable;
3676 case BFPT_DWORD15_QER_SR2_BIT7:
3677 nor->flags &= ~SNOR_F_HAS_16BIT_SR;
3678 params->quad_enable = sr2_bit7_quad_enable;
3681 case BFPT_DWORD15_QER_SR2_BIT1:
3683 * JESD216 rev B or later does not specify if writing only one
3684 * byte to the Status Register clears or not the Status
3685 * Register 2, so let's be cautious and keep the default
3686 * assumption of a 16-bit Write Status (01h) command.
3688 nor->flags |= SNOR_F_HAS_16BIT_SR;
3690 params->quad_enable = spansion_read_cr_quad_enable;
3697 return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt, params);
3700 #define SMPT_CMD_ADDRESS_LEN_MASK GENMASK(23, 22)
3701 #define SMPT_CMD_ADDRESS_LEN_0 (0x0UL << 22)
3702 #define SMPT_CMD_ADDRESS_LEN_3 (0x1UL << 22)
3703 #define SMPT_CMD_ADDRESS_LEN_4 (0x2UL << 22)
3704 #define SMPT_CMD_ADDRESS_LEN_USE_CURRENT (0x3UL << 22)
3706 #define SMPT_CMD_READ_DUMMY_MASK GENMASK(19, 16)
3707 #define SMPT_CMD_READ_DUMMY_SHIFT 16
3708 #define SMPT_CMD_READ_DUMMY(_cmd) \
3709 (((_cmd) & SMPT_CMD_READ_DUMMY_MASK) >> SMPT_CMD_READ_DUMMY_SHIFT)
3710 #define SMPT_CMD_READ_DUMMY_IS_VARIABLE 0xfUL
3712 #define SMPT_CMD_READ_DATA_MASK GENMASK(31, 24)
3713 #define SMPT_CMD_READ_DATA_SHIFT 24
3714 #define SMPT_CMD_READ_DATA(_cmd) \
3715 (((_cmd) & SMPT_CMD_READ_DATA_MASK) >> SMPT_CMD_READ_DATA_SHIFT)
3717 #define SMPT_CMD_OPCODE_MASK GENMASK(15, 8)
3718 #define SMPT_CMD_OPCODE_SHIFT 8
3719 #define SMPT_CMD_OPCODE(_cmd) \
3720 (((_cmd) & SMPT_CMD_OPCODE_MASK) >> SMPT_CMD_OPCODE_SHIFT)
3722 #define SMPT_MAP_REGION_COUNT_MASK GENMASK(23, 16)
3723 #define SMPT_MAP_REGION_COUNT_SHIFT 16
3724 #define SMPT_MAP_REGION_COUNT(_header) \
3725 ((((_header) & SMPT_MAP_REGION_COUNT_MASK) >> \
3726 SMPT_MAP_REGION_COUNT_SHIFT) + 1)
3728 #define SMPT_MAP_ID_MASK GENMASK(15, 8)
3729 #define SMPT_MAP_ID_SHIFT 8
3730 #define SMPT_MAP_ID(_header) \
3731 (((_header) & SMPT_MAP_ID_MASK) >> SMPT_MAP_ID_SHIFT)
3733 #define SMPT_MAP_REGION_SIZE_MASK GENMASK(31, 8)
3734 #define SMPT_MAP_REGION_SIZE_SHIFT 8
3735 #define SMPT_MAP_REGION_SIZE(_region) \
3736 (((((_region) & SMPT_MAP_REGION_SIZE_MASK) >> \
3737 SMPT_MAP_REGION_SIZE_SHIFT) + 1) * 256)
3739 #define SMPT_MAP_REGION_ERASE_TYPE_MASK GENMASK(3, 0)
3740 #define SMPT_MAP_REGION_ERASE_TYPE(_region) \
3741 ((_region) & SMPT_MAP_REGION_ERASE_TYPE_MASK)
3743 #define SMPT_DESC_TYPE_MAP BIT(1)
3744 #define SMPT_DESC_END BIT(0)
3747 * spi_nor_smpt_addr_width() - return the address width used in the
3748 * configuration detection command.
3749 * @nor: pointer to a 'struct spi_nor'
3750 * @settings: configuration detection command descriptor, dword1
3752 static u8 spi_nor_smpt_addr_width(const struct spi_nor *nor, const u32 settings)
3754 switch (settings & SMPT_CMD_ADDRESS_LEN_MASK) {
3755 case SMPT_CMD_ADDRESS_LEN_0:
3757 case SMPT_CMD_ADDRESS_LEN_3:
3759 case SMPT_CMD_ADDRESS_LEN_4:
3761 case SMPT_CMD_ADDRESS_LEN_USE_CURRENT:
3764 return nor->addr_width;
3769 * spi_nor_smpt_read_dummy() - return the configuration detection command read
3770 * latency, in clock cycles.
3771 * @nor: pointer to a 'struct spi_nor'
3772 * @settings: configuration detection command descriptor, dword1
3774 * Return: the number of dummy cycles for an SMPT read
3776 static u8 spi_nor_smpt_read_dummy(const struct spi_nor *nor, const u32 settings)
3778 u8 read_dummy = SMPT_CMD_READ_DUMMY(settings);
3780 if (read_dummy == SMPT_CMD_READ_DUMMY_IS_VARIABLE)
3781 return nor->read_dummy;
3786 * spi_nor_get_map_in_use() - get the configuration map in use
3787 * @nor: pointer to a 'struct spi_nor'
3788 * @smpt: pointer to the sector map parameter table
3789 * @smpt_len: sector map parameter table length
3791 * Return: pointer to the map in use, ERR_PTR(-errno) otherwise.
3793 static const u32 *spi_nor_get_map_in_use(struct spi_nor *nor, const u32 *smpt,
3801 u8 addr_width, read_opcode, read_dummy;
3802 u8 read_data_mask, map_id;
3804 /* Use a kmalloc'ed bounce buffer to guarantee it is DMA-able. */
3805 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3807 return ERR_PTR(-ENOMEM);
3809 addr_width = nor->addr_width;
3810 read_dummy = nor->read_dummy;
3811 read_opcode = nor->read_opcode;
3814 /* Determine if there are any optional Detection Command Descriptors */
3815 for (i = 0; i < smpt_len; i += 2) {
3816 if (smpt[i] & SMPT_DESC_TYPE_MAP)
3819 read_data_mask = SMPT_CMD_READ_DATA(smpt[i]);
3820 nor->addr_width = spi_nor_smpt_addr_width(nor, smpt[i]);
3821 nor->read_dummy = spi_nor_smpt_read_dummy(nor, smpt[i]);
3822 nor->read_opcode = SMPT_CMD_OPCODE(smpt[i]);
3825 err = spi_nor_read_raw(nor, addr, 1, buf);
3832 * Build an index value that is used to select the Sector Map
3833 * Configuration that is currently in use.
3835 map_id = map_id << 1 | !!(*buf & read_data_mask);
3839 * If command descriptors are provided, they always precede map
3840 * descriptors in the table. There is no need to start the iteration
3841 * over smpt array all over again.
3843 * Find the matching configuration map.
3845 ret = ERR_PTR(-EINVAL);
3846 while (i < smpt_len) {
3847 if (SMPT_MAP_ID(smpt[i]) == map_id) {
3853 * If there are no more configuration map descriptors and no
3854 * configuration ID matched the configuration identifier, the
3855 * sector address map is unknown.
3857 if (smpt[i] & SMPT_DESC_END)
3860 /* increment the table index to the next map */
3861 i += SMPT_MAP_REGION_COUNT(smpt[i]) + 1;
3867 nor->addr_width = addr_width;
3868 nor->read_dummy = read_dummy;
3869 nor->read_opcode = read_opcode;
3874 * spi_nor_region_check_overlay() - set overlay bit when the region is overlaid
3875 * @region: pointer to a structure that describes a SPI NOR erase region
3876 * @erase: pointer to a structure that describes a SPI NOR erase type
3877 * @erase_type: erase type bitmask
3880 spi_nor_region_check_overlay(struct spi_nor_erase_region *region,
3881 const struct spi_nor_erase_type *erase,
3882 const u8 erase_type)
3886 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
3887 if (!(erase_type & BIT(i)))
3889 if (region->size & erase[i].size_mask) {
3890 spi_nor_region_mark_overlay(region);
3897 * spi_nor_init_non_uniform_erase_map() - initialize the non-uniform erase map
3898 * @nor: pointer to a 'struct spi_nor'
3899 * @params: pointer to a duplicate 'struct spi_nor_flash_parameter' that is
3900 * used for storing SFDP parsed data
3901 * @smpt: pointer to the sector map parameter table
3903 * Return: 0 on success, -errno otherwise.
3906 spi_nor_init_non_uniform_erase_map(struct spi_nor *nor,
3907 struct spi_nor_flash_parameter *params,
3910 struct spi_nor_erase_map *map = ¶ms->erase_map;
3911 struct spi_nor_erase_type *erase = map->erase_type;
3912 struct spi_nor_erase_region *region;
3916 u8 uniform_erase_type, save_uniform_erase_type;
3917 u8 erase_type, regions_erase_type;
3919 region_count = SMPT_MAP_REGION_COUNT(*smpt);
3921 * The regions will be freed when the driver detaches from the
3924 region = devm_kcalloc(nor->dev, region_count, sizeof(*region),
3928 map->regions = region;
3930 uniform_erase_type = 0xff;
3931 regions_erase_type = 0;
3933 /* Populate regions. */
3934 for (i = 0; i < region_count; i++) {
3935 j = i + 1; /* index for the region dword */
3936 region[i].size = SMPT_MAP_REGION_SIZE(smpt[j]);
3937 erase_type = SMPT_MAP_REGION_ERASE_TYPE(smpt[j]);
3938 region[i].offset = offset | erase_type;
3940 spi_nor_region_check_overlay(®ion[i], erase, erase_type);
3943 * Save the erase types that are supported in all regions and
3944 * can erase the entire flash memory.
3946 uniform_erase_type &= erase_type;
3949 * regions_erase_type mask will indicate all the erase types
3950 * supported in this configuration map.
3952 regions_erase_type |= erase_type;
3954 offset = (region[i].offset & ~SNOR_ERASE_FLAGS_MASK) +
3958 save_uniform_erase_type = map->uniform_erase_type;
3959 map->uniform_erase_type = spi_nor_sort_erase_mask(map,
3960 uniform_erase_type);
3962 if (!regions_erase_type) {
3964 * Roll back to the previous uniform_erase_type mask, SMPT is
3967 map->uniform_erase_type = save_uniform_erase_type;
3972 * BFPT advertises all the erase types supported by all the possible
3973 * map configurations. Mask out the erase types that are not supported
3974 * by the current map configuration.
3976 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++)
3977 if (!(regions_erase_type & BIT(erase[i].idx)))
3978 spi_nor_set_erase_type(&erase[i], 0, 0xFF);
3980 spi_nor_region_mark_end(®ion[i - 1]);
3986 * spi_nor_parse_smpt() - parse Sector Map Parameter Table
3987 * @nor: pointer to a 'struct spi_nor'
3988 * @smpt_header: sector map parameter table header
3989 * @params: pointer to a duplicate 'struct spi_nor_flash_parameter'
3990 * that is used for storing SFDP parsed data
3992 * This table is optional, but when available, we parse it to identify the
3993 * location and size of sectors within the main data array of the flash memory
3994 * device and to identify which Erase Types are supported by each sector.
3996 * Return: 0 on success, -errno otherwise.
3998 static int spi_nor_parse_smpt(struct spi_nor *nor,
3999 const struct sfdp_parameter_header *smpt_header,
4000 struct spi_nor_flash_parameter *params)
4002 const u32 *sector_map;
4008 /* Read the Sector Map Parameter Table. */
4009 len = smpt_header->length * sizeof(*smpt);
4010 smpt = kmalloc(len, GFP_KERNEL);
4014 addr = SFDP_PARAM_HEADER_PTP(smpt_header);
4015 ret = spi_nor_read_sfdp(nor, addr, len, smpt);
4019 /* Fix endianness of the SMPT DWORDs. */
4020 for (i = 0; i < smpt_header->length; i++)
4021 smpt[i] = le32_to_cpu(smpt[i]);
4023 sector_map = spi_nor_get_map_in_use(nor, smpt, smpt_header->length);
4024 if (IS_ERR(sector_map)) {
4025 ret = PTR_ERR(sector_map);
4029 ret = spi_nor_init_non_uniform_erase_map(nor, params, sector_map);
4033 spi_nor_regions_sort_erase_types(¶ms->erase_map);
4040 #define SFDP_4BAIT_DWORD_MAX 2
4043 /* The hardware capability. */
4047 * The <supported_bit> bit in DWORD1 of the 4BAIT tells us whether
4048 * the associated 4-byte address op code is supported.
4054 * spi_nor_parse_4bait() - parse the 4-Byte Address Instruction Table
4055 * @nor: pointer to a 'struct spi_nor'.
4056 * @param_header: pointer to the 'struct sfdp_parameter_header' describing
4057 * the 4-Byte Address Instruction Table length and version.
4058 * @params: pointer to the 'struct spi_nor_flash_parameter' to be.
4060 * Return: 0 on success, -errno otherwise.
4062 static int spi_nor_parse_4bait(struct spi_nor *nor,
4063 const struct sfdp_parameter_header *param_header,
4064 struct spi_nor_flash_parameter *params)
4066 static const struct sfdp_4bait reads[] = {
4067 { SNOR_HWCAPS_READ, BIT(0) },
4068 { SNOR_HWCAPS_READ_FAST, BIT(1) },
4069 { SNOR_HWCAPS_READ_1_1_2, BIT(2) },
4070 { SNOR_HWCAPS_READ_1_2_2, BIT(3) },
4071 { SNOR_HWCAPS_READ_1_1_4, BIT(4) },
4072 { SNOR_HWCAPS_READ_1_4_4, BIT(5) },
4073 { SNOR_HWCAPS_READ_1_1_1_DTR, BIT(13) },
4074 { SNOR_HWCAPS_READ_1_2_2_DTR, BIT(14) },
4075 { SNOR_HWCAPS_READ_1_4_4_DTR, BIT(15) },
4077 static const struct sfdp_4bait programs[] = {
4078 { SNOR_HWCAPS_PP, BIT(6) },
4079 { SNOR_HWCAPS_PP_1_1_4, BIT(7) },
4080 { SNOR_HWCAPS_PP_1_4_4, BIT(8) },
4082 static const struct sfdp_4bait erases[SNOR_ERASE_TYPE_MAX] = {
4083 { 0u /* not used */, BIT(9) },
4084 { 0u /* not used */, BIT(10) },
4085 { 0u /* not used */, BIT(11) },
4086 { 0u /* not used */, BIT(12) },
4088 struct spi_nor_pp_command *params_pp = params->page_programs;
4089 struct spi_nor_erase_map *map = ¶ms->erase_map;
4090 struct spi_nor_erase_type *erase_type = map->erase_type;
4093 u32 addr, discard_hwcaps, read_hwcaps, pp_hwcaps, erase_mask;
4096 if (param_header->major != SFDP_JESD216_MAJOR ||
4097 param_header->length < SFDP_4BAIT_DWORD_MAX)
4100 /* Read the 4-byte Address Instruction Table. */
4101 len = sizeof(*dwords) * SFDP_4BAIT_DWORD_MAX;
4103 /* Use a kmalloc'ed bounce buffer to guarantee it is DMA-able. */
4104 dwords = kmalloc(len, GFP_KERNEL);
4108 addr = SFDP_PARAM_HEADER_PTP(param_header);
4109 ret = spi_nor_read_sfdp(nor, addr, len, dwords);
4113 /* Fix endianness of the 4BAIT DWORDs. */
4114 for (i = 0; i < SFDP_4BAIT_DWORD_MAX; i++)
4115 dwords[i] = le32_to_cpu(dwords[i]);
4118 * Compute the subset of (Fast) Read commands for which the 4-byte
4119 * version is supported.
4123 for (i = 0; i < ARRAY_SIZE(reads); i++) {
4124 const struct sfdp_4bait *read = &reads[i];
4126 discard_hwcaps |= read->hwcaps;
4127 if ((params->hwcaps.mask & read->hwcaps) &&
4128 (dwords[0] & read->supported_bit))
4129 read_hwcaps |= read->hwcaps;
4133 * Compute the subset of Page Program commands for which the 4-byte
4134 * version is supported.
4137 for (i = 0; i < ARRAY_SIZE(programs); i++) {
4138 const struct sfdp_4bait *program = &programs[i];
4141 * The 4 Byte Address Instruction (Optional) Table is the only
4142 * SFDP table that indicates support for Page Program Commands.
4143 * Bypass the params->hwcaps.mask and consider 4BAIT the biggest
4144 * authority for specifying Page Program support.
4146 discard_hwcaps |= program->hwcaps;
4147 if (dwords[0] & program->supported_bit)
4148 pp_hwcaps |= program->hwcaps;
4152 * Compute the subset of Sector Erase commands for which the 4-byte
4153 * version is supported.
4156 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
4157 const struct sfdp_4bait *erase = &erases[i];
4159 if (dwords[0] & erase->supported_bit)
4160 erase_mask |= BIT(i);
4163 /* Replicate the sort done for the map's erase types in BFPT. */
4164 erase_mask = spi_nor_sort_erase_mask(map, erase_mask);
4167 * We need at least one 4-byte op code per read, program and erase
4168 * operation; the .read(), .write() and .erase() hooks share the
4169 * nor->addr_width value.
4171 if (!read_hwcaps || !pp_hwcaps || !erase_mask)
4175 * Discard all operations from the 4-byte instruction set which are
4176 * not supported by this memory.
4178 params->hwcaps.mask &= ~discard_hwcaps;
4179 params->hwcaps.mask |= (read_hwcaps | pp_hwcaps);
4181 /* Use the 4-byte address instruction set. */
4182 for (i = 0; i < SNOR_CMD_READ_MAX; i++) {
4183 struct spi_nor_read_command *read_cmd = ¶ms->reads[i];
4185 read_cmd->opcode = spi_nor_convert_3to4_read(read_cmd->opcode);
4188 /* 4BAIT is the only SFDP table that indicates page program support. */
4189 if (pp_hwcaps & SNOR_HWCAPS_PP)
4190 spi_nor_set_pp_settings(¶ms_pp[SNOR_CMD_PP],
4191 SPINOR_OP_PP_4B, SNOR_PROTO_1_1_1);
4192 if (pp_hwcaps & SNOR_HWCAPS_PP_1_1_4)
4193 spi_nor_set_pp_settings(¶ms_pp[SNOR_CMD_PP_1_1_4],
4194 SPINOR_OP_PP_1_1_4_4B,
4196 if (pp_hwcaps & SNOR_HWCAPS_PP_1_4_4)
4197 spi_nor_set_pp_settings(¶ms_pp[SNOR_CMD_PP_1_4_4],
4198 SPINOR_OP_PP_1_4_4_4B,
4201 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
4202 if (erase_mask & BIT(i))
4203 erase_type[i].opcode = (dwords[1] >>
4204 erase_type[i].idx * 8) & 0xFF;
4206 spi_nor_set_erase_type(&erase_type[i], 0u, 0xFF);
4210 * We set SNOR_F_HAS_4BAIT in order to skip spi_nor_set_4byte_opcodes()
4211 * later because we already did the conversion to 4byte opcodes. Also,
4212 * this latest function implements a legacy quirk for the erase size of
4213 * Spansion memory. However this quirk is no longer needed with new
4214 * SFDP compliant memories.
4216 nor->addr_width = 4;
4217 nor->flags |= SNOR_F_4B_OPCODES | SNOR_F_HAS_4BAIT;
4226 * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
4227 * @nor: pointer to a 'struct spi_nor'
4228 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
4231 * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
4232 * specification. This is a standard which tends to supported by almost all
4233 * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
4234 * runtime the main parameters needed to perform basic SPI flash operations such
4235 * as Fast Read, Page Program or Sector Erase commands.
4237 * Return: 0 on success, -errno otherwise.
4239 static int spi_nor_parse_sfdp(struct spi_nor *nor,
4240 struct spi_nor_flash_parameter *params)
4242 const struct sfdp_parameter_header *param_header, *bfpt_header;
4243 struct sfdp_parameter_header *param_headers = NULL;
4244 struct sfdp_header header;
4245 struct device *dev = nor->dev;
4249 /* Get the SFDP header. */
4250 err = spi_nor_read_sfdp_dma_unsafe(nor, 0, sizeof(header), &header);
4254 /* Check the SFDP header version. */
4255 if (le32_to_cpu(header.signature) != SFDP_SIGNATURE ||
4256 header.major != SFDP_JESD216_MAJOR)
4260 * Verify that the first and only mandatory parameter header is a
4261 * Basic Flash Parameter Table header as specified in JESD216.
4263 bfpt_header = &header.bfpt_header;
4264 if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID ||
4265 bfpt_header->major != SFDP_JESD216_MAJOR)
4269 * Allocate memory then read all parameter headers with a single
4270 * Read SFDP command. These parameter headers will actually be parsed
4271 * twice: a first time to get the latest revision of the basic flash
4272 * parameter table, then a second time to handle the supported optional
4274 * Hence we read the parameter headers once for all to reduce the
4275 * processing time. Also we use kmalloc() instead of devm_kmalloc()
4276 * because we don't need to keep these parameter headers: the allocated
4277 * memory is always released with kfree() before exiting this function.
4280 psize = header.nph * sizeof(*param_headers);
4282 param_headers = kmalloc(psize, GFP_KERNEL);
4286 err = spi_nor_read_sfdp(nor, sizeof(header),
4287 psize, param_headers);
4289 dev_dbg(dev, "failed to read SFDP parameter headers\n");
4295 * Check other parameter headers to get the latest revision of
4296 * the basic flash parameter table.
4298 for (i = 0; i < header.nph; i++) {
4299 param_header = ¶m_headers[i];
4301 if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID &&
4302 param_header->major == SFDP_JESD216_MAJOR &&
4303 (param_header->minor > bfpt_header->minor ||
4304 (param_header->minor == bfpt_header->minor &&
4305 param_header->length > bfpt_header->length)))
4306 bfpt_header = param_header;
4309 err = spi_nor_parse_bfpt(nor, bfpt_header, params);
4313 /* Parse optional parameter tables. */
4314 for (i = 0; i < header.nph; i++) {
4315 param_header = ¶m_headers[i];
4317 switch (SFDP_PARAM_HEADER_ID(param_header)) {
4318 case SFDP_SECTOR_MAP_ID:
4319 err = spi_nor_parse_smpt(nor, param_header, params);
4323 err = spi_nor_parse_4bait(nor, param_header, params);
4331 dev_warn(dev, "Failed to parse optional parameter table: %04x\n",
4332 SFDP_PARAM_HEADER_ID(param_header));
4334 * Let's not drop all information we extracted so far
4335 * if optional table parsers fail. In case of failing,
4336 * each optional parser is responsible to roll back to
4337 * the previously known spi_nor data.
4344 kfree(param_headers);
4348 static int spi_nor_select_read(struct spi_nor *nor,
4351 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
4352 const struct spi_nor_read_command *read;
4357 cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
4361 read = &nor->params.reads[cmd];
4362 nor->read_opcode = read->opcode;
4363 nor->read_proto = read->proto;
4366 * In the spi-nor framework, we don't need to make the difference
4367 * between mode clock cycles and wait state clock cycles.
4368 * Indeed, the value of the mode clock cycles is used by a QSPI
4369 * flash memory to know whether it should enter or leave its 0-4-4
4370 * (Continuous Read / XIP) mode.
4371 * eXecution In Place is out of the scope of the mtd sub-system.
4372 * Hence we choose to merge both mode and wait state clock cycles
4373 * into the so called dummy clock cycles.
4375 nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
4379 static int spi_nor_select_pp(struct spi_nor *nor,
4382 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
4383 const struct spi_nor_pp_command *pp;
4388 cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
4392 pp = &nor->params.page_programs[cmd];
4393 nor->program_opcode = pp->opcode;
4394 nor->write_proto = pp->proto;
4399 * spi_nor_select_uniform_erase() - select optimum uniform erase type
4400 * @map: the erase map of the SPI NOR
4401 * @wanted_size: the erase type size to search for. Contains the value of
4402 * info->sector_size or of the "small sector" size in case
4403 * CONFIG_MTD_SPI_NOR_USE_4K_SECTORS is defined.
4405 * Once the optimum uniform sector erase command is found, disable all the
4408 * Return: pointer to erase type on success, NULL otherwise.
4410 static const struct spi_nor_erase_type *
4411 spi_nor_select_uniform_erase(struct spi_nor_erase_map *map,
4412 const u32 wanted_size)
4414 const struct spi_nor_erase_type *tested_erase, *erase = NULL;
4416 u8 uniform_erase_type = map->uniform_erase_type;
4418 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
4419 if (!(uniform_erase_type & BIT(i)))
4422 tested_erase = &map->erase_type[i];
4425 * If the current erase size is the one, stop here:
4426 * we have found the right uniform Sector Erase command.
4428 if (tested_erase->size == wanted_size) {
4429 erase = tested_erase;
4434 * Otherwise, the current erase size is still a valid canditate.
4435 * Select the biggest valid candidate.
4437 if (!erase && tested_erase->size)
4438 erase = tested_erase;
4439 /* keep iterating to find the wanted_size */
4445 /* Disable all other Sector Erase commands. */
4446 map->uniform_erase_type &= ~SNOR_ERASE_TYPE_MASK;
4447 map->uniform_erase_type |= BIT(erase - map->erase_type);
4451 static int spi_nor_select_erase(struct spi_nor *nor)
4453 struct spi_nor_erase_map *map = &nor->params.erase_map;
4454 const struct spi_nor_erase_type *erase = NULL;
4455 struct mtd_info *mtd = &nor->mtd;
4456 u32 wanted_size = nor->info->sector_size;
4460 * The previous implementation handling Sector Erase commands assumed
4461 * that the SPI flash memory has an uniform layout then used only one
4462 * of the supported erase sizes for all Sector Erase commands.
4463 * So to be backward compatible, the new implementation also tries to
4464 * manage the SPI flash memory as uniform with a single erase sector
4465 * size, when possible.
4467 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
4468 /* prefer "small sector" erase if possible */
4469 wanted_size = 4096u;
4472 if (spi_nor_has_uniform_erase(nor)) {
4473 erase = spi_nor_select_uniform_erase(map, wanted_size);
4476 nor->erase_opcode = erase->opcode;
4477 mtd->erasesize = erase->size;
4482 * For non-uniform SPI flash memory, set mtd->erasesize to the
4483 * maximum erase sector size. No need to set nor->erase_opcode.
4485 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
4486 if (map->erase_type[i].size) {
4487 erase = &map->erase_type[i];
4495 mtd->erasesize = erase->size;
4499 static int spi_nor_default_setup(struct spi_nor *nor,
4500 const struct spi_nor_hwcaps *hwcaps)
4502 struct spi_nor_flash_parameter *params = &nor->params;
4503 u32 ignored_mask, shared_mask;
4507 * Keep only the hardware capabilities supported by both the SPI
4508 * controller and the SPI flash memory.
4510 shared_mask = hwcaps->mask & params->hwcaps.mask;
4514 * When called from spi_nor_probe(), all caps are set and we
4515 * need to discard some of them based on what the SPI
4516 * controller actually supports (using spi_mem_supports_op()).
4518 spi_nor_spimem_adjust_hwcaps(nor, &shared_mask);
4521 * SPI n-n-n protocols are not supported when the SPI
4522 * controller directly implements the spi_nor interface.
4523 * Yet another reason to switch to spi-mem.
4525 ignored_mask = SNOR_HWCAPS_X_X_X;
4526 if (shared_mask & ignored_mask) {
4528 "SPI n-n-n protocols are not supported.\n");
4529 shared_mask &= ~ignored_mask;
4533 /* Select the (Fast) Read command. */
4534 err = spi_nor_select_read(nor, shared_mask);
4537 "can't select read settings supported by both the SPI controller and memory.\n");
4541 /* Select the Page Program command. */
4542 err = spi_nor_select_pp(nor, shared_mask);
4545 "can't select write settings supported by both the SPI controller and memory.\n");
4549 /* Select the Sector Erase command. */
4550 err = spi_nor_select_erase(nor);
4553 "can't select erase settings supported by both the SPI controller and memory.\n");
4560 static int spi_nor_setup(struct spi_nor *nor,
4561 const struct spi_nor_hwcaps *hwcaps)
4563 if (!nor->params.setup)
4566 return nor->params.setup(nor, hwcaps);
4569 static void atmel_set_default_init(struct spi_nor *nor)
4571 nor->flags |= SNOR_F_HAS_LOCK;
4574 static void intel_set_default_init(struct spi_nor *nor)
4576 nor->flags |= SNOR_F_HAS_LOCK;
4579 static void macronix_set_default_init(struct spi_nor *nor)
4581 nor->params.quad_enable = macronix_quad_enable;
4582 nor->params.set_4byte = macronix_set_4byte;
4585 static void sst_set_default_init(struct spi_nor *nor)
4587 nor->flags |= SNOR_F_HAS_LOCK;
4590 static void st_micron_set_default_init(struct spi_nor *nor)
4592 nor->flags |= SNOR_F_HAS_LOCK;
4593 nor->params.quad_enable = NULL;
4594 nor->params.set_4byte = st_micron_set_4byte;
4597 static void winbond_set_default_init(struct spi_nor *nor)
4599 nor->params.set_4byte = winbond_set_4byte;
4603 * spi_nor_manufacturer_init_params() - Initialize the flash's parameters and
4604 * settings based on MFR register and ->default_init() hook.
4605 * @nor: pointer to a 'struct spi-nor'.
4607 static void spi_nor_manufacturer_init_params(struct spi_nor *nor)
4609 /* Init flash parameters based on MFR */
4610 switch (JEDEC_MFR(nor->info)) {
4611 case SNOR_MFR_ATMEL:
4612 atmel_set_default_init(nor);
4615 case SNOR_MFR_INTEL:
4616 intel_set_default_init(nor);
4619 case SNOR_MFR_MACRONIX:
4620 macronix_set_default_init(nor);
4624 case SNOR_MFR_MICRON:
4625 st_micron_set_default_init(nor);
4629 sst_set_default_init(nor);
4632 case SNOR_MFR_WINBOND:
4633 winbond_set_default_init(nor);
4640 if (nor->info->fixups && nor->info->fixups->default_init)
4641 nor->info->fixups->default_init(nor);
4645 * spi_nor_sfdp_init_params() - Initialize the flash's parameters and settings
4646 * based on JESD216 SFDP standard.
4647 * @nor: pointer to a 'struct spi-nor'.
4649 * The method has a roll-back mechanism: in case the SFDP parsing fails, the
4650 * legacy flash parameters and settings will be restored.
4652 static void spi_nor_sfdp_init_params(struct spi_nor *nor)
4654 struct spi_nor_flash_parameter sfdp_params;
4656 memcpy(&sfdp_params, &nor->params, sizeof(sfdp_params));
4658 if (spi_nor_parse_sfdp(nor, &sfdp_params)) {
4659 nor->addr_width = 0;
4660 nor->flags &= ~SNOR_F_4B_OPCODES;
4662 memcpy(&nor->params, &sfdp_params, sizeof(nor->params));
4667 * spi_nor_info_init_params() - Initialize the flash's parameters and settings
4668 * based on nor->info data.
4669 * @nor: pointer to a 'struct spi-nor'.
4671 static void spi_nor_info_init_params(struct spi_nor *nor)
4673 struct spi_nor_flash_parameter *params = &nor->params;
4674 struct spi_nor_erase_map *map = ¶ms->erase_map;
4675 const struct flash_info *info = nor->info;
4676 struct device_node *np = spi_nor_get_flash_node(nor);
4679 /* Initialize legacy flash parameters and settings. */
4680 params->quad_enable = spansion_read_cr_quad_enable;
4681 params->set_4byte = spansion_set_4byte;
4682 params->setup = spi_nor_default_setup;
4683 /* Default to 16-bit Write Status (01h) Command */
4684 nor->flags |= SNOR_F_HAS_16BIT_SR;
4686 /* Set SPI NOR sizes. */
4687 params->size = (u64)info->sector_size * info->n_sectors;
4688 params->page_size = info->page_size;
4690 if (!(info->flags & SPI_NOR_NO_FR)) {
4691 /* Default to Fast Read for DT and non-DT platform devices. */
4692 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
4694 /* Mask out Fast Read if not requested at DT instantiation. */
4695 if (np && !of_property_read_bool(np, "m25p,fast-read"))
4696 params->hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
4699 /* (Fast) Read settings. */
4700 params->hwcaps.mask |= SNOR_HWCAPS_READ;
4701 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ],
4702 0, 0, SPINOR_OP_READ,
4705 if (params->hwcaps.mask & SNOR_HWCAPS_READ_FAST)
4706 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_FAST],
4707 0, 8, SPINOR_OP_READ_FAST,
4710 if (info->flags & SPI_NOR_DUAL_READ) {
4711 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
4712 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_2],
4713 0, 8, SPINOR_OP_READ_1_1_2,
4717 if (info->flags & SPI_NOR_QUAD_READ) {
4718 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
4719 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_4],
4720 0, 8, SPINOR_OP_READ_1_1_4,
4724 if (info->flags & SPI_NOR_OCTAL_READ) {
4725 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8;
4726 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_8],
4727 0, 8, SPINOR_OP_READ_1_1_8,
4731 /* Page Program settings. */
4732 params->hwcaps.mask |= SNOR_HWCAPS_PP;
4733 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP],
4734 SPINOR_OP_PP, SNOR_PROTO_1_1_1);
4737 * Sector Erase settings. Sort Erase Types in ascending order, with the
4738 * smallest erase size starting at BIT(0).
4742 if (info->flags & SECT_4K_PMC) {
4743 erase_mask |= BIT(i);
4744 spi_nor_set_erase_type(&map->erase_type[i], 4096u,
4745 SPINOR_OP_BE_4K_PMC);
4747 } else if (info->flags & SECT_4K) {
4748 erase_mask |= BIT(i);
4749 spi_nor_set_erase_type(&map->erase_type[i], 4096u,
4753 erase_mask |= BIT(i);
4754 spi_nor_set_erase_type(&map->erase_type[i], info->sector_size,
4756 spi_nor_init_uniform_erase_map(map, erase_mask, params->size);
4759 static void spansion_post_sfdp_fixups(struct spi_nor *nor)
4761 struct mtd_info *mtd = &nor->mtd;
4763 if (mtd->size <= SZ_16M)
4766 nor->flags |= SNOR_F_4B_OPCODES;
4767 /* No small sector erase for 4-byte command set */
4768 nor->erase_opcode = SPINOR_OP_SE;
4769 nor->mtd.erasesize = nor->info->sector_size;
4772 static void s3an_post_sfdp_fixups(struct spi_nor *nor)
4774 nor->params.setup = s3an_nor_setup;
4778 * spi_nor_post_sfdp_fixups() - Updates the flash's parameters and settings
4779 * after SFDP has been parsed (is also called for SPI NORs that do not
4781 * @nor: pointer to a 'struct spi_nor'
4783 * Typically used to tweak various parameters that could not be extracted by
4784 * other means (i.e. when information provided by the SFDP/flash_info tables
4785 * are incomplete or wrong).
4787 static void spi_nor_post_sfdp_fixups(struct spi_nor *nor)
4789 switch (JEDEC_MFR(nor->info)) {
4790 case SNOR_MFR_SPANSION:
4791 spansion_post_sfdp_fixups(nor);
4798 if (nor->info->flags & SPI_S3AN)
4799 s3an_post_sfdp_fixups(nor);
4801 if (nor->info->fixups && nor->info->fixups->post_sfdp)
4802 nor->info->fixups->post_sfdp(nor);
4806 * spi_nor_late_init_params() - Late initialization of default flash parameters.
4807 * @nor: pointer to a 'struct spi_nor'
4809 * Used to set default flash parameters and settings when the ->default_init()
4810 * hook or the SFDP parser let voids.
4812 static void spi_nor_late_init_params(struct spi_nor *nor)
4815 * NOR protection support. When locking_ops are not provided, we pick
4818 if (nor->flags & SNOR_F_HAS_LOCK && !nor->params.locking_ops)
4819 nor->params.locking_ops = &stm_locking_ops;
4823 * spi_nor_init_params() - Initialize the flash's parameters and settings.
4824 * @nor: pointer to a 'struct spi-nor'.
4826 * The flash parameters and settings are initialized based on a sequence of
4827 * calls that are ordered by priority:
4829 * 1/ Default flash parameters initialization. The initializations are done
4830 * based on nor->info data:
4831 * spi_nor_info_init_params()
4833 * which can be overwritten by:
4834 * 2/ Manufacturer flash parameters initialization. The initializations are
4835 * done based on MFR register, or when the decisions can not be done solely
4836 * based on MFR, by using specific flash_info tweeks, ->default_init():
4837 * spi_nor_manufacturer_init_params()
4839 * which can be overwritten by:
4840 * 3/ SFDP flash parameters initialization. JESD216 SFDP is a standard and
4841 * should be more accurate that the above.
4842 * spi_nor_sfdp_init_params()
4844 * Please note that there is a ->post_bfpt() fixup hook that can overwrite
4845 * the flash parameters and settings immediately after parsing the Basic
4846 * Flash Parameter Table.
4848 * which can be overwritten by:
4849 * 4/ Post SFDP flash parameters initialization. Used to tweak various
4850 * parameters that could not be extracted by other means (i.e. when
4851 * information provided by the SFDP/flash_info tables are incomplete or
4853 * spi_nor_post_sfdp_fixups()
4855 * 5/ Late default flash parameters initialization, used when the
4856 * ->default_init() hook or the SFDP parser do not set specific params.
4857 * spi_nor_late_init_params()
4859 static void spi_nor_init_params(struct spi_nor *nor)
4861 spi_nor_info_init_params(nor);
4863 spi_nor_manufacturer_init_params(nor);
4865 if ((nor->info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)) &&
4866 !(nor->info->flags & SPI_NOR_SKIP_SFDP))
4867 spi_nor_sfdp_init_params(nor);
4869 spi_nor_post_sfdp_fixups(nor);
4871 spi_nor_late_init_params(nor);
4875 * spi_nor_quad_enable() - enable Quad I/O if needed.
4876 * @nor: pointer to a 'struct spi_nor'
4878 * Return: 0 on success, -errno otherwise.
4880 static int spi_nor_quad_enable(struct spi_nor *nor)
4882 if (!nor->params.quad_enable)
4885 if (!(spi_nor_get_protocol_width(nor->read_proto) == 4 ||
4886 spi_nor_get_protocol_width(nor->write_proto) == 4))
4889 return nor->params.quad_enable(nor);
4893 * spi_nor_unlock_all() - Unlocks the entire flash memory array.
4894 * @nor: pointer to a 'struct spi_nor'.
4896 * Some SPI NOR flashes are write protected by default after a power-on reset
4897 * cycle, in order to avoid inadvertent writes during power-up. Backward
4898 * compatibility imposes to unlock the entire flash memory array at power-up
4901 static int spi_nor_unlock_all(struct spi_nor *nor)
4903 if (nor->flags & SNOR_F_HAS_LOCK)
4904 return spi_nor_unlock(&nor->mtd, 0, nor->params.size);
4909 static int spi_nor_init(struct spi_nor *nor)
4913 err = spi_nor_quad_enable(nor);
4915 dev_dbg(nor->dev, "quad mode not supported\n");
4919 err = spi_nor_unlock_all(nor);
4921 dev_dbg(nor->dev, "Failed to unlock the entire flash memory array\n");
4925 if (nor->addr_width == 4 && !(nor->flags & SNOR_F_4B_OPCODES)) {
4927 * If the RESET# pin isn't hooked up properly, or the system
4928 * otherwise doesn't perform a reset command in the boot
4929 * sequence, it's impossible to 100% protect against unexpected
4930 * reboots (e.g., crashes). Warn the user (or hopefully, system
4931 * designer) that this is bad.
4933 WARN_ONCE(nor->flags & SNOR_F_BROKEN_RESET,
4934 "enabling reset hack; may not recover from unexpected reboots\n");
4935 nor->params.set_4byte(nor, true);
4941 /* mtd resume handler */
4942 static void spi_nor_resume(struct mtd_info *mtd)
4944 struct spi_nor *nor = mtd_to_spi_nor(mtd);
4945 struct device *dev = nor->dev;
4948 /* re-initialize the nor chip */
4949 ret = spi_nor_init(nor);
4951 dev_err(dev, "resume() failed\n");
4954 void spi_nor_restore(struct spi_nor *nor)
4956 /* restore the addressing mode */
4957 if (nor->addr_width == 4 && !(nor->flags & SNOR_F_4B_OPCODES) &&
4958 nor->flags & SNOR_F_BROKEN_RESET)
4959 nor->params.set_4byte(nor, false);
4961 EXPORT_SYMBOL_GPL(spi_nor_restore);
4963 static const struct flash_info *spi_nor_match_id(const char *name)
4965 const struct flash_info *id = spi_nor_ids;
4968 if (!strcmp(name, id->name))
4975 static int spi_nor_set_addr_width(struct spi_nor *nor)
4977 if (nor->addr_width) {
4978 /* already configured from SFDP */
4979 } else if (nor->info->addr_width) {
4980 nor->addr_width = nor->info->addr_width;
4981 } else if (nor->mtd.size > 0x1000000) {
4982 /* enable 4-byte addressing if the device exceeds 16MiB */
4983 nor->addr_width = 4;
4985 nor->addr_width = 3;
4988 if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
4989 dev_dbg(nor->dev, "address width is too large: %u\n",
4994 /* Set 4byte opcodes when possible. */
4995 if (nor->addr_width == 4 && nor->flags & SNOR_F_4B_OPCODES &&
4996 !(nor->flags & SNOR_F_HAS_4BAIT))
4997 spi_nor_set_4byte_opcodes(nor);
5002 static void spi_nor_debugfs_init(struct spi_nor *nor,
5003 const struct flash_info *info)
5005 struct mtd_info *mtd = &nor->mtd;
5007 mtd->dbg.partname = info->name;
5008 mtd->dbg.partid = devm_kasprintf(nor->dev, GFP_KERNEL, "spi-nor:%*phN",
5009 info->id_len, info->id);
5012 static const struct flash_info *spi_nor_get_flash_info(struct spi_nor *nor,
5015 const struct flash_info *info = NULL;
5018 info = spi_nor_match_id(name);
5019 /* Try to auto-detect if chip name wasn't specified or not found */
5021 info = spi_nor_read_id(nor);
5022 if (IS_ERR_OR_NULL(info))
5023 return ERR_PTR(-ENOENT);
5026 * If caller has specified name of flash model that can normally be
5027 * detected using JEDEC, let's verify it.
5029 if (name && info->id_len) {
5030 const struct flash_info *jinfo;
5032 jinfo = spi_nor_read_id(nor);
5033 if (IS_ERR(jinfo)) {
5035 } else if (jinfo != info) {
5037 * JEDEC knows better, so overwrite platform ID. We
5038 * can't trust partitions any longer, but we'll let
5039 * mtd apply them anyway, since some partitions may be
5040 * marked read-only, and we don't want to lose that
5041 * information, even if it's not 100% accurate.
5043 dev_warn(nor->dev, "found %s, expected %s\n",
5044 jinfo->name, info->name);
5052 int spi_nor_scan(struct spi_nor *nor, const char *name,
5053 const struct spi_nor_hwcaps *hwcaps)
5055 const struct flash_info *info;
5056 struct device *dev = nor->dev;
5057 struct mtd_info *mtd = &nor->mtd;
5058 struct device_node *np = spi_nor_get_flash_node(nor);
5059 struct spi_nor_flash_parameter *params = &nor->params;
5063 ret = spi_nor_check(nor);
5067 /* Reset SPI protocol for all commands. */
5068 nor->reg_proto = SNOR_PROTO_1_1_1;
5069 nor->read_proto = SNOR_PROTO_1_1_1;
5070 nor->write_proto = SNOR_PROTO_1_1_1;
5073 * We need the bounce buffer early to read/write registers when going
5074 * through the spi-mem layer (buffers have to be DMA-able).
5075 * For spi-mem drivers, we'll reallocate a new buffer if
5076 * nor->page_size turns out to be greater than PAGE_SIZE (which
5077 * shouldn't happen before long since NOR pages are usually less
5078 * than 1KB) after spi_nor_scan() returns.
5080 nor->bouncebuf_size = PAGE_SIZE;
5081 nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size,
5083 if (!nor->bouncebuf)
5086 info = spi_nor_get_flash_info(nor, name);
5088 return PTR_ERR(info);
5092 spi_nor_debugfs_init(nor, info);
5094 mutex_init(&nor->lock);
5097 * Make sure the XSR_RDY flag is set before calling
5098 * spi_nor_wait_till_ready(). Xilinx S3AN share MFR
5099 * with Atmel spi-nor
5101 if (info->flags & SPI_NOR_XSR_RDY)
5102 nor->flags |= SNOR_F_READY_XSR_RDY;
5104 if (info->flags & SPI_NOR_HAS_LOCK)
5105 nor->flags |= SNOR_F_HAS_LOCK;
5107 /* Init flash parameters based on flash_info struct and SFDP */
5108 spi_nor_init_params(nor);
5111 mtd->name = dev_name(dev);
5113 mtd->type = MTD_NORFLASH;
5115 mtd->flags = MTD_CAP_NORFLASH;
5116 mtd->size = params->size;
5117 mtd->_erase = spi_nor_erase;
5118 mtd->_read = spi_nor_read;
5119 mtd->_resume = spi_nor_resume;
5121 if (nor->params.locking_ops) {
5122 mtd->_lock = spi_nor_lock;
5123 mtd->_unlock = spi_nor_unlock;
5124 mtd->_is_locked = spi_nor_is_locked;
5127 /* sst nor chips use AAI word program */
5128 if (info->flags & SST_WRITE)
5129 mtd->_write = sst_write;
5131 mtd->_write = spi_nor_write;
5133 if (info->flags & USE_FSR)
5134 nor->flags |= SNOR_F_USE_FSR;
5135 if (info->flags & SPI_NOR_HAS_TB)
5136 nor->flags |= SNOR_F_HAS_SR_TB;
5137 if (info->flags & NO_CHIP_ERASE)
5138 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
5139 if (info->flags & USE_CLSR)
5140 nor->flags |= SNOR_F_USE_CLSR;
5142 if (info->flags & SPI_NOR_NO_ERASE)
5143 mtd->flags |= MTD_NO_ERASE;
5145 mtd->dev.parent = dev;
5146 nor->page_size = params->page_size;
5147 mtd->writebufsize = nor->page_size;
5149 if (of_property_read_bool(np, "broken-flash-reset"))
5150 nor->flags |= SNOR_F_BROKEN_RESET;
5153 * Configure the SPI memory:
5154 * - select op codes for (Fast) Read, Page Program and Sector Erase.
5155 * - set the number of dummy cycles (mode cycles + wait states).
5156 * - set the SPI protocols for register and memory accesses.
5158 ret = spi_nor_setup(nor, hwcaps);
5162 if (info->flags & SPI_NOR_4B_OPCODES)
5163 nor->flags |= SNOR_F_4B_OPCODES;
5165 ret = spi_nor_set_addr_width(nor);
5169 /* Send all the required SPI flash commands to initialize device */
5170 ret = spi_nor_init(nor);
5174 dev_info(dev, "%s (%lld Kbytes)\n", info->name,
5175 (long long)mtd->size >> 10);
5178 "mtd .name = %s, .size = 0x%llx (%lldMiB), "
5179 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
5180 mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
5181 mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
5183 if (mtd->numeraseregions)
5184 for (i = 0; i < mtd->numeraseregions; i++)
5186 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
5187 ".erasesize = 0x%.8x (%uKiB), "
5188 ".numblocks = %d }\n",
5189 i, (long long)mtd->eraseregions[i].offset,
5190 mtd->eraseregions[i].erasesize,
5191 mtd->eraseregions[i].erasesize / 1024,
5192 mtd->eraseregions[i].numblocks);
5195 EXPORT_SYMBOL_GPL(spi_nor_scan);
5197 static int spi_nor_probe(struct spi_mem *spimem)
5199 struct spi_device *spi = spimem->spi;
5200 struct flash_platform_data *data = dev_get_platdata(&spi->dev);
5201 struct spi_nor *nor;
5203 * Enable all caps by default. The core will mask them after
5204 * checking what's really supported using spi_mem_supports_op().
5206 const struct spi_nor_hwcaps hwcaps = { .mask = SNOR_HWCAPS_ALL };
5210 nor = devm_kzalloc(&spi->dev, sizeof(*nor), GFP_KERNEL);
5214 nor->spimem = spimem;
5215 nor->dev = &spi->dev;
5216 spi_nor_set_flash_node(nor, spi->dev.of_node);
5218 spi_mem_set_drvdata(spimem, nor);
5220 if (data && data->name)
5221 nor->mtd.name = data->name;
5224 nor->mtd.name = spi_mem_get_name(spimem);
5227 * For some (historical?) reason many platforms provide two different
5228 * names in flash_platform_data: "name" and "type". Quite often name is
5229 * set to "m25p80" and then "type" provides a real chip name.
5230 * If that's the case, respect "type" and ignore a "name".
5232 if (data && data->type)
5233 flash_name = data->type;
5234 else if (!strcmp(spi->modalias, "spi-nor"))
5235 flash_name = NULL; /* auto-detect */
5237 flash_name = spi->modalias;
5239 ret = spi_nor_scan(nor, flash_name, &hwcaps);
5244 * None of the existing parts have > 512B pages, but let's play safe
5245 * and add this logic so that if anyone ever adds support for such
5246 * a NOR we don't end up with buffer overflows.
5248 if (nor->page_size > PAGE_SIZE) {
5249 nor->bouncebuf_size = nor->page_size;
5250 devm_kfree(nor->dev, nor->bouncebuf);
5251 nor->bouncebuf = devm_kmalloc(nor->dev,
5252 nor->bouncebuf_size,
5254 if (!nor->bouncebuf)
5258 return mtd_device_register(&nor->mtd, data ? data->parts : NULL,
5259 data ? data->nr_parts : 0);
5262 static int spi_nor_remove(struct spi_mem *spimem)
5264 struct spi_nor *nor = spi_mem_get_drvdata(spimem);
5266 spi_nor_restore(nor);
5268 /* Clean up MTD stuff. */
5269 return mtd_device_unregister(&nor->mtd);
5272 static void spi_nor_shutdown(struct spi_mem *spimem)
5274 struct spi_nor *nor = spi_mem_get_drvdata(spimem);
5276 spi_nor_restore(nor);
5280 * Do NOT add to this array without reading the following:
5282 * Historically, many flash devices are bound to this driver by their name. But
5283 * since most of these flash are compatible to some extent, and their
5284 * differences can often be differentiated by the JEDEC read-ID command, we
5285 * encourage new users to add support to the spi-nor library, and simply bind
5286 * against a generic string here (e.g., "jedec,spi-nor").
5288 * Many flash names are kept here in this list (as well as in spi-nor.c) to
5289 * keep them available as module aliases for existing platforms.
5291 static const struct spi_device_id spi_nor_dev_ids[] = {
5293 * Allow non-DT platform devices to bind to the "spi-nor" modalias, and
5294 * hack around the fact that the SPI core does not provide uevent
5295 * matching for .of_match_table
5300 * Entries not used in DTs that should be safe to drop after replacing
5301 * them with "spi-nor" in platform data.
5303 {"s25sl064a"}, {"w25x16"}, {"m25p10"}, {"m25px64"},
5306 * Entries that were used in DTs without "jedec,spi-nor" fallback and
5307 * should be kept for backward compatibility.
5309 {"at25df321a"}, {"at25df641"}, {"at26df081a"},
5310 {"mx25l4005a"}, {"mx25l1606e"}, {"mx25l6405d"}, {"mx25l12805d"},
5311 {"mx25l25635e"},{"mx66l51235l"},
5312 {"n25q064"}, {"n25q128a11"}, {"n25q128a13"}, {"n25q512a"},
5313 {"s25fl256s1"}, {"s25fl512s"}, {"s25sl12801"}, {"s25fl008k"},
5315 {"sst25vf040b"},{"sst25vf016b"},{"sst25vf032b"},{"sst25wf040"},
5316 {"m25p40"}, {"m25p80"}, {"m25p16"}, {"m25p32"},
5317 {"m25p64"}, {"m25p128"},
5318 {"w25x80"}, {"w25x32"}, {"w25q32"}, {"w25q32dw"},
5319 {"w25q80bl"}, {"w25q128"}, {"w25q256"},
5321 /* Flashes that can't be detected using JEDEC */
5322 {"m25p05-nonjedec"}, {"m25p10-nonjedec"}, {"m25p20-nonjedec"},
5323 {"m25p40-nonjedec"}, {"m25p80-nonjedec"}, {"m25p16-nonjedec"},
5324 {"m25p32-nonjedec"}, {"m25p64-nonjedec"}, {"m25p128-nonjedec"},
5326 /* Everspin MRAMs (non-JEDEC) */
5327 { "mr25h128" }, /* 128 Kib, 40 MHz */
5328 { "mr25h256" }, /* 256 Kib, 40 MHz */
5329 { "mr25h10" }, /* 1 Mib, 40 MHz */
5330 { "mr25h40" }, /* 4 Mib, 40 MHz */
5334 MODULE_DEVICE_TABLE(spi, spi_nor_dev_ids);
5336 static const struct of_device_id spi_nor_of_table[] = {
5338 * Generic compatibility for SPI NOR that can be identified by the
5339 * JEDEC READ ID opcode (0x9F). Use this, if possible.
5341 { .compatible = "jedec,spi-nor" },
5344 MODULE_DEVICE_TABLE(of, spi_nor_of_table);
5347 * REVISIT: many of these chips have deep power-down modes, which
5348 * should clearly be entered on suspend() to minimize power use.
5349 * And also when they're otherwise idle...
5351 static struct spi_mem_driver spi_nor_driver = {
5355 .of_match_table = spi_nor_of_table,
5357 .id_table = spi_nor_dev_ids,
5359 .probe = spi_nor_probe,
5360 .remove = spi_nor_remove,
5361 .shutdown = spi_nor_shutdown,
5363 module_spi_mem_driver(spi_nor_driver);
5365 MODULE_LICENSE("GPL v2");
5366 MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
5367 MODULE_AUTHOR("Mike Lavender");
5368 MODULE_DESCRIPTION("framework for SPI NOR");