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