xref: /openbmc/linux/drivers/mtd/spi-nor/otp.c (revision 388161ca)
1069089acSMichael Walle // SPDX-License-Identifier: GPL-2.0
2069089acSMichael Walle /*
3069089acSMichael Walle  * OTP support for SPI NOR flashes
4069089acSMichael Walle  *
5069089acSMichael Walle  * Copyright (C) 2021 Michael Walle <michael@walle.cc>
6069089acSMichael Walle  */
7069089acSMichael Walle 
8069089acSMichael Walle #include <linux/log2.h>
9069089acSMichael Walle #include <linux/mtd/mtd.h>
10069089acSMichael Walle #include <linux/mtd/spi-nor.h>
11069089acSMichael Walle 
12069089acSMichael Walle #include "core.h"
13069089acSMichael Walle 
14069089acSMichael Walle #define spi_nor_otp_region_len(nor) ((nor)->params->otp.org->len)
15069089acSMichael Walle #define spi_nor_otp_n_regions(nor) ((nor)->params->otp.org->n_regions)
16069089acSMichael Walle 
17cad3193fSMichael Walle /**
18d5b813e4SMichael Walle  * spi_nor_otp_read_secr() - read security register
19cad3193fSMichael Walle  * @nor:	pointer to 'struct spi_nor'
20a6e2cd4dSMichael Walle  * @addr:       offset to read from
21cad3193fSMichael Walle  * @len:        number of bytes to read
22cad3193fSMichael Walle  * @buf:        pointer to dst buffer
23cad3193fSMichael Walle  *
24d5b813e4SMichael Walle  * Read a security register by using the SPINOR_OP_RSECR commands.
25d5b813e4SMichael Walle  *
26d5b813e4SMichael Walle  * In Winbond/GigaDevice datasheets the term "security register" stands for
27d5b813e4SMichael Walle  * an one-time-programmable memory area, consisting of multiple bytes (usually
28d5b813e4SMichael Walle  * 256). Thus one "security register" maps to one OTP region.
29d5b813e4SMichael Walle  *
30d5b813e4SMichael Walle  * This method is used on GigaDevice and Winbond flashes.
31d5b813e4SMichael Walle  *
32d5b813e4SMichael Walle  * Please note, the read must not span multiple registers.
33cad3193fSMichael Walle  *
34cad3193fSMichael Walle  * Return: number of bytes read successfully, -errno otherwise
35cad3193fSMichael Walle  */
36cad3193fSMichael Walle int spi_nor_otp_read_secr(struct spi_nor *nor, loff_t addr, size_t len, u8 *buf)
37cad3193fSMichael Walle {
38cad3193fSMichael Walle 	u8 addr_width, read_opcode, read_dummy;
39cad3193fSMichael Walle 	struct spi_mem_dirmap_desc *rdesc;
40cad3193fSMichael Walle 	enum spi_nor_protocol read_proto;
41cad3193fSMichael Walle 	int ret;
42cad3193fSMichael Walle 
43cad3193fSMichael Walle 	read_opcode = nor->read_opcode;
44cad3193fSMichael Walle 	addr_width = nor->addr_width;
45cad3193fSMichael Walle 	read_dummy = nor->read_dummy;
46cad3193fSMichael Walle 	read_proto = nor->read_proto;
47cad3193fSMichael Walle 	rdesc = nor->dirmap.rdesc;
48cad3193fSMichael Walle 
49cad3193fSMichael Walle 	nor->read_opcode = SPINOR_OP_RSECR;
50cad3193fSMichael Walle 	nor->read_dummy = 8;
51cad3193fSMichael Walle 	nor->read_proto = SNOR_PROTO_1_1_1;
52cad3193fSMichael Walle 	nor->dirmap.rdesc = NULL;
53cad3193fSMichael Walle 
54cad3193fSMichael Walle 	ret = spi_nor_read_data(nor, addr, len, buf);
55cad3193fSMichael Walle 
56cad3193fSMichael Walle 	nor->read_opcode = read_opcode;
57cad3193fSMichael Walle 	nor->addr_width = addr_width;
58cad3193fSMichael Walle 	nor->read_dummy = read_dummy;
59cad3193fSMichael Walle 	nor->read_proto = read_proto;
60cad3193fSMichael Walle 	nor->dirmap.rdesc = rdesc;
61cad3193fSMichael Walle 
62cad3193fSMichael Walle 	return ret;
63cad3193fSMichael Walle }
64cad3193fSMichael Walle 
65cad3193fSMichael Walle /**
66d5b813e4SMichael Walle  * spi_nor_otp_write_secr() - write security register
67cad3193fSMichael Walle  * @nor:        pointer to 'struct spi_nor'
68a6e2cd4dSMichael Walle  * @addr:       offset to write to
69cad3193fSMichael Walle  * @len:        number of bytes to write
70cad3193fSMichael Walle  * @buf:        pointer to src buffer
71cad3193fSMichael Walle  *
72d5b813e4SMichael Walle  * Write a security register by using the SPINOR_OP_PSECR commands.
73cad3193fSMichael Walle  *
74d5b813e4SMichael Walle  * For more information on the term "security register", see the documentation
75d5b813e4SMichael Walle  * of spi_nor_otp_read_secr().
76d5b813e4SMichael Walle  *
77d5b813e4SMichael Walle  * This method is used on GigaDevice and Winbond flashes.
78d5b813e4SMichael Walle  *
79d5b813e4SMichael Walle  * Please note, the write must not span multiple registers.
80cad3193fSMichael Walle  *
81cad3193fSMichael Walle  * Return: number of bytes written successfully, -errno otherwise
82cad3193fSMichael Walle  */
831df1fc8cSTudor Ambarus int spi_nor_otp_write_secr(struct spi_nor *nor, loff_t addr, size_t len,
841df1fc8cSTudor Ambarus 			   const u8 *buf)
85cad3193fSMichael Walle {
86cad3193fSMichael Walle 	enum spi_nor_protocol write_proto;
87cad3193fSMichael Walle 	struct spi_mem_dirmap_desc *wdesc;
88cad3193fSMichael Walle 	u8 addr_width, program_opcode;
89cad3193fSMichael Walle 	int ret, written;
90cad3193fSMichael Walle 
91cad3193fSMichael Walle 	program_opcode = nor->program_opcode;
92cad3193fSMichael Walle 	addr_width = nor->addr_width;
93cad3193fSMichael Walle 	write_proto = nor->write_proto;
94cad3193fSMichael Walle 	wdesc = nor->dirmap.wdesc;
95cad3193fSMichael Walle 
96cad3193fSMichael Walle 	nor->program_opcode = SPINOR_OP_PSECR;
97cad3193fSMichael Walle 	nor->write_proto = SNOR_PROTO_1_1_1;
98cad3193fSMichael Walle 	nor->dirmap.wdesc = NULL;
99cad3193fSMichael Walle 
100cad3193fSMichael Walle 	/*
101cad3193fSMichael Walle 	 * We only support a write to one single page. For now all winbond
102d5b813e4SMichael Walle 	 * flashes only have one page per security register.
103cad3193fSMichael Walle 	 */
104cad3193fSMichael Walle 	ret = spi_nor_write_enable(nor);
105cad3193fSMichael Walle 	if (ret)
106cad3193fSMichael Walle 		goto out;
107cad3193fSMichael Walle 
108cad3193fSMichael Walle 	written = spi_nor_write_data(nor, addr, len, buf);
109cad3193fSMichael Walle 	if (written < 0)
110cad3193fSMichael Walle 		goto out;
111cad3193fSMichael Walle 
112cad3193fSMichael Walle 	ret = spi_nor_wait_till_ready(nor);
113cad3193fSMichael Walle 
114cad3193fSMichael Walle out:
115cad3193fSMichael Walle 	nor->program_opcode = program_opcode;
116cad3193fSMichael Walle 	nor->addr_width = addr_width;
117cad3193fSMichael Walle 	nor->write_proto = write_proto;
118cad3193fSMichael Walle 	nor->dirmap.wdesc = wdesc;
119cad3193fSMichael Walle 
120cad3193fSMichael Walle 	return ret ?: written;
121cad3193fSMichael Walle }
122cad3193fSMichael Walle 
123cad3193fSMichael Walle static int spi_nor_otp_lock_bit_cr(unsigned int region)
124cad3193fSMichael Walle {
125cad3193fSMichael Walle 	static const int lock_bits[] = { SR2_LB1, SR2_LB2, SR2_LB3 };
126cad3193fSMichael Walle 
127cad3193fSMichael Walle 	if (region >= ARRAY_SIZE(lock_bits))
128cad3193fSMichael Walle 		return -EINVAL;
129cad3193fSMichael Walle 
130cad3193fSMichael Walle 	return lock_bits[region];
131cad3193fSMichael Walle }
132cad3193fSMichael Walle 
133cad3193fSMichael Walle /**
134cad3193fSMichael Walle  * spi_nor_otp_lock_sr2() - lock the OTP region
135cad3193fSMichael Walle  * @nor:        pointer to 'struct spi_nor'
136cad3193fSMichael Walle  * @region:     OTP region
137cad3193fSMichael Walle  *
138cad3193fSMichael Walle  * Lock the OTP region by writing the status register-2. This method is used on
139cad3193fSMichael Walle  * GigaDevice and Winbond flashes.
140cad3193fSMichael Walle  *
141cad3193fSMichael Walle  * Return: 0 on success, -errno otherwise.
142cad3193fSMichael Walle  */
143cad3193fSMichael Walle int spi_nor_otp_lock_sr2(struct spi_nor *nor, unsigned int region)
144cad3193fSMichael Walle {
145cad3193fSMichael Walle 	u8 *cr = nor->bouncebuf;
146cad3193fSMichael Walle 	int ret, lock_bit;
147cad3193fSMichael Walle 
148cad3193fSMichael Walle 	lock_bit = spi_nor_otp_lock_bit_cr(region);
149cad3193fSMichael Walle 	if (lock_bit < 0)
150cad3193fSMichael Walle 		return lock_bit;
151cad3193fSMichael Walle 
152cad3193fSMichael Walle 	ret = spi_nor_read_cr(nor, cr);
153cad3193fSMichael Walle 	if (ret)
154cad3193fSMichael Walle 		return ret;
155cad3193fSMichael Walle 
156cad3193fSMichael Walle 	/* no need to write the register if region is already locked */
157cad3193fSMichael Walle 	if (cr[0] & lock_bit)
158cad3193fSMichael Walle 		return 0;
159cad3193fSMichael Walle 
160cad3193fSMichael Walle 	cr[0] |= lock_bit;
161cad3193fSMichael Walle 
162cad3193fSMichael Walle 	return spi_nor_write_16bit_cr_and_check(nor, cr[0]);
163cad3193fSMichael Walle }
164cad3193fSMichael Walle 
165cad3193fSMichael Walle /**
166cad3193fSMichael Walle  * spi_nor_otp_is_locked_sr2() - get the OTP region lock status
167cad3193fSMichael Walle  * @nor:        pointer to 'struct spi_nor'
168cad3193fSMichael Walle  * @region:     OTP region
169cad3193fSMichael Walle  *
170cad3193fSMichael Walle  * Retrieve the OTP region lock bit by reading the status register-2. This
171cad3193fSMichael Walle  * method is used on GigaDevice and Winbond flashes.
172cad3193fSMichael Walle  *
173cad3193fSMichael Walle  * Return: 0 on success, -errno otherwise.
174cad3193fSMichael Walle  */
175cad3193fSMichael Walle int spi_nor_otp_is_locked_sr2(struct spi_nor *nor, unsigned int region)
176cad3193fSMichael Walle {
177cad3193fSMichael Walle 	u8 *cr = nor->bouncebuf;
178cad3193fSMichael Walle 	int ret, lock_bit;
179cad3193fSMichael Walle 
180cad3193fSMichael Walle 	lock_bit = spi_nor_otp_lock_bit_cr(region);
181cad3193fSMichael Walle 	if (lock_bit < 0)
182cad3193fSMichael Walle 		return lock_bit;
183cad3193fSMichael Walle 
184cad3193fSMichael Walle 	ret = spi_nor_read_cr(nor, cr);
185cad3193fSMichael Walle 	if (ret)
186cad3193fSMichael Walle 		return ret;
187cad3193fSMichael Walle 
188cad3193fSMichael Walle 	return cr[0] & lock_bit;
189cad3193fSMichael Walle }
190cad3193fSMichael Walle 
191069089acSMichael Walle static loff_t spi_nor_otp_region_start(const struct spi_nor *nor, unsigned int region)
192069089acSMichael Walle {
193069089acSMichael Walle 	const struct spi_nor_otp_organization *org = nor->params->otp.org;
194069089acSMichael Walle 
195069089acSMichael Walle 	return org->base + region * org->offset;
196069089acSMichael Walle }
197069089acSMichael Walle 
198069089acSMichael Walle static size_t spi_nor_otp_size(struct spi_nor *nor)
199069089acSMichael Walle {
200069089acSMichael Walle 	return spi_nor_otp_n_regions(nor) * spi_nor_otp_region_len(nor);
201069089acSMichael Walle }
202069089acSMichael Walle 
203069089acSMichael Walle /* Translate the file offsets from and to OTP regions. */
204069089acSMichael Walle static loff_t spi_nor_otp_region_to_offset(struct spi_nor *nor, unsigned int region)
205069089acSMichael Walle {
206069089acSMichael Walle 	return region * spi_nor_otp_region_len(nor);
207069089acSMichael Walle }
208069089acSMichael Walle 
209069089acSMichael Walle static unsigned int spi_nor_otp_offset_to_region(struct spi_nor *nor, loff_t ofs)
210069089acSMichael Walle {
211069089acSMichael Walle 	return div64_u64(ofs, spi_nor_otp_region_len(nor));
212069089acSMichael Walle }
213069089acSMichael Walle 
214069089acSMichael Walle static int spi_nor_mtd_otp_info(struct mtd_info *mtd, size_t len,
215069089acSMichael Walle 				size_t *retlen, struct otp_info *buf)
216069089acSMichael Walle {
217069089acSMichael Walle 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
218069089acSMichael Walle 	const struct spi_nor_otp_ops *ops = nor->params->otp.ops;
219069089acSMichael Walle 	unsigned int n_regions = spi_nor_otp_n_regions(nor);
220069089acSMichael Walle 	unsigned int i;
221069089acSMichael Walle 	int ret, locked;
222069089acSMichael Walle 
223069089acSMichael Walle 	if (len < n_regions * sizeof(*buf))
224069089acSMichael Walle 		return -ENOSPC;
225069089acSMichael Walle 
226069089acSMichael Walle 	ret = spi_nor_lock_and_prep(nor);
227069089acSMichael Walle 	if (ret)
228069089acSMichael Walle 		return ret;
229069089acSMichael Walle 
230069089acSMichael Walle 	for (i = 0; i < n_regions; i++) {
231069089acSMichael Walle 		buf->start = spi_nor_otp_region_to_offset(nor, i);
232069089acSMichael Walle 		buf->length = spi_nor_otp_region_len(nor);
233069089acSMichael Walle 
234069089acSMichael Walle 		locked = ops->is_locked(nor, i);
235069089acSMichael Walle 		if (locked < 0) {
236069089acSMichael Walle 			ret = locked;
237069089acSMichael Walle 			goto out;
238069089acSMichael Walle 		}
239069089acSMichael Walle 
240069089acSMichael Walle 		buf->locked = !!locked;
241069089acSMichael Walle 		buf++;
242069089acSMichael Walle 	}
243069089acSMichael Walle 
244069089acSMichael Walle 	*retlen = n_regions * sizeof(*buf);
245069089acSMichael Walle 
246069089acSMichael Walle out:
247069089acSMichael Walle 	spi_nor_unlock_and_unprep(nor);
248069089acSMichael Walle 
249069089acSMichael Walle 	return ret;
250069089acSMichael Walle }
251069089acSMichael Walle 
252*388161caSMichael Walle static int spi_nor_mtd_otp_range_is_locked(struct spi_nor *nor, loff_t ofs,
253*388161caSMichael Walle 					   size_t len)
254*388161caSMichael Walle {
255*388161caSMichael Walle 	const struct spi_nor_otp_ops *ops = nor->params->otp.ops;
256*388161caSMichael Walle 	unsigned int region;
257*388161caSMichael Walle 	int locked;
258*388161caSMichael Walle 
259*388161caSMichael Walle 	/*
260*388161caSMichael Walle 	 * If any of the affected OTP regions are locked the entire range is
261*388161caSMichael Walle 	 * considered locked.
262*388161caSMichael Walle 	 */
263*388161caSMichael Walle 	for (region = spi_nor_otp_offset_to_region(nor, ofs);
264*388161caSMichael Walle 	     region <= spi_nor_otp_offset_to_region(nor, ofs + len - 1);
265*388161caSMichael Walle 	     region++) {
266*388161caSMichael Walle 		locked = ops->is_locked(nor, region);
267*388161caSMichael Walle 		/* take the branch it is locked or in case of an error */
268*388161caSMichael Walle 		if (locked)
269*388161caSMichael Walle 			return locked;
270*388161caSMichael Walle 	}
271*388161caSMichael Walle 
272*388161caSMichael Walle 	return 0;
273*388161caSMichael Walle }
274*388161caSMichael Walle 
275069089acSMichael Walle static int spi_nor_mtd_otp_read_write(struct mtd_info *mtd, loff_t ofs,
276069089acSMichael Walle 				      size_t total_len, size_t *retlen,
2771df1fc8cSTudor Ambarus 				      const u8 *buf, bool is_write)
278069089acSMichael Walle {
279069089acSMichael Walle 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
280069089acSMichael Walle 	const struct spi_nor_otp_ops *ops = nor->params->otp.ops;
281069089acSMichael Walle 	const size_t rlen = spi_nor_otp_region_len(nor);
282069089acSMichael Walle 	loff_t rstart, rofs;
283069089acSMichael Walle 	unsigned int region;
284069089acSMichael Walle 	size_t len;
285069089acSMichael Walle 	int ret;
286069089acSMichael Walle 
287069089acSMichael Walle 	if (ofs < 0 || ofs >= spi_nor_otp_size(nor))
288069089acSMichael Walle 		return 0;
289069089acSMichael Walle 
290*388161caSMichael Walle 	/* don't access beyond the end */
291*388161caSMichael Walle 	total_len = min_t(size_t, total_len, spi_nor_otp_size(nor) - ofs);
292*388161caSMichael Walle 
293*388161caSMichael Walle 	if (!total_len)
294*388161caSMichael Walle 		return 0;
295*388161caSMichael Walle 
296069089acSMichael Walle 	ret = spi_nor_lock_and_prep(nor);
297069089acSMichael Walle 	if (ret)
298069089acSMichael Walle 		return ret;
299069089acSMichael Walle 
300*388161caSMichael Walle 	if (is_write) {
301*388161caSMichael Walle 		ret = spi_nor_mtd_otp_range_is_locked(nor, ofs, total_len);
302*388161caSMichael Walle 		if (ret < 0) {
303*388161caSMichael Walle 			goto out;
304*388161caSMichael Walle 		} else if (ret) {
305*388161caSMichael Walle 			ret = -EROFS;
306*388161caSMichael Walle 			goto out;
307*388161caSMichael Walle 		}
308*388161caSMichael Walle 	}
309069089acSMichael Walle 
310069089acSMichael Walle 	while (total_len) {
311069089acSMichael Walle 		/*
312069089acSMichael Walle 		 * The OTP regions are mapped into a contiguous area starting
313069089acSMichael Walle 		 * at 0 as expected by the MTD layer. This will map the MTD
314069089acSMichael Walle 		 * file offsets to the address of an OTP region as used in the
315069089acSMichael Walle 		 * actual SPI commands.
316069089acSMichael Walle 		 */
317069089acSMichael Walle 		region = spi_nor_otp_offset_to_region(nor, ofs);
318069089acSMichael Walle 		rstart = spi_nor_otp_region_start(nor, region);
319069089acSMichael Walle 
320069089acSMichael Walle 		/*
321069089acSMichael Walle 		 * The size of a OTP region is expected to be a power of two,
322069089acSMichael Walle 		 * thus we can just mask the lower bits and get the offset into
323069089acSMichael Walle 		 * a region.
324069089acSMichael Walle 		 */
325069089acSMichael Walle 		rofs = ofs & (rlen - 1);
326069089acSMichael Walle 
327069089acSMichael Walle 		/* don't access beyond one OTP region */
328069089acSMichael Walle 		len = min_t(size_t, total_len, rlen - rofs);
329069089acSMichael Walle 
330069089acSMichael Walle 		if (is_write)
331069089acSMichael Walle 			ret = ops->write(nor, rstart + rofs, len, buf);
332069089acSMichael Walle 		else
3331df1fc8cSTudor Ambarus 			ret = ops->read(nor, rstart + rofs, len, (u8 *)buf);
334069089acSMichael Walle 		if (ret == 0)
335069089acSMichael Walle 			ret = -EIO;
336069089acSMichael Walle 		if (ret < 0)
337069089acSMichael Walle 			goto out;
338069089acSMichael Walle 
339069089acSMichael Walle 		*retlen += ret;
340069089acSMichael Walle 		ofs += ret;
341069089acSMichael Walle 		buf += ret;
342069089acSMichael Walle 		total_len -= ret;
343069089acSMichael Walle 	}
344069089acSMichael Walle 	ret = 0;
345069089acSMichael Walle 
346069089acSMichael Walle out:
347069089acSMichael Walle 	spi_nor_unlock_and_unprep(nor);
348069089acSMichael Walle 	return ret;
349069089acSMichael Walle }
350069089acSMichael Walle 
351069089acSMichael Walle static int spi_nor_mtd_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
352069089acSMichael Walle 				size_t *retlen, u8 *buf)
353069089acSMichael Walle {
354069089acSMichael Walle 	return spi_nor_mtd_otp_read_write(mtd, from, len, retlen, buf, false);
355069089acSMichael Walle }
356069089acSMichael Walle 
357069089acSMichael Walle static int spi_nor_mtd_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
3581df1fc8cSTudor Ambarus 				 size_t *retlen, const u8 *buf)
359069089acSMichael Walle {
360069089acSMichael Walle 	return spi_nor_mtd_otp_read_write(mtd, to, len, retlen, buf, true);
361069089acSMichael Walle }
362069089acSMichael Walle 
363069089acSMichael Walle static int spi_nor_mtd_otp_lock(struct mtd_info *mtd, loff_t from, size_t len)
364069089acSMichael Walle {
365069089acSMichael Walle 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
366069089acSMichael Walle 	const struct spi_nor_otp_ops *ops = nor->params->otp.ops;
367069089acSMichael Walle 	const size_t rlen = spi_nor_otp_region_len(nor);
368069089acSMichael Walle 	unsigned int region;
369069089acSMichael Walle 	int ret;
370069089acSMichael Walle 
371069089acSMichael Walle 	if (from < 0 || (from + len) > spi_nor_otp_size(nor))
372069089acSMichael Walle 		return -EINVAL;
373069089acSMichael Walle 
374069089acSMichael Walle 	/* the user has to explicitly ask for whole regions */
375069089acSMichael Walle 	if (!IS_ALIGNED(len, rlen) || !IS_ALIGNED(from, rlen))
376069089acSMichael Walle 		return -EINVAL;
377069089acSMichael Walle 
378069089acSMichael Walle 	ret = spi_nor_lock_and_prep(nor);
379069089acSMichael Walle 	if (ret)
380069089acSMichael Walle 		return ret;
381069089acSMichael Walle 
382069089acSMichael Walle 	while (len) {
383069089acSMichael Walle 		region = spi_nor_otp_offset_to_region(nor, from);
384069089acSMichael Walle 		ret = ops->lock(nor, region);
385069089acSMichael Walle 		if (ret)
386069089acSMichael Walle 			goto out;
387069089acSMichael Walle 
388069089acSMichael Walle 		len -= rlen;
389069089acSMichael Walle 		from += rlen;
390069089acSMichael Walle 	}
391069089acSMichael Walle 
392069089acSMichael Walle out:
393069089acSMichael Walle 	spi_nor_unlock_and_unprep(nor);
394069089acSMichael Walle 
395069089acSMichael Walle 	return ret;
396069089acSMichael Walle }
397069089acSMichael Walle 
398069089acSMichael Walle void spi_nor_otp_init(struct spi_nor *nor)
399069089acSMichael Walle {
400069089acSMichael Walle 	struct mtd_info *mtd = &nor->mtd;
401069089acSMichael Walle 
402069089acSMichael Walle 	if (!nor->params->otp.ops)
403069089acSMichael Walle 		return;
404069089acSMichael Walle 
405069089acSMichael Walle 	if (WARN_ON(!is_power_of_2(spi_nor_otp_region_len(nor))))
406069089acSMichael Walle 		return;
407069089acSMichael Walle 
408069089acSMichael Walle 	/*
409069089acSMichael Walle 	 * We only support user_prot callbacks (yet).
410069089acSMichael Walle 	 *
411069089acSMichael Walle 	 * Some SPI NOR flashes like Macronix ones can be ordered in two
412069089acSMichael Walle 	 * different variants. One with a factory locked OTP area and one where
413069089acSMichael Walle 	 * it is left to the user to write to it. The factory locked OTP is
414069089acSMichael Walle 	 * usually preprogrammed with an "electrical serial number". We don't
415069089acSMichael Walle 	 * support these for now.
416069089acSMichael Walle 	 */
417069089acSMichael Walle 	mtd->_get_user_prot_info = spi_nor_mtd_otp_info;
418069089acSMichael Walle 	mtd->_read_user_prot_reg = spi_nor_mtd_otp_read;
419069089acSMichael Walle 	mtd->_write_user_prot_reg = spi_nor_mtd_otp_write;
420069089acSMichael Walle 	mtd->_lock_user_prot_reg = spi_nor_mtd_otp_lock;
421069089acSMichael Walle }
422