Project open source address: github.com/Mculover666…

0. Tutorial complete directory

In the last article, we ported uboot to JZ2440 development board.

  • Preliminary transplant uboot uboot study notes | 03-2012.04 to JZ2440 (modified clock, configure a serial port)

Before supporting Nor Flash operations, you should first understand the Nor Flash read and write methods.

  • S3C2440 – bare-metal – 09 | article use S3C2440 Nor Flash operation

After the uboot startup, CPU information (such as clock information) is normally printed, but then the Uboot program prompts flash error and crashes, as shown in the figure. In this article, we describe how to locate the problem, solve the problem, and enable uboot to support NOR Flash reading and writing:

1. Locate Flash read/write errors

The method of locating the error problem is simple, just locate the red log message “Flash:” in the program, this content search is slow in VS Code global search, so follow the uboot boot process to find, soon, in the arch/arm/lib/board.c file, In the board_init_r function, which is stage 2 of the uboot boot.

You can see it in the code:

Flash: Log information is displayed. “, then call flash_ini t to obtain flash_size, and then judge the obtained flash_size. If flash_size is 0, print the “failed” log, and call hang function to suspend the CPU, that is, crash:



So, preliminary location,The problem is the flash_init functionEnter the function to view its source code.

The source code for the flash_init function is defined indrivers/mtd/cfi_flash.cIn the file, in this function, the key code is shown as follows:

Flash_detect_legacy = flash_detect_legacy = flash_get_size = flash_DETECt_legacy = flash_DETECt_legacy = flash_DETECt_legacy = flash_GEt_size = flash_DETECt_legacy = flash_DETECt_legacy = flash_DETECt_legacy = flash_GEt_size = CFI; And then back.

Therefore, the current problem becomes: there is an error in the two methods of obtaining CFI information in Flash, and CFI information is not obtained.

  • The first method: flash_DETECt_legacy

By jumping to its definition, you can see that the source code isDefine the CONFIG_FLASH_CFI_LEGACY configuration through the macroFlash_detect_legacy: enabled; flash_DETECt_legacy: enabled;



In the source code of the function, there is a debugging message output through the debug statement, which can be used to view the three ids read:

By default, the debug function is disabled. Enable the debug macro definition at the beginning of the file:

Recompile uboot:

make distclean
make smdk2440_config
make
Copy the code

Then download u-boot.bin to Nor Flash, boot, and check the serial port output:

Isn’t it great!

As you can see in the output debugging information, Flash CFI is read out, but Flash still fails, waiting for further analysis, and then analyze the cause of the problem according to the source code.

2. The Flash fails even though the READ ID is correct

In the debugging information, you can see that the read ID isc2 2249 0According to the Nor Flash chip manual, 0xC2 represents Manifacture ID and 0x2249 represents Device ID. The read information is correct, but why does the system still indicate Flash error?

The problem is that JEDEC, an ancient standard, reads the ID and matches it with the Flash library (an array). If a match is found, it retrieves pre-defined information, such as Flash size, as shown in the red box:



The jedec_flash_match function is defined indrivers/mtd/jedec_flash.cIn the file, the source code is shown as follows:



Translate the comments for this function:

The jedec ID is matched against the table, and if the value is matched, the entire flash_info structure is populated!

Therefore, there is only one case where the correct ID value is read but the system still reports an error in Flash: the pre-set table does not contain the information matched by the current FlashID.

3. Add onboard Nor Flash jeDEC information

Jedec_table is also defined in this file (drivers/ MTD /jedec_flash.c).

JZ2440 development board Nor Flash model is MX29LV160DBTI-70G, size is 16m-bit, That translates to 2MB in bytes.

At the end of the jedec_table, add the jedec information of this Flash like other information:

  • 1) mfr_id

The information is Manifacture ID in the fileinclude/flash.hWhere MX’s are used:

  • ② dev_id (device ID)

Device ID in filedrivers/mtd/jedec_flash.cMedium macro definition, as shown in figure, added to this version of Flash:

  • (3) the name

You can write this as you like.

  • ④ Uaddr (unlock addr, unlock address)

Use the 16-bit unlock address macro to define MTD_UADDR_0x0555_0x02AA.

  • ⑤ DevSize (device capacity)

The capacity is also theredrivers/mtd/jedec_flash.cFile set, here select SIZE_2MiB:

  • ⑥ CmdSet (instruction set to use)

Use the default CFI_CMDSET_AMD_LEGACY.

  • ⑦NumEraseRegions(Number of erasure area types)

The Flash chip has a total of four erasure sector structures, as shown in the figure:

  • End regions

Fill in the details of the ERASEINFO() macro definition for the ERASEINFO region type above in the following format:

ERASEINFO(size,blocks)	// Indicate the sIze (in bytes) of the sector blocks
Copy the code

Based on the above analysis, the Flash information finally filled is as follows:

/* JZ2440 development board NoR FLASH */
#ifdef CONFIG_SYS_FLASH_LEGACY_1024Kx16
	{
		.mfr_id		= (u8)MX_MANUFACT,
		.dev_id		= MX29LV160DBTI,
		.name		= "MX MX29LV160DBTI",
		.uaddr		= {
			[1] = MTD_UADDR_0x0555_0x02AA /* x16 */
		},
		.DevSize		= SIZE_2MiB,
		.CmdSet			= CFI_CMDSET_AMD_LEGACY,
		.NumEraseRegions	= 4,
		.regions		= {
			ERASEINFO(16*1024.1),
			ERASEINFO(8*1024.2),
			ERASEINFO(32*1024.1),
			ERASEINFO(64*1024.31),}},#endif
Copy the code

Add the information to the board configuration fileinclude/configs/smdk2440.hTo open this Flash macro definition:



Compile, download, view output at serial terminal:



If the number of sectors is greater than that of the macro definition CONFIG_SYS_MAX_FLASH_SECT, if the number of sectors is greater than that of the macro definition CONFIG_SYS_MAX_FLASH_SECT, The system will print this line of log information:



Find the macro definition in the board configuration file, the actual Flash sector is 35, modify the macro definition:



Compile, download, and view serial output again:

You can see that there is no problem with Flash detection, uboot is normally booted to the COMMAND line interface, and the command can be used normally. After debugging, no debugging information is outputdrivers/mtd/cfi_flash.cClose the macro definition added at the beginning of the file:



Compiling the output again, you can see that the Flash detection debugging information is not printed.

4. Modify the uboot command line header

In the previous output, the uboot command line works normally, but the output header is smdK2410, which can be found in the board configuration fileinclude/configs/smdk2440.hModify the macro definition, as shown in the figure, to the information we need to display:



After modification, compile again, download, check the serial port output, the result is as expected:

Alternatively, you can use the uboot commandflinfoView the current flash memory information and compare it with the above knowledge.

The sector start address corresponds to the data manual (RO is specified in the Uboot software, RO indicates the current program storage area) :

To receive more exciting articles and resources, please subscribe to my wechat official account: “McUlover666”.