blob: 939b99d937d166127ff12087cb050563f3619605 [file] [log] [blame]
Sean Anderson52a1db72020-10-25 21:46:58 -04001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2020, Sean Anderson <seanga2@gmail.com>
4 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
5 */
6
7#include <common.h>
8#include <clk.h>
9#include <dm.h>
10#include <timer.h>
11#include <asm/io.h>
Simon Glass95588622020-12-22 19:30:28 -070012#include <dm/device-internal.h>
Sean Anderson52a1db72020-10-25 21:46:58 -040013#include <linux/err.h>
14
15/* mtime register */
16#define MTIME_REG(base) ((ulong)(base) + 0xbff8)
17
Pragnesh Patel02038c32021-01-17 18:11:25 +053018static u64 notrace sifive_clint_get_count(struct udevice *dev)
Sean Anderson52a1db72020-10-25 21:46:58 -040019{
Simon Glass95588622020-12-22 19:30:28 -070020 return readq((void __iomem *)MTIME_REG(dev_get_priv(dev)));
Sean Anderson52a1db72020-10-25 21:46:58 -040021}
22
Pragnesh Patel02038c32021-01-17 18:11:25 +053023#if CONFIG_IS_ENABLED(RISCV_MMODE) && IS_ENABLED(CONFIG_TIMER_EARLY)
24/**
25 * timer_early_get_rate() - Get the timer rate before driver model
26 */
27unsigned long notrace timer_early_get_rate(void)
28{
29 return RISCV_MMODE_TIMER_FREQ;
30}
31
32/**
33 * timer_early_get_count() - Get the timer count before driver model
34 *
35 */
36u64 notrace timer_early_get_count(void)
37{
38 return readq((void __iomem *)MTIME_REG(RISCV_MMODE_TIMERBASE));
39}
40#endif
41
Sean Anderson52a1db72020-10-25 21:46:58 -040042static const struct timer_ops sifive_clint_ops = {
43 .get_count = sifive_clint_get_count,
44};
45
46static int sifive_clint_probe(struct udevice *dev)
47{
Simon Glass95588622020-12-22 19:30:28 -070048 dev_set_priv(dev, dev_read_addr_ptr(dev));
49 if (!dev_get_priv(dev))
Sean Anderson52a1db72020-10-25 21:46:58 -040050 return -EINVAL;
51
52 return timer_timebase_fallback(dev);
53}
54
55static const struct udevice_id sifive_clint_ids[] = {
56 { .compatible = "riscv,clint0" },
Bin Meng896cbfe2021-03-27 19:57:35 +080057 { .compatible = "sifive,clint0" },
Sean Anderson52a1db72020-10-25 21:46:58 -040058 { }
59};
60
61U_BOOT_DRIVER(sifive_clint) = {
62 .name = "sifive_clint",
63 .id = UCLASS_TIMER,
64 .of_match = sifive_clint_ids,
65 .probe = sifive_clint_probe,
66 .ops = &sifive_clint_ops,
67 .flags = DM_FLAG_PRE_RELOC,
68};