blob: 620b6e8ff9a4cfac6745eb6477718ce14a93d05f [file] [log] [blame]
Sergey Lapin10794322008-10-31 12:28:43 +01001/*
2 * (C) Copyright 2007-2008
Stelian Popc9e798d2011-11-01 00:00:39 +01003 * Stelian Pop <stelian@popies.net>
Sergey Lapin10794322008-10-31 12:28:43 +01004 * Lead Tech Design <www.leadtechdesign.com>
5 *
6 * (C) Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
7 *
Wu, Joshbdfd59a2012-08-23 00:05:36 +00008 * Add Programmable Multibit ECC support for various AT91 SoC
9 * (C) Copyright 2012 ATMEL, Hong Xu
10 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020011 * SPDX-License-Identifier: GPL-2.0+
Sergey Lapin10794322008-10-31 12:28:43 +010012 */
13
14#include <common.h>
Andreas Bießmannac45bb12013-11-29 12:13:45 +010015#include <asm/gpio.h>
Sergey Lapin10794322008-10-31 12:28:43 +010016#include <asm/arch/gpio.h>
Sergey Lapin10794322008-10-31 12:28:43 +010017
Wu, Joshddd85972013-07-03 11:11:48 +080018#include <malloc.h>
Sergey Lapin10794322008-10-31 12:28:43 +010019#include <nand.h>
Wu, Joshbdfd59a2012-08-23 00:05:36 +000020#include <watchdog.h>
Heiko Schocherc1ec4062014-10-31 08:31:01 +010021#include <linux/mtd/nand_ecc.h>
Sergey Lapin10794322008-10-31 12:28:43 +010022
Nikolay Petukhov7c27b7b2010-03-19 10:49:27 +050023#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, Joshbdfd59a2012-08-23 00:05:36 +000033#ifdef CONFIG_ATMEL_NAND_HW_PMECC
34
Bo Shen0b0b4f52014-03-03 14:47:16 +080035#ifdef CONFIG_SPL_BUILD
36#undef CONFIG_SYS_NAND_ONFI_DETECTION
37#endif
38
Wu, Joshbdfd59a2012-08-23 00:05:36 +000039struct 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, Joshddd85972013-07-03 11:11:48 +080058 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, Joshbdfd59a2012-08-23 00:05:36 +000065};
66
67static struct atmel_nand_host pmecc_host;
68static 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 */
83static 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
89static 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
105static 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, Joshddd85972013-07-03 11:11:48 +0800117static 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
128static 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, Joshbdfd59a2012-08-23 00:05:36 +0000159static 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, Josh14b3b442014-06-24 18:18:06 +0800168 value = pmecc_readl(host->pmecc, rem_port[sector].rem[i / 2]);
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000169 if (i & 1)
170 value >>= 16;
171 value &= 0xffff;
172 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
173 }
174}
175
176static 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 */
226static 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
382static 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, Josh14b3b442014-06-24 18:18:06 +0800396 pmecc_writel(host->pmerrloc, eldis, PMERRLOC_DISABLE);
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000397
398 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
Wu, Josh14b3b442014-06-24 18:18:06 +0800399 pmecc_writel(host->pmerrloc, sigma[i],
400 smu[(cap + 1) * num + i]);
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000401 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, Josh14b3b442014-06-24 18:18:06 +0800408 pmecc_writel(host->pmerrloc, elcfg, val);
409 pmecc_writel(host->pmerrloc, elen,
410 sector_size * 8 + host->pmecc_degree * cap);
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000411
412 while (--timeout) {
Wu, Josh14b3b442014-06-24 18:18:06 +0800413 if (pmecc_readl(host->pmerrloc, elisr) & PMERRLOC_CALC_DONE)
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000414 break;
415 WATCHDOG_RESET();
416 udelay(1);
417 }
418
419 if (!timeout) {
Wu, Joshc0dc3de2013-10-18 17:46:34 +0800420 dev_err(host->dev, "atmel_nand : Timeout to calculate PMECC error location\n");
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000421 return -1;
422 }
423
Wu, Josh14b3b442014-06-24 18:18:06 +0800424 roots_nbr = (pmecc_readl(host->pmerrloc, elisr) & PMERRLOC_ERR_NUM_MASK)
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000425 >> 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
435static 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, Josh14b3b442014-06-24 18:18:06 +0800448 tmp = pmecc_readl(host->pmerrloc, el[i]) - 1;
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000449 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, Joshc55cc572013-10-18 17:46:33 +0800460 dev_dbg(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000461 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, Joshc55cc572013-10-18 17:46:33 +0800470 dev_dbg(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000471 pos, bit_pos, err_byte, ecc[tmp]);
472 }
473
474 i++;
475 err_nbr--;
476 }
477
478 return;
479}
480
481static 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
496normal_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, Joshc0dc3de2013-10-18 17:46:34 +0800508 dev_err(host->dev, "PMECC: Too many errors\n");
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000509 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
523static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
Sergey Lapindfe64e22013-01-14 03:46:50 +0000524 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000525{
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, Joshc0dc3de2013-10-18 17:46:34 +0800552 dev_err(host->dev, "atmel_nand : Timeout to read PMECC page\n");
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000553 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 Lapindfe64e22013-01-14 03:46:50 +0000564static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
565 struct nand_chip *chip, const uint8_t *buf,
566 int oob_required)
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000567{
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, Joshc0dc3de2013-10-18 17:46:34 +0800592 dev_err(host->dev, "atmel_nand : Timeout to read PMECC status, fail to write PMECC in oob\n");
Sergey Lapindfe64e22013-01-14 03:46:50 +0000593 goto out;
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000594 }
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, Josh14b3b442014-06-24 18:18:06 +0800602 pmecc_readb(host->pmecc, ecc_port[i].ecc[j]);
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000603 }
604 }
605 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapindfe64e22013-01-14 03:46:50 +0000606out:
607 return 0;
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000608}
609
610static 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, Josha07d2292013-07-04 15:36:23 +0800673#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 */
683static 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 */
715static 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
Josh Wu7df44862014-11-10 15:24:00 +0800766#if defined(NO_GALOIS_TABLE_IN_ROM)
767static uint16_t *pmecc_galois_table;
768static inline int deg(unsigned int poly)
769{
770 /* polynomial degree is the most-significant bit index */
771 return fls(poly) - 1;
772}
773
774static int build_gf_tables(int mm, unsigned int poly,
775 int16_t *index_of, int16_t *alpha_to)
776{
777 unsigned int i, x = 1;
778 const unsigned int k = 1 << deg(poly);
779 unsigned int nn = (1 << mm) - 1;
780
781 /* primitive polynomial must be of degree m */
782 if (k != (1u << mm))
783 return -EINVAL;
784
785 for (i = 0; i < nn; i++) {
786 alpha_to[i] = x;
787 index_of[x] = i;
788 if (i && (x == 1))
789 /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
790 return -EINVAL;
791 x <<= 1;
792 if (x & k)
793 x ^= poly;
794 }
795
796 alpha_to[nn] = 1;
797 index_of[0] = 0;
798
799 return 0;
800}
801
802static uint16_t *create_lookup_table(int sector_size)
803{
804 int degree = (sector_size == 512) ?
805 PMECC_GF_DIMENSION_13 :
806 PMECC_GF_DIMENSION_14;
807 unsigned int poly = (sector_size == 512) ?
808 PMECC_GF_13_PRIMITIVE_POLY :
809 PMECC_GF_14_PRIMITIVE_POLY;
810 int table_size = (sector_size == 512) ?
811 PMECC_INDEX_TABLE_SIZE_512 :
812 PMECC_INDEX_TABLE_SIZE_1024;
813
814 int16_t *addr = kzalloc(2 * table_size * sizeof(uint16_t), GFP_KERNEL);
815 if (addr && build_gf_tables(degree, poly, addr, addr + table_size))
816 return NULL;
817
818 return (uint16_t *)addr;
819}
820#endif
821
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000822static int atmel_pmecc_nand_init_params(struct nand_chip *nand,
823 struct mtd_info *mtd)
824{
825 struct atmel_nand_host *host;
826 int cap, sector_size;
827
828 host = nand->priv = &pmecc_host;
829
830 nand->ecc.mode = NAND_ECC_HW;
831 nand->ecc.calculate = NULL;
832 nand->ecc.correct = NULL;
833 nand->ecc.hwctl = NULL;
834
Wu, Josha07d2292013-07-04 15:36:23 +0800835#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
836 host->pmecc_corr_cap = host->pmecc_sector_size = 0;
837
838#ifdef CONFIG_PMECC_CAP
839 host->pmecc_corr_cap = CONFIG_PMECC_CAP;
840#endif
841#ifdef CONFIG_PMECC_SECTOR_SIZE
842 host->pmecc_sector_size = CONFIG_PMECC_SECTOR_SIZE;
843#endif
844 /* Get ECC requirement of ONFI parameters. And if CONFIG_PMECC_CAP or
845 * CONFIG_PMECC_SECTOR_SIZE not defined, then use ecc_bits, sector_size
846 * from ONFI.
847 */
848 if (pmecc_choose_ecc(host, nand, &cap, &sector_size)) {
849 dev_err(host->dev, "The NAND flash's ECC requirement(ecc_bits: %d, sector_size: %d) are not support!",
850 cap, sector_size);
851 return -EINVAL;
852 }
853
854 if (cap > host->pmecc_corr_cap)
855 dev_info(host->dev, "WARNING: Using different ecc correct bits(%d bit) from Nand ONFI ECC reqirement (%d bit).\n",
856 host->pmecc_corr_cap, cap);
857 if (sector_size < host->pmecc_sector_size)
858 dev_info(host->dev, "WARNING: Using different ecc correct sector size (%d bytes) from Nand ONFI ECC reqirement (%d bytes).\n",
859 host->pmecc_sector_size, sector_size);
860#else /* CONFIG_SYS_NAND_ONFI_DETECTION */
861 host->pmecc_corr_cap = CONFIG_PMECC_CAP;
862 host->pmecc_sector_size = CONFIG_PMECC_SECTOR_SIZE;
863#endif
864
865 cap = host->pmecc_corr_cap;
866 sector_size = host->pmecc_sector_size;
867
868 /* TODO: need check whether cap & sector_size is validate */
Josh Wu7df44862014-11-10 15:24:00 +0800869#if defined(NO_GALOIS_TABLE_IN_ROM)
870 /*
871 * As pmecc_rom_base is the begin of the gallois field table, So the
872 * index offset just set as 0.
873 */
874 host->pmecc_index_table_offset = 0;
875#else
Wu, Joshb2d96dc2013-07-03 11:11:45 +0800876 if (host->pmecc_sector_size == 512)
877 host->pmecc_index_table_offset = ATMEL_PMECC_INDEX_OFFSET_512;
878 else
879 host->pmecc_index_table_offset = ATMEL_PMECC_INDEX_OFFSET_1024;
Josh Wu7df44862014-11-10 15:24:00 +0800880#endif
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000881
Wu, Joshb9c83c62012-09-09 23:45:49 +0000882 MTDDEBUG(MTD_DEBUG_LEVEL1,
883 "Initialize PMECC params, cap: %d, sector: %d\n",
884 cap, sector_size);
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000885
886 host->pmecc = (struct pmecc_regs __iomem *) ATMEL_BASE_PMECC;
887 host->pmerrloc = (struct pmecc_errloc_regs __iomem *)
888 ATMEL_BASE_PMERRLOC;
Josh Wu7df44862014-11-10 15:24:00 +0800889#if defined(NO_GALOIS_TABLE_IN_ROM)
890 pmecc_galois_table = create_lookup_table(host->pmecc_sector_size);
891 if (!pmecc_galois_table) {
892 dev_err(host->dev, "out of memory\n");
893 return -ENOMEM;
894 }
895
896 host->pmecc_rom_base = (void __iomem *)pmecc_galois_table;
897#else
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000898 host->pmecc_rom_base = (void __iomem *) ATMEL_BASE_ROM;
Josh Wu7df44862014-11-10 15:24:00 +0800899#endif
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000900
901 /* ECC is calculated for the whole page (1 step) */
902 nand->ecc.size = mtd->writesize;
903
904 /* set ECC page size and oob layout */
905 switch (mtd->writesize) {
906 case 2048:
907 case 4096:
Wu, Josh16dddef2013-10-18 17:46:31 +0800908 case 8192:
Wu, Josh1bd3e2a2013-08-23 15:09:05 +0800909 host->pmecc_degree = (sector_size == 512) ?
910 PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000911 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
912 host->pmecc_sector_number = mtd->writesize / sector_size;
913 host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
914 cap, sector_size);
915 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
916 host->pmecc_index_of = host->pmecc_rom_base +
917 host->pmecc_index_table_offset;
918
919 nand->ecc.steps = 1;
920 nand->ecc.bytes = host->pmecc_bytes_per_sector *
921 host->pmecc_sector_number;
Wu, Josh16dddef2013-10-18 17:46:31 +0800922
923 if (nand->ecc.bytes > MTD_MAX_ECCPOS_ENTRIES_LARGE) {
924 dev_err(host->dev, "too large eccpos entries. max support ecc.bytes is %d\n",
925 MTD_MAX_ECCPOS_ENTRIES_LARGE);
926 return -EINVAL;
927 }
928
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000929 if (nand->ecc.bytes > mtd->oobsize - 2) {
Wu, Joshc0dc3de2013-10-18 17:46:34 +0800930 dev_err(host->dev, "No room for ECC bytes\n");
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000931 return -EINVAL;
932 }
933 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
934 mtd->oobsize,
935 nand->ecc.bytes);
936 nand->ecc.layout = &atmel_pmecc_oobinfo;
937 break;
938 case 512:
939 case 1024:
940 /* TODO */
Wu, Joshc0dc3de2013-10-18 17:46:34 +0800941 dev_err(host->dev, "Unsupported page size for PMECC, use Software ECC\n");
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000942 default:
943 /* page size not handled by HW ECC */
944 /* switching back to soft ECC */
945 nand->ecc.mode = NAND_ECC_SOFT;
946 nand->ecc.read_page = NULL;
947 nand->ecc.postpad = 0;
948 nand->ecc.prepad = 0;
949 nand->ecc.bytes = 0;
950 return 0;
951 }
952
Wu, Joshddd85972013-07-03 11:11:48 +0800953 /* Allocate data for PMECC computation */
954 if (pmecc_data_alloc(host)) {
955 dev_err(host->dev, "Cannot allocate memory for PMECC computation!\n");
956 return -ENOMEM;
957 }
958
Boris BREZILLONd357b942014-09-02 10:23:09 +0200959 nand->options |= NAND_NO_SUBPAGE_WRITE;
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000960 nand->ecc.read_page = atmel_nand_pmecc_read_page;
961 nand->ecc.write_page = atmel_nand_pmecc_write_page;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000962 nand->ecc.strength = cap;
Wu, Joshbdfd59a2012-08-23 00:05:36 +0000963
964 atmel_pmecc_core_init(mtd);
965
966 return 0;
967}
968
969#else
970
Nikolay Petukhov7c27b7b2010-03-19 10:49:27 +0500971/* oob layout for large page size
972 * bad block info is on bytes 0 and 1
973 * the bytes have to be consecutives to avoid
974 * several NAND_CMD_RNDOUT during read
975 */
976static struct nand_ecclayout atmel_oobinfo_large = {
977 .eccbytes = 4,
978 .eccpos = {60, 61, 62, 63},
979 .oobfree = {
980 {2, 58}
981 },
982};
983
984/* oob layout for small page size
985 * bad block info is on bytes 4 and 5
986 * the bytes have to be consecutives to avoid
987 * several NAND_CMD_RNDOUT during read
988 */
989static struct nand_ecclayout atmel_oobinfo_small = {
990 .eccbytes = 4,
991 .eccpos = {0, 1, 2, 3},
992 .oobfree = {
993 {6, 10}
994 },
995};
996
997/*
998 * Calculate HW ECC
999 *
1000 * function called after a write
1001 *
1002 * mtd: MTD block structure
1003 * dat: raw data (unused)
1004 * ecc_code: buffer for ECC
1005 */
1006static int atmel_nand_calculate(struct mtd_info *mtd,
1007 const u_char *dat, unsigned char *ecc_code)
1008{
Nikolay Petukhov7c27b7b2010-03-19 10:49:27 +05001009 unsigned int ecc_value;
1010
1011 /* get the first 2 ECC bytes */
1012 ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR);
1013
1014 ecc_code[0] = ecc_value & 0xFF;
1015 ecc_code[1] = (ecc_value >> 8) & 0xFF;
1016
1017 /* get the last 2 ECC bytes */
1018 ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, NPR) & ATMEL_ECC_NPARITY;
1019
1020 ecc_code[2] = ecc_value & 0xFF;
1021 ecc_code[3] = (ecc_value >> 8) & 0xFF;
1022
1023 return 0;
1024}
1025
1026/*
1027 * HW ECC read page function
1028 *
1029 * mtd: mtd info structure
1030 * chip: nand chip info structure
1031 * buf: buffer to store read data
Sergey Lapindfe64e22013-01-14 03:46:50 +00001032 * oob_required: caller expects OOB data read to chip->oob_poi
Nikolay Petukhov7c27b7b2010-03-19 10:49:27 +05001033 */
Sergey Lapindfe64e22013-01-14 03:46:50 +00001034static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1035 uint8_t *buf, int oob_required, int page)
Nikolay Petukhov7c27b7b2010-03-19 10:49:27 +05001036{
1037 int eccsize = chip->ecc.size;
1038 int eccbytes = chip->ecc.bytes;
1039 uint32_t *eccpos = chip->ecc.layout->eccpos;
1040 uint8_t *p = buf;
1041 uint8_t *oob = chip->oob_poi;
1042 uint8_t *ecc_pos;
1043 int stat;
1044
1045 /* read the page */
1046 chip->read_buf(mtd, p, eccsize);
1047
1048 /* move to ECC position if needed */
1049 if (eccpos[0] != 0) {
1050 /* This only works on large pages
1051 * because the ECC controller waits for
1052 * NAND_CMD_RNDOUTSTART after the
1053 * NAND_CMD_RNDOUT.
1054 * anyway, for small pages, the eccpos[0] == 0
1055 */
1056 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1057 mtd->writesize + eccpos[0], -1);
1058 }
1059
1060 /* the ECC controller needs to read the ECC just after the data */
1061 ecc_pos = oob + eccpos[0];
1062 chip->read_buf(mtd, ecc_pos, eccbytes);
1063
1064 /* check if there's an error */
1065 stat = chip->ecc.correct(mtd, p, oob, NULL);
1066
1067 if (stat < 0)
1068 mtd->ecc_stats.failed++;
1069 else
1070 mtd->ecc_stats.corrected += stat;
1071
1072 /* get back to oob start (end of page) */
1073 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1074
1075 /* read the oob */
1076 chip->read_buf(mtd, oob, mtd->oobsize);
1077
1078 return 0;
1079}
1080
1081/*
1082 * HW ECC Correction
1083 *
1084 * function called after a read
1085 *
1086 * mtd: MTD block structure
1087 * dat: raw data read from the chip
1088 * read_ecc: ECC from the chip (unused)
1089 * isnull: unused
1090 *
1091 * Detect and correct a 1 bit error for a page
1092 */
1093static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
1094 u_char *read_ecc, u_char *isnull)
1095{
1096 struct nand_chip *nand_chip = mtd->priv;
Wu, Joshae797942012-08-23 00:05:35 +00001097 unsigned int ecc_status;
Nikolay Petukhov7c27b7b2010-03-19 10:49:27 +05001098 unsigned int ecc_word, ecc_bit;
1099
1100 /* get the status from the Status Register */
1101 ecc_status = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, SR);
1102
1103 /* if there's no error */
1104 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
1105 return 0;
1106
1107 /* get error bit offset (4 bits) */
1108 ecc_bit = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_BITADDR;
1109 /* get word address (12 bits) */
1110 ecc_word = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_WORDADDR;
1111 ecc_word >>= 4;
1112
1113 /* if there are multiple errors */
1114 if (ecc_status & ATMEL_ECC_MULERR) {
1115 /* check if it is a freshly erased block
1116 * (filled with 0xff) */
1117 if ((ecc_bit == ATMEL_ECC_BITADDR)
1118 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
1119 /* the block has just been erased, return OK */
1120 return 0;
1121 }
1122 /* it doesn't seems to be a freshly
1123 * erased block.
1124 * We can't correct so many errors */
Wu, Joshc0dc3de2013-10-18 17:46:34 +08001125 dev_warn(host->dev, "atmel_nand : multiple errors detected."
Nikolay Petukhov7c27b7b2010-03-19 10:49:27 +05001126 " Unable to correct.\n");
1127 return -EIO;
1128 }
1129
1130 /* if there's a single bit error : we can correct it */
1131 if (ecc_status & ATMEL_ECC_ECCERR) {
1132 /* there's nothing much to do here.
1133 * the bit error is on the ECC itself.
1134 */
Wu, Joshc0dc3de2013-10-18 17:46:34 +08001135 dev_warn(host->dev, "atmel_nand : one bit error on ECC code."
Nikolay Petukhov7c27b7b2010-03-19 10:49:27 +05001136 " Nothing to correct\n");
1137 return 0;
1138 }
1139
Wu, Joshc0dc3de2013-10-18 17:46:34 +08001140 dev_warn(host->dev, "atmel_nand : one bit error on data."
Nikolay Petukhov7c27b7b2010-03-19 10:49:27 +05001141 " (word offset in the page :"
1142 " 0x%x bit offset : 0x%x)\n",
1143 ecc_word, ecc_bit);
1144 /* correct the error */
1145 if (nand_chip->options & NAND_BUSWIDTH_16) {
1146 /* 16 bits words */
1147 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1148 } else {
1149 /* 8 bits words */
1150 dat[ecc_word] ^= (1 << ecc_bit);
1151 }
Wu, Joshc0dc3de2013-10-18 17:46:34 +08001152 dev_warn(host->dev, "atmel_nand : error corrected\n");
Nikolay Petukhov7c27b7b2010-03-19 10:49:27 +05001153 return 1;
1154}
1155
1156/*
1157 * Enable HW ECC : unused on most chips
1158 */
1159static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1160{
1161}
Wu, Joshfe2185e2012-08-23 00:05:34 +00001162
1163int atmel_hwecc_nand_init_param(struct nand_chip *nand, struct mtd_info *mtd)
1164{
1165 nand->ecc.mode = NAND_ECC_HW;
1166 nand->ecc.calculate = atmel_nand_calculate;
1167 nand->ecc.correct = atmel_nand_correct;
1168 nand->ecc.hwctl = atmel_nand_hwctl;
1169 nand->ecc.read_page = atmel_nand_read_page;
1170 nand->ecc.bytes = 4;
1171
1172 if (nand->ecc.mode == NAND_ECC_HW) {
1173 /* ECC is calculated for the whole page (1 step) */
1174 nand->ecc.size = mtd->writesize;
1175
1176 /* set ECC page size and oob layout */
1177 switch (mtd->writesize) {
1178 case 512:
1179 nand->ecc.layout = &atmel_oobinfo_small;
1180 ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
1181 ATMEL_ECC_PAGESIZE_528);
1182 break;
1183 case 1024:
1184 nand->ecc.layout = &atmel_oobinfo_large;
1185 ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
1186 ATMEL_ECC_PAGESIZE_1056);
1187 break;
1188 case 2048:
1189 nand->ecc.layout = &atmel_oobinfo_large;
1190 ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
1191 ATMEL_ECC_PAGESIZE_2112);
1192 break;
1193 case 4096:
1194 nand->ecc.layout = &atmel_oobinfo_large;
1195 ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
1196 ATMEL_ECC_PAGESIZE_4224);
1197 break;
1198 default:
1199 /* page size not handled by HW ECC */
1200 /* switching back to soft ECC */
1201 nand->ecc.mode = NAND_ECC_SOFT;
1202 nand->ecc.calculate = NULL;
1203 nand->ecc.correct = NULL;
1204 nand->ecc.hwctl = NULL;
1205 nand->ecc.read_page = NULL;
1206 nand->ecc.postpad = 0;
1207 nand->ecc.prepad = 0;
1208 nand->ecc.bytes = 0;
1209 break;
1210 }
1211 }
1212
1213 return 0;
1214}
1215
Wu, Joshbdfd59a2012-08-23 00:05:36 +00001216#endif /* CONFIG_ATMEL_NAND_HW_PMECC */
1217
1218#endif /* CONFIG_ATMEL_NAND_HWECC */
Nikolay Petukhov7c27b7b2010-03-19 10:49:27 +05001219
Jean-Christophe PLAGNIOL-VILLARD74c076d2009-03-22 10:22:34 +01001220static void at91_nand_hwcontrol(struct mtd_info *mtd,
Sergey Lapin10794322008-10-31 12:28:43 +01001221 int cmd, unsigned int ctrl)
1222{
1223 struct nand_chip *this = mtd->priv;
1224
1225 if (ctrl & NAND_CTRL_CHANGE) {
1226 ulong IO_ADDR_W = (ulong) this->IO_ADDR_W;
Jean-Christophe PLAGNIOL-VILLARD74c076d2009-03-22 10:22:34 +01001227 IO_ADDR_W &= ~(CONFIG_SYS_NAND_MASK_ALE
1228 | CONFIG_SYS_NAND_MASK_CLE);
Sergey Lapin10794322008-10-31 12:28:43 +01001229
1230 if (ctrl & NAND_CLE)
Jean-Christophe PLAGNIOL-VILLARD74c076d2009-03-22 10:22:34 +01001231 IO_ADDR_W |= CONFIG_SYS_NAND_MASK_CLE;
Sergey Lapin10794322008-10-31 12:28:43 +01001232 if (ctrl & NAND_ALE)
Jean-Christophe PLAGNIOL-VILLARD74c076d2009-03-22 10:22:34 +01001233 IO_ADDR_W |= CONFIG_SYS_NAND_MASK_ALE;
Sergey Lapin10794322008-10-31 12:28:43 +01001234
michael67a490d2011-03-14 21:16:38 +00001235#ifdef CONFIG_SYS_NAND_ENABLE_PIN
Andreas Bießmannac45bb12013-11-29 12:13:45 +01001236 gpio_set_value(CONFIG_SYS_NAND_ENABLE_PIN, !(ctrl & NAND_NCE));
michael67a490d2011-03-14 21:16:38 +00001237#endif
Sergey Lapin10794322008-10-31 12:28:43 +01001238 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
Jean-Christophe PLAGNIOL-VILLARD74c076d2009-03-22 10:22:34 +01001245#ifdef CONFIG_SYS_NAND_READY_PIN
1246static int at91_nand_ready(struct mtd_info *mtd)
Sergey Lapin10794322008-10-31 12:28:43 +01001247{
Andreas Bießmannac45bb12013-11-29 12:13:45 +01001248 return gpio_get_value(CONFIG_SYS_NAND_READY_PIN);
Sergey Lapin10794322008-10-31 12:28:43 +01001249}
Jean-Christophe PLAGNIOL-VILLARD74c076d2009-03-22 10:22:34 +01001250#endif
Sergey Lapin10794322008-10-31 12:28:43 +01001251
Bo Shen0b0b4f52014-03-03 14:47:16 +08001252#ifdef CONFIG_SPL_BUILD
1253/* The following code is for SPL */
1254static nand_info_t mtd;
1255static struct nand_chip nand_chip;
1256
1257static int nand_command(int block, int page, uint32_t offs, u8 cmd)
1258{
1259 struct nand_chip *this = mtd.priv;
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
Heiko Schocher667af362014-10-31 08:31:03 +01001264 while (!this->dev_ready(&mtd))
Bo Shen0b0b4f52014-03-03 14:47:16 +08001265 ;
1266
1267 if (cmd == NAND_CMD_READOOB) {
1268 offs += CONFIG_SYS_NAND_PAGE_SIZE;
1269 cmd = NAND_CMD_READ0;
1270 }
1271
1272 hwctrl(&mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
1273
Brian Norris27ce9e42014-05-06 00:46:17 +05301274 if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd))
Bo Shen0b0b4f52014-03-03 14:47:16 +08001275 offs >>= 1;
1276
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);
1281#ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
1282 hwctrl(&mtd, (page_addr >> 16) & 0x0f, NAND_CTRL_ALE);
1283#endif
1284 hwctrl(&mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
1285
1286 hwctrl(&mtd, NAND_CMD_READSTART, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
1287 hwctrl(&mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
1288
Heiko Schocher667af362014-10-31 08:31:03 +01001289 while (!this->dev_ready(&mtd))
Bo Shen0b0b4f52014-03-03 14:47:16 +08001290 ;
1291
1292 return 0;
1293}
1294
1295static int nand_is_bad_block(int block)
1296{
1297 struct nand_chip *this = mtd.priv;
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
1313static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
1314#define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \
1315 CONFIG_SYS_NAND_ECCSIZE)
1316#define ECCTOTAL (ECCSTEPS * CONFIG_SYS_NAND_ECCBYTES)
1317
1318static int nand_read_page(int block, int page, void *dst)
1319{
1320 struct nand_chip *this = mtd.priv;
1321 u_char ecc_calc[ECCTOTAL];
1322 u_char ecc_code[ECCTOTAL];
1323 u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
1324 int eccsize = CONFIG_SYS_NAND_ECCSIZE;
1325 int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
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)
1333 this->ecc.hwctl(&mtd, NAND_ECC_READ);
1334 this->read_buf(&mtd, p, eccsize);
1335 this->ecc.calculate(&mtd, p, &ecc_calc[i]);
1336 }
1337 this->read_buf(&mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
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)
1346 this->ecc.correct(&mtd, p, &ecc_code[i], &ecc_calc[i]);
1347
1348 return 0;
1349}
Heiko Schocher4dfd3602014-10-31 08:31:02 +01001350
1351int spl_nand_erase_one(int block, int page)
1352{
1353 struct nand_chip *this = mtd.priv;
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)
1359 nand_chip.select_chip(&mtd, 0);
1360
1361 page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
1362 hwctrl(&mtd, NAND_CMD_ERASE1, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
1363 /* Row address */
1364 hwctrl(&mtd, (page_addr & 0xff), NAND_CTRL_ALE | NAND_CTRL_CHANGE);
1365 hwctrl(&mtd, ((page_addr >> 8) & 0xff),
1366 NAND_CTRL_ALE | NAND_CTRL_CHANGE);
1367#ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
1368 /* One more address cycle for devices > 128MiB */
1369 hwctrl(&mtd, (page_addr >> 16) & 0x0f,
1370 NAND_CTRL_ALE | NAND_CTRL_CHANGE);
1371#endif
1372
1373 hwctrl(&mtd, NAND_CMD_ERASE2, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
1374 udelay(2000);
1375
1376 while (!this->dev_ready(&mtd))
1377 ;
1378
1379 nand_deselect();
1380
1381 return 0;
1382}
Bo Shen0b0b4f52014-03-03 14:47:16 +08001383#else
1384static int nand_read_page(int block, int page, void *dst)
1385{
1386 struct nand_chip *this = mtd.priv;
1387
1388 nand_command(block, page, 0, NAND_CMD_READ0);
1389 atmel_nand_pmecc_read_page(&mtd, this, dst, 0, page);
1390
1391 return 0;
1392}
1393#endif /* CONFIG_SPL_NAND_ECC */
1394
1395int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
1396{
1397 unsigned int block, lastblock;
1398 unsigned int page;
1399
1400 block = offs / CONFIG_SYS_NAND_BLOCK_SIZE;
1401 lastblock = (offs + size - 1) / CONFIG_SYS_NAND_BLOCK_SIZE;
1402 page = (offs % CONFIG_SYS_NAND_BLOCK_SIZE) / CONFIG_SYS_NAND_PAGE_SIZE;
1403
1404 while (block <= lastblock) {
1405 if (!nand_is_bad_block(block)) {
1406 while (page < CONFIG_SYS_NAND_PAGE_COUNT) {
1407 nand_read_page(block, page, dst);
1408 dst += CONFIG_SYS_NAND_PAGE_SIZE;
1409 page++;
1410 }
1411
1412 page = 0;
1413 } else {
1414 lastblock++;
1415 }
1416
1417 block++;
1418 }
1419
1420 return 0;
1421}
1422
1423int at91_nand_wait_ready(struct mtd_info *mtd)
1424{
1425 struct nand_chip *this = mtd->priv;
1426
1427 udelay(this->chip_delay);
1428
Heiko Schocher667af362014-10-31 08:31:03 +01001429 return 1;
Bo Shen0b0b4f52014-03-03 14:47:16 +08001430}
1431
1432int board_nand_init(struct nand_chip *nand)
1433{
1434 int ret = 0;
1435
1436 nand->ecc.mode = NAND_ECC_SOFT;
1437#ifdef CONFIG_SYS_NAND_DBW_16
1438 nand->options = NAND_BUSWIDTH_16;
1439 nand->read_buf = nand_read_buf16;
1440#else
1441 nand->read_buf = nand_read_buf;
1442#endif
1443 nand->cmd_ctrl = at91_nand_hwcontrol;
1444#ifdef CONFIG_SYS_NAND_READY_PIN
1445 nand->dev_ready = at91_nand_ready;
1446#else
1447 nand->dev_ready = at91_nand_wait_ready;
1448#endif
1449 nand->chip_delay = 20;
1450
1451#ifdef CONFIG_ATMEL_NAND_HWECC
1452#ifdef CONFIG_ATMEL_NAND_HW_PMECC
1453 ret = atmel_pmecc_nand_init_params(nand, &mtd);
1454#endif
1455#endif
1456
1457 return ret;
1458}
1459
1460void nand_init(void)
1461{
1462 mtd.writesize = CONFIG_SYS_NAND_PAGE_SIZE;
1463 mtd.oobsize = CONFIG_SYS_NAND_OOBSIZE;
1464 mtd.priv = &nand_chip;
1465 nand_chip.IO_ADDR_R = (void __iomem *)CONFIG_SYS_NAND_BASE;
1466 nand_chip.IO_ADDR_W = (void __iomem *)CONFIG_SYS_NAND_BASE;
1467 board_nand_init(&nand_chip);
1468
1469#ifdef CONFIG_SPL_NAND_ECC
1470 if (nand_chip.ecc.mode == NAND_ECC_SOFT) {
1471 nand_chip.ecc.calculate = nand_calculate_ecc;
1472 nand_chip.ecc.correct = nand_correct_data;
1473 }
1474#endif
1475
1476 if (nand_chip.select_chip)
1477 nand_chip.select_chip(&mtd, 0);
1478}
1479
1480void nand_deselect(void)
1481{
1482 if (nand_chip.select_chip)
1483 nand_chip.select_chip(&mtd, -1);
1484}
1485
1486#else
1487
Wu, Joshfe2185e2012-08-23 00:05:34 +00001488#ifndef CONFIG_SYS_NAND_BASE_LIST
1489#define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
Nikolay Petukhov7c27b7b2010-03-19 10:49:27 +05001490#endif
Wu, Joshfe2185e2012-08-23 00:05:34 +00001491static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
1492static ulong base_addr[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
1493
1494int atmel_nand_chip_init(int devnum, ulong base_addr)
1495{
1496 int ret;
1497 struct mtd_info *mtd = &nand_info[devnum];
1498 struct nand_chip *nand = &nand_chip[devnum];
1499
1500 mtd->priv = nand;
1501 nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
Nikolay Petukhov7c27b7b2010-03-19 10:49:27 +05001502
Bo Shen7604a3f2013-08-28 14:54:26 +00001503#ifdef CONFIG_NAND_ECC_BCH
1504 nand->ecc.mode = NAND_ECC_SOFT_BCH;
1505#else
Sergey Lapin10794322008-10-31 12:28:43 +01001506 nand->ecc.mode = NAND_ECC_SOFT;
Bo Shen7604a3f2013-08-28 14:54:26 +00001507#endif
Sergey Lapin10794322008-10-31 12:28:43 +01001508#ifdef CONFIG_SYS_NAND_DBW_16
1509 nand->options = NAND_BUSWIDTH_16;
1510#endif
Jean-Christophe PLAGNIOL-VILLARD74c076d2009-03-22 10:22:34 +01001511 nand->cmd_ctrl = at91_nand_hwcontrol;
1512#ifdef CONFIG_SYS_NAND_READY_PIN
1513 nand->dev_ready = at91_nand_ready;
1514#endif
Wu, Josh16dddef2013-10-18 17:46:31 +08001515 nand->chip_delay = 75;
Sergey Lapin10794322008-10-31 12:28:43 +01001516
Wu, Joshfe2185e2012-08-23 00:05:34 +00001517 ret = nand_scan_ident(mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL);
1518 if (ret)
1519 return ret;
Nikolay Petukhov7c27b7b2010-03-19 10:49:27 +05001520
1521#ifdef CONFIG_ATMEL_NAND_HWECC
Wu, Joshbdfd59a2012-08-23 00:05:36 +00001522#ifdef CONFIG_ATMEL_NAND_HW_PMECC
1523 ret = atmel_pmecc_nand_init_params(nand, mtd);
1524#else
Wu, Joshfe2185e2012-08-23 00:05:34 +00001525 ret = atmel_hwecc_nand_init_param(nand, mtd);
Wu, Joshbdfd59a2012-08-23 00:05:36 +00001526#endif
Wu, Joshfe2185e2012-08-23 00:05:34 +00001527 if (ret)
1528 return ret;
Nikolay Petukhov7c27b7b2010-03-19 10:49:27 +05001529#endif
1530
Wu, Joshfe2185e2012-08-23 00:05:34 +00001531 ret = nand_scan_tail(mtd);
1532 if (!ret)
1533 nand_register(devnum);
1534
1535 return ret;
1536}
1537
1538void board_nand_init(void)
1539{
1540 int i;
1541 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
1542 if (atmel_nand_chip_init(i, base_addr[i]))
Wu, Joshc0dc3de2013-10-18 17:46:34 +08001543 dev_err(host->dev, "atmel_nand: Fail to initialize #%d chip",
Wu, Joshfe2185e2012-08-23 00:05:34 +00001544 i);
Sergey Lapin10794322008-10-31 12:28:43 +01001545}
Bo Shen0b0b4f52014-03-03 14:47:16 +08001546#endif /* CONFIG_SPL_BUILD */