Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Thomas Chou | 7b059dc | 2015-10-30 15:35:52 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw> |
Thomas Chou | 7b059dc | 2015-10-30 15:35:52 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <errno.h> |
| 9 | #include <timer.h> |
| 10 | #include <os.h> |
| 11 | |
Simon Glass | 3c7c187b | 2016-02-24 09:14:51 -0700 | [diff] [blame] | 12 | #define SANDBOX_TIMER_RATE 1000000 |
| 13 | |
Thomas Chou | 7b059dc | 2015-10-30 15:35:52 +0800 | [diff] [blame] | 14 | /* system timer offset in ms */ |
| 15 | static unsigned long sandbox_timer_offset; |
| 16 | |
Neil Armstrong | 77c0dbc | 2019-04-11 17:01:23 +0200 | [diff] [blame] | 17 | void timer_test_add_offset(unsigned long offset) |
Thomas Chou | 7b059dc | 2015-10-30 15:35:52 +0800 | [diff] [blame] | 18 | { |
| 19 | sandbox_timer_offset += offset; |
| 20 | } |
| 21 | |
Simon Glass | 3c7c187b | 2016-02-24 09:14:51 -0700 | [diff] [blame] | 22 | u64 notrace timer_early_get_count(void) |
| 23 | { |
| 24 | return os_get_nsec() / 1000 + sandbox_timer_offset * 1000; |
| 25 | } |
| 26 | |
| 27 | unsigned long notrace timer_early_get_rate(void) |
Thomas Chou | 7b059dc | 2015-10-30 15:35:52 +0800 | [diff] [blame] | 28 | { |
Simon Glass | 3c7c187b | 2016-02-24 09:14:51 -0700 | [diff] [blame] | 29 | return SANDBOX_TIMER_RATE; |
| 30 | } |
| 31 | |
Sean Anderson | 947fc2d | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 32 | static notrace u64 sandbox_timer_get_count(struct udevice *dev) |
Simon Glass | 3c7c187b | 2016-02-24 09:14:51 -0700 | [diff] [blame] | 33 | { |
Sean Anderson | 947fc2d | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 34 | return timer_early_get_count(); |
Thomas Chou | 7b059dc | 2015-10-30 15:35:52 +0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static int sandbox_timer_probe(struct udevice *dev) |
| 38 | { |
Stephen Warren | 4faff28 | 2016-01-06 10:33:04 -0700 | [diff] [blame] | 39 | struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 40 | |
Simon Glass | 41d9706 | 2021-03-15 18:11:06 +1300 | [diff] [blame] | 41 | if (CONFIG_IS_ENABLED(CPU) && |
| 42 | dev_read_bool(dev, "sandbox,timebase-frequency-fallback")) |
Sean Anderson | 79d3bba | 2020-09-28 10:52:23 -0400 | [diff] [blame] | 43 | return timer_timebase_fallback(dev); |
| 44 | else if (!uc_priv->clock_rate) |
Simon Glass | 3c7c187b | 2016-02-24 09:14:51 -0700 | [diff] [blame] | 45 | uc_priv->clock_rate = SANDBOX_TIMER_RATE; |
Stephen Warren | 4faff28 | 2016-01-06 10:33:04 -0700 | [diff] [blame] | 46 | |
Thomas Chou | 7b059dc | 2015-10-30 15:35:52 +0800 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static const struct timer_ops sandbox_timer_ops = { |
| 51 | .get_count = sandbox_timer_get_count, |
| 52 | }; |
| 53 | |
| 54 | static const struct udevice_id sandbox_timer_ids[] = { |
| 55 | { .compatible = "sandbox,timer" }, |
| 56 | { } |
| 57 | }; |
| 58 | |
| 59 | U_BOOT_DRIVER(sandbox_timer) = { |
| 60 | .name = "sandbox_timer", |
| 61 | .id = UCLASS_TIMER, |
| 62 | .of_match = sandbox_timer_ids, |
| 63 | .probe = sandbox_timer_probe, |
| 64 | .ops = &sandbox_timer_ops, |
| 65 | .flags = DM_FLAG_PRE_RELOC, |
| 66 | }; |
Stephen Warren | 4faff28 | 2016-01-06 10:33:04 -0700 | [diff] [blame] | 67 | |
| 68 | /* This is here in case we don't have a device tree */ |
Heinrich Schuchardt | dcbc02e | 2023-02-22 01:39:18 +0100 | [diff] [blame] | 69 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 1d8364a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 70 | U_BOOT_DRVINFO(sandbox_timer_non_fdt) = { |
Stephen Warren | 4faff28 | 2016-01-06 10:33:04 -0700 | [diff] [blame] | 71 | .name = "sandbox_timer", |
| 72 | }; |
Heinrich Schuchardt | dcbc02e | 2023-02-22 01:39:18 +0100 | [diff] [blame] | 73 | #endif |