Table of contents
- Can I boot from NAND flash?
- Is there support for 32-bit wide NAND flash ?
- How do I erase my MTD partition on NAND flash?
- Can I copy a JFFS2 image to NAND via /dev/mtdX?
- Can I use seek/read/write on /dev/mtdX?
- Must my boot-loader be aware of NAND flash?
- Can I program NAND via JTAG?
- How do I use NAND simulator?
Can I boot from NAND flash?
Not from a bare NAND chip. You need a glue logic around, which gives you memory access to the chip on boot-up, like the DiskOnChip devices do. This will be a quite complex CPLD. An alternative is to use a small e.g. 1MB NOR flash, which contains the boot code and maybe a compressed kernel image. Then you can use JFFS2 or UBIFS on NAND as your root file-system.
Some chips have internal circuitry which is capable of reading and booting from the first page of NAND after power up. This allows you to boot a small (e.g. 256/512/2048 byte) boot code, which can initialize SDRAM, then read and execute a more complete bootloader from subsequent NAND blocks. An example is U-Boot, which can be built as a 2-stage "initial" and "secondary" bootloader for use with NAND.
Samson's S3C2410 ARM based SOC-CPU provides a mechanism to boot from NAND flash.
Is there support for 32-bit wide NAND flash?
No. The generic NAND driver supports 8/16 bit wide NAND flash only. 32 bit NAND flash can be built by using 2 or 4 chips and connect them to D0-7, D8-D15, D16-D23 and D24-D31 on the data bus. You can tie all corresponding control signals together. But you have to build a new nand16 or nand32 driver, which can be derived from the existing nand.c. Be aware, that the write-buffer size is 2 or 4 times as big as on 8 bit NAND. This means, if you flush the buffer to ensure, that your data are on disk, you may waste much more memory space than on 8 bit NAND. Another point is bad block handling. When a block on 1 chip is bad, the corresponding blocks on the other chips are lost too, as you present them as one big block to the file-system driver. The JFFS2 code, which handles the write-buffer and the out of band (spare) area of NAND doesn't support 32 bit neither.
How do I erase my MTD partition on NAND flash?
flash_erase (erase one eraseblock) or
utilities may be used. If you are going to use this partition with JFFS2, then
it is recommended to use
--jffs2 options. This
will write the so called "clean-marker" to the out of band area, so you can
mount the file-system and no further formatting is needed. This is also
necessary before you copy a filesystem image to the chip.
Can I copy a JFFS2 image to NAND via /dev/mtdX?
Technically yes, as long as this MTD partition does not contain bad blocks. But
it is generally a bad idea. For bad block aware copying, batter use nandwrite
Also, make sure, that the erasesize you set to mkfs.jffs2 is the same as the erasesize of your chip. Data will only be written with ECC, if your default settings for OOB-layout selection are set for JFFS2.
Can I use seek/read/write on /dev/mtdX?
Yes, as long as your program is aware of bad blocks. Make sure to set the
desired ECC layout by ioctl (
MEMSETOOBSEL). A example for bad
block handling and usage of ioctl (
MEMOOBSEL) can be found in
Must my bootloader be aware of NAND flash?
Yes, if you use your bootloader to erase the flash chip and copy a file-system
image to it. For erase make sure, that you don't erase factory-marked bad
blocks. They are marked in the 6th byte (offset
0x5) in the out of
band area of the first page of a block. The block is bad, if any bit in this
byte is zero. If you erase such a block, the bad block information is erased
too and lost. Further use of this block will lead to erroneous results.
For JFFS2 file-systems it's recommended to program the JFFS2 erased marker into the out of band area of the first page in each erased block after erasing the block. Do not program it into the data area of the page!
For (obsolete) 256 byte page size devices program the following data into the out of band area:
For 512 byte page size devices program the following data into the out of band area:
If you copy a file-system image to the chip, it's recommended to write it with ECC. You can use the ECC code in the nand driver to do this. If you have a bad block on your chip, just skip this block and copy the data to the next block. JFFS2 and other NAND flash aware file-systems handle this gap.
Can I program NAND via JTAG?
Yes, you can. The requirements are the same as for boot-loaders (see here).
How do I use NAND simulator?
NAND simulator (
nandsim) is an extremely useful debugging and
development tool which simulates NAND flashes in RAM or a file. To select the
simulated flash type one should specify ID bytes of your flash - the ones
which are returned by the "Read ID" command (
0x90) - consult the
flash manual. The following are examples of input parameters:
modprobe nandsim first_id_byte=0x20 second_id_byte=0x33- 16MiB, 512 bytes page;
modprobe nandsim first_id_byte=0x20 second_id_byte=0x35- 32MiB, 512 bytes page;
modprobe nandsim first_id_byte=0x20 second_id_byte=0x36- 64MiB, 512 bytes page;
modprobe nandsim first_id_byte=0x20 second_id_byte=0x78- 128MiB, 512 bytes page;
modprobe nandsim first_id_byte=0x20 second_id_byte=0x71- 256MiB, 512 bytes page;
modprobe nandsim first_id_byte=0x20 second_id_byte=0xa2 third_id_byte=0x00 fourth_id_byte=0x15- 64MiB, 2048 bytes page;
modprobe nandsim first_id_byte=0xec second_id_byte=0xa1 third_id_byte=0x00 fourth_id_byte=0x15- 128MiB, 2048 bytes page;
modprobe nandsim first_id_byte=0x20 second_id_byte=0xaa third_id_byte=0x00 fourth_id_byte=0x15- 256MiB, 2048 bytes page;
modprobe nandsim first_id_byte=0x20 second_id_byte=0xac third_id_byte=0x00 fourth_id_byte=0x15- 512MiB, 2048 bytes page;
modprobe nandsim first_id_byte=0xec second_id_byte=0xd3 third_id_byte=0x51 fourth_id_byte=0x95- 1GiB, 2048 bytes page;
If you do not have enough RAM, you can make
nandsim emulate the
flash on top of a file. Please, use the
cache_file nandsim module
nandsim can emulate various errors and report wear statistics,
which is extremely useful when testing how flash software handles errors (e.g.,
what does JFFS2 do in case of bit-flips or write errors).
nandsim can emulate 16-bit bus width
devices, but it may not work.