| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * davinci_nand.c - NAND Flash Driver for DaVinci family chips |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 7 | 8 | * Sander Huijsen <Shuijsen@optelecom-nkf.com> |
|---|
| 8 | 9 | * Troy Kisky <troy.kisky@boundarydevices.com> |
|---|
| 9 | 10 | * Dirk Behme <Dirk.Behme@gmail.com> |
|---|
| 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 | | - * |
|---|
| 21 | | - * You should have received a copy of the GNU General Public License |
|---|
| 22 | | - * along with this program; if not, write to the Free Software |
|---|
| 23 | | - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 24 | 11 | */ |
|---|
| 25 | 12 | |
|---|
| 26 | 13 | #include <linux/kernel.h> |
|---|
| 27 | 14 | #include <linux/module.h> |
|---|
| 28 | 15 | #include <linux/platform_device.h> |
|---|
| 29 | 16 | #include <linux/err.h> |
|---|
| 30 | | -#include <linux/io.h> |
|---|
| 17 | +#include <linux/iopoll.h> |
|---|
| 31 | 18 | #include <linux/mtd/rawnand.h> |
|---|
| 32 | 19 | #include <linux/mtd/partitions.h> |
|---|
| 33 | 20 | #include <linux/slab.h> |
|---|
| .. | .. |
|---|
| 51 | 38 | * outputs in a "wire-AND" configuration, with no per-chip signals. |
|---|
| 52 | 39 | */ |
|---|
| 53 | 40 | struct davinci_nand_info { |
|---|
| 41 | + struct nand_controller controller; |
|---|
| 54 | 42 | struct nand_chip chip; |
|---|
| 55 | 43 | |
|---|
| 56 | 44 | struct platform_device *pdev; |
|---|
| .. | .. |
|---|
| 94 | 82 | /*----------------------------------------------------------------------*/ |
|---|
| 95 | 83 | |
|---|
| 96 | 84 | /* |
|---|
| 97 | | - * Access to hardware control lines: ALE, CLE, secondary chipselect. |
|---|
| 98 | | - */ |
|---|
| 99 | | - |
|---|
| 100 | | -static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd, |
|---|
| 101 | | - unsigned int ctrl) |
|---|
| 102 | | -{ |
|---|
| 103 | | - struct davinci_nand_info *info = to_davinci_nand(mtd); |
|---|
| 104 | | - void __iomem *addr = info->current_cs; |
|---|
| 105 | | - struct nand_chip *nand = mtd_to_nand(mtd); |
|---|
| 106 | | - |
|---|
| 107 | | - /* Did the control lines change? */ |
|---|
| 108 | | - if (ctrl & NAND_CTRL_CHANGE) { |
|---|
| 109 | | - if ((ctrl & NAND_CTRL_CLE) == NAND_CTRL_CLE) |
|---|
| 110 | | - addr += info->mask_cle; |
|---|
| 111 | | - else if ((ctrl & NAND_CTRL_ALE) == NAND_CTRL_ALE) |
|---|
| 112 | | - addr += info->mask_ale; |
|---|
| 113 | | - |
|---|
| 114 | | - nand->IO_ADDR_W = addr; |
|---|
| 115 | | - } |
|---|
| 116 | | - |
|---|
| 117 | | - if (cmd != NAND_CMD_NONE) |
|---|
| 118 | | - iowrite8(cmd, nand->IO_ADDR_W); |
|---|
| 119 | | -} |
|---|
| 120 | | - |
|---|
| 121 | | -static void nand_davinci_select_chip(struct mtd_info *mtd, int chip) |
|---|
| 122 | | -{ |
|---|
| 123 | | - struct davinci_nand_info *info = to_davinci_nand(mtd); |
|---|
| 124 | | - |
|---|
| 125 | | - info->current_cs = info->vaddr; |
|---|
| 126 | | - |
|---|
| 127 | | - /* maybe kick in a second chipselect */ |
|---|
| 128 | | - if (chip > 0) |
|---|
| 129 | | - info->current_cs += info->mask_chipsel; |
|---|
| 130 | | - |
|---|
| 131 | | - info->chip.IO_ADDR_W = info->current_cs; |
|---|
| 132 | | - info->chip.IO_ADDR_R = info->chip.IO_ADDR_W; |
|---|
| 133 | | -} |
|---|
| 134 | | - |
|---|
| 135 | | -/*----------------------------------------------------------------------*/ |
|---|
| 136 | | - |
|---|
| 137 | | -/* |
|---|
| 138 | 85 | * 1-bit hardware ECC ... context maintained for each core chipselect |
|---|
| 139 | 86 | */ |
|---|
| 140 | 87 | |
|---|
| .. | .. |
|---|
| 146 | 93 | + 4 * info->core_chipsel); |
|---|
| 147 | 94 | } |
|---|
| 148 | 95 | |
|---|
| 149 | | -static void nand_davinci_hwctl_1bit(struct mtd_info *mtd, int mode) |
|---|
| 96 | +static void nand_davinci_hwctl_1bit(struct nand_chip *chip, int mode) |
|---|
| 150 | 97 | { |
|---|
| 151 | 98 | struct davinci_nand_info *info; |
|---|
| 152 | 99 | uint32_t nandcfr; |
|---|
| 153 | 100 | unsigned long flags; |
|---|
| 154 | 101 | |
|---|
| 155 | | - info = to_davinci_nand(mtd); |
|---|
| 102 | + info = to_davinci_nand(nand_to_mtd(chip)); |
|---|
| 156 | 103 | |
|---|
| 157 | 104 | /* Reset ECC hardware */ |
|---|
| 158 | | - nand_davinci_readecc_1bit(mtd); |
|---|
| 105 | + nand_davinci_readecc_1bit(nand_to_mtd(chip)); |
|---|
| 159 | 106 | |
|---|
| 160 | 107 | spin_lock_irqsave(&davinci_nand_lock, flags); |
|---|
| 161 | 108 | |
|---|
| .. | .. |
|---|
| 170 | 117 | /* |
|---|
| 171 | 118 | * Read hardware ECC value and pack into three bytes |
|---|
| 172 | 119 | */ |
|---|
| 173 | | -static int nand_davinci_calculate_1bit(struct mtd_info *mtd, |
|---|
| 174 | | - const u_char *dat, u_char *ecc_code) |
|---|
| 120 | +static int nand_davinci_calculate_1bit(struct nand_chip *chip, |
|---|
| 121 | + const u_char *dat, u_char *ecc_code) |
|---|
| 175 | 122 | { |
|---|
| 176 | | - unsigned int ecc_val = nand_davinci_readecc_1bit(mtd); |
|---|
| 123 | + unsigned int ecc_val = nand_davinci_readecc_1bit(nand_to_mtd(chip)); |
|---|
| 177 | 124 | unsigned int ecc24 = (ecc_val & 0x0fff) | ((ecc_val & 0x0fff0000) >> 4); |
|---|
| 178 | 125 | |
|---|
| 179 | 126 | /* invert so that erased block ecc is correct */ |
|---|
| .. | .. |
|---|
| 185 | 132 | return 0; |
|---|
| 186 | 133 | } |
|---|
| 187 | 134 | |
|---|
| 188 | | -static int nand_davinci_correct_1bit(struct mtd_info *mtd, u_char *dat, |
|---|
| 135 | +static int nand_davinci_correct_1bit(struct nand_chip *chip, u_char *dat, |
|---|
| 189 | 136 | u_char *read_ecc, u_char *calc_ecc) |
|---|
| 190 | 137 | { |
|---|
| 191 | | - struct nand_chip *chip = mtd_to_nand(mtd); |
|---|
| 192 | 138 | uint32_t eccNand = read_ecc[0] | (read_ecc[1] << 8) | |
|---|
| 193 | 139 | (read_ecc[2] << 16); |
|---|
| 194 | 140 | uint32_t eccCalc = calc_ecc[0] | (calc_ecc[1] << 8) | |
|---|
| .. | .. |
|---|
| 222 | 168 | /* |
|---|
| 223 | 169 | * 4-bit hardware ECC ... context maintained over entire AEMIF |
|---|
| 224 | 170 | * |
|---|
| 225 | | - * This is a syndrome engine, but we avoid NAND_ECC_HW_SYNDROME |
|---|
| 171 | + * This is a syndrome engine, but we avoid NAND_ECC_PLACEMENT_INTERLEAVED |
|---|
| 226 | 172 | * since that forces use of a problematic "infix OOB" layout. |
|---|
| 227 | 173 | * Among other things, it trashes manufacturer bad block markers. |
|---|
| 228 | 174 | * Also, and specific to this hardware, it ECC-protects the "prepad" |
|---|
| .. | .. |
|---|
| 231 | 177 | * OOB without recomputing ECC. |
|---|
| 232 | 178 | */ |
|---|
| 233 | 179 | |
|---|
| 234 | | -static void nand_davinci_hwctl_4bit(struct mtd_info *mtd, int mode) |
|---|
| 180 | +static void nand_davinci_hwctl_4bit(struct nand_chip *chip, int mode) |
|---|
| 235 | 181 | { |
|---|
| 236 | | - struct davinci_nand_info *info = to_davinci_nand(mtd); |
|---|
| 182 | + struct davinci_nand_info *info = to_davinci_nand(nand_to_mtd(chip)); |
|---|
| 237 | 183 | unsigned long flags; |
|---|
| 238 | 184 | u32 val; |
|---|
| 239 | 185 | |
|---|
| .. | .. |
|---|
| 266 | 212 | } |
|---|
| 267 | 213 | |
|---|
| 268 | 214 | /* Terminate read ECC; or return ECC (as bytes) of data written to NAND. */ |
|---|
| 269 | | -static int nand_davinci_calculate_4bit(struct mtd_info *mtd, |
|---|
| 270 | | - const u_char *dat, u_char *ecc_code) |
|---|
| 215 | +static int nand_davinci_calculate_4bit(struct nand_chip *chip, |
|---|
| 216 | + const u_char *dat, u_char *ecc_code) |
|---|
| 271 | 217 | { |
|---|
| 272 | | - struct davinci_nand_info *info = to_davinci_nand(mtd); |
|---|
| 218 | + struct davinci_nand_info *info = to_davinci_nand(nand_to_mtd(chip)); |
|---|
| 273 | 219 | u32 raw_ecc[4], *p; |
|---|
| 274 | 220 | unsigned i; |
|---|
| 275 | 221 | |
|---|
| .. | .. |
|---|
| 303 | 249 | /* Correct up to 4 bits in data we just read, using state left in the |
|---|
| 304 | 250 | * hardware plus the ecc_code computed when it was first written. |
|---|
| 305 | 251 | */ |
|---|
| 306 | | -static int nand_davinci_correct_4bit(struct mtd_info *mtd, |
|---|
| 307 | | - u_char *data, u_char *ecc_code, u_char *null) |
|---|
| 252 | +static int nand_davinci_correct_4bit(struct nand_chip *chip, u_char *data, |
|---|
| 253 | + u_char *ecc_code, u_char *null) |
|---|
| 308 | 254 | { |
|---|
| 309 | 255 | int i; |
|---|
| 310 | | - struct davinci_nand_info *info = to_davinci_nand(mtd); |
|---|
| 256 | + struct davinci_nand_info *info = to_davinci_nand(nand_to_mtd(chip)); |
|---|
| 311 | 257 | unsigned short ecc10[8]; |
|---|
| 312 | 258 | unsigned short *ecc16; |
|---|
| 313 | 259 | u32 syndrome[4]; |
|---|
| .. | .. |
|---|
| 425 | 371 | return corrected; |
|---|
| 426 | 372 | } |
|---|
| 427 | 373 | |
|---|
| 428 | | -/*----------------------------------------------------------------------*/ |
|---|
| 429 | | - |
|---|
| 430 | | -/* |
|---|
| 431 | | - * NOTE: NAND boot requires ALE == EM_A[1], CLE == EM_A[2], so that's |
|---|
| 432 | | - * how these chips are normally wired. This translates to both 8 and 16 |
|---|
| 433 | | - * bit busses using ALE == BIT(3) in byte addresses, and CLE == BIT(4). |
|---|
| 374 | +/** |
|---|
| 375 | + * nand_davinci_read_page_hwecc_oob_first - Hardware ECC page read with ECC |
|---|
| 376 | + * data read from OOB area |
|---|
| 377 | + * @chip: nand chip info structure |
|---|
| 378 | + * @buf: buffer to store read data |
|---|
| 379 | + * @oob_required: caller requires OOB data read to chip->oob_poi |
|---|
| 380 | + * @page: page number to read |
|---|
| 434 | 381 | * |
|---|
| 435 | | - * For now we assume that configuration, or any other one which ignores |
|---|
| 436 | | - * the two LSBs for NAND access ... so we can issue 32-bit reads/writes |
|---|
| 437 | | - * and have that transparently morphed into multiple NAND operations. |
|---|
| 382 | + * Hardware ECC for large page chips, which requires the ECC data to be |
|---|
| 383 | + * extracted from the OOB before the actual data is read. |
|---|
| 438 | 384 | */ |
|---|
| 439 | | -static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
|---|
| 385 | +static int nand_davinci_read_page_hwecc_oob_first(struct nand_chip *chip, |
|---|
| 386 | + uint8_t *buf, |
|---|
| 387 | + int oob_required, int page) |
|---|
| 440 | 388 | { |
|---|
| 441 | | - struct nand_chip *chip = mtd_to_nand(mtd); |
|---|
| 389 | + struct mtd_info *mtd = nand_to_mtd(chip); |
|---|
| 390 | + int i, eccsize = chip->ecc.size, ret; |
|---|
| 391 | + int eccbytes = chip->ecc.bytes; |
|---|
| 392 | + int eccsteps = chip->ecc.steps; |
|---|
| 393 | + uint8_t *p = buf; |
|---|
| 394 | + uint8_t *ecc_code = chip->ecc.code_buf; |
|---|
| 395 | + unsigned int max_bitflips = 0; |
|---|
| 442 | 396 | |
|---|
| 443 | | - if ((0x03 & ((uintptr_t)buf)) == 0 && (0x03 & len) == 0) |
|---|
| 444 | | - ioread32_rep(chip->IO_ADDR_R, buf, len >> 2); |
|---|
| 445 | | - else if ((0x01 & ((uintptr_t)buf)) == 0 && (0x01 & len) == 0) |
|---|
| 446 | | - ioread16_rep(chip->IO_ADDR_R, buf, len >> 1); |
|---|
| 447 | | - else |
|---|
| 448 | | - ioread8_rep(chip->IO_ADDR_R, buf, len); |
|---|
| 449 | | -} |
|---|
| 397 | + /* Read the OOB area first */ |
|---|
| 398 | + ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
|---|
| 399 | + if (ret) |
|---|
| 400 | + return ret; |
|---|
| 450 | 401 | |
|---|
| 451 | | -static void nand_davinci_write_buf(struct mtd_info *mtd, |
|---|
| 452 | | - const uint8_t *buf, int len) |
|---|
| 453 | | -{ |
|---|
| 454 | | - struct nand_chip *chip = mtd_to_nand(mtd); |
|---|
| 402 | + /* Move read cursor to start of page */ |
|---|
| 403 | + ret = nand_change_read_column_op(chip, 0, NULL, 0, false); |
|---|
| 404 | + if (ret) |
|---|
| 405 | + return ret; |
|---|
| 455 | 406 | |
|---|
| 456 | | - if ((0x03 & ((uintptr_t)buf)) == 0 && (0x03 & len) == 0) |
|---|
| 457 | | - iowrite32_rep(chip->IO_ADDR_R, buf, len >> 2); |
|---|
| 458 | | - else if ((0x01 & ((uintptr_t)buf)) == 0 && (0x01 & len) == 0) |
|---|
| 459 | | - iowrite16_rep(chip->IO_ADDR_R, buf, len >> 1); |
|---|
| 460 | | - else |
|---|
| 461 | | - iowrite8_rep(chip->IO_ADDR_R, buf, len); |
|---|
| 462 | | -} |
|---|
| 407 | + ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
|---|
| 408 | + chip->ecc.total); |
|---|
| 409 | + if (ret) |
|---|
| 410 | + return ret; |
|---|
| 463 | 411 | |
|---|
| 464 | | -/* |
|---|
| 465 | | - * Check hardware register for wait status. Returns 1 if device is ready, |
|---|
| 466 | | - * 0 if it is still busy. |
|---|
| 467 | | - */ |
|---|
| 468 | | -static int nand_davinci_dev_ready(struct mtd_info *mtd) |
|---|
| 469 | | -{ |
|---|
| 470 | | - struct davinci_nand_info *info = to_davinci_nand(mtd); |
|---|
| 412 | + for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
|---|
| 413 | + int stat; |
|---|
| 471 | 414 | |
|---|
| 472 | | - return davinci_nand_readl(info, NANDFSR_OFFSET) & BIT(0); |
|---|
| 415 | + chip->ecc.hwctl(chip, NAND_ECC_READ); |
|---|
| 416 | + |
|---|
| 417 | + ret = nand_read_data_op(chip, p, eccsize, false, false); |
|---|
| 418 | + if (ret) |
|---|
| 419 | + return ret; |
|---|
| 420 | + |
|---|
| 421 | + stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL); |
|---|
| 422 | + if (stat == -EBADMSG && |
|---|
| 423 | + (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
|---|
| 424 | + /* check for empty pages with bitflips */ |
|---|
| 425 | + stat = nand_check_erased_ecc_chunk(p, eccsize, |
|---|
| 426 | + &ecc_code[i], |
|---|
| 427 | + eccbytes, NULL, 0, |
|---|
| 428 | + chip->ecc.strength); |
|---|
| 429 | + } |
|---|
| 430 | + |
|---|
| 431 | + if (stat < 0) { |
|---|
| 432 | + mtd->ecc_stats.failed++; |
|---|
| 433 | + } else { |
|---|
| 434 | + mtd->ecc_stats.corrected += stat; |
|---|
| 435 | + max_bitflips = max_t(unsigned int, max_bitflips, stat); |
|---|
| 436 | + } |
|---|
| 437 | + } |
|---|
| 438 | + return max_bitflips; |
|---|
| 473 | 439 | } |
|---|
| 474 | 440 | |
|---|
| 475 | 441 | /*----------------------------------------------------------------------*/ |
|---|
| .. | .. |
|---|
| 560 | 526 | if (!of_property_read_string(pdev->dev.of_node, |
|---|
| 561 | 527 | "ti,davinci-ecc-mode", &mode)) { |
|---|
| 562 | 528 | if (!strncmp("none", mode, 4)) |
|---|
| 563 | | - pdata->ecc_mode = NAND_ECC_NONE; |
|---|
| 529 | + pdata->engine_type = NAND_ECC_ENGINE_TYPE_NONE; |
|---|
| 564 | 530 | if (!strncmp("soft", mode, 4)) |
|---|
| 565 | | - pdata->ecc_mode = NAND_ECC_SOFT; |
|---|
| 531 | + pdata->engine_type = NAND_ECC_ENGINE_TYPE_SOFT; |
|---|
| 566 | 532 | if (!strncmp("hw", mode, 2)) |
|---|
| 567 | | - pdata->ecc_mode = NAND_ECC_HW; |
|---|
| 533 | + pdata->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST; |
|---|
| 568 | 534 | } |
|---|
| 569 | 535 | if (!of_property_read_u32(pdev->dev.of_node, |
|---|
| 570 | 536 | "ti,davinci-ecc-bits", &prop)) |
|---|
| .. | .. |
|---|
| 615 | 581 | if (IS_ERR(pdata)) |
|---|
| 616 | 582 | return PTR_ERR(pdata); |
|---|
| 617 | 583 | |
|---|
| 618 | | - switch (info->chip.ecc.mode) { |
|---|
| 619 | | - case NAND_ECC_NONE: |
|---|
| 584 | + /* Use board-specific ECC config */ |
|---|
| 585 | + info->chip.ecc.engine_type = pdata->engine_type; |
|---|
| 586 | + info->chip.ecc.placement = pdata->ecc_placement; |
|---|
| 587 | + |
|---|
| 588 | + switch (info->chip.ecc.engine_type) { |
|---|
| 589 | + case NAND_ECC_ENGINE_TYPE_NONE: |
|---|
| 620 | 590 | pdata->ecc_bits = 0; |
|---|
| 621 | 591 | break; |
|---|
| 622 | | - case NAND_ECC_SOFT: |
|---|
| 592 | + case NAND_ECC_ENGINE_TYPE_SOFT: |
|---|
| 623 | 593 | pdata->ecc_bits = 0; |
|---|
| 624 | 594 | /* |
|---|
| 625 | | - * This driver expects Hamming based ECC when ecc_mode is set |
|---|
| 626 | | - * to NAND_ECC_SOFT. Force ecc.algo to NAND_ECC_HAMMING to |
|---|
| 627 | | - * avoid adding an extra ->ecc_algo field to |
|---|
| 628 | | - * davinci_nand_pdata. |
|---|
| 595 | + * This driver expects Hamming based ECC when engine_type is set |
|---|
| 596 | + * to NAND_ECC_ENGINE_TYPE_SOFT. Force ecc.algo to |
|---|
| 597 | + * NAND_ECC_ALGO_HAMMING to avoid adding an extra ->ecc_algo |
|---|
| 598 | + * field to davinci_nand_pdata. |
|---|
| 629 | 599 | */ |
|---|
| 630 | | - info->chip.ecc.algo = NAND_ECC_HAMMING; |
|---|
| 600 | + info->chip.ecc.algo = NAND_ECC_ALGO_HAMMING; |
|---|
| 631 | 601 | break; |
|---|
| 632 | | - case NAND_ECC_HW: |
|---|
| 602 | + case NAND_ECC_ENGINE_TYPE_ON_HOST: |
|---|
| 633 | 603 | if (pdata->ecc_bits == 4) { |
|---|
| 604 | + int chunks = mtd->writesize / 512; |
|---|
| 605 | + |
|---|
| 606 | + if (!chunks || mtd->oobsize < 16) { |
|---|
| 607 | + dev_dbg(&info->pdev->dev, "too small\n"); |
|---|
| 608 | + return -EINVAL; |
|---|
| 609 | + } |
|---|
| 610 | + |
|---|
| 634 | 611 | /* |
|---|
| 635 | 612 | * No sanity checks: CPUs must support this, |
|---|
| 636 | 613 | * and the chips may not use NAND_BUSWIDTH_16. |
|---|
| .. | .. |
|---|
| 652 | 629 | info->chip.ecc.hwctl = nand_davinci_hwctl_4bit; |
|---|
| 653 | 630 | info->chip.ecc.bytes = 10; |
|---|
| 654 | 631 | info->chip.ecc.options = NAND_ECC_GENERIC_ERASED_CHECK; |
|---|
| 655 | | - info->chip.ecc.algo = NAND_ECC_BCH; |
|---|
| 632 | + info->chip.ecc.algo = NAND_ECC_ALGO_BCH; |
|---|
| 633 | + |
|---|
| 634 | + /* |
|---|
| 635 | + * Update ECC layout if needed ... for 1-bit HW ECC, the |
|---|
| 636 | + * default is OK, but it allocates 6 bytes when only 3 |
|---|
| 637 | + * are needed (for each 512 bytes). For 4-bit HW ECC, |
|---|
| 638 | + * the default is not usable: 10 bytes needed, not 6. |
|---|
| 639 | + * |
|---|
| 640 | + * For small page chips, preserve the manufacturer's |
|---|
| 641 | + * badblock marking data ... and make sure a flash BBT |
|---|
| 642 | + * table marker fits in the free bytes. |
|---|
| 643 | + */ |
|---|
| 644 | + if (chunks == 1) { |
|---|
| 645 | + mtd_set_ooblayout(mtd, |
|---|
| 646 | + &hwecc4_small_ooblayout_ops); |
|---|
| 647 | + } else if (chunks == 4 || chunks == 8) { |
|---|
| 648 | + mtd_set_ooblayout(mtd, |
|---|
| 649 | + nand_get_large_page_ooblayout()); |
|---|
| 650 | + info->chip.ecc.read_page = nand_davinci_read_page_hwecc_oob_first; |
|---|
| 651 | + } else { |
|---|
| 652 | + return -EIO; |
|---|
| 653 | + } |
|---|
| 656 | 654 | } else { |
|---|
| 657 | 655 | /* 1bit ecc hamming */ |
|---|
| 658 | 656 | info->chip.ecc.calculate = nand_davinci_calculate_1bit; |
|---|
| 659 | 657 | info->chip.ecc.correct = nand_davinci_correct_1bit; |
|---|
| 660 | 658 | info->chip.ecc.hwctl = nand_davinci_hwctl_1bit; |
|---|
| 661 | 659 | info->chip.ecc.bytes = 3; |
|---|
| 662 | | - info->chip.ecc.algo = NAND_ECC_HAMMING; |
|---|
| 660 | + info->chip.ecc.algo = NAND_ECC_ALGO_HAMMING; |
|---|
| 663 | 661 | } |
|---|
| 664 | 662 | info->chip.ecc.size = 512; |
|---|
| 665 | 663 | info->chip.ecc.strength = pdata->ecc_bits; |
|---|
| .. | .. |
|---|
| 668 | 666 | return -EINVAL; |
|---|
| 669 | 667 | } |
|---|
| 670 | 668 | |
|---|
| 671 | | - /* |
|---|
| 672 | | - * Update ECC layout if needed ... for 1-bit HW ECC, the default |
|---|
| 673 | | - * is OK, but it allocates 6 bytes when only 3 are needed (for |
|---|
| 674 | | - * each 512 bytes). For the 4-bit HW ECC, that default is not |
|---|
| 675 | | - * usable: 10 bytes are needed, not 6. |
|---|
| 676 | | - */ |
|---|
| 677 | | - if (pdata->ecc_bits == 4) { |
|---|
| 678 | | - int chunks = mtd->writesize / 512; |
|---|
| 669 | + return ret; |
|---|
| 670 | +} |
|---|
| 679 | 671 | |
|---|
| 680 | | - if (!chunks || mtd->oobsize < 16) { |
|---|
| 681 | | - dev_dbg(&info->pdev->dev, "too small\n"); |
|---|
| 682 | | - return -EINVAL; |
|---|
| 683 | | - } |
|---|
| 672 | +static void nand_davinci_data_in(struct davinci_nand_info *info, void *buf, |
|---|
| 673 | + unsigned int len, bool force_8bit) |
|---|
| 674 | +{ |
|---|
| 675 | + u32 alignment = ((uintptr_t)buf | len) & 3; |
|---|
| 684 | 676 | |
|---|
| 685 | | - /* For small page chips, preserve the manufacturer's |
|---|
| 686 | | - * badblock marking data ... and make sure a flash BBT |
|---|
| 687 | | - * table marker fits in the free bytes. |
|---|
| 688 | | - */ |
|---|
| 689 | | - if (chunks == 1) { |
|---|
| 690 | | - mtd_set_ooblayout(mtd, &hwecc4_small_ooblayout_ops); |
|---|
| 691 | | - } else if (chunks == 4 || chunks == 8) { |
|---|
| 692 | | - mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
|---|
| 693 | | - info->chip.ecc.mode = NAND_ECC_HW_OOB_FIRST; |
|---|
| 694 | | - } else { |
|---|
| 695 | | - return -EIO; |
|---|
| 677 | + if (force_8bit || (alignment & 1)) |
|---|
| 678 | + ioread8_rep(info->current_cs, buf, len); |
|---|
| 679 | + else if (alignment & 3) |
|---|
| 680 | + ioread16_rep(info->current_cs, buf, len >> 1); |
|---|
| 681 | + else |
|---|
| 682 | + ioread32_rep(info->current_cs, buf, len >> 2); |
|---|
| 683 | +} |
|---|
| 684 | + |
|---|
| 685 | +static void nand_davinci_data_out(struct davinci_nand_info *info, |
|---|
| 686 | + const void *buf, unsigned int len, |
|---|
| 687 | + bool force_8bit) |
|---|
| 688 | +{ |
|---|
| 689 | + u32 alignment = ((uintptr_t)buf | len) & 3; |
|---|
| 690 | + |
|---|
| 691 | + if (force_8bit || (alignment & 1)) |
|---|
| 692 | + iowrite8_rep(info->current_cs, buf, len); |
|---|
| 693 | + else if (alignment & 3) |
|---|
| 694 | + iowrite16_rep(info->current_cs, buf, len >> 1); |
|---|
| 695 | + else |
|---|
| 696 | + iowrite32_rep(info->current_cs, buf, len >> 2); |
|---|
| 697 | +} |
|---|
| 698 | + |
|---|
| 699 | +static int davinci_nand_exec_instr(struct davinci_nand_info *info, |
|---|
| 700 | + const struct nand_op_instr *instr) |
|---|
| 701 | +{ |
|---|
| 702 | + unsigned int i, timeout_us; |
|---|
| 703 | + u32 status; |
|---|
| 704 | + int ret; |
|---|
| 705 | + |
|---|
| 706 | + switch (instr->type) { |
|---|
| 707 | + case NAND_OP_CMD_INSTR: |
|---|
| 708 | + iowrite8(instr->ctx.cmd.opcode, |
|---|
| 709 | + info->current_cs + info->mask_cle); |
|---|
| 710 | + break; |
|---|
| 711 | + |
|---|
| 712 | + case NAND_OP_ADDR_INSTR: |
|---|
| 713 | + for (i = 0; i < instr->ctx.addr.naddrs; i++) { |
|---|
| 714 | + iowrite8(instr->ctx.addr.addrs[i], |
|---|
| 715 | + info->current_cs + info->mask_ale); |
|---|
| 696 | 716 | } |
|---|
| 717 | + break; |
|---|
| 718 | + |
|---|
| 719 | + case NAND_OP_DATA_IN_INSTR: |
|---|
| 720 | + nand_davinci_data_in(info, instr->ctx.data.buf.in, |
|---|
| 721 | + instr->ctx.data.len, |
|---|
| 722 | + instr->ctx.data.force_8bit); |
|---|
| 723 | + break; |
|---|
| 724 | + |
|---|
| 725 | + case NAND_OP_DATA_OUT_INSTR: |
|---|
| 726 | + nand_davinci_data_out(info, instr->ctx.data.buf.out, |
|---|
| 727 | + instr->ctx.data.len, |
|---|
| 728 | + instr->ctx.data.force_8bit); |
|---|
| 729 | + break; |
|---|
| 730 | + |
|---|
| 731 | + case NAND_OP_WAITRDY_INSTR: |
|---|
| 732 | + timeout_us = instr->ctx.waitrdy.timeout_ms * 1000; |
|---|
| 733 | + ret = readl_relaxed_poll_timeout(info->base + NANDFSR_OFFSET, |
|---|
| 734 | + status, status & BIT(0), 100, |
|---|
| 735 | + timeout_us); |
|---|
| 736 | + if (ret) |
|---|
| 737 | + return ret; |
|---|
| 738 | + |
|---|
| 739 | + break; |
|---|
| 697 | 740 | } |
|---|
| 698 | 741 | |
|---|
| 699 | | - return ret; |
|---|
| 742 | + if (instr->delay_ns) |
|---|
| 743 | + ndelay(instr->delay_ns); |
|---|
| 744 | + |
|---|
| 745 | + return 0; |
|---|
| 746 | +} |
|---|
| 747 | + |
|---|
| 748 | +static int davinci_nand_exec_op(struct nand_chip *chip, |
|---|
| 749 | + const struct nand_operation *op, |
|---|
| 750 | + bool check_only) |
|---|
| 751 | +{ |
|---|
| 752 | + struct davinci_nand_info *info = to_davinci_nand(nand_to_mtd(chip)); |
|---|
| 753 | + unsigned int i; |
|---|
| 754 | + |
|---|
| 755 | + if (check_only) |
|---|
| 756 | + return 0; |
|---|
| 757 | + |
|---|
| 758 | + info->current_cs = info->vaddr + (op->cs * info->mask_chipsel); |
|---|
| 759 | + |
|---|
| 760 | + for (i = 0; i < op->ninstrs; i++) { |
|---|
| 761 | + int ret; |
|---|
| 762 | + |
|---|
| 763 | + ret = davinci_nand_exec_instr(info, &op->instrs[i]); |
|---|
| 764 | + if (ret) |
|---|
| 765 | + return ret; |
|---|
| 766 | + } |
|---|
| 767 | + |
|---|
| 768 | + return 0; |
|---|
| 700 | 769 | } |
|---|
| 701 | 770 | |
|---|
| 702 | 771 | static const struct nand_controller_ops davinci_nand_controller_ops = { |
|---|
| 703 | 772 | .attach_chip = davinci_nand_attach_chip, |
|---|
| 773 | + .exec_op = davinci_nand_exec_op, |
|---|
| 704 | 774 | }; |
|---|
| 705 | 775 | |
|---|
| 706 | 776 | static int nand_davinci_probe(struct platform_device *pdev) |
|---|
| .. | .. |
|---|
| 764 | 834 | mtd->dev.parent = &pdev->dev; |
|---|
| 765 | 835 | nand_set_flash_node(&info->chip, pdev->dev.of_node); |
|---|
| 766 | 836 | |
|---|
| 767 | | - info->chip.IO_ADDR_R = vaddr; |
|---|
| 768 | | - info->chip.IO_ADDR_W = vaddr; |
|---|
| 769 | | - info->chip.chip_delay = 0; |
|---|
| 770 | | - info->chip.select_chip = nand_davinci_select_chip; |
|---|
| 771 | | - |
|---|
| 772 | 837 | /* options such as NAND_BBT_USE_FLASH */ |
|---|
| 773 | 838 | info->chip.bbt_options = pdata->bbt_options; |
|---|
| 774 | 839 | /* options such as 16-bit widths */ |
|---|
| .. | .. |
|---|
| 785 | 850 | info->mask_ale = pdata->mask_ale ? : MASK_ALE; |
|---|
| 786 | 851 | info->mask_cle = pdata->mask_cle ? : MASK_CLE; |
|---|
| 787 | 852 | |
|---|
| 788 | | - /* Set address of hardware control function */ |
|---|
| 789 | | - info->chip.cmd_ctrl = nand_davinci_hwcontrol; |
|---|
| 790 | | - info->chip.dev_ready = nand_davinci_dev_ready; |
|---|
| 791 | | - |
|---|
| 792 | | - /* Speed up buffer I/O */ |
|---|
| 793 | | - info->chip.read_buf = nand_davinci_read_buf; |
|---|
| 794 | | - info->chip.write_buf = nand_davinci_write_buf; |
|---|
| 795 | | - |
|---|
| 796 | | - /* Use board-specific ECC config */ |
|---|
| 797 | | - info->chip.ecc.mode = pdata->ecc_mode; |
|---|
| 798 | | - |
|---|
| 799 | 853 | spin_lock_irq(&davinci_nand_lock); |
|---|
| 800 | 854 | |
|---|
| 801 | 855 | /* put CSxNAND into NAND mode */ |
|---|
| .. | .. |
|---|
| 806 | 860 | spin_unlock_irq(&davinci_nand_lock); |
|---|
| 807 | 861 | |
|---|
| 808 | 862 | /* Scan to find existence of the device(s) */ |
|---|
| 809 | | - info->chip.dummy_controller.ops = &davinci_nand_controller_ops; |
|---|
| 863 | + nand_controller_init(&info->controller); |
|---|
| 864 | + info->controller.ops = &davinci_nand_controller_ops; |
|---|
| 865 | + info->chip.controller = &info->controller; |
|---|
| 810 | 866 | ret = nand_scan(&info->chip, pdata->mask_chipsel ? 2 : 1); |
|---|
| 811 | 867 | if (ret < 0) { |
|---|
| 812 | 868 | dev_dbg(&pdev->dev, "no NAND chip(s) found\n"); |
|---|
| .. | .. |
|---|
| 835 | 891 | static int nand_davinci_remove(struct platform_device *pdev) |
|---|
| 836 | 892 | { |
|---|
| 837 | 893 | struct davinci_nand_info *info = platform_get_drvdata(pdev); |
|---|
| 894 | + struct nand_chip *chip = &info->chip; |
|---|
| 895 | + int ret; |
|---|
| 838 | 896 | |
|---|
| 839 | 897 | spin_lock_irq(&davinci_nand_lock); |
|---|
| 840 | | - if (info->chip.ecc.mode == NAND_ECC_HW_SYNDROME) |
|---|
| 898 | + if (info->chip.ecc.placement == NAND_ECC_PLACEMENT_INTERLEAVED) |
|---|
| 841 | 899 | ecc4_busy = false; |
|---|
| 842 | 900 | spin_unlock_irq(&davinci_nand_lock); |
|---|
| 843 | 901 | |
|---|
| 844 | | - nand_release(&info->chip); |
|---|
| 902 | + ret = mtd_device_unregister(nand_to_mtd(chip)); |
|---|
| 903 | + WARN_ON(ret); |
|---|
| 904 | + nand_cleanup(chip); |
|---|
| 845 | 905 | |
|---|
| 846 | 906 | return 0; |
|---|
| 847 | 907 | } |
|---|