Skip to contentSkip to conversation

mtd: spi-nor: Fix SST AAI write mode opcode handling

PATCH In review Sanjaikumar V S 3 participants 4 messages Mar 31 – Sep 11

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 mtd: spi-nor: Fix SST AAI write mode opcode handling

From: Sanjaikumar V S <[email protected]>

When the SPI controller lacks direct mapping support, the fallback path in spi_nor_spimem_write_data() uses nor->write_proto based operation template. However, this template uses the standard page program opcode set during probe, not the AAI opcode required for SST flash.

Additionally, controllers that do support direct mapping will also use the wrong opcode since the dirmap template is created at probe time with the standard page program opcode.

Fix this by:
1. Checking the nodirmap flag in spi_nor_spimem_write_data() to ensure the code falls through to spi_nor_spimem_exec_op() path which builds the operation at runtime with the correct program_opcode.
2. Setting nodirmap=true for SST AAI devices in sst_nor_late_init() to disable dirmap and force the runtime opcode path.

This only affects SST devices with SST_WRITE flag. Other SST devices that use standard page program can still benefit from dirmap.

Fixes df5c21002cf4 ("mtd: spi-nor: use spi-mem dirmap API")

---
Changes since v4:
- Disable dirmap for SST AAI devices in sst_nor_late_init() to fix the case when controller supports direct mapping (Pratyush)
- Updated commit message and subject to reflect the broader fix

Note: Patch 1/2 from v4 series is already in spi-nor/next.

I don't have hardware to test the new sst.c change. Hendrik, could you please verify this on your SST25VF032B setup?

 drivers/mtd/spi-nor/core.c |  2 +-
 drivers/mtd/spi-nor/sst.c  | 10 +++++++++-
 2 files changed, 10 insertions(+), 2 deletions(-)
drivers/mtd/spi-nor/core.c +1 -1
index e6c1fda61f57..2e4b167cab57 100644
@@ -281,7 +281,7 @@ static ssize_t spi_nor_spimem_write_data(struct spi_nor *nor, loff_t to,
 	if (spi_nor_spimem_bounce(nor, &op))
 		memcpy(nor->bouncebuf, buf, op.data.nbytes);
 
-	if (nor->dirmap.wdesc) {
+	if (nor->dirmap.wdesc && !nor->dirmap.wdesc->nodirmap) {
 		nbytes = spi_mem_dirmap_write(nor->dirmap.wdesc, op.addr.val,
 					      op.data.nbytes, op.data.buf.out);
 	} else {
drivers/mtd/spi-nor/sst.c +9 -1
index db02c14ba16f..cd2f04830a6b 100644
@@ -267,8 +267,16 @@ static int sst_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
 
 static int sst_nor_late_init(struct spi_nor *nor)
 {
-	if (nor->info->mfr_flags & SST_WRITE)
+	if (nor->info->mfr_flags & SST_WRITE) {
 		nor->mtd._write = sst_nor_write;
+		/*
+		 * AAI mode requires dynamic opcode changes (BP vs AAI_WP).
+		 * Disable dirmap to ensure spi_nor_spimem_exec_op() uses
+		 * the runtime opcode instead of the dirmap template.
+		 */
+		if (nor->dirmap.wdesc)
+			nor->dirmap.wdesc->nodirmap = true;
+	}
 
 	return 0;
 }
Signature · 1 line
2.43.0
Tested-by Reviewed-by on v5 1/1
#1 lore raw

Hello,

Quoted 30 lines · Sanjaikumar V S

From: Sanjaikumar V S <[email protected]>

When the SPI controller lacks direct mapping support, the fallback path in spi_nor_spimem_write_data() uses nor->write_proto based operation template. However, this template uses the standard page program opcode set during probe, not the AAI opcode required for SST flash.

Additionally, controllers that do support direct mapping will also use the wrong opcode since the dirmap template is created at probe time with the standard page program opcode.

Fix this by:
1. Checking the nodirmap flag in spi_nor_spimem_write_data() to ensure the code falls through to spi_nor_spimem_exec_op() path which builds the operation at runtime with the correct program_opcode.
2. Setting nodirmap=true for SST AAI devices in sst_nor_late_init() to disable dirmap and force the runtime opcode path.

This only affects SST devices with SST_WRITE flag. Other SST devices that use standard page program can still benefit from dirmap.

Fixes df5c21002cf4 ("mtd: spi-nor: use spi-mem dirmap API")

---
Changes since v4:
- Disable dirmap for SST AAI devices in sst_nor_late_init() to fix the case when controller supports direct mapping (Pratyush)
- Updated commit message and subject to reflect the broader fix

Quoted 5 lines

Note: Patch 1/2 from v4 series is already in spi-nor/next.

I don't have hardware to test the new sst.c change. Hendrik, could you please verify this on your SST25VF032B setup?

retested, works the same as v4 on the SST25VF032B, don't have other SST flashes at hand to test though.

Regards,
Hendrik

Quoted 39 lines
  drivers/mtd/spi-nor/core.c |  2 +-
  drivers/mtd/spi-nor/sst.c  | 10 +++++++++-
  2 files changed, 10 insertions(+), 2 deletions(-)
drivers/mtd/spi-nor/core.c +1 -1
index e6c1fda61f57..2e4b167cab57 100644
@@ -281,7 +281,7 @@ static ssize_t spi_nor_spimem_write_data(struct spi_nor *nor, loff_t to,
  	if (spi_nor_spimem_bounce(nor, &op))
  		memcpy(nor->bouncebuf, buf, op.data.nbytes);
 
-	if (nor->dirmap.wdesc) {
+	if (nor->dirmap.wdesc && !nor->dirmap.wdesc->nodirmap) {
  		nbytes = spi_mem_dirmap_write(nor->dirmap.wdesc, op.addr.val,
  					      op.data.nbytes, op.data.buf.out);
  	} else {
drivers/mtd/spi-nor/sst.c +9 -1
index db02c14ba16f..cd2f04830a6b 100644
@@ -267,8 +267,16 @@ static int sst_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
 
  static int sst_nor_late_init(struct spi_nor *nor)
  {
-	if (nor->info->mfr_flags & SST_WRITE)
+	if (nor->info->mfr_flags & SST_WRITE) {
  		nor->mtd._write = sst_nor_write;
+		/*
+		 * AAI mode requires dynamic opcode changes (BP vs AAI_WP).
+		 * Disable dirmap to ensure spi_nor_spimem_exec_op() uses
+		 * the runtime opcode instead of the dirmap template.
+		 */
+		if (nor->dirmap.wdesc)
+			nor->dirmap.wdesc->nodirmap = true;
+	}
 
  	return 0;
  }
#2 lore raw

Hi Pratyush,

In v4 you suggested updating dirmap_info with the right opcodes. I went with a different approach in v5 -- disabling dirmap for SST AAI devices in sst_nor_late_init() instead. The reasoning is that updating dirmap_info at runtime is problematic since AAI requires dynamic opcode and address byte changes per write, and controllers may cache the template at dirmap_create time.

Hendrik has tested this approach on his SST25VF032B.

Does this approach work for you, or would you prefer a different direction?

Thanks,
Sanjaikumar

maintainer
#3 lore raw
In reply to Sanjaikumar V S
Quoted 6 lines · Sanjaikumar V S

From: Sanjaikumar V S <[email protected]>

When the SPI controller lacks direct mapping support, the fallback path in spi_nor_spimem_write_data() uses nor->write_proto based operation template. However, this template uses the standard page program opcode set during probe, not the AAI opcode required for SST flash.

So it looks like that mtd->_write override in sst.c is rather hacky..

Quoted 10 lines

Additionally, controllers that do support direct mapping will also use the wrong opcode since the dirmap template is created at probe time with the standard page program opcode.

Fix this by:
1. Checking the nodirmap flag in spi_nor_spimem_write_data() to ensure the code falls through to spi_nor_spimem_exec_op() path which builds the operation at runtime with the correct program_opcode.
2. Setting nodirmap=true for SST AAI devices in sst_nor_late_init() to disable dirmap and force the runtime opcode path.

I'd be fine with disabling dirmap support as a workaround. But I'm not sure if we are supposed to change the nodirmap property as that is a property owned/set by the controller.

Could we just compare the mtd->_write == spi_nor_write and then skip the dirmap support? With a big here be dragons warning.

-michael