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 uint64_t 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, uint64_t len,
117 return spi_nor_check_lock_status_sr(nor, ofs, len, sr, true);
120 static bool spi_nor_is_unlocked_sr(struct spi_nor *nor, loff_t ofs,
123 return spi_nor_check_lock_status_sr(nor, ofs, len, sr, false);
127 * Lock a region of the flash. Compatible with ST Micro and similar flash.
128 * Supports the block protection bits BP{0,1,2}/BP{0,1,2,3} in the status
130 * (SR). Does not support these features found in newer SR bitfields:
131 * - SEC: sector/block protect - only handle SEC=0 (block protect)
132 * - CMP: complement protect - only support CMP=0 (range is not complemented)
134 * Support for the following is provided conditionally for some flash:
135 * - TB: top/bottom protect
137 * Sample table portion for 8MB flash (Winbond w25q64fw):
139 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
140 * --------------------------------------------------------------------------
141 * X | X | 0 | 0 | 0 | NONE | NONE
142 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
143 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
144 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
145 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
146 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
147 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
148 * X | X | 1 | 1 | 1 | 8 MB | ALL
149 * ------|-------|-------|-------|-------|---------------|-------------------
150 * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
151 * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
152 * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
153 * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
154 * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
155 * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
157 * Returns negative on errors, 0 on success.
159 static int spi_nor_sr_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
161 struct mtd_info *mtd = &nor->mtd;
163 int ret, status_old, status_new;
164 u8 mask = spi_nor_get_sr_bp_mask(nor);
165 u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
168 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
171 ret = spi_nor_read_sr(nor, nor->bouncebuf);
175 status_old = nor->bouncebuf[0];
177 /* If nothing in our range is unlocked, we don't need to do anything */
178 if (spi_nor_is_locked_sr(nor, ofs, len, status_old))
181 /* If anything below us is unlocked, we can't use 'bottom' protection */
182 if (!spi_nor_is_locked_sr(nor, 0, ofs, status_old))
183 can_be_bottom = false;
185 /* If anything above us is unlocked, we can't use 'top' protection */
186 if (!spi_nor_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
190 if (!can_be_bottom && !can_be_top)
193 /* Prefer top, if both are valid */
194 use_top = can_be_top;
196 /* lock_len: length of region that should end up locked */
198 lock_len = mtd->size - ofs;
200 lock_len = ofs + len;
202 if (lock_len == mtd->size) {
205 min_prot_len = spi_nor_get_min_prot_length_sr(nor);
206 pow = ilog2(lock_len) - ilog2(min_prot_len) + 1;
207 val = pow << SR_BP_SHIFT;
209 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
210 val = (val & ~SR_BP3) | SR_BP3_BIT6;
215 /* Don't "lock" with no region! */
220 status_new = (status_old & ~mask & ~tb_mask) | val;
223 * Disallow further writes if WP# pin is neither left floating nor
224 * wrongly tied to GND (that includes internal pull-downs).
225 * WP# pin hard strapped to GND can be a valid use case.
227 if (!(nor->flags & SNOR_F_NO_WP))
228 status_new |= SR_SRWD;
231 status_new |= tb_mask;
233 /* Don't bother if they're the same */
234 if (status_new == status_old)
237 /* Only modify protection if it will not unlock other areas */
238 if ((status_new & mask) < (status_old & mask))
241 return spi_nor_write_sr_and_check(nor, status_new);
245 * Unlock a region of the flash. See spi_nor_sr_lock() for more info
247 * Returns negative on errors, 0 on success.
249 static int spi_nor_sr_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
251 struct mtd_info *mtd = &nor->mtd;
253 int ret, status_old, status_new;
254 u8 mask = spi_nor_get_sr_bp_mask(nor);
255 u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
258 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
261 ret = spi_nor_read_sr(nor, nor->bouncebuf);
265 status_old = nor->bouncebuf[0];
267 /* If nothing in our range is locked, we don't need to do anything */
268 if (spi_nor_is_unlocked_sr(nor, ofs, len, status_old))
271 /* If anything below us is locked, we can't use 'top' protection */
272 if (!spi_nor_is_unlocked_sr(nor, 0, ofs, status_old))
275 /* If anything above us is locked, we can't use 'bottom' protection */
276 if (!spi_nor_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
278 can_be_bottom = false;
280 if (!can_be_bottom && !can_be_top)
283 /* Prefer top, if both are valid */
284 use_top = can_be_top;
286 /* lock_len: length of region that should remain locked */
288 lock_len = mtd->size - (ofs + len);
293 val = 0; /* fully unlocked */
295 min_prot_len = spi_nor_get_min_prot_length_sr(nor);
296 pow = ilog2(lock_len) - ilog2(min_prot_len) + 1;
297 val = pow << SR_BP_SHIFT;
299 if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
300 val = (val & ~SR_BP3) | SR_BP3_BIT6;
302 /* Some power-of-two sizes are not supported */
307 status_new = (status_old & ~mask & ~tb_mask) | val;
309 /* Don't protect status register if we're fully unlocked */
311 status_new &= ~SR_SRWD;
314 status_new |= tb_mask;
316 /* Don't bother if they're the same */
317 if (status_new == status_old)
320 /* Only modify protection if it will not lock other areas */
321 if ((status_new & mask) > (status_old & mask))
324 return spi_nor_write_sr_and_check(nor, status_new);
328 * Check if a region of the flash is (completely) locked. See spi_nor_sr_lock()
331 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
332 * negative on errors.
334 static int spi_nor_sr_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
338 ret = spi_nor_read_sr(nor, nor->bouncebuf);
342 return spi_nor_is_locked_sr(nor, ofs, len, nor->bouncebuf[0]);
345 static const struct spi_nor_locking_ops spi_nor_sr_locking_ops = {
346 .lock = spi_nor_sr_lock,
347 .unlock = spi_nor_sr_unlock,
348 .is_locked = spi_nor_sr_is_locked,
351 void spi_nor_init_default_locking_ops(struct spi_nor *nor)
353 nor->params->locking_ops = &spi_nor_sr_locking_ops;
356 static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
358 struct spi_nor *nor = mtd_to_spi_nor(mtd);
361 ret = spi_nor_prep_and_lock(nor);
365 ret = nor->params->locking_ops->lock(nor, ofs, len);
367 spi_nor_unlock_and_unprep(nor);
371 static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
373 struct spi_nor *nor = mtd_to_spi_nor(mtd);
376 ret = spi_nor_prep_and_lock(nor);
380 ret = nor->params->locking_ops->unlock(nor, ofs, len);
382 spi_nor_unlock_and_unprep(nor);
386 static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
388 struct spi_nor *nor = mtd_to_spi_nor(mtd);
391 ret = spi_nor_prep_and_lock(nor);
395 ret = nor->params->locking_ops->is_locked(nor, ofs, len);
397 spi_nor_unlock_and_unprep(nor);
402 * spi_nor_try_unlock_all() - Tries to unlock the entire flash memory array.
403 * @nor: pointer to a 'struct spi_nor'.
405 * Some SPI NOR flashes are write protected by default after a power-on reset
406 * cycle, in order to avoid inadvertent writes during power-up. Backward
407 * compatibility imposes to unlock the entire flash memory array at power-up
410 * Unprotecting the entire flash array will fail for boards which are hardware
411 * write-protected. Thus any errors are ignored.
413 void spi_nor_try_unlock_all(struct spi_nor *nor)
417 if (!(nor->flags & SNOR_F_HAS_LOCK))
420 dev_dbg(nor->dev, "Unprotecting entire flash array\n");
422 ret = spi_nor_unlock(&nor->mtd, 0, nor->params->size);
424 dev_dbg(nor->dev, "Failed to unlock the entire flash memory array\n");
427 void spi_nor_set_mtd_locking_ops(struct spi_nor *nor)
429 struct mtd_info *mtd = &nor->mtd;
431 if (!nor->params->locking_ops)
434 mtd->_lock = spi_nor_lock;
435 mtd->_unlock = spi_nor_unlock;
436 mtd->_is_locked = spi_nor_is_locked;