blob: c846bfb9f122abd79595d77bbfb8246ba10bc5aa [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Thomas Chou9961a0b2015-10-30 15:35:52 +08002/*
3 * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
Thomas Chou9961a0b2015-10-30 15:35:52 +08004 */
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <timer.h>
10#include <os.h>
11
Simon Glass01476ea2016-02-24 09:14:51 -070012#define SANDBOX_TIMER_RATE 1000000
13
Thomas Chou9961a0b2015-10-30 15:35:52 +080014/* system timer offset in ms */
15static unsigned long sandbox_timer_offset;
16
Neil Armstrongd0a9b822019-04-11 17:01:23 +020017void timer_test_add_offset(unsigned long offset)
Thomas Chou9961a0b2015-10-30 15:35:52 +080018{
19 sandbox_timer_offset += offset;
20}
21
Simon Glass01476ea2016-02-24 09:14:51 -070022u64 notrace timer_early_get_count(void)
Thomas Chou9961a0b2015-10-30 15:35:52 +080023{
Simon Glass01476ea2016-02-24 09:14:51 -070024 return os_get_nsec() / 1000 + sandbox_timer_offset * 1000;
25}
26
27unsigned long notrace timer_early_get_rate(void)
28{
29 return SANDBOX_TIMER_RATE;
30}
31
Sean Anderson8af7bb92020-10-07 14:37:44 -040032static notrace u64 sandbox_timer_get_count(struct udevice *dev)
Simon Glass01476ea2016-02-24 09:14:51 -070033{
Sean Anderson8af7bb92020-10-07 14:37:44 -040034 return timer_early_get_count();
Thomas Chou9961a0b2015-10-30 15:35:52 +080035}
36
37static int sandbox_timer_probe(struct udevice *dev)
38{
Stephen Warrenbb883f82016-01-06 10:33:04 -070039 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
40
Simon Glass539db6a2021-03-15 18:11:06 +130041 if (CONFIG_IS_ENABLED(CPU) &&
42 dev_read_bool(dev, "sandbox,timebase-frequency-fallback"))
Sean Anderson7616e362020-09-28 10:52:23 -040043 return timer_timebase_fallback(dev);
44 else if (!uc_priv->clock_rate)
Simon Glass01476ea2016-02-24 09:14:51 -070045 uc_priv->clock_rate = SANDBOX_TIMER_RATE;
Stephen Warrenbb883f82016-01-06 10:33:04 -070046
Thomas Chou9961a0b2015-10-30 15:35:52 +080047 return 0;
48}
49
50static const struct timer_ops sandbox_timer_ops = {
51 .get_count = sandbox_timer_get_count,
52};
53
54static const struct udevice_id sandbox_timer_ids[] = {
55 { .compatible = "sandbox,timer" },
56 { }
57};
58
59U_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 Warrenbb883f82016-01-06 10:33:04 -070067
68/* This is here in case we don't have a device tree */
Simon Glass20e442a2020-12-28 20:34:54 -070069U_BOOT_DRVINFO(sandbox_timer_non_fdt) = {
Stephen Warrenbb883f82016-01-06 10:33:04 -070070 .name = "sandbox_timer",
71};