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