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