[PATCH v2 3/4] mtd: spi-nor: macronix: Enable RDCR for the parts that support it

From: Miquel Raynal

Date: Thu Nov 26 2026 - 08:42:56 EST


Some Macronix parts have support for reading SR2, in which case the
opcode 0x15 will be used. Since the opcode is now cleared by default,
opt-in for the parts whose datasheet documents RDCR.

Sometimes the JEDEC ID is not useful since Macronix has re-used IDs
across incompatible parts. In this case, another discriminant is used on
top (eg. SFDP support or 4B addressing capability):
- 128 Mbit variants (typically MX25L12805D) have no RDCR, but the
MX25L12833F and MX25L12845G are SFDP-capable and have RDCR support.
Use the SFDP presence to discriminate the RDCR capability.
- 256 Mbit variants have no RDCR support, except the MX25L25635F and
MX25L25645G parts which happen to also be 4-byte-capable. Use that
capability to discriminate RDCR support.

Some parts have no RDCR support at all. For those, SFDP overwrites the
manufacturer defaults and restores the JEDEC opcode (0x35) and 16-bit
writes when the BFPT reports QER_NONE. Clear them again from the
catch-all late_init, which runs before the per-part opt-in fixups.

Signed-off-by: Miquel Raynal <miquel.raynal@xxxxxxxxxxx>
---
drivers/mtd/spi-nor/macronix.c | 60 +++++++++++++++++++++++++++++++++++++++---
1 file changed, 56 insertions(+), 4 deletions(-)

diff --git a/drivers/mtd/spi-nor/macronix.c b/drivers/mtd/spi-nor/macronix.c
index 92241f7b0057..9a40c2b14cb4 100644
--- a/drivers/mtd/spi-nor/macronix.c
+++ b/drivers/mtd/spi-nor/macronix.c
@@ -14,6 +14,7 @@
#define MXIC_NOR_ADDR_CR2_DC 0x00000300 /* CR2 address for setting dummy cycles */
#define MXIC_NOR_REG_DOPI_EN 0x2 /* Enable Octal DTR */
#define MXIC_NOR_REG_SPI_EN 0x0 /* Enable SPI */
+#define MXIC_NOR_OP_RDCR 0x15 /* Read Configuration Register opcode */

/* Convert dummy cycles to bit pattern */
#define MXIC_NOR_REG_DC(p) \
@@ -25,6 +26,27 @@
SPI_MEM_OP_NO_DUMMY, \
SPI_MEM_OP_DATA_OUT(ndata, buf, 0))

+/*
+ * RDCR support is not tied to the presence of SFDP, but the SFDP
+ * presence is sometimes enough to discriminate parts with the same
+ * JEDEC ID wrt their RDCR support.
+ */
+static bool macronix_nor_has_sfdp(const struct spi_nor *nor)
+{
+ return nor->sfdp;
+}
+
+static int macronix_rdcr_late_init(struct spi_nor *nor)
+{
+ nor->params->opcodes.read_sr2 = MXIC_NOR_OP_RDCR;
+
+ return 0;
+}
+
+static const struct spi_nor_fixups macronix_rdcr_fixups = {
+ .late_init = macronix_rdcr_late_init,
+};
+
static int
mx25l25635_post_bfpt_fixups(struct spi_nor *nor,
const struct sfdp_parameter_header *bfpt_header,
@@ -38,9 +60,15 @@ mx25l25635_post_bfpt_fixups(struct spi_nor *nor,
* We need a way to differentiate MX25L25635E and MX25L25635F, and it
* seems that the F version advertises support for Fast Read 4-4-4 in
* its BFPT table.
+ *
+ * The MX25L25635F and MX25L25645G variants also have a configuration
+ * register readable with RDCR; the MX25L25635E does not. Use the same
+ * Fast Read 4-4-4 bit to enable both.
*/
- if (bfpt->dwords[SFDP_DWORD(5)] & BFPT_DWORD5_FAST_READ_4_4_4)
+ if (bfpt->dwords[SFDP_DWORD(5)] & BFPT_DWORD5_FAST_READ_4_4_4) {
nor->params->flags |= SNOR_F_4B_OPCODES;
+ nor->params->opcodes.read_sr2 = MXIC_NOR_OP_RDCR;
+ }

return 0;
}
@@ -338,9 +366,22 @@ static void macronix_nor_default_init(struct spi_nor *nor)

static int macronix_nor_late_init(struct spi_nor *nor)
{
- if (!nor->params->set_4byte_addr_mode)
- nor->params->set_4byte_addr_mode = spi_nor_set_4byte_addr_mode_en4b_ex4b;
- nor->params->set_octal_dtr = macronix_nor_set_octal_dtr;
+ struct spi_nor_flash_parameter *params = nor->params;
+
+ /*
+ * SFDP restores the JEDEC RDCR opcode (0x35) and 16-bit writes when
+ * the BFPT reports QER_NONE. Macronix does not use 0x35, and a
+ * 16-bit WRSR would send zeroes to a CR we cannot read. Clear them
+ * again; the per-part 0x15 opt-in fixups run after this one.
+ */
+ if (params->opcodes.read_sr2 == SPINOR_OP_RDCR) {
+ params->opcodes.read_sr2 = 0;
+ params->opcodes.write_sr1_and_sr2 = 0;
+ }
+
+ if (!params->set_4byte_addr_mode)
+ params->set_4byte_addr_mode = spi_nor_set_4byte_addr_mode_en4b_ex4b;
+ params->set_octal_dtr = macronix_nor_set_octal_dtr;

return 0;
}
@@ -352,6 +393,17 @@ static const struct spi_nor_fixups macronix_nor_fixups = {

static const struct spi_nor_fixup macronix_fixups[] = {
{ .fixups = &macronix_nor_fixups },
+ { .id = SNOR_ID(0xc2, 0x20, 0x18), .match = macronix_nor_has_sfdp,
+ .fixups = &macronix_rdcr_fixups },
+ { .id = SNOR_ID(0xc2, 0x20, 0x1a), .fixups = &macronix_rdcr_fixups },
+ { .id = SNOR_ID(0xc2, 0x20, 0x1b), .fixups = &macronix_rdcr_fixups },
+ { .id = SNOR_ID(0xc2, 0x20, 0x1c), .fixups = &macronix_rdcr_fixups },
+ { .id = SNOR_ID(0xc2, 0x23, 0x14), .fixups = &macronix_rdcr_fixups },
+ { .id = SNOR_ID(0xc2, 0x25, 0x3a), .fixups = &macronix_rdcr_fixups },
+ { .id = SNOR_ID(0xc2, 0x25, 0x3b), .fixups = &macronix_rdcr_fixups },
+ { .id = SNOR_ID(0xc2, 0x25, 0x3c), .fixups = &macronix_rdcr_fixups },
+ { .id = SNOR_ID(0xc2, 0x28, 0x15), .fixups = &macronix_rdcr_fixups },
+ { .id = SNOR_ID(0xc2, 0x28, 0x16), .fixups = &macronix_rdcr_fixups },
{ .id = SNOR_ID(0xc2, 0x20, 0x18), .fixups = &mx25l12805d_4pp3b_fixups },
{ .id = SNOR_ID(0xc2, 0x20, 0x19), .fixups = &mx25l25635_fixups },
{ .id = SNOR_ID(0xc2, 0x20, 0x1a), .fixups = &macronix_qpp4b_fixups,

--
2.56.0