1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2005, Intec Automation Inc. 4 * Copyright (C) 2014, Freescale Semiconductor, Inc. 5 */ 6 7 #include <linux/mtd/spi-nor.h> 8 9 #include "core.h" 10 11 /* flash_info mfr_flag. Used to read proprietary FSR register. */ 12 #define USE_FSR BIT(0) 13 14 #define SPINOR_OP_RDFSR 0x70 /* Read flag status register */ 15 #define SPINOR_OP_CLFSR 0x50 /* Clear flag status register */ 16 #define SPINOR_OP_MT_DTR_RD 0xfd /* Fast Read opcode in DTR mode */ 17 #define SPINOR_OP_MT_RD_ANY_REG 0x85 /* Read volatile register */ 18 #define SPINOR_OP_MT_WR_ANY_REG 0x81 /* Write volatile register */ 19 #define SPINOR_REG_MT_CFR0V 0x00 /* For setting octal DTR mode */ 20 #define SPINOR_REG_MT_CFR1V 0x01 /* For setting dummy cycles */ 21 #define SPINOR_REG_MT_CFR1V_DEF 0x1f /* Default dummy cycles */ 22 #define SPINOR_MT_OCT_DTR 0xe7 /* Enable Octal DTR. */ 23 #define SPINOR_MT_EXSPI 0xff /* Enable Extended SPI (default) */ 24 25 /* Flag Status Register bits */ 26 #define FSR_READY BIT(7) /* Device status, 0 = Busy, 1 = Ready */ 27 #define FSR_E_ERR BIT(5) /* Erase operation status */ 28 #define FSR_P_ERR BIT(4) /* Program operation status */ 29 #define FSR_PT_ERR BIT(1) /* Protection error bit */ 30 31 /* Micron ST SPI NOR flash operations. */ 32 #define MICRON_ST_NOR_WR_ANY_REG_OP(naddr, addr, ndata, buf) \ 33 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_MT_WR_ANY_REG, 0), \ 34 SPI_MEM_OP_ADDR(naddr, addr, 0), \ 35 SPI_MEM_OP_NO_DUMMY, \ 36 SPI_MEM_OP_DATA_OUT(ndata, buf, 0)) 37 38 #define MICRON_ST_RDFSR_OP(buf) \ 39 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDFSR, 0), \ 40 SPI_MEM_OP_NO_ADDR, \ 41 SPI_MEM_OP_NO_DUMMY, \ 42 SPI_MEM_OP_DATA_IN(1, buf, 0)) 43 44 #define MICRON_ST_CLFSR_OP \ 45 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_CLFSR, 0), \ 46 SPI_MEM_OP_NO_ADDR, \ 47 SPI_MEM_OP_NO_DUMMY, \ 48 SPI_MEM_OP_NO_DATA) 49 50 static int micron_st_nor_octal_dtr_en(struct spi_nor *nor) 51 { 52 struct spi_mem_op op; 53 u8 *buf = nor->bouncebuf; 54 int ret; 55 u8 addr_mode_nbytes = nor->params->addr_mode_nbytes; 56 57 /* Use 20 dummy cycles for memory array reads. */ 58 *buf = 20; 59 op = (struct spi_mem_op) 60 MICRON_ST_NOR_WR_ANY_REG_OP(addr_mode_nbytes, 61 SPINOR_REG_MT_CFR1V, 1, buf); 62 ret = spi_nor_write_any_volatile_reg(nor, &op, nor->reg_proto); 63 if (ret) 64 return ret; 65 66 buf[0] = SPINOR_MT_OCT_DTR; 67 op = (struct spi_mem_op) 68 MICRON_ST_NOR_WR_ANY_REG_OP(addr_mode_nbytes, 69 SPINOR_REG_MT_CFR0V, 1, buf); 70 ret = spi_nor_write_any_volatile_reg(nor, &op, nor->reg_proto); 71 if (ret) 72 return ret; 73 74 /* Read flash ID to make sure the switch was successful. */ 75 ret = spi_nor_read_id(nor, 0, 8, buf, SNOR_PROTO_8_8_8_DTR); 76 if (ret) { 77 dev_dbg(nor->dev, "error %d reading JEDEC ID after enabling 8D-8D-8D mode\n", ret); 78 return ret; 79 } 80 81 if (memcmp(buf, nor->info->id, nor->info->id_len)) 82 return -EINVAL; 83 84 return 0; 85 } 86 87 static int micron_st_nor_octal_dtr_dis(struct spi_nor *nor) 88 { 89 struct spi_mem_op op; 90 u8 *buf = nor->bouncebuf; 91 int ret; 92 93 /* 94 * The register is 1-byte wide, but 1-byte transactions are not allowed 95 * in 8D-8D-8D mode. The next register is the dummy cycle configuration 96 * register. Since the transaction needs to be at least 2 bytes wide, 97 * set the next register to its default value. This also makes sense 98 * because the value was changed when enabling 8D-8D-8D mode, it should 99 * be reset when disabling. 100 */ 101 buf[0] = SPINOR_MT_EXSPI; 102 buf[1] = SPINOR_REG_MT_CFR1V_DEF; 103 op = (struct spi_mem_op) 104 MICRON_ST_NOR_WR_ANY_REG_OP(nor->addr_nbytes, 105 SPINOR_REG_MT_CFR0V, 2, buf); 106 ret = spi_nor_write_any_volatile_reg(nor, &op, SNOR_PROTO_8_8_8_DTR); 107 if (ret) 108 return ret; 109 110 /* Read flash ID to make sure the switch was successful. */ 111 ret = spi_nor_read_id(nor, 0, 0, buf, SNOR_PROTO_1_1_1); 112 if (ret) { 113 dev_dbg(nor->dev, "error %d reading JEDEC ID after disabling 8D-8D-8D mode\n", ret); 114 return ret; 115 } 116 117 if (memcmp(buf, nor->info->id, nor->info->id_len)) 118 return -EINVAL; 119 120 return 0; 121 } 122 123 static int micron_st_nor_octal_dtr_enable(struct spi_nor *nor, bool enable) 124 { 125 return enable ? micron_st_nor_octal_dtr_en(nor) : 126 micron_st_nor_octal_dtr_dis(nor); 127 } 128 129 static void mt35xu512aba_default_init(struct spi_nor *nor) 130 { 131 nor->params->octal_dtr_enable = micron_st_nor_octal_dtr_enable; 132 } 133 134 static void mt35xu512aba_post_sfdp_fixup(struct spi_nor *nor) 135 { 136 /* Set the Fast Read settings. */ 137 nor->params->hwcaps.mask |= SNOR_HWCAPS_READ_8_8_8_DTR; 138 spi_nor_set_read_settings(&nor->params->reads[SNOR_CMD_READ_8_8_8_DTR], 139 0, 20, SPINOR_OP_MT_DTR_RD, 140 SNOR_PROTO_8_8_8_DTR); 141 142 nor->cmd_ext_type = SPI_NOR_EXT_REPEAT; 143 nor->params->rdsr_dummy = 8; 144 nor->params->rdsr_addr_nbytes = 0; 145 146 /* 147 * The BFPT quad enable field is set to a reserved value so the quad 148 * enable function is ignored by spi_nor_parse_bfpt(). Make sure we 149 * disable it. 150 */ 151 nor->params->quad_enable = NULL; 152 } 153 154 static const struct spi_nor_fixups mt35xu512aba_fixups = { 155 .default_init = mt35xu512aba_default_init, 156 .post_sfdp = mt35xu512aba_post_sfdp_fixup, 157 }; 158 159 static const struct flash_info micron_nor_parts[] = { 160 { "mt35xu512aba", INFO(0x2c5b1a, 0, 128 * 1024, 512) 161 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_OCTAL_READ | 162 SPI_NOR_OCTAL_DTR_READ | SPI_NOR_OCTAL_DTR_PP) 163 FIXUP_FLAGS(SPI_NOR_4B_OPCODES | SPI_NOR_IO_MODE_EN_VOLATILE) 164 MFR_FLAGS(USE_FSR) 165 .fixups = &mt35xu512aba_fixups 166 }, 167 { "mt35xu02g", INFO(0x2c5b1c, 0, 128 * 1024, 2048) 168 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_OCTAL_READ) 169 FIXUP_FLAGS(SPI_NOR_4B_OPCODES) 170 MFR_FLAGS(USE_FSR) 171 }, 172 }; 173 174 static const struct flash_info st_nor_parts[] = { 175 { "n25q016a", INFO(0x20bb15, 0, 64 * 1024, 32) 176 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_QUAD_READ) }, 177 { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64) 178 NO_SFDP_FLAGS(SPI_NOR_QUAD_READ) }, 179 { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64) 180 NO_SFDP_FLAGS(SPI_NOR_QUAD_READ) }, 181 { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128) 182 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_QUAD_READ) }, 183 { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128) 184 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_QUAD_READ) }, 185 { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256) 186 FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB | SPI_NOR_4BIT_BP | 187 SPI_NOR_BP3_SR_BIT6) 188 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_QUAD_READ) 189 MFR_FLAGS(USE_FSR) 190 }, 191 { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256) 192 FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB | SPI_NOR_4BIT_BP | 193 SPI_NOR_BP3_SR_BIT6) 194 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_QUAD_READ) 195 MFR_FLAGS(USE_FSR) 196 }, 197 { "mt25ql256a", INFO6(0x20ba19, 0x104400, 64 * 1024, 512) 198 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) 199 FIXUP_FLAGS(SPI_NOR_4B_OPCODES) 200 MFR_FLAGS(USE_FSR) 201 }, 202 { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512) 203 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ | 204 SPI_NOR_QUAD_READ) 205 MFR_FLAGS(USE_FSR) 206 }, 207 { "mt25qu256a", INFO6(0x20bb19, 0x104400, 64 * 1024, 512) 208 FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB | SPI_NOR_4BIT_BP | 209 SPI_NOR_BP3_SR_BIT6) 210 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) 211 FIXUP_FLAGS(SPI_NOR_4B_OPCODES) 212 MFR_FLAGS(USE_FSR) 213 }, 214 { "n25q256ax1", INFO(0x20bb19, 0, 64 * 1024, 512) 215 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_QUAD_READ) 216 MFR_FLAGS(USE_FSR) 217 }, 218 { "mt25ql512a", INFO6(0x20ba20, 0x104400, 64 * 1024, 1024) 219 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) 220 FIXUP_FLAGS(SPI_NOR_4B_OPCODES) 221 MFR_FLAGS(USE_FSR) 222 }, 223 { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024) 224 FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB | SPI_NOR_4BIT_BP | 225 SPI_NOR_BP3_SR_BIT6) 226 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_QUAD_READ) 227 MFR_FLAGS(USE_FSR) 228 }, 229 { "mt25qu512a", INFO6(0x20bb20, 0x104400, 64 * 1024, 1024) 230 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) 231 FIXUP_FLAGS(SPI_NOR_4B_OPCODES) 232 MFR_FLAGS(USE_FSR) 233 }, 234 { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024) 235 FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB | SPI_NOR_4BIT_BP | 236 SPI_NOR_BP3_SR_BIT6) 237 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_QUAD_READ) 238 MFR_FLAGS(USE_FSR) 239 }, 240 { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048) 241 FLAGS(SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB | SPI_NOR_4BIT_BP | 242 SPI_NOR_BP3_SR_BIT6 | NO_CHIP_ERASE) 243 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_QUAD_READ) 244 MFR_FLAGS(USE_FSR) 245 }, 246 { "n25q00a", INFO(0x20bb21, 0, 64 * 1024, 2048) 247 FLAGS(NO_CHIP_ERASE) 248 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_QUAD_READ) 249 MFR_FLAGS(USE_FSR) 250 }, 251 { "mt25ql02g", INFO(0x20ba22, 0, 64 * 1024, 4096) 252 FLAGS(NO_CHIP_ERASE) 253 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_QUAD_READ) 254 MFR_FLAGS(USE_FSR) 255 }, 256 { "mt25qu02g", INFO(0x20bb22, 0, 64 * 1024, 4096) 257 FLAGS(NO_CHIP_ERASE) 258 NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ | 259 SPI_NOR_QUAD_READ) 260 MFR_FLAGS(USE_FSR) 261 }, 262 263 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2) }, 264 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4) }, 265 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4) }, 266 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8) }, 267 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16) }, 268 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32) }, 269 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64) }, 270 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128) }, 271 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64) }, 272 273 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2) }, 274 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4) }, 275 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4) }, 276 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8) }, 277 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16) }, 278 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32) }, 279 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64) }, 280 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128) }, 281 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64) }, 282 283 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2) }, 284 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16) }, 285 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32) }, 286 287 { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4) }, 288 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16) }, 289 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32) 290 NO_SFDP_FLAGS(SECT_4K) }, 291 292 { "m25px16", INFO(0x207115, 0, 64 * 1024, 32) 293 NO_SFDP_FLAGS(SECT_4K) }, 294 { "m25px32", INFO(0x207116, 0, 64 * 1024, 64) 295 NO_SFDP_FLAGS(SECT_4K) }, 296 { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64) 297 NO_SFDP_FLAGS(SECT_4K) }, 298 { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64) 299 NO_SFDP_FLAGS(SECT_4K) }, 300 { "m25px64", INFO(0x207117, 0, 64 * 1024, 128) }, 301 { "m25px80", INFO(0x207114, 0, 64 * 1024, 16) }, 302 }; 303 304 /** 305 * micron_st_nor_set_4byte_addr_mode() - Set 4-byte address mode for ST and 306 * Micron flashes. 307 * @nor: pointer to 'struct spi_nor'. 308 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte 309 * address mode. 310 * 311 * Return: 0 on success, -errno otherwise. 312 */ 313 static int micron_st_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable) 314 { 315 int ret; 316 317 ret = spi_nor_write_enable(nor); 318 if (ret) 319 return ret; 320 321 ret = spi_nor_set_4byte_addr_mode(nor, enable); 322 if (ret) 323 return ret; 324 325 return spi_nor_write_disable(nor); 326 } 327 328 /** 329 * micron_st_nor_read_fsr() - Read the Flag Status Register. 330 * @nor: pointer to 'struct spi_nor' 331 * @fsr: pointer to a DMA-able buffer where the value of the 332 * Flag Status Register will be written. Should be at least 2 333 * bytes. 334 * 335 * Return: 0 on success, -errno otherwise. 336 */ 337 static int micron_st_nor_read_fsr(struct spi_nor *nor, u8 *fsr) 338 { 339 int ret; 340 341 if (nor->spimem) { 342 struct spi_mem_op op = MICRON_ST_RDFSR_OP(fsr); 343 344 if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) { 345 op.addr.nbytes = nor->params->rdsr_addr_nbytes; 346 op.dummy.nbytes = nor->params->rdsr_dummy; 347 /* 348 * We don't want to read only one byte in DTR mode. So, 349 * read 2 and then discard the second byte. 350 */ 351 op.data.nbytes = 2; 352 } 353 354 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); 355 356 ret = spi_mem_exec_op(nor->spimem, &op); 357 } else { 358 ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDFSR, fsr, 359 1); 360 } 361 362 if (ret) 363 dev_dbg(nor->dev, "error %d reading FSR\n", ret); 364 365 return ret; 366 } 367 368 /** 369 * micron_st_nor_clear_fsr() - Clear the Flag Status Register. 370 * @nor: pointer to 'struct spi_nor'. 371 */ 372 static void micron_st_nor_clear_fsr(struct spi_nor *nor) 373 { 374 int ret; 375 376 if (nor->spimem) { 377 struct spi_mem_op op = MICRON_ST_CLFSR_OP; 378 379 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); 380 381 ret = spi_mem_exec_op(nor->spimem, &op); 382 } else { 383 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_CLFSR, 384 NULL, 0); 385 } 386 387 if (ret) 388 dev_dbg(nor->dev, "error %d clearing FSR\n", ret); 389 } 390 391 /** 392 * micron_st_nor_ready() - Query the Status Register as well as the Flag Status 393 * Register to see if the flash is ready for new commands. If there are any 394 * errors in the FSR clear them. 395 * @nor: pointer to 'struct spi_nor'. 396 * 397 * Return: 1 if ready, 0 if not ready, -errno on errors. 398 */ 399 static int micron_st_nor_ready(struct spi_nor *nor) 400 { 401 int sr_ready, ret; 402 403 sr_ready = spi_nor_sr_ready(nor); 404 if (sr_ready < 0) 405 return sr_ready; 406 407 ret = micron_st_nor_read_fsr(nor, nor->bouncebuf); 408 if (ret) { 409 /* 410 * Some controllers, such as Intel SPI, do not support low 411 * level operations such as reading the flag status 412 * register. They only expose small amount of high level 413 * operations to the software. If this is the case we use 414 * only the status register value. 415 */ 416 return ret == -EOPNOTSUPP ? sr_ready : ret; 417 } 418 419 if (nor->bouncebuf[0] & (FSR_E_ERR | FSR_P_ERR)) { 420 if (nor->bouncebuf[0] & FSR_E_ERR) 421 dev_err(nor->dev, "Erase operation failed.\n"); 422 else 423 dev_err(nor->dev, "Program operation failed.\n"); 424 425 if (nor->bouncebuf[0] & FSR_PT_ERR) 426 dev_err(nor->dev, 427 "Attempted to modify a protected sector.\n"); 428 429 micron_st_nor_clear_fsr(nor); 430 431 /* 432 * WEL bit remains set to one when an erase or page program 433 * error occurs. Issue a Write Disable command to protect 434 * against inadvertent writes that can possibly corrupt the 435 * contents of the memory. 436 */ 437 ret = spi_nor_write_disable(nor); 438 if (ret) 439 return ret; 440 441 return -EIO; 442 } 443 444 return sr_ready && !!(nor->bouncebuf[0] & FSR_READY); 445 } 446 447 static void micron_st_nor_default_init(struct spi_nor *nor) 448 { 449 nor->flags |= SNOR_F_HAS_LOCK; 450 nor->flags &= ~SNOR_F_HAS_16BIT_SR; 451 nor->params->quad_enable = NULL; 452 nor->params->set_4byte_addr_mode = micron_st_nor_set_4byte_addr_mode; 453 } 454 455 static void micron_st_nor_late_init(struct spi_nor *nor) 456 { 457 if (nor->info->mfr_flags & USE_FSR) 458 nor->params->ready = micron_st_nor_ready; 459 } 460 461 static const struct spi_nor_fixups micron_st_nor_fixups = { 462 .default_init = micron_st_nor_default_init, 463 .late_init = micron_st_nor_late_init, 464 }; 465 466 const struct spi_nor_manufacturer spi_nor_micron = { 467 .name = "micron", 468 .parts = micron_nor_parts, 469 .nparts = ARRAY_SIZE(micron_nor_parts), 470 .fixups = µn_st_nor_fixups, 471 }; 472 473 const struct spi_nor_manufacturer spi_nor_st = { 474 .name = "st", 475 .parts = st_nor_parts, 476 .nparts = ARRAY_SIZE(st_nor_parts), 477 .fixups = µn_st_nor_fixups, 478 }; 479