diff --git a/README.md b/README.md index 5da1f79..0fd29dd 100644 --- a/README.md +++ b/README.md @@ -216,6 +216,7 @@ See code for all available configurations. | [Raspberry Pi 4](raspberry-pi/4) | `` | | [Samsung Series 9 NP900X3C](samsung/np900x3c) | `` | | [StarFive VisionFive v1](starfive/visionfive/v1) | `` | +| [StarFive VisionFive 2](starfive/visionfive/v2) | `` | | [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 8a2f54f..76b5a5a 100644 --- a/flake.nix +++ b/flake.nix @@ -156,6 +156,7 @@ kobol-helios4 = import ./kobol/helios4; samsung-np900x3c = import ./samsung/np900x3c; starfive-visionfive-v1 = import ./starfive/visionfive/v1; + starfive-visionfive-2 = import ./starfive/visionfive/v2; supermicro = import ./supermicro; supermicro-a1sri-2758f = import ./supermicro/a1sri-2758f; supermicro-m11sdv-8c-ln4f = import ./supermicro/m11sdv-8c-ln4f; diff --git a/starfive/visionfive/v2/README.md b/starfive/visionfive/v2/README.md new file mode 100644 index 0000000..51e811f --- /dev/null +++ b/starfive/visionfive/v2/README.md @@ -0,0 +1,59 @@ +# Creating a SD-Image + +Create and configure the `flake.nix` file: +``` nix +{ + inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + inputs.nixos-hardware.url = "github:nixos/nixos-hardware"; + + # Some dependencies of this flake are not yet available on non linux systems + inputs.systems.url = "github:nix-systems/x86_64-linux"; + inputs.flake-utils.url = "github:numtide/flake-utils"; + inputs.flake-utils.inputs.systems.follows = "systems"; + + outputs = { self, nixpkgs, nixos-hardware, flake-utils, ... }: + flake-utils.lib.eachDefaultSystem (system: + rec { + packages.default = packages.sd-image; + packages.sd-image = (import "${nixpkgs}/nixos" { + configuration = + { config, ... }: { + imports = [ + "${nixos-hardware}/starfive/visionfive/v2/sd-image-installer.nix" + ]; + + # If you want to use ssh set a password + # users.users.nixos.password = "super secure password"; + # OR add your public ssh key + # users.users.nixos.openssh.authorizedKeys.keys = [ "ssh-rsa ..." ]; + + # AND configure networking + # networking.interfaces.end0.useDHCP = true; + # networking.interfaces.end1.useDHCP = true; + + # If you have the 2A variant uncomment the following line + # hardware.deviceTree.name = + # lib.mkDefault "starfive/jh7110-starfive-visionfive-2-v1.2a.dtb"; + + # Additional configuration goes here + + sdImage.compressImage = false; + + nixpkgs.crossSystem = { + config = "riscv64-unknown-linux-gnu"; + system = "riscv64-linux"; + }; + + system.stateVersion = "23.05"; + }; + inherit system; + }).config.system.build.sdImage; + }); +} +``` + +Build the sd image. + +``` sh +nix build .# +``` diff --git a/starfive/visionfive/v2/default.nix b/starfive/visionfive/v2/default.nix new file mode 100644 index 0000000..ee1ab3f --- /dev/null +++ b/starfive/visionfive/v2/default.nix @@ -0,0 +1,24 @@ +{ config, lib, pkgs, ... }: { + 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-6.3.nix { + inherit (config.boot) kernelPatches; + }); + + kernelParams = + lib.mkDefault [ "console=tty0" "console=ttyS0,115200n8" "earlycon=sbi" ]; + + initrd.availableKernelModules = [ "dw_mmc_starfive" ]; + + loader = { + grub.enable = lib.mkDefault false; + generic-extlinux-compatible.enable = lib.mkDefault true; + }; + }; + + hardware.deviceTree.name = + lib.mkDefault "starfive/jh7110-starfive-visionfive-2-v1.3b.dtb"; +} diff --git a/starfive/visionfive/v2/firmware.nix b/starfive/visionfive/v2/firmware.nix new file mode 100644 index 0000000..97592b3 --- /dev/null +++ b/starfive/visionfive/v2/firmware.nix @@ -0,0 +1,61 @@ +{ callPackage, pkgsBuildHost, runCommand, writeText, stdenv, dtc }: +let + uboot = callPackage ./uboot.nix { }; + opensbi = callPackage ./opensbi.nix { + withPayload = "${uboot}/u-boot.bin"; + withFDT = "${uboot}/starfive_visionfive2.dtb"; + }; + spl-tool = pkgsBuildHost.callPackage ./spl-tool.nix { }; + its-file = writeText "visionfive2-uboot-fit-image.its" '' + /dts-v1/; + + / { + description = "U-boot-spl FIT image for JH7110 VisionFive2"; + #address-cells = <2>; + + images { + firmware { + description = "u-boot"; + data = /incbin/("${opensbi}/share/opensbi/lp64/generic/firmware/fw_payload.bin"); + type = "firmware"; + arch = "riscv"; + os = "u-boot"; + load = <0x0 0x40000000>; + entry = <0x0 0x40000000>; + compression = "none"; + }; + }; + + configurations { + default = "config-1"; + + config-1 { + description = "U-boot-spl FIT config for JH7110 VisionFive2"; + firmware = "firmware"; + }; + }; + }; + ''; +in { + inherit opensbi uboot; + spl = stdenv.mkDerivation { + name = "starfive-visionfive2-spl"; + depsBuildBuild = [ spl-tool ]; + phases = [ "installPhase" ]; + installPhase = '' + mkdir -p $out/share/starfive-visionfive2/ + ln -s ${uboot}/u-boot-spl.bin . + spl_tool -c -f ./u-boot-spl.bin + cp u-boot-spl.bin.normal.out $out/share/starfive-visionfive2/spl.bin + ''; + }; + uboot-fit-image = stdenv.mkDerivation { + name = "starfive-visionfive2-uboot-fit-image"; + nativeBuildInputs = [ dtc ]; + phases = [ "installPhase" ]; + installPhase = '' + mkdir -p $out/share/starfive-visionfive2/ + ${uboot}/mkimage -f ${its-file} -A riscv -O u-boot -T firmware $out/share/starfive-visionfive2/visionfive2_fw_payload.img + ''; + }; +} diff --git a/starfive/visionfive/v2/fix-memory-size.patch b/starfive/visionfive/v2/fix-memory-size.patch new file mode 100644 index 0000000..a8bd5bc --- /dev/null +++ b/starfive/visionfive/v2/fix-memory-size.patch @@ -0,0 +1,13 @@ +diff --git a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi +index 752bb0b6fd00..93670da6cabd 100644 +--- a/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi ++++ b/arch/riscv/boot/dts/starfive/jh7110-starfive-visionfive-2.dtsi +@@ -30,7 +30,7 @@ cpus { + + memory@40000000 { + device_type = "memory"; +- reg = <0x0 0x40000000 0x1 0x0>; ++ reg = <0x0 0x40000000 0x2 0x0>; + }; + + thermal-zones { diff --git a/starfive/visionfive/v2/linux-6.3.nix b/starfive/visionfive/v2/linux-6.3.nix new file mode 100644 index 0000000..0a07584 --- /dev/null +++ b/starfive/visionfive/v2/linux-6.3.nix @@ -0,0 +1,37 @@ +{ lib, callPackage, linuxPackagesFor, kernelPatches, fetchpatch, ... }: + +let + modDirVersion = "6.3.0-rc4"; + linuxPkg = { lib, fetchFromGitHub, buildLinux, ... }@args: + buildLinux (args // { + version = "${modDirVersion}-starfive-visionfive2"; + + src = fetchFromGitHub { + owner = "starfive-tech"; + repo = "linux"; + rev = "a57bdb1d13f93c8fc1b3c668cc74d585bb20f3f8"; + sha256 = "sha256-jnQnJChIGCyJt+zwGfUTsMhrwmWek/ngIM6Pae6OXuI="; + }; + + inherit modDirVersion; + kernelPatches = [ + { patch = ./fix-memory-size.patch; } + { + patch = fetchpatch { + url = + "https://github.com/torvalds/linux/commit/d83806c4c0cccc0d6d3c3581a11983a9c186a138.diff"; + hash = "sha256-xUnEJkzQRIIBF/0GIpS0Cd+h6OdSiJlyva5xwxtleE0="; + }; + } + ] ++ kernelPatches; + + structuredExtraConfig = with lib.kernel; { + PL330_DMA = no; + PINCTRL_STARFIVE_JH7110_SYS = yes; + SERIAL_8250_DW = yes; + }; + + extraMeta.branch = "JH7110_VisionFive2_upstream"; + } // (args.argsOverride or { })); + +in lib.recurseIntoAttrs (linuxPackagesFor (callPackage linuxPkg { })) diff --git a/starfive/visionfive/v2/opensbi.nix b/starfive/visionfive/v2/opensbi.nix new file mode 100644 index 0000000..6a7e84f --- /dev/null +++ b/starfive/visionfive/v2/opensbi.nix @@ -0,0 +1,42 @@ +{ lib +, stdenv +, fetchFromGitHub +, python3 +, withPlatform ? "generic" +, withPayload ? null +, withFDT ? null +}: + +stdenv.mkDerivation rec { + pname = "opensbi"; + version = "1.3-git-2868f26"; + + src = fetchFromGitHub { + owner = "riscv-software-src"; + repo = "opensbi"; + rev = "2868f26131308ff345382084681ea89c5b0159f1"; + sha256 = "sha256-E+nVFLSpH6lQ2nVmMlVRTr7qYRVY0ULW7gUvAyTr90I="; + }; + + postPatch = '' + patchShebangs ./scripts + ''; + + nativeBuildInputs = [ python3 ]; + + installFlags = [ + "I=$(out)" + ]; + + makeFlags = [ + "PLATFORM=${withPlatform}" + "FW_TEXT_START=0x40000000" + ] ++ lib.optionals (withPayload != null) [ + "FW_PAYLOAD_PATH=${withPayload}" + ] ++ lib.optionals (withFDT != null) [ + "FW_FDT_PATH=${withFDT}" + ]; + + dontStrip = true; + dontPatchELF = true; +} diff --git a/starfive/visionfive/v2/sd-image-installer.nix b/starfive/visionfive/v2/sd-image-installer.nix new file mode 100644 index 0000000..ee9d364 --- /dev/null +++ b/starfive/visionfive/v2/sd-image-installer.nix @@ -0,0 +1,11 @@ +{ modulesPath, ... }: +{ + imports = [ + "${modulesPath}/profiles/installation-device.nix" + ./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/v2/sd-image.nix b/starfive/visionfive/v2/sd-image.nix new file mode 100644 index 0000000..06af34c --- /dev/null +++ b/starfive/visionfive/v2/sd-image.nix @@ -0,0 +1,51 @@ +{ config, pkgs, lib, modulesPath, ... }: + +let + firmware = pkgs.callPackage ./firmware.nix { }; +in { + imports = [ + "${modulesPath}/profiles/base.nix" + "${modulesPath}/installer/sd-card/sd-image.nix" + ./default.nix + ]; + + sdImage = { + imageName = + "${config.sdImage.imageBaseName}-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}-starfive-visionfive2.img"; + + # Overridden by postBuildCommands + populateFirmwareCommands = ""; + + firmwarePartitionOffset = 4; + firmwareSize = 4; + + postBuildCommands = '' + # preserve root partition + eval $(partx $img -o START,SECTORS --nr 2 --pairs) + + # increase image size for gpt backup header + truncate -s '+2M' $img + + sfdisk $img <