
Where reasonable, each program attempts to emulate the MS-DOS equivalent command.The command set supported by a flash device can be enabled from the kernel configuration.
If UBI is compiled into the kernel, the mtd device to attach may be specified in the ubi.mtd kernel boot parameter, e.g.,

Linux Mtd Command Driver Is Registered
Scan nandflashS3c2410_nand_add_partition(info, nmtd, sets) //4. For this reason, it provides an abstract interface between the hardware and the upper layer.2.2 First look at the entry function of s3c2410.c:View Image static int _init s3c2410_nand_init(void)Printk("S3C24XX NAND Driver, (c) 2004 Simtec Electronics\n") Platform_driver_register(&s3c2412_nand_driver) Platform_driver_register(&s3c2440_nand_driver) Return platform_driver_register(&s3c2410_nand_driver) In the entry function, a platform platform device driver is registered, which means that when it matches the nandflash device, it will call s3c2440_nand_driver ->probe to initializeWe enter the probe function to see how to initializeView Image static int s3c24xx_nand_probe(struct platform_device *pdev, enum s3c_cpu_type cpu_type)Err = s3c2410_nand_inithw(info, pdev) //Initialize hardware hardware, set TACLS, TWRPH0, TWRPH1 communication timing, etc.S3c2410_nand_init_chip(info, nmtd, sets) //Initialize the chipNmtd->scan_res = nand_scan(&nmtd->mtd, (sets)? sets->nr_chips: 1) //3. The main purpose of MTD is to make the driver of the new memory device easier. Next, let's refer to the built-in nand flash driver, located in drivers/mtd/nand/s3c2410.cBecause mtd (memory technology device storage technology device) is a subsystem of Linux used to access memory devices (ROM, flash). Similarly, when we want to exit the read data/write data, it is also reset.1.4 The reset command is 0xff, and its timing diagram is as follows:1.5 Similarly, let's refer to the read address timing diagram to see:The column Address corresponds to the column address, which means the 2k address in a pageRow Address corresponds to the row address, which indicates which pageThe diagram of the cycle of 5 addresses is as follows:2. It is necessary to judge the RnB pin before reading and writing data), as shown in the following figure:If under the nand flash controller, we only need the following steps to read the ID (very convenient):1) Set bit1=0 in register NFCONT (0x4E000004) to enable chip select2) Write register NFCMMD (0x4E000008)=0X90, send command3) Write to register NFADDR(0x4E00000C)=0X00, send address4) While judge whether nRE (read enable) is low level5) Read the register NFDATA (0x4E000010) to read the data1.3 We test in uboot, and read id through md and mw commands (x should be lowercase)As shown in the figure below, 0XEC 0XDA 0X10 0X95 is finally readIt just corresponds to the data in our nand flash manual (where 0XEC represents the manufacturer ID, 0XDA represents the device ID):If we want to exit the read ID command, we only need to reset.
Linux Mtd Command Code And Comments
The s3c2410_nand_add_partition() function in probe() is mainly to register the nand flash of the MTD deviceFinally it called s3c2410_nand_add_partition()->add_mtd_partitions() -> add_mtd_device()Among them, the add_mtd_partitions() function mainly realizes the creation of multiple partitions, that is, multiple calls to add_mtd_device()When only setting nand_flash as one partition, just call add_mtd_device() directly.4.1 The prototype of the add_mtd_partitions() function is as follows:View Image int add_mtd_partitions(struct mtd_info *master, const struct mtd_partition *parts,int nbparts) //Create multiple partition mtd devices//master: is the mtd device to be created//parts: an array of partition information, its structure is mtd_partition, the structure is as follows:Char *name //Partition name, such as bootloader, params, kernel, rootU_int32_t offset //The offset value of the partitionStruct nand_ecclayout *ecclayout //OOB layoutStruct mtd_info **mtdp //MTD pointer, not commonly used//nbparts: equal to the number of arrays of partition information, indicating the number of partitions to be createdFor example, when we start the kernel, we can also find the partition information of nandflash that comes with the kernel:4.2 The add_mtd_device() function is as follows:View Image int add_mtd_device(struct mtd_info *mtd)//Create a mtd deviceList_for_each(this, &mtd_notifiers)//4.3 Find the list_head structure in the mtd_notifiers linked listStruct mtd_notifier *not = list_entry(this, struct mtd_notifier, list) //Find struct mtd_notifier *not through list_headNot->add(mtd) //Finally call the add() function of mtd_notifier4.3 We search the mtd_notifiers linked list in the above functionLook at the list_head structure inside, where it is put, you can find out what the add() is executed.4.4 As shown in the figure below, it is found that list_head is placed in the mtd_notifiers linked list in register_mtd_user()4.5 Continue to search for register_mtd_user(), which is called如上图,找到被 drivers/mtd/mtdchar.c、 drivers/mtd/mtd_blkdevs.c调用(4.6节和4.7节会分析)是因为mtd层既提供了字符设备的操作接口(mtdchar.c), 也实现了块设备的操作接口(mtd_blkdevs.c)我们在控制台输入ls -l/dev/mtd*,也能找到块MTD设备节点和字符MTD设备节点,如下图所示:上图中,可以看到共创了4个分区的设备,每个分区都包含了两个字符设备(mtd%d,mtd%dro)、一个块设备(mtdblock0).其中MTD的块设备的主设备号为31,MTD的字符设备的主设备号为90 (后面会讲到在哪被创建)4.6 我们进入上面搜到的drivers/mtd/mtdchar. To nandflash)3.1 The nand_get_flash_type() function is as follows:View Image static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,struct nand_chip *chip,int busw, int *maf_id)Chip->select_chip(mtd, 0) //Call member select_chip of nand_chip structure to enable flash chip selectionChip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1) //3.2 call the member cmdfunc of the nand_chip structure, send the read id command, and finally save the data in the mtd structure*maf_id = chip->read_byte(mtd) //Get manufacturer ID,Dev_id = chip->read_byte(mtd) //Get device ID/* 3.3 The for loop matches the nand_flash_ids array and finds the corresponding nandflash information*/For (i = 0 nand_flash_ids.name != NULL i++)If (dev_id = nand_flash_ids.id)//match device ID/* 3.4 If the match is successful, the nandflash parameter will be printed*/Printk(KERN_INFO "NAND device: Manufacturer ID:""0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,Dev_id, nand_manuf_ids.name, mtd->name) From the above code and comments, the nand_chip structure is to store hardware-related functions (this structure will be discussed later)3.2 Among them, NAND_CMD_READID is defined as 0x90, that is, send 0X90 command, and 0x00 address to read id, and finally put it in mtd3.3 The nand_flash_ids array is a global variable, here by matching the device ID, we can determine how much memory our nand flash isAs shown in the figure below, in the chip manual, see the device ID of nand flash=0XDA3.4 Then print out the nand flash parameters, we can see when we start the kernel:4. The nand_scan() scanning function in probe() above is located in/drivers/mtd/nand/nand_base.cIt will call nand_scan()->nand_scan_ident()->nand_get_flash_type() to get the type of flash memoryAnd nand_scan()->nand_scan_ident()->nand_scan_tail() to construct the members of the mtd device (read, write, erase, etc.
