Bin Meng | 60262cd0 | 2018-12-12 06:12:27 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Sean Anderson | c33efaf | 2020-09-28 10:52:21 -0400 | [diff] [blame] | 3 | * Copyright (C) 2020, Sean Anderson <seanga2@gmail.com> |
Bin Meng | 60262cd0 | 2018-12-12 06:12:27 -0800 | [diff] [blame] | 4 | * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com> |
Sean Anderson | c33efaf | 2020-09-28 10:52:21 -0400 | [diff] [blame] | 5 | * Copyright (C) 2018, Anup Patel <anup@brainfault.org> |
| 6 | * Copyright (C) 2012 Regents of the University of California |
Bin Meng | 60262cd0 | 2018-12-12 06:12:27 -0800 | [diff] [blame] | 7 | * |
Sean Anderson | c33efaf | 2020-09-28 10:52:21 -0400 | [diff] [blame] | 8 | * RISC-V architecturally-defined generic timer driver |
Bin Meng | 60262cd0 | 2018-12-12 06:12:27 -0800 | [diff] [blame] | 9 | * |
Sean Anderson | c33efaf | 2020-09-28 10:52:21 -0400 | [diff] [blame] | 10 | * This driver provides generic timer support for S-mode U-Boot. |
Bin Meng | 60262cd0 | 2018-12-12 06:12:27 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
Chanho Park | 74fbd74 | 2023-09-06 14:18:13 +0900 | [diff] [blame] | 14 | #include <div64.h> |
Bin Meng | 60262cd0 | 2018-12-12 06:12:27 -0800 | [diff] [blame] | 15 | #include <dm.h> |
| 16 | #include <errno.h> |
Torsten Duwe | f39f8f7 | 2023-08-14 18:05:28 +0200 | [diff] [blame] | 17 | #include <fdt_support.h> |
Bin Meng | 60262cd0 | 2018-12-12 06:12:27 -0800 | [diff] [blame] | 18 | #include <timer.h> |
Sean Anderson | c33efaf | 2020-09-28 10:52:21 -0400 | [diff] [blame] | 19 | #include <asm/csr.h> |
Bin Meng | 60262cd0 | 2018-12-12 06:12:27 -0800 | [diff] [blame] | 20 | |
Pragnesh Patel | bc8d12b | 2021-01-17 18:11:25 +0530 | [diff] [blame] | 21 | static u64 notrace riscv_timer_get_count(struct udevice *dev) |
Bin Meng | 60262cd0 | 2018-12-12 06:12:27 -0800 | [diff] [blame] | 22 | { |
Sean Anderson | 8af7bb9 | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 23 | __maybe_unused u32 hi, lo; |
Sean Anderson | c33efaf | 2020-09-28 10:52:21 -0400 | [diff] [blame] | 24 | |
Sean Anderson | 8af7bb9 | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 25 | if (IS_ENABLED(CONFIG_64BIT)) |
| 26 | return csr_read(CSR_TIME); |
Sean Anderson | c33efaf | 2020-09-28 10:52:21 -0400 | [diff] [blame] | 27 | |
Sean Anderson | 8af7bb9 | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 28 | do { |
| 29 | hi = csr_read(CSR_TIMEH); |
| 30 | lo = csr_read(CSR_TIME); |
| 31 | } while (hi != csr_read(CSR_TIMEH)); |
Sean Anderson | c33efaf | 2020-09-28 10:52:21 -0400 | [diff] [blame] | 32 | |
Sean Anderson | 8af7bb9 | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 33 | return ((u64)hi << 32) | lo; |
Bin Meng | 60262cd0 | 2018-12-12 06:12:27 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Pragnesh Patel | bc8d12b | 2021-01-17 18:11:25 +0530 | [diff] [blame] | 36 | #if CONFIG_IS_ENABLED(RISCV_SMODE) && IS_ENABLED(CONFIG_TIMER_EARLY) |
| 37 | /** |
| 38 | * timer_early_get_rate() - Get the timer rate before driver model |
| 39 | */ |
| 40 | unsigned long notrace timer_early_get_rate(void) |
| 41 | { |
| 42 | return RISCV_SMODE_TIMER_FREQ; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * timer_early_get_count() - Get the timer count before driver model |
| 47 | * |
| 48 | */ |
| 49 | u64 notrace timer_early_get_count(void) |
| 50 | { |
| 51 | return riscv_timer_get_count(NULL); |
| 52 | } |
| 53 | #endif |
| 54 | |
Chanho Park | 74fbd74 | 2023-09-06 14:18:13 +0900 | [diff] [blame] | 55 | #if CONFIG_IS_ENABLED(RISCV_SMODE) && CONFIG_IS_ENABLED(BOOTSTAGE) |
| 56 | ulong timer_get_boot_us(void) |
| 57 | { |
| 58 | int ret; |
| 59 | u64 ticks = 0; |
| 60 | u32 rate; |
| 61 | |
| 62 | ret = dm_timer_init(); |
| 63 | if (!ret) { |
| 64 | rate = timer_get_rate(gd->timer); |
| 65 | timer_get_count(gd->timer, &ticks); |
| 66 | } else { |
| 67 | rate = RISCV_SMODE_TIMER_FREQ; |
| 68 | ticks = riscv_timer_get_count(NULL); |
| 69 | } |
| 70 | |
| 71 | /* Below is converted from time(us) = (tick / rate) * 10000000 */ |
| 72 | return lldiv(ticks * 1000, (rate / 1000)); |
| 73 | } |
| 74 | #endif |
| 75 | |
Bin Meng | 60262cd0 | 2018-12-12 06:12:27 -0800 | [diff] [blame] | 76 | static int riscv_timer_probe(struct udevice *dev) |
| 77 | { |
| 78 | struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Torsten Duwe | f39f8f7 | 2023-08-14 18:05:28 +0200 | [diff] [blame] | 79 | u32 rate; |
Bin Meng | 60262cd0 | 2018-12-12 06:12:27 -0800 | [diff] [blame] | 80 | |
Torsten Duwe | f39f8f7 | 2023-08-14 18:05:28 +0200 | [diff] [blame] | 81 | /* When this function was called from the CPU driver, clock |
| 82 | * frequency is passed as driver data. |
| 83 | */ |
| 84 | rate = dev->driver_data; |
| 85 | |
| 86 | /* When called from an FDT match, the rate needs to be looked up. */ |
| 87 | if (!rate && gd->fdt_blob) { |
| 88 | rate = fdt_getprop_u32_default(gd->fdt_blob, |
| 89 | "/cpus", "timebase-frequency", 0); |
| 90 | } |
| 91 | |
| 92 | uc_priv->clock_rate = rate; |
| 93 | |
| 94 | /* With rate==0, timer uclass post_probe might later fail with -EINVAL. |
| 95 | * Give a hint at the cause for debugging. |
| 96 | */ |
| 97 | if (!rate) |
| 98 | log_err("riscv_timer_probe with invalid clock rate 0!\n"); |
Bin Meng | 60262cd0 | 2018-12-12 06:12:27 -0800 | [diff] [blame] | 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static const struct timer_ops riscv_timer_ops = { |
| 104 | .get_count = riscv_timer_get_count, |
| 105 | }; |
| 106 | |
Torsten Duwe | f39f8f7 | 2023-08-14 18:05:28 +0200 | [diff] [blame] | 107 | static const struct udevice_id riscv_timer_ids[] = { |
| 108 | { .compatible = "riscv,timer", }, |
| 109 | { } |
| 110 | }; |
| 111 | |
Bin Meng | 60262cd0 | 2018-12-12 06:12:27 -0800 | [diff] [blame] | 112 | U_BOOT_DRIVER(riscv_timer) = { |
| 113 | .name = "riscv_timer", |
| 114 | .id = UCLASS_TIMER, |
Torsten Duwe | f39f8f7 | 2023-08-14 18:05:28 +0200 | [diff] [blame] | 115 | .of_match = of_match_ptr(riscv_timer_ids), |
Bin Meng | 60262cd0 | 2018-12-12 06:12:27 -0800 | [diff] [blame] | 116 | .probe = riscv_timer_probe, |
| 117 | .ops = &riscv_timer_ops, |
| 118 | .flags = DM_FLAG_PRE_RELOC, |
| 119 | }; |