1 // SPDX-License-Identifier: GPL-2.0+
3 * NAND driver for TI DaVinci based boards.
5 * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
7 * Based on Linux DaVinci NAND driver by TI. Original copyright follows:
12 * linux/drivers/mtd/nand/raw/nand_davinci.c
16 * Copyright (C) 2006 Texas Instruments.
18 * ----------------------------------------------------------------------------
20 * ----------------------------------------------------------------------------
23 * This is a device driver for the NAND flash device found on the
24 * DaVinci board which utilizes the Samsung k9k2g08 part.
27 ver. 1.0: Feb 2005, Vinod/Sudhakar
33 #include <linux/mtd/rawnand.h>
36 #include <dm/uclass.h>
37 #include <asm/ti-common/davinci_nand.h>
38 #include <linux/printk.h>
40 /* Definitions for 4-bit hardware ECC */
41 #define NAND_TIMEOUT 10240
42 #define NAND_ECC_BUSY 0xC
43 #define NAND_4BITECC_MASK 0x03FF03FF
44 #define EMIF_NANDFSR_ECC_STATE_MASK 0x00000F00
45 #define ECC_STATE_NO_ERR 0x0
46 #define ECC_STATE_TOO_MANY_ERRS 0x1
47 #define ECC_STATE_ERR_CORR_COMP_P 0x2
48 #define ECC_STATE_ERR_CORR_COMP_N 0x3
51 * Exploit the little endianness of the ARM to do multi-byte transfers
52 * per device read. This can perform over twice as quickly as individual
53 * byte transfers when buffer alignment is conducive.
55 * NOTE: This only works if the NAND is not connected to the 2 LSBs of
56 * the address bus. On Davinci EVM platforms this has always been true.
58 static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
60 struct nand_chip *chip = mtd_to_nand(mtd);
61 const u32 *nand = chip->IO_ADDR_R;
63 /* Make sure that buf is 32 bit aligned */
64 if (((int)buf & 0x3) != 0) {
65 if (((int)buf & 0x1) != 0) {
73 if (((int)buf & 0x3) != 0) {
75 *(u16 *)buf = readw(nand);
82 /* copy aligned data */
84 *(u32 *)buf = __raw_readl(nand);
89 /* mop up any remaining bytes */
92 *(u16 *)buf = readw(nand);
102 static void nand_davinci_write_buf(struct mtd_info *mtd, const uint8_t *buf,
105 struct nand_chip *chip = mtd_to_nand(mtd);
106 const u32 *nand = chip->IO_ADDR_W;
108 /* Make sure that buf is 32 bit aligned */
109 if (((int)buf & 0x3) != 0) {
110 if (((int)buf & 0x1) != 0) {
118 if (((int)buf & 0x3) != 0) {
120 writew(*(u16 *)buf, nand);
127 /* copy aligned data */
129 __raw_writel(*(u32 *)buf, nand);
134 /* mop up any remaining bytes */
137 writew(*(u16 *)buf, nand);
147 static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd,
150 struct nand_chip *this = mtd_to_nand(mtd);
151 u_int32_t IO_ADDR_W = (u_int32_t)this->IO_ADDR_W;
153 if (ctrl & NAND_CTRL_CHANGE) {
154 IO_ADDR_W &= ~(MASK_ALE|MASK_CLE);
157 IO_ADDR_W |= MASK_CLE;
159 IO_ADDR_W |= MASK_ALE;
160 this->IO_ADDR_W = (void __iomem *) IO_ADDR_W;
163 if (cmd != NAND_CMD_NONE)
164 writeb(cmd, IO_ADDR_W);
167 #ifdef CONFIG_SYS_NAND_HW_ECC
169 static u_int32_t nand_davinci_readecc(struct mtd_info *mtd)
173 ecc = __raw_readl(&(davinci_emif_regs->nandfecc[
174 CFG_SYS_NAND_CS - 2]));
179 static void nand_davinci_enable_hwecc(struct mtd_info *mtd, int mode)
183 /* reading the ECC result register resets the ECC calculation */
184 nand_davinci_readecc(mtd);
186 val = __raw_readl(&davinci_emif_regs->nandfcr);
187 val |= DAVINCI_NANDFCR_NAND_ENABLE(CFG_SYS_NAND_CS);
188 val |= DAVINCI_NANDFCR_1BIT_ECC_START(CFG_SYS_NAND_CS);
189 __raw_writel(val, &davinci_emif_regs->nandfcr);
192 static int nand_davinci_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
197 tmp = nand_davinci_readecc(mtd);
199 /* Squeeze 4 bytes ECC into 3 bytes by removing RESERVED bits
200 * and shifting. RESERVED bits are 31 to 28 and 15 to 12. */
201 tmp = (tmp & 0x00000fff) | ((tmp & 0x0fff0000) >> 4);
203 /* Invert so that erased block ECC is correct */
207 *ecc_code++ = tmp >> 8;
208 *ecc_code++ = tmp >> 16;
210 /* NOTE: the above code matches mainline Linux:
211 * .PQR.stu ==> ~PQRstu
213 * MontaVista/TI kernels encode those bytes differently, use
214 * complicated (and allegedly sometimes-wrong) correction code,
215 * and usually shipped with U-Boot that uses software ECC:
216 * .PQR.stu ==> PsQRtu
218 * If you need MV/TI compatible NAND I/O in U-Boot, it should
219 * be possible to (a) change the mangling above, (b) reverse
220 * that mangling in nand_davinci_correct_data() below.
226 static int nand_davinci_correct_data(struct mtd_info *mtd, u_char *dat,
227 u_char *read_ecc, u_char *calc_ecc)
229 struct nand_chip *this = mtd_to_nand(mtd);
230 u_int32_t ecc_nand = read_ecc[0] | (read_ecc[1] << 8) |
232 u_int32_t ecc_calc = calc_ecc[0] | (calc_ecc[1] << 8) |
234 u_int32_t diff = ecc_calc ^ ecc_nand;
237 if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) {
238 /* Correctable error */
239 if ((diff >> (12 + 3)) < this->ecc.size) {
240 uint8_t find_bit = 1 << ((diff >> 12) & 7);
241 uint32_t find_byte = diff >> (12 + 3);
243 dat[find_byte] ^= find_bit;
244 pr_debug("Correcting single "
245 "bit ECC error at offset: %d, bit: "
246 "%d\n", find_byte, find_bit);
251 } else if (!(diff & (diff - 1))) {
252 /* Single bit ECC error in the ECC itself,
254 pr_debug("Single bit ECC error in " "ECC.\n");
257 /* Uncorrectable error */
258 pr_debug("ECC UNCORRECTED_ERROR 1\n");
264 #endif /* CONFIG_SYS_NAND_HW_ECC */
266 #ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST
267 static struct nand_ecclayout nand_davinci_4bit_layout_oobfirst = {
268 #if defined(CONFIG_SYS_NAND_PAGE_2K)
270 #ifdef CONFIG_NAND_6BYTES_OOB_FREE_10BYTES_ECC
272 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
273 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
274 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
275 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
278 {2, 4}, {16, 6}, {32, 6}, {48, 6},
283 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
284 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
285 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
289 {.offset = 2, .length = 22, },
291 #endif /* #ifdef CONFIG_NAND_6BYTES_OOB_FREE_10BYTES_ECC */
292 #elif defined(CONFIG_SYS_NAND_PAGE_4K)
295 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
296 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
297 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
298 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
299 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
300 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
301 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
302 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
305 {.offset = 2, .length = 46, },
310 #if defined CONFIG_KEYSTONE_RBL_NAND
311 static struct nand_ecclayout nand_keystone_rbl_4bit_layout_oobfirst = {
312 #if defined(CONFIG_SYS_NAND_PAGE_2K)
315 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
316 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
317 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
318 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
321 {.offset = 2, .length = 4, },
322 {.offset = 16, .length = 6, },
323 {.offset = 32, .length = 6, },
324 {.offset = 48, .length = 6, },
326 #elif defined(CONFIG_SYS_NAND_PAGE_4K)
329 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
330 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
331 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
332 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
333 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
334 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
335 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
336 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
339 {.offset = 2, .length = 4, },
340 {.offset = 16, .length = 6, },
341 {.offset = 32, .length = 6, },
342 {.offset = 48, .length = 6, },
343 {.offset = 64, .length = 6, },
344 {.offset = 80, .length = 6, },
345 {.offset = 96, .length = 6, },
346 {.offset = 112, .length = 6, },
351 #ifdef CONFIG_SYS_NAND_PAGE_2K
352 #define KEYSTONE_NAND_MAX_RBL_PAGE (0x100000 >> 11)
353 #elif defined(CONFIG_SYS_NAND_PAGE_4K)
354 #define KEYSTONE_NAND_MAX_RBL_PAGE (0x100000 >> 12)
358 * nand_davinci_write_page - write one page
359 * @mtd: MTD device structure
360 * @chip: NAND chip descriptor
361 * @buf: the data to write
362 * @oob_required: must write chip->oob_poi to OOB
363 * @page: page number to write
364 * @raw: use _raw version of write_page
366 static int nand_davinci_write_page(struct mtd_info *mtd, struct nand_chip *chip,
367 uint32_t offset, int data_len,
368 const uint8_t *buf, int oob_required,
373 struct nand_ecclayout *saved_ecc_layout;
375 /* save current ECC layout and assign Keystone RBL ECC layout */
376 if (page < KEYSTONE_NAND_MAX_RBL_PAGE) {
377 saved_ecc_layout = chip->ecc.layout;
378 chip->ecc.layout = &nand_keystone_rbl_4bit_layout_oobfirst;
379 mtd->oobavail = chip->ecc.layout->oobavail;
382 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
385 status = chip->ecc.write_page_raw(mtd, chip, buf,
388 status = chip->ecc.write_page(mtd, chip, buf,
397 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
398 status = chip->waitfunc(mtd, chip);
400 if (status & NAND_STATUS_FAIL) {
406 /* restore ECC layout */
407 if (page < KEYSTONE_NAND_MAX_RBL_PAGE) {
408 chip->ecc.layout = saved_ecc_layout;
409 mtd->oobavail = saved_ecc_layout->oobavail;
416 * nand_davinci_read_page_hwecc - hardware ECC based page read function
417 * @mtd: mtd info structure
418 * @chip: nand chip info structure
419 * @buf: buffer to store read data
420 * @oob_required: caller requires OOB data read to chip->oob_poi
421 * @page: page number to read
423 * Not for syndrome calculating ECC controllers which need a special oob layout.
425 static int nand_davinci_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
426 uint8_t *buf, int oob_required, int page)
428 int i, eccsize = chip->ecc.size;
429 int eccbytes = chip->ecc.bytes;
430 int eccsteps = chip->ecc.steps;
433 uint8_t *ecc_code = chip->buffers->ecccode;
434 uint8_t *ecc_calc = chip->buffers->ecccalc;
435 struct nand_ecclayout *saved_ecc_layout = chip->ecc.layout;
437 /* save current ECC layout and assign Keystone RBL ECC layout */
438 if (page < KEYSTONE_NAND_MAX_RBL_PAGE) {
439 chip->ecc.layout = &nand_keystone_rbl_4bit_layout_oobfirst;
440 mtd->oobavail = chip->ecc.layout->oobavail;
443 eccpos = chip->ecc.layout->eccpos;
445 /* Read the OOB area first */
446 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
447 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
448 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
450 for (i = 0; i < chip->ecc.total; i++)
451 ecc_code[i] = chip->oob_poi[eccpos[i]];
453 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
456 chip->ecc.hwctl(mtd, NAND_ECC_READ);
457 chip->read_buf(mtd, p, eccsize);
458 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
460 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
462 mtd->ecc_stats.failed++;
464 mtd->ecc_stats.corrected += stat;
467 /* restore ECC layout */
468 if (page < KEYSTONE_NAND_MAX_RBL_PAGE) {
469 chip->ecc.layout = saved_ecc_layout;
470 mtd->oobavail = saved_ecc_layout->oobavail;
475 #endif /* CONFIG_KEYSTONE_RBL_NAND */
477 static void nand_davinci_4bit_enable_hwecc(struct mtd_info *mtd, int mode)
485 * Start a new ECC calculation for reading or writing 512 bytes
488 val = __raw_readl(&davinci_emif_regs->nandfcr);
489 val &= ~DAVINCI_NANDFCR_4BIT_ECC_SEL_MASK;
490 val |= DAVINCI_NANDFCR_NAND_ENABLE(CFG_SYS_NAND_CS);
491 val |= DAVINCI_NANDFCR_4BIT_ECC_SEL(CFG_SYS_NAND_CS);
492 val |= DAVINCI_NANDFCR_4BIT_ECC_START;
493 __raw_writel(val, &davinci_emif_regs->nandfcr);
495 case NAND_ECC_READSYN:
496 val = __raw_readl(&davinci_emif_regs->nand4bitecc[0]);
503 static u32 nand_davinci_4bit_readecc(struct mtd_info *mtd, unsigned int ecc[4])
507 for (i = 0; i < 4; i++) {
508 ecc[i] = __raw_readl(&davinci_emif_regs->nand4bitecc[i]) &
515 static int nand_davinci_4bit_calculate_ecc(struct mtd_info *mtd,
519 unsigned int hw_4ecc[4];
522 nand_davinci_4bit_readecc(mtd, hw_4ecc);
524 /*Convert 10 bit ecc value to 8 bit */
525 for (i = 0; i < 2; i++) {
526 unsigned int hw_ecc_low = hw_4ecc[i * 2];
527 unsigned int hw_ecc_hi = hw_4ecc[(i * 2) + 1];
529 /* Take first 8 bits from val1 (count1=0) or val5 (count1=1) */
530 *ecc_code++ = hw_ecc_low & 0xFF;
533 * Take 2 bits as LSB bits from val1 (count1=0) or val5
534 * (count1=1) and 6 bits from val2 (count1=0) or
538 ((hw_ecc_low >> 8) & 0x3) | ((hw_ecc_low >> 14) & 0xFC);
541 * Take 4 bits from val2 (count1=0) or val5 (count1=1) and
542 * 4 bits from val3 (count1=0) or val6 (count1=1)
545 ((hw_ecc_low >> 22) & 0xF) | ((hw_ecc_hi << 4) & 0xF0);
548 * Take 6 bits from val3(count1=0) or val6 (count1=1) and
549 * 2 bits from val4 (count1=0) or val7 (count1=1)
552 ((hw_ecc_hi >> 4) & 0x3F) | ((hw_ecc_hi >> 10) & 0xC0);
554 /* Take 8 bits from val4 (count1=0) or val7 (count1=1) */
555 *ecc_code++ = (hw_ecc_hi >> 18) & 0xFF;
561 static int nand_davinci_4bit_correct_data(struct mtd_info *mtd, uint8_t *dat,
562 uint8_t *read_ecc, uint8_t *calc_ecc)
565 unsigned int hw_4ecc[4];
566 unsigned int iserror;
567 unsigned short *ecc16;
568 unsigned int numerrors, erroraddress, errorvalue;
572 * Check for an ECC where all bytes are 0xFF. If this is the case, we
573 * will assume we are looking at an erased page and we should ignore
576 for (i = 0; i < 10; i++) {
577 if (read_ecc[i] != 0xFF)
583 /* Convert 8 bit in to 10 bit */
584 ecc16 = (unsigned short *)&read_ecc[0];
587 * Write the parity values in the NAND Flash 4-bit ECC Load register.
588 * Write each parity value one at a time starting from 4bit_ecc_val8
592 /*Take 2 bits from 8th byte and 8 bits from 9th byte */
593 __raw_writel(((ecc16[4]) >> 6) & 0x3FF,
594 &davinci_emif_regs->nand4biteccload);
596 /* Take 4 bits from 7th byte and 6 bits from 8th byte */
597 __raw_writel((((ecc16[3]) >> 12) & 0xF) | ((((ecc16[4])) << 4) & 0x3F0),
598 &davinci_emif_regs->nand4biteccload);
600 /* Take 6 bits from 6th byte and 4 bits from 7th byte */
601 __raw_writel((ecc16[3] >> 2) & 0x3FF,
602 &davinci_emif_regs->nand4biteccload);
604 /* Take 8 bits from 5th byte and 2 bits from 6th byte */
605 __raw_writel(((ecc16[2]) >> 8) | ((((ecc16[3])) << 8) & 0x300),
606 &davinci_emif_regs->nand4biteccload);
608 /*Take 2 bits from 3rd byte and 8 bits from 4th byte */
609 __raw_writel((((ecc16[1]) >> 14) & 0x3) | ((((ecc16[2])) << 2) & 0x3FC),
610 &davinci_emif_regs->nand4biteccload);
612 /* Take 4 bits form 2nd bytes and 6 bits from 3rd bytes */
613 __raw_writel(((ecc16[1]) >> 4) & 0x3FF,
614 &davinci_emif_regs->nand4biteccload);
616 /* Take 6 bits from 1st byte and 4 bits from 2nd byte */
617 __raw_writel((((ecc16[0]) >> 10) & 0x3F) | (((ecc16[1]) << 6) & 0x3C0),
618 &davinci_emif_regs->nand4biteccload);
620 /* Take 10 bits from 0th and 1st bytes */
621 __raw_writel((ecc16[0]) & 0x3FF,
622 &davinci_emif_regs->nand4biteccload);
625 * Perform a dummy read to the EMIF Revision Code and Status register.
626 * This is required to ensure time for syndrome calculation after
627 * writing the ECC values in previous step.
630 val = __raw_readl(&davinci_emif_regs->nandfsr);
633 * Read the syndrome from the NAND Flash 4-Bit ECC 1-4 registers.
634 * A syndrome value of 0 means no bit errors. If the syndrome is
635 * non-zero then go further otherwise return.
637 nand_davinci_4bit_readecc(mtd, hw_4ecc);
639 if (!(hw_4ecc[0] | hw_4ecc[1] | hw_4ecc[2] | hw_4ecc[3]))
643 * Clear any previous address calculation by doing a dummy read of an
644 * error address register.
646 val = __raw_readl(&davinci_emif_regs->nanderradd1);
649 * Set the addr_calc_st bit(bit no 13) in the NAND Flash Control
652 __raw_writel(DAVINCI_NANDFCR_4BIT_CALC_START,
653 &davinci_emif_regs->nandfcr);
656 * Wait for the corr_state field (bits 8 to 11) in the
657 * NAND Flash Status register to be not equal to 0x0, 0x1, 0x2, or 0x3.
658 * Otherwise ECC calculation has not even begun and the next loop might
659 * fail because of a false positive!
663 val = __raw_readl(&davinci_emif_regs->nandfsr);
666 } while ((i > 0) && !val);
669 * Wait for the corr_state field (bits 8 to 11) in the
670 * NAND Flash Status register to be equal to 0x0, 0x1, 0x2, or 0x3.
674 val = __raw_readl(&davinci_emif_regs->nandfsr);
677 } while ((i > 0) && val);
679 iserror = __raw_readl(&davinci_emif_regs->nandfsr);
680 iserror &= EMIF_NANDFSR_ECC_STATE_MASK;
681 iserror = iserror >> 8;
684 * ECC_STATE_TOO_MANY_ERRS (0x1) means errors cannot be
685 * corrected (five or more errors). The number of errors
686 * calculated (err_num field) differs from the number of errors
687 * searched. ECC_STATE_ERR_CORR_COMP_P (0x2) means error
688 * correction complete (errors on bit 8 or 9).
689 * ECC_STATE_ERR_CORR_COMP_N (0x3) means error correction
690 * complete (error exists).
693 if (iserror == ECC_STATE_NO_ERR) {
694 val = __raw_readl(&davinci_emif_regs->nanderrval1);
696 } else if (iserror == ECC_STATE_TOO_MANY_ERRS) {
697 val = __raw_readl(&davinci_emif_regs->nanderrval1);
701 numerrors = ((__raw_readl(&davinci_emif_regs->nandfsr) >> 16)
704 /* Read the error address, error value and correct */
705 for (i = 0; i < numerrors; i++) {
708 ((__raw_readl(&davinci_emif_regs->nanderradd2) >>
709 (16 * (i & 1))) & 0x3FF);
710 erroraddress = ((512 + 7) - erroraddress);
712 ((__raw_readl(&davinci_emif_regs->nanderrval2) >>
713 (16 * (i & 1))) & 0xFF);
716 ((__raw_readl(&davinci_emif_regs->nanderradd1) >>
717 (16 * (i & 1))) & 0x3FF);
718 erroraddress = ((512 + 7) - erroraddress);
720 ((__raw_readl(&davinci_emif_regs->nanderrval1) >>
721 (16 * (i & 1))) & 0xFF);
723 /* xor the corrupt data with error value */
724 if (erroraddress < 512)
725 dat[erroraddress] ^= errorvalue;
730 #endif /* CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST */
732 static int nand_davinci_dev_ready(struct mtd_info *mtd)
734 return __raw_readl(&davinci_emif_regs->nandfsr) & 0x1;
737 static void davinci_nand_init(struct nand_chip *nand)
739 #if defined CONFIG_KEYSTONE_RBL_NAND
741 struct nand_ecclayout *layout;
743 layout = &nand_keystone_rbl_4bit_layout_oobfirst;
744 layout->oobavail = 0;
745 for (i = 0; i < ARRAY_SIZE(layout->oobfree) &&
746 layout->oobfree[i].length; i++)
747 layout->oobavail += layout->oobfree[i].length;
749 nand->write_page = nand_davinci_write_page;
750 nand->ecc.read_page = nand_davinci_read_page_hwecc;
752 nand->chip_delay = 0;
753 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
754 nand->bbt_options |= NAND_BBT_USE_FLASH;
756 #ifdef CONFIG_SYS_NAND_NO_SUBPAGE_WRITE
757 nand->options |= NAND_NO_SUBPAGE_WRITE;
759 #ifdef CONFIG_SYS_NAND_BUSWIDTH_16BIT
760 nand->options |= NAND_BUSWIDTH_16;
762 #ifdef CONFIG_SYS_NAND_HW_ECC
763 nand->ecc.mode = NAND_ECC_HW;
764 nand->ecc.size = 512;
766 nand->ecc.strength = 1;
767 nand->ecc.calculate = nand_davinci_calculate_ecc;
768 nand->ecc.correct = nand_davinci_correct_data;
769 nand->ecc.hwctl = nand_davinci_enable_hwecc;
770 #elif defined(CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST)
771 nand->ecc.mode = NAND_ECC_HW_OOB_FIRST;
772 nand->ecc.size = 512;
773 nand->ecc.bytes = 10;
774 nand->ecc.strength = 4;
775 nand->ecc.calculate = nand_davinci_4bit_calculate_ecc;
776 nand->ecc.correct = nand_davinci_4bit_correct_data;
777 nand->ecc.hwctl = nand_davinci_4bit_enable_hwecc;
778 nand->ecc.layout = &nand_davinci_4bit_layout_oobfirst;
779 #elif defined(CONFIG_SYS_NAND_SOFT_ECC)
780 nand->ecc.mode = NAND_ECC_SOFT;
782 /* Set address of hardware control function */
783 nand->cmd_ctrl = nand_davinci_hwcontrol;
785 nand->read_buf = nand_davinci_read_buf;
786 nand->write_buf = nand_davinci_write_buf;
788 nand->dev_ready = nand_davinci_dev_ready;
791 #if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
792 static int davinci_nand_probe(struct udevice *dev)
794 struct nand_chip *nand = dev_get_priv(dev);
795 struct mtd_info *mtd = nand_to_mtd(nand);
798 nand->IO_ADDR_R = (void __iomem *)CFG_SYS_NAND_BASE;
799 nand->IO_ADDR_W = (void __iomem *)CFG_SYS_NAND_BASE;
801 davinci_nand_init(nand);
803 ret = nand_scan(mtd, CONFIG_SYS_NAND_MAX_CHIPS);
807 return nand_register(0, mtd);
810 static const struct udevice_id davinci_nand_ids[] = {
811 { .compatible = "ti,davinci-nand" },
815 U_BOOT_DRIVER(davinci_nand) = {
816 .name = "davinci-nand",
818 .of_match = davinci_nand_ids,
819 .probe = davinci_nand_probe,
820 .priv_auto = sizeof(struct nand_chip),
823 void board_nand_init(void)
828 ret = uclass_get_device_by_driver(UCLASS_MTD,
829 DM_DRIVER_GET(davinci_nand), &dev);
830 if (ret && ret != -ENODEV)
831 pr_err("Failed to initialize %s: %d\n", dev->name, ret);
834 int board_nand_init(struct nand_chip *chip) __attribute__((weak));
835 int board_nand_init(struct nand_chip *chip)
837 davinci_nand_init(chip);
840 #endif /* CONFIG_SYS_NAND_SELF_INIT */