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 (spinand->op_templates->cont_read_cache && req->continuous) 507 rdesc->info.op_tmpl = &rdesc->info.secondary_op_tmpl; 508 else 509 rdesc->info.op_tmpl = &rdesc->info.primary_op_tmpl; 510 511 if (nand->ecc.engine->integration == NAND_ECC_ENGINE_INTEGRATION_PIPELINED && 512 req->mode != MTD_OPS_RAW) 513 rdesc->info.op_tmpl->data.ecc = true; 514 else 515 rdesc->info.op_tmpl->data.ecc = false; 516 517 if (spinand->flags & SPINAND_HAS_READ_PLANE_SELECT_BIT) 518 column |= req->pos.plane << fls(nanddev_page_size(nand)); 519 520 while (nbytes) { 521 ret = spi_mem_dirmap_read(rdesc, column, nbytes, buf); 522 if (ret < 0) 523 return ret; 524 525 if (!ret || ret > nbytes) 526 return -EIO; 527 528 nbytes -= ret; 529 column += ret; 530 buf += ret; 531 532 /* 533 * Dirmap accesses are allowed to toggle the CS. 534 * Toggling the CS during a continuous read is forbidden. 535 */ 536 if (nbytes && req->continuous) { 537 /* 538 * Spi controller with broken support of continuous 539 * reading was detected. Disable future use of 540 * continuous reading and return -EAGAIN to retry 541 * reading within regular mode. 542 */ 543 spinand->cont_read_possible = false; 544 return -EAGAIN; 545 } 546 } 547 548 if (req->datalen) 549 memcpy(req->databuf.in, spinand->databuf + req->dataoffs, 550 req->datalen); 551 552 if (req->ooblen) { 553 if (req->mode == MTD_OPS_AUTO_OOB) 554 mtd_ooblayout_get_databytes(mtd, req->oobbuf.in, 555 spinand->oobbuf, 556 req->ooboffs, 557 req->ooblen); 558 else 559 memcpy(req->oobbuf.in, spinand->oobbuf + req->ooboffs, 560 req->ooblen); 561 } 562 563 return 0; 564 } 565 566 static int spinand_write_to_cache_op(struct spinand_device *spinand, 567 const struct nand_page_io_req *req) 568 { 569 struct nand_device *nand = spinand_to_nand(spinand); 570 struct mtd_info *mtd = spinand_to_mtd(spinand); 571 struct spi_mem_dirmap_desc *wdesc; 572 unsigned int nbytes, column = 0; 573 void *buf = spinand->databuf; 574 ssize_t ret; 575 576 /* 577 * Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset 578 * the cache content to 0xFF (depends on vendor implementation), so we 579 * must fill the page cache entirely even if we only want to program 580 * the data portion of the page, otherwise we might corrupt the BBM or 581 * user data previously programmed in OOB area. 582 * 583 * Only reset the data buffer manually, the OOB buffer is prepared by 584 * ECC engines ->prepare_io_req() callback. 585 */ 586 nbytes = nanddev_page_size(nand) + nanddev_per_page_oobsize(nand); 587 memset(spinand->databuf, 0xff, nanddev_page_size(nand)); 588 589 if (req->datalen) 590 memcpy(spinand->databuf + req->dataoffs, req->databuf.out, 591 req->datalen); 592 593 if (req->ooblen) { 594 if (req->mode == MTD_OPS_AUTO_OOB) 595 mtd_ooblayout_set_databytes(mtd, req->oobbuf.out, 596 spinand->oobbuf, 597 req->ooboffs, 598 req->ooblen); 599 else 600 memcpy(spinand->oobbuf + req->ooboffs, req->oobbuf.out, 601 req->ooblen); 602 } 603 604 wdesc = spinand->dirmaps[req->pos.plane].wdesc; 605 606 if (nand->ecc.engine->integration == NAND_ECC_ENGINE_INTEGRATION_PIPELINED && 607 req->mode != MTD_OPS_RAW) 608 wdesc->info.op_tmpl->data.ecc = true; 609 else 610 wdesc->info.op_tmpl->data.ecc = false; 611 612 if (spinand->flags & SPINAND_HAS_PROG_PLANE_SELECT_BIT) 613 column |= req->pos.plane << fls(nanddev_page_size(nand)); 614 615 while (nbytes) { 616 ret = spi_mem_dirmap_write(wdesc, column, nbytes, buf); 617 if (ret < 0) 618 return ret; 619 620 if (!ret || ret > nbytes) 621 return -EIO; 622 623 nbytes -= ret; 624 column += ret; 625 buf += ret; 626 } 627 628 return 0; 629 } 630 631 static int spinand_program_op(struct spinand_device *spinand, 632 const struct nand_page_io_req *req) 633 { 634 struct nand_device *nand = spinand_to_nand(spinand); 635 unsigned int row = nanddev_pos_to_row(nand, &req->pos); 636 struct spi_mem_op op = SPINAND_OP(spinand, prog_exec, row); 637 638 return spi_mem_exec_op(spinand->spimem, &op); 639 } 640 641 static int spinand_erase_op(struct spinand_device *spinand, 642 const struct nand_pos *pos) 643 { 644 struct nand_device *nand = spinand_to_nand(spinand); 645 unsigned int row = nanddev_pos_to_row(nand, pos); 646 struct spi_mem_op op = SPINAND_OP(spinand, blk_erase, row); 647 648 return spi_mem_exec_op(spinand->spimem, &op); 649 } 650 651 /** 652 * spinand_wait() - Poll memory device status 653 * @spinand: the spinand device 654 * @initial_delay_us: delay in us before starting to poll 655 * @poll_delay_us: time to sleep between reads in us 656 * @s: the pointer to variable to store the value of REG_STATUS 657 * 658 * This function polls a status register (REG_STATUS) and returns when 659 * the STATUS_READY bit is 0 or when the timeout has expired. 660 * 661 * Return: 0 on success, a negative error code otherwise. 662 */ 663 int spinand_wait(struct spinand_device *spinand, unsigned long initial_delay_us, 664 unsigned long poll_delay_us, u8 *s) 665 { 666 struct spi_mem_op op = SPINAND_OP(spinand, get_feature, 667 REG_STATUS, spinand->scratchbuf); 668 u8 status; 669 int ret; 670 671 ret = spi_mem_poll_status(spinand->spimem, &op, STATUS_BUSY, 0, 672 initial_delay_us, 673 poll_delay_us, 674 SPINAND_WAITRDY_TIMEOUT_MS); 675 if (ret) 676 return ret; 677 678 status = *spinand->scratchbuf; 679 if (!(status & STATUS_BUSY)) 680 goto out; 681 682 /* 683 * Extra read, just in case the STATUS_READY bit has changed 684 * since our last check 685 */ 686 ret = spinand_read_status(spinand, &status); 687 if (ret) 688 return ret; 689 690 out: 691 if (s) 692 *s = status; 693 694 return status & STATUS_BUSY ? -ETIMEDOUT : 0; 695 } 696 697 static int spinand_read_id_op(struct spinand_device *spinand, u8 naddr, 698 u8 ndummy, u8 *buf) 699 { 700 struct spi_mem_op op = SPINAND_OP(spinand, readid, 701 naddr, ndummy, spinand->scratchbuf, SPINAND_MAX_ID_LEN); 702 int ret; 703 704 ret = spi_mem_exec_op(spinand->spimem, &op); 705 if (!ret) 706 memcpy(buf, spinand->scratchbuf, SPINAND_MAX_ID_LEN); 707 708 return ret; 709 } 710 711 static int spinand_reset_op(struct spinand_device *spinand) 712 { 713 struct spi_mem_op op = SPINAND_OP(spinand, reset); 714 int ret; 715 716 ret = spi_mem_exec_op(spinand->spimem, &op); 717 if (ret) 718 return ret; 719 720 return spinand_wait(spinand, 721 SPINAND_RESET_INITIAL_DELAY_US, 722 SPINAND_RESET_POLL_DELAY_US, 723 NULL); 724 } 725 726 static int spinand_lock_block(struct spinand_device *spinand, u8 lock) 727 { 728 return spinand_write_reg_op(spinand, REG_BLOCK_LOCK, lock); 729 } 730 731 /** 732 * spinand_read_page() - Read a page 733 * @spinand: the spinand device 734 * @req: the I/O request 735 * 736 * Return: 0 or a positive number of bitflips corrected on success. 737 * A negative error code otherwise. 738 */ 739 int spinand_read_page(struct spinand_device *spinand, 740 const struct nand_page_io_req *req) 741 { 742 struct nand_device *nand = spinand_to_nand(spinand); 743 u8 status; 744 int ret; 745 746 ret = nand_ecc_prepare_io_req(nand, (struct nand_page_io_req *)req); 747 if (ret) 748 return ret; 749 750 ret = spinand_load_page_op(spinand, req); 751 if (ret) 752 return ret; 753 754 ret = spinand_wait(spinand, 755 SPINAND_READ_INITIAL_DELAY_US, 756 SPINAND_READ_POLL_DELAY_US, 757 &status); 758 if (ret < 0) 759 return ret; 760 761 spinand_ondie_ecc_save_status(nand, status); 762 763 ret = spinand_read_from_cache_op(spinand, req); 764 if (ret) 765 return ret; 766 767 return nand_ecc_finish_io_req(nand, (struct nand_page_io_req *)req); 768 } 769 770 /** 771 * spinand_write_page() - Write a page 772 * @spinand: the spinand device 773 * @req: the I/O request 774 * 775 * Return: 0 or a positive number of bitflips corrected on success. 776 * A negative error code otherwise. 777 */ 778 int spinand_write_page(struct spinand_device *spinand, 779 const struct nand_page_io_req *req) 780 { 781 struct nand_device *nand = spinand_to_nand(spinand); 782 u8 status; 783 int ret; 784 785 ret = nand_ecc_prepare_io_req(nand, (struct nand_page_io_req *)req); 786 if (ret) 787 return ret; 788 789 ret = spinand_write_enable_op(spinand); 790 if (ret) 791 return ret; 792 793 ret = spinand_write_to_cache_op(spinand, req); 794 if (ret) 795 return ret; 796 797 ret = spinand_program_op(spinand, req); 798 if (ret) 799 return ret; 800 801 ret = spinand_wait(spinand, 802 SPINAND_WRITE_INITIAL_DELAY_US, 803 SPINAND_WRITE_POLL_DELAY_US, 804 &status); 805 if (ret) 806 return ret; 807 808 if (status & STATUS_PROG_FAILED) 809 return -EIO; 810 811 return nand_ecc_finish_io_req(nand, (struct nand_page_io_req *)req); 812 } 813 814 static int spinand_mtd_regular_page_read(struct mtd_info *mtd, loff_t from, 815 struct mtd_oob_ops *ops, 816 unsigned int *max_bitflips) 817 { 818 struct spinand_device *spinand = mtd_to_spinand(mtd); 819 struct nand_device *nand = mtd_to_nanddev(mtd); 820 struct mtd_ecc_stats old_stats; 821 struct nand_io_iter iter; 822 bool disable_ecc = false; 823 bool ecc_failed = false; 824 unsigned int retry_mode = 0; 825 int ret; 826 827 old_stats = mtd->ecc_stats; 828 829 if (ops->mode == MTD_OPS_RAW || !mtd->ooblayout) 830 disable_ecc = true; 831 832 nanddev_io_for_each_page(nand, NAND_PAGE_READ, from, ops, &iter) { 833 if (disable_ecc) 834 iter.req.mode = MTD_OPS_RAW; 835 836 ret = spinand_select_target(spinand, iter.req.pos.target); 837 if (ret) 838 break; 839 840 read_retry: 841 ret = spinand_read_page(spinand, &iter.req); 842 if (ret < 0 && ret != -EBADMSG) 843 break; 844 845 if (ret == -EBADMSG && spinand->set_read_retry) { 846 if (spinand->read_retries && (++retry_mode <= spinand->read_retries)) { 847 ret = spinand->set_read_retry(spinand, retry_mode); 848 if (ret < 0) { 849 spinand->set_read_retry(spinand, 0); 850 return ret; 851 } 852 853 /* Reset ecc_stats; retry */ 854 mtd->ecc_stats = old_stats; 855 goto read_retry; 856 } else { 857 /* No more retry modes; real failure */ 858 ecc_failed = true; 859 } 860 } else if (ret == -EBADMSG) { 861 ecc_failed = true; 862 } else { 863 *max_bitflips = max_t(unsigned int, *max_bitflips, ret); 864 } 865 866 ret = 0; 867 ops->retlen += iter.req.datalen; 868 ops->oobretlen += iter.req.ooblen; 869 870 /* Reset to retry mode 0 */ 871 if (retry_mode) { 872 retry_mode = 0; 873 ret = spinand->set_read_retry(spinand, retry_mode); 874 if (ret < 0) 875 return ret; 876 } 877 } 878 879 if (ecc_failed && !ret) 880 ret = -EBADMSG; 881 882 return ret; 883 } 884 885 static int spinand_mtd_continuous_page_read(struct mtd_info *mtd, loff_t from, 886 struct mtd_oob_ops *ops, 887 unsigned int *max_bitflips) 888 { 889 struct spinand_device *spinand = mtd_to_spinand(mtd); 890 struct nand_device *nand = mtd_to_nanddev(mtd); 891 struct nand_io_iter iter; 892 u8 status; 893 int ret; 894 895 ret = spinand_cont_read_enable(spinand, true); 896 if (ret) 897 return ret; 898 899 /* 900 * The cache is divided into two halves. While one half of the cache has 901 * the requested data, the other half is loaded with the next chunk of data. 902 * Therefore, the host can read out the data continuously from page to page. 903 * Each data read must be a multiple of 4-bytes and full pages should be read; 904 * otherwise, the data output might get out of sequence from one read command 905 * to another. 906 * 907 * Continuous reads never cross LUN boundaries. Some devices don't 908 * support crossing planes boundaries. Some devices don't even support 909 * crossing blocks boundaries. The common case being to read through UBI, 910 * we will very rarely read two consequent blocks or more, so let's only enable 911 * continuous reads when reading within the same erase block. 912 */ 913 nanddev_io_for_each_block(nand, NAND_PAGE_READ, from, ops, &iter) { 914 ret = spinand_select_target(spinand, iter.req.pos.target); 915 if (ret) 916 goto end_cont_read; 917 918 ret = nand_ecc_prepare_io_req(nand, &iter.req); 919 if (ret) 920 goto end_cont_read; 921 922 ret = spinand_load_page_op(spinand, &iter.req); 923 if (ret) 924 goto end_cont_read; 925 926 ret = spinand_wait(spinand, SPINAND_READ_INITIAL_DELAY_US, 927 SPINAND_READ_POLL_DELAY_US, NULL); 928 if (ret < 0) 929 goto end_cont_read; 930 931 ret = spinand_read_from_cache_op(spinand, &iter.req); 932 if (ret) 933 goto end_cont_read; 934 935 ops->retlen += iter.req.datalen; 936 937 ret = spinand_read_status(spinand, &status); 938 if (ret) 939 goto end_cont_read; 940 941 spinand_ondie_ecc_save_status(nand, status); 942 943 ret = nand_ecc_finish_io_req(nand, &iter.req); 944 if (ret < 0) 945 goto end_cont_read; 946 947 *max_bitflips = max_t(unsigned int, *max_bitflips, ret); 948 ret = 0; 949 } 950 951 end_cont_read: 952 /* 953 * Once all the data has been read out, the host can either pull CS# 954 * high and wait for tRST or manually clear the bit in the configuration 955 * register to terminate the continuous read operation. We have no 956 * guarantee the SPI controller drivers will effectively deassert the CS 957 * when we expect them to, so take the register based approach. 958 */ 959 spinand_cont_read_enable(spinand, false); 960 961 return ret; 962 } 963 964 static void spinand_cont_read_init(struct spinand_device *spinand) 965 { 966 struct nand_device *nand = spinand_to_nand(spinand); 967 enum nand_ecc_engine_type engine_type = nand->ecc.ctx.conf.engine_type; 968 struct spi_controller *ctlr = spinand->spimem->spi->controller; 969 970 /* OOBs cannot be retrieved so external/on-host ECC engine won't work */ 971 if (spinand->set_cont_read) { 972 /* 973 * Ensure continuous read is disabled on probe. 974 * Some devices retain this state across soft reset, 975 * which leaves the OOB area inaccessible and results 976 * in false positive returns from spinand_isbad(). 977 */ 978 spinand_cont_read_enable(spinand, false); 979 980 if ((engine_type == NAND_ECC_ENGINE_TYPE_ON_DIE || 981 engine_type == NAND_ECC_ENGINE_TYPE_NONE) && 982 !spi_mem_controller_is_capable(ctlr, no_cs_assertion)) 983 spinand->cont_read_possible = true; 984 } 985 } 986 987 static bool spinand_use_cont_read(struct mtd_info *mtd, loff_t from, 988 struct mtd_oob_ops *ops) 989 { 990 struct nand_device *nand = mtd_to_nanddev(mtd); 991 struct spinand_device *spinand = nand_to_spinand(nand); 992 struct nand_pos start_pos, end_pos; 993 994 if (!spinand->cont_read_possible) 995 return false; 996 997 /* OOBs won't be retrieved */ 998 if (ops->ooblen || ops->oobbuf) 999 return false; 1000 1001 nanddev_offs_to_pos(nand, from, &start_pos); 1002 nanddev_offs_to_pos(nand, from + ops->len - 1, &end_pos); 1003 1004 return start_pos.page < end_pos.page; 1005 } 1006 1007 static int spinand_mtd_read(struct mtd_info *mtd, loff_t from, 1008 struct mtd_oob_ops *ops) 1009 { 1010 struct spinand_device *spinand = mtd_to_spinand(mtd); 1011 struct mtd_ecc_stats old_stats; 1012 unsigned int max_bitflips = 0; 1013 int ret; 1014 1015 mutex_lock(&spinand->lock); 1016 1017 old_stats = mtd->ecc_stats; 1018 1019 if (spinand_use_cont_read(mtd, from, ops)) { 1020 ret = spinand_mtd_continuous_page_read(mtd, from, ops, &max_bitflips); 1021 if (ret == -EAGAIN && !spinand->cont_read_possible) { 1022 /* 1023 * Spi controller with broken support of continuous 1024 * reading was detected (see spinand_read_from_cache_op()), 1025 * repeat reading in regular mode. 1026 */ 1027 ret = spinand_mtd_regular_page_read(mtd, from, ops, &max_bitflips); 1028 } 1029 } else { 1030 ret = spinand_mtd_regular_page_read(mtd, from, ops, &max_bitflips); 1031 } 1032 1033 if (ops->stats) { 1034 ops->stats->uncorrectable_errors += 1035 mtd->ecc_stats.failed - old_stats.failed; 1036 ops->stats->corrected_bitflips += 1037 mtd->ecc_stats.corrected - old_stats.corrected; 1038 } 1039 1040 mutex_unlock(&spinand->lock); 1041 1042 return ret ? ret : max_bitflips; 1043 } 1044 1045 static int spinand_mtd_write(struct mtd_info *mtd, loff_t to, 1046 struct mtd_oob_ops *ops) 1047 { 1048 struct spinand_device *spinand = mtd_to_spinand(mtd); 1049 struct nand_device *nand = mtd_to_nanddev(mtd); 1050 struct nand_io_iter iter; 1051 bool disable_ecc = false; 1052 int ret = 0; 1053 1054 if (ops->mode == MTD_OPS_RAW || !mtd->ooblayout) 1055 disable_ecc = true; 1056 1057 mutex_lock(&spinand->lock); 1058 1059 nanddev_io_for_each_page(nand, NAND_PAGE_WRITE, to, ops, &iter) { 1060 if (disable_ecc) 1061 iter.req.mode = MTD_OPS_RAW; 1062 1063 ret = spinand_select_target(spinand, iter.req.pos.target); 1064 if (ret) 1065 break; 1066 1067 ret = spinand_write_page(spinand, &iter.req); 1068 if (ret) 1069 break; 1070 1071 ops->retlen += iter.req.datalen; 1072 ops->oobretlen += iter.req.ooblen; 1073 } 1074 1075 mutex_unlock(&spinand->lock); 1076 1077 return ret; 1078 } 1079 1080 static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos) 1081 { 1082 struct spinand_device *spinand = nand_to_spinand(nand); 1083 u8 marker[2] = { }; 1084 struct nand_page_io_req req = { 1085 .pos = *pos, 1086 .ooblen = sizeof(marker), 1087 .ooboffs = 0, 1088 .oobbuf.in = marker, 1089 .mode = MTD_OPS_RAW, 1090 }; 1091 int ret; 1092 1093 spinand_select_target(spinand, pos->target); 1094 1095 ret = spinand_read_page(spinand, &req); 1096 if (ret == -EOPNOTSUPP) { 1097 /* Retry with ECC in case raw access is not supported */ 1098 req.mode = MTD_OPS_PLACE_OOB; 1099 spinand_read_page(spinand, &req); 1100 } 1101 1102 if (marker[0] != 0xff || marker[1] != 0xff) 1103 return true; 1104 1105 return false; 1106 } 1107 1108 static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs) 1109 { 1110 struct nand_device *nand = mtd_to_nanddev(mtd); 1111 struct spinand_device *spinand = nand_to_spinand(nand); 1112 struct nand_pos pos; 1113 int ret; 1114 1115 nanddev_offs_to_pos(nand, offs, &pos); 1116 mutex_lock(&spinand->lock); 1117 ret = nanddev_isbad(nand, &pos); 1118 mutex_unlock(&spinand->lock); 1119 1120 return ret; 1121 } 1122 1123 static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos) 1124 { 1125 struct spinand_device *spinand = nand_to_spinand(nand); 1126 u8 marker[2] = { }; 1127 struct nand_page_io_req req = { 1128 .pos = *pos, 1129 .ooboffs = 0, 1130 .ooblen = sizeof(marker), 1131 .oobbuf.out = marker, 1132 .mode = MTD_OPS_RAW, 1133 }; 1134 int ret; 1135 1136 ret = spinand_select_target(spinand, pos->target); 1137 if (ret) 1138 return ret; 1139 1140 ret = spinand_write_page(spinand, &req); 1141 if (ret == -EOPNOTSUPP) { 1142 /* Retry with ECC in case raw access is not supported */ 1143 req.mode = MTD_OPS_PLACE_OOB; 1144 ret = spinand_write_page(spinand, &req); 1145 } 1146 1147 return ret; 1148 } 1149 1150 static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs) 1151 { 1152 struct nand_device *nand = mtd_to_nanddev(mtd); 1153 struct spinand_device *spinand = nand_to_spinand(nand); 1154 struct nand_pos pos; 1155 int ret; 1156 1157 nanddev_offs_to_pos(nand, offs, &pos); 1158 mutex_lock(&spinand->lock); 1159 ret = nanddev_markbad(nand, &pos); 1160 mutex_unlock(&spinand->lock); 1161 1162 return ret; 1163 } 1164 1165 static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos) 1166 { 1167 struct spinand_device *spinand = nand_to_spinand(nand); 1168 u8 status; 1169 int ret; 1170 1171 ret = spinand_select_target(spinand, pos->target); 1172 if (ret) 1173 return ret; 1174 1175 ret = spinand_write_enable_op(spinand); 1176 if (ret) 1177 return ret; 1178 1179 ret = spinand_erase_op(spinand, pos); 1180 if (ret) 1181 return ret; 1182 1183 ret = spinand_wait(spinand, 1184 SPINAND_ERASE_INITIAL_DELAY_US, 1185 SPINAND_ERASE_POLL_DELAY_US, 1186 &status); 1187 1188 if (!ret && (status & STATUS_ERASE_FAILED)) 1189 ret = -EIO; 1190 1191 return ret; 1192 } 1193 1194 static int spinand_mtd_erase(struct mtd_info *mtd, 1195 struct erase_info *einfo) 1196 { 1197 struct spinand_device *spinand = mtd_to_spinand(mtd); 1198 int ret; 1199 1200 mutex_lock(&spinand->lock); 1201 ret = nanddev_mtd_erase(mtd, einfo); 1202 mutex_unlock(&spinand->lock); 1203 1204 return ret; 1205 } 1206 1207 static int spinand_mtd_block_isreserved(struct mtd_info *mtd, loff_t offs) 1208 { 1209 struct spinand_device *spinand = mtd_to_spinand(mtd); 1210 struct nand_device *nand = mtd_to_nanddev(mtd); 1211 struct nand_pos pos; 1212 int ret; 1213 1214 nanddev_offs_to_pos(nand, offs, &pos); 1215 mutex_lock(&spinand->lock); 1216 ret = nanddev_isreserved(nand, &pos); 1217 mutex_unlock(&spinand->lock); 1218 1219 return ret; 1220 } 1221 1222 static struct spi_mem_dirmap_desc *spinand_create_rdesc( 1223 struct spinand_device *spinand, 1224 struct spi_mem_dirmap_info *info) 1225 { 1226 struct nand_device *nand = spinand_to_nand(spinand); 1227 struct spi_mem_dirmap_desc *desc = NULL; 1228 1229 if (spinand->cont_read_possible) { 1230 /* 1231 * spi controller may return an error if info->length is 1232 * too large 1233 */ 1234 info->length = nanddev_eraseblock_size(nand); 1235 desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev, 1236 spinand->spimem, info); 1237 } 1238 1239 if (IS_ERR_OR_NULL(desc)) { 1240 /* 1241 * continuous reading is not supported by flash or 1242 * its spi controller, use regular reading 1243 */ 1244 spinand->cont_read_possible = false; 1245 memset(&info->secondary_op_tmpl, 0, sizeof(info->secondary_op_tmpl)); 1246 1247 info->length = nanddev_page_size(nand) + 1248 nanddev_per_page_oobsize(nand); 1249 desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev, 1250 spinand->spimem, info); 1251 } 1252 1253 return desc; 1254 } 1255 1256 static int spinand_create_dirmap(struct spinand_device *spinand, 1257 unsigned int plane) 1258 { 1259 struct nand_device *nand = spinand_to_nand(spinand); 1260 struct spi_mem_dirmap_info info = { 0 }; 1261 struct spi_mem_dirmap_desc *desc; 1262 bool enable_ecc = false, secondary_op = false; 1263 1264 if (nand->ecc.engine->integration == NAND_ECC_ENGINE_INTEGRATION_PIPELINED) 1265 enable_ecc = true; 1266 1267 if (spinand->cont_read_possible && spinand->op_templates->cont_read_cache) 1268 secondary_op = true; 1269 1270 /* 1271 * Continuous read implies that only the main data is retrieved, backed 1272 * by an on-die ECC engine. It is not possible to use a pipelind ECC 1273 * engine with continuous read. 1274 */ 1275 if (enable_ecc && secondary_op) { 1276 secondary_op = false; 1277 spinand->cont_read_possible = false; 1278 } 1279 1280 /* The plane number is passed in MSB just above the column address */ 1281 info.offset = plane << fls(nand->memorg.pagesize); 1282 1283 /* Write descriptor */ 1284 info.length = nanddev_page_size(nand) + nanddev_per_page_oobsize(nand); 1285 info.primary_op_tmpl = *spinand->op_templates->update_cache; 1286 info.primary_op_tmpl.data.ecc = enable_ecc; 1287 desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev, 1288 spinand->spimem, &info); 1289 if (IS_ERR(desc)) 1290 return PTR_ERR(desc); 1291 1292 spinand->dirmaps[plane].wdesc = desc; 1293 1294 /* Read descriptor */ 1295 info.primary_op_tmpl = *spinand->op_templates->read_cache; 1296 info.primary_op_tmpl.data.ecc = enable_ecc; 1297 if (secondary_op) { 1298 info.secondary_op_tmpl = *spinand->op_templates->cont_read_cache; 1299 info.secondary_op_tmpl.data.ecc = enable_ecc; 1300 } 1301 desc = spinand_create_rdesc(spinand, &info); 1302 if (IS_ERR(desc)) 1303 return PTR_ERR(desc); 1304 1305 spinand->dirmaps[plane].rdesc = desc; 1306 1307 return 0; 1308 } 1309 1310 static int spinand_create_dirmaps(struct spinand_device *spinand) 1311 { 1312 struct nand_device *nand = spinand_to_nand(spinand); 1313 int i, ret; 1314 1315 spinand->dirmaps = devm_kzalloc(&spinand->spimem->spi->dev, 1316 sizeof(*spinand->dirmaps) * 1317 nand->memorg.planes_per_lun, 1318 GFP_KERNEL); 1319 if (!spinand->dirmaps) 1320 return -ENOMEM; 1321 1322 for (i = 0; i < nand->memorg.planes_per_lun; i++) { 1323 ret = spinand_create_dirmap(spinand, i); 1324 if (ret) 1325 return ret; 1326 } 1327 1328 return 0; 1329 } 1330 1331 static int spinand_randomizer_init(struct spinand_device *spinand) 1332 { 1333 struct device_node *np = spinand->spimem->spi->dev.of_node; 1334 u32 rand_val; 1335 int ret; 1336 1337 if (!spinand->set_randomizer) 1338 return 0; 1339 1340 ret = of_property_read_u32(np, "nand-randomizer", &rand_val); 1341 if (ret) 1342 return 0; 1343 1344 return spinand->set_randomizer(spinand, rand_val); 1345 } 1346 1347 static const struct nand_ops spinand_ops = { 1348 .erase = spinand_erase, 1349 .markbad = spinand_markbad, 1350 .isbad = spinand_isbad, 1351 }; 1352 1353 static const struct spinand_manufacturer *spinand_manufacturers[] = { 1354 &alliancememory_spinand_manufacturer, 1355 &ato_spinand_manufacturer, 1356 &dosilicon_spinand_manufacturer, 1357 &esmt_8c_spinand_manufacturer, 1358 &esmt_c8_spinand_manufacturer, 1359 &fmsh_spinand_manufacturer, 1360 &foresee_spinand_manufacturer, 1361 &gigadevice_spinand_manufacturer, 1362 ¯onix_spinand_manufacturer, 1363 µn_spinand_manufacturer, 1364 ¶gon_spinand_manufacturer, 1365 &skyhigh_spinand_manufacturer, 1366 &toshiba_spinand_manufacturer, 1367 &winbond_spinand_manufacturer, 1368 &xtx_spinand_manufacturer, 1369 }; 1370 1371 static int spinand_manufacturer_match(struct spinand_device *spinand, 1372 enum spinand_readid_method rdid_method) 1373 { 1374 u8 *id = spinand->id.data; 1375 unsigned int i; 1376 int ret; 1377 1378 for (i = 0; i < ARRAY_SIZE(spinand_manufacturers); i++) { 1379 const struct spinand_manufacturer *manufacturer = 1380 spinand_manufacturers[i]; 1381 1382 if (id[0] != manufacturer->id) 1383 continue; 1384 1385 ret = spinand_match_and_init(spinand, 1386 manufacturer->chips, 1387 manufacturer->nchips, 1388 rdid_method); 1389 if (ret < 0) 1390 continue; 1391 1392 spinand->manufacturer = manufacturer; 1393 return 0; 1394 } 1395 return -EOPNOTSUPP; 1396 } 1397 1398 static int spinand_id_detect(struct spinand_device *spinand) 1399 { 1400 u8 *id = spinand->id.data; 1401 int ret; 1402 1403 ret = spinand_read_id_op(spinand, 0, 0, id); 1404 if (ret) 1405 return ret; 1406 ret = spinand_manufacturer_match(spinand, SPINAND_READID_METHOD_OPCODE); 1407 if (!ret) 1408 return 0; 1409 1410 ret = spinand_read_id_op(spinand, 1, 0, id); 1411 if (ret) 1412 return ret; 1413 ret = spinand_manufacturer_match(spinand, 1414 SPINAND_READID_METHOD_OPCODE_ADDR); 1415 if (!ret) 1416 return 0; 1417 1418 ret = spinand_read_id_op(spinand, 0, 1, id); 1419 if (ret) 1420 return ret; 1421 ret = spinand_manufacturer_match(spinand, 1422 SPINAND_READID_METHOD_OPCODE_DUMMY); 1423 1424 return ret; 1425 } 1426 1427 static int spinand_manufacturer_init(struct spinand_device *spinand) 1428 { 1429 int ret; 1430 1431 if (spinand->manufacturer->ops->init) { 1432 ret = spinand->manufacturer->ops->init(spinand); 1433 if (ret) 1434 return ret; 1435 } 1436 1437 return 0; 1438 } 1439 1440 static void spinand_manufacturer_cleanup(struct spinand_device *spinand) 1441 { 1442 /* Release manufacturer private data */ 1443 if (spinand->manufacturer->ops->cleanup) 1444 return spinand->manufacturer->ops->cleanup(spinand); 1445 } 1446 1447 bool spinand_op_is_odtr(const struct spi_mem_op *op) 1448 { 1449 return op->cmd.dtr && op->cmd.buswidth == 8; 1450 } 1451 1452 static void spinand_init_ssdr_templates(struct spinand_device *spinand) 1453 { 1454 struct spinand_mem_ops *tmpl = &spinand->ssdr_op_templates; 1455 1456 tmpl->reset = (struct spi_mem_op)SPINAND_RESET_1S_0_0_OP; 1457 tmpl->readid = (struct spi_mem_op)SPINAND_READID_1S_1S_1S_OP(0, 0, NULL, 0); 1458 tmpl->wr_en = (struct spi_mem_op)SPINAND_WR_EN_1S_0_0_OP; 1459 tmpl->wr_dis = (struct spi_mem_op)SPINAND_WR_DIS_1S_0_0_OP; 1460 tmpl->set_feature = (struct spi_mem_op)SPINAND_SET_FEATURE_1S_1S_1S_OP(0, NULL); 1461 tmpl->get_feature = (struct spi_mem_op)SPINAND_GET_FEATURE_1S_1S_1S_OP(0, NULL); 1462 tmpl->blk_erase = (struct spi_mem_op)SPINAND_BLK_ERASE_1S_1S_0_OP(0); 1463 tmpl->page_read = (struct spi_mem_op)SPINAND_PAGE_READ_1S_1S_0_OP(0); 1464 tmpl->prog_exec = (struct spi_mem_op)SPINAND_PROG_EXEC_1S_1S_0_OP(0); 1465 spinand->op_templates = &spinand->ssdr_op_templates; 1466 spinand->bus_iface = SSDR; 1467 } 1468 1469 static int spinand_support_vendor_ops(struct spinand_device *spinand, 1470 const struct spinand_info *info, 1471 enum spinand_bus_interface iface) 1472 { 1473 int i; 1474 1475 if (!info->vendor_ops) 1476 return 0; 1477 /* 1478 * The vendor ops array is only used in order to verify this chip and all its memory 1479 * operations are supported. If we see patterns emerging, we could ideally name these 1480 * operations and define them at the SPI NAND core level instead. 1481 * For now, this only serves as a sanity check. 1482 */ 1483 for (i = 0; i < info->vendor_ops->nops; i++) { 1484 const struct spi_mem_op *op = &info->vendor_ops->ops[i]; 1485 1486 if ((iface == SSDR && spinand_op_is_odtr(op)) || 1487 (iface == ODTR && !spinand_op_is_odtr(op))) 1488 continue; 1489 1490 if (!spi_mem_supports_op(spinand->spimem, op)) 1491 return -EOPNOTSUPP; 1492 } 1493 1494 return 0; 1495 } 1496 1497 static int spinand_init_odtr_instruction_set(struct spinand_device *spinand) 1498 { 1499 struct spinand_mem_ops *tmpl = &spinand->odtr_op_templates; 1500 1501 tmpl->reset = (struct spi_mem_op)SPINAND_RESET_8D_0_0_OP; 1502 if (!spi_mem_supports_op(spinand->spimem, &tmpl->reset)) 1503 return -EOPNOTSUPP; 1504 1505 tmpl->readid = (struct spi_mem_op)SPINAND_READID_8D_8D_8D_OP(0, 0, NULL, 0); 1506 if (!spi_mem_supports_op(spinand->spimem, &tmpl->readid)) 1507 return -EOPNOTSUPP; 1508 1509 tmpl->wr_en = (struct spi_mem_op)SPINAND_WR_EN_8D_0_0_OP; 1510 if (!spi_mem_supports_op(spinand->spimem, &tmpl->wr_en)) 1511 return -EOPNOTSUPP; 1512 1513 tmpl->wr_dis = (struct spi_mem_op)SPINAND_WR_DIS_8D_0_0_OP; 1514 if (!spi_mem_supports_op(spinand->spimem, &tmpl->wr_dis)) 1515 return -EOPNOTSUPP; 1516 1517 tmpl->set_feature = (struct spi_mem_op)SPINAND_SET_FEATURE_8D_8D_8D_OP(0, NULL); 1518 if (!spi_mem_supports_op(spinand->spimem, &tmpl->set_feature)) 1519 return -EOPNOTSUPP; 1520 1521 tmpl->get_feature = (struct spi_mem_op)SPINAND_GET_FEATURE_8D_8D_8D_OP(0, NULL); 1522 if (!spi_mem_supports_op(spinand->spimem, &tmpl->get_feature)) 1523 return -EOPNOTSUPP; 1524 1525 tmpl->blk_erase = (struct spi_mem_op)SPINAND_BLK_ERASE_8D_8D_0_OP(0); 1526 if (!spi_mem_supports_op(spinand->spimem, &tmpl->blk_erase)) 1527 return -EOPNOTSUPP; 1528 1529 if (spinand->flags & SPINAND_ODTR_PACKED_PAGE_READ) 1530 tmpl->page_read = (struct spi_mem_op)SPINAND_PAGE_READ_PACKED_8D_8D_0_OP(0); 1531 else 1532 tmpl->page_read = (struct spi_mem_op)SPINAND_PAGE_READ_8D_8D_0_OP(0); 1533 if (!spi_mem_supports_op(spinand->spimem, &tmpl->page_read)) { 1534 return -EOPNOTSUPP; 1535 } 1536 1537 tmpl->prog_exec = (struct spi_mem_op)SPINAND_PROG_EXEC_8D_8D_0_OP(0); 1538 if (!spi_mem_supports_op(spinand->spimem, &tmpl->prog_exec)) 1539 return -EOPNOTSUPP; 1540 1541 return 0; 1542 } 1543 1544 static const struct spi_mem_op * 1545 spinand_select_op_variant(struct spinand_device *spinand, enum spinand_bus_interface iface, 1546 const struct spinand_op_variants *variants) 1547 { 1548 struct nand_device *nand = spinand_to_nand(spinand); 1549 const struct spi_mem_op *best_variant = NULL; 1550 u64 best_op_duration_ns = ULLONG_MAX; 1551 unsigned int i; 1552 1553 for (i = 0; i < variants->nops; i++) { 1554 struct spi_mem_op op = variants->ops[i]; 1555 u64 op_duration_ns = 0; 1556 unsigned int nbytes; 1557 int ret; 1558 1559 if ((iface == SSDR && spinand_op_is_odtr(&op)) || 1560 (iface == ODTR && !spinand_op_is_odtr(&op))) 1561 continue; 1562 1563 nbytes = nanddev_per_page_oobsize(nand) + 1564 nanddev_page_size(nand); 1565 1566 while (nbytes) { 1567 op.data.nbytes = nbytes; 1568 ret = spi_mem_adjust_op_size(spinand->spimem, &op); 1569 if (ret) 1570 break; 1571 1572 spi_mem_adjust_op_freq(spinand->spimem, &op); 1573 1574 if (!spi_mem_supports_op(spinand->spimem, &op)) 1575 break; 1576 1577 nbytes -= op.data.nbytes; 1578 1579 op_duration_ns += spi_mem_calc_op_duration(spinand->spimem, &op); 1580 } 1581 1582 if (!nbytes && op_duration_ns < best_op_duration_ns) { 1583 best_op_duration_ns = op_duration_ns; 1584 best_variant = &variants->ops[i]; 1585 } 1586 } 1587 1588 return best_variant; 1589 } 1590 1591 /** 1592 * spinand_match_and_init() - Try to find a match between a device ID and an 1593 * entry in a spinand_info table 1594 * @spinand: SPI NAND object 1595 * @table: SPI NAND device description table 1596 * @table_size: size of the device description table 1597 * @rdid_method: read id method to match 1598 * 1599 * Match between a device ID retrieved through the READ_ID command and an 1600 * entry in the SPI NAND description table. If a match is found, the spinand 1601 * object will be initialized with information provided by the matching 1602 * spinand_info entry. 1603 * 1604 * Return: 0 on success, a negative error code otherwise. 1605 */ 1606 int spinand_match_and_init(struct spinand_device *spinand, 1607 const struct spinand_info *table, 1608 unsigned int table_size, 1609 enum spinand_readid_method rdid_method) 1610 { 1611 u8 *id = spinand->id.data; 1612 struct nand_device *nand = spinand_to_nand(spinand); 1613 unsigned int i; 1614 int ret; 1615 1616 for (i = 0; i < table_size; i++) { 1617 const struct spinand_info *info = &table[i]; 1618 const struct spi_mem_op *op; 1619 1620 if (rdid_method != info->devid.method) 1621 continue; 1622 1623 if (memcmp(id + 1, info->devid.id, info->devid.len)) 1624 continue; 1625 1626 nand->memorg = table[i].memorg; 1627 nanddev_set_ecc_requirements(nand, &table[i].eccreq); 1628 spinand->eccinfo = table[i].eccinfo; 1629 spinand->flags = table[i].flags; 1630 spinand->id.len = 1 + table[i].devid.len; 1631 spinand->select_target = table[i].select_target; 1632 spinand->configure_chip = table[i].configure_chip; 1633 spinand->set_cont_read = table[i].set_cont_read; 1634 spinand->fact_otp = &table[i].fact_otp; 1635 spinand->user_otp = &table[i].user_otp; 1636 spinand->read_retries = table[i].read_retries; 1637 spinand->set_read_retry = table[i].set_read_retry; 1638 spinand->set_randomizer = table[i].set_randomizer; 1639 1640 /* I/O variants selection with single-spi SDR commands */ 1641 1642 op = spinand_select_op_variant(spinand, SSDR, 1643 info->op_variants.read_cache); 1644 if (!op) 1645 return -EOPNOTSUPP; 1646 1647 spinand->ssdr_op_templates.read_cache = op; 1648 1649 op = spinand_select_op_variant(spinand, SSDR, 1650 info->op_variants.write_cache); 1651 if (!op) 1652 return -EOPNOTSUPP; 1653 1654 spinand->ssdr_op_templates.write_cache = op; 1655 1656 op = spinand_select_op_variant(spinand, SSDR, 1657 info->op_variants.update_cache); 1658 if (!op) 1659 return -EOPNOTSUPP; 1660 1661 spinand->ssdr_op_templates.update_cache = op; 1662 1663 ret = spinand_support_vendor_ops(spinand, info, SSDR); 1664 if (ret) 1665 return ret; 1666 1667 if (info->op_variants.cont_read_cache) { 1668 op = spinand_select_op_variant(spinand, SSDR, 1669 info->op_variants.cont_read_cache); 1670 if (op) { 1671 const struct spi_mem_op *read_op; 1672 1673 read_op = spinand->ssdr_op_templates.read_cache; 1674 1675 /* 1676 * Sometimes the fastest continuous read variant may not 1677 * be supported. In this case, prefer to use the fastest 1678 * read from cache variant and disable continuous reads. 1679 */ 1680 if (read_op->cmd.buswidth > op->cmd.buswidth || 1681 (read_op->cmd.dtr && !op->cmd.dtr) || 1682 read_op->addr.buswidth > op->addr.buswidth || 1683 (read_op->addr.dtr && !op->addr.dtr) || 1684 read_op->data.buswidth > op->data.buswidth || 1685 (read_op->data.dtr && !op->data.dtr)) 1686 spinand->cont_read_possible = false; 1687 else 1688 spinand->ssdr_op_templates.cont_read_cache = op; 1689 } else { 1690 spinand->cont_read_possible = false; 1691 } 1692 } 1693 1694 /* I/O variants selection with octo-spi DDR commands (optional) */ 1695 1696 ret = spinand_init_odtr_instruction_set(spinand); 1697 if (ret) 1698 return 0; 1699 1700 ret = spinand_support_vendor_ops(spinand, info, ODTR); 1701 if (ret) 1702 return 0; 1703 1704 op = spinand_select_op_variant(spinand, ODTR, 1705 info->op_variants.read_cache); 1706 spinand->odtr_op_templates.read_cache = op; 1707 1708 op = spinand_select_op_variant(spinand, ODTR, 1709 info->op_variants.write_cache); 1710 spinand->odtr_op_templates.write_cache = op; 1711 1712 op = spinand_select_op_variant(spinand, ODTR, 1713 info->op_variants.update_cache); 1714 spinand->odtr_op_templates.update_cache = op; 1715 1716 if (info->op_variants.cont_read_cache) { 1717 op = spinand_select_op_variant(spinand, ODTR, 1718 info->op_variants.cont_read_cache); 1719 if (op) 1720 spinand->odtr_op_templates.cont_read_cache = op; 1721 else 1722 spinand->cont_read_possible = false; 1723 } 1724 1725 return 0; 1726 } 1727 1728 return -EOPNOTSUPP; 1729 } 1730 1731 static int spinand_detect(struct spinand_device *spinand) 1732 { 1733 struct device *dev = &spinand->spimem->spi->dev; 1734 struct nand_device *nand = spinand_to_nand(spinand); 1735 int ret; 1736 1737 ret = spinand_reset_op(spinand); 1738 if (ret) 1739 return ret; 1740 1741 ret = spinand_id_detect(spinand); 1742 if (ret) { 1743 dev_err(dev, "unknown raw ID %*phN\n", SPINAND_MAX_ID_LEN, 1744 spinand->id.data); 1745 return ret; 1746 } 1747 1748 if (nand->memorg.ntargets > 1 && !spinand->select_target) { 1749 dev_err(dev, 1750 "SPI NANDs with more than one die must implement ->select_target()\n"); 1751 return -EINVAL; 1752 } 1753 1754 dev_info(&spinand->spimem->spi->dev, 1755 "%s SPI NAND was found.\n", spinand->manufacturer->name); 1756 dev_info(&spinand->spimem->spi->dev, 1757 "%llu MiB, block size: %zu KiB, page size: %zu, OOB size: %u\n", 1758 nanddev_size(nand) >> 20, nanddev_eraseblock_size(nand) >> 10, 1759 nanddev_page_size(nand), nanddev_per_page_oobsize(nand)); 1760 1761 return 0; 1762 } 1763 1764 static int spinand_configure_chip(struct spinand_device *spinand) 1765 { 1766 bool odtr = false, quad_enable = false; 1767 int ret; 1768 1769 if (spinand->odtr_op_templates.read_cache && 1770 spinand->odtr_op_templates.write_cache && 1771 spinand->odtr_op_templates.update_cache) 1772 odtr = true; 1773 1774 if (odtr) { 1775 if (!spinand->configure_chip) 1776 goto try_ssdr; 1777 1778 /* ODTR bus interface configuration happens here */ 1779 ret = spinand->configure_chip(spinand, ODTR); 1780 if (ret) { 1781 spinand->odtr_op_templates.read_cache = NULL; 1782 spinand->odtr_op_templates.write_cache = NULL; 1783 spinand->odtr_op_templates.update_cache = NULL; 1784 goto try_ssdr; 1785 } 1786 1787 spinand->op_templates = &spinand->odtr_op_templates; 1788 spinand->bus_iface = ODTR; 1789 1790 return 0; 1791 } 1792 1793 try_ssdr: 1794 if (spinand->flags & SPINAND_HAS_QE_BIT) { 1795 if (spinand->ssdr_op_templates.read_cache->data.buswidth == 4 || 1796 spinand->ssdr_op_templates.write_cache->data.buswidth == 4 || 1797 spinand->ssdr_op_templates.update_cache->data.buswidth == 4) 1798 quad_enable = true; 1799 } 1800 1801 ret = spinand_init_quad_enable(spinand, quad_enable); 1802 if (ret) 1803 return ret; 1804 1805 if (spinand->configure_chip) { 1806 ret = spinand->configure_chip(spinand, SSDR); 1807 if (ret) 1808 return ret; 1809 } 1810 1811 return ret; 1812 } 1813 1814 static int spinand_init_flash(struct spinand_device *spinand) 1815 { 1816 struct device *dev = &spinand->spimem->spi->dev; 1817 struct nand_device *nand = spinand_to_nand(spinand); 1818 int ret, i; 1819 1820 ret = spinand_read_cfg(spinand); 1821 if (ret) 1822 return ret; 1823 1824 ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0); 1825 if (ret) 1826 return ret; 1827 1828 ret = spinand_manufacturer_init(spinand); 1829 if (ret) { 1830 dev_err(dev, 1831 "Failed to initialize the SPI NAND chip (err = %d)\n", 1832 ret); 1833 return ret; 1834 } 1835 1836 ret = spinand_configure_chip(spinand); 1837 if (ret) 1838 goto manuf_cleanup; 1839 1840 /* After power up, all blocks are locked, so unlock them here. */ 1841 for (i = 0; i < nand->memorg.ntargets; i++) { 1842 ret = spinand_select_target(spinand, i); 1843 if (ret) 1844 goto manuf_cleanup; 1845 1846 ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED); 1847 if (ret) 1848 goto manuf_cleanup; 1849 } 1850 1851 return 0; 1852 1853 manuf_cleanup: 1854 spinand_manufacturer_cleanup(spinand); 1855 1856 return ret; 1857 } 1858 1859 static void spinand_mtd_resume(struct mtd_info *mtd) 1860 { 1861 struct spinand_device *spinand = mtd_to_spinand(mtd); 1862 int ret; 1863 1864 ret = spinand_reset_op(spinand); 1865 if (ret) 1866 return; 1867 1868 ret = spinand_init_flash(spinand); 1869 if (ret) 1870 return; 1871 1872 spinand_ecc_enable(spinand, false); 1873 } 1874 1875 static int spinand_mtd_suspend(struct mtd_info *mtd) 1876 { 1877 struct spinand_device *spinand = mtd_to_spinand(mtd); 1878 int ret; 1879 1880 /* 1881 * Return to SSDR interface in the suspend path to make sure the 1882 * reset operation is correctly processed upon resume. 1883 * 1884 * Note: Once back in SSDR mode, every operation but the page helpers 1885 * (dirmap based I/O accessors) will work. Page accesses would require 1886 * destroying and recreating the dirmaps twice to work, which would be 1887 * impacting for no reason, as this is just a transitional state. 1888 */ 1889 if (spinand->bus_iface == ODTR) { 1890 ret = spinand->configure_chip(spinand, SSDR); 1891 if (ret) 1892 return ret; 1893 1894 spinand->op_templates = &spinand->ssdr_op_templates; 1895 spinand->bus_iface = SSDR; 1896 } 1897 1898 return 0; 1899 } 1900 1901 static int spinand_init(struct spinand_device *spinand) 1902 { 1903 struct device *dev = &spinand->spimem->spi->dev; 1904 struct mtd_info *mtd = spinand_to_mtd(spinand); 1905 struct nand_device *nand = mtd_to_nanddev(mtd); 1906 int ret; 1907 1908 /* 1909 * We need a scratch buffer because the spi_mem interface requires that 1910 * buf passed in spi_mem_op->data.buf be DMA-able. 1911 */ 1912 spinand->scratchbuf = kzalloc(SPINAND_MAX_ID_LEN, GFP_KERNEL); 1913 if (!spinand->scratchbuf) 1914 return -ENOMEM; 1915 1916 spinand_init_ssdr_templates(spinand); 1917 1918 ret = spinand_detect(spinand); 1919 if (ret) 1920 goto err_free_bufs; 1921 1922 /* 1923 * Use kzalloc() instead of devm_kzalloc() here, because some drivers 1924 * may use this buffer for DMA access. 1925 * Memory allocated by devm_ does not guarantee DMA-safe alignment. 1926 */ 1927 spinand->databuf = kzalloc(nanddev_eraseblock_size(nand), 1928 GFP_KERNEL); 1929 if (!spinand->databuf) { 1930 ret = -ENOMEM; 1931 goto err_free_bufs; 1932 } 1933 1934 spinand->oobbuf = spinand->databuf + nanddev_page_size(nand); 1935 1936 ret = spinand_init_cfg_cache(spinand); 1937 if (ret) 1938 goto err_free_bufs; 1939 1940 ret = spinand_init_flash(spinand); 1941 if (ret) 1942 goto err_free_bufs; 1943 1944 ret = nanddev_init(nand, &spinand_ops, THIS_MODULE); 1945 if (ret) 1946 goto err_manuf_cleanup; 1947 1948 /* SPI-NAND default ECC engine is on-die */ 1949 nand->ecc.defaults.engine_type = NAND_ECC_ENGINE_TYPE_ON_DIE; 1950 nand->ecc.ondie_engine = &spinand_ondie_ecc_engine; 1951 1952 spinand_ecc_enable(spinand, false); 1953 ret = nanddev_ecc_engine_init(nand); 1954 if (ret) 1955 goto err_cleanup_nanddev; 1956 1957 /* 1958 * Continuous read can only be enabled with an on-die ECC engine, so the 1959 * ECC initialization must have happened previously. 1960 */ 1961 spinand_cont_read_init(spinand); 1962 ret = spinand_randomizer_init(spinand); 1963 if (ret) 1964 goto err_cleanup_nanddev; 1965 1966 mtd->_read_oob = spinand_mtd_read; 1967 mtd->_write_oob = spinand_mtd_write; 1968 mtd->_block_isbad = spinand_mtd_block_isbad; 1969 mtd->_block_markbad = spinand_mtd_block_markbad; 1970 mtd->_block_isreserved = spinand_mtd_block_isreserved; 1971 mtd->_erase = spinand_mtd_erase; 1972 mtd->_max_bad_blocks = nanddev_mtd_max_bad_blocks; 1973 mtd->_suspend = spinand_mtd_suspend; 1974 mtd->_resume = spinand_mtd_resume; 1975 1976 if (spinand_user_otp_size(spinand) || spinand_fact_otp_size(spinand)) { 1977 ret = spinand_set_mtd_otp_ops(spinand); 1978 if (ret) 1979 goto err_cleanup_ecc_engine; 1980 } 1981 1982 if (nand->ecc.engine) { 1983 ret = mtd_ooblayout_count_freebytes(mtd); 1984 if (ret < 0) 1985 goto err_cleanup_ecc_engine; 1986 } 1987 1988 mtd->oobavail = ret; 1989 1990 /* Propagate ECC information to mtd_info */ 1991 mtd->ecc_strength = nanddev_get_ecc_conf(nand)->strength; 1992 mtd->ecc_step_size = nanddev_get_ecc_conf(nand)->step_size; 1993 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); 1994 1995 ret = spinand_create_dirmaps(spinand); 1996 if (ret) { 1997 dev_err(dev, 1998 "Failed to create direct mappings for read/write operations (err = %d)\n", 1999 ret); 2000 goto err_cleanup_ecc_engine; 2001 } 2002 2003 return 0; 2004 2005 err_cleanup_ecc_engine: 2006 nanddev_ecc_engine_cleanup(nand); 2007 2008 err_cleanup_nanddev: 2009 nanddev_cleanup(nand); 2010 2011 err_manuf_cleanup: 2012 spinand_manufacturer_cleanup(spinand); 2013 2014 err_free_bufs: 2015 kfree(spinand->databuf); 2016 kfree(spinand->scratchbuf); 2017 return ret; 2018 } 2019 2020 static void spinand_cleanup(struct spinand_device *spinand) 2021 { 2022 struct nand_device *nand = spinand_to_nand(spinand); 2023 2024 nanddev_ecc_engine_cleanup(nand); 2025 nanddev_cleanup(nand); 2026 spinand_manufacturer_cleanup(spinand); 2027 kfree(spinand->databuf); 2028 kfree(spinand->scratchbuf); 2029 } 2030 2031 static int spinand_probe(struct spi_mem *mem) 2032 { 2033 struct spinand_device *spinand; 2034 struct mtd_info *mtd; 2035 int ret; 2036 2037 spinand = devm_kzalloc(&mem->spi->dev, sizeof(*spinand), 2038 GFP_KERNEL); 2039 if (!spinand) 2040 return -ENOMEM; 2041 2042 spinand->spimem = mem; 2043 spi_mem_set_drvdata(mem, spinand); 2044 spinand_set_of_node(spinand, mem->spi->dev.of_node); 2045 mutex_init(&spinand->lock); 2046 mtd = spinand_to_mtd(spinand); 2047 mtd->dev.parent = &mem->spi->dev; 2048 2049 ret = spinand_init(spinand); 2050 if (ret) 2051 return ret; 2052 2053 ret = mtd_device_register(mtd, NULL, 0); 2054 if (ret) 2055 goto err_spinand_cleanup; 2056 2057 return 0; 2058 2059 err_spinand_cleanup: 2060 spinand_cleanup(spinand); 2061 2062 return ret; 2063 } 2064 2065 static int spinand_remove(struct spi_mem *mem) 2066 { 2067 struct spinand_device *spinand; 2068 struct mtd_info *mtd; 2069 int ret; 2070 2071 spinand = spi_mem_get_drvdata(mem); 2072 mtd = spinand_to_mtd(spinand); 2073 2074 ret = mtd_device_unregister(mtd); 2075 if (ret) 2076 return ret; 2077 2078 spinand_cleanup(spinand); 2079 2080 return 0; 2081 } 2082 2083 static const struct spi_device_id spinand_ids[] = { 2084 { .name = "spi-nand" }, 2085 { /* sentinel */ }, 2086 }; 2087 MODULE_DEVICE_TABLE(spi, spinand_ids); 2088 2089 #ifdef CONFIG_OF 2090 static const struct of_device_id spinand_of_ids[] = { 2091 { .compatible = "spi-nand" }, 2092 { /* sentinel */ }, 2093 }; 2094 MODULE_DEVICE_TABLE(of, spinand_of_ids); 2095 #endif 2096 2097 static struct spi_mem_driver spinand_drv = { 2098 .spidrv = { 2099 .id_table = spinand_ids, 2100 .driver = { 2101 .name = "spi-nand", 2102 .of_match_table = of_match_ptr(spinand_of_ids), 2103 }, 2104 }, 2105 .probe = spinand_probe, 2106 .remove = spinand_remove, 2107 }; 2108 module_spi_mem_driver(spinand_drv); 2109 2110 MODULE_DESCRIPTION("SPI NAND framework"); 2111 MODULE_AUTHOR("Peter Pan<peterpandong@micron.com>"); 2112 MODULE_LICENSE("GPL v2"); 2113