1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Based on m25p80.c, by Mike Lavender (mike@steroidmicros.com), with 4 * influence from lart.c (Abraham Van Der Merwe) and mtd_dataflash.c 5 * 6 * Copyright (C) 2005, Intec Automation Inc. 7 * Copyright (C) 2014, Freescale Semiconductor, Inc. 8 * 9 * Synced from Linux v4.19 10 */ 11 12 #include <common.h> 13 #include <linux/err.h> 14 #include <linux/errno.h> 15 #include <linux/log2.h> 16 #include <linux/math64.h> 17 #include <linux/sizes.h> 18 19 #include <linux/mtd/mtd.h> 20 #include <linux/mtd/spi-nor.h> 21 #include <spi-mem.h> 22 #include <spi.h> 23 24 #include "sf_internal.h" 25 26 /* Define max times to check status register before we give up. */ 27 28 /* 29 * For everything but full-chip erase; probably could be much smaller, but kept 30 * around for safety for now 31 */ 32 33 #define HZ CONFIG_SYS_HZ 34 35 #define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ) 36 37 /** 38 * spi_nor_setup_op() - Set up common properties of a spi-mem op. 39 * @nor: pointer to a 'struct spi_nor' 40 * @op: pointer to the 'struct spi_mem_op' whose properties 41 * need to be initialized. 42 * @proto: the protocol from which the properties need to be set. 43 */ 44 void spi_nor_setup_op(const struct spi_nor *nor, 45 struct spi_mem_op *op, 46 const enum spi_nor_protocol proto) 47 { 48 op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(proto); 49 50 if (op->addr.nbytes) 51 op->addr.buswidth = spi_nor_get_protocol_addr_nbits(proto); 52 53 if (op->dummy.nbytes) 54 op->dummy.buswidth = spi_nor_get_protocol_addr_nbits(proto); 55 56 if (op->data.nbytes) 57 op->data.buswidth = spi_nor_get_protocol_data_nbits(proto); 58 } 59 60 static int spi_nor_read_write_reg(struct spi_nor *nor, struct spi_mem_op 61 *op, void *buf) 62 { 63 if (op->data.dir == SPI_MEM_DATA_IN) 64 op->data.buf.in = buf; 65 else 66 op->data.buf.out = buf; 67 return spi_mem_exec_op(nor->spi, op); 68 } 69 70 static int spi_nor_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len) 71 { 72 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(code, 1), 73 SPI_MEM_OP_NO_ADDR, 74 SPI_MEM_OP_NO_DUMMY, 75 SPI_MEM_OP_DATA_IN(len, NULL, 1)); 76 int ret; 77 78 ret = spi_nor_read_write_reg(nor, &op, val); 79 if (ret < 0) 80 dev_dbg(&flash->spimem->spi->dev, "error %d reading %x\n", ret, 81 code); 82 83 return ret; 84 } 85 86 static int spi_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len) 87 { 88 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 1), 89 SPI_MEM_OP_NO_ADDR, 90 SPI_MEM_OP_NO_DUMMY, 91 SPI_MEM_OP_DATA_OUT(len, NULL, 1)); 92 93 return spi_nor_read_write_reg(nor, &op, buf); 94 } 95 96 #ifdef CONFIG_SPI_FLASH_SPANSION 97 static int spansion_read_any_reg(struct spi_nor *nor, u32 addr, u8 dummy, 98 u8 *val) 99 { 100 int ret; 101 u8 dummy_ori; 102 struct spi_mem_op op = 103 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDAR, 1), 104 SPI_MEM_OP_ADDR(nor->addr_width, addr, 1), 105 SPI_MEM_OP_DUMMY(dummy / 8, 1), 106 SPI_MEM_OP_DATA_IN(1, NULL, 1)); 107 108 dummy_ori = nor->read_dummy; 109 nor->read_dummy = dummy; 110 111 ret = spi_nor_read_write_reg(nor, &op, val); 112 113 nor->read_dummy = dummy_ori; 114 115 return ret; 116 } 117 118 static int spansion_write_any_reg(struct spi_nor *nor, u32 addr, u8 val) 119 { 120 struct spi_mem_op op = 121 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRAR, 1), 122 SPI_MEM_OP_ADDR(nor->addr_width, addr, 1), 123 SPI_MEM_OP_NO_DUMMY, 124 SPI_MEM_OP_DATA_OUT(1, NULL, 1)); 125 126 return spi_nor_read_write_reg(nor, &op, &val); 127 } 128 #endif 129 130 static ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len, 131 u_char *buf) 132 { 133 struct spi_mem_op op = 134 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1), 135 SPI_MEM_OP_ADDR(nor->addr_width, from, 1), 136 SPI_MEM_OP_DUMMY(nor->read_dummy, 1), 137 SPI_MEM_OP_DATA_IN(len, buf, 1)); 138 size_t remaining = len; 139 int ret; 140 141 /* get transfer protocols. */ 142 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto); 143 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto); 144 op.dummy.buswidth = op.addr.buswidth; 145 op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto); 146 147 /* convert the dummy cycles to the number of bytes */ 148 op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8; 149 150 while (remaining) { 151 op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX; 152 ret = spi_mem_adjust_op_size(nor->spi, &op); 153 if (ret) 154 return ret; 155 156 ret = spi_mem_exec_op(nor->spi, &op); 157 if (ret) 158 return ret; 159 160 op.addr.val += op.data.nbytes; 161 remaining -= op.data.nbytes; 162 op.data.buf.in += op.data.nbytes; 163 } 164 165 return len; 166 } 167 168 static ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len, 169 const u_char *buf) 170 { 171 struct spi_mem_op op = 172 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1), 173 SPI_MEM_OP_ADDR(nor->addr_width, to, 1), 174 SPI_MEM_OP_NO_DUMMY, 175 SPI_MEM_OP_DATA_OUT(len, buf, 1)); 176 size_t remaining = len; 177 int ret; 178 179 /* get transfer protocols. */ 180 op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto); 181 op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto); 182 op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto); 183 184 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second) 185 op.addr.nbytes = 0; 186 187 while (remaining) { 188 op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX; 189 ret = spi_mem_adjust_op_size(nor->spi, &op); 190 if (ret) 191 return ret; 192 193 ret = spi_mem_exec_op(nor->spi, &op); 194 if (ret) 195 return ret; 196 197 op.addr.val += op.data.nbytes; 198 remaining -= op.data.nbytes; 199 op.data.buf.out += op.data.nbytes; 200 } 201 202 return len; 203 } 204 205 /* 206 * Read the status register, returning its value in the location 207 * Return the status register value. 208 * Returns negative if error occurred. 209 */ 210 static int read_sr(struct spi_nor *nor) 211 { 212 int ret; 213 u8 val; 214 215 ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1); 216 if (ret < 0) { 217 pr_debug("error %d reading SR\n", (int)ret); 218 return ret; 219 } 220 221 return val; 222 } 223 224 static int read_winbond_sr2(struct spi_nor *nor) 225 { 226 int ret; 227 u8 val; 228 229 ret = nor->read_reg(nor, SPINOR_OP_WINBOND_RDSR2, &val, 1); 230 if (ret < 0) { 231 pr_debug("error %d reading SR2\n", (int)ret); 232 return ret; 233 } 234 235 return val; 236 } 237 238 /* 239 * Read the flag status register, returning its value in the location 240 * Return the status register value. 241 * Returns negative if error occurred. 242 */ 243 static int read_fsr(struct spi_nor *nor) 244 { 245 int ret; 246 u8 val; 247 248 ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1); 249 if (ret < 0) { 250 pr_debug("error %d reading FSR\n", ret); 251 return ret; 252 } 253 254 return val; 255 } 256 257 /* 258 * Read configuration register, returning its value in the 259 * location. Return the configuration register value. 260 * Returns negative if error occurred. 261 */ 262 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND) 263 static int read_cr(struct spi_nor *nor) 264 { 265 int ret; 266 u8 val; 267 268 ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1); 269 if (ret < 0) { 270 dev_dbg(nor->dev, "error %d reading CR\n", ret); 271 return ret; 272 } 273 274 return val; 275 } 276 #endif 277 278 /* 279 * Write status register 1 byte 280 * Returns negative if error occurred. 281 */ 282 static int write_sr(struct spi_nor *nor, u8 val) 283 { 284 nor->cmd_buf[0] = val; 285 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1); 286 } 287 288 static int write_winbond_sr2(struct spi_nor *nor, u8 val) 289 { 290 nor->cmd_buf[0] = val; 291 return nor->write_reg(nor, SPINOR_OP_WINBOND_WRSR2, nor->cmd_buf, 1); 292 } 293 294 /* 295 * Set write enable latch with Write Enable command. 296 * Returns negative if error occurred. 297 */ 298 static int write_enable(struct spi_nor *nor) 299 { 300 return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0); 301 } 302 303 /* 304 * Send write disable instruction to the chip. 305 */ 306 static int write_disable(struct spi_nor *nor) 307 { 308 return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0); 309 } 310 311 static struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd) 312 { 313 return mtd->priv; 314 } 315 316 #ifndef CONFIG_SPI_FLASH_BAR 317 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size) 318 { 319 size_t i; 320 321 for (i = 0; i < size; i++) 322 if (table[i][0] == opcode) 323 return table[i][1]; 324 325 /* No conversion found, keep input op code. */ 326 return opcode; 327 } 328 329 static u8 spi_nor_convert_3to4_read(u8 opcode) 330 { 331 static const u8 spi_nor_3to4_read[][2] = { 332 { SPINOR_OP_READ, SPINOR_OP_READ_4B }, 333 { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B }, 334 { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B }, 335 { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B }, 336 { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B }, 337 { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B }, 338 339 { SPINOR_OP_READ_1_1_1_DTR, SPINOR_OP_READ_1_1_1_DTR_4B }, 340 { SPINOR_OP_READ_1_2_2_DTR, SPINOR_OP_READ_1_2_2_DTR_4B }, 341 { SPINOR_OP_READ_1_4_4_DTR, SPINOR_OP_READ_1_4_4_DTR_4B }, 342 }; 343 344 return spi_nor_convert_opcode(opcode, spi_nor_3to4_read, 345 ARRAY_SIZE(spi_nor_3to4_read)); 346 } 347 348 static u8 spi_nor_convert_3to4_program(u8 opcode) 349 { 350 static const u8 spi_nor_3to4_program[][2] = { 351 { SPINOR_OP_PP, SPINOR_OP_PP_4B }, 352 { SPINOR_OP_PP_1_1_4, SPINOR_OP_PP_1_1_4_4B }, 353 { SPINOR_OP_PP_1_4_4, SPINOR_OP_PP_1_4_4_4B }, 354 }; 355 356 return spi_nor_convert_opcode(opcode, spi_nor_3to4_program, 357 ARRAY_SIZE(spi_nor_3to4_program)); 358 } 359 360 static u8 spi_nor_convert_3to4_erase(u8 opcode) 361 { 362 static const u8 spi_nor_3to4_erase[][2] = { 363 { SPINOR_OP_BE_4K, SPINOR_OP_BE_4K_4B }, 364 { SPINOR_OP_BE_32K, SPINOR_OP_BE_32K_4B }, 365 { SPINOR_OP_SE, SPINOR_OP_SE_4B }, 366 }; 367 368 return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase, 369 ARRAY_SIZE(spi_nor_3to4_erase)); 370 } 371 372 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor, 373 const struct flash_info *info) 374 { 375 /* Do some manufacturer fixups first */ 376 switch (JEDEC_MFR(info)) { 377 case SNOR_MFR_SPANSION: 378 case SNOR_MFR_CYPRESS: 379 /* No small sector erase for 4-byte command set */ 380 nor->erase_opcode = SPINOR_OP_SE; 381 nor->mtd.erasesize = info->sector_size; 382 break; 383 384 default: 385 break; 386 } 387 388 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode); 389 nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode); 390 nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode); 391 } 392 #endif /* !CONFIG_SPI_FLASH_BAR */ 393 394 /* Enable/disable 4-byte addressing mode. */ 395 static int set_4byte(struct spi_nor *nor, const struct flash_info *info, 396 int enable) 397 { 398 int status; 399 bool need_wren = false; 400 u8 cmd; 401 402 switch (JEDEC_MFR(info)) { 403 case SNOR_MFR_ST: 404 case SNOR_MFR_MICRON: 405 /* Some Micron need WREN command; all will accept it */ 406 need_wren = true; 407 case SNOR_MFR_MACRONIX: 408 case SNOR_MFR_WINBOND: 409 case SNOR_MFR_GIGADEVICE: 410 case SNOR_MFR_ISSI: 411 case SNOR_MFR_CYPRESS: 412 if (need_wren) 413 write_enable(nor); 414 415 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B; 416 status = nor->write_reg(nor, cmd, NULL, 0); 417 if (need_wren) 418 write_disable(nor); 419 420 if (!status && !enable && 421 JEDEC_MFR(info) == SNOR_MFR_WINBOND) { 422 /* 423 * On Winbond W25Q256FV, leaving 4byte mode causes 424 * the Extended Address Register to be set to 1, so all 425 * 3-byte-address reads come from the second 16M. 426 * We must clear the register to enable normal behavior. 427 */ 428 write_enable(nor); 429 nor->cmd_buf[0] = 0; 430 nor->write_reg(nor, SPINOR_OP_WREAR, nor->cmd_buf, 1); 431 write_disable(nor); 432 } 433 434 return status; 435 default: 436 /* Spansion style */ 437 nor->cmd_buf[0] = enable << 7; 438 return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1); 439 } 440 } 441 442 static int spi_nor_sr_ready(struct spi_nor *nor) 443 { 444 int sr = read_sr(nor); 445 446 if (sr < 0) 447 return sr; 448 449 if (nor->flags & SNOR_F_USE_CLSR && sr & (SR_E_ERR | SR_P_ERR)) { 450 if (sr & SR_E_ERR) 451 dev_dbg(nor->dev, "Erase Error occurred\n"); 452 else 453 dev_dbg(nor->dev, "Programming Error occurred\n"); 454 455 nor->write_reg(nor, SPINOR_OP_CLSR, NULL, 0); 456 return -EIO; 457 } 458 459 return !(sr & SR_WIP); 460 } 461 462 static int spi_nor_fsr_ready(struct spi_nor *nor) 463 { 464 int fsr = read_fsr(nor); 465 466 if (fsr < 0) 467 return fsr; 468 469 if (fsr & (FSR_E_ERR | FSR_P_ERR)) { 470 if (fsr & FSR_E_ERR) 471 dev_dbg(nor->dev, "Erase operation failed.\n"); 472 else 473 dev_dbg(nor->dev, "Program operation failed.\n"); 474 475 if (fsr & FSR_PT_ERR) 476 dev_dbg(nor->dev, 477 "Attempted to modify a protected sector.\n"); 478 479 nor->write_reg(nor, SPINOR_OP_CLFSR, NULL, 0); 480 return -EIO; 481 } 482 483 return fsr & FSR_READY; 484 } 485 486 static int spi_nor_ready(struct spi_nor *nor) 487 { 488 int sr, fsr; 489 490 sr = spi_nor_sr_ready(nor); 491 if (sr < 0) 492 return sr; 493 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1; 494 if (fsr < 0) 495 return fsr; 496 return sr && fsr; 497 } 498 499 /* 500 * Service routine to read status register until ready, or timeout occurs. 501 * Returns non-zero if error. 502 */ 503 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor, 504 unsigned long timeout) 505 { 506 unsigned long timebase; 507 int ret; 508 509 timebase = get_timer(0); 510 511 while (get_timer(timebase) < timeout) { 512 ret = spi_nor_ready(nor); 513 if (ret < 0) 514 return ret; 515 if (ret) 516 return 0; 517 } 518 519 dev_err(nor->dev, "flash operation timed out\n"); 520 521 return -ETIMEDOUT; 522 } 523 524 static int spi_nor_wait_till_ready(struct spi_nor *nor) 525 { 526 return spi_nor_wait_till_ready_with_timeout(nor, 527 DEFAULT_READY_WAIT_JIFFIES); 528 } 529 530 #ifdef CONFIG_SPI_FLASH_BAR 531 /* 532 * This "clean_bar" is necessary in a situation when one was accessing 533 * spi flash memory > 16 MiB by using Bank Address Register's BA24 bit. 534 * 535 * After it the BA24 bit shall be cleared to allow access to correct 536 * memory region after SW reset (by calling "reset" command). 537 * 538 * Otherwise, the BA24 bit may be left set and then after reset, the 539 * ROM would read/write/erase SPL from 16 MiB * bank_sel address. 540 */ 541 static int clean_bar(struct spi_nor *nor) 542 { 543 u8 cmd, bank_sel = 0; 544 545 if (nor->bank_curr == 0) 546 return 0; 547 cmd = nor->bank_write_cmd; 548 nor->bank_curr = 0; 549 write_enable(nor); 550 551 return nor->write_reg(nor, cmd, &bank_sel, 1); 552 } 553 554 static int write_bar(struct spi_nor *nor, u32 offset) 555 { 556 u8 cmd, bank_sel; 557 int ret; 558 559 bank_sel = offset / SZ_16M; 560 if (bank_sel == nor->bank_curr) 561 goto bar_end; 562 563 cmd = nor->bank_write_cmd; 564 write_enable(nor); 565 ret = nor->write_reg(nor, cmd, &bank_sel, 1); 566 if (ret < 0) { 567 debug("SF: fail to write bank register\n"); 568 return ret; 569 } 570 571 bar_end: 572 nor->bank_curr = bank_sel; 573 return nor->bank_curr; 574 } 575 576 static int read_bar(struct spi_nor *nor, const struct flash_info *info) 577 { 578 u8 curr_bank = 0; 579 int ret; 580 581 switch (JEDEC_MFR(info)) { 582 case SNOR_MFR_SPANSION: 583 nor->bank_read_cmd = SPINOR_OP_BRRD; 584 nor->bank_write_cmd = SPINOR_OP_BRWR; 585 break; 586 default: 587 nor->bank_read_cmd = SPINOR_OP_RDEAR; 588 nor->bank_write_cmd = SPINOR_OP_WREAR; 589 } 590 591 ret = nor->read_reg(nor, nor->bank_read_cmd, 592 &curr_bank, 1); 593 if (ret) { 594 debug("SF: fail to read bank addr register\n"); 595 return ret; 596 } 597 nor->bank_curr = curr_bank; 598 599 return 0; 600 } 601 #endif 602 603 /* 604 * Initiate the erasure of a single sector 605 */ 606 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr) 607 { 608 u8 buf[SPI_NOR_MAX_ADDR_WIDTH]; 609 int i; 610 int ret = 0; 611 612 if (nor->erase) 613 return nor->erase(nor, addr); 614 615 /* 616 * Default implementation, if driver doesn't have a specialized HW 617 * control 618 */ 619 for (i = nor->addr_width - 1; i >= 0; i--) { 620 buf[i] = addr & 0xff; 621 addr >>= 8; 622 } 623 624 ret = nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width); 625 if (ret) 626 return ret; 627 628 return nor->mtd.erasesize; 629 } 630 631 /* 632 * Erase an address range on the nor chip. The address range may extend 633 * one or more erase sectors. Return an error is there is a problem erasing. 634 */ 635 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr) 636 { 637 struct spi_nor *nor = mtd_to_spi_nor(mtd); 638 u32 addr, len, rem; 639 int ret; 640 641 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr, 642 (long long)instr->len); 643 644 div_u64_rem(instr->len, mtd->erasesize, &rem); 645 if (rem) 646 return -EINVAL; 647 648 addr = instr->addr; 649 len = instr->len; 650 651 while (len) { 652 #ifdef CONFIG_SPI_FLASH_BAR 653 ret = write_bar(nor, addr); 654 if (ret < 0) 655 return ret; 656 #endif 657 write_enable(nor); 658 659 ret = spi_nor_erase_sector(nor, addr); 660 if (ret < 0) 661 goto erase_err; 662 663 addr += ret; 664 len -= ret; 665 666 ret = spi_nor_wait_till_ready(nor); 667 if (ret) 668 goto erase_err; 669 } 670 671 erase_err: 672 #ifdef CONFIG_SPI_FLASH_BAR 673 ret = clean_bar(nor); 674 #endif 675 write_disable(nor); 676 677 return ret; 678 } 679 680 #ifdef CONFIG_SPI_FLASH_SPANSION 681 /** 682 * spansion_erase_non_uniform() - erase non-uniform sectors for Spansion/Cypress 683 * chips 684 * @nor: pointer to a 'struct spi_nor' 685 * @addr: address of the sector to erase 686 * @opcode_4k: opcode for 4K sector erase 687 * @ovlsz_top: size of overlaid portion at the top address 688 * @ovlsz_btm: size of overlaid portion at the bottom address 689 * 690 * Erase an address range on the nor chip that can contain 4KB sectors overlaid 691 * on top and/or bottom. The appropriate erase opcode and size are chosen by 692 * address to erase and size of overlaid portion. 693 * 694 * Return: number of bytes erased on success, -errno otherwise. 695 */ 696 static int spansion_erase_non_uniform(struct spi_nor *nor, u32 addr, 697 u8 opcode_4k, u32 ovlsz_top, 698 u32 ovlsz_btm) 699 { 700 struct spi_mem_op op = 701 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 0), 702 SPI_MEM_OP_ADDR(nor->addr_width, addr, 0), 703 SPI_MEM_OP_NO_DUMMY, 704 SPI_MEM_OP_NO_DATA); 705 struct mtd_info *mtd = &nor->mtd; 706 u32 erasesize; 707 int ret; 708 709 /* 4KB sectors */ 710 if (op.addr.val < ovlsz_btm || 711 op.addr.val >= mtd->size - ovlsz_top) { 712 op.cmd.opcode = opcode_4k; 713 erasesize = SZ_4K; 714 715 /* Non-overlaid portion in the normal sector at the bottom */ 716 } else if (op.addr.val == ovlsz_btm) { 717 op.cmd.opcode = nor->erase_opcode; 718 erasesize = mtd->erasesize - ovlsz_btm; 719 720 /* Non-overlaid portion in the normal sector at the top */ 721 } else if (op.addr.val == mtd->size - mtd->erasesize) { 722 op.cmd.opcode = nor->erase_opcode; 723 erasesize = mtd->erasesize - ovlsz_top; 724 725 /* Normal sectors */ 726 } else { 727 op.cmd.opcode = nor->erase_opcode; 728 erasesize = mtd->erasesize; 729 } 730 731 spi_nor_setup_op(nor, &op, nor->write_proto); 732 733 ret = spi_mem_exec_op(nor->spi, &op); 734 if (ret) 735 return ret; 736 737 return erasesize; 738 } 739 740 static int s25hx_t_erase_non_uniform(struct spi_nor *nor, loff_t addr) 741 { 742 /* Support 32 x 4KB sectors at bottom */ 743 return spansion_erase_non_uniform(nor, addr, SPINOR_OP_BE_4K_4B, 0, 744 SZ_128K); 745 } 746 747 static int s25hx_t_setup(struct spi_nor *nor, const struct flash_info *info) 748 { 749 int ret; 750 u8 cfr3v; 751 752 #ifdef CONFIG_SPI_FLASH_BAR 753 return -ENOTSUPP; /* Bank Address Register is not supported */ 754 #endif 755 /* 756 * Read CFR3V to check if uniform sector is selected. If not, assign an 757 * erase hook that supports non-uniform erase. 758 */ 759 ret = spansion_read_any_reg(nor, SPINOR_REG_ADDR_CFR3V, 0, &cfr3v); 760 if (ret) 761 return ret; 762 763 if (!(cfr3v & CFR3V_UNHYSA)) 764 nor->erase = s25hx_t_erase_non_uniform; 765 766 return 0; 767 } 768 #endif 769 770 static int micron_read_nvcr(struct spi_nor *nor) 771 { 772 int ret; 773 int val; 774 775 ret = nor->read_reg(nor, SPINOR_OP_MICRON_RDNVCR, (u8 *)&val, 2); 776 if (ret < 0) { 777 dev_err(nor->dev, "[Micron] error %d reading NVCR\n", ret); 778 return ret; 779 } 780 781 return val; 782 } 783 784 static int micron_write_nvcr(struct spi_nor *nor, int val) 785 { 786 int ret; 787 788 write_enable(nor); 789 790 nor->cmd_buf[0] = val & 0xff; 791 nor->cmd_buf[1] = (val >> 8) & 0xff; 792 793 ret = nor->write_reg(nor, SPINOR_OP_MICRON_WRNVCR, nor->cmd_buf, 2); 794 if (ret < 0) { 795 dev_err(nor->dev, 796 "[Micron] error while writing configuration register\n"); 797 return -EINVAL; 798 } 799 800 ret = spi_nor_wait_till_ready(nor); 801 if (ret) { 802 dev_err(nor->dev, 803 "[Micron] timeout while writing configuration register\n"); 804 return ret; 805 } 806 807 return 0; 808 } 809 810 static int micron_read_cr_quad_enable(struct spi_nor *nor) 811 { 812 int ret; 813 814 /* Check current Quad Enable bit value. */ 815 ret = micron_read_nvcr(nor); 816 if (ret < 0) { 817 dev_err(dev, "[Micron] error while reading nonvolatile configuration register\n"); 818 return -EINVAL; 819 } 820 821 if ((ret & MICRON_RST_HOLD_CTRL) == 0) 822 return 0; 823 824 ret &= ~MICRON_RST_HOLD_CTRL; 825 826 /* Keep the current value of the Status Register. */ 827 ret = micron_write_nvcr(nor, ret); 828 if (ret < 0) { 829 dev_err(dev, "[Micron] error while writing nonvolatile configuration register\n"); 830 return -EINVAL; 831 } 832 833 ret = micron_read_nvcr(nor); 834 if (ret > 0 && (ret & MICRON_RST_HOLD_CTRL)) { 835 dev_err(nor->dev, "[Micron] Quad bit not set\n"); 836 return -EINVAL; 837 } 838 839 return 0; 840 } 841 842 static bool cypress_s25hx_t(const struct flash_info *info) 843 { 844 if (JEDEC_MFR(info) == SNOR_MFR_CYPRESS) { 845 switch (info->id[1]) { 846 case 0x2a: /* S25HL (QSPI, 3.3V) */ 847 case 0x2b: /* S25HS (QSPI, 1.8V) */ 848 return true; 849 break; 850 851 default: 852 break; 853 } 854 } 855 856 return false; 857 } 858 859 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST) 860 /* Write status register and ensure bits in mask match written values */ 861 static int write_sr_and_check(struct spi_nor *nor, u8 status_new, u8 mask) 862 { 863 int ret; 864 865 write_enable(nor); 866 ret = write_sr(nor, status_new); 867 if (ret) 868 return ret; 869 870 ret = spi_nor_wait_till_ready(nor); 871 if (ret) 872 return ret; 873 874 ret = read_sr(nor); 875 if (ret < 0) 876 return ret; 877 878 return ((ret & mask) != (status_new & mask)) ? -EIO : 0; 879 } 880 881 static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs, 882 uint64_t *len) 883 { 884 struct mtd_info *mtd = &nor->mtd; 885 u8 mask = SR_BP2 | SR_BP1 | SR_BP0; 886 int shift = ffs(mask) - 1; 887 int pow; 888 889 if (!(sr & mask)) { 890 /* No protection */ 891 *ofs = 0; 892 *len = 0; 893 } else { 894 pow = ((sr & mask) ^ mask) >> shift; 895 *len = mtd->size >> pow; 896 if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB) 897 *ofs = 0; 898 else 899 *ofs = mtd->size - *len; 900 } 901 } 902 903 /* 904 * Return 1 if the entire region is locked (if @locked is true) or unlocked (if 905 * @locked is false); 0 otherwise 906 */ 907 static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, u64 len, 908 u8 sr, bool locked) 909 { 910 loff_t lock_offs; 911 uint64_t lock_len; 912 913 if (!len) 914 return 1; 915 916 stm_get_locked_range(nor, sr, &lock_offs, &lock_len); 917 918 if (locked) 919 /* Requested range is a sub-range of locked range */ 920 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs); 921 else 922 /* Requested range does not overlap with locked range */ 923 return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs); 924 } 925 926 static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len, 927 u8 sr) 928 { 929 return stm_check_lock_status_sr(nor, ofs, len, sr, true); 930 } 931 932 static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len, 933 u8 sr) 934 { 935 return stm_check_lock_status_sr(nor, ofs, len, sr, false); 936 } 937 938 /* 939 * Lock a region of the flash. Compatible with ST Micro and similar flash. 940 * Supports the block protection bits BP{0,1,2} in the status register 941 * (SR). Does not support these features found in newer SR bitfields: 942 * - SEC: sector/block protect - only handle SEC=0 (block protect) 943 * - CMP: complement protect - only support CMP=0 (range is not complemented) 944 * 945 * Support for the following is provided conditionally for some flash: 946 * - TB: top/bottom protect 947 * 948 * Sample table portion for 8MB flash (Winbond w25q64fw): 949 * 950 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion 951 * -------------------------------------------------------------------------- 952 * X | X | 0 | 0 | 0 | NONE | NONE 953 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64 954 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32 955 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16 956 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8 957 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4 958 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2 959 * X | X | 1 | 1 | 1 | 8 MB | ALL 960 * ------|-------|-------|-------|-------|---------------|------------------- 961 * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64 962 * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32 963 * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16 964 * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8 965 * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4 966 * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2 967 * 968 * Returns negative on errors, 0 on success. 969 */ 970 static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len) 971 { 972 struct mtd_info *mtd = &nor->mtd; 973 int status_old, status_new; 974 u8 mask = SR_BP2 | SR_BP1 | SR_BP0; 975 u8 shift = ffs(mask) - 1, pow, val; 976 loff_t lock_len; 977 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB; 978 bool use_top; 979 980 status_old = read_sr(nor); 981 if (status_old < 0) 982 return status_old; 983 984 /* If nothing in our range is unlocked, we don't need to do anything */ 985 if (stm_is_locked_sr(nor, ofs, len, status_old)) 986 return 0; 987 988 /* If anything below us is unlocked, we can't use 'bottom' protection */ 989 if (!stm_is_locked_sr(nor, 0, ofs, status_old)) 990 can_be_bottom = false; 991 992 /* If anything above us is unlocked, we can't use 'top' protection */ 993 if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len), 994 status_old)) 995 can_be_top = false; 996 997 if (!can_be_bottom && !can_be_top) 998 return -EINVAL; 999 1000 /* Prefer top, if both are valid */ 1001 use_top = can_be_top; 1002 1003 /* lock_len: length of region that should end up locked */ 1004 if (use_top) 1005 lock_len = mtd->size - ofs; 1006 else 1007 lock_len = ofs + len; 1008 1009 /* 1010 * Need smallest pow such that: 1011 * 1012 * 1 / (2^pow) <= (len / size) 1013 * 1014 * so (assuming power-of-2 size) we do: 1015 * 1016 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len)) 1017 */ 1018 pow = ilog2(mtd->size) - ilog2(lock_len); 1019 val = mask - (pow << shift); 1020 if (val & ~mask) 1021 return -EINVAL; 1022 /* Don't "lock" with no region! */ 1023 if (!(val & mask)) 1024 return -EINVAL; 1025 1026 status_new = (status_old & ~mask & ~SR_TB) | val; 1027 1028 /* Disallow further writes if WP pin is asserted */ 1029 status_new |= SR_SRWD; 1030 1031 if (!use_top) 1032 status_new |= SR_TB; 1033 1034 /* Don't bother if they're the same */ 1035 if (status_new == status_old) 1036 return 0; 1037 1038 /* Only modify protection if it will not unlock other areas */ 1039 if ((status_new & mask) < (status_old & mask)) 1040 return -EINVAL; 1041 1042 return write_sr_and_check(nor, status_new, mask); 1043 } 1044 1045 /* 1046 * Unlock a region of the flash. See stm_lock() for more info 1047 * 1048 * Returns negative on errors, 0 on success. 1049 */ 1050 static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len) 1051 { 1052 struct mtd_info *mtd = &nor->mtd; 1053 int status_old, status_new; 1054 u8 mask = SR_BP2 | SR_BP1 | SR_BP0; 1055 u8 shift = ffs(mask) - 1, pow, val; 1056 loff_t lock_len; 1057 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB; 1058 bool use_top; 1059 1060 status_old = read_sr(nor); 1061 if (status_old < 0) 1062 return status_old; 1063 1064 /* If nothing in our range is locked, we don't need to do anything */ 1065 if (stm_is_unlocked_sr(nor, ofs, len, status_old)) 1066 return 0; 1067 1068 /* If anything below us is locked, we can't use 'top' protection */ 1069 if (!stm_is_unlocked_sr(nor, 0, ofs, status_old)) 1070 can_be_top = false; 1071 1072 /* If anything above us is locked, we can't use 'bottom' protection */ 1073 if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len), 1074 status_old)) 1075 can_be_bottom = false; 1076 1077 if (!can_be_bottom && !can_be_top) 1078 return -EINVAL; 1079 1080 /* Prefer top, if both are valid */ 1081 use_top = can_be_top; 1082 1083 /* lock_len: length of region that should remain locked */ 1084 if (use_top) 1085 lock_len = mtd->size - (ofs + len); 1086 else 1087 lock_len = ofs; 1088 1089 /* 1090 * Need largest pow such that: 1091 * 1092 * 1 / (2^pow) >= (len / size) 1093 * 1094 * so (assuming power-of-2 size) we do: 1095 * 1096 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len)) 1097 */ 1098 pow = ilog2(mtd->size) - order_base_2(lock_len); 1099 if (lock_len == 0) { 1100 val = 0; /* fully unlocked */ 1101 } else { 1102 val = mask - (pow << shift); 1103 /* Some power-of-two sizes are not supported */ 1104 if (val & ~mask) 1105 return -EINVAL; 1106 } 1107 1108 status_new = (status_old & ~mask & ~SR_TB) | val; 1109 1110 /* Don't protect status register if we're fully unlocked */ 1111 if (lock_len == 0) 1112 status_new &= ~SR_SRWD; 1113 1114 if (!use_top) 1115 status_new |= SR_TB; 1116 1117 /* Don't bother if they're the same */ 1118 if (status_new == status_old) 1119 return 0; 1120 1121 /* Only modify protection if it will not lock other areas */ 1122 if ((status_new & mask) > (status_old & mask)) 1123 return -EINVAL; 1124 1125 return write_sr_and_check(nor, status_new, mask); 1126 } 1127 1128 /* 1129 * Check if a region of the flash is (completely) locked. See stm_lock() for 1130 * more info. 1131 * 1132 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and 1133 * negative on errors. 1134 */ 1135 static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len) 1136 { 1137 int status; 1138 1139 status = read_sr(nor); 1140 if (status < 0) 1141 return status; 1142 1143 return stm_is_locked_sr(nor, ofs, len, status); 1144 } 1145 #endif /* CONFIG_SPI_FLASH_STMICRO */ 1146 1147 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor) 1148 { 1149 int tmp; 1150 u8 id[SPI_NOR_MAX_ID_LEN]; 1151 const struct flash_info *info; 1152 1153 tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN); 1154 if (tmp < 0) { 1155 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp); 1156 return ERR_PTR(tmp); 1157 } 1158 1159 info = spi_nor_ids; 1160 for (; info->name; info++) { 1161 if (info->id_len) { 1162 if (!memcmp(info->id, id, info->id_len)) 1163 return info; 1164 } 1165 } 1166 1167 dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n", 1168 id[0], id[1], id[2]); 1169 return ERR_PTR(-ENODEV); 1170 } 1171 1172 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len, 1173 size_t *retlen, u_char *buf) 1174 { 1175 struct spi_nor *nor = mtd_to_spi_nor(mtd); 1176 int ret; 1177 1178 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len); 1179 1180 while (len) { 1181 loff_t addr = from; 1182 size_t read_len = len; 1183 1184 #ifdef CONFIG_SPI_FLASH_BAR 1185 u32 remain_len; 1186 1187 ret = write_bar(nor, addr); 1188 if (ret < 0) 1189 return log_ret(ret); 1190 remain_len = (SZ_16M * (nor->bank_curr + 1)) - addr; 1191 1192 if (len < remain_len) 1193 read_len = len; 1194 else 1195 read_len = remain_len; 1196 #endif 1197 1198 ret = nor->read(nor, addr, read_len, buf); 1199 if (ret == 0) { 1200 /* We shouldn't see 0-length reads */ 1201 ret = -EIO; 1202 goto read_err; 1203 } 1204 if (ret < 0) 1205 goto read_err; 1206 1207 *retlen += ret; 1208 buf += ret; 1209 from += ret; 1210 len -= ret; 1211 } 1212 ret = 0; 1213 1214 read_err: 1215 #ifdef CONFIG_SPI_FLASH_BAR 1216 ret = clean_bar(nor); 1217 #endif 1218 return ret; 1219 } 1220 1221 #ifdef CONFIG_SPI_FLASH_SST 1222 static int sst_write_byteprogram(struct spi_nor *nor, loff_t to, size_t len, 1223 size_t *retlen, const u_char *buf) 1224 { 1225 size_t actual; 1226 int ret = 0; 1227 1228 for (actual = 0; actual < len; actual++) { 1229 nor->program_opcode = SPINOR_OP_BP; 1230 1231 write_enable(nor); 1232 /* write one byte. */ 1233 ret = nor->write(nor, to, 1, buf + actual); 1234 if (ret < 0) 1235 goto sst_write_err; 1236 ret = spi_nor_wait_till_ready(nor); 1237 if (ret) 1238 goto sst_write_err; 1239 to++; 1240 } 1241 1242 sst_write_err: 1243 write_disable(nor); 1244 return ret; 1245 } 1246 1247 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len, 1248 size_t *retlen, const u_char *buf) 1249 { 1250 struct spi_nor *nor = mtd_to_spi_nor(mtd); 1251 struct spi_slave *spi = nor->spi; 1252 size_t actual; 1253 int ret; 1254 1255 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len); 1256 if (spi->mode & SPI_TX_BYTE) 1257 return sst_write_byteprogram(nor, to, len, retlen, buf); 1258 1259 write_enable(nor); 1260 1261 nor->sst_write_second = false; 1262 1263 actual = to % 2; 1264 /* Start write from odd address. */ 1265 if (actual) { 1266 nor->program_opcode = SPINOR_OP_BP; 1267 1268 /* write one byte. */ 1269 ret = nor->write(nor, to, 1, buf); 1270 if (ret < 0) 1271 goto sst_write_err; 1272 ret = spi_nor_wait_till_ready(nor); 1273 if (ret) 1274 goto sst_write_err; 1275 } 1276 to += actual; 1277 1278 /* Write out most of the data here. */ 1279 for (; actual < len - 1; actual += 2) { 1280 nor->program_opcode = SPINOR_OP_AAI_WP; 1281 1282 /* write two bytes. */ 1283 ret = nor->write(nor, to, 2, buf + actual); 1284 if (ret < 0) 1285 goto sst_write_err; 1286 ret = spi_nor_wait_till_ready(nor); 1287 if (ret) 1288 goto sst_write_err; 1289 to += 2; 1290 nor->sst_write_second = true; 1291 } 1292 nor->sst_write_second = false; 1293 1294 write_disable(nor); 1295 ret = spi_nor_wait_till_ready(nor); 1296 if (ret) 1297 goto sst_write_err; 1298 1299 /* Write out trailing byte if it exists. */ 1300 if (actual != len) { 1301 write_enable(nor); 1302 1303 nor->program_opcode = SPINOR_OP_BP; 1304 ret = nor->write(nor, to, 1, buf + actual); 1305 if (ret < 0) 1306 goto sst_write_err; 1307 ret = spi_nor_wait_till_ready(nor); 1308 if (ret) 1309 goto sst_write_err; 1310 write_disable(nor); 1311 actual += 1; 1312 } 1313 sst_write_err: 1314 *retlen += actual; 1315 return ret; 1316 } 1317 #endif 1318 /* 1319 * Write an address range to the nor chip. Data must be written in 1320 * FLASH_PAGESIZE chunks. The address range may be any size provided 1321 * it is within the physical boundaries. 1322 */ 1323 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len, 1324 size_t *retlen, const u_char *buf) 1325 { 1326 struct spi_nor *nor = mtd_to_spi_nor(mtd); 1327 size_t page_offset, page_remain, i; 1328 ssize_t ret; 1329 1330 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len); 1331 1332 for (i = 0; i < len; ) { 1333 ssize_t written; 1334 loff_t addr = to + i; 1335 1336 /* 1337 * If page_size is a power of two, the offset can be quickly 1338 * calculated with an AND operation. On the other cases we 1339 * need to do a modulus operation (more expensive). 1340 * Power of two numbers have only one bit set and we can use 1341 * the instruction hweight32 to detect if we need to do a 1342 * modulus (do_div()) or not. 1343 */ 1344 if (hweight32(nor->page_size) == 1) { 1345 page_offset = addr & (nor->page_size - 1); 1346 } else { 1347 u64 aux = addr; 1348 1349 page_offset = do_div(aux, nor->page_size); 1350 } 1351 /* the size of data remaining on the first page */ 1352 page_remain = min_t(size_t, 1353 nor->page_size - page_offset, len - i); 1354 1355 #ifdef CONFIG_SPI_FLASH_BAR 1356 ret = write_bar(nor, addr); 1357 if (ret < 0) 1358 return ret; 1359 #endif 1360 write_enable(nor); 1361 ret = nor->write(nor, addr, page_remain, buf + i); 1362 if (ret < 0) 1363 goto write_err; 1364 written = ret; 1365 1366 ret = spi_nor_wait_till_ready(nor); 1367 if (ret) 1368 goto write_err; 1369 *retlen += written; 1370 i += written; 1371 if (written != page_remain) { 1372 ret = -EIO; 1373 goto write_err; 1374 } 1375 } 1376 1377 write_err: 1378 #ifdef CONFIG_SPI_FLASH_BAR 1379 ret = clean_bar(nor); 1380 #endif 1381 return ret; 1382 } 1383 1384 #ifdef CONFIG_SPI_FLASH_MACRONIX 1385 /** 1386 * macronix_quad_enable() - set QE bit in Status Register. 1387 * @nor: pointer to a 'struct spi_nor' 1388 * 1389 * Set the Quad Enable (QE) bit in the Status Register. 1390 * 1391 * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories. 1392 * 1393 * Return: 0 on success, -errno otherwise. 1394 */ 1395 static int macronix_quad_enable(struct spi_nor *nor) 1396 { 1397 int ret, val; 1398 1399 val = read_sr(nor); 1400 if (val < 0) 1401 return val; 1402 if (val & SR_QUAD_EN_MX) 1403 return 0; 1404 1405 write_enable(nor); 1406 1407 write_sr(nor, val | SR_QUAD_EN_MX); 1408 1409 ret = spi_nor_wait_till_ready(nor); 1410 if (ret) 1411 return ret; 1412 1413 ret = read_sr(nor); 1414 if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) { 1415 dev_err(nor->dev, "Macronix Quad bit not set\n"); 1416 return -EINVAL; 1417 } 1418 1419 return 0; 1420 } 1421 #endif 1422 1423 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND) 1424 /* 1425 * Write status Register and configuration register with 2 bytes 1426 * The first byte will be written to the status register, while the 1427 * second byte will be written to the configuration register. 1428 * Return negative if error occurred. 1429 */ 1430 static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr) 1431 { 1432 int ret; 1433 1434 write_enable(nor); 1435 1436 ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2); 1437 if (ret < 0) { 1438 dev_dbg(nor->dev, 1439 "error while writing configuration register\n"); 1440 return -EINVAL; 1441 } 1442 1443 ret = spi_nor_wait_till_ready(nor); 1444 if (ret) { 1445 dev_dbg(nor->dev, 1446 "timeout while writing configuration register\n"); 1447 return ret; 1448 } 1449 1450 return 0; 1451 } 1452 1453 /** 1454 * spansion_read_cr_quad_enable() - set QE bit in Configuration Register. 1455 * @nor: pointer to a 'struct spi_nor' 1456 * 1457 * Set the Quad Enable (QE) bit in the Configuration Register. 1458 * This function should be used with QSPI memories supporting the Read 1459 * Configuration Register (35h) instruction. 1460 * 1461 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI 1462 * memories. 1463 * 1464 * Return: 0 on success, -errno otherwise. 1465 */ 1466 static int spansion_read_cr_quad_enable(struct spi_nor *nor) 1467 { 1468 u8 sr_cr[2]; 1469 int ret; 1470 1471 /* Check current Quad Enable bit value. */ 1472 ret = read_cr(nor); 1473 if (ret < 0) { 1474 dev_dbg(nor->dev, "error while reading configuration register\n"); 1475 return -EINVAL; 1476 } 1477 1478 if (ret & CR_QUAD_EN_SPAN) 1479 return 0; 1480 1481 sr_cr[1] = ret | CR_QUAD_EN_SPAN; 1482 1483 /* Keep the current value of the Status Register. */ 1484 ret = read_sr(nor); 1485 if (ret < 0) { 1486 dev_dbg(nor->dev, "error while reading status register\n"); 1487 return -EINVAL; 1488 } 1489 sr_cr[0] = ret; 1490 1491 ret = write_sr_cr(nor, sr_cr); 1492 if (ret) 1493 return ret; 1494 1495 /* Read back and check it. */ 1496 ret = read_cr(nor); 1497 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) { 1498 dev_dbg(nor->dev, "Spansion Quad bit not set\n"); 1499 return -EINVAL; 1500 } 1501 1502 return 0; 1503 } 1504 1505 /** 1506 * sr2_bit1_quad_enable() - set QE bit in Status Register 2. 1507 * @nor: pointer to a 'struct spi_nor' 1508 * 1509 * Set the Quad Enable (QE) bit in the Status Register 2. 1510 * 1511 * Return: 0 on success, -errno otherwise. 1512 */ 1513 static int winbond_sr2_bit1_quad_enable(struct spi_nor *nor) 1514 { 1515 u8 sr2 = 0; 1516 int ret; 1517 1518 /* Check current Quad Enable bit value. */ 1519 ret = read_winbond_sr2(nor); 1520 if (ret < 0) { 1521 dev_err(nor->dev, "error while reading status register 2\n"); 1522 return -EINVAL; 1523 } 1524 1525 if (ret & SR2_QUAD_EN_BIT1) 1526 return 0; 1527 1528 /* Update the Quad Enable bit. */ 1529 sr2 = (u8)(ret | SR2_QUAD_EN_BIT1); 1530 1531 write_enable(nor); 1532 1533 ret = write_winbond_sr2(nor, sr2); 1534 if (ret < 0) { 1535 dev_err(nor->dev, "error while writing status register 2\n"); 1536 return -EINVAL; 1537 } 1538 1539 ret = spi_nor_wait_till_ready(nor); 1540 if (ret < 0) { 1541 dev_err(nor->dev, "timeout while writing status register 2\n"); 1542 return ret; 1543 } 1544 1545 /* Read back and check it. */ 1546 ret = read_winbond_sr2(nor); 1547 if (ret < 0 || !(ret & SR2_QUAD_EN_BIT1)) { 1548 dev_err(nor->dev, "SR2 Quad bit not set\n"); 1549 return -EINVAL; 1550 } 1551 1552 return 0; 1553 } 1554 1555 #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT) 1556 /** 1557 * spansion_no_read_cr_quad_enable() - set QE bit in Configuration Register. 1558 * @nor: pointer to a 'struct spi_nor' 1559 * 1560 * Set the Quad Enable (QE) bit in the Configuration Register. 1561 * This function should be used with QSPI memories not supporting the Read 1562 * Configuration Register (35h) instruction. 1563 * 1564 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI 1565 * memories. 1566 * 1567 * Return: 0 on success, -errno otherwise. 1568 */ 1569 static int spansion_no_read_cr_quad_enable(struct spi_nor *nor) 1570 { 1571 u8 sr_cr[2]; 1572 int ret; 1573 1574 /* Keep the current value of the Status Register. */ 1575 ret = read_sr(nor); 1576 if (ret < 0) { 1577 dev_dbg(nor->dev, "error while reading status register\n"); 1578 return -EINVAL; 1579 } 1580 sr_cr[0] = ret; 1581 sr_cr[1] = CR_QUAD_EN_SPAN; 1582 1583 return write_sr_cr(nor, sr_cr); 1584 } 1585 1586 #endif /* CONFIG_SPI_FLASH_SFDP_SUPPORT */ 1587 #endif /* CONFIG_SPI_FLASH_SPANSION */ 1588 1589 struct spi_nor_read_command { 1590 u8 num_mode_clocks; 1591 u8 num_wait_states; 1592 u8 opcode; 1593 enum spi_nor_protocol proto; 1594 }; 1595 1596 struct spi_nor_pp_command { 1597 u8 opcode; 1598 enum spi_nor_protocol proto; 1599 }; 1600 1601 enum spi_nor_read_command_index { 1602 SNOR_CMD_READ, 1603 SNOR_CMD_READ_FAST, 1604 SNOR_CMD_READ_1_1_1_DTR, 1605 1606 /* Dual SPI */ 1607 SNOR_CMD_READ_1_1_2, 1608 SNOR_CMD_READ_1_2_2, 1609 SNOR_CMD_READ_2_2_2, 1610 SNOR_CMD_READ_1_2_2_DTR, 1611 1612 /* Quad SPI */ 1613 SNOR_CMD_READ_1_1_4, 1614 SNOR_CMD_READ_1_4_4, 1615 SNOR_CMD_READ_4_4_4, 1616 SNOR_CMD_READ_1_4_4_DTR, 1617 1618 /* Octo SPI */ 1619 SNOR_CMD_READ_1_1_8, 1620 SNOR_CMD_READ_1_8_8, 1621 SNOR_CMD_READ_8_8_8, 1622 SNOR_CMD_READ_1_8_8_DTR, 1623 1624 SNOR_CMD_READ_MAX 1625 }; 1626 1627 enum spi_nor_pp_command_index { 1628 SNOR_CMD_PP, 1629 1630 /* Quad SPI */ 1631 SNOR_CMD_PP_1_1_4, 1632 SNOR_CMD_PP_1_4_4, 1633 SNOR_CMD_PP_4_4_4, 1634 1635 /* Octo SPI */ 1636 SNOR_CMD_PP_1_1_8, 1637 SNOR_CMD_PP_1_8_8, 1638 SNOR_CMD_PP_8_8_8, 1639 1640 SNOR_CMD_PP_MAX 1641 }; 1642 1643 struct spi_nor_flash_parameter { 1644 u64 size; 1645 u32 page_size; 1646 1647 struct spi_nor_hwcaps hwcaps; 1648 struct spi_nor_read_command reads[SNOR_CMD_READ_MAX]; 1649 struct spi_nor_pp_command page_programs[SNOR_CMD_PP_MAX]; 1650 1651 int (*quad_enable)(struct spi_nor *nor); 1652 }; 1653 1654 #ifdef CONFIG_SPI_FLASH_SPANSION 1655 /** 1656 * spansion_quad_enable_volatile() - enable Quad I/O mode in volatile register. 1657 * @nor: pointer to a 'struct spi_nor' 1658 * 1659 * It is recommended to update volatile registers in the field application due 1660 * to a risk of the non-volatile registers corruption by power interrupt. This 1661 * function sets Quad Enable bit in CFR1 volatile. 1662 * 1663 * Return: 0 on success, -errno otherwise. 1664 */ 1665 static int spansion_quad_enable_volatile(struct spi_nor *nor) 1666 { 1667 u32 addr = SPINOR_REG_ADDR_CFR1V; 1668 1669 u8 cr; 1670 int ret; 1671 1672 /* Check current Quad Enable bit value. */ 1673 ret = spansion_read_any_reg(nor, addr, 0, &cr); 1674 if (ret < 0) { 1675 dev_dbg(nor->dev, 1676 "error while reading configuration register\n"); 1677 return -EINVAL; 1678 } 1679 1680 if (cr & CR_QUAD_EN_SPAN) 1681 return 0; 1682 1683 cr |= CR_QUAD_EN_SPAN; 1684 1685 write_enable(nor); 1686 1687 ret = spansion_write_any_reg(nor, addr, cr); 1688 1689 if (ret < 0) { 1690 dev_dbg(nor->dev, 1691 "error while writing configuration register\n"); 1692 return -EINVAL; 1693 } 1694 1695 /* Read back and check it. */ 1696 ret = spansion_read_any_reg(nor, addr, 0, &cr); 1697 if (ret || !(cr & CR_QUAD_EN_SPAN)) { 1698 dev_dbg(nor->dev, "Spansion Quad bit not set\n"); 1699 return -EINVAL; 1700 } 1701 1702 return 0; 1703 } 1704 #endif 1705 1706 static void 1707 spi_nor_set_read_settings(struct spi_nor_read_command *read, 1708 u8 num_mode_clocks, 1709 u8 num_wait_states, 1710 u8 opcode, 1711 enum spi_nor_protocol proto) 1712 { 1713 read->num_mode_clocks = num_mode_clocks; 1714 read->num_wait_states = num_wait_states; 1715 read->opcode = opcode; 1716 read->proto = proto; 1717 } 1718 1719 static void 1720 spi_nor_set_pp_settings(struct spi_nor_pp_command *pp, 1721 u8 opcode, 1722 enum spi_nor_protocol proto) 1723 { 1724 pp->opcode = opcode; 1725 pp->proto = proto; 1726 } 1727 1728 #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT) 1729 /* 1730 * Serial Flash Discoverable Parameters (SFDP) parsing. 1731 */ 1732 1733 /** 1734 * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters. 1735 * @nor: pointer to a 'struct spi_nor' 1736 * @addr: offset in the SFDP area to start reading data from 1737 * @len: number of bytes to read 1738 * @buf: buffer where the SFDP data are copied into (dma-safe memory) 1739 * 1740 * Whatever the actual numbers of bytes for address and dummy cycles are 1741 * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always 1742 * followed by a 3-byte address and 8 dummy clock cycles. 1743 * 1744 * Return: 0 on success, -errno otherwise. 1745 */ 1746 static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr, 1747 size_t len, void *buf) 1748 { 1749 u8 addr_width, read_opcode, read_dummy; 1750 enum spi_nor_protocol read_proto; 1751 int ret; 1752 1753 read_opcode = nor->read_opcode; 1754 addr_width = nor->addr_width; 1755 read_dummy = nor->read_dummy; 1756 read_proto = nor->read_proto; 1757 1758 nor->read_opcode = SPINOR_OP_RDSFDP; 1759 nor->read_proto = SNOR_PROTO_1_1_1; 1760 nor->addr_width = 3; 1761 nor->read_dummy = 8; 1762 1763 while (len) { 1764 ret = nor->read(nor, addr, len, (u8 *)buf); 1765 if (!ret || ret > len) { 1766 ret = -EIO; 1767 goto read_err; 1768 } 1769 if (ret < 0) 1770 goto read_err; 1771 1772 buf += ret; 1773 addr += ret; 1774 len -= ret; 1775 } 1776 ret = 0; 1777 1778 read_err: 1779 nor->read_opcode = read_opcode; 1780 nor->addr_width = addr_width; 1781 nor->read_dummy = read_dummy; 1782 nor->read_proto = read_proto; 1783 1784 return ret; 1785 } 1786 1787 struct sfdp_parameter_header { 1788 u8 id_lsb; 1789 u8 minor; 1790 u8 major; 1791 u8 length; /* in double words */ 1792 u8 parameter_table_pointer[3]; /* byte address */ 1793 u8 id_msb; 1794 }; 1795 1796 #define SFDP_PARAM_HEADER_ID(p) (((p)->id_msb << 8) | (p)->id_lsb) 1797 #define SFDP_PARAM_HEADER_PTP(p) \ 1798 (((p)->parameter_table_pointer[2] << 16) | \ 1799 ((p)->parameter_table_pointer[1] << 8) | \ 1800 ((p)->parameter_table_pointer[0] << 0)) 1801 1802 #define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */ 1803 #define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */ 1804 1805 #define SFDP_SIGNATURE 0x50444653U 1806 #define SFDP_JESD216_MAJOR 1 1807 #define SFDP_JESD216_MINOR 0 1808 #define SFDP_JESD216A_MINOR 5 1809 #define SFDP_JESD216B_MINOR 6 1810 1811 struct sfdp_header { 1812 u32 signature; /* Ox50444653U <=> "SFDP" */ 1813 u8 minor; 1814 u8 major; 1815 u8 nph; /* 0-base number of parameter headers */ 1816 u8 unused; 1817 1818 /* Basic Flash Parameter Table. */ 1819 struct sfdp_parameter_header bfpt_header; 1820 }; 1821 1822 /* Basic Flash Parameter Table */ 1823 1824 /* 1825 * JESD216 rev B defines a Basic Flash Parameter Table of 16 DWORDs. 1826 * They are indexed from 1 but C arrays are indexed from 0. 1827 */ 1828 #define BFPT_DWORD(i) ((i) - 1) 1829 #define BFPT_DWORD_MAX 16 1830 1831 /* The first version of JESB216 defined only 9 DWORDs. */ 1832 #define BFPT_DWORD_MAX_JESD216 9 1833 1834 /* 1st DWORD. */ 1835 #define BFPT_DWORD1_FAST_READ_1_1_2 BIT(16) 1836 #define BFPT_DWORD1_ADDRESS_BYTES_MASK GENMASK(18, 17) 1837 #define BFPT_DWORD1_ADDRESS_BYTES_3_ONLY (0x0UL << 17) 1838 #define BFPT_DWORD1_ADDRESS_BYTES_3_OR_4 (0x1UL << 17) 1839 #define BFPT_DWORD1_ADDRESS_BYTES_4_ONLY (0x2UL << 17) 1840 #define BFPT_DWORD1_DTR BIT(19) 1841 #define BFPT_DWORD1_FAST_READ_1_2_2 BIT(20) 1842 #define BFPT_DWORD1_FAST_READ_1_4_4 BIT(21) 1843 #define BFPT_DWORD1_FAST_READ_1_1_4 BIT(22) 1844 1845 /* 5th DWORD. */ 1846 #define BFPT_DWORD5_FAST_READ_2_2_2 BIT(0) 1847 #define BFPT_DWORD5_FAST_READ_4_4_4 BIT(4) 1848 1849 /* 11th DWORD. */ 1850 #define BFPT_DWORD11_PAGE_SIZE_SHIFT 4 1851 #define BFPT_DWORD11_PAGE_SIZE_MASK GENMASK(7, 4) 1852 1853 /* 15th DWORD. */ 1854 1855 /* 1856 * (from JESD216 rev B) 1857 * Quad Enable Requirements (QER): 1858 * - 000b: Device does not have a QE bit. Device detects 1-1-4 and 1-4-4 1859 * reads based on instruction. DQ3/HOLD# functions are hold during 1860 * instruction phase. 1861 * - 001b: QE is bit 1 of status register 2. It is set via Write Status with 1862 * two data bytes where bit 1 of the second byte is one. 1863 * [...] 1864 * Writing only one byte to the status register has the side-effect of 1865 * clearing status register 2, including the QE bit. The 100b code is 1866 * used if writing one byte to the status register does not modify 1867 * status register 2. 1868 * - 010b: QE is bit 6 of status register 1. It is set via Write Status with 1869 * one data byte where bit 6 is one. 1870 * [...] 1871 * - 011b: QE is bit 7 of status register 2. It is set via Write status 1872 * register 2 instruction 3Eh with one data byte where bit 7 is one. 1873 * [...] 1874 * The status register 2 is read using instruction 3Fh. 1875 * - 100b: QE is bit 1 of status register 2. It is set via Write Status with 1876 * two data bytes where bit 1 of the second byte is one. 1877 * [...] 1878 * In contrast to the 001b code, writing one byte to the status 1879 * register does not modify status register 2. 1880 * - 101b: QE is bit 1 of status register 2. Status register 1 is read using 1881 * Read Status instruction 05h. Status register2 is read using 1882 * instruction 35h. QE is set via Writ Status instruction 01h with 1883 * two data bytes where bit 1 of the second byte is one. 1884 * [...] 1885 */ 1886 #define BFPT_DWORD15_QER_MASK GENMASK(22, 20) 1887 #define BFPT_DWORD15_QER_NONE (0x0UL << 20) /* Micron */ 1888 #define BFPT_DWORD15_QER_SR2_BIT1_BUGGY (0x1UL << 20) 1889 #define BFPT_DWORD15_QER_SR1_BIT6 (0x2UL << 20) /* Macronix */ 1890 #define BFPT_DWORD15_QER_SR2_BIT7 (0x3UL << 20) 1891 #define BFPT_DWORD15_QER_SR2_BIT1_NO_RD (0x4UL << 20) 1892 #define BFPT_DWORD15_QER_SR2_BIT1 (0x5UL << 20) /* Spansion */ 1893 #define BFPT_DWORD15_QER_NONE_111 (0x7UL << 20) /* Gigadevice */ 1894 1895 struct sfdp_bfpt { 1896 u32 dwords[BFPT_DWORD_MAX]; 1897 }; 1898 1899 /* Fast Read settings. */ 1900 1901 static void 1902 spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read, 1903 u16 half, 1904 enum spi_nor_protocol proto) 1905 { 1906 read->num_mode_clocks = (half >> 5) & 0x07; 1907 read->num_wait_states = (half >> 0) & 0x1f; 1908 read->opcode = (half >> 8) & 0xff; 1909 read->proto = proto; 1910 } 1911 1912 struct sfdp_bfpt_read { 1913 /* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */ 1914 u32 hwcaps; 1915 1916 /* 1917 * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us 1918 * whether the Fast Read x-y-z command is supported. 1919 */ 1920 u32 supported_dword; 1921 u32 supported_bit; 1922 1923 /* 1924 * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD 1925 * encodes the op code, the number of mode clocks and the number of wait 1926 * states to be used by Fast Read x-y-z command. 1927 */ 1928 u32 settings_dword; 1929 u32 settings_shift; 1930 1931 /* The SPI protocol for this Fast Read x-y-z command. */ 1932 enum spi_nor_protocol proto; 1933 }; 1934 1935 static const struct sfdp_bfpt_read sfdp_bfpt_reads[] = { 1936 /* Fast Read 1-1-2 */ 1937 { 1938 SNOR_HWCAPS_READ_1_1_2, 1939 BFPT_DWORD(1), BIT(16), /* Supported bit */ 1940 BFPT_DWORD(4), 0, /* Settings */ 1941 SNOR_PROTO_1_1_2, 1942 }, 1943 1944 /* Fast Read 1-2-2 */ 1945 { 1946 SNOR_HWCAPS_READ_1_2_2, 1947 BFPT_DWORD(1), BIT(20), /* Supported bit */ 1948 BFPT_DWORD(4), 16, /* Settings */ 1949 SNOR_PROTO_1_2_2, 1950 }, 1951 1952 /* Fast Read 2-2-2 */ 1953 { 1954 SNOR_HWCAPS_READ_2_2_2, 1955 BFPT_DWORD(5), BIT(0), /* Supported bit */ 1956 BFPT_DWORD(6), 16, /* Settings */ 1957 SNOR_PROTO_2_2_2, 1958 }, 1959 1960 /* Fast Read 1-1-4 */ 1961 { 1962 SNOR_HWCAPS_READ_1_1_4, 1963 BFPT_DWORD(1), BIT(22), /* Supported bit */ 1964 BFPT_DWORD(3), 16, /* Settings */ 1965 SNOR_PROTO_1_1_4, 1966 }, 1967 1968 /* Fast Read 1-4-4 */ 1969 { 1970 SNOR_HWCAPS_READ_1_4_4, 1971 BFPT_DWORD(1), BIT(21), /* Supported bit */ 1972 BFPT_DWORD(3), 0, /* Settings */ 1973 SNOR_PROTO_1_4_4, 1974 }, 1975 1976 /* Fast Read 4-4-4 */ 1977 { 1978 SNOR_HWCAPS_READ_4_4_4, 1979 BFPT_DWORD(5), BIT(4), /* Supported bit */ 1980 BFPT_DWORD(7), 16, /* Settings */ 1981 SNOR_PROTO_4_4_4, 1982 }, 1983 }; 1984 1985 struct sfdp_bfpt_erase { 1986 /* 1987 * The half-word at offset <shift> in DWORD <dwoard> encodes the 1988 * op code and erase sector size to be used by Sector Erase commands. 1989 */ 1990 u32 dword; 1991 u32 shift; 1992 }; 1993 1994 static const struct sfdp_bfpt_erase sfdp_bfpt_erases[] = { 1995 /* Erase Type 1 in DWORD8 bits[15:0] */ 1996 {BFPT_DWORD(8), 0}, 1997 1998 /* Erase Type 2 in DWORD8 bits[31:16] */ 1999 {BFPT_DWORD(8), 16}, 2000 2001 /* Erase Type 3 in DWORD9 bits[15:0] */ 2002 {BFPT_DWORD(9), 0}, 2003 2004 /* Erase Type 4 in DWORD9 bits[31:16] */ 2005 {BFPT_DWORD(9), 16}, 2006 }; 2007 2008 static int spi_nor_hwcaps_read2cmd(u32 hwcaps); 2009 2010 /** 2011 * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table. 2012 * @nor: pointer to a 'struct spi_nor' 2013 * @bfpt_header: pointer to the 'struct sfdp_parameter_header' describing 2014 * the Basic Flash Parameter Table length and version 2015 * @params: pointer to the 'struct spi_nor_flash_parameter' to be 2016 * filled 2017 * 2018 * The Basic Flash Parameter Table is the main and only mandatory table as 2019 * defined by the SFDP (JESD216) specification. 2020 * It provides us with the total size (memory density) of the data array and 2021 * the number of address bytes for Fast Read, Page Program and Sector Erase 2022 * commands. 2023 * For Fast READ commands, it also gives the number of mode clock cycles and 2024 * wait states (regrouped in the number of dummy clock cycles) for each 2025 * supported instruction op code. 2026 * For Page Program, the page size is now available since JESD216 rev A, however 2027 * the supported instruction op codes are still not provided. 2028 * For Sector Erase commands, this table stores the supported instruction op 2029 * codes and the associated sector sizes. 2030 * Finally, the Quad Enable Requirements (QER) are also available since JESD216 2031 * rev A. The QER bits encode the manufacturer dependent procedure to be 2032 * executed to set the Quad Enable (QE) bit in some internal register of the 2033 * Quad SPI memory. Indeed the QE bit, when it exists, must be set before 2034 * sending any Quad SPI command to the memory. Actually, setting the QE bit 2035 * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2 2036 * and IO3 hence enabling 4 (Quad) I/O lines. 2037 * 2038 * Return: 0 on success, -errno otherwise. 2039 */ 2040 static int spi_nor_parse_bfpt(struct spi_nor *nor, 2041 const struct sfdp_parameter_header *bfpt_header, 2042 struct spi_nor_flash_parameter *params) 2043 { 2044 struct mtd_info *mtd = &nor->mtd; 2045 struct sfdp_bfpt bfpt; 2046 size_t len; 2047 int i, cmd, err; 2048 u32 addr; 2049 u16 half; 2050 2051 /* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */ 2052 if (bfpt_header->length < BFPT_DWORD_MAX_JESD216) 2053 return -EINVAL; 2054 2055 /* Read the Basic Flash Parameter Table. */ 2056 len = min_t(size_t, sizeof(bfpt), 2057 bfpt_header->length * sizeof(u32)); 2058 addr = SFDP_PARAM_HEADER_PTP(bfpt_header); 2059 memset(&bfpt, 0, sizeof(bfpt)); 2060 err = spi_nor_read_sfdp(nor, addr, len, &bfpt); 2061 if (err < 0) 2062 return err; 2063 2064 /* Fix endianness of the BFPT DWORDs. */ 2065 for (i = 0; i < BFPT_DWORD_MAX; i++) 2066 bfpt.dwords[i] = le32_to_cpu(bfpt.dwords[i]); 2067 2068 /* Number of address bytes. */ 2069 switch (bfpt.dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) { 2070 case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY: 2071 nor->addr_width = 3; 2072 break; 2073 2074 case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY: 2075 nor->addr_width = 4; 2076 break; 2077 2078 default: 2079 break; 2080 } 2081 2082 /* Flash Memory Density (in bits). */ 2083 params->size = bfpt.dwords[BFPT_DWORD(2)]; 2084 if (params->size & BIT(31)) { 2085 params->size &= ~BIT(31); 2086 2087 /* 2088 * Prevent overflows on params->size. Anyway, a NOR of 2^64 2089 * bits is unlikely to exist so this error probably means 2090 * the BFPT we are reading is corrupted/wrong. 2091 */ 2092 if (params->size > 63) 2093 return -EINVAL; 2094 2095 params->size = 1ULL << params->size; 2096 } else { 2097 params->size++; 2098 } 2099 params->size >>= 3; /* Convert to bytes. */ 2100 2101 /* Fast Read settings. */ 2102 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_reads); i++) { 2103 const struct sfdp_bfpt_read *rd = &sfdp_bfpt_reads[i]; 2104 struct spi_nor_read_command *read; 2105 2106 if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) { 2107 params->hwcaps.mask &= ~rd->hwcaps; 2108 continue; 2109 } 2110 2111 params->hwcaps.mask |= rd->hwcaps; 2112 cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps); 2113 read = ¶ms->reads[cmd]; 2114 half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift; 2115 spi_nor_set_read_settings_from_bfpt(read, half, rd->proto); 2116 } 2117 2118 /* Sector Erase settings. */ 2119 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_erases); i++) { 2120 const struct sfdp_bfpt_erase *er = &sfdp_bfpt_erases[i]; 2121 u32 erasesize; 2122 u8 opcode; 2123 2124 half = bfpt.dwords[er->dword] >> er->shift; 2125 erasesize = half & 0xff; 2126 2127 /* erasesize == 0 means this Erase Type is not supported. */ 2128 if (!erasesize) 2129 continue; 2130 2131 erasesize = 1U << erasesize; 2132 opcode = (half >> 8) & 0xff; 2133 #ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS 2134 if (erasesize == SZ_4K) { 2135 nor->erase_opcode = opcode; 2136 mtd->erasesize = erasesize; 2137 break; 2138 } 2139 #endif 2140 if (!mtd->erasesize || mtd->erasesize < erasesize) { 2141 nor->erase_opcode = opcode; 2142 mtd->erasesize = erasesize; 2143 } 2144 } 2145 2146 /* Stop here if not JESD216 rev A or later. */ 2147 if (bfpt_header->length < BFPT_DWORD_MAX) 2148 return 0; 2149 2150 /* Page size: this field specifies 'N' so the page size = 2^N bytes. */ 2151 params->page_size = bfpt.dwords[BFPT_DWORD(11)]; 2152 params->page_size &= BFPT_DWORD11_PAGE_SIZE_MASK; 2153 params->page_size >>= BFPT_DWORD11_PAGE_SIZE_SHIFT; 2154 params->page_size = 1U << params->page_size; 2155 2156 /* Quad Enable Requirements. */ 2157 switch (bfpt.dwords[BFPT_DWORD(15)] & BFPT_DWORD15_QER_MASK) { 2158 case BFPT_DWORD15_QER_NONE: 2159 case BFPT_DWORD15_QER_NONE_111: 2160 params->quad_enable = NULL; 2161 break; 2162 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND) 2163 case BFPT_DWORD15_QER_SR2_BIT1_BUGGY: 2164 case BFPT_DWORD15_QER_SR2_BIT1_NO_RD: 2165 params->quad_enable = spansion_no_read_cr_quad_enable; 2166 break; 2167 #endif 2168 #ifdef CONFIG_SPI_FLASH_MACRONIX 2169 case BFPT_DWORD15_QER_SR1_BIT6: 2170 params->quad_enable = macronix_quad_enable; 2171 break; 2172 #endif 2173 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND) 2174 case BFPT_DWORD15_QER_SR2_BIT1: 2175 params->quad_enable = spansion_read_cr_quad_enable; 2176 break; 2177 #endif 2178 default: 2179 return -EINVAL; 2180 } 2181 2182 return 0; 2183 } 2184 2185 /** 2186 * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters. 2187 * @nor: pointer to a 'struct spi_nor' 2188 * @params: pointer to the 'struct spi_nor_flash_parameter' to be 2189 * filled 2190 * 2191 * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216 2192 * specification. This is a standard which tends to supported by almost all 2193 * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at 2194 * runtime the main parameters needed to perform basic SPI flash operations such 2195 * as Fast Read, Page Program or Sector Erase commands. 2196 * 2197 * Return: 0 on success, -errno otherwise. 2198 */ 2199 static int spi_nor_parse_sfdp(struct spi_nor *nor, 2200 struct spi_nor_flash_parameter *params) 2201 { 2202 const struct sfdp_parameter_header *param_header, *bfpt_header; 2203 struct sfdp_parameter_header *param_headers = NULL; 2204 struct sfdp_header header; 2205 size_t psize; 2206 int i, err; 2207 2208 /* Get the SFDP header. */ 2209 err = spi_nor_read_sfdp(nor, 0, sizeof(header), &header); 2210 if (err < 0) 2211 return err; 2212 2213 /* Check the SFDP header version. */ 2214 if (le32_to_cpu(header.signature) != SFDP_SIGNATURE || 2215 header.major != SFDP_JESD216_MAJOR) 2216 return -EINVAL; 2217 2218 /* 2219 * Verify that the first and only mandatory parameter header is a 2220 * Basic Flash Parameter Table header as specified in JESD216. 2221 */ 2222 bfpt_header = &header.bfpt_header; 2223 if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID || 2224 bfpt_header->major != SFDP_JESD216_MAJOR) 2225 return -EINVAL; 2226 2227 /* 2228 * Allocate memory then read all parameter headers with a single 2229 * Read SFDP command. These parameter headers will actually be parsed 2230 * twice: a first time to get the latest revision of the basic flash 2231 * parameter table, then a second time to handle the supported optional 2232 * tables. 2233 * Hence we read the parameter headers once for all to reduce the 2234 * processing time. Also we use kmalloc() instead of devm_kmalloc() 2235 * because we don't need to keep these parameter headers: the allocated 2236 * memory is always released with kfree() before exiting this function. 2237 */ 2238 if (header.nph) { 2239 psize = header.nph * sizeof(*param_headers); 2240 2241 param_headers = kmalloc(psize, GFP_KERNEL); 2242 if (!param_headers) 2243 return -ENOMEM; 2244 2245 err = spi_nor_read_sfdp(nor, sizeof(header), 2246 psize, param_headers); 2247 if (err < 0) { 2248 dev_err(dev, "failed to read SFDP parameter headers\n"); 2249 goto exit; 2250 } 2251 } 2252 2253 /* 2254 * Check other parameter headers to get the latest revision of 2255 * the basic flash parameter table. 2256 */ 2257 for (i = 0; i < header.nph; i++) { 2258 param_header = ¶m_headers[i]; 2259 2260 if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID && 2261 param_header->major == SFDP_JESD216_MAJOR && 2262 (param_header->minor > bfpt_header->minor || 2263 (param_header->minor == bfpt_header->minor && 2264 param_header->length > bfpt_header->length))) 2265 bfpt_header = param_header; 2266 } 2267 2268 err = spi_nor_parse_bfpt(nor, bfpt_header, params); 2269 if (err) 2270 goto exit; 2271 2272 /* Parse other parameter headers. */ 2273 for (i = 0; i < header.nph; i++) { 2274 param_header = ¶m_headers[i]; 2275 2276 switch (SFDP_PARAM_HEADER_ID(param_header)) { 2277 case SFDP_SECTOR_MAP_ID: 2278 dev_info(dev, "non-uniform erase sector maps are not supported yet.\n"); 2279 break; 2280 2281 default: 2282 break; 2283 } 2284 2285 if (err) 2286 goto exit; 2287 } 2288 2289 exit: 2290 kfree(param_headers); 2291 return err; 2292 } 2293 #else 2294 static int spi_nor_parse_sfdp(struct spi_nor *nor, 2295 struct spi_nor_flash_parameter *params) 2296 { 2297 return -EINVAL; 2298 } 2299 #endif /* SPI_FLASH_SFDP_SUPPORT */ 2300 2301 static int spi_nor_init_params(struct spi_nor *nor, 2302 const struct flash_info *info, 2303 struct spi_nor_flash_parameter *params) 2304 { 2305 int ret; 2306 /* Set legacy flash parameters as default. */ 2307 memset(params, 0, sizeof(*params)); 2308 2309 /* Set SPI NOR sizes. */ 2310 params->size = info->sector_size * info->n_sectors; 2311 params->page_size = info->page_size; 2312 2313 /* (Fast) Read settings. */ 2314 params->hwcaps.mask |= SNOR_HWCAPS_READ; 2315 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ], 2316 0, 0, SPINOR_OP_READ, 2317 SNOR_PROTO_1_1_1); 2318 2319 if (!(info->flags & SPI_NOR_NO_FR)) { 2320 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST; 2321 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_FAST], 2322 0, 8, SPINOR_OP_READ_FAST, 2323 SNOR_PROTO_1_1_1); 2324 #ifdef CONFIG_SPI_FLASH_SPANSION 2325 if (cypress_s25hx_t(info)) 2326 params->reads[SNOR_CMD_READ_FAST].num_mode_clocks = 8; 2327 #endif 2328 } 2329 2330 if (info->flags & SPI_NOR_DUAL_READ) { 2331 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2; 2332 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_2], 2333 0, 8, SPINOR_OP_READ_1_1_2, 2334 SNOR_PROTO_1_1_2); 2335 } 2336 2337 if (info->flags & SPI_NOR_QUAD_READ) { 2338 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4; 2339 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_4], 2340 0, 8, SPINOR_OP_READ_1_1_4, 2341 SNOR_PROTO_1_1_4); 2342 } 2343 2344 /* Page Program settings. */ 2345 params->hwcaps.mask |= SNOR_HWCAPS_PP; 2346 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP], 2347 SPINOR_OP_PP, SNOR_PROTO_1_1_1); 2348 2349 if (info->flags & SPI_NOR_QUAD_READ) { 2350 params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4; 2351 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP_1_1_4], 2352 SPINOR_OP_PP_1_1_4, SNOR_PROTO_1_1_4); 2353 } 2354 2355 /* Select the procedure to set the Quad Enable bit. */ 2356 if (params->hwcaps.mask & (SNOR_HWCAPS_READ_QUAD | 2357 SNOR_HWCAPS_PP_QUAD)) { 2358 switch (JEDEC_MFR(info)) { 2359 #ifdef CONFIG_SPI_FLASH_MACRONIX 2360 case SNOR_MFR_MACRONIX: 2361 params->quad_enable = macronix_quad_enable; 2362 break; 2363 #endif 2364 case SNOR_MFR_ST: 2365 case SNOR_MFR_MICRON: 2366 case SNOR_MFR_ISSI: 2367 break; 2368 #ifdef CONFIG_SPI_FLASH_SPANSION 2369 case SNOR_MFR_CYPRESS: 2370 if (info->id[1] == 0x2a || info->id[1] == 0x2b) { 2371 params->quad_enable = spansion_quad_enable_volatile; 2372 } 2373 break; 2374 #endif 2375 2376 default: 2377 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND) 2378 /* Kept only for backward compatibility purpose. */ 2379 params->quad_enable = spansion_read_cr_quad_enable; 2380 #endif 2381 break; 2382 } 2383 } 2384 2385 /* Override the parameters with data read from SFDP tables. */ 2386 nor->addr_width = 0; 2387 nor->mtd.erasesize = 0; 2388 if ((info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)) && 2389 !(info->flags & SPI_NOR_SKIP_SFDP)) { 2390 struct spi_nor_flash_parameter sfdp_params; 2391 2392 memcpy(&sfdp_params, params, sizeof(sfdp_params)); 2393 if (spi_nor_parse_sfdp(nor, &sfdp_params)) { 2394 nor->addr_width = 0; 2395 nor->mtd.erasesize = 0; 2396 } else { 2397 memcpy(params, &sfdp_params, sizeof(*params)); 2398 #ifdef CONFIG_SPI_FLASH_SPANSION 2399 if (cypress_s25hx_t(info)) { 2400 /* BFPT fixup */ 2401 nor->erase_opcode = SPINOR_OP_SE_4B; 2402 nor->mtd.erasesize = info->sector_size; 2403 ret = set_4byte(nor, info, 1); 2404 if (ret) 2405 return ret; 2406 2407 nor->addr_width = 4; 2408 2409 /* SFDP fixup */ 2410 /* Default page size is 256-byte, but BFPT reports 512-byte */ 2411 params->page_size = 256; 2412 /* READ_FAST_4B (0Ch) requires mode cycles*/ 2413 params->reads[SNOR_CMD_READ_FAST].opcode = SPINOR_OP_READ_FAST_4B; 2414 params->reads[SNOR_CMD_READ_FAST].num_mode_clocks = 8; 2415 /* PP_1_1_4 is not supported */ 2416 params->hwcaps.mask &= ~SNOR_HWCAPS_PP_1_1_4; 2417 /* Use volatile register to enable quad */ 2418 params->quad_enable = spansion_quad_enable_volatile; 2419 } 2420 #endif 2421 } 2422 2423 /* need to disable hold/reset pin feature */ 2424 if (JEDEC_MFR(info) == SNOR_MFR_ST) 2425 params->quad_enable = micron_read_cr_quad_enable; 2426 2427 if (JEDEC_MFR(info) == SNOR_MFR_GIGADEVICE) 2428 params->quad_enable = winbond_sr2_bit1_quad_enable; 2429 } 2430 2431 return 0; 2432 } 2433 2434 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size) 2435 { 2436 size_t i; 2437 2438 for (i = 0; i < size; i++) 2439 if (table[i][0] == (int)hwcaps) 2440 return table[i][1]; 2441 2442 return -EINVAL; 2443 } 2444 2445 static int spi_nor_hwcaps_read2cmd(u32 hwcaps) 2446 { 2447 static const int hwcaps_read2cmd[][2] = { 2448 { SNOR_HWCAPS_READ, SNOR_CMD_READ }, 2449 { SNOR_HWCAPS_READ_FAST, SNOR_CMD_READ_FAST }, 2450 { SNOR_HWCAPS_READ_1_1_1_DTR, SNOR_CMD_READ_1_1_1_DTR }, 2451 { SNOR_HWCAPS_READ_1_1_2, SNOR_CMD_READ_1_1_2 }, 2452 { SNOR_HWCAPS_READ_1_2_2, SNOR_CMD_READ_1_2_2 }, 2453 { SNOR_HWCAPS_READ_2_2_2, SNOR_CMD_READ_2_2_2 }, 2454 { SNOR_HWCAPS_READ_1_2_2_DTR, SNOR_CMD_READ_1_2_2_DTR }, 2455 { SNOR_HWCAPS_READ_1_1_4, SNOR_CMD_READ_1_1_4 }, 2456 { SNOR_HWCAPS_READ_1_4_4, SNOR_CMD_READ_1_4_4 }, 2457 { SNOR_HWCAPS_READ_4_4_4, SNOR_CMD_READ_4_4_4 }, 2458 { SNOR_HWCAPS_READ_1_4_4_DTR, SNOR_CMD_READ_1_4_4_DTR }, 2459 { SNOR_HWCAPS_READ_1_1_8, SNOR_CMD_READ_1_1_8 }, 2460 { SNOR_HWCAPS_READ_1_8_8, SNOR_CMD_READ_1_8_8 }, 2461 { SNOR_HWCAPS_READ_8_8_8, SNOR_CMD_READ_8_8_8 }, 2462 { SNOR_HWCAPS_READ_1_8_8_DTR, SNOR_CMD_READ_1_8_8_DTR }, 2463 }; 2464 2465 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd, 2466 ARRAY_SIZE(hwcaps_read2cmd)); 2467 } 2468 2469 static int spi_nor_hwcaps_pp2cmd(u32 hwcaps) 2470 { 2471 static const int hwcaps_pp2cmd[][2] = { 2472 { SNOR_HWCAPS_PP, SNOR_CMD_PP }, 2473 { SNOR_HWCAPS_PP_1_1_4, SNOR_CMD_PP_1_1_4 }, 2474 { SNOR_HWCAPS_PP_1_4_4, SNOR_CMD_PP_1_4_4 }, 2475 { SNOR_HWCAPS_PP_4_4_4, SNOR_CMD_PP_4_4_4 }, 2476 { SNOR_HWCAPS_PP_1_1_8, SNOR_CMD_PP_1_1_8 }, 2477 { SNOR_HWCAPS_PP_1_8_8, SNOR_CMD_PP_1_8_8 }, 2478 { SNOR_HWCAPS_PP_8_8_8, SNOR_CMD_PP_8_8_8 }, 2479 }; 2480 2481 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd, 2482 ARRAY_SIZE(hwcaps_pp2cmd)); 2483 } 2484 2485 static int spi_nor_select_read(struct spi_nor *nor, 2486 const struct spi_nor_flash_parameter *params, 2487 u32 shared_hwcaps) 2488 { 2489 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1; 2490 const struct spi_nor_read_command *read; 2491 2492 if (best_match < 0) 2493 return -EINVAL; 2494 2495 cmd = spi_nor_hwcaps_read2cmd(BIT(best_match)); 2496 if (cmd < 0) 2497 return -EINVAL; 2498 2499 read = ¶ms->reads[cmd]; 2500 nor->read_opcode = read->opcode; 2501 nor->read_proto = read->proto; 2502 2503 /* 2504 * In the spi-nor framework, we don't need to make the difference 2505 * between mode clock cycles and wait state clock cycles. 2506 * Indeed, the value of the mode clock cycles is used by a QSPI 2507 * flash memory to know whether it should enter or leave its 0-4-4 2508 * (Continuous Read / XIP) mode. 2509 * eXecution In Place is out of the scope of the mtd sub-system. 2510 * Hence we choose to merge both mode and wait state clock cycles 2511 * into the so called dummy clock cycles. 2512 */ 2513 nor->read_dummy = read->num_mode_clocks + read->num_wait_states; 2514 return 0; 2515 } 2516 2517 static int spi_nor_select_pp(struct spi_nor *nor, 2518 const struct spi_nor_flash_parameter *params, 2519 u32 shared_hwcaps) 2520 { 2521 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1; 2522 const struct spi_nor_pp_command *pp; 2523 2524 if (best_match < 0) 2525 return -EINVAL; 2526 2527 cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match)); 2528 if (cmd < 0) 2529 return -EINVAL; 2530 2531 pp = ¶ms->page_programs[cmd]; 2532 nor->program_opcode = pp->opcode; 2533 nor->write_proto = pp->proto; 2534 return 0; 2535 } 2536 2537 static int spi_nor_select_erase(struct spi_nor *nor, 2538 const struct flash_info *info) 2539 { 2540 struct mtd_info *mtd = &nor->mtd; 2541 2542 /* Do nothing if already configured from SFDP. */ 2543 if (mtd->erasesize) 2544 return 0; 2545 2546 #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS 2547 /* prefer "small sector" erase if possible */ 2548 if (info->flags & SECT_4K) { 2549 nor->erase_opcode = SPINOR_OP_BE_4K; 2550 mtd->erasesize = 4096; 2551 } else if (info->flags & SECT_4K_PMC) { 2552 nor->erase_opcode = SPINOR_OP_BE_4K_PMC; 2553 mtd->erasesize = 4096; 2554 } else 2555 #endif 2556 { 2557 nor->erase_opcode = SPINOR_OP_SE; 2558 mtd->erasesize = info->sector_size; 2559 } 2560 return 0; 2561 } 2562 2563 static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info, 2564 const struct spi_nor_flash_parameter *params, 2565 const struct spi_nor_hwcaps *hwcaps) 2566 { 2567 u32 ignored_mask, shared_mask; 2568 bool enable_quad_io; 2569 int err; 2570 2571 /* 2572 * Keep only the hardware capabilities supported by both the SPI 2573 * controller and the SPI flash memory. 2574 */ 2575 shared_mask = hwcaps->mask & params->hwcaps.mask; 2576 2577 /* SPI n-n-n protocols are not supported yet. */ 2578 ignored_mask = (SNOR_HWCAPS_READ_1_1_1_DTR | 2579 SNOR_HWCAPS_READ_1_2_2 | 2580 SNOR_HWCAPS_READ_1_2_2_DTR | 2581 SNOR_HWCAPS_READ_2_2_2 | 2582 SNOR_HWCAPS_READ_1_4_4 | 2583 SNOR_HWCAPS_READ_1_4_4_DTR | 2584 SNOR_HWCAPS_READ_4_4_4 | 2585 SNOR_HWCAPS_READ_8_8_8 | 2586 SNOR_HWCAPS_PP_1_4_4 | 2587 SNOR_HWCAPS_PP_4_4_4 | 2588 SNOR_HWCAPS_PP_8_8_8); 2589 if (shared_mask & ignored_mask) { 2590 dev_dbg(nor->dev, 2591 "SPI n-n-n protocols are not supported yet.\n"); 2592 shared_mask &= ~ignored_mask; 2593 } 2594 2595 /* Select the (Fast) Read command. */ 2596 err = spi_nor_select_read(nor, params, shared_mask); 2597 if (err) { 2598 dev_dbg(nor->dev, 2599 "can't select read settings supported by both the SPI controller and memory.\n"); 2600 return err; 2601 } 2602 2603 /* Select the Page Program command. */ 2604 err = spi_nor_select_pp(nor, params, shared_mask); 2605 if (err) { 2606 dev_dbg(nor->dev, 2607 "can't select write settings supported by both the SPI controller and memory.\n"); 2608 return err; 2609 } 2610 2611 /* Select the Sector Erase command. */ 2612 err = spi_nor_select_erase(nor, info); 2613 if (err) { 2614 dev_dbg(nor->dev, 2615 "can't select erase settings supported by both the SPI controller and memory.\n"); 2616 return err; 2617 } 2618 2619 /* Enable Quad I/O if needed. */ 2620 enable_quad_io = (spi_nor_get_protocol_width(nor->read_proto) == 4 || 2621 spi_nor_get_protocol_width(nor->write_proto) == 4); 2622 if (enable_quad_io && params->quad_enable) 2623 nor->quad_enable = params->quad_enable; 2624 else 2625 nor->quad_enable = NULL; 2626 2627 return 0; 2628 } 2629 2630 static int spi_nor_init(struct spi_nor *nor) 2631 { 2632 int err; 2633 2634 /* 2635 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up 2636 * with the software protection bits set 2637 */ 2638 if (JEDEC_MFR(nor->info) == SNOR_MFR_ATMEL || 2639 JEDEC_MFR(nor->info) == SNOR_MFR_INTEL || 2640 JEDEC_MFR(nor->info) == SNOR_MFR_SST || 2641 nor->info->flags & SPI_NOR_HAS_LOCK) { 2642 write_enable(nor); 2643 write_sr(nor, 0); 2644 spi_nor_wait_till_ready(nor); 2645 } 2646 2647 if (nor->quad_enable) { 2648 err = nor->quad_enable(nor); 2649 if (err) { 2650 dev_dbg(nor->dev, "quad mode not supported\n"); 2651 return err; 2652 } 2653 } 2654 2655 if (nor->addr_width == 4 && 2656 (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION)) { 2657 2658 /* 2659 * If the RESET# pin isn't hooked up properly, or the system 2660 * otherwise doesn't perform a reset command in the boot 2661 * sequence, it's impossible to 100% protect against unexpected 2662 * reboots (e.g., crashes). Warn the user (or hopefully, system 2663 * designer) that this is bad. 2664 */ 2665 if (nor->flags & SNOR_F_BROKEN_RESET) 2666 printf("enabling reset hack; may not recover from unexpected reboots\n"); 2667 set_4byte(nor, nor->info, 1); 2668 } 2669 2670 return 0; 2671 } 2672 2673 int spi_nor_scan(struct spi_nor *nor) 2674 { 2675 struct spi_nor_flash_parameter params; 2676 const struct flash_info *info = NULL; 2677 struct mtd_info *mtd = &nor->mtd; 2678 struct spi_nor_hwcaps hwcaps = { 2679 .mask = SNOR_HWCAPS_READ | 2680 SNOR_HWCAPS_READ_FAST | 2681 SNOR_HWCAPS_PP, 2682 }; 2683 struct spi_slave *spi = nor->spi; 2684 int ret; 2685 2686 /* Reset SPI protocol for all commands. */ 2687 nor->reg_proto = SNOR_PROTO_1_1_1; 2688 nor->read_proto = SNOR_PROTO_1_1_1; 2689 nor->write_proto = SNOR_PROTO_1_1_1; 2690 nor->read = spi_nor_read_data; 2691 nor->write = spi_nor_write_data; 2692 nor->read_reg = spi_nor_read_reg; 2693 nor->write_reg = spi_nor_write_reg; 2694 2695 if (spi->mode & SPI_RX_QUAD) { 2696 hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4; 2697 2698 if (spi->mode & SPI_TX_QUAD) 2699 hwcaps.mask |= (SNOR_HWCAPS_READ_1_4_4 | 2700 SNOR_HWCAPS_PP_1_1_4 | 2701 SNOR_HWCAPS_PP_1_4_4); 2702 } else if (spi->mode & SPI_RX_DUAL) { 2703 hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2; 2704 2705 if (spi->mode & SPI_TX_DUAL) 2706 hwcaps.mask |= SNOR_HWCAPS_READ_1_2_2; 2707 } 2708 2709 info = spi_nor_read_id(nor); 2710 if (IS_ERR_OR_NULL(info)) 2711 return -ENOENT; 2712 /* Parse the Serial Flash Discoverable Parameters table. */ 2713 ret = spi_nor_init_params(nor, info, ¶ms); 2714 if (ret) 2715 return ret; 2716 2717 if (!mtd->name) 2718 mtd->name = info->name; 2719 mtd->priv = nor; 2720 mtd->type = MTD_NORFLASH; 2721 mtd->writesize = 1; 2722 mtd->flags = MTD_CAP_NORFLASH; 2723 mtd->size = params.size; 2724 mtd->_erase = spi_nor_erase; 2725 mtd->_read = spi_nor_read; 2726 2727 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST) 2728 /* NOR protection support for STmicro/Micron chips and similar */ 2729 if (JEDEC_MFR(info) == SNOR_MFR_ST || 2730 JEDEC_MFR(info) == SNOR_MFR_MICRON || 2731 JEDEC_MFR(info) == SNOR_MFR_SST || 2732 info->flags & SPI_NOR_HAS_LOCK) { 2733 nor->flash_lock = stm_lock; 2734 nor->flash_unlock = stm_unlock; 2735 nor->flash_is_locked = stm_is_locked; 2736 } 2737 #endif 2738 2739 #ifdef CONFIG_SPI_FLASH_SST 2740 /* sst nor chips use AAI word program */ 2741 if (info->flags & SST_WRITE) 2742 mtd->_write = sst_write; 2743 else 2744 #endif 2745 mtd->_write = spi_nor_write; 2746 2747 if (info->flags & USE_FSR) 2748 nor->flags |= SNOR_F_USE_FSR; 2749 if (info->flags & SPI_NOR_HAS_TB) 2750 nor->flags |= SNOR_F_HAS_SR_TB; 2751 if (info->flags & NO_CHIP_ERASE) 2752 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE; 2753 if (info->flags & USE_CLSR) 2754 nor->flags |= SNOR_F_USE_CLSR; 2755 2756 if (info->flags & SPI_NOR_NO_ERASE) 2757 mtd->flags |= MTD_NO_ERASE; 2758 2759 nor->page_size = params.page_size; 2760 mtd->writebufsize = nor->page_size; 2761 2762 #ifdef CONFIG_SPI_FLASH_SPANSION 2763 if (cypress_s25hx_t(info)) { 2764 /* 2765 * The Cypress Semper family has transparent ECC. To preserve 2766 * ECC enabled, multi-pass programming within the same 16-byte 2767 * ECC data unit needs to be avoided. Set writesize to the page 2768 * size and remove the MTD_BIT_WRITEABLE flag in mtd_info to 2769 * prevent multi-pass programming. 2770 */ 2771 nor->mtd.writesize = params.page_size; 2772 nor->mtd.flags &= ~MTD_BIT_WRITEABLE; 2773 ret = s25hx_t_setup(nor, info); 2774 if (ret) { 2775 dev_err(nor->dev, "fail to setup s25hx_t flash\n"); 2776 return ret; 2777 } 2778 } 2779 #endif 2780 2781 /* Some devices cannot do fast-read, no matter what DT tells us */ 2782 if ((info->flags & SPI_NOR_NO_FR) || (spi->mode & SPI_RX_SLOW)) 2783 params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST; 2784 2785 /* 2786 * Configure the SPI memory: 2787 * - select op codes for (Fast) Read, Page Program and Sector Erase. 2788 * - set the number of dummy cycles (mode cycles + wait states). 2789 * - set the SPI protocols for register and memory accesses. 2790 * - set the Quad Enable bit if needed (required by SPI x-y-4 protos). 2791 */ 2792 ret = spi_nor_setup(nor, info, ¶ms, &hwcaps); 2793 if (ret) 2794 return ret; 2795 2796 if (nor->addr_width) { 2797 /* already configured from SFDP */ 2798 } else if (info->addr_width) { 2799 nor->addr_width = info->addr_width; 2800 } else if (mtd->size > SZ_16M) { 2801 #ifndef CONFIG_SPI_FLASH_BAR 2802 /* enable 4-byte addressing if the device exceeds 16MiB */ 2803 nor->addr_width = 4; 2804 if (JEDEC_MFR(info) == SNOR_MFR_SPANSION || 2805 info->flags & SPI_NOR_4B_OPCODES) 2806 spi_nor_set_4byte_opcodes(nor, info); 2807 #else 2808 /* Configure the BAR - discover bank cmds and read current bank */ 2809 nor->addr_width = 3; 2810 ret = read_bar(nor, info); 2811 if (ret < 0) 2812 return ret; 2813 #endif 2814 } else { 2815 nor->addr_width = 3; 2816 } 2817 2818 if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) { 2819 dev_dbg(dev, "address width is too large: %u\n", 2820 nor->addr_width); 2821 return -EINVAL; 2822 } 2823 2824 /* Send all the required SPI flash commands to initialize device */ 2825 nor->info = info; 2826 ret = spi_nor_init(nor); 2827 if (ret) 2828 return ret; 2829 2830 nor->name = mtd->name; 2831 nor->size = mtd->size; 2832 nor->erase_size = mtd->erasesize; 2833 nor->sector_size = mtd->erasesize; 2834 2835 #ifndef CONFIG_SPL_BUILD 2836 printf("SF: Detected %s with page size ", nor->name); 2837 print_size(nor->page_size, ", erase size "); 2838 print_size(nor->erase_size, ", total "); 2839 print_size(nor->size, ""); 2840 puts("\n"); 2841 #endif 2842 2843 return 0; 2844 } 2845 2846 /* U-Boot specific functions, need to extend MTD to support these */ 2847 int spi_flash_cmd_get_sw_write_prot(struct spi_nor *nor) 2848 { 2849 int sr = read_sr(nor); 2850 2851 if (sr < 0) 2852 return sr; 2853 2854 return (sr >> 2) & 7; 2855 } 2856