2 * (C) Copyright 2007-2008
3 * Stelian Pop <stelian@popies.net>
4 * Lead Tech Design <www.leadtechdesign.com>
6 * (C) Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
8 * Add Programmable Multibit ECC support for various AT91 SoC
9 * (C) Copyright 2012 ATMEL, Hong Xu
11 * See file CREDITS for list of people who contributed to this
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 #include <asm/arch/hardware.h>
32 #include <asm/arch/gpio.h>
33 #include <asm/arch/at91_pio.h>
38 #ifdef CONFIG_ATMEL_NAND_HWECC
40 /* Register access macros */
41 #define ecc_readl(add, reg) \
42 readl(AT91_BASE_SYS + add + ATMEL_ECC_##reg)
43 #define ecc_writel(add, reg, value) \
44 writel((value), AT91_BASE_SYS + add + ATMEL_ECC_##reg)
46 #include "atmel_nand_ecc.h" /* Hardware ECC registers */
48 #ifdef CONFIG_ATMEL_NAND_HW_PMECC
50 struct atmel_nand_host {
51 struct pmecc_regs __iomem *pmecc;
52 struct pmecc_errloc_regs __iomem *pmerrloc;
53 void __iomem *pmecc_rom_base;
56 u16 pmecc_sector_size;
57 u32 pmecc_index_table_offset;
59 int pmecc_bytes_per_sector;
60 int pmecc_sector_number;
61 int pmecc_degree; /* Degree of remainders */
62 int pmecc_cw_len; /* Length of codeword */
64 /* lookup table for alpha_to and index_of */
65 void __iomem *pmecc_alpha_to;
66 void __iomem *pmecc_index_of;
68 /* data for pmecc computation */
69 int16_t pmecc_smu[(CONFIG_PMECC_CAP + 2) * (2 * CONFIG_PMECC_CAP + 1)];
70 int16_t pmecc_partial_syn[2 * CONFIG_PMECC_CAP + 1];
71 int16_t pmecc_si[2 * CONFIG_PMECC_CAP + 1];
72 int16_t pmecc_lmu[CONFIG_PMECC_CAP + 1]; /* polynomal order */
73 int pmecc_mu[CONFIG_PMECC_CAP + 1];
74 int pmecc_dmu[CONFIG_PMECC_CAP + 1];
75 int pmecc_delta[CONFIG_PMECC_CAP + 1];
78 static struct atmel_nand_host pmecc_host;
79 static struct nand_ecclayout atmel_pmecc_oobinfo;
82 * Return number of ecc bytes per sector according to sector size and
83 * correction capability
85 * Following table shows what at91 PMECC supported:
86 * Correction Capability Sector_512_bytes Sector_1024_bytes
87 * ===================== ================ =================
88 * 2-bits 4-bytes 4-bytes
89 * 4-bits 7-bytes 7-bytes
90 * 8-bits 13-bytes 14-bytes
91 * 12-bits 20-bytes 21-bytes
92 * 24-bits 39-bytes 42-bytes
94 static int pmecc_get_ecc_bytes(int cap, int sector_size)
96 int m = 12 + sector_size / 512;
97 return (m * cap + 7) / 8;
100 static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
101 int oobsize, int ecc_len)
105 layout->eccbytes = ecc_len;
107 /* ECC will occupy the last ecc_len bytes continuously */
108 for (i = 0; i < ecc_len; i++)
109 layout->eccpos[i] = oobsize - ecc_len + i;
111 layout->oobfree[0].offset = 2;
112 layout->oobfree[0].length =
113 oobsize - ecc_len - layout->oobfree[0].offset;
116 static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
120 table_size = host->pmecc_sector_size == 512 ?
121 PMECC_INDEX_TABLE_SIZE_512 : PMECC_INDEX_TABLE_SIZE_1024;
123 /* the ALPHA lookup table is right behind the INDEX lookup table. */
124 return host->pmecc_rom_base + host->pmecc_index_table_offset +
125 table_size * sizeof(int16_t);
128 static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
130 struct nand_chip *nand_chip = mtd->priv;
131 struct atmel_nand_host *host = nand_chip->priv;
135 /* Fill odd syndromes */
136 for (i = 0; i < host->pmecc_corr_cap; i++) {
137 value = readl(&host->pmecc->rem_port[sector].rem[i / 2]);
141 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
145 static void pmecc_substitute(struct mtd_info *mtd)
147 struct nand_chip *nand_chip = mtd->priv;
148 struct atmel_nand_host *host = nand_chip->priv;
149 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
150 int16_t __iomem *index_of = host->pmecc_index_of;
151 int16_t *partial_syn = host->pmecc_partial_syn;
152 const int cap = host->pmecc_corr_cap;
156 /* si[] is a table that holds the current syndrome value,
157 * an element of that table belongs to the field
161 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
163 /* Computation 2t syndromes based on S(x) */
165 for (i = 1; i < 2 * cap; i += 2) {
166 for (j = 0; j < host->pmecc_degree; j++) {
167 if (partial_syn[i] & (0x1 << j))
168 si[i] = readw(alpha_to + i * j) ^ si[i];
171 /* Even syndrome = (Odd syndrome) ** 2 */
172 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
178 tmp = readw(index_of + si[j]);
179 tmp = (tmp * 2) % host->pmecc_cw_len;
180 si[i] = readw(alpha_to + tmp);
186 * This function defines a Berlekamp iterative procedure for
187 * finding the value of the error location polynomial.
188 * The input is si[], initialize by pmecc_substitute().
189 * The output is smu[][].
191 * This function is written according to chip datasheet Chapter:
192 * Find the Error Location Polynomial Sigma(x) of Section:
193 * Programmable Multibit ECC Control (PMECC).
195 static void pmecc_get_sigma(struct mtd_info *mtd)
197 struct nand_chip *nand_chip = mtd->priv;
198 struct atmel_nand_host *host = nand_chip->priv;
200 int16_t *lmu = host->pmecc_lmu;
201 int16_t *si = host->pmecc_si;
202 int *mu = host->pmecc_mu;
203 int *dmu = host->pmecc_dmu; /* Discrepancy */
204 int *delta = host->pmecc_delta; /* Delta order */
205 int cw_len = host->pmecc_cw_len;
206 const int16_t cap = host->pmecc_corr_cap;
207 const int num = 2 * cap + 1;
208 int16_t __iomem *index_of = host->pmecc_index_of;
209 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
211 uint32_t dmu_0_count, tmp;
212 int16_t *smu = host->pmecc_smu;
214 /* index of largest delta */
219 /* Init the Sigma(x) */
220 memset(smu, 0, sizeof(int16_t) * ARRAY_SIZE(smu));
231 /* discrepancy set to 1 */
233 /* polynom order set to 0 */
235 /* delta[0] = (mu[0] * 2 - lmu[0]) >> 1; */
242 /* Sigma(x) set to 1 */
245 /* discrepancy set to S1 */
248 /* polynom order set to 0 */
251 /* delta[1] = (mu[1] * 2 - lmu[1]) >> 1; */
254 for (i = 1; i <= cap; i++) {
256 /* Begin Computing Sigma (Mu+1) and L(mu) */
257 /* check if discrepancy is set to 0 */
261 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
262 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
267 if (dmu_0_count == tmp) {
268 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
269 smu[(cap + 1) * num + j] =
272 lmu[cap + 1] = lmu[i];
277 for (j = 0; j <= lmu[i] >> 1; j++)
278 smu[(i + 1) * num + j] = smu[i * num + j];
280 /* copy previous polynom order to the next */
285 /* find largest delta with dmu != 0 */
286 for (j = 0; j < i; j++) {
287 if ((dmu[j]) && (delta[j] > largest)) {
293 /* compute difference */
294 diff = (mu[i] - mu[ro]);
296 /* Compute degree of the new smu polynomial */
297 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
300 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
302 /* Init smu[i+1] with 0 */
303 for (k = 0; k < num; k++)
304 smu[(i + 1) * num + k] = 0;
306 /* Compute smu[i+1] */
307 for (k = 0; k <= lmu[ro] >> 1; k++) {
310 if (!(smu[ro * num + k] && dmu[i]))
312 a = readw(index_of + dmu[i]);
313 b = readw(index_of + dmu[ro]);
314 c = readw(index_of + smu[ro * num + k]);
315 tmp = a + (cw_len - b) + c;
316 a = readw(alpha_to + tmp % cw_len);
317 smu[(i + 1) * num + (k + diff)] = a;
320 for (k = 0; k <= lmu[i] >> 1; k++)
321 smu[(i + 1) * num + k] ^= smu[i * num + k];
324 /* End Computing Sigma (Mu+1) and L(mu) */
325 /* In either case compute delta */
326 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
328 /* Do not compute discrepancy for the last iteration */
332 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
335 dmu[i + 1] = si[tmp + 3];
336 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
339 smu[(i + 1) * num + k]);
340 b = si[2 * (i - 1) + 3 - k];
341 c = readw(index_of + b);
344 dmu[i + 1] = readw(alpha_to + tmp) ^
351 static int pmecc_err_location(struct mtd_info *mtd)
353 struct nand_chip *nand_chip = mtd->priv;
354 struct atmel_nand_host *host = nand_chip->priv;
355 const int cap = host->pmecc_corr_cap;
356 const int num = 2 * cap + 1;
357 int sector_size = host->pmecc_sector_size;
358 int err_nbr = 0; /* number of error */
359 int roots_nbr; /* number of roots */
362 int16_t *smu = host->pmecc_smu;
363 int timeout = PMECC_MAX_TIMEOUT_US;
365 writel(PMERRLOC_DISABLE, &host->pmerrloc->eldis);
367 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
368 writel(smu[(cap + 1) * num + i], &host->pmerrloc->sigma[i]);
372 val = PMERRLOC_ELCFG_NUM_ERRORS(err_nbr - 1);
373 if (sector_size == 1024)
374 val |= PMERRLOC_ELCFG_SECTOR_1024;
376 writel(val, &host->pmerrloc->elcfg);
377 writel(sector_size * 8 + host->pmecc_degree * cap,
378 &host->pmerrloc->elen);
381 if (readl(&host->pmerrloc->elisr) & PMERRLOC_CALC_DONE)
388 printk(KERN_ERR "atmel_nand : Timeout to calculate PMECC error location\n");
392 roots_nbr = (readl(&host->pmerrloc->elisr) & PMERRLOC_ERR_NUM_MASK)
394 /* Number of roots == degree of smu hence <= cap */
395 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
398 /* Number of roots does not match the degree of smu
399 * unable to correct error */
403 static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
404 int sector_num, int extra_bytes, int err_nbr)
406 struct nand_chip *nand_chip = mtd->priv;
407 struct atmel_nand_host *host = nand_chip->priv;
409 int byte_pos, bit_pos, sector_size, pos;
413 sector_size = host->pmecc_sector_size;
416 tmp = readl(&host->pmerrloc->el[i]) - 1;
420 if (byte_pos >= (sector_size + extra_bytes))
421 BUG(); /* should never happen */
423 if (byte_pos < sector_size) {
424 err_byte = *(buf + byte_pos);
425 *(buf + byte_pos) ^= (1 << bit_pos);
427 pos = sector_num * host->pmecc_sector_size + byte_pos;
428 printk(KERN_INFO "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
429 pos, bit_pos, err_byte, *(buf + byte_pos));
431 /* Bit flip in OOB area */
432 tmp = sector_num * host->pmecc_bytes_per_sector
433 + (byte_pos - sector_size);
435 ecc[tmp] ^= (1 << bit_pos);
437 pos = tmp + nand_chip->ecc.layout->eccpos[0];
438 printk(KERN_INFO "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
439 pos, bit_pos, err_byte, ecc[tmp]);
449 static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
452 struct nand_chip *nand_chip = mtd->priv;
453 struct atmel_nand_host *host = nand_chip->priv;
454 int i, err_nbr, eccbytes;
457 eccbytes = nand_chip->ecc.bytes;
458 for (i = 0; i < eccbytes; i++)
461 /* Erased page, return OK */
465 for (i = 0; i < host->pmecc_sector_number; i++) {
467 if (pmecc_stat & 0x1) {
468 buf_pos = buf + i * host->pmecc_sector_size;
470 pmecc_gen_syndrome(mtd, i);
471 pmecc_substitute(mtd);
472 pmecc_get_sigma(mtd);
474 err_nbr = pmecc_err_location(mtd);
476 printk(KERN_ERR "PMECC: Too many errors\n");
477 mtd->ecc_stats.failed++;
480 pmecc_correct_data(mtd, buf_pos, ecc, i,
481 host->pmecc_bytes_per_sector, err_nbr);
482 mtd->ecc_stats.corrected += err_nbr;
491 static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
492 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
494 struct atmel_nand_host *host = chip->priv;
495 int eccsize = chip->ecc.size;
496 uint8_t *oob = chip->oob_poi;
497 uint32_t *eccpos = chip->ecc.layout->eccpos;
499 int timeout = PMECC_MAX_TIMEOUT_US;
501 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
502 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
503 pmecc_writel(host->pmecc, cfg, ((pmecc_readl(host->pmecc, cfg))
504 & ~PMECC_CFG_WRITE_OP) | PMECC_CFG_AUTO_ENABLE);
506 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
507 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DATA);
509 chip->read_buf(mtd, buf, eccsize);
510 chip->read_buf(mtd, oob, mtd->oobsize);
513 if (!(pmecc_readl(host->pmecc, sr) & PMECC_SR_BUSY))
520 printk(KERN_ERR "atmel_nand : Timeout to read PMECC page\n");
524 stat = pmecc_readl(host->pmecc, isr);
526 if (pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]) != 0)
532 static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
533 struct nand_chip *chip, const uint8_t *buf,
536 struct atmel_nand_host *host = chip->priv;
537 uint32_t *eccpos = chip->ecc.layout->eccpos;
539 int timeout = PMECC_MAX_TIMEOUT_US;
541 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
542 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
544 pmecc_writel(host->pmecc, cfg, (pmecc_readl(host->pmecc, cfg) |
545 PMECC_CFG_WRITE_OP) & ~PMECC_CFG_AUTO_ENABLE);
547 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
548 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DATA);
550 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
553 if (!(pmecc_readl(host->pmecc, sr) & PMECC_SR_BUSY))
560 printk(KERN_ERR "atmel_nand : Timeout to read PMECC status, fail to write PMECC in oob\n");
564 for (i = 0; i < host->pmecc_sector_number; i++) {
565 for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
568 pos = i * host->pmecc_bytes_per_sector + j;
569 chip->oob_poi[eccpos[pos]] =
570 readb(&host->pmecc->ecc_port[i].ecc[j]);
573 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
578 static void atmel_pmecc_core_init(struct mtd_info *mtd)
580 struct nand_chip *nand_chip = mtd->priv;
581 struct atmel_nand_host *host = nand_chip->priv;
583 struct nand_ecclayout *ecc_layout;
585 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
586 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
588 switch (host->pmecc_corr_cap) {
590 val = PMECC_CFG_BCH_ERR2;
593 val = PMECC_CFG_BCH_ERR4;
596 val = PMECC_CFG_BCH_ERR8;
599 val = PMECC_CFG_BCH_ERR12;
602 val = PMECC_CFG_BCH_ERR24;
606 if (host->pmecc_sector_size == 512)
607 val |= PMECC_CFG_SECTOR512;
608 else if (host->pmecc_sector_size == 1024)
609 val |= PMECC_CFG_SECTOR1024;
611 switch (host->pmecc_sector_number) {
613 val |= PMECC_CFG_PAGE_1SECTOR;
616 val |= PMECC_CFG_PAGE_2SECTORS;
619 val |= PMECC_CFG_PAGE_4SECTORS;
622 val |= PMECC_CFG_PAGE_8SECTORS;
626 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
627 | PMECC_CFG_AUTO_DISABLE);
628 pmecc_writel(host->pmecc, cfg, val);
630 ecc_layout = nand_chip->ecc.layout;
631 pmecc_writel(host->pmecc, sarea, mtd->oobsize - 1);
632 pmecc_writel(host->pmecc, saddr, ecc_layout->eccpos[0]);
633 pmecc_writel(host->pmecc, eaddr,
634 ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
635 /* See datasheet about PMECC Clock Control Register */
636 pmecc_writel(host->pmecc, clk, PMECC_CLK_133MHZ);
637 pmecc_writel(host->pmecc, idr, 0xff);
638 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
641 static int atmel_pmecc_nand_init_params(struct nand_chip *nand,
642 struct mtd_info *mtd)
644 struct atmel_nand_host *host;
645 int cap, sector_size;
647 host = nand->priv = &pmecc_host;
649 nand->ecc.mode = NAND_ECC_HW;
650 nand->ecc.calculate = NULL;
651 nand->ecc.correct = NULL;
652 nand->ecc.hwctl = NULL;
654 cap = host->pmecc_corr_cap = CONFIG_PMECC_CAP;
655 sector_size = host->pmecc_sector_size = CONFIG_PMECC_SECTOR_SIZE;
656 host->pmecc_index_table_offset = CONFIG_PMECC_INDEX_TABLE_OFFSET;
658 MTDDEBUG(MTD_DEBUG_LEVEL1,
659 "Initialize PMECC params, cap: %d, sector: %d\n",
662 host->pmecc = (struct pmecc_regs __iomem *) ATMEL_BASE_PMECC;
663 host->pmerrloc = (struct pmecc_errloc_regs __iomem *)
665 host->pmecc_rom_base = (void __iomem *) ATMEL_BASE_ROM;
667 /* ECC is calculated for the whole page (1 step) */
668 nand->ecc.size = mtd->writesize;
670 /* set ECC page size and oob layout */
671 switch (mtd->writesize) {
674 host->pmecc_degree = PMECC_GF_DIMENSION_13;
675 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
676 host->pmecc_sector_number = mtd->writesize / sector_size;
677 host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
679 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
680 host->pmecc_index_of = host->pmecc_rom_base +
681 host->pmecc_index_table_offset;
684 nand->ecc.bytes = host->pmecc_bytes_per_sector *
685 host->pmecc_sector_number;
686 if (nand->ecc.bytes > mtd->oobsize - 2) {
687 printk(KERN_ERR "No room for ECC bytes\n");
690 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
693 nand->ecc.layout = &atmel_pmecc_oobinfo;
698 printk(KERN_ERR "Unsupported page size for PMECC, use Software ECC\n");
700 /* page size not handled by HW ECC */
701 /* switching back to soft ECC */
702 nand->ecc.mode = NAND_ECC_SOFT;
703 nand->ecc.read_page = NULL;
704 nand->ecc.postpad = 0;
705 nand->ecc.prepad = 0;
710 nand->ecc.read_page = atmel_nand_pmecc_read_page;
711 nand->ecc.write_page = atmel_nand_pmecc_write_page;
712 nand->ecc.strength = cap;
714 atmel_pmecc_core_init(mtd);
721 /* oob layout for large page size
722 * bad block info is on bytes 0 and 1
723 * the bytes have to be consecutives to avoid
724 * several NAND_CMD_RNDOUT during read
726 static struct nand_ecclayout atmel_oobinfo_large = {
728 .eccpos = {60, 61, 62, 63},
734 /* oob layout for small page size
735 * bad block info is on bytes 4 and 5
736 * the bytes have to be consecutives to avoid
737 * several NAND_CMD_RNDOUT during read
739 static struct nand_ecclayout atmel_oobinfo_small = {
741 .eccpos = {0, 1, 2, 3},
750 * function called after a write
752 * mtd: MTD block structure
753 * dat: raw data (unused)
754 * ecc_code: buffer for ECC
756 static int atmel_nand_calculate(struct mtd_info *mtd,
757 const u_char *dat, unsigned char *ecc_code)
759 unsigned int ecc_value;
761 /* get the first 2 ECC bytes */
762 ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR);
764 ecc_code[0] = ecc_value & 0xFF;
765 ecc_code[1] = (ecc_value >> 8) & 0xFF;
767 /* get the last 2 ECC bytes */
768 ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, NPR) & ATMEL_ECC_NPARITY;
770 ecc_code[2] = ecc_value & 0xFF;
771 ecc_code[3] = (ecc_value >> 8) & 0xFF;
777 * HW ECC read page function
779 * mtd: mtd info structure
780 * chip: nand chip info structure
781 * buf: buffer to store read data
782 * oob_required: caller expects OOB data read to chip->oob_poi
784 static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
785 uint8_t *buf, int oob_required, int page)
787 int eccsize = chip->ecc.size;
788 int eccbytes = chip->ecc.bytes;
789 uint32_t *eccpos = chip->ecc.layout->eccpos;
791 uint8_t *oob = chip->oob_poi;
796 chip->read_buf(mtd, p, eccsize);
798 /* move to ECC position if needed */
799 if (eccpos[0] != 0) {
800 /* This only works on large pages
801 * because the ECC controller waits for
802 * NAND_CMD_RNDOUTSTART after the
804 * anyway, for small pages, the eccpos[0] == 0
806 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
807 mtd->writesize + eccpos[0], -1);
810 /* the ECC controller needs to read the ECC just after the data */
811 ecc_pos = oob + eccpos[0];
812 chip->read_buf(mtd, ecc_pos, eccbytes);
814 /* check if there's an error */
815 stat = chip->ecc.correct(mtd, p, oob, NULL);
818 mtd->ecc_stats.failed++;
820 mtd->ecc_stats.corrected += stat;
822 /* get back to oob start (end of page) */
823 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
826 chip->read_buf(mtd, oob, mtd->oobsize);
834 * function called after a read
836 * mtd: MTD block structure
837 * dat: raw data read from the chip
838 * read_ecc: ECC from the chip (unused)
841 * Detect and correct a 1 bit error for a page
843 static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
844 u_char *read_ecc, u_char *isnull)
846 struct nand_chip *nand_chip = mtd->priv;
847 unsigned int ecc_status;
848 unsigned int ecc_word, ecc_bit;
850 /* get the status from the Status Register */
851 ecc_status = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, SR);
853 /* if there's no error */
854 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
857 /* get error bit offset (4 bits) */
858 ecc_bit = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_BITADDR;
859 /* get word address (12 bits) */
860 ecc_word = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_WORDADDR;
863 /* if there are multiple errors */
864 if (ecc_status & ATMEL_ECC_MULERR) {
865 /* check if it is a freshly erased block
866 * (filled with 0xff) */
867 if ((ecc_bit == ATMEL_ECC_BITADDR)
868 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
869 /* the block has just been erased, return OK */
872 /* it doesn't seems to be a freshly
874 * We can't correct so many errors */
875 printk(KERN_WARNING "atmel_nand : multiple errors detected."
876 " Unable to correct.\n");
880 /* if there's a single bit error : we can correct it */
881 if (ecc_status & ATMEL_ECC_ECCERR) {
882 /* there's nothing much to do here.
883 * the bit error is on the ECC itself.
885 printk(KERN_WARNING "atmel_nand : one bit error on ECC code."
886 " Nothing to correct\n");
890 printk(KERN_WARNING "atmel_nand : one bit error on data."
891 " (word offset in the page :"
892 " 0x%x bit offset : 0x%x)\n",
894 /* correct the error */
895 if (nand_chip->options & NAND_BUSWIDTH_16) {
897 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
900 dat[ecc_word] ^= (1 << ecc_bit);
902 printk(KERN_WARNING "atmel_nand : error corrected\n");
907 * Enable HW ECC : unused on most chips
909 static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
913 int atmel_hwecc_nand_init_param(struct nand_chip *nand, struct mtd_info *mtd)
915 nand->ecc.mode = NAND_ECC_HW;
916 nand->ecc.calculate = atmel_nand_calculate;
917 nand->ecc.correct = atmel_nand_correct;
918 nand->ecc.hwctl = atmel_nand_hwctl;
919 nand->ecc.read_page = atmel_nand_read_page;
922 if (nand->ecc.mode == NAND_ECC_HW) {
923 /* ECC is calculated for the whole page (1 step) */
924 nand->ecc.size = mtd->writesize;
926 /* set ECC page size and oob layout */
927 switch (mtd->writesize) {
929 nand->ecc.layout = &atmel_oobinfo_small;
930 ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
931 ATMEL_ECC_PAGESIZE_528);
934 nand->ecc.layout = &atmel_oobinfo_large;
935 ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
936 ATMEL_ECC_PAGESIZE_1056);
939 nand->ecc.layout = &atmel_oobinfo_large;
940 ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
941 ATMEL_ECC_PAGESIZE_2112);
944 nand->ecc.layout = &atmel_oobinfo_large;
945 ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
946 ATMEL_ECC_PAGESIZE_4224);
949 /* page size not handled by HW ECC */
950 /* switching back to soft ECC */
951 nand->ecc.mode = NAND_ECC_SOFT;
952 nand->ecc.calculate = NULL;
953 nand->ecc.correct = NULL;
954 nand->ecc.hwctl = NULL;
955 nand->ecc.read_page = NULL;
956 nand->ecc.postpad = 0;
957 nand->ecc.prepad = 0;
966 #endif /* CONFIG_ATMEL_NAND_HW_PMECC */
968 #endif /* CONFIG_ATMEL_NAND_HWECC */
970 static void at91_nand_hwcontrol(struct mtd_info *mtd,
971 int cmd, unsigned int ctrl)
973 struct nand_chip *this = mtd->priv;
975 if (ctrl & NAND_CTRL_CHANGE) {
976 ulong IO_ADDR_W = (ulong) this->IO_ADDR_W;
977 IO_ADDR_W &= ~(CONFIG_SYS_NAND_MASK_ALE
978 | CONFIG_SYS_NAND_MASK_CLE);
981 IO_ADDR_W |= CONFIG_SYS_NAND_MASK_CLE;
983 IO_ADDR_W |= CONFIG_SYS_NAND_MASK_ALE;
985 #ifdef CONFIG_SYS_NAND_ENABLE_PIN
986 at91_set_gpio_value(CONFIG_SYS_NAND_ENABLE_PIN,
989 this->IO_ADDR_W = (void *) IO_ADDR_W;
992 if (cmd != NAND_CMD_NONE)
993 writeb(cmd, this->IO_ADDR_W);
996 #ifdef CONFIG_SYS_NAND_READY_PIN
997 static int at91_nand_ready(struct mtd_info *mtd)
999 return at91_get_gpio_value(CONFIG_SYS_NAND_READY_PIN);
1003 #ifndef CONFIG_SYS_NAND_BASE_LIST
1004 #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
1006 static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
1007 static ulong base_addr[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
1009 int atmel_nand_chip_init(int devnum, ulong base_addr)
1012 struct mtd_info *mtd = &nand_info[devnum];
1013 struct nand_chip *nand = &nand_chip[devnum];
1016 nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
1018 nand->ecc.mode = NAND_ECC_SOFT;
1019 #ifdef CONFIG_SYS_NAND_DBW_16
1020 nand->options = NAND_BUSWIDTH_16;
1022 nand->cmd_ctrl = at91_nand_hwcontrol;
1023 #ifdef CONFIG_SYS_NAND_READY_PIN
1024 nand->dev_ready = at91_nand_ready;
1026 nand->chip_delay = 20;
1028 ret = nand_scan_ident(mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL);
1032 #ifdef CONFIG_ATMEL_NAND_HWECC
1033 #ifdef CONFIG_ATMEL_NAND_HW_PMECC
1034 ret = atmel_pmecc_nand_init_params(nand, mtd);
1036 ret = atmel_hwecc_nand_init_param(nand, mtd);
1042 ret = nand_scan_tail(mtd);
1044 nand_register(devnum);
1049 void board_nand_init(void)
1052 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
1053 if (atmel_nand_chip_init(i, base_addr[i]))
1054 printk(KERN_ERR "atmel_nand: Fail to initialize #%d chip",