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 erase.addr = opts->offset;
87 erase_length = opts->length;
89 isNAND = meminfo->type == MTD_NANDFLASH ? 1 : 0;
92 cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
93 cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
95 struct nand_oobinfo *oobinfo = &meminfo->oobinfo;
97 /* check for autoplacement */
98 if (oobinfo->useecc == MTD_NANDECC_AUTOPLACE) {
99 /* get the position of the free bytes */
100 if (!oobinfo->oobfree[0][1]) {
101 printf(" Eeep. Autoplacement selected "
102 "and no empty space in oob\n");
105 clmpos = oobinfo->oobfree[0][0];
106 clmlen = oobinfo->oobfree[0][1];
111 switch (meminfo->oobsize) {
127 cleanmarker.totlen = cpu_to_je32(8);
130 cpu_to_je32(sizeof(struct jffs2_unknown_node));
132 cleanmarker.hdr_crc = cpu_to_je32(
133 crc32_no_comp(0, (unsigned char *) &cleanmarker,
134 sizeof(struct jffs2_unknown_node) - 4));
137 /* scrub option allows to erase badblock. To prevent internal
138 * check from erase() method, set block check method to dummy
139 * and disable bad block table while erasing.
142 struct nand_chip *priv_nand = meminfo->priv;
144 nand_block_bad_old = priv_nand->block_bad;
145 priv_nand->block_bad = nand_block_bad_scrub;
146 /* we don't need the bad block table anymore...
147 * after scrub, there are no bad blocks left!
149 if (priv_nand->bbt) {
150 kfree(priv_nand->bbt);
152 priv_nand->bbt = NULL;
156 erase.addr < opts->offset + erase_length;
157 erase.addr += meminfo->erasesize) {
161 if (!opts->scrub && bbtest) {
162 int ret = meminfo->block_isbad(meminfo, erase.addr);
165 printf("\rSkipping bad block at "
171 } else if (ret < 0) {
172 printf("\n%s: MTD get bad block failed: %d\n",
179 result = meminfo->erase(meminfo, &erase);
181 printf("\n%s: MTD Erase failure: %d\n",
186 /* format for JFFS2 ? */
189 /* write cleanmarker */
192 result = meminfo->write_oob(meminfo,
199 printf("\n%s: MTD writeoob failure: %d\n",
204 printf("\n%s: this erase routine only supports"
212 ((unsigned long long)
213 (erase.addr+meminfo->erasesize-opts->offset)
214 * 100 / erase_length);
216 /* output progress message only at whole percent
217 * steps to reduce the number of messages printed
218 * on (slow) serial consoles
220 if (percent != percent_complete) {
221 percent_complete = percent;
223 printf("\rErasing at 0x%x -- %3d%% complete.",
224 erase.addr, percent);
226 if (opts->jffs2 && result == 0)
227 printf(" Cleanmarker written at 0x%x.",
235 if (nand_block_bad_old) {
236 struct nand_chip *priv_nand = meminfo->priv;
238 priv_nand->block_bad = nand_block_bad_old;
239 priv_nand->scan_bbt(meminfo);
245 #define MAX_PAGE_SIZE 2048
246 #define MAX_OOB_SIZE 64
249 * buffer array used for writing data
251 static unsigned char data_buf[MAX_PAGE_SIZE];
252 static unsigned char oob_buf[MAX_OOB_SIZE];
254 /* OOB layouts to pass into the kernel as default */
255 static struct nand_oobinfo none_oobinfo = {
256 .useecc = MTD_NANDECC_OFF,
259 static struct nand_oobinfo jffs2_oobinfo = {
260 .useecc = MTD_NANDECC_PLACE,
262 .eccpos = { 0, 1, 2, 3, 6, 7 }
265 static struct nand_oobinfo yaffs_oobinfo = {
266 .useecc = MTD_NANDECC_PLACE,
268 .eccpos = { 8, 9, 10, 13, 14, 15}
271 static struct nand_oobinfo autoplace_oobinfo = {
272 .useecc = MTD_NANDECC_AUTOPLACE
276 * nand_write_opts: - write image to NAND flash with support for various options
278 * @param meminfo NAND device to erase
279 * @param opts write options (@see nand_write_options)
280 * @return 0 in case of success
282 * This code is ported from nandwrite.c from Linux mtd utils by
283 * Steven J. Hill and Thomas Gleixner.
285 int nand_write_opts(nand_info_t *meminfo, const nand_write_options_t *opts)
293 int oobinfochanged = 0;
294 int percent_complete = -1;
295 struct nand_oobinfo old_oobinfo;
296 ulong mtdoffset = opts->offset;
297 ulong erasesize_blockalign;
298 u_char *buffer = opts->buffer;
302 if (opts->pad && opts->writeoob) {
303 printf("Can't pad when oob data is present.\n");
307 /* set erasesize to specified number of blocks - to match
308 * jffs2 (virtual) block size */
309 if (opts->blockalign == 0) {
310 erasesize_blockalign = meminfo->erasesize;
312 erasesize_blockalign = meminfo->erasesize * opts->blockalign;
315 /* make sure device page sizes are valid */
316 if (!(meminfo->oobsize == 16 && meminfo->oobblock == 512)
317 && !(meminfo->oobsize == 8 && meminfo->oobblock == 256)
318 && !(meminfo->oobsize == 64 && meminfo->oobblock == 2048)) {
319 printf("Unknown flash (not normal NAND)\n");
323 /* read the current oob info */
324 memcpy(&old_oobinfo, &meminfo->oobinfo, sizeof(old_oobinfo));
326 /* write without ecc? */
328 memcpy(&meminfo->oobinfo, &none_oobinfo,
329 sizeof(meminfo->oobinfo));
334 if (opts->autoplace && (old_oobinfo.useecc != MTD_NANDECC_AUTOPLACE)) {
336 memcpy(&meminfo->oobinfo, &autoplace_oobinfo,
337 sizeof(meminfo->oobinfo));
341 /* force OOB layout for jffs2 or yaffs? */
342 if (opts->forcejffs2 || opts->forceyaffs) {
343 struct nand_oobinfo *oobsel =
344 opts->forcejffs2 ? &jffs2_oobinfo : &yaffs_oobinfo;
346 if (meminfo->oobsize == 8) {
347 if (opts->forceyaffs) {
348 printf("YAFSS cannot operate on "
349 "256 Byte page size\n");
352 /* Adjust number of ecc bytes */
353 jffs2_oobinfo.eccbytes = 3;
356 memcpy(&meminfo->oobinfo, oobsel, sizeof(meminfo->oobinfo));
359 /* get image length */
360 imglen = opts->length;
361 pagelen = meminfo->oobblock
362 + ((opts->writeoob != 0) ? meminfo->oobsize : 0);
364 /* check, if file is pagealigned */
365 if ((!opts->pad) && ((imglen % pagelen) != 0)) {
366 printf("Input block length is not page aligned\n");
370 /* check, if length fits into device */
371 if (((imglen / pagelen) * meminfo->oobblock)
372 > (meminfo->size - opts->offset)) {
373 printf("Image %d bytes, NAND page %d bytes, "
374 "OOB area %u bytes, device size %u bytes\n",
375 imglen, pagelen, meminfo->oobblock, meminfo->size);
376 printf("Input block does not fit into device\n");
383 /* get data from input and write to the device */
384 while (imglen && (mtdoffset < meminfo->size)) {
389 * new eraseblock, check for bad block(s). Stay in the
390 * loop to be sure if the offset changes because of
391 * a bad block, that the next block that will be
392 * written to is also checked. Thus avoiding errors if
393 * the block(s) after the skipped block(s) is also bad
394 * (number of blocks depending on the blockalign
396 while (blockstart != (mtdoffset & (~erasesize_blockalign+1))) {
397 blockstart = mtdoffset & (~erasesize_blockalign+1);
401 /* check all the blocks in an erase block for
404 int ret = meminfo->block_isbad(meminfo, offs);
407 printf("Bad block check failed\n");
413 printf("\rBad block at 0x%lx "
414 "in erase block from "
415 "0x%x will be skipped\n",
421 mtdoffset = blockstart
422 + erasesize_blockalign;
424 offs += erasesize_blockalign
426 } while (offs < blockstart + erasesize_blockalign);
429 readlen = meminfo->oobblock;
430 if (opts->pad && (imglen < readlen)) {
432 memset(data_buf + readlen, 0xff,
433 meminfo->oobblock - readlen);
436 /* read page data from input memory buffer */
437 memcpy(data_buf, buffer, readlen);
440 if (opts->writeoob) {
441 /* read OOB data from input memory block, exit
443 memcpy(oob_buf, buffer, meminfo->oobsize);
444 buffer += meminfo->oobsize;
446 /* write OOB data first, as ecc will be placed
448 result = meminfo->write_oob(meminfo,
456 printf("\nMTD writeoob failure: %d\n",
460 imglen -= meminfo->oobsize;
463 /* write out the page data */
464 result = meminfo->write(meminfo,
468 (unsigned char *) &data_buf);
471 printf("writing NAND page at offset 0x%lx failed\n",
479 ((unsigned long long)
480 (opts->length-imglen) * 100
482 /* output progress message only at whole percent
483 * steps to reduce the number of messages printed
484 * on (slow) serial consoles
486 if (percent != percent_complete) {
487 printf("\rWriting data at 0x%x "
488 "-- %3d%% complete.",
490 percent_complete = percent;
494 mtdoffset += meminfo->oobblock;
501 if (oobinfochanged) {
502 memcpy(&meminfo->oobinfo, &old_oobinfo,
503 sizeof(meminfo->oobinfo));
507 printf("Data did not fit into device, due to bad blocks\n");
516 * nand_read_opts: - read image from NAND flash with support for various options
518 * @param meminfo NAND device to erase
519 * @param opts read options (@see struct nand_read_options)
520 * @return 0 in case of success
523 int nand_read_opts(nand_info_t *meminfo, const nand_read_options_t *opts)
525 int imglen = opts->length;
529 int percent_complete = -1;
532 ulong mtdoffset = opts->offset;
533 u_char *buffer = opts->buffer;
536 /* make sure device page sizes are valid */
537 if (!(meminfo->oobsize == 16 && meminfo->oobblock == 512)
538 && !(meminfo->oobsize == 8 && meminfo->oobblock == 256)
539 && !(meminfo->oobsize == 64 && meminfo->oobblock == 2048)) {
540 printf("Unknown flash (not normal NAND)\n");
544 pagelen = meminfo->oobblock
545 + ((opts->readoob != 0) ? meminfo->oobsize : 0);
547 /* check, if length is not larger than device */
548 if (((imglen / pagelen) * meminfo->oobblock)
549 > (meminfo->size - opts->offset)) {
550 printf("Image %d bytes, NAND page %d bytes, "
551 "OOB area %u bytes, device size %u bytes\n",
552 imglen, pagelen, meminfo->oobblock, meminfo->size);
553 printf("Input block is larger than device\n");
560 /* get data from input and write to the device */
561 while (imglen && (mtdoffset < meminfo->size)) {
566 * new eraseblock, check for bad block(s). Stay in the
567 * loop to be sure if the offset changes because of
568 * a bad block, that the next block that will be
569 * written to is also checked. Thus avoiding errors if
570 * the block(s) after the skipped block(s) is also bad
571 * (number of blocks depending on the blockalign
573 while (blockstart != (mtdoffset & (~meminfo->erasesize+1))) {
574 blockstart = mtdoffset & (~meminfo->erasesize+1);
578 /* check all the blocks in an erase block for
581 int ret = meminfo->block_isbad(meminfo, offs);
584 printf("Bad block check failed\n");
590 printf("\rBad block at 0x%lx "
591 "in erase block from "
592 "0x%x will be skipped\n",
598 mtdoffset = blockstart
599 + meminfo->erasesize;
601 offs += meminfo->erasesize;
603 } while (offs < blockstart + meminfo->erasesize);
607 /* read page data to memory buffer */
608 result = meminfo->read(meminfo,
612 (unsigned char *) &data_buf);
615 printf("reading NAND page at offset 0x%lx failed\n",
620 if (imglen < readlen) {
624 memcpy(buffer, data_buf, readlen);
629 result = meminfo->read_oob(meminfo,
637 printf("\nMTD readoob failure: %d\n",
643 if (imglen < readlen) {
647 memcpy(buffer, oob_buf, readlen);
655 ((unsigned long long)
656 (opts->length-imglen) * 100
658 /* output progress message only at whole percent
659 * steps to reduce the number of messages printed
660 * on (slow) serial consoles
662 if (percent != percent_complete) {
664 printf("\rReading data from 0x%x "
665 "-- %3d%% complete.",
667 percent_complete = percent;
671 mtdoffset += meminfo->oobblock;
678 printf("Could not read entire image due to bad blocks\n");
686 /******************************************************************************
687 * Support for locking / unlocking operations of some NAND devices
688 *****************************************************************************/
690 #define NAND_CMD_LOCK 0x2a
691 #define NAND_CMD_LOCK_TIGHT 0x2c
692 #define NAND_CMD_UNLOCK1 0x23
693 #define NAND_CMD_UNLOCK2 0x24
694 #define NAND_CMD_LOCK_STATUS 0x7a
697 * nand_lock: Set all pages of NAND flash chip to the LOCK or LOCK-TIGHT
700 * @param meminfo nand mtd instance
701 * @param tight bring device in lock tight mode
703 * @return 0 on success, -1 in case of error
705 * The lock / lock-tight command only applies to the whole chip. To get some
706 * parts of the chip lock and others unlocked use the following sequence:
708 * - Lock all pages of the chip using nand_lock(mtd, 0) (or the lockpre pin)
709 * - Call nand_unlock() once for each consecutive area to be unlocked
710 * - If desired: Bring the chip to the lock-tight state using nand_lock(mtd, 1)
712 * If the device is in lock-tight state software can't change the
713 * current active lock/unlock state of all pages. nand_lock() / nand_unlock()
714 * calls will fail. It is only posible to leave lock-tight state by
715 * an hardware signal (low pulse on _WP pin) or by power down.
717 int nand_lock(nand_info_t *meminfo, int tight)
721 struct nand_chip *this = meminfo->priv;
723 /* select the NAND device */
724 this->select_chip(meminfo, 0);
726 this->cmdfunc(meminfo,
727 (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK),
730 /* call wait ready function */
731 status = this->waitfunc(meminfo, this, FL_WRITING);
733 /* see if device thinks it succeeded */
738 /* de-select the NAND device */
739 this->select_chip(meminfo, -1);
744 * nand_get_lock_status: - query current lock state from one page of NAND
747 * @param meminfo nand mtd instance
748 * @param offset page address to query (muss be page aligned!)
750 * @return -1 in case of error
752 * bitfield with the following combinations:
753 * NAND_LOCK_STATUS_TIGHT: page in tight state
754 * NAND_LOCK_STATUS_LOCK: page locked
755 * NAND_LOCK_STATUS_UNLOCK: page unlocked
758 int nand_get_lock_status(nand_info_t *meminfo, ulong offset)
763 struct nand_chip *this = meminfo->priv;
765 /* select the NAND device */
766 chipnr = (int)(offset >> this->chip_shift);
767 this->select_chip(meminfo, chipnr);
770 if ((offset & (meminfo->oobblock - 1)) != 0) {
771 printf ("nand_get_lock_status: "
772 "Start address must be beginning of "
778 /* check the Lock Status */
779 page = (int)(offset >> this->page_shift);
780 this->cmdfunc(meminfo, NAND_CMD_LOCK_STATUS, -1, page & this->pagemask);
782 ret = this->read_byte(meminfo) & (NAND_LOCK_STATUS_TIGHT
783 | NAND_LOCK_STATUS_LOCK
784 | NAND_LOCK_STATUS_UNLOCK);
787 /* de-select the NAND device */
788 this->select_chip(meminfo, -1);
793 * nand_unlock: - Unlock area of NAND pages
794 * only one consecutive area can be unlocked at one time!
796 * @param meminfo nand mtd instance
797 * @param start start byte address
798 * @param length number of bytes to unlock (must be a multiple of
799 * page size nand->oobblock)
801 * @return 0 on success, -1 in case of error
803 int nand_unlock(nand_info_t *meminfo, ulong start, ulong length)
809 struct nand_chip *this = meminfo->priv;
810 printf ("nand_unlock: start: %08x, length: %d!\n",
811 (int)start, (int)length);
813 /* select the NAND device */
814 chipnr = (int)(start >> this->chip_shift);
815 this->select_chip(meminfo, chipnr);
817 /* check the WP bit */
818 this->cmdfunc(meminfo, NAND_CMD_STATUS, -1, -1);
819 if ((this->read_byte(meminfo) & 0x80) == 0) {
820 printf ("nand_unlock: Device is write protected!\n");
825 if ((start & (meminfo->oobblock - 1)) != 0) {
826 printf ("nand_unlock: Start address must be beginning of "
832 if (length == 0 || (length & (meminfo->oobblock - 1)) != 0) {
833 printf ("nand_unlock: Length must be a multiple of nand page "
839 /* submit address of first page to unlock */
840 page = (int)(start >> this->page_shift);
841 this->cmdfunc(meminfo, NAND_CMD_UNLOCK1, -1, page & this->pagemask);
843 /* submit ADDRESS of LAST page to unlock */
844 page += (int)(length >> this->page_shift) - 1;
845 this->cmdfunc(meminfo, NAND_CMD_UNLOCK2, -1, page & this->pagemask);
847 /* call wait ready function */
848 status = this->waitfunc(meminfo, this, FL_WRITING);
849 /* see if device thinks it succeeded */
851 /* there was an error */
857 /* de-select the NAND device */
858 this->select_chip(meminfo, -1);
862 #endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) && !defined(CFG_NAND_LEGACY) */