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 }; 932 933 static int spinand_manufacturer_match(struct spinand_device *spinand, 934 enum spinand_readid_method rdid_method) 935 { 936 u8 *id = spinand->id.data; 937 unsigned int i; 938 int ret; 939 940 for (i = 0; i < ARRAY_SIZE(spinand_manufacturers); i++) { 941 const struct spinand_manufacturer *manufacturer = 942 spinand_manufacturers[i]; 943 944 if (id[0] != manufacturer->id) 945 continue; 946 947 ret = spinand_match_and_init(spinand, 948 manufacturer->chips, 949 manufacturer->nchips, 950 rdid_method); 951 if (ret < 0) 952 continue; 953 954 spinand->manufacturer = manufacturer; 955 return 0; 956 } 957 return -ENOTSUPP; 958 } 959 960 static int spinand_id_detect(struct spinand_device *spinand) 961 { 962 u8 *id = spinand->id.data; 963 int ret; 964 965 ret = spinand_read_id_op(spinand, 0, 0, id); 966 if (ret) 967 return ret; 968 ret = spinand_manufacturer_match(spinand, SPINAND_READID_METHOD_OPCODE); 969 if (!ret) 970 return 0; 971 972 ret = spinand_read_id_op(spinand, 1, 0, id); 973 if (ret) 974 return ret; 975 ret = spinand_manufacturer_match(spinand, 976 SPINAND_READID_METHOD_OPCODE_ADDR); 977 if (!ret) 978 return 0; 979 980 ret = spinand_read_id_op(spinand, 0, 1, id); 981 if (ret) 982 return ret; 983 ret = spinand_manufacturer_match(spinand, 984 SPINAND_READID_METHOD_OPCODE_DUMMY); 985 986 return ret; 987 } 988 989 static int spinand_manufacturer_init(struct spinand_device *spinand) 990 { 991 if (spinand->manufacturer->ops->init) 992 return spinand->manufacturer->ops->init(spinand); 993 994 return 0; 995 } 996 997 static void spinand_manufacturer_cleanup(struct spinand_device *spinand) 998 { 999 /* Release manufacturer private data */ 1000 if (spinand->manufacturer->ops->cleanup) 1001 return spinand->manufacturer->ops->cleanup(spinand); 1002 } 1003 1004 static const struct spi_mem_op * 1005 spinand_select_op_variant(struct spinand_device *spinand, 1006 const struct spinand_op_variants *variants) 1007 { 1008 struct nand_device *nand = spinand_to_nand(spinand); 1009 unsigned int i; 1010 1011 for (i = 0; i < variants->nops; i++) { 1012 struct spi_mem_op op = variants->ops[i]; 1013 unsigned int nbytes; 1014 int ret; 1015 1016 nbytes = nanddev_per_page_oobsize(nand) + 1017 nanddev_page_size(nand); 1018 1019 while (nbytes) { 1020 op.data.nbytes = nbytes; 1021 ret = spi_mem_adjust_op_size(spinand->slave, &op); 1022 if (ret) 1023 break; 1024 1025 if (!spi_mem_supports_op(spinand->slave, &op)) 1026 break; 1027 1028 nbytes -= op.data.nbytes; 1029 } 1030 1031 if (!nbytes) 1032 return &variants->ops[i]; 1033 } 1034 1035 return NULL; 1036 } 1037 1038 /** 1039 * spinand_match_and_init() - Try to find a match between a device ID and an 1040 * entry in a spinand_info table 1041 * @spinand: SPI NAND object 1042 * @table: SPI NAND device description table 1043 * @table_size: size of the device description table 1044 * @rdid_method: read id method to match 1045 * 1046 * Match between a device ID retrieved through the READ_ID command and an 1047 * entry in the SPI NAND description table. If a match is found, the spinand 1048 * object will be initialized with information provided by the matching 1049 * spinand_info entry. 1050 * 1051 * Return: 0 on success, a negative error code otherwise. 1052 */ 1053 int spinand_match_and_init(struct spinand_device *spinand, 1054 const struct spinand_info *table, 1055 unsigned int table_size, 1056 enum spinand_readid_method rdid_method) 1057 { 1058 u8 *id = spinand->id.data; 1059 struct nand_device *nand = spinand_to_nand(spinand); 1060 unsigned int i; 1061 1062 for (i = 0; i < table_size; i++) { 1063 const struct spinand_info *info = &table[i]; 1064 const struct spi_mem_op *op; 1065 1066 if (rdid_method != info->devid.method) 1067 continue; 1068 1069 if (memcmp(id + 1, info->devid.id, info->devid.len)) 1070 continue; 1071 1072 nand->memorg = table[i].memorg; 1073 nand->eccreq = table[i].eccreq; 1074 spinand->eccinfo = table[i].eccinfo; 1075 spinand->flags = table[i].flags; 1076 spinand->id.len = 1 + table[i].devid.len; 1077 spinand->select_target = table[i].select_target; 1078 1079 op = spinand_select_op_variant(spinand, 1080 info->op_variants.read_cache); 1081 if (!op) 1082 return -ENOTSUPP; 1083 1084 spinand->op_templates.read_cache = op; 1085 1086 op = spinand_select_op_variant(spinand, 1087 info->op_variants.write_cache); 1088 if (!op) 1089 return -ENOTSUPP; 1090 1091 spinand->op_templates.write_cache = op; 1092 1093 op = spinand_select_op_variant(spinand, 1094 info->op_variants.update_cache); 1095 spinand->op_templates.update_cache = op; 1096 1097 return 0; 1098 } 1099 1100 return -ENOTSUPP; 1101 } 1102 1103 static int spinand_detect(struct spinand_device *spinand) 1104 { 1105 struct nand_device *nand = spinand_to_nand(spinand); 1106 int ret; 1107 1108 #if !CONFIG_IS_ENABLED(SUPPORT_USBPLUG) 1109 ret = spinand_reset_op(spinand); 1110 if (ret) 1111 return ret; 1112 #endif 1113 1114 ret = spinand_id_detect(spinand); 1115 if (ret) { 1116 dev_err(dev, "unknown raw ID %x %x %x\n", 1117 spinand->id.data[0], spinand->id.data[1], spinand->id.data[2]); 1118 return ret; 1119 } 1120 dev_err(dev, "SPI Nand ID %x %x %x\n", 1121 spinand->id.data[0], spinand->id.data[1], spinand->id.data[2]); 1122 1123 if (nand->memorg.ntargets > 1 && !spinand->select_target) { 1124 dev_err(dev, 1125 "SPI NANDs with more than one die must implement ->select_target()\n"); 1126 return -EINVAL; 1127 } 1128 1129 dev_info(spinand->slave->dev, 1130 "%s SPI NAND was found.\n", spinand->manufacturer->name); 1131 dev_info(spinand->slave->dev, 1132 "%llu MiB, block size: %zu KiB, page size: %zu, OOB size: %u\n", 1133 nanddev_size(nand) >> 20, nanddev_eraseblock_size(nand) >> 10, 1134 nanddev_page_size(nand), nanddev_per_page_oobsize(nand)); 1135 1136 return 0; 1137 } 1138 1139 static int spinand_noecc_ooblayout_ecc(struct mtd_info *mtd, int section, 1140 struct mtd_oob_region *region) 1141 { 1142 return -ERANGE; 1143 } 1144 1145 static int spinand_noecc_ooblayout_free(struct mtd_info *mtd, int section, 1146 struct mtd_oob_region *region) 1147 { 1148 if (section) 1149 return -ERANGE; 1150 1151 /* Reserve 2 bytes for the BBM. */ 1152 region->offset = 2; 1153 region->length = 62; 1154 1155 return 0; 1156 } 1157 1158 static const struct mtd_ooblayout_ops spinand_noecc_ooblayout = { 1159 .ecc = spinand_noecc_ooblayout_ecc, 1160 .rfree = spinand_noecc_ooblayout_free, 1161 }; 1162 1163 static int spinand_init(struct spinand_device *spinand) 1164 { 1165 struct mtd_info *mtd = spinand_to_mtd(spinand); 1166 struct nand_device *nand = mtd_to_nanddev(mtd); 1167 int ret, i; 1168 1169 /* 1170 * We need a scratch buffer because the spi_mem interface requires that 1171 * buf passed in spi_mem_op->data.buf be DMA-able. 1172 */ 1173 spinand->scratchbuf = kzalloc(SPINAND_MAX_ID_LEN, GFP_KERNEL); 1174 if (!spinand->scratchbuf) 1175 return -ENOMEM; 1176 1177 ret = spinand_detect(spinand); 1178 if (ret) 1179 goto err_free_bufs; 1180 1181 /* 1182 * Use kzalloc() instead of devm_kzalloc() here, because some drivers 1183 * may use this buffer for DMA access. 1184 * Memory allocated by devm_ does not guarantee DMA-safe alignment. 1185 */ 1186 spinand->databuf = kzalloc(nanddev_page_size(nand) + 1187 nanddev_per_page_oobsize(nand), 1188 GFP_KERNEL); 1189 if (!spinand->databuf) { 1190 ret = -ENOMEM; 1191 goto err_free_bufs; 1192 } 1193 1194 spinand->oobbuf = spinand->databuf + nanddev_page_size(nand); 1195 1196 ret = spinand_init_cfg_cache(spinand); 1197 if (ret) 1198 goto err_free_bufs; 1199 1200 ret = spinand_init_quad_enable(spinand); 1201 if (ret) 1202 goto err_free_bufs; 1203 1204 ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0); 1205 if (ret) 1206 goto err_free_bufs; 1207 1208 ret = spinand_manufacturer_init(spinand); 1209 if (ret) { 1210 dev_err(dev, 1211 "Failed to initialize the SPI NAND chip (err = %d)\n", 1212 ret); 1213 goto err_free_bufs; 1214 } 1215 1216 /* After power up, all blocks are locked, so unlock them here. */ 1217 for (i = 0; i < nand->memorg.ntargets; i++) { 1218 ret = spinand_select_target(spinand, i); 1219 if (ret) 1220 goto err_free_bufs; 1221 1222 /* HWP_EN must be enabled first before block unlock region is set */ 1223 if (spinand->id.data[0] == 0x01) { 1224 ret = spinand_lock_block(spinand, HWP_EN); 1225 if (ret) 1226 goto err_free_bufs; 1227 } 1228 1229 ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED); 1230 if (ret) 1231 goto err_free_bufs; 1232 } 1233 1234 nand->bbt.option = NANDDEV_BBT_USE_FLASH; 1235 ret = nanddev_init(nand, &spinand_ops, THIS_MODULE); 1236 if (ret) 1237 goto err_manuf_cleanup; 1238 1239 /* 1240 * Right now, we don't support ECC, so let the whole oob 1241 * area is available for user. 1242 */ 1243 mtd->_read_oob = spinand_mtd_read; 1244 mtd->_write_oob = spinand_mtd_write; 1245 mtd->_block_isbad = spinand_mtd_block_isbad; 1246 mtd->_block_markbad = spinand_mtd_block_markbad; 1247 mtd->_block_isreserved = spinand_mtd_block_isreserved; 1248 mtd->_erase = spinand_mtd_erase; 1249 1250 if (spinand->eccinfo.ooblayout) 1251 mtd_set_ooblayout(mtd, spinand->eccinfo.ooblayout); 1252 else 1253 mtd_set_ooblayout(mtd, &spinand_noecc_ooblayout); 1254 1255 ret = mtd_ooblayout_count_freebytes(mtd); 1256 if (ret < 0) 1257 goto err_cleanup_nanddev; 1258 1259 mtd->oobavail = ret; 1260 1261 /* Propagate ECC information to mtd_info */ 1262 mtd->ecc_strength = nand->eccreq.strength; 1263 mtd->ecc_step_size = nand->eccreq.step_size; 1264 1265 return 0; 1266 1267 err_cleanup_nanddev: 1268 nanddev_cleanup(nand); 1269 1270 err_manuf_cleanup: 1271 spinand_manufacturer_cleanup(spinand); 1272 1273 err_free_bufs: 1274 kfree(spinand->databuf); 1275 kfree(spinand->scratchbuf); 1276 return ret; 1277 } 1278 1279 static void spinand_cleanup(struct spinand_device *spinand) 1280 { 1281 struct nand_device *nand = spinand_to_nand(spinand); 1282 1283 nanddev_cleanup(nand); 1284 spinand_manufacturer_cleanup(spinand); 1285 kfree(spinand->databuf); 1286 kfree(spinand->scratchbuf); 1287 } 1288 1289 static int spinand_bind(struct udevice *udev) 1290 { 1291 int ret = 0; 1292 1293 #ifdef CONFIG_MTD_BLK 1294 struct udevice *bdev; 1295 1296 ret = blk_create_devicef(udev, "mtd_blk", "blk", IF_TYPE_MTD, 1297 BLK_MTD_SPI_NAND, 512, 0, &bdev); 1298 if (ret) 1299 printf("Cannot create block device\n"); 1300 #endif 1301 return ret; 1302 } 1303 1304 static int spinand_probe(struct udevice *dev) 1305 { 1306 struct spinand_device *spinand = dev_get_priv(dev); 1307 struct spi_slave *slave = dev_get_parent_priv(dev); 1308 struct mtd_info *mtd = dev_get_uclass_priv(dev); 1309 struct nand_device *nand = spinand_to_nand(spinand); 1310 int ret; 1311 1312 #ifndef __UBOOT__ 1313 spinand = devm_kzalloc(&mem->spi->dev, sizeof(*spinand), 1314 GFP_KERNEL); 1315 if (!spinand) 1316 return -ENOMEM; 1317 1318 spinand->spimem = mem; 1319 spi_mem_set_drvdata(mem, spinand); 1320 spinand_set_of_node(spinand, mem->spi->dev.of_node); 1321 mutex_init(&spinand->lock); 1322 1323 mtd = spinand_to_mtd(spinand); 1324 mtd->dev.parent = &mem->spi->dev; 1325 #else 1326 nand->mtd = mtd; 1327 mtd->priv = nand; 1328 mtd->dev = dev; 1329 mtd->name = malloc(20); 1330 if (!mtd->name) 1331 return -ENOMEM; 1332 sprintf(mtd->name, "spi-nand%d", spi_nand_idx++); 1333 spinand->slave = slave; 1334 spinand_set_of_node(spinand, dev->node.np); 1335 #endif 1336 1337 ret = spinand_init(spinand); 1338 if (ret) 1339 return ret; 1340 1341 #ifndef __UBOOT__ 1342 ret = mtd_device_register(mtd, NULL, 0); 1343 #else 1344 ret = add_mtd_device(mtd); 1345 #endif 1346 if (ret) 1347 goto err_spinand_cleanup; 1348 1349 return 0; 1350 1351 err_spinand_cleanup: 1352 spinand_cleanup(spinand); 1353 1354 return ret; 1355 } 1356 1357 #ifndef __UBOOT__ 1358 static int spinand_remove(struct udevice *slave) 1359 { 1360 struct spinand_device *spinand; 1361 struct mtd_info *mtd; 1362 int ret; 1363 1364 spinand = spi_mem_get_drvdata(slave); 1365 mtd = spinand_to_mtd(spinand); 1366 free(mtd->name); 1367 1368 ret = mtd_device_unregister(mtd); 1369 if (ret) 1370 return ret; 1371 1372 spinand_cleanup(spinand); 1373 1374 return 0; 1375 } 1376 1377 static const struct spi_device_id spinand_ids[] = { 1378 { .name = "spi-nand" }, 1379 { /* sentinel */ }, 1380 }; 1381 1382 #ifdef CONFIG_OF 1383 static const struct of_device_id spinand_of_ids[] = { 1384 { .compatible = "spi-nand" }, 1385 { /* sentinel */ }, 1386 }; 1387 #endif 1388 1389 static struct spi_mem_driver spinand_drv = { 1390 .spidrv = { 1391 .id_table = spinand_ids, 1392 .driver = { 1393 .name = "spi-nand", 1394 .of_match_table = of_match_ptr(spinand_of_ids), 1395 }, 1396 }, 1397 .probe = spinand_probe, 1398 .remove = spinand_remove, 1399 }; 1400 module_spi_mem_driver(spinand_drv); 1401 1402 MODULE_DESCRIPTION("SPI NAND framework"); 1403 MODULE_AUTHOR("Peter Pan<peterpandong@micron.com>"); 1404 MODULE_LICENSE("GPL v2"); 1405 #endif /* __UBOOT__ */ 1406 1407 static const struct udevice_id spinand_ids[] = { 1408 { .compatible = "spi-nand" }, 1409 { /* sentinel */ }, 1410 }; 1411 1412 U_BOOT_DRIVER(spinand) = { 1413 .name = "spi_nand", 1414 .id = UCLASS_MTD, 1415 .of_match = spinand_ids, 1416 .bind = spinand_bind, 1417 .priv_auto_alloc_size = sizeof(struct spinand_device), 1418 .probe = spinand_probe, 1419 }; 1420