blob: d3c6e51ba80d6267cb245ef85aa6f331a4f602e5 [file] [log] [blame]
Sergey Kubushync74b2102007-08-10 20:26:18 +02001/*
2 * NAND driver for TI DaVinci based boards.
3 *
4 * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
5 *
6 * Based on Linux DaVinci NAND driver by TI. Original copyright follows:
7 */
8
9/*
10 *
11 * linux/drivers/mtd/nand/nand_davinci.c
12 *
13 * NAND Flash Driver
14 *
15 * Copyright (C) 2006 Texas Instruments.
16 *
17 * ----------------------------------------------------------------------------
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 * ----------------------------------------------------------------------------
33 *
34 * Overview:
35 * This is a device driver for the NAND flash device found on the
36 * DaVinci board which utilizes the Samsung k9k2g08 part.
37 *
38 Modifications:
39 ver. 1.0: Feb 2005, Vinod/Sudhakar
40 -
41 *
42 */
43
44#include <common.h>
William Juulcfa460a2007-10-31 13:53:06 +010045#include <asm/io.h>
Sergey Kubushync74b2102007-08-10 20:26:18 +020046#include <nand.h>
47#include <asm/arch/nand_defs.h>
48#include <asm/arch/emif_defs.h>
49
Sandeep Paulraj77b351c2009-08-18 10:10:42 -040050/* Definitions for 4-bit hardware ECC */
51#define NAND_TIMEOUT 10240
52#define NAND_ECC_BUSY 0xC
53#define NAND_4BITECC_MASK 0x03FF03FF
54#define EMIF_NANDFSR_ECC_STATE_MASK 0x00000F00
55#define ECC_STATE_NO_ERR 0x0
56#define ECC_STATE_TOO_MANY_ERRS 0x1
57#define ECC_STATE_ERR_CORR_COMP_P 0x2
58#define ECC_STATE_ERR_CORR_COMP_N 0x3
59
David Brownellfcb77472009-04-28 13:19:50 -070060static emif_registers *const emif_regs = (void *) DAVINCI_ASYNC_EMIF_CNTRL_BASE;
61
Nick Thompson20da6f42009-12-16 11:15:58 +000062/*
63 * Exploit the little endianness of the ARM to do multi-byte transfers
64 * per device read. This can perform over twice as quickly as individual
65 * byte transfers when buffer alignment is conducive.
66 *
67 * NOTE: This only works if the NAND is not connected to the 2 LSBs of
68 * the address bus. On Davinci EVM platforms this has always been true.
69 */
70static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
71{
72 struct nand_chip *chip = mtd->priv;
73 const u32 *nand = chip->IO_ADDR_R;
74
75 /* Make sure that buf is 32 bit aligned */
76 if (((int)buf & 0x3) != 0) {
77 if (((int)buf & 0x1) != 0) {
78 if (len) {
79 *buf = readb(nand);
80 buf += 1;
81 len--;
82 }
83 }
84
85 if (((int)buf & 0x3) != 0) {
86 if (len >= 2) {
87 *(u16 *)buf = readw(nand);
88 buf += 2;
89 len -= 2;
90 }
91 }
92 }
93
94 /* copy aligned data */
95 while (len >= 4) {
96 *(u32 *)buf = readl(nand);
97 buf += 4;
98 len -= 4;
99 }
100
101 /* mop up any remaining bytes */
102 if (len) {
103 if (len >= 2) {
104 *(u16 *)buf = readw(nand);
105 buf += 2;
106 len -= 2;
107 }
108
109 if (len)
110 *buf = readb(nand);
111 }
112}
113
114static void nand_davinci_write_buf(struct mtd_info *mtd, const uint8_t *buf,
115 int len)
116{
117 struct nand_chip *chip = mtd->priv;
118 const u32 *nand = chip->IO_ADDR_W;
119
120 /* Make sure that buf is 32 bit aligned */
121 if (((int)buf & 0x3) != 0) {
122 if (((int)buf & 0x1) != 0) {
123 if (len) {
124 writeb(*buf, nand);
125 buf += 1;
126 len--;
127 }
128 }
129
130 if (((int)buf & 0x3) != 0) {
131 if (len >= 2) {
132 writew(*(u16 *)buf, nand);
133 buf += 2;
134 len -= 2;
135 }
136 }
137 }
138
139 /* copy aligned data */
140 while (len >= 4) {
141 writel(*(u32 *)buf, nand);
142 buf += 4;
143 len -= 4;
144 }
145
146 /* mop up any remaining bytes */
147 if (len) {
148 if (len >= 2) {
149 writew(*(u16 *)buf, nand);
150 buf += 2;
151 len -= 2;
152 }
153
154 if (len)
155 writeb(*buf, nand);
156 }
157}
158
William Juulcfa460a2007-10-31 13:53:06 +0100159static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200160{
161 struct nand_chip *this = mtd->priv;
162 u_int32_t IO_ADDR_W = (u_int32_t)this->IO_ADDR_W;
163
William Juulcfa460a2007-10-31 13:53:06 +0100164 if (ctrl & NAND_CTRL_CHANGE) {
Nick Thompson20da6f42009-12-16 11:15:58 +0000165 IO_ADDR_W &= ~(MASK_ALE|MASK_CLE);
166
William Juulcfa460a2007-10-31 13:53:06 +0100167 if ( ctrl & NAND_CLE )
Sergey Kubushync74b2102007-08-10 20:26:18 +0200168 IO_ADDR_W |= MASK_CLE;
William Juulcfa460a2007-10-31 13:53:06 +0100169 if ( ctrl & NAND_ALE )
Sergey Kubushync74b2102007-08-10 20:26:18 +0200170 IO_ADDR_W |= MASK_ALE;
William Juulcfa460a2007-10-31 13:53:06 +0100171 this->IO_ADDR_W = (void __iomem *) IO_ADDR_W;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200172 }
173
William Juul5e1dae52007-11-09 13:32:30 +0100174 if (cmd != NAND_CMD_NONE)
Nick Thompson20da6f42009-12-16 11:15:58 +0000175 writeb(cmd, IO_ADDR_W);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200176}
177
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200178#ifdef CONFIG_SYS_NAND_HW_ECC
Sergey Kubushync74b2102007-08-10 20:26:18 +0200179
180static void nand_davinci_enable_hwecc(struct mtd_info *mtd, int mode)
181{
Sergey Kubushync74b2102007-08-10 20:26:18 +0200182 int dummy;
183
David Brownellfcb77472009-04-28 13:19:50 -0700184 dummy = emif_regs->NANDF1ECC;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200185
David Brownellfcb77472009-04-28 13:19:50 -0700186 /* FIXME: only chipselect 0 is supported for now */
187 emif_regs->NANDFCR |= 1 << 8;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200188}
189
190static u_int32_t nand_davinci_readecc(struct mtd_info *mtd, u_int32_t region)
191{
192 u_int32_t ecc = 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200193
194 if (region == 1)
David Brownellfcb77472009-04-28 13:19:50 -0700195 ecc = emif_regs->NANDF1ECC;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200196 else if (region == 2)
David Brownellfcb77472009-04-28 13:19:50 -0700197 ecc = emif_regs->NANDF2ECC;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200198 else if (region == 3)
David Brownellfcb77472009-04-28 13:19:50 -0700199 ecc = emif_regs->NANDF3ECC;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200200 else if (region == 4)
David Brownellfcb77472009-04-28 13:19:50 -0700201 ecc = emif_regs->NANDF4ECC;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200202
203 return(ecc);
204}
205
206static int nand_davinci_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
207{
208 u_int32_t tmp;
Hugo Villeneuve9b05aa72008-08-30 17:06:55 -0400209 const int region = 1;
210
211 tmp = nand_davinci_readecc(mtd, region);
212
213 /* Squeeze 4 bytes ECC into 3 bytes by removing RESERVED bits
214 * and shifting. RESERVED bits are 31 to 28 and 15 to 12. */
215 tmp = (tmp & 0x00000fff) | ((tmp & 0x0fff0000) >> 4);
216
217 /* Invert so that erased block ECC is correct */
218 tmp = ~tmp;
219
220 *ecc_code++ = tmp;
221 *ecc_code++ = tmp >> 8;
222 *ecc_code++ = tmp >> 16;
David Brownell6e29ed82009-04-28 13:19:53 -0700223
224 /* NOTE: the above code matches mainline Linux:
225 * .PQR.stu ==> ~PQRstu
226 *
227 * MontaVista/TI kernels encode those bytes differently, use
228 * complicated (and allegedly sometimes-wrong) correction code,
229 * and usually shipped with U-Boot that uses software ECC:
230 * .PQR.stu ==> PsQRtu
231 *
232 * If you need MV/TI compatible NAND I/O in U-Boot, it should
233 * be possible to (a) change the mangling above, (b) reverse
234 * that mangling in nand_davinci_correct_data() below.
235 */
236
237 return 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200238}
239
Sergey Kubushync74b2102007-08-10 20:26:18 +0200240static int nand_davinci_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
241{
Hugo Villeneuve9b05aa72008-08-30 17:06:55 -0400242 struct nand_chip *this = mtd->priv;
Hugo Villeneuve9b05aa72008-08-30 17:06:55 -0400243 u_int32_t ecc_nand = read_ecc[0] | (read_ecc[1] << 8) |
244 (read_ecc[2] << 16);
245 u_int32_t ecc_calc = calc_ecc[0] | (calc_ecc[1] << 8) |
246 (calc_ecc[2] << 16);
247 u_int32_t diff = ecc_calc ^ ecc_nand;
248
249 if (diff) {
250 if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) {
251 /* Correctable error */
252 if ((diff >> (12 + 3)) < this->ecc.size) {
253 uint8_t find_bit = 1 << ((diff >> 12) & 7);
254 uint32_t find_byte = diff >> (12 + 3);
255
256 dat[find_byte] ^= find_bit;
257 MTDDEBUG(MTD_DEBUG_LEVEL0, "Correcting single "
258 "bit ECC error at offset: %d, bit: "
259 "%d\n", find_byte, find_bit);
260 return 1;
261 } else {
262 return -1;
263 }
264 } else if (!(diff & (diff - 1))) {
265 /* Single bit ECC error in the ECC itself,
266 nothing to fix */
267 MTDDEBUG(MTD_DEBUG_LEVEL0, "Single bit ECC error in "
268 "ECC.\n");
269 return 1;
270 } else {
271 /* Uncorrectable error */
272 MTDDEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR 1\n");
273 return -1;
274 }
275 }
Sergey Kubushync74b2102007-08-10 20:26:18 +0200276 return(0);
277}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200278#endif /* CONFIG_SYS_NAND_HW_ECC */
Sergey Kubushync74b2102007-08-10 20:26:18 +0200279
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400280#ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST
281static struct nand_ecclayout nand_davinci_4bit_layout_oobfirst = {
Sandeep Paulraj10a5a7992009-11-19 23:04:42 -0500282#if defined(CONFIG_SYS_NAND_PAGE_2K)
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400283 .eccbytes = 40,
284 .eccpos = {
285 24, 25, 26, 27, 28,
286 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
287 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
288 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
289 59, 60, 61, 62, 63,
290 },
291 .oobfree = {
292 {.offset = 2, .length = 22, },
293 },
Sandeep Paulraj10a5a7992009-11-19 23:04:42 -0500294#elif defined(CONFIG_SYS_NAND_PAGE_4K)
295 .eccbytes = 80,
296 .eccpos = {
297 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
298 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
299 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
300 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
301 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
302 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
303 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
304 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
305 },
306 .oobfree = {
307 {.offset = 2, .length = 46, },
308 },
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400309#endif
310};
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400311
312static void nand_davinci_4bit_enable_hwecc(struct mtd_info *mtd, int mode)
313{
314 u32 val;
315
316 switch (mode) {
317 case NAND_ECC_WRITE:
318 case NAND_ECC_READ:
319 /*
320 * Start a new ECC calculation for reading or writing 512 bytes
321 * of data.
322 */
323 val = (emif_regs->NANDFCR & ~(3 << 4)) | (1 << 12);
324 emif_regs->NANDFCR = val;
325 break;
326 case NAND_ECC_READSYN:
327 val = emif_regs->NAND4BITECC1;
328 break;
329 default:
330 break;
331 }
332}
333
334static u32 nand_davinci_4bit_readecc(struct mtd_info *mtd, unsigned int ecc[4])
335{
336 ecc[0] = emif_regs->NAND4BITECC1 & NAND_4BITECC_MASK;
337 ecc[1] = emif_regs->NAND4BITECC2 & NAND_4BITECC_MASK;
338 ecc[2] = emif_regs->NAND4BITECC3 & NAND_4BITECC_MASK;
339 ecc[3] = emif_regs->NAND4BITECC4 & NAND_4BITECC_MASK;
340
341 return 0;
342}
343
344static int nand_davinci_4bit_calculate_ecc(struct mtd_info *mtd,
345 const uint8_t *dat,
346 uint8_t *ecc_code)
347{
Nick Thompson20da6f42009-12-16 11:15:58 +0000348 unsigned int hw_4ecc[4];
349 unsigned int i;
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400350
351 nand_davinci_4bit_readecc(mtd, hw_4ecc);
352
353 /*Convert 10 bit ecc value to 8 bit */
Nick Thompson20da6f42009-12-16 11:15:58 +0000354 for (i = 0; i < 2; i++) {
355 unsigned int hw_ecc_low = hw_4ecc[i * 2];
356 unsigned int hw_ecc_hi = hw_4ecc[(i * 2) + 1];
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400357
358 /* Take first 8 bits from val1 (count1=0) or val5 (count1=1) */
Nick Thompson20da6f42009-12-16 11:15:58 +0000359 *ecc_code++ = hw_ecc_low & 0xFF;
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400360
361 /*
362 * Take 2 bits as LSB bits from val1 (count1=0) or val5
363 * (count1=1) and 6 bits from val2 (count1=0) or
364 * val5 (count1=1)
365 */
Nick Thompson20da6f42009-12-16 11:15:58 +0000366 *ecc_code++ =
367 ((hw_ecc_low >> 8) & 0x3) | ((hw_ecc_low >> 14) & 0xFC);
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400368
369 /*
370 * Take 4 bits from val2 (count1=0) or val5 (count1=1) and
371 * 4 bits from val3 (count1=0) or val6 (count1=1)
372 */
Nick Thompson20da6f42009-12-16 11:15:58 +0000373 *ecc_code++ =
374 ((hw_ecc_low >> 22) & 0xF) | ((hw_ecc_hi << 4) & 0xF0);
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400375
376 /*
377 * Take 6 bits from val3(count1=0) or val6 (count1=1) and
378 * 2 bits from val4 (count1=0) or val7 (count1=1)
379 */
Nick Thompson20da6f42009-12-16 11:15:58 +0000380 *ecc_code++ =
381 ((hw_ecc_hi >> 4) & 0x3F) | ((hw_ecc_hi >> 10) & 0xC0);
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400382
383 /* Take 8 bits from val4 (count1=0) or val7 (count1=1) */
Nick Thompson20da6f42009-12-16 11:15:58 +0000384 *ecc_code++ = (hw_ecc_hi >> 18) & 0xFF;
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400385 }
Nick Thompson20da6f42009-12-16 11:15:58 +0000386
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400387 return 0;
388}
389
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400390static int nand_davinci_4bit_correct_data(struct mtd_info *mtd, uint8_t *dat,
391 uint8_t *read_ecc, uint8_t *calc_ecc)
392{
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400393 int i;
Nick Thompson20da6f42009-12-16 11:15:58 +0000394 unsigned int hw_4ecc[4];
395 unsigned int iserror;
396 unsigned short *ecc16;
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400397 unsigned int numerrors, erroraddress, errorvalue;
398 u32 val;
399
400 /*
401 * Check for an ECC where all bytes are 0xFF. If this is the case, we
402 * will assume we are looking at an erased page and we should ignore
403 * the ECC.
404 */
405 for (i = 0; i < 10; i++) {
406 if (read_ecc[i] != 0xFF)
407 break;
408 }
409 if (i == 10)
410 return 0;
411
412 /* Convert 8 bit in to 10 bit */
Nick Thompson20da6f42009-12-16 11:15:58 +0000413 ecc16 = (unsigned short *)&read_ecc[0];
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400414
415 /*
416 * Write the parity values in the NAND Flash 4-bit ECC Load register.
417 * Write each parity value one at a time starting from 4bit_ecc_val8
418 * to 4bit_ecc_val1.
419 */
Nick Thompson20da6f42009-12-16 11:15:58 +0000420
421 /*Take 2 bits from 8th byte and 8 bits from 9th byte */
422 writel(((ecc16[4]) >> 6) & 0x3FF, &emif_regs->NAND4BITECCLOAD);
423
424 /* Take 4 bits from 7th byte and 6 bits from 8th byte */
425 writel((((ecc16[3]) >> 12) & 0xF) | ((((ecc16[4])) << 4) & 0x3F0),
426 &emif_regs->NAND4BITECCLOAD);
427
428 /* Take 6 bits from 6th byte and 4 bits from 7th byte */
429 writel((ecc16[3] >> 2) & 0x3FF, &emif_regs->NAND4BITECCLOAD);
430
431 /* Take 8 bits from 5th byte and 2 bits from 6th byte */
432 writel(((ecc16[2]) >> 8) | ((((ecc16[3])) << 8) & 0x300),
433 &emif_regs->NAND4BITECCLOAD);
434
435 /*Take 2 bits from 3rd byte and 8 bits from 4th byte */
436 writel((((ecc16[1]) >> 14) & 0x3) | ((((ecc16[2])) << 2) & 0x3FC),
437 &emif_regs->NAND4BITECCLOAD);
438
439 /* Take 4 bits form 2nd bytes and 6 bits from 3rd bytes */
440 writel(((ecc16[1]) >> 4) & 0x3FF, &emif_regs->NAND4BITECCLOAD);
441
442 /* Take 6 bits from 1st byte and 4 bits from 2nd byte */
443 writel((((ecc16[0]) >> 10) & 0x3F) | (((ecc16[1]) << 6) & 0x3C0),
444 &emif_regs->NAND4BITECCLOAD);
445
446 /* Take 10 bits from 0th and 1st bytes */
447 writel((ecc16[0]) & 0x3FF, &emif_regs->NAND4BITECCLOAD);
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400448
449 /*
450 * Perform a dummy read to the EMIF Revision Code and Status register.
451 * This is required to ensure time for syndrome calculation after
452 * writing the ECC values in previous step.
453 */
454
455 val = emif_regs->NANDFSR;
456
457 /*
458 * Read the syndrome from the NAND Flash 4-Bit ECC 1-4 registers.
459 * A syndrome value of 0 means no bit errors. If the syndrome is
460 * non-zero then go further otherwise return.
461 */
462 nand_davinci_4bit_readecc(mtd, hw_4ecc);
463
Nick Thompson20da6f42009-12-16 11:15:58 +0000464 if (!(hw_4ecc[0] | hw_4ecc[1] | hw_4ecc[2] | hw_4ecc[3]))
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400465 return 0;
466
467 /*
468 * Clear any previous address calculation by doing a dummy read of an
469 * error address register.
470 */
471 val = emif_regs->NANDERRADD1;
472
473 /*
474 * Set the addr_calc_st bit(bit no 13) in the NAND Flash Control
475 * register to 1.
476 */
477 emif_regs->NANDFCR |= 1 << 13;
478
479 /*
480 * Wait for the corr_state field (bits 8 to 11)in the
481 * NAND Flash Status register to be equal to 0x0, 0x1, 0x2, or 0x3.
482 */
483 i = NAND_TIMEOUT;
484 do {
485 val = emif_regs->NANDFSR;
486 val &= 0xc00;
487 i--;
488 } while ((i > 0) && val);
489
490 iserror = emif_regs->NANDFSR;
491 iserror &= EMIF_NANDFSR_ECC_STATE_MASK;
492 iserror = iserror >> 8;
493
494 /*
495 * ECC_STATE_TOO_MANY_ERRS (0x1) means errors cannot be
496 * corrected (five or more errors). The number of errors
497 * calculated (err_num field) differs from the number of errors
498 * searched. ECC_STATE_ERR_CORR_COMP_P (0x2) means error
499 * correction complete (errors on bit 8 or 9).
500 * ECC_STATE_ERR_CORR_COMP_N (0x3) means error correction
501 * complete (error exists).
502 */
503
504 if (iserror == ECC_STATE_NO_ERR) {
505 val = emif_regs->NANDERRVAL1;
506 return 0;
507 } else if (iserror == ECC_STATE_TOO_MANY_ERRS) {
508 val = emif_regs->NANDERRVAL1;
509 return -1;
510 }
511
512 numerrors = ((emif_regs->NANDFSR >> 16) & 0x3) + 1;
513
514 /* Read the error address, error value and correct */
515 for (i = 0; i < numerrors; i++) {
516 if (i > 1) {
517 erroraddress =
518 ((emif_regs->NANDERRADD2 >>
519 (16 * (i & 1))) & 0x3FF);
520 erroraddress = ((512 + 7) - erroraddress);
521 errorvalue =
522 ((emif_regs->NANDERRVAL2 >>
523 (16 * (i & 1))) & 0xFF);
524 } else {
525 erroraddress =
526 ((emif_regs->NANDERRADD1 >>
527 (16 * (i & 1))) & 0x3FF);
528 erroraddress = ((512 + 7) - erroraddress);
529 errorvalue =
530 ((emif_regs->NANDERRVAL1 >>
531 (16 * (i & 1))) & 0xFF);
532 }
533 /* xor the corrupt data with error value */
534 if (erroraddress < 512)
535 dat[erroraddress] ^= errorvalue;
536 }
537
538 return numerrors;
539}
Scott Woodd44e9c12009-09-28 16:33:18 -0500540#endif /* CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST */
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400541
Sergey Kubushync74b2102007-08-10 20:26:18 +0200542static int nand_davinci_dev_ready(struct mtd_info *mtd)
543{
David Brownellfcb77472009-04-28 13:19:50 -0700544 return emif_regs->NANDFSR & 0x1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200545}
546
547static void nand_flash_init(void)
548{
David Brownellfcb77472009-04-28 13:19:50 -0700549 /* This is for DM6446 EVM and *very* similar. DO NOT GROW THIS!
550 * Instead, have your board_init() set EMIF timings, based on its
551 * knowledge of the clocks and what devices are hooked up ... and
552 * don't even do that unless no UBL handled it.
553 */
David Brownelled727d32009-07-13 16:29:04 -0700554#ifdef CONFIG_SOC_DM644X
Wolfgang Denk950a3922008-04-11 15:11:26 +0200555 u_int32_t acfg1 = 0x3ffffffc;
Wolfgang Denk950a3922008-04-11 15:11:26 +0200556
557 /*------------------------------------------------------------------*
558 * NAND FLASH CHIP TIMEOUT @ 459 MHz *
559 * *
560 * AEMIF.CLK freq = PLL1/6 = 459/6 = 76.5 MHz *
561 * AEMIF.CLK period = 1/76.5 MHz = 13.1 ns *
562 * *
563 *------------------------------------------------------------------*/
564 acfg1 = 0
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200565 | (0 << 31 ) /* selectStrobe */
566 | (0 << 30 ) /* extWait */
567 | (1 << 26 ) /* writeSetup 10 ns */
568 | (3 << 20 ) /* writeStrobe 40 ns */
569 | (1 << 17 ) /* writeHold 10 ns */
570 | (1 << 13 ) /* readSetup 10 ns */
571 | (5 << 7 ) /* readStrobe 60 ns */
572 | (1 << 4 ) /* readHold 10 ns */
573 | (3 << 2 ) /* turnAround ?? ns */
574 | (0 << 0 ) /* asyncSize 8-bit bus */
575 ;
Wolfgang Denk950a3922008-04-11 15:11:26 +0200576
Thomas Langed583ef52009-06-20 11:02:17 +0200577 emif_regs->AB1CR = acfg1; /* CS2 */
578
579 emif_regs->NANDFCR = 0x00000101; /* NAND flash on CS2 */
David Brownellfcb77472009-04-28 13:19:50 -0700580#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200581}
582
David Brownell154b5482009-05-10 15:43:01 -0700583void davinci_nand_init(struct nand_chip *nand)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200584{
Sergey Kubushync74b2102007-08-10 20:26:18 +0200585 nand->chip_delay = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200586#ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400587 nand->options |= NAND_USE_FLASH_BBT;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200588#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200589#ifdef CONFIG_SYS_NAND_HW_ECC
William Juul5e1dae52007-11-09 13:32:30 +0100590 nand->ecc.mode = NAND_ECC_HW;
William Juul5e1dae52007-11-09 13:32:30 +0100591 nand->ecc.size = 512;
592 nand->ecc.bytes = 3;
William Juulcfa460a2007-10-31 13:53:06 +0100593 nand->ecc.calculate = nand_davinci_calculate_ecc;
594 nand->ecc.correct = nand_davinci_correct_data;
William Juul4cbb6512007-11-08 10:39:53 +0100595 nand->ecc.hwctl = nand_davinci_enable_hwecc;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200596#else
William Juul5e1dae52007-11-09 13:32:30 +0100597 nand->ecc.mode = NAND_ECC_SOFT;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200598#endif /* CONFIG_SYS_NAND_HW_ECC */
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400599#ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST
600 nand->ecc.mode = NAND_ECC_HW_OOB_FIRST;
601 nand->ecc.size = 512;
602 nand->ecc.bytes = 10;
603 nand->ecc.calculate = nand_davinci_4bit_calculate_ecc;
604 nand->ecc.correct = nand_davinci_4bit_correct_data;
605 nand->ecc.hwctl = nand_davinci_4bit_enable_hwecc;
606 nand->ecc.layout = &nand_davinci_4bit_layout_oobfirst;
607#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200608 /* Set address of hardware control function */
William Juulcfa460a2007-10-31 13:53:06 +0100609 nand->cmd_ctrl = nand_davinci_hwcontrol;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200610
Nick Thompson20da6f42009-12-16 11:15:58 +0000611 nand->read_buf = nand_davinci_read_buf;
612 nand->write_buf = nand_davinci_write_buf;
613
Sergey Kubushync74b2102007-08-10 20:26:18 +0200614 nand->dev_ready = nand_davinci_dev_ready;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200615
616 nand_flash_init();
David Brownell154b5482009-05-10 15:43:01 -0700617}
Sergey Kubushync74b2102007-08-10 20:26:18 +0200618
David Brownell154b5482009-05-10 15:43:01 -0700619int board_nand_init(struct nand_chip *chip) __attribute__((weak));
620
621int board_nand_init(struct nand_chip *chip)
622{
623 davinci_nand_init(chip);
624 return 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200625}