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.
37 #define ENOTSUPP 524 /* Operation is not supported */
41 #include <linux/err.h>
42 #include <linux/mtd/compat.h>
43 #include <linux/mtd/mtd.h>
44 #include <linux/mtd/nand.h>
45 #include <linux/mtd/nand_ecc.h>
46 #include <linux/mtd/nand_bch.h>
48 #ifdef CONFIG_MTD_PARTITIONS
49 #include <linux/mtd/partitions.h>
53 #include <asm/errno.h>
56 * CONFIG_SYS_NAND_RESET_CNT is used as a timeout mechanism when resetting
57 * a flash. NAND flash is initialized prior to interrupts so standard timers
58 * can't be used. CONFIG_SYS_NAND_RESET_CNT should be set to a value
59 * which is greater than (max NAND reset time / NAND status read time).
60 * A conservative default of 200000 (500 us / 25 ns) is used as a default.
62 #ifndef CONFIG_SYS_NAND_RESET_CNT
63 #define CONFIG_SYS_NAND_RESET_CNT 200000
66 /* Define default oob placement schemes for large and small page devices */
67 static struct nand_ecclayout nand_oob_8 = {
77 static struct nand_ecclayout nand_oob_16 = {
79 .eccpos = {0, 1, 2, 3, 6, 7},
85 static struct nand_ecclayout nand_oob_64 = {
88 40, 41, 42, 43, 44, 45, 46, 47,
89 48, 49, 50, 51, 52, 53, 54, 55,
90 56, 57, 58, 59, 60, 61, 62, 63},
96 static struct nand_ecclayout nand_oob_128 = {
99 80, 81, 82, 83, 84, 85, 86, 87,
100 88, 89, 90, 91, 92, 93, 94, 95,
101 96, 97, 98, 99, 100, 101, 102, 103,
102 104, 105, 106, 107, 108, 109, 110, 111,
103 112, 113, 114, 115, 116, 117, 118, 119,
104 120, 121, 122, 123, 124, 125, 126, 127},
111 static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
114 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
115 struct mtd_oob_ops *ops);
117 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this);
120 * nand_release_device - [GENERIC] release chip
121 * @mtd: MTD device structure
123 * Deselect, release chip lock and wake up anyone waiting on the device
125 static void nand_release_device (struct mtd_info *mtd)
127 struct nand_chip *this = mtd->priv;
128 this->select_chip(mtd, -1); /* De-select the NAND device */
132 * nand_read_byte - [DEFAULT] read one byte from the chip
133 * @mtd: MTD device structure
135 * Default read function for 8bit buswith
137 uint8_t nand_read_byte(struct mtd_info *mtd)
139 struct nand_chip *chip = mtd->priv;
140 return readb(chip->IO_ADDR_R);
144 * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip
145 * @mtd: MTD device structure
147 * Default read function for 16bit buswith with
148 * endianess conversion
150 static uint8_t nand_read_byte16(struct mtd_info *mtd)
152 struct nand_chip *chip = mtd->priv;
153 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
157 * nand_read_word - [DEFAULT] read one word from the chip
158 * @mtd: MTD device structure
160 * Default read function for 16bit buswith without
161 * endianess conversion
163 static u16 nand_read_word(struct mtd_info *mtd)
165 struct nand_chip *chip = mtd->priv;
166 return readw(chip->IO_ADDR_R);
170 * nand_select_chip - [DEFAULT] control CE line
171 * @mtd: MTD device structure
172 * @chipnr: chipnumber to select, -1 for deselect
174 * Default select function for 1 chip devices.
176 static void nand_select_chip(struct mtd_info *mtd, int chipnr)
178 struct nand_chip *chip = mtd->priv;
182 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
193 * nand_write_buf - [DEFAULT] write buffer to chip
194 * @mtd: MTD device structure
196 * @len: number of bytes to write
198 * Default write function for 8bit buswith
200 void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
203 struct nand_chip *chip = mtd->priv;
205 for (i = 0; i < len; i++)
206 writeb(buf[i], chip->IO_ADDR_W);
210 * nand_read_buf - [DEFAULT] read chip data into buffer
211 * @mtd: MTD device structure
212 * @buf: buffer to store date
213 * @len: number of bytes to read
215 * Default read function for 8bit buswith
217 void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
220 struct nand_chip *chip = mtd->priv;
222 for (i = 0; i < len; i++)
223 buf[i] = readb(chip->IO_ADDR_R);
227 * nand_verify_buf - [DEFAULT] Verify chip data against buffer
228 * @mtd: MTD device structure
229 * @buf: buffer containing the data to compare
230 * @len: number of bytes to compare
232 * Default verify function for 8bit buswith
234 static int nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
237 struct nand_chip *chip = mtd->priv;
239 for (i = 0; i < len; i++)
240 if (buf[i] != readb(chip->IO_ADDR_R))
246 * nand_write_buf16 - [DEFAULT] write buffer to chip
247 * @mtd: MTD device structure
249 * @len: number of bytes to write
251 * Default write function for 16bit buswith
253 void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
256 struct nand_chip *chip = mtd->priv;
257 u16 *p = (u16 *) buf;
260 for (i = 0; i < len; i++)
261 writew(p[i], chip->IO_ADDR_W);
266 * nand_read_buf16 - [DEFAULT] read chip data into buffer
267 * @mtd: MTD device structure
268 * @buf: buffer to store date
269 * @len: number of bytes to read
271 * Default read function for 16bit buswith
273 void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
276 struct nand_chip *chip = mtd->priv;
277 u16 *p = (u16 *) buf;
280 for (i = 0; i < len; i++)
281 p[i] = readw(chip->IO_ADDR_R);
285 * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer
286 * @mtd: MTD device structure
287 * @buf: buffer containing the data to compare
288 * @len: number of bytes to compare
290 * Default verify function for 16bit buswith
292 static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
295 struct nand_chip *chip = mtd->priv;
296 u16 *p = (u16 *) buf;
299 for (i = 0; i < len; i++)
300 if (p[i] != readw(chip->IO_ADDR_R))
307 * nand_block_bad - [DEFAULT] Read bad block marker from the chip
308 * @mtd: MTD device structure
309 * @ofs: offset from device start
310 * @getchip: 0, if the chip is already selected
312 * Check, if the block is bad.
314 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
316 int page, chipnr, res = 0;
317 struct nand_chip *chip = mtd->priv;
320 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
323 chipnr = (int)(ofs >> chip->chip_shift);
325 nand_get_device(chip, mtd, FL_READING);
327 /* Select the NAND device */
328 chip->select_chip(mtd, chipnr);
331 if (chip->options & NAND_BUSWIDTH_16) {
332 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
334 bad = cpu_to_le16(chip->read_word(mtd));
335 if (chip->badblockpos & 0x1)
337 if ((bad & 0xFF) != 0xff)
340 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
341 if (chip->read_byte(mtd) != 0xff)
346 nand_release_device(mtd);
352 * nand_default_block_markbad - [DEFAULT] mark a block bad
353 * @mtd: MTD device structure
354 * @ofs: offset from device start
356 * This is the default implementation, which can be overridden by
357 * a hardware specific driver.
359 static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
361 struct nand_chip *chip = mtd->priv;
362 uint8_t buf[2] = { 0, 0 };
365 /* Get block number */
366 block = (int)(ofs >> chip->bbt_erase_shift);
368 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
370 /* Do we have a flash based bad block table ? */
371 if (chip->options & NAND_USE_FLASH_BBT)
372 ret = nand_update_bbt(mtd, ofs);
374 /* We write two bytes, so we dont have to mess with 16 bit
377 nand_get_device(chip, mtd, FL_WRITING);
379 chip->ops.len = chip->ops.ooblen = 2;
380 chip->ops.datbuf = NULL;
381 chip->ops.oobbuf = buf;
382 chip->ops.ooboffs = chip->badblockpos & ~0x01;
384 ret = nand_do_write_oob(mtd, ofs, &chip->ops);
385 nand_release_device(mtd);
388 mtd->ecc_stats.badblocks++;
394 * nand_check_wp - [GENERIC] check if the chip is write protected
395 * @mtd: MTD device structure
396 * Check, if the device is write protected
398 * The function expects, that the device is already selected
400 static int nand_check_wp(struct mtd_info *mtd)
402 struct nand_chip *chip = mtd->priv;
403 /* Check the WP bit */
404 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
405 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
409 * nand_block_checkbad - [GENERIC] Check if a block is marked bad
410 * @mtd: MTD device structure
411 * @ofs: offset from device start
412 * @getchip: 0, if the chip is already selected
413 * @allowbbt: 1, if its allowed to access the bbt area
415 * Check, if the block is bad. Either by reading the bad block table or
416 * calling of the scan function.
418 static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip,
421 struct nand_chip *chip = mtd->priv;
423 if (!(chip->options & NAND_BBT_SCANNED)) {
424 chip->options |= NAND_BBT_SCANNED;
429 return chip->block_bad(mtd, ofs, getchip);
431 /* Return info from the table */
432 return nand_isbad_bbt(mtd, ofs, allowbbt);
436 * Wait for the ready pin, after a command
437 * The timeout is catched later.
439 void nand_wait_ready(struct mtd_info *mtd)
441 struct nand_chip *chip = mtd->priv;
442 u32 timeo = (CONFIG_SYS_HZ * 20) / 1000;
445 time_start = get_timer(0);
447 /* wait until command is processed or timeout occures */
448 while (get_timer(time_start) < timeo) {
450 if (chip->dev_ready(mtd))
456 * nand_command - [DEFAULT] Send command to NAND device
457 * @mtd: MTD device structure
458 * @command: the command to be sent
459 * @column: the column address for this command, -1 if none
460 * @page_addr: the page address for this command, -1 if none
462 * Send command to NAND device. This function is used for small page
463 * devices (256/512 Bytes per page)
465 static void nand_command(struct mtd_info *mtd, unsigned int command,
466 int column, int page_addr)
468 register struct nand_chip *chip = mtd->priv;
469 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
470 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
473 * Write out the command to the device.
475 if (command == NAND_CMD_SEQIN) {
478 if (column >= mtd->writesize) {
480 column -= mtd->writesize;
481 readcmd = NAND_CMD_READOOB;
482 } else if (column < 256) {
483 /* First 256 bytes --> READ0 */
484 readcmd = NAND_CMD_READ0;
487 readcmd = NAND_CMD_READ1;
489 chip->cmd_ctrl(mtd, readcmd, ctrl);
490 ctrl &= ~NAND_CTRL_CHANGE;
492 chip->cmd_ctrl(mtd, command, ctrl);
495 * Address cycle, when necessary
497 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
498 /* Serially input address */
500 /* Adjust columns for 16 bit buswidth */
501 if (chip->options & NAND_BUSWIDTH_16)
503 chip->cmd_ctrl(mtd, column, ctrl);
504 ctrl &= ~NAND_CTRL_CHANGE;
506 if (page_addr != -1) {
507 chip->cmd_ctrl(mtd, page_addr, ctrl);
508 ctrl &= ~NAND_CTRL_CHANGE;
509 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
510 /* One more address cycle for devices > 32MiB */
511 if (chip->chipsize > (32 << 20))
512 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
514 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
517 * program and erase have their own busy handlers
518 * status and sequential in needs no delay
522 case NAND_CMD_PAGEPROG:
523 case NAND_CMD_ERASE1:
524 case NAND_CMD_ERASE2:
526 case NAND_CMD_STATUS:
532 udelay(chip->chip_delay);
533 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
534 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
536 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
537 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
541 /* This applies to read commands */
544 * If we don't have access to the busy pin, we apply the given
547 if (!chip->dev_ready) {
548 udelay(chip->chip_delay);
552 /* Apply this short delay always to ensure that we do wait tWB in
553 * any case on any machine. */
556 nand_wait_ready(mtd);
560 * nand_command_lp - [DEFAULT] Send command to NAND large page device
561 * @mtd: MTD device structure
562 * @command: the command to be sent
563 * @column: the column address for this command, -1 if none
564 * @page_addr: the page address for this command, -1 if none
566 * Send command to NAND device. This is the version for the new large page
567 * devices We dont have the separate regions as we have in the small page
568 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
570 static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
571 int column, int page_addr)
573 register struct nand_chip *chip = mtd->priv;
574 uint32_t rst_sts_cnt = CONFIG_SYS_NAND_RESET_CNT;
576 /* Emulate NAND_CMD_READOOB */
577 if (command == NAND_CMD_READOOB) {
578 column += mtd->writesize;
579 command = NAND_CMD_READ0;
582 /* Command latch cycle */
583 chip->cmd_ctrl(mtd, command & 0xff,
584 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
586 if (column != -1 || page_addr != -1) {
587 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
589 /* Serially input address */
591 /* Adjust columns for 16 bit buswidth */
592 if (chip->options & NAND_BUSWIDTH_16)
594 chip->cmd_ctrl(mtd, column, ctrl);
595 ctrl &= ~NAND_CTRL_CHANGE;
596 chip->cmd_ctrl(mtd, column >> 8, ctrl);
598 if (page_addr != -1) {
599 chip->cmd_ctrl(mtd, page_addr, ctrl);
600 chip->cmd_ctrl(mtd, page_addr >> 8,
601 NAND_NCE | NAND_ALE);
602 /* One more address cycle for devices > 128MiB */
603 if (chip->chipsize > (128 << 20))
604 chip->cmd_ctrl(mtd, page_addr >> 16,
605 NAND_NCE | NAND_ALE);
608 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
611 * program and erase have their own busy handlers
612 * status, sequential in, and deplete1 need no delay
616 case NAND_CMD_CACHEDPROG:
617 case NAND_CMD_PAGEPROG:
618 case NAND_CMD_ERASE1:
619 case NAND_CMD_ERASE2:
622 case NAND_CMD_STATUS:
623 case NAND_CMD_DEPLETE1:
627 * read error status commands require only a short delay
629 case NAND_CMD_STATUS_ERROR:
630 case NAND_CMD_STATUS_ERROR0:
631 case NAND_CMD_STATUS_ERROR1:
632 case NAND_CMD_STATUS_ERROR2:
633 case NAND_CMD_STATUS_ERROR3:
634 udelay(chip->chip_delay);
640 udelay(chip->chip_delay);
641 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
642 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
643 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
644 NAND_NCE | NAND_CTRL_CHANGE);
645 while (!(chip->read_byte(mtd) & NAND_STATUS_READY) &&
649 case NAND_CMD_RNDOUT:
650 /* No ready / busy check necessary */
651 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
652 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
653 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
654 NAND_NCE | NAND_CTRL_CHANGE);
658 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
659 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
660 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
661 NAND_NCE | NAND_CTRL_CHANGE);
663 /* This applies to read commands */
666 * If we don't have access to the busy pin, we apply the given
669 if (!chip->dev_ready) {
670 udelay(chip->chip_delay);
675 /* Apply this short delay always to ensure that we do wait tWB in
676 * any case on any machine. */
679 nand_wait_ready(mtd);
683 * nand_get_device - [GENERIC] Get chip for selected access
684 * @chip: the nand chip descriptor
685 * @mtd: MTD device structure
686 * @new_state: the state which is requested
688 * Get the device and lock it for exclusive access
690 static int nand_get_device (struct nand_chip *this, struct mtd_info *mtd, int new_state)
692 this->state = new_state;
697 * nand_wait - [DEFAULT] wait until the command is done
698 * @mtd: MTD device structure
699 * @chip: NAND chip structure
701 * Wait for command done. This applies to erase and program only
702 * Erase can take up to 400ms and program up to 20ms according to
703 * general NAND and SmartMedia specs
705 static int nand_wait(struct mtd_info *mtd, struct nand_chip *this)
708 int state = this->state;
711 if (state == FL_ERASING)
712 timeo = (CONFIG_SYS_HZ * 400) / 1000;
714 timeo = (CONFIG_SYS_HZ * 20) / 1000;
716 if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
717 this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
719 this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
721 time_start = get_timer(0);
724 if (get_timer(time_start) > timeo) {
729 if (this->dev_ready) {
730 if (this->dev_ready(mtd))
733 if (this->read_byte(mtd) & NAND_STATUS_READY)
737 #ifdef PPCHAMELON_NAND_TIMER_HACK
738 time_start = get_timer(0);
739 while (get_timer(time_start) < 10)
741 #endif /* PPCHAMELON_NAND_TIMER_HACK */
743 return this->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
751 * @page: page number to read
753 * Not for syndrome calculating ecc controllers, which use a special oob layout
755 static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
756 uint8_t *buf, int page)
758 chip->read_buf(mtd, buf, mtd->writesize);
759 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
764 * nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
765 * @mtd: mtd info structure
766 * @chip: nand chip info structure
767 * @buf: buffer to store read data
768 * @page: page number to read
770 * We need a special oob layout and handling even when OOB isn't used.
772 static int nand_read_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
773 uint8_t *buf, int page)
775 int eccsize = chip->ecc.size;
776 int eccbytes = chip->ecc.bytes;
777 uint8_t *oob = chip->oob_poi;
780 for (steps = chip->ecc.steps; steps > 0; steps--) {
781 chip->read_buf(mtd, buf, eccsize);
784 if (chip->ecc.prepad) {
785 chip->read_buf(mtd, oob, chip->ecc.prepad);
786 oob += chip->ecc.prepad;
789 chip->read_buf(mtd, oob, eccbytes);
792 if (chip->ecc.postpad) {
793 chip->read_buf(mtd, oob, chip->ecc.postpad);
794 oob += chip->ecc.postpad;
798 size = mtd->oobsize - (oob - chip->oob_poi);
800 chip->read_buf(mtd, oob, size);
806 * nand_read_page_swecc - [REPLACABLE] software ecc based page read function
807 * @mtd: mtd info structure
808 * @chip: nand chip info structure
809 * @buf: buffer to store read data
810 * @page: page number to read
812 static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
813 uint8_t *buf, int page)
815 int i, eccsize = chip->ecc.size;
816 int eccbytes = chip->ecc.bytes;
817 int eccsteps = chip->ecc.steps;
819 uint8_t *ecc_calc = chip->buffers->ecccalc;
820 uint8_t *ecc_code = chip->buffers->ecccode;
821 uint32_t *eccpos = chip->ecc.layout->eccpos;
823 chip->ecc.read_page_raw(mtd, chip, buf, page);
825 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
826 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
828 for (i = 0; i < chip->ecc.total; i++)
829 ecc_code[i] = chip->oob_poi[eccpos[i]];
831 eccsteps = chip->ecc.steps;
834 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
837 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
839 mtd->ecc_stats.failed++;
841 mtd->ecc_stats.corrected += stat;
847 * nand_read_subpage - [REPLACABLE] software ecc based sub-page read function
848 * @mtd: mtd info structure
849 * @chip: nand chip info structure
850 * @data_offs: offset of requested data within the page
851 * @readlen: data length
852 * @bufpoi: buffer to store read data
854 static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi)
856 int start_step, end_step, num_steps;
857 uint32_t *eccpos = chip->ecc.layout->eccpos;
859 int data_col_addr, i, gaps = 0;
860 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
861 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
863 /* Column address wihin the page aligned to ECC size (256bytes). */
864 start_step = data_offs / chip->ecc.size;
865 end_step = (data_offs + readlen - 1) / chip->ecc.size;
866 num_steps = end_step - start_step + 1;
868 /* Data size aligned to ECC ecc.size*/
869 datafrag_len = num_steps * chip->ecc.size;
870 eccfrag_len = num_steps * chip->ecc.bytes;
872 data_col_addr = start_step * chip->ecc.size;
873 /* If we read not a page aligned data */
874 if (data_col_addr != 0)
875 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
877 p = bufpoi + data_col_addr;
878 chip->read_buf(mtd, p, datafrag_len);
881 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
882 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
884 /* The performance is faster if to position offsets
885 according to ecc.pos. Let make sure here that
886 there are no gaps in ecc positions */
887 for (i = 0; i < eccfrag_len - 1; i++) {
888 if (eccpos[i + start_step * chip->ecc.bytes] + 1 !=
889 eccpos[i + start_step * chip->ecc.bytes + 1]) {
895 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
896 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
898 /* send the command to read the particular ecc bytes */
899 /* take care about buswidth alignment in read_buf */
900 aligned_pos = eccpos[start_step * chip->ecc.bytes] & ~(busw - 1);
901 aligned_len = eccfrag_len;
902 if (eccpos[start_step * chip->ecc.bytes] & (busw - 1))
904 if (eccpos[(start_step + num_steps) * chip->ecc.bytes] & (busw - 1))
907 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize + aligned_pos, -1);
908 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
911 for (i = 0; i < eccfrag_len; i++)
912 chip->buffers->ecccode[i] = chip->oob_poi[eccpos[i + start_step * chip->ecc.bytes]];
914 p = bufpoi + data_col_addr;
915 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
918 stat = chip->ecc.correct(mtd, p, &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
920 mtd->ecc_stats.failed++;
922 mtd->ecc_stats.corrected += stat;
928 * nand_read_page_hwecc - [REPLACABLE] hardware ecc based page read function
929 * @mtd: mtd info structure
930 * @chip: nand chip info structure
931 * @buf: buffer to store read data
932 * @page: page number to read
934 * Not for syndrome calculating ecc controllers which need a special oob layout
936 static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
937 uint8_t *buf, int page)
939 int i, eccsize = chip->ecc.size;
940 int eccbytes = chip->ecc.bytes;
941 int eccsteps = chip->ecc.steps;
943 uint8_t *ecc_calc = chip->buffers->ecccalc;
944 uint8_t *ecc_code = chip->buffers->ecccode;
945 uint32_t *eccpos = chip->ecc.layout->eccpos;
947 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
948 chip->ecc.hwctl(mtd, NAND_ECC_READ);
949 chip->read_buf(mtd, p, eccsize);
950 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
952 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
954 for (i = 0; i < chip->ecc.total; i++)
955 ecc_code[i] = chip->oob_poi[eccpos[i]];
957 eccsteps = chip->ecc.steps;
960 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
963 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
965 mtd->ecc_stats.failed++;
967 mtd->ecc_stats.corrected += stat;
973 * nand_read_page_hwecc_oob_first - [REPLACABLE] hw ecc, read oob first
974 * @mtd: mtd info structure
975 * @chip: nand chip info structure
976 * @buf: buffer to store read data
977 * @page: page number to read
979 * Hardware ECC for large page chips, require OOB to be read first.
980 * For this ECC mode, the write_page method is re-used from ECC_HW.
981 * These methods read/write ECC from the OOB area, unlike the
982 * ECC_HW_SYNDROME support with multiple ECC steps, follows the
983 * "infix ECC" scheme and reads/writes ECC from the data area, by
984 * overwriting the NAND manufacturer bad block markings.
986 static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
987 struct nand_chip *chip, uint8_t *buf, int page)
989 int i, eccsize = chip->ecc.size;
990 int eccbytes = chip->ecc.bytes;
991 int eccsteps = chip->ecc.steps;
993 uint8_t *ecc_code = chip->buffers->ecccode;
994 uint32_t *eccpos = chip->ecc.layout->eccpos;
995 uint8_t *ecc_calc = chip->buffers->ecccalc;
997 /* Read the OOB area first */
998 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
999 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1000 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1002 for (i = 0; i < chip->ecc.total; i++)
1003 ecc_code[i] = chip->oob_poi[eccpos[i]];
1005 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1008 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1009 chip->read_buf(mtd, p, eccsize);
1010 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1012 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1014 mtd->ecc_stats.failed++;
1016 mtd->ecc_stats.corrected += stat;
1022 * nand_read_page_syndrome - [REPLACABLE] hardware ecc syndrom based page read
1023 * @mtd: mtd info structure
1024 * @chip: nand chip info structure
1025 * @buf: buffer to store read data
1026 * @page: page number to read
1028 * The hw generator calculates the error syndrome automatically. Therefor
1029 * we need a special oob layout and handling.
1031 static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1032 uint8_t *buf, int page)
1034 int i, eccsize = chip->ecc.size;
1035 int eccbytes = chip->ecc.bytes;
1036 int eccsteps = chip->ecc.steps;
1038 uint8_t *oob = chip->oob_poi;
1040 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1043 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1044 chip->read_buf(mtd, p, eccsize);
1046 if (chip->ecc.prepad) {
1047 chip->read_buf(mtd, oob, chip->ecc.prepad);
1048 oob += chip->ecc.prepad;
1051 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1052 chip->read_buf(mtd, oob, eccbytes);
1053 stat = chip->ecc.correct(mtd, p, oob, NULL);
1056 mtd->ecc_stats.failed++;
1058 mtd->ecc_stats.corrected += stat;
1062 if (chip->ecc.postpad) {
1063 chip->read_buf(mtd, oob, chip->ecc.postpad);
1064 oob += chip->ecc.postpad;
1068 /* Calculate remaining oob bytes */
1069 i = mtd->oobsize - (oob - chip->oob_poi);
1071 chip->read_buf(mtd, oob, i);
1077 * nand_transfer_oob - [Internal] Transfer oob to client buffer
1078 * @chip: nand chip structure
1079 * @oob: oob destination address
1080 * @ops: oob ops structure
1081 * @len: size of oob to transfer
1083 static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob,
1084 struct mtd_oob_ops *ops, size_t len)
1090 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1093 case MTD_OOB_AUTO: {
1094 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1095 uint32_t boffs = 0, roffs = ops->ooboffs;
1098 for(; free->length && len; free++, len -= bytes) {
1099 /* Read request not from offset 0 ? */
1100 if (unlikely(roffs)) {
1101 if (roffs >= free->length) {
1102 roffs -= free->length;
1105 boffs = free->offset + roffs;
1106 bytes = min_t(size_t, len,
1107 (free->length - roffs));
1110 bytes = min_t(size_t, len, free->length);
1111 boffs = free->offset;
1113 memcpy(oob, chip->oob_poi + boffs, bytes);
1125 * nand_do_read_ops - [Internal] Read data with ECC
1127 * @mtd: MTD device structure
1128 * @from: offset to read from
1129 * @ops: oob ops structure
1131 * Internal function. Called with chip held.
1133 static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1134 struct mtd_oob_ops *ops)
1136 int chipnr, page, realpage, col, bytes, aligned;
1137 struct nand_chip *chip = mtd->priv;
1138 struct mtd_ecc_stats stats;
1139 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1142 uint32_t readlen = ops->len;
1143 uint32_t oobreadlen = ops->ooblen;
1144 uint8_t *bufpoi, *oob, *buf;
1146 stats = mtd->ecc_stats;
1148 chipnr = (int)(from >> chip->chip_shift);
1149 chip->select_chip(mtd, chipnr);
1151 realpage = (int)(from >> chip->page_shift);
1152 page = realpage & chip->pagemask;
1154 col = (int)(from & (mtd->writesize - 1));
1162 bytes = min(mtd->writesize - col, readlen);
1163 aligned = (bytes == mtd->writesize);
1165 /* Is the current page in the buffer ? */
1166 if (realpage != chip->pagebuf || oob) {
1167 bufpoi = aligned ? buf : chip->buffers->databuf;
1169 if (likely(sndcmd)) {
1170 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
1174 /* Now read the page into the buffer */
1175 if (unlikely(ops->mode == MTD_OOB_RAW))
1176 ret = chip->ecc.read_page_raw(mtd, chip,
1178 else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
1179 ret = chip->ecc.read_subpage(mtd, chip, col, bytes, bufpoi);
1181 ret = chip->ecc.read_page(mtd, chip, bufpoi,
1186 /* Transfer not aligned data */
1188 if (!NAND_SUBPAGE_READ(chip) && !oob)
1189 chip->pagebuf = realpage;
1190 memcpy(buf, chip->buffers->databuf + col, bytes);
1195 if (unlikely(oob)) {
1196 /* Raw mode does data:oob:data:oob */
1197 if (ops->mode != MTD_OOB_RAW) {
1198 int toread = min(oobreadlen,
1199 chip->ecc.layout->oobavail);
1201 oob = nand_transfer_oob(chip,
1203 oobreadlen -= toread;
1206 buf = nand_transfer_oob(chip,
1207 buf, ops, mtd->oobsize);
1210 if (!(chip->options & NAND_NO_READRDY)) {
1212 * Apply delay or wait for ready/busy pin. Do
1213 * this before the AUTOINCR check, so no
1214 * problems arise if a chip which does auto
1215 * increment is marked as NOAUTOINCR by the
1218 if (!chip->dev_ready)
1219 udelay(chip->chip_delay);
1221 nand_wait_ready(mtd);
1224 memcpy(buf, chip->buffers->databuf + col, bytes);
1233 /* For subsequent reads align to page boundary. */
1235 /* Increment page address */
1238 page = realpage & chip->pagemask;
1239 /* Check, if we cross a chip boundary */
1242 chip->select_chip(mtd, -1);
1243 chip->select_chip(mtd, chipnr);
1246 /* Check, if the chip supports auto page increment
1247 * or if we have hit a block boundary.
1249 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1253 ops->retlen = ops->len - (size_t) readlen;
1255 ops->oobretlen = ops->ooblen - oobreadlen;
1260 if (mtd->ecc_stats.failed - stats.failed)
1263 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1267 * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc
1268 * @mtd: MTD device structure
1269 * @from: offset to read from
1270 * @len: number of bytes to read
1271 * @retlen: pointer to variable to store the number of read bytes
1272 * @buf: the databuffer to put data
1274 * Get hold of the chip and call nand_do_read
1276 static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
1277 size_t *retlen, uint8_t *buf)
1279 struct nand_chip *chip = mtd->priv;
1282 /* Do not allow reads past end of device */
1283 if ((from + len) > mtd->size)
1288 nand_get_device(chip, mtd, FL_READING);
1290 chip->ops.len = len;
1291 chip->ops.datbuf = buf;
1292 chip->ops.oobbuf = NULL;
1294 ret = nand_do_read_ops(mtd, from, &chip->ops);
1296 *retlen = chip->ops.retlen;
1298 nand_release_device(mtd);
1304 * nand_read_oob_std - [REPLACABLE] the most common OOB data read function
1305 * @mtd: mtd info structure
1306 * @chip: nand chip info structure
1307 * @page: page number to read
1308 * @sndcmd: flag whether to issue read command or not
1310 static int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1311 int page, int sndcmd)
1314 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1317 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1322 * nand_read_oob_syndrome - [REPLACABLE] OOB data read function for HW ECC
1324 * @mtd: mtd info structure
1325 * @chip: nand chip info structure
1326 * @page: page number to read
1327 * @sndcmd: flag whether to issue read command or not
1329 static int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1330 int page, int sndcmd)
1332 uint8_t *buf = chip->oob_poi;
1333 int length = mtd->oobsize;
1334 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1335 int eccsize = chip->ecc.size;
1336 uint8_t *bufpoi = buf;
1337 int i, toread, sndrnd = 0, pos;
1339 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
1340 for (i = 0; i < chip->ecc.steps; i++) {
1342 pos = eccsize + i * (eccsize + chunk);
1343 if (mtd->writesize > 512)
1344 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
1346 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
1349 toread = min_t(int, length, chunk);
1350 chip->read_buf(mtd, bufpoi, toread);
1355 chip->read_buf(mtd, bufpoi, length);
1361 * nand_write_oob_std - [REPLACABLE] the most common OOB data write function
1362 * @mtd: mtd info structure
1363 * @chip: nand chip info structure
1364 * @page: page number to write
1366 static int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip,
1370 const uint8_t *buf = chip->oob_poi;
1371 int length = mtd->oobsize;
1373 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1374 chip->write_buf(mtd, buf, length);
1375 /* Send command to program the OOB data */
1376 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1378 status = chip->waitfunc(mtd, chip);
1380 return status & NAND_STATUS_FAIL ? -EIO : 0;
1384 * nand_write_oob_syndrome - [REPLACABLE] OOB data write function for HW ECC
1385 * with syndrome - only for large page flash !
1386 * @mtd: mtd info structure
1387 * @chip: nand chip info structure
1388 * @page: page number to write
1390 static int nand_write_oob_syndrome(struct mtd_info *mtd,
1391 struct nand_chip *chip, int page)
1393 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
1394 int eccsize = chip->ecc.size, length = mtd->oobsize;
1395 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
1396 const uint8_t *bufpoi = chip->oob_poi;
1399 * data-ecc-data-ecc ... ecc-oob
1401 * data-pad-ecc-pad-data-pad .... ecc-pad-oob
1403 if (!chip->ecc.prepad && !chip->ecc.postpad) {
1404 pos = steps * (eccsize + chunk);
1409 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
1410 for (i = 0; i < steps; i++) {
1412 if (mtd->writesize <= 512) {
1413 uint32_t fill = 0xFFFFFFFF;
1417 int num = min_t(int, len, 4);
1418 chip->write_buf(mtd, (uint8_t *)&fill,
1423 pos = eccsize + i * (eccsize + chunk);
1424 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
1428 len = min_t(int, length, chunk);
1429 chip->write_buf(mtd, bufpoi, len);
1434 chip->write_buf(mtd, bufpoi, length);
1436 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1437 status = chip->waitfunc(mtd, chip);
1439 return status & NAND_STATUS_FAIL ? -EIO : 0;
1443 * nand_do_read_oob - [Intern] NAND read out-of-band
1444 * @mtd: MTD device structure
1445 * @from: offset to read from
1446 * @ops: oob operations description structure
1448 * NAND read out-of-band data from the spare area
1450 static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
1451 struct mtd_oob_ops *ops)
1453 int page, realpage, chipnr, sndcmd = 1;
1454 struct nand_chip *chip = mtd->priv;
1455 int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1456 int readlen = ops->ooblen;
1458 uint8_t *buf = ops->oobbuf;
1460 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08Lx, len = %i\n",
1461 (unsigned long long)from, readlen);
1463 if (ops->mode == MTD_OOB_AUTO)
1464 len = chip->ecc.layout->oobavail;
1468 if (unlikely(ops->ooboffs >= len)) {
1469 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1470 "Attempt to start read outside oob\n");
1474 /* Do not allow reads past end of device */
1475 if (unlikely(from >= mtd->size ||
1476 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
1477 (from >> chip->page_shift)) * len)) {
1478 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1479 "Attempt read beyond end of device\n");
1483 chipnr = (int)(from >> chip->chip_shift);
1484 chip->select_chip(mtd, chipnr);
1486 /* Shift to get page */
1487 realpage = (int)(from >> chip->page_shift);
1488 page = realpage & chip->pagemask;
1492 sndcmd = chip->ecc.read_oob(mtd, chip, page, sndcmd);
1494 len = min(len, readlen);
1495 buf = nand_transfer_oob(chip, buf, ops, len);
1497 if (!(chip->options & NAND_NO_READRDY)) {
1499 * Apply delay or wait for ready/busy pin. Do this
1500 * before the AUTOINCR check, so no problems arise if a
1501 * chip which does auto increment is marked as
1502 * NOAUTOINCR by the board driver.
1504 if (!chip->dev_ready)
1505 udelay(chip->chip_delay);
1507 nand_wait_ready(mtd);
1514 /* Increment page address */
1517 page = realpage & chip->pagemask;
1518 /* Check, if we cross a chip boundary */
1521 chip->select_chip(mtd, -1);
1522 chip->select_chip(mtd, chipnr);
1525 /* Check, if the chip supports auto page increment
1526 * or if we have hit a block boundary.
1528 if (!NAND_CANAUTOINCR(chip) || !(page & blkcheck))
1532 ops->oobretlen = ops->ooblen;
1537 * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
1538 * @mtd: MTD device structure
1539 * @from: offset to read from
1540 * @ops: oob operation description structure
1542 * NAND read data and/or out-of-band data
1544 static int nand_read_oob(struct mtd_info *mtd, loff_t from,
1545 struct mtd_oob_ops *ops)
1547 struct nand_chip *chip = mtd->priv;
1548 int ret = -ENOTSUPP;
1552 /* Do not allow reads past end of device */
1553 if (ops->datbuf && (from + ops->len) > mtd->size) {
1554 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
1555 "Attempt read beyond end of device\n");
1559 nand_get_device(chip, mtd, FL_READING);
1572 ret = nand_do_read_oob(mtd, from, ops);
1574 ret = nand_do_read_ops(mtd, from, ops);
1577 nand_release_device(mtd);
1583 * nand_write_page_raw - [Intern] raw page write function
1584 * @mtd: mtd info structure
1585 * @chip: nand chip info structure
1588 * Not for syndrome calculating ecc controllers, which use a special oob layout
1590 static void nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1593 chip->write_buf(mtd, buf, mtd->writesize);
1594 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1598 * nand_write_page_raw_syndrome - [Intern] raw page write function
1599 * @mtd: mtd info structure
1600 * @chip: nand chip info structure
1603 * We need a special oob layout and handling even when ECC isn't checked.
1605 static void nand_write_page_raw_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1608 int eccsize = chip->ecc.size;
1609 int eccbytes = chip->ecc.bytes;
1610 uint8_t *oob = chip->oob_poi;
1613 for (steps = chip->ecc.steps; steps > 0; steps--) {
1614 chip->write_buf(mtd, buf, eccsize);
1617 if (chip->ecc.prepad) {
1618 chip->write_buf(mtd, oob, chip->ecc.prepad);
1619 oob += chip->ecc.prepad;
1622 chip->read_buf(mtd, oob, eccbytes);
1625 if (chip->ecc.postpad) {
1626 chip->write_buf(mtd, oob, chip->ecc.postpad);
1627 oob += chip->ecc.postpad;
1631 size = mtd->oobsize - (oob - chip->oob_poi);
1633 chip->write_buf(mtd, oob, size);
1636 * nand_write_page_swecc - [REPLACABLE] software ecc based page write function
1637 * @mtd: mtd info structure
1638 * @chip: nand chip info structure
1641 static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1644 int i, eccsize = chip->ecc.size;
1645 int eccbytes = chip->ecc.bytes;
1646 int eccsteps = chip->ecc.steps;
1647 uint8_t *ecc_calc = chip->buffers->ecccalc;
1648 const uint8_t *p = buf;
1649 uint32_t *eccpos = chip->ecc.layout->eccpos;
1651 /* Software ecc calculation */
1652 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1653 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1655 for (i = 0; i < chip->ecc.total; i++)
1656 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1658 chip->ecc.write_page_raw(mtd, chip, buf);
1662 * nand_write_page_hwecc - [REPLACABLE] hardware ecc based page write function
1663 * @mtd: mtd info structure
1664 * @chip: nand chip info structure
1667 static void nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1670 int i, eccsize = chip->ecc.size;
1671 int eccbytes = chip->ecc.bytes;
1672 int eccsteps = chip->ecc.steps;
1673 uint8_t *ecc_calc = chip->buffers->ecccalc;
1674 const uint8_t *p = buf;
1675 uint32_t *eccpos = chip->ecc.layout->eccpos;
1677 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1678 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1679 chip->write_buf(mtd, p, eccsize);
1680 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1683 for (i = 0; i < chip->ecc.total; i++)
1684 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1686 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
1690 * nand_write_page_syndrome - [REPLACABLE] hardware ecc syndrom based page write
1691 * @mtd: mtd info structure
1692 * @chip: nand chip info structure
1695 * The hw generator calculates the error syndrome automatically. Therefor
1696 * we need a special oob layout and handling.
1698 static void nand_write_page_syndrome(struct mtd_info *mtd,
1699 struct nand_chip *chip, const uint8_t *buf)
1701 int i, eccsize = chip->ecc.size;
1702 int eccbytes = chip->ecc.bytes;
1703 int eccsteps = chip->ecc.steps;
1704 const uint8_t *p = buf;
1705 uint8_t *oob = chip->oob_poi;
1707 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1709 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
1710 chip->write_buf(mtd, p, eccsize);
1712 if (chip->ecc.prepad) {
1713 chip->write_buf(mtd, oob, chip->ecc.prepad);
1714 oob += chip->ecc.prepad;
1717 chip->ecc.calculate(mtd, p, oob);
1718 chip->write_buf(mtd, oob, eccbytes);
1721 if (chip->ecc.postpad) {
1722 chip->write_buf(mtd, oob, chip->ecc.postpad);
1723 oob += chip->ecc.postpad;
1727 /* Calculate remaining oob bytes */
1728 i = mtd->oobsize - (oob - chip->oob_poi);
1730 chip->write_buf(mtd, oob, i);
1734 * nand_write_page - [REPLACEABLE] write one page
1735 * @mtd: MTD device structure
1736 * @chip: NAND chip descriptor
1737 * @buf: the data to write
1738 * @page: page number to write
1739 * @cached: cached programming
1740 * @raw: use _raw version of write_page
1742 static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1743 const uint8_t *buf, int page, int cached, int raw)
1747 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1750 chip->ecc.write_page_raw(mtd, chip, buf);
1752 chip->ecc.write_page(mtd, chip, buf);
1755 * Cached progamming disabled for now, Not sure if its worth the
1756 * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1760 if (!cached || !(chip->options & NAND_CACHEPRG)) {
1762 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1763 status = chip->waitfunc(mtd, chip);
1765 * See if operation failed and additional status checks are
1768 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1769 status = chip->errstat(mtd, chip, FL_WRITING, status,
1772 if (status & NAND_STATUS_FAIL)
1775 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1776 status = chip->waitfunc(mtd, chip);
1779 #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1780 /* Send command to read back the data */
1781 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1783 if (chip->verify_buf(mtd, buf, mtd->writesize))
1790 * nand_fill_oob - [Internal] Transfer client buffer to oob
1791 * @chip: nand chip structure
1792 * @oob: oob data buffer
1793 * @ops: oob ops structure
1795 static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob,
1796 struct mtd_oob_ops *ops)
1798 size_t len = ops->ooblen;
1804 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
1807 case MTD_OOB_AUTO: {
1808 struct nand_oobfree *free = chip->ecc.layout->oobfree;
1809 uint32_t boffs = 0, woffs = ops->ooboffs;
1812 for(; free->length && len; free++, len -= bytes) {
1813 /* Write request not from offset 0 ? */
1814 if (unlikely(woffs)) {
1815 if (woffs >= free->length) {
1816 woffs -= free->length;
1819 boffs = free->offset + woffs;
1820 bytes = min_t(size_t, len,
1821 (free->length - woffs));
1824 bytes = min_t(size_t, len, free->length);
1825 boffs = free->offset;
1827 memcpy(chip->oob_poi + boffs, oob, bytes);
1838 #define NOTALIGNED(x) (x & (chip->subpagesize - 1)) != 0
1841 * nand_do_write_ops - [Internal] NAND write with ECC
1842 * @mtd: MTD device structure
1843 * @to: offset to write to
1844 * @ops: oob operations description structure
1846 * NAND write with ECC
1848 static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1849 struct mtd_oob_ops *ops)
1851 int chipnr, realpage, page, blockmask, column;
1852 struct nand_chip *chip = mtd->priv;
1853 uint32_t writelen = ops->len;
1854 uint8_t *oob = ops->oobbuf;
1855 uint8_t *buf = ops->datbuf;
1862 column = to & (mtd->writesize - 1);
1863 subpage = column || (writelen & (mtd->writesize - 1));
1868 chipnr = (int)(to >> chip->chip_shift);
1869 chip->select_chip(mtd, chipnr);
1871 /* Check, if it is write protected */
1872 if (nand_check_wp(mtd)) {
1873 printk (KERN_NOTICE "nand_do_write_ops: Device is write protected\n");
1877 realpage = (int)(to >> chip->page_shift);
1878 page = realpage & chip->pagemask;
1879 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
1881 /* Invalidate the page cache, when we write to the cached page */
1882 if (to <= (chip->pagebuf << chip->page_shift) &&
1883 (chip->pagebuf << chip->page_shift) < (to + ops->len))
1886 /* If we're not given explicit OOB data, let it be 0xFF */
1888 memset(chip->oob_poi, 0xff, mtd->oobsize);
1893 int bytes = mtd->writesize;
1894 int cached = writelen > bytes && page != blockmask;
1895 uint8_t *wbuf = buf;
1897 /* Partial page write ? */
1898 if (unlikely(column || writelen < (mtd->writesize - 1))) {
1900 bytes = min_t(int, bytes - column, (int) writelen);
1902 memset(chip->buffers->databuf, 0xff, mtd->writesize);
1903 memcpy(&chip->buffers->databuf[column], buf, bytes);
1904 wbuf = chip->buffers->databuf;
1908 oob = nand_fill_oob(chip, oob, ops);
1910 ret = chip->write_page(mtd, chip, wbuf, page, cached,
1911 (ops->mode == MTD_OOB_RAW));
1923 page = realpage & chip->pagemask;
1924 /* Check, if we cross a chip boundary */
1927 chip->select_chip(mtd, -1);
1928 chip->select_chip(mtd, chipnr);
1932 ops->retlen = ops->len - writelen;
1934 ops->oobretlen = ops->ooblen;
1939 * nand_write - [MTD Interface] NAND write with ECC
1940 * @mtd: MTD device structure
1941 * @to: offset to write to
1942 * @len: number of bytes to write
1943 * @retlen: pointer to variable to store the number of written bytes
1944 * @buf: the data to write
1946 * NAND write with ECC
1948 static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
1949 size_t *retlen, const uint8_t *buf)
1951 struct nand_chip *chip = mtd->priv;
1954 /* Do not allow writes past end of device */
1955 if ((to + len) > mtd->size)
1960 nand_get_device(chip, mtd, FL_WRITING);
1962 chip->ops.len = len;
1963 chip->ops.datbuf = (uint8_t *)buf;
1964 chip->ops.oobbuf = NULL;
1966 ret = nand_do_write_ops(mtd, to, &chip->ops);
1968 *retlen = chip->ops.retlen;
1970 nand_release_device(mtd);
1976 * nand_do_write_oob - [MTD Interface] NAND write out-of-band
1977 * @mtd: MTD device structure
1978 * @to: offset to write to
1979 * @ops: oob operation description structure
1981 * NAND write out-of-band
1983 static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
1984 struct mtd_oob_ops *ops)
1986 int chipnr, page, status, len;
1987 struct nand_chip *chip = mtd->priv;
1989 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n",
1990 (unsigned int)to, (int)ops->ooblen);
1992 if (ops->mode == MTD_OOB_AUTO)
1993 len = chip->ecc.layout->oobavail;
1997 /* Do not allow write past end of page */
1998 if ((ops->ooboffs + ops->ooblen) > len) {
1999 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_write_oob: "
2000 "Attempt to write past end of page\n");
2004 if (unlikely(ops->ooboffs >= len)) {
2005 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2006 "Attempt to start write outside oob\n");
2010 /* Do not allow reads past end of device */
2011 if (unlikely(to >= mtd->size ||
2012 ops->ooboffs + ops->ooblen >
2013 ((mtd->size >> chip->page_shift) -
2014 (to >> chip->page_shift)) * len)) {
2015 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2016 "Attempt write beyond end of device\n");
2020 chipnr = (int)(to >> chip->chip_shift);
2021 chip->select_chip(mtd, chipnr);
2023 /* Shift to get page */
2024 page = (int)(to >> chip->page_shift);
2027 * Reset the chip. Some chips (like the Toshiba TC5832DC found in one
2028 * of my DiskOnChip 2000 test units) will clear the whole data page too
2029 * if we don't do this. I have no clue why, but I seem to have 'fixed'
2030 * it in the doc2000 driver in August 1999. dwmw2.
2032 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2034 /* Check, if it is write protected */
2035 if (nand_check_wp(mtd))
2038 /* Invalidate the page cache, if we write to the cached page */
2039 if (page == chip->pagebuf)
2042 memset(chip->oob_poi, 0xff, mtd->oobsize);
2043 nand_fill_oob(chip, ops->oobbuf, ops);
2044 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2045 memset(chip->oob_poi, 0xff, mtd->oobsize);
2050 ops->oobretlen = ops->ooblen;
2056 * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
2057 * @mtd: MTD device structure
2058 * @to: offset to write to
2059 * @ops: oob operation description structure
2061 static int nand_write_oob(struct mtd_info *mtd, loff_t to,
2062 struct mtd_oob_ops *ops)
2064 struct nand_chip *chip = mtd->priv;
2065 int ret = -ENOTSUPP;
2069 /* Do not allow writes past end of device */
2070 if (ops->datbuf && (to + ops->len) > mtd->size) {
2071 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_read_oob: "
2072 "Attempt read beyond end of device\n");
2076 nand_get_device(chip, mtd, FL_WRITING);
2089 ret = nand_do_write_oob(mtd, to, ops);
2091 ret = nand_do_write_ops(mtd, to, ops);
2094 nand_release_device(mtd);
2099 * single_erease_cmd - [GENERIC] NAND standard block erase command function
2100 * @mtd: MTD device structure
2101 * @page: the page address of the block which will be erased
2103 * Standard erase command for NAND chips
2105 static void single_erase_cmd(struct mtd_info *mtd, int page)
2107 struct nand_chip *chip = mtd->priv;
2108 /* Send commands to erase a block */
2109 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2110 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2114 * multi_erease_cmd - [GENERIC] AND specific block erase command function
2115 * @mtd: MTD device structure
2116 * @page: the page address of the block which will be erased
2118 * AND multi block erase command function
2119 * Erase 4 consecutive blocks
2121 static void multi_erase_cmd(struct mtd_info *mtd, int page)
2123 struct nand_chip *chip = mtd->priv;
2124 /* Send commands to erase a block */
2125 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2126 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2127 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++);
2128 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
2129 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
2133 * nand_erase - [MTD Interface] erase block(s)
2134 * @mtd: MTD device structure
2135 * @instr: erase instruction
2137 * Erase one ore more blocks
2139 static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
2141 return nand_erase_nand(mtd, instr, 0);
2144 #define BBT_PAGE_MASK 0xffffff3f
2146 * nand_erase_nand - [Internal] erase block(s)
2147 * @mtd: MTD device structure
2148 * @instr: erase instruction
2149 * @allowbbt: allow erasing the bbt area
2151 * Erase one ore more blocks
2153 int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2156 int page, status, pages_per_block, ret, chipnr;
2157 struct nand_chip *chip = mtd->priv;
2158 loff_t rewrite_bbt[CONFIG_SYS_NAND_MAX_CHIPS] = {0};
2159 unsigned int bbt_masked_page = 0xffffffff;
2162 MTDDEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%012llx, "
2163 "len = %llu\n", (unsigned long long) instr->addr,
2164 (unsigned long long) instr->len);
2166 /* Start address must align on block boundary */
2167 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
2168 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n");
2172 /* Length must align on block boundary */
2173 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
2174 MTDDEBUG (MTD_DEBUG_LEVEL0,
2175 "nand_erase: Length not block aligned\n");
2179 /* Do not allow erase past end of device */
2180 if ((instr->len + instr->addr) > mtd->size) {
2181 MTDDEBUG (MTD_DEBUG_LEVEL0,
2182 "nand_erase: Erase past end of device\n");
2186 instr->fail_addr = 0xffffffff;
2188 /* Grab the lock and see if the device is available */
2189 nand_get_device(chip, mtd, FL_ERASING);
2191 /* Shift to get first page */
2192 page = (int)(instr->addr >> chip->page_shift);
2193 chipnr = (int)(instr->addr >> chip->chip_shift);
2195 /* Calculate pages in each block */
2196 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
2198 /* Select the NAND device */
2199 chip->select_chip(mtd, chipnr);
2201 /* Check, if it is write protected */
2202 if (nand_check_wp(mtd)) {
2203 MTDDEBUG (MTD_DEBUG_LEVEL0,
2204 "nand_erase: Device is write protected!!!\n");
2205 instr->state = MTD_ERASE_FAILED;
2210 * If BBT requires refresh, set the BBT page mask to see if the BBT
2211 * should be rewritten. Otherwise the mask is set to 0xffffffff which
2212 * can not be matched. This is also done when the bbt is actually
2213 * erased to avoid recusrsive updates
2215 if (chip->options & BBT_AUTO_REFRESH && !allowbbt)
2216 bbt_masked_page = chip->bbt_td->pages[chipnr] & BBT_PAGE_MASK;
2218 /* Loop through the pages */
2221 instr->state = MTD_ERASING;
2226 * heck if we have a bad block, we do not erase bad blocks !
2228 if (!instr->scrub && nand_block_checkbad(mtd, ((loff_t) page) <<
2229 chip->page_shift, 0, allowbbt)) {
2230 printk(KERN_WARNING "nand_erase: attempt to erase a "
2231 "bad block at page 0x%08x\n", page);
2232 instr->state = MTD_ERASE_FAILED;
2237 * Invalidate the page cache, if we erase the block which
2238 * contains the current cached page
2240 if (page <= chip->pagebuf && chip->pagebuf <
2241 (page + pages_per_block))
2244 chip->erase_cmd(mtd, page & chip->pagemask);
2246 status = chip->waitfunc(mtd, chip);
2249 * See if operation failed and additional status checks are
2252 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
2253 status = chip->errstat(mtd, chip, FL_ERASING,
2256 /* See if block erase succeeded */
2257 if (status & NAND_STATUS_FAIL) {
2258 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase: "
2259 "Failed erase, page 0x%08x\n", page);
2260 instr->state = MTD_ERASE_FAILED;
2261 instr->fail_addr = ((loff_t)page << chip->page_shift);
2266 * If BBT requires refresh, set the BBT rewrite flag to the
2269 if (bbt_masked_page != 0xffffffff &&
2270 (page & BBT_PAGE_MASK) == bbt_masked_page)
2271 rewrite_bbt[chipnr] =
2272 ((loff_t)page << chip->page_shift);
2274 /* Increment page address and decrement length */
2275 len -= (1 << chip->phys_erase_shift);
2276 page += pages_per_block;
2278 /* Check, if we cross a chip boundary */
2279 if (len && !(page & chip->pagemask)) {
2281 chip->select_chip(mtd, -1);
2282 chip->select_chip(mtd, chipnr);
2285 * If BBT requires refresh and BBT-PERCHIP, set the BBT
2286 * page mask to see if this BBT should be rewritten
2288 if (bbt_masked_page != 0xffffffff &&
2289 (chip->bbt_td->options & NAND_BBT_PERCHIP))
2290 bbt_masked_page = chip->bbt_td->pages[chipnr] &
2294 instr->state = MTD_ERASE_DONE;
2298 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
2300 /* Deselect and wake up anyone waiting on the device */
2301 nand_release_device(mtd);
2303 /* Do call back function */
2305 mtd_erase_callback(instr);
2308 * If BBT requires refresh and erase was successful, rewrite any
2309 * selected bad block tables
2311 if (bbt_masked_page == 0xffffffff || ret)
2314 for (chipnr = 0; chipnr < chip->numchips; chipnr++) {
2315 if (!rewrite_bbt[chipnr])
2317 /* update the BBT for chip */
2318 MTDDEBUG (MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt "
2319 "(%d:0x%0llx 0x%0x)\n", chipnr, rewrite_bbt[chipnr],
2320 chip->bbt_td->pages[chipnr]);
2321 nand_update_bbt(mtd, rewrite_bbt[chipnr]);
2324 /* Return more or less happy */
2329 * nand_sync - [MTD Interface] sync
2330 * @mtd: MTD device structure
2332 * Sync is actually a wait for chip ready function
2334 static void nand_sync(struct mtd_info *mtd)
2336 struct nand_chip *chip = mtd->priv;
2338 MTDDEBUG (MTD_DEBUG_LEVEL3, "nand_sync: called\n");
2340 /* Grab the lock and see if the device is available */
2341 nand_get_device(chip, mtd, FL_SYNCING);
2342 /* Release it and go back */
2343 nand_release_device(mtd);
2347 * nand_block_isbad - [MTD Interface] Check if block at offset is bad
2348 * @mtd: MTD device structure
2349 * @offs: offset relative to mtd start
2351 static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
2353 /* Check for invalid offset */
2354 if (offs > mtd->size)
2357 return nand_block_checkbad(mtd, offs, 1, 0);
2361 * nand_block_markbad - [MTD Interface] Mark block at the given offset as bad
2362 * @mtd: MTD device structure
2363 * @ofs: offset relative to mtd start
2365 static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
2367 struct nand_chip *chip = mtd->priv;
2370 if ((ret = nand_block_isbad(mtd, ofs))) {
2371 /* If it was bad already, return success and do nothing. */
2377 return chip->block_markbad(mtd, ofs);
2381 * Set default functions
2383 static void nand_set_defaults(struct nand_chip *chip, int busw)
2385 /* check for proper chip_delay setup, set 20us if not */
2386 if (!chip->chip_delay)
2387 chip->chip_delay = 20;
2389 /* check, if a user supplied command function given */
2390 if (chip->cmdfunc == NULL)
2391 chip->cmdfunc = nand_command;
2393 /* check, if a user supplied wait function given */
2394 if (chip->waitfunc == NULL)
2395 chip->waitfunc = nand_wait;
2397 if (!chip->select_chip)
2398 chip->select_chip = nand_select_chip;
2399 if (!chip->read_byte)
2400 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
2401 if (!chip->read_word)
2402 chip->read_word = nand_read_word;
2403 if (!chip->block_bad)
2404 chip->block_bad = nand_block_bad;
2405 if (!chip->block_markbad)
2406 chip->block_markbad = nand_default_block_markbad;
2407 if (!chip->write_buf)
2408 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
2409 if (!chip->read_buf)
2410 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
2411 if (!chip->verify_buf)
2412 chip->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf;
2413 if (!chip->scan_bbt)
2414 chip->scan_bbt = nand_default_bbt;
2415 if (!chip->controller)
2416 chip->controller = &chip->hwcontrol;
2419 #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
2420 static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
2426 for (i = 0; i < 8; i++)
2427 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2434 * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise
2436 static int nand_flash_detect_onfi(struct mtd_info *mtd,
2437 struct nand_chip *chip,
2440 struct nand_onfi_params *p = &chip->onfi_params;
2444 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
2445 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
2446 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
2449 printk(KERN_INFO "ONFI flash detected\n");
2450 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
2451 for (i = 0; i < 3; i++) {
2452 chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
2453 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
2454 le16_to_cpu(p->crc)) {
2455 printk(KERN_INFO "ONFI param page %d valid\n", i);
2464 val = le16_to_cpu(p->revision);
2466 chip->onfi_version = 23;
2467 else if (val & (1 << 4))
2468 chip->onfi_version = 22;
2469 else if (val & (1 << 3))
2470 chip->onfi_version = 21;
2471 else if (val & (1 << 2))
2472 chip->onfi_version = 20;
2473 else if (val & (1 << 1))
2474 chip->onfi_version = 10;
2476 chip->onfi_version = 0;
2478 if (!chip->onfi_version) {
2479 printk(KERN_INFO "%s: unsupported ONFI "
2480 "version: %d\n", __func__, val);
2485 mtd->name = p->model;
2487 mtd->writesize = le32_to_cpu(p->byte_per_page);
2488 mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
2489 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
2490 chip->chipsize = (uint64_t)le32_to_cpu(p->blocks_per_lun) * mtd->erasesize;
2492 if (le16_to_cpu(p->features) & 1)
2493 *busw = NAND_BUSWIDTH_16;
2498 static inline int nand_flash_detect_onfi(struct mtd_info *mtd,
2499 struct nand_chip *chip,
2506 static void nand_flash_detect_non_onfi(struct mtd_info *mtd,
2507 struct nand_chip *chip,
2508 const struct nand_flash_dev *type,
2511 /* Newer devices have all the information in additional id bytes */
2512 if (!type->pagesize) {
2514 /* The 3rd id byte holds MLC / multichip data */
2515 chip->cellinfo = chip->read_byte(mtd);
2516 /* The 4th id byte is the important one */
2517 extid = chip->read_byte(mtd);
2519 mtd->writesize = 1024 << (extid & 0x3);
2522 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
2524 /* Calc blocksize. Blocksize is multiples of 64KiB */
2525 mtd->erasesize = (64 * 1024) << (extid & 0x03);
2527 /* Get buswidth information */
2528 *busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
2532 * Old devices have chip data hardcoded in the device id table
2534 mtd->erasesize = type->erasesize;
2535 mtd->writesize = type->pagesize;
2536 mtd->oobsize = mtd->writesize / 32;
2537 *busw = type->options & NAND_BUSWIDTH_16;
2542 * Get the flash and manufacturer id and lookup if the type is supported
2544 static const struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2545 struct nand_chip *chip,
2547 int *maf_id, int *dev_id,
2548 const struct nand_flash_dev *type)
2551 int tmp_id, tmp_manf;
2553 /* Select the device */
2554 chip->select_chip(mtd, 0);
2557 * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx)
2560 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2562 /* Send the command for reading device ID */
2563 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2565 /* Read manufacturer and device IDs */
2566 *maf_id = chip->read_byte(mtd);
2567 *dev_id = chip->read_byte(mtd);
2569 /* Try again to make sure, as some systems the bus-hold or other
2570 * interface concerns can cause random data which looks like a
2571 * possibly credible NAND flash to appear. If the two results do
2572 * not match, ignore the device completely.
2575 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2577 /* Read manufacturer and device IDs */
2579 tmp_manf = chip->read_byte(mtd);
2580 tmp_id = chip->read_byte(mtd);
2582 if (tmp_manf != *maf_id || tmp_id != *dev_id) {
2583 printk(KERN_INFO "%s: second ID read did not match "
2584 "%02x,%02x against %02x,%02x\n", __func__,
2585 *maf_id, *dev_id, tmp_manf, tmp_id);
2586 return ERR_PTR(-ENODEV);
2590 type = nand_flash_ids;
2592 for (; type->name != NULL; type++)
2593 if (*dev_id == type->id)
2597 /* supress warning if there is no nand */
2598 if (*maf_id != 0x00 && *maf_id != 0xff &&
2599 *dev_id != 0x00 && *dev_id != 0xff)
2600 printk(KERN_INFO "%s: unknown NAND device: "
2601 "Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
2602 __func__, *maf_id, *dev_id);
2603 return ERR_PTR(-ENODEV);
2607 mtd->name = type->name;
2609 chip->chipsize = (uint64_t)type->chipsize << 20;
2610 chip->onfi_version = 0;
2612 ret = nand_flash_detect_onfi(mtd, chip, &busw);
2614 nand_flash_detect_non_onfi(mtd, chip, type, &busw);
2616 /* Get chip options, preserve non chip based options */
2617 chip->options &= ~NAND_CHIPOPTIONS_MSK;
2618 chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
2621 * Set chip as a default. Board drivers can override it, if necessary
2623 chip->options |= NAND_NO_AUTOINCR;
2625 /* Try to identify manufacturer */
2626 for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
2627 if (nand_manuf_ids[maf_idx].id == *maf_id)
2632 * Check, if buswidth is correct. Hardware drivers should set
2635 if (busw != (chip->options & NAND_BUSWIDTH_16)) {
2636 printk(KERN_INFO "NAND device: Manufacturer ID:"
2637 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
2638 *dev_id, nand_manuf_ids[maf_idx].name, mtd->name);
2639 printk(KERN_WARNING "NAND bus width %d instead %d bit\n",
2640 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8,
2642 return ERR_PTR(-EINVAL);
2645 /* Calculate the address shift from the page size */
2646 chip->page_shift = ffs(mtd->writesize) - 1;
2647 /* Convert chipsize to number of pages per chip -1. */
2648 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
2650 chip->bbt_erase_shift = chip->phys_erase_shift =
2651 ffs(mtd->erasesize) - 1;
2652 if (chip->chipsize & 0xffffffff)
2653 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
2655 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)) + 31;
2657 /* Set the bad block position */
2658 chip->badblockpos = mtd->writesize > 512 ?
2659 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2661 /* Check if chip is a not a samsung device. Do not clear the
2662 * options for chips which are not having an extended id.
2664 if (*maf_id != NAND_MFR_SAMSUNG && !type->pagesize)
2665 chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
2667 /* Check for AND chips with 4 page planes */
2668 if (chip->options & NAND_4PAGE_ARRAY)
2669 chip->erase_cmd = multi_erase_cmd;
2671 chip->erase_cmd = single_erase_cmd;
2673 /* Do not replace user supplied command function ! */
2674 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
2675 chip->cmdfunc = nand_command_lp;
2677 MTDDEBUG (MTD_DEBUG_LEVEL0, "NAND device: Manufacturer ID:"
2678 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id, *dev_id,
2679 nand_manuf_ids[maf_idx].name, type->name);
2685 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2686 * @mtd: MTD device structure
2687 * @maxchips: Number of chips to scan for
2688 * @table: Alternative NAND ID table
2690 * This is the first phase of the normal nand_scan() function. It
2691 * reads the flash ID and sets up MTD fields accordingly.
2693 * The mtd->owner field must be set to the module of the caller.
2695 int nand_scan_ident(struct mtd_info *mtd, int maxchips,
2696 const struct nand_flash_dev *table)
2698 int i, busw, nand_maf_id, nand_dev_id;
2699 struct nand_chip *chip = mtd->priv;
2700 const struct nand_flash_dev *type;
2702 /* Get buswidth to select the correct functions */
2703 busw = chip->options & NAND_BUSWIDTH_16;
2704 /* Set the default functions */
2705 nand_set_defaults(chip, busw);
2707 /* Read the flash type */
2708 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id, &nand_dev_id, table);
2711 #ifndef CONFIG_SYS_NAND_QUIET_TEST
2712 printk(KERN_WARNING "No NAND device found!!!\n");
2714 chip->select_chip(mtd, -1);
2715 return PTR_ERR(type);
2718 /* Check for a chip array */
2719 for (i = 1; i < maxchips; i++) {
2720 chip->select_chip(mtd, i);
2721 /* See comment in nand_get_flash_type for reset */
2722 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
2723 /* Send the command for reading device ID */
2724 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
2725 /* Read manufacturer and device IDs */
2726 if (nand_maf_id != chip->read_byte(mtd) ||
2727 nand_dev_id != chip->read_byte(mtd))
2732 printk(KERN_INFO "%d NAND chips detected\n", i);
2735 /* Store the number of chips and calc total size for mtd */
2737 mtd->size = i * chip->chipsize;
2744 * nand_scan_tail - [NAND Interface] Scan for the NAND device
2745 * @mtd: MTD device structure
2747 * This is the second phase of the normal nand_scan() function. It
2748 * fills out all the uninitialized function pointers with the defaults
2749 * and scans for a bad block table if appropriate.
2751 int nand_scan_tail(struct mtd_info *mtd)
2754 struct nand_chip *chip = mtd->priv;
2756 if (!(chip->options & NAND_OWN_BUFFERS))
2757 chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
2761 /* Set the internal oob buffer location, just after the page data */
2762 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
2765 * If no default placement scheme is given, select an appropriate one
2767 if (!chip->ecc.layout && (chip->ecc.mode != NAND_ECC_SOFT_BCH)) {
2768 switch (mtd->oobsize) {
2770 chip->ecc.layout = &nand_oob_8;
2773 chip->ecc.layout = &nand_oob_16;
2776 chip->ecc.layout = &nand_oob_64;
2779 chip->ecc.layout = &nand_oob_128;
2782 printk(KERN_WARNING "No oob scheme defined for "
2783 "oobsize %d\n", mtd->oobsize);
2787 if (!chip->write_page)
2788 chip->write_page = nand_write_page;
2791 * check ECC mode, default to software if 3byte/512byte hardware ECC is
2792 * selected and we have 256 byte pagesize fallback to software ECC
2795 switch (chip->ecc.mode) {
2796 case NAND_ECC_HW_OOB_FIRST:
2797 /* Similar to NAND_ECC_HW, but a separate read_page handle */
2798 if (!chip->ecc.calculate || !chip->ecc.correct ||
2800 printk(KERN_WARNING "No ECC functions supplied, "
2801 "Hardware ECC not possible\n");
2804 if (!chip->ecc.read_page)
2805 chip->ecc.read_page = nand_read_page_hwecc_oob_first;
2808 /* Use standard hwecc read page function ? */
2809 if (!chip->ecc.read_page)
2810 chip->ecc.read_page = nand_read_page_hwecc;
2811 if (!chip->ecc.write_page)
2812 chip->ecc.write_page = nand_write_page_hwecc;
2813 if (!chip->ecc.read_page_raw)
2814 chip->ecc.read_page_raw = nand_read_page_raw;
2815 if (!chip->ecc.write_page_raw)
2816 chip->ecc.write_page_raw = nand_write_page_raw;
2817 if (!chip->ecc.read_oob)
2818 chip->ecc.read_oob = nand_read_oob_std;
2819 if (!chip->ecc.write_oob)
2820 chip->ecc.write_oob = nand_write_oob_std;
2822 case NAND_ECC_HW_SYNDROME:
2823 if ((!chip->ecc.calculate || !chip->ecc.correct ||
2824 !chip->ecc.hwctl) &&
2825 (!chip->ecc.read_page ||
2826 chip->ecc.read_page == nand_read_page_hwecc ||
2827 !chip->ecc.write_page ||
2828 chip->ecc.write_page == nand_write_page_hwecc)) {
2829 printk(KERN_WARNING "No ECC functions supplied, "
2830 "Hardware ECC not possible\n");
2833 /* Use standard syndrome read/write page function ? */
2834 if (!chip->ecc.read_page)
2835 chip->ecc.read_page = nand_read_page_syndrome;
2836 if (!chip->ecc.write_page)
2837 chip->ecc.write_page = nand_write_page_syndrome;
2838 if (!chip->ecc.read_page_raw)
2839 chip->ecc.read_page_raw = nand_read_page_raw_syndrome;
2840 if (!chip->ecc.write_page_raw)
2841 chip->ecc.write_page_raw = nand_write_page_raw_syndrome;
2842 if (!chip->ecc.read_oob)
2843 chip->ecc.read_oob = nand_read_oob_syndrome;
2844 if (!chip->ecc.write_oob)
2845 chip->ecc.write_oob = nand_write_oob_syndrome;
2847 if (mtd->writesize >= chip->ecc.size)
2849 printk(KERN_WARNING "%d byte HW ECC not possible on "
2850 "%d byte page size, fallback to SW ECC\n",
2851 chip->ecc.size, mtd->writesize);
2852 chip->ecc.mode = NAND_ECC_SOFT;
2855 chip->ecc.calculate = nand_calculate_ecc;
2856 chip->ecc.correct = nand_correct_data;
2857 chip->ecc.read_page = nand_read_page_swecc;
2858 chip->ecc.read_subpage = nand_read_subpage;
2859 chip->ecc.write_page = nand_write_page_swecc;
2860 chip->ecc.read_page_raw = nand_read_page_raw;
2861 chip->ecc.write_page_raw = nand_write_page_raw;
2862 chip->ecc.read_oob = nand_read_oob_std;
2863 chip->ecc.write_oob = nand_write_oob_std;
2864 chip->ecc.size = 256;
2865 chip->ecc.bytes = 3;
2868 case NAND_ECC_SOFT_BCH:
2869 if (!mtd_nand_has_bch()) {
2870 printk(KERN_WARNING "CONFIG_MTD_ECC_BCH not enabled\n");
2873 chip->ecc.calculate = nand_bch_calculate_ecc;
2874 chip->ecc.correct = nand_bch_correct_data;
2875 chip->ecc.read_page = nand_read_page_swecc;
2876 chip->ecc.read_subpage = nand_read_subpage;
2877 chip->ecc.write_page = nand_write_page_swecc;
2878 chip->ecc.read_page_raw = nand_read_page_raw;
2879 chip->ecc.write_page_raw = nand_write_page_raw;
2880 chip->ecc.read_oob = nand_read_oob_std;
2881 chip->ecc.write_oob = nand_write_oob_std;
2883 * Board driver should supply ecc.size and ecc.bytes values to
2884 * select how many bits are correctable; see nand_bch_init()
2886 * Otherwise, default to 4 bits for large page devices
2888 if (!chip->ecc.size && (mtd->oobsize >= 64)) {
2889 chip->ecc.size = 512;
2890 chip->ecc.bytes = 7;
2892 chip->ecc.priv = nand_bch_init(mtd,
2896 if (!chip->ecc.priv)
2897 printk(KERN_WARNING "BCH ECC initialization failed!\n");
2902 printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. "
2903 "This is not recommended !!\n");
2904 chip->ecc.read_page = nand_read_page_raw;
2905 chip->ecc.write_page = nand_write_page_raw;
2906 chip->ecc.read_oob = nand_read_oob_std;
2907 chip->ecc.read_page_raw = nand_read_page_raw;
2908 chip->ecc.write_page_raw = nand_write_page_raw;
2909 chip->ecc.write_oob = nand_write_oob_std;
2910 chip->ecc.size = mtd->writesize;
2911 chip->ecc.bytes = 0;
2915 printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n",
2921 * The number of bytes available for a client to place data into
2922 * the out of band area
2924 chip->ecc.layout->oobavail = 0;
2925 for (i = 0; chip->ecc.layout->oobfree[i].length
2926 && i < ARRAY_SIZE(chip->ecc.layout->oobfree); i++)
2927 chip->ecc.layout->oobavail +=
2928 chip->ecc.layout->oobfree[i].length;
2929 mtd->oobavail = chip->ecc.layout->oobavail;
2932 * Set the number of read / write steps for one page depending on ECC
2935 chip->ecc.steps = mtd->writesize / chip->ecc.size;
2936 if(chip->ecc.steps * chip->ecc.size != mtd->writesize) {
2937 printk(KERN_WARNING "Invalid ecc parameters\n");
2940 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
2943 * Allow subpage writes up to ecc.steps. Not possible for MLC
2946 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2947 !(chip->cellinfo & NAND_CI_CELLTYPE_MSK)) {
2948 switch(chip->ecc.steps) {
2950 mtd->subpage_sft = 1;
2955 mtd->subpage_sft = 2;
2959 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
2961 /* Initialize state */
2962 chip->state = FL_READY;
2964 /* De-select the device */
2965 chip->select_chip(mtd, -1);
2967 /* Invalidate the pagebuffer reference */
2970 /* Fill in remaining MTD driver data */
2971 mtd->type = MTD_NANDFLASH;
2972 mtd->flags = MTD_CAP_NANDFLASH;
2973 mtd->erase = nand_erase;
2975 mtd->unpoint = NULL;
2976 mtd->read = nand_read;
2977 mtd->write = nand_write;
2978 mtd->read_oob = nand_read_oob;
2979 mtd->write_oob = nand_write_oob;
2980 mtd->sync = nand_sync;
2983 mtd->block_isbad = nand_block_isbad;
2984 mtd->block_markbad = nand_block_markbad;
2986 /* propagate ecc.layout to mtd_info */
2987 mtd->ecclayout = chip->ecc.layout;
2989 /* Check, if we should skip the bad block table scan */
2990 if (chip->options & NAND_SKIP_BBTSCAN)
2991 chip->options |= NAND_BBT_SCANNED;
2997 * nand_scan - [NAND Interface] Scan for the NAND device
2998 * @mtd: MTD device structure
2999 * @maxchips: Number of chips to scan for
3001 * This fills out all the uninitialized function pointers
3002 * with the defaults.
3003 * The flash ID is read and the mtd/chip structures are
3004 * filled with the appropriate values.
3005 * The mtd->owner field must be set to the module of the caller
3008 int nand_scan(struct mtd_info *mtd, int maxchips)
3012 ret = nand_scan_ident(mtd, maxchips, NULL);
3014 ret = nand_scan_tail(mtd);
3019 * nand_release - [NAND Interface] Free resources held by the NAND device
3020 * @mtd: MTD device structure
3022 void nand_release(struct mtd_info *mtd)
3024 struct nand_chip *chip = mtd->priv;
3026 if (chip->ecc.mode == NAND_ECC_SOFT_BCH)
3027 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
3029 #ifdef CONFIG_MTD_PARTITIONS
3030 /* Deregister partitions */
3031 del_mtd_partitions(mtd);
3034 /* Free bad block table memory */
3036 if (!(chip->options & NAND_OWN_BUFFERS))
3037 kfree(chip->buffers);