1 // SPDX-License-Identifier: GPL-2.0
3 * OTP support for SPI NOR flashes
5 * Copyright (C) 2021 Michael Walle <michael@walle.cc>
8 #include <linux/log2.h>
9 #include <linux/mtd/mtd.h>
10 #include <linux/mtd/spi-nor.h>
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)
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
24 * Read OTP data from one region by using the SPINOR_OP_RSECR commands. This
25 * method is used on GigaDevice and Winbond flashes.
27 * Return: number of bytes read successfully, -errno otherwise
29 int spi_nor_otp_read_secr(struct spi_nor *nor, loff_t addr, size_t len, u8 *buf)
31 u8 addr_width, read_opcode, read_dummy;
32 struct spi_mem_dirmap_desc *rdesc;
33 enum spi_nor_protocol read_proto;
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;
42 nor->read_opcode = SPINOR_OP_RSECR;
45 nor->read_proto = SNOR_PROTO_1_1_1;
46 nor->dirmap.rdesc = NULL;
48 ret = spi_nor_read_data(nor, addr, len, buf);
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;
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
66 * Write OTP data to one region by using the SPINOR_OP_PSECR commands. This
67 * method is used on GigaDevice and Winbond flashes.
69 * Please note, the write must not span multiple OTP regions.
71 * Return: number of bytes written successfully, -errno otherwise
73 int spi_nor_otp_write_secr(struct spi_nor *nor, loff_t addr, size_t len,
76 enum spi_nor_protocol write_proto;
77 struct spi_mem_dirmap_desc *wdesc;
78 u8 addr_width, program_opcode;
81 program_opcode = nor->program_opcode;
82 addr_width = nor->addr_width;
83 write_proto = nor->write_proto;
84 wdesc = nor->dirmap.wdesc;
86 nor->program_opcode = SPINOR_OP_PSECR;
88 nor->write_proto = SNOR_PROTO_1_1_1;
89 nor->dirmap.wdesc = NULL;
92 * We only support a write to one single page. For now all winbond
93 * flashes only have one page per OTP region.
95 ret = spi_nor_write_enable(nor);
99 written = spi_nor_write_data(nor, addr, len, buf);
103 ret = spi_nor_wait_till_ready(nor);
106 nor->program_opcode = program_opcode;
107 nor->addr_width = addr_width;
108 nor->write_proto = write_proto;
109 nor->dirmap.wdesc = wdesc;
111 return ret ?: written;
114 static int spi_nor_otp_lock_bit_cr(unsigned int region)
116 static const int lock_bits[] = { SR2_LB1, SR2_LB2, SR2_LB3 };
118 if (region >= ARRAY_SIZE(lock_bits))
121 return lock_bits[region];
125 * spi_nor_otp_lock_sr2() - lock the OTP region
126 * @nor: pointer to 'struct spi_nor'
127 * @region: OTP region
129 * Lock the OTP region by writing the status register-2. This method is used on
130 * GigaDevice and Winbond flashes.
132 * Return: 0 on success, -errno otherwise.
134 int spi_nor_otp_lock_sr2(struct spi_nor *nor, unsigned int region)
136 u8 *cr = nor->bouncebuf;
139 lock_bit = spi_nor_otp_lock_bit_cr(region);
143 ret = spi_nor_read_cr(nor, cr);
147 /* no need to write the register if region is already locked */
148 if (cr[0] & lock_bit)
153 return spi_nor_write_16bit_cr_and_check(nor, cr[0]);
157 * spi_nor_otp_is_locked_sr2() - get the OTP region lock status
158 * @nor: pointer to 'struct spi_nor'
159 * @region: OTP region
161 * Retrieve the OTP region lock bit by reading the status register-2. This
162 * method is used on GigaDevice and Winbond flashes.
164 * Return: 0 on success, -errno otherwise.
166 int spi_nor_otp_is_locked_sr2(struct spi_nor *nor, unsigned int region)
168 u8 *cr = nor->bouncebuf;
171 lock_bit = spi_nor_otp_lock_bit_cr(region);
175 ret = spi_nor_read_cr(nor, cr);
179 return cr[0] & lock_bit;
182 static loff_t spi_nor_otp_region_start(const struct spi_nor *nor, unsigned int region)
184 const struct spi_nor_otp_organization *org = nor->params->otp.org;
186 return org->base + region * org->offset;
189 static size_t spi_nor_otp_size(struct spi_nor *nor)
191 return spi_nor_otp_n_regions(nor) * spi_nor_otp_region_len(nor);
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)
197 return region * spi_nor_otp_region_len(nor);
200 static unsigned int spi_nor_otp_offset_to_region(struct spi_nor *nor, loff_t ofs)
202 return div64_u64(ofs, spi_nor_otp_region_len(nor));
205 static int spi_nor_mtd_otp_info(struct mtd_info *mtd, size_t len,
206 size_t *retlen, struct otp_info *buf)
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);
214 if (len < n_regions * sizeof(*buf))
217 ret = spi_nor_lock_and_prep(nor);
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);
225 locked = ops->is_locked(nor, i);
231 buf->locked = !!locked;
235 *retlen = n_regions * sizeof(*buf);
238 spi_nor_unlock_and_unprep(nor);
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)
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);
255 if (ofs < 0 || ofs >= spi_nor_otp_size(nor))
258 ret = spi_nor_lock_and_prep(nor);
262 /* don't access beyond the end */
263 total_len = min_t(size_t, total_len, spi_nor_otp_size(nor) - ofs);
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.
273 region = spi_nor_otp_offset_to_region(nor, ofs);
274 rstart = spi_nor_otp_region_start(nor, region);
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
281 rofs = ofs & (rlen - 1);
283 /* don't access beyond one OTP region */
284 len = min_t(size_t, total_len, rlen - rofs);
287 ret = ops->write(nor, rstart + rofs, len, buf);
289 ret = ops->read(nor, rstart + rofs, len, (u8 *)buf);
303 spi_nor_unlock_and_unprep(nor);
307 static int spi_nor_mtd_otp_read(struct mtd_info *mtd, loff_t from, size_t len,
308 size_t *retlen, u8 *buf)
310 return spi_nor_mtd_otp_read_write(mtd, from, len, retlen, buf, false);
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)
316 return spi_nor_mtd_otp_read_write(mtd, to, len, retlen, buf, true);
319 static int spi_nor_mtd_otp_lock(struct mtd_info *mtd, loff_t from, size_t len)
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);
327 if (from < 0 || (from + len) > spi_nor_otp_size(nor))
330 /* the user has to explicitly ask for whole regions */
331 if (!IS_ALIGNED(len, rlen) || !IS_ALIGNED(from, rlen))
334 ret = spi_nor_lock_and_prep(nor);
339 region = spi_nor_otp_offset_to_region(nor, from);
340 ret = ops->lock(nor, region);
349 spi_nor_unlock_and_unprep(nor);
354 void spi_nor_otp_init(struct spi_nor *nor)
356 struct mtd_info *mtd = &nor->mtd;
358 if (!nor->params->otp.ops)
361 if (WARN_ON(!is_power_of_2(spi_nor_otp_region_len(nor))))
365 * We only support user_prot callbacks (yet).
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.
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;