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.
9 * Synced from Linux v4.19
13 #include <display_options.h>
17 #include <dm/device_compat.h>
18 #include <dm/devres.h>
19 #include <linux/bitops.h>
20 #include <linux/err.h>
21 #include <linux/errno.h>
22 #include <linux/log2.h>
23 #include <linux/math64.h>
24 #include <linux/sizes.h>
25 #include <linux/bitfield.h>
26 #include <linux/delay.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/mtd/spi-nor.h>
30 #include <mtd/cfi_flash.h>
34 #include "sf_internal.h"
36 /* Define max times to check status register before we give up. */
39 * For everything but full-chip erase; probably could be much smaller, but kept
40 * around for safety for now
43 #define HZ CONFIG_SYS_HZ
45 #define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ)
47 #define ROUND_UP_TO(x, y) (((x) + (y) - 1) / (y) * (y))
49 struct sfdp_parameter_header {
53 u8 length; /* in double words */
54 u8 parameter_table_pointer[3]; /* byte address */
58 #define SFDP_PARAM_HEADER_ID(p) (((p)->id_msb << 8) | (p)->id_lsb)
59 #define SFDP_PARAM_HEADER_PTP(p) \
60 (((p)->parameter_table_pointer[2] << 16) | \
61 ((p)->parameter_table_pointer[1] << 8) | \
62 ((p)->parameter_table_pointer[0] << 0))
64 #define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
65 #define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
66 #define SFDP_SST_ID 0x01bf /* Manufacturer specific Table */
67 #define SFDP_PROFILE1_ID 0xff05 /* xSPI Profile 1.0 Table */
68 #define SFDP_SCCR_MAP_ID 0xff87 /*
69 * Status, Control and Configuration
73 #define SFDP_SIGNATURE 0x50444653U
74 #define SFDP_JESD216_MAJOR 1
75 #define SFDP_JESD216_MINOR 0
76 #define SFDP_JESD216A_MINOR 5
77 #define SFDP_JESD216B_MINOR 6
80 u32 signature; /* Ox50444653U <=> "SFDP" */
83 u8 nph; /* 0-base number of parameter headers */
86 /* Basic Flash Parameter Table. */
87 struct sfdp_parameter_header bfpt_header;
90 /* Basic Flash Parameter Table */
93 * JESD216 rev D defines a Basic Flash Parameter Table of 20 DWORDs.
94 * They are indexed from 1 but C arrays are indexed from 0.
96 #define BFPT_DWORD(i) ((i) - 1)
97 #define BFPT_DWORD_MAX 20
99 /* The first version of JESB216 defined only 9 DWORDs. */
100 #define BFPT_DWORD_MAX_JESD216 9
101 #define BFPT_DWORD_MAX_JESD216B 16
104 #define BFPT_DWORD1_FAST_READ_1_1_2 BIT(16)
105 #define BFPT_DWORD1_ADDRESS_BYTES_MASK GENMASK(18, 17)
106 #define BFPT_DWORD1_ADDRESS_BYTES_3_ONLY (0x0UL << 17)
107 #define BFPT_DWORD1_ADDRESS_BYTES_3_OR_4 (0x1UL << 17)
108 #define BFPT_DWORD1_ADDRESS_BYTES_4_ONLY (0x2UL << 17)
109 #define BFPT_DWORD1_DTR BIT(19)
110 #define BFPT_DWORD1_FAST_READ_1_2_2 BIT(20)
111 #define BFPT_DWORD1_FAST_READ_1_4_4 BIT(21)
112 #define BFPT_DWORD1_FAST_READ_1_1_4 BIT(22)
115 #define BFPT_DWORD5_FAST_READ_2_2_2 BIT(0)
116 #define BFPT_DWORD5_FAST_READ_4_4_4 BIT(4)
119 #define BFPT_DWORD11_PAGE_SIZE_SHIFT 4
120 #define BFPT_DWORD11_PAGE_SIZE_MASK GENMASK(7, 4)
125 * (from JESD216 rev B)
126 * Quad Enable Requirements (QER):
127 * - 000b: Device does not have a QE bit. Device detects 1-1-4 and 1-4-4
128 * reads based on instruction. DQ3/HOLD# functions are hold during
130 * - 001b: QE is bit 1 of status register 2. It is set via Write Status with
131 * two data bytes where bit 1 of the second byte is one.
133 * Writing only one byte to the status register has the side-effect of
134 * clearing status register 2, including the QE bit. The 100b code is
135 * used if writing one byte to the status register does not modify
137 * - 010b: QE is bit 6 of status register 1. It is set via Write Status with
138 * one data byte where bit 6 is one.
140 * - 011b: QE is bit 7 of status register 2. It is set via Write status
141 * register 2 instruction 3Eh with one data byte where bit 7 is one.
143 * The status register 2 is read using instruction 3Fh.
144 * - 100b: QE is bit 1 of status register 2. It is set via Write Status with
145 * two data bytes where bit 1 of the second byte is one.
147 * In contrast to the 001b code, writing one byte to the status
148 * register does not modify status register 2.
149 * - 101b: QE is bit 1 of status register 2. Status register 1 is read using
150 * Read Status instruction 05h. Status register2 is read using
151 * instruction 35h. QE is set via Writ Status instruction 01h with
152 * two data bytes where bit 1 of the second byte is one.
155 #define BFPT_DWORD15_QER_MASK GENMASK(22, 20)
156 #define BFPT_DWORD15_QER_NONE (0x0UL << 20) /* Micron */
157 #define BFPT_DWORD15_QER_SR2_BIT1_BUGGY (0x1UL << 20)
158 #define BFPT_DWORD15_QER_SR1_BIT6 (0x2UL << 20) /* Macronix */
159 #define BFPT_DWORD15_QER_SR2_BIT7 (0x3UL << 20)
160 #define BFPT_DWORD15_QER_SR2_BIT1_NO_RD (0x4UL << 20)
161 #define BFPT_DWORD15_QER_SR2_BIT1 (0x5UL << 20) /* Spansion */
163 #define BFPT_DWORD16_SOFT_RST BIT(12)
164 #define BFPT_DWORD16_EX4B_PWRCYC BIT(21)
166 #define BFPT_DWORD18_CMD_EXT_MASK GENMASK(30, 29)
167 #define BFPT_DWORD18_CMD_EXT_REP (0x0UL << 29) /* Repeat */
168 #define BFPT_DWORD18_CMD_EXT_INV (0x1UL << 29) /* Invert */
169 #define BFPT_DWORD18_CMD_EXT_RES (0x2UL << 29) /* Reserved */
170 #define BFPT_DWORD18_CMD_EXT_16B (0x3UL << 29) /* 16-bit opcode */
172 /* xSPI Profile 1.0 table (from JESD216D.01). */
173 #define PROFILE1_DWORD1_RD_FAST_CMD GENMASK(15, 8)
174 #define PROFILE1_DWORD1_RDSR_DUMMY BIT(28)
175 #define PROFILE1_DWORD1_RDSR_ADDR_BYTES BIT(29)
176 #define PROFILE1_DWORD4_DUMMY_200MHZ GENMASK(11, 7)
177 #define PROFILE1_DWORD5_DUMMY_166MHZ GENMASK(31, 27)
178 #define PROFILE1_DWORD5_DUMMY_133MHZ GENMASK(21, 17)
179 #define PROFILE1_DWORD5_DUMMY_100MHZ GENMASK(11, 7)
180 #define PROFILE1_DUMMY_DEFAULT 20
182 /* Status, Control and Configuration Register Map(SCCR) */
183 #define SCCR_DWORD22_OCTAL_DTR_EN_VOLATILE BIT(31)
186 u32 dwords[BFPT_DWORD_MAX];
190 * struct spi_nor_fixups - SPI NOR fixup hooks
191 * @default_init: called after default flash parameters init. Used to tweak
192 * flash parameters when information provided by the flash_info
193 * table is incomplete or wrong.
194 * @post_bfpt: called after the BFPT table has been parsed
195 * @post_sfdp: called after SFDP has been parsed (is also called for SPI NORs
196 * that do not support RDSFDP). Typically used to tweak various
197 * parameters that could not be extracted by other means (i.e.
198 * when information provided by the SFDP/flash_info tables are
199 * incomplete or wrong).
201 * Those hooks can be used to tweak the SPI NOR configuration when the SFDP
202 * table is broken or not available.
204 struct spi_nor_fixups {
205 void (*default_init)(struct spi_nor *nor);
206 int (*post_bfpt)(struct spi_nor *nor,
207 const struct sfdp_parameter_header *bfpt_header,
208 const struct sfdp_bfpt *bfpt,
209 struct spi_nor_flash_parameter *params);
210 void (*post_sfdp)(struct spi_nor *nor,
211 struct spi_nor_flash_parameter *params);
214 #define SPI_NOR_SRST_SLEEP_LEN 200
217 * spi_nor_get_cmd_ext() - Get the command opcode extension based on the
219 * @nor: pointer to a 'struct spi_nor'
220 * @op: pointer to the 'struct spi_mem_op' whose properties
221 * need to be initialized.
223 * Right now, only "repeat" and "invert" are supported.
225 * Return: The opcode extension.
227 static u8 spi_nor_get_cmd_ext(const struct spi_nor *nor,
228 const struct spi_mem_op *op)
230 switch (nor->cmd_ext_type) {
231 case SPI_NOR_EXT_INVERT:
232 return ~op->cmd.opcode;
234 case SPI_NOR_EXT_REPEAT:
235 return op->cmd.opcode;
238 dev_dbg(nor->dev, "Unknown command extension type\n");
244 * spi_nor_setup_op() - Set up common properties of a spi-mem op.
245 * @nor: pointer to a 'struct spi_nor'
246 * @op: pointer to the 'struct spi_mem_op' whose properties
247 * need to be initialized.
248 * @proto: the protocol from which the properties need to be set.
250 void spi_nor_setup_op(const struct spi_nor *nor,
251 struct spi_mem_op *op,
252 const enum spi_nor_protocol proto)
256 op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(proto);
259 op->addr.buswidth = spi_nor_get_protocol_addr_nbits(proto);
261 if (op->dummy.nbytes)
262 op->dummy.buswidth = spi_nor_get_protocol_addr_nbits(proto);
265 op->data.buswidth = spi_nor_get_protocol_data_nbits(proto);
267 if (spi_nor_protocol_is_dtr(proto)) {
269 * spi-mem supports mixed DTR modes, but right now we can only
270 * have all phases either DTR or STR. IOW, spi-mem can have
271 * something like 4S-4D-4D, but spi-nor can't. So, set all 4
272 * phases to either DTR or STR.
274 op->cmd.dtr = op->addr.dtr = op->dummy.dtr =
277 /* 2 bytes per clock cycle in DTR mode. */
278 op->dummy.nbytes *= 2;
280 ext = spi_nor_get_cmd_ext(nor, op);
281 op->cmd.opcode = (op->cmd.opcode << 8) | ext;
286 static int spi_nor_read_write_reg(struct spi_nor *nor, struct spi_mem_op
289 if (op->data.dir == SPI_MEM_DATA_IN)
290 op->data.buf.in = buf;
292 op->data.buf.out = buf;
293 return spi_mem_exec_op(nor->spi, op);
296 static int spi_nor_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len)
298 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(code, 0),
301 SPI_MEM_OP_DATA_IN(len, NULL, 0));
304 spi_nor_setup_op(nor, &op, nor->reg_proto);
306 ret = spi_nor_read_write_reg(nor, &op, val);
308 dev_dbg(nor->dev, "error %d reading %x\n", ret, code);
313 static int spi_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
315 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 0),
318 SPI_MEM_OP_DATA_OUT(len, NULL, 0));
320 spi_nor_setup_op(nor, &op, nor->reg_proto);
323 op.data.dir = SPI_MEM_NO_DATA;
325 return spi_nor_read_write_reg(nor, &op, buf);
328 #ifdef CONFIG_SPI_FLASH_SPANSION
329 static int spansion_read_any_reg(struct spi_nor *nor, u32 addr, u8 dummy,
332 struct spi_mem_op op =
333 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDAR, 1),
334 SPI_MEM_OP_ADDR(nor->addr_mode_nbytes, addr, 1),
335 SPI_MEM_OP_DUMMY(dummy / 8, 1),
336 SPI_MEM_OP_DATA_IN(1, NULL, 1));
338 return spi_nor_read_write_reg(nor, &op, val);
341 static int spansion_write_any_reg(struct spi_nor *nor, u32 addr, u8 val)
343 struct spi_mem_op op =
344 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRAR, 1),
345 SPI_MEM_OP_ADDR(nor->addr_mode_nbytes, addr, 1),
347 SPI_MEM_OP_DATA_OUT(1, NULL, 1));
349 return spi_nor_read_write_reg(nor, &op, &val);
353 static ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
356 struct spi_mem_op op =
357 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0),
358 SPI_MEM_OP_ADDR(nor->addr_width, from, 0),
359 SPI_MEM_OP_DUMMY(nor->read_dummy, 0),
360 SPI_MEM_OP_DATA_IN(len, buf, 0));
361 size_t remaining = len;
364 spi_nor_setup_op(nor, &op, nor->read_proto);
366 /* convert the dummy cycles to the number of bytes */
367 op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
368 if (spi_nor_protocol_is_dtr(nor->read_proto))
369 op.dummy.nbytes *= 2;
372 op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
374 if (CONFIG_IS_ENABLED(SPI_DIRMAP) && nor->dirmap.rdesc) {
376 * Record current operation information which may be used
377 * when the address or data length exceeds address mapping.
379 memcpy(&nor->dirmap.rdesc->info.op_tmpl, &op,
380 sizeof(struct spi_mem_op));
381 ret = spi_mem_dirmap_read(nor->dirmap.rdesc,
382 op.addr.val, op.data.nbytes,
386 op.data.nbytes = ret;
388 ret = spi_mem_adjust_op_size(nor->spi, &op);
392 ret = spi_mem_exec_op(nor->spi, &op);
397 op.addr.val += op.data.nbytes;
398 remaining -= op.data.nbytes;
399 op.data.buf.in += op.data.nbytes;
405 static ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
408 struct spi_mem_op op =
409 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0),
410 SPI_MEM_OP_ADDR(nor->addr_width, to, 0),
412 SPI_MEM_OP_DATA_OUT(len, buf, 0));
415 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
418 spi_nor_setup_op(nor, &op, nor->write_proto);
420 if (CONFIG_IS_ENABLED(SPI_DIRMAP) && nor->dirmap.wdesc) {
421 memcpy(&nor->dirmap.wdesc->info.op_tmpl, &op,
422 sizeof(struct spi_mem_op));
423 op.data.nbytes = spi_mem_dirmap_write(nor->dirmap.wdesc, op.addr.val,
424 op.data.nbytes, op.data.buf.out);
426 ret = spi_mem_adjust_op_size(nor->spi, &op);
429 op.data.nbytes = len < op.data.nbytes ? len : op.data.nbytes;
431 ret = spi_mem_exec_op(nor->spi, &op);
436 return op.data.nbytes;
440 * Read the status register, returning its value in the location
441 * Return the status register value.
442 * Returns negative if error occurred.
444 static int read_sr(struct spi_nor *nor)
446 struct spi_mem_op op;
449 u8 addr_nbytes, dummy;
451 if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) {
452 addr_nbytes = nor->rdsr_addr_nbytes;
453 dummy = nor->rdsr_dummy;
459 op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDSR, 0),
460 SPI_MEM_OP_ADDR(addr_nbytes, 0, 0),
461 SPI_MEM_OP_DUMMY(dummy, 0),
462 SPI_MEM_OP_DATA_IN(1, NULL, 0));
464 spi_nor_setup_op(nor, &op, nor->reg_proto);
467 * We don't want to read only one byte in DTR mode. So, read 2 and then
468 * discard the second byte.
470 if (spi_nor_protocol_is_dtr(nor->reg_proto))
473 ret = spi_nor_read_write_reg(nor, &op, val);
475 pr_debug("error %d reading SR\n", (int)ret);
483 * Read the flag status register, returning its value in the location
484 * Return the status register value.
485 * Returns negative if error occurred.
487 static int read_fsr(struct spi_nor *nor)
489 struct spi_mem_op op;
492 u8 addr_nbytes, dummy;
494 if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) {
495 addr_nbytes = nor->rdsr_addr_nbytes;
496 dummy = nor->rdsr_dummy;
502 op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDFSR, 0),
503 SPI_MEM_OP_ADDR(addr_nbytes, 0, 0),
504 SPI_MEM_OP_DUMMY(dummy, 0),
505 SPI_MEM_OP_DATA_IN(1, NULL, 0));
507 spi_nor_setup_op(nor, &op, nor->reg_proto);
510 * We don't want to read only one byte in DTR mode. So, read 2 and then
511 * discard the second byte.
513 if (spi_nor_protocol_is_dtr(nor->reg_proto))
516 ret = spi_nor_read_write_reg(nor, &op, val);
518 pr_debug("error %d reading FSR\n", ret);
526 * Read configuration register, returning its value in the
527 * location. Return the configuration register value.
528 * Returns negative if error occurred.
530 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
531 static int read_cr(struct spi_nor *nor)
536 ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
538 dev_dbg(nor->dev, "error %d reading CR\n", ret);
547 * Write status register 1 byte
548 * Returns negative if error occurred.
550 static int write_sr(struct spi_nor *nor, u8 val)
552 nor->cmd_buf[0] = val;
553 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
557 * Set write enable latch with Write Enable command.
558 * Returns negative if error occurred.
560 static int write_enable(struct spi_nor *nor)
562 return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
566 * Send write disable instruction to the chip.
568 static int write_disable(struct spi_nor *nor)
570 return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
573 static struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
578 #ifndef CONFIG_SPI_FLASH_BAR
579 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
583 for (i = 0; i < size; i++)
584 if (table[i][0] == opcode)
587 /* No conversion found, keep input op code. */
591 static u8 spi_nor_convert_3to4_read(u8 opcode)
593 static const u8 spi_nor_3to4_read[][2] = {
594 { SPINOR_OP_READ, SPINOR_OP_READ_4B },
595 { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B },
596 { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
597 { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
598 { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
599 { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
600 { SPINOR_OP_READ_1_1_8, SPINOR_OP_READ_1_1_8_4B },
601 { SPINOR_OP_READ_1_8_8, SPINOR_OP_READ_1_8_8_4B },
603 { SPINOR_OP_READ_1_1_1_DTR, SPINOR_OP_READ_1_1_1_DTR_4B },
604 { SPINOR_OP_READ_1_2_2_DTR, SPINOR_OP_READ_1_2_2_DTR_4B },
605 { SPINOR_OP_READ_1_4_4_DTR, SPINOR_OP_READ_1_4_4_DTR_4B },
608 return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
609 ARRAY_SIZE(spi_nor_3to4_read));
612 static u8 spi_nor_convert_3to4_program(u8 opcode)
614 static const u8 spi_nor_3to4_program[][2] = {
615 { SPINOR_OP_PP, SPINOR_OP_PP_4B },
616 { SPINOR_OP_PP_1_1_4, SPINOR_OP_PP_1_1_4_4B },
617 { SPINOR_OP_PP_1_4_4, SPINOR_OP_PP_1_4_4_4B },
618 { SPINOR_OP_PP_1_1_8, SPINOR_OP_PP_1_1_8_4B },
619 { SPINOR_OP_PP_1_8_8, SPINOR_OP_PP_1_8_8_4B },
622 return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
623 ARRAY_SIZE(spi_nor_3to4_program));
626 static u8 spi_nor_convert_3to4_erase(u8 opcode)
628 static const u8 spi_nor_3to4_erase[][2] = {
629 { SPINOR_OP_BE_4K, SPINOR_OP_BE_4K_4B },
630 { SPINOR_OP_BE_32K, SPINOR_OP_BE_32K_4B },
631 { SPINOR_OP_SE, SPINOR_OP_SE_4B },
634 return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
635 ARRAY_SIZE(spi_nor_3to4_erase));
638 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor,
639 const struct flash_info *info)
641 /* Do some manufacturer fixups first */
642 switch (JEDEC_MFR(info)) {
643 case SNOR_MFR_SPANSION:
644 /* No small sector erase for 4-byte command set */
645 nor->erase_opcode = SPINOR_OP_SE;
646 nor->mtd.erasesize = info->sector_size;
653 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
654 nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
655 nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
657 #endif /* !CONFIG_SPI_FLASH_BAR */
659 /* Enable/disable 4-byte addressing mode. */
660 static int set_4byte(struct spi_nor *nor, const struct flash_info *info,
664 bool need_wren = false;
667 switch (JEDEC_MFR(info)) {
669 case SNOR_MFR_MICRON:
670 /* Some Micron need WREN command; all will accept it */
674 case SNOR_MFR_MACRONIX:
675 case SNOR_MFR_WINBOND:
679 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
680 status = nor->write_reg(nor, cmd, NULL, 0);
684 if (!status && !enable &&
685 JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
687 * On Winbond W25Q256FV, leaving 4byte mode causes
688 * the Extended Address Register to be set to 1, so all
689 * 3-byte-address reads come from the second 16M.
690 * We must clear the register to enable normal behavior.
694 nor->write_reg(nor, SPINOR_OP_WREAR, nor->cmd_buf, 1);
699 case SNOR_MFR_CYPRESS:
700 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B_CYPRESS;
701 return nor->write_reg(nor, cmd, NULL, 0);
704 nor->cmd_buf[0] = enable << 7;
705 return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
709 #ifdef CONFIG_SPI_FLASH_SPANSION
711 * Read status register 1 by using Read Any Register command to support multi
714 static int spansion_sr_ready(struct spi_nor *nor, u32 addr_base, u8 dummy)
716 u32 reg_addr = addr_base + SPINOR_REG_ADDR_STR1V;
720 ret = spansion_read_any_reg(nor, reg_addr, dummy, &sr);
724 if (sr & (SR_E_ERR | SR_P_ERR)) {
726 dev_dbg(nor->dev, "Erase Error occurred\n");
728 dev_dbg(nor->dev, "Programming Error occurred\n");
730 nor->write_reg(nor, SPINOR_OP_CLSR, NULL, 0);
734 return !(sr & SR_WIP);
738 static int spi_nor_sr_ready(struct spi_nor *nor)
740 int sr = read_sr(nor);
745 if (nor->flags & SNOR_F_USE_CLSR && sr & (SR_E_ERR | SR_P_ERR)) {
747 dev_dbg(nor->dev, "Erase Error occurred\n");
749 dev_dbg(nor->dev, "Programming Error occurred\n");
751 nor->write_reg(nor, SPINOR_OP_CLSR, NULL, 0);
755 return !(sr & SR_WIP);
758 static int spi_nor_fsr_ready(struct spi_nor *nor)
760 int fsr = read_fsr(nor);
765 if (fsr & (FSR_E_ERR | FSR_P_ERR)) {
767 dev_err(nor->dev, "Erase operation failed.\n");
769 dev_err(nor->dev, "Program operation failed.\n");
771 if (fsr & FSR_PT_ERR)
773 "Attempted to modify a protected sector.\n");
775 nor->write_reg(nor, SPINOR_OP_CLFSR, NULL, 0);
779 return fsr & FSR_READY;
782 static int spi_nor_default_ready(struct spi_nor *nor)
786 sr = spi_nor_sr_ready(nor);
789 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
795 static int spi_nor_ready(struct spi_nor *nor)
798 return nor->ready(nor);
800 return spi_nor_default_ready(nor);
804 * Service routine to read status register until ready, or timeout occurs.
805 * Returns non-zero if error.
807 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
808 unsigned long timeout)
810 unsigned long timebase;
813 timebase = get_timer(0);
815 while (get_timer(timebase) < timeout) {
816 ret = spi_nor_ready(nor);
823 dev_err(nor->dev, "flash operation timed out\n");
828 static int spi_nor_wait_till_ready(struct spi_nor *nor)
830 return spi_nor_wait_till_ready_with_timeout(nor,
831 DEFAULT_READY_WAIT_JIFFIES);
834 #ifdef CONFIG_SPI_FLASH_BAR
836 * This "clean_bar" is necessary in a situation when one was accessing
837 * spi flash memory > 16 MiB by using Bank Address Register's BA24 bit.
839 * After it the BA24 bit shall be cleared to allow access to correct
840 * memory region after SW reset (by calling "reset" command).
842 * Otherwise, the BA24 bit may be left set and then after reset, the
843 * ROM would read/write/erase SPL from 16 MiB * bank_sel address.
845 static int clean_bar(struct spi_nor *nor)
847 u8 cmd, bank_sel = 0;
849 if (nor->bank_curr == 0)
851 cmd = nor->bank_write_cmd;
855 return nor->write_reg(nor, cmd, &bank_sel, 1);
858 static int write_bar(struct spi_nor *nor, u32 offset)
863 bank_sel = offset / SZ_16M;
864 if (bank_sel == nor->bank_curr)
867 cmd = nor->bank_write_cmd;
869 ret = nor->write_reg(nor, cmd, &bank_sel, 1);
871 debug("SF: fail to write bank register\n");
876 nor->bank_curr = bank_sel;
877 return nor->bank_curr;
880 static int read_bar(struct spi_nor *nor, const struct flash_info *info)
885 switch (JEDEC_MFR(info)) {
886 case SNOR_MFR_SPANSION:
887 nor->bank_read_cmd = SPINOR_OP_BRRD;
888 nor->bank_write_cmd = SPINOR_OP_BRWR;
891 nor->bank_read_cmd = SPINOR_OP_RDEAR;
892 nor->bank_write_cmd = SPINOR_OP_WREAR;
895 ret = nor->read_reg(nor, nor->bank_read_cmd,
898 debug("SF: fail to read bank addr register\n");
901 nor->bank_curr = curr_bank;
908 * spi_nor_erase_chip() - Erase the entire flash memory.
909 * @nor: pointer to 'struct spi_nor'.
911 * Return: 0 on success, -errno otherwise.
913 static int spi_nor_erase_chip(struct spi_nor *nor)
915 struct spi_mem_op op =
916 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_CHIP_ERASE, 0),
922 spi_nor_setup_op(nor, &op, nor->write_proto);
924 ret = spi_mem_exec_op(nor->spi, &op);
928 return nor->mtd.size;
932 * Initiate the erasure of a single sector. Returns the number of bytes erased
933 * on success, a negative error code on error.
935 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
937 struct spi_mem_op op =
938 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 0),
939 SPI_MEM_OP_ADDR(nor->addr_width, addr, 0),
944 spi_nor_setup_op(nor, &op, nor->write_proto);
947 return nor->erase(nor, addr);
950 * Default implementation, if driver doesn't have a specialized HW
953 ret = spi_mem_exec_op(nor->spi, &op);
957 return nor->mtd.erasesize;
961 * Erase an address range on the nor chip. The address range may extend
962 * one or more erase sectors. Return an error is there is a problem erasing.
964 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
966 struct spi_nor *nor = mtd_to_spi_nor(mtd);
967 bool addr_known = false;
971 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
972 (long long)instr->len);
974 div_u64_rem(instr->len, mtd->erasesize, &rem);
983 instr->state = MTD_ERASING;
988 if (!IS_ENABLED(CONFIG_SPL_BUILD) && ctrlc()) {
993 #ifdef CONFIG_SPI_FLASH_BAR
994 ret = write_bar(nor, addr);
998 ret = write_enable(nor);
1002 if (len == mtd->size &&
1003 !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) {
1004 ret = spi_nor_erase_chip(nor);
1006 ret = spi_nor_erase_sector(nor, addr);
1014 ret = spi_nor_wait_till_ready(nor);
1021 #ifdef CONFIG_SPI_FLASH_BAR
1022 err = clean_bar(nor);
1026 err = write_disable(nor);
1032 instr->fail_addr = addr_known ? addr : MTD_FAIL_ADDR_UNKNOWN;
1033 instr->state = MTD_ERASE_FAILED;
1035 instr->state = MTD_ERASE_DONE;
1041 #ifdef CONFIG_SPI_FLASH_SPANSION
1043 * spansion_erase_non_uniform() - erase non-uniform sectors for Spansion/Cypress
1045 * @nor: pointer to a 'struct spi_nor'
1046 * @addr: address of the sector to erase
1047 * @opcode_4k: opcode for 4K sector erase
1048 * @ovlsz_top: size of overlaid portion at the top address
1049 * @ovlsz_btm: size of overlaid portion at the bottom address
1051 * Erase an address range on the nor chip that can contain 4KB sectors overlaid
1052 * on top and/or bottom. The appropriate erase opcode and size are chosen by
1053 * address to erase and size of overlaid portion.
1055 * Return: number of bytes erased on success, -errno otherwise.
1057 static int spansion_erase_non_uniform(struct spi_nor *nor, u32 addr,
1058 u8 opcode_4k, u32 ovlsz_top,
1061 struct spi_mem_op op =
1062 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 0),
1063 SPI_MEM_OP_ADDR(nor->addr_width, addr, 0),
1064 SPI_MEM_OP_NO_DUMMY,
1065 SPI_MEM_OP_NO_DATA);
1066 struct mtd_info *mtd = &nor->mtd;
1071 if (op.addr.val < ovlsz_btm ||
1072 op.addr.val >= mtd->size - ovlsz_top) {
1073 op.cmd.opcode = opcode_4k;
1076 /* Non-overlaid portion in the normal sector at the bottom */
1077 } else if (op.addr.val == ovlsz_btm) {
1078 op.cmd.opcode = nor->erase_opcode;
1079 erasesize = mtd->erasesize - ovlsz_btm;
1081 /* Non-overlaid portion in the normal sector at the top */
1082 } else if (op.addr.val == mtd->size - mtd->erasesize) {
1083 op.cmd.opcode = nor->erase_opcode;
1084 erasesize = mtd->erasesize - ovlsz_top;
1086 /* Normal sectors */
1088 op.cmd.opcode = nor->erase_opcode;
1089 erasesize = mtd->erasesize;
1092 spi_nor_setup_op(nor, &op, nor->write_proto);
1094 ret = spi_mem_exec_op(nor->spi, &op);
1102 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
1103 /* Write status register and ensure bits in mask match written values */
1104 static int write_sr_and_check(struct spi_nor *nor, u8 status_new, u8 mask)
1109 ret = write_sr(nor, status_new);
1113 ret = spi_nor_wait_till_ready(nor);
1121 return ((ret & mask) != (status_new & mask)) ? -EIO : 0;
1124 static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
1127 struct mtd_info *mtd = &nor->mtd;
1128 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1129 int shift = ffs(mask) - 1;
1137 pow = ((sr & mask) ^ mask) >> shift;
1138 *len = mtd->size >> pow;
1139 if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
1142 *ofs = mtd->size - *len;
1147 * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
1148 * @locked is false); 0 otherwise
1150 static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, u64 len,
1159 stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
1162 /* Requested range is a sub-range of locked range */
1163 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
1165 /* Requested range does not overlap with locked range */
1166 return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
1169 static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
1172 return stm_check_lock_status_sr(nor, ofs, len, sr, true);
1175 static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
1178 return stm_check_lock_status_sr(nor, ofs, len, sr, false);
1182 * Lock a region of the flash. Compatible with ST Micro and similar flash.
1183 * Supports the block protection bits BP{0,1,2} in the status register
1184 * (SR). Does not support these features found in newer SR bitfields:
1185 * - SEC: sector/block protect - only handle SEC=0 (block protect)
1186 * - CMP: complement protect - only support CMP=0 (range is not complemented)
1188 * Support for the following is provided conditionally for some flash:
1189 * - TB: top/bottom protect
1191 * Sample table portion for 8MB flash (Winbond w25q64fw):
1193 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
1194 * --------------------------------------------------------------------------
1195 * X | X | 0 | 0 | 0 | NONE | NONE
1196 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
1197 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
1198 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
1199 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
1200 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
1201 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
1202 * X | X | 1 | 1 | 1 | 8 MB | ALL
1203 * ------|-------|-------|-------|-------|---------------|-------------------
1204 * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
1205 * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
1206 * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
1207 * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
1208 * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
1209 * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
1211 * Returns negative on errors, 0 on success.
1213 static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1215 struct mtd_info *mtd = &nor->mtd;
1216 int status_old, status_new;
1217 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1218 u8 shift = ffs(mask) - 1, pow, val;
1220 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
1223 status_old = read_sr(nor);
1227 /* If nothing in our range is unlocked, we don't need to do anything */
1228 if (stm_is_locked_sr(nor, ofs, len, status_old))
1231 /* If anything below us is unlocked, we can't use 'bottom' protection */
1232 if (!stm_is_locked_sr(nor, 0, ofs, status_old))
1233 can_be_bottom = false;
1235 /* If anything above us is unlocked, we can't use 'top' protection */
1236 if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
1240 if (!can_be_bottom && !can_be_top)
1243 /* Prefer top, if both are valid */
1244 use_top = can_be_top;
1246 /* lock_len: length of region that should end up locked */
1248 lock_len = mtd->size - ofs;
1250 lock_len = ofs + len;
1253 * Need smallest pow such that:
1255 * 1 / (2^pow) <= (len / size)
1257 * so (assuming power-of-2 size) we do:
1259 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
1261 pow = ilog2(mtd->size) - ilog2(lock_len);
1262 val = mask - (pow << shift);
1265 /* Don't "lock" with no region! */
1269 status_new = (status_old & ~mask & ~SR_TB) | val;
1271 /* Disallow further writes if WP pin is asserted */
1272 status_new |= SR_SRWD;
1275 status_new |= SR_TB;
1277 /* Don't bother if they're the same */
1278 if (status_new == status_old)
1281 /* Only modify protection if it will not unlock other areas */
1282 if ((status_new & mask) < (status_old & mask))
1285 return write_sr_and_check(nor, status_new, mask);
1289 * Unlock a region of the flash. See stm_lock() for more info
1291 * Returns negative on errors, 0 on success.
1293 static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1295 struct mtd_info *mtd = &nor->mtd;
1296 int status_old, status_new;
1297 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1298 u8 shift = ffs(mask) - 1, pow, val;
1300 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
1303 status_old = read_sr(nor);
1307 /* If nothing in our range is locked, we don't need to do anything */
1308 if (stm_is_unlocked_sr(nor, ofs, len, status_old))
1311 /* If anything below us is locked, we can't use 'top' protection */
1312 if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
1315 /* If anything above us is locked, we can't use 'bottom' protection */
1316 if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
1318 can_be_bottom = false;
1320 if (!can_be_bottom && !can_be_top)
1323 /* Prefer top, if both are valid */
1324 use_top = can_be_top;
1326 /* lock_len: length of region that should remain locked */
1328 lock_len = mtd->size - (ofs + len);
1333 * Need largest pow such that:
1335 * 1 / (2^pow) >= (len / size)
1337 * so (assuming power-of-2 size) we do:
1339 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
1341 pow = ilog2(mtd->size) - order_base_2(lock_len);
1342 if (lock_len == 0) {
1343 val = 0; /* fully unlocked */
1345 val = mask - (pow << shift);
1346 /* Some power-of-two sizes are not supported */
1351 status_new = (status_old & ~mask & ~SR_TB) | val;
1353 /* Don't protect status register if we're fully unlocked */
1355 status_new &= ~SR_SRWD;
1358 status_new |= SR_TB;
1360 /* Don't bother if they're the same */
1361 if (status_new == status_old)
1364 /* Only modify protection if it will not lock other areas */
1365 if ((status_new & mask) > (status_old & mask))
1368 return write_sr_and_check(nor, status_new, mask);
1372 * Check if a region of the flash is (completely) unlocked. See stm_lock() for
1375 * Returns 1 if entire region is unlocked, 0 if any portion is locked, and
1376 * negative on errors.
1378 static int stm_is_unlocked(struct spi_nor *nor, loff_t ofs, uint64_t len)
1382 status = read_sr(nor);
1386 return stm_is_unlocked_sr(nor, ofs, len, status);
1388 #endif /* CONFIG_SPI_FLASH_STMICRO */
1390 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
1393 u8 id[SPI_NOR_MAX_ID_LEN];
1394 const struct flash_info *info;
1396 tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
1398 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
1399 return ERR_PTR(tmp);
1403 for (; info->name; info++) {
1405 if (!memcmp(info->id, id, info->id_len))
1410 dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
1411 id[0], id[1], id[2]);
1412 return ERR_PTR(-ENODEV);
1415 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
1416 size_t *retlen, u_char *buf)
1418 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1421 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
1425 size_t read_len = len;
1427 #ifdef CONFIG_SPI_FLASH_BAR
1430 ret = write_bar(nor, addr);
1432 return log_ret(ret);
1433 remain_len = (SZ_16M * (nor->bank_curr + 1)) - addr;
1435 if (len < remain_len)
1438 read_len = remain_len;
1441 ret = nor->read(nor, addr, read_len, buf);
1443 /* We shouldn't see 0-length reads */
1458 #ifdef CONFIG_SPI_FLASH_BAR
1459 ret = clean_bar(nor);
1464 #ifdef CONFIG_SPI_FLASH_SST
1466 * sst26 flash series has its own block protection implementation:
1467 * 4x - 8 KByte blocks - read & write protection bits - upper addresses
1468 * 1x - 32 KByte blocks - write protection bits
1469 * rest - 64 KByte blocks - write protection bits
1470 * 1x - 32 KByte blocks - write protection bits
1471 * 4x - 8 KByte blocks - read & write protection bits - lower addresses
1473 * We'll support only per 64k lock/unlock so lower and upper 64 KByte region
1474 * will be treated as single block.
1476 #define SST26_BPR_8K_NUM 4
1477 #define SST26_MAX_BPR_REG_LEN (18 + 1)
1478 #define SST26_BOUND_REG_SIZE ((32 + SST26_BPR_8K_NUM * 8) * SZ_1K)
1486 static bool sst26_process_bpr(u32 bpr_size, u8 *cmd, u32 bit, enum lock_ctl ctl)
1489 case SST26_CTL_LOCK:
1490 cmd[bpr_size - (bit / 8) - 1] |= BIT(bit % 8);
1492 case SST26_CTL_UNLOCK:
1493 cmd[bpr_size - (bit / 8) - 1] &= ~BIT(bit % 8);
1495 case SST26_CTL_CHECK:
1496 return !!(cmd[bpr_size - (bit / 8) - 1] & BIT(bit % 8));
1503 * Lock, unlock or check lock status of the flash region of the flash (depending
1504 * on the lock_ctl value)
1506 static int sst26_lock_ctl(struct spi_nor *nor, loff_t ofs, uint64_t len, enum lock_ctl ctl)
1508 struct mtd_info *mtd = &nor->mtd;
1509 u32 i, bpr_ptr, rptr_64k, lptr_64k, bpr_size;
1510 bool lower_64k = false, upper_64k = false;
1511 u8 bpr_buff[SST26_MAX_BPR_REG_LEN] = {};
1514 /* Check length and offset for 64k alignment */
1515 if ((ofs & (SZ_64K - 1)) || (len & (SZ_64K - 1))) {
1516 dev_err(nor->dev, "length or offset is not 64KiB allighned\n");
1520 if (ofs + len > mtd->size) {
1521 dev_err(nor->dev, "range is more than device size: %#llx + %#llx > %#llx\n",
1522 ofs, len, mtd->size);
1526 /* SST26 family has only 16 Mbit, 32 Mbit and 64 Mbit IC */
1527 if (mtd->size != SZ_2M &&
1528 mtd->size != SZ_4M &&
1532 bpr_size = 2 + (mtd->size / SZ_64K / 8);
1534 ret = nor->read_reg(nor, SPINOR_OP_READ_BPR, bpr_buff, bpr_size);
1536 dev_err(nor->dev, "fail to read block-protection register\n");
1540 rptr_64k = min_t(u32, ofs + len, mtd->size - SST26_BOUND_REG_SIZE);
1541 lptr_64k = max_t(u32, ofs, SST26_BOUND_REG_SIZE);
1543 upper_64k = ((ofs + len) > (mtd->size - SST26_BOUND_REG_SIZE));
1544 lower_64k = (ofs < SST26_BOUND_REG_SIZE);
1546 /* Lower bits in block-protection register are about 64k region */
1547 bpr_ptr = lptr_64k / SZ_64K - 1;
1549 /* Process 64K blocks region */
1550 while (lptr_64k < rptr_64k) {
1551 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1558 /* 32K and 8K region bits in BPR are after 64k region bits */
1559 bpr_ptr = (mtd->size - 2 * SST26_BOUND_REG_SIZE) / SZ_64K;
1561 /* Process lower 32K block region */
1563 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1568 /* Process upper 32K block region */
1570 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1575 /* Process lower 8K block regions */
1576 for (i = 0; i < SST26_BPR_8K_NUM; i++) {
1578 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1581 /* In 8K area BPR has both read and write protection bits */
1585 /* Process upper 8K block regions */
1586 for (i = 0; i < SST26_BPR_8K_NUM; i++) {
1588 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1591 /* In 8K area BPR has both read and write protection bits */
1595 /* If we check region status we don't need to write BPR back */
1596 if (ctl == SST26_CTL_CHECK)
1599 ret = nor->write_reg(nor, SPINOR_OP_WRITE_BPR, bpr_buff, bpr_size);
1601 dev_err(nor->dev, "fail to write block-protection register\n");
1608 static int sst26_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1610 return sst26_lock_ctl(nor, ofs, len, SST26_CTL_UNLOCK);
1613 static int sst26_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1615 return sst26_lock_ctl(nor, ofs, len, SST26_CTL_LOCK);
1619 * Returns EACCES (positive value) if region is (partially) locked, 0 if region
1620 * is completely unlocked, and negative on errors.
1622 static int sst26_is_unlocked(struct spi_nor *nor, loff_t ofs, uint64_t len)
1625 * is_unlocked function is used for check before reading or erasing
1626 * flash region, so offset and length might be not 64k aligned, so
1627 * adjust them to be 64k aligned as sst26_lock_ctl works only with 64k
1630 ofs -= ofs & (SZ_64K - 1);
1631 len = len & (SZ_64K - 1) ? (len & ~(SZ_64K - 1)) + SZ_64K : len;
1633 return !sst26_lock_ctl(nor, ofs, len, SST26_CTL_CHECK);
1636 static int sst_write_byteprogram(struct spi_nor *nor, loff_t to, size_t len,
1637 size_t *retlen, const u_char *buf)
1642 for (actual = 0; actual < len; actual++) {
1643 nor->program_opcode = SPINOR_OP_BP;
1646 /* write one byte. */
1647 ret = nor->write(nor, to, 1, buf + actual);
1650 ret = spi_nor_wait_till_ready(nor);
1661 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
1662 size_t *retlen, const u_char *buf)
1664 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1665 struct spi_slave *spi = nor->spi;
1669 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1670 if (spi->mode & SPI_TX_BYTE)
1671 return sst_write_byteprogram(nor, to, len, retlen, buf);
1675 nor->sst_write_second = false;
1678 /* Start write from odd address. */
1680 nor->program_opcode = SPINOR_OP_BP;
1682 /* write one byte. */
1683 ret = nor->write(nor, to, 1, buf);
1686 ret = spi_nor_wait_till_ready(nor);
1692 /* Write out most of the data here. */
1693 for (; actual < len - 1; actual += 2) {
1694 nor->program_opcode = SPINOR_OP_AAI_WP;
1696 /* write two bytes. */
1697 ret = nor->write(nor, to, 2, buf + actual);
1700 ret = spi_nor_wait_till_ready(nor);
1704 nor->sst_write_second = true;
1706 nor->sst_write_second = false;
1709 ret = spi_nor_wait_till_ready(nor);
1713 /* Write out trailing byte if it exists. */
1714 if (actual != len) {
1717 nor->program_opcode = SPINOR_OP_BP;
1718 ret = nor->write(nor, to, 1, buf + actual);
1721 ret = spi_nor_wait_till_ready(nor);
1733 * Write an address range to the nor chip. Data must be written in
1734 * FLASH_PAGESIZE chunks. The address range may be any size provided
1735 * it is within the physical boundaries.
1737 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
1738 size_t *retlen, const u_char *buf)
1740 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1741 size_t page_offset, page_remain, i;
1744 #ifdef CONFIG_SPI_FLASH_SST
1745 /* sst nor chips use AAI word program */
1746 if (nor->info->flags & SST_WRITE)
1747 return sst_write(mtd, to, len, retlen, buf);
1750 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1752 for (i = 0; i < len; ) {
1754 loff_t addr = to + i;
1758 * If page_size is a power of two, the offset can be quickly
1759 * calculated with an AND operation. On the other cases we
1760 * need to do a modulus operation (more expensive).
1762 if (is_power_of_2(nor->page_size)) {
1763 page_offset = addr & (nor->page_size - 1);
1767 page_offset = do_div(aux, nor->page_size);
1769 /* the size of data remaining on the first page */
1770 page_remain = min_t(size_t,
1771 nor->page_size - page_offset, len - i);
1773 #ifdef CONFIG_SPI_FLASH_BAR
1774 ret = write_bar(nor, addr);
1779 ret = nor->write(nor, addr, page_remain, buf + i);
1784 ret = spi_nor_wait_till_ready(nor);
1792 #ifdef CONFIG_SPI_FLASH_BAR
1793 ret = clean_bar(nor);
1798 #if defined(CONFIG_SPI_FLASH_MACRONIX) || defined(CONFIG_SPI_FLASH_ISSI)
1800 * macronix_quad_enable() - set QE bit in Status Register.
1801 * @nor: pointer to a 'struct spi_nor'
1803 * Set the Quad Enable (QE) bit in the Status Register.
1805 * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
1807 * Return: 0 on success, -errno otherwise.
1809 static int macronix_quad_enable(struct spi_nor *nor)
1816 if (val & SR_QUAD_EN_MX)
1821 write_sr(nor, val | SR_QUAD_EN_MX);
1823 ret = spi_nor_wait_till_ready(nor);
1828 if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
1829 dev_err(nor->dev, "Macronix Quad bit not set\n");
1837 #ifdef CONFIG_SPI_FLASH_SPANSION
1839 * spansion_quad_enable_volatile() - enable Quad I/O mode in volatile register.
1840 * @nor: pointer to a 'struct spi_nor'
1841 * @addr_base: base address of register (can be >0 in multi-die parts)
1842 * @dummy: number of dummy cycles for register read
1844 * It is recommended to update volatile registers in the field application due
1845 * to a risk of the non-volatile registers corruption by power interrupt. This
1846 * function sets Quad Enable bit in CFR1 volatile.
1848 * Return: 0 on success, -errno otherwise.
1850 static int spansion_quad_enable_volatile(struct spi_nor *nor, u32 addr_base,
1853 u32 addr = addr_base + SPINOR_REG_ADDR_CFR1V;
1858 /* Check current Quad Enable bit value. */
1859 ret = spansion_read_any_reg(nor, addr, dummy, &cr);
1862 "error while reading configuration register\n");
1866 if (cr & CR_QUAD_EN_SPAN)
1869 cr |= CR_QUAD_EN_SPAN;
1873 ret = spansion_write_any_reg(nor, addr, cr);
1877 "error while writing configuration register\n");
1881 /* Read back and check it. */
1882 ret = spansion_read_any_reg(nor, addr, dummy, &cr);
1883 if (ret || !(cr & CR_QUAD_EN_SPAN)) {
1884 dev_dbg(nor->dev, "Spansion Quad bit not set\n");
1892 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
1894 * Write status Register and configuration register with 2 bytes
1895 * The first byte will be written to the status register, while the
1896 * second byte will be written to the configuration register.
1897 * Return negative if error occurred.
1899 static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
1905 ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
1908 "error while writing configuration register\n");
1912 ret = spi_nor_wait_till_ready(nor);
1915 "timeout while writing configuration register\n");
1923 * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
1924 * @nor: pointer to a 'struct spi_nor'
1926 * Set the Quad Enable (QE) bit in the Configuration Register.
1927 * This function should be used with QSPI memories supporting the Read
1928 * Configuration Register (35h) instruction.
1930 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1933 * Return: 0 on success, -errno otherwise.
1935 static int spansion_read_cr_quad_enable(struct spi_nor *nor)
1940 /* Check current Quad Enable bit value. */
1944 "error while reading configuration register\n");
1948 if (ret & CR_QUAD_EN_SPAN)
1951 sr_cr[1] = ret | CR_QUAD_EN_SPAN;
1953 /* Keep the current value of the Status Register. */
1956 dev_dbg(nor->dev, "error while reading status register\n");
1961 ret = write_sr_cr(nor, sr_cr);
1965 /* Read back and check it. */
1967 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
1968 dev_dbg(nor->dev, "Spansion Quad bit not set\n");
1975 #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT)
1977 * spansion_no_read_cr_quad_enable() - set QE bit in Configuration Register.
1978 * @nor: pointer to a 'struct spi_nor'
1980 * Set the Quad Enable (QE) bit in the Configuration Register.
1981 * This function should be used with QSPI memories not supporting the Read
1982 * Configuration Register (35h) instruction.
1984 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1987 * Return: 0 on success, -errno otherwise.
1989 static int spansion_no_read_cr_quad_enable(struct spi_nor *nor)
1994 /* Keep the current value of the Status Register. */
1997 dev_dbg(nor->dev, "error while reading status register\n");
2001 sr_cr[1] = CR_QUAD_EN_SPAN;
2003 return write_sr_cr(nor, sr_cr);
2006 #endif /* CONFIG_SPI_FLASH_SFDP_SUPPORT */
2007 #endif /* CONFIG_SPI_FLASH_SPANSION */
2010 spi_nor_set_read_settings(struct spi_nor_read_command *read,
2014 enum spi_nor_protocol proto)
2016 read->num_mode_clocks = num_mode_clocks;
2017 read->num_wait_states = num_wait_states;
2018 read->opcode = opcode;
2019 read->proto = proto;
2023 spi_nor_set_pp_settings(struct spi_nor_pp_command *pp,
2025 enum spi_nor_protocol proto)
2027 pp->opcode = opcode;
2031 #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT)
2033 * Serial Flash Discoverable Parameters (SFDP) parsing.
2037 * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
2038 * @nor: pointer to a 'struct spi_nor'
2039 * @addr: offset in the SFDP area to start reading data from
2040 * @len: number of bytes to read
2041 * @buf: buffer where the SFDP data are copied into (dma-safe memory)
2043 * Whatever the actual numbers of bytes for address and dummy cycles are
2044 * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
2045 * followed by a 3-byte address and 8 dummy clock cycles.
2047 * Return: 0 on success, -errno otherwise.
2049 static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr,
2050 size_t len, void *buf)
2052 u8 addr_width, read_opcode, read_dummy;
2055 read_opcode = nor->read_opcode;
2056 addr_width = nor->addr_width;
2057 read_dummy = nor->read_dummy;
2059 nor->read_opcode = SPINOR_OP_RDSFDP;
2060 nor->addr_width = 3;
2061 nor->read_dummy = 8;
2064 ret = nor->read(nor, addr, len, (u8 *)buf);
2065 if (!ret || ret > len) {
2079 nor->read_opcode = read_opcode;
2080 nor->addr_width = addr_width;
2081 nor->read_dummy = read_dummy;
2086 /* Fast Read settings. */
2089 spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read,
2091 enum spi_nor_protocol proto)
2093 read->num_mode_clocks = (half >> 5) & 0x07;
2094 read->num_wait_states = (half >> 0) & 0x1f;
2095 read->opcode = (half >> 8) & 0xff;
2096 read->proto = proto;
2099 struct sfdp_bfpt_read {
2100 /* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
2104 * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us
2105 * whether the Fast Read x-y-z command is supported.
2107 u32 supported_dword;
2111 * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
2112 * encodes the op code, the number of mode clocks and the number of wait
2113 * states to be used by Fast Read x-y-z command.
2118 /* The SPI protocol for this Fast Read x-y-z command. */
2119 enum spi_nor_protocol proto;
2122 static const struct sfdp_bfpt_read sfdp_bfpt_reads[] = {
2123 /* Fast Read 1-1-2 */
2125 SNOR_HWCAPS_READ_1_1_2,
2126 BFPT_DWORD(1), BIT(16), /* Supported bit */
2127 BFPT_DWORD(4), 0, /* Settings */
2131 /* Fast Read 1-2-2 */
2133 SNOR_HWCAPS_READ_1_2_2,
2134 BFPT_DWORD(1), BIT(20), /* Supported bit */
2135 BFPT_DWORD(4), 16, /* Settings */
2139 /* Fast Read 2-2-2 */
2141 SNOR_HWCAPS_READ_2_2_2,
2142 BFPT_DWORD(5), BIT(0), /* Supported bit */
2143 BFPT_DWORD(6), 16, /* Settings */
2147 /* Fast Read 1-1-4 */
2149 SNOR_HWCAPS_READ_1_1_4,
2150 BFPT_DWORD(1), BIT(22), /* Supported bit */
2151 BFPT_DWORD(3), 16, /* Settings */
2155 /* Fast Read 1-4-4 */
2157 SNOR_HWCAPS_READ_1_4_4,
2158 BFPT_DWORD(1), BIT(21), /* Supported bit */
2159 BFPT_DWORD(3), 0, /* Settings */
2163 /* Fast Read 4-4-4 */
2165 SNOR_HWCAPS_READ_4_4_4,
2166 BFPT_DWORD(5), BIT(4), /* Supported bit */
2167 BFPT_DWORD(7), 16, /* Settings */
2172 struct sfdp_bfpt_erase {
2174 * The half-word at offset <shift> in DWORD <dwoard> encodes the
2175 * op code and erase sector size to be used by Sector Erase commands.
2181 static const struct sfdp_bfpt_erase sfdp_bfpt_erases[] = {
2182 /* Erase Type 1 in DWORD8 bits[15:0] */
2185 /* Erase Type 2 in DWORD8 bits[31:16] */
2186 {BFPT_DWORD(8), 16},
2188 /* Erase Type 3 in DWORD9 bits[15:0] */
2191 /* Erase Type 4 in DWORD9 bits[31:16] */
2192 {BFPT_DWORD(9), 16},
2195 static int spi_nor_hwcaps_read2cmd(u32 hwcaps);
2198 spi_nor_post_bfpt_fixups(struct spi_nor *nor,
2199 const struct sfdp_parameter_header *bfpt_header,
2200 const struct sfdp_bfpt *bfpt,
2201 struct spi_nor_flash_parameter *params)
2203 if (nor->fixups && nor->fixups->post_bfpt)
2204 return nor->fixups->post_bfpt(nor, bfpt_header, bfpt, params);
2210 * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
2211 * @nor: pointer to a 'struct spi_nor'
2212 * @bfpt_header: pointer to the 'struct sfdp_parameter_header' describing
2213 * the Basic Flash Parameter Table length and version
2214 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
2217 * The Basic Flash Parameter Table is the main and only mandatory table as
2218 * defined by the SFDP (JESD216) specification.
2219 * It provides us with the total size (memory density) of the data array and
2220 * the number of address bytes for Fast Read, Page Program and Sector Erase
2222 * For Fast READ commands, it also gives the number of mode clock cycles and
2223 * wait states (regrouped in the number of dummy clock cycles) for each
2224 * supported instruction op code.
2225 * For Page Program, the page size is now available since JESD216 rev A, however
2226 * the supported instruction op codes are still not provided.
2227 * For Sector Erase commands, this table stores the supported instruction op
2228 * codes and the associated sector sizes.
2229 * Finally, the Quad Enable Requirements (QER) are also available since JESD216
2230 * rev A. The QER bits encode the manufacturer dependent procedure to be
2231 * executed to set the Quad Enable (QE) bit in some internal register of the
2232 * Quad SPI memory. Indeed the QE bit, when it exists, must be set before
2233 * sending any Quad SPI command to the memory. Actually, setting the QE bit
2234 * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2
2235 * and IO3 hence enabling 4 (Quad) I/O lines.
2237 * Return: 0 on success, -errno otherwise.
2239 static int spi_nor_parse_bfpt(struct spi_nor *nor,
2240 const struct sfdp_parameter_header *bfpt_header,
2241 struct spi_nor_flash_parameter *params)
2243 struct mtd_info *mtd = &nor->mtd;
2244 struct sfdp_bfpt bfpt;
2250 /* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */
2251 if (bfpt_header->length < BFPT_DWORD_MAX_JESD216)
2254 /* Read the Basic Flash Parameter Table. */
2255 len = min_t(size_t, sizeof(bfpt),
2256 bfpt_header->length * sizeof(u32));
2257 addr = SFDP_PARAM_HEADER_PTP(bfpt_header);
2258 memset(&bfpt, 0, sizeof(bfpt));
2259 err = spi_nor_read_sfdp(nor, addr, len, &bfpt);
2263 /* Fix endianness of the BFPT DWORDs. */
2264 for (i = 0; i < BFPT_DWORD_MAX; i++)
2265 bfpt.dwords[i] = le32_to_cpu(bfpt.dwords[i]);
2267 /* Number of address bytes. */
2268 switch (bfpt.dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) {
2269 case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY:
2270 case BFPT_DWORD1_ADDRESS_BYTES_3_OR_4:
2271 nor->addr_width = 3;
2272 nor->addr_mode_nbytes = 3;
2275 case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY:
2276 nor->addr_width = 4;
2277 nor->addr_mode_nbytes = 4;
2284 /* Flash Memory Density (in bits). */
2285 params->size = bfpt.dwords[BFPT_DWORD(2)];
2286 if (params->size & BIT(31)) {
2287 params->size &= ~BIT(31);
2290 * Prevent overflows on params->size. Anyway, a NOR of 2^64
2291 * bits is unlikely to exist so this error probably means
2292 * the BFPT we are reading is corrupted/wrong.
2294 if (params->size > 63)
2297 params->size = 1ULL << params->size;
2301 params->size >>= 3; /* Convert to bytes. */
2303 /* Fast Read settings. */
2304 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_reads); i++) {
2305 const struct sfdp_bfpt_read *rd = &sfdp_bfpt_reads[i];
2306 struct spi_nor_read_command *read;
2308 if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) {
2309 params->hwcaps.mask &= ~rd->hwcaps;
2313 params->hwcaps.mask |= rd->hwcaps;
2314 cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps);
2315 read = ¶ms->reads[cmd];
2316 half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift;
2317 spi_nor_set_read_settings_from_bfpt(read, half, rd->proto);
2320 /* Sector Erase settings. */
2321 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_erases); i++) {
2322 const struct sfdp_bfpt_erase *er = &sfdp_bfpt_erases[i];
2326 half = bfpt.dwords[er->dword] >> er->shift;
2327 erasesize = half & 0xff;
2329 /* erasesize == 0 means this Erase Type is not supported. */
2333 erasesize = 1U << erasesize;
2334 opcode = (half >> 8) & 0xff;
2335 #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
2336 if (erasesize == SZ_4K) {
2337 nor->erase_opcode = opcode;
2338 mtd->erasesize = erasesize;
2342 if (!mtd->erasesize || mtd->erasesize < erasesize) {
2343 nor->erase_opcode = opcode;
2344 mtd->erasesize = erasesize;
2348 /* Stop here if not JESD216 rev A or later. */
2349 if (bfpt_header->length == BFPT_DWORD_MAX_JESD216)
2350 return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt,
2353 /* Page size: this field specifies 'N' so the page size = 2^N bytes. */
2354 params->page_size = bfpt.dwords[BFPT_DWORD(11)];
2355 params->page_size &= BFPT_DWORD11_PAGE_SIZE_MASK;
2356 params->page_size >>= BFPT_DWORD11_PAGE_SIZE_SHIFT;
2357 params->page_size = 1U << params->page_size;
2359 /* Quad Enable Requirements. */
2360 switch (bfpt.dwords[BFPT_DWORD(15)] & BFPT_DWORD15_QER_MASK) {
2361 case BFPT_DWORD15_QER_NONE:
2362 params->quad_enable = NULL;
2364 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
2365 case BFPT_DWORD15_QER_SR2_BIT1_BUGGY:
2366 case BFPT_DWORD15_QER_SR2_BIT1_NO_RD:
2367 params->quad_enable = spansion_no_read_cr_quad_enable;
2370 #if defined(CONFIG_SPI_FLASH_MACRONIX) || defined(CONFIG_SPI_FLASH_ISSI)
2371 case BFPT_DWORD15_QER_SR1_BIT6:
2372 params->quad_enable = macronix_quad_enable;
2375 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
2376 case BFPT_DWORD15_QER_SR2_BIT1:
2377 params->quad_enable = spansion_read_cr_quad_enable;
2381 dev_dbg(nor->dev, "BFPT QER reserved value used\n");
2385 /* Soft Reset support. */
2386 if (bfpt.dwords[BFPT_DWORD(16)] & BFPT_DWORD16_SOFT_RST)
2387 nor->flags |= SNOR_F_SOFT_RESET;
2389 /* Stop here if JESD216 rev B. */
2390 if (bfpt_header->length == BFPT_DWORD_MAX_JESD216B)
2391 return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt,
2394 /* 8D-8D-8D command extension. */
2395 switch (bfpt.dwords[BFPT_DWORD(18)] & BFPT_DWORD18_CMD_EXT_MASK) {
2396 case BFPT_DWORD18_CMD_EXT_REP:
2397 nor->cmd_ext_type = SPI_NOR_EXT_REPEAT;
2400 case BFPT_DWORD18_CMD_EXT_INV:
2401 nor->cmd_ext_type = SPI_NOR_EXT_INVERT;
2404 case BFPT_DWORD18_CMD_EXT_RES:
2407 case BFPT_DWORD18_CMD_EXT_16B:
2408 dev_err(nor->dev, "16-bit opcodes not supported\n");
2412 return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt, params);
2416 * spi_nor_parse_microchip_sfdp() - parse the Microchip manufacturer specific
2418 * @nor: pointer to a 'struct spi_nor'.
2419 * @param_header: pointer to the SFDP parameter header.
2421 * Return: 0 on success, -errno otherwise.
2424 spi_nor_parse_microchip_sfdp(struct spi_nor *nor,
2425 const struct sfdp_parameter_header *param_header)
2431 size = param_header->length * sizeof(u32);
2432 addr = SFDP_PARAM_HEADER_PTP(param_header);
2434 nor->manufacturer_sfdp = devm_kmalloc(nor->dev, size, GFP_KERNEL);
2435 if (!nor->manufacturer_sfdp)
2438 ret = spi_nor_read_sfdp(nor, addr, size, nor->manufacturer_sfdp);
2444 * spi_nor_parse_profile1() - parse the xSPI Profile 1.0 table
2445 * @nor: pointer to a 'struct spi_nor'
2446 * @profile1_header: pointer to the 'struct sfdp_parameter_header' describing
2447 * the 4-Byte Address Instruction Table length and version.
2448 * @params: pointer to the 'struct spi_nor_flash_parameter' to be.
2450 * Return: 0 on success, -errno otherwise.
2452 static int spi_nor_parse_profile1(struct spi_nor *nor,
2453 const struct sfdp_parameter_header *profile1_header,
2454 struct spi_nor_flash_parameter *params)
2456 u32 *table, opcode, addr;
2461 len = profile1_header->length * sizeof(*table);
2462 table = kmalloc(len, GFP_KERNEL);
2466 addr = SFDP_PARAM_HEADER_PTP(profile1_header);
2467 ret = spi_nor_read_sfdp(nor, addr, len, table);
2471 /* Fix endianness of the table DWORDs. */
2472 for (i = 0; i < profile1_header->length; i++)
2473 table[i] = le32_to_cpu(table[i]);
2475 /* Get 8D-8D-8D fast read opcode and dummy cycles. */
2476 opcode = FIELD_GET(PROFILE1_DWORD1_RD_FAST_CMD, table[0]);
2479 * We don't know what speed the controller is running at. Find the
2480 * dummy cycles for the fastest frequency the flash can run at to be
2481 * sure we are never short of dummy cycles. A value of 0 means the
2482 * frequency is not supported.
2484 * Default to PROFILE1_DUMMY_DEFAULT if we don't find anything, and let
2485 * flashes set the correct value if needed in their fixup hooks.
2487 dummy = FIELD_GET(PROFILE1_DWORD4_DUMMY_200MHZ, table[3]);
2489 dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_166MHZ, table[4]);
2491 dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_133MHZ, table[4]);
2493 dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_100MHZ, table[4]);
2495 dummy = PROFILE1_DUMMY_DEFAULT;
2497 /* Round up to an even value to avoid tripping controllers up. */
2498 dummy = ROUND_UP_TO(dummy, 2);
2500 /* Update the fast read settings. */
2501 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_8_8_8_DTR],
2503 SNOR_PROTO_8_8_8_DTR);
2506 * Set the Read Status Register dummy cycles and dummy address bytes.
2508 if (table[0] & PROFILE1_DWORD1_RDSR_DUMMY)
2509 params->rdsr_dummy = 8;
2511 params->rdsr_dummy = 4;
2513 if (table[0] & PROFILE1_DWORD1_RDSR_ADDR_BYTES)
2514 params->rdsr_addr_nbytes = 4;
2516 params->rdsr_addr_nbytes = 0;
2524 * spi_nor_parse_sccr() - Parse the Status, Control and Configuration Register
2526 * @nor: pointer to a 'struct spi_nor'
2527 * @sccr_header: pointer to the 'struct sfdp_parameter_header' describing
2528 * the SCCR Map table length and version.
2530 * Return: 0 on success, -errno otherwise.
2532 static int spi_nor_parse_sccr(struct spi_nor *nor,
2533 const struct sfdp_parameter_header *sccr_header)
2539 len = sccr_header->length * sizeof(*table);
2540 table = kmalloc(len, GFP_KERNEL);
2544 addr = SFDP_PARAM_HEADER_PTP(sccr_header);
2545 ret = spi_nor_read_sfdp(nor, addr, len, table);
2549 /* Fix endianness of the table DWORDs. */
2550 for (i = 0; i < sccr_header->length; i++)
2551 table[i] = le32_to_cpu(table[i]);
2553 if (FIELD_GET(SCCR_DWORD22_OCTAL_DTR_EN_VOLATILE, table[21]))
2554 nor->flags |= SNOR_F_IO_MODE_EN_VOLATILE;
2562 * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
2563 * @nor: pointer to a 'struct spi_nor'
2564 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
2567 * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
2568 * specification. This is a standard which tends to supported by almost all
2569 * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
2570 * runtime the main parameters needed to perform basic SPI flash operations such
2571 * as Fast Read, Page Program or Sector Erase commands.
2573 * Return: 0 on success, -errno otherwise.
2575 static int spi_nor_parse_sfdp(struct spi_nor *nor,
2576 struct spi_nor_flash_parameter *params)
2578 const struct sfdp_parameter_header *param_header, *bfpt_header;
2579 struct sfdp_parameter_header *param_headers = NULL;
2580 struct sfdp_header header;
2584 /* Get the SFDP header. */
2585 err = spi_nor_read_sfdp(nor, 0, sizeof(header), &header);
2589 /* Check the SFDP header version. */
2590 if (le32_to_cpu(header.signature) != SFDP_SIGNATURE ||
2591 header.major != SFDP_JESD216_MAJOR)
2595 * Verify that the first and only mandatory parameter header is a
2596 * Basic Flash Parameter Table header as specified in JESD216.
2598 bfpt_header = &header.bfpt_header;
2599 if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID ||
2600 bfpt_header->major != SFDP_JESD216_MAJOR)
2604 * Allocate memory then read all parameter headers with a single
2605 * Read SFDP command. These parameter headers will actually be parsed
2606 * twice: a first time to get the latest revision of the basic flash
2607 * parameter table, then a second time to handle the supported optional
2609 * Hence we read the parameter headers once for all to reduce the
2610 * processing time. Also we use kmalloc() instead of devm_kmalloc()
2611 * because we don't need to keep these parameter headers: the allocated
2612 * memory is always released with kfree() before exiting this function.
2615 psize = header.nph * sizeof(*param_headers);
2617 param_headers = kmalloc(psize, GFP_KERNEL);
2621 err = spi_nor_read_sfdp(nor, sizeof(header),
2622 psize, param_headers);
2625 "failed to read SFDP parameter headers\n");
2631 * Check other parameter headers to get the latest revision of
2632 * the basic flash parameter table.
2634 for (i = 0; i < header.nph; i++) {
2635 param_header = ¶m_headers[i];
2637 if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID &&
2638 param_header->major == SFDP_JESD216_MAJOR &&
2639 (param_header->minor > bfpt_header->minor ||
2640 (param_header->minor == bfpt_header->minor &&
2641 param_header->length > bfpt_header->length)))
2642 bfpt_header = param_header;
2645 err = spi_nor_parse_bfpt(nor, bfpt_header, params);
2649 /* Parse other parameter headers. */
2650 for (i = 0; i < header.nph; i++) {
2651 param_header = ¶m_headers[i];
2653 switch (SFDP_PARAM_HEADER_ID(param_header)) {
2654 case SFDP_SECTOR_MAP_ID:
2656 "non-uniform erase sector maps are not supported yet.\n");
2660 err = spi_nor_parse_microchip_sfdp(nor, param_header);
2663 case SFDP_PROFILE1_ID:
2664 err = spi_nor_parse_profile1(nor, param_header, params);
2667 case SFDP_SCCR_MAP_ID:
2668 err = spi_nor_parse_sccr(nor, param_header);
2677 "Failed to parse optional parameter table: %04x\n",
2678 SFDP_PARAM_HEADER_ID(param_header));
2680 * Let's not drop all information we extracted so far
2681 * if optional table parsers fail. In case of failing,
2682 * each optional parser is responsible to roll back to
2683 * the previously known spi_nor data.
2690 kfree(param_headers);
2694 static int spi_nor_parse_sfdp(struct spi_nor *nor,
2695 struct spi_nor_flash_parameter *params)
2699 #endif /* SPI_FLASH_SFDP_SUPPORT */
2702 * spi_nor_post_sfdp_fixups() - Updates the flash's parameters and settings
2703 * after SFDP has been parsed (is also called for SPI NORs that do not
2705 * @nor: pointer to a 'struct spi_nor'
2707 * Typically used to tweak various parameters that could not be extracted by
2708 * other means (i.e. when information provided by the SFDP/flash_info tables
2709 * are incomplete or wrong).
2711 static void spi_nor_post_sfdp_fixups(struct spi_nor *nor,
2712 struct spi_nor_flash_parameter *params)
2714 if (nor->fixups && nor->fixups->post_sfdp)
2715 nor->fixups->post_sfdp(nor, params);
2718 static void spi_nor_default_init_fixups(struct spi_nor *nor)
2720 if (nor->fixups && nor->fixups->default_init)
2721 nor->fixups->default_init(nor);
2724 static int spi_nor_init_params(struct spi_nor *nor,
2725 const struct flash_info *info,
2726 struct spi_nor_flash_parameter *params)
2728 /* Set legacy flash parameters as default. */
2729 memset(params, 0, sizeof(*params));
2731 /* Set SPI NOR sizes. */
2732 params->size = info->sector_size * info->n_sectors;
2733 params->page_size = info->page_size;
2735 if (!(info->flags & SPI_NOR_NO_FR)) {
2736 /* Default to Fast Read for DT and non-DT platform devices. */
2737 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
2739 /* Mask out Fast Read if not requested at DT instantiation. */
2740 #if CONFIG_IS_ENABLED(DM_SPI)
2741 if (!ofnode_read_bool(dev_ofnode(nor->spi->dev),
2743 params->hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
2747 /* (Fast) Read settings. */
2748 params->hwcaps.mask |= SNOR_HWCAPS_READ;
2749 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ],
2750 0, 0, SPINOR_OP_READ,
2753 if (params->hwcaps.mask & SNOR_HWCAPS_READ_FAST)
2754 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_FAST],
2755 0, 8, SPINOR_OP_READ_FAST,
2758 if (info->flags & SPI_NOR_DUAL_READ) {
2759 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
2760 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_2],
2761 0, 8, SPINOR_OP_READ_1_1_2,
2765 if (info->flags & SPI_NOR_QUAD_READ) {
2766 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
2767 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_4],
2768 0, 8, SPINOR_OP_READ_1_1_4,
2772 if (info->flags & SPI_NOR_OCTAL_READ) {
2773 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8;
2774 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_8],
2775 0, 8, SPINOR_OP_READ_1_1_8,
2779 if (info->flags & SPI_NOR_OCTAL_DTR_READ) {
2780 params->hwcaps.mask |= SNOR_HWCAPS_READ_8_8_8_DTR;
2781 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_8_8_8_DTR],
2782 0, 20, SPINOR_OP_READ_FAST,
2783 SNOR_PROTO_8_8_8_DTR);
2786 /* Page Program settings. */
2787 params->hwcaps.mask |= SNOR_HWCAPS_PP;
2788 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP],
2789 SPINOR_OP_PP, SNOR_PROTO_1_1_1);
2792 * Since xSPI Page Program opcode is backward compatible with
2793 * Legacy SPI, use Legacy SPI opcode there as well.
2795 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP_8_8_8_DTR],
2796 SPINOR_OP_PP, SNOR_PROTO_8_8_8_DTR);
2798 if (info->flags & SPI_NOR_QUAD_READ) {
2799 params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4;
2800 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP_1_1_4],
2801 SPINOR_OP_PP_1_1_4, SNOR_PROTO_1_1_4);
2804 /* Select the procedure to set the Quad Enable bit. */
2805 if (params->hwcaps.mask & (SNOR_HWCAPS_READ_QUAD |
2806 SNOR_HWCAPS_PP_QUAD)) {
2807 switch (JEDEC_MFR(info)) {
2808 #if defined(CONFIG_SPI_FLASH_MACRONIX) || defined(CONFIG_SPI_FLASH_ISSI)
2809 case SNOR_MFR_MACRONIX:
2811 params->quad_enable = macronix_quad_enable;
2815 case SNOR_MFR_MICRON:
2819 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
2820 /* Kept only for backward compatibility purpose. */
2821 params->quad_enable = spansion_read_cr_quad_enable;
2827 spi_nor_default_init_fixups(nor);
2829 /* Override the parameters with data read from SFDP tables. */
2830 nor->addr_width = 0;
2831 nor->mtd.erasesize = 0;
2832 if ((info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2833 SPI_NOR_OCTAL_DTR_READ)) &&
2834 !(info->flags & SPI_NOR_SKIP_SFDP)) {
2835 struct spi_nor_flash_parameter sfdp_params;
2837 memcpy(&sfdp_params, params, sizeof(sfdp_params));
2838 if (spi_nor_parse_sfdp(nor, &sfdp_params)) {
2839 nor->addr_width = 0;
2840 nor->mtd.erasesize = 0;
2842 memcpy(params, &sfdp_params, sizeof(*params));
2846 spi_nor_post_sfdp_fixups(nor, params);
2851 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
2855 for (i = 0; i < size; i++)
2856 if (table[i][0] == (int)hwcaps)
2862 static int spi_nor_hwcaps_read2cmd(u32 hwcaps)
2864 static const int hwcaps_read2cmd[][2] = {
2865 { SNOR_HWCAPS_READ, SNOR_CMD_READ },
2866 { SNOR_HWCAPS_READ_FAST, SNOR_CMD_READ_FAST },
2867 { SNOR_HWCAPS_READ_1_1_1_DTR, SNOR_CMD_READ_1_1_1_DTR },
2868 { SNOR_HWCAPS_READ_1_1_2, SNOR_CMD_READ_1_1_2 },
2869 { SNOR_HWCAPS_READ_1_2_2, SNOR_CMD_READ_1_2_2 },
2870 { SNOR_HWCAPS_READ_2_2_2, SNOR_CMD_READ_2_2_2 },
2871 { SNOR_HWCAPS_READ_1_2_2_DTR, SNOR_CMD_READ_1_2_2_DTR },
2872 { SNOR_HWCAPS_READ_1_1_4, SNOR_CMD_READ_1_1_4 },
2873 { SNOR_HWCAPS_READ_1_4_4, SNOR_CMD_READ_1_4_4 },
2874 { SNOR_HWCAPS_READ_4_4_4, SNOR_CMD_READ_4_4_4 },
2875 { SNOR_HWCAPS_READ_1_4_4_DTR, SNOR_CMD_READ_1_4_4_DTR },
2876 { SNOR_HWCAPS_READ_1_1_8, SNOR_CMD_READ_1_1_8 },
2877 { SNOR_HWCAPS_READ_1_8_8, SNOR_CMD_READ_1_8_8 },
2878 { SNOR_HWCAPS_READ_8_8_8, SNOR_CMD_READ_8_8_8 },
2879 { SNOR_HWCAPS_READ_1_8_8_DTR, SNOR_CMD_READ_1_8_8_DTR },
2880 { SNOR_HWCAPS_READ_8_8_8_DTR, SNOR_CMD_READ_8_8_8_DTR },
2883 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
2884 ARRAY_SIZE(hwcaps_read2cmd));
2887 static int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
2889 static const int hwcaps_pp2cmd[][2] = {
2890 { SNOR_HWCAPS_PP, SNOR_CMD_PP },
2891 { SNOR_HWCAPS_PP_1_1_4, SNOR_CMD_PP_1_1_4 },
2892 { SNOR_HWCAPS_PP_1_4_4, SNOR_CMD_PP_1_4_4 },
2893 { SNOR_HWCAPS_PP_4_4_4, SNOR_CMD_PP_4_4_4 },
2894 { SNOR_HWCAPS_PP_1_1_8, SNOR_CMD_PP_1_1_8 },
2895 { SNOR_HWCAPS_PP_1_8_8, SNOR_CMD_PP_1_8_8 },
2896 { SNOR_HWCAPS_PP_8_8_8, SNOR_CMD_PP_8_8_8 },
2897 { SNOR_HWCAPS_PP_8_8_8_DTR, SNOR_CMD_PP_8_8_8_DTR },
2900 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
2901 ARRAY_SIZE(hwcaps_pp2cmd));
2904 #ifdef CONFIG_SPI_FLASH_SMART_HWCAPS
2906 * spi_nor_check_op - check if the operation is supported by controller
2907 * @nor: pointer to a 'struct spi_nor'
2908 * @op: pointer to op template to be checked
2910 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2912 static int spi_nor_check_op(struct spi_nor *nor,
2913 struct spi_mem_op *op)
2916 * First test with 4 address bytes. The opcode itself might be a 3B
2917 * addressing opcode but we don't care, because SPI controller
2918 * implementation should not check the opcode, but just the sequence.
2920 op->addr.nbytes = 4;
2921 if (!spi_mem_supports_op(nor->spi, op)) {
2922 if (nor->mtd.size > SZ_16M)
2925 /* If flash size <= 16MB, 3 address bytes are sufficient */
2926 op->addr.nbytes = 3;
2927 if (!spi_mem_supports_op(nor->spi, op))
2935 * spi_nor_check_readop - check if the read op is supported by controller
2936 * @nor: pointer to a 'struct spi_nor'
2937 * @read: pointer to op template to be checked
2939 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2941 static int spi_nor_check_readop(struct spi_nor *nor,
2942 const struct spi_nor_read_command *read)
2944 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(read->opcode, 0),
2945 SPI_MEM_OP_ADDR(3, 0, 0),
2946 SPI_MEM_OP_DUMMY(1, 0),
2947 SPI_MEM_OP_DATA_IN(2, NULL, 0));
2949 spi_nor_setup_op(nor, &op, read->proto);
2951 op.dummy.nbytes = (read->num_mode_clocks + read->num_wait_states) *
2952 op.dummy.buswidth / 8;
2953 if (spi_nor_protocol_is_dtr(nor->read_proto))
2954 op.dummy.nbytes *= 2;
2956 return spi_nor_check_op(nor, &op);
2960 * spi_nor_check_pp - check if the page program op is supported by controller
2961 * @nor: pointer to a 'struct spi_nor'
2962 * @pp: pointer to op template to be checked
2964 * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2966 static int spi_nor_check_pp(struct spi_nor *nor,
2967 const struct spi_nor_pp_command *pp)
2969 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(pp->opcode, 0),
2970 SPI_MEM_OP_ADDR(3, 0, 0),
2971 SPI_MEM_OP_NO_DUMMY,
2972 SPI_MEM_OP_DATA_OUT(2, NULL, 0));
2974 spi_nor_setup_op(nor, &op, pp->proto);
2976 return spi_nor_check_op(nor, &op);
2980 * spi_nor_adjust_hwcaps - Find optimal Read/Write protocol based on SPI
2981 * controller capabilities
2982 * @nor: pointer to a 'struct spi_nor'
2983 * @params: pointer to the 'struct spi_nor_flash_parameter'
2984 * representing SPI NOR flash capabilities
2985 * @hwcaps: pointer to resulting capabilities after adjusting
2986 * according to controller and flash's capability
2988 * Discard caps based on what the SPI controller actually supports (using
2989 * spi_mem_supports_op()).
2992 spi_nor_adjust_hwcaps(struct spi_nor *nor,
2993 const struct spi_nor_flash_parameter *params,
2999 * Start by assuming the controller supports every capability.
3000 * We will mask them after checking what's really supported
3001 * using spi_mem_supports_op().
3003 *hwcaps = SNOR_HWCAPS_ALL & params->hwcaps.mask;
3005 /* X-X-X modes are not supported yet, mask them all. */
3006 *hwcaps &= ~SNOR_HWCAPS_X_X_X;
3009 * If the reset line is broken, we do not want to enter a stateful
3012 if (nor->flags & SNOR_F_BROKEN_RESET)
3013 *hwcaps &= ~(SNOR_HWCAPS_X_X_X | SNOR_HWCAPS_X_X_X_DTR);
3015 for (cap = 0; cap < sizeof(*hwcaps) * BITS_PER_BYTE; cap++) {
3018 if (!(*hwcaps & BIT(cap)))
3021 rdidx = spi_nor_hwcaps_read2cmd(BIT(cap));
3023 spi_nor_check_readop(nor, ¶ms->reads[rdidx]))
3024 *hwcaps &= ~BIT(cap);
3026 ppidx = spi_nor_hwcaps_pp2cmd(BIT(cap));
3030 if (spi_nor_check_pp(nor, ¶ms->page_programs[ppidx]))
3031 *hwcaps &= ~BIT(cap);
3036 * spi_nor_adjust_hwcaps - Find optimal Read/Write protocol based on SPI
3037 * controller capabilities
3038 * @nor: pointer to a 'struct spi_nor'
3039 * @params: pointer to the 'struct spi_nor_flash_parameter'
3040 * representing SPI NOR flash capabilities
3041 * @hwcaps: pointer to resulting capabilities after adjusting
3042 * according to controller and flash's capability
3044 * Select caps based on what the SPI controller and SPI flash both support.
3047 spi_nor_adjust_hwcaps(struct spi_nor *nor,
3048 const struct spi_nor_flash_parameter *params,
3051 struct spi_slave *spi = nor->spi;
3052 u32 ignored_mask = (SNOR_HWCAPS_READ_2_2_2 |
3053 SNOR_HWCAPS_READ_4_4_4 |
3054 SNOR_HWCAPS_READ_8_8_8 |
3055 SNOR_HWCAPS_PP_4_4_4 |
3056 SNOR_HWCAPS_PP_8_8_8);
3057 u32 spi_hwcaps = (SNOR_HWCAPS_READ | SNOR_HWCAPS_READ_FAST |
3060 /* Get the hardware capabilities the SPI controller supports. */
3061 if (spi->mode & SPI_RX_OCTAL) {
3062 spi_hwcaps |= SNOR_HWCAPS_READ_1_1_8;
3064 if (spi->mode & SPI_TX_OCTAL)
3065 spi_hwcaps |= (SNOR_HWCAPS_READ_1_8_8 |
3066 SNOR_HWCAPS_PP_1_1_8 |
3067 SNOR_HWCAPS_PP_1_8_8);
3068 } else if (spi->mode & SPI_RX_QUAD) {
3069 spi_hwcaps |= SNOR_HWCAPS_READ_1_1_4;
3071 if (spi->mode & SPI_TX_QUAD)
3072 spi_hwcaps |= (SNOR_HWCAPS_READ_1_4_4 |
3073 SNOR_HWCAPS_PP_1_1_4 |
3074 SNOR_HWCAPS_PP_1_4_4);
3075 } else if (spi->mode & SPI_RX_DUAL) {
3076 spi_hwcaps |= SNOR_HWCAPS_READ_1_1_2;
3078 if (spi->mode & SPI_TX_DUAL)
3079 spi_hwcaps |= SNOR_HWCAPS_READ_1_2_2;
3083 * Keep only the hardware capabilities supported by both the SPI
3084 * controller and the SPI flash memory.
3086 *hwcaps = spi_hwcaps & params->hwcaps.mask;
3087 if (*hwcaps & ignored_mask) {
3089 "SPI n-n-n protocols are not supported yet.\n");
3090 *hwcaps &= ~ignored_mask;
3093 #endif /* CONFIG_SPI_FLASH_SMART_HWCAPS */
3095 static int spi_nor_select_read(struct spi_nor *nor,
3096 const struct spi_nor_flash_parameter *params,
3099 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
3100 const struct spi_nor_read_command *read;
3105 cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
3109 read = ¶ms->reads[cmd];
3110 nor->read_opcode = read->opcode;
3111 nor->read_proto = read->proto;
3114 * In the spi-nor framework, we don't need to make the difference
3115 * between mode clock cycles and wait state clock cycles.
3116 * Indeed, the value of the mode clock cycles is used by a QSPI
3117 * flash memory to know whether it should enter or leave its 0-4-4
3118 * (Continuous Read / XIP) mode.
3119 * eXecution In Place is out of the scope of the mtd sub-system.
3120 * Hence we choose to merge both mode and wait state clock cycles
3121 * into the so called dummy clock cycles.
3123 nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
3127 static int spi_nor_select_pp(struct spi_nor *nor,
3128 const struct spi_nor_flash_parameter *params,
3131 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
3132 const struct spi_nor_pp_command *pp;
3137 cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
3141 pp = ¶ms->page_programs[cmd];
3142 nor->program_opcode = pp->opcode;
3143 nor->write_proto = pp->proto;
3147 static int spi_nor_select_erase(struct spi_nor *nor,
3148 const struct flash_info *info)
3150 struct mtd_info *mtd = &nor->mtd;
3152 /* Do nothing if already configured from SFDP. */
3156 #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
3157 /* prefer "small sector" erase if possible */
3158 if (info->flags & SECT_4K) {
3159 nor->erase_opcode = SPINOR_OP_BE_4K;
3160 mtd->erasesize = 4096;
3161 } else if (info->flags & SECT_4K_PMC) {
3162 nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
3163 mtd->erasesize = 4096;
3167 nor->erase_opcode = SPINOR_OP_SE;
3168 mtd->erasesize = info->sector_size;
3173 static int spi_nor_default_setup(struct spi_nor *nor,
3174 const struct flash_info *info,
3175 const struct spi_nor_flash_parameter *params)
3178 bool enable_quad_io;
3181 spi_nor_adjust_hwcaps(nor, params, &shared_mask);
3183 /* Select the (Fast) Read command. */
3184 err = spi_nor_select_read(nor, params, shared_mask);
3187 "can't select read settings supported by both the SPI controller and memory.\n");
3191 /* Select the Page Program command. */
3192 err = spi_nor_select_pp(nor, params, shared_mask);
3195 "can't select write settings supported by both the SPI controller and memory.\n");
3199 /* Select the Sector Erase command. */
3200 err = spi_nor_select_erase(nor, info);
3203 "can't select erase settings supported by both the SPI controller and memory.\n");
3207 /* Enable Quad I/O if needed. */
3208 enable_quad_io = (spi_nor_get_protocol_width(nor->read_proto) == 4 ||
3209 spi_nor_get_protocol_width(nor->write_proto) == 4);
3210 if (enable_quad_io && params->quad_enable)
3211 nor->quad_enable = params->quad_enable;
3213 nor->quad_enable = NULL;
3218 static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
3219 const struct spi_nor_flash_parameter *params)
3224 return nor->setup(nor, info, params);
3227 #ifdef CONFIG_SPI_FLASH_SPANSION
3229 /* Use ID byte 4 to distinguish S25FS256T and S25Hx-T */
3230 #define S25FS256T_ID4 (0x08)
3232 /* Number of dummy cycle for Read Any Register (RDAR) op. */
3233 #define S25FS_S_RDAR_DUMMY 8
3235 static int s25fs_s_quad_enable(struct spi_nor *nor)
3237 return spansion_quad_enable_volatile(nor, 0, S25FS_S_RDAR_DUMMY);
3240 static int s25fs_s_erase_non_uniform(struct spi_nor *nor, loff_t addr)
3242 /* Support 8 x 4KB sectors at bottom */
3243 return spansion_erase_non_uniform(nor, addr, SPINOR_OP_BE_4K_4B, 0, SZ_32K);
3246 static int s25fs_s_setup(struct spi_nor *nor, const struct flash_info *info,
3247 const struct spi_nor_flash_parameter *params)
3252 /* Bank Address Register is not supported */
3253 if (CONFIG_IS_ENABLED(SPI_FLASH_BAR))
3257 * Read CR3V to check if uniform sector is selected. If not, assign an
3258 * erase hook that supports non-uniform erase.
3260 ret = spansion_read_any_reg(nor, SPINOR_REG_ADDR_CFR3V,
3261 S25FS_S_RDAR_DUMMY, &cfr3v);
3264 if (!(cfr3v & CFR3V_UNHYSA))
3265 nor->erase = s25fs_s_erase_non_uniform;
3267 return spi_nor_default_setup(nor, info, params);
3270 static void s25fs_s_default_init(struct spi_nor *nor)
3272 nor->setup = s25fs_s_setup;
3275 static int s25fs_s_post_bfpt_fixup(struct spi_nor *nor,
3276 const struct sfdp_parameter_header *header,
3277 const struct sfdp_bfpt *bfpt,
3278 struct spi_nor_flash_parameter *params)
3280 /* The erase size is set to 4K from BFPT, but it's wrong. Fix it. */
3281 nor->erase_opcode = SPINOR_OP_SE;
3282 nor->mtd.erasesize = nor->info->sector_size;
3284 /* The S25FS-S chip family reports 512-byte pages in BFPT but
3285 * in reality the write buffer still wraps at the safe default
3286 * of 256 bytes. Overwrite the page size advertised by BFPT
3287 * to get the writes working.
3289 params->page_size = 256;
3294 static void s25fs_s_post_sfdp_fixup(struct spi_nor *nor,
3295 struct spi_nor_flash_parameter *params)
3297 /* READ_1_1_2 is not supported */
3298 params->hwcaps.mask &= ~SNOR_HWCAPS_READ_1_1_2;
3299 /* READ_1_1_4 is not supported */
3300 params->hwcaps.mask &= ~SNOR_HWCAPS_READ_1_1_4;
3301 /* PP_1_1_4 is not supported */
3302 params->hwcaps.mask &= ~SNOR_HWCAPS_PP_1_1_4;
3303 /* Use volatile register to enable quad */
3304 params->quad_enable = s25fs_s_quad_enable;
3307 static struct spi_nor_fixups s25fs_s_fixups = {
3308 .default_init = s25fs_s_default_init,
3309 .post_bfpt = s25fs_s_post_bfpt_fixup,
3310 .post_sfdp = s25fs_s_post_sfdp_fixup,
3313 static int s25_mdp_ready(struct spi_nor *nor)
3318 for (addr = 0; addr < nor->mtd.size; addr += SZ_128M) {
3319 ret = spansion_sr_ready(nor, addr, 0);
3327 static int s25_quad_enable(struct spi_nor *nor)
3332 for (addr = 0; addr < nor->mtd.size; addr += SZ_128M) {
3333 ret = spansion_quad_enable_volatile(nor, addr, 0);
3341 static int s25_erase_non_uniform(struct spi_nor *nor, loff_t addr)
3343 /* Support 32 x 4KB sectors at bottom */
3344 return spansion_erase_non_uniform(nor, addr, SPINOR_OP_BE_4K_4B, 0,
3348 static int s25_setup(struct spi_nor *nor, const struct flash_info *info,
3349 const struct spi_nor_flash_parameter *params)
3354 #ifdef CONFIG_SPI_FLASH_BAR
3355 return -ENOTSUPP; /* Bank Address Register is not supported */
3358 * S25FS256T has multiple sector architecture options, with selection of
3359 * count and location of 128KB and 64KB sectors. This driver supports
3360 * uniform 128KB only due to complexity of non-uniform layout.
3362 if (nor->info->id[4] == S25FS256T_ID4) {
3363 ret = spansion_read_any_reg(nor, SPINOR_REG_ADDR_ARCFN, 8, &cr);
3367 if (cr) /* Option 0 (ARCFN[7:0] == 0x00) is uniform */
3370 return spi_nor_default_setup(nor, info, params);
3374 * Read CFR3V to check if uniform sector is selected. If not, assign an
3375 * erase hook that supports non-uniform erase.
3377 ret = spansion_read_any_reg(nor, SPINOR_REG_ADDR_CFR3V, 0, &cr);
3380 if (!(cr & CFR3V_UNHYSA))
3381 nor->erase = s25_erase_non_uniform;
3384 * For the multi-die package parts, the ready() hook is needed to check
3385 * all dies' status via read any register.
3387 if (nor->mtd.size > SZ_128M)
3388 nor->ready = s25_mdp_ready;
3390 return spi_nor_default_setup(nor, info, params);
3393 static void s25_default_init(struct spi_nor *nor)
3395 nor->setup = s25_setup;
3398 static int s25_post_bfpt_fixup(struct spi_nor *nor,
3399 const struct sfdp_parameter_header *header,
3400 const struct sfdp_bfpt *bfpt,
3401 struct spi_nor_flash_parameter *params)
3407 /* erase size in case it is set to 4K from BFPT */
3408 nor->erase_opcode = SPINOR_OP_SE_4B;
3409 nor->mtd.erasesize = nor->info->sector_size;
3412 * The default address mode in multi-die package parts (>1Gb) may be
3413 * 3- or 4-byte, depending on model number. BootROM code in some SoCs
3414 * use 3-byte mode for backward compatibility and should switch to
3415 * 4-byte mode after BootROM phase. Since registers in the 2nd die are
3416 * mapped within 32-bit address space, we need to make sure the flash is
3417 * in 4-byte address mode. The default address mode can be distinguished
3418 * by BFPT 16th DWORD. Power cycle exits 4-byte address mode if default
3419 * is 3-byte address mode.
3421 if (params->size > SZ_128M) {
3422 if (bfpt->dwords[BFPT_DWORD(16)] & BFPT_DWORD16_EX4B_PWRCYC) {
3423 ret = set_4byte(nor, nor->info, 1);
3427 nor->addr_mode_nbytes = 4;
3430 /* The default address mode in S25FS256T is 4. */
3431 if (nor->info->id[4] == S25FS256T_ID4)
3432 nor->addr_mode_nbytes = 4;
3435 * The page_size is set to 512B from BFPT, but it actually depends on
3436 * the configuration register. Look up the CFR3V and determine the
3437 * page_size. For multi-die package parts, use 512B only when the all
3438 * dies are configured to 512B buffer.
3440 for (addr = 0; addr < params->size; addr += SZ_128M) {
3441 ret = spansion_read_any_reg(nor, addr + SPINOR_REG_ADDR_CFR3V,
3446 if (!(cfr3v & CFR3V_PGMBUF)) {
3447 params->page_size = 256;
3451 params->page_size = 512;
3456 static void s25_post_sfdp_fixup(struct spi_nor *nor,
3457 struct spi_nor_flash_parameter *params)
3459 if (nor->info->id[4] == S25FS256T_ID4) {
3460 /* PP_1_1_4 is supported */
3461 params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4;
3463 /* READ_FAST_4B (0Ch) requires mode cycles*/
3464 params->reads[SNOR_CMD_READ_FAST].num_mode_clocks = 8;
3465 /* PP_1_1_4 is not supported */
3466 params->hwcaps.mask &= ~SNOR_HWCAPS_PP_1_1_4;
3467 /* Use volatile register to enable quad */
3468 params->quad_enable = s25_quad_enable;
3472 static struct spi_nor_fixups s25_fixups = {
3473 .default_init = s25_default_init,
3474 .post_bfpt = s25_post_bfpt_fixup,
3475 .post_sfdp = s25_post_sfdp_fixup,
3478 static int s25fl256l_setup(struct spi_nor *nor, const struct flash_info *info,
3479 const struct spi_nor_flash_parameter *params)
3481 return -ENOTSUPP; /* Bank Address Register is not supported */
3484 static void s25fl256l_default_init(struct spi_nor *nor)
3486 nor->setup = s25fl256l_setup;
3489 static struct spi_nor_fixups s25fl256l_fixups = {
3490 .default_init = s25fl256l_default_init,
3494 #ifdef CONFIG_SPI_FLASH_S28HX_T
3496 * spi_nor_cypress_octal_dtr_enable() - Enable octal DTR on Cypress flashes.
3497 * @nor: pointer to a 'struct spi_nor'
3499 * This also sets the memory access latency cycles to 24 to allow the flash to
3500 * run at up to 200MHz.
3502 * Return: 0 on success, -errno otherwise.
3504 static int spi_nor_cypress_octal_dtr_enable(struct spi_nor *nor)
3506 struct spi_mem_op op;
3511 /* Use 24 dummy cycles for memory array reads. */
3512 ret = write_enable(nor);
3516 buf = SPINOR_REG_CYPRESS_CFR2_MEMLAT_11_24;
3517 op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WR_ANY_REG, 1),
3518 SPI_MEM_OP_ADDR(addr_width, SPINOR_REG_CYPRESS_CFR2V, 1),
3519 SPI_MEM_OP_NO_DUMMY,
3520 SPI_MEM_OP_DATA_OUT(1, &buf, 1));
3521 ret = spi_mem_exec_op(nor->spi, &op);
3524 "failed to set default memory latency value: %d\n",
3528 ret = spi_nor_wait_till_ready(nor);
3532 nor->read_dummy = 24;
3534 /* Set the octal and DTR enable bits. */
3535 ret = write_enable(nor);
3539 buf = SPINOR_REG_CYPRESS_CFR5_OCT_DTR_EN;
3540 op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WR_ANY_REG, 1),
3541 SPI_MEM_OP_ADDR(addr_width, SPINOR_REG_CYPRESS_CFR5V, 1),
3542 SPI_MEM_OP_NO_DUMMY,
3543 SPI_MEM_OP_DATA_OUT(1, &buf, 1));
3544 ret = spi_mem_exec_op(nor->spi, &op);
3546 dev_warn(nor->dev, "Failed to enable octal DTR mode\n");
3553 static int s28hx_t_erase_non_uniform(struct spi_nor *nor, loff_t addr)
3555 /* Factory default configuration: 32 x 4 KiB sectors at bottom. */
3556 return spansion_erase_non_uniform(nor, addr, SPINOR_OP_S28_SE_4K,
3560 static int s28hx_t_setup(struct spi_nor *nor, const struct flash_info *info,
3561 const struct spi_nor_flash_parameter *params)
3563 struct spi_mem_op op;
3568 ret = spi_nor_wait_till_ready(nor);
3573 * Check CFR3V to check if non-uniform sector mode is selected. If it
3574 * is, set the erase hook to the non-uniform erase procedure.
3576 op = (struct spi_mem_op)
3577 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RD_ANY_REG, 1),
3578 SPI_MEM_OP_ADDR(addr_width,
3579 SPINOR_REG_CYPRESS_CFR3V, 1),
3580 SPI_MEM_OP_NO_DUMMY,
3581 SPI_MEM_OP_DATA_IN(1, &buf, 1));
3583 ret = spi_mem_exec_op(nor->spi, &op);
3587 if (!(buf & SPINOR_REG_CYPRESS_CFR3_UNISECT))
3588 nor->erase = s28hx_t_erase_non_uniform;
3590 return spi_nor_default_setup(nor, info, params);
3593 static void s28hx_t_default_init(struct spi_nor *nor)
3595 nor->octal_dtr_enable = spi_nor_cypress_octal_dtr_enable;
3596 nor->setup = s28hx_t_setup;
3599 static void s28hx_t_post_sfdp_fixup(struct spi_nor *nor,
3600 struct spi_nor_flash_parameter *params)
3603 * On older versions of the flash the xSPI Profile 1.0 table has the
3604 * 8D-8D-8D Fast Read opcode as 0x00. But it actually should be 0xEE.
3606 if (params->reads[SNOR_CMD_READ_8_8_8_DTR].opcode == 0)
3607 params->reads[SNOR_CMD_READ_8_8_8_DTR].opcode =
3608 SPINOR_OP_CYPRESS_RD_FAST;
3610 params->hwcaps.mask |= SNOR_HWCAPS_PP_8_8_8_DTR;
3612 /* This flash is also missing the 4-byte Page Program opcode bit. */
3613 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP],
3614 SPINOR_OP_PP_4B, SNOR_PROTO_1_1_1);
3616 * Since xSPI Page Program opcode is backward compatible with
3617 * Legacy SPI, use Legacy SPI opcode there as well.
3619 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP_8_8_8_DTR],
3620 SPINOR_OP_PP_4B, SNOR_PROTO_8_8_8_DTR);
3623 * The xSPI Profile 1.0 table advertises the number of additional
3624 * address bytes needed for Read Status Register command as 0 but the
3625 * actual value for that is 4.
3627 params->rdsr_addr_nbytes = 4;
3630 static int s28hx_t_post_bfpt_fixup(struct spi_nor *nor,
3631 const struct sfdp_parameter_header *bfpt_header,
3632 const struct sfdp_bfpt *bfpt,
3633 struct spi_nor_flash_parameter *params)
3635 struct spi_mem_op op;
3641 * The BFPT table advertises a 512B page size but the page size is
3642 * actually configurable (with the default being 256B). Read from
3643 * CFR3V[4] and set the correct size.
3645 op = (struct spi_mem_op)
3646 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RD_ANY_REG, 1),
3647 SPI_MEM_OP_ADDR(addr_width, SPINOR_REG_CYPRESS_CFR3V, 1),
3648 SPI_MEM_OP_NO_DUMMY,
3649 SPI_MEM_OP_DATA_IN(1, &buf, 1));
3650 ret = spi_mem_exec_op(nor->spi, &op);
3654 if (buf & SPINOR_REG_CYPRESS_CFR3_PGSZ)
3655 params->page_size = 512;
3657 params->page_size = 256;
3660 * The BFPT advertises that it supports 4k erases, and the datasheet
3661 * says the same. But 4k erases did not work when testing. So, use 256k
3664 nor->erase_opcode = SPINOR_OP_SE_4B;
3665 nor->mtd.erasesize = 0x40000;
3670 static struct spi_nor_fixups s28hx_t_fixups = {
3671 .default_init = s28hx_t_default_init,
3672 .post_sfdp = s28hx_t_post_sfdp_fixup,
3673 .post_bfpt = s28hx_t_post_bfpt_fixup,
3675 #endif /* CONFIG_SPI_FLASH_S28HX_T */
3677 #ifdef CONFIG_SPI_FLASH_MT35XU
3678 static int spi_nor_micron_octal_dtr_enable(struct spi_nor *nor)
3680 struct spi_mem_op op;
3685 /* Set dummy cycles for Fast Read to the default of 20. */
3686 ret = write_enable(nor);
3691 op = (struct spi_mem_op)
3692 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_MT_WR_ANY_REG, 1),
3693 SPI_MEM_OP_ADDR(addr_width, SPINOR_REG_MT_CFR1V, 1),
3694 SPI_MEM_OP_NO_DUMMY,
3695 SPI_MEM_OP_DATA_OUT(1, &buf, 1));
3696 ret = spi_mem_exec_op(nor->spi, &op);
3700 ret = spi_nor_wait_till_ready(nor);
3704 nor->read_dummy = 20;
3706 ret = write_enable(nor);
3710 buf = SPINOR_MT_OCT_DTR;
3711 op = (struct spi_mem_op)
3712 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_MT_WR_ANY_REG, 1),
3713 SPI_MEM_OP_ADDR(addr_width, SPINOR_REG_MT_CFR0V, 1),
3714 SPI_MEM_OP_NO_DUMMY,
3715 SPI_MEM_OP_DATA_OUT(1, &buf, 1));
3716 ret = spi_mem_exec_op(nor->spi, &op);
3718 dev_err(nor->dev, "Failed to enable octal DTR mode\n");
3725 static void mt35xu512aba_default_init(struct spi_nor *nor)
3727 nor->octal_dtr_enable = spi_nor_micron_octal_dtr_enable;
3730 static void mt35xu512aba_post_sfdp_fixup(struct spi_nor *nor,
3731 struct spi_nor_flash_parameter *params)
3733 /* Set the Fast Read settings. */
3734 params->hwcaps.mask |= SNOR_HWCAPS_READ_8_8_8_DTR;
3735 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_8_8_8_DTR],
3736 0, 20, SPINOR_OP_MT_DTR_RD,
3737 SNOR_PROTO_8_8_8_DTR);
3739 params->hwcaps.mask |= SNOR_HWCAPS_PP_8_8_8_DTR;
3741 nor->cmd_ext_type = SPI_NOR_EXT_REPEAT;
3742 params->rdsr_dummy = 8;
3743 params->rdsr_addr_nbytes = 0;
3746 * The BFPT quad enable field is set to a reserved value so the quad
3747 * enable function is ignored by spi_nor_parse_bfpt(). Make sure we
3750 params->quad_enable = NULL;
3753 static struct spi_nor_fixups mt35xu512aba_fixups = {
3754 .default_init = mt35xu512aba_default_init,
3755 .post_sfdp = mt35xu512aba_post_sfdp_fixup,
3757 #endif /* CONFIG_SPI_FLASH_MT35XU */
3759 #if CONFIG_IS_ENABLED(SPI_FLASH_MACRONIX)
3761 * spi_nor_macronix_octal_dtr_enable() - Enable octal DTR on Macronix flashes.
3762 * @nor: pointer to a 'struct spi_nor'
3764 * Set Macronix max dummy cycles 20 to allow the flash to run at fastest frequency.
3766 * Return: 0 on success, -errno otherwise.
3768 static int spi_nor_macronix_octal_dtr_enable(struct spi_nor *nor)
3770 struct spi_mem_op op;
3774 ret = write_enable(nor);
3778 buf = SPINOR_REG_MXIC_DC_20;
3779 op = (struct spi_mem_op)
3780 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WR_CR2, 1),
3781 SPI_MEM_OP_ADDR(4, SPINOR_REG_MXIC_CR2_DC, 1),
3782 SPI_MEM_OP_NO_DUMMY,
3783 SPI_MEM_OP_DATA_OUT(1, &buf, 1));
3785 ret = spi_mem_exec_op(nor->spi, &op);
3789 ret = spi_nor_wait_till_ready(nor);
3793 nor->read_dummy = MXIC_MAX_DC;
3794 ret = write_enable(nor);
3798 buf = SPINOR_REG_MXIC_OPI_DTR_EN;
3799 op = (struct spi_mem_op)
3800 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WR_CR2, 1),
3801 SPI_MEM_OP_ADDR(4, SPINOR_REG_MXIC_CR2_MODE, 1),
3802 SPI_MEM_OP_NO_DUMMY,
3803 SPI_MEM_OP_DATA_OUT(1, &buf, 1));
3805 ret = spi_mem_exec_op(nor->spi, &op);
3807 dev_err(nor->dev, "Failed to enable octal DTR mode\n");
3810 nor->reg_proto = SNOR_PROTO_8_8_8_DTR;
3815 static void macronix_octal_default_init(struct spi_nor *nor)
3817 nor->octal_dtr_enable = spi_nor_macronix_octal_dtr_enable;
3820 static void macronix_octal_post_sfdp_fixup(struct spi_nor *nor,
3821 struct spi_nor_flash_parameter *params)
3824 * Adding SNOR_HWCAPS_PP_8_8_8_DTR in hwcaps.mask when
3825 * SPI_NOR_OCTAL_DTR_READ flag exists.
3827 if (params->hwcaps.mask & SNOR_HWCAPS_READ_8_8_8_DTR)
3828 params->hwcaps.mask |= SNOR_HWCAPS_PP_8_8_8_DTR;
3831 static struct spi_nor_fixups macronix_octal_fixups = {
3832 .default_init = macronix_octal_default_init,
3833 .post_sfdp = macronix_octal_post_sfdp_fixup,
3835 #endif /* CONFIG_SPI_FLASH_MACRONIX */
3837 /** spi_nor_octal_dtr_enable() - enable Octal DTR I/O if needed
3838 * @nor: pointer to a 'struct spi_nor'
3840 * Return: 0 on success, -errno otherwise.
3842 static int spi_nor_octal_dtr_enable(struct spi_nor *nor)
3846 if (!nor->octal_dtr_enable)
3849 if (!(nor->read_proto == SNOR_PROTO_8_8_8_DTR &&
3850 nor->write_proto == SNOR_PROTO_8_8_8_DTR))
3853 if (!(nor->flags & SNOR_F_IO_MODE_EN_VOLATILE))
3856 ret = nor->octal_dtr_enable(nor);
3860 nor->reg_proto = SNOR_PROTO_8_8_8_DTR;
3865 static int spi_nor_init(struct spi_nor *nor)
3869 err = spi_nor_octal_dtr_enable(nor);
3871 dev_dbg(nor->dev, "Octal DTR mode not supported\n");
3876 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
3877 * with the software protection bits set
3879 if (IS_ENABLED(CONFIG_SPI_FLASH_UNLOCK_ALL) &&
3880 (JEDEC_MFR(nor->info) == SNOR_MFR_ATMEL ||
3881 JEDEC_MFR(nor->info) == SNOR_MFR_INTEL ||
3882 JEDEC_MFR(nor->info) == SNOR_MFR_SST ||
3883 nor->info->flags & SPI_NOR_HAS_LOCK)) {
3886 spi_nor_wait_till_ready(nor);
3889 if (nor->quad_enable) {
3890 err = nor->quad_enable(nor);
3892 dev_dbg(nor->dev, "quad mode not supported\n");
3897 if (nor->addr_width == 4 &&
3898 !(nor->info->flags & SPI_NOR_OCTAL_DTR_READ) &&
3899 (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION) &&
3900 !(nor->info->flags & SPI_NOR_4B_OPCODES)) {
3902 * If the RESET# pin isn't hooked up properly, or the system
3903 * otherwise doesn't perform a reset command in the boot
3904 * sequence, it's impossible to 100% protect against unexpected
3905 * reboots (e.g., crashes). Warn the user (or hopefully, system
3906 * designer) that this is bad.
3908 if (nor->flags & SNOR_F_BROKEN_RESET)
3909 debug("enabling reset hack; may not recover from unexpected reboots\n");
3910 set_4byte(nor, nor->info, 1);
3916 #ifdef CONFIG_SPI_FLASH_SOFT_RESET
3918 * spi_nor_soft_reset() - perform the JEDEC Software Reset sequence
3919 * @nor: the spi_nor structure
3921 * This function can be used to switch from Octal DTR mode to legacy mode on a
3922 * flash that supports it. The soft reset is executed in Octal DTR mode.
3924 * Return: 0 for success, -errno for failure.
3926 static int spi_nor_soft_reset(struct spi_nor *nor)
3928 struct spi_mem_op op;
3930 enum spi_nor_cmd_ext ext;
3932 ext = nor->cmd_ext_type;
3933 if (nor->cmd_ext_type == SPI_NOR_EXT_NONE) {
3934 nor->cmd_ext_type = SPI_NOR_EXT_REPEAT;
3935 #if CONFIG_IS_ENABLED(SPI_NOR_BOOT_SOFT_RESET_EXT_INVERT)
3936 nor->cmd_ext_type = SPI_NOR_EXT_INVERT;
3937 #endif /* SPI_NOR_BOOT_SOFT_RESET_EXT_INVERT */
3940 op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_SRSTEN, 0),
3941 SPI_MEM_OP_NO_DUMMY,
3943 SPI_MEM_OP_NO_DATA);
3944 spi_nor_setup_op(nor, &op, SNOR_PROTO_8_8_8_DTR);
3945 ret = spi_mem_exec_op(nor->spi, &op);
3947 dev_warn(nor->dev, "Software reset enable failed: %d\n", ret);
3951 op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_SRST, 0),
3952 SPI_MEM_OP_NO_DUMMY,
3954 SPI_MEM_OP_NO_DATA);
3955 spi_nor_setup_op(nor, &op, SNOR_PROTO_8_8_8_DTR);
3956 ret = spi_mem_exec_op(nor->spi, &op);
3958 dev_warn(nor->dev, "Software reset failed: %d\n", ret);
3963 * Software Reset is not instant, and the delay varies from flash to
3964 * flash. Looking at a few flashes, most range somewhere below 100
3965 * microseconds. So, wait for 200ms just to be sure.
3967 udelay(SPI_NOR_SRST_SLEEP_LEN);
3970 nor->cmd_ext_type = ext;
3973 #endif /* CONFIG_SPI_FLASH_SOFT_RESET */
3975 int spi_nor_remove(struct spi_nor *nor)
3977 #ifdef CONFIG_SPI_FLASH_SOFT_RESET
3978 if (nor->info->flags & SPI_NOR_OCTAL_DTR_READ &&
3979 nor->flags & SNOR_F_SOFT_RESET)
3980 return spi_nor_soft_reset(nor);
3986 void spi_nor_set_fixups(struct spi_nor *nor)
3988 #ifdef CONFIG_SPI_FLASH_SPANSION
3989 if (JEDEC_MFR(nor->info) == SNOR_MFR_CYPRESS) {
3990 switch (nor->info->id[1]) {
3991 case 0x2a: /* S25HL (QSPI, 3.3V) */
3992 case 0x2b: /* S25HS (QSPI, 1.8V) */
3993 nor->fixups = &s25_fixups;
3996 #ifdef CONFIG_SPI_FLASH_S28HX_T
3997 case 0x5a: /* S28HL (Octal, 3.3V) */
3998 case 0x5b: /* S28HS (Octal, 1.8V) */
3999 nor->fixups = &s28hx_t_fixups;
4008 if (CONFIG_IS_ENABLED(SPI_FLASH_BAR) &&
4009 !strcmp(nor->info->name, "s25fl256l"))
4010 nor->fixups = &s25fl256l_fixups;
4012 /* For FS-S (family ID = 0x81) */
4013 if (JEDEC_MFR(nor->info) == SNOR_MFR_SPANSION && nor->info->id[5] == 0x81)
4014 nor->fixups = &s25fs_s_fixups;
4017 #ifdef CONFIG_SPI_FLASH_MT35XU
4018 if (!strcmp(nor->info->name, "mt35xu512aba"))
4019 nor->fixups = &mt35xu512aba_fixups;
4022 #if CONFIG_IS_ENABLED(SPI_FLASH_MACRONIX)
4023 nor->fixups = ¯onix_octal_fixups;
4024 #endif /* SPI_FLASH_MACRONIX */
4027 int spi_nor_scan(struct spi_nor *nor)
4029 struct spi_nor_flash_parameter params;
4030 const struct flash_info *info = NULL;
4031 struct mtd_info *mtd = &nor->mtd;
4032 struct spi_slave *spi = nor->spi;
4036 #ifdef CONFIG_FLASH_CFI_MTD
4037 cfi_mtd_nb = CFI_FLASH_BANKS;
4040 /* Reset SPI protocol for all commands. */
4041 nor->reg_proto = SNOR_PROTO_1_1_1;
4042 nor->read_proto = SNOR_PROTO_1_1_1;
4043 nor->write_proto = SNOR_PROTO_1_1_1;
4044 nor->read = spi_nor_read_data;
4045 nor->write = spi_nor_write_data;
4046 nor->read_reg = spi_nor_read_reg;
4047 nor->write_reg = spi_nor_write_reg;
4049 nor->setup = spi_nor_default_setup;
4051 #ifdef CONFIG_SPI_FLASH_SOFT_RESET_ON_BOOT
4053 * When the flash is handed to us in a stateful mode like 8D-8D-8D, it
4054 * is difficult to detect the mode the flash is in. One option is to
4055 * read SFDP in all modes and see which one gives the correct "SFDP"
4056 * signature, but not all flashes support SFDP in 8D-8D-8D mode.
4058 * Further, even if you detect the mode of the flash via SFDP, you
4059 * still have the problem of actually reading the ID. The Read ID
4060 * command is not standardized across flash vendors. Flashes can have
4061 * different dummy cycles needed for reading the ID. Some flashes even
4062 * expect a 4-byte dummy address with the Read ID command. All this
4063 * information cannot be obtained from the SFDP table.
4065 * So, perform a Software Reset sequence before reading the ID and
4066 * initializing the flash. A Soft Reset will bring back the flash in
4067 * its default protocol mode assuming no non-volatile configuration was
4068 * set. This will let us detect the flash even if ROM hands it to us in
4071 * To accommodate cases where there is more than one flash on a board,
4072 * and only one of them needs a soft reset, failure to reset is not
4073 * made fatal, and we still try to read ID if possible.
4075 spi_nor_soft_reset(nor);
4076 #endif /* CONFIG_SPI_FLASH_SOFT_RESET_ON_BOOT */
4078 info = spi_nor_read_id(nor);
4079 if (IS_ERR_OR_NULL(info))
4083 spi_nor_set_fixups(nor);
4085 /* Parse the Serial Flash Discoverable Parameters table. */
4086 ret = spi_nor_init_params(nor, info, ¶ms);
4091 sprintf(nor->mtd_name, "%s%d",
4092 MTD_DEV_TYPE(MTD_DEV_TYPE_NOR),
4093 cfi_mtd_nb + dev_seq(nor->dev));
4094 mtd->name = nor->mtd_name;
4096 mtd->dev = nor->dev;
4098 mtd->type = MTD_NORFLASH;
4100 mtd->flags = MTD_CAP_NORFLASH;
4101 mtd->size = params.size;
4102 mtd->_erase = spi_nor_erase;
4103 mtd->_read = spi_nor_read;
4104 mtd->_write = spi_nor_write;
4106 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
4107 /* NOR protection support for STmicro/Micron chips and similar */
4108 if (JEDEC_MFR(info) == SNOR_MFR_ST ||
4109 JEDEC_MFR(info) == SNOR_MFR_MICRON ||
4110 JEDEC_MFR(info) == SNOR_MFR_SST ||
4111 info->flags & SPI_NOR_HAS_LOCK) {
4112 nor->flash_lock = stm_lock;
4113 nor->flash_unlock = stm_unlock;
4114 nor->flash_is_unlocked = stm_is_unlocked;
4118 #ifdef CONFIG_SPI_FLASH_SST
4120 * sst26 series block protection implementation differs from other
4123 if (info->flags & SPI_NOR_HAS_SST26LOCK) {
4124 nor->flash_lock = sst26_lock;
4125 nor->flash_unlock = sst26_unlock;
4126 nor->flash_is_unlocked = sst26_is_unlocked;
4130 if (info->flags & USE_FSR)
4131 nor->flags |= SNOR_F_USE_FSR;
4132 if (info->flags & SPI_NOR_HAS_TB)
4133 nor->flags |= SNOR_F_HAS_SR_TB;
4134 if (info->flags & NO_CHIP_ERASE)
4135 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
4136 if (info->flags & USE_CLSR)
4137 nor->flags |= SNOR_F_USE_CLSR;
4139 if (info->flags & SPI_NOR_NO_ERASE)
4140 mtd->flags |= MTD_NO_ERASE;
4142 nor->page_size = params.page_size;
4143 mtd->writebufsize = nor->page_size;
4145 /* Some devices cannot do fast-read, no matter what DT tells us */
4146 if ((info->flags & SPI_NOR_NO_FR) || (spi->mode & SPI_RX_SLOW))
4147 params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
4150 * Configure the SPI memory:
4151 * - select op codes for (Fast) Read, Page Program and Sector Erase.
4152 * - set the number of dummy cycles (mode cycles + wait states).
4153 * - set the SPI protocols for register and memory accesses.
4154 * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
4156 ret = spi_nor_setup(nor, info, ¶ms);
4160 if (spi_nor_protocol_is_dtr(nor->read_proto)) {
4161 /* Always use 4-byte addresses in DTR mode. */
4162 nor->addr_width = 4;
4163 } else if (nor->addr_width) {
4164 /* already configured from SFDP */
4165 } else if (info->addr_width) {
4166 nor->addr_width = info->addr_width;
4168 nor->addr_width = 3;
4171 if (nor->addr_width == 3 && mtd->size > SZ_16M) {
4172 #ifndef CONFIG_SPI_FLASH_BAR
4173 /* enable 4-byte addressing if the device exceeds 16MiB */
4174 nor->addr_width = 4;
4175 if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
4176 info->flags & SPI_NOR_4B_OPCODES)
4177 spi_nor_set_4byte_opcodes(nor, info);
4179 /* Configure the BAR - discover bank cmds and read current bank */
4180 nor->addr_width = 3;
4181 ret = read_bar(nor, info);
4187 if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
4188 dev_dbg(nor->dev, "address width is too large: %u\n",
4193 /* Send all the required SPI flash commands to initialize device */
4194 ret = spi_nor_init(nor);
4198 nor->rdsr_dummy = params.rdsr_dummy;
4199 nor->rdsr_addr_nbytes = params.rdsr_addr_nbytes;
4200 nor->name = info->name;
4201 nor->size = mtd->size;
4202 nor->erase_size = mtd->erasesize;
4203 nor->sector_size = mtd->erasesize;
4205 #ifndef CONFIG_SPL_BUILD
4206 printf("SF: Detected %s with page size ", nor->name);
4207 print_size(nor->page_size, ", erase size ");
4208 print_size(nor->erase_size, ", total ");
4209 print_size(nor->size, "");
4216 /* U-Boot specific functions, need to extend MTD to support these */
4217 int spi_flash_cmd_get_sw_write_prot(struct spi_nor *nor)
4219 int sr = read_sr(nor);
4224 return (sr >> 2) & 7;