| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * NXP LPC32XX NAND SLC driver |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 7 | 8 | * |
|---|
| 8 | 9 | * Copyright © 2011 NXP Semiconductors |
|---|
| 9 | 10 | * Copyright © 2012 Roland Stigge |
|---|
| 10 | | - * |
|---|
| 11 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 12 | | - * it under the terms of the GNU General Public License as published by |
|---|
| 13 | | - * the Free Software Foundation; either version 2 of the License, or |
|---|
| 14 | | - * (at your option) any later version. |
|---|
| 15 | | - * |
|---|
| 16 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 17 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 18 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 19 | | - * GNU General Public License for more details. |
|---|
| 20 | 11 | */ |
|---|
| 21 | 12 | |
|---|
| 22 | 13 | #include <linux/slab.h> |
|---|
| .. | .. |
|---|
| 278 | 269 | /* |
|---|
| 279 | 270 | * Hardware specific access to control lines |
|---|
| 280 | 271 | */ |
|---|
| 281 | | -static void lpc32xx_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, |
|---|
| 282 | | - unsigned int ctrl) |
|---|
| 272 | +static void lpc32xx_nand_cmd_ctrl(struct nand_chip *chip, int cmd, |
|---|
| 273 | + unsigned int ctrl) |
|---|
| 283 | 274 | { |
|---|
| 284 | 275 | uint32_t tmp; |
|---|
| 285 | | - struct nand_chip *chip = mtd_to_nand(mtd); |
|---|
| 286 | 276 | struct lpc32xx_nand_host *host = nand_get_controller_data(chip); |
|---|
| 287 | 277 | |
|---|
| 288 | 278 | /* Does CE state need to be changed? */ |
|---|
| .. | .. |
|---|
| 304 | 294 | /* |
|---|
| 305 | 295 | * Read the Device Ready pin |
|---|
| 306 | 296 | */ |
|---|
| 307 | | -static int lpc32xx_nand_device_ready(struct mtd_info *mtd) |
|---|
| 297 | +static int lpc32xx_nand_device_ready(struct nand_chip *chip) |
|---|
| 308 | 298 | { |
|---|
| 309 | | - struct nand_chip *chip = mtd_to_nand(mtd); |
|---|
| 310 | 299 | struct lpc32xx_nand_host *host = nand_get_controller_data(chip); |
|---|
| 311 | 300 | int rdy = 0; |
|---|
| 312 | 301 | |
|---|
| .. | .. |
|---|
| 337 | 326 | /* |
|---|
| 338 | 327 | * Prepares SLC for transfers with H/W ECC enabled |
|---|
| 339 | 328 | */ |
|---|
| 340 | | -static void lpc32xx_nand_ecc_enable(struct mtd_info *mtd, int mode) |
|---|
| 329 | +static void lpc32xx_nand_ecc_enable(struct nand_chip *chip, int mode) |
|---|
| 341 | 330 | { |
|---|
| 342 | 331 | /* Hardware ECC is enabled automatically in hardware as needed */ |
|---|
| 343 | 332 | } |
|---|
| .. | .. |
|---|
| 345 | 334 | /* |
|---|
| 346 | 335 | * Calculates the ECC for the data |
|---|
| 347 | 336 | */ |
|---|
| 348 | | -static int lpc32xx_nand_ecc_calculate(struct mtd_info *mtd, |
|---|
| 337 | +static int lpc32xx_nand_ecc_calculate(struct nand_chip *chip, |
|---|
| 349 | 338 | const unsigned char *buf, |
|---|
| 350 | 339 | unsigned char *code) |
|---|
| 351 | 340 | { |
|---|
| .. | .. |
|---|
| 359 | 348 | /* |
|---|
| 360 | 349 | * Read a single byte from NAND device |
|---|
| 361 | 350 | */ |
|---|
| 362 | | -static uint8_t lpc32xx_nand_read_byte(struct mtd_info *mtd) |
|---|
| 351 | +static uint8_t lpc32xx_nand_read_byte(struct nand_chip *chip) |
|---|
| 363 | 352 | { |
|---|
| 364 | | - struct nand_chip *chip = mtd_to_nand(mtd); |
|---|
| 365 | 353 | struct lpc32xx_nand_host *host = nand_get_controller_data(chip); |
|---|
| 366 | 354 | |
|---|
| 367 | 355 | return (uint8_t)readl(SLC_DATA(host->io_base)); |
|---|
| .. | .. |
|---|
| 370 | 358 | /* |
|---|
| 371 | 359 | * Simple device read without ECC |
|---|
| 372 | 360 | */ |
|---|
| 373 | | -static void lpc32xx_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
|---|
| 361 | +static void lpc32xx_nand_read_buf(struct nand_chip *chip, u_char *buf, int len) |
|---|
| 374 | 362 | { |
|---|
| 375 | | - struct nand_chip *chip = mtd_to_nand(mtd); |
|---|
| 376 | 363 | struct lpc32xx_nand_host *host = nand_get_controller_data(chip); |
|---|
| 377 | 364 | |
|---|
| 378 | 365 | /* Direct device read with no ECC */ |
|---|
| .. | .. |
|---|
| 383 | 370 | /* |
|---|
| 384 | 371 | * Simple device write without ECC |
|---|
| 385 | 372 | */ |
|---|
| 386 | | -static void lpc32xx_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
|---|
| 373 | +static void lpc32xx_nand_write_buf(struct nand_chip *chip, const uint8_t *buf, |
|---|
| 374 | + int len) |
|---|
| 387 | 375 | { |
|---|
| 388 | | - struct nand_chip *chip = mtd_to_nand(mtd); |
|---|
| 389 | 376 | struct lpc32xx_nand_host *host = nand_get_controller_data(chip); |
|---|
| 390 | 377 | |
|---|
| 391 | 378 | /* Direct device write with no ECC */ |
|---|
| .. | .. |
|---|
| 396 | 383 | /* |
|---|
| 397 | 384 | * Read the OOB data from the device without ECC using FIFO method |
|---|
| 398 | 385 | */ |
|---|
| 399 | | -static int lpc32xx_nand_read_oob_syndrome(struct mtd_info *mtd, |
|---|
| 400 | | - struct nand_chip *chip, int page) |
|---|
| 386 | +static int lpc32xx_nand_read_oob_syndrome(struct nand_chip *chip, int page) |
|---|
| 401 | 387 | { |
|---|
| 388 | + struct mtd_info *mtd = nand_to_mtd(chip); |
|---|
| 389 | + |
|---|
| 402 | 390 | return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
|---|
| 403 | 391 | } |
|---|
| 404 | 392 | |
|---|
| 405 | 393 | /* |
|---|
| 406 | 394 | * Write the OOB data to the device without ECC using FIFO method |
|---|
| 407 | 395 | */ |
|---|
| 408 | | -static int lpc32xx_nand_write_oob_syndrome(struct mtd_info *mtd, |
|---|
| 409 | | - struct nand_chip *chip, int page) |
|---|
| 396 | +static int lpc32xx_nand_write_oob_syndrome(struct nand_chip *chip, int page) |
|---|
| 410 | 397 | { |
|---|
| 398 | + struct mtd_info *mtd = nand_to_mtd(chip); |
|---|
| 399 | + |
|---|
| 411 | 400 | return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi, |
|---|
| 412 | 401 | mtd->oobsize); |
|---|
| 413 | 402 | } |
|---|
| .. | .. |
|---|
| 610 | 599 | * Read the data and OOB data from the device, use ECC correction with the |
|---|
| 611 | 600 | * data, disable ECC for the OOB data |
|---|
| 612 | 601 | */ |
|---|
| 613 | | -static int lpc32xx_nand_read_page_syndrome(struct mtd_info *mtd, |
|---|
| 614 | | - struct nand_chip *chip, uint8_t *buf, |
|---|
| 602 | +static int lpc32xx_nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf, |
|---|
| 615 | 603 | int oob_required, int page) |
|---|
| 616 | 604 | { |
|---|
| 605 | + struct mtd_info *mtd = nand_to_mtd(chip); |
|---|
| 617 | 606 | struct lpc32xx_nand_host *host = nand_get_controller_data(chip); |
|---|
| 618 | 607 | struct mtd_oob_region oobregion = { }; |
|---|
| 619 | 608 | int stat, i, status, error; |
|---|
| .. | .. |
|---|
| 626 | 615 | status = lpc32xx_xfer(mtd, buf, chip->ecc.steps, 1); |
|---|
| 627 | 616 | |
|---|
| 628 | 617 | /* Get OOB data */ |
|---|
| 629 | | - chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
|---|
| 618 | + chip->legacy.read_buf(chip, chip->oob_poi, mtd->oobsize); |
|---|
| 630 | 619 | |
|---|
| 631 | 620 | /* Convert to stored ECC format */ |
|---|
| 632 | 621 | lpc32xx_slc_ecc_copy(tmpecc, (uint32_t *) host->ecc_buf, chip->ecc.steps); |
|---|
| .. | .. |
|---|
| 639 | 628 | oobecc = chip->oob_poi + oobregion.offset; |
|---|
| 640 | 629 | |
|---|
| 641 | 630 | for (i = 0; i < chip->ecc.steps; i++) { |
|---|
| 642 | | - stat = chip->ecc.correct(mtd, buf, oobecc, |
|---|
| 631 | + stat = chip->ecc.correct(chip, buf, oobecc, |
|---|
| 643 | 632 | &tmpecc[i * chip->ecc.bytes]); |
|---|
| 644 | 633 | if (stat < 0) |
|---|
| 645 | 634 | mtd->ecc_stats.failed++; |
|---|
| .. | .. |
|---|
| 657 | 646 | * Read the data and OOB data from the device, no ECC correction with the |
|---|
| 658 | 647 | * data or OOB data |
|---|
| 659 | 648 | */ |
|---|
| 660 | | -static int lpc32xx_nand_read_page_raw_syndrome(struct mtd_info *mtd, |
|---|
| 661 | | - struct nand_chip *chip, |
|---|
| 649 | +static int lpc32xx_nand_read_page_raw_syndrome(struct nand_chip *chip, |
|---|
| 662 | 650 | uint8_t *buf, int oob_required, |
|---|
| 663 | 651 | int page) |
|---|
| 664 | 652 | { |
|---|
| 653 | + struct mtd_info *mtd = nand_to_mtd(chip); |
|---|
| 654 | + |
|---|
| 665 | 655 | /* Issue read command */ |
|---|
| 666 | 656 | nand_read_page_op(chip, page, 0, NULL, 0); |
|---|
| 667 | 657 | |
|---|
| 668 | 658 | /* Raw reads can just use the FIFO interface */ |
|---|
| 669 | | - chip->read_buf(mtd, buf, chip->ecc.size * chip->ecc.steps); |
|---|
| 670 | | - chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
|---|
| 659 | + chip->legacy.read_buf(chip, buf, chip->ecc.size * chip->ecc.steps); |
|---|
| 660 | + chip->legacy.read_buf(chip, chip->oob_poi, mtd->oobsize); |
|---|
| 671 | 661 | |
|---|
| 672 | 662 | return 0; |
|---|
| 673 | 663 | } |
|---|
| .. | .. |
|---|
| 676 | 666 | * Write the data and OOB data to the device, use ECC with the data, |
|---|
| 677 | 667 | * disable ECC for the OOB data |
|---|
| 678 | 668 | */ |
|---|
| 679 | | -static int lpc32xx_nand_write_page_syndrome(struct mtd_info *mtd, |
|---|
| 680 | | - struct nand_chip *chip, |
|---|
| 669 | +static int lpc32xx_nand_write_page_syndrome(struct nand_chip *chip, |
|---|
| 681 | 670 | const uint8_t *buf, |
|---|
| 682 | 671 | int oob_required, int page) |
|---|
| 683 | 672 | { |
|---|
| 673 | + struct mtd_info *mtd = nand_to_mtd(chip); |
|---|
| 684 | 674 | struct lpc32xx_nand_host *host = nand_get_controller_data(chip); |
|---|
| 685 | 675 | struct mtd_oob_region oobregion = { }; |
|---|
| 686 | 676 | uint8_t *pb; |
|---|
| .. | .. |
|---|
| 705 | 695 | lpc32xx_slc_ecc_copy(pb, (uint32_t *)host->ecc_buf, chip->ecc.steps); |
|---|
| 706 | 696 | |
|---|
| 707 | 697 | /* Write ECC data to device */ |
|---|
| 708 | | - chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
|---|
| 698 | + chip->legacy.write_buf(chip, chip->oob_poi, mtd->oobsize); |
|---|
| 709 | 699 | |
|---|
| 710 | 700 | return nand_prog_page_end_op(chip); |
|---|
| 711 | 701 | } |
|---|
| .. | .. |
|---|
| 714 | 704 | * Write the data and OOB data to the device, no ECC correction with the |
|---|
| 715 | 705 | * data or OOB data |
|---|
| 716 | 706 | */ |
|---|
| 717 | | -static int lpc32xx_nand_write_page_raw_syndrome(struct mtd_info *mtd, |
|---|
| 718 | | - struct nand_chip *chip, |
|---|
| 707 | +static int lpc32xx_nand_write_page_raw_syndrome(struct nand_chip *chip, |
|---|
| 719 | 708 | const uint8_t *buf, |
|---|
| 720 | 709 | int oob_required, int page) |
|---|
| 721 | 710 | { |
|---|
| 711 | + struct mtd_info *mtd = nand_to_mtd(chip); |
|---|
| 712 | + |
|---|
| 722 | 713 | /* Raw writes can just use the FIFO interface */ |
|---|
| 723 | 714 | nand_prog_page_begin_op(chip, page, 0, buf, |
|---|
| 724 | 715 | chip->ecc.size * chip->ecc.steps); |
|---|
| 725 | | - chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
|---|
| 716 | + chip->legacy.write_buf(chip, chip->oob_poi, mtd->oobsize); |
|---|
| 726 | 717 | |
|---|
| 727 | 718 | return nand_prog_page_end_op(chip); |
|---|
| 728 | 719 | } |
|---|
| .. | .. |
|---|
| 784 | 775 | struct mtd_info *mtd = nand_to_mtd(chip); |
|---|
| 785 | 776 | struct lpc32xx_nand_host *host = nand_get_controller_data(chip); |
|---|
| 786 | 777 | |
|---|
| 778 | + if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST) |
|---|
| 779 | + return 0; |
|---|
| 780 | + |
|---|
| 787 | 781 | /* OOB and ECC CPU and DMA work areas */ |
|---|
| 788 | 782 | host->ecc_buf = (uint32_t *)(host->data_buf + LPC32XX_DMA_DATA_SIZE); |
|---|
| 789 | 783 | |
|---|
| .. | .. |
|---|
| 795 | 789 | if (mtd->writesize <= 512) |
|---|
| 796 | 790 | mtd_set_ooblayout(mtd, &lpc32xx_ooblayout_ops); |
|---|
| 797 | 791 | |
|---|
| 792 | + chip->ecc.placement = NAND_ECC_PLACEMENT_INTERLEAVED; |
|---|
| 798 | 793 | /* These sizes remain the same regardless of page size */ |
|---|
| 799 | 794 | chip->ecc.size = 256; |
|---|
| 795 | + chip->ecc.strength = 1; |
|---|
| 800 | 796 | chip->ecc.bytes = LPC32XX_SLC_DEV_ECC_BYTES; |
|---|
| 801 | 797 | chip->ecc.prepad = 0; |
|---|
| 802 | 798 | chip->ecc.postpad = 0; |
|---|
| 799 | + chip->ecc.read_page_raw = lpc32xx_nand_read_page_raw_syndrome; |
|---|
| 800 | + chip->ecc.read_page = lpc32xx_nand_read_page_syndrome; |
|---|
| 801 | + chip->ecc.write_page_raw = lpc32xx_nand_write_page_raw_syndrome; |
|---|
| 802 | + chip->ecc.write_page = lpc32xx_nand_write_page_syndrome; |
|---|
| 803 | + chip->ecc.write_oob = lpc32xx_nand_write_oob_syndrome; |
|---|
| 804 | + chip->ecc.read_oob = lpc32xx_nand_read_oob_syndrome; |
|---|
| 805 | + chip->ecc.calculate = lpc32xx_nand_ecc_calculate; |
|---|
| 806 | + chip->ecc.correct = nand_correct_data; |
|---|
| 807 | + chip->ecc.hwctl = lpc32xx_nand_ecc_enable; |
|---|
| 803 | 808 | |
|---|
| 804 | 809 | /* |
|---|
| 805 | 810 | * Use a custom BBT marker setup for small page FLASH that |
|---|
| .. | .. |
|---|
| 878 | 883 | goto enable_wp; |
|---|
| 879 | 884 | |
|---|
| 880 | 885 | /* Set NAND IO addresses and command/ready functions */ |
|---|
| 881 | | - chip->IO_ADDR_R = SLC_DATA(host->io_base); |
|---|
| 882 | | - chip->IO_ADDR_W = SLC_DATA(host->io_base); |
|---|
| 883 | | - chip->cmd_ctrl = lpc32xx_nand_cmd_ctrl; |
|---|
| 884 | | - chip->dev_ready = lpc32xx_nand_device_ready; |
|---|
| 885 | | - chip->chip_delay = 20; /* 20us command delay time */ |
|---|
| 886 | + chip->legacy.IO_ADDR_R = SLC_DATA(host->io_base); |
|---|
| 887 | + chip->legacy.IO_ADDR_W = SLC_DATA(host->io_base); |
|---|
| 888 | + chip->legacy.cmd_ctrl = lpc32xx_nand_cmd_ctrl; |
|---|
| 889 | + chip->legacy.dev_ready = lpc32xx_nand_device_ready; |
|---|
| 890 | + chip->legacy.chip_delay = 20; /* 20us command delay time */ |
|---|
| 886 | 891 | |
|---|
| 887 | 892 | /* Init NAND controller */ |
|---|
| 888 | 893 | lpc32xx_nand_setup(host); |
|---|
| .. | .. |
|---|
| 890 | 895 | platform_set_drvdata(pdev, host); |
|---|
| 891 | 896 | |
|---|
| 892 | 897 | /* NAND callbacks for LPC32xx SLC hardware */ |
|---|
| 893 | | - chip->ecc.mode = NAND_ECC_HW_SYNDROME; |
|---|
| 894 | | - chip->read_byte = lpc32xx_nand_read_byte; |
|---|
| 895 | | - chip->read_buf = lpc32xx_nand_read_buf; |
|---|
| 896 | | - chip->write_buf = lpc32xx_nand_write_buf; |
|---|
| 897 | | - chip->ecc.read_page_raw = lpc32xx_nand_read_page_raw_syndrome; |
|---|
| 898 | | - chip->ecc.read_page = lpc32xx_nand_read_page_syndrome; |
|---|
| 899 | | - chip->ecc.write_page_raw = lpc32xx_nand_write_page_raw_syndrome; |
|---|
| 900 | | - chip->ecc.write_page = lpc32xx_nand_write_page_syndrome; |
|---|
| 901 | | - chip->ecc.write_oob = lpc32xx_nand_write_oob_syndrome; |
|---|
| 902 | | - chip->ecc.read_oob = lpc32xx_nand_read_oob_syndrome; |
|---|
| 903 | | - chip->ecc.calculate = lpc32xx_nand_ecc_calculate; |
|---|
| 904 | | - chip->ecc.correct = nand_correct_data; |
|---|
| 905 | | - chip->ecc.strength = 1; |
|---|
| 906 | | - chip->ecc.hwctl = lpc32xx_nand_ecc_enable; |
|---|
| 898 | + chip->legacy.read_byte = lpc32xx_nand_read_byte; |
|---|
| 899 | + chip->legacy.read_buf = lpc32xx_nand_read_buf; |
|---|
| 900 | + chip->legacy.write_buf = lpc32xx_nand_write_buf; |
|---|
| 907 | 901 | |
|---|
| 908 | 902 | /* |
|---|
| 909 | 903 | * Allocate a large enough buffer for a single huge page plus |
|---|
| .. | .. |
|---|
| 924 | 918 | } |
|---|
| 925 | 919 | |
|---|
| 926 | 920 | /* Find NAND device */ |
|---|
| 927 | | - chip->dummy_controller.ops = &lpc32xx_nand_controller_ops; |
|---|
| 921 | + chip->legacy.dummy_controller.ops = &lpc32xx_nand_controller_ops; |
|---|
| 928 | 922 | res = nand_scan(chip, 1); |
|---|
| 929 | 923 | if (res) |
|---|
| 930 | 924 | goto release_dma; |
|---|
| .. | .. |
|---|
| 956 | 950 | { |
|---|
| 957 | 951 | uint32_t tmp; |
|---|
| 958 | 952 | struct lpc32xx_nand_host *host = platform_get_drvdata(pdev); |
|---|
| 953 | + struct nand_chip *chip = &host->nand_chip; |
|---|
| 954 | + int ret; |
|---|
| 959 | 955 | |
|---|
| 960 | | - nand_release(&host->nand_chip); |
|---|
| 956 | + ret = mtd_device_unregister(nand_to_mtd(chip)); |
|---|
| 957 | + WARN_ON(ret); |
|---|
| 958 | + nand_cleanup(chip); |
|---|
| 961 | 959 | dma_release_channel(host->dma_chan); |
|---|
| 962 | 960 | |
|---|
| 963 | 961 | /* Force CE high */ |
|---|