From c8efa952f9d76c058972f12007f9945ff7bf3ee8 Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Mallo Date: Tue, 25 Jun 2024 11:11:05 +0200 Subject: [PATCH] Add configuration for Lagarto Ox --- flake.nix | 35 +++++++++++++ lagarto-ox.nix | 132 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 lagarto-ox.nix diff --git a/flake.nix b/flake.nix index f549cf1..cef84f7 100644 --- a/flake.nix +++ b/flake.nix @@ -58,6 +58,17 @@ ./no-compressed.nix ]; }; + + # FPGA Lagarto Ox CPU + lagarto-ox = nixosSystem { + specialArgs = { inherit self; }; + system = "${system}"; + modules = [ + ./configuration.nix + ./lagarto-ox.nix + ./no-compressed.nix + ]; + }; }; # A development shell with QEMU ready to boot the RISC-V system in an x86 @@ -97,5 +108,29 @@ echo " UBOOT_ENV = $UBOOT_ENV" ''; }; + + devShells.x86_64-linux.lagarto-ox = + let + nixosconf = self.nixosConfigurations.lagarto-ox; + syspkgs = nixosconf.pkgs; + build = nixosconf.config.system.build; + in syspkgs.mkShell { + pname = "lagarto-ox-shell"; + TOPLEVEL = build.toplevel; + OPENSBI = syspkgs.opensbi; + KERNEL = build.kernel; + INITRD = build.initialRamdisk; + ROOTFS = build.sdImage; + UBOOT_ENV = syspkgs.uboot-env; + shellHook = '' + echo "Here are the current system pieces:" + echo " TOPLEVEL = $TOPLEVEL" + echo " KERNEL = $KERNEL" + echo " OPENSBI = $OPENSBI" + echo " INITRD = $INITRD" + echo " ROOTFS = $ROOTFS" + echo " UBOOT_ENV = $UBOOT_ENV" + ''; + }; }; } diff --git a/lagarto-ox.nix b/lagarto-ox.nix new file mode 100644 index 0000000..51359ba --- /dev/null +++ b/lagarto-ox.nix @@ -0,0 +1,132 @@ +{ config, lib, pkgs, modulesPath, ... }: + +{ + imports = [ + "${modulesPath}/installer/sd-card/sd-image.nix" + ]; + + #boot.kernelPackages = pkgs.linuxPackages_latest; + boot = { + kernelPatches = [ + { + name = assert false; "sbi-early-console"; + patch = null; + extraConfig = + # Early console via SBI + '' + RISCV_SBI y + RISCV_SBI_V01 y + SERIAL_EARLYCON y + SERIAL_EARLYCON_RISCV_SBI y + HVC_DRIVER y + HVC_RISCV_SBI y + '' + # Allows regions of persistent memory to be described in the device-tree. + + '' + OF_PMEM y + '' + # Allow you to use a contiguous range of reserved memory as one or more + # persistent block devices (/dev/pmem0) + + '' + LIBNVDIMM y + BLK_DEV_PMEM y + '' + ; + } + ]; + + initrd = { + # Avoid zstd as we don't have the tools in "cucu" machine + compressor = "gzip"; + kernelModules = [ ]; + }; + + loader = { + grub.enable = false; + generic-extlinux-compatible.enable = true; + }; + }; + + # No network + services.openssh.enable = false; + + # Run getty on /dev/console and restartt until it works + systemd.services."serial-getty@console" = { + enable = true; + wantedBy = [ "getty.target" ]; # to start at boot + serviceConfig.Restart = "always"; + }; + + # Disable hvc0 as it is racing for the same console + systemd.services."serial-getty@hvc0" = { + enable = lib.mkForce false; + wantedBy = lib.mkForce [ ]; + }; + + sdImage = { + # The image will be loaded as-is in memory, so no compression + compressImage = false; + imageName = "rootfs.img"; + # Not needed for now + expandOnBoot = false; + populateFirmwareCommands = ""; + populateRootCommands = '' + mkdir -p ./files/boot + ${config.boot.loader.generic-extlinux-compatible.populateCmd} \ + -c ${config.system.build.toplevel} \ + -d ./files/boot + ''; + }; + + nixpkgs.overlays = [ (final: prev: { + uboot = prev.ubootQemuRiscv64Smode.override { + filesToInstall = [ "u-boot.bin" "u-boot-nodtb.bin" ]; + extraConfig = '' + CONFIG_RISCV_ISA_C=n + CONFIG_REQUIRE_SERIAL_CONSOLE=n + CONFIG_SERIAL_SEARCH_ALL=y + CONFIG_OF_CONTROL=y + CONFIG_OF_BOARD=y + CONFIG_OF_HAS_PRIOR_STAGE=y + CONFIG_BLKMAP=y + CONFIG_CMD_BLKMAP=y + CONFIG_SBI_V01=y + CONFIG_DEBUG_UART=y + CONFIG_DEBUG_UART_ANNOUNCE=y + CONFIG_DEBUG_SBI_CONSOLE=y + ''; + }; + + uboot-env = let + init = "${config.system.build.toplevel}/init"; + initrd = "${config.system.build.initialRamdisk}/initrd"; + in prev.runCommand "uboot.txt" {} '' + cat > $out < + + # Reduce memory to 3 GiB [0x80000000, 0x140000000) + fdt set /memory@80000000 reg <0x0 0x80000000 0x0 0xc0000000> + + # Set kernel options + setenv bootargs "root=/dev/ram0 loglevel=7 debug rw earlycon=sbi boot.trace console=hvc0 init=${init}" + + EOF + + # Populate the bood commands from the initrd size + echo "# Boot system" >> $out + echo "setenv ramdisk_size $(stat --format %s $(readlink -f ${initrd}))" >> $out + echo 'booti ''${kernel_addr_r} ''${ramdisk_addr_r}:''${ramdisk_size} ''${fdtcontroladdr}' >> $out + ''; + + opensbi = prev.opensbi.overrideAttrs (old: { + makeFlags = old.makeFlags ++ [ + "PLATFORM=fpga/openpiton" + "FW_PAYLOAD_PATH=${final.uboot}/u-boot-nodtb.bin" + ]; + patches = [ ./opensbi-lagarto-hun.patch ]; + }); + }) ]; +}