mirror of
https://github.com/fergalmoran/ladybird.git
synced 2026-01-07 17:15:26 +00:00
This simple delay loop uses the time CSR to wait for the given amount of time. The tick frequency of the CSR is read from the /cpus/timebase-frequency devicetree property.
34 lines
843 B
C++
34 lines
843 B
C++
/*
|
|
* Copyright (c) 2023, Sönke Holz <sholz8530@gmail.com>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <AK/Assertions.h>
|
|
#include <Kernel/Arch/Processor.h>
|
|
#include <Kernel/Arch/riscv64/CPU.h>
|
|
#include <Kernel/Arch/riscv64/CSR.h>
|
|
#include <Kernel/Arch/riscv64/Delay.h>
|
|
|
|
namespace Kernel {
|
|
|
|
static u32 s_timebase_frequency = 0;
|
|
|
|
void microseconds_delay(u32 microseconds)
|
|
{
|
|
VERIFY(s_timebase_frequency != 0);
|
|
|
|
u64 const start = RISCV64::CSR::read(RISCV64::CSR::Address::TIME);
|
|
u64 const delta = (static_cast<u64>(microseconds) * s_timebase_frequency) / 1'000'000ull;
|
|
|
|
while ((RISCV64::CSR::read(RISCV64::CSR::Address::TIME) - start) < delta)
|
|
Processor::pause();
|
|
}
|
|
|
|
void init_delay_loop()
|
|
{
|
|
s_timebase_frequency = DeviceTree::get().resolve_property("/cpus/timebase-frequency"sv).value().as<u32>();
|
|
}
|
|
|
|
}
|