NixOS on ARM/Radxa ROCK 4: Difference between revisions

Pigs (talk | contribs)
m Add category
Added instructions to build and write U-Boot to SPI flash present on various Rock 4 boards
 
Line 65: Line 65:
The stock UEFI ISO boots fine for installation purposes. If you wish to flash directly to SD/EMMC, the extlinux 'sdimage' may require a minor u-boot env change (<code>env set ramdisk_addr_r 0x06800000</code>, <code>env save</code>) in order to boot, but should work after this change has been made.   
The stock UEFI ISO boots fine for installation purposes. If you wish to flash directly to SD/EMMC, the extlinux 'sdimage' may require a minor u-boot env change (<code>env set ramdisk_addr_r 0x06800000</code>, <code>env save</code>) in order to boot, but should work after this change has been made.   


(TODO: SPI flash installation directions, for boards where this is present. This requires changes in Nixpkgs, so that the build command presented here will output the file "u-boot-rockchip-spi.bin".)
==== SPI flash installation from SD card ====
In Nixpkgs only SD card binaries are built by default. To get U-Boot binary for SPI flash and change other relevant settings the image needs to be built with overrides. To include <code>u-boot-rockchip-spi.bin</code> in <code>result/</code> folder it needs to added to the <code>filesToInstall</code>. Also included here is <code>u-boot-rockchip.bin</code> which includes <code>idbloader.img</code> and <code>u-boot.itb</code> in a single file.
 
By default this image is meant for Rock (Pi) 4A board. You might want to change this to another Rock 4 family board like Rock (Pi) 4C by setting <code>defconfig</code> to be <code>"rock-pi-4c-rk3399_defconfig"</code> or Rock 4SE with <code>"rock-4se-rk3399_defconfig"</code>. Another option is <code>extraConfig</code> where you can add overrides of defconfigs for example to use Rock (Pi) 4A defconfig but change device tree path to plus version of the boards by changing <code>CONFIG_DEFAULT_DEVICE_TREE</code> and <code>CONFIG_DEFAULT_FDT_FILE</code>.
 
Lastly at least on Rock (Pi) 4C there seems to be an issue upstream with a diagnostic blue LED turned on preventing SPI flash access. Here is a patch included to fix this in <code>extraPatches</code>.<syntaxhighlight lang="bash">
nix-build -E "
with import <nixpkgs> { };
pkgsCross.aarch64-multiplatform.ubootRockPi4.override {
  defconfig = \"rock-pi-4c-rk3399_defconfig\";
  filesToInstall = [ \"u-boot-rockchip.bin\" \"u-boot-rockchip-spi.bin\" ];
  extraPatches = [ (fetchpatch {
    url=\"https://gist.githubusercontent.com/Diamondtroller/e82d1c4a8b55beac26cc38209414a8b8/raw/ae4795cd4bf690c575fafa6b98714c47d548da1d/spi.patch\";
    hash=\"sha256-rEbmywPeOR5smIRhYUwcsfPeK+LiEMO2b3lmwrnJ+k4=\";
    }) ];
}"
 
</syntaxhighlight>After building U-Boot images SD card needs to be modified to have two partitions. One partition is meant for U-Boot, which will be the environment to flash the SPI chip, while second partition will contain the U-Boot binary file suitable for SPI chip. Create file system for second partition. Start of the first partition shouldn't be changed, other values can be changed as long as the files fit.<syntaxhighlight lang="bash">
echo -e 'label: gpt\nunit: sectors\nsector-size: 512\nfirst-lba:64\n\nstart=64, size=16M, name="U-Boot"\nsize=8M, name="Payload"' | sudo sfdisk /dev/mmcblk0
sudo mkfs.ext4 /dev/mmcblk0p2
 
</syntaxhighlight>Here are provided commands to copy the file to the second partition.<syntaxhighlight lang="bash">
mkdir /tmp/mnt
sudo mount /dev/mmcblk0p2 /tmp/mnt
sudo cp result/u-boot-rockchip-spi.bin /tmp/mnt
sudo umount /dev/mmcblk0p2
</syntaxhighlight>And here is the command to write U-Boot to the first partition.<syntaxhighlight lang="bash">
sudo dd if=result/u-boot-rockchip.bin of=/dev/mmcblk0p1 bs=1M
</syntaxhighlight>Insert the SD card into your board and boot into it. In case where you have corrupted or non-booting U-Boot in the flash you need to short SPI clock pin. Pin 25 is the ground pin and pin 23 is the SPI clock pin, connect those. You can see diagram of pins in [https://docs.radxa.com/en/rock4/hardware/rock4-gpio official docs]. However when you have booted into the U-Boot on SD card, this wire should be removed to access the flash. The idea is to prevent BootROM from trying to boot off of SPI flash. You can read about bootflow more in [https://opensource.rock-chips.com/wiki_Boot_option#Boot_flow Rockchip wiki]. When the board has booted into U-Boot enter these commands based on [https://docs.u-boot.org/en/latest/board/rockchip/rockchip.html#spi U-Boot docs]:
 
# probe the SPI flash for reading and writing, you should get SF: followed by info about SPI flash chip.
# load u-boot-rockchip-spi.bin file from mmc (SD card) on bus 1, partition 2 to the memory just after U-Boot.
# write to the SPI flash the contents of the file which are loaded into the memory.
<syntaxhighlight>
=> sf probe
=> load mmc 1:2 $kernel_addr_r u-boot-rockchip-spi.bin
=> sf update $fileaddr 0 $filesize
</syntaxhighlight>


== System configuration ==
== System configuration ==