]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mtd/nand/omap_gpmc.c
Merge branch 'tx51-bugfix' into karo-tx-merge
[karo-tx-uboot.git] / drivers / mtd / nand / omap_gpmc.c
1 /*
2  * (C) Copyright 2004-2008 Texas Instruments, <www.ti.com>
3  * Rohit Choraria <rohitkc@ti.com>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9 #include <asm/io.h>
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>
17 #include <nand.h>
18 #ifdef CONFIG_AM33XX
19 #include <asm/arch/elm.h>
20 #endif
21
22 static uint8_t cs;
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;
27
28 static struct gpmc *gpmc_cfg = (struct gpmc *)GPMC_BASE;
29
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' };
33
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 */
38         .len = 4,
39         .veroffs = 4, /* may be overwritten depending on ECC layout */
40         .maxblocks = 4,
41         .pattern = bbt_pattern,
42 };
43
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 */
48         .len = 4,
49         .veroffs = 4, /* may be overwritten depending on ECC layout */
50         .maxblocks = 4,
51         .pattern = mirror_pattern,
52 };
53 #endif
54
55 /*
56  * omap_nand_hwcontrol - Set the address pointers corretly for the
57  *                      following address/data/command operation
58  */
59 static void omap_nand_hwcontrol(struct mtd_info *mtd, int32_t cmd,
60                                 uint32_t ctrl)
61 {
62         register struct nand_chip *this = mtd->priv;
63
64         /*
65          * Point the IO_ADDR to DATA and ADDRESS registers instead
66          * of chip address
67          */
68         switch (ctrl) {
69         case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
70                 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
71                 break;
72         case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
73                 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_adr;
74                 break;
75         case NAND_CTRL_CHANGE | NAND_NCE:
76                 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
77                 break;
78         }
79
80         if (cmd != NAND_CMD_NONE)
81                 writeb(cmd, this->IO_ADDR_W);
82 }
83
84 #ifdef CONFIG_SPL_BUILD
85 /* Check wait pin as dev ready indicator */
86 int omap_spl_dev_ready(struct mtd_info *mtd)
87 {
88         return gpmc_cfg->status & (1 << 8);
89 }
90 #endif
91
92 /*
93  * omap_hwecc_init - Initialize the Hardware ECC for NAND flash in
94  *                   GPMC controller
95  * @mtd:        MTD device structure
96  *
97  */
98 static void __maybe_unused omap_hwecc_init(struct nand_chip *chip)
99 {
100         /*
101          * Init ECC Control Register
102          * Clear all ECC | Enable Reg1
103          */
104         writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
105         writel(ECCSIZE1 | ECCSIZE0 | ECCSIZE0SEL, &gpmc_cfg->ecc_size_config);
106 }
107
108 /*
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
111  *
112  * @ecc_buf:    buffer to store ecc code
113  *
114  * @return:     re-formatted ECC value
115  */
116 static uint32_t gen_true_ecc(uint8_t *ecc_buf)
117 {
118         return ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xF0) << 20) |
119                 ((ecc_buf[2] & 0x0F) << 8);
120 }
121
122 /*
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
128  *
129  * @mtd:                 MTD device structure
130  * @dat:                 page data
131  * @read_ecc:            ecc read from nand flash
132  * @calc_ecc:            ecc read from ECC registers
133  *
134  * @return 0 if data is OK or corrected, else returns -1
135  */
136 static int __maybe_unused omap_correct_data(struct mtd_info *mtd, uint8_t *dat,
137                                 uint8_t *read_ecc, uint8_t *calc_ecc)
138 {
139         uint32_t orig_ecc, new_ecc, res, hm;
140         uint16_t parity_bits, byte;
141         uint8_t bit;
142
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;
148         if (res) {
149                 /* Get the hamming width */
150                 hm = hweight32(res);
151                 /* Single bit errors can be corrected! */
152                 if (hm == 12) {
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 */
162                         return 2;
163                 } else {
164                         /*
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!
176                          */
177                         if ((orig_ecc == 0x0FFF0FFF) && (new_ecc == 0x00000000))
178                                 return 0;
179                         printf("Error: Bad compare! failed\n");
180                         /* detected 2 bit error */
181                         return -1;
182                 }
183         }
184         return 0;
185 }
186
187 /*
188  *  omap_calculate_ecc - Generate non-inverted ECC bytes.
189  *
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
199  *  @dat:       unused
200  *  @ecc_code:  ecc_code buffer
201  */
202 static int __maybe_unused omap_calculate_ecc(struct mtd_info *mtd,
203                 const uint8_t *dat, uint8_t *ecc_code)
204 {
205         u_int32_t val;
206
207         /* Start Reading from HW ECC1_Result = 0x200 */
208         val = readl(&gpmc_cfg->ecc1_result);
209
210         ecc_code[0] = val & 0xFF;
211         ecc_code[1] = (val >> 16) & 0xFF;
212         ecc_code[2] = ((val >> 8) & 0x0F) | ((val >> 20) & 0xF0);
213
214         /*
215          * Stop reading anymore ECC vals and clear old results
216          * enable will be called if more reads are required
217          */
218         writel(0x000, &gpmc_cfg->ecc_config);
219
220         return 0;
221 }
222
223 /*
224  * omap_enable_ecc - This function enables the hardware ecc functionality
225  * @mtd:        MTD device structure
226  * @mode:       Read/Write mode
227  */
228 static void __maybe_unused omap_enable_hwecc(struct mtd_info *mtd, int32_t mode)
229 {
230         struct nand_chip *chip = mtd->priv;
231         uint32_t val, dev_width = (chip->options & NAND_BUSWIDTH_16) >> 1;
232
233         switch (mode) {
234         case NAND_ECC_READ:
235         case NAND_ECC_WRITE:
236                 /* Clear the ecc result registers, select ecc reg as 1 */
237                 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
238
239                 /*
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
243                  */
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);
248                 break;
249         default:
250                 printf("Error: Unrecognized Mode[%d]!\n", mode);
251                 break;
252         }
253 }
254
255 /*
256  * Generic BCH interface
257  */
258 struct nand_bch_priv {
259         uint8_t mode;
260         uint8_t type;
261         uint8_t nibbles;
262         struct bch_control *control;
263 };
264
265 /* bch types */
266 #define ECC_BCH4        0
267 #define ECC_BCH8        1
268 #define ECC_BCH16       2
269
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 */
273
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
279
280 /*
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
283  * library).
284  * When some users with other BCH strength will exists this have to change!
285  */
286 static __maybe_unused struct nand_bch_priv bch_priv = {
287         .mode = NAND_ECC_HW_BCH,
288         .type = ECC_BCH8,
289         .nibbles = ECC_BCH8_NIBBLES,
290         .control = NULL
291 };
292
293 /*
294  * omap_hwecc_init_bch - Initialize the BCH Hardware ECC for NAND flash in
295  *                              GPMC controller
296  * @mtd:        MTD device structure
297  * @mode:       Read/Write mode
298  */
299 __maybe_unused
300 static void omap_hwecc_init_bch(struct nand_chip *chip, int32_t mode)
301 {
302         uint32_t val;
303         uint32_t dev_width = (chip->options & NAND_BUSWIDTH_16) >> 1;
304 #ifdef CONFIG_AM33XX
305         uint32_t unused_length = 0;
306 #endif
307         uint32_t wr_mode = BCH_WRAPMODE_6;
308         struct nand_bch_priv *bch = chip->priv;
309
310         /* Clear the ecc result registers, select ecc reg as 1 */
311         writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
312
313 #ifdef CONFIG_AM33XX
314         wr_mode = BCH_WRAPMODE_1;
315
316         switch (bch->nibbles) {
317         case ECC_BCH4_NIBBLES:
318                 unused_length = 3;
319                 break;
320         case ECC_BCH8_NIBBLES:
321                 unused_length = 2;
322                 break;
323         case ECC_BCH16_NIBBLES:
324                 unused_length = 0;
325                 break;
326         }
327
328         /*
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.
332          */
333         switch (mode) {
334         case NAND_ECC_WRITE:
335                 /* write access only setup eccsize1 config */
336                 val = ((unused_length + bch->nibbles) << 22);
337                 break;
338
339         case NAND_ECC_READ:
340         default:
341                 /*
342                  * by default eccsize0 selected for ecc1resultsize
343                  * eccsize0 config.
344                  */
345                 val  = (bch->nibbles << 12);
346                 /* eccsize1 config */
347                 val |= (unused_length << 22);
348                 break;
349         }
350 #else
351         /*
352          * This ecc_size_config setting is for BCH sw library.
353          *
354          * Note: we only support BCH8 currently with BCH sw library!
355          * Should be really easy to adobt to BCH4, however some omap3 have
356          * flaws with BCH4.
357          *
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)
361          */
362         val = (32 << 22) | (0 << 12);
363 #endif
364         /* ecc size configuration */
365         writel(val, &gpmc_cfg->ecc_size_config);
366
367         /*
368          * Configure the ecc engine in gpmc
369          * We assume 512 Byte sector pages for access to NAND.
370          */
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);
378 }
379
380 /*
381  * omap_enable_ecc_bch - This function enables the bch h/w ecc functionality
382  * @mtd:        MTD device structure
383  * @mode:       Read/Write mode
384  */
385 __maybe_unused
386 static void omap_enable_ecc_bch(struct mtd_info *mtd, int32_t mode)
387 {
388         struct nand_chip *chip = mtd->priv;
389
390         omap_hwecc_init_bch(chip, mode);
391         /* enable ecc */
392         writel((readl(&gpmc_cfg->ecc_config) | 0x1), &gpmc_cfg->ecc_config);
393 }
394
395 /*
396  * omap_ecc_disable - Disable H/W ECC calculation
397  *
398  * @mtd:        MTD device structure
399  */
400 static void __maybe_unused omap_ecc_disable(struct mtd_info *mtd)
401 {
402         writel((readl(&gpmc_cfg->ecc_config) & ~0x1), &gpmc_cfg->ecc_config);
403 }
404
405 /*
406  * BCH8 support (needs ELM and thus AM33xx-only)
407  */
408 #ifdef CONFIG_AM33XX
409 /*
410  * omap_read_bch8_result - Read BCH result for BCH8 level
411  *
412  * @mtd:        MTD device structure
413  * @big_endian: When set read register 3 first
414  * @ecc_code:   Read syndrome from BCH result registers
415  */
416 static void omap_read_bch8_result(struct mtd_info *mtd, uint8_t big_endian,
417                                 uint8_t *ecc_code)
418 {
419         uint32_t *ptr;
420         int8_t i = 0, j;
421
422         if (big_endian) {
423                 ptr = &gpmc_cfg->bch_result_0_3[0].bch_result_x[3];
424                 ecc_code[i++] = readl(ptr) & 0xFF;
425                 ptr--;
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;
431                         ptr--;
432                 }
433         } else {
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;
440                         ptr++;
441                 }
442                 ecc_code[i++] = readl(ptr) & 0xFF;
443                 ecc_code[i++] = 0;      /* 14th byte is always zero */
444         }
445 }
446
447 /*
448  * omap_rotate_ecc_bch - Rotate the syndrome bytes
449  *
450  * @mtd:        MTD device structure
451  * @calc_ecc:   ECC read from ECC registers
452  * @syndrome:   Rotated syndrome will be retuned in this array
453  *
454  */
455 static void omap_rotate_ecc_bch(struct mtd_info *mtd, uint8_t *calc_ecc,
456                 uint8_t *syndrome)
457 {
458         struct nand_chip *chip = mtd->priv;
459         struct nand_bch_priv *bch = chip->priv;
460         uint8_t n_bytes = 0;
461         int8_t i, j;
462
463         switch (bch->type) {
464         case ECC_BCH4:
465                 n_bytes = 8;
466                 break;
467
468         case ECC_BCH16:
469                 n_bytes = 28;
470                 break;
471
472         case ECC_BCH8:
473         default:
474                 n_bytes = 13;
475                 break;
476         }
477
478         for (i = 0, j = (n_bytes-1); i < n_bytes; i++, j--)
479                 syndrome[i] =  calc_ecc[j];
480 }
481
482 /*
483  *  omap_calculate_ecc_bch - Read BCH ECC result
484  *
485  *  @mtd:       MTD structure
486  *  @dat:       unused
487  *  @ecc_code:  ecc_code buffer
488  */
489 static int omap_calculate_ecc_bch(struct mtd_info *mtd, const uint8_t *dat,
490                                 uint8_t *ecc_code)
491 {
492         struct nand_chip *chip = mtd->priv;
493         struct nand_bch_priv *bch = chip->priv;
494         uint8_t big_endian = 1;
495         int8_t ret = 0;
496
497         if (bch->type == ECC_BCH8)
498                 omap_read_bch8_result(mtd, big_endian, ecc_code);
499         else /* BCH4 and BCH16 currently not supported */
500                 ret = -1;
501
502         /*
503          * Stop reading anymore ECC vals and clear old results
504          * enable will be called if more reads are required
505          */
506         omap_ecc_disable(mtd);
507
508         return ret;
509 }
510
511 /*
512  * omap_fix_errors_bch - Correct bch error in the data
513  *
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
518  *
519  */
520 static void omap_fix_errors_bch(struct mtd_info *mtd, uint8_t *data,
521                 uint32_t error_count, uint32_t *error_loc)
522 {
523         struct nand_chip *chip = mtd->priv;
524         struct nand_bch_priv *bch = chip->priv;
525         uint8_t count = 0;
526         uint32_t error_byte_pos;
527         uint32_t error_bit_mask;
528         uint32_t last_bit = (bch->nibbles * 4) - 1;
529
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;
539                         /* Error Bit mask */
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;
543                 }
544         }
545 }
546
547 /*
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
550  *
551  * @mtd:        MTD device structure
552  * @dat:        page data
553  * @read_ecc:   ecc read from nand flash (ignored)
554  * @calc_ecc:   ecc read from ECC registers
555  *
556  * @return 0 if data is OK or corrected, else returns -1
557  */
558 static int omap_correct_data_bch(struct mtd_info *mtd, uint8_t *dat,
559                                 uint8_t *read_ecc, uint8_t *calc_ecc)
560 {
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;
567
568         ecc_flag = 0;
569         for (i = 0; i < chip->ecc.bytes; i++)
570                 if (read_ecc[i] != 0xff)
571                         ecc_flag = 1;
572
573         if (!ecc_flag)
574                 return 0;
575
576         elm_reset();
577         elm_config((enum bch_level)(bch->type));
578
579         /*
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.
582          */
583         omap_rotate_ecc_bch(mtd, calc_ecc, syndrome);
584
585         /* use elm module to check for errors */
586         if (elm_check_error(syndrome, bch->nibbles, &error_count,
587                                 error_loc) != 0) {
588                 printf("ECC: uncorrectable.\n");
589                 return -1;
590         }
591
592         /* correct bch error */
593         if (error_count > 0)
594                 omap_fix_errors_bch(mtd, dat, error_count, error_loc);
595
596         return 0;
597 }
598
599 /**
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
606  *
607  */
608 static int omap_read_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
609                                 uint8_t *buf, int oob_required, int page)
610 {
611         int i, eccsize = chip->ecc.size;
612         int eccbytes = chip->ecc.bytes;
613         int eccsteps = chip->ecc.steps;
614         uint8_t *p = buf;
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;
619         uint32_t data_pos;
620         uint32_t oob_pos;
621
622         data_pos = 0;
623         /* oob area start */
624         oob_pos = (eccsize * eccsteps) + chip->ecc.layout->eccpos[0];
625         oob += chip->ecc.layout->eccpos[0];
626
627         for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize,
628                                 oob += eccbytes) {
629                 chip->ecc.hwctl(mtd, NAND_ECC_READ);
630                 /* read data */
631                 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_pos, page);
632                 chip->read_buf(mtd, p, eccsize);
633
634                 /* read respective ecc from oob area */
635                 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, page);
636                 chip->read_buf(mtd, oob, eccbytes);
637                 /* read syndrome */
638                 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
639
640                 data_pos += eccsize;
641                 oob_pos += eccbytes;
642         }
643
644         for (i = 0; i < chip->ecc.total; i++)
645                 ecc_code[i] = chip->oob_poi[eccpos[i]];
646
647         eccsteps = chip->ecc.steps;
648         p = buf;
649
650         for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
651                 int stat;
652
653                 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
654                 if (stat < 0)
655                         mtd->ecc_stats.failed++;
656                 else
657                         mtd->ecc_stats.corrected += stat;
658         }
659         return 0;
660 }
661 #endif /* CONFIG_AM33XX */
662
663 /*
664  * OMAP3 BCH8 support (with BCH library)
665  */
666 #ifdef CONFIG_NAND_OMAP_BCH8
667 /*
668  *  omap_calculate_ecc_bch - Read BCH ECC result
669  *
670  *  @mtd:       MTD device structure
671  *  @dat:       The pointer to data on which ecc is computed (unused here)
672  *  @ecc:       The ECC output buffer
673  */
674 static int omap_calculate_ecc_bch(struct mtd_info *mtd, const uint8_t *dat,
675                                 uint8_t *ecc)
676 {
677         int ret = 0;
678         size_t i;
679         unsigned long nsectors, val1, val2, val3, val4;
680
681         nsectors = ((readl(&gpmc_cfg->ecc_config) >> 4) & 0x7) + 1;
682
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]);
689
690                 /*
691                  * Add constant polynomial to remainder, in order to get an ecc
692                  * sequence of 0xFFs for a buffer filled with 0xFFs.
693                  */
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);
707         }
708
709         /*
710          * Stop reading anymore ECC vals and clear old results
711          * enable will be called if more reads are required
712          */
713         omap_ecc_disable(mtd);
714
715         return ret;
716 }
717
718 /**
719  * omap_correct_data_bch - Decode received data and correct errors
720  * @mtd: MTD device structure
721  * @data: page data
722  * @read_ecc: ecc read from nand flash
723  * @calc_ecc: ecc read from HW ECC registers
724  */
725 static int omap_correct_data_bch(struct mtd_info *mtd, u_char *data,
726                                  u_char *read_ecc, u_char *calc_ecc)
727 {
728         int i, count;
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;
734
735         count = decode_bch(bch, NULL, 512, read_ecc, calc_ecc, NULL, errloc);
736         if (count > 0) {
737                 /* correct errors */
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]);
743 #ifdef DEBUG
744                         puts("read_ecc: ");
745                         /*
746                          * BCH8 have 13 bytes of ECC; BCH4 needs adoption
747                          * here!
748                          */
749                         for (i = 0; i < 13; i++)
750                                 printf("%02x ", read_ecc[i]);
751                         puts("\n");
752                         puts("calc_ecc: ");
753                         for (i = 0; i < 13; i++)
754                                 printf("%02x ", calc_ecc[i]);
755                         puts("\n");
756 #endif
757                 }
758         } else if (count < 0) {
759                 puts("ecc unrecoverable error\n");
760         }
761         return count;
762 }
763
764 /**
765  * omap_free_bch - Release BCH ecc resources
766  * @mtd: MTD device structure
767  */
768 static void __maybe_unused omap_free_bch(struct mtd_info *mtd)
769 {
770         struct nand_chip *chip = mtd->priv;
771         struct nand_bch_priv *chip_priv = chip->priv;
772         struct bch_control *bch = NULL;
773
774         if (chip_priv)
775                 bch = chip_priv->control;
776
777         if (bch) {
778                 free_bch(bch);
779                 chip_priv->control = NULL;
780         }
781 }
782 #endif /* CONFIG_NAND_OMAP_BCH8 */
783
784 #ifndef CONFIG_SPL_BUILD
785 /*
786  * omap_nand_switch_ecc - switch the ECC operation between different engines
787  * (h/w and s/w) and different algorithms (hamming and BCHx)
788  *
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)
792  */
793 void omap_nand_switch_ecc(uint32_t hardware, uint32_t eccstrength)
794 {
795         struct nand_chip *nand;
796         struct mtd_info *mtd;
797
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");
802                 return;
803         }
804
805         mtd = &nand_info[nand_curr_device];
806         nand = mtd->priv;
807
808         nand->options |= NAND_OWN_BUFFERS;
809
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;
820
821         /* Setup the ecc configurations again */
822         if (hardware) {
823                 if (eccstrength == 1) {
824                         nand->ecc.mode = NAND_ECC_HW;
825                         nand->ecc.layout = &hw_nand_oob;
826                         nand->ecc.size = 512;
827                         nand->ecc.bytes = 3;
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");
833                 }
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;
839 #ifdef CONFIG_AM33XX
840                         nand->ecc.bytes = 14;
841                         nand->ecc.read_page = omap_read_page_bch;
842 #else
843                         nand->ecc.bytes = 13;
844 #endif
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");
850                 }
851 #endif
852         } else {
853                 nand->ecc.mode = NAND_ECC_SOFT;
854                 /* Use mtd default settings */
855                 nand->ecc.layout = NULL;
856                 nand->ecc.size = 0;
857                 printf("SW ECC selected\n");
858         }
859
860         /* Update NAND handling after ECC mode switch */
861         nand_scan_tail(mtd);
862
863         nand->options &= ~NAND_OWN_BUFFERS;
864 }
865 #endif /* CONFIG_SPL_BUILD */
866
867 /*
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
877  *   read regs (tR)
878  * - options: various chip options. They can partly be set to inform
879  *   nand_scan about special functionality. See the defines for further
880  *   explanation
881  */
882 int board_nand_init(struct nand_chip *nand)
883 {
884         int32_t gpmc_config = 0;
885         cs = 0;
886
887         /*
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
892          * devices.
893          */
894         while (cs < GPMC_MAX_CS) {
895                 /* Check if NAND type is set */
896                 if ((readl(&gpmc_cfg->cs[cs].config1) & 0xC00) == 0x800) {
897                         /* Found it!! */
898                         break;
899                 }
900                 cs++;
901         }
902         if (cs >= GPMC_MAX_CS) {
903                 printf("NAND: Unable to find NAND settings in "
904                         "GPMC Configuration - quitting\n");
905                 return -ENODEV;
906         }
907
908         gpmc_config = readl(&gpmc_cfg->config);
909         /* Disable Write protect */
910         gpmc_config |= 0x10;
911         writel(gpmc_config, &gpmc_cfg->config);
912
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;
915
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;
921
922         nand->chip_delay = 100;
923
924 #if defined(CONFIG_AM33XX) || defined(CONFIG_NAND_OMAP_BCH8)
925 #ifdef CONFIG_AM33XX
926         /* AM33xx uses the ELM */
927         /* required in case of BCH */
928         elm_init();
929 #else
930         /*
931          * Whereas other OMAP based SoC do not have the ELM, they use the BCH
932          * SW library.
933          */
934         bch_priv.control = init_bch(13, 8, 0x201b /* hw polynominal */);
935         if (!bch_priv.control) {
936                 puts("Could not init_bch()\n");
937                 return -ENODEV;
938         }
939 #endif
940         /* BCH info that will be correct for SPL or overridden otherwise. */
941         nand->priv = &bch_priv;
942 #endif
943
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;
954 #ifdef CONFIG_AM33XX
955         nand->ecc.read_page = omap_read_page_bch;
956 #endif
957         omap_hwecc_init_bch(nand, NAND_ECC_READ);
958 #else
959 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_NAND_SOFTECC)
960         nand->ecc.mode = NAND_ECC_SOFT;
961 #else
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);
971 #endif
972 #endif
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 +
977                         sizeof(bbt_pattern);
978
979                 bbt_mirror_descr.offs = nand->ecc.layout->oobfree[0].offset;
980                 bbt_mirror_descr.veroffs = bbt_mirror_descr.offs +
981                         sizeof(mirror_pattern);
982         }
983
984         nand->bbt_options |= NAND_BBT_USE_FLASH;
985         nand->bbt_td = &bbt_main_descr;
986         nand->bbt_md = &bbt_mirror_descr;
987 #endif
988
989 #ifdef CONFIG_SPL_BUILD
990         if (nand->options & NAND_BUSWIDTH_16)
991                 nand->read_buf = nand_read_buf16;
992         else
993                 nand->read_buf = nand_read_buf;
994         nand->dev_ready = omap_spl_dev_ready;
995 #endif
996
997         return 0;
998 }