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 static struct spi_mem_op 104 spinand_fill_page_read_packed_op(struct spinand_device *spinand, u64 addr) 105 { 106 struct spi_mem_op op = spinand->op_templates->page_read; 107 108 op.cmd.opcode |= addr >> 16; 109 op.addr.val = addr & 0xFFFF; 110 111 return op; 112 } 113 114 struct spi_mem_op 115 spinand_fill_prog_exec_op(struct spinand_device *spinand, u64 addr) 116 { 117 struct spi_mem_op op = spinand->op_templates->prog_exec; 118 119 op.addr.val = addr; 120 121 return op; 122 } 123 124 int spinand_read_reg_op(struct spinand_device *spinand, u8 reg, u8 *val) 125 { 126 struct spi_mem_op op = SPINAND_OP(spinand, get_feature, 127 reg, spinand->scratchbuf); 128 int ret; 129 130 ret = spi_mem_exec_op(spinand->spimem, &op); 131 if (ret) 132 return ret; 133 134 *val = *spinand->scratchbuf; 135 return 0; 136 } 137 138 int spinand_write_reg_op(struct spinand_device *spinand, u8 reg, u8 val) 139 { 140 struct spi_mem_op op = SPINAND_OP(spinand, set_feature, 141 reg, spinand->scratchbuf); 142 143 *spinand->scratchbuf = val; 144 return spi_mem_exec_op(spinand->spimem, &op); 145 } 146 147 static int spinand_read_status(struct spinand_device *spinand, u8 *status) 148 { 149 return spinand_read_reg_op(spinand, REG_STATUS, status); 150 } 151 152 static int spinand_get_cfg(struct spinand_device *spinand, u8 *cfg) 153 { 154 struct nand_device *nand = spinand_to_nand(spinand); 155 156 if (WARN_ON(spinand->cur_target < 0 || 157 spinand->cur_target >= nand->memorg.ntargets)) 158 return -EINVAL; 159 160 *cfg = spinand->cfg_cache[spinand->cur_target]; 161 return 0; 162 } 163 164 static int spinand_set_cfg(struct spinand_device *spinand, u8 cfg) 165 { 166 struct nand_device *nand = spinand_to_nand(spinand); 167 int ret; 168 169 if (WARN_ON(spinand->cur_target < 0 || 170 spinand->cur_target >= nand->memorg.ntargets)) 171 return -EINVAL; 172 173 if (spinand->cfg_cache[spinand->cur_target] == cfg) 174 return 0; 175 176 ret = spinand_write_reg_op(spinand, REG_CFG, cfg); 177 if (ret) 178 return ret; 179 180 spinand->cfg_cache[spinand->cur_target] = cfg; 181 return 0; 182 } 183 184 /** 185 * spinand_upd_cfg() - Update the configuration register 186 * @spinand: the spinand device 187 * @mask: the mask encoding the bits to update in the config reg 188 * @val: the new value to apply 189 * 190 * Update the configuration register. 191 * 192 * Return: 0 on success, a negative error code otherwise. 193 */ 194 int spinand_upd_cfg(struct spinand_device *spinand, u8 mask, u8 val) 195 { 196 int ret; 197 u8 cfg; 198 199 ret = spinand_get_cfg(spinand, &cfg); 200 if (ret) 201 return ret; 202 203 cfg &= ~mask; 204 cfg |= val; 205 206 return spinand_set_cfg(spinand, cfg); 207 } 208 209 /** 210 * spinand_select_target() - Select a specific NAND target/die 211 * @spinand: the spinand device 212 * @target: the target/die to select 213 * 214 * Select a new target/die. If chip only has one die, this function is a NOOP. 215 * 216 * Return: 0 on success, a negative error code otherwise. 217 */ 218 int spinand_select_target(struct spinand_device *spinand, unsigned int target) 219 { 220 struct nand_device *nand = spinand_to_nand(spinand); 221 int ret; 222 223 if (WARN_ON(target >= nand->memorg.ntargets)) 224 return -EINVAL; 225 226 if (spinand->cur_target == target) 227 return 0; 228 229 if (nand->memorg.ntargets == 1) { 230 spinand->cur_target = target; 231 return 0; 232 } 233 234 ret = spinand->select_target(spinand, target); 235 if (ret) 236 return ret; 237 238 spinand->cur_target = target; 239 return 0; 240 } 241 242 static int spinand_read_cfg(struct spinand_device *spinand) 243 { 244 struct nand_device *nand = spinand_to_nand(spinand); 245 unsigned int target; 246 int ret; 247 248 for (target = 0; target < nand->memorg.ntargets; target++) { 249 ret = spinand_select_target(spinand, target); 250 if (ret) 251 return ret; 252 253 /* 254 * We use spinand_read_reg_op() instead of spinand_get_cfg() 255 * here to bypass the config cache. 256 */ 257 ret = spinand_read_reg_op(spinand, REG_CFG, 258 &spinand->cfg_cache[target]); 259 if (ret) 260 return ret; 261 } 262 263 return 0; 264 } 265 266 static int spinand_init_cfg_cache(struct spinand_device *spinand) 267 { 268 struct nand_device *nand = spinand_to_nand(spinand); 269 struct device *dev = &spinand->spimem->spi->dev; 270 271 spinand->cfg_cache = devm_kcalloc(dev, 272 nand->memorg.ntargets, 273 sizeof(*spinand->cfg_cache), 274 GFP_KERNEL); 275 if (!spinand->cfg_cache) 276 return -ENOMEM; 277 278 return 0; 279 } 280 281 static int spinand_init_quad_enable(struct spinand_device *spinand, 282 bool enable) 283 { 284 return spinand_upd_cfg(spinand, CFG_QUAD_ENABLE, 285 enable ? CFG_QUAD_ENABLE : 0); 286 } 287 288 static int spinand_ecc_enable(struct spinand_device *spinand, 289 bool enable) 290 { 291 return spinand_upd_cfg(spinand, CFG_ECC_ENABLE, 292 enable ? CFG_ECC_ENABLE : 0); 293 } 294 295 static int spinand_cont_read_enable(struct spinand_device *spinand, 296 bool enable) 297 { 298 return spinand->set_cont_read(spinand, enable); 299 } 300 301 static int spinand_check_ecc_status(struct spinand_device *spinand, u8 status) 302 { 303 struct nand_device *nand = spinand_to_nand(spinand); 304 305 if (spinand->eccinfo.get_status) 306 return spinand->eccinfo.get_status(spinand, status); 307 308 switch (status & STATUS_ECC_MASK) { 309 case STATUS_ECC_NO_BITFLIPS: 310 return 0; 311 312 case STATUS_ECC_HAS_BITFLIPS: 313 /* 314 * We have no way to know exactly how many bitflips have been 315 * fixed, so let's return the maximum possible value so that 316 * wear-leveling layers move the data immediately. 317 */ 318 return nanddev_get_ecc_conf(nand)->strength; 319 320 case STATUS_ECC_UNCOR_ERROR: 321 return -EBADMSG; 322 323 default: 324 break; 325 } 326 327 return -EINVAL; 328 } 329 330 static int spinand_noecc_ooblayout_ecc(struct mtd_info *mtd, int section, 331 struct mtd_oob_region *region) 332 { 333 return -ERANGE; 334 } 335 336 static int spinand_noecc_ooblayout_free(struct mtd_info *mtd, int section, 337 struct mtd_oob_region *region) 338 { 339 if (section) 340 return -ERANGE; 341 342 /* Reserve 2 bytes for the BBM. */ 343 region->offset = 2; 344 region->length = 62; 345 346 return 0; 347 } 348 349 static const struct mtd_ooblayout_ops spinand_noecc_ooblayout = { 350 .ecc = spinand_noecc_ooblayout_ecc, 351 .free = spinand_noecc_ooblayout_free, 352 }; 353 354 static int spinand_ondie_ecc_init_ctx(struct nand_device *nand) 355 { 356 struct spinand_device *spinand = nand_to_spinand(nand); 357 struct mtd_info *mtd = nanddev_to_mtd(nand); 358 struct spinand_ondie_ecc_conf *engine_conf; 359 360 nand->ecc.ctx.conf.engine_type = NAND_ECC_ENGINE_TYPE_ON_DIE; 361 nand->ecc.ctx.conf.step_size = nand->ecc.requirements.step_size; 362 nand->ecc.ctx.conf.strength = nand->ecc.requirements.strength; 363 364 engine_conf = kzalloc_obj(*engine_conf); 365 if (!engine_conf) 366 return -ENOMEM; 367 368 nand->ecc.ctx.priv = engine_conf; 369 370 if (spinand->eccinfo.ooblayout) 371 mtd_set_ooblayout(mtd, spinand->eccinfo.ooblayout); 372 else 373 mtd_set_ooblayout(mtd, &spinand_noecc_ooblayout); 374 375 return 0; 376 } 377 378 static void spinand_ondie_ecc_cleanup_ctx(struct nand_device *nand) 379 { 380 kfree(nand->ecc.ctx.priv); 381 } 382 383 static int spinand_ondie_ecc_prepare_io_req(struct nand_device *nand, 384 struct nand_page_io_req *req) 385 { 386 struct spinand_device *spinand = nand_to_spinand(nand); 387 bool enable = (req->mode != MTD_OPS_RAW); 388 389 if (!enable && spinand->flags & SPINAND_NO_RAW_ACCESS) 390 return -EOPNOTSUPP; 391 392 memset(spinand->oobbuf, 0xff, nanddev_per_page_oobsize(nand)); 393 394 /* Only enable or disable the engine */ 395 return spinand_ecc_enable(spinand, enable); 396 } 397 398 static int spinand_ondie_ecc_finish_io_req(struct nand_device *nand, 399 struct nand_page_io_req *req) 400 { 401 struct spinand_ondie_ecc_conf *engine_conf = nand->ecc.ctx.priv; 402 struct spinand_device *spinand = nand_to_spinand(nand); 403 struct mtd_info *mtd = spinand_to_mtd(spinand); 404 int ret; 405 406 if (req->mode == MTD_OPS_RAW) 407 return 0; 408 409 /* Nothing to do when finishing a page write */ 410 if (req->type == NAND_PAGE_WRITE) 411 return 0; 412 413 /* Finish a page read: check the status, report errors/bitflips */ 414 ret = spinand_check_ecc_status(spinand, engine_conf->status); 415 if (ret == -EBADMSG) { 416 mtd->ecc_stats.failed++; 417 } else if (ret > 0) { 418 unsigned int pages; 419 420 /* 421 * Continuous reads don't allow us to get the detail, 422 * so we may exagerate the actual number of corrected bitflips. 423 */ 424 if (!req->continuous) 425 pages = 1; 426 else 427 pages = req->datalen / nanddev_page_size(nand); 428 429 mtd->ecc_stats.corrected += ret * pages; 430 } 431 432 return ret; 433 } 434 435 static const struct nand_ecc_engine_ops spinand_ondie_ecc_engine_ops = { 436 .init_ctx = spinand_ondie_ecc_init_ctx, 437 .cleanup_ctx = spinand_ondie_ecc_cleanup_ctx, 438 .prepare_io_req = spinand_ondie_ecc_prepare_io_req, 439 .finish_io_req = spinand_ondie_ecc_finish_io_req, 440 }; 441 442 static struct nand_ecc_engine spinand_ondie_ecc_engine = { 443 .ops = &spinand_ondie_ecc_engine_ops, 444 }; 445 446 static void spinand_ondie_ecc_save_status(struct nand_device *nand, u8 status) 447 { 448 struct spinand_ondie_ecc_conf *engine_conf = nand->ecc.ctx.priv; 449 450 if (nand->ecc.ctx.conf.engine_type == NAND_ECC_ENGINE_TYPE_ON_DIE && 451 engine_conf) 452 engine_conf->status = status; 453 } 454 455 int spinand_write_enable_op(struct spinand_device *spinand) 456 { 457 struct spi_mem_op op = SPINAND_OP(spinand, wr_en); 458 459 return spi_mem_exec_op(spinand->spimem, &op); 460 } 461 462 static int spinand_load_page_op(struct spinand_device *spinand, 463 const struct nand_page_io_req *req) 464 { 465 struct nand_device *nand = spinand_to_nand(spinand); 466 unsigned int row = nanddev_pos_to_row(nand, &req->pos); 467 bool packed = spinand->flags & SPINAND_ODTR_PACKED_PAGE_READ; 468 struct spi_mem_op op = packed ? 469 SPINAND_OP(spinand, page_read_packed, row) : 470 SPINAND_OP(spinand, page_read, row); 471 472 return spi_mem_exec_op(spinand->spimem, &op); 473 } 474 475 static int spinand_read_from_cache_op(struct spinand_device *spinand, 476 const struct nand_page_io_req *req) 477 { 478 struct nand_device *nand = spinand_to_nand(spinand); 479 struct mtd_info *mtd = spinand_to_mtd(spinand); 480 struct spi_mem_dirmap_desc *rdesc; 481 unsigned int nbytes = 0; 482 void *buf = NULL; 483 u16 column = 0; 484 ssize_t ret; 485 486 if (req->datalen) { 487 buf = spinand->databuf; 488 if (!req->continuous) 489 nbytes = nanddev_page_size(nand); 490 else 491 nbytes = round_up(req->dataoffs + req->datalen, 492 nanddev_page_size(nand)); 493 column = 0; 494 } 495 496 if (req->ooblen) { 497 nbytes += nanddev_per_page_oobsize(nand); 498 if (!buf) { 499 buf = spinand->oobbuf; 500 column = nanddev_page_size(nand); 501 } 502 } 503 504 rdesc = spinand->dirmaps[req->pos.plane].rdesc; 505 506 if (nand->ecc.engine->integration == NAND_ECC_ENGINE_INTEGRATION_PIPELINED && 507 req->mode != MTD_OPS_RAW) 508 rdesc->info.op_tmpl->data.ecc = true; 509 else 510 rdesc->info.op_tmpl->data.ecc = false; 511 512 if (spinand->flags & SPINAND_HAS_READ_PLANE_SELECT_BIT) 513 column |= req->pos.plane << fls(nanddev_page_size(nand)); 514 515 while (nbytes) { 516 ret = spi_mem_dirmap_read(rdesc, column, nbytes, buf); 517 if (ret < 0) 518 return ret; 519 520 if (!ret || ret > nbytes) 521 return -EIO; 522 523 nbytes -= ret; 524 column += ret; 525 buf += ret; 526 527 /* 528 * Dirmap accesses are allowed to toggle the CS. 529 * Toggling the CS during a continuous read is forbidden. 530 */ 531 if (nbytes && req->continuous) { 532 /* 533 * Spi controller with broken support of continuous 534 * reading was detected. Disable future use of 535 * continuous reading and return -EAGAIN to retry 536 * reading within regular mode. 537 */ 538 spinand->cont_read_possible = false; 539 return -EAGAIN; 540 } 541 } 542 543 if (req->datalen) 544 memcpy(req->databuf.in, spinand->databuf + req->dataoffs, 545 req->datalen); 546 547 if (req->ooblen) { 548 if (req->mode == MTD_OPS_AUTO_OOB) 549 mtd_ooblayout_get_databytes(mtd, req->oobbuf.in, 550 spinand->oobbuf, 551 req->ooboffs, 552 req->ooblen); 553 else 554 memcpy(req->oobbuf.in, spinand->oobbuf + req->ooboffs, 555 req->ooblen); 556 } 557 558 return 0; 559 } 560 561 static int spinand_write_to_cache_op(struct spinand_device *spinand, 562 const struct nand_page_io_req *req) 563 { 564 struct nand_device *nand = spinand_to_nand(spinand); 565 struct mtd_info *mtd = spinand_to_mtd(spinand); 566 struct spi_mem_dirmap_desc *wdesc; 567 unsigned int nbytes, column = 0; 568 void *buf = spinand->databuf; 569 ssize_t ret; 570 571 /* 572 * Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset 573 * the cache content to 0xFF (depends on vendor implementation), so we 574 * must fill the page cache entirely even if we only want to program 575 * the data portion of the page, otherwise we might corrupt the BBM or 576 * user data previously programmed in OOB area. 577 * 578 * Only reset the data buffer manually, the OOB buffer is prepared by 579 * ECC engines ->prepare_io_req() callback. 580 */ 581 nbytes = nanddev_page_size(nand) + nanddev_per_page_oobsize(nand); 582 memset(spinand->databuf, 0xff, nanddev_page_size(nand)); 583 584 if (req->datalen) 585 memcpy(spinand->databuf + req->dataoffs, req->databuf.out, 586 req->datalen); 587 588 if (req->ooblen) { 589 if (req->mode == MTD_OPS_AUTO_OOB) 590 mtd_ooblayout_set_databytes(mtd, req->oobbuf.out, 591 spinand->oobbuf, 592 req->ooboffs, 593 req->ooblen); 594 else 595 memcpy(spinand->oobbuf + req->ooboffs, req->oobbuf.out, 596 req->ooblen); 597 } 598 599 wdesc = spinand->dirmaps[req->pos.plane].wdesc; 600 601 if (nand->ecc.engine->integration == NAND_ECC_ENGINE_INTEGRATION_PIPELINED && 602 req->mode != MTD_OPS_RAW) 603 wdesc->info.op_tmpl->data.ecc = true; 604 else 605 wdesc->info.op_tmpl->data.ecc = false; 606 607 if (spinand->flags & SPINAND_HAS_PROG_PLANE_SELECT_BIT) 608 column |= req->pos.plane << fls(nanddev_page_size(nand)); 609 610 while (nbytes) { 611 ret = spi_mem_dirmap_write(wdesc, column, nbytes, buf); 612 if (ret < 0) 613 return ret; 614 615 if (!ret || ret > nbytes) 616 return -EIO; 617 618 nbytes -= ret; 619 column += ret; 620 buf += ret; 621 } 622 623 return 0; 624 } 625 626 static int spinand_program_op(struct spinand_device *spinand, 627 const struct nand_page_io_req *req) 628 { 629 struct nand_device *nand = spinand_to_nand(spinand); 630 unsigned int row = nanddev_pos_to_row(nand, &req->pos); 631 struct spi_mem_op op = SPINAND_OP(spinand, prog_exec, row); 632 633 return spi_mem_exec_op(spinand->spimem, &op); 634 } 635 636 static int spinand_erase_op(struct spinand_device *spinand, 637 const struct nand_pos *pos) 638 { 639 struct nand_device *nand = spinand_to_nand(spinand); 640 unsigned int row = nanddev_pos_to_row(nand, pos); 641 struct spi_mem_op op = SPINAND_OP(spinand, blk_erase, row); 642 643 return spi_mem_exec_op(spinand->spimem, &op); 644 } 645 646 /** 647 * spinand_wait() - Poll memory device status 648 * @spinand: the spinand device 649 * @initial_delay_us: delay in us before starting to poll 650 * @poll_delay_us: time to sleep between reads in us 651 * @s: the pointer to variable to store the value of REG_STATUS 652 * 653 * This function polls a status register (REG_STATUS) and returns when 654 * the STATUS_READY bit is 0 or when the timeout has expired. 655 * 656 * Return: 0 on success, a negative error code otherwise. 657 */ 658 int spinand_wait(struct spinand_device *spinand, unsigned long initial_delay_us, 659 unsigned long poll_delay_us, u8 *s) 660 { 661 struct spi_mem_op op = SPINAND_OP(spinand, get_feature, 662 REG_STATUS, spinand->scratchbuf); 663 u8 status; 664 int ret; 665 666 ret = spi_mem_poll_status(spinand->spimem, &op, STATUS_BUSY, 0, 667 initial_delay_us, 668 poll_delay_us, 669 SPINAND_WAITRDY_TIMEOUT_MS); 670 if (ret) 671 return ret; 672 673 status = *spinand->scratchbuf; 674 if (!(status & STATUS_BUSY)) 675 goto out; 676 677 /* 678 * Extra read, just in case the STATUS_READY bit has changed 679 * since our last check 680 */ 681 ret = spinand_read_status(spinand, &status); 682 if (ret) 683 return ret; 684 685 out: 686 if (s) 687 *s = status; 688 689 return status & STATUS_BUSY ? -ETIMEDOUT : 0; 690 } 691 692 static int spinand_read_id_op(struct spinand_device *spinand, u8 naddr, 693 u8 ndummy, u8 *buf) 694 { 695 struct spi_mem_op op = SPINAND_OP(spinand, readid, 696 naddr, ndummy, spinand->scratchbuf, SPINAND_MAX_ID_LEN); 697 int ret; 698 699 ret = spi_mem_exec_op(spinand->spimem, &op); 700 if (!ret) 701 memcpy(buf, spinand->scratchbuf, SPINAND_MAX_ID_LEN); 702 703 return ret; 704 } 705 706 static int spinand_reset_op(struct spinand_device *spinand) 707 { 708 struct spi_mem_op op = SPINAND_OP(spinand, reset); 709 int ret; 710 711 ret = spi_mem_exec_op(spinand->spimem, &op); 712 if (ret) 713 return ret; 714 715 return spinand_wait(spinand, 716 SPINAND_RESET_INITIAL_DELAY_US, 717 SPINAND_RESET_POLL_DELAY_US, 718 NULL); 719 } 720 721 static int spinand_lock_block(struct spinand_device *spinand, u8 lock) 722 { 723 return spinand_write_reg_op(spinand, REG_BLOCK_LOCK, lock); 724 } 725 726 /** 727 * spinand_read_page() - Read a page 728 * @spinand: the spinand device 729 * @req: the I/O request 730 * 731 * Return: 0 or a positive number of bitflips corrected on success. 732 * A negative error code otherwise. 733 */ 734 int spinand_read_page(struct spinand_device *spinand, 735 const struct nand_page_io_req *req) 736 { 737 struct nand_device *nand = spinand_to_nand(spinand); 738 u8 status; 739 int ret; 740 741 ret = nand_ecc_prepare_io_req(nand, (struct nand_page_io_req *)req); 742 if (ret) 743 return ret; 744 745 ret = spinand_load_page_op(spinand, req); 746 if (ret) 747 return ret; 748 749 ret = spinand_wait(spinand, 750 SPINAND_READ_INITIAL_DELAY_US, 751 SPINAND_READ_POLL_DELAY_US, 752 &status); 753 if (ret < 0) 754 return ret; 755 756 spinand_ondie_ecc_save_status(nand, status); 757 758 ret = spinand_read_from_cache_op(spinand, req); 759 if (ret) 760 return ret; 761 762 return nand_ecc_finish_io_req(nand, (struct nand_page_io_req *)req); 763 } 764 765 /** 766 * spinand_write_page() - Write a page 767 * @spinand: the spinand device 768 * @req: the I/O request 769 * 770 * Return: 0 or a positive number of bitflips corrected on success. 771 * A negative error code otherwise. 772 */ 773 int spinand_write_page(struct spinand_device *spinand, 774 const struct nand_page_io_req *req) 775 { 776 struct nand_device *nand = spinand_to_nand(spinand); 777 u8 status; 778 int ret; 779 780 ret = nand_ecc_prepare_io_req(nand, (struct nand_page_io_req *)req); 781 if (ret) 782 return ret; 783 784 ret = spinand_write_enable_op(spinand); 785 if (ret) 786 return ret; 787 788 ret = spinand_write_to_cache_op(spinand, req); 789 if (ret) 790 return ret; 791 792 ret = spinand_program_op(spinand, req); 793 if (ret) 794 return ret; 795 796 ret = spinand_wait(spinand, 797 SPINAND_WRITE_INITIAL_DELAY_US, 798 SPINAND_WRITE_POLL_DELAY_US, 799 &status); 800 if (ret) 801 return ret; 802 803 if (status & STATUS_PROG_FAILED) 804 return -EIO; 805 806 return nand_ecc_finish_io_req(nand, (struct nand_page_io_req *)req); 807 } 808 809 static int spinand_mtd_regular_page_read(struct mtd_info *mtd, loff_t from, 810 struct mtd_oob_ops *ops, 811 unsigned int *max_bitflips) 812 { 813 struct spinand_device *spinand = mtd_to_spinand(mtd); 814 struct nand_device *nand = mtd_to_nanddev(mtd); 815 struct mtd_ecc_stats old_stats; 816 struct nand_io_iter iter; 817 bool disable_ecc = false; 818 bool ecc_failed = false; 819 unsigned int retry_mode = 0; 820 int ret; 821 822 old_stats = mtd->ecc_stats; 823 824 if (ops->mode == MTD_OPS_RAW || !mtd->ooblayout) 825 disable_ecc = true; 826 827 nanddev_io_for_each_page(nand, NAND_PAGE_READ, from, ops, &iter) { 828 if (disable_ecc) 829 iter.req.mode = MTD_OPS_RAW; 830 831 ret = spinand_select_target(spinand, iter.req.pos.target); 832 if (ret) 833 break; 834 835 read_retry: 836 ret = spinand_read_page(spinand, &iter.req); 837 if (ret < 0 && ret != -EBADMSG) 838 break; 839 840 if (ret == -EBADMSG && spinand->set_read_retry) { 841 if (spinand->read_retries && (++retry_mode <= spinand->read_retries)) { 842 ret = spinand->set_read_retry(spinand, retry_mode); 843 if (ret < 0) { 844 spinand->set_read_retry(spinand, 0); 845 return ret; 846 } 847 848 /* Reset ecc_stats; retry */ 849 mtd->ecc_stats = old_stats; 850 goto read_retry; 851 } else { 852 /* No more retry modes; real failure */ 853 ecc_failed = true; 854 } 855 } else if (ret == -EBADMSG) { 856 ecc_failed = true; 857 } else { 858 *max_bitflips = max_t(unsigned int, *max_bitflips, ret); 859 } 860 861 ret = 0; 862 ops->retlen += iter.req.datalen; 863 ops->oobretlen += iter.req.ooblen; 864 865 /* Reset to retry mode 0 */ 866 if (retry_mode) { 867 retry_mode = 0; 868 ret = spinand->set_read_retry(spinand, retry_mode); 869 if (ret < 0) 870 return ret; 871 } 872 } 873 874 if (ecc_failed && !ret) 875 ret = -EBADMSG; 876 877 return ret; 878 } 879 880 static int spinand_mtd_continuous_page_read(struct mtd_info *mtd, loff_t from, 881 struct mtd_oob_ops *ops, 882 unsigned int *max_bitflips) 883 { 884 struct spinand_device *spinand = mtd_to_spinand(mtd); 885 struct nand_device *nand = mtd_to_nanddev(mtd); 886 struct nand_io_iter iter; 887 u8 status; 888 int ret; 889 890 ret = spinand_cont_read_enable(spinand, true); 891 if (ret) 892 return ret; 893 894 /* 895 * The cache is divided into two halves. While one half of the cache has 896 * the requested data, the other half is loaded with the next chunk of data. 897 * Therefore, the host can read out the data continuously from page to page. 898 * Each data read must be a multiple of 4-bytes and full pages should be read; 899 * otherwise, the data output might get out of sequence from one read command 900 * to another. 901 * 902 * Continuous reads never cross LUN boundaries. Some devices don't 903 * support crossing planes boundaries. Some devices don't even support 904 * crossing blocks boundaries. The common case being to read through UBI, 905 * we will very rarely read two consequent blocks or more, so let's only enable 906 * continuous reads when reading within the same erase block. 907 */ 908 nanddev_io_for_each_block(nand, NAND_PAGE_READ, from, ops, &iter) { 909 ret = spinand_select_target(spinand, iter.req.pos.target); 910 if (ret) 911 goto end_cont_read; 912 913 ret = nand_ecc_prepare_io_req(nand, &iter.req); 914 if (ret) 915 goto end_cont_read; 916 917 ret = spinand_load_page_op(spinand, &iter.req); 918 if (ret) 919 goto end_cont_read; 920 921 ret = spinand_wait(spinand, SPINAND_READ_INITIAL_DELAY_US, 922 SPINAND_READ_POLL_DELAY_US, NULL); 923 if (ret < 0) 924 goto end_cont_read; 925 926 ret = spinand_read_from_cache_op(spinand, &iter.req); 927 if (ret) 928 goto end_cont_read; 929 930 ops->retlen += iter.req.datalen; 931 932 ret = spinand_read_status(spinand, &status); 933 if (ret) 934 goto end_cont_read; 935 936 spinand_ondie_ecc_save_status(nand, status); 937 938 ret = nand_ecc_finish_io_req(nand, &iter.req); 939 if (ret < 0) 940 goto end_cont_read; 941 942 *max_bitflips = max_t(unsigned int, *max_bitflips, ret); 943 ret = 0; 944 } 945 946 end_cont_read: 947 /* 948 * Once all the data has been read out, the host can either pull CS# 949 * high and wait for tRST or manually clear the bit in the configuration 950 * register to terminate the continuous read operation. We have no 951 * guarantee the SPI controller drivers will effectively deassert the CS 952 * when we expect them to, so take the register based approach. 953 */ 954 spinand_cont_read_enable(spinand, false); 955 956 return ret; 957 } 958 959 static void spinand_cont_read_init(struct spinand_device *spinand) 960 { 961 struct nand_device *nand = spinand_to_nand(spinand); 962 enum nand_ecc_engine_type engine_type = nand->ecc.ctx.conf.engine_type; 963 964 /* OOBs cannot be retrieved so external/on-host ECC engine won't work */ 965 if (spinand->set_cont_read && 966 (engine_type == NAND_ECC_ENGINE_TYPE_ON_DIE || 967 engine_type == NAND_ECC_ENGINE_TYPE_NONE)) { 968 spinand->cont_read_possible = true; 969 970 /* 971 * Ensure continuous read is disabled on probe. 972 * Some devices retain this state across soft reset, 973 * which leaves the OOB area inaccessible and results 974 * in false positive returns from spinand_isbad(). 975 */ 976 spinand_cont_read_enable(spinand, false); 977 } 978 } 979 980 static bool spinand_use_cont_read(struct mtd_info *mtd, loff_t from, 981 struct mtd_oob_ops *ops) 982 { 983 struct nand_device *nand = mtd_to_nanddev(mtd); 984 struct spinand_device *spinand = nand_to_spinand(nand); 985 struct nand_pos start_pos, end_pos; 986 987 if (!spinand->cont_read_possible) 988 return false; 989 990 /* OOBs won't be retrieved */ 991 if (ops->ooblen || ops->oobbuf) 992 return false; 993 994 nanddev_offs_to_pos(nand, from, &start_pos); 995 nanddev_offs_to_pos(nand, from + ops->len - 1, &end_pos); 996 997 return start_pos.page < end_pos.page; 998 } 999 1000 static int spinand_mtd_read(struct mtd_info *mtd, loff_t from, 1001 struct mtd_oob_ops *ops) 1002 { 1003 struct spinand_device *spinand = mtd_to_spinand(mtd); 1004 struct mtd_ecc_stats old_stats; 1005 unsigned int max_bitflips = 0; 1006 int ret; 1007 1008 mutex_lock(&spinand->lock); 1009 1010 old_stats = mtd->ecc_stats; 1011 1012 if (spinand_use_cont_read(mtd, from, ops)) { 1013 ret = spinand_mtd_continuous_page_read(mtd, from, ops, &max_bitflips); 1014 if (ret == -EAGAIN && !spinand->cont_read_possible) { 1015 /* 1016 * Spi controller with broken support of continuous 1017 * reading was detected (see spinand_read_from_cache_op()), 1018 * repeat reading in regular mode. 1019 */ 1020 ret = spinand_mtd_regular_page_read(mtd, from, ops, &max_bitflips); 1021 } 1022 } else { 1023 ret = spinand_mtd_regular_page_read(mtd, from, ops, &max_bitflips); 1024 } 1025 1026 if (ops->stats) { 1027 ops->stats->uncorrectable_errors += 1028 mtd->ecc_stats.failed - old_stats.failed; 1029 ops->stats->corrected_bitflips += 1030 mtd->ecc_stats.corrected - old_stats.corrected; 1031 } 1032 1033 mutex_unlock(&spinand->lock); 1034 1035 return ret ? ret : max_bitflips; 1036 } 1037 1038 static int spinand_mtd_write(struct mtd_info *mtd, loff_t to, 1039 struct mtd_oob_ops *ops) 1040 { 1041 struct spinand_device *spinand = mtd_to_spinand(mtd); 1042 struct nand_device *nand = mtd_to_nanddev(mtd); 1043 struct nand_io_iter iter; 1044 bool disable_ecc = false; 1045 int ret = 0; 1046 1047 if (ops->mode == MTD_OPS_RAW || !mtd->ooblayout) 1048 disable_ecc = true; 1049 1050 mutex_lock(&spinand->lock); 1051 1052 nanddev_io_for_each_page(nand, NAND_PAGE_WRITE, to, ops, &iter) { 1053 if (disable_ecc) 1054 iter.req.mode = MTD_OPS_RAW; 1055 1056 ret = spinand_select_target(spinand, iter.req.pos.target); 1057 if (ret) 1058 break; 1059 1060 ret = spinand_write_page(spinand, &iter.req); 1061 if (ret) 1062 break; 1063 1064 ops->retlen += iter.req.datalen; 1065 ops->oobretlen += iter.req.ooblen; 1066 } 1067 1068 mutex_unlock(&spinand->lock); 1069 1070 return ret; 1071 } 1072 1073 static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos) 1074 { 1075 struct spinand_device *spinand = nand_to_spinand(nand); 1076 u8 marker[2] = { }; 1077 struct nand_page_io_req req = { 1078 .pos = *pos, 1079 .ooblen = sizeof(marker), 1080 .ooboffs = 0, 1081 .oobbuf.in = marker, 1082 .mode = MTD_OPS_RAW, 1083 }; 1084 int ret; 1085 1086 spinand_select_target(spinand, pos->target); 1087 1088 ret = spinand_read_page(spinand, &req); 1089 if (ret == -EOPNOTSUPP) { 1090 /* Retry with ECC in case raw access is not supported */ 1091 req.mode = MTD_OPS_PLACE_OOB; 1092 spinand_read_page(spinand, &req); 1093 } 1094 1095 if (marker[0] != 0xff || marker[1] != 0xff) 1096 return true; 1097 1098 return false; 1099 } 1100 1101 static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs) 1102 { 1103 struct nand_device *nand = mtd_to_nanddev(mtd); 1104 struct spinand_device *spinand = nand_to_spinand(nand); 1105 struct nand_pos pos; 1106 int ret; 1107 1108 nanddev_offs_to_pos(nand, offs, &pos); 1109 mutex_lock(&spinand->lock); 1110 ret = nanddev_isbad(nand, &pos); 1111 mutex_unlock(&spinand->lock); 1112 1113 return ret; 1114 } 1115 1116 static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos) 1117 { 1118 struct spinand_device *spinand = nand_to_spinand(nand); 1119 u8 marker[2] = { }; 1120 struct nand_page_io_req req = { 1121 .pos = *pos, 1122 .ooboffs = 0, 1123 .ooblen = sizeof(marker), 1124 .oobbuf.out = marker, 1125 .mode = MTD_OPS_RAW, 1126 }; 1127 int ret; 1128 1129 ret = spinand_select_target(spinand, pos->target); 1130 if (ret) 1131 return ret; 1132 1133 ret = spinand_write_page(spinand, &req); 1134 if (ret == -EOPNOTSUPP) { 1135 /* Retry with ECC in case raw access is not supported */ 1136 req.mode = MTD_OPS_PLACE_OOB; 1137 ret = spinand_write_page(spinand, &req); 1138 } 1139 1140 return ret; 1141 } 1142 1143 static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs) 1144 { 1145 struct nand_device *nand = mtd_to_nanddev(mtd); 1146 struct spinand_device *spinand = nand_to_spinand(nand); 1147 struct nand_pos pos; 1148 int ret; 1149 1150 nanddev_offs_to_pos(nand, offs, &pos); 1151 mutex_lock(&spinand->lock); 1152 ret = nanddev_markbad(nand, &pos); 1153 mutex_unlock(&spinand->lock); 1154 1155 return ret; 1156 } 1157 1158 static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos) 1159 { 1160 struct spinand_device *spinand = nand_to_spinand(nand); 1161 u8 status; 1162 int ret; 1163 1164 ret = spinand_select_target(spinand, pos->target); 1165 if (ret) 1166 return ret; 1167 1168 ret = spinand_write_enable_op(spinand); 1169 if (ret) 1170 return ret; 1171 1172 ret = spinand_erase_op(spinand, pos); 1173 if (ret) 1174 return ret; 1175 1176 ret = spinand_wait(spinand, 1177 SPINAND_ERASE_INITIAL_DELAY_US, 1178 SPINAND_ERASE_POLL_DELAY_US, 1179 &status); 1180 1181 if (!ret && (status & STATUS_ERASE_FAILED)) 1182 ret = -EIO; 1183 1184 return ret; 1185 } 1186 1187 static int spinand_mtd_erase(struct mtd_info *mtd, 1188 struct erase_info *einfo) 1189 { 1190 struct spinand_device *spinand = mtd_to_spinand(mtd); 1191 int ret; 1192 1193 mutex_lock(&spinand->lock); 1194 ret = nanddev_mtd_erase(mtd, einfo); 1195 mutex_unlock(&spinand->lock); 1196 1197 return ret; 1198 } 1199 1200 static int spinand_mtd_block_isreserved(struct mtd_info *mtd, loff_t offs) 1201 { 1202 struct spinand_device *spinand = mtd_to_spinand(mtd); 1203 struct nand_device *nand = mtd_to_nanddev(mtd); 1204 struct nand_pos pos; 1205 int ret; 1206 1207 nanddev_offs_to_pos(nand, offs, &pos); 1208 mutex_lock(&spinand->lock); 1209 ret = nanddev_isreserved(nand, &pos); 1210 mutex_unlock(&spinand->lock); 1211 1212 return ret; 1213 } 1214 1215 static struct spi_mem_dirmap_desc *spinand_create_rdesc( 1216 struct spinand_device *spinand, 1217 struct spi_mem_dirmap_info *info) 1218 { 1219 struct nand_device *nand = spinand_to_nand(spinand); 1220 struct spi_mem_dirmap_desc *desc = NULL; 1221 1222 if (spinand->cont_read_possible) { 1223 /* 1224 * spi controller may return an error if info->length is 1225 * too large 1226 */ 1227 info->length = nanddev_eraseblock_size(nand); 1228 desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev, 1229 spinand->spimem, info); 1230 } 1231 1232 if (IS_ERR_OR_NULL(desc)) { 1233 /* 1234 * continuous reading is not supported by flash or 1235 * its spi controller, use regular reading 1236 */ 1237 spinand->cont_read_possible = false; 1238 1239 info->length = nanddev_page_size(nand) + 1240 nanddev_per_page_oobsize(nand); 1241 desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev, 1242 spinand->spimem, info); 1243 } 1244 1245 return desc; 1246 } 1247 1248 static int spinand_create_dirmap(struct spinand_device *spinand, 1249 unsigned int plane) 1250 { 1251 struct nand_device *nand = spinand_to_nand(spinand); 1252 struct spi_mem_dirmap_info info = { 0 }; 1253 struct spi_mem_dirmap_desc *desc; 1254 bool enable_ecc = false; 1255 1256 if (nand->ecc.engine->integration == NAND_ECC_ENGINE_INTEGRATION_PIPELINED) 1257 enable_ecc = true; 1258 1259 /* The plane number is passed in MSB just above the column address */ 1260 info.offset = plane << fls(nand->memorg.pagesize); 1261 1262 /* Write descriptor */ 1263 info.length = nanddev_page_size(nand) + nanddev_per_page_oobsize(nand); 1264 info.primary_op_tmpl = *spinand->op_templates->update_cache; 1265 info.primary_op_tmpl.data.ecc = enable_ecc; 1266 desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev, 1267 spinand->spimem, &info); 1268 if (IS_ERR(desc)) 1269 return PTR_ERR(desc); 1270 1271 spinand->dirmaps[plane].wdesc = desc; 1272 1273 /* Read descriptor */ 1274 info.primary_op_tmpl = *spinand->op_templates->read_cache; 1275 info.primary_op_tmpl.data.ecc = enable_ecc; 1276 desc = spinand_create_rdesc(spinand, &info); 1277 if (IS_ERR(desc)) 1278 return PTR_ERR(desc); 1279 1280 spinand->dirmaps[plane].rdesc = desc; 1281 1282 return 0; 1283 } 1284 1285 static int spinand_create_dirmaps(struct spinand_device *spinand) 1286 { 1287 struct nand_device *nand = spinand_to_nand(spinand); 1288 int i, ret; 1289 1290 spinand->dirmaps = devm_kzalloc(&spinand->spimem->spi->dev, 1291 sizeof(*spinand->dirmaps) * 1292 nand->memorg.planes_per_lun, 1293 GFP_KERNEL); 1294 if (!spinand->dirmaps) 1295 return -ENOMEM; 1296 1297 for (i = 0; i < nand->memorg.planes_per_lun; i++) { 1298 ret = spinand_create_dirmap(spinand, i); 1299 if (ret) 1300 return ret; 1301 } 1302 1303 return 0; 1304 } 1305 1306 static const struct nand_ops spinand_ops = { 1307 .erase = spinand_erase, 1308 .markbad = spinand_markbad, 1309 .isbad = spinand_isbad, 1310 }; 1311 1312 static const struct spinand_manufacturer *spinand_manufacturers[] = { 1313 &alliancememory_spinand_manufacturer, 1314 &ato_spinand_manufacturer, 1315 &dosilicon_spinand_manufacturer, 1316 &esmt_8c_spinand_manufacturer, 1317 &esmt_c8_spinand_manufacturer, 1318 &fmsh_spinand_manufacturer, 1319 &foresee_spinand_manufacturer, 1320 &gigadevice_spinand_manufacturer, 1321 ¯onix_spinand_manufacturer, 1322 µn_spinand_manufacturer, 1323 ¶gon_spinand_manufacturer, 1324 &skyhigh_spinand_manufacturer, 1325 &toshiba_spinand_manufacturer, 1326 &winbond_spinand_manufacturer, 1327 &xtx_spinand_manufacturer, 1328 }; 1329 1330 static int spinand_manufacturer_match(struct spinand_device *spinand, 1331 enum spinand_readid_method rdid_method) 1332 { 1333 u8 *id = spinand->id.data; 1334 unsigned int i; 1335 int ret; 1336 1337 for (i = 0; i < ARRAY_SIZE(spinand_manufacturers); i++) { 1338 const struct spinand_manufacturer *manufacturer = 1339 spinand_manufacturers[i]; 1340 1341 if (id[0] != manufacturer->id) 1342 continue; 1343 1344 ret = spinand_match_and_init(spinand, 1345 manufacturer->chips, 1346 manufacturer->nchips, 1347 rdid_method); 1348 if (ret < 0) 1349 continue; 1350 1351 spinand->manufacturer = manufacturer; 1352 return 0; 1353 } 1354 return -EOPNOTSUPP; 1355 } 1356 1357 static int spinand_id_detect(struct spinand_device *spinand) 1358 { 1359 u8 *id = spinand->id.data; 1360 int ret; 1361 1362 ret = spinand_read_id_op(spinand, 0, 0, id); 1363 if (ret) 1364 return ret; 1365 ret = spinand_manufacturer_match(spinand, SPINAND_READID_METHOD_OPCODE); 1366 if (!ret) 1367 return 0; 1368 1369 ret = spinand_read_id_op(spinand, 1, 0, id); 1370 if (ret) 1371 return ret; 1372 ret = spinand_manufacturer_match(spinand, 1373 SPINAND_READID_METHOD_OPCODE_ADDR); 1374 if (!ret) 1375 return 0; 1376 1377 ret = spinand_read_id_op(spinand, 0, 1, id); 1378 if (ret) 1379 return ret; 1380 ret = spinand_manufacturer_match(spinand, 1381 SPINAND_READID_METHOD_OPCODE_DUMMY); 1382 1383 return ret; 1384 } 1385 1386 static int spinand_manufacturer_init(struct spinand_device *spinand) 1387 { 1388 int ret; 1389 1390 if (spinand->manufacturer->ops->init) { 1391 ret = spinand->manufacturer->ops->init(spinand); 1392 if (ret) 1393 return ret; 1394 } 1395 1396 return 0; 1397 } 1398 1399 static void spinand_manufacturer_cleanup(struct spinand_device *spinand) 1400 { 1401 /* Release manufacturer private data */ 1402 if (spinand->manufacturer->ops->cleanup) 1403 return spinand->manufacturer->ops->cleanup(spinand); 1404 } 1405 1406 bool spinand_op_is_odtr(const struct spi_mem_op *op) 1407 { 1408 return op->cmd.dtr && op->cmd.buswidth == 8; 1409 } 1410 1411 static void spinand_init_ssdr_templates(struct spinand_device *spinand) 1412 { 1413 struct spinand_mem_ops *tmpl = &spinand->ssdr_op_templates; 1414 1415 tmpl->reset = (struct spi_mem_op)SPINAND_RESET_1S_0_0_OP; 1416 tmpl->readid = (struct spi_mem_op)SPINAND_READID_1S_1S_1S_OP(0, 0, NULL, 0); 1417 tmpl->wr_en = (struct spi_mem_op)SPINAND_WR_EN_1S_0_0_OP; 1418 tmpl->wr_dis = (struct spi_mem_op)SPINAND_WR_DIS_1S_0_0_OP; 1419 tmpl->set_feature = (struct spi_mem_op)SPINAND_SET_FEATURE_1S_1S_1S_OP(0, NULL); 1420 tmpl->get_feature = (struct spi_mem_op)SPINAND_GET_FEATURE_1S_1S_1S_OP(0, NULL); 1421 tmpl->blk_erase = (struct spi_mem_op)SPINAND_BLK_ERASE_1S_1S_0_OP(0); 1422 tmpl->page_read = (struct spi_mem_op)SPINAND_PAGE_READ_1S_1S_0_OP(0); 1423 tmpl->prog_exec = (struct spi_mem_op)SPINAND_PROG_EXEC_1S_1S_0_OP(0); 1424 spinand->op_templates = &spinand->ssdr_op_templates; 1425 spinand->bus_iface = SSDR; 1426 } 1427 1428 static int spinand_support_vendor_ops(struct spinand_device *spinand, 1429 const struct spinand_info *info, 1430 enum spinand_bus_interface iface) 1431 { 1432 int i; 1433 1434 if (!info->vendor_ops) 1435 return 0; 1436 /* 1437 * The vendor ops array is only used in order to verify this chip and all its memory 1438 * operations are supported. If we see patterns emerging, we could ideally name these 1439 * operations and define them at the SPI NAND core level instead. 1440 * For now, this only serves as a sanity check. 1441 */ 1442 for (i = 0; i < info->vendor_ops->nops; i++) { 1443 const struct spi_mem_op *op = &info->vendor_ops->ops[i]; 1444 1445 if ((iface == SSDR && spinand_op_is_odtr(op)) || 1446 (iface == ODTR && !spinand_op_is_odtr(op))) 1447 continue; 1448 1449 if (!spi_mem_supports_op(spinand->spimem, op)) 1450 return -EOPNOTSUPP; 1451 } 1452 1453 return 0; 1454 } 1455 1456 static int spinand_init_odtr_instruction_set(struct spinand_device *spinand) 1457 { 1458 struct spinand_mem_ops *tmpl = &spinand->odtr_op_templates; 1459 1460 tmpl->reset = (struct spi_mem_op)SPINAND_RESET_8D_0_0_OP; 1461 if (!spi_mem_supports_op(spinand->spimem, &tmpl->reset)) 1462 return -EOPNOTSUPP; 1463 1464 tmpl->readid = (struct spi_mem_op)SPINAND_READID_8D_8D_8D_OP(0, 0, NULL, 0); 1465 if (!spi_mem_supports_op(spinand->spimem, &tmpl->readid)) 1466 return -EOPNOTSUPP; 1467 1468 tmpl->wr_en = (struct spi_mem_op)SPINAND_WR_EN_8D_0_0_OP; 1469 if (!spi_mem_supports_op(spinand->spimem, &tmpl->wr_en)) 1470 return -EOPNOTSUPP; 1471 1472 tmpl->wr_dis = (struct spi_mem_op)SPINAND_WR_DIS_8D_0_0_OP; 1473 if (!spi_mem_supports_op(spinand->spimem, &tmpl->wr_dis)) 1474 return -EOPNOTSUPP; 1475 1476 tmpl->set_feature = (struct spi_mem_op)SPINAND_SET_FEATURE_8D_8D_8D_OP(0, NULL); 1477 if (!spi_mem_supports_op(spinand->spimem, &tmpl->set_feature)) 1478 return -EOPNOTSUPP; 1479 1480 tmpl->get_feature = (struct spi_mem_op)SPINAND_GET_FEATURE_8D_8D_8D_OP(0, NULL); 1481 if (!spi_mem_supports_op(spinand->spimem, &tmpl->get_feature)) 1482 return -EOPNOTSUPP; 1483 1484 tmpl->blk_erase = (struct spi_mem_op)SPINAND_BLK_ERASE_8D_8D_0_OP(0); 1485 if (!spi_mem_supports_op(spinand->spimem, &tmpl->blk_erase)) 1486 return -EOPNOTSUPP; 1487 1488 if (spinand->flags & SPINAND_ODTR_PACKED_PAGE_READ) 1489 tmpl->page_read = (struct spi_mem_op)SPINAND_PAGE_READ_PACKED_8D_8D_0_OP(0); 1490 else 1491 tmpl->page_read = (struct spi_mem_op)SPINAND_PAGE_READ_8D_8D_0_OP(0); 1492 if (!spi_mem_supports_op(spinand->spimem, &tmpl->page_read)) { 1493 return -EOPNOTSUPP; 1494 } 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