summaryrefslogtreecommitdiff
path: root/src/soc/marvell/armada38x/monotonic_timer.c
blob: 49fa6cd76f16c8f214df90cabd9ce31262e1be4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * This file is part of the coreboot project.
 *
 * Copyright 2015 Google Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <arch/io.h>
#include <timer.h>
#include <delay.h>

#define TIMER_CTRL_REG 0xf1020300
#define TIMER_RELOAD_REG 0xf1020310
#define TIMER_REG 0xf1020314
#define TIMER_RELOAD_VALUE 0xffffffff

#define MHZ_NUM 25

void init_timer(void)
{
	unsigned int reg;

	/* Set the reload timer */
	write32((void *)TIMER_RELOAD_REG, TIMER_RELOAD_VALUE);
	/* Set the initial value to TIMER_RELOAD_VALUE - 1
	 * (instead of TIMER_RELOAD_VALUE) to avoid 'fake'
	 * overflow being detected in timer_monotonic_get
	 * if it's called close enough with 'this' function */
	write32((void *)TIMER_REG, TIMER_RELOAD_VALUE - 1);
	reg = read32((const void *)TIMER_CTRL_REG);
	/* Let it start counting */
	reg |= 0x3;
	write32((void *)TIMER_CTRL_REG, reg);
}

void timer_monotonic_get(struct mono_time *mt)
{
	static uint64_t total_ticks = 0;
	uint64_t overflow = 0;
	uint32_t current_ticks =
		TIMER_RELOAD_VALUE - read32((const void *)TIMER_REG);

	/* Assuming at most one overflow happened since last call */
	if (current_ticks <= total_ticks)
		overflow = 1ULL << 32;

	total_ticks = (((total_ticks + overflow) >> 32) << 32) + current_ticks;
	mono_time_set_usecs(mt, total_ticks / MHZ_NUM);
}