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