1 // SPDX-License-Identifier: GPL-2.0
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.
9 * Synced from Linux v4.19
16 #include <dm/device_compat.h>
17 #include <dm/devres.h>
18 #include <linux/bitops.h>
19 #include <linux/err.h>
20 #include <linux/errno.h>
21 #include <linux/log2.h>
22 #include <linux/math64.h>
23 #include <linux/sizes.h>
24 #include <linux/bitfield.h>
25 #include <linux/delay.h>
27 #include <linux/mtd/mtd.h>
28 #include <linux/mtd/spi-nor.h>
32 #include "sf_internal.h"
34 /* Define max times to check status register before we give up. */
37 * For everything but full-chip erase; probably could be much smaller, but kept
38 * around for safety for now
41 #define HZ CONFIG_SYS_HZ
43 #define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ)
45 #define ROUND_UP_TO(x, y) (((x) + (y) - 1) / (y) * (y))
47 struct sfdp_parameter_header {
51 u8 length; /* in double words */
52 u8 parameter_table_pointer[3]; /* byte address */
56 #define SFDP_PARAM_HEADER_ID(p) (((p)->id_msb << 8) | (p)->id_lsb)
57 #define SFDP_PARAM_HEADER_PTP(p) \
58 (((p)->parameter_table_pointer[2] << 16) | \
59 ((p)->parameter_table_pointer[1] << 8) | \
60 ((p)->parameter_table_pointer[0] << 0))
62 #define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
63 #define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
64 #define SFDP_SST_ID 0x01bf /* Manufacturer specific Table */
65 #define SFDP_PROFILE1_ID 0xff05 /* xSPI Profile 1.0 Table */
67 #define SFDP_SIGNATURE 0x50444653U
68 #define SFDP_JESD216_MAJOR 1
69 #define SFDP_JESD216_MINOR 0
70 #define SFDP_JESD216A_MINOR 5
71 #define SFDP_JESD216B_MINOR 6
74 u32 signature; /* Ox50444653U <=> "SFDP" */
77 u8 nph; /* 0-base number of parameter headers */
80 /* Basic Flash Parameter Table. */
81 struct sfdp_parameter_header bfpt_header;
84 /* Basic Flash Parameter Table */
87 * JESD216 rev D defines a Basic Flash Parameter Table of 20 DWORDs.
88 * They are indexed from 1 but C arrays are indexed from 0.
90 #define BFPT_DWORD(i) ((i) - 1)
91 #define BFPT_DWORD_MAX 20
93 /* The first version of JESB216 defined only 9 DWORDs. */
94 #define BFPT_DWORD_MAX_JESD216 9
95 #define BFPT_DWORD_MAX_JESD216B 16
98 #define BFPT_DWORD1_FAST_READ_1_1_2 BIT(16)
99 #define BFPT_DWORD1_ADDRESS_BYTES_MASK GENMASK(18, 17)
100 #define BFPT_DWORD1_ADDRESS_BYTES_3_ONLY (0x0UL << 17)
101 #define BFPT_DWORD1_ADDRESS_BYTES_3_OR_4 (0x1UL << 17)
102 #define BFPT_DWORD1_ADDRESS_BYTES_4_ONLY (0x2UL << 17)
103 #define BFPT_DWORD1_DTR BIT(19)
104 #define BFPT_DWORD1_FAST_READ_1_2_2 BIT(20)
105 #define BFPT_DWORD1_FAST_READ_1_4_4 BIT(21)
106 #define BFPT_DWORD1_FAST_READ_1_1_4 BIT(22)
109 #define BFPT_DWORD5_FAST_READ_2_2_2 BIT(0)
110 #define BFPT_DWORD5_FAST_READ_4_4_4 BIT(4)
113 #define BFPT_DWORD11_PAGE_SIZE_SHIFT 4
114 #define BFPT_DWORD11_PAGE_SIZE_MASK GENMASK(7, 4)
119 * (from JESD216 rev B)
120 * Quad Enable Requirements (QER):
121 * - 000b: Device does not have a QE bit. Device detects 1-1-4 and 1-4-4
122 * reads based on instruction. DQ3/HOLD# functions are hold during
124 * - 001b: QE is bit 1 of status register 2. It is set via Write Status with
125 * two data bytes where bit 1 of the second byte is one.
127 * Writing only one byte to the status register has the side-effect of
128 * clearing status register 2, including the QE bit. The 100b code is
129 * used if writing one byte to the status register does not modify
131 * - 010b: QE is bit 6 of status register 1. It is set via Write Status with
132 * one data byte where bit 6 is one.
134 * - 011b: QE is bit 7 of status register 2. It is set via Write status
135 * register 2 instruction 3Eh with one data byte where bit 7 is one.
137 * The status register 2 is read using instruction 3Fh.
138 * - 100b: QE is bit 1 of status register 2. It is set via Write Status with
139 * two data bytes where bit 1 of the second byte is one.
141 * In contrast to the 001b code, writing one byte to the status
142 * register does not modify status register 2.
143 * - 101b: QE is bit 1 of status register 2. Status register 1 is read using
144 * Read Status instruction 05h. Status register2 is read using
145 * instruction 35h. QE is set via Writ Status instruction 01h with
146 * two data bytes where bit 1 of the second byte is one.
149 #define BFPT_DWORD15_QER_MASK GENMASK(22, 20)
150 #define BFPT_DWORD15_QER_NONE (0x0UL << 20) /* Micron */
151 #define BFPT_DWORD15_QER_SR2_BIT1_BUGGY (0x1UL << 20)
152 #define BFPT_DWORD15_QER_SR1_BIT6 (0x2UL << 20) /* Macronix */
153 #define BFPT_DWORD15_QER_SR2_BIT7 (0x3UL << 20)
154 #define BFPT_DWORD15_QER_SR2_BIT1_NO_RD (0x4UL << 20)
155 #define BFPT_DWORD15_QER_SR2_BIT1 (0x5UL << 20) /* Spansion */
157 #define BFPT_DWORD16_SOFT_RST BIT(12)
159 #define BFPT_DWORD18_CMD_EXT_MASK GENMASK(30, 29)
160 #define BFPT_DWORD18_CMD_EXT_REP (0x0UL << 29) /* Repeat */
161 #define BFPT_DWORD18_CMD_EXT_INV (0x1UL << 29) /* Invert */
162 #define BFPT_DWORD18_CMD_EXT_RES (0x2UL << 29) /* Reserved */
163 #define BFPT_DWORD18_CMD_EXT_16B (0x3UL << 29) /* 16-bit opcode */
165 /* xSPI Profile 1.0 table (from JESD216D.01). */
166 #define PROFILE1_DWORD1_RD_FAST_CMD GENMASK(15, 8)
167 #define PROFILE1_DWORD1_RDSR_DUMMY BIT(28)
168 #define PROFILE1_DWORD1_RDSR_ADDR_BYTES BIT(29)
169 #define PROFILE1_DWORD4_DUMMY_200MHZ GENMASK(11, 7)
170 #define PROFILE1_DWORD5_DUMMY_166MHZ GENMASK(31, 27)
171 #define PROFILE1_DWORD5_DUMMY_133MHZ GENMASK(21, 17)
172 #define PROFILE1_DWORD5_DUMMY_100MHZ GENMASK(11, 7)
173 #define PROFILE1_DUMMY_DEFAULT 20
176 u32 dwords[BFPT_DWORD_MAX];
180 * struct spi_nor_fixups - SPI NOR fixup hooks
181 * @default_init: called after default flash parameters init. Used to tweak
182 * flash parameters when information provided by the flash_info
183 * table is incomplete or wrong.
184 * @post_bfpt: called after the BFPT table has been parsed
185 * @post_sfdp: called after SFDP has been parsed (is also called for SPI NORs
186 * that do not support RDSFDP). Typically used to tweak various
187 * parameters that could not be extracted by other means (i.e.
188 * when information provided by the SFDP/flash_info tables are
189 * incomplete or wrong).
191 * Those hooks can be used to tweak the SPI NOR configuration when the SFDP
192 * table is broken or not available.
194 struct spi_nor_fixups {
195 void (*default_init)(struct spi_nor *nor);
196 int (*post_bfpt)(struct spi_nor *nor,
197 const struct sfdp_parameter_header *bfpt_header,
198 const struct sfdp_bfpt *bfpt,
199 struct spi_nor_flash_parameter *params);
200 void (*post_sfdp)(struct spi_nor *nor,
201 struct spi_nor_flash_parameter *params);
204 #define SPI_NOR_SRST_SLEEP_LEN 200
207 * spi_nor_get_cmd_ext() - Get the command opcode extension based on the
209 * @nor: pointer to a 'struct spi_nor'
210 * @op: pointer to the 'struct spi_mem_op' whose properties
211 * need to be initialized.
213 * Right now, only "repeat" and "invert" are supported.
215 * Return: The opcode extension.
217 static u8 spi_nor_get_cmd_ext(const struct spi_nor *nor,
218 const struct spi_mem_op *op)
220 switch (nor->cmd_ext_type) {
221 case SPI_NOR_EXT_INVERT:
222 return ~op->cmd.opcode;
224 case SPI_NOR_EXT_REPEAT:
225 return op->cmd.opcode;
228 dev_dbg(nor->dev, "Unknown command extension type\n");
234 * spi_nor_setup_op() - Set up common properties of a spi-mem op.
235 * @nor: pointer to a 'struct spi_nor'
236 * @op: pointer to the 'struct spi_mem_op' whose properties
237 * need to be initialized.
238 * @proto: the protocol from which the properties need to be set.
240 static void spi_nor_setup_op(const struct spi_nor *nor,
241 struct spi_mem_op *op,
242 const enum spi_nor_protocol proto)
246 op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(proto);
249 op->addr.buswidth = spi_nor_get_protocol_addr_nbits(proto);
251 if (op->dummy.nbytes)
252 op->dummy.buswidth = spi_nor_get_protocol_addr_nbits(proto);
255 op->data.buswidth = spi_nor_get_protocol_data_nbits(proto);
257 if (spi_nor_protocol_is_dtr(proto)) {
259 * spi-mem supports mixed DTR modes, but right now we can only
260 * have all phases either DTR or STR. IOW, spi-mem can have
261 * something like 4S-4D-4D, but spi-nor can't. So, set all 4
262 * phases to either DTR or STR.
264 op->cmd.dtr = op->addr.dtr = op->dummy.dtr =
267 /* 2 bytes per clock cycle in DTR mode. */
268 op->dummy.nbytes *= 2;
270 ext = spi_nor_get_cmd_ext(nor, op);
271 op->cmd.opcode = (op->cmd.opcode << 8) | ext;
276 static int spi_nor_read_write_reg(struct spi_nor *nor, struct spi_mem_op
279 if (op->data.dir == SPI_MEM_DATA_IN)
280 op->data.buf.in = buf;
282 op->data.buf.out = buf;
283 return spi_mem_exec_op(nor->spi, op);
286 static int spi_nor_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len)
288 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(code, 0),
291 SPI_MEM_OP_DATA_IN(len, NULL, 0));
294 spi_nor_setup_op(nor, &op, nor->reg_proto);
296 ret = spi_nor_read_write_reg(nor, &op, val);
298 dev_dbg(nor->dev, "error %d reading %x\n", ret, code);
303 static int spi_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
305 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 0),
308 SPI_MEM_OP_DATA_OUT(len, NULL, 0));
310 spi_nor_setup_op(nor, &op, nor->reg_proto);
313 op.data.dir = SPI_MEM_NO_DATA;
315 return spi_nor_read_write_reg(nor, &op, buf);
318 static ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
321 struct spi_mem_op op =
322 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0),
323 SPI_MEM_OP_ADDR(nor->addr_width, from, 0),
324 SPI_MEM_OP_DUMMY(nor->read_dummy, 0),
325 SPI_MEM_OP_DATA_IN(len, buf, 0));
326 size_t remaining = len;
329 spi_nor_setup_op(nor, &op, nor->read_proto);
331 /* convert the dummy cycles to the number of bytes */
332 op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
333 if (spi_nor_protocol_is_dtr(nor->read_proto))
334 op.dummy.nbytes *= 2;
337 op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
338 ret = spi_mem_adjust_op_size(nor->spi, &op);
342 ret = spi_mem_exec_op(nor->spi, &op);
346 op.addr.val += op.data.nbytes;
347 remaining -= op.data.nbytes;
348 op.data.buf.in += op.data.nbytes;
354 static ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
357 struct spi_mem_op op =
358 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0),
359 SPI_MEM_OP_ADDR(nor->addr_width, to, 0),
361 SPI_MEM_OP_DATA_OUT(len, buf, 0));
364 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
367 spi_nor_setup_op(nor, &op, nor->write_proto);
369 ret = spi_mem_adjust_op_size(nor->spi, &op);
372 op.data.nbytes = len < op.data.nbytes ? len : op.data.nbytes;
374 ret = spi_mem_exec_op(nor->spi, &op);
378 return op.data.nbytes;
382 * Read the status register, returning its value in the location
383 * Return the status register value.
384 * Returns negative if error occurred.
386 static int read_sr(struct spi_nor *nor)
388 struct spi_mem_op op;
391 u8 addr_nbytes, dummy;
393 if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) {
394 addr_nbytes = nor->rdsr_addr_nbytes;
395 dummy = nor->rdsr_dummy;
401 op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDSR, 0),
402 SPI_MEM_OP_ADDR(addr_nbytes, 0, 0),
403 SPI_MEM_OP_DUMMY(dummy, 0),
404 SPI_MEM_OP_DATA_IN(1, NULL, 0));
406 spi_nor_setup_op(nor, &op, nor->reg_proto);
409 * We don't want to read only one byte in DTR mode. So, read 2 and then
410 * discard the second byte.
412 if (spi_nor_protocol_is_dtr(nor->reg_proto))
415 ret = spi_nor_read_write_reg(nor, &op, val);
417 pr_debug("error %d reading SR\n", (int)ret);
425 * Read the flag status register, returning its value in the location
426 * Return the status register value.
427 * Returns negative if error occurred.
429 static int read_fsr(struct spi_nor *nor)
431 struct spi_mem_op op;
434 u8 addr_nbytes, dummy;
436 if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) {
437 addr_nbytes = nor->rdsr_addr_nbytes;
438 dummy = nor->rdsr_dummy;
444 op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDFSR, 0),
445 SPI_MEM_OP_ADDR(addr_nbytes, 0, 0),
446 SPI_MEM_OP_DUMMY(dummy, 0),
447 SPI_MEM_OP_DATA_IN(1, NULL, 0));
449 spi_nor_setup_op(nor, &op, nor->reg_proto);
452 * We don't want to read only one byte in DTR mode. So, read 2 and then
453 * discard the second byte.
455 if (spi_nor_protocol_is_dtr(nor->reg_proto))
458 ret = spi_nor_read_write_reg(nor, &op, val);
460 pr_debug("error %d reading FSR\n", ret);
468 * Read configuration register, returning its value in the
469 * location. Return the configuration register value.
470 * Returns negative if error occurred.
472 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
473 static int read_cr(struct spi_nor *nor)
478 ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
480 dev_dbg(nor->dev, "error %d reading CR\n", ret);
489 * Write status register 1 byte
490 * Returns negative if error occurred.
492 static int write_sr(struct spi_nor *nor, u8 val)
494 nor->cmd_buf[0] = val;
495 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
499 * Set write enable latch with Write Enable command.
500 * Returns negative if error occurred.
502 static int write_enable(struct spi_nor *nor)
504 return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
508 * Send write disable instruction to the chip.
510 static int write_disable(struct spi_nor *nor)
512 return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
515 static struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
520 #ifndef CONFIG_SPI_FLASH_BAR
521 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
525 for (i = 0; i < size; i++)
526 if (table[i][0] == opcode)
529 /* No conversion found, keep input op code. */
533 static u8 spi_nor_convert_3to4_read(u8 opcode)
535 static const u8 spi_nor_3to4_read[][2] = {
536 { SPINOR_OP_READ, SPINOR_OP_READ_4B },
537 { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B },
538 { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
539 { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
540 { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
541 { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
542 { SPINOR_OP_READ_1_1_8, SPINOR_OP_READ_1_1_8_4B },
543 { SPINOR_OP_READ_1_8_8, SPINOR_OP_READ_1_8_8_4B },
545 { SPINOR_OP_READ_1_1_1_DTR, SPINOR_OP_READ_1_1_1_DTR_4B },
546 { SPINOR_OP_READ_1_2_2_DTR, SPINOR_OP_READ_1_2_2_DTR_4B },
547 { SPINOR_OP_READ_1_4_4_DTR, SPINOR_OP_READ_1_4_4_DTR_4B },
550 return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
551 ARRAY_SIZE(spi_nor_3to4_read));
554 static u8 spi_nor_convert_3to4_program(u8 opcode)
556 static const u8 spi_nor_3to4_program[][2] = {
557 { SPINOR_OP_PP, SPINOR_OP_PP_4B },
558 { SPINOR_OP_PP_1_1_4, SPINOR_OP_PP_1_1_4_4B },
559 { SPINOR_OP_PP_1_4_4, SPINOR_OP_PP_1_4_4_4B },
560 { SPINOR_OP_PP_1_1_8, SPINOR_OP_PP_1_1_8_4B },
561 { SPINOR_OP_PP_1_8_8, SPINOR_OP_PP_1_8_8_4B },
564 return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
565 ARRAY_SIZE(spi_nor_3to4_program));
568 static u8 spi_nor_convert_3to4_erase(u8 opcode)
570 static const u8 spi_nor_3to4_erase[][2] = {
571 { SPINOR_OP_BE_4K, SPINOR_OP_BE_4K_4B },
572 { SPINOR_OP_BE_32K, SPINOR_OP_BE_32K_4B },
573 { SPINOR_OP_SE, SPINOR_OP_SE_4B },
576 return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
577 ARRAY_SIZE(spi_nor_3to4_erase));
580 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor,
581 const struct flash_info *info)
583 /* Do some manufacturer fixups first */
584 switch (JEDEC_MFR(info)) {
585 case SNOR_MFR_SPANSION:
586 /* No small sector erase for 4-byte command set */
587 nor->erase_opcode = SPINOR_OP_SE;
588 nor->mtd.erasesize = info->sector_size;
595 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
596 nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
597 nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
599 #endif /* !CONFIG_SPI_FLASH_BAR */
601 /* Enable/disable 4-byte addressing mode. */
602 static int set_4byte(struct spi_nor *nor, const struct flash_info *info,
606 bool need_wren = false;
609 switch (JEDEC_MFR(info)) {
611 case SNOR_MFR_MICRON:
612 /* Some Micron need WREN command; all will accept it */
615 case SNOR_MFR_MACRONIX:
616 case SNOR_MFR_WINBOND:
620 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
621 status = nor->write_reg(nor, cmd, NULL, 0);
625 if (!status && !enable &&
626 JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
628 * On Winbond W25Q256FV, leaving 4byte mode causes
629 * the Extended Address Register to be set to 1, so all
630 * 3-byte-address reads come from the second 16M.
631 * We must clear the register to enable normal behavior.
635 nor->write_reg(nor, SPINOR_OP_WREAR, nor->cmd_buf, 1);
642 nor->cmd_buf[0] = enable << 7;
643 return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
647 static int spi_nor_sr_ready(struct spi_nor *nor)
649 int sr = read_sr(nor);
654 if (nor->flags & SNOR_F_USE_CLSR && sr & (SR_E_ERR | SR_P_ERR)) {
656 dev_dbg(nor->dev, "Erase Error occurred\n");
658 dev_dbg(nor->dev, "Programming Error occurred\n");
660 nor->write_reg(nor, SPINOR_OP_CLSR, NULL, 0);
664 return !(sr & SR_WIP);
667 static int spi_nor_fsr_ready(struct spi_nor *nor)
669 int fsr = read_fsr(nor);
674 if (fsr & (FSR_E_ERR | FSR_P_ERR)) {
676 dev_err(nor->dev, "Erase operation failed.\n");
678 dev_err(nor->dev, "Program operation failed.\n");
680 if (fsr & FSR_PT_ERR)
682 "Attempted to modify a protected sector.\n");
684 nor->write_reg(nor, SPINOR_OP_CLFSR, NULL, 0);
688 return fsr & FSR_READY;
691 static int spi_nor_ready(struct spi_nor *nor)
695 sr = spi_nor_sr_ready(nor);
698 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
705 * Service routine to read status register until ready, or timeout occurs.
706 * Returns non-zero if error.
708 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
709 unsigned long timeout)
711 unsigned long timebase;
714 timebase = get_timer(0);
716 while (get_timer(timebase) < timeout) {
717 ret = spi_nor_ready(nor);
724 dev_err(nor->dev, "flash operation timed out\n");
729 static int spi_nor_wait_till_ready(struct spi_nor *nor)
731 return spi_nor_wait_till_ready_with_timeout(nor,
732 DEFAULT_READY_WAIT_JIFFIES);
735 #ifdef CONFIG_SPI_FLASH_BAR
737 * This "clean_bar" is necessary in a situation when one was accessing
738 * spi flash memory > 16 MiB by using Bank Address Register's BA24 bit.
740 * After it the BA24 bit shall be cleared to allow access to correct
741 * memory region after SW reset (by calling "reset" command).
743 * Otherwise, the BA24 bit may be left set and then after reset, the
744 * ROM would read/write/erase SPL from 16 MiB * bank_sel address.
746 static int clean_bar(struct spi_nor *nor)
748 u8 cmd, bank_sel = 0;
750 if (nor->bank_curr == 0)
752 cmd = nor->bank_write_cmd;
756 return nor->write_reg(nor, cmd, &bank_sel, 1);
759 static int write_bar(struct spi_nor *nor, u32 offset)
764 bank_sel = offset / SZ_16M;
765 if (bank_sel == nor->bank_curr)
768 cmd = nor->bank_write_cmd;
770 ret = nor->write_reg(nor, cmd, &bank_sel, 1);
772 debug("SF: fail to write bank register\n");
777 nor->bank_curr = bank_sel;
778 return nor->bank_curr;
781 static int read_bar(struct spi_nor *nor, const struct flash_info *info)
786 switch (JEDEC_MFR(info)) {
787 case SNOR_MFR_SPANSION:
788 nor->bank_read_cmd = SPINOR_OP_BRRD;
789 nor->bank_write_cmd = SPINOR_OP_BRWR;
792 nor->bank_read_cmd = SPINOR_OP_RDEAR;
793 nor->bank_write_cmd = SPINOR_OP_WREAR;
796 ret = nor->read_reg(nor, nor->bank_read_cmd,
799 debug("SF: fail to read bank addr register\n");
802 nor->bank_curr = curr_bank;
809 * Initiate the erasure of a single sector
811 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
813 struct spi_mem_op op =
814 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 0),
815 SPI_MEM_OP_ADDR(nor->addr_width, addr, 0),
819 spi_nor_setup_op(nor, &op, nor->write_proto);
822 return nor->erase(nor, addr);
825 * Default implementation, if driver doesn't have a specialized HW
828 return spi_mem_exec_op(nor->spi, &op);
832 * Erase an address range on the nor chip. The address range may extend
833 * one or more erase sectors. Return an error is there is a problem erasing.
835 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
837 struct spi_nor *nor = mtd_to_spi_nor(mtd);
841 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
842 (long long)instr->len);
847 div_u64_rem(instr->len, mtd->erasesize, &rem);
856 #ifdef CONFIG_SPI_FLASH_BAR
857 ret = write_bar(nor, addr);
863 ret = spi_nor_erase_sector(nor, addr);
867 addr += mtd->erasesize;
868 len -= mtd->erasesize;
870 ret = spi_nor_wait_till_ready(nor);
876 #ifdef CONFIG_SPI_FLASH_BAR
877 ret = clean_bar(nor);
884 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
885 /* Write status register and ensure bits in mask match written values */
886 static int write_sr_and_check(struct spi_nor *nor, u8 status_new, u8 mask)
891 ret = write_sr(nor, status_new);
895 ret = spi_nor_wait_till_ready(nor);
903 return ((ret & mask) != (status_new & mask)) ? -EIO : 0;
906 static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
909 struct mtd_info *mtd = &nor->mtd;
910 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
911 int shift = ffs(mask) - 1;
919 pow = ((sr & mask) ^ mask) >> shift;
920 *len = mtd->size >> pow;
921 if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
924 *ofs = mtd->size - *len;
929 * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
930 * @locked is false); 0 otherwise
932 static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, u64 len,
941 stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
944 /* Requested range is a sub-range of locked range */
945 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
947 /* Requested range does not overlap with locked range */
948 return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
951 static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
954 return stm_check_lock_status_sr(nor, ofs, len, sr, true);
957 static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
960 return stm_check_lock_status_sr(nor, ofs, len, sr, false);
964 * Lock a region of the flash. Compatible with ST Micro and similar flash.
965 * Supports the block protection bits BP{0,1,2} in the status register
966 * (SR). Does not support these features found in newer SR bitfields:
967 * - SEC: sector/block protect - only handle SEC=0 (block protect)
968 * - CMP: complement protect - only support CMP=0 (range is not complemented)
970 * Support for the following is provided conditionally for some flash:
971 * - TB: top/bottom protect
973 * Sample table portion for 8MB flash (Winbond w25q64fw):
975 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
976 * --------------------------------------------------------------------------
977 * X | X | 0 | 0 | 0 | NONE | NONE
978 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
979 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
980 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
981 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
982 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
983 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
984 * X | X | 1 | 1 | 1 | 8 MB | ALL
985 * ------|-------|-------|-------|-------|---------------|-------------------
986 * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
987 * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
988 * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
989 * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
990 * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
991 * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
993 * Returns negative on errors, 0 on success.
995 static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
997 struct mtd_info *mtd = &nor->mtd;
998 int status_old, status_new;
999 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1000 u8 shift = ffs(mask) - 1, pow, val;
1002 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
1005 status_old = read_sr(nor);
1009 /* If nothing in our range is unlocked, we don't need to do anything */
1010 if (stm_is_locked_sr(nor, ofs, len, status_old))
1013 /* If anything below us is unlocked, we can't use 'bottom' protection */
1014 if (!stm_is_locked_sr(nor, 0, ofs, status_old))
1015 can_be_bottom = false;
1017 /* If anything above us is unlocked, we can't use 'top' protection */
1018 if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
1022 if (!can_be_bottom && !can_be_top)
1025 /* Prefer top, if both are valid */
1026 use_top = can_be_top;
1028 /* lock_len: length of region that should end up locked */
1030 lock_len = mtd->size - ofs;
1032 lock_len = ofs + len;
1035 * Need smallest pow such that:
1037 * 1 / (2^pow) <= (len / size)
1039 * so (assuming power-of-2 size) we do:
1041 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
1043 pow = ilog2(mtd->size) - ilog2(lock_len);
1044 val = mask - (pow << shift);
1047 /* Don't "lock" with no region! */
1051 status_new = (status_old & ~mask & ~SR_TB) | val;
1053 /* Disallow further writes if WP pin is asserted */
1054 status_new |= SR_SRWD;
1057 status_new |= SR_TB;
1059 /* Don't bother if they're the same */
1060 if (status_new == status_old)
1063 /* Only modify protection if it will not unlock other areas */
1064 if ((status_new & mask) < (status_old & mask))
1067 return write_sr_and_check(nor, status_new, mask);
1071 * Unlock a region of the flash. See stm_lock() for more info
1073 * Returns negative on errors, 0 on success.
1075 static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1077 struct mtd_info *mtd = &nor->mtd;
1078 int status_old, status_new;
1079 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1080 u8 shift = ffs(mask) - 1, pow, val;
1082 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
1085 status_old = read_sr(nor);
1089 /* If nothing in our range is locked, we don't need to do anything */
1090 if (stm_is_unlocked_sr(nor, ofs, len, status_old))
1093 /* If anything below us is locked, we can't use 'top' protection */
1094 if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
1097 /* If anything above us is locked, we can't use 'bottom' protection */
1098 if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
1100 can_be_bottom = false;
1102 if (!can_be_bottom && !can_be_top)
1105 /* Prefer top, if both are valid */
1106 use_top = can_be_top;
1108 /* lock_len: length of region that should remain locked */
1110 lock_len = mtd->size - (ofs + len);
1115 * Need largest pow such that:
1117 * 1 / (2^pow) >= (len / size)
1119 * so (assuming power-of-2 size) we do:
1121 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
1123 pow = ilog2(mtd->size) - order_base_2(lock_len);
1124 if (lock_len == 0) {
1125 val = 0; /* fully unlocked */
1127 val = mask - (pow << shift);
1128 /* Some power-of-two sizes are not supported */
1133 status_new = (status_old & ~mask & ~SR_TB) | val;
1135 /* Don't protect status register if we're fully unlocked */
1137 status_new &= ~SR_SRWD;
1140 status_new |= SR_TB;
1142 /* Don't bother if they're the same */
1143 if (status_new == status_old)
1146 /* Only modify protection if it will not lock other areas */
1147 if ((status_new & mask) > (status_old & mask))
1150 return write_sr_and_check(nor, status_new, mask);
1154 * Check if a region of the flash is (completely) locked. See stm_lock() for
1157 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
1158 * negative on errors.
1160 static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
1164 status = read_sr(nor);
1168 return stm_is_locked_sr(nor, ofs, len, status);
1170 #endif /* CONFIG_SPI_FLASH_STMICRO */
1172 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
1175 u8 id[SPI_NOR_MAX_ID_LEN];
1176 const struct flash_info *info;
1178 tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
1180 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
1181 return ERR_PTR(tmp);
1185 for (; info->name; info++) {
1187 if (!memcmp(info->id, id, info->id_len))
1192 dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
1193 id[0], id[1], id[2]);
1194 return ERR_PTR(-ENODEV);
1197 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
1198 size_t *retlen, u_char *buf)
1200 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1203 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
1207 size_t read_len = len;
1209 #ifdef CONFIG_SPI_FLASH_BAR
1212 ret = write_bar(nor, addr);
1214 return log_ret(ret);
1215 remain_len = (SZ_16M * (nor->bank_curr + 1)) - addr;
1217 if (len < remain_len)
1220 read_len = remain_len;
1223 ret = nor->read(nor, addr, read_len, buf);
1225 /* We shouldn't see 0-length reads */
1240 #ifdef CONFIG_SPI_FLASH_BAR
1241 ret = clean_bar(nor);
1246 #ifdef CONFIG_SPI_FLASH_SST
1248 * sst26 flash series has its own block protection implementation:
1249 * 4x - 8 KByte blocks - read & write protection bits - upper addresses
1250 * 1x - 32 KByte blocks - write protection bits
1251 * rest - 64 KByte blocks - write protection bits
1252 * 1x - 32 KByte blocks - write protection bits
1253 * 4x - 8 KByte blocks - read & write protection bits - lower addresses
1255 * We'll support only per 64k lock/unlock so lower and upper 64 KByte region
1256 * will be treated as single block.
1258 #define SST26_BPR_8K_NUM 4
1259 #define SST26_MAX_BPR_REG_LEN (18 + 1)
1260 #define SST26_BOUND_REG_SIZE ((32 + SST26_BPR_8K_NUM * 8) * SZ_1K)
1268 static bool sst26_process_bpr(u32 bpr_size, u8 *cmd, u32 bit, enum lock_ctl ctl)
1271 case SST26_CTL_LOCK:
1272 cmd[bpr_size - (bit / 8) - 1] |= BIT(bit % 8);
1274 case SST26_CTL_UNLOCK:
1275 cmd[bpr_size - (bit / 8) - 1] &= ~BIT(bit % 8);
1277 case SST26_CTL_CHECK:
1278 return !!(cmd[bpr_size - (bit / 8) - 1] & BIT(bit % 8));
1285 * Lock, unlock or check lock status of the flash region of the flash (depending
1286 * on the lock_ctl value)
1288 static int sst26_lock_ctl(struct spi_nor *nor, loff_t ofs, uint64_t len, enum lock_ctl ctl)
1290 struct mtd_info *mtd = &nor->mtd;
1291 u32 i, bpr_ptr, rptr_64k, lptr_64k, bpr_size;
1292 bool lower_64k = false, upper_64k = false;
1293 u8 bpr_buff[SST26_MAX_BPR_REG_LEN] = {};
1296 /* Check length and offset for 64k alignment */
1297 if ((ofs & (SZ_64K - 1)) || (len & (SZ_64K - 1))) {
1298 dev_err(nor->dev, "length or offset is not 64KiB allighned\n");
1302 if (ofs + len > mtd->size) {
1303 dev_err(nor->dev, "range is more than device size: %#llx + %#llx > %#llx\n",
1304 ofs, len, mtd->size);
1308 /* SST26 family has only 16 Mbit, 32 Mbit and 64 Mbit IC */
1309 if (mtd->size != SZ_2M &&
1310 mtd->size != SZ_4M &&
1314 bpr_size = 2 + (mtd->size / SZ_64K / 8);
1316 ret = nor->read_reg(nor, SPINOR_OP_READ_BPR, bpr_buff, bpr_size);
1318 dev_err(nor->dev, "fail to read block-protection register\n");
1322 rptr_64k = min_t(u32, ofs + len, mtd->size - SST26_BOUND_REG_SIZE);
1323 lptr_64k = max_t(u32, ofs, SST26_BOUND_REG_SIZE);
1325 upper_64k = ((ofs + len) > (mtd->size - SST26_BOUND_REG_SIZE));
1326 lower_64k = (ofs < SST26_BOUND_REG_SIZE);
1328 /* Lower bits in block-protection register are about 64k region */
1329 bpr_ptr = lptr_64k / SZ_64K - 1;
1331 /* Process 64K blocks region */
1332 while (lptr_64k < rptr_64k) {
1333 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1340 /* 32K and 8K region bits in BPR are after 64k region bits */
1341 bpr_ptr = (mtd->size - 2 * SST26_BOUND_REG_SIZE) / SZ_64K;
1343 /* Process lower 32K block region */
1345 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1350 /* Process upper 32K block region */
1352 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1357 /* Process lower 8K block regions */
1358 for (i = 0; i < SST26_BPR_8K_NUM; i++) {
1360 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1363 /* In 8K area BPR has both read and write protection bits */
1367 /* Process upper 8K block regions */
1368 for (i = 0; i < SST26_BPR_8K_NUM; i++) {
1370 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1373 /* In 8K area BPR has both read and write protection bits */
1377 /* If we check region status we don't need to write BPR back */
1378 if (ctl == SST26_CTL_CHECK)
1381 ret = nor->write_reg(nor, SPINOR_OP_WRITE_BPR, bpr_buff, bpr_size);
1383 dev_err(nor->dev, "fail to write block-protection register\n");
1390 static int sst26_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1392 return sst26_lock_ctl(nor, ofs, len, SST26_CTL_UNLOCK);
1395 static int sst26_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1397 return sst26_lock_ctl(nor, ofs, len, SST26_CTL_LOCK);
1401 * Returns EACCES (positive value) if region is locked, 0 if region is unlocked,
1402 * and negative on errors.
1404 static int sst26_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
1407 * is_locked function is used for check before reading or erasing flash
1408 * region, so offset and length might be not 64k allighned, so adjust
1409 * them to be 64k allighned as sst26_lock_ctl works only with 64k
1410 * allighned regions.
1412 ofs -= ofs & (SZ_64K - 1);
1413 len = len & (SZ_64K - 1) ? (len & ~(SZ_64K - 1)) + SZ_64K : len;
1415 return sst26_lock_ctl(nor, ofs, len, SST26_CTL_CHECK);
1418 static int sst_write_byteprogram(struct spi_nor *nor, loff_t to, size_t len,
1419 size_t *retlen, const u_char *buf)
1424 for (actual = 0; actual < len; actual++) {
1425 nor->program_opcode = SPINOR_OP_BP;
1428 /* write one byte. */
1429 ret = nor->write(nor, to, 1, buf + actual);
1432 ret = spi_nor_wait_till_ready(nor);
1443 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
1444 size_t *retlen, const u_char *buf)
1446 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1447 struct spi_slave *spi = nor->spi;
1451 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1452 if (spi->mode & SPI_TX_BYTE)
1453 return sst_write_byteprogram(nor, to, len, retlen, buf);
1457 nor->sst_write_second = false;
1460 /* Start write from odd address. */
1462 nor->program_opcode = SPINOR_OP_BP;
1464 /* write one byte. */
1465 ret = nor->write(nor, to, 1, buf);
1468 ret = spi_nor_wait_till_ready(nor);
1474 /* Write out most of the data here. */
1475 for (; actual < len - 1; actual += 2) {
1476 nor->program_opcode = SPINOR_OP_AAI_WP;
1478 /* write two bytes. */
1479 ret = nor->write(nor, to, 2, buf + actual);
1482 ret = spi_nor_wait_till_ready(nor);
1486 nor->sst_write_second = true;
1488 nor->sst_write_second = false;
1491 ret = spi_nor_wait_till_ready(nor);
1495 /* Write out trailing byte if it exists. */
1496 if (actual != len) {
1499 nor->program_opcode = SPINOR_OP_BP;
1500 ret = nor->write(nor, to, 1, buf + actual);
1503 ret = spi_nor_wait_till_ready(nor);
1515 * Write an address range to the nor chip. Data must be written in
1516 * FLASH_PAGESIZE chunks. The address range may be any size provided
1517 * it is within the physical boundaries.
1519 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
1520 size_t *retlen, const u_char *buf)
1522 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1523 size_t page_offset, page_remain, i;
1526 #ifdef CONFIG_SPI_FLASH_SST
1527 /* sst nor chips use AAI word program */
1528 if (nor->info->flags & SST_WRITE)
1529 return sst_write(mtd, to, len, retlen, buf);
1532 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1537 for (i = 0; i < len; ) {
1539 loff_t addr = to + i;
1543 * If page_size is a power of two, the offset can be quickly
1544 * calculated with an AND operation. On the other cases we
1545 * need to do a modulus operation (more expensive).
1547 if (is_power_of_2(nor->page_size)) {
1548 page_offset = addr & (nor->page_size - 1);
1552 page_offset = do_div(aux, nor->page_size);
1554 /* the size of data remaining on the first page */
1555 page_remain = min_t(size_t,
1556 nor->page_size - page_offset, len - i);
1558 #ifdef CONFIG_SPI_FLASH_BAR
1559 ret = write_bar(nor, addr);
1564 ret = nor->write(nor, addr, page_remain, buf + i);
1569 ret = spi_nor_wait_till_ready(nor);
1577 #ifdef CONFIG_SPI_FLASH_BAR
1578 ret = clean_bar(nor);
1583 #if defined(CONFIG_SPI_FLASH_MACRONIX) || defined(CONFIG_SPI_FLASH_ISSI)
1585 * macronix_quad_enable() - set QE bit in Status Register.
1586 * @nor: pointer to a 'struct spi_nor'
1588 * Set the Quad Enable (QE) bit in the Status Register.
1590 * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
1592 * Return: 0 on success, -errno otherwise.
1594 static int macronix_quad_enable(struct spi_nor *nor)
1601 if (val & SR_QUAD_EN_MX)
1606 write_sr(nor, val | SR_QUAD_EN_MX);
1608 ret = spi_nor_wait_till_ready(nor);
1613 if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
1614 dev_err(nor->dev, "Macronix Quad bit not set\n");
1622 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
1624 * Write status Register and configuration register with 2 bytes
1625 * The first byte will be written to the status register, while the
1626 * second byte will be written to the configuration register.
1627 * Return negative if error occurred.
1629 static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
1635 ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
1638 "error while writing configuration register\n");
1642 ret = spi_nor_wait_till_ready(nor);
1645 "timeout while writing configuration register\n");
1653 * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
1654 * @nor: pointer to a 'struct spi_nor'
1656 * Set the Quad Enable (QE) bit in the Configuration Register.
1657 * This function should be used with QSPI memories supporting the Read
1658 * Configuration Register (35h) instruction.
1660 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1663 * Return: 0 on success, -errno otherwise.
1665 static int spansion_read_cr_quad_enable(struct spi_nor *nor)
1670 /* Check current Quad Enable bit value. */
1674 "error while reading configuration register\n");
1678 if (ret & CR_QUAD_EN_SPAN)
1681 sr_cr[1] = ret | CR_QUAD_EN_SPAN;
1683 /* Keep the current value of the Status Register. */
1686 dev_dbg(nor->dev, "error while reading status register\n");
1691 ret = write_sr_cr(nor, sr_cr);
1695 /* Read back and check it. */
1697 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
1698 dev_dbg(nor->dev, "Spansion Quad bit not set\n");
1705 #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT)
1707 * spansion_no_read_cr_quad_enable() - set QE bit in Configuration Register.
1708 * @nor: pointer to a 'struct spi_nor'
1710 * Set the Quad Enable (QE) bit in the Configuration Register.
1711 * This function should be used with QSPI memories not supporting the Read
1712 * Configuration Register (35h) instruction.
1714 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1717 * Return: 0 on success, -errno otherwise.
1719 static int spansion_no_read_cr_quad_enable(struct spi_nor *nor)
1724 /* Keep the current value of the Status Register. */
1727 dev_dbg(nor->dev, "error while reading status register\n");
1731 sr_cr[1] = CR_QUAD_EN_SPAN;
1733 return write_sr_cr(nor, sr_cr);
1736 #endif /* CONFIG_SPI_FLASH_SFDP_SUPPORT */
1737 #endif /* CONFIG_SPI_FLASH_SPANSION */
1740 spi_nor_set_read_settings(struct spi_nor_read_command *read,
1744 enum spi_nor_protocol proto)
1746 read->num_mode_clocks = num_mode_clocks;
1747 read->num_wait_states = num_wait_states;
1748 read->opcode = opcode;
1749 read->proto = proto;
1753 spi_nor_set_pp_settings(struct spi_nor_pp_command *pp,
1755 enum spi_nor_protocol proto)
1757 pp->opcode = opcode;
1761 #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT)
1763 * Serial Flash Discoverable Parameters (SFDP) parsing.
1767 * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
1768 * @nor: pointer to a 'struct spi_nor'
1769 * @addr: offset in the SFDP area to start reading data from
1770 * @len: number of bytes to read
1771 * @buf: buffer where the SFDP data are copied into (dma-safe memory)
1773 * Whatever the actual numbers of bytes for address and dummy cycles are
1774 * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
1775 * followed by a 3-byte address and 8 dummy clock cycles.
1777 * Return: 0 on success, -errno otherwise.
1779 static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr,
1780 size_t len, void *buf)
1782 u8 addr_width, read_opcode, read_dummy;
1785 read_opcode = nor->read_opcode;
1786 addr_width = nor->addr_width;
1787 read_dummy = nor->read_dummy;
1789 nor->read_opcode = SPINOR_OP_RDSFDP;
1790 nor->addr_width = 3;
1791 nor->read_dummy = 8;
1794 ret = nor->read(nor, addr, len, (u8 *)buf);
1795 if (!ret || ret > len) {
1809 nor->read_opcode = read_opcode;
1810 nor->addr_width = addr_width;
1811 nor->read_dummy = read_dummy;
1816 /* Fast Read settings. */
1819 spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read,
1821 enum spi_nor_protocol proto)
1823 read->num_mode_clocks = (half >> 5) & 0x07;
1824 read->num_wait_states = (half >> 0) & 0x1f;
1825 read->opcode = (half >> 8) & 0xff;
1826 read->proto = proto;
1829 struct sfdp_bfpt_read {
1830 /* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
1834 * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us
1835 * whether the Fast Read x-y-z command is supported.
1837 u32 supported_dword;
1841 * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
1842 * encodes the op code, the number of mode clocks and the number of wait
1843 * states to be used by Fast Read x-y-z command.
1848 /* The SPI protocol for this Fast Read x-y-z command. */
1849 enum spi_nor_protocol proto;
1852 static const struct sfdp_bfpt_read sfdp_bfpt_reads[] = {
1853 /* Fast Read 1-1-2 */
1855 SNOR_HWCAPS_READ_1_1_2,
1856 BFPT_DWORD(1), BIT(16), /* Supported bit */
1857 BFPT_DWORD(4), 0, /* Settings */
1861 /* Fast Read 1-2-2 */
1863 SNOR_HWCAPS_READ_1_2_2,
1864 BFPT_DWORD(1), BIT(20), /* Supported bit */
1865 BFPT_DWORD(4), 16, /* Settings */
1869 /* Fast Read 2-2-2 */
1871 SNOR_HWCAPS_READ_2_2_2,
1872 BFPT_DWORD(5), BIT(0), /* Supported bit */
1873 BFPT_DWORD(6), 16, /* Settings */
1877 /* Fast Read 1-1-4 */
1879 SNOR_HWCAPS_READ_1_1_4,
1880 BFPT_DWORD(1), BIT(22), /* Supported bit */
1881 BFPT_DWORD(3), 16, /* Settings */
1885 /* Fast Read 1-4-4 */
1887 SNOR_HWCAPS_READ_1_4_4,
1888 BFPT_DWORD(1), BIT(21), /* Supported bit */
1889 BFPT_DWORD(3), 0, /* Settings */
1893 /* Fast Read 4-4-4 */
1895 SNOR_HWCAPS_READ_4_4_4,
1896 BFPT_DWORD(5), BIT(4), /* Supported bit */
1897 BFPT_DWORD(7), 16, /* Settings */
1902 struct sfdp_bfpt_erase {
1904 * The half-word at offset <shift> in DWORD <dwoard> encodes the
1905 * op code and erase sector size to be used by Sector Erase commands.
1911 static const struct sfdp_bfpt_erase sfdp_bfpt_erases[] = {
1912 /* Erase Type 1 in DWORD8 bits[15:0] */
1915 /* Erase Type 2 in DWORD8 bits[31:16] */
1916 {BFPT_DWORD(8), 16},
1918 /* Erase Type 3 in DWORD9 bits[15:0] */
1921 /* Erase Type 4 in DWORD9 bits[31:16] */
1922 {BFPT_DWORD(9), 16},
1925 static int spi_nor_hwcaps_read2cmd(u32 hwcaps);
1928 spi_nor_post_bfpt_fixups(struct spi_nor *nor,
1929 const struct sfdp_parameter_header *bfpt_header,
1930 const struct sfdp_bfpt *bfpt,
1931 struct spi_nor_flash_parameter *params)
1933 if (nor->fixups && nor->fixups->post_bfpt)
1934 return nor->fixups->post_bfpt(nor, bfpt_header, bfpt, params);
1940 * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
1941 * @nor: pointer to a 'struct spi_nor'
1942 * @bfpt_header: pointer to the 'struct sfdp_parameter_header' describing
1943 * the Basic Flash Parameter Table length and version
1944 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
1947 * The Basic Flash Parameter Table is the main and only mandatory table as
1948 * defined by the SFDP (JESD216) specification.
1949 * It provides us with the total size (memory density) of the data array and
1950 * the number of address bytes for Fast Read, Page Program and Sector Erase
1952 * For Fast READ commands, it also gives the number of mode clock cycles and
1953 * wait states (regrouped in the number of dummy clock cycles) for each
1954 * supported instruction op code.
1955 * For Page Program, the page size is now available since JESD216 rev A, however
1956 * the supported instruction op codes are still not provided.
1957 * For Sector Erase commands, this table stores the supported instruction op
1958 * codes and the associated sector sizes.
1959 * Finally, the Quad Enable Requirements (QER) are also available since JESD216
1960 * rev A. The QER bits encode the manufacturer dependent procedure to be
1961 * executed to set the Quad Enable (QE) bit in some internal register of the
1962 * Quad SPI memory. Indeed the QE bit, when it exists, must be set before
1963 * sending any Quad SPI command to the memory. Actually, setting the QE bit
1964 * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2
1965 * and IO3 hence enabling 4 (Quad) I/O lines.
1967 * Return: 0 on success, -errno otherwise.
1969 static int spi_nor_parse_bfpt(struct spi_nor *nor,
1970 const struct sfdp_parameter_header *bfpt_header,
1971 struct spi_nor_flash_parameter *params)
1973 struct mtd_info *mtd = &nor->mtd;
1974 struct sfdp_bfpt bfpt;
1980 /* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */
1981 if (bfpt_header->length < BFPT_DWORD_MAX_JESD216)
1984 /* Read the Basic Flash Parameter Table. */
1985 len = min_t(size_t, sizeof(bfpt),
1986 bfpt_header->length * sizeof(u32));
1987 addr = SFDP_PARAM_HEADER_PTP(bfpt_header);
1988 memset(&bfpt, 0, sizeof(bfpt));
1989 err = spi_nor_read_sfdp(nor, addr, len, &bfpt);
1993 /* Fix endianness of the BFPT DWORDs. */
1994 for (i = 0; i < BFPT_DWORD_MAX; i++)
1995 bfpt.dwords[i] = le32_to_cpu(bfpt.dwords[i]);
1997 /* Number of address bytes. */
1998 switch (bfpt.dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) {
1999 case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY:
2000 nor->addr_width = 3;
2003 case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY:
2004 nor->addr_width = 4;
2011 /* Flash Memory Density (in bits). */
2012 params->size = bfpt.dwords[BFPT_DWORD(2)];
2013 if (params->size & BIT(31)) {
2014 params->size &= ~BIT(31);
2017 * Prevent overflows on params->size. Anyway, a NOR of 2^64
2018 * bits is unlikely to exist so this error probably means
2019 * the BFPT we are reading is corrupted/wrong.
2021 if (params->size > 63)
2024 params->size = 1ULL << params->size;
2028 params->size >>= 3; /* Convert to bytes. */
2030 /* Fast Read settings. */
2031 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_reads); i++) {
2032 const struct sfdp_bfpt_read *rd = &sfdp_bfpt_reads[i];
2033 struct spi_nor_read_command *read;
2035 if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) {
2036 params->hwcaps.mask &= ~rd->hwcaps;
2040 params->hwcaps.mask |= rd->hwcaps;
2041 cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps);
2042 read = ¶ms->reads[cmd];
2043 half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift;
2044 spi_nor_set_read_settings_from_bfpt(read, half, rd->proto);
2047 /* Sector Erase settings. */
2048 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_erases); i++) {
2049 const struct sfdp_bfpt_erase *er = &sfdp_bfpt_erases[i];
2053 half = bfpt.dwords[er->dword] >> er->shift;
2054 erasesize = half & 0xff;
2056 /* erasesize == 0 means this Erase Type is not supported. */
2060 erasesize = 1U << erasesize;
2061 opcode = (half >> 8) & 0xff;
2062 #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
2063 if (erasesize == SZ_4K) {
2064 nor->erase_opcode = opcode;
2065 mtd->erasesize = erasesize;
2069 if (!mtd->erasesize || mtd->erasesize < erasesize) {
2070 nor->erase_opcode = opcode;
2071 mtd->erasesize = erasesize;
2075 /* Stop here if not JESD216 rev A or later. */
2076 if (bfpt_header->length == BFPT_DWORD_MAX_JESD216)
2077 return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt,
2080 /* Page size: this field specifies 'N' so the page size = 2^N bytes. */
2081 params->page_size = bfpt.dwords[BFPT_DWORD(11)];
2082 params->page_size &= BFPT_DWORD11_PAGE_SIZE_MASK;
2083 params->page_size >>= BFPT_DWORD11_PAGE_SIZE_SHIFT;
2084 params->page_size = 1U << params->page_size;
2086 /* Quad Enable Requirements. */
2087 switch (bfpt.dwords[BFPT_DWORD(15)] & BFPT_DWORD15_QER_MASK) {
2088 case BFPT_DWORD15_QER_NONE:
2089 params->quad_enable = NULL;
2091 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
2092 case BFPT_DWORD15_QER_SR2_BIT1_BUGGY:
2093 case BFPT_DWORD15_QER_SR2_BIT1_NO_RD:
2094 params->quad_enable = spansion_no_read_cr_quad_enable;
2097 #if defined(CONFIG_SPI_FLASH_MACRONIX) || defined(CONFIG_SPI_FLASH_ISSI)
2098 case BFPT_DWORD15_QER_SR1_BIT6:
2099 params->quad_enable = macronix_quad_enable;
2102 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
2103 case BFPT_DWORD15_QER_SR2_BIT1:
2104 params->quad_enable = spansion_read_cr_quad_enable;
2108 dev_dbg(nor->dev, "BFPT QER reserved value used\n");
2112 /* Soft Reset support. */
2113 if (bfpt.dwords[BFPT_DWORD(16)] & BFPT_DWORD16_SOFT_RST)
2114 nor->flags |= SNOR_F_SOFT_RESET;
2116 /* Stop here if JESD216 rev B. */
2117 if (bfpt_header->length == BFPT_DWORD_MAX_JESD216B)
2118 return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt,
2121 /* 8D-8D-8D command extension. */
2122 switch (bfpt.dwords[BFPT_DWORD(18)] & BFPT_DWORD18_CMD_EXT_MASK) {
2123 case BFPT_DWORD18_CMD_EXT_REP:
2124 nor->cmd_ext_type = SPI_NOR_EXT_REPEAT;
2127 case BFPT_DWORD18_CMD_EXT_INV:
2128 nor->cmd_ext_type = SPI_NOR_EXT_INVERT;
2131 case BFPT_DWORD18_CMD_EXT_RES:
2134 case BFPT_DWORD18_CMD_EXT_16B:
2135 dev_err(nor->dev, "16-bit opcodes not supported\n");
2139 return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt, params);
2143 * spi_nor_parse_microchip_sfdp() - parse the Microchip manufacturer specific
2145 * @nor: pointer to a 'struct spi_nor'.
2146 * @param_header: pointer to the SFDP parameter header.
2148 * Return: 0 on success, -errno otherwise.
2151 spi_nor_parse_microchip_sfdp(struct spi_nor *nor,
2152 const struct sfdp_parameter_header *param_header)
2158 size = param_header->length * sizeof(u32);
2159 addr = SFDP_PARAM_HEADER_PTP(param_header);
2161 nor->manufacturer_sfdp = devm_kmalloc(nor->dev, size, GFP_KERNEL);
2162 if (!nor->manufacturer_sfdp)
2165 ret = spi_nor_read_sfdp(nor, addr, size, nor->manufacturer_sfdp);
2171 * spi_nor_parse_profile1() - parse the xSPI Profile 1.0 table
2172 * @nor: pointer to a 'struct spi_nor'
2173 * @profile1_header: pointer to the 'struct sfdp_parameter_header' describing
2174 * the 4-Byte Address Instruction Table length and version.
2175 * @params: pointer to the 'struct spi_nor_flash_parameter' to be.
2177 * Return: 0 on success, -errno otherwise.
2179 static int spi_nor_parse_profile1(struct spi_nor *nor,
2180 const struct sfdp_parameter_header *profile1_header,
2181 struct spi_nor_flash_parameter *params)
2183 u32 *table, opcode, addr;
2188 len = profile1_header->length * sizeof(*table);
2189 table = kmalloc(len, GFP_KERNEL);
2193 addr = SFDP_PARAM_HEADER_PTP(profile1_header);
2194 ret = spi_nor_read_sfdp(nor, addr, len, table);
2198 /* Fix endianness of the table DWORDs. */
2199 for (i = 0; i < profile1_header->length; i++)
2200 table[i] = le32_to_cpu(table[i]);
2202 /* Get 8D-8D-8D fast read opcode and dummy cycles. */
2203 opcode = FIELD_GET(PROFILE1_DWORD1_RD_FAST_CMD, table[0]);
2206 * We don't know what speed the controller is running at. Find the
2207 * dummy cycles for the fastest frequency the flash can run at to be
2208 * sure we are never short of dummy cycles. A value of 0 means the
2209 * frequency is not supported.
2211 * Default to PROFILE1_DUMMY_DEFAULT if we don't find anything, and let
2212 * flashes set the correct value if needed in their fixup hooks.
2214 dummy = FIELD_GET(PROFILE1_DWORD4_DUMMY_200MHZ, table[3]);
2216 dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_166MHZ, table[4]);
2218 dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_133MHZ, table[4]);
2220 dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_100MHZ, table[4]);
2222 dummy = PROFILE1_DUMMY_DEFAULT;
2224 /* Round up to an even value to avoid tripping controllers up. */
2225 dummy = ROUND_UP_TO(dummy, 2);
2227 /* Update the fast read settings. */
2228 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_8_8_8_DTR],
2230 SNOR_PROTO_8_8_8_DTR);
2233 * Set the Read Status Register dummy cycles and dummy address bytes.
2235 if (table[0] & PROFILE1_DWORD1_RDSR_DUMMY)
2236 params->rdsr_dummy = 8;
2238 params->rdsr_dummy = 4;
2240 if (table[0] & PROFILE1_DWORD1_RDSR_ADDR_BYTES)
2241 params->rdsr_addr_nbytes = 4;
2243 params->rdsr_addr_nbytes = 0;
2251 * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
2252 * @nor: pointer to a 'struct spi_nor'
2253 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
2256 * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
2257 * specification. This is a standard which tends to supported by almost all
2258 * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
2259 * runtime the main parameters needed to perform basic SPI flash operations such
2260 * as Fast Read, Page Program or Sector Erase commands.
2262 * Return: 0 on success, -errno otherwise.
2264 static int spi_nor_parse_sfdp(struct spi_nor *nor,
2265 struct spi_nor_flash_parameter *params)
2267 const struct sfdp_parameter_header *param_header, *bfpt_header;
2268 struct sfdp_parameter_header *param_headers = NULL;
2269 struct sfdp_header header;
2273 /* Get the SFDP header. */
2274 err = spi_nor_read_sfdp(nor, 0, sizeof(header), &header);
2278 /* Check the SFDP header version. */
2279 if (le32_to_cpu(header.signature) != SFDP_SIGNATURE ||
2280 header.major != SFDP_JESD216_MAJOR)
2284 * Verify that the first and only mandatory parameter header is a
2285 * Basic Flash Parameter Table header as specified in JESD216.
2287 bfpt_header = &header.bfpt_header;
2288 if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID ||
2289 bfpt_header->major != SFDP_JESD216_MAJOR)
2293 * Allocate memory then read all parameter headers with a single
2294 * Read SFDP command. These parameter headers will actually be parsed
2295 * twice: a first time to get the latest revision of the basic flash
2296 * parameter table, then a second time to handle the supported optional
2298 * Hence we read the parameter headers once for all to reduce the
2299 * processing time. Also we use kmalloc() instead of devm_kmalloc()
2300 * because we don't need to keep these parameter headers: the allocated
2301 * memory is always released with kfree() before exiting this function.
2304 psize = header.nph * sizeof(*param_headers);
2306 param_headers = kmalloc(psize, GFP_KERNEL);
2310 err = spi_nor_read_sfdp(nor, sizeof(header),
2311 psize, param_headers);
2314 "failed to read SFDP parameter headers\n");
2320 * Check other parameter headers to get the latest revision of
2321 * the basic flash parameter table.
2323 for (i = 0; i < header.nph; i++) {
2324 param_header = ¶m_headers[i];
2326 if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID &&
2327 param_header->major == SFDP_JESD216_MAJOR &&
2328 (param_header->minor > bfpt_header->minor ||
2329 (param_header->minor == bfpt_header->minor &&
2330 param_header->length > bfpt_header->length)))
2331 bfpt_header = param_header;
2334 err = spi_nor_parse_bfpt(nor, bfpt_header, params);
2338 /* Parse other parameter headers. */
2339 for (i = 0; i < header.nph; i++) {
2340 param_header = ¶m_headers[i];
2342 switch (SFDP_PARAM_HEADER_ID(param_header)) {
2343 case SFDP_SECTOR_MAP_ID:
2345 "non-uniform erase sector maps are not supported yet.\n");
2349 err = spi_nor_parse_microchip_sfdp(nor, param_header);
2352 case SFDP_PROFILE1_ID:
2353 err = spi_nor_parse_profile1(nor, param_header, params);
2362 "Failed to parse optional parameter table: %04x\n",
2363 SFDP_PARAM_HEADER_ID(param_header));
2365 * Let's not drop all information we extracted so far
2366 * if optional table parsers fail. In case of failing,
2367 * each optional parser is responsible to roll back to
2368 * the previously known spi_nor data.
2375 kfree(param_headers);
2379 static int spi_nor_parse_sfdp(struct spi_nor *nor,
2380 struct spi_nor_flash_parameter *params)
2384 #endif /* SPI_FLASH_SFDP_SUPPORT */
2387 * spi_nor_post_sfdp_fixups() - Updates the flash's parameters and settings
2388 * after SFDP has been parsed (is also called for SPI NORs that do not
2390 * @nor: pointer to a 'struct spi_nor'
2392 * Typically used to tweak various parameters that could not be extracted by
2393 * other means (i.e. when information provided by the SFDP/flash_info tables
2394 * are incomplete or wrong).
2396 static void spi_nor_post_sfdp_fixups(struct spi_nor *nor,
2397 struct spi_nor_flash_parameter *params)
2399 if (nor->fixups && nor->fixups->post_sfdp)
2400 nor->fixups->post_sfdp(nor, params);
2403 static void spi_nor_default_init_fixups(struct spi_nor *nor)
2405 if (nor->fixups && nor->fixups->default_init)
2406 nor->fixups->default_init(nor);
2409 static int spi_nor_init_params(struct spi_nor *nor,
2410 const struct flash_info *info,
2411 struct spi_nor_flash_parameter *params)
2413 /* Set legacy flash parameters as default. */
2414 memset(params, 0, sizeof(*params));
2416 /* Set SPI NOR sizes. */
2417 params->size = info->sector_size * info->n_sectors;
2418 params->page_size = info->page_size;
2420 /* (Fast) Read settings. */
2421 params->hwcaps.mask |= SNOR_HWCAPS_READ;
2422 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ],
2423 0, 0, SPINOR_OP_READ,
2426 if (!(info->flags & SPI_NOR_NO_FR)) {
2427 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
2428 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_FAST],
2429 0, 8, SPINOR_OP_READ_FAST,
2433 if (info->flags & SPI_NOR_DUAL_READ) {
2434 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
2435 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_2],
2436 0, 8, SPINOR_OP_READ_1_1_2,
2440 if (info->flags & SPI_NOR_QUAD_READ) {
2441 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
2442 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_4],
2443 0, 8, SPINOR_OP_READ_1_1_4,
2447 if (info->flags & SPI_NOR_OCTAL_READ) {
2448 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8;
2449 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_8],
2450 0, 8, SPINOR_OP_READ_1_1_8,
2454 if (info->flags & SPI_NOR_OCTAL_DTR_READ) {
2455 params->hwcaps.mask |= SNOR_HWCAPS_READ_8_8_8_DTR;
2456 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_8_8_8_DTR],
2457 0, 20, SPINOR_OP_READ_FAST,
2458 SNOR_PROTO_8_8_8_DTR);
2461 /* Page Program settings. */
2462 params->hwcaps.mask |= SNOR_HWCAPS_PP;
2463 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP],
2464 SPINOR_OP_PP, SNOR_PROTO_1_1_1);
2467 * Since xSPI Page Program opcode is backward compatible with
2468 * Legacy SPI, use Legacy SPI opcode there as well.
2470 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP_8_8_8_DTR],
2471 SPINOR_OP_PP, SNOR_PROTO_8_8_8_DTR);
2473 if (info->flags & SPI_NOR_QUAD_READ) {
2474 params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4;
2475 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP_1_1_4],
2476 SPINOR_OP_PP_1_1_4, SNOR_PROTO_1_1_4);
2479 /* Select the procedure to set the Quad Enable bit. */
2480 if (params->hwcaps.mask & (SNOR_HWCAPS_READ_QUAD |
2481 SNOR_HWCAPS_PP_QUAD)) {
2482 switch (JEDEC_MFR(info)) {
2483 #if defined(CONFIG_SPI_FLASH_MACRONIX) || defined(CONFIG_SPI_FLASH_ISSI)
2484 case SNOR_MFR_MACRONIX:
2486 params->quad_enable = macronix_quad_enable;
2490 case SNOR_MFR_MICRON:
2494 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
2495 /* Kept only for backward compatibility purpose. */
2496 params->quad_enable = spansion_read_cr_quad_enable;
2502 spi_nor_default_init_fixups(nor);
2504 /* Override the parameters with data read from SFDP tables. */
2505 nor->addr_width = 0;
2506 nor->mtd.erasesize = 0;
2507 if ((info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2508 SPI_NOR_OCTAL_DTR_READ)) &&
2509 !(info->flags & SPI_NOR_SKIP_SFDP)) {
2510 struct spi_nor_flash_parameter sfdp_params;
2512 memcpy(&sfdp_params, params, sizeof(sfdp_params));
2513 if (spi_nor_parse_sfdp(nor, &sfdp_params)) {
2514 nor->addr_width = 0;
2515 nor->mtd.erasesize = 0;
2517 memcpy(params, &sfdp_params, sizeof(*params));
2521 spi_nor_post_sfdp_fixups(nor, params);
2526 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
2530 for (i = 0; i < size; i++)
2531 if (table[i][0] == (int)hwcaps)
2537 static int spi_nor_hwcaps_read2cmd(u32 hwcaps)
2539 static const int hwcaps_read2cmd[][2] = {
2540 { SNOR_HWCAPS_READ, SNOR_CMD_READ },
2541 { SNOR_HWCAPS_READ_FAST, SNOR_CMD_READ_FAST },
2542 { SNOR_HWCAPS_READ_1_1_1_DTR, SNOR_CMD_READ_1_1_1_DTR },
2543 { SNOR_HWCAPS_READ_1_1_2, SNOR_CMD_READ_1_1_2 },
2544 { SNOR_HWCAPS_READ_1_2_2, SNOR_CMD_READ_1_2_2 },
2545 { SNOR_HWCAPS_READ_2_2_2, SNOR_CMD_READ_2_2_2 },
2546 { SNOR_HWCAPS_READ_1_2_2_DTR, SNOR_CMD_READ_1_2_2_DTR },
2547 { SNOR_HWCAPS_READ_1_1_4, SNOR_CMD_READ_1_1_4 },
2548 { SNOR_HWCAPS_READ_1_4_4, SNOR_CMD_READ_1_4_4 },
2549 { SNOR_HWCAPS_READ_4_4_4, SNOR_CMD_READ_4_4_4 },
2550 { SNOR_HWCAPS_READ_1_4_4_DTR, SNOR_CMD_READ_1_4_4_DTR },
2551 { SNOR_HWCAPS_READ_1_1_8, SNOR_CMD_READ_1_1_8 },
2552 { SNOR_HWCAPS_READ_1_8_8, SNOR_CMD_READ_1_8_8 },
2553 { SNOR_HWCAPS_READ_8_8_8, SNOR_CMD_READ_8_8_8 },
2554 { SNOR_HWCAPS_READ_1_8_8_DTR, SNOR_CMD_READ_1_8_8_DTR },
2555 { SNOR_HWCAPS_READ_8_8_8_DTR, SNOR_CMD_READ_8_8_8_DTR },
2558 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
2559 ARRAY_SIZE(hwcaps_read2cmd));
2562 static int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
2564 static const int hwcaps_pp2cmd[][2] = {
2565 { SNOR_HWCAPS_PP, SNOR_CMD_PP },
2566 { SNOR_HWCAPS_PP_1_1_4, SNOR_CMD_PP_1_1_4 },
2567 { SNOR_HWCAPS_PP_1_4_4, SNOR_CMD_PP_1_4_4 },
2568 { SNOR_HWCAPS_PP_4_4_4, SNOR_CMD_PP_4_4_4 },
2569 { SNOR_HWCAPS_PP_1_1_8, SNOR_CMD_PP_1_1_8 },
2570 { SNOR_HWCAPS_PP_1_8_8, SNOR_CMD_PP_1_8_8 },
2571 { SNOR_HWCAPS_PP_8_8_8, SNOR_CMD_PP_8_8_8 },
2572 { SNOR_HWCAPS_PP_8_8_8_DTR, SNOR_CMD_PP_8_8_8_DTR },
2575 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
2576 ARRAY_SIZE(hwcaps_pp2cmd));
2579 #ifdef CONFIG_SPI_FLASH_SMART_HWCAPS
2581 * spi_nor_check_op - check if the operation is supported by controller
2582 * @nor: pointer to a 'struct spi_nor'
2583 * @op: pointer to op template to be checked
2585 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2587 static int spi_nor_check_op(struct spi_nor *nor,
2588 struct spi_mem_op *op)
2591 * First test with 4 address bytes. The opcode itself might be a 3B
2592 * addressing opcode but we don't care, because SPI controller
2593 * implementation should not check the opcode, but just the sequence.
2595 op->addr.nbytes = 4;
2596 if (!spi_mem_supports_op(nor->spi, op)) {
2597 if (nor->mtd.size > SZ_16M)
2600 /* If flash size <= 16MB, 3 address bytes are sufficient */
2601 op->addr.nbytes = 3;
2602 if (!spi_mem_supports_op(nor->spi, op))
2610 * spi_nor_check_readop - check if the read op is supported by controller
2611 * @nor: pointer to a 'struct spi_nor'
2612 * @read: pointer to op template to be checked
2614 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2616 static int spi_nor_check_readop(struct spi_nor *nor,
2617 const struct spi_nor_read_command *read)
2619 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(read->opcode, 0),
2620 SPI_MEM_OP_ADDR(3, 0, 0),
2621 SPI_MEM_OP_DUMMY(1, 0),
2622 SPI_MEM_OP_DATA_IN(2, NULL, 0));
2624 spi_nor_setup_op(nor, &op, read->proto);
2626 op.dummy.nbytes = (read->num_mode_clocks + read->num_wait_states) *
2627 op.dummy.buswidth / 8;
2628 if (spi_nor_protocol_is_dtr(nor->read_proto))
2629 op.dummy.nbytes *= 2;
2631 return spi_nor_check_op(nor, &op);
2635 * spi_nor_check_pp - check if the page program op is supported by controller
2636 * @nor: pointer to a 'struct spi_nor'
2637 * @pp: pointer to op template to be checked
2639 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2641 static int spi_nor_check_pp(struct spi_nor *nor,
2642 const struct spi_nor_pp_command *pp)
2644 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(pp->opcode, 0),
2645 SPI_MEM_OP_ADDR(3, 0, 0),
2646 SPI_MEM_OP_NO_DUMMY,
2647 SPI_MEM_OP_DATA_OUT(2, NULL, 0));
2649 spi_nor_setup_op(nor, &op, pp->proto);
2651 return spi_nor_check_op(nor, &op);
2655 * spi_nor_adjust_hwcaps - Find optimal Read/Write protocol based on SPI
2656 * controller capabilities
2657 * @nor: pointer to a 'struct spi_nor'
2658 * @params: pointer to the 'struct spi_nor_flash_parameter'
2659 * representing SPI NOR flash capabilities
2660 * @hwcaps: pointer to resulting capabilities after adjusting
2661 * according to controller and flash's capability
2663 * Discard caps based on what the SPI controller actually supports (using
2664 * spi_mem_supports_op()).
2667 spi_nor_adjust_hwcaps(struct spi_nor *nor,
2668 const struct spi_nor_flash_parameter *params,
2674 * Enable all caps by default. We will mask them after checking what's
2675 * really supported using spi_mem_supports_op().
2677 *hwcaps = SNOR_HWCAPS_ALL;
2679 /* X-X-X modes are not supported yet, mask them all. */
2680 *hwcaps &= ~SNOR_HWCAPS_X_X_X;
2683 * If the reset line is broken, we do not want to enter a stateful
2686 if (nor->flags & SNOR_F_BROKEN_RESET)
2687 *hwcaps &= ~(SNOR_HWCAPS_X_X_X | SNOR_HWCAPS_X_X_X_DTR);
2689 for (cap = 0; cap < sizeof(*hwcaps) * BITS_PER_BYTE; cap++) {
2692 if (!(*hwcaps & BIT(cap)))
2695 rdidx = spi_nor_hwcaps_read2cmd(BIT(cap));
2697 spi_nor_check_readop(nor, ¶ms->reads[rdidx]))
2698 *hwcaps &= ~BIT(cap);
2700 ppidx = spi_nor_hwcaps_pp2cmd(BIT(cap));
2704 if (spi_nor_check_pp(nor, ¶ms->page_programs[ppidx]))
2705 *hwcaps &= ~BIT(cap);
2710 * spi_nor_adjust_hwcaps - Find optimal Read/Write protocol based on SPI
2711 * controller capabilities
2712 * @nor: pointer to a 'struct spi_nor'
2713 * @params: pointer to the 'struct spi_nor_flash_parameter'
2714 * representing SPI NOR flash capabilities
2715 * @hwcaps: pointer to resulting capabilities after adjusting
2716 * according to controller and flash's capability
2718 * Select caps based on what the SPI controller and SPI flash both support.
2721 spi_nor_adjust_hwcaps(struct spi_nor *nor,
2722 const struct spi_nor_flash_parameter *params,
2725 struct spi_slave *spi = nor->spi;
2726 u32 ignored_mask = (SNOR_HWCAPS_READ_2_2_2 |
2727 SNOR_HWCAPS_READ_4_4_4 |
2728 SNOR_HWCAPS_READ_8_8_8 |
2729 SNOR_HWCAPS_PP_4_4_4 |
2730 SNOR_HWCAPS_PP_8_8_8);
2731 u32 spi_hwcaps = (SNOR_HWCAPS_READ | SNOR_HWCAPS_READ_FAST |
2734 /* Get the hardware capabilities the SPI controller supports. */
2735 if (spi->mode & SPI_RX_OCTAL) {
2736 spi_hwcaps |= SNOR_HWCAPS_READ_1_1_8;
2738 if (spi->mode & SPI_TX_OCTAL)
2739 spi_hwcaps |= (SNOR_HWCAPS_READ_1_8_8 |
2740 SNOR_HWCAPS_PP_1_1_8 |
2741 SNOR_HWCAPS_PP_1_8_8);
2742 } else if (spi->mode & SPI_RX_QUAD) {
2743 spi_hwcaps |= SNOR_HWCAPS_READ_1_1_4;
2745 if (spi->mode & SPI_TX_QUAD)
2746 spi_hwcaps |= (SNOR_HWCAPS_READ_1_4_4 |
2747 SNOR_HWCAPS_PP_1_1_4 |
2748 SNOR_HWCAPS_PP_1_4_4);
2749 } else if (spi->mode & SPI_RX_DUAL) {
2750 spi_hwcaps |= SNOR_HWCAPS_READ_1_1_2;
2752 if (spi->mode & SPI_TX_DUAL)
2753 spi_hwcaps |= SNOR_HWCAPS_READ_1_2_2;
2757 * Keep only the hardware capabilities supported by both the SPI
2758 * controller and the SPI flash memory.
2760 *hwcaps = spi_hwcaps & params->hwcaps.mask;
2761 if (*hwcaps & ignored_mask) {
2763 "SPI n-n-n protocols are not supported yet.\n");
2764 *hwcaps &= ~ignored_mask;
2767 #endif /* CONFIG_SPI_FLASH_SMART_HWCAPS */
2769 static int spi_nor_select_read(struct spi_nor *nor,
2770 const struct spi_nor_flash_parameter *params,
2773 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
2774 const struct spi_nor_read_command *read;
2779 cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
2783 read = ¶ms->reads[cmd];
2784 nor->read_opcode = read->opcode;
2785 nor->read_proto = read->proto;
2788 * In the spi-nor framework, we don't need to make the difference
2789 * between mode clock cycles and wait state clock cycles.
2790 * Indeed, the value of the mode clock cycles is used by a QSPI
2791 * flash memory to know whether it should enter or leave its 0-4-4
2792 * (Continuous Read / XIP) mode.
2793 * eXecution In Place is out of the scope of the mtd sub-system.
2794 * Hence we choose to merge both mode and wait state clock cycles
2795 * into the so called dummy clock cycles.
2797 nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
2801 static int spi_nor_select_pp(struct spi_nor *nor,
2802 const struct spi_nor_flash_parameter *params,
2805 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
2806 const struct spi_nor_pp_command *pp;
2811 cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
2815 pp = ¶ms->page_programs[cmd];
2816 nor->program_opcode = pp->opcode;
2817 nor->write_proto = pp->proto;
2821 static int spi_nor_select_erase(struct spi_nor *nor,
2822 const struct flash_info *info)
2824 struct mtd_info *mtd = &nor->mtd;
2826 /* Do nothing if already configured from SFDP. */
2830 #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
2831 /* prefer "small sector" erase if possible */
2832 if (info->flags & SECT_4K) {
2833 nor->erase_opcode = SPINOR_OP_BE_4K;
2834 mtd->erasesize = 4096;
2835 } else if (info->flags & SECT_4K_PMC) {
2836 nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
2837 mtd->erasesize = 4096;
2841 nor->erase_opcode = SPINOR_OP_SE;
2842 mtd->erasesize = info->sector_size;
2847 static int spi_nor_default_setup(struct spi_nor *nor,
2848 const struct flash_info *info,
2849 const struct spi_nor_flash_parameter *params)
2852 bool enable_quad_io;
2855 spi_nor_adjust_hwcaps(nor, params, &shared_mask);
2857 /* Select the (Fast) Read command. */
2858 err = spi_nor_select_read(nor, params, shared_mask);
2861 "can't select read settings supported by both the SPI controller and memory.\n");
2865 /* Select the Page Program command. */
2866 err = spi_nor_select_pp(nor, params, shared_mask);
2869 "can't select write settings supported by both the SPI controller and memory.\n");
2873 /* Select the Sector Erase command. */
2874 err = spi_nor_select_erase(nor, info);
2877 "can't select erase settings supported by both the SPI controller and memory.\n");
2881 /* Enable Quad I/O if needed. */
2882 enable_quad_io = (spi_nor_get_protocol_width(nor->read_proto) == 4 ||
2883 spi_nor_get_protocol_width(nor->write_proto) == 4);
2884 if (enable_quad_io && params->quad_enable)
2885 nor->quad_enable = params->quad_enable;
2887 nor->quad_enable = NULL;
2892 static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
2893 const struct spi_nor_flash_parameter *params)
2898 return nor->setup(nor, info, params);
2901 /** spi_nor_octal_dtr_enable() - enable Octal DTR I/O if needed
2902 * @nor: pointer to a 'struct spi_nor'
2904 * Return: 0 on success, -errno otherwise.
2906 static int spi_nor_octal_dtr_enable(struct spi_nor *nor)
2910 if (!nor->octal_dtr_enable)
2913 if (!(nor->read_proto == SNOR_PROTO_8_8_8_DTR &&
2914 nor->write_proto == SNOR_PROTO_8_8_8_DTR))
2917 ret = nor->octal_dtr_enable(nor);
2921 nor->reg_proto = SNOR_PROTO_8_8_8_DTR;
2926 static int spi_nor_init(struct spi_nor *nor)
2930 err = spi_nor_octal_dtr_enable(nor);
2932 dev_dbg(nor->dev, "Octal DTR mode not supported\n");
2937 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
2938 * with the software protection bits set
2940 if (IS_ENABLED(CONFIG_SPI_FLASH_UNLOCK_ALL) &&
2941 (JEDEC_MFR(nor->info) == SNOR_MFR_ATMEL ||
2942 JEDEC_MFR(nor->info) == SNOR_MFR_INTEL ||
2943 JEDEC_MFR(nor->info) == SNOR_MFR_SST ||
2944 nor->info->flags & SPI_NOR_HAS_LOCK)) {
2947 spi_nor_wait_till_ready(nor);
2950 if (nor->quad_enable) {
2951 err = nor->quad_enable(nor);
2953 dev_dbg(nor->dev, "quad mode not supported\n");
2958 if (nor->addr_width == 4 &&
2959 !(nor->info->flags & SPI_NOR_OCTAL_DTR_READ) &&
2960 (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION) &&
2961 !(nor->info->flags & SPI_NOR_4B_OPCODES)) {
2963 * If the RESET# pin isn't hooked up properly, or the system
2964 * otherwise doesn't perform a reset command in the boot
2965 * sequence, it's impossible to 100% protect against unexpected
2966 * reboots (e.g., crashes). Warn the user (or hopefully, system
2967 * designer) that this is bad.
2969 if (nor->flags & SNOR_F_BROKEN_RESET)
2970 debug("enabling reset hack; may not recover from unexpected reboots\n");
2971 set_4byte(nor, nor->info, 1);
2977 #ifdef CONFIG_SPI_FLASH_SOFT_RESET
2979 * spi_nor_soft_reset() - perform the JEDEC Software Reset sequence
2980 * @nor: the spi_nor structure
2982 * This function can be used to switch from Octal DTR mode to legacy mode on a
2983 * flash that supports it. The soft reset is executed in Octal DTR mode.
2985 * Return: 0 for success, -errno for failure.
2987 static int spi_nor_soft_reset(struct spi_nor *nor)
2989 struct spi_mem_op op;
2991 enum spi_nor_cmd_ext ext;
2993 ext = nor->cmd_ext_type;
2994 nor->cmd_ext_type = SPI_NOR_EXT_REPEAT;
2996 op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_SRSTEN, 0),
2997 SPI_MEM_OP_NO_DUMMY,
2999 SPI_MEM_OP_NO_DATA);
3000 spi_nor_setup_op(nor, &op, SNOR_PROTO_8_8_8_DTR);
3001 ret = spi_mem_exec_op(nor->spi, &op);
3003 dev_warn(nor->dev, "Software reset enable failed: %d\n", ret);
3007 op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_SRST, 0),
3008 SPI_MEM_OP_NO_DUMMY,
3010 SPI_MEM_OP_NO_DATA);
3011 spi_nor_setup_op(nor, &op, SNOR_PROTO_8_8_8_DTR);
3012 ret = spi_mem_exec_op(nor->spi, &op);
3014 dev_warn(nor->dev, "Software reset failed: %d\n", ret);
3019 * Software Reset is not instant, and the delay varies from flash to
3020 * flash. Looking at a few flashes, most range somewhere below 100
3021 * microseconds. So, wait for 200ms just to be sure.
3023 udelay(SPI_NOR_SRST_SLEEP_LEN);
3026 nor->cmd_ext_type = ext;
3029 #endif /* CONFIG_SPI_FLASH_SOFT_RESET */
3031 int spi_nor_remove(struct spi_nor *nor)
3033 #ifdef CONFIG_SPI_FLASH_SOFT_RESET
3034 if (nor->info->flags & SPI_NOR_OCTAL_DTR_READ &&
3035 nor->flags & SNOR_F_SOFT_RESET)
3036 return spi_nor_soft_reset(nor);
3042 void spi_nor_set_fixups(struct spi_nor *nor)
3046 int spi_nor_scan(struct spi_nor *nor)
3048 struct spi_nor_flash_parameter params;
3049 const struct flash_info *info = NULL;
3050 struct mtd_info *mtd = &nor->mtd;
3051 struct spi_slave *spi = nor->spi;
3054 /* Reset SPI protocol for all commands. */
3055 nor->reg_proto = SNOR_PROTO_1_1_1;
3056 nor->read_proto = SNOR_PROTO_1_1_1;
3057 nor->write_proto = SNOR_PROTO_1_1_1;
3058 nor->read = spi_nor_read_data;
3059 nor->write = spi_nor_write_data;
3060 nor->read_reg = spi_nor_read_reg;
3061 nor->write_reg = spi_nor_write_reg;
3063 nor->setup = spi_nor_default_setup;
3065 #ifdef CONFIG_SPI_FLASH_SOFT_RESET_ON_BOOT
3067 * When the flash is handed to us in a stateful mode like 8D-8D-8D, it
3068 * is difficult to detect the mode the flash is in. One option is to
3069 * read SFDP in all modes and see which one gives the correct "SFDP"
3070 * signature, but not all flashes support SFDP in 8D-8D-8D mode.
3072 * Further, even if you detect the mode of the flash via SFDP, you
3073 * still have the problem of actually reading the ID. The Read ID
3074 * command is not standardized across flash vendors. Flashes can have
3075 * different dummy cycles needed for reading the ID. Some flashes even
3076 * expect a 4-byte dummy address with the Read ID command. All this
3077 * information cannot be obtained from the SFDP table.
3079 * So, perform a Software Reset sequence before reading the ID and
3080 * initializing the flash. A Soft Reset will bring back the flash in
3081 * its default protocol mode assuming no non-volatile configuration was
3082 * set. This will let us detect the flash even if ROM hands it to us in
3085 * To accommodate cases where there is more than one flash on a board,
3086 * and only one of them needs a soft reset, failure to reset is not
3087 * made fatal, and we still try to read ID if possible.
3089 spi_nor_soft_reset(nor);
3090 #endif /* CONFIG_SPI_FLASH_SOFT_RESET_ON_BOOT */
3092 info = spi_nor_read_id(nor);
3093 if (IS_ERR_OR_NULL(info))
3097 spi_nor_set_fixups(nor);
3099 /* Parse the Serial Flash Discoverable Parameters table. */
3100 ret = spi_nor_init_params(nor, info, ¶ms);
3105 mtd->name = info->name;
3106 mtd->dev = nor->dev;
3108 mtd->type = MTD_NORFLASH;
3110 mtd->flags = MTD_CAP_NORFLASH;
3111 mtd->size = params.size;
3112 mtd->_erase = spi_nor_erase;
3113 mtd->_read = spi_nor_read;
3114 mtd->_write = spi_nor_write;
3116 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
3117 /* NOR protection support for STmicro/Micron chips and similar */
3118 if (JEDEC_MFR(info) == SNOR_MFR_ST ||
3119 JEDEC_MFR(info) == SNOR_MFR_MICRON ||
3120 JEDEC_MFR(info) == SNOR_MFR_SST ||
3121 info->flags & SPI_NOR_HAS_LOCK) {
3122 nor->flash_lock = stm_lock;
3123 nor->flash_unlock = stm_unlock;
3124 nor->flash_is_locked = stm_is_locked;
3128 #ifdef CONFIG_SPI_FLASH_SST
3130 * sst26 series block protection implementation differs from other
3133 if (info->flags & SPI_NOR_HAS_SST26LOCK) {
3134 nor->flash_lock = sst26_lock;
3135 nor->flash_unlock = sst26_unlock;
3136 nor->flash_is_locked = sst26_is_locked;
3140 if (info->flags & USE_FSR)
3141 nor->flags |= SNOR_F_USE_FSR;
3142 if (info->flags & SPI_NOR_HAS_TB)
3143 nor->flags |= SNOR_F_HAS_SR_TB;
3144 if (info->flags & NO_CHIP_ERASE)
3145 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
3146 if (info->flags & USE_CLSR)
3147 nor->flags |= SNOR_F_USE_CLSR;
3149 if (info->flags & SPI_NOR_NO_ERASE)
3150 mtd->flags |= MTD_NO_ERASE;
3152 nor->page_size = params.page_size;
3153 mtd->writebufsize = nor->page_size;
3155 /* Some devices cannot do fast-read, no matter what DT tells us */
3156 if ((info->flags & SPI_NOR_NO_FR) || (spi->mode & SPI_RX_SLOW))
3157 params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
3160 * Configure the SPI memory:
3161 * - select op codes for (Fast) Read, Page Program and Sector Erase.
3162 * - set the number of dummy cycles (mode cycles + wait states).
3163 * - set the SPI protocols for register and memory accesses.
3164 * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
3166 ret = spi_nor_setup(nor, info, ¶ms);
3170 if (spi_nor_protocol_is_dtr(nor->read_proto)) {
3171 /* Always use 4-byte addresses in DTR mode. */
3172 nor->addr_width = 4;
3173 } else if (nor->addr_width) {
3174 /* already configured from SFDP */
3175 } else if (info->addr_width) {
3176 nor->addr_width = info->addr_width;
3178 nor->addr_width = 3;
3181 if (nor->addr_width == 3 && mtd->size > SZ_16M) {
3182 #ifndef CONFIG_SPI_FLASH_BAR
3183 /* enable 4-byte addressing if the device exceeds 16MiB */
3184 nor->addr_width = 4;
3185 if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
3186 info->flags & SPI_NOR_4B_OPCODES)
3187 spi_nor_set_4byte_opcodes(nor, info);
3189 /* Configure the BAR - discover bank cmds and read current bank */
3190 nor->addr_width = 3;
3191 ret = read_bar(nor, info);
3197 if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
3198 dev_dbg(nor->dev, "address width is too large: %u\n",
3203 /* Send all the required SPI flash commands to initialize device */
3204 ret = spi_nor_init(nor);
3208 nor->rdsr_dummy = params.rdsr_dummy;
3209 nor->rdsr_addr_nbytes = params.rdsr_addr_nbytes;
3210 nor->name = mtd->name;
3211 nor->size = mtd->size;
3212 nor->erase_size = mtd->erasesize;
3213 nor->sector_size = mtd->erasesize;
3215 #ifndef CONFIG_SPL_BUILD
3216 printf("SF: Detected %s with page size ", nor->name);
3217 print_size(nor->page_size, ", erase size ");
3218 print_size(nor->erase_size, ", total ");
3219 print_size(nor->size, "");
3226 /* U-Boot specific functions, need to extend MTD to support these */
3227 int spi_flash_cmd_get_sw_write_prot(struct spi_nor *nor)
3229 int sr = read_sr(nor);
3234 return (sr >> 2) & 7;