1 The Linux MTD, YAFFS Howto
User ProgramSystem Call InterfaceBlock Device InterfaceFIle System(jffs,yaffs)Virtual File SystemMTD Driver ModuleNAND FLASH MEMORYMTD ModuleUser ModuleDriver Module
Picture 1-1 MTD System Organization
1.1 MTD Nand Device Driver
Modify drivers/mtd/nand/nand.c and eos_nand.c(Copy spia.c).
int __init eosnand_init (void)
{
struct nand_chip *this;
*(volatile unsigned char *) 0x1ff00010 = 0x82; // Pin Mux Control Register set
eos_writel(0x33002, NFMCON); // Nand Flash Memory Control Register set
…
this = (struct nand_chip *) (&eosnand_mtd[1]);
…
this->hwcontrol = eosnand_hwcontrol; // Device I/O Set
this->chip_delay = 15; // delay set
this->eccmode = NAND_ECC_SOFT; // ECC Use Set.
// target nand flash ID search
if (nand_scan (eosnand_mtd)) {
kfree (eosnand_mtd);
return -ENXIO;
}
// initialized eosnand_mtd registration
add_mtd_partitions(eosnand_mtd, partition_info, NUM_PARTITIONS);
return 0;
}
Code 1-1 eosnand_init()
EOS Nand Flash Device I/O Setup Function
Void eosnand_hwcontrol(int cmd)
{
switch(cmd){
case NAND_CTL_CLRNCE: // Nand Flash Enable
eos_writel(eos_readl(NFMCON) & ~0x02, NFMCON);
case NAND_CTL_SETNCE: // Nand Flash Disable
eos_writel(eos_readl(NFMCON) | 0x02, NFMCON);
case NAND_CTL_CHKRB: // Nand Flash busy check
{
int temp;
while(1) {
temp = eos_readb(NFMSTAT);
temp &= 0x01;
if (temp) break;
}
}
}
}
#define nand_select() this->hwcontrol(NAND_CTL_SETNCE);
#define nand_deselect() this->hwcontrol(NAND_CTL_CLRNCE);
#define nand_busy() this->hwcontrol(NAND_CTL_CHKRB);
Code 1-2 eosnand_hwcontrol()
Next, Manufacture ID read and additional hardware initializing function.
int nand_scan (struct mtd_info *mtd)
{
int i, nand_maf_id, nand_dev_id;
struct nand_chip *this = mtd->priv;
// check for proper chip_delay setup, set 20us if not
if (!this->chip_delay) this->chip_delay = 20;
// check, if a user supplied command function given
if (this->cmdfunc == NULL)
this->cmdfunc = nand_command;
…
nand_select(); // Nand Chip Enable
// Function to read Device ID.
this->cmdfunc (mtd, NAND_CMD_READID, 0x00, -1);
// Manufacture ID and Device ID read.
nand_maf_id = eos_readb (NFMDATA);
nand_dev_id = eos_readb (NFMDATA);
// Save Device information.
for (i = 0; nand_flash_ids[i].name != NULL; i++) {
if (nand_dev_id == nand_flash_ids[i].id && !mtd->size) { … }
}
…
nand_deselect (); // Nand Chip Disable
…
mtd->erase = nand_erase;
mtd->read = nand_read;
mtd->write = nand_write;
return 0;
}
Code 1-3 nand_scan()
Raw level functions of nand_erase, nand_read, nand_write are already implemented in nand.c. So, Eos only modify writeb and readb.
For example,
writeb (command, NAND_IO_ADDR); -> eos_writeb (command, NFMCMD);
readb (this->IO_ADDR_R); -> eos_readb(NFMDATA);
Next code is NAND Flash Partition setup.
// 0M ~ 3M : nand code + bootloader + ramdisk + kernel
// 3M ~ 5M : Root FileSystem(YAFFS)
// 5M ~ 8M : User Space
const static struct mtd_partition partition_info[] = {
{
.name = "Root FileSystem(YAFFS)",
.offset = 3*1024*1024,
.size = 2*1024*1024
},
{
.name = "User Space",
.offset = 5*1024*1024,
.size = 3*1024*1024
}
};
#define NUM_PARTITIONS 2
Code 1-4 Nand Flash Partition Setup.
Add eos_nand.c in Makefile.
obj-$(CONFIG_MTD_NAND_EOS) += eos_nand.o
include/config/arch/eos.h ?? #define CONFIG_ARCH_EOS 1.
drivers/mtd/nand/Config.in
if [ "$CONFIG_ARCH_EOS" = "y" ]; then
dep_tristate 'NAND Flash device on EOS board' CONFIG_MTD_NAND_EOS $CONFIG_MTD_NAND
fi
Kernel Compile Selection.
Memory Technology Device (MTD) --->
[*] Memory Technology Device (MTD) support
[*] MTD partitioning support
[*] Direct char device access to MTD devices
[*] Caching block device access to MTD devices
NAND Flash Device Drivers --->
[*] NAND Device Support
[*] NAND Flash device on EOS board
File systems --->
[*] Yaffs filesystem on NAND
1.2 Including yaffs as a kernel fs.
NAND flash is cheap and has a fast erase time as compared with NOR flash. The NAND physical interface is very simple. The small size and low current requirements make it very suitable for embedded systems.
NAND flash mostly use mtd+yaffs than mtd+jffs2.
YAFFS Public Site is http://www.aleph1.co.uk/armlinux/projects/. After you download yaffs
source, do followning.
① Create [linux]/fs/yaffs directory in your kernel.
② Copy devextras.h, yaffs_fs.c, yaffs_guts.c, yaffs_guts.h, yaffs_mtdif.c, yaffs_mtdif.h, yaffsinterface.h, yportenv.h, yaffs_config.h, Makefile to that directory.
③ Modify Makefie
// Comment all line and add following.
O_TARGET := yaffs.o
obj-y := yaffs_fs.o yaffs_guts.o yaffs_mtdif.o yaffs_ecc.o
obj-m := $(O_TARGET)
include $(TOPDIR)/Rules.make
④ [linux]/fs/Config.in.
if [ "CONFIG_MTD_NAND" = "y" ]; then
tristate "Yaffs filesystem on NAND" CONFIG_YAFFS_FS
fi
⑤ [linux]/fs/Makefile
subdir-$(CONFIG_YAFFS_FS) += yaffs
⑥ Make menuconfig and select Yaffs.
Picture 1-2 Yaffs Setup of Menuconfig
⑦ make dep -> make and download kernel image.
/>cat /proc/filesystems
Picture 1-3 Yaffs Filesystem List
?? ote : If Error occur in the middle of kernel-compile, download the latest MTD version. N
MTD download : ftp://ftp.uk.linux.org/pub/people/dwmw2/mtd/cvs/
Source change howto :
- First, delete the old source.
# rm –rf linux/drivers/mtd
# rm –rf linux/fs/jffs
# rm –rf linux/fs/jffs2
- Copy the download mtd source
# cp –rf mtd/drivers/mtd linux/drivers/
# cp -rf mtd/include/* linux/include/
# cp -rf mtd/fs/jffs linux/fs/jffs
# cp -rf mtd/fs/jffs2 linux/fs/jffs2
# cp -rf mtd/fs/ffs2 linux/fs/ffs2
# cp -rf mtd/lib/zlib_deflate linux/lib/
# cp -rf mtd/lib/zlib_inflate linux/lib/
1.3 Yaffs FileSystem Test
?? Partition setup
Download kernel image on Eos board and boot, so you will see the below screen.
Picture 1-4 Yaffs Partition Setup Screen
/dev/mtd0, /dev/mtdblock0 : Root FileSystem Space
/dev/mtd1, /dev/mtdblock1 : User Space
?? Device file Creation
Note : If Character Device’s sub-number is odd, you can’t write the Device. So, If you need to read and write, create even.
/>mknod /dev/mtd0 c 90 0
/>mknod /dev/mtd1 c 90 2
/>mknod /dev/mtdblock0 c 31 0
/>mknod /dev/mtdblock1 c 31 1
Picture 1-5 MTD Device file
?? Mount, Umount and Delete
- Create folder to mount
/>mkdir /mnt/flash0
/>mkdir /mnt/flash1
- Mount Block-Device on folder
/>mount –t yaffs /dev/mtdblock0 /mnt/flash0
/>mount –t yaffs /dev/mtdblock1 /mnt/flash1
Picture 1-6 YAFFS Mount
- Deletion file in the flash.
#!/bin/sh
eraseall /dev/mtd0
umount /mnt/flash0
mount –t yaffs /dev/mtdblock0 /mnt/flash0
Picture 15-7 YAFFS File Dele
- Umount
/> umount /mnt/flash0
/> umount /mnt/flash1
1.4 HOWTO on incorporating yaffs as a root fs.
* Root File System correspond to mtdblock0(0x300000 ~ 0x500000).
?? Copy File-System into mtdblock0 area.
① Boot Eos-Board.(you should make use of Ramdisk)
② Erase the mtdblock0
/>eraseall /dev/mtd0
③ Create the mount directory and mount.
/>mkdir –p /mnt/flash0
/>mount –t yaffs /dev/mtdblock0 /mnt/flash0
④ Copy filesystem into /mnt/flash0.
/>cp –drf /bin /mnt/flash0 />cp –drf /home /mnt/flash0
/>cp –drf /tmp /mnt/flash0 />cp –drf /dev /mnt/flash0
/>cp –drf /lib /mnt/flash0 />cp –drf /root /mnt/flash0
/>cp –drf /usr /mnt/flash0 />cp –drf /etc /mnt/flash0
/>cp –drf /opt /mnt/flash0 />cp –drf /sbin /mnt/flash0
/>cp –drf /var /mnt/flash0 />cp linuxrc /mnt/flash0
/>mkdir –p /mnt/flash0/mnt/flash1
⑤ Modify /mnt/flash0/etc/rc.
Mount –t yaffs /dev/mtdblock0 /mnt/flash0. -> Make line comment(annotate)
⑥ Modify /mnt/flash0/etc/fstab
#/dev/root / ext2 rw,noauto 0 1 ?? Comment
/dev/mtdblock0 / yaffs defaults 1 1 ?? Add
proc /proc proc defaults 0 0
devpts /dev/pts devpts defaults 0 0
tmpfs /tmp tmpfs defaults 0 0
⑦ Create rc2 into /mnt/flash1.
// Modify according to your Ethernet enviroment.
ifconfig eth0 210.102.3.122 netmask 255.255.255.128
route add default gw 210.102.3.10
cat /etc/motd
⑧ Modify pathname.h into init-ae32000.
#define _PATH_RC2 "/mnt/flash0/rc2" // Before modify
??
#define _PATH_RC2 "/mnt/flash1/rc2" // After modify
?? Modify Kernel Source
① make menuconfig -> Block Devices
< > RAM disk support -> delete
<*> Loopback device support . -> select
② arch/ae32000bnommu/kernel/setup.c
#define CONFIG_CMDLINE "root=/dev/ram0"
->
#define CONFIG_CMDLINE "noinitrd root=/dev/mtdblock0"
③ make dep -> make
④ Download kernel and boot
⑤ If you see the below screen, done successfully.
Picture 1-8 Yaffs as a root filesystem
Reference download address : ftp://ftp.adc.co.kr/pub/uclinux/EOS-uClinux/user/