Lines Matching +full:write +full:- +full:protect
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()
39 /* Reserved one for "protect none" and one for "protect all". */ in spi_nor_get_min_prot_length_sr()
40 bp_slots = (1 << hweight8(mask)) - 2; in spi_nor_get_min_prot_length_sr()
41 bp_slots_needed = ilog2(nor->info->n_sectors); in spi_nor_get_min_prot_length_sr()
44 return nor->info->sector_size << in spi_nor_get_min_prot_length_sr()
45 (bp_slots_needed - bp_slots); in spi_nor_get_min_prot_length_sr()
47 return nor->info->sector_size; in spi_nor_get_min_prot_length_sr()
53 struct mtd_info *mtd = &nor->mtd; in spi_nor_get_locked_range_sr()
59 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3_BIT6) in spi_nor_get_locked_range_sr()
72 *len = min_prot_len << (bp - 1); in spi_nor_get_locked_range_sr()
74 if (*len > mtd->size) in spi_nor_get_locked_range_sr()
75 *len = mtd->size; in spi_nor_get_locked_range_sr()
77 if (nor->flags & SNOR_F_HAS_SR_TB && sr & tb_mask) in spi_nor_get_locked_range_sr()
80 *ofs = mtd->size - *len; in spi_nor_get_locked_range_sr()
102 /* Requested range is a sub-range of locked range */ in spi_nor_check_lock_status_sr()
126 * - SEC: sector/block protect - only handle SEC=0 (block protect)
127 * - CMP: complement protect - only support CMP=0 (range is not complemented)
130 * - TB: top/bottom protect
135 * --------------------------------------------------------------------------
144 * ------|-------|-------|-------|-------|---------------|-------------------
156 struct mtd_info *mtd = &nor->mtd; in spi_nor_sr_lock()
163 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB; in spi_nor_sr_lock()
166 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr_lock()
170 status_old = nor->bouncebuf[0]; in spi_nor_sr_lock()
181 if (!spi_nor_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len), in spi_nor_sr_lock()
186 return -EINVAL; in spi_nor_sr_lock()
193 lock_len = mtd->size - ofs; in spi_nor_sr_lock()
197 if (lock_len == mtd->size) { in spi_nor_sr_lock()
201 pow = ilog2(lock_len) - ilog2(min_prot_len) + 1; in spi_nor_sr_lock()
204 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3) in spi_nor_sr_lock()
208 return -EINVAL; in spi_nor_sr_lock()
212 return -EINVAL; in spi_nor_sr_lock()
229 return -EINVAL; in spi_nor_sr_lock()
241 struct mtd_info *mtd = &nor->mtd; in spi_nor_sr_unlock()
248 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB; in spi_nor_sr_unlock()
251 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr_unlock()
255 status_old = nor->bouncebuf[0]; in spi_nor_sr_unlock()
266 if (!spi_nor_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len), in spi_nor_sr_unlock()
271 return -EINVAL; in spi_nor_sr_unlock()
278 lock_len = mtd->size - (ofs + len); in spi_nor_sr_unlock()
286 pow = ilog2(lock_len) - ilog2(min_prot_len) + 1; in spi_nor_sr_unlock()
289 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3) in spi_nor_sr_unlock()
292 /* Some power-of-two sizes are not supported */ in spi_nor_sr_unlock()
294 return -EINVAL; in spi_nor_sr_unlock()
299 /* Don't protect status register if we're fully unlocked */ in spi_nor_sr_unlock()
312 return -EINVAL; in spi_nor_sr_unlock()
328 ret = spi_nor_read_sr(nor, nor->bouncebuf); in spi_nor_sr_is_locked()
332 return spi_nor_is_locked_sr(nor, ofs, len, nor->bouncebuf[0]); in spi_nor_sr_is_locked()
343 nor->params->locking_ops = &spi_nor_sr_locking_ops; in spi_nor_init_default_locking_ops()
355 ret = nor->params->locking_ops->lock(nor, ofs, len); in spi_nor_lock()
370 ret = nor->params->locking_ops->unlock(nor, ofs, len); in spi_nor_unlock()
385 ret = nor->params->locking_ops->is_locked(nor, ofs, len); in spi_nor_is_locked()
392 * spi_nor_try_unlock_all() - Tries to unlock the entire flash memory array.
395 * Some SPI NOR flashes are write protected by default after a power-on reset
396 * cycle, in order to avoid inadvertent writes during power-up. Backward
397 * compatibility imposes to unlock the entire flash memory array at power-up
401 * write-protected. Thus any errors are ignored.
407 if (!(nor->flags & SNOR_F_HAS_LOCK)) in spi_nor_try_unlock_all()
410 dev_dbg(nor->dev, "Unprotecting entire flash array\n"); in spi_nor_try_unlock_all()
412 ret = spi_nor_unlock(&nor->mtd, 0, nor->params->size); in spi_nor_try_unlock_all()
414 dev_dbg(nor->dev, "Failed to unlock the entire flash memory array\n"); in spi_nor_try_unlock_all()
419 struct mtd_info *mtd = &nor->mtd; in spi_nor_set_mtd_locking_ops()
421 if (!nor->params->locking_ops) in spi_nor_set_mtd_locking_ops()
424 mtd->_lock = spi_nor_lock; in spi_nor_set_mtd_locking_ops()
425 mtd->_unlock = spi_nor_unlock; in spi_nor_set_mtd_locking_ops()
426 mtd->_is_locked = spi_nor_is_locked; in spi_nor_set_mtd_locking_ops()