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