How To Boot from NAND Flash on the Freescale MPC8313E-RDB

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 Freescale MPC8313E-RDB.

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:

fe000000.flash: Found 1 x16 devices at 0x0 in 16-bit bank
 Amd/Fujitsu Extended Query Table at 0x0040
fe000000.flash: Swapping erase regions for broken CFI table.
number of CFI chips: 1
cfi_cmdset_0002: Disabling erase-suspend-program due to code brokenness.
RedBoot partition parsing not available
Creating 5 MTD partitions on "fe000000.flash":
0x00000000-0x00100000 : "uboot" 
0x00100000-0x00300000 : "kernel" 
0x00300000-0x00700000 : "rootfs" 
0x00700000-0x00710000 : "dtb" 
0x00710000-0x00800000 : "vscutil" 
NAND device: Manufacturer ID: 0xec, Chip ID: 0x75 (Samsung NAND 32MiB 3,3V 8-bit)
Bad block table found at page 65504, version 0x01
Bad block table found at page 65472, version 0x01
RedBoot partition parsing not available
Creating 3 MTD partitions on "e2800000.flash":
0x00000000-0x00100000 : "u-boot" 
0x00100000-0x00400000 : "kernel" 
0x00400000-0x02000000 : "fs" 
eLBC NAND device at 0xe2800000, bank 1

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

0x00000000-0x00100000

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

The second partition starts at 0x100000 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.

Device Number Device Offset Size Name
mtd0 NOR 0x00000000 0x00100000 u-boot
mtd1 0x00100000 0x00200000 kernel
mtd2 0x00300000 0x00400000 rootfs
mtd3 0x00700000 0x00010000 dtb
mtd4 0x07100000 0x000f0000 vscutil
mtd5 NAND Chip 0x00000000 0x00100000 u-boot
mtd6 0x00100000 0x00300000 kernel
mtd7 0x00400000 0x01c00000 fs

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: 00100000 00010000 "uboot" 
mtd1: 00200000 00010000 "kernel" 
mtd2: 00400000 00010000 "rootfs" 
mtd3: 00010000 00010000 "dtb" 
mtd4: 000f0000 00010000 "vscutil" 
mtd5: 00100000 00004000 "u-boot" 
mtd6: 00300000 00004000 "kernel" 
mtd7: 01c00000 00004000 "fs" 

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 or device tree, 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
mtd6 device tree
mtd7 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 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 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.

Writing the Kernel

  1. 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.
  2. Transfer the kernel file to RAM. This can be done using a number of methods, although TFTP is probably easiest.
  3. Write the image to NAND using the nand write command.
    => 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 mtd6 for our kernel, which corresponds to offset 0x100000 from the beginning of NAND.

=> nand erase 100000 300000
=> tftp 200000 kernel_mpc8313e-rdb-1
=> nand write 200000 100000 2E0000

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 mtd6 for our device tree, which is shared with the kernel. We have already erased this section, so we shouldn't have to do it again. We will write it to offset 0x3E0000 from the beginning of NAND.

=> tftp 400000 mpc8572ds.dtb
=> nand write 400000 3E0000 10000

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.

NOTE: Since U-Boot can only write to NAND directly from RAM, and the RFS images are typically much larger than the available memory, you cannot use U-Boot to write the RFS.

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 mtd7 for our RFS, which corresponds to the entirety of NAND chip 1.

# flash_eraseall /dev/mtd7
# nandwrite /dev/mtd7 /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.

  1. 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.
  2. Copy the device tree into RAM using the nand read command:
    => nand read <address> <offset> <size>

    The value 0x400000 as the address has been tested and works.
  3. 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.
  4. 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 100000 2e0000
=> nand read 400000 3e0000 10000
=> setenv bootargs console=ttyS0,115200 rootfstype=jffs2 root=/dev/mtdblock7 rw
=> bootm 200000 - 400000