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