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> |
Andreas Bießmann | a4c24d3 | 2013-11-29 12:13:45 +0100 | [diff] [blame] | 15 | #include <asm/gpio.h> |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 16 | #include <asm/arch/gpio.h> |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 17 | |
Wu, Josh | 4e87b315 | 2013-07-03 11:11:48 +0800 | [diff] [blame] | 18 | #include <malloc.h> |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 19 | #include <nand.h> |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 20 | #include <watchdog.h> |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 21 | |
Nikolay Petukhov | e6015ca | 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 | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 32 | #ifdef CONFIG_ATMEL_NAND_HW_PMECC |
| 33 | |
Bo Shen | 9415b87 | 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 | fd3091d | 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 | 4e87b315 | 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 | fd3091d | 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 | 4e87b315 | 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 | fd3091d | 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 | b31868f | 2014-06-24 18:18:06 +0800 | [diff] [blame^] | 167 | value = pmecc_readl(host->pmecc, rem_port[sector].rem[i / 2]); |
Wu, Josh | fd3091d | 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 | b31868f | 2014-06-24 18:18:06 +0800 | [diff] [blame^] | 395 | pmecc_writel(host->pmerrloc, eldis, PMERRLOC_DISABLE); |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 396 | |
| 397 | for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) { |
Wu, Josh | b31868f | 2014-06-24 18:18:06 +0800 | [diff] [blame^] | 398 | pmecc_writel(host->pmerrloc, sigma[i], |
| 399 | smu[(cap + 1) * num + i]); |
Wu, Josh | fd3091d | 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 | b31868f | 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 | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 410 | |
| 411 | while (--timeout) { |
Wu, Josh | b31868f | 2014-06-24 18:18:06 +0800 | [diff] [blame^] | 412 | if (pmecc_readl(host->pmerrloc, elisr) & PMERRLOC_CALC_DONE) |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 413 | break; |
| 414 | WATCHDOG_RESET(); |
| 415 | udelay(1); |
| 416 | } |
| 417 | |
| 418 | if (!timeout) { |
Wu, Josh | a8bd1d4 | 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 | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 420 | return -1; |
| 421 | } |
| 422 | |
Wu, Josh | b31868f | 2014-06-24 18:18:06 +0800 | [diff] [blame^] | 423 | roots_nbr = (pmecc_readl(host->pmerrloc, elisr) & PMERRLOC_ERR_NUM_MASK) |
Wu, Josh | fd3091d | 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 | b31868f | 2014-06-24 18:18:06 +0800 | [diff] [blame^] | 447 | tmp = pmecc_readl(host->pmerrloc, el[i]) - 1; |
Wu, Josh | fd3091d | 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 | 0453402 | 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 | fd3091d | 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 | 0453402 | 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 | fd3091d | 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 | a8bd1d4 | 2013-10-18 17:46:34 +0800 | [diff] [blame] | 507 | dev_err(host->dev, "PMECC: Too many errors\n"); |
Wu, Josh | fd3091d | 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 | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 523 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
Wu, Josh | fd3091d | 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 | a8bd1d4 | 2013-10-18 17:46:34 +0800 | [diff] [blame] | 551 | dev_err(host->dev, "atmel_nand : Timeout to read PMECC page\n"); |
Wu, Josh | fd3091d | 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 | 3a38a55 | 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 | fd3091d | 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 | a8bd1d4 | 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 | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 592 | goto out; |
Wu, Josh | fd3091d | 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 | b31868f | 2014-06-24 18:18:06 +0800 | [diff] [blame^] | 601 | pmecc_readb(host->pmecc, ecc_port[i].ecc[j]); |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 605 | out: |
| 606 | return 0; |
Wu, Josh | fd3091d | 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 | 3b4c7f6 | 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 | fd3091d | 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 | 3b4c7f6 | 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 | b45c949 | 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 | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 817 | |
Wu, Josh | 8c68888 | 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 | fd3091d | 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 | f9f69b1 | 2013-10-18 17:46:31 +0800 | [diff] [blame] | 834 | case 8192: |
Wu, Josh | 89bdc8e | 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 | fd3091d | 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 | f9f69b1 | 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 | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 855 | if (nand->ecc.bytes > mtd->oobsize - 2) { |
Wu, Josh | a8bd1d4 | 2013-10-18 17:46:34 +0800 | [diff] [blame] | 856 | dev_err(host->dev, "No room for ECC bytes\n"); |
Wu, Josh | fd3091d | 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 | a8bd1d4 | 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 | fd3091d | 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 | 4e87b315 | 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 | |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 885 | nand->ecc.read_page = atmel_nand_pmecc_read_page; |
| 886 | nand->ecc.write_page = atmel_nand_pmecc_write_page; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 887 | nand->ecc.strength = cap; |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 888 | |
| 889 | atmel_pmecc_core_init(mtd); |
| 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | #else |
| 895 | |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 896 | /* oob layout for large page size |
| 897 | * bad block info is on bytes 0 and 1 |
| 898 | * the bytes have to be consecutives to avoid |
| 899 | * several NAND_CMD_RNDOUT during read |
| 900 | */ |
| 901 | static struct nand_ecclayout atmel_oobinfo_large = { |
| 902 | .eccbytes = 4, |
| 903 | .eccpos = {60, 61, 62, 63}, |
| 904 | .oobfree = { |
| 905 | {2, 58} |
| 906 | }, |
| 907 | }; |
| 908 | |
| 909 | /* oob layout for small page size |
| 910 | * bad block info is on bytes 4 and 5 |
| 911 | * the bytes have to be consecutives to avoid |
| 912 | * several NAND_CMD_RNDOUT during read |
| 913 | */ |
| 914 | static struct nand_ecclayout atmel_oobinfo_small = { |
| 915 | .eccbytes = 4, |
| 916 | .eccpos = {0, 1, 2, 3}, |
| 917 | .oobfree = { |
| 918 | {6, 10} |
| 919 | }, |
| 920 | }; |
| 921 | |
| 922 | /* |
| 923 | * Calculate HW ECC |
| 924 | * |
| 925 | * function called after a write |
| 926 | * |
| 927 | * mtd: MTD block structure |
| 928 | * dat: raw data (unused) |
| 929 | * ecc_code: buffer for ECC |
| 930 | */ |
| 931 | static int atmel_nand_calculate(struct mtd_info *mtd, |
| 932 | const u_char *dat, unsigned char *ecc_code) |
| 933 | { |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 934 | unsigned int ecc_value; |
| 935 | |
| 936 | /* get the first 2 ECC bytes */ |
| 937 | ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR); |
| 938 | |
| 939 | ecc_code[0] = ecc_value & 0xFF; |
| 940 | ecc_code[1] = (ecc_value >> 8) & 0xFF; |
| 941 | |
| 942 | /* get the last 2 ECC bytes */ |
| 943 | ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, NPR) & ATMEL_ECC_NPARITY; |
| 944 | |
| 945 | ecc_code[2] = ecc_value & 0xFF; |
| 946 | ecc_code[3] = (ecc_value >> 8) & 0xFF; |
| 947 | |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | /* |
| 952 | * HW ECC read page function |
| 953 | * |
| 954 | * mtd: mtd info structure |
| 955 | * chip: nand chip info structure |
| 956 | * buf: buffer to store read data |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 957 | * oob_required: caller expects OOB data read to chip->oob_poi |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 958 | */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 959 | static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip, |
| 960 | uint8_t *buf, int oob_required, int page) |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 961 | { |
| 962 | int eccsize = chip->ecc.size; |
| 963 | int eccbytes = chip->ecc.bytes; |
| 964 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 965 | uint8_t *p = buf; |
| 966 | uint8_t *oob = chip->oob_poi; |
| 967 | uint8_t *ecc_pos; |
| 968 | int stat; |
| 969 | |
| 970 | /* read the page */ |
| 971 | chip->read_buf(mtd, p, eccsize); |
| 972 | |
| 973 | /* move to ECC position if needed */ |
| 974 | if (eccpos[0] != 0) { |
| 975 | /* This only works on large pages |
| 976 | * because the ECC controller waits for |
| 977 | * NAND_CMD_RNDOUTSTART after the |
| 978 | * NAND_CMD_RNDOUT. |
| 979 | * anyway, for small pages, the eccpos[0] == 0 |
| 980 | */ |
| 981 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
| 982 | mtd->writesize + eccpos[0], -1); |
| 983 | } |
| 984 | |
| 985 | /* the ECC controller needs to read the ECC just after the data */ |
| 986 | ecc_pos = oob + eccpos[0]; |
| 987 | chip->read_buf(mtd, ecc_pos, eccbytes); |
| 988 | |
| 989 | /* check if there's an error */ |
| 990 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
| 991 | |
| 992 | if (stat < 0) |
| 993 | mtd->ecc_stats.failed++; |
| 994 | else |
| 995 | mtd->ecc_stats.corrected += stat; |
| 996 | |
| 997 | /* get back to oob start (end of page) */ |
| 998 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1); |
| 999 | |
| 1000 | /* read the oob */ |
| 1001 | chip->read_buf(mtd, oob, mtd->oobsize); |
| 1002 | |
| 1003 | return 0; |
| 1004 | } |
| 1005 | |
| 1006 | /* |
| 1007 | * HW ECC Correction |
| 1008 | * |
| 1009 | * function called after a read |
| 1010 | * |
| 1011 | * mtd: MTD block structure |
| 1012 | * dat: raw data read from the chip |
| 1013 | * read_ecc: ECC from the chip (unused) |
| 1014 | * isnull: unused |
| 1015 | * |
| 1016 | * Detect and correct a 1 bit error for a page |
| 1017 | */ |
| 1018 | static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat, |
| 1019 | u_char *read_ecc, u_char *isnull) |
| 1020 | { |
| 1021 | struct nand_chip *nand_chip = mtd->priv; |
Wu, Josh | 1586d1b | 2012-08-23 00:05:35 +0000 | [diff] [blame] | 1022 | unsigned int ecc_status; |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1023 | unsigned int ecc_word, ecc_bit; |
| 1024 | |
| 1025 | /* get the status from the Status Register */ |
| 1026 | ecc_status = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, SR); |
| 1027 | |
| 1028 | /* if there's no error */ |
| 1029 | if (likely(!(ecc_status & ATMEL_ECC_RECERR))) |
| 1030 | return 0; |
| 1031 | |
| 1032 | /* get error bit offset (4 bits) */ |
| 1033 | ecc_bit = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_BITADDR; |
| 1034 | /* get word address (12 bits) */ |
| 1035 | ecc_word = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_WORDADDR; |
| 1036 | ecc_word >>= 4; |
| 1037 | |
| 1038 | /* if there are multiple errors */ |
| 1039 | if (ecc_status & ATMEL_ECC_MULERR) { |
| 1040 | /* check if it is a freshly erased block |
| 1041 | * (filled with 0xff) */ |
| 1042 | if ((ecc_bit == ATMEL_ECC_BITADDR) |
| 1043 | && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) { |
| 1044 | /* the block has just been erased, return OK */ |
| 1045 | return 0; |
| 1046 | } |
| 1047 | /* it doesn't seems to be a freshly |
| 1048 | * erased block. |
| 1049 | * We can't correct so many errors */ |
Wu, Josh | a8bd1d4 | 2013-10-18 17:46:34 +0800 | [diff] [blame] | 1050 | dev_warn(host->dev, "atmel_nand : multiple errors detected." |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1051 | " Unable to correct.\n"); |
| 1052 | return -EIO; |
| 1053 | } |
| 1054 | |
| 1055 | /* if there's a single bit error : we can correct it */ |
| 1056 | if (ecc_status & ATMEL_ECC_ECCERR) { |
| 1057 | /* there's nothing much to do here. |
| 1058 | * the bit error is on the ECC itself. |
| 1059 | */ |
Wu, Josh | a8bd1d4 | 2013-10-18 17:46:34 +0800 | [diff] [blame] | 1060 | dev_warn(host->dev, "atmel_nand : one bit error on ECC code." |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1061 | " Nothing to correct\n"); |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
Wu, Josh | a8bd1d4 | 2013-10-18 17:46:34 +0800 | [diff] [blame] | 1065 | dev_warn(host->dev, "atmel_nand : one bit error on data." |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1066 | " (word offset in the page :" |
| 1067 | " 0x%x bit offset : 0x%x)\n", |
| 1068 | ecc_word, ecc_bit); |
| 1069 | /* correct the error */ |
| 1070 | if (nand_chip->options & NAND_BUSWIDTH_16) { |
| 1071 | /* 16 bits words */ |
| 1072 | ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit); |
| 1073 | } else { |
| 1074 | /* 8 bits words */ |
| 1075 | dat[ecc_word] ^= (1 << ecc_bit); |
| 1076 | } |
Wu, Josh | a8bd1d4 | 2013-10-18 17:46:34 +0800 | [diff] [blame] | 1077 | dev_warn(host->dev, "atmel_nand : error corrected\n"); |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1078 | return 1; |
| 1079 | } |
| 1080 | |
| 1081 | /* |
| 1082 | * Enable HW ECC : unused on most chips |
| 1083 | */ |
| 1084 | static void atmel_nand_hwctl(struct mtd_info *mtd, int mode) |
| 1085 | { |
| 1086 | } |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1087 | |
| 1088 | int atmel_hwecc_nand_init_param(struct nand_chip *nand, struct mtd_info *mtd) |
| 1089 | { |
| 1090 | nand->ecc.mode = NAND_ECC_HW; |
| 1091 | nand->ecc.calculate = atmel_nand_calculate; |
| 1092 | nand->ecc.correct = atmel_nand_correct; |
| 1093 | nand->ecc.hwctl = atmel_nand_hwctl; |
| 1094 | nand->ecc.read_page = atmel_nand_read_page; |
| 1095 | nand->ecc.bytes = 4; |
| 1096 | |
| 1097 | if (nand->ecc.mode == NAND_ECC_HW) { |
| 1098 | /* ECC is calculated for the whole page (1 step) */ |
| 1099 | nand->ecc.size = mtd->writesize; |
| 1100 | |
| 1101 | /* set ECC page size and oob layout */ |
| 1102 | switch (mtd->writesize) { |
| 1103 | case 512: |
| 1104 | nand->ecc.layout = &atmel_oobinfo_small; |
| 1105 | ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR, |
| 1106 | ATMEL_ECC_PAGESIZE_528); |
| 1107 | break; |
| 1108 | case 1024: |
| 1109 | nand->ecc.layout = &atmel_oobinfo_large; |
| 1110 | ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR, |
| 1111 | ATMEL_ECC_PAGESIZE_1056); |
| 1112 | break; |
| 1113 | case 2048: |
| 1114 | nand->ecc.layout = &atmel_oobinfo_large; |
| 1115 | ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR, |
| 1116 | ATMEL_ECC_PAGESIZE_2112); |
| 1117 | break; |
| 1118 | case 4096: |
| 1119 | nand->ecc.layout = &atmel_oobinfo_large; |
| 1120 | ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR, |
| 1121 | ATMEL_ECC_PAGESIZE_4224); |
| 1122 | break; |
| 1123 | default: |
| 1124 | /* page size not handled by HW ECC */ |
| 1125 | /* switching back to soft ECC */ |
| 1126 | nand->ecc.mode = NAND_ECC_SOFT; |
| 1127 | nand->ecc.calculate = NULL; |
| 1128 | nand->ecc.correct = NULL; |
| 1129 | nand->ecc.hwctl = NULL; |
| 1130 | nand->ecc.read_page = NULL; |
| 1131 | nand->ecc.postpad = 0; |
| 1132 | nand->ecc.prepad = 0; |
| 1133 | nand->ecc.bytes = 0; |
| 1134 | break; |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 1141 | #endif /* CONFIG_ATMEL_NAND_HW_PMECC */ |
| 1142 | |
| 1143 | #endif /* CONFIG_ATMEL_NAND_HWECC */ |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1144 | |
Jean-Christophe PLAGNIOL-VILLARD | c9539ba | 2009-03-22 10:22:34 +0100 | [diff] [blame] | 1145 | static void at91_nand_hwcontrol(struct mtd_info *mtd, |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1146 | int cmd, unsigned int ctrl) |
| 1147 | { |
| 1148 | struct nand_chip *this = mtd->priv; |
| 1149 | |
| 1150 | if (ctrl & NAND_CTRL_CHANGE) { |
| 1151 | ulong IO_ADDR_W = (ulong) this->IO_ADDR_W; |
Jean-Christophe PLAGNIOL-VILLARD | c9539ba | 2009-03-22 10:22:34 +0100 | [diff] [blame] | 1152 | IO_ADDR_W &= ~(CONFIG_SYS_NAND_MASK_ALE |
| 1153 | | CONFIG_SYS_NAND_MASK_CLE); |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1154 | |
| 1155 | if (ctrl & NAND_CLE) |
Jean-Christophe PLAGNIOL-VILLARD | c9539ba | 2009-03-22 10:22:34 +0100 | [diff] [blame] | 1156 | IO_ADDR_W |= CONFIG_SYS_NAND_MASK_CLE; |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1157 | if (ctrl & NAND_ALE) |
Jean-Christophe PLAGNIOL-VILLARD | c9539ba | 2009-03-22 10:22:34 +0100 | [diff] [blame] | 1158 | IO_ADDR_W |= CONFIG_SYS_NAND_MASK_ALE; |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1159 | |
michael | 4b5cef7 | 2011-03-14 21:16:38 +0000 | [diff] [blame] | 1160 | #ifdef CONFIG_SYS_NAND_ENABLE_PIN |
Andreas Bießmann | a4c24d3 | 2013-11-29 12:13:45 +0100 | [diff] [blame] | 1161 | gpio_set_value(CONFIG_SYS_NAND_ENABLE_PIN, !(ctrl & NAND_NCE)); |
michael | 4b5cef7 | 2011-03-14 21:16:38 +0000 | [diff] [blame] | 1162 | #endif |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1163 | this->IO_ADDR_W = (void *) IO_ADDR_W; |
| 1164 | } |
| 1165 | |
| 1166 | if (cmd != NAND_CMD_NONE) |
| 1167 | writeb(cmd, this->IO_ADDR_W); |
| 1168 | } |
| 1169 | |
Jean-Christophe PLAGNIOL-VILLARD | c9539ba | 2009-03-22 10:22:34 +0100 | [diff] [blame] | 1170 | #ifdef CONFIG_SYS_NAND_READY_PIN |
| 1171 | static int at91_nand_ready(struct mtd_info *mtd) |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1172 | { |
Andreas Bießmann | a4c24d3 | 2013-11-29 12:13:45 +0100 | [diff] [blame] | 1173 | return gpio_get_value(CONFIG_SYS_NAND_READY_PIN); |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1174 | } |
Jean-Christophe PLAGNIOL-VILLARD | c9539ba | 2009-03-22 10:22:34 +0100 | [diff] [blame] | 1175 | #endif |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1176 | |
Bo Shen | 9415b87 | 2014-03-03 14:47:16 +0800 | [diff] [blame] | 1177 | #ifdef CONFIG_SPL_BUILD |
| 1178 | /* The following code is for SPL */ |
| 1179 | static nand_info_t mtd; |
| 1180 | static struct nand_chip nand_chip; |
| 1181 | |
| 1182 | static int nand_command(int block, int page, uint32_t offs, u8 cmd) |
| 1183 | { |
| 1184 | struct nand_chip *this = mtd.priv; |
| 1185 | int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT; |
| 1186 | void (*hwctrl)(struct mtd_info *mtd, int cmd, |
| 1187 | unsigned int ctrl) = this->cmd_ctrl; |
| 1188 | |
| 1189 | while (this->dev_ready(&mtd)) |
| 1190 | ; |
| 1191 | |
| 1192 | if (cmd == NAND_CMD_READOOB) { |
| 1193 | offs += CONFIG_SYS_NAND_PAGE_SIZE; |
| 1194 | cmd = NAND_CMD_READ0; |
| 1195 | } |
| 1196 | |
| 1197 | hwctrl(&mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
| 1198 | |
Brian Norris | 6767522 | 2014-05-06 00:46:17 +0530 | [diff] [blame] | 1199 | if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd)) |
Bo Shen | 9415b87 | 2014-03-03 14:47:16 +0800 | [diff] [blame] | 1200 | offs >>= 1; |
| 1201 | |
| 1202 | hwctrl(&mtd, offs & 0xff, NAND_CTRL_ALE | NAND_CTRL_CHANGE); |
| 1203 | hwctrl(&mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); |
| 1204 | hwctrl(&mtd, (page_addr & 0xff), NAND_CTRL_ALE); |
| 1205 | hwctrl(&mtd, ((page_addr >> 8) & 0xff), NAND_CTRL_ALE); |
| 1206 | #ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE |
| 1207 | hwctrl(&mtd, (page_addr >> 16) & 0x0f, NAND_CTRL_ALE); |
| 1208 | #endif |
| 1209 | hwctrl(&mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
| 1210 | |
| 1211 | hwctrl(&mtd, NAND_CMD_READSTART, NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
| 1212 | hwctrl(&mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
| 1213 | |
| 1214 | while (this->dev_ready(&mtd)) |
| 1215 | ; |
| 1216 | |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
| 1220 | static int nand_is_bad_block(int block) |
| 1221 | { |
| 1222 | struct nand_chip *this = mtd.priv; |
| 1223 | |
| 1224 | nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS, NAND_CMD_READOOB); |
| 1225 | |
| 1226 | if (this->options & NAND_BUSWIDTH_16) { |
| 1227 | if (readw(this->IO_ADDR_R) != 0xffff) |
| 1228 | return 1; |
| 1229 | } else { |
| 1230 | if (readb(this->IO_ADDR_R) != 0xff) |
| 1231 | return 1; |
| 1232 | } |
| 1233 | |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
| 1237 | #ifdef CONFIG_SPL_NAND_ECC |
| 1238 | static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS; |
| 1239 | #define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \ |
| 1240 | CONFIG_SYS_NAND_ECCSIZE) |
| 1241 | #define ECCTOTAL (ECCSTEPS * CONFIG_SYS_NAND_ECCBYTES) |
| 1242 | |
| 1243 | static int nand_read_page(int block, int page, void *dst) |
| 1244 | { |
| 1245 | struct nand_chip *this = mtd.priv; |
| 1246 | u_char ecc_calc[ECCTOTAL]; |
| 1247 | u_char ecc_code[ECCTOTAL]; |
| 1248 | u_char oob_data[CONFIG_SYS_NAND_OOBSIZE]; |
| 1249 | int eccsize = CONFIG_SYS_NAND_ECCSIZE; |
| 1250 | int eccbytes = CONFIG_SYS_NAND_ECCBYTES; |
| 1251 | int eccsteps = ECCSTEPS; |
| 1252 | int i; |
| 1253 | uint8_t *p = dst; |
| 1254 | nand_command(block, page, 0, NAND_CMD_READ0); |
| 1255 | |
| 1256 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1257 | if (this->ecc.mode != NAND_ECC_SOFT) |
| 1258 | this->ecc.hwctl(&mtd, NAND_ECC_READ); |
| 1259 | this->read_buf(&mtd, p, eccsize); |
| 1260 | this->ecc.calculate(&mtd, p, &ecc_calc[i]); |
| 1261 | } |
| 1262 | this->read_buf(&mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE); |
| 1263 | |
| 1264 | for (i = 0; i < ECCTOTAL; i++) |
| 1265 | ecc_code[i] = oob_data[nand_ecc_pos[i]]; |
| 1266 | |
| 1267 | eccsteps = ECCSTEPS; |
| 1268 | p = dst; |
| 1269 | |
| 1270 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 1271 | this->ecc.correct(&mtd, p, &ecc_code[i], &ecc_calc[i]); |
| 1272 | |
| 1273 | return 0; |
| 1274 | } |
| 1275 | #else |
| 1276 | static int nand_read_page(int block, int page, void *dst) |
| 1277 | { |
| 1278 | struct nand_chip *this = mtd.priv; |
| 1279 | |
| 1280 | nand_command(block, page, 0, NAND_CMD_READ0); |
| 1281 | atmel_nand_pmecc_read_page(&mtd, this, dst, 0, page); |
| 1282 | |
| 1283 | return 0; |
| 1284 | } |
| 1285 | #endif /* CONFIG_SPL_NAND_ECC */ |
| 1286 | |
| 1287 | int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst) |
| 1288 | { |
| 1289 | unsigned int block, lastblock; |
| 1290 | unsigned int page; |
| 1291 | |
| 1292 | block = offs / CONFIG_SYS_NAND_BLOCK_SIZE; |
| 1293 | lastblock = (offs + size - 1) / CONFIG_SYS_NAND_BLOCK_SIZE; |
| 1294 | page = (offs % CONFIG_SYS_NAND_BLOCK_SIZE) / CONFIG_SYS_NAND_PAGE_SIZE; |
| 1295 | |
| 1296 | while (block <= lastblock) { |
| 1297 | if (!nand_is_bad_block(block)) { |
| 1298 | while (page < CONFIG_SYS_NAND_PAGE_COUNT) { |
| 1299 | nand_read_page(block, page, dst); |
| 1300 | dst += CONFIG_SYS_NAND_PAGE_SIZE; |
| 1301 | page++; |
| 1302 | } |
| 1303 | |
| 1304 | page = 0; |
| 1305 | } else { |
| 1306 | lastblock++; |
| 1307 | } |
| 1308 | |
| 1309 | block++; |
| 1310 | } |
| 1311 | |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | int at91_nand_wait_ready(struct mtd_info *mtd) |
| 1316 | { |
| 1317 | struct nand_chip *this = mtd->priv; |
| 1318 | |
| 1319 | udelay(this->chip_delay); |
| 1320 | |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
| 1324 | int board_nand_init(struct nand_chip *nand) |
| 1325 | { |
| 1326 | int ret = 0; |
| 1327 | |
| 1328 | nand->ecc.mode = NAND_ECC_SOFT; |
| 1329 | #ifdef CONFIG_SYS_NAND_DBW_16 |
| 1330 | nand->options = NAND_BUSWIDTH_16; |
| 1331 | nand->read_buf = nand_read_buf16; |
| 1332 | #else |
| 1333 | nand->read_buf = nand_read_buf; |
| 1334 | #endif |
| 1335 | nand->cmd_ctrl = at91_nand_hwcontrol; |
| 1336 | #ifdef CONFIG_SYS_NAND_READY_PIN |
| 1337 | nand->dev_ready = at91_nand_ready; |
| 1338 | #else |
| 1339 | nand->dev_ready = at91_nand_wait_ready; |
| 1340 | #endif |
| 1341 | nand->chip_delay = 20; |
| 1342 | |
| 1343 | #ifdef CONFIG_ATMEL_NAND_HWECC |
| 1344 | #ifdef CONFIG_ATMEL_NAND_HW_PMECC |
| 1345 | ret = atmel_pmecc_nand_init_params(nand, &mtd); |
| 1346 | #endif |
| 1347 | #endif |
| 1348 | |
| 1349 | return ret; |
| 1350 | } |
| 1351 | |
| 1352 | void nand_init(void) |
| 1353 | { |
| 1354 | mtd.writesize = CONFIG_SYS_NAND_PAGE_SIZE; |
| 1355 | mtd.oobsize = CONFIG_SYS_NAND_OOBSIZE; |
| 1356 | mtd.priv = &nand_chip; |
| 1357 | nand_chip.IO_ADDR_R = (void __iomem *)CONFIG_SYS_NAND_BASE; |
| 1358 | nand_chip.IO_ADDR_W = (void __iomem *)CONFIG_SYS_NAND_BASE; |
| 1359 | board_nand_init(&nand_chip); |
| 1360 | |
| 1361 | #ifdef CONFIG_SPL_NAND_ECC |
| 1362 | if (nand_chip.ecc.mode == NAND_ECC_SOFT) { |
| 1363 | nand_chip.ecc.calculate = nand_calculate_ecc; |
| 1364 | nand_chip.ecc.correct = nand_correct_data; |
| 1365 | } |
| 1366 | #endif |
| 1367 | |
| 1368 | if (nand_chip.select_chip) |
| 1369 | nand_chip.select_chip(&mtd, 0); |
| 1370 | } |
| 1371 | |
| 1372 | void nand_deselect(void) |
| 1373 | { |
| 1374 | if (nand_chip.select_chip) |
| 1375 | nand_chip.select_chip(&mtd, -1); |
| 1376 | } |
| 1377 | |
| 1378 | #else |
| 1379 | |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1380 | #ifndef CONFIG_SYS_NAND_BASE_LIST |
| 1381 | #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE } |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1382 | #endif |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1383 | static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE]; |
| 1384 | static ulong base_addr[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST; |
| 1385 | |
| 1386 | int atmel_nand_chip_init(int devnum, ulong base_addr) |
| 1387 | { |
| 1388 | int ret; |
| 1389 | struct mtd_info *mtd = &nand_info[devnum]; |
| 1390 | struct nand_chip *nand = &nand_chip[devnum]; |
| 1391 | |
| 1392 | mtd->priv = nand; |
| 1393 | nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr; |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1394 | |
Bo Shen | bb5a12f | 2013-08-28 14:54:26 +0000 | [diff] [blame] | 1395 | #ifdef CONFIG_NAND_ECC_BCH |
| 1396 | nand->ecc.mode = NAND_ECC_SOFT_BCH; |
| 1397 | #else |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1398 | nand->ecc.mode = NAND_ECC_SOFT; |
Bo Shen | bb5a12f | 2013-08-28 14:54:26 +0000 | [diff] [blame] | 1399 | #endif |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1400 | #ifdef CONFIG_SYS_NAND_DBW_16 |
| 1401 | nand->options = NAND_BUSWIDTH_16; |
| 1402 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | c9539ba | 2009-03-22 10:22:34 +0100 | [diff] [blame] | 1403 | nand->cmd_ctrl = at91_nand_hwcontrol; |
| 1404 | #ifdef CONFIG_SYS_NAND_READY_PIN |
| 1405 | nand->dev_ready = at91_nand_ready; |
| 1406 | #endif |
Wu, Josh | f9f69b1 | 2013-10-18 17:46:31 +0800 | [diff] [blame] | 1407 | nand->chip_delay = 75; |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1408 | |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1409 | ret = nand_scan_ident(mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL); |
| 1410 | if (ret) |
| 1411 | return ret; |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1412 | |
| 1413 | #ifdef CONFIG_ATMEL_NAND_HWECC |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 1414 | #ifdef CONFIG_ATMEL_NAND_HW_PMECC |
| 1415 | ret = atmel_pmecc_nand_init_params(nand, mtd); |
| 1416 | #else |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1417 | ret = atmel_hwecc_nand_init_param(nand, mtd); |
Wu, Josh | fd3091d | 2012-08-23 00:05:36 +0000 | [diff] [blame] | 1418 | #endif |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1419 | if (ret) |
| 1420 | return ret; |
| 1421 | #endif |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1422 | |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1423 | ret = nand_scan_tail(mtd); |
| 1424 | if (!ret) |
| 1425 | nand_register(devnum); |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1426 | |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1427 | return ret; |
| 1428 | } |
Nikolay Petukhov | e6015ca | 2010-03-19 10:49:27 +0500 | [diff] [blame] | 1429 | |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1430 | void board_nand_init(void) |
| 1431 | { |
| 1432 | int i; |
| 1433 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) |
| 1434 | if (atmel_nand_chip_init(i, base_addr[i])) |
Wu, Josh | a8bd1d4 | 2013-10-18 17:46:34 +0800 | [diff] [blame] | 1435 | dev_err(host->dev, "atmel_nand: Fail to initialize #%d chip", |
Wu, Josh | 6cded6d | 2012-08-23 00:05:34 +0000 | [diff] [blame] | 1436 | i); |
Sergey Lapin | 77e524c | 2008-10-31 12:28:43 +0100 | [diff] [blame] | 1437 | } |
Bo Shen | 9415b87 | 2014-03-03 14:47:16 +0800 | [diff] [blame] | 1438 | #endif /* CONFIG_SPL_BUILD */ |