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 = 0; 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 &heyangtek_spinand_manufacturer, 1363 ¯onix_spinand_manufacturer, 1364 µn_spinand_manufacturer, 1365 ¶gon_spinand_manufacturer, 1366 &skyhigh_spinand_manufacturer, 1367 &toshiba_spinand_manufacturer, 1368 &winbond_spinand_manufacturer, 1369 &xtx_spinand_manufacturer, 1370 }; 1371 1372 static int spinand_manufacturer_match(struct spinand_device *spinand, 1373 enum spinand_readid_method rdid_method) 1374 { 1375 u8 *id = spinand->id.data; 1376 unsigned int i; 1377 int ret; 1378 1379 for (i = 0; i < ARRAY_SIZE(spinand_manufacturers); i++) { 1380 const struct spinand_manufacturer *manufacturer = 1381 spinand_manufacturers[i]; 1382 1383 if (id[0] != manufacturer->id) 1384 continue; 1385 1386 ret = spinand_match_and_init(spinand, 1387 manufacturer->chips, 1388 manufacturer->nchips, 1389 rdid_method); 1390 if (ret < 0) 1391 continue; 1392 1393 spinand->manufacturer = manufacturer; 1394 return 0; 1395 } 1396 return -EOPNOTSUPP; 1397 } 1398 1399 static int spinand_id_detect(struct spinand_device *spinand) 1400 { 1401 u8 *id = spinand->id.data; 1402 int ret; 1403 1404 ret = spinand_read_id_op(spinand, 0, 0, id); 1405 if (ret) 1406 return ret; 1407 ret = spinand_manufacturer_match(spinand, SPINAND_READID_METHOD_OPCODE); 1408 if (!ret) 1409 return 0; 1410 1411 ret = spinand_read_id_op(spinand, 1, 0, id); 1412 if (ret) 1413 return ret; 1414 ret = spinand_manufacturer_match(spinand, 1415 SPINAND_READID_METHOD_OPCODE_ADDR); 1416 if (!ret) 1417 return 0; 1418 1419 ret = spinand_read_id_op(spinand, 0, 1, id); 1420 if (ret) 1421 return ret; 1422 ret = spinand_manufacturer_match(spinand, 1423 SPINAND_READID_METHOD_OPCODE_DUMMY); 1424 1425 return ret; 1426 } 1427 1428 static int spinand_manufacturer_init(struct spinand_device *spinand) 1429 { 1430 int ret; 1431 1432 if (spinand->manufacturer->ops->init) { 1433 ret = spinand->manufacturer->ops->init(spinand); 1434 if (ret) 1435 return ret; 1436 } 1437 1438 return 0; 1439 } 1440 1441 static void spinand_manufacturer_cleanup(struct spinand_device *spinand) 1442 { 1443 /* Release manufacturer private data */ 1444 if (spinand->manufacturer->ops->cleanup) 1445 return spinand->manufacturer->ops->cleanup(spinand); 1446 } 1447 1448 bool spinand_op_is_odtr(const struct spi_mem_op *op) 1449 { 1450 return op->cmd.dtr && op->cmd.buswidth == 8; 1451 } 1452 1453 static void spinand_init_ssdr_templates(struct spinand_device *spinand) 1454 { 1455 struct spinand_mem_ops *tmpl = &spinand->ssdr_op_templates; 1456 1457 tmpl->reset = (struct spi_mem_op)SPINAND_RESET_1S_0_0_OP; 1458 tmpl->readid = (struct spi_mem_op)SPINAND_READID_1S_1S_1S_OP(0, 0, NULL, 0); 1459 tmpl->wr_en = (struct spi_mem_op)SPINAND_WR_EN_1S_0_0_OP; 1460 tmpl->wr_dis = (struct spi_mem_op)SPINAND_WR_DIS_1S_0_0_OP; 1461 tmpl->set_feature = (struct spi_mem_op)SPINAND_SET_FEATURE_1S_1S_1S_OP(0, NULL); 1462 tmpl->get_feature = (struct spi_mem_op)SPINAND_GET_FEATURE_1S_1S_1S_OP(0, NULL); 1463 tmpl->blk_erase = (struct spi_mem_op)SPINAND_BLK_ERASE_1S_1S_0_OP(0); 1464 tmpl->page_read = (struct spi_mem_op)SPINAND_PAGE_READ_1S_1S_0_OP(0); 1465 tmpl->prog_exec = (struct spi_mem_op)SPINAND_PROG_EXEC_1S_1S_0_OP(0); 1466 spinand->op_templates = &spinand->ssdr_op_templates; 1467 spinand->bus_iface = SSDR; 1468 } 1469 1470 static int spinand_support_vendor_ops(struct spinand_device *spinand, 1471 const struct spinand_info *info, 1472 enum spinand_bus_interface iface) 1473 { 1474 int i; 1475 1476 if (!info->vendor_ops) 1477 return 0; 1478 /* 1479 * The vendor ops array is only used in order to verify this chip and all its memory 1480 * operations are supported. If we see patterns emerging, we could ideally name these 1481 * operations and define them at the SPI NAND core level instead. 1482 * For now, this only serves as a sanity check. 1483 */ 1484 for (i = 0; i < info->vendor_ops->nops; i++) { 1485 const struct spi_mem_op *op = &info->vendor_ops->ops[i]; 1486 1487 if ((iface == SSDR && spinand_op_is_odtr(op)) || 1488 (iface == ODTR && !spinand_op_is_odtr(op))) 1489 continue; 1490 1491 if (!spi_mem_supports_op(spinand->spimem, op)) 1492 return -EOPNOTSUPP; 1493 } 1494 1495 return 0; 1496 } 1497 1498 static int spinand_init_odtr_instruction_set(struct spinand_device *spinand) 1499 { 1500 struct spinand_mem_ops *tmpl = &spinand->odtr_op_templates; 1501 1502 tmpl->reset = (struct spi_mem_op)SPINAND_RESET_8D_0_0_OP; 1503 if (!spi_mem_supports_op(spinand->spimem, &tmpl->reset)) 1504 return -EOPNOTSUPP; 1505 1506 tmpl->readid = (struct spi_mem_op)SPINAND_READID_8D_8D_8D_OP(0, 0, NULL, 0); 1507 if (!spi_mem_supports_op(spinand->spimem, &tmpl->readid)) 1508 return -EOPNOTSUPP; 1509 1510 tmpl->wr_en = (struct spi_mem_op)SPINAND_WR_EN_8D_0_0_OP; 1511 if (!spi_mem_supports_op(spinand->spimem, &tmpl->wr_en)) 1512 return -EOPNOTSUPP; 1513 1514 tmpl->wr_dis = (struct spi_mem_op)SPINAND_WR_DIS_8D_0_0_OP; 1515 if (!spi_mem_supports_op(spinand->spimem, &tmpl->wr_dis)) 1516 return -EOPNOTSUPP; 1517 1518 tmpl->set_feature = (struct spi_mem_op)SPINAND_SET_FEATURE_8D_8D_8D_OP(0, NULL); 1519 if (!spi_mem_supports_op(spinand->spimem, &tmpl->set_feature)) 1520 return -EOPNOTSUPP; 1521 1522 tmpl->get_feature = (struct spi_mem_op)SPINAND_GET_FEATURE_8D_8D_8D_OP(0, NULL); 1523 if (!spi_mem_supports_op(spinand->spimem, &tmpl->get_feature)) 1524 return -EOPNOTSUPP; 1525 1526 tmpl->blk_erase = (struct spi_mem_op)SPINAND_BLK_ERASE_8D_8D_0_OP(0); 1527 if (!spi_mem_supports_op(spinand->spimem, &tmpl->blk_erase)) 1528 return -EOPNOTSUPP; 1529 1530 if (spinand->flags & SPINAND_ODTR_PACKED_PAGE_READ) 1531 tmpl->page_read = (struct spi_mem_op)SPINAND_PAGE_READ_PACKED_8D_8D_0_OP(0); 1532 else 1533 tmpl->page_read = (struct spi_mem_op)SPINAND_PAGE_READ_8D_8D_0_OP(0); 1534 if (!spi_mem_supports_op(spinand->spimem, &tmpl->page_read)) { 1535 return -EOPNOTSUPP; 1536 } 1537 1538 tmpl->prog_exec = (struct spi_mem_op)SPINAND_PROG_EXEC_8D_8D_0_OP(0); 1539 if (!spi_mem_supports_op(spinand->spimem, &tmpl->prog_exec)) 1540 return -EOPNOTSUPP; 1541 1542 return 0; 1543 } 1544 1545 static const struct spi_mem_op * 1546 spinand_select_op_variant(struct spinand_device *spinand, enum spinand_bus_interface iface, 1547 const struct spinand_op_variants *variants) 1548 { 1549 struct nand_device *nand = spinand_to_nand(spinand); 1550 const struct spi_mem_op *best_variant = NULL; 1551 u64 best_op_duration_ns = ULLONG_MAX; 1552 unsigned int i; 1553 1554 for (i = 0; i < variants->nops; i++) { 1555 struct spi_mem_op op = variants->ops[i]; 1556 u64 op_duration_ns = 0; 1557 unsigned int nbytes; 1558 int ret; 1559 1560 if ((iface == SSDR && spinand_op_is_odtr(&op)) || 1561 (iface == ODTR && !spinand_op_is_odtr(&op))) 1562 continue; 1563 1564 nbytes = nanddev_per_page_oobsize(nand) + 1565 nanddev_page_size(nand); 1566 1567 while (nbytes) { 1568 op.data.nbytes = nbytes; 1569 ret = spi_mem_adjust_op_size(spinand->spimem, &op); 1570 if (ret) 1571 break; 1572 1573 spi_mem_adjust_op_freq(spinand->spimem, &op); 1574 1575 if (!spi_mem_supports_op(spinand->spimem, &op)) 1576 break; 1577 1578 nbytes -= op.data.nbytes; 1579 1580 op_duration_ns += spi_mem_calc_op_duration(spinand->spimem, &op); 1581 } 1582 1583 if (!nbytes && op_duration_ns < best_op_duration_ns) { 1584 best_op_duration_ns = op_duration_ns; 1585 best_variant = &variants->ops[i]; 1586 } 1587 } 1588 1589 return best_variant; 1590 } 1591 1592 /** 1593 * spinand_match_and_init() - Try to find a match between a device ID and an 1594 * entry in a spinand_info table 1595 * @spinand: SPI NAND object 1596 * @table: SPI NAND device description table 1597 * @table_size: size of the device description table 1598 * @rdid_method: read id method to match 1599 * 1600 * Match between a device ID retrieved through the READ_ID command and an 1601 * entry in the SPI NAND description table. If a match is found, the spinand 1602 * object will be initialized with information provided by the matching 1603 * spinand_info entry. 1604 * 1605 * Return: 0 on success, a negative error code otherwise. 1606 */ 1607 int spinand_match_and_init(struct spinand_device *spinand, 1608 const struct spinand_info *table, 1609 unsigned int table_size, 1610 enum spinand_readid_method rdid_method) 1611 { 1612 u8 *id = spinand->id.data; 1613 struct nand_device *nand = spinand_to_nand(spinand); 1614 unsigned int i; 1615 int ret; 1616 1617 for (i = 0; i < table_size; i++) { 1618 const struct spinand_info *info = &table[i]; 1619 const struct spi_mem_op *op; 1620 1621 if (rdid_method != info->devid.method) 1622 continue; 1623 1624 if (memcmp(id + 1, info->devid.id, info->devid.len)) 1625 continue; 1626 1627 nand->memorg = table[i].memorg; 1628 nanddev_set_ecc_requirements(nand, &table[i].eccreq); 1629 spinand->eccinfo = table[i].eccinfo; 1630 spinand->flags = table[i].flags; 1631 spinand->id.len = 1 + table[i].devid.len; 1632 spinand->select_target = table[i].select_target; 1633 spinand->configure_chip = table[i].configure_chip; 1634 spinand->set_cont_read = table[i].set_cont_read; 1635 spinand->fact_otp = &table[i].fact_otp; 1636 spinand->user_otp = &table[i].user_otp; 1637 spinand->read_retries = table[i].read_retries; 1638 spinand->set_read_retry = table[i].set_read_retry; 1639 spinand->set_randomizer = table[i].set_randomizer; 1640 1641 /* I/O variants selection with single-spi SDR commands */ 1642 1643 op = spinand_select_op_variant(spinand, SSDR, 1644 info->op_variants.read_cache); 1645 if (!op) 1646 return -EOPNOTSUPP; 1647 1648 spinand->ssdr_op_templates.read_cache = op; 1649 1650 op = spinand_select_op_variant(spinand, SSDR, 1651 info->op_variants.write_cache); 1652 if (!op) 1653 return -EOPNOTSUPP; 1654 1655 spinand->ssdr_op_templates.write_cache = op; 1656 1657 op = spinand_select_op_variant(spinand, SSDR, 1658 info->op_variants.update_cache); 1659 if (!op) 1660 return -EOPNOTSUPP; 1661 1662 spinand->ssdr_op_templates.update_cache = op; 1663 1664 ret = spinand_support_vendor_ops(spinand, info, SSDR); 1665 if (ret) 1666 return ret; 1667 1668 if (info->op_variants.cont_read_cache) { 1669 op = spinand_select_op_variant(spinand, SSDR, 1670 info->op_variants.cont_read_cache); 1671 if (op) { 1672 const struct spi_mem_op *read_op; 1673 1674 read_op = spinand->ssdr_op_templates.read_cache; 1675 1676 /* 1677 * Sometimes the fastest continuous read variant may not 1678 * be supported. In this case, prefer to use the fastest 1679 * read from cache variant and disable continuous reads. 1680 */ 1681 if (read_op->cmd.buswidth > op->cmd.buswidth || 1682 (read_op->cmd.dtr && !op->cmd.dtr) || 1683 read_op->addr.buswidth > op->addr.buswidth || 1684 (read_op->addr.dtr && !op->addr.dtr) || 1685 read_op->data.buswidth > op->data.buswidth || 1686 (read_op->data.dtr && !op->data.dtr)) 1687 spinand->cont_read_possible = false; 1688 else 1689 spinand->ssdr_op_templates.cont_read_cache = op; 1690 } else { 1691 spinand->cont_read_possible = false; 1692 } 1693 } 1694 1695 /* I/O variants selection with octo-spi DDR commands (optional) */ 1696 1697 ret = spinand_init_odtr_instruction_set(spinand); 1698 if (ret) 1699 return 0; 1700 1701 ret = spinand_support_vendor_ops(spinand, info, ODTR); 1702 if (ret) 1703 return 0; 1704 1705 op = spinand_select_op_variant(spinand, ODTR, 1706 info->op_variants.read_cache); 1707 spinand->odtr_op_templates.read_cache = op; 1708 1709 op = spinand_select_op_variant(spinand, ODTR, 1710 info->op_variants.write_cache); 1711 spinand->odtr_op_templates.write_cache = op; 1712 1713 op = spinand_select_op_variant(spinand, ODTR, 1714 info->op_variants.update_cache); 1715 spinand->odtr_op_templates.update_cache = op; 1716 1717 if (info->op_variants.cont_read_cache) { 1718 op = spinand_select_op_variant(spinand, ODTR, 1719 info->op_variants.cont_read_cache); 1720 if (op) 1721 spinand->odtr_op_templates.cont_read_cache = op; 1722 else 1723 spinand->cont_read_possible = false; 1724 } 1725 1726 return 0; 1727 } 1728 1729 return -EOPNOTSUPP; 1730 } 1731 1732 static int spinand_detect(struct spinand_device *spinand) 1733 { 1734 struct device *dev = &spinand->spimem->spi->dev; 1735 struct nand_device *nand = spinand_to_nand(spinand); 1736 int ret; 1737 1738 ret = spinand_reset_op(spinand); 1739 if (ret) 1740 return ret; 1741 1742 ret = spinand_id_detect(spinand); 1743 if (ret) { 1744 dev_err(dev, "unknown raw ID %*phN\n", SPINAND_MAX_ID_LEN, 1745 spinand->id.data); 1746 return ret; 1747 } 1748 1749 if (nand->memorg.ntargets > 1 && !spinand->select_target) { 1750 dev_err(dev, 1751 "SPI NANDs with more than one die must implement ->select_target()\n"); 1752 return -EINVAL; 1753 } 1754 1755 dev_info(&spinand->spimem->spi->dev, 1756 "%s SPI NAND was found.\n", spinand->manufacturer->name); 1757 dev_info(&spinand->spimem->spi->dev, 1758 "%llu MiB, block size: %zu KiB, page size: %zu, OOB size: %u\n", 1759 nanddev_size(nand) >> 20, nanddev_eraseblock_size(nand) >> 10, 1760 nanddev_page_size(nand), nanddev_per_page_oobsize(nand)); 1761 1762 return 0; 1763 } 1764 1765 static int spinand_configure_chip(struct spinand_device *spinand) 1766 { 1767 bool odtr = false, quad_enable = false; 1768 int ret; 1769 1770 if (spinand->odtr_op_templates.read_cache && 1771 spinand->odtr_op_templates.write_cache && 1772 spinand->odtr_op_templates.update_cache) 1773 odtr = true; 1774 1775 if (odtr) { 1776 if (!spinand->configure_chip) 1777 goto try_ssdr; 1778 1779 /* ODTR bus interface configuration happens here */ 1780 ret = spinand->configure_chip(spinand, ODTR); 1781 if (ret) { 1782 spinand->odtr_op_templates.read_cache = NULL; 1783 spinand->odtr_op_templates.write_cache = NULL; 1784 spinand->odtr_op_templates.update_cache = NULL; 1785 goto try_ssdr; 1786 } 1787 1788 spinand->op_templates = &spinand->odtr_op_templates; 1789 spinand->bus_iface = ODTR; 1790 1791 return 0; 1792 } 1793 1794 try_ssdr: 1795 if (spinand->flags & SPINAND_HAS_QE_BIT) { 1796 if (spinand->ssdr_op_templates.read_cache->data.buswidth == 4 || 1797 spinand->ssdr_op_templates.write_cache->data.buswidth == 4 || 1798 spinand->ssdr_op_templates.update_cache->data.buswidth == 4) 1799 quad_enable = true; 1800 } 1801 1802 ret = spinand_init_quad_enable(spinand, quad_enable); 1803 if (ret) 1804 return ret; 1805 1806 if (spinand->configure_chip) { 1807 ret = spinand->configure_chip(spinand, SSDR); 1808 if (ret) 1809 return ret; 1810 } 1811 1812 return ret; 1813 } 1814 1815 static int spinand_init_flash(struct spinand_device *spinand) 1816 { 1817 struct device *dev = &spinand->spimem->spi->dev; 1818 struct nand_device *nand = spinand_to_nand(spinand); 1819 int ret, i; 1820 1821 ret = spinand_read_cfg(spinand); 1822 if (ret) 1823 return ret; 1824 1825 ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0); 1826 if (ret) 1827 return ret; 1828 1829 ret = spinand_manufacturer_init(spinand); 1830 if (ret) { 1831 dev_err(dev, 1832 "Failed to initialize the SPI NAND chip (err = %d)\n", 1833 ret); 1834 return ret; 1835 } 1836 1837 ret = spinand_configure_chip(spinand); 1838 if (ret) 1839 goto manuf_cleanup; 1840 1841 /* After power up, all blocks are locked, so unlock them here. */ 1842 for (i = 0; i < nand->memorg.ntargets; i++) { 1843 ret = spinand_select_target(spinand, i); 1844 if (ret) 1845 goto manuf_cleanup; 1846 1847 ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED); 1848 if (ret) 1849 goto manuf_cleanup; 1850 } 1851 1852 return 0; 1853 1854 manuf_cleanup: 1855 spinand_manufacturer_cleanup(spinand); 1856 1857 return ret; 1858 } 1859 1860 static void spinand_mtd_resume(struct mtd_info *mtd) 1861 { 1862 struct spinand_device *spinand = mtd_to_spinand(mtd); 1863 int ret; 1864 1865 ret = spinand_reset_op(spinand); 1866 if (ret) 1867 return; 1868 1869 ret = spinand_init_flash(spinand); 1870 if (ret) 1871 return; 1872 1873 spinand_ecc_enable(spinand, false); 1874 } 1875 1876 static int spinand_mtd_suspend(struct mtd_info *mtd) 1877 { 1878 struct spinand_device *spinand = mtd_to_spinand(mtd); 1879 int ret; 1880 1881 /* 1882 * Return to SSDR interface in the suspend path to make sure the 1883 * reset operation is correctly processed upon resume. 1884 * 1885 * Note: Once back in SSDR mode, every operation but the page helpers 1886 * (dirmap based I/O accessors) will work. Page accesses would require 1887 * destroying and recreating the dirmaps twice to work, which would be 1888 * impacting for no reason, as this is just a transitional state. 1889 */ 1890 if (spinand->bus_iface == ODTR) { 1891 ret = spinand->configure_chip(spinand, SSDR); 1892 if (ret) 1893 return ret; 1894 1895 spinand->op_templates = &spinand->ssdr_op_templates; 1896 spinand->bus_iface = SSDR; 1897 } 1898 1899 return 0; 1900 } 1901 1902 static int spinand_init(struct spinand_device *spinand) 1903 { 1904 struct device *dev = &spinand->spimem->spi->dev; 1905 struct mtd_info *mtd = spinand_to_mtd(spinand); 1906 struct nand_device *nand = mtd_to_nanddev(mtd); 1907 int ret; 1908 1909 /* 1910 * We need a scratch buffer because the spi_mem interface requires that 1911 * buf passed in spi_mem_op->data.buf be DMA-able. 1912 */ 1913 spinand->scratchbuf = kzalloc(SPINAND_MAX_ID_LEN, GFP_KERNEL); 1914 if (!spinand->scratchbuf) 1915 return -ENOMEM; 1916 1917 spinand_init_ssdr_templates(spinand); 1918 1919 ret = spinand_detect(spinand); 1920 if (ret) 1921 goto err_free_bufs; 1922 1923 /* 1924 * Use kzalloc() instead of devm_kzalloc() here, because some drivers 1925 * may use this buffer for DMA access. 1926 * Memory allocated by devm_ does not guarantee DMA-safe alignment. 1927 */ 1928 spinand->databuf = kzalloc(nanddev_eraseblock_size(nand), 1929 GFP_KERNEL); 1930 if (!spinand->databuf) { 1931 ret = -ENOMEM; 1932 goto err_free_bufs; 1933 } 1934 1935 spinand->oobbuf = spinand->databuf + nanddev_page_size(nand); 1936 1937 ret = spinand_init_cfg_cache(spinand); 1938 if (ret) 1939 goto err_free_bufs; 1940 1941 ret = spinand_init_flash(spinand); 1942 if (ret) 1943 goto err_free_bufs; 1944 1945 ret = nanddev_init(nand, &spinand_ops, THIS_MODULE); 1946 if (ret) 1947 goto err_manuf_cleanup; 1948 1949 /* SPI-NAND default ECC engine is on-die */ 1950 nand->ecc.defaults.engine_type = NAND_ECC_ENGINE_TYPE_ON_DIE; 1951 nand->ecc.ondie_engine = &spinand_ondie_ecc_engine; 1952 1953 spinand_ecc_enable(spinand, false); 1954 ret = nanddev_ecc_engine_init(nand); 1955 if (ret) 1956 goto err_cleanup_nanddev; 1957 1958 /* 1959 * Continuous read can only be enabled with an on-die ECC engine, so the 1960 * ECC initialization must have happened previously. 1961 */ 1962 spinand_cont_read_init(spinand); 1963 ret = spinand_randomizer_init(spinand); 1964 if (ret) 1965 goto err_cleanup_nanddev; 1966 1967 mtd->_read_oob = spinand_mtd_read; 1968 mtd->_write_oob = spinand_mtd_write; 1969 mtd->_block_isbad = spinand_mtd_block_isbad; 1970 mtd->_block_markbad = spinand_mtd_block_markbad; 1971 mtd->_block_isreserved = spinand_mtd_block_isreserved; 1972 mtd->_erase = spinand_mtd_erase; 1973 mtd->_max_bad_blocks = nanddev_mtd_max_bad_blocks; 1974 mtd->_suspend = spinand_mtd_suspend; 1975 mtd->_resume = spinand_mtd_resume; 1976 1977 if (spinand_user_otp_size(spinand) || spinand_fact_otp_size(spinand)) { 1978 ret = spinand_set_mtd_otp_ops(spinand); 1979 if (ret) 1980 goto err_cleanup_ecc_engine; 1981 } 1982 1983 if (nand->ecc.engine) { 1984 ret = mtd_ooblayout_count_freebytes(mtd); 1985 if (ret < 0) 1986 goto err_cleanup_ecc_engine; 1987 } 1988 1989 mtd->oobavail = ret; 1990 1991 /* Propagate ECC information to mtd_info */ 1992 mtd->ecc_strength = nanddev_get_ecc_conf(nand)->strength; 1993 mtd->ecc_step_size = nanddev_get_ecc_conf(nand)->step_size; 1994 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); 1995 1996 ret = spinand_create_dirmaps(spinand); 1997 if (ret) { 1998 dev_err(dev, 1999 "Failed to create direct mappings for read/write operations (err = %d)\n", 2000 ret); 2001 goto err_cleanup_ecc_engine; 2002 } 2003 2004 return 0; 2005 2006 err_cleanup_ecc_engine: 2007 nanddev_ecc_engine_cleanup(nand); 2008 2009 err_cleanup_nanddev: 2010 nanddev_cleanup(nand); 2011 2012 err_manuf_cleanup: 2013 spinand_manufacturer_cleanup(spinand); 2014 2015 err_free_bufs: 2016 kfree(spinand->databuf); 2017 kfree(spinand->scratchbuf); 2018 return ret; 2019 } 2020 2021 static void spinand_cleanup(struct spinand_device *spinand) 2022 { 2023 struct nand_device *nand = spinand_to_nand(spinand); 2024 2025 nanddev_ecc_engine_cleanup(nand); 2026 nanddev_cleanup(nand); 2027 spinand_manufacturer_cleanup(spinand); 2028 kfree(spinand->databuf); 2029 kfree(spinand->scratchbuf); 2030 } 2031 2032 static int spinand_probe(struct spi_mem *mem) 2033 { 2034 struct spinand_device *spinand; 2035 struct mtd_info *mtd; 2036 int ret; 2037 2038 spinand = devm_kzalloc(&mem->spi->dev, sizeof(*spinand), 2039 GFP_KERNEL); 2040 if (!spinand) 2041 return -ENOMEM; 2042 2043 spinand->spimem = mem; 2044 spi_mem_set_drvdata(mem, spinand); 2045 spinand_set_of_node(spinand, mem->spi->dev.of_node); 2046 mutex_init(&spinand->lock); 2047 mtd = spinand_to_mtd(spinand); 2048 mtd->dev.parent = &mem->spi->dev; 2049 2050 ret = spinand_init(spinand); 2051 if (ret) 2052 return ret; 2053 2054 ret = mtd_device_register(mtd, NULL, 0); 2055 if (ret) 2056 goto err_spinand_cleanup; 2057 2058 return 0; 2059 2060 err_spinand_cleanup: 2061 spinand_cleanup(spinand); 2062 2063 return ret; 2064 } 2065 2066 static int spinand_remove(struct spi_mem *mem) 2067 { 2068 struct spinand_device *spinand; 2069 struct mtd_info *mtd; 2070 int ret; 2071 2072 spinand = spi_mem_get_drvdata(mem); 2073 mtd = spinand_to_mtd(spinand); 2074 2075 ret = mtd_device_unregister(mtd); 2076 if (ret) 2077 return ret; 2078 2079 spinand_cleanup(spinand); 2080 2081 return 0; 2082 } 2083 2084 static const struct spi_device_id spinand_ids[] = { 2085 { .name = "spi-nand" }, 2086 { /* sentinel */ }, 2087 }; 2088 MODULE_DEVICE_TABLE(spi, spinand_ids); 2089 2090 #ifdef CONFIG_OF 2091 static const struct of_device_id spinand_of_ids[] = { 2092 { .compatible = "spi-nand" }, 2093 { /* sentinel */ }, 2094 }; 2095 MODULE_DEVICE_TABLE(of, spinand_of_ids); 2096 #endif 2097 2098 static struct spi_mem_driver spinand_drv = { 2099 .spidrv = { 2100 .id_table = spinand_ids, 2101 .driver = { 2102 .name = "spi-nand", 2103 .of_match_table = of_match_ptr(spinand_of_ids), 2104 }, 2105 }, 2106 .probe = spinand_probe, 2107 .remove = spinand_remove, 2108 }; 2109 module_spi_mem_driver(spinand_drv); 2110 2111 MODULE_DESCRIPTION("SPI NAND framework"); 2112 MODULE_AUTHOR("Peter Pan<peterpandong@micron.com>"); 2113 MODULE_LICENSE("GPL v2"); 2114