How To Boot from NAND Flash on the TI OMAPL138 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_MMC_DAVINCI=n
    • CONFIG_MTD_PARTITIONS=y
    • CONFIG_MTD_CMDLINE_PARTS=y (optional)
    • CONFIG_MTD_BLOCK=y
    • CONFIG_MTD_NAND=y
    • CONFIG_MTD_NAND_DAVINCI=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:

davinci_nand davinci_nand.1: Using 4-bit hardware ECC - Syndrome
NAND device: Manufacturer ID: 0x2c, Chip ID: 0xdc (Micron NAND 512MiB 3,3V 8-bit)
Creating 2 MTD partitions on "davinci_nand.1":
0x000000000000-0x000000500000 : "NAND filesystem" 
0x000000500000-0x000020000000 : "RFS" 
davinci_nand davinci_nand.1: controller rev. 2.5
m25p80 spi1.0: m25p64 (8192 Kbytes)
Creating 3 MTD partitions on "m25p80":
0x000000000000-0x000000040000 : "U-Boot" 
0x000000040000-0x000000044000 : "U-Boot Environment" 
Moving partition 2: 0x000000044000 -> 0x000000050000
0x000000050000-0x000000800000 : "Linux" 

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

0x000000000000-0x000000500000

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

The second partition starts at 0x500000 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 split between 1 NAND chip and 1 SPI Flash chip. This is the default configuration in the Timesys-provided kernel.

Device Number Device Offset Size Name
mtd0 NAND 0x000000000000 0x00500000 NAND filesystem
mtd1 0x000000500000 0x1fb00000 RFS
mtd2 SPI Flash 0x000000000000 0x040000 U-Boot
mtd3 0x000000040000 0x010000 U-Boot Environment
mtd4 0x000000050000 0x7b0000 Linux

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: 00500000 00020000 "NAND filesystem" 
mtd1: 1fb00000 00020000 "RFS" 
mtd2: 00040000 00010000 "U-Boot" 
mtd3: 00004000 00010000 "U-Boot Environment" 
mtd4: 007b0000 00010000 "Linux" 

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

mtd0 kernel
mtd1 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-davinci.

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 and device tree 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.

NOTE The default U-Boot image on the OMAPL138 EVM board does not include support for NAND. However, the version built by Timesys does. If you wish to use U-Boot to burn the images, you must flash this image. Instructions for flashing U-Boot to the board can be found on the Davinci Linux Wiki. The U-Boot image is located in the bootloaders directory of your build.

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

U-Boot > nand device 0
U-Boot > nand erase 0 500000
U-Boot > tftp c0700000 kernel_omapl138-evm-1
U-Boot > nand write c0700000 0 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 mtd1 for our RFS, which corresponds to offset 0x500000 from the beginning of NAND.

U-Boot > nand device 0
U-Boot > nand erase 500000 1fb00000
U-Boot > tftp c0700000 rootfs.jffs2
U-Boot > nand write c0700000 500000 <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 mtd1 for our RFS, which corresponds to offset 0x500000 from the beginning of NAND.

# flash_eraseall /dev/mtd1
# nandwrite /dev/mtd1 /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 *0xc0700000 * has been tested and works.
  2. Set the kernel command line to boot from NAND flash:
    U-Boot > setenv bootargs console=ttyS2,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.
  3. Boot the kernel using the bootm command.
    U-Boot > bootm

Example

U-Boot > nand read c0700000 0 500000
U-Boot > setenv bootargs console=ttyS2,115200n8 rootfstype=jffs2 root=/dev/mtdblock1 rw
U-Boot > bootm