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