xref: /linux/drivers/mtd/spi-nor/swp.c (revision 45bd2d77fbedec862204bb5c0fcaba2b7fa5fb56)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * SPI NOR Software Write Protection logic.
4  *
5  * Copyright (C) 2005, Intec Automation Inc.
6  * Copyright (C) 2014, Freescale Semiconductor, Inc.
7  */
8 #include <linux/math64.h>
9 #include <linux/mtd/mtd.h>
10 #include <linux/mtd/spi-nor.h>
11 
12 #include "core.h"
13 
14 static u8 spi_nor_get_sr_bp_mask(struct spi_nor *nor)
15 {
16 	u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
17 
18 	if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6)
19 		return mask | SR_BP3_BIT6;
20 
21 	if (nor->flags & SNOR_F_HAS_4BIT_BP)
22 		return mask | SR_BP3;
23 
24 	return mask;
25 }
26 
27 static u8 spi_nor_get_sr_tb_mask(struct spi_nor *nor)
28 {
29 	if (nor->flags & SNOR_F_HAS_SR_TB_BIT6)
30 		return SR_TB_BIT6;
31 	else if (nor->flags & SNOR_F_HAS_SR_TB)
32 		return SR_TB_BIT5;
33 	else
34 		return 0;
35 }
36 
37 static u64 spi_nor_get_min_prot_length_sr(struct spi_nor *nor)
38 {
39 	unsigned int bp_slots, bp_slots_needed;
40 	/*
41 	 * sector_size will eventually be replaced with the max erase size of
42 	 * the flash. For now, we need to have that ugly default.
43 	 */
44 	unsigned int sector_size = nor->info->sector_size ?: SPI_NOR_DEFAULT_SECTOR_SIZE;
45 	u64 n_sectors = div_u64(nor->params->size, sector_size);
46 	u8 mask = spi_nor_get_sr_bp_mask(nor);
47 
48 	/* Reserved one for "protect none" and one for "protect all". */
49 	bp_slots = (1 << hweight8(mask)) - 2;
50 	bp_slots_needed = ilog2(n_sectors);
51 
52 	if (bp_slots_needed > bp_slots)
53 		return sector_size << (bp_slots_needed - bp_slots);
54 	else
55 		return sector_size;
56 }
57 
58 static void spi_nor_get_locked_range_sr(struct spi_nor *nor, u8 sr, loff_t *ofs,
59 					u64 *len)
60 {
61 	u64 min_prot_len;
62 	u8 mask = spi_nor_get_sr_bp_mask(nor);
63 	u8 tb_mask = spi_nor_get_sr_tb_mask(nor);
64 	u8 bp, val = sr & mask;
65 
66 	if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3_BIT6)
67 		val = (val & ~SR_BP3_BIT6) | SR_BP3;
68 
69 	bp = val >> SR_BP_SHIFT;
70 
71 	if (!bp) {
72 		/* No protection */
73 		*ofs = 0;
74 		*len = 0;
75 		return;
76 	}
77 
78 	min_prot_len = spi_nor_get_min_prot_length_sr(nor);
79 	*len = min_prot_len << (bp - 1);
80 
81 	if (*len > nor->params->size)
82 		*len = nor->params->size;
83 
84 	if (nor->flags & SNOR_F_HAS_SR_TB && sr & tb_mask)
85 		*ofs = 0;
86 	else
87 		*ofs = nor->params->size - *len;
88 }
89 
90 /*
91  * Return true if the entire region is locked (if @locked is true) or unlocked
92  * (if @locked is false); false otherwise.
93  */
94 static bool spi_nor_check_lock_status_sr(struct spi_nor *nor, loff_t ofs,
95 					 u64 len, u8 sr, bool locked)
96 {
97 	loff_t lock_offs, lock_offs_max, offs_max;
98 	u64 lock_len;
99 
100 	if (!len)
101 		return true;
102 
103 	spi_nor_get_locked_range_sr(nor, sr, &lock_offs, &lock_len);
104 
105 	lock_offs_max = lock_offs + lock_len;
106 	offs_max = ofs + len;
107 
108 	if (locked)
109 		/* Requested range is a sub-range of locked range */
110 		return (offs_max <= lock_offs_max) && (ofs >= lock_offs);
111 	else
112 		/* Requested range does not overlap with locked range */
113 		return (ofs >= lock_offs_max) || (offs_max <= lock_offs);
114 }
115 
116 static bool spi_nor_is_locked_sr(struct spi_nor *nor, loff_t ofs, u64 len, u8 sr)
117 {
118 	return spi_nor_check_lock_status_sr(nor, ofs, len, sr, true);
119 }
120 
121 static bool spi_nor_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, u64 len,
122 				   u8 sr)
123 {
124 	return spi_nor_check_lock_status_sr(nor, ofs, len, sr, false);
125 }
126 
127 /*
128  * Lock a region of the flash. Compatible with ST Micro and similar flash.
129  * Supports the block protection bits BP{0,1,2}/BP{0,1,2,3} in the status
130  * register
131  * (SR). Does not support these features found in newer SR bitfields:
132  *   - SEC: sector/block protect - only handle SEC=0 (block protect)
133  *   - CMP: complement protect - only support CMP=0 (range is not complemented)
134  *
135  * Support for the following is provided conditionally for some flash:
136  *   - TB: top/bottom protect
137  *
138  * Sample table portion for 8MB flash (Winbond w25q64fw):
139  *
140  *   SEC  |  TB   |  BP2  |  BP1  |  BP0  |  Prot Length  | Protected Portion
141  *  --------------------------------------------------------------------------
142  *    X   |   X   |   0   |   0   |   0   |  NONE         | NONE
143  *    0   |   0   |   0   |   0   |   1   |  128 KB       | Upper 1/64
144  *    0   |   0   |   0   |   1   |   0   |  256 KB       | Upper 1/32
145  *    0   |   0   |   0   |   1   |   1   |  512 KB       | Upper 1/16
146  *    0   |   0   |   1   |   0   |   0   |  1 MB         | Upper 1/8
147  *    0   |   0   |   1   |   0   |   1   |  2 MB         | Upper 1/4
148  *    0   |   0   |   1   |   1   |   0   |  4 MB         | Upper 1/2
149  *    X   |   X   |   1   |   1   |   1   |  8 MB         | ALL
150  *  ------|-------|-------|-------|-------|---------------|-------------------
151  *    0   |   1   |   0   |   0   |   1   |  128 KB       | Lower 1/64
152  *    0   |   1   |   0   |   1   |   0   |  256 KB       | Lower 1/32
153  *    0   |   1   |   0   |   1   |   1   |  512 KB       | Lower 1/16
154  *    0   |   1   |   1   |   0   |   0   |  1 MB         | Lower 1/8
155  *    0   |   1   |   1   |   0   |   1   |  2 MB         | Lower 1/4
156  *    0   |   1   |   1   |   1   |   0   |  4 MB         | Lower 1/2
157  *
158  * Returns negative on errors, 0 on success.
159  */
160 static int spi_nor_sr_lock(struct spi_nor *nor, loff_t ofs, u64 len)
161 {
162 	u64 min_prot_len;
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);
166 	u8 pow, val;
167 	loff_t lock_len;
168 	bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
169 	bool use_top;
170 
171 	ret = spi_nor_read_sr(nor, nor->bouncebuf);
172 	if (ret)
173 		return ret;
174 
175 	status_old = nor->bouncebuf[0];
176 
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))
179 		return 0;
180 
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;
184 
185 	/* If anything above us is unlocked, we can't use 'top' protection */
186 	if (!spi_nor_is_locked_sr(nor, ofs + len, nor->params->size - (ofs + len),
187 				  status_old))
188 		can_be_top = false;
189 
190 	if (!can_be_bottom && !can_be_top)
191 		return -EINVAL;
192 
193 	/* Prefer top, if both are valid */
194 	use_top = can_be_top;
195 
196 	/* lock_len: length of region that should end up locked */
197 	if (use_top)
198 		lock_len = nor->params->size - ofs;
199 	else
200 		lock_len = ofs + len;
201 
202 	if (lock_len == nor->params->size) {
203 		val = mask;
204 	} else {
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;
208 
209 		if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
210 			val = (val & ~SR_BP3) | SR_BP3_BIT6;
211 
212 		if (val & ~mask)
213 			return -EINVAL;
214 
215 		/* Don't "lock" with no region! */
216 		if (!(val & mask))
217 			return -EINVAL;
218 	}
219 
220 	status_new = (status_old & ~mask & ~tb_mask) | val;
221 
222 	/*
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.
226 	 */
227 	if (!(nor->flags & SNOR_F_NO_WP))
228 		status_new |= SR_SRWD;
229 
230 	if (!use_top)
231 		status_new |= tb_mask;
232 
233 	/* Don't bother if they're the same */
234 	if (status_new == status_old)
235 		return 0;
236 
237 	/* Only modify protection if it will not unlock other areas */
238 	if ((status_new & mask) < (status_old & mask))
239 		return -EINVAL;
240 
241 	return spi_nor_write_sr_and_check(nor, status_new);
242 }
243 
244 /*
245  * Unlock a region of the flash. See spi_nor_sr_lock() for more info
246  *
247  * Returns negative on errors, 0 on success.
248  */
249 static int spi_nor_sr_unlock(struct spi_nor *nor, loff_t ofs, u64 len)
250 {
251 	u64 min_prot_len;
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);
255 	u8 pow, val;
256 	loff_t lock_len;
257 	bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
258 	bool use_top;
259 
260 	ret = spi_nor_read_sr(nor, nor->bouncebuf);
261 	if (ret)
262 		return ret;
263 
264 	status_old = nor->bouncebuf[0];
265 
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))
268 		return 0;
269 
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))
272 		can_be_top = false;
273 
274 	/* If anything above us is locked, we can't use 'bottom' protection */
275 	if (!spi_nor_is_unlocked_sr(nor, ofs + len, nor->params->size - (ofs + len),
276 				    status_old))
277 		can_be_bottom = false;
278 
279 	if (!can_be_bottom && !can_be_top)
280 		return -EINVAL;
281 
282 	/* Prefer top, if both are valid */
283 	use_top = can_be_top;
284 
285 	/* lock_len: length of region that should remain locked */
286 	if (use_top)
287 		lock_len = nor->params->size - (ofs + len);
288 	else
289 		lock_len = ofs;
290 
291 	if (lock_len == 0) {
292 		val = 0; /* fully unlocked */
293 	} else {
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;
297 
298 		if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
299 			val = (val & ~SR_BP3) | SR_BP3_BIT6;
300 
301 		/* Some power-of-two sizes are not supported */
302 		if (val & ~mask)
303 			return -EINVAL;
304 	}
305 
306 	status_new = (status_old & ~mask & ~tb_mask) | val;
307 
308 	/* Don't protect status register if we're fully unlocked */
309 	if (lock_len == 0)
310 		status_new &= ~SR_SRWD;
311 
312 	if (!use_top)
313 		status_new |= tb_mask;
314 
315 	/* Don't bother if they're the same */
316 	if (status_new == status_old)
317 		return 0;
318 
319 	/* Only modify protection if it will not lock other areas */
320 	if ((status_new & mask) > (status_old & mask))
321 		return -EINVAL;
322 
323 	return spi_nor_write_sr_and_check(nor, status_new);
324 }
325 
326 /*
327  * Check if a region of the flash is (completely) locked. See spi_nor_sr_lock()
328  * for more info.
329  *
330  * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
331  * negative on errors.
332  */
333 static int spi_nor_sr_is_locked(struct spi_nor *nor, loff_t ofs, u64 len)
334 {
335 	int ret;
336 
337 	ret = spi_nor_read_sr(nor, nor->bouncebuf);
338 	if (ret)
339 		return ret;
340 
341 	return spi_nor_is_locked_sr(nor, ofs, len, nor->bouncebuf[0]);
342 }
343 
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,
348 };
349 
350 void spi_nor_init_default_locking_ops(struct spi_nor *nor)
351 {
352 	nor->params->locking_ops = &spi_nor_sr_locking_ops;
353 }
354 
355 static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, u64 len)
356 {
357 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
358 	int ret;
359 
360 	ret = spi_nor_prep_and_lock(nor);
361 	if (ret)
362 		return ret;
363 
364 	ret = nor->params->locking_ops->lock(nor, ofs, len);
365 
366 	spi_nor_unlock_and_unprep(nor);
367 	return ret;
368 }
369 
370 static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, u64 len)
371 {
372 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
373 	int ret;
374 
375 	ret = spi_nor_prep_and_lock(nor);
376 	if (ret)
377 		return ret;
378 
379 	ret = nor->params->locking_ops->unlock(nor, ofs, len);
380 
381 	spi_nor_unlock_and_unprep(nor);
382 	return ret;
383 }
384 
385 static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, u64 len)
386 {
387 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
388 	int ret;
389 
390 	ret = spi_nor_prep_and_lock(nor);
391 	if (ret)
392 		return ret;
393 
394 	ret = nor->params->locking_ops->is_locked(nor, ofs, len);
395 
396 	spi_nor_unlock_and_unprep(nor);
397 	return ret;
398 }
399 
400 /**
401  * spi_nor_try_unlock_all() - Tries to unlock the entire flash memory array.
402  * @nor:	pointer to a 'struct spi_nor'.
403  *
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
407  * by default.
408  *
409  * Unprotecting the entire flash array will fail for boards which are hardware
410  * write-protected. Thus any errors are ignored.
411  */
412 void spi_nor_try_unlock_all(struct spi_nor *nor)
413 {
414 	int ret;
415 
416 	if (!(nor->flags & SNOR_F_HAS_LOCK))
417 		return;
418 
419 	dev_dbg(nor->dev, "Unprotecting entire flash array\n");
420 
421 	ret = spi_nor_unlock(&nor->mtd, 0, nor->params->size);
422 	if (ret)
423 		dev_dbg(nor->dev, "Failed to unlock the entire flash memory array\n");
424 }
425 
426 void spi_nor_set_mtd_locking_ops(struct spi_nor *nor)
427 {
428 	struct mtd_info *mtd = &nor->mtd;
429 
430 	if (!nor->params->locking_ops)
431 		return;
432 
433 	mtd->_lock = spi_nor_lock;
434 	mtd->_unlock = spi_nor_unlock;
435 	mtd->_is_locked = spi_nor_is_locked;
436 }
437