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