xref: /rk3399_rockchip-uboot/drivers/timer/timer-uclass.c (revision 9ca07ebbac3c94974db75e25004203289d9013bc)
1c8a7ba9eSThomas Chou /*
2c8a7ba9eSThomas Chou  * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
3c8a7ba9eSThomas Chou  *
4c8a7ba9eSThomas Chou  * SPDX-License-Identifier:	GPL-2.0+
5c8a7ba9eSThomas Chou  */
6c8a7ba9eSThomas Chou 
7c8a7ba9eSThomas Chou #include <common.h>
8c8a7ba9eSThomas Chou #include <dm.h>
9c8a7ba9eSThomas Chou #include <errno.h>
10c8a7ba9eSThomas Chou #include <timer.h>
11c8a7ba9eSThomas Chou 
12579eb5a0SBin Meng DECLARE_GLOBAL_DATA_PTR;
13579eb5a0SBin Meng 
14c8a7ba9eSThomas Chou /*
15435ae76eSBin Meng  * Implement a timer uclass to work with lib/time.c. The timer is usually
16*9ca07ebbSBin Meng  * a 32/64 bits free-running up counter. The get_rate() method is used to get
17c8a7ba9eSThomas Chou  * the input clock frequency of the timer. The get_count() method is used
18*9ca07ebbSBin Meng  * to get the current 64 bits count value. If the hardware is counting down,
19c8a7ba9eSThomas Chou  * the value should be inversed inside the method. There may be no real
20c8a7ba9eSThomas Chou  * tick, and no timer interrupt.
21c8a7ba9eSThomas Chou  */
22c8a7ba9eSThomas Chou 
23*9ca07ebbSBin Meng int timer_get_count(struct udevice *dev, u64 *count)
24c8a7ba9eSThomas Chou {
25c8a7ba9eSThomas Chou 	const struct timer_ops *ops = device_get_ops(dev);
26c8a7ba9eSThomas Chou 
27c8a7ba9eSThomas Chou 	if (!ops->get_count)
28c8a7ba9eSThomas Chou 		return -ENOSYS;
29c8a7ba9eSThomas Chou 
30c8a7ba9eSThomas Chou 	return ops->get_count(dev, count);
31c8a7ba9eSThomas Chou }
32c8a7ba9eSThomas Chou 
33c8a7ba9eSThomas Chou unsigned long timer_get_rate(struct udevice *dev)
34c8a7ba9eSThomas Chou {
35c8a7ba9eSThomas Chou 	struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
36c8a7ba9eSThomas Chou 
37c8a7ba9eSThomas Chou 	return uc_priv->clock_rate;
38c8a7ba9eSThomas Chou }
39c8a7ba9eSThomas Chou 
40579eb5a0SBin Meng static int timer_pre_probe(struct udevice *dev)
41579eb5a0SBin Meng {
42579eb5a0SBin Meng 	struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
43579eb5a0SBin Meng 
44579eb5a0SBin Meng 	uc_priv->clock_rate = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
45579eb5a0SBin Meng 					     "clock-frequency", 0);
46579eb5a0SBin Meng 
47579eb5a0SBin Meng 	return 0;
48579eb5a0SBin Meng }
49579eb5a0SBin Meng 
50*9ca07ebbSBin Meng u64 timer_conv_64(u32 count)
51*9ca07ebbSBin Meng {
52*9ca07ebbSBin Meng 	/* increment tbh if tbl has rolled over */
53*9ca07ebbSBin Meng 	if (count < gd->timebase_l)
54*9ca07ebbSBin Meng 		gd->timebase_h++;
55*9ca07ebbSBin Meng 	gd->timebase_l = count;
56*9ca07ebbSBin Meng 	return ((u64)gd->timebase_h << 32) | gd->timebase_l;
57*9ca07ebbSBin Meng }
58*9ca07ebbSBin Meng 
59c8a7ba9eSThomas Chou UCLASS_DRIVER(timer) = {
60c8a7ba9eSThomas Chou 	.id		= UCLASS_TIMER,
61c8a7ba9eSThomas Chou 	.name		= "timer",
62579eb5a0SBin Meng 	.pre_probe	= timer_pre_probe,
63c8a7ba9eSThomas Chou 	.per_device_auto_alloc_size = sizeof(struct timer_dev_priv),
64c8a7ba9eSThomas Chou };
65