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 10 #include <linux/cleanup.h> 11 #include <linux/delay.h> 12 #include <linux/device.h> 13 #include <linux/err.h> 14 #include <linux/errno.h> 15 #include <linux/math64.h> 16 #include <linux/module.h> 17 #include <linux/mtd/mtd.h> 18 #include <linux/mtd/spi-nor.h> 19 #include <linux/mutex.h> 20 #include <linux/of.h> 21 #include <linux/regulator/consumer.h> 22 #include <linux/sched/task_stack.h> 23 #include <linux/sizes.h> 24 #include <linux/slab.h> 25 #include <linux/spi/flash.h> 26 27 #include "core.h" 28 29 /* Define max times to check status register before we give up. */ 30 31 /* 32 * For everything but full-chip erase; probably could be much smaller, but kept 33 * around for safety for now 34 */ 35 #define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ) 36 37 /* 38 * For full-chip erase, calibrated to a 2MB flash (M25P16); should be scaled up 39 * for larger flash 40 */ 41 #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ) 42 43 #define SPI_NOR_MAX_ADDR_NBYTES 4 44 45 #define SPI_NOR_SRST_SLEEP_MIN 200 46 #define SPI_NOR_SRST_SLEEP_MAX 400 47 48 /** 49 * spi_nor_get_cmd_ext() - Get the command opcode extension based on the 50 * extension type. 51 * @nor: pointer to a 'struct spi_nor' 52 * @op: pointer to the 'struct spi_mem_op' whose properties 53 * need to be initialized. 54 * 55 * Right now, only "repeat" and "invert" are supported. 56 * 57 * Return: The opcode extension. 58 */ 59 static u8 spi_nor_get_cmd_ext(const struct spi_nor *nor, 60 const struct spi_mem_op *op) 61 { 62 switch (nor->params->cmd_ext_type) { 63 case SPI_NOR_EXT_INVERT: 64 return ~op->cmd.opcode; 65 66 case SPI_NOR_EXT_REPEAT: 67 return op->cmd.opcode; 68 69 default: 70 dev_err(nor->dev, "Unknown command extension type\n"); 71 return 0; 72 } 73 } 74 75 /** 76 * spi_nor_spimem_setup_op() - Set up common properties of a spi-mem op. 77 * @nor: pointer to a 'struct spi_nor' 78 * @op: pointer to the 'struct spi_mem_op' whose properties 79 * need to be initialized. 80 * @proto: the protocol from which the properties need to be set. 81 */ 82 void spi_nor_spimem_setup_op(const struct spi_nor *nor, 83 struct spi_mem_op *op, 84 const enum spi_nor_protocol proto) 85 { 86 struct spi_nor_flash_parameter *params = nor->params; 87 u8 ext; 88 89 op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(proto); 90 91 if (op->addr.nbytes) 92 op->addr.buswidth = spi_nor_get_protocol_addr_nbits(proto); 93 94 if (op->dummy.nbytes) 95 op->dummy.buswidth = spi_nor_get_protocol_addr_nbits(proto); 96 97 if (op->data.nbytes) 98 op->data.buswidth = spi_nor_get_protocol_data_nbits(proto); 99 100 if (spi_nor_protocol_is_dtr(proto)) { 101 /* 102 * SPIMEM supports mixed DTR modes, but right now we can only 103 * have all phases either DTR or STR. IOW, SPIMEM can have 104 * something like 4S-4D-4D, but SPI NOR can't. So, set all 4 105 * phases to either DTR or STR. 106 */ 107 op->cmd.dtr = true; 108 op->addr.dtr = true; 109 op->dummy.dtr = true; 110 op->data.dtr = true; 111 112 /* 2 bytes per clock cycle in DTR mode. */ 113 op->dummy.nbytes *= 2; 114 115 ext = spi_nor_get_cmd_ext(nor, op); 116 op->cmd.opcode = (op->cmd.opcode << 8) | ext; 117 op->cmd.nbytes = 2; 118 } 119 120 if (proto == SNOR_PROTO_8_8_8_DTR && params->flags & SNOR_F_SWAP16) 121 op->data.swap16 = true; 122 } 123 124 /** 125 * spi_nor_spimem_bounce() - check if a bounce buffer is needed for the data 126 * transfer 127 * @nor: pointer to 'struct spi_nor' 128 * @op: pointer to 'struct spi_mem_op' template for transfer 129 * 130 * If we have to use the bounce buffer, the data field in @op will be updated. 131 * 132 * Return: true if the bounce buffer is needed, false if not 133 */ 134 static bool spi_nor_spimem_bounce(struct spi_nor *nor, struct spi_mem_op *op) 135 { 136 /* op->data.buf.in occupies the same memory as op->data.buf.out */ 137 if (object_is_on_stack(op->data.buf.in) || 138 !virt_addr_valid(op->data.buf.in)) { 139 if (op->data.nbytes > nor->bouncebuf_size) 140 op->data.nbytes = nor->bouncebuf_size; 141 op->data.buf.in = nor->bouncebuf; 142 return true; 143 } 144 145 return false; 146 } 147 148 /** 149 * spi_nor_spimem_exec_op() - execute a memory operation 150 * @nor: pointer to 'struct spi_nor' 151 * @op: pointer to 'struct spi_mem_op' template for transfer 152 * 153 * Return: 0 on success, -error otherwise. 154 */ 155 static int spi_nor_spimem_exec_op(struct spi_nor *nor, struct spi_mem_op *op) 156 { 157 int error; 158 159 error = spi_mem_adjust_op_size(nor->spimem, op); 160 if (error) 161 return error; 162 163 return spi_mem_exec_op(nor->spimem, op); 164 } 165 166 int spi_nor_controller_ops_read_reg(struct spi_nor *nor, u8 opcode, 167 u8 *buf, size_t len) 168 { 169 if (spi_nor_protocol_is_dtr(nor->reg_proto)) 170 return -EOPNOTSUPP; 171 172 return nor->controller_ops->read_reg(nor, opcode, buf, len); 173 } 174 175 int spi_nor_controller_ops_write_reg(struct spi_nor *nor, u8 opcode, 176 const u8 *buf, size_t len) 177 { 178 if (spi_nor_protocol_is_dtr(nor->reg_proto)) 179 return -EOPNOTSUPP; 180 181 return nor->controller_ops->write_reg(nor, opcode, buf, len); 182 } 183 184 static int spi_nor_controller_ops_erase(struct spi_nor *nor, loff_t offs) 185 { 186 if (spi_nor_protocol_is_dtr(nor->reg_proto)) 187 return -EOPNOTSUPP; 188 189 return nor->controller_ops->erase(nor, offs); 190 } 191 192 /** 193 * spi_nor_spimem_read_data() - read data from flash's memory region via 194 * spi-mem 195 * @nor: pointer to 'struct spi_nor' 196 * @from: offset to read from 197 * @len: number of bytes to read 198 * @buf: pointer to dst buffer 199 * 200 * Return: number of bytes read successfully, -errno otherwise 201 */ 202 static ssize_t spi_nor_spimem_read_data(struct spi_nor *nor, loff_t from, 203 size_t len, u8 *buf) 204 { 205 struct spi_mem_op op = 206 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0), 207 SPI_MEM_OP_ADDR(nor->addr_nbytes, from, 0), 208 SPI_MEM_OP_DUMMY(nor->read_dummy, 0), 209 SPI_MEM_OP_DATA_IN(len, buf, 0)); 210 bool usebouncebuf; 211 ssize_t nbytes; 212 int error; 213 214 spi_nor_spimem_setup_op(nor, &op, nor->read_proto); 215 216 /* convert the dummy cycles to the number of bytes */ 217 op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8; 218 if (spi_nor_protocol_is_dtr(nor->read_proto)) 219 op.dummy.nbytes *= 2; 220 221 usebouncebuf = spi_nor_spimem_bounce(nor, &op); 222 223 if (nor->dirmap.rdesc) { 224 nbytes = spi_mem_dirmap_read(nor->dirmap.rdesc, op.addr.val, 225 op.data.nbytes, op.data.buf.in); 226 } else { 227 error = spi_nor_spimem_exec_op(nor, &op); 228 if (error) 229 return error; 230 nbytes = op.data.nbytes; 231 } 232 233 if (usebouncebuf && nbytes > 0) 234 memcpy(buf, op.data.buf.in, nbytes); 235 236 return nbytes; 237 } 238 239 /** 240 * spi_nor_read_data() - read data from flash memory 241 * @nor: pointer to 'struct spi_nor' 242 * @from: offset to read from 243 * @len: number of bytes to read 244 * @buf: pointer to dst buffer 245 * 246 * Return: number of bytes read successfully, -errno otherwise 247 */ 248 ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len, u8 *buf) 249 { 250 if (nor->spimem) 251 return spi_nor_spimem_read_data(nor, from, len, buf); 252 253 return nor->controller_ops->read(nor, from, len, buf); 254 } 255 256 /** 257 * spi_nor_spimem_write_data() - write data to flash memory via 258 * spi-mem 259 * @nor: pointer to 'struct spi_nor' 260 * @to: offset to write to 261 * @len: number of bytes to write 262 * @buf: pointer to src buffer 263 * 264 * Return: number of bytes written successfully, -errno otherwise 265 */ 266 static ssize_t spi_nor_spimem_write_data(struct spi_nor *nor, loff_t to, 267 size_t len, const u8 *buf) 268 { 269 struct spi_mem_op op = 270 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0), 271 SPI_MEM_OP_ADDR(nor->addr_nbytes, to, 0), 272 SPI_MEM_OP_NO_DUMMY, 273 SPI_MEM_OP_DATA_OUT(len, buf, 0)); 274 ssize_t nbytes; 275 int error; 276 277 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second) 278 op.addr.nbytes = 0; 279 280 spi_nor_spimem_setup_op(nor, &op, nor->write_proto); 281 282 if (spi_nor_spimem_bounce(nor, &op)) 283 memcpy(nor->bouncebuf, buf, op.data.nbytes); 284 285 if (nor->dirmap.wdesc) { 286 nbytes = spi_mem_dirmap_write(nor->dirmap.wdesc, op.addr.val, 287 op.data.nbytes, op.data.buf.out); 288 } else { 289 error = spi_nor_spimem_exec_op(nor, &op); 290 if (error) 291 return error; 292 nbytes = op.data.nbytes; 293 } 294 295 return nbytes; 296 } 297 298 /** 299 * spi_nor_write_data() - write data to flash memory 300 * @nor: pointer to 'struct spi_nor' 301 * @to: offset to write to 302 * @len: number of bytes to write 303 * @buf: pointer to src buffer 304 * 305 * Return: number of bytes written successfully, -errno otherwise 306 */ 307 ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len, 308 const u8 *buf) 309 { 310 if (nor->spimem) 311 return spi_nor_spimem_write_data(nor, to, len, buf); 312 313 return nor->controller_ops->write(nor, to, len, buf); 314 } 315 316 /** 317 * spi_nor_read_any_reg() - read any register from flash memory, nonvolatile or 318 * volatile. 319 * @nor: pointer to 'struct spi_nor'. 320 * @op: SPI memory operation. op->data.buf must be DMA-able. 321 * @proto: SPI protocol to use for the register operation. 322 * 323 * Return: zero on success, -errno otherwise 324 */ 325 int spi_nor_read_any_reg(struct spi_nor *nor, struct spi_mem_op *op, 326 enum spi_nor_protocol proto) 327 { 328 if (!nor->spimem) 329 return -EOPNOTSUPP; 330 331 spi_nor_spimem_setup_op(nor, op, proto); 332 return spi_nor_spimem_exec_op(nor, op); 333 } 334 335 /** 336 * spi_nor_write_any_volatile_reg() - write any volatile register to flash 337 * memory. 338 * @nor: pointer to 'struct spi_nor' 339 * @op: SPI memory operation. op->data.buf must be DMA-able. 340 * @proto: SPI protocol to use for the register operation. 341 * 342 * Writing volatile registers are instant according to some manufacturers 343 * (Cypress, Micron) and do not need any status polling. 344 * 345 * Return: zero on success, -errno otherwise 346 */ 347 int spi_nor_write_any_volatile_reg(struct spi_nor *nor, struct spi_mem_op *op, 348 enum spi_nor_protocol proto) 349 { 350 int ret; 351 352 if (!nor->spimem) 353 return -EOPNOTSUPP; 354 355 ret = spi_nor_write_enable(nor); 356 if (ret) 357 return ret; 358 spi_nor_spimem_setup_op(nor, op, proto); 359 return spi_nor_spimem_exec_op(nor, op); 360 } 361 362 /** 363 * spi_nor_write_enable() - Set write enable latch with Write Enable command. 364 * @nor: pointer to 'struct spi_nor'. 365 * 366 * Return: 0 on success, -errno otherwise. 367 */ 368 int spi_nor_write_enable(struct spi_nor *nor) 369 { 370 int ret; 371 372 if (nor->spimem) { 373 struct spi_mem_op op = SPI_NOR_WREN_OP; 374 375 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); 376 377 ret = spi_mem_exec_op(nor->spimem, &op); 378 } else { 379 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WREN, 380 NULL, 0); 381 } 382 383 if (ret) 384 dev_dbg(nor->dev, "error %d on Write Enable\n", ret); 385 386 return ret; 387 } 388 389 /** 390 * spi_nor_write_disable() - Send Write Disable instruction to the chip. 391 * @nor: pointer to 'struct spi_nor'. 392 * 393 * Return: 0 on success, -errno otherwise. 394 */ 395 int spi_nor_write_disable(struct spi_nor *nor) 396 { 397 int ret; 398 399 if (nor->spimem) { 400 struct spi_mem_op op = SPI_NOR_WRDI_OP; 401 402 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); 403 404 ret = spi_mem_exec_op(nor->spimem, &op); 405 } else { 406 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_WRDI, 407 NULL, 0); 408 } 409 410 if (ret) 411 dev_dbg(nor->dev, "error %d on Write Disable\n", ret); 412 413 return ret; 414 } 415 416 /** 417 * spi_nor_read_id() - Read the JEDEC ID. 418 * @nor: pointer to 'struct spi_nor'. 419 * @naddr: number of address bytes to send. Can be zero if the operation 420 * does not need to send an address. 421 * @ndummy: number of dummy bytes to send after an opcode or address. Can 422 * be zero if the operation does not require dummy bytes. 423 * @id: pointer to a DMA-able buffer where the value of the JEDEC ID 424 * will be written. 425 * @proto: the SPI protocol for register operation. 426 * 427 * Return: 0 on success, -errno otherwise. 428 */ 429 int spi_nor_read_id(struct spi_nor *nor, u8 naddr, u8 ndummy, u8 *id, 430 enum spi_nor_protocol proto) 431 { 432 int ret; 433 434 if (nor->spimem) { 435 struct spi_mem_op op = 436 SPI_NOR_READID_OP(naddr, ndummy, id, SPI_NOR_MAX_ID_LEN); 437 438 spi_nor_spimem_setup_op(nor, &op, proto); 439 ret = spi_mem_exec_op(nor->spimem, &op); 440 } else { 441 ret = nor->controller_ops->read_reg(nor, SPINOR_OP_RDID, id, 442 SPI_NOR_MAX_ID_LEN); 443 } 444 return ret; 445 } 446 447 /** 448 * spi_nor_set_4byte_addr_mode_en4b_ex4b() - Enter/Exit 4-byte address mode 449 * using SPINOR_OP_EN4B/SPINOR_OP_EX4B. Typically used by 450 * Winbond and Macronix. 451 * @nor: pointer to 'struct spi_nor'. 452 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte 453 * address mode. 454 * 455 * Return: 0 on success, -errno otherwise. 456 */ 457 int spi_nor_set_4byte_addr_mode_en4b_ex4b(struct spi_nor *nor, bool enable) 458 { 459 int ret; 460 461 if (nor->spimem) { 462 struct spi_mem_op op = SPI_NOR_EN4B_EX4B_OP(enable); 463 464 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); 465 466 ret = spi_mem_exec_op(nor->spimem, &op); 467 } else { 468 ret = spi_nor_controller_ops_write_reg(nor, 469 enable ? SPINOR_OP_EN4B : 470 SPINOR_OP_EX4B, 471 NULL, 0); 472 } 473 474 if (ret) 475 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret); 476 477 return ret; 478 } 479 480 /** 481 * spi_nor_set_4byte_addr_mode_wren_en4b_ex4b() - Set 4-byte address mode using 482 * SPINOR_OP_WREN followed by SPINOR_OP_EN4B or SPINOR_OP_EX4B. Typically used 483 * by ST and Micron flashes. 484 * @nor: pointer to 'struct spi_nor'. 485 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte 486 * address mode. 487 * 488 * Return: 0 on success, -errno otherwise. 489 */ 490 int spi_nor_set_4byte_addr_mode_wren_en4b_ex4b(struct spi_nor *nor, bool enable) 491 { 492 int ret; 493 494 ret = spi_nor_write_enable(nor); 495 if (ret) 496 return ret; 497 498 ret = spi_nor_set_4byte_addr_mode_en4b_ex4b(nor, enable); 499 if (ret) 500 return ret; 501 502 return spi_nor_write_disable(nor); 503 } 504 505 /** 506 * spi_nor_set_4byte_addr_mode_brwr() - Set 4-byte address mode using 507 * SPINOR_OP_BRWR. Typically used by Spansion flashes. 508 * @nor: pointer to 'struct spi_nor'. 509 * @enable: true to enter the 4-byte address mode, false to exit the 4-byte 510 * address mode. 511 * 512 * 8-bit volatile bank register used to define A[30:A24] bits. MSB (bit[7]) is 513 * used to enable/disable 4-byte address mode. When MSB is set to ‘1’, 4-byte 514 * address mode is active and A[30:24] bits are don’t care. Write instruction is 515 * SPINOR_OP_BRWR(17h) with 1 byte of data. 516 * 517 * Return: 0 on success, -errno otherwise. 518 */ 519 int spi_nor_set_4byte_addr_mode_brwr(struct spi_nor *nor, bool enable) 520 { 521 int ret; 522 523 nor->bouncebuf[0] = enable << 7; 524 525 if (nor->spimem) { 526 struct spi_mem_op op = SPI_NOR_BRWR_OP(nor->bouncebuf); 527 528 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); 529 530 ret = spi_mem_exec_op(nor->spimem, &op); 531 } else { 532 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_BRWR, 533 nor->bouncebuf, 1); 534 } 535 536 if (ret) 537 dev_dbg(nor->dev, "error %d setting 4-byte mode\n", ret); 538 539 return ret; 540 } 541 542 /** 543 * spi_nor_sr_ready() - Query the Status Register to see if the flash is ready 544 * for new commands. 545 * @nor: pointer to 'struct spi_nor'. 546 * 547 * Return: 1 if ready, 0 if not ready, -errno on errors. 548 */ 549 int spi_nor_sr_ready(struct spi_nor *nor) 550 { 551 int ret; 552 u8 sr; 553 554 ret = spi_nor_read_sr1(nor, &sr); 555 if (ret) 556 return ret; 557 558 return !(sr & SR_WIP); 559 } 560 561 /** 562 * spi_nor_use_parallel_locking() - Checks if RWW locking scheme shall be used 563 * @nor: pointer to 'struct spi_nor'. 564 * 565 * Return: true if parallel locking is enabled, false otherwise. 566 */ 567 static bool spi_nor_use_parallel_locking(struct spi_nor *nor) 568 { 569 return nor->params->flags & SNOR_F_RWW; 570 } 571 572 /* Locking helpers for status read operations */ 573 static int spi_nor_rww_start_rdst(struct spi_nor *nor) 574 { 575 struct spi_nor_rww *rww = &nor->rww; 576 577 guard(mutex)(&nor->lock); 578 579 if (rww->ongoing_io || rww->ongoing_rd) 580 return -EAGAIN; 581 582 rww->ongoing_io = true; 583 rww->ongoing_rd = true; 584 585 return 0; 586 } 587 588 static void spi_nor_rww_end_rdst(struct spi_nor *nor) 589 { 590 struct spi_nor_rww *rww = &nor->rww; 591 592 guard(mutex)(&nor->lock); 593 594 rww->ongoing_io = false; 595 rww->ongoing_rd = false; 596 } 597 598 static int spi_nor_lock_rdst(struct spi_nor *nor) 599 { 600 if (spi_nor_use_parallel_locking(nor)) 601 return spi_nor_rww_start_rdst(nor); 602 603 return 0; 604 } 605 606 static void spi_nor_unlock_rdst(struct spi_nor *nor) 607 { 608 if (spi_nor_use_parallel_locking(nor)) { 609 spi_nor_rww_end_rdst(nor); 610 wake_up(&nor->rww.wait); 611 } 612 } 613 614 /** 615 * spi_nor_ready() - Query the flash to see if it is ready for new commands. 616 * @nor: pointer to 'struct spi_nor'. 617 * 618 * Return: 1 if ready, 0 if not ready, -errno on errors. 619 */ 620 static int spi_nor_ready(struct spi_nor *nor) 621 { 622 int ret; 623 624 ret = spi_nor_lock_rdst(nor); 625 if (ret) 626 return 0; 627 628 /* Flashes might override the standard routine. */ 629 if (nor->params->ready) 630 ret = nor->params->ready(nor); 631 else 632 ret = spi_nor_sr_ready(nor); 633 634 spi_nor_unlock_rdst(nor); 635 636 return ret; 637 } 638 639 /** 640 * spi_nor_wait_till_ready_with_timeout() - Service routine to read the 641 * Status Register until ready, or timeout occurs. 642 * @nor: pointer to "struct spi_nor". 643 * @timeout_jiffies: jiffies to wait until timeout. 644 * 645 * Return: 0 on success, -errno otherwise. 646 */ 647 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor, 648 unsigned long timeout_jiffies) 649 { 650 unsigned long deadline; 651 int timeout = 0, ret; 652 653 deadline = jiffies + timeout_jiffies; 654 655 while (!timeout) { 656 if (time_after_eq(jiffies, deadline)) 657 timeout = 1; 658 659 ret = spi_nor_ready(nor); 660 if (ret < 0) 661 return ret; 662 if (ret) 663 return 0; 664 665 cond_resched(); 666 } 667 668 dev_dbg(nor->dev, "flash operation timed out\n"); 669 670 return -ETIMEDOUT; 671 } 672 673 /** 674 * spi_nor_wait_till_ready() - Wait for a predefined amount of time for the 675 * flash to be ready, or timeout occurs. 676 * @nor: pointer to "struct spi_nor". 677 * 678 * Return: 0 on success, -errno otherwise. 679 */ 680 int spi_nor_wait_till_ready(struct spi_nor *nor) 681 { 682 return spi_nor_wait_till_ready_with_timeout(nor, 683 DEFAULT_READY_WAIT_JIFFIES); 684 } 685 686 /** 687 * spi_nor_global_block_unlock() - Unlock Global Block Protection. 688 * @nor: pointer to 'struct spi_nor'. 689 * 690 * Return: 0 on success, -errno otherwise. 691 */ 692 int spi_nor_global_block_unlock(struct spi_nor *nor) 693 { 694 int ret; 695 696 ret = spi_nor_write_enable(nor); 697 if (ret) 698 return ret; 699 700 if (nor->spimem) { 701 struct spi_mem_op op = SPI_NOR_GBULK_OP; 702 703 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); 704 705 ret = spi_mem_exec_op(nor->spimem, &op); 706 } else { 707 ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_GBULK, 708 NULL, 0); 709 } 710 711 if (ret) { 712 dev_dbg(nor->dev, "error %d on Global Block Unlock\n", ret); 713 return ret; 714 } 715 716 return spi_nor_wait_till_ready(nor); 717 } 718 719 /** 720 * spi_nor_read_sr_ll() - Low-level Status Registers read. 721 * @nor: pointer to 'struct spi_nor'. 722 * @opcode: opcode for the status register read operation. 723 * @sr: pointer to buffer for storing the content of the status registers. 724 * @len: number of status registers to read (1 or 2). 725 * 726 * Return: 0 on success, -errno otherwise. 727 */ 728 int spi_nor_read_sr_ll(struct spi_nor *nor, u8 opcode, u8 *sr, 729 unsigned int len) 730 { 731 int ret, i; 732 733 if (len > 2) 734 return -EINVAL; 735 736 for (i = 0; i < len; i++) 737 nor->bouncebuf[i] = 0; 738 739 if (nor->spimem) { 740 struct spi_mem_op op = SPI_NOR_RDSR_OP(opcode, nor->bouncebuf, len); 741 742 if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) { 743 op.addr.nbytes = nor->params->rdsr_addr_nbytes; 744 op.dummy.nbytes = nor->params->rdsr_dummy; 745 /* 746 * We don't want to read only one byte in DTR mode. So, 747 * read 2 and then discard the second byte. 748 */ 749 op.data.nbytes = 2; 750 } 751 752 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); 753 754 ret = spi_mem_exec_op(nor->spimem, &op); 755 } else { 756 ret = spi_nor_controller_ops_read_reg(nor, opcode, nor->bouncebuf, len); 757 } 758 759 memcpy(sr, nor->bouncebuf, len); 760 761 if (ret) 762 dev_dbg(nor->dev, "Error %d reading Status Registers\n", ret); 763 764 return ret; 765 } 766 767 /** 768 * spi_nor_write_sr_ll() - Low-level Status Registers write. 769 * @nor: pointer to 'struct spi_nor'. 770 * @opcode: opcode for the status register write operation. 771 * @sr: pointer to status registers buffer to write. 772 * @len: number of status registers to write. 773 * 774 * Return: 0 on success, -errno otherwise. 775 */ 776 static int spi_nor_write_sr_ll(struct spi_nor *nor, u8 opcode, const u8 *sr, 777 unsigned int len) 778 { 779 int ret, i; 780 781 if (len > 2) 782 return -EINVAL; 783 784 ret = spi_nor_write_enable(nor); 785 if (ret) 786 return ret; 787 788 for (i = 0; i < len; i++) 789 nor->bouncebuf[i] = sr[i]; 790 791 if (nor->spimem) { 792 struct spi_mem_op op = SPI_NOR_WRSR_OP(opcode, 793 nor->bouncebuf, len); 794 795 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); 796 797 ret = spi_mem_exec_op(nor->spimem, &op); 798 } else { 799 ret = spi_nor_controller_ops_write_reg(nor, opcode, 800 nor->bouncebuf, len); 801 } 802 803 if (ret) { 804 dev_dbg(nor->dev, "Error %d writing Status Registers\n", ret); 805 return ret; 806 } 807 808 return spi_nor_wait_till_ready(nor); 809 } 810 811 /** 812 * spi_nor_read_sr1() - Read SR1 only 813 * Useful for: 814 * - The core to offer a generic "read SR1 and SR2" capability 815 * - Manufacturer drivers (since they know the chip SR layout) 816 * - Polling the BUSY bit in the core 817 * 818 * @nor: the spi_nor structure 819 * @sr1: pointer to a valid SR1 buffer 820 * 821 * Return 0 or errno. 822 */ 823 int spi_nor_read_sr1(struct spi_nor *nor, u8 *sr1) 824 { 825 return spi_nor_read_sr_ll(nor, nor->params->opcodes.read_sr1, sr1, 1); 826 } 827 828 /** 829 * spi_nor_read_sr2() - Read SR2 only 830 * Useful for: 831 * - The core to offer a generic "read SR1 and SR2" capability 832 * - Manufacturer drivers (since they know the chip SR layout) 833 * - SR2 based OTP configuration 834 * 835 * @nor: the spi_nor structure 836 * @sr2: pointer to a valid SR2 buffer 837 * 838 * Return 0 or errno. 839 */ 840 int spi_nor_read_sr2(struct spi_nor *nor, u8 *sr2) 841 { 842 struct spi_nor_flash_parameter *params = nor->params; 843 844 if (!params->opcodes.read_sr2) 845 return -EINVAL; 846 847 return spi_nor_read_sr_ll(nor, params->opcodes.read_sr2, sr2, 1); 848 } 849 850 /** 851 * spi_nor_read_sr1_and_sr2() - Read SR1 then SR2 852 * General purpose helper, always safe to call. Will expectedly ignore 853 * SR2 on certain chips. 854 * 855 * @nor: the spi_nor structure 856 * @sr: pointer to a valid 2-byte array 857 * 858 * Return 0 or errno. 859 */ 860 int spi_nor_read_sr1_and_sr2(struct spi_nor *nor, u8 *sr) 861 { 862 int ret; 863 864 ret = spi_nor_read_sr1(nor, &sr[0]); 865 if (ret) 866 return ret; 867 868 if (nor->params->opcodes.read_sr2) 869 ret = spi_nor_read_sr2(nor, &sr[1]); 870 else 871 sr[1] = 0; 872 873 return ret; 874 } 875 876 /** 877 * spi_nor_write_sr1() - Write SR1 only 878 * Useful for: 879 * - Manufacturer drivers (since they know the chip SR layout) 880 * 881 * @nor: the spi_nor structure 882 * @sr1: pointer to a valid SR1 buffer 883 * 884 * Return 0 or errno. 885 */ 886 int spi_nor_write_sr1(struct spi_nor *nor, const u8 *sr1) 887 { 888 if (WARN_ONCE(!nor->params->opcodes.write_sr1, 889 "Restricted helper use, write SR1 not supported")) 890 return -EIO; 891 892 return spi_nor_write_sr_ll(nor, nor->params->opcodes.write_sr1, sr1, 1); 893 } 894 895 /** 896 * spi_nor_write_sr2() - Write SR2 only 897 * Useful for: 898 * - Manufacturer drivers (since they know the chip SR layout) 899 * - SR2 based OTP configuration 900 * 901 * @nor: the spi_nor structure 902 * @sr2: pointer to a valid SR2 buffer 903 * 904 * Return 0 or errno. 905 */ 906 int spi_nor_write_sr2(struct spi_nor *nor, const u8 *sr2) 907 { 908 if (WARN_ONCE(!nor->params->opcodes.write_sr2, 909 "Restricted helper use, write SR2 not supported")) 910 return -EIO; 911 912 return spi_nor_write_sr_ll(nor, nor->params->opcodes.write_sr2, sr2, 1); 913 } 914 915 /** 916 * spi_nor_write_sr1_and_sr2() - Write SR1 then SR2 917 * General purpose helper, always safe to call. Will expectedly ignore 918 * SR2 on certain chips. 919 * 920 * @nor: the spi_nor structure 921 * @sr: pointer to a valid 2-byte array 922 * 923 * Return 0 or errno. 924 */ 925 static int spi_nor_write_sr1_and_sr2(struct spi_nor *nor, const u8 *sr) 926 { 927 struct spi_nor_flash_parameter *params = nor->params; 928 int ret; 929 930 if (params->opcodes.write_sr1_and_sr2) 931 return spi_nor_write_sr_ll(nor, 932 params->opcodes.write_sr1_and_sr2, 933 sr, 2); 934 935 ret = spi_nor_write_sr1(nor, &sr[0]); 936 if (ret) 937 return ret; 938 939 if (params->opcodes.write_sr2) 940 return spi_nor_write_sr2(nor, &sr[1]); 941 942 return 0; 943 } 944 945 /** 946 * spi_nor_write_sr1_and_sr2_and_check() - Write SR1 then SR2, then read 947 * them back and verifies 948 * General purpose helper, always safe to call. Will expectedly ignore 949 * SR2 on certain chips. 950 * 951 * @nor: the spi_nor structure 952 * @sr: pointer to a valid 2-byte array 953 * 954 * Return 0 or errno. 955 */ 956 int spi_nor_write_sr1_and_sr2_and_check(struct spi_nor *nor, const u8 *sr) 957 { 958 u8 tmp[2]; 959 int ret; 960 961 ret = spi_nor_write_sr1_and_sr2(nor, sr); 962 if (ret) 963 return ret; 964 965 ret = spi_nor_read_sr1_and_sr2(nor, tmp); 966 if (ret) 967 return ret; 968 969 if (sr[0] != tmp[0]) 970 return -EIO; 971 972 if (nor->params->opcodes.read_sr2 && sr[1] != tmp[1]) 973 return -EIO; 974 975 return 0; 976 } 977 978 /** 979 * spi_nor_generic_quad_enable() - Read the status registers, 980 * apply the QE bit mask, 981 * write the status registers, 982 * read them back and check the content. 983 * 984 * @nor: pointer to 'struct spi_nor' 985 * 986 * Return: 0 on success, -errno otherwise. 987 */ 988 static int spi_nor_generic_quad_enable(struct spi_nor *nor) 989 { 990 u8 *qe_mask = nor->params->qe_mask; 991 u8 sr[2] = {}; 992 int ret; 993 994 if (!qe_mask[0] && !qe_mask[1]) 995 return 0; 996 997 ret = spi_nor_read_sr1_and_sr2(nor, sr); 998 if (ret) 999 return ret; 1000 1001 if (sr[0] & qe_mask[0] || sr[1] & qe_mask[1]) 1002 return 0; 1003 1004 sr[0] |= qe_mask[0]; 1005 sr[1] |= qe_mask[1]; 1006 1007 return spi_nor_write_sr1_and_sr2_and_check(nor, sr); 1008 } 1009 1010 /** 1011 * spi_nor_erase_die() - Erase the entire die. 1012 * @nor: pointer to 'struct spi_nor'. 1013 * @addr: address of the die. 1014 * @die_size: size of the die. 1015 * 1016 * Return: 0 on success, -errno otherwise. 1017 */ 1018 static int spi_nor_erase_die(struct spi_nor *nor, loff_t addr, size_t die_size) 1019 { 1020 bool multi_die = nor->mtd.size != die_size; 1021 int ret; 1022 1023 dev_dbg(nor->dev, " %lldKiB\n", (long long)(die_size >> 10)); 1024 1025 if (nor->spimem) { 1026 struct spi_mem_op op = 1027 SPI_NOR_DIE_ERASE_OP(nor->params->opcodes.die_erase, 1028 nor->addr_nbytes, addr, multi_die); 1029 1030 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); 1031 1032 ret = spi_mem_exec_op(nor->spimem, &op); 1033 } else { 1034 if (multi_die) 1035 return -EOPNOTSUPP; 1036 1037 ret = spi_nor_controller_ops_write_reg(nor, 1038 SPINOR_OP_CHIP_ERASE, 1039 NULL, 0); 1040 } 1041 1042 if (ret) 1043 dev_dbg(nor->dev, "error %d erasing chip\n", ret); 1044 1045 return ret; 1046 } 1047 1048 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size) 1049 { 1050 size_t i; 1051 1052 for (i = 0; i < size; i++) 1053 if (table[i][0] == opcode) 1054 return table[i][1]; 1055 1056 /* No conversion found, keep input op code. */ 1057 return opcode; 1058 } 1059 1060 u8 spi_nor_convert_3to4_read(u8 opcode) 1061 { 1062 static const u8 spi_nor_3to4_read[][2] = { 1063 { SPINOR_OP_READ, SPINOR_OP_READ_4B }, 1064 { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B }, 1065 { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B }, 1066 { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B }, 1067 { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B }, 1068 { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B }, 1069 { SPINOR_OP_READ_1_1_8, SPINOR_OP_READ_1_1_8_4B }, 1070 { SPINOR_OP_READ_1_8_8, SPINOR_OP_READ_1_8_8_4B }, 1071 1072 { SPINOR_OP_READ_1_1_1_DTR, SPINOR_OP_READ_1_1_1_DTR_4B }, 1073 { SPINOR_OP_READ_1_2_2_DTR, SPINOR_OP_READ_1_2_2_DTR_4B }, 1074 { SPINOR_OP_READ_1_4_4_DTR, SPINOR_OP_READ_1_4_4_DTR_4B }, 1075 }; 1076 1077 return spi_nor_convert_opcode(opcode, spi_nor_3to4_read, 1078 ARRAY_SIZE(spi_nor_3to4_read)); 1079 } 1080 1081 static u8 spi_nor_convert_3to4_program(u8 opcode) 1082 { 1083 static const u8 spi_nor_3to4_program[][2] = { 1084 { SPINOR_OP_PP, SPINOR_OP_PP_4B }, 1085 { SPINOR_OP_PP_1_1_4, SPINOR_OP_PP_1_1_4_4B }, 1086 { SPINOR_OP_PP_1_4_4, SPINOR_OP_PP_1_4_4_4B }, 1087 { SPINOR_OP_PP_1_1_8, SPINOR_OP_PP_1_1_8_4B }, 1088 { SPINOR_OP_PP_1_8_8, SPINOR_OP_PP_1_8_8_4B }, 1089 }; 1090 1091 return spi_nor_convert_opcode(opcode, spi_nor_3to4_program, 1092 ARRAY_SIZE(spi_nor_3to4_program)); 1093 } 1094 1095 static u8 spi_nor_convert_3to4_erase(u8 opcode) 1096 { 1097 static const u8 spi_nor_3to4_erase[][2] = { 1098 { SPINOR_OP_BE_4K, SPINOR_OP_BE_4K_4B }, 1099 { SPINOR_OP_BE_32K, SPINOR_OP_BE_32K_4B }, 1100 { SPINOR_OP_SE, SPINOR_OP_SE_4B }, 1101 }; 1102 1103 return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase, 1104 ARRAY_SIZE(spi_nor_3to4_erase)); 1105 } 1106 1107 static bool spi_nor_has_uniform_erase(const struct spi_nor *nor) 1108 { 1109 return !!nor->params->erase_map.uniform_region.erase_mask; 1110 } 1111 1112 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor) 1113 { 1114 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode); 1115 nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode); 1116 nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode); 1117 1118 if (!spi_nor_has_uniform_erase(nor)) { 1119 struct spi_nor_erase_map *map = &nor->params->erase_map; 1120 struct spi_nor_erase_type *erase; 1121 int i; 1122 1123 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) { 1124 erase = &map->erase_type[i]; 1125 erase->opcode = 1126 spi_nor_convert_3to4_erase(erase->opcode); 1127 } 1128 } 1129 } 1130 1131 static int spi_nor_prep(struct spi_nor *nor) 1132 { 1133 int ret = 0; 1134 1135 if (nor->controller_ops && nor->controller_ops->prepare) 1136 ret = nor->controller_ops->prepare(nor); 1137 1138 return ret; 1139 } 1140 1141 static void spi_nor_unprep(struct spi_nor *nor) 1142 { 1143 if (nor->controller_ops && nor->controller_ops->unprepare) 1144 nor->controller_ops->unprepare(nor); 1145 } 1146 1147 static void spi_nor_offset_to_banks(u64 bank_size, loff_t start, size_t len, 1148 u8 *first, u8 *last) 1149 { 1150 /* This is currently safe, the number of banks being very small */ 1151 *first = DIV_ROUND_DOWN_ULL(start, bank_size); 1152 *last = DIV_ROUND_DOWN_ULL(start + len - 1, bank_size); 1153 } 1154 1155 /* Generic helpers for internal locking and serialization */ 1156 static bool spi_nor_rww_start_io(struct spi_nor *nor) 1157 { 1158 struct spi_nor_rww *rww = &nor->rww; 1159 1160 guard(mutex)(&nor->lock); 1161 1162 if (rww->ongoing_io) 1163 return false; 1164 1165 rww->ongoing_io = true; 1166 1167 return true; 1168 } 1169 1170 static void spi_nor_rww_end_io(struct spi_nor *nor) 1171 { 1172 guard(mutex)(&nor->lock); 1173 nor->rww.ongoing_io = false; 1174 } 1175 1176 static int spi_nor_lock_device(struct spi_nor *nor) 1177 { 1178 if (!spi_nor_use_parallel_locking(nor)) 1179 return 0; 1180 1181 return wait_event_killable(nor->rww.wait, spi_nor_rww_start_io(nor)); 1182 } 1183 1184 static void spi_nor_unlock_device(struct spi_nor *nor) 1185 { 1186 if (spi_nor_use_parallel_locking(nor)) { 1187 spi_nor_rww_end_io(nor); 1188 wake_up(&nor->rww.wait); 1189 } 1190 } 1191 1192 /* Generic helpers for internal locking and serialization */ 1193 static bool spi_nor_rww_start_exclusive(struct spi_nor *nor) 1194 { 1195 struct spi_nor_rww *rww = &nor->rww; 1196 1197 guard(mutex)(&nor->lock); 1198 1199 if (rww->ongoing_io || rww->ongoing_rd || rww->ongoing_pe) 1200 return false; 1201 1202 rww->ongoing_io = true; 1203 rww->ongoing_rd = true; 1204 rww->ongoing_pe = true; 1205 1206 return true; 1207 } 1208 1209 static void spi_nor_rww_end_exclusive(struct spi_nor *nor) 1210 { 1211 struct spi_nor_rww *rww = &nor->rww; 1212 1213 guard(mutex)(&nor->lock); 1214 rww->ongoing_io = false; 1215 rww->ongoing_rd = false; 1216 rww->ongoing_pe = false; 1217 } 1218 1219 int spi_nor_prep_and_lock(struct spi_nor *nor) 1220 { 1221 int ret; 1222 1223 ret = spi_nor_prep(nor); 1224 if (ret) 1225 return ret; 1226 1227 if (!spi_nor_use_parallel_locking(nor)) 1228 mutex_lock(&nor->lock); 1229 else 1230 ret = wait_event_killable(nor->rww.wait, 1231 spi_nor_rww_start_exclusive(nor)); 1232 1233 if (ret) 1234 spi_nor_unprep(nor); 1235 1236 return ret; 1237 } 1238 1239 void spi_nor_unlock_and_unprep(struct spi_nor *nor) 1240 { 1241 if (!spi_nor_use_parallel_locking(nor)) { 1242 mutex_unlock(&nor->lock); 1243 } else { 1244 spi_nor_rww_end_exclusive(nor); 1245 wake_up(&nor->rww.wait); 1246 } 1247 1248 spi_nor_unprep(nor); 1249 } 1250 1251 /* Internal locking helpers for program and erase operations */ 1252 static bool spi_nor_rww_start_pe(struct spi_nor *nor, loff_t start, size_t len) 1253 { 1254 struct spi_nor_rww *rww = &nor->rww; 1255 unsigned int used_banks = 0; 1256 u8 first, last; 1257 int bank; 1258 1259 guard(mutex)(&nor->lock); 1260 1261 if (rww->ongoing_io || rww->ongoing_rd || rww->ongoing_pe) 1262 return false; 1263 1264 spi_nor_offset_to_banks(nor->params->bank_size, start, len, &first, &last); 1265 for (bank = first; bank <= last; bank++) { 1266 if (rww->used_banks & BIT(bank)) 1267 return false; 1268 1269 used_banks |= BIT(bank); 1270 } 1271 1272 rww->used_banks |= used_banks; 1273 rww->ongoing_pe = true; 1274 1275 return true; 1276 } 1277 1278 static void spi_nor_rww_end_pe(struct spi_nor *nor, loff_t start, size_t len) 1279 { 1280 struct spi_nor_rww *rww = &nor->rww; 1281 u8 first, last; 1282 int bank; 1283 1284 guard(mutex)(&nor->lock); 1285 1286 spi_nor_offset_to_banks(nor->params->bank_size, start, len, &first, &last); 1287 for (bank = first; bank <= last; bank++) 1288 rww->used_banks &= ~BIT(bank); 1289 1290 rww->ongoing_pe = false; 1291 } 1292 1293 static int spi_nor_prep_and_lock_pe(struct spi_nor *nor, loff_t start, size_t len) 1294 { 1295 int ret; 1296 1297 ret = spi_nor_prep(nor); 1298 if (ret) 1299 return ret; 1300 1301 if (!spi_nor_use_parallel_locking(nor)) 1302 mutex_lock(&nor->lock); 1303 else 1304 ret = wait_event_killable(nor->rww.wait, 1305 spi_nor_rww_start_pe(nor, start, len)); 1306 1307 if (ret) 1308 spi_nor_unprep(nor); 1309 1310 return ret; 1311 } 1312 1313 static void spi_nor_unlock_and_unprep_pe(struct spi_nor *nor, loff_t start, size_t len) 1314 { 1315 if (!spi_nor_use_parallel_locking(nor)) { 1316 mutex_unlock(&nor->lock); 1317 } else { 1318 spi_nor_rww_end_pe(nor, start, len); 1319 wake_up(&nor->rww.wait); 1320 } 1321 1322 spi_nor_unprep(nor); 1323 } 1324 1325 /* Internal locking helpers for read operations */ 1326 static bool spi_nor_rww_start_rd(struct spi_nor *nor, loff_t start, size_t len) 1327 { 1328 struct spi_nor_rww *rww = &nor->rww; 1329 unsigned int used_banks = 0; 1330 u8 first, last; 1331 int bank; 1332 1333 guard(mutex)(&nor->lock); 1334 1335 if (rww->ongoing_io || rww->ongoing_rd) 1336 return false; 1337 1338 spi_nor_offset_to_banks(nor->params->bank_size, start, len, &first, &last); 1339 for (bank = first; bank <= last; bank++) { 1340 if (rww->used_banks & BIT(bank)) 1341 return false; 1342 1343 used_banks |= BIT(bank); 1344 } 1345 1346 rww->used_banks |= used_banks; 1347 rww->ongoing_io = true; 1348 rww->ongoing_rd = true; 1349 1350 return true; 1351 } 1352 1353 static void spi_nor_rww_end_rd(struct spi_nor *nor, loff_t start, size_t len) 1354 { 1355 struct spi_nor_rww *rww = &nor->rww; 1356 u8 first, last; 1357 int bank; 1358 1359 guard(mutex)(&nor->lock); 1360 1361 spi_nor_offset_to_banks(nor->params->bank_size, start, len, &first, &last); 1362 for (bank = first; bank <= last; bank++) 1363 nor->rww.used_banks &= ~BIT(bank); 1364 1365 rww->ongoing_io = false; 1366 rww->ongoing_rd = false; 1367 } 1368 1369 static int spi_nor_prep_and_lock_rd(struct spi_nor *nor, loff_t start, size_t len) 1370 { 1371 int ret; 1372 1373 ret = spi_nor_prep(nor); 1374 if (ret) 1375 return ret; 1376 1377 if (!spi_nor_use_parallel_locking(nor)) 1378 mutex_lock(&nor->lock); 1379 else 1380 ret = wait_event_killable(nor->rww.wait, 1381 spi_nor_rww_start_rd(nor, start, len)); 1382 1383 if (ret) 1384 spi_nor_unprep(nor); 1385 1386 return ret; 1387 } 1388 1389 static void spi_nor_unlock_and_unprep_rd(struct spi_nor *nor, loff_t start, size_t len) 1390 { 1391 if (!spi_nor_use_parallel_locking(nor)) { 1392 mutex_unlock(&nor->lock); 1393 } else { 1394 spi_nor_rww_end_rd(nor, start, len); 1395 wake_up(&nor->rww.wait); 1396 } 1397 1398 spi_nor_unprep(nor); 1399 } 1400 1401 /* 1402 * Initiate the erasure of a single sector 1403 */ 1404 int spi_nor_erase_sector(struct spi_nor *nor, u32 addr) 1405 { 1406 int i; 1407 1408 if (nor->spimem) { 1409 struct spi_mem_op op = 1410 SPI_NOR_SECTOR_ERASE_OP(nor->erase_opcode, 1411 nor->addr_nbytes, addr); 1412 1413 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); 1414 1415 return spi_mem_exec_op(nor->spimem, &op); 1416 } else if (nor->controller_ops->erase) { 1417 return spi_nor_controller_ops_erase(nor, addr); 1418 } 1419 1420 /* 1421 * Default implementation, if driver doesn't have a specialized HW 1422 * control 1423 */ 1424 for (i = nor->addr_nbytes - 1; i >= 0; i--) { 1425 nor->bouncebuf[i] = addr & 0xff; 1426 addr >>= 8; 1427 } 1428 1429 return spi_nor_controller_ops_write_reg(nor, nor->erase_opcode, 1430 nor->bouncebuf, nor->addr_nbytes); 1431 } 1432 1433 /** 1434 * spi_nor_div_by_erase_size() - calculate remainder and update new dividend 1435 * @erase: pointer to a structure that describes a SPI NOR erase type 1436 * @dividend: dividend value 1437 * @remainder: pointer to u32 remainder (will be updated) 1438 * 1439 * Return: the result of the division 1440 */ 1441 static u64 spi_nor_div_by_erase_size(const struct spi_nor_erase_type *erase, 1442 u64 dividend, u32 *remainder) 1443 { 1444 /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */ 1445 *remainder = (u32)dividend & erase->size_mask; 1446 return dividend >> erase->size_shift; 1447 } 1448 1449 /** 1450 * spi_nor_find_best_erase_type() - find the best erase type for the given 1451 * offset in the serial flash memory and the 1452 * number of bytes to erase. The region in 1453 * which the address fits is expected to be 1454 * provided. 1455 * @map: the erase map of the SPI NOR 1456 * @region: pointer to a structure that describes a SPI NOR erase region 1457 * @addr: offset in the serial flash memory 1458 * @len: number of bytes to erase 1459 * 1460 * Return: a pointer to the best fitted erase type, NULL otherwise. 1461 */ 1462 static const struct spi_nor_erase_type * 1463 spi_nor_find_best_erase_type(const struct spi_nor_erase_map *map, 1464 const struct spi_nor_erase_region *region, 1465 u64 addr, u32 len) 1466 { 1467 const struct spi_nor_erase_type *erase; 1468 u32 rem; 1469 int i; 1470 1471 /* 1472 * Erase types are ordered by size, with the smallest erase type at 1473 * index 0. 1474 */ 1475 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) { 1476 /* Does the erase region support the tested erase type? */ 1477 if (!(region->erase_mask & BIT(i))) 1478 continue; 1479 1480 erase = &map->erase_type[i]; 1481 if (!erase->size) 1482 continue; 1483 1484 /* Alignment is not mandatory for overlaid regions */ 1485 if (region->overlaid && region->size <= len) 1486 return erase; 1487 1488 /* Don't erase more than what the user has asked for. */ 1489 if (erase->size > len) 1490 continue; 1491 1492 spi_nor_div_by_erase_size(erase, addr, &rem); 1493 if (!rem) 1494 return erase; 1495 } 1496 1497 return NULL; 1498 } 1499 1500 /** 1501 * spi_nor_init_erase_cmd() - initialize an erase command 1502 * @region: pointer to a structure that describes a SPI NOR erase region 1503 * @erase: pointer to a structure that describes a SPI NOR erase type 1504 * 1505 * Return: the pointer to the allocated erase command, ERR_PTR(-errno) 1506 * otherwise. 1507 */ 1508 static struct spi_nor_erase_command * 1509 spi_nor_init_erase_cmd(const struct spi_nor_erase_region *region, 1510 const struct spi_nor_erase_type *erase) 1511 { 1512 struct spi_nor_erase_command *cmd; 1513 1514 cmd = kmalloc_obj(*cmd); 1515 if (!cmd) 1516 return ERR_PTR(-ENOMEM); 1517 1518 INIT_LIST_HEAD(&cmd->list); 1519 cmd->opcode = erase->opcode; 1520 cmd->count = 1; 1521 1522 if (region->overlaid) 1523 cmd->size = region->size; 1524 else 1525 cmd->size = erase->size; 1526 1527 return cmd; 1528 } 1529 1530 /** 1531 * spi_nor_destroy_erase_cmd_list() - destroy erase command list 1532 * @erase_list: list of erase commands 1533 */ 1534 static void spi_nor_destroy_erase_cmd_list(struct list_head *erase_list) 1535 { 1536 struct spi_nor_erase_command *cmd, *next; 1537 1538 list_for_each_entry_safe(cmd, next, erase_list, list) { 1539 list_del(&cmd->list); 1540 kfree(cmd); 1541 } 1542 } 1543 1544 /** 1545 * spi_nor_init_erase_cmd_list() - initialize erase command list 1546 * @nor: pointer to a 'struct spi_nor' 1547 * @erase_list: list of erase commands to be executed once we validate that the 1548 * erase can be performed 1549 * @addr: offset in the serial flash memory 1550 * @len: number of bytes to erase 1551 * 1552 * Builds the list of best fitted erase commands and verifies if the erase can 1553 * be performed. 1554 * 1555 * Return: 0 on success, -errno otherwise. 1556 */ 1557 static int spi_nor_init_erase_cmd_list(struct spi_nor *nor, 1558 struct list_head *erase_list, 1559 u64 addr, u32 len) 1560 { 1561 const struct spi_nor_erase_map *map = &nor->params->erase_map; 1562 const struct spi_nor_erase_type *erase, *prev_erase = NULL; 1563 struct spi_nor_erase_region *region; 1564 struct spi_nor_erase_command *cmd = NULL; 1565 u64 region_end; 1566 unsigned int i; 1567 int ret = -EINVAL; 1568 1569 for (i = 0; i < map->n_regions && len; i++) { 1570 region = &map->regions[i]; 1571 region_end = region->offset + region->size; 1572 1573 while (len && addr >= region->offset && addr < region_end) { 1574 erase = spi_nor_find_best_erase_type(map, region, addr, 1575 len); 1576 if (!erase) 1577 goto destroy_erase_cmd_list; 1578 1579 if (prev_erase != erase || erase->size != cmd->size || 1580 region->overlaid) { 1581 cmd = spi_nor_init_erase_cmd(region, erase); 1582 if (IS_ERR(cmd)) { 1583 ret = PTR_ERR(cmd); 1584 goto destroy_erase_cmd_list; 1585 } 1586 1587 list_add_tail(&cmd->list, erase_list); 1588 } else { 1589 cmd->count++; 1590 } 1591 1592 len -= cmd->size; 1593 addr += cmd->size; 1594 prev_erase = erase; 1595 } 1596 } 1597 1598 return 0; 1599 1600 destroy_erase_cmd_list: 1601 spi_nor_destroy_erase_cmd_list(erase_list); 1602 return ret; 1603 } 1604 1605 /** 1606 * spi_nor_erase_multi_sectors() - perform a non-uniform erase 1607 * @nor: pointer to a 'struct spi_nor' 1608 * @addr: offset in the serial flash memory 1609 * @len: number of bytes to erase 1610 * 1611 * Build a list of best fitted erase commands and execute it once we validate 1612 * that the erase can be performed. 1613 * 1614 * Return: 0 on success, -errno otherwise. 1615 */ 1616 static int spi_nor_erase_multi_sectors(struct spi_nor *nor, u64 addr, u32 len) 1617 { 1618 LIST_HEAD(erase_list); 1619 struct spi_nor_erase_command *cmd, *next; 1620 int ret; 1621 1622 ret = spi_nor_init_erase_cmd_list(nor, &erase_list, addr, len); 1623 if (ret) 1624 return ret; 1625 1626 list_for_each_entry_safe(cmd, next, &erase_list, list) { 1627 nor->erase_opcode = cmd->opcode; 1628 while (cmd->count) { 1629 dev_vdbg(nor->dev, "erase_cmd->size = 0x%08x, erase_cmd->opcode = 0x%02x, erase_cmd->count = %u\n", 1630 cmd->size, cmd->opcode, cmd->count); 1631 1632 ret = spi_nor_lock_device(nor); 1633 if (ret) 1634 goto destroy_erase_cmd_list; 1635 1636 ret = spi_nor_write_enable(nor); 1637 if (ret) { 1638 spi_nor_unlock_device(nor); 1639 goto destroy_erase_cmd_list; 1640 } 1641 1642 ret = spi_nor_erase_sector(nor, addr); 1643 spi_nor_unlock_device(nor); 1644 if (ret) 1645 goto destroy_erase_cmd_list; 1646 1647 ret = spi_nor_wait_till_ready(nor); 1648 if (ret) 1649 goto destroy_erase_cmd_list; 1650 1651 addr += cmd->size; 1652 cmd->count--; 1653 } 1654 list_del(&cmd->list); 1655 kfree(cmd); 1656 } 1657 1658 return 0; 1659 1660 destroy_erase_cmd_list: 1661 spi_nor_destroy_erase_cmd_list(&erase_list); 1662 return ret; 1663 } 1664 1665 static int spi_nor_erase_dice(struct spi_nor *nor, loff_t addr, 1666 size_t len, size_t die_size) 1667 { 1668 unsigned long timeout; 1669 int ret; 1670 1671 /* 1672 * Scale the timeout linearly with the size of the flash, with 1673 * a minimum calibrated to an old 2MB flash. We could try to 1674 * pull these from CFI/SFDP, but these values should be good 1675 * enough for now. 1676 */ 1677 timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES, 1678 CHIP_ERASE_2MB_READY_WAIT_JIFFIES * 1679 (unsigned long)(nor->mtd.size / SZ_2M)); 1680 1681 do { 1682 ret = spi_nor_lock_device(nor); 1683 if (ret) 1684 return ret; 1685 1686 ret = spi_nor_write_enable(nor); 1687 if (ret) { 1688 spi_nor_unlock_device(nor); 1689 return ret; 1690 } 1691 1692 ret = spi_nor_erase_die(nor, addr, die_size); 1693 1694 spi_nor_unlock_device(nor); 1695 if (ret) 1696 return ret; 1697 1698 ret = spi_nor_wait_till_ready_with_timeout(nor, timeout); 1699 if (ret) 1700 return ret; 1701 1702 addr += die_size; 1703 len -= die_size; 1704 1705 } while (len); 1706 1707 return 0; 1708 } 1709 1710 /* 1711 * Erase an address range on the nor chip. The address range may extend 1712 * one or more erase sectors. Return an error if there is a problem erasing. 1713 */ 1714 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr) 1715 { 1716 struct spi_nor *nor = mtd_to_spi_nor(mtd); 1717 u8 n_dice = nor->params->n_dice; 1718 bool multi_die_erase = false; 1719 u32 addr, len, rem; 1720 size_t die_size; 1721 int ret; 1722 1723 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr, 1724 (long long)instr->len); 1725 1726 if (spi_nor_has_uniform_erase(nor)) { 1727 div_u64_rem(instr->len, mtd->erasesize, &rem); 1728 if (rem) 1729 return -EINVAL; 1730 } 1731 1732 addr = instr->addr; 1733 len = instr->len; 1734 1735 if (n_dice) { 1736 die_size = div_u64(mtd->size, n_dice); 1737 if (!(len & (die_size - 1)) && !(addr & (die_size - 1))) 1738 multi_die_erase = true; 1739 } else { 1740 die_size = mtd->size; 1741 } 1742 1743 ret = spi_nor_prep_and_lock_pe(nor, instr->addr, instr->len); 1744 if (ret) 1745 return ret; 1746 1747 /* chip (die) erase? */ 1748 if (len == mtd->size || multi_die_erase) { 1749 ret = spi_nor_erase_dice(nor, addr, len, die_size); 1750 if (ret) 1751 goto erase_err; 1752 1753 /* REVISIT in some cases we could speed up erasing large regions 1754 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up 1755 * to use "small sector erase", but that's not always optimal. 1756 */ 1757 1758 /* "sector"-at-a-time erase */ 1759 } else if (spi_nor_has_uniform_erase(nor)) { 1760 while (len) { 1761 ret = spi_nor_lock_device(nor); 1762 if (ret) 1763 goto erase_err; 1764 1765 ret = spi_nor_write_enable(nor); 1766 if (ret) { 1767 spi_nor_unlock_device(nor); 1768 goto erase_err; 1769 } 1770 1771 ret = spi_nor_erase_sector(nor, addr); 1772 spi_nor_unlock_device(nor); 1773 if (ret) 1774 goto erase_err; 1775 1776 ret = spi_nor_wait_till_ready(nor); 1777 if (ret) 1778 goto erase_err; 1779 1780 addr += mtd->erasesize; 1781 len -= mtd->erasesize; 1782 } 1783 1784 /* erase multiple sectors */ 1785 } else { 1786 ret = spi_nor_erase_multi_sectors(nor, addr, len); 1787 if (ret) 1788 goto erase_err; 1789 } 1790 1791 ret = spi_nor_write_disable(nor); 1792 1793 erase_err: 1794 spi_nor_unlock_and_unprep_pe(nor, instr->addr, instr->len); 1795 1796 return ret; 1797 } 1798 1799 static const struct spi_nor_manufacturer *manufacturers[] = { 1800 &spi_nor_atmel, 1801 &spi_nor_eon, 1802 &spi_nor_esmt, 1803 &spi_nor_everspin, 1804 &spi_nor_gigadevice, 1805 &spi_nor_intel, 1806 &spi_nor_issi, 1807 &spi_nor_macronix, 1808 &spi_nor_micron, 1809 &spi_nor_st, 1810 &spi_nor_spansion, 1811 &spi_nor_sst, 1812 &spi_nor_winbond, 1813 &spi_nor_xmc, 1814 }; 1815 1816 static const struct flash_info spi_nor_generic_flash = { 1817 .name = "spi-nor-generic", 1818 }; 1819 1820 static const struct flash_info *spi_nor_match_id(struct spi_nor *nor, 1821 const u8 *id) 1822 { 1823 const struct flash_info *part; 1824 unsigned int i, j; 1825 1826 for (i = 0; i < ARRAY_SIZE(manufacturers); i++) { 1827 for (j = 0; j < manufacturers[i]->nparts; j++) { 1828 part = &manufacturers[i]->parts[j]; 1829 if (part->id && 1830 !memcmp(part->id->bytes, id, part->id->len)) { 1831 nor->manufacturer = manufacturers[i]; 1832 return part; 1833 } 1834 } 1835 } 1836 1837 return NULL; 1838 } 1839 1840 static const struct flash_info *spi_nor_detect(struct spi_nor *nor) 1841 { 1842 const struct flash_info *info; 1843 u8 *id = nor->bouncebuf; 1844 int ret; 1845 1846 ret = spi_nor_read_id(nor, 0, 0, id, nor->reg_proto); 1847 if (ret) { 1848 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", ret); 1849 return ERR_PTR(ret); 1850 } 1851 1852 /* Cache the complete flash ID. */ 1853 nor->id = devm_kmemdup(nor->dev, id, SPI_NOR_MAX_ID_LEN, GFP_KERNEL); 1854 if (!nor->id) 1855 return ERR_PTR(-ENOMEM); 1856 1857 info = spi_nor_match_id(nor, id); 1858 1859 /* Fallback to a generic flash described only by its SFDP data. */ 1860 if (!info) { 1861 ret = spi_nor_check_sfdp_signature(nor); 1862 if (!ret) 1863 info = &spi_nor_generic_flash; 1864 } 1865 1866 if (!info) { 1867 dev_err(nor->dev, "unrecognized JEDEC id bytes: %*ph\n", 1868 SPI_NOR_MAX_ID_LEN, id); 1869 return ERR_PTR(-ENODEV); 1870 } 1871 return info; 1872 } 1873 1874 /* 1875 * On Octal DTR capable flashes, reads cannot start or end at an odd 1876 * address in Octal DTR mode. Extra bytes need to be read at the start 1877 * or end to make sure both the start address and length remain even. 1878 */ 1879 static int spi_nor_octal_dtr_read(struct spi_nor *nor, loff_t from, size_t len, 1880 u_char *buf) 1881 { 1882 u_char *tmp_buf; 1883 size_t tmp_len; 1884 loff_t start, end; 1885 int ret, bytes_read; 1886 1887 if (IS_ALIGNED(from, 2) && IS_ALIGNED(len, 2)) 1888 return spi_nor_read_data(nor, from, len, buf); 1889 else if (IS_ALIGNED(from, 2) && len > PAGE_SIZE) 1890 return spi_nor_read_data(nor, from, round_down(len, PAGE_SIZE), 1891 buf); 1892 1893 tmp_buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 1894 if (!tmp_buf) 1895 return -ENOMEM; 1896 1897 start = round_down(from, 2); 1898 end = round_up(from + len, 2); 1899 1900 /* 1901 * Avoid allocating too much memory. The requested read length might be 1902 * quite large. Allocating a buffer just as large (slightly bigger, in 1903 * fact) would put unnecessary memory pressure on the system. 1904 * 1905 * For example if the read is from 3 to 1M, then this will read from 2 1906 * to 4098. The reads from 4098 to 1M will then not need a temporary 1907 * buffer so they can proceed as normal. 1908 */ 1909 tmp_len = min_t(size_t, end - start, PAGE_SIZE); 1910 1911 ret = spi_nor_read_data(nor, start, tmp_len, tmp_buf); 1912 if (ret == 0) { 1913 ret = -EIO; 1914 goto out; 1915 } 1916 if (ret < 0) 1917 goto out; 1918 1919 /* 1920 * More bytes are read than actually requested, but that number can't be 1921 * reported to the calling function or it will confuse its calculations. 1922 * Calculate how many of the _requested_ bytes were read. 1923 */ 1924 bytes_read = ret; 1925 1926 if (from != start) 1927 ret -= from - start; 1928 1929 /* 1930 * Only account for extra bytes at the end if they were actually read. 1931 * For example, if the total length was truncated because of temporary 1932 * buffer size limit then the adjustment for the extra bytes at the end 1933 * is not needed. 1934 */ 1935 if (start + bytes_read == end) 1936 ret -= end - (from + len); 1937 1938 memcpy(buf, tmp_buf + (from - start), ret); 1939 out: 1940 kfree(tmp_buf); 1941 return ret; 1942 } 1943 1944 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len, 1945 size_t *retlen, u_char *buf) 1946 { 1947 struct spi_nor *nor = mtd_to_spi_nor(mtd); 1948 loff_t from_lock = from; 1949 size_t len_lock = len; 1950 ssize_t ret; 1951 1952 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len); 1953 1954 ret = spi_nor_prep_and_lock_rd(nor, from_lock, len_lock); 1955 if (ret) 1956 return ret; 1957 1958 while (len) { 1959 loff_t addr = from; 1960 1961 if (nor->read_proto == SNOR_PROTO_8_8_8_DTR) 1962 ret = spi_nor_octal_dtr_read(nor, addr, len, buf); 1963 else 1964 ret = spi_nor_read_data(nor, addr, len, buf); 1965 1966 if (ret == 0) { 1967 /* We shouldn't see 0-length reads */ 1968 ret = -EIO; 1969 goto read_err; 1970 } 1971 if (ret < 0) 1972 goto read_err; 1973 1974 WARN_ON(ret > len); 1975 *retlen += ret; 1976 buf += ret; 1977 from += ret; 1978 len -= ret; 1979 } 1980 ret = 0; 1981 1982 read_err: 1983 spi_nor_unlock_and_unprep_rd(nor, from_lock, len_lock); 1984 1985 return ret; 1986 } 1987 1988 /* 1989 * On Octal DTR capable flashes, writes cannot start or end at an odd address 1990 * in Octal DTR mode. Extra 0xff bytes need to be appended or prepended to 1991 * make sure the start address and end address are even. 0xff is used because 1992 * on NOR flashes a program operation can only flip bits from 1 to 0, not the 1993 * other way round. 0 to 1 flip needs to happen via erases. 1994 */ 1995 static int spi_nor_octal_dtr_write(struct spi_nor *nor, loff_t to, size_t len, 1996 const u8 *buf) 1997 { 1998 u8 *tmp_buf; 1999 size_t bytes_written; 2000 loff_t start, end; 2001 int ret; 2002 2003 if (IS_ALIGNED(to, 2) && IS_ALIGNED(len, 2)) 2004 return spi_nor_write_data(nor, to, len, buf); 2005 2006 tmp_buf = kmalloc(nor->params->page_size, GFP_KERNEL); 2007 if (!tmp_buf) 2008 return -ENOMEM; 2009 2010 memset(tmp_buf, 0xff, nor->params->page_size); 2011 2012 start = round_down(to, 2); 2013 end = round_up(to + len, 2); 2014 2015 memcpy(tmp_buf + (to - start), buf, len); 2016 2017 ret = spi_nor_write_data(nor, start, end - start, tmp_buf); 2018 if (ret == 0) { 2019 ret = -EIO; 2020 goto out; 2021 } 2022 if (ret < 0) 2023 goto out; 2024 2025 /* 2026 * More bytes are written than actually requested, but that number can't 2027 * be reported to the calling function or it will confuse its 2028 * calculations. Calculate how many of the _requested_ bytes were 2029 * written. 2030 */ 2031 bytes_written = ret; 2032 2033 if (to != start) 2034 ret -= to - start; 2035 2036 /* 2037 * Only account for extra bytes at the end if they were actually 2038 * written. For example, if for some reason the controller could only 2039 * complete a partial write then the adjustment for the extra bytes at 2040 * the end is not needed. 2041 */ 2042 if (start + bytes_written == end) 2043 ret -= end - (to + len); 2044 2045 out: 2046 kfree(tmp_buf); 2047 return ret; 2048 } 2049 2050 /* 2051 * Write an address range to the nor chip. Data must be written in 2052 * FLASH_PAGESIZE chunks. The address range may be any size provided 2053 * it is within the physical boundaries. 2054 */ 2055 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len, 2056 size_t *retlen, const u_char *buf) 2057 { 2058 struct spi_nor *nor = mtd_to_spi_nor(mtd); 2059 size_t i; 2060 ssize_t ret; 2061 u32 page_size = nor->params->page_size; 2062 2063 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len); 2064 2065 ret = spi_nor_prep_and_lock_pe(nor, to, len); 2066 if (ret) 2067 return ret; 2068 2069 for (i = 0; i < len; ) { 2070 ssize_t written; 2071 loff_t addr = to + i; 2072 size_t page_offset = addr & (page_size - 1); 2073 /* the size of data remaining on the first page */ 2074 size_t page_remain = min_t(size_t, page_size - page_offset, len - i); 2075 2076 ret = spi_nor_lock_device(nor); 2077 if (ret) 2078 goto write_err; 2079 2080 ret = spi_nor_write_enable(nor); 2081 if (ret) { 2082 spi_nor_unlock_device(nor); 2083 goto write_err; 2084 } 2085 2086 if (nor->write_proto == SNOR_PROTO_8_8_8_DTR) 2087 ret = spi_nor_octal_dtr_write(nor, addr, page_remain, 2088 buf + i); 2089 else 2090 ret = spi_nor_write_data(nor, addr, page_remain, 2091 buf + i); 2092 spi_nor_unlock_device(nor); 2093 if (ret < 0) 2094 goto write_err; 2095 written = ret; 2096 2097 ret = spi_nor_wait_till_ready(nor); 2098 if (ret) 2099 goto write_err; 2100 *retlen += written; 2101 i += written; 2102 } 2103 2104 write_err: 2105 spi_nor_unlock_and_unprep_pe(nor, to, len); 2106 2107 return ret; 2108 } 2109 2110 static int spi_nor_check(struct spi_nor *nor) 2111 { 2112 if (!nor->dev || 2113 (!nor->spimem && !nor->controller_ops) || 2114 (!nor->spimem && nor->controller_ops && 2115 (!nor->controller_ops->read || 2116 !nor->controller_ops->write || 2117 !nor->controller_ops->read_reg || 2118 !nor->controller_ops->write_reg))) { 2119 pr_err("spi-nor: please fill all the necessary fields!\n"); 2120 return -EINVAL; 2121 } 2122 2123 if (nor->spimem && nor->controller_ops) { 2124 dev_err(nor->dev, "nor->spimem and nor->controller_ops are mutually exclusive, please set just one of them.\n"); 2125 return -EINVAL; 2126 } 2127 2128 return 0; 2129 } 2130 2131 void 2132 spi_nor_set_read_settings(struct spi_nor_read_command *read, 2133 u8 num_mode_clocks, 2134 u8 num_wait_states, 2135 u8 opcode, 2136 enum spi_nor_protocol proto) 2137 { 2138 read->num_mode_clocks = num_mode_clocks; 2139 read->num_wait_states = num_wait_states; 2140 read->opcode = opcode; 2141 read->proto = proto; 2142 } 2143 2144 void spi_nor_set_pp_settings(struct spi_nor_pp_command *pp, u8 opcode, 2145 enum spi_nor_protocol proto) 2146 { 2147 pp->opcode = opcode; 2148 pp->proto = proto; 2149 } 2150 2151 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size) 2152 { 2153 size_t i; 2154 2155 for (i = 0; i < size; i++) 2156 if (table[i][0] == (int)hwcaps) 2157 return table[i][1]; 2158 2159 return -EINVAL; 2160 } 2161 2162 int spi_nor_hwcaps_read2cmd(u32 hwcaps) 2163 { 2164 static const int hwcaps_read2cmd[][2] = { 2165 { SNOR_HWCAPS_READ, SNOR_CMD_READ }, 2166 { SNOR_HWCAPS_READ_FAST, SNOR_CMD_READ_FAST }, 2167 { SNOR_HWCAPS_READ_1_1_1_DTR, SNOR_CMD_READ_1_1_1_DTR }, 2168 { SNOR_HWCAPS_READ_1_1_2, SNOR_CMD_READ_1_1_2 }, 2169 { SNOR_HWCAPS_READ_1_2_2, SNOR_CMD_READ_1_2_2 }, 2170 { SNOR_HWCAPS_READ_2_2_2, SNOR_CMD_READ_2_2_2 }, 2171 { SNOR_HWCAPS_READ_1_2_2_DTR, SNOR_CMD_READ_1_2_2_DTR }, 2172 { SNOR_HWCAPS_READ_1_1_4, SNOR_CMD_READ_1_1_4 }, 2173 { SNOR_HWCAPS_READ_1_4_4, SNOR_CMD_READ_1_4_4 }, 2174 { SNOR_HWCAPS_READ_4_4_4, SNOR_CMD_READ_4_4_4 }, 2175 { SNOR_HWCAPS_READ_1_4_4_DTR, SNOR_CMD_READ_1_4_4_DTR }, 2176 { SNOR_HWCAPS_READ_1_1_8, SNOR_CMD_READ_1_1_8 }, 2177 { SNOR_HWCAPS_READ_1_8_8, SNOR_CMD_READ_1_8_8 }, 2178 { SNOR_HWCAPS_READ_8_8_8, SNOR_CMD_READ_8_8_8 }, 2179 { SNOR_HWCAPS_READ_1_8_8_DTR, SNOR_CMD_READ_1_8_8_DTR }, 2180 { SNOR_HWCAPS_READ_8_8_8_DTR, SNOR_CMD_READ_8_8_8_DTR }, 2181 }; 2182 2183 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd, 2184 ARRAY_SIZE(hwcaps_read2cmd)); 2185 } 2186 2187 int spi_nor_hwcaps_pp2cmd(u32 hwcaps) 2188 { 2189 static const int hwcaps_pp2cmd[][2] = { 2190 { SNOR_HWCAPS_PP, SNOR_CMD_PP }, 2191 { SNOR_HWCAPS_PP_1_1_4, SNOR_CMD_PP_1_1_4 }, 2192 { SNOR_HWCAPS_PP_1_4_4, SNOR_CMD_PP_1_4_4 }, 2193 { SNOR_HWCAPS_PP_4_4_4, SNOR_CMD_PP_4_4_4 }, 2194 { SNOR_HWCAPS_PP_1_1_8, SNOR_CMD_PP_1_1_8 }, 2195 { SNOR_HWCAPS_PP_1_8_8, SNOR_CMD_PP_1_8_8 }, 2196 { SNOR_HWCAPS_PP_8_8_8, SNOR_CMD_PP_8_8_8 }, 2197 { SNOR_HWCAPS_PP_8_8_8_DTR, SNOR_CMD_PP_8_8_8_DTR }, 2198 }; 2199 2200 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd, 2201 ARRAY_SIZE(hwcaps_pp2cmd)); 2202 } 2203 2204 /** 2205 * spi_nor_spimem_check_read_pp_op - check if a read or a page program operation is 2206 * supported by controller 2207 *@nor: pointer to a 'struct spi_nor' 2208 *@op: pointer to op template to be checked 2209 * 2210 * Returns 0 if operation is supported, -EOPNOTSUPP otherwise. 2211 */ 2212 static int spi_nor_spimem_check_read_pp_op(struct spi_nor *nor, 2213 struct spi_mem_op *op) 2214 { 2215 /* 2216 * First test with 4 address bytes. The opcode itself might 2217 * be a 3B addressing opcode but we don't care, because 2218 * SPI controller implementation should not check the opcode, 2219 * but just the sequence. 2220 */ 2221 op->addr.nbytes = 4; 2222 if (!spi_mem_supports_op(nor->spimem, op)) { 2223 if (nor->params->size > SZ_16M) 2224 return -EOPNOTSUPP; 2225 2226 /* If flash size <= 16MB, 3 address bytes are sufficient */ 2227 op->addr.nbytes = 3; 2228 if (!spi_mem_supports_op(nor->spimem, op)) 2229 return -EOPNOTSUPP; 2230 } 2231 2232 return 0; 2233 } 2234 2235 /** 2236 * spi_nor_spimem_check_readop - check if the read op is supported 2237 * by controller 2238 *@nor: pointer to a 'struct spi_nor' 2239 *@read: pointer to op template to be checked 2240 * 2241 * Returns 0 if operation is supported, -EOPNOTSUPP otherwise. 2242 */ 2243 static int spi_nor_spimem_check_readop(struct spi_nor *nor, 2244 const struct spi_nor_read_command *read) 2245 { 2246 struct spi_mem_op op = SPI_NOR_READ_OP(read->opcode); 2247 2248 spi_nor_spimem_setup_op(nor, &op, read->proto); 2249 2250 /* convert the dummy cycles to the number of bytes */ 2251 op.dummy.nbytes = (read->num_mode_clocks + read->num_wait_states) * 2252 op.dummy.buswidth / 8; 2253 if (spi_nor_protocol_is_dtr(read->proto)) 2254 op.dummy.nbytes *= 2; 2255 2256 return spi_nor_spimem_check_read_pp_op(nor, &op); 2257 } 2258 2259 /** 2260 * spi_nor_spimem_check_pp - check if the page program op is supported 2261 * by controller 2262 *@nor: pointer to a 'struct spi_nor' 2263 *@pp: pointer to op template to be checked 2264 * 2265 * Returns 0 if operation is supported, -EOPNOTSUPP otherwise. 2266 */ 2267 static int spi_nor_spimem_check_pp(struct spi_nor *nor, 2268 const struct spi_nor_pp_command *pp) 2269 { 2270 struct spi_mem_op op = SPI_NOR_PP_OP(pp->opcode); 2271 2272 spi_nor_spimem_setup_op(nor, &op, pp->proto); 2273 2274 return spi_nor_spimem_check_read_pp_op(nor, &op); 2275 } 2276 2277 /** 2278 * spi_nor_spimem_adjust_hwcaps - Find optimal Read/Write protocol 2279 * based on SPI controller capabilities 2280 * @nor: pointer to a 'struct spi_nor' 2281 * @hwcaps: pointer to resulting capabilities after adjusting 2282 * according to controller and flash's capability 2283 */ 2284 static void 2285 spi_nor_spimem_adjust_hwcaps(struct spi_nor *nor, u32 *hwcaps) 2286 { 2287 struct spi_nor_flash_parameter *params = nor->params; 2288 unsigned int cap; 2289 u8 opcode; 2290 2291 /* X-X-X modes are not supported yet, mask them all. */ 2292 *hwcaps &= ~SNOR_HWCAPS_X_X_X; 2293 2294 /* 2295 * If the reset line is broken, we do not want to enter a stateful 2296 * mode. 2297 */ 2298 if (params->flags & SNOR_F_BROKEN_RESET) 2299 *hwcaps &= ~(SNOR_HWCAPS_X_X_X | SNOR_HWCAPS_X_X_X_DTR); 2300 2301 for (cap = 0; cap < sizeof(*hwcaps) * BITS_PER_BYTE; cap++) { 2302 int rdidx, ppidx; 2303 2304 if (!(*hwcaps & BIT(cap))) 2305 continue; 2306 2307 rdidx = spi_nor_hwcaps_read2cmd(BIT(cap)); 2308 if (rdidx >= 0 && 2309 spi_nor_spimem_check_readop(nor, ¶ms->reads[rdidx])) 2310 *hwcaps &= ~BIT(cap); 2311 2312 ppidx = spi_nor_hwcaps_pp2cmd(BIT(cap)); 2313 if (ppidx < 0) 2314 continue; 2315 2316 if (spi_nor_spimem_check_pp(nor, 2317 ¶ms->page_programs[ppidx])) 2318 *hwcaps &= ~BIT(cap); 2319 } 2320 2321 /* Some SPI controllers might not support reading SR2 */ 2322 opcode = nor->params->opcodes.read_sr2; 2323 if (opcode) { 2324 struct spi_mem_op op = SPI_NOR_RDSR_OP(opcode, nor->bouncebuf, 1); 2325 2326 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); 2327 2328 if (!spi_mem_supports_op(nor->spimem, &op)) 2329 nor->params->opcodes.read_sr2 = 0; 2330 } 2331 } 2332 2333 /** 2334 * spi_nor_set_erase_type() - set a SPI NOR erase type 2335 * @erase: pointer to a structure that describes a SPI NOR erase type 2336 * @size: the size of the sector/block erased by the erase type 2337 * @opcode: the SPI command op code to erase the sector/block 2338 */ 2339 void spi_nor_set_erase_type(struct spi_nor_erase_type *erase, u32 size, 2340 u8 opcode) 2341 { 2342 erase->size = size; 2343 erase->opcode = opcode; 2344 /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */ 2345 erase->size_shift = ffs(erase->size) - 1; 2346 erase->size_mask = (1 << erase->size_shift) - 1; 2347 } 2348 2349 /** 2350 * spi_nor_mask_erase_type() - mask out a SPI NOR erase type 2351 * @erase: pointer to a structure that describes a SPI NOR erase type 2352 */ 2353 void spi_nor_mask_erase_type(struct spi_nor_erase_type *erase) 2354 { 2355 erase->size = 0; 2356 } 2357 2358 /** 2359 * spi_nor_init_uniform_erase_map() - Initialize uniform erase map 2360 * @map: the erase map of the SPI NOR 2361 * @erase_mask: bitmask encoding erase types that can erase the entire 2362 * flash memory 2363 * @flash_size: the spi nor flash memory size 2364 */ 2365 void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map, 2366 u8 erase_mask, u64 flash_size) 2367 { 2368 map->uniform_region.offset = 0; 2369 map->uniform_region.size = flash_size; 2370 map->uniform_region.erase_mask = erase_mask; 2371 map->regions = &map->uniform_region; 2372 map->n_regions = 1; 2373 } 2374 2375 static int spi_nor_select_read(struct spi_nor *nor, 2376 u32 shared_hwcaps) 2377 { 2378 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1; 2379 const struct spi_nor_read_command *read; 2380 2381 if (best_match < 0) 2382 return -EINVAL; 2383 2384 cmd = spi_nor_hwcaps_read2cmd(BIT(best_match)); 2385 if (cmd < 0) 2386 return -EINVAL; 2387 2388 read = &nor->params->reads[cmd]; 2389 nor->read_opcode = read->opcode; 2390 nor->read_proto = read->proto; 2391 2392 /* 2393 * In the SPI NOR framework, we don't need to make the difference 2394 * between mode clock cycles and wait state clock cycles. 2395 * Indeed, the value of the mode clock cycles is used by a QSPI 2396 * flash memory to know whether it should enter or leave its 0-4-4 2397 * (Continuous Read / XIP) mode. 2398 * eXecution In Place is out of the scope of the mtd sub-system. 2399 * Hence we choose to merge both mode and wait state clock cycles 2400 * into the so called dummy clock cycles. 2401 */ 2402 nor->read_dummy = read->num_mode_clocks + read->num_wait_states; 2403 return 0; 2404 } 2405 2406 static int spi_nor_select_pp(struct spi_nor *nor, 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 = &nor->params->page_programs[cmd]; 2420 nor->program_opcode = pp->opcode; 2421 nor->write_proto = pp->proto; 2422 return 0; 2423 } 2424 2425 /** 2426 * spi_nor_select_uniform_erase() - select optimum uniform erase type 2427 * @map: the erase map of the SPI NOR 2428 * 2429 * Once the optimum uniform sector erase command is found, disable all the 2430 * other. 2431 * 2432 * Return: pointer to erase type on success, NULL otherwise. 2433 */ 2434 static const struct spi_nor_erase_type * 2435 spi_nor_select_uniform_erase(struct spi_nor_erase_map *map) 2436 { 2437 const struct spi_nor_erase_type *tested_erase, *erase = NULL; 2438 int i; 2439 u8 uniform_erase_type = map->uniform_region.erase_mask; 2440 2441 /* 2442 * Search for the biggest erase size, except for when compiled 2443 * to use 4k erases. 2444 */ 2445 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) { 2446 if (!(uniform_erase_type & BIT(i))) 2447 continue; 2448 2449 tested_erase = &map->erase_type[i]; 2450 2451 /* Skip masked erase types. */ 2452 if (!tested_erase->size) 2453 continue; 2454 2455 /* 2456 * If the current erase size is the 4k one, stop here, 2457 * we have found the right uniform Sector Erase command. 2458 */ 2459 if (IS_ENABLED(CONFIG_MTD_SPI_NOR_USE_4K_SECTORS) && 2460 tested_erase->size == SZ_4K) { 2461 erase = tested_erase; 2462 break; 2463 } 2464 2465 /* 2466 * Otherwise, the current erase size is still a valid candidate. 2467 * Select the biggest valid candidate. 2468 */ 2469 if (!erase && tested_erase->size) 2470 erase = tested_erase; 2471 /* keep iterating to find the wanted_size */ 2472 } 2473 2474 if (!erase) 2475 return NULL; 2476 2477 /* Disable all other Sector Erase commands. */ 2478 map->uniform_region.erase_mask = BIT(erase - map->erase_type); 2479 return erase; 2480 } 2481 2482 static int spi_nor_select_erase(struct spi_nor *nor) 2483 { 2484 struct spi_nor_erase_map *map = &nor->params->erase_map; 2485 const struct spi_nor_erase_type *erase = NULL; 2486 struct mtd_info *mtd = &nor->mtd; 2487 int i; 2488 2489 /* 2490 * The previous implementation handling Sector Erase commands assumed 2491 * that the SPI flash memory has an uniform layout then used only one 2492 * of the supported erase sizes for all Sector Erase commands. 2493 * So to be backward compatible, the new implementation also tries to 2494 * manage the SPI flash memory as uniform with a single erase sector 2495 * size, when possible. 2496 */ 2497 if (spi_nor_has_uniform_erase(nor)) { 2498 erase = spi_nor_select_uniform_erase(map); 2499 if (!erase) 2500 return -EINVAL; 2501 nor->erase_opcode = erase->opcode; 2502 mtd->erasesize = erase->size; 2503 return 0; 2504 } 2505 2506 /* 2507 * For non-uniform SPI flash memory, set mtd->erasesize to the 2508 * maximum erase sector size. No need to set nor->erase_opcode. 2509 */ 2510 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) { 2511 if (map->erase_type[i].size) { 2512 erase = &map->erase_type[i]; 2513 break; 2514 } 2515 } 2516 2517 if (!erase) 2518 return -EINVAL; 2519 2520 mtd->erasesize = erase->size; 2521 return 0; 2522 } 2523 2524 static int spi_nor_set_addr_nbytes(struct spi_nor *nor) 2525 { 2526 struct spi_nor_flash_parameter *params = nor->params; 2527 2528 if (nor->params->addr_nbytes) { 2529 nor->addr_nbytes = nor->params->addr_nbytes; 2530 } else if (nor->read_proto == SNOR_PROTO_8_8_8_DTR) { 2531 /* 2532 * In 8D-8D-8D mode, one byte takes half a cycle to transfer. So 2533 * in this protocol an odd addr_nbytes cannot be used because 2534 * then the address phase would only span a cycle and a half. 2535 * Half a cycle would be left over. We would then have to start 2536 * the dummy phase in the middle of a cycle and so too the data 2537 * phase, and we will end the transaction with half a cycle left 2538 * over. 2539 * 2540 * Force all 8D-8D-8D flashes to use an addr_nbytes of 4 to 2541 * avoid this situation. 2542 */ 2543 nor->addr_nbytes = 4; 2544 } else if (nor->info->addr_nbytes) { 2545 nor->addr_nbytes = nor->info->addr_nbytes; 2546 } else { 2547 nor->addr_nbytes = 3; 2548 } 2549 2550 if (nor->addr_nbytes == 3 && nor->params->size > 0x1000000) { 2551 /* enable 4-byte addressing if the device exceeds 16MiB */ 2552 nor->addr_nbytes = 4; 2553 } 2554 2555 if (nor->addr_nbytes > SPI_NOR_MAX_ADDR_NBYTES) { 2556 dev_dbg(nor->dev, "The number of address bytes is too large: %u\n", 2557 nor->addr_nbytes); 2558 return -EINVAL; 2559 } 2560 2561 /* Set 4byte opcodes when possible. */ 2562 if (nor->addr_nbytes == 4 && params->flags & SNOR_F_4B_OPCODES && 2563 !(params->flags & SNOR_F_HAS_4BAIT)) 2564 spi_nor_set_4byte_opcodes(nor); 2565 2566 return 0; 2567 } 2568 2569 static int spi_nor_setup(struct spi_nor *nor, 2570 const struct spi_nor_hwcaps *hwcaps) 2571 { 2572 struct spi_nor_flash_parameter *params = nor->params; 2573 u32 ignored_mask, shared_mask; 2574 int err; 2575 2576 /* 2577 * Keep only the hardware capabilities supported by both the SPI 2578 * controller and the SPI flash memory. 2579 */ 2580 shared_mask = hwcaps->mask & params->hwcaps.mask; 2581 2582 if (nor->spimem) { 2583 /* 2584 * When called from spi_nor_probe(), all caps are set and we 2585 * need to discard some of them based on what the SPI 2586 * controller actually supports (using spi_mem_supports_op()). 2587 */ 2588 spi_nor_spimem_adjust_hwcaps(nor, &shared_mask); 2589 } else { 2590 /* 2591 * SPI n-n-n protocols are not supported when the SPI 2592 * controller directly implements the spi_nor interface. 2593 * Yet another reason to switch to spi-mem. 2594 */ 2595 ignored_mask = SNOR_HWCAPS_X_X_X | SNOR_HWCAPS_X_X_X_DTR; 2596 if (shared_mask & ignored_mask) { 2597 dev_dbg(nor->dev, 2598 "SPI n-n-n protocols are not supported.\n"); 2599 shared_mask &= ~ignored_mask; 2600 } 2601 } 2602 2603 /* Select the (Fast) Read command. */ 2604 err = spi_nor_select_read(nor, shared_mask); 2605 if (err) { 2606 dev_dbg(nor->dev, 2607 "can't select read settings supported by both the SPI controller and memory.\n"); 2608 return err; 2609 } 2610 2611 /* Select the Page Program command. */ 2612 err = spi_nor_select_pp(nor, shared_mask); 2613 if (err) { 2614 dev_dbg(nor->dev, 2615 "can't select write settings supported by both the SPI controller and memory.\n"); 2616 return err; 2617 } 2618 2619 /* Select the Sector Erase command. */ 2620 err = spi_nor_select_erase(nor); 2621 if (err) { 2622 dev_dbg(nor->dev, 2623 "can't select erase settings supported by both the SPI controller and memory.\n"); 2624 return err; 2625 } 2626 2627 return spi_nor_set_addr_nbytes(nor); 2628 } 2629 2630 bool spi_nor_fixup_match(const struct spi_nor *nor, 2631 const struct spi_nor_fixup *fixup) 2632 { 2633 const struct spi_nor_id *id = nor->info ? nor->info->id : NULL; 2634 2635 /* Filter by ID first, if available */ 2636 if (fixup->id) { 2637 if (!id || fixup->id->len > id->len || 2638 memcmp(id->bytes, fixup->id->bytes, fixup->id->len)) 2639 return false; 2640 } 2641 2642 /* Further filter with the match callback, if provided */ 2643 if (fixup->match) 2644 return fixup->match(nor); 2645 2646 /* Either there was an ID and it matched, or it is a catch-all entry */ 2647 return true; 2648 } 2649 2650 /** 2651 * spi_nor_manufacturer_init_params() - Initialize the flash's parameters and 2652 * settings based on MFR register and ->default_init() hook. 2653 * @nor: pointer to a 'struct spi_nor'. 2654 */ 2655 static void spi_nor_manufacturer_init_params(struct spi_nor *nor) 2656 { 2657 const struct spi_nor_fixup *fixups; 2658 unsigned int i; 2659 2660 if (!nor->manufacturer || !nor->manufacturer->fixups) 2661 return; 2662 2663 fixups = nor->manufacturer->fixups; 2664 2665 for (i = 0; i < nor->manufacturer->nfixups; i++) { 2666 if (fixups[i].fixups && fixups[i].fixups->default_init && 2667 spi_nor_fixup_match(nor, &fixups[i])) 2668 fixups[i].fixups->default_init(nor); 2669 } 2670 } 2671 2672 /** 2673 * spi_nor_no_sfdp_init_params() - Initialize the flash's parameters and 2674 * settings based on nor->info->sfdp_flags. 2675 * If the flash supports SFDP but the information is wrong and the settings from 2676 * this function can not be retrieved by parsing SFDP, one should instead use 2677 * the fixup hooks and update the wrong bits. 2678 * @nor: pointer to a 'struct spi_nor'. 2679 */ 2680 static void spi_nor_no_sfdp_init_params(struct spi_nor *nor) 2681 { 2682 struct spi_nor_flash_parameter *params = nor->params; 2683 struct spi_nor_erase_map *map = ¶ms->erase_map; 2684 const struct flash_info *info = nor->info; 2685 const u8 no_sfdp_flags = info->no_sfdp_flags; 2686 u8 i, erase_mask; 2687 2688 if (no_sfdp_flags & SPI_NOR_DUAL_READ) { 2689 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2; 2690 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_2], 2691 0, 8, SPINOR_OP_READ_1_1_2, 2692 SNOR_PROTO_1_1_2); 2693 } 2694 2695 if (no_sfdp_flags & SPI_NOR_QUAD_READ) { 2696 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4; 2697 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_4], 2698 0, 8, SPINOR_OP_READ_1_1_4, 2699 SNOR_PROTO_1_1_4); 2700 } 2701 2702 if (no_sfdp_flags & SPI_NOR_OCTAL_READ) { 2703 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8; 2704 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_1_1_8], 2705 0, 8, SPINOR_OP_READ_1_1_8, 2706 SNOR_PROTO_1_1_8); 2707 } 2708 2709 if (no_sfdp_flags & SPI_NOR_OCTAL_DTR_READ) { 2710 params->hwcaps.mask |= SNOR_HWCAPS_READ_8_8_8_DTR; 2711 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_8_8_8_DTR], 2712 0, 20, SPINOR_OP_READ_FAST, 2713 SNOR_PROTO_8_8_8_DTR); 2714 } 2715 2716 if (no_sfdp_flags & SPI_NOR_OCTAL_DTR_PP) { 2717 params->hwcaps.mask |= SNOR_HWCAPS_PP_8_8_8_DTR; 2718 /* 2719 * Since xSPI Page Program opcode is backward compatible with 2720 * Legacy SPI, use Legacy SPI opcode there as well. 2721 */ 2722 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP_8_8_8_DTR], 2723 SPINOR_OP_PP, SNOR_PROTO_8_8_8_DTR); 2724 } 2725 2726 /* 2727 * Sector Erase settings. Sort Erase Types in ascending order, with the 2728 * smallest erase size starting at BIT(0). 2729 */ 2730 erase_mask = 0; 2731 i = 0; 2732 if (no_sfdp_flags & SECT_4K) { 2733 erase_mask |= BIT(i); 2734 spi_nor_set_erase_type(&map->erase_type[i], 4096u, 2735 SPINOR_OP_BE_4K); 2736 i++; 2737 } 2738 erase_mask |= BIT(i); 2739 spi_nor_set_erase_type(&map->erase_type[i], 2740 info->sector_size ?: SPI_NOR_DEFAULT_SECTOR_SIZE, 2741 SPINOR_OP_SE); 2742 spi_nor_init_uniform_erase_map(map, erase_mask, params->size); 2743 } 2744 2745 /** 2746 * spi_nor_init_flags() - Initialize NOR flags for settings that are not defined 2747 * in the JESD216 SFDP standard, thus can not be retrieved when parsing SFDP. 2748 * @nor: pointer to a 'struct spi_nor' 2749 */ 2750 static void spi_nor_init_flags(struct spi_nor *nor) 2751 { 2752 struct spi_nor_flash_parameter *params = nor->params; 2753 struct device_node *np = spi_nor_get_flash_node(nor); 2754 const u16 flags = nor->info->flags; 2755 2756 if (of_property_read_bool(np, "broken-flash-reset")) 2757 params->flags |= SNOR_F_BROKEN_RESET; 2758 2759 if (of_property_read_bool(np, "no-wp")) 2760 params->flags |= SNOR_F_NO_WP; 2761 2762 if (flags & SPI_NOR_SWP_IS_VOLATILE) 2763 params->flags |= SNOR_F_SWP_IS_VOLATILE; 2764 2765 if (flags & SPI_NOR_HAS_LOCK) 2766 params->flags |= SNOR_F_HAS_LOCK; 2767 2768 if (flags & SPI_NOR_HAS_TB) { 2769 params->flags |= SNOR_F_HAS_SR_TB; 2770 if (flags & SPI_NOR_TB_SR_BIT6) 2771 params->flags |= SNOR_F_HAS_SR_TB_BIT6; 2772 } 2773 2774 if (flags & SPI_NOR_4BIT_BP) { 2775 params->flags |= SNOR_F_HAS_4BIT_BP; 2776 if (flags & SPI_NOR_BP3_SR_BIT6) 2777 params->flags |= SNOR_F_HAS_SR_BP3_BIT6; 2778 } 2779 2780 if (flags & SPI_NOR_HAS_CMP) 2781 params->flags |= SNOR_F_HAS_SR2_CMP_BIT6; 2782 2783 if (flags & SPI_NOR_RWW && nor->params->n_banks > 1 && 2784 !nor->controller_ops) 2785 params->flags |= SNOR_F_RWW; 2786 } 2787 2788 /** 2789 * spi_nor_init_fixup_flags() - Initialize NOR flags for settings that can not 2790 * be discovered by SFDP for this particular flash because the SFDP table that 2791 * indicates this support is not defined in the flash. In case the table for 2792 * this support is defined but has wrong values, one should instead use a 2793 * post_sfdp() hook to set the SNOR_F equivalent flag. 2794 * @nor: pointer to a 'struct spi_nor' 2795 */ 2796 static void spi_nor_init_fixup_flags(struct spi_nor *nor) 2797 { 2798 struct spi_nor_flash_parameter *params = nor->params; 2799 const struct spi_nor_fixup *fixups; 2800 unsigned int i; 2801 2802 if (!nor->manufacturer || !nor->manufacturer->fixups) 2803 return; 2804 2805 fixups = nor->manufacturer->fixups; 2806 2807 for (i = 0; i < nor->manufacturer->nfixups; i++) { 2808 if (!fixups[i].fixup_flags || 2809 !spi_nor_fixup_match(nor, &fixups[i])) 2810 continue; 2811 2812 if (fixups[i].fixup_flags & SPI_NOR_4B_OPCODES) 2813 params->flags |= SNOR_F_4B_OPCODES; 2814 if (fixups[i].fixup_flags & SPI_NOR_IO_MODE_EN_VOLATILE) 2815 params->flags |= SNOR_F_IO_MODE_EN_VOLATILE; 2816 } 2817 } 2818 2819 /** 2820 * spi_nor_late_init_params() - Late initialization of default flash parameters. 2821 * @nor: pointer to a 'struct spi_nor' 2822 * 2823 * Used to initialize flash parameters that are not declared in the JESD216 2824 * SFDP standard, or where SFDP tables are not defined at all. 2825 * Will replace the spi_nor_manufacturer_init_params() method. 2826 */ 2827 static int spi_nor_late_init_params(struct spi_nor *nor) 2828 { 2829 struct spi_nor_flash_parameter *params = nor->params; 2830 const struct spi_nor_fixup *fixups; 2831 unsigned int i; 2832 int ret; 2833 2834 /* Needed by some late_init hooks */ 2835 spi_nor_init_flags(nor); 2836 2837 if (nor->manufacturer && nor->manufacturer->fixups) { 2838 fixups = nor->manufacturer->fixups; 2839 2840 for (i = 0; i < nor->manufacturer->nfixups; i++) { 2841 if (fixups[i].fixups && fixups[i].fixups->late_init && 2842 spi_nor_fixup_match(nor, &fixups[i])) { 2843 ret = fixups[i].fixups->late_init(nor); 2844 if (ret) 2845 return ret; 2846 } 2847 } 2848 } 2849 2850 if (!nor->params->opcodes.die_erase) 2851 nor->params->opcodes.die_erase = SPINOR_OP_CHIP_ERASE; 2852 2853 /* Default method kept for backward compatibility. */ 2854 if (!params->set_4byte_addr_mode) 2855 params->set_4byte_addr_mode = spi_nor_set_4byte_addr_mode_brwr; 2856 2857 spi_nor_init_fixup_flags(nor); 2858 2859 /* 2860 * NOR protection support. When locking_ops are not provided, we pick 2861 * the default ones. 2862 */ 2863 if (params->flags & SNOR_F_HAS_LOCK && !nor->params->locking_ops) 2864 spi_nor_init_default_locking_ops(nor); 2865 2866 if (params->n_banks > 1) 2867 params->bank_size = div_u64(params->size, params->n_banks); 2868 2869 return 0; 2870 } 2871 2872 /** 2873 * spi_nor_init_default_params() - Default initialization of flash parameters 2874 * and settings. Done for all flashes, regardless is they define SFDP tables 2875 * or not. 2876 * @nor: pointer to a 'struct spi_nor'. 2877 */ 2878 static void spi_nor_init_default_params(struct spi_nor *nor) 2879 { 2880 struct spi_nor_flash_parameter *params = nor->params; 2881 const struct flash_info *info = nor->info; 2882 struct device_node *np = spi_nor_get_flash_node(nor); 2883 2884 /* Default to 16-bit Read/Write Status commands */ 2885 params->opcodes.read_sr1 = SPINOR_OP_RDSR; 2886 params->opcodes.read_sr2 = SPINOR_OP_RDCR; 2887 params->opcodes.write_sr1_and_sr2 = SPINOR_OP_WRSR; 2888 params->quad_enable = spi_nor_generic_quad_enable; 2889 params->qe_mask[1] = BIT(1); 2890 2891 params->otp.org = info->otp; 2892 2893 /* Set SPI NOR sizes. */ 2894 params->writesize = 1; 2895 params->size = info->size; 2896 params->bank_size = params->size; 2897 params->page_size = info->page_size ?: SPI_NOR_DEFAULT_PAGE_SIZE; 2898 params->n_banks = info->n_banks ?: SPI_NOR_DEFAULT_N_BANKS; 2899 2900 /* Default to Fast Read for non-DT and enable it if requested by DT. */ 2901 if (!np || of_property_read_bool(np, "m25p,fast-read")) 2902 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST; 2903 2904 /* (Fast) Read settings. */ 2905 params->hwcaps.mask |= SNOR_HWCAPS_READ; 2906 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ], 2907 0, 0, SPINOR_OP_READ, 2908 SNOR_PROTO_1_1_1); 2909 2910 if (params->hwcaps.mask & SNOR_HWCAPS_READ_FAST) 2911 spi_nor_set_read_settings(¶ms->reads[SNOR_CMD_READ_FAST], 2912 0, 8, SPINOR_OP_READ_FAST, 2913 SNOR_PROTO_1_1_1); 2914 /* Page Program settings. */ 2915 params->hwcaps.mask |= SNOR_HWCAPS_PP; 2916 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP], 2917 SPINOR_OP_PP, SNOR_PROTO_1_1_1); 2918 2919 if (info->flags & SPI_NOR_QUAD_PP) { 2920 params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4; 2921 spi_nor_set_pp_settings(¶ms->page_programs[SNOR_CMD_PP_1_1_4], 2922 SPINOR_OP_PP_1_1_4, SNOR_PROTO_1_1_4); 2923 } 2924 } 2925 2926 /** 2927 * spi_nor_init_params() - Initialize the flash's parameters and settings. 2928 * @nor: pointer to a 'struct spi_nor'. 2929 * 2930 * The flash parameters and settings are initialized based on a sequence of 2931 * calls that are ordered by priority: 2932 * 2933 * 1/ Default flash parameters initialization. The initializations are done 2934 * based on nor->info data: 2935 * spi_nor_init_default_params() 2936 * spi_nor_no_sfdp_init_params() 2937 * 2938 * which can be overwritten by: 2939 * 2/ Manufacturer flash parameters initialization. The initializations are 2940 * done based on MFR register, or when the decisions can not be done solely 2941 * based on MFR, by using specific flash_info tweeks, ->default_init(): 2942 * spi_nor_manufacturer_init_params() 2943 * 2944 * which can be overwritten by: 2945 * 3/ SFDP flash parameters initialization. JESD216 SFDP is a standard and 2946 * should be more accurate that the above. 2947 * spi_nor_parse_sfdp() 2948 * 2949 * Please note that there is a ->post_bfpt() fixup hook that can overwrite 2950 * the flash parameters and settings immediately after parsing the Basic 2951 * Flash Parameter Table. 2952 * spi_nor_post_sfdp_fixups() is called after the SFDP tables are parsed. 2953 * It is used to tweak various flash parameters when information provided 2954 * by the SFDP tables are wrong. 2955 * 2956 * which can be overwritten by: 2957 * 4/ Late flash parameters initialization, used to initialize flash 2958 * parameters that are not declared in the JESD216 SFDP standard, or where SFDP 2959 * tables are not defined at all. 2960 * spi_nor_late_init_params() 2961 * 2962 * Return: 0 on success, -errno otherwise. 2963 */ 2964 static int spi_nor_init_params(struct spi_nor *nor) 2965 { 2966 int ret; 2967 2968 nor->params = devm_kzalloc_obj(nor->dev, *nor->params); 2969 if (!nor->params) 2970 return -ENOMEM; 2971 2972 spi_nor_init_default_params(nor); 2973 spi_nor_no_sfdp_init_params(nor); 2974 spi_nor_manufacturer_init_params(nor); 2975 2976 ret = spi_nor_parse_sfdp(nor); 2977 if (ret && spi_nor_needs_sfdp(nor)) { 2978 dev_err(nor->dev, 2979 "SFDP parsing failed. You need to manually declare the flash parameters.\n"); 2980 return ret; 2981 } 2982 2983 ret = spi_nor_late_init_params(nor); 2984 if (ret) 2985 return ret; 2986 2987 if (WARN_ON(!is_power_of_2(nor->params->page_size))) 2988 return -EINVAL; 2989 2990 return 0; 2991 } 2992 2993 /** spi_nor_set_octal_dtr() - enable or disable Octal DTR I/O. 2994 * @nor: pointer to a 'struct spi_nor' 2995 * @enable: whether to enable or disable Octal DTR 2996 * 2997 * Return: 0 on success, -errno otherwise. 2998 */ 2999 static int spi_nor_set_octal_dtr(struct spi_nor *nor, bool enable) 3000 { 3001 int ret; 3002 3003 if (!nor->params->set_octal_dtr) 3004 return 0; 3005 3006 if (!(nor->read_proto == SNOR_PROTO_8_8_8_DTR && 3007 nor->write_proto == SNOR_PROTO_8_8_8_DTR)) 3008 return 0; 3009 3010 if (!(nor->params->flags & SNOR_F_IO_MODE_EN_VOLATILE)) 3011 return 0; 3012 3013 ret = nor->params->set_octal_dtr(nor, enable); 3014 if (ret) 3015 return ret; 3016 3017 if (enable) 3018 nor->reg_proto = SNOR_PROTO_8_8_8_DTR; 3019 else 3020 nor->reg_proto = SNOR_PROTO_1_1_1; 3021 3022 return 0; 3023 } 3024 3025 /** 3026 * spi_nor_quad_enable() - enable Quad I/O if needed. 3027 * @nor: pointer to a 'struct spi_nor' 3028 * 3029 * Return: 0 on success, -errno otherwise. 3030 */ 3031 static int spi_nor_quad_enable(struct spi_nor *nor) 3032 { 3033 if (!nor->params->quad_enable) 3034 return 0; 3035 3036 if (!(spi_nor_get_protocol_width(nor->read_proto) == 4 || 3037 spi_nor_get_protocol_width(nor->write_proto) == 4)) 3038 return 0; 3039 3040 return nor->params->quad_enable(nor); 3041 } 3042 3043 /** 3044 * spi_nor_set_4byte_addr_mode() - Set address mode. 3045 * @nor: pointer to a 'struct spi_nor'. 3046 * @enable: enable/disable 4 byte address mode. 3047 * 3048 * Return: 0 on success, -errno otherwise. 3049 */ 3050 int spi_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable) 3051 { 3052 struct spi_nor_flash_parameter *params = nor->params; 3053 int ret; 3054 3055 if (enable) { 3056 /* 3057 * If the RESET# pin isn't hooked up properly, or the system 3058 * otherwise doesn't perform a reset command in the boot 3059 * sequence, it's impossible to 100% protect against unexpected 3060 * reboots (e.g., crashes). Warn the user (or hopefully, system 3061 * designer) that this is bad. 3062 */ 3063 WARN_ONCE(nor->params->flags & SNOR_F_BROKEN_RESET, 3064 "enabling reset hack; may not recover from unexpected reboots\n"); 3065 } 3066 3067 ret = params->set_4byte_addr_mode(nor, enable); 3068 if (ret && ret != -EOPNOTSUPP) 3069 return ret; 3070 3071 if (enable) { 3072 params->addr_nbytes = 4; 3073 params->addr_mode_nbytes = 4; 3074 } else { 3075 params->addr_nbytes = 3; 3076 params->addr_mode_nbytes = 3; 3077 } 3078 3079 return 0; 3080 } 3081 3082 static int spi_nor_init(struct spi_nor *nor) 3083 { 3084 struct spi_nor_flash_parameter *params = nor->params; 3085 int err; 3086 3087 err = spi_nor_set_octal_dtr(nor, true); 3088 if (err) { 3089 dev_dbg(nor->dev, "octal mode not supported\n"); 3090 return err; 3091 } 3092 3093 err = spi_nor_quad_enable(nor); 3094 if (err) { 3095 dev_dbg(nor->dev, "quad mode not supported\n"); 3096 return err; 3097 } 3098 3099 /* 3100 * Some SPI NOR flashes are write protected by default after a power-on 3101 * reset cycle, in order to avoid inadvertent writes during power-up. 3102 * Backward compatibility imposes to unlock the entire flash memory 3103 * array at power-up by default. Depending on the kernel configuration 3104 * (1) do nothing, (2) always unlock the entire flash array or (3) 3105 * unlock the entire flash array only when the software write 3106 * protection bits are volatile. The latter is indicated by 3107 * SNOR_F_SWP_IS_VOLATILE. 3108 */ 3109 spi_nor_cache_sr_lock_bits(nor, NULL); 3110 if (IS_ENABLED(CONFIG_MTD_SPI_NOR_SWP_DISABLE) || 3111 (IS_ENABLED(CONFIG_MTD_SPI_NOR_SWP_DISABLE_ON_VOLATILE) && 3112 params->flags & SNOR_F_SWP_IS_VOLATILE)) { 3113 spi_nor_try_unlock_all(nor); 3114 } 3115 3116 if (nor->addr_nbytes == 4 && 3117 nor->read_proto != SNOR_PROTO_8_8_8_DTR && 3118 !(params->flags & SNOR_F_4B_OPCODES)) 3119 return spi_nor_set_4byte_addr_mode(nor, true); 3120 3121 return 0; 3122 } 3123 3124 /** 3125 * spi_nor_soft_reset() - Perform a software reset 3126 * @nor: pointer to 'struct spi_nor' 3127 * 3128 * Performs a "Soft Reset and Enter Default Protocol Mode" sequence which resets 3129 * the device to its power-on-reset state. This is useful when the software has 3130 * made some changes to device (volatile) registers and needs to reset it before 3131 * shutting down, for example. 3132 * 3133 * Not every flash supports this sequence. The same set of opcodes might be used 3134 * for some other operation on a flash that does not support this. Support for 3135 * this sequence can be discovered via SFDP in the BFPT table. 3136 * 3137 * Return: 0 on success, -errno otherwise. 3138 */ 3139 static void spi_nor_soft_reset(struct spi_nor *nor) 3140 { 3141 struct spi_mem_op op; 3142 int ret; 3143 3144 op = (struct spi_mem_op)SPINOR_SRSTEN_OP; 3145 3146 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); 3147 3148 ret = spi_mem_exec_op(nor->spimem, &op); 3149 if (ret) { 3150 if (ret != -EOPNOTSUPP) 3151 dev_warn(nor->dev, "Software reset failed: %d\n", ret); 3152 return; 3153 } 3154 3155 op = (struct spi_mem_op)SPINOR_SRST_OP; 3156 3157 spi_nor_spimem_setup_op(nor, &op, nor->reg_proto); 3158 3159 ret = spi_mem_exec_op(nor->spimem, &op); 3160 if (ret) { 3161 dev_warn(nor->dev, "Software reset failed: %d\n", ret); 3162 return; 3163 } 3164 3165 /* 3166 * Software Reset is not instant, and the delay varies from flash to 3167 * flash. Looking at a few flashes, most range somewhere below 100 3168 * microseconds. So, sleep for a range of 200-400 us. 3169 */ 3170 usleep_range(SPI_NOR_SRST_SLEEP_MIN, SPI_NOR_SRST_SLEEP_MAX); 3171 } 3172 3173 /* mtd suspend handler */ 3174 static int spi_nor_suspend(struct mtd_info *mtd) 3175 { 3176 struct spi_nor *nor = mtd_to_spi_nor(mtd); 3177 int ret; 3178 3179 /* Disable octal DTR mode if we enabled it. */ 3180 ret = spi_nor_set_octal_dtr(nor, false); 3181 if (ret) 3182 dev_err(nor->dev, "suspend() failed\n"); 3183 3184 return ret; 3185 } 3186 3187 /* mtd resume handler */ 3188 static void spi_nor_resume(struct mtd_info *mtd) 3189 { 3190 struct spi_nor *nor = mtd_to_spi_nor(mtd); 3191 struct device *dev = nor->dev; 3192 int ret; 3193 3194 /* re-initialize the nor chip */ 3195 ret = spi_nor_init(nor); 3196 if (ret) 3197 dev_err(dev, "resume() failed\n"); 3198 } 3199 3200 static int spi_nor_get_device(struct mtd_info *mtd) 3201 { 3202 struct mtd_info *master = mtd_get_master(mtd); 3203 struct spi_nor *nor = mtd_to_spi_nor(master); 3204 struct device *dev; 3205 3206 if (nor->spimem) 3207 dev = nor->spimem->spi->controller->dev.parent; 3208 else 3209 dev = nor->dev; 3210 3211 if (!try_module_get(dev->driver->owner)) 3212 return -ENODEV; 3213 3214 return 0; 3215 } 3216 3217 static void spi_nor_put_device(struct mtd_info *mtd) 3218 { 3219 struct mtd_info *master = mtd_get_master(mtd); 3220 struct spi_nor *nor = mtd_to_spi_nor(master); 3221 struct device *dev; 3222 3223 if (nor->spimem) 3224 dev = nor->spimem->spi->controller->dev.parent; 3225 else 3226 dev = nor->dev; 3227 3228 module_put(dev->driver->owner); 3229 } 3230 3231 static void spi_nor_restore(struct spi_nor *nor) 3232 { 3233 struct spi_nor_flash_parameter *params = nor->params; 3234 int ret; 3235 3236 /* restore the addressing mode */ 3237 if (nor->addr_nbytes == 4 && !(params->flags & SNOR_F_4B_OPCODES) && 3238 params->flags & SNOR_F_BROKEN_RESET) { 3239 ret = spi_nor_set_4byte_addr_mode(nor, false); 3240 if (ret) 3241 /* 3242 * Do not stop the execution in the hope that the flash 3243 * will default to the 3-byte address mode after the 3244 * software reset. 3245 */ 3246 dev_err(nor->dev, "Failed to exit 4-byte address mode, err = %d\n", ret); 3247 } 3248 3249 if (params->flags & SNOR_F_SOFT_RESET) 3250 spi_nor_soft_reset(nor); 3251 } 3252 3253 static const struct flash_info *spi_nor_match_name(struct spi_nor *nor, 3254 const char *name) 3255 { 3256 unsigned int i, j; 3257 3258 for (i = 0; i < ARRAY_SIZE(manufacturers); i++) { 3259 for (j = 0; j < manufacturers[i]->nparts; j++) { 3260 if (manufacturers[i]->parts[j].name && 3261 !strcmp(name, manufacturers[i]->parts[j].name)) { 3262 nor->manufacturer = manufacturers[i]; 3263 return &manufacturers[i]->parts[j]; 3264 } 3265 } 3266 } 3267 3268 return NULL; 3269 } 3270 3271 static const struct flash_info *spi_nor_get_flash_info(struct spi_nor *nor, 3272 const char *name) 3273 { 3274 const struct flash_info *info = NULL; 3275 3276 if (name) 3277 info = spi_nor_match_name(nor, name); 3278 /* 3279 * Auto-detect if chip name wasn't specified or not found, or the chip 3280 * has an ID. If the chip supposedly has an ID, we also do an 3281 * auto-detection to compare it later. 3282 */ 3283 if (!info || info->id) { 3284 const struct flash_info *jinfo; 3285 3286 jinfo = spi_nor_detect(nor); 3287 if (IS_ERR(jinfo)) 3288 return jinfo; 3289 3290 /* 3291 * If caller has specified name of flash model that can normally 3292 * be detected using JEDEC, let's verify it. 3293 */ 3294 if (info && jinfo != info) 3295 dev_warn(nor->dev, "found %s, expected %s\n", 3296 jinfo->name, info->name); 3297 3298 /* If info was set before, JEDEC knows better. */ 3299 info = jinfo; 3300 } 3301 3302 return info; 3303 } 3304 3305 static u32 3306 spi_nor_get_region_erasesize(const struct spi_nor_erase_region *region, 3307 const struct spi_nor_erase_type *erase_type) 3308 { 3309 int i; 3310 3311 if (region->overlaid) 3312 return region->size; 3313 3314 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) { 3315 if (region->erase_mask & BIT(i)) 3316 return erase_type[i].size; 3317 } 3318 3319 return 0; 3320 } 3321 3322 static int spi_nor_set_mtd_eraseregions(struct spi_nor *nor) 3323 { 3324 const struct spi_nor_erase_map *map = &nor->params->erase_map; 3325 const struct spi_nor_erase_region *region = map->regions; 3326 struct mtd_erase_region_info *mtd_region; 3327 struct mtd_info *mtd = &nor->mtd; 3328 u32 erasesize, i; 3329 3330 mtd_region = devm_kzalloc_objs(nor->dev, *mtd_region, map->n_regions); 3331 if (!mtd_region) 3332 return -ENOMEM; 3333 3334 for (i = 0; i < map->n_regions; i++) { 3335 erasesize = spi_nor_get_region_erasesize(®ion[i], 3336 map->erase_type); 3337 if (!erasesize) 3338 return -EINVAL; 3339 3340 mtd_region[i].erasesize = erasesize; 3341 mtd_region[i].numblocks = div_u64(region[i].size, erasesize); 3342 mtd_region[i].offset = region[i].offset; 3343 } 3344 3345 mtd->numeraseregions = map->n_regions; 3346 mtd->eraseregions = mtd_region; 3347 3348 return 0; 3349 } 3350 3351 static int spi_nor_set_mtd_info(struct spi_nor *nor) 3352 { 3353 struct mtd_info *mtd = &nor->mtd; 3354 struct device *dev = nor->dev; 3355 3356 spi_nor_set_mtd_locking_ops(nor); 3357 spi_nor_set_mtd_otp_ops(nor); 3358 3359 mtd->dev.parent = dev; 3360 if (!mtd->name) 3361 mtd->name = dev_name(dev); 3362 mtd->type = MTD_NORFLASH; 3363 mtd->flags = MTD_CAP_NORFLASH; 3364 /* Unset BIT_WRITEABLE to enable JFFS2 write buffer for ECC'd NOR */ 3365 if (nor->params->flags & SNOR_F_ECC) 3366 mtd->flags &= ~MTD_BIT_WRITEABLE; 3367 if (nor->info->flags & SPI_NOR_NO_ERASE) 3368 mtd->flags |= MTD_NO_ERASE; 3369 else 3370 mtd->_erase = spi_nor_erase; 3371 mtd->writesize = nor->params->writesize; 3372 mtd->writebufsize = nor->params->page_size; 3373 mtd->size = nor->params->size; 3374 mtd->_read = spi_nor_read; 3375 /* Might be already set by some SST flashes. */ 3376 if (!mtd->_write) 3377 mtd->_write = spi_nor_write; 3378 mtd->_suspend = spi_nor_suspend; 3379 mtd->_resume = spi_nor_resume; 3380 mtd->_get_device = spi_nor_get_device; 3381 mtd->_put_device = spi_nor_put_device; 3382 3383 if (!spi_nor_has_uniform_erase(nor)) 3384 return spi_nor_set_mtd_eraseregions(nor); 3385 3386 return 0; 3387 } 3388 3389 static int spi_nor_hw_reset(struct spi_nor *nor) 3390 { 3391 struct gpio_desc *reset; 3392 3393 reset = devm_gpiod_get_optional(nor->dev, "reset", GPIOD_OUT_LOW); 3394 if (IS_ERR_OR_NULL(reset)) 3395 return PTR_ERR_OR_ZERO(reset); 3396 3397 /* 3398 * Experimental delay values by looking at different flash device 3399 * vendors datasheets. 3400 */ 3401 usleep_range(1, 5); 3402 gpiod_set_value_cansleep(reset, 1); 3403 usleep_range(100, 150); 3404 gpiod_set_value_cansleep(reset, 0); 3405 usleep_range(1000, 1200); 3406 3407 return 0; 3408 } 3409 3410 int spi_nor_scan(struct spi_nor *nor, const char *name, 3411 const struct spi_nor_hwcaps *hwcaps) 3412 { 3413 const struct flash_info *info; 3414 struct device *dev = nor->dev; 3415 int ret; 3416 3417 ret = spi_nor_check(nor); 3418 if (ret) 3419 return ret; 3420 3421 /* Reset SPI protocol for all commands. */ 3422 nor->reg_proto = SNOR_PROTO_1_1_1; 3423 nor->read_proto = SNOR_PROTO_1_1_1; 3424 nor->write_proto = SNOR_PROTO_1_1_1; 3425 3426 /* 3427 * We need the bounce buffer early to read/write registers when going 3428 * through the spi-mem layer (buffers have to be DMA-able). 3429 * For spi-mem drivers, we'll reallocate a new buffer if 3430 * nor->params->page_size turns out to be greater than PAGE_SIZE (which 3431 * shouldn't happen before long since NOR pages are usually less 3432 * than 1KB) after spi_nor_scan() returns. 3433 */ 3434 nor->bouncebuf_size = PAGE_SIZE; 3435 nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size, 3436 GFP_KERNEL); 3437 if (!nor->bouncebuf) 3438 return -ENOMEM; 3439 3440 ret = spi_nor_hw_reset(nor); 3441 if (ret) 3442 return ret; 3443 3444 info = spi_nor_get_flash_info(nor, name); 3445 if (IS_ERR(info)) 3446 return PTR_ERR(info); 3447 3448 nor->info = info; 3449 nor->partname = info->name; 3450 3451 mutex_init(&nor->lock); 3452 3453 /* Init flash parameters based on flash_info struct and SFDP */ 3454 ret = spi_nor_init_params(nor); 3455 if (ret) 3456 return ret; 3457 3458 if (spi_nor_use_parallel_locking(nor)) 3459 init_waitqueue_head(&nor->rww.wait); 3460 3461 /* 3462 * Configure the SPI memory: 3463 * - select op codes for (Fast) Read, Page Program and Sector Erase. 3464 * - set the number of dummy cycles (mode cycles + wait states). 3465 * - set the SPI protocols for register and memory accesses. 3466 * - set the number of address bytes. 3467 */ 3468 ret = spi_nor_setup(nor, hwcaps); 3469 if (ret) 3470 return ret; 3471 3472 /* Send all the required SPI flash commands to initialize device */ 3473 ret = spi_nor_init(nor); 3474 if (ret) 3475 return ret; 3476 3477 /* No mtd_info fields should be used up to this point. */ 3478 ret = spi_nor_set_mtd_info(nor); 3479 if (ret) 3480 return ret; 3481 3482 dev_dbg(dev, "Manufacturer and device ID: %*phN\n", 3483 SPI_NOR_MAX_ID_LEN, nor->id); 3484 3485 return 0; 3486 } 3487 EXPORT_SYMBOL_GPL(spi_nor_scan); 3488 3489 static int spi_nor_create_read_dirmap(struct spi_nor *nor) 3490 { 3491 struct spi_mem_dirmap_info info = { 3492 .op_tmpl = &info.primary_op_tmpl, 3493 .primary_op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0), 3494 SPI_MEM_OP_ADDR(nor->addr_nbytes, 0, 0), 3495 SPI_MEM_OP_DUMMY(nor->read_dummy, 0), 3496 SPI_MEM_OP_DATA_IN(0, NULL, 0)), 3497 .offset = 0, 3498 .length = nor->params->size, 3499 }; 3500 struct spi_mem_op *op = info.op_tmpl; 3501 3502 spi_nor_spimem_setup_op(nor, op, nor->read_proto); 3503 3504 /* convert the dummy cycles to the number of bytes */ 3505 op->dummy.nbytes = (nor->read_dummy * op->dummy.buswidth) / 8; 3506 if (spi_nor_protocol_is_dtr(nor->read_proto)) 3507 op->dummy.nbytes *= 2; 3508 3509 /* 3510 * Since spi_nor_spimem_setup_op() only sets buswidth when the number 3511 * of data bytes is non-zero, the data buswidth won't be set here. So, 3512 * do it explicitly. 3513 */ 3514 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto); 3515 3516 nor->dirmap.rdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem, 3517 &info); 3518 return PTR_ERR_OR_ZERO(nor->dirmap.rdesc); 3519 } 3520 3521 static int spi_nor_create_write_dirmap(struct spi_nor *nor) 3522 { 3523 struct spi_mem_dirmap_info info = { 3524 .op_tmpl = &info.primary_op_tmpl, 3525 .primary_op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0), 3526 SPI_MEM_OP_ADDR(nor->addr_nbytes, 0, 0), 3527 SPI_MEM_OP_NO_DUMMY, 3528 SPI_MEM_OP_DATA_OUT(0, NULL, 0)), 3529 .offset = 0, 3530 .length = nor->params->size, 3531 }; 3532 struct spi_mem_op *op = info.op_tmpl; 3533 3534 if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second) 3535 op->addr.nbytes = 0; 3536 3537 spi_nor_spimem_setup_op(nor, op, nor->write_proto); 3538 3539 /* 3540 * Since spi_nor_spimem_setup_op() only sets buswidth when the number 3541 * of data bytes is non-zero, the data buswidth won't be set here. So, 3542 * do it explicitly. 3543 */ 3544 op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto); 3545 3546 nor->dirmap.wdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem, 3547 &info); 3548 return PTR_ERR_OR_ZERO(nor->dirmap.wdesc); 3549 } 3550 3551 static int spi_nor_probe(struct spi_mem *spimem) 3552 { 3553 struct spi_device *spi = spimem->spi; 3554 struct device *dev = &spi->dev; 3555 struct flash_platform_data *data = dev_get_platdata(dev); 3556 struct spi_nor *nor; 3557 /* 3558 * Enable all caps by default. The core will mask them after 3559 * checking what's really supported using spi_mem_supports_op(). 3560 */ 3561 const struct spi_nor_hwcaps hwcaps = { .mask = SNOR_HWCAPS_ALL }; 3562 char *flash_name; 3563 int ret; 3564 3565 ret = devm_regulator_get_enable(dev, "vcc"); 3566 if (ret) 3567 return ret; 3568 3569 nor = devm_kzalloc_obj(dev, *nor); 3570 if (!nor) 3571 return -ENOMEM; 3572 3573 nor->spimem = spimem; 3574 nor->dev = dev; 3575 spi_nor_set_flash_node(nor, dev->of_node); 3576 3577 spi_mem_set_drvdata(spimem, nor); 3578 3579 if (data && data->name) 3580 nor->mtd.name = data->name; 3581 3582 if (!nor->mtd.name) 3583 nor->mtd.name = spi_mem_get_name(spimem); 3584 3585 /* 3586 * For some (historical?) reason many platforms provide two different 3587 * names in flash_platform_data: "name" and "type". Quite often name is 3588 * set to "m25p80" and then "type" provides a real chip name. 3589 * If that's the case, respect "type" and ignore a "name". 3590 */ 3591 if (data && data->type) 3592 flash_name = data->type; 3593 else if (!strcmp(spi->modalias, "spi-nor")) 3594 flash_name = NULL; /* auto-detect */ 3595 else 3596 flash_name = spi->modalias; 3597 3598 ret = spi_nor_scan(nor, flash_name, &hwcaps); 3599 if (ret) 3600 return ret; 3601 3602 spi_nor_debugfs_register(nor); 3603 3604 /* 3605 * None of the existing parts have > 512B pages, but let's play safe 3606 * and add this logic so that if anyone ever adds support for such 3607 * a NOR we don't end up with buffer overflows. 3608 */ 3609 if (nor->params->page_size > PAGE_SIZE) { 3610 nor->bouncebuf_size = nor->params->page_size; 3611 devm_kfree(dev, nor->bouncebuf); 3612 nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size, 3613 GFP_KERNEL); 3614 if (!nor->bouncebuf) 3615 return -ENOMEM; 3616 } 3617 3618 ret = spi_nor_create_read_dirmap(nor); 3619 if (ret) 3620 return ret; 3621 3622 ret = spi_nor_create_write_dirmap(nor); 3623 if (ret) 3624 return ret; 3625 3626 return mtd_device_register(&nor->mtd, data ? data->parts : NULL, 3627 data ? data->nr_parts : 0); 3628 } 3629 3630 static int spi_nor_remove(struct spi_mem *spimem) 3631 { 3632 struct spi_nor *nor = spi_mem_get_drvdata(spimem); 3633 3634 spi_nor_restore(nor); 3635 3636 /* Clean up MTD stuff. */ 3637 return mtd_device_unregister(&nor->mtd); 3638 } 3639 3640 static void spi_nor_shutdown(struct spi_mem *spimem) 3641 { 3642 struct spi_nor *nor = spi_mem_get_drvdata(spimem); 3643 3644 spi_nor_restore(nor); 3645 } 3646 3647 /* 3648 * Do NOT add to this array without reading the following: 3649 * 3650 * Historically, many flash devices are bound to this driver by their name. But 3651 * since most of these flash are compatible to some extent, and their 3652 * differences can often be differentiated by the JEDEC read-ID command, we 3653 * encourage new users to add support to the spi-nor library, and simply bind 3654 * against a generic string here (e.g., "jedec,spi-nor"). 3655 * 3656 * Many flash names are kept here in this list to keep them available 3657 * as module aliases for existing platforms. 3658 */ 3659 static const struct spi_device_id spi_nor_dev_ids[] = { 3660 /* 3661 * Allow non-DT platform devices to bind to the "spi-nor" modalias, and 3662 * hack around the fact that the SPI core does not provide uevent 3663 * matching for .of_match_table 3664 */ 3665 {"spi-nor"}, 3666 3667 /* 3668 * Entries not used in DTs that should be safe to drop after replacing 3669 * them with "spi-nor" in platform data. 3670 */ 3671 {"s25sl064a"}, {"w25x16"}, {"m25p10"}, {"m25px64"}, 3672 3673 /* 3674 * Entries that were used in DTs without "jedec,spi-nor" fallback and 3675 * should be kept for backward compatibility. 3676 */ 3677 {"at25df321a"}, {"at25df641"}, {"at26df081a"}, 3678 {"mx25l4005a"}, {"mx25l1606e"}, {"mx25l6405d"}, {"mx25l12805d"}, 3679 {"mx25l25635e"},{"mx66l51235l"}, 3680 {"n25q064"}, {"n25q128a11"}, {"n25q128a13"}, {"n25q512a"}, 3681 {"s25fl256s1"}, {"s25fl512s"}, {"s25sl12801"}, {"s25fl008k"}, 3682 {"s25fl064k"}, 3683 {"sst25vf040b"},{"sst25vf016b"},{"sst25vf032b"},{"sst25wf040"}, 3684 {"m25p40"}, {"m25p80"}, {"m25p16"}, {"m25p32"}, 3685 {"m25p64"}, {"m25p128"}, 3686 {"w25x80"}, {"w25x32"}, {"w25q32"}, {"w25q32dw"}, 3687 {"w25q80bl"}, {"w25q128"}, {"w25q256"}, 3688 3689 /* Flashes that can't be detected using JEDEC */ 3690 {"m25p05-nonjedec"}, {"m25p10-nonjedec"}, {"m25p20-nonjedec"}, 3691 {"m25p40-nonjedec"}, {"m25p80-nonjedec"}, {"m25p16-nonjedec"}, 3692 {"m25p32-nonjedec"}, {"m25p64-nonjedec"}, {"m25p128-nonjedec"}, 3693 3694 /* Everspin MRAMs (non-JEDEC) */ 3695 { "mr25h128" }, /* 128 Kib, 40 MHz */ 3696 { "mr25h256" }, /* 256 Kib, 40 MHz */ 3697 { "mr25h10" }, /* 1 Mib, 40 MHz */ 3698 { "mr25h40" }, /* 4 Mib, 40 MHz */ 3699 3700 { }, 3701 }; 3702 MODULE_DEVICE_TABLE(spi, spi_nor_dev_ids); 3703 3704 static const struct of_device_id spi_nor_of_table[] = { 3705 /* 3706 * Generic compatibility for SPI NOR that can be identified by the 3707 * JEDEC READ ID opcode (0x9F). Use this, if possible. 3708 */ 3709 { .compatible = "jedec,spi-nor" }, 3710 { /* sentinel */ }, 3711 }; 3712 MODULE_DEVICE_TABLE(of, spi_nor_of_table); 3713 3714 /* 3715 * REVISIT: many of these chips have deep power-down modes, which 3716 * should clearly be entered on suspend() to minimize power use. 3717 * And also when they're otherwise idle... 3718 */ 3719 static struct spi_mem_driver spi_nor_driver = { 3720 .spidrv = { 3721 .driver = { 3722 .name = "spi-nor", 3723 .of_match_table = spi_nor_of_table, 3724 .dev_groups = spi_nor_sysfs_groups, 3725 }, 3726 .id_table = spi_nor_dev_ids, 3727 }, 3728 .probe = spi_nor_probe, 3729 .remove = spi_nor_remove, 3730 .shutdown = spi_nor_shutdown, 3731 }; 3732 3733 static int __init spi_nor_module_init(void) 3734 { 3735 return spi_mem_driver_register(&spi_nor_driver); 3736 } 3737 module_init(spi_nor_module_init); 3738 3739 static void __exit spi_nor_module_exit(void) 3740 { 3741 spi_mem_driver_unregister(&spi_nor_driver); 3742 spi_nor_debugfs_shutdown(); 3743 } 3744 module_exit(spi_nor_module_exit); 3745 3746 MODULE_LICENSE("GPL v2"); 3747 MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>"); 3748 MODULE_AUTHOR("Mike Lavender"); 3749 MODULE_DESCRIPTION("framework for SPI NOR"); 3750