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