25. Linux-Nor Flash Driver (detailed description), 25. linux-norflash

Source: Internet
Author: User

25. Linux-Nor Flash Driver (detailed description), 25. linux-norflash

1. nor hardwareIntroduction:

 

From the schematic diagram, we can see that nor flash has an address line and a data line. It is similar to our SDRAM interface and can directly read data, but it cannot directly write data like SDRAM. It requires commands.

1.1 of US 2440There are 27 address lines in total.Root (LADDR0 ~ 26 ),Why is it 27?Root?

Because 2440 has a total of 7 bank memory blocks, each bank = 128 MB = (2 ^ 27) B, so there are 27 data lines

1.2 Why Nor FlashAddress line A0Is connected to 2440LADDR1Upper?

Because the Nor Flash data has a total of 16 bits, that is, each address stores 2B data, and our 2440 each address stores 1B data. For example, when 2440 accesses the 0X00 address, the System reads the 16-bit data of the zero address on the Nor Flash, and the 2440 memory controller finds the low 8-bit bytes based on 0x00, and returns the data to the CPU

1.3 nandAnd norDifferences:

Nor flash is more expensive than nand, has a small capacity, and is slow in Data erasure and writing. The advantage is that the interface is simple, stable, non-bit inversion, and bad blocks. It is often used to store key data, nand flash is often used to store large data volumes.

In 2440, the hardware switch is used to set OM0 to Nand start or Nor start, as shown in:

 

Specific OM0 parameters are as follows, where 2440 of OM1 pins are grounded by default

 

For nand boot: the starting 4KB of nand flash will be automatically loaded into the 2440 built-in SRAM cache so that you can directly read and write

For nor startup: the memory accessed by 2440 is nor flash, which can be directly written but cannot be directly read.

 

2. nor flashThe command is as follows (Mx29lv800bbtc.pdf)

 

Word is for 16-bit nand, and byte is for 8-bit nand.

2.1 For example, when we want a program (write 0xff data to the 0x20 address)

The following steps are required:

Write 0xAA to the nor address 0x555

Write 0x55 to the nor address 0x2AA

Write 0xA0 to the nor address 0x555 // enter the program mode

Write 0xff (PD) to the nor address 0x20 (PA) // write 0xff to 0x20

(It will remain in program mode, and you can exit after executing reset mode)

2.2 The NORThere are two specifications: jedec and cfi (common flash interface)

Jedec

Like nandflash, the read ID is used to match the jedec_table [] array in drivers/mtd/chips/jedec_probe.c in the Linux kernel, to determine the parameters (name, capacity, bit width, etc.) of norflash, as shown in:

 

Cfi

It is to save these parameters in cfi mode and write 0x98 to the 0x55 address of nor to enter cfi mode,

Shows the cfi mode commands:

 

In cfi mode, for example, reading the data at the nor address 0x27 can read the nor capacity.

As shown in, the address * 2 is because of norAddress line A0Connect to US 2440A1Upper

Read 0X15, 0x15 = 21, for example, exactly corresponds to the 21 nor address lines in our schematic, so the capacity is 2 ^ 21 = 2 MB

 

2.3 why is the A20 PIN not connected?

For MySQL 2440 ~ The capacity of A19 is exactly 2 MB, which is consistent with the data read in cfi mode. Therefore, A20 is not connected.

 

3. Next we will analyze how to write norflashDriver

3.1 let's first recall the previous nandflsh DRIVER:

The nandflsh driver is placed in the kernel's mtd device, and the mtd device knows how to operate nandflash through commands/addresses/data, therefore, the previous nandflash driver only implemented hardware-related operations (mtd_info, nand_chip struct, and nand controller startup)

Similarly, the norflash driver is also placed in the kernel mtd device. The mtd device also knows how to read and write the nor, but does not know the norflash Bit Width (number of data lines), base address, and so on, therefore, our norflash driver must also implement hardware-related operations and provide mtd device calls.

 

3.2 refer to the kernel's built-in nor DRIVER: drivers/mtd/maps/physmap. c

Enter its init function:

 

We found that two platform device drivers were registered and entered the physmap_flash struct:

 

Three undefined variables are found:

CONFIG_MTD_PHYSMAP_BANKWIDTH: nandflash byte width

CONFIG_MTD_PHYSMAP_START: nandflash physical base address

CONFIG_MTD_PHYSMAP_LEN: nandflash capacity Length

These three variables are configured through the menuconfig menu of linux. If you fill in the values by yourself, you do not need to configure them using the menuconfig menu.

 

3.3 next we will configure the kernel,Then mount the norflashDrive the experiment

3.4 First make menuconfig, configure the above three variables, and then set them as modules

-> Device Drivers

-> Memory Technology Device (MTD) support

-> Mapping drivers for chip access // enter the ing driver

 

<M> CFI Flash device in physical memory map // set norflash that supports cfi as a module

  • (0x0) Physical start address of flash mapping // you can specify the Physical base address.
  • (0x1000000) Physical length of flash mapping // set the capacity length, which must be greater than or equal to 2 MB of its own nor
  • (2) Bank width in octets (NEW) // set the byte width. Because the nor is 16 bits, It is equal to 2.

 

3.5 make modules compilation Module

As shown in, you can see that physmap. c is compiled into the. ko module.

 

3.6 and put it in the nfs directory to start the development board

As shown in, insmod prints a string of information:

 

As shown in, you can see that two mtd0 character devices are created and one mtd0 device is created:

 

 

4. Next we will analyze physmap. c,How to Write norflashDriven

The probe function of physmap. c is as follows:

Struct physmap_flash_info {struct mtd_info * mtd; // implements operations such as read/write erasure on flash, struct map_info map; // stores the hardware-related struct resource * res; # ifdef CONFIG_MTD_PARTITIONS int nr_parts; struct mtd_partition * parts; # endif}; static const char * rom_probe_types [] = {"cfi_probe", "jedec_probe", "map_rom", NULL}; // chip name
...... Static int physmap_flash_probe (struct platform_device * dev) {const char ** probe_type ;...... /* 1. assign struct */info = kzarloc (sizeof (struct physmap_flash_info), GFP_KERNEL);/* 2. set the map_info struct */info-> map. name = dev-> dev. bus_id; // norflash name info-> map. phys = dev-> resource-> start; // physical base address info-> map. size = dev-> resource-> end-dev-> resource-> start + 1; // capacity length info-> map. bankwidth = physmap_data-> width; // byte width info-> map. virt = ioremap (info-> map. phys, info-> map. size); // virtual address
Simple_map_init (& info-> map); // initialize other members of map_info: probe_type = rom_probe_types;/* 3. set the mtd_info struct * // * to identify the chip by the name indicated by probe_type. When the do_map_probe () function returns NULL, it indicates that the corresponding mtd_info struct is not found, return to the current info-> mtd */for (; info-> mtd = NULL & * probe_type! = NULL; probe_type ++) info-> mtd = do_map_probe (* probe_type, & info-> map); // use do_map_probe () to identify the chip if (info-> mtd = NULL) {// if the chip is not found, log out of the previously registered items and exit dev_err (& dev-> dev, "map_probe failed \ n"); err =-ENXIO; goto err_out;} info-> mtd-> owner = THIS_MODULE;/* 4. add an mtd device */add_mtd_device (info-> mtd); return 0; err_out: physmap_flash_remove (dev); // This function is used to deregister the previously registered thing return err ;}

 

According to the code and comments above, it is similar to the nandflash driver in the previous section. Here we set the map_info struct and mtd_info struct, when we want to partition norflash, we need to use add_mtd_partitions ().

When * probe_type = "cfi_probe:

The chip is identified by do_map_probe ("cfi_probe", & info-> map.

The cfi_probe_chip () function in drivers/mtd/chips/cfi_probe.c is used to access the cfi mode and read the chip information.

When * probe_type = "jedec_probe:

In the end, the jedec_probe_chip () function in drivers/mtd/chips/jedec_probe.c is used to use the read ID command, and the ID is used to match the jedec_table [] array.

So register a block device driver,Perform the following steps:

  • 1. Allocate the mtd_info struct and map_info struct.
  • 2. Set the map_info struct.
  • 3. Set the mtd_info struct
  • 4. Use add_mtd_partitions () or add_mtd_device () to create MTD characters/Block devices

 

5. For more information, see physmap. c.Write norflah by yourselfDriver

The Code is as follows:

# Include <linux/module. h> # include <linux/types. h> # include <linux/kernel. h> # include <linux/init. h> # include <linux/slab. h> # include <linux/device. h> # include <linux/platform_device.h> # include <linux/mtd. h> # include <linux/mtd/map. h> # include <linux/mtd/partitions. h> # include <asm/io. h> static struct mtd_info * mynor_mtd_info; static struct map_info * mynor_map_info; static struct mtd_partition mynor_partitions [] = {[0] = {. name = "bootloader ",. size = 0x00040000 ,. offset = 0 ,}, [1] = {. name = "root ",. offset = MTDPART_OFS_APPEND ,. size = MTDPART_SIZ_FULL, }}; static const char * mynor_probe_types [] = {"cfi_probe", "jedec_probe", NULL}; static int mynor_init (void) {int val; /* 1. assign the map_info struct and mtd_info struct */mynor_mtd_info = kzarloc (sizeof (struct mtd_info), GFP_KERNEL); struct = kzarloc (sizeof (struct map_info), GFP_KERNEL);/* 2. set the map_info struct */mynor_map_info-> name = "my_nor"; mynor_map_info-> phys = 0x0; // the physical address mynor_map_info-> size = 0x1000000; // = 16 M, length must be greater than or equal to norflash capacity of 2 MB mynor_map_info-> bankwidth = 2; // 16-Bit Width mynor_map_info-> virt = ioremap (0x0, mynor_map_info-> size); // virtual address simple_map_init (mynor_map_info);/* 3. set mtd_info struct */
Mynor_mtd_info = do_map_probe ("cfi_probe", mynor_map_info );
If (! Mynor_mtd_info)
{
Mynor_mtd_info = do_map_probe ("jedec_probe", mynor_map_info );
}

If (! Mynor_mtd_info)
{
Printk ("not available norflash !!! \ R \ n ");
Goto err_out;
}
Mynor_mtd_info-> owner = THIS_MODULE;

 

/* 4. create MTD character/block device */add_mtd_partitions (mynor_mtd_info, mynor_partitions, 2) using delimiter () or add_mtd_device (); return 0; err_out: iounmap (mynor_map_info-> virt ); // cancel the virtual address ing kfree (mynor_map_info); kfree (mynor_mtd_info); return 0;} static void mynor_exit (void) {del_mtd_partitions (mynor_mtd_info ); // unmount the partition iounmap (mynor_map_info-> virt); // cancel the virtual address ing kfree (idle); kfree (mynor_mtd_info);} module_init (mynor_init); module_exit (mynor_exit ); MODULE_LICENSE ("GPL ");

 

6. Mount the driver test (Must be in norStart mounting)

After the driver is mounted to insmod, as shown in:

 

We can see that two partitions, "bootloader" and "root", are created, as shown in. We can see that two pairs of mtd characters/Block devices are created.

 

6.1 next, we will test the root partition (mtd1) (we 'd better erase it once before using flash)

The procedure is as follows:

. /Flash_eraseall-j/dev/mtd1 // use flash_eraseal to erase the root partition (mtd1) mount-t jffs2/dev/mtdblock1/mnt // mount the file system with mount, -t: File System type)

 

Next, you can read and write any file in the/mnt Directory, which will be stored in the mtdblock1 Device of flash.

(PS: You can refer to the mtdram. c Provided by the kernel, which uses memory to simulate flash, and uses memcopy () to implement memory read/write erasure)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.