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 <linux/mtd/omap_gpmc.h>
13 #include <linux/mtd/nand_ecc.h>
14 #include <linux/bch.h>
15 #include <linux/compiler.h>
17 #include <linux/mtd/omap_elm.h>
19 #define BADBLOCK_MARKER_LENGTH 2
20 #define SECTOR_BYTES 512
21 #define ECCCLEAR (0x1 << 8)
22 #define ECCRESULTREG1 (0x1 << 0)
23 /* 4 bit padding to make byte aligned, 56 = 52 + 4 */
24 #define BCH4_BIT_PAD 4
27 static u8 bch8_polynomial[] = {0xef, 0x51, 0x2e, 0x09, 0xed, 0x93, 0x9a, 0xc2,
28 0x97, 0x79, 0xe5, 0x24, 0xb5};
30 static uint8_t cs_next;
31 static __maybe_unused struct nand_ecclayout omap_ecclayout;
33 #if defined(CONFIG_NAND_OMAP_GPMC_WSCFG)
34 static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE] =
35 { CONFIG_NAND_OMAP_GPMC_WSCFG };
37 /* wscfg is preset to zero since its a static variable */
38 static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE];
42 * Driver configurations
44 struct omap_nand_info {
45 struct bch_control *control;
46 enum omap_ecc ecc_scheme;
48 uint8_t ws; /* wait status pin (0,1) */
51 /* We are wasting a bit of memory but al least we are safe */
52 static struct omap_nand_info omap_nand_info[GPMC_MAX_CS];
54 static struct gpmc __iomem *gpmc_cfg = (void __iomem *)GPMC_BASE;
56 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
57 static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
58 static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
60 static struct nand_bbt_descr bbt_main_descr = {
61 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
62 NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
63 .offs = 0, /* may be overwritten depending on ECC layout */
65 .veroffs = 4, /* may be overwritten depending on ECC layout */
67 .pattern = bbt_pattern,
70 static struct nand_bbt_descr bbt_mirror_descr = {
71 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
72 NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
73 .offs = 0, /* may be overwritten depending on ECC layout */
75 .veroffs = 4, /* may be overwritten depending on ECC layout */
77 .pattern = mirror_pattern,
81 #define PREFETCH_FIFOTHRESHOLD_MAX 0x40
82 #define PREFETCH_FIFOTHRESHOLD(val) ((val) << 8)
84 #define PREFETCH_ENABLEOPTIMIZEDACCESS (0x1 << 27)
86 #define GPMC_PREFETCH_STATUS_FIFO_CNT(val) (((val) >> 24) & 0x7F)
87 #define GPMC_PREFETCH_STATUS_COUNT(val) ((val) & 0x00003fff)
89 #define CS_NUM_SHIFT 24
90 #define ENABLE_PREFETCH (0x1 << 7)
91 #define DMA_MPU_MODE 2
93 #define OMAP_NAND_TIMEOUT_MS 5000
95 #define PRINT_REG(x) debug("+++ %.15s (0x%08x)=0x%08x\n", #x, &gpmc_cfg->x, readl(&gpmc_cfg->x))
97 #ifdef CONFIG_SYS_GPMC_PREFETCH_ENABLE
99 * gpmc_prefetch_enable - configures and starts prefetch transfer
100 * @cs: cs (chip select) number
101 * @fifo_th: fifo threshold to be used for read/ write
102 * @count: number of bytes to be transferred
103 * @is_write: prefetch read(0) or write post(1) mode
105 static inline void gpmc_prefetch_enable(int cs, int fifo_th,
106 unsigned int count, int is_write)
108 writel(count, &gpmc_cfg->pref_config2);
110 /* Set the prefetch read / post write and enable the engine.
111 * Set which cs is has requested for.
113 uint32_t val = (cs << CS_NUM_SHIFT) |
114 PREFETCH_ENABLEOPTIMIZEDACCESS |
115 PREFETCH_FIFOTHRESHOLD(fifo_th) |
118 writel(val, &gpmc_cfg->pref_config1);
120 /* Start the prefetch engine */
121 writel(0x1, &gpmc_cfg->pref_control);
125 * gpmc_prefetch_reset - disables and stops the prefetch engine
127 static inline void gpmc_prefetch_reset(void)
129 /* Stop the PFPW engine */
130 writel(0x0, &gpmc_cfg->pref_control);
132 /* Reset/disable the PFPW engine */
133 writel(0x0, &gpmc_cfg->pref_config1);
136 //#define FIFO_IOADDR (nand->IO_ADDR_R)
137 #define FIFO_IOADDR PISMO1_NAND_BASE
140 * read_buf_pref - read data from NAND controller into buffer
141 * @mtd: MTD device structure
142 * @buf: buffer to store date
143 * @len: number of bytes to read
145 static void read_buf_pref(struct mtd_info *mtd, u_char *buf, int len)
147 gpmc_prefetch_enable(cs, PREFETCH_FIFOTHRESHOLD_MAX, len, 0);
149 // Get number of bytes waiting in the FIFO
150 uint32_t read_bytes = GPMC_PREFETCH_STATUS_FIFO_CNT(readl(&gpmc_cfg->pref_status));
154 // Alignment of Destination Buffer
155 while (read_bytes && ((unsigned int)buf & 3)) {
156 *buf++ = readb(FIFO_IOADDR);
160 // Use maximum word size (32bit) inside this loop, because speed is limited by
161 // GPMC bus arbitration with a maximum transfer rate of 3.000.000/sec.
162 len -= read_bytes & ~3;
163 while (read_bytes >= 4) {
164 *((uint32_t*)buf) = readl(FIFO_IOADDR);
168 // Transfer the last (non-aligned) bytes only at the last iteration,
169 // to maintain full speed up to the end of the transfer.
170 if (read_bytes == len) {
172 *buf++ = readb(FIFO_IOADDR);
178 gpmc_prefetch_reset();
182 * write_buf_pref - write buffer to NAND controller
183 * @mtd: MTD device structure
185 * @len: number of bytes to write
187 static void write_buf_pref(struct mtd_info *mtd, const u_char *buf, int len)
189 /* configure and start prefetch transfer */
190 gpmc_prefetch_enable(cs, PREFETCH_FIFOTHRESHOLD_MAX, len, 1);
193 // Get number of free bytes in the FIFO
194 uint32_t write_bytes = GPMC_PREFETCH_STATUS_FIFO_CNT(readl(&gpmc_cfg->pref_status));
196 // don't write more bytes than requested
197 if (write_bytes > len)
200 // Alignment of Source Buffer
201 while (write_bytes && ((unsigned int)buf & 3)) {
202 writeb(*buf++, FIFO_IOADDR);
207 // Use maximum word size (32bit) inside this loop, because speed is limited by
208 // GPMC bus arbitration with a maximum transfer rate of 3.000.000/sec.
209 len -= write_bytes & ~3;
210 while (write_bytes >= 4) {
211 writel(*((uint32_t*)buf), FIFO_IOADDR);
216 // Transfer the last (non-aligned) bytes only at the last iteration,
217 // to maintain full speed up to the end of the transfer.
218 if (write_bytes == len) {
219 while (write_bytes) {
220 writeb(*buf++, FIFO_IOADDR);
227 /* wait for data to be flushed out before resetting the prefetch */
228 while ((len = GPMC_PREFETCH_STATUS_COUNT(readl(&gpmc_cfg->pref_status)))) {
229 debug("%u bytes still in FIFO\n", PREFETCH_FIFOTHRESHOLD_MAX - len);
233 /* disable and stop the PFPW engine */
234 gpmc_prefetch_reset();
236 #endif /* CONFIG_SYS_GPMC_PREFETCH_ENABLE */
239 * omap_nand_hwcontrol - Set the address pointers corretly for the
240 * following address/data/command operation
242 static void omap_nand_hwcontrol(struct mtd_info *mtd, int32_t cmd,
245 register struct nand_chip *this = mtd->priv;
246 struct omap_nand_info *info = this->priv;
250 * Point the IO_ADDR to DATA and ADDRESS registers instead
254 case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
255 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
257 case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
258 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_adr;
260 case NAND_CTRL_CHANGE | NAND_NCE:
261 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
265 if (cmd != NAND_CMD_NONE)
266 writeb(cmd, this->IO_ADDR_W);
269 /* Check wait pin as dev ready indicator */
270 static int omap_dev_ready(struct mtd_info *mtd)
272 return !!(readl(&gpmc_cfg->status) & (1 << (8 + info->ws)));
276 * gen_true_ecc - This function will generate true ECC value, which
277 * can be used when correcting data read from NAND flash memory core
279 * @ecc_buf: buffer to store ecc code
281 * @return: re-formatted ECC value
283 static uint32_t gen_true_ecc(uint8_t *ecc_buf)
285 return ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xF0) << 20) |
286 ((ecc_buf[2] & 0x0F) << 8);
290 * omap_correct_data - Compares the ecc read from nand spare area with ECC
291 * registers values and corrects one bit error if it has occured
292 * Further details can be had from OMAP TRM and the following selected links:
293 * http://en.wikipedia.org/wiki/Hamming_code
294 * http://www.cs.utexas.edu/users/plaxton/c/337/05f/slides/ErrorCorrection-4.pdf
296 * @mtd: MTD device structure
298 * @read_ecc: ecc read from nand flash
299 * @calc_ecc: ecc read from ECC registers
301 * @return 0 if data is OK or corrected, else returns -1
303 static int __maybe_unused omap_correct_data(struct mtd_info *mtd, uint8_t *dat,
304 uint8_t *read_ecc, uint8_t *calc_ecc)
306 uint32_t orig_ecc, new_ecc, res, hm;
307 uint16_t parity_bits, byte;
310 /* Regenerate the orginal ECC */
311 orig_ecc = gen_true_ecc(read_ecc);
312 new_ecc = gen_true_ecc(calc_ecc);
313 /* Get the XOR of real ecc */
314 res = orig_ecc ^ new_ecc;
316 /* Get the hamming width */
318 /* Single bit errors can be corrected! */
320 /* Correctable data! */
321 parity_bits = res >> 16;
322 bit = (parity_bits & 0x7);
323 byte = (parity_bits >> 3) & 0x1FF;
324 /* Flip the bit to correct */
325 dat[byte] ^= (0x1 << bit);
326 } else if (hm == 1) {
327 printf("Error: Ecc is wrong\n");
328 /* ECC itself is corrupted */
332 * hm distance != parity pairs OR one, could mean 2 bit
333 * error OR potentially be on a blank page..
334 * orig_ecc: contains spare area data from nand flash.
335 * new_ecc: generated ecc while reading data area.
336 * Note: if the ecc = 0, all data bits from which it was
337 * generated are 0xFF.
338 * The 3 byte(24 bits) ecc is generated per 512byte
339 * chunk of a page. If orig_ecc(from spare area)
340 * is 0xFF && new_ecc(computed now from data area)=0x0,
341 * this means that data area is 0xFF and spare area is
342 * 0xFF. A sure sign of a erased page!
344 if ((orig_ecc == 0x0FFF0FFF) && (new_ecc == 0x00000000))
346 printf("Error: Bad compare! failed\n");
347 /* detected 2 bit error */
355 * omap_enable_hwecc - configures GPMC as per ECC scheme before read/write
356 * @mtd: MTD device structure
357 * @mode: Read/Write mode
360 static void omap_enable_hwecc(struct mtd_info *mtd, int32_t mode)
362 struct nand_chip *nand = mtd->priv;
363 struct omap_nand_info *info = nand->priv;
364 unsigned int dev_width = (nand->options & NAND_BUSWIDTH_16) ? 1 : 0;
365 unsigned int ecc_algo = 0;
366 unsigned int bch_type = 0;
367 unsigned int eccsize1 = 0x00, eccsize0 = 0x00, bch_wrapmode = 0x00;
368 u32 ecc_size_config_val = 0;
369 u32 ecc_config_val = 0;
372 /* configure GPMC for specific ecc-scheme */
373 switch (info->ecc_scheme) {
374 case OMAP_ECC_HAM1_CODE_SW:
376 case OMAP_ECC_HAM1_CODE_HW:
383 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
384 case OMAP_ECC_BCH8_CODE_HW:
387 if (mode == NAND_ECC_WRITE) {
389 eccsize0 = 0; /* extra bits in nibbles per sector */
390 eccsize1 = 28; /* OOB bits in nibbles per sector */
393 eccsize0 = 26; /* ECC bits in nibbles per sector */
394 eccsize1 = 2; /* non-ECC bits in nibbles per sector */
397 case OMAP_ECC_BCH16_CODE_HW:
400 if (mode == NAND_ECC_WRITE) {
402 eccsize0 = 0; /* extra bits in nibbles per sector */
403 eccsize1 = 52; /* OOB bits in nibbles per sector */
406 eccsize0 = 52; /* ECC bits in nibbles per sector */
407 eccsize1 = 0; /* non-ECC bits in nibbles per sector */
413 /* Clear ecc and enable bits */
414 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
415 /* Configure ecc size for BCH */
416 ecc_size_config_val = (eccsize1 << 22) | (eccsize0 << 12);
417 writel(ecc_size_config_val, &gpmc_cfg->ecc_size_config);
419 /* Configure device details for BCH engine */
420 ecc_config_val = ((ecc_algo << 16) | /* HAM1 | BCHx */
421 (bch_type << 12) | /* BCH4/BCH8/BCH16 */
422 (bch_wrapmode << 8) | /* wrap mode */
423 (dev_width << 7) | /* bus width */
424 (0x0 << 4) | /* number of sectors */
425 (cs << 1) | /* ECC CS */
426 (0x1)); /* enable ECC */
427 writel(ecc_config_val, &gpmc_cfg->ecc_config);
431 * omap_calculate_ecc - Read ECC result
432 * @mtd: MTD structure
434 * @ecc_code: ecc_code buffer
435 * Using noninverted ECC can be considered ugly since writing a blank
436 * page ie. padding will clear the ECC bytes. This is no problem as
437 * long nobody is trying to write data on the seemingly unused page.
438 * Reading an erased page will produce an ECC mismatch between
439 * generated and read ECC bytes that has to be dealt with separately.
440 * E.g. if page is 0xFF (fresh erased), and if HW ECC engine within GPMC
441 * is used, the result of read will be 0x0 while the ECC offsets of the
442 * spare area will be 0xFF which will result in an ECC mismatch.
444 static int omap_calculate_ecc(struct mtd_info *mtd, const uint8_t *dat,
447 struct nand_chip *chip = mtd->priv;
448 struct omap_nand_info *info = chip->priv;
449 uint32_t *ptr, val = 0;
452 switch (info->ecc_scheme) {
453 case OMAP_ECC_HAM1_CODE_HW:
454 val = readl(&gpmc_cfg->ecc1_result);
455 ecc_code[0] = val & 0xFF;
456 ecc_code[1] = (val >> 16) & 0xFF;
457 ecc_code[2] = ((val >> 8) & 0x0F) | ((val >> 20) & 0xF0);
460 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
462 case OMAP_ECC_BCH8_CODE_HW:
463 ptr = &gpmc_cfg->bch_result_0_3[0].bch_result_x[3];
465 ecc_code[i++] = (val >> 0) & 0xFF;
467 for (j = 0; j < 3; j++) {
469 ecc_code[i++] = (val >> 24) & 0xFF;
470 ecc_code[i++] = (val >> 16) & 0xFF;
471 ecc_code[i++] = (val >> 8) & 0xFF;
472 ecc_code[i++] = (val >> 0) & 0xFF;
476 case OMAP_ECC_BCH16_CODE_HW:
477 val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[2]);
478 ecc_code[i++] = (val >> 8) & 0xFF;
479 ecc_code[i++] = (val >> 0) & 0xFF;
480 val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[1]);
481 ecc_code[i++] = (val >> 24) & 0xFF;
482 ecc_code[i++] = (val >> 16) & 0xFF;
483 ecc_code[i++] = (val >> 8) & 0xFF;
484 ecc_code[i++] = (val >> 0) & 0xFF;
485 val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[0]);
486 ecc_code[i++] = (val >> 24) & 0xFF;
487 ecc_code[i++] = (val >> 16) & 0xFF;
488 ecc_code[i++] = (val >> 8) & 0xFF;
489 ecc_code[i++] = (val >> 0) & 0xFF;
490 for (j = 3; j >= 0; j--) {
491 val = readl(&gpmc_cfg->bch_result_0_3[0].bch_result_x[j]
493 ecc_code[i++] = (val >> 24) & 0xFF;
494 ecc_code[i++] = (val >> 16) & 0xFF;
495 ecc_code[i++] = (val >> 8) & 0xFF;
496 ecc_code[i++] = (val >> 0) & 0xFF;
502 /* ECC scheme specific syndrome customizations */
503 switch (info->ecc_scheme) {
504 case OMAP_ECC_HAM1_CODE_HW:
507 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
509 for (i = 0; i < chip->ecc.bytes; i++)
510 *(ecc_code + i) = *(ecc_code + i) ^
514 case OMAP_ECC_BCH8_CODE_HW:
515 ecc_code[chip->ecc.bytes - 1] = 0x00;
517 case OMAP_ECC_BCH16_CODE_HW:
525 #ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
527 #define PREFETCH_CONFIG1_CS_SHIFT 24
528 #define PREFETCH_FIFOTHRESHOLD_MAX 0x40
529 #define PREFETCH_FIFOTHRESHOLD(val) ((val) << 8)
530 #define PREFETCH_STATUS_COUNT(val) (val & 0x00003fff)
531 #define PREFETCH_STATUS_FIFO_CNT(val) ((val >> 24) & 0x7F)
532 #define ENABLE_PREFETCH (1 << 7)
535 * omap_prefetch_enable - configures and starts prefetch transfer
536 * @fifo_th: fifo threshold to be used for read/ write
537 * @count: number of bytes to be transferred
538 * @is_write: prefetch read(0) or write post(1) mode
539 * @cs: chip select to use
541 static int omap_prefetch_enable(int fifo_th, unsigned int count, int is_write, int cs)
545 if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX)
548 if (readl(&gpmc_cfg->prefetch_control))
551 /* Set the amount of bytes to be prefetched */
552 writel(count, &gpmc_cfg->prefetch_config2);
554 val = (cs << PREFETCH_CONFIG1_CS_SHIFT) | (is_write & 1) |
555 PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH;
556 writel(val, &gpmc_cfg->prefetch_config1);
558 /* Start the prefetch engine */
559 writel(1, &gpmc_cfg->prefetch_control);
565 * omap_prefetch_reset - disables and stops the prefetch engine
567 static void omap_prefetch_reset(void)
569 writel(0, &gpmc_cfg->prefetch_control);
570 writel(0, &gpmc_cfg->prefetch_config1);
573 static int __read_prefetch_aligned(struct nand_chip *chip, uint32_t *buf, int len)
577 struct omap_nand_info *info = chip->priv;
579 ret = omap_prefetch_enable(PREFETCH_FIFOTHRESHOLD_MAX, len, 0, info->cs);
586 cnt = readl(&gpmc_cfg->prefetch_status);
587 cnt = PREFETCH_STATUS_FIFO_CNT(cnt);
589 for (i = 0; i < cnt / 4; i++) {
590 *buf++ = readl(CONFIG_SYS_NAND_BASE);
595 omap_prefetch_reset();
600 static inline void omap_nand_read(struct mtd_info *mtd, uint8_t *buf, int len)
602 struct nand_chip *chip = mtd->priv;
604 if (chip->options & NAND_BUSWIDTH_16)
605 nand_read_buf16(mtd, buf, len);
607 nand_read_buf(mtd, buf, len);
610 static void omap_nand_read_prefetch(struct mtd_info *mtd, uint8_t *buf, int len)
614 struct nand_chip *chip = mtd->priv;
617 * If the destination buffer is unaligned, start with reading
618 * the overlap byte-wise.
620 head = ((uint32_t) buf) % 4;
622 omap_nand_read(mtd, buf, head);
628 * Only transfer multiples of 4 bytes in a pre-fetched fashion.
629 * If there's a residue, care for it byte-wise afterwards.
633 ret = __read_prefetch_aligned(chip, (uint32_t *)buf, len - tail);
635 /* fallback in case the prefetch engine is busy */
636 omap_nand_read(mtd, buf, len);
639 omap_nand_read(mtd, buf, tail);
642 #endif /* CONFIG_NAND_OMAP_GPMC_PREFETCH */
644 #ifdef CONFIG_NAND_OMAP_ELM
646 * omap_reverse_list - re-orders list elements in reverse order [internal]
647 * @list: pointer to start of list
648 * @length: length of list
650 static void omap_reverse_list(u8 *list, unsigned int length)
653 unsigned int half_length = length / 2;
655 for (i = 0, j = length - 1; i < half_length; i++, j--) {
663 * omap_correct_data_bch - Compares the ecc read from nand spare area
664 * with ECC registers values and corrects one bit error if it has occured
666 * @mtd: MTD device structure
668 * @read_ecc: ecc read from nand flash (ignored)
669 * @calc_ecc: ecc read from ECC registers
671 * @return 0 if data is OK or corrected, else returns -1
673 static int omap_correct_data_bch(struct mtd_info *mtd, uint8_t *dat,
674 uint8_t *read_ecc, uint8_t *calc_ecc)
676 struct nand_chip *chip = mtd->priv;
677 struct omap_nand_info *info = chip->priv;
678 struct nand_ecc_ctrl *ecc = &chip->ecc;
679 uint32_t error_count = 0, error_max;
680 uint32_t error_loc[ELM_MAX_ERROR_COUNT];
681 enum bch_level bch_type;
682 uint32_t i, ecc_flag = 0;
684 uint32_t byte_pos, bit_pos;
687 /* check calculated ecc */
688 for (i = 0; i < ecc->bytes && !ecc_flag; i++) {
689 if (calc_ecc[i] != 0x00)
695 /* check for whether its a erased-page */
697 for (i = 0; i < ecc->bytes && !ecc_flag; i++) {
698 if (read_ecc[i] != 0xff)
705 * while reading ECC result we read it in big endian.
706 * Hence while loading to ELM we have rotate to get the right endian.
708 switch (info->ecc_scheme) {
709 case OMAP_ECC_BCH8_CODE_HW:
710 bch_type = BCH_8_BIT;
711 omap_reverse_list(calc_ecc, ecc->bytes - 1);
713 case OMAP_ECC_BCH16_CODE_HW:
714 bch_type = BCH_16_BIT;
715 omap_reverse_list(calc_ecc, ecc->bytes);
720 /* use elm module to check for errors */
721 elm_config(bch_type);
722 err = elm_check_error(calc_ecc, bch_type, &error_count, error_loc);
726 /* correct bch error */
727 for (count = 0; count < error_count; count++) {
728 switch (info->ecc_scheme) {
729 case OMAP_ECC_BCH8_CODE_HW:
730 /* 14th byte in ECC is reserved to match ROM layout */
731 error_max = SECTOR_BYTES + (ecc->bytes - 1);
733 case OMAP_ECC_BCH16_CODE_HW:
734 error_max = SECTOR_BYTES + ecc->bytes;
739 byte_pos = error_max - (error_loc[count] / 8) - 1;
740 bit_pos = error_loc[count] % 8;
741 if (byte_pos < SECTOR_BYTES) {
742 dat[byte_pos] ^= 1 << bit_pos;
743 printf("nand: bit-flip corrected @data=%d\n", byte_pos);
744 } else if (byte_pos < error_max) {
745 read_ecc[byte_pos - SECTOR_BYTES] ^= 1 << bit_pos;
746 printf("nand: bit-flip corrected @oob=%d\n", byte_pos -
750 printf("nand: error: invalid bit-flip location\n");
753 return (err) ? err : error_count;
757 * omap_read_page_bch - hardware ecc based page read function
758 * @mtd: mtd info structure
759 * @chip: nand chip info structure
760 * @buf: buffer to store read data
761 * @oob_required: caller expects OOB data read to chip->oob_poi
762 * @page: page number to read
765 static int omap_read_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
766 uint8_t *buf, int oob_required, int page)
768 int i, eccsize = chip->ecc.size;
769 int eccbytes = chip->ecc.bytes;
770 int eccsteps = chip->ecc.steps;
772 uint8_t *ecc_calc = chip->buffers->ecccalc;
773 uint8_t *ecc_code = chip->buffers->ecccode;
774 uint32_t *eccpos = chip->ecc.layout->eccpos;
775 uint8_t *oob = &chip->oob_poi[eccpos[0]];
781 oob_pos = (eccsize * eccsteps) + eccpos[0];
783 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize,
785 chip->ecc.hwctl(mtd, NAND_ECC_READ);
787 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_pos, -1);
788 chip->read_buf(mtd, p, eccsize);
790 /* read respective ecc from oob area */
791 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1);
792 chip->read_buf(mtd, oob, eccbytes);
794 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
797 /* reread the OOB area to get the metadata */
798 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, page);
799 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
806 for (i = 0; i < chip->ecc.total; i++)
807 ecc_code[i] = chip->oob_poi[eccpos[i]];
809 eccsteps = chip->ecc.steps;
812 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
815 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
817 mtd->ecc_stats.failed++;
819 mtd->ecc_stats.corrected += stat;
823 #endif /* CONFIG_NAND_OMAP_ELM */
826 * OMAP3 BCH8 support (with BCH library)
830 * omap_correct_data_bch_sw - Decode received data and correct errors
831 * @mtd: MTD device structure
833 * @read_ecc: ecc read from nand flash
834 * @calc_ecc: ecc read from HW ECC registers
836 static int omap_correct_data_bch_sw(struct mtd_info *mtd, u_char *data,
837 u_char *read_ecc, u_char *calc_ecc)
840 /* cannot correct more than 8 errors */
841 unsigned int errloc[8];
842 struct nand_chip *chip = mtd->priv;
843 struct omap_nand_info *info = chip->priv;
845 count = decode_bch(info->control, NULL, 512, read_ecc, calc_ecc,
849 for (i = 0; i < count; i++) {
850 /* correct data only, not ecc bytes */
851 if (errloc[i] < 8*512)
852 data[errloc[i]/8] ^= 1 << (errloc[i] & 7);
853 printf("corrected bitflip %u\n", errloc[i]);
855 printf("read_ecc: ");
857 * BCH8 have 13 bytes of ECC; BCH4 needs adoption
860 for (i = 0; i < 13; i++)
861 printf("%02x ", read_ecc[i]);
863 printf("calc_ecc: ");
864 for (i = 0; i < 13; i++)
865 printf("%02x ", calc_ecc[i]);
869 } else if (count < 0) {
870 printf("ecc unrecoverable error\n");
876 * omap_free_bch - Release BCH ecc resources
877 * @mtd: MTD device structure
879 static void __maybe_unused omap_free_bch(struct mtd_info *mtd)
881 struct nand_chip *chip = mtd->priv;
882 struct omap_nand_info *info = chip->priv;
885 free_bch(info->control);
886 info->control = NULL;
889 #endif /* CONFIG_BCH */
892 * omap_select_ecc_scheme - configures driver for particular ecc-scheme
893 * @nand: NAND chip device structure
894 * @ecc_scheme: ecc scheme to configure
895 * @pagesize: number of main-area bytes per page of NAND device
896 * @oobsize: number of OOB/spare bytes per page of NAND device
898 static int omap_select_ecc_scheme(struct nand_chip *nand,
899 enum omap_ecc ecc_scheme, unsigned int pagesize, unsigned int oobsize) {
900 struct omap_nand_info *info = nand->priv;
901 struct nand_ecclayout *ecclayout = &omap_ecclayout;
902 int eccsteps = pagesize / SECTOR_BYTES;
905 switch (ecc_scheme) {
906 case OMAP_ECC_HAM1_CODE_SW:
907 debug("nand: selected OMAP_ECC_HAM1_CODE_SW\n");
908 /* For this ecc-scheme, ecc.bytes, ecc.layout, ... are
909 * initialized in nand_scan_tail(), so just set ecc.mode */
910 info->control = NULL;
911 nand->ecc.mode = NAND_ECC_SOFT;
912 nand->ecc.layout = NULL;
916 case OMAP_ECC_HAM1_CODE_HW:
917 debug("nand: selected OMAP_ECC_HAM1_CODE_HW\n");
918 /* check ecc-scheme requirements before updating ecc info */
919 if ((3 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
920 printf("nand: error: insufficient OOB: require=%d\n", (
921 (3 * eccsteps) + BADBLOCK_MARKER_LENGTH));
924 info->control = NULL;
925 /* populate ecc specific fields */
926 memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
927 nand->ecc.mode = NAND_ECC_HW;
928 nand->ecc.strength = 1;
929 nand->ecc.size = SECTOR_BYTES;
931 nand->ecc.hwctl = omap_enable_hwecc;
932 nand->ecc.correct = omap_correct_data;
933 nand->ecc.calculate = omap_calculate_ecc;
934 /* define ecc-layout */
935 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
936 for (i = 0; i < ecclayout->eccbytes; i++) {
937 if (nand->options & NAND_BUSWIDTH_16)
938 ecclayout->eccpos[i] = i + 2;
940 ecclayout->eccpos[i] = i + 1;
942 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
943 ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
944 BADBLOCK_MARKER_LENGTH;
947 case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
949 debug("nand: selected OMAP_ECC_BCH8_CODE_HW_DETECTION_SW\n");
950 /* check ecc-scheme requirements before updating ecc info */
951 if ((13 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
952 printf("nand: error: insufficient OOB: require=%d\n", (
953 (13 * eccsteps) + BADBLOCK_MARKER_LENGTH));
956 /* check if BCH S/W library can be used for error detection */
957 info->control = init_bch(13, 8, 0x201b);
958 if (!info->control) {
959 printf("nand: error: could not init_bch()\n");
962 /* populate ecc specific fields */
963 memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
964 nand->ecc.mode = NAND_ECC_HW;
965 nand->ecc.strength = 8;
966 nand->ecc.size = SECTOR_BYTES;
967 nand->ecc.bytes = 13;
968 nand->ecc.hwctl = omap_enable_hwecc;
969 nand->ecc.correct = omap_correct_data_bch_sw;
970 nand->ecc.calculate = omap_calculate_ecc;
971 /* define ecc-layout */
972 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
973 ecclayout->eccpos[0] = BADBLOCK_MARKER_LENGTH;
974 for (i = 1; i < ecclayout->eccbytes; i++) {
975 if (i % nand->ecc.bytes)
976 ecclayout->eccpos[i] =
977 ecclayout->eccpos[i - 1] + 1;
979 ecclayout->eccpos[i] =
980 ecclayout->eccpos[i - 1] + 2;
982 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
983 ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
984 BADBLOCK_MARKER_LENGTH;
987 printf("nand: error: CONFIG_BCH required for ECC\n");
991 case OMAP_ECC_BCH8_CODE_HW:
992 #ifdef CONFIG_NAND_OMAP_ELM
993 debug("nand: selected OMAP_ECC_BCH8_CODE_HW\n");
994 /* check ecc-scheme requirements before updating ecc info */
995 if ((14 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
996 printf("nand: error: insufficient OOB: require=%d\n", (
997 (14 * eccsteps) + BADBLOCK_MARKER_LENGTH));
1000 /* intialize ELM for ECC error detection */
1002 info->control = NULL;
1003 /* populate ecc specific fields */
1004 memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
1005 nand->ecc.mode = NAND_ECC_HW;
1006 nand->ecc.strength = 8;
1007 nand->ecc.size = SECTOR_BYTES;
1008 nand->ecc.bytes = 14;
1009 nand->ecc.hwctl = omap_enable_hwecc;
1010 nand->ecc.correct = omap_correct_data_bch;
1011 nand->ecc.calculate = omap_calculate_ecc;
1012 nand->ecc.read_page = omap_read_page_bch;
1013 /* define ecc-layout */
1014 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
1015 for (i = 0; i < ecclayout->eccbytes; i++)
1016 ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH;
1017 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
1018 ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
1019 BADBLOCK_MARKER_LENGTH;
1022 printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
1026 case OMAP_ECC_BCH16_CODE_HW:
1027 #ifdef CONFIG_NAND_OMAP_ELM
1028 debug("nand: using OMAP_ECC_BCH16_CODE_HW\n");
1029 /* check ecc-scheme requirements before updating ecc info */
1030 if ((26 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
1031 printf("nand: error: insufficient OOB: require=%d\n", (
1032 (26 * eccsteps) + BADBLOCK_MARKER_LENGTH));
1035 /* intialize ELM for ECC error detection */
1037 /* populate ecc specific fields */
1038 nand->ecc.mode = NAND_ECC_HW;
1039 nand->ecc.size = SECTOR_BYTES;
1040 nand->ecc.bytes = 26;
1041 nand->ecc.strength = 16;
1042 nand->ecc.hwctl = omap_enable_hwecc;
1043 nand->ecc.correct = omap_correct_data_bch;
1044 nand->ecc.calculate = omap_calculate_ecc;
1045 nand->ecc.read_page = omap_read_page_bch;
1046 /* define ecc-layout */
1047 ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
1048 for (i = 0; i < ecclayout->eccbytes; i++)
1049 ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH;
1050 ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
1051 ecclayout->oobfree[0].length = oobsize - nand->ecc.bytes -
1052 BADBLOCK_MARKER_LENGTH;
1055 printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
1059 debug("nand: error: ecc scheme not enabled or supported\n");
1063 /* nand_scan_tail() sets ham1 sw ecc; hw ecc layout is set by driver */
1064 if (ecc_scheme != OMAP_ECC_HAM1_CODE_SW)
1065 nand->ecc.layout = ecclayout;
1067 info->ecc_scheme = ecc_scheme;
1071 #ifndef CONFIG_SPL_BUILD
1073 * omap_nand_switch_ecc - switch the ECC operation between different engines
1074 * (h/w and s/w) and different algorithms (hamming and BCHx)
1076 * @hardware - true if one of the HW engines should be used
1077 * @eccstrength - the number of bits that could be corrected
1078 * (1 - hamming, 4 - BCH4, 8 - BCH8, 16 - BCH16)
1080 int __maybe_unused omap_nand_switch_ecc(uint32_t hardware, uint32_t eccstrength)
1082 struct nand_chip *nand;
1083 struct mtd_info *mtd;
1086 if (nand_curr_device < 0 ||
1087 nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
1088 !nand_info[nand_curr_device].name) {
1089 printf("nand: error: no NAND devices found\n");
1093 mtd = &nand_info[nand_curr_device];
1095 nand->options |= NAND_OWN_BUFFERS;
1096 nand->options &= ~NAND_SUBPAGE_READ;
1097 /* Setup the ecc configurations again */
1099 if (eccstrength == 1) {
1100 err = omap_select_ecc_scheme(nand,
1101 OMAP_ECC_HAM1_CODE_HW,
1102 mtd->writesize, mtd->oobsize);
1103 } else if (eccstrength == 8) {
1104 err = omap_select_ecc_scheme(nand,
1105 OMAP_ECC_BCH8_CODE_HW,
1106 mtd->writesize, mtd->oobsize);
1108 printf("nand: error: unsupported ECC scheme\n");
1112 if (eccstrength == 1) {
1113 err = omap_select_ecc_scheme(nand,
1114 OMAP_ECC_HAM1_CODE_SW,
1115 mtd->writesize, mtd->oobsize);
1116 } else if (eccstrength == 8) {
1117 err = omap_select_ecc_scheme(nand,
1118 OMAP_ECC_BCH8_CODE_HW_DETECTION_SW,
1119 mtd->writesize, mtd->oobsize);
1121 printf("nand: error: unsupported ECC scheme\n");
1126 /* Update NAND handling after ECC mode switch */
1128 err = nand_scan_tail(mtd);
1131 #endif /* CONFIG_SPL_BUILD */
1134 * Board-specific NAND initialization. The following members of the
1135 * argument are board-specific:
1136 * - IO_ADDR_R: address to read the 8 I/O lines of the flash device
1137 * - IO_ADDR_W: address to write the 8 I/O lines of the flash device
1138 * - cmd_ctrl: hardwarespecific function for accesing control-lines
1139 * - waitfunc: hardwarespecific function for accesing device ready/busy line
1140 * - ecc.hwctl: function to enable (reset) hardware ecc generator
1141 * - ecc.mode: mode of ecc, see defines
1142 * - chip_delay: chip dependent delay for transfering data from array to
1144 * - options: various chip options. They can partly be set to inform
1145 * nand_scan about special functionality. See the defines for further
1148 int board_nand_init(struct nand_chip *nand)
1150 int32_t gpmc_config = 0;
1154 * xloader/Uboot's gpmc configuration would have configured GPMC for
1155 * nand type of memory. The following logic scans and latches on to the
1156 * first CS with NAND type memory.
1157 * TBD: need to make this logic generic to handle multiple CS NAND
1160 while (cs < GPMC_MAX_CS) {
1161 /* Check if NAND type is set */
1162 if ((readl(&gpmc_cfg->cs[cs].config1) & 0xC00) == 0x800) {
1168 if (cs >= GPMC_MAX_CS) {
1169 printf("nand: error: Unable to find NAND settings in "
1170 "GPMC Configuration - quitting\n");
1174 gpmc_config = readl(&gpmc_cfg->config);
1175 /* Disable Write protect */
1176 gpmc_config |= 0x10;
1177 writel(gpmc_config, &gpmc_cfg->config);
1179 nand->IO_ADDR_R = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
1180 nand->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
1181 omap_nand_info[cs].control = NULL;
1182 omap_nand_info[cs].cs = cs;
1183 omap_nand_info[cs].ws = wscfg[cs];
1184 nand->priv = &omap_nand_info[cs];
1185 nand->cmd_ctrl = omap_nand_hwcontrol;
1186 nand->options |= NAND_NO_PADDING | NAND_CACHEPRG;
1187 nand->chip_delay = 100;
1188 nand->ecc.layout = &omap_ecclayout;
1190 /* configure driver and controller based on NAND device bus-width */
1191 gpmc_config = readl(&gpmc_cfg->cs[cs].config1);
1192 #if defined(CONFIG_SYS_NAND_BUSWIDTH_16BIT)
1193 nand->options |= NAND_BUSWIDTH_16;
1194 writel(gpmc_config | (0x1 << 12), &gpmc_cfg->cs[cs].config1);
1196 nand->options &= ~NAND_BUSWIDTH_16;
1197 writel(gpmc_config & ~(0x1 << 12), &gpmc_cfg->cs[cs].config1);
1199 /* select ECC scheme */
1200 #if defined(CONFIG_NAND_OMAP_ECCSCHEME)
1201 err = omap_select_ecc_scheme(nand, CONFIG_NAND_OMAP_ECCSCHEME,
1202 CONFIG_SYS_NAND_PAGE_SIZE, CONFIG_SYS_NAND_OOBSIZE);
1204 /* pagesize and oobsize are not required to configure sw ecc-scheme */
1205 err = omap_select_ecc_scheme(nand, OMAP_ECC_HAM1_CODE_SW,
1210 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1211 if (nand->ecc.layout) {
1212 bbt_main_descr.offs = nand->ecc.layout->oobfree[0].offset;
1213 bbt_main_descr.veroffs = bbt_main_descr.offs +
1214 sizeof(bbt_pattern);
1216 bbt_mirror_descr.offs = nand->ecc.layout->oobfree[0].offset;
1217 bbt_mirror_descr.veroffs = bbt_mirror_descr.offs +
1218 sizeof(mirror_pattern);
1221 nand->bbt_options |= NAND_BBT_USE_FLASH;
1222 nand->bbt_td = &bbt_main_descr;
1223 nand->bbt_md = &bbt_mirror_descr;
1226 #ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
1227 nand->read_buf = omap_nand_read_prefetch;
1229 if (nand->options & NAND_BUSWIDTH_16)
1230 nand->read_buf = nand_read_buf16;
1232 nand->read_buf = nand_read_buf;
1235 nand->dev_ready = omap_dev_ready;