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,
39 //#include <jffs2/jffs2.h>
41 typedef struct erase_info erase_info_t
;
42 typedef struct mtd_info mtd_info_t
;
44 /* support only for native endian JFFS2 */
45 #define cpu_to_je16(x) (x)
46 #define cpu_to_je32(x) (x)
48 /*****************************************************************************/
49 static int nand_block_bad_scrub(struct mtd_info
*mtd
, loff_t ofs
, int getchip
)
55 * nand_erase_opts: - erase NAND flash with support for various options
58 * @param meminfo NAND device to erase
59 * @param opts options, @see struct nand_erase_options
60 * @return 0 in case of success
62 * This code is ported from flash_eraseall.c from Linux mtd utils by
63 * Arcom Control System Ltd.
65 int nand_erase_opts(nand_info_t
*meminfo
, const nand_erase_options_t
*opts
)
67 struct jffs2_unknown_node cleanmarker
;
75 int percent_complete
= -1;
76 int (*nand_block_bad_old
)(struct mtd_info
*, loff_t
, int) = NULL
;
77 const char *mtd_device
= meminfo
->name
;
79 memset(&erase
, 0, sizeof(erase
));
82 erase
.len
= meminfo
->erasesize
;
83 erase
.addr
= opts
->offset
;
84 erase_length
= opts
->length
;
86 isNAND
= meminfo
->type
== MTD_NANDFLASH
? 1 : 0;
89 cleanmarker
.magic
= cpu_to_je16 (JFFS2_MAGIC_BITMASK
);
90 cleanmarker
.nodetype
= cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER
);
92 struct nand_oobinfo
*oobinfo
= &meminfo
->oobinfo
;
94 /* check for autoplacement */
95 if (oobinfo
->useecc
== MTD_NANDECC_AUTOPLACE
) {
96 /* get the position of the free bytes */
97 if (!oobinfo
->oobfree
[0][1]) {
98 printf(" Eeep. Autoplacement selected "
99 "and no empty space in oob\n");
102 clmpos
= oobinfo
->oobfree
[0][0];
103 clmlen
= oobinfo
->oobfree
[0][1];
108 switch (meminfo
->oobsize
) {
124 cleanmarker
.totlen
= cpu_to_je32(8);
127 cpu_to_je32(sizeof(struct jffs2_unknown_node
));
129 cleanmarker
.hdr_crc
= cpu_to_je32(
130 crc32_no_comp(0, (unsigned char *) &cleanmarker
,
131 sizeof(struct jffs2_unknown_node
) - 4));
134 /* scrub option allows to erase badblock. To prevent internal
135 * check from erase() method, set block check method to dummy
136 * and disable bad block table while erasing.
139 struct nand_chip
*priv_nand
= meminfo
->priv
;
141 nand_block_bad_old
= priv_nand
->block_bad
;
142 priv_nand
->block_bad
= nand_block_bad_scrub
;
143 /* we don't need the bad block table anymore...
144 * after scrub, there are no bad blocks left!
146 if (priv_nand
->bbt
) {
147 kfree(priv_nand
->bbt
);
149 priv_nand
->bbt
= NULL
;
153 erase
.addr
< opts
->offset
+ erase_length
;
154 erase
.addr
+= meminfo
->erasesize
) {
158 if (!opts
->scrub
&& bbtest
) {
159 int ret
= meminfo
->block_isbad(meminfo
, erase
.addr
);
162 printf("\rSkipping bad block at "
168 } else if (ret
< 0) {
169 printf("\n%s: MTD get bad block failed: %d\n",
176 result
= meminfo
->erase(meminfo
, &erase
);
178 printf("\n%s: MTD Erase failure: %d\n",
183 /* format for JFFS2 ? */
186 /* write cleanmarker */
189 result
= meminfo
->write_oob(meminfo
,
196 printf("\n%s: MTD writeoob failure: %d\n",
201 printf("\n%s: this erase routine only supports"
209 ((unsigned long long)
210 (erase
.addr
+meminfo
->erasesize
-opts
->offset
)
211 * 100 / erase_length
);
213 /* output progress message only at whole percent
214 * steps to reduce the number of messages printed
215 * on (slow) serial consoles
217 if (percent
!= percent_complete
) {
218 percent_complete
= percent
;
220 printf("\rErasing at 0x%x -- %3d%% complete.",
221 erase
.addr
, percent
);
223 if (opts
->jffs2
&& result
== 0)
224 printf(" Cleanmarker written at 0x%x.",
232 if (nand_block_bad_old
) {
233 struct nand_chip
*priv_nand
= meminfo
->priv
;
235 priv_nand
->block_bad
= nand_block_bad_old
;
236 priv_nand
->scan_bbt(meminfo
);
242 #define MAX_PAGE_SIZE 2048
243 #define MAX_OOB_SIZE 64
246 * buffer array used for writing data
248 static unsigned char data_buf
[MAX_PAGE_SIZE
];
249 static unsigned char oob_buf
[MAX_OOB_SIZE
];
251 /* OOB layouts to pass into the kernel as default */
252 static struct nand_oobinfo none_oobinfo
= {
253 .useecc
= MTD_NANDECC_OFF
,
256 static struct nand_oobinfo jffs2_oobinfo
= {
257 .useecc
= MTD_NANDECC_PLACE
,
259 .eccpos
= { 0, 1, 2, 3, 6, 7 }
262 static struct nand_oobinfo yaffs_oobinfo
= {
263 .useecc
= MTD_NANDECC_PLACE
,
265 .eccpos
= { 8, 9, 10, 13, 14, 15}
268 static struct nand_oobinfo autoplace_oobinfo
= {
269 .useecc
= MTD_NANDECC_AUTOPLACE
273 * nand_write_opts: - write image to NAND flash with support for various options
275 * @param meminfo NAND device to erase
276 * @param opts write options (@see nand_write_options)
277 * @return 0 in case of success
279 * This code is ported from nandwrite.c from Linux mtd utils by
280 * Steven J. Hill and Thomas Gleixner.
282 int nand_write_opts(nand_info_t
*meminfo
, const nand_write_options_t
*opts
)
290 int oobinfochanged
= 0;
291 int percent_complete
= -1;
292 struct nand_oobinfo old_oobinfo
;
293 ulong mtdoffset
= opts
->offset
;
294 ulong erasesize_blockalign
;
295 u_char
*buffer
= opts
->buffer
;
299 if (opts
->pad
&& opts
->writeoob
) {
300 printf("Can't pad when oob data is present.\n");
304 /* set erasesize to specified number of blocks - to match
305 * jffs2 (virtual) block size */
306 if (opts
->blockalign
== 0) {
307 erasesize_blockalign
= meminfo
->erasesize
;
309 erasesize_blockalign
= meminfo
->erasesize
* opts
->blockalign
;
312 /* make sure device page sizes are valid */
313 if (!(meminfo
->oobsize
== 16 && meminfo
->oobblock
== 512)
314 && !(meminfo
->oobsize
== 8 && meminfo
->oobblock
== 256)
315 && !(meminfo
->oobsize
== 64 && meminfo
->oobblock
== 2048)) {
316 printf("Unknown flash (not normal NAND)\n");
320 /* read the current oob info */
321 memcpy(&old_oobinfo
, &meminfo
->oobinfo
, sizeof(old_oobinfo
));
323 /* write without ecc? */
325 memcpy(&meminfo
->oobinfo
, &none_oobinfo
,
326 sizeof(meminfo
->oobinfo
));
331 if (opts
->autoplace
&& (old_oobinfo
.useecc
!= MTD_NANDECC_AUTOPLACE
)) {
333 memcpy(&meminfo
->oobinfo
, &autoplace_oobinfo
,
334 sizeof(meminfo
->oobinfo
));
338 /* force OOB layout for jffs2 or yaffs? */
339 if (opts
->forcejffs2
|| opts
->forceyaffs
) {
340 struct nand_oobinfo
*oobsel
=
341 opts
->forcejffs2
? &jffs2_oobinfo
: &yaffs_oobinfo
;
343 if (meminfo
->oobsize
== 8) {
344 if (opts
->forceyaffs
) {
345 printf("YAFSS cannot operate on "
346 "256 Byte page size\n");
349 /* Adjust number of ecc bytes */
350 jffs2_oobinfo
.eccbytes
= 3;
353 memcpy(&meminfo
->oobinfo
, oobsel
, sizeof(meminfo
->oobinfo
));
356 /* get image length */
357 imglen
= opts
->length
;
358 pagelen
= meminfo
->oobblock
359 + ((opts
->writeoob
!= 0) ? meminfo
->oobsize
: 0);
361 /* check, if file is pagealigned */
362 if ((!opts
->pad
) && ((imglen
% pagelen
) != 0)) {
363 printf("Input block length is not page aligned\n");
367 /* check, if length fits into device */
368 if (((imglen
/ pagelen
) * meminfo
->oobblock
)
369 > (meminfo
->size
- opts
->offset
)) {
370 printf("Image %d bytes, NAND page %d bytes, "
371 "OOB area %u bytes, device size %u bytes\n",
372 imglen
, pagelen
, meminfo
->oobblock
, meminfo
->size
);
373 printf("Input block does not fit into device\n");
380 /* get data from input and write to the device */
381 while (imglen
&& (mtdoffset
< meminfo
->size
)) {
386 * new eraseblock, check for bad block(s). Stay in the
387 * loop to be sure if the offset changes because of
388 * a bad block, that the next block that will be
389 * written to is also checked. Thus avoiding errors if
390 * the block(s) after the skipped block(s) is also bad
391 * (number of blocks depending on the blockalign
393 while (blockstart
!= (mtdoffset
& (~erasesize_blockalign
+1))) {
394 blockstart
= mtdoffset
& (~erasesize_blockalign
+1);
398 /* check all the blocks in an erase block for
401 int ret
= meminfo
->block_isbad(meminfo
, offs
);
404 printf("Bad block check failed\n");
410 printf("\rBad block at 0x%lx "
411 "in erase block from "
412 "0x%x will be skipped\n",
418 mtdoffset
= blockstart
419 + erasesize_blockalign
;
421 offs
+= erasesize_blockalign
423 } while (offs
< blockstart
+ erasesize_blockalign
);
426 readlen
= meminfo
->oobblock
;
427 if (opts
->pad
&& (imglen
< readlen
)) {
429 memset(data_buf
+ readlen
, 0xff,
430 meminfo
->oobblock
- readlen
);
433 /* read page data from input memory buffer */
434 memcpy(data_buf
, buffer
, readlen
);
437 if (opts
->writeoob
) {
438 /* read OOB data from input memory block, exit
440 memcpy(oob_buf
, buffer
, meminfo
->oobsize
);
441 buffer
+= meminfo
->oobsize
;
443 /* write OOB data first, as ecc will be placed
445 result
= meminfo
->write_oob(meminfo
,
453 printf("\nMTD writeoob failure: %d\n",
457 imglen
-= meminfo
->oobsize
;
460 /* write out the page data */
461 result
= meminfo
->write(meminfo
,
465 (unsigned char *) &data_buf
);
468 printf("writing NAND page at offset 0x%lx failed\n",
476 ((unsigned long long)
477 (opts
->length
-imglen
) * 100
479 /* output progress message only at whole percent
480 * steps to reduce the number of messages printed
481 * on (slow) serial consoles
483 if (percent
!= percent_complete
) {
484 printf("\rWriting data at 0x%x "
485 "-- %3d%% complete.",
487 percent_complete
= percent
;
491 mtdoffset
+= meminfo
->oobblock
;
498 if (oobinfochanged
) {
499 memcpy(&meminfo
->oobinfo
, &old_oobinfo
,
500 sizeof(meminfo
->oobinfo
));
504 printf("Data did not fit into device, due to bad blocks\n");
513 * nand_read_opts: - read image from NAND flash with support for various options
515 * @param meminfo NAND device to erase
516 * @param opts read options (@see struct nand_read_options)
517 * @return 0 in case of success
520 int nand_read_opts(nand_info_t
*meminfo
, const nand_read_options_t
*opts
)
522 int imglen
= opts
->length
;
526 int percent_complete
= -1;
529 ulong mtdoffset
= opts
->offset
;
530 u_char
*buffer
= opts
->buffer
;
533 /* make sure device page sizes are valid */
534 if (!(meminfo
->oobsize
== 16 && meminfo
->oobblock
== 512)
535 && !(meminfo
->oobsize
== 8 && meminfo
->oobblock
== 256)
536 && !(meminfo
->oobsize
== 64 && meminfo
->oobblock
== 2048)) {
537 printf("Unknown flash (not normal NAND)\n");
541 pagelen
= meminfo
->oobblock
542 + ((opts
->readoob
!= 0) ? meminfo
->oobsize
: 0);
544 /* check, if length is not larger than device */
545 if (((imglen
/ pagelen
) * meminfo
->oobblock
)
546 > (meminfo
->size
- opts
->offset
)) {
547 printf("Image %d bytes, NAND page %d bytes, "
548 "OOB area %u bytes, device size %u bytes\n",
549 imglen
, pagelen
, meminfo
->oobblock
, meminfo
->size
);
550 printf("Input block is larger than device\n");
557 /* get data from input and write to the device */
558 while (imglen
&& (mtdoffset
< meminfo
->size
)) {
563 * new eraseblock, check for bad block(s). Stay in the
564 * loop to be sure if the offset changes because of
565 * a bad block, that the next block that will be
566 * written to is also checked. Thus avoiding errors if
567 * the block(s) after the skipped block(s) is also bad
568 * (number of blocks depending on the blockalign
570 while (blockstart
!= (mtdoffset
& (~meminfo
->erasesize
+1))) {
571 blockstart
= mtdoffset
& (~meminfo
->erasesize
+1);
575 /* check all the blocks in an erase block for
578 int ret
= meminfo
->block_isbad(meminfo
, offs
);
581 printf("Bad block check failed\n");
587 printf("\rBad block at 0x%lx "
588 "in erase block from "
589 "0x%x will be skipped\n",
595 mtdoffset
= blockstart
596 + meminfo
->erasesize
;
598 offs
+= meminfo
->erasesize
;
600 } while (offs
< blockstart
+ meminfo
->erasesize
);
604 /* read page data to memory buffer */
605 result
= meminfo
->read(meminfo
,
609 (unsigned char *) &data_buf
);
612 printf("reading NAND page at offset 0x%lx failed\n",
617 if (imglen
< readlen
) {
621 memcpy(buffer
, data_buf
, readlen
);
626 result
= meminfo
->read_oob(meminfo
,
634 printf("\nMTD readoob failure: %d\n",
640 if (imglen
< readlen
) {
644 memcpy(buffer
, oob_buf
, readlen
);
652 ((unsigned long long)
653 (opts
->length
-imglen
) * 100
655 /* output progress message only at whole percent
656 * steps to reduce the number of messages printed
657 * on (slow) serial consoles
659 if (percent
!= percent_complete
) {
661 printf("\rReading data from 0x%x "
662 "-- %3d%% complete.",
664 percent_complete
= percent
;
668 mtdoffset
+= meminfo
->oobblock
;
675 printf("Could not read entire image due to bad blocks\n");
683 /******************************************************************************
684 * Support for locking / unlocking operations of some NAND devices
685 *****************************************************************************/
687 #define NAND_CMD_LOCK 0x2a
688 #define NAND_CMD_LOCK_TIGHT 0x2c
689 #define NAND_CMD_UNLOCK1 0x23
690 #define NAND_CMD_UNLOCK2 0x24
691 #define NAND_CMD_LOCK_STATUS 0x7a
694 * nand_lock: Set all pages of NAND flash chip to the LOCK or LOCK-TIGHT
697 * @param meminfo nand mtd instance
698 * @param tight bring device in lock tight mode
700 * @return 0 on success, -1 in case of error
702 * The lock / lock-tight command only applies to the whole chip. To get some
703 * parts of the chip lock and others unlocked use the following sequence:
705 * - Lock all pages of the chip using nand_lock(mtd, 0) (or the lockpre pin)
706 * - Call nand_unlock() once for each consecutive area to be unlocked
707 * - If desired: Bring the chip to the lock-tight state using nand_lock(mtd, 1)
709 * If the device is in lock-tight state software can't change the
710 * current active lock/unlock state of all pages. nand_lock() / nand_unlock()
711 * calls will fail. It is only posible to leave lock-tight state by
712 * an hardware signal (low pulse on _WP pin) or by power down.
714 int nand_lock(nand_info_t
*meminfo
, int tight
)
718 struct nand_chip
*this = meminfo
->priv
;
720 /* select the NAND device */
721 this->select_chip(meminfo
, 0);
723 this->cmdfunc(meminfo
,
724 (tight
? NAND_CMD_LOCK_TIGHT
: NAND_CMD_LOCK
),
727 /* call wait ready function */
728 status
= this->waitfunc(meminfo
, this, FL_WRITING
);
730 /* see if device thinks it succeeded */
735 /* de-select the NAND device */
736 this->select_chip(meminfo
, -1);
741 * nand_get_lock_status: - query current lock state from one page of NAND
744 * @param meminfo nand mtd instance
745 * @param offset page address to query (muss be page aligned!)
747 * @return -1 in case of error
749 * bitfield with the following combinations:
750 * NAND_LOCK_STATUS_TIGHT: page in tight state
751 * NAND_LOCK_STATUS_LOCK: page locked
752 * NAND_LOCK_STATUS_UNLOCK: page unlocked
755 int nand_get_lock_status(nand_info_t
*meminfo
, ulong offset
)
760 struct nand_chip
*this = meminfo
->priv
;
762 /* select the NAND device */
763 chipnr
= (int)(offset
>> this->chip_shift
);
764 this->select_chip(meminfo
, chipnr
);
767 if ((offset
& (meminfo
->oobblock
- 1)) != 0) {
768 printf ("nand_get_lock_status: "
769 "Start address must be beginning of "
775 /* check the Lock Status */
776 page
= (int)(offset
>> this->page_shift
);
777 this->cmdfunc(meminfo
, NAND_CMD_LOCK_STATUS
, -1, page
& this->pagemask
);
779 ret
= this->read_byte(meminfo
) & (NAND_LOCK_STATUS_TIGHT
780 | NAND_LOCK_STATUS_LOCK
781 | NAND_LOCK_STATUS_UNLOCK
);
784 /* de-select the NAND device */
785 this->select_chip(meminfo
, -1);
790 * nand_unlock: - Unlock area of NAND pages
791 * only one consecutive area can be unlocked at one time!
793 * @param meminfo nand mtd instance
794 * @param start start byte address
795 * @param length number of bytes to unlock (must be a multiple of
796 * page size nand->oobblock)
798 * @return 0 on success, -1 in case of error
800 int nand_unlock(nand_info_t
*meminfo
, ulong start
, ulong length
)
806 struct nand_chip
*this = meminfo
->priv
;
807 printf ("nand_unlock: start: %08x, length: %d!\n",
808 (int)start
, (int)length
);
810 /* select the NAND device */
811 chipnr
= (int)(start
>> this->chip_shift
);
812 this->select_chip(meminfo
, chipnr
);
814 /* check the WP bit */
815 this->cmdfunc(meminfo
, NAND_CMD_STATUS
, -1, -1);
816 if ((this->read_byte(meminfo
) & 0x80) == 0) {
817 printf ("nand_unlock: Device is write protected!\n");
822 if ((start
& (meminfo
->oobblock
- 1)) != 0) {
823 printf ("nand_unlock: Start address must be beginning of "
829 if (length
== 0 || (length
& (meminfo
->oobblock
- 1)) != 0) {
830 printf ("nand_unlock: Length must be a multiple of nand page "
836 /* submit address of first page to unlock */
837 page
= (int)(start
>> this->page_shift
);
838 this->cmdfunc(meminfo
, NAND_CMD_UNLOCK1
, -1, page
& this->pagemask
);
840 /* submit ADDRESS of LAST page to unlock */
841 page
+= (int)(length
>> this->page_shift
) - 1;
842 this->cmdfunc(meminfo
, NAND_CMD_UNLOCK2
, -1, page
& this->pagemask
);
844 /* call wait ready function */
845 status
= this->waitfunc(meminfo
, this, FL_WRITING
);
846 /* see if device thinks it succeeded */
848 /* there was an error */
854 /* de-select the NAND device */
855 this->select_chip(meminfo
, -1);