Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 2 | /* |
| 3 | * NAND driver for TI DaVinci based boards. |
| 4 | * |
| 5 | * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net> |
| 6 | * |
| 7 | * Based on Linux DaVinci NAND driver by TI. Original copyright follows: |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * |
Miquel Raynal | 1f1ae15 | 2018-08-16 17:30:07 +0200 | [diff] [blame] | 12 | * linux/drivers/mtd/nand/raw/nand_davinci.c |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 13 | * |
| 14 | * NAND Flash Driver |
| 15 | * |
| 16 | * Copyright (C) 2006 Texas Instruments. |
| 17 | * |
| 18 | * ---------------------------------------------------------------------------- |
| 19 | * |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 20 | * ---------------------------------------------------------------------------- |
| 21 | * |
| 22 | * Overview: |
| 23 | * This is a device driver for the NAND flash device found on the |
| 24 | * DaVinci board which utilizes the Samsung k9k2g08 part. |
| 25 | * |
| 26 | Modifications: |
| 27 | ver. 1.0: Feb 2005, Vinod/Sudhakar |
| 28 | - |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 29 | */ |
| 30 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 31 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 32 | #include <log.h> |
Tom Rini | 3bde7e2 | 2021-09-22 14:50:35 -0400 | [diff] [blame] | 33 | #include <linux/mtd/rawnand.h> |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 34 | #include <asm/io.h> |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 35 | #include <nand.h> |
Bartosz Golaszewski | 8a14dfe | 2019-07-29 08:58:03 +0200 | [diff] [blame] | 36 | #include <dm/uclass.h> |
Khoronzhuk, Ivan | 753a00a | 2014-06-07 04:22:52 +0300 | [diff] [blame] | 37 | #include <asm/ti-common/davinci_nand.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 38 | #include <linux/printk.h> |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 39 | |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 40 | /* Definitions for 4-bit hardware ECC */ |
| 41 | #define NAND_TIMEOUT 10240 |
| 42 | #define NAND_ECC_BUSY 0xC |
| 43 | #define NAND_4BITECC_MASK 0x03FF03FF |
Wolfgang Denk | 62fb2b4 | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 44 | #define EMIF_NANDFSR_ECC_STATE_MASK 0x00000F00 |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 45 | #define ECC_STATE_NO_ERR 0x0 |
| 46 | #define ECC_STATE_TOO_MANY_ERRS 0x1 |
| 47 | #define ECC_STATE_ERR_CORR_COMP_P 0x2 |
| 48 | #define ECC_STATE_ERR_CORR_COMP_N 0x3 |
| 49 | |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 50 | /* |
| 51 | * Exploit the little endianness of the ARM to do multi-byte transfers |
| 52 | * per device read. This can perform over twice as quickly as individual |
| 53 | * byte transfers when buffer alignment is conducive. |
| 54 | * |
| 55 | * NOTE: This only works if the NAND is not connected to the 2 LSBs of |
| 56 | * the address bus. On Davinci EVM platforms this has always been true. |
| 57 | */ |
| 58 | static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
| 59 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 60 | struct nand_chip *chip = mtd_to_nand(mtd); |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 61 | const u32 *nand = chip->IO_ADDR_R; |
| 62 | |
| 63 | /* Make sure that buf is 32 bit aligned */ |
| 64 | if (((int)buf & 0x3) != 0) { |
| 65 | if (((int)buf & 0x1) != 0) { |
| 66 | if (len) { |
| 67 | *buf = readb(nand); |
| 68 | buf += 1; |
| 69 | len--; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if (((int)buf & 0x3) != 0) { |
| 74 | if (len >= 2) { |
| 75 | *(u16 *)buf = readw(nand); |
| 76 | buf += 2; |
| 77 | len -= 2; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /* copy aligned data */ |
| 83 | while (len >= 4) { |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 84 | *(u32 *)buf = __raw_readl(nand); |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 85 | buf += 4; |
| 86 | len -= 4; |
| 87 | } |
| 88 | |
| 89 | /* mop up any remaining bytes */ |
| 90 | if (len) { |
| 91 | if (len >= 2) { |
| 92 | *(u16 *)buf = readw(nand); |
| 93 | buf += 2; |
| 94 | len -= 2; |
| 95 | } |
| 96 | |
| 97 | if (len) |
| 98 | *buf = readb(nand); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | static void nand_davinci_write_buf(struct mtd_info *mtd, const uint8_t *buf, |
| 103 | int len) |
| 104 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 105 | struct nand_chip *chip = mtd_to_nand(mtd); |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 106 | const u32 *nand = chip->IO_ADDR_W; |
| 107 | |
| 108 | /* Make sure that buf is 32 bit aligned */ |
| 109 | if (((int)buf & 0x3) != 0) { |
| 110 | if (((int)buf & 0x1) != 0) { |
| 111 | if (len) { |
| 112 | writeb(*buf, nand); |
| 113 | buf += 1; |
| 114 | len--; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (((int)buf & 0x3) != 0) { |
| 119 | if (len >= 2) { |
| 120 | writew(*(u16 *)buf, nand); |
| 121 | buf += 2; |
| 122 | len -= 2; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /* copy aligned data */ |
| 128 | while (len >= 4) { |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 129 | __raw_writel(*(u32 *)buf, nand); |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 130 | buf += 4; |
| 131 | len -= 4; |
| 132 | } |
| 133 | |
| 134 | /* mop up any remaining bytes */ |
| 135 | if (len) { |
| 136 | if (len >= 2) { |
| 137 | writew(*(u16 *)buf, nand); |
| 138 | buf += 2; |
| 139 | len -= 2; |
| 140 | } |
| 141 | |
| 142 | if (len) |
| 143 | writeb(*buf, nand); |
| 144 | } |
| 145 | } |
| 146 | |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 147 | static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd, |
| 148 | unsigned int ctrl) |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 149 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 150 | struct nand_chip *this = mtd_to_nand(mtd); |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 151 | u_int32_t IO_ADDR_W = (u_int32_t)this->IO_ADDR_W; |
| 152 | |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 153 | if (ctrl & NAND_CTRL_CHANGE) { |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 154 | IO_ADDR_W &= ~(MASK_ALE|MASK_CLE); |
| 155 | |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 156 | if (ctrl & NAND_CLE) |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 157 | IO_ADDR_W |= MASK_CLE; |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 158 | if (ctrl & NAND_ALE) |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 159 | IO_ADDR_W |= MASK_ALE; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 160 | this->IO_ADDR_W = (void __iomem *) IO_ADDR_W; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 161 | } |
| 162 | |
William Juul | 9e9c2c1 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 163 | if (cmd != NAND_CMD_NONE) |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 164 | writeb(cmd, IO_ADDR_W); |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 165 | } |
| 166 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 167 | #ifdef CONFIG_SYS_NAND_HW_ECC |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 168 | |
Laurence Withers | 0357a76 | 2011-09-26 16:02:30 +0000 | [diff] [blame] | 169 | static u_int32_t nand_davinci_readecc(struct mtd_info *mtd) |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 170 | { |
Laurence Withers | 0357a76 | 2011-09-26 16:02:30 +0000 | [diff] [blame] | 171 | u_int32_t ecc = 0; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 172 | |
Laurence Withers | 0357a76 | 2011-09-26 16:02:30 +0000 | [diff] [blame] | 173 | ecc = __raw_readl(&(davinci_emif_regs->nandfecc[ |
Tom Rini | b421349 | 2022-11-12 17:36:51 -0500 | [diff] [blame] | 174 | CFG_SYS_NAND_CS - 2])); |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 175 | |
Laurence Withers | 0357a76 | 2011-09-26 16:02:30 +0000 | [diff] [blame] | 176 | return ecc; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Laurence Withers | 0357a76 | 2011-09-26 16:02:30 +0000 | [diff] [blame] | 179 | static void nand_davinci_enable_hwecc(struct mtd_info *mtd, int mode) |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 180 | { |
Laurence Withers | 0357a76 | 2011-09-26 16:02:30 +0000 | [diff] [blame] | 181 | u_int32_t val; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 182 | |
Laurence Withers | 0357a76 | 2011-09-26 16:02:30 +0000 | [diff] [blame] | 183 | /* reading the ECC result register resets the ECC calculation */ |
| 184 | nand_davinci_readecc(mtd); |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 185 | |
Laurence Withers | 0357a76 | 2011-09-26 16:02:30 +0000 | [diff] [blame] | 186 | val = __raw_readl(&davinci_emif_regs->nandfcr); |
Tom Rini | b421349 | 2022-11-12 17:36:51 -0500 | [diff] [blame] | 187 | val |= DAVINCI_NANDFCR_NAND_ENABLE(CFG_SYS_NAND_CS); |
| 188 | val |= DAVINCI_NANDFCR_1BIT_ECC_START(CFG_SYS_NAND_CS); |
Laurence Withers | 0357a76 | 2011-09-26 16:02:30 +0000 | [diff] [blame] | 189 | __raw_writel(val, &davinci_emif_regs->nandfcr); |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 192 | static int nand_davinci_calculate_ecc(struct mtd_info *mtd, const u_char *dat, |
| 193 | u_char *ecc_code) |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 194 | { |
| 195 | u_int32_t tmp; |
Hugo Villeneuve | 73dc0e4 | 2008-08-30 17:06:55 -0400 | [diff] [blame] | 196 | |
Laurence Withers | 0357a76 | 2011-09-26 16:02:30 +0000 | [diff] [blame] | 197 | tmp = nand_davinci_readecc(mtd); |
Hugo Villeneuve | 73dc0e4 | 2008-08-30 17:06:55 -0400 | [diff] [blame] | 198 | |
| 199 | /* Squeeze 4 bytes ECC into 3 bytes by removing RESERVED bits |
| 200 | * and shifting. RESERVED bits are 31 to 28 and 15 to 12. */ |
| 201 | tmp = (tmp & 0x00000fff) | ((tmp & 0x0fff0000) >> 4); |
| 202 | |
| 203 | /* Invert so that erased block ECC is correct */ |
| 204 | tmp = ~tmp; |
| 205 | |
| 206 | *ecc_code++ = tmp; |
| 207 | *ecc_code++ = tmp >> 8; |
| 208 | *ecc_code++ = tmp >> 16; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 209 | |
David Brownell | 962d4c1 | 2009-04-28 13:19:53 -0700 | [diff] [blame] | 210 | /* NOTE: the above code matches mainline Linux: |
| 211 | * .PQR.stu ==> ~PQRstu |
| 212 | * |
| 213 | * MontaVista/TI kernels encode those bytes differently, use |
| 214 | * complicated (and allegedly sometimes-wrong) correction code, |
| 215 | * and usually shipped with U-Boot that uses software ECC: |
| 216 | * .PQR.stu ==> PsQRtu |
| 217 | * |
| 218 | * If you need MV/TI compatible NAND I/O in U-Boot, it should |
| 219 | * be possible to (a) change the mangling above, (b) reverse |
| 220 | * that mangling in nand_davinci_correct_data() below. |
| 221 | */ |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 222 | |
David Brownell | 962d4c1 | 2009-04-28 13:19:53 -0700 | [diff] [blame] | 223 | return 0; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 226 | static int nand_davinci_correct_data(struct mtd_info *mtd, u_char *dat, |
| 227 | u_char *read_ecc, u_char *calc_ecc) |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 228 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 229 | struct nand_chip *this = mtd_to_nand(mtd); |
Hugo Villeneuve | 73dc0e4 | 2008-08-30 17:06:55 -0400 | [diff] [blame] | 230 | u_int32_t ecc_nand = read_ecc[0] | (read_ecc[1] << 8) | |
| 231 | (read_ecc[2] << 16); |
| 232 | u_int32_t ecc_calc = calc_ecc[0] | (calc_ecc[1] << 8) | |
| 233 | (calc_ecc[2] << 16); |
| 234 | u_int32_t diff = ecc_calc ^ ecc_nand; |
| 235 | |
| 236 | if (diff) { |
| 237 | if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) { |
| 238 | /* Correctable error */ |
| 239 | if ((diff >> (12 + 3)) < this->ecc.size) { |
| 240 | uint8_t find_bit = 1 << ((diff >> 12) & 7); |
| 241 | uint32_t find_byte = diff >> (12 + 3); |
| 242 | |
| 243 | dat[find_byte] ^= find_bit; |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 244 | pr_debug("Correcting single " |
Hugo Villeneuve | 73dc0e4 | 2008-08-30 17:06:55 -0400 | [diff] [blame] | 245 | "bit ECC error at offset: %d, bit: " |
| 246 | "%d\n", find_byte, find_bit); |
| 247 | return 1; |
| 248 | } else { |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 249 | return -EBADMSG; |
Hugo Villeneuve | 73dc0e4 | 2008-08-30 17:06:55 -0400 | [diff] [blame] | 250 | } |
| 251 | } else if (!(diff & (diff - 1))) { |
| 252 | /* Single bit ECC error in the ECC itself, |
| 253 | nothing to fix */ |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 254 | pr_debug("Single bit ECC error in " "ECC.\n"); |
Hugo Villeneuve | 73dc0e4 | 2008-08-30 17:06:55 -0400 | [diff] [blame] | 255 | return 1; |
| 256 | } else { |
| 257 | /* Uncorrectable error */ |
Masahiro Yamada | f8a5d51 | 2017-10-18 00:10:48 +0900 | [diff] [blame] | 258 | pr_debug("ECC UNCORRECTED_ERROR 1\n"); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 259 | return -EBADMSG; |
Hugo Villeneuve | 73dc0e4 | 2008-08-30 17:06:55 -0400 | [diff] [blame] | 260 | } |
| 261 | } |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 262 | return 0; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 263 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 264 | #endif /* CONFIG_SYS_NAND_HW_ECC */ |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 265 | |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 266 | #ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST |
| 267 | static struct nand_ecclayout nand_davinci_4bit_layout_oobfirst = { |
Sandeep Paulraj | d348286 | 2009-11-19 23:04:42 -0500 | [diff] [blame] | 268 | #if defined(CONFIG_SYS_NAND_PAGE_2K) |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 269 | .eccbytes = 40, |
Heiko Schocher | 56596e1 | 2013-09-06 05:21:23 +0200 | [diff] [blame] | 270 | #ifdef CONFIG_NAND_6BYTES_OOB_FREE_10BYTES_ECC |
| 271 | .eccpos = { |
| 272 | 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 273 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 274 | 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, |
| 275 | 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, |
| 276 | }, |
| 277 | .oobfree = { |
| 278 | {2, 4}, {16, 6}, {32, 6}, {48, 6}, |
| 279 | }, |
| 280 | #else |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 281 | .eccpos = { |
| 282 | 24, 25, 26, 27, 28, |
| 283 | 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, |
| 284 | 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, |
| 285 | 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, |
| 286 | 59, 60, 61, 62, 63, |
| 287 | }, |
| 288 | .oobfree = { |
| 289 | {.offset = 2, .length = 22, }, |
| 290 | }, |
Heiko Schocher | 56596e1 | 2013-09-06 05:21:23 +0200 | [diff] [blame] | 291 | #endif /* #ifdef CONFIG_NAND_6BYTES_OOB_FREE_10BYTES_ECC */ |
Sandeep Paulraj | d348286 | 2009-11-19 23:04:42 -0500 | [diff] [blame] | 292 | #elif defined(CONFIG_SYS_NAND_PAGE_4K) |
| 293 | .eccbytes = 80, |
| 294 | .eccpos = { |
| 295 | 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, |
| 296 | 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, |
| 297 | 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, |
| 298 | 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, |
| 299 | 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, |
| 300 | 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, |
| 301 | 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, |
| 302 | 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, |
| 303 | }, |
| 304 | .oobfree = { |
| 305 | {.offset = 2, .length = 46, }, |
| 306 | }, |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 307 | #endif |
| 308 | }; |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 309 | |
Khoronzhuk, Ivan | 2f64e47 | 2014-07-04 15:03:25 +0300 | [diff] [blame] | 310 | #if defined CONFIG_KEYSTONE_RBL_NAND |
Khoronzhuk, Ivan | 2f64e47 | 2014-07-04 15:03:25 +0300 | [diff] [blame] | 311 | static struct nand_ecclayout nand_keystone_rbl_4bit_layout_oobfirst = { |
Khoronzhuk, Ivan | 9016c6f | 2014-09-02 00:20:02 +0300 | [diff] [blame] | 312 | #if defined(CONFIG_SYS_NAND_PAGE_2K) |
Khoronzhuk, Ivan | 2f64e47 | 2014-07-04 15:03:25 +0300 | [diff] [blame] | 313 | .eccbytes = 40, |
| 314 | .eccpos = { |
| 315 | 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 316 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 317 | 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, |
| 318 | 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, |
| 319 | }, |
| 320 | .oobfree = { |
| 321 | {.offset = 2, .length = 4, }, |
| 322 | {.offset = 16, .length = 6, }, |
| 323 | {.offset = 32, .length = 6, }, |
| 324 | {.offset = 48, .length = 6, }, |
| 325 | }, |
| 326 | #elif defined(CONFIG_SYS_NAND_PAGE_4K) |
| 327 | .eccbytes = 80, |
| 328 | .eccpos = { |
| 329 | 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 330 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 331 | 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, |
| 332 | 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, |
| 333 | 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, |
| 334 | 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, |
| 335 | 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, |
| 336 | 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, |
| 337 | }, |
| 338 | .oobfree = { |
| 339 | {.offset = 2, .length = 4, }, |
| 340 | {.offset = 16, .length = 6, }, |
| 341 | {.offset = 32, .length = 6, }, |
| 342 | {.offset = 48, .length = 6, }, |
| 343 | {.offset = 64, .length = 6, }, |
| 344 | {.offset = 80, .length = 6, }, |
| 345 | {.offset = 96, .length = 6, }, |
| 346 | {.offset = 112, .length = 6, }, |
| 347 | }, |
| 348 | #endif |
| 349 | }; |
| 350 | |
| 351 | #ifdef CONFIG_SYS_NAND_PAGE_2K |
Tom Rini | dada0e3 | 2021-09-12 20:32:24 -0400 | [diff] [blame] | 352 | #define KEYSTONE_NAND_MAX_RBL_PAGE (0x100000 >> 11) |
Khoronzhuk, Ivan | 2f64e47 | 2014-07-04 15:03:25 +0300 | [diff] [blame] | 353 | #elif defined(CONFIG_SYS_NAND_PAGE_4K) |
Tom Rini | dada0e3 | 2021-09-12 20:32:24 -0400 | [diff] [blame] | 354 | #define KEYSTONE_NAND_MAX_RBL_PAGE (0x100000 >> 12) |
Khoronzhuk, Ivan | 2f64e47 | 2014-07-04 15:03:25 +0300 | [diff] [blame] | 355 | #endif |
| 356 | |
| 357 | /** |
| 358 | * nand_davinci_write_page - write one page |
| 359 | * @mtd: MTD device structure |
| 360 | * @chip: NAND chip descriptor |
| 361 | * @buf: the data to write |
| 362 | * @oob_required: must write chip->oob_poi to OOB |
| 363 | * @page: page number to write |
Khoronzhuk, Ivan | 2f64e47 | 2014-07-04 15:03:25 +0300 | [diff] [blame] | 364 | * @raw: use _raw version of write_page |
| 365 | */ |
| 366 | static int nand_davinci_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
Khoronzhuk, Ivan | d5c6580 | 2014-09-06 22:17:07 +0300 | [diff] [blame] | 367 | uint32_t offset, int data_len, |
Khoronzhuk, Ivan | 2f64e47 | 2014-07-04 15:03:25 +0300 | [diff] [blame] | 368 | const uint8_t *buf, int oob_required, |
Boris Brezillon | b9bf43c | 2017-11-22 02:38:24 +0900 | [diff] [blame] | 369 | int page, int raw) |
Khoronzhuk, Ivan | 2f64e47 | 2014-07-04 15:03:25 +0300 | [diff] [blame] | 370 | { |
| 371 | int status; |
| 372 | int ret = 0; |
| 373 | struct nand_ecclayout *saved_ecc_layout; |
| 374 | |
| 375 | /* save current ECC layout and assign Keystone RBL ECC layout */ |
Tom Rini | dada0e3 | 2021-09-12 20:32:24 -0400 | [diff] [blame] | 376 | if (page < KEYSTONE_NAND_MAX_RBL_PAGE) { |
Khoronzhuk, Ivan | 2f64e47 | 2014-07-04 15:03:25 +0300 | [diff] [blame] | 377 | saved_ecc_layout = chip->ecc.layout; |
| 378 | chip->ecc.layout = &nand_keystone_rbl_4bit_layout_oobfirst; |
| 379 | mtd->oobavail = chip->ecc.layout->oobavail; |
| 380 | } |
| 381 | |
| 382 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page); |
| 383 | |
Scott Wood | 46e1310 | 2016-05-30 13:57:57 -0500 | [diff] [blame] | 384 | if (unlikely(raw)) { |
| 385 | status = chip->ecc.write_page_raw(mtd, chip, buf, |
| 386 | oob_required, page); |
| 387 | } else { |
| 388 | status = chip->ecc.write_page(mtd, chip, buf, |
| 389 | oob_required, page); |
| 390 | } |
Khoronzhuk, Ivan | 2f64e47 | 2014-07-04 15:03:25 +0300 | [diff] [blame] | 391 | |
| 392 | if (status < 0) { |
| 393 | ret = status; |
| 394 | goto err; |
| 395 | } |
| 396 | |
| 397 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 398 | status = chip->waitfunc(mtd, chip); |
| 399 | |
Khoronzhuk, Ivan | 2f64e47 | 2014-07-04 15:03:25 +0300 | [diff] [blame] | 400 | if (status & NAND_STATUS_FAIL) { |
| 401 | ret = -EIO; |
| 402 | goto err; |
| 403 | } |
| 404 | |
Khoronzhuk, Ivan | 2f64e47 | 2014-07-04 15:03:25 +0300 | [diff] [blame] | 405 | err: |
| 406 | /* restore ECC layout */ |
Tom Rini | dada0e3 | 2021-09-12 20:32:24 -0400 | [diff] [blame] | 407 | if (page < KEYSTONE_NAND_MAX_RBL_PAGE) { |
Khoronzhuk, Ivan | 2f64e47 | 2014-07-04 15:03:25 +0300 | [diff] [blame] | 408 | chip->ecc.layout = saved_ecc_layout; |
| 409 | mtd->oobavail = saved_ecc_layout->oobavail; |
| 410 | } |
| 411 | |
| 412 | return ret; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * nand_davinci_read_page_hwecc - hardware ECC based page read function |
| 417 | * @mtd: mtd info structure |
| 418 | * @chip: nand chip info structure |
| 419 | * @buf: buffer to store read data |
| 420 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 421 | * @page: page number to read |
| 422 | * |
| 423 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
| 424 | */ |
| 425 | static int nand_davinci_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
| 426 | uint8_t *buf, int oob_required, int page) |
| 427 | { |
| 428 | int i, eccsize = chip->ecc.size; |
| 429 | int eccbytes = chip->ecc.bytes; |
| 430 | int eccsteps = chip->ecc.steps; |
| 431 | uint32_t *eccpos; |
| 432 | uint8_t *p = buf; |
| 433 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 434 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 435 | struct nand_ecclayout *saved_ecc_layout = chip->ecc.layout; |
| 436 | |
| 437 | /* save current ECC layout and assign Keystone RBL ECC layout */ |
Tom Rini | dada0e3 | 2021-09-12 20:32:24 -0400 | [diff] [blame] | 438 | if (page < KEYSTONE_NAND_MAX_RBL_PAGE) { |
Khoronzhuk, Ivan | 2f64e47 | 2014-07-04 15:03:25 +0300 | [diff] [blame] | 439 | chip->ecc.layout = &nand_keystone_rbl_4bit_layout_oobfirst; |
| 440 | mtd->oobavail = chip->ecc.layout->oobavail; |
| 441 | } |
| 442 | |
| 443 | eccpos = chip->ecc.layout->eccpos; |
| 444 | |
| 445 | /* Read the OOB area first */ |
| 446 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
| 447 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 448 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); |
| 449 | |
| 450 | for (i = 0; i < chip->ecc.total; i++) |
| 451 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
| 452 | |
| 453 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 454 | int stat; |
| 455 | |
| 456 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 457 | chip->read_buf(mtd, p, eccsize); |
| 458 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 459 | |
| 460 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL); |
| 461 | if (stat < 0) |
| 462 | mtd->ecc_stats.failed++; |
| 463 | else |
| 464 | mtd->ecc_stats.corrected += stat; |
| 465 | } |
| 466 | |
| 467 | /* restore ECC layout */ |
Tom Rini | dada0e3 | 2021-09-12 20:32:24 -0400 | [diff] [blame] | 468 | if (page < KEYSTONE_NAND_MAX_RBL_PAGE) { |
Khoronzhuk, Ivan | 2f64e47 | 2014-07-04 15:03:25 +0300 | [diff] [blame] | 469 | chip->ecc.layout = saved_ecc_layout; |
| 470 | mtd->oobavail = saved_ecc_layout->oobavail; |
| 471 | } |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | #endif /* CONFIG_KEYSTONE_RBL_NAND */ |
| 476 | |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 477 | static void nand_davinci_4bit_enable_hwecc(struct mtd_info *mtd, int mode) |
| 478 | { |
| 479 | u32 val; |
| 480 | |
| 481 | switch (mode) { |
| 482 | case NAND_ECC_WRITE: |
| 483 | case NAND_ECC_READ: |
| 484 | /* |
| 485 | * Start a new ECC calculation for reading or writing 512 bytes |
| 486 | * of data. |
| 487 | */ |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 488 | val = __raw_readl(&davinci_emif_regs->nandfcr); |
Nick Thompson | 789c887 | 2009-12-12 12:12:26 -0500 | [diff] [blame] | 489 | val &= ~DAVINCI_NANDFCR_4BIT_ECC_SEL_MASK; |
Tom Rini | b421349 | 2022-11-12 17:36:51 -0500 | [diff] [blame] | 490 | val |= DAVINCI_NANDFCR_NAND_ENABLE(CFG_SYS_NAND_CS); |
| 491 | val |= DAVINCI_NANDFCR_4BIT_ECC_SEL(CFG_SYS_NAND_CS); |
Nick Thompson | 789c887 | 2009-12-12 12:12:26 -0500 | [diff] [blame] | 492 | val |= DAVINCI_NANDFCR_4BIT_ECC_START; |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 493 | __raw_writel(val, &davinci_emif_regs->nandfcr); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 494 | break; |
| 495 | case NAND_ECC_READSYN: |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 496 | val = __raw_readl(&davinci_emif_regs->nand4bitecc[0]); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 497 | break; |
| 498 | default: |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | static u32 nand_davinci_4bit_readecc(struct mtd_info *mtd, unsigned int ecc[4]) |
| 504 | { |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 505 | int i; |
| 506 | |
| 507 | for (i = 0; i < 4; i++) { |
| 508 | ecc[i] = __raw_readl(&davinci_emif_regs->nand4bitecc[i]) & |
| 509 | NAND_4BITECC_MASK; |
| 510 | } |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | static int nand_davinci_4bit_calculate_ecc(struct mtd_info *mtd, |
| 516 | const uint8_t *dat, |
| 517 | uint8_t *ecc_code) |
| 518 | { |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 519 | unsigned int hw_4ecc[4]; |
| 520 | unsigned int i; |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 521 | |
| 522 | nand_davinci_4bit_readecc(mtd, hw_4ecc); |
| 523 | |
| 524 | /*Convert 10 bit ecc value to 8 bit */ |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 525 | for (i = 0; i < 2; i++) { |
| 526 | unsigned int hw_ecc_low = hw_4ecc[i * 2]; |
| 527 | unsigned int hw_ecc_hi = hw_4ecc[(i * 2) + 1]; |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 528 | |
| 529 | /* Take first 8 bits from val1 (count1=0) or val5 (count1=1) */ |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 530 | *ecc_code++ = hw_ecc_low & 0xFF; |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 531 | |
| 532 | /* |
| 533 | * Take 2 bits as LSB bits from val1 (count1=0) or val5 |
| 534 | * (count1=1) and 6 bits from val2 (count1=0) or |
| 535 | * val5 (count1=1) |
| 536 | */ |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 537 | *ecc_code++ = |
| 538 | ((hw_ecc_low >> 8) & 0x3) | ((hw_ecc_low >> 14) & 0xFC); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 539 | |
| 540 | /* |
| 541 | * Take 4 bits from val2 (count1=0) or val5 (count1=1) and |
| 542 | * 4 bits from val3 (count1=0) or val6 (count1=1) |
| 543 | */ |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 544 | *ecc_code++ = |
| 545 | ((hw_ecc_low >> 22) & 0xF) | ((hw_ecc_hi << 4) & 0xF0); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 546 | |
| 547 | /* |
| 548 | * Take 6 bits from val3(count1=0) or val6 (count1=1) and |
| 549 | * 2 bits from val4 (count1=0) or val7 (count1=1) |
| 550 | */ |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 551 | *ecc_code++ = |
| 552 | ((hw_ecc_hi >> 4) & 0x3F) | ((hw_ecc_hi >> 10) & 0xC0); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 553 | |
| 554 | /* Take 8 bits from val4 (count1=0) or val7 (count1=1) */ |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 555 | *ecc_code++ = (hw_ecc_hi >> 18) & 0xFF; |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 556 | } |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 557 | |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 558 | return 0; |
| 559 | } |
| 560 | |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 561 | static int nand_davinci_4bit_correct_data(struct mtd_info *mtd, uint8_t *dat, |
| 562 | uint8_t *read_ecc, uint8_t *calc_ecc) |
| 563 | { |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 564 | int i; |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 565 | unsigned int hw_4ecc[4]; |
| 566 | unsigned int iserror; |
| 567 | unsigned short *ecc16; |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 568 | unsigned int numerrors, erroraddress, errorvalue; |
| 569 | u32 val; |
| 570 | |
| 571 | /* |
| 572 | * Check for an ECC where all bytes are 0xFF. If this is the case, we |
| 573 | * will assume we are looking at an erased page and we should ignore |
| 574 | * the ECC. |
| 575 | */ |
| 576 | for (i = 0; i < 10; i++) { |
| 577 | if (read_ecc[i] != 0xFF) |
| 578 | break; |
| 579 | } |
| 580 | if (i == 10) |
| 581 | return 0; |
| 582 | |
| 583 | /* Convert 8 bit in to 10 bit */ |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 584 | ecc16 = (unsigned short *)&read_ecc[0]; |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 585 | |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 586 | /* |
| 587 | * Write the parity values in the NAND Flash 4-bit ECC Load register. |
| 588 | * Write each parity value one at a time starting from 4bit_ecc_val8 |
| 589 | * to 4bit_ecc_val1. |
| 590 | */ |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 591 | |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 592 | /*Take 2 bits from 8th byte and 8 bits from 9th byte */ |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 593 | __raw_writel(((ecc16[4]) >> 6) & 0x3FF, |
| 594 | &davinci_emif_regs->nand4biteccload); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 595 | |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 596 | /* Take 4 bits from 7th byte and 6 bits from 8th byte */ |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 597 | __raw_writel((((ecc16[3]) >> 12) & 0xF) | ((((ecc16[4])) << 4) & 0x3F0), |
| 598 | &davinci_emif_regs->nand4biteccload); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 599 | |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 600 | /* Take 6 bits from 6th byte and 4 bits from 7th byte */ |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 601 | __raw_writel((ecc16[3] >> 2) & 0x3FF, |
| 602 | &davinci_emif_regs->nand4biteccload); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 603 | |
| 604 | /* Take 8 bits from 5th byte and 2 bits from 6th byte */ |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 605 | __raw_writel(((ecc16[2]) >> 8) | ((((ecc16[3])) << 8) & 0x300), |
| 606 | &davinci_emif_regs->nand4biteccload); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 607 | |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 608 | /*Take 2 bits from 3rd byte and 8 bits from 4th byte */ |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 609 | __raw_writel((((ecc16[1]) >> 14) & 0x3) | ((((ecc16[2])) << 2) & 0x3FC), |
| 610 | &davinci_emif_regs->nand4biteccload); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 611 | |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 612 | /* Take 4 bits form 2nd bytes and 6 bits from 3rd bytes */ |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 613 | __raw_writel(((ecc16[1]) >> 4) & 0x3FF, |
| 614 | &davinci_emif_regs->nand4biteccload); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 615 | |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 616 | /* Take 6 bits from 1st byte and 4 bits from 2nd byte */ |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 617 | __raw_writel((((ecc16[0]) >> 10) & 0x3F) | (((ecc16[1]) << 6) & 0x3C0), |
| 618 | &davinci_emif_regs->nand4biteccload); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 619 | |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 620 | /* Take 10 bits from 0th and 1st bytes */ |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 621 | __raw_writel((ecc16[0]) & 0x3FF, |
| 622 | &davinci_emif_regs->nand4biteccload); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 623 | |
| 624 | /* |
| 625 | * Perform a dummy read to the EMIF Revision Code and Status register. |
| 626 | * This is required to ensure time for syndrome calculation after |
| 627 | * writing the ECC values in previous step. |
| 628 | */ |
| 629 | |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 630 | val = __raw_readl(&davinci_emif_regs->nandfsr); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 631 | |
| 632 | /* |
| 633 | * Read the syndrome from the NAND Flash 4-Bit ECC 1-4 registers. |
| 634 | * A syndrome value of 0 means no bit errors. If the syndrome is |
| 635 | * non-zero then go further otherwise return. |
| 636 | */ |
| 637 | nand_davinci_4bit_readecc(mtd, hw_4ecc); |
| 638 | |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 639 | if (!(hw_4ecc[0] | hw_4ecc[1] | hw_4ecc[2] | hw_4ecc[3])) |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 640 | return 0; |
| 641 | |
| 642 | /* |
| 643 | * Clear any previous address calculation by doing a dummy read of an |
| 644 | * error address register. |
| 645 | */ |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 646 | val = __raw_readl(&davinci_emif_regs->nanderradd1); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 647 | |
| 648 | /* |
| 649 | * Set the addr_calc_st bit(bit no 13) in the NAND Flash Control |
| 650 | * register to 1. |
| 651 | */ |
Ben Gardiner | dfd19ea | 2010-10-14 17:26:17 -0400 | [diff] [blame] | 652 | __raw_writel(DAVINCI_NANDFCR_4BIT_CALC_START, |
| 653 | &davinci_emif_regs->nandfcr); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 654 | |
| 655 | /* |
Wolfram Sang | 328d055 | 2010-09-09 13:54:41 +0200 | [diff] [blame] | 656 | * Wait for the corr_state field (bits 8 to 11) in the |
| 657 | * NAND Flash Status register to be not equal to 0x0, 0x1, 0x2, or 0x3. |
| 658 | * Otherwise ECC calculation has not even begun and the next loop might |
| 659 | * fail because of a false positive! |
| 660 | */ |
| 661 | i = NAND_TIMEOUT; |
| 662 | do { |
| 663 | val = __raw_readl(&davinci_emif_regs->nandfsr); |
| 664 | val &= 0xc00; |
| 665 | i--; |
| 666 | } while ((i > 0) && !val); |
| 667 | |
| 668 | /* |
| 669 | * Wait for the corr_state field (bits 8 to 11) in the |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 670 | * NAND Flash Status register to be equal to 0x0, 0x1, 0x2, or 0x3. |
| 671 | */ |
| 672 | i = NAND_TIMEOUT; |
| 673 | do { |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 674 | val = __raw_readl(&davinci_emif_regs->nandfsr); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 675 | val &= 0xc00; |
| 676 | i--; |
| 677 | } while ((i > 0) && val); |
| 678 | |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 679 | iserror = __raw_readl(&davinci_emif_regs->nandfsr); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 680 | iserror &= EMIF_NANDFSR_ECC_STATE_MASK; |
| 681 | iserror = iserror >> 8; |
| 682 | |
| 683 | /* |
| 684 | * ECC_STATE_TOO_MANY_ERRS (0x1) means errors cannot be |
| 685 | * corrected (five or more errors). The number of errors |
| 686 | * calculated (err_num field) differs from the number of errors |
| 687 | * searched. ECC_STATE_ERR_CORR_COMP_P (0x2) means error |
| 688 | * correction complete (errors on bit 8 or 9). |
| 689 | * ECC_STATE_ERR_CORR_COMP_N (0x3) means error correction |
| 690 | * complete (error exists). |
| 691 | */ |
| 692 | |
| 693 | if (iserror == ECC_STATE_NO_ERR) { |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 694 | val = __raw_readl(&davinci_emif_regs->nanderrval1); |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 695 | return 0; |
| 696 | } else if (iserror == ECC_STATE_TOO_MANY_ERRS) { |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 697 | val = __raw_readl(&davinci_emif_regs->nanderrval1); |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 698 | return -EBADMSG; |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 699 | } |
| 700 | |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 701 | numerrors = ((__raw_readl(&davinci_emif_regs->nandfsr) >> 16) |
| 702 | & 0x3) + 1; |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 703 | |
| 704 | /* Read the error address, error value and correct */ |
| 705 | for (i = 0; i < numerrors; i++) { |
| 706 | if (i > 1) { |
| 707 | erroraddress = |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 708 | ((__raw_readl(&davinci_emif_regs->nanderradd2) >> |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 709 | (16 * (i & 1))) & 0x3FF); |
| 710 | erroraddress = ((512 + 7) - erroraddress); |
| 711 | errorvalue = |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 712 | ((__raw_readl(&davinci_emif_regs->nanderrval2) >> |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 713 | (16 * (i & 1))) & 0xFF); |
| 714 | } else { |
| 715 | erroraddress = |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 716 | ((__raw_readl(&davinci_emif_regs->nanderradd1) >> |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 717 | (16 * (i & 1))) & 0x3FF); |
| 718 | erroraddress = ((512 + 7) - erroraddress); |
| 719 | errorvalue = |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 720 | ((__raw_readl(&davinci_emif_regs->nanderrval1) >> |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 721 | (16 * (i & 1))) & 0xFF); |
| 722 | } |
| 723 | /* xor the corrupt data with error value */ |
| 724 | if (erroraddress < 512) |
| 725 | dat[erroraddress] ^= errorvalue; |
| 726 | } |
| 727 | |
| 728 | return numerrors; |
| 729 | } |
Scott Wood | ce63031 | 2009-09-28 16:33:18 -0500 | [diff] [blame] | 730 | #endif /* CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST */ |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 731 | |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 732 | static int nand_davinci_dev_ready(struct mtd_info *mtd) |
| 733 | { |
Cyril Chemparathy | c4e7224 | 2010-03-17 10:03:10 -0400 | [diff] [blame] | 734 | return __raw_readl(&davinci_emif_regs->nandfsr) & 0x1; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 735 | } |
| 736 | |
Bartosz Golaszewski | 2d4e330 | 2019-07-29 08:58:02 +0200 | [diff] [blame] | 737 | static void davinci_nand_init(struct nand_chip *nand) |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 738 | { |
Khoronzhuk, Ivan | 2f64e47 | 2014-07-04 15:03:25 +0300 | [diff] [blame] | 739 | #if defined CONFIG_KEYSTONE_RBL_NAND |
| 740 | int i; |
| 741 | struct nand_ecclayout *layout; |
| 742 | |
| 743 | layout = &nand_keystone_rbl_4bit_layout_oobfirst; |
| 744 | layout->oobavail = 0; |
Heinrich Schuchardt | 397b062 | 2019-07-30 23:29:21 +0200 | [diff] [blame] | 745 | for (i = 0; i < ARRAY_SIZE(layout->oobfree) && |
| 746 | layout->oobfree[i].length; i++) |
Khoronzhuk, Ivan | 2f64e47 | 2014-07-04 15:03:25 +0300 | [diff] [blame] | 747 | layout->oobavail += layout->oobfree[i].length; |
| 748 | |
| 749 | nand->write_page = nand_davinci_write_page; |
| 750 | nand->ecc.read_page = nand_davinci_read_page_hwecc; |
| 751 | #endif |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 752 | nand->chip_delay = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 753 | #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 754 | nand->bbt_options |= NAND_BBT_USE_FLASH; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 755 | #endif |
Karicheri, Muralidharan | c1dc61b | 2014-04-04 13:16:50 -0400 | [diff] [blame] | 756 | #ifdef CONFIG_SYS_NAND_NO_SUBPAGE_WRITE |
| 757 | nand->options |= NAND_NO_SUBPAGE_WRITE; |
| 758 | #endif |
Fabien Parent | ddc0094 | 2016-11-29 14:31:29 +0100 | [diff] [blame] | 759 | #ifdef CONFIG_SYS_NAND_BUSWIDTH_16BIT |
| 760 | nand->options |= NAND_BUSWIDTH_16; |
| 761 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 762 | #ifdef CONFIG_SYS_NAND_HW_ECC |
William Juul | 9e9c2c1 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 763 | nand->ecc.mode = NAND_ECC_HW; |
William Juul | 9e9c2c1 | 2007-11-09 13:32:30 +0100 | [diff] [blame] | 764 | nand->ecc.size = 512; |
| 765 | nand->ecc.bytes = 3; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 766 | nand->ecc.strength = 1; |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 767 | nand->ecc.calculate = nand_davinci_calculate_ecc; |
| 768 | nand->ecc.correct = nand_davinci_correct_data; |
William Juul | b76ec38 | 2007-11-08 10:39:53 +0100 | [diff] [blame] | 769 | nand->ecc.hwctl = nand_davinci_enable_hwecc; |
Tom Rini | d1286e1 | 2022-11-12 17:36:45 -0500 | [diff] [blame] | 770 | #elif defined(CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST) |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 771 | nand->ecc.mode = NAND_ECC_HW_OOB_FIRST; |
| 772 | nand->ecc.size = 512; |
| 773 | nand->ecc.bytes = 10; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 774 | nand->ecc.strength = 4; |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 775 | nand->ecc.calculate = nand_davinci_4bit_calculate_ecc; |
| 776 | nand->ecc.correct = nand_davinci_4bit_correct_data; |
| 777 | nand->ecc.hwctl = nand_davinci_4bit_enable_hwecc; |
| 778 | nand->ecc.layout = &nand_davinci_4bit_layout_oobfirst; |
Tom Rini | d1286e1 | 2022-11-12 17:36:45 -0500 | [diff] [blame] | 779 | #elif defined(CONFIG_SYS_NAND_SOFT_ECC) |
| 780 | nand->ecc.mode = NAND_ECC_SOFT; |
Sandeep Paulraj | bfeb0fd | 2009-08-18 10:10:42 -0400 | [diff] [blame] | 781 | #endif |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 782 | /* Set address of hardware control function */ |
William Juul | 52c0796 | 2007-10-31 13:53:06 +0100 | [diff] [blame] | 783 | nand->cmd_ctrl = nand_davinci_hwcontrol; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 784 | |
Nick Thompson | 2779472 | 2009-12-16 11:15:58 +0000 | [diff] [blame] | 785 | nand->read_buf = nand_davinci_read_buf; |
| 786 | nand->write_buf = nand_davinci_write_buf; |
| 787 | |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 788 | nand->dev_ready = nand_davinci_dev_ready; |
David Brownell | f4b0b9d | 2009-05-10 15:43:01 -0700 | [diff] [blame] | 789 | } |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 790 | |
Tom Rini | 9837245 | 2021-12-12 22:12:36 -0500 | [diff] [blame] | 791 | #if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT) |
Bartosz Golaszewski | 8a14dfe | 2019-07-29 08:58:03 +0200 | [diff] [blame] | 792 | static int davinci_nand_probe(struct udevice *dev) |
| 793 | { |
| 794 | struct nand_chip *nand = dev_get_priv(dev); |
| 795 | struct mtd_info *mtd = nand_to_mtd(nand); |
| 796 | int ret; |
| 797 | |
Tom Rini | b421349 | 2022-11-12 17:36:51 -0500 | [diff] [blame] | 798 | nand->IO_ADDR_R = (void __iomem *)CFG_SYS_NAND_BASE; |
| 799 | nand->IO_ADDR_W = (void __iomem *)CFG_SYS_NAND_BASE; |
Bartosz Golaszewski | 8a14dfe | 2019-07-29 08:58:03 +0200 | [diff] [blame] | 800 | |
| 801 | davinci_nand_init(nand); |
| 802 | |
| 803 | ret = nand_scan(mtd, CONFIG_SYS_NAND_MAX_CHIPS); |
| 804 | if (ret) |
| 805 | return ret; |
| 806 | |
| 807 | return nand_register(0, mtd); |
| 808 | } |
| 809 | |
| 810 | static const struct udevice_id davinci_nand_ids[] = { |
| 811 | { .compatible = "ti,davinci-nand" }, |
| 812 | { } |
| 813 | }; |
| 814 | |
| 815 | U_BOOT_DRIVER(davinci_nand) = { |
| 816 | .name = "davinci-nand", |
| 817 | .id = UCLASS_MTD, |
| 818 | .of_match = davinci_nand_ids, |
| 819 | .probe = davinci_nand_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 820 | .priv_auto = sizeof(struct nand_chip), |
Bartosz Golaszewski | 8a14dfe | 2019-07-29 08:58:03 +0200 | [diff] [blame] | 821 | }; |
| 822 | |
| 823 | void board_nand_init(void) |
| 824 | { |
| 825 | struct udevice *dev; |
| 826 | int ret; |
David Brownell | f4b0b9d | 2009-05-10 15:43:01 -0700 | [diff] [blame] | 827 | |
Bartosz Golaszewski | 8a14dfe | 2019-07-29 08:58:03 +0200 | [diff] [blame] | 828 | ret = uclass_get_device_by_driver(UCLASS_MTD, |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 829 | DM_DRIVER_GET(davinci_nand), &dev); |
Bartosz Golaszewski | 8a14dfe | 2019-07-29 08:58:03 +0200 | [diff] [blame] | 830 | if (ret && ret != -ENODEV) |
| 831 | pr_err("Failed to initialize %s: %d\n", dev->name, ret); |
| 832 | } |
| 833 | #else |
| 834 | int board_nand_init(struct nand_chip *chip) __attribute__((weak)); |
David Brownell | f4b0b9d | 2009-05-10 15:43:01 -0700 | [diff] [blame] | 835 | int board_nand_init(struct nand_chip *chip) |
| 836 | { |
| 837 | davinci_nand_init(chip); |
| 838 | return 0; |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 839 | } |
Bartosz Golaszewski | 8a14dfe | 2019-07-29 08:58:03 +0200 | [diff] [blame] | 840 | #endif /* CONFIG_SYS_NAND_SELF_INIT */ |