diff --git a/Cargo.lock b/Cargo.lock index 44c015b988..cd2889fecd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -460,6 +460,7 @@ dependencies = [ "kernel_guard", "lazy_init", "percpu", + "volatile 0.2.7", ] [[package]] diff --git a/modules/axruntime/Cargo.toml b/modules/axruntime/Cargo.toml index ccd000b1d4..4cd3c62b59 100644 --- a/modules/axruntime/Cargo.toml +++ b/modules/axruntime/Cargo.toml @@ -24,6 +24,7 @@ net = ["axdriver", "axnet"] display = ["axdriver", "axdisplay"] [dependencies] +volatile = "0.2.4" axhal = { path = "../axhal" } axlog = { path = "../axlog" } axconfig = { path = "../axconfig" } diff --git a/modules/axruntime/src/lib.rs b/modules/axruntime/src/lib.rs index a62c3055cb..bca73eead0 100644 --- a/modules/axruntime/src/lib.rs +++ b/modules/axruntime/src/lib.rs @@ -18,6 +18,7 @@ #![cfg_attr(not(test), no_std)] #![feature(doc_auto_cfg)] +#![feature(core_intrinsics)] #[macro_use] extern crate axlog; @@ -25,6 +26,7 @@ extern crate axlog; #[cfg(all(target_os = "none", not(test)))] mod lang_items; mod trap; +mod rtc; #[cfg(feature = "smp")] mod mp; @@ -189,6 +191,18 @@ pub extern "C" fn rust_main(cpu_id: usize, dtb: usize) -> ! { core::hint::spin_loop(); } + unsafe{ + info!("before"); + /* + let mut myrtc = rtc::Pl031rtc::new(); + let x = myrtc.time(); + */ + rtc::init(); + let x = rtc::PL031_RTC.time(); + info!("{}",x); + info!("after"); + } + unsafe { main() }; #[cfg(feature = "multitask")] diff --git a/modules/axruntime/src/rtc.rs b/modules/axruntime/src/rtc.rs new file mode 100644 index 0000000000..e21312ca9b --- /dev/null +++ b/modules/axruntime/src/rtc.rs @@ -0,0 +1,44 @@ +use core::intrinsics::{volatile_load, volatile_store}; + +static RTC_DR: u32 = 0x000; +static RTC_MR: u32 = 0x004; +static RTC_LR: u32 = 0x008; +static RTC_CR: u32 = 0x00c; +static RTC_IMSC: u32 = 0x010; +static RTC_RIS: u32 = 0x014; +static RTC_MIS: u32 = 0x018; +static RTC_ICR: u32 = 0x01c; + +pub static mut PL031_RTC: Pl031rtc = Pl031rtc { + address: 0, +}; + +pub unsafe fn init() { + PL031_RTC.init(); +} + +pub struct Pl031rtc { + pub address: usize, +} + +pub const PHYS_OFFSET: usize = 0xffff_0000_0000_0000; + +impl Pl031rtc { + unsafe fn init(&mut self) { + self.address = PHYS_OFFSET + 0x09010000; + } + + unsafe fn read(&self, reg: u32) -> u32 { + let val = volatile_load((self.address + reg as usize) as *const u32); + val + } + + unsafe fn write(&mut self, reg: u32, value: u32) { + volatile_store((self.address + reg as usize) as *mut u32, value); + } + + pub fn time(&mut self) -> u64 { + let seconds = unsafe { self.read(RTC_DR) } as u64; + seconds + } +} \ No newline at end of file diff --git a/platforms/aarch64-qemu-virt.toml b/platforms/aarch64-qemu-virt.toml index 723bba7eb1..5ba797d565 100644 --- a/platforms/aarch64-qemu-virt.toml +++ b/platforms/aarch64-qemu-virt.toml @@ -23,6 +23,7 @@ mmio-regions = [ ["0x0a00_0000", "0x4000"], # VirtIO ["0x1000_0000", "0x2eff_0000"], # PCI memory ranges (ranges 1: 32-bit MMIO space) ["0x40_1000_0000", "0x1000_0000"], # PCI config space + ["0x0901_0000", "0x100"], ] # VirtIO MMIO regions with format (`base_paddr`, `size`). virtio-mmio-regions = [