1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2016-2017 Micron Technology, Inc. 4 * 5 * Authors: 6 * Peter Pan <peterpandong@micron.com> 7 * Boris Brezillon <boris.brezillon@bootlin.com> 8 */ 9 10 #define pr_fmt(fmt) "spi-nand: " fmt 11 12 #include <linux/device.h> 13 #include <linux/jiffies.h> 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/mtd/spinand.h> 17 #include <linux/of.h> 18 #include <linux/slab.h> 19 #include <linux/string.h> 20 #include <linux/spi/spi.h> 21 #include <linux/spi/spi-mem.h> 22 23 static struct spi_mem_op 24 spinand_fill_reset_op(struct spinand_device *spinand) 25 { 26 return spinand->op_templates->reset; 27 } 28 29 static struct spi_mem_op 30 spinand_fill_readid_op(struct spinand_device *spinand, 31 u8 naddr, u8 ndummy, void *buf, unsigned int len) 32 { 33 struct spi_mem_op op = spinand->op_templates->readid; 34 35 op.addr.nbytes = naddr; 36 op.dummy.nbytes = ndummy; 37 op.data.buf.in = buf; 38 op.data.nbytes = len; 39 40 return op; 41 } 42 43 struct spi_mem_op 44 spinand_fill_wr_en_op(struct spinand_device *spinand) 45 { 46 return spinand->op_templates->wr_en; 47 } 48 49 static __maybe_unused struct spi_mem_op 50 spinand_fill_wr_dis_op(struct spinand_device *spinand) 51 { 52 return spinand->op_templates->wr_dis; 53 } 54 55 struct spi_mem_op 56 spinand_fill_set_feature_op(struct spinand_device *spinand, u64 reg, const void *valptr) 57 { 58 struct spi_mem_op op = spinand->op_templates->set_feature; 59 60 if (op.cmd.dtr && op.cmd.buswidth == 8) 61 reg |= reg << 8; 62 63 op.addr.val = reg; 64 op.data.buf.out = valptr; 65 66 return op; 67 } 68 69 struct spi_mem_op 70 spinand_fill_get_feature_op(struct spinand_device *spinand, u64 reg, void *valptr) 71 { 72 struct spi_mem_op op = spinand->op_templates->get_feature; 73 74 if (op.cmd.dtr && op.cmd.buswidth == 8) 75 reg |= reg << 8; 76 77 op.addr.val = reg; 78 op.data.buf.in = valptr; 79 80 return op; 81 } 82 83 static struct spi_mem_op 84 spinand_fill_blk_erase_op(struct spinand_device *spinand, u64 addr) 85 { 86 struct spi_mem_op op = spinand->op_templates->blk_erase; 87 88 op.addr.val = addr; 89 90 return op; 91 } 92 93 static struct spi_mem_op 94 spinand_fill_page_read_op(struct spinand_device *spinand, u64 addr) 95 { 96 struct spi_mem_op op = spinand->op_templates->page_read; 97 98 op.addr.val = addr; 99 100 return op; 101 } 102 103 struct spi_mem_op 104 spinand_fill_prog_exec_op(struct spinand_device *spinand, u64 addr) 105 { 106 struct spi_mem_op op = spinand->op_templates->prog_exec; 107 108 op.addr.val = addr; 109 110 return op; 111 } 112 113 int spinand_read_reg_op(struct spinand_device *spinand, u8 reg, u8 *val) 114 { 115 struct spi_mem_op op = SPINAND_OP(spinand, get_feature, 116 reg, spinand->scratchbuf); 117 int ret; 118 119 ret = spi_mem_exec_op(spinand->spimem, &op); 120 if (ret) 121 return ret; 122 123 *val = *spinand->scratchbuf; 124 return 0; 125 } 126 127 int spinand_write_reg_op(struct spinand_device *spinand, u8 reg, u8 val) 128 { 129 struct spi_mem_op op = SPINAND_OP(spinand, set_feature, 130 reg, spinand->scratchbuf); 131 132 *spinand->scratchbuf = val; 133 return spi_mem_exec_op(spinand->spimem, &op); 134 } 135 136 static int spinand_read_status(struct spinand_device *spinand, u8 *status) 137 { 138 return spinand_read_reg_op(spinand, REG_STATUS, status); 139 } 140 141 static int spinand_get_cfg(struct spinand_device *spinand, u8 *cfg) 142 { 143 struct nand_device *nand = spinand_to_nand(spinand); 144 145 if (WARN_ON(spinand->cur_target < 0 || 146 spinand->cur_target >= nand->memorg.ntargets)) 147 return -EINVAL; 148 149 *cfg = spinand->cfg_cache[spinand->cur_target]; 150 return 0; 151 } 152 153 static int spinand_set_cfg(struct spinand_device *spinand, u8 cfg) 154 { 155 struct nand_device *nand = spinand_to_nand(spinand); 156 int ret; 157 158 if (WARN_ON(spinand->cur_target < 0 || 159 spinand->cur_target >= nand->memorg.ntargets)) 160 return -EINVAL; 161 162 if (spinand->cfg_cache[spinand->cur_target] == cfg) 163 return 0; 164 165 ret = spinand_write_reg_op(spinand, REG_CFG, cfg); 166 if (ret) 167 return ret; 168 169 spinand->cfg_cache[spinand->cur_target] = cfg; 170 return 0; 171 } 172 173 /** 174 * spinand_upd_cfg() - Update the configuration register 175 * @spinand: the spinand device 176 * @mask: the mask encoding the bits to update in the config reg 177 * @val: the new value to apply 178 * 179 * Update the configuration register. 180 * 181 * Return: 0 on success, a negative error code otherwise. 182 */ 183 int spinand_upd_cfg(struct spinand_device *spinand, u8 mask, u8 val) 184 { 185 int ret; 186 u8 cfg; 187 188 ret = spinand_get_cfg(spinand, &cfg); 189 if (ret) 190 return ret; 191 192 cfg &= ~mask; 193 cfg |= val; 194 195 return spinand_set_cfg(spinand, cfg); 196 } 197 198 /** 199 * spinand_select_target() - Select a specific NAND target/die 200 * @spinand: the spinand device 201 * @target: the target/die to select 202 * 203 * Select a new target/die. If chip only has one die, this function is a NOOP. 204 * 205 * Return: 0 on success, a negative error code otherwise. 206 */ 207 int spinand_select_target(struct spinand_device *spinand, unsigned int target) 208 { 209 struct nand_device *nand = spinand_to_nand(spinand); 210 int ret; 211 212 if (WARN_ON(target >= nand->memorg.ntargets)) 213 return -EINVAL; 214 215 if (spinand->cur_target == target) 216 return 0; 217 218 if (nand->memorg.ntargets == 1) { 219 spinand->cur_target = target; 220 return 0; 221 } 222 223 ret = spinand->select_target(spinand, target); 224 if (ret) 225 return ret; 226 227 spinand->cur_target = target; 228 return 0; 229 } 230 231 static int spinand_read_cfg(struct spinand_device *spinand) 232 { 233 struct nand_device *nand = spinand_to_nand(spinand); 234 unsigned int target; 235 int ret; 236 237 for (target = 0; target < nand->memorg.ntargets; target++) { 238 ret = spinand_select_target(spinand, target); 239 if (ret) 240 return ret; 241 242 /* 243 * We use spinand_read_reg_op() instead of spinand_get_cfg() 244 * here to bypass the config cache. 245 */ 246 ret = spinand_read_reg_op(spinand, REG_CFG, 247 &spinand->cfg_cache[target]); 248 if (ret) 249 return ret; 250 } 251 252 return 0; 253 } 254 255 static int spinand_init_cfg_cache(struct spinand_device *spinand) 256 { 257 struct nand_device *nand = spinand_to_nand(spinand); 258 struct device *dev = &spinand->spimem->spi->dev; 259 260 spinand->cfg_cache = devm_kcalloc(dev, 261 nand->memorg.ntargets, 262 sizeof(*spinand->cfg_cache), 263 GFP_KERNEL); 264 if (!spinand->cfg_cache) 265 return -ENOMEM; 266 267 return 0; 268 } 269 270 static int spinand_init_quad_enable(struct spinand_device *spinand, 271 bool enable) 272 { 273 return spinand_upd_cfg(spinand, CFG_QUAD_ENABLE, 274 enable ? CFG_QUAD_ENABLE : 0); 275 } 276 277 static int spinand_ecc_enable(struct spinand_device *spinand, 278 bool enable) 279 { 280 return spinand_upd_cfg(spinand, CFG_ECC_ENABLE, 281 enable ? CFG_ECC_ENABLE : 0); 282 } 283 284 static int spinand_cont_read_enable(struct spinand_device *spinand, 285 bool enable) 286 { 287 return spinand->set_cont_read(spinand, enable); 288 } 289 290 static int spinand_check_ecc_status(struct spinand_device *spinand, u8 status) 291 { 292 struct nand_device *nand = spinand_to_nand(spinand); 293 294 if (spinand->eccinfo.get_status) 295 return spinand->eccinfo.get_status(spinand, status); 296 297 switch (status & STATUS_ECC_MASK) { 298 case STATUS_ECC_NO_BITFLIPS: 299 return 0; 300 301 case STATUS_ECC_HAS_BITFLIPS: 302 /* 303 * We have no way to know exactly how many bitflips have been 304 * fixed, so let's return the maximum possible value so that 305 * wear-leveling layers move the data immediately. 306 */ 307 return nanddev_get_ecc_conf(nand)->strength; 308 309 case STATUS_ECC_UNCOR_ERROR: 310 return -EBADMSG; 311 312 default: 313 break; 314 } 315 316 return -EINVAL; 317 } 318 319 static int spinand_noecc_ooblayout_ecc(struct mtd_info *mtd, int section, 320 struct mtd_oob_region *region) 321 { 322 return -ERANGE; 323 } 324 325 static int spinand_noecc_ooblayout_free(struct mtd_info *mtd, int section, 326 struct mtd_oob_region *region) 327 { 328 if (section) 329 return -ERANGE; 330 331 /* Reserve 2 bytes for the BBM. */ 332 region->offset = 2; 333 region->length = 62; 334 335 return 0; 336 } 337 338 static const struct mtd_ooblayout_ops spinand_noecc_ooblayout = { 339 .ecc = spinand_noecc_ooblayout_ecc, 340 .free = spinand_noecc_ooblayout_free, 341 }; 342 343 static int spinand_ondie_ecc_init_ctx(struct nand_device *nand) 344 { 345 struct spinand_device *spinand = nand_to_spinand(nand); 346 struct mtd_info *mtd = nanddev_to_mtd(nand); 347 struct spinand_ondie_ecc_conf *engine_conf; 348 349 nand->ecc.ctx.conf.engine_type = NAND_ECC_ENGINE_TYPE_ON_DIE; 350 nand->ecc.ctx.conf.step_size = nand->ecc.requirements.step_size; 351 nand->ecc.ctx.conf.strength = nand->ecc.requirements.strength; 352 353 engine_conf = kzalloc_obj(*engine_conf); 354 if (!engine_conf) 355 return -ENOMEM; 356 357 nand->ecc.ctx.priv = engine_conf; 358 359 if (spinand->eccinfo.ooblayout) 360 mtd_set_ooblayout(mtd, spinand->eccinfo.ooblayout); 361 else 362 mtd_set_ooblayout(mtd, &spinand_noecc_ooblayout); 363 364 return 0; 365 } 366 367 static void spinand_ondie_ecc_cleanup_ctx(struct nand_device *nand) 368 { 369 kfree(nand->ecc.ctx.priv); 370 } 371 372 static int spinand_ondie_ecc_prepare_io_req(struct nand_device *nand, 373 struct nand_page_io_req *req) 374 { 375 struct spinand_device *spinand = nand_to_spinand(nand); 376 bool enable = (req->mode != MTD_OPS_RAW); 377 378 if (!enable && spinand->flags & SPINAND_NO_RAW_ACCESS) 379 return -EOPNOTSUPP; 380 381 memset(spinand->oobbuf, 0xff, nanddev_per_page_oobsize(nand)); 382 383 /* Only enable or disable the engine */ 384 return spinand_ecc_enable(spinand, enable); 385 } 386 387 static int spinand_ondie_ecc_finish_io_req(struct nand_device *nand, 388 struct nand_page_io_req *req) 389 { 390 struct spinand_ondie_ecc_conf *engine_conf = nand->ecc.ctx.priv; 391 struct spinand_device *spinand = nand_to_spinand(nand); 392 struct mtd_info *mtd = spinand_to_mtd(spinand); 393 int ret; 394 395 if (req->mode == MTD_OPS_RAW) 396 return 0; 397 398 /* Nothing to do when finishing a page write */ 399 if (req->type == NAND_PAGE_WRITE) 400 return 0; 401 402 /* Finish a page read: check the status, report errors/bitflips */ 403 ret = spinand_check_ecc_status(spinand, engine_conf->status); 404 if (ret == -EBADMSG) { 405 mtd->ecc_stats.failed++; 406 } else if (ret > 0) { 407 unsigned int pages; 408 409 /* 410 * Continuous reads don't allow us to get the detail, 411 * so we may exagerate the actual number of corrected bitflips. 412 */ 413 if (!req->continuous) 414 pages = 1; 415 else 416 pages = req->datalen / nanddev_page_size(nand); 417 418 mtd->ecc_stats.corrected += ret * pages; 419 } 420 421 return ret; 422 } 423 424 static const struct nand_ecc_engine_ops spinand_ondie_ecc_engine_ops = { 425 .init_ctx = spinand_ondie_ecc_init_ctx, 426 .cleanup_ctx = spinand_ondie_ecc_cleanup_ctx, 427 .prepare_io_req = spinand_ondie_ecc_prepare_io_req, 428 .finish_io_req = spinand_ondie_ecc_finish_io_req, 429 }; 430 431 static struct nand_ecc_engine spinand_ondie_ecc_engine = { 432 .ops = &spinand_ondie_ecc_engine_ops, 433 }; 434 435 static void spinand_ondie_ecc_save_status(struct nand_device *nand, u8 status) 436 { 437 struct spinand_ondie_ecc_conf *engine_conf = nand->ecc.ctx.priv; 438 439 if (nand->ecc.ctx.conf.engine_type == NAND_ECC_ENGINE_TYPE_ON_DIE && 440 engine_conf) 441 engine_conf->status = status; 442 } 443 444 int spinand_write_enable_op(struct spinand_device *spinand) 445 { 446 struct spi_mem_op op = SPINAND_OP(spinand, wr_en); 447 448 return spi_mem_exec_op(spinand->spimem, &op); 449 } 450 451 static int spinand_load_page_op(struct spinand_device *spinand, 452 const struct nand_page_io_req *req) 453 { 454 struct nand_device *nand = spinand_to_nand(spinand); 455 unsigned int row = nanddev_pos_to_row(nand, &req->pos); 456 struct spi_mem_op op = SPINAND_OP(spinand, page_read, row); 457 458 return spi_mem_exec_op(spinand->spimem, &op); 459 } 460 461 static int spinand_read_from_cache_op(struct spinand_device *spinand, 462 const struct nand_page_io_req *req) 463 { 464 struct nand_device *nand = spinand_to_nand(spinand); 465 struct mtd_info *mtd = spinand_to_mtd(spinand); 466 struct spi_mem_dirmap_desc *rdesc; 467 unsigned int nbytes = 0; 468 void *buf = NULL; 469 u16 column = 0; 470 ssize_t ret; 471 472 if (req->datalen) { 473 buf = spinand->databuf; 474 if (!req->continuous) 475 nbytes = nanddev_page_size(nand); 476 else 477 nbytes = round_up(req->dataoffs + req->datalen, 478 nanddev_page_size(nand)); 479 column = 0; 480 } 481 482 if (req->ooblen) { 483 nbytes += nanddev_per_page_oobsize(nand); 484 if (!buf) { 485 buf = spinand->oobbuf; 486 column = nanddev_page_size(nand); 487 } 488 } 489 490 if (req->mode == MTD_OPS_RAW) 491 rdesc = spinand->dirmaps[req->pos.plane].rdesc; 492 else 493 rdesc = spinand->dirmaps[req->pos.plane].rdesc_ecc; 494 495 if (spinand->flags & SPINAND_HAS_READ_PLANE_SELECT_BIT) 496 column |= req->pos.plane << fls(nanddev_page_size(nand)); 497 498 while (nbytes) { 499 ret = spi_mem_dirmap_read(rdesc, column, nbytes, buf); 500 if (ret < 0) 501 return ret; 502 503 if (!ret || ret > nbytes) 504 return -EIO; 505 506 nbytes -= ret; 507 column += ret; 508 buf += ret; 509 510 /* 511 * Dirmap accesses are allowed to toggle the CS. 512 * Toggling the CS during a continuous read is forbidden. 513 */ 514 if (nbytes && req->continuous) { 515 /* 516 * Spi controller with broken support of continuous 517 * reading was detected. Disable future use of 518 * continuous reading and return -EAGAIN to retry 519 * reading within regular mode. 520 */ 521 spinand->cont_read_possible = false; 522 return -EAGAIN; 523 } 524 } 525 526 if (req->datalen) 527 memcpy(req->databuf.in, spinand->databuf + req->dataoffs, 528 req->datalen); 529 530 if (req->ooblen) { 531 if (req->mode == MTD_OPS_AUTO_OOB) 532 mtd_ooblayout_get_databytes(mtd, req->oobbuf.in, 533 spinand->oobbuf, 534 req->ooboffs, 535 req->ooblen); 536 else 537 memcpy(req->oobbuf.in, spinand->oobbuf + req->ooboffs, 538 req->ooblen); 539 } 540 541 return 0; 542 } 543 544 static int spinand_write_to_cache_op(struct spinand_device *spinand, 545 const struct nand_page_io_req *req) 546 { 547 struct nand_device *nand = spinand_to_nand(spinand); 548 struct mtd_info *mtd = spinand_to_mtd(spinand); 549 struct spi_mem_dirmap_desc *wdesc; 550 unsigned int nbytes, column = 0; 551 void *buf = spinand->databuf; 552 ssize_t ret; 553 554 /* 555 * Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset 556 * the cache content to 0xFF (depends on vendor implementation), so we 557 * must fill the page cache entirely even if we only want to program 558 * the data portion of the page, otherwise we might corrupt the BBM or 559 * user data previously programmed in OOB area. 560 * 561 * Only reset the data buffer manually, the OOB buffer is prepared by 562 * ECC engines ->prepare_io_req() callback. 563 */ 564 nbytes = nanddev_page_size(nand) + nanddev_per_page_oobsize(nand); 565 memset(spinand->databuf, 0xff, nanddev_page_size(nand)); 566 567 if (req->datalen) 568 memcpy(spinand->databuf + req->dataoffs, req->databuf.out, 569 req->datalen); 570 571 if (req->ooblen) { 572 if (req->mode == MTD_OPS_AUTO_OOB) 573 mtd_ooblayout_set_databytes(mtd, req->oobbuf.out, 574 spinand->oobbuf, 575 req->ooboffs, 576 req->ooblen); 577 else 578 memcpy(spinand->oobbuf + req->ooboffs, req->oobbuf.out, 579 req->ooblen); 580 } 581 582 if (req->mode == MTD_OPS_RAW) 583 wdesc = spinand->dirmaps[req->pos.plane].wdesc; 584 else 585 wdesc = spinand->dirmaps[req->pos.plane].wdesc_ecc; 586 587 if (spinand->flags & SPINAND_HAS_PROG_PLANE_SELECT_BIT) 588 column |= req->pos.plane << fls(nanddev_page_size(nand)); 589 590 while (nbytes) { 591 ret = spi_mem_dirmap_write(wdesc, column, nbytes, buf); 592 if (ret < 0) 593 return ret; 594 595 if (!ret || ret > nbytes) 596 return -EIO; 597 598 nbytes -= ret; 599 column += ret; 600 buf += ret; 601 } 602 603 return 0; 604 } 605 606 static int spinand_program_op(struct spinand_device *spinand, 607 const struct nand_page_io_req *req) 608 { 609 struct nand_device *nand = spinand_to_nand(spinand); 610 unsigned int row = nanddev_pos_to_row(nand, &req->pos); 611 struct spi_mem_op op = SPINAND_OP(spinand, prog_exec, row); 612 613 return spi_mem_exec_op(spinand->spimem, &op); 614 } 615 616 static int spinand_erase_op(struct spinand_device *spinand, 617 const struct nand_pos *pos) 618 { 619 struct nand_device *nand = spinand_to_nand(spinand); 620 unsigned int row = nanddev_pos_to_row(nand, pos); 621 struct spi_mem_op op = SPINAND_OP(spinand, blk_erase, row); 622 623 return spi_mem_exec_op(spinand->spimem, &op); 624 } 625 626 /** 627 * spinand_wait() - Poll memory device status 628 * @spinand: the spinand device 629 * @initial_delay_us: delay in us before starting to poll 630 * @poll_delay_us: time to sleep between reads in us 631 * @s: the pointer to variable to store the value of REG_STATUS 632 * 633 * This function polls a status register (REG_STATUS) and returns when 634 * the STATUS_READY bit is 0 or when the timeout has expired. 635 * 636 * Return: 0 on success, a negative error code otherwise. 637 */ 638 int spinand_wait(struct spinand_device *spinand, unsigned long initial_delay_us, 639 unsigned long poll_delay_us, u8 *s) 640 { 641 struct spi_mem_op op = SPINAND_OP(spinand, get_feature, 642 REG_STATUS, spinand->scratchbuf); 643 u8 status; 644 int ret; 645 646 ret = spi_mem_poll_status(spinand->spimem, &op, STATUS_BUSY, 0, 647 initial_delay_us, 648 poll_delay_us, 649 SPINAND_WAITRDY_TIMEOUT_MS); 650 if (ret) 651 return ret; 652 653 status = *spinand->scratchbuf; 654 if (!(status & STATUS_BUSY)) 655 goto out; 656 657 /* 658 * Extra read, just in case the STATUS_READY bit has changed 659 * since our last check 660 */ 661 ret = spinand_read_status(spinand, &status); 662 if (ret) 663 return ret; 664 665 out: 666 if (s) 667 *s = status; 668 669 return status & STATUS_BUSY ? -ETIMEDOUT : 0; 670 } 671 672 static int spinand_read_id_op(struct spinand_device *spinand, u8 naddr, 673 u8 ndummy, u8 *buf) 674 { 675 struct spi_mem_op op = SPINAND_OP(spinand, readid, 676 naddr, ndummy, spinand->scratchbuf, SPINAND_MAX_ID_LEN); 677 int ret; 678 679 ret = spi_mem_exec_op(spinand->spimem, &op); 680 if (!ret) 681 memcpy(buf, spinand->scratchbuf, SPINAND_MAX_ID_LEN); 682 683 return ret; 684 } 685 686 static int spinand_reset_op(struct spinand_device *spinand) 687 { 688 struct spi_mem_op op = SPINAND_OP(spinand, reset); 689 int ret; 690 691 ret = spi_mem_exec_op(spinand->spimem, &op); 692 if (ret) 693 return ret; 694 695 return spinand_wait(spinand, 696 SPINAND_RESET_INITIAL_DELAY_US, 697 SPINAND_RESET_POLL_DELAY_US, 698 NULL); 699 } 700 701 static int spinand_lock_block(struct spinand_device *spinand, u8 lock) 702 { 703 return spinand_write_reg_op(spinand, REG_BLOCK_LOCK, lock); 704 } 705 706 /** 707 * spinand_read_page() - Read a page 708 * @spinand: the spinand device 709 * @req: the I/O request 710 * 711 * Return: 0 or a positive number of bitflips corrected on success. 712 * A negative error code otherwise. 713 */ 714 int spinand_read_page(struct spinand_device *spinand, 715 const struct nand_page_io_req *req) 716 { 717 struct nand_device *nand = spinand_to_nand(spinand); 718 u8 status; 719 int ret; 720 721 ret = nand_ecc_prepare_io_req(nand, (struct nand_page_io_req *)req); 722 if (ret) 723 return ret; 724 725 ret = spinand_load_page_op(spinand, req); 726 if (ret) 727 return ret; 728 729 ret = spinand_wait(spinand, 730 SPINAND_READ_INITIAL_DELAY_US, 731 SPINAND_READ_POLL_DELAY_US, 732 &status); 733 if (ret < 0) 734 return ret; 735 736 spinand_ondie_ecc_save_status(nand, status); 737 738 ret = spinand_read_from_cache_op(spinand, req); 739 if (ret) 740 return ret; 741 742 return nand_ecc_finish_io_req(nand, (struct nand_page_io_req *)req); 743 } 744 745 /** 746 * spinand_write_page() - Write a page 747 * @spinand: the spinand device 748 * @req: the I/O request 749 * 750 * Return: 0 or a positive number of bitflips corrected on success. 751 * A negative error code otherwise. 752 */ 753 int spinand_write_page(struct spinand_device *spinand, 754 const struct nand_page_io_req *req) 755 { 756 struct nand_device *nand = spinand_to_nand(spinand); 757 u8 status; 758 int ret; 759 760 ret = nand_ecc_prepare_io_req(nand, (struct nand_page_io_req *)req); 761 if (ret) 762 return ret; 763 764 ret = spinand_write_enable_op(spinand); 765 if (ret) 766 return ret; 767 768 ret = spinand_write_to_cache_op(spinand, req); 769 if (ret) 770 return ret; 771 772 ret = spinand_program_op(spinand, req); 773 if (ret) 774 return ret; 775 776 ret = spinand_wait(spinand, 777 SPINAND_WRITE_INITIAL_DELAY_US, 778 SPINAND_WRITE_POLL_DELAY_US, 779 &status); 780 if (ret) 781 return ret; 782 783 if (status & STATUS_PROG_FAILED) 784 return -EIO; 785 786 return nand_ecc_finish_io_req(nand, (struct nand_page_io_req *)req); 787 } 788 789 static int spinand_mtd_regular_page_read(struct mtd_info *mtd, loff_t from, 790 struct mtd_oob_ops *ops, 791 unsigned int *max_bitflips) 792 { 793 struct spinand_device *spinand = mtd_to_spinand(mtd); 794 struct nand_device *nand = mtd_to_nanddev(mtd); 795 struct mtd_ecc_stats old_stats; 796 struct nand_io_iter iter; 797 bool disable_ecc = false; 798 bool ecc_failed = false; 799 unsigned int retry_mode = 0; 800 int ret; 801 802 old_stats = mtd->ecc_stats; 803 804 if (ops->mode == MTD_OPS_RAW || !mtd->ooblayout) 805 disable_ecc = true; 806 807 nanddev_io_for_each_page(nand, NAND_PAGE_READ, from, ops, &iter) { 808 if (disable_ecc) 809 iter.req.mode = MTD_OPS_RAW; 810 811 ret = spinand_select_target(spinand, iter.req.pos.target); 812 if (ret) 813 break; 814 815 read_retry: 816 ret = spinand_read_page(spinand, &iter.req); 817 if (ret < 0 && ret != -EBADMSG) 818 break; 819 820 if (ret == -EBADMSG && spinand->set_read_retry) { 821 if (spinand->read_retries && (++retry_mode <= spinand->read_retries)) { 822 ret = spinand->set_read_retry(spinand, retry_mode); 823 if (ret < 0) { 824 spinand->set_read_retry(spinand, 0); 825 return ret; 826 } 827 828 /* Reset ecc_stats; retry */ 829 mtd->ecc_stats = old_stats; 830 goto read_retry; 831 } else { 832 /* No more retry modes; real failure */ 833 ecc_failed = true; 834 } 835 } else if (ret == -EBADMSG) { 836 ecc_failed = true; 837 } else { 838 *max_bitflips = max_t(unsigned int, *max_bitflips, ret); 839 } 840 841 ret = 0; 842 ops->retlen += iter.req.datalen; 843 ops->oobretlen += iter.req.ooblen; 844 845 /* Reset to retry mode 0 */ 846 if (retry_mode) { 847 retry_mode = 0; 848 ret = spinand->set_read_retry(spinand, retry_mode); 849 if (ret < 0) 850 return ret; 851 } 852 } 853 854 if (ecc_failed && !ret) 855 ret = -EBADMSG; 856 857 return ret; 858 } 859 860 static int spinand_mtd_continuous_page_read(struct mtd_info *mtd, loff_t from, 861 struct mtd_oob_ops *ops, 862 unsigned int *max_bitflips) 863 { 864 struct spinand_device *spinand = mtd_to_spinand(mtd); 865 struct nand_device *nand = mtd_to_nanddev(mtd); 866 struct nand_io_iter iter; 867 u8 status; 868 int ret; 869 870 ret = spinand_cont_read_enable(spinand, true); 871 if (ret) 872 return ret; 873 874 /* 875 * The cache is divided into two halves. While one half of the cache has 876 * the requested data, the other half is loaded with the next chunk of data. 877 * Therefore, the host can read out the data continuously from page to page. 878 * Each data read must be a multiple of 4-bytes and full pages should be read; 879 * otherwise, the data output might get out of sequence from one read command 880 * to another. 881 */ 882 nanddev_io_for_each_block(nand, NAND_PAGE_READ, from, ops, &iter) { 883 ret = spinand_select_target(spinand, iter.req.pos.target); 884 if (ret) 885 goto end_cont_read; 886 887 ret = nand_ecc_prepare_io_req(nand, &iter.req); 888 if (ret) 889 goto end_cont_read; 890 891 ret = spinand_load_page_op(spinand, &iter.req); 892 if (ret) 893 goto end_cont_read; 894 895 ret = spinand_wait(spinand, SPINAND_READ_INITIAL_DELAY_US, 896 SPINAND_READ_POLL_DELAY_US, NULL); 897 if (ret < 0) 898 goto end_cont_read; 899 900 ret = spinand_read_from_cache_op(spinand, &iter.req); 901 if (ret) 902 goto end_cont_read; 903 904 ops->retlen += iter.req.datalen; 905 906 ret = spinand_read_status(spinand, &status); 907 if (ret) 908 goto end_cont_read; 909 910 spinand_ondie_ecc_save_status(nand, status); 911 912 ret = nand_ecc_finish_io_req(nand, &iter.req); 913 if (ret < 0) 914 goto end_cont_read; 915 916 *max_bitflips = max_t(unsigned int, *max_bitflips, ret); 917 ret = 0; 918 } 919 920 end_cont_read: 921 /* 922 * Once all the data has been read out, the host can either pull CS# 923 * high and wait for tRST or manually clear the bit in the configuration 924 * register to terminate the continuous read operation. We have no 925 * guarantee the SPI controller drivers will effectively deassert the CS 926 * when we expect them to, so take the register based approach. 927 */ 928 spinand_cont_read_enable(spinand, false); 929 930 return ret; 931 } 932 933 static void spinand_cont_read_init(struct spinand_device *spinand) 934 { 935 struct nand_device *nand = spinand_to_nand(spinand); 936 enum nand_ecc_engine_type engine_type = nand->ecc.ctx.conf.engine_type; 937 938 /* OOBs cannot be retrieved so external/on-host ECC engine won't work */ 939 if (spinand->set_cont_read && 940 (engine_type == NAND_ECC_ENGINE_TYPE_ON_DIE || 941 engine_type == NAND_ECC_ENGINE_TYPE_NONE)) { 942 spinand->cont_read_possible = true; 943 944 /* 945 * Ensure continuous read is disabled on probe. 946 * Some devices retain this state across soft reset, 947 * which leaves the OOB area inaccessible and results 948 * in false positive returns from spinand_isbad(). 949 */ 950 spinand_cont_read_enable(spinand, false); 951 } 952 } 953 954 static bool spinand_use_cont_read(struct mtd_info *mtd, loff_t from, 955 struct mtd_oob_ops *ops) 956 { 957 struct nand_device *nand = mtd_to_nanddev(mtd); 958 struct spinand_device *spinand = nand_to_spinand(nand); 959 struct nand_pos start_pos, end_pos; 960 961 if (!spinand->cont_read_possible) 962 return false; 963 964 /* OOBs won't be retrieved */ 965 if (ops->ooblen || ops->oobbuf) 966 return false; 967 968 nanddev_offs_to_pos(nand, from, &start_pos); 969 nanddev_offs_to_pos(nand, from + ops->len - 1, &end_pos); 970 971 /* 972 * Continuous reads never cross LUN boundaries. Some devices don't 973 * support crossing planes boundaries. Some devices don't even support 974 * crossing blocks boundaries. The common case being to read through UBI, 975 * we will very rarely read two consequent blocks or more, so it is safer 976 * and easier (can be improved) to only enable continuous reads when 977 * reading within the same erase block. 978 */ 979 if (start_pos.target != end_pos.target || 980 start_pos.plane != end_pos.plane || 981 start_pos.eraseblock != end_pos.eraseblock) 982 return false; 983 984 return start_pos.page < end_pos.page; 985 } 986 987 static int spinand_mtd_read(struct mtd_info *mtd, loff_t from, 988 struct mtd_oob_ops *ops) 989 { 990 struct spinand_device *spinand = mtd_to_spinand(mtd); 991 struct mtd_ecc_stats old_stats; 992 unsigned int max_bitflips = 0; 993 int ret; 994 995 mutex_lock(&spinand->lock); 996 997 old_stats = mtd->ecc_stats; 998 999 if (spinand_use_cont_read(mtd, from, ops)) { 1000 ret = spinand_mtd_continuous_page_read(mtd, from, ops, &max_bitflips); 1001 if (ret == -EAGAIN && !spinand->cont_read_possible) { 1002 /* 1003 * Spi controller with broken support of continuous 1004 * reading was detected (see spinand_read_from_cache_op()), 1005 * repeat reading in regular mode. 1006 */ 1007 ret = spinand_mtd_regular_page_read(mtd, from, ops, &max_bitflips); 1008 } 1009 } else { 1010 ret = spinand_mtd_regular_page_read(mtd, from, ops, &max_bitflips); 1011 } 1012 1013 if (ops->stats) { 1014 ops->stats->uncorrectable_errors += 1015 mtd->ecc_stats.failed - old_stats.failed; 1016 ops->stats->corrected_bitflips += 1017 mtd->ecc_stats.corrected - old_stats.corrected; 1018 } 1019 1020 mutex_unlock(&spinand->lock); 1021 1022 return ret ? ret : max_bitflips; 1023 } 1024 1025 static int spinand_mtd_write(struct mtd_info *mtd, loff_t to, 1026 struct mtd_oob_ops *ops) 1027 { 1028 struct spinand_device *spinand = mtd_to_spinand(mtd); 1029 struct nand_device *nand = mtd_to_nanddev(mtd); 1030 struct nand_io_iter iter; 1031 bool disable_ecc = false; 1032 int ret = 0; 1033 1034 if (ops->mode == MTD_OPS_RAW || !mtd->ooblayout) 1035 disable_ecc = true; 1036 1037 mutex_lock(&spinand->lock); 1038 1039 nanddev_io_for_each_page(nand, NAND_PAGE_WRITE, to, ops, &iter) { 1040 if (disable_ecc) 1041 iter.req.mode = MTD_OPS_RAW; 1042 1043 ret = spinand_select_target(spinand, iter.req.pos.target); 1044 if (ret) 1045 break; 1046 1047 ret = spinand_write_page(spinand, &iter.req); 1048 if (ret) 1049 break; 1050 1051 ops->retlen += iter.req.datalen; 1052 ops->oobretlen += iter.req.ooblen; 1053 } 1054 1055 mutex_unlock(&spinand->lock); 1056 1057 return ret; 1058 } 1059 1060 static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos) 1061 { 1062 struct spinand_device *spinand = nand_to_spinand(nand); 1063 u8 marker[2] = { }; 1064 struct nand_page_io_req req = { 1065 .pos = *pos, 1066 .ooblen = sizeof(marker), 1067 .ooboffs = 0, 1068 .oobbuf.in = marker, 1069 .mode = MTD_OPS_RAW, 1070 }; 1071 int ret; 1072 1073 spinand_select_target(spinand, pos->target); 1074 1075 ret = spinand_read_page(spinand, &req); 1076 if (ret == -EOPNOTSUPP) { 1077 /* Retry with ECC in case raw access is not supported */ 1078 req.mode = MTD_OPS_PLACE_OOB; 1079 spinand_read_page(spinand, &req); 1080 } 1081 1082 if (marker[0] != 0xff || marker[1] != 0xff) 1083 return true; 1084 1085 return false; 1086 } 1087 1088 static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs) 1089 { 1090 struct nand_device *nand = mtd_to_nanddev(mtd); 1091 struct spinand_device *spinand = nand_to_spinand(nand); 1092 struct nand_pos pos; 1093 int ret; 1094 1095 nanddev_offs_to_pos(nand, offs, &pos); 1096 mutex_lock(&spinand->lock); 1097 ret = nanddev_isbad(nand, &pos); 1098 mutex_unlock(&spinand->lock); 1099 1100 return ret; 1101 } 1102 1103 static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos) 1104 { 1105 struct spinand_device *spinand = nand_to_spinand(nand); 1106 u8 marker[2] = { }; 1107 struct nand_page_io_req req = { 1108 .pos = *pos, 1109 .ooboffs = 0, 1110 .ooblen = sizeof(marker), 1111 .oobbuf.out = marker, 1112 .mode = MTD_OPS_RAW, 1113 }; 1114 int ret; 1115 1116 ret = spinand_select_target(spinand, pos->target); 1117 if (ret) 1118 return ret; 1119 1120 ret = spinand_write_page(spinand, &req); 1121 if (ret == -EOPNOTSUPP) { 1122 /* Retry with ECC in case raw access is not supported */ 1123 req.mode = MTD_OPS_PLACE_OOB; 1124 ret = spinand_write_page(spinand, &req); 1125 } 1126 1127 return ret; 1128 } 1129 1130 static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs) 1131 { 1132 struct nand_device *nand = mtd_to_nanddev(mtd); 1133 struct spinand_device *spinand = nand_to_spinand(nand); 1134 struct nand_pos pos; 1135 int ret; 1136 1137 nanddev_offs_to_pos(nand, offs, &pos); 1138 mutex_lock(&spinand->lock); 1139 ret = nanddev_markbad(nand, &pos); 1140 mutex_unlock(&spinand->lock); 1141 1142 return ret; 1143 } 1144 1145 static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos) 1146 { 1147 struct spinand_device *spinand = nand_to_spinand(nand); 1148 u8 status; 1149 int ret; 1150 1151 ret = spinand_select_target(spinand, pos->target); 1152 if (ret) 1153 return ret; 1154 1155 ret = spinand_write_enable_op(spinand); 1156 if (ret) 1157 return ret; 1158 1159 ret = spinand_erase_op(spinand, pos); 1160 if (ret) 1161 return ret; 1162 1163 ret = spinand_wait(spinand, 1164 SPINAND_ERASE_INITIAL_DELAY_US, 1165 SPINAND_ERASE_POLL_DELAY_US, 1166 &status); 1167 1168 if (!ret && (status & STATUS_ERASE_FAILED)) 1169 ret = -EIO; 1170 1171 return ret; 1172 } 1173 1174 static int spinand_mtd_erase(struct mtd_info *mtd, 1175 struct erase_info *einfo) 1176 { 1177 struct spinand_device *spinand = mtd_to_spinand(mtd); 1178 int ret; 1179 1180 mutex_lock(&spinand->lock); 1181 ret = nanddev_mtd_erase(mtd, einfo); 1182 mutex_unlock(&spinand->lock); 1183 1184 return ret; 1185 } 1186 1187 static int spinand_mtd_block_isreserved(struct mtd_info *mtd, loff_t offs) 1188 { 1189 struct spinand_device *spinand = mtd_to_spinand(mtd); 1190 struct nand_device *nand = mtd_to_nanddev(mtd); 1191 struct nand_pos pos; 1192 int ret; 1193 1194 nanddev_offs_to_pos(nand, offs, &pos); 1195 mutex_lock(&spinand->lock); 1196 ret = nanddev_isreserved(nand, &pos); 1197 mutex_unlock(&spinand->lock); 1198 1199 return ret; 1200 } 1201 1202 static struct spi_mem_dirmap_desc *spinand_create_rdesc( 1203 struct spinand_device *spinand, 1204 struct spi_mem_dirmap_info *info) 1205 { 1206 struct nand_device *nand = spinand_to_nand(spinand); 1207 struct spi_mem_dirmap_desc *desc = NULL; 1208 1209 if (spinand->cont_read_possible) { 1210 /* 1211 * spi controller may return an error if info->length is 1212 * too large 1213 */ 1214 info->length = nanddev_eraseblock_size(nand); 1215 desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev, 1216 spinand->spimem, info); 1217 } 1218 1219 if (IS_ERR_OR_NULL(desc)) { 1220 /* 1221 * continuous reading is not supported by flash or 1222 * its spi controller, use regular reading 1223 */ 1224 spinand->cont_read_possible = false; 1225 1226 info->length = nanddev_page_size(nand) + 1227 nanddev_per_page_oobsize(nand); 1228 desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev, 1229 spinand->spimem, info); 1230 } 1231 1232 return desc; 1233 } 1234 1235 static int spinand_create_dirmap(struct spinand_device *spinand, 1236 unsigned int plane) 1237 { 1238 struct nand_device *nand = spinand_to_nand(spinand); 1239 struct spi_mem_dirmap_info info = { 0 }; 1240 struct spi_mem_dirmap_desc *desc; 1241 1242 /* The plane number is passed in MSB just above the column address */ 1243 info.offset = plane << fls(nand->memorg.pagesize); 1244 1245 info.length = nanddev_page_size(nand) + nanddev_per_page_oobsize(nand); 1246 info.op_tmpl = *spinand->op_templates->update_cache; 1247 desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev, 1248 spinand->spimem, &info); 1249 if (IS_ERR(desc)) 1250 return PTR_ERR(desc); 1251 1252 spinand->dirmaps[plane].wdesc = desc; 1253 1254 info.op_tmpl = *spinand->op_templates->read_cache; 1255 desc = spinand_create_rdesc(spinand, &info); 1256 if (IS_ERR(desc)) 1257 return PTR_ERR(desc); 1258 1259 spinand->dirmaps[plane].rdesc = desc; 1260 1261 if (nand->ecc.engine->integration != NAND_ECC_ENGINE_INTEGRATION_PIPELINED) { 1262 spinand->dirmaps[plane].wdesc_ecc = spinand->dirmaps[plane].wdesc; 1263 spinand->dirmaps[plane].rdesc_ecc = spinand->dirmaps[plane].rdesc; 1264 1265 return 0; 1266 } 1267 1268 info.length = nanddev_page_size(nand) + nanddev_per_page_oobsize(nand); 1269 info.op_tmpl = *spinand->op_templates->update_cache; 1270 info.op_tmpl.data.ecc = true; 1271 desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev, 1272 spinand->spimem, &info); 1273 if (IS_ERR(desc)) 1274 return PTR_ERR(desc); 1275 1276 spinand->dirmaps[plane].wdesc_ecc = desc; 1277 1278 info.op_tmpl = *spinand->op_templates->read_cache; 1279 info.op_tmpl.data.ecc = true; 1280 desc = spinand_create_rdesc(spinand, &info); 1281 if (IS_ERR(desc)) 1282 return PTR_ERR(desc); 1283 1284 spinand->dirmaps[plane].rdesc_ecc = desc; 1285 1286 return 0; 1287 } 1288 1289 static int spinand_create_dirmaps(struct spinand_device *spinand) 1290 { 1291 struct nand_device *nand = spinand_to_nand(spinand); 1292 int i, ret; 1293 1294 spinand->dirmaps = devm_kzalloc(&spinand->spimem->spi->dev, 1295 sizeof(*spinand->dirmaps) * 1296 nand->memorg.planes_per_lun, 1297 GFP_KERNEL); 1298 if (!spinand->dirmaps) 1299 return -ENOMEM; 1300 1301 for (i = 0; i < nand->memorg.planes_per_lun; i++) { 1302 ret = spinand_create_dirmap(spinand, i); 1303 if (ret) 1304 return ret; 1305 } 1306 1307 return 0; 1308 } 1309 1310 static const struct nand_ops spinand_ops = { 1311 .erase = spinand_erase, 1312 .markbad = spinand_markbad, 1313 .isbad = spinand_isbad, 1314 }; 1315 1316 static const struct spinand_manufacturer *spinand_manufacturers[] = { 1317 &alliancememory_spinand_manufacturer, 1318 &ato_spinand_manufacturer, 1319 &dosilicon_spinand_manufacturer, 1320 &esmt_8c_spinand_manufacturer, 1321 &esmt_c8_spinand_manufacturer, 1322 &fmsh_spinand_manufacturer, 1323 &foresee_spinand_manufacturer, 1324 &gigadevice_spinand_manufacturer, 1325 ¯onix_spinand_manufacturer, 1326 µn_spinand_manufacturer, 1327 ¶gon_spinand_manufacturer, 1328 &skyhigh_spinand_manufacturer, 1329 &toshiba_spinand_manufacturer, 1330 &winbond_spinand_manufacturer, 1331 &xtx_spinand_manufacturer, 1332 }; 1333 1334 static int spinand_manufacturer_match(struct spinand_device *spinand, 1335 enum spinand_readid_method rdid_method) 1336 { 1337 u8 *id = spinand->id.data; 1338 unsigned int i; 1339 int ret; 1340 1341 for (i = 0; i < ARRAY_SIZE(spinand_manufacturers); i++) { 1342 const struct spinand_manufacturer *manufacturer = 1343 spinand_manufacturers[i]; 1344 1345 if (id[0] != manufacturer->id) 1346 continue; 1347 1348 ret = spinand_match_and_init(spinand, 1349 manufacturer->chips, 1350 manufacturer->nchips, 1351 rdid_method); 1352 if (ret < 0) 1353 continue; 1354 1355 spinand->manufacturer = manufacturer; 1356 return 0; 1357 } 1358 return -EOPNOTSUPP; 1359 } 1360 1361 static int spinand_id_detect(struct spinand_device *spinand) 1362 { 1363 u8 *id = spinand->id.data; 1364 int ret; 1365 1366 ret = spinand_read_id_op(spinand, 0, 0, id); 1367 if (ret) 1368 return ret; 1369 ret = spinand_manufacturer_match(spinand, SPINAND_READID_METHOD_OPCODE); 1370 if (!ret) 1371 return 0; 1372 1373 ret = spinand_read_id_op(spinand, 1, 0, id); 1374 if (ret) 1375 return ret; 1376 ret = spinand_manufacturer_match(spinand, 1377 SPINAND_READID_METHOD_OPCODE_ADDR); 1378 if (!ret) 1379 return 0; 1380 1381 ret = spinand_read_id_op(spinand, 0, 1, id); 1382 if (ret) 1383 return ret; 1384 ret = spinand_manufacturer_match(spinand, 1385 SPINAND_READID_METHOD_OPCODE_DUMMY); 1386 1387 return ret; 1388 } 1389 1390 static int spinand_manufacturer_init(struct spinand_device *spinand) 1391 { 1392 int ret; 1393 1394 if (spinand->manufacturer->ops->init) { 1395 ret = spinand->manufacturer->ops->init(spinand); 1396 if (ret) 1397 return ret; 1398 } 1399 1400 return 0; 1401 } 1402 1403 static void spinand_manufacturer_cleanup(struct spinand_device *spinand) 1404 { 1405 /* Release manufacturer private data */ 1406 if (spinand->manufacturer->ops->cleanup) 1407 return spinand->manufacturer->ops->cleanup(spinand); 1408 } 1409 1410 static bool spinand_op_is_odtr(const struct spi_mem_op *op) 1411 { 1412 return op->cmd.dtr && op->cmd.buswidth == 8; 1413 } 1414 1415 static void spinand_init_ssdr_templates(struct spinand_device *spinand) 1416 { 1417 struct spinand_mem_ops *tmpl = &spinand->ssdr_op_templates; 1418 1419 tmpl->reset = (struct spi_mem_op)SPINAND_RESET_1S_0_0_OP; 1420 tmpl->readid = (struct spi_mem_op)SPINAND_READID_1S_1S_1S_OP(0, 0, NULL, 0); 1421 tmpl->wr_en = (struct spi_mem_op)SPINAND_WR_EN_1S_0_0_OP; 1422 tmpl->wr_dis = (struct spi_mem_op)SPINAND_WR_DIS_1S_0_0_OP; 1423 tmpl->set_feature = (struct spi_mem_op)SPINAND_SET_FEATURE_1S_1S_1S_OP(0, NULL); 1424 tmpl->get_feature = (struct spi_mem_op)SPINAND_GET_FEATURE_1S_1S_1S_OP(0, NULL); 1425 tmpl->blk_erase = (struct spi_mem_op)SPINAND_BLK_ERASE_1S_1S_0_OP(0); 1426 tmpl->page_read = (struct spi_mem_op)SPINAND_PAGE_READ_1S_1S_0_OP(0); 1427 tmpl->prog_exec = (struct spi_mem_op)SPINAND_PROG_EXEC_1S_1S_0_OP(0); 1428 spinand->op_templates = &spinand->ssdr_op_templates; 1429 spinand->bus_iface = SSDR; 1430 } 1431 1432 static int spinand_support_vendor_ops(struct spinand_device *spinand, 1433 const struct spinand_info *info, 1434 enum spinand_bus_interface iface) 1435 { 1436 int i; 1437 1438 if (!info->vendor_ops) 1439 return 0; 1440 /* 1441 * The vendor ops array is only used in order to verify this chip and all its memory 1442 * operations are supported. If we see patterns emerging, we could ideally name these 1443 * operations and define them at the SPI NAND core level instead. 1444 * For now, this only serves as a sanity check. 1445 */ 1446 for (i = 0; i < info->vendor_ops->nops; i++) { 1447 const struct spi_mem_op *op = &info->vendor_ops->ops[i]; 1448 1449 if ((iface == SSDR && spinand_op_is_odtr(op)) || 1450 (iface == ODTR && !spinand_op_is_odtr(op))) 1451 continue; 1452 1453 if (!spi_mem_supports_op(spinand->spimem, op)) 1454 return -EOPNOTSUPP; 1455 } 1456 1457 return 0; 1458 } 1459 1460 static int spinand_init_odtr_instruction_set(struct spinand_device *spinand) 1461 { 1462 struct spinand_mem_ops *tmpl = &spinand->odtr_op_templates; 1463 1464 tmpl->reset = (struct spi_mem_op)SPINAND_RESET_8D_0_0_OP; 1465 if (!spi_mem_supports_op(spinand->spimem, &tmpl->reset)) 1466 return -EOPNOTSUPP; 1467 1468 tmpl->readid = (struct spi_mem_op)SPINAND_READID_8D_8D_8D_OP(0, 0, NULL, 0); 1469 if (!spi_mem_supports_op(spinand->spimem, &tmpl->readid)) 1470 return -EOPNOTSUPP; 1471 1472 tmpl->wr_en = (struct spi_mem_op)SPINAND_WR_EN_8D_0_0_OP; 1473 if (!spi_mem_supports_op(spinand->spimem, &tmpl->wr_en)) 1474 return -EOPNOTSUPP; 1475 1476 tmpl->wr_dis = (struct spi_mem_op)SPINAND_WR_DIS_8D_0_0_OP; 1477 if (!spi_mem_supports_op(spinand->spimem, &tmpl->wr_dis)) 1478 return -EOPNOTSUPP; 1479 1480 tmpl->set_feature = (struct spi_mem_op)SPINAND_SET_FEATURE_8D_8D_8D_OP(0, NULL); 1481 if (!spi_mem_supports_op(spinand->spimem, &tmpl->set_feature)) 1482 return -EOPNOTSUPP; 1483 1484 tmpl->get_feature = (struct spi_mem_op)SPINAND_GET_FEATURE_8D_8D_8D_OP(0, NULL); 1485 if (!spi_mem_supports_op(spinand->spimem, &tmpl->get_feature)) 1486 return -EOPNOTSUPP; 1487 1488 tmpl->blk_erase = (struct spi_mem_op)SPINAND_BLK_ERASE_8D_8D_0_OP(0); 1489 if (!spi_mem_supports_op(spinand->spimem, &tmpl->blk_erase)) 1490 return -EOPNOTSUPP; 1491 1492 tmpl->page_read = (struct spi_mem_op)SPINAND_PAGE_READ_8D_8D_0_OP(0); 1493 if (!spi_mem_supports_op(spinand->spimem, &tmpl->page_read)) 1494 return -EOPNOTSUPP; 1495 1496 tmpl->prog_exec = (struct spi_mem_op)SPINAND_PROG_EXEC_8D_8D_0_OP(0); 1497 if (!spi_mem_supports_op(spinand->spimem, &tmpl->prog_exec)) 1498 return -EOPNOTSUPP; 1499 1500 return 0; 1501 } 1502 1503 static const struct spi_mem_op * 1504 spinand_select_op_variant(struct spinand_device *spinand, enum spinand_bus_interface iface, 1505 const struct spinand_op_variants *variants) 1506 { 1507 struct nand_device *nand = spinand_to_nand(spinand); 1508 const struct spi_mem_op *best_variant = NULL; 1509 u64 best_op_duration_ns = ULLONG_MAX; 1510 unsigned int i; 1511 1512 for (i = 0; i < variants->nops; i++) { 1513 struct spi_mem_op op = variants->ops[i]; 1514 u64 op_duration_ns = 0; 1515 unsigned int nbytes; 1516 int ret; 1517 1518 if ((iface == SSDR && spinand_op_is_odtr(&op)) || 1519 (iface == ODTR && !spinand_op_is_odtr(&op))) 1520 continue; 1521 1522 nbytes = nanddev_per_page_oobsize(nand) + 1523 nanddev_page_size(nand); 1524 1525 while (nbytes) { 1526 op.data.nbytes = nbytes; 1527 ret = spi_mem_adjust_op_size(spinand->spimem, &op); 1528 if (ret) 1529 break; 1530 1531 spi_mem_adjust_op_freq(spinand->spimem, &op); 1532 1533 if (!spi_mem_supports_op(spinand->spimem, &op)) 1534 break; 1535 1536 nbytes -= op.data.nbytes; 1537 1538 op_duration_ns += spi_mem_calc_op_duration(spinand->spimem, &op); 1539 } 1540 1541 if (!nbytes && op_duration_ns < best_op_duration_ns) { 1542 best_op_duration_ns = op_duration_ns; 1543 best_variant = &variants->ops[i]; 1544 } 1545 } 1546 1547 return best_variant; 1548 } 1549 1550 /** 1551 * spinand_match_and_init() - Try to find a match between a device ID and an 1552 * entry in a spinand_info table 1553 * @spinand: SPI NAND object 1554 * @table: SPI NAND device description table 1555 * @table_size: size of the device description table 1556 * @rdid_method: read id method to match 1557 * 1558 * Match between a device ID retrieved through the READ_ID command and an 1559 * entry in the SPI NAND description table. If a match is found, the spinand 1560 * object will be initialized with information provided by the matching 1561 * spinand_info entry. 1562 * 1563 * Return: 0 on success, a negative error code otherwise. 1564 */ 1565 int spinand_match_and_init(struct spinand_device *spinand, 1566 const struct spinand_info *table, 1567 unsigned int table_size, 1568 enum spinand_readid_method rdid_method) 1569 { 1570 u8 *id = spinand->id.data; 1571 struct nand_device *nand = spinand_to_nand(spinand); 1572 unsigned int i; 1573 int ret; 1574 1575 for (i = 0; i < table_size; i++) { 1576 const struct spinand_info *info = &table[i]; 1577 const struct spi_mem_op *op; 1578 1579 if (rdid_method != info->devid.method) 1580 continue; 1581 1582 if (memcmp(id + 1, info->devid.id, info->devid.len)) 1583 continue; 1584 1585 nand->memorg = table[i].memorg; 1586 nanddev_set_ecc_requirements(nand, &table[i].eccreq); 1587 spinand->eccinfo = table[i].eccinfo; 1588 spinand->flags = table[i].flags; 1589 spinand->id.len = 1 + table[i].devid.len; 1590 spinand->select_target = table[i].select_target; 1591 spinand->configure_chip = table[i].configure_chip; 1592 spinand->set_cont_read = table[i].set_cont_read; 1593 spinand->fact_otp = &table[i].fact_otp; 1594 spinand->user_otp = &table[i].user_otp; 1595 spinand->read_retries = table[i].read_retries; 1596 spinand->set_read_retry = table[i].set_read_retry; 1597 1598 /* I/O variants selection with single-spi SDR commands */ 1599 1600 op = spinand_select_op_variant(spinand, SSDR, 1601 info->op_variants.read_cache); 1602 if (!op) 1603 return -EOPNOTSUPP; 1604 1605 spinand->ssdr_op_templates.read_cache = op; 1606 1607 op = spinand_select_op_variant(spinand, SSDR, 1608 info->op_variants.write_cache); 1609 if (!op) 1610 return -EOPNOTSUPP; 1611 1612 spinand->ssdr_op_templates.write_cache = op; 1613 1614 op = spinand_select_op_variant(spinand, SSDR, 1615 info->op_variants.update_cache); 1616 if (!op) 1617 return -EOPNOTSUPP; 1618 1619 spinand->ssdr_op_templates.update_cache = op; 1620 1621 ret = spinand_support_vendor_ops(spinand, info, SSDR); 1622 if (ret) 1623 return ret; 1624 1625 /* I/O variants selection with octo-spi DDR commands (optional) */ 1626 1627 ret = spinand_init_odtr_instruction_set(spinand); 1628 if (ret) 1629 return 0; 1630 1631 ret = spinand_support_vendor_ops(spinand, info, ODTR); 1632 if (ret) 1633 return 0; 1634 1635 op = spinand_select_op_variant(spinand, ODTR, 1636 info->op_variants.read_cache); 1637 spinand->odtr_op_templates.read_cache = op; 1638 1639 op = spinand_select_op_variant(spinand, ODTR, 1640 info->op_variants.write_cache); 1641 spinand->odtr_op_templates.write_cache = op; 1642 1643 op = spinand_select_op_variant(spinand, ODTR, 1644 info->op_variants.update_cache); 1645 spinand->odtr_op_templates.update_cache = op; 1646 1647 return 0; 1648 } 1649 1650 return -EOPNOTSUPP; 1651 } 1652 1653 static int spinand_detect(struct spinand_device *spinand) 1654 { 1655 struct device *dev = &spinand->spimem->spi->dev; 1656 struct nand_device *nand = spinand_to_nand(spinand); 1657 int ret; 1658 1659 ret = spinand_reset_op(spinand); 1660 if (ret) 1661 return ret; 1662 1663 ret = spinand_id_detect(spinand); 1664 if (ret) { 1665 dev_err(dev, "unknown raw ID %*phN\n", SPINAND_MAX_ID_LEN, 1666 spinand->id.data); 1667 return ret; 1668 } 1669 1670 if (nand->memorg.ntargets > 1 && !spinand->select_target) { 1671 dev_err(dev, 1672 "SPI NANDs with more than one die must implement ->select_target()\n"); 1673 return -EINVAL; 1674 } 1675 1676 dev_info(&spinand->spimem->spi->dev, 1677 "%s SPI NAND was found.\n", spinand->manufacturer->name); 1678 dev_info(&spinand->spimem->spi->dev, 1679 "%llu MiB, block size: %zu KiB, page size: %zu, OOB size: %u\n", 1680 nanddev_size(nand) >> 20, nanddev_eraseblock_size(nand) >> 10, 1681 nanddev_page_size(nand), nanddev_per_page_oobsize(nand)); 1682 1683 return 0; 1684 } 1685 1686 static int spinand_configure_chip(struct spinand_device *spinand) 1687 { 1688 bool odtr = false, quad_enable = false; 1689 int ret; 1690 1691 if (spinand->odtr_op_templates.read_cache && 1692 spinand->odtr_op_templates.write_cache && 1693 spinand->odtr_op_templates.update_cache) 1694 odtr = true; 1695 1696 if (odtr) { 1697 if (!spinand->configure_chip) 1698 goto try_ssdr; 1699 1700 /* ODTR bus interface configuration happens here */ 1701 ret = spinand->configure_chip(spinand, ODTR); 1702 if (ret) { 1703 spinand->odtr_op_templates.read_cache = NULL; 1704 spinand->odtr_op_templates.write_cache = NULL; 1705 spinand->odtr_op_templates.update_cache = NULL; 1706 goto try_ssdr; 1707 } 1708 1709 spinand->op_templates = &spinand->odtr_op_templates; 1710 spinand->bus_iface = ODTR; 1711 1712 return 0; 1713 } 1714 1715 try_ssdr: 1716 if (spinand->flags & SPINAND_HAS_QE_BIT) { 1717 if (spinand->ssdr_op_templates.read_cache->data.buswidth == 4 || 1718 spinand->ssdr_op_templates.write_cache->data.buswidth == 4 || 1719 spinand->ssdr_op_templates.update_cache->data.buswidth == 4) 1720 quad_enable = true; 1721 } 1722 1723 ret = spinand_init_quad_enable(spinand, quad_enable); 1724 if (ret) 1725 return ret; 1726 1727 if (spinand->configure_chip) { 1728 ret = spinand->configure_chip(spinand, SSDR); 1729 if (ret) 1730 return ret; 1731 } 1732 1733 return ret; 1734 } 1735 1736 static int spinand_init_flash(struct spinand_device *spinand) 1737 { 1738 struct device *dev = &spinand->spimem->spi->dev; 1739 struct nand_device *nand = spinand_to_nand(spinand); 1740 int ret, i; 1741 1742 ret = spinand_read_cfg(spinand); 1743 if (ret) 1744 return ret; 1745 1746 ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0); 1747 if (ret) 1748 return ret; 1749 1750 ret = spinand_manufacturer_init(spinand); 1751 if (ret) { 1752 dev_err(dev, 1753 "Failed to initialize the SPI NAND chip (err = %d)\n", 1754 ret); 1755 return ret; 1756 } 1757 1758 ret = spinand_configure_chip(spinand); 1759 if (ret) 1760 goto manuf_cleanup; 1761 1762 /* After power up, all blocks are locked, so unlock them here. */ 1763 for (i = 0; i < nand->memorg.ntargets; i++) { 1764 ret = spinand_select_target(spinand, i); 1765 if (ret) 1766 goto manuf_cleanup; 1767 1768 ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED); 1769 if (ret) 1770 goto manuf_cleanup; 1771 } 1772 1773 return 0; 1774 1775 manuf_cleanup: 1776 spinand_manufacturer_cleanup(spinand); 1777 1778 return ret; 1779 } 1780 1781 static void spinand_mtd_resume(struct mtd_info *mtd) 1782 { 1783 struct spinand_device *spinand = mtd_to_spinand(mtd); 1784 int ret; 1785 1786 ret = spinand_reset_op(spinand); 1787 if (ret) 1788 return; 1789 1790 ret = spinand_init_flash(spinand); 1791 if (ret) 1792 return; 1793 1794 spinand_ecc_enable(spinand, false); 1795 } 1796 1797 static int spinand_mtd_suspend(struct mtd_info *mtd) 1798 { 1799 struct spinand_device *spinand = mtd_to_spinand(mtd); 1800 int ret; 1801 1802 /* 1803 * Return to SSDR interface in the suspend path to make sure the 1804 * reset operation is correctly processed upon resume. 1805 * 1806 * Note: Once back in SSDR mode, every operation but the page helpers 1807 * (dirmap based I/O accessors) will work. Page accesses would require 1808 * destroying and recreating the dirmaps twice to work, which would be 1809 * impacting for no reason, as this is just a transitional state. 1810 */ 1811 if (spinand->bus_iface == ODTR) { 1812 ret = spinand->configure_chip(spinand, SSDR); 1813 if (ret) 1814 return ret; 1815 1816 spinand->op_templates = &spinand->ssdr_op_templates; 1817 spinand->bus_iface = SSDR; 1818 } 1819 1820 return 0; 1821 } 1822 1823 static int spinand_init(struct spinand_device *spinand) 1824 { 1825 struct device *dev = &spinand->spimem->spi->dev; 1826 struct mtd_info *mtd = spinand_to_mtd(spinand); 1827 struct nand_device *nand = mtd_to_nanddev(mtd); 1828 int ret; 1829 1830 /* 1831 * We need a scratch buffer because the spi_mem interface requires that 1832 * buf passed in spi_mem_op->data.buf be DMA-able. 1833 */ 1834 spinand->scratchbuf = kzalloc(SPINAND_MAX_ID_LEN, GFP_KERNEL); 1835 if (!spinand->scratchbuf) 1836 return -ENOMEM; 1837 1838 spinand_init_ssdr_templates(spinand); 1839 1840 ret = spinand_detect(spinand); 1841 if (ret) 1842 goto err_free_bufs; 1843 1844 /* 1845 * Use kzalloc() instead of devm_kzalloc() here, because some drivers 1846 * may use this buffer for DMA access. 1847 * Memory allocated by devm_ does not guarantee DMA-safe alignment. 1848 */ 1849 spinand->databuf = kzalloc(nanddev_eraseblock_size(nand), 1850 GFP_KERNEL); 1851 if (!spinand->databuf) { 1852 ret = -ENOMEM; 1853 goto err_free_bufs; 1854 } 1855 1856 spinand->oobbuf = spinand->databuf + nanddev_page_size(nand); 1857 1858 ret = spinand_init_cfg_cache(spinand); 1859 if (ret) 1860 goto err_free_bufs; 1861 1862 ret = spinand_init_flash(spinand); 1863 if (ret) 1864 goto err_free_bufs; 1865 1866 ret = nanddev_init(nand, &spinand_ops, THIS_MODULE); 1867 if (ret) 1868 goto err_manuf_cleanup; 1869 1870 /* SPI-NAND default ECC engine is on-die */ 1871 nand->ecc.defaults.engine_type = NAND_ECC_ENGINE_TYPE_ON_DIE; 1872 nand->ecc.ondie_engine = &spinand_ondie_ecc_engine; 1873 1874 spinand_ecc_enable(spinand, false); 1875 ret = nanddev_ecc_engine_init(nand); 1876 if (ret) 1877 goto err_cleanup_nanddev; 1878 1879 /* 1880 * Continuous read can only be enabled with an on-die ECC engine, so the 1881 * ECC initialization must have happened previously. 1882 */ 1883 spinand_cont_read_init(spinand); 1884 1885 mtd->_read_oob = spinand_mtd_read; 1886 mtd->_write_oob = spinand_mtd_write; 1887 mtd->_block_isbad = spinand_mtd_block_isbad; 1888 mtd->_block_markbad = spinand_mtd_block_markbad; 1889 mtd->_block_isreserved = spinand_mtd_block_isreserved; 1890 mtd->_erase = spinand_mtd_erase; 1891 mtd->_max_bad_blocks = nanddev_mtd_max_bad_blocks; 1892 mtd->_suspend = spinand_mtd_suspend; 1893 mtd->_resume = spinand_mtd_resume; 1894 1895 if (spinand_user_otp_size(spinand) || spinand_fact_otp_size(spinand)) { 1896 ret = spinand_set_mtd_otp_ops(spinand); 1897 if (ret) 1898 goto err_cleanup_ecc_engine; 1899 } 1900 1901 if (nand->ecc.engine) { 1902 ret = mtd_ooblayout_count_freebytes(mtd); 1903 if (ret < 0) 1904 goto err_cleanup_ecc_engine; 1905 } 1906 1907 mtd->oobavail = ret; 1908 1909 /* Propagate ECC information to mtd_info */ 1910 mtd->ecc_strength = nanddev_get_ecc_conf(nand)->strength; 1911 mtd->ecc_step_size = nanddev_get_ecc_conf(nand)->step_size; 1912 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); 1913 1914 ret = spinand_create_dirmaps(spinand); 1915 if (ret) { 1916 dev_err(dev, 1917 "Failed to create direct mappings for read/write operations (err = %d)\n", 1918 ret); 1919 goto err_cleanup_ecc_engine; 1920 } 1921 1922 return 0; 1923 1924 err_cleanup_ecc_engine: 1925 nanddev_ecc_engine_cleanup(nand); 1926 1927 err_cleanup_nanddev: 1928 nanddev_cleanup(nand); 1929 1930 err_manuf_cleanup: 1931 spinand_manufacturer_cleanup(spinand); 1932 1933 err_free_bufs: 1934 kfree(spinand->databuf); 1935 kfree(spinand->scratchbuf); 1936 return ret; 1937 } 1938 1939 static void spinand_cleanup(struct spinand_device *spinand) 1940 { 1941 struct nand_device *nand = spinand_to_nand(spinand); 1942 1943 nanddev_ecc_engine_cleanup(nand); 1944 nanddev_cleanup(nand); 1945 spinand_manufacturer_cleanup(spinand); 1946 kfree(spinand->databuf); 1947 kfree(spinand->scratchbuf); 1948 } 1949 1950 static int spinand_probe(struct spi_mem *mem) 1951 { 1952 struct spinand_device *spinand; 1953 struct mtd_info *mtd; 1954 int ret; 1955 1956 spinand = devm_kzalloc(&mem->spi->dev, sizeof(*spinand), 1957 GFP_KERNEL); 1958 if (!spinand) 1959 return -ENOMEM; 1960 1961 spinand->spimem = mem; 1962 spi_mem_set_drvdata(mem, spinand); 1963 spinand_set_of_node(spinand, mem->spi->dev.of_node); 1964 mutex_init(&spinand->lock); 1965 mtd = spinand_to_mtd(spinand); 1966 mtd->dev.parent = &mem->spi->dev; 1967 1968 ret = spinand_init(spinand); 1969 if (ret) 1970 return ret; 1971 1972 ret = mtd_device_register(mtd, NULL, 0); 1973 if (ret) 1974 goto err_spinand_cleanup; 1975 1976 return 0; 1977 1978 err_spinand_cleanup: 1979 spinand_cleanup(spinand); 1980 1981 return ret; 1982 } 1983 1984 static int spinand_remove(struct spi_mem *mem) 1985 { 1986 struct spinand_device *spinand; 1987 struct mtd_info *mtd; 1988 int ret; 1989 1990 spinand = spi_mem_get_drvdata(mem); 1991 mtd = spinand_to_mtd(spinand); 1992 1993 ret = mtd_device_unregister(mtd); 1994 if (ret) 1995 return ret; 1996 1997 spinand_cleanup(spinand); 1998 1999 return 0; 2000 } 2001 2002 static const struct spi_device_id spinand_ids[] = { 2003 { .name = "spi-nand" }, 2004 { /* sentinel */ }, 2005 }; 2006 MODULE_DEVICE_TABLE(spi, spinand_ids); 2007 2008 #ifdef CONFIG_OF 2009 static const struct of_device_id spinand_of_ids[] = { 2010 { .compatible = "spi-nand" }, 2011 { /* sentinel */ }, 2012 }; 2013 MODULE_DEVICE_TABLE(of, spinand_of_ids); 2014 #endif 2015 2016 static struct spi_mem_driver spinand_drv = { 2017 .spidrv = { 2018 .id_table = spinand_ids, 2019 .driver = { 2020 .name = "spi-nand", 2021 .of_match_table = of_match_ptr(spinand_of_ids), 2022 }, 2023 }, 2024 .probe = spinand_probe, 2025 .remove = spinand_remove, 2026 }; 2027 module_spi_mem_driver(spinand_drv); 2028 2029 MODULE_DESCRIPTION("SPI NAND framework"); 2030 MODULE_AUTHOR("Peter Pan<peterpandong@micron.com>"); 2031 MODULE_LICENSE("GPL v2"); 2032