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