blob: 5a1da633d3a6e2a298ef0cd56f1cda66b8d1634c [file] [log] [blame]
Sergey Kubushyne8f39122007-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 Juul52c07962007-10-31 13:53:06 +010045#include <asm/io.h>
Sergey Kubushyne8f39122007-08-10 20:26:18 +020046
47#ifdef CFG_USE_NAND
Jean-Christophe PLAGNIOL-VILLARD719bb5f2008-08-13 01:40:43 +020048#if !defined(CONFIG_NAND_LEGACY)
Sergey Kubushyne8f39122007-08-10 20:26:18 +020049
50#include <nand.h>
51#include <asm/arch/nand_defs.h>
52#include <asm/arch/emif_defs.h>
53
54extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
55
William Juul52c07962007-10-31 13:53:06 +010056static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Sergey Kubushyne8f39122007-08-10 20:26:18 +020057{
58 struct nand_chip *this = mtd->priv;
59 u_int32_t IO_ADDR_W = (u_int32_t)this->IO_ADDR_W;
60
61 IO_ADDR_W &= ~(MASK_ALE|MASK_CLE);
62
William Juul52c07962007-10-31 13:53:06 +010063 if (ctrl & NAND_CTRL_CHANGE) {
64 if ( ctrl & NAND_CLE )
Sergey Kubushyne8f39122007-08-10 20:26:18 +020065 IO_ADDR_W |= MASK_CLE;
William Juul52c07962007-10-31 13:53:06 +010066 if ( ctrl & NAND_ALE )
Sergey Kubushyne8f39122007-08-10 20:26:18 +020067 IO_ADDR_W |= MASK_ALE;
William Juul52c07962007-10-31 13:53:06 +010068 this->IO_ADDR_W = (void __iomem *) IO_ADDR_W;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020069 }
70
William Juul9e9c2c12007-11-09 13:32:30 +010071 if (cmd != NAND_CMD_NONE)
William Juul52c07962007-10-31 13:53:06 +010072 writeb(cmd, this->IO_ADDR_W);
Sergey Kubushyne8f39122007-08-10 20:26:18 +020073}
74
75/* Set WP on deselect, write enable on select */
76static void nand_davinci_select_chip(struct mtd_info *mtd, int chip)
77{
78#define GPIO_SET_DATA01 0x01c67018
79#define GPIO_CLR_DATA01 0x01c6701c
80#define GPIO_NAND_WP (1 << 4)
81#ifdef SONATA_BOARD_GPIOWP
82 if (chip < 0) {
83 REG(GPIO_CLR_DATA01) |= GPIO_NAND_WP;
84 } else {
85 REG(GPIO_SET_DATA01) |= GPIO_NAND_WP;
86 }
87#endif
88}
89
90#ifdef CFG_NAND_HW_ECC
91#ifdef CFG_NAND_LARGEPAGE
Sergey Kubushynd7be2ee2008-01-09 15:36:20 +010092static struct nand_ecclayout davinci_nand_ecclayout = {
Sergey Kubushyne8f39122007-08-10 20:26:18 +020093 .eccbytes = 12,
94 .eccpos = {8, 9, 10, 24, 25, 26, 40, 41, 42, 56, 57, 58},
Sergey Kubushynd7be2ee2008-01-09 15:36:20 +010095 .oobfree = {
96 {.offset = 2, .length = 6},
97 {.offset = 12, .length = 12},
98 {.offset = 28, .length = 12},
99 {.offset = 44, .length = 12},
100 {.offset = 60, .length = 4}
101 }
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200102};
103#elif defined(CFG_NAND_SMALLPAGE)
Sergey Kubushynd7be2ee2008-01-09 15:36:20 +0100104static struct nand_ecclayout davinci_nand_ecclayout = {
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200105 .eccbytes = 3,
106 .eccpos = {0, 1, 2},
Sergey Kubushynd7be2ee2008-01-09 15:36:20 +0100107 .oobfree = {
108 {.offset = 6, .length = 2},
109 {.offset = 8, .length = 8}
110 }
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200111};
112#else
113#error "Either CFG_NAND_LARGEPAGE or CFG_NAND_SMALLPAGE must be defined!"
114#endif
115
116static void nand_davinci_enable_hwecc(struct mtd_info *mtd, int mode)
117{
118 emifregs emif_addr;
119 int dummy;
120
121 emif_addr = (emifregs)DAVINCI_ASYNC_EMIF_CNTRL_BASE;
122
123 dummy = emif_addr->NANDF1ECC;
124 dummy = emif_addr->NANDF2ECC;
125 dummy = emif_addr->NANDF3ECC;
126 dummy = emif_addr->NANDF4ECC;
127
Wolfgang Denka48499f2008-04-11 15:11:26 +0200128 emif_addr->NANDFCR |= (1 << 8);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200129}
130
131static u_int32_t nand_davinci_readecc(struct mtd_info *mtd, u_int32_t region)
132{
133 u_int32_t ecc = 0;
134 emifregs emif_base_addr;
135
136 emif_base_addr = (emifregs)DAVINCI_ASYNC_EMIF_CNTRL_BASE;
137
138 if (region == 1)
139 ecc = emif_base_addr->NANDF1ECC;
140 else if (region == 2)
141 ecc = emif_base_addr->NANDF2ECC;
142 else if (region == 3)
143 ecc = emif_base_addr->NANDF3ECC;
144 else if (region == 4)
145 ecc = emif_base_addr->NANDF4ECC;
146
147 return(ecc);
148}
149
150static int nand_davinci_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
151{
152 u_int32_t tmp;
153 int region, n;
154 struct nand_chip *this = mtd->priv;
155
William Juul52c07962007-10-31 13:53:06 +0100156 n = (this->ecc.size/512);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200157
Wolfgang Denka48499f2008-04-11 15:11:26 +0200158 region = 1;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200159 while (n--) {
160 tmp = nand_davinci_readecc(mtd, region);
161 *ecc_code++ = tmp;
162 *ecc_code++ = tmp >> 16;
163 *ecc_code++ = ((tmp >> 8) & 0x0f) | ((tmp >> 20) & 0xf0);
164 region++;
165 }
166 return(0);
167}
168
169static void nand_davinci_gen_true_ecc(u_int8_t *ecc_buf)
170{
171 u_int32_t tmp = ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xf0) << 20) | ((ecc_buf[2] & 0x0f) << 8);
172
173 ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) | P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
174 ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) | P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
175 ecc_buf[2] = ~( P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) | P1e(tmp) | P2048o(tmp) | P2048e(tmp));
176}
177
178static int nand_davinci_compare_ecc(u_int8_t *ecc_nand, u_int8_t *ecc_calc, u_int8_t *page_data)
179{
180 u_int32_t i;
181 u_int8_t tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
182 u_int8_t comp0_bit[8], comp1_bit[8], comp2_bit[8];
183 u_int8_t ecc_bit[24];
184 u_int8_t ecc_sum = 0;
185 u_int8_t find_bit = 0;
186 u_int32_t find_byte = 0;
187 int is_ecc_ff;
188
189 is_ecc_ff = ((*ecc_nand == 0xff) && (*(ecc_nand + 1) == 0xff) && (*(ecc_nand + 2) == 0xff));
190
191 nand_davinci_gen_true_ecc(ecc_nand);
192 nand_davinci_gen_true_ecc(ecc_calc);
193
194 for (i = 0; i <= 2; i++) {
195 *(ecc_nand + i) = ~(*(ecc_nand + i));
196 *(ecc_calc + i) = ~(*(ecc_calc + i));
197 }
198
199 for (i = 0; i < 8; i++) {
200 tmp0_bit[i] = *ecc_nand % 2;
201 *ecc_nand = *ecc_nand / 2;
202 }
203
204 for (i = 0; i < 8; i++) {
205 tmp1_bit[i] = *(ecc_nand + 1) % 2;
206 *(ecc_nand + 1) = *(ecc_nand + 1) / 2;
207 }
208
209 for (i = 0; i < 8; i++) {
210 tmp2_bit[i] = *(ecc_nand + 2) % 2;
211 *(ecc_nand + 2) = *(ecc_nand + 2) / 2;
212 }
213
214 for (i = 0; i < 8; i++) {
215 comp0_bit[i] = *ecc_calc % 2;
216 *ecc_calc = *ecc_calc / 2;
217 }
218
219 for (i = 0; i < 8; i++) {
220 comp1_bit[i] = *(ecc_calc + 1) % 2;
221 *(ecc_calc + 1) = *(ecc_calc + 1) / 2;
222 }
223
224 for (i = 0; i < 8; i++) {
225 comp2_bit[i] = *(ecc_calc + 2) % 2;
226 *(ecc_calc + 2) = *(ecc_calc + 2) / 2;
227 }
228
229 for (i = 0; i< 6; i++)
230 ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
231
232 for (i = 0; i < 8; i++)
233 ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
234
235 for (i = 0; i < 8; i++)
236 ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
237
238 ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
239 ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
240
241 for (i = 0; i < 24; i++)
242 ecc_sum += ecc_bit[i];
243
244 switch (ecc_sum) {
245 case 0:
246 /* Not reached because this function is not called if
247 ECC values are equal */
248 return 0;
249 case 1:
250 /* Uncorrectable error */
Scott Wooddf83c472008-06-20 12:38:57 -0500251 MTDDEBUG (MTD_DEBUG_LEVEL0,
252 "ECC UNCORRECTED_ERROR 1\n");
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200253 return(-1);
254 case 12:
255 /* Correctable error */
256 find_byte = (ecc_bit[23] << 8) +
257 (ecc_bit[21] << 7) +
258 (ecc_bit[19] << 6) +
259 (ecc_bit[17] << 5) +
260 (ecc_bit[15] << 4) +
261 (ecc_bit[13] << 3) +
262 (ecc_bit[11] << 2) +
263 (ecc_bit[9] << 1) +
264 ecc_bit[7];
265
266 find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
267
Scott Wooddf83c472008-06-20 12:38:57 -0500268 MTDDEBUG (MTD_DEBUG_LEVEL0, "Correcting single bit ECC "
269 "error at offset: %d, bit: %d\n",
270 find_byte, find_bit);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200271
272 page_data[find_byte] ^= (1 << find_bit);
273
274 return(0);
275 default:
276 if (is_ecc_ff) {
277 if (ecc_calc[0] == 0 && ecc_calc[1] == 0 && ecc_calc[2] == 0)
278 return(0);
279 }
Scott Wooddf83c472008-06-20 12:38:57 -0500280 MTDDEBUG (MTD_DEBUG_LEVEL0,
281 "UNCORRECTED_ERROR default\n");
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200282 return(-1);
283 }
284}
285
286static int nand_davinci_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
287{
288 struct nand_chip *this;
289 int block_count = 0, i, rc;
290
291 this = mtd->priv;
William Juul52c07962007-10-31 13:53:06 +0100292 block_count = (this->ecc.size/512);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200293 for (i = 0; i < block_count; i++) {
294 if (memcmp(read_ecc, calc_ecc, 3) != 0) {
295 rc = nand_davinci_compare_ecc(read_ecc, calc_ecc, dat);
296 if (rc < 0) {
297 return(rc);
298 }
299 }
300 read_ecc += 3;
301 calc_ecc += 3;
302 dat += 512;
303 }
304 return(0);
305}
306#endif
307
308static int nand_davinci_dev_ready(struct mtd_info *mtd)
309{
310 emifregs emif_addr;
311
312 emif_addr = (emifregs)DAVINCI_ASYNC_EMIF_CNTRL_BASE;
313
314 return(emif_addr->NANDFSR & 0x1);
315}
316
William Juul52c07962007-10-31 13:53:06 +0100317static int nand_davinci_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200318{
319 while(!nand_davinci_dev_ready(mtd)) {;}
320 *NAND_CE0CLE = NAND_STATUS;
321 return(*NAND_CE0DATA);
322}
323
324static void nand_flash_init(void)
325{
Wolfgang Denka48499f2008-04-11 15:11:26 +0200326 u_int32_t acfg1 = 0x3ffffffc;
327 u_int32_t acfg2 = 0x3ffffffc;
328 u_int32_t acfg3 = 0x3ffffffc;
329 u_int32_t acfg4 = 0x3ffffffc;
330 emifregs emif_regs;
331
332 /*------------------------------------------------------------------*
333 * NAND FLASH CHIP TIMEOUT @ 459 MHz *
334 * *
335 * AEMIF.CLK freq = PLL1/6 = 459/6 = 76.5 MHz *
336 * AEMIF.CLK period = 1/76.5 MHz = 13.1 ns *
337 * *
338 *------------------------------------------------------------------*/
339 acfg1 = 0
Wolfgang Denka1be4762008-05-20 16:00:29 +0200340 | (0 << 31 ) /* selectStrobe */
341 | (0 << 30 ) /* extWait */
342 | (1 << 26 ) /* writeSetup 10 ns */
343 | (3 << 20 ) /* writeStrobe 40 ns */
344 | (1 << 17 ) /* writeHold 10 ns */
345 | (1 << 13 ) /* readSetup 10 ns */
346 | (5 << 7 ) /* readStrobe 60 ns */
347 | (1 << 4 ) /* readHold 10 ns */
348 | (3 << 2 ) /* turnAround ?? ns */
349 | (0 << 0 ) /* asyncSize 8-bit bus */
350 ;
Wolfgang Denka48499f2008-04-11 15:11:26 +0200351
352 emif_regs = (emifregs)DAVINCI_ASYNC_EMIF_CNTRL_BASE;
353
354 emif_regs->AWCCR |= 0x10000000;
355 emif_regs->AB1CR = acfg1; /* 0x08244128 */;
356 emif_regs->AB2CR = acfg2;
357 emif_regs->AB3CR = acfg3;
358 emif_regs->AB4CR = acfg4;
359 emif_regs->NANDFCR = 0x00000101;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200360}
361
362int board_nand_init(struct nand_chip *nand)
363{
364 nand->IO_ADDR_R = (void __iomem *)NAND_CE0DATA;
365 nand->IO_ADDR_W = (void __iomem *)NAND_CE0DATA;
366 nand->chip_delay = 0;
367 nand->select_chip = nand_davinci_select_chip;
368#ifdef CFG_NAND_USE_FLASH_BBT
369 nand->options = NAND_USE_FLASH_BBT;
370#endif
371#ifdef CFG_NAND_HW_ECC
William Juul9e9c2c12007-11-09 13:32:30 +0100372 nand->ecc.mode = NAND_ECC_HW;
Hugo Villeneuve6fcbfd02008-08-19 16:21:03 -0400373#ifdef CFG_NAND_LARGEPAGE
William Juul9e9c2c12007-11-09 13:32:30 +0100374 nand->ecc.size = 2048;
375 nand->ecc.bytes = 12;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200376#elif defined(CFG_NAND_SMALLPAGE)
William Juul9e9c2c12007-11-09 13:32:30 +0100377 nand->ecc.size = 512;
378 nand->ecc.bytes = 3;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200379#else
380#error "Either CFG_NAND_LARGEPAGE or CFG_NAND_SMALLPAGE must be defined!"
381#endif
Sergey Kubushynd7be2ee2008-01-09 15:36:20 +0100382 nand->ecc.layout = &davinci_nand_ecclayout;
William Juul52c07962007-10-31 13:53:06 +0100383 nand->ecc.calculate = nand_davinci_calculate_ecc;
384 nand->ecc.correct = nand_davinci_correct_data;
William Juulb76ec382007-11-08 10:39:53 +0100385 nand->ecc.hwctl = nand_davinci_enable_hwecc;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200386#else
William Juul9e9c2c12007-11-09 13:32:30 +0100387 nand->ecc.mode = NAND_ECC_SOFT;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200388#endif
389
390 /* Set address of hardware control function */
William Juul52c07962007-10-31 13:53:06 +0100391 nand->cmd_ctrl = nand_davinci_hwcontrol;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200392
393 nand->dev_ready = nand_davinci_dev_ready;
394 nand->waitfunc = nand_davinci_waitfunc;
395
396 nand_flash_init();
397
398 return(0);
399}
400
401#else
402#error "U-Boot legacy NAND support not available for DaVinci chips"
403#endif
404#endif /* CFG_USE_NAND */