blob: 6a503c2f15359714cecd993efe64d5dd48c10f5f [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
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <timer.h>
10#include <os.h>
11
Simon Glass3c7c187b2016-02-24 09:14:51 -070012#define SANDBOX_TIMER_RATE 1000000
13
Thomas Chou7b059dc2015-10-30 15:35:52 +080014/* system timer offset in ms */
15static unsigned long sandbox_timer_offset;
16
Neil Armstrong77c0dbc2019-04-11 17:01:23 +020017void timer_test_add_offset(unsigned long offset)
Thomas Chou7b059dc2015-10-30 15:35:52 +080018{
19 sandbox_timer_offset += offset;
20}
21
Simon Glass3c7c187b2016-02-24 09:14:51 -070022u64 notrace timer_early_get_count(void)
23{
24 return os_get_nsec() / 1000 + sandbox_timer_offset * 1000;
25}
26
27unsigned long notrace timer_early_get_rate(void)
Thomas Chou7b059dc2015-10-30 15:35:52 +080028{
Simon Glass3c7c187b2016-02-24 09:14:51 -070029 return SANDBOX_TIMER_RATE;
30}
31
32static notrace int sandbox_timer_get_count(struct udevice *dev, u64 *count)
33{
34 *count = timer_early_get_count();
Thomas Chou7b059dc2015-10-30 15:35:52 +080035
36 return 0;
37}
38
39static int sandbox_timer_probe(struct udevice *dev)
40{
Stephen Warren4faff282016-01-06 10:33:04 -070041 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
42
Sean Anderson79d3bba2020-09-28 10:52:23 -040043 if (dev_read_bool(dev, "sandbox,timebase-frequency-fallback"))
44 return timer_timebase_fallback(dev);
45 else if (!uc_priv->clock_rate)
Simon Glass3c7c187b2016-02-24 09:14:51 -070046 uc_priv->clock_rate = SANDBOX_TIMER_RATE;
Stephen Warren4faff282016-01-06 10:33:04 -070047
Thomas Chou7b059dc2015-10-30 15:35:52 +080048 return 0;
49}
50
51static const struct timer_ops sandbox_timer_ops = {
52 .get_count = sandbox_timer_get_count,
53};
54
55static const struct udevice_id sandbox_timer_ids[] = {
56 { .compatible = "sandbox,timer" },
57 { }
58};
59
60U_BOOT_DRIVER(sandbox_timer) = {
61 .name = "sandbox_timer",
62 .id = UCLASS_TIMER,
63 .of_match = sandbox_timer_ids,
64 .probe = sandbox_timer_probe,
65 .ops = &sandbox_timer_ops,
66 .flags = DM_FLAG_PRE_RELOC,
67};
Stephen Warren4faff282016-01-06 10:33:04 -070068
69/* This is here in case we don't have a device tree */
70U_BOOT_DEVICE(sandbox_timer_non_fdt) = {
71 .name = "sandbox_timer",
72};