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;
37 u8 mask = spi_nor_get_sr_bp_mask(nor);
39 /* Reserved one for "protect none" and one for "protect all". */
40 bp_slots = (1 << hweight8(mask)) - 2;
41 bp_slots_needed = ilog2(nor->info->n_sectors);
43 if (bp_slots_needed > bp_slots)
44 return nor->info->sector_size <<
45 (bp_slots_needed - bp_slots);
47 return nor->info->sector_size;
50 static void spi_nor_get_locked_range_sr(struct spi_nor *nor, u8 sr, loff_t *ofs,
53 struct mtd_info *mtd = &nor->mtd;
55 u8 mask = spi_nor_get_sr_bp_mask(nor);
56 u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
57 u8 bp, val = sr & mask;
59 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3_BIT6)
60 val = (val & ~SR_BP3_BIT6) | SR_BP3;
62 bp = val >> SR_BP_SHIFT;
71 min_prot_len = spi_nor_get_min_prot_length_sr(nor);
72 *len = min_prot_len << (bp - 1);
77 if (nor->flags & SNOR_F_HAS_SR_TB && sr & tb_mask)
80 *ofs = mtd->size - *len;
84 * Return true if the entire region is locked (if @locked is true) or unlocked
85 * (if @locked is false); false otherwise.
87 static bool spi_nor_check_lock_status_sr(struct spi_nor *nor, loff_t ofs,
88 uint64_t len, u8 sr, bool locked)
90 loff_t lock_offs, lock_offs_max, offs_max;
96 spi_nor_get_locked_range_sr(nor, sr, &lock_offs, &lock_len);
98 lock_offs_max = lock_offs + lock_len;
102 /* Requested range is a sub-range of locked range */
103 return (offs_max <= lock_offs_max) && (ofs >= lock_offs);
105 /* Requested range does not overlap with locked range */
106 return (ofs >= lock_offs_max) || (offs_max <= lock_offs);
109 static bool spi_nor_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
112 return spi_nor_check_lock_status_sr(nor, ofs, len, sr, true);
115 static bool spi_nor_is_unlocked_sr(struct spi_nor *nor, loff_t ofs,
118 return spi_nor_check_lock_status_sr(nor, ofs, len, sr, false);
122 * Lock a region of the flash. Compatible with ST Micro and similar flash.
123 * Supports the block protection bits BP{0,1,2}/BP{0,1,2,3} in the status
125 * (SR). Does not support these features found in newer SR bitfields:
126 * - SEC: sector/block protect - only handle SEC=0 (block protect)
127 * - CMP: complement protect - only support CMP=0 (range is not complemented)
129 * Support for the following is provided conditionally for some flash:
130 * - TB: top/bottom protect
132 * Sample table portion for 8MB flash (Winbond w25q64fw):
134 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
135 * --------------------------------------------------------------------------
136 * X | X | 0 | 0 | 0 | NONE | NONE
137 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
138 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
139 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
140 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
141 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
142 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
143 * X | X | 1 | 1 | 1 | 8 MB | ALL
144 * ------|-------|-------|-------|-------|---------------|-------------------
145 * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
146 * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
147 * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
148 * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
149 * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
150 * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
152 * Returns negative on errors, 0 on success.
154 static int spi_nor_sr_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
156 struct mtd_info *mtd = &nor->mtd;
158 int ret, status_old, status_new;
159 u8 mask = spi_nor_get_sr_bp_mask(nor);
160 u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
163 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
166 ret = spi_nor_read_sr(nor, nor->bouncebuf);
170 status_old = nor->bouncebuf[0];
172 /* If nothing in our range is unlocked, we don't need to do anything */
173 if (spi_nor_is_locked_sr(nor, ofs, len, status_old))
176 /* If anything below us is unlocked, we can't use 'bottom' protection */
177 if (!spi_nor_is_locked_sr(nor, 0, ofs, status_old))
178 can_be_bottom = false;
180 /* If anything above us is unlocked, we can't use 'top' protection */
181 if (!spi_nor_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
185 if (!can_be_bottom && !can_be_top)
188 /* Prefer top, if both are valid */
189 use_top = can_be_top;
191 /* lock_len: length of region that should end up locked */
193 lock_len = mtd->size - ofs;
195 lock_len = ofs + len;
197 if (lock_len == mtd->size) {
200 min_prot_len = spi_nor_get_min_prot_length_sr(nor);
201 pow = ilog2(lock_len) - ilog2(min_prot_len) + 1;
202 val = pow << SR_BP_SHIFT;
204 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
205 val = (val & ~SR_BP3) | SR_BP3_BIT6;
210 /* Don't "lock" with no region! */
215 status_new = (status_old & ~mask & ~tb_mask) | val;
218 * Disallow further writes if WP# pin is neither left floating nor
219 * wrongly tied to GND (that includes internal pull-downs).
220 * WP# pin hard strapped to GND can be a valid use case.
222 if (!(nor->flags & SNOR_F_NO_WP))
223 status_new |= SR_SRWD;
226 status_new |= tb_mask;
228 /* Don't bother if they're the same */
229 if (status_new == status_old)
232 /* Only modify protection if it will not unlock other areas */
233 if ((status_new & mask) < (status_old & mask))
236 return spi_nor_write_sr_and_check(nor, status_new);
240 * Unlock a region of the flash. See spi_nor_sr_lock() for more info
242 * Returns negative on errors, 0 on success.
244 static int spi_nor_sr_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
246 struct mtd_info *mtd = &nor->mtd;
248 int ret, status_old, status_new;
249 u8 mask = spi_nor_get_sr_bp_mask(nor);
250 u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
253 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
256 ret = spi_nor_read_sr(nor, nor->bouncebuf);
260 status_old = nor->bouncebuf[0];
262 /* If nothing in our range is locked, we don't need to do anything */
263 if (spi_nor_is_unlocked_sr(nor, ofs, len, status_old))
266 /* If anything below us is locked, we can't use 'top' protection */
267 if (!spi_nor_is_unlocked_sr(nor, 0, ofs, status_old))
270 /* If anything above us is locked, we can't use 'bottom' protection */
271 if (!spi_nor_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
273 can_be_bottom = false;
275 if (!can_be_bottom && !can_be_top)
278 /* Prefer top, if both are valid */
279 use_top = can_be_top;
281 /* lock_len: length of region that should remain locked */
283 lock_len = mtd->size - (ofs + len);
288 val = 0; /* fully unlocked */
290 min_prot_len = spi_nor_get_min_prot_length_sr(nor);
291 pow = ilog2(lock_len) - ilog2(min_prot_len) + 1;
292 val = pow << SR_BP_SHIFT;
294 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
295 val = (val & ~SR_BP3) | SR_BP3_BIT6;
297 /* Some power-of-two sizes are not supported */
302 status_new = (status_old & ~mask & ~tb_mask) | val;
304 /* Don't protect status register if we're fully unlocked */
306 status_new &= ~SR_SRWD;
309 status_new |= tb_mask;
311 /* Don't bother if they're the same */
312 if (status_new == status_old)
315 /* Only modify protection if it will not lock other areas */
316 if ((status_new & mask) > (status_old & mask))
319 return spi_nor_write_sr_and_check(nor, status_new);
323 * Check if a region of the flash is (completely) locked. See spi_nor_sr_lock()
326 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
327 * negative on errors.
329 static int spi_nor_sr_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
333 ret = spi_nor_read_sr(nor, nor->bouncebuf);
337 return spi_nor_is_locked_sr(nor, ofs, len, nor->bouncebuf[0]);
340 static const struct spi_nor_locking_ops spi_nor_sr_locking_ops = {
341 .lock = spi_nor_sr_lock,
342 .unlock = spi_nor_sr_unlock,
343 .is_locked = spi_nor_sr_is_locked,
346 void spi_nor_init_default_locking_ops(struct spi_nor *nor)
348 nor->params->locking_ops = &spi_nor_sr_locking_ops;
351 static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
353 struct spi_nor *nor = mtd_to_spi_nor(mtd);
356 ret = spi_nor_prep_and_lock(nor);
360 ret = nor->params->locking_ops->lock(nor, ofs, len);
362 spi_nor_unlock_and_unprep(nor);
366 static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
368 struct spi_nor *nor = mtd_to_spi_nor(mtd);
371 ret = spi_nor_prep_and_lock(nor);
375 ret = nor->params->locking_ops->unlock(nor, ofs, len);
377 spi_nor_unlock_and_unprep(nor);
381 static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
383 struct spi_nor *nor = mtd_to_spi_nor(mtd);
386 ret = spi_nor_prep_and_lock(nor);
390 ret = nor->params->locking_ops->is_locked(nor, ofs, len);
392 spi_nor_unlock_and_unprep(nor);
397 * spi_nor_try_unlock_all() - Tries to unlock the entire flash memory array.
398 * @nor: pointer to a 'struct spi_nor'.
400 * Some SPI NOR flashes are write protected by default after a power-on reset
401 * cycle, in order to avoid inadvertent writes during power-up. Backward
402 * compatibility imposes to unlock the entire flash memory array at power-up
405 * Unprotecting the entire flash array will fail for boards which are hardware
406 * write-protected. Thus any errors are ignored.
408 void spi_nor_try_unlock_all(struct spi_nor *nor)
412 if (!(nor->flags & SNOR_F_HAS_LOCK))
415 dev_dbg(nor->dev, "Unprotecting entire flash array\n");
417 ret = spi_nor_unlock(&nor->mtd, 0, nor->params->size);
419 dev_dbg(nor->dev, "Failed to unlock the entire flash memory array\n");
422 void spi_nor_set_mtd_locking_ops(struct spi_nor *nor)
424 struct mtd_info *mtd = &nor->mtd;
426 if (!nor->params->locking_ops)
429 mtd->_lock = spi_nor_lock;
430 mtd->_unlock = spi_nor_unlock;
431 mtd->_is_locked = spi_nor_is_locked;