mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v5] mtd: spi-nor: Fix SST AAI write mode opcode handling
@ 2026-03-31  9:50 Sanjaikumar V S
  2026-04-01 15:53 ` Hendrik Donner
  2026-09-11  8:09 ` Michael Walle
  0 siblings, 2 replies; 4+ messages in thread
From: Sanjaikumar V S @ 2026-03-31  9:50 UTC (permalink / raw)
  To: mwalle, pratyush
  Cc: hd, linux-kernel, linux-mtd, miquel.raynal, richard,
	sanjaikumar.vs, sanjaikumarvs, stable, tudor.ambarus, vigneshr

From: Sanjaikumar V S <sanjaikumar.vs@dicortech.com>

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")
Cc: stable@vger.kernel.org
Signed-off-by: Sanjaikumar V S <sanjaikumar.vs@dicortech.com>
---
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(-)

diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-nor/core.c
index e6c1fda61f57..2e4b167cab57 100644
--- a/drivers/mtd/spi-nor/core.c
+++ b/drivers/mtd/spi-nor/core.c
@@ -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 {
diff --git a/drivers/mtd/spi-nor/sst.c b/drivers/mtd/spi-nor/sst.c
index db02c14ba16f..cd2f04830a6b 100644
--- a/drivers/mtd/spi-nor/sst.c
+++ b/drivers/mtd/spi-nor/sst.c
@@ -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.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v5] mtd: spi-nor: Fix SST AAI write mode opcode handling
  2026-03-31  9:50 [PATCH v5] mtd: spi-nor: Fix SST AAI write mode opcode handling Sanjaikumar V S
@ 2026-04-01 15:53 ` Hendrik Donner
  2026-04-20  9:02   ` Sanjaikumar V S
  2026-09-11  8:09 ` Michael Walle
  1 sibling, 1 reply; 4+ messages in thread
From: Hendrik Donner @ 2026-04-01 15:53 UTC (permalink / raw)
  To: Sanjaikumar V S, mwalle, pratyush
  Cc: linux-kernel, linux-mtd, miquel.raynal, richard, sanjaikumar.vs,
	stable, tudor.ambarus, vigneshr

Hello,

On 3/31/26 11:50, Sanjaikumar V S wrote:
> From: Sanjaikumar V S <sanjaikumar.vs@dicortech.com>
> 
> 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")
> Cc: stable@vger.kernel.org
> Signed-off-by: Sanjaikumar V S <sanjaikumar.vs@dicortech.com>
> ---
> 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?
> 

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

Tested-by: Hendrik Donner <hd@os-cillation.de>
Reviewed-by: Hendrik Donner <hd@os-cillation.de>

Regards,
Hendrik

>   drivers/mtd/spi-nor/core.c |  2 +-
>   drivers/mtd/spi-nor/sst.c  | 10 +++++++++-
>   2 files changed, 10 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-nor/core.c
> index e6c1fda61f57..2e4b167cab57 100644
> --- a/drivers/mtd/spi-nor/core.c
> +++ b/drivers/mtd/spi-nor/core.c
> @@ -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 {
> diff --git a/drivers/mtd/spi-nor/sst.c b/drivers/mtd/spi-nor/sst.c
> index db02c14ba16f..cd2f04830a6b 100644
> --- a/drivers/mtd/spi-nor/sst.c
> +++ b/drivers/mtd/spi-nor/sst.c
> @@ -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;
>   }


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v5] mtd: spi-nor: Fix SST AAI write mode opcode handling
  2026-04-01 15:53 ` Hendrik Donner
@ 2026-04-20  9:02   ` Sanjaikumar V S
  0 siblings, 0 replies; 4+ messages in thread
From: Sanjaikumar V S @ 2026-04-20  9:02 UTC (permalink / raw)
  To: pratyush
  Cc: hd, linux-kernel, linux-mtd, miquel.raynal, mwalle, richard,
	sanjaikumar.vs, sanjaikumarvs, stable, tudor.ambarus, vigneshr

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v5] mtd: spi-nor: Fix SST AAI write mode opcode handling
  2026-03-31  9:50 [PATCH v5] mtd: spi-nor: Fix SST AAI write mode opcode handling Sanjaikumar V S
  2026-04-01 15:53 ` Hendrik Donner
@ 2026-09-11  8:09 ` Michael Walle
  1 sibling, 0 replies; 4+ messages in thread
From: Michael Walle @ 2026-09-11  8:09 UTC (permalink / raw)
  To: Sanjaikumar V S, pratyush
  Cc: hd, linux-kernel, linux-mtd, miquel.raynal, richard,
	sanjaikumar.vs, stable, tudor.ambarus, vigneshr

[-- Attachment #1: Type: text/plain, Size: 1355 bytes --]

On Tue Mar 31, 2026 at 11:50 AM CEST, Sanjaikumar V S wrote:
> From: Sanjaikumar V S <sanjaikumar.vs@dicortech.com>
>
> 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..

> 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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 297 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-09-11  8:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-03-31  9:50 [PATCH v5] mtd: spi-nor: Fix SST AAI write mode opcode handling Sanjaikumar V S
2026-04-01 15:53 ` Hendrik Donner
2026-04-20  9:02   ` Sanjaikumar V S
2026-09-11  8:09 ` Michael Walle

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®