xref: /rk3399_rockchip-uboot/drivers/mtd/nand/raw/davinci_nand.c (revision 0d1b2165f037855103bbc8e853f8ad1ce9c41865)
1*cfcc706cSMiquel Raynal /*
2*cfcc706cSMiquel Raynal  * NAND driver for TI DaVinci based boards.
3*cfcc706cSMiquel Raynal  *
4*cfcc706cSMiquel Raynal  * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
5*cfcc706cSMiquel Raynal  *
6*cfcc706cSMiquel Raynal  * Based on Linux DaVinci NAND driver by TI. Original copyright follows:
7*cfcc706cSMiquel Raynal  */
8*cfcc706cSMiquel Raynal 
9*cfcc706cSMiquel Raynal /*
10*cfcc706cSMiquel Raynal  *
11*cfcc706cSMiquel Raynal  * linux/drivers/mtd/nand/raw/nand_davinci.c
12*cfcc706cSMiquel Raynal  *
13*cfcc706cSMiquel Raynal  * NAND Flash Driver
14*cfcc706cSMiquel Raynal  *
15*cfcc706cSMiquel Raynal  * Copyright (C) 2006 Texas Instruments.
16*cfcc706cSMiquel Raynal  *
17*cfcc706cSMiquel Raynal  * ----------------------------------------------------------------------------
18*cfcc706cSMiquel Raynal  *
19*cfcc706cSMiquel Raynal  * SPDX-License-Identifier:	GPL-2.0+
20*cfcc706cSMiquel Raynal  *
21*cfcc706cSMiquel Raynal  * ----------------------------------------------------------------------------
22*cfcc706cSMiquel Raynal  *
23*cfcc706cSMiquel Raynal  *  Overview:
24*cfcc706cSMiquel Raynal  *   This is a device driver for the NAND flash device found on the
25*cfcc706cSMiquel Raynal  *   DaVinci board which utilizes the Samsung k9k2g08 part.
26*cfcc706cSMiquel Raynal  *
27*cfcc706cSMiquel Raynal  Modifications:
28*cfcc706cSMiquel Raynal  ver. 1.0: Feb 2005, Vinod/Sudhakar
29*cfcc706cSMiquel Raynal  -
30*cfcc706cSMiquel Raynal  */
31*cfcc706cSMiquel Raynal 
32*cfcc706cSMiquel Raynal #include <common.h>
33*cfcc706cSMiquel Raynal #include <asm/io.h>
34*cfcc706cSMiquel Raynal #include <nand.h>
35*cfcc706cSMiquel Raynal #include <asm/ti-common/davinci_nand.h>
36*cfcc706cSMiquel Raynal 
37*cfcc706cSMiquel Raynal /* Definitions for 4-bit hardware ECC */
38*cfcc706cSMiquel Raynal #define NAND_TIMEOUT			10240
39*cfcc706cSMiquel Raynal #define NAND_ECC_BUSY			0xC
40*cfcc706cSMiquel Raynal #define NAND_4BITECC_MASK		0x03FF03FF
41*cfcc706cSMiquel Raynal #define EMIF_NANDFSR_ECC_STATE_MASK  	0x00000F00
42*cfcc706cSMiquel Raynal #define ECC_STATE_NO_ERR		0x0
43*cfcc706cSMiquel Raynal #define ECC_STATE_TOO_MANY_ERRS		0x1
44*cfcc706cSMiquel Raynal #define ECC_STATE_ERR_CORR_COMP_P	0x2
45*cfcc706cSMiquel Raynal #define ECC_STATE_ERR_CORR_COMP_N	0x3
46*cfcc706cSMiquel Raynal 
47*cfcc706cSMiquel Raynal /*
48*cfcc706cSMiquel Raynal  * Exploit the little endianness of the ARM to do multi-byte transfers
49*cfcc706cSMiquel Raynal  * per device read. This can perform over twice as quickly as individual
50*cfcc706cSMiquel Raynal  * byte transfers when buffer alignment is conducive.
51*cfcc706cSMiquel Raynal  *
52*cfcc706cSMiquel Raynal  * NOTE: This only works if the NAND is not connected to the 2 LSBs of
53*cfcc706cSMiquel Raynal  * the address bus. On Davinci EVM platforms this has always been true.
54*cfcc706cSMiquel Raynal  */
nand_davinci_read_buf(struct mtd_info * mtd,uint8_t * buf,int len)55*cfcc706cSMiquel Raynal static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
56*cfcc706cSMiquel Raynal {
57*cfcc706cSMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
58*cfcc706cSMiquel Raynal 	const u32 *nand = chip->IO_ADDR_R;
59*cfcc706cSMiquel Raynal 
60*cfcc706cSMiquel Raynal 	/* Make sure that buf is 32 bit aligned */
61*cfcc706cSMiquel Raynal 	if (((int)buf & 0x3) != 0) {
62*cfcc706cSMiquel Raynal 		if (((int)buf & 0x1) != 0) {
63*cfcc706cSMiquel Raynal 			if (len) {
64*cfcc706cSMiquel Raynal 				*buf = readb(nand);
65*cfcc706cSMiquel Raynal 				buf += 1;
66*cfcc706cSMiquel Raynal 				len--;
67*cfcc706cSMiquel Raynal 			}
68*cfcc706cSMiquel Raynal 		}
69*cfcc706cSMiquel Raynal 
70*cfcc706cSMiquel Raynal 		if (((int)buf & 0x3) != 0) {
71*cfcc706cSMiquel Raynal 			if (len >= 2) {
72*cfcc706cSMiquel Raynal 				*(u16 *)buf = readw(nand);
73*cfcc706cSMiquel Raynal 				buf += 2;
74*cfcc706cSMiquel Raynal 				len -= 2;
75*cfcc706cSMiquel Raynal 			}
76*cfcc706cSMiquel Raynal 		}
77*cfcc706cSMiquel Raynal 	}
78*cfcc706cSMiquel Raynal 
79*cfcc706cSMiquel Raynal 	/* copy aligned data */
80*cfcc706cSMiquel Raynal 	while (len >= 4) {
81*cfcc706cSMiquel Raynal 		*(u32 *)buf = __raw_readl(nand);
82*cfcc706cSMiquel Raynal 		buf += 4;
83*cfcc706cSMiquel Raynal 		len -= 4;
84*cfcc706cSMiquel Raynal 	}
85*cfcc706cSMiquel Raynal 
86*cfcc706cSMiquel Raynal 	/* mop up any remaining bytes */
87*cfcc706cSMiquel Raynal 	if (len) {
88*cfcc706cSMiquel Raynal 		if (len >= 2) {
89*cfcc706cSMiquel Raynal 			*(u16 *)buf = readw(nand);
90*cfcc706cSMiquel Raynal 			buf += 2;
91*cfcc706cSMiquel Raynal 			len -= 2;
92*cfcc706cSMiquel Raynal 		}
93*cfcc706cSMiquel Raynal 
94*cfcc706cSMiquel Raynal 		if (len)
95*cfcc706cSMiquel Raynal 			*buf = readb(nand);
96*cfcc706cSMiquel Raynal 	}
97*cfcc706cSMiquel Raynal }
98*cfcc706cSMiquel Raynal 
nand_davinci_write_buf(struct mtd_info * mtd,const uint8_t * buf,int len)99*cfcc706cSMiquel Raynal static void nand_davinci_write_buf(struct mtd_info *mtd, const uint8_t *buf,
100*cfcc706cSMiquel Raynal 				   int len)
101*cfcc706cSMiquel Raynal {
102*cfcc706cSMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
103*cfcc706cSMiquel Raynal 	const u32 *nand = chip->IO_ADDR_W;
104*cfcc706cSMiquel Raynal 
105*cfcc706cSMiquel Raynal 	/* Make sure that buf is 32 bit aligned */
106*cfcc706cSMiquel Raynal 	if (((int)buf & 0x3) != 0) {
107*cfcc706cSMiquel Raynal 		if (((int)buf & 0x1) != 0) {
108*cfcc706cSMiquel Raynal 			if (len) {
109*cfcc706cSMiquel Raynal 				writeb(*buf, nand);
110*cfcc706cSMiquel Raynal 				buf += 1;
111*cfcc706cSMiquel Raynal 				len--;
112*cfcc706cSMiquel Raynal 			}
113*cfcc706cSMiquel Raynal 		}
114*cfcc706cSMiquel Raynal 
115*cfcc706cSMiquel Raynal 		if (((int)buf & 0x3) != 0) {
116*cfcc706cSMiquel Raynal 			if (len >= 2) {
117*cfcc706cSMiquel Raynal 				writew(*(u16 *)buf, nand);
118*cfcc706cSMiquel Raynal 				buf += 2;
119*cfcc706cSMiquel Raynal 				len -= 2;
120*cfcc706cSMiquel Raynal 			}
121*cfcc706cSMiquel Raynal 		}
122*cfcc706cSMiquel Raynal 	}
123*cfcc706cSMiquel Raynal 
124*cfcc706cSMiquel Raynal 	/* copy aligned data */
125*cfcc706cSMiquel Raynal 	while (len >= 4) {
126*cfcc706cSMiquel Raynal 		__raw_writel(*(u32 *)buf, nand);
127*cfcc706cSMiquel Raynal 		buf += 4;
128*cfcc706cSMiquel Raynal 		len -= 4;
129*cfcc706cSMiquel Raynal 	}
130*cfcc706cSMiquel Raynal 
131*cfcc706cSMiquel Raynal 	/* mop up any remaining bytes */
132*cfcc706cSMiquel Raynal 	if (len) {
133*cfcc706cSMiquel Raynal 		if (len >= 2) {
134*cfcc706cSMiquel Raynal 			writew(*(u16 *)buf, nand);
135*cfcc706cSMiquel Raynal 			buf += 2;
136*cfcc706cSMiquel Raynal 			len -= 2;
137*cfcc706cSMiquel Raynal 		}
138*cfcc706cSMiquel Raynal 
139*cfcc706cSMiquel Raynal 		if (len)
140*cfcc706cSMiquel Raynal 			writeb(*buf, nand);
141*cfcc706cSMiquel Raynal 	}
142*cfcc706cSMiquel Raynal }
143*cfcc706cSMiquel Raynal 
nand_davinci_hwcontrol(struct mtd_info * mtd,int cmd,unsigned int ctrl)144*cfcc706cSMiquel Raynal static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd,
145*cfcc706cSMiquel Raynal 		unsigned int ctrl)
146*cfcc706cSMiquel Raynal {
147*cfcc706cSMiquel Raynal 	struct		nand_chip *this = mtd_to_nand(mtd);
148*cfcc706cSMiquel Raynal 	u_int32_t	IO_ADDR_W = (u_int32_t)this->IO_ADDR_W;
149*cfcc706cSMiquel Raynal 
150*cfcc706cSMiquel Raynal 	if (ctrl & NAND_CTRL_CHANGE) {
151*cfcc706cSMiquel Raynal 		IO_ADDR_W &= ~(MASK_ALE|MASK_CLE);
152*cfcc706cSMiquel Raynal 
153*cfcc706cSMiquel Raynal 		if (ctrl & NAND_CLE)
154*cfcc706cSMiquel Raynal 			IO_ADDR_W |= MASK_CLE;
155*cfcc706cSMiquel Raynal 		if (ctrl & NAND_ALE)
156*cfcc706cSMiquel Raynal 			IO_ADDR_W |= MASK_ALE;
157*cfcc706cSMiquel Raynal 		this->IO_ADDR_W = (void __iomem *) IO_ADDR_W;
158*cfcc706cSMiquel Raynal 	}
159*cfcc706cSMiquel Raynal 
160*cfcc706cSMiquel Raynal 	if (cmd != NAND_CMD_NONE)
161*cfcc706cSMiquel Raynal 		writeb(cmd, IO_ADDR_W);
162*cfcc706cSMiquel Raynal }
163*cfcc706cSMiquel Raynal 
164*cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_HW_ECC
165*cfcc706cSMiquel Raynal 
nand_davinci_readecc(struct mtd_info * mtd)166*cfcc706cSMiquel Raynal static u_int32_t nand_davinci_readecc(struct mtd_info *mtd)
167*cfcc706cSMiquel Raynal {
168*cfcc706cSMiquel Raynal 	u_int32_t	ecc = 0;
169*cfcc706cSMiquel Raynal 
170*cfcc706cSMiquel Raynal 	ecc = __raw_readl(&(davinci_emif_regs->nandfecc[
171*cfcc706cSMiquel Raynal 				CONFIG_SYS_NAND_CS - 2]));
172*cfcc706cSMiquel Raynal 
173*cfcc706cSMiquel Raynal 	return ecc;
174*cfcc706cSMiquel Raynal }
175*cfcc706cSMiquel Raynal 
nand_davinci_enable_hwecc(struct mtd_info * mtd,int mode)176*cfcc706cSMiquel Raynal static void nand_davinci_enable_hwecc(struct mtd_info *mtd, int mode)
177*cfcc706cSMiquel Raynal {
178*cfcc706cSMiquel Raynal 	u_int32_t	val;
179*cfcc706cSMiquel Raynal 
180*cfcc706cSMiquel Raynal 	/* reading the ECC result register resets the ECC calculation */
181*cfcc706cSMiquel Raynal 	nand_davinci_readecc(mtd);
182*cfcc706cSMiquel Raynal 
183*cfcc706cSMiquel Raynal 	val = __raw_readl(&davinci_emif_regs->nandfcr);
184*cfcc706cSMiquel Raynal 	val |= DAVINCI_NANDFCR_NAND_ENABLE(CONFIG_SYS_NAND_CS);
185*cfcc706cSMiquel Raynal 	val |= DAVINCI_NANDFCR_1BIT_ECC_START(CONFIG_SYS_NAND_CS);
186*cfcc706cSMiquel Raynal 	__raw_writel(val, &davinci_emif_regs->nandfcr);
187*cfcc706cSMiquel Raynal }
188*cfcc706cSMiquel Raynal 
nand_davinci_calculate_ecc(struct mtd_info * mtd,const u_char * dat,u_char * ecc_code)189*cfcc706cSMiquel Raynal static int nand_davinci_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
190*cfcc706cSMiquel Raynal 		u_char *ecc_code)
191*cfcc706cSMiquel Raynal {
192*cfcc706cSMiquel Raynal 	u_int32_t		tmp;
193*cfcc706cSMiquel Raynal 
194*cfcc706cSMiquel Raynal 	tmp = nand_davinci_readecc(mtd);
195*cfcc706cSMiquel Raynal 
196*cfcc706cSMiquel Raynal 	/* Squeeze 4 bytes ECC into 3 bytes by removing RESERVED bits
197*cfcc706cSMiquel Raynal 	 * and shifting. RESERVED bits are 31 to 28 and 15 to 12. */
198*cfcc706cSMiquel Raynal 	tmp = (tmp & 0x00000fff) | ((tmp & 0x0fff0000) >> 4);
199*cfcc706cSMiquel Raynal 
200*cfcc706cSMiquel Raynal 	/* Invert so that erased block ECC is correct */
201*cfcc706cSMiquel Raynal 	tmp = ~tmp;
202*cfcc706cSMiquel Raynal 
203*cfcc706cSMiquel Raynal 	*ecc_code++ = tmp;
204*cfcc706cSMiquel Raynal 	*ecc_code++ = tmp >>  8;
205*cfcc706cSMiquel Raynal 	*ecc_code++ = tmp >> 16;
206*cfcc706cSMiquel Raynal 
207*cfcc706cSMiquel Raynal 	/* NOTE:  the above code matches mainline Linux:
208*cfcc706cSMiquel Raynal 	 *	.PQR.stu ==> ~PQRstu
209*cfcc706cSMiquel Raynal 	 *
210*cfcc706cSMiquel Raynal 	 * MontaVista/TI kernels encode those bytes differently, use
211*cfcc706cSMiquel Raynal 	 * complicated (and allegedly sometimes-wrong) correction code,
212*cfcc706cSMiquel Raynal 	 * and usually shipped with U-Boot that uses software ECC:
213*cfcc706cSMiquel Raynal 	 *	.PQR.stu ==> PsQRtu
214*cfcc706cSMiquel Raynal 	 *
215*cfcc706cSMiquel Raynal 	 * If you need MV/TI compatible NAND I/O in U-Boot, it should
216*cfcc706cSMiquel Raynal 	 * be possible to (a) change the mangling above, (b) reverse
217*cfcc706cSMiquel Raynal 	 * that mangling in nand_davinci_correct_data() below.
218*cfcc706cSMiquel Raynal 	 */
219*cfcc706cSMiquel Raynal 
220*cfcc706cSMiquel Raynal 	return 0;
221*cfcc706cSMiquel Raynal }
222*cfcc706cSMiquel Raynal 
nand_davinci_correct_data(struct mtd_info * mtd,u_char * dat,u_char * read_ecc,u_char * calc_ecc)223*cfcc706cSMiquel Raynal static int nand_davinci_correct_data(struct mtd_info *mtd, u_char *dat,
224*cfcc706cSMiquel Raynal 		u_char *read_ecc, u_char *calc_ecc)
225*cfcc706cSMiquel Raynal {
226*cfcc706cSMiquel Raynal 	struct nand_chip *this = mtd_to_nand(mtd);
227*cfcc706cSMiquel Raynal 	u_int32_t ecc_nand = read_ecc[0] | (read_ecc[1] << 8) |
228*cfcc706cSMiquel Raynal 					  (read_ecc[2] << 16);
229*cfcc706cSMiquel Raynal 	u_int32_t ecc_calc = calc_ecc[0] | (calc_ecc[1] << 8) |
230*cfcc706cSMiquel Raynal 					  (calc_ecc[2] << 16);
231*cfcc706cSMiquel Raynal 	u_int32_t diff = ecc_calc ^ ecc_nand;
232*cfcc706cSMiquel Raynal 
233*cfcc706cSMiquel Raynal 	if (diff) {
234*cfcc706cSMiquel Raynal 		if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) {
235*cfcc706cSMiquel Raynal 			/* Correctable error */
236*cfcc706cSMiquel Raynal 			if ((diff >> (12 + 3)) < this->ecc.size) {
237*cfcc706cSMiquel Raynal 				uint8_t find_bit = 1 << ((diff >> 12) & 7);
238*cfcc706cSMiquel Raynal 				uint32_t find_byte = diff >> (12 + 3);
239*cfcc706cSMiquel Raynal 
240*cfcc706cSMiquel Raynal 				dat[find_byte] ^= find_bit;
241*cfcc706cSMiquel Raynal 				pr_debug("Correcting single "
242*cfcc706cSMiquel Raynal 					 "bit ECC error at offset: %d, bit: "
243*cfcc706cSMiquel Raynal 					 "%d\n", find_byte, find_bit);
244*cfcc706cSMiquel Raynal 				return 1;
245*cfcc706cSMiquel Raynal 			} else {
246*cfcc706cSMiquel Raynal 				return -EBADMSG;
247*cfcc706cSMiquel Raynal 			}
248*cfcc706cSMiquel Raynal 		} else if (!(diff & (diff - 1))) {
249*cfcc706cSMiquel Raynal 			/* Single bit ECC error in the ECC itself,
250*cfcc706cSMiquel Raynal 			   nothing to fix */
251*cfcc706cSMiquel Raynal 			pr_debug("Single bit ECC error in " "ECC.\n");
252*cfcc706cSMiquel Raynal 			return 1;
253*cfcc706cSMiquel Raynal 		} else {
254*cfcc706cSMiquel Raynal 			/* Uncorrectable error */
255*cfcc706cSMiquel Raynal 			pr_debug("ECC UNCORRECTED_ERROR 1\n");
256*cfcc706cSMiquel Raynal 			return -EBADMSG;
257*cfcc706cSMiquel Raynal 		}
258*cfcc706cSMiquel Raynal 	}
259*cfcc706cSMiquel Raynal 	return 0;
260*cfcc706cSMiquel Raynal }
261*cfcc706cSMiquel Raynal #endif /* CONFIG_SYS_NAND_HW_ECC */
262*cfcc706cSMiquel Raynal 
263*cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST
264*cfcc706cSMiquel Raynal static struct nand_ecclayout nand_davinci_4bit_layout_oobfirst = {
265*cfcc706cSMiquel Raynal #if defined(CONFIG_SYS_NAND_PAGE_2K)
266*cfcc706cSMiquel Raynal 	.eccbytes = 40,
267*cfcc706cSMiquel Raynal #ifdef CONFIG_NAND_6BYTES_OOB_FREE_10BYTES_ECC
268*cfcc706cSMiquel Raynal 	.eccpos = {
269*cfcc706cSMiquel Raynal 		6,   7,  8,  9, 10,	11, 12, 13, 14, 15,
270*cfcc706cSMiquel Raynal 		22, 23, 24, 25, 26,	27, 28, 29, 30, 31,
271*cfcc706cSMiquel Raynal 		38, 39, 40, 41, 42,	43, 44, 45, 46, 47,
272*cfcc706cSMiquel Raynal 		54, 55, 56, 57, 58,	59, 60, 61, 62, 63,
273*cfcc706cSMiquel Raynal 	},
274*cfcc706cSMiquel Raynal 	.oobfree = {
275*cfcc706cSMiquel Raynal 		{2, 4}, {16, 6}, {32, 6}, {48, 6},
276*cfcc706cSMiquel Raynal 	},
277*cfcc706cSMiquel Raynal #else
278*cfcc706cSMiquel Raynal 	.eccpos = {
279*cfcc706cSMiquel Raynal 		24, 25, 26, 27, 28,
280*cfcc706cSMiquel Raynal 		29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
281*cfcc706cSMiquel Raynal 		39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
282*cfcc706cSMiquel Raynal 		49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
283*cfcc706cSMiquel Raynal 		59, 60, 61, 62, 63,
284*cfcc706cSMiquel Raynal 		},
285*cfcc706cSMiquel Raynal 	.oobfree = {
286*cfcc706cSMiquel Raynal 		{.offset = 2, .length = 22, },
287*cfcc706cSMiquel Raynal 	},
288*cfcc706cSMiquel Raynal #endif	/* #ifdef CONFIG_NAND_6BYTES_OOB_FREE_10BYTES_ECC */
289*cfcc706cSMiquel Raynal #elif defined(CONFIG_SYS_NAND_PAGE_4K)
290*cfcc706cSMiquel Raynal 	.eccbytes = 80,
291*cfcc706cSMiquel Raynal 	.eccpos = {
292*cfcc706cSMiquel Raynal 		48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
293*cfcc706cSMiquel Raynal 		58, 59, 60, 61, 62, 63,	64, 65, 66, 67,
294*cfcc706cSMiquel Raynal 		68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
295*cfcc706cSMiquel Raynal 		78, 79,	80, 81, 82, 83,	84, 85, 86, 87,
296*cfcc706cSMiquel Raynal 		88, 89, 90, 91, 92, 93,	94, 95, 96, 97,
297*cfcc706cSMiquel Raynal 		98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
298*cfcc706cSMiquel Raynal 		108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
299*cfcc706cSMiquel Raynal 		118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
300*cfcc706cSMiquel Raynal 		},
301*cfcc706cSMiquel Raynal 	.oobfree = {
302*cfcc706cSMiquel Raynal 		{.offset = 2, .length = 46, },
303*cfcc706cSMiquel Raynal 	},
304*cfcc706cSMiquel Raynal #endif
305*cfcc706cSMiquel Raynal };
306*cfcc706cSMiquel Raynal 
307*cfcc706cSMiquel Raynal #if defined CONFIG_KEYSTONE_RBL_NAND
308*cfcc706cSMiquel Raynal static struct nand_ecclayout nand_keystone_rbl_4bit_layout_oobfirst = {
309*cfcc706cSMiquel Raynal #if defined(CONFIG_SYS_NAND_PAGE_2K)
310*cfcc706cSMiquel Raynal 	.eccbytes = 40,
311*cfcc706cSMiquel Raynal 	.eccpos = {
312*cfcc706cSMiquel Raynal 		6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
313*cfcc706cSMiquel Raynal 		22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
314*cfcc706cSMiquel Raynal 		38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
315*cfcc706cSMiquel Raynal 		54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
316*cfcc706cSMiquel Raynal 	},
317*cfcc706cSMiquel Raynal 	.oobfree = {
318*cfcc706cSMiquel Raynal 		{.offset = 2, .length = 4, },
319*cfcc706cSMiquel Raynal 		{.offset = 16, .length = 6, },
320*cfcc706cSMiquel Raynal 		{.offset = 32, .length = 6, },
321*cfcc706cSMiquel Raynal 		{.offset = 48, .length = 6, },
322*cfcc706cSMiquel Raynal 	},
323*cfcc706cSMiquel Raynal #elif defined(CONFIG_SYS_NAND_PAGE_4K)
324*cfcc706cSMiquel Raynal 	.eccbytes = 80,
325*cfcc706cSMiquel Raynal 	.eccpos = {
326*cfcc706cSMiquel Raynal 		6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
327*cfcc706cSMiquel Raynal 		22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
328*cfcc706cSMiquel Raynal 		38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
329*cfcc706cSMiquel Raynal 		54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
330*cfcc706cSMiquel Raynal 		70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
331*cfcc706cSMiquel Raynal 		86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
332*cfcc706cSMiquel Raynal 		102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
333*cfcc706cSMiquel Raynal 		118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
334*cfcc706cSMiquel Raynal 	},
335*cfcc706cSMiquel Raynal 	.oobfree = {
336*cfcc706cSMiquel Raynal 		{.offset = 2, .length = 4, },
337*cfcc706cSMiquel Raynal 		{.offset = 16, .length = 6, },
338*cfcc706cSMiquel Raynal 		{.offset = 32, .length = 6, },
339*cfcc706cSMiquel Raynal 		{.offset = 48, .length = 6, },
340*cfcc706cSMiquel Raynal 		{.offset = 64, .length = 6, },
341*cfcc706cSMiquel Raynal 		{.offset = 80, .length = 6, },
342*cfcc706cSMiquel Raynal 		{.offset = 96, .length = 6, },
343*cfcc706cSMiquel Raynal 		{.offset = 112, .length = 6, },
344*cfcc706cSMiquel Raynal 	},
345*cfcc706cSMiquel Raynal #endif
346*cfcc706cSMiquel Raynal };
347*cfcc706cSMiquel Raynal 
348*cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_PAGE_2K
349*cfcc706cSMiquel Raynal #define CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE	CONFIG_KEYSTONE_NAND_MAX_RBL_SIZE >> 11
350*cfcc706cSMiquel Raynal #elif defined(CONFIG_SYS_NAND_PAGE_4K)
351*cfcc706cSMiquel Raynal #define CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE	CONFIG_KEYSTONE_NAND_MAX_RBL_SIZE >> 12
352*cfcc706cSMiquel Raynal #endif
353*cfcc706cSMiquel Raynal 
354*cfcc706cSMiquel Raynal /**
355*cfcc706cSMiquel Raynal  * nand_davinci_write_page - write one page
356*cfcc706cSMiquel Raynal  * @mtd: MTD device structure
357*cfcc706cSMiquel Raynal  * @chip: NAND chip descriptor
358*cfcc706cSMiquel Raynal  * @buf: the data to write
359*cfcc706cSMiquel Raynal  * @oob_required: must write chip->oob_poi to OOB
360*cfcc706cSMiquel Raynal  * @page: page number to write
361*cfcc706cSMiquel Raynal  * @raw: use _raw version of write_page
362*cfcc706cSMiquel Raynal  */
nand_davinci_write_page(struct mtd_info * mtd,struct nand_chip * chip,uint32_t offset,int data_len,const uint8_t * buf,int oob_required,int page,int raw)363*cfcc706cSMiquel Raynal static int nand_davinci_write_page(struct mtd_info *mtd, struct nand_chip *chip,
364*cfcc706cSMiquel Raynal 				   uint32_t offset, int data_len,
365*cfcc706cSMiquel Raynal 				   const uint8_t *buf, int oob_required,
366*cfcc706cSMiquel Raynal 				   int page, int raw)
367*cfcc706cSMiquel Raynal {
368*cfcc706cSMiquel Raynal 	int status;
369*cfcc706cSMiquel Raynal 	int ret = 0;
370*cfcc706cSMiquel Raynal 	struct nand_ecclayout *saved_ecc_layout;
371*cfcc706cSMiquel Raynal 
372*cfcc706cSMiquel Raynal 	/* save current ECC layout and assign Keystone RBL ECC layout */
373*cfcc706cSMiquel Raynal 	if (page < CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE) {
374*cfcc706cSMiquel Raynal 		saved_ecc_layout = chip->ecc.layout;
375*cfcc706cSMiquel Raynal 		chip->ecc.layout = &nand_keystone_rbl_4bit_layout_oobfirst;
376*cfcc706cSMiquel Raynal 		mtd->oobavail = chip->ecc.layout->oobavail;
377*cfcc706cSMiquel Raynal 	}
378*cfcc706cSMiquel Raynal 
379*cfcc706cSMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
380*cfcc706cSMiquel Raynal 
381*cfcc706cSMiquel Raynal 	if (unlikely(raw)) {
382*cfcc706cSMiquel Raynal 		status = chip->ecc.write_page_raw(mtd, chip, buf,
383*cfcc706cSMiquel Raynal 						  oob_required, page);
384*cfcc706cSMiquel Raynal 	} else {
385*cfcc706cSMiquel Raynal 		status = chip->ecc.write_page(mtd, chip, buf,
386*cfcc706cSMiquel Raynal 					      oob_required, page);
387*cfcc706cSMiquel Raynal 	}
388*cfcc706cSMiquel Raynal 
389*cfcc706cSMiquel Raynal 	if (status < 0) {
390*cfcc706cSMiquel Raynal 		ret = status;
391*cfcc706cSMiquel Raynal 		goto err;
392*cfcc706cSMiquel Raynal 	}
393*cfcc706cSMiquel Raynal 
394*cfcc706cSMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
395*cfcc706cSMiquel Raynal 	status = chip->waitfunc(mtd, chip);
396*cfcc706cSMiquel Raynal 
397*cfcc706cSMiquel Raynal 	if (status & NAND_STATUS_FAIL) {
398*cfcc706cSMiquel Raynal 		ret = -EIO;
399*cfcc706cSMiquel Raynal 		goto err;
400*cfcc706cSMiquel Raynal 	}
401*cfcc706cSMiquel Raynal 
402*cfcc706cSMiquel Raynal err:
403*cfcc706cSMiquel Raynal 	/* restore ECC layout */
404*cfcc706cSMiquel Raynal 	if (page < CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE) {
405*cfcc706cSMiquel Raynal 		chip->ecc.layout = saved_ecc_layout;
406*cfcc706cSMiquel Raynal 		mtd->oobavail = saved_ecc_layout->oobavail;
407*cfcc706cSMiquel Raynal 	}
408*cfcc706cSMiquel Raynal 
409*cfcc706cSMiquel Raynal 	return ret;
410*cfcc706cSMiquel Raynal }
411*cfcc706cSMiquel Raynal 
412*cfcc706cSMiquel Raynal /**
413*cfcc706cSMiquel Raynal  * nand_davinci_read_page_hwecc - hardware ECC based page read function
414*cfcc706cSMiquel Raynal  * @mtd: mtd info structure
415*cfcc706cSMiquel Raynal  * @chip: nand chip info structure
416*cfcc706cSMiquel Raynal  * @buf: buffer to store read data
417*cfcc706cSMiquel Raynal  * @oob_required: caller requires OOB data read to chip->oob_poi
418*cfcc706cSMiquel Raynal  * @page: page number to read
419*cfcc706cSMiquel Raynal  *
420*cfcc706cSMiquel Raynal  * Not for syndrome calculating ECC controllers which need a special oob layout.
421*cfcc706cSMiquel Raynal  */
nand_davinci_read_page_hwecc(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)422*cfcc706cSMiquel Raynal static int nand_davinci_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
423*cfcc706cSMiquel Raynal 				uint8_t *buf, int oob_required, int page)
424*cfcc706cSMiquel Raynal {
425*cfcc706cSMiquel Raynal 	int i, eccsize = chip->ecc.size;
426*cfcc706cSMiquel Raynal 	int eccbytes = chip->ecc.bytes;
427*cfcc706cSMiquel Raynal 	int eccsteps = chip->ecc.steps;
428*cfcc706cSMiquel Raynal 	uint32_t *eccpos;
429*cfcc706cSMiquel Raynal 	uint8_t *p = buf;
430*cfcc706cSMiquel Raynal 	uint8_t *ecc_code = chip->buffers->ecccode;
431*cfcc706cSMiquel Raynal 	uint8_t *ecc_calc = chip->buffers->ecccalc;
432*cfcc706cSMiquel Raynal 	struct nand_ecclayout *saved_ecc_layout = chip->ecc.layout;
433*cfcc706cSMiquel Raynal 
434*cfcc706cSMiquel Raynal 	/* save current ECC layout and assign Keystone RBL ECC layout */
435*cfcc706cSMiquel Raynal 	if (page < CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE) {
436*cfcc706cSMiquel Raynal 		chip->ecc.layout = &nand_keystone_rbl_4bit_layout_oobfirst;
437*cfcc706cSMiquel Raynal 		mtd->oobavail = chip->ecc.layout->oobavail;
438*cfcc706cSMiquel Raynal 	}
439*cfcc706cSMiquel Raynal 
440*cfcc706cSMiquel Raynal 	eccpos = chip->ecc.layout->eccpos;
441*cfcc706cSMiquel Raynal 
442*cfcc706cSMiquel Raynal 	/* Read the OOB area first */
443*cfcc706cSMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
444*cfcc706cSMiquel Raynal 	chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
445*cfcc706cSMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
446*cfcc706cSMiquel Raynal 
447*cfcc706cSMiquel Raynal 	for (i = 0; i < chip->ecc.total; i++)
448*cfcc706cSMiquel Raynal 		ecc_code[i] = chip->oob_poi[eccpos[i]];
449*cfcc706cSMiquel Raynal 
450*cfcc706cSMiquel Raynal 	for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
451*cfcc706cSMiquel Raynal 		int stat;
452*cfcc706cSMiquel Raynal 
453*cfcc706cSMiquel Raynal 		chip->ecc.hwctl(mtd, NAND_ECC_READ);
454*cfcc706cSMiquel Raynal 		chip->read_buf(mtd, p, eccsize);
455*cfcc706cSMiquel Raynal 		chip->ecc.calculate(mtd, p, &ecc_calc[i]);
456*cfcc706cSMiquel Raynal 
457*cfcc706cSMiquel Raynal 		stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
458*cfcc706cSMiquel Raynal 		if (stat < 0)
459*cfcc706cSMiquel Raynal 			mtd->ecc_stats.failed++;
460*cfcc706cSMiquel Raynal 		else
461*cfcc706cSMiquel Raynal 			mtd->ecc_stats.corrected += stat;
462*cfcc706cSMiquel Raynal 	}
463*cfcc706cSMiquel Raynal 
464*cfcc706cSMiquel Raynal 	/* restore ECC layout */
465*cfcc706cSMiquel Raynal 	if (page < CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE) {
466*cfcc706cSMiquel Raynal 		chip->ecc.layout = saved_ecc_layout;
467*cfcc706cSMiquel Raynal 		mtd->oobavail = saved_ecc_layout->oobavail;
468*cfcc706cSMiquel Raynal 	}
469*cfcc706cSMiquel Raynal 
470*cfcc706cSMiquel Raynal 	return 0;
471*cfcc706cSMiquel Raynal }
472*cfcc706cSMiquel Raynal #endif /* CONFIG_KEYSTONE_RBL_NAND */
473*cfcc706cSMiquel Raynal 
nand_davinci_4bit_enable_hwecc(struct mtd_info * mtd,int mode)474*cfcc706cSMiquel Raynal static void nand_davinci_4bit_enable_hwecc(struct mtd_info *mtd, int mode)
475*cfcc706cSMiquel Raynal {
476*cfcc706cSMiquel Raynal 	u32 val;
477*cfcc706cSMiquel Raynal 
478*cfcc706cSMiquel Raynal 	switch (mode) {
479*cfcc706cSMiquel Raynal 	case NAND_ECC_WRITE:
480*cfcc706cSMiquel Raynal 	case NAND_ECC_READ:
481*cfcc706cSMiquel Raynal 		/*
482*cfcc706cSMiquel Raynal 		 * Start a new ECC calculation for reading or writing 512 bytes
483*cfcc706cSMiquel Raynal 		 * of data.
484*cfcc706cSMiquel Raynal 		 */
485*cfcc706cSMiquel Raynal 		val = __raw_readl(&davinci_emif_regs->nandfcr);
486*cfcc706cSMiquel Raynal 		val &= ~DAVINCI_NANDFCR_4BIT_ECC_SEL_MASK;
487*cfcc706cSMiquel Raynal 		val |= DAVINCI_NANDFCR_NAND_ENABLE(CONFIG_SYS_NAND_CS);
488*cfcc706cSMiquel Raynal 		val |= DAVINCI_NANDFCR_4BIT_ECC_SEL(CONFIG_SYS_NAND_CS);
489*cfcc706cSMiquel Raynal 		val |= DAVINCI_NANDFCR_4BIT_ECC_START;
490*cfcc706cSMiquel Raynal 		__raw_writel(val, &davinci_emif_regs->nandfcr);
491*cfcc706cSMiquel Raynal 		break;
492*cfcc706cSMiquel Raynal 	case NAND_ECC_READSYN:
493*cfcc706cSMiquel Raynal 		val = __raw_readl(&davinci_emif_regs->nand4bitecc[0]);
494*cfcc706cSMiquel Raynal 		break;
495*cfcc706cSMiquel Raynal 	default:
496*cfcc706cSMiquel Raynal 		break;
497*cfcc706cSMiquel Raynal 	}
498*cfcc706cSMiquel Raynal }
499*cfcc706cSMiquel Raynal 
nand_davinci_4bit_readecc(struct mtd_info * mtd,unsigned int ecc[4])500*cfcc706cSMiquel Raynal static u32 nand_davinci_4bit_readecc(struct mtd_info *mtd, unsigned int ecc[4])
501*cfcc706cSMiquel Raynal {
502*cfcc706cSMiquel Raynal 	int i;
503*cfcc706cSMiquel Raynal 
504*cfcc706cSMiquel Raynal 	for (i = 0; i < 4; i++) {
505*cfcc706cSMiquel Raynal 		ecc[i] = __raw_readl(&davinci_emif_regs->nand4bitecc[i]) &
506*cfcc706cSMiquel Raynal 			NAND_4BITECC_MASK;
507*cfcc706cSMiquel Raynal 	}
508*cfcc706cSMiquel Raynal 
509*cfcc706cSMiquel Raynal 	return 0;
510*cfcc706cSMiquel Raynal }
511*cfcc706cSMiquel Raynal 
nand_davinci_4bit_calculate_ecc(struct mtd_info * mtd,const uint8_t * dat,uint8_t * ecc_code)512*cfcc706cSMiquel Raynal static int nand_davinci_4bit_calculate_ecc(struct mtd_info *mtd,
513*cfcc706cSMiquel Raynal 					   const uint8_t *dat,
514*cfcc706cSMiquel Raynal 					   uint8_t *ecc_code)
515*cfcc706cSMiquel Raynal {
516*cfcc706cSMiquel Raynal 	unsigned int hw_4ecc[4];
517*cfcc706cSMiquel Raynal 	unsigned int i;
518*cfcc706cSMiquel Raynal 
519*cfcc706cSMiquel Raynal 	nand_davinci_4bit_readecc(mtd, hw_4ecc);
520*cfcc706cSMiquel Raynal 
521*cfcc706cSMiquel Raynal 	/*Convert 10 bit ecc value to 8 bit */
522*cfcc706cSMiquel Raynal 	for (i = 0; i < 2; i++) {
523*cfcc706cSMiquel Raynal 		unsigned int hw_ecc_low = hw_4ecc[i * 2];
524*cfcc706cSMiquel Raynal 		unsigned int hw_ecc_hi = hw_4ecc[(i * 2) + 1];
525*cfcc706cSMiquel Raynal 
526*cfcc706cSMiquel Raynal 		/* Take first 8 bits from val1 (count1=0) or val5 (count1=1) */
527*cfcc706cSMiquel Raynal 		*ecc_code++ = hw_ecc_low & 0xFF;
528*cfcc706cSMiquel Raynal 
529*cfcc706cSMiquel Raynal 		/*
530*cfcc706cSMiquel Raynal 		 * Take 2 bits as LSB bits from val1 (count1=0) or val5
531*cfcc706cSMiquel Raynal 		 * (count1=1) and 6 bits from val2 (count1=0) or
532*cfcc706cSMiquel Raynal 		 * val5 (count1=1)
533*cfcc706cSMiquel Raynal 		 */
534*cfcc706cSMiquel Raynal 		*ecc_code++ =
535*cfcc706cSMiquel Raynal 		    ((hw_ecc_low >> 8) & 0x3) | ((hw_ecc_low >> 14) & 0xFC);
536*cfcc706cSMiquel Raynal 
537*cfcc706cSMiquel Raynal 		/*
538*cfcc706cSMiquel Raynal 		 * Take 4 bits from val2 (count1=0) or val5 (count1=1) and
539*cfcc706cSMiquel Raynal 		 * 4 bits from val3 (count1=0) or val6 (count1=1)
540*cfcc706cSMiquel Raynal 		 */
541*cfcc706cSMiquel Raynal 		*ecc_code++ =
542*cfcc706cSMiquel Raynal 		    ((hw_ecc_low >> 22) & 0xF) | ((hw_ecc_hi << 4) & 0xF0);
543*cfcc706cSMiquel Raynal 
544*cfcc706cSMiquel Raynal 		/*
545*cfcc706cSMiquel Raynal 		 * Take 6 bits from val3(count1=0) or val6 (count1=1) and
546*cfcc706cSMiquel Raynal 		 * 2 bits from val4 (count1=0) or  val7 (count1=1)
547*cfcc706cSMiquel Raynal 		 */
548*cfcc706cSMiquel Raynal 		*ecc_code++ =
549*cfcc706cSMiquel Raynal 		    ((hw_ecc_hi >> 4) & 0x3F) | ((hw_ecc_hi >> 10) & 0xC0);
550*cfcc706cSMiquel Raynal 
551*cfcc706cSMiquel Raynal 		/* Take 8 bits from val4 (count1=0) or val7 (count1=1) */
552*cfcc706cSMiquel Raynal 		*ecc_code++ = (hw_ecc_hi >> 18) & 0xFF;
553*cfcc706cSMiquel Raynal 	}
554*cfcc706cSMiquel Raynal 
555*cfcc706cSMiquel Raynal 	return 0;
556*cfcc706cSMiquel Raynal }
557*cfcc706cSMiquel Raynal 
nand_davinci_4bit_correct_data(struct mtd_info * mtd,uint8_t * dat,uint8_t * read_ecc,uint8_t * calc_ecc)558*cfcc706cSMiquel Raynal static int nand_davinci_4bit_correct_data(struct mtd_info *mtd, uint8_t *dat,
559*cfcc706cSMiquel Raynal 					  uint8_t *read_ecc, uint8_t *calc_ecc)
560*cfcc706cSMiquel Raynal {
561*cfcc706cSMiquel Raynal 	int i;
562*cfcc706cSMiquel Raynal 	unsigned int hw_4ecc[4];
563*cfcc706cSMiquel Raynal 	unsigned int iserror;
564*cfcc706cSMiquel Raynal 	unsigned short *ecc16;
565*cfcc706cSMiquel Raynal 	unsigned int numerrors, erroraddress, errorvalue;
566*cfcc706cSMiquel Raynal 	u32 val;
567*cfcc706cSMiquel Raynal 
568*cfcc706cSMiquel Raynal 	/*
569*cfcc706cSMiquel Raynal 	 * Check for an ECC where all bytes are 0xFF.  If this is the case, we
570*cfcc706cSMiquel Raynal 	 * will assume we are looking at an erased page and we should ignore
571*cfcc706cSMiquel Raynal 	 * the ECC.
572*cfcc706cSMiquel Raynal 	 */
573*cfcc706cSMiquel Raynal 	for (i = 0; i < 10; i++) {
574*cfcc706cSMiquel Raynal 		if (read_ecc[i] != 0xFF)
575*cfcc706cSMiquel Raynal 			break;
576*cfcc706cSMiquel Raynal 	}
577*cfcc706cSMiquel Raynal 	if (i == 10)
578*cfcc706cSMiquel Raynal 		return 0;
579*cfcc706cSMiquel Raynal 
580*cfcc706cSMiquel Raynal 	/* Convert 8 bit in to 10 bit */
581*cfcc706cSMiquel Raynal 	ecc16 = (unsigned short *)&read_ecc[0];
582*cfcc706cSMiquel Raynal 
583*cfcc706cSMiquel Raynal 	/*
584*cfcc706cSMiquel Raynal 	 * Write the parity values in the NAND Flash 4-bit ECC Load register.
585*cfcc706cSMiquel Raynal 	 * Write each parity value one at a time starting from 4bit_ecc_val8
586*cfcc706cSMiquel Raynal 	 * to 4bit_ecc_val1.
587*cfcc706cSMiquel Raynal 	 */
588*cfcc706cSMiquel Raynal 
589*cfcc706cSMiquel Raynal 	/*Take 2 bits from 8th byte and 8 bits from 9th byte */
590*cfcc706cSMiquel Raynal 	__raw_writel(((ecc16[4]) >> 6) & 0x3FF,
591*cfcc706cSMiquel Raynal 			&davinci_emif_regs->nand4biteccload);
592*cfcc706cSMiquel Raynal 
593*cfcc706cSMiquel Raynal 	/* Take 4 bits from 7th byte and 6 bits from 8th byte */
594*cfcc706cSMiquel Raynal 	__raw_writel((((ecc16[3]) >> 12) & 0xF) | ((((ecc16[4])) << 4) & 0x3F0),
595*cfcc706cSMiquel Raynal 			&davinci_emif_regs->nand4biteccload);
596*cfcc706cSMiquel Raynal 
597*cfcc706cSMiquel Raynal 	/* Take 6 bits from 6th byte and 4 bits from 7th byte */
598*cfcc706cSMiquel Raynal 	__raw_writel((ecc16[3] >> 2) & 0x3FF,
599*cfcc706cSMiquel Raynal 			&davinci_emif_regs->nand4biteccload);
600*cfcc706cSMiquel Raynal 
601*cfcc706cSMiquel Raynal 	/* Take 8 bits from 5th byte and 2 bits from 6th byte */
602*cfcc706cSMiquel Raynal 	__raw_writel(((ecc16[2]) >> 8) | ((((ecc16[3])) << 8) & 0x300),
603*cfcc706cSMiquel Raynal 			&davinci_emif_regs->nand4biteccload);
604*cfcc706cSMiquel Raynal 
605*cfcc706cSMiquel Raynal 	/*Take 2 bits from 3rd byte and 8 bits from 4th byte */
606*cfcc706cSMiquel Raynal 	__raw_writel((((ecc16[1]) >> 14) & 0x3) | ((((ecc16[2])) << 2) & 0x3FC),
607*cfcc706cSMiquel Raynal 			&davinci_emif_regs->nand4biteccload);
608*cfcc706cSMiquel Raynal 
609*cfcc706cSMiquel Raynal 	/* Take 4 bits form 2nd bytes and 6 bits from 3rd bytes */
610*cfcc706cSMiquel Raynal 	__raw_writel(((ecc16[1]) >> 4) & 0x3FF,
611*cfcc706cSMiquel Raynal 			&davinci_emif_regs->nand4biteccload);
612*cfcc706cSMiquel Raynal 
613*cfcc706cSMiquel Raynal 	/* Take 6 bits from 1st byte and 4 bits from 2nd byte */
614*cfcc706cSMiquel Raynal 	__raw_writel((((ecc16[0]) >> 10) & 0x3F) | (((ecc16[1]) << 6) & 0x3C0),
615*cfcc706cSMiquel Raynal 			&davinci_emif_regs->nand4biteccload);
616*cfcc706cSMiquel Raynal 
617*cfcc706cSMiquel Raynal 	/* Take 10 bits from 0th and 1st bytes */
618*cfcc706cSMiquel Raynal 	__raw_writel((ecc16[0]) & 0x3FF,
619*cfcc706cSMiquel Raynal 			&davinci_emif_regs->nand4biteccload);
620*cfcc706cSMiquel Raynal 
621*cfcc706cSMiquel Raynal 	/*
622*cfcc706cSMiquel Raynal 	 * Perform a dummy read to the EMIF Revision Code and Status register.
623*cfcc706cSMiquel Raynal 	 * This is required to ensure time for syndrome calculation after
624*cfcc706cSMiquel Raynal 	 * writing the ECC values in previous step.
625*cfcc706cSMiquel Raynal 	 */
626*cfcc706cSMiquel Raynal 
627*cfcc706cSMiquel Raynal 	val = __raw_readl(&davinci_emif_regs->nandfsr);
628*cfcc706cSMiquel Raynal 
629*cfcc706cSMiquel Raynal 	/*
630*cfcc706cSMiquel Raynal 	 * Read the syndrome from the NAND Flash 4-Bit ECC 1-4 registers.
631*cfcc706cSMiquel Raynal 	 * A syndrome value of 0 means no bit errors. If the syndrome is
632*cfcc706cSMiquel Raynal 	 * non-zero then go further otherwise return.
633*cfcc706cSMiquel Raynal 	 */
634*cfcc706cSMiquel Raynal 	nand_davinci_4bit_readecc(mtd, hw_4ecc);
635*cfcc706cSMiquel Raynal 
636*cfcc706cSMiquel Raynal 	if (!(hw_4ecc[0] | hw_4ecc[1] | hw_4ecc[2] | hw_4ecc[3]))
637*cfcc706cSMiquel Raynal 		return 0;
638*cfcc706cSMiquel Raynal 
639*cfcc706cSMiquel Raynal 	/*
640*cfcc706cSMiquel Raynal 	 * Clear any previous address calculation by doing a dummy read of an
641*cfcc706cSMiquel Raynal 	 * error address register.
642*cfcc706cSMiquel Raynal 	 */
643*cfcc706cSMiquel Raynal 	val = __raw_readl(&davinci_emif_regs->nanderradd1);
644*cfcc706cSMiquel Raynal 
645*cfcc706cSMiquel Raynal 	/*
646*cfcc706cSMiquel Raynal 	 * Set the addr_calc_st bit(bit no 13) in the NAND Flash Control
647*cfcc706cSMiquel Raynal 	 * register to 1.
648*cfcc706cSMiquel Raynal 	 */
649*cfcc706cSMiquel Raynal 	__raw_writel(DAVINCI_NANDFCR_4BIT_CALC_START,
650*cfcc706cSMiquel Raynal 			&davinci_emif_regs->nandfcr);
651*cfcc706cSMiquel Raynal 
652*cfcc706cSMiquel Raynal 	/*
653*cfcc706cSMiquel Raynal 	 * Wait for the corr_state field (bits 8 to 11) in the
654*cfcc706cSMiquel Raynal 	 * NAND Flash Status register to be not equal to 0x0, 0x1, 0x2, or 0x3.
655*cfcc706cSMiquel Raynal 	 * Otherwise ECC calculation has not even begun and the next loop might
656*cfcc706cSMiquel Raynal 	 * fail because of a false positive!
657*cfcc706cSMiquel Raynal 	 */
658*cfcc706cSMiquel Raynal 	i = NAND_TIMEOUT;
659*cfcc706cSMiquel Raynal 	do {
660*cfcc706cSMiquel Raynal 		val = __raw_readl(&davinci_emif_regs->nandfsr);
661*cfcc706cSMiquel Raynal 		val &= 0xc00;
662*cfcc706cSMiquel Raynal 		i--;
663*cfcc706cSMiquel Raynal 	} while ((i > 0) && !val);
664*cfcc706cSMiquel Raynal 
665*cfcc706cSMiquel Raynal 	/*
666*cfcc706cSMiquel Raynal 	 * Wait for the corr_state field (bits 8 to 11) in the
667*cfcc706cSMiquel Raynal 	 * NAND Flash Status register to be equal to 0x0, 0x1, 0x2, or 0x3.
668*cfcc706cSMiquel Raynal 	 */
669*cfcc706cSMiquel Raynal 	i = NAND_TIMEOUT;
670*cfcc706cSMiquel Raynal 	do {
671*cfcc706cSMiquel Raynal 		val = __raw_readl(&davinci_emif_regs->nandfsr);
672*cfcc706cSMiquel Raynal 		val &= 0xc00;
673*cfcc706cSMiquel Raynal 		i--;
674*cfcc706cSMiquel Raynal 	} while ((i > 0) && val);
675*cfcc706cSMiquel Raynal 
676*cfcc706cSMiquel Raynal 	iserror = __raw_readl(&davinci_emif_regs->nandfsr);
677*cfcc706cSMiquel Raynal 	iserror &= EMIF_NANDFSR_ECC_STATE_MASK;
678*cfcc706cSMiquel Raynal 	iserror = iserror >> 8;
679*cfcc706cSMiquel Raynal 
680*cfcc706cSMiquel Raynal 	/*
681*cfcc706cSMiquel Raynal 	 * ECC_STATE_TOO_MANY_ERRS (0x1) means errors cannot be
682*cfcc706cSMiquel Raynal 	 * corrected (five or more errors).  The number of errors
683*cfcc706cSMiquel Raynal 	 * calculated (err_num field) differs from the number of errors
684*cfcc706cSMiquel Raynal 	 * searched.  ECC_STATE_ERR_CORR_COMP_P (0x2) means error
685*cfcc706cSMiquel Raynal 	 * correction complete (errors on bit 8 or 9).
686*cfcc706cSMiquel Raynal 	 * ECC_STATE_ERR_CORR_COMP_N (0x3) means error correction
687*cfcc706cSMiquel Raynal 	 * complete (error exists).
688*cfcc706cSMiquel Raynal 	 */
689*cfcc706cSMiquel Raynal 
690*cfcc706cSMiquel Raynal 	if (iserror == ECC_STATE_NO_ERR) {
691*cfcc706cSMiquel Raynal 		val = __raw_readl(&davinci_emif_regs->nanderrval1);
692*cfcc706cSMiquel Raynal 		return 0;
693*cfcc706cSMiquel Raynal 	} else if (iserror == ECC_STATE_TOO_MANY_ERRS) {
694*cfcc706cSMiquel Raynal 		val = __raw_readl(&davinci_emif_regs->nanderrval1);
695*cfcc706cSMiquel Raynal 		return -EBADMSG;
696*cfcc706cSMiquel Raynal 	}
697*cfcc706cSMiquel Raynal 
698*cfcc706cSMiquel Raynal 	numerrors = ((__raw_readl(&davinci_emif_regs->nandfsr) >> 16)
699*cfcc706cSMiquel Raynal 			& 0x3) + 1;
700*cfcc706cSMiquel Raynal 
701*cfcc706cSMiquel Raynal 	/* Read the error address, error value and correct */
702*cfcc706cSMiquel Raynal 	for (i = 0; i < numerrors; i++) {
703*cfcc706cSMiquel Raynal 		if (i > 1) {
704*cfcc706cSMiquel Raynal 			erroraddress =
705*cfcc706cSMiquel Raynal 			    ((__raw_readl(&davinci_emif_regs->nanderradd2) >>
706*cfcc706cSMiquel Raynal 			      (16 * (i & 1))) & 0x3FF);
707*cfcc706cSMiquel Raynal 			erroraddress = ((512 + 7) - erroraddress);
708*cfcc706cSMiquel Raynal 			errorvalue =
709*cfcc706cSMiquel Raynal 			    ((__raw_readl(&davinci_emif_regs->nanderrval2) >>
710*cfcc706cSMiquel Raynal 			      (16 * (i & 1))) & 0xFF);
711*cfcc706cSMiquel Raynal 		} else {
712*cfcc706cSMiquel Raynal 			erroraddress =
713*cfcc706cSMiquel Raynal 			    ((__raw_readl(&davinci_emif_regs->nanderradd1) >>
714*cfcc706cSMiquel Raynal 			      (16 * (i & 1))) & 0x3FF);
715*cfcc706cSMiquel Raynal 			erroraddress = ((512 + 7) - erroraddress);
716*cfcc706cSMiquel Raynal 			errorvalue =
717*cfcc706cSMiquel Raynal 			    ((__raw_readl(&davinci_emif_regs->nanderrval1) >>
718*cfcc706cSMiquel Raynal 			      (16 * (i & 1))) & 0xFF);
719*cfcc706cSMiquel Raynal 		}
720*cfcc706cSMiquel Raynal 		/* xor the corrupt data with error value */
721*cfcc706cSMiquel Raynal 		if (erroraddress < 512)
722*cfcc706cSMiquel Raynal 			dat[erroraddress] ^= errorvalue;
723*cfcc706cSMiquel Raynal 	}
724*cfcc706cSMiquel Raynal 
725*cfcc706cSMiquel Raynal 	return numerrors;
726*cfcc706cSMiquel Raynal }
727*cfcc706cSMiquel Raynal #endif /* CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST */
728*cfcc706cSMiquel Raynal 
nand_davinci_dev_ready(struct mtd_info * mtd)729*cfcc706cSMiquel Raynal static int nand_davinci_dev_ready(struct mtd_info *mtd)
730*cfcc706cSMiquel Raynal {
731*cfcc706cSMiquel Raynal 	return __raw_readl(&davinci_emif_regs->nandfsr) & 0x1;
732*cfcc706cSMiquel Raynal }
733*cfcc706cSMiquel Raynal 
davinci_nand_init(struct nand_chip * nand)734*cfcc706cSMiquel Raynal void davinci_nand_init(struct nand_chip *nand)
735*cfcc706cSMiquel Raynal {
736*cfcc706cSMiquel Raynal #if defined CONFIG_KEYSTONE_RBL_NAND
737*cfcc706cSMiquel Raynal 	int i;
738*cfcc706cSMiquel Raynal 	struct nand_ecclayout *layout;
739*cfcc706cSMiquel Raynal 
740*cfcc706cSMiquel Raynal 	layout = &nand_keystone_rbl_4bit_layout_oobfirst;
741*cfcc706cSMiquel Raynal 	layout->oobavail = 0;
742*cfcc706cSMiquel Raynal 	for (i = 0; layout->oobfree[i].length &&
743*cfcc706cSMiquel Raynal 	     i < ARRAY_SIZE(layout->oobfree); i++)
744*cfcc706cSMiquel Raynal 		layout->oobavail += layout->oobfree[i].length;
745*cfcc706cSMiquel Raynal 
746*cfcc706cSMiquel Raynal 	nand->write_page = nand_davinci_write_page;
747*cfcc706cSMiquel Raynal 	nand->ecc.read_page = nand_davinci_read_page_hwecc;
748*cfcc706cSMiquel Raynal #endif
749*cfcc706cSMiquel Raynal 	nand->chip_delay  = 0;
750*cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
751*cfcc706cSMiquel Raynal 	nand->bbt_options	  |= NAND_BBT_USE_FLASH;
752*cfcc706cSMiquel Raynal #endif
753*cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_NO_SUBPAGE_WRITE
754*cfcc706cSMiquel Raynal 	nand->options	  |= NAND_NO_SUBPAGE_WRITE;
755*cfcc706cSMiquel Raynal #endif
756*cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_BUSWIDTH_16BIT
757*cfcc706cSMiquel Raynal 	nand->options	  |= NAND_BUSWIDTH_16;
758*cfcc706cSMiquel Raynal #endif
759*cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_HW_ECC
760*cfcc706cSMiquel Raynal 	nand->ecc.mode = NAND_ECC_HW;
761*cfcc706cSMiquel Raynal 	nand->ecc.size = 512;
762*cfcc706cSMiquel Raynal 	nand->ecc.bytes = 3;
763*cfcc706cSMiquel Raynal 	nand->ecc.strength = 1;
764*cfcc706cSMiquel Raynal 	nand->ecc.calculate = nand_davinci_calculate_ecc;
765*cfcc706cSMiquel Raynal 	nand->ecc.correct  = nand_davinci_correct_data;
766*cfcc706cSMiquel Raynal 	nand->ecc.hwctl  = nand_davinci_enable_hwecc;
767*cfcc706cSMiquel Raynal #else
768*cfcc706cSMiquel Raynal 	nand->ecc.mode = NAND_ECC_SOFT;
769*cfcc706cSMiquel Raynal #endif /* CONFIG_SYS_NAND_HW_ECC */
770*cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST
771*cfcc706cSMiquel Raynal 	nand->ecc.mode = NAND_ECC_HW_OOB_FIRST;
772*cfcc706cSMiquel Raynal 	nand->ecc.size = 512;
773*cfcc706cSMiquel Raynal 	nand->ecc.bytes = 10;
774*cfcc706cSMiquel Raynal 	nand->ecc.strength = 4;
775*cfcc706cSMiquel Raynal 	nand->ecc.calculate = nand_davinci_4bit_calculate_ecc;
776*cfcc706cSMiquel Raynal 	nand->ecc.correct = nand_davinci_4bit_correct_data;
777*cfcc706cSMiquel Raynal 	nand->ecc.hwctl = nand_davinci_4bit_enable_hwecc;
778*cfcc706cSMiquel Raynal 	nand->ecc.layout = &nand_davinci_4bit_layout_oobfirst;
779*cfcc706cSMiquel Raynal #endif
780*cfcc706cSMiquel Raynal 	/* Set address of hardware control function */
781*cfcc706cSMiquel Raynal 	nand->cmd_ctrl = nand_davinci_hwcontrol;
782*cfcc706cSMiquel Raynal 
783*cfcc706cSMiquel Raynal 	nand->read_buf = nand_davinci_read_buf;
784*cfcc706cSMiquel Raynal 	nand->write_buf = nand_davinci_write_buf;
785*cfcc706cSMiquel Raynal 
786*cfcc706cSMiquel Raynal 	nand->dev_ready = nand_davinci_dev_ready;
787*cfcc706cSMiquel Raynal }
788*cfcc706cSMiquel Raynal 
789*cfcc706cSMiquel Raynal int board_nand_init(struct nand_chip *chip) __attribute__((weak));
790*cfcc706cSMiquel Raynal 
board_nand_init(struct nand_chip * chip)791*cfcc706cSMiquel Raynal int board_nand_init(struct nand_chip *chip)
792*cfcc706cSMiquel Raynal {
793*cfcc706cSMiquel Raynal 	davinci_nand_init(chip);
794*cfcc706cSMiquel Raynal 	return 0;
795*cfcc706cSMiquel Raynal }
796