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