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