AquilaOS documentation
How it works, and its code
Toggle main menu visibility
Loading...
Searching...
No Matches
hpet.c
Go to the documentation of this file.
1
14
15
#include <
core/system.h
>
16
#include <
core/arch.h
>
17
#include <
cpu/sdt.h
>
18
#include <
platform/misc.h
>
19
20
#define HPET_CAP 0x000
/* its high half is the period, in fs */
21
#define HPET_CNF 0x010
22
#define HPET_CNT 0x0F0
23
24
#define CNF_ENABLE 0x1
25
#define CNF_LEGACY 0x2
26
27
#define PERIOD_MAX_FS 100000000ULL
/* 100 ns, the most the specification allows */
28
29
static
volatile
uint32_t
*
regs
;
30
static
uint64_t
period_fs
;
31
33
static
inline
uint32_t
reg32
(
unsigned
off)
34
{
35
return
regs
[off / 4];
36
}
37
39
static
inline
void
reg32_write
(
unsigned
off,
uint32_t
val)
40
{
41
regs
[off / 4] = val;
42
}
43
51
uint64_t
x86_hpet_read
(
void
)
52
{
53
uint32_t
hi, lo;
54
55
do
{
56
hi =
reg32
(
HPET_CNT
+ 4);
57
lo =
reg32
(
HPET_CNT
);
58
}
while
(hi !=
reg32
(
HPET_CNT
+ 4));
59
60
return
(
uint64_t
) hi << 32 | lo;
61
}
62
69
uint32_t
x86_hpet_read32
(
void
)
70
{
71
return
reg32
(
HPET_CNT
);
72
}
73
79
uint64_t
x86_hpet_period_fs
(
void
)
80
{
81
return
period_fs
;
82
}
83
90
int
x86_hpet_init
(
void
)
91
{
92
struct
acpi_hpet
*t =
x86_acpi_table
?
x86_acpi_table
(
"HPET"
) :
NULL
;
93
94
if
(!t || t->
header
.
length
<
sizeof
(*t) || t->
address
.
space
!= 0 || !t->
address
.
address
)
95
return
-ENODEV;
96
97
regs
=
arch_mmio_map
(t->
address
.
address
, 1024);
98
99
if
(!
regs
)
100
return
-ENOMEM;
101
102
period_fs
=
reg32
(
HPET_CAP
+ 4);
103
104
if
(!
period_fs
||
period_fs
>
PERIOD_MAX_FS
) {
105
printk
(
"hpet: bad period %ld fs\n"
,
period_fs
);
106
return
-ENODEV;
107
}
108
109
/* start counting from 0, leaving the PIT as it is */
110
reg32_write
(
HPET_CNF
,
reg32
(
HPET_CNF
) & ~(
CNF_ENABLE
|
CNF_LEGACY
));
111
reg32_write
(
HPET_CNT
, 0);
112
reg32_write
(
HPET_CNT
+ 4, 0);
113
reg32_write
(
HPET_CNF
, (
reg32
(
HPET_CNF
) & ~
CNF_LEGACY
) |
CNF_ENABLE
);
114
115
printk
(
"hpet: at %p, %ld fs a count\n"
, (
void
*) (
uintptr_t
) t->
address
.
address
,
period_fs
);
116
return
0;
117
}
PERIOD_MAX_FS
#define PERIOD_MAX_FS
Definition
hpet.c:27
period_fs
static uint64_t period_fs
Definition
hpet.c:30
HPET_CNF
#define HPET_CNF
Definition
hpet.c:21
CNF_LEGACY
#define CNF_LEGACY
Definition
hpet.c:25
CNF_ENABLE
#define CNF_ENABLE
Definition
hpet.c:24
HPET_CNT
#define HPET_CNT
Definition
hpet.c:22
HPET_CAP
#define HPET_CAP
Definition
hpet.c:20
regs
static volatile uint32_t * regs
Definition
hpet.c:29
reg32_write
static void reg32_write(unsigned off, uint32_t val)
Writes a 32-bit HPET register.
Definition
hpet.c:39
reg32
static uint32_t reg32(unsigned off)
Reads a 32-bit HPET register.
Definition
hpet.c:33
arch.h
system.h
misc.h
x86_hpet_init
int x86_hpet_init(void)
Finds the HPET and starts its counter.
Definition
hpet.c:90
x86_hpet_read
uint64_t x86_hpet_read(void)
Reads the HPET's 64-bit counter.
Definition
hpet.c:51
x86_acpi_table
void * x86_acpi_table(const char *signature)
Finds one of ACPI's tables, by its signature.
Definition
acpi.c:183
x86_hpet_read32
uint32_t x86_hpet_read32(void)
Reads the low half of the HPET's counter only: a single read, which matters because each read is slow...
Definition
hpet.c:69
x86_hpet_period_fs
uint64_t x86_hpet_period_fs(void)
Returns the HPET counter's period.
Definition
hpet.c:79
arch_mmio_map
void * arch_mmio_map(uintptr_t paddr, size_t size)
Maps a device's registers into the kernel's address space, uncached (memory-mapped I/O).
Definition
pmap.c:508
printk
int printk(const char *fmt,...)
Writes a kernel message, to the message buffer and the consoles.
Definition
printk.c:249
sdt.h
NULL
#define NULL
Definition
stddef.h:27
uintptr_t
__SIZE_TYPE__ uintptr_t
Definition
stddef.h:12
uint32_t
unsigned int uint32_t
Definition
stddef.h:22
uint64_t
unsigned long long int uint64_t
Definition
stddef.h:23
acpi_gas::address
uint64_t address
Definition
sdt.h:59
acpi_gas::space
uint8_t space
Definition
sdt.h:55
acpi_hpet
The HPET table: where the HPET's registers are.
Definition
sdt.h:63
acpi_hpet::header
struct acpi_sdt_header header
Definition
sdt.h:64
acpi_hpet::address
struct acpi_gas address
Definition
sdt.h:66
acpi_sdt_header::length
uint32_t length
Definition
sdt.h:31
kernel
arch
i386
platform
misc
hpet.c
Generated by
1.18.0