blob: a8da936349498cb817b22d291dd8007e1feb474c [file] [log] [blame]
Thomas Chou9961a0b2015-10-30 15:35:52 +08001/*
2 * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <timer.h>
11#include <os.h>
12
13/* system timer offset in ms */
14static unsigned long sandbox_timer_offset;
15
16void sandbox_timer_add_offset(unsigned long offset)
17{
18 sandbox_timer_offset += offset;
19}
20
Bin Meng9ca07eb2015-11-24 13:31:17 -070021static int sandbox_timer_get_count(struct udevice *dev, u64 *count)
Thomas Chou9961a0b2015-10-30 15:35:52 +080022{
23 *count = os_get_nsec() / 1000 + sandbox_timer_offset * 1000;
24
25 return 0;
26}
27
28static int sandbox_timer_probe(struct udevice *dev)
29{
Stephen Warrenbb883f82016-01-06 10:33:04 -070030 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
31
32 if (!uc_priv->clock_rate)
33 uc_priv->clock_rate = 1000000;
34
Thomas Chou9961a0b2015-10-30 15:35:52 +080035 return 0;
36}
37
38static const struct timer_ops sandbox_timer_ops = {
39 .get_count = sandbox_timer_get_count,
40};
41
42static const struct udevice_id sandbox_timer_ids[] = {
43 { .compatible = "sandbox,timer" },
44 { }
45};
46
47U_BOOT_DRIVER(sandbox_timer) = {
48 .name = "sandbox_timer",
49 .id = UCLASS_TIMER,
50 .of_match = sandbox_timer_ids,
51 .probe = sandbox_timer_probe,
52 .ops = &sandbox_timer_ops,
53 .flags = DM_FLAG_PRE_RELOC,
54};
Stephen Warrenbb883f82016-01-06 10:33:04 -070055
56/* This is here in case we don't have a device tree */
57U_BOOT_DEVICE(sandbox_timer_non_fdt) = {
58 .name = "sandbox_timer",
59};