2 * drivers/nand/nand_util.c
4 * Copyright (C) 2006 by Weiss-Electronic GmbH.
7 * @author: Guido Classen <clagix@gmail.com>
8 * @descr: NAND Flash support
9 * @references: borrowed heavily from Linux mtd-utils code:
10 * flash_eraseall.c by Arcom Control System Ltd
11 * nandwrite.c by Steven J. Hill (sjhill@realitydiluted.com)
12 * and Thomas Gleixner (tglx@linutronix.de)
14 * See file CREDITS for list of people who contributed to this
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License version
19 * 2 as published by the Free Software Foundation.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
35 #if (CONFIG_COMMANDS & CFG_CMD_NAND) && !defined(CFG_NAND_LEGACY)
42 #include <jffs2/jffs2.h>
44 typedef struct erase_info erase_info_t;
45 typedef struct mtd_info mtd_info_t;
47 /* support only for native endian JFFS2 */
48 #define cpu_to_je16(x) (x)
49 #define cpu_to_je32(x) (x)
51 /*****************************************************************************/
52 static int nand_block_bad_scrub(struct mtd_info *mtd, loff_t ofs, int getchip)
58 * nand_erase_opts: - erase NAND flash with support for various options
61 * @param meminfo NAND device to erase
62 * @param opts options, @see struct nand_erase_options
63 * @return 0 in case of success
65 * This code is ported from flash_eraseall.c from Linux mtd utils by
66 * Arcom Control System Ltd.
68 int nand_erase_opts(nand_info_t *meminfo, const nand_erase_options_t *opts)
70 struct jffs2_unknown_node cleanmarker;
78 int percent_complete = -1;
79 int (*nand_block_bad_old)(struct mtd_info *, loff_t, int) = NULL;
80 const char *mtd_device = meminfo->name;
82 memset(&erase, 0, sizeof(erase));
85 erase.len = meminfo->erasesize;
86 if (opts->offset == 0 && opts->length == 0) {
87 /* erase complete chip */
89 erase_length = meminfo->size;
91 /* erase specified region */
92 erase.addr = opts->offset;
93 erase_length = opts->length;
96 isNAND = meminfo->type == MTD_NANDFLASH ? 1 : 0;
99 cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
100 cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
102 struct nand_oobinfo *oobinfo = &meminfo->oobinfo;
104 /* check for autoplacement */
105 if (oobinfo->useecc == MTD_NANDECC_AUTOPLACE) {
106 /* get the position of the free bytes */
107 if (!oobinfo->oobfree[0][1]) {
108 printf(" Eeep. Autoplacement selected "
109 "and no empty space in oob\n");
112 clmpos = oobinfo->oobfree[0][0];
113 clmlen = oobinfo->oobfree[0][1];
118 switch (meminfo->oobsize) {
134 cleanmarker.totlen = cpu_to_je32(8);
137 cpu_to_je32(sizeof(struct jffs2_unknown_node));
139 cleanmarker.hdr_crc = cpu_to_je32(
140 crc32_no_comp(0, (unsigned char *) &cleanmarker,
141 sizeof(struct jffs2_unknown_node) - 4));
144 /* scrub option allows to erase badblock. To prevent internal
145 * check from erase() method, set block check method to dummy
146 * and disable bad block table while erasing.
149 struct nand_chip *priv_nand = meminfo->priv;
151 nand_block_bad_old = priv_nand->block_bad;
152 priv_nand->block_bad = nand_block_bad_scrub;
153 /* we don't need the bad block table anymore...
154 * after scrub, there are no bad blocks left!
156 if (priv_nand->bbt) {
157 kfree(priv_nand->bbt);
159 priv_nand->bbt = NULL;
163 erase.addr < opts->offset + erase_length;
164 erase.addr += meminfo->erasesize) {
168 if (!opts->scrub && bbtest) {
169 int ret = meminfo->block_isbad(meminfo, erase.addr);
172 printf("\rSkipping bad block at "
178 } else if (ret < 0) {
179 printf("\n%s: MTD get bad block failed: %d\n",
186 result = meminfo->erase(meminfo, &erase);
188 printf("\n%s: MTD Erase failure: %d\n",
193 /* format for JFFS2 ? */
196 /* write cleanmarker */
199 result = meminfo->write_oob(meminfo,
206 printf("\n%s: MTD writeoob failure: %d\n",
211 printf("\n%s: this erase routine only supports"
219 ((unsigned long long)
220 (erase.addr+meminfo->erasesize-opts->offset)
221 * 100 / erase_length);
223 /* output progress message only at whole percent
224 * steps to reduce the number of messages printed
225 * on (slow) serial consoles
227 if (percent != percent_complete) {
228 percent_complete = percent;
230 printf("\rErasing at 0x%x -- %3d%% complete.",
231 erase.addr, percent);
233 if (opts->jffs2 && result == 0)
234 printf(" Cleanmarker written at 0x%x.",
242 if (nand_block_bad_old) {
243 struct nand_chip *priv_nand = meminfo->priv;
245 priv_nand->block_bad = nand_block_bad_old;
246 priv_nand->scan_bbt(meminfo);
252 #define MAX_PAGE_SIZE 2048
253 #define MAX_OOB_SIZE 64
256 * buffer array used for writing data
258 static unsigned char data_buf[MAX_PAGE_SIZE];
259 static unsigned char oob_buf[MAX_OOB_SIZE];
261 /* OOB layouts to pass into the kernel as default */
262 static struct nand_oobinfo none_oobinfo = {
263 .useecc = MTD_NANDECC_OFF,
266 static struct nand_oobinfo jffs2_oobinfo = {
267 .useecc = MTD_NANDECC_PLACE,
269 .eccpos = { 0, 1, 2, 3, 6, 7 }
272 static struct nand_oobinfo yaffs_oobinfo = {
273 .useecc = MTD_NANDECC_PLACE,
275 .eccpos = { 8, 9, 10, 13, 14, 15}
278 static struct nand_oobinfo autoplace_oobinfo = {
279 .useecc = MTD_NANDECC_AUTOPLACE
283 * nand_write_opts: - write image to NAND flash with support for various options
285 * @param meminfo NAND device to erase
286 * @param opts write options (@see nand_write_options)
287 * @return 0 in case of success
289 * This code is ported from nandwrite.c from Linux mtd utils by
290 * Steven J. Hill and Thomas Gleixner.
292 int nand_write_opts(nand_info_t *meminfo, const nand_write_options_t *opts)
300 int oobinfochanged = 0;
301 int percent_complete = -1;
302 struct nand_oobinfo old_oobinfo;
303 ulong mtdoffset = opts->offset;
304 ulong erasesize_blockalign;
305 u_char *buffer = opts->buffer;
309 if (opts->pad && opts->writeoob) {
310 printf("Can't pad when oob data is present.\n");
314 /* set erasesize to specified number of blocks - to match
315 * jffs2 (virtual) block size */
316 if (opts->blockalign == 0) {
317 erasesize_blockalign = meminfo->erasesize;
319 erasesize_blockalign = meminfo->erasesize * opts->blockalign;
322 /* make sure device page sizes are valid */
323 if (!(meminfo->oobsize == 16 && meminfo->oobblock == 512)
324 && !(meminfo->oobsize == 8 && meminfo->oobblock == 256)
325 && !(meminfo->oobsize == 64 && meminfo->oobblock == 2048)) {
326 printf("Unknown flash (not normal NAND)\n");
330 /* read the current oob info */
331 memcpy(&old_oobinfo, &meminfo->oobinfo, sizeof(old_oobinfo));
333 /* write without ecc? */
335 memcpy(&meminfo->oobinfo, &none_oobinfo,
336 sizeof(meminfo->oobinfo));
341 if (opts->autoplace && (old_oobinfo.useecc != MTD_NANDECC_AUTOPLACE)) {
343 memcpy(&meminfo->oobinfo, &autoplace_oobinfo,
344 sizeof(meminfo->oobinfo));
348 /* force OOB layout for jffs2 or yaffs? */
349 if (opts->forcejffs2 || opts->forceyaffs) {
350 struct nand_oobinfo *oobsel =
351 opts->forcejffs2 ? &jffs2_oobinfo : &yaffs_oobinfo;
353 if (meminfo->oobsize == 8) {
354 if (opts->forceyaffs) {
355 printf("YAFSS cannot operate on "
356 "256 Byte page size\n");
359 /* Adjust number of ecc bytes */
360 jffs2_oobinfo.eccbytes = 3;
363 memcpy(&meminfo->oobinfo, oobsel, sizeof(meminfo->oobinfo));
366 /* get image length */
367 imglen = opts->length;
368 pagelen = meminfo->oobblock
369 + ((opts->writeoob != 0) ? meminfo->oobsize : 0);
371 /* check, if file is pagealigned */
372 if ((!opts->pad) && ((imglen % pagelen) != 0)) {
373 printf("Input block length is not page aligned\n");
377 /* check, if length fits into device */
378 if (((imglen / pagelen) * meminfo->oobblock)
379 > (meminfo->size - opts->offset)) {
380 printf("Image %d bytes, NAND page %d bytes, "
381 "OOB area %u bytes, device size %u bytes\n",
382 imglen, pagelen, meminfo->oobblock, meminfo->size);
383 printf("Input block does not fit into device\n");
390 /* get data from input and write to the device */
391 while (imglen && (mtdoffset < meminfo->size)) {
396 * new eraseblock, check for bad block(s). Stay in the
397 * loop to be sure if the offset changes because of
398 * a bad block, that the next block that will be
399 * written to is also checked. Thus avoiding errors if
400 * the block(s) after the skipped block(s) is also bad
401 * (number of blocks depending on the blockalign
403 while (blockstart != (mtdoffset & (~erasesize_blockalign+1))) {
404 blockstart = mtdoffset & (~erasesize_blockalign+1);
408 /* check all the blocks in an erase block for
411 int ret = meminfo->block_isbad(meminfo, offs);
414 printf("Bad block check failed\n");
420 printf("\rBad block at 0x%lx "
421 "in erase block from "
422 "0x%x will be skipped\n",
428 mtdoffset = blockstart
429 + erasesize_blockalign;
431 offs += erasesize_blockalign
433 } while (offs < blockstart + erasesize_blockalign);
436 readlen = meminfo->oobblock;
437 if (opts->pad && (imglen < readlen)) {
439 memset(data_buf + readlen, 0xff,
440 meminfo->oobblock - readlen);
443 /* read page data from input memory buffer */
444 memcpy(data_buf, buffer, readlen);
447 if (opts->writeoob) {
448 /* read OOB data from input memory block, exit
450 memcpy(oob_buf, buffer, meminfo->oobsize);
451 buffer += meminfo->oobsize;
453 /* write OOB data first, as ecc will be placed
455 result = meminfo->write_oob(meminfo,
463 printf("\nMTD writeoob failure: %d\n",
467 imglen -= meminfo->oobsize;
470 /* write out the page data */
471 result = meminfo->write(meminfo,
475 (unsigned char *) &data_buf);
478 printf("writing NAND page at offset 0x%lx failed\n",
486 ((unsigned long long)
487 (opts->length-imglen) * 100
489 /* output progress message only at whole percent
490 * steps to reduce the number of messages printed
491 * on (slow) serial consoles
493 if (percent != percent_complete) {
494 printf("\rWriting data at 0x%x "
495 "-- %3d%% complete.",
497 percent_complete = percent;
501 mtdoffset += meminfo->oobblock;
508 if (oobinfochanged) {
509 memcpy(&meminfo->oobinfo, &old_oobinfo,
510 sizeof(meminfo->oobinfo));
514 printf("Data did not fit into device, due to bad blocks\n");
523 * nand_read_opts: - read image from NAND flash with support for various options
525 * @param meminfo NAND device to erase
526 * @param opts read options (@see struct nand_read_options)
527 * @return 0 in case of success
530 int nand_read_opts(nand_info_t *meminfo, const nand_read_options_t *opts)
532 int imglen = opts->length;
536 int percent_complete = -1;
539 ulong mtdoffset = opts->offset;
540 u_char *buffer = opts->buffer;
543 /* make sure device page sizes are valid */
544 if (!(meminfo->oobsize == 16 && meminfo->oobblock == 512)
545 && !(meminfo->oobsize == 8 && meminfo->oobblock == 256)
546 && !(meminfo->oobsize == 64 && meminfo->oobblock == 2048)) {
547 printf("Unknown flash (not normal NAND)\n");
551 pagelen = meminfo->oobblock
552 + ((opts->readoob != 0) ? meminfo->oobsize : 0);
554 /* check, if length is not larger than device */
555 if (((imglen / pagelen) * meminfo->oobblock)
556 > (meminfo->size - opts->offset)) {
557 printf("Image %d bytes, NAND page %d bytes, "
558 "OOB area %u bytes, device size %u bytes\n",
559 imglen, pagelen, meminfo->oobblock, meminfo->size);
560 printf("Input block is larger than device\n");
567 /* get data from input and write to the device */
568 while (imglen && (mtdoffset < meminfo->size)) {
573 * new eraseblock, check for bad block(s). Stay in the
574 * loop to be sure if the offset changes because of
575 * a bad block, that the next block that will be
576 * written to is also checked. Thus avoiding errors if
577 * the block(s) after the skipped block(s) is also bad
578 * (number of blocks depending on the blockalign
580 while (blockstart != (mtdoffset & (~meminfo->erasesize+1))) {
581 blockstart = mtdoffset & (~meminfo->erasesize+1);
585 /* check all the blocks in an erase block for
588 int ret = meminfo->block_isbad(meminfo, offs);
591 printf("Bad block check failed\n");
597 printf("\rBad block at 0x%lx "
598 "in erase block from "
599 "0x%x will be skipped\n",
605 mtdoffset = blockstart
606 + meminfo->erasesize;
608 offs += meminfo->erasesize;
610 } while (offs < blockstart + meminfo->erasesize);
614 /* read page data to memory buffer */
615 result = meminfo->read(meminfo,
619 (unsigned char *) &data_buf);
622 printf("reading NAND page at offset 0x%lx failed\n",
627 if (imglen < readlen) {
631 memcpy(buffer, data_buf, readlen);
636 result = meminfo->read_oob(meminfo,
644 printf("\nMTD readoob failure: %d\n",
650 if (imglen < readlen) {
654 memcpy(buffer, oob_buf, readlen);
662 ((unsigned long long)
663 (opts->length-imglen) * 100
665 /* output progress message only at whole percent
666 * steps to reduce the number of messages printed
667 * on (slow) serial consoles
669 if (percent != percent_complete) {
671 printf("\rReading data from 0x%x "
672 "-- %3d%% complete.",
674 percent_complete = percent;
678 mtdoffset += meminfo->oobblock;
685 printf("Could not read entire image due to bad blocks\n");
693 /******************************************************************************
694 * Support for locking / unlocking operations of some NAND devices
695 *****************************************************************************/
697 #define NAND_CMD_LOCK 0x2a
698 #define NAND_CMD_LOCK_TIGHT 0x2c
699 #define NAND_CMD_UNLOCK1 0x23
700 #define NAND_CMD_UNLOCK2 0x24
701 #define NAND_CMD_LOCK_STATUS 0x7a
704 * nand_lock: Set all pages of NAND flash chip to the LOCK or LOCK-TIGHT
707 * @param meminfo nand mtd instance
708 * @param tight bring device in lock tight mode
710 * @return 0 on success, -1 in case of error
712 * The lock / lock-tight command only applies to the whole chip. To get some
713 * parts of the chip lock and others unlocked use the following sequence:
715 * - Lock all pages of the chip using nand_lock(mtd, 0) (or the lockpre pin)
716 * - Call nand_unlock() once for each consecutive area to be unlocked
717 * - If desired: Bring the chip to the lock-tight state using nand_lock(mtd, 1)
719 * If the device is in lock-tight state software can't change the
720 * current active lock/unlock state of all pages. nand_lock() / nand_unlock()
721 * calls will fail. It is only posible to leave lock-tight state by
722 * an hardware signal (low pulse on _WP pin) or by power down.
724 int nand_lock(nand_info_t *meminfo, int tight)
728 struct nand_chip *this = meminfo->priv;
730 /* select the NAND device */
731 this->select_chip(meminfo, 0);
733 this->cmdfunc(meminfo,
734 (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK),
737 /* call wait ready function */
738 status = this->waitfunc(meminfo, this, FL_WRITING);
740 /* see if device thinks it succeeded */
745 /* de-select the NAND device */
746 this->select_chip(meminfo, -1);
751 * nand_get_lock_status: - query current lock state from one page of NAND
754 * @param meminfo nand mtd instance
755 * @param offset page address to query (muss be page aligned!)
757 * @return -1 in case of error
759 * bitfield with the following combinations:
760 * NAND_LOCK_STATUS_TIGHT: page in tight state
761 * NAND_LOCK_STATUS_LOCK: page locked
762 * NAND_LOCK_STATUS_UNLOCK: page unlocked
765 int nand_get_lock_status(nand_info_t *meminfo, ulong offset)
770 struct nand_chip *this = meminfo->priv;
772 /* select the NAND device */
773 chipnr = (int)(offset >> this->chip_shift);
774 this->select_chip(meminfo, chipnr);
777 if ((offset & (meminfo->oobblock - 1)) != 0) {
778 printf ("nand_get_lock_status: "
779 "Start address must be beginning of "
785 /* check the Lock Status */
786 page = (int)(offset >> this->page_shift);
787 this->cmdfunc(meminfo, NAND_CMD_LOCK_STATUS, -1, page & this->pagemask);
789 ret = this->read_byte(meminfo) & (NAND_LOCK_STATUS_TIGHT
790 | NAND_LOCK_STATUS_LOCK
791 | NAND_LOCK_STATUS_UNLOCK);
794 /* de-select the NAND device */
795 this->select_chip(meminfo, -1);
800 * nand_unlock: - Unlock area of NAND pages
801 * only one consecutive area can be unlocked at one time!
803 * @param meminfo nand mtd instance
804 * @param start start byte address
805 * @param length number of bytes to unlock (must be a multiple of
806 * page size nand->oobblock)
808 * @return 0 on success, -1 in case of error
810 int nand_unlock(nand_info_t *meminfo, ulong start, ulong length)
816 struct nand_chip *this = meminfo->priv;
817 printf ("nand_unlock: start: %08x, length: %d!\n",
818 (int)start, (int)length);
820 /* select the NAND device */
821 chipnr = (int)(start >> this->chip_shift);
822 this->select_chip(meminfo, chipnr);
824 /* check the WP bit */
825 this->cmdfunc(meminfo, NAND_CMD_STATUS, -1, -1);
826 if ((this->read_byte(meminfo) & 0x80) == 0) {
827 printf ("nand_unlock: Device is write protected!\n");
832 if ((start & (meminfo->oobblock - 1)) != 0) {
833 printf ("nand_unlock: Start address must be beginning of "
839 if (length == 0 || (length & (meminfo->oobblock - 1)) != 0) {
840 printf ("nand_unlock: Length must be a multiple of nand page "
846 /* submit address of first page to unlock */
847 page = (int)(start >> this->page_shift);
848 this->cmdfunc(meminfo, NAND_CMD_UNLOCK1, -1, page & this->pagemask);
850 /* submit ADDRESS of LAST page to unlock */
851 page += (int)(length >> this->page_shift) - 1;
852 this->cmdfunc(meminfo, NAND_CMD_UNLOCK2, -1, page & this->pagemask);
854 /* call wait ready function */
855 status = this->waitfunc(meminfo, this, FL_WRITING);
856 /* see if device thinks it succeeded */
858 /* there was an error */
864 /* de-select the NAND device */
865 this->select_chip(meminfo, -1);
869 #endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) && !defined(CFG_NAND_LEGACY) */