3 * Vipin Kumar, ST Microelectronics, vipin.kumar@st.com.
6 * Amit Virdi, ST Microelectronics, amit.virdi@st.com.
8 * See file CREDITS for list of people who contributed to this
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.
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.
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,
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>
36 static u32 fsmc_version;
37 static struct fsmc_regs *const fsmc_regs_p = (struct fsmc_regs *)
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
46 static struct nand_ecclayout fsmc_ecc4_lp_layout = {
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
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}
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.
82 static struct nand_ecclayout fsmc_ecc4_224_layout = {
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
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}
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
122 static struct fsmc_eccplace fsmc_eccpl_lp = {
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}
135 static struct nand_ecclayout fsmc_ecc4_sp_layout = {
137 .eccpos = { 0, 1, 2, 3, 6, 7, 8,
138 9, 10, 11, 12, 13, 14
141 {.offset = 15, .length = 1},
145 static struct fsmc_eccplace fsmc_eccpl_sp = {
147 {.offset = 0, .length = 4},
148 {.offset = 6, .length = 9}
152 static struct nand_ecclayout fsmc_ecc1_layout = {
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},
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}
168 /* Count the number of 0's in buff upto a max of max_bits */
169 static int count_written_bits(uint8_t *buff, int size, int max_bits)
171 int k, written_bits = 0;
173 for (k = 0; k < size; k++) {
174 written_bits += hweight8(~buff[k]);
175 if (written_bits > max_bits)
182 static void fsmc_nand_hwcontrol(struct mtd_info *mtd, int cmd, uint ctrl)
184 struct nand_chip *this = mtd->priv;
187 if (ctrl & NAND_CTRL_CHANGE) {
188 IO_ADDR_W = (ulong)this->IO_ADDR_W;
190 IO_ADDR_W &= ~(CONFIG_SYS_NAND_CLE | CONFIG_SYS_NAND_ALE);
192 IO_ADDR_W |= CONFIG_SYS_NAND_CLE;
194 IO_ADDR_W |= CONFIG_SYS_NAND_ALE;
196 if (ctrl & NAND_NCE) {
197 writel(readl(&fsmc_regs_p->pc) |
198 FSMC_ENABLE, &fsmc_regs_p->pc);
200 writel(readl(&fsmc_regs_p->pc) &
201 ~FSMC_ENABLE, &fsmc_regs_p->pc);
203 this->IO_ADDR_W = (void *)IO_ADDR_W;
206 if (cmd != NAND_CMD_NONE)
207 writeb(cmd, this->IO_ADDR_W);
210 static int fsmc_bch8_correct_data(struct mtd_info *mtd, u_char *dat,
211 u_char *read_ecc, u_char *calc_ecc)
213 /* The calculated ecc is actually the correction index in data */
216 u32 ecc1, ecc2, ecc3, ecc4;
218 num_err = (readl(&fsmc_regs_p->sts) >> 10) & 0xF;
220 if (likely(num_err == 0))
223 if (unlikely(num_err > 8)) {
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
229 * This is a workaround to skip performing correction in case
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)
238 int bits_ecc = count_written_bits(read_ecc, 13, 8);
239 int bits_data = count_written_bits(dat, 512, 8);
241 if ((bits_ecc + bits_data) <= 8) {
243 memset(dat, 0xff, 512);
244 return bits_data + bits_ecc;
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);
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);
265 while (i < num_err) {
268 if (err_idx[i] < 512 * 8)
269 __change_bit(err_idx[i], dat);
277 static int fsmc_read_hwecc(struct mtd_info *mtd,
278 const u_char *data, u_char *ecc)
281 int timeout = CONFIG_SYS_HZ;
284 switch (fsmc_version) {
286 start = get_timer(0);
287 while (get_timer(start) < timeout) {
289 * Busy waiting for ecc computation
290 * to finish for 512 bytes
292 if (readl(&fsmc_regs_p->sts) & FSMC_CODE_RDY)
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);
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);
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);
314 ecc_tmp = readl(&fsmc_regs_p->sts);
315 ecc[12] = (u_char) (ecc_tmp >> 16);
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);
329 void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
331 writel(readl(&fsmc_regs_p->pc) & ~FSMC_ECCPLEN_256,
333 writel(readl(&fsmc_regs_p->pc) & ~FSMC_ECCEN,
335 writel(readl(&fsmc_regs_p->pc) | FSMC_ECCEN,
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
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
352 static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
353 uint8_t *buf, int page)
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;
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)));
365 /* Differentiate between small and large page ecc place definitions */
366 if (mtd->writesize == 512)
367 fsmc_eccpl = &fsmc_eccpl_sp;
369 fsmc_eccpl = &fsmc_eccpl_lp;
371 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
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);
377 for (j = 0; j < eccbytes;) {
378 off = fsmc_eccpl->eccplace[group].offset;
379 len = fsmc_eccpl->eccplace[group].length;
383 * length is intentionally kept a higher multiple of 2
384 * to read at least 13 bytes even in case of 16 bit NAND
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);
394 memcpy(&ecc_code[i], oob, 13);
395 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
397 stat = chip->ecc.correct(mtd, p, &ecc_code[i],
400 mtd->ecc_stats.failed++;
402 mtd->ecc_stats.corrected += stat;
408 int fsmc_nand_init(struct nand_chip *nand)
411 struct mtd_info *mtd;
413 u32 peripid2 = readl(&fsmc_regs_p->peripid2);
415 fsmc_version = (peripid2 >> FSMC_REVISION_SHFT) &
418 writel(readl(&fsmc_regs_p->ctrl) | FSMC_WP, &fsmc_regs_p->ctrl);
420 #if defined(CONFIG_SYS_FSMC_NAND_16BIT)
421 writel(FSMC_DEVWID_16 | FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON,
423 #elif defined(CONFIG_SYS_FSMC_NAND_8BIT)
424 writel(FSMC_DEVWID_8 | FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON,
427 #error Please define CONFIG_SYS_FSMC_NAND_16BIT or CONFIG_SYS_FSMC_NAND_8BIT
429 writel(readl(&fsmc_regs_p->pc) | FSMC_TCLR_1 | FSMC_TAR_1,
431 writel(FSMC_THIZ_1 | FSMC_THOLD_4 | FSMC_TWAIT_6 | FSMC_TSET_0,
433 writel(FSMC_THIZ_1 | FSMC_THOLD_4 | FSMC_TWAIT_6 | FSMC_TSET_0,
434 &fsmc_regs_p->attrib);
437 #if defined(CONFIG_SYS_FSMC_NAND_16BIT)
438 nand->options |= NAND_BUSWIDTH_16;
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;
449 mtd = &nand_info[chip_nr++];
452 switch (fsmc_version) {
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;
460 if (mtd->oobsize == 224)
461 nand->ecc.layout = &fsmc_ecc4_224_layout;
463 nand->ecc.layout = &fsmc_ecc4_lp_layout;
469 nand->ecc.layout = &fsmc_ecc1_layout;
470 nand->ecc.correct = nand_correct_data;
474 /* Detect NAND chips */
475 if (nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_DEVICE, NULL))
478 if (nand_scan_tail(mtd))
481 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
482 if (nand_register(i))