2 * NAND driver for TI DaVinci based boards.
4 * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
6 * Based on Linux DaVinci NAND driver by TI. Original copyright follows:
11 * linux/drivers/mtd/nand/nand_davinci.c
15 * Copyright (C) 2006 Texas Instruments.
17 * ----------------------------------------------------------------------------
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.
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.
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 * ----------------------------------------------------------------------------
35 * This is a device driver for the NAND flash device found on the
36 * DaVinci board which utilizes the Samsung k9k2g08 part.
39 ver. 1.0: Feb 2005, Vinod/Sudhakar
47 #include <asm/arch/nand_defs.h>
48 #include <asm/arch/emif_defs.h>
50 /* 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
60 static emif_registers *const emif_regs = (void *) DAVINCI_ASYNC_EMIF_CNTRL_BASE;
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.
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.
70 static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
72 struct nand_chip *chip = mtd->priv;
73 const u32 *nand = chip->IO_ADDR_R;
75 /* Make sure that buf is 32 bit aligned */
76 if (((int)buf & 0x3) != 0) {
77 if (((int)buf & 0x1) != 0) {
85 if (((int)buf & 0x3) != 0) {
87 *(u16 *)buf = readw(nand);
94 /* copy aligned data */
96 *(u32 *)buf = readl(nand);
101 /* mop up any remaining bytes */
104 *(u16 *)buf = readw(nand);
114 static void nand_davinci_write_buf(struct mtd_info *mtd, const uint8_t *buf,
117 struct nand_chip *chip = mtd->priv;
118 const u32 *nand = chip->IO_ADDR_W;
120 /* Make sure that buf is 32 bit aligned */
121 if (((int)buf & 0x3) != 0) {
122 if (((int)buf & 0x1) != 0) {
130 if (((int)buf & 0x3) != 0) {
132 writew(*(u16 *)buf, nand);
139 /* copy aligned data */
141 writel(*(u32 *)buf, nand);
146 /* mop up any remaining bytes */
149 writew(*(u16 *)buf, nand);
159 static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
161 struct nand_chip *this = mtd->priv;
162 u_int32_t IO_ADDR_W = (u_int32_t)this->IO_ADDR_W;
164 if (ctrl & NAND_CTRL_CHANGE) {
165 IO_ADDR_W &= ~(MASK_ALE|MASK_CLE);
167 if ( ctrl & NAND_CLE )
168 IO_ADDR_W |= MASK_CLE;
169 if ( ctrl & NAND_ALE )
170 IO_ADDR_W |= MASK_ALE;
171 this->IO_ADDR_W = (void __iomem *) IO_ADDR_W;
174 if (cmd != NAND_CMD_NONE)
175 writeb(cmd, IO_ADDR_W);
178 #ifdef CONFIG_SYS_NAND_HW_ECC
180 static void nand_davinci_enable_hwecc(struct mtd_info *mtd, int mode)
184 dummy = emif_regs->NANDF1ECC;
186 /* FIXME: only chipselect 0 is supported for now */
187 emif_regs->NANDFCR |= 1 << 8;
190 static u_int32_t nand_davinci_readecc(struct mtd_info *mtd, u_int32_t region)
195 ecc = emif_regs->NANDF1ECC;
196 else if (region == 2)
197 ecc = emif_regs->NANDF2ECC;
198 else if (region == 3)
199 ecc = emif_regs->NANDF3ECC;
200 else if (region == 4)
201 ecc = emif_regs->NANDF4ECC;
206 static int nand_davinci_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
209 const int region = 1;
211 tmp = nand_davinci_readecc(mtd, region);
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);
217 /* Invert so that erased block ECC is correct */
221 *ecc_code++ = tmp >> 8;
222 *ecc_code++ = tmp >> 16;
224 /* NOTE: the above code matches mainline Linux:
225 * .PQR.stu ==> ~PQRstu
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
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.
240 static int nand_davinci_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
242 struct nand_chip *this = mtd->priv;
243 u_int32_t ecc_nand = read_ecc[0] | (read_ecc[1] << 8) |
245 u_int32_t ecc_calc = calc_ecc[0] | (calc_ecc[1] << 8) |
247 u_int32_t diff = ecc_calc ^ ecc_nand;
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);
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);
264 } else if (!(diff & (diff - 1))) {
265 /* Single bit ECC error in the ECC itself,
267 MTDDEBUG(MTD_DEBUG_LEVEL0, "Single bit ECC error in "
271 /* Uncorrectable error */
272 MTDDEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR 1\n");
278 #endif /* CONFIG_SYS_NAND_HW_ECC */
280 #ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST
281 static struct nand_ecclayout nand_davinci_4bit_layout_oobfirst = {
282 #if defined(CONFIG_SYS_NAND_PAGE_2K)
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,
292 {.offset = 2, .length = 22, },
294 #elif defined(CONFIG_SYS_NAND_PAGE_4K)
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,
307 {.offset = 2, .length = 46, },
312 static void nand_davinci_4bit_enable_hwecc(struct mtd_info *mtd, int mode)
320 * Start a new ECC calculation for reading or writing 512 bytes
323 val = (emif_regs->NANDFCR & ~(3 << 4)) | (1 << 12);
324 emif_regs->NANDFCR = val;
326 case NAND_ECC_READSYN:
327 val = emif_regs->NAND4BITECC1;
334 static u32 nand_davinci_4bit_readecc(struct mtd_info *mtd, unsigned int ecc[4])
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;
344 static int nand_davinci_4bit_calculate_ecc(struct mtd_info *mtd,
348 unsigned int hw_4ecc[4];
351 nand_davinci_4bit_readecc(mtd, hw_4ecc);
353 /*Convert 10 bit ecc value to 8 bit */
354 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];
358 /* Take first 8 bits from val1 (count1=0) or val5 (count1=1) */
359 *ecc_code++ = hw_ecc_low & 0xFF;
362 * Take 2 bits as LSB bits from val1 (count1=0) or val5
363 * (count1=1) and 6 bits from val2 (count1=0) or
367 ((hw_ecc_low >> 8) & 0x3) | ((hw_ecc_low >> 14) & 0xFC);
370 * Take 4 bits from val2 (count1=0) or val5 (count1=1) and
371 * 4 bits from val3 (count1=0) or val6 (count1=1)
374 ((hw_ecc_low >> 22) & 0xF) | ((hw_ecc_hi << 4) & 0xF0);
377 * Take 6 bits from val3(count1=0) or val6 (count1=1) and
378 * 2 bits from val4 (count1=0) or val7 (count1=1)
381 ((hw_ecc_hi >> 4) & 0x3F) | ((hw_ecc_hi >> 10) & 0xC0);
383 /* Take 8 bits from val4 (count1=0) or val7 (count1=1) */
384 *ecc_code++ = (hw_ecc_hi >> 18) & 0xFF;
390 static int nand_davinci_4bit_correct_data(struct mtd_info *mtd, uint8_t *dat,
391 uint8_t *read_ecc, uint8_t *calc_ecc)
394 unsigned int hw_4ecc[4];
395 unsigned int iserror;
396 unsigned short *ecc16;
397 unsigned int numerrors, erroraddress, errorvalue;
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
405 for (i = 0; i < 10; i++) {
406 if (read_ecc[i] != 0xFF)
412 /* Convert 8 bit in to 10 bit */
413 ecc16 = (unsigned short *)&read_ecc[0];
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
421 /*Take 2 bits from 8th byte and 8 bits from 9th byte */
422 writel(((ecc16[4]) >> 6) & 0x3FF, &emif_regs->NAND4BITECCLOAD);
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);
428 /* Take 6 bits from 6th byte and 4 bits from 7th byte */
429 writel((ecc16[3] >> 2) & 0x3FF, &emif_regs->NAND4BITECCLOAD);
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);
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);
439 /* Take 4 bits form 2nd bytes and 6 bits from 3rd bytes */
440 writel(((ecc16[1]) >> 4) & 0x3FF, &emif_regs->NAND4BITECCLOAD);
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);
446 /* Take 10 bits from 0th and 1st bytes */
447 writel((ecc16[0]) & 0x3FF, &emif_regs->NAND4BITECCLOAD);
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.
455 val = emif_regs->NANDFSR;
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.
462 nand_davinci_4bit_readecc(mtd, hw_4ecc);
464 if (!(hw_4ecc[0] | hw_4ecc[1] | hw_4ecc[2] | hw_4ecc[3]))
468 * Clear any previous address calculation by doing a dummy read of an
469 * error address register.
471 val = emif_regs->NANDERRADD1;
474 * Set the addr_calc_st bit(bit no 13) in the NAND Flash Control
477 emif_regs->NANDFCR |= 1 << 13;
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.
485 val = emif_regs->NANDFSR;
488 } while ((i > 0) && val);
490 iserror = emif_regs->NANDFSR;
491 iserror &= EMIF_NANDFSR_ECC_STATE_MASK;
492 iserror = iserror >> 8;
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).
504 if (iserror == ECC_STATE_NO_ERR) {
505 val = emif_regs->NANDERRVAL1;
507 } else if (iserror == ECC_STATE_TOO_MANY_ERRS) {
508 val = emif_regs->NANDERRVAL1;
512 numerrors = ((emif_regs->NANDFSR >> 16) & 0x3) + 1;
514 /* Read the error address, error value and correct */
515 for (i = 0; i < numerrors; i++) {
518 ((emif_regs->NANDERRADD2 >>
519 (16 * (i & 1))) & 0x3FF);
520 erroraddress = ((512 + 7) - erroraddress);
522 ((emif_regs->NANDERRVAL2 >>
523 (16 * (i & 1))) & 0xFF);
526 ((emif_regs->NANDERRADD1 >>
527 (16 * (i & 1))) & 0x3FF);
528 erroraddress = ((512 + 7) - erroraddress);
530 ((emif_regs->NANDERRVAL1 >>
531 (16 * (i & 1))) & 0xFF);
533 /* xor the corrupt data with error value */
534 if (erroraddress < 512)
535 dat[erroraddress] ^= errorvalue;
540 #endif /* CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST */
542 static int nand_davinci_dev_ready(struct mtd_info *mtd)
544 return emif_regs->NANDFSR & 0x1;
547 static void nand_flash_init(void)
549 /* 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.
554 #ifdef CONFIG_SOC_DM644X
555 u_int32_t acfg1 = 0x3ffffffc;
557 /*------------------------------------------------------------------*
558 * NAND FLASH CHIP TIMEOUT @ 459 MHz *
560 * AEMIF.CLK freq = PLL1/6 = 459/6 = 76.5 MHz *
561 * AEMIF.CLK period = 1/76.5 MHz = 13.1 ns *
563 *------------------------------------------------------------------*/
565 | (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 */
577 emif_regs->AB1CR = acfg1; /* CS2 */
579 emif_regs->NANDFCR = 0x00000101; /* NAND flash on CS2 */
583 void davinci_nand_init(struct nand_chip *nand)
585 nand->chip_delay = 0;
586 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
587 nand->options |= NAND_USE_FLASH_BBT;
589 #ifdef CONFIG_SYS_NAND_HW_ECC
590 nand->ecc.mode = NAND_ECC_HW;
591 nand->ecc.size = 512;
593 nand->ecc.calculate = nand_davinci_calculate_ecc;
594 nand->ecc.correct = nand_davinci_correct_data;
595 nand->ecc.hwctl = nand_davinci_enable_hwecc;
597 nand->ecc.mode = NAND_ECC_SOFT;
598 #endif /* CONFIG_SYS_NAND_HW_ECC */
599 #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;
608 /* Set address of hardware control function */
609 nand->cmd_ctrl = nand_davinci_hwcontrol;
611 nand->read_buf = nand_davinci_read_buf;
612 nand->write_buf = nand_davinci_write_buf;
614 nand->dev_ready = nand_davinci_dev_ready;
619 int board_nand_init(struct nand_chip *chip) __attribute__((weak));
621 int board_nand_init(struct nand_chip *chip)
623 davinci_nand_init(chip);