2 * (C) Copyright 2004-2008 Texas Instruments, <www.ti.com>
3 * Rohit Choraria <rohitkc@ti.com>
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/errno.h>
11 #include <asm/arch/mem.h>
12 #include <asm/arch/cpu.h>
13 #include <asm/omap_gpmc.h>
14 #include <linux/mtd/nand_ecc.h>
15 #include <linux/bch.h>
16 #include <linux/compiler.h>
19 #include <asm/arch/elm.h>
23 static __maybe_unused struct nand_ecclayout hw_nand_oob =
24 GPMC_NAND_HW_ECC_LAYOUT;
25 static __maybe_unused struct nand_ecclayout hw_bch8_nand_oob =
26 GPMC_NAND_HW_BCH8_ECC_LAYOUT;
28 static struct gpmc *gpmc_cfg = (struct gpmc *)GPMC_BASE;
30 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
31 static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
32 static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
34 static struct nand_bbt_descr bbt_main_descr = {
35 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
36 NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
37 .offs = 0, /* may be overwritten depending on ECC layout */
39 .veroffs = 4, /* may be overwritten depending on ECC layout */
41 .pattern = bbt_pattern,
44 static struct nand_bbt_descr bbt_mirror_descr = {
45 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
46 NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
47 .offs = 0, /* may be overwritten depending on ECC layout */
49 .veroffs = 4, /* may be overwritten depending on ECC layout */
51 .pattern = mirror_pattern,
56 * omap_nand_hwcontrol - Set the address pointers corretly for the
57 * following address/data/command operation
59 static void omap_nand_hwcontrol(struct mtd_info *mtd, int32_t cmd,
62 register struct nand_chip *this = mtd->priv;
65 * Point the IO_ADDR to DATA and ADDRESS registers instead
69 case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
70 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
72 case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
73 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_adr;
75 case NAND_CTRL_CHANGE | NAND_NCE:
76 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
80 if (cmd != NAND_CMD_NONE)
81 writeb(cmd, this->IO_ADDR_W);
84 #ifdef CONFIG_SPL_BUILD
85 /* Check wait pin as dev ready indicator */
86 int omap_spl_dev_ready(struct mtd_info *mtd)
88 return gpmc_cfg->status & (1 << 8);
93 * omap_hwecc_init - Initialize the Hardware ECC for NAND flash in
95 * @mtd: MTD device structure
98 static void __maybe_unused omap_hwecc_init(struct nand_chip *chip)
101 * Init ECC Control Register
102 * Clear all ECC | Enable Reg1
104 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
105 writel(ECCSIZE1 | ECCSIZE0 | ECCSIZE0SEL, &gpmc_cfg->ecc_size_config);
109 * gen_true_ecc - This function will generate true ECC value, which
110 * can be used when correcting data read from NAND flash memory core
112 * @ecc_buf: buffer to store ecc code
114 * @return: re-formatted ECC value
116 static uint32_t gen_true_ecc(uint8_t *ecc_buf)
118 return ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xF0) << 20) |
119 ((ecc_buf[2] & 0x0F) << 8);
123 * omap_correct_data - Compares the ecc read from nand spare area with ECC
124 * registers values and corrects one bit error if it has occured
125 * Further details can be had from OMAP TRM and the following selected links:
126 * http://en.wikipedia.org/wiki/Hamming_code
127 * http://www.cs.utexas.edu/users/plaxton/c/337/05f/slides/ErrorCorrection-4.pdf
129 * @mtd: MTD device structure
131 * @read_ecc: ecc read from nand flash
132 * @calc_ecc: ecc read from ECC registers
134 * @return 0 if data is OK or corrected, else returns -1
136 static int __maybe_unused omap_correct_data(struct mtd_info *mtd, uint8_t *dat,
137 uint8_t *read_ecc, uint8_t *calc_ecc)
139 uint32_t orig_ecc, new_ecc, res, hm;
140 uint16_t parity_bits, byte;
143 /* Regenerate the orginal ECC */
144 orig_ecc = gen_true_ecc(read_ecc);
145 new_ecc = gen_true_ecc(calc_ecc);
146 /* Get the XOR of real ecc */
147 res = orig_ecc ^ new_ecc;
149 /* Get the hamming width */
151 /* Single bit errors can be corrected! */
153 /* Correctable data! */
154 parity_bits = res >> 16;
155 bit = (parity_bits & 0x7);
156 byte = (parity_bits >> 3) & 0x1FF;
157 /* Flip the bit to correct */
158 dat[byte] ^= (0x1 << bit);
159 } else if (hm == 1) {
160 printf("Error: Ecc is wrong\n");
161 /* ECC itself is corrupted */
165 * hm distance != parity pairs OR one, could mean 2 bit
166 * error OR potentially be on a blank page..
167 * orig_ecc: contains spare area data from nand flash.
168 * new_ecc: generated ecc while reading data area.
169 * Note: if the ecc = 0, all data bits from which it was
170 * generated are 0xFF.
171 * The 3 byte(24 bits) ecc is generated per 512byte
172 * chunk of a page. If orig_ecc(from spare area)
173 * is 0xFF && new_ecc(computed now from data area)=0x0,
174 * this means that data area is 0xFF and spare area is
175 * 0xFF. A sure sign of a erased page!
177 if ((orig_ecc == 0x0FFF0FFF) && (new_ecc == 0x00000000))
179 printf("Error: Bad compare! failed\n");
180 /* detected 2 bit error */
188 * omap_calculate_ecc - Generate non-inverted ECC bytes.
190 * Using noninverted ECC can be considered ugly since writing a blank
191 * page ie. padding will clear the ECC bytes. This is no problem as
192 * long nobody is trying to write data on the seemingly unused page.
193 * Reading an erased page will produce an ECC mismatch between
194 * generated and read ECC bytes that has to be dealt with separately.
195 * E.g. if page is 0xFF (fresh erased), and if HW ECC engine within GPMC
196 * is used, the result of read will be 0x0 while the ECC offsets of the
197 * spare area will be 0xFF which will result in an ECC mismatch.
198 * @mtd: MTD structure
200 * @ecc_code: ecc_code buffer
202 static int __maybe_unused omap_calculate_ecc(struct mtd_info *mtd,
203 const uint8_t *dat, uint8_t *ecc_code)
207 /* Start Reading from HW ECC1_Result = 0x200 */
208 val = readl(&gpmc_cfg->ecc1_result);
210 ecc_code[0] = val & 0xFF;
211 ecc_code[1] = (val >> 16) & 0xFF;
212 ecc_code[2] = ((val >> 8) & 0x0F) | ((val >> 20) & 0xF0);
215 * Stop reading anymore ECC vals and clear old results
216 * enable will be called if more reads are required
218 writel(0x000, &gpmc_cfg->ecc_config);
224 * omap_enable_ecc - This function enables the hardware ecc functionality
225 * @mtd: MTD device structure
226 * @mode: Read/Write mode
228 static void __maybe_unused omap_enable_hwecc(struct mtd_info *mtd, int32_t mode)
230 struct nand_chip *chip = mtd->priv;
231 uint32_t val, dev_width = (chip->options & NAND_BUSWIDTH_16) >> 1;
236 /* Clear the ecc result registers, select ecc reg as 1 */
237 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
240 * Size 0 = 0xFF, Size1 is 0xFF - both are 512 bytes
241 * tell all regs to generate size0 sized regs
242 * we just have a single ECC engine for all CS
244 writel(ECCSIZE1 | ECCSIZE0 | ECCSIZE0SEL,
245 &gpmc_cfg->ecc_size_config);
246 val = (dev_width << 7) | (cs << 1) | (0x1);
247 writel(val, &gpmc_cfg->ecc_config);
250 printf("Error: Unrecognized Mode[%d]!\n", mode);
256 * Generic BCH interface
258 struct nand_bch_priv {
262 struct bch_control *control;
270 /* GPMC ecc engine settings */
271 #define BCH_WRAPMODE_1 1 /* BCH wrap mode 1 */
272 #define BCH_WRAPMODE_6 6 /* BCH wrap mode 6 */
274 /* BCH nibbles for diff bch levels */
275 #define NAND_ECC_HW_BCH ((uint8_t)(NAND_ECC_HW_OOB_FIRST) + 1)
276 #define ECC_BCH4_NIBBLES 13
277 #define ECC_BCH8_NIBBLES 26
278 #define ECC_BCH16_NIBBLES 52
281 * This can be a single instance cause all current users have only one NAND
282 * with nearly the same setup (BCH8, some with ELM and others with sw BCH
284 * When some users with other BCH strength will exists this have to change!
286 static __maybe_unused struct nand_bch_priv bch_priv = {
287 .mode = NAND_ECC_HW_BCH,
289 .nibbles = ECC_BCH8_NIBBLES,
294 * omap_hwecc_init_bch - Initialize the BCH Hardware ECC for NAND flash in
296 * @mtd: MTD device structure
297 * @mode: Read/Write mode
300 static void omap_hwecc_init_bch(struct nand_chip *chip, int32_t mode)
303 uint32_t dev_width = (chip->options & NAND_BUSWIDTH_16) >> 1;
305 uint32_t unused_length = 0;
307 uint32_t wr_mode = BCH_WRAPMODE_6;
308 struct nand_bch_priv *bch = chip->priv;
310 /* Clear the ecc result registers, select ecc reg as 1 */
311 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
314 wr_mode = BCH_WRAPMODE_1;
316 switch (bch->nibbles) {
317 case ECC_BCH4_NIBBLES:
320 case ECC_BCH8_NIBBLES:
323 case ECC_BCH16_NIBBLES:
329 * This is ecc_size_config for ELM mode.
330 * Here we are using different settings for read and write access and
331 * also depending on BCH strength.
335 /* write access only setup eccsize1 config */
336 val = ((unused_length + bch->nibbles) << 22);
342 * by default eccsize0 selected for ecc1resultsize
345 val = (bch->nibbles << 12);
346 /* eccsize1 config */
347 val |= (unused_length << 22);
352 * This ecc_size_config setting is for BCH sw library.
354 * Note: we only support BCH8 currently with BCH sw library!
355 * Should be really easy to adobt to BCH4, however some omap3 have
358 * Here we are using wrapping mode 6 both for reading and writing, with:
359 * size0 = 0 (no additional protected byte in spare area)
360 * size1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
362 val = (32 << 22) | (0 << 12);
364 /* ecc size configuration */
365 writel(val, &gpmc_cfg->ecc_size_config);
368 * Configure the ecc engine in gpmc
369 * We assume 512 Byte sector pages for access to NAND.
371 val = (1 << 16); /* enable BCH mode */
372 val |= (bch->type << 12); /* setup BCH type */
373 val |= (wr_mode << 8); /* setup wrapping mode */
374 val |= (dev_width << 7); /* setup device width (16 or 8 bit) */
375 val |= (cs << 1); /* setup chip select to work on */
376 debug("set ECC_CONFIG=0x%08x\n", val);
377 writel(val, &gpmc_cfg->ecc_config);
381 * omap_enable_ecc_bch - This function enables the bch h/w ecc functionality
382 * @mtd: MTD device structure
383 * @mode: Read/Write mode
386 static void omap_enable_ecc_bch(struct mtd_info *mtd, int32_t mode)
388 struct nand_chip *chip = mtd->priv;
390 omap_hwecc_init_bch(chip, mode);
392 writel((readl(&gpmc_cfg->ecc_config) | 0x1), &gpmc_cfg->ecc_config);
396 * omap_ecc_disable - Disable H/W ECC calculation
398 * @mtd: MTD device structure
400 static void __maybe_unused omap_ecc_disable(struct mtd_info *mtd)
402 writel((readl(&gpmc_cfg->ecc_config) & ~0x1), &gpmc_cfg->ecc_config);
406 * BCH8 support (needs ELM and thus AM33xx-only)
410 * omap_read_bch8_result - Read BCH result for BCH8 level
412 * @mtd: MTD device structure
413 * @big_endian: When set read register 3 first
414 * @ecc_code: Read syndrome from BCH result registers
416 static void omap_read_bch8_result(struct mtd_info *mtd, uint8_t big_endian,
423 ptr = &gpmc_cfg->bch_result_0_3[0].bch_result_x[3];
424 ecc_code[i++] = readl(ptr) & 0xFF;
426 for (j = 0; j < 3; j++) {
427 ecc_code[i++] = (readl(ptr) >> 24) & 0xFF;
428 ecc_code[i++] = (readl(ptr) >> 16) & 0xFF;
429 ecc_code[i++] = (readl(ptr) >> 8) & 0xFF;
430 ecc_code[i++] = readl(ptr) & 0xFF;
434 ptr = &gpmc_cfg->bch_result_0_3[0].bch_result_x[0];
435 for (j = 0; j < 3; j++) {
436 ecc_code[i++] = readl(ptr) & 0xFF;
437 ecc_code[i++] = (readl(ptr) >> 8) & 0xFF;
438 ecc_code[i++] = (readl(ptr) >> 16) & 0xFF;
439 ecc_code[i++] = (readl(ptr) >> 24) & 0xFF;
442 ecc_code[i++] = readl(ptr) & 0xFF;
443 ecc_code[i++] = 0; /* 14th byte is always zero */
448 * omap_rotate_ecc_bch - Rotate the syndrome bytes
450 * @mtd: MTD device structure
451 * @calc_ecc: ECC read from ECC registers
452 * @syndrome: Rotated syndrome will be retuned in this array
455 static void omap_rotate_ecc_bch(struct mtd_info *mtd, uint8_t *calc_ecc,
458 struct nand_chip *chip = mtd->priv;
459 struct nand_bch_priv *bch = chip->priv;
478 for (i = 0, j = (n_bytes-1); i < n_bytes; i++, j--)
479 syndrome[i] = calc_ecc[j];
483 * omap_calculate_ecc_bch - Read BCH ECC result
485 * @mtd: MTD structure
487 * @ecc_code: ecc_code buffer
489 static int omap_calculate_ecc_bch(struct mtd_info *mtd, const uint8_t *dat,
492 struct nand_chip *chip = mtd->priv;
493 struct nand_bch_priv *bch = chip->priv;
494 uint8_t big_endian = 1;
497 if (bch->type == ECC_BCH8)
498 omap_read_bch8_result(mtd, big_endian, ecc_code);
499 else /* BCH4 and BCH16 currently not supported */
503 * Stop reading anymore ECC vals and clear old results
504 * enable will be called if more reads are required
506 omap_ecc_disable(mtd);
512 * omap_fix_errors_bch - Correct bch error in the data
514 * @mtd: MTD device structure
515 * @data: Data read from flash
516 * @error_count:Number of errors in data
517 * @error_loc: Locations of errors in the data
520 static void omap_fix_errors_bch(struct mtd_info *mtd, uint8_t *data,
521 uint32_t error_count, uint32_t *error_loc)
523 struct nand_chip *chip = mtd->priv;
524 struct nand_bch_priv *bch = chip->priv;
526 uint32_t error_byte_pos;
527 uint32_t error_bit_mask;
528 uint32_t last_bit = (bch->nibbles * 4) - 1;
530 /* Flip all bits as specified by the error location array. */
531 /* FOR( each found error location flip the bit ) */
532 for (count = 0; count < error_count; count++) {
533 if (error_loc[count] > last_bit) {
534 /* Remove the ECC spare bits from correction. */
535 error_loc[count] -= (last_bit + 1);
536 /* Offset bit in data region */
537 error_byte_pos = ((512 * 8) -
538 (error_loc[count]) - 1) / 8;
540 error_bit_mask = 0x1 << (error_loc[count] % 8);
541 /* Toggle the error bit to make the correction. */
542 data[error_byte_pos] ^= error_bit_mask;
548 * omap_correct_data_bch - Compares the ecc read from nand spare area
549 * with ECC registers values and corrects one bit error if it has occured
551 * @mtd: MTD device structure
553 * @read_ecc: ecc read from nand flash (ignored)
554 * @calc_ecc: ecc read from ECC registers
556 * @return 0 if data is OK or corrected, else returns -1
558 static int omap_correct_data_bch(struct mtd_info *mtd, uint8_t *dat,
559 uint8_t *read_ecc, uint8_t *calc_ecc)
561 struct nand_chip *chip = mtd->priv;
562 struct nand_bch_priv *bch = chip->priv;
563 uint8_t syndrome[28];
564 uint32_t error_count = 0;
565 uint32_t error_loc[8];
566 uint32_t i, ecc_flag;
569 for (i = 0; i < chip->ecc.bytes; i++)
570 if (read_ecc[i] != 0xff)
577 elm_config((enum bch_level)(bch->type));
580 * while reading ECC result we read it in big endian.
581 * Hence while loading to ELM we have rotate to get the right endian.
583 omap_rotate_ecc_bch(mtd, calc_ecc, syndrome);
585 /* use elm module to check for errors */
586 if (elm_check_error(syndrome, bch->nibbles, &error_count,
588 printf("ECC: uncorrectable.\n");
592 /* correct bch error */
594 omap_fix_errors_bch(mtd, dat, error_count, error_loc);
600 * omap_read_page_bch - hardware ecc based page read function
601 * @mtd: mtd info structure
602 * @chip: nand chip info structure
603 * @buf: buffer to store read data
604 * @oob_required: caller expects OOB data read to chip->oob_poi
605 * @page: page number to read
608 static int omap_read_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
609 uint8_t *buf, int oob_required, int page)
611 int i, eccsize = chip->ecc.size;
612 int eccbytes = chip->ecc.bytes;
613 int eccsteps = chip->ecc.steps;
615 uint8_t *ecc_calc = chip->buffers->ecccalc;
616 uint8_t *ecc_code = chip->buffers->ecccode;
617 uint32_t *eccpos = chip->ecc.layout->eccpos;
618 uint8_t *oob = chip->oob_poi;
624 oob_pos = (eccsize * eccsteps) + chip->ecc.layout->eccpos[0];
625 oob += chip->ecc.layout->eccpos[0];
627 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize,
629 chip->ecc.hwctl(mtd, NAND_ECC_READ);
631 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_pos, page);
632 chip->read_buf(mtd, p, eccsize);
634 /* read respective ecc from oob area */
635 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, page);
636 chip->read_buf(mtd, oob, eccbytes);
638 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
644 for (i = 0; i < chip->ecc.total; i++)
645 ecc_code[i] = chip->oob_poi[eccpos[i]];
647 eccsteps = chip->ecc.steps;
650 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
653 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
655 mtd->ecc_stats.failed++;
657 mtd->ecc_stats.corrected += stat;
661 #endif /* CONFIG_AM33XX */
664 * OMAP3 BCH8 support (with BCH library)
666 #ifdef CONFIG_NAND_OMAP_BCH8
668 * omap_calculate_ecc_bch - Read BCH ECC result
670 * @mtd: MTD device structure
671 * @dat: The pointer to data on which ecc is computed (unused here)
672 * @ecc: The ECC output buffer
674 static int omap_calculate_ecc_bch(struct mtd_info *mtd, const uint8_t *dat,
679 unsigned long nsectors, val1, val2, val3, val4;
681 nsectors = ((readl(&gpmc_cfg->ecc_config) >> 4) & 0x7) + 1;
683 for (i = 0; i < nsectors; i++) {
684 /* Read hw-computed remainder */
685 val1 = readl(&gpmc_cfg->bch_result_0_3[i].bch_result_x[0]);
686 val2 = readl(&gpmc_cfg->bch_result_0_3[i].bch_result_x[1]);
687 val3 = readl(&gpmc_cfg->bch_result_0_3[i].bch_result_x[2]);
688 val4 = readl(&gpmc_cfg->bch_result_0_3[i].bch_result_x[3]);
691 * Add constant polynomial to remainder, in order to get an ecc
692 * sequence of 0xFFs for a buffer filled with 0xFFs.
694 *ecc++ = 0xef ^ (val4 & 0xFF);
695 *ecc++ = 0x51 ^ ((val3 >> 24) & 0xFF);
696 *ecc++ = 0x2e ^ ((val3 >> 16) & 0xFF);
697 *ecc++ = 0x09 ^ ((val3 >> 8) & 0xFF);
698 *ecc++ = 0xed ^ (val3 & 0xFF);
699 *ecc++ = 0x93 ^ ((val2 >> 24) & 0xFF);
700 *ecc++ = 0x9a ^ ((val2 >> 16) & 0xFF);
701 *ecc++ = 0xc2 ^ ((val2 >> 8) & 0xFF);
702 *ecc++ = 0x97 ^ (val2 & 0xFF);
703 *ecc++ = 0x79 ^ ((val1 >> 24) & 0xFF);
704 *ecc++ = 0xe5 ^ ((val1 >> 16) & 0xFF);
705 *ecc++ = 0x24 ^ ((val1 >> 8) & 0xFF);
706 *ecc++ = 0xb5 ^ (val1 & 0xFF);
710 * Stop reading anymore ECC vals and clear old results
711 * enable will be called if more reads are required
713 omap_ecc_disable(mtd);
719 * omap_correct_data_bch - Decode received data and correct errors
720 * @mtd: MTD device structure
722 * @read_ecc: ecc read from nand flash
723 * @calc_ecc: ecc read from HW ECC registers
725 static int omap_correct_data_bch(struct mtd_info *mtd, u_char *data,
726 u_char *read_ecc, u_char *calc_ecc)
729 /* cannot correct more than 8 errors */
730 unsigned int errloc[8];
731 struct nand_chip *chip = mtd->priv;
732 struct nand_bch_priv *chip_priv = chip->priv;
733 struct bch_control *bch = chip_priv->control;
735 count = decode_bch(bch, NULL, 512, read_ecc, calc_ecc, NULL, errloc);
738 for (i = 0; i < count; i++) {
739 /* correct data only, not ecc bytes */
740 if (errloc[i] < 8*512)
741 data[errloc[i]/8] ^= 1 << (errloc[i] & 7);
742 printf("corrected bitflip %u\n", errloc[i]);
746 * BCH8 have 13 bytes of ECC; BCH4 needs adoption
749 for (i = 0; i < 13; i++)
750 printf("%02x ", read_ecc[i]);
753 for (i = 0; i < 13; i++)
754 printf("%02x ", calc_ecc[i]);
758 } else if (count < 0) {
759 puts("ecc unrecoverable error\n");
765 * omap_free_bch - Release BCH ecc resources
766 * @mtd: MTD device structure
768 static void __maybe_unused omap_free_bch(struct mtd_info *mtd)
770 struct nand_chip *chip = mtd->priv;
771 struct nand_bch_priv *chip_priv = chip->priv;
772 struct bch_control *bch = NULL;
775 bch = chip_priv->control;
779 chip_priv->control = NULL;
782 #endif /* CONFIG_NAND_OMAP_BCH8 */
784 #ifndef CONFIG_SPL_BUILD
786 * omap_nand_switch_ecc - switch the ECC operation between different engines
787 * (h/w and s/w) and different algorithms (hamming and BCHx)
789 * @hardware - true if one of the HW engines should be used
790 * @eccstrength - the number of bits that could be corrected
791 * (1 - hamming, 4 - BCH4, 8 - BCH8, 16 - BCH16)
793 void omap_nand_switch_ecc(uint32_t hardware, uint32_t eccstrength)
795 struct nand_chip *nand;
796 struct mtd_info *mtd;
798 if (nand_curr_device < 0 ||
799 nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
800 !nand_info[nand_curr_device].name) {
801 printf("Error: Can't switch ecc, no devices available\n");
805 mtd = &nand_info[nand_curr_device];
808 nand->options |= NAND_OWN_BUFFERS;
810 /* Reset ecc interface */
811 nand->ecc.mode = NAND_ECC_NONE;
812 nand->ecc.read_page = NULL;
813 nand->ecc.write_page = NULL;
814 nand->ecc.read_oob = NULL;
815 nand->ecc.write_oob = NULL;
816 nand->ecc.hwctl = NULL;
817 nand->ecc.correct = NULL;
818 nand->ecc.calculate = NULL;
819 nand->ecc.strength = eccstrength;
821 /* Setup the ecc configurations again */
823 if (eccstrength == 1) {
824 nand->ecc.mode = NAND_ECC_HW;
825 nand->ecc.layout = &hw_nand_oob;
826 nand->ecc.size = 512;
828 nand->ecc.hwctl = omap_enable_hwecc;
829 nand->ecc.correct = omap_correct_data;
830 nand->ecc.calculate = omap_calculate_ecc;
831 omap_hwecc_init(nand);
832 printf("1-bit hamming HW ECC selected\n");
834 #if defined(CONFIG_AM33XX) || defined(CONFIG_NAND_OMAP_BCH8)
835 else if (eccstrength == 8) {
836 nand->ecc.mode = NAND_ECC_HW;
837 nand->ecc.layout = &hw_bch8_nand_oob;
838 nand->ecc.size = 512;
840 nand->ecc.bytes = 14;
841 nand->ecc.read_page = omap_read_page_bch;
843 nand->ecc.bytes = 13;
845 nand->ecc.hwctl = omap_enable_ecc_bch;
846 nand->ecc.correct = omap_correct_data_bch;
847 nand->ecc.calculate = omap_calculate_ecc_bch;
848 omap_hwecc_init_bch(nand, NAND_ECC_READ);
849 printf("8-bit BCH HW ECC selected\n");
853 nand->ecc.mode = NAND_ECC_SOFT;
854 /* Use mtd default settings */
855 nand->ecc.layout = NULL;
857 printf("SW ECC selected\n");
860 /* Update NAND handling after ECC mode switch */
863 nand->options &= ~NAND_OWN_BUFFERS;
865 #endif /* CONFIG_SPL_BUILD */
868 * Board-specific NAND initialization. The following members of the
869 * argument are board-specific:
870 * - IO_ADDR_R: address to read the 8 I/O lines of the flash device
871 * - IO_ADDR_W: address to write the 8 I/O lines of the flash device
872 * - cmd_ctrl: hardwarespecific function for accesing control-lines
873 * - waitfunc: hardwarespecific function for accesing device ready/busy line
874 * - ecc.hwctl: function to enable (reset) hardware ecc generator
875 * - ecc.mode: mode of ecc, see defines
876 * - chip_delay: chip dependent delay for transfering data from array to
878 * - options: various chip options. They can partly be set to inform
879 * nand_scan about special functionality. See the defines for further
882 int board_nand_init(struct nand_chip *nand)
884 int32_t gpmc_config = 0;
888 * xloader/Uboot's gpmc configuration would have configured GPMC for
889 * nand type of memory. The following logic scans and latches on to the
890 * first CS with NAND type memory.
891 * TBD: need to make this logic generic to handle multiple CS NAND
894 while (cs < GPMC_MAX_CS) {
895 /* Check if NAND type is set */
896 if ((readl(&gpmc_cfg->cs[cs].config1) & 0xC00) == 0x800) {
902 if (cs >= GPMC_MAX_CS) {
903 printf("NAND: Unable to find NAND settings in "
904 "GPMC Configuration - quitting\n");
908 gpmc_config = readl(&gpmc_cfg->config);
909 /* Disable Write protect */
911 writel(gpmc_config, &gpmc_cfg->config);
913 nand->IO_ADDR_R = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
914 nand->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
916 nand->cmd_ctrl = omap_nand_hwcontrol;
917 nand->options = NAND_NO_PADDING | NAND_CACHEPRG;
918 /* If we are 16 bit dev, our gpmc config tells us that */
919 if ((readl(&gpmc_cfg->cs[cs].config1) & 0x3000) == 0x1000)
920 nand->options |= NAND_BUSWIDTH_16;
922 nand->chip_delay = 100;
924 #if defined(CONFIG_AM33XX) || defined(CONFIG_NAND_OMAP_BCH8)
926 /* AM33xx uses the ELM */
927 /* required in case of BCH */
931 * Whereas other OMAP based SoC do not have the ELM, they use the BCH
934 bch_priv.control = init_bch(13, 8, 0x201b /* hw polynominal */);
935 if (!bch_priv.control) {
936 puts("Could not init_bch()\n");
940 /* BCH info that will be correct for SPL or overridden otherwise. */
941 nand->priv = &bch_priv;
944 /* Default ECC mode */
945 #if defined(CONFIG_AM33XX) || defined(CONFIG_NAND_OMAP_BCH8)
946 nand->ecc.mode = NAND_ECC_HW;
947 nand->ecc.layout = &hw_bch8_nand_oob;
948 nand->ecc.size = CONFIG_SYS_NAND_ECCSIZE;
949 nand->ecc.bytes = CONFIG_SYS_NAND_ECCBYTES;
950 nand->ecc.strength = 8;
951 nand->ecc.hwctl = omap_enable_ecc_bch;
952 nand->ecc.correct = omap_correct_data_bch;
953 nand->ecc.calculate = omap_calculate_ecc_bch;
955 nand->ecc.read_page = omap_read_page_bch;
957 omap_hwecc_init_bch(nand, NAND_ECC_READ);
959 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_NAND_SOFTECC)
960 nand->ecc.mode = NAND_ECC_SOFT;
962 nand->ecc.mode = NAND_ECC_HW;
963 nand->ecc.layout = &hw_nand_oob;
964 nand->ecc.size = CONFIG_SYS_NAND_ECCSIZE;
965 nand->ecc.bytes = CONFIG_SYS_NAND_ECCBYTES;
966 nand->ecc.hwctl = omap_enable_hwecc;
967 nand->ecc.correct = omap_correct_data;
968 nand->ecc.calculate = omap_calculate_ecc;
969 nand->ecc.strength = 1;
970 omap_hwecc_init(nand);
973 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
974 if (nand->ecc.layout) {
975 bbt_main_descr.offs = nand->ecc.layout->oobfree[0].offset;
976 bbt_main_descr.veroffs = bbt_main_descr.offs +
979 bbt_mirror_descr.offs = nand->ecc.layout->oobfree[0].offset;
980 bbt_mirror_descr.veroffs = bbt_mirror_descr.offs +
981 sizeof(mirror_pattern);
984 nand->bbt_options |= NAND_BBT_USE_FLASH;
985 nand->bbt_td = &bbt_main_descr;
986 nand->bbt_md = &bbt_mirror_descr;
989 #ifdef CONFIG_SPL_BUILD
990 if (nand->options & NAND_BUSWIDTH_16)
991 nand->read_buf = nand_read_buf16;
993 nand->read_buf = nand_read_buf;
994 nand->dev_ready = omap_spl_dev_ready;