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