AquilaOS documentation
How it works, and its code
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
29static volatile uint32_t *regs;
31
33static inline uint32_t reg32(unsigned off)
34{
35 return regs[off / 4];
36}
37
39static inline void reg32_write(unsigned off, uint32_t val)
40{
41 regs[off / 4] = val;
42}
43
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
70{
71 return reg32(HPET_CNT);
72}
73
80{
81 return period_fs;
82}
83
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
98
99 if (!regs)
100 return -ENOMEM;
101
102 period_fs = reg32(HPET_CAP + 4);
103
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 */
112 reg32_write(HPET_CNT + 4, 0);
114
115 printk("hpet: at %p, %ld fs a count\n", (void *) (uintptr_t) t->address.address, period_fs);
116 return 0;
117}
#define PERIOD_MAX_FS
Definition hpet.c:27
static uint64_t period_fs
Definition hpet.c:30
#define HPET_CNF
Definition hpet.c:21
#define CNF_LEGACY
Definition hpet.c:25
#define CNF_ENABLE
Definition hpet.c:24
#define HPET_CNT
Definition hpet.c:22
#define HPET_CAP
Definition hpet.c:20
static volatile uint32_t * regs
Definition hpet.c:29
static void reg32_write(unsigned off, uint32_t val)
Writes a 32-bit HPET register.
Definition hpet.c:39
static uint32_t reg32(unsigned off)
Reads a 32-bit HPET register.
Definition hpet.c:33
int x86_hpet_init(void)
Finds the HPET and starts its counter.
Definition hpet.c:90
uint64_t x86_hpet_read(void)
Reads the HPET's 64-bit counter.
Definition hpet.c:51
void * x86_acpi_table(const char *signature)
Finds one of ACPI's tables, by its signature.
Definition acpi.c:183
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
uint64_t x86_hpet_period_fs(void)
Returns the HPET counter's period.
Definition hpet.c:79
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
int printk(const char *fmt,...)
Writes a kernel message, to the message buffer and the consoles.
Definition printk.c:249
#define NULL
Definition stddef.h:27
__SIZE_TYPE__ uintptr_t
Definition stddef.h:12
unsigned int uint32_t
Definition stddef.h:22
unsigned long long int uint64_t
Definition stddef.h:23
uint64_t address
Definition sdt.h:59
uint8_t space
Definition sdt.h:55
The HPET table: where the HPET's registers are.
Definition sdt.h:63
struct acpi_sdt_header header
Definition sdt.h:64
struct acpi_gas address
Definition sdt.h:66
uint32_t length
Definition sdt.h:31