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