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