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