blob: 449fcfcfd5964c4c201f56bc3f861084c14f3dfb [file] [log] [blame]
Bin Meng25399032018-12-12 06:12:27 -08001// SPDX-License-Identifier: GPL-2.0+
2/*
Sean Anderson9baaaef2020-09-28 10:52:21 -04003 * Copyright (C) 2020, Sean Anderson <seanga2@gmail.com>
Bin Meng25399032018-12-12 06:12:27 -08004 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
Sean Anderson9baaaef2020-09-28 10:52:21 -04005 * Copyright (C) 2018, Anup Patel <anup@brainfault.org>
6 * Copyright (C) 2012 Regents of the University of California
Bin Meng25399032018-12-12 06:12:27 -08007 *
Sean Anderson9baaaef2020-09-28 10:52:21 -04008 * RISC-V architecturally-defined generic timer driver
Bin Meng25399032018-12-12 06:12:27 -08009 *
Sean Anderson9baaaef2020-09-28 10:52:21 -040010 * This driver provides generic timer support for S-mode U-Boot.
Bin Meng25399032018-12-12 06:12:27 -080011 */
12
13#include <common.h>
14#include <dm.h>
15#include <errno.h>
16#include <timer.h>
Sean Anderson9baaaef2020-09-28 10:52:21 -040017#include <asm/csr.h>
Bin Meng25399032018-12-12 06:12:27 -080018
19static int riscv_timer_get_count(struct udevice *dev, u64 *count)
20{
Sean Anderson9baaaef2020-09-28 10:52:21 -040021 if (IS_ENABLED(CONFIG_64BIT)) {
22 *count = csr_read(CSR_TIME);
23 } else {
24 u32 hi, lo;
25
26 do {
27 hi = csr_read(CSR_TIMEH);
28 lo = csr_read(CSR_TIME);
29 } while (hi != csr_read(CSR_TIMEH));
30
31 *count = ((u64)hi << 32) | lo;
32 }
33
34 return 0;
Bin Meng25399032018-12-12 06:12:27 -080035}
36
37static int riscv_timer_probe(struct udevice *dev)
38{
39 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
40
41 /* clock frequency was passed from the cpu driver as driver data */
42 uc_priv->clock_rate = dev->driver_data;
43
44 return 0;
45}
46
47static const struct timer_ops riscv_timer_ops = {
48 .get_count = riscv_timer_get_count,
49};
50
51U_BOOT_DRIVER(riscv_timer) = {
52 .name = "riscv_timer",
53 .id = UCLASS_TIMER,
54 .probe = riscv_timer_probe,
55 .ops = &riscv_timer_ops,
56 .flags = DM_FLAG_PRE_RELOC,
57};