Skip to content

Commit 3dd8012

Browse files
mtd: spi-nor: add TB (Top/Bottom) protect support
Some flash support a bit in the status register that inverts protection so that it applies to the bottom of the flash, not the top. This yields additions to the protection range table, as noted in the comments. Because this feature is not universal to all flash that support lock/unlock, control it via a new flag. Signed-off-by: Brian Norris <computersforpeace@gmail.com> Tested-by: Ezequiel Garcia <ezequiel@vanguardiasur.com.ar>
1 parent 76a4707 commit 3dd8012

2 files changed

Lines changed: 65 additions & 7 deletions

File tree

‎drivers/mtd/spi-nor/spi-nor.c‎

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ struct flash_info {
7070
#define SPI_NOR_QUAD_READ BIT(6) /* Flash supports Quad Read */
7171
#define USE_FSR BIT(7) /* use flag status register */
7272
#define SPI_NOR_HAS_LOCK BIT(8) /* Flash supports lock/unlock via SR */
73+
#define SPI_NOR_HAS_TB BIT(9) /*
74+
* Flash SR has Top/Bottom (TB) protect
75+
* bit. Must be used with
76+
* SPI_NOR_HAS_LOCK.
77+
*/
7378
};
7479

7580
#define JEDEC_MFR(info) ((info)->id[0])
@@ -435,7 +440,10 @@ static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
435440
} else {
436441
pow = ((sr & mask) ^ mask) >> shift;
437442
*len = mtd->size >> pow;
438-
*ofs = mtd->size - *len;
443+
if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
444+
*ofs = 0;
445+
else
446+
*ofs = mtd->size - *len;
439447
}
440448
}
441449

@@ -476,12 +484,14 @@ static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
476484

477485
/*
478486
* Lock a region of the flash. Compatible with ST Micro and similar flash.
479-
* Supports only the block protection bits BP{0,1,2} in the status register
487+
* Supports the block protection bits BP{0,1,2} in the status register
480488
* (SR). Does not support these features found in newer SR bitfields:
481-
* - TB: top/bottom protect - only handle TB=0 (top protect)
482489
* - SEC: sector/block protect - only handle SEC=0 (block protect)
483490
* - CMP: complement protect - only support CMP=0 (range is not complemented)
484491
*
492+
* Support for the following is provided conditionally for some flash:
493+
* - TB: top/bottom protect
494+
*
485495
* Sample table portion for 8MB flash (Winbond w25q64fw):
486496
*
487497
* SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
@@ -494,6 +504,13 @@ static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
494504
* 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
495505
* 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
496506
* X | X | 1 | 1 | 1 | 8 MB | ALL
507+
* ------|-------|-------|-------|-------|---------------|-------------------
508+
* 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
509+
* 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
510+
* 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
511+
* 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
512+
* 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
513+
* 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
497514
*
498515
* Returns negative on errors, 0 on success.
499516
*/
@@ -504,6 +521,8 @@ static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
504521
u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
505522
u8 shift = ffs(mask) - 1, pow, val;
506523
loff_t lock_len;
524+
bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
525+
bool use_top;
507526
int ret;
508527

509528
status_old = read_sr(nor);
@@ -514,13 +533,26 @@ static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
514533
if (stm_is_locked_sr(nor, ofs, len, status_old))
515534
return 0;
516535

536+
/* If anything below us is unlocked, we can't use 'bottom' protection */
537+
if (!stm_is_locked_sr(nor, 0, ofs, status_old))
538+
can_be_bottom = false;
539+
517540
/* If anything above us is unlocked, we can't use 'top' protection */
518541
if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
519542
status_old))
543+
can_be_top = false;
544+
545+
if (!can_be_bottom && !can_be_top)
520546
return -EINVAL;
521547

548+
/* Prefer top, if both are valid */
549+
use_top = can_be_top;
550+
522551
/* lock_len: length of region that should end up locked */
523-
lock_len = mtd->size - ofs;
552+
if (use_top)
553+
lock_len = mtd->size - ofs;
554+
else
555+
lock_len = ofs + len;
524556

525557
/*
526558
* Need smallest pow such that:
@@ -539,11 +571,14 @@ static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
539571
if (!(val & mask))
540572
return -EINVAL;
541573

542-
status_new = (status_old & ~mask) | val;
574+
status_new = (status_old & ~mask & ~SR_TB) | val;
543575

544576
/* Disallow further writes if WP pin is asserted */
545577
status_new |= SR_SRWD;
546578

579+
if (!use_top)
580+
status_new |= SR_TB;
581+
547582
/* Don't bother if they're the same */
548583
if (status_new == status_old)
549584
return 0;
@@ -571,6 +606,8 @@ static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
571606
u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
572607
u8 shift = ffs(mask) - 1, pow, val;
573608
loff_t lock_len;
609+
bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
610+
bool use_top;
574611
int ret;
575612

576613
status_old = read_sr(nor);
@@ -583,10 +620,24 @@ static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
583620

584621
/* If anything below us is locked, we can't use 'top' protection */
585622
if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
623+
can_be_top = false;
624+
625+
/* If anything above us is locked, we can't use 'bottom' protection */
626+
if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
627+
status_old))
628+
can_be_bottom = false;
629+
630+
if (!can_be_bottom && !can_be_top)
586631
return -EINVAL;
587632

633+
/* Prefer top, if both are valid */
634+
use_top = can_be_top;
635+
588636
/* lock_len: length of region that should remain locked */
589-
lock_len = mtd->size - (ofs + len);
637+
if (use_top)
638+
lock_len = mtd->size - (ofs + len);
639+
else
640+
lock_len = ofs;
590641

591642
/*
592643
* Need largest pow such that:
@@ -607,12 +658,15 @@ static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
607658
return -EINVAL;
608659
}
609660

610-
status_new = (status_old & ~mask) | val;
661+
status_new = (status_old & ~mask & ~SR_TB) | val;
611662

612663
/* Don't protect status register if we're fully unlocked */
613664
if (lock_len == mtd->size)
614665
status_new &= ~SR_SRWD;
615666

667+
if (!use_top)
668+
status_new |= SR_TB;
669+
616670
/* Don't bother if they're the same */
617671
if (status_new == status_old)
618672
return 0;
@@ -1277,6 +1331,8 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
12771331

12781332
if (info->flags & USE_FSR)
12791333
nor->flags |= SNOR_F_USE_FSR;
1334+
if (info->flags & SPI_NOR_HAS_TB)
1335+
nor->flags |= SNOR_F_HAS_SR_TB;
12801336

12811337
#ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
12821338
/* prefer "small sector" erase if possible */

‎include/linux/mtd/spi-nor.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
#define SR_BP0 BIT(2) /* Block protect 0 */
8686
#define SR_BP1 BIT(3) /* Block protect 1 */
8787
#define SR_BP2 BIT(4) /* Block protect 2 */
88+
#define SR_TB BIT(5) /* Top/Bottom protect */
8889
#define SR_SRWD BIT(7) /* SR write protect */
8990

9091
#define SR_QUAD_EN_MX BIT(6) /* Macronix Quad I/O */
@@ -116,6 +117,7 @@ enum spi_nor_ops {
116117

117118
enum spi_nor_option_flags {
118119
SNOR_F_USE_FSR = BIT(0),
120+
SNOR_F_HAS_SR_TB = BIT(1),
119121
};
120122

121123
/**

0 commit comments

Comments
 (0)