> >
> > if (!bp) {
> > /* No protection */
> > @@ -1881,7 +1911,7 @@ static int stm_is_unlocked_sr(struct
> > spi_nor
> > *nor, loff_t ofs, uint64_t len,
> >
> > /*
> > * Lock a region of the flash. Compatible with ST Micro and
> > similar
> > flash.
> > - * Supports the block protection bits BP{0,1,2} in the status
> > register
> > + * Supports the block protection bits BP{0,1,2}/BP{0,1,2,3} in
> > the
> > status register
> > * (SR). Does not support these features found in newer SR
> > bitfields:
> > * - SEC: sector/block protect - only handle SEC=0 (block
> > protect)
> > * - CMP: complement protect - only support CMP=0 (range is
> > not
> > complemented)
> > @@ -1889,7 +1919,7 @@ static int stm_is_unlocked_sr(struct
> > spi_nor
> > *nor, loff_t ofs, uint64_t len,
> > * Support for the following is provided conditionally for some
> > flash:
> > * - TB: top/bottom protect
> > *
> > - * Sample table portion for 8MB flash (Winbond w25q64fw):
> > + * Sample table portion for 8MB flash (Winbond w25q64fw / BP0-
> > 2):
> > *
> > * SEC | TB | BP2 | BP1 | BP0 | Prot Length |
> > Protected
> > Portion
> > *
> > -----------------------------------------------------------------
> > ---------
> > @@ -1909,6 +1939,32 @@ static int stm_is_unlocked_sr(struct
> > spi_nor
> > *nor, loff_t ofs, uint64_t len,
> > * 0 | 1 | 1 | 0 | 1 | 2 MB |
> > Lower
> > 1/4
> > * 0 | 1 | 1 | 1 | 0 | 4 MB |
> > Lower
> > 1/2
> > *
> > + * Sample table portion for 64MB flash (Micron n25q512ax3 / BP0-
> > 3):
> > + *
> > + * TB | BP3 | BP2 | BP1 | BP0 | Prot Length |
> > Protected
> > Portion
> > + *
> > -----------------------------------------------------------------
> > ---------
> > + * 0 | 0 | 0 | 0 | 0 | NONE | NONE
> > + * 0 | 0 | 0 | 0 | 1 | 64 KB |
> > Upper
> > 1/1024
> > + * 0 | 0 | 0 | 1 | 0 | 128 KB |
> > Upper
> > 1/512
> > + * 0 | 0 | 0 | 1 | 1 | 256 KB |
> > Upper
> > 1/256
> > + * ...
> > + * 0 | 1 | 0 | 0 | 1 | 16 MB |
> > Upper
> > 1/4
> > + * 0 | 1 | 0 | 1 | 0 | 32 MB |
> > Upper
> > 1/2
> > + * 0 | 1 | 0 | 1 | 1 | 64 MB | ALL
> > + * 0 | 1 | 1 | 0 | 0 | 64 MB | ALL
> > + * ...
> > + *
> > ------|-------|-------|-------|-------|---------------|----------
> > ---------
> > + * 1 | 0 | 0 | 0 | 0 | NONE | NONE
> > + * 1 | 0 | 0 | 0 | 1 | 64 KB |
> > Lower
> > 1/1024
> > + * 1 | 0 | 0 | 1 | 0 | 128 KB |
> > Lower
> > 1/512
> > + * 1 | 0 | 0 | 1 | 1 | 256 KB |
> > Lower
> > 1/256
> > + * ...
> > + * 1 | 1 | 0 | 0 | 1 | 16 MB |
> > Lower
> > 1/4
> > + * 1 | 1 | 0 | 1 | 0 | 32 MB |
> > Lower
> > 1/2
> > + * 1 | 1 | 0 | 1 | 1 | 64 MB | ALL
> > + * 1 | 1 | 1 | 0 | 0 | 64 MB | ALL
> > + * ...
> > + *
> > * Returns negative on errors, 0 on success.
> > */
> > static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t
> > len)
> > @@ -1960,6 +2016,9 @@ static int stm_lock(struct spi_nor *nor,
> > loff_t
> > ofs, uint64_t len)
> > min_prot_len = stm_get_min_prot_length(nor);
> > pow = ilog2(lock_len) - ilog2(min_prot_len) + 1;
> > val = pow << SR_BP_SHIFT;
> > +
> > + if (val & BIT(5) && mask & SR_BP3_BIT6)
> > + val = (val & ~BIT(5)) | BIT(6);
>
> .. and the use just the following here:
>
> if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
> val = (val & ~SR_BP3) | SR_BP3_BIT6;
>
> Ie. just use the "normal case" where all BP bits are next to each
> other
> and then fixup the resulting value and shift the SR3 bit if
> necessary.
> This will be much easier to read.
>
Yes, I agree. It would be better to minimize this kind of conversion
in
one place.
> > }
> >
> > if (val & ~mask)
> > @@ -2042,6 +2101,9 @@ static int stm_unlock(struct spi_nor *nor,
> > loff_t ofs, uint64_t len)
> > pow = ilog2(lock_len) - ilog2(min_prot_len) + 1;
> > val = pow << SR_BP_SHIFT;
> >
> > + if (val & BIT(5) && mask & SR_BP3_BIT6)
> > + val = (val & ~BIT(5)) | BIT(6);
> > +
>
> here would be the other way around:
>
> if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3_BIT6)
> val = (val & ~SR_BP3_BIT6) | SR_BP3;
>
>
> > /* Some power-of-two sizes are not supported */
> > if (val & ~mask)
> > return -EINVAL;
> > @@ -5244,6 +5306,12 @@ int spi_nor_scan(struct spi_nor *nor,
> > const
> > char
> > *name,
> > if (info->flags & USE_CLSR)
> > nor->flags |= SNOR_F_USE_CLSR;
> >
> > + if (info->flags & SPI_NOR_4BIT_BP) {
> > + nor->flags |= SNOR_F_HAS_4BIT_BP;
> > + if (info->flags & SPI_NOR_BP3_SR_BIT6)
> > + nor->flags |= SNOR_F_HAS_SR_BP3_BIT6;
> > + }
> > +
> > if (info->flags & SPI_NOR_NO_ERASE)
> > mtd->flags |= MTD_NO_ERASE;
> >
> > diff --git a/include/linux/mtd/spi-nor.h b/include/linux/mtd/spi-
> > nor.h
> > index de90724f62f1..0190ed21576a 100644
> > --- a/include/linux/mtd/spi-nor.h
> > +++ b/include/linux/mtd/spi-nor.h
> > @@ -129,7 +129,9 @@
> > #define SR_BP1 BIT(3) /* Block protect
> > 1
> > */
> > #define SR_BP2 BIT(4) /* Block protect
> > 2
> > */
> > #define SR_TB_BIT5 BIT(5) /* Top/Bottom
> > protect */
> > +#define SR_BP3_BIT5 BIT(5) /* Block protect
> > 3
> > */
>
> IMHO just SR_BP3. but that is a matter of taste. But it is easier
> on
> the eye in the mask = SR_BP3 | SR_BP2 etc case.
>
SR_BP3 would be a more appropriate name if we could set the case with
all BP bits next to each other as the "normal case."
I'm going to write patches based on latest spi-nor/next including
what
you mentioned.
Thanks,
> -michael
>
> > #define SR_TB_BIT6 BIT(6) /* Top/Bottom
> > protect */
> > +#define SR_BP3_BIT6 BIT(6) /* Block protect
> > 3
> > */
> > #define SR_SRWD BIT(7) /* SR write
> > protect
> > */
> > /* Spansion/Cypress specific status bits */
> > #define SR_E_ERR BIT(5)
> > @@ -240,6 +242,8 @@ enum spi_nor_option_flags {
> > SNOR_F_HAS_16BIT_SR = BIT(9),
> > SNOR_F_NO_READ_CR = BIT(10),
> > SNOR_F_HAS_SR_TB_BIT6 = BIT(11),
> > + SNOR_F_HAS_4BIT_BP = BIT(12),
> > + SNOR_F_HAS_SR_BP3_BIT6 = BIT(13),
> >
> > };