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;
217 /* Disallow further writes if WP pin is asserted */
218 status_new |= SR_SRWD;
221 status_new |= tb_mask;
223 /* Don't bother if they're the same */
224 if (status_new == status_old)
227 /* Only modify protection if it will not unlock other areas */
228 if ((status_new & mask) < (status_old & mask))
231 return spi_nor_write_sr_and_check(nor, status_new);
235 * Unlock a region of the flash. See spi_nor_sr_lock() for more info
237 * Returns negative on errors, 0 on success.
239 static int spi_nor_sr_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
241 struct mtd_info *mtd = &nor->mtd;
243 int ret, status_old, status_new;
244 u8 mask = spi_nor_get_sr_bp_mask(nor);
245 u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
248 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
251 ret = spi_nor_read_sr(nor, nor->bouncebuf);
255 status_old = nor->bouncebuf[0];
257 /* If nothing in our range is locked, we don't need to do anything */
258 if (spi_nor_is_unlocked_sr(nor, ofs, len, status_old))
261 /* If anything below us is locked, we can't use 'top' protection */
262 if (!spi_nor_is_unlocked_sr(nor, 0, ofs, status_old))
265 /* If anything above us is locked, we can't use 'bottom' protection */
266 if (!spi_nor_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
268 can_be_bottom = false;
270 if (!can_be_bottom && !can_be_top)
273 /* Prefer top, if both are valid */
274 use_top = can_be_top;
276 /* lock_len: length of region that should remain locked */
278 lock_len = mtd->size - (ofs + len);
283 val = 0; /* fully unlocked */
285 min_prot_len = spi_nor_get_min_prot_length_sr(nor);
286 pow = ilog2(lock_len) - ilog2(min_prot_len) + 1;
287 val = pow << SR_BP_SHIFT;
289 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
290 val = (val & ~SR_BP3) | SR_BP3_BIT6;
292 /* Some power-of-two sizes are not supported */
297 status_new = (status_old & ~mask & ~tb_mask) | val;
299 /* Don't protect status register if we're fully unlocked */
301 status_new &= ~SR_SRWD;
304 status_new |= tb_mask;
306 /* Don't bother if they're the same */
307 if (status_new == status_old)
310 /* Only modify protection if it will not lock other areas */
311 if ((status_new & mask) > (status_old & mask))
314 return spi_nor_write_sr_and_check(nor, status_new);
318 * Check if a region of the flash is (completely) locked. See spi_nor_sr_lock()
321 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
322 * negative on errors.
324 static int spi_nor_sr_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
328 ret = spi_nor_read_sr(nor, nor->bouncebuf);
332 return spi_nor_is_locked_sr(nor, ofs, len, nor->bouncebuf[0]);
335 static const struct spi_nor_locking_ops spi_nor_sr_locking_ops = {
336 .lock = spi_nor_sr_lock,
337 .unlock = spi_nor_sr_unlock,
338 .is_locked = spi_nor_sr_is_locked,
341 void spi_nor_init_default_locking_ops(struct spi_nor *nor)
343 nor->params->locking_ops = &spi_nor_sr_locking_ops;
346 static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
348 struct spi_nor *nor = mtd_to_spi_nor(mtd);
351 ret = spi_nor_lock_and_prep(nor);
355 ret = nor->params->locking_ops->lock(nor, ofs, len);
357 spi_nor_unlock_and_unprep(nor);
361 static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
363 struct spi_nor *nor = mtd_to_spi_nor(mtd);
366 ret = spi_nor_lock_and_prep(nor);
370 ret = nor->params->locking_ops->unlock(nor, ofs, len);
372 spi_nor_unlock_and_unprep(nor);
376 static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
378 struct spi_nor *nor = mtd_to_spi_nor(mtd);
381 ret = spi_nor_lock_and_prep(nor);
385 ret = nor->params->locking_ops->is_locked(nor, ofs, len);
387 spi_nor_unlock_and_unprep(nor);
392 * spi_nor_try_unlock_all() - Tries to unlock the entire flash memory array.
393 * @nor: pointer to a 'struct spi_nor'.
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
400 * Unprotecting the entire flash array will fail for boards which are hardware
401 * write-protected. Thus any errors are ignored.
403 void spi_nor_try_unlock_all(struct spi_nor *nor)
407 if (!(nor->flags & SNOR_F_HAS_LOCK))
410 dev_dbg(nor->dev, "Unprotecting entire flash array\n");
412 ret = spi_nor_unlock(&nor->mtd, 0, nor->params->size);
414 dev_dbg(nor->dev, "Failed to unlock the entire flash memory array\n");
417 void spi_nor_register_locking_ops(struct spi_nor *nor)
419 struct mtd_info *mtd = &nor->mtd;
421 if (!nor->params->locking_ops)
424 mtd->_lock = spi_nor_lock;
425 mtd->_unlock = spi_nor_unlock;
426 mtd->_is_locked = spi_nor_is_locked;