xref: /rk3399_rockchip-uboot/drivers/mtd/nand/raw/omap_gpmc.c (revision cfcc706c901d603707657919484e4f65467be9ff)
1*cfcc706cSMiquel Raynal /*
2*cfcc706cSMiquel Raynal  * (C) Copyright 2004-2008 Texas Instruments, <www.ti.com>
3*cfcc706cSMiquel Raynal  * Rohit Choraria <rohitkc@ti.com>
4*cfcc706cSMiquel Raynal  *
5*cfcc706cSMiquel Raynal  * SPDX-License-Identifier:	GPL-2.0+
6*cfcc706cSMiquel Raynal  */
7*cfcc706cSMiquel Raynal 
8*cfcc706cSMiquel Raynal #include <common.h>
9*cfcc706cSMiquel Raynal #include <asm/io.h>
10*cfcc706cSMiquel Raynal #include <linux/errno.h>
11*cfcc706cSMiquel Raynal #include <asm/arch/mem.h>
12*cfcc706cSMiquel Raynal #include <linux/mtd/omap_gpmc.h>
13*cfcc706cSMiquel Raynal #include <linux/mtd/nand_ecc.h>
14*cfcc706cSMiquel Raynal #include <linux/bch.h>
15*cfcc706cSMiquel Raynal #include <linux/compiler.h>
16*cfcc706cSMiquel Raynal #include <nand.h>
17*cfcc706cSMiquel Raynal #include <linux/mtd/omap_elm.h>
18*cfcc706cSMiquel Raynal 
19*cfcc706cSMiquel Raynal #define BADBLOCK_MARKER_LENGTH	2
20*cfcc706cSMiquel Raynal #define SECTOR_BYTES		512
21*cfcc706cSMiquel Raynal #define ECCCLEAR		(0x1 << 8)
22*cfcc706cSMiquel Raynal #define ECCRESULTREG1		(0x1 << 0)
23*cfcc706cSMiquel Raynal /* 4 bit padding to make byte aligned, 56 = 52 + 4 */
24*cfcc706cSMiquel Raynal #define BCH4_BIT_PAD		4
25*cfcc706cSMiquel Raynal 
26*cfcc706cSMiquel Raynal #ifdef CONFIG_BCH
27*cfcc706cSMiquel Raynal static u8  bch8_polynomial[] = {0xef, 0x51, 0x2e, 0x09, 0xed, 0x93, 0x9a, 0xc2,
28*cfcc706cSMiquel Raynal 				0x97, 0x79, 0xe5, 0x24, 0xb5};
29*cfcc706cSMiquel Raynal #endif
30*cfcc706cSMiquel Raynal static uint8_t cs_next;
31*cfcc706cSMiquel Raynal static __maybe_unused struct nand_ecclayout omap_ecclayout;
32*cfcc706cSMiquel Raynal 
33*cfcc706cSMiquel Raynal #if defined(CONFIG_NAND_OMAP_GPMC_WSCFG)
34*cfcc706cSMiquel Raynal static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE] =
35*cfcc706cSMiquel Raynal 	{ CONFIG_NAND_OMAP_GPMC_WSCFG };
36*cfcc706cSMiquel Raynal #else
37*cfcc706cSMiquel Raynal /* wscfg is preset to zero since its a static variable */
38*cfcc706cSMiquel Raynal static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE];
39*cfcc706cSMiquel Raynal #endif
40*cfcc706cSMiquel Raynal 
41*cfcc706cSMiquel Raynal /*
42*cfcc706cSMiquel Raynal  * Driver configurations
43*cfcc706cSMiquel Raynal  */
44*cfcc706cSMiquel Raynal struct omap_nand_info {
45*cfcc706cSMiquel Raynal 	struct bch_control *control;
46*cfcc706cSMiquel Raynal 	enum omap_ecc ecc_scheme;
47*cfcc706cSMiquel Raynal 	uint8_t cs;
48*cfcc706cSMiquel Raynal 	uint8_t ws;		/* wait status pin (0,1) */
49*cfcc706cSMiquel Raynal };
50*cfcc706cSMiquel Raynal 
51*cfcc706cSMiquel Raynal /* We are wasting a bit of memory but al least we are safe */
52*cfcc706cSMiquel Raynal static struct omap_nand_info omap_nand_info[GPMC_MAX_CS];
53*cfcc706cSMiquel Raynal 
54*cfcc706cSMiquel Raynal /*
55*cfcc706cSMiquel Raynal  * omap_nand_hwcontrol - Set the address pointers corretly for the
56*cfcc706cSMiquel Raynal  *			following address/data/command operation
57*cfcc706cSMiquel Raynal  */
omap_nand_hwcontrol(struct mtd_info * mtd,int32_t cmd,uint32_t ctrl)58*cfcc706cSMiquel Raynal static void omap_nand_hwcontrol(struct mtd_info *mtd, int32_t cmd,
59*cfcc706cSMiquel Raynal 				uint32_t ctrl)
60*cfcc706cSMiquel Raynal {
61*cfcc706cSMiquel Raynal 	register struct nand_chip *this = mtd_to_nand(mtd);
62*cfcc706cSMiquel Raynal 	struct omap_nand_info *info = nand_get_controller_data(this);
63*cfcc706cSMiquel Raynal 	int cs = info->cs;
64*cfcc706cSMiquel Raynal 
65*cfcc706cSMiquel Raynal 	/*
66*cfcc706cSMiquel Raynal 	 * Point the IO_ADDR to DATA and ADDRESS registers instead
67*cfcc706cSMiquel Raynal 	 * of chip address
68*cfcc706cSMiquel Raynal 	 */
69*cfcc706cSMiquel Raynal 	switch (ctrl) {
70*cfcc706cSMiquel Raynal 	case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
71*cfcc706cSMiquel Raynal 		this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
72*cfcc706cSMiquel Raynal 		break;
73*cfcc706cSMiquel Raynal 	case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
74*cfcc706cSMiquel Raynal 		this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_adr;
75*cfcc706cSMiquel Raynal 		break;
76*cfcc706cSMiquel Raynal 	case NAND_CTRL_CHANGE | NAND_NCE:
77*cfcc706cSMiquel Raynal 		this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
78*cfcc706cSMiquel Raynal 		break;
79*cfcc706cSMiquel Raynal 	}
80*cfcc706cSMiquel Raynal 
81*cfcc706cSMiquel Raynal 	if (cmd != NAND_CMD_NONE)
82*cfcc706cSMiquel Raynal 		writeb(cmd, this->IO_ADDR_W);
83*cfcc706cSMiquel Raynal }
84*cfcc706cSMiquel Raynal 
85*cfcc706cSMiquel Raynal /* Check wait pin as dev ready indicator */
omap_dev_ready(struct mtd_info * mtd)86*cfcc706cSMiquel Raynal static int omap_dev_ready(struct mtd_info *mtd)
87*cfcc706cSMiquel Raynal {
88*cfcc706cSMiquel Raynal 	register struct nand_chip *this = mtd_to_nand(mtd);
89*cfcc706cSMiquel Raynal 	struct omap_nand_info *info = nand_get_controller_data(this);
90*cfcc706cSMiquel Raynal 	return gpmc_cfg->status & (1 << (8 + info->ws));
91*cfcc706cSMiquel Raynal }
92*cfcc706cSMiquel Raynal 
93*cfcc706cSMiquel Raynal /*
94*cfcc706cSMiquel Raynal  * gen_true_ecc - This function will generate true ECC value, which
95*cfcc706cSMiquel Raynal  * can be used when correcting data read from NAND flash memory core
96*cfcc706cSMiquel Raynal  *
97*cfcc706cSMiquel Raynal  * @ecc_buf:	buffer to store ecc code
98*cfcc706cSMiquel Raynal  *
99*cfcc706cSMiquel Raynal  * @return:	re-formatted ECC value
100*cfcc706cSMiquel Raynal  */
gen_true_ecc(uint8_t * ecc_buf)101*cfcc706cSMiquel Raynal static uint32_t gen_true_ecc(uint8_t *ecc_buf)
102*cfcc706cSMiquel Raynal {
103*cfcc706cSMiquel Raynal 	return ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xF0) << 20) |
104*cfcc706cSMiquel Raynal 		((ecc_buf[2] & 0x0F) << 8);
105*cfcc706cSMiquel Raynal }
106*cfcc706cSMiquel Raynal 
107*cfcc706cSMiquel Raynal /*
108*cfcc706cSMiquel Raynal  * omap_correct_data - Compares the ecc read from nand spare area with ECC
109*cfcc706cSMiquel Raynal  * registers values and corrects one bit error if it has occurred
110*cfcc706cSMiquel Raynal  * Further details can be had from OMAP TRM and the following selected links:
111*cfcc706cSMiquel Raynal  * http://en.wikipedia.org/wiki/Hamming_code
112*cfcc706cSMiquel Raynal  * http://www.cs.utexas.edu/users/plaxton/c/337/05f/slides/ErrorCorrection-4.pdf
113*cfcc706cSMiquel Raynal  *
114*cfcc706cSMiquel Raynal  * @mtd:		 MTD device structure
115*cfcc706cSMiquel Raynal  * @dat:		 page data
116*cfcc706cSMiquel Raynal  * @read_ecc:		 ecc read from nand flash
117*cfcc706cSMiquel Raynal  * @calc_ecc:		 ecc read from ECC registers
118*cfcc706cSMiquel Raynal  *
119*cfcc706cSMiquel Raynal  * @return 0 if data is OK or corrected, else returns -1
120*cfcc706cSMiquel Raynal  */
omap_correct_data(struct mtd_info * mtd,uint8_t * dat,uint8_t * read_ecc,uint8_t * calc_ecc)121*cfcc706cSMiquel Raynal static int __maybe_unused omap_correct_data(struct mtd_info *mtd, uint8_t *dat,
122*cfcc706cSMiquel Raynal 				uint8_t *read_ecc, uint8_t *calc_ecc)
123*cfcc706cSMiquel Raynal {
124*cfcc706cSMiquel Raynal 	uint32_t orig_ecc, new_ecc, res, hm;
125*cfcc706cSMiquel Raynal 	uint16_t parity_bits, byte;
126*cfcc706cSMiquel Raynal 	uint8_t bit;
127*cfcc706cSMiquel Raynal 
128*cfcc706cSMiquel Raynal 	/* Regenerate the orginal ECC */
129*cfcc706cSMiquel Raynal 	orig_ecc = gen_true_ecc(read_ecc);
130*cfcc706cSMiquel Raynal 	new_ecc = gen_true_ecc(calc_ecc);
131*cfcc706cSMiquel Raynal 	/* Get the XOR of real ecc */
132*cfcc706cSMiquel Raynal 	res = orig_ecc ^ new_ecc;
133*cfcc706cSMiquel Raynal 	if (res) {
134*cfcc706cSMiquel Raynal 		/* Get the hamming width */
135*cfcc706cSMiquel Raynal 		hm = hweight32(res);
136*cfcc706cSMiquel Raynal 		/* Single bit errors can be corrected! */
137*cfcc706cSMiquel Raynal 		if (hm == 12) {
138*cfcc706cSMiquel Raynal 			/* Correctable data! */
139*cfcc706cSMiquel Raynal 			parity_bits = res >> 16;
140*cfcc706cSMiquel Raynal 			bit = (parity_bits & 0x7);
141*cfcc706cSMiquel Raynal 			byte = (parity_bits >> 3) & 0x1FF;
142*cfcc706cSMiquel Raynal 			/* Flip the bit to correct */
143*cfcc706cSMiquel Raynal 			dat[byte] ^= (0x1 << bit);
144*cfcc706cSMiquel Raynal 		} else if (hm == 1) {
145*cfcc706cSMiquel Raynal 			printf("Error: Ecc is wrong\n");
146*cfcc706cSMiquel Raynal 			/* ECC itself is corrupted */
147*cfcc706cSMiquel Raynal 			return 2;
148*cfcc706cSMiquel Raynal 		} else {
149*cfcc706cSMiquel Raynal 			/*
150*cfcc706cSMiquel Raynal 			 * hm distance != parity pairs OR one, could mean 2 bit
151*cfcc706cSMiquel Raynal 			 * error OR potentially be on a blank page..
152*cfcc706cSMiquel Raynal 			 * orig_ecc: contains spare area data from nand flash.
153*cfcc706cSMiquel Raynal 			 * new_ecc: generated ecc while reading data area.
154*cfcc706cSMiquel Raynal 			 * Note: if the ecc = 0, all data bits from which it was
155*cfcc706cSMiquel Raynal 			 * generated are 0xFF.
156*cfcc706cSMiquel Raynal 			 * The 3 byte(24 bits) ecc is generated per 512byte
157*cfcc706cSMiquel Raynal 			 * chunk of a page. If orig_ecc(from spare area)
158*cfcc706cSMiquel Raynal 			 * is 0xFF && new_ecc(computed now from data area)=0x0,
159*cfcc706cSMiquel Raynal 			 * this means that data area is 0xFF and spare area is
160*cfcc706cSMiquel Raynal 			 * 0xFF. A sure sign of a erased page!
161*cfcc706cSMiquel Raynal 			 */
162*cfcc706cSMiquel Raynal 			if ((orig_ecc == 0x0FFF0FFF) && (new_ecc == 0x00000000))
163*cfcc706cSMiquel Raynal 				return 0;
164*cfcc706cSMiquel Raynal 			printf("Error: Bad compare! failed\n");
165*cfcc706cSMiquel Raynal 			/* detected 2 bit error */
166*cfcc706cSMiquel Raynal 			return -EBADMSG;
167*cfcc706cSMiquel Raynal 		}
168*cfcc706cSMiquel Raynal 	}
169*cfcc706cSMiquel Raynal 	return 0;
170*cfcc706cSMiquel Raynal }
171*cfcc706cSMiquel Raynal 
172*cfcc706cSMiquel Raynal /*
173*cfcc706cSMiquel Raynal  * omap_enable_hwecc - configures GPMC as per ECC scheme before read/write
174*cfcc706cSMiquel Raynal  * @mtd:	MTD device structure
175*cfcc706cSMiquel Raynal  * @mode:	Read/Write mode
176*cfcc706cSMiquel Raynal  */
177*cfcc706cSMiquel Raynal __maybe_unused
omap_enable_hwecc(struct mtd_info * mtd,int32_t mode)178*cfcc706cSMiquel Raynal static void omap_enable_hwecc(struct mtd_info *mtd, int32_t mode)
179*cfcc706cSMiquel Raynal {
180*cfcc706cSMiquel Raynal 	struct nand_chip	*nand	= mtd_to_nand(mtd);
181*cfcc706cSMiquel Raynal 	struct omap_nand_info	*info	= nand_get_controller_data(nand);
182*cfcc706cSMiquel Raynal 	unsigned int dev_width = (nand->options & NAND_BUSWIDTH_16) ? 1 : 0;
183*cfcc706cSMiquel Raynal 	unsigned int ecc_algo = 0;
184*cfcc706cSMiquel Raynal 	unsigned int bch_type = 0;
185*cfcc706cSMiquel Raynal 	unsigned int eccsize1 = 0x00, eccsize0 = 0x00, bch_wrapmode = 0x00;
186*cfcc706cSMiquel Raynal 	u32 ecc_size_config_val = 0;
187*cfcc706cSMiquel Raynal 	u32 ecc_config_val = 0;
188*cfcc706cSMiquel Raynal 	int cs = info->cs;
189*cfcc706cSMiquel Raynal 
190*cfcc706cSMiquel Raynal 	/* configure GPMC for specific ecc-scheme */
191*cfcc706cSMiquel Raynal 	switch (info->ecc_scheme) {
192*cfcc706cSMiquel Raynal 	case OMAP_ECC_HAM1_CODE_SW:
193*cfcc706cSMiquel Raynal 		return;
194*cfcc706cSMiquel Raynal 	case OMAP_ECC_HAM1_CODE_HW:
195*cfcc706cSMiquel Raynal 		ecc_algo = 0x0;
196*cfcc706cSMiquel Raynal 		bch_type = 0x0;
197*cfcc706cSMiquel Raynal 		bch_wrapmode = 0x00;
198*cfcc706cSMiquel Raynal 		eccsize0 = 0xFF;
199*cfcc706cSMiquel Raynal 		eccsize1 = 0xFF;
200*cfcc706cSMiquel Raynal 		break;
201*cfcc706cSMiquel Raynal 	case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
202*cfcc706cSMiquel Raynal 	case OMAP_ECC_BCH8_CODE_HW:
203*cfcc706cSMiquel Raynal 		ecc_algo = 0x1;
204*cfcc706cSMiquel Raynal 		bch_type = 0x1;
205*cfcc706cSMiquel Raynal 		if (mode == NAND_ECC_WRITE) {
206*cfcc706cSMiquel Raynal 			bch_wrapmode = 0x01;
207*cfcc706cSMiquel Raynal 			eccsize0 = 0;  /* extra bits in nibbles per sector */
208*cfcc706cSMiquel Raynal 			eccsize1 = 28; /* OOB bits in nibbles per sector */
209*cfcc706cSMiquel Raynal 		} else {
210*cfcc706cSMiquel Raynal 			bch_wrapmode = 0x01;
211*cfcc706cSMiquel Raynal 			eccsize0 = 26; /* ECC bits in nibbles per sector */
212*cfcc706cSMiquel Raynal 			eccsize1 = 2;  /* non-ECC bits in nibbles per sector */
213*cfcc706cSMiquel Raynal 		}
214*cfcc706cSMiquel Raynal 		break;
215*cfcc706cSMiquel Raynal 	case OMAP_ECC_BCH16_CODE_HW:
216*cfcc706cSMiquel Raynal 		ecc_algo = 0x1;
217*cfcc706cSMiquel Raynal 		bch_type = 0x2;
218*cfcc706cSMiquel Raynal 		if (mode == NAND_ECC_WRITE) {
219*cfcc706cSMiquel Raynal 			bch_wrapmode = 0x01;
220*cfcc706cSMiquel Raynal 			eccsize0 = 0;  /* extra bits in nibbles per sector */
221*cfcc706cSMiquel Raynal 			eccsize1 = 52; /* OOB bits in nibbles per sector */
222*cfcc706cSMiquel Raynal 		} else {
223*cfcc706cSMiquel Raynal 			bch_wrapmode = 0x01;
224*cfcc706cSMiquel Raynal 			eccsize0 = 52; /* ECC bits in nibbles per sector */
225*cfcc706cSMiquel Raynal 			eccsize1 = 0;  /* non-ECC bits in nibbles per sector */
226*cfcc706cSMiquel Raynal 		}
227*cfcc706cSMiquel Raynal 		break;
228*cfcc706cSMiquel Raynal 	default:
229*cfcc706cSMiquel Raynal 		return;
230*cfcc706cSMiquel Raynal 	}
231*cfcc706cSMiquel Raynal 	/* Clear ecc and enable bits */
232*cfcc706cSMiquel Raynal 	writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
233*cfcc706cSMiquel Raynal 	/* Configure ecc size for BCH */
234*cfcc706cSMiquel Raynal 	ecc_size_config_val = (eccsize1 << 22) | (eccsize0 << 12);
235*cfcc706cSMiquel Raynal 	writel(ecc_size_config_val, &gpmc_cfg->ecc_size_config);
236*cfcc706cSMiquel Raynal 
237*cfcc706cSMiquel Raynal 	/* Configure device details for BCH engine */
238*cfcc706cSMiquel Raynal 	ecc_config_val = ((ecc_algo << 16)	| /* HAM1 | BCHx */
239*cfcc706cSMiquel Raynal 			(bch_type << 12)	| /* BCH4/BCH8/BCH16 */
240*cfcc706cSMiquel Raynal 			(bch_wrapmode << 8)	| /* wrap mode */
241*cfcc706cSMiquel Raynal 			(dev_width << 7)	| /* bus width */
242*cfcc706cSMiquel Raynal 			(0x0 << 4)		| /* number of sectors */
243*cfcc706cSMiquel Raynal 			(cs <<  1)		| /* ECC CS */
244*cfcc706cSMiquel Raynal 			(0x1));			  /* enable ECC */
245*cfcc706cSMiquel Raynal 	writel(ecc_config_val, &gpmc_cfg->ecc_config);
246*cfcc706cSMiquel Raynal }
247*cfcc706cSMiquel Raynal 
248*cfcc706cSMiquel Raynal /*
249*cfcc706cSMiquel Raynal  *  omap_calculate_ecc - Read ECC result
250*cfcc706cSMiquel Raynal  *  @mtd:	MTD structure
251*cfcc706cSMiquel Raynal  *  @dat:	unused
252*cfcc706cSMiquel Raynal  *  @ecc_code:	ecc_code buffer
253*cfcc706cSMiquel Raynal  *  Using noninverted ECC can be considered ugly since writing a blank
254*cfcc706cSMiquel Raynal  *  page ie. padding will clear the ECC bytes. This is no problem as
255*cfcc706cSMiquel Raynal  *  long nobody is trying to write data on the seemingly unused page.
256*cfcc706cSMiquel Raynal  *  Reading an erased page will produce an ECC mismatch between
257*cfcc706cSMiquel Raynal  *  generated and read ECC bytes that has to be dealt with separately.
258*cfcc706cSMiquel Raynal  *  E.g. if page is 0xFF (fresh erased), and if HW ECC engine within GPMC
259*cfcc706cSMiquel Raynal  *  is used, the result of read will be 0x0 while the ECC offsets of the
260*cfcc706cSMiquel Raynal  *  spare area will be 0xFF which will result in an ECC mismatch.
261*cfcc706cSMiquel Raynal  */
omap_calculate_ecc(struct mtd_info * mtd,const uint8_t * dat,uint8_t * ecc_code)262*cfcc706cSMiquel Raynal static int omap_calculate_ecc(struct mtd_info *mtd, const uint8_t *dat,
263*cfcc706cSMiquel Raynal 				uint8_t *ecc_code)
264*cfcc706cSMiquel Raynal {
265*cfcc706cSMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
266*cfcc706cSMiquel Raynal 	struct omap_nand_info *info = nand_get_controller_data(chip);
267*cfcc706cSMiquel Raynal 	const uint32_t *ptr;
268*cfcc706cSMiquel Raynal 	uint32_t val = 0;
269*cfcc706cSMiquel Raynal 	int8_t i = 0, j;
270*cfcc706cSMiquel Raynal 
271*cfcc706cSMiquel Raynal 	switch (info->ecc_scheme) {
272*cfcc706cSMiquel Raynal 	case OMAP_ECC_HAM1_CODE_HW:
273*cfcc706cSMiquel Raynal 		val = readl(&gpmc_cfg->ecc1_result);
274*cfcc706cSMiquel Raynal 		ecc_code[0] = val & 0xFF;
275*cfcc706cSMiquel Raynal 		ecc_code[1] = (val >> 16) & 0xFF;
276*cfcc706cSMiquel Raynal 		ecc_code[2] = ((val >> 8) & 0x0F) | ((val >> 20) & 0xF0);
277*cfcc706cSMiquel Raynal 		break;
278*cfcc706cSMiquel Raynal #ifdef CONFIG_BCH
279*cfcc706cSMiquel Raynal 	case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
280*cfcc706cSMiquel Raynal #endif
281*cfcc706cSMiquel Raynal 	case OMAP_ECC_BCH8_CODE_HW:
282*cfcc706cSMiquel Raynal 		ptr = &gpmc_cfg->bch_result_0_3[0].bch_result_x[3];
283*cfcc706cSMiquel Raynal 		val = readl(ptr);
284*cfcc706cSMiquel Raynal 		ecc_code[i++] = (val >>  0) & 0xFF;
285*cfcc706cSMiquel Raynal 		ptr--;
286*cfcc706cSMiquel Raynal 		for (j = 0; j < 3; j++) {
287*cfcc706cSMiquel Raynal 			val = readl(ptr);
288*cfcc706cSMiquel Raynal 			ecc_code[i++] = (val >> 24) & 0xFF;
289*cfcc706cSMiquel Raynal 			ecc_code[i++] = (val >> 16) & 0xFF;
290*cfcc706cSMiquel Raynal 			ecc_code[i++] = (val >>  8) & 0xFF;
291*cfcc706cSMiquel Raynal 			ecc_code[i++] = (val >>  0) & 0xFF;
292*cfcc706cSMiquel Raynal 			ptr--;
293*cfcc706cSMiquel Raynal 		}
294*cfcc706cSMiquel Raynal 		break;
295*cfcc706cSMiquel Raynal 	case OMAP_ECC_BCH16_CODE_HW:
296*cfcc706cSMiquel Raynal 		val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[2]);
297*cfcc706cSMiquel Raynal 		ecc_code[i++] = (val >>  8) & 0xFF;
298*cfcc706cSMiquel Raynal 		ecc_code[i++] = (val >>  0) & 0xFF;
299*cfcc706cSMiquel Raynal 		val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[1]);
300*cfcc706cSMiquel Raynal 		ecc_code[i++] = (val >> 24) & 0xFF;
301*cfcc706cSMiquel Raynal 		ecc_code[i++] = (val >> 16) & 0xFF;
302*cfcc706cSMiquel Raynal 		ecc_code[i++] = (val >>  8) & 0xFF;
303*cfcc706cSMiquel Raynal 		ecc_code[i++] = (val >>  0) & 0xFF;
304*cfcc706cSMiquel Raynal 		val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[0]);
305*cfcc706cSMiquel Raynal 		ecc_code[i++] = (val >> 24) & 0xFF;
306*cfcc706cSMiquel Raynal 		ecc_code[i++] = (val >> 16) & 0xFF;
307*cfcc706cSMiquel Raynal 		ecc_code[i++] = (val >>  8) & 0xFF;
308*cfcc706cSMiquel Raynal 		ecc_code[i++] = (val >>  0) & 0xFF;
309*cfcc706cSMiquel Raynal 		for (j = 3; j >= 0; j--) {
310*cfcc706cSMiquel Raynal 			val = readl(&gpmc_cfg->bch_result_0_3[0].bch_result_x[j]
311*cfcc706cSMiquel Raynal 									);
312*cfcc706cSMiquel Raynal 			ecc_code[i++] = (val >> 24) & 0xFF;
313*cfcc706cSMiquel Raynal 			ecc_code[i++] = (val >> 16) & 0xFF;
314*cfcc706cSMiquel Raynal 			ecc_code[i++] = (val >>  8) & 0xFF;
315*cfcc706cSMiquel Raynal 			ecc_code[i++] = (val >>  0) & 0xFF;
316*cfcc706cSMiquel Raynal 		}
317*cfcc706cSMiquel Raynal 		break;
318*cfcc706cSMiquel Raynal 	default:
319*cfcc706cSMiquel Raynal 		return -EINVAL;
320*cfcc706cSMiquel Raynal 	}
321*cfcc706cSMiquel Raynal 	/* ECC scheme specific syndrome customizations */
322*cfcc706cSMiquel Raynal 	switch (info->ecc_scheme) {
323*cfcc706cSMiquel Raynal 	case OMAP_ECC_HAM1_CODE_HW:
324*cfcc706cSMiquel Raynal 		break;
325*cfcc706cSMiquel Raynal #ifdef CONFIG_BCH
326*cfcc706cSMiquel Raynal 	case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
327*cfcc706cSMiquel Raynal 
328*cfcc706cSMiquel Raynal 		for (i = 0; i < chip->ecc.bytes; i++)
329*cfcc706cSMiquel Raynal 			*(ecc_code + i) = *(ecc_code + i) ^
330*cfcc706cSMiquel Raynal 						bch8_polynomial[i];
331*cfcc706cSMiquel Raynal 		break;
332*cfcc706cSMiquel Raynal #endif
333*cfcc706cSMiquel Raynal 	case OMAP_ECC_BCH8_CODE_HW:
334*cfcc706cSMiquel Raynal 		ecc_code[chip->ecc.bytes - 1] = 0x00;
335*cfcc706cSMiquel Raynal 		break;
336*cfcc706cSMiquel Raynal 	case OMAP_ECC_BCH16_CODE_HW:
337*cfcc706cSMiquel Raynal 		break;
338*cfcc706cSMiquel Raynal 	default:
339*cfcc706cSMiquel Raynal 		return -EINVAL;
340*cfcc706cSMiquel Raynal 	}
341*cfcc706cSMiquel Raynal 	return 0;
342*cfcc706cSMiquel Raynal }
343*cfcc706cSMiquel Raynal 
344*cfcc706cSMiquel Raynal #ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
345*cfcc706cSMiquel Raynal 
346*cfcc706cSMiquel Raynal #define PREFETCH_CONFIG1_CS_SHIFT	24
347*cfcc706cSMiquel Raynal #define PREFETCH_FIFOTHRESHOLD_MAX	0x40
348*cfcc706cSMiquel Raynal #define PREFETCH_FIFOTHRESHOLD(val)	((val) << 8)
349*cfcc706cSMiquel Raynal #define PREFETCH_STATUS_COUNT(val)	(val & 0x00003fff)
350*cfcc706cSMiquel Raynal #define PREFETCH_STATUS_FIFO_CNT(val)	((val >> 24) & 0x7F)
351*cfcc706cSMiquel Raynal #define ENABLE_PREFETCH			(1 << 7)
352*cfcc706cSMiquel Raynal 
353*cfcc706cSMiquel Raynal /**
354*cfcc706cSMiquel Raynal  * omap_prefetch_enable - configures and starts prefetch transfer
355*cfcc706cSMiquel Raynal  * @fifo_th: fifo threshold to be used for read/ write
356*cfcc706cSMiquel Raynal  * @count: number of bytes to be transferred
357*cfcc706cSMiquel Raynal  * @is_write: prefetch read(0) or write post(1) mode
358*cfcc706cSMiquel Raynal  * @cs: chip select to use
359*cfcc706cSMiquel Raynal  */
omap_prefetch_enable(int fifo_th,unsigned int count,int is_write,int cs)360*cfcc706cSMiquel Raynal static int omap_prefetch_enable(int fifo_th, unsigned int count, int is_write, int cs)
361*cfcc706cSMiquel Raynal {
362*cfcc706cSMiquel Raynal 	uint32_t val;
363*cfcc706cSMiquel Raynal 
364*cfcc706cSMiquel Raynal 	if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX)
365*cfcc706cSMiquel Raynal 		return -EINVAL;
366*cfcc706cSMiquel Raynal 
367*cfcc706cSMiquel Raynal 	if (readl(&gpmc_cfg->prefetch_control))
368*cfcc706cSMiquel Raynal 		return -EBUSY;
369*cfcc706cSMiquel Raynal 
370*cfcc706cSMiquel Raynal 	/* Set the amount of bytes to be prefetched */
371*cfcc706cSMiquel Raynal 	writel(count, &gpmc_cfg->prefetch_config2);
372*cfcc706cSMiquel Raynal 
373*cfcc706cSMiquel Raynal 	val = (cs << PREFETCH_CONFIG1_CS_SHIFT) | (is_write & 1) |
374*cfcc706cSMiquel Raynal 		PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH;
375*cfcc706cSMiquel Raynal 	writel(val, &gpmc_cfg->prefetch_config1);
376*cfcc706cSMiquel Raynal 
377*cfcc706cSMiquel Raynal 	/*  Start the prefetch engine */
378*cfcc706cSMiquel Raynal 	writel(1, &gpmc_cfg->prefetch_control);
379*cfcc706cSMiquel Raynal 
380*cfcc706cSMiquel Raynal 	return 0;
381*cfcc706cSMiquel Raynal }
382*cfcc706cSMiquel Raynal 
383*cfcc706cSMiquel Raynal /**
384*cfcc706cSMiquel Raynal  * omap_prefetch_reset - disables and stops the prefetch engine
385*cfcc706cSMiquel Raynal  */
omap_prefetch_reset(void)386*cfcc706cSMiquel Raynal static void omap_prefetch_reset(void)
387*cfcc706cSMiquel Raynal {
388*cfcc706cSMiquel Raynal 	writel(0, &gpmc_cfg->prefetch_control);
389*cfcc706cSMiquel Raynal 	writel(0, &gpmc_cfg->prefetch_config1);
390*cfcc706cSMiquel Raynal }
391*cfcc706cSMiquel Raynal 
__read_prefetch_aligned(struct nand_chip * chip,uint32_t * buf,int len)392*cfcc706cSMiquel Raynal static int __read_prefetch_aligned(struct nand_chip *chip, uint32_t *buf, int len)
393*cfcc706cSMiquel Raynal {
394*cfcc706cSMiquel Raynal 	int ret;
395*cfcc706cSMiquel Raynal 	uint32_t cnt;
396*cfcc706cSMiquel Raynal 	struct omap_nand_info *info = nand_get_controller_data(chip);
397*cfcc706cSMiquel Raynal 
398*cfcc706cSMiquel Raynal 	ret = omap_prefetch_enable(PREFETCH_FIFOTHRESHOLD_MAX, len, 0, info->cs);
399*cfcc706cSMiquel Raynal 	if (ret < 0)
400*cfcc706cSMiquel Raynal 		return ret;
401*cfcc706cSMiquel Raynal 
402*cfcc706cSMiquel Raynal 	do {
403*cfcc706cSMiquel Raynal 		int i;
404*cfcc706cSMiquel Raynal 
405*cfcc706cSMiquel Raynal 		cnt = readl(&gpmc_cfg->prefetch_status);
406*cfcc706cSMiquel Raynal 		cnt = PREFETCH_STATUS_FIFO_CNT(cnt);
407*cfcc706cSMiquel Raynal 
408*cfcc706cSMiquel Raynal 		for (i = 0; i < cnt / 4; i++) {
409*cfcc706cSMiquel Raynal 			*buf++ = readl(CONFIG_SYS_NAND_BASE);
410*cfcc706cSMiquel Raynal 			len -= 4;
411*cfcc706cSMiquel Raynal 		}
412*cfcc706cSMiquel Raynal 	} while (len);
413*cfcc706cSMiquel Raynal 
414*cfcc706cSMiquel Raynal 	omap_prefetch_reset();
415*cfcc706cSMiquel Raynal 
416*cfcc706cSMiquel Raynal 	return 0;
417*cfcc706cSMiquel Raynal }
418*cfcc706cSMiquel Raynal 
omap_nand_read(struct mtd_info * mtd,uint8_t * buf,int len)419*cfcc706cSMiquel Raynal static inline void omap_nand_read(struct mtd_info *mtd, uint8_t *buf, int len)
420*cfcc706cSMiquel Raynal {
421*cfcc706cSMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
422*cfcc706cSMiquel Raynal 
423*cfcc706cSMiquel Raynal 	if (chip->options & NAND_BUSWIDTH_16)
424*cfcc706cSMiquel Raynal 		nand_read_buf16(mtd, buf, len);
425*cfcc706cSMiquel Raynal 	else
426*cfcc706cSMiquel Raynal 		nand_read_buf(mtd, buf, len);
427*cfcc706cSMiquel Raynal }
428*cfcc706cSMiquel Raynal 
omap_nand_read_prefetch(struct mtd_info * mtd,uint8_t * buf,int len)429*cfcc706cSMiquel Raynal static void omap_nand_read_prefetch(struct mtd_info *mtd, uint8_t *buf, int len)
430*cfcc706cSMiquel Raynal {
431*cfcc706cSMiquel Raynal 	int ret;
432*cfcc706cSMiquel Raynal 	uint32_t head, tail;
433*cfcc706cSMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
434*cfcc706cSMiquel Raynal 
435*cfcc706cSMiquel Raynal 	/*
436*cfcc706cSMiquel Raynal 	 * If the destination buffer is unaligned, start with reading
437*cfcc706cSMiquel Raynal 	 * the overlap byte-wise.
438*cfcc706cSMiquel Raynal 	 */
439*cfcc706cSMiquel Raynal 	head = ((uint32_t) buf) % 4;
440*cfcc706cSMiquel Raynal 	if (head) {
441*cfcc706cSMiquel Raynal 		omap_nand_read(mtd, buf, head);
442*cfcc706cSMiquel Raynal 		buf += head;
443*cfcc706cSMiquel Raynal 		len -= head;
444*cfcc706cSMiquel Raynal 	}
445*cfcc706cSMiquel Raynal 
446*cfcc706cSMiquel Raynal 	/*
447*cfcc706cSMiquel Raynal 	 * Only transfer multiples of 4 bytes in a pre-fetched fashion.
448*cfcc706cSMiquel Raynal 	 * If there's a residue, care for it byte-wise afterwards.
449*cfcc706cSMiquel Raynal 	 */
450*cfcc706cSMiquel Raynal 	tail = len % 4;
451*cfcc706cSMiquel Raynal 
452*cfcc706cSMiquel Raynal 	ret = __read_prefetch_aligned(chip, (uint32_t *)buf, len - tail);
453*cfcc706cSMiquel Raynal 	if (ret < 0) {
454*cfcc706cSMiquel Raynal 		/* fallback in case the prefetch engine is busy */
455*cfcc706cSMiquel Raynal 		omap_nand_read(mtd, buf, len);
456*cfcc706cSMiquel Raynal 	} else if (tail) {
457*cfcc706cSMiquel Raynal 		buf += len - tail;
458*cfcc706cSMiquel Raynal 		omap_nand_read(mtd, buf, tail);
459*cfcc706cSMiquel Raynal 	}
460*cfcc706cSMiquel Raynal }
461*cfcc706cSMiquel Raynal #endif /* CONFIG_NAND_OMAP_GPMC_PREFETCH */
462*cfcc706cSMiquel Raynal 
463*cfcc706cSMiquel Raynal #ifdef CONFIG_NAND_OMAP_ELM
464*cfcc706cSMiquel Raynal /*
465*cfcc706cSMiquel Raynal  * omap_reverse_list - re-orders list elements in reverse order [internal]
466*cfcc706cSMiquel Raynal  * @list:	pointer to start of list
467*cfcc706cSMiquel Raynal  * @length:	length of list
468*cfcc706cSMiquel Raynal */
omap_reverse_list(u8 * list,unsigned int length)469*cfcc706cSMiquel Raynal static void omap_reverse_list(u8 *list, unsigned int length)
470*cfcc706cSMiquel Raynal {
471*cfcc706cSMiquel Raynal 	unsigned int i, j;
472*cfcc706cSMiquel Raynal 	unsigned int half_length = length / 2;
473*cfcc706cSMiquel Raynal 	u8 tmp;
474*cfcc706cSMiquel Raynal 	for (i = 0, j = length - 1; i < half_length; i++, j--) {
475*cfcc706cSMiquel Raynal 		tmp = list[i];
476*cfcc706cSMiquel Raynal 		list[i] = list[j];
477*cfcc706cSMiquel Raynal 		list[j] = tmp;
478*cfcc706cSMiquel Raynal 	}
479*cfcc706cSMiquel Raynal }
480*cfcc706cSMiquel Raynal 
481*cfcc706cSMiquel Raynal /*
482*cfcc706cSMiquel Raynal  * omap_correct_data_bch - Compares the ecc read from nand spare area
483*cfcc706cSMiquel Raynal  * with ECC registers values and corrects one bit error if it has occurred
484*cfcc706cSMiquel Raynal  *
485*cfcc706cSMiquel Raynal  * @mtd:	MTD device structure
486*cfcc706cSMiquel Raynal  * @dat:	page data
487*cfcc706cSMiquel Raynal  * @read_ecc:	ecc read from nand flash (ignored)
488*cfcc706cSMiquel Raynal  * @calc_ecc:	ecc read from ECC registers
489*cfcc706cSMiquel Raynal  *
490*cfcc706cSMiquel Raynal  * @return 0 if data is OK or corrected, else returns -1
491*cfcc706cSMiquel Raynal  */
omap_correct_data_bch(struct mtd_info * mtd,uint8_t * dat,uint8_t * read_ecc,uint8_t * calc_ecc)492*cfcc706cSMiquel Raynal static int omap_correct_data_bch(struct mtd_info *mtd, uint8_t *dat,
493*cfcc706cSMiquel Raynal 				uint8_t *read_ecc, uint8_t *calc_ecc)
494*cfcc706cSMiquel Raynal {
495*cfcc706cSMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
496*cfcc706cSMiquel Raynal 	struct omap_nand_info *info = nand_get_controller_data(chip);
497*cfcc706cSMiquel Raynal 	struct nand_ecc_ctrl *ecc = &chip->ecc;
498*cfcc706cSMiquel Raynal 	uint32_t error_count = 0, error_max;
499*cfcc706cSMiquel Raynal 	uint32_t error_loc[ELM_MAX_ERROR_COUNT];
500*cfcc706cSMiquel Raynal 	enum bch_level bch_type;
501*cfcc706cSMiquel Raynal 	uint32_t i, ecc_flag = 0;
502*cfcc706cSMiquel Raynal 	uint8_t count;
503*cfcc706cSMiquel Raynal 	uint32_t byte_pos, bit_pos;
504*cfcc706cSMiquel Raynal 	int err = 0;
505*cfcc706cSMiquel Raynal 
506*cfcc706cSMiquel Raynal 	/* check calculated ecc */
507*cfcc706cSMiquel Raynal 	for (i = 0; i < ecc->bytes && !ecc_flag; i++) {
508*cfcc706cSMiquel Raynal 		if (calc_ecc[i] != 0x00)
509*cfcc706cSMiquel Raynal 			ecc_flag = 1;
510*cfcc706cSMiquel Raynal 	}
511*cfcc706cSMiquel Raynal 	if (!ecc_flag)
512*cfcc706cSMiquel Raynal 		return 0;
513*cfcc706cSMiquel Raynal 
514*cfcc706cSMiquel Raynal 	/* check for whether its a erased-page */
515*cfcc706cSMiquel Raynal 	ecc_flag = 0;
516*cfcc706cSMiquel Raynal 	for (i = 0; i < ecc->bytes && !ecc_flag; i++) {
517*cfcc706cSMiquel Raynal 		if (read_ecc[i] != 0xff)
518*cfcc706cSMiquel Raynal 			ecc_flag = 1;
519*cfcc706cSMiquel Raynal 	}
520*cfcc706cSMiquel Raynal 	if (!ecc_flag)
521*cfcc706cSMiquel Raynal 		return 0;
522*cfcc706cSMiquel Raynal 
523*cfcc706cSMiquel Raynal 	/*
524*cfcc706cSMiquel Raynal 	 * while reading ECC result we read it in big endian.
525*cfcc706cSMiquel Raynal 	 * Hence while loading to ELM we have rotate to get the right endian.
526*cfcc706cSMiquel Raynal 	 */
527*cfcc706cSMiquel Raynal 	switch (info->ecc_scheme) {
528*cfcc706cSMiquel Raynal 	case OMAP_ECC_BCH8_CODE_HW:
529*cfcc706cSMiquel Raynal 		bch_type = BCH_8_BIT;
530*cfcc706cSMiquel Raynal 		omap_reverse_list(calc_ecc, ecc->bytes - 1);
531*cfcc706cSMiquel Raynal 		break;
532*cfcc706cSMiquel Raynal 	case OMAP_ECC_BCH16_CODE_HW:
533*cfcc706cSMiquel Raynal 		bch_type = BCH_16_BIT;
534*cfcc706cSMiquel Raynal 		omap_reverse_list(calc_ecc, ecc->bytes);
535*cfcc706cSMiquel Raynal 		break;
536*cfcc706cSMiquel Raynal 	default:
537*cfcc706cSMiquel Raynal 		return -EINVAL;
538*cfcc706cSMiquel Raynal 	}
539*cfcc706cSMiquel Raynal 	/* use elm module to check for errors */
540*cfcc706cSMiquel Raynal 	elm_config(bch_type);
541*cfcc706cSMiquel Raynal 	err = elm_check_error(calc_ecc, bch_type, &error_count, error_loc);
542*cfcc706cSMiquel Raynal 	if (err)
543*cfcc706cSMiquel Raynal 		return err;
544*cfcc706cSMiquel Raynal 
545*cfcc706cSMiquel Raynal 	/* correct bch error */
546*cfcc706cSMiquel Raynal 	for (count = 0; count < error_count; count++) {
547*cfcc706cSMiquel Raynal 		switch (info->ecc_scheme) {
548*cfcc706cSMiquel Raynal 		case OMAP_ECC_BCH8_CODE_HW:
549*cfcc706cSMiquel Raynal 			/* 14th byte in ECC is reserved to match ROM layout */
550*cfcc706cSMiquel Raynal 			error_max = SECTOR_BYTES + (ecc->bytes - 1);
551*cfcc706cSMiquel Raynal 			break;
552*cfcc706cSMiquel Raynal 		case OMAP_ECC_BCH16_CODE_HW:
553*cfcc706cSMiquel Raynal 			error_max = SECTOR_BYTES + ecc->bytes;
554*cfcc706cSMiquel Raynal 			break;
555*cfcc706cSMiquel Raynal 		default:
556*cfcc706cSMiquel Raynal 			return -EINVAL;
557*cfcc706cSMiquel Raynal 		}
558*cfcc706cSMiquel Raynal 		byte_pos = error_max - (error_loc[count] / 8) - 1;
559*cfcc706cSMiquel Raynal 		bit_pos  = error_loc[count] % 8;
560*cfcc706cSMiquel Raynal 		if (byte_pos < SECTOR_BYTES) {
561*cfcc706cSMiquel Raynal 			dat[byte_pos] ^= 1 << bit_pos;
562*cfcc706cSMiquel Raynal 			debug("nand: bit-flip corrected @data=%d\n", byte_pos);
563*cfcc706cSMiquel Raynal 		} else if (byte_pos < error_max) {
564*cfcc706cSMiquel Raynal 			read_ecc[byte_pos - SECTOR_BYTES] ^= 1 << bit_pos;
565*cfcc706cSMiquel Raynal 			debug("nand: bit-flip corrected @oob=%d\n", byte_pos -
566*cfcc706cSMiquel Raynal 								SECTOR_BYTES);
567*cfcc706cSMiquel Raynal 		} else {
568*cfcc706cSMiquel Raynal 			err = -EBADMSG;
569*cfcc706cSMiquel Raynal 			printf("nand: error: invalid bit-flip location\n");
570*cfcc706cSMiquel Raynal 		}
571*cfcc706cSMiquel Raynal 	}
572*cfcc706cSMiquel Raynal 	return (err) ? err : error_count;
573*cfcc706cSMiquel Raynal }
574*cfcc706cSMiquel Raynal 
575*cfcc706cSMiquel Raynal /**
576*cfcc706cSMiquel Raynal  * omap_read_page_bch - hardware ecc based page read function
577*cfcc706cSMiquel Raynal  * @mtd:	mtd info structure
578*cfcc706cSMiquel Raynal  * @chip:	nand chip info structure
579*cfcc706cSMiquel Raynal  * @buf:	buffer to store read data
580*cfcc706cSMiquel Raynal  * @oob_required: caller expects OOB data read to chip->oob_poi
581*cfcc706cSMiquel Raynal  * @page:	page number to read
582*cfcc706cSMiquel Raynal  *
583*cfcc706cSMiquel Raynal  */
omap_read_page_bch(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)584*cfcc706cSMiquel Raynal static int omap_read_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
585*cfcc706cSMiquel Raynal 				uint8_t *buf, int oob_required, int page)
586*cfcc706cSMiquel Raynal {
587*cfcc706cSMiquel Raynal 	int i, eccsize = chip->ecc.size;
588*cfcc706cSMiquel Raynal 	int eccbytes = chip->ecc.bytes;
589*cfcc706cSMiquel Raynal 	int eccsteps = chip->ecc.steps;
590*cfcc706cSMiquel Raynal 	uint8_t *p = buf;
591*cfcc706cSMiquel Raynal 	uint8_t *ecc_calc = chip->buffers->ecccalc;
592*cfcc706cSMiquel Raynal 	uint8_t *ecc_code = chip->buffers->ecccode;
593*cfcc706cSMiquel Raynal 	uint32_t *eccpos = chip->ecc.layout->eccpos;
594*cfcc706cSMiquel Raynal 	uint8_t *oob = chip->oob_poi;
595*cfcc706cSMiquel Raynal 	uint32_t data_pos;
596*cfcc706cSMiquel Raynal 	uint32_t oob_pos;
597*cfcc706cSMiquel Raynal 
598*cfcc706cSMiquel Raynal 	data_pos = 0;
599*cfcc706cSMiquel Raynal 	/* oob area start */
600*cfcc706cSMiquel Raynal 	oob_pos = (eccsize * eccsteps) + chip->ecc.layout->eccpos[0];
601*cfcc706cSMiquel Raynal 	oob += chip->ecc.layout->eccpos[0];
602*cfcc706cSMiquel Raynal 
603*cfcc706cSMiquel Raynal 	for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize,
604*cfcc706cSMiquel Raynal 				oob += eccbytes) {
605*cfcc706cSMiquel Raynal 		chip->ecc.hwctl(mtd, NAND_ECC_READ);
606*cfcc706cSMiquel Raynal 		/* read data */
607*cfcc706cSMiquel Raynal 		chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_pos, -1);
608*cfcc706cSMiquel Raynal 		chip->read_buf(mtd, p, eccsize);
609*cfcc706cSMiquel Raynal 
610*cfcc706cSMiquel Raynal 		/* read respective ecc from oob area */
611*cfcc706cSMiquel Raynal 		chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1);
612*cfcc706cSMiquel Raynal 		chip->read_buf(mtd, oob, eccbytes);
613*cfcc706cSMiquel Raynal 		/* read syndrome */
614*cfcc706cSMiquel Raynal 		chip->ecc.calculate(mtd, p, &ecc_calc[i]);
615*cfcc706cSMiquel Raynal 
616*cfcc706cSMiquel Raynal 		data_pos += eccsize;
617*cfcc706cSMiquel Raynal 		oob_pos += eccbytes;
618*cfcc706cSMiquel Raynal 	}
619*cfcc706cSMiquel Raynal 
620*cfcc706cSMiquel Raynal 	for (i = 0; i < chip->ecc.total; i++)
621*cfcc706cSMiquel Raynal 		ecc_code[i] = chip->oob_poi[eccpos[i]];
622*cfcc706cSMiquel Raynal 
623*cfcc706cSMiquel Raynal 	eccsteps = chip->ecc.steps;
624*cfcc706cSMiquel Raynal 	p = buf;
625*cfcc706cSMiquel Raynal 
626*cfcc706cSMiquel Raynal 	for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
627*cfcc706cSMiquel Raynal 		int stat;
628*cfcc706cSMiquel Raynal 
629*cfcc706cSMiquel Raynal 		stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
630*cfcc706cSMiquel Raynal 		if (stat < 0)
631*cfcc706cSMiquel Raynal 			mtd->ecc_stats.failed++;
632*cfcc706cSMiquel Raynal 		else
633*cfcc706cSMiquel Raynal 			mtd->ecc_stats.corrected += stat;
634*cfcc706cSMiquel Raynal 	}
635*cfcc706cSMiquel Raynal 	return 0;
636*cfcc706cSMiquel Raynal }
637*cfcc706cSMiquel Raynal #endif /* CONFIG_NAND_OMAP_ELM */
638*cfcc706cSMiquel Raynal 
639*cfcc706cSMiquel Raynal /*
640*cfcc706cSMiquel Raynal  * OMAP3 BCH8 support (with BCH library)
641*cfcc706cSMiquel Raynal  */
642*cfcc706cSMiquel Raynal #ifdef CONFIG_BCH
643*cfcc706cSMiquel Raynal /**
644*cfcc706cSMiquel Raynal  * omap_correct_data_bch_sw - Decode received data and correct errors
645*cfcc706cSMiquel Raynal  * @mtd: MTD device structure
646*cfcc706cSMiquel Raynal  * @data: page data
647*cfcc706cSMiquel Raynal  * @read_ecc: ecc read from nand flash
648*cfcc706cSMiquel Raynal  * @calc_ecc: ecc read from HW ECC registers
649*cfcc706cSMiquel Raynal  */
omap_correct_data_bch_sw(struct mtd_info * mtd,u_char * data,u_char * read_ecc,u_char * calc_ecc)650*cfcc706cSMiquel Raynal static int omap_correct_data_bch_sw(struct mtd_info *mtd, u_char *data,
651*cfcc706cSMiquel Raynal 				 u_char *read_ecc, u_char *calc_ecc)
652*cfcc706cSMiquel Raynal {
653*cfcc706cSMiquel Raynal 	int i, count;
654*cfcc706cSMiquel Raynal 	/* cannot correct more than 8 errors */
655*cfcc706cSMiquel Raynal 	unsigned int errloc[8];
656*cfcc706cSMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
657*cfcc706cSMiquel Raynal 	struct omap_nand_info *info = nand_get_controller_data(chip);
658*cfcc706cSMiquel Raynal 
659*cfcc706cSMiquel Raynal 	count = decode_bch(info->control, NULL, SECTOR_BYTES,
660*cfcc706cSMiquel Raynal 				read_ecc, calc_ecc, NULL, errloc);
661*cfcc706cSMiquel Raynal 	if (count > 0) {
662*cfcc706cSMiquel Raynal 		/* correct errors */
663*cfcc706cSMiquel Raynal 		for (i = 0; i < count; i++) {
664*cfcc706cSMiquel Raynal 			/* correct data only, not ecc bytes */
665*cfcc706cSMiquel Raynal 			if (errloc[i] < SECTOR_BYTES << 3)
666*cfcc706cSMiquel Raynal 				data[errloc[i] >> 3] ^= 1 << (errloc[i] & 7);
667*cfcc706cSMiquel Raynal 			debug("corrected bitflip %u\n", errloc[i]);
668*cfcc706cSMiquel Raynal #ifdef DEBUG
669*cfcc706cSMiquel Raynal 			puts("read_ecc: ");
670*cfcc706cSMiquel Raynal 			/*
671*cfcc706cSMiquel Raynal 			 * BCH8 have 13 bytes of ECC; BCH4 needs adoption
672*cfcc706cSMiquel Raynal 			 * here!
673*cfcc706cSMiquel Raynal 			 */
674*cfcc706cSMiquel Raynal 			for (i = 0; i < 13; i++)
675*cfcc706cSMiquel Raynal 				printf("%02x ", read_ecc[i]);
676*cfcc706cSMiquel Raynal 			puts("\n");
677*cfcc706cSMiquel Raynal 			puts("calc_ecc: ");
678*cfcc706cSMiquel Raynal 			for (i = 0; i < 13; i++)
679*cfcc706cSMiquel Raynal 				printf("%02x ", calc_ecc[i]);
680*cfcc706cSMiquel Raynal 			puts("\n");
681*cfcc706cSMiquel Raynal #endif
682*cfcc706cSMiquel Raynal 		}
683*cfcc706cSMiquel Raynal 	} else if (count < 0) {
684*cfcc706cSMiquel Raynal 		puts("ecc unrecoverable error\n");
685*cfcc706cSMiquel Raynal 	}
686*cfcc706cSMiquel Raynal 	return count;
687*cfcc706cSMiquel Raynal }
688*cfcc706cSMiquel Raynal 
689*cfcc706cSMiquel Raynal /**
690*cfcc706cSMiquel Raynal  * omap_free_bch - Release BCH ecc resources
691*cfcc706cSMiquel Raynal  * @mtd: MTD device structure
692*cfcc706cSMiquel Raynal  */
omap_free_bch(struct mtd_info * mtd)693*cfcc706cSMiquel Raynal static void __maybe_unused omap_free_bch(struct mtd_info *mtd)
694*cfcc706cSMiquel Raynal {
695*cfcc706cSMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
696*cfcc706cSMiquel Raynal 	struct omap_nand_info *info = nand_get_controller_data(chip);
697*cfcc706cSMiquel Raynal 
698*cfcc706cSMiquel Raynal 	if (info->control) {
699*cfcc706cSMiquel Raynal 		free_bch(info->control);
700*cfcc706cSMiquel Raynal 		info->control = NULL;
701*cfcc706cSMiquel Raynal 	}
702*cfcc706cSMiquel Raynal }
703*cfcc706cSMiquel Raynal #endif /* CONFIG_BCH */
704*cfcc706cSMiquel Raynal 
705*cfcc706cSMiquel Raynal /**
706*cfcc706cSMiquel Raynal  * omap_select_ecc_scheme - configures driver for particular ecc-scheme
707*cfcc706cSMiquel Raynal  * @nand: NAND chip device structure
708*cfcc706cSMiquel Raynal  * @ecc_scheme: ecc scheme to configure
709*cfcc706cSMiquel Raynal  * @pagesize: number of main-area bytes per page of NAND device
710*cfcc706cSMiquel Raynal  * @oobsize: number of OOB/spare bytes per page of NAND device
711*cfcc706cSMiquel Raynal  */
omap_select_ecc_scheme(struct nand_chip * nand,enum omap_ecc ecc_scheme,unsigned int pagesize,unsigned int oobsize)712*cfcc706cSMiquel Raynal static int omap_select_ecc_scheme(struct nand_chip *nand,
713*cfcc706cSMiquel Raynal 	enum omap_ecc ecc_scheme, unsigned int pagesize, unsigned int oobsize) {
714*cfcc706cSMiquel Raynal 	struct omap_nand_info	*info		= nand_get_controller_data(nand);
715*cfcc706cSMiquel Raynal 	struct nand_ecclayout	*ecclayout	= &omap_ecclayout;
716*cfcc706cSMiquel Raynal 	int eccsteps = pagesize / SECTOR_BYTES;
717*cfcc706cSMiquel Raynal 	int i;
718*cfcc706cSMiquel Raynal 
719*cfcc706cSMiquel Raynal 	switch (ecc_scheme) {
720*cfcc706cSMiquel Raynal 	case OMAP_ECC_HAM1_CODE_SW:
721*cfcc706cSMiquel Raynal 		debug("nand: selected OMAP_ECC_HAM1_CODE_SW\n");
722*cfcc706cSMiquel Raynal 		/* For this ecc-scheme, ecc.bytes, ecc.layout, ... are
723*cfcc706cSMiquel Raynal 		 * initialized in nand_scan_tail(), so just set ecc.mode */
724*cfcc706cSMiquel Raynal 		info->control		= NULL;
725*cfcc706cSMiquel Raynal 		nand->ecc.mode		= NAND_ECC_SOFT;
726*cfcc706cSMiquel Raynal 		nand->ecc.layout	= NULL;
727*cfcc706cSMiquel Raynal 		nand->ecc.size		= 0;
728*cfcc706cSMiquel Raynal 		break;
729*cfcc706cSMiquel Raynal 
730*cfcc706cSMiquel Raynal 	case OMAP_ECC_HAM1_CODE_HW:
731*cfcc706cSMiquel Raynal 		debug("nand: selected OMAP_ECC_HAM1_CODE_HW\n");
732*cfcc706cSMiquel Raynal 		/* check ecc-scheme requirements before updating ecc info */
733*cfcc706cSMiquel Raynal 		if ((3 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
734*cfcc706cSMiquel Raynal 			printf("nand: error: insufficient OOB: require=%d\n", (
735*cfcc706cSMiquel Raynal 				(3 * eccsteps) + BADBLOCK_MARKER_LENGTH));
736*cfcc706cSMiquel Raynal 			return -EINVAL;
737*cfcc706cSMiquel Raynal 		}
738*cfcc706cSMiquel Raynal 		info->control		= NULL;
739*cfcc706cSMiquel Raynal 		/* populate ecc specific fields */
740*cfcc706cSMiquel Raynal 		memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
741*cfcc706cSMiquel Raynal 		nand->ecc.mode		= NAND_ECC_HW;
742*cfcc706cSMiquel Raynal 		nand->ecc.strength	= 1;
743*cfcc706cSMiquel Raynal 		nand->ecc.size		= SECTOR_BYTES;
744*cfcc706cSMiquel Raynal 		nand->ecc.bytes		= 3;
745*cfcc706cSMiquel Raynal 		nand->ecc.hwctl		= omap_enable_hwecc;
746*cfcc706cSMiquel Raynal 		nand->ecc.correct	= omap_correct_data;
747*cfcc706cSMiquel Raynal 		nand->ecc.calculate	= omap_calculate_ecc;
748*cfcc706cSMiquel Raynal 		/* define ecc-layout */
749*cfcc706cSMiquel Raynal 		ecclayout->eccbytes	= nand->ecc.bytes * eccsteps;
750*cfcc706cSMiquel Raynal 		for (i = 0; i < ecclayout->eccbytes; i++) {
751*cfcc706cSMiquel Raynal 			if (nand->options & NAND_BUSWIDTH_16)
752*cfcc706cSMiquel Raynal 				ecclayout->eccpos[i] = i + 2;
753*cfcc706cSMiquel Raynal 			else
754*cfcc706cSMiquel Raynal 				ecclayout->eccpos[i] = i + 1;
755*cfcc706cSMiquel Raynal 		}
756*cfcc706cSMiquel Raynal 		ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
757*cfcc706cSMiquel Raynal 		ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
758*cfcc706cSMiquel Raynal 						BADBLOCK_MARKER_LENGTH;
759*cfcc706cSMiquel Raynal 		break;
760*cfcc706cSMiquel Raynal 
761*cfcc706cSMiquel Raynal 	case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
762*cfcc706cSMiquel Raynal #ifdef CONFIG_BCH
763*cfcc706cSMiquel Raynal 		debug("nand: selected OMAP_ECC_BCH8_CODE_HW_DETECTION_SW\n");
764*cfcc706cSMiquel Raynal 		/* check ecc-scheme requirements before updating ecc info */
765*cfcc706cSMiquel Raynal 		if ((13 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
766*cfcc706cSMiquel Raynal 			printf("nand: error: insufficient OOB: require=%d\n", (
767*cfcc706cSMiquel Raynal 				(13 * eccsteps) + BADBLOCK_MARKER_LENGTH));
768*cfcc706cSMiquel Raynal 			return -EINVAL;
769*cfcc706cSMiquel Raynal 		}
770*cfcc706cSMiquel Raynal 		/* check if BCH S/W library can be used for error detection */
771*cfcc706cSMiquel Raynal 		info->control = init_bch(13, 8, 0x201b);
772*cfcc706cSMiquel Raynal 		if (!info->control) {
773*cfcc706cSMiquel Raynal 			printf("nand: error: could not init_bch()\n");
774*cfcc706cSMiquel Raynal 			return -ENODEV;
775*cfcc706cSMiquel Raynal 		}
776*cfcc706cSMiquel Raynal 		/* populate ecc specific fields */
777*cfcc706cSMiquel Raynal 		memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
778*cfcc706cSMiquel Raynal 		nand->ecc.mode		= NAND_ECC_HW;
779*cfcc706cSMiquel Raynal 		nand->ecc.strength	= 8;
780*cfcc706cSMiquel Raynal 		nand->ecc.size		= SECTOR_BYTES;
781*cfcc706cSMiquel Raynal 		nand->ecc.bytes		= 13;
782*cfcc706cSMiquel Raynal 		nand->ecc.hwctl		= omap_enable_hwecc;
783*cfcc706cSMiquel Raynal 		nand->ecc.correct	= omap_correct_data_bch_sw;
784*cfcc706cSMiquel Raynal 		nand->ecc.calculate	= omap_calculate_ecc;
785*cfcc706cSMiquel Raynal 		/* define ecc-layout */
786*cfcc706cSMiquel Raynal 		ecclayout->eccbytes	= nand->ecc.bytes * eccsteps;
787*cfcc706cSMiquel Raynal 		ecclayout->eccpos[0]	= BADBLOCK_MARKER_LENGTH;
788*cfcc706cSMiquel Raynal 		for (i = 1; i < ecclayout->eccbytes; i++) {
789*cfcc706cSMiquel Raynal 			if (i % nand->ecc.bytes)
790*cfcc706cSMiquel Raynal 				ecclayout->eccpos[i] =
791*cfcc706cSMiquel Raynal 						ecclayout->eccpos[i - 1] + 1;
792*cfcc706cSMiquel Raynal 			else
793*cfcc706cSMiquel Raynal 				ecclayout->eccpos[i] =
794*cfcc706cSMiquel Raynal 						ecclayout->eccpos[i - 1] + 2;
795*cfcc706cSMiquel Raynal 		}
796*cfcc706cSMiquel Raynal 		ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
797*cfcc706cSMiquel Raynal 		ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
798*cfcc706cSMiquel Raynal 						BADBLOCK_MARKER_LENGTH;
799*cfcc706cSMiquel Raynal 		break;
800*cfcc706cSMiquel Raynal #else
801*cfcc706cSMiquel Raynal 		printf("nand: error: CONFIG_BCH required for ECC\n");
802*cfcc706cSMiquel Raynal 		return -EINVAL;
803*cfcc706cSMiquel Raynal #endif
804*cfcc706cSMiquel Raynal 
805*cfcc706cSMiquel Raynal 	case OMAP_ECC_BCH8_CODE_HW:
806*cfcc706cSMiquel Raynal #ifdef CONFIG_NAND_OMAP_ELM
807*cfcc706cSMiquel Raynal 		debug("nand: selected OMAP_ECC_BCH8_CODE_HW\n");
808*cfcc706cSMiquel Raynal 		/* check ecc-scheme requirements before updating ecc info */
809*cfcc706cSMiquel Raynal 		if ((14 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
810*cfcc706cSMiquel Raynal 			printf("nand: error: insufficient OOB: require=%d\n", (
811*cfcc706cSMiquel Raynal 				(14 * eccsteps) + BADBLOCK_MARKER_LENGTH));
812*cfcc706cSMiquel Raynal 			return -EINVAL;
813*cfcc706cSMiquel Raynal 		}
814*cfcc706cSMiquel Raynal 		/* intialize ELM for ECC error detection */
815*cfcc706cSMiquel Raynal 		elm_init();
816*cfcc706cSMiquel Raynal 		info->control		= NULL;
817*cfcc706cSMiquel Raynal 		/* populate ecc specific fields */
818*cfcc706cSMiquel Raynal 		memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
819*cfcc706cSMiquel Raynal 		nand->ecc.mode		= NAND_ECC_HW;
820*cfcc706cSMiquel Raynal 		nand->ecc.strength	= 8;
821*cfcc706cSMiquel Raynal 		nand->ecc.size		= SECTOR_BYTES;
822*cfcc706cSMiquel Raynal 		nand->ecc.bytes		= 14;
823*cfcc706cSMiquel Raynal 		nand->ecc.hwctl		= omap_enable_hwecc;
824*cfcc706cSMiquel Raynal 		nand->ecc.correct	= omap_correct_data_bch;
825*cfcc706cSMiquel Raynal 		nand->ecc.calculate	= omap_calculate_ecc;
826*cfcc706cSMiquel Raynal 		nand->ecc.read_page	= omap_read_page_bch;
827*cfcc706cSMiquel Raynal 		/* define ecc-layout */
828*cfcc706cSMiquel Raynal 		ecclayout->eccbytes	= nand->ecc.bytes * eccsteps;
829*cfcc706cSMiquel Raynal 		for (i = 0; i < ecclayout->eccbytes; i++)
830*cfcc706cSMiquel Raynal 			ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH;
831*cfcc706cSMiquel Raynal 		ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
832*cfcc706cSMiquel Raynal 		ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
833*cfcc706cSMiquel Raynal 						BADBLOCK_MARKER_LENGTH;
834*cfcc706cSMiquel Raynal 		break;
835*cfcc706cSMiquel Raynal #else
836*cfcc706cSMiquel Raynal 		printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
837*cfcc706cSMiquel Raynal 		return -EINVAL;
838*cfcc706cSMiquel Raynal #endif
839*cfcc706cSMiquel Raynal 
840*cfcc706cSMiquel Raynal 	case OMAP_ECC_BCH16_CODE_HW:
841*cfcc706cSMiquel Raynal #ifdef CONFIG_NAND_OMAP_ELM
842*cfcc706cSMiquel Raynal 		debug("nand: using OMAP_ECC_BCH16_CODE_HW\n");
843*cfcc706cSMiquel Raynal 		/* check ecc-scheme requirements before updating ecc info */
844*cfcc706cSMiquel Raynal 		if ((26 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
845*cfcc706cSMiquel Raynal 			printf("nand: error: insufficient OOB: require=%d\n", (
846*cfcc706cSMiquel Raynal 				(26 * eccsteps) + BADBLOCK_MARKER_LENGTH));
847*cfcc706cSMiquel Raynal 			return -EINVAL;
848*cfcc706cSMiquel Raynal 		}
849*cfcc706cSMiquel Raynal 		/* intialize ELM for ECC error detection */
850*cfcc706cSMiquel Raynal 		elm_init();
851*cfcc706cSMiquel Raynal 		/* populate ecc specific fields */
852*cfcc706cSMiquel Raynal 		nand->ecc.mode		= NAND_ECC_HW;
853*cfcc706cSMiquel Raynal 		nand->ecc.size		= SECTOR_BYTES;
854*cfcc706cSMiquel Raynal 		nand->ecc.bytes		= 26;
855*cfcc706cSMiquel Raynal 		nand->ecc.strength	= 16;
856*cfcc706cSMiquel Raynal 		nand->ecc.hwctl		= omap_enable_hwecc;
857*cfcc706cSMiquel Raynal 		nand->ecc.correct	= omap_correct_data_bch;
858*cfcc706cSMiquel Raynal 		nand->ecc.calculate	= omap_calculate_ecc;
859*cfcc706cSMiquel Raynal 		nand->ecc.read_page	= omap_read_page_bch;
860*cfcc706cSMiquel Raynal 		/* define ecc-layout */
861*cfcc706cSMiquel Raynal 		ecclayout->eccbytes	= nand->ecc.bytes * eccsteps;
862*cfcc706cSMiquel Raynal 		for (i = 0; i < ecclayout->eccbytes; i++)
863*cfcc706cSMiquel Raynal 			ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH;
864*cfcc706cSMiquel Raynal 		ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
865*cfcc706cSMiquel Raynal 		ecclayout->oobfree[0].length = oobsize - nand->ecc.bytes -
866*cfcc706cSMiquel Raynal 						BADBLOCK_MARKER_LENGTH;
867*cfcc706cSMiquel Raynal 		break;
868*cfcc706cSMiquel Raynal #else
869*cfcc706cSMiquel Raynal 		printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
870*cfcc706cSMiquel Raynal 		return -EINVAL;
871*cfcc706cSMiquel Raynal #endif
872*cfcc706cSMiquel Raynal 	default:
873*cfcc706cSMiquel Raynal 		debug("nand: error: ecc scheme not enabled or supported\n");
874*cfcc706cSMiquel Raynal 		return -EINVAL;
875*cfcc706cSMiquel Raynal 	}
876*cfcc706cSMiquel Raynal 
877*cfcc706cSMiquel Raynal 	/* nand_scan_tail() sets ham1 sw ecc; hw ecc layout is set by driver */
878*cfcc706cSMiquel Raynal 	if (ecc_scheme != OMAP_ECC_HAM1_CODE_SW)
879*cfcc706cSMiquel Raynal 		nand->ecc.layout = ecclayout;
880*cfcc706cSMiquel Raynal 
881*cfcc706cSMiquel Raynal 	info->ecc_scheme = ecc_scheme;
882*cfcc706cSMiquel Raynal 	return 0;
883*cfcc706cSMiquel Raynal }
884*cfcc706cSMiquel Raynal 
885*cfcc706cSMiquel Raynal #ifndef CONFIG_SPL_BUILD
886*cfcc706cSMiquel Raynal /*
887*cfcc706cSMiquel Raynal  * omap_nand_switch_ecc - switch the ECC operation between different engines
888*cfcc706cSMiquel Raynal  * (h/w and s/w) and different algorithms (hamming and BCHx)
889*cfcc706cSMiquel Raynal  *
890*cfcc706cSMiquel Raynal  * @hardware		- true if one of the HW engines should be used
891*cfcc706cSMiquel Raynal  * @eccstrength		- the number of bits that could be corrected
892*cfcc706cSMiquel Raynal  *			  (1 - hamming, 4 - BCH4, 8 - BCH8, 16 - BCH16)
893*cfcc706cSMiquel Raynal  */
omap_nand_switch_ecc(uint32_t hardware,uint32_t eccstrength)894*cfcc706cSMiquel Raynal int __maybe_unused omap_nand_switch_ecc(uint32_t hardware, uint32_t eccstrength)
895*cfcc706cSMiquel Raynal {
896*cfcc706cSMiquel Raynal 	struct nand_chip *nand;
897*cfcc706cSMiquel Raynal 	struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
898*cfcc706cSMiquel Raynal 	int err = 0;
899*cfcc706cSMiquel Raynal 
900*cfcc706cSMiquel Raynal 	if (!mtd) {
901*cfcc706cSMiquel Raynal 		printf("nand: error: no NAND devices found\n");
902*cfcc706cSMiquel Raynal 		return -ENODEV;
903*cfcc706cSMiquel Raynal 	}
904*cfcc706cSMiquel Raynal 
905*cfcc706cSMiquel Raynal 	nand = mtd_to_nand(mtd);
906*cfcc706cSMiquel Raynal 	nand->options |= NAND_OWN_BUFFERS;
907*cfcc706cSMiquel Raynal 	nand->options &= ~NAND_SUBPAGE_READ;
908*cfcc706cSMiquel Raynal 	/* Setup the ecc configurations again */
909*cfcc706cSMiquel Raynal 	if (hardware) {
910*cfcc706cSMiquel Raynal 		if (eccstrength == 1) {
911*cfcc706cSMiquel Raynal 			err = omap_select_ecc_scheme(nand,
912*cfcc706cSMiquel Raynal 					OMAP_ECC_HAM1_CODE_HW,
913*cfcc706cSMiquel Raynal 					mtd->writesize, mtd->oobsize);
914*cfcc706cSMiquel Raynal 		} else if (eccstrength == 8) {
915*cfcc706cSMiquel Raynal 			err = omap_select_ecc_scheme(nand,
916*cfcc706cSMiquel Raynal 					OMAP_ECC_BCH8_CODE_HW,
917*cfcc706cSMiquel Raynal 					mtd->writesize, mtd->oobsize);
918*cfcc706cSMiquel Raynal 		} else if (eccstrength == 16) {
919*cfcc706cSMiquel Raynal 			err = omap_select_ecc_scheme(nand,
920*cfcc706cSMiquel Raynal 					OMAP_ECC_BCH16_CODE_HW,
921*cfcc706cSMiquel Raynal 					mtd->writesize, mtd->oobsize);
922*cfcc706cSMiquel Raynal 		} else {
923*cfcc706cSMiquel Raynal 			printf("nand: error: unsupported ECC scheme\n");
924*cfcc706cSMiquel Raynal 			return -EINVAL;
925*cfcc706cSMiquel Raynal 		}
926*cfcc706cSMiquel Raynal 	} else {
927*cfcc706cSMiquel Raynal 		if (eccstrength == 1) {
928*cfcc706cSMiquel Raynal 			err = omap_select_ecc_scheme(nand,
929*cfcc706cSMiquel Raynal 					OMAP_ECC_HAM1_CODE_SW,
930*cfcc706cSMiquel Raynal 					mtd->writesize, mtd->oobsize);
931*cfcc706cSMiquel Raynal 		} else if (eccstrength == 8) {
932*cfcc706cSMiquel Raynal 			err = omap_select_ecc_scheme(nand,
933*cfcc706cSMiquel Raynal 					OMAP_ECC_BCH8_CODE_HW_DETECTION_SW,
934*cfcc706cSMiquel Raynal 					mtd->writesize, mtd->oobsize);
935*cfcc706cSMiquel Raynal 		} else {
936*cfcc706cSMiquel Raynal 			printf("nand: error: unsupported ECC scheme\n");
937*cfcc706cSMiquel Raynal 			return -EINVAL;
938*cfcc706cSMiquel Raynal 		}
939*cfcc706cSMiquel Raynal 	}
940*cfcc706cSMiquel Raynal 
941*cfcc706cSMiquel Raynal 	/* Update NAND handling after ECC mode switch */
942*cfcc706cSMiquel Raynal 	if (!err)
943*cfcc706cSMiquel Raynal 		err = nand_scan_tail(mtd);
944*cfcc706cSMiquel Raynal 	return err;
945*cfcc706cSMiquel Raynal }
946*cfcc706cSMiquel Raynal #endif /* CONFIG_SPL_BUILD */
947*cfcc706cSMiquel Raynal 
948*cfcc706cSMiquel Raynal /*
949*cfcc706cSMiquel Raynal  * Board-specific NAND initialization. The following members of the
950*cfcc706cSMiquel Raynal  * argument are board-specific:
951*cfcc706cSMiquel Raynal  * - IO_ADDR_R: address to read the 8 I/O lines of the flash device
952*cfcc706cSMiquel Raynal  * - IO_ADDR_W: address to write the 8 I/O lines of the flash device
953*cfcc706cSMiquel Raynal  * - cmd_ctrl: hardwarespecific function for accesing control-lines
954*cfcc706cSMiquel Raynal  * - waitfunc: hardwarespecific function for accesing device ready/busy line
955*cfcc706cSMiquel Raynal  * - ecc.hwctl: function to enable (reset) hardware ecc generator
956*cfcc706cSMiquel Raynal  * - ecc.mode: mode of ecc, see defines
957*cfcc706cSMiquel Raynal  * - chip_delay: chip dependent delay for transfering data from array to
958*cfcc706cSMiquel Raynal  *   read regs (tR)
959*cfcc706cSMiquel Raynal  * - options: various chip options. They can partly be set to inform
960*cfcc706cSMiquel Raynal  *   nand_scan about special functionality. See the defines for further
961*cfcc706cSMiquel Raynal  *   explanation
962*cfcc706cSMiquel Raynal  */
board_nand_init(struct nand_chip * nand)963*cfcc706cSMiquel Raynal int board_nand_init(struct nand_chip *nand)
964*cfcc706cSMiquel Raynal {
965*cfcc706cSMiquel Raynal 	int32_t gpmc_config = 0;
966*cfcc706cSMiquel Raynal 	int cs = cs_next++;
967*cfcc706cSMiquel Raynal 	int err = 0;
968*cfcc706cSMiquel Raynal 	/*
969*cfcc706cSMiquel Raynal 	 * xloader/Uboot's gpmc configuration would have configured GPMC for
970*cfcc706cSMiquel Raynal 	 * nand type of memory. The following logic scans and latches on to the
971*cfcc706cSMiquel Raynal 	 * first CS with NAND type memory.
972*cfcc706cSMiquel Raynal 	 * TBD: need to make this logic generic to handle multiple CS NAND
973*cfcc706cSMiquel Raynal 	 * devices.
974*cfcc706cSMiquel Raynal 	 */
975*cfcc706cSMiquel Raynal 	while (cs < GPMC_MAX_CS) {
976*cfcc706cSMiquel Raynal 		/* Check if NAND type is set */
977*cfcc706cSMiquel Raynal 		if ((readl(&gpmc_cfg->cs[cs].config1) & 0xC00) == 0x800) {
978*cfcc706cSMiquel Raynal 			/* Found it!! */
979*cfcc706cSMiquel Raynal 			break;
980*cfcc706cSMiquel Raynal 		}
981*cfcc706cSMiquel Raynal 		cs++;
982*cfcc706cSMiquel Raynal 	}
983*cfcc706cSMiquel Raynal 	if (cs >= GPMC_MAX_CS) {
984*cfcc706cSMiquel Raynal 		printf("nand: error: Unable to find NAND settings in "
985*cfcc706cSMiquel Raynal 			"GPMC Configuration - quitting\n");
986*cfcc706cSMiquel Raynal 		return -ENODEV;
987*cfcc706cSMiquel Raynal 	}
988*cfcc706cSMiquel Raynal 
989*cfcc706cSMiquel Raynal 	gpmc_config = readl(&gpmc_cfg->config);
990*cfcc706cSMiquel Raynal 	/* Disable Write protect */
991*cfcc706cSMiquel Raynal 	gpmc_config |= 0x10;
992*cfcc706cSMiquel Raynal 	writel(gpmc_config, &gpmc_cfg->config);
993*cfcc706cSMiquel Raynal 
994*cfcc706cSMiquel Raynal 	nand->IO_ADDR_R = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
995*cfcc706cSMiquel Raynal 	nand->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
996*cfcc706cSMiquel Raynal 	omap_nand_info[cs].control = NULL;
997*cfcc706cSMiquel Raynal 	omap_nand_info[cs].cs = cs;
998*cfcc706cSMiquel Raynal 	omap_nand_info[cs].ws = wscfg[cs];
999*cfcc706cSMiquel Raynal 	nand_set_controller_data(nand, &omap_nand_info[cs]);
1000*cfcc706cSMiquel Raynal 	nand->cmd_ctrl	= omap_nand_hwcontrol;
1001*cfcc706cSMiquel Raynal 	nand->options	|= NAND_NO_PADDING | NAND_CACHEPRG;
1002*cfcc706cSMiquel Raynal 	nand->chip_delay = 100;
1003*cfcc706cSMiquel Raynal 	nand->ecc.layout = &omap_ecclayout;
1004*cfcc706cSMiquel Raynal 
1005*cfcc706cSMiquel Raynal 	/* configure driver and controller based on NAND device bus-width */
1006*cfcc706cSMiquel Raynal 	gpmc_config = readl(&gpmc_cfg->cs[cs].config1);
1007*cfcc706cSMiquel Raynal #if defined(CONFIG_SYS_NAND_BUSWIDTH_16BIT)
1008*cfcc706cSMiquel Raynal 	nand->options |= NAND_BUSWIDTH_16;
1009*cfcc706cSMiquel Raynal 	writel(gpmc_config | (0x1 << 12), &gpmc_cfg->cs[cs].config1);
1010*cfcc706cSMiquel Raynal #else
1011*cfcc706cSMiquel Raynal 	nand->options &= ~NAND_BUSWIDTH_16;
1012*cfcc706cSMiquel Raynal 	writel(gpmc_config & ~(0x1 << 12), &gpmc_cfg->cs[cs].config1);
1013*cfcc706cSMiquel Raynal #endif
1014*cfcc706cSMiquel Raynal 	/* select ECC scheme */
1015*cfcc706cSMiquel Raynal #if defined(CONFIG_NAND_OMAP_ECCSCHEME)
1016*cfcc706cSMiquel Raynal 	err = omap_select_ecc_scheme(nand, CONFIG_NAND_OMAP_ECCSCHEME,
1017*cfcc706cSMiquel Raynal 			CONFIG_SYS_NAND_PAGE_SIZE, CONFIG_SYS_NAND_OOBSIZE);
1018*cfcc706cSMiquel Raynal #else
1019*cfcc706cSMiquel Raynal 	/* pagesize and oobsize are not required to configure sw ecc-scheme */
1020*cfcc706cSMiquel Raynal 	err = omap_select_ecc_scheme(nand, OMAP_ECC_HAM1_CODE_SW,
1021*cfcc706cSMiquel Raynal 			0, 0);
1022*cfcc706cSMiquel Raynal #endif
1023*cfcc706cSMiquel Raynal 	if (err)
1024*cfcc706cSMiquel Raynal 		return err;
1025*cfcc706cSMiquel Raynal 
1026*cfcc706cSMiquel Raynal #ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
1027*cfcc706cSMiquel Raynal 	nand->read_buf = omap_nand_read_prefetch;
1028*cfcc706cSMiquel Raynal #else
1029*cfcc706cSMiquel Raynal 	if (nand->options & NAND_BUSWIDTH_16)
1030*cfcc706cSMiquel Raynal 		nand->read_buf = nand_read_buf16;
1031*cfcc706cSMiquel Raynal 	else
1032*cfcc706cSMiquel Raynal 		nand->read_buf = nand_read_buf;
1033*cfcc706cSMiquel Raynal #endif
1034*cfcc706cSMiquel Raynal 
1035*cfcc706cSMiquel Raynal 	nand->dev_ready = omap_dev_ready;
1036*cfcc706cSMiquel Raynal 
1037*cfcc706cSMiquel Raynal 	return 0;
1038*cfcc706cSMiquel Raynal }
1039