Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 1 | // 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 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 7 | #include <config.h> |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 8 | #include <clk.h> |
Chanho Park | 229f469 | 2023-09-06 14:18:14 +0900 | [diff] [blame] | 9 | #include <div64.h> |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <timer.h> |
| 12 | #include <asm/io.h> |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 13 | #include <dm/device-internal.h> |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 14 | #include <linux/err.h> |
| 15 | |
Bin Meng | 51e3855 | 2023-06-21 23:11:44 +0800 | [diff] [blame] | 16 | #define CLINT_MTIME_OFFSET 0xbff8 |
| 17 | #define ACLINT_MTIME_OFFSET 0 |
| 18 | |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 19 | /* mtime register */ |
Bin Meng | 51e3855 | 2023-06-21 23:11:44 +0800 | [diff] [blame] | 20 | #define MTIME_REG(base, offset) ((ulong)(base) + (offset)) |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 21 | |
Bin Meng | b5f0372 | 2023-06-21 23:11:46 +0800 | [diff] [blame] | 22 | static u64 notrace riscv_aclint_timer_get_count(struct udevice *dev) |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 23 | { |
Bin Meng | 51e3855 | 2023-06-21 23:11:44 +0800 | [diff] [blame] | 24 | return readq((void __iomem *)MTIME_REG(dev_get_priv(dev), |
| 25 | dev_get_driver_data(dev))); |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 26 | } |
| 27 | |
Pragnesh Patel | 02038c3 | 2021-01-17 18:11:25 +0530 | [diff] [blame] | 28 | #if CONFIG_IS_ENABLED(RISCV_MMODE) && IS_ENABLED(CONFIG_TIMER_EARLY) |
| 29 | /** |
| 30 | * timer_early_get_rate() - Get the timer rate before driver model |
| 31 | */ |
| 32 | unsigned long notrace timer_early_get_rate(void) |
| 33 | { |
| 34 | return RISCV_MMODE_TIMER_FREQ; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * timer_early_get_count() - Get the timer count before driver model |
| 39 | * |
| 40 | */ |
| 41 | u64 notrace timer_early_get_count(void) |
| 42 | { |
Bin Meng | 51e3855 | 2023-06-21 23:11:44 +0800 | [diff] [blame] | 43 | return readq((void __iomem *)MTIME_REG(RISCV_MMODE_TIMERBASE, |
| 44 | RISCV_MMODE_TIMEROFF)); |
Pragnesh Patel | 02038c3 | 2021-01-17 18:11:25 +0530 | [diff] [blame] | 45 | } |
| 46 | #endif |
| 47 | |
Chanho Park | 229f469 | 2023-09-06 14:18:14 +0900 | [diff] [blame] | 48 | #if CONFIG_IS_ENABLED(RISCV_MMODE) && CONFIG_IS_ENABLED(BOOTSTAGE) |
| 49 | ulong timer_get_boot_us(void) |
| 50 | { |
| 51 | int ret; |
| 52 | u64 ticks = 0; |
| 53 | u32 rate; |
| 54 | |
| 55 | ret = dm_timer_init(); |
| 56 | if (!ret) { |
| 57 | rate = timer_get_rate(gd->timer); |
| 58 | timer_get_count(gd->timer, &ticks); |
| 59 | } else { |
| 60 | rate = RISCV_MMODE_TIMER_FREQ; |
| 61 | ticks = readq((void __iomem *)MTIME_REG(RISCV_MMODE_TIMERBASE, |
| 62 | RISCV_MMODE_TIMEROFF)); |
| 63 | } |
| 64 | |
| 65 | /* Below is converted from time(us) = (tick / rate) * 10000000 */ |
| 66 | return lldiv(ticks * 1000, (rate / 1000)); |
| 67 | } |
| 68 | #endif |
| 69 | |
Bin Meng | b5f0372 | 2023-06-21 23:11:46 +0800 | [diff] [blame] | 70 | static const struct timer_ops riscv_aclint_timer_ops = { |
| 71 | .get_count = riscv_aclint_timer_get_count, |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 72 | }; |
| 73 | |
Bin Meng | b5f0372 | 2023-06-21 23:11:46 +0800 | [diff] [blame] | 74 | static int riscv_aclint_timer_probe(struct udevice *dev) |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 75 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 76 | dev_set_priv(dev, dev_read_addr_ptr(dev)); |
| 77 | if (!dev_get_priv(dev)) |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 78 | return -EINVAL; |
| 79 | |
| 80 | return timer_timebase_fallback(dev); |
| 81 | } |
| 82 | |
Bin Meng | b5f0372 | 2023-06-21 23:11:46 +0800 | [diff] [blame] | 83 | static const struct udevice_id riscv_aclint_timer_ids[] = { |
Bin Meng | 51e3855 | 2023-06-21 23:11:44 +0800 | [diff] [blame] | 84 | { .compatible = "riscv,clint0", .data = CLINT_MTIME_OFFSET }, |
| 85 | { .compatible = "sifive,clint0", .data = CLINT_MTIME_OFFSET }, |
| 86 | { .compatible = "riscv,aclint-mtimer", .data = ACLINT_MTIME_OFFSET }, |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 87 | { } |
| 88 | }; |
| 89 | |
Bin Meng | b5f0372 | 2023-06-21 23:11:46 +0800 | [diff] [blame] | 90 | U_BOOT_DRIVER(riscv_aclint_timer) = { |
| 91 | .name = "riscv_aclint_timer", |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 92 | .id = UCLASS_TIMER, |
Bin Meng | b5f0372 | 2023-06-21 23:11:46 +0800 | [diff] [blame] | 93 | .of_match = riscv_aclint_timer_ids, |
| 94 | .probe = riscv_aclint_timer_probe, |
| 95 | .ops = &riscv_aclint_timer_ops, |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 96 | .flags = DM_FLAG_PRE_RELOC, |
| 97 | }; |