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