xref: /openbmc/linux/drivers/mtd/spi-nor/otp.c (revision 388161ca)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * OTP support for SPI NOR flashes
4  *
5  * Copyright (C) 2021 Michael Walle <michael@walle.cc>
6  */
7 
8 #include <linux/log2.h>
9 #include <linux/mtd/mtd.h>
10 #include <linux/mtd/spi-nor.h>
11 
12 #include "core.h"
13 
14 #define spi_nor_otp_region_len(nor) ((nor)->params->otp.org->len)
15 #define spi_nor_otp_n_regions(nor) ((nor)->params->otp.org->n_regions)
16 
17 /**
18  * spi_nor_otp_read_secr() - read security register
19  * @nor:	pointer to 'struct spi_nor'
20  * @addr:       offset to read from
21  * @len:        number of bytes to read
22  * @buf:        pointer to dst buffer
23  *
24  * Read a security register by using the SPINOR_OP_RSECR commands.
25  *
26  * In Winbond/GigaDevice datasheets the term "security register" stands for
27  * an one-time-programmable memory area, consisting of multiple bytes (usually
28  * 256). Thus one "security register" maps to one OTP region.
29  *
30  * This method is used on GigaDevice and Winbond flashes.
31  *
32  * Please note, the read must not span multiple registers.
33  *
34  * Return: number of bytes read successfully, -errno otherwise
35  */
36 int spi_nor_otp_read_secr(struct spi_nor *nor, loff_t addr, size_t len, u8 *buf)
37 {
38 	u8 addr_width, read_opcode, read_dummy;
39 	struct spi_mem_dirmap_desc *rdesc;
40 	enum spi_nor_protocol read_proto;
41 	int ret;
42 
43 	read_opcode = nor->read_opcode;
44 	addr_width = nor->addr_width;
45 	read_dummy = nor->read_dummy;
46 	read_proto = nor->read_proto;
47 	rdesc = nor->dirmap.rdesc;
48 
49 	nor->read_opcode = SPINOR_OP_RSECR;
50 	nor->read_dummy = 8;
51 	nor->read_proto = SNOR_PROTO_1_1_1;
52 	nor->dirmap.rdesc = NULL;
53 
54 	ret = spi_nor_read_data(nor, addr, len, buf);
55 
56 	nor->read_opcode = read_opcode;
57 	nor->addr_width = addr_width;
58 	nor->read_dummy = read_dummy;
59 	nor->read_proto = read_proto;
60 	nor->dirmap.rdesc = rdesc;
61 
62 	return ret;
63 }
64 
65 /**
66  * spi_nor_otp_write_secr() - write security register
67  * @nor:        pointer to 'struct spi_nor'
68  * @addr:       offset to write to
69  * @len:        number of bytes to write
70  * @buf:        pointer to src buffer
71  *
72  * Write a security register by using the SPINOR_OP_PSECR commands.
73  *
74  * For more information on the term "security register", see the documentation
75  * of spi_nor_otp_read_secr().
76  *
77  * This method is used on GigaDevice and Winbond flashes.
78  *
79  * Please note, the write must not span multiple registers.
80  *
81  * Return: number of bytes written successfully, -errno otherwise
82  */
83 int spi_nor_otp_write_secr(struct spi_nor *nor, loff_t addr, size_t len,
84 			   const u8 *buf)
85 {
86 	enum spi_nor_protocol write_proto;
87 	struct spi_mem_dirmap_desc *wdesc;
88 	u8 addr_width, program_opcode;
89 	int ret, written;
90 
91 	program_opcode = nor->program_opcode;
92 	addr_width = nor->addr_width;
93 	write_proto = nor->write_proto;
94 	wdesc = nor->dirmap.wdesc;
95 
96 	nor->program_opcode = SPINOR_OP_PSECR;
97 	nor->write_proto = SNOR_PROTO_1_1_1;
98 	nor->dirmap.wdesc = NULL;
99 
100 	/*
101 	 * We only support a write to one single page. For now all winbond
102 	 * flashes only have one page per security register.
103 	 */
104 	ret = spi_nor_write_enable(nor);
105 	if (ret)
106 		goto out;
107 
108 	written = spi_nor_write_data(nor, addr, len, buf);
109 	if (written < 0)
110 		goto out;
111 
112 	ret = spi_nor_wait_till_ready(nor);
113 
114 out:
115 	nor->program_opcode = program_opcode;
116 	nor->addr_width = addr_width;
117 	nor->write_proto = write_proto;
118 	nor->dirmap.wdesc = wdesc;
119 
120 	return ret ?: written;
121 }
122 
123 static int spi_nor_otp_lock_bit_cr(unsigned int region)
124 {
125 	static const int lock_bits[] = { SR2_LB1, SR2_LB2, SR2_LB3 };
126 
127 	if (region >= ARRAY_SIZE(lock_bits))
128 		return -EINVAL;
129 
130 	return lock_bits[region];
131 }
132 
133 /**
134  * spi_nor_otp_lock_sr2() - lock the OTP region
135  * @nor:        pointer to 'struct spi_nor'
136  * @region:     OTP region
137  *
138  * Lock the OTP region by writing the status register-2. This method is used on
139  * GigaDevice and Winbond flashes.
140  *
141  * Return: 0 on success, -errno otherwise.
142  */
143 int spi_nor_otp_lock_sr2(struct spi_nor *nor, unsigned int region)
144 {
145 	u8 *cr = nor->bouncebuf;
146 	int ret, lock_bit;
147 
148 	lock_bit = spi_nor_otp_lock_bit_cr(region);
149 	if (lock_bit < 0)
150 		return lock_bit;
151 
152 	ret = spi_nor_read_cr(nor, cr);
153 	if (ret)
154 		return ret;
155 
156 	/* no need to write the register if region is already locked */
157 	if (cr[0] & lock_bit)
158 		return 0;
159 
160 	cr[0] |= lock_bit;
161 
162 	return spi_nor_write_16bit_cr_and_check(nor, cr[0]);
163 }
164 
165 /**
166  * spi_nor_otp_is_locked_sr2() - get the OTP region lock status
167  * @nor:        pointer to 'struct spi_nor'
168  * @region:     OTP region
169  *
170  * Retrieve the OTP region lock bit by reading the status register-2. This
171  * method is used on GigaDevice and Winbond flashes.
172  *
173  * Return: 0 on success, -errno otherwise.
174  */
175 int spi_nor_otp_is_locked_sr2(struct spi_nor *nor, unsigned int region)
176 {
177 	u8 *cr = nor->bouncebuf;
178 	int ret, lock_bit;
179 
180 	lock_bit = spi_nor_otp_lock_bit_cr(region);
181 	if (lock_bit < 0)
182 		return lock_bit;
183 
184 	ret = spi_nor_read_cr(nor, cr);
185 	if (ret)
186 		return ret;
187 
188 	return cr[0] & lock_bit;
189 }
190 
191 static loff_t spi_nor_otp_region_start(const struct spi_nor *nor, unsigned int region)
192 {
193 	const struct spi_nor_otp_organization *org = nor->params->otp.org;
194 
195 	return org->base + region * org->offset;
196 }
197 
198 static size_t spi_nor_otp_size(struct spi_nor *nor)
199 {
200 	return spi_nor_otp_n_regions(nor) * spi_nor_otp_region_len(nor);
201 }
202 
203 /* Translate the file offsets from and to OTP regions. */
204 static loff_t spi_nor_otp_region_to_offset(struct spi_nor *nor, unsigned int region)
205 {
206 	return region * spi_nor_otp_region_len(nor);
207 }
208 
209 static unsigned int spi_nor_otp_offset_to_region(struct spi_nor *nor, loff_t ofs)
210 {
211 	return div64_u64(ofs, spi_nor_otp_region_len(nor));
212 }
213 
214 static int spi_nor_mtd_otp_info(struct mtd_info *mtd, size_t len,
215 				size_t *retlen, struct otp_info *buf)
216 {
217 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
218 	const struct spi_nor_otp_ops *ops = nor->params->otp.ops;
219 	unsigned int n_regions = spi_nor_otp_n_regions(nor);
220 	unsigned int i;
221 	int ret, locked;
222 
223 	if (len < n_regions * sizeof(*buf))
224 		return -ENOSPC;
225 
226 	ret = spi_nor_lock_and_prep(nor);
227 	if (ret)
228 		return ret;
229 
230 	for (i = 0; i < n_regions; i++) {
231 		buf->start = spi_nor_otp_region_to_offset(nor, i);
232 		buf->length = spi_nor_otp_region_len(nor);
233 
234 		locked = ops->is_locked(nor, i);
235 		if (locked < 0) {
236 			ret = locked;
237 			goto out;
238 		}
239 
240 		buf->locked = !!locked;
241 		buf++;
242 	}
243 
244 	*retlen = n_regions * sizeof(*buf);
245 
246 out:
247 	spi_nor_unlock_and_unprep(nor);
248 
249 	return ret;
250 }
251 
252 static int spi_nor_mtd_otp_range_is_locked(struct spi_nor *nor, loff_t ofs,
253 					   size_t len)
254 {
255 	const struct spi_nor_otp_ops *ops = nor->params->otp.ops;
256 	unsigned int region;
257 	int locked;
258 
259 	/*
260 	 * If any of the affected OTP regions are locked the entire range is
261 	 * considered locked.
262 	 */
263 	for (region = spi_nor_otp_offset_to_region(nor, ofs);
264 	     region <= spi_nor_otp_offset_to_region(nor, ofs + len - 1);
265 	     region++) {
266 		locked = ops->is_locked(nor, region);
267 		/* take the branch it is locked or in case of an error */
268 		if (locked)
269 			return locked;
270 	}
271 
272 	return 0;
273 }
274 
275 static int spi_nor_mtd_otp_read_write(struct mtd_info *mtd, loff_t ofs,
276 				      size_t total_len, size_t *retlen,
277 				      const u8 *buf, bool is_write)
278 {
279 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
280 	const struct spi_nor_otp_ops *ops = nor->params->otp.ops;
281 	const size_t rlen = spi_nor_otp_region_len(nor);
282 	loff_t rstart, rofs;
283 	unsigned int region;
284 	size_t len;
285 	int ret;
286 
287 	if (ofs < 0 || ofs >= spi_nor_otp_size(nor))
288 		return 0;
289 
290 	/* don't access beyond the end */
291 	total_len = min_t(size_t, total_len, spi_nor_otp_size(nor) - ofs);
292 
293 	if (!total_len)
294 		return 0;
295 
296 	ret = spi_nor_lock_and_prep(nor);
297 	if (ret)
298 		return ret;
299 
300 	if (is_write) {
301 		ret = spi_nor_mtd_otp_range_is_locked(nor, ofs, total_len);
302 		if (ret < 0) {
303 			goto out;
304 		} else if (ret) {
305 			ret = -EROFS;
306 			goto out;
307 		}
308 	}
309 
310 	while (total_len) {
311 		/*
312 		 * The OTP regions are mapped into a contiguous area starting
313 		 * at 0 as expected by the MTD layer. This will map the MTD
314 		 * file offsets to the address of an OTP region as used in the
315 		 * actual SPI commands.
316 		 */
317 		region = spi_nor_otp_offset_to_region(nor, ofs);
318 		rstart = spi_nor_otp_region_start(nor, region);
319 
320 		/*
321 		 * The size of a OTP region is expected to be a power of two,
322 		 * thus we can just mask the lower bits and get the offset into
323 		 * a region.
324 		 */
325 		rofs = ofs & (rlen - 1);
326 
327 		/* don't access beyond one OTP region */
328 		len = min_t(size_t, total_len, rlen - rofs);
329 
330 		if (is_write)
331 			ret = ops->write(nor, rstart + rofs, len, buf);
332 		else
333 			ret = ops->read(nor, rstart + rofs, len, (u8 *)buf);
334 		if (ret == 0)
335 			ret = -EIO;
336 		if (ret < 0)
337 			goto out;
338 
339 		*retlen += ret;
340 		ofs += ret;
341 		buf += ret;
342 		total_len -= ret;
343 	}
344 	ret = 0;
345 
346 out:
347 	spi_nor_unlock_and_unprep(nor);
348 	return ret;
349 }
350 
351 static int spi_nor_mtd_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
352 				size_t *retlen, u8 *buf)
353 {
354 	return spi_nor_mtd_otp_read_write(mtd, from, len, retlen, buf, false);
355 }
356 
357 static int spi_nor_mtd_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
358 				 size_t *retlen, const u8 *buf)
359 {
360 	return spi_nor_mtd_otp_read_write(mtd, to, len, retlen, buf, true);
361 }
362 
363 static int spi_nor_mtd_otp_lock(struct mtd_info *mtd, loff_t from, size_t len)
364 {
365 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
366 	const struct spi_nor_otp_ops *ops = nor->params->otp.ops;
367 	const size_t rlen = spi_nor_otp_region_len(nor);
368 	unsigned int region;
369 	int ret;
370 
371 	if (from < 0 || (from + len) > spi_nor_otp_size(nor))
372 		return -EINVAL;
373 
374 	/* the user has to explicitly ask for whole regions */
375 	if (!IS_ALIGNED(len, rlen) || !IS_ALIGNED(from, rlen))
376 		return -EINVAL;
377 
378 	ret = spi_nor_lock_and_prep(nor);
379 	if (ret)
380 		return ret;
381 
382 	while (len) {
383 		region = spi_nor_otp_offset_to_region(nor, from);
384 		ret = ops->lock(nor, region);
385 		if (ret)
386 			goto out;
387 
388 		len -= rlen;
389 		from += rlen;
390 	}
391 
392 out:
393 	spi_nor_unlock_and_unprep(nor);
394 
395 	return ret;
396 }
397 
398 void spi_nor_otp_init(struct spi_nor *nor)
399 {
400 	struct mtd_info *mtd = &nor->mtd;
401 
402 	if (!nor->params->otp.ops)
403 		return;
404 
405 	if (WARN_ON(!is_power_of_2(spi_nor_otp_region_len(nor))))
406 		return;
407 
408 	/*
409 	 * We only support user_prot callbacks (yet).
410 	 *
411 	 * Some SPI NOR flashes like Macronix ones can be ordered in two
412 	 * different variants. One with a factory locked OTP area and one where
413 	 * it is left to the user to write to it. The factory locked OTP is
414 	 * usually preprogrammed with an "electrical serial number". We don't
415 	 * support these for now.
416 	 */
417 	mtd->_get_user_prot_info = spi_nor_mtd_otp_info;
418 	mtd->_read_user_prot_reg = spi_nor_mtd_otp_read;
419 	mtd->_write_user_prot_reg = spi_nor_mtd_otp_write;
420 	mtd->_lock_user_prot_reg = spi_nor_mtd_otp_lock;
421 }
422