Commit | Line | Data |
---|---|---|
1da177e4 LT |
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. | |
8 | * | |
9 | * Additional technical information is available on | |
10 | * http://www.linux-mtd.infradead.org/tech/nand.html | |
11 | * | |
12 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) | |
13 | * 2002 Thomas Gleixner (tglx@linutronix.de) | |
14 | * | |
15 | * 02-08-2004 tglx: support for strange chips, which cannot auto increment | |
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 | |
24 | * | |
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 | * | |
30f464b7 DM |
31 | * 12-05-2004 dmarlin: add workaround for Renesas AG-AND chips "disturb" issue. |
32 | * Basically, any block not rewritten may lose data when surrounding blocks | |
33 | * are rewritten many times. JFFS2 ensures this doesn't happen for blocks | |
34 | * it uses, but the Bad Block Table(s) may not be rewritten. To ensure they | |
35 | * do not lose data, force them to be rewritten when some of the surrounding | |
36 | * blocks are erased. Rather than tracking a specific nearby block (which | |
37 | * could itself go bad), use a page address 'mask' to select several blocks | |
38 | * in the same area, and rewrite the BBT when any of them are erased. | |
39 | * | |
40 | * 01-03-2005 dmarlin: added support for the device recovery command sequence for Renesas | |
41 | * AG-AND chips. If there was a sudden loss of power during an erase operation, | |
42 | * a "device recovery" operation must be performed when power is restored | |
43 | * to ensure correct operation. | |
44 | * | |
068e3c0a DM |
45 | * 01-20-2005 dmarlin: added support for optional hardware specific callback routine to |
46 | * perform extra error status checks on erase and write failures. This required | |
47 | * adding a wrapper function for nand_read_ecc. | |
48 | * | |
1da177e4 LT |
49 | * Credits: |
50 | * David Woodhouse for adding multichip support | |
51 | * | |
52 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the | |
53 | * rework for 2K page size chips | |
54 | * | |
55 | * TODO: | |
56 | * Enable cached programming for 2k page size chips | |
57 | * Check, if mtd->ecctype should be set to MTD_ECC_HW | |
58 | * if we have HW ecc support. | |
59 | * The AG-AND chips have nice features for speed improvement, | |
60 | * which are not supported yet. Read / program 4 pages in one go. | |
61 | * | |
19870da7 | 62 | * $Id: nand_base.c,v 1.147 2005/07/15 07:18:06 gleixner Exp $ |
1da177e4 LT |
63 | * |
64 | * This program is free software; you can redistribute it and/or modify | |
65 | * it under the terms of the GNU General Public License version 2 as | |
66 | * published by the Free Software Foundation. | |
67 | * | |
68 | */ | |
69 | ||
70 | #include <linux/delay.h> | |
71 | #include <linux/errno.h> | |
72 | #include <linux/sched.h> | |
73 | #include <linux/slab.h> | |
74 | #include <linux/types.h> | |
75 | #include <linux/mtd/mtd.h> | |
76 | #include <linux/mtd/nand.h> | |
77 | #include <linux/mtd/nand_ecc.h> | |
78 | #include <linux/mtd/compatmac.h> | |
79 | #include <linux/interrupt.h> | |
80 | #include <linux/bitops.h> | |
81 | #include <asm/io.h> | |
82 | ||
83 | #ifdef CONFIG_MTD_PARTITIONS | |
84 | #include <linux/mtd/partitions.h> | |
85 | #endif | |
86 | ||
87 | /* Define default oob placement schemes for large and small page devices */ | |
88 | static struct nand_oobinfo nand_oob_8 = { | |
89 | .useecc = MTD_NANDECC_AUTOPLACE, | |
90 | .eccbytes = 3, | |
91 | .eccpos = {0, 1, 2}, | |
92 | .oobfree = { {3, 2}, {6, 2} } | |
93 | }; | |
94 | ||
95 | static struct nand_oobinfo nand_oob_16 = { | |
96 | .useecc = MTD_NANDECC_AUTOPLACE, | |
97 | .eccbytes = 6, | |
98 | .eccpos = {0, 1, 2, 3, 6, 7}, | |
99 | .oobfree = { {8, 8} } | |
100 | }; | |
101 | ||
102 | static struct nand_oobinfo nand_oob_64 = { | |
103 | .useecc = MTD_NANDECC_AUTOPLACE, | |
104 | .eccbytes = 24, | |
105 | .eccpos = { | |
106 | 40, 41, 42, 43, 44, 45, 46, 47, | |
107 | 48, 49, 50, 51, 52, 53, 54, 55, | |
108 | 56, 57, 58, 59, 60, 61, 62, 63}, | |
109 | .oobfree = { {2, 38} } | |
110 | }; | |
111 | ||
112 | /* This is used for padding purposes in nand_write_oob */ | |
113 | static u_char ffchars[] = { | |
114 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
115 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
116 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
117 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
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 | }; | |
123 | ||
124 | /* | |
125 | * NAND low-level MTD interface functions | |
126 | */ | |
127 | static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len); | |
128 | static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len); | |
129 | static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len); | |
130 | ||
131 | static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf); | |
132 | static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len, | |
133 | size_t * retlen, u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel); | |
134 | static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf); | |
135 | static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf); | |
136 | static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len, | |
137 | size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel); | |
138 | static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char *buf); | |
139 | static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, | |
140 | unsigned long count, loff_t to, size_t * retlen); | |
141 | static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, | |
142 | unsigned long count, loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel); | |
143 | static int nand_erase (struct mtd_info *mtd, struct erase_info *instr); | |
144 | static void nand_sync (struct mtd_info *mtd); | |
145 | ||
146 | /* Some internal functions */ | |
147 | static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, u_char *oob_buf, | |
148 | struct nand_oobinfo *oobsel, int mode); | |
149 | #ifdef CONFIG_MTD_NAND_VERIFY_WRITE | |
150 | static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages, | |
151 | u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode); | |
152 | #else | |
153 | #define nand_verify_pages(...) (0) | |
154 | #endif | |
155 | ||
156 | static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state); | |
157 | ||
158 | /** | |
159 | * nand_release_device - [GENERIC] release chip | |
160 | * @mtd: MTD device structure | |
161 | * | |
162 | * Deselect, release chip lock and wake up anyone waiting on the device | |
163 | */ | |
164 | static void nand_release_device (struct mtd_info *mtd) | |
165 | { | |
166 | struct nand_chip *this = mtd->priv; | |
167 | ||
168 | /* De-select the NAND device */ | |
169 | this->select_chip(mtd, -1); | |
0dfc6246 | 170 | |
1da177e4 | 171 | if (this->controller) { |
0dfc6246 | 172 | /* Release the controller and the chip */ |
1da177e4 LT |
173 | spin_lock(&this->controller->lock); |
174 | this->controller->active = NULL; | |
0dfc6246 TG |
175 | this->state = FL_READY; |
176 | wake_up(&this->controller->wq); | |
1da177e4 | 177 | spin_unlock(&this->controller->lock); |
0dfc6246 TG |
178 | } else { |
179 | /* Release the chip */ | |
180 | spin_lock(&this->chip_lock); | |
181 | this->state = FL_READY; | |
182 | wake_up(&this->wq); | |
183 | spin_unlock(&this->chip_lock); | |
1da177e4 | 184 | } |
1da177e4 LT |
185 | } |
186 | ||
187 | /** | |
188 | * nand_read_byte - [DEFAULT] read one byte from the chip | |
189 | * @mtd: MTD device structure | |
190 | * | |
191 | * Default read function for 8bit buswith | |
192 | */ | |
193 | static u_char nand_read_byte(struct mtd_info *mtd) | |
194 | { | |
195 | struct nand_chip *this = mtd->priv; | |
196 | return readb(this->IO_ADDR_R); | |
197 | } | |
198 | ||
199 | /** | |
200 | * nand_write_byte - [DEFAULT] write one byte to the chip | |
201 | * @mtd: MTD device structure | |
202 | * @byte: pointer to data byte to write | |
203 | * | |
204 | * Default write function for 8it buswith | |
205 | */ | |
206 | static void nand_write_byte(struct mtd_info *mtd, u_char byte) | |
207 | { | |
208 | struct nand_chip *this = mtd->priv; | |
209 | writeb(byte, this->IO_ADDR_W); | |
210 | } | |
211 | ||
212 | /** | |
213 | * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip | |
214 | * @mtd: MTD device structure | |
215 | * | |
216 | * Default read function for 16bit buswith with | |
217 | * endianess conversion | |
218 | */ | |
219 | static u_char nand_read_byte16(struct mtd_info *mtd) | |
220 | { | |
221 | struct nand_chip *this = mtd->priv; | |
222 | return (u_char) cpu_to_le16(readw(this->IO_ADDR_R)); | |
223 | } | |
224 | ||
225 | /** | |
226 | * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip | |
227 | * @mtd: MTD device structure | |
228 | * @byte: pointer to data byte to write | |
229 | * | |
230 | * Default write function for 16bit buswith with | |
231 | * endianess conversion | |
232 | */ | |
233 | static void nand_write_byte16(struct mtd_info *mtd, u_char byte) | |
234 | { | |
235 | struct nand_chip *this = mtd->priv; | |
236 | writew(le16_to_cpu((u16) byte), this->IO_ADDR_W); | |
237 | } | |
238 | ||
239 | /** | |
240 | * nand_read_word - [DEFAULT] read one word from the chip | |
241 | * @mtd: MTD device structure | |
242 | * | |
243 | * Default read function for 16bit buswith without | |
244 | * endianess conversion | |
245 | */ | |
246 | static u16 nand_read_word(struct mtd_info *mtd) | |
247 | { | |
248 | struct nand_chip *this = mtd->priv; | |
249 | return readw(this->IO_ADDR_R); | |
250 | } | |
251 | ||
252 | /** | |
253 | * nand_write_word - [DEFAULT] write one word to the chip | |
254 | * @mtd: MTD device structure | |
255 | * @word: data word to write | |
256 | * | |
257 | * Default write function for 16bit buswith without | |
258 | * endianess conversion | |
259 | */ | |
260 | static void nand_write_word(struct mtd_info *mtd, u16 word) | |
261 | { | |
262 | struct nand_chip *this = mtd->priv; | |
263 | writew(word, this->IO_ADDR_W); | |
264 | } | |
265 | ||
266 | /** | |
267 | * nand_select_chip - [DEFAULT] control CE line | |
268 | * @mtd: MTD device structure | |
269 | * @chip: chipnumber to select, -1 for deselect | |
270 | * | |
271 | * Default select function for 1 chip devices. | |
272 | */ | |
273 | static void nand_select_chip(struct mtd_info *mtd, int chip) | |
274 | { | |
275 | struct nand_chip *this = mtd->priv; | |
276 | switch(chip) { | |
277 | case -1: | |
278 | this->hwcontrol(mtd, NAND_CTL_CLRNCE); | |
279 | break; | |
280 | case 0: | |
281 | this->hwcontrol(mtd, NAND_CTL_SETNCE); | |
282 | break; | |
283 | ||
284 | default: | |
285 | BUG(); | |
286 | } | |
287 | } | |
288 | ||
289 | /** | |
290 | * nand_write_buf - [DEFAULT] write buffer to chip | |
291 | * @mtd: MTD device structure | |
292 | * @buf: data buffer | |
293 | * @len: number of bytes to write | |
294 | * | |
295 | * Default write function for 8bit buswith | |
296 | */ | |
297 | static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len) | |
298 | { | |
299 | int i; | |
300 | struct nand_chip *this = mtd->priv; | |
301 | ||
302 | for (i=0; i<len; i++) | |
303 | writeb(buf[i], this->IO_ADDR_W); | |
304 | } | |
305 | ||
306 | /** | |
307 | * nand_read_buf - [DEFAULT] read chip data into buffer | |
308 | * @mtd: MTD device structure | |
309 | * @buf: buffer to store date | |
310 | * @len: number of bytes to read | |
311 | * | |
312 | * Default read function for 8bit buswith | |
313 | */ | |
314 | static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) | |
315 | { | |
316 | int i; | |
317 | struct nand_chip *this = mtd->priv; | |
318 | ||
319 | for (i=0; i<len; i++) | |
320 | buf[i] = readb(this->IO_ADDR_R); | |
321 | } | |
322 | ||
323 | /** | |
324 | * nand_verify_buf - [DEFAULT] Verify chip data against buffer | |
325 | * @mtd: MTD device structure | |
326 | * @buf: buffer containing the data to compare | |
327 | * @len: number of bytes to compare | |
328 | * | |
329 | * Default verify function for 8bit buswith | |
330 | */ | |
331 | static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len) | |
332 | { | |
333 | int i; | |
334 | struct nand_chip *this = mtd->priv; | |
335 | ||
336 | for (i=0; i<len; i++) | |
337 | if (buf[i] != readb(this->IO_ADDR_R)) | |
338 | return -EFAULT; | |
339 | ||
340 | return 0; | |
341 | } | |
342 | ||
343 | /** | |
344 | * nand_write_buf16 - [DEFAULT] write buffer to chip | |
345 | * @mtd: MTD device structure | |
346 | * @buf: data buffer | |
347 | * @len: number of bytes to write | |
348 | * | |
349 | * Default write function for 16bit buswith | |
350 | */ | |
351 | static void nand_write_buf16(struct mtd_info *mtd, const u_char *buf, int len) | |
352 | { | |
353 | int i; | |
354 | struct nand_chip *this = mtd->priv; | |
355 | u16 *p = (u16 *) buf; | |
356 | len >>= 1; | |
357 | ||
358 | for (i=0; i<len; i++) | |
359 | writew(p[i], this->IO_ADDR_W); | |
360 | ||
361 | } | |
362 | ||
363 | /** | |
364 | * nand_read_buf16 - [DEFAULT] read chip data into buffer | |
365 | * @mtd: MTD device structure | |
366 | * @buf: buffer to store date | |
367 | * @len: number of bytes to read | |
368 | * | |
369 | * Default read function for 16bit buswith | |
370 | */ | |
371 | static void nand_read_buf16(struct mtd_info *mtd, u_char *buf, int len) | |
372 | { | |
373 | int i; | |
374 | struct nand_chip *this = mtd->priv; | |
375 | u16 *p = (u16 *) buf; | |
376 | len >>= 1; | |
377 | ||
378 | for (i=0; i<len; i++) | |
379 | p[i] = readw(this->IO_ADDR_R); | |
380 | } | |
381 | ||
382 | /** | |
383 | * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer | |
384 | * @mtd: MTD device structure | |
385 | * @buf: buffer containing the data to compare | |
386 | * @len: number of bytes to compare | |
387 | * | |
388 | * Default verify function for 16bit buswith | |
389 | */ | |
390 | static int nand_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len) | |
391 | { | |
392 | int i; | |
393 | struct nand_chip *this = mtd->priv; | |
394 | u16 *p = (u16 *) buf; | |
395 | len >>= 1; | |
396 | ||
397 | for (i=0; i<len; i++) | |
398 | if (p[i] != readw(this->IO_ADDR_R)) | |
399 | return -EFAULT; | |
400 | ||
401 | return 0; | |
402 | } | |
403 | ||
404 | /** | |
405 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip | |
406 | * @mtd: MTD device structure | |
407 | * @ofs: offset from device start | |
408 | * @getchip: 0, if the chip is already selected | |
409 | * | |
410 | * Check, if the block is bad. | |
411 | */ | |
412 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip) | |
413 | { | |
414 | int page, chipnr, res = 0; | |
415 | struct nand_chip *this = mtd->priv; | |
416 | u16 bad; | |
417 | ||
418 | if (getchip) { | |
419 | page = (int)(ofs >> this->page_shift); | |
420 | chipnr = (int)(ofs >> this->chip_shift); | |
421 | ||
422 | /* Grab the lock and see if the device is available */ | |
423 | nand_get_device (this, mtd, FL_READING); | |
424 | ||
425 | /* Select the NAND device */ | |
426 | this->select_chip(mtd, chipnr); | |
427 | } else | |
428 | page = (int) ofs; | |
429 | ||
430 | if (this->options & NAND_BUSWIDTH_16) { | |
431 | this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page & this->pagemask); | |
432 | bad = cpu_to_le16(this->read_word(mtd)); | |
433 | if (this->badblockpos & 0x1) | |
434 | bad >>= 1; | |
435 | if ((bad & 0xFF) != 0xff) | |
436 | res = 1; | |
437 | } else { | |
438 | this->cmdfunc (mtd, NAND_CMD_READOOB, this->badblockpos, page & this->pagemask); | |
439 | if (this->read_byte(mtd) != 0xff) | |
440 | res = 1; | |
441 | } | |
442 | ||
443 | if (getchip) { | |
444 | /* Deselect and wake up anyone waiting on the device */ | |
445 | nand_release_device(mtd); | |
446 | } | |
447 | ||
448 | return res; | |
449 | } | |
450 | ||
451 | /** | |
452 | * nand_default_block_markbad - [DEFAULT] mark a block bad | |
453 | * @mtd: MTD device structure | |
454 | * @ofs: offset from device start | |
455 | * | |
456 | * This is the default implementation, which can be overridden by | |
457 | * a hardware specific driver. | |
458 | */ | |
459 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) | |
460 | { | |
461 | struct nand_chip *this = mtd->priv; | |
462 | u_char buf[2] = {0, 0}; | |
463 | size_t retlen; | |
464 | int block; | |
465 | ||
466 | /* Get block number */ | |
467 | block = ((int) ofs) >> this->bbt_erase_shift; | |
41ce9214 AB |
468 | if (this->bbt) |
469 | this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1); | |
1da177e4 LT |
470 | |
471 | /* Do we have a flash based bad block table ? */ | |
472 | if (this->options & NAND_USE_FLASH_BBT) | |
473 | return nand_update_bbt (mtd, ofs); | |
474 | ||
475 | /* We write two bytes, so we dont have to mess with 16 bit access */ | |
476 | ofs += mtd->oobsize + (this->badblockpos & ~0x01); | |
477 | return nand_write_oob (mtd, ofs , 2, &retlen, buf); | |
478 | } | |
479 | ||
480 | /** | |
481 | * nand_check_wp - [GENERIC] check if the chip is write protected | |
482 | * @mtd: MTD device structure | |
483 | * Check, if the device is write protected | |
484 | * | |
485 | * The function expects, that the device is already selected | |
486 | */ | |
487 | static int nand_check_wp (struct mtd_info *mtd) | |
488 | { | |
489 | struct nand_chip *this = mtd->priv; | |
490 | /* Check the WP bit */ | |
491 | this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1); | |
a4ab4c5d | 492 | return (this->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1; |
1da177e4 LT |
493 | } |
494 | ||
495 | /** | |
496 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad | |
497 | * @mtd: MTD device structure | |
498 | * @ofs: offset from device start | |
499 | * @getchip: 0, if the chip is already selected | |
500 | * @allowbbt: 1, if its allowed to access the bbt area | |
501 | * | |
502 | * Check, if the block is bad. Either by reading the bad block table or | |
503 | * calling of the scan function. | |
504 | */ | |
505 | static int nand_block_checkbad (struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt) | |
506 | { | |
507 | struct nand_chip *this = mtd->priv; | |
508 | ||
509 | if (!this->bbt) | |
510 | return this->block_bad(mtd, ofs, getchip); | |
511 | ||
512 | /* Return info from the table */ | |
513 | return nand_isbad_bbt (mtd, ofs, allowbbt); | |
514 | } | |
515 | ||
3b88775c TG |
516 | /* |
517 | * Wait for the ready pin, after a command | |
518 | * The timeout is catched later. | |
519 | */ | |
520 | static void nand_wait_ready(struct mtd_info *mtd) | |
521 | { | |
522 | struct nand_chip *this = mtd->priv; | |
523 | unsigned long timeo = jiffies + 2; | |
524 | ||
525 | /* wait until command is processed or timeout occures */ | |
526 | do { | |
527 | if (this->dev_ready(mtd)) | |
528 | return; | |
529 | } while (time_before(jiffies, timeo)); | |
530 | } | |
531 | ||
1da177e4 LT |
532 | /** |
533 | * nand_command - [DEFAULT] Send command to NAND device | |
534 | * @mtd: MTD device structure | |
535 | * @command: the command to be sent | |
536 | * @column: the column address for this command, -1 if none | |
537 | * @page_addr: the page address for this command, -1 if none | |
538 | * | |
539 | * Send command to NAND device. This function is used for small page | |
540 | * devices (256/512 Bytes per page) | |
541 | */ | |
542 | static void nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr) | |
543 | { | |
544 | register struct nand_chip *this = mtd->priv; | |
545 | ||
546 | /* Begin command latch cycle */ | |
547 | this->hwcontrol(mtd, NAND_CTL_SETCLE); | |
548 | /* | |
549 | * Write out the command to the device. | |
550 | */ | |
551 | if (command == NAND_CMD_SEQIN) { | |
552 | int readcmd; | |
553 | ||
554 | if (column >= mtd->oobblock) { | |
555 | /* OOB area */ | |
556 | column -= mtd->oobblock; | |
557 | readcmd = NAND_CMD_READOOB; | |
558 | } else if (column < 256) { | |
559 | /* First 256 bytes --> READ0 */ | |
560 | readcmd = NAND_CMD_READ0; | |
561 | } else { | |
562 | column -= 256; | |
563 | readcmd = NAND_CMD_READ1; | |
564 | } | |
565 | this->write_byte(mtd, readcmd); | |
566 | } | |
567 | this->write_byte(mtd, command); | |
568 | ||
569 | /* Set ALE and clear CLE to start address cycle */ | |
570 | this->hwcontrol(mtd, NAND_CTL_CLRCLE); | |
571 | ||
572 | if (column != -1 || page_addr != -1) { | |
573 | this->hwcontrol(mtd, NAND_CTL_SETALE); | |
574 | ||
575 | /* Serially input address */ | |
576 | if (column != -1) { | |
577 | /* Adjust columns for 16 bit buswidth */ | |
578 | if (this->options & NAND_BUSWIDTH_16) | |
579 | column >>= 1; | |
580 | this->write_byte(mtd, column); | |
581 | } | |
582 | if (page_addr != -1) { | |
583 | this->write_byte(mtd, (unsigned char) (page_addr & 0xff)); | |
584 | this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff)); | |
585 | /* One more address cycle for devices > 32MiB */ | |
586 | if (this->chipsize > (32 << 20)) | |
587 | this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0x0f)); | |
588 | } | |
589 | /* Latch in address */ | |
590 | this->hwcontrol(mtd, NAND_CTL_CLRALE); | |
591 | } | |
592 | ||
593 | /* | |
594 | * program and erase have their own busy handlers | |
595 | * status and sequential in needs no delay | |
596 | */ | |
597 | switch (command) { | |
598 | ||
599 | case NAND_CMD_PAGEPROG: | |
600 | case NAND_CMD_ERASE1: | |
601 | case NAND_CMD_ERASE2: | |
602 | case NAND_CMD_SEQIN: | |
603 | case NAND_CMD_STATUS: | |
604 | return; | |
605 | ||
606 | case NAND_CMD_RESET: | |
607 | if (this->dev_ready) | |
608 | break; | |
609 | udelay(this->chip_delay); | |
610 | this->hwcontrol(mtd, NAND_CTL_SETCLE); | |
611 | this->write_byte(mtd, NAND_CMD_STATUS); | |
612 | this->hwcontrol(mtd, NAND_CTL_CLRCLE); | |
a4ab4c5d | 613 | while ( !(this->read_byte(mtd) & NAND_STATUS_READY)); |
1da177e4 LT |
614 | return; |
615 | ||
616 | /* This applies to read commands */ | |
617 | default: | |
618 | /* | |
619 | * If we don't have access to the busy pin, we apply the given | |
620 | * command delay | |
621 | */ | |
622 | if (!this->dev_ready) { | |
623 | udelay (this->chip_delay); | |
624 | return; | |
625 | } | |
626 | } | |
1da177e4 LT |
627 | /* Apply this short delay always to ensure that we do wait tWB in |
628 | * any case on any machine. */ | |
629 | ndelay (100); | |
3b88775c TG |
630 | |
631 | nand_wait_ready(mtd); | |
1da177e4 LT |
632 | } |
633 | ||
634 | /** | |
635 | * nand_command_lp - [DEFAULT] Send command to NAND large page device | |
636 | * @mtd: MTD device structure | |
637 | * @command: the command to be sent | |
638 | * @column: the column address for this command, -1 if none | |
639 | * @page_addr: the page address for this command, -1 if none | |
640 | * | |
641 | * Send command to NAND device. This is the version for the new large page devices | |
642 | * We dont have the seperate regions as we have in the small page devices. | |
643 | * We must emulate NAND_CMD_READOOB to keep the code compatible. | |
644 | * | |
645 | */ | |
646 | static void nand_command_lp (struct mtd_info *mtd, unsigned command, int column, int page_addr) | |
647 | { | |
648 | register struct nand_chip *this = mtd->priv; | |
649 | ||
650 | /* Emulate NAND_CMD_READOOB */ | |
651 | if (command == NAND_CMD_READOOB) { | |
652 | column += mtd->oobblock; | |
653 | command = NAND_CMD_READ0; | |
654 | } | |
655 | ||
656 | ||
657 | /* Begin command latch cycle */ | |
658 | this->hwcontrol(mtd, NAND_CTL_SETCLE); | |
659 | /* Write out the command to the device. */ | |
30f464b7 | 660 | this->write_byte(mtd, (command & 0xff)); |
1da177e4 LT |
661 | /* End command latch cycle */ |
662 | this->hwcontrol(mtd, NAND_CTL_CLRCLE); | |
663 | ||
664 | if (column != -1 || page_addr != -1) { | |
665 | this->hwcontrol(mtd, NAND_CTL_SETALE); | |
666 | ||
667 | /* Serially input address */ | |
668 | if (column != -1) { | |
669 | /* Adjust columns for 16 bit buswidth */ | |
670 | if (this->options & NAND_BUSWIDTH_16) | |
671 | column >>= 1; | |
672 | this->write_byte(mtd, column & 0xff); | |
673 | this->write_byte(mtd, column >> 8); | |
674 | } | |
675 | if (page_addr != -1) { | |
676 | this->write_byte(mtd, (unsigned char) (page_addr & 0xff)); | |
677 | this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff)); | |
678 | /* One more address cycle for devices > 128MiB */ | |
679 | if (this->chipsize > (128 << 20)) | |
680 | this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0xff)); | |
681 | } | |
682 | /* Latch in address */ | |
683 | this->hwcontrol(mtd, NAND_CTL_CLRALE); | |
684 | } | |
685 | ||
686 | /* | |
687 | * program and erase have their own busy handlers | |
30f464b7 DM |
688 | * status, sequential in, and deplete1 need no delay |
689 | */ | |
1da177e4 LT |
690 | switch (command) { |
691 | ||
692 | case NAND_CMD_CACHEDPROG: | |
693 | case NAND_CMD_PAGEPROG: | |
694 | case NAND_CMD_ERASE1: | |
695 | case NAND_CMD_ERASE2: | |
696 | case NAND_CMD_SEQIN: | |
697 | case NAND_CMD_STATUS: | |
30f464b7 | 698 | case NAND_CMD_DEPLETE1: |
1da177e4 LT |
699 | return; |
700 | ||
30f464b7 DM |
701 | /* |
702 | * read error status commands require only a short delay | |
703 | */ | |
704 | case NAND_CMD_STATUS_ERROR: | |
705 | case NAND_CMD_STATUS_ERROR0: | |
706 | case NAND_CMD_STATUS_ERROR1: | |
707 | case NAND_CMD_STATUS_ERROR2: | |
708 | case NAND_CMD_STATUS_ERROR3: | |
709 | udelay(this->chip_delay); | |
710 | return; | |
1da177e4 LT |
711 | |
712 | case NAND_CMD_RESET: | |
713 | if (this->dev_ready) | |
714 | break; | |
715 | udelay(this->chip_delay); | |
716 | this->hwcontrol(mtd, NAND_CTL_SETCLE); | |
717 | this->write_byte(mtd, NAND_CMD_STATUS); | |
718 | this->hwcontrol(mtd, NAND_CTL_CLRCLE); | |
a4ab4c5d | 719 | while ( !(this->read_byte(mtd) & NAND_STATUS_READY)); |
1da177e4 LT |
720 | return; |
721 | ||
722 | case NAND_CMD_READ0: | |
723 | /* Begin command latch cycle */ | |
724 | this->hwcontrol(mtd, NAND_CTL_SETCLE); | |
725 | /* Write out the start read command */ | |
726 | this->write_byte(mtd, NAND_CMD_READSTART); | |
727 | /* End command latch cycle */ | |
728 | this->hwcontrol(mtd, NAND_CTL_CLRCLE); | |
729 | /* Fall through into ready check */ | |
730 | ||
731 | /* This applies to read commands */ | |
732 | default: | |
733 | /* | |
734 | * If we don't have access to the busy pin, we apply the given | |
735 | * command delay | |
736 | */ | |
737 | if (!this->dev_ready) { | |
738 | udelay (this->chip_delay); | |
739 | return; | |
740 | } | |
741 | } | |
3b88775c | 742 | |
1da177e4 LT |
743 | /* Apply this short delay always to ensure that we do wait tWB in |
744 | * any case on any machine. */ | |
745 | ndelay (100); | |
3b88775c TG |
746 | |
747 | nand_wait_ready(mtd); | |
1da177e4 LT |
748 | } |
749 | ||
750 | /** | |
751 | * nand_get_device - [GENERIC] Get chip for selected access | |
752 | * @this: the nand chip descriptor | |
753 | * @mtd: MTD device structure | |
754 | * @new_state: the state which is requested | |
755 | * | |
756 | * Get the device and lock it for exclusive access | |
757 | */ | |
758 | static void nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state) | |
759 | { | |
0dfc6246 TG |
760 | struct nand_chip *active; |
761 | spinlock_t *lock; | |
762 | wait_queue_head_t *wq; | |
1da177e4 LT |
763 | DECLARE_WAITQUEUE (wait, current); |
764 | ||
0dfc6246 TG |
765 | lock = (this->controller) ? &this->controller->lock : &this->chip_lock; |
766 | wq = (this->controller) ? &this->controller->wq : &this->wq; | |
1da177e4 | 767 | retry: |
0dfc6246 TG |
768 | active = this; |
769 | spin_lock(lock); | |
770 | ||
1da177e4 LT |
771 | /* Hardware controller shared among independend devices */ |
772 | if (this->controller) { | |
1da177e4 LT |
773 | if (this->controller->active) |
774 | active = this->controller->active; | |
775 | else | |
776 | this->controller->active = this; | |
1da177e4 | 777 | } |
0dfc6246 TG |
778 | if (active == this && this->state == FL_READY) { |
779 | this->state = new_state; | |
780 | spin_unlock(lock); | |
781 | return; | |
782 | } | |
783 | set_current_state(TASK_UNINTERRUPTIBLE); | |
784 | add_wait_queue(wq, &wait); | |
785 | spin_unlock(lock); | |
786 | schedule(); | |
787 | remove_wait_queue(wq, &wait); | |
1da177e4 LT |
788 | goto retry; |
789 | } | |
790 | ||
791 | /** | |
792 | * nand_wait - [DEFAULT] wait until the command is done | |
793 | * @mtd: MTD device structure | |
794 | * @this: NAND chip structure | |
795 | * @state: state to select the max. timeout value | |
796 | * | |
797 | * Wait for command done. This applies to erase and program only | |
798 | * Erase can take up to 400ms and program up to 20ms according to | |
799 | * general NAND and SmartMedia specs | |
800 | * | |
801 | */ | |
802 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state) | |
803 | { | |
804 | ||
805 | unsigned long timeo = jiffies; | |
806 | int status; | |
807 | ||
808 | if (state == FL_ERASING) | |
809 | timeo += (HZ * 400) / 1000; | |
810 | else | |
811 | timeo += (HZ * 20) / 1000; | |
812 | ||
813 | /* Apply this short delay always to ensure that we do wait tWB in | |
814 | * any case on any machine. */ | |
815 | ndelay (100); | |
816 | ||
817 | if ((state == FL_ERASING) && (this->options & NAND_IS_AND)) | |
818 | this->cmdfunc (mtd, NAND_CMD_STATUS_MULTI, -1, -1); | |
819 | else | |
820 | this->cmdfunc (mtd, NAND_CMD_STATUS, -1, -1); | |
821 | ||
822 | while (time_before(jiffies, timeo)) { | |
823 | /* Check, if we were interrupted */ | |
824 | if (this->state != state) | |
825 | return 0; | |
826 | ||
827 | if (this->dev_ready) { | |
828 | if (this->dev_ready(mtd)) | |
829 | break; | |
830 | } else { | |
831 | if (this->read_byte(mtd) & NAND_STATUS_READY) | |
832 | break; | |
833 | } | |
20a6c211 | 834 | cond_resched(); |
1da177e4 LT |
835 | } |
836 | status = (int) this->read_byte(mtd); | |
837 | return status; | |
838 | } | |
839 | ||
840 | /** | |
841 | * nand_write_page - [GENERIC] write one page | |
842 | * @mtd: MTD device structure | |
843 | * @this: NAND chip structure | |
844 | * @page: startpage inside the chip, must be called with (page & this->pagemask) | |
845 | * @oob_buf: out of band data buffer | |
846 | * @oobsel: out of band selecttion structre | |
847 | * @cached: 1 = enable cached programming if supported by chip | |
848 | * | |
849 | * Nand_page_program function is used for write and writev ! | |
850 | * This function will always program a full page of data | |
851 | * If you call it with a non page aligned buffer, you're lost :) | |
852 | * | |
853 | * Cached programming is not supported yet. | |
854 | */ | |
855 | static int nand_write_page (struct mtd_info *mtd, struct nand_chip *this, int page, | |
856 | u_char *oob_buf, struct nand_oobinfo *oobsel, int cached) | |
857 | { | |
858 | int i, status; | |
0a18cde6 | 859 | u_char ecc_code[32]; |
1da177e4 LT |
860 | int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE; |
861 | int *oob_config = oobsel->eccpos; | |
862 | int datidx = 0, eccidx = 0, eccsteps = this->eccsteps; | |
863 | int eccbytes = 0; | |
864 | ||
865 | /* FIXME: Enable cached programming */ | |
866 | cached = 0; | |
867 | ||
868 | /* Send command to begin auto page programming */ | |
869 | this->cmdfunc (mtd, NAND_CMD_SEQIN, 0x00, page); | |
870 | ||
871 | /* Write out complete page of data, take care of eccmode */ | |
872 | switch (eccmode) { | |
873 | /* No ecc, write all */ | |
874 | case NAND_ECC_NONE: | |
875 | printk (KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n"); | |
876 | this->write_buf(mtd, this->data_poi, mtd->oobblock); | |
877 | break; | |
878 | ||
879 | /* Software ecc 3/256, write all */ | |
880 | case NAND_ECC_SOFT: | |
881 | for (; eccsteps; eccsteps--) { | |
882 | this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code); | |
883 | for (i = 0; i < 3; i++, eccidx++) | |
884 | oob_buf[oob_config[eccidx]] = ecc_code[i]; | |
885 | datidx += this->eccsize; | |
886 | } | |
887 | this->write_buf(mtd, this->data_poi, mtd->oobblock); | |
888 | break; | |
889 | default: | |
890 | eccbytes = this->eccbytes; | |
891 | for (; eccsteps; eccsteps--) { | |
892 | /* enable hardware ecc logic for write */ | |
893 | this->enable_hwecc(mtd, NAND_ECC_WRITE); | |
894 | this->write_buf(mtd, &this->data_poi[datidx], this->eccsize); | |
895 | this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code); | |
896 | for (i = 0; i < eccbytes; i++, eccidx++) | |
897 | oob_buf[oob_config[eccidx]] = ecc_code[i]; | |
898 | /* If the hardware ecc provides syndromes then | |
899 | * the ecc code must be written immidiately after | |
900 | * the data bytes (words) */ | |
901 | if (this->options & NAND_HWECC_SYNDROME) | |
902 | this->write_buf(mtd, ecc_code, eccbytes); | |
903 | datidx += this->eccsize; | |
904 | } | |
905 | break; | |
906 | } | |
907 | ||
908 | /* Write out OOB data */ | |
909 | if (this->options & NAND_HWECC_SYNDROME) | |
910 | this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes); | |
911 | else | |
912 | this->write_buf(mtd, oob_buf, mtd->oobsize); | |
913 | ||
914 | /* Send command to actually program the data */ | |
915 | this->cmdfunc (mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1); | |
916 | ||
917 | if (!cached) { | |
918 | /* call wait ready function */ | |
919 | status = this->waitfunc (mtd, this, FL_WRITING); | |
068e3c0a DM |
920 | |
921 | /* See if operation failed and additional status checks are available */ | |
922 | if ((status & NAND_STATUS_FAIL) && (this->errstat)) { | |
923 | status = this->errstat(mtd, this, FL_WRITING, status, page); | |
924 | } | |
925 | ||
1da177e4 | 926 | /* See if device thinks it succeeded */ |
a4ab4c5d | 927 | if (status & NAND_STATUS_FAIL) { |
1da177e4 LT |
928 | DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page); |
929 | return -EIO; | |
930 | } | |
931 | } else { | |
932 | /* FIXME: Implement cached programming ! */ | |
933 | /* wait until cache is ready*/ | |
934 | // status = this->waitfunc (mtd, this, FL_CACHEDRPG); | |
935 | } | |
936 | return 0; | |
937 | } | |
938 | ||
939 | #ifdef CONFIG_MTD_NAND_VERIFY_WRITE | |
940 | /** | |
941 | * nand_verify_pages - [GENERIC] verify the chip contents after a write | |
942 | * @mtd: MTD device structure | |
943 | * @this: NAND chip structure | |
944 | * @page: startpage inside the chip, must be called with (page & this->pagemask) | |
945 | * @numpages: number of pages to verify | |
946 | * @oob_buf: out of band data buffer | |
947 | * @oobsel: out of band selecttion structre | |
948 | * @chipnr: number of the current chip | |
949 | * @oobmode: 1 = full buffer verify, 0 = ecc only | |
950 | * | |
951 | * The NAND device assumes that it is always writing to a cleanly erased page. | |
952 | * Hence, it performs its internal write verification only on bits that | |
953 | * transitioned from 1 to 0. The device does NOT verify the whole page on a | |
954 | * byte by byte basis. It is possible that the page was not completely erased | |
955 | * or the page is becoming unusable due to wear. The read with ECC would catch | |
956 | * the error later when the ECC page check fails, but we would rather catch | |
957 | * it early in the page write stage. Better to write no data than invalid data. | |
958 | */ | |
959 | static int nand_verify_pages (struct mtd_info *mtd, struct nand_chip *this, int page, int numpages, | |
960 | u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode) | |
961 | { | |
962 | int i, j, datidx = 0, oobofs = 0, res = -EIO; | |
963 | int eccsteps = this->eccsteps; | |
964 | int hweccbytes; | |
0a18cde6 | 965 | u_char oobdata[64]; |
1da177e4 LT |
966 | |
967 | hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0; | |
968 | ||
969 | /* Send command to read back the first page */ | |
970 | this->cmdfunc (mtd, NAND_CMD_READ0, 0, page); | |
971 | ||
972 | for(;;) { | |
973 | for (j = 0; j < eccsteps; j++) { | |
974 | /* Loop through and verify the data */ | |
975 | if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) { | |
976 | DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page); | |
977 | goto out; | |
978 | } | |
979 | datidx += mtd->eccsize; | |
980 | /* Have we a hw generator layout ? */ | |
981 | if (!hweccbytes) | |
982 | continue; | |
983 | if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) { | |
984 | DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page); | |
985 | goto out; | |
986 | } | |
987 | oobofs += hweccbytes; | |
988 | } | |
989 | ||
990 | /* check, if we must compare all data or if we just have to | |
991 | * compare the ecc bytes | |
992 | */ | |
993 | if (oobmode) { | |
994 | if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) { | |
995 | DEBUG (MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page); | |
996 | goto out; | |
997 | } | |
998 | } else { | |
999 | /* Read always, else autoincrement fails */ | |
1000 | this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps); | |
1001 | ||
1002 | if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) { | |
1003 | int ecccnt = oobsel->eccbytes; | |
1004 | ||
1005 | for (i = 0; i < ecccnt; i++) { | |
1006 | int idx = oobsel->eccpos[i]; | |
1007 | if (oobdata[idx] != oob_buf[oobofs + idx] ) { | |
1008 | DEBUG (MTD_DEBUG_LEVEL0, | |
1009 | "%s: Failed ECC write " | |
1010 | "verify, page 0x%08x, " "%6i bytes were succesful\n", __FUNCTION__, page, i); | |
1011 | goto out; | |
1012 | } | |
1013 | } | |
1014 | } | |
1015 | } | |
1016 | oobofs += mtd->oobsize - hweccbytes * eccsteps; | |
1017 | page++; | |
1018 | numpages--; | |
1019 | ||
1020 | /* Apply delay or wait for ready/busy pin | |
1021 | * Do this before the AUTOINCR check, so no problems | |
1022 | * arise if a chip which does auto increment | |
1023 | * is marked as NOAUTOINCR by the board driver. | |
1024 | * Do this also before returning, so the chip is | |
1025 | * ready for the next command. | |
1026 | */ | |
1027 | if (!this->dev_ready) | |
1028 | udelay (this->chip_delay); | |
1029 | else | |
3b88775c | 1030 | nand_wait_ready(mtd); |
1da177e4 LT |
1031 | |
1032 | /* All done, return happy */ | |
1033 | if (!numpages) | |
1034 | return 0; | |
1035 | ||
1036 | ||
1037 | /* Check, if the chip supports auto page increment */ | |
1038 | if (!NAND_CANAUTOINCR(this)) | |
1039 | this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page); | |
1040 | } | |
1041 | /* | |
1042 | * Terminate the read command. We come here in case of an error | |
1043 | * So we must issue a reset command. | |
1044 | */ | |
1045 | out: | |
1046 | this->cmdfunc (mtd, NAND_CMD_RESET, -1, -1); | |
1047 | return res; | |
1048 | } | |
1049 | #endif | |
1050 | ||
1051 | /** | |
068e3c0a | 1052 | * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc |
1da177e4 LT |
1053 | * @mtd: MTD device structure |
1054 | * @from: offset to read from | |
1055 | * @len: number of bytes to read | |
1056 | * @retlen: pointer to variable to store the number of read bytes | |
1057 | * @buf: the databuffer to put data | |
1058 | * | |
068e3c0a DM |
1059 | * This function simply calls nand_do_read_ecc with oob buffer and oobsel = NULL |
1060 | * and flags = 0xff | |
1061 | */ | |
1da177e4 LT |
1062 | static int nand_read (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf) |
1063 | { | |
22c60f5f TG |
1064 | return nand_do_read_ecc (mtd, from, len, retlen, buf, NULL, &mtd->oobinfo, 0xff); |
1065 | } | |
1da177e4 LT |
1066 | |
1067 | ||
1068 | /** | |
068e3c0a | 1069 | * nand_read_ecc - [MTD Interface] MTD compability function for nand_do_read_ecc |
1da177e4 LT |
1070 | * @mtd: MTD device structure |
1071 | * @from: offset to read from | |
1072 | * @len: number of bytes to read | |
1073 | * @retlen: pointer to variable to store the number of read bytes | |
1074 | * @buf: the databuffer to put data | |
1075 | * @oob_buf: filesystem supplied oob data buffer | |
1076 | * @oobsel: oob selection structure | |
1077 | * | |
068e3c0a | 1078 | * This function simply calls nand_do_read_ecc with flags = 0xff |
1da177e4 LT |
1079 | */ |
1080 | static int nand_read_ecc (struct mtd_info *mtd, loff_t from, size_t len, | |
1081 | size_t * retlen, u_char * buf, u_char * oob_buf, struct nand_oobinfo *oobsel) | |
068e3c0a | 1082 | { |
22c60f5f TG |
1083 | /* use userspace supplied oobinfo, if zero */ |
1084 | if (oobsel == NULL) | |
1085 | oobsel = &mtd->oobinfo; | |
068e3c0a DM |
1086 | return nand_do_read_ecc(mtd, from, len, retlen, buf, oob_buf, oobsel, 0xff); |
1087 | } | |
1088 | ||
1089 | ||
1090 | /** | |
1091 | * nand_do_read_ecc - [MTD Interface] Read data with ECC | |
1092 | * @mtd: MTD device structure | |
1093 | * @from: offset to read from | |
1094 | * @len: number of bytes to read | |
1095 | * @retlen: pointer to variable to store the number of read bytes | |
1096 | * @buf: the databuffer to put data | |
bb75ba4c | 1097 | * @oob_buf: filesystem supplied oob data buffer (can be NULL) |
22c60f5f | 1098 | * @oobsel: oob selection structure |
068e3c0a DM |
1099 | * @flags: flag to indicate if nand_get_device/nand_release_device should be preformed |
1100 | * and how many corrected error bits are acceptable: | |
1101 | * bits 0..7 - number of tolerable errors | |
1102 | * bit 8 - 0 == do not get/release chip, 1 == get/release chip | |
1103 | * | |
1104 | * NAND read with ECC | |
1105 | */ | |
1106 | int nand_do_read_ecc (struct mtd_info *mtd, loff_t from, size_t len, | |
1107 | size_t * retlen, u_char * buf, u_char * oob_buf, | |
1108 | struct nand_oobinfo *oobsel, int flags) | |
1da177e4 | 1109 | { |
22c60f5f | 1110 | |
1da177e4 LT |
1111 | int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1; |
1112 | int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0; | |
1113 | struct nand_chip *this = mtd->priv; | |
1114 | u_char *data_poi, *oob_data = oob_buf; | |
0a18cde6 JL |
1115 | u_char ecc_calc[32]; |
1116 | u_char ecc_code[32]; | |
1da177e4 LT |
1117 | int eccmode, eccsteps; |
1118 | int *oob_config, datidx; | |
1119 | int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1; | |
1120 | int eccbytes; | |
1121 | int compareecc = 1; | |
1122 | int oobreadlen; | |
1123 | ||
1124 | ||
1125 | DEBUG (MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len); | |
1126 | ||
1127 | /* Do not allow reads past end of device */ | |
1128 | if ((from + len) > mtd->size) { | |
1129 | DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n"); | |
1130 | *retlen = 0; | |
1131 | return -EINVAL; | |
1132 | } | |
1133 | ||
1134 | /* Grab the lock and see if the device is available */ | |
068e3c0a DM |
1135 | if (flags & NAND_GET_DEVICE) |
1136 | nand_get_device (this, mtd, FL_READING); | |
1da177e4 | 1137 | |
1da177e4 LT |
1138 | /* Autoplace of oob data ? Use the default placement scheme */ |
1139 | if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) | |
1140 | oobsel = this->autooob; | |
1141 | ||
1142 | eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE; | |
1143 | oob_config = oobsel->eccpos; | |
1144 | ||
1145 | /* Select the NAND device */ | |
1146 | chipnr = (int)(from >> this->chip_shift); | |
1147 | this->select_chip(mtd, chipnr); | |
1148 | ||
1149 | /* First we calculate the starting page */ | |
1150 | realpage = (int) (from >> this->page_shift); | |
1151 | page = realpage & this->pagemask; | |
1152 | ||
1153 | /* Get raw starting column */ | |
1154 | col = from & (mtd->oobblock - 1); | |
1155 | ||
1156 | end = mtd->oobblock; | |
1157 | ecc = this->eccsize; | |
1158 | eccbytes = this->eccbytes; | |
1159 | ||
1160 | if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME)) | |
1161 | compareecc = 0; | |
1162 | ||
1163 | oobreadlen = mtd->oobsize; | |
1164 | if (this->options & NAND_HWECC_SYNDROME) | |
1165 | oobreadlen -= oobsel->eccbytes; | |
1166 | ||
1167 | /* Loop until all data read */ | |
1168 | while (read < len) { | |
1169 | ||
1170 | int aligned = (!col && (len - read) >= end); | |
1171 | /* | |
1172 | * If the read is not page aligned, we have to read into data buffer | |
1173 | * due to ecc, else we read into return buffer direct | |
1174 | */ | |
1175 | if (aligned) | |
1176 | data_poi = &buf[read]; | |
1177 | else | |
1178 | data_poi = this->data_buf; | |
1179 | ||
1180 | /* Check, if we have this page in the buffer | |
1181 | * | |
1182 | * FIXME: Make it work when we must provide oob data too, | |
1183 | * check the usage of data_buf oob field | |
1184 | */ | |
1185 | if (realpage == this->pagebuf && !oob_buf) { | |
1186 | /* aligned read ? */ | |
1187 | if (aligned) | |
1188 | memcpy (data_poi, this->data_buf, end); | |
1189 | goto readdata; | |
1190 | } | |
1191 | ||
1192 | /* Check, if we must send the read command */ | |
1193 | if (sndcmd) { | |
1194 | this->cmdfunc (mtd, NAND_CMD_READ0, 0x00, page); | |
1195 | sndcmd = 0; | |
1196 | } | |
1197 | ||
1198 | /* get oob area, if we have no oob buffer from fs-driver */ | |
90e260c8 TG |
1199 | if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE || |
1200 | oobsel->useecc == MTD_NANDECC_AUTOPL_USR) | |
1da177e4 LT |
1201 | oob_data = &this->data_buf[end]; |
1202 | ||
1203 | eccsteps = this->eccsteps; | |
1204 | ||
1205 | switch (eccmode) { | |
1206 | case NAND_ECC_NONE: { /* No ECC, Read in a page */ | |
1207 | static unsigned long lastwhinge = 0; | |
1208 | if ((lastwhinge / HZ) != (jiffies / HZ)) { | |
1209 | printk (KERN_WARNING "Reading data from NAND FLASH without ECC is not recommended\n"); | |
1210 | lastwhinge = jiffies; | |
1211 | } | |
1212 | this->read_buf(mtd, data_poi, end); | |
1213 | break; | |
1214 | } | |
1215 | ||
1216 | case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */ | |
1217 | this->read_buf(mtd, data_poi, end); | |
1218 | for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=3, datidx += ecc) | |
1219 | this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]); | |
1220 | break; | |
1221 | ||
1222 | default: | |
1223 | for (i = 0, datidx = 0; eccsteps; eccsteps--, i+=eccbytes, datidx += ecc) { | |
1224 | this->enable_hwecc(mtd, NAND_ECC_READ); | |
1225 | this->read_buf(mtd, &data_poi[datidx], ecc); | |
1226 | ||
1227 | /* HW ecc with syndrome calculation must read the | |
1228 | * syndrome from flash immidiately after the data */ | |
1229 | if (!compareecc) { | |
1230 | /* Some hw ecc generators need to know when the | |
1231 | * syndrome is read from flash */ | |
1232 | this->enable_hwecc(mtd, NAND_ECC_READSYN); | |
1233 | this->read_buf(mtd, &oob_data[i], eccbytes); | |
1234 | /* We calc error correction directly, it checks the hw | |
1235 | * generator for an error, reads back the syndrome and | |
1236 | * does the error correction on the fly */ | |
068e3c0a DM |
1237 | ecc_status = this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]); |
1238 | if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) { | |
1da177e4 LT |
1239 | DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: " |
1240 | "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr); | |
1241 | ecc_failed++; | |
1242 | } | |
1243 | } else { | |
1244 | this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]); | |
1245 | } | |
1246 | } | |
1247 | break; | |
1248 | } | |
1249 | ||
1250 | /* read oobdata */ | |
1251 | this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen); | |
1252 | ||
1253 | /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */ | |
1254 | if (!compareecc) | |
1255 | goto readoob; | |
1256 | ||
1257 | /* Pick the ECC bytes out of the oob data */ | |
1258 | for (j = 0; j < oobsel->eccbytes; j++) | |
1259 | ecc_code[j] = oob_data[oob_config[j]]; | |
1260 | ||
1261 | /* correct data, if neccecary */ | |
1262 | for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) { | |
1263 | ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]); | |
1264 | ||
1265 | /* Get next chunk of ecc bytes */ | |
1266 | j += eccbytes; | |
1267 | ||
1268 | /* Check, if we have a fs supplied oob-buffer, | |
1269 | * This is the legacy mode. Used by YAFFS1 | |
1270 | * Should go away some day | |
1271 | */ | |
1272 | if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) { | |
1273 | int *p = (int *)(&oob_data[mtd->oobsize]); | |
1274 | p[i] = ecc_status; | |
1275 | } | |
1276 | ||
068e3c0a | 1277 | if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) { |
1da177e4 LT |
1278 | DEBUG (MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page); |
1279 | ecc_failed++; | |
1280 | } | |
1281 | } | |
1282 | ||
1283 | readoob: | |
1284 | /* check, if we have a fs supplied oob-buffer */ | |
1285 | if (oob_buf) { | |
1286 | /* without autoplace. Legacy mode used by YAFFS1 */ | |
1287 | switch(oobsel->useecc) { | |
1288 | case MTD_NANDECC_AUTOPLACE: | |
90e260c8 | 1289 | case MTD_NANDECC_AUTOPL_USR: |
1da177e4 | 1290 | /* Walk through the autoplace chunks */ |
82e1d19f | 1291 | for (i = 0; oobsel->oobfree[i][1]; i++) { |
1da177e4 LT |
1292 | int from = oobsel->oobfree[i][0]; |
1293 | int num = oobsel->oobfree[i][1]; | |
1294 | memcpy(&oob_buf[oob], &oob_data[from], num); | |
82e1d19f | 1295 | oob += num; |
1da177e4 | 1296 | } |
1da177e4 LT |
1297 | break; |
1298 | case MTD_NANDECC_PLACE: | |
1299 | /* YAFFS1 legacy mode */ | |
1300 | oob_data += this->eccsteps * sizeof (int); | |
1301 | default: | |
1302 | oob_data += mtd->oobsize; | |
1303 | } | |
1304 | } | |
1305 | readdata: | |
1306 | /* Partial page read, transfer data into fs buffer */ | |
1307 | if (!aligned) { | |
1308 | for (j = col; j < end && read < len; j++) | |
1309 | buf[read++] = data_poi[j]; | |
1310 | this->pagebuf = realpage; | |
1311 | } else | |
1312 | read += mtd->oobblock; | |
1313 | ||
1314 | /* Apply delay or wait for ready/busy pin | |
1315 | * Do this before the AUTOINCR check, so no problems | |
1316 | * arise if a chip which does auto increment | |
1317 | * is marked as NOAUTOINCR by the board driver. | |
1318 | */ | |
1319 | if (!this->dev_ready) | |
1320 | udelay (this->chip_delay); | |
1321 | else | |
3b88775c | 1322 | nand_wait_ready(mtd); |
1da177e4 LT |
1323 | |
1324 | if (read == len) | |
1325 | break; | |
1326 | ||
1327 | /* For subsequent reads align to page boundary. */ | |
1328 | col = 0; | |
1329 | /* Increment page address */ | |
1330 | realpage++; | |
1331 | ||
1332 | page = realpage & this->pagemask; | |
1333 | /* Check, if we cross a chip boundary */ | |
1334 | if (!page) { | |
1335 | chipnr++; | |
1336 | this->select_chip(mtd, -1); | |
1337 | this->select_chip(mtd, chipnr); | |
1338 | } | |
1339 | /* Check, if the chip supports auto page increment | |
1340 | * or if we have hit a block boundary. | |
1341 | */ | |
1342 | if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) | |
1343 | sndcmd = 1; | |
1344 | } | |
1345 | ||
1346 | /* Deselect and wake up anyone waiting on the device */ | |
068e3c0a DM |
1347 | if (flags & NAND_GET_DEVICE) |
1348 | nand_release_device(mtd); | |
1da177e4 LT |
1349 | |
1350 | /* | |
1351 | * Return success, if no ECC failures, else -EBADMSG | |
1352 | * fs driver will take care of that, because | |
1353 | * retlen == desired len and result == -EBADMSG | |
1354 | */ | |
1355 | *retlen = read; | |
1356 | return ecc_failed ? -EBADMSG : 0; | |
1357 | } | |
1358 | ||
1359 | /** | |
1360 | * nand_read_oob - [MTD Interface] NAND read out-of-band | |
1361 | * @mtd: MTD device structure | |
1362 | * @from: offset to read from | |
1363 | * @len: number of bytes to read | |
1364 | * @retlen: pointer to variable to store the number of read bytes | |
1365 | * @buf: the databuffer to put data | |
1366 | * | |
1367 | * NAND read out-of-band data from the spare area | |
1368 | */ | |
1369 | static int nand_read_oob (struct mtd_info *mtd, loff_t from, size_t len, size_t * retlen, u_char * buf) | |
1370 | { | |
1371 | int i, col, page, chipnr; | |
1372 | struct nand_chip *this = mtd->priv; | |
1373 | int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1; | |
1374 | ||
1375 | DEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int) from, (int) len); | |
1376 | ||
1377 | /* Shift to get page */ | |
1378 | page = (int)(from >> this->page_shift); | |
1379 | chipnr = (int)(from >> this->chip_shift); | |
1380 | ||
1381 | /* Mask to get column */ | |
1382 | col = from & (mtd->oobsize - 1); | |
1383 | ||
1384 | /* Initialize return length value */ | |
1385 | *retlen = 0; | |
1386 | ||
1387 | /* Do not allow reads past end of device */ | |
1388 | if ((from + len) > mtd->size) { | |
1389 | DEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: Attempt read beyond end of device\n"); | |
1390 | *retlen = 0; | |
1391 | return -EINVAL; | |
1392 | } | |
1393 | ||
1394 | /* Grab the lock and see if the device is available */ | |
1395 | nand_get_device (this, mtd , FL_READING); | |
1396 | ||
1397 | /* Select the NAND device */ | |
1398 | this->select_chip(mtd, chipnr); | |
1399 | ||
1400 | /* Send the read command */ | |
1401 | this->cmdfunc (mtd, NAND_CMD_READOOB, col, page & this->pagemask); | |
1402 | /* | |
1403 | * Read the data, if we read more than one page | |
1404 | * oob data, let the device transfer the data ! | |
1405 | */ | |
1406 | i = 0; | |
1407 | while (i < len) { | |
1408 | int thislen = mtd->oobsize - col; | |
1409 | thislen = min_t(int, thislen, len); | |
1410 | this->read_buf(mtd, &buf[i], thislen); | |
1411 | i += thislen; | |
1da177e4 LT |
1412 | |
1413 | /* Read more ? */ | |
1414 | if (i < len) { | |
1415 | page++; | |
1416 | col = 0; | |
1417 | ||
1418 | /* Check, if we cross a chip boundary */ | |
1419 | if (!(page & this->pagemask)) { | |
1420 | chipnr++; | |
1421 | this->select_chip(mtd, -1); | |
1422 | this->select_chip(mtd, chipnr); | |
1423 | } | |
1424 | ||
19870da7 TG |
1425 | /* Apply delay or wait for ready/busy pin |
1426 | * Do this before the AUTOINCR check, so no problems | |
1427 | * arise if a chip which does auto increment | |
1428 | * is marked as NOAUTOINCR by the board driver. | |
1429 | */ | |
1430 | if (!this->dev_ready) | |
1431 | udelay (this->chip_delay); | |
1432 | else | |
1433 | nand_wait_ready(mtd); | |
1434 | ||
1da177e4 LT |
1435 | /* Check, if the chip supports auto page increment |
1436 | * or if we have hit a block boundary. | |
1437 | */ | |
1438 | if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) { | |
1439 | /* For subsequent page reads set offset to 0 */ | |
1440 | this->cmdfunc (mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask); | |
1441 | } | |
1442 | } | |
1443 | } | |
1444 | ||
1445 | /* Deselect and wake up anyone waiting on the device */ | |
1446 | nand_release_device(mtd); | |
1447 | ||
1448 | /* Return happy */ | |
1449 | *retlen = len; | |
1450 | return 0; | |
1451 | } | |
1452 | ||
1453 | /** | |
1454 | * nand_read_raw - [GENERIC] Read raw data including oob into buffer | |
1455 | * @mtd: MTD device structure | |
1456 | * @buf: temporary buffer | |
1457 | * @from: offset to read from | |
1458 | * @len: number of bytes to read | |
1459 | * @ooblen: number of oob data bytes to read | |
1460 | * | |
1461 | * Read raw data including oob into buffer | |
1462 | */ | |
1463 | int nand_read_raw (struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen) | |
1464 | { | |
1465 | struct nand_chip *this = mtd->priv; | |
1466 | int page = (int) (from >> this->page_shift); | |
1467 | int chip = (int) (from >> this->chip_shift); | |
1468 | int sndcmd = 1; | |
1469 | int cnt = 0; | |
1470 | int pagesize = mtd->oobblock + mtd->oobsize; | |
1471 | int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1; | |
1472 | ||
1473 | /* Do not allow reads past end of device */ | |
1474 | if ((from + len) > mtd->size) { | |
1475 | DEBUG (MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt read beyond end of device\n"); | |
1476 | return -EINVAL; | |
1477 | } | |
1478 | ||
1479 | /* Grab the lock and see if the device is available */ | |
1480 | nand_get_device (this, mtd , FL_READING); | |
1481 | ||
1482 | this->select_chip (mtd, chip); | |
1483 | ||
1484 | /* Add requested oob length */ | |
1485 | len += ooblen; | |
1486 | ||
1487 | while (len) { | |
1488 | if (sndcmd) | |
1489 | this->cmdfunc (mtd, NAND_CMD_READ0, 0, page & this->pagemask); | |
1490 | sndcmd = 0; | |
1491 | ||
1492 | this->read_buf (mtd, &buf[cnt], pagesize); | |
1493 | ||
1494 | len -= pagesize; | |
1495 | cnt += pagesize; | |
1496 | page++; | |
1497 | ||
1498 | if (!this->dev_ready) | |
1499 | udelay (this->chip_delay); | |
1500 | else | |
3b88775c | 1501 | nand_wait_ready(mtd); |
1da177e4 LT |
1502 | |
1503 | /* Check, if the chip supports auto page increment */ | |
1504 | if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) | |
1505 | sndcmd = 1; | |
1506 | } | |
1507 | ||
1508 | /* Deselect and wake up anyone waiting on the device */ | |
1509 | nand_release_device(mtd); | |
1510 | return 0; | |
1511 | } | |
1512 | ||
1513 | ||
1514 | /** | |
1515 | * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer | |
1516 | * @mtd: MTD device structure | |
1517 | * @fsbuf: buffer given by fs driver | |
1518 | * @oobsel: out of band selection structre | |
1519 | * @autoplace: 1 = place given buffer into the oob bytes | |
1520 | * @numpages: number of pages to prepare | |
1521 | * | |
1522 | * Return: | |
1523 | * 1. Filesystem buffer available and autoplacement is off, | |
1524 | * return filesystem buffer | |
1525 | * 2. No filesystem buffer or autoplace is off, return internal | |
1526 | * buffer | |
1527 | * 3. Filesystem buffer is given and autoplace selected | |
1528 | * put data from fs buffer into internal buffer and | |
1529 | * retrun internal buffer | |
1530 | * | |
1531 | * Note: The internal buffer is filled with 0xff. This must | |
1532 | * be done only once, when no autoplacement happens | |
1533 | * Autoplacement sets the buffer dirty flag, which | |
1534 | * forces the 0xff fill before using the buffer again. | |
1535 | * | |
1536 | */ | |
1537 | static u_char * nand_prepare_oobbuf (struct mtd_info *mtd, u_char *fsbuf, struct nand_oobinfo *oobsel, | |
1538 | int autoplace, int numpages) | |
1539 | { | |
1540 | struct nand_chip *this = mtd->priv; | |
1541 | int i, len, ofs; | |
1542 | ||
1543 | /* Zero copy fs supplied buffer */ | |
1544 | if (fsbuf && !autoplace) | |
1545 | return fsbuf; | |
1546 | ||
1547 | /* Check, if the buffer must be filled with ff again */ | |
1548 | if (this->oobdirty) { | |
1549 | memset (this->oob_buf, 0xff, | |
1550 | mtd->oobsize << (this->phys_erase_shift - this->page_shift)); | |
1551 | this->oobdirty = 0; | |
1552 | } | |
1553 | ||
1554 | /* If we have no autoplacement or no fs buffer use the internal one */ | |
1555 | if (!autoplace || !fsbuf) | |
1556 | return this->oob_buf; | |
1557 | ||
1558 | /* Walk through the pages and place the data */ | |
1559 | this->oobdirty = 1; | |
1560 | ofs = 0; | |
1561 | while (numpages--) { | |
1562 | for (i = 0, len = 0; len < mtd->oobavail; i++) { | |
1563 | int to = ofs + oobsel->oobfree[i][0]; | |
1564 | int num = oobsel->oobfree[i][1]; | |
1565 | memcpy (&this->oob_buf[to], fsbuf, num); | |
1566 | len += num; | |
1567 | fsbuf += num; | |
1568 | } | |
1569 | ofs += mtd->oobavail; | |
1570 | } | |
1571 | return this->oob_buf; | |
1572 | } | |
1573 | ||
1574 | #define NOTALIGNED(x) (x & (mtd->oobblock-1)) != 0 | |
1575 | ||
1576 | /** | |
1577 | * nand_write - [MTD Interface] compability function for nand_write_ecc | |
1578 | * @mtd: MTD device structure | |
1579 | * @to: offset to write to | |
1580 | * @len: number of bytes to write | |
1581 | * @retlen: pointer to variable to store the number of written bytes | |
1582 | * @buf: the data to write | |
1583 | * | |
1584 | * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL | |
1585 | * | |
1586 | */ | |
1587 | static int nand_write (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf) | |
1588 | { | |
1589 | return (nand_write_ecc (mtd, to, len, retlen, buf, NULL, NULL)); | |
1590 | } | |
1591 | ||
1592 | /** | |
1593 | * nand_write_ecc - [MTD Interface] NAND write with ECC | |
1594 | * @mtd: MTD device structure | |
1595 | * @to: offset to write to | |
1596 | * @len: number of bytes to write | |
1597 | * @retlen: pointer to variable to store the number of written bytes | |
1598 | * @buf: the data to write | |
1599 | * @eccbuf: filesystem supplied oob data buffer | |
1600 | * @oobsel: oob selection structure | |
1601 | * | |
1602 | * NAND write with ECC | |
1603 | */ | |
1604 | static int nand_write_ecc (struct mtd_info *mtd, loff_t to, size_t len, | |
1605 | size_t * retlen, const u_char * buf, u_char * eccbuf, struct nand_oobinfo *oobsel) | |
1606 | { | |
1607 | int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr; | |
1608 | int autoplace = 0, numpages, totalpages; | |
1609 | struct nand_chip *this = mtd->priv; | |
1610 | u_char *oobbuf, *bufstart; | |
1611 | int ppblock = (1 << (this->phys_erase_shift - this->page_shift)); | |
1612 | ||
1613 | DEBUG (MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len); | |
1614 | ||
1615 | /* Initialize retlen, in case of early exit */ | |
1616 | *retlen = 0; | |
1617 | ||
1618 | /* Do not allow write past end of device */ | |
1619 | if ((to + len) > mtd->size) { | |
1620 | DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n"); | |
1621 | return -EINVAL; | |
1622 | } | |
1623 | ||
1624 | /* reject writes, which are not page aligned */ | |
1625 | if (NOTALIGNED (to) || NOTALIGNED(len)) { | |
1626 | printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n"); | |
1627 | return -EINVAL; | |
1628 | } | |
1629 | ||
1630 | /* Grab the lock and see if the device is available */ | |
1631 | nand_get_device (this, mtd, FL_WRITING); | |
1632 | ||
1633 | /* Calculate chipnr */ | |
1634 | chipnr = (int)(to >> this->chip_shift); | |
1635 | /* Select the NAND device */ | |
1636 | this->select_chip(mtd, chipnr); | |
1637 | ||
1638 | /* Check, if it is write protected */ | |
1639 | if (nand_check_wp(mtd)) | |
1640 | goto out; | |
1641 | ||
1642 | /* if oobsel is NULL, use chip defaults */ | |
1643 | if (oobsel == NULL) | |
1644 | oobsel = &mtd->oobinfo; | |
1645 | ||
1646 | /* Autoplace of oob data ? Use the default placement scheme */ | |
1647 | if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) { | |
1648 | oobsel = this->autooob; | |
1649 | autoplace = 1; | |
1650 | } | |
90e260c8 TG |
1651 | if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR) |
1652 | autoplace = 1; | |
1da177e4 LT |
1653 | |
1654 | /* Setup variables and oob buffer */ | |
1655 | totalpages = len >> this->page_shift; | |
1656 | page = (int) (to >> this->page_shift); | |
1657 | /* Invalidate the page cache, if we write to the cached page */ | |
1658 | if (page <= this->pagebuf && this->pagebuf < (page + totalpages)) | |
1659 | this->pagebuf = -1; | |
1660 | ||
1661 | /* Set it relative to chip */ | |
1662 | page &= this->pagemask; | |
1663 | startpage = page; | |
1664 | /* Calc number of pages we can write in one go */ | |
1665 | numpages = min (ppblock - (startpage & (ppblock - 1)), totalpages); | |
1666 | oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, autoplace, numpages); | |
1667 | bufstart = (u_char *)buf; | |
1668 | ||
1669 | /* Loop until all data is written */ | |
1670 | while (written < len) { | |
1671 | ||
1672 | this->data_poi = (u_char*) &buf[written]; | |
1673 | /* Write one page. If this is the last page to write | |
1674 | * or the last page in this block, then use the | |
1675 | * real pageprogram command, else select cached programming | |
1676 | * if supported by the chip. | |
1677 | */ | |
1678 | ret = nand_write_page (mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0)); | |
1679 | if (ret) { | |
1680 | DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d\n", ret); | |
1681 | goto out; | |
1682 | } | |
1683 | /* Next oob page */ | |
1684 | oob += mtd->oobsize; | |
1685 | /* Update written bytes count */ | |
1686 | written += mtd->oobblock; | |
1687 | if (written == len) | |
1688 | goto cmp; | |
1689 | ||
1690 | /* Increment page address */ | |
1691 | page++; | |
1692 | ||
1693 | /* Have we hit a block boundary ? Then we have to verify and | |
1694 | * if verify is ok, we have to setup the oob buffer for | |
1695 | * the next pages. | |
1696 | */ | |
1697 | if (!(page & (ppblock - 1))){ | |
1698 | int ofs; | |
1699 | this->data_poi = bufstart; | |
1700 | ret = nand_verify_pages (mtd, this, startpage, | |
1701 | page - startpage, | |
1702 | oobbuf, oobsel, chipnr, (eccbuf != NULL)); | |
1703 | if (ret) { | |
1704 | DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret); | |
1705 | goto out; | |
1706 | } | |
1707 | *retlen = written; | |
1708 | ||
1709 | ofs = autoplace ? mtd->oobavail : mtd->oobsize; | |
1710 | if (eccbuf) | |
1711 | eccbuf += (page - startpage) * ofs; | |
1712 | totalpages -= page - startpage; | |
1713 | numpages = min (totalpages, ppblock); | |
1714 | page &= this->pagemask; | |
1715 | startpage = page; | |
1716 | oobbuf = nand_prepare_oobbuf (mtd, eccbuf, oobsel, | |
1717 | autoplace, numpages); | |
1718 | /* Check, if we cross a chip boundary */ | |
1719 | if (!page) { | |
1720 | chipnr++; | |
1721 | this->select_chip(mtd, -1); | |
1722 | this->select_chip(mtd, chipnr); | |
1723 | } | |
1724 | } | |
1725 | } | |
1726 | /* Verify the remaining pages */ | |
1727 | cmp: | |
1728 | this->data_poi = bufstart; | |
1729 | ret = nand_verify_pages (mtd, this, startpage, totalpages, | |
1730 | oobbuf, oobsel, chipnr, (eccbuf != NULL)); | |
1731 | if (!ret) | |
1732 | *retlen = written; | |
1733 | else | |
1734 | DEBUG (MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret); | |
1735 | ||
1736 | out: | |
1737 | /* Deselect and wake up anyone waiting on the device */ | |
1738 | nand_release_device(mtd); | |
1739 | ||
1740 | return ret; | |
1741 | } | |
1742 | ||
1743 | ||
1744 | /** | |
1745 | * nand_write_oob - [MTD Interface] NAND write out-of-band | |
1746 | * @mtd: MTD device structure | |
1747 | * @to: offset to write to | |
1748 | * @len: number of bytes to write | |
1749 | * @retlen: pointer to variable to store the number of written bytes | |
1750 | * @buf: the data to write | |
1751 | * | |
1752 | * NAND write out-of-band | |
1753 | */ | |
1754 | static int nand_write_oob (struct mtd_info *mtd, loff_t to, size_t len, size_t * retlen, const u_char * buf) | |
1755 | { | |
1756 | int column, page, status, ret = -EIO, chipnr; | |
1757 | struct nand_chip *this = mtd->priv; | |
1758 | ||
1759 | DEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int) to, (int) len); | |
1760 | ||
1761 | /* Shift to get page */ | |
1762 | page = (int) (to >> this->page_shift); | |
1763 | chipnr = (int) (to >> this->chip_shift); | |
1764 | ||
1765 | /* Mask to get column */ | |
1766 | column = to & (mtd->oobsize - 1); | |
1767 | ||
1768 | /* Initialize return length value */ | |
1769 | *retlen = 0; | |
1770 | ||
1771 | /* Do not allow write past end of page */ | |
1772 | if ((column + len) > mtd->oobsize) { | |
1773 | DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n"); | |
1774 | return -EINVAL; | |
1775 | } | |
1776 | ||
1777 | /* Grab the lock and see if the device is available */ | |
1778 | nand_get_device (this, mtd, FL_WRITING); | |
1779 | ||
1780 | /* Select the NAND device */ | |
1781 | this->select_chip(mtd, chipnr); | |
1782 | ||
1783 | /* Reset the chip. Some chips (like the Toshiba TC5832DC found | |
1784 | in one of my DiskOnChip 2000 test units) will clear the whole | |
1785 | data page too if we don't do this. I have no clue why, but | |
1786 | I seem to have 'fixed' it in the doc2000 driver in | |
1787 | August 1999. dwmw2. */ | |
1788 | this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); | |
1789 | ||
1790 | /* Check, if it is write protected */ | |
1791 | if (nand_check_wp(mtd)) | |
1792 | goto out; | |
1793 | ||
1794 | /* Invalidate the page cache, if we write to the cached page */ | |
1795 | if (page == this->pagebuf) | |
1796 | this->pagebuf = -1; | |
1797 | ||
1798 | if (NAND_MUST_PAD(this)) { | |
1799 | /* Write out desired data */ | |
1800 | this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock, page & this->pagemask); | |
1801 | /* prepad 0xff for partial programming */ | |
1802 | this->write_buf(mtd, ffchars, column); | |
1803 | /* write data */ | |
1804 | this->write_buf(mtd, buf, len); | |
1805 | /* postpad 0xff for partial programming */ | |
1806 | this->write_buf(mtd, ffchars, mtd->oobsize - (len+column)); | |
1807 | } else { | |
1808 | /* Write out desired data */ | |
1809 | this->cmdfunc (mtd, NAND_CMD_SEQIN, mtd->oobblock + column, page & this->pagemask); | |
1810 | /* write data */ | |
1811 | this->write_buf(mtd, buf, len); | |
1812 | } | |
1813 | /* Send command to program the OOB data */ | |
1814 | this->cmdfunc (mtd, NAND_CMD_PAGEPROG, -1, -1); | |
1815 | ||
1816 | status = this->waitfunc (mtd, this, FL_WRITING); | |
1817 | ||
1818 | /* See if device thinks it succeeded */ | |
a4ab4c5d | 1819 | if (status & NAND_STATUS_FAIL) { |
1da177e4 LT |
1820 | DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page); |
1821 | ret = -EIO; | |
1822 | goto out; | |
1823 | } | |
1824 | /* Return happy */ | |
1825 | *retlen = len; | |
1826 | ||
1827 | #ifdef CONFIG_MTD_NAND_VERIFY_WRITE | |
1828 | /* Send command to read back the data */ | |
1829 | this->cmdfunc (mtd, NAND_CMD_READOOB, column, page & this->pagemask); | |
1830 | ||
1831 | if (this->verify_buf(mtd, buf, len)) { | |
1832 | DEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page); | |
1833 | ret = -EIO; | |
1834 | goto out; | |
1835 | } | |
1836 | #endif | |
1837 | ret = 0; | |
1838 | out: | |
1839 | /* Deselect and wake up anyone waiting on the device */ | |
1840 | nand_release_device(mtd); | |
1841 | ||
1842 | return ret; | |
1843 | } | |
1844 | ||
1845 | ||
1846 | /** | |
1847 | * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc | |
1848 | * @mtd: MTD device structure | |
1849 | * @vecs: the iovectors to write | |
1850 | * @count: number of vectors | |
1851 | * @to: offset to write to | |
1852 | * @retlen: pointer to variable to store the number of written bytes | |
1853 | * | |
1854 | * NAND write with kvec. This just calls the ecc function | |
1855 | */ | |
1856 | static int nand_writev (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, | |
1857 | loff_t to, size_t * retlen) | |
1858 | { | |
1859 | return (nand_writev_ecc (mtd, vecs, count, to, retlen, NULL, NULL)); | |
1860 | } | |
1861 | ||
1862 | /** | |
1863 | * nand_writev_ecc - [MTD Interface] write with iovec with ecc | |
1864 | * @mtd: MTD device structure | |
1865 | * @vecs: the iovectors to write | |
1866 | * @count: number of vectors | |
1867 | * @to: offset to write to | |
1868 | * @retlen: pointer to variable to store the number of written bytes | |
1869 | * @eccbuf: filesystem supplied oob data buffer | |
1870 | * @oobsel: oob selection structure | |
1871 | * | |
1872 | * NAND write with iovec with ecc | |
1873 | */ | |
1874 | static int nand_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, | |
1875 | loff_t to, size_t * retlen, u_char *eccbuf, struct nand_oobinfo *oobsel) | |
1876 | { | |
1877 | int i, page, len, total_len, ret = -EIO, written = 0, chipnr; | |
1878 | int oob, numpages, autoplace = 0, startpage; | |
1879 | struct nand_chip *this = mtd->priv; | |
1880 | int ppblock = (1 << (this->phys_erase_shift - this->page_shift)); | |
1881 | u_char *oobbuf, *bufstart; | |
1882 | ||
1883 | /* Preset written len for early exit */ | |
1884 | *retlen = 0; | |
1885 | ||
1886 | /* Calculate total length of data */ | |
1887 | total_len = 0; | |
1888 | for (i = 0; i < count; i++) | |
1889 | total_len += (int) vecs[i].iov_len; | |
1890 | ||
1891 | DEBUG (MTD_DEBUG_LEVEL3, | |
1892 | "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int) to, (unsigned int) total_len, count); | |
1893 | ||
1894 | /* Do not allow write past end of page */ | |
1895 | if ((to + total_len) > mtd->size) { | |
1896 | DEBUG (MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n"); | |
1897 | return -EINVAL; | |
1898 | } | |
1899 | ||
1900 | /* reject writes, which are not page aligned */ | |
1901 | if (NOTALIGNED (to) || NOTALIGNED(total_len)) { | |
1902 | printk (KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n"); | |
1903 | return -EINVAL; | |
1904 | } | |
1905 | ||
1906 | /* Grab the lock and see if the device is available */ | |
1907 | nand_get_device (this, mtd, FL_WRITING); | |
1908 | ||
1909 | /* Get the current chip-nr */ | |
1910 | chipnr = (int) (to >> this->chip_shift); | |
1911 | /* Select the NAND device */ | |
1912 | this->select_chip(mtd, chipnr); | |
1913 | ||
1914 | /* Check, if it is write protected */ | |
1915 | if (nand_check_wp(mtd)) | |
1916 | goto out; | |
1917 | ||
1918 | /* if oobsel is NULL, use chip defaults */ | |
1919 | if (oobsel == NULL) | |
1920 | oobsel = &mtd->oobinfo; | |
1921 | ||
1922 | /* Autoplace of oob data ? Use the default placement scheme */ | |
1923 | if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) { | |
1924 | oobsel = this->autooob; | |
1925 | autoplace = 1; | |
1926 | } | |
90e260c8 TG |
1927 | if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR) |
1928 | autoplace = 1; | |
1da177e4 LT |
1929 | |
1930 | /* Setup start page */ | |
1931 | page = (int) (to >> this->page_shift); | |
1932 | /* Invalidate the page cache, if we write to the cached page */ | |
1933 | if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift)) | |
1934 | this->pagebuf = -1; | |
1935 | ||
1936 | startpage = page & this->pagemask; | |
1937 | ||
1938 | /* Loop until all kvec' data has been written */ | |
1939 | len = 0; | |
1940 | while (count) { | |
1941 | /* If the given tuple is >= pagesize then | |
1942 | * write it out from the iov | |
1943 | */ | |
1944 | if ((vecs->iov_len - len) >= mtd->oobblock) { | |
1945 | /* Calc number of pages we can write | |
1946 | * out of this iov in one go */ | |
1947 | numpages = (vecs->iov_len - len) >> this->page_shift; | |
1948 | /* Do not cross block boundaries */ | |
1949 | numpages = min (ppblock - (startpage & (ppblock - 1)), numpages); | |
1950 | oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages); | |
1951 | bufstart = (u_char *)vecs->iov_base; | |
1952 | bufstart += len; | |
1953 | this->data_poi = bufstart; | |
1954 | oob = 0; | |
1955 | for (i = 1; i <= numpages; i++) { | |
1956 | /* Write one page. If this is the last page to write | |
1957 | * then use the real pageprogram command, else select | |
1958 | * cached programming if supported by the chip. | |
1959 | */ | |
1960 | ret = nand_write_page (mtd, this, page & this->pagemask, | |
1961 | &oobbuf[oob], oobsel, i != numpages); | |
1962 | if (ret) | |
1963 | goto out; | |
1964 | this->data_poi += mtd->oobblock; | |
1965 | len += mtd->oobblock; | |
1966 | oob += mtd->oobsize; | |
1967 | page++; | |
1968 | } | |
1969 | /* Check, if we have to switch to the next tuple */ | |
1970 | if (len >= (int) vecs->iov_len) { | |
1971 | vecs++; | |
1972 | len = 0; | |
1973 | count--; | |
1974 | } | |
1975 | } else { | |
1976 | /* We must use the internal buffer, read data out of each | |
1977 | * tuple until we have a full page to write | |
1978 | */ | |
1979 | int cnt = 0; | |
1980 | while (cnt < mtd->oobblock) { | |
1981 | if (vecs->iov_base != NULL && vecs->iov_len) | |
1982 | this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++]; | |
1983 | /* Check, if we have to switch to the next tuple */ | |
1984 | if (len >= (int) vecs->iov_len) { | |
1985 | vecs++; | |
1986 | len = 0; | |
1987 | count--; | |
1988 | } | |
1989 | } | |
1990 | this->pagebuf = page; | |
1991 | this->data_poi = this->data_buf; | |
1992 | bufstart = this->data_poi; | |
1993 | numpages = 1; | |
1994 | oobbuf = nand_prepare_oobbuf (mtd, NULL, oobsel, autoplace, numpages); | |
1995 | ret = nand_write_page (mtd, this, page & this->pagemask, | |
1996 | oobbuf, oobsel, 0); | |
1997 | if (ret) | |
1998 | goto out; | |
1999 | page++; | |
2000 | } | |
2001 | ||
2002 | this->data_poi = bufstart; | |
2003 | ret = nand_verify_pages (mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0); | |
2004 | if (ret) | |
2005 | goto out; | |
2006 | ||
2007 | written += mtd->oobblock * numpages; | |
2008 | /* All done ? */ | |
2009 | if (!count) | |
2010 | break; | |
2011 | ||
2012 | startpage = page & this->pagemask; | |
2013 | /* Check, if we cross a chip boundary */ | |
2014 | if (!startpage) { | |
2015 | chipnr++; | |
2016 | this->select_chip(mtd, -1); | |
2017 | this->select_chip(mtd, chipnr); | |
2018 | } | |
2019 | } | |
2020 | ret = 0; | |
2021 | out: | |
2022 | /* Deselect and wake up anyone waiting on the device */ | |
2023 | nand_release_device(mtd); | |
2024 | ||
2025 | *retlen = written; | |
2026 | return ret; | |
2027 | } | |
2028 | ||
2029 | /** | |
2030 | * single_erease_cmd - [GENERIC] NAND standard block erase command function | |
2031 | * @mtd: MTD device structure | |
2032 | * @page: the page address of the block which will be erased | |
2033 | * | |
2034 | * Standard erase command for NAND chips | |
2035 | */ | |
2036 | static void single_erase_cmd (struct mtd_info *mtd, int page) | |
2037 | { | |
2038 | struct nand_chip *this = mtd->priv; | |
2039 | /* Send commands to erase a block */ | |
2040 | this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page); | |
2041 | this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1); | |
2042 | } | |
2043 | ||
2044 | /** | |
2045 | * multi_erease_cmd - [GENERIC] AND specific block erase command function | |
2046 | * @mtd: MTD device structure | |
2047 | * @page: the page address of the block which will be erased | |
2048 | * | |
2049 | * AND multi block erase command function | |
2050 | * Erase 4 consecutive blocks | |
2051 | */ | |
2052 | static void multi_erase_cmd (struct mtd_info *mtd, int page) | |
2053 | { | |
2054 | struct nand_chip *this = mtd->priv; | |
2055 | /* Send commands to erase a block */ | |
2056 | this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++); | |
2057 | this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++); | |
2058 | this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page++); | |
2059 | this->cmdfunc (mtd, NAND_CMD_ERASE1, -1, page); | |
2060 | this->cmdfunc (mtd, NAND_CMD_ERASE2, -1, -1); | |
2061 | } | |
2062 | ||
2063 | /** | |
2064 | * nand_erase - [MTD Interface] erase block(s) | |
2065 | * @mtd: MTD device structure | |
2066 | * @instr: erase instruction | |
2067 | * | |
2068 | * Erase one ore more blocks | |
2069 | */ | |
2070 | static int nand_erase (struct mtd_info *mtd, struct erase_info *instr) | |
2071 | { | |
2072 | return nand_erase_nand (mtd, instr, 0); | |
2073 | } | |
2074 | ||
30f464b7 | 2075 | #define BBT_PAGE_MASK 0xffffff3f |
1da177e4 LT |
2076 | /** |
2077 | * nand_erase_intern - [NAND Interface] erase block(s) | |
2078 | * @mtd: MTD device structure | |
2079 | * @instr: erase instruction | |
2080 | * @allowbbt: allow erasing the bbt area | |
2081 | * | |
2082 | * Erase one ore more blocks | |
2083 | */ | |
2084 | int nand_erase_nand (struct mtd_info *mtd, struct erase_info *instr, int allowbbt) | |
2085 | { | |
2086 | int page, len, status, pages_per_block, ret, chipnr; | |
2087 | struct nand_chip *this = mtd->priv; | |
30f464b7 DM |
2088 | int rewrite_bbt[NAND_MAX_CHIPS]={0}; /* flags to indicate the page, if bbt needs to be rewritten. */ |
2089 | unsigned int bbt_masked_page; /* bbt mask to compare to page being erased. */ | |
2090 | /* It is used to see if the current page is in the same */ | |
2091 | /* 256 block group and the same bank as the bbt. */ | |
1da177e4 LT |
2092 | |
2093 | DEBUG (MTD_DEBUG_LEVEL3, | |
2094 | "nand_erase: start = 0x%08x, len = %i\n", (unsigned int) instr->addr, (unsigned int) instr->len); | |
2095 | ||
2096 | /* Start address must align on block boundary */ | |
2097 | if (instr->addr & ((1 << this->phys_erase_shift) - 1)) { | |
2098 | DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n"); | |
2099 | return -EINVAL; | |
2100 | } | |
2101 | ||
2102 | /* Length must align on block boundary */ | |
2103 | if (instr->len & ((1 << this->phys_erase_shift) - 1)) { | |
2104 | DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n"); | |
2105 | return -EINVAL; | |
2106 | } | |
2107 | ||
2108 | /* Do not allow erase past end of device */ | |
2109 | if ((instr->len + instr->addr) > mtd->size) { | |
2110 | DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n"); | |
2111 | return -EINVAL; | |
2112 | } | |
2113 | ||
2114 | instr->fail_addr = 0xffffffff; | |
2115 | ||
2116 | /* Grab the lock and see if the device is available */ | |
2117 | nand_get_device (this, mtd, FL_ERASING); | |
2118 | ||
2119 | /* Shift to get first page */ | |
2120 | page = (int) (instr->addr >> this->page_shift); | |
2121 | chipnr = (int) (instr->addr >> this->chip_shift); | |
2122 | ||
2123 | /* Calculate pages in each block */ | |
2124 | pages_per_block = 1 << (this->phys_erase_shift - this->page_shift); | |
2125 | ||
2126 | /* Select the NAND device */ | |
2127 | this->select_chip(mtd, chipnr); | |
2128 | ||
2129 | /* Check the WP bit */ | |
2130 | /* Check, if it is write protected */ | |
2131 | if (nand_check_wp(mtd)) { | |
2132 | DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n"); | |
2133 | instr->state = MTD_ERASE_FAILED; | |
2134 | goto erase_exit; | |
2135 | } | |
2136 | ||
30f464b7 DM |
2137 | /* if BBT requires refresh, set the BBT page mask to see if the BBT should be rewritten */ |
2138 | if (this->options & BBT_AUTO_REFRESH) { | |
2139 | bbt_masked_page = this->bbt_td->pages[chipnr] & BBT_PAGE_MASK; | |
2140 | } else { | |
2141 | bbt_masked_page = 0xffffffff; /* should not match anything */ | |
2142 | } | |
2143 | ||
1da177e4 LT |
2144 | /* Loop through the pages */ |
2145 | len = instr->len; | |
2146 | ||
2147 | instr->state = MTD_ERASING; | |
2148 | ||
2149 | while (len) { | |
2150 | /* Check if we have a bad block, we do not erase bad blocks ! */ | |
2151 | if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) { | |
2152 | printk (KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page); | |
2153 | instr->state = MTD_ERASE_FAILED; | |
2154 | goto erase_exit; | |
2155 | } | |
2156 | ||
2157 | /* Invalidate the page cache, if we erase the block which contains | |
2158 | the current cached page */ | |
2159 | if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block)) | |
2160 | this->pagebuf = -1; | |
2161 | ||
2162 | this->erase_cmd (mtd, page & this->pagemask); | |
2163 | ||
2164 | status = this->waitfunc (mtd, this, FL_ERASING); | |
2165 | ||
068e3c0a DM |
2166 | /* See if operation failed and additional status checks are available */ |
2167 | if ((status & NAND_STATUS_FAIL) && (this->errstat)) { | |
2168 | status = this->errstat(mtd, this, FL_ERASING, status, page); | |
2169 | } | |
2170 | ||
1da177e4 | 2171 | /* See if block erase succeeded */ |
a4ab4c5d | 2172 | if (status & NAND_STATUS_FAIL) { |
1da177e4 LT |
2173 | DEBUG (MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page); |
2174 | instr->state = MTD_ERASE_FAILED; | |
2175 | instr->fail_addr = (page << this->page_shift); | |
2176 | goto erase_exit; | |
2177 | } | |
30f464b7 DM |
2178 | |
2179 | /* if BBT requires refresh, set the BBT rewrite flag to the page being erased */ | |
2180 | if (this->options & BBT_AUTO_REFRESH) { | |
2181 | if (((page & BBT_PAGE_MASK) == bbt_masked_page) && | |
2182 | (page != this->bbt_td->pages[chipnr])) { | |
2183 | rewrite_bbt[chipnr] = (page << this->page_shift); | |
2184 | } | |
2185 | } | |
1da177e4 LT |
2186 | |
2187 | /* Increment page address and decrement length */ | |
2188 | len -= (1 << this->phys_erase_shift); | |
2189 | page += pages_per_block; | |
2190 | ||
2191 | /* Check, if we cross a chip boundary */ | |
2192 | if (len && !(page & this->pagemask)) { | |
2193 | chipnr++; | |
2194 | this->select_chip(mtd, -1); | |
2195 | this->select_chip(mtd, chipnr); | |
30f464b7 DM |
2196 | |
2197 | /* if BBT requires refresh and BBT-PERCHIP, | |
2198 | * set the BBT page mask to see if this BBT should be rewritten */ | |
2199 | if ((this->options & BBT_AUTO_REFRESH) && (this->bbt_td->options & NAND_BBT_PERCHIP)) { | |
2200 | bbt_masked_page = this->bbt_td->pages[chipnr] & BBT_PAGE_MASK; | |
2201 | } | |
2202 | ||
1da177e4 LT |
2203 | } |
2204 | } | |
2205 | instr->state = MTD_ERASE_DONE; | |
2206 | ||
2207 | erase_exit: | |
2208 | ||
2209 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; | |
2210 | /* Do call back function */ | |
2211 | if (!ret) | |
2212 | mtd_erase_callback(instr); | |
2213 | ||
2214 | /* Deselect and wake up anyone waiting on the device */ | |
2215 | nand_release_device(mtd); | |
2216 | ||
30f464b7 DM |
2217 | /* if BBT requires refresh and erase was successful, rewrite any selected bad block tables */ |
2218 | if ((this->options & BBT_AUTO_REFRESH) && (!ret)) { | |
2219 | for (chipnr = 0; chipnr < this->numchips; chipnr++) { | |
2220 | if (rewrite_bbt[chipnr]) { | |
2221 | /* update the BBT for chip */ | |
2222 | DEBUG (MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt (%d:0x%0x 0x%0x)\n", | |
2223 | chipnr, rewrite_bbt[chipnr], this->bbt_td->pages[chipnr]); | |
2224 | nand_update_bbt (mtd, rewrite_bbt[chipnr]); | |
2225 | } | |
2226 | } | |
2227 | } | |
2228 | ||
1da177e4 LT |
2229 | /* Return more or less happy */ |
2230 | return ret; | |
2231 | } | |
2232 | ||
2233 | /** | |
2234 | * nand_sync - [MTD Interface] sync | |
2235 | * @mtd: MTD device structure | |
2236 | * | |
2237 | * Sync is actually a wait for chip ready function | |
2238 | */ | |
2239 | static void nand_sync (struct mtd_info *mtd) | |
2240 | { | |
2241 | struct nand_chip *this = mtd->priv; | |
2242 | ||
2243 | DEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n"); | |
2244 | ||
2245 | /* Grab the lock and see if the device is available */ | |
2246 | nand_get_device (this, mtd, FL_SYNCING); | |
2247 | /* Release it and go back */ | |
2248 | nand_release_device (mtd); | |
2249 | } | |
2250 | ||
2251 | ||
2252 | /** | |
2253 | * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad | |
2254 | * @mtd: MTD device structure | |
2255 | * @ofs: offset relative to mtd start | |
2256 | */ | |
2257 | static int nand_block_isbad (struct mtd_info *mtd, loff_t ofs) | |
2258 | { | |
2259 | /* Check for invalid offset */ | |
2260 | if (ofs > mtd->size) | |
2261 | return -EINVAL; | |
2262 | ||
2263 | return nand_block_checkbad (mtd, ofs, 1, 0); | |
2264 | } | |
2265 | ||
2266 | /** | |
2267 | * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad | |
2268 | * @mtd: MTD device structure | |
2269 | * @ofs: offset relative to mtd start | |
2270 | */ | |
2271 | static int nand_block_markbad (struct mtd_info *mtd, loff_t ofs) | |
2272 | { | |
2273 | struct nand_chip *this = mtd->priv; | |
2274 | int ret; | |
2275 | ||
2276 | if ((ret = nand_block_isbad(mtd, ofs))) { | |
2277 | /* If it was bad already, return success and do nothing. */ | |
2278 | if (ret > 0) | |
2279 | return 0; | |
2280 | return ret; | |
2281 | } | |
2282 | ||
2283 | return this->block_markbad(mtd, ofs); | |
2284 | } | |
2285 | ||
2286 | /** | |
2287 | * nand_scan - [NAND Interface] Scan for the NAND device | |
2288 | * @mtd: MTD device structure | |
2289 | * @maxchips: Number of chips to scan for | |
2290 | * | |
2291 | * This fills out all the not initialized function pointers | |
2292 | * with the defaults. | |
2293 | * The flash ID is read and the mtd/chip structures are | |
2294 | * filled with the appropriate values. Buffers are allocated if | |
2295 | * they are not provided by the board driver | |
2296 | * | |
2297 | */ | |
2298 | int nand_scan (struct mtd_info *mtd, int maxchips) | |
2299 | { | |
3b946e3f | 2300 | int i, nand_maf_id, nand_dev_id, busw, maf_id; |
1da177e4 LT |
2301 | struct nand_chip *this = mtd->priv; |
2302 | ||
2303 | /* Get buswidth to select the correct functions*/ | |
2304 | busw = this->options & NAND_BUSWIDTH_16; | |
2305 | ||
2306 | /* check for proper chip_delay setup, set 20us if not */ | |
2307 | if (!this->chip_delay) | |
2308 | this->chip_delay = 20; | |
2309 | ||
2310 | /* check, if a user supplied command function given */ | |
2311 | if (this->cmdfunc == NULL) | |
2312 | this->cmdfunc = nand_command; | |
2313 | ||
2314 | /* check, if a user supplied wait function given */ | |
2315 | if (this->waitfunc == NULL) | |
2316 | this->waitfunc = nand_wait; | |
2317 | ||
2318 | if (!this->select_chip) | |
2319 | this->select_chip = nand_select_chip; | |
2320 | if (!this->write_byte) | |
2321 | this->write_byte = busw ? nand_write_byte16 : nand_write_byte; | |
2322 | if (!this->read_byte) | |
2323 | this->read_byte = busw ? nand_read_byte16 : nand_read_byte; | |
2324 | if (!this->write_word) | |
2325 | this->write_word = nand_write_word; | |
2326 | if (!this->read_word) | |
2327 | this->read_word = nand_read_word; | |
2328 | if (!this->block_bad) | |
2329 | this->block_bad = nand_block_bad; | |
2330 | if (!this->block_markbad) | |
2331 | this->block_markbad = nand_default_block_markbad; | |
2332 | if (!this->write_buf) | |
2333 | this->write_buf = busw ? nand_write_buf16 : nand_write_buf; | |
2334 | if (!this->read_buf) | |
2335 | this->read_buf = busw ? nand_read_buf16 : nand_read_buf; | |
2336 | if (!this->verify_buf) | |
2337 | this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf; | |
2338 | if (!this->scan_bbt) | |
2339 | this->scan_bbt = nand_default_bbt; | |
2340 | ||
2341 | /* Select the device */ | |
2342 | this->select_chip(mtd, 0); | |
2343 | ||
2344 | /* Send the command for reading device ID */ | |
2345 | this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1); | |
2346 | ||
2347 | /* Read manufacturer and device IDs */ | |
2348 | nand_maf_id = this->read_byte(mtd); | |
2349 | nand_dev_id = this->read_byte(mtd); | |
2350 | ||
2351 | /* Print and store flash device information */ | |
2352 | for (i = 0; nand_flash_ids[i].name != NULL; i++) { | |
2353 | ||
2354 | if (nand_dev_id != nand_flash_ids[i].id) | |
2355 | continue; | |
2356 | ||
2357 | if (!mtd->name) mtd->name = nand_flash_ids[i].name; | |
2358 | this->chipsize = nand_flash_ids[i].chipsize << 20; | |
2359 | ||
2360 | /* New devices have all the information in additional id bytes */ | |
2361 | if (!nand_flash_ids[i].pagesize) { | |
2362 | int extid; | |
2363 | /* The 3rd id byte contains non relevant data ATM */ | |
2364 | extid = this->read_byte(mtd); | |
2365 | /* The 4th id byte is the important one */ | |
2366 | extid = this->read_byte(mtd); | |
2367 | /* Calc pagesize */ | |
2368 | mtd->oobblock = 1024 << (extid & 0x3); | |
2369 | extid >>= 2; | |
2370 | /* Calc oobsize */ | |
2371 | mtd->oobsize = (8 << (extid & 0x03)) * (mtd->oobblock / 512); | |
2372 | extid >>= 2; | |
2373 | /* Calc blocksize. Blocksize is multiples of 64KiB */ | |
2374 | mtd->erasesize = (64 * 1024) << (extid & 0x03); | |
2375 | extid >>= 2; | |
2376 | /* Get buswidth information */ | |
2377 | busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0; | |
2378 | ||
2379 | } else { | |
2380 | /* Old devices have this data hardcoded in the | |
2381 | * device id table */ | |
2382 | mtd->erasesize = nand_flash_ids[i].erasesize; | |
2383 | mtd->oobblock = nand_flash_ids[i].pagesize; | |
2384 | mtd->oobsize = mtd->oobblock / 32; | |
2385 | busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16; | |
2386 | } | |
2387 | ||
0ea4a755 KP |
2388 | /* Try to identify manufacturer */ |
2389 | for (maf_id = 0; nand_manuf_ids[maf_id].id != 0x0; maf_id++) { | |
2390 | if (nand_manuf_ids[maf_id].id == nand_maf_id) | |
2391 | break; | |
2392 | } | |
2393 | ||
1da177e4 LT |
2394 | /* Check, if buswidth is correct. Hardware drivers should set |
2395 | * this correct ! */ | |
2396 | if (busw != (this->options & NAND_BUSWIDTH_16)) { | |
2397 | printk (KERN_INFO "NAND device: Manufacturer ID:" | |
2398 | " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id, | |
0ea4a755 | 2399 | nand_manuf_ids[maf_id].name , mtd->name); |
1da177e4 LT |
2400 | printk (KERN_WARNING |
2401 | "NAND bus width %d instead %d bit\n", | |
2402 | (this->options & NAND_BUSWIDTH_16) ? 16 : 8, | |
2403 | busw ? 16 : 8); | |
2404 | this->select_chip(mtd, -1); | |
2405 | return 1; | |
2406 | } | |
2407 | ||
2408 | /* Calculate the address shift from the page size */ | |
2409 | this->page_shift = ffs(mtd->oobblock) - 1; | |
2410 | this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1; | |
2411 | this->chip_shift = ffs(this->chipsize) - 1; | |
2412 | ||
2413 | /* Set the bad block position */ | |
2414 | this->badblockpos = mtd->oobblock > 512 ? | |
2415 | NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS; | |
2416 | ||
2417 | /* Get chip options, preserve non chip based options */ | |
2418 | this->options &= ~NAND_CHIPOPTIONS_MSK; | |
2419 | this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK; | |
2420 | /* Set this as a default. Board drivers can override it, if neccecary */ | |
2421 | this->options |= NAND_NO_AUTOINCR; | |
2422 | /* Check if this is a not a samsung device. Do not clear the options | |
2423 | * for chips which are not having an extended id. | |
2424 | */ | |
2425 | if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize) | |
2426 | this->options &= ~NAND_SAMSUNG_LP_OPTIONS; | |
2427 | ||
2428 | /* Check for AND chips with 4 page planes */ | |
2429 | if (this->options & NAND_4PAGE_ARRAY) | |
2430 | this->erase_cmd = multi_erase_cmd; | |
2431 | else | |
2432 | this->erase_cmd = single_erase_cmd; | |
2433 | ||
2434 | /* Do not replace user supplied command function ! */ | |
2435 | if (mtd->oobblock > 512 && this->cmdfunc == nand_command) | |
2436 | this->cmdfunc = nand_command_lp; | |
2437 | ||
1da177e4 LT |
2438 | printk (KERN_INFO "NAND device: Manufacturer ID:" |
2439 | " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id, | |
0ea4a755 | 2440 | nand_manuf_ids[maf_id].name , nand_flash_ids[i].name); |
1da177e4 LT |
2441 | break; |
2442 | } | |
2443 | ||
2444 | if (!nand_flash_ids[i].name) { | |
2445 | printk (KERN_WARNING "No NAND device found!!!\n"); | |
2446 | this->select_chip(mtd, -1); | |
2447 | return 1; | |
2448 | } | |
2449 | ||
2450 | for (i=1; i < maxchips; i++) { | |
2451 | this->select_chip(mtd, i); | |
2452 | ||
2453 | /* Send the command for reading device ID */ | |
2454 | this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1); | |
2455 | ||
2456 | /* Read manufacturer and device IDs */ | |
2457 | if (nand_maf_id != this->read_byte(mtd) || | |
2458 | nand_dev_id != this->read_byte(mtd)) | |
2459 | break; | |
2460 | } | |
2461 | if (i > 1) | |
2462 | printk(KERN_INFO "%d NAND chips detected\n", i); | |
2463 | ||
2464 | /* Allocate buffers, if neccecary */ | |
2465 | if (!this->oob_buf) { | |
2466 | size_t len; | |
2467 | len = mtd->oobsize << (this->phys_erase_shift - this->page_shift); | |
2468 | this->oob_buf = kmalloc (len, GFP_KERNEL); | |
2469 | if (!this->oob_buf) { | |
2470 | printk (KERN_ERR "nand_scan(): Cannot allocate oob_buf\n"); | |
2471 | return -ENOMEM; | |
2472 | } | |
2473 | this->options |= NAND_OOBBUF_ALLOC; | |
2474 | } | |
2475 | ||
2476 | if (!this->data_buf) { | |
2477 | size_t len; | |
2478 | len = mtd->oobblock + mtd->oobsize; | |
2479 | this->data_buf = kmalloc (len, GFP_KERNEL); | |
2480 | if (!this->data_buf) { | |
2481 | if (this->options & NAND_OOBBUF_ALLOC) | |
2482 | kfree (this->oob_buf); | |
2483 | printk (KERN_ERR "nand_scan(): Cannot allocate data_buf\n"); | |
2484 | return -ENOMEM; | |
2485 | } | |
2486 | this->options |= NAND_DATABUF_ALLOC; | |
2487 | } | |
2488 | ||
2489 | /* Store the number of chips and calc total size for mtd */ | |
2490 | this->numchips = i; | |
2491 | mtd->size = i * this->chipsize; | |
2492 | /* Convert chipsize to number of pages per chip -1. */ | |
2493 | this->pagemask = (this->chipsize >> this->page_shift) - 1; | |
2494 | /* Preset the internal oob buffer */ | |
2495 | memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift)); | |
2496 | ||
2497 | /* If no default placement scheme is given, select an | |
2498 | * appropriate one */ | |
2499 | if (!this->autooob) { | |
2500 | /* Select the appropriate default oob placement scheme for | |
2501 | * placement agnostic filesystems */ | |
2502 | switch (mtd->oobsize) { | |
2503 | case 8: | |
2504 | this->autooob = &nand_oob_8; | |
2505 | break; | |
2506 | case 16: | |
2507 | this->autooob = &nand_oob_16; | |
2508 | break; | |
2509 | case 64: | |
2510 | this->autooob = &nand_oob_64; | |
2511 | break; | |
2512 | default: | |
2513 | printk (KERN_WARNING "No oob scheme defined for oobsize %d\n", | |
2514 | mtd->oobsize); | |
2515 | BUG(); | |
2516 | } | |
2517 | } | |
2518 | ||
2519 | /* The number of bytes available for the filesystem to place fs dependend | |
2520 | * oob data */ | |
998cf640 TG |
2521 | mtd->oobavail = 0; |
2522 | for (i = 0; this->autooob->oobfree[i][1]; i++) | |
2523 | mtd->oobavail += this->autooob->oobfree[i][1]; | |
1da177e4 LT |
2524 | |
2525 | /* | |
2526 | * check ECC mode, default to software | |
2527 | * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize | |
2528 | * fallback to software ECC | |
2529 | */ | |
2530 | this->eccsize = 256; /* set default eccsize */ | |
2531 | this->eccbytes = 3; | |
2532 | ||
2533 | switch (this->eccmode) { | |
2534 | case NAND_ECC_HW12_2048: | |
2535 | if (mtd->oobblock < 2048) { | |
2536 | printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", | |
2537 | mtd->oobblock); | |
2538 | this->eccmode = NAND_ECC_SOFT; | |
2539 | this->calculate_ecc = nand_calculate_ecc; | |
2540 | this->correct_data = nand_correct_data; | |
2541 | } else | |
2542 | this->eccsize = 2048; | |
2543 | break; | |
2544 | ||
2545 | case NAND_ECC_HW3_512: | |
2546 | case NAND_ECC_HW6_512: | |
2547 | case NAND_ECC_HW8_512: | |
2548 | if (mtd->oobblock == 256) { | |
2549 | printk (KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n"); | |
2550 | this->eccmode = NAND_ECC_SOFT; | |
2551 | this->calculate_ecc = nand_calculate_ecc; | |
2552 | this->correct_data = nand_correct_data; | |
2553 | } else | |
2554 | this->eccsize = 512; /* set eccsize to 512 */ | |
2555 | break; | |
2556 | ||
2557 | case NAND_ECC_HW3_256: | |
2558 | break; | |
2559 | ||
2560 | case NAND_ECC_NONE: | |
2561 | printk (KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n"); | |
2562 | this->eccmode = NAND_ECC_NONE; | |
2563 | break; | |
2564 | ||
2565 | case NAND_ECC_SOFT: | |
2566 | this->calculate_ecc = nand_calculate_ecc; | |
2567 | this->correct_data = nand_correct_data; | |
2568 | break; | |
2569 | ||
2570 | default: | |
2571 | printk (KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode); | |
2572 | BUG(); | |
2573 | } | |
2574 | ||
2575 | /* Check hardware ecc function availability and adjust number of ecc bytes per | |
2576 | * calculation step | |
2577 | */ | |
2578 | switch (this->eccmode) { | |
2579 | case NAND_ECC_HW12_2048: | |
2580 | this->eccbytes += 4; | |
2581 | case NAND_ECC_HW8_512: | |
2582 | this->eccbytes += 2; | |
2583 | case NAND_ECC_HW6_512: | |
2584 | this->eccbytes += 3; | |
2585 | case NAND_ECC_HW3_512: | |
2586 | case NAND_ECC_HW3_256: | |
2587 | if (this->calculate_ecc && this->correct_data && this->enable_hwecc) | |
2588 | break; | |
2589 | printk (KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n"); | |
2590 | BUG(); | |
2591 | } | |
2592 | ||
2593 | mtd->eccsize = this->eccsize; | |
2594 | ||
2595 | /* Set the number of read / write steps for one page to ensure ECC generation */ | |
2596 | switch (this->eccmode) { | |
2597 | case NAND_ECC_HW12_2048: | |
2598 | this->eccsteps = mtd->oobblock / 2048; | |
2599 | break; | |
2600 | case NAND_ECC_HW3_512: | |
2601 | case NAND_ECC_HW6_512: | |
2602 | case NAND_ECC_HW8_512: | |
2603 | this->eccsteps = mtd->oobblock / 512; | |
2604 | break; | |
2605 | case NAND_ECC_HW3_256: | |
2606 | case NAND_ECC_SOFT: | |
2607 | this->eccsteps = mtd->oobblock / 256; | |
2608 | break; | |
2609 | ||
2610 | case NAND_ECC_NONE: | |
2611 | this->eccsteps = 1; | |
2612 | break; | |
2613 | } | |
2614 | ||
2615 | /* Initialize state, waitqueue and spinlock */ | |
2616 | this->state = FL_READY; | |
2617 | init_waitqueue_head (&this->wq); | |
2618 | spin_lock_init (&this->chip_lock); | |
2619 | ||
2620 | /* De-select the device */ | |
2621 | this->select_chip(mtd, -1); | |
2622 | ||
2623 | /* Invalidate the pagebuffer reference */ | |
2624 | this->pagebuf = -1; | |
2625 | ||
2626 | /* Fill in remaining MTD driver data */ | |
2627 | mtd->type = MTD_NANDFLASH; | |
2628 | mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC; | |
2629 | mtd->ecctype = MTD_ECC_SW; | |
2630 | mtd->erase = nand_erase; | |
2631 | mtd->point = NULL; | |
2632 | mtd->unpoint = NULL; | |
2633 | mtd->read = nand_read; | |
2634 | mtd->write = nand_write; | |
2635 | mtd->read_ecc = nand_read_ecc; | |
2636 | mtd->write_ecc = nand_write_ecc; | |
2637 | mtd->read_oob = nand_read_oob; | |
2638 | mtd->write_oob = nand_write_oob; | |
2639 | mtd->readv = NULL; | |
2640 | mtd->writev = nand_writev; | |
2641 | mtd->writev_ecc = nand_writev_ecc; | |
2642 | mtd->sync = nand_sync; | |
2643 | mtd->lock = NULL; | |
2644 | mtd->unlock = NULL; | |
2645 | mtd->suspend = NULL; | |
2646 | mtd->resume = NULL; | |
2647 | mtd->block_isbad = nand_block_isbad; | |
2648 | mtd->block_markbad = nand_block_markbad; | |
2649 | ||
2650 | /* and make the autooob the default one */ | |
2651 | memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo)); | |
2652 | ||
2653 | mtd->owner = THIS_MODULE; | |
0040bf38 TG |
2654 | |
2655 | /* Check, if we should skip the bad block table scan */ | |
2656 | if (this->options & NAND_SKIP_BBTSCAN) | |
2657 | return 0; | |
1da177e4 LT |
2658 | |
2659 | /* Build bad block table */ | |
2660 | return this->scan_bbt (mtd); | |
2661 | } | |
2662 | ||
2663 | /** | |
2664 | * nand_release - [NAND Interface] Free resources held by the NAND device | |
2665 | * @mtd: MTD device structure | |
2666 | */ | |
2667 | void nand_release (struct mtd_info *mtd) | |
2668 | { | |
2669 | struct nand_chip *this = mtd->priv; | |
2670 | ||
2671 | #ifdef CONFIG_MTD_PARTITIONS | |
2672 | /* Deregister partitions */ | |
2673 | del_mtd_partitions (mtd); | |
2674 | #endif | |
2675 | /* Deregister the device */ | |
2676 | del_mtd_device (mtd); | |
2677 | ||
2678 | /* Free bad block table memory, if allocated */ | |
2679 | if (this->bbt) | |
2680 | kfree (this->bbt); | |
2681 | /* Buffer allocated by nand_scan ? */ | |
2682 | if (this->options & NAND_OOBBUF_ALLOC) | |
2683 | kfree (this->oob_buf); | |
2684 | /* Buffer allocated by nand_scan ? */ | |
2685 | if (this->options & NAND_DATABUF_ALLOC) | |
2686 | kfree (this->data_buf); | |
2687 | } | |
2688 | ||
d7e78d4f TG |
2689 | EXPORT_SYMBOL_GPL (nand_scan); |
2690 | EXPORT_SYMBOL_GPL (nand_release); | |
1da177e4 LT |
2691 | |
2692 | MODULE_LICENSE ("GPL"); | |
2693 | MODULE_AUTHOR ("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>"); | |
2694 | MODULE_DESCRIPTION ("Generic NAND flash driver code"); |