1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <common.h>
25#include <asm/io.h>
26#include <asm/errno.h>
27#include <asm/arch/mem.h>
28#include <asm/arch/cpu.h>
29#include <asm/omap_gpmc.h>
30#include <linux/mtd/nand_ecc.h>
31#include <linux/bch.h>
32#include <linux/compiler.h>
33#include <nand.h>
34#ifdef CONFIG_AM33XX
35#include <asm/arch/elm.h>
36#endif
37
38static uint8_t cs;
39static __maybe_unused struct nand_ecclayout hw_nand_oob =
40 GPMC_NAND_HW_ECC_LAYOUT;
41static __maybe_unused struct nand_ecclayout hw_bch8_nand_oob =
42 GPMC_NAND_HW_BCH8_ECC_LAYOUT;
43
44
45
46
47
48static void omap_nand_hwcontrol(struct mtd_info *mtd, int32_t cmd,
49 uint32_t ctrl)
50{
51 register struct nand_chip *this = mtd->priv;
52
53
54
55
56
57 switch (ctrl) {
58 case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
59 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
60 break;
61 case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
62 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_adr;
63 break;
64 case NAND_CTRL_CHANGE | NAND_NCE:
65 this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
66 break;
67 }
68
69 if (cmd != NAND_CMD_NONE)
70 writeb(cmd, this->IO_ADDR_W);
71}
72
73#ifdef CONFIG_SPL_BUILD
74
75int omap_spl_dev_ready(struct mtd_info *mtd)
76{
77 return gpmc_cfg->status & (1 << 8);
78}
79#endif
80
81
82
83
84
85
86
87static void __maybe_unused omap_hwecc_init(struct nand_chip *chip)
88{
89
90
91
92
93 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
94 writel(ECCSIZE1 | ECCSIZE0 | ECCSIZE0SEL, &gpmc_cfg->ecc_size_config);
95}
96
97
98
99
100
101
102
103
104
105static uint32_t gen_true_ecc(uint8_t *ecc_buf)
106{
107 return ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xF0) << 20) |
108 ((ecc_buf[2] & 0x0F) << 8);
109}
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125static int __maybe_unused omap_correct_data(struct mtd_info *mtd, uint8_t *dat,
126 uint8_t *read_ecc, uint8_t *calc_ecc)
127{
128 uint32_t orig_ecc, new_ecc, res, hm;
129 uint16_t parity_bits, byte;
130 uint8_t bit;
131
132
133 orig_ecc = gen_true_ecc(read_ecc);
134 new_ecc = gen_true_ecc(calc_ecc);
135
136 res = orig_ecc ^ new_ecc;
137 if (res) {
138
139 hm = hweight32(res);
140
141 if (hm == 12) {
142
143 parity_bits = res >> 16;
144 bit = (parity_bits & 0x7);
145 byte = (parity_bits >> 3) & 0x1FF;
146
147 dat[byte] ^= (0x1 << bit);
148 } else if (hm == 1) {
149 printf("Error: Ecc is wrong\n");
150
151 return 2;
152 } else {
153
154
155
156
157
158
159
160
161
162
163
164
165
166 if ((orig_ecc == 0x0FFF0FFF) && (new_ecc == 0x00000000))
167 return 0;
168 printf("Error: Bad compare! failed\n");
169
170 return -1;
171 }
172 }
173 return 0;
174}
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191static int __maybe_unused omap_calculate_ecc(struct mtd_info *mtd,
192 const uint8_t *dat, uint8_t *ecc_code)
193{
194 u_int32_t val;
195
196
197 val = readl(&gpmc_cfg->ecc1_result);
198
199 ecc_code[0] = val & 0xFF;
200 ecc_code[1] = (val >> 16) & 0xFF;
201 ecc_code[2] = ((val >> 8) & 0x0F) | ((val >> 20) & 0xF0);
202
203
204
205
206
207 writel(0x000, &gpmc_cfg->ecc_config);
208
209 return 0;
210}
211
212
213
214
215
216
217static void __maybe_unused omap_enable_hwecc(struct mtd_info *mtd, int32_t mode)
218{
219 struct nand_chip *chip = mtd->priv;
220 uint32_t val, dev_width = (chip->options & NAND_BUSWIDTH_16) >> 1;
221
222 switch (mode) {
223 case NAND_ECC_READ:
224 case NAND_ECC_WRITE:
225
226 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
227
228
229
230
231
232
233 writel(ECCSIZE1 | ECCSIZE0 | ECCSIZE0SEL,
234 &gpmc_cfg->ecc_size_config);
235 val = (dev_width << 7) | (cs << 1) | (0x1);
236 writel(val, &gpmc_cfg->ecc_config);
237 break;
238 default:
239 printf("Error: Unrecognized Mode[%d]!\n", mode);
240 break;
241 }
242}
243
244
245
246
247struct nand_bch_priv {
248 uint8_t mode;
249 uint8_t type;
250 uint8_t nibbles;
251 struct bch_control *control;
252};
253
254
255#define ECC_BCH4 0
256#define ECC_BCH8 1
257#define ECC_BCH16 2
258
259
260#define BCH_WRAPMODE_1 1
261#define BCH_WRAPMODE_6 6
262
263
264#define NAND_ECC_HW_BCH ((uint8_t)(NAND_ECC_HW_OOB_FIRST) + 1)
265#define ECC_BCH4_NIBBLES 13
266#define ECC_BCH8_NIBBLES 26
267#define ECC_BCH16_NIBBLES 52
268
269
270
271
272
273
274
275static __maybe_unused struct nand_bch_priv bch_priv = {
276 .mode = NAND_ECC_HW_BCH,
277 .type = ECC_BCH8,
278 .nibbles = ECC_BCH8_NIBBLES,
279 .control = NULL
280};
281
282
283
284
285
286
287
288__maybe_unused
289static void omap_hwecc_init_bch(struct nand_chip *chip, int32_t mode)
290{
291 uint32_t val;
292 uint32_t dev_width = (chip->options & NAND_BUSWIDTH_16) >> 1;
293#ifdef CONFIG_AM33XX
294 uint32_t unused_length = 0;
295#endif
296 uint32_t wr_mode = BCH_WRAPMODE_6;
297 struct nand_bch_priv *bch = chip->priv;
298
299
300 writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
301
302#ifdef CONFIG_AM33XX
303 wr_mode = BCH_WRAPMODE_1;
304
305 switch (bch->nibbles) {
306 case ECC_BCH4_NIBBLES:
307 unused_length = 3;
308 break;
309 case ECC_BCH8_NIBBLES:
310 unused_length = 2;
311 break;
312 case ECC_BCH16_NIBBLES:
313 unused_length = 0;
314 break;
315 }
316
317
318
319
320
321
322 switch (mode) {
323 case NAND_ECC_WRITE:
324
325 val = ((unused_length + bch->nibbles) << 22);
326 break;
327
328 case NAND_ECC_READ:
329 default:
330
331
332
333
334 val = (bch->nibbles << 12);
335
336 val |= (unused_length << 22);
337 break;
338 }
339#else
340
341
342
343
344
345
346
347
348
349
350
351 val = (32 << 22) | (0 << 12);
352#endif
353
354 writel(val, &gpmc_cfg->ecc_size_config);
355
356
357
358
359
360 val = (1 << 16);
361 val |= (bch->type << 12);
362 val |= (wr_mode << 8);
363 val |= (dev_width << 7);
364 val |= (cs << 1);
365 debug("set ECC_CONFIG=0x%08x\n", val);
366 writel(val, &gpmc_cfg->ecc_config);
367}
368
369
370
371
372
373
374__maybe_unused
375static void omap_enable_ecc_bch(struct mtd_info *mtd, int32_t mode)
376{
377 struct nand_chip *chip = mtd->priv;
378
379 omap_hwecc_init_bch(chip, mode);
380
381 writel((readl(&gpmc_cfg->ecc_config) | 0x1), &gpmc_cfg->ecc_config);
382}
383
384
385
386
387
388
389static void __maybe_unused omap_ecc_disable(struct mtd_info *mtd)
390{
391 writel((readl(&gpmc_cfg->ecc_config) & ~0x1), &gpmc_cfg->ecc_config);
392}
393
394
395
396
397#ifdef CONFIG_AM33XX
398
399
400
401
402
403
404
405static void omap_read_bch8_result(struct mtd_info *mtd, uint8_t big_endian,
406 uint8_t *ecc_code)
407{
408 uint32_t *ptr;
409 int8_t i = 0, j;
410
411 if (big_endian) {
412 ptr = &gpmc_cfg->bch_result_0_3[0].bch_result_x[3];
413 ecc_code[i++] = readl(ptr) & 0xFF;
414 ptr--;
415 for (j = 0; j < 3; j++) {
416 ecc_code[i++] = (readl(ptr) >> 24) & 0xFF;
417 ecc_code[i++] = (readl(ptr) >> 16) & 0xFF;
418 ecc_code[i++] = (readl(ptr) >> 8) & 0xFF;
419 ecc_code[i++] = readl(ptr) & 0xFF;
420 ptr--;
421 }
422 } else {
423 ptr = &gpmc_cfg->bch_result_0_3[0].bch_result_x[0];
424 for (j = 0; j < 3; j++) {
425 ecc_code[i++] = readl(ptr) & 0xFF;
426 ecc_code[i++] = (readl(ptr) >> 8) & 0xFF;
427 ecc_code[i++] = (readl(ptr) >> 16) & 0xFF;
428 ecc_code[i++] = (readl(ptr) >> 24) & 0xFF;
429 ptr++;
430 }
431 ecc_code[i++] = readl(ptr) & 0xFF;
432 ecc_code[i++] = 0;
433 }
434}
435
436
437
438
439
440
441
442
443
444static void omap_rotate_ecc_bch(struct mtd_info *mtd, uint8_t *calc_ecc,
445 uint8_t *syndrome)
446{
447 struct nand_chip *chip = mtd->priv;
448 struct nand_bch_priv *bch = chip->priv;
449 uint8_t n_bytes = 0;
450 int8_t i, j;
451
452 switch (bch->type) {
453 case ECC_BCH4:
454 n_bytes = 8;
455 break;
456
457 case ECC_BCH16:
458 n_bytes = 28;
459 break;
460
461 case ECC_BCH8:
462 default:
463 n_bytes = 13;
464 break;
465 }
466
467 for (i = 0, j = (n_bytes-1); i < n_bytes; i++, j--)
468 syndrome[i] = calc_ecc[j];
469}
470
471
472
473
474
475
476
477
478static int omap_calculate_ecc_bch(struct mtd_info *mtd, const uint8_t *dat,
479 uint8_t *ecc_code)
480{
481 struct nand_chip *chip = mtd->priv;
482 struct nand_bch_priv *bch = chip->priv;
483 uint8_t big_endian = 1;
484 int8_t ret = 0;
485
486 if (bch->type == ECC_BCH8)
487 omap_read_bch8_result(mtd, big_endian, ecc_code);
488 else
489 ret = -1;
490
491
492
493
494
495 omap_ecc_disable(mtd);
496
497 return ret;
498}
499
500
501
502
503
504
505
506
507
508
509static void omap_fix_errors_bch(struct mtd_info *mtd, uint8_t *data,
510 uint32_t error_count, uint32_t *error_loc)
511{
512 struct nand_chip *chip = mtd->priv;
513 struct nand_bch_priv *bch = chip->priv;
514 uint8_t count = 0;
515 uint32_t error_byte_pos;
516 uint32_t error_bit_mask;
517 uint32_t last_bit = (bch->nibbles * 4) - 1;
518
519
520
521 for (count = 0; count < error_count; count++) {
522 if (error_loc[count] > last_bit) {
523
524 error_loc[count] -= (last_bit + 1);
525
526 error_byte_pos = ((512 * 8) -
527 (error_loc[count]) - 1) / 8;
528
529 error_bit_mask = 0x1 << (error_loc[count] % 8);
530
531 data[error_byte_pos] ^= error_bit_mask;
532 }
533 }
534}
535
536
537
538
539
540
541
542
543
544
545
546
547static int omap_correct_data_bch(struct mtd_info *mtd, uint8_t *dat,
548 uint8_t *read_ecc, uint8_t *calc_ecc)
549{
550 struct nand_chip *chip = mtd->priv;
551 struct nand_bch_priv *bch = chip->priv;
552 uint8_t syndrome[28];
553 uint32_t error_count = 0;
554 uint32_t error_loc[8];
555 uint32_t i, ecc_flag;
556
557 ecc_flag = 0;
558 for (i = 0; i < chip->ecc.bytes; i++)
559 if (read_ecc[i] != 0xff)
560 ecc_flag = 1;
561
562 if (!ecc_flag)
563 return 0;
564
565 elm_reset();
566 elm_config((enum bch_level)(bch->type));
567
568
569
570
571
572 omap_rotate_ecc_bch(mtd, calc_ecc, syndrome);
573
574
575 if (elm_check_error(syndrome, bch->nibbles, &error_count,
576 error_loc) != 0) {
577 printf("ECC: uncorrectable.\n");
578 return -1;
579 }
580
581
582 if (error_count > 0)
583 omap_fix_errors_bch(mtd, dat, error_count, error_loc);
584
585 return 0;
586}
587
588
589
590
591
592
593
594
595
596static int omap_read_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
597 uint8_t *buf, int page)
598{
599 int i, eccsize = chip->ecc.size;
600 int eccbytes = chip->ecc.bytes;
601 int eccsteps = chip->ecc.steps;
602 uint8_t *p = buf;
603 uint8_t *ecc_calc = chip->buffers->ecccalc;
604 uint8_t *ecc_code = chip->buffers->ecccode;
605 uint32_t *eccpos = chip->ecc.layout->eccpos;
606 uint8_t *oob = chip->oob_poi;
607 uint32_t data_pos;
608 uint32_t oob_pos;
609
610 data_pos = 0;
611
612 oob_pos = (eccsize * eccsteps) + chip->ecc.layout->eccpos[0];
613 oob += chip->ecc.layout->eccpos[0];
614
615 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize,
616 oob += eccbytes) {
617 chip->ecc.hwctl(mtd, NAND_ECC_READ);
618
619 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_pos, page);
620 chip->read_buf(mtd, p, eccsize);
621
622
623 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, page);
624 chip->read_buf(mtd, oob, eccbytes);
625
626 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
627
628 data_pos += eccsize;
629 oob_pos += eccbytes;
630 }
631
632 for (i = 0; i < chip->ecc.total; i++)
633 ecc_code[i] = chip->oob_poi[eccpos[i]];
634
635 eccsteps = chip->ecc.steps;
636 p = buf;
637
638 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
639 int stat;
640
641 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
642 if (stat < 0)
643 mtd->ecc_stats.failed++;
644 else
645 mtd->ecc_stats.corrected += stat;
646 }
647 return 0;
648}
649#endif
650
651
652
653
654#ifdef CONFIG_NAND_OMAP_BCH8
655
656
657
658
659
660
661
662static int omap_calculate_ecc_bch(struct mtd_info *mtd, const uint8_t *dat,
663 uint8_t *ecc)
664{
665 int ret = 0;
666 size_t i;
667 unsigned long nsectors, val1, val2, val3, val4;
668
669 nsectors = ((readl(&gpmc_cfg->ecc_config) >> 4) & 0x7) + 1;
670
671 for (i = 0; i < nsectors; i++) {
672
673 val1 = readl(&gpmc_cfg->bch_result_0_3[i].bch_result_x[0]);
674 val2 = readl(&gpmc_cfg->bch_result_0_3[i].bch_result_x[1]);
675 val3 = readl(&gpmc_cfg->bch_result_0_3[i].bch_result_x[2]);
676 val4 = readl(&gpmc_cfg->bch_result_0_3[i].bch_result_x[3]);
677
678
679
680
681
682 *ecc++ = 0xef ^ (val4 & 0xFF);
683 *ecc++ = 0x51 ^ ((val3 >> 24) & 0xFF);
684 *ecc++ = 0x2e ^ ((val3 >> 16) & 0xFF);
685 *ecc++ = 0x09 ^ ((val3 >> 8) & 0xFF);
686 *ecc++ = 0xed ^ (val3 & 0xFF);
687 *ecc++ = 0x93 ^ ((val2 >> 24) & 0xFF);
688 *ecc++ = 0x9a ^ ((val2 >> 16) & 0xFF);
689 *ecc++ = 0xc2 ^ ((val2 >> 8) & 0xFF);
690 *ecc++ = 0x97 ^ (val2 & 0xFF);
691 *ecc++ = 0x79 ^ ((val1 >> 24) & 0xFF);
692 *ecc++ = 0xe5 ^ ((val1 >> 16) & 0xFF);
693 *ecc++ = 0x24 ^ ((val1 >> 8) & 0xFF);
694 *ecc++ = 0xb5 ^ (val1 & 0xFF);
695 }
696
697
698
699
700
701 omap_ecc_disable(mtd);
702
703 return ret;
704}
705
706
707
708
709
710
711
712
713static int omap_correct_data_bch(struct mtd_info *mtd, u_char *data,
714 u_char *read_ecc, u_char *calc_ecc)
715{
716 int i, count;
717
718 unsigned int errloc[8];
719 struct nand_chip *chip = mtd->priv;
720 struct nand_bch_priv *chip_priv = chip->priv;
721 struct bch_control *bch = chip_priv->control;
722
723 count = decode_bch(bch, NULL, 512, read_ecc, calc_ecc, NULL, errloc);
724 if (count > 0) {
725
726 for (i = 0; i < count; i++) {
727
728 if (errloc[i] < 8*512)
729 data[errloc[i]/8] ^= 1 << (errloc[i] & 7);
730 printf("corrected bitflip %u\n", errloc[i]);
731#ifdef DEBUG
732 puts("read_ecc: ");
733
734
735
736
737 for (i = 0; i < 13; i++)
738 printf("%02x ", read_ecc[i]);
739 puts("\n");
740 puts("calc_ecc: ");
741 for (i = 0; i < 13; i++)
742 printf("%02x ", calc_ecc[i]);
743 puts("\n");
744#endif
745 }
746 } else if (count < 0) {
747 puts("ecc unrecoverable error\n");
748 }
749 return count;
750}
751
752
753
754
755
756static void __maybe_unused omap_free_bch(struct mtd_info *mtd)
757{
758 struct nand_chip *chip = mtd->priv;
759 struct nand_bch_priv *chip_priv = chip->priv;
760 struct bch_control *bch = NULL;
761
762 if (chip_priv)
763 bch = chip_priv->control;
764
765 if (bch) {
766 free_bch(bch);
767 chip_priv->control = NULL;
768 }
769}
770#endif
771
772#ifndef CONFIG_SPL_BUILD
773
774
775
776
777
778
779
780
781void omap_nand_switch_ecc(uint32_t hardware, uint32_t eccstrength)
782{
783 struct nand_chip *nand;
784 struct mtd_info *mtd;
785
786 if (nand_curr_device < 0 ||
787 nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
788 !nand_info[nand_curr_device].name) {
789 printf("Error: Can't switch ecc, no devices available\n");
790 return;
791 }
792
793 mtd = &nand_info[nand_curr_device];
794 nand = mtd->priv;
795
796 nand->options |= NAND_OWN_BUFFERS;
797
798
799 nand->ecc.mode = NAND_ECC_NONE;
800 nand->ecc.read_page = NULL;
801 nand->ecc.write_page = NULL;
802 nand->ecc.read_oob = NULL;
803 nand->ecc.write_oob = NULL;
804 nand->ecc.hwctl = NULL;
805 nand->ecc.correct = NULL;
806 nand->ecc.calculate = NULL;
807
808
809 if (hardware) {
810 if (eccstrength == 1) {
811 nand->ecc.mode = NAND_ECC_HW;
812 nand->ecc.layout = &hw_nand_oob;
813 nand->ecc.size = 512;
814 nand->ecc.bytes = 3;
815 nand->ecc.hwctl = omap_enable_hwecc;
816 nand->ecc.correct = omap_correct_data;
817 nand->ecc.calculate = omap_calculate_ecc;
818 omap_hwecc_init(nand);
819 printf("1-bit hamming HW ECC selected\n");
820 }
821#if defined(CONFIG_AM33XX) || defined(CONFIG_NAND_OMAP_BCH8)
822 else if (eccstrength == 8) {
823 nand->ecc.mode = NAND_ECC_HW;
824 nand->ecc.layout = &hw_bch8_nand_oob;
825 nand->ecc.size = 512;
826#ifdef CONFIG_AM33XX
827 nand->ecc.bytes = 14;
828 nand->ecc.read_page = omap_read_page_bch;
829#else
830 nand->ecc.bytes = 13;
831#endif
832 nand->ecc.hwctl = omap_enable_ecc_bch;
833 nand->ecc.correct = omap_correct_data_bch;
834 nand->ecc.calculate = omap_calculate_ecc_bch;
835 omap_hwecc_init_bch(nand, NAND_ECC_READ);
836 printf("8-bit BCH HW ECC selected\n");
837 }
838#endif
839 } else {
840 nand->ecc.mode = NAND_ECC_SOFT;
841
842 nand->ecc.layout = NULL;
843 nand->ecc.size = 0;
844 printf("SW ECC selected\n");
845 }
846
847
848 nand_scan_tail(mtd);
849
850 nand->options &= ~NAND_OWN_BUFFERS;
851}
852#endif
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869int board_nand_init(struct nand_chip *nand)
870{
871 int32_t gpmc_config = 0;
872 cs = 0;
873
874
875
876
877
878
879
880
881 while (cs < GPMC_MAX_CS) {
882
883 if ((readl(&gpmc_cfg->cs[cs].config1) & 0xC00) == 0x800) {
884
885 break;
886 }
887 cs++;
888 }
889 if (cs >= GPMC_MAX_CS) {
890 printf("NAND: Unable to find NAND settings in "
891 "GPMC Configuration - quitting\n");
892 return -ENODEV;
893 }
894
895 gpmc_config = readl(&gpmc_cfg->config);
896
897 gpmc_config |= 0x10;
898 writel(gpmc_config, &gpmc_cfg->config);
899
900 nand->IO_ADDR_R = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
901 nand->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
902
903 nand->cmd_ctrl = omap_nand_hwcontrol;
904 nand->options = NAND_NO_PADDING | NAND_CACHEPRG | NAND_NO_AUTOINCR;
905
906 if ((readl(&gpmc_cfg->cs[cs].config1) & 0x3000) == 0x1000)
907 nand->options |= NAND_BUSWIDTH_16;
908
909 nand->chip_delay = 100;
910
911#if defined(CONFIG_AM33XX) || defined(CONFIG_NAND_OMAP_BCH8)
912#ifdef CONFIG_AM33XX
913
914
915 elm_init();
916#else
917
918
919
920
921 bch_priv.control = init_bch(13, 8, 0x201b );
922 if (!bch_priv.control) {
923 puts("Could not init_bch()\n");
924 return -ENODEV;
925 }
926#endif
927
928 nand->priv = &bch_priv;
929#endif
930
931
932#if defined(CONFIG_AM33XX) || defined(CONFIG_NAND_OMAP_BCH8)
933 nand->ecc.mode = NAND_ECC_HW;
934 nand->ecc.layout = &hw_bch8_nand_oob;
935 nand->ecc.size = CONFIG_SYS_NAND_ECCSIZE;
936 nand->ecc.bytes = CONFIG_SYS_NAND_ECCBYTES;
937 nand->ecc.hwctl = omap_enable_ecc_bch;
938 nand->ecc.correct = omap_correct_data_bch;
939 nand->ecc.calculate = omap_calculate_ecc_bch;
940#ifdef CONFIG_AM33XX
941 nand->ecc.read_page = omap_read_page_bch;
942#endif
943 omap_hwecc_init_bch(nand, NAND_ECC_READ);
944#else
945#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_NAND_SOFTECC)
946 nand->ecc.mode = NAND_ECC_SOFT;
947#else
948 nand->ecc.mode = NAND_ECC_HW;
949 nand->ecc.layout = &hw_nand_oob;
950 nand->ecc.size = CONFIG_SYS_NAND_ECCSIZE;
951 nand->ecc.bytes = CONFIG_SYS_NAND_ECCBYTES;
952 nand->ecc.hwctl = omap_enable_hwecc;
953 nand->ecc.correct = omap_correct_data;
954 nand->ecc.calculate = omap_calculate_ecc;
955 omap_hwecc_init(nand);
956#endif
957#endif
958
959#ifdef CONFIG_SPL_BUILD
960 if (nand->options & NAND_BUSWIDTH_16)
961 nand->read_buf = nand_read_buf16;
962 else
963 nand->read_buf = nand_read_buf;
964 nand->dev_ready = omap_spl_dev_ready;
965#endif
966
967 return 0;
968}
969