Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007-2008 |
Stelian Pop | 5ee0c7f | 2011-11-01 00:00:39 +0100 | [diff] [blame] | 3 | * Stelian Pop <stelian@popies.net> |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 4 | * Lead Tech Design <www.leadtechdesign.com> |
| 5 | * |
| 6 | * (C) Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas |
| 7 | * |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 8 | * Add Programmable Multibit ECC support for various AT91 SoC |
| 9 | * (C) Copyright 2012 ATMEL, Hong Xu |
| 10 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 11 | * SPDX-License-Identifier: GPL-2.0+ |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <common.h> |
Jean-Christophe PLAGNIOL-VILLARD | c9539ba | 2009-03-22 10:22:34 +0100 | [diff] [blame] | 15 | #include <asm/arch/hardware.h> |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 16 | #include <asm/arch/gpio.h> |
| 17 | #include <asm/arch/at91_pio.h> |
| 18 | |
Wu, Josh | 4e87b315 | 2013-07-03 11:11:48 +0800 | [diff] [blame] | 19 | #include <malloc.h> |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 20 | #include <nand.h> |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 21 | #include <watchdog.h> |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 22 | |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 23 | #ifdef CONFIG_ATMEL_NAND_HWECC |
| 24 | |
| 25 | /* Register access macros */ |
| 26 | #define ecc_readl(add, reg) \ |
| 27 | readl(AT91_BASE_SYS + add + ATMEL_ECC_##reg) |
| 28 | #define ecc_writel(add, reg, value) \ |
| 29 | writel((value), AT91_BASE_SYS + add + ATMEL_ECC_##reg) |
| 30 | |
| 31 | #include "atmel_nand_ecc.h" /* Hardware ECC registers */ |
| 32 | |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 33 | #ifdef CONFIG_ATMEL_NAND_HW_PMECC |
| 34 | |
| 35 | struct atmel_nand_host { |
| 36 | struct pmecc_regs __iomem *pmecc; |
| 37 | struct pmecc_errloc_regs __iomem *pmerrloc; |
| 38 | void __iomem *pmecc_rom_base; |
| 39 | |
| 40 | u8 pmecc_corr_cap; |
| 41 | u16 pmecc_sector_size; |
| 42 | u32 pmecc_index_table_offset; |
| 43 | |
| 44 | int pmecc_bytes_per_sector; |
| 45 | int pmecc_sector_number; |
| 46 | int pmecc_degree; /* Degree of remainders */ |
| 47 | int pmecc_cw_len; /* Length of codeword */ |
| 48 | |
| 49 | /* lookup table for alpha_to and index_of */ |
| 50 | void __iomem *pmecc_alpha_to; |
| 51 | void __iomem *pmecc_index_of; |
| 52 | |
| 53 | /* data for pmecc computation */ |
Wu, Josh | 4e87b315 | 2013-07-03 11:11:48 +0800 | [diff] [blame] | 54 | int16_t *pmecc_smu; |
| 55 | int16_t *pmecc_partial_syn; |
| 56 | int16_t *pmecc_si; |
| 57 | int16_t *pmecc_lmu; /* polynomal order */ |
| 58 | int *pmecc_mu; |
| 59 | int *pmecc_dmu; |
| 60 | int *pmecc_delta; |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | static struct atmel_nand_host pmecc_host; |
| 64 | static struct nand_ecclayout atmel_pmecc_oobinfo; |
| 65 | |
| 66 | /* |
| 67 | * Return number of ecc bytes per sector according to sector size and |
| 68 | * correction capability |
| 69 | * |
| 70 | * Following table shows what at91 PMECC supported: |
| 71 | * Correction Capability Sector_512_bytes Sector_1024_bytes |
| 72 | * ===================== ================ ================= |
| 73 | * 2-bits 4-bytes 4-bytes |
| 74 | * 4-bits 7-bytes 7-bytes |
| 75 | * 8-bits 13-bytes 14-bytes |
| 76 | * 12-bits 20-bytes 21-bytes |
| 77 | * 24-bits 39-bytes 42-bytes |
| 78 | */ |
| 79 | static int pmecc_get_ecc_bytes(int cap, int sector_size) |
| 80 | { |
| 81 | int m = 12 + sector_size / 512; |
| 82 | return (m * cap + 7) / 8; |
| 83 | } |
| 84 | |
| 85 | static void pmecc_config_ecc_layout(struct nand_ecclayout *layout, |
| 86 | int oobsize, int ecc_len) |
| 87 | { |
| 88 | int i; |
| 89 | |
| 90 | layout->eccbytes = ecc_len; |
| 91 | |
| 92 | /* ECC will occupy the last ecc_len bytes continuously */ |
| 93 | for (i = 0; i < ecc_len; i++) |
| 94 | layout->eccpos[i] = oobsize - ecc_len + i; |
| 95 | |
| 96 | layout->oobfree[0].offset = 2; |
| 97 | layout->oobfree[0].length = |
| 98 | oobsize - ecc_len - layout->oobfree[0].offset; |
| 99 | } |
| 100 | |
| 101 | static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host) |
| 102 | { |
| 103 | int table_size; |
| 104 | |
| 105 | table_size = host->pmecc_sector_size == 512 ? |
| 106 | PMECC_INDEX_TABLE_SIZE_512 : PMECC_INDEX_TABLE_SIZE_1024; |
| 107 | |
| 108 | /* the ALPHA lookup table is right behind the INDEX lookup table. */ |
| 109 | return host->pmecc_rom_base + host->pmecc_index_table_offset + |
| 110 | table_size * sizeof(int16_t); |
| 111 | } |
| 112 | |
Wu, Josh | 4e87b315 | 2013-07-03 11:11:48 +0800 | [diff] [blame] | 113 | static void pmecc_data_free(struct atmel_nand_host *host) |
| 114 | { |
| 115 | free(host->pmecc_partial_syn); |
| 116 | free(host->pmecc_si); |
| 117 | free(host->pmecc_lmu); |
| 118 | free(host->pmecc_smu); |
| 119 | free(host->pmecc_mu); |
| 120 | free(host->pmecc_dmu); |
| 121 | free(host->pmecc_delta); |
| 122 | } |
| 123 | |
| 124 | static int pmecc_data_alloc(struct atmel_nand_host *host) |
| 125 | { |
| 126 | const int cap = host->pmecc_corr_cap; |
| 127 | int size; |
| 128 | |
| 129 | size = (2 * cap + 1) * sizeof(int16_t); |
| 130 | host->pmecc_partial_syn = malloc(size); |
| 131 | host->pmecc_si = malloc(size); |
| 132 | host->pmecc_lmu = malloc((cap + 1) * sizeof(int16_t)); |
| 133 | host->pmecc_smu = malloc((cap + 2) * size); |
| 134 | |
| 135 | size = (cap + 1) * sizeof(int); |
| 136 | host->pmecc_mu = malloc(size); |
| 137 | host->pmecc_dmu = malloc(size); |
| 138 | host->pmecc_delta = malloc(size); |
| 139 | |
| 140 | if (host->pmecc_partial_syn && |
| 141 | host->pmecc_si && |
| 142 | host->pmecc_lmu && |
| 143 | host->pmecc_smu && |
| 144 | host->pmecc_mu && |
| 145 | host->pmecc_dmu && |
| 146 | host->pmecc_delta) |
| 147 | return 0; |
| 148 | |
| 149 | /* error happened */ |
| 150 | pmecc_data_free(host); |
| 151 | return -ENOMEM; |
| 152 | |
| 153 | } |
| 154 | |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 155 | static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector) |
| 156 | { |
| 157 | struct nand_chip *nand_chip = mtd->priv; |
| 158 | struct atmel_nand_host *host = nand_chip->priv; |
| 159 | int i; |
| 160 | uint32_t value; |
| 161 | |
| 162 | /* Fill odd syndromes */ |
| 163 | for (i = 0; i < host->pmecc_corr_cap; i++) { |
| 164 | value = readl(&host->pmecc->rem_port[sector].rem[i / 2]); |
| 165 | if (i & 1) |
| 166 | value >>= 16; |
| 167 | value &= 0xffff; |
| 168 | host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | static void pmecc_substitute(struct mtd_info *mtd) |
| 173 | { |
| 174 | struct nand_chip *nand_chip = mtd->priv; |
| 175 | struct atmel_nand_host *host = nand_chip->priv; |
| 176 | int16_t __iomem *alpha_to = host->pmecc_alpha_to; |
| 177 | int16_t __iomem *index_of = host->pmecc_index_of; |
| 178 | int16_t *partial_syn = host->pmecc_partial_syn; |
| 179 | const int cap = host->pmecc_corr_cap; |
| 180 | int16_t *si; |
| 181 | int i, j; |
| 182 | |
| 183 | /* si[] is a table that holds the current syndrome value, |
| 184 | * an element of that table belongs to the field |
| 185 | */ |
| 186 | si = host->pmecc_si; |
| 187 | |
| 188 | memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1)); |
| 189 | |
| 190 | /* Computation 2t syndromes based on S(x) */ |
| 191 | /* Odd syndromes */ |
| 192 | for (i = 1; i < 2 * cap; i += 2) { |
| 193 | for (j = 0; j < host->pmecc_degree; j++) { |
| 194 | if (partial_syn[i] & (0x1 << j)) |
| 195 | si[i] = readw(alpha_to + i * j) ^ si[i]; |
| 196 | } |
| 197 | } |
| 198 | /* Even syndrome = (Odd syndrome) ** 2 */ |
| 199 | for (i = 2, j = 1; j <= cap; i = ++j << 1) { |
| 200 | if (si[j] == 0) { |
| 201 | si[i] = 0; |
| 202 | } else { |
| 203 | int16_t tmp; |
| 204 | |
| 205 | tmp = readw(index_of + si[j]); |
| 206 | tmp = (tmp * 2) % host->pmecc_cw_len; |
| 207 | si[i] = readw(alpha_to + tmp); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * This function defines a Berlekamp iterative procedure for |
| 214 | * finding the value of the error location polynomial. |
| 215 | * The input is si[], initialize by pmecc_substitute(). |
| 216 | * The output is smu[][]. |
| 217 | * |
| 218 | * This function is written according to chip datasheet Chapter: |
| 219 | * Find the Error Location Polynomial Sigma(x) of Section: |
| 220 | * Programmable Multibit ECC Control (PMECC). |
| 221 | */ |
| 222 | static void pmecc_get_sigma(struct mtd_info *mtd) |
| 223 | { |
| 224 | struct nand_chip *nand_chip = mtd->priv; |
| 225 | struct atmel_nand_host *host = nand_chip->priv; |
| 226 | |
| 227 | int16_t *lmu = host->pmecc_lmu; |
| 228 | int16_t *si = host->pmecc_si; |
| 229 | int *mu = host->pmecc_mu; |
| 230 | int *dmu = host->pmecc_dmu; /* Discrepancy */ |
| 231 | int *delta = host->pmecc_delta; /* Delta order */ |
| 232 | int cw_len = host->pmecc_cw_len; |
| 233 | const int16_t cap = host->pmecc_corr_cap; |
| 234 | const int num = 2 * cap + 1; |
| 235 | int16_t __iomem *index_of = host->pmecc_index_of; |
| 236 | int16_t __iomem *alpha_to = host->pmecc_alpha_to; |
| 237 | int i, j, k; |
| 238 | uint32_t dmu_0_count, tmp; |
| 239 | int16_t *smu = host->pmecc_smu; |
| 240 | |
| 241 | /* index of largest delta */ |
| 242 | int ro; |
| 243 | int largest; |
| 244 | int diff; |
| 245 | |
| 246 | /* Init the Sigma(x) */ |
| 247 | memset(smu, 0, sizeof(int16_t) * ARRAY_SIZE(smu)); |
| 248 | |
| 249 | dmu_0_count = 0; |
| 250 | |
| 251 | /* First Row */ |
| 252 | |
| 253 | /* Mu */ |
| 254 | mu[0] = -1; |
| 255 | |
| 256 | smu[0] = 1; |
| 257 | |
| 258 | /* discrepancy set to 1 */ |
| 259 | dmu[0] = 1; |
| 260 | /* polynom order set to 0 */ |
| 261 | lmu[0] = 0; |
| 262 | /* delta[0] = (mu[0] * 2 - lmu[0]) >> 1; */ |
| 263 | delta[0] = -1; |
| 264 | |
| 265 | /* Second Row */ |
| 266 | |
| 267 | /* Mu */ |
| 268 | mu[1] = 0; |
| 269 | /* Sigma(x) set to 1 */ |
| 270 | smu[num] = 1; |
| 271 | |
| 272 | /* discrepancy set to S1 */ |
| 273 | dmu[1] = si[1]; |
| 274 | |
| 275 | /* polynom order set to 0 */ |
| 276 | lmu[1] = 0; |
| 277 | |
| 278 | /* delta[1] = (mu[1] * 2 - lmu[1]) >> 1; */ |
| 279 | delta[1] = 0; |
| 280 | |
| 281 | for (i = 1; i <= cap; i++) { |
| 282 | mu[i + 1] = i << 1; |
| 283 | /* Begin Computing Sigma (Mu+1) and L(mu) */ |
| 284 | /* check if discrepancy is set to 0 */ |
| 285 | if (dmu[i] == 0) { |
| 286 | dmu_0_count++; |
| 287 | |
| 288 | tmp = ((cap - (lmu[i] >> 1) - 1) / 2); |
| 289 | if ((cap - (lmu[i] >> 1) - 1) & 0x1) |
| 290 | tmp += 2; |
| 291 | else |
| 292 | tmp += 1; |
| 293 | |
| 294 | if (dmu_0_count == tmp) { |
| 295 | for (j = 0; j <= (lmu[i] >> 1) + 1; j++) |
| 296 | smu[(cap + 1) * num + j] = |
| 297 | smu[i * num + j]; |
| 298 | |
| 299 | lmu[cap + 1] = lmu[i]; |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | /* copy polynom */ |
| 304 | for (j = 0; j <= lmu[i] >> 1; j++) |
| 305 | smu[(i + 1) * num + j] = smu[i * num + j]; |
| 306 | |
| 307 | /* copy previous polynom order to the next */ |
| 308 | lmu[i + 1] = lmu[i]; |
| 309 | } else { |
| 310 | ro = 0; |
| 311 | largest = -1; |
| 312 | /* find largest delta with dmu != 0 */ |
| 313 | for (j = 0; j < i; j++) { |
| 314 | if ((dmu[j]) && (delta[j] > largest)) { |
| 315 | largest = delta[j]; |
| 316 | ro = j; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /* compute difference */ |
| 321 | diff = (mu[i] - mu[ro]); |
| 322 | |
| 323 | /* Compute degree of the new smu polynomial */ |
| 324 | if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff)) |
| 325 | lmu[i + 1] = lmu[i]; |
| 326 | else |
| 327 | lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2; |
| 328 | |
| 329 | /* Init smu[i+1] with 0 */ |
| 330 | for (k = 0; k < num; k++) |
| 331 | smu[(i + 1) * num + k] = 0; |
| 332 | |
| 333 | /* Compute smu[i+1] */ |
| 334 | for (k = 0; k <= lmu[ro] >> 1; k++) { |
| 335 | int16_t a, b, c; |
| 336 | |
| 337 | if (!(smu[ro * num + k] && dmu[i])) |
| 338 | continue; |
| 339 | a = readw(index_of + dmu[i]); |
| 340 | b = readw(index_of + dmu[ro]); |
| 341 | c = readw(index_of + smu[ro * num + k]); |
| 342 | tmp = a + (cw_len - b) + c; |
| 343 | a = readw(alpha_to + tmp % cw_len); |
| 344 | smu[(i + 1) * num + (k + diff)] = a; |
| 345 | } |
| 346 | |
| 347 | for (k = 0; k <= lmu[i] >> 1; k++) |
| 348 | smu[(i + 1) * num + k] ^= smu[i * num + k]; |
| 349 | } |
| 350 | |
| 351 | /* End Computing Sigma (Mu+1) and L(mu) */ |
| 352 | /* In either case compute delta */ |
| 353 | delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1; |
| 354 | |
| 355 | /* Do not compute discrepancy for the last iteration */ |
| 356 | if (i >= cap) |
| 357 | continue; |
| 358 | |
| 359 | for (k = 0; k <= (lmu[i + 1] >> 1); k++) { |
| 360 | tmp = 2 * (i - 1); |
| 361 | if (k == 0) { |
| 362 | dmu[i + 1] = si[tmp + 3]; |
| 363 | } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) { |
| 364 | int16_t a, b, c; |
| 365 | a = readw(index_of + |
| 366 | smu[(i + 1) * num + k]); |
| 367 | b = si[2 * (i - 1) + 3 - k]; |
| 368 | c = readw(index_of + b); |
| 369 | tmp = a + c; |
| 370 | tmp %= cw_len; |
| 371 | dmu[i + 1] = readw(alpha_to + tmp) ^ |
| 372 | dmu[i + 1]; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | static int pmecc_err_location(struct mtd_info *mtd) |
| 379 | { |
| 380 | struct nand_chip *nand_chip = mtd->priv; |
| 381 | struct atmel_nand_host *host = nand_chip->priv; |
| 382 | const int cap = host->pmecc_corr_cap; |
| 383 | const int num = 2 * cap + 1; |
| 384 | int sector_size = host->pmecc_sector_size; |
| 385 | int err_nbr = 0; /* number of error */ |
| 386 | int roots_nbr; /* number of roots */ |
| 387 | int i; |
| 388 | uint32_t val; |
| 389 | int16_t *smu = host->pmecc_smu; |
| 390 | int timeout = PMECC_MAX_TIMEOUT_US; |
| 391 | |
| 392 | writel(PMERRLOC_DISABLE, &host->pmerrloc->eldis); |
| 393 | |
| 394 | for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) { |
| 395 | writel(smu[(cap + 1) * num + i], &host->pmerrloc->sigma[i]); |
| 396 | err_nbr++; |
| 397 | } |
| 398 | |
| 399 | val = PMERRLOC_ELCFG_NUM_ERRORS(err_nbr - 1); |
| 400 | if (sector_size == 1024) |
| 401 | val |= PMERRLOC_ELCFG_SECTOR_1024; |
| 402 | |
| 403 | writel(val, &host->pmerrloc->elcfg); |
| 404 | writel(sector_size * 8 + host->pmecc_degree * cap, |
| 405 | &host->pmerrloc->elen); |
| 406 | |
| 407 | while (--timeout) { |
| 408 | if (readl(&host->pmerrloc->elisr) & PMERRLOC_CALC_DONE) |
| 409 | break; |
| 410 | WATCHDOG_RESET(); |
| 411 | udelay(1); |
| 412 | } |
| 413 | |
| 414 | if (!timeout) { |
| 415 | printk(KERN_ERR "atmel_nand : Timeout to calculate PMECC error location\n"); |
| 416 | return -1; |
| 417 | } |
| 418 | |
| 419 | roots_nbr = (readl(&host->pmerrloc->elisr) & PMERRLOC_ERR_NUM_MASK) |
| 420 | >> 8; |
| 421 | /* Number of roots == degree of smu hence <= cap */ |
| 422 | if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1) |
| 423 | return err_nbr - 1; |
| 424 | |
| 425 | /* Number of roots does not match the degree of smu |
| 426 | * unable to correct error */ |
| 427 | return -1; |
| 428 | } |
| 429 | |
| 430 | static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc, |
| 431 | int sector_num, int extra_bytes, int err_nbr) |
| 432 | { |
| 433 | struct nand_chip *nand_chip = mtd->priv; |
| 434 | struct atmel_nand_host *host = nand_chip->priv; |
| 435 | int i = 0; |
| 436 | int byte_pos, bit_pos, sector_size, pos; |
| 437 | uint32_t tmp; |
| 438 | uint8_t err_byte; |
| 439 | |
| 440 | sector_size = host->pmecc_sector_size; |
| 441 | |
| 442 | while (err_nbr) { |
| 443 | tmp = readl(&host->pmerrloc->el[i]) - 1; |
| 444 | byte_pos = tmp / 8; |
| 445 | bit_pos = tmp % 8; |
| 446 | |
| 447 | if (byte_pos >= (sector_size + extra_bytes)) |
| 448 | BUG(); /* should never happen */ |
| 449 | |
| 450 | if (byte_pos < sector_size) { |
| 451 | err_byte = *(buf + byte_pos); |
| 452 | *(buf + byte_pos) ^= (1 << bit_pos); |
| 453 | |
| 454 | pos = sector_num * host->pmecc_sector_size + byte_pos; |
| 455 | printk(KERN_INFO "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n", |
| 456 | pos, bit_pos, err_byte, *(buf + byte_pos)); |
| 457 | } else { |
| 458 | /* Bit flip in OOB area */ |
| 459 | tmp = sector_num * host->pmecc_bytes_per_sector |
| 460 | + (byte_pos - sector_size); |
| 461 | err_byte = ecc[tmp]; |
| 462 | ecc[tmp] ^= (1 << bit_pos); |
| 463 | |
| 464 | pos = tmp + nand_chip->ecc.layout->eccpos[0]; |
| 465 | printk(KERN_INFO "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n", |
| 466 | pos, bit_pos, err_byte, ecc[tmp]); |
| 467 | } |
| 468 | |
| 469 | i++; |
| 470 | err_nbr--; |
| 471 | } |
| 472 | |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf, |
| 477 | u8 *ecc) |
| 478 | { |
| 479 | struct nand_chip *nand_chip = mtd->priv; |
| 480 | struct atmel_nand_host *host = nand_chip->priv; |
| 481 | int i, err_nbr, eccbytes; |
| 482 | uint8_t *buf_pos; |
| 483 | |
| 484 | eccbytes = nand_chip->ecc.bytes; |
| 485 | for (i = 0; i < eccbytes; i++) |
| 486 | if (ecc[i] != 0xff) |
| 487 | goto normal_check; |
| 488 | /* Erased page, return OK */ |
| 489 | return 0; |
| 490 | |
| 491 | normal_check: |
| 492 | for (i = 0; i < host->pmecc_sector_number; i++) { |
| 493 | err_nbr = 0; |
| 494 | if (pmecc_stat & 0x1) { |
| 495 | buf_pos = buf + i * host->pmecc_sector_size; |
| 496 | |
| 497 | pmecc_gen_syndrome(mtd, i); |
| 498 | pmecc_substitute(mtd); |
| 499 | pmecc_get_sigma(mtd); |
| 500 | |
| 501 | err_nbr = pmecc_err_location(mtd); |
| 502 | if (err_nbr == -1) { |
| 503 | printk(KERN_ERR "PMECC: Too many errors\n"); |
| 504 | mtd->ecc_stats.failed++; |
| 505 | return -EIO; |
| 506 | } else { |
| 507 | pmecc_correct_data(mtd, buf_pos, ecc, i, |
| 508 | host->pmecc_bytes_per_sector, err_nbr); |
| 509 | mtd->ecc_stats.corrected += err_nbr; |
| 510 | } |
| 511 | } |
| 512 | pmecc_stat >>= 1; |
| 513 | } |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | static int atmel_nand_pmecc_read_page(struct mtd_info *mtd, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 519 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 520 | { |
| 521 | struct atmel_nand_host *host = chip->priv; |
| 522 | int eccsize = chip->ecc.size; |
| 523 | uint8_t *oob = chip->oob_poi; |
| 524 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 525 | uint32_t stat; |
| 526 | int timeout = PMECC_MAX_TIMEOUT_US; |
| 527 | |
| 528 | pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST); |
| 529 | pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE); |
| 530 | pmecc_writel(host->pmecc, cfg, ((pmecc_readl(host->pmecc, cfg)) |
| 531 | & ~PMECC_CFG_WRITE_OP) | PMECC_CFG_AUTO_ENABLE); |
| 532 | |
| 533 | pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE); |
| 534 | pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DATA); |
| 535 | |
| 536 | chip->read_buf(mtd, buf, eccsize); |
| 537 | chip->read_buf(mtd, oob, mtd->oobsize); |
| 538 | |
| 539 | while (--timeout) { |
| 540 | if (!(pmecc_readl(host->pmecc, sr) & PMECC_SR_BUSY)) |
| 541 | break; |
| 542 | WATCHDOG_RESET(); |
| 543 | udelay(1); |
| 544 | } |
| 545 | |
| 546 | if (!timeout) { |
| 547 | printk(KERN_ERR "atmel_nand : Timeout to read PMECC page\n"); |
| 548 | return -1; |
| 549 | } |
| 550 | |
| 551 | stat = pmecc_readl(host->pmecc, isr); |
| 552 | if (stat != 0) |
| 553 | if (pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]) != 0) |
| 554 | return -EIO; |
| 555 | |
| 556 | return 0; |
| 557 | } |
| 558 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 559 | static int atmel_nand_pmecc_write_page(struct mtd_info *mtd, |
| 560 | struct nand_chip *chip, const uint8_t *buf, |
| 561 | int oob_required) |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 562 | { |
| 563 | struct atmel_nand_host *host = chip->priv; |
| 564 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 565 | int i, j; |
| 566 | int timeout = PMECC_MAX_TIMEOUT_US; |
| 567 | |
| 568 | pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST); |
| 569 | pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE); |
| 570 | |
| 571 | pmecc_writel(host->pmecc, cfg, (pmecc_readl(host->pmecc, cfg) | |
| 572 | PMECC_CFG_WRITE_OP) & ~PMECC_CFG_AUTO_ENABLE); |
| 573 | |
| 574 | pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE); |
| 575 | pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DATA); |
| 576 | |
| 577 | chip->write_buf(mtd, (u8 *)buf, mtd->writesize); |
| 578 | |
| 579 | while (--timeout) { |
| 580 | if (!(pmecc_readl(host->pmecc, sr) & PMECC_SR_BUSY)) |
| 581 | break; |
| 582 | WATCHDOG_RESET(); |
| 583 | udelay(1); |
| 584 | } |
| 585 | |
| 586 | if (!timeout) { |
| 587 | printk(KERN_ERR "atmel_nand : Timeout to read PMECC status, fail to write PMECC in oob\n"); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 588 | goto out; |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | for (i = 0; i < host->pmecc_sector_number; i++) { |
| 592 | for (j = 0; j < host->pmecc_bytes_per_sector; j++) { |
| 593 | int pos; |
| 594 | |
| 595 | pos = i * host->pmecc_bytes_per_sector + j; |
| 596 | chip->oob_poi[eccpos[pos]] = |
| 597 | readb(&host->pmecc->ecc_port[i].ecc[j]); |
| 598 | } |
| 599 | } |
| 600 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 601 | out: |
| 602 | return 0; |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static void atmel_pmecc_core_init(struct mtd_info *mtd) |
| 606 | { |
| 607 | struct nand_chip *nand_chip = mtd->priv; |
| 608 | struct atmel_nand_host *host = nand_chip->priv; |
| 609 | uint32_t val = 0; |
| 610 | struct nand_ecclayout *ecc_layout; |
| 611 | |
| 612 | pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST); |
| 613 | pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE); |
| 614 | |
| 615 | switch (host->pmecc_corr_cap) { |
| 616 | case 2: |
| 617 | val = PMECC_CFG_BCH_ERR2; |
| 618 | break; |
| 619 | case 4: |
| 620 | val = PMECC_CFG_BCH_ERR4; |
| 621 | break; |
| 622 | case 8: |
| 623 | val = PMECC_CFG_BCH_ERR8; |
| 624 | break; |
| 625 | case 12: |
| 626 | val = PMECC_CFG_BCH_ERR12; |
| 627 | break; |
| 628 | case 24: |
| 629 | val = PMECC_CFG_BCH_ERR24; |
| 630 | break; |
| 631 | } |
| 632 | |
| 633 | if (host->pmecc_sector_size == 512) |
| 634 | val |= PMECC_CFG_SECTOR512; |
| 635 | else if (host->pmecc_sector_size == 1024) |
| 636 | val |= PMECC_CFG_SECTOR1024; |
| 637 | |
| 638 | switch (host->pmecc_sector_number) { |
| 639 | case 1: |
| 640 | val |= PMECC_CFG_PAGE_1SECTOR; |
| 641 | break; |
| 642 | case 2: |
| 643 | val |= PMECC_CFG_PAGE_2SECTORS; |
| 644 | break; |
| 645 | case 4: |
| 646 | val |= PMECC_CFG_PAGE_4SECTORS; |
| 647 | break; |
| 648 | case 8: |
| 649 | val |= PMECC_CFG_PAGE_8SECTORS; |
| 650 | break; |
| 651 | } |
| 652 | |
| 653 | val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE |
| 654 | | PMECC_CFG_AUTO_DISABLE); |
| 655 | pmecc_writel(host->pmecc, cfg, val); |
| 656 | |
| 657 | ecc_layout = nand_chip->ecc.layout; |
| 658 | pmecc_writel(host->pmecc, sarea, mtd->oobsize - 1); |
| 659 | pmecc_writel(host->pmecc, saddr, ecc_layout->eccpos[0]); |
| 660 | pmecc_writel(host->pmecc, eaddr, |
| 661 | ecc_layout->eccpos[ecc_layout->eccbytes - 1]); |
| 662 | /* See datasheet about PMECC Clock Control Register */ |
| 663 | pmecc_writel(host->pmecc, clk, PMECC_CLK_133MHZ); |
| 664 | pmecc_writel(host->pmecc, idr, 0xff); |
| 665 | pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE); |
| 666 | } |
| 667 | |
Wu, Josh | 3b4c7f6 | 2013-07-04 15:36:23 +0800 | [diff] [blame] | 668 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
| 669 | /* |
| 670 | * get_onfi_ecc_param - Get ECC requirement from ONFI parameters |
| 671 | * @ecc_bits: store the ONFI ECC correct bits capbility |
| 672 | * @sector_size: in how many bytes that ONFI require to correct @ecc_bits |
| 673 | * |
| 674 | * Returns -1 if ONFI parameters is not supported. In this case @ecc_bits, |
| 675 | * @sector_size are initialize to 0. |
| 676 | * Return 0 if success to get the ECC requirement. |
| 677 | */ |
| 678 | static int get_onfi_ecc_param(struct nand_chip *chip, |
| 679 | int *ecc_bits, int *sector_size) |
| 680 | { |
| 681 | *ecc_bits = *sector_size = 0; |
| 682 | |
| 683 | if (chip->onfi_params.ecc_bits == 0xff) |
| 684 | /* TODO: the sector_size and ecc_bits need to be find in |
| 685 | * extended ecc parameter, currently we don't support it. |
| 686 | */ |
| 687 | return -1; |
| 688 | |
| 689 | *ecc_bits = chip->onfi_params.ecc_bits; |
| 690 | |
| 691 | /* The default sector size (ecc codeword size) is 512 */ |
| 692 | *sector_size = 512; |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * pmecc_choose_ecc - Get ecc requirement from ONFI parameters. If |
| 699 | * pmecc_corr_cap or pmecc_sector_size is 0, then set it as |
| 700 | * ONFI ECC parameters. |
| 701 | * @host: point to an atmel_nand_host structure. |
| 702 | * if host->pmecc_corr_cap is 0 then set it as the ONFI ecc_bits. |
| 703 | * if host->pmecc_sector_size is 0 then set it as the ONFI sector_size. |
| 704 | * @chip: point to an nand_chip structure. |
| 705 | * @cap: store the ONFI ECC correct bits capbility |
| 706 | * @sector_size: in how many bytes that ONFI require to correct @ecc_bits |
| 707 | * |
| 708 | * Return 0 if success. otherwise return the error code. |
| 709 | */ |
| 710 | static int pmecc_choose_ecc(struct atmel_nand_host *host, |
| 711 | struct nand_chip *chip, |
| 712 | int *cap, int *sector_size) |
| 713 | { |
| 714 | /* Get ECC requirement from ONFI parameters */ |
| 715 | *cap = *sector_size = 0; |
| 716 | if (chip->onfi_version) { |
| 717 | if (!get_onfi_ecc_param(chip, cap, sector_size)) { |
| 718 | MTDDEBUG(MTD_DEBUG_LEVEL1, "ONFI params, minimum required ECC: %d bits in %d bytes\n", |
| 719 | *cap, *sector_size); |
| 720 | } else { |
| 721 | dev_info(host->dev, "NAND chip ECC reqirement is in Extended ONFI parameter, we don't support yet.\n"); |
| 722 | } |
| 723 | } else { |
| 724 | dev_info(host->dev, "NAND chip is not ONFI compliant, assume ecc_bits is 2 in 512 bytes"); |
| 725 | } |
| 726 | if (*cap == 0 && *sector_size == 0) { |
| 727 | /* Non-ONFI compliant or use extended ONFI parameters */ |
| 728 | *cap = 2; |
| 729 | *sector_size = 512; |
| 730 | } |
| 731 | |
| 732 | /* If head file doesn't specify then use the one in ONFI parameters */ |
| 733 | if (host->pmecc_corr_cap == 0) { |
| 734 | /* use the most fitable ecc bits (the near bigger one ) */ |
| 735 | if (*cap <= 2) |
| 736 | host->pmecc_corr_cap = 2; |
| 737 | else if (*cap <= 4) |
| 738 | host->pmecc_corr_cap = 4; |
| 739 | else if (*cap <= 8) |
| 740 | host->pmecc_corr_cap = 8; |
| 741 | else if (*cap <= 12) |
| 742 | host->pmecc_corr_cap = 12; |
| 743 | else if (*cap <= 24) |
| 744 | host->pmecc_corr_cap = 24; |
| 745 | else |
| 746 | return -EINVAL; |
| 747 | } |
| 748 | if (host->pmecc_sector_size == 0) { |
| 749 | /* use the most fitable sector size (the near smaller one ) */ |
| 750 | if (*sector_size >= 1024) |
| 751 | host->pmecc_sector_size = 1024; |
| 752 | else if (*sector_size >= 512) |
| 753 | host->pmecc_sector_size = 512; |
| 754 | else |
| 755 | return -EINVAL; |
| 756 | } |
| 757 | return 0; |
| 758 | } |
| 759 | #endif |
| 760 | |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 761 | static int atmel_pmecc_nand_init_params(struct nand_chip *nand, |
| 762 | struct mtd_info *mtd) |
| 763 | { |
| 764 | struct atmel_nand_host *host; |
| 765 | int cap, sector_size; |
| 766 | |
| 767 | host = nand->priv = &pmecc_host; |
| 768 | |
| 769 | nand->ecc.mode = NAND_ECC_HW; |
| 770 | nand->ecc.calculate = NULL; |
| 771 | nand->ecc.correct = NULL; |
| 772 | nand->ecc.hwctl = NULL; |
| 773 | |
Wu, Josh | 3b4c7f6 | 2013-07-04 15:36:23 +0800 | [diff] [blame] | 774 | #ifdef CONFIG_SYS_NAND_ONFI_DETECTION |
| 775 | host->pmecc_corr_cap = host->pmecc_sector_size = 0; |
| 776 | |
| 777 | #ifdef CONFIG_PMECC_CAP |
| 778 | host->pmecc_corr_cap = CONFIG_PMECC_CAP; |
| 779 | #endif |
| 780 | #ifdef CONFIG_PMECC_SECTOR_SIZE |
| 781 | host->pmecc_sector_size = CONFIG_PMECC_SECTOR_SIZE; |
| 782 | #endif |
| 783 | /* Get ECC requirement of ONFI parameters. And if CONFIG_PMECC_CAP or |
| 784 | * CONFIG_PMECC_SECTOR_SIZE not defined, then use ecc_bits, sector_size |
| 785 | * from ONFI. |
| 786 | */ |
| 787 | if (pmecc_choose_ecc(host, nand, &cap, §or_size)) { |
| 788 | dev_err(host->dev, "The NAND flash's ECC requirement(ecc_bits: %d, sector_size: %d) are not support!", |
| 789 | cap, sector_size); |
| 790 | return -EINVAL; |
| 791 | } |
| 792 | |
| 793 | if (cap > host->pmecc_corr_cap) |
| 794 | dev_info(host->dev, "WARNING: Using different ecc correct bits(%d bit) from Nand ONFI ECC reqirement (%d bit).\n", |
| 795 | host->pmecc_corr_cap, cap); |
| 796 | if (sector_size < host->pmecc_sector_size) |
| 797 | dev_info(host->dev, "WARNING: Using different ecc correct sector size (%d bytes) from Nand ONFI ECC reqirement (%d bytes).\n", |
| 798 | host->pmecc_sector_size, sector_size); |
| 799 | #else /* CONFIG_SYS_NAND_ONFI_DETECTION */ |
| 800 | host->pmecc_corr_cap = CONFIG_PMECC_CAP; |
| 801 | host->pmecc_sector_size = CONFIG_PMECC_SECTOR_SIZE; |
| 802 | #endif |
| 803 | |
| 804 | cap = host->pmecc_corr_cap; |
| 805 | sector_size = host->pmecc_sector_size; |
| 806 | |
| 807 | /* TODO: need check whether cap & sector_size is validate */ |
| 808 | |
Wu, Josh | b45c949 | 2013-07-03 11:11:45 +0800 | [diff] [blame] | 809 | if (host->pmecc_sector_size == 512) |
| 810 | host->pmecc_index_table_offset = ATMEL_PMECC_INDEX_OFFSET_512; |
| 811 | else |
| 812 | host->pmecc_index_table_offset = ATMEL_PMECC_INDEX_OFFSET_1024; |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 813 | |
Wu, Josh | 8c68888 | 2012-09-09 23:45:49 +0000 | [diff] [blame] | 814 | MTDDEBUG(MTD_DEBUG_LEVEL1, |
| 815 | "Initialize PMECC params, cap: %d, sector: %d\n", |
| 816 | cap, sector_size); |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 817 | |
| 818 | host->pmecc = (struct pmecc_regs __iomem *) ATMEL_BASE_PMECC; |
| 819 | host->pmerrloc = (struct pmecc_errloc_regs __iomem *) |
| 820 | ATMEL_BASE_PMERRLOC; |
| 821 | host->pmecc_rom_base = (void __iomem *) ATMEL_BASE_ROM; |
| 822 | |
| 823 | /* ECC is calculated for the whole page (1 step) */ |
| 824 | nand->ecc.size = mtd->writesize; |
| 825 | |
| 826 | /* set ECC page size and oob layout */ |
| 827 | switch (mtd->writesize) { |
| 828 | case 2048: |
| 829 | case 4096: |
Wu, Josh | 89bdc8e | 2013-08-23 15:09:05 +0800 | [diff] [blame] | 830 | host->pmecc_degree = (sector_size == 512) ? |
| 831 | PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14; |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 832 | host->pmecc_cw_len = (1 << host->pmecc_degree) - 1; |
| 833 | host->pmecc_sector_number = mtd->writesize / sector_size; |
| 834 | host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes( |
| 835 | cap, sector_size); |
| 836 | host->pmecc_alpha_to = pmecc_get_alpha_to(host); |
| 837 | host->pmecc_index_of = host->pmecc_rom_base + |
| 838 | host->pmecc_index_table_offset; |
| 839 | |
| 840 | nand->ecc.steps = 1; |
| 841 | nand->ecc.bytes = host->pmecc_bytes_per_sector * |
| 842 | host->pmecc_sector_number; |
| 843 | if (nand->ecc.bytes > mtd->oobsize - 2) { |
| 844 | printk(KERN_ERR "No room for ECC bytes\n"); |
| 845 | return -EINVAL; |
| 846 | } |
| 847 | pmecc_config_ecc_layout(&atmel_pmecc_oobinfo, |
| 848 | mtd->oobsize, |
| 849 | nand->ecc.bytes); |
| 850 | nand->ecc.layout = &atmel_pmecc_oobinfo; |
| 851 | break; |
| 852 | case 512: |
| 853 | case 1024: |
| 854 | /* TODO */ |
| 855 | printk(KERN_ERR "Unsupported page size for PMECC, use Software ECC\n"); |
| 856 | default: |
| 857 | /* page size not handled by HW ECC */ |
| 858 | /* switching back to soft ECC */ |
| 859 | nand->ecc.mode = NAND_ECC_SOFT; |
| 860 | nand->ecc.read_page = NULL; |
| 861 | nand->ecc.postpad = 0; |
| 862 | nand->ecc.prepad = 0; |
| 863 | nand->ecc.bytes = 0; |
| 864 | return 0; |
| 865 | } |
| 866 | |
Wu, Josh | 4e87b315 | 2013-07-03 11:11:48 +0800 | [diff] [blame] | 867 | /* Allocate data for PMECC computation */ |
| 868 | if (pmecc_data_alloc(host)) { |
| 869 | dev_err(host->dev, "Cannot allocate memory for PMECC computation!\n"); |
| 870 | return -ENOMEM; |
| 871 | } |
| 872 | |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 873 | nand->ecc.read_page = atmel_nand_pmecc_read_page; |
| 874 | nand->ecc.write_page = atmel_nand_pmecc_write_page; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 875 | nand->ecc.strength = cap; |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 876 | |
| 877 | atmel_pmecc_core_init(mtd); |
| 878 | |
| 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | #else |
| 883 | |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 884 | /* oob layout for large page size |
| 885 | * bad block info is on bytes 0 and 1 |
| 886 | * the bytes have to be consecutives to avoid |
| 887 | * several NAND_CMD_RNDOUT during read |
| 888 | */ |
| 889 | static struct nand_ecclayout atmel_oobinfo_large = { |
| 890 | .eccbytes = 4, |
| 891 | .eccpos = {60, 61, 62, 63}, |
| 892 | .oobfree = { |
| 893 | {2, 58} |
| 894 | }, |
| 895 | }; |
| 896 | |
| 897 | /* oob layout for small page size |
| 898 | * bad block info is on bytes 4 and 5 |
| 899 | * the bytes have to be consecutives to avoid |
| 900 | * several NAND_CMD_RNDOUT during read |
| 901 | */ |
| 902 | static struct nand_ecclayout atmel_oobinfo_small = { |
| 903 | .eccbytes = 4, |
| 904 | .eccpos = {0, 1, 2, 3}, |
| 905 | .oobfree = { |
| 906 | {6, 10} |
| 907 | }, |
| 908 | }; |
| 909 | |
| 910 | /* |
| 911 | * Calculate HW ECC |
| 912 | * |
| 913 | * function called after a write |
| 914 | * |
| 915 | * mtd: MTD block structure |
| 916 | * dat: raw data (unused) |
| 917 | * ecc_code: buffer for ECC |
| 918 | */ |
| 919 | static int atmel_nand_calculate(struct mtd_info *mtd, |
| 920 | const u_char *dat, unsigned char *ecc_code) |
| 921 | { |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 922 | unsigned int ecc_value; |
| 923 | |
| 924 | /* get the first 2 ECC bytes */ |
| 925 | ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR); |
| 926 | |
| 927 | ecc_code[0] = ecc_value & 0xFF; |
| 928 | ecc_code[1] = (ecc_value >> 8) & 0xFF; |
| 929 | |
| 930 | /* get the last 2 ECC bytes */ |
| 931 | ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, NPR) & ATMEL_ECC_NPARITY; |
| 932 | |
| 933 | ecc_code[2] = ecc_value & 0xFF; |
| 934 | ecc_code[3] = (ecc_value >> 8) & 0xFF; |
| 935 | |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | * HW ECC read page function |
| 941 | * |
| 942 | * mtd: mtd info structure |
| 943 | * chip: nand chip info structure |
| 944 | * buf: buffer to store read data |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 945 | * oob_required: caller expects OOB data read to chip->oob_poi |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 946 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 947 | static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip, |
| 948 | uint8_t *buf, int oob_required, int page) |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 949 | { |
| 950 | int eccsize = chip->ecc.size; |
| 951 | int eccbytes = chip->ecc.bytes; |
| 952 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 953 | uint8_t *p = buf; |
| 954 | uint8_t *oob = chip->oob_poi; |
| 955 | uint8_t *ecc_pos; |
| 956 | int stat; |
| 957 | |
| 958 | /* read the page */ |
| 959 | chip->read_buf(mtd, p, eccsize); |
| 960 | |
| 961 | /* move to ECC position if needed */ |
| 962 | if (eccpos[0] != 0) { |
| 963 | /* This only works on large pages |
| 964 | * because the ECC controller waits for |
| 965 | * NAND_CMD_RNDOUTSTART after the |
| 966 | * NAND_CMD_RNDOUT. |
| 967 | * anyway, for small pages, the eccpos[0] == 0 |
| 968 | */ |
| 969 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
| 970 | mtd->writesize + eccpos[0], -1); |
| 971 | } |
| 972 | |
| 973 | /* the ECC controller needs to read the ECC just after the data */ |
| 974 | ecc_pos = oob + eccpos[0]; |
| 975 | chip->read_buf(mtd, ecc_pos, eccbytes); |
| 976 | |
| 977 | /* check if there's an error */ |
| 978 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
| 979 | |
| 980 | if (stat < 0) |
| 981 | mtd->ecc_stats.failed++; |
| 982 | else |
| 983 | mtd->ecc_stats.corrected += stat; |
| 984 | |
| 985 | /* get back to oob start (end of page) */ |
| 986 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1); |
| 987 | |
| 988 | /* read the oob */ |
| 989 | chip->read_buf(mtd, oob, mtd->oobsize); |
| 990 | |
| 991 | return 0; |
| 992 | } |
| 993 | |
| 994 | /* |
| 995 | * HW ECC Correction |
| 996 | * |
| 997 | * function called after a read |
| 998 | * |
| 999 | * mtd: MTD block structure |
| 1000 | * dat: raw data read from the chip |
| 1001 | * read_ecc: ECC from the chip (unused) |
| 1002 | * isnull: unused |
| 1003 | * |
| 1004 | * Detect and correct a 1 bit error for a page |
| 1005 | */ |
| 1006 | static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat, |
| 1007 | u_char *read_ecc, u_char *isnull) |
| 1008 | { |
| 1009 | struct nand_chip *nand_chip = mtd->priv; |
Wu, Josh | 1586d1b | 2012-08-23 00:05:35 +0000 | [diff] [blame] | 1010 | unsigned int ecc_status; |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1011 | unsigned int ecc_word, ecc_bit; |
| 1012 | |
| 1013 | /* get the status from the Status Register */ |
| 1014 | ecc_status = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, SR); |
| 1015 | |
| 1016 | /* if there's no error */ |
| 1017 | if (likely(!(ecc_status & ATMEL_ECC_RECERR))) |
| 1018 | return 0; |
| 1019 | |
| 1020 | /* get error bit offset (4 bits) */ |
| 1021 | ecc_bit = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_BITADDR; |
| 1022 | /* get word address (12 bits) */ |
| 1023 | ecc_word = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_WORDADDR; |
| 1024 | ecc_word >>= 4; |
| 1025 | |
| 1026 | /* if there are multiple errors */ |
| 1027 | if (ecc_status & ATMEL_ECC_MULERR) { |
| 1028 | /* check if it is a freshly erased block |
| 1029 | * (filled with 0xff) */ |
| 1030 | if ((ecc_bit == ATMEL_ECC_BITADDR) |
| 1031 | && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) { |
| 1032 | /* the block has just been erased, return OK */ |
| 1033 | return 0; |
| 1034 | } |
| 1035 | /* it doesn't seems to be a freshly |
| 1036 | * erased block. |
| 1037 | * We can't correct so many errors */ |
| 1038 | printk(KERN_WARNING "atmel_nand : multiple errors detected." |
| 1039 | " Unable to correct.\n"); |
| 1040 | return -EIO; |
| 1041 | } |
| 1042 | |
| 1043 | /* if there's a single bit error : we can correct it */ |
| 1044 | if (ecc_status & ATMEL_ECC_ECCERR) { |
| 1045 | /* there's nothing much to do here. |
| 1046 | * the bit error is on the ECC itself. |
| 1047 | */ |
| 1048 | printk(KERN_WARNING "atmel_nand : one bit error on ECC code." |
| 1049 | " Nothing to correct\n"); |
| 1050 | return 0; |
| 1051 | } |
| 1052 | |
| 1053 | printk(KERN_WARNING "atmel_nand : one bit error on data." |
| 1054 | " (word offset in the page :" |
| 1055 | " 0x%x bit offset : 0x%x)\n", |
| 1056 | ecc_word, ecc_bit); |
| 1057 | /* correct the error */ |
| 1058 | if (nand_chip->options & NAND_BUSWIDTH_16) { |
| 1059 | /* 16 bits words */ |
| 1060 | ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit); |
| 1061 | } else { |
| 1062 | /* 8 bits words */ |
| 1063 | dat[ecc_word] ^= (1 << ecc_bit); |
| 1064 | } |
| 1065 | printk(KERN_WARNING "atmel_nand : error corrected\n"); |
| 1066 | return 1; |
| 1067 | } |
| 1068 | |
| 1069 | /* |
| 1070 | * Enable HW ECC : unused on most chips |
| 1071 | */ |
| 1072 | static void atmel_nand_hwctl(struct mtd_info *mtd, int mode) |
| 1073 | { |
| 1074 | } |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1075 | |
| 1076 | int atmel_hwecc_nand_init_param(struct nand_chip *nand, struct mtd_info *mtd) |
| 1077 | { |
| 1078 | nand->ecc.mode = NAND_ECC_HW; |
| 1079 | nand->ecc.calculate = atmel_nand_calculate; |
| 1080 | nand->ecc.correct = atmel_nand_correct; |
| 1081 | nand->ecc.hwctl = atmel_nand_hwctl; |
| 1082 | nand->ecc.read_page = atmel_nand_read_page; |
| 1083 | nand->ecc.bytes = 4; |
| 1084 | |
| 1085 | if (nand->ecc.mode == NAND_ECC_HW) { |
| 1086 | /* ECC is calculated for the whole page (1 step) */ |
| 1087 | nand->ecc.size = mtd->writesize; |
| 1088 | |
| 1089 | /* set ECC page size and oob layout */ |
| 1090 | switch (mtd->writesize) { |
| 1091 | case 512: |
| 1092 | nand->ecc.layout = &atmel_oobinfo_small; |
| 1093 | ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR, |
| 1094 | ATMEL_ECC_PAGESIZE_528); |
| 1095 | break; |
| 1096 | case 1024: |
| 1097 | nand->ecc.layout = &atmel_oobinfo_large; |
| 1098 | ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR, |
| 1099 | ATMEL_ECC_PAGESIZE_1056); |
| 1100 | break; |
| 1101 | case 2048: |
| 1102 | nand->ecc.layout = &atmel_oobinfo_large; |
| 1103 | ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR, |
| 1104 | ATMEL_ECC_PAGESIZE_2112); |
| 1105 | break; |
| 1106 | case 4096: |
| 1107 | nand->ecc.layout = &atmel_oobinfo_large; |
| 1108 | ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR, |
| 1109 | ATMEL_ECC_PAGESIZE_4224); |
| 1110 | break; |
| 1111 | default: |
| 1112 | /* page size not handled by HW ECC */ |
| 1113 | /* switching back to soft ECC */ |
| 1114 | nand->ecc.mode = NAND_ECC_SOFT; |
| 1115 | nand->ecc.calculate = NULL; |
| 1116 | nand->ecc.correct = NULL; |
| 1117 | nand->ecc.hwctl = NULL; |
| 1118 | nand->ecc.read_page = NULL; |
| 1119 | nand->ecc.postpad = 0; |
| 1120 | nand->ecc.prepad = 0; |
| 1121 | nand->ecc.bytes = 0; |
| 1122 | break; |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 1129 | #endif /* CONFIG_ATMEL_NAND_HW_PMECC */ |
| 1130 | |
| 1131 | #endif /* CONFIG_ATMEL_NAND_HWECC */ |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1132 | |
Jean-Christophe PLAGNIOL-VILLARD | c9539ba | 2009-03-22 10:22:34 +0100 | [diff] [blame] | 1133 | static void at91_nand_hwcontrol(struct mtd_info *mtd, |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1134 | int cmd, unsigned int ctrl) |
| 1135 | { |
| 1136 | struct nand_chip *this = mtd->priv; |
| 1137 | |
| 1138 | if (ctrl & NAND_CTRL_CHANGE) { |
| 1139 | ulong IO_ADDR_W = (ulong) this->IO_ADDR_W; |
Jean-Christophe PLAGNIOL-VILLARD | c9539ba | 2009-03-22 10:22:34 +0100 | [diff] [blame] | 1140 | IO_ADDR_W &= ~(CONFIG_SYS_NAND_MASK_ALE |
| 1141 | | CONFIG_SYS_NAND_MASK_CLE); |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1142 | |
| 1143 | if (ctrl & NAND_CLE) |
Jean-Christophe PLAGNIOL-VILLARD | c9539ba | 2009-03-22 10:22:34 +0100 | [diff] [blame] | 1144 | IO_ADDR_W |= CONFIG_SYS_NAND_MASK_CLE; |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1145 | if (ctrl & NAND_ALE) |
Jean-Christophe PLAGNIOL-VILLARD | c9539ba | 2009-03-22 10:22:34 +0100 | [diff] [blame] | 1146 | IO_ADDR_W |= CONFIG_SYS_NAND_MASK_ALE; |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1147 | |
michael | 4b5cef7 | 2011-03-14 21:16:38 +0000 | [diff] [blame] | 1148 | #ifdef CONFIG_SYS_NAND_ENABLE_PIN |
Jean-Christophe PLAGNIOL-VILLARD | c9539ba | 2009-03-22 10:22:34 +0100 | [diff] [blame] | 1149 | at91_set_gpio_value(CONFIG_SYS_NAND_ENABLE_PIN, |
| 1150 | !(ctrl & NAND_NCE)); |
michael | 4b5cef7 | 2011-03-14 21:16:38 +0000 | [diff] [blame] | 1151 | #endif |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1152 | this->IO_ADDR_W = (void *) IO_ADDR_W; |
| 1153 | } |
| 1154 | |
| 1155 | if (cmd != NAND_CMD_NONE) |
| 1156 | writeb(cmd, this->IO_ADDR_W); |
| 1157 | } |
| 1158 | |
Jean-Christophe PLAGNIOL-VILLARD | c9539ba | 2009-03-22 10:22:34 +0100 | [diff] [blame] | 1159 | #ifdef CONFIG_SYS_NAND_READY_PIN |
| 1160 | static int at91_nand_ready(struct mtd_info *mtd) |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1161 | { |
Jean-Christophe PLAGNIOL-VILLARD | c9539ba | 2009-03-22 10:22:34 +0100 | [diff] [blame] | 1162 | return at91_get_gpio_value(CONFIG_SYS_NAND_READY_PIN); |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1163 | } |
Jean-Christophe PLAGNIOL-VILLARD | c9539ba | 2009-03-22 10:22:34 +0100 | [diff] [blame] | 1164 | #endif |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1165 | |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1166 | #ifndef CONFIG_SYS_NAND_BASE_LIST |
| 1167 | #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE } |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1168 | #endif |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1169 | static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE]; |
| 1170 | static ulong base_addr[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST; |
| 1171 | |
| 1172 | int atmel_nand_chip_init(int devnum, ulong base_addr) |
| 1173 | { |
| 1174 | int ret; |
| 1175 | struct mtd_info *mtd = &nand_info[devnum]; |
| 1176 | struct nand_chip *nand = &nand_chip[devnum]; |
| 1177 | |
| 1178 | mtd->priv = nand; |
| 1179 | nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr; |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1180 | |
Bo Shen | bb5a12f | 2013-08-28 14:54:26 +0000 | [diff] [blame] | 1181 | #ifdef CONFIG_NAND_ECC_BCH |
| 1182 | nand->ecc.mode = NAND_ECC_SOFT_BCH; |
| 1183 | #else |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1184 | nand->ecc.mode = NAND_ECC_SOFT; |
Bo Shen | bb5a12f | 2013-08-28 14:54:26 +0000 | [diff] [blame] | 1185 | #endif |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1186 | #ifdef CONFIG_SYS_NAND_DBW_16 |
| 1187 | nand->options = NAND_BUSWIDTH_16; |
| 1188 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | c9539ba | 2009-03-22 10:22:34 +0100 | [diff] [blame] | 1189 | nand->cmd_ctrl = at91_nand_hwcontrol; |
| 1190 | #ifdef CONFIG_SYS_NAND_READY_PIN |
| 1191 | nand->dev_ready = at91_nand_ready; |
| 1192 | #endif |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1193 | nand->chip_delay = 20; |
| 1194 | |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1195 | ret = nand_scan_ident(mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL); |
| 1196 | if (ret) |
| 1197 | return ret; |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1198 | |
| 1199 | #ifdef CONFIG_ATMEL_NAND_HWECC |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 1200 | #ifdef CONFIG_ATMEL_NAND_HW_PMECC |
| 1201 | ret = atmel_pmecc_nand_init_params(nand, mtd); |
| 1202 | #else |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1203 | ret = atmel_hwecc_nand_init_param(nand, mtd); |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 1204 | #endif |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1205 | if (ret) |
| 1206 | return ret; |
| 1207 | #endif |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1208 | |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1209 | ret = nand_scan_tail(mtd); |
| 1210 | if (!ret) |
| 1211 | nand_register(devnum); |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1212 | |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1213 | return ret; |
| 1214 | } |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1215 | |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1216 | void board_nand_init(void) |
| 1217 | { |
| 1218 | int i; |
| 1219 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) |
| 1220 | if (atmel_nand_chip_init(i, base_addr[i])) |
| 1221 | printk(KERN_ERR "atmel_nand: Fail to initialize #%d chip", |
| 1222 | i); |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1223 | } |