]>
Commit | Line | Data |
---|---|---|
932394ac WD |
1 | /* |
2 | * drivers/mtd/nand.c | |
3 | * | |
4 | * Overview: | |
5 | * This is the generic MTD driver for NAND flash devices. It should be | |
6 | * capable of working with almost all NAND chips currently available. | |
7 | * Basic support for AG-AND chips is provided. | |
ac7eb8a3 | 8 | * |
932394ac WD |
9 | * Additional technical information is available on |
10 | * http://www.linux-mtd.infradead.org/tech/nand.html | |
ac7eb8a3 | 11 | * |
932394ac WD |
12 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
13 | * 2002 Thomas Gleixner (tglx@linutronix.de) | |
14 | * | |
ac7eb8a3 | 15 | * 02-08-2004 tglx: support for strange chips, which cannot auto increment |
932394ac WD |
16 | * pages on read / read_oob |
17 | * | |
18 | * 03-17-2004 tglx: Check ready before auto increment check. Simon Bayes | |
19 | * pointed this out, as he marked an auto increment capable chip | |
20 | * as NOAUTOINCR in the board driver. | |
21 | * Make reads over block boundaries work too | |
22 | * | |
23 | * 04-14-2004 tglx: first working version for 2k page size chips | |
ac7eb8a3 | 24 | * |
932394ac WD |
25 | * 05-19-2004 tglx: Basic support for Renesas AG-AND chips |
26 | * | |
27 | * 09-24-2004 tglx: add support for hardware controllers (e.g. ECC) shared | |
28 | * among multiple independend devices. Suggestions and initial patch | |
29 | * from Ben Dooks <ben-mtd@fluff.org> | |
30 | * | |
31 | * Credits: | |
ac7eb8a3 WD |
32 | * David Woodhouse for adding multichip support |
33 | * | |
932394ac WD |
34 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
35 | * rework for 2K page size chips | |
36 | * | |
37 | * TODO: | |
38 | * Enable cached programming for 2k page size chips | |
39 | * Check, if mtd->ecctype should be set to MTD_ECC_HW | |
40 | * if we have HW ecc support. | |
41 | * The AG-AND chips have nice features for speed improvement, | |
42 | * which are not supported yet. Read / program 4 pages in one go. | |
43 | * | |
44 | * $Id: nand_base.c,v 1.126 2004/12/13 11:22:25 lavinen Exp $ | |
45 | * | |
46 | * This program is free software; you can redistribute it and/or modify | |
47 | * it under the terms of the GNU General Public License version 2 as | |
48 | * published by the Free Software Foundation. | |
49 | * | |
50 | */ | |
51 | ||
52 | /* XXX U-BOOT XXX */ | |
53 | #if 0 | |
54 | #include <linux/delay.h> | |
55 | #include <linux/errno.h> | |
56 | #include <linux/sched.h> | |
57 | #include <linux/slab.h> | |
58 | #include <linux/types.h> | |
59 | #include <linux/mtd/mtd.h> | |
60 | #include <linux/mtd/nand.h> | |
61 | #include <linux/mtd/nand_ecc.h> | |
62 | #include <linux/mtd/compatmac.h> | |
63 | #include <linux/interrupt.h> | |
64 | #include <linux/bitops.h> | |
65 | #include <asm/io.h> | |
66 | ||
67 | #ifdef CONFIG_MTD_PARTITIONS | |
68 | #include <linux/mtd/partitions.h> | |
69 | #endif | |
70 | ||
ac7eb8a3 | 71 | #endif |
932394ac WD |
72 | |
73 | #include <common.h> | |
addb2e16 | 74 | |
6db39708 | 75 | #if (CONFIG_COMMANDS & CFG_CMD_NAND) && !defined(CFG_NAND_LEGACY) |
932394ac WD |
76 | |
77 | #include <malloc.h> | |
78 | #include <watchdog.h> | |
79 | #include <linux/mtd/compat.h> | |
80 | #include <linux/mtd/mtd.h> | |
81 | #include <linux/mtd/nand.h> | |
82 | #include <linux/mtd/nand_ecc.h> | |
83 | ||
84 | #include <asm/io.h> | |
85 | #include <asm/errno.h> | |
86 | ||
87 | #ifdef CONFIG_JFFS2_NAND | |
88 | #include <jffs2/jffs2.h> | |
89 | #endif | |
90 | ||
932394ac WD |
91 | /* Define default oob placement schemes for large and small page devices */ |
92 | static struct nand_oobinfo nand_oob_8 = { | |
93 | .useecc = MTD_NANDECC_AUTOPLACE, | |
94 | .eccbytes = 3, | |
95 | .eccpos = {0, 1, 2}, | |
96 | .oobfree = { {3, 2}, {6, 2} } | |
97 | }; | |
98 | ||
99 | static struct nand_oobinfo nand_oob_16 = { | |
100 | .useecc = MTD_NANDECC_AUTOPLACE, | |
101 | .eccbytes = 6, | |
102 | .eccpos = {0, 1, 2, 3, 6, 7}, | |
103 | .oobfree = { {8, 8} } | |
104 | }; | |
105 | ||
106 | static struct nand_oobinfo nand_oob_64 = { | |
107 | .useecc = MTD_NANDECC_AUTOPLACE, | |
108 | .eccbytes = 24, | |
109 | .eccpos = { | |
ac7eb8a3 WD |
110 | 40, 41, 42, 43, 44, 45, 46, 47, |
111 | 48, 49, 50, 51, 52, 53, 54, 55, | |
932394ac WD |
112 | 56, 57, 58, 59, 60, 61, 62, 63}, |
113 | .oobfree = { {2, 38} } | |
114 | }; | |
115 | ||
116 | /* This is used for padding purposes in nand_write_oob */ | |
117 | static u_char ffchars[] = { | |
118 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
119 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
120 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
121 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
122 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
123 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
124 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
125 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
126 | }; | |
127 | ||
128 | /* | |
129 | * NAND low-level MTD interface functions | |
130 | */ | |
131 | static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len); | |
132 | static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len); | |
133 | static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len); | |
134 | ||
135 | static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf); | |
136 | static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len, | |
137 | size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel); | |
138 | static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf); | |
139 | static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf); | |
140 | static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len, | |
141 | size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel); | |
142 | static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char *buf); | |
143 | /* XXX U-BOOT XXX */ | |
144 | #if 0 | |
145 | static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, | |
146 | unsigned long count, loff_t to, size_t * retlen); | |
147 | static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, | |
148 | unsigned long count, loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel); | |
149 | #endif | |
150 | static int nand_erase (struct mtd_info *mtd, struct erase_info *instr); | |
151 | static void nand_sync (struct mtd_info *mtd); | |
152 | ||
153 | /* Some internal functions */ | |
154 | static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, u_char *oob_buf, | |
155 | struct nand_oobinfo *oobsel, int mode); | |
156 | #ifdef CONFIG_MTD_NAND_VERIFY_WRITE | |
ac7eb8a3 | 157 | static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages, |
932394ac WD |
158 | u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode); |
159 | #else | |
160 | #define nand_verify_pages(...) (0) | |
161 | #endif | |
ac7eb8a3 | 162 | |
932394ac WD |
163 | static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state); |
164 | ||
165 | /** | |
166 | * nand_release_device - [GENERIC] release chip | |
167 | * @mtd: MTD device structure | |
ac7eb8a3 WD |
168 | * |
169 | * Deselect, release chip lock and wake up anyone waiting on the device | |
932394ac WD |
170 | */ |
171 | /* XXX U-BOOT XXX */ | |
172 | #if 0 | |
173 | static void nand_release_device (struct mtd_info *mtd) | |
174 | { | |
175 | struct nand_chip *this = mtd->priv; | |
176 | ||
177 | /* De-select the NAND device */ | |
178 | this->select_chip(mtd, -1); | |
179 | /* Do we have a hardware controller ? */ | |
180 | if (this->controller) { | |
181 | spin_lock(&this->controller->lock); | |
182 | this->controller->active = NULL; | |
183 | spin_unlock(&this->controller->lock); | |
184 | } | |
185 | /* Release the chip */ | |
186 | spin_lock (&this->chip_lock); | |
187 | this->state = FL_READY; | |
188 | wake_up (&this->wq); | |
189 | spin_unlock (&this->chip_lock); | |
190 | } | |
191 | #else | |
8e9655f8 WD |
192 | static void nand_release_device (struct mtd_info *mtd) |
193 | { | |
194 | struct nand_chip *this = mtd->priv; | |
195 | this->select_chip(mtd, -1); /* De-select the NAND device */ | |
196 | } | |
932394ac WD |
197 | #endif |
198 | ||
199 | /** | |
200 | * nand_read_byte - [DEFAULT] read one byte from the chip | |
201 | * @mtd: MTD device structure | |
202 | * | |
203 | * Default read function for 8bit buswith | |
204 | */ | |
205 | static u_char nand_read_byte(struct mtd_info *mtd) | |
206 | { | |
207 | struct nand_chip *this = mtd->priv; | |
208 | return readb(this->IO_ADDR_R); | |
209 | } | |
210 | ||
211 | /** | |
212 | * nand_write_byte - [DEFAULT] write one byte to the chip | |
213 | * @mtd: MTD device structure | |
214 | * @byte: pointer to data byte to write | |
215 | * | |
216 | * Default write function for 8it buswith | |
217 | */ | |
218 | static void nand_write_byte(struct mtd_info *mtd, u_char byte) | |
219 | { | |
220 | struct nand_chip *this = mtd->priv; | |
221 | writeb(byte, this->IO_ADDR_W); | |
222 | } | |
223 | ||
224 | /** | |
225 | * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip | |
226 | * @mtd: MTD device structure | |
227 | * | |
ac7eb8a3 | 228 | * Default read function for 16bit buswith with |
932394ac WD |
229 | * endianess conversion |
230 | */ | |
231 | static u_char nand_read_byte16(struct mtd_info *mtd) | |
232 | { | |
233 | struct nand_chip *this = mtd->priv; | |
234 | return (u_char) cpu_to_le16(readw(this->IO_ADDR_R)); | |
235 | } | |
236 | ||
237 | /** | |
238 | * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip | |
239 | * @mtd: MTD device structure | |
240 | * @byte: pointer to data byte to write | |
241 | * | |
242 | * Default write function for 16bit buswith with | |
243 | * endianess conversion | |
244 | */ | |
245 | static void nand_write_byte16(struct mtd_info *mtd, u_char byte) | |
246 | { | |
247 | struct nand_chip *this = mtd->priv; | |
248 | writew(le16_to_cpu((u16) byte), this->IO_ADDR_W); | |
249 | } | |
250 | ||
251 | /** | |
252 | * nand_read_word - [DEFAULT] read one word from the chip | |
253 | * @mtd: MTD device structure | |
254 | * | |
ac7eb8a3 | 255 | * Default read function for 16bit buswith without |
932394ac WD |
256 | * endianess conversion |
257 | */ | |
258 | static u16 nand_read_word(struct mtd_info *mtd) | |
259 | { | |
260 | struct nand_chip *this = mtd->priv; | |
261 | return readw(this->IO_ADDR_R); | |
262 | } | |
263 | ||
264 | /** | |
265 | * nand_write_word - [DEFAULT] write one word to the chip | |
266 | * @mtd: MTD device structure | |
267 | * @word: data word to write | |
268 | * | |
ac7eb8a3 | 269 | * Default write function for 16bit buswith without |
932394ac WD |
270 | * endianess conversion |
271 | */ | |
272 | static void nand_write_word(struct mtd_info *mtd, u16 word) | |
273 | { | |
274 | struct nand_chip *this = mtd->priv; | |
275 | writew(word, this->IO_ADDR_W); | |
276 | } | |
277 | ||
278 | /** | |
279 | * nand_select_chip - [DEFAULT] control CE line | |
280 | * @mtd: MTD device structure | |
281 | * @chip: chipnumber to select, -1 for deselect | |
282 | * | |
283 | * Default select function for 1 chip devices. | |
284 | */ | |
285 | static void nand_select_chip(struct mtd_info *mtd, int chip) | |
286 | { | |
287 | struct nand_chip *this = mtd->priv; | |
288 | switch(chip) { | |
289 | case -1: | |
ac7eb8a3 | 290 | this->hwcontrol(mtd, NAND_CTL_CLRNCE); |
932394ac WD |
291 | break; |
292 | case 0: | |
293 | this->hwcontrol(mtd, NAND_CTL_SETNCE); | |
294 | break; | |
295 | ||
296 | default: | |
297 | BUG(); | |
298 | } | |
299 | } | |
300 | ||
301 | /** | |
302 | * nand_write_buf - [DEFAULT] write buffer to chip | |
303 | * @mtd: MTD device structure | |
304 | * @buf: data buffer | |
305 | * @len: number of bytes to write | |
306 | * | |
307 | * Default write function for 8bit buswith | |
308 | */ | |
309 | static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len) | |
310 | { | |
311 | int i; | |
312 | struct nand_chip *this = mtd->priv; | |
313 | ||
314 | for (i=0; i<len; i++) | |
315 | writeb(buf[i], this->IO_ADDR_W); | |
316 | } | |
317 | ||
318 | /** | |
ac7eb8a3 | 319 | * nand_read_buf - [DEFAULT] read chip data into buffer |
932394ac WD |
320 | * @mtd: MTD device structure |
321 | * @buf: buffer to store date | |
322 | * @len: number of bytes to read | |
323 | * | |
324 | * Default read function for 8bit buswith | |
325 | */ | |
326 | static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) | |
327 | { | |
328 | int i; | |
329 | struct nand_chip *this = mtd->priv; | |
330 | ||
331 | for (i=0; i<len; i++) | |
332 | buf[i] = readb(this->IO_ADDR_R); | |
333 | } | |
334 | ||
335 | /** | |
ac7eb8a3 | 336 | * nand_verify_buf - [DEFAULT] Verify chip data against buffer |
932394ac WD |
337 | * @mtd: MTD device structure |
338 | * @buf: buffer containing the data to compare | |
339 | * @len: number of bytes to compare | |
340 | * | |
341 | * Default verify function for 8bit buswith | |
342 | */ | |
343 | static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len) | |
344 | { | |
345 | int i; | |
346 | struct nand_chip *this = mtd->priv; | |
347 | ||
348 | for (i=0; i<len; i++) | |
349 | if (buf[i] != readb(this->IO_ADDR_R)) | |
350 | return -EFAULT; | |
351 | ||
352 | return 0; | |
353 | } | |
354 | ||
355 | /** | |
356 | * nand_write_buf16 - [DEFAULT] write buffer to chip | |
357 | * @mtd: MTD device structure | |
358 | * @buf: data buffer | |
359 | * @len: number of bytes to write | |
360 | * | |
361 | * Default write function for 16bit buswith | |
362 | */ | |
363 | static void nand_write_buf16(struct mtd_info *mtd, const u_char *buf, int len) | |
364 | { | |
365 | int i; | |
366 | struct nand_chip *this = mtd->priv; | |
367 | u16 *p = (u16 *) buf; | |
368 | len >>= 1; | |
ac7eb8a3 | 369 | |
932394ac WD |
370 | for (i=0; i<len; i++) |
371 | writew(p[i], this->IO_ADDR_W); | |
ac7eb8a3 | 372 | |
932394ac WD |
373 | } |
374 | ||
375 | /** | |
ac7eb8a3 | 376 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
932394ac WD |
377 | * @mtd: MTD device structure |
378 | * @buf: buffer to store date | |
379 | * @len: number of bytes to read | |
380 | * | |
381 | * Default read function for 16bit buswith | |
382 | */ | |
383 | static void nand_read_buf16(struct mtd_info *mtd, u_char *buf, int len) | |
384 | { | |
385 | int i; | |
386 | struct nand_chip *this = mtd->priv; | |
387 | u16 *p = (u16 *) buf; | |
388 | len >>= 1; | |
389 | ||
390 | for (i=0; i<len; i++) | |
391 | p[i] = readw(this->IO_ADDR_R); | |
392 | } | |
393 | ||
394 | /** | |
ac7eb8a3 | 395 | * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer |
932394ac WD |
396 | * @mtd: MTD device structure |
397 | * @buf: buffer containing the data to compare | |
398 | * @len: number of bytes to compare | |
399 | * | |
400 | * Default verify function for 16bit buswith | |
401 | */ | |
402 | static int nand_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len) | |
403 | { | |
404 | int i; | |
405 | struct nand_chip *this = mtd->priv; | |
406 | u16 *p = (u16 *) buf; | |
407 | len >>= 1; | |
408 | ||
409 | for (i=0; i<len; i++) | |
410 | if (p[i] != readw(this->IO_ADDR_R)) | |
411 | return -EFAULT; | |
412 | ||
413 | return 0; | |
414 | } | |
415 | ||
416 | /** | |
417 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip | |
418 | * @mtd: MTD device structure | |
419 | * @ofs: offset from device start | |
420 | * @getchip: 0, if the chip is already selected | |
421 | * | |
ac7eb8a3 | 422 | * Check, if the block is bad. |
932394ac WD |
423 | */ |
424 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip) | |
425 | { | |
426 | int page, chipnr, res = 0; | |
427 | struct nand_chip *this = mtd->priv; | |
428 | u16 bad; | |
429 | ||
430 | if (getchip) { | |
431 | page = (int)(ofs >> this->page_shift); | |
432 | chipnr = (int)(ofs >> this->chip_shift); | |
433 | ||
434 | /* Grab the lock and see if the device is available */ | |
435 | nand_get_device (this, mtd, FL_READING); | |
436 | ||
437 | /* Select the NAND device */ | |
438 | this->select_chip(mtd, chipnr); | |
ac7eb8a3 WD |
439 | } else |
440 | page = (int) ofs; | |
932394ac WD |
441 | |
442 | if (this->options & NAND_BUSWIDTH_16) { | |
443 | this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page & this->pagemask); | |
444 | bad = cpu_to_le16(this->read_word(mtd)); | |
445 | if (this->badblockpos & 0x1) | |
446 | bad >>= 1; | |
447 | if ((bad & 0xFF) != 0xff) | |
448 | res = 1; | |
449 | } else { | |
450 | this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos, page & this->pagemask); | |
451 | if (this->read_byte(mtd) != 0xff) | |
452 | res = 1; | |
453 | } | |
ac7eb8a3 | 454 | |
932394ac WD |
455 | if (getchip) { |
456 | /* Deselect and wake up anyone waiting on the device */ | |
457 | nand_release_device(mtd); | |
ac7eb8a3 WD |
458 | } |
459 | ||
932394ac WD |
460 | return res; |
461 | } | |
462 | ||
463 | /** | |
464 | * nand_default_block_markbad - [DEFAULT] mark a block bad | |
465 | * @mtd: MTD device structure | |
466 | * @ofs: offset from device start | |
467 | * | |
468 | * This is the default implementation, which can be overridden by | |
469 | * a hardware specific driver. | |
470 | */ | |
471 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) | |
472 | { | |
473 | struct nand_chip *this = mtd->priv; | |
474 | u_char buf[2] = {0, 0}; | |
475 | size_t retlen; | |
476 | int block; | |
ac7eb8a3 | 477 | |
932394ac WD |
478 | /* Get block number */ |
479 | block = ((int) ofs) >> this->bbt_erase_shift; | |
480 | this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1); | |
481 | ||
482 | /* Do we have a flash based bad block table ? */ | |
483 | if (this->options & NAND_USE_FLASH_BBT) | |
484 | return nand_update_bbt (mtd, ofs); | |
ac7eb8a3 | 485 | |
932394ac WD |
486 | /* We write two bytes, so we dont have to mess with 16 bit access */ |
487 | ofs += mtd->oobsize + (this->badblockpos & ~0x01); | |
488 | return nand_write_oob (mtd, ofs , 2, &retlen, buf); | |
489 | } | |
490 | ||
ac7eb8a3 | 491 | /** |
932394ac WD |
492 | * nand_check_wp - [GENERIC] check if the chip is write protected |
493 | * @mtd: MTD device structure | |
ac7eb8a3 | 494 | * Check, if the device is write protected |
932394ac | 495 | * |
ac7eb8a3 | 496 | * The function expects, that the device is already selected |
932394ac WD |
497 | */ |
498 | static int nand_check_wp (struct mtd_info *mtd) | |
499 | { | |
500 | struct nand_chip *this = mtd->priv; | |
501 | /* Check the WP bit */ | |
502 | this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1); | |
ac7eb8a3 | 503 | return (this->read_byte(mtd) & 0x80) ? 0 : 1; |
932394ac | 504 | } |