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