1 // SPDX-License-Identifier: GPL-2.0
3 * SPI NOR Software Write Protection logic.
5 * Copyright (C) 2005, Intec Automation Inc.
6 * Copyright (C) 2014, Freescale Semiconductor, Inc.
8 #include <linux/mtd/mtd.h>
9 #include <linux/mtd/spi-nor.h>
13 static u8 spi_nor_get_sr_bp_mask(struct spi_nor *nor)
15 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
17 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6)
18 return mask | SR_BP3_BIT6;
20 if (nor->flags & SNOR_F_HAS_4BIT_BP)
26 static u8 spi_nor_get_sr_tb_mask(struct spi_nor *nor)
28 if (nor->flags & SNOR_F_HAS_SR_TB_BIT6)
34 static u64 spi_nor_get_min_prot_length_sr(struct spi_nor *nor)
36 unsigned int bp_slots, bp_slots_needed;
38 * sector_size will eventually be replaced with the max erase size of
39 * the flash. For now, we need to have that ugly default.
41 unsigned int sector_size = nor->info->sector_size ?: SPI_NOR_DEFAULT_SECTOR_SIZE;
42 u64 n_sectors = div_u64(nor->params->size, sector_size);
43 u8 mask = spi_nor_get_sr_bp_mask(nor);
45 /* Reserved one for "protect none" and one for "protect all". */
46 bp_slots = (1 << hweight8(mask)) - 2;
47 bp_slots_needed = ilog2(n_sectors);
49 if (bp_slots_needed > bp_slots)
50 return sector_size << (bp_slots_needed - bp_slots);
55 static void spi_nor_get_locked_range_sr(struct spi_nor *nor, u8 sr, loff_t *ofs,
58 struct mtd_info *mtd = &nor->mtd;
60 u8 mask = spi_nor_get_sr_bp_mask(nor);
61 u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
62 u8 bp, val = sr & mask;
64 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3_BIT6)
65 val = (val & ~SR_BP3_BIT6) | SR_BP3;
67 bp = val >> SR_BP_SHIFT;
76 min_prot_len = spi_nor_get_min_prot_length_sr(nor);
77 *len = min_prot_len << (bp - 1);
82 if (nor->flags & SNOR_F_HAS_SR_TB && sr & tb_mask)
85 *ofs = mtd->size - *len;
89 * Return true if the entire region is locked (if @locked is true) or unlocked
90 * (if @locked is false); false otherwise.
92 static bool spi_nor_check_lock_status_sr(struct spi_nor *nor, loff_t ofs,
93 u64 len, u8 sr, bool locked)
95 loff_t lock_offs, lock_offs_max, offs_max;
101 spi_nor_get_locked_range_sr(nor, sr, &lock_offs, &lock_len);
103 lock_offs_max = lock_offs + lock_len;
104 offs_max = ofs + len;
107 /* Requested range is a sub-range of locked range */
108 return (offs_max <= lock_offs_max) && (ofs >= lock_offs);
110 /* Requested range does not overlap with locked range */
111 return (ofs >= lock_offs_max) || (offs_max <= lock_offs);
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);
126 * Lock a region of the flash. Compatible with ST Micro and similar flash.
127 * Supports the block protection bits BP{0,1,2}/BP{0,1,2,3} in the status
129 * (SR). Does not support these features found in newer SR bitfields:
130 * - SEC: sector/block protect - only handle SEC=0 (block protect)
131 * - CMP: complement protect - only support CMP=0 (range is not complemented)
133 * Support for the following is provided conditionally for some flash:
134 * - TB: top/bottom protect
136 * Sample table portion for 8MB flash (Winbond w25q64fw):
138 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
139 * --------------------------------------------------------------------------
140 * X | X | 0 | 0 | 0 | NONE | NONE
141 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
142 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
143 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
144 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
145 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
146 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
147 * X | X | 1 | 1 | 1 | 8 MB | ALL
148 * ------|-------|-------|-------|-------|---------------|-------------------
149 * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
150 * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
151 * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
152 * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
153 * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
154 * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
156 * Returns negative on errors, 0 on success.
158 static int spi_nor_sr_lock(struct spi_nor *nor, loff_t ofs, u64 len)
160 struct mtd_info *mtd = &nor->mtd;
162 int ret, status_old, status_new;
163 u8 mask = spi_nor_get_sr_bp_mask(nor);
164 u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
167 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
170 ret = spi_nor_read_sr(nor, nor->bouncebuf);
174 status_old = nor->bouncebuf[0];
176 /* If nothing in our range is unlocked, we don't need to do anything */
177 if (spi_nor_is_locked_sr(nor, ofs, len, status_old))
180 /* If anything below us is unlocked, we can't use 'bottom' protection */
181 if (!spi_nor_is_locked_sr(nor, 0, ofs, status_old))
182 can_be_bottom = false;
184 /* If anything above us is unlocked, we can't use 'top' protection */
185 if (!spi_nor_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
189 if (!can_be_bottom && !can_be_top)
192 /* Prefer top, if both are valid */
193 use_top = can_be_top;
195 /* lock_len: length of region that should end up locked */
197 lock_len = mtd->size - ofs;
199 lock_len = ofs + len;
201 if (lock_len == mtd->size) {
204 min_prot_len = spi_nor_get_min_prot_length_sr(nor);
205 pow = ilog2(lock_len) - ilog2(min_prot_len) + 1;
206 val = pow << SR_BP_SHIFT;
208 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
209 val = (val & ~SR_BP3) | SR_BP3_BIT6;
214 /* Don't "lock" with no region! */
219 status_new = (status_old & ~mask & ~tb_mask) | val;
222 * Disallow further writes if WP# pin is neither left floating nor
223 * wrongly tied to GND (that includes internal pull-downs).
224 * WP# pin hard strapped to GND can be a valid use case.
226 if (!(nor->flags & SNOR_F_NO_WP))
227 status_new |= SR_SRWD;
230 status_new |= tb_mask;
232 /* Don't bother if they're the same */
233 if (status_new == status_old)
236 /* Only modify protection if it will not unlock other areas */
237 if ((status_new & mask) < (status_old & mask))
240 return spi_nor_write_sr_and_check(nor, status_new);
244 * Unlock a region of the flash. See spi_nor_sr_lock() for more info
246 * Returns negative on errors, 0 on success.
248 static int spi_nor_sr_unlock(struct spi_nor *nor, loff_t ofs, u64 len)
250 struct mtd_info *mtd = &nor->mtd;
252 int ret, status_old, status_new;
253 u8 mask = spi_nor_get_sr_bp_mask(nor);
254 u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
257 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
260 ret = spi_nor_read_sr(nor, nor->bouncebuf);
264 status_old = nor->bouncebuf[0];
266 /* If nothing in our range is locked, we don't need to do anything */
267 if (spi_nor_is_unlocked_sr(nor, ofs, len, status_old))
270 /* If anything below us is locked, we can't use 'top' protection */
271 if (!spi_nor_is_unlocked_sr(nor, 0, ofs, status_old))
274 /* If anything above us is locked, we can't use 'bottom' protection */
275 if (!spi_nor_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
277 can_be_bottom = false;
279 if (!can_be_bottom && !can_be_top)
282 /* Prefer top, if both are valid */
283 use_top = can_be_top;
285 /* lock_len: length of region that should remain locked */
287 lock_len = mtd->size - (ofs + len);
292 val = 0; /* fully unlocked */
294 min_prot_len = spi_nor_get_min_prot_length_sr(nor);
295 pow = ilog2(lock_len) - ilog2(min_prot_len) + 1;
296 val = pow << SR_BP_SHIFT;
298 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
299 val = (val & ~SR_BP3) | SR_BP3_BIT6;
301 /* Some power-of-two sizes are not supported */
306 status_new = (status_old & ~mask & ~tb_mask) | val;
308 /* Don't protect status register if we're fully unlocked */
310 status_new &= ~SR_SRWD;
313 status_new |= tb_mask;
315 /* Don't bother if they're the same */
316 if (status_new == status_old)
319 /* Only modify protection if it will not lock other areas */
320 if ((status_new & mask) > (status_old & mask))
323 return spi_nor_write_sr_and_check(nor, status_new);
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
331 * negative on errors.
333 static int spi_nor_sr_is_locked(struct spi_nor *nor, loff_t ofs, u64 len)
337 ret = spi_nor_read_sr(nor, nor->bouncebuf);
341 return spi_nor_is_locked_sr(nor, ofs, len, nor->bouncebuf[0]);
344 static const struct spi_nor_locking_ops spi_nor_sr_locking_ops = {
345 .lock = spi_nor_sr_lock,
346 .unlock = spi_nor_sr_unlock,
347 .is_locked = spi_nor_sr_is_locked,
350 void spi_nor_init_default_locking_ops(struct spi_nor *nor)
352 nor->params->locking_ops = &spi_nor_sr_locking_ops;
355 static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, u64 len)
357 struct spi_nor *nor = mtd_to_spi_nor(mtd);
360 ret = spi_nor_prep_and_lock(nor);
364 ret = nor->params->locking_ops->lock(nor, ofs, len);
366 spi_nor_unlock_and_unprep(nor);
370 static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, u64 len)
372 struct spi_nor *nor = mtd_to_spi_nor(mtd);
375 ret = spi_nor_prep_and_lock(nor);
379 ret = nor->params->locking_ops->unlock(nor, ofs, len);
381 spi_nor_unlock_and_unprep(nor);
385 static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, u64 len)
387 struct spi_nor *nor = mtd_to_spi_nor(mtd);
390 ret = spi_nor_prep_and_lock(nor);
394 ret = nor->params->locking_ops->is_locked(nor, ofs, len);
396 spi_nor_unlock_and_unprep(nor);
401 * spi_nor_try_unlock_all() - Tries to unlock the entire flash memory array.
402 * @nor: pointer to a 'struct spi_nor'.
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
409 * Unprotecting the entire flash array will fail for boards which are hardware
410 * write-protected. Thus any errors are ignored.
412 void spi_nor_try_unlock_all(struct spi_nor *nor)
416 if (!(nor->flags & SNOR_F_HAS_LOCK))
419 dev_dbg(nor->dev, "Unprotecting entire flash array\n");
421 ret = spi_nor_unlock(&nor->mtd, 0, nor->params->size);
423 dev_dbg(nor->dev, "Failed to unlock the entire flash memory array\n");
426 void spi_nor_set_mtd_locking_ops(struct spi_nor *nor)
428 struct mtd_info *mtd = &nor->mtd;
430 if (!nor->params->locking_ops)
433 mtd->_lock = spi_nor_lock;
434 mtd->_unlock = spi_nor_unlock;
435 mtd->_is_locked = spi_nor_is_locked;