Add RISC-V timer support

This commit is contained in:
Zejun Zhao
2025-04-28 11:04:29 +08:00
committed by Tate, Hongliang Tian
parent 63daf69e17
commit ffb4097436
5 changed files with 159 additions and 53 deletions

View File

@ -1,28 +1,40 @@
// SPDX-License-Identifier: MPL-2.0
use ostd::{arch::timer::GOLDFISH_IO_MEM, mm::VmIoOnce};
use ostd::{arch::boot::DEVICE_TREE, io::IoMem, mm::VmIoOnce};
use chrono::{DateTime, Datelike, Timelike};
use crate::{SystemTime, rtc::Driver};
pub struct RtcGoldfish;
pub struct RtcGoldfish {
io_mem: IoMem,
}
impl Driver for RtcGoldfish {
fn try_new() -> Option<RtcGoldfish> {
GOLDFISH_IO_MEM.get()?;
Some(RtcGoldfish)
let chosen = DEVICE_TREE.get().unwrap().find_node("/soc/rtc").unwrap();
if let Some(compatible) = chosen.compatible()
&& compatible.all().any(|c| c == "google,goldfish-rtc")
{
let region = chosen.reg().unwrap().next().unwrap();
let io_mem = IoMem::acquire(
region.starting_address as usize
..region.starting_address as usize + region.size.unwrap(),
)
.unwrap();
Some(RtcGoldfish { io_mem })
} else {
None
}
}
fn read_rtc(&self) -> SystemTime {
const TIME_LOW: usize = 0;
const TIME_HIGH: usize = 4;
let io_mem = GOLDFISH_IO_MEM.get().unwrap();
let mut last_time_high = io_mem.read_once(TIME_HIGH).unwrap();
let mut last_time_high = self.io_mem.read_once(TIME_HIGH).unwrap();
let timestamp = loop {
let time_low: u32 = io_mem.read_once(TIME_LOW).unwrap();
let time_high: u32 = io_mem.read_once(TIME_HIGH).unwrap();
let time_low: u32 = self.io_mem.read_once(TIME_LOW).unwrap();
let time_high: u32 = self.io_mem.read_once(TIME_HIGH).unwrap();
if last_time_high == time_high {
break ((time_high as u64) << 32) | time_low as u64;
}