blob: c1baf3c69ece6ad206c0095e1656f2939b5fd42a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Thomas Chou7b059dc2015-10-30 15:35:52 +08002/*
3 * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
Thomas Chou7b059dc2015-10-30 15:35:52 +08004 */
5
Thomas Chou7b059dc2015-10-30 15:35:52 +08006#include <dm.h>
7#include <errno.h>
8#include <timer.h>
9#include <os.h>
10
Simon Glass3c7c187b2016-02-24 09:14:51 -070011#define SANDBOX_TIMER_RATE 1000000
12
Thomas Chou7b059dc2015-10-30 15:35:52 +080013/* system timer offset in ms */
14static unsigned long sandbox_timer_offset;
15
Neil Armstrong77c0dbc2019-04-11 17:01:23 +020016void timer_test_add_offset(unsigned long offset)
Thomas Chou7b059dc2015-10-30 15:35:52 +080017{
18 sandbox_timer_offset += offset;
19}
20
Simon Glass5fa92d62025-02-07 11:30:35 -070021ulong timer_test_get_offset(void)
22{
23 return sandbox_timer_offset;
24};
25
Simon Glass3c7c187b2016-02-24 09:14:51 -070026u64 notrace timer_early_get_count(void)
27{
28 return os_get_nsec() / 1000 + sandbox_timer_offset * 1000;
29}
30
31unsigned long notrace timer_early_get_rate(void)
Thomas Chou7b059dc2015-10-30 15:35:52 +080032{
Simon Glass3c7c187b2016-02-24 09:14:51 -070033 return SANDBOX_TIMER_RATE;
34}
35
Sean Anderson947fc2d2020-10-07 14:37:44 -040036static notrace u64 sandbox_timer_get_count(struct udevice *dev)
Simon Glass3c7c187b2016-02-24 09:14:51 -070037{
Sean Anderson947fc2d2020-10-07 14:37:44 -040038 return timer_early_get_count();
Thomas Chou7b059dc2015-10-30 15:35:52 +080039}
40
41static int sandbox_timer_probe(struct udevice *dev)
42{
Stephen Warren4faff282016-01-06 10:33:04 -070043 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
44
Simon Glass41d97062021-03-15 18:11:06 +130045 if (CONFIG_IS_ENABLED(CPU) &&
46 dev_read_bool(dev, "sandbox,timebase-frequency-fallback"))
Sean Anderson79d3bba2020-09-28 10:52:23 -040047 return timer_timebase_fallback(dev);
48 else if (!uc_priv->clock_rate)
Simon Glass3c7c187b2016-02-24 09:14:51 -070049 uc_priv->clock_rate = SANDBOX_TIMER_RATE;
Stephen Warren4faff282016-01-06 10:33:04 -070050
Thomas Chou7b059dc2015-10-30 15:35:52 +080051 return 0;
52}
53
54static const struct timer_ops sandbox_timer_ops = {
55 .get_count = sandbox_timer_get_count,
56};
57
58static const struct udevice_id sandbox_timer_ids[] = {
59 { .compatible = "sandbox,timer" },
60 { }
61};
62
63U_BOOT_DRIVER(sandbox_timer) = {
64 .name = "sandbox_timer",
65 .id = UCLASS_TIMER,
66 .of_match = sandbox_timer_ids,
67 .probe = sandbox_timer_probe,
68 .ops = &sandbox_timer_ops,
69 .flags = DM_FLAG_PRE_RELOC,
70};
Stephen Warren4faff282016-01-06 10:33:04 -070071
72/* This is here in case we don't have a device tree */
Heinrich Schuchardtdcbc02e2023-02-22 01:39:18 +010073#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass1d8364a2020-12-28 20:34:54 -070074U_BOOT_DRVINFO(sandbox_timer_non_fdt) = {
Stephen Warren4faff282016-01-06 10:33:04 -070075 .name = "sandbox_timer",
76};
Heinrich Schuchardtdcbc02e2023-02-22 01:39:18 +010077#endif