How To Boot from NAND Flash on the TI AM3517 EVM

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 TI OMAPL138 EVM 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)
  • NAND Flash support in U-Boot (the default U-Boot image does not support this)

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:

omap2-nand driver initializing
NAND device: Manufacturer ID: 0x2c, Chip ID: 0xbc (Micron NAND 512MiB 1,8V 16-bit)
Creating 5 MTD partitions on "omap2-nand.0":
0x000000000000-0x000000080000 : "xloader-nand" 
0x000000080000-0x000000240000 : "uboot-nand" 
0x000000240000-0x000000280000 : "params-nand" 
0x000000280000-0x000000780000 : "linux-nand" 
0x000000780000-0x000020000000 : "jffs2-nand" 

Based on this, you can determine the partition scheme of the device. The line

0x000000000000-0x000000080000

states that the first partition begins at offset 0 from the start of NAND, and continues until address 0x80000, giving it a size of 0x80000 bytes.

The second partition starts at 0x80000 and is 0x1fb00000 bytes long.

The partitions are numbered in the order that they are detected, starting at 0. In the above example, there are 5 partitions on 1 NAND chip. This is the default configuration in the Timesys-provided kernel.

Device Number Device Offset Size Name
mtd0 NAND 0x000000000000 0x00080000 xloader-nand
mtd1 0x000000080000 0x001c0000 uboot-nand
mtd2 0x000000240000 0x00040000 params-nand
mtd3 0x000000280000 0x00500000 linux-nand
mtd4 0x000000780000 0x1f880000 jffs2-nand

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: 00080000 00020000 "xloader-nand" 
mtd1: 001c0000 00020000 "uboot-nand" 
mtd2: 00040000 00020000 "params-nand" 
mtd3: 00500000 00020000 "linux-nand" 
mtd4: 1f880000 00020000 "jffs2-nand" 

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. mtd4), 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:

mtd3 kernel
mtd4 RFS

Modifying NAND 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 sections in the board specific file in arch/arm/mach-omap2.

Writing Kernel to NAND Flash

You can write the kernel 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 this file to flash.

If you do not have a JTAG environment set up, you can use U-Boot to write the kernel to NAND. The U-Boot NAND routines are bad-block safe, which means they will automatically account for (and skip) bad blocks. 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.

  1. Activate the proper NAND flash chip using the nand device command.
    U-Boot > nand device <number>

    Where <number> is the NAND bank.
  2. Erase the region in NAND where you wish to write the kernel using the nand erase command.
    U-Boot > 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.
  3. Transfer the kernel file to RAM. This can be done using a number of methods, although TFTP is probably easiest.
  4. Write the image to NAND using the nand write command.
    U-Boot > nand write <address> <offset> <size>

    Where <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 mtd3 for our kernel, which corresponds to offset 0x280000 from the beginning of NAND.

U-Boot > nand device 0
U-Boot > nand erase 280000 500000
U-Boot > tftp 82000000 kernel_am3517-evm-1
U-Boot > nand write 82000000 280000 500000

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

NOTE: Since U-Boot can only write to NAND directly from RAM, you can only use U-Boot if your filesystem is smaller than the available RAM.

You write the file system in the same manner as the kernel. Just pick a different NAND offset.

Example

We are using mtd4 for our RFS, which corresponds to offset 0x780000 from the beginning of NAND.

U-Boot > nand device 0
U-Boot > nand erase 780000 1f880000
U-Boot > tftp 82000000 rootfs.jffs2
U-Boot > nand write 82000000 780000 <size of jffs2 file>

Using the Kernel

NOTE: Your NFS-based RFS must have mtd-utils installed in order to modify flash from userspace.

  1. On the host machine, copy your flash image into your NFS directory.
  2. On the target machine, boot Linux into an NFS-based RFS.
  3. 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.
  4. Write your image to flash using the nandwrite command
    # nandwrite /dev/mtdN <image path>

Example

We are using mtd4 for our RFS, which corresponds to offset 0x780000 from the beginning of NAND.

# flash_eraseall /dev/mtd4
# nandwrite /dev/mtd4 /rootfs.jffs2

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.

  1. Copy the kernel into RAM using the nand read command:
    U-Boot > 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 *0x82000000 * has been tested and works.
  2. Set the kernel command line to boot from NAND flash:
    U-Boot > setenv bootargs console=ttyS2,115200n8 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.
  3. Boot the kernel using the bootm command.
    U-Boot > bootm

Example

U-Boot > nand read 82000000 280000 500000
U-Boot > setenv bootargs console=ttyS2,115200n8 rootfstype=jffs2 root=/dev/mtdblock4 rw
U-Boot > bootm