xref: /OK3568_Linux_fs/u-boot/drivers/mtd/nand/raw/fsmc_nand.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright 2010
3*4882a593Smuzhiyun  * Vipin Kumar, ST Microelectronics, vipin.kumar@st.com.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * (C) Copyright 2012
6*4882a593Smuzhiyun  * Amit Virdi, ST Microelectronics, amit.virdi@st.com.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <common.h>
12*4882a593Smuzhiyun #include <nand.h>
13*4882a593Smuzhiyun #include <asm/io.h>
14*4882a593Smuzhiyun #include <linux/bitops.h>
15*4882a593Smuzhiyun #include <linux/err.h>
16*4882a593Smuzhiyun #include <linux/mtd/nand_ecc.h>
17*4882a593Smuzhiyun #include <linux/mtd/fsmc_nand.h>
18*4882a593Smuzhiyun #include <asm/arch/hardware.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun static u32 fsmc_version;
21*4882a593Smuzhiyun static struct fsmc_regs *const fsmc_regs_p = (struct fsmc_regs *)
22*4882a593Smuzhiyun 	CONFIG_SYS_FSMC_BASE;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun  * ECC4 and ECC1 have 13 bytes and 3 bytes of ecc respectively for 512 bytes of
26*4882a593Smuzhiyun  * data. ECC4 can correct up to 8 bits in 512 bytes of data while ECC1 can
27*4882a593Smuzhiyun  * correct 1 bit in 512 bytes
28*4882a593Smuzhiyun  */
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun static struct nand_ecclayout fsmc_ecc4_lp_layout = {
31*4882a593Smuzhiyun 	.eccbytes = 104,
32*4882a593Smuzhiyun 	.eccpos = {  2,   3,   4,   5,   6,   7,   8,
33*4882a593Smuzhiyun 		9,  10,  11,  12,  13,  14,
34*4882a593Smuzhiyun 		18,  19,  20,  21,  22,  23,  24,
35*4882a593Smuzhiyun 		25,  26,  27,  28,  29,  30,
36*4882a593Smuzhiyun 		34,  35,  36,  37,  38,  39,  40,
37*4882a593Smuzhiyun 		41,  42,  43,  44,  45,  46,
38*4882a593Smuzhiyun 		50,  51,  52,  53,  54,  55,  56,
39*4882a593Smuzhiyun 		57,  58,  59,  60,  61,  62,
40*4882a593Smuzhiyun 		66,  67,  68,  69,  70,  71,  72,
41*4882a593Smuzhiyun 		73,  74,  75,  76,  77,  78,
42*4882a593Smuzhiyun 		82,  83,  84,  85,  86,  87,  88,
43*4882a593Smuzhiyun 		89,  90,  91,  92,  93,  94,
44*4882a593Smuzhiyun 		98,  99, 100, 101, 102, 103, 104,
45*4882a593Smuzhiyun 		105, 106, 107, 108, 109, 110,
46*4882a593Smuzhiyun 		114, 115, 116, 117, 118, 119, 120,
47*4882a593Smuzhiyun 		121, 122, 123, 124, 125, 126
48*4882a593Smuzhiyun 	},
49*4882a593Smuzhiyun 	.oobfree = {
50*4882a593Smuzhiyun 		{.offset = 15, .length = 3},
51*4882a593Smuzhiyun 		{.offset = 31, .length = 3},
52*4882a593Smuzhiyun 		{.offset = 47, .length = 3},
53*4882a593Smuzhiyun 		{.offset = 63, .length = 3},
54*4882a593Smuzhiyun 		{.offset = 79, .length = 3},
55*4882a593Smuzhiyun 		{.offset = 95, .length = 3},
56*4882a593Smuzhiyun 		{.offset = 111, .length = 3},
57*4882a593Smuzhiyun 		{.offset = 127, .length = 1}
58*4882a593Smuzhiyun 	}
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun  * ECC4 layout for NAND of pagesize 4096 bytes & OOBsize 224 bytes. 13*8 bytes
63*4882a593Smuzhiyun  * of OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block & 118
64*4882a593Smuzhiyun  * bytes are free for use.
65*4882a593Smuzhiyun  */
66*4882a593Smuzhiyun static struct nand_ecclayout fsmc_ecc4_224_layout = {
67*4882a593Smuzhiyun 	.eccbytes = 104,
68*4882a593Smuzhiyun 	.eccpos = {  2,   3,   4,   5,   6,   7,   8,
69*4882a593Smuzhiyun 		9,  10,  11,  12,  13,  14,
70*4882a593Smuzhiyun 		18,  19,  20,  21,  22,  23,  24,
71*4882a593Smuzhiyun 		25,  26,  27,  28,  29,  30,
72*4882a593Smuzhiyun 		34,  35,  36,  37,  38,  39,  40,
73*4882a593Smuzhiyun 		41,  42,  43,  44,  45,  46,
74*4882a593Smuzhiyun 		50,  51,  52,  53,  54,  55,  56,
75*4882a593Smuzhiyun 		57,  58,  59,  60,  61,  62,
76*4882a593Smuzhiyun 		66,  67,  68,  69,  70,  71,  72,
77*4882a593Smuzhiyun 		73,  74,  75,  76,  77,  78,
78*4882a593Smuzhiyun 		82,  83,  84,  85,  86,  87,  88,
79*4882a593Smuzhiyun 		89,  90,  91,  92,  93,  94,
80*4882a593Smuzhiyun 		98,  99, 100, 101, 102, 103, 104,
81*4882a593Smuzhiyun 		105, 106, 107, 108, 109, 110,
82*4882a593Smuzhiyun 		114, 115, 116, 117, 118, 119, 120,
83*4882a593Smuzhiyun 		121, 122, 123, 124, 125, 126
84*4882a593Smuzhiyun 	},
85*4882a593Smuzhiyun 	.oobfree = {
86*4882a593Smuzhiyun 		{.offset = 15, .length = 3},
87*4882a593Smuzhiyun 		{.offset = 31, .length = 3},
88*4882a593Smuzhiyun 		{.offset = 47, .length = 3},
89*4882a593Smuzhiyun 		{.offset = 63, .length = 3},
90*4882a593Smuzhiyun 		{.offset = 79, .length = 3},
91*4882a593Smuzhiyun 		{.offset = 95, .length = 3},
92*4882a593Smuzhiyun 		{.offset = 111, .length = 3},
93*4882a593Smuzhiyun 		{.offset = 127, .length = 97}
94*4882a593Smuzhiyun 	}
95*4882a593Smuzhiyun };
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /*
98*4882a593Smuzhiyun  * ECC placement definitions in oobfree type format
99*4882a593Smuzhiyun  * There are 13 bytes of ecc for every 512 byte block and it has to be read
100*4882a593Smuzhiyun  * consecutively and immediately after the 512 byte data block for hardware to
101*4882a593Smuzhiyun  * generate the error bit offsets in 512 byte data
102*4882a593Smuzhiyun  * Managing the ecc bytes in the following way makes it easier for software to
103*4882a593Smuzhiyun  * read ecc bytes consecutive to data bytes. This way is similar to
104*4882a593Smuzhiyun  * oobfree structure maintained already in u-boot nand driver
105*4882a593Smuzhiyun  */
106*4882a593Smuzhiyun static struct fsmc_eccplace fsmc_eccpl_lp = {
107*4882a593Smuzhiyun 	.eccplace = {
108*4882a593Smuzhiyun 		{.offset = 2, .length = 13},
109*4882a593Smuzhiyun 		{.offset = 18, .length = 13},
110*4882a593Smuzhiyun 		{.offset = 34, .length = 13},
111*4882a593Smuzhiyun 		{.offset = 50, .length = 13},
112*4882a593Smuzhiyun 		{.offset = 66, .length = 13},
113*4882a593Smuzhiyun 		{.offset = 82, .length = 13},
114*4882a593Smuzhiyun 		{.offset = 98, .length = 13},
115*4882a593Smuzhiyun 		{.offset = 114, .length = 13}
116*4882a593Smuzhiyun 	}
117*4882a593Smuzhiyun };
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun static struct nand_ecclayout fsmc_ecc4_sp_layout = {
120*4882a593Smuzhiyun 	.eccbytes = 13,
121*4882a593Smuzhiyun 	.eccpos = { 0,  1,  2,  3,  6,  7, 8,
122*4882a593Smuzhiyun 		9, 10, 11, 12, 13, 14
123*4882a593Smuzhiyun 	},
124*4882a593Smuzhiyun 	.oobfree = {
125*4882a593Smuzhiyun 		{.offset = 15, .length = 1},
126*4882a593Smuzhiyun 	}
127*4882a593Smuzhiyun };
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun static struct fsmc_eccplace fsmc_eccpl_sp = {
130*4882a593Smuzhiyun 	.eccplace = {
131*4882a593Smuzhiyun 		{.offset = 0, .length = 4},
132*4882a593Smuzhiyun 		{.offset = 6, .length = 9}
133*4882a593Smuzhiyun 	}
134*4882a593Smuzhiyun };
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun static struct nand_ecclayout fsmc_ecc1_layout = {
137*4882a593Smuzhiyun 	.eccbytes = 24,
138*4882a593Smuzhiyun 	.eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52,
139*4882a593Smuzhiyun 		66, 67, 68, 82, 83, 84, 98, 99, 100, 114, 115, 116},
140*4882a593Smuzhiyun 	.oobfree = {
141*4882a593Smuzhiyun 		{.offset = 8, .length = 8},
142*4882a593Smuzhiyun 		{.offset = 24, .length = 8},
143*4882a593Smuzhiyun 		{.offset = 40, .length = 8},
144*4882a593Smuzhiyun 		{.offset = 56, .length = 8},
145*4882a593Smuzhiyun 		{.offset = 72, .length = 8},
146*4882a593Smuzhiyun 		{.offset = 88, .length = 8},
147*4882a593Smuzhiyun 		{.offset = 104, .length = 8},
148*4882a593Smuzhiyun 		{.offset = 120, .length = 8}
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun };
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun /* Count the number of 0's in buff upto a max of max_bits */
count_written_bits(uint8_t * buff,int size,int max_bits)153*4882a593Smuzhiyun static int count_written_bits(uint8_t *buff, int size, int max_bits)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun 	int k, written_bits = 0;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	for (k = 0; k < size; k++) {
158*4882a593Smuzhiyun 		written_bits += hweight8(~buff[k]);
159*4882a593Smuzhiyun 		if (written_bits > max_bits)
160*4882a593Smuzhiyun 			break;
161*4882a593Smuzhiyun 	}
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	return written_bits;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun 
fsmc_nand_hwcontrol(struct mtd_info * mtd,int cmd,uint ctrl)166*4882a593Smuzhiyun static void fsmc_nand_hwcontrol(struct mtd_info *mtd, int cmd, uint ctrl)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	struct nand_chip *this = mtd_to_nand(mtd);
169*4882a593Smuzhiyun 	ulong IO_ADDR_W;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	if (ctrl & NAND_CTRL_CHANGE) {
172*4882a593Smuzhiyun 		IO_ADDR_W = (ulong)this->IO_ADDR_W;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 		IO_ADDR_W &= ~(CONFIG_SYS_NAND_CLE | CONFIG_SYS_NAND_ALE);
175*4882a593Smuzhiyun 		if (ctrl & NAND_CLE)
176*4882a593Smuzhiyun 			IO_ADDR_W |= CONFIG_SYS_NAND_CLE;
177*4882a593Smuzhiyun 		if (ctrl & NAND_ALE)
178*4882a593Smuzhiyun 			IO_ADDR_W |= CONFIG_SYS_NAND_ALE;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 		if (ctrl & NAND_NCE) {
181*4882a593Smuzhiyun 			writel(readl(&fsmc_regs_p->pc) |
182*4882a593Smuzhiyun 					FSMC_ENABLE, &fsmc_regs_p->pc);
183*4882a593Smuzhiyun 		} else {
184*4882a593Smuzhiyun 			writel(readl(&fsmc_regs_p->pc) &
185*4882a593Smuzhiyun 					~FSMC_ENABLE, &fsmc_regs_p->pc);
186*4882a593Smuzhiyun 		}
187*4882a593Smuzhiyun 		this->IO_ADDR_W = (void *)IO_ADDR_W;
188*4882a593Smuzhiyun 	}
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	if (cmd != NAND_CMD_NONE)
191*4882a593Smuzhiyun 		writeb(cmd, this->IO_ADDR_W);
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun 
fsmc_bch8_correct_data(struct mtd_info * mtd,u_char * dat,u_char * read_ecc,u_char * calc_ecc)194*4882a593Smuzhiyun static int fsmc_bch8_correct_data(struct mtd_info *mtd, u_char *dat,
195*4882a593Smuzhiyun 		u_char *read_ecc, u_char *calc_ecc)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	/* The calculated ecc is actually the correction index in data */
198*4882a593Smuzhiyun 	u32 err_idx[8];
199*4882a593Smuzhiyun 	u32 num_err, i;
200*4882a593Smuzhiyun 	u32 ecc1, ecc2, ecc3, ecc4;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	num_err = (readl(&fsmc_regs_p->sts) >> 10) & 0xF;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	if (likely(num_err == 0))
205*4882a593Smuzhiyun 		return 0;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	if (unlikely(num_err > 8)) {
208*4882a593Smuzhiyun 		/*
209*4882a593Smuzhiyun 		 * This is a temporary erase check. A newly erased page read
210*4882a593Smuzhiyun 		 * would result in an ecc error because the oob data is also
211*4882a593Smuzhiyun 		 * erased to FF and the calculated ecc for an FF data is not
212*4882a593Smuzhiyun 		 * FF..FF.
213*4882a593Smuzhiyun 		 * This is a workaround to skip performing correction in case
214*4882a593Smuzhiyun 		 * data is FF..FF
215*4882a593Smuzhiyun 		 *
216*4882a593Smuzhiyun 		 * Logic:
217*4882a593Smuzhiyun 		 * For every page, each bit written as 0 is counted until these
218*4882a593Smuzhiyun 		 * number of bits are greater than 8 (the maximum correction
219*4882a593Smuzhiyun 		 * capability of FSMC for each 512 + 13 bytes)
220*4882a593Smuzhiyun 		 */
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 		int bits_ecc = count_written_bits(read_ecc, 13, 8);
223*4882a593Smuzhiyun 		int bits_data = count_written_bits(dat, 512, 8);
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 		if ((bits_ecc + bits_data) <= 8) {
226*4882a593Smuzhiyun 			if (bits_data)
227*4882a593Smuzhiyun 				memset(dat, 0xff, 512);
228*4882a593Smuzhiyun 			return bits_data + bits_ecc;
229*4882a593Smuzhiyun 		}
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 		return -EBADMSG;
232*4882a593Smuzhiyun 	}
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	ecc1 = readl(&fsmc_regs_p->ecc1);
235*4882a593Smuzhiyun 	ecc2 = readl(&fsmc_regs_p->ecc2);
236*4882a593Smuzhiyun 	ecc3 = readl(&fsmc_regs_p->ecc3);
237*4882a593Smuzhiyun 	ecc4 = readl(&fsmc_regs_p->sts);
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	err_idx[0] = (ecc1 >> 0) & 0x1FFF;
240*4882a593Smuzhiyun 	err_idx[1] = (ecc1 >> 13) & 0x1FFF;
241*4882a593Smuzhiyun 	err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
242*4882a593Smuzhiyun 	err_idx[3] = (ecc2 >> 7) & 0x1FFF;
243*4882a593Smuzhiyun 	err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
244*4882a593Smuzhiyun 	err_idx[5] = (ecc3 >> 1) & 0x1FFF;
245*4882a593Smuzhiyun 	err_idx[6] = (ecc3 >> 14) & 0x1FFF;
246*4882a593Smuzhiyun 	err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	i = 0;
249*4882a593Smuzhiyun 	while (i < num_err) {
250*4882a593Smuzhiyun 		err_idx[i] ^= 3;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 		if (err_idx[i] < 512 * 8)
253*4882a593Smuzhiyun 			__change_bit(err_idx[i], dat);
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 		i++;
256*4882a593Smuzhiyun 	}
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	return num_err;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun 
fsmc_read_hwecc(struct mtd_info * mtd,const u_char * data,u_char * ecc)261*4882a593Smuzhiyun static int fsmc_read_hwecc(struct mtd_info *mtd,
262*4882a593Smuzhiyun 			const u_char *data, u_char *ecc)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	u_int ecc_tmp;
265*4882a593Smuzhiyun 	int timeout = CONFIG_SYS_HZ;
266*4882a593Smuzhiyun 	ulong start;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	switch (fsmc_version) {
269*4882a593Smuzhiyun 	case FSMC_VER8:
270*4882a593Smuzhiyun 		start = get_timer(0);
271*4882a593Smuzhiyun 		while (get_timer(start) < timeout) {
272*4882a593Smuzhiyun 			/*
273*4882a593Smuzhiyun 			 * Busy waiting for ecc computation
274*4882a593Smuzhiyun 			 * to finish for 512 bytes
275*4882a593Smuzhiyun 			 */
276*4882a593Smuzhiyun 			if (readl(&fsmc_regs_p->sts) & FSMC_CODE_RDY)
277*4882a593Smuzhiyun 				break;
278*4882a593Smuzhiyun 		}
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 		ecc_tmp = readl(&fsmc_regs_p->ecc1);
281*4882a593Smuzhiyun 		ecc[0] = (u_char) (ecc_tmp >> 0);
282*4882a593Smuzhiyun 		ecc[1] = (u_char) (ecc_tmp >> 8);
283*4882a593Smuzhiyun 		ecc[2] = (u_char) (ecc_tmp >> 16);
284*4882a593Smuzhiyun 		ecc[3] = (u_char) (ecc_tmp >> 24);
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 		ecc_tmp = readl(&fsmc_regs_p->ecc2);
287*4882a593Smuzhiyun 		ecc[4] = (u_char) (ecc_tmp >> 0);
288*4882a593Smuzhiyun 		ecc[5] = (u_char) (ecc_tmp >> 8);
289*4882a593Smuzhiyun 		ecc[6] = (u_char) (ecc_tmp >> 16);
290*4882a593Smuzhiyun 		ecc[7] = (u_char) (ecc_tmp >> 24);
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 		ecc_tmp = readl(&fsmc_regs_p->ecc3);
293*4882a593Smuzhiyun 		ecc[8] = (u_char) (ecc_tmp >> 0);
294*4882a593Smuzhiyun 		ecc[9] = (u_char) (ecc_tmp >> 8);
295*4882a593Smuzhiyun 		ecc[10] = (u_char) (ecc_tmp >> 16);
296*4882a593Smuzhiyun 		ecc[11] = (u_char) (ecc_tmp >> 24);
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 		ecc_tmp = readl(&fsmc_regs_p->sts);
299*4882a593Smuzhiyun 		ecc[12] = (u_char) (ecc_tmp >> 16);
300*4882a593Smuzhiyun 		break;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	default:
303*4882a593Smuzhiyun 		ecc_tmp = readl(&fsmc_regs_p->ecc1);
304*4882a593Smuzhiyun 		ecc[0] = (u_char) (ecc_tmp >> 0);
305*4882a593Smuzhiyun 		ecc[1] = (u_char) (ecc_tmp >> 8);
306*4882a593Smuzhiyun 		ecc[2] = (u_char) (ecc_tmp >> 16);
307*4882a593Smuzhiyun 		break;
308*4882a593Smuzhiyun 	}
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	return 0;
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun 
fsmc_enable_hwecc(struct mtd_info * mtd,int mode)313*4882a593Smuzhiyun void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun 	writel(readl(&fsmc_regs_p->pc) & ~FSMC_ECCPLEN_256,
316*4882a593Smuzhiyun 			&fsmc_regs_p->pc);
317*4882a593Smuzhiyun 	writel(readl(&fsmc_regs_p->pc) & ~FSMC_ECCEN,
318*4882a593Smuzhiyun 			&fsmc_regs_p->pc);
319*4882a593Smuzhiyun 	writel(readl(&fsmc_regs_p->pc) | FSMC_ECCEN,
320*4882a593Smuzhiyun 			&fsmc_regs_p->pc);
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun /*
324*4882a593Smuzhiyun  * fsmc_read_page_hwecc
325*4882a593Smuzhiyun  * @mtd:	mtd info structure
326*4882a593Smuzhiyun  * @chip:	nand chip info structure
327*4882a593Smuzhiyun  * @buf:	buffer to store read data
328*4882a593Smuzhiyun  * @oob_required:	caller expects OOB data read to chip->oob_poi
329*4882a593Smuzhiyun  * @page:	page number to read
330*4882a593Smuzhiyun  *
331*4882a593Smuzhiyun  * This routine is needed for fsmc verison 8 as reading from NAND chip has to be
332*4882a593Smuzhiyun  * performed in a strict sequence as follows:
333*4882a593Smuzhiyun  * data(512 byte) -> ecc(13 byte)
334*4882a593Smuzhiyun  * After this read, fsmc hardware generates and reports error data bits(upto a
335*4882a593Smuzhiyun  * max of 8 bits)
336*4882a593Smuzhiyun  */
fsmc_read_page_hwecc(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)337*4882a593Smuzhiyun static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
338*4882a593Smuzhiyun 				 uint8_t *buf, int oob_required, int page)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun 	struct fsmc_eccplace *fsmc_eccpl;
341*4882a593Smuzhiyun 	int i, j, s, stat, eccsize = chip->ecc.size;
342*4882a593Smuzhiyun 	int eccbytes = chip->ecc.bytes;
343*4882a593Smuzhiyun 	int eccsteps = chip->ecc.steps;
344*4882a593Smuzhiyun 	uint8_t *p = buf;
345*4882a593Smuzhiyun 	uint8_t *ecc_calc = chip->buffers->ecccalc;
346*4882a593Smuzhiyun 	uint8_t *ecc_code = chip->buffers->ecccode;
347*4882a593Smuzhiyun 	int off, len, group = 0;
348*4882a593Smuzhiyun 	uint8_t oob[13] __attribute__ ((aligned (2)));
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	/* Differentiate between small and large page ecc place definitions */
351*4882a593Smuzhiyun 	if (mtd->writesize == 512)
352*4882a593Smuzhiyun 		fsmc_eccpl = &fsmc_eccpl_sp;
353*4882a593Smuzhiyun 	else
354*4882a593Smuzhiyun 		fsmc_eccpl = &fsmc_eccpl_lp;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 		chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
359*4882a593Smuzhiyun 		chip->ecc.hwctl(mtd, NAND_ECC_READ);
360*4882a593Smuzhiyun 		chip->read_buf(mtd, p, eccsize);
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 		for (j = 0; j < eccbytes;) {
363*4882a593Smuzhiyun 			off = fsmc_eccpl->eccplace[group].offset;
364*4882a593Smuzhiyun 			len = fsmc_eccpl->eccplace[group].length;
365*4882a593Smuzhiyun 			group++;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 			/*
368*4882a593Smuzhiyun 			 * length is intentionally kept a higher multiple of 2
369*4882a593Smuzhiyun 			 * to read at least 13 bytes even in case of 16 bit NAND
370*4882a593Smuzhiyun 			 * devices
371*4882a593Smuzhiyun 			 */
372*4882a593Smuzhiyun 			if (chip->options & NAND_BUSWIDTH_16)
373*4882a593Smuzhiyun 				len = roundup(len, 2);
374*4882a593Smuzhiyun 			chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
375*4882a593Smuzhiyun 			chip->read_buf(mtd, oob + j, len);
376*4882a593Smuzhiyun 			j += len;
377*4882a593Smuzhiyun 		}
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 		memcpy(&ecc_code[i], oob, 13);
380*4882a593Smuzhiyun 		chip->ecc.calculate(mtd, p, &ecc_calc[i]);
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 		stat = chip->ecc.correct(mtd, p, &ecc_code[i],
383*4882a593Smuzhiyun 				&ecc_calc[i]);
384*4882a593Smuzhiyun 		if (stat < 0)
385*4882a593Smuzhiyun 			mtd->ecc_stats.failed++;
386*4882a593Smuzhiyun 		else
387*4882a593Smuzhiyun 			mtd->ecc_stats.corrected += stat;
388*4882a593Smuzhiyun 	}
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	return 0;
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun #ifndef CONFIG_SPL_BUILD
394*4882a593Smuzhiyun /*
395*4882a593Smuzhiyun  * fsmc_nand_switch_ecc - switch the ECC operation between different engines
396*4882a593Smuzhiyun  *
397*4882a593Smuzhiyun  * @eccstrength		- the number of bits that could be corrected
398*4882a593Smuzhiyun  *			  (1 - HW, 4 - SW BCH4)
399*4882a593Smuzhiyun  */
fsmc_nand_switch_ecc(uint32_t eccstrength)400*4882a593Smuzhiyun int fsmc_nand_switch_ecc(uint32_t eccstrength)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun 	struct nand_chip *nand;
403*4882a593Smuzhiyun 	struct mtd_info *mtd;
404*4882a593Smuzhiyun 	int err;
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	/*
407*4882a593Smuzhiyun 	 * This functions is only called on SPEAr600 platforms, supporting
408*4882a593Smuzhiyun 	 * 1 bit HW ECC. The BCH8 HW ECC (FSMC_VER8) from the ST-Ericsson
409*4882a593Smuzhiyun 	 * Nomadik SoC is currently supporting this fsmc_nand_switch_ecc()
410*4882a593Smuzhiyun 	 * function, as it doesn't need to switch to a different ECC layout.
411*4882a593Smuzhiyun 	 */
412*4882a593Smuzhiyun 	mtd = get_nand_dev_by_index(nand_curr_device);
413*4882a593Smuzhiyun 	nand = mtd_to_nand(mtd);
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	/* Setup the ecc configurations again */
416*4882a593Smuzhiyun 	if (eccstrength == 1) {
417*4882a593Smuzhiyun 		nand->ecc.mode = NAND_ECC_HW;
418*4882a593Smuzhiyun 		nand->ecc.bytes = 3;
419*4882a593Smuzhiyun 		nand->ecc.strength = 1;
420*4882a593Smuzhiyun 		nand->ecc.layout = &fsmc_ecc1_layout;
421*4882a593Smuzhiyun 		nand->ecc.calculate = fsmc_read_hwecc;
422*4882a593Smuzhiyun 		nand->ecc.correct = nand_correct_data;
423*4882a593Smuzhiyun 	} else if (eccstrength == 4) {
424*4882a593Smuzhiyun 		/*
425*4882a593Smuzhiyun 		 * .calculate .correct and .bytes will be set in
426*4882a593Smuzhiyun 		 * nand_scan_tail()
427*4882a593Smuzhiyun 		 */
428*4882a593Smuzhiyun 		nand->ecc.mode = NAND_ECC_SOFT_BCH;
429*4882a593Smuzhiyun 		nand->ecc.strength = 4;
430*4882a593Smuzhiyun 		nand->ecc.layout = NULL;
431*4882a593Smuzhiyun 	} else {
432*4882a593Smuzhiyun 		printf("Error: ECC strength %d not supported!\n", eccstrength);
433*4882a593Smuzhiyun 	}
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	/* Update NAND handling after ECC mode switch */
436*4882a593Smuzhiyun 	err = nand_scan_tail(mtd);
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	return err;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun #endif /* CONFIG_SPL_BUILD */
441*4882a593Smuzhiyun 
fsmc_nand_init(struct nand_chip * nand)442*4882a593Smuzhiyun int fsmc_nand_init(struct nand_chip *nand)
443*4882a593Smuzhiyun {
444*4882a593Smuzhiyun 	static int chip_nr;
445*4882a593Smuzhiyun 	struct mtd_info *mtd;
446*4882a593Smuzhiyun 	u32 peripid2 = readl(&fsmc_regs_p->peripid2);
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	fsmc_version = (peripid2 >> FSMC_REVISION_SHFT) &
449*4882a593Smuzhiyun 		FSMC_REVISION_MSK;
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	writel(readl(&fsmc_regs_p->ctrl) | FSMC_WP, &fsmc_regs_p->ctrl);
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun #if defined(CONFIG_SYS_FSMC_NAND_16BIT)
454*4882a593Smuzhiyun 	writel(FSMC_DEVWID_16 | FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON,
455*4882a593Smuzhiyun 			&fsmc_regs_p->pc);
456*4882a593Smuzhiyun #elif defined(CONFIG_SYS_FSMC_NAND_8BIT)
457*4882a593Smuzhiyun 	writel(FSMC_DEVWID_8 | FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON,
458*4882a593Smuzhiyun 			&fsmc_regs_p->pc);
459*4882a593Smuzhiyun #else
460*4882a593Smuzhiyun #error Please define CONFIG_SYS_FSMC_NAND_16BIT or CONFIG_SYS_FSMC_NAND_8BIT
461*4882a593Smuzhiyun #endif
462*4882a593Smuzhiyun 	writel(readl(&fsmc_regs_p->pc) | FSMC_TCLR_1 | FSMC_TAR_1,
463*4882a593Smuzhiyun 			&fsmc_regs_p->pc);
464*4882a593Smuzhiyun 	writel(FSMC_THIZ_1 | FSMC_THOLD_4 | FSMC_TWAIT_6 | FSMC_TSET_0,
465*4882a593Smuzhiyun 			&fsmc_regs_p->comm);
466*4882a593Smuzhiyun 	writel(FSMC_THIZ_1 | FSMC_THOLD_4 | FSMC_TWAIT_6 | FSMC_TSET_0,
467*4882a593Smuzhiyun 			&fsmc_regs_p->attrib);
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	nand->options = 0;
470*4882a593Smuzhiyun #if defined(CONFIG_SYS_FSMC_NAND_16BIT)
471*4882a593Smuzhiyun 	nand->options |= NAND_BUSWIDTH_16;
472*4882a593Smuzhiyun #endif
473*4882a593Smuzhiyun 	nand->ecc.mode = NAND_ECC_HW;
474*4882a593Smuzhiyun 	nand->ecc.size = 512;
475*4882a593Smuzhiyun 	nand->ecc.calculate = fsmc_read_hwecc;
476*4882a593Smuzhiyun 	nand->ecc.hwctl = fsmc_enable_hwecc;
477*4882a593Smuzhiyun 	nand->cmd_ctrl = fsmc_nand_hwcontrol;
478*4882a593Smuzhiyun 	nand->IO_ADDR_R = nand->IO_ADDR_W =
479*4882a593Smuzhiyun 		(void  __iomem *)CONFIG_SYS_NAND_BASE;
480*4882a593Smuzhiyun 	nand->badblockbits = 7;
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	mtd = nand_to_mtd(nand);
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	switch (fsmc_version) {
485*4882a593Smuzhiyun 	case FSMC_VER8:
486*4882a593Smuzhiyun 		nand->ecc.bytes = 13;
487*4882a593Smuzhiyun 		nand->ecc.strength = 8;
488*4882a593Smuzhiyun 		nand->ecc.correct = fsmc_bch8_correct_data;
489*4882a593Smuzhiyun 		nand->ecc.read_page = fsmc_read_page_hwecc;
490*4882a593Smuzhiyun 		if (mtd->writesize == 512)
491*4882a593Smuzhiyun 			nand->ecc.layout = &fsmc_ecc4_sp_layout;
492*4882a593Smuzhiyun 		else {
493*4882a593Smuzhiyun 			if (mtd->oobsize == 224)
494*4882a593Smuzhiyun 				nand->ecc.layout = &fsmc_ecc4_224_layout;
495*4882a593Smuzhiyun 			else
496*4882a593Smuzhiyun 				nand->ecc.layout = &fsmc_ecc4_lp_layout;
497*4882a593Smuzhiyun 		}
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 		break;
500*4882a593Smuzhiyun 	default:
501*4882a593Smuzhiyun 		nand->ecc.bytes = 3;
502*4882a593Smuzhiyun 		nand->ecc.strength = 1;
503*4882a593Smuzhiyun 		nand->ecc.layout = &fsmc_ecc1_layout;
504*4882a593Smuzhiyun 		nand->ecc.correct = nand_correct_data;
505*4882a593Smuzhiyun 		break;
506*4882a593Smuzhiyun 	}
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	/* Detect NAND chips */
509*4882a593Smuzhiyun 	if (nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_DEVICE, NULL))
510*4882a593Smuzhiyun 		return -ENXIO;
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 	if (nand_scan_tail(mtd))
513*4882a593Smuzhiyun 		return -ENXIO;
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	if (nand_register(chip_nr++, mtd))
516*4882a593Smuzhiyun 		return -ENXIO;
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	return 0;
519*4882a593Smuzhiyun }
520