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