From 2f48ad5f404c5deb129dfaca26856627f2515971 Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Mallo Date: Thu, 5 Sep 2024 16:59:06 +0200 Subject: [PATCH] Define DT constants in a C header file Allows writing baremetal programs that can read the correct offsets of devices or clock frequencies. --- dts/.gitignore | 2 ++ dts/Makefile | 17 +++++++++++++++++ ox-plic.dts => dts/lagarto_ox.dts | 18 ++++++++++-------- dts/lagarto_ox.h | 9 +++++++++ lagarto-ox.nix | 22 ++++++++++++++++------ 5 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 dts/.gitignore create mode 100644 dts/Makefile rename ox-plic.dts => dts/lagarto_ox.dts (95%) create mode 100644 dts/lagarto_ox.h diff --git a/dts/.gitignore b/dts/.gitignore new file mode 100644 index 0000000..2008a4a --- /dev/null +++ b/dts/.gitignore @@ -0,0 +1,2 @@ +*.pp.dts +*.dtb diff --git a/dts/Makefile b/dts/Makefile new file mode 100644 index 0000000..7f7d3be --- /dev/null +++ b/dts/Makefile @@ -0,0 +1,17 @@ +CC?=gcc +DTC?=dtc + +CPPFLAGS=-E -C -P -nostdinc -undef -x assembler-with-cpp + +all: lagarto_ox.dtb + +clean: + rm -f *.pp.dts *.dtb + +%.pp.dts: %.dts *.h + $(CC) $(CPPFLAGS) $< -o $@ + +%.dtb: %.pp.dts + dtc -O dtb -o $@ $^ + +.PRECIOUS: %.pp.dts diff --git a/ox-plic.dts b/dts/lagarto_ox.dts similarity index 95% rename from ox-plic.dts rename to dts/lagarto_ox.dts index b233d59..5a4776d 100644 --- a/ox-plic.dts +++ b/dts/lagarto_ox.dts @@ -1,3 +1,5 @@ +#include "lagarto_ox.h" + /dts-v1/; / { #address-cells = <2>; @@ -19,9 +21,9 @@ // The RTC timer is clocked at the CPU frequency / 1525, so // around 32786.88 Hz - timebase-frequency = <32786>; + timebase-frequency = ; CPU0: cpu@0 { - clock-frequency = <50000000>; /* 50 MHz */ + clock-frequency = ; /* 50 MHz */ device_type = "cpu"; reg = <0>; status = "okay"; @@ -120,14 +122,14 @@ * consoles */ /* The serial for the kernel console */ - uart_console: serial@40001000 { + uart_console: serial@UART0_ADDR_HEX { compatible = "ns16550"; - reg = <0x0 0x40001000 0x0 0x1000>; + reg = <0x0 UART0_ADDR 0x0 0x1000>; reg-shift = <2>; /* No interrupts for this UART, use console=hvc0 */ /* This clock is the SERIAL_CLK */ - clock-frequency = <50000000>; - current-speed = <115200>; + clock-frequency = ; + current-speed = ; status = "okay"; }; /* The serial for interrupt tests */ @@ -138,8 +140,8 @@ /* Output interrupt 1 (the first one) */ interrupts = <1>; interrupt-parent = <&PLIC>; - clock-frequency = <50000000>; - current-speed = <115200>; + clock-frequency = ; + current-speed = ; status = "okay"; }; diff --git a/dts/lagarto_ox.h b/dts/lagarto_ox.h new file mode 100644 index 0000000..4a93006 --- /dev/null +++ b/dts/lagarto_ox.h @@ -0,0 +1,9 @@ +#define CPU_CLOCK_FREQUENCY 50000000 // 50 MHz +#define RTC_CLOCK_FREQUENCY 32786 + +#define UART_SPEED 115200 +#define UART0_ADDR 0x40001000 +#define UART0_ADDR_HEX 40001000 + +#define PLIC_ADDR 0x40800000 +#define UART_ADDR 0x40001000 diff --git a/lagarto-ox.nix b/lagarto-ox.nix index 4505759..01f54f9 100644 --- a/lagarto-ox.nix +++ b/lagarto-ox.nix @@ -370,11 +370,21 @@ # }; #})); - ox-dtb = prev.runCommand "ox.dtb" { - dtsFile = ./ox-plic.dts; - #dtsFile = ./xavi.dts; + ox-dtb = prev.stdenv.mkDerivation rec { + name = "ox.dtb"; + src = ./dts; + dontConfigure = true; nativeBuildInputs = [ prev.buildPackages.dtc ]; - } "dtc -O dtb -o $out $dtsFile"; + buildPhase = '' + make lagarto_ox.dtb + ''; + installPhase = '' + mkdir $out + cp lagarto_ox.* $out + ''; + dontFixup = true; + hardeningDisable = [ "all" ]; + }; #bitstream = "${final.bitstreams}/lagarto-3-ox/gold.bit"; bitstream = "${final.bitstreams}/lagarto-3-ox/ox_u55c_450d0ff0_fix_delegation_v2.bit"; @@ -448,7 +458,7 @@ extraMakeFlags = [ "V=1" #"KCPPFLAGS=-DLOG_DEBUG" - #"EXT_DTB=${final.ox-dtb}" + #"EXT_DTB=${final.ox-dtb}/lagarto_ox.dtb" ]; }; @@ -509,7 +519,7 @@ #"PLATFORM_RISCV_ISA=rv64g" # No compressed instructions #"PLATFORM_RISCV_ABI=lp64d" "FW_PAYLOAD_PATH=${final.uboot}/u-boot-nodtb.bin" - "FW_FDT_PATH=${final.ox-dtb}" + "FW_FDT_PATH=${final.ox-dtb}/lagarto_ox.dtb" # Ensure it doesn't overlap from the 0x80200000 where the kernel will be # placed.