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