]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - drivers/mtd/nand/nand_base.c
nand: Remove CONFIG_MTD_NAND_VERIFY_WRITE
[karo-tx-uboot.git] / drivers / mtd / nand / nand_base.c
1 /*
2  *  drivers/mtd/nand.c
3  *
4  *  Overview:
5  *   This is the generic MTD driver for NAND flash devices. It should be
6  *   capable of working with almost all NAND chips currently available.
7  *
8  *      Additional technical information is available on
9  *      http://www.linux-mtd.infradead.org/doc/nand.html
10  *
11  *  Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
12  *                2002-2006 Thomas Gleixner (tglx@linutronix.de)
13  *
14  *  Credits:
15  *      David Woodhouse for adding multichip support
16  *
17  *      Aleph One Ltd. and Toby Churchill Ltd. for supporting the
18  *      rework for 2K page size chips
19  *
20  *  TODO:
21  *      Enable cached programming for 2k page size chips
22  *      Check, if mtd->ecctype should be set to MTD_ECC_HW
23  *      if we have HW ECC support.
24  *      BBT table is not serialized, has to be fixed
25  *
26  * This program is free software; you can redistribute it and/or modify
27  * it under the terms of the GNU General Public License version 2 as
28  * published by the Free Software Foundation.
29  *
30  */
31
32 #ifndef __UBOOT__
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
35 #include <linux/module.h>
36 #include <linux/delay.h>
37 #include <linux/errno.h>
38 #include <linux/err.h>
39 #include <linux/sched.h>
40 #include <linux/slab.h>
41 #include <linux/types.h>
42 #include <linux/mtd/mtd.h>
43 #include <linux/mtd/nand.h>
44 #include <linux/mtd/nand_ecc.h>
45 #include <linux/mtd/nand_bch.h>
46 #include <linux/interrupt.h>
47 #include <linux/bitops.h>
48 #include <linux/leds.h>
49 #include <linux/io.h>
50 #include <linux/mtd/partitions.h>
51 #else
52 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
53 #include <common.h>
54 #include <malloc.h>
55 #include <watchdog.h>
56 #include <linux/err.h>
57 #include <linux/compat.h>
58 #include <linux/mtd/mtd.h>
59 #include <linux/mtd/nand.h>
60 #include <linux/mtd/nand_ecc.h>
61 #include <linux/mtd/nand_bch.h>
62 #ifdef CONFIG_MTD_PARTITIONS
63 #include <linux/mtd/partitions.h>
64 #endif
65 #include <asm/io.h>
66 #include <asm/errno.h>
67
68 /*
69  * CONFIG_SYS_NAND_RESET_CNT is used as a timeout mechanism when resetting
70  * a flash.  NAND flash is initialized prior to interrupts so standard timers
71  * can't be used.  CONFIG_SYS_NAND_RESET_CNT should be set to a value
72  * which is greater than (max NAND reset time / NAND status read time).
73  * A conservative default of 200000 (500 us / 25 ns) is used as a default.
74  */
75 #ifndef CONFIG_SYS_NAND_RESET_CNT
76 #define CONFIG_SYS_NAND_RESET_CNT 200000
77 #endif
78
79 static bool is_module_text_address(unsigned long addr) {return 0;}
80 #endif
81
82 /* Define default oob placement schemes for large and small page devices */
83 static struct nand_ecclayout nand_oob_8 = {
84         .eccbytes = 3,
85         .eccpos = {0, 1, 2},
86         .oobfree = {
87                 {.offset = 3,
88                  .length = 2},
89                 {.offset = 6,
90                  .length = 2} }
91 };
92
93 static struct nand_ecclayout nand_oob_16 = {
94         .eccbytes = 6,
95         .eccpos = {0, 1, 2, 3, 6, 7},
96         .oobfree = {
97                 {.offset = 8,
98                  . length = 8} }
99 };
100
101 static struct nand_ecclayout nand_oob_64 = {
102         .eccbytes = 24,
103         .eccpos = {
104                    40, 41, 42, 43, 44, 45, 46, 47,
105                    48, 49, 50, 51, 52, 53, 54, 55,
106                    56, 57, 58, 59, 60, 61, 62, 63},
107         .oobfree = {
108                 {.offset = 2,
109                  .length = 38} }
110 };
111
112 static struct nand_ecclayout nand_oob_128 = {
113         .eccbytes = 48,
114         .eccpos = {
115                    80, 81, 82, 83, 84, 85, 86, 87,
116                    88, 89, 90, 91, 92, 93, 94, 95,
117                    96, 97, 98, 99, 100, 101, 102, 103,
118                    104, 105, 106, 107, 108, 109, 110, 111,
119                    112, 113, 114, 115, 116, 117, 118, 119,
120                    120, 121, 122, 123, 124, 125, 126, 127},
121         .oobfree = {
122                 {.offset = 2,
123                  .length = 78} }
124 };
125
126 static int nand_get_device(struct mtd_info *mtd, int new_state);
127
128 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
129                              struct mtd_oob_ops *ops);
130
131 /*
132  * For devices which display every fart in the system on a separate LED. Is
133  * compiled away when LED support is disabled.
134  */
135 DEFINE_LED_TRIGGER(nand_led_trigger);
136
137 static int check_offs_len(struct mtd_info *mtd,
138                                         loff_t ofs, uint64_t len)
139 {
140         struct nand_chip *chip = mtd->priv;
141         int ret = 0;
142
143         /* Start address must align on block boundary */
144         if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
145                 pr_debug("%s: unaligned address\n", __func__);
146                 ret = -EINVAL;
147         }
148
149         /* Length must align on block boundary */
150         if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
151                 pr_debug("%s: length not block aligned\n", __func__);
152                 ret = -EINVAL;
153         }
154
155         return ret;
156 }
157
158 /**
159  * nand_release_device - [GENERIC] release chip
160  * @mtd: MTD device structure
161  *
162  * Release chip lock and wake up anyone waiting on the device.
163  */
164 static void nand_release_device(struct mtd_info *mtd)
165 {
166         struct nand_chip *chip = mtd->priv;
167
168 #ifndef __UBOOT__
169         /* Release the controller and the chip */
170         spin_lock(&chip->controller->lock);
171         chip->controller->active = NULL;
172         chip->state = FL_READY;
173         wake_up(&chip->controller->wq);
174         spin_unlock(&chip->controller->lock);
175 #else
176         /* De-select the NAND device */
177         chip->select_chip(mtd, -1);
178 #endif
179 }
180
181 /**
182  * nand_read_byte - [DEFAULT] read one byte from the chip
183  * @mtd: MTD device structure
184  *
185  * Default read function for 8bit buswidth
186  */
187 #ifndef __UBOOT__
188 static uint8_t nand_read_byte(struct mtd_info *mtd)
189 #else
190 uint8_t nand_read_byte(struct mtd_info *mtd)
191 #endif
192 {
193         struct nand_chip *chip = mtd->priv;
194         return readb(chip->IO_ADDR_R);
195 }
196
197 /**
198  * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
199  * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
200  * @mtd: MTD device structure
201  *
202  * Default read function for 16bit buswidth with endianness conversion.
203  *
204  */
205 static uint8_t nand_read_byte16(struct mtd_info *mtd)
206 {
207         struct nand_chip *chip = mtd->priv;
208         return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
209 }
210
211 /**
212  * nand_read_word - [DEFAULT] read one word from the chip
213  * @mtd: MTD device structure
214  *
215  * Default read function for 16bit buswidth without endianness conversion.
216  */
217 static u16 nand_read_word(struct mtd_info *mtd)
218 {
219         struct nand_chip *chip = mtd->priv;
220         return readw(chip->IO_ADDR_R);
221 }
222
223 /**
224  * nand_select_chip - [DEFAULT] control CE line
225  * @mtd: MTD device structure
226  * @chipnr: chipnumber to select, -1 for deselect
227  *
228  * Default select function for 1 chip devices.
229  */
230 static void nand_select_chip(struct mtd_info *mtd, int chipnr)
231 {
232         struct nand_chip *chip = mtd->priv;
233
234         switch (chipnr) {
235         case -1:
236                 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
237                 break;
238         case 0:
239                 break;
240
241         default:
242                 BUG();
243         }
244 }
245
246 /**
247  * nand_write_byte - [DEFAULT] write single byte to chip
248  * @mtd: MTD device structure
249  * @byte: value to write
250  *
251  * Default function to write a byte to I/O[7:0]
252  */
253 static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
254 {
255         struct nand_chip *chip = mtd->priv;
256
257         chip->write_buf(mtd, &byte, 1);
258 }
259
260 /**
261  * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
262  * @mtd: MTD device structure
263  * @byte: value to write
264  *
265  * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
266  */
267 static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
268 {
269         struct nand_chip *chip = mtd->priv;
270         uint16_t word = byte;
271
272         /*
273          * It's not entirely clear what should happen to I/O[15:8] when writing
274          * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
275          *
276          *    When the host supports a 16-bit bus width, only data is
277          *    transferred at the 16-bit width. All address and command line
278          *    transfers shall use only the lower 8-bits of the data bus. During
279          *    command transfers, the host may place any value on the upper
280          *    8-bits of the data bus. During address transfers, the host shall
281          *    set the upper 8-bits of the data bus to 00h.
282          *
283          * One user of the write_byte callback is nand_onfi_set_features. The
284          * four parameters are specified to be written to I/O[7:0], but this is
285          * neither an address nor a command transfer. Let's assume a 0 on the
286          * upper I/O lines is OK.
287          */
288         chip->write_buf(mtd, (uint8_t *)&word, 2);
289 }
290
291 #if defined(__UBOOT__) && !defined(CONFIG_BLACKFIN)
292 static void iowrite8_rep(void *addr, const uint8_t *buf, int len)
293 {
294         int i;
295
296         for (i = 0; i < len; i++)
297                 writeb(buf[i], addr);
298 }
299 static void ioread8_rep(void *addr, uint8_t *buf, int len)
300 {
301         int i;
302
303         for (i = 0; i < len; i++)
304                 buf[i] = readb(addr);
305 }
306
307 static void ioread16_rep(void *addr, void *buf, int len)
308 {
309         int i;
310         u16 *p = (u16 *) buf;
311
312         for (i = 0; i < len; i++)
313                 p[i] = readw(addr);
314 }
315
316 static void iowrite16_rep(void *addr, void *buf, int len)
317 {
318         int i;
319         u16 *p = (u16 *) buf;
320
321         for (i = 0; i < len; i++)
322                 writew(p[i], addr);
323 }
324 #endif
325
326 /**
327  * nand_write_buf - [DEFAULT] write buffer to chip
328  * @mtd: MTD device structure
329  * @buf: data buffer
330  * @len: number of bytes to write
331  *
332  * Default write function for 8bit buswidth.
333  */
334 #ifndef __UBOOT__
335 static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
336 #else
337 void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
338 #endif
339 {
340         struct nand_chip *chip = mtd->priv;
341
342         iowrite8_rep(chip->IO_ADDR_W, buf, len);
343 }
344
345 /**
346  * nand_read_buf - [DEFAULT] read chip data into buffer
347  * @mtd: MTD device structure
348  * @buf: buffer to store date
349  * @len: number of bytes to read
350  *
351  * Default read function for 8bit buswidth.
352  */
353 #ifndef __UBOOT__
354 static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
355 #else
356 void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
357 #endif
358 {
359         struct nand_chip *chip = mtd->priv;
360
361         ioread8_rep(chip->IO_ADDR_R, buf, len);
362 }
363
364 /**
365  * nand_write_buf16 - [DEFAULT] write buffer to chip
366  * @mtd: MTD device structure
367  * @buf: data buffer
368  * @len: number of bytes to write
369  *
370  * Default write function for 16bit buswidth.
371  */
372 #ifndef __UBOOT__
373 static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
374 #else
375 void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
376 #endif
377 {
378         struct nand_chip *chip = mtd->priv;
379         u16 *p = (u16 *) buf;
380
381         iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
382 }
383
384 /**
385  * nand_read_buf16 - [DEFAULT] read chip data into buffer
386  * @mtd: MTD device structure
387  * @buf: buffer to store date
388  * @len: number of bytes to read
389  *
390  * Default read function for 16bit buswidth.
391  */
392 #ifndef __UBOOT__
393 static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
394 #else
395 void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
396 #endif
397 {
398         struct nand_chip *chip = mtd->priv;
399         u16 *p = (u16 *) buf;
400
401         ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
402 }
403
404 /**
405  * nand_block_bad - [DEFAULT] Read bad block marker from the chip
406  * @mtd: MTD device structure
407  * @ofs: offset from device start
408  * @getchip: 0, if the chip is already selected
409  *
410  * Check, if the block is bad.
411  */
412 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
413 {
414         int page, chipnr, res = 0, i = 0;
415         struct nand_chip *chip = mtd->priv;
416         u16 bad;
417
418         if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
419                 ofs += mtd->erasesize - mtd->writesize;
420
421         page = (int)(ofs >> chip->page_shift) & chip->pagemask;
422
423         if (getchip) {
424                 chipnr = (int)(ofs >> chip->chip_shift);
425
426                 nand_get_device(mtd, FL_READING);
427
428                 /* Select the NAND device */
429                 chip->select_chip(mtd, chipnr);
430         }
431
432         do {
433                 if (chip->options & NAND_BUSWIDTH_16) {
434                         chip->cmdfunc(mtd, NAND_CMD_READOOB,
435                                         chip->badblockpos & 0xFE, page);
436                         bad = cpu_to_le16(chip->read_word(mtd));
437                         if (chip->badblockpos & 0x1)
438                                 bad >>= 8;
439                         else
440                                 bad &= 0xFF;
441                 } else {
442                         chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
443                                         page);
444                         bad = chip->read_byte(mtd);
445                 }
446
447                 if (likely(chip->badblockbits == 8))
448                         res = bad != 0xFF;
449                 else
450                         res = hweight8(bad) < chip->badblockbits;
451                 ofs += mtd->writesize;
452                 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
453                 i++;
454         } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
455
456         if (getchip) {
457                 chip->select_chip(mtd, -1);
458                 nand_release_device(mtd);
459         }
460
461         return res;
462 }
463
464 /**
465  * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker
466  * @mtd: MTD device structure
467  * @ofs: offset from device start
468  *
469  * This is the default implementation, which can be overridden by a hardware
470  * specific driver. It provides the details for writing a bad block marker to a
471  * block.
472  */
473 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
474 {
475         struct nand_chip *chip = mtd->priv;
476         struct mtd_oob_ops ops;
477         uint8_t buf[2] = { 0, 0 };
478         int ret = 0, res, i = 0;
479
480         ops.datbuf = NULL;
481         ops.oobbuf = buf;
482         ops.ooboffs = chip->badblockpos;
483         if (chip->options & NAND_BUSWIDTH_16) {
484                 ops.ooboffs &= ~0x01;
485                 ops.len = ops.ooblen = 2;
486         } else {
487                 ops.len = ops.ooblen = 1;
488         }
489         ops.mode = MTD_OPS_PLACE_OOB;
490
491         /* Write to first/last page(s) if necessary */
492         if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
493                 ofs += mtd->erasesize - mtd->writesize;
494         do {
495                 res = nand_do_write_oob(mtd, ofs, &ops);
496                 if (!ret)
497                         ret = res;
498
499                 i++;
500                 ofs += mtd->writesize;
501         } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
502
503         return ret;
504 }
505
506 /**
507  * nand_block_markbad_lowlevel - mark a block bad
508  * @mtd: MTD device structure
509  * @ofs: offset from device start
510  *
511  * This function performs the generic NAND bad block marking steps (i.e., bad
512  * block table(s) and/or marker(s)). We only allow the hardware driver to
513  * specify how to write bad block markers to OOB (chip->block_markbad).
514  *
515  * We try operations in the following order:
516  *  (1) erase the affected block, to allow OOB marker to be written cleanly
517  *  (2) write bad block marker to OOB area of affected block (unless flag
518  *      NAND_BBT_NO_OOB_BBM is present)
519  *  (3) update the BBT
520  * Note that we retain the first error encountered in (2) or (3), finish the
521  * procedures, and dump the error in the end.
522 */
523 static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
524 {
525         struct nand_chip *chip = mtd->priv;
526         int res, ret = 0;
527
528         if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
529                 struct erase_info einfo;
530
531                 /* Attempt erase before marking OOB */
532                 memset(&einfo, 0, sizeof(einfo));
533                 einfo.mtd = mtd;
534                 einfo.addr = ofs;
535                 einfo.len = 1ULL << chip->phys_erase_shift;
536                 nand_erase_nand(mtd, &einfo, 0);
537
538                 /* Write bad block marker to OOB */
539                 nand_get_device(mtd, FL_WRITING);
540                 ret = chip->block_markbad(mtd, ofs);
541                 nand_release_device(mtd);
542         }
543
544         /* Mark block bad in BBT */
545         if (chip->bbt) {
546                 res = nand_markbad_bbt(mtd, ofs);
547                 if (!ret)
548                         ret = res;
549         }
550
551         if (!ret)
552                 mtd->ecc_stats.badblocks++;
553
554         return ret;
555 }
556
557 /**
558  * nand_check_wp - [GENERIC] check if the chip is write protected
559  * @mtd: MTD device structure
560  *
561  * Check, if the device is write protected. The function expects, that the
562  * device is already selected.
563  */
564 static int nand_check_wp(struct mtd_info *mtd)
565 {
566         struct nand_chip *chip = mtd->priv;
567
568         /* Broken xD cards report WP despite being writable */
569         if (chip->options & NAND_BROKEN_XD)
570                 return 0;
571
572         /* Check the WP bit */
573         chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
574         return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
575 }
576
577 /**
578  * nand_block_checkbad - [GENERIC] Check if a block is marked bad
579  * @mtd: MTD device structure
580  * @ofs: offset from device start
581  * @getchip: 0, if the chip is already selected
582  * @allowbbt: 1, if its allowed to access the bbt area
583  *
584  * Check, if the block is bad. Either by reading the bad block table or
585  * calling of the scan function.
586  */
587 static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
588                                int allowbbt)
589 {
590         struct nand_chip *chip = mtd->priv;
591
592         if (!(chip->options & NAND_SKIP_BBTSCAN) &&
593             !(chip->options & NAND_BBT_SCANNED)) {
594                 chip->options |= NAND_BBT_SCANNED;
595                 chip->scan_bbt(mtd);
596         }
597
598         if (!chip->bbt)
599                 return chip->block_bad(mtd, ofs, getchip);
600
601         /* Return info from the table */
602         return nand_isbad_bbt(mtd, ofs, allowbbt);
603 }
604
605 #ifndef __UBOOT__
606 /**
607  * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
608  * @mtd: MTD device structure
609  * @timeo: Timeout
610  *
611  * Helper function for nand_wait_ready used when needing to wait in interrupt
612  * context.
613  */
614 static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
615 {
616         struct nand_chip *chip = mtd->priv;
617         int i;
618
619         /* Wait for the device to get ready */
620         for (i = 0; i < timeo; i++) {
621                 if (chip->dev_ready(mtd))
622                         break;
623                 touch_softlockup_watchdog();
624                 mdelay(1);
625         }
626 }
627 #endif
628
629 /* Wait for the ready pin, after a command. The timeout is caught later. */
630 void nand_wait_ready(struct mtd_info *mtd)
631 {
632         struct nand_chip *chip = mtd->priv;
633 #ifndef __UBOOT__
634         unsigned long timeo = jiffies + msecs_to_jiffies(20);
635
636         /* 400ms timeout */
637         if (in_interrupt() || oops_in_progress)
638                 return panic_nand_wait_ready(mtd, 400);
639
640         led_trigger_event(nand_led_trigger, LED_FULL);
641         /* Wait until command is processed or timeout occurs */
642         do {
643                 if (chip->dev_ready(mtd))
644                         break;
645                 touch_softlockup_watchdog();
646         } while (time_before(jiffies, timeo));
647         led_trigger_event(nand_led_trigger, LED_OFF);
648 #else
649         u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
650         u32 time_start;
651
652         time_start = get_timer(0);
653         /* Wait until command is processed or timeout occurs */
654         while (get_timer(time_start) < timeo) {
655                 if (chip->dev_ready)
656                         if (chip->dev_ready(mtd))
657                                 break;
658         }
659 #endif
660 }
661 EXPORT_SYMBOL_GPL(nand_wait_ready);
662
663 /**
664  * nand_command - [DEFAULT] Send command to NAND device
665  * @mtd: MTD device structure
666  * @command: the command to be sent
667  * @column: the column address for this command, -1 if none
668  * @page_addr: the page address for this command, -1 if none
669  *
670  * Send command to NAND device. This function is used for small page devices
671  * (512 Bytes per page).
672  */
673 static void nand_command(struct mtd_info *mtd, unsigned int command,
674                          int column, int page_addr)
675 {
676         register struct nand_chip *chip = mtd->priv;
677         int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
678         uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
679
680         /* Write out the command to the device */
681         if (command == NAND_CMD_SEQIN) {
682                 int readcmd;
683
684                 if (column >= mtd->writesize) {
685                         /* OOB area */
686                         column -= mtd->writesize;
687                         readcmd = NAND_CMD_READOOB;
688                 } else if (column < 256) {
689                         /* First 256 bytes --> READ0 */
690                         readcmd = NAND_CMD_READ0;
691                 } else {
692                         column -= 256;
693                         readcmd = NAND_CMD_READ1;
694                 }
695                 chip->cmd_ctrl(mtd, readcmd, ctrl);
696                 ctrl &= ~NAND_CTRL_CHANGE;
697         }
698         chip->cmd_ctrl(mtd, command, ctrl);
699
700         /* Address cycle, when necessary */
701         ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
702         /* Serially input address */
703         if (column != -1) {
704                 /* Adjust columns for 16 bit buswidth */
705                 if (chip->options & NAND_BUSWIDTH_16 &&
706                                 !nand_opcode_8bits(command))
707                         column >>= 1;
708                 chip->cmd_ctrl(mtd, column, ctrl);
709                 ctrl &= ~NAND_CTRL_CHANGE;
710         }
711         if (page_addr != -1) {
712                 chip->cmd_ctrl(mtd, page_addr, ctrl);
713                 ctrl &= ~NAND_CTRL_CHANGE;
714                 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
715                 /* One more address cycle for devices > 32MiB */
716                 if (chip->chipsize > (32 << 20))
717                         chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
718         }
719         chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
720
721         /*
722          * Program and erase have their own busy handlers status and sequential
723          * in needs no delay
724          */
725         switch (command) {
726
727         case NAND_CMD_PAGEPROG:
728         case NAND_CMD_ERASE1:
729         case NAND_CMD_ERASE2:
730         case NAND_CMD_SEQIN:
731         case NAND_CMD_STATUS:
732                 return;
733
734         case NAND_CMD_RESET:
735                 if (chip->dev_ready)
736                         break;
737                 udelay(chip->chip_delay);
738                 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
739                                NAND_CTRL_CLE | NAND_CTRL_CHANGE);
740                 chip->cmd_ctrl(mtd,
741                                NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
742                 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
743                         (rst_sts_cnt--));
744                 return;
745
746                 /* This applies to read commands */
747         default:
748                 /*
749                  * If we don't have access to the busy pin, we apply the given
750                  * command delay
751                  */
752                 if (!chip->dev_ready) {
753                         udelay(chip->chip_delay);
754                         return;
755                 }
756         }
757         /*
758          * Apply this short delay always to ensure that we do wait tWB in
759          * any case on any machine.
760          */
761         ndelay(100);
762
763         nand_wait_ready(mtd);
764 }
765
766 /**
767  * nand_command_lp - [DEFAULT] Send command to NAND large page device
768  * @mtd: MTD device structure
769  * @command: the command to be sent
770  * @column: the column address for this command, -1 if none
771  * @page_addr: the page address for this command, -1 if none
772  *
773  * Send command to NAND device. This is the version for the new large page
774  * devices. We don't have the separate regions as we have in the small page
775  * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
776  */
777 static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
778                             int column, int page_addr)
779 {
780         register struct nand_chip *chip = mtd->priv;
781         uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
782
783         /* Emulate NAND_CMD_READOOB */
784         if (command == NAND_CMD_READOOB) {
785                 column += mtd->writesize;
786                 command = NAND_CMD_READ0;
787         }
788
789         /* Command latch cycle */
790         chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
791
792         if (column != -1 || page_addr != -1) {
793                 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
794
795                 /* Serially input address */
796                 if (column != -1) {
797                         /* Adjust columns for 16 bit buswidth */
798                         if (chip->options & NAND_BUSWIDTH_16 &&
799                                         !nand_opcode_8bits(command))
800                                 column >>= 1;
801                         chip->cmd_ctrl(mtd, column, ctrl);
802                         ctrl &= ~NAND_CTRL_CHANGE;
803                         chip->cmd_ctrl(mtd, column >> 8, ctrl);
804                 }
805                 if (page_addr != -1) {
806                         chip->cmd_ctrl(mtd, page_addr, ctrl);
807                         chip->cmd_ctrl(mtd, page_addr >> 8,
808                                        NAND_NCE | NAND_ALE);
809                         /* One more address cycle for devices > 128MiB */
810                         if (chip->chipsize > (128 << 20))
811                                 chip->cmd_ctrl(mtd, page_addr >> 16,
812                                                NAND_NCE | NAND_ALE);
813                 }
814         }
815         chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
816
817         /*
818          * Program and erase have their own busy handlers status, sequential
819          * in, and deplete1 need no delay.
820          */
821         switch (command) {
822
823         case NAND_CMD_CACHEDPROG:
824         case NAND_CMD_PAGEPROG:
825         case NAND_CMD_ERASE1:
826         case NAND_CMD_ERASE2:
827         case NAND_CMD_SEQIN:
828         case NAND_CMD_RNDIN:
829         case NAND_CMD_STATUS:
830                 return;
831
832         case NAND_CMD_RESET:
833                 if (chip->dev_ready)
834                         break;
835                 udelay(chip->chip_delay);
836                 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
837                                NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
838                 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
839                                NAND_NCE | NAND_CTRL_CHANGE);
840                 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
841                         (rst_sts_cnt--));
842                 return;
843
844         case NAND_CMD_RNDOUT:
845                 /* No ready / busy check necessary */
846                 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
847                                NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
848                 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
849                                NAND_NCE | NAND_CTRL_CHANGE);
850                 return;
851
852         case NAND_CMD_READ0:
853                 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
854                                NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
855                 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
856                                NAND_NCE | NAND_CTRL_CHANGE);
857
858                 /* This applies to read commands */
859         default:
860                 /*
861                  * If we don't have access to the busy pin, we apply the given
862                  * command delay.
863                  */
864                 if (!chip->dev_ready) {
865                         udelay(chip->chip_delay);
866                         return;
867                 }
868         }
869
870         /*
871          * Apply this short delay always to ensure that we do wait tWB in
872          * any case on any machine.
873          */
874         ndelay(100);
875
876         nand_wait_ready(mtd);
877 }
878
879 /**
880  * panic_nand_get_device - [GENERIC] Get chip for selected access
881  * @chip: the nand chip descriptor
882  * @mtd: MTD device structure
883  * @new_state: the state which is requested
884  *
885  * Used when in panic, no locks are taken.
886  */
887 static void panic_nand_get_device(struct nand_chip *chip,
888                       struct mtd_info *mtd, int new_state)
889 {
890         /* Hardware controller shared among independent devices */
891         chip->controller->active = chip;
892         chip->state = new_state;
893 }
894
895 /**
896  * nand_get_device - [GENERIC] Get chip for selected access
897  * @mtd: MTD device structure
898  * @new_state: the state which is requested
899  *
900  * Get the device and lock it for exclusive access
901  */
902 static int
903 nand_get_device(struct mtd_info *mtd, int new_state)
904 {
905         struct nand_chip *chip = mtd->priv;
906 #ifndef __UBOOT__
907         spinlock_t *lock = &chip->controller->lock;
908         wait_queue_head_t *wq = &chip->controller->wq;
909         DECLARE_WAITQUEUE(wait, current);
910 retry:
911         spin_lock(lock);
912
913         /* Hardware controller shared among independent devices */
914         if (!chip->controller->active)
915                 chip->controller->active = chip;
916
917         if (chip->controller->active == chip && chip->state == FL_READY) {
918                 chip->state = new_state;
919                 spin_unlock(lock);
920                 return 0;
921         }
922         if (new_state == FL_PM_SUSPENDED) {
923                 if (chip->controller->active->state == FL_PM_SUSPENDED) {
924                         chip->state = FL_PM_SUSPENDED;
925                         spin_unlock(lock);
926                         return 0;
927                 }
928         }
929         set_current_state(TASK_UNINTERRUPTIBLE);
930         add_wait_queue(wq, &wait);
931         spin_unlock(lock);
932         schedule();
933         remove_wait_queue(wq, &wait);
934         goto retry;
935 #else
936         chip->state = new_state;
937         return 0;
938 #endif
939 }
940
941 /**
942  * panic_nand_wait - [GENERIC] wait until the command is done
943  * @mtd: MTD device structure
944  * @chip: NAND chip structure
945  * @timeo: timeout
946  *
947  * Wait for command done. This is a helper function for nand_wait used when
948  * we are in interrupt context. May happen when in panic and trying to write
949  * an oops through mtdoops.
950  */
951 static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
952                             unsigned long timeo)
953 {
954         int i;
955         for (i = 0; i < timeo; i++) {
956                 if (chip->dev_ready) {
957                         if (chip->dev_ready(mtd))
958                                 break;
959                 } else {
960                         if (chip->read_byte(mtd) & NAND_STATUS_READY)
961                                 break;
962                 }
963                 mdelay(1);
964         }
965 }
966
967 /**
968  * nand_wait - [DEFAULT] wait until the command is done
969  * @mtd: MTD device structure
970  * @chip: NAND chip structure
971  *
972  * Wait for command done. This applies to erase and program only. Erase can
973  * take up to 400ms and program up to 20ms according to general NAND and
974  * SmartMedia specs.
975  */
976 static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
977 {
978
979         int status, state = chip->state;
980         unsigned long timeo = (state == FL_ERASING ? 400 : 20);
981
982         led_trigger_event(nand_led_trigger, LED_FULL);
983
984         /*
985          * Apply this short delay always to ensure that we do wait tWB in any
986          * case on any machine.
987          */
988         ndelay(100);
989
990         chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
991
992 #ifndef __UBOOT__
993         if (in_interrupt() || oops_in_progress)
994                 panic_nand_wait(mtd, chip, timeo);
995         else {
996                 timeo = jiffies + msecs_to_jiffies(timeo);
997                 while (time_before(jiffies, timeo)) {
998                         if (chip->dev_ready) {
999                                 if (chip->dev_ready(mtd))
1000                                         break;
1001                         } else {
1002                                 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1003                                         break;
1004                         }
1005                         cond_resched();
1006                 }
1007         }
1008 #else
1009         u32 timer = (CONFIG_SYS_HZ * timeo) / 1000;
1010         u32 time_start;
1011  
1012         time_start = get_timer(0);
1013         while (get_timer(time_start) < timer) {
1014                 if (chip->dev_ready) {
1015                         if (chip->dev_ready(mtd))
1016                                 break;
1017                 } else {
1018                         if (chip->read_byte(mtd) & NAND_STATUS_READY)
1019                                 break;
1020                 }
1021         }
1022 #endif
1023         led_trigger_event(nand_led_trigger, LED_OFF);
1024
1025         status = (int)chip->read_byte(mtd);
1026         /* This can happen if in case of timeout or buggy dev_ready */
1027         WARN_ON(!(status & NAND_STATUS_READY));
1028         return status;
1029 }
1030
1031 #ifndef __UBOOT__
1032 /**
1033  * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks
1034  * @mtd: mtd info
1035  * @ofs: offset to start unlock from
1036  * @len: length to unlock
1037  * @invert: when = 0, unlock the range of blocks within the lower and
1038  *                    upper boundary address
1039  *          when = 1, unlock the range of blocks outside the boundaries
1040  *                    of the lower and upper boundary address
1041  *
1042  * Returs unlock status.
1043  */
1044 static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1045                                         uint64_t len, int invert)
1046 {
1047         int ret = 0;
1048         int status, page;
1049         struct nand_chip *chip = mtd->priv;
1050
1051         /* Submit address of first page to unlock */
1052         page = ofs >> chip->page_shift;
1053         chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1054
1055         /* Submit address of last page to unlock */
1056         page = (ofs + len) >> chip->page_shift;
1057         chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1058                                 (page | invert) & chip->pagemask);
1059
1060         /* Call wait ready function */
1061         status = chip->waitfunc(mtd, chip);
1062         /* See if device thinks it succeeded */
1063         if (status & NAND_STATUS_FAIL) {
1064                 pr_debug("%s: error status = 0x%08x\n",
1065                                         __func__, status);
1066                 ret = -EIO;
1067         }
1068
1069         return ret;
1070 }
1071
1072 /**
1073  * nand_unlock - [REPLACEABLE] unlocks specified locked blocks
1074  * @mtd: mtd info
1075  * @ofs: offset to start unlock from
1076  * @len: length to unlock
1077  *
1078  * Returns unlock status.
1079  */
1080 int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1081 {
1082         int ret = 0;
1083         int chipnr;
1084         struct nand_chip *chip = mtd->priv;
1085
1086         pr_debug("%s: start = 0x%012llx, len = %llu\n",
1087                         __func__, (unsigned long long)ofs, len);
1088
1089         if (check_offs_len(mtd, ofs, len))
1090                 ret = -EINVAL;
1091
1092         /* Align to last block address if size addresses end of the device */
1093         if (ofs + len == mtd->size)
1094                 len -= mtd->erasesize;
1095
1096         nand_get_device(mtd, FL_UNLOCKING);
1097
1098         /* Shift to get chip number */
1099         chipnr = ofs >> chip->chip_shift;
1100
1101         chip->select_chip(mtd, chipnr);
1102
1103         /* Check, if it is write protected */
1104         if (nand_check_wp(mtd)) {
1105                 pr_debug("%s: device is write protected!\n",
1106                                         __func__);
1107                 ret = -EIO;
1108                 goto out;
1109         }
1110
1111         ret = __nand_unlock(mtd, ofs, len, 0);
1112
1113 out:
1114         chip->select_chip(mtd, -1);
1115         nand_release_device(mtd);
1116
1117         return ret;
1118 }
1119 EXPORT_SYMBOL(nand_unlock);
1120
1121 /**
1122  * nand_lock - [REPLACEABLE] locks all blocks present in the device
1123  * @mtd: mtd info
1124  * @ofs: offset to start unlock from
1125  * @len: length to unlock
1126  *
1127  * This feature is not supported in many NAND parts. 'Micron' NAND parts do
1128  * have this feature, but it allows only to lock all blocks, not for specified
1129  * range for block. Implementing 'lock' feature by making use of 'unlock', for
1130  * now.
1131  *
1132  * Returns lock status.
1133  */
1134 int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1135 {
1136         int ret = 0;
1137         int chipnr, status, page;
1138         struct nand_chip *chip = mtd->priv;
1139
1140         pr_debug("%s: start = 0x%012llx, len = %llu\n",
1141                         __func__, (unsigned long long)ofs, len);
1142
1143         if (check_offs_len(mtd, ofs, len))
1144                 ret = -EINVAL;
1145
1146         nand_get_device(mtd, FL_LOCKING);
1147
1148         /* Shift to get chip number */
1149         chipnr = ofs >> chip->chip_shift;
1150
1151         chip->select_chip(mtd, chipnr);
1152
1153         /* Check, if it is write protected */
1154         if (nand_check_wp(mtd)) {
1155                 pr_debug("%s: device is write protected!\n",
1156                                         __func__);
1157                 status = MTD_ERASE_FAILED;
1158                 ret = -EIO;
1159                 goto out;
1160         }
1161
1162         /* Submit address of first page to lock */
1163         page = ofs >> chip->page_shift;
1164         chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1165
1166         /* Call wait ready function */
1167         status = chip->waitfunc(mtd, chip);
1168         /* See if device thinks it succeeded */
1169         if (status & NAND_STATUS_FAIL) {
1170                 pr_debug("%s: error status = 0x%08x\n",
1171                                         __func__, status);
1172                 ret = -EIO;
1173                 goto out;
1174         }
1175
1176         ret = __nand_unlock(mtd, ofs, len, 0x1);
1177
1178 out:
1179         chip->select_chip(mtd, -1);
1180         nand_release_device(mtd);
1181
1182         return ret;
1183 }
1184 EXPORT_SYMBOL(nand_lock);
1185 #endif
1186
1187 /**
1188  * nand_read_page_raw - [INTERN] read raw page data without ecc
1189  * @mtd: mtd info structure
1190  * @chip: nand chip info structure
1191  * @buf: buffer to store read data
1192  * @oob_required: caller requires OOB data read to chip->oob_poi
1193  * @page: page number to read
1194  *
1195  * Not for syndrome calculating ECC controllers, which use a special oob layout.
1196  */
1197 static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1198                               uint8_t *buf, int oob_required, int page)
1199 {
1200         chip->read_buf(mtd, buf, mtd->writesize);
1201         if (oob_required)
1202                 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1203         return 0;
1204 }
1205
1206 /**
1207  * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc
1208  * @mtd: mtd info structure
1209  * @chip: nand chip info structure
1210  * @buf: buffer to store read data
1211  * @oob_required: caller requires OOB data read to chip->oob_poi
1212  * @page: page number to read
1213  *
1214  * We need a special oob layout and handling even when OOB isn't used.
1215  */
1216 static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
1217                                        struct nand_chip *chip, uint8_t *buf,
1218                                        int oob_required, int page)
1219 {
1220         int eccsize = chip->ecc.size;
1221         int eccbytes = chip->ecc.bytes;
1222         uint8_t *oob = chip->oob_poi;
1223         int steps, size;
1224
1225         for (steps = chip->ecc.steps; steps > 0; steps--) {
1226                 chip->read_buf(mtd, buf, eccsize);
1227                 buf += eccsize;
1228
1229                 if (chip->ecc.prepad) {
1230                         chip->read_buf(mtd, oob, chip->ecc.prepad);
1231                         oob += chip->ecc.prepad;
1232                 }
1233
1234                 chip->read_buf(mtd, oob, eccbytes);
1235                 oob += eccbytes;
1236
1237                 if (chip->ecc.postpad) {
1238                         chip->read_buf(mtd, oob, chip->ecc.postpad);
1239                         oob += chip->ecc.postpad;
1240                 }
1241         }
1242
1243         size = mtd->oobsize - (oob - chip->oob_poi);
1244         if (size)
1245                 chip->read_buf(mtd, oob, size);
1246
1247         return 0;
1248 }
1249
1250 /**
1251  * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function
1252  * @mtd: mtd info structure
1253  * @chip: nand chip info structure
1254  * @buf: buffer to store read data
1255  * @oob_required: caller requires OOB data read to chip->oob_poi
1256  * @page: page number to read
1257  */
1258 static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1259                                 uint8_t *buf, int oob_required, int page)
1260 {
1261         int i, eccsize = chip->ecc.size;
1262         int eccbytes = chip->ecc.bytes;
1263         int eccsteps = chip->ecc.steps;
1264         uint8_t *p = buf;
1265         uint8_t *ecc_calc = chip->buffers->ecccalc;
1266         uint8_t *ecc_code = chip->buffers->ecccode;
1267         uint32_t *eccpos = chip->ecc.layout->eccpos;
1268         unsigned int max_bitflips = 0;
1269
1270         chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
1271
1272         for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1273                 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1274
1275         for (i = 0; i < chip->ecc.total; i++)
1276                 ecc_code[i] = chip->oob_poi[eccpos[i]];
1277
1278         eccsteps = chip->ecc.steps;
1279         p = buf;
1280
1281         for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1282                 int stat;
1283
1284                 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1285                 if (stat < 0) {
1286                         mtd->ecc_stats.failed++;
1287                 } else {
1288                         mtd->ecc_stats.corrected += stat;
1289                         max_bitflips = max_t(unsigned int, max_bitflips, stat);
1290                 }
1291         }
1292         return max_bitflips;
1293 }
1294
1295 /**
1296  * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function
1297  * @mtd: mtd info structure
1298  * @chip: nand chip info structure
1299  * @data_offs: offset of requested data within the page
1300  * @readlen: data length
1301  * @bufpoi: buffer to store read data
1302  * @page: page number to read
1303  */
1304 static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1305                         uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1306                         int page)
1307 {
1308         int start_step, end_step, num_steps;
1309         uint32_t *eccpos = chip->ecc.layout->eccpos;
1310         uint8_t *p;
1311         int data_col_addr, i, gaps = 0;
1312         int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1313         int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
1314         int index;
1315         unsigned int max_bitflips = 0;
1316
1317         /* Column address within the page aligned to ECC size (256bytes) */
1318         start_step = data_offs / chip->ecc.size;
1319         end_step = (data_offs + readlen - 1) / chip->ecc.size;
1320         num_steps = end_step - start_step + 1;
1321         index = start_step * chip->ecc.bytes;
1322
1323         /* Data size aligned to ECC ecc.size */
1324         datafrag_len = num_steps * chip->ecc.size;
1325         eccfrag_len = num_steps * chip->ecc.bytes;
1326
1327         data_col_addr = start_step * chip->ecc.size;
1328         /* If we read not a page aligned data */
1329         if (data_col_addr != 0)
1330                 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1331
1332         p = bufpoi + data_col_addr;
1333         chip->read_buf(mtd, p, datafrag_len);
1334
1335         /* Calculate ECC */
1336         for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1337                 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1338
1339         /*
1340          * The performance is faster if we position offsets according to
1341          * ecc.pos. Let's make sure that there are no gaps in ECC positions.
1342          */
1343         for (i = 0; i < eccfrag_len - 1; i++) {
1344                 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
1345                         eccpos[i + start_step * chip->ecc.bytes + 1]) {
1346                         gaps = 1;
1347                         break;
1348                 }
1349         }
1350         if (gaps) {
1351                 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1352                 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1353         } else {
1354                 /*
1355                  * Send the command to read the particular ECC bytes take care
1356                  * about buswidth alignment in read_buf.
1357                  */
1358                 aligned_pos = eccpos[index] & ~(busw - 1);
1359                 aligned_len = eccfrag_len;
1360                 if (eccpos[index] & (busw - 1))
1361                         aligned_len++;
1362                 if (eccpos[index + (num_steps * chip->ecc.bytes)] & (busw - 1))
1363                         aligned_len++;
1364
1365                 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1366                                         mtd->writesize + aligned_pos, -1);
1367                 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1368         }
1369
1370         for (i = 0; i < eccfrag_len; i++)
1371                 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + index]];
1372
1373         p = bufpoi + data_col_addr;
1374         for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1375                 int stat;
1376
1377                 stat = chip->ecc.correct(mtd, p,
1378                         &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1379                 if (stat < 0) {
1380                         mtd->ecc_stats.failed++;
1381                 } else {
1382                         mtd->ecc_stats.corrected += stat;
1383                         max_bitflips = max_t(unsigned int, max_bitflips, stat);
1384                 }
1385         }
1386         return max_bitflips;
1387 }
1388
1389 /**
1390  * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function
1391  * @mtd: mtd info structure
1392  * @chip: nand chip info structure
1393  * @buf: buffer to store read data
1394  * @oob_required: caller requires OOB data read to chip->oob_poi
1395  * @page: page number to read
1396  *
1397  * Not for syndrome calculating ECC controllers which need a special oob layout.
1398  */
1399 static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1400                                 uint8_t *buf, int oob_required, int page)
1401 {
1402         int i, eccsize = chip->ecc.size;
1403         int eccbytes = chip->ecc.bytes;
1404         int eccsteps = chip->ecc.steps;
1405         uint8_t *p = buf;
1406         uint8_t *ecc_calc = chip->buffers->ecccalc;
1407         uint8_t *ecc_code = chip->buffers->ecccode;
1408         uint32_t *eccpos = chip->ecc.layout->eccpos;
1409         unsigned int max_bitflips = 0;
1410
1411         for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1412                 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1413                 chip->read_buf(mtd, p, eccsize);
1414                 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1415         }
1416         chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1417
1418         for (i = 0; i < chip->ecc.total; i++)
1419                 ecc_code[i] = chip->oob_poi[eccpos[i]];
1420
1421         eccsteps = chip->ecc.steps;
1422         p = buf;
1423
1424         for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1425                 int stat;
1426
1427                 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1428                 if (stat < 0) {
1429                         mtd->ecc_stats.failed++;
1430                 } else {
1431                         mtd->ecc_stats.corrected += stat;
1432                         max_bitflips = max_t(unsigned int, max_bitflips, stat);
1433                 }
1434         }
1435         return max_bitflips;
1436 }
1437
1438 /**
1439  * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first
1440  * @mtd: mtd info structure
1441  * @chip: nand chip info structure
1442  * @buf: buffer to store read data
1443  * @oob_required: caller requires OOB data read to chip->oob_poi
1444  * @page: page number to read
1445  *
1446  * Hardware ECC for large page chips, require OOB to be read first. For this
1447  * ECC mode, the write_page method is re-used from ECC_HW. These methods
1448  * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with
1449  * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from
1450  * the data area, by overwriting the NAND manufacturer bad block markings.
1451  */
1452 static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1453         struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
1454 {
1455         int i, eccsize = chip->ecc.size;
1456         int eccbytes = chip->ecc.bytes;
1457         int eccsteps = chip->ecc.steps;
1458         uint8_t *p = buf;
1459         uint8_t *ecc_code = chip->buffers->ecccode;
1460         uint32_t *eccpos = chip->ecc.layout->eccpos;
1461         uint8_t *ecc_calc = chip->buffers->ecccalc;
1462         unsigned int max_bitflips = 0;
1463
1464         /* Read the OOB area first */
1465         chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1466         chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1467         chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1468
1469         for (i = 0; i < chip->ecc.total; i++)
1470                 ecc_code[i] = chip->oob_poi[eccpos[i]];
1471
1472         for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1473                 int stat;
1474
1475                 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1476                 chip->read_buf(mtd, p, eccsize);
1477                 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1478
1479                 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1480                 if (stat < 0) {
1481                         mtd->ecc_stats.failed++;
1482                 } else {
1483                         mtd->ecc_stats.corrected += stat;
1484                         max_bitflips = max_t(unsigned int, max_bitflips, stat);
1485                 }
1486         }
1487         return max_bitflips;
1488 }
1489
1490 /**
1491  * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read
1492  * @mtd: mtd info structure
1493  * @chip: nand chip info structure
1494  * @buf: buffer to store read data
1495  * @oob_required: caller requires OOB data read to chip->oob_poi
1496  * @page: page number to read
1497  *
1498  * The hw generator calculates the error syndrome automatically. Therefore we
1499  * need a special oob layout and handling.
1500  */
1501 static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1502                                    uint8_t *buf, int oob_required, int page)
1503 {
1504         int i, eccsize = chip->ecc.size;
1505         int eccbytes = chip->ecc.bytes;
1506         int eccsteps = chip->ecc.steps;
1507         uint8_t *p = buf;
1508         uint8_t *oob = chip->oob_poi;
1509         unsigned int max_bitflips = 0;
1510
1511         for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1512                 int stat;
1513
1514                 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1515                 chip->read_buf(mtd, p, eccsize);
1516
1517                 if (chip->ecc.prepad) {
1518                         chip->read_buf(mtd, oob, chip->ecc.prepad);
1519                         oob += chip->ecc.prepad;
1520                 }
1521
1522                 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1523                 chip->read_buf(mtd, oob, eccbytes);
1524                 stat = chip->ecc.correct(mtd, p, oob, NULL);
1525
1526                 if (stat < 0) {
1527                         mtd->ecc_stats.failed++;
1528                 } else {
1529                         mtd->ecc_stats.corrected += stat;
1530                         max_bitflips = max_t(unsigned int, max_bitflips, stat);
1531                 }
1532
1533                 oob += eccbytes;
1534
1535                 if (chip->ecc.postpad) {
1536                         chip->read_buf(mtd, oob, chip->ecc.postpad);
1537                         oob += chip->ecc.postpad;
1538                 }
1539         }
1540
1541         /* Calculate remaining oob bytes */
1542         i = mtd->oobsize - (oob - chip->oob_poi);
1543         if (i)
1544                 chip->read_buf(mtd, oob, i);
1545
1546         return max_bitflips;
1547 }
1548
1549 /**
1550  * nand_transfer_oob - [INTERN] Transfer oob to client buffer
1551  * @chip: nand chip structure
1552  * @oob: oob destination address
1553  * @ops: oob ops structure
1554  * @len: size of oob to transfer
1555  */
1556 static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1557                                   struct mtd_oob_ops *ops, size_t len)
1558 {
1559         switch (ops->mode) {
1560
1561         case MTD_OPS_PLACE_OOB:
1562         case MTD_OPS_RAW:
1563                 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1564                 return oob + len;
1565
1566         case MTD_OPS_AUTO_OOB: {
1567                 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1568                 uint32_t boffs = 0, roffs = ops->ooboffs;
1569                 size_t bytes = 0;
1570
1571                 for (; free->length && len; free++, len -= bytes) {
1572                         /* Read request not from offset 0? */
1573                         if (unlikely(roffs)) {
1574                                 if (roffs >= free->length) {
1575                                         roffs -= free->length;
1576                                         continue;
1577                                 }
1578                                 boffs = free->offset + roffs;
1579                                 bytes = min_t(size_t, len,
1580                                               (free->length - roffs));
1581                                 roffs = 0;
1582                         } else {
1583                                 bytes = min_t(size_t, len, free->length);
1584                                 boffs = free->offset;
1585                         }
1586                         memcpy(oob, chip->oob_poi + boffs, bytes);
1587                         oob += bytes;
1588                 }
1589                 return oob;
1590         }
1591         default:
1592                 BUG();
1593         }
1594         return NULL;
1595 }
1596
1597 /**
1598  * nand_setup_read_retry - [INTERN] Set the READ RETRY mode
1599  * @mtd: MTD device structure
1600  * @retry_mode: the retry mode to use
1601  *
1602  * Some vendors supply a special command to shift the Vt threshold, to be used
1603  * when there are too many bitflips in a page (i.e., ECC error). After setting
1604  * a new threshold, the host should retry reading the page.
1605  */
1606 static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
1607 {
1608         struct nand_chip *chip = mtd->priv;
1609
1610         pr_debug("setting READ RETRY mode %d\n", retry_mode);
1611
1612         if (retry_mode >= chip->read_retries)
1613                 return -EINVAL;
1614
1615         if (!chip->setup_read_retry)
1616                 return -EOPNOTSUPP;
1617
1618         return chip->setup_read_retry(mtd, retry_mode);
1619 }
1620
1621 /**
1622  * nand_do_read_ops - [INTERN] Read data with ECC
1623  * @mtd: MTD device structure
1624  * @from: offset to read from
1625  * @ops: oob ops structure
1626  *
1627  * Internal function. Called with chip held.
1628  */
1629 static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1630                             struct mtd_oob_ops *ops)
1631 {
1632         int chipnr, page, realpage, col, bytes, aligned, oob_required;
1633         struct nand_chip *chip = mtd->priv;
1634         int ret = 0;
1635         uint32_t readlen = ops->len;
1636         uint32_t oobreadlen = ops->ooblen;
1637         uint32_t max_oobsize = ops->mode == MTD_OPS_AUTO_OOB ?
1638                 mtd->oobavail : mtd->oobsize;
1639
1640         uint8_t *bufpoi, *oob, *buf;
1641         unsigned int max_bitflips = 0;
1642         int retry_mode = 0;
1643         bool ecc_fail = false;
1644
1645         chipnr = (int)(from >> chip->chip_shift);
1646         chip->select_chip(mtd, chipnr);
1647
1648         realpage = (int)(from >> chip->page_shift);
1649         page = realpage & chip->pagemask;
1650
1651         col = (int)(from & (mtd->writesize - 1));
1652
1653         buf = ops->datbuf;
1654         oob = ops->oobbuf;
1655         oob_required = oob ? 1 : 0;
1656
1657         while (1) {
1658                 unsigned int ecc_failures = mtd->ecc_stats.failed;
1659
1660                 WATCHDOG_RESET();
1661                 bytes = min(mtd->writesize - col, readlen);
1662                 aligned = (bytes == mtd->writesize);
1663
1664                 /* Is the current page in the buffer? */
1665                 if (realpage != chip->pagebuf || oob) {
1666                         bufpoi = aligned ? buf : chip->buffers->databuf;
1667
1668 read_retry:
1669                         chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1670
1671                         /*
1672                          * Now read the page into the buffer.  Absent an error,
1673                          * the read methods return max bitflips per ecc step.
1674                          */
1675                         if (unlikely(ops->mode == MTD_OPS_RAW))
1676                                 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
1677                                                               oob_required,
1678                                                               page);
1679                         else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
1680                                  !oob)
1681                                 ret = chip->ecc.read_subpage(mtd, chip,
1682                                                         col, bytes, bufpoi,
1683                                                         page);
1684                         else
1685                                 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1686                                                           oob_required, page);
1687                         if (ret < 0) {
1688                                 if (!aligned)
1689                                         /* Invalidate page cache */
1690                                         chip->pagebuf = -1;
1691                                 break;
1692                         }
1693
1694                         max_bitflips = max_t(unsigned int, max_bitflips, ret);
1695
1696                         /* Transfer not aligned data */
1697                         if (!aligned) {
1698                                 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
1699                                     !(mtd->ecc_stats.failed - ecc_failures) &&
1700                                     (ops->mode != MTD_OPS_RAW)) {
1701                                         chip->pagebuf = realpage;
1702                                         chip->pagebuf_bitflips = ret;
1703                                 } else {
1704                                         /* Invalidate page cache */
1705                                         chip->pagebuf = -1;
1706                                 }
1707                                 memcpy(buf, chip->buffers->databuf + col, bytes);
1708                         }
1709
1710                         if (unlikely(oob)) {
1711                                 int toread = min(oobreadlen, max_oobsize);
1712
1713                                 if (toread) {
1714                                         oob = nand_transfer_oob(chip,
1715                                                 oob, ops, toread);
1716                                         oobreadlen -= toread;
1717                                 }
1718                         }
1719
1720                         if (chip->options & NAND_NEED_READRDY) {
1721                                 /* Apply delay or wait for ready/busy pin */
1722                                 if (!chip->dev_ready)
1723                                         udelay(chip->chip_delay);
1724                                 else
1725                                         nand_wait_ready(mtd);
1726                         }
1727
1728                         if (mtd->ecc_stats.failed - ecc_failures) {
1729                                 if (retry_mode + 1 < chip->read_retries) {
1730                                         retry_mode++;
1731                                         ret = nand_setup_read_retry(mtd,
1732                                                         retry_mode);
1733                                         if (ret < 0)
1734                                                 break;
1735
1736                                         /* Reset failures; retry */
1737                                         mtd->ecc_stats.failed = ecc_failures;
1738                                         goto read_retry;
1739                                 } else {
1740                                         /* No more retry modes; real failure */
1741                                         ecc_fail = true;
1742                                 }
1743                         }
1744
1745                         buf += bytes;
1746                 } else {
1747                         memcpy(buf, chip->buffers->databuf + col, bytes);
1748                         buf += bytes;
1749                         max_bitflips = max_t(unsigned int, max_bitflips,
1750                                              chip->pagebuf_bitflips);
1751                 }
1752
1753                 readlen -= bytes;
1754
1755                 /* Reset to retry mode 0 */
1756                 if (retry_mode) {
1757                         ret = nand_setup_read_retry(mtd, 0);
1758                         if (ret < 0)
1759                                 break;
1760                         retry_mode = 0;
1761                 }
1762
1763                 if (!readlen)
1764                         break;
1765
1766                 /* For subsequent reads align to page boundary */
1767                 col = 0;
1768                 /* Increment page address */
1769                 realpage++;
1770
1771                 page = realpage & chip->pagemask;
1772                 /* Check, if we cross a chip boundary */
1773                 if (!page) {
1774                         chipnr++;
1775                         chip->select_chip(mtd, -1);
1776                         chip->select_chip(mtd, chipnr);
1777                 }
1778         }
1779         chip->select_chip(mtd, -1);
1780
1781         ops->retlen = ops->len - (size_t) readlen;
1782         if (oob)
1783                 ops->oobretlen = ops->ooblen - oobreadlen;
1784
1785         if (ret < 0)
1786                 return ret;
1787
1788         if (ecc_fail)
1789                 return -EBADMSG;
1790
1791         return max_bitflips;
1792 }
1793
1794 /**
1795  * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc
1796  * @mtd: MTD device structure
1797  * @from: offset to read from
1798  * @len: number of bytes to read
1799  * @retlen: pointer to variable to store the number of read bytes
1800  * @buf: the databuffer to put data
1801  *
1802  * Get hold of the chip and call nand_do_read.
1803  */
1804 static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1805                      size_t *retlen, uint8_t *buf)
1806 {
1807         struct mtd_oob_ops ops;
1808         int ret;
1809
1810         nand_get_device(mtd, FL_READING);
1811         ops.len = len;
1812         ops.datbuf = buf;
1813         ops.oobbuf = NULL;
1814         ops.mode = MTD_OPS_PLACE_OOB;
1815         ret = nand_do_read_ops(mtd, from, &ops);
1816         *retlen = ops.retlen;
1817         nand_release_device(mtd);
1818         return ret;
1819 }
1820
1821 /**
1822  * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function
1823  * @mtd: mtd info structure
1824  * @chip: nand chip info structure
1825  * @page: page number to read
1826  */
1827 static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1828                              int page)
1829 {
1830         chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1831         chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1832         return 0;
1833 }
1834
1835 /**
1836  * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC
1837  *                          with syndromes
1838  * @mtd: mtd info structure
1839  * @chip: nand chip info structure
1840  * @page: page number to read
1841  */
1842 static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1843                                   int page)
1844 {
1845         uint8_t *buf = chip->oob_poi;
1846         int length = mtd->oobsize;
1847         int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1848         int eccsize = chip->ecc.size;
1849         uint8_t *bufpoi = buf;
1850         int i, toread, sndrnd = 0, pos;
1851
1852         chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1853         for (i = 0; i < chip->ecc.steps; i++) {
1854                 if (sndrnd) {
1855                         pos = eccsize + i * (eccsize + chunk);
1856                         if (mtd->writesize > 512)
1857                                 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1858                         else
1859                                 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1860                 } else
1861                         sndrnd = 1;
1862                 toread = min_t(int, length, chunk);
1863                 chip->read_buf(mtd, bufpoi, toread);
1864                 bufpoi += toread;
1865                 length -= toread;
1866         }
1867         if (length > 0)
1868                 chip->read_buf(mtd, bufpoi, length);
1869
1870         return 0;
1871 }
1872
1873 /**
1874  * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function
1875  * @mtd: mtd info structure
1876  * @chip: nand chip info structure
1877  * @page: page number to write
1878  */
1879 static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1880                               int page)
1881 {
1882         int status = 0;
1883         const uint8_t *buf = chip->oob_poi;
1884         int length = mtd->oobsize;
1885
1886         chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1887         chip->write_buf(mtd, buf, length);
1888         /* Send command to program the OOB data */
1889         chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1890
1891         status = chip->waitfunc(mtd, chip);
1892
1893         return status & NAND_STATUS_FAIL ? -EIO : 0;
1894 }
1895
1896 /**
1897  * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC
1898  *                           with syndrome - only for large page flash
1899  * @mtd: mtd info structure
1900  * @chip: nand chip info structure
1901  * @page: page number to write
1902  */
1903 static int nand_write_oob_syndrome(struct mtd_info *mtd,
1904                                    struct nand_chip *chip, int page)
1905 {
1906         int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1907         int eccsize = chip->ecc.size, length = mtd->oobsize;
1908         int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1909         const uint8_t *bufpoi = chip->oob_poi;
1910
1911         /*
1912          * data-ecc-data-ecc ... ecc-oob
1913          * or
1914          * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1915          */
1916         if (!chip->ecc.prepad && !chip->ecc.postpad) {
1917                 pos = steps * (eccsize + chunk);
1918                 steps = 0;
1919         } else
1920                 pos = eccsize;
1921
1922         chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1923         for (i = 0; i < steps; i++) {
1924                 if (sndcmd) {
1925                         if (mtd->writesize <= 512) {
1926                                 uint32_t fill = 0xFFFFFFFF;
1927
1928                                 len = eccsize;
1929                                 while (len > 0) {
1930                                         int num = min_t(int, len, 4);
1931                                         chip->write_buf(mtd, (uint8_t *)&fill,
1932                                                         num);
1933                                         len -= num;
1934                                 }
1935                         } else {
1936                                 pos = eccsize + i * (eccsize + chunk);
1937                                 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1938                         }
1939                 } else
1940                         sndcmd = 1;
1941                 len = min_t(int, length, chunk);
1942                 chip->write_buf(mtd, bufpoi, len);
1943                 bufpoi += len;
1944                 length -= len;
1945         }
1946         if (length > 0)
1947                 chip->write_buf(mtd, bufpoi, length);
1948
1949         chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1950         status = chip->waitfunc(mtd, chip);
1951
1952         return status & NAND_STATUS_FAIL ? -EIO : 0;
1953 }
1954
1955 /**
1956  * nand_do_read_oob - [INTERN] NAND read out-of-band
1957  * @mtd: MTD device structure
1958  * @from: offset to read from
1959  * @ops: oob operations description structure
1960  *
1961  * NAND read out-of-band data from the spare area.
1962  */
1963 static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1964                             struct mtd_oob_ops *ops)
1965 {
1966         int page, realpage, chipnr;
1967         struct nand_chip *chip = mtd->priv;
1968         struct mtd_ecc_stats stats;
1969         int readlen = ops->ooblen;
1970         int len;
1971         uint8_t *buf = ops->oobbuf;
1972         int ret = 0;
1973
1974         pr_debug("%s: from = 0x%08Lx, len = %i\n",
1975                         __func__, (unsigned long long)from, readlen);
1976
1977         stats = mtd->ecc_stats;
1978
1979         if (ops->mode == MTD_OPS_AUTO_OOB)
1980                 len = chip->ecc.layout->oobavail;
1981         else
1982                 len = mtd->oobsize;
1983
1984         if (unlikely(ops->ooboffs >= len)) {
1985                 pr_debug("%s: attempt to start read outside oob\n",
1986                                 __func__);
1987                 return -EINVAL;
1988         }
1989
1990         /* Do not allow reads past end of device */
1991         if (unlikely(from >= mtd->size ||
1992                      ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1993                                         (from >> chip->page_shift)) * len)) {
1994                 pr_debug("%s: attempt to read beyond end of device\n",
1995                                 __func__);
1996                 return -EINVAL;
1997         }
1998
1999         chipnr = (int)(from >> chip->chip_shift);
2000         chip->select_chip(mtd, chipnr);
2001
2002         /* Shift to get page */
2003         realpage = (int)(from >> chip->page_shift);
2004         page = realpage & chip->pagemask;
2005
2006         while (1) {
2007                 WATCHDOG_RESET();
2008
2009                 if (ops->mode == MTD_OPS_RAW)
2010                         ret = chip->ecc.read_oob_raw(mtd, chip, page);
2011                 else
2012                         ret = chip->ecc.read_oob(mtd, chip, page);
2013
2014                 if (ret < 0)
2015                         break;
2016
2017                 len = min(len, readlen);
2018                 buf = nand_transfer_oob(chip, buf, ops, len);
2019
2020                 if (chip->options & NAND_NEED_READRDY) {
2021                         /* Apply delay or wait for ready/busy pin */
2022                         if (!chip->dev_ready)
2023                                 udelay(chip->chip_delay);
2024                         else
2025                                 nand_wait_ready(mtd);
2026                 }
2027
2028                 readlen -= len;
2029                 if (!readlen)
2030                         break;
2031
2032                 /* Increment page address */
2033                 realpage++;
2034
2035                 page = realpage & chip->pagemask;
2036                 /* Check, if we cross a chip boundary */
2037                 if (!page) {
2038                         chipnr++;
2039                         chip->select_chip(mtd, -1);
2040                         chip->select_chip(mtd, chipnr);
2041                 }
2042         }
2043         chip->select_chip(mtd, -1);
2044
2045         ops->oobretlen = ops->ooblen - readlen;
2046
2047         if (ret < 0)
2048                 return ret;
2049
2050         if (mtd->ecc_stats.failed - stats.failed)
2051                 return -EBADMSG;
2052
2053         return  mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
2054 }
2055
2056 /**
2057  * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
2058  * @mtd: MTD device structure
2059  * @from: offset to read from
2060  * @ops: oob operation description structure
2061  *
2062  * NAND read data and/or out-of-band data.
2063  */
2064 static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2065                          struct mtd_oob_ops *ops)
2066 {
2067         int ret = -ENOTSUPP;
2068
2069         ops->retlen = 0;
2070
2071         /* Do not allow reads past end of device */
2072         if (ops->datbuf && (from + ops->len) > mtd->size) {
2073                 pr_debug("%s: attempt to read beyond end of device\n",
2074                                 __func__);
2075                 return -EINVAL;
2076         }
2077
2078         nand_get_device(mtd, FL_READING);
2079
2080         switch (ops->mode) {
2081         case MTD_OPS_PLACE_OOB:
2082         case MTD_OPS_AUTO_OOB:
2083         case MTD_OPS_RAW:
2084                 break;
2085
2086         default:
2087                 goto out;
2088         }
2089
2090         if (!ops->datbuf)
2091                 ret = nand_do_read_oob(mtd, from, ops);
2092         else
2093                 ret = nand_do_read_ops(mtd, from, ops);
2094
2095 out:
2096         nand_release_device(mtd);
2097         return ret;
2098 }
2099
2100
2101 /**
2102  * nand_write_page_raw - [INTERN] raw page write function
2103  * @mtd: mtd info structure
2104  * @chip: nand chip info structure
2105  * @buf: data buffer
2106  * @oob_required: must write chip->oob_poi to OOB
2107  *
2108  * Not for syndrome calculating ECC controllers, which use a special oob layout.
2109  */
2110 static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2111                                 const uint8_t *buf, int oob_required)
2112 {
2113         chip->write_buf(mtd, buf, mtd->writesize);
2114         if (oob_required)
2115                 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2116
2117         return 0;
2118 }
2119
2120 /**
2121  * nand_write_page_raw_syndrome - [INTERN] raw page write function
2122  * @mtd: mtd info structure
2123  * @chip: nand chip info structure
2124  * @buf: data buffer
2125  * @oob_required: must write chip->oob_poi to OOB
2126  *
2127  * We need a special oob layout and handling even when ECC isn't checked.
2128  */
2129 static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
2130                                         struct nand_chip *chip,
2131                                         const uint8_t *buf, int oob_required)
2132 {
2133         int eccsize = chip->ecc.size;
2134         int eccbytes = chip->ecc.bytes;
2135         uint8_t *oob = chip->oob_poi;
2136         int steps, size;
2137
2138         for (steps = chip->ecc.steps; steps > 0; steps--) {
2139                 chip->write_buf(mtd, buf, eccsize);
2140                 buf += eccsize;
2141
2142                 if (chip->ecc.prepad) {
2143                         chip->write_buf(mtd, oob, chip->ecc.prepad);
2144                         oob += chip->ecc.prepad;
2145                 }
2146
2147                 chip->write_buf(mtd, oob, eccbytes);
2148                 oob += eccbytes;
2149
2150                 if (chip->ecc.postpad) {
2151                         chip->write_buf(mtd, oob, chip->ecc.postpad);
2152                         oob += chip->ecc.postpad;
2153                 }
2154         }
2155
2156         size = mtd->oobsize - (oob - chip->oob_poi);
2157         if (size)
2158                 chip->write_buf(mtd, oob, size);
2159
2160         return 0;
2161 }
2162 /**
2163  * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function
2164  * @mtd: mtd info structure
2165  * @chip: nand chip info structure
2166  * @buf: data buffer
2167  * @oob_required: must write chip->oob_poi to OOB
2168  */
2169 static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
2170                                   const uint8_t *buf, int oob_required)
2171 {
2172         int i, eccsize = chip->ecc.size;
2173         int eccbytes = chip->ecc.bytes;
2174         int eccsteps = chip->ecc.steps;
2175         uint8_t *ecc_calc = chip->buffers->ecccalc;
2176         const uint8_t *p = buf;
2177         uint32_t *eccpos = chip->ecc.layout->eccpos;
2178
2179         /* Software ECC calculation */
2180         for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2181                 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2182
2183         for (i = 0; i < chip->ecc.total; i++)
2184                 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2185
2186         return chip->ecc.write_page_raw(mtd, chip, buf, 1);
2187 }
2188
2189 /**
2190  * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function
2191  * @mtd: mtd info structure
2192  * @chip: nand chip info structure
2193  * @buf: data buffer
2194  * @oob_required: must write chip->oob_poi to OOB
2195  */
2196 static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
2197                                   const uint8_t *buf, int oob_required)
2198 {
2199         int i, eccsize = chip->ecc.size;
2200         int eccbytes = chip->ecc.bytes;
2201         int eccsteps = chip->ecc.steps;
2202         uint8_t *ecc_calc = chip->buffers->ecccalc;
2203         const uint8_t *p = buf;
2204         uint32_t *eccpos = chip->ecc.layout->eccpos;
2205
2206         for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2207                 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2208                 chip->write_buf(mtd, p, eccsize);
2209                 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2210         }
2211
2212         for (i = 0; i < chip->ecc.total; i++)
2213                 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2214
2215         chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2216
2217         return 0;
2218 }
2219
2220
2221 /**
2222  * nand_write_subpage_hwecc - [REPLACABLE] hardware ECC based subpage write
2223  * @mtd:        mtd info structure
2224  * @chip:       nand chip info structure
2225  * @offset:     column address of subpage within the page
2226  * @data_len:   data length
2227  * @buf:        data buffer
2228  * @oob_required: must write chip->oob_poi to OOB
2229  */
2230 static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2231                                 struct nand_chip *chip, uint32_t offset,
2232                                 uint32_t data_len, const uint8_t *buf,
2233                                 int oob_required)
2234 {
2235         uint8_t *oob_buf  = chip->oob_poi;
2236         uint8_t *ecc_calc = chip->buffers->ecccalc;
2237         int ecc_size      = chip->ecc.size;
2238         int ecc_bytes     = chip->ecc.bytes;
2239         int ecc_steps     = chip->ecc.steps;
2240         uint32_t *eccpos  = chip->ecc.layout->eccpos;
2241         uint32_t start_step = offset / ecc_size;
2242         uint32_t end_step   = (offset + data_len - 1) / ecc_size;
2243         int oob_bytes       = mtd->oobsize / ecc_steps;
2244         int step, i;
2245
2246         for (step = 0; step < ecc_steps; step++) {
2247                 /* configure controller for WRITE access */
2248                 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2249
2250                 /* write data (untouched subpages already masked by 0xFF) */
2251                 chip->write_buf(mtd, buf, ecc_size);
2252
2253                 /* mask ECC of un-touched subpages by padding 0xFF */
2254                 if ((step < start_step) || (step > end_step))
2255                         memset(ecc_calc, 0xff, ecc_bytes);
2256                 else
2257                         chip->ecc.calculate(mtd, buf, ecc_calc);
2258
2259                 /* mask OOB of un-touched subpages by padding 0xFF */
2260                 /* if oob_required, preserve OOB metadata of written subpage */
2261                 if (!oob_required || (step < start_step) || (step > end_step))
2262                         memset(oob_buf, 0xff, oob_bytes);
2263
2264                 buf += ecc_size;
2265                 ecc_calc += ecc_bytes;
2266                 oob_buf  += oob_bytes;
2267         }
2268
2269         /* copy calculated ECC for whole page to chip->buffer->oob */
2270         /* this include masked-value(0xFF) for unwritten subpages */
2271         ecc_calc = chip->buffers->ecccalc;
2272         for (i = 0; i < chip->ecc.total; i++)
2273                 chip->oob_poi[eccpos[i]] = ecc_calc[i];
2274
2275         /* write OOB buffer to NAND device */
2276         chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2277
2278         return 0;
2279 }
2280
2281
2282 /**
2283  * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write
2284  * @mtd: mtd info structure
2285  * @chip: nand chip info structure
2286  * @buf: data buffer
2287  * @oob_required: must write chip->oob_poi to OOB
2288  *
2289  * The hw generator calculates the error syndrome automatically. Therefore we
2290  * need a special oob layout and handling.
2291  */
2292 static int nand_write_page_syndrome(struct mtd_info *mtd,
2293                                     struct nand_chip *chip,
2294                                     const uint8_t *buf, int oob_required)
2295 {
2296         int i, eccsize = chip->ecc.size;
2297         int eccbytes = chip->ecc.bytes;
2298         int eccsteps = chip->ecc.steps;
2299         const uint8_t *p = buf;
2300         uint8_t *oob = chip->oob_poi;
2301
2302         for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2303
2304                 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2305                 chip->write_buf(mtd, p, eccsize);
2306
2307                 if (chip->ecc.prepad) {
2308                         chip->write_buf(mtd, oob, chip->ecc.prepad);
2309                         oob += chip->ecc.prepad;
2310                 }
2311
2312                 chip->ecc.calculate(mtd, p, oob);
2313                 chip->write_buf(mtd, oob, eccbytes);
2314                 oob += eccbytes;
2315
2316                 if (chip->ecc.postpad) {
2317                         chip->write_buf(mtd, oob, chip->ecc.postpad);
2318                         oob += chip->ecc.postpad;
2319                 }
2320         }
2321
2322         /* Calculate remaining oob bytes */
2323         i = mtd->oobsize - (oob - chip->oob_poi);
2324         if (i)
2325                 chip->write_buf(mtd, oob, i);
2326
2327         return 0;
2328 }
2329
2330 /**
2331  * nand_write_page - [REPLACEABLE] write one page
2332  * @mtd: MTD device structure
2333  * @chip: NAND chip descriptor
2334  * @offset: address offset within the page
2335  * @data_len: length of actual data to be written
2336  * @buf: the data to write
2337  * @oob_required: must write chip->oob_poi to OOB
2338  * @page: page number to write
2339  * @cached: cached programming
2340  * @raw: use _raw version of write_page
2341  */
2342 static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
2343                 uint32_t offset, int data_len, const uint8_t *buf,
2344                 int oob_required, int page, int cached, int raw)
2345 {
2346         int status, subpage;
2347
2348         if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2349                 chip->ecc.write_subpage)
2350                 subpage = offset || (data_len < mtd->writesize);
2351         else
2352                 subpage = 0;
2353
2354         chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2355
2356         if (unlikely(raw))
2357                 status = chip->ecc.write_page_raw(mtd, chip, buf,
2358                                                         oob_required);
2359         else if (subpage)
2360                 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2361                                                          buf, oob_required);
2362         else
2363                 status = chip->ecc.write_page(mtd, chip, buf, oob_required);
2364
2365         if (status < 0)
2366                 return status;
2367
2368         /*
2369          * Cached progamming disabled for now. Not sure if it's worth the
2370          * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s).
2371          */
2372         cached = 0;
2373
2374         if (!cached || !NAND_HAS_CACHEPROG(chip)) {
2375
2376                 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2377                 status = chip->waitfunc(mtd, chip);
2378                 /*
2379                  * See if operation failed and additional status checks are
2380                  * available.
2381                  */
2382                 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2383                         status = chip->errstat(mtd, chip, FL_WRITING, status,
2384                                                page);
2385
2386                 if (status & NAND_STATUS_FAIL)
2387                         return -EIO;
2388         } else {
2389                 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
2390                 status = chip->waitfunc(mtd, chip);
2391         }
2392
2393         return 0;
2394 }
2395
2396 /**
2397  * nand_fill_oob - [INTERN] Transfer client buffer to oob
2398  * @mtd: MTD device structure
2399  * @oob: oob data buffer
2400  * @len: oob data write length
2401  * @ops: oob ops structure
2402  */
2403 static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2404                               struct mtd_oob_ops *ops)
2405 {
2406         struct nand_chip *chip = mtd->priv;
2407
2408         /*
2409          * Initialise to all 0xFF, to avoid the possibility of left over OOB
2410          * data from a previous OOB read.
2411          */
2412         memset(chip->oob_poi, 0xff, mtd->oobsize);
2413
2414         switch (ops->mode) {
2415
2416         case MTD_OPS_PLACE_OOB:
2417         case MTD_OPS_RAW:
2418                 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2419                 return oob + len;
2420
2421         case MTD_OPS_AUTO_OOB: {
2422                 struct nand_oobfree *free = chip->ecc.layout->oobfree;
2423                 uint32_t boffs = 0, woffs = ops->ooboffs;
2424                 size_t bytes = 0;
2425
2426                 for (; free->length && len; free++, len -= bytes) {
2427                         /* Write request not from offset 0? */
2428                         if (unlikely(woffs)) {
2429                                 if (woffs >= free->length) {
2430                                         woffs -= free->length;
2431                                         continue;
2432                                 }
2433                                 boffs = free->offset + woffs;
2434                                 bytes = min_t(size_t, len,
2435                                               (free->length - woffs));
2436                                 woffs = 0;
2437                         } else {
2438                                 bytes = min_t(size_t, len, free->length);
2439                                 boffs = free->offset;
2440                         }
2441                         memcpy(chip->oob_poi + boffs, oob, bytes);
2442                         oob += bytes;
2443                 }
2444                 return oob;
2445         }
2446         default:
2447                 BUG();
2448         }
2449         return NULL;
2450 }
2451
2452 #define NOTALIGNED(x)   (((x) & (chip->subpagesize - 1)) != 0)
2453
2454 /**
2455  * nand_do_write_ops - [INTERN] NAND write with ECC
2456  * @mtd: MTD device structure
2457  * @to: offset to write to
2458  * @ops: oob operations description structure
2459  *
2460  * NAND write with ECC.
2461  */
2462 static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2463                              struct mtd_oob_ops *ops)
2464 {
2465         int chipnr, realpage, page, blockmask, column;
2466         struct nand_chip *chip = mtd->priv;
2467         uint32_t writelen = ops->len;
2468
2469         uint32_t oobwritelen = ops->ooblen;
2470         uint32_t oobmaxlen = ops->mode == MTD_OPS_AUTO_OOB ?
2471                                 mtd->oobavail : mtd->oobsize;
2472
2473         uint8_t *oob = ops->oobbuf;
2474         uint8_t *buf = ops->datbuf;
2475         int ret;
2476         int oob_required = oob ? 1 : 0;
2477
2478         ops->retlen = 0;
2479         if (!writelen)
2480                 return 0;
2481
2482 #ifndef __UBOOT__
2483         /* Reject writes, which are not page aligned */
2484         if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
2485 #else
2486         /* Reject writes, which are not page aligned */
2487         if (NOTALIGNED(to)) {
2488 #endif
2489                 pr_notice("%s: attempt to write non page aligned data\n",
2490                            __func__);
2491                 return -EINVAL;
2492         }
2493
2494         column = to & (mtd->writesize - 1);
2495
2496         chipnr = (int)(to >> chip->chip_shift);
2497         chip->select_chip(mtd, chipnr);
2498
2499         /* Check, if it is write protected */
2500         if (nand_check_wp(mtd)) {
2501                 ret = -EIO;
2502                 goto err_out;
2503         }
2504
2505         realpage = (int)(to >> chip->page_shift);
2506         page = realpage & chip->pagemask;
2507         blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2508
2509         /* Invalidate the page cache, when we write to the cached page */
2510         if (to <= (chip->pagebuf << chip->page_shift) &&
2511             (chip->pagebuf << chip->page_shift) < (to + ops->len))
2512                 chip->pagebuf = -1;
2513
2514         /* Don't allow multipage oob writes with offset */
2515         if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2516                 ret = -EINVAL;
2517                 goto err_out;
2518         }
2519
2520         while (1) {
2521                 int bytes = mtd->writesize;
2522                 int cached = writelen > bytes && page != blockmask;
2523                 uint8_t *wbuf = buf;
2524
2525                 WATCHDOG_RESET();
2526                 /* Partial page write? */
2527                 if (unlikely(column || writelen < (mtd->writesize - 1))) {
2528                         cached = 0;
2529                         bytes = min_t(int, bytes - column, (int) writelen);
2530                         chip->pagebuf = -1;
2531                         memset(chip->buffers->databuf, 0xff, mtd->writesize);
2532                         memcpy(&chip->buffers->databuf[column], buf, bytes);
2533                         wbuf = chip->buffers->databuf;
2534                 }
2535
2536                 if (unlikely(oob)) {
2537                         size_t len = min(oobwritelen, oobmaxlen);
2538                         oob = nand_fill_oob(mtd, oob, len, ops);
2539                         oobwritelen -= len;
2540                 } else {
2541                         /* We still need to erase leftover OOB data */
2542                         memset(chip->oob_poi, 0xff, mtd->oobsize);
2543                 }
2544                 ret = chip->write_page(mtd, chip, column, bytes, wbuf,
2545                                         oob_required, page, cached,
2546                                         (ops->mode == MTD_OPS_RAW));
2547                 if (ret)
2548                         break;
2549
2550                 writelen -= bytes;
2551                 if (!writelen)
2552                         break;
2553
2554                 column = 0;
2555                 buf += bytes;
2556                 realpage++;
2557
2558                 page = realpage & chip->pagemask;
2559                 /* Check, if we cross a chip boundary */
2560                 if (!page) {
2561                         chipnr++;
2562                         chip->select_chip(mtd, -1);
2563                         chip->select_chip(mtd, chipnr);
2564                 }
2565         }
2566
2567         ops->retlen = ops->len - writelen;
2568         if (unlikely(oob))
2569                 ops->oobretlen = ops->ooblen;
2570
2571 err_out:
2572         chip->select_chip(mtd, -1);
2573         return ret;
2574 }
2575
2576 /**
2577  * panic_nand_write - [MTD Interface] NAND write with ECC
2578  * @mtd: MTD device structure
2579  * @to: offset to write to
2580  * @len: number of bytes to write
2581  * @retlen: pointer to variable to store the number of written bytes
2582  * @buf: the data to write
2583  *
2584  * NAND write with ECC. Used when performing writes in interrupt context, this
2585  * may for example be called by mtdoops when writing an oops while in panic.
2586  */
2587 static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2588                             size_t *retlen, const uint8_t *buf)
2589 {
2590         struct nand_chip *chip = mtd->priv;
2591         struct mtd_oob_ops ops;
2592         int ret;
2593
2594         /* Wait for the device to get ready */
2595         panic_nand_wait(mtd, chip, 400);
2596
2597         /* Grab the device */
2598         panic_nand_get_device(chip, mtd, FL_WRITING);
2599
2600         ops.len = len;
2601         ops.datbuf = (uint8_t *)buf;
2602         ops.oobbuf = NULL;
2603         ops.mode = MTD_OPS_PLACE_OOB;
2604
2605         ret = nand_do_write_ops(mtd, to, &ops);
2606
2607         *retlen = ops.retlen;
2608         return ret;
2609 }
2610
2611 /**
2612  * nand_write - [MTD Interface] NAND write with ECC
2613  * @mtd: MTD device structure
2614  * @to: offset to write to
2615  * @len: number of bytes to write
2616  * @retlen: pointer to variable to store the number of written bytes
2617  * @buf: the data to write
2618  *
2619  * NAND write with ECC.
2620  */
2621 static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2622                           size_t *retlen, const uint8_t *buf)
2623 {
2624         struct mtd_oob_ops ops;
2625         int ret;
2626
2627         nand_get_device(mtd, FL_WRITING);
2628         ops.len = len;
2629         ops.datbuf = (uint8_t *)buf;
2630         ops.oobbuf = NULL;
2631         ops.mode = MTD_OPS_PLACE_OOB;
2632         ret = nand_do_write_ops(mtd, to, &ops);
2633         *retlen = ops.retlen;
2634         nand_release_device(mtd);
2635         return ret;
2636 }
2637
2638 /**
2639  * nand_do_write_oob - [MTD Interface] NAND write out-of-band
2640  * @mtd: MTD device structure
2641  * @to: offset to write to
2642  * @ops: oob operation description structure
2643  *
2644  * NAND write out-of-band.
2645  */
2646 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2647                              struct mtd_oob_ops *ops)
2648 {
2649         int chipnr, page, status, len;
2650         struct nand_chip *chip = mtd->priv;
2651
2652         pr_debug("%s: to = 0x%08x, len = %i\n",
2653                          __func__, (unsigned int)to, (int)ops->ooblen);
2654
2655         if (ops->mode == MTD_OPS_AUTO_OOB)
2656                 len = chip->ecc.layout->oobavail;
2657         else
2658                 len = mtd->oobsize;
2659
2660         /* Do not allow write past end of page */
2661         if ((ops->ooboffs + ops->ooblen) > len) {
2662                 pr_debug("%s: attempt to write past end of page\n",
2663                                 __func__);
2664                 return -EINVAL;
2665         }
2666
2667         if (unlikely(ops->ooboffs >= len)) {
2668                 pr_debug("%s: attempt to start write outside oob\n",
2669                                 __func__);
2670                 return -EINVAL;
2671         }
2672
2673         /* Do not allow write past end of device */
2674         if (unlikely(to >= mtd->size ||
2675                      ops->ooboffs + ops->ooblen >
2676                         ((mtd->size >> chip->page_shift) -
2677                          (to >> chip->page_shift)) * len)) {
2678                 pr_debug("%s: attempt to write beyond end of device\n",
2679                                 __func__);
2680                 return -EINVAL;
2681         }
2682
2683         chipnr = (int)(to >> chip->chip_shift);
2684         chip->select_chip(mtd, chipnr);
2685
2686         /* Shift to get page */
2687         page = (int)(to >> chip->page_shift);
2688
2689         /*
2690          * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2691          * of my DiskOnChip 2000 test units) will clear the whole data page too
2692          * if we don't do this. I have no clue why, but I seem to have 'fixed'
2693          * it in the doc2000 driver in August 1999.  dwmw2.
2694          */
2695         chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2696
2697         /* Check, if it is write protected */
2698         if (nand_check_wp(mtd)) {
2699                 chip->select_chip(mtd, -1);
2700                 return -EROFS;
2701         }
2702
2703         /* Invalidate the page cache, if we write to the cached page */
2704         if (page == chip->pagebuf)
2705                 chip->pagebuf = -1;
2706
2707         nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
2708
2709         if (ops->mode == MTD_OPS_RAW)
2710                 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
2711         else
2712                 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2713
2714         chip->select_chip(mtd, -1);
2715
2716         if (status)
2717                 return status;
2718
2719         ops->oobretlen = ops->ooblen;
2720
2721         return 0;
2722 }
2723
2724 /**
2725  * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2726  * @mtd: MTD device structure
2727  * @to: offset to write to
2728  * @ops: oob operation description structure
2729  */
2730 static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2731                           struct mtd_oob_ops *ops)
2732 {
2733         int ret = -ENOTSUPP;
2734
2735         ops->retlen = 0;
2736
2737         /* Do not allow writes past end of device */
2738         if (ops->datbuf && (to + ops->len) > mtd->size) {
2739                 pr_debug("%s: attempt to write beyond end of device\n",
2740                                 __func__);
2741                 return -EINVAL;
2742         }
2743
2744         nand_get_device(mtd, FL_WRITING);
2745
2746         switch (ops->mode) {
2747         case MTD_OPS_PLACE_OOB:
2748         case MTD_OPS_AUTO_OOB:
2749         case MTD_OPS_RAW:
2750                 break;
2751
2752         default:
2753                 goto out;
2754         }
2755
2756         if (!ops->datbuf)
2757                 ret = nand_do_write_oob(mtd, to, ops);
2758         else
2759                 ret = nand_do_write_ops(mtd, to, ops);
2760
2761 out:
2762         nand_release_device(mtd);
2763         return ret;
2764 }
2765
2766 /**
2767  * single_erase_cmd - [GENERIC] NAND standard block erase command function
2768  * @mtd: MTD device structure
2769  * @page: the page address of the block which will be erased
2770  *
2771  * Standard erase command for NAND chips.
2772  */
2773 static void single_erase_cmd(struct mtd_info *mtd, int page)
2774 {
2775         struct nand_chip *chip = mtd->priv;
2776         /* Send commands to erase a block */
2777         chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2778         chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2779 }
2780
2781 /**
2782  * nand_erase - [MTD Interface] erase block(s)
2783  * @mtd: MTD device structure
2784  * @instr: erase instruction
2785  *
2786  * Erase one ore more blocks.
2787  */
2788 static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
2789 {
2790         return nand_erase_nand(mtd, instr, 0);
2791 }
2792
2793 /**
2794  * nand_erase_nand - [INTERN] erase block(s)
2795  * @mtd: MTD device structure
2796  * @instr: erase instruction
2797  * @allowbbt: allow erasing the bbt area
2798  *
2799  * Erase one ore more blocks.
2800  */
2801 int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2802                     int allowbbt)
2803 {
2804         int page, status, pages_per_block, ret, chipnr;
2805         struct nand_chip *chip = mtd->priv;
2806         loff_t len;
2807
2808         pr_debug("%s: start = 0x%012llx, len = %llu\n",
2809                         __func__, (unsigned long long)instr->addr,
2810                         (unsigned long long)instr->len);
2811
2812         if (check_offs_len(mtd, instr->addr, instr->len))
2813                 return -EINVAL;
2814
2815         /* Grab the lock and see if the device is available */
2816         nand_get_device(mtd, FL_ERASING);
2817
2818         /* Shift to get first page */
2819         page = (int)(instr->addr >> chip->page_shift);
2820         chipnr = (int)(instr->addr >> chip->chip_shift);
2821
2822         /* Calculate pages in each block */
2823         pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
2824
2825         /* Select the NAND device */
2826         chip->select_chip(mtd, chipnr);
2827
2828         /* Check, if it is write protected */
2829         if (nand_check_wp(mtd)) {
2830                 pr_debug("%s: device is write protected!\n",
2831                                 __func__);
2832                 instr->state = MTD_ERASE_FAILED;
2833                 goto erase_exit;
2834         }
2835
2836         /* Loop through the pages */
2837         len = instr->len;
2838
2839         instr->state = MTD_ERASING;
2840
2841         while (len) {
2842                 WATCHDOG_RESET();
2843
2844                 /* Check if we have a bad block, we do not erase bad blocks! */
2845                 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
2846                                         chip->page_shift, 0, allowbbt)) {
2847                         pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
2848                                     __func__, page);
2849                         instr->state = MTD_ERASE_FAILED;
2850                         goto erase_exit;
2851                 }
2852
2853                 /*
2854                  * Invalidate the page cache, if we erase the block which
2855                  * contains the current cached page.
2856                  */
2857                 if (page <= chip->pagebuf && chip->pagebuf <
2858                     (page + pages_per_block))
2859                         chip->pagebuf = -1;
2860
2861                 chip->erase_cmd(mtd, page & chip->pagemask);
2862
2863                 status = chip->waitfunc(mtd, chip);
2864
2865                 /*
2866                  * See if operation failed and additional status checks are
2867                  * available
2868                  */
2869                 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2870                         status = chip->errstat(mtd, chip, FL_ERASING,
2871                                                status, page);
2872
2873                 /* See if block erase succeeded */
2874                 if (status & NAND_STATUS_FAIL) {
2875                         pr_debug("%s: failed erase, page 0x%08x\n",
2876                                         __func__, page);
2877                         instr->state = MTD_ERASE_FAILED;
2878                         instr->fail_addr =
2879                                 ((loff_t)page << chip->page_shift);
2880                         goto erase_exit;
2881                 }
2882
2883                 /* Increment page address and decrement length */
2884                 len -= (1ULL << chip->phys_erase_shift);
2885                 page += pages_per_block;
2886
2887                 /* Check, if we cross a chip boundary */
2888                 if (len && !(page & chip->pagemask)) {
2889                         chipnr++;
2890                         chip->select_chip(mtd, -1);
2891                         chip->select_chip(mtd, chipnr);
2892                 }
2893         }
2894         instr->state = MTD_ERASE_DONE;
2895
2896 erase_exit:
2897
2898         ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2899
2900         /* Deselect and wake up anyone waiting on the device */
2901         chip->select_chip(mtd, -1);
2902         nand_release_device(mtd);
2903
2904         /* Do call back function */
2905         if (!ret)
2906                 mtd_erase_callback(instr);
2907
2908         /* Return more or less happy */
2909         return ret;
2910 }
2911
2912 /**
2913  * nand_sync - [MTD Interface] sync
2914  * @mtd: MTD device structure
2915  *
2916  * Sync is actually a wait for chip ready function.
2917  */
2918 static void nand_sync(struct mtd_info *mtd)
2919 {
2920         pr_debug("%s: called\n", __func__);
2921
2922         /* Grab the lock and see if the device is available */
2923         nand_get_device(mtd, FL_SYNCING);
2924         /* Release it and go back */
2925         nand_release_device(mtd);
2926 }
2927
2928 /**
2929  * nand_block_isbad - [MTD Interface] Check if block at offset is bad
2930  * @mtd: MTD device structure
2931  * @offs: offset relative to mtd start
2932  */
2933 static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
2934 {
2935         return nand_block_checkbad(mtd, offs, 1, 0);
2936 }
2937
2938 /**
2939  * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
2940  * @mtd: MTD device structure
2941  * @ofs: offset relative to mtd start
2942  */
2943 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2944 {
2945         int ret;
2946
2947         ret = nand_block_isbad(mtd, ofs);
2948         if (ret) {
2949                 /* If it was bad already, return success and do nothing */
2950                 if (ret > 0)
2951                         return 0;
2952                 return ret;
2953         }
2954
2955         return nand_block_markbad_lowlevel(mtd, ofs);
2956 }
2957
2958 /**
2959  * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand
2960  * @mtd: MTD device structure
2961  * @chip: nand chip info structure
2962  * @addr: feature address.
2963  * @subfeature_param: the subfeature parameters, a four bytes array.
2964  */
2965 static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
2966                         int addr, uint8_t *subfeature_param)
2967 {
2968         int status;
2969         int i;
2970
2971 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2972         if (!chip->onfi_version ||
2973             !(le16_to_cpu(chip->onfi_params.opt_cmd)
2974               & ONFI_OPT_CMD_SET_GET_FEATURES))
2975                 return -EINVAL;
2976 #endif
2977
2978         chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
2979         for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
2980                 chip->write_byte(mtd, subfeature_param[i]);
2981
2982         status = chip->waitfunc(mtd, chip);
2983         if (status & NAND_STATUS_FAIL)
2984                 return -EIO;
2985         return 0;
2986 }
2987
2988 /**
2989  * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand
2990  * @mtd: MTD device structure
2991  * @chip: nand chip info structure
2992  * @addr: feature address.
2993  * @subfeature_param: the subfeature parameters, a four bytes array.
2994  */
2995 static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
2996                         int addr, uint8_t *subfeature_param)
2997 {
2998         int i;
2999
3000 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3001         if (!chip->onfi_version ||
3002             !(le16_to_cpu(chip->onfi_params.opt_cmd)
3003               & ONFI_OPT_CMD_SET_GET_FEATURES))
3004                 return -EINVAL;
3005 #endif
3006
3007         /* clear the sub feature parameters */
3008         memset(subfeature_param, 0, ONFI_SUBFEATURE_PARAM_LEN);
3009
3010         chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
3011         for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3012                 *subfeature_param++ = chip->read_byte(mtd);
3013         return 0;
3014 }
3015
3016 #ifndef __UBOOT__
3017 /**
3018  * nand_suspend - [MTD Interface] Suspend the NAND flash
3019  * @mtd: MTD device structure
3020  */
3021 static int nand_suspend(struct mtd_info *mtd)
3022 {
3023         return nand_get_device(mtd, FL_PM_SUSPENDED);
3024 }
3025
3026 /**
3027  * nand_resume - [MTD Interface] Resume the NAND flash
3028  * @mtd: MTD device structure
3029  */
3030 static void nand_resume(struct mtd_info *mtd)
3031 {
3032         struct nand_chip *chip = mtd->priv;
3033
3034         if (chip->state == FL_PM_SUSPENDED)
3035                 nand_release_device(mtd);
3036         else
3037                 pr_err("%s called for a chip which is not in suspended state\n",
3038                         __func__);
3039 }
3040 #endif
3041
3042 /* Set default functions */
3043 static void nand_set_defaults(struct nand_chip *chip, int busw)
3044 {
3045         /* check for proper chip_delay setup, set 20us if not */
3046         if (!chip->chip_delay)
3047                 chip->chip_delay = 20;
3048
3049         /* check, if a user supplied command function given */
3050         if (chip->cmdfunc == NULL)
3051                 chip->cmdfunc = nand_command;
3052
3053         /* check, if a user supplied wait function given */
3054         if (chip->waitfunc == NULL)
3055                 chip->waitfunc = nand_wait;
3056
3057         if (!chip->select_chip)
3058                 chip->select_chip = nand_select_chip;
3059
3060         /* set for ONFI nand */
3061         if (!chip->onfi_set_features)
3062                 chip->onfi_set_features = nand_onfi_set_features;
3063         if (!chip->onfi_get_features)
3064                 chip->onfi_get_features = nand_onfi_get_features;
3065
3066         /* If called twice, pointers that depend on busw may need to be reset */
3067         if (!chip->read_byte || chip->read_byte == nand_read_byte)
3068                 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3069         if (!chip->read_word)
3070                 chip->read_word = nand_read_word;
3071         if (!chip->block_bad)
3072                 chip->block_bad = nand_block_bad;
3073         if (!chip->block_markbad)
3074                 chip->block_markbad = nand_default_block_markbad;
3075         if (!chip->write_buf || chip->write_buf == nand_write_buf)
3076                 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
3077         if (!chip->write_byte || chip->write_byte == nand_write_byte)
3078                 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3079         if (!chip->read_buf || chip->read_buf == nand_read_buf)
3080                 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
3081         if (!chip->scan_bbt)
3082                 chip->scan_bbt = nand_default_bbt;
3083
3084         if (!chip->controller) {
3085                 chip->controller = &chip->hwcontrol;
3086                 spin_lock_init(&chip->controller->lock);
3087                 init_waitqueue_head(&chip->controller->wq);
3088         }
3089
3090 }
3091
3092 /* Sanitize ONFI strings so we can safely print them */
3093 #ifndef __UBOOT__
3094 static void sanitize_string(uint8_t *s, size_t len)
3095 #else
3096 static void sanitize_string(char *s, size_t len)
3097 #endif
3098 {
3099         ssize_t i;
3100
3101         /* Null terminate */
3102         s[len - 1] = 0;
3103
3104         /* Remove non printable chars */
3105         for (i = 0; i < len - 1; i++) {
3106                 if (s[i] < ' ' || s[i] > 127)
3107                         s[i] = '?';
3108         }
3109
3110         /* Remove trailing spaces */
3111         strim(s);
3112 }
3113
3114 static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3115 {
3116         int i;
3117         while (len--) {
3118                 crc ^= *p++ << 8;
3119                 for (i = 0; i < 8; i++)
3120                         crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3121         }
3122
3123         return crc;
3124 }
3125
3126 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3127 /* Parse the Extended Parameter Page. */
3128 static int nand_flash_detect_ext_param_page(struct mtd_info *mtd,
3129                 struct nand_chip *chip, struct nand_onfi_params *p)
3130 {
3131         struct onfi_ext_param_page *ep;
3132         struct onfi_ext_section *s;
3133         struct onfi_ext_ecc_info *ecc;
3134         uint8_t *cursor;
3135         int ret = -EINVAL;
3136         int len;
3137         int i;
3138
3139         len = le16_to_cpu(p->ext_param_page_length) * 16;
3140         ep = kmalloc(len, GFP_KERNEL);
3141         if (!ep)
3142                 return -ENOMEM;
3143
3144         /* Send our own NAND_CMD_PARAM. */
3145         chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3146
3147         /* Use the Change Read Column command to skip the ONFI param pages. */
3148         chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3149                         sizeof(*p) * p->num_of_param_pages , -1);
3150
3151         /* Read out the Extended Parameter Page. */
3152         chip->read_buf(mtd, (uint8_t *)ep, len);
3153         if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3154                 != le16_to_cpu(ep->crc))) {
3155                 pr_debug("fail in the CRC.\n");
3156                 goto ext_out;
3157         }
3158
3159         /*
3160          * Check the signature.
3161          * Do not strictly follow the ONFI spec, maybe changed in future.
3162          */
3163 #ifndef __UBOOT__
3164         if (strncmp(ep->sig, "EPPS", 4)) {
3165 #else
3166         if (strncmp((char *)ep->sig, "EPPS", 4)) {
3167 #endif
3168                 pr_debug("The signature is invalid.\n");
3169                 goto ext_out;
3170         }
3171
3172         /* find the ECC section. */
3173         cursor = (uint8_t *)(ep + 1);
3174         for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3175                 s = ep->sections + i;
3176                 if (s->type == ONFI_SECTION_TYPE_2)
3177                         break;
3178                 cursor += s->length * 16;
3179         }
3180         if (i == ONFI_EXT_SECTION_MAX) {
3181                 pr_debug("We can not find the ECC section.\n");
3182                 goto ext_out;
3183         }
3184
3185         /* get the info we want. */
3186         ecc = (struct onfi_ext_ecc_info *)cursor;
3187
3188         if (!ecc->codeword_size) {
3189                 pr_debug("Invalid codeword size\n");
3190                 goto ext_out;
3191         }
3192
3193         chip->ecc_strength_ds = ecc->ecc_bits;
3194         chip->ecc_step_ds = 1 << ecc->codeword_size;
3195         ret = 0;
3196
3197 ext_out:
3198         kfree(ep);
3199         return ret;
3200 }
3201
3202 static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode)
3203 {
3204         struct nand_chip *chip = mtd->priv;
3205         uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode};
3206
3207         return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY,
3208                         feature);
3209 }
3210
3211 /*
3212  * Configure chip properties from Micron vendor-specific ONFI table
3213  */
3214 static void nand_onfi_detect_micron(struct nand_chip *chip,
3215                 struct nand_onfi_params *p)
3216 {
3217         struct nand_onfi_vendor_micron *micron = (void *)p->vendor;
3218
3219         if (le16_to_cpu(p->vendor_revision) < 1)
3220                 return;
3221
3222         chip->read_retries = micron->read_retry_options;
3223         chip->setup_read_retry = nand_setup_read_retry_micron;
3224 }
3225
3226 /*
3227  * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise.
3228  */
3229 static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
3230                                         int *busw)
3231 {
3232         struct nand_onfi_params *p = &chip->onfi_params;
3233         int i, j;
3234         int val;
3235
3236         /* Try ONFI for unknown chip or LP */
3237         chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3238         if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3239                 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3240                 return 0;
3241
3242         chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3243         for (i = 0; i < 3; i++) {
3244                 for (j = 0; j < sizeof(*p); j++)
3245                         ((uint8_t *)p)[j] = chip->read_byte(mtd);
3246                 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3247                                 le16_to_cpu(p->crc)) {
3248                         break;
3249                 }
3250         }
3251
3252         if (i == 3) {
3253                 pr_err("Could not find valid ONFI parameter page; aborting\n");
3254                 return 0;
3255         }
3256
3257         /* Check version */
3258         val = le16_to_cpu(p->revision);
3259         if (val & (1 << 5))
3260                 chip->onfi_version = 23;
3261         else if (val & (1 << 4))
3262                 chip->onfi_version = 22;
3263         else if (val & (1 << 3))
3264                 chip->onfi_version = 21;
3265         else if (val & (1 << 2))
3266                 chip->onfi_version = 20;
3267         else if (val & (1 << 1))
3268                 chip->onfi_version = 10;
3269
3270         if (!chip->onfi_version) {
3271                 pr_info("unsupported ONFI version: %d\n", val);
3272                 return 0;
3273         }
3274
3275         sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3276         sanitize_string(p->model, sizeof(p->model));
3277         if (!mtd->name)
3278                 mtd->name = p->model;
3279
3280         mtd->writesize = le32_to_cpu(p->byte_per_page);
3281
3282         /*
3283          * pages_per_block and blocks_per_lun may not be a power-of-2 size
3284          * (don't ask me who thought of this...). MTD assumes that these
3285          * dimensions will be power-of-2, so just truncate the remaining area.
3286          */
3287         mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3288         mtd->erasesize *= mtd->writesize;
3289
3290         mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3291
3292         /* See erasesize comment */
3293         chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3294         chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3295         chip->bits_per_cell = p->bits_per_cell;
3296
3297         if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
3298                 *busw = NAND_BUSWIDTH_16;
3299         else
3300                 *busw = 0;
3301
3302         if (p->ecc_bits != 0xff) {
3303                 chip->ecc_strength_ds = p->ecc_bits;
3304                 chip->ecc_step_ds = 512;
3305         } else if (chip->onfi_version >= 21 &&
3306                 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3307
3308                 /*
3309                  * The nand_flash_detect_ext_param_page() uses the
3310                  * Change Read Column command which maybe not supported
3311                  * by the chip->cmdfunc. So try to update the chip->cmdfunc
3312                  * now. We do not replace user supplied command function.
3313                  */
3314                 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3315                         chip->cmdfunc = nand_command_lp;
3316
3317                 /* The Extended Parameter Page is supported since ONFI 2.1. */
3318                 if (nand_flash_detect_ext_param_page(mtd, chip, p))
3319                         pr_warn("Failed to detect ONFI extended param page\n");
3320         } else {
3321                 pr_warn("Could not retrieve ONFI ECC requirements\n");
3322         }
3323
3324         if (p->jedec_id == NAND_MFR_MICRON)
3325                 nand_onfi_detect_micron(chip, p);
3326
3327         return 1;
3328 }
3329 #else
3330 static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
3331                                         int *busw)
3332 {
3333         return 0;
3334 }
3335 #endif
3336
3337 /*
3338  * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise.
3339  */
3340 static int nand_flash_detect_jedec(struct mtd_info *mtd, struct nand_chip *chip,
3341                                         int *busw)
3342 {
3343         struct nand_jedec_params *p = &chip->jedec_params;
3344         struct jedec_ecc_info *ecc;
3345         int val;
3346         int i, j;
3347
3348         /* Try JEDEC for unknown chip or LP */
3349         chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3350         if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3351                 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3352                 chip->read_byte(mtd) != 'C')
3353                 return 0;
3354
3355         chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3356         for (i = 0; i < 3; i++) {
3357                 for (j = 0; j < sizeof(*p); j++)
3358                         ((uint8_t *)p)[j] = chip->read_byte(mtd);
3359
3360                 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3361                                 le16_to_cpu(p->crc))
3362                         break;
3363         }
3364
3365         if (i == 3) {
3366                 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3367                 return 0;
3368         }
3369
3370         /* Check version */
3371         val = le16_to_cpu(p->revision);
3372         if (val & (1 << 2))
3373                 chip->jedec_version = 10;
3374         else if (val & (1 << 1))
3375                 chip->jedec_version = 1; /* vendor specific version */
3376
3377         if (!chip->jedec_version) {
3378                 pr_info("unsupported JEDEC version: %d\n", val);
3379                 return 0;
3380         }
3381
3382         sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3383         sanitize_string(p->model, sizeof(p->model));
3384         if (!mtd->name)
3385                 mtd->name = p->model;
3386
3387         mtd->writesize = le32_to_cpu(p->byte_per_page);
3388
3389         /* Please reference to the comment for nand_flash_detect_onfi. */
3390         mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3391         mtd->erasesize *= mtd->writesize;
3392
3393         mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3394
3395         /* Please reference to the comment for nand_flash_detect_onfi. */
3396         chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3397         chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3398         chip->bits_per_cell = p->bits_per_cell;
3399
3400         if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3401                 *busw = NAND_BUSWIDTH_16;
3402         else
3403                 *busw = 0;
3404
3405         /* ECC info */
3406         ecc = &p->ecc_info[0];
3407
3408         if (ecc->codeword_size >= 9) {
3409                 chip->ecc_strength_ds = ecc->ecc_bits;
3410                 chip->ecc_step_ds = 1 << ecc->codeword_size;
3411         } else {
3412                 pr_warn("Invalid codeword size\n");
3413         }
3414
3415         return 1;
3416 }
3417
3418 /*
3419  * nand_id_has_period - Check if an ID string has a given wraparound period
3420  * @id_data: the ID string
3421  * @arrlen: the length of the @id_data array
3422  * @period: the period of repitition
3423  *
3424  * Check if an ID string is repeated within a given sequence of bytes at
3425  * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a
3426  * period of 3). This is a helper function for nand_id_len(). Returns non-zero
3427  * if the repetition has a period of @period; otherwise, returns zero.
3428  */
3429 static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3430 {
3431         int i, j;
3432         for (i = 0; i < period; i++)
3433                 for (j = i + period; j < arrlen; j += period)
3434                         if (id_data[i] != id_data[j])
3435                                 return 0;
3436         return 1;
3437 }
3438
3439 /*
3440  * nand_id_len - Get the length of an ID string returned by CMD_READID
3441  * @id_data: the ID string
3442  * @arrlen: the length of the @id_data array
3443
3444  * Returns the length of the ID string, according to known wraparound/trailing
3445  * zero patterns. If no pattern exists, returns the length of the array.
3446  */
3447 static int nand_id_len(u8 *id_data, int arrlen)
3448 {
3449         int last_nonzero, period;
3450
3451         /* Find last non-zero byte */
3452         for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3453                 if (id_data[last_nonzero])
3454                         break;
3455
3456         /* All zeros */
3457         if (last_nonzero < 0)
3458                 return 0;
3459
3460         /* Calculate wraparound period */
3461         for (period = 1; period < arrlen; period++)
3462                 if (nand_id_has_period(id_data, arrlen, period))
3463                         break;
3464
3465         /* There's a repeated pattern */
3466         if (period < arrlen)
3467                 return period;
3468
3469         /* There are trailing zeros */
3470         if (last_nonzero < arrlen - 1)
3471                 return last_nonzero + 1;
3472
3473         /* No pattern detected */
3474         return arrlen;
3475 }
3476
3477 /* Extract the bits of per cell from the 3rd byte of the extended ID */
3478 static int nand_get_bits_per_cell(u8 cellinfo)
3479 {
3480         int bits;
3481
3482         bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3483         bits >>= NAND_CI_CELLTYPE_SHIFT;
3484         return bits + 1;
3485 }
3486
3487 /*
3488  * Many new NAND share similar device ID codes, which represent the size of the
3489  * chip. The rest of the parameters must be decoded according to generic or
3490  * manufacturer-specific "extended ID" decoding patterns.
3491  */
3492 static void nand_decode_ext_id(struct mtd_info *mtd, struct nand_chip *chip,
3493                                 u8 id_data[8], int *busw)
3494 {
3495         int extid, id_len;
3496         /* The 3rd id byte holds MLC / multichip data */
3497         chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3498         /* The 4th id byte is the important one */
3499         extid = id_data[3];
3500
3501         id_len = nand_id_len(id_data, 8);
3502
3503         /*
3504          * Field definitions are in the following datasheets:
3505          * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
3506          * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44)
3507          * Hynix MLC   (6 byte ID): Hynix H27UBG8T2B (p.22)
3508          *
3509          * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung
3510          * ID to decide what to do.
3511          */
3512         if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG &&
3513                         !nand_is_slc(chip) && id_data[5] != 0x00) {
3514                 /* Calc pagesize */
3515                 mtd->writesize = 2048 << (extid & 0x03);
3516                 extid >>= 2;
3517                 /* Calc oobsize */
3518                 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3519                 case 1:
3520                         mtd->oobsize = 128;
3521                         break;
3522                 case 2:
3523                         mtd->oobsize = 218;
3524                         break;
3525                 case 3:
3526                         mtd->oobsize = 400;
3527                         break;
3528                 case 4:
3529                         mtd->oobsize = 436;
3530                         break;
3531                 case 5:
3532                         mtd->oobsize = 512;
3533                         break;
3534                 case 6:
3535                         mtd->oobsize = 640;
3536                         break;
3537                 case 7:
3538                 default: /* Other cases are "reserved" (unknown) */
3539                         mtd->oobsize = 1024;
3540                         break;
3541                 }
3542                 extid >>= 2;
3543                 /* Calc blocksize */
3544                 mtd->erasesize = (128 * 1024) <<
3545                         (((extid >> 1) & 0x04) | (extid & 0x03));
3546                 *busw = 0;
3547         } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX &&
3548                         !nand_is_slc(chip)) {
3549                 unsigned int tmp;
3550
3551                 /* Calc pagesize */
3552                 mtd->writesize = 2048 << (extid & 0x03);
3553                 extid >>= 2;
3554                 /* Calc oobsize */
3555                 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3556                 case 0:
3557                         mtd->oobsize = 128;
3558                         break;
3559                 case 1:
3560                         mtd->oobsize = 224;
3561                         break;
3562                 case 2:
3563                         mtd->oobsize = 448;
3564                         break;
3565                 case 3:
3566                         mtd->oobsize = 64;
3567                         break;
3568                 case 4:
3569                         mtd->oobsize = 32;
3570                         break;
3571                 case 5:
3572                         mtd->oobsize = 16;
3573                         break;
3574                 default:
3575                         mtd->oobsize = 640;
3576                         break;
3577                 }
3578                 extid >>= 2;
3579                 /* Calc blocksize */
3580                 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3581                 if (tmp < 0x03)
3582                         mtd->erasesize = (128 * 1024) << tmp;
3583                 else if (tmp == 0x03)
3584                         mtd->erasesize = 768 * 1024;
3585                 else
3586                         mtd->erasesize = (64 * 1024) << tmp;
3587                 *busw = 0;
3588         } else {
3589                 /* Calc pagesize */
3590                 mtd->writesize = 1024 << (extid & 0x03);
3591                 extid >>= 2;
3592                 /* Calc oobsize */
3593                 mtd->oobsize = (8 << (extid & 0x01)) *
3594                         (mtd->writesize >> 9);
3595                 extid >>= 2;
3596                 /* Calc blocksize. Blocksize is multiples of 64KiB */
3597                 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3598                 extid >>= 2;
3599                 /* Get buswidth information */
3600                 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
3601
3602                 /*
3603                  * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
3604                  * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
3605                  * follows:
3606                  * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
3607                  *                         110b -> 24nm
3608                  * - ID byte 5, bit[7]:    1 -> BENAND, 0 -> raw SLC
3609                  */
3610                 if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA &&
3611                                 nand_is_slc(chip) &&
3612                                 (id_data[5] & 0x7) == 0x6 /* 24nm */ &&
3613                                 !(id_data[4] & 0x80) /* !BENAND */) {
3614                         mtd->oobsize = 32 * mtd->writesize >> 9;
3615                 }
3616
3617         }
3618 }
3619
3620 /*
3621  * Old devices have chip data hardcoded in the device ID table. nand_decode_id
3622  * decodes a matching ID table entry and assigns the MTD size parameters for
3623  * the chip.
3624  */
3625 static void nand_decode_id(struct mtd_info *mtd, struct nand_chip *chip,
3626                                 struct nand_flash_dev *type, u8 id_data[8],
3627                                 int *busw)
3628 {
3629         int maf_id = id_data[0];
3630
3631         mtd->erasesize = type->erasesize;
3632         mtd->writesize = type->pagesize;
3633         mtd->oobsize = mtd->writesize / 32;
3634         *busw = type->options & NAND_BUSWIDTH_16;
3635
3636         /* All legacy ID NAND are small-page, SLC */
3637         chip->bits_per_cell = 1;
3638
3639         /*
3640          * Check for Spansion/AMD ID + repeating 5th, 6th byte since
3641          * some Spansion chips have erasesize that conflicts with size
3642          * listed in nand_ids table.
3643          * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
3644          */
3645         if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3646                         && id_data[6] == 0x00 && id_data[7] == 0x00
3647                         && mtd->writesize == 512) {
3648                 mtd->erasesize = 128 * 1024;
3649                 mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
3650         }
3651 }
3652
3653 /*
3654  * Set the bad block marker/indicator (BBM/BBI) patterns according to some
3655  * heuristic patterns using various detected parameters (e.g., manufacturer,
3656  * page size, cell-type information).
3657  */
3658 static void nand_decode_bbm_options(struct mtd_info *mtd,
3659                                     struct nand_chip *chip, u8 id_data[8])
3660 {
3661         int maf_id = id_data[0];
3662
3663         /* Set the bad block position */
3664         if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3665                 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3666         else
3667                 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3668
3669         /*
3670          * Bad block marker is stored in the last page of each block on Samsung
3671          * and Hynix MLC devices; stored in first two pages of each block on
3672          * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba,
3673          * AMD/Spansion, and Macronix.  All others scan only the first page.
3674          */
3675         if (!nand_is_slc(chip) &&
3676                         (maf_id == NAND_MFR_SAMSUNG ||
3677                          maf_id == NAND_MFR_HYNIX))
3678                 chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
3679         else if ((nand_is_slc(chip) &&
3680                                 (maf_id == NAND_MFR_SAMSUNG ||
3681                                  maf_id == NAND_MFR_HYNIX ||
3682                                  maf_id == NAND_MFR_TOSHIBA ||
3683                                  maf_id == NAND_MFR_AMD ||
3684                                  maf_id == NAND_MFR_MACRONIX)) ||
3685                         (mtd->writesize == 2048 &&
3686                          maf_id == NAND_MFR_MICRON))
3687                 chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
3688 }
3689
3690 static inline bool is_full_id_nand(struct nand_flash_dev *type)
3691 {
3692         return type->id_len;
3693 }
3694
3695 static bool find_full_id_nand(struct mtd_info *mtd, struct nand_chip *chip,
3696                    struct nand_flash_dev *type, u8 *id_data, int *busw)
3697 {
3698 #ifndef __UBOOT__
3699         if (!strncmp(type->id, id_data, type->id_len)) {
3700 #else
3701         if (!strncmp((char *)type->id, (char *)id_data, type->id_len)) {
3702 #endif
3703                 mtd->writesize = type->pagesize;
3704                 mtd->erasesize = type->erasesize;
3705                 mtd->oobsize = type->oobsize;
3706
3707                 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3708                 chip->chipsize = (uint64_t)type->chipsize << 20;
3709                 chip->options |= type->options;
3710                 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3711                 chip->ecc_step_ds = NAND_ECC_STEP(type);
3712
3713                 *busw = type->options & NAND_BUSWIDTH_16;
3714
3715                 if (!mtd->name)
3716                         mtd->name = type->name;
3717
3718                 return true;
3719         }
3720         return false;
3721 }
3722
3723 /*
3724  * Get the flash and manufacturer id and lookup if the type is supported.
3725  */
3726 static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
3727                                                   struct nand_chip *chip,
3728                                                   int *maf_id, int *dev_id,
3729                                                   struct nand_flash_dev *type)
3730 {
3731         int busw;
3732         int i, maf_idx;
3733         u8 id_data[8];
3734
3735         /* Select the device */
3736         chip->select_chip(mtd, 0);
3737
3738         /*
3739          * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
3740          * after power-up.
3741          */
3742         chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3743
3744         /* Send the command for reading device ID */
3745         chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3746
3747         /* Read manufacturer and device IDs */
3748         *maf_id = chip->read_byte(mtd);
3749         *dev_id = chip->read_byte(mtd);
3750
3751         /*
3752          * Try again to make sure, as some systems the bus-hold or other
3753          * interface concerns can cause random data which looks like a
3754          * possibly credible NAND flash to appear. If the two results do
3755          * not match, ignore the device completely.
3756          */
3757
3758         chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3759
3760         /* Read entire ID string */
3761         for (i = 0; i < 8; i++)
3762                 id_data[i] = chip->read_byte(mtd);
3763
3764         if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
3765                 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
3766                         *maf_id, *dev_id, id_data[0], id_data[1]);
3767                 return ERR_PTR(-ENODEV);
3768         }
3769
3770         if (!type)
3771                 type = nand_flash_ids;
3772
3773         for (; type->name != NULL; type++) {
3774                 if (is_full_id_nand(type)) {
3775                         if (find_full_id_nand(mtd, chip, type, id_data, &busw))
3776                                 goto ident_done;
3777                 } else if (*dev_id == type->dev_id) {
3778                                 break;
3779                 }
3780         }
3781
3782         chip->onfi_version = 0;
3783         if (!type->name || !type->pagesize) {
3784                 /* Check is chip is ONFI compliant */
3785                 if (nand_flash_detect_onfi(mtd, chip, &busw))
3786                         goto ident_done;
3787
3788                 /* Check if the chip is JEDEC compliant */
3789                 if (nand_flash_detect_jedec(mtd, chip, &busw))
3790                         goto ident_done;
3791         }
3792
3793         if (!type->name)
3794                 return ERR_PTR(-ENODEV);
3795
3796         if (!mtd->name)
3797                 mtd->name = type->name;
3798
3799         chip->chipsize = (uint64_t)type->chipsize << 20;
3800
3801         if (!type->pagesize && chip->init_size) {
3802                 /* Set the pagesize, oobsize, erasesize by the driver */
3803                 busw = chip->init_size(mtd, chip, id_data);
3804         } else if (!type->pagesize) {
3805                 /* Decode parameters from extended ID */
3806                 nand_decode_ext_id(mtd, chip, id_data, &busw);
3807         } else {
3808                 nand_decode_id(mtd, chip, type, id_data, &busw);
3809         }
3810         /* Get chip options */
3811         chip->options |= type->options;
3812
3813         /*
3814          * Check if chip is not a Samsung device. Do not clear the
3815          * options for chips which do not have an extended id.
3816          */
3817         if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
3818                 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
3819 ident_done:
3820
3821         /* Try to identify manufacturer */
3822         for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
3823                 if (nand_manuf_ids[maf_idx].id == *maf_id)
3824                         break;
3825         }
3826
3827         if (chip->options & NAND_BUSWIDTH_AUTO) {
3828                 WARN_ON(chip->options & NAND_BUSWIDTH_16);
3829                 chip->options |= busw;
3830                 nand_set_defaults(chip, busw);
3831         } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
3832                 /*
3833                  * Check, if buswidth is correct. Hardware drivers should set
3834                  * chip correct!
3835                  */
3836                 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3837                         *maf_id, *dev_id);
3838                 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name);
3839                 pr_warn("bus width %d instead %d bit\n",
3840                            (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
3841                            busw ? 16 : 8);
3842                 return ERR_PTR(-EINVAL);
3843         }
3844
3845         nand_decode_bbm_options(mtd, chip, id_data);
3846
3847         /* Calculate the address shift from the page size */
3848         chip->page_shift = ffs(mtd->writesize) - 1;
3849         /* Convert chipsize to number of pages per chip -1 */
3850         chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
3851
3852         chip->bbt_erase_shift = chip->phys_erase_shift =
3853                 ffs(mtd->erasesize) - 1;
3854         if (chip->chipsize & 0xffffffff)
3855                 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
3856         else {
3857                 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
3858                 chip->chip_shift += 32 - 1;
3859         }
3860
3861         chip->badblockbits = 8;
3862         chip->erase_cmd = single_erase_cmd;
3863
3864         /* Do not replace user supplied command function! */
3865         if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3866                 chip->cmdfunc = nand_command_lp;
3867
3868         pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
3869                 *maf_id, *dev_id);
3870
3871 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
3872         if (chip->onfi_version)
3873                 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3874                                 chip->onfi_params.model);
3875         else if (chip->jedec_version)
3876                 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3877                                 chip->jedec_params.model);
3878         else
3879                 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3880                                 type->name);
3881 #else
3882         if (chip->jedec_version)
3883                 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3884                                 chip->jedec_params.model);
3885         else
3886                 pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3887                                 type->name);
3888
3889         pr_info("%s %s\n", nand_manuf_ids[maf_idx].name,
3890                 type->name);
3891 #endif
3892
3893         pr_info("%dMiB, %s, page size: %d, OOB size: %d\n",
3894                 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
3895                 mtd->writesize, mtd->oobsize);
3896         return type;
3897 }
3898
3899 /**
3900  * nand_scan_ident - [NAND Interface] Scan for the NAND device
3901  * @mtd: MTD device structure
3902  * @maxchips: number of chips to scan for
3903  * @table: alternative NAND ID table
3904  *
3905  * This is the first phase of the normal nand_scan() function. It reads the
3906  * flash ID and sets up MTD fields accordingly.
3907  *
3908  * The mtd->owner field must be set to the module of the caller.
3909  */
3910 int nand_scan_ident(struct mtd_info *mtd, int maxchips,
3911                     struct nand_flash_dev *table)
3912 {
3913         int i, nand_maf_id, nand_dev_id;
3914         struct nand_chip *chip = mtd->priv;
3915         struct nand_flash_dev *type;
3916
3917         /* Set the default functions */
3918         nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
3919
3920         /* Read the flash type */
3921         type = nand_get_flash_type(mtd, chip, &nand_maf_id,
3922                                    &nand_dev_id, table);
3923
3924         if (IS_ERR(type)) {
3925                 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
3926                         pr_warn("No NAND device found\n");
3927                 chip->select_chip(mtd, -1);
3928                 return PTR_ERR(type);
3929         }
3930
3931         chip->select_chip(mtd, -1);
3932
3933         /* Check for a chip array */
3934         for (i = 1; i < maxchips; i++) {
3935                 chip->select_chip(mtd, i);
3936                 /* See comment in nand_get_flash_type for reset */
3937                 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
3938                 /* Send the command for reading device ID */
3939                 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
3940                 /* Read manufacturer and device IDs */
3941                 if (nand_maf_id != chip->read_byte(mtd) ||
3942                     nand_dev_id != chip->read_byte(mtd)) {
3943                         chip->select_chip(mtd, -1);
3944                         break;
3945                 }
3946                 chip->select_chip(mtd, -1);
3947         }
3948
3949 #ifdef DEBUG
3950         if (i > 1)
3951                 pr_info("%d chips detected\n", i);
3952 #endif
3953
3954         /* Store the number of chips and calc total size for mtd */
3955         chip->numchips = i;
3956         mtd->size = i * chip->chipsize;
3957
3958         return 0;
3959 }
3960 EXPORT_SYMBOL(nand_scan_ident);
3961
3962
3963 /**
3964  * nand_scan_tail - [NAND Interface] Scan for the NAND device
3965  * @mtd: MTD device structure
3966  *
3967  * This is the second phase of the normal nand_scan() function. It fills out
3968  * all the uninitialized function pointers with the defaults and scans for a
3969  * bad block table if appropriate.
3970  */
3971 int nand_scan_tail(struct mtd_info *mtd)
3972 {
3973         int i;
3974         struct nand_chip *chip = mtd->priv;
3975         struct nand_ecc_ctrl *ecc = &chip->ecc;
3976         struct nand_buffers *nbuf;
3977
3978         /* New bad blocks should be marked in OOB, flash-based BBT, or both */
3979         BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3980                         !(chip->bbt_options & NAND_BBT_USE_FLASH));
3981
3982         if (!(chip->options & NAND_OWN_BUFFERS)) {
3983 #ifndef __UBOOT__
3984                 nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
3985                                 + mtd->oobsize * 3, GFP_KERNEL);
3986                 if (!nbuf)
3987                         return -ENOMEM;
3988                 nbuf->ecccalc = (uint8_t *)(nbuf + 1);
3989                 nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
3990                 nbuf->databuf = nbuf->ecccode + mtd->oobsize;
3991 #else
3992                 nbuf = kzalloc(sizeof(struct nand_buffers), GFP_KERNEL);
3993 #endif
3994
3995                 chip->buffers = nbuf;
3996         } else {
3997                 if (!chip->buffers)
3998                         return -ENOMEM;
3999         }
4000
4001         /* Set the internal oob buffer location, just after the page data */
4002         chip->oob_poi = chip->buffers->databuf + mtd->writesize;
4003
4004         /*
4005          * If no default placement scheme is given, select an appropriate one.
4006          */
4007         if (!ecc->layout && (ecc->mode != NAND_ECC_SOFT_BCH)) {
4008                 switch (mtd->oobsize) {
4009                 case 8:
4010                         ecc->layout = &nand_oob_8;
4011                         break;
4012                 case 16:
4013                         ecc->layout = &nand_oob_16;
4014                         break;
4015                 case 64:
4016                         ecc->layout = &nand_oob_64;
4017                         break;
4018                 case 128:
4019                         ecc->layout = &nand_oob_128;
4020                         break;
4021                 default:
4022                         pr_warn("No oob scheme defined for oobsize %d\n",
4023                                    mtd->oobsize);
4024                         BUG();
4025                 }
4026         }
4027
4028         if (!chip->write_page)
4029                 chip->write_page = nand_write_page;
4030
4031         /*
4032          * Check ECC mode, default to software if 3byte/512byte hardware ECC is
4033          * selected and we have 256 byte pagesize fallback to software ECC
4034          */
4035
4036         switch (ecc->mode) {
4037         case NAND_ECC_HW_OOB_FIRST:
4038                 /* Similar to NAND_ECC_HW, but a separate read_page handle */
4039                 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
4040                         pr_warn("No ECC functions supplied; "
4041                                    "hardware ECC not possible\n");
4042                         BUG();
4043                 }
4044                 if (!ecc->read_page)
4045                         ecc->read_page = nand_read_page_hwecc_oob_first;
4046
4047         case NAND_ECC_HW:
4048                 /* Use standard hwecc read page function? */
4049                 if (!ecc->read_page)
4050                         ecc->read_page = nand_read_page_hwecc;
4051                 if (!ecc->write_page)
4052                         ecc->write_page = nand_write_page_hwecc;
4053                 if (!ecc->read_page_raw)
4054                         ecc->read_page_raw = nand_read_page_raw;
4055                 if (!ecc->write_page_raw)
4056                         ecc->write_page_raw = nand_write_page_raw;
4057                 if (!ecc->read_oob)
4058                         ecc->read_oob = nand_read_oob_std;
4059                 if (!ecc->write_oob)
4060                         ecc->write_oob = nand_write_oob_std;
4061                 if (!ecc->read_subpage)
4062                         ecc->read_subpage = nand_read_subpage;
4063                 if (!ecc->write_subpage)
4064                         ecc->write_subpage = nand_write_subpage_hwecc;
4065
4066         case NAND_ECC_HW_SYNDROME:
4067                 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4068                     (!ecc->read_page ||
4069                      ecc->read_page == nand_read_page_hwecc ||
4070                      !ecc->write_page ||
4071                      ecc->write_page == nand_write_page_hwecc)) {
4072                         pr_warn("No ECC functions supplied; "
4073                                    "hardware ECC not possible\n");
4074                         BUG();
4075                 }
4076                 /* Use standard syndrome read/write page function? */
4077                 if (!ecc->read_page)
4078                         ecc->read_page = nand_read_page_syndrome;
4079                 if (!ecc->write_page)
4080                         ecc->write_page = nand_write_page_syndrome;
4081                 if (!ecc->read_page_raw)
4082                         ecc->read_page_raw = nand_read_page_raw_syndrome;
4083                 if (!ecc->write_page_raw)
4084                         ecc->write_page_raw = nand_write_page_raw_syndrome;
4085                 if (!ecc->read_oob)
4086                         ecc->read_oob = nand_read_oob_syndrome;
4087                 if (!ecc->write_oob)
4088                         ecc->write_oob = nand_write_oob_syndrome;
4089
4090                 if (mtd->writesize >= ecc->size) {
4091                         if (!ecc->strength) {
4092                                 pr_warn("Driver must set ecc.strength when using hardware ECC\n");
4093                                 BUG();
4094                         }
4095                         break;
4096                 }
4097                 pr_warn("%d byte HW ECC not possible on "
4098                            "%d byte page size, fallback to SW ECC\n",
4099                            ecc->size, mtd->writesize);
4100                 ecc->mode = NAND_ECC_SOFT;
4101
4102         case NAND_ECC_SOFT:
4103                 ecc->calculate = nand_calculate_ecc;
4104                 ecc->correct = nand_correct_data;
4105                 ecc->read_page = nand_read_page_swecc;
4106                 ecc->read_subpage = nand_read_subpage;
4107                 ecc->write_page = nand_write_page_swecc;
4108                 ecc->read_page_raw = nand_read_page_raw;
4109                 ecc->write_page_raw = nand_write_page_raw;
4110                 ecc->read_oob = nand_read_oob_std;
4111                 ecc->write_oob = nand_write_oob_std;
4112                 if (!ecc->size)
4113                         ecc->size = 256;
4114                 ecc->bytes = 3;
4115                 ecc->strength = 1;
4116                 break;
4117
4118         case NAND_ECC_SOFT_BCH:
4119                 if (!mtd_nand_has_bch()) {
4120                         pr_warn("CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4121                         BUG();
4122                 }
4123                 ecc->calculate = nand_bch_calculate_ecc;
4124                 ecc->correct = nand_bch_correct_data;
4125                 ecc->read_page = nand_read_page_swecc;
4126                 ecc->read_subpage = nand_read_subpage;
4127                 ecc->write_page = nand_write_page_swecc;
4128                 ecc->read_page_raw = nand_read_page_raw;
4129                 ecc->write_page_raw = nand_write_page_raw;
4130                 ecc->read_oob = nand_read_oob_std;
4131                 ecc->write_oob = nand_write_oob_std;
4132                 /*
4133                  * Board driver should supply ecc.size and ecc.bytes values to
4134                  * select how many bits are correctable; see nand_bch_init()
4135                  * for details. Otherwise, default to 4 bits for large page
4136                  * devices.
4137                  */
4138                 if (!ecc->size && (mtd->oobsize >= 64)) {
4139                         ecc->size = 512;
4140                         ecc->bytes = 7;
4141                 }
4142                 ecc->priv = nand_bch_init(mtd, ecc->size, ecc->bytes,
4143                                                &ecc->layout);
4144                 if (!ecc->priv) {
4145                         pr_warn("BCH ECC initialization failed!\n");
4146                         BUG();
4147                 }
4148                 ecc->strength = ecc->bytes * 8 / fls(8 * ecc->size);
4149                 break;
4150
4151         case NAND_ECC_NONE:
4152                 pr_warn("NAND_ECC_NONE selected by board driver. "
4153                            "This is not recommended!\n");
4154                 ecc->read_page = nand_read_page_raw;
4155                 ecc->write_page = nand_write_page_raw;
4156                 ecc->read_oob = nand_read_oob_std;
4157                 ecc->read_page_raw = nand_read_page_raw;
4158                 ecc->write_page_raw = nand_write_page_raw;
4159                 ecc->write_oob = nand_write_oob_std;
4160                 ecc->size = mtd->writesize;
4161                 ecc->bytes = 0;
4162                 ecc->strength = 0;
4163                 break;
4164
4165         default:
4166                 pr_warn("Invalid NAND_ECC_MODE %d\n", ecc->mode);
4167                 BUG();
4168         }
4169
4170         /* For many systems, the standard OOB write also works for raw */
4171         if (!ecc->read_oob_raw)
4172                 ecc->read_oob_raw = ecc->read_oob;
4173         if (!ecc->write_oob_raw)
4174                 ecc->write_oob_raw = ecc->write_oob;
4175
4176         /*
4177          * The number of bytes available for a client to place data into
4178          * the out of band area.
4179          */
4180         ecc->layout->oobavail = 0;
4181         for (i = 0; ecc->layout->oobfree[i].length
4182                         && i < ARRAY_SIZE(ecc->layout->oobfree); i++)
4183                 ecc->layout->oobavail += ecc->layout->oobfree[i].length;
4184         mtd->oobavail = ecc->layout->oobavail;
4185
4186         /*
4187          * Set the number of read / write steps for one page depending on ECC
4188          * mode.
4189          */
4190         ecc->steps = mtd->writesize / ecc->size;
4191         if (ecc->steps * ecc->size != mtd->writesize) {
4192                 pr_warn("Invalid ECC parameters\n");
4193                 pr_warn("steps=%d size=%d writesize=%d\n",
4194                         chip->ecc.steps, chip->ecc.size, mtd->writesize);
4195                 BUG();
4196         }
4197         ecc->total = ecc->steps * ecc->bytes;
4198
4199         /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */
4200         if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
4201                 switch (ecc->steps) {
4202                 case 2:
4203                         mtd->subpage_sft = 1;
4204                         break;
4205                 case 4:
4206                 case 8:
4207                 case 16:
4208                         mtd->subpage_sft = 2;
4209                         break;
4210                 }
4211         }
4212         chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
4213
4214         /* Initialize state */
4215         chip->state = FL_READY;
4216
4217         /* Invalidate the pagebuffer reference */
4218         chip->pagebuf = -1;
4219
4220         /* Large page NAND with SOFT_ECC should support subpage reads */
4221         if ((ecc->mode == NAND_ECC_SOFT) && (chip->page_shift > 9))
4222                 chip->options |= NAND_SUBPAGE_READ;
4223
4224         /* Fill in remaining MTD driver data */
4225         mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
4226         mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
4227                                                 MTD_CAP_NANDFLASH;
4228         mtd->_erase = nand_erase;
4229 #ifndef __UBOOT__
4230         mtd->_point = NULL;
4231         mtd->_unpoint = NULL;
4232 #endif
4233         mtd->_read = nand_read;
4234         mtd->_write = nand_write;
4235         mtd->_panic_write = panic_nand_write;
4236         mtd->_read_oob = nand_read_oob;
4237         mtd->_write_oob = nand_write_oob;
4238         mtd->_sync = nand_sync;
4239         mtd->_lock = NULL;
4240         mtd->_unlock = NULL;
4241 #ifndef __UBOOT__
4242         mtd->_suspend = nand_suspend;
4243         mtd->_resume = nand_resume;
4244 #endif
4245         mtd->_block_isbad = nand_block_isbad;
4246         mtd->_block_markbad = nand_block_markbad;
4247         mtd->writebufsize = mtd->writesize;
4248
4249         /* propagate ecc info to mtd_info */
4250         mtd->ecclayout = ecc->layout;
4251         mtd->ecc_strength = ecc->strength;
4252         mtd->ecc_step_size = ecc->size;
4253         /*
4254          * Initialize bitflip_threshold to its default prior scan_bbt() call.
4255          * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be
4256          * properly set.
4257          */
4258         if (!mtd->bitflip_threshold)
4259                 mtd->bitflip_threshold = mtd->ecc_strength;
4260
4261         return 0;
4262 }
4263 EXPORT_SYMBOL(nand_scan_tail);
4264
4265 /*
4266  * is_module_text_address() isn't exported, and it's mostly a pointless
4267  * test if this is a module _anyway_ -- they'd have to try _really_ hard
4268  * to call us from in-kernel code if the core NAND support is modular.
4269  */
4270 #ifdef MODULE
4271 #define caller_is_module() (1)
4272 #else
4273 #define caller_is_module() \
4274         is_module_text_address((unsigned long)__builtin_return_address(0))
4275 #endif
4276
4277 /**
4278  * nand_scan - [NAND Interface] Scan for the NAND device
4279  * @mtd: MTD device structure
4280  * @maxchips: number of chips to scan for
4281  *
4282  * This fills out all the uninitialized function pointers with the defaults.
4283  * The flash ID is read and the mtd/chip structures are filled with the
4284  * appropriate values. The mtd->owner field must be set to the module of the
4285  * caller.
4286  */
4287 int nand_scan(struct mtd_info *mtd, int maxchips)
4288 {
4289         int ret;
4290
4291         /* Many callers got this wrong, so check for it for a while... */
4292         if (!mtd->owner && caller_is_module()) {
4293                 pr_crit("%s called with NULL mtd->owner!\n", __func__);
4294                 BUG();
4295         }
4296
4297         ret = nand_scan_ident(mtd, maxchips, NULL);
4298         if (!ret)
4299                 ret = nand_scan_tail(mtd);
4300         return ret;
4301 }
4302 EXPORT_SYMBOL(nand_scan);
4303
4304 #ifndef __UBOOT__
4305 /**
4306  * nand_release - [NAND Interface] Free resources held by the NAND device
4307  * @mtd: MTD device structure
4308  */
4309 void nand_release(struct mtd_info *mtd)
4310 {
4311         struct nand_chip *chip = mtd->priv;
4312
4313         if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
4314                 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
4315
4316         mtd_device_unregister(mtd);
4317
4318         /* Free bad block table memory */
4319         kfree(chip->bbt);
4320         if (!(chip->options & NAND_OWN_BUFFERS))
4321                 kfree(chip->buffers);
4322
4323         /* Free bad block descriptor memory */
4324         if (chip->badblock_pattern && chip->badblock_pattern->options
4325                         & NAND_BBT_DYNAMICSTRUCT)
4326                 kfree(chip->badblock_pattern);
4327 }
4328 EXPORT_SYMBOL_GPL(nand_release);
4329
4330 static int __init nand_base_init(void)
4331 {
4332         led_trigger_register_simple("nand-disk", &nand_led_trigger);
4333         return 0;
4334 }
4335
4336 static void __exit nand_base_exit(void)
4337 {
4338         led_trigger_unregister_simple(nand_led_trigger);
4339 }
4340 #endif
4341
4342 module_init(nand_base_init);
4343 module_exit(nand_base_exit);
4344
4345 MODULE_LICENSE("GPL");
4346 MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
4347 MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
4348 MODULE_DESCRIPTION("Generic NAND flash driver code");