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