diff --git a/README.md b/README.md index 80510852f..367a61311 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,7 @@ See code for all available configurations. | [Raspberry Pi 2](raspberry-pi/2) | `` | | [Raspberry Pi 4](raspberry-pi/4) | `` | | [Samsung Series 9 NP900X3C](samsung/np900x3c) | `` | +| [StarFive VisionFive v1](starfive/visionfive/v1) | `` | | [Supermicro A1SRi-2758F](supermicro/a1sri-2758f) | `` | | [Supermicro M11SDV-8C-LN4F](supermicro/m11sdv-8c-ln4f) | `` | | [Supermicro X10SLL-F](supermicro/x10sll-f) | `` | diff --git a/flake.nix b/flake.nix index 6bfd46b81..3d0757d47 100644 --- a/flake.nix +++ b/flake.nix @@ -132,6 +132,7 @@ raspberry-pi-4 = import ./raspberry-pi/4; kobol-helios4 = import ./kobol/helios4; samsung-np900x3c = import ./samsung/np900x3c; + starfive-visionfive-v1 = import ./starfive/visionfive/v1; supermicro = import ./supermicro; supermicro-a1sri-2758f = import ./supermicro/a1sri-2758f; supermicro-m11sdv-8c-ln4f = import ./supermicro/m11sdv-8c-ln4f; diff --git a/starfive/visionfive/v1/README.md b/starfive/visionfive/v1/README.md new file mode 100644 index 000000000..704a1847d --- /dev/null +++ b/starfive/visionfive/v1/README.md @@ -0,0 +1,25 @@ +### Build the SD image +- ``nix-build "" -A config.system.build.sdImage -I nixos-config=iso.nix`` + +- ``iso.nix`` + ```nix + { config, ... }: + + { + imports = [ ]; + # or imports = [ "${nixos-hardware-directory}/starfive/visionfive/v1/sd-image-installer.nix" ]; + + nixpkgs.crossSystem = { + config = "riscv64-unknown-linux-gnu"; + system = "riscv64-linux"; + }; + } + ``` + +### Relevant documentation +- Flashing +- - https://doc-en.rvspace.org/VisionFive/Quick_Start_Guide/VisionFive_QSG/hardware_connection.html +- - https://doc-en.rvspace.org/VisionFive/Quick_Start_Guide/VisionFive_QSG/using_xmodem1.html +- Recovery +- - https://doc-en.rvspace.org/VisionFive/Quick_Start_Guide/VisionFive_QSG/hardware_setup.html +- - https://doc-en.rvspace.org/VisionFive/Quick_Start_Guide/VisionFive_QSG/for_maclinux4.html diff --git a/starfive/visionfive/v1/default.nix b/starfive/visionfive/v1/default.nix new file mode 100644 index 000000000..4a3c3dd0b --- /dev/null +++ b/starfive/visionfive/v1/default.nix @@ -0,0 +1,31 @@ +{ config, lib, pkgs, ...}: + +{ + hardware.deviceTree.name = lib.mkDefault "starfive/jh7100-starfive-visionfive-v1.dtb"; + systemd.services."serial-getty@hvc0".enable = lib.mkDefault false; + environment.systemPackages = with pkgs; lib.mkDefault [ mtdutils ]; + + boot = { + # Force no ZFS (from nixos/modules/profiles/base.nix) until updated to kernel 6.0 + supportedFilesystems = lib.mkForce [ "btrfs" "reiserfs" "vfat" "f2fs" "xfs" "ntfs" "cifs" ]; + consoleLogLevel = lib.mkDefault 7; + kernelPackages = lib.mkDefault (pkgs.callPackage ./linux.nix { inherit (config.boot) kernelPatches; }); + + kernelParams = lib.mkDefault [ + "console=tty0" + "console=ttyS0,115200n8" + "earlycon=sbi" + ]; + + initrd.kernelModules = lib.mkDefault [ + "dw-axi-dmac-platform" + "dw_mmc-pltfm" + "spi-dw-mmio" + ]; + + loader = { + grub.enable = lib.mkDefault false; + generic-extlinux-compatible.enable = lib.mkDefault true; + }; + }; +} diff --git a/starfive/visionfive/v1/firmware.nix b/starfive/visionfive/v1/firmware.nix new file mode 100644 index 000000000..3ea637579 --- /dev/null +++ b/starfive/visionfive/v1/firmware.nix @@ -0,0 +1,35 @@ +{ runCommand +, buildPackages +, pkgs +}: + +let + uboot = pkgs.callPackage ./uboot.nix { }; + + opensbi = pkgs.opensbi.override { + withPayload = "${uboot}/u-boot.bin"; + withFDT = "${uboot}/u-boot.dtb"; + }; +in runCommand "firmware-starfive-visionfive-v1" { + nativeBuildInputs = with buildPackages; [ xxd ]; +} '' + function handle_file { + inFile=$1 + echo inFile: $inFile + outFile=$2 + inSize=`stat -c "%s" $inFile` + inSize32HexBe=`printf "%08x\n" $inSize` + inSize32HexLe=''${inSize32HexBe:6:2}''${inSize32HexBe:4:2}''${inSize32HexBe:2:2}''${inSize32HexBe:0:2} + echo "inSize: $inSize (0x$inSize32HexBe, LE:0x$inSize32HexLe)" + echo $inSize32HexLe | xxd -r -ps > $outFile + cat $inFile >> $outFile + echo outFile: $outFile + outSize=`stat -c "%s" $outFile` + outSize32HexBe=`printf "%08x\n" $outSize` + echo "outSize: $outSize (0x$outSize32HexBe)" + } + + mkdir -p "$out/nix-support" + echo "file bin \"$out/opensbi_u-boot_starfive_visionfive_v1.bin\"" >> "$out/nix-support/hydra-build-products" + handle_file ${opensbi}/share/opensbi/lp64/generic/firmware/fw_payload.bin $out/opensbi_u-boot_starfive_visionfive_v1.bin +'' diff --git a/starfive/visionfive/v1/linux.nix b/starfive/visionfive/v1/linux.nix new file mode 100644 index 000000000..889090993 --- /dev/null +++ b/starfive/visionfive/v1/linux.nix @@ -0,0 +1,36 @@ +{ lib +, callPackage +, linuxPackagesFor +, kernelPatches +, ... }: + +let + modDirVersion = "6.0.0"; + + linuxPkg = { fetchFromGitHub, buildLinux, ... } @ args: + buildLinux (args // { + inherit modDirVersion kernelPatches; + version = "${modDirVersion}-starfive-visionfive-v1"; + + src = fetchFromGitHub { + owner = "starfive-tech"; + repo = "linux"; + rev = "cfcb617265422c0af0ae5bc9688dceba2d10b27a"; + sha256 = "sha256-EAMCOtJZ51xSLySQPaZyomfa/1Xs9kNedz04tIbELqg="; + }; + + defconfig = "starfive_jh7100_fedora_defconfig"; + + structuredExtraConfig = with lib.kernel; { + KEXEC = yes; + SERIAL_8250_DW = yes; + PINCTRL_STARFIVE = yes; + DW_AXI_DMAC_STARFIVE = yes; + PTP_1588_CLOCK = yes; + STMMAC_ETH = yes; + STMMAC_PCI = yes; + }; + + extraMeta.branch = "visionfive"; + } // (args.argsOverride or { })); +in lib.recurseIntoAttrs (linuxPackagesFor (callPackage linuxPkg { })) diff --git a/starfive/visionfive/v1/sd-image-installer.nix b/starfive/visionfive/v1/sd-image-installer.nix new file mode 100644 index 000000000..c08ba961c --- /dev/null +++ b/starfive/visionfive/v1/sd-image-installer.nix @@ -0,0 +1,12 @@ +# To build, use: +# nix-build "" -I nixos-config=starfive/visionfive/v1/sd-image-installer.nix -A config.system.build.sdImage +{ + imports = [ + + ./sd-image.nix + ]; + + # the installation media is also the installation target, + # so we don't want to provide the installation configuration.nix. + installer.cloneConfig = false; +} diff --git a/starfive/visionfive/v1/sd-image.nix b/starfive/visionfive/v1/sd-image.nix new file mode 100644 index 000000000..7a8aa8158 --- /dev/null +++ b/starfive/visionfive/v1/sd-image.nix @@ -0,0 +1,29 @@ +# To build, use: +# nix-build "" -I nixos-config=starfive/visionfive/v1/sd-image.nix -A config.system.build.sdImage +{ config, pkgs, ... }: + +let + firmware = pkgs.callPackage ./firmware.nix { }; +in { + imports = [ + + + ./default.nix + ]; + + sdImage = { + imageName = "${config.sdImage.imageBaseName}-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}-starfive-visionfive-v1.img"; + + # We have to use custom boot firmware since we do not support + # StarFive's Fedora MMC partition layout. Thus, we include this in + # the image's firmware partition so the user can flash the custom firmware. + populateFirmwareCommands = '' + cp ${firmware}/opensbi_u-boot_starfive_visionfive_v1.bin firmware/opensbi_u-boot_starfive_visionfive_v1.bin + ''; + + populateRootCommands = '' + mkdir -p ./files/boot + ${config.boot.loader.generic-extlinux-compatible.populateCmd} -c ${config.system.build.toplevel} -d ./files/boot + ''; + }; +} diff --git a/starfive/visionfive/v1/uboot.nix b/starfive/visionfive/v1/uboot.nix new file mode 100644 index 000000000..9eebeb9fa --- /dev/null +++ b/starfive/visionfive/v1/uboot.nix @@ -0,0 +1,17 @@ +{ fetchFromGitHub +, buildUBoot +}: + +buildUBoot { + version = "2022.04"; + + src = fetchFromGitHub { + owner = "Madouura"; + repo = "u-boot"; + rev = "fe61fbcc8c5d3f7a589d2a6ea61855ab77de621f"; + sha256 = "sha256-jMZYxAHB37pNzzLdb8wupZA1CeD0gB84x18B7XVzq/M="; + }; + + defconfig = "starfive_jh7100_visionfive_smode_defconfig"; + filesToInstall = [ "u-boot.bin" "u-boot.dtb" ]; +}