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 * SPDX-License-Identifier: GPL-2.0+
16 #include <asm/arch/gpio.h>
22 #ifdef CONFIG_ATMEL_NAND_HWECC
24 /* Register access macros */
25 #define ecc_readl(add, reg) \
26 readl(AT91_BASE_SYS + add + ATMEL_ECC_##reg)
27 #define ecc_writel(add, reg, value) \
28 writel((value), AT91_BASE_SYS + add + ATMEL_ECC_##reg)
30 #include "atmel_nand_ecc.h" /* Hardware ECC registers */
32 #ifdef CONFIG_ATMEL_NAND_HW_PMECC
34 #ifdef CONFIG_SPL_BUILD
35 #undef CONFIG_SYS_NAND_ONFI_DETECTION
38 struct atmel_nand_host {
39 struct pmecc_regs __iomem *pmecc;
40 struct pmecc_errloc_regs __iomem *pmerrloc;
41 void __iomem *pmecc_rom_base;
44 u16 pmecc_sector_size;
45 u32 pmecc_index_table_offset;
47 int pmecc_bytes_per_sector;
48 int pmecc_sector_number;
49 int pmecc_degree; /* Degree of remainders */
50 int pmecc_cw_len; /* Length of codeword */
52 /* lookup table for alpha_to and index_of */
53 void __iomem *pmecc_alpha_to;
54 void __iomem *pmecc_index_of;
56 /* data for pmecc computation */
58 int16_t *pmecc_partial_syn;
60 int16_t *pmecc_lmu; /* polynomal order */
66 static struct atmel_nand_host pmecc_host;
67 static struct nand_ecclayout atmel_pmecc_oobinfo;
70 * Return number of ecc bytes per sector according to sector size and
71 * correction capability
73 * Following table shows what at91 PMECC supported:
74 * Correction Capability Sector_512_bytes Sector_1024_bytes
75 * ===================== ================ =================
76 * 2-bits 4-bytes 4-bytes
77 * 4-bits 7-bytes 7-bytes
78 * 8-bits 13-bytes 14-bytes
79 * 12-bits 20-bytes 21-bytes
80 * 24-bits 39-bytes 42-bytes
82 static int pmecc_get_ecc_bytes(int cap, int sector_size)
84 int m = 12 + sector_size / 512;
85 return (m * cap + 7) / 8;
88 static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
89 int oobsize, int ecc_len)
93 layout->eccbytes = ecc_len;
95 /* ECC will occupy the last ecc_len bytes continuously */
96 for (i = 0; i < ecc_len; i++)
97 layout->eccpos[i] = oobsize - ecc_len + i;
99 layout->oobfree[0].offset = 2;
100 layout->oobfree[0].length =
101 oobsize - ecc_len - layout->oobfree[0].offset;
104 static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
108 table_size = host->pmecc_sector_size == 512 ?
109 PMECC_INDEX_TABLE_SIZE_512 : PMECC_INDEX_TABLE_SIZE_1024;
111 /* the ALPHA lookup table is right behind the INDEX lookup table. */
112 return host->pmecc_rom_base + host->pmecc_index_table_offset +
113 table_size * sizeof(int16_t);
116 static void pmecc_data_free(struct atmel_nand_host *host)
118 free(host->pmecc_partial_syn);
119 free(host->pmecc_si);
120 free(host->pmecc_lmu);
121 free(host->pmecc_smu);
122 free(host->pmecc_mu);
123 free(host->pmecc_dmu);
124 free(host->pmecc_delta);
127 static int pmecc_data_alloc(struct atmel_nand_host *host)
129 const int cap = host->pmecc_corr_cap;
132 size = (2 * cap + 1) * sizeof(int16_t);
133 host->pmecc_partial_syn = malloc(size);
134 host->pmecc_si = malloc(size);
135 host->pmecc_lmu = malloc((cap + 1) * sizeof(int16_t));
136 host->pmecc_smu = malloc((cap + 2) * size);
138 size = (cap + 1) * sizeof(int);
139 host->pmecc_mu = malloc(size);
140 host->pmecc_dmu = malloc(size);
141 host->pmecc_delta = malloc(size);
143 if (host->pmecc_partial_syn &&
153 pmecc_data_free(host);
158 static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
160 struct nand_chip *nand_chip = mtd->priv;
161 struct atmel_nand_host *host = nand_chip->priv;
165 /* Fill odd syndromes */
166 for (i = 0; i < host->pmecc_corr_cap; i++) {
167 value = pmecc_readl(host->pmecc, rem_port[sector].rem[i / 2]);
171 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
175 static void pmecc_substitute(struct mtd_info *mtd)
177 struct nand_chip *nand_chip = mtd->priv;
178 struct atmel_nand_host *host = nand_chip->priv;
179 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
180 int16_t __iomem *index_of = host->pmecc_index_of;
181 int16_t *partial_syn = host->pmecc_partial_syn;
182 const int cap = host->pmecc_corr_cap;
186 /* si[] is a table that holds the current syndrome value,
187 * an element of that table belongs to the field
191 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
193 /* Computation 2t syndromes based on S(x) */
195 for (i = 1; i < 2 * cap; i += 2) {
196 for (j = 0; j < host->pmecc_degree; j++) {
197 if (partial_syn[i] & (0x1 << j))
198 si[i] = readw(alpha_to + i * j) ^ si[i];
201 /* Even syndrome = (Odd syndrome) ** 2 */
202 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
208 tmp = readw(index_of + si[j]);
209 tmp = (tmp * 2) % host->pmecc_cw_len;
210 si[i] = readw(alpha_to + tmp);
216 * This function defines a Berlekamp iterative procedure for
217 * finding the value of the error location polynomial.
218 * The input is si[], initialize by pmecc_substitute().
219 * The output is smu[][].
221 * This function is written according to chip datasheet Chapter:
222 * Find the Error Location Polynomial Sigma(x) of Section:
223 * Programmable Multibit ECC Control (PMECC).
225 static void pmecc_get_sigma(struct mtd_info *mtd)
227 struct nand_chip *nand_chip = mtd->priv;
228 struct atmel_nand_host *host = nand_chip->priv;
230 int16_t *lmu = host->pmecc_lmu;
231 int16_t *si = host->pmecc_si;
232 int *mu = host->pmecc_mu;
233 int *dmu = host->pmecc_dmu; /* Discrepancy */
234 int *delta = host->pmecc_delta; /* Delta order */
235 int cw_len = host->pmecc_cw_len;
236 const int16_t cap = host->pmecc_corr_cap;
237 const int num = 2 * cap + 1;
238 int16_t __iomem *index_of = host->pmecc_index_of;
239 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
241 uint32_t dmu_0_count, tmp;
242 int16_t *smu = host->pmecc_smu;
244 /* index of largest delta */
249 /* Init the Sigma(x) */
250 memset(smu, 0, sizeof(int16_t) * ARRAY_SIZE(smu));
261 /* discrepancy set to 1 */
263 /* polynom order set to 0 */
265 /* delta[0] = (mu[0] * 2 - lmu[0]) >> 1; */
272 /* Sigma(x) set to 1 */
275 /* discrepancy set to S1 */
278 /* polynom order set to 0 */
281 /* delta[1] = (mu[1] * 2 - lmu[1]) >> 1; */
284 for (i = 1; i <= cap; i++) {
286 /* Begin Computing Sigma (Mu+1) and L(mu) */
287 /* check if discrepancy is set to 0 */
291 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
292 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
297 if (dmu_0_count == tmp) {
298 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
299 smu[(cap + 1) * num + j] =
302 lmu[cap + 1] = lmu[i];
307 for (j = 0; j <= lmu[i] >> 1; j++)
308 smu[(i + 1) * num + j] = smu[i * num + j];
310 /* copy previous polynom order to the next */
315 /* find largest delta with dmu != 0 */
316 for (j = 0; j < i; j++) {
317 if ((dmu[j]) && (delta[j] > largest)) {
323 /* compute difference */
324 diff = (mu[i] - mu[ro]);
326 /* Compute degree of the new smu polynomial */
327 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
330 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
332 /* Init smu[i+1] with 0 */
333 for (k = 0; k < num; k++)
334 smu[(i + 1) * num + k] = 0;
336 /* Compute smu[i+1] */
337 for (k = 0; k <= lmu[ro] >> 1; k++) {
340 if (!(smu[ro * num + k] && dmu[i]))
342 a = readw(index_of + dmu[i]);
343 b = readw(index_of + dmu[ro]);
344 c = readw(index_of + smu[ro * num + k]);
345 tmp = a + (cw_len - b) + c;
346 a = readw(alpha_to + tmp % cw_len);
347 smu[(i + 1) * num + (k + diff)] = a;
350 for (k = 0; k <= lmu[i] >> 1; k++)
351 smu[(i + 1) * num + k] ^= smu[i * num + k];
354 /* End Computing Sigma (Mu+1) and L(mu) */
355 /* In either case compute delta */
356 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
358 /* Do not compute discrepancy for the last iteration */
362 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
365 dmu[i + 1] = si[tmp + 3];
366 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
369 smu[(i + 1) * num + k]);
370 b = si[2 * (i - 1) + 3 - k];
371 c = readw(index_of + b);
374 dmu[i + 1] = readw(alpha_to + tmp) ^
381 static int pmecc_err_location(struct mtd_info *mtd)
383 struct nand_chip *nand_chip = mtd->priv;
384 struct atmel_nand_host *host = nand_chip->priv;
385 const int cap = host->pmecc_corr_cap;
386 const int num = 2 * cap + 1;
387 int sector_size = host->pmecc_sector_size;
388 int err_nbr = 0; /* number of error */
389 int roots_nbr; /* number of roots */
392 int16_t *smu = host->pmecc_smu;
393 int timeout = PMECC_MAX_TIMEOUT_US;
395 pmecc_writel(host->pmerrloc, eldis, PMERRLOC_DISABLE);
397 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
398 pmecc_writel(host->pmerrloc, sigma[i],
399 smu[(cap + 1) * num + i]);
403 val = PMERRLOC_ELCFG_NUM_ERRORS(err_nbr - 1);
404 if (sector_size == 1024)
405 val |= PMERRLOC_ELCFG_SECTOR_1024;
407 pmecc_writel(host->pmerrloc, elcfg, val);
408 pmecc_writel(host->pmerrloc, elen,
409 sector_size * 8 + host->pmecc_degree * cap);
412 if (pmecc_readl(host->pmerrloc, elisr) & PMERRLOC_CALC_DONE)
419 dev_err(host->dev, "atmel_nand : Timeout to calculate PMECC error location\n");
423 roots_nbr = (pmecc_readl(host->pmerrloc, elisr) & PMERRLOC_ERR_NUM_MASK)
425 /* Number of roots == degree of smu hence <= cap */
426 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
429 /* Number of roots does not match the degree of smu
430 * unable to correct error */
434 static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
435 int sector_num, int extra_bytes, int err_nbr)
437 struct nand_chip *nand_chip = mtd->priv;
438 struct atmel_nand_host *host = nand_chip->priv;
440 int byte_pos, bit_pos, sector_size, pos;
444 sector_size = host->pmecc_sector_size;
447 tmp = pmecc_readl(host->pmerrloc, el[i]) - 1;
451 if (byte_pos >= (sector_size + extra_bytes))
452 BUG(); /* should never happen */
454 if (byte_pos < sector_size) {
455 err_byte = *(buf + byte_pos);
456 *(buf + byte_pos) ^= (1 << bit_pos);
458 pos = sector_num * host->pmecc_sector_size + byte_pos;
459 dev_dbg(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
460 pos, bit_pos, err_byte, *(buf + byte_pos));
462 /* Bit flip in OOB area */
463 tmp = sector_num * host->pmecc_bytes_per_sector
464 + (byte_pos - sector_size);
466 ecc[tmp] ^= (1 << bit_pos);
468 pos = tmp + nand_chip->ecc.layout->eccpos[0];
469 dev_dbg(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
470 pos, bit_pos, err_byte, ecc[tmp]);
480 static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
483 struct nand_chip *nand_chip = mtd->priv;
484 struct atmel_nand_host *host = nand_chip->priv;
485 int i, err_nbr, eccbytes;
488 eccbytes = nand_chip->ecc.bytes;
489 for (i = 0; i < eccbytes; i++)
492 /* Erased page, return OK */
496 for (i = 0; i < host->pmecc_sector_number; i++) {
498 if (pmecc_stat & 0x1) {
499 buf_pos = buf + i * host->pmecc_sector_size;
501 pmecc_gen_syndrome(mtd, i);
502 pmecc_substitute(mtd);
503 pmecc_get_sigma(mtd);
505 err_nbr = pmecc_err_location(mtd);
507 dev_err(host->dev, "PMECC: Too many errors\n");
508 mtd->ecc_stats.failed++;
511 pmecc_correct_data(mtd, buf_pos, ecc, i,
512 host->pmecc_bytes_per_sector, err_nbr);
513 mtd->ecc_stats.corrected += err_nbr;
522 static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
523 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
525 struct atmel_nand_host *host = chip->priv;
526 int eccsize = chip->ecc.size;
527 uint8_t *oob = chip->oob_poi;
528 uint32_t *eccpos = chip->ecc.layout->eccpos;
530 int timeout = PMECC_MAX_TIMEOUT_US;
532 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
533 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
534 pmecc_writel(host->pmecc, cfg, ((pmecc_readl(host->pmecc, cfg))
535 & ~PMECC_CFG_WRITE_OP) | PMECC_CFG_AUTO_ENABLE);
537 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
538 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DATA);
540 chip->read_buf(mtd, buf, eccsize);
541 chip->read_buf(mtd, oob, mtd->oobsize);
544 if (!(pmecc_readl(host->pmecc, sr) & PMECC_SR_BUSY))
551 dev_err(host->dev, "atmel_nand : Timeout to read PMECC page\n");
555 stat = pmecc_readl(host->pmecc, isr);
557 if (pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]) != 0)
563 static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
564 struct nand_chip *chip, const uint8_t *buf,
567 struct atmel_nand_host *host = chip->priv;
568 uint32_t *eccpos = chip->ecc.layout->eccpos;
570 int timeout = PMECC_MAX_TIMEOUT_US;
572 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
573 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
575 pmecc_writel(host->pmecc, cfg, (pmecc_readl(host->pmecc, cfg) |
576 PMECC_CFG_WRITE_OP) & ~PMECC_CFG_AUTO_ENABLE);
578 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
579 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DATA);
581 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
584 if (!(pmecc_readl(host->pmecc, sr) & PMECC_SR_BUSY))
591 dev_err(host->dev, "atmel_nand : Timeout to read PMECC status, fail to write PMECC in oob\n");
595 for (i = 0; i < host->pmecc_sector_number; i++) {
596 for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
599 pos = i * host->pmecc_bytes_per_sector + j;
600 chip->oob_poi[eccpos[pos]] =
601 pmecc_readb(host->pmecc, ecc_port[i].ecc[j]);
604 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
609 static void atmel_pmecc_core_init(struct mtd_info *mtd)
611 struct nand_chip *nand_chip = mtd->priv;
612 struct atmel_nand_host *host = nand_chip->priv;
614 struct nand_ecclayout *ecc_layout;
616 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
617 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
619 switch (host->pmecc_corr_cap) {
621 val = PMECC_CFG_BCH_ERR2;
624 val = PMECC_CFG_BCH_ERR4;
627 val = PMECC_CFG_BCH_ERR8;
630 val = PMECC_CFG_BCH_ERR12;
633 val = PMECC_CFG_BCH_ERR24;
637 if (host->pmecc_sector_size == 512)
638 val |= PMECC_CFG_SECTOR512;
639 else if (host->pmecc_sector_size == 1024)
640 val |= PMECC_CFG_SECTOR1024;
642 switch (host->pmecc_sector_number) {
644 val |= PMECC_CFG_PAGE_1SECTOR;
647 val |= PMECC_CFG_PAGE_2SECTORS;
650 val |= PMECC_CFG_PAGE_4SECTORS;
653 val |= PMECC_CFG_PAGE_8SECTORS;
657 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
658 | PMECC_CFG_AUTO_DISABLE);
659 pmecc_writel(host->pmecc, cfg, val);
661 ecc_layout = nand_chip->ecc.layout;
662 pmecc_writel(host->pmecc, sarea, mtd->oobsize - 1);
663 pmecc_writel(host->pmecc, saddr, ecc_layout->eccpos[0]);
664 pmecc_writel(host->pmecc, eaddr,
665 ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
666 /* See datasheet about PMECC Clock Control Register */
667 pmecc_writel(host->pmecc, clk, PMECC_CLK_133MHZ);
668 pmecc_writel(host->pmecc, idr, 0xff);
669 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
672 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
674 * get_onfi_ecc_param - Get ECC requirement from ONFI parameters
675 * @ecc_bits: store the ONFI ECC correct bits capbility
676 * @sector_size: in how many bytes that ONFI require to correct @ecc_bits
678 * Returns -1 if ONFI parameters is not supported. In this case @ecc_bits,
679 * @sector_size are initialize to 0.
680 * Return 0 if success to get the ECC requirement.
682 static int get_onfi_ecc_param(struct nand_chip *chip,
683 int *ecc_bits, int *sector_size)
685 *ecc_bits = *sector_size = 0;
687 if (chip->onfi_params.ecc_bits == 0xff)
688 /* TODO: the sector_size and ecc_bits need to be find in
689 * extended ecc parameter, currently we don't support it.
693 *ecc_bits = chip->onfi_params.ecc_bits;
695 /* The default sector size (ecc codeword size) is 512 */
702 * pmecc_choose_ecc - Get ecc requirement from ONFI parameters. If
703 * pmecc_corr_cap or pmecc_sector_size is 0, then set it as
704 * ONFI ECC parameters.
705 * @host: point to an atmel_nand_host structure.
706 * if host->pmecc_corr_cap is 0 then set it as the ONFI ecc_bits.
707 * if host->pmecc_sector_size is 0 then set it as the ONFI sector_size.
708 * @chip: point to an nand_chip structure.
709 * @cap: store the ONFI ECC correct bits capbility
710 * @sector_size: in how many bytes that ONFI require to correct @ecc_bits
712 * Return 0 if success. otherwise return the error code.
714 static int pmecc_choose_ecc(struct atmel_nand_host *host,
715 struct nand_chip *chip,
716 int *cap, int *sector_size)
718 /* Get ECC requirement from ONFI parameters */
719 *cap = *sector_size = 0;
720 if (chip->onfi_version) {
721 if (!get_onfi_ecc_param(chip, cap, sector_size)) {
722 MTDDEBUG(MTD_DEBUG_LEVEL1, "ONFI params, minimum required ECC: %d bits in %d bytes\n",
725 dev_info(host->dev, "NAND chip ECC reqirement is in Extended ONFI parameter, we don't support yet.\n");
728 dev_info(host->dev, "NAND chip is not ONFI compliant, assume ecc_bits is 2 in 512 bytes");
730 if (*cap == 0 && *sector_size == 0) {
731 /* Non-ONFI compliant or use extended ONFI parameters */
736 /* If head file doesn't specify then use the one in ONFI parameters */
737 if (host->pmecc_corr_cap == 0) {
738 /* use the most fitable ecc bits (the near bigger one ) */
740 host->pmecc_corr_cap = 2;
742 host->pmecc_corr_cap = 4;
744 host->pmecc_corr_cap = 8;
746 host->pmecc_corr_cap = 12;
748 host->pmecc_corr_cap = 24;
752 if (host->pmecc_sector_size == 0) {
753 /* use the most fitable sector size (the near smaller one ) */
754 if (*sector_size >= 1024)
755 host->pmecc_sector_size = 1024;
756 else if (*sector_size >= 512)
757 host->pmecc_sector_size = 512;
765 static int atmel_pmecc_nand_init_params(struct nand_chip *nand,
766 struct mtd_info *mtd)
768 struct atmel_nand_host *host;
769 int cap, sector_size;
771 host = nand->priv = &pmecc_host;
773 nand->ecc.mode = NAND_ECC_HW;
774 nand->ecc.calculate = NULL;
775 nand->ecc.correct = NULL;
776 nand->ecc.hwctl = NULL;
778 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
779 host->pmecc_corr_cap = host->pmecc_sector_size = 0;
781 #ifdef CONFIG_PMECC_CAP
782 host->pmecc_corr_cap = CONFIG_PMECC_CAP;
784 #ifdef CONFIG_PMECC_SECTOR_SIZE
785 host->pmecc_sector_size = CONFIG_PMECC_SECTOR_SIZE;
787 /* Get ECC requirement of ONFI parameters. And if CONFIG_PMECC_CAP or
788 * CONFIG_PMECC_SECTOR_SIZE not defined, then use ecc_bits, sector_size
791 if (pmecc_choose_ecc(host, nand, &cap, §or_size)) {
792 dev_err(host->dev, "The NAND flash's ECC requirement(ecc_bits: %d, sector_size: %d) are not support!",
797 if (cap > host->pmecc_corr_cap)
798 dev_info(host->dev, "WARNING: Using different ecc correct bits(%d bit) from Nand ONFI ECC reqirement (%d bit).\n",
799 host->pmecc_corr_cap, cap);
800 if (sector_size < host->pmecc_sector_size)
801 dev_info(host->dev, "WARNING: Using different ecc correct sector size (%d bytes) from Nand ONFI ECC reqirement (%d bytes).\n",
802 host->pmecc_sector_size, sector_size);
803 #else /* CONFIG_SYS_NAND_ONFI_DETECTION */
804 host->pmecc_corr_cap = CONFIG_PMECC_CAP;
805 host->pmecc_sector_size = CONFIG_PMECC_SECTOR_SIZE;
808 cap = host->pmecc_corr_cap;
809 sector_size = host->pmecc_sector_size;
811 /* TODO: need check whether cap & sector_size is validate */
813 if (host->pmecc_sector_size == 512)
814 host->pmecc_index_table_offset = ATMEL_PMECC_INDEX_OFFSET_512;
816 host->pmecc_index_table_offset = ATMEL_PMECC_INDEX_OFFSET_1024;
818 MTDDEBUG(MTD_DEBUG_LEVEL1,
819 "Initialize PMECC params, cap: %d, sector: %d\n",
822 host->pmecc = (struct pmecc_regs __iomem *) ATMEL_BASE_PMECC;
823 host->pmerrloc = (struct pmecc_errloc_regs __iomem *)
825 host->pmecc_rom_base = (void __iomem *) ATMEL_BASE_ROM;
827 /* ECC is calculated for the whole page (1 step) */
828 nand->ecc.size = mtd->writesize;
830 /* set ECC page size and oob layout */
831 switch (mtd->writesize) {
835 host->pmecc_degree = (sector_size == 512) ?
836 PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
837 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
838 host->pmecc_sector_number = mtd->writesize / sector_size;
839 host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
841 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
842 host->pmecc_index_of = host->pmecc_rom_base +
843 host->pmecc_index_table_offset;
846 nand->ecc.bytes = host->pmecc_bytes_per_sector *
847 host->pmecc_sector_number;
849 if (nand->ecc.bytes > MTD_MAX_ECCPOS_ENTRIES_LARGE) {
850 dev_err(host->dev, "too large eccpos entries. max support ecc.bytes is %d\n",
851 MTD_MAX_ECCPOS_ENTRIES_LARGE);
855 if (nand->ecc.bytes > mtd->oobsize - 2) {
856 dev_err(host->dev, "No room for ECC bytes\n");
859 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
862 nand->ecc.layout = &atmel_pmecc_oobinfo;
867 dev_err(host->dev, "Unsupported page size for PMECC, use Software ECC\n");
869 /* page size not handled by HW ECC */
870 /* switching back to soft ECC */
871 nand->ecc.mode = NAND_ECC_SOFT;
872 nand->ecc.read_page = NULL;
873 nand->ecc.postpad = 0;
874 nand->ecc.prepad = 0;
879 /* Allocate data for PMECC computation */
880 if (pmecc_data_alloc(host)) {
881 dev_err(host->dev, "Cannot allocate memory for PMECC computation!\n");
885 nand->options |= NAND_NO_SUBPAGE_WRITE;
886 nand->ecc.read_page = atmel_nand_pmecc_read_page;
887 nand->ecc.write_page = atmel_nand_pmecc_write_page;
888 nand->ecc.strength = cap;
890 atmel_pmecc_core_init(mtd);
897 /* oob layout for large page size
898 * bad block info is on bytes 0 and 1
899 * the bytes have to be consecutives to avoid
900 * several NAND_CMD_RNDOUT during read
902 static struct nand_ecclayout atmel_oobinfo_large = {
904 .eccpos = {60, 61, 62, 63},
910 /* oob layout for small page size
911 * bad block info is on bytes 4 and 5
912 * the bytes have to be consecutives to avoid
913 * several NAND_CMD_RNDOUT during read
915 static struct nand_ecclayout atmel_oobinfo_small = {
917 .eccpos = {0, 1, 2, 3},
926 * function called after a write
928 * mtd: MTD block structure
929 * dat: raw data (unused)
930 * ecc_code: buffer for ECC
932 static int atmel_nand_calculate(struct mtd_info *mtd,
933 const u_char *dat, unsigned char *ecc_code)
935 unsigned int ecc_value;
937 /* get the first 2 ECC bytes */
938 ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR);
940 ecc_code[0] = ecc_value & 0xFF;
941 ecc_code[1] = (ecc_value >> 8) & 0xFF;
943 /* get the last 2 ECC bytes */
944 ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, NPR) & ATMEL_ECC_NPARITY;
946 ecc_code[2] = ecc_value & 0xFF;
947 ecc_code[3] = (ecc_value >> 8) & 0xFF;
953 * HW ECC read page function
955 * mtd: mtd info structure
956 * chip: nand chip info structure
957 * buf: buffer to store read data
958 * oob_required: caller expects OOB data read to chip->oob_poi
960 static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
961 uint8_t *buf, int oob_required, int page)
963 int eccsize = chip->ecc.size;
964 int eccbytes = chip->ecc.bytes;
965 uint32_t *eccpos = chip->ecc.layout->eccpos;
967 uint8_t *oob = chip->oob_poi;
972 chip->read_buf(mtd, p, eccsize);
974 /* move to ECC position if needed */
975 if (eccpos[0] != 0) {
976 /* This only works on large pages
977 * because the ECC controller waits for
978 * NAND_CMD_RNDOUTSTART after the
980 * anyway, for small pages, the eccpos[0] == 0
982 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
983 mtd->writesize + eccpos[0], -1);
986 /* the ECC controller needs to read the ECC just after the data */
987 ecc_pos = oob + eccpos[0];
988 chip->read_buf(mtd, ecc_pos, eccbytes);
990 /* check if there's an error */
991 stat = chip->ecc.correct(mtd, p, oob, NULL);
994 mtd->ecc_stats.failed++;
996 mtd->ecc_stats.corrected += stat;
998 /* get back to oob start (end of page) */
999 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1002 chip->read_buf(mtd, oob, mtd->oobsize);
1010 * function called after a read
1012 * mtd: MTD block structure
1013 * dat: raw data read from the chip
1014 * read_ecc: ECC from the chip (unused)
1017 * Detect and correct a 1 bit error for a page
1019 static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
1020 u_char *read_ecc, u_char *isnull)
1022 struct nand_chip *nand_chip = mtd->priv;
1023 unsigned int ecc_status;
1024 unsigned int ecc_word, ecc_bit;
1026 /* get the status from the Status Register */
1027 ecc_status = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, SR);
1029 /* if there's no error */
1030 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
1033 /* get error bit offset (4 bits) */
1034 ecc_bit = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_BITADDR;
1035 /* get word address (12 bits) */
1036 ecc_word = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_WORDADDR;
1039 /* if there are multiple errors */
1040 if (ecc_status & ATMEL_ECC_MULERR) {
1041 /* check if it is a freshly erased block
1042 * (filled with 0xff) */
1043 if ((ecc_bit == ATMEL_ECC_BITADDR)
1044 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
1045 /* the block has just been erased, return OK */
1048 /* it doesn't seems to be a freshly
1050 * We can't correct so many errors */
1051 dev_warn(host->dev, "atmel_nand : multiple errors detected."
1052 " Unable to correct.\n");
1056 /* if there's a single bit error : we can correct it */
1057 if (ecc_status & ATMEL_ECC_ECCERR) {
1058 /* there's nothing much to do here.
1059 * the bit error is on the ECC itself.
1061 dev_warn(host->dev, "atmel_nand : one bit error on ECC code."
1062 " Nothing to correct\n");
1066 dev_warn(host->dev, "atmel_nand : one bit error on data."
1067 " (word offset in the page :"
1068 " 0x%x bit offset : 0x%x)\n",
1070 /* correct the error */
1071 if (nand_chip->options & NAND_BUSWIDTH_16) {
1073 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1076 dat[ecc_word] ^= (1 << ecc_bit);
1078 dev_warn(host->dev, "atmel_nand : error corrected\n");
1083 * Enable HW ECC : unused on most chips
1085 static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1089 int atmel_hwecc_nand_init_param(struct nand_chip *nand, struct mtd_info *mtd)
1091 nand->ecc.mode = NAND_ECC_HW;
1092 nand->ecc.calculate = atmel_nand_calculate;
1093 nand->ecc.correct = atmel_nand_correct;
1094 nand->ecc.hwctl = atmel_nand_hwctl;
1095 nand->ecc.read_page = atmel_nand_read_page;
1096 nand->ecc.bytes = 4;
1098 if (nand->ecc.mode == NAND_ECC_HW) {
1099 /* ECC is calculated for the whole page (1 step) */
1100 nand->ecc.size = mtd->writesize;
1102 /* set ECC page size and oob layout */
1103 switch (mtd->writesize) {
1105 nand->ecc.layout = &atmel_oobinfo_small;
1106 ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
1107 ATMEL_ECC_PAGESIZE_528);
1110 nand->ecc.layout = &atmel_oobinfo_large;
1111 ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
1112 ATMEL_ECC_PAGESIZE_1056);
1115 nand->ecc.layout = &atmel_oobinfo_large;
1116 ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
1117 ATMEL_ECC_PAGESIZE_2112);
1120 nand->ecc.layout = &atmel_oobinfo_large;
1121 ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
1122 ATMEL_ECC_PAGESIZE_4224);
1125 /* page size not handled by HW ECC */
1126 /* switching back to soft ECC */
1127 nand->ecc.mode = NAND_ECC_SOFT;
1128 nand->ecc.calculate = NULL;
1129 nand->ecc.correct = NULL;
1130 nand->ecc.hwctl = NULL;
1131 nand->ecc.read_page = NULL;
1132 nand->ecc.postpad = 0;
1133 nand->ecc.prepad = 0;
1134 nand->ecc.bytes = 0;
1142 #endif /* CONFIG_ATMEL_NAND_HW_PMECC */
1144 #endif /* CONFIG_ATMEL_NAND_HWECC */
1146 static void at91_nand_hwcontrol(struct mtd_info *mtd,
1147 int cmd, unsigned int ctrl)
1149 struct nand_chip *this = mtd->priv;
1151 if (ctrl & NAND_CTRL_CHANGE) {
1152 ulong IO_ADDR_W = (ulong) this->IO_ADDR_W;
1153 IO_ADDR_W &= ~(CONFIG_SYS_NAND_MASK_ALE
1154 | CONFIG_SYS_NAND_MASK_CLE);
1156 if (ctrl & NAND_CLE)
1157 IO_ADDR_W |= CONFIG_SYS_NAND_MASK_CLE;
1158 if (ctrl & NAND_ALE)
1159 IO_ADDR_W |= CONFIG_SYS_NAND_MASK_ALE;
1161 #ifdef CONFIG_SYS_NAND_ENABLE_PIN
1162 gpio_set_value(CONFIG_SYS_NAND_ENABLE_PIN, !(ctrl & NAND_NCE));
1164 this->IO_ADDR_W = (void *) IO_ADDR_W;
1167 if (cmd != NAND_CMD_NONE)
1168 writeb(cmd, this->IO_ADDR_W);
1171 #ifdef CONFIG_SYS_NAND_READY_PIN
1172 static int at91_nand_ready(struct mtd_info *mtd)
1174 return gpio_get_value(CONFIG_SYS_NAND_READY_PIN);
1178 #ifdef CONFIG_SPL_BUILD
1179 /* The following code is for SPL */
1180 static nand_info_t mtd;
1181 static struct nand_chip nand_chip;
1183 static int nand_command(int block, int page, uint32_t offs, u8 cmd)
1185 struct nand_chip *this = mtd.priv;
1186 int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
1187 void (*hwctrl)(struct mtd_info *mtd, int cmd,
1188 unsigned int ctrl) = this->cmd_ctrl;
1190 while (this->dev_ready(&mtd))
1193 if (cmd == NAND_CMD_READOOB) {
1194 offs += CONFIG_SYS_NAND_PAGE_SIZE;
1195 cmd = NAND_CMD_READ0;
1198 hwctrl(&mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
1200 if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd))
1203 hwctrl(&mtd, offs & 0xff, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
1204 hwctrl(&mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE);
1205 hwctrl(&mtd, (page_addr & 0xff), NAND_CTRL_ALE);
1206 hwctrl(&mtd, ((page_addr >> 8) & 0xff), NAND_CTRL_ALE);
1207 #ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
1208 hwctrl(&mtd, (page_addr >> 16) & 0x0f, NAND_CTRL_ALE);
1210 hwctrl(&mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
1212 hwctrl(&mtd, NAND_CMD_READSTART, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
1213 hwctrl(&mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
1215 while (this->dev_ready(&mtd))
1221 static int nand_is_bad_block(int block)
1223 struct nand_chip *this = mtd.priv;
1225 nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS, NAND_CMD_READOOB);
1227 if (this->options & NAND_BUSWIDTH_16) {
1228 if (readw(this->IO_ADDR_R) != 0xffff)
1231 if (readb(this->IO_ADDR_R) != 0xff)
1238 #ifdef CONFIG_SPL_NAND_ECC
1239 static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
1240 #define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \
1241 CONFIG_SYS_NAND_ECCSIZE)
1242 #define ECCTOTAL (ECCSTEPS * CONFIG_SYS_NAND_ECCBYTES)
1244 static int nand_read_page(int block, int page, void *dst)
1246 struct nand_chip *this = mtd.priv;
1247 u_char ecc_calc[ECCTOTAL];
1248 u_char ecc_code[ECCTOTAL];
1249 u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
1250 int eccsize = CONFIG_SYS_NAND_ECCSIZE;
1251 int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
1252 int eccsteps = ECCSTEPS;
1255 nand_command(block, page, 0, NAND_CMD_READ0);
1257 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1258 if (this->ecc.mode != NAND_ECC_SOFT)
1259 this->ecc.hwctl(&mtd, NAND_ECC_READ);
1260 this->read_buf(&mtd, p, eccsize);
1261 this->ecc.calculate(&mtd, p, &ecc_calc[i]);
1263 this->read_buf(&mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
1265 for (i = 0; i < ECCTOTAL; i++)
1266 ecc_code[i] = oob_data[nand_ecc_pos[i]];
1268 eccsteps = ECCSTEPS;
1271 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1272 this->ecc.correct(&mtd, p, &ecc_code[i], &ecc_calc[i]);
1277 static int nand_read_page(int block, int page, void *dst)
1279 struct nand_chip *this = mtd.priv;
1281 nand_command(block, page, 0, NAND_CMD_READ0);
1282 atmel_nand_pmecc_read_page(&mtd, this, dst, 0, page);
1286 #endif /* CONFIG_SPL_NAND_ECC */
1288 int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
1290 unsigned int block, lastblock;
1293 block = offs / CONFIG_SYS_NAND_BLOCK_SIZE;
1294 lastblock = (offs + size - 1) / CONFIG_SYS_NAND_BLOCK_SIZE;
1295 page = (offs % CONFIG_SYS_NAND_BLOCK_SIZE) / CONFIG_SYS_NAND_PAGE_SIZE;
1297 while (block <= lastblock) {
1298 if (!nand_is_bad_block(block)) {
1299 while (page < CONFIG_SYS_NAND_PAGE_COUNT) {
1300 nand_read_page(block, page, dst);
1301 dst += CONFIG_SYS_NAND_PAGE_SIZE;
1316 int at91_nand_wait_ready(struct mtd_info *mtd)
1318 struct nand_chip *this = mtd->priv;
1320 udelay(this->chip_delay);
1325 int board_nand_init(struct nand_chip *nand)
1329 nand->ecc.mode = NAND_ECC_SOFT;
1330 #ifdef CONFIG_SYS_NAND_DBW_16
1331 nand->options = NAND_BUSWIDTH_16;
1332 nand->read_buf = nand_read_buf16;
1334 nand->read_buf = nand_read_buf;
1336 nand->cmd_ctrl = at91_nand_hwcontrol;
1337 #ifdef CONFIG_SYS_NAND_READY_PIN
1338 nand->dev_ready = at91_nand_ready;
1340 nand->dev_ready = at91_nand_wait_ready;
1342 nand->chip_delay = 20;
1344 #ifdef CONFIG_ATMEL_NAND_HWECC
1345 #ifdef CONFIG_ATMEL_NAND_HW_PMECC
1346 ret = atmel_pmecc_nand_init_params(nand, &mtd);
1353 void nand_init(void)
1355 mtd.writesize = CONFIG_SYS_NAND_PAGE_SIZE;
1356 mtd.oobsize = CONFIG_SYS_NAND_OOBSIZE;
1357 mtd.priv = &nand_chip;
1358 nand_chip.IO_ADDR_R = (void __iomem *)CONFIG_SYS_NAND_BASE;
1359 nand_chip.IO_ADDR_W = (void __iomem *)CONFIG_SYS_NAND_BASE;
1360 board_nand_init(&nand_chip);
1362 #ifdef CONFIG_SPL_NAND_ECC
1363 if (nand_chip.ecc.mode == NAND_ECC_SOFT) {
1364 nand_chip.ecc.calculate = nand_calculate_ecc;
1365 nand_chip.ecc.correct = nand_correct_data;
1369 if (nand_chip.select_chip)
1370 nand_chip.select_chip(&mtd, 0);
1373 void nand_deselect(void)
1375 if (nand_chip.select_chip)
1376 nand_chip.select_chip(&mtd, -1);
1381 #ifndef CONFIG_SYS_NAND_BASE_LIST
1382 #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
1384 static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
1385 static ulong base_addr[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
1387 int atmel_nand_chip_init(int devnum, ulong base_addr)
1390 struct mtd_info *mtd = &nand_info[devnum];
1391 struct nand_chip *nand = &nand_chip[devnum];
1394 nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
1396 #ifdef CONFIG_NAND_ECC_BCH
1397 nand->ecc.mode = NAND_ECC_SOFT_BCH;
1399 nand->ecc.mode = NAND_ECC_SOFT;
1401 #ifdef CONFIG_SYS_NAND_DBW_16
1402 nand->options = NAND_BUSWIDTH_16;
1404 nand->cmd_ctrl = at91_nand_hwcontrol;
1405 #ifdef CONFIG_SYS_NAND_READY_PIN
1406 nand->dev_ready = at91_nand_ready;
1408 nand->chip_delay = 75;
1410 ret = nand_scan_ident(mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL);
1414 #ifdef CONFIG_ATMEL_NAND_HWECC
1415 #ifdef CONFIG_ATMEL_NAND_HW_PMECC
1416 ret = atmel_pmecc_nand_init_params(nand, mtd);
1418 ret = atmel_hwecc_nand_init_param(nand, mtd);
1424 ret = nand_scan_tail(mtd);
1426 nand_register(devnum);
1431 void board_nand_init(void)
1434 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
1435 if (atmel_nand_chip_init(i, base_addr[i]))
1436 dev_err(host->dev, "atmel_nand: Fail to initialize #%d chip",
1439 #endif /* CONFIG_SPL_BUILD */