blob: de23b85404b66cf2a4cb91beb6b68ad7ed70289c [file] [log] [blame]
Sean Anderson47d7e3b2020-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 Glass0fd3d912020-12-22 19:30:28 -070012#include <dm/device-internal.h>
Sean Anderson47d7e3b2020-10-25 21:46:58 -040013#include <linux/err.h>
14
15/* mtime register */
16#define MTIME_REG(base) ((ulong)(base) + 0xbff8)
17
18static u64 sifive_clint_get_count(struct udevice *dev)
19{
Simon Glass0fd3d912020-12-22 19:30:28 -070020 return readq((void __iomem *)MTIME_REG(dev_get_priv(dev)));
Sean Anderson47d7e3b2020-10-25 21:46:58 -040021}
22
23static const struct timer_ops sifive_clint_ops = {
24 .get_count = sifive_clint_get_count,
25};
26
27static int sifive_clint_probe(struct udevice *dev)
28{
Simon Glass0fd3d912020-12-22 19:30:28 -070029 dev_set_priv(dev, dev_read_addr_ptr(dev));
30 if (!dev_get_priv(dev))
Sean Anderson47d7e3b2020-10-25 21:46:58 -040031 return -EINVAL;
32
33 return timer_timebase_fallback(dev);
34}
35
36static const struct udevice_id sifive_clint_ids[] = {
37 { .compatible = "riscv,clint0" },
38 { }
39};
40
41U_BOOT_DRIVER(sifive_clint) = {
42 .name = "sifive_clint",
43 .id = UCLASS_TIMER,
44 .of_match = sifive_clint_ids,
45 .probe = sifive_clint_probe,
46 .ops = &sifive_clint_ops,
47 .flags = DM_FLAG_PRE_RELOC,
48};