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/hardware.h>
12 #include <asm/arch/mem.h>
13 #include <linux/mtd/omap_gpmc.h>
14 #include <linux/mtd/nand_ecc.h>
15 #include <linux/bch.h>
16 #include <linux/compiler.h>
18 #include <linux/mtd/omap_elm.h>
20 #define BADBLOCK_MARKER_LENGTH 2
21 #define SECTOR_BYTES 512
22 #define ECCCLEAR (0x1 << 8)
23 #define ECCRESULTREG1 (0x1 << 0)
24 /* 4 bit padding to make byte aligned, 56 = 52 + 4 */
25 #define BCH4_BIT_PAD 4
28 static u8 bch8_polynomial[] = {0xef, 0x51, 0x2e, 0x09, 0xed, 0x93, 0x9a, 0xc2,
29 0x97, 0x79, 0xe5, 0x24, 0xb5};
31 static uint8_t cs_next;
32 static __maybe_unused struct nand_ecclayout omap_ecclayout;
34 #if defined(CONFIG_NAND_OMAP_GPMC_WSCFG)
35 static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE] =
36 { CONFIG_NAND_OMAP_GPMC_WSCFG };
38 /* wscfg is preset to zero since its a static variable */
39 static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE];
43 * Driver configurations
45 struct omap_nand_info {
46 struct bch_control *control;
47 enum omap_ecc ecc_scheme;
49 uint8_t ws; /* wait status pin (0,1) */
52 /* We are wasting a bit of memory but al least we are safe */
53 static struct omap_nand_info omap_nand_info[GPMC_MAX_CS];
55 static struct gpmc __iomem *gpmc_cfg = (void __iomem *)GPMC_BASE;
57 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
58 static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
59 static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
61 static struct nand_bbt_descr bbt_main_descr = {
62 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
63 NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
64 .offs = 0, /* may be overwritten depending on ECC layout */
66 .veroffs = 4, /* may be overwritten depending on ECC layout */
68 .pattern = bbt_pattern,
71 static struct nand_bbt_descr bbt_mirror_descr = {
72 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
73 NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
74 .offs = 0, /* may be overwritten depending on ECC layout */
76 .veroffs = 4, /* may be overwritten depending on ECC layout */
78 .pattern = mirror_pattern,
82 #define PREFETCH_FIFOTHRESHOLD_MAX 0x40
83 #define PREFETCH_FIFOTHRESHOLD(val) ((val) << 8)
85 #define PREFETCH_ENABLEOPTIMIZEDACCESS (0x1 << 27)
87 #define GPMC_PREFETCH_STATUS_FIFO_CNT(val) (((val) >> 24) & 0x7F)
88 #define GPMC_PREFETCH_STATUS_COUNT(val) ((val) & 0x00003fff)
90 #define CS_NUM_SHIFT 24
91 #define ENABLE_PREFETCH (0x1 << 7)
92 #define DMA_MPU_MODE 2
94 #define OMAP_NAND_TIMEOUT_MS 5000
96 #define PRINT_REG(x) debug("+++ %.15s (0x%08x)=0x%08x\n", #x, &gpmc_cfg->x, readl(&gpmc_cfg->x))
99 * omap_nand_hwcontrol - Set the address pointers corretly for the
100 * following address/data/command operation
102 static void omap_nand_hwcontrol(struct mtd_info *mtd, int32_t cmd,
105 register struct nand_chip *this = mtd->priv;
106 struct omap_nand_info *info = this->priv;
110 * Point the IO_ADDR to DATA and ADDRESS registers instead
114 case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
115 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
117 case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
118 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_adr;
120 case NAND_CTRL_CHANGE | NAND_NCE:
121 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
125 if (cmd != NAND_CMD_NONE)
126 writeb(cmd, this->IO_ADDR_W);
129 /* Check wait pin as dev ready indicator */
130 static int omap_dev_ready(struct mtd_info *mtd)
132 struct nand_chip *this = mtd->priv;
133 struct omap_nand_info *info = this->priv;
134 return !!(readl(&gpmc_cfg->status) & (1 << (8 + info->ws)));
138 * gen_true_ecc - This function will generate true ECC value, which
139 * can be used when correcting data read from NAND flash memory core
141 * @ecc_buf: buffer to store ecc code
143 * @return: re-formatted ECC value
145 static uint32_t gen_true_ecc(uint8_t *ecc_buf)
147 return ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xF0) << 20) |
148 ((ecc_buf[2] & 0x0F) << 8);
152 * omap_correct_data - Compares the ecc read from nand spare area with ECC
153 * registers values and corrects one bit error if it has occured
154 * Further details can be had from OMAP TRM and the following selected links:
155 * http://en.wikipedia.org/wiki/Hamming_code
156 * http://www.cs.utexas.edu/users/plaxton/c/337/05f/slides/ErrorCorrection-4.pdf
158 * @mtd: MTD device structure
160 * @read_ecc: ecc read from nand flash
161 * @calc_ecc: ecc read from ECC registers
163 * @return 0 if data is OK or corrected, else returns -1
165 static int __maybe_unused omap_correct_data(struct mtd_info *mtd, uint8_t *dat,
166 uint8_t *read_ecc, uint8_t *calc_ecc)
168 uint32_t orig_ecc, new_ecc, res, hm;
169 uint16_t parity_bits, byte;
172 /* Regenerate the orginal ECC */
173 orig_ecc = gen_true_ecc(read_ecc);
174 new_ecc = gen_true_ecc(calc_ecc);
175 /* Get the XOR of real ecc */
176 res = orig_ecc ^ new_ecc;
178 /* Get the hamming width */
180 /* Single bit errors can be corrected! */
182 /* Correctable data! */
183 parity_bits = res >> 16;
184 bit = (parity_bits & 0x7);
185 byte = (parity_bits >> 3) & 0x1FF;
186 /* Flip the bit to correct */
187 dat[byte] ^= (0x1 << bit);
188 } else if (hm == 1) {
189 printf("Error: Ecc is wrong\n");
190 /* ECC itself is corrupted */
194 * hm distance != parity pairs OR one, could mean 2 bit
195 * error OR potentially be on a blank page..
196 * orig_ecc: contains spare area data from nand flash.
197 * new_ecc: generated ecc while reading data area.
198 * Note: if the ecc = 0, all data bits from which it was
199 * generated are 0xFF.
200 * The 3 byte(24 bits) ecc is generated per 512byte
201 * chunk of a page. If orig_ecc(from spare area)
202 * is 0xFF && new_ecc(computed now from data area)=0x0,
203 * this means that data area is 0xFF and spare area is
204 * 0xFF. A sure sign of a erased page!
206 if ((orig_ecc == 0x0FFF0FFF) && (new_ecc == 0x00000000))
208 printf("Error: Bad compare! failed\n");
209 /* detected 2 bit error */
217 * omap_enable_hwecc - configures GPMC as per ECC scheme before read/write
218 * @mtd: MTD device structure
219 * @mode: Read/Write mode
222 static void omap_enable_hwecc(struct mtd_info *mtd, int32_t mode)
224 struct nand_chip *nand = mtd->priv;
225 struct omap_nand_info *info = nand->priv;
226 unsigned int dev_width = (nand->options & NAND_BUSWIDTH_16) ? 1 : 0;
227 unsigned int ecc_algo = 0;
228 unsigned int bch_type = 0;
229 unsigned int eccsize1 = 0x00, eccsize0 = 0x00, bch_wrapmode = 0x00;
230 u32 ecc_size_config_val = 0;
231 u32 ecc_config_val = 0;
234 /* configure GPMC for specific ecc-scheme */
235 switch (info->ecc_scheme) {
236 case OMAP_ECC_HAM1_CODE_SW:
238 case OMAP_ECC_HAM1_CODE_HW:
245 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
246 case OMAP_ECC_BCH8_CODE_HW:
249 if (mode == NAND_ECC_WRITE) {
251 eccsize0 = 0; /* extra bits in nibbles per sector */
252 eccsize1 = 28; /* OOB bits in nibbles per sector */
255 eccsize0 = 26; /* ECC bits in nibbles per sector */
256 eccsize1 = 2; /* non-ECC bits in nibbles per sector */
259 case OMAP_ECC_BCH16_CODE_HW:
262 if (mode == NAND_ECC_WRITE) {
264 eccsize0 = 0; /* extra bits in nibbles per sector */
265 eccsize1 = 52; /* OOB bits in nibbles per sector */
268 eccsize0 = 52; /* ECC bits in nibbles per sector */
269 eccsize1 = 0; /* non-ECC bits in nibbles per sector */
275 /* Clear ecc and enable bits */
276 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
277 /* Configure ecc size for BCH */
278 ecc_size_config_val = (eccsize1 << 22) | (eccsize0 << 12);
279 writel(ecc_size_config_val, &gpmc_cfg->ecc_size_config);
281 /* Configure device details for BCH engine */
282 ecc_config_val = ((ecc_algo << 16) | /* HAM1 | BCHx */
283 (bch_type << 12) | /* BCH4/BCH8/BCH16 */
284 (bch_wrapmode << 8) | /* wrap mode */
285 (dev_width << 7) | /* bus width */
286 (0x0 << 4) | /* number of sectors */
287 (cs << 1) | /* ECC CS */
288 (0x1)); /* enable ECC */
289 writel(ecc_config_val, &gpmc_cfg->ecc_config);
293 * omap_calculate_ecc - Read ECC result
294 * @mtd: MTD structure
296 * @ecc_code: ecc_code buffer
297 * Using noninverted ECC can be considered ugly since writing a blank
298 * page ie. padding will clear the ECC bytes. This is no problem as
299 * long nobody is trying to write data on the seemingly unused page.
300 * Reading an erased page will produce an ECC mismatch between
301 * generated and read ECC bytes that has to be dealt with separately.
302 * E.g. if page is 0xFF (fresh erased), and if HW ECC engine within GPMC
303 * is used, the result of read will be 0x0 while the ECC offsets of the
304 * spare area will be 0xFF which will result in an ECC mismatch.
306 static int omap_calculate_ecc(struct mtd_info *mtd, const uint8_t *dat,
309 struct nand_chip *chip = mtd->priv;
310 struct omap_nand_info *info = chip->priv;
311 uint32_t *ptr, val = 0;
314 switch (info->ecc_scheme) {
315 case OMAP_ECC_HAM1_CODE_HW:
316 val = readl(&gpmc_cfg->ecc1_result);
317 ecc_code[0] = val & 0xFF;
318 ecc_code[1] = (val >> 16) & 0xFF;
319 ecc_code[2] = ((val >> 8) & 0x0F) | ((val >> 20) & 0xF0);
322 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
324 case OMAP_ECC_BCH8_CODE_HW:
325 ptr = &gpmc_cfg->bch_result_0_3[0].bch_result_x[3];
327 ecc_code[i++] = (val >> 0) & 0xFF;
329 for (j = 0; j < 3; j++) {
331 ecc_code[i++] = (val >> 24) & 0xFF;
332 ecc_code[i++] = (val >> 16) & 0xFF;
333 ecc_code[i++] = (val >> 8) & 0xFF;
334 ecc_code[i++] = (val >> 0) & 0xFF;
338 case OMAP_ECC_BCH16_CODE_HW:
339 val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[2]);
340 ecc_code[i++] = (val >> 8) & 0xFF;
341 ecc_code[i++] = (val >> 0) & 0xFF;
342 val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[1]);
343 ecc_code[i++] = (val >> 24) & 0xFF;
344 ecc_code[i++] = (val >> 16) & 0xFF;
345 ecc_code[i++] = (val >> 8) & 0xFF;
346 ecc_code[i++] = (val >> 0) & 0xFF;
347 val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[0]);
348 ecc_code[i++] = (val >> 24) & 0xFF;
349 ecc_code[i++] = (val >> 16) & 0xFF;
350 ecc_code[i++] = (val >> 8) & 0xFF;
351 ecc_code[i++] = (val >> 0) & 0xFF;
352 for (j = 3; j >= 0; j--) {
353 val = readl(&gpmc_cfg->bch_result_0_3[0].bch_result_x[j]
355 ecc_code[i++] = (val >> 24) & 0xFF;
356 ecc_code[i++] = (val >> 16) & 0xFF;
357 ecc_code[i++] = (val >> 8) & 0xFF;
358 ecc_code[i++] = (val >> 0) & 0xFF;
364 /* ECC scheme specific syndrome customizations */
365 switch (info->ecc_scheme) {
366 case OMAP_ECC_HAM1_CODE_HW:
369 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
371 for (i = 0; i < chip->ecc.bytes; i++)
372 *(ecc_code + i) = *(ecc_code + i) ^
376 case OMAP_ECC_BCH8_CODE_HW:
377 ecc_code[chip->ecc.bytes - 1] = 0x00;
379 case OMAP_ECC_BCH16_CODE_HW:
387 #ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
389 #define PREFETCH_CONFIG1_CS_SHIFT 24
390 #define PREFETCH_FIFOTHRESHOLD_MAX 0x40
391 #define PREFETCH_FIFOTHRESHOLD(val) ((val) << 8)
392 #define PREFETCH_STATUS_COUNT(val) (val & 0x00003fff)
393 #define PREFETCH_STATUS_FIFO_CNT(val) ((val >> 24) & 0x7F)
394 #define ENABLE_PREFETCH (1 << 7)
397 * omap_prefetch_enable - configures and starts prefetch transfer
398 * @fifo_th: fifo threshold to be used for read/ write
399 * @count: number of bytes to be transferred
400 * @is_write: prefetch read(0) or write post(1) mode
401 * @cs: chip select to use
403 static int omap_prefetch_enable(int fifo_th, unsigned int count, int is_write, int cs)
407 if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX)
410 if (readl(&gpmc_cfg->prefetch_control))
413 /* Set the amount of bytes to be prefetched */
414 writel(count, &gpmc_cfg->prefetch_config2);
416 val = (cs << PREFETCH_CONFIG1_CS_SHIFT) | (is_write & 1) |
417 PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH;
418 writel(val, &gpmc_cfg->prefetch_config1);
420 /* Start the prefetch engine */
421 writel(1, &gpmc_cfg->prefetch_control);
427 * omap_prefetch_reset - disables and stops the prefetch engine
429 static void omap_prefetch_reset(void)
431 writel(0, &gpmc_cfg->prefetch_control);
432 writel(0, &gpmc_cfg->prefetch_config1);
435 static int __read_prefetch_aligned(struct nand_chip *chip, uint32_t *buf, int len)
439 struct omap_nand_info *info = chip->priv;
441 ret = omap_prefetch_enable(PREFETCH_FIFOTHRESHOLD_MAX, len, 0, info->cs);
448 cnt = readl(&gpmc_cfg->prefetch_status);
449 cnt = PREFETCH_STATUS_FIFO_CNT(cnt);
451 for (i = 0; i < cnt / 4; i++) {
452 *buf++ = readl(CONFIG_SYS_NAND_BASE);
457 omap_prefetch_reset();
462 static inline void omap_nand_read(struct mtd_info *mtd, uint8_t *buf, int len)
464 struct nand_chip *chip = mtd->priv;
466 if (chip->options & NAND_BUSWIDTH_16)
467 nand_read_buf16(mtd, buf, len);
469 nand_read_buf(mtd, buf, len);
472 static void omap_nand_read_prefetch(struct mtd_info *mtd, uint8_t *buf, int len)
476 struct nand_chip *chip = mtd->priv;
479 * If the destination buffer is unaligned, start with reading
480 * the overlap byte-wise.
482 head = ((uint32_t) buf) % 4;
484 omap_nand_read(mtd, buf, head);
490 * Only transfer multiples of 4 bytes in a pre-fetched fashion.
491 * If there's a residue, care for it byte-wise afterwards.
495 ret = __read_prefetch_aligned(chip, (uint32_t *)buf, len - tail);
497 /* fallback in case the prefetch engine is busy */
498 omap_nand_read(mtd, buf, len);
501 omap_nand_read(mtd, buf, tail);
504 #endif /* CONFIG_NAND_OMAP_GPMC_PREFETCH */
506 #ifdef CONFIG_NAND_OMAP_ELM
508 * omap_reverse_list - re-orders list elements in reverse order [internal]
509 * @list: pointer to start of list
510 * @length: length of list
512 static void omap_reverse_list(u8 *list, unsigned int length)
515 unsigned int half_length = length / 2;
517 for (i = 0, j = length - 1; i < half_length; i++, j--) {
525 * omap_correct_data_bch - Compares the ecc read from nand spare area
526 * with ECC registers values and corrects one bit error if it has occured
528 * @mtd: MTD device structure
530 * @read_ecc: ecc read from nand flash (ignored)
531 * @calc_ecc: ecc read from ECC registers
533 * @return 0 if data is OK or corrected, else returns -1
535 static int omap_correct_data_bch(struct mtd_info *mtd, uint8_t *dat,
536 uint8_t *read_ecc, uint8_t *calc_ecc)
538 struct nand_chip *chip = mtd->priv;
539 struct omap_nand_info *info = chip->priv;
540 struct nand_ecc_ctrl *ecc = &chip->ecc;
541 uint32_t error_count = 0, error_max;
542 uint32_t error_loc[ELM_MAX_ERROR_COUNT];
543 enum bch_level bch_type;
544 uint32_t i, ecc_flag = 0;
546 uint32_t byte_pos, bit_pos;
549 /* check calculated ecc */
550 for (i = 0; i < ecc->bytes && !ecc_flag; i++) {
551 if (calc_ecc[i] != 0x00)
557 /* check for whether its a erased-page */
559 for (i = 0; i < ecc->bytes && !ecc_flag; i++) {
560 if (read_ecc[i] != 0xff)
567 * while reading ECC result we read it in big endian.
568 * Hence while loading to ELM we have rotate to get the right endian.
570 switch (info->ecc_scheme) {
571 case OMAP_ECC_BCH8_CODE_HW:
572 bch_type = BCH_8_BIT;
573 omap_reverse_list(calc_ecc, ecc->bytes - 1);
575 case OMAP_ECC_BCH16_CODE_HW:
576 bch_type = BCH_16_BIT;
577 omap_reverse_list(calc_ecc, ecc->bytes);
582 /* use elm module to check for errors */
583 elm_config(bch_type);
584 err = elm_check_error(calc_ecc, bch_type, &error_count, error_loc);
588 /* correct bch error */
589 for (count = 0; count < error_count; count++) {
590 switch (info->ecc_scheme) {
591 case OMAP_ECC_BCH8_CODE_HW:
592 /* 14th byte in ECC is reserved to match ROM layout */
593 error_max = SECTOR_BYTES + (ecc->bytes - 1);
595 case OMAP_ECC_BCH16_CODE_HW:
596 error_max = SECTOR_BYTES + ecc->bytes;
601 byte_pos = error_max - (error_loc[count] / 8) - 1;
602 bit_pos = error_loc[count] % 8;
603 if (byte_pos < SECTOR_BYTES) {
604 dat[byte_pos] ^= 1 << bit_pos;
605 printf("nand: bit-flip corrected @data=%d\n", byte_pos);
606 } else if (byte_pos < error_max) {
607 read_ecc[byte_pos - SECTOR_BYTES] ^= 1 << bit_pos;
608 printf("nand: bit-flip corrected @oob=%d\n", byte_pos -
612 printf("nand: error: invalid bit-flip location\n");
615 return (err) ? err : error_count;
619 * omap_read_page_bch - hardware ecc based page read function
620 * @mtd: mtd info structure
621 * @chip: nand chip info structure
622 * @buf: buffer to store read data
623 * @oob_required: caller expects OOB data read to chip->oob_poi
624 * @page: page number to read
627 static int omap_read_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
628 uint8_t *buf, int oob_required, int page)
630 int i, eccsize = chip->ecc.size;
631 int eccbytes = chip->ecc.bytes;
632 int eccsteps = chip->ecc.steps;
634 uint8_t *ecc_calc = chip->buffers->ecccalc;
635 uint8_t *ecc_code = chip->buffers->ecccode;
636 uint32_t *eccpos = chip->ecc.layout->eccpos;
637 uint8_t *oob = &chip->oob_poi[eccpos[0]];
643 oob_pos = (eccsize * eccsteps) + eccpos[0];
645 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize,
647 chip->ecc.hwctl(mtd, NAND_ECC_READ);
649 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_pos, -1);
650 chip->read_buf(mtd, p, eccsize);
652 /* read respective ecc from oob area */
653 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1);
654 chip->read_buf(mtd, oob, eccbytes);
656 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
659 /* reread the OOB area to get the metadata */
660 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, page);
661 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
668 for (i = 0; i < chip->ecc.total; i++)
669 ecc_code[i] = chip->oob_poi[eccpos[i]];
671 eccsteps = chip->ecc.steps;
674 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
677 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
679 mtd->ecc_stats.failed++;
681 mtd->ecc_stats.corrected += stat;
685 #endif /* CONFIG_NAND_OMAP_ELM */
688 * OMAP3 BCH8 support (with BCH library)
692 * omap_correct_data_bch_sw - Decode received data and correct errors
693 * @mtd: MTD device structure
695 * @read_ecc: ecc read from nand flash
696 * @calc_ecc: ecc read from HW ECC registers
698 static int omap_correct_data_bch_sw(struct mtd_info *mtd, u_char *data,
699 u_char *read_ecc, u_char *calc_ecc)
702 /* cannot correct more than 8 errors */
703 unsigned int errloc[8];
704 struct nand_chip *chip = mtd->priv;
705 struct omap_nand_info *info = chip->priv;
707 count = decode_bch(info->control, NULL, 512, read_ecc, calc_ecc,
711 for (i = 0; i < count; i++) {
712 /* correct data only, not ecc bytes */
713 if (errloc[i] < 8*512)
714 data[errloc[i]/8] ^= 1 << (errloc[i] & 7);
715 printf("corrected bitflip %u\n", errloc[i]);
717 printf("read_ecc: ");
719 * BCH8 have 13 bytes of ECC; BCH4 needs adoption
722 for (i = 0; i < 13; i++)
723 printf("%02x ", read_ecc[i]);
725 printf("calc_ecc: ");
726 for (i = 0; i < 13; i++)
727 printf("%02x ", calc_ecc[i]);
731 } else if (count < 0) {
732 printf("ecc unrecoverable error\n");
738 * omap_free_bch - Release BCH ecc resources
739 * @mtd: MTD device structure
741 static void __maybe_unused omap_free_bch(struct mtd_info *mtd)
743 struct nand_chip *chip = mtd->priv;
744 struct omap_nand_info *info = chip->priv;
747 free_bch(info->control);
748 info->control = NULL;
751 #endif /* CONFIG_BCH */
754 * omap_select_ecc_scheme - configures driver for particular ecc-scheme
755 * @nand: NAND chip device structure
756 * @ecc_scheme: ecc scheme to configure
757 * @pagesize: number of main-area bytes per page of NAND device
758 * @oobsize: number of OOB/spare bytes per page of NAND device
760 static int omap_select_ecc_scheme(struct nand_chip *nand,
761 enum omap_ecc ecc_scheme, unsigned int pagesize, unsigned int oobsize) {
762 struct omap_nand_info *info = nand->priv;
763 struct nand_ecclayout *ecclayout = &omap_ecclayout;
764 int eccsteps = pagesize / SECTOR_BYTES;
767 switch (ecc_scheme) {
768 case OMAP_ECC_HAM1_CODE_SW:
769 debug("nand: selected OMAP_ECC_HAM1_CODE_SW\n");
770 /* For this ecc-scheme, ecc.bytes, ecc.layout, ... are
771 * initialized in nand_scan_tail(), so just set ecc.mode */
772 info->control = NULL;
773 nand->ecc.mode = NAND_ECC_SOFT;
774 nand->ecc.layout = NULL;
778 case OMAP_ECC_HAM1_CODE_HW:
779 debug("nand: selected OMAP_ECC_HAM1_CODE_HW\n");
780 /* check ecc-scheme requirements before updating ecc info */
781 if ((3 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
782 printf("nand: error: insufficient OOB: require=%d\n", (
783 (3 * eccsteps) + BADBLOCK_MARKER_LENGTH));
786 info->control = NULL;
787 /* populate ecc specific fields */
788 memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
789 nand->ecc.mode = NAND_ECC_HW;
790 nand->ecc.strength = 1;
791 nand->ecc.size = SECTOR_BYTES;
793 nand->ecc.hwctl = omap_enable_hwecc;
794 nand->ecc.correct = omap_correct_data;
795 nand->ecc.calculate = omap_calculate_ecc;
796 /* define ecc-layout */
797 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
798 for (i = 0; i < ecclayout->eccbytes; i++) {
799 if (nand->options & NAND_BUSWIDTH_16)
800 ecclayout->eccpos[i] = i + 2;
802 ecclayout->eccpos[i] = i + 1;
804 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
805 ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
806 BADBLOCK_MARKER_LENGTH;
809 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
811 debug("nand: selected OMAP_ECC_BCH8_CODE_HW_DETECTION_SW\n");
812 /* check ecc-scheme requirements before updating ecc info */
813 if ((13 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
814 printf("nand: error: insufficient OOB: require=%d\n", (
815 (13 * eccsteps) + BADBLOCK_MARKER_LENGTH));
818 /* check if BCH S/W library can be used for error detection */
819 info->control = init_bch(13, 8, 0x201b);
820 if (!info->control) {
821 printf("nand: error: could not init_bch()\n");
824 /* populate ecc specific fields */
825 memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
826 nand->ecc.mode = NAND_ECC_HW;
827 nand->ecc.strength = 8;
828 nand->ecc.size = SECTOR_BYTES;
829 nand->ecc.bytes = 13;
830 nand->ecc.hwctl = omap_enable_hwecc;
831 nand->ecc.correct = omap_correct_data_bch_sw;
832 nand->ecc.calculate = omap_calculate_ecc;
833 /* define ecc-layout */
834 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
835 ecclayout->eccpos[0] = BADBLOCK_MARKER_LENGTH;
836 for (i = 1; i < ecclayout->eccbytes; i++) {
837 if (i % nand->ecc.bytes)
838 ecclayout->eccpos[i] =
839 ecclayout->eccpos[i - 1] + 1;
841 ecclayout->eccpos[i] =
842 ecclayout->eccpos[i - 1] + 2;
844 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
845 ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
846 BADBLOCK_MARKER_LENGTH;
849 printf("nand: error: CONFIG_BCH required for ECC\n");
853 case OMAP_ECC_BCH8_CODE_HW:
854 #ifdef CONFIG_NAND_OMAP_ELM
855 debug("nand: selected OMAP_ECC_BCH8_CODE_HW\n");
856 /* check ecc-scheme requirements before updating ecc info */
857 if ((14 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
858 printf("nand: error: insufficient OOB: require=%d\n", (
859 (14 * eccsteps) + BADBLOCK_MARKER_LENGTH));
862 /* intialize ELM for ECC error detection */
864 info->control = NULL;
865 /* populate ecc specific fields */
866 memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
867 nand->ecc.mode = NAND_ECC_HW;
868 nand->ecc.strength = 8;
869 nand->ecc.size = SECTOR_BYTES;
870 nand->ecc.bytes = 14;
871 nand->ecc.hwctl = omap_enable_hwecc;
872 nand->ecc.correct = omap_correct_data_bch;
873 nand->ecc.calculate = omap_calculate_ecc;
874 nand->ecc.read_page = omap_read_page_bch;
875 /* define ecc-layout */
876 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
877 for (i = 0; i < ecclayout->eccbytes; i++)
878 ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH;
879 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
880 ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
881 BADBLOCK_MARKER_LENGTH;
884 printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
888 case OMAP_ECC_BCH16_CODE_HW:
889 #ifdef CONFIG_NAND_OMAP_ELM
890 debug("nand: using OMAP_ECC_BCH16_CODE_HW\n");
891 /* check ecc-scheme requirements before updating ecc info */
892 if ((26 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
893 printf("nand: error: insufficient OOB: require=%d\n", (
894 (26 * eccsteps) + BADBLOCK_MARKER_LENGTH));
897 /* intialize ELM for ECC error detection */
899 /* populate ecc specific fields */
900 nand->ecc.mode = NAND_ECC_HW;
901 nand->ecc.size = SECTOR_BYTES;
902 nand->ecc.bytes = 26;
903 nand->ecc.strength = 16;
904 nand->ecc.hwctl = omap_enable_hwecc;
905 nand->ecc.correct = omap_correct_data_bch;
906 nand->ecc.calculate = omap_calculate_ecc;
907 nand->ecc.read_page = omap_read_page_bch;
908 /* define ecc-layout */
909 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
910 for (i = 0; i < ecclayout->eccbytes; i++)
911 ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH;
912 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
913 ecclayout->oobfree[0].length = oobsize - nand->ecc.bytes -
914 BADBLOCK_MARKER_LENGTH;
917 printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
921 debug("nand: error: ecc scheme not enabled or supported\n");
925 /* nand_scan_tail() sets ham1 sw ecc; hw ecc layout is set by driver */
926 if (ecc_scheme != OMAP_ECC_HAM1_CODE_SW)
927 nand->ecc.layout = ecclayout;
929 info->ecc_scheme = ecc_scheme;
933 #ifndef CONFIG_SPL_BUILD
935 * omap_nand_switch_ecc - switch the ECC operation between different engines
936 * (h/w and s/w) and different algorithms (hamming and BCHx)
938 * @hardware - true if one of the HW engines should be used
939 * @eccstrength - the number of bits that could be corrected
940 * (1 - hamming, 4 - BCH4, 8 - BCH8, 16 - BCH16)
942 int __maybe_unused omap_nand_switch_ecc(uint32_t hardware, uint32_t eccstrength)
944 struct nand_chip *nand;
945 struct mtd_info *mtd;
948 if (nand_curr_device < 0 ||
949 nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
950 !nand_info[nand_curr_device].name) {
951 printf("nand: error: no NAND devices found\n");
955 mtd = &nand_info[nand_curr_device];
957 nand->options |= NAND_OWN_BUFFERS;
958 nand->options &= ~NAND_SUBPAGE_READ;
959 /* Setup the ecc configurations again */
961 if (eccstrength == 1) {
962 err = omap_select_ecc_scheme(nand,
963 OMAP_ECC_HAM1_CODE_HW,
964 mtd->writesize, mtd->oobsize);
965 } else if (eccstrength == 8) {
966 err = omap_select_ecc_scheme(nand,
967 OMAP_ECC_BCH8_CODE_HW,
968 mtd->writesize, mtd->oobsize);
970 printf("nand: error: unsupported ECC scheme\n");
974 if (eccstrength == 1) {
975 err = omap_select_ecc_scheme(nand,
976 OMAP_ECC_HAM1_CODE_SW,
977 mtd->writesize, mtd->oobsize);
978 } else if (eccstrength == 8) {
979 err = omap_select_ecc_scheme(nand,
980 OMAP_ECC_BCH8_CODE_HW_DETECTION_SW,
981 mtd->writesize, mtd->oobsize);
983 printf("nand: error: unsupported ECC scheme\n");
988 /* Update NAND handling after ECC mode switch */
990 err = nand_scan_tail(mtd);
993 #endif /* CONFIG_SPL_BUILD */
996 * Board-specific NAND initialization. The following members of the
997 * argument are board-specific:
998 * - IO_ADDR_R: address to read the 8 I/O lines of the flash device
999 * - IO_ADDR_W: address to write the 8 I/O lines of the flash device
1000 * - cmd_ctrl: hardwarespecific function for accesing control-lines
1001 * - waitfunc: hardwarespecific function for accesing device ready/busy line
1002 * - ecc.hwctl: function to enable (reset) hardware ecc generator
1003 * - ecc.mode: mode of ecc, see defines
1004 * - chip_delay: chip dependent delay for transfering data from array to
1006 * - options: various chip options. They can partly be set to inform
1007 * nand_scan about special functionality. See the defines for further
1010 int board_nand_init(struct nand_chip *nand)
1012 int32_t gpmc_config = 0;
1016 * xloader/Uboot's gpmc configuration would have configured GPMC for
1017 * nand type of memory. The following logic scans and latches on to the
1018 * first CS with NAND type memory.
1019 * TBD: need to make this logic generic to handle multiple CS NAND
1022 while (cs < GPMC_MAX_CS) {
1023 /* Check if NAND type is set */
1024 if ((readl(&gpmc_cfg->cs[cs].config1) & 0xC00) == 0x800) {
1030 if (cs >= GPMC_MAX_CS) {
1031 printf("nand: error: Unable to find NAND settings in "
1032 "GPMC Configuration - quitting\n");
1036 gpmc_config = readl(&gpmc_cfg->config);
1037 /* Disable Write protect */
1038 gpmc_config |= 0x10;
1039 writel(gpmc_config, &gpmc_cfg->config);
1041 nand->IO_ADDR_R = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
1042 nand->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
1043 omap_nand_info[cs].control = NULL;
1044 omap_nand_info[cs].cs = cs;
1045 omap_nand_info[cs].ws = wscfg[cs];
1046 nand->priv = &omap_nand_info[cs];
1047 nand->cmd_ctrl = omap_nand_hwcontrol;
1048 nand->options |= NAND_NO_PADDING | NAND_CACHEPRG;
1049 nand->chip_delay = 100;
1050 nand->ecc.layout = &omap_ecclayout;
1052 /* configure driver and controller based on NAND device bus-width */
1053 gpmc_config = readl(&gpmc_cfg->cs[cs].config1);
1054 #if defined(CONFIG_SYS_NAND_BUSWIDTH_16BIT)
1055 nand->options |= NAND_BUSWIDTH_16;
1056 writel(gpmc_config | (0x1 << 12), &gpmc_cfg->cs[cs].config1);
1058 nand->options &= ~NAND_BUSWIDTH_16;
1059 writel(gpmc_config & ~(0x1 << 12), &gpmc_cfg->cs[cs].config1);
1061 /* select ECC scheme */
1062 #if defined(CONFIG_NAND_OMAP_ECCSCHEME)
1063 err = omap_select_ecc_scheme(nand, CONFIG_NAND_OMAP_ECCSCHEME,
1064 CONFIG_SYS_NAND_PAGE_SIZE, CONFIG_SYS_NAND_OOBSIZE);
1066 /* pagesize and oobsize are not required to configure sw ecc-scheme */
1067 err = omap_select_ecc_scheme(nand, OMAP_ECC_HAM1_CODE_SW,
1072 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1073 if (nand->ecc.layout) {
1074 bbt_main_descr.offs = nand->ecc.layout->oobfree[0].offset;
1075 bbt_main_descr.veroffs = bbt_main_descr.offs +
1076 sizeof(bbt_pattern);
1078 bbt_mirror_descr.offs = nand->ecc.layout->oobfree[0].offset;
1079 bbt_mirror_descr.veroffs = bbt_mirror_descr.offs +
1080 sizeof(mirror_pattern);
1083 nand->bbt_options |= NAND_BBT_USE_FLASH;
1084 nand->bbt_td = &bbt_main_descr;
1085 nand->bbt_md = &bbt_mirror_descr;
1088 #ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
1089 nand->read_buf = omap_nand_read_prefetch;
1091 if (nand->options & NAND_BUSWIDTH_16)
1092 nand->read_buf = nand_read_buf16;
1094 nand->read_buf = nand_read_buf;
1097 nand->dev_ready = omap_dev_ready;