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