How To Boot from NAND Flash on the AMCC 440EPx
Table of Contents
- Table of contents
- HOWTO Boot from NAND Flash on the AMCC 440EPx
NFS-based root file systems are very useful in a development environment. However, once you start to move to a production system, it is no longer a viable option. Instead, you will want your device to boot from some sort of persistent memory, such as NAND flash. This document explains how to boot a complete system from NAND flash on the AMCC 440EPx board.
Prerequisites
- Kernel uImage with the following configuration options enabled:
- CONFIG_MTD_PARTITIONS=y
- CONFIG_MTD_CMDLINE_PARTS=y (optional)
- CONFIG_MTD_OF_PARTS=y
- CONFIG_MTD_BLOCK=y
- CONFIG_MTD_NAND=y
- CONFIG_MTD_NAND_FSL_ELBC=y
- CONFIG_MTD_UBI=y (If using UBI and UBIFS)
- CONFIG_UBIFS_FS=y (If using UBI and UBIFS)
- CONFIG_JFFS2_FS=y (If using JFFS2)
- CONFIG_JFFS2_SUMMARY=y (If using JFFS2 with summary support)
- RFS file (typically JFFS2 or UBI)
Building RFS Images
Determining your Flash Partitions
The kernel has the ability to divide a single flash device into multiple partitions. We can take advantage of this to keep the kernel from using sections of the flash that have been reserved for the kernel and bootloaders. To determine the partition scheme of the NAND flash, you can look at the kernel output at boot time. Here is an example:
1fc000000.nor_flash: Found 1 x16 devices at 0x0 in 16-bit bank Amd/Fujitsu Extended Query Table at 0x0040 1fc000000.nor_flash: CFI does not contain boot bank location. Assuming top. number of CFI chips: 1 cfi_cmdset_0002: Disabling erase-suspend-program due to code brokenness. RedBoot partition parsing not available Creating 6 MTD partitions on "1fc000000.nor_flash": 0x000000000000-0x000000180000 : "dtb" 0x000000180000-0x000000380000 : "kernel" 0x000000380000-0x000003e20000 : "file system" 0x000003e20000-0x000003f60000 : "kozio" 0x000003f60000-0x000003fa0000 : "env" 0x000003fa0000-0x000004000000 : "u-boot" NAND device: Manufacturer ID: 0xec, Chip ID: 0x75 (Samsung NAND 32MiB 3,3V 8-bit) Scanning device for bad blocks Creating 3 MTD partitions on "1d0000000.ndfc.nand": 0x000000000000-0x000000200000 : "kernel" 0x000000000000-0x000000004000 : "dtb" 0x000000000000-0x000001dfc000 : "user"
Based on this, you can determine the partition scheme of the device. The line
0x000000000000-0x000000180000
states that the first partition begins at offset 0 from the start of NOR, and continues until address 0x180000, giving it a size of 0x180000 bytes.
The second partition starts at 0x180000 and is 0x200000 bytes long.
The partitions are numbered in the order that they are detected, starting at 0. In the above example, there are 8 partitions, split between 1 NOR chip and 1 NAND chip. This is the default configuration in the Timesys-provided device tree:
Note: NOR addresses are only valid in U-Boot, which is using a virtual addressing scheme.
NOR Partitions
Device Number | Address | Size | Name |
---|---|---|---|
mtd0 | 0xfc000000 | 0x180000 | dtb |
mtd1 | 0xfc180000 | 0x200000 | kernel |
mtd2 | 0xfc380000 | 0x3aa0000 | file system |
mtd3 | 0xffe20000 | 0x140000 | kozio |
mtd4 | 0xfff60000 | 0x40000 | env |
mtd5 | 0xfffa0000 | 0x60000 | u-boot |
NAND Partitions
Device Number | Offset | Size | Name |
---|---|---|---|
mtd6 | 0x000000000000 | 0x200000 | kernel |
mtd7 | 0x000000200000 | 0x4000 | dtb |
mtd8 | 0x000000204000 | 0x1dfc000 | user |
If you are able to boot into a filesystem, you can use the file /proc/mtd to see the exact partitioning scheme for all MTDs on your board:
# cat /proc/mtd dev: size erasesize name mtd0: 00180000 00020000 "dtb" mtd1: 00200000 00020000 "kernel" mtd2: 03aa0000 00020000 "file system" mtd3: 00140000 00020000 "kozio" mtd4: 00040000 00020000 "env" mtd5: 00060000 00020000 "u-boot" mtd6: 00200000 00004000 "kernel" mtd7: 00004000 00004000 "dtb" mtd8: 01dfc000 00004000 "user"
size corresponds to the density (in bytes), and erasesize corresponds to the Physical Erase Block size (in Bytes).
Use this information to determine where to put your kernel and RFS. The RFS should have its own partition all to itself (e.g. mtd7), while the kernel can share a partition with a bootloader, size permitting. Make a note of the offsets that you are using for each one, since they will be used in a later step.
For this document, we will use the following scheme:
mtd6 | kernel |
mtd7 | dtb |
mtd8 | RFS |
Modifying Partitioning
If the partitions in the kernel are not adequate, as in the example above, you can use command line MTD partitioning to modify the layout:
You can also modify the partitions in the Linux kernel by changing the nand and nor sections in the device tree file.
Writing Kernel and Device Tree to NAND Flash
You can write the kernel and device tree to NAND flash using a number of methods. The simplest way may be to use your JTAG debugger. However, since there are a number of JTAG solutions available, you should consult the documentation for your setup to determine the method for writing these files to flash.
If you do not have a JTAG environment set up, you can use U-Boot to write the kernel and device tree to NAND. The basic method for writing a file to NAND is to erase the region that you wish to write to, transfer your file into RAM, then write the file to NAND.
Writing the Kernel
- Erase the region in NAND where you wish to write the kernel using the nand erase command.
# nand erase <offset> <size>
Where <offset> is the address relative to the beginning of the NAND chip, and <size> is the number of bytes to erase. - Transfer the kernel file to RAM. This can be done using a number of methods, although TFTP is probably easiest.
- Write the image to NAND using the nand write command.
# cp.b <address> <offset> <size>
Where <src address> is the address in RAM where you loaded the kernel to. <offset> and <size> should be the same as those used in the erase procedure above.
Example
We are using mtd6 for our kernel, which corresponds to offset 0x0 from the beginning of NAND.
=> nand erase 0 200000 => tftp 200000 uImage-2.6.30-ts-powerpc => nand write 200000 0 200000
Writing the Device Tree
You write the device tree in the same manner as the kernel. Just pick a different NAND offset.
Example
We are using mtd7 for our device tree, which corresponds to offset 0x200000 from the beginning of NAND.
=> nand erase 200000 4000 => tftp 800000 amcc440epx.dtb => nand write 800000 200000 4000
Writing RFS to NAND Flash
You can write the RFS to NAND flash using a number of methods. The easiest is using your JTAG, although we do not provide instructions for how to do this. Instead, please consult your JTAG documentation for help. The easiest way to write an RFS to NAND without the aid of a JTAG is to use a kernel booted with an NFS-based RFS.
Using U-Boot
You can write file system images to flash in the same manner as the kernel. Just pick a different NAND offset.
Example
We are using mtd8 for our device tree, which corresponds to offset 0x240000 from the beginning of NAND.
=> nand erase 240000 1dfc000 => tftp 200000 rootfs.jffs2 => nand write 200000 240000 1dfc000
Using a Linux kernel
Since U-Boot can only write to NAND directly from RAM, your RFS image cannot be larger than the available memory. If it is, then you cannot use U-Boot to write the RFS. Instead, you must boot Linux on your system by some other means first, such as NFS, then use the Linux utility mtd-utils to modify flash from userspace.
- On the host machine, copy your flash image into your NFS directory.
- On the target machine, boot Linux into an NFS-based RFS.
- Erase the NAND partition that you wish to use using the flash_eraseall command
# flash_eraseall /dev/mtdN
Where N is the partition number that you wish to erase. - Write your image to flash using the nandwrite command
# nandwrite /dev/mtdN <image path>
Example
We are using mtd8 for our RFS, which corresponds to the remainder of the NAND chip.
# flash_eraseall /dev/mtd8# nandwrite /dev/mtd8 /rootfs.jffs2.sum
Boot procedure
In order to boot from NAND, you must load the kernel from NAND into memory, and then tell the kernel where to look for the RFS, and how to load it.
- Copy the kernel into RAM using the nand read command:
=> nand read <address> <offset> <size>
This command will read <size> bytes from NAND address <offset> and move it to <address> in RAM. <size> should be greater than or equal to the size of the kernel. <offset> should be the start of the kernel partition in NAND, the same value used in the section Writing Kernel to NAND Flash above. <address> should be some location in RAM. The value 0x200000 has been tested and works. - Copy the device tree into RAM using the nand read command:
=> nand read <address> <offset> <size>
The value 0x800000 as the address has been tested and works. - Set the kernel command line to boot from NAND flash:
=> setenv bootargs console=ttyS0,115200 rootfstype=jffs2 root=/dev/mtdblockN rw
You should substitute the proper MTD device number for N in the root parameter above. See the UBIFS doc for information about how to use UBI. - Boot the kernel using the bootm command.
U-Boot> bootm <kernel address> - <device tree address>
<kernel address> and <device tree address> are the same as the respective values of address used in the NAND read commands above.
Example
=> nand read 200000 0 200000 => nand read 800000 200000 4000 => setenv bootcmd console=ttyS0,115200 rootfstype=jffs2 root=/dev/mtdblock8 rw => bootm 200000 - 800000