Enabling SPI-NOR Flash on Raspberry Pi 5 with Device Tree Modification
Introduction
In this article, we document the process of enabling and verifying an SPI-NOR flash device on a Raspberry Pi 5. We go through modifying the device tree, rebuilding it, and validating the SPI-NOR detection using Linux commands. This guide provides detailed explanations of the commands used and how conclusions were drawn from the outputs.
Modifying the Device Tree
To enable SPI-NOR flash, we need to modify the Device Tree Source (DTS) file to include the correct configuration. The relevant modifications involve defining the SPI controller node and specifying the SPI-NOR flash as a child device.
The modified device tree entry might look like this:
&spi10 {
status = "okay";
flash@0 {
// compatible = "spidev";
compatible = "jedec,spi-nor";
reg = <0>;
spi-max-frequency = <50000000>;
};
};
This change ensures that the SPI controller is enabled and that the SPI-NOR flash is properly recognized as a JEDEC-compatible NOR device.
Rebuilding the Device Tree
After modifying the DTS file, we need to recompile it into a Device Tree Blob (DTB). The standard way to rebuild the device tree on Raspberry Pi is by running:
make -j$(nproc) ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- dtbs
This command:
Once built, the new DTB file should be copied to the boot partition:
sudo cp arch/arm64/boot/dts/broadcom/*.dtb /boot/firmware/
Finally, reboot the system for the changes to take effect:
sudo reboot
Verifying SPI-NOR Flash Detection
Once the system is boot up, we check if the SPI-NOR flash is detected correctly.
Checking Kernel Modules
lsmod | grep nor
Output:
spi_nor 114688 0
mtd 98304 3 spi_nor,ofpart
Interpretation:
spi_nor Module
mtd Module
Since the spi_nor and mtd modules are loaded, this confirms that the kernel has successfully recognized the SPI-NOR flash memory. To further verify detection and operation, checking /proc/mtd and dmesg logs is essential.
Inspecting Kernel Log
dmesg | grep spi
Relevant output:
[ 9.719773] spi-nor spi10.0: s25fl016k (2048 Kbytes)
This log confirms that the SPI-NOR flash (S25FL016K) has been detected and initialized.
Checking MTD (Memory Technology Device) Partitions
cat /proc/mtd
Output:
dev: size erasesize name
mtd0: 00200000 00001000 "spi10.0"
Interpretation:
This confirms that the flash memory is correctly registered as an MTD device and can be accessed for read, write, and erase operations.
Installing MTD Utilities
To interact with the SPI NOR flash, install the MTD utilities:
领英推荐
sudo apt update
sudo apt install mtd-utils
Interacting with the SPI NOR Flash
Once the device is set up, you can read, write, and erase it using MTD utilities.
Checking Flash Information
mtd_debug info /dev/mtd0
This provides details about the flash memory.
Reading Flash Data
To dump the flash contents into a file:
sudo dd if=/dev/mtd0 of=flash_backup.bin
Or using mtd_debug:
sudo mtd_debug read /dev/mtd0 0 0x200000 flash_backup.bin
Erasing Flash
To erase the entire SPI NOR flash:
sudo flash_erase /dev/mtd0 0 0
?? Warning: This permanently erases all data!
Writing to Flash
To write a new firmware binary:
sudo mtd_debug write /dev/mtd0 0 0x200000 new_firmware.bin
Ensure the file size does not exceed the flash size.
?? The Underlying Logic of Device Tree Change
flash@0 {
// compatible = "spidev";
compatible = "jedec,spi-nor";
This is a change in the Device Tree configuration, switching from configuring a spidev device to configuring an spi - nor flash device. Here are the impacts:
Device driver matching
Impact on the /dev directory
$ ls /dev/spi*
/dev/spidev10.0
Original configuration: After loading the spidev driver, device nodes like /dev/spidev1.0 are usually generated in the /dev directory. This node can be used for general data transmission operations via the SPI interface, but it can't be directly used for operations like mounting a file system on the flash device or reading and writing data from/to it.
$ ls spi*
ls: cannot access 'spi*': No such file or directory
$ ls mtd*
mtd0 mtd0ro
Function implementation
Conclusion
Through device tree modification, recompilation, and validation steps, we successfully enabled an SPI-NOR flash device on the Raspberry Pi 5. By analyzing kernel logs, module status, and MTD partitions, we confirmed that the flash memory is properly recognized and accessible in Linux. These steps provide a solid foundation for using SPI-NOR flash for firmware storage, boot configurations, or data logging applications.
Appendix:
In the Linux operating system, the /proc file system is a virtual file system that provides an interface to access information about running processes and kernel - related data. Here's why /proc/mtd exists and how the path organization in /proc generally works:
Why /proc/mtd exists
Rule of path organization in /proc
In general, the naming convention in /proc is designed to be intuitive, with the path components often directly related to the type of information being exposed. The file system is used as a convenient way to export important system - level and process - level data to user - space for monitoring, debugging, and configuration purposes.
Embedded Software Engineer | C/C++ | C# | Linux Kernel | Yocto
2 周SPI flash is useful for storing some configs or serial numbers. This way you can ensure that data will survive full device reflash.