mtd: spi-nor: Quad Enable and (un)lock methods
Subsystems (2)
v5 cover mtd: spi-nor: Quad Enable and (un)lock methods
From: Tudor Ambarus <[email protected]>
Tested on w25q128jvq.
Fixed the clearing of QE bit on (un)lock() operations. Reworked the Quad Enable methods and the disabling of the block write protection at power-up.
v5:
- Rename all Quad Enable methods in one patch
- Extend the Read Back test to both SR1 and SR2 in one patch
- Reorder patches, so that the fixes come one after another
- Collect R-b tags.
v4:
- Use dev_dbg insted of dev_err for low level info
- replace "&nor->bouncebuf[0]" with "nor->bouncebuf" and "&sr_cr[0]" with "sr_cr". Update across all patches.
v3: split patches, update retlen handling in sst_write.
v2:
- Introduce spi_nor_write_16bit_cr_and_check() as per Vignesh's suggestion. The Configuration Register contains bits that can be updated in future: FREEZE, CMP. Provide a generic method that allows updating all bits of the Configuration Register.
- Fix SNOR_F_NO_READ_CR case in
"mtd: spi-nor: Rework the disabling of block write protection". When the flash doesn't support the CR Read command, we make an assumption about the value of the QE bit. In spi_nor_init(), call spi_nor_quad_enable() first, then spi_nor_unlock_all(), so that at the spi_nor_unlock_all() time we can be sure the QE bit has value one, because of the previous call to spi_nor_quad_enable().
- Fix if statement in spi_nor_write_sr_and_check():
if (nor->flags & SNOR_F_HAS_16BIT_SR)
- Fix documentation warnings.
- New patch: "mtd: spi-nor: Check all the bits written, not just the BP ones".
- Drop Global Unlock patches, will send them in a different patch set.
The patch set can be tested using mtd-utils:
1/ do a read-erase-write-read-back test immediately after boot, to check the spi_nor_unlock_all() method. The focus is on the erase/write methods, we want to see if the flash is unlocked at power-up.
mtd_debug read /dev/mtd-yours offset size read-file hexdump read-file
mtd_debug erase /dev/mtd-yours offset size
dd if=/dev/urandom of=write-file bs=please-choose count=please-choose mtd_debug write /dev/mtd-yours offset write-file-size write-file mtd_debug read /dev/mtd-yours offset write-file-size read-file sha1sum read-file write-file
2/ lock flash then try to erase/write it, to see if the lock works
flash_lock /dev/mtd-yours offset block-count
Do the read-erase-write-read-back test from 1/. The contents of flash should not change in the erase and write steps.
3/ unlock flash and do the read-erase-write-read-back from 1/. The value of the
QEE should not change and you should be able to erase and write the flash. Test 1/ should be successful.
Tudor Ambarus (6):
mtd: spi-nor: Fix clearing of QE bit on lock()/unlock()
mtd: spi-nor: Rework the disabling of block write protection
mtd: spi-nor: Extend the SR Read Back test
mtd: spi-nor: Rename CR_QUAD_EN_SPAN to SR2_QUAD_EN_BIT1
mtd: spi-nor: Merge spansion Quad Enable methods
mtd: spi-nor: Rename Quad Enable methods
drivers/mtd/spi-nor/spi-nor.c | 438 ++++++++++++++++++++++++------------------ include/linux/mtd/spi-nor.h | 12 +- 2 files changed, 254 insertions(+), 196 deletions(-)
Signature · 1 line
2.9.5
PATCH v5 1/6 mtd: spi-nor: Fix clearing of QE bit on lock()/unlock()
From: Tudor Ambarus <[email protected]>
Make sure that when doing a lock() or an unlock() operation we don't clear the QE bit from Status Register 2.
JESD216 revB or later offers information about the *default* Status Register commands to use (see BFPT DWORDS[15], bits 22:20). In this standard, Status Register 1 refers to the first data byte transferred on a Read Status (05h) or Write Status (01h) command. Status register 2 refers to the byte read using instruction 35h. Status register 2 is the second byte transferred in a Write Status (01h) command.
Industry naming and definitions of these Status Registers may differ. The definitions are described in JESD216B, BFPT DWORDS[15], bits 22:20. There are cases in which writing only one byte to the Status Register 1 has the side-effect of clearing Status Register 2 and implicitly the Quad Enable bit. This side-effect is hit just by the BFPT_DWORD15_QER_SR2_BIT1_BUGGY and BFPT_DWORD15_QER_SR2_BIT1 cases.
--- drivers/mtd/spi-nor/spi-nor.c | 120 ++++++++++++++++++++++++++++++++++++++++-- include/linux/mtd/spi-nor.h | 3 ++ 2 files changed, 118 insertions(+), 5 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +115 -5
@@ -959,12 +959,19 @@ static int spi_nor_write_sr(struct spi_nor *nor, const u8 *sr, size_t len)return spi_nor_wait_till_ready (nor); } -/* Write status register and ensure bits in mask match written values */ -static int spi_nor_write_sr_and_check (struct spi_nor *nor,u8 status_new) +/** +* spi_nor_write_sr1_and_check() - Write one byte to the Status Register 1 and +* ensure that the byte written match the received value. +* @nor: pointer to a 'struct spi_nor'. +* @sr1: byte value to be written to the Status Register. +* +* Return: 0 on success, -errno otherwise. +*/ +static int spi_nor_write_sr1_and_check (struct spi_nor *nor,u8 sr1) {int ret; - nor->bouncebuf[0 ] = status_new; + nor->bouncebuf[0 ] = sr1; ret =spi_nor_write_sr (nor, nor->bouncebuf,1 );if (ret)
@@ -974,8 +981,73 @@ static int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 status_new)if (ret)return ret; -if (nor->bouncebuf[0 ] != status_new) { -dev_dbg (nor->dev,"SR: read back test failed\n" ); +if (nor->bouncebuf[0 ] != sr1) { +dev_dbg (nor->dev,"SR1: read back test failed\n" ); +return -EIO ; + } + +return 0 ; +} + +/** +* spi_nor_write_16bit_sr_and_check() - Write the Status Register 1 and the +* Status Register 2 in one shot. Ensure that the byte written in the Status +* Register 1 match the received value, and that the 16-bit Write did not +* affect what was already in the Status Register 2. +* @nor: pointer to a 'struct spi_nor'. +* @sr1: byte value to be written to the Status Register 1. +* +* Return: 0 on success, -errno otherwise. +*/ +static int spi_nor_write_16bit_sr_and_check (struct spi_nor *nor,u8 sr1) +{ +int ret; +u8 *sr_cr = nor->bouncebuf; +u8 cr_written; + +/* Make sure we don't overwrite the contents of Status Register 2. */ +if (!(nor->flags &SNOR_F_NO_READ_CR )) { + ret =spi_nor_read_cr (nor, &sr_cr[1 ]); +if (ret) +return ret; + }else if (nor->params.quad_enable) { +/* +* If the Status Register 2 Read command (35h) is not +* supported, we should at least be sure we don't +* change the value of the SR2 Quad Enable bit. +* +* We can safely assume that when the Quad Enable method is +* set, the value of the QE bit is one, as a consequence of the +* nor->params.quad_enable() call. +* +* We can safely assume that the Quad Enable bit is present in +* the Status Register 2 at BIT(1). According to the JESD216 +* revB standard, BFPT DWORDS[15], bits 22:20, the 16-bit +* Write Status (01h) command is available just for the cases +* in which the QE bit is described in SR2 at BIT(1). +*/ + sr_cr[1 ] =CR_QUAD_EN_SPAN ; + }else { + sr_cr[1 ] =0 ; + } + + sr_cr[0 ] = sr1; + + ret =spi_nor_write_sr (nor, sr_cr,2 ); +if (ret) +return ret; + +if (nor->flags &SNOR_F_NO_READ_CR ) +return 0 ; + + cr_written = sr_cr[1 ]; + + ret =spi_nor_read_cr (nor, &sr_cr[1 ]); +if (ret) +return ret; + +if (cr_written != sr_cr[1 ]) { +dev_dbg (nor->dev,"CR: read back test failed\n" );return -EIO ; }
@@ -983,6 +1055,23 @@ static int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 status_new) }/** +* spi_nor_write_sr_and_check() - Write the Status Register 1 and ensure that +* the byte written match the received value without affecting other bits in the +* Status Register 1 and 2. +* @nor: pointer to a 'struct spi_nor'. +* @sr1: byte value to be written to the Status Register. +* +* Return: 0 on success, -errno otherwise. +*/ +static int spi_nor_write_sr_and_check (struct spi_nor *nor,u8 sr1) +{ +if (nor->flags &SNOR_F_HAS_16BIT_SR ) +return spi_nor_write_16bit_sr_and_check (nor, sr1); + +return spi_nor_write_sr1_and_check (nor, sr1); +} + +/** * spi_nor_write_sr2() - Write the Status Register 2 using the * SPINOR_OP_WRSR2 (3eh) command. * @nor: pointer to 'struct spi_nor'.
@@ -3634,19 +3723,38 @@ static int spi_nor_parse_bfpt(struct spi_nor *nor,break ;case BFPT_DWORD15_QER_SR2_BIT1_BUGGY : +/* +* Writing only one byte to the Status Register has the +* side-effect of clearing Status Register 2. +*/ case BFPT_DWORD15_QER_SR2_BIT1_NO_RD : +/* +* Read Configuration Register (35h) instruction is not +* supported. +*/ + nor->flags |=SNOR_F_HAS_16BIT_SR |SNOR_F_NO_READ_CR ; params->quad_enable = spansion_no_read_cr_quad_enable;break ;case BFPT_DWORD15_QER_SR1_BIT6 : + nor->flags &= ~SNOR_F_HAS_16BIT_SR ; params->quad_enable = macronix_quad_enable;break ;case BFPT_DWORD15_QER_SR2_BIT7 : + nor->flags &= ~SNOR_F_HAS_16BIT_SR ; params->quad_enable = sr2_bit7_quad_enable;break ;case BFPT_DWORD15_QER_SR2_BIT1 : +/* +* JESD216 rev B or later does not specify if writing only one +* byte to the Status Register clears or not the Status +* Register 2, so let's be cautious and keep the default +* assumption of a 16-bit Write Status (01h) command. +*/ + nor->flags |=SNOR_F_HAS_16BIT_SR ; + params->quad_enable = spansion_read_cr_quad_enable;break ;
@@ -4613,6 +4721,8 @@ static void spi_nor_info_init_params(struct spi_nor *nor) params->quad_enable = spansion_read_cr_quad_enable; params->set_4byte = spansion_set_4byte; params->setup = spi_nor_default_setup; +/* Default to 16-bit Write Status (01h) Command */ + nor->flags |=SNOR_F_HAS_16BIT_SR ;/* Set SPI NOR sizes. */ params->size = (u64 )info->sector_size * info->n_sectors;
include/linux/mtd/spi-nor.h +3 -0
@@ -243,6 +243,9 @@ enum spi_nor_option_flags {SNOR_F_4B_OPCODES =BIT (6 ),SNOR_F_HAS_4BAIT =BIT (7 ),SNOR_F_HAS_LOCK =BIT (8 ), +SNOR_F_HAS_16BIT_SR =BIT (9 ), + SNOR_F_NO_READ_CR = BIT(10), + };/**
Signature · 1 line
2.9.5
PATCH v5 2/6 mtd: spi-nor: Rework the disabling of block write protection
In reply to [email protected]From: Tudor Ambarus <[email protected]>
spi_nor_unlock() unlocks blocks of memory or the entire flash memory array, if requested. clear_sr_bp() unlocks the entire flash memory array at boot time. This calls for some unification, clear_sr_bp() is just an optimization for the case when the unlock request covers the entire flash size.
Get rid of clear_sr_bp() and introduce spi_nor_unlock_all(), which is just a call to spi_nor_unlock() for the entire flash memory array. This fixes a bug that was present in spi_nor_spansion_clear_sr_bp(). When the QE bit was zero, we used the Write Status (01h) command with one data byte, which might cleared the Status Register 2. We now always use the Write Status (01h) command with two data bytes when SNOR_F_HAS_16BIT_SR is set, to avoid clearing the Status Register 2.
The SNOR_F_NO_READ_CR case is treated as well. When the flash doesn't support the CR Read command, we make an assumption about the value of the QE bit. In spi_nor_init(), call spi_nor_quad_enable() first, then spi_nor_unlock_all(), so that at the spi_nor_unlock_all() time we can be sure the QE bit has value one, because of the previous call to spi_nor_quad_enable().
Get rid of the MFR handling and implement specific manufacturer default_init() fixup hooks.
Note that this changes a bit the logic for the SNOR_MFR_ATMEL, SNOR_MFR_INTEL and SNOR_MFR_SST cases. Before this patch, the Atmel, Intel and SST chips did not set the locking ops, but unlocked the entire flash at boot time, while now they are setting the locking ops to stm_locking_ops. This should work, since the the disable of the block protection at the boot time used the same Status Register bits to unlock the flash, as in the stm_locking_ops case.
--- drivers/mtd/spi-nor/spi-nor.c | 140 +++++++++++++++--------------------------- include/linux/mtd/spi-nor.h | 3 - 2 files changed, 50 insertions(+), 93 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +50 -90
@@ -2185,74 +2185,6 @@ static int sr2_bit7_quad_enable(struct spi_nor *nor)return 0 ; } -/** -* spi_nor_clear_sr_bp() - clear the Status Register Block Protection bits. -* @nor: pointer to a 'struct spi_nor' -* -* Read-modify-write function that clears the Block Protection bits from the -* Status Register without affecting other bits. -* -* Return: 0 on success, -errno otherwise. -*/ -static int spi_nor_clear_sr_bp (struct spi_nor *nor) -{ -int ret; -u8 mask =SR_BP2 |SR_BP1 |SR_BP0 ; - - ret =spi_nor_read_sr (nor, nor->bouncebuf); -if (ret) -return ret; - - nor->bouncebuf[0 ] &= ~mask; - -return spi_nor_write_sr (nor, nor->bouncebuf,1 ); -} - -/** -* spi_nor_spansion_clear_sr_bp() - clear the Status Register Block Protection -* bits on spansion flashes. -* @nor: pointer to a 'struct spi_nor' -* -* Read-modify-write function that clears the Block Protection bits from the -* Status Register without affecting other bits. The function is tightly -* coupled with the spansion_read_cr_quad_enable() function. Both assume that -* the Write Register with 16 bits, together with the Read Configuration -* Register (35h) instructions are supported. -* -* Return: 0 on success, -errno otherwise. -*/ -static int spi_nor_spansion_clear_sr_bp (struct spi_nor *nor) -{ -int ret; -u8 mask =SR_BP2 |SR_BP1 |SR_BP0 ; -u8 *sr_cr = nor->bouncebuf; - -/* Check current Quad Enable bit value. */ - ret =spi_nor_read_cr (nor, &sr_cr[1 ]); -if (ret) -return ret; - -/* -* When the configuration register Quad Enable bit is one, only the -* Write Status (01h) command with two data bytes may be used. -*/ -if (sr_cr[1 ] &CR_QUAD_EN_SPAN ) { - ret =spi_nor_read_sr (nor, sr_cr); -if (ret) -return ret; - - sr_cr[0 ] &= ~mask; - -return spi_nor_write_sr (nor, sr_cr,2 ); - } - -/* -* If the Quad Enable bit is zero, use the Write Status (01h) command -* with one data byte. -*/ -return spi_nor_clear_sr_bp (nor); -} -/* Used when the "_ext_id" is two bytes at most */ #define INFO (_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \ .id = { \
@@ -4634,12 +4566,27 @@ static int spi_nor_setup(struct spi_nor *nor,return nor->params.setup (nor, hwcaps); } +static void atmel_set_default_init (struct spi_nor *nor) +{ + nor->flags |=SNOR_F_HAS_LOCK ; +} + +static void intel_set_default_init (struct spi_nor *nor) +{ + nor->flags |=SNOR_F_HAS_LOCK ; +} +static void macronix_set_default_init (struct spi_nor *nor) { nor->params.quad_enable = macronix_quad_enable; nor->params.set_4byte = macronix_set_4byte; } +static void sst_set_default_init (struct spi_nor *nor) +{ + nor->flags |=SNOR_F_HAS_LOCK ; +} +static void st_micron_set_default_init (struct spi_nor *nor) { nor->flags |=SNOR_F_HAS_LOCK ;
@@ -4661,6 +4608,14 @@ static void spi_nor_manufacturer_init_params(struct spi_nor *nor) {/* Init flash parameters based on MFR */ switch (JEDEC_MFR (nor->info)) { +case SNOR_MFR_ATMEL : +atmel_set_default_init (nor); +break ; + +case SNOR_MFR_INTEL : +intel_set_default_init (nor); +break ; +case SNOR_MFR_MACRONIX :macronix_set_default_init (nor);break ;
@@ -4670,6 +4625,10 @@ static void spi_nor_manufacturer_init_params(struct spi_nor *nor)st_micron_set_default_init (nor);break ; +case SNOR_MFR_SST : +sst_set_default_init (nor); +break ; +case SNOR_MFR_WINBOND :winbond_set_default_init (nor);break ;
@@ -4930,21 +4889,26 @@ static int spi_nor_quad_enable(struct spi_nor *nor)return nor->params.quad_enable (nor); } -static int spi_nor_init (struct spi_nor *nor) +/** +* spi_nor_unlock_all() - Unlocks the entire flash memory array. +* @nor: pointer to a 'struct spi_nor'. +* +* Some SPI NOR flashes are write protected by default after a power-on reset +* cycle, in order to avoid inadvertent writes during power-up. Backward +* compatibility imposes to unlock the entire flash memory array at power-up +* by default. +*/ +static int spi_nor_unlock_all (struct spi_nor *nor) { -int err; +if (nor->flags &SNOR_F_HAS_LOCK ) +return spi_nor_unlock (&nor->mtd,0 , nor->params.size); -if (nor->clear_sr_bp) { -if (nor->params.quad_enable == spansion_read_cr_quad_enable) - nor->clear_sr_bp = spi_nor_spansion_clear_sr_bp; +return 0 ; +} - err = nor->clear_sr_bp (nor); -if (err) { -dev_dbg (nor->dev, -"fail to clear block protection bits\n" ); -return err; - } - } +static int spi_nor_init (struct spi_nor *nor) +{ +int err; err =spi_nor_quad_enable (nor);if (err) {
@@ -4952,6 +4916,12 @@ static int spi_nor_init(struct spi_nor *nor)return err; } + err =spi_nor_unlock_all (nor); +if (err) { +dev_dbg (nor->dev,"Failed to unlock the entire flash memory array\n" ); +return err; + } +if (nor->addr_width ==4 && !(nor->flags &SNOR_F_4B_OPCODES )) {/* * If the RESET# pin isn't hooked up properly, or the system
@@ -5134,16 +5104,6 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,if (info->flags &SPI_NOR_HAS_LOCK ) nor->flags |=SNOR_F_HAS_LOCK ; -/* -* Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up -* with the software protection bits set. -*/ -if (JEDEC_MFR (nor->info) ==SNOR_MFR_ATMEL || -JEDEC_MFR (nor->info) ==SNOR_MFR_INTEL || -JEDEC_MFR (nor->info) ==SNOR_MFR_SST || - nor->info->flags &SPI_NOR_HAS_LOCK ) - nor->clear_sr_bp = spi_nor_clear_sr_bp; -/* Init flash parameters based on flash_info struct and SFDP */ spi_nor_init_params (nor);
include/linux/mtd/spi-nor.h +0 -3
@@ -581,8 +581,6 @@ struct flash_info;* @write_proto: the SPI protocol for write operations * @reg_proto the SPI protocol for read_reg/write_reg/erase operations * @controller_ops: SPI NOR controller driver specific operations. -* @clear_sr_bp: [FLASH-SPECIFIC] clears the Block Protection Bits from -* the SPI NOR Status Register. * @params: [FLASH-SPECIFIC] SPI-NOR flash parameters and settings. * The structure includes legacy flash parameters and * settings that can be overwritten by the spi_nor_fixups
@@ -611,7 +609,6 @@ struct spi_nor {const struct spi_nor_controller_ops *controller_ops; -int (*clear_sr_bp)(struct spi_nor *nor);struct spi_nor_flash_parameter params;void *priv;
Signature · 1 line
2.9.5
Re: [PATCH v5 2/6] mtd: spi-nor: Rework the disabling of block write protection
Quoted 23 lines · On 07-Nov-19 2:11 PM, [email protected] wrote:
From: Tudor Ambarus <[email protected]>
spi_nor_unlock()unlocks blocks of memory or the entire flash memory array, if requested.clear_sr_bp()unlocks the entire flash memory array at boot time. This calls for some unification,clear_sr_bp()is just an optimization for the case when the unlock request covers the entire flash size.Get rid of
clear_sr_bp()and introducespi_nor_unlock_all(), which is just a call tospi_nor_unlock()for the entire flash memory array. This fixes a bug that was present inspi_nor_spansion_clear_sr_bp(). When the QE bit was zero, we used the Write Status (01h) command with one data byte, which might cleared the Status Register 2. We now always use the Write Status (01h) command with two data bytes whenSNOR_F_HAS_16BIT_SRis set, to avoid clearing the Status Register 2.The
SNOR_F_NO_READ_CRcase is treated as well. When the flash doesn't support the CR Read command, we make an assumption about the value of the QE bit. Inspi_nor_init(), callspi_nor_quad_enable()first, thenspi_nor_unlock_all(), so that at thespi_nor_unlock_all()time we can be sure the QE bit has value one, because of the previous call tospi_nor_quad_enable().
Quoted 8 lines
Get rid of the MFR handling and implement specific manufacturer
default_init()fixup hooks.Note that this changes a bit the logic for the
SNOR_MFR_ATMEL,SNOR_MFR_INTELandSNOR_MFR_SSTcases. Before this patch, the Atmel, Intel and SST chips did not set the locking ops, but unlocked the entire flash at boot time, while now they are setting the locking ops tostm_locking_ops. This should work, since the the disable of the block
Nit: s/the the/the
Quoted 6 lines
protection at the boot time used the same Status Register bits to unlock the flash, as in the
stm_locking_opscase.Suggested-by Boris Brezillon <[email protected]>Signed-off-by Tudor Ambarus <[email protected]>---
Regards
Vignesh
Quoted 228 lines
drivers/mtd/spi-nor/spi-nor.c | 140 +++++++++++++++--------------------------- include/linux/mtd/spi-nor.h | 3 - 2 files changed, 50 insertions(+), 93 deletions(-)drivers/mtd/spi-nor/spi-nor.c +50
-90@@ -2185,74 +2185,6 @@ static int sr2_bit7_quad_enable(struct spi_nor *nor)return 0 ; } -/** -* spi_nor_clear_sr_bp() - clear the Status Register Block Protection bits. -* @nor: pointer to a 'struct spi_nor' -* -* Read-modify-write function that clears the Block Protection bits from the -* Status Register without affecting other bits. -* -* Return: 0 on success, -errno otherwise. -*/ -static int spi_nor_clear_sr_bp (struct spi_nor *nor) -{ -int ret; -u8 mask =SR_BP2 |SR_BP1 |SR_BP0 ; - - ret =spi_nor_read_sr (nor, nor->bouncebuf); -if (ret) -return ret; - - nor->bouncebuf[0 ] &= ~mask; - -return spi_nor_write_sr (nor, nor->bouncebuf,1 ); -} - -/** -* spi_nor_spansion_clear_sr_bp() - clear the Status Register Block Protection -* bits on spansion flashes. -* @nor: pointer to a 'struct spi_nor' -* -* Read-modify-write function that clears the Block Protection bits from the -* Status Register without affecting other bits. The function is tightly -* coupled with the spansion_read_cr_quad_enable() function. Both assume that -* the Write Register with 16 bits, together with the Read Configuration -* Register (35h) instructions are supported. -* -* Return: 0 on success, -errno otherwise. -*/ -static int spi_nor_spansion_clear_sr_bp (struct spi_nor *nor) -{ -int ret; -u8 mask =SR_BP2 |SR_BP1 |SR_BP0 ; -u8 *sr_cr = nor->bouncebuf; - -/* Check current Quad Enable bit value. */ - ret =spi_nor_read_cr (nor, &sr_cr[1 ]); -if (ret) -return ret; - -/* -* When the configuration register Quad Enable bit is one, only the -* Write Status (01h) command with two data bytes may be used. -*/ -if (sr_cr[1 ] &CR_QUAD_EN_SPAN ) { - ret =spi_nor_read_sr (nor, sr_cr); -if (ret) -return ret; - - sr_cr[0 ] &= ~mask; - -return spi_nor_write_sr (nor, sr_cr,2 ); - } - -/* -* If the Quad Enable bit is zero, use the Write Status (01h) command -* with one data byte. -*/ -return spi_nor_clear_sr_bp (nor); -} -/* Used when the "_ext_id" is two bytes at most */ #define INFO (_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \ .id = { \@@ -4634,12 +4566,27 @@ static int spi_nor_setup(struct spi_nor *nor,return nor->params.setup (nor, hwcaps); } +static void atmel_set_default_init (struct spi_nor *nor) +{ + nor->flags |=SNOR_F_HAS_LOCK ; +} + +static void intel_set_default_init (struct spi_nor *nor) +{ + nor->flags |=SNOR_F_HAS_LOCK ; +} +static void macronix_set_default_init (struct spi_nor *nor) { nor->params.quad_enable = macronix_quad_enable; nor->params.set_4byte = macronix_set_4byte; } +static void sst_set_default_init (struct spi_nor *nor) +{ + nor->flags |=SNOR_F_HAS_LOCK ; +} +static void st_micron_set_default_init (struct spi_nor *nor) { nor->flags |=SNOR_F_HAS_LOCK ;@@ -4661,6 +4608,14 @@ static void spi_nor_manufacturer_init_params(struct spi_nor *nor) {/* Init flash parameters based on MFR */ switch (JEDEC_MFR (nor->info)) { +case SNOR_MFR_ATMEL : +atmel_set_default_init (nor); +break ; + +case SNOR_MFR_INTEL : +intel_set_default_init (nor); +break ; +case SNOR_MFR_MACRONIX :macronix_set_default_init (nor);break ;@@ -4670,6 +4625,10 @@ static void spi_nor_manufacturer_init_params(struct spi_nor *nor)st_micron_set_default_init (nor);break ; +case SNOR_MFR_SST : +sst_set_default_init (nor); +break ; +case SNOR_MFR_WINBOND :winbond_set_default_init (nor);break ;@@ -4930,21 +4889,26 @@ static int spi_nor_quad_enable(struct spi_nor *nor)return nor->params.quad_enable (nor); } -static int spi_nor_init (struct spi_nor *nor) +/** +* spi_nor_unlock_all() - Unlocks the entire flash memory array. +* @nor: pointer to a 'struct spi_nor'. +* +* Some SPI NOR flashes are write protected by default after a power-on reset +* cycle, in order to avoid inadvertent writes during power-up. Backward +* compatibility imposes to unlock the entire flash memory array at power-up +* by default. +*/ +static int spi_nor_unlock_all (struct spi_nor *nor) { -int err; +if (nor->flags &SNOR_F_HAS_LOCK ) +return spi_nor_unlock (&nor->mtd,0 , nor->params.size); -if (nor->clear_sr_bp) { -if (nor->params.quad_enable == spansion_read_cr_quad_enable) - nor->clear_sr_bp = spi_nor_spansion_clear_sr_bp; +return 0 ; +} - err = nor->clear_sr_bp (nor); -if (err) { -dev_dbg (nor->dev, -"fail to clear block protection bits\n" ); -return err; - } - } +static int spi_nor_init (struct spi_nor *nor) +{ +int err; err =spi_nor_quad_enable (nor);if (err) {@@ -4952,6 +4916,12 @@ static int spi_nor_init(struct spi_nor *nor)return err; } + err =spi_nor_unlock_all (nor); +if (err) { +dev_dbg (nor->dev,"Failed to unlock the entire flash memory array\n" ); +return err; + } +if (nor->addr_width ==4 && !(nor->flags &SNOR_F_4B_OPCODES )) {/* * If the RESET# pin isn't hooked up properly, or the system @@ -5134,16 +5104,6 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,if (info->flags &SPI_NOR_HAS_LOCK ) nor->flags |=SNOR_F_HAS_LOCK ; -/* -* Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up -* with the software protection bits set. -*/ -if (JEDEC_MFR (nor->info) ==SNOR_MFR_ATMEL || -JEDEC_MFR (nor->info) ==SNOR_MFR_INTEL || -JEDEC_MFR (nor->info) ==SNOR_MFR_SST || - nor->info->flags &SPI_NOR_HAS_LOCK ) - nor->clear_sr_bp = spi_nor_clear_sr_bp; -/* Init flash parameters based on flash_info struct and SFDP */ spi_nor_init_params (nor);include/linux/mtd/spi-nor.h +0
-3@@ -581,8 +581,6 @@ struct flash_info;* @write_proto: the SPI protocol for write operations * @reg_proto the SPI protocol for read_reg/write_reg/erase operations * @controller_ops: SPI NOR controller driver specific operations. -* @clear_sr_bp: [FLASH-SPECIFIC] clears the Block Protection Bits from -* the SPI NOR Status Register. * @params: [FLASH-SPECIFIC] SPI-NOR flash parameters and settings. * The structure includes legacy flash parameters and * settings that can be overwritten by the spi_nor_fixups @@ -611,7 +609,6 @@ struct spi_nor {const struct spi_nor_controller_ops *controller_ops; -int (*clear_sr_bp)(struct spi_nor *nor);struct spi_nor_flash_parameter params;void *priv;
PATCH v5 3/6 mtd: spi-nor: Extend the SR Read Back test
In reply to [email protected]From: Tudor Ambarus <[email protected]>
Test that all the bits from Status Register 1 and Status Register 2 were written correctly.
--- drivers/mtd/spi-nor/spi-nor.c | 58 +++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 19 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +39 -19
@@ -2047,20 +2047,7 @@ static int macronix_quad_enable(struct spi_nor *nor) nor->bouncebuf[0 ] |=SR_QUAD_EN_MX ; - ret =spi_nor_write_sr (nor, nor->bouncebuf,1 ); -if (ret) -return ret; - - ret =spi_nor_read_sr (nor, nor->bouncebuf); -if (ret) -return ret; - -if (!(nor->bouncebuf[0 ] &SR_QUAD_EN_MX )) { -dev_dbg (nor->dev,"Macronix Quad bit not set\n" ); -return -EIO ; - } - -return 0 ; +return spi_nor_write_sr1_and_check (nor, nor->bouncebuf[0 ]); }/**
@@ -2080,6 +2067,7 @@ static int spansion_no_read_cr_quad_enable(struct spi_nor *nor) {u8 *sr_cr = nor->bouncebuf;int ret; +u8 sr_written;/* Keep the current value of the Status Register. */ ret =spi_nor_read_sr (nor, sr_cr);
@@ -2088,7 +2076,22 @@ static int spansion_no_read_cr_quad_enable(struct spi_nor *nor) sr_cr[1 ] =CR_QUAD_EN_SPAN ; -return spi_nor_write_sr (nor, sr_cr,2 ); + ret =spi_nor_write_sr (nor, sr_cr,2 ); +if (ret) +return ret; + + sr_written = sr_cr[0 ]; + + ret =spi_nor_read_sr (nor, sr_cr); +if (ret) +return ret; + +if (sr_cr[0 ] != sr_written) { +dev_err (nor->dev,"SR: Read back test failed\n" ); +return -EIO ; + } + +return 0 ; }/**
@@ -2108,6 +2111,7 @@ static int spansion_read_cr_quad_enable(struct spi_nor *nor) {u8 *sr_cr = nor->bouncebuf;int ret; +u8 sr_written;/* Check current Quad Enable bit value. */ ret =spi_nor_read_cr (nor, &sr_cr[1 ]);
@@ -2128,13 +2132,26 @@ static int spansion_read_cr_quad_enable(struct spi_nor *nor)if (ret)return ret; + sr_written = sr_cr[0 ]; + + ret =spi_nor_read_sr (nor, sr_cr); +if (ret) +return ret; + +if (sr_written != sr_cr[0 ]) { +dev_err (nor->dev,"SR: Read back test failed\n" ); +return -EIO ; + } + + sr_written = sr_cr[1 ]; +/* Read back and check it. */ ret =spi_nor_read_cr (nor, &sr_cr[1 ]);if (ret)return ret; -if (!(sr_cr[1 ] &CR_QUAD_EN_SPAN )) { -dev_dbg (nor->dev,"Spansion Quad bit not set\n" ); +if (sr_cr[1 ] != sr_written) { +dev_dbg (nor->dev,"CR: Read back test failed\n" );return -EIO ; }
@@ -2157,6 +2174,7 @@ static int sr2_bit7_quad_enable(struct spi_nor *nor) {u8 *sr2 = nor->bouncebuf;int ret; +u8 sr2_written;/* Check current Quad Enable bit value. */ ret =spi_nor_read_sr2 (nor, sr2);
@@ -2172,13 +2190,15 @@ static int sr2_bit7_quad_enable(struct spi_nor *nor)if (ret)return ret; + sr2_written = *sr2; +/* Read back and check it. */ ret =spi_nor_read_sr2 (nor, sr2);if (ret)return ret; -if (!(*sr2 &SR2_QUAD_EN_BIT7 )) { -dev_dbg (nor->dev,"SR2 Quad bit not set\n" ); +if (*sr2 != sr2_written) { +dev_dbg (nor->dev,"SR2: Read back test failed\n" );return -EIO ; }
Signature · 1 line
2.9.5
Re: [PATCH v5 3/6] mtd: spi-nor: Extend the SR Read Back test
Quoted 6 lines · On 07-Nov-19 2:11 PM, [email protected] wrote:
From: Tudor Ambarus <[email protected]>
Test that all the bits from Status Register 1 and Status Register 2 were written correctly.
Signed-off-by Tudor Ambarus <[email protected]>
Regards
Vignesh
Quoted 116 lines
--- drivers/mtd/spi-nor/spi-nor.c | 58 +++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 19 deletions(-)drivers/mtd/spi-nor/spi-nor.c +37
-17@@ -2047,20 +2047,7 @@ static int macronix_quad_enable(struct spi_nor *nor) nor->bouncebuf[0 ] |=SR_QUAD_EN_MX ; - ret =spi_nor_write_sr (nor, nor->bouncebuf,1 ); -if (ret) -return ret; - - ret =spi_nor_read_sr (nor, nor->bouncebuf); -if (ret) -return ret; - -if (!(nor->bouncebuf[0 ] &SR_QUAD_EN_MX )) { -dev_dbg (nor->dev,"Macronix Quad bit not set\n" ); -return -EIO ; - } - -return 0 ; +return spi_nor_write_sr1_and_check (nor, nor->bouncebuf[0 ]); }/** @@ -2080,6 +2067,7 @@ static int spansion_no_read_cr_quad_enable(struct spi_nor *nor) {u8 *sr_cr = nor->bouncebuf;int ret; +u8 sr_written;/* Keep the current value of the Status Register. */ ret =spi_nor_read_sr (nor, sr_cr);@@ -2088,7 +2076,22 @@ static int spansion_no_read_cr_quad_enable(struct spi_nor *nor) sr_cr[1 ] =CR_QUAD_EN_SPAN ; -return spi_nor_write_sr (nor, sr_cr,2 ); + ret =spi_nor_write_sr (nor, sr_cr,2 ); +if (ret) +return ret; + + sr_written = sr_cr[0 ]; + + ret =spi_nor_read_sr (nor, sr_cr); +if (ret) +return ret; + +if (sr_cr[0 ] != sr_written) { +dev_err (nor->dev,"SR: Read back test failed\n" ); +return -EIO ; + } + +return 0 ; }/** @@ -2108,6 +2111,7 @@ static int spansion_read_cr_quad_enable(struct spi_nor *nor) {u8 *sr_cr = nor->bouncebuf;int ret; +u8 sr_written;/* Check current Quad Enable bit value. */ ret =spi_nor_read_cr (nor, &sr_cr[1 ]);@@ -2128,13 +2132,26 @@ static int spansion_read_cr_quad_enable(struct spi_nor *nor)if (ret)return ret; + sr_written = sr_cr[0 ]; + + ret =spi_nor_read_sr (nor, sr_cr); +if (ret) +return ret; + +if (sr_written != sr_cr[0 ]) { +dev_err (nor->dev,"SR: Read back test failed\n" ); +return -EIO ; + } + + sr_written = sr_cr[1 ]; +/* Read back and check it. */ ret =spi_nor_read_cr (nor, &sr_cr[1 ]);if (ret)return ret; -if (!(sr_cr[1 ] &CR_QUAD_EN_SPAN )) { -dev_dbg (nor->dev,"Spansion Quad bit not set\n" ); +if (sr_cr[1 ] != sr_written) { +dev_dbg (nor->dev,"CR: Read back test failed\n" );return -EIO ; }@@ -2157,6 +2174,7 @@ static int sr2_bit7_quad_enable(struct spi_nor *nor) {u8 *sr2 = nor->bouncebuf;int ret; +u8 sr2_written;/* Check current Quad Enable bit value. */ ret =spi_nor_read_sr2 (nor, sr2);@@ -2172,13 +2190,15 @@ static int sr2_bit7_quad_enable(struct spi_nor *nor)if (ret)return ret; + sr2_written = *sr2; +/* Read back and check it. */ ret =spi_nor_read_sr2 (nor, sr2);
drivers/mtd/spi-nor/spi-nor.c static int sr2_bit7_quad_enable(struct spi_nor *nor) · line 2204
Quoted 10 lines
drivers/mtd/spi-nor/spi-nor.c +2
-2if (ret)return ret; -if (!(*sr2 &SR2_QUAD_EN_BIT7 )) { -dev_dbg (nor->dev,"SR2 Quad bit not set\n" ); +if (*sr2 != sr2_written) { +dev_dbg (nor->dev,"SR2: Read back test failed\n" );return -EIO ; }
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
PATCH v5 4/6 mtd: spi-nor: Rename CR_QUAD_EN_SPAN to SR2_QUAD_EN_BIT1
In reply to [email protected]From: Tudor Ambarus <[email protected]>
JEDEC Basic Flash Parameter Table, 15th DWORD, bits 22:20, refers to this bit as "bit 1 of the status register 2". Rename the macro accordingly.
--- drivers/mtd/spi-nor/spi-nor.c | 8 ++++---- include/linux/mtd/spi-nor.h | 4 +--- 2 files changed, 5 insertions(+), 7 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +4 -4
@@ -1026,7 +1026,7 @@ static int spi_nor_write_16bit_sr_and_check(struct spi_nor *nor, u8 sr1)* Write Status (01h) command is available just for the cases * in which the QE bit is described in SR2 at BIT(1). */ - sr_cr[1 ] =CR_QUAD_EN_SPAN ; + sr_cr[1 ] =SR2_QUAD_EN_BIT1 ; }else { sr_cr[1 ] =0 ; }
@@ -2074,7 +2074,7 @@ static int spansion_no_read_cr_quad_enable(struct spi_nor *nor)if (ret)return ret; - sr_cr[1 ] =CR_QUAD_EN_SPAN ; + sr_cr[1 ] =SR2_QUAD_EN_BIT1 ; ret =spi_nor_write_sr (nor, sr_cr,2 );if (ret)
@@ -2118,10 +2118,10 @@ static int spansion_read_cr_quad_enable(struct spi_nor *nor)if (ret)return ret; -if (sr_cr[1 ] &CR_QUAD_EN_SPAN ) +if (sr_cr[1 ] &SR2_QUAD_EN_BIT1 )return 0 ; - sr_cr[1 ] |=CR_QUAD_EN_SPAN ; + sr_cr[1 ] |=SR2_QUAD_EN_BIT1 ;/* Keep the current value of the Status Register. */ ret =spi_nor_read_sr (nor, sr_cr);
include/linux/mtd/spi-nor.h +1 -3
@@ -144,10 +144,8 @@#define FSR_P_ERR BIT(4)/* Program operation status */ #define FSR_PT_ERR BIT(1)/* Protection error bit */ -/* Configuration Register bits. */ -#define CR_QUAD_EN_SPAN BIT(1)/* Spansion Quad I/O */ -/* Status Register 2 bits. */ +#define SR2_QUAD_EN_BIT1 BIT(1)#define SR2_QUAD_EN_BIT7 BIT(7)/* Supported SPI protocols */
Signature · 1 line
2.9.5
PATCH v5 5/6 mtd: spi-nor: Merge spansion Quad Enable methods
In reply to [email protected]From: Tudor Ambarus <[email protected]>
Merge
spansion_no_read_cr_quad_enable()
spansion_read_cr_quad_enable()
into
spi_nor_sr2_bit1_quad_enable().
Reduce code duplication by introducing spi_nor_write_16bit_cr_and_check(). The Configuration Register contains bits that can be updated in future:
FREEZE, CMP. Provide a generic method that allows updating all bits of the Configuration Register.
--- drivers/mtd/spi-nor/spi-nor.c | 161 +++++++++++++++++------------------------- 1 file changed, 66 insertions(+), 95 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +66 -95
@@ -1055,6 +1055,59 @@ static int spi_nor_write_16bit_sr_and_check(struct spi_nor *nor, u8 sr1) }/** +* spi_nor_write_16bit_cr_and_check() - Write the Status Register 1 and the +* Configuration Register in one shot. Ensure that the byte written in the +* Configuration Register match the received value, and that the 16-bit Write +* did not affect what was already in the Status Register 1. +* @nor: pointer to a 'struct spi_nor'. +* @cr: byte value to be written to the Configuration Register. +* +* Return: 0 on success, -errno otherwise. +*/ +static int spi_nor_write_16bit_cr_and_check (struct spi_nor *nor,u8 cr) +{ +int ret; +u8 *sr_cr = nor->bouncebuf; +u8 sr_written; + +/* Keep the current value of the Status Register 1. */ + ret =spi_nor_read_sr (nor, sr_cr); +if (ret) +return ret; + + sr_cr[1 ] = cr; + + ret =spi_nor_write_sr (nor, sr_cr,2 ); +if (ret) +return ret; + + sr_written = sr_cr[0 ]; + + ret =spi_nor_read_sr (nor, sr_cr); +if (ret) +return ret; + +if (sr_written != sr_cr[0 ]) { +dev_dbg (nor->dev,"SR: Read back test failed\n" ); +return -EIO ; + } + +if (nor->flags &SNOR_F_NO_READ_CR ) +return 0 ; + + ret =spi_nor_read_cr (nor, &sr_cr[1 ]); +if (ret) +return ret; + +if (cr != sr_cr[1 ]) { +dev_dbg (nor->dev,"CR: read back test failed\n" ); +return -EIO ; + } + +return 0 ; +} + +/** * spi_nor_write_sr_and_check() - Write the Status Register 1 and ensure that * the byte written match the received value without affecting other bits in the * Status Register 1 and 2.
@@ -2051,111 +2104,29 @@ static int macronix_quad_enable(struct spi_nor *nor) }/** -* spansion_no_read_cr_quad_enable() - set QE bit in Configuration Register. -* @nor: pointer to a 'struct spi_nor' +* spi_nor_sr2_bit1_quad_enable() - set the Quad Enable BIT(1) in the Status +* Register 2. +* @nor: pointer to a 'struct spi_nor'. * -* Set the Quad Enable (QE) bit in the Configuration Register. -* This function should be used with QSPI memories not supporting the Read -* Configuration Register (35h) instruction. -* -* bit 1 of the Configuration Register is the QE bit for Spansion like QSPI -* memories. +* Bit 1 of the Status Register 2 is the QE bit for Spansion like QSPI memories. * * Return: 0 on success, -errno otherwise. */ -static int spansion_no_read_cr_quad_enable (struct spi_nor *nor) +static int spi_nor_sr2_bit1_quad_enable (struct spi_nor *nor) { -u8 *sr_cr = nor->bouncebuf;int ret; -u8 sr_written; - -/* Keep the current value of the Status Register. */ - ret =spi_nor_read_sr (nor, sr_cr); -if (ret) -return ret; - - sr_cr[1 ] =SR2_QUAD_EN_BIT1 ; - - ret =spi_nor_write_sr (nor, sr_cr,2 ); -if (ret) -return ret; - sr_written = sr_cr[0 ]; - - ret =spi_nor_read_sr (nor, sr_cr); -if (ret) -return ret; - -if (sr_cr[0 ] != sr_written) { -dev_err (nor->dev,"SR: Read back test failed\n" ); -return -EIO ; - } - -return 0 ; -} - -/** -* spansion_read_cr_quad_enable() - set QE bit in Configuration Register. -* @nor: pointer to a 'struct spi_nor' -* -* Set the Quad Enable (QE) bit in the Configuration Register. -* This function should be used with QSPI memories supporting the Read -* Configuration Register (35h) instruction. -* -* bit 1 of the Configuration Register is the QE bit for Spansion like QSPI -* memories. -* -* Return: 0 on success, -errno otherwise. -*/ -static int spansion_read_cr_quad_enable (struct spi_nor *nor) -{ -u8 *sr_cr = nor->bouncebuf; -int ret; -u8 sr_written; +if (nor->flags &SNOR_F_NO_READ_CR ) +return spi_nor_write_16bit_cr_and_check (nor,SR2_QUAD_EN_BIT1 ); -/* Check current Quad Enable bit value. */ - ret =spi_nor_read_cr (nor, &sr_cr[1 ]); + ret =spi_nor_read_cr (nor, nor->bouncebuf);if (ret)return ret; -if (sr_cr[1 ] &SR2_QUAD_EN_BIT1 ) +if (nor->bouncebuf[0 ] &SR2_QUAD_EN_BIT1 )return 0 ; - sr_cr[1 ] |=SR2_QUAD_EN_BIT1 ; - -/* Keep the current value of the Status Register. */ - ret =spi_nor_read_sr (nor, sr_cr); -if (ret) -return ret; - - ret =spi_nor_write_sr (nor, sr_cr,2 ); -if (ret) -return ret; - - sr_written = sr_cr[0 ]; - - ret =spi_nor_read_sr (nor, sr_cr); -if (ret) -return ret; - -if (sr_written != sr_cr[0 ]) { -dev_err (nor->dev,"SR: Read back test failed\n" ); -return -EIO ; - } - - sr_written = sr_cr[1 ]; - -/* Read back and check it. */ - ret =spi_nor_read_cr (nor, &sr_cr[1 ]); -if (ret) -return ret; - -if (sr_cr[1 ] != sr_written) { -dev_dbg (nor->dev,"CR: Read back test failed\n" ); -return -EIO ; - } - -return 0 ; +return spi_nor_write_16bit_cr_and_check (nor, nor->bouncebuf[0 ]); }/**
@@ -3685,7 +3656,7 @@ static int spi_nor_parse_bfpt(struct spi_nor *nor,* supported. */ nor->flags |=SNOR_F_HAS_16BIT_SR |SNOR_F_NO_READ_CR ; - params->quad_enable = spansion_no_read_cr_quad_enable; + params->quad_enable = spi_nor_sr2_bit1_quad_enable;break ;case BFPT_DWORD15_QER_SR1_BIT6 :
@@ -3707,7 +3678,7 @@ static int spi_nor_parse_bfpt(struct spi_nor *nor,*/ nor->flags |=SNOR_F_HAS_16BIT_SR ; - params->quad_enable = spansion_read_cr_quad_enable; + params->quad_enable = spi_nor_sr2_bit1_quad_enable;break ;default :
@@ -4697,7 +4668,7 @@ static void spi_nor_info_init_params(struct spi_nor *nor)u8 i, erase_mask;/* Initialize legacy flash parameters and settings. */ - params->quad_enable = spansion_read_cr_quad_enable; + params->quad_enable = spi_nor_sr2_bit1_quad_enable; params->set_4byte = spansion_set_4byte; params->setup = spi_nor_default_setup;/* Default to 16-bit Write Status (01h) Command */
Signature · 1 line
2.9.5
PATCH v5 6/6 mtd: spi-nor: Rename Quad Enable methods
In reply to [email protected]From: Tudor Ambarus <[email protected]>
Rename macronix_quad_enable() to a generic name:spi_nor_sr1_bit6_quad_enable().
Prepend "spi_nor_" to "sr2_bit7_quad_enable". All SPI NOR generic methods should be prepended by "spi_nor_".
--- drivers/mtd/spi-nor/spi-nor.c | 25 ++++++++++++------------- include/linux/mtd/spi-nor.h | 2 +- 2 files changed, 13 insertions(+), 14 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +12 -13
@@ -2078,16 +2078,15 @@ static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) }/** -* macronix_quad_enable() - set QE bit in Status Register. +* spi_nor_sr1_bit6_quad_enable() - Set the Quad Enable BIT(6) in the Status +* Register 1. * @nor: pointer to a 'struct spi_nor' * -* Set the Quad Enable (QE) bit in the Status Register. -* -* bit 6 of the Status Register is the QE bit for Macronix like QSPI memories. +* Bit 6 of the Status Register 1 is the QE bit for Macronix like QSPI memories. * * Return: 0 on success, -errno otherwise. */ -static int macronix_quad_enable (struct spi_nor *nor) +static int spi_nor_sr1_bit6_quad_enable (struct spi_nor *nor) {int ret;
@@ -2095,10 +2094,10 @@ static int macronix_quad_enable(struct spi_nor *nor)if (ret)return ret; -if (nor->bouncebuf[0 ] &SR_QUAD_EN_MX ) +if (nor->bouncebuf[0 ] &SR1_QUAD_EN_BIT6 )return 0 ; - nor->bouncebuf[0 ] |=SR_QUAD_EN_MX ; + nor->bouncebuf[0 ] |=SR1_QUAD_EN_BIT6 ;return spi_nor_write_sr1_and_check (nor, nor->bouncebuf[0 ]); }
@@ -2130,7 +2129,7 @@ static int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor) }/** -* sr2_bit7_quad_enable() - set QE bit in Status Register 2. +* spi_nor_sr2_bit7_quad_enable() - set QE bit in Status Register 2. * @nor: pointer to a 'struct spi_nor' * * Set the Quad Enable (QE) bit in the Status Register 2.
@@ -2141,7 +2140,7 @@ static int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor)* * Return: 0 on success, -errno otherwise. */ -static int sr2_bit7_quad_enable (struct spi_nor *nor) +static int spi_nor_sr2_bit7_quad_enable (struct spi_nor *nor) {u8 *sr2 = nor->bouncebuf;int ret;
@@ -2281,7 +2280,7 @@ static void gd25q256_default_init(struct spi_nor *nor)* indicate the quad_enable method for this case, we need * to set it in the default_init fixup hook. */ - nor->params.quad_enable = macronix_quad_enable; + nor->params.quad_enable = spi_nor_sr1_bit6_quad_enable; }static struct spi_nor_fixups gd25q256_fixups = {
@@ -3661,12 +3660,12 @@ static int spi_nor_parse_bfpt(struct spi_nor *nor,case BFPT_DWORD15_QER_SR1_BIT6 : nor->flags &= ~SNOR_F_HAS_16BIT_SR ; - params->quad_enable = macronix_quad_enable; + params->quad_enable = spi_nor_sr1_bit6_quad_enable;break ;case BFPT_DWORD15_QER_SR2_BIT7 : nor->flags &= ~SNOR_F_HAS_16BIT_SR ; - params->quad_enable = sr2_bit7_quad_enable; + params->quad_enable = spi_nor_sr2_bit7_quad_enable;break ;case BFPT_DWORD15_QER_SR2_BIT1 :
@@ -4569,7 +4568,7 @@ static void intel_set_default_init(struct spi_nor *nor)static void macronix_set_default_init (struct spi_nor *nor) { - nor->params.quad_enable = macronix_quad_enable; + nor->params.quad_enable = spi_nor_sr1_bit6_quad_enable; nor->params.set_4byte = macronix_set_4byte; }
include/linux/mtd/spi-nor.h +1 -1
@@ -133,7 +133,7 @@#define SR_E_ERR BIT(5)#define SR_P_ERR BIT(6) -#define SR_QUAD_EN_MX BIT(6)/* Macronix Quad I/O */ +#define SR1_QUAD_EN_BIT6 BIT(6)/* Enhanced Volatile Configuration Register bits */ #define EVCR_QUAD_EN_MICRON BIT(7)/* Micron Quad I/O */
Signature · 1 line
2.9.5
Re: [PATCH v5 6/6] mtd: spi-nor: Rename Quad Enable methods
Quoted 9 lines · On 07-Nov-19 2:12 PM, [email protected] wrote:
From: Tudor Ambarus <[email protected]>
Rename
macronix_quad_enable()to a generic name:spi_nor_sr1_bit6_quad_enable().Prepend "
spi_nor_" to "sr2_bit7_quad_enable". All SPI NOR generic methods should be prepended by "spi_nor_".Signed-off-by Tudor Ambarus <[email protected]>
Regards
Vignesh
Quoted 108 lines
--- drivers/mtd/spi-nor/spi-nor.c | 25 ++++++++++++------------- include/linux/mtd/spi-nor.h | 2 +- 2 files changed, 13 insertions(+), 14 deletions(-)drivers/mtd/spi-nor/spi-nor.c +12
-13@@ -2078,16 +2078,15 @@ static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) }/** -* macronix_quad_enable() - set QE bit in Status Register. +* spi_nor_sr1_bit6_quad_enable() - Set the Quad Enable BIT(6) in the Status +* Register 1. * @nor: pointer to a 'struct spi_nor' * -* Set the Quad Enable (QE) bit in the Status Register. -* -* bit 6 of the Status Register is the QE bit for Macronix like QSPI memories. +* Bit 6 of the Status Register 1 is the QE bit for Macronix like QSPI memories. * * Return: 0 on success, -errno otherwise. */ -static int macronix_quad_enable (struct spi_nor *nor) +static int spi_nor_sr1_bit6_quad_enable (struct spi_nor *nor) {int ret;@@ -2095,10 +2094,10 @@ static int macronix_quad_enable(struct spi_nor *nor)if (ret)return ret; -if (nor->bouncebuf[0 ] &SR_QUAD_EN_MX ) +if (nor->bouncebuf[0 ] &SR1_QUAD_EN_BIT6 )return 0 ; - nor->bouncebuf[0 ] |=SR_QUAD_EN_MX ; + nor->bouncebuf[0 ] |=SR1_QUAD_EN_BIT6 ;return spi_nor_write_sr1_and_check (nor, nor->bouncebuf[0 ]); }@@ -2130,7 +2129,7 @@ static int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor) }/** -* sr2_bit7_quad_enable() - set QE bit in Status Register 2. +* spi_nor_sr2_bit7_quad_enable() - set QE bit in Status Register 2. * @nor: pointer to a 'struct spi_nor' * * Set the Quad Enable (QE) bit in the Status Register 2. @@ -2141,7 +2140,7 @@ static int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor)* * Return: 0 on success, -errno otherwise. */ -static int sr2_bit7_quad_enable (struct spi_nor *nor) +static int spi_nor_sr2_bit7_quad_enable (struct spi_nor *nor) {u8 *sr2 = nor->bouncebuf;int ret;@@ -2281,7 +2280,7 @@ static void gd25q256_default_init(struct spi_nor *nor)* indicate the quad_enable method for this case, we need * to set it in the default_init fixup hook. */ - nor->params.quad_enable = macronix_quad_enable; + nor->params.quad_enable = spi_nor_sr1_bit6_quad_enable; }static struct spi_nor_fixups gd25q256_fixups = {@@ -3661,12 +3660,12 @@ static int spi_nor_parse_bfpt(struct spi_nor *nor,case BFPT_DWORD15_QER_SR1_BIT6 : nor->flags &= ~SNOR_F_HAS_16BIT_SR ; - params->quad_enable = macronix_quad_enable; + params->quad_enable = spi_nor_sr1_bit6_quad_enable;break ;case BFPT_DWORD15_QER_SR2_BIT7 : nor->flags &= ~SNOR_F_HAS_16BIT_SR ; - params->quad_enable = sr2_bit7_quad_enable; + params->quad_enable = spi_nor_sr2_bit7_quad_enable;break ;case BFPT_DWORD15_QER_SR2_BIT1 :@@ -4569,7 +4568,7 @@ static void intel_set_default_init(struct spi_nor *nor)static void macronix_set_default_init (struct spi_nor *nor) { - nor->params.quad_enable = macronix_quad_enable; + nor->params.quad_enable = spi_nor_sr1_bit6_quad_enable; nor->params.set_4byte = macronix_set_4byte; }include/linux/mtd/spi-nor.h +1
-1@@ -133,7 +133,7 @@#define SR_E_ERR BIT(5)#define SR_P_ERR BIT(6) -#define SR_QUAD_EN_MX BIT(6)/* Macronix Quad I/O */ +#define SR1_QUAD_EN_BIT6 BIT(6)/* Enhanced Volatile Configuration Register bits */ #define EVCR_QUAD_EN_MICRON BIT(7)/* Micron Quad I/O */
Quoted 65 lines · On 11/07/2019 10:41 AM, [email protected] wrote:
From: Tudor Ambarus <[email protected]>
Tested on w25q128jvq.
Fixed the clearing of QE bit on (un)lock() operations. Reworked the Quad Enable methods and the disabling of the block write protection at power-up.
v5:
- Rename all Quad Enable methods in one patch
- Extend the Read Back test to both SR1 and SR2 in one patch
- Reorder patches, so that the fixes come one after another
- Collect R-b tags.v4:
- Usedev_dbginsted ofdev_errfor low level info
- replace "&nor->bouncebuf[0]" with "nor->bouncebuf" and "&sr_cr[0]" with "sr_cr". Update across all patches.v3: split patches, update retlen handling in
sst_write.v2:
- Introducespi_nor_write_16bit_cr_and_check()as per Vignesh's suggestion. The Configuration Register contains bits that can be updated in future: FREEZE, CMP. Provide a generic method that allows updating all bits of the Configuration Register.
- FixSNOR_F_NO_READ_CRcase in
"mtd: spi-nor: Rework the disabling of block write protection". When the flash doesn't support the CR Read command, we make an assumption about the value of the QE bit. Inspi_nor_init(), callspi_nor_quad_enable()first, thenspi_nor_unlock_all(), so that at thespi_nor_unlock_all()time we can be sure the QE bit has value one, because of the previous call tospi_nor_quad_enable().
- Fix if statement inspi_nor_write_sr_and_check():
if (nor->flags&SNOR_F_HAS_16BIT_SR)
- Fix documentation warnings.
- New patch: "mtd: spi-nor: Check all the bits written, not just the BP ones".
- Drop Global Unlock patches, will send them in a different patch set.The patch set can be tested using mtd-utils:
1/ do a read-erase-write-read-back test immediately after boot, to check thespi_nor_unlock_all()method. The focus is on the erase/write methods, we want to see if the flash is unlocked at power-up.
mtd_debugread/dev/mtd-yoursoffset size read-file hexdump read-file
mtd_debugerase/dev/mtd-yoursoffset size
dd if=/dev/urandom of=write-file bs=please-choose count=please-choosemtd_debugwrite/dev/mtd-yoursoffset write-file-size write-filemtd_debugread/dev/mtd-yoursoffset write-file-size read-file sha1sum read-file write-file
2/ lock flash then try to erase/write it, to see if the lock works
flash_lock/dev/mtd-yoursoffset block-count
Do the read-erase-write-read-back test from 1/. The contents of flash should not change in the erase and write steps.
3/ unlock flash and do the read-erase-write-read-back from 1/. The value of the
QEE should not change and you should be able to erase and write the flash. Test 1/ should be successful.Tudor Ambarus (6):
mtd: spi-nor: Fix clearing of QE bit on lock()/unlock()
mtd: spi-nor: Rework the disabling of block write protection
mtd: spi-nor: Extend the SR Read Back test
mtd: spi-nor: RenameCR_QUAD_EN_SPANtoSR2_QUAD_EN_BIT1
mtd: spi-nor: Merge spansion Quad Enable methods
mtd: spi-nor: Rename Quad Enable methods
Quoted 4 lines
drivers/mtd/spi-nor/spi-nor.c | 438 ++++++++++++++++++++++++------------------ include/linux/mtd/spi-nor.h | 12 +- 2 files changed, 254 insertions(+), 196 deletions(-)
Amend commit description for patch 2/6, s/the the/the. All applied to spi-nor/next.