blob: 20baaf61307aa3f6a65165a5f17e7f36915634fc [file] [log] [blame]
Rick Chen73766772019-04-02 15:56:40 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019, Rick Chen <rick@andestech.com>
Sean Anderson87e6ce52020-09-28 10:52:24 -04004 * Copyright (C) 2020, Sean Anderson <seanga2@gmail.com>
Rick Chen73766772019-04-02 15:56:40 +08005 *
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
Rick Chen73766772019-04-02 15:56:40 +080011#include <dm.h>
Sean Anderson87e6ce52020-09-28 10:52:24 -040012#include <timer.h>
Rick Chen73766772019-04-02 15:56:40 +080013#include <asm/io.h>
Simon Glass95588622020-12-22 19:30:28 -070014#include <dm/device-internal.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070015#include <linux/err.h>
Rick Chen73766772019-04-02 15:56:40 +080016
17/* mtime register */
18#define MTIME_REG(base) ((ulong)(base))
19
Pragnesh Patel02038c32021-01-17 18:11:25 +053020static u64 notrace andes_plmt_get_count(struct udevice *dev)
Sean Anderson87e6ce52020-09-28 10:52:24 -040021{
Simon Glass95588622020-12-22 19:30:28 -070022 return readq((void __iomem *)MTIME_REG(dev_get_priv(dev)));
Sean Anderson87e6ce52020-09-28 10:52:24 -040023}
Rick Chen73766772019-04-02 15:56:40 +080024
Pragnesh Patel02038c32021-01-17 18:11:25 +053025#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 */
29unsigned 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 */
38u64 notrace timer_early_get_count(void)
39{
40 return readq((void __iomem *)MTIME_REG(RISCV_MMODE_TIMERBASE));
41}
42#endif
43
Sean Anderson87e6ce52020-09-28 10:52:24 -040044static const struct timer_ops andes_plmt_ops = {
45 .get_count = andes_plmt_get_count,
46};
Rick Chen73766772019-04-02 15:56:40 +080047
Sean Anderson87e6ce52020-09-28 10:52:24 -040048static int andes_plmt_probe(struct udevice *dev)
49{
Simon Glass95588622020-12-22 19:30:28 -070050 dev_set_priv(dev, dev_read_addr_ptr(dev));
51 if (!dev_get_priv(dev))
Sean Anderson87e6ce52020-09-28 10:52:24 -040052 return -EINVAL;
Rick Chen73766772019-04-02 15:56:40 +080053
Sean Anderson87e6ce52020-09-28 10:52:24 -040054 return timer_timebase_fallback(dev);
Rick Chen73766772019-04-02 15:56:40 +080055}
56
57static const struct udevice_id andes_plmt_ids[] = {
Yu Chien Peter Lin739cd6f2022-10-25 23:03:50 +080058 { .compatible = "andestech,plmt0" },
Rick Chen73766772019-04-02 15:56:40 +080059 { }
60};
61
62U_BOOT_DRIVER(andes_plmt) = {
63 .name = "andes_plmt",
Sean Anderson87e6ce52020-09-28 10:52:24 -040064 .id = UCLASS_TIMER,
Rick Chen73766772019-04-02 15:56:40 +080065 .of_match = andes_plmt_ids,
Sean Anderson87e6ce52020-09-28 10:52:24 -040066 .ops = &andes_plmt_ops,
67 .probe = andes_plmt_probe,
Rick Chen73766772019-04-02 15:56:40 +080068 .flags = DM_FLAG_PRE_RELOC,
69};