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