mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 12:47:45 +00:00
Kernel: Add a Timer class for aarch64
For now, this can only query microseconds since boot. Use this to print a timestamp every second. This busy-loops until a second has passed. This might be a good first use of interrupts soon. qemu used to not implement this timer at some point, but it seems to work fine even in qemu now (qemu v 5.2.0).
This commit is contained in:
parent
496d2e3c29
commit
bc213ad7a2
4 changed files with 91 additions and 1 deletions
51
Kernel/Prekernel/Arch/aarch64/Timer.cpp
Normal file
51
Kernel/Prekernel/Arch/aarch64/Timer.cpp
Normal file
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Nico Weber <thakis@chromium.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <Kernel/Prekernel/Arch/aarch64/MMIO.h>
|
||||
#include <Kernel/Prekernel/Arch/aarch64/Timer.h>
|
||||
|
||||
namespace Prekernel {
|
||||
|
||||
// "12.1 System Timer Registers" / "10.2 System Timer Registers"
|
||||
struct TimerRegisters {
|
||||
u32 control_and_status;
|
||||
u32 counter_low;
|
||||
u32 counter_high;
|
||||
u32 compare[4];
|
||||
};
|
||||
|
||||
// Bits of the `control_and_status` register.
|
||||
// See "CS register" in Broadcom doc for details.
|
||||
enum FlagBits {
|
||||
SystemTimerMatch0 = 1 << 0,
|
||||
SystemTimerMatch1 = 1 << 1,
|
||||
SystemTimerMatch2 = 1 << 2,
|
||||
SystemTimerMatch3 = 1 << 3,
|
||||
};
|
||||
|
||||
Timer::Timer()
|
||||
: m_registers(MMIO::the().peripheral<TimerRegisters>(0x3000))
|
||||
{
|
||||
}
|
||||
|
||||
Timer& Timer::the()
|
||||
{
|
||||
static Timer instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
u64 Timer::microseconds_since_boot()
|
||||
{
|
||||
u32 high = m_registers->counter_high;
|
||||
u32 low = m_registers->counter_low;
|
||||
if (high != m_registers->counter_high) {
|
||||
high = m_registers->counter_high;
|
||||
low = m_registers->counter_low;
|
||||
}
|
||||
return (static_cast<u64>(high) << 32) | low;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue