Lines Matching +full:write +full:- +full:protect

1 // SPDX-License-Identifier: GPL-2.0
3 * SPI NOR Software Write Protection logic.
10 #include <linux/mtd/spi-nor.h>
18 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6) in spi_nor_get_sr_bp_mask()
21 if (nor->flags & SNOR_F_HAS_4BIT_BP) in spi_nor_get_sr_bp_mask()
29 if (nor->flags & SNOR_F_HAS_SR_TB_BIT6) in spi_nor_get_sr_tb_mask()
42 unsigned int sector_size = nor->info->sector_size ?: SPI_NOR_DEFAULT_SECTOR_SIZE; in spi_nor_get_min_prot_length_sr()
43 u64 n_sectors = div_u64(nor->params->size, sector_size); in spi_nor_get_min_prot_length_sr()
46 /* Reserved one for "protect none" and one for "protect all". */ in spi_nor_get_min_prot_length_sr()
47 bp_slots = (1 << hweight8(mask)) - 2; in spi_nor_get_min_prot_length_sr()
51 return sector_size << (bp_slots_needed - bp_slots); in spi_nor_get_min_prot_length_sr()
59 struct mtd_info *mtd = &nor->mtd; in spi_nor_get_locked_range_sr()
65 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3_BIT6) in spi_nor_get_locked_range_sr()
78 *len = min_prot_len << (bp - 1); in spi_nor_get_locked_range_sr()
80 if (*len > mtd->size) in spi_nor_get_locked_range_sr()
81 *len = mtd->size; in spi_nor_get_locked_range_sr()
83 if (nor->flags & SNOR_F_HAS_SR_TB && sr & tb_mask) in spi_nor_get_locked_range_sr()
86 *ofs = mtd->size - *len; in spi_nor_get_locked_range_sr()
108 /* Requested range is a sub-range of locked range */ in spi_nor_check_lock_status_sr()
131 * - SEC: sector/block protect - only handle SEC=0 (block protect)
132 * - CMP: complement protect - only support CMP=0 (range is not complemented)
135 * - TB: top/bottom protect
140 * --------------------------------------------------------------------------
149 * ------|-------|-------|-------|-------|---------------|-------------------
161 struct mtd_info *mtd = &nor->mtd; in spi_nor_sr_lock()
168 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB; in spi_nor_sr_lock()
171 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr_lock()
175 status_old = nor->bouncebuf[0]; in spi_nor_sr_lock()
186 if (!spi_nor_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len), in spi_nor_sr_lock()
191 return -EINVAL; in spi_nor_sr_lock()
198 lock_len = mtd->size - ofs; in spi_nor_sr_lock()
202 if (lock_len == mtd->size) { in spi_nor_sr_lock()
206 pow = ilog2(lock_len) - ilog2(min_prot_len) + 1; in spi_nor_sr_lock()
209 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3) in spi_nor_sr_lock()
213 return -EINVAL; in spi_nor_sr_lock()
217 return -EINVAL; in spi_nor_sr_lock()
224 * wrongly tied to GND (that includes internal pull-downs). in spi_nor_sr_lock()
227 if (!(nor->flags & SNOR_F_NO_WP)) in spi_nor_sr_lock()
239 return -EINVAL; in spi_nor_sr_lock()
251 struct mtd_info *mtd = &nor->mtd; in spi_nor_sr_unlock()
258 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB; in spi_nor_sr_unlock()
261 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr_unlock()
265 status_old = nor->bouncebuf[0]; in spi_nor_sr_unlock()
276 if (!spi_nor_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len), in spi_nor_sr_unlock()
281 return -EINVAL; in spi_nor_sr_unlock()
288 lock_len = mtd->size - (ofs + len); in spi_nor_sr_unlock()
296 pow = ilog2(lock_len) - ilog2(min_prot_len) + 1; in spi_nor_sr_unlock()
299 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3) in spi_nor_sr_unlock()
302 /* Some power-of-two sizes are not supported */ in spi_nor_sr_unlock()
304 return -EINVAL; in spi_nor_sr_unlock()
309 /* Don't protect status register if we're fully unlocked */ in spi_nor_sr_unlock()
322 return -EINVAL; in spi_nor_sr_unlock()
338 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr_is_locked()
342 return spi_nor_is_locked_sr(nor, ofs, len, nor->bouncebuf[0]); in spi_nor_sr_is_locked()
353 nor->params->locking_ops = &spi_nor_sr_locking_ops; in spi_nor_init_default_locking_ops()
365 ret = nor->params->locking_ops->lock(nor, ofs, len); in spi_nor_lock()
380 ret = nor->params->locking_ops->unlock(nor, ofs, len); in spi_nor_unlock()
395 ret = nor->params->locking_ops->is_locked(nor, ofs, len); in spi_nor_is_locked()
402 * spi_nor_try_unlock_all() - Tries to unlock the entire flash memory array.
405 * Some SPI NOR flashes are write protected by default after a power-on reset
406 * cycle, in order to avoid inadvertent writes during power-up. Backward
407 * compatibility imposes to unlock the entire flash memory array at power-up
411 * write-protected. Thus any errors are ignored.
417 if (!(nor->flags & SNOR_F_HAS_LOCK)) in spi_nor_try_unlock_all()
420 dev_dbg(nor->dev, "Unprotecting entire flash array\n"); in spi_nor_try_unlock_all()
422 ret = spi_nor_unlock(&nor->mtd, 0, nor->params->size); in spi_nor_try_unlock_all()
424 dev_dbg(nor->dev, "Failed to unlock the entire flash memory array\n"); in spi_nor_try_unlock_all()
429 struct mtd_info *mtd = &nor->mtd; in spi_nor_set_mtd_locking_ops()
431 if (!nor->params->locking_ops) in spi_nor_set_mtd_locking_ops()
434 mtd->_lock = spi_nor_lock; in spi_nor_set_mtd_locking_ops()
435 mtd->_unlock = spi_nor_unlock; in spi_nor_set_mtd_locking_ops()
436 mtd->_is_locked = spi_nor_is_locked; in spi_nor_set_mtd_locking_ops()