xref: /openbmc/linux/drivers/mtd/spi-nor/otp.c (revision 1df1fc8c)
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 /**
18cad3193fSMichael Walle  * spi_nor_otp_read_secr() - read OTP data
19cad3193fSMichael Walle  * @nor:	pointer to 'struct spi_nor'
20cad3193fSMichael Walle  * @from:       offset to read from
21cad3193fSMichael Walle  * @len:        number of bytes to read
22cad3193fSMichael Walle  * @buf:        pointer to dst buffer
23cad3193fSMichael Walle  *
24cad3193fSMichael Walle  * Read OTP data from one region by using the SPINOR_OP_RSECR commands. This
25cad3193fSMichael Walle  * method is used on GigaDevice and Winbond flashes.
26cad3193fSMichael Walle  *
27cad3193fSMichael Walle  * Return: number of bytes read successfully, -errno otherwise
28cad3193fSMichael Walle  */
29cad3193fSMichael Walle int spi_nor_otp_read_secr(struct spi_nor *nor, loff_t addr, size_t len, u8 *buf)
30cad3193fSMichael Walle {
31cad3193fSMichael Walle 	u8 addr_width, read_opcode, read_dummy;
32cad3193fSMichael Walle 	struct spi_mem_dirmap_desc *rdesc;
33cad3193fSMichael Walle 	enum spi_nor_protocol read_proto;
34cad3193fSMichael Walle 	int ret;
35cad3193fSMichael Walle 
36cad3193fSMichael Walle 	read_opcode = nor->read_opcode;
37cad3193fSMichael Walle 	addr_width = nor->addr_width;
38cad3193fSMichael Walle 	read_dummy = nor->read_dummy;
39cad3193fSMichael Walle 	read_proto = nor->read_proto;
40cad3193fSMichael Walle 	rdesc = nor->dirmap.rdesc;
41cad3193fSMichael Walle 
42cad3193fSMichael Walle 	nor->read_opcode = SPINOR_OP_RSECR;
43cad3193fSMichael Walle 	nor->addr_width = 3;
44cad3193fSMichael Walle 	nor->read_dummy = 8;
45cad3193fSMichael Walle 	nor->read_proto = SNOR_PROTO_1_1_1;
46cad3193fSMichael Walle 	nor->dirmap.rdesc = NULL;
47cad3193fSMichael Walle 
48cad3193fSMichael Walle 	ret = spi_nor_read_data(nor, addr, len, buf);
49cad3193fSMichael Walle 
50cad3193fSMichael Walle 	nor->read_opcode = read_opcode;
51cad3193fSMichael Walle 	nor->addr_width = addr_width;
52cad3193fSMichael Walle 	nor->read_dummy = read_dummy;
53cad3193fSMichael Walle 	nor->read_proto = read_proto;
54cad3193fSMichael Walle 	nor->dirmap.rdesc = rdesc;
55cad3193fSMichael Walle 
56cad3193fSMichael Walle 	return ret;
57cad3193fSMichael Walle }
58cad3193fSMichael Walle 
59cad3193fSMichael Walle /**
60cad3193fSMichael Walle  * spi_nor_otp_write_secr() - write OTP data
61cad3193fSMichael Walle  * @nor:        pointer to 'struct spi_nor'
62cad3193fSMichael Walle  * @to:         offset to write to
63cad3193fSMichael Walle  * @len:        number of bytes to write
64cad3193fSMichael Walle  * @buf:        pointer to src buffer
65cad3193fSMichael Walle  *
66cad3193fSMichael Walle  * Write OTP data to one region by using the SPINOR_OP_PSECR commands. This
67cad3193fSMichael Walle  * method is used on GigaDevice and Winbond flashes.
68cad3193fSMichael Walle  *
69cad3193fSMichael Walle  * Please note, the write must not span multiple OTP regions.
70cad3193fSMichael Walle  *
71cad3193fSMichael Walle  * Return: number of bytes written successfully, -errno otherwise
72cad3193fSMichael Walle  */
73*1df1fc8cSTudor Ambarus int spi_nor_otp_write_secr(struct spi_nor *nor, loff_t addr, size_t len,
74*1df1fc8cSTudor Ambarus 			   const u8 *buf)
75cad3193fSMichael Walle {
76cad3193fSMichael Walle 	enum spi_nor_protocol write_proto;
77cad3193fSMichael Walle 	struct spi_mem_dirmap_desc *wdesc;
78cad3193fSMichael Walle 	u8 addr_width, program_opcode;
79cad3193fSMichael Walle 	int ret, written;
80cad3193fSMichael Walle 
81cad3193fSMichael Walle 	program_opcode = nor->program_opcode;
82cad3193fSMichael Walle 	addr_width = nor->addr_width;
83cad3193fSMichael Walle 	write_proto = nor->write_proto;
84cad3193fSMichael Walle 	wdesc = nor->dirmap.wdesc;
85cad3193fSMichael Walle 
86cad3193fSMichael Walle 	nor->program_opcode = SPINOR_OP_PSECR;
87cad3193fSMichael Walle 	nor->addr_width = 3;
88cad3193fSMichael Walle 	nor->write_proto = SNOR_PROTO_1_1_1;
89cad3193fSMichael Walle 	nor->dirmap.wdesc = NULL;
90cad3193fSMichael Walle 
91cad3193fSMichael Walle 	/*
92cad3193fSMichael Walle 	 * We only support a write to one single page. For now all winbond
93cad3193fSMichael Walle 	 * flashes only have one page per OTP region.
94cad3193fSMichael Walle 	 */
95cad3193fSMichael Walle 	ret = spi_nor_write_enable(nor);
96cad3193fSMichael Walle 	if (ret)
97cad3193fSMichael Walle 		goto out;
98cad3193fSMichael Walle 
99cad3193fSMichael Walle 	written = spi_nor_write_data(nor, addr, len, buf);
100cad3193fSMichael Walle 	if (written < 0)
101cad3193fSMichael Walle 		goto out;
102cad3193fSMichael Walle 
103cad3193fSMichael Walle 	ret = spi_nor_wait_till_ready(nor);
104cad3193fSMichael Walle 
105cad3193fSMichael Walle out:
106cad3193fSMichael Walle 	nor->program_opcode = program_opcode;
107cad3193fSMichael Walle 	nor->addr_width = addr_width;
108cad3193fSMichael Walle 	nor->write_proto = write_proto;
109cad3193fSMichael Walle 	nor->dirmap.wdesc = wdesc;
110cad3193fSMichael Walle 
111cad3193fSMichael Walle 	return ret ?: written;
112cad3193fSMichael Walle }
113cad3193fSMichael Walle 
114cad3193fSMichael Walle static int spi_nor_otp_lock_bit_cr(unsigned int region)
115cad3193fSMichael Walle {
116cad3193fSMichael Walle 	static const int lock_bits[] = { SR2_LB1, SR2_LB2, SR2_LB3 };
117cad3193fSMichael Walle 
118cad3193fSMichael Walle 	if (region >= ARRAY_SIZE(lock_bits))
119cad3193fSMichael Walle 		return -EINVAL;
120cad3193fSMichael Walle 
121cad3193fSMichael Walle 	return lock_bits[region];
122cad3193fSMichael Walle }
123cad3193fSMichael Walle 
124cad3193fSMichael Walle /**
125cad3193fSMichael Walle  * spi_nor_otp_lock_sr2() - lock the OTP region
126cad3193fSMichael Walle  * @nor:        pointer to 'struct spi_nor'
127cad3193fSMichael Walle  * @region:     OTP region
128cad3193fSMichael Walle  *
129cad3193fSMichael Walle  * Lock the OTP region by writing the status register-2. This method is used on
130cad3193fSMichael Walle  * GigaDevice and Winbond flashes.
131cad3193fSMichael Walle  *
132cad3193fSMichael Walle  * Return: 0 on success, -errno otherwise.
133cad3193fSMichael Walle  */
134cad3193fSMichael Walle int spi_nor_otp_lock_sr2(struct spi_nor *nor, unsigned int region)
135cad3193fSMichael Walle {
136cad3193fSMichael Walle 	u8 *cr = nor->bouncebuf;
137cad3193fSMichael Walle 	int ret, lock_bit;
138cad3193fSMichael Walle 
139cad3193fSMichael Walle 	lock_bit = spi_nor_otp_lock_bit_cr(region);
140cad3193fSMichael Walle 	if (lock_bit < 0)
141cad3193fSMichael Walle 		return lock_bit;
142cad3193fSMichael Walle 
143cad3193fSMichael Walle 	ret = spi_nor_read_cr(nor, cr);
144cad3193fSMichael Walle 	if (ret)
145cad3193fSMichael Walle 		return ret;
146cad3193fSMichael Walle 
147cad3193fSMichael Walle 	/* no need to write the register if region is already locked */
148cad3193fSMichael Walle 	if (cr[0] & lock_bit)
149cad3193fSMichael Walle 		return 0;
150cad3193fSMichael Walle 
151cad3193fSMichael Walle 	cr[0] |= lock_bit;
152cad3193fSMichael Walle 
153cad3193fSMichael Walle 	return spi_nor_write_16bit_cr_and_check(nor, cr[0]);
154cad3193fSMichael Walle }
155cad3193fSMichael Walle 
156cad3193fSMichael Walle /**
157cad3193fSMichael Walle  * spi_nor_otp_is_locked_sr2() - get the OTP region lock status
158cad3193fSMichael Walle  * @nor:        pointer to 'struct spi_nor'
159cad3193fSMichael Walle  * @region:     OTP region
160cad3193fSMichael Walle  *
161cad3193fSMichael Walle  * Retrieve the OTP region lock bit by reading the status register-2. This
162cad3193fSMichael Walle  * method is used on GigaDevice and Winbond flashes.
163cad3193fSMichael Walle  *
164cad3193fSMichael Walle  * Return: 0 on success, -errno otherwise.
165cad3193fSMichael Walle  */
166cad3193fSMichael Walle int spi_nor_otp_is_locked_sr2(struct spi_nor *nor, unsigned int region)
167cad3193fSMichael Walle {
168cad3193fSMichael Walle 	u8 *cr = nor->bouncebuf;
169cad3193fSMichael Walle 	int ret, lock_bit;
170cad3193fSMichael Walle 
171cad3193fSMichael Walle 	lock_bit = spi_nor_otp_lock_bit_cr(region);
172cad3193fSMichael Walle 	if (lock_bit < 0)
173cad3193fSMichael Walle 		return lock_bit;
174cad3193fSMichael Walle 
175cad3193fSMichael Walle 	ret = spi_nor_read_cr(nor, cr);
176cad3193fSMichael Walle 	if (ret)
177cad3193fSMichael Walle 		return ret;
178cad3193fSMichael Walle 
179cad3193fSMichael Walle 	return cr[0] & lock_bit;
180cad3193fSMichael Walle }
181cad3193fSMichael Walle 
182069089acSMichael Walle static loff_t spi_nor_otp_region_start(const struct spi_nor *nor, unsigned int region)
183069089acSMichael Walle {
184069089acSMichael Walle 	const struct spi_nor_otp_organization *org = nor->params->otp.org;
185069089acSMichael Walle 
186069089acSMichael Walle 	return org->base + region * org->offset;
187069089acSMichael Walle }
188069089acSMichael Walle 
189069089acSMichael Walle static size_t spi_nor_otp_size(struct spi_nor *nor)
190069089acSMichael Walle {
191069089acSMichael Walle 	return spi_nor_otp_n_regions(nor) * spi_nor_otp_region_len(nor);
192069089acSMichael Walle }
193069089acSMichael Walle 
194069089acSMichael Walle /* Translate the file offsets from and to OTP regions. */
195069089acSMichael Walle static loff_t spi_nor_otp_region_to_offset(struct spi_nor *nor, unsigned int region)
196069089acSMichael Walle {
197069089acSMichael Walle 	return region * spi_nor_otp_region_len(nor);
198069089acSMichael Walle }
199069089acSMichael Walle 
200069089acSMichael Walle static unsigned int spi_nor_otp_offset_to_region(struct spi_nor *nor, loff_t ofs)
201069089acSMichael Walle {
202069089acSMichael Walle 	return div64_u64(ofs, spi_nor_otp_region_len(nor));
203069089acSMichael Walle }
204069089acSMichael Walle 
205069089acSMichael Walle static int spi_nor_mtd_otp_info(struct mtd_info *mtd, size_t len,
206069089acSMichael Walle 				size_t *retlen, struct otp_info *buf)
207069089acSMichael Walle {
208069089acSMichael Walle 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
209069089acSMichael Walle 	const struct spi_nor_otp_ops *ops = nor->params->otp.ops;
210069089acSMichael Walle 	unsigned int n_regions = spi_nor_otp_n_regions(nor);
211069089acSMichael Walle 	unsigned int i;
212069089acSMichael Walle 	int ret, locked;
213069089acSMichael Walle 
214069089acSMichael Walle 	if (len < n_regions * sizeof(*buf))
215069089acSMichael Walle 		return -ENOSPC;
216069089acSMichael Walle 
217069089acSMichael Walle 	ret = spi_nor_lock_and_prep(nor);
218069089acSMichael Walle 	if (ret)
219069089acSMichael Walle 		return ret;
220069089acSMichael Walle 
221069089acSMichael Walle 	for (i = 0; i < n_regions; i++) {
222069089acSMichael Walle 		buf->start = spi_nor_otp_region_to_offset(nor, i);
223069089acSMichael Walle 		buf->length = spi_nor_otp_region_len(nor);
224069089acSMichael Walle 
225069089acSMichael Walle 		locked = ops->is_locked(nor, i);
226069089acSMichael Walle 		if (locked < 0) {
227069089acSMichael Walle 			ret = locked;
228069089acSMichael Walle 			goto out;
229069089acSMichael Walle 		}
230069089acSMichael Walle 
231069089acSMichael Walle 		buf->locked = !!locked;
232069089acSMichael Walle 		buf++;
233069089acSMichael Walle 	}
234069089acSMichael Walle 
235069089acSMichael Walle 	*retlen = n_regions * sizeof(*buf);
236069089acSMichael Walle 
237069089acSMichael Walle out:
238069089acSMichael Walle 	spi_nor_unlock_and_unprep(nor);
239069089acSMichael Walle 
240069089acSMichael Walle 	return ret;
241069089acSMichael Walle }
242069089acSMichael Walle 
243069089acSMichael Walle static int spi_nor_mtd_otp_read_write(struct mtd_info *mtd, loff_t ofs,
244069089acSMichael Walle 				      size_t total_len, size_t *retlen,
245*1df1fc8cSTudor Ambarus 				      const u8 *buf, bool is_write)
246069089acSMichael Walle {
247069089acSMichael Walle 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
248069089acSMichael Walle 	const struct spi_nor_otp_ops *ops = nor->params->otp.ops;
249069089acSMichael Walle 	const size_t rlen = spi_nor_otp_region_len(nor);
250069089acSMichael Walle 	loff_t rstart, rofs;
251069089acSMichael Walle 	unsigned int region;
252069089acSMichael Walle 	size_t len;
253069089acSMichael Walle 	int ret;
254069089acSMichael Walle 
255069089acSMichael Walle 	if (ofs < 0 || ofs >= spi_nor_otp_size(nor))
256069089acSMichael Walle 		return 0;
257069089acSMichael Walle 
258069089acSMichael Walle 	ret = spi_nor_lock_and_prep(nor);
259069089acSMichael Walle 	if (ret)
260069089acSMichael Walle 		return ret;
261069089acSMichael Walle 
262069089acSMichael Walle 	/* don't access beyond the end */
263069089acSMichael Walle 	total_len = min_t(size_t, total_len, spi_nor_otp_size(nor) - ofs);
264069089acSMichael Walle 
265069089acSMichael Walle 	*retlen = 0;
266069089acSMichael Walle 	while (total_len) {
267069089acSMichael Walle 		/*
268069089acSMichael Walle 		 * The OTP regions are mapped into a contiguous area starting
269069089acSMichael Walle 		 * at 0 as expected by the MTD layer. This will map the MTD
270069089acSMichael Walle 		 * file offsets to the address of an OTP region as used in the
271069089acSMichael Walle 		 * actual SPI commands.
272069089acSMichael Walle 		 */
273069089acSMichael Walle 		region = spi_nor_otp_offset_to_region(nor, ofs);
274069089acSMichael Walle 		rstart = spi_nor_otp_region_start(nor, region);
275069089acSMichael Walle 
276069089acSMichael Walle 		/*
277069089acSMichael Walle 		 * The size of a OTP region is expected to be a power of two,
278069089acSMichael Walle 		 * thus we can just mask the lower bits and get the offset into
279069089acSMichael Walle 		 * a region.
280069089acSMichael Walle 		 */
281069089acSMichael Walle 		rofs = ofs & (rlen - 1);
282069089acSMichael Walle 
283069089acSMichael Walle 		/* don't access beyond one OTP region */
284069089acSMichael Walle 		len = min_t(size_t, total_len, rlen - rofs);
285069089acSMichael Walle 
286069089acSMichael Walle 		if (is_write)
287069089acSMichael Walle 			ret = ops->write(nor, rstart + rofs, len, buf);
288069089acSMichael Walle 		else
289*1df1fc8cSTudor Ambarus 			ret = ops->read(nor, rstart + rofs, len, (u8 *)buf);
290069089acSMichael Walle 		if (ret == 0)
291069089acSMichael Walle 			ret = -EIO;
292069089acSMichael Walle 		if (ret < 0)
293069089acSMichael Walle 			goto out;
294069089acSMichael Walle 
295069089acSMichael Walle 		*retlen += ret;
296069089acSMichael Walle 		ofs += ret;
297069089acSMichael Walle 		buf += ret;
298069089acSMichael Walle 		total_len -= ret;
299069089acSMichael Walle 	}
300069089acSMichael Walle 	ret = 0;
301069089acSMichael Walle 
302069089acSMichael Walle out:
303069089acSMichael Walle 	spi_nor_unlock_and_unprep(nor);
304069089acSMichael Walle 	return ret;
305069089acSMichael Walle }
306069089acSMichael Walle 
307069089acSMichael Walle static int spi_nor_mtd_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
308069089acSMichael Walle 				size_t *retlen, u8 *buf)
309069089acSMichael Walle {
310069089acSMichael Walle 	return spi_nor_mtd_otp_read_write(mtd, from, len, retlen, buf, false);
311069089acSMichael Walle }
312069089acSMichael Walle 
313069089acSMichael Walle static int spi_nor_mtd_otp_write(struct mtd_info *mtd, loff_t to, size_t len,
314*1df1fc8cSTudor Ambarus 				 size_t *retlen, const u8 *buf)
315069089acSMichael Walle {
316069089acSMichael Walle 	return spi_nor_mtd_otp_read_write(mtd, to, len, retlen, buf, true);
317069089acSMichael Walle }
318069089acSMichael Walle 
319069089acSMichael Walle static int spi_nor_mtd_otp_lock(struct mtd_info *mtd, loff_t from, size_t len)
320069089acSMichael Walle {
321069089acSMichael Walle 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
322069089acSMichael Walle 	const struct spi_nor_otp_ops *ops = nor->params->otp.ops;
323069089acSMichael Walle 	const size_t rlen = spi_nor_otp_region_len(nor);
324069089acSMichael Walle 	unsigned int region;
325069089acSMichael Walle 	int ret;
326069089acSMichael Walle 
327069089acSMichael Walle 	if (from < 0 || (from + len) > spi_nor_otp_size(nor))
328069089acSMichael Walle 		return -EINVAL;
329069089acSMichael Walle 
330069089acSMichael Walle 	/* the user has to explicitly ask for whole regions */
331069089acSMichael Walle 	if (!IS_ALIGNED(len, rlen) || !IS_ALIGNED(from, rlen))
332069089acSMichael Walle 		return -EINVAL;
333069089acSMichael Walle 
334069089acSMichael Walle 	ret = spi_nor_lock_and_prep(nor);
335069089acSMichael Walle 	if (ret)
336069089acSMichael Walle 		return ret;
337069089acSMichael Walle 
338069089acSMichael Walle 	while (len) {
339069089acSMichael Walle 		region = spi_nor_otp_offset_to_region(nor, from);
340069089acSMichael Walle 		ret = ops->lock(nor, region);
341069089acSMichael Walle 		if (ret)
342069089acSMichael Walle 			goto out;
343069089acSMichael Walle 
344069089acSMichael Walle 		len -= rlen;
345069089acSMichael Walle 		from += rlen;
346069089acSMichael Walle 	}
347069089acSMichael Walle 
348069089acSMichael Walle out:
349069089acSMichael Walle 	spi_nor_unlock_and_unprep(nor);
350069089acSMichael Walle 
351069089acSMichael Walle 	return ret;
352069089acSMichael Walle }
353069089acSMichael Walle 
354069089acSMichael Walle void spi_nor_otp_init(struct spi_nor *nor)
355069089acSMichael Walle {
356069089acSMichael Walle 	struct mtd_info *mtd = &nor->mtd;
357069089acSMichael Walle 
358069089acSMichael Walle 	if (!nor->params->otp.ops)
359069089acSMichael Walle 		return;
360069089acSMichael Walle 
361069089acSMichael Walle 	if (WARN_ON(!is_power_of_2(spi_nor_otp_region_len(nor))))
362069089acSMichael Walle 		return;
363069089acSMichael Walle 
364069089acSMichael Walle 	/*
365069089acSMichael Walle 	 * We only support user_prot callbacks (yet).
366069089acSMichael Walle 	 *
367069089acSMichael Walle 	 * Some SPI NOR flashes like Macronix ones can be ordered in two
368069089acSMichael Walle 	 * different variants. One with a factory locked OTP area and one where
369069089acSMichael Walle 	 * it is left to the user to write to it. The factory locked OTP is
370069089acSMichael Walle 	 * usually preprogrammed with an "electrical serial number". We don't
371069089acSMichael Walle 	 * support these for now.
372069089acSMichael Walle 	 */
373069089acSMichael Walle 	mtd->_get_user_prot_info = spi_nor_mtd_otp_info;
374069089acSMichael Walle 	mtd->_read_user_prot_reg = spi_nor_mtd_otp_read;
375069089acSMichael Walle 	mtd->_write_user_prot_reg = spi_nor_mtd_otp_write;
376069089acSMichael Walle 	mtd->_lock_user_prot_reg = spi_nor_mtd_otp_lock;
377069089acSMichael Walle }
378