Lines Matching full:nor
3 * SPI NOR Software Write Protection logic.
10 #include <linux/mtd/spi-nor.h>
14 static u8 spi_nor_get_sr_bp_mask(struct spi_nor *nor)
18 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6)
21 if (nor->flags & SNOR_F_HAS_4BIT_BP)
27 static u8 spi_nor_get_sr_tb_mask(struct spi_nor *nor)
29 if (nor->flags & SNOR_F_HAS_SR_TB_BIT6)
35 static u64 spi_nor_get_min_prot_length_sr(struct spi_nor *nor)
42 unsigned int sector_size = nor->info->sector_size ?: SPI_NOR_DEFAULT_SECTOR_SIZE;
43 u64 n_sectors = div_u64(nor->params->size, sector_size);
44 u8 mask = spi_nor_get_sr_bp_mask(nor);
56 static void spi_nor_get_locked_range_sr(struct spi_nor *nor, u8 sr, loff_t *ofs,
60 u8 mask = spi_nor_get_sr_bp_mask(nor);
61 u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
64 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3_BIT6)
76 min_prot_len = spi_nor_get_min_prot_length_sr(nor);
79 if (*len > nor->params->size)
80 *len = nor->params->size;
82 if (nor->flags & SNOR_F_HAS_SR_TB && sr & tb_mask)
85 *ofs = nor->params->size - *len;
92 static bool spi_nor_check_lock_status_sr(struct spi_nor *nor, loff_t ofs,
101 spi_nor_get_locked_range_sr(nor, sr, &lock_offs, &lock_len);
114 static bool spi_nor_is_locked_sr(struct spi_nor *nor, loff_t ofs, u64 len, u8 sr)
116 return spi_nor_check_lock_status_sr(nor, ofs, len, sr, true);
119 static bool spi_nor_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, u64 len,
122 return spi_nor_check_lock_status_sr(nor, ofs, len, sr, false);
158 static int spi_nor_sr_lock(struct spi_nor *nor, loff_t ofs, u64 len)
162 u8 mask = spi_nor_get_sr_bp_mask(nor);
163 u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
166 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
169 ret = spi_nor_read_sr(nor, nor->bouncebuf);
173 status_old = nor->bouncebuf[0];
176 if (spi_nor_is_locked_sr(nor, ofs, len, status_old))
180 if (!spi_nor_is_locked_sr(nor, 0, ofs, status_old))
184 if (!spi_nor_is_locked_sr(nor, ofs + len, nor->params->size - (ofs + len),
196 lock_len = nor->params->size - ofs;
200 if (lock_len == nor->params->size) {
203 min_prot_len = spi_nor_get_min_prot_length_sr(nor);
207 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
221 * Disallow further writes if WP# pin is neither left floating nor
225 if (!(nor->flags & SNOR_F_NO_WP))
239 return spi_nor_write_sr_and_check(nor, status_new);
247 static int spi_nor_sr_unlock(struct spi_nor *nor, loff_t ofs, u64 len)
251 u8 mask = spi_nor_get_sr_bp_mask(nor);
252 u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
255 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
258 ret = spi_nor_read_sr(nor, nor->bouncebuf);
262 status_old = nor->bouncebuf[0];
265 if (spi_nor_is_unlocked_sr(nor, ofs, len, status_old))
269 if (!spi_nor_is_unlocked_sr(nor, 0, ofs, status_old))
273 if (!spi_nor_is_unlocked_sr(nor, ofs + len, nor->params->size - (ofs + len),
285 lock_len = nor->params->size - (ofs + len);
292 min_prot_len = spi_nor_get_min_prot_length_sr(nor);
296 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
321 return spi_nor_write_sr_and_check(nor, status_new);
331 static int spi_nor_sr_is_locked(struct spi_nor *nor, loff_t ofs, u64 len)
335 ret = spi_nor_read_sr(nor, nor->bouncebuf);
339 return spi_nor_is_locked_sr(nor, ofs, len, nor->bouncebuf[0]);
348 void spi_nor_init_default_locking_ops(struct spi_nor *nor)
350 nor->params->locking_ops = &spi_nor_sr_locking_ops;
355 struct spi_nor *nor = mtd_to_spi_nor(mtd);
358 ret = spi_nor_prep_and_lock(nor);
362 ret = nor->params->locking_ops->lock(nor, ofs, len);
364 spi_nor_unlock_and_unprep(nor);
370 struct spi_nor *nor = mtd_to_spi_nor(mtd);
373 ret = spi_nor_prep_and_lock(nor);
377 ret = nor->params->locking_ops->unlock(nor, ofs, len);
379 spi_nor_unlock_and_unprep(nor);
385 struct spi_nor *nor = mtd_to_spi_nor(mtd);
388 ret = spi_nor_prep_and_lock(nor);
392 ret = nor->params->locking_ops->is_locked(nor, ofs, len);
394 spi_nor_unlock_and_unprep(nor);
400 * @nor: pointer to a 'struct spi_nor'.
402 * Some SPI NOR flashes are write protected by default after a power-on reset
410 void spi_nor_try_unlock_all(struct spi_nor *nor)
414 if (!(nor->flags & SNOR_F_HAS_LOCK))
417 dev_dbg(nor->dev, "Unprotecting entire flash array\n");
419 ret = spi_nor_unlock(&nor->mtd, 0, nor->params->size);
421 dev_dbg(nor->dev, "Failed to unlock the entire flash memory array\n");
424 void spi_nor_set_mtd_locking_ops(struct spi_nor *nor)
426 struct mtd_info *mtd = &nor->mtd;
428 if (!nor->params->locking_ops)