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