summaryrefslogtreecommitdiffstats
path: root/arch/riscv/lib/riscv_timer.c
blob: 919d77d4b5948a220e7ce50e7f7ca26bae35032e (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
58
59
60
61
62
63
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (C) 2017 Antony Pavlov <antonynpavlov@gmail.com>
 *
 * This file is part of barebox.
 *
 * 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.
 *
 */

/**
 * @file
 * @brief Clocksource based on RISC-V cycle CSR timer
 */

#include <init.h>
#include <of.h>
#include <linux/clk.h>
#include <clock.h>

static uint64_t rdcycle_read(void)
{
	register unsigned long __v;

	__asm__ __volatile__ ("rdcycle %0" : "=r" (__v));

	return __v;
}

static struct clocksource rdcycle_cs = {
	.read	= rdcycle_read,
	.mask	= CLOCKSOURCE_MASK(32),
};

static int rdcycle_cs_init(void)
{
	unsigned int cycle_frequency;

	/* default rate: 100 MHz */
	cycle_frequency = 100000000;

	if (IS_ENABLED(CONFIG_OFTREE)) {
		struct device_node *np;
		struct clk *clk;

		np = of_get_cpu_node(0, NULL);
		if (np) {
			clk = of_clk_get(np, 0);
			if (!IS_ERR(clk)) {
				cycle_frequency = clk_get_rate(clk);
			}
		}
	}

	clocks_calc_mult_shift(&rdcycle_cs.mult, &rdcycle_cs.shift,
		cycle_frequency, NSEC_PER_SEC, 10);

	return init_clock(&rdcycle_cs);
}
postcore_initcall(rdcycle_cs_init);