Linux Audio

Check our new training course

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