Skip to content

Commit 5bf0e69

Browse files
mtd: spi-nor: add mtd_is_locked() support
This enables ioctl(MEMISLOCKED). Status can now be reported in the mtdinfo or flash_lock utilities found in mtd-utils. Signed-off-by: Brian Norris <computersforpeace@gmail.com>
1 parent 62593cf commit 5bf0e69

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,24 @@ static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
549549
return write_sr(nor, status_new);
550550
}
551551

552+
/*
553+
* Check if a region of the flash is (completely) locked. See stm_lock() for
554+
* more info.
555+
*
556+
* Returns 1 if entire region is locked, 0 if any portion is unlocked, and
557+
* negative on errors.
558+
*/
559+
static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
560+
{
561+
int status;
562+
563+
status = read_sr(nor);
564+
if (status < 0)
565+
return status;
566+
567+
return stm_is_locked_sr(nor, ofs, len, status);
568+
}
569+
552570
static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
553571
{
554572
struct spi_nor *nor = mtd_to_spi_nor(mtd);
@@ -579,6 +597,21 @@ static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
579597
return ret;
580598
}
581599

600+
static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
601+
{
602+
struct spi_nor *nor = mtd_to_spi_nor(mtd);
603+
int ret;
604+
605+
ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
606+
if (ret)
607+
return ret;
608+
609+
ret = nor->flash_is_locked(nor, ofs, len);
610+
611+
spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
612+
return ret;
613+
}
614+
582615
/* Used when the "_ext_id" is two bytes at most */
583616
#define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
584617
.id = { \
@@ -1186,11 +1219,13 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
11861219
if (JEDEC_MFR(info) == SNOR_MFR_MICRON) {
11871220
nor->flash_lock = stm_lock;
11881221
nor->flash_unlock = stm_unlock;
1222+
nor->flash_is_locked = stm_is_locked;
11891223
}
11901224

1191-
if (nor->flash_lock && nor->flash_unlock) {
1225+
if (nor->flash_lock && nor->flash_unlock && nor->flash_is_locked) {
11921226
mtd->_lock = spi_nor_lock;
11931227
mtd->_unlock = spi_nor_unlock;
1228+
mtd->_is_locked = spi_nor_is_locked;
11941229
}
11951230

11961231
/* sst nor chips use AAI word program */

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ struct mtd_info;
147147
* at the offset @offs
148148
* @flash_lock: [FLASH-SPECIFIC] lock a region of the SPI NOR
149149
* @flash_unlock: [FLASH-SPECIFIC] unlock a region of the SPI NOR
150+
* @flash_is_locked: [FLASH-SPECIFIC] check if a region of the SPI NOR is
151+
* completely locked
150152
* @priv: the private data
151153
*/
152154
struct spi_nor {
@@ -178,6 +180,7 @@ struct spi_nor {
178180

179181
int (*flash_lock)(struct spi_nor *nor, loff_t ofs, uint64_t len);
180182
int (*flash_unlock)(struct spi_nor *nor, loff_t ofs, uint64_t len);
183+
int (*flash_is_locked)(struct spi_nor *nor, loff_t ofs, uint64_t len);
181184

182185
void *priv;
183186
};

0 commit comments

Comments
 (0)