1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <common.h>
29
30#include <nand.h>
31#include <asm/arch/s3c6400.h>
32
33#include <asm/io.h>
34#include <asm/errno.h>
35
36#define MAX_CHIPS 2
37static int nand_cs[MAX_CHIPS] = {0, 1};
38
39#ifdef CONFIG_NAND_SPL
40#define printf(arg...) do {} while (0)
41#endif
42
43
44#ifdef S3C_NAND_DEBUG
45
46
47
48
49static void print_oob(const char *header, struct mtd_info *mtd)
50{
51 int i;
52 struct nand_chip *chip = mtd->priv;
53
54 printf("%s:\t", header);
55
56 for (i = 0; i < 64; i++)
57 printf("%02x ", chip->oob_poi[i]);
58
59 printf("\n");
60}
61#endif
62
63#ifdef CONFIG_NAND_SPL
64static u_char nand_read_byte(struct mtd_info *mtd)
65{
66 struct nand_chip *this = mtd->priv;
67 return readb(this->IO_ADDR_R);
68}
69
70static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
71{
72 int i;
73 struct nand_chip *this = mtd->priv;
74
75 for (i = 0; i < len; i++)
76 writeb(buf[i], this->IO_ADDR_W);
77}
78
79static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
80{
81 int i;
82 struct nand_chip *this = mtd->priv;
83
84 for (i = 0; i < len; i++)
85 buf[i] = readb(this->IO_ADDR_R);
86}
87#endif
88
89static void s3c_nand_select_chip(struct mtd_info *mtd, int chip)
90{
91 int ctrl = readl(NFCONT);
92
93 switch (chip) {
94 case -1:
95 ctrl |= 6;
96 break;
97 case 0:
98 ctrl &= ~2;
99 break;
100 case 1:
101 ctrl &= ~4;
102 break;
103 default:
104 return;
105 }
106
107 writel(ctrl, NFCONT);
108}
109
110
111
112
113
114static void s3c_nand_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
115{
116 struct nand_chip *this = mtd->priv;
117
118 if (ctrl & NAND_CTRL_CHANGE) {
119 if (ctrl & NAND_CLE)
120 this->IO_ADDR_W = (void __iomem *)NFCMMD;
121 else if (ctrl & NAND_ALE)
122 this->IO_ADDR_W = (void __iomem *)NFADDR;
123 else
124 this->IO_ADDR_W = (void __iomem *)NFDATA;
125 if (ctrl & NAND_NCE)
126 s3c_nand_select_chip(mtd, *(int *)this->priv);
127 else
128 s3c_nand_select_chip(mtd, -1);
129 }
130
131 if (cmd != NAND_CMD_NONE)
132 writeb(cmd, this->IO_ADDR_W);
133}
134
135
136
137
138
139static int s3c_nand_device_ready(struct mtd_info *mtdinfo)
140{
141 return !!(readl(NFSTAT) & NFSTAT_RnB);
142}
143
144#ifdef CONFIG_SYS_S3C_NAND_HWECC
145
146
147
148
149static void s3c_nand_enable_hwecc(struct mtd_info *mtd, int mode)
150{
151 u_long nfcont, nfconf;
152
153
154
155
156
157
158 nfconf = readl(NFCONF) & ~NFCONF_ECC_4BIT;
159
160 writel(nfconf, NFCONF);
161
162
163 nfcont = readl(NFCONT);
164 nfcont |= NFCONT_INITECC;
165 nfcont &= ~NFCONT_MECCLOCK;
166
167 if (mode == NAND_ECC_WRITE)
168 nfcont |= NFCONT_ECC_ENC;
169 else if (mode == NAND_ECC_READ)
170 nfcont &= ~NFCONT_ECC_ENC;
171
172 writel(nfcont, NFCONT);
173}
174
175
176
177
178
179
180static int s3c_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
181 u_char *ecc_code)
182{
183 u_long nfcont, nfmecc0;
184
185
186 nfcont = readl(NFCONT);
187 nfcont |= NFCONT_MECCLOCK;
188 writel(nfcont, NFCONT);
189
190 nfmecc0 = readl(NFMECC0);
191
192 ecc_code[0] = nfmecc0 & 0xff;
193 ecc_code[1] = (nfmecc0 >> 8) & 0xff;
194 ecc_code[2] = (nfmecc0 >> 16) & 0xff;
195 ecc_code[3] = (nfmecc0 >> 24) & 0xff;
196
197 return 0;
198}
199
200
201
202
203
204
205
206
207
208
209static int s3c_nand_correct_data(struct mtd_info *mtd, u_char *dat,
210 u_char *read_ecc, u_char *calc_ecc)
211{
212 int ret = -1;
213 u_long nfestat0, nfmeccdata0, nfmeccdata1, err_byte_addr;
214 u_char err_type, repaired;
215
216
217 nfmeccdata0 = (calc_ecc[1] << 16) | calc_ecc[0];
218 nfmeccdata1 = (calc_ecc[3] << 16) | calc_ecc[2];
219 writel(nfmeccdata0, NFMECCDATA0);
220 writel(nfmeccdata1, NFMECCDATA1);
221
222
223 nfestat0 = readl(NFESTAT0);
224 err_type = nfestat0 & 0x3;
225
226 switch (err_type) {
227 case 0:
228 ret = 0;
229 break;
230
231 case 1:
232
233
234
235
236
237 err_byte_addr = (nfestat0 >> 7) & 0x7ff;
238 repaired = dat[err_byte_addr] ^ (1 << ((nfestat0 >> 4) & 0x7));
239
240 printf("S3C NAND: 1 bit error detected at byte %ld. "
241 "Correcting from 0x%02x to 0x%02x...OK\n",
242 err_byte_addr, dat[err_byte_addr], repaired);
243
244 dat[err_byte_addr] = repaired;
245
246 ret = 1;
247 break;
248
249 case 2:
250 case 3:
251 printf("S3C NAND: ECC uncorrectable error detected. "
252 "Not correctable.\n");
253 ret = -1;
254 break;
255 }
256
257 return ret;
258}
259#endif
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279int board_nand_init(struct nand_chip *nand)
280{
281 static int chip_n;
282
283 if (chip_n >= MAX_CHIPS)
284 return -ENODEV;
285
286 NFCONT_REG = (NFCONT_REG & ~NFCONT_WP) | NFCONT_ENABLE | 0x6;
287
288 nand->IO_ADDR_R = (void __iomem *)NFDATA;
289 nand->IO_ADDR_W = (void __iomem *)NFDATA;
290 nand->cmd_ctrl = s3c_nand_hwcontrol;
291 nand->dev_ready = s3c_nand_device_ready;
292 nand->select_chip = s3c_nand_select_chip;
293 nand->options = 0;
294#ifdef CONFIG_NAND_SPL
295 nand->read_byte = nand_read_byte;
296 nand->write_buf = nand_write_buf;
297 nand->read_buf = nand_read_buf;
298#endif
299
300#ifdef CONFIG_SYS_S3C_NAND_HWECC
301 nand->ecc.hwctl = s3c_nand_enable_hwecc;
302 nand->ecc.calculate = s3c_nand_calculate_ecc;
303 nand->ecc.correct = s3c_nand_correct_data;
304
305
306
307
308
309 nand->ecc.mode = NAND_ECC_HW;
310 nand->ecc.size = CONFIG_SYS_NAND_ECCSIZE;
311 nand->ecc.bytes = CONFIG_SYS_NAND_ECCBYTES;
312#else
313 nand->ecc.mode = NAND_ECC_SOFT;
314#endif
315
316 nand->priv = nand_cs + chip_n++;
317
318 return 0;
319}
320