mtd: spi-nor: otp: 4 byte mode fix and erase support
Subsystems (2)
v6 cover mtd: spi-nor: otp: 4 byte mode fix and erase support
This series is the follow up on the single patch
mtd: spi-nor: implement OTP erase for Winbond and similar flashes
Pratyush Yadav discovered a likely problem with bigger flashes, the address to access the security registers is either 3 or 4 byte (at least for winbond flashes).
Changes since v5:
- squash new patch into "Fixes:" patch
Changes since v4:
- add new patch to get rid taking the spi lock if length is zero. Please note, that I didn't squash this into the "Fixes:" patch because it is unrelated to the actual bug.
- add comment which explains that we could also branch on an error in spi_nor_mtd_otp_range_is_locked()
- check zero length in spi_nor_mtd_otp_erase() and return early before taking the lock
Changes since v3:
- new patch to check for read-only OTP regions before writing
- clarify term "security register"
- don't combine lock and erase functions anymore. there are now more difference than similarities.
Changes since v2:
- fix 3/4 byte mode access
- use spi_nor_erase_sector() by swapping the nor->erase_opcode
- use more consistent wording regarding the security registers
Changes since v1:
- fixed kernel doc
Michael Walle (4):
mtd: spi-nor: otp: fix access to security registers in 4 byte mode
mtd: spi-nor: otp: use more consistent wording
mtd: spi-nor: otp: return -EROFS if region is read-only
mtd: spi-nor: otp: implement erase for Winbond and similar flashes
drivers/mtd/spi-nor/core.c | 2 +- drivers/mtd/spi-nor/core.h | 4 + drivers/mtd/spi-nor/otp.c | 156 +++++++++++++++++++++++++++++++--- drivers/mtd/spi-nor/winbond.c | 1 + 4 files changed, 149 insertions(+), 14 deletions(-)
Signature · 1 line
2.20.1
PATCH v6 1/4 mtd: spi-nor: otp: fix access to security registers in 4 byte mode
The security registers either take a 3 byte or a 4 byte address offset, depending on the address mode of the flash. Thus just leave the nor->addr_width as is.
--- drivers/mtd/spi-nor/otp.c | 2 -- 1 file changed, 2 deletions(-)
drivers/mtd/spi-nor/otp.c +0 -2
@@ -40,7 +40,6 @@ int spi_nor_otp_read_secr(struct spi_nor *nor, loff_t addr, size_t len, u8 *buf) rdesc = nor->dirmap.rdesc; nor->read_opcode =SPINOR_OP_RSECR ; - nor->addr_width =3 ; nor->read_dummy =8 ; nor->read_proto =SNOR_PROTO_1_1_1 ; nor->dirmap.rdesc =NULL ;
@@ -84,7 +83,6 @@ int spi_nor_otp_write_secr(struct spi_nor *nor, loff_t addr, size_t len, wdesc = nor->dirmap.wdesc; nor->program_opcode =SPINOR_OP_PSECR ; - nor->addr_width =3 ; nor->write_proto =SNOR_PROTO_1_1_1 ; nor->dirmap.wdesc =NULL ;
Signature · 1 line
2.20.1
PATCH v6 2/4 mtd: spi-nor: otp: use more consistent wording
In reply to Michael WalleUse the wording as used in the datasheet to describe the access methods of the security registers (aka OTP storage). This will also match the function names.
--- drivers/mtd/spi-nor/otp.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)
drivers/mtd/spi-nor/otp.c +19 -8
@@ -15,14 +15,21 @@#define spi_nor_otp_n_regions (nor) ((nor)->params->otp.org->n_regions)/** -* spi_nor_otp_read_secr() - read OTP data +* spi_nor_otp_read_secr() - read security register * @nor: pointer to 'struct spi_nor' * @addr: offset to read from * @len: number of bytes to read * @buf: pointer to dst buffer * -* Read OTP data from one region by using the SPINOR_OP_RSECR commands. This -* method is used on GigaDevice and Winbond flashes. +* Read a security register by using the SPINOR_OP_RSECR commands. +* +* In Winbond/GigaDevice datasheets the term "security register" stands for +* an one-time-programmable memory area, consisting of multiple bytes (usually +* 256). Thus one "security register" maps to one OTP region. +* +* This method is used on GigaDevice and Winbond flashes. +* +* Please note, the read must not span multiple registers. * * Return: number of bytes read successfully, -errno otherwise */
@@ -56,16 +63,20 @@ int spi_nor_otp_read_secr(struct spi_nor *nor, loff_t addr, size_t len, u8 *buf) }/** -* spi_nor_otp_write_secr() - write OTP data +* spi_nor_otp_write_secr() - write security register * @nor: pointer to 'struct spi_nor' * @addr: offset to write to * @len: number of bytes to write * @buf: pointer to src buffer * -* Write OTP data to one region by using the SPINOR_OP_PSECR commands. This -* method is used on GigaDevice and Winbond flashes. +* Write a security register by using the SPINOR_OP_PSECR commands. +* +* For more information on the term "security register", see the documentation +* of spi_nor_otp_read_secr(). +* +* This method is used on GigaDevice and Winbond flashes. * -* Please note, the write must not span multiple OTP regions. +* Please note, the write must not span multiple registers. * * Return: number of bytes written successfully, -errno otherwise */
@@ -88,7 +99,7 @@ int spi_nor_otp_write_secr(struct spi_nor *nor, loff_t addr, size_t len,/* * We only support a write to one single page. For now all winbond -* flashes only have one page per OTP region. +* flashes only have one page per security register. */ ret =spi_nor_write_enable (nor);if (ret)
Signature · 1 line
2.20.1
PATCH v6 3/4 mtd: spi-nor: otp: return -EROFS if region is read-only
In reply to Michael WalleSPI NOR flashes will just ignore program commands if the OTP region is locked. Thus, a user might not notice that the intended write didn't end up in the flash. Return -EROFS to the user in this case. From what I can tell, chips/cfi_cmdset_0001.c also return this error code.
One could optimize spi_nor_mtd_otp_range_is_locked() to read the status register only once and not for every OTP region, but for that we would need some more invasive changes. Given that this is one-time-programmable memory and the normal access mode is reading, we just live with the small overhead.
By moving the code around a bit, we can just check the length before calling spi_nor_mtd_otp_range_is_locked() and avoid an underflow there if a len is 0. This way we don't need to take the lock either. We also skip the "*retlen = 0" assignment, mtdcore already takes care of that for us.
--- drivers/mtd/spi-nor/otp.c | 41 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-)
drivers/mtd/spi-nor/otp.c +38 -3
@@ -249,6 +249,29 @@ static int spi_nor_mtd_otp_info(struct mtd_info *mtd, size_t len,return ret; } +static int spi_nor_mtd_otp_range_is_locked (struct spi_nor *nor,loff_t ofs, +size_t len) +{ +const struct spi_nor_otp_ops *ops = nor->params->otp.ops; +unsigned int region; +int locked; + +/* +* If any of the affected OTP regions are locked the entire range is +* considered locked. +*/ +for (region =spi_nor_otp_offset_to_region (nor, ofs); + region <=spi_nor_otp_offset_to_region (nor, ofs + len -1 ); + region++) { + locked = ops->is_locked (nor, region); +/* take the branch it is locked or in case of an error */ +if (locked) +return locked; + } + +return 0 ; +} +static int spi_nor_mtd_otp_read_write (struct mtd_info *mtd,loff_t ofs,size_t total_len,size_t *retlen,const u8 *buf,bool is_write)
@@ -264,14 +287,26 @@ static int spi_nor_mtd_otp_read_write(struct mtd_info *mtd, loff_t ofs,if (ofs <0 || ofs >=spi_nor_otp_size (nor))return 0 ; +/* don't access beyond the end */ + total_len =min_t (size_t, total_len,spi_nor_otp_size (nor) - ofs); + +if (!total_len) +return 0 ; + ret =spi_nor_lock_and_prep (nor);if (ret)return ret; -/* don't access beyond the end */ - total_len =min_t (size_t, total_len,spi_nor_otp_size (nor) - ofs); +if (is_write) { + ret =spi_nor_mtd_otp_range_is_locked (nor, ofs, total_len); +if (ret <0 ) { + goto out; + }else if (ret) { + ret = -EROFS ; + goto out; + } + } - *retlen =0 ;while (total_len) {/* * The OTP regions are mapped into a contiguous area starting
Signature · 1 line
2.20.1
PATCH v6 4/4 mtd: spi-nor: otp: implement erase for Winbond and similar flashes
In reply to Michael WalleWinbond flashes with OTP support provide a command to erase the OTP data. This might come in handy during development.
This was tested with a Winbond W25Q32JW on a LS1028A SoC with the NXP FSPI controller.
--- drivers/mtd/spi-nor/core.c | 2 +- drivers/mtd/spi-nor/core.h | 4 ++ drivers/mtd/spi-nor/otp.c | 86 +++++++++++++++++++++++++++++++++++ drivers/mtd/spi-nor/winbond.c | 1 + 4 files changed, 92 insertions(+), 1 deletion(-)
drivers/mtd/spi-nor/core.c +1 -1
@@ -1318,7 +1318,7 @@ static u32 spi_nor_convert_addr(struct spi_nor *nor, loff_t addr)/* * Initiate the erasure of a single sector */ -static int spi_nor_erase_sector (struct spi_nor *nor,u32 addr) +int spi_nor_erase_sector (struct spi_nor *nor,u32 addr) {int i;
drivers/mtd/spi-nor/core.h +4 -0
@@ -207,6 +207,7 @@ struct spi_nor_otp_organization {* @read: read from the SPI NOR OTP area. * @write: write to the SPI NOR OTP area. * @lock: lock an OTP region. +* @erase: erase an OTP region. * @is_locked: check if an OTP region of the SPI NOR is locked. */ struct spi_nor_otp_ops {
@@ -214,6 +215,7 @@ struct spi_nor_otp_ops {int (*write)(struct spi_nor *nor,loff_t addr,size_t len,const u8 *buf);int (*lock)(struct spi_nor *nor,unsigned int region); +int (*erase)(struct spi_nor *nor,loff_t addr);int (*is_locked)(struct spi_nor *nor,unsigned int region); };
@@ -503,10 +505,12 @@ ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len, u8 *buf);ssize_t spi_nor_write_data (struct spi_nor *nor,loff_t to,size_t len,const u8 *buf); +int spi_nor_erase_sector (struct spi_nor *nor,u32 addr);int spi_nor_otp_read_secr (struct spi_nor *nor,loff_t addr,size_t len,u8 *buf);int spi_nor_otp_write_secr (struct spi_nor *nor,loff_t addr,size_t len,const u8 *buf); +int spi_nor_otp_erase_secr (struct spi_nor *nor,loff_t addr);int spi_nor_otp_lock_sr2 (struct spi_nor *nor,unsigned int region);int spi_nor_otp_is_locked_sr2 (struct spi_nor *nor,unsigned int region);
drivers/mtd/spi-nor/otp.c +86 -0
102 lines not on this page · raw message
drivers/mtd/spi-nor/winbond.c +1 -0
@@ -139,6 +139,7 @@ static int winbond_set_4byte_addr_mode(struct spi_nor *nor, bool enable)static const struct spi_nor_otp_ops winbond_otp_ops = { .read = spi_nor_otp_read_secr, .write = spi_nor_otp_write_secr, + .erase = spi_nor_otp_erase_secr, .lock = spi_nor_otp_lock_sr2, .is_locked = spi_nor_otp_is_locked_sr2, };
Signature · 1 line
2.20.1
Quoted 8 lines · Michael Walle
This series is the follow up on the single patch
mtd: spi-nor: implement OTP erase for Winbond and similar flashesPratyush Yadav discovered a likely problem with bigger flashes, the address to access the security registers is either 3 or 4 byte (at least for winbond flashes).
[...]
Applied to spi-nor/next, thanks!
[1/4] mtd: spi-nor: otp: fix access to security registers in 4 byte mode
https://git.kernel.org/mtd/c/b97b1a7698 [2/4] mtd: spi-nor: otp: use more consistent wording https://git.kernel.org/mtd/c/d5b813e484 [3/4] mtd: spi-nor: otp: return -EROFS if region is read-only https://git.kernel.org/mtd/c/388161ca45 [4/4] mtd: spi-nor: otp: implement erase for Winbond and similar flashes https://git.kernel.org/mtd/c/c6ec3e1e3a
--
Regards
Vignesh