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