From 0cc1214203def995d0863a389bbbe002c24a377f Mon Sep 17 00:00:00 2001 From: Jakob Leifhelm Date: Sun, 16 Apr 2023 22:43:32 +0200 Subject: [PATCH] Init starfive visionfive 2 --- flake.nix | 1 + starfive/visionfive/v2/README.md | 5 ++ starfive/visionfive/v2/default.nix | 25 +++++++++ starfive/visionfive/v2/firmware.nix | 56 +++++++++++++++++++ starfive/visionfive/v2/fix-memory-size.patch | 13 +++++ starfive/visionfive/v2/linux_6_3.nix | 28 ++++++++++ starfive/visionfive/v2/opensbi.nix | 42 ++++++++++++++ starfive/visionfive/v2/sd-image-installer.nix | 11 ++++ starfive/visionfive/v2/sd-image.nix | 51 +++++++++++++++++ starfive/visionfive/v2/spl-tool.nix | 18 ++++++ starfive/visionfive/v2/uboot.nix | 20 +++++++ 11 files changed, 270 insertions(+) create mode 100644 starfive/visionfive/v2/README.md create mode 100644 starfive/visionfive/v2/default.nix create mode 100644 starfive/visionfive/v2/firmware.nix create mode 100644 starfive/visionfive/v2/fix-memory-size.patch create mode 100644 starfive/visionfive/v2/linux_6_3.nix create mode 100644 starfive/visionfive/v2/opensbi.nix create mode 100644 starfive/visionfive/v2/sd-image-installer.nix create mode 100644 starfive/visionfive/v2/sd-image.nix create mode 100644 starfive/visionfive/v2/spl-tool.nix create mode 100644 starfive/visionfive/v2/uboot.nix 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..10496e7 --- /dev/null +++ b/starfive/visionfive/v2/README.md @@ -0,0 +1,5 @@ +# Creating SD-Image + +``` nix + +``` diff --git a/starfive/visionfive/v2/default.nix b/starfive/visionfive/v2/default.nix new file mode 100644 index 0000000..8cdd608 --- /dev/null +++ b/starfive/visionfive/v2/default.nix @@ -0,0 +1,25 @@ +{ 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..c313068 --- /dev/null +++ b/starfive/visionfive/v2/firmware.nix @@ -0,0 +1,56 @@ +{ callPackage, 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 = 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 = runCommand "starfive-visionfive2-spl" { } '' + mkdir -p $out/share/starfive-visionfive2/ + ln -s ${uboot}/u-boot-spl.bin . + ${spl-tool}/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..88c7887 --- /dev/null +++ b/starfive/visionfive/v2/linux_6_3.nix @@ -0,0 +1,28 @@ +{ lib, callPackage, linuxPackagesFor, kernelPatches, fetchpatch, ... }: + +let + modDirVersion = "6.3.0-rc3"; + linuxPkg = { lib, fetchFromGitHub, buildLinux, ... }@args: + buildLinux (args // { + version = "${modDirVersion}-starfive-visionfive2"; + + src = fetchFromGitHub { + owner = "starfive-tech"; + repo = "linux"; + rev = "2a6909fb414dfc72ae391791ec6edc3eedd13e6f"; + sha256 = "sha256-FeY6N+hk0PTpuIuA1hkcS+B+ozn6iHV6YaRVx1kuYHc="; + }; + + inherit modDirVersion; + kernelPatches = [{ patch = ./fix-memory-size.patch; }] ++ 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 <