@@ -1536,13 +1536,34 @@ static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
1536
1536
return ret ;
1537
1537
}
1538
1538
1539
+ static u8 spi_nor_get_sr_bp_mask (struct spi_nor * nor )
1540
+ {
1541
+ u8 mask = SR_BP2 | SR_BP1 | SR_BP0 ;
1542
+
1543
+ if (nor -> flags & SNOR_F_HAS_SR_BP3_BIT6 )
1544
+ return mask | SR_BP3_BIT6 ;
1545
+
1546
+ if (nor -> flags & SNOR_F_HAS_4BIT_BP )
1547
+ return mask | SR_BP3 ;
1548
+
1549
+ return mask ;
1550
+ }
1551
+
1552
+ static u8 spi_nor_get_sr_tb_mask (struct spi_nor * nor )
1553
+ {
1554
+ if (nor -> flags & SNOR_F_HAS_SR_TB_BIT6 )
1555
+ return SR_TB_BIT6 ;
1556
+ else
1557
+ return SR_TB_BIT5 ;
1558
+ }
1559
+
1539
1560
static u64 spi_nor_get_min_prot_length_sr (struct spi_nor * nor )
1540
1561
{
1541
1562
unsigned int bp_slots , bp_slots_needed ;
1542
- u8 mask = SR_BP2 | SR_BP1 | SR_BP0 ;
1563
+ u8 mask = spi_nor_get_sr_bp_mask ( nor ) ;
1543
1564
1544
1565
/* Reserved one for "protect none" and one for "protect all". */
1545
- bp_slots = (mask >> SR_BP_SHIFT ) + 1 - 2 ;
1566
+ bp_slots = (1 << hweight8 ( mask )) - 2 ;
1546
1567
bp_slots_needed = ilog2 (nor -> info -> n_sectors );
1547
1568
1548
1569
if (bp_slots_needed > bp_slots )
@@ -1557,12 +1578,14 @@ static void spi_nor_get_locked_range_sr(struct spi_nor *nor, u8 sr, loff_t *ofs,
1557
1578
{
1558
1579
struct mtd_info * mtd = & nor -> mtd ;
1559
1580
u64 min_prot_len ;
1560
- u8 mask = SR_BP2 | SR_BP1 | SR_BP0 ;
1561
- u8 tb_mask = SR_TB_BIT5 ;
1562
- u8 bp = ( sr & mask ) >> SR_BP_SHIFT ;
1581
+ u8 mask = spi_nor_get_sr_bp_mask ( nor ) ;
1582
+ u8 tb_mask = spi_nor_get_sr_tb_mask ( nor ) ;
1583
+ u8 bp , val = sr & mask ;
1563
1584
1564
- if (nor -> flags & SNOR_F_HAS_SR_TB_BIT6 )
1565
- tb_mask = SR_TB_BIT6 ;
1585
+ if (nor -> flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3_BIT6 )
1586
+ val = (val & ~SR_BP3_BIT6 ) | SR_BP3 ;
1587
+
1588
+ bp = val >> SR_BP_SHIFT ;
1566
1589
1567
1590
if (!bp ) {
1568
1591
/* No protection */
@@ -1620,7 +1643,8 @@ static int spi_nor_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
1620
1643
1621
1644
/*
1622
1645
* Lock a region of the flash. Compatible with ST Micro and similar flash.
1623
- * Supports the block protection bits BP{0,1,2} in the status register
1646
+ * Supports the block protection bits BP{0,1,2}/BP{0,1,2,3} in the status
1647
+ * register
1624
1648
* (SR). Does not support these features found in newer SR bitfields:
1625
1649
* - SEC: sector/block protect - only handle SEC=0 (block protect)
1626
1650
* - CMP: complement protect - only support CMP=0 (range is not complemented)
@@ -1655,8 +1679,8 @@ static int spi_nor_sr_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1655
1679
struct mtd_info * mtd = & nor -> mtd ;
1656
1680
u64 min_prot_len ;
1657
1681
int ret , status_old , status_new ;
1658
- u8 mask = SR_BP2 | SR_BP1 | SR_BP0 ;
1659
- u8 tb_mask = SR_TB_BIT5 ;
1682
+ u8 mask = spi_nor_get_sr_bp_mask ( nor ) ;
1683
+ u8 tb_mask = spi_nor_get_sr_tb_mask ( nor ) ;
1660
1684
u8 pow , val ;
1661
1685
loff_t lock_len ;
1662
1686
bool can_be_top = true, can_be_bottom = nor -> flags & SNOR_F_HAS_SR_TB ;
@@ -1693,16 +1717,16 @@ static int spi_nor_sr_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1693
1717
else
1694
1718
lock_len = ofs + len ;
1695
1719
1696
- if (nor -> flags & SNOR_F_HAS_SR_TB_BIT6 )
1697
- tb_mask = SR_TB_BIT6 ;
1698
-
1699
1720
if (lock_len == mtd -> size ) {
1700
1721
val = mask ;
1701
1722
} else {
1702
1723
min_prot_len = spi_nor_get_min_prot_length_sr (nor );
1703
1724
pow = ilog2 (lock_len ) - ilog2 (min_prot_len ) + 1 ;
1704
1725
val = pow << SR_BP_SHIFT ;
1705
1726
1727
+ if (nor -> flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3 )
1728
+ val = (val & ~SR_BP3 ) | SR_BP3_BIT6 ;
1729
+
1706
1730
if (val & ~mask )
1707
1731
return - EINVAL ;
1708
1732
@@ -1740,8 +1764,8 @@ static int spi_nor_sr_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1740
1764
struct mtd_info * mtd = & nor -> mtd ;
1741
1765
u64 min_prot_len ;
1742
1766
int ret , status_old , status_new ;
1743
- u8 mask = SR_BP2 | SR_BP1 | SR_BP0 ;
1744
- u8 tb_mask = SR_TB_BIT5 ;
1767
+ u8 mask = spi_nor_get_sr_bp_mask ( nor ) ;
1768
+ u8 tb_mask = spi_nor_get_sr_tb_mask ( nor ) ;
1745
1769
u8 pow , val ;
1746
1770
loff_t lock_len ;
1747
1771
bool can_be_top = true, can_be_bottom = nor -> flags & SNOR_F_HAS_SR_TB ;
@@ -1778,16 +1802,16 @@ static int spi_nor_sr_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1778
1802
else
1779
1803
lock_len = ofs ;
1780
1804
1781
- if (nor -> flags & SNOR_F_HAS_SR_TB_BIT6 )
1782
- tb_mask = SR_TB_BIT6 ;
1783
-
1784
1805
if (lock_len == 0 ) {
1785
1806
val = 0 ; /* fully unlocked */
1786
1807
} else {
1787
1808
min_prot_len = spi_nor_get_min_prot_length_sr (nor );
1788
1809
pow = ilog2 (lock_len ) - ilog2 (min_prot_len ) + 1 ;
1789
1810
val = pow << SR_BP_SHIFT ;
1790
1811
1812
+ if (nor -> flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3 )
1813
+ val = (val & ~SR_BP3 ) | SR_BP3_BIT6 ;
1814
+
1791
1815
/* Some power-of-two sizes are not supported */
1792
1816
if (val & ~mask )
1793
1817
return - EINVAL ;
@@ -3147,6 +3171,12 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
3147
3171
if (info -> flags & USE_CLSR )
3148
3172
nor -> flags |= SNOR_F_USE_CLSR ;
3149
3173
3174
+ if (info -> flags & SPI_NOR_4BIT_BP ) {
3175
+ nor -> flags |= SNOR_F_HAS_4BIT_BP ;
3176
+ if (info -> flags & SPI_NOR_BP3_SR_BIT6 )
3177
+ nor -> flags |= SNOR_F_HAS_SR_BP3_BIT6 ;
3178
+ }
3179
+
3150
3180
if (info -> flags & SPI_NOR_NO_ERASE )
3151
3181
mtd -> flags |= MTD_NO_ERASE ;
3152
3182
0 commit comments