Skip to contentSkip to conversation

mtd: spi-nor: winbond: Fix protection handling

PATCH Applied Eliav Farber 4 participants 10 messages Feb 18 – Mar 30

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

v2 cover mtd: spi-nor: winbond: Fix protection handling

This series fixes block protection handling for the following Winbond SPI-NOR devices:
- w25q256jwm
- w25q256jw
- w25q64jvm

The devices do not correctly describe their Status Register layout and protection capabilities (locking flags, TB bit, BP bit configuration). As a result, the spi-nor core rejects protection requests and locking operations fail with -EINVAL, e.g.:

flash_lock -l /dev/mtd29
flash_lock: error!: could not lock device: /dev/mtd29 error 22 (Invalid argument)

Update the device flags to match the actual hardware behavior so that locking and unlocking regions work correctly.

All changes were validated using flash_lock on the affected devices.

Changes in v2:
- Remove last patch in series that added dual/quad read for w25q64jvm
- Added Minimum testing requirements for w25q256jwm and w25q64jvm

Eliav Farber (3):
mtd: spi-nor: winbond: Fix locking support for w25q256jwm
mtd: spi-nor: winbond: Fix locking support for w25q256jw
mtd: spi-nor: winbond: Fix locking support for w25q64jvm

 drivers/mtd/spi-nor/winbond.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v2 1/3 mtd: spi-nor: winbond: Fix locking support for w25q256jwm

The Winbond w25q256jwm device supports four Block Protect (BP) bits and uses Status Register bit 6 as the Top/Bottom (TB) protect bit.

Update the flash parameters by enabling SPI_NOR_4BIT_BP and SPI_NOR_TB_SR_BIT6. Without these flags, the locking configuration is incorrect.

Reference:
https://www.winbond.com/hq/support/documentation/levelOne.jsp?__locale=en&DocNo=DA00-W25Q256JW.1

---
This flash is populated on the al11_k2v6_jrd10 board and was tested at 35700000 frequency using the amazon,alpine-dw-apb-ssi SPI controller.

root@alpine:~# cat /sys/bus/spi/devices/spi3.0/spi-nor/partname
w25q256jwm
root@alpine:~# cat /sys/bus/spi/devices/spi3.0/spi-nor/jedec_id 
ef8019
root@alpine:~# cat /sys/bus/spi/devices/spi3.0/spi-nor/manufacturer 
winbond
root@alpine:~# xxd -p /sys/bus/spi/devices/spi3.0/spi-nor/sfdp 
53464450060101ff00060110800000ff84000102d00000ffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffe520fbffffffff0f44eb086b083b42bbfeffffffffff
0000ffff40eb0c200f5210d800003602a60082ea14d3e96376337a757a75
f7bdd55c19f75dffe970f9a5ffffffffffffffffffffffffffffffffff0a
f0ff21ffdcff 
root@alpine:~# cat /sys/kernel/debug/spi-nor/spi3.0/capabilities
Supported read modes by the flash
 1S-1S-1S
  opcode        0x13
  mode cycles   0
  dummy cycles  0
 1S-1S-2S
  opcode        0x3c
  mode cycles   0
  dummy cycles  8
 1S-2S-2S
  opcode        0xbc
  mode cycles   2
  dummy cycles  2
 1S-1S-4S
  opcode        0x6c
  mode cycles   0
  dummy cycles  8
 1S-4S-4S
  opcode        0xec
  mode cycles   2
  dummy cycles  4
 4S-4S-4S
  opcode        0xec
  mode cycles   2
  dummy cycles  0

Supported page program modes by the flash
1S-1S-1S
opcode 0x12
1S-1S-4S
opcode 0x34

root@alpine:~# cat /sys/kernel/debug/spi-nor/spi3.0/params
name            w25q256jwm
id              ef 80 19 00 00 00
size            32.0 MiB
write size      1
page size       256
address nbytes  4
flags           HAS_SR_TB | 4B_OPCODES | HAS_4BAIT | HAS_LOCK | HAS_16BIT_SR | HAS_SR_TB_BIT6 | HAS_4BIT_BP | SOFT_RESET

opcodes

 read           0x13
  dummy cycles  0
 erase          0x21
 program        0x12
 8D extension   none

protocols

 read           1S-1S-1S
 write          1S-1S-1S
 register       1S-1S-1S

erase commands
21 (4.00 KiB) [1]
dc (64.0 KiB) [3]
c7 (32.0 MiB)

sector map
region (in hex) | erase mask | overlaid
------------------+------------+----------
00000000-01ffffff | [ 1 ] | no

root@alpine:~# dd if=/dev/urandom of=./spi_test bs=65536 count=1
1+0 records in
1+0 records out
65536 bytes (66 kB, 64 KiB) copied, 0.0002316 s, 283 MB/s
root@alpine:~# mtd_debug erase /dev/mtd22 0 65536
Erased 65536 bytes from address 0x00000000 in flash
root@alpine:~# mtd_debug read /dev/mtd22 0 65536 spi_read
Copied 65536 bytes from address 0x00000000 in flash to spi_read
root@alpine:~# hexdump spi_read
0000000 ffff ffff ffff ffff ffff ffff ffff ffff
*
0010000
root@alpine:~# sha256sum spi_read
71189f7fb6aed638640078fba3a35fda6c39c8962e74dcc75935aac948da9063  spi_read
root@alpine:~# mtd_debug write /dev/mtd22 0 65536 spi_test
Copied 65536 bytes from spi_test to address 0x00000000 in flash
root@alpine:~# mtd_debug read /dev/mtd22 0 65536 spi_read
Copied 65536 bytes from address 0x00000000 in flash to spi_read	
root@alpine:~# sha256sum spi*
b16b45f0cb74ba65d81766e9007d8cd1fbd3ac7ca7fbbd76fa4d181c3c1d52c0  spi_read
b16b45f0cb74ba65d81766e9007d8cd1fbd3ac7ca7fbbd76fa4d181c3c1d52c0  spi_test
root@alpine:~# mtd_debug erase /dev/mtd22 0 65536
Erased 65536 bytes from address 0x00000000 in flash
root@alpine:~# mtd_debug read /dev/mtd22 0 65536 spi_read
Copied 65536 bytes from address 0x00000000 in flash to spi_read
root@alpine:~# sha256sum spi*
71189f7fb6aed638640078fba3a35fda6c39c8962e74dcc75935aac948da9063  spi_read
b16b45f0cb74ba65d81766e9007d8cd1fbd3ac7ca7fbbd76fa4d181c3c1d52c0  spi_test
root@alpine:~# mtd_debug info /dev/mtd22
mtd.type = MTD_NORFLASH
mtd.flags = MTD_CAP_NORFLASH
mtd.size = 65536 (64K)
mtd.erasesize = 4096 (4K)
mtd.writesize = 1 
mtd.oobsize = 0 
regions = 0
 drivers/mtd/spi-nor/winbond.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/spi-nor/winbond.c +1 -1
index fb855fe44733..55f1209936d5 100644
@@ -337,7 +337,7 @@ static const struct flash_info winbond_nor_parts[] = {
 		.id = SNOR_ID(0xef, 0x80, 0x19),
 		.name = "w25q256jwm",
 		.size = SZ_32M,
-		.flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
+		.flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB | SPI_NOR_TB_SR_BIT6 | SPI_NOR_4BIT_BP,
 		.no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
 	}, {
 		.id = SNOR_ID(0xef, 0x80, 0x20),
Signature · 1 line
2.47.3
#2 lore raw

PATCH v2 2/3 mtd: spi-nor: winbond: Fix locking support for w25q256jw

In reply to Eliav Farber

The Winbond w25q256jw device:
- Supports lock/unlock via SR.
- Has Top/Bottom (TB) protect bit.
- Uses Status Register bit 6 as the Top/Bottom (TB) protect bit.
- Supports four Block Protect (BP) bits.

Update the flash parameters by enabling SPI_NOR_HAS_LOCK, SPI_NOR_HAS_TB, SPI_NOR_TB_SR_BIT6 and SPI_NOR_4BIT_BP. Without these flags, the locking configuration is incorrect.

Reference:
https://www.winbond.com/hq/support/documentation/levelOne.jsp?__locale=en&DocNo=DA00-W25Q256JW.1

---
 drivers/mtd/spi-nor/winbond.c | 1 +
 1 file changed, 1 insertion(+)
drivers/mtd/spi-nor/winbond.c +1 -0
index 55f1209936d5..6d325da121b4 100644
@@ -274,6 +274,7 @@ static const struct flash_info winbond_nor_parts[] = {
 		.id = SNOR_ID(0xef, 0x60, 0x19),
 		.name = "w25q256jw",
 		.size = SZ_32M,
+		.flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB | SPI_NOR_TB_SR_BIT6 | SPI_NOR_4BIT_BP,
 		.no_sfdp_flags = SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ,
 	}, {
 		.id = SNOR_ID(0xef, 0x60, 0x20),
Signature · 1 line
2.47.3
#3 lore raw

PATCH v2 3/3 mtd: spi-nor: winbond: Fix locking support for w25q64jvm

In reply to Eliav Farber

The Winbond w25q64jvm supports block protection through the Status Register (SR) and provides a Top/Bottom (TB) protection bit.

Enable SPI_NOR_HAS_LOCK and SPI_NOR_HAS_TB for this device to properly describe its locking capabilities.

The device uses Status Register bit 5 as the TB bit and supports only three Block Protect (BP) bits. Therefore, do not set SPI_NOR_TB_SR_BIT6 or SPI_NOR_4BIT_BP.

Reference:
https://www.winbond.com/hq/support/documentation/levelOne.jsp?__locale=en&DocNo=DA00-W25Q64JV.1

---
This flash is populated on the al10_k2v5_jrd_aio_makalu board and was tested at 35700000 frequency using the amazon,alpine-dw-apb-ssi SPI controller.

~ # cat /sys/bus/spi/devices/spi0.2/spi-nor/partname w25q64jvm
~ # cat /sys/bus/spi/devices/spi0.2/spi-nor/jedec_id ef7017
~ # cat /sys/bus/spi/devices/spi0.2/spi-nor/manufacturer winbond

~ # xxd -p /sys/bus/spi/devices/spi0.2/spi-nor/sfdp 53464450050100ff00050110800000ffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ffffffffffffffffe520f9ffffffff0344eb086b083b42bbfeffffffffff 0000ffff40eb0c200f5210d800003602a60082ea14c4e96376337a757a75 f7a2d55c19f74dffe930f880

~ # cat /sys/kernel/debug/spi-nor/spi0.2/capabilities Supported read modes by the flash
1S-1S-1S

  opcode        0x03
  mode cycles   0
  dummy cycles  0
 1S-1S-2S
  opcode        0x3b
  mode cycles   0
  dummy cycles  8
 1S-2S-2S
  opcode        0xbb
  mode cycles   2
  dummy cycles  2
 1S-1S-4S
  opcode        0x6b
  mode cycles   0
  dummy cycles  8
 1S-4S-4S
  opcode        0xeb
  mode cycles   2
  dummy cycles  4
 4S-4S-4S
  opcode        0xeb
  mode cycles   2
  dummy cycles  0

Supported page program modes by the flash
1S-1S-1S
opcode 0x02

~ # cat /sys/kernel/debug/spi-nor/spi0.2/params

name            w25q64jvm
id              ef 70 17 00 00 00
size            8.00 MiB
write size      1
page size       256
address nbytes  3
flags           HAS_SR_TB | HAS_LOCK | HAS_16BIT_SR | NO_READ_CR | SOFT_RESET

opcodes

 read           0x03
  dummy cycles  0
 erase          0x20
 program        0x02
 8D extension   none

protocols

 read           1S-1S-1S
 write          1S-1S-1S
 register       1S-1S-1S

erase commands
20 (4.00 KiB) [1]
52 (32.0 KiB) [2]
d8 (64.0 KiB) [3]
c7 (8.00 MiB)

sector map
region (in hex) | erase mask | overlaid
------------------+------------+----------
00000000-007fffff | [ 1 ] | no

/tmp # dd if=/dev/urandom of=./spi_test bs=1M count=2 2+0 records in
2+0 records out
2097152 bytes (2.0MB) copied, 0.010381 seconds, 192.7MB/s

/tmp # ./mtd_debug erase /dev/mtd2 0 2097152 Erased 2097152 bytes from address 0x00000000 in flash

/tmp # ./mtd_debug read /dev/mtd2 0 2097152 spi_read Copied 2097152 bytes from address 0x00000000 in flash to spi_read

/tmp # hexdump spi_read
0000000 ffff ffff ffff ffff ffff ffff ffff ffff
*
0200000
/tmp # sha256sum spi_read 4bda3a28f4ffe603c0ec1258c0034d65a1a0d35ab7bd523a834608adabf03cc5 spi_read

/tmp # mtd_debug write /dev/mtd0 0 2097152 spi_test -sh: mtd_debug: not found
/tmp # ./mtd_debug write /dev/mtd2 0 2097152 spi_test Copied 2097152 bytes from spi_test to address 0x00000000 in flash

/tmp # ./mtd_debug read /dev/mtd2 0 2097152 spi_read Copied 2097152 bytes from address 0x00000000 in flash to spi_read

/tmp # sha256sum spi* 555b3718aa92b1313ff7f93e75306267a8ed0c5f4d92cc63c1eac4366731059b spi_read 555b3718aa92b1313ff7f93e75306267a8ed0c5f4d92cc63c1eac4366731059b spi_test

/tmp # ./mtd_debug erase /dev/mtd2 0 2097152 Erased 2097152 bytes from address 0x00000000 in flash

/tmp # ./mtd_debug read /dev/mtd2 0 2097152 spi_read Copied 2097152 bytes from address 0x00000000 in flash to spi_read

/tmp # sha256sum spi* 4bda3a28f4ffe603c0ec1258c0034d65a1a0d35ab7bd523a834608adabf03cc5 spi_read 555b3718aa92b1313ff7f93e75306267a8ed0c5f4d92cc63c1eac4366731059b spi_test

/tmp # ./mtd_debug info /dev/mtd2
mtd.type = MTD_NORFLASH
mtd.flags = MTD_CAP_NORFLASH
mtd.size = 8388608 (8M)
mtd.erasesize = 4096 (4K)
mtd.writesize = 1
mtd.oobsize = 0
regions = 0

 drivers/mtd/spi-nor/winbond.c | 1 +
 1 file changed, 1 insertion(+)
drivers/mtd/spi-nor/winbond.c +1 -0
index 6d325da121b4..eaa547d36aad 100644
@@ -296,6 +296,7 @@ static const struct flash_info winbond_nor_parts[] = {
 		.id = SNOR_ID(0xef, 0x70, 0x17),
 		.name = "w25q64jvm",
 		.size = SZ_8M,
+		.flags = SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB,
 		.no_sfdp_flags = SECT_4K,
 	}, {
 		.id = SNOR_ID(0xef, 0x70, 0x18),
Signature · 1 line
2.47.3
maintainer
#4 lore raw
In reply to Eliav Farber

Hi Eliav,

Quoted 15 lines · Eliav Farber

This series fixes block protection handling for the following Winbond SPI-NOR devices:
- w25q256jwm
- w25q256jw
- w25q64jvm

The devices do not correctly describe their Status Register layout and protection capabilities (locking flags, TB bit, BP bit configuration). As a result, the spi-nor core rejects protection requests and locking operations fail with -EINVAL, e.g.:

flash_lock -l /dev/mtd29
flash_lock: error!: could not lock device: /dev/mtd29 error 22 (Invalid argument)

Quoted 7 lines

Update the device flags to match the actual hardware behavior so that locking and unlocking regions work correctly.

All changes were validated using flash_lock on the affected devices.

Changes in v2:
- Remove last patch in series that added dual/quad read for w25q64jvm

Why? Unfortunately, you didn't answer my questions.

Quoted 1 line

- Added Minimum testing requirements for w25q256jwm and w25q64jvm

Thanks, but why was the w25q256jw left out?

Quoted 4 lines

Eliav Farber (3):
mtd: spi-nor: winbond: Fix locking support for w25q256jwm
mtd: spi-nor: winbond: Fix locking support for w25q256jw
mtd: spi-nor: winbond: Fix locking support for w25q64jvm

You're supposed to pick the Reviewed-by: tags up before sending a new version, see [1].

-michael

[1] https://docs.kernel.org/process/submitting-patches.html

Quoted 3 lines
 drivers/mtd/spi-nor/winbond.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
#5 lore raw
Quoted 4 lines
Quoted 2 lines

Changes in v2:
- Remove last patch in series that added dual/quad read for w25q64jvm

Why? Unfortunately, you didn't answer my questions.

The SFDP dump I added indicates that both dual-read and quad-read modes are supported.

Quoted 3 lines
Quoted 1 line

- Added Minimum testing requirements for w25q256jwm and w25q64jvm

Thanks, but why was the w25q256jw left out?

I still haven’t been able to get a card with w25q256jw. Is it needed in order to add the lock flags?

---
Regards, Eliav

maintainer
#6 lore raw
Quoted 7 lines · Eliav Farber
Quoted 4 lines
Quoted 2 lines

Changes in v2:
- Remove last patch in series that added dual/quad read for w25q64jvm

Why? Unfortunately, you didn't answer my questions.

The SFDP dump I added indicates that both dual-read and quad-read modes are supported.

That was already the case for your initial submission. So what has changed?

If you're a bit more verbose in the changelog, that would make things much easier for us reviewers.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

- Added Minimum testing requirements for w25q256jwm and w25q64jvm

Thanks, but why was the w25q256jw left out?

I still haven’t been able to get a card with w25q256jw. Is it needed in order to add the lock flags?

Not from my side, other maintainers may disagree. I'm more interested in the SFDP dump. In any case, you already have my Reviewed-by.

-michael

maintainer Applied
#7 lore raw
In reply to Eliav Farber
Quoted 11 lines · Eliav Farber

This series fixes block protection handling for the following Winbond SPI-NOR devices:
- w25q256jwm
- w25q256jw
- w25q64jvm

The devices do not correctly describe their Status Register layout and protection capabilities (locking flags, TB bit, BP bit configuration). As a result, the spi-nor core rejects protection requests and locking operations fail with -EINVAL, e.g.:

Quoted 8 lines

flash_lock -l /dev/mtd29
flash_lock: error!: could not lock device: /dev/mtd29 error 22 (Invalid argument)

Update the device flags to match the actual hardware behavior so that locking and unlocking regions work correctly.

All changes were validated using flash_lock on the affected devices.

Applied patches 1 and 3 to spi-nor/next. Thanks!

Skipped patch 2 since you say that you haven't got a device to test with. I don't have a very strong opinion on this, but I do think we should test patches on real hardware just to we get some sanity checking for the patch.

[...]

Signature · 2 lines
Regards,
Pratyush Yadav
maintainer
#8 lore raw
Quoted 13 lines
Quoted 13 lines

The devices do not correctly describe their Status Register layout and protection capabilities (locking flags, TB bit, BP bit configuration). As a result, the spi-nor core rejects protection requests and locking operations fail with -EINVAL, e.g.:

flash_lock -l /dev/mtd29
flash_lock: error!: could not lock device: /dev/mtd29 error 22 (Invalid argument)

Update the device flags to match the actual hardware behavior so that locking and unlocking regions work correctly.

Quoted 8 lines
Quoted 1 line

All changes were validated using flash_lock on the affected devices.

Applied patches 1 and 3 to spi-nor/next. Thanks!

Skipped patch 2 since you say that you haven't got a device to test with. I don't have a very strong opinion on this, but I do think we should test patches on real hardware just to we get some sanity checking for the patch.

Honestly, I am pretty convinced patch 2 cannot make more harm as the block protection is already badly broken by not being described at all. Furthermore, for similar chips I have, these bits are relevant, so I would rather be in favour of going forward with patch 2, because at most it will just not fix the behaviour as it pretends it does.

My 2cts :)

Miquèl

maintainer
#9 lore raw
Quoted 21 lines · Miquel Raynal
Quoted 12 lines
Quoted 12 lines

The devices do not correctly describe their Status Register layout and protection capabilities (locking flags, TB bit, BP bit configuration). As a result, the spi-nor core rejects protection requests and locking operations fail with -EINVAL, e.g.:

flash_lock -l /dev/mtd29
flash_lock: error!: could not lock device: /dev/mtd29 error 22 (Invalid argument)

Update the device flags to match the actual hardware behavior so that locking and unlocking regions work correctly.

Quoted 8 lines
Quoted 1 line

All changes were validated using flash_lock on the affected devices.

Applied patches 1 and 3 to spi-nor/next. Thanks!

Skipped patch 2 since you say that you haven't got a device to test with. I don't have a very strong opinion on this, but I do think we should test patches on real hardware just to we get some sanity checking for the patch.

Quoted 7 lines

Honestly, I am pretty convinced patch 2 cannot make more harm as the block protection is already badly broken by not being described at all. Furthermore, for similar chips I have, these bits are relevant, so I would rather be in favour of going forward with patch 2, because at most it will just not fix the behaviour as it pretends it does.

My 2cts :)

Hmm, okay, I'll apply that one too then.

Signature · 2 lines
Regards,
Pratyush Yadav