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