blob: 94db4188e7a063e7c120a57e7ac7a93eeb630ebd [file] [log] [blame]
Gabe Black3c7e9392013-05-26 07:15:57 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Gabe Black3c7e9392013-05-26 07:15:57 -070014 */
15
16#include <stdint.h>
17#include <delay.h>
18#include <timer.h>
19
20#include "dmtimer.h"
21
22static struct monotonic_counter {
23 int initialized;
24 struct mono_time time;
25 uint64_t last_value;
26} mono_counter;
27
28static const uint32_t clocks_per_usec = OSC_HZ/1000000;
29
30void timer_monotonic_get(struct mono_time *mt)
31{
32 uint64_t current_tick;
33 uint64_t usecs_elapsed;
34
35 if (!mono_counter.initialized) {
36 init_timer();
37 mono_counter.last_value = dmtimer_raw_value(0);
38 mono_counter.initialized = 1;
39 }
40
41 current_tick = dmtimer_raw_value(0);
42 usecs_elapsed = (current_tick - mono_counter.last_value) /
43 clocks_per_usec;
44
45 /* Update current time and tick values only if a full tick occurred. */
46 if (usecs_elapsed) {
47 mono_time_add_usecs(&mono_counter.time, usecs_elapsed);
48 mono_counter.last_value = current_tick;
49 }
50
51 /* Save result. */
52 *mt = mono_counter.time;
53}