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 | |
| 7 | #include <common.h> |
| 8 | #include <clk.h> |
| 9 | #include <dm.h> |
| 10 | #include <timer.h> |
| 11 | #include <asm/io.h> |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 12 | #include <dm/device-internal.h> |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 13 | #include <linux/err.h> |
| 14 | |
Bin Meng | 51e3855 | 2023-06-21 23:11:44 +0800 | [diff] [blame] | 15 | #define CLINT_MTIME_OFFSET 0xbff8 |
| 16 | #define ACLINT_MTIME_OFFSET 0 |
| 17 | |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 18 | /* mtime register */ |
Bin Meng | 51e3855 | 2023-06-21 23:11:44 +0800 | [diff] [blame] | 19 | #define MTIME_REG(base, offset) ((ulong)(base) + (offset)) |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 20 | |
Bin Meng | b5f0372 | 2023-06-21 23:11:46 +0800 | [diff] [blame] | 21 | static u64 notrace riscv_aclint_timer_get_count(struct udevice *dev) |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 22 | { |
Bin Meng | 51e3855 | 2023-06-21 23:11:44 +0800 | [diff] [blame] | 23 | return readq((void __iomem *)MTIME_REG(dev_get_priv(dev), |
| 24 | dev_get_driver_data(dev))); |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 25 | } |
| 26 | |
Pragnesh Patel | 02038c3 | 2021-01-17 18:11:25 +0530 | [diff] [blame] | 27 | #if CONFIG_IS_ENABLED(RISCV_MMODE) && IS_ENABLED(CONFIG_TIMER_EARLY) |
| 28 | /** |
| 29 | * timer_early_get_rate() - Get the timer rate before driver model |
| 30 | */ |
| 31 | unsigned long notrace timer_early_get_rate(void) |
| 32 | { |
| 33 | return RISCV_MMODE_TIMER_FREQ; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * timer_early_get_count() - Get the timer count before driver model |
| 38 | * |
| 39 | */ |
| 40 | u64 notrace timer_early_get_count(void) |
| 41 | { |
Bin Meng | 51e3855 | 2023-06-21 23:11:44 +0800 | [diff] [blame] | 42 | return readq((void __iomem *)MTIME_REG(RISCV_MMODE_TIMERBASE, |
| 43 | RISCV_MMODE_TIMEROFF)); |
Pragnesh Patel | 02038c3 | 2021-01-17 18:11:25 +0530 | [diff] [blame] | 44 | } |
| 45 | #endif |
| 46 | |
Bin Meng | b5f0372 | 2023-06-21 23:11:46 +0800 | [diff] [blame] | 47 | static const struct timer_ops riscv_aclint_timer_ops = { |
| 48 | .get_count = riscv_aclint_timer_get_count, |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 49 | }; |
| 50 | |
Bin Meng | b5f0372 | 2023-06-21 23:11:46 +0800 | [diff] [blame] | 51 | static int riscv_aclint_timer_probe(struct udevice *dev) |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 52 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 53 | dev_set_priv(dev, dev_read_addr_ptr(dev)); |
| 54 | if (!dev_get_priv(dev)) |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 55 | return -EINVAL; |
| 56 | |
| 57 | return timer_timebase_fallback(dev); |
| 58 | } |
| 59 | |
Bin Meng | b5f0372 | 2023-06-21 23:11:46 +0800 | [diff] [blame] | 60 | static const struct udevice_id riscv_aclint_timer_ids[] = { |
Bin Meng | 51e3855 | 2023-06-21 23:11:44 +0800 | [diff] [blame] | 61 | { .compatible = "riscv,clint0", .data = CLINT_MTIME_OFFSET }, |
| 62 | { .compatible = "sifive,clint0", .data = CLINT_MTIME_OFFSET }, |
| 63 | { .compatible = "riscv,aclint-mtimer", .data = ACLINT_MTIME_OFFSET }, |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 64 | { } |
| 65 | }; |
| 66 | |
Bin Meng | b5f0372 | 2023-06-21 23:11:46 +0800 | [diff] [blame] | 67 | U_BOOT_DRIVER(riscv_aclint_timer) = { |
| 68 | .name = "riscv_aclint_timer", |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 69 | .id = UCLASS_TIMER, |
Bin Meng | b5f0372 | 2023-06-21 23:11:46 +0800 | [diff] [blame] | 70 | .of_match = riscv_aclint_timer_ids, |
| 71 | .probe = riscv_aclint_timer_probe, |
| 72 | .ops = &riscv_aclint_timer_ops, |
Sean Anderson | 52a1db7 | 2020-10-25 21:46:58 -0400 | [diff] [blame] | 73 | .flags = DM_FLAG_PRE_RELOC, |
| 74 | }; |