How To Boot from NAND Flash on the Freescale MPC8572DS

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 MPC8572DS.

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:

e8000000.nor: Found 1 x16 devices at 0x0 in 16-bit bank
 Amd/Fujitsu Extended Query Table at 0x0040
e8000000.nor: 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 7 MTD partitions on "e8000000.nor":
0x000000000000-0x000003000000 : "ramdisk" 
0x000003000000-0x000003e00000 : "diagnostic" 
0x000003e00000-0x000004000000 : "dink" 
0x000004000000-0x000004400000 : "kernel" 
0x000004400000-0x000007f00000 : "jffs2" 
0x000007f00000-0x000007f80000 : "dtb" 
0x000007f80000-0x000008000000 : "u-boot" 
NAND device: Manufacturer ID: 0xec, Chip ID: 0xd3 (Samsung NAND 1GiB 3,3V 8-bit)
RedBoot partition parsing not available
Creating 6 MTD partitions on "ffa00000.flash":
0x000000000000-0x000002000000 : "u-boot" 
0x000002000000-0x000012000000 : "jffs2" 
0x000012000000-0x00001a000000 : "ramdisk" 
0x00001a000000-0x00001e000000 : "kernel" 
0x00001e000000-0x00001f000000 : "dtb" 
0x00001f000000-0x000040000000 : "empty" 
eLBC NAND device at 0xffa00000, bank 2
NAND device: Manufacturer ID: 0xec, Chip ID: 0xd3 (Samsung NAND 1GiB 3,3V 8-bit)
RedBoot partition parsing not available
eLBC NAND device at 0xffa40000, bank 4
NAND device: Manufacturer ID: 0xec, Chip ID: 0xd3 (Samsung NAND 1GiB 3,3V 8-bit)
RedBoot partition parsing not available
eLBC NAND device at 0xffa80000, bank 5
NAND device: Manufacturer ID: 0xec, Chip ID: 0xd3 (Samsung NAND 1GiB 3,3V 8-bit)
RedBoot partition parsing not available
eLBC NAND device at 0xffac0000, bank 6

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

0x000000000000-0x000003000000

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

The second partition starts at 0x3000000 and is 0xe00000 bytes long.

The partitions are numbered in the order that they are detected, starting at 0. In the above example, there are 16 partitions, split between 1 NOR chip and 4 NAND chips. This is the default configuration in the Timesys-provided device tree.

Device Number Device Offset Size Name
mtd0 NOR 0x000000000000 0x3000000 ramdisk
mtd1 0x000003000000 0xe00000 diagnostic
mtd2 0x000003e00000 0x200000 dink
mtd3 0x000004000000 0x400000 kernel
mtd4 0x000004400000 0x3b00000 jffs2
mtd5 0x000007f00000 0x80000 dtb
mtd6 0x000007f80000 0x80000 u-boot
mtd7 NAND Chip 0 0x000000000000 0x2000000 u-boot
mtd8 0x000002000000 0x10000000 jffs2
mtd9 0x000012000000 0x8000000 ramdisk
mtd10 0x00001a000000 0x4000000 kernel
mtd11 0x00001e000000 0x1000000 dtb
mtd12 0x00001f000000 0x21000000 empty
mtd13 NAND Chip 1 0x000000000000 0x000040000000 ffa40000.flash
mtd14 NAND Chip 2 0x000000000000 0x000040000000 ffa80000.flash
mtd15 NAND Chip 3 0x000000000000 0x000040000000 ffac0000.flash

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: 03000000 00020000 "ramdisk" 
mtd1: 00e00000 00020000 "diagnostic" 
mtd2: 00200000 00020000 "dink" 
mtd3: 00400000 00020000 "kernel" 
mtd4: 03b00000 00020000 "jffs2" 
mtd5: 00080000 00020000 "dtb" 
mtd6: 00080000 00020000 "u-boot" 
mtd7: 02000000 00020000 "u-boot" 
mtd8: 10000000 00020000 "jffs2" 
mtd9: 08000000 00020000 "ramdisk" 
mtd10: 04000000 00020000 "kernel" 
mtd11: 01000000 00020000 "dtb" 
mtd12: 21000000 00020000 "empty" 
mtd13: 40000000 00020000 "ffa40000.flash" 
mtd14: 40000000 00020000 "ffa80000.flash" 
mtd15: 40000000 00020000 "ffac0000.flash" 

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

mtd10 kernel
mtd11 device tree
mtd13 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. Activate the proper NAND flash chip using the nand device command.
    => 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.
    => 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.
    => 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 mtd10 for our kernel, which corresponds to offset 0x1a000000 from the beginning of NAND chip 0.

=> nand device 0
=> nand erase 1a000000 300000
=> tftp 1000000 kernel_mpc8572ds-1
=> nand write 1000000 1a000000 300000

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

=> nand device 0
=> nand erase 1e000000 20000
=> tftp c00000 mpc8572ds.dtb
=> nand write c00000 1e000000 200000

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

# flash_eraseall /dev/mtd13
# nandwrite /dev/mtd13 /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 0x1000000 has been tested and works.
  2. Copy the device tree into RAM using the nand read command:
    => nand read <address> <offset> <size>

    The value 0xC00000 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 1000000 1a000000 300000
=> nand read c00000 1e000000 200000
=> setenv bootargs console=ttyS0,115200 rootfstype=jffs2 root=/dev/mtdblock13 rw
=> bootm 1000000 - c00000