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