xref: /openbmc/linux/drivers/mtd/spi-nor/swp.c (revision d7bbdc9b)
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 
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 
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 
34 static u64 spi_nor_get_min_prot_length_sr(struct spi_nor *nor)
35 {
36 	unsigned int bp_slots, bp_slots_needed;
37 	u8 mask = spi_nor_get_sr_bp_mask(nor);
38 
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);
42 
43 	if (bp_slots_needed > bp_slots)
44 		return nor->info->sector_size <<
45 			(bp_slots_needed - bp_slots);
46 	else
47 		return nor->info->sector_size;
48 }
49 
50 static void spi_nor_get_locked_range_sr(struct spi_nor *nor, u8 sr, loff_t *ofs,
51 					uint64_t *len)
52 {
53 	struct mtd_info *mtd = &nor->mtd;
54 	u64 min_prot_len;
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;
58 
59 	if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3_BIT6)
60 		val = (val & ~SR_BP3_BIT6) | SR_BP3;
61 
62 	bp = val >> SR_BP_SHIFT;
63 
64 	if (!bp) {
65 		/* No protection */
66 		*ofs = 0;
67 		*len = 0;
68 		return;
69 	}
70 
71 	min_prot_len = spi_nor_get_min_prot_length_sr(nor);
72 	*len = min_prot_len << (bp - 1);
73 
74 	if (*len > mtd->size)
75 		*len = mtd->size;
76 
77 	if (nor->flags & SNOR_F_HAS_SR_TB && sr & tb_mask)
78 		*ofs = 0;
79 	else
80 		*ofs = mtd->size - *len;
81 }
82 
83 /*
84  * Return true if the entire region is locked (if @locked is true) or unlocked
85  * (if @locked is false); false otherwise.
86  */
87 static bool spi_nor_check_lock_status_sr(struct spi_nor *nor, loff_t ofs,
88 					 uint64_t len, u8 sr, bool locked)
89 {
90 	loff_t lock_offs, lock_offs_max, offs_max;
91 	uint64_t lock_len;
92 
93 	if (!len)
94 		return true;
95 
96 	spi_nor_get_locked_range_sr(nor, sr, &lock_offs, &lock_len);
97 
98 	lock_offs_max = lock_offs + lock_len;
99 	offs_max = ofs + len;
100 
101 	if (locked)
102 		/* Requested range is a sub-range of locked range */
103 		return (offs_max <= lock_offs_max) && (ofs >= lock_offs);
104 	else
105 		/* Requested range does not overlap with locked range */
106 		return (ofs >= lock_offs_max) || (offs_max <= lock_offs);
107 }
108 
109 static bool spi_nor_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
110 				 u8 sr)
111 {
112 	return spi_nor_check_lock_status_sr(nor, ofs, len, sr, true);
113 }
114 
115 static bool spi_nor_is_unlocked_sr(struct spi_nor *nor, loff_t ofs,
116 				   uint64_t len, u8 sr)
117 {
118 	return spi_nor_check_lock_status_sr(nor, ofs, len, sr, false);
119 }
120 
121 /*
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
124  * register
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)
128  *
129  * Support for the following is provided conditionally for some flash:
130  *   - TB: top/bottom protect
131  *
132  * Sample table portion for 8MB flash (Winbond w25q64fw):
133  *
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
151  *
152  * Returns negative on errors, 0 on success.
153  */
154 static int spi_nor_sr_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
155 {
156 	struct mtd_info *mtd = &nor->mtd;
157 	u64 min_prot_len;
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);
161 	u8 pow, val;
162 	loff_t lock_len;
163 	bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
164 	bool use_top;
165 
166 	ret = spi_nor_read_sr(nor, nor->bouncebuf);
167 	if (ret)
168 		return ret;
169 
170 	status_old = nor->bouncebuf[0];
171 
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))
174 		return 0;
175 
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;
179 
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),
182 				  status_old))
183 		can_be_top = false;
184 
185 	if (!can_be_bottom && !can_be_top)
186 		return -EINVAL;
187 
188 	/* Prefer top, if both are valid */
189 	use_top = can_be_top;
190 
191 	/* lock_len: length of region that should end up locked */
192 	if (use_top)
193 		lock_len = mtd->size - ofs;
194 	else
195 		lock_len = ofs + len;
196 
197 	if (lock_len == mtd->size) {
198 		val = mask;
199 	} else {
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;
203 
204 		if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
205 			val = (val & ~SR_BP3) | SR_BP3_BIT6;
206 
207 		if (val & ~mask)
208 			return -EINVAL;
209 
210 		/* Don't "lock" with no region! */
211 		if (!(val & mask))
212 			return -EINVAL;
213 	}
214 
215 	status_new = (status_old & ~mask & ~tb_mask) | val;
216 
217 	/*
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.
221 	 */
222 	if (!(nor->flags & SNOR_F_NO_WP))
223 		status_new |= SR_SRWD;
224 
225 	if (!use_top)
226 		status_new |= tb_mask;
227 
228 	/* Don't bother if they're the same */
229 	if (status_new == status_old)
230 		return 0;
231 
232 	/* Only modify protection if it will not unlock other areas */
233 	if ((status_new & mask) < (status_old & mask))
234 		return -EINVAL;
235 
236 	return spi_nor_write_sr_and_check(nor, status_new);
237 }
238 
239 /*
240  * Unlock a region of the flash. See spi_nor_sr_lock() for more info
241  *
242  * Returns negative on errors, 0 on success.
243  */
244 static int spi_nor_sr_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
245 {
246 	struct mtd_info *mtd = &nor->mtd;
247 	u64 min_prot_len;
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);
251 	u8 pow, val;
252 	loff_t lock_len;
253 	bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
254 	bool use_top;
255 
256 	ret = spi_nor_read_sr(nor, nor->bouncebuf);
257 	if (ret)
258 		return ret;
259 
260 	status_old = nor->bouncebuf[0];
261 
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))
264 		return 0;
265 
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))
268 		can_be_top = false;
269 
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),
272 				    status_old))
273 		can_be_bottom = false;
274 
275 	if (!can_be_bottom && !can_be_top)
276 		return -EINVAL;
277 
278 	/* Prefer top, if both are valid */
279 	use_top = can_be_top;
280 
281 	/* lock_len: length of region that should remain locked */
282 	if (use_top)
283 		lock_len = mtd->size - (ofs + len);
284 	else
285 		lock_len = ofs;
286 
287 	if (lock_len == 0) {
288 		val = 0; /* fully unlocked */
289 	} else {
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;
293 
294 		if (nor->flags & SNOR_F_HAS_SR_BP3_BIT6 && val & SR_BP3)
295 			val = (val & ~SR_BP3) | SR_BP3_BIT6;
296 
297 		/* Some power-of-two sizes are not supported */
298 		if (val & ~mask)
299 			return -EINVAL;
300 	}
301 
302 	status_new = (status_old & ~mask & ~tb_mask) | val;
303 
304 	/* Don't protect status register if we're fully unlocked */
305 	if (lock_len == 0)
306 		status_new &= ~SR_SRWD;
307 
308 	if (!use_top)
309 		status_new |= tb_mask;
310 
311 	/* Don't bother if they're the same */
312 	if (status_new == status_old)
313 		return 0;
314 
315 	/* Only modify protection if it will not lock other areas */
316 	if ((status_new & mask) > (status_old & mask))
317 		return -EINVAL;
318 
319 	return spi_nor_write_sr_and_check(nor, status_new);
320 }
321 
322 /*
323  * Check if a region of the flash is (completely) locked. See spi_nor_sr_lock()
324  * for more info.
325  *
326  * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
327  * negative on errors.
328  */
329 static int spi_nor_sr_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
330 {
331 	int ret;
332 
333 	ret = spi_nor_read_sr(nor, nor->bouncebuf);
334 	if (ret)
335 		return ret;
336 
337 	return spi_nor_is_locked_sr(nor, ofs, len, nor->bouncebuf[0]);
338 }
339 
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,
344 };
345 
346 void spi_nor_init_default_locking_ops(struct spi_nor *nor)
347 {
348 	nor->params->locking_ops = &spi_nor_sr_locking_ops;
349 }
350 
351 static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
352 {
353 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
354 	int ret;
355 
356 	ret = spi_nor_prep_and_lock(nor);
357 	if (ret)
358 		return ret;
359 
360 	ret = nor->params->locking_ops->lock(nor, ofs, len);
361 
362 	spi_nor_unlock_and_unprep(nor);
363 	return ret;
364 }
365 
366 static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
367 {
368 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
369 	int ret;
370 
371 	ret = spi_nor_prep_and_lock(nor);
372 	if (ret)
373 		return ret;
374 
375 	ret = nor->params->locking_ops->unlock(nor, ofs, len);
376 
377 	spi_nor_unlock_and_unprep(nor);
378 	return ret;
379 }
380 
381 static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
382 {
383 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
384 	int ret;
385 
386 	ret = spi_nor_prep_and_lock(nor);
387 	if (ret)
388 		return ret;
389 
390 	ret = nor->params->locking_ops->is_locked(nor, ofs, len);
391 
392 	spi_nor_unlock_and_unprep(nor);
393 	return ret;
394 }
395 
396 /**
397  * spi_nor_try_unlock_all() - Tries to unlock the entire flash memory array.
398  * @nor:	pointer to a 'struct spi_nor'.
399  *
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
403  * by default.
404  *
405  * Unprotecting the entire flash array will fail for boards which are hardware
406  * write-protected. Thus any errors are ignored.
407  */
408 void spi_nor_try_unlock_all(struct spi_nor *nor)
409 {
410 	int ret;
411 
412 	if (!(nor->flags & SNOR_F_HAS_LOCK))
413 		return;
414 
415 	dev_dbg(nor->dev, "Unprotecting entire flash array\n");
416 
417 	ret = spi_nor_unlock(&nor->mtd, 0, nor->params->size);
418 	if (ret)
419 		dev_dbg(nor->dev, "Failed to unlock the entire flash memory array\n");
420 }
421 
422 void spi_nor_set_mtd_locking_ops(struct spi_nor *nor)
423 {
424 	struct mtd_info *mtd = &nor->mtd;
425 
426 	if (!nor->params->locking_ops)
427 		return;
428 
429 	mtd->_lock = spi_nor_lock;
430 	mtd->_unlock = spi_nor_unlock;
431 	mtd->_is_locked = spi_nor_is_locked;
432 }
433