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