1#include <acpi/hpet.h>
2#include <kassert.h>
3#include <time/clock.h>
4#include <time/names.h>
5
6#include "internal.h"
7
8static uint64_t hpet_clock_read(struct clock *clk) {
9 (void) clk;
10 return hpet_read64(HPET_MAIN_COUNTER_OFFSET);
11}
12
13struct clock *hpet_clock_init(void) {
14 if (!hpet_base)
15 return NULL;
16
17 struct clock *clk = clock_create(CLOCK_NAME_HPET);
18 if (!clk)
19 return NULL;
20
21 /*
22 * hpet_fs_per_tick is femtoseconds (10^-15 s) per tick.
23 * freq_hz = 10^15 / hpet_fs_per_tick.
24 * freq_khz = 10^12 / hpet_fs_per_tick.
25 */
26 freq_khz_t freq_khz = 1000000000000ULL / hpet_fs_per_tick;
27
28 clk->read = hpet_clock_read;
29 clk->frequency_khz = freq_khz;
30 clk->mult = clock_frequency_to_mult(clock: clk);
31 clk->state = CLOCK_STATE_ON;
32 clk->rating = CLOCK_RATING_GOOD;
33 clk->flags = CLOCK_FLAG_HRES;
34
35 clock_register(c: clk);
36 return clk;
37}
38