1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2019, Rick Chen <rick@andestech.com>
4 * Copyright (C) 2020, Sean Anderson <seanga2@gmail.com>
5 *
6 * U-Boot syscon driver for Andes's Platform Level Machine Timer (PLMT).
7 * The PLMT block holds memory-mapped mtime register
8 * associated with timer tick.
9 */
10
11 #include <dm.h>
12 #include <timer.h>
13 #include <asm/io.h>
14 #include <dm/device-internal.h>
15 #include <linux/err.h>
16
17 /* mtime register */
18 #define MTIME_REG(base) ((ulong)(base))
19
andes_plmt_get_count(struct udevice * dev)20 static u64 notrace andes_plmt_get_count(struct udevice *dev)
21 {
22 return readq((void __iomem *)MTIME_REG(dev_get_priv(dev)));
23 }
24
25 #if CONFIG_IS_ENABLED(RISCV_MMODE) && IS_ENABLED(CONFIG_TIMER_EARLY)
26 /**
27 * timer_early_get_rate() - Get the timer rate before driver model
28 */
timer_early_get_rate(void)29 unsigned long notrace timer_early_get_rate(void)
30 {
31 return RISCV_MMODE_TIMER_FREQ;
32 }
33
34 /**
35 * timer_early_get_count() - Get the timer count before driver model
36 *
37 */
timer_early_get_count(void)38 u64 notrace timer_early_get_count(void)
39 {
40 return readq((void __iomem *)MTIME_REG(RISCV_MMODE_TIMERBASE));
41 }
42 #endif
43
44 static const struct timer_ops andes_plmt_ops = {
45 .get_count = andes_plmt_get_count,
46 };
47
andes_plmt_probe(struct udevice * dev)48 static int andes_plmt_probe(struct udevice *dev)
49 {
50 dev_set_priv(dev, dev_read_addr_ptr(dev));
51 if (!dev_get_priv(dev))
52 return -EINVAL;
53
54 return timer_timebase_fallback(dev);
55 }
56
57 static const struct udevice_id andes_plmt_ids[] = {
58 { .compatible = "andestech,plmt0" },
59 { }
60 };
61
62 U_BOOT_DRIVER(andes_plmt) = {
63 .name = "andes_plmt",
64 .id = UCLASS_TIMER,
65 .of_match = andes_plmt_ids,
66 .ops = &andes_plmt_ops,
67 .probe = andes_plmt_probe,
68 .flags = DM_FLAG_PRE_RELOC,
69 };
70