5 * This is the generic MTD driver for NAND flash devices. It should be
6 * capable of working with almost all NAND chips currently available.
7 * Basic support for AG-AND chips is provided.
9 * Additional technical information is available on
10 * http://www.linux-mtd.infradead.org/doc/nand.html
12 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
13 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
16 * David Woodhouse for adding multichip support
18 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
19 * rework for 2K page size chips
22 * Enable cached programming for 2k page size chips
23 * Check, if mtd->ecctype should be set to MTD_ECC_HW
24 * if we have HW ecc support.
25 * The AG-AND chips have nice features for speed improvement,
26 * which are not supported yet. Read / program 4 pages in one go.
27 * BBT table is not serialized, has to be fixed
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License version 2 as
31 * published by the Free Software Foundation.
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/compatmac.h>
46 #include <linux/interrupt.h>
47 #include <linux/bitops.h>
48 #include <linux/leds.h>
51 #ifdef CONFIG_MTD_PARTITIONS
52 #include <linux/mtd/partitions.h>
55 /* Define default oob placement schemes for large and small page devices */
56 static struct nand_ecclayout nand_oob_8 = {
66 static struct nand_ecclayout nand_oob_16 = {
68 .eccpos = {0, 1, 2, 3, 6, 7},
74 static struct nand_ecclayout nand_oob_64 = {
77 40, 41, 42, 43, 44, 45, 46, 47,
78 48, 49, 50, 51, 52, 53, 54, 55,
79 56, 57, 58, 59, 60, 61, 62, 63},
85 static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
88 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
89 struct mtd_oob_ops *ops);
92 * For devices which display every fart in the system on a separate LED. Is
93 * compiled away when LED support is disabled.
95 DEFINE_LED_TRIGGER(nand_led_trigger);
98 * nand_release_device - [GENERIC] release chip
99 * @mtd: MTD device structure
101 * Deselect, release chip lock and wake up anyone waiting on the device
103 static void nand_release_device(struct mtd_info *mtd)
105 struct nand_chip *chip = mtd->priv;
107 /* De-select the NAND device */
108 chip->select_chip(mtd, -1);
110 /* Release the controller and the chip */
111 spin_lock(&chip->controller->lock);
112 chip->controller->active = NULL;
113 chip->state = FL_READY;
114 wake_up(&chip->controller->wq);
115 spin_unlock(&chip->controller->lock);
119 * nand_read_byte - [DEFAULT] read one byte from the chip
120 * @mtd: MTD device structure
122 * Default read function for 8bit buswith
124 static uint8_t nand_read_byte(struct mtd_info *mtd)
126 struct nand_chip *chip = mtd->priv;
127 return readb(chip->IO_ADDR_R);
131 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
132 * @mtd: MTD device structure
134 * Default read function for 16bit buswith with
135 * endianess conversion
137 static uint8_t nand_read_byte16(struct mtd_info *mtd)
139 struct nand_chip *chip = mtd->priv;
140 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
144 * nand_read_word - [DEFAULT] read one word from the chip
145 * @mtd: MTD device structure
147 * Default read function for 16bit buswith without
148 * endianess conversion
150 static u16 nand_read_word(struct mtd_info *mtd)
152 struct nand_chip *chip = mtd->priv;
153 return readw(chip->IO_ADDR_R);
157 * nand_select_chip - [DEFAULT] control CE line
158 * @mtd: MTD device structure
159 * @chipnr: chipnumber to select, -1 for deselect
161 * Default select function for 1 chip devices.
163 static void nand_select_chip(struct mtd_info *mtd, int chipnr)
165 struct nand_chip *chip = mtd->priv;
169 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
180 * nand_write_buf - [DEFAULT] write buffer to chip
181 * @mtd: MTD device structure
183 * @len: number of bytes to write
185 * Default write function for 8bit buswith
187 static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
190 struct nand_chip *chip = mtd->priv;
192 for (i = 0; i < len; i++)
193 writeb(buf[i], chip->IO_ADDR_W);
197 * nand_read_buf - [DEFAULT] read chip data into buffer
198 * @mtd: MTD device structure
199 * @buf: buffer to store date
200 * @len: number of bytes to read
202 * Default read function for 8bit buswith
204 static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
207 struct nand_chip *chip = mtd->priv;
209 for (i = 0; i < len; i++)
210 buf[i] = readb(chip->IO_ADDR_R);
214 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
215 * @mtd: MTD device structure
216 * @buf: buffer containing the data to compare
217 * @len: number of bytes to compare
219 * Default verify function for 8bit buswith
221 static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
224 struct nand_chip *chip = mtd->priv;
226 for (i = 0; i < len; i++)
227 if (buf[i] != readb(chip->IO_ADDR_R))
233 * nand_write_buf16 - [DEFAULT] write buffer to chip
234 * @mtd: MTD device structure
236 * @len: number of bytes to write
238 * Default write function for 16bit buswith
240 static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
243 struct nand_chip *chip = mtd->priv;
244 u16 *p = (u16 *) buf;
247 for (i = 0; i < len; i++)
248 writew(p[i], chip->IO_ADDR_W);
253 * nand_read_buf16 - [DEFAULT] read chip data into buffer
254 * @mtd: MTD device structure
255 * @buf: buffer to store date
256 * @len: number of bytes to read
258 * Default read function for 16bit buswith
260 static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
263 struct nand_chip *chip = mtd->priv;
264 u16 *p = (u16 *) buf;
267 for (i = 0; i < len; i++)
268 p[i] = readw(chip->IO_ADDR_R);
272 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
273 * @mtd: MTD device structure
274 * @buf: buffer containing the data to compare
275 * @len: number of bytes to compare
277 * Default verify function for 16bit buswith
279 static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
282 struct nand_chip *chip = mtd->priv;
283 u16 *p = (u16 *) buf;
286 for (i = 0; i < len; i++)
287 if (p[i] != readw(chip->IO_ADDR_R))
294 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
295 * @mtd: MTD device structure
296 * @ofs: offset from device start
297 * @getchip: 0, if the chip is already selected
299 * Check, if the block is bad.
301 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
303 int page, chipnr, res = 0;
304 struct nand_chip *chip = mtd->priv;
307 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
310 chipnr = (int)(ofs >> chip->chip_shift);
312 nand_get_device(chip, mtd, FL_READING);
314 /* Select the NAND device */
315 chip->select_chip(mtd, chipnr);
318 if (chip->options & NAND_BUSWIDTH_16) {
319 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
321 bad = cpu_to_le16(chip->read_word(mtd));
322 if (chip->badblockpos & 0x1)
324 if ((bad & 0xFF) != 0xff)
327 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
328 if (chip->read_byte(mtd) != 0xff)
333 nand_release_device(mtd);
339 * nand_default_block_markbad - [DEFAULT] mark a block bad
340 * @mtd: MTD device structure
341 * @ofs: offset from device start
343 * This is the default implementation, which can be overridden by
344 * a hardware specific driver.
346 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
348 struct nand_chip *chip = mtd->priv;
349 uint8_t buf[2] = { 0, 0 };
352 /* Get block number */
353 block = (int)(ofs >> chip->bbt_erase_shift);
355 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
357 /* Do we have a flash based bad block table ? */
358 if (chip->options & NAND_USE_FLASH_BBT)
359 ret = nand_update_bbt(mtd, ofs);
361 /* We write two bytes, so we dont have to mess with 16 bit
364 nand_get_device(chip, mtd, FL_WRITING);
366 chip->ops.len = chip->ops.ooblen = 2;
367 chip->ops.datbuf = NULL;
368 chip->ops.oobbuf = buf;
369 chip->ops.ooboffs = chip->badblockpos & ~0x01;
371 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
372 nand_release_device(mtd);
375 mtd->ecc_stats.badblocks++;
381 * nand_check_wp - [GENERIC] check if the chip is write protected
382 * @mtd: MTD device structure
383 * Check, if the device is write protected
385 * The function expects, that the device is already selected
387 static int nand_check_wp(struct mtd_info *mtd)
389 struct nand_chip *chip = mtd->priv;
390 /* Check the WP bit */
391 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
392 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
396 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
397 * @mtd: MTD device structure
398 * @ofs: offset from device start
399 * @getchip: 0, if the chip is already selected
400 * @allowbbt: 1, if its allowed to access the bbt area
402 * Check, if the block is bad. Either by reading the bad block table or
403 * calling of the scan function.
405 static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
408 struct nand_chip *chip = mtd->priv;
411 return chip->block_bad(mtd, ofs, getchip);
413 /* Return info from the table */
414 return nand_isbad_bbt(mtd, ofs, allowbbt);
418 * Wait for the ready pin, after a command
419 * The timeout is catched later.
421 void nand_wait_ready(struct mtd_info *mtd)
423 struct nand_chip *chip = mtd->priv;
424 unsigned long timeo = jiffies + 2;
426 led_trigger_event(nand_led_trigger, LED_FULL);
427 /* wait until command is processed or timeout occures */
429 if (chip->dev_ready(mtd))
431 touch_softlockup_watchdog();
432 } while (time_before(jiffies, timeo));
433 led_trigger_event(nand_led_trigger, LED_OFF);
435 EXPORT_SYMBOL_GPL(nand_wait_ready);
438 * nand_command - [DEFAULT] Send command to NAND device
439 * @mtd: MTD device structure
440 * @command: the command to be sent
441 * @column: the column address for this command, -1 if none
442 * @page_addr: the page address for this command, -1 if none
444 * Send command to NAND device. This function is used for small page
445 * devices (256/512 Bytes per page)
447 static void nand_command(struct mtd_info *mtd, unsigned int command,
448 int column, int page_addr)
450 register struct nand_chip *chip = mtd->priv;
451 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
454 * Write out the command to the device.
456 if (command == NAND_CMD_SEQIN) {
459 if (column >= mtd->writesize) {
461 column -= mtd->writesize;
462 readcmd = NAND_CMD_READOOB;
463 } else if (column < 256) {
464 /* First 256 bytes --> READ0 */
465 readcmd = NAND_CMD_READ0;
468 readcmd = NAND_CMD_READ1;
470 chip->cmd_ctrl(mtd, readcmd, ctrl);
471 ctrl &= ~NAND_CTRL_CHANGE;
473 chip->cmd_ctrl(mtd, command, ctrl);
476 * Address cycle, when necessary
478 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
479 /* Serially input address */
481 /* Adjust columns for 16 bit buswidth */
482 if (chip->options & NAND_BUSWIDTH_16)
484 chip->cmd_ctrl(mtd, column, ctrl);
485 ctrl &= ~NAND_CTRL_CHANGE;
487 if (page_addr != -1) {
488 chip->cmd_ctrl(mtd, page_addr, ctrl);
489 ctrl &= ~NAND_CTRL_CHANGE;
490 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
491 /* One more address cycle for devices > 32MiB */
492 if (chip->chipsize > (32 << 20))
493 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
495 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
498 * program and erase have their own busy handlers
499 * status and sequential in needs no delay
503 case NAND_CMD_PAGEPROG:
504 case NAND_CMD_ERASE1:
505 case NAND_CMD_ERASE2:
507 case NAND_CMD_STATUS:
513 udelay(chip->chip_delay);
514 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
515 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
517 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
518 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
521 /* This applies to read commands */
524 * If we don't have access to the busy pin, we apply the given
527 if (!chip->dev_ready) {
528 udelay(chip->chip_delay);
532 /* Apply this short delay always to ensure that we do wait tWB in
533 * any case on any machine. */
536 nand_wait_ready(mtd);
540 * nand_command_lp - [DEFAULT] Send command to NAND large page device
541 * @mtd: MTD device structure
542 * @command: the command to be sent
543 * @column: the column address for this command, -1 if none
544 * @page_addr: the page address for this command, -1 if none
546 * Send command to NAND device. This is the version for the new large page
547 * devices We dont have the separate regions as we have in the small page
548 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
550 static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
551 int column, int page_addr)
553 register struct nand_chip *chip = mtd->priv;
555 /* Emulate NAND_CMD_READOOB */
556 if (command == NAND_CMD_READOOB) {
557 column += mtd->writesize;
558 command = NAND_CMD_READ0;
561 /* Command latch cycle */
562 chip->cmd_ctrl(mtd, command & 0xff,
563 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
565 if (column != -1 || page_addr != -1) {
566 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
568 /* Serially input address */
570 /* Adjust columns for 16 bit buswidth */
571 if (chip->options & NAND_BUSWIDTH_16)
573 chip->cmd_ctrl(mtd, column, ctrl);
574 ctrl &= ~NAND_CTRL_CHANGE;
575 chip->cmd_ctrl(mtd, column >> 8, ctrl);
577 if (page_addr != -1) {
578 chip->cmd_ctrl(mtd, page_addr, ctrl);
579 chip->cmd_ctrl(mtd, page_addr >> 8,
580 NAND_NCE | NAND_ALE);
581 /* One more address cycle for devices > 128MiB */
582 if (chip->chipsize > (128 << 20))
583 chip->cmd_ctrl(mtd, page_addr >> 16,
584 NAND_NCE | NAND_ALE);
587 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
590 * program and erase have their own busy handlers
591 * status, sequential in, and deplete1 need no delay
595 case NAND_CMD_CACHEDPROG:
596 case NAND_CMD_PAGEPROG:
597 case NAND_CMD_ERASE1:
598 case NAND_CMD_ERASE2:
601 case NAND_CMD_STATUS:
602 case NAND_CMD_DEPLETE1:
606 * read error status commands require only a short delay
608 case NAND_CMD_STATUS_ERROR:
609 case NAND_CMD_STATUS_ERROR0:
610 case NAND_CMD_STATUS_ERROR1:
611 case NAND_CMD_STATUS_ERROR2:
612 case NAND_CMD_STATUS_ERROR3:
613 udelay(chip->chip_delay);
619 udelay(chip->chip_delay);
620 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
621 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
622 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
623 NAND_NCE | NAND_CTRL_CHANGE);
624 while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
627 case NAND_CMD_RNDOUT:
628 /* No ready / busy check necessary */
629 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
630 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
631 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
632 NAND_NCE | NAND_CTRL_CHANGE);
636 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
637 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
638 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
639 NAND_NCE | NAND_CTRL_CHANGE);
641 /* This applies to read commands */
644 * If we don't have access to the busy pin, we apply the given
647 if (!chip->dev_ready) {
648 udelay(chip->chip_delay);
653 /* Apply this short delay always to ensure that we do wait tWB in
654 * any case on any machine. */
657 nand_wait_ready(mtd);
661 * nand_get_device - [GENERIC] Get chip for selected access
662 * @chip: the nand chip descriptor
663 * @mtd: MTD device structure
664 * @new_state: the state which is requested
666 * Get the device and lock it for exclusive access
669 nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
671 spinlock_t *lock = &chip->controller->lock;
672 wait_queue_head_t *wq = &chip->controller->wq;
673 DECLARE_WAITQUEUE(wait, current);
677 /* Hardware controller shared among independend devices */
678 /* Hardware controller shared among independend devices */
679 if (!chip->controller->active)
680 chip->controller->active = chip;
682 if (chip->controller->active == chip && chip->state == FL_READY) {
683 chip->state = new_state;
687 if (new_state == FL_PM_SUSPENDED) {
689 return (chip->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN;
691 set_current_state(TASK_UNINTERRUPTIBLE);
692 add_wait_queue(wq, &wait);
695 remove_wait_queue(wq, &wait);
700 * nand_wait - [DEFAULT] wait until the command is done
701 * @mtd: MTD device structure
702 * @chip: NAND chip structure
704 * Wait for command done. This applies to erase and program only
705 * Erase can take up to 400ms and program up to 20ms according to
706 * general NAND and SmartMedia specs
708 static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
711 unsigned long timeo = jiffies;
712 int status, state = chip->state;
714 if (state == FL_ERASING)
715 timeo += (HZ * 400) / 1000;
717 timeo += (HZ * 20) / 1000;
719 led_trigger_event(nand_led_trigger, LED_FULL);
721 /* Apply this short delay always to ensure that we do wait tWB in
722 * any case on any machine. */
725 if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
726 chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
728 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
730 while (time_before(jiffies, timeo)) {
731 if (chip->dev_ready) {
732 if (chip->dev_ready(mtd))
735 if (chip->read_byte(mtd) & NAND_STATUS_READY)
740 led_trigger_event(nand_led_trigger, LED_OFF);
742 status = (int)chip->read_byte(mtd);
747 * nand_read_page_raw - [Intern] read raw page data without ecc
748 * @mtd: mtd info structure
749 * @chip: nand chip info structure
750 * @buf: buffer to store read data
752 static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
755 chip->read_buf(mtd, buf, mtd->writesize);
756 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
761 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
762 * @mtd: mtd info structure
763 * @chip: nand chip info structure
764 * @buf: buffer to store read data
766 static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
769 int i, eccsize = chip->ecc.size;
770 int eccbytes = chip->ecc.bytes;
771 int eccsteps = chip->ecc.steps;
773 uint8_t *ecc_calc = chip->buffers->ecccalc;
774 uint8_t *ecc_code = chip->buffers->ecccode;
775 uint32_t *eccpos = chip->ecc.layout->eccpos;
777 chip->ecc.read_page_raw(mtd, chip, buf);
779 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
780 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
782 for (i = 0; i < chip->ecc.total; i++)
783 ecc_code[i] = chip->oob_poi[eccpos[i]];
785 eccsteps = chip->ecc.steps;
788 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
791 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
793 mtd->ecc_stats.failed++;
795 mtd->ecc_stats.corrected += stat;
801 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
802 * @mtd: mtd info structure
803 * @chip: nand chip info structure
804 * @data_offs: offset of requested data within the page
805 * @readlen: data length
806 * @bufpoi: buffer to store read data
808 static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
810 int start_step, end_step, num_steps;
811 uint32_t *eccpos = chip->ecc.layout->eccpos;
813 int data_col_addr, i, gaps = 0;
814 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
815 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
817 /* Column address wihin the page aligned to ECC size (256bytes). */
818 start_step = data_offs / chip->ecc.size;
819 end_step = (data_offs + readlen - 1) / chip->ecc.size;
820 num_steps = end_step - start_step + 1;
822 /* Data size aligned to ECC ecc.size*/
823 datafrag_len = num_steps * chip->ecc.size;
824 eccfrag_len = num_steps * chip->ecc.bytes;
826 data_col_addr = start_step * chip->ecc.size;
827 /* If we read not a page aligned data */
828 if (data_col_addr != 0)
829 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
831 p = bufpoi + data_col_addr;
832 chip->read_buf(mtd, p, datafrag_len);
835 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
836 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
838 /* The performance is faster if to position offsets
839 according to ecc.pos. Let make sure here that
840 there are no gaps in ecc positions */
841 for (i = 0; i < eccfrag_len - 1; i++) {
842 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
843 eccpos[i + start_step * chip->ecc.bytes + 1]) {
849 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
850 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
852 /* send the command to read the particular ecc bytes */
853 /* take care about buswidth alignment in read_buf */
854 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
855 aligned_len = eccfrag_len;
856 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
858 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
861 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
862 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
865 for (i = 0; i < eccfrag_len; i++)
866 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
868 p = bufpoi + data_col_addr;
869 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
872 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
874 mtd->ecc_stats.failed++;
876 mtd->ecc_stats.corrected += stat;
882 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
883 * @mtd: mtd info structure
884 * @chip: nand chip info structure
885 * @buf: buffer to store read data
887 * Not for syndrome calculating ecc controllers which need a special oob layout
889 static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
892 int i, eccsize = chip->ecc.size;
893 int eccbytes = chip->ecc.bytes;
894 int eccsteps = chip->ecc.steps;
896 uint8_t *ecc_calc = chip->buffers->ecccalc;
897 uint8_t *ecc_code = chip->buffers->ecccode;
898 uint32_t *eccpos = chip->ecc.layout->eccpos;
900 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
901 chip->ecc.hwctl(mtd, NAND_ECC_READ);
902 chip->read_buf(mtd, p, eccsize);
903 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
905 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
907 for (i = 0; i < chip->ecc.total; i++)
908 ecc_code[i] = chip->oob_poi[eccpos[i]];
910 eccsteps = chip->ecc.steps;
913 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
916 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
918 mtd->ecc_stats.failed++;
920 mtd->ecc_stats.corrected += stat;
926 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
927 * @mtd: mtd info structure
928 * @chip: nand chip info structure
929 * @buf: buffer to store read data
931 * The hw generator calculates the error syndrome automatically. Therefor
932 * we need a special oob layout and handling.
934 static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
937 int i, eccsize = chip->ecc.size;
938 int eccbytes = chip->ecc.bytes;
939 int eccsteps = chip->ecc.steps;
941 uint8_t *oob = chip->oob_poi;
943 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
946 chip->ecc.hwctl(mtd, NAND_ECC_READ);
947 chip->read_buf(mtd, p, eccsize);
949 if (chip->ecc.prepad) {
950 chip->read_buf(mtd, oob, chip->ecc.prepad);
951 oob += chip->ecc.prepad;
954 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
955 chip->read_buf(mtd, oob, eccbytes);
956 stat = chip->ecc.correct(mtd, p, oob, NULL);
959 mtd->ecc_stats.failed++;
961 mtd->ecc_stats.corrected += stat;
965 if (chip->ecc.postpad) {
966 chip->read_buf(mtd, oob, chip->ecc.postpad);
967 oob += chip->ecc.postpad;
971 /* Calculate remaining oob bytes */
972 i = mtd->oobsize - (oob - chip->oob_poi);
974 chip->read_buf(mtd, oob, i);
980 * nand_transfer_oob - [Internal] Transfer oob to client buffer
981 * @chip: nand chip structure
982 * @oob: oob destination address
983 * @ops: oob ops structure
984 * @len: size of oob to transfer
986 static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
987 struct mtd_oob_ops *ops, size_t len)
993 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
997 struct nand_oobfree *free = chip->ecc.layout->oobfree;
998 uint32_t boffs = 0, roffs = ops->ooboffs;
1001 for(; free->length && len; free++, len -= bytes) {
1002 /* Read request not from offset 0 ? */
1003 if (unlikely(roffs)) {
1004 if (roffs >= free->length) {
1005 roffs -= free->length;
1008 boffs = free->offset + roffs;
1009 bytes = min_t(size_t, len,
1010 (free->length - roffs));
1013 bytes = min_t(size_t, len, free->length);
1014 boffs = free->offset;
1016 memcpy(oob, chip->oob_poi + boffs, bytes);
1028 * nand_do_read_ops - [Internal] Read data with ECC
1030 * @mtd: MTD device structure
1031 * @from: offset to read from
1032 * @ops: oob ops structure
1034 * Internal function. Called with chip held.
1036 static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1037 struct mtd_oob_ops *ops)
1039 int chipnr, page, realpage, col, bytes, aligned;
1040 struct nand_chip *chip = mtd->priv;
1041 struct mtd_ecc_stats stats;
1042 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1045 uint32_t readlen = ops->len;
1046 uint32_t oobreadlen = ops->ooblen;
1047 uint8_t *bufpoi, *oob, *buf;
1049 stats = mtd->ecc_stats;
1051 chipnr = (int)(from >> chip->chip_shift);
1052 chip->select_chip(mtd, chipnr);
1054 realpage = (int)(from >> chip->page_shift);
1055 page = realpage & chip->pagemask;
1057 col = (int)(from & (mtd->writesize - 1));
1063 bytes = min(mtd->writesize - col, readlen);
1064 aligned = (bytes == mtd->writesize);
1066 /* Is the current page in the buffer ? */
1067 if (realpage != chip->pagebuf || oob) {
1068 bufpoi = aligned ? buf : chip->buffers->databuf;
1070 if (likely(sndcmd)) {
1071 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1075 /* Now read the page into the buffer */
1076 if (unlikely(ops->mode == MTD_OOB_RAW))
1077 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi);
1078 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1079 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
1081 ret = chip->ecc.read_page(mtd, chip, bufpoi);
1085 /* Transfer not aligned data */
1087 if (!NAND_SUBPAGE_READ(chip) && !oob)
1088 chip->pagebuf = realpage;
1089 memcpy(buf, chip->buffers->databuf + col, bytes);
1094 if (unlikely(oob)) {
1095 /* Raw mode does data:oob:data:oob */
1096 if (ops->mode != MTD_OOB_RAW) {
1097 int toread = min(oobreadlen,
1098 chip->ecc.layout->oobavail);
1100 oob = nand_transfer_oob(chip,
1102 oobreadlen -= toread;
1105 buf = nand_transfer_oob(chip,
1106 buf, ops, mtd->oobsize);
1109 if (!(chip->options & NAND_NO_READRDY)) {
1111 * Apply delay or wait for ready/busy pin. Do
1112 * this before the AUTOINCR check, so no
1113 * problems arise if a chip which does auto
1114 * increment is marked as NOAUTOINCR by the
1117 if (!chip->dev_ready)
1118 udelay(chip->chip_delay);
1120 nand_wait_ready(mtd);
1123 memcpy(buf, chip->buffers->databuf + col, bytes);
1132 /* For subsequent reads align to page boundary. */
1134 /* Increment page address */
1137 page = realpage & chip->pagemask;
1138 /* Check, if we cross a chip boundary */
1141 chip->select_chip(mtd, -1);
1142 chip->select_chip(mtd, chipnr);
1145 /* Check, if the chip supports auto page increment
1146 * or if we have hit a block boundary.
1148 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1152 ops->retlen = ops->len - (size_t) readlen;
1154 ops->oobretlen = ops->ooblen - oobreadlen;
1159 if (mtd->ecc_stats.failed - stats.failed)
1162 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1166 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1167 * @mtd: MTD device structure
1168 * @from: offset to read from
1169 * @len: number of bytes to read
1170 * @retlen: pointer to variable to store the number of read bytes
1171 * @buf: the databuffer to put data
1173 * Get hold of the chip and call nand_do_read
1175 static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1176 size_t *retlen, uint8_t *buf)
1178 struct nand_chip *chip = mtd->priv;
1181 /* Do not allow reads past end of device */
1182 if ((from + len) > mtd->size)
1187 nand_get_device(chip, mtd, FL_READING);
1189 chip->ops.len = len;
1190 chip->ops.datbuf = buf;
1191 chip->ops.oobbuf = NULL;
1193 ret = nand_do_read_ops(mtd, from, &chip->ops);
1195 *retlen = chip->ops.retlen;
1197 nand_release_device(mtd);
1203 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1204 * @mtd: mtd info structure
1205 * @chip: nand chip info structure
1206 * @page: page number to read
1207 * @sndcmd: flag whether to issue read command or not
1209 static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1210 int page, int sndcmd)
1213 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1216 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1221 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1223 * @mtd: mtd info structure
1224 * @chip: nand chip info structure
1225 * @page: page number to read
1226 * @sndcmd: flag whether to issue read command or not
1228 static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1229 int page, int sndcmd)
1231 uint8_t *buf = chip->oob_poi;
1232 int length = mtd->oobsize;
1233 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1234 int eccsize = chip->ecc.size;
1235 uint8_t *bufpoi = buf;
1236 int i, toread, sndrnd = 0, pos;
1238 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1239 for (i = 0; i < chip->ecc.steps; i++) {
1241 pos = eccsize + i * (eccsize + chunk);
1242 if (mtd->writesize > 512)
1243 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1245 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1248 toread = min_t(int, length, chunk);
1249 chip->read_buf(mtd, bufpoi, toread);
1254 chip->read_buf(mtd, bufpoi, length);
1260 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1261 * @mtd: mtd info structure
1262 * @chip: nand chip info structure
1263 * @page: page number to write
1265 static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1269 const uint8_t *buf = chip->oob_poi;
1270 int length = mtd->oobsize;
1272 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1273 chip->write_buf(mtd, buf, length);
1274 /* Send command to program the OOB data */
1275 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1277 status = chip->waitfunc(mtd, chip);
1279 return status & NAND_STATUS_FAIL ? -EIO : 0;
1283 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1284 * with syndrome - only for large page flash !
1285 * @mtd: mtd info structure
1286 * @chip: nand chip info structure
1287 * @page: page number to write
1289 static int nand_write_oob_syndrome(struct mtd_info *mtd,
1290 struct nand_chip *chip, int page)
1292 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1293 int eccsize = chip->ecc.size, length = mtd->oobsize;
1294 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1295 const uint8_t *bufpoi = chip->oob_poi;
1298 * data-ecc-data-ecc ... ecc-oob
1300 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1302 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1303 pos = steps * (eccsize + chunk);
1308 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1309 for (i = 0; i < steps; i++) {
1311 if (mtd->writesize <= 512) {
1312 uint32_t fill = 0xFFFFFFFF;
1316 int num = min_t(int, len, 4);
1317 chip->write_buf(mtd, (uint8_t *)&fill,
1322 pos = eccsize + i * (eccsize + chunk);
1323 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1327 len = min_t(int, length, chunk);
1328 chip->write_buf(mtd, bufpoi, len);
1333 chip->write_buf(mtd, bufpoi, length);
1335 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1336 status = chip->waitfunc(mtd, chip);
1338 return status & NAND_STATUS_FAIL ? -EIO : 0;
1342 * nand_do_read_oob - [Intern] NAND read out-of-band
1343 * @mtd: MTD device structure
1344 * @from: offset to read from
1345 * @ops: oob operations description structure
1347 * NAND read out-of-band data from the spare area
1349 static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1350 struct mtd_oob_ops *ops)
1352 int page, realpage, chipnr, sndcmd = 1;
1353 struct nand_chip *chip = mtd->priv;
1354 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1355 int readlen = ops->ooblen;
1357 uint8_t *buf = ops->oobbuf;
1359 DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1360 (unsigned long long)from, readlen);
1362 if (ops->mode == MTD_OOB_AUTO)
1363 len = chip->ecc.layout->oobavail;
1367 if (unlikely(ops->ooboffs >= len)) {
1368 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1369 "Attempt to start read outside oob\n");
1373 /* Do not allow reads past end of device */
1374 if (unlikely(from >= mtd->size ||
1375 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1376 (from >> chip->page_shift)) * len)) {
1377 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1378 "Attempt read beyond end of device\n");
1382 chipnr = (int)(from >> chip->chip_shift);
1383 chip->select_chip(mtd, chipnr);
1385 /* Shift to get page */
1386 realpage = (int)(from >> chip->page_shift);
1387 page = realpage & chip->pagemask;
1390 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
1392 len = min(len, readlen);
1393 buf = nand_transfer_oob(chip, buf, ops, len);
1395 if (!(chip->options & NAND_NO_READRDY)) {
1397 * Apply delay or wait for ready/busy pin. Do this
1398 * before the AUTOINCR check, so no problems arise if a
1399 * chip which does auto increment is marked as
1400 * NOAUTOINCR by the board driver.
1402 if (!chip->dev_ready)
1403 udelay(chip->chip_delay);
1405 nand_wait_ready(mtd);
1412 /* Increment page address */
1415 page = realpage & chip->pagemask;
1416 /* Check, if we cross a chip boundary */
1419 chip->select_chip(mtd, -1);
1420 chip->select_chip(mtd, chipnr);
1423 /* Check, if the chip supports auto page increment
1424 * or if we have hit a block boundary.
1426 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1430 ops->oobretlen = ops->ooblen;
1435 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
1436 * @mtd: MTD device structure
1437 * @from: offset to read from
1438 * @ops: oob operation description structure
1440 * NAND read data and/or out-of-band data
1442 static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1443 struct mtd_oob_ops *ops)
1445 struct nand_chip *chip = mtd->priv;
1446 int ret = -ENOTSUPP;
1450 /* Do not allow reads past end of device */
1451 if (ops->datbuf && (from + ops->len) > mtd->size) {
1452 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1453 "Attempt read beyond end of device\n");
1457 nand_get_device(chip, mtd, FL_READING);
1470 ret = nand_do_read_oob(mtd, from, ops);
1472 ret = nand_do_read_ops(mtd, from, ops);
1475 nand_release_device(mtd);
1481 * nand_write_page_raw - [Intern] raw page write function
1482 * @mtd: mtd info structure
1483 * @chip: nand chip info structure
1486 static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1489 chip->write_buf(mtd, buf, mtd->writesize);
1490 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1494 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
1495 * @mtd: mtd info structure
1496 * @chip: nand chip info structure
1499 static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1502 int i, eccsize = chip->ecc.size;
1503 int eccbytes = chip->ecc.bytes;
1504 int eccsteps = chip->ecc.steps;
1505 uint8_t *ecc_calc = chip->buffers->ecccalc;
1506 const uint8_t *p = buf;
1507 uint32_t *eccpos = chip->ecc.layout->eccpos;
1509 /* Software ecc calculation */
1510 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1511 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1513 for (i = 0; i < chip->ecc.total; i++)
1514 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1516 chip->ecc.write_page_raw(mtd, chip, buf);
1520 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
1521 * @mtd: mtd info structure
1522 * @chip: nand chip info structure
1525 static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1528 int i, eccsize = chip->ecc.size;
1529 int eccbytes = chip->ecc.bytes;
1530 int eccsteps = chip->ecc.steps;
1531 uint8_t *ecc_calc = chip->buffers->ecccalc;
1532 const uint8_t *p = buf;
1533 uint32_t *eccpos = chip->ecc.layout->eccpos;
1535 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1536 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1537 chip->write_buf(mtd, p, eccsize);
1538 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1541 for (i = 0; i < chip->ecc.total; i++)
1542 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1544 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1548 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
1549 * @mtd: mtd info structure
1550 * @chip: nand chip info structure
1553 * The hw generator calculates the error syndrome automatically. Therefor
1554 * we need a special oob layout and handling.
1556 static void nand_write_page_syndrome(struct mtd_info *mtd,
1557 struct nand_chip *chip, const uint8_t *buf)
1559 int i, eccsize = chip->ecc.size;
1560 int eccbytes = chip->ecc.bytes;
1561 int eccsteps = chip->ecc.steps;
1562 const uint8_t *p = buf;
1563 uint8_t *oob = chip->oob_poi;
1565 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1567 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1568 chip->write_buf(mtd, p, eccsize);
1570 if (chip->ecc.prepad) {
1571 chip->write_buf(mtd, oob, chip->ecc.prepad);
1572 oob += chip->ecc.prepad;
1575 chip->ecc.calculate(mtd, p, oob);
1576 chip->write_buf(mtd, oob, eccbytes);
1579 if (chip->ecc.postpad) {
1580 chip->write_buf(mtd, oob, chip->ecc.postpad);
1581 oob += chip->ecc.postpad;
1585 /* Calculate remaining oob bytes */
1586 i = mtd->oobsize - (oob - chip->oob_poi);
1588 chip->write_buf(mtd, oob, i);
1592 * nand_write_page - [REPLACEABLE] write one page
1593 * @mtd: MTD device structure
1594 * @chip: NAND chip descriptor
1595 * @buf: the data to write
1596 * @page: page number to write
1597 * @cached: cached programming
1598 * @raw: use _raw version of write_page
1600 static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1601 const uint8_t *buf, int page, int cached, int raw)
1605 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1608 chip->ecc.write_page_raw(mtd, chip, buf);
1610 chip->ecc.write_page(mtd, chip, buf);
1613 * Cached progamming disabled for now, Not sure if its worth the
1614 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1618 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1620 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1621 status = chip->waitfunc(mtd, chip);
1623 * See if operation failed and additional status checks are
1626 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1627 status = chip->errstat(mtd, chip, FL_WRITING, status,
1630 if (status & NAND_STATUS_FAIL)
1633 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1634 status = chip->waitfunc(mtd, chip);
1637 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1638 /* Send command to read back the data */
1639 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1641 if (chip->verify_buf(mtd, buf, mtd->writesize))
1648 * nand_fill_oob - [Internal] Transfer client buffer to oob
1649 * @chip: nand chip structure
1650 * @oob: oob data buffer
1651 * @ops: oob ops structure
1653 static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1654 struct mtd_oob_ops *ops)
1656 size_t len = ops->ooblen;
1662 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1665 case MTD_OOB_AUTO: {
1666 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1667 uint32_t boffs = 0, woffs = ops->ooboffs;
1670 for(; free->length && len; free++, len -= bytes) {
1671 /* Write request not from offset 0 ? */
1672 if (unlikely(woffs)) {
1673 if (woffs >= free->length) {
1674 woffs -= free->length;
1677 boffs = free->offset + woffs;
1678 bytes = min_t(size_t, len,
1679 (free->length - woffs));
1682 bytes = min_t(size_t, len, free->length);
1683 boffs = free->offset;
1685 memcpy(chip->oob_poi + boffs, oob, bytes);
1696 #define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
1699 * nand_do_write_ops - [Internal] NAND write with ECC
1700 * @mtd: MTD device structure
1701 * @to: offset to write to
1702 * @ops: oob operations description structure
1704 * NAND write with ECC
1706 static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1707 struct mtd_oob_ops *ops)
1709 int chipnr, realpage, page, blockmask, column;
1710 struct nand_chip *chip = mtd->priv;
1711 uint32_t writelen = ops->len;
1712 uint8_t *oob = ops->oobbuf;
1713 uint8_t *buf = ops->datbuf;
1720 /* reject writes, which are not page aligned */
1721 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
1722 printk(KERN_NOTICE "nand_write: "
1723 "Attempt to write not page aligned data\n");
1727 column = to & (mtd->writesize - 1);
1728 subpage = column || (writelen & (mtd->writesize - 1));
1733 chipnr = (int)(to >> chip->chip_shift);
1734 chip->select_chip(mtd, chipnr);
1736 /* Check, if it is write protected */
1737 if (nand_check_wp(mtd))
1740 realpage = (int)(to >> chip->page_shift);
1741 page = realpage & chip->pagemask;
1742 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1744 /* Invalidate the page cache, when we write to the cached page */
1745 if (to <= (chip->pagebuf << chip->page_shift) &&
1746 (chip->pagebuf << chip->page_shift) < (to + ops->len))
1749 /* If we're not given explicit OOB data, let it be 0xFF */
1751 memset(chip->oob_poi, 0xff, mtd->oobsize);
1754 int bytes = mtd->writesize;
1755 int cached = writelen > bytes && page != blockmask;
1756 uint8_t *wbuf = buf;
1758 /* Partial page write ? */
1759 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1761 bytes = min_t(int, bytes - column, (int) writelen);
1763 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1764 memcpy(&chip->buffers->databuf[column], buf, bytes);
1765 wbuf = chip->buffers->databuf;
1769 oob = nand_fill_oob(chip, oob, ops);
1771 ret = chip->write_page(mtd, chip, wbuf, page, cached,
1772 (ops->mode == MTD_OOB_RAW));
1784 page = realpage & chip->pagemask;
1785 /* Check, if we cross a chip boundary */
1788 chip->select_chip(mtd, -1);
1789 chip->select_chip(mtd, chipnr);
1793 ops->retlen = ops->len - writelen;
1795 ops->oobretlen = ops->ooblen;
1800 * nand_write - [MTD Interface] NAND write with ECC
1801 * @mtd: MTD device structure
1802 * @to: offset to write to
1803 * @len: number of bytes to write
1804 * @retlen: pointer to variable to store the number of written bytes
1805 * @buf: the data to write
1807 * NAND write with ECC
1809 static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
1810 size_t *retlen, const uint8_t *buf)
1812 struct nand_chip *chip = mtd->priv;
1815 /* Do not allow reads past end of device */
1816 if ((to + len) > mtd->size)
1821 nand_get_device(chip, mtd, FL_WRITING);
1823 chip->ops.len = len;
1824 chip->ops.datbuf = (uint8_t *)buf;
1825 chip->ops.oobbuf = NULL;
1827 ret = nand_do_write_ops(mtd, to, &chip->ops);
1829 *retlen = chip->ops.retlen;
1831 nand_release_device(mtd);
1837 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
1838 * @mtd: MTD device structure
1839 * @to: offset to write to
1840 * @ops: oob operation description structure
1842 * NAND write out-of-band
1844 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1845 struct mtd_oob_ops *ops)
1847 int chipnr, page, status, len;
1848 struct nand_chip *chip = mtd->priv;
1850 DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
1851 (unsigned int)to, (int)ops->ooblen);
1853 if (ops->mode == MTD_OOB_AUTO)
1854 len = chip->ecc.layout->oobavail;
1858 /* Do not allow write past end of page */
1859 if ((ops->ooboffs + ops->ooblen) > len) {
1860 DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: "
1861 "Attempt to write past end of page\n");
1865 if (unlikely(ops->ooboffs >= len)) {
1866 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1867 "Attempt to start write outside oob\n");
1871 /* Do not allow reads past end of device */
1872 if (unlikely(to >= mtd->size ||
1873 ops->ooboffs + ops->ooblen >
1874 ((mtd->size >> chip->page_shift) -
1875 (to >> chip->page_shift)) * len)) {
1876 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1877 "Attempt write beyond end of device\n");
1881 chipnr = (int)(to >> chip->chip_shift);
1882 chip->select_chip(mtd, chipnr);
1884 /* Shift to get page */
1885 page = (int)(to >> chip->page_shift);
1888 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
1889 * of my DiskOnChip 2000 test units) will clear the whole data page too
1890 * if we don't do this. I have no clue why, but I seem to have 'fixed'
1891 * it in the doc2000 driver in August 1999. dwmw2.
1893 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1895 /* Check, if it is write protected */
1896 if (nand_check_wp(mtd))
1899 /* Invalidate the page cache, if we write to the cached page */
1900 if (page == chip->pagebuf)
1903 memset(chip->oob_poi, 0xff, mtd->oobsize);
1904 nand_fill_oob(chip, ops->oobbuf, ops);
1905 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
1906 memset(chip->oob_poi, 0xff, mtd->oobsize);
1911 ops->oobretlen = ops->ooblen;
1917 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
1918 * @mtd: MTD device structure
1919 * @to: offset to write to
1920 * @ops: oob operation description structure
1922 static int nand_write_oob(struct mtd_info *mtd, loff_t to,
1923 struct mtd_oob_ops *ops)
1925 struct nand_chip *chip = mtd->priv;
1926 int ret = -ENOTSUPP;
1930 /* Do not allow writes past end of device */
1931 if (ops->datbuf && (to + ops->len) > mtd->size) {
1932 DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: "
1933 "Attempt read beyond end of device\n");
1937 nand_get_device(chip, mtd, FL_WRITING);
1950 ret = nand_do_write_oob(mtd, to, ops);
1952 ret = nand_do_write_ops(mtd, to, ops);
1955 nand_release_device(mtd);
1960 * single_erease_cmd - [GENERIC] NAND standard block erase command function
1961 * @mtd: MTD device structure
1962 * @page: the page address of the block which will be erased
1964 * Standard erase command for NAND chips
1966 static void single_erase_cmd(struct mtd_info *mtd, int page)
1968 struct nand_chip *chip = mtd->priv;
1969 /* Send commands to erase a block */
1970 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1971 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1975 * multi_erease_cmd - [GENERIC] AND specific block erase command function
1976 * @mtd: MTD device structure
1977 * @page: the page address of the block which will be erased
1979 * AND multi block erase command function
1980 * Erase 4 consecutive blocks
1982 static void multi_erase_cmd(struct mtd_info *mtd, int page)
1984 struct nand_chip *chip = mtd->priv;
1985 /* Send commands to erase a block */
1986 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1987 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1988 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
1989 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1990 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1994 * nand_erase - [MTD Interface] erase block(s)
1995 * @mtd: MTD device structure
1996 * @instr: erase instruction
1998 * Erase one ore more blocks
2000 static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
2002 return nand_erase_nand(mtd, instr, 0);
2005 #define BBT_PAGE_MASK 0xffffff3f
2007 * nand_erase_nand - [Internal] erase block(s)
2008 * @mtd: MTD device structure
2009 * @instr: erase instruction
2010 * @allowbbt: allow erasing the bbt area
2012 * Erase one ore more blocks
2014 int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2017 int page, len, status, pages_per_block, ret, chipnr;
2018 struct nand_chip *chip = mtd->priv;
2019 int rewrite_bbt[NAND_MAX_CHIPS]={0};
2020 unsigned int bbt_masked_page = 0xffffffff;
2022 DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n",
2023 (unsigned int)instr->addr, (unsigned int)instr->len);
2025 /* Start address must align on block boundary */
2026 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
2027 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
2031 /* Length must align on block boundary */
2032 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
2033 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
2034 "Length not block aligned\n");
2038 /* Do not allow erase past end of device */
2039 if ((instr->len + instr->addr) > mtd->size) {
2040 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
2041 "Erase past end of device\n");
2045 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
2047 /* Grab the lock and see if the device is available */
2048 nand_get_device(chip, mtd, FL_ERASING);
2050 /* Shift to get first page */
2051 page = (int)(instr->addr >> chip->page_shift);
2052 chipnr = (int)(instr->addr >> chip->chip_shift);
2054 /* Calculate pages in each block */
2055 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
2057 /* Select the NAND device */
2058 chip->select_chip(mtd, chipnr);
2060 /* Check, if it is write protected */
2061 if (nand_check_wp(mtd)) {
2062 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
2063 "Device is write protected!!!\n");
2064 instr->state = MTD_ERASE_FAILED;
2069 * If BBT requires refresh, set the BBT page mask to see if the BBT
2070 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2071 * can not be matched. This is also done when the bbt is actually
2072 * erased to avoid recusrsive updates
2074 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2075 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2077 /* Loop through the pages */
2080 instr->state = MTD_ERASING;
2084 * heck if we have a bad block, we do not erase bad blocks !
2086 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2087 chip->page_shift, 0, allowbbt)) {
2088 printk(KERN_WARNING "nand_erase: attempt to erase a "
2089 "bad block at page 0x%08x\n", page);
2090 instr->state = MTD_ERASE_FAILED;
2095 * Invalidate the page cache, if we erase the block which
2096 * contains the current cached page
2098 if (page <= chip->pagebuf && chip->pagebuf <
2099 (page + pages_per_block))
2102 chip->erase_cmd(mtd, page & chip->pagemask);
2104 status = chip->waitfunc(mtd, chip);
2107 * See if operation failed and additional status checks are
2110 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2111 status = chip->errstat(mtd, chip, FL_ERASING,
2114 /* See if block erase succeeded */
2115 if (status & NAND_STATUS_FAIL) {
2116 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: "
2117 "Failed erase, page 0x%08x\n", page);
2118 instr->state = MTD_ERASE_FAILED;
2119 instr->fail_addr = (page << chip->page_shift);
2124 * If BBT requires refresh, set the BBT rewrite flag to the
2127 if (bbt_masked_page != 0xffffffff &&
2128 (page & BBT_PAGE_MASK) == bbt_masked_page)
2129 rewrite_bbt[chipnr] = (page << chip->page_shift);
2131 /* Increment page address and decrement length */
2132 len -= (1 << chip->phys_erase_shift);
2133 page += pages_per_block;
2135 /* Check, if we cross a chip boundary */
2136 if (len && !(page & chip->pagemask)) {
2138 chip->select_chip(mtd, -1);
2139 chip->select_chip(mtd, chipnr);
2142 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2143 * page mask to see if this BBT should be rewritten
2145 if (bbt_masked_page != 0xffffffff &&
2146 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2147 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2151 instr->state = MTD_ERASE_DONE;
2155 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2157 /* Deselect and wake up anyone waiting on the device */
2158 nand_release_device(mtd);
2160 /* Do call back function */
2162 mtd_erase_callback(instr);
2165 * If BBT requires refresh and erase was successful, rewrite any
2166 * selected bad block tables
2168 if (bbt_masked_page == 0xffffffff || ret)
2171 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2172 if (!rewrite_bbt[chipnr])
2174 /* update the BBT for chip */
2175 DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
2176 "(%d:0x%0x 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
2177 chip->bbt_td->pages[chipnr]);
2178 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2181 /* Return more or less happy */
2186 * nand_sync - [MTD Interface] sync
2187 * @mtd: MTD device structure
2189 * Sync is actually a wait for chip ready function
2191 static void nand_sync(struct mtd_info *mtd)
2193 struct nand_chip *chip = mtd->priv;
2195 DEBUG(MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2197 /* Grab the lock and see if the device is available */
2198 nand_get_device(chip, mtd, FL_SYNCING);
2199 /* Release it and go back */
2200 nand_release_device(mtd);
2204 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
2205 * @mtd: MTD device structure
2206 * @offs: offset relative to mtd start
2208 static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
2210 /* Check for invalid offset */
2211 if (offs > mtd->size)
2214 return nand_block_checkbad(mtd, offs, 1, 0);
2218 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
2219 * @mtd: MTD device structure
2220 * @ofs: offset relative to mtd start
2222 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2224 struct nand_chip *chip = mtd->priv;
2227 if ((ret = nand_block_isbad(mtd, ofs))) {
2228 /* If it was bad already, return success and do nothing. */
2234 return chip->block_markbad(mtd, ofs);
2238 * nand_suspend - [MTD Interface] Suspend the NAND flash
2239 * @mtd: MTD device structure
2241 static int nand_suspend(struct mtd_info *mtd)
2243 struct nand_chip *chip = mtd->priv;
2245 return nand_get_device(chip, mtd, FL_PM_SUSPENDED);
2249 * nand_resume - [MTD Interface] Resume the NAND flash
2250 * @mtd: MTD device structure
2252 static void nand_resume(struct mtd_info *mtd)
2254 struct nand_chip *chip = mtd->priv;
2256 if (chip->state == FL_PM_SUSPENDED)
2257 nand_release_device(mtd);
2259 printk(KERN_ERR "nand_resume() called for a chip which is not "
2260 "in suspended state\n");
2264 * Set default functions
2266 static void nand_set_defaults(struct nand_chip *chip, int busw)
2268 /* check for proper chip_delay setup, set 20us if not */
2269 if (!chip->chip_delay)
2270 chip->chip_delay = 20;
2272 /* check, if a user supplied command function given */
2273 if (chip->cmdfunc == NULL)
2274 chip->cmdfunc = nand_command;
2276 /* check, if a user supplied wait function given */
2277 if (chip->waitfunc == NULL)
2278 chip->waitfunc = nand_wait;
2280 if (!chip->select_chip)
2281 chip->select_chip = nand_select_chip;
2282 if (!chip->read_byte)
2283 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2284 if (!chip->read_word)
2285 chip->read_word = nand_read_word;
2286 if (!chip->block_bad)
2287 chip->block_bad = nand_block_bad;
2288 if (!chip->block_markbad)
2289 chip->block_markbad = nand_default_block_markbad;
2290 if (!chip->write_buf)
2291 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2292 if (!chip->read_buf)
2293 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2294 if (!chip->verify_buf)
2295 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2296 if (!chip->scan_bbt)
2297 chip->scan_bbt = nand_default_bbt;
2299 if (!chip->controller) {
2300 chip->controller = &chip->hwcontrol;
2301 spin_lock_init(&chip->controller->lock);
2302 init_waitqueue_head(&chip->controller->wq);
2308 * Get the flash and manufacturer id and lookup if the type is supported
2310 static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2311 struct nand_chip *chip,
2312 int busw, int *maf_id)
2314 struct nand_flash_dev *type = NULL;
2315 int i, dev_id, maf_idx;
2316 int tmp_id, tmp_manf;
2318 /* Select the device */
2319 chip->select_chip(mtd, 0);
2321 /* Send the command for reading device ID */
2322 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2324 /* Read manufacturer and device IDs */
2325 *maf_id = chip->read_byte(mtd);
2326 dev_id = chip->read_byte(mtd);
2328 /* Try again to make sure, as some systems the bus-hold or other
2329 * interface concerns can cause random data which looks like a
2330 * possibly credible NAND flash to appear. If the two results do
2331 * not match, ignore the device completely.
2334 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2336 /* Read manufacturer and device IDs */
2338 tmp_manf = chip->read_byte(mtd);
2339 tmp_id = chip->read_byte(mtd);
2341 if (tmp_manf != *maf_id || tmp_id != dev_id) {
2342 printk(KERN_INFO "%s: second ID read did not match "
2343 "%02x,%02x against %02x,%02x\n", __func__,
2344 *maf_id, dev_id, tmp_manf, tmp_id);
2345 return ERR_PTR(-ENODEV);
2348 /* Lookup the flash id */
2349 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
2350 if (dev_id == nand_flash_ids[i].id) {
2351 type = &nand_flash_ids[i];
2357 return ERR_PTR(-ENODEV);
2360 mtd->name = type->name;
2362 chip->chipsize = type->chipsize << 20;
2364 /* Newer devices have all the information in additional id bytes */
2365 if (!type->pagesize) {
2367 /* The 3rd id byte holds MLC / multichip data */
2368 chip->cellinfo = chip->read_byte(mtd);
2369 /* The 4th id byte is the important one */
2370 extid = chip->read_byte(mtd);
2372 mtd->writesize = 1024 << (extid & 0x3);
2375 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
2377 /* Calc blocksize. Blocksize is multiples of 64KiB */
2378 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2380 /* Get buswidth information */
2381 busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2385 * Old devices have chip data hardcoded in the device id table
2387 mtd->erasesize = type->erasesize;
2388 mtd->writesize = type->pagesize;
2389 mtd->oobsize = mtd->writesize / 32;
2390 busw = type->options & NAND_BUSWIDTH_16;
2393 /* Try to identify manufacturer */
2394 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2395 if (nand_manuf_ids[maf_idx].id == *maf_id)
2400 * Check, if buswidth is correct. Hardware drivers should set
2403 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
2404 printk(KERN_INFO "NAND device: Manufacturer ID:"
2405 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2406 dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2407 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
2408 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
2410 return ERR_PTR(-EINVAL);
2413 /* Calculate the address shift from the page size */
2414 chip->page_shift = ffs(mtd->writesize) - 1;
2415 /* Convert chipsize to number of pages per chip -1. */
2416 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
2418 chip->bbt_erase_shift = chip->phys_erase_shift =
2419 ffs(mtd->erasesize) - 1;
2420 chip->chip_shift = ffs(chip->chipsize) - 1;
2422 /* Set the bad block position */
2423 chip->badblockpos = mtd->writesize > 512 ?
2424 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2426 /* Get chip options, preserve non chip based options */
2427 chip->options &= ~NAND_CHIPOPTIONS_MSK;
2428 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
2431 * Set chip as a default. Board drivers can override it, if necessary
2433 chip->options |= NAND_NO_AUTOINCR;
2435 /* Check if chip is a not a samsung device. Do not clear the
2436 * options for chips which are not having an extended id.
2438 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2439 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2441 /* Check for AND chips with 4 page planes */
2442 if (chip->options & NAND_4PAGE_ARRAY)
2443 chip->erase_cmd = multi_erase_cmd;
2445 chip->erase_cmd = single_erase_cmd;
2447 /* Do not replace user supplied command function ! */
2448 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2449 chip->cmdfunc = nand_command_lp;
2451 printk(KERN_INFO "NAND device: Manufacturer ID:"
2452 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, dev_id,
2453 nand_manuf_ids[maf_idx].name, type->name);
2459 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2460 * @mtd: MTD device structure
2461 * @maxchips: Number of chips to scan for
2463 * This is the first phase of the normal nand_scan() function. It
2464 * reads the flash ID and sets up MTD fields accordingly.
2466 * The mtd->owner field must be set to the module of the caller.
2468 int nand_scan_ident(struct mtd_info *mtd, int maxchips)
2470 int i, busw, nand_maf_id;
2471 struct nand_chip *chip = mtd->priv;
2472 struct nand_flash_dev *type;
2474 /* Get buswidth to select the correct functions */
2475 busw = chip->options & NAND_BUSWIDTH_16;
2476 /* Set the default functions */
2477 nand_set_defaults(chip, busw);
2479 /* Read the flash type */
2480 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id);
2483 printk(KERN_WARNING "No NAND device found!!!\n");
2484 chip->select_chip(mtd, -1);
2485 return PTR_ERR(type);
2488 /* Check for a chip array */
2489 for (i = 1; i < maxchips; i++) {
2490 chip->select_chip(mtd, i);
2491 /* Send the command for reading device ID */
2492 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2493 /* Read manufacturer and device IDs */
2494 if (nand_maf_id != chip->read_byte(mtd) ||
2495 type->id != chip->read_byte(mtd))
2499 printk(KERN_INFO "%d NAND chips detected\n", i);
2501 /* Store the number of chips and calc total size for mtd */
2503 mtd->size = i * chip->chipsize;
2510 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2511 * @mtd: MTD device structure
2512 * @maxchips: Number of chips to scan for
2514 * This is the second phase of the normal nand_scan() function. It
2515 * fills out all the uninitialized function pointers with the defaults
2516 * and scans for a bad block table if appropriate.
2518 int nand_scan_tail(struct mtd_info *mtd)
2521 struct nand_chip *chip = mtd->priv;
2523 if (!(chip->options & NAND_OWN_BUFFERS))
2524 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2528 /* Set the internal oob buffer location, just after the page data */
2529 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
2532 * If no default placement scheme is given, select an appropriate one
2534 if (!chip->ecc.layout) {
2535 switch (mtd->oobsize) {
2537 chip->ecc.layout = &nand_oob_8;
2540 chip->ecc.layout = &nand_oob_16;
2543 chip->ecc.layout = &nand_oob_64;
2546 printk(KERN_WARNING "No oob scheme defined for "
2547 "oobsize %d\n", mtd->oobsize);
2552 if (!chip->write_page)
2553 chip->write_page = nand_write_page;
2556 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2557 * selected and we have 256 byte pagesize fallback to software ECC
2559 if (!chip->ecc.read_page_raw)
2560 chip->ecc.read_page_raw = nand_read_page_raw;
2561 if (!chip->ecc.write_page_raw)
2562 chip->ecc.write_page_raw = nand_write_page_raw;
2564 switch (chip->ecc.mode) {
2566 /* Use standard hwecc read page function ? */
2567 if (!chip->ecc.read_page)
2568 chip->ecc.read_page = nand_read_page_hwecc;
2569 if (!chip->ecc.write_page)
2570 chip->ecc.write_page = nand_write_page_hwecc;
2571 if (!chip->ecc.read_oob)
2572 chip->ecc.read_oob = nand_read_oob_std;
2573 if (!chip->ecc.write_oob)
2574 chip->ecc.write_oob = nand_write_oob_std;
2576 case NAND_ECC_HW_SYNDROME:
2577 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2578 !chip->ecc.hwctl) &&
2579 (!chip->ecc.read_page ||
2580 chip->ecc.read_page == nand_read_page_hwecc ||
2581 !chip->ecc.write_page ||
2582 chip->ecc.write_page == nand_write_page_hwecc)) {
2583 printk(KERN_WARNING "No ECC functions supplied, "
2584 "Hardware ECC not possible\n");
2587 /* Use standard syndrome read/write page function ? */
2588 if (!chip->ecc.read_page)
2589 chip->ecc.read_page = nand_read_page_syndrome;
2590 if (!chip->ecc.write_page)
2591 chip->ecc.write_page = nand_write_page_syndrome;
2592 if (!chip->ecc.read_oob)
2593 chip->ecc.read_oob = nand_read_oob_syndrome;
2594 if (!chip->ecc.write_oob)
2595 chip->ecc.write_oob = nand_write_oob_syndrome;
2597 if (mtd->writesize >= chip->ecc.size)
2599 printk(KERN_WARNING "%d byte HW ECC not possible on "
2600 "%d byte page size, fallback to SW ECC\n",
2601 chip->ecc.size, mtd->writesize);
2602 chip->ecc.mode = NAND_ECC_SOFT;
2605 chip->ecc.calculate = nand_calculate_ecc;
2606 chip->ecc.correct = nand_correct_data;
2607 chip->ecc.read_page = nand_read_page_swecc;
2608 chip->ecc.read_subpage = nand_read_subpage;
2609 chip->ecc.write_page = nand_write_page_swecc;
2610 chip->ecc.read_oob = nand_read_oob_std;
2611 chip->ecc.write_oob = nand_write_oob_std;
2612 chip->ecc.size = 256;
2613 chip->ecc.bytes = 3;
2617 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2618 "This is not recommended !!\n");
2619 chip->ecc.read_page = nand_read_page_raw;
2620 chip->ecc.write_page = nand_write_page_raw;
2621 chip->ecc.read_oob = nand_read_oob_std;
2622 chip->ecc.write_oob = nand_write_oob_std;
2623 chip->ecc.size = mtd->writesize;
2624 chip->ecc.bytes = 0;
2628 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
2634 * The number of bytes available for a client to place data into
2635 * the out of band area
2637 chip->ecc.layout->oobavail = 0;
2638 for (i = 0; chip->ecc.layout->oobfree[i].length; i++)
2639 chip->ecc.layout->oobavail +=
2640 chip->ecc.layout->oobfree[i].length;
2641 mtd->oobavail = chip->ecc.layout->oobavail;
2644 * Set the number of read / write steps for one page depending on ECC
2647 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2648 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
2649 printk(KERN_WARNING "Invalid ecc parameters\n");
2652 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
2655 * Allow subpage writes up to ecc.steps. Not possible for MLC
2658 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2659 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2660 switch(chip->ecc.steps) {
2662 mtd->subpage_sft = 1;
2666 mtd->subpage_sft = 2;
2670 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
2672 /* Initialize state */
2673 chip->state = FL_READY;
2675 /* De-select the device */
2676 chip->select_chip(mtd, -1);
2678 /* Invalidate the pagebuffer reference */
2681 /* Fill in remaining MTD driver data */
2682 mtd->type = MTD_NANDFLASH;
2683 mtd->flags = MTD_CAP_NANDFLASH;
2684 mtd->erase = nand_erase;
2686 mtd->unpoint = NULL;
2687 mtd->read = nand_read;
2688 mtd->write = nand_write;
2689 mtd->read_oob = nand_read_oob;
2690 mtd->write_oob = nand_write_oob;
2691 mtd->sync = nand_sync;
2694 mtd->suspend = nand_suspend;
2695 mtd->resume = nand_resume;
2696 mtd->block_isbad = nand_block_isbad;
2697 mtd->block_markbad = nand_block_markbad;
2699 /* propagate ecc.layout to mtd_info */
2700 mtd->ecclayout = chip->ecc.layout;
2702 /* Check, if we should skip the bad block table scan */
2703 if (chip->options & NAND_SKIP_BBTSCAN)
2706 /* Build bad block table */
2707 return chip->scan_bbt(mtd);
2710 /* module_text_address() isn't exported, and it's mostly a pointless
2711 test if this is a module _anyway_ -- they'd have to try _really_ hard
2712 to call us from in-kernel code if the core NAND support is modular. */
2714 #define caller_is_module() (1)
2716 #define caller_is_module() \
2717 module_text_address((unsigned long)__builtin_return_address(0))
2721 * nand_scan - [NAND Interface] Scan for the NAND device
2722 * @mtd: MTD device structure
2723 * @maxchips: Number of chips to scan for
2725 * This fills out all the uninitialized function pointers
2726 * with the defaults.
2727 * The flash ID is read and the mtd/chip structures are
2728 * filled with the appropriate values.
2729 * The mtd->owner field must be set to the module of the caller
2732 int nand_scan(struct mtd_info *mtd, int maxchips)
2736 /* Many callers got this wrong, so check for it for a while... */
2737 if (!mtd->owner && caller_is_module()) {
2738 printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n");
2742 ret = nand_scan_ident(mtd, maxchips);
2744 ret = nand_scan_tail(mtd);
2749 * nand_release - [NAND Interface] Free resources held by the NAND device
2750 * @mtd: MTD device structure
2752 void nand_release(struct mtd_info *mtd)
2754 struct nand_chip *chip = mtd->priv;
2756 #ifdef CONFIG_MTD_PARTITIONS
2757 /* Deregister partitions */
2758 del_mtd_partitions(mtd);
2760 /* Deregister the device */
2761 del_mtd_device(mtd);
2763 /* Free bad block table memory */
2765 if (!(chip->options & NAND_OWN_BUFFERS))
2766 kfree(chip->buffers);
2769 EXPORT_SYMBOL_GPL(nand_scan);
2770 EXPORT_SYMBOL_GPL(nand_scan_ident);
2771 EXPORT_SYMBOL_GPL(nand_scan_tail);
2772 EXPORT_SYMBOL_GPL(nand_release);
2774 static int __init nand_base_init(void)
2776 led_trigger_register_simple("nand-disk", &nand_led_trigger);
2780 static void __exit nand_base_exit(void)
2782 led_trigger_unregister_simple(nand_led_trigger);
2785 module_init(nand_base_init);
2786 module_exit(nand_base_exit);
2788 MODULE_LICENSE("GPL");
2789 MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>");
2790 MODULE_DESCRIPTION("Generic NAND flash driver code");