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 #ifndef __UBOOT__ 13 #include <linux/device.h> 14 #include <linux/jiffies.h> 15 #include <linux/kernel.h> 16 #include <linux/module.h> 17 #include <linux/mtd/spinand.h> 18 #include <linux/of.h> 19 #include <linux/slab.h> 20 #include <linux/spi/spi.h> 21 #include <linux/spi/spi-mem.h> 22 #else 23 #include <common.h> 24 #include <errno.h> 25 #include <spi.h> 26 #include <spi-mem.h> 27 #include <linux/mtd/spinand.h> 28 #endif 29 30 /* SPI NAND index visible in MTD names */ 31 static int spi_nand_idx; 32 33 static void spinand_cache_op_adjust_colum(struct spinand_device *spinand, 34 const struct nand_page_io_req *req, 35 u16 *column) 36 { 37 struct nand_device *nand = spinand_to_nand(spinand); 38 unsigned int shift; 39 40 if (nand->memorg.planes_per_lun < 2) 41 return; 42 43 /* The plane number is passed in MSB just above the column address */ 44 shift = fls(nand->memorg.pagesize); 45 *column |= req->pos.plane << shift; 46 } 47 48 static int spinand_read_reg_op(struct spinand_device *spinand, u8 reg, u8 *val) 49 { 50 struct spi_mem_op op = SPINAND_GET_FEATURE_OP(reg, 51 spinand->scratchbuf); 52 int ret; 53 54 ret = spi_mem_exec_op(spinand->slave, &op); 55 if (ret) 56 return ret; 57 58 *val = *spinand->scratchbuf; 59 return 0; 60 } 61 62 static int spinand_write_reg_op(struct spinand_device *spinand, u8 reg, u8 val) 63 { 64 struct spi_mem_op op = SPINAND_SET_FEATURE_OP(reg, 65 spinand->scratchbuf); 66 67 *spinand->scratchbuf = val; 68 return spi_mem_exec_op(spinand->slave, &op); 69 } 70 71 static int spinand_read_status(struct spinand_device *spinand, u8 *status) 72 { 73 return spinand_read_reg_op(spinand, REG_STATUS, status); 74 } 75 76 static int spinand_get_cfg(struct spinand_device *spinand, u8 *cfg) 77 { 78 struct nand_device *nand = spinand_to_nand(spinand); 79 80 if (WARN_ON(spinand->cur_target < 0 || 81 spinand->cur_target >= nand->memorg.ntargets)) 82 return -EINVAL; 83 84 *cfg = spinand->cfg_cache[spinand->cur_target]; 85 return 0; 86 } 87 88 static int spinand_set_cfg(struct spinand_device *spinand, u8 cfg) 89 { 90 struct nand_device *nand = spinand_to_nand(spinand); 91 int ret; 92 93 if (WARN_ON(spinand->cur_target < 0 || 94 spinand->cur_target >= nand->memorg.ntargets)) 95 return -EINVAL; 96 97 if (spinand->cfg_cache[spinand->cur_target] == cfg) 98 return 0; 99 100 ret = spinand_write_reg_op(spinand, REG_CFG, cfg); 101 if (ret) 102 return ret; 103 104 spinand->cfg_cache[spinand->cur_target] = cfg; 105 return 0; 106 } 107 108 /** 109 * spinand_upd_cfg() - Update the configuration register 110 * @spinand: the spinand device 111 * @mask: the mask encoding the bits to update in the config reg 112 * @val: the new value to apply 113 * 114 * Update the configuration register. 115 * 116 * Return: 0 on success, a negative error code otherwise. 117 */ 118 int spinand_upd_cfg(struct spinand_device *spinand, u8 mask, u8 val) 119 { 120 int ret; 121 u8 cfg; 122 123 ret = spinand_get_cfg(spinand, &cfg); 124 if (ret) 125 return ret; 126 127 cfg &= ~mask; 128 cfg |= val; 129 130 return spinand_set_cfg(spinand, cfg); 131 } 132 133 /** 134 * spinand_select_target() - Select a specific NAND target/die 135 * @spinand: the spinand device 136 * @target: the target/die to select 137 * 138 * Select a new target/die. If chip only has one die, this function is a NOOP. 139 * 140 * Return: 0 on success, a negative error code otherwise. 141 */ 142 int spinand_select_target(struct spinand_device *spinand, unsigned int target) 143 { 144 struct nand_device *nand = spinand_to_nand(spinand); 145 int ret; 146 147 if (WARN_ON(target >= nand->memorg.ntargets)) 148 return -EINVAL; 149 150 if (spinand->cur_target == target) 151 return 0; 152 153 if (nand->memorg.ntargets == 1) { 154 spinand->cur_target = target; 155 return 0; 156 } 157 158 ret = spinand->select_target(spinand, target); 159 if (ret) 160 return ret; 161 162 spinand->cur_target = target; 163 return 0; 164 } 165 166 static int spinand_init_cfg_cache(struct spinand_device *spinand) 167 { 168 struct nand_device *nand = spinand_to_nand(spinand); 169 struct udevice *dev = spinand->slave->dev; 170 unsigned int target; 171 int ret; 172 173 spinand->cfg_cache = devm_kzalloc(dev, 174 sizeof(*spinand->cfg_cache) * 175 nand->memorg.ntargets, 176 GFP_KERNEL); 177 if (!spinand->cfg_cache) 178 return -ENOMEM; 179 180 for (target = 0; target < nand->memorg.ntargets; target++) { 181 ret = spinand_select_target(spinand, target); 182 if (ret) 183 return ret; 184 185 /* 186 * We use spinand_read_reg_op() instead of spinand_get_cfg() 187 * here to bypass the config cache. 188 */ 189 ret = spinand_read_reg_op(spinand, REG_CFG, 190 &spinand->cfg_cache[target]); 191 if (ret) 192 return ret; 193 } 194 195 return 0; 196 } 197 198 static int spinand_init_quad_enable(struct spinand_device *spinand) 199 { 200 bool enable = false; 201 202 if (!(spinand->flags & SPINAND_HAS_QE_BIT)) 203 return 0; 204 205 if (spinand->op_templates.read_cache->data.buswidth == 4 || 206 spinand->op_templates.write_cache->data.buswidth == 4 || 207 spinand->op_templates.update_cache->data.buswidth == 4) 208 enable = true; 209 210 return spinand_upd_cfg(spinand, CFG_QUAD_ENABLE, 211 enable ? CFG_QUAD_ENABLE : 0); 212 } 213 214 static int spinand_ecc_enable(struct spinand_device *spinand, 215 bool enable) 216 { 217 return spinand_upd_cfg(spinand, CFG_ECC_ENABLE, 218 enable ? CFG_ECC_ENABLE : 0); 219 } 220 221 static int spinand_write_enable_op(struct spinand_device *spinand) 222 { 223 struct spi_mem_op op = SPINAND_WR_EN_DIS_OP(true); 224 225 return spi_mem_exec_op(spinand->slave, &op); 226 } 227 228 static int spinand_load_page_op(struct spinand_device *spinand, 229 const struct nand_page_io_req *req) 230 { 231 struct nand_device *nand = spinand_to_nand(spinand); 232 unsigned int row = nanddev_pos_to_row(nand, &req->pos); 233 struct spi_mem_op op = SPINAND_PAGE_READ_OP(row); 234 235 return spi_mem_exec_op(spinand->slave, &op); 236 } 237 238 static int spinand_read_from_cache_op(struct spinand_device *spinand, 239 const struct nand_page_io_req *req) 240 { 241 struct spi_mem_op op = *spinand->op_templates.read_cache; 242 struct nand_device *nand = spinand_to_nand(spinand); 243 struct mtd_info *mtd = nanddev_to_mtd(nand); 244 struct nand_page_io_req adjreq = *req; 245 unsigned int nbytes = 0; 246 void *buf = NULL; 247 u16 column = 0; 248 int ret; 249 250 if (req->datalen) { 251 adjreq.datalen = nanddev_page_size(nand); 252 adjreq.dataoffs = 0; 253 adjreq.databuf.in = spinand->databuf; 254 buf = spinand->databuf; 255 nbytes = adjreq.datalen; 256 } 257 258 if (req->ooblen) { 259 adjreq.ooblen = nanddev_per_page_oobsize(nand); 260 adjreq.ooboffs = 0; 261 adjreq.oobbuf.in = spinand->oobbuf; 262 nbytes += nanddev_per_page_oobsize(nand); 263 if (!buf) { 264 buf = spinand->oobbuf; 265 column = nanddev_page_size(nand); 266 } 267 } 268 269 spinand_cache_op_adjust_colum(spinand, &adjreq, &column); 270 op.addr.val = column; 271 272 /* 273 * Some controllers are limited in term of max RX data size. In this 274 * case, just repeat the READ_CACHE operation after updating the 275 * column. 276 */ 277 while (nbytes) { 278 op.data.buf.in = buf; 279 op.data.nbytes = nbytes; 280 ret = spi_mem_adjust_op_size(spinand->slave, &op); 281 if (ret) 282 return ret; 283 284 ret = spi_mem_exec_op(spinand->slave, &op); 285 if (ret) 286 return ret; 287 288 buf += op.data.nbytes; 289 nbytes -= op.data.nbytes; 290 op.addr.val += op.data.nbytes; 291 } 292 293 if (req->datalen) 294 memcpy(req->databuf.in, spinand->databuf + req->dataoffs, 295 req->datalen); 296 297 if (req->ooblen) { 298 if (req->mode == MTD_OPS_AUTO_OOB) 299 mtd_ooblayout_get_databytes(mtd, req->oobbuf.in, 300 spinand->oobbuf, 301 req->ooboffs, 302 req->ooblen); 303 else 304 memcpy(req->oobbuf.in, spinand->oobbuf + req->ooboffs, 305 req->ooblen); 306 } 307 308 return 0; 309 } 310 311 static int spinand_write_to_cache_op(struct spinand_device *spinand, 312 const struct nand_page_io_req *req) 313 { 314 struct spi_mem_op op = *spinand->op_templates.write_cache; 315 struct nand_device *nand = spinand_to_nand(spinand); 316 struct mtd_info *mtd = nanddev_to_mtd(nand); 317 struct nand_page_io_req adjreq = *req; 318 unsigned int nbytes = 0; 319 void *buf = NULL; 320 u16 column = 0; 321 int ret; 322 323 /* 324 * Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset 325 * the cache content to 0xFF (depends on vendor implementation), so we 326 * must fill the page cache entirely even if we only want to program 327 * the data portion of the page, otherwise we might corrupt the BBM or 328 * user data previously programmed in OOB area. 329 */ 330 memset(spinand->databuf, 0xff, 331 nanddev_page_size(nand) + 332 nanddev_per_page_oobsize(nand)); 333 334 if (req->datalen) { 335 memcpy(spinand->databuf + req->dataoffs, req->databuf.out, 336 req->datalen); 337 adjreq.dataoffs = 0; 338 adjreq.datalen = nanddev_page_size(nand); 339 adjreq.databuf.out = spinand->databuf; 340 nbytes = adjreq.datalen; 341 buf = spinand->databuf; 342 } 343 344 if (req->ooblen) { 345 if (req->mode == MTD_OPS_AUTO_OOB) 346 mtd_ooblayout_set_databytes(mtd, req->oobbuf.out, 347 spinand->oobbuf, 348 req->ooboffs, 349 req->ooblen); 350 else 351 memcpy(spinand->oobbuf + req->ooboffs, req->oobbuf.out, 352 req->ooblen); 353 354 adjreq.ooblen = nanddev_per_page_oobsize(nand); 355 adjreq.ooboffs = 0; 356 nbytes += nanddev_per_page_oobsize(nand); 357 if (!buf) { 358 buf = spinand->oobbuf; 359 column = nanddev_page_size(nand); 360 } 361 } 362 363 spinand_cache_op_adjust_colum(spinand, &adjreq, &column); 364 365 op = *spinand->op_templates.write_cache; 366 op.addr.val = column; 367 368 /* 369 * Some controllers are limited in term of max TX data size. In this 370 * case, split the operation into one LOAD CACHE and one or more 371 * LOAD RANDOM CACHE. 372 */ 373 while (nbytes) { 374 op.data.buf.out = buf; 375 op.data.nbytes = nbytes; 376 377 ret = spi_mem_adjust_op_size(spinand->slave, &op); 378 if (ret) 379 return ret; 380 381 ret = spi_mem_exec_op(spinand->slave, &op); 382 if (ret) 383 return ret; 384 385 buf += op.data.nbytes; 386 nbytes -= op.data.nbytes; 387 op.addr.val += op.data.nbytes; 388 389 /* 390 * We need to use the RANDOM LOAD CACHE operation if there's 391 * more than one iteration, because the LOAD operation resets 392 * the cache to 0xff. 393 */ 394 if (nbytes) { 395 column = op.addr.val; 396 op = *spinand->op_templates.update_cache; 397 op.addr.val = column; 398 } 399 } 400 401 return 0; 402 } 403 404 static int spinand_program_op(struct spinand_device *spinand, 405 const struct nand_page_io_req *req) 406 { 407 struct nand_device *nand = spinand_to_nand(spinand); 408 unsigned int row = nanddev_pos_to_row(nand, &req->pos); 409 struct spi_mem_op op = SPINAND_PROG_EXEC_OP(row); 410 411 return spi_mem_exec_op(spinand->slave, &op); 412 } 413 414 static int spinand_erase_op(struct spinand_device *spinand, 415 const struct nand_pos *pos) 416 { 417 struct nand_device *nand = &spinand->base; 418 unsigned int row = nanddev_pos_to_row(nand, pos); 419 struct spi_mem_op op = SPINAND_BLK_ERASE_OP(row); 420 421 return spi_mem_exec_op(spinand->slave, &op); 422 } 423 424 static int spinand_wait(struct spinand_device *spinand, u8 *s) 425 { 426 unsigned long start, stop; 427 u8 status; 428 int ret; 429 430 start = get_timer(0); 431 stop = 400; 432 do { 433 ret = spinand_read_status(spinand, &status); 434 if (ret) 435 return ret; 436 437 if (!(status & STATUS_BUSY)) 438 goto out; 439 } while (get_timer(start) < stop); 440 441 /* 442 * Extra read, just in case the STATUS_READY bit has changed 443 * since our last check 444 */ 445 ret = spinand_read_status(spinand, &status); 446 if (ret) 447 return ret; 448 449 out: 450 if (s) 451 *s = status; 452 453 return status & STATUS_BUSY ? -ETIMEDOUT : 0; 454 } 455 456 static int spinand_read_id_op(struct spinand_device *spinand, u8 naddr, 457 u8 ndummy, u8 *buf) 458 { 459 struct spi_mem_op op = SPINAND_READID_OP( 460 naddr, ndummy, spinand->scratchbuf, SPINAND_MAX_ID_LEN); 461 int ret; 462 463 ret = spi_mem_exec_op(spinand->slave, &op); 464 if (!ret) 465 memcpy(buf, spinand->scratchbuf, SPINAND_MAX_ID_LEN); 466 467 return ret; 468 } 469 470 #if !CONFIG_IS_ENABLED(SUPPORT_USBPLUG) 471 static int spinand_reset_op(struct spinand_device *spinand) 472 { 473 struct spi_mem_op op = SPINAND_RESET_OP; 474 int ret; 475 476 ret = spi_mem_exec_op(spinand->slave, &op); 477 if (ret) 478 return ret; 479 480 return spinand_wait(spinand, NULL); 481 } 482 #endif 483 484 static int spinand_lock_block(struct spinand_device *spinand, u8 lock) 485 { 486 return spinand_write_reg_op(spinand, REG_BLOCK_LOCK, lock); 487 } 488 489 static int spinand_check_ecc_status(struct spinand_device *spinand, u8 status) 490 { 491 struct nand_device *nand = spinand_to_nand(spinand); 492 493 if (spinand->eccinfo.get_status) 494 return spinand->eccinfo.get_status(spinand, status); 495 496 switch (status & STATUS_ECC_MASK) { 497 case STATUS_ECC_NO_BITFLIPS: 498 return 0; 499 500 case STATUS_ECC_HAS_BITFLIPS: 501 /* 502 * We have no way to know exactly how many bitflips have been 503 * fixed, so let's return the maximum possible value so that 504 * wear-leveling layers move the data immediately. 505 */ 506 return nand->eccreq.strength; 507 508 case STATUS_ECC_UNCOR_ERROR: 509 return -EBADMSG; 510 511 default: 512 break; 513 } 514 515 return -EINVAL; 516 } 517 518 static int spinand_read_page(struct spinand_device *spinand, 519 const struct nand_page_io_req *req, 520 bool ecc_enabled) 521 { 522 u8 status = 0; 523 int ret; 524 525 ret = spinand_load_page_op(spinand, req); 526 if (ret) 527 return ret; 528 529 ret = spinand_wait(spinand, &status); 530 /* 531 * When there is data outside of OIP in the status, the status data is 532 * inaccurate and needs to be reconfirmed 533 */ 534 if (spinand->id.data[0] == 0x01 && status && !ret) 535 ret = spinand_wait(spinand, &status); 536 if (ret < 0) 537 return ret; 538 539 ret = spinand_read_from_cache_op(spinand, req); 540 if (ret) 541 return ret; 542 543 if (!ecc_enabled) 544 return 0; 545 546 return spinand_check_ecc_status(spinand, status); 547 } 548 549 static int spinand_write_page(struct spinand_device *spinand, 550 const struct nand_page_io_req *req) 551 { 552 u8 status; 553 int ret; 554 555 ret = spinand_write_enable_op(spinand); 556 if (ret) 557 return ret; 558 559 ret = spinand_write_to_cache_op(spinand, req); 560 if (ret) 561 return ret; 562 563 ret = spinand_program_op(spinand, req); 564 if (ret) 565 return ret; 566 567 ret = spinand_wait(spinand, &status); 568 if (!ret && (status & STATUS_PROG_FAILED)) 569 ret = -EIO; 570 571 return ret; 572 } 573 574 static int spinand_mtd_read(struct mtd_info *mtd, loff_t from, 575 struct mtd_oob_ops *ops) 576 { 577 struct spinand_device *spinand = mtd_to_spinand(mtd); 578 struct nand_device *nand = mtd_to_nanddev(mtd); 579 unsigned int max_bitflips = 0; 580 struct nand_io_iter iter; 581 bool enable_ecc = false; 582 bool ecc_failed = false; 583 int ret = 0; 584 585 if (ops->mode != MTD_OPS_RAW && spinand->eccinfo.ooblayout) 586 enable_ecc = true; 587 588 #ifndef __UBOOT__ 589 mutex_lock(&spinand->lock); 590 #endif 591 592 nanddev_io_for_each_page(nand, from, ops, &iter) { 593 ret = spinand_select_target(spinand, iter.req.pos.target); 594 if (ret) 595 break; 596 597 ret = spinand_ecc_enable(spinand, enable_ecc); 598 if (ret) 599 break; 600 601 ret = spinand_read_page(spinand, &iter.req, enable_ecc); 602 if (ret < 0 && ret != -EBADMSG) 603 break; 604 605 if (ret == -EBADMSG) { 606 ecc_failed = true; 607 mtd->ecc_stats.failed++; 608 } else { 609 mtd->ecc_stats.corrected += ret; 610 max_bitflips = max_t(unsigned int, max_bitflips, ret); 611 } 612 613 ret = 0; 614 ops->retlen += iter.req.datalen; 615 ops->oobretlen += iter.req.ooblen; 616 } 617 618 #ifndef __UBOOT__ 619 mutex_unlock(&spinand->lock); 620 #endif 621 if (ecc_failed && !ret) 622 ret = -EBADMSG; 623 624 return ret ? ret : max_bitflips; 625 } 626 627 static int spinand_mtd_write(struct mtd_info *mtd, loff_t to, 628 struct mtd_oob_ops *ops) 629 { 630 struct spinand_device *spinand = mtd_to_spinand(mtd); 631 struct nand_device *nand = mtd_to_nanddev(mtd); 632 struct nand_io_iter iter; 633 bool enable_ecc = false; 634 int ret = 0; 635 636 if (ops->mode != MTD_OPS_RAW && mtd->ooblayout) 637 enable_ecc = true; 638 639 #ifndef __UBOOT__ 640 mutex_lock(&spinand->lock); 641 #endif 642 643 nanddev_io_for_each_page(nand, to, ops, &iter) { 644 ret = spinand_select_target(spinand, iter.req.pos.target); 645 if (ret) 646 break; 647 648 ret = spinand_ecc_enable(spinand, enable_ecc); 649 if (ret) 650 break; 651 652 ret = spinand_write_page(spinand, &iter.req); 653 if (ret) 654 break; 655 656 ops->retlen += iter.req.datalen; 657 ops->oobretlen += iter.req.ooblen; 658 } 659 660 #ifndef __UBOOT__ 661 mutex_unlock(&spinand->lock); 662 #endif 663 664 return ret; 665 } 666 667 static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos) 668 { 669 struct spinand_device *spinand = nand_to_spinand(nand); 670 u8 marker[2] = { }; 671 struct nand_page_io_req req = { 672 .pos = *pos, 673 .ooblen = sizeof(marker), 674 .ooboffs = 0, 675 .oobbuf.in = marker, 676 .mode = MTD_OPS_RAW, 677 }; 678 679 spinand_select_target(spinand, pos->target); 680 spinand_read_page(spinand, &req, false); 681 if (marker[0] != 0xff || marker[1] != 0xff) 682 return true; 683 684 return false; 685 } 686 687 static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs) 688 { 689 struct nand_device *nand = mtd_to_nanddev(mtd); 690 #ifndef __UBOOT__ 691 struct spinand_device *spinand = nand_to_spinand(nand); 692 #endif 693 struct nand_pos pos; 694 int ret; 695 696 nanddev_offs_to_pos(nand, offs, &pos); 697 #ifndef __UBOOT__ 698 mutex_lock(&spinand->lock); 699 #endif 700 ret = nanddev_isbad(nand, &pos); 701 #ifndef __UBOOT__ 702 mutex_unlock(&spinand->lock); 703 #endif 704 return ret; 705 } 706 707 static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos) 708 { 709 struct spinand_device *spinand = nand_to_spinand(nand); 710 u8 marker[2] = { 0, 0 }; 711 struct nand_page_io_req req = { 712 .pos = *pos, 713 .ooboffs = 0, 714 .ooblen = sizeof(marker), 715 .oobbuf.out = marker, 716 .mode = MTD_OPS_RAW, 717 }; 718 int ret; 719 720 ret = spinand_select_target(spinand, pos->target); 721 if (ret) 722 return ret; 723 724 ret = spinand_write_enable_op(spinand); 725 if (ret) 726 return ret; 727 728 return spinand_write_page(spinand, &req); 729 } 730 731 static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs) 732 { 733 struct nand_device *nand = mtd_to_nanddev(mtd); 734 #ifndef __UBOOT__ 735 struct spinand_device *spinand = nand_to_spinand(nand); 736 #endif 737 struct nand_pos pos; 738 int ret; 739 740 nanddev_offs_to_pos(nand, offs, &pos); 741 #ifndef __UBOOT__ 742 mutex_lock(&spinand->lock); 743 #endif 744 ret = nanddev_markbad(nand, &pos); 745 #ifndef __UBOOT__ 746 mutex_unlock(&spinand->lock); 747 #endif 748 return ret; 749 } 750 751 static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos) 752 { 753 struct spinand_device *spinand = nand_to_spinand(nand); 754 u8 status; 755 int ret; 756 757 ret = spinand_select_target(spinand, pos->target); 758 if (ret) 759 return ret; 760 761 ret = spinand_write_enable_op(spinand); 762 if (ret) 763 return ret; 764 765 ret = spinand_erase_op(spinand, pos); 766 if (ret) 767 return ret; 768 769 ret = spinand_wait(spinand, &status); 770 if (!ret && (status & STATUS_ERASE_FAILED)) 771 ret = -EIO; 772 773 return ret; 774 } 775 776 static int spinand_mtd_erase(struct mtd_info *mtd, 777 struct erase_info *einfo) 778 { 779 #ifndef __UBOOT__ 780 struct spinand_device *spinand = mtd_to_spinand(mtd); 781 #endif 782 int ret; 783 784 #ifndef __UBOOT__ 785 mutex_lock(&spinand->lock); 786 #endif 787 ret = nanddev_mtd_erase(mtd, einfo); 788 #ifndef __UBOOT__ 789 mutex_unlock(&spinand->lock); 790 #endif 791 792 return ret; 793 } 794 795 static int spinand_mtd_block_isreserved(struct mtd_info *mtd, loff_t offs) 796 { 797 #ifndef __UBOOT__ 798 struct spinand_device *spinand = mtd_to_spinand(mtd); 799 #endif 800 struct nand_device *nand = mtd_to_nanddev(mtd); 801 struct nand_pos pos; 802 int ret; 803 804 nanddev_offs_to_pos(nand, offs, &pos); 805 #ifndef __UBOOT__ 806 mutex_lock(&spinand->lock); 807 #endif 808 ret = nanddev_isreserved(nand, &pos); 809 #ifndef __UBOOT__ 810 mutex_unlock(&spinand->lock); 811 #endif 812 813 return ret; 814 } 815 816 const struct spi_mem_op * 817 spinand_find_supported_op(struct spinand_device *spinand, 818 const struct spi_mem_op *ops, 819 unsigned int nops) 820 { 821 unsigned int i; 822 823 for (i = 0; i < nops; i++) { 824 if (spi_mem_supports_op(spinand->slave, &ops[i])) 825 return &ops[i]; 826 } 827 828 return NULL; 829 } 830 831 static const struct nand_ops spinand_ops = { 832 .erase = spinand_erase, 833 .markbad = spinand_markbad, 834 .isbad = spinand_isbad, 835 }; 836 837 static const struct spinand_manufacturer *spinand_manufacturers[] = { 838 #ifdef CONFIG_SPI_NAND_GIGADEVICE 839 &gigadevice_spinand_manufacturer, 840 #endif 841 #ifdef CONFIG_SPI_NAND_MACRONIX 842 ¯onix_spinand_manufacturer, 843 #endif 844 #ifdef CONFIG_SPI_NAND_MICRON 845 µn_spinand_manufacturer, 846 #endif 847 #ifdef CONFIG_SPI_NAND_TOSHIBA 848 &toshiba_spinand_manufacturer, 849 #endif 850 #ifdef CONFIG_SPI_NAND_WINBOND 851 &winbond_spinand_manufacturer, 852 #endif 853 #ifdef CONFIG_SPI_NAND_DOSILICON 854 &dosilicon_spinand_manufacturer, 855 #endif 856 #ifdef CONFIG_SPI_NAND_ESMT 857 &esmt_spinand_manufacturer, 858 #endif 859 #ifdef CONFIG_SPI_NAND_XINCUN 860 &xincun_spinand_manufacturer, 861 #endif 862 #ifdef CONFIG_SPI_NAND_XTX 863 &xtx_spinand_manufacturer, 864 #endif 865 #ifdef CONFIG_SPI_NAND_HYF 866 &hyf_spinand_manufacturer, 867 #endif 868 #ifdef CONFIG_SPI_NAND_FMSH 869 &fmsh_spinand_manufacturer, 870 #endif 871 #ifdef CONFIG_SPI_NAND_FORESEE 872 &foresee_spinand_manufacturer, 873 #endif 874 #ifdef CONFIG_SPI_NAND_BIWIN 875 &biwin_spinand_manufacturer, 876 #endif 877 #ifdef CONFIG_SPI_NAND_ETRON 878 &etron_spinand_manufacturer, 879 #endif 880 #ifdef CONFIG_SPI_NAND_JSC 881 &jsc_spinand_manufacturer, 882 #endif 883 #ifdef CONFIG_SPI_NAND_SILICONGO 884 &silicongo_spinand_manufacturer, 885 #endif 886 #ifdef CONFIG_SPI_NAND_UNIM 887 &unim_spinand_manufacturer, 888 &unim_zl_spinand_manufacturer, 889 #endif 890 #ifdef CONFIG_SPI_NAND_SKYHIGH 891 &skyhigh_spinand_manufacturer, 892 #endif 893 #ifdef CONFIG_SPI_NAND_GSTO 894 &gsto_spinand_manufacturer, 895 #endif 896 #ifdef CONFIG_SPI_NAND_ZBIT 897 &zbit_spinand_manufacturer, 898 #endif 899 }; 900 901 static int spinand_manufacturer_match(struct spinand_device *spinand, 902 enum spinand_readid_method rdid_method) 903 { 904 u8 *id = spinand->id.data; 905 unsigned int i; 906 int ret; 907 908 for (i = 0; i < ARRAY_SIZE(spinand_manufacturers); i++) { 909 const struct spinand_manufacturer *manufacturer = 910 spinand_manufacturers[i]; 911 912 if (id[0] != manufacturer->id) 913 continue; 914 915 ret = spinand_match_and_init(spinand, 916 manufacturer->chips, 917 manufacturer->nchips, 918 rdid_method); 919 if (ret < 0) 920 continue; 921 922 spinand->manufacturer = manufacturer; 923 return 0; 924 } 925 return -ENOTSUPP; 926 } 927 928 static int spinand_id_detect(struct spinand_device *spinand) 929 { 930 u8 *id = spinand->id.data; 931 int ret; 932 933 ret = spinand_read_id_op(spinand, 0, 0, id); 934 if (ret) 935 return ret; 936 ret = spinand_manufacturer_match(spinand, SPINAND_READID_METHOD_OPCODE); 937 if (!ret) 938 return 0; 939 940 ret = spinand_read_id_op(spinand, 1, 0, id); 941 if (ret) 942 return ret; 943 ret = spinand_manufacturer_match(spinand, 944 SPINAND_READID_METHOD_OPCODE_ADDR); 945 if (!ret) 946 return 0; 947 948 ret = spinand_read_id_op(spinand, 0, 1, id); 949 if (ret) 950 return ret; 951 ret = spinand_manufacturer_match(spinand, 952 SPINAND_READID_METHOD_OPCODE_DUMMY); 953 954 return ret; 955 } 956 957 static int spinand_manufacturer_init(struct spinand_device *spinand) 958 { 959 if (spinand->manufacturer->ops->init) 960 return spinand->manufacturer->ops->init(spinand); 961 962 return 0; 963 } 964 965 static void spinand_manufacturer_cleanup(struct spinand_device *spinand) 966 { 967 /* Release manufacturer private data */ 968 if (spinand->manufacturer->ops->cleanup) 969 return spinand->manufacturer->ops->cleanup(spinand); 970 } 971 972 static const struct spi_mem_op * 973 spinand_select_op_variant(struct spinand_device *spinand, 974 const struct spinand_op_variants *variants) 975 { 976 struct nand_device *nand = spinand_to_nand(spinand); 977 unsigned int i; 978 979 for (i = 0; i < variants->nops; i++) { 980 struct spi_mem_op op = variants->ops[i]; 981 unsigned int nbytes; 982 int ret; 983 984 nbytes = nanddev_per_page_oobsize(nand) + 985 nanddev_page_size(nand); 986 987 while (nbytes) { 988 op.data.nbytes = nbytes; 989 ret = spi_mem_adjust_op_size(spinand->slave, &op); 990 if (ret) 991 break; 992 993 if (!spi_mem_supports_op(spinand->slave, &op)) 994 break; 995 996 nbytes -= op.data.nbytes; 997 } 998 999 if (!nbytes) 1000 return &variants->ops[i]; 1001 } 1002 1003 return NULL; 1004 } 1005 1006 /** 1007 * spinand_match_and_init() - Try to find a match between a device ID and an 1008 * entry in a spinand_info table 1009 * @spinand: SPI NAND object 1010 * @table: SPI NAND device description table 1011 * @table_size: size of the device description table 1012 * @rdid_method: read id method to match 1013 * 1014 * Match between a device ID retrieved through the READ_ID command and an 1015 * entry in the SPI NAND description table. If a match is found, the spinand 1016 * object will be initialized with information provided by the matching 1017 * spinand_info entry. 1018 * 1019 * Return: 0 on success, a negative error code otherwise. 1020 */ 1021 int spinand_match_and_init(struct spinand_device *spinand, 1022 const struct spinand_info *table, 1023 unsigned int table_size, 1024 enum spinand_readid_method rdid_method) 1025 { 1026 u8 *id = spinand->id.data; 1027 struct nand_device *nand = spinand_to_nand(spinand); 1028 unsigned int i; 1029 1030 for (i = 0; i < table_size; i++) { 1031 const struct spinand_info *info = &table[i]; 1032 const struct spi_mem_op *op; 1033 1034 if (rdid_method != info->devid.method) 1035 continue; 1036 1037 if (memcmp(id + 1, info->devid.id, info->devid.len)) 1038 continue; 1039 1040 nand->memorg = table[i].memorg; 1041 nand->eccreq = table[i].eccreq; 1042 spinand->eccinfo = table[i].eccinfo; 1043 spinand->flags = table[i].flags; 1044 spinand->id.len = 1 + table[i].devid.len; 1045 spinand->select_target = table[i].select_target; 1046 1047 op = spinand_select_op_variant(spinand, 1048 info->op_variants.read_cache); 1049 if (!op) 1050 return -ENOTSUPP; 1051 1052 spinand->op_templates.read_cache = op; 1053 1054 op = spinand_select_op_variant(spinand, 1055 info->op_variants.write_cache); 1056 if (!op) 1057 return -ENOTSUPP; 1058 1059 spinand->op_templates.write_cache = op; 1060 1061 op = spinand_select_op_variant(spinand, 1062 info->op_variants.update_cache); 1063 spinand->op_templates.update_cache = op; 1064 1065 return 0; 1066 } 1067 1068 return -ENOTSUPP; 1069 } 1070 1071 static int spinand_detect(struct spinand_device *spinand) 1072 { 1073 struct nand_device *nand = spinand_to_nand(spinand); 1074 int ret; 1075 1076 #if !CONFIG_IS_ENABLED(SUPPORT_USBPLUG) 1077 ret = spinand_reset_op(spinand); 1078 if (ret) 1079 return ret; 1080 #endif 1081 1082 ret = spinand_id_detect(spinand); 1083 if (ret) { 1084 dev_err(dev, "unknown raw ID %x %x %x\n", 1085 spinand->id.data[0], spinand->id.data[1], spinand->id.data[2]); 1086 return ret; 1087 } 1088 dev_err(dev, "SPI Nand ID %x %x %x\n", 1089 spinand->id.data[0], spinand->id.data[1], spinand->id.data[2]); 1090 1091 if (nand->memorg.ntargets > 1 && !spinand->select_target) { 1092 dev_err(dev, 1093 "SPI NANDs with more than one die must implement ->select_target()\n"); 1094 return -EINVAL; 1095 } 1096 1097 dev_info(spinand->slave->dev, 1098 "%s SPI NAND was found.\n", spinand->manufacturer->name); 1099 dev_info(spinand->slave->dev, 1100 "%llu MiB, block size: %zu KiB, page size: %zu, OOB size: %u\n", 1101 nanddev_size(nand) >> 20, nanddev_eraseblock_size(nand) >> 10, 1102 nanddev_page_size(nand), nanddev_per_page_oobsize(nand)); 1103 1104 return 0; 1105 } 1106 1107 static int spinand_noecc_ooblayout_ecc(struct mtd_info *mtd, int section, 1108 struct mtd_oob_region *region) 1109 { 1110 return -ERANGE; 1111 } 1112 1113 static int spinand_noecc_ooblayout_free(struct mtd_info *mtd, int section, 1114 struct mtd_oob_region *region) 1115 { 1116 if (section) 1117 return -ERANGE; 1118 1119 /* Reserve 2 bytes for the BBM. */ 1120 region->offset = 2; 1121 region->length = 62; 1122 1123 return 0; 1124 } 1125 1126 static const struct mtd_ooblayout_ops spinand_noecc_ooblayout = { 1127 .ecc = spinand_noecc_ooblayout_ecc, 1128 .rfree = spinand_noecc_ooblayout_free, 1129 }; 1130 1131 static int spinand_init(struct spinand_device *spinand) 1132 { 1133 struct mtd_info *mtd = spinand_to_mtd(spinand); 1134 struct nand_device *nand = mtd_to_nanddev(mtd); 1135 int ret, i; 1136 1137 /* 1138 * We need a scratch buffer because the spi_mem interface requires that 1139 * buf passed in spi_mem_op->data.buf be DMA-able. 1140 */ 1141 spinand->scratchbuf = kzalloc(SPINAND_MAX_ID_LEN, GFP_KERNEL); 1142 if (!spinand->scratchbuf) 1143 return -ENOMEM; 1144 1145 ret = spinand_detect(spinand); 1146 if (ret) 1147 goto err_free_bufs; 1148 1149 /* 1150 * Use kzalloc() instead of devm_kzalloc() here, because some drivers 1151 * may use this buffer for DMA access. 1152 * Memory allocated by devm_ does not guarantee DMA-safe alignment. 1153 */ 1154 spinand->databuf = kzalloc(nanddev_page_size(nand) + 1155 nanddev_per_page_oobsize(nand), 1156 GFP_KERNEL); 1157 if (!spinand->databuf) { 1158 ret = -ENOMEM; 1159 goto err_free_bufs; 1160 } 1161 1162 spinand->oobbuf = spinand->databuf + nanddev_page_size(nand); 1163 1164 ret = spinand_init_cfg_cache(spinand); 1165 if (ret) 1166 goto err_free_bufs; 1167 1168 ret = spinand_init_quad_enable(spinand); 1169 if (ret) 1170 goto err_free_bufs; 1171 1172 ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0); 1173 if (ret) 1174 goto err_free_bufs; 1175 1176 ret = spinand_manufacturer_init(spinand); 1177 if (ret) { 1178 dev_err(dev, 1179 "Failed to initialize the SPI NAND chip (err = %d)\n", 1180 ret); 1181 goto err_free_bufs; 1182 } 1183 1184 /* After power up, all blocks are locked, so unlock them here. */ 1185 for (i = 0; i < nand->memorg.ntargets; i++) { 1186 ret = spinand_select_target(spinand, i); 1187 if (ret) 1188 goto err_free_bufs; 1189 1190 /* HWP_EN must be enabled first before block unlock region is set */ 1191 if (spinand->id.data[0] == 0x01) { 1192 ret = spinand_lock_block(spinand, HWP_EN); 1193 if (ret) 1194 goto err_free_bufs; 1195 } 1196 1197 ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED); 1198 if (ret) 1199 goto err_free_bufs; 1200 } 1201 1202 nand->bbt.option = NANDDEV_BBT_USE_FLASH; 1203 ret = nanddev_init(nand, &spinand_ops, THIS_MODULE); 1204 if (ret) 1205 goto err_manuf_cleanup; 1206 1207 /* 1208 * Right now, we don't support ECC, so let the whole oob 1209 * area is available for user. 1210 */ 1211 mtd->_read_oob = spinand_mtd_read; 1212 mtd->_write_oob = spinand_mtd_write; 1213 mtd->_block_isbad = spinand_mtd_block_isbad; 1214 mtd->_block_markbad = spinand_mtd_block_markbad; 1215 mtd->_block_isreserved = spinand_mtd_block_isreserved; 1216 mtd->_erase = spinand_mtd_erase; 1217 1218 if (spinand->eccinfo.ooblayout) 1219 mtd_set_ooblayout(mtd, spinand->eccinfo.ooblayout); 1220 else 1221 mtd_set_ooblayout(mtd, &spinand_noecc_ooblayout); 1222 1223 ret = mtd_ooblayout_count_freebytes(mtd); 1224 if (ret < 0) 1225 goto err_cleanup_nanddev; 1226 1227 mtd->oobavail = ret; 1228 1229 /* Propagate ECC information to mtd_info */ 1230 mtd->ecc_strength = nand->eccreq.strength; 1231 mtd->ecc_step_size = nand->eccreq.step_size; 1232 1233 return 0; 1234 1235 err_cleanup_nanddev: 1236 nanddev_cleanup(nand); 1237 1238 err_manuf_cleanup: 1239 spinand_manufacturer_cleanup(spinand); 1240 1241 err_free_bufs: 1242 kfree(spinand->databuf); 1243 kfree(spinand->scratchbuf); 1244 return ret; 1245 } 1246 1247 static void spinand_cleanup(struct spinand_device *spinand) 1248 { 1249 struct nand_device *nand = spinand_to_nand(spinand); 1250 1251 nanddev_cleanup(nand); 1252 spinand_manufacturer_cleanup(spinand); 1253 kfree(spinand->databuf); 1254 kfree(spinand->scratchbuf); 1255 } 1256 1257 static int spinand_bind(struct udevice *udev) 1258 { 1259 int ret = 0; 1260 1261 #ifdef CONFIG_MTD_BLK 1262 struct udevice *bdev; 1263 1264 ret = blk_create_devicef(udev, "mtd_blk", "blk", IF_TYPE_MTD, 1265 BLK_MTD_SPI_NAND, 512, 0, &bdev); 1266 if (ret) 1267 printf("Cannot create block device\n"); 1268 #endif 1269 return ret; 1270 } 1271 1272 static int spinand_probe(struct udevice *dev) 1273 { 1274 struct spinand_device *spinand = dev_get_priv(dev); 1275 struct spi_slave *slave = dev_get_parent_priv(dev); 1276 struct mtd_info *mtd = dev_get_uclass_priv(dev); 1277 struct nand_device *nand = spinand_to_nand(spinand); 1278 int ret; 1279 1280 #ifndef __UBOOT__ 1281 spinand = devm_kzalloc(&mem->spi->dev, sizeof(*spinand), 1282 GFP_KERNEL); 1283 if (!spinand) 1284 return -ENOMEM; 1285 1286 spinand->spimem = mem; 1287 spi_mem_set_drvdata(mem, spinand); 1288 spinand_set_of_node(spinand, mem->spi->dev.of_node); 1289 mutex_init(&spinand->lock); 1290 1291 mtd = spinand_to_mtd(spinand); 1292 mtd->dev.parent = &mem->spi->dev; 1293 #else 1294 nand->mtd = mtd; 1295 mtd->priv = nand; 1296 mtd->dev = dev; 1297 mtd->name = malloc(20); 1298 if (!mtd->name) 1299 return -ENOMEM; 1300 sprintf(mtd->name, "spi-nand%d", spi_nand_idx++); 1301 spinand->slave = slave; 1302 spinand_set_of_node(spinand, dev->node.np); 1303 #endif 1304 1305 ret = spinand_init(spinand); 1306 if (ret) 1307 return ret; 1308 1309 #ifndef __UBOOT__ 1310 ret = mtd_device_register(mtd, NULL, 0); 1311 #else 1312 ret = add_mtd_device(mtd); 1313 #endif 1314 if (ret) 1315 goto err_spinand_cleanup; 1316 1317 return 0; 1318 1319 err_spinand_cleanup: 1320 spinand_cleanup(spinand); 1321 1322 return ret; 1323 } 1324 1325 #ifndef __UBOOT__ 1326 static int spinand_remove(struct udevice *slave) 1327 { 1328 struct spinand_device *spinand; 1329 struct mtd_info *mtd; 1330 int ret; 1331 1332 spinand = spi_mem_get_drvdata(slave); 1333 mtd = spinand_to_mtd(spinand); 1334 free(mtd->name); 1335 1336 ret = mtd_device_unregister(mtd); 1337 if (ret) 1338 return ret; 1339 1340 spinand_cleanup(spinand); 1341 1342 return 0; 1343 } 1344 1345 static const struct spi_device_id spinand_ids[] = { 1346 { .name = "spi-nand" }, 1347 { /* sentinel */ }, 1348 }; 1349 1350 #ifdef CONFIG_OF 1351 static const struct of_device_id spinand_of_ids[] = { 1352 { .compatible = "spi-nand" }, 1353 { /* sentinel */ }, 1354 }; 1355 #endif 1356 1357 static struct spi_mem_driver spinand_drv = { 1358 .spidrv = { 1359 .id_table = spinand_ids, 1360 .driver = { 1361 .name = "spi-nand", 1362 .of_match_table = of_match_ptr(spinand_of_ids), 1363 }, 1364 }, 1365 .probe = spinand_probe, 1366 .remove = spinand_remove, 1367 }; 1368 module_spi_mem_driver(spinand_drv); 1369 1370 MODULE_DESCRIPTION("SPI NAND framework"); 1371 MODULE_AUTHOR("Peter Pan<peterpandong@micron.com>"); 1372 MODULE_LICENSE("GPL v2"); 1373 #endif /* __UBOOT__ */ 1374 1375 static const struct udevice_id spinand_ids[] = { 1376 { .compatible = "spi-nand" }, 1377 { /* sentinel */ }, 1378 }; 1379 1380 U_BOOT_DRIVER(spinand) = { 1381 .name = "spi_nand", 1382 .id = UCLASS_MTD, 1383 .of_match = spinand_ids, 1384 .bind = spinand_bind, 1385 .priv_auto_alloc_size = sizeof(struct spinand_device), 1386 .probe = spinand_probe, 1387 }; 1388