xref: /rk3399_rockchip-uboot/drivers/timer/timer-uclass.c (revision a5acafb25598ef409f48a7884316a9a4e96c63a4)
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>
9c8336975SMugunthan V N #include <dm/lists.h>
10c8336975SMugunthan V N #include <dm/device-internal.h>
11*a5acafb2SZakharov Vlad #include <clk.h>
12c8a7ba9eSThomas Chou #include <errno.h>
13c8a7ba9eSThomas Chou #include <timer.h>
14c8a7ba9eSThomas Chou 
15579eb5a0SBin Meng DECLARE_GLOBAL_DATA_PTR;
16579eb5a0SBin Meng 
17c8a7ba9eSThomas Chou /*
18435ae76eSBin Meng  * Implement a timer uclass to work with lib/time.c. The timer is usually
199ca07ebbSBin Meng  * a 32/64 bits free-running up counter. The get_rate() method is used to get
20c8a7ba9eSThomas Chou  * the input clock frequency of the timer. The get_count() method is used
219ca07ebbSBin Meng  * to get the current 64 bits count value. If the hardware is counting down,
22c8a7ba9eSThomas Chou  * the value should be inversed inside the method. There may be no real
23c8a7ba9eSThomas Chou  * tick, and no timer interrupt.
24c8a7ba9eSThomas Chou  */
25c8a7ba9eSThomas Chou 
264f051824SSimon Glass int notrace timer_get_count(struct udevice *dev, u64 *count)
27c8a7ba9eSThomas Chou {
28c8a7ba9eSThomas Chou 	const struct timer_ops *ops = device_get_ops(dev);
29c8a7ba9eSThomas Chou 
30c8a7ba9eSThomas Chou 	if (!ops->get_count)
31c8a7ba9eSThomas Chou 		return -ENOSYS;
32c8a7ba9eSThomas Chou 
33c8a7ba9eSThomas Chou 	return ops->get_count(dev, count);
34c8a7ba9eSThomas Chou }
35c8a7ba9eSThomas Chou 
364f051824SSimon Glass unsigned long notrace timer_get_rate(struct udevice *dev)
37c8a7ba9eSThomas Chou {
384f051824SSimon Glass 	struct timer_dev_priv *uc_priv = dev->uclass_priv;
39c8a7ba9eSThomas Chou 
40c8a7ba9eSThomas Chou 	return uc_priv->clock_rate;
41c8a7ba9eSThomas Chou }
42c8a7ba9eSThomas Chou 
43579eb5a0SBin Meng static int timer_pre_probe(struct udevice *dev)
44579eb5a0SBin Meng {
45579eb5a0SBin Meng 	struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
46*a5acafb2SZakharov Vlad 	struct clk timer_clk;
47*a5acafb2SZakharov Vlad 	int err;
48*a5acafb2SZakharov Vlad 	ulong ret;
49579eb5a0SBin Meng 
50*a5acafb2SZakharov Vlad 	err = clk_get_by_index(dev, 0, &timer_clk);
51*a5acafb2SZakharov Vlad 	if (!err) {
52*a5acafb2SZakharov Vlad 		ret = clk_get_rate(&timer_clk);
53*a5acafb2SZakharov Vlad 		if (IS_ERR_VALUE(ret))
54*a5acafb2SZakharov Vlad 			return ret;
55*a5acafb2SZakharov Vlad 		uc_priv->clock_rate = ret;
56*a5acafb2SZakharov Vlad 	} else
57*a5acafb2SZakharov Vlad 		uc_priv->clock_rate = fdtdec_get_int(gd->fdt_blob,
58*a5acafb2SZakharov Vlad 				dev->of_offset,	"clock-frequency", 0);
59579eb5a0SBin Meng 
60579eb5a0SBin Meng 	return 0;
61579eb5a0SBin Meng }
62579eb5a0SBin Meng 
630a7edce0SStephen Warren static int timer_post_probe(struct udevice *dev)
640a7edce0SStephen Warren {
650a7edce0SStephen Warren 	struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
660a7edce0SStephen Warren 
670a7edce0SStephen Warren 	if (!uc_priv->clock_rate)
680a7edce0SStephen Warren 		return -EINVAL;
690a7edce0SStephen Warren 
700a7edce0SStephen Warren 	return 0;
710a7edce0SStephen Warren }
720a7edce0SStephen Warren 
739ca07ebbSBin Meng u64 timer_conv_64(u32 count)
749ca07ebbSBin Meng {
759ca07ebbSBin Meng 	/* increment tbh if tbl has rolled over */
769ca07ebbSBin Meng 	if (count < gd->timebase_l)
779ca07ebbSBin Meng 		gd->timebase_h++;
789ca07ebbSBin Meng 	gd->timebase_l = count;
799ca07ebbSBin Meng 	return ((u64)gd->timebase_h << 32) | gd->timebase_l;
809ca07ebbSBin Meng }
819ca07ebbSBin Meng 
82c8336975SMugunthan V N int notrace dm_timer_init(void)
83c8336975SMugunthan V N {
84c8336975SMugunthan V N 	const void *blob = gd->fdt_blob;
85c8336975SMugunthan V N 	struct udevice *dev = NULL;
86c8336975SMugunthan V N 	int node;
87c8336975SMugunthan V N 	int ret;
88c8336975SMugunthan V N 
89c8336975SMugunthan V N 	if (gd->timer)
90c8336975SMugunthan V N 		return 0;
91c8336975SMugunthan V N 
92c8336975SMugunthan V N 	/* Check for a chosen timer to be used for tick */
93c8336975SMugunthan V N 	node = fdtdec_get_chosen_node(blob, "tick-timer");
94c8336975SMugunthan V N 	if (node < 0) {
95c8336975SMugunthan V N 		/* No chosen timer, trying first available timer */
963f603cbbSSimon Glass 		ret = uclass_first_device_err(UCLASS_TIMER, &dev);
97c8336975SMugunthan V N 		if (ret)
98c8336975SMugunthan V N 			return ret;
99c8336975SMugunthan V N 	} else {
100c8336975SMugunthan V N 		if (uclass_get_device_by_of_offset(UCLASS_TIMER, node, &dev)) {
101c8336975SMugunthan V N 			/*
102c8336975SMugunthan V N 			 * If the timer is not marked to be bound before
103c8336975SMugunthan V N 			 * relocation, bind it anyway.
104c8336975SMugunthan V N 			 */
105c8336975SMugunthan V N 			if (node > 0 &&
106c8336975SMugunthan V N 			    !lists_bind_fdt(gd->dm_root, blob, node, &dev)) {
107c8336975SMugunthan V N 				ret = device_probe(dev);
108c8336975SMugunthan V N 				if (ret)
109c8336975SMugunthan V N 					return ret;
110c8336975SMugunthan V N 			}
111c8336975SMugunthan V N 		}
112c8336975SMugunthan V N 	}
113c8336975SMugunthan V N 
114c8336975SMugunthan V N 	if (dev) {
115c8336975SMugunthan V N 		gd->timer = dev;
116c8336975SMugunthan V N 		return 0;
117c8336975SMugunthan V N 	}
118c8336975SMugunthan V N 
119c8336975SMugunthan V N 	return -ENODEV;
120c8336975SMugunthan V N }
121c8336975SMugunthan V N 
122c8a7ba9eSThomas Chou UCLASS_DRIVER(timer) = {
123c8a7ba9eSThomas Chou 	.id		= UCLASS_TIMER,
124c8a7ba9eSThomas Chou 	.name		= "timer",
125579eb5a0SBin Meng 	.pre_probe	= timer_pre_probe,
126a5d80113SMugunthan V N 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
1270a7edce0SStephen Warren 	.post_probe	= timer_post_probe,
128c8a7ba9eSThomas Chou 	.per_device_auto_alloc_size = sizeof(struct timer_dev_priv),
129c8a7ba9eSThomas Chou };
130