blob: ca40c6ac0977fe214cbbe4c2763c821d6804dc1c [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#include <nand.h>
47#include <asm/arch/nand_defs.h>
48#include <asm/arch/emif_defs.h>
49
David Brownell0bf53c42009-04-28 13:19:50 -070050static emif_registers *const emif_regs = (void *) DAVINCI_ASYNC_EMIF_CNTRL_BASE;
51
William Juul52c07962007-10-31 13:53:06 +010052static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Sergey Kubushyne8f39122007-08-10 20:26:18 +020053{
54 struct nand_chip *this = mtd->priv;
55 u_int32_t IO_ADDR_W = (u_int32_t)this->IO_ADDR_W;
56
57 IO_ADDR_W &= ~(MASK_ALE|MASK_CLE);
58
William Juul52c07962007-10-31 13:53:06 +010059 if (ctrl & NAND_CTRL_CHANGE) {
60 if ( ctrl & NAND_CLE )
Sergey Kubushyne8f39122007-08-10 20:26:18 +020061 IO_ADDR_W |= MASK_CLE;
William Juul52c07962007-10-31 13:53:06 +010062 if ( ctrl & NAND_ALE )
Sergey Kubushyne8f39122007-08-10 20:26:18 +020063 IO_ADDR_W |= MASK_ALE;
William Juul52c07962007-10-31 13:53:06 +010064 this->IO_ADDR_W = (void __iomem *) IO_ADDR_W;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020065 }
66
William Juul9e9c2c12007-11-09 13:32:30 +010067 if (cmd != NAND_CMD_NONE)
William Juul52c07962007-10-31 13:53:06 +010068 writeb(cmd, this->IO_ADDR_W);
Sergey Kubushyne8f39122007-08-10 20:26:18 +020069}
70
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020071#ifdef CONFIG_SYS_NAND_HW_ECC
Sergey Kubushyne8f39122007-08-10 20:26:18 +020072
73static void nand_davinci_enable_hwecc(struct mtd_info *mtd, int mode)
74{
Sergey Kubushyne8f39122007-08-10 20:26:18 +020075 int dummy;
76
David Brownell0bf53c42009-04-28 13:19:50 -070077 dummy = emif_regs->NANDF1ECC;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020078
David Brownell0bf53c42009-04-28 13:19:50 -070079 /* FIXME: only chipselect 0 is supported for now */
80 emif_regs->NANDFCR |= 1 << 8;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020081}
82
83static u_int32_t nand_davinci_readecc(struct mtd_info *mtd, u_int32_t region)
84{
85 u_int32_t ecc = 0;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020086
87 if (region == 1)
David Brownell0bf53c42009-04-28 13:19:50 -070088 ecc = emif_regs->NANDF1ECC;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020089 else if (region == 2)
David Brownell0bf53c42009-04-28 13:19:50 -070090 ecc = emif_regs->NANDF2ECC;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020091 else if (region == 3)
David Brownell0bf53c42009-04-28 13:19:50 -070092 ecc = emif_regs->NANDF3ECC;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020093 else if (region == 4)
David Brownell0bf53c42009-04-28 13:19:50 -070094 ecc = emif_regs->NANDF4ECC;
Sergey Kubushyne8f39122007-08-10 20:26:18 +020095
96 return(ecc);
97}
98
99static int nand_davinci_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
100{
101 u_int32_t tmp;
Hugo Villeneuve73dc0e42008-08-30 17:06:55 -0400102 const int region = 1;
103
104 tmp = nand_davinci_readecc(mtd, region);
105
106 /* Squeeze 4 bytes ECC into 3 bytes by removing RESERVED bits
107 * and shifting. RESERVED bits are 31 to 28 and 15 to 12. */
108 tmp = (tmp & 0x00000fff) | ((tmp & 0x0fff0000) >> 4);
109
110 /* Invert so that erased block ECC is correct */
111 tmp = ~tmp;
112
113 *ecc_code++ = tmp;
114 *ecc_code++ = tmp >> 8;
115 *ecc_code++ = tmp >> 16;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200116
David Brownell962d4c12009-04-28 13:19:53 -0700117 /* NOTE: the above code matches mainline Linux:
118 * .PQR.stu ==> ~PQRstu
119 *
120 * MontaVista/TI kernels encode those bytes differently, use
121 * complicated (and allegedly sometimes-wrong) correction code,
122 * and usually shipped with U-Boot that uses software ECC:
123 * .PQR.stu ==> PsQRtu
124 *
125 * If you need MV/TI compatible NAND I/O in U-Boot, it should
126 * be possible to (a) change the mangling above, (b) reverse
127 * that mangling in nand_davinci_correct_data() below.
128 */
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200129
David Brownell962d4c12009-04-28 13:19:53 -0700130 return 0;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200131}
132
133static int nand_davinci_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
134{
Hugo Villeneuve73dc0e42008-08-30 17:06:55 -0400135 struct nand_chip *this = mtd->priv;
Hugo Villeneuve73dc0e42008-08-30 17:06:55 -0400136 u_int32_t ecc_nand = read_ecc[0] | (read_ecc[1] << 8) |
137 (read_ecc[2] << 16);
138 u_int32_t ecc_calc = calc_ecc[0] | (calc_ecc[1] << 8) |
139 (calc_ecc[2] << 16);
140 u_int32_t diff = ecc_calc ^ ecc_nand;
141
142 if (diff) {
143 if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) {
144 /* Correctable error */
145 if ((diff >> (12 + 3)) < this->ecc.size) {
146 uint8_t find_bit = 1 << ((diff >> 12) & 7);
147 uint32_t find_byte = diff >> (12 + 3);
148
149 dat[find_byte] ^= find_bit;
150 MTDDEBUG(MTD_DEBUG_LEVEL0, "Correcting single "
151 "bit ECC error at offset: %d, bit: "
152 "%d\n", find_byte, find_bit);
153 return 1;
154 } else {
155 return -1;
156 }
157 } else if (!(diff & (diff - 1))) {
158 /* Single bit ECC error in the ECC itself,
159 nothing to fix */
160 MTDDEBUG(MTD_DEBUG_LEVEL0, "Single bit ECC error in "
161 "ECC.\n");
162 return 1;
163 } else {
164 /* Uncorrectable error */
165 MTDDEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR 1\n");
166 return -1;
167 }
168 }
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200169 return(0);
170}
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200171#endif /* CONFIG_SYS_NAND_HW_ECC */
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200172
173static int nand_davinci_dev_ready(struct mtd_info *mtd)
174{
David Brownell0bf53c42009-04-28 13:19:50 -0700175 return emif_regs->NANDFSR & 0x1;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200176}
177
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200178static void nand_flash_init(void)
179{
David Brownell0bf53c42009-04-28 13:19:50 -0700180 /* This is for DM6446 EVM and *very* similar. DO NOT GROW THIS!
181 * Instead, have your board_init() set EMIF timings, based on its
182 * knowledge of the clocks and what devices are hooked up ... and
183 * don't even do that unless no UBL handled it.
184 */
185#ifdef CONFIG_SOC_DM6446
Wolfgang Denka48499f2008-04-11 15:11:26 +0200186 u_int32_t acfg1 = 0x3ffffffc;
Wolfgang Denka48499f2008-04-11 15:11:26 +0200187
188 /*------------------------------------------------------------------*
189 * NAND FLASH CHIP TIMEOUT @ 459 MHz *
190 * *
191 * AEMIF.CLK freq = PLL1/6 = 459/6 = 76.5 MHz *
192 * AEMIF.CLK period = 1/76.5 MHz = 13.1 ns *
193 * *
194 *------------------------------------------------------------------*/
195 acfg1 = 0
Wolfgang Denka1be4762008-05-20 16:00:29 +0200196 | (0 << 31 ) /* selectStrobe */
197 | (0 << 30 ) /* extWait */
198 | (1 << 26 ) /* writeSetup 10 ns */
199 | (3 << 20 ) /* writeStrobe 40 ns */
200 | (1 << 17 ) /* writeHold 10 ns */
201 | (1 << 13 ) /* readSetup 10 ns */
202 | (5 << 7 ) /* readStrobe 60 ns */
203 | (1 << 4 ) /* readHold 10 ns */
204 | (3 << 2 ) /* turnAround ?? ns */
205 | (0 << 0 ) /* asyncSize 8-bit bus */
206 ;
Wolfgang Denka48499f2008-04-11 15:11:26 +0200207
Thomas Langeb4aeefd2009-06-20 11:02:17 +0200208 emif_regs->AB1CR = acfg1; /* CS2 */
209
210 emif_regs->NANDFCR = 0x00000101; /* NAND flash on CS2 */
David Brownell0bf53c42009-04-28 13:19:50 -0700211#endif
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200212}
213
David Brownellf4b0b9d2009-05-10 15:43:01 -0700214void davinci_nand_init(struct nand_chip *nand)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200215{
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200216 nand->chip_delay = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200217#ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200218 nand->options = NAND_USE_FLASH_BBT;
219#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200220#ifdef CONFIG_SYS_NAND_HW_ECC
William Juul9e9c2c12007-11-09 13:32:30 +0100221 nand->ecc.mode = NAND_ECC_HW;
William Juul9e9c2c12007-11-09 13:32:30 +0100222 nand->ecc.size = 512;
223 nand->ecc.bytes = 3;
William Juul52c07962007-10-31 13:53:06 +0100224 nand->ecc.calculate = nand_davinci_calculate_ecc;
225 nand->ecc.correct = nand_davinci_correct_data;
William Juulb76ec382007-11-08 10:39:53 +0100226 nand->ecc.hwctl = nand_davinci_enable_hwecc;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200227#else
William Juul9e9c2c12007-11-09 13:32:30 +0100228 nand->ecc.mode = NAND_ECC_SOFT;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200229#endif /* CONFIG_SYS_NAND_HW_ECC */
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200230
231 /* Set address of hardware control function */
William Juul52c07962007-10-31 13:53:06 +0100232 nand->cmd_ctrl = nand_davinci_hwcontrol;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200233
234 nand->dev_ready = nand_davinci_dev_ready;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200235
236 nand_flash_init();
David Brownellf4b0b9d2009-05-10 15:43:01 -0700237}
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200238
David Brownellf4b0b9d2009-05-10 15:43:01 -0700239int board_nand_init(struct nand_chip *chip) __attribute__((weak));
240
241int board_nand_init(struct nand_chip *chip)
242{
243 davinci_nand_init(chip);
244 return 0;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200245}