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