Lines Matching +full:software +full:- +full:locked
1 // SPDX-License-Identifier: GPL-2.0
3 * SPI NOR Software Write Protection logic.
9 #include <linux/mtd/spi-nor.h>
17 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6) in spi_nor_get_sr_bp_mask()
20 if (nor->flags & SNOR_F_HAS_4BIT_BP) in spi_nor_get_sr_bp_mask()
28 if (nor->flags & SNOR_F_HAS_SR_TB_BIT6) in spi_nor_get_sr_tb_mask()
41 unsigned int sector_size = nor->info->sector_size ?: SPI_NOR_DEFAULT_SECTOR_SIZE; in spi_nor_get_min_prot_length_sr()
42 u64 n_sectors = div_u64(nor->params->size, sector_size); in spi_nor_get_min_prot_length_sr()
46 bp_slots = (1 << hweight8(mask)) - 2; in spi_nor_get_min_prot_length_sr()
50 return sector_size << (bp_slots_needed - bp_slots); in spi_nor_get_min_prot_length_sr()
58 struct mtd_info *mtd = &nor->mtd; in spi_nor_get_locked_range_sr()
64 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3_BIT6) in spi_nor_get_locked_range_sr()
77 *len = min_prot_len << (bp - 1); in spi_nor_get_locked_range_sr()
79 if (*len > mtd->size) in spi_nor_get_locked_range_sr()
80 *len = mtd->size; in spi_nor_get_locked_range_sr()
82 if (nor->flags & SNOR_F_HAS_SR_TB && sr & tb_mask) in spi_nor_get_locked_range_sr()
85 *ofs = mtd->size - *len; in spi_nor_get_locked_range_sr()
89 * Return true if the entire region is locked (if @locked is true) or unlocked
90 * (if @locked is false); false otherwise.
93 u64 len, u8 sr, bool locked) in spi_nor_check_lock_status_sr() argument
106 if (locked) in spi_nor_check_lock_status_sr()
107 /* Requested range is a sub-range of locked range */ in spi_nor_check_lock_status_sr()
110 /* Requested range does not overlap with locked range */ in spi_nor_check_lock_status_sr()
130 * - SEC: sector/block protect - only handle SEC=0 (block protect)
131 * - CMP: complement protect - only support CMP=0 (range is not complemented)
134 * - TB: top/bottom protect
139 * --------------------------------------------------------------------------
148 * ------|-------|-------|-------|-------|---------------|-------------------
160 struct mtd_info *mtd = &nor->mtd; in spi_nor_sr_lock()
167 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB; in spi_nor_sr_lock()
170 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr_lock()
174 status_old = nor->bouncebuf[0]; in spi_nor_sr_lock()
185 if (!spi_nor_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len), in spi_nor_sr_lock()
190 return -EINVAL; in spi_nor_sr_lock()
195 /* lock_len: length of region that should end up locked */ in spi_nor_sr_lock()
197 lock_len = mtd->size - ofs; in spi_nor_sr_lock()
201 if (lock_len == mtd->size) { in spi_nor_sr_lock()
205 pow = ilog2(lock_len) - ilog2(min_prot_len) + 1; in spi_nor_sr_lock()
208 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3) in spi_nor_sr_lock()
212 return -EINVAL; in spi_nor_sr_lock()
216 return -EINVAL; in spi_nor_sr_lock()
223 * wrongly tied to GND (that includes internal pull-downs). in spi_nor_sr_lock()
226 if (!(nor->flags & SNOR_F_NO_WP)) in spi_nor_sr_lock()
238 return -EINVAL; in spi_nor_sr_lock()
250 struct mtd_info *mtd = &nor->mtd; in spi_nor_sr_unlock()
257 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB; in spi_nor_sr_unlock()
260 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr_unlock()
264 status_old = nor->bouncebuf[0]; in spi_nor_sr_unlock()
266 /* If nothing in our range is locked, we don't need to do anything */ in spi_nor_sr_unlock()
270 /* If anything below us is locked, we can't use 'top' protection */ in spi_nor_sr_unlock()
274 /* If anything above us is locked, we can't use 'bottom' protection */ in spi_nor_sr_unlock()
275 if (!spi_nor_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len), in spi_nor_sr_unlock()
280 return -EINVAL; in spi_nor_sr_unlock()
285 /* lock_len: length of region that should remain locked */ in spi_nor_sr_unlock()
287 lock_len = mtd->size - (ofs + len); in spi_nor_sr_unlock()
295 pow = ilog2(lock_len) - ilog2(min_prot_len) + 1; in spi_nor_sr_unlock()
298 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3) in spi_nor_sr_unlock()
301 /* Some power-of-two sizes are not supported */ in spi_nor_sr_unlock()
303 return -EINVAL; in spi_nor_sr_unlock()
321 return -EINVAL; in spi_nor_sr_unlock()
327 * Check if a region of the flash is (completely) locked. See spi_nor_sr_lock()
330 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
337 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr_is_locked()
341 return spi_nor_is_locked_sr(nor, ofs, len, nor->bouncebuf[0]); in spi_nor_sr_is_locked()
352 nor->params->locking_ops = &spi_nor_sr_locking_ops; in spi_nor_init_default_locking_ops()
364 ret = nor->params->locking_ops->lock(nor, ofs, len); in spi_nor_lock()
379 ret = nor->params->locking_ops->unlock(nor, ofs, len); in spi_nor_unlock()
394 ret = nor->params->locking_ops->is_locked(nor, ofs, len); in spi_nor_is_locked()
401 * spi_nor_try_unlock_all() - Tries to unlock the entire flash memory array.
404 * Some SPI NOR flashes are write protected by default after a power-on reset
405 * cycle, in order to avoid inadvertent writes during power-up. Backward
406 * compatibility imposes to unlock the entire flash memory array at power-up
410 * write-protected. Thus any errors are ignored.
416 if (!(nor->flags & SNOR_F_HAS_LOCK)) in spi_nor_try_unlock_all()
419 dev_dbg(nor->dev, "Unprotecting entire flash array\n"); in spi_nor_try_unlock_all()
421 ret = spi_nor_unlock(&nor->mtd, 0, nor->params->size); in spi_nor_try_unlock_all()
423 dev_dbg(nor->dev, "Failed to unlock the entire flash memory array\n"); in spi_nor_try_unlock_all()
428 struct mtd_info *mtd = &nor->mtd; in spi_nor_set_mtd_locking_ops()
430 if (!nor->params->locking_ops) in spi_nor_set_mtd_locking_ops()
433 mtd->_lock = spi_nor_lock; in spi_nor_set_mtd_locking_ops()
434 mtd->_unlock = spi_nor_unlock; in spi_nor_set_mtd_locking_ops()
435 mtd->_is_locked = spi_nor_is_locked; in spi_nor_set_mtd_locking_ops()