spi: cadence_qspi: Initialize read and write watermark registers
Subsystems (4)
PATCH 20/30 spi: cadence_qspi: Initialize read and write watermark registers
From: Ashok Reddy Soma <[email protected]>
Read and Write watermark registers are not initialized. Set read watermark to half of the FIFO and write watermark to 1/8 of the FIFO size.
Read watermark indicates if SRAM fill level is above this watermark, interrupt will be generated and read or DMA can be performed.
Write watermark indicates the maximum fill level of SRAM when write is performed to device.
These values of 1/2 for read and 1/8 for write are chosen similar to Linux driver.
--- drivers/spi/cadence_qspi_apb.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/spi/cadence_qspi_apb.c +8 -0
@@ -362,6 +362,14 @@ void cadence_qspi_apb_controller_init(struct cadence_spi_priv *priv)/* Indirect mode configurations */ writel (priv->fifo_depth /2 , priv->regbase +CQSPI_REG_SRAMPARTITION ); +/* Program read watermark -- 1/2 of the FIFO. */ +writel (priv->fifo_depth * priv->fifo_width /2 , + priv->regbase +CQSPI_REG_INDIRECTRDWATERMARK ); + +/* Program write watermark -- 1/8 of the FIFO. */ +writel (priv->fifo_depth * priv->fifo_width /8 , + priv->regbase +CQSPI_REG_INDIRECTWRWATERMARK ); +/* Disable all interrupts */ writel (0 , priv->regbase +CQSPI_REG_IRQMASK );
Signature · 1 line
2.27.0
PATCH 21/30 spi: cadence_qspi: Enable ECO bit for higher frequencies
From: Ashok Reddy Soma <[email protected]>
Enable ECO bit for Versal for frequencies above 120Mhz for octal spi to work properly.
--- drivers/spi/cadence_qspi.h | 1 + drivers/spi/cadence_qspi_apb.c | 4 ++++ 2 files changed, 5 insertions(+)
drivers/spi/cadence_qspi.h +1 -0
@@ -55,6 +55,7 @@#define CQSPI_READ_ID_LEN 3#define CQSPI_READID_LOOP_MAX 10#define TERA_MACRO 1000000000000l +#define TAP_GRAN_SEL_MIN_FREQ 120000000#define OSPI_CTRL_RST 0xF1260304
drivers/spi/cadence_qspi_apb.c +4 -0
@@ -382,6 +382,10 @@ void cadence_qspi_apb_controller_init(struct cadence_spi_priv *priv)writel (reg, priv->regbase +CQSPI_REG_CONFIG ); +if (IS_ENABLED (CONFIG_ARCH_VERSAL ) && + priv->ref_clk_hz >=TAP_GRAN_SEL_MIN_FREQ ) +writel (1 , priv->regbase +CQSPI_REG_ECO ); +cadence_qspi_apb_controller_enable (priv->regbase); }
Signature · 1 line
2.27.0
PATCH 22/30 spi: cadence_qspi: Add spi mem dtr support ops
In reply to Tejas BhumkarFrom: T Karthik Reddy <[email protected]>
In DDR mode, current default spi_mem_dtr_supports_op() function does not allow mixed DTR operation functionality. So implement cadence specific cadence_spi_mem_dtr_supports_op() function to verifying only the command buswidth and command opcode bytes which satisfies the DTR protocol.
--- drivers/spi/cadence_qspi.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
drivers/spi/cadence_qspi.c +16 -1
@@ -718,6 +718,21 @@ static int cadence_spi_mem_exec_op(struct spi_slave *spi,return err; } +static bool cadence_spi_mem_dtr_supports_op (struct spi_slave *slave, +const struct spi_mem_op *op) +{ +/* +* In DTR mode, except op->cmd all other parameters like address, +* dummy and data could be 0. +* So lets only check if the cmd buswidth and number of opcode bytes +* are true for DTR to support. +*/ +if (op->cmd.buswidth ==8 && op->cmd.nbytes %2 ) +return false; + +return true; +} +static bool cadence_spi_mem_supports_op (struct spi_slave *slave,const struct spi_mem_op *op) {
@@ -740,7 +755,7 @@ static bool cadence_spi_mem_supports_op(struct spi_slave *slave,return false;if (all_true) -return spi_mem_dtr_supports_op (slave, op); +return cadence_spi_mem_dtr_supports_op (slave, op);else return spi_mem_default_supports_op (slave, op); }
Signature · 1 line
2.27.0
PATCH 23/30 spi: cadence_qspi: Write aligned byte length to ahbbase
In reply to Tejas BhumkarFrom: Ashok Reddy Soma <[email protected]>
Incase of non-aligned length of flash data, ahbbase address is written directly with byte count. This is causing AHB bus error's sometimes and resulting in kernel crash while booting linux. To avoid this write 4 byte aligned byte count to ahbbase address.
Also use a temporary variable with 0xffffffff data and overwrite this temp with unaligned bytes data before writing to ahbbase.
The value 0xffffffff is chosen as this is flash memory, worst case we will write 0xff to any location which doesn't effect any bits.
State: pending
--- drivers/spi/cadence_qspi_apb.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/spi/cadence_qspi_apb.c +6 -4
@@ -934,10 +934,12 @@ cadence_qspi_apb_indirect_write_execute(struct cadence_spi_priv *priv,while (remaining >0 ) { write_bytes = remaining > page_size ? page_size : remaining;writesl (priv->ahbbase, bb_txbuf, write_bytes >>2 ); -if (write_bytes %4 ) -writesb (priv->ahbbase, - bb_txbuf +rounddown (write_bytes,4 ), - write_bytes %4 ); +if (write_bytes %4 ) { +unsigned int temp =0xffffffff ; + +memcpy (&temp, bb_txbuf +rounddown (write_bytes,4 ), write_bytes %4 ); +writel (temp, priv->ahbbase); + }/* Wait up to Indirect Operation Complete bit to set */ ret = readl_poll_timeout(priv->regbase + CQSPI_REG_IRQSTATUS, cr,
Signature · 1 line
2.27.0
PATCH 24/30 arm64: versal: Enable soft reset support for xspi flashes
In reply to Tejas BhumkarActivate the xSPI Software Reset support, which will be utilized to transition from octal DTR mode to legacy mode during shutdown and boot (if enabled).
--- configs/xilinx_versal_virt_defconfig | 2 ++ 1 file changed, 2 insertions(+)
configs/xilinx_versal_virt_defconfig +2 -0
@@ -95,6 +95,8 @@ CONFIG_MMC_SDHCI_ZYNQ=y CONFIG_ZYNQ_SDHCI_MIN_FREQ=100000 CONFIG_MTD=y CONFIG_DM_SPI_FLASH=y +CONFIG_SPI_FLASH_SOFT_RESET=y +CONFIG_SPI_FLASH_SOFT_RESET_ON_BOOT=y CONFIG_SPI_FLASH_MT35XU=y CONFIG_SPI_FLASH_GIGADEVICE=y CONFIG_SPI_FLASH_ISSI=y
Signature · 1 line
2.27.0
PATCH 25/30 arm64: versal: Enable octal DTR mode
In reply to Tejas BhumkarThe Cadence driver must switch between SDR and DTR modes based on commands from the SPI-NOR framework and the configuration set by SPI_FLASH_DTR_ENABLE. If SPI_FLASH_DTR_ENABLE is enabled, the driver should transition to DTR mode; if it is not defined, the driver should avoid switching to DTR mode, signaling that the controller does not support DTR.
--- configs/xilinx_versal_virt_defconfig | 1 + 1 file changed, 1 insertion(+)
configs/xilinx_versal_virt_defconfig +1 -0
@@ -98,6 +98,7 @@ CONFIG_DM_SPI_FLASH=y CONFIG_SPI_FLASH_SOFT_RESET=y CONFIG_SPI_FLASH_SOFT_RESET_ON_BOOT=y CONFIG_SPI_FLASH_MT35XU=y +CONFIG_SPI_FLASH_DTR_ENABLE=y CONFIG_SPI_FLASH_GIGADEVICE=y CONFIG_SPI_FLASH_ISSI=y CONFIG_SPI_FLASH_MACRONIX=y
Signature · 1 line
2.27.0
PATCH 26/30 mtd: spi-nor: Add block protection support for micron flashes
In reply to Tejas BhumkarFrom: T Karthik Reddy <[email protected]>
Micron nor flashes provide block protection support using BP0, BP1, BP2, BP3 & TB bits in status register. This patch supports for micron nor flashes with manufacturer id 0x20 and 0x2c.
Where BP(Block Protection) bits defines memory to be software protected against PROGRAM or ERASE operations. When one or more block protect bits are set to 1, a designated memory area is protected from PROGRAM and ERASE operations. TB(Top/Bottom) bit determines whether the protected memory area defined by the block protect bits starts from the top or bottom of the memory array.
Block Protection table for N25Q00AA with size 128MB, sector size 64KB and with 2048 sectors.
Top protection:
--------------
TB BP3 BP2 BP1 BP0 Protected Area Un-Protected Area
0 0 0 0 0 None All sectors
0 0 0 0 1 Sector 2047 Sectors (0 to 2046)
0 0 0 1 0 Sectors (2046 to 2047) Sectors (0 to 2045)
0 0 0 1 1 Sectors (2044 to 2047) Sectors (0 to 2043) 0 0 1 0 0 Sectors (2040 to 2047) Sectors (0 to 2039) 0 0 1 0 1 Sectors (2032 to 2047) Sectors (0 to 2031) 0 0 1 1 0 Sectors (2016 to 2047) Sectors (0 to 2015) 0 0 1 1 1 Sectors (1984 to 2047) Sectors (0 to 1983) 0 1 0 0 0 Sectors (1920 to 2047) Sectors (0 to 1919) 0 1 0 0 1 Sectors (1792 to 2047) Sectors (0 to 1791) 0 1 0 1 0 Sectors (1936 to 2047) Sectors (0 to 1535)
0 1 0 1 1 Sectors (1024 to 2047) None
Bottom protection:
-----------------
TB BP3 BP2 BP1 BP0 Protected Area Un-protected Area
1 0 0 0 0 None All sectors
1 0 0 0 1 sector 0 Sectors (1 to 2047)
1 0 0 1 0 Sectors (0 to 1) Sectors (2 to 2047)
1 0 0 1 1 Sectors (0 to 3) Sectors (4 to 2047)
1 0 1 0 0 Sectors (0 to 7) Sectors (8 to 2047)
1 0 1 0 1 Sectors (0 to 15) Sectors (16 to 2047)
1 0 1 1 0 Sectors (0 to 31) Sectors (32 to 2047)
1 0 1 1 1 Sectors (0 to 63) Sectors (64 to 2047)
1 1 0 0 0 Sectors (0 to 127) Sectors (128 to 2047)
1 1 0 0 1 Sectors (0 to 255) Sectors (256 to 2047)
1 1 0 1 0 Sectors (0 to 511) Sectors (512 to 2047)
1 1 0 1 1 Sectors (0 to 1023) Sectors (1024 to 2047)
--- drivers/mtd/spi/spi-nor-core.c | 255 +++++++++++++++++++++++++++++++++ include/linux/mtd/spi-nor.h | 7 + 2 files changed, 262 insertions(+)
drivers/mtd/spi/spi-nor-core.c +255 -0
@@ -4336,6 +4336,253 @@ static int spi_nor_init(struct spi_nor *nor)return 0 ; } +#if defined(CONFIG_SPI_FLASH_LOCK ) +#if defined(CONFIG_SPI_FLASH_STMICRO ) +static inline uint16_t min_lockable_sectors (struct spi_nor *nor, +uint16_t n_sectors) +{ +/* lock granularity */ +return 1 ; +} + +static inline uint32_t get_protected_area_start (struct spi_nor *nor, +u8 lock_bits, +bool is_bottom) +{ +u16 n_sectors; +u32 sector_size, flash_size; +int ret; + + sector_size = nor->sector_size; + +if (nor->flags &SNOR_F_HAS_PARALLEL ) { + n_sectors = (nor->size >>0x01 ) / sector_size; + flash_size = nor->size >>0x01 ; + }else { + n_sectors = nor->size / sector_size; + flash_size = nor->size; + } + +if (!is_bottom) + ret = flash_size - ((1 << (lock_bits -1 )) * sector_size * +min_lockable_sectors (nor, n_sectors)); +else + ret = (1 << (lock_bits -1 )) * sector_size * +min_lockable_sectors (nor, n_sectors); + +return ret; +} + +static uint8_t min_protected_area_including_offset (struct spi_nor *nor, +u32 offset, +bool is_bottom) +{ +u8 lock_bits, lockbits_limit; + + lockbits_limit =MAX_LOCKBITS ; + +for (lock_bits =1 ; lock_bits < lockbits_limit; lock_bits++) { +if (!is_bottom) { +/* top protection */ +if (offset >=get_protected_area_start (nor, + lock_bits, + is_bottom)) +break ; + }else { +/* bottom protection */ +if (offset <=get_protected_area_start (nor, + lock_bits, + is_bottom)) +break ; + } + } +return lock_bits; +} + +static int write_sr_modify_protection (struct spi_nor *nor,u8 status, +u8 lock_bits,bool is_bottom) +{ +u8 status_new, bp_mask; +int ret; + + status_new = status & ~BP_MASK ; + bp_mask = (lock_bits <<BP_SHIFT ) &BP_MASK ; + + status_new &= ~SR_BP3 ; +/* Protected area starts from top */ + status_new &= ~SR_TB ; + +/* If bottom area is to be Protected set SR_TB */ +if (is_bottom) + status_new |=SR_TB ; + +if (lock_bits >7 ) + bp_mask |=SR_BP3 ; + + status_new |= bp_mask; + +write_enable (nor); + + nor->spi->flags |=SPI_XFER_LOWER ; + + ret =write_sr (nor, status_new); +if (ret) +return ret; + +if (nor->flags &SNOR_F_HAS_PARALLEL ) { + nor->spi->flags |=SPI_XFER_UPPER ; + ret =write_sr (nor, status_new); +if (ret) +return ret; + } + +return write_disable (nor); +} + +static void micron_get_locked_range (struct spi_nor *nor,u8 sr,loff_t *ofs, +uint64_t *len) +{ +u8 mask =SR_BP0 |SR_BP1 |SR_BP2 ; +int shift =ffs (mask) -1 ; +int pow; +u64 norsize = nor->size; + +if (nor->flags &SNOR_F_HAS_PARALLEL ) + norsize /=2 ; + +if (!(sr & (mask |SR_BP3 ))) { +/* No protection */ + *ofs =0 ; + *len =0 ; + }else { + pow = (sr & mask) >> shift; + pow |= sr &SR_BP3 ?BIT (3 ) :0 ; + +if (pow) + pow--; + + *len = nor->sector_size << pow; +if (*len >= norsize) + *len = norsize; + +if (!(sr &SR_TB )) + *ofs = norsize - *len; +else + *ofs =0 ; + +debug ("%s, ofs:0x%lx, len:0x%lx\n" , __func__, + (unsigned long )*ofs, (unsigned long )*len); + } +} + +static int micron_is_unlocked_sr (struct spi_nor *nor,loff_t ofs,uint64_t len, +u8 sr) +{ +loff_t lock_offs; +u64 lock_len; +int given_range, locked_range; +bool locked_value = false; +bool offset_value = false; + +if (nor->flags &SNOR_F_HAS_PARALLEL ) + ofs /=2 ; + +/* Avoid dividing by 2 of data length for size 1 */ +if (nor->flags &SNOR_F_HAS_PARALLEL && len !=1 ) + len /=2 ; + +debug ("%s, ofs:0x%lx, len:0x%lx\n" , __func__, + (unsigned long )ofs, + (unsigned long )len); + +micron_get_locked_range (nor, sr, &lock_offs, &lock_len); + + given_range = ofs + len; + locked_range = lock_offs + lock_len; + +if (given_range <= locked_range) + locked_value = true; + +if (ofs >= lock_offs) + offset_value = true; + +if (sr &SR_TB ) +return !locked_value; + +return !(locked_value && offset_value); +} + +static int micron_flash_lock (struct spi_nor *nor,loff_t ofs,uint64_t len) +{ +u8 status_old, status_old_up; +u8 lock_bits; +loff_t lock_len; +int ret =0 ; +bool is_bottom = false;/* Use TOP protection by default */ + +if (nor->flags &SNOR_F_HAS_PARALLEL ) + nor->spi->flags |=SPI_XFER_LOWER ; + + status_old =read_sr (nor); +if (status_old <0 ) +return status_old; + +if (nor->flags &SNOR_F_HAS_PARALLEL ) { + nor->spi->flags |=SPI_XFER_UPPER ; + status_old_up =read_sr (nor); +if (status_old_up <0 ) +return status_old_up; +if ((status_old &BPTB_MASK ) != (status_old_up &BPTB_MASK )) { +printf ("BP is different in both flashes lo:0x%x, up:0x%x\n" , + status_old, status_old_up); +return -EINVAL ; + } + } + +if (ofs < nor->size /2 ) + is_bottom = true;/* Change it to bottom protection */ + +debug ("Status in both flashes lo:0x%x, up:0x%x\n" , + status_old, status_old_up); + +if (nor->flags &SNOR_F_HAS_PARALLEL ) { + ofs /=2 ; + len /=2 ; + } + +if (!is_bottom) + lock_len = ofs; +else + lock_len = ofs + len; + + lock_bits =min_protected_area_including_offset (nor, lock_len, + is_bottom); + +if (lock_bits > ((status_old & (BP_MASK <<BP_SHIFT )) >>2 )) + ret =write_sr_modify_protection (nor, status_old, lock_bits, + is_bottom); + +return ret; +} + +static int micron_flash_unlock (struct spi_nor *nor,loff_t ofs,uint64_t len) +{ +return write_sr_modify_protection (nor,0 ,0 ,0 ); +} + +static int micron_is_unlocked (struct spi_nor *nor,loff_t ofs,uint64_t len) +{ +int status; + + status =read_sr (nor); +if (status <0 ) +return status; + +return micron_is_unlocked_sr (nor, ofs, len, status); +} +#endif /* CONFIG_SPI_FLASH_STMICRO */ +#endif /* CONFIG_SPI_FLASH_LOCK */ +#ifdef CONFIG_SPI_FLASH_SOFT_RESET /** * spi_nor_soft_reset() - perform the JEDEC Software Reset sequence
@@ -4545,6 +4792,14 @@ int spi_nor_scan(struct spi_nor *nor) }#endif +#if defined(CONFIG_SPI_FLASH_STMICRO ) +if (JEDEC_MFR (info) ==SNOR_MFR_ST ||JEDEC_MFR (info) ==SNOR_MFR_MICRON ) { + nor->flash_lock = micron_flash_lock; + nor->flash_unlock = micron_flash_unlock; + nor->flash_is_unlocked = micron_is_unlocked; + } +#endif +#ifdef CONFIG_SPI_FLASH_SST /* * sst26 series block protection implementation differs from other
include/linux/mtd/spi-nor.h +7 -0
@@ -166,8 +166,15 @@#define SR_BP0 BIT(2)/* Block protect 0 */ #define SR_BP1 BIT(3)/* Block protect 1 */ #define SR_BP2 BIT(4)/* Block protect 2 */ +#define SR_BP3 BIT(6)/* Block protect 3 */ #define SR_TB BIT(5)/* Top/Bottom protect */ #define SR_SRWD BIT(7)/* SR write protect */ + +#define BPTB_MASK 0x7C/* BP & TB bits mask */ +#define BP_MASK 0x1C/* BP bits mask */ +#define BP_SHIFT 0x2/* BP shift*/ +#define MAX_LOCKBITS 15 +/* Spansion/Cypress specific status bits */ #define SR_E_ERR BIT(5)#define SR_P_ERR BIT(6)
Signature · 1 line
2.27.0
PATCH 27/30 mtd: spi-nor: Add support for locking on Macronix nor flashes
In reply to Tejas BhumkarFrom: Venkatesh Yadav Abbarapu <[email protected]>
Macronix chips implements locking in (power-of-two multiple of) 64K blocks, not as a fraction of the chip's size. Bit 5 in the status register is not a top/bottom select bit, but instead a fourth value bit, allowing locking between 2^0 and 2^14 64K blocks (so up to 1GiB), either from top or bottom.
The top/bottom select is instead done via a bit in the configuration register, which is OTP, so once set to use bottom protect, one cannot use top. On top of that, reading the configuration register uses a different opcode (0x15) than the existing SPINOR_OP_RDCR (0x35).
Here's an attempt to implement locking support for Macronix flash memory devices. This implementation has been tested and confirmed to work on the specific chip used on our boards. Additionally, based on the information from data sheets of various other Macronix chips, suggest they behave in the same way.
Used bottom protect to test the locking and unlocking functionality on the zc1751+dc1 board.
--- drivers/mtd/spi/spi-nor-core.c | 259 +++++++++++++++++++++++++++++++++ include/linux/mtd/spi-nor.h | 3 + 2 files changed, 262 insertions(+)
drivers/mtd/spi/spi-nor-core.c +259 -0
@@ -4581,6 +4581,257 @@ static int micron_is_unlocked(struct spi_nor *nor, loff_t ofs, uint64_t len)return micron_is_unlocked_sr (nor, ofs, len, status); }#endif /* CONFIG_SPI_FLASH_STMICRO */ + +#if defined(CONFIG_SPI_FLASH_MACRONIX ) +/** +* mx_write_sr_cr() - write status and configuration register +* @nor: pointer to a 'struct spi_nor' +* @sr_cr: pointer status and configuration register +* +* Write status Register and configuration register with 2 bytes +* The first byte will be written to the status register, while the +* second byte will be written to the configuration register. +* +* Return: 0 on success, -errno if error occurred. +*/ +static int mx_write_sr_cr (struct spi_nor *nor,u8 *sr_cr) +{ +int ret; + +write_enable (nor); + + ret = nor->write_reg (nor,SPINOR_OP_WRSR , sr_cr,2 ); +if (ret <0 ) { +dev_dbg (nor->dev, +"error while writing configuration register\n" ); +return -EINVAL ; + } + + ret =spi_nor_wait_till_ready (nor); +if (ret) { +dev_dbg (nor->dev, +"timeout while writing configuration register\n" ); +return ret; + } + + ret =write_disable (nor); +if (ret) +return ret; + +return 0 ; +} + +static int mx_read_cr (struct spi_nor *nor) +{ +int ret; +u8 val; + + ret = nor->read_reg (nor,SPINOR_OP_RDCR_MX , &val,1 ); +if (ret <0 ) { +dev_dbg (nor->dev,"error %d reading CR\n" , ret); +return ret; + } + +return val; +} + +/** +* mx_get_locked_range() - get the locked range +* @nor: pointer to a 'struct spi_nor' +* @sr: status register +* @cr: configuration register +* @ofs: flash offset +* @len: length to be locked +* +* Macronix flashes do not work by locking some 1/2^k fraction of the +* flash - instead, the BP{0,1,2,3} bits define a number of protected +* 64K blocks. +*/ +static void mx_get_locked_range (struct spi_nor *nor,u8 sr,u8 cr, +loff_t *ofs,uint64_t *len) +{ +struct mtd_info *mtd = &nor->mtd; +int pow, shift; +u8 mask =SR_BP0 |SR_BP1 |SR_BP2 |SR_BP3_MX ; + + shift =ffs (mask) -1 ; + + pow = ((sr & mask) >> shift) -1 ; +if (pow <0 ) { +/* No protection */ + *ofs =0 ; + *len =0 ; + }else { + *len = (uint64_t )SZ_64K << pow; +if (*len > mtd->size) + *len = mtd->size; +if (cr &CR_TB_MX ) + *ofs =0 ; +else + *ofs = mtd->size - *len; + } +} + +/** +* mx_check_lock_status() - check the locked status +* @nor: pointer to a 'struct spi_nor' +* @ofs: flash offset +* @len: length to be locked +* @sr: status register +* @cr: configuration register +* @locked: locked:1 unlocked:0 value +* +* Return: 1 if the entire region is locked (if @locked is true) or unlocked (if +* @locked is false); 0 otherwise. +*/ +static int mx_check_lock_status (struct spi_nor *nor,loff_t ofs,u64 len, +u8 sr,u8 cr,bool locked) +{ +loff_t lock_offs; +u64 lock_len; + +if (!len) +return 1 ; + +mx_get_locked_range (nor, sr, cr, &lock_offs, &lock_len); + +if (locked) +/* Requested range is a sub-range of locked range */ +return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs); + +/* Requested range does not overlap with locked range */ +return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs); +} + +static int mx_lock_unlock (struct spi_nor *nor,loff_t ofs,uint64_t len,bool lock) +{ +struct mtd_info *mtd = &nor->mtd; +int sr, cr, ret, val; +loff_t lock_len, blocks; +bool can_be_top, can_be_bottom, use_top; +u8 sr_cr[2 ], shift; +u8 mask =SR_BP0 |SR_BP1 |SR_BP2 |SR_BP3_MX ; + + shift =ffs (mask) -1 ; + + sr =read_sr (nor); +if (sr <0 ) +return sr; + + cr =mx_read_cr (nor); +if (cr <0 ) +return cr; + +log_debug ("SPI Protection: %s\n" , (cr &CR_TB_MX ) ?"bottom" :"top" ); + +/* CR_TB is OTP, so we can't use 'top' protection if that is already set. */ + can_be_top = !(cr &CR_TB_MX ); + can_be_bottom = true; + +/* If the whole range is already locked (unlocked), we don't need to do anything */ +if (mx_check_lock_status (nor, ofs, len, sr, cr, lock)) +return 0 ; + +/* To use 'bottom' ('top') protection, everything below us must be locked (unlocked). */ +if (!mx_check_lock_status (nor,0 , ofs, sr, cr, lock)) { +if (lock) + can_be_bottom = false; +else + can_be_top = false; + } + +/* To use 'top' ('bottom') protection, everything above us must be locked (unlocked). */ +if (!mx_check_lock_status (nor, ofs + len, mtd->size - (ofs + len), sr, cr, lock)) { +if (lock) + can_be_top = false; +else + can_be_bottom = false; + } + +if (!can_be_bottom && !can_be_top) +return -EINVAL ; + +/* Prefer top, if both are valid */ + use_top = can_be_top; + +/* lock_len: length of region that should end up locked */ +if (lock) + lock_len = use_top ? mtd->size - ofs : ofs + len; +else + lock_len = use_top ? mtd->size - (ofs + len) : ofs; + +/* lock_len must be a power-of-2 (2^0 .. 2^14) multiple of 64K, or 0 */ +if (lock_len & (SZ_64K -1 )) +return -EINVAL ; + + blocks = lock_len /SZ_64K ; +if ((blocks !=0 && !is_power_of_2 (blocks)) || blocks >1 <<14 ) +return -EINVAL ; + +/* Compute new values of sr/cr */ + val = blocks ?ilog2 (blocks) +1 :0 ; + sr_cr[0 ] = sr & ~mask; + sr_cr[0 ] |= val << shift; +/* +* Disallow further writes if WP pin is asserted, but remove +* that bit if we unlocked the whole chip. +*/ +if (lock_len) + sr_cr[0 ] |=SR_SRWD ; +else + sr_cr[0 ] &= ~SR_SRWD ; + + sr_cr[1 ] = cr | (use_top ?0 :CR_TB_MX ); + +/* Don't bother if they're the same */ +if (sr == sr_cr[0 ] && cr == sr_cr[1 ]) +return 0 ; + + ret =mx_write_sr_cr (nor, sr_cr); +if (ret) +return ret; + +/* Check that the bits got written as expected */ + sr =read_sr (nor); +if (sr <0 ) +return sr; + + cr =mx_read_cr (nor); +if (cr <0 ) +return cr; + +if ((sr & mask) != (sr_cr[0 ] & mask) || + (cr &CR_TB_MX ) != (sr_cr[1 ] &CR_TB_MX )) +return -EIO ; + +return 0 ; +} + +static int mx_lock (struct spi_nor *nor,loff_t ofs,uint64_t len) +{ +return mx_lock_unlock (nor, ofs, len, true); +} + +static int mx_unlock (struct spi_nor *nor,loff_t ofs,uint64_t len) +{ +return mx_lock_unlock (nor, ofs, len, false); +} + +static int mx_is_unlocked (struct spi_nor *nor,loff_t ofs,uint64_t len) +{ +int sr, cr; + + sr =read_sr (nor); +if (sr <0 ) +return sr; + + cr =mx_read_cr (nor); +if (cr <0 ) +return cr; + +return mx_check_lock_status (nor, ofs, len, sr, cr, false); +} +#endif /* CONFIG_SPI_FLASH_MACRONIX */ #endif /* CONFIG_SPI_FLASH_LOCK */ #ifdef CONFIG_SPI_FLASH_SOFT_RESET
@@ -4800,6 +5051,14 @@ int spi_nor_scan(struct spi_nor *nor) }#endif +#if defined(CONFIG_SPI_FLASH_MACRONIX ) +if (JEDEC_MFR (info) ==SNOR_MFR_MACRONIX ) { + nor->flash_lock = mx_lock; + nor->flash_unlock = mx_unlock; + nor->flash_is_unlocked = mx_is_unlocked; + } +#endif +#ifdef CONFIG_SPI_FLASH_SST /* * sst26 series block protection implementation differs from other
include/linux/mtd/spi-nor.h +3 -0
@@ -70,6 +70,7 @@#define SPINOR_OP_RDID 0x9f/* Read JEDEC ID */ #define SPINOR_OP_RDSFDP 0x5a/* Read SFDP */ #define SPINOR_OP_RDCR 0x35/* Read configuration register */ +#define SPINOR_OP_RDCR_MX 0x15/* Read configuration register (Macronix) */ #define SPINOR_OP_RDFSR 0x70/* Read flag status register */ #define SPINOR_OP_CLFSR 0x50/* Clear flag status register */ #define SPINOR_OP_RDEAR 0xc8/* Read Extended Address Register */
@@ -167,6 +168,7 @@#define SR_BP1 BIT(3)/* Block protect 1 */ #define SR_BP2 BIT(4)/* Block protect 2 */ #define SR_BP3 BIT(6)/* Block protect 3 */ +#define SR_BP3_MX BIT(5)/* Block protect 3 (Macronix) */ #define SR_TB BIT(5)/* Top/Bottom protect */ #define SR_SRWD BIT(7)/* SR write protect */
@@ -192,6 +194,7 @@/* Configuration Register bits. */ #define CR_QUAD_EN_SPAN BIT(1)/* Spansion Quad I/O */ +#define CR_TB_MX BIT(3)/* Macronix Top/Bottom protect */ /* Status Register 2 bits. */ #define SR2_QUAD_EN_BIT7 BIT(7)
Signature · 1 line
2.27.0
PATCH 28/30 mtd: spi-nor: Add support for locking on ISSI nor flashes
In reply to Tejas BhumkarFrom: Venkatesh Yadav Abbarapu <[email protected]>
ISSI chips implements locking in (power-of-two multiple of) 64K blocks, not as a fraction of the chip's size. Bit 5 in the status register is not a top/bottom select bit, but instead a fourth value bit, allowing locking between 2^0 and 2^14 64K blocks (so up to 1GiB), either from top or bottom.
The top/bottom select is instead done via a bit in the function register, which is OTP, so once set to use bottom protect, one cannot use top. On top of that, reading the function register uses a opcode SPINOR_OP_RDFR (0x48) and writing to the function register uses the opcode SPINOR_OP_WRFR(0x42).
Here's an attempt at implementing a locking feature for ISSI flash memory devices. This implementation has been rigorously tested and proven to work on the chip used in our boards. Additionally, after examining data sheets for various other ISSI chips, it appears that they follow a similar behavior.
Used "bottom protect" to test the lock and unlock functionality on the Versal Tenzing board. Please note that the Block Protection table is specific to the IS25LP01G, which has a size of 128MB, a sector size of 64KB, and comprises 2048 sectors.
--- drivers/mtd/spi/sf_internal.h | 6 + drivers/mtd/spi/spi-nor-core.c | 340 +++++++++++++++++++++++++++++++++ include/linux/mtd/spi-nor.h | 7 + 3 files changed, 353 insertions(+)
drivers/mtd/spi/sf_internal.h +6 -0
@@ -71,6 +71,12 @@ struct flash_info {#define SPI_NOR_OCTAL_DTR_READ BIT(17)/* Flash supports Octal DTR Read */ #define SPI_NOR_OCTAL_DTR_PP BIT(18)/* Flash supports Octal DTR page program */ #define SPI_NOR_MULTI_DIE BIT(19)/* Flash has multi dies & need split reads*/ +#define SPI_NOR_HAS_BP3 BIT(20)/* Flash SR has block protect bits +* for lock/unlock purpose, few support +* BP0-BP2 while few support BP0-BP3. +* This flag identifies devices that +* support BP3 bit. +*/ };extern const struct flash_info spi_nor_ids[];
drivers/mtd/spi/spi-nor-core.c +340 -0
352 lines not on this page · raw message
include/linux/mtd/spi-nor.h +7 -0
@@ -77,6 +77,8 @@#define SPINOR_OP_WREAR 0xc5/* Write Extended Address Register */ #define SPINOR_OP_SRSTEN 0x66/* Software Reset Enable */ #define SPINOR_OP_SRST 0x99/* Software Reset */ +#define SPINOR_OP_RDFR 0x48/* Read Function register */ +#define SPINOR_OP_WRFR 0x42/* Write Function register 1 byte */ /* 4-byte address opcodes - used on Spansion and some Macronix flashes. */ #define SPINOR_OP_READ_4B 0x13/* Read data bytes (low frequency) */
@@ -169,6 +171,7 @@#define SR_BP2 BIT(4)/* Block protect 2 */ #define SR_BP3 BIT(6)/* Block protect 3 */ #define SR_BP3_MX BIT(5)/* Block protect 3 (Macronix) */ +#define SR_BP3_ISSI BIT(5)/* Block protect 3 (ISSI) */ #define SR_TB BIT(5)/* Top/Bottom protect */ #define SR_SRWD BIT(7)/* SR write protect */
@@ -192,6 +195,9 @@#define FSR_P_ERR BIT(4)/* Program operation status */ #define FSR_PT_ERR BIT(1)/* Protection error bit */ +/* Function register bit */ +#define FR_TB BIT(1)/*ISSI: Top/Bottom protect */ +/* Configuration Register bits. */ #define CR_QUAD_EN_SPAN BIT(1)/* Spansion Quad I/O */ #define CR_TB_MX BIT(3)/* Macronix Top/Bottom protect */
@@ -322,6 +328,7 @@ enum spi_nor_option_flags {SNOR_F_IO_MODE_EN_VOLATILE =BIT (8 ),SNOR_F_HAS_STACKED =BIT (9 ),SNOR_F_HAS_PARALLEL =BIT (10 ), + SNOR_F_HAS_BP3 = BIT(11), };struct spi_nor ;
Signature · 1 line
2.27.0
PATCH 29/30 mtd: spi-nor: Add support for locking on GIGADEVICE nor flashes
In reply to Tejas BhumkarFrom: Venkatesh Yadav Abbarapu <[email protected]>
GIGADEVICE nor flashes provide block protection support using BP0, BP1, BP2, BP3 & TB bits in status register.
BP(Block Protection) bits defines memory to be software protected against PROGRAM or ERASE operations. When one or more block protect bits are set to 1, a designated memory area is protected from PROGRAM and ERASE operations. TB(Top/Bottom) bit determines whether the protected memory area defined by the block protect bits starts from the top or bottom of the memory array.
Used bottom/top protect to test lock/unlock on zc1751+dc1 board.
--- drivers/mtd/spi/spi-nor-core.c | 309 +++++++++++++++++++++++++++++++++ include/linux/mtd/spi-nor.h | 2 + 2 files changed, 311 insertions(+)
drivers/mtd/spi/spi-nor-core.c +309 -0
@@ -5164,6 +5164,307 @@ static int issi_flash_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)return ret; }#endif /* CONFIG_SPI_FLASH_ISSI */ + +#if defined(CONFIG_SPI_FLASH_GIGADEVICE ) +static void giga_get_locked_range (struct spi_nor *nor,u8 sr,loff_t *ofs, +uint64_t *len) +{ +struct mtd_info *mtd = &nor->mtd; +int shift =0 ; +int pow; +u8 mask =SR_BP0 |SR_BP1 |SR_BP2 |SR_BP3_GIGA ; +u32 sector_size; + + sector_size = nor->sector_size; +if (nor->flags &SNOR_F_HAS_PARALLEL ) + sector_size >>=1 ; + + shift =ffs (mask) -1 ; + +if (!(sr & mask)) { +/* No protection */ + *ofs =0 ; + *len =0 ; + }else { + pow = ((sr & mask) >> shift) -1 ; + *len = sector_size << pow; +if (*len > mtd->size) + *len = mtd->size; +/* GIGA device's have top/bottom select bit in status reg */ +if (nor->flags &SNOR_F_HAS_SR_TB && sr &SR_TB_GIGA ) + *ofs =0 ; +else + *ofs = mtd->size - *len; + } +} + +/** +* giga_check_lock_status_sr() - check the status register and return +* the region is locked or unlocked +* @nor: pointer to a 'struct spi_nor'. +* @ofs: offset of the flash +* @len: length to be locked +* @sr: status register +* @locked: locked:1 unlocked:0 value +* +* Return: 1 if the entire region is locked (if @locked is true) or unlocked (if +* @locked is false); 0 otherwise. +*/ +static int giga_check_lock_status_sr (struct spi_nor *nor,loff_t ofs,u64 len, +u8 sr,bool locked) +{ +loff_t lock_offs; +u64 lock_len; + +if (!len) +return 1 ; + +giga_get_locked_range (nor, sr, &lock_offs, &lock_len); +if (locked) +/* Requested range is a sub-range of locked range */ +return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs); + +/* Requested range does not overlap with locked range */ +return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs); +} + +/** +* giga_is_locked_sr() - check if the memory region is locked +* @nor: pointer to a 'struct spi_nor'. +* @ofs: offset of the flash +* @len: length to be locked +* @sr: status register +* +* Check if memory region is locked. +* +* Return: false if region is locked 0 otherwise. +*/ +static int giga_is_locked_sr (struct spi_nor *nor,loff_t ofs,uint64_t len, +u8 sr) +{ +return giga_check_lock_status_sr (nor, ofs, len, sr, true); +} + +/** +* giga_is_unlocked_sr() - check if the memory region is unlocked +* @nor: pointer to a 'struct spi_nor'. +* @ofs: offset of the flash +* @len: length to be locked +* @sr: status register +* +* Check if memory region is unlocked. +* +* Return: false if region is locked 0 otherwise. +*/ +static int giga_is_unlocked_sr (struct spi_nor *nor,loff_t ofs,uint64_t len, +u8 sr) +{ +return giga_check_lock_status_sr (nor, ofs, len, sr, false); +} + +/** +* giga_is_unlocked() - check if the memory region is unlocked +* @nor: pointer to a 'struct spi_nor'. +* @ofs: offset of the flash +* @len: length to be locked +* +* Check if memory region is unlocked +* +* Return: false if region is locked 0 otherwise. +*/ +static int giga_is_unlocked (struct spi_nor *nor,loff_t ofs,uint64_t len) +{ +int sr; + + sr =read_sr (nor); +if (sr <0 ) +return sr; + +return giga_check_lock_status_sr (nor, ofs, len, sr, false); +} + +/** +* giga_nor_select_zone() - Select top area or bottom area to lock/unlock +* @nor: pointer to a 'struct spi_nor'. +* @ofs: offset from which to lock memory. +* @len: number of bytes to unlock. +* @sr: status register +* @tb: pointer to top/bottom bool used in caller function +* @op: zone selection is for lock/unlock operation. 1: lock 0:unlock +* +* Select the top area / bottom area pattern to protect memory blocks. +* +* Return: negative on errors, 0 on success. +*/ +static int giga_nor_select_zone (struct spi_nor *nor,loff_t ofs,uint64_t len, +u8 sr,bool *tb,bool op) +{ +int retval =1 ; +bool can_be_bottom = nor->flags &SNOR_F_HAS_SR_TB , can_be_top = true; + +if (op) { +/* Select for lock zone operation */ + +/* +* If nothing in our range is unlocked, we don't need +* to do anything. +*/ +if (giga_is_locked_sr (nor, ofs, len, sr)) +return 0 ; + +/* +* If anything below us is unlocked, we can't use 'bottom' +* protection. +*/ +if (!giga_is_locked_sr (nor,0 , ofs, sr)) + can_be_bottom = false; + +/* +* If anything above us is unlocked, we can't use 'top' +* protection. +*/ +if (!giga_is_locked_sr (nor, ofs + len, + nor->mtd.size - (ofs + len), sr)) + can_be_top = false; + }else { +/* Select unlock zone */ + +/* +* If nothing in our range is locked, we don't need to +* do anything. +*/ +if (giga_is_unlocked_sr (nor, ofs, len, sr)) +return 0 ; + +/* +* If anything below us is locked, we can't use 'top' +* protection +*/ +if (!giga_is_unlocked_sr (nor,0 , ofs, sr)) + can_be_top = false; + +/* +* If anything above us is locked, we can't use 'bottom' +* protection +*/ +if (!giga_is_unlocked_sr (nor, ofs + len, + nor->mtd.size - (ofs + len), sr)) + can_be_bottom = false; + } + +if (!can_be_bottom && !can_be_top) +return -EINVAL ; + +/* Prefer top, if both are valid */ + *tb = can_be_top; +return retval; +} + +/** +* giga_flash_lock() - set BP[0123] write-protection. +* @nor: pointer to a 'struct spi_nor'. +* @ofs: offset from which to lock memory. +* @len: number of bytes to unlock. +* +* Lock a region of the flash.Implementation is based on stm_lock +* Supports the block protection bits BP{0,1,2,3} in status register +* +* Return: 0 on success, -errno otherwise. +*/ +static int giga_flash_lock (struct spi_nor *nor,loff_t ofs,uint64_t len) +{ +int status_old, status_new, blk_prot; +loff_t lock_len; +u8 pow, ret, shift; +bool use_top = false; +u8 mask =SR_BP0 |SR_BP1 |SR_BP2 |SR_BP3_GIGA ; + + shift =ffs (mask) -1 ; + + status_old =read_sr (nor); +/* if status reg is Write protected don't update bit protection */ +if (status_old &SR_SRWD ) { +dev_err (nor->dev, +"SR is write protected, can't update BP bits...\n" ); +return -EINVAL ; + } + +log_debug ("SPI Protection: %s\n" , (status_old &SR_TB_GIGA ) ?"bottom" :"top" ); + + ret =giga_nor_select_zone (nor, ofs, len, status_old, &use_top,1 ); +/* Older protected blocks include the new requested block's */ +if (ret <=0 ) +return ret; + +/* lock_len: length of region that should end up locked */ +if (use_top) + lock_len = nor->mtd.size - ofs; +else + lock_len = ofs + len; + + pow =order_base_2 (lock_len); + blk_prot = mask & (((pow +1 ) &0xf ) << shift); +if (lock_len <=0 ) { +dev_err (nor->dev,"invalid Length to protect" ); +return -EINVAL ; + } + + status_new = status_old | blk_prot; +if (!use_top) + status_new |=SR_TB_GIGA ; +else + status_new &= ~SR_TB_GIGA ; + +if (status_old == status_new) +return 0 ; + +return write_sr_and_check (nor, status_new, mask); +} + +/** +* giga_flash_unlock() - clear BP[0123] write-protection. +* @nor: pointer to a 'struct spi_nor'. +* @ofs: offset from which to unlock memory. +* @len: number of bytes to unlock. +* +* Bits [2345] of the Status Register are BP[0123]. +* Clear the corresponding BP status bits. +* +* Return: 0 on success, -errno otherwise. +*/ +static int giga_flash_unlock (struct spi_nor *nor,loff_t ofs,uint64_t len) +{ +int ret, val; +u8 mask =SR_BP0 |SR_BP1 |SR_BP2 |SR_BP3_GIGA ; + + val =read_sr (nor); +if (val <0 ) +return val; + +if (!(val & mask)) +return 0 ; + +write_enable (nor); + +write_sr (nor, val & ~mask); + + ret =spi_nor_wait_till_ready (nor); +if (ret) +return ret; + + ret =write_disable (nor); +if (ret) +return ret; + + ret =read_sr (nor); +if (ret >0 && !(ret & mask)) { +dev_info (nor->dev,"block protect bits cleared SR: 0x%x\n" , + ret); + ret =0 ; + } +return ret; +} +#endif /* CONFIG_SPI_FLASH_GIGADEVICE */ #endif /* CONFIG_SPI_FLASH_LOCK */ #ifdef CONFIG_SPI_FLASH_SOFT_RESET
@@ -5399,6 +5700,14 @@ int spi_nor_scan(struct spi_nor *nor) }#endif +#if defined(CONFIG_SPI_FLASH_GIGADEVICE ) +if (JEDEC_MFR (info) ==SNOR_MFR_GIGADEVICE ) { + nor->flash_lock = giga_flash_lock; + nor->flash_unlock = giga_flash_unlock; + nor->flash_is_unlocked = giga_is_unlocked; + } +#endif +#ifdef CONFIG_SPI_FLASH_SST /* * sst26 series block protection implementation differs from other
include/linux/mtd/spi-nor.h +2 -0
@@ -172,6 +172,8 @@#define SR_BP3 BIT(6)/* Block protect 3 */ #define SR_BP3_MX BIT(5)/* Block protect 3 (Macronix) */ #define SR_BP3_ISSI BIT(5)/* Block protect 3 (ISSI) */ +#define SR_TB_GIGA BIT(6)/* Top/Bottom protect (GIGADEVICE)*/ +#define SR_BP3_GIGA BIT(5)/* Block protect 3 (GIGADEVICE) */ #define SR_TB BIT(5)/* Top/Bottom protect */ #define SR_SRWD BIT(7)/* SR write protect */
Signature · 1 line
2.27.0