xref: /rk3399_rockchip-uboot/drivers/timer/timer-uclass.c (revision 5867875cb7098007b82c3f5b5dd7af9c7cf9e63d)
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>
11e842c8b7SPhilipp Tomsich #include <dm/root.h>
12a5acafb2SZakharov Vlad #include <clk.h>
13c8a7ba9eSThomas Chou #include <errno.h>
14c8a7ba9eSThomas Chou #include <timer.h>
15c8a7ba9eSThomas Chou 
16579eb5a0SBin Meng DECLARE_GLOBAL_DATA_PTR;
17579eb5a0SBin Meng 
18c8a7ba9eSThomas Chou /*
19435ae76eSBin Meng  * Implement a timer uclass to work with lib/time.c. The timer is usually
209ca07ebbSBin Meng  * a 32/64 bits free-running up counter. The get_rate() method is used to get
21c8a7ba9eSThomas Chou  * the input clock frequency of the timer. The get_count() method is used
229ca07ebbSBin Meng  * to get the current 64 bits count value. If the hardware is counting down,
23c8a7ba9eSThomas Chou  * the value should be inversed inside the method. There may be no real
24c8a7ba9eSThomas Chou  * tick, and no timer interrupt.
25c8a7ba9eSThomas Chou  */
26c8a7ba9eSThomas Chou 
timer_get_count(struct udevice * dev,u64 * count)274f051824SSimon Glass int notrace timer_get_count(struct udevice *dev, u64 *count)
28c8a7ba9eSThomas Chou {
29c8a7ba9eSThomas Chou 	const struct timer_ops *ops = device_get_ops(dev);
30c8a7ba9eSThomas Chou 
31c8a7ba9eSThomas Chou 	if (!ops->get_count)
32c8a7ba9eSThomas Chou 		return -ENOSYS;
33c8a7ba9eSThomas Chou 
34c8a7ba9eSThomas Chou 	return ops->get_count(dev, count);
35c8a7ba9eSThomas Chou }
36c8a7ba9eSThomas Chou 
timer_get_rate(struct udevice * dev)374f051824SSimon Glass unsigned long notrace timer_get_rate(struct udevice *dev)
38c8a7ba9eSThomas Chou {
394f051824SSimon Glass 	struct timer_dev_priv *uc_priv = dev->uclass_priv;
40c8a7ba9eSThomas Chou 
41c8a7ba9eSThomas Chou 	return uc_priv->clock_rate;
42c8a7ba9eSThomas Chou }
43c8a7ba9eSThomas Chou 
timer_pre_probe(struct udevice * dev)44579eb5a0SBin Meng static int timer_pre_probe(struct udevice *dev)
45579eb5a0SBin Meng {
46b1a16002SPhilipp Tomsich #if !CONFIG_IS_ENABLED(OF_PLATDATA)
47579eb5a0SBin Meng 	struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
48a5acafb2SZakharov Vlad 	struct clk timer_clk;
49a5acafb2SZakharov Vlad 	int err;
50a5acafb2SZakharov Vlad 	ulong ret;
51579eb5a0SBin Meng 
52a5acafb2SZakharov Vlad 	err = clk_get_by_index(dev, 0, &timer_clk);
53a5acafb2SZakharov Vlad 	if (!err) {
54a5acafb2SZakharov Vlad 		ret = clk_get_rate(&timer_clk);
55a5acafb2SZakharov Vlad 		if (IS_ERR_VALUE(ret))
56a5acafb2SZakharov Vlad 			return ret;
57a5acafb2SZakharov Vlad 		uc_priv->clock_rate = ret;
58e842c8b7SPhilipp Tomsich 	} else {
59e842c8b7SPhilipp Tomsich 		uc_priv->clock_rate =
60e842c8b7SPhilipp Tomsich 			dev_read_u32_default(dev, "clock-frequency", 0);
61e842c8b7SPhilipp Tomsich 	}
62b1a16002SPhilipp Tomsich #endif
63579eb5a0SBin Meng 
64579eb5a0SBin Meng 	return 0;
65579eb5a0SBin Meng }
66579eb5a0SBin Meng 
timer_post_probe(struct udevice * dev)670a7edce0SStephen Warren static int timer_post_probe(struct udevice *dev)
680a7edce0SStephen Warren {
690a7edce0SStephen Warren 	struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
700a7edce0SStephen Warren 
710a7edce0SStephen Warren 	if (!uc_priv->clock_rate)
720a7edce0SStephen Warren 		return -EINVAL;
730a7edce0SStephen Warren 
740a7edce0SStephen Warren 	return 0;
750a7edce0SStephen Warren }
760a7edce0SStephen Warren 
timer_conv_64(u32 count)779ca07ebbSBin Meng u64 timer_conv_64(u32 count)
789ca07ebbSBin Meng {
799ca07ebbSBin Meng 	/* increment tbh if tbl has rolled over */
809ca07ebbSBin Meng 	if (count < gd->timebase_l)
819ca07ebbSBin Meng 		gd->timebase_h++;
829ca07ebbSBin Meng 	gd->timebase_l = count;
839ca07ebbSBin Meng 	return ((u64)gd->timebase_h << 32) | gd->timebase_l;
849ca07ebbSBin Meng }
859ca07ebbSBin Meng 
dm_timer_init(void)86c8336975SMugunthan V N int notrace dm_timer_init(void)
87c8336975SMugunthan V N {
88c8336975SMugunthan V N 	struct udevice *dev = NULL;
89e842c8b7SPhilipp Tomsich 	__maybe_unused ofnode node;
90c8336975SMugunthan V N 	int ret;
91c8336975SMugunthan V N 
92c8336975SMugunthan V N 	if (gd->timer)
93c8336975SMugunthan V N 		return 0;
94c8336975SMugunthan V N 
95*5867875cSPhilipp Tomsich 	/*
96*5867875cSPhilipp Tomsich 	 * Directly access gd->dm_root to suppress error messages, if the
97*5867875cSPhilipp Tomsich 	 * virtual root driver does not yet exist.
98*5867875cSPhilipp Tomsich 	 */
99*5867875cSPhilipp Tomsich 	if (gd->dm_root == NULL)
100*5867875cSPhilipp Tomsich 		return -EAGAIN;
101*5867875cSPhilipp Tomsich 
102b1a16002SPhilipp Tomsich #if !CONFIG_IS_ENABLED(OF_PLATDATA)
103c8336975SMugunthan V N 	/* Check for a chosen timer to be used for tick */
104e842c8b7SPhilipp Tomsich 	node = ofnode_get_chosen_node("tick-timer");
105e842c8b7SPhilipp Tomsich 
106e842c8b7SPhilipp Tomsich 	if (ofnode_valid(node) &&
107e842c8b7SPhilipp Tomsich 	    uclass_get_device_by_ofnode(UCLASS_TIMER, node, &dev)) {
108c8336975SMugunthan V N 		/*
109c8336975SMugunthan V N 		 * If the timer is not marked to be bound before
110c8336975SMugunthan V N 		 * relocation, bind it anyway.
111c8336975SMugunthan V N 		 */
112e842c8b7SPhilipp Tomsich 		if (!lists_bind_fdt(dm_root(), node, &dev)) {
113c8336975SMugunthan V N 			ret = device_probe(dev);
114c8336975SMugunthan V N 			if (ret)
115c8336975SMugunthan V N 				return ret;
116c8336975SMugunthan V N 		}
117c8336975SMugunthan V N 	}
118e842c8b7SPhilipp Tomsich #endif
119e842c8b7SPhilipp Tomsich 
120e842c8b7SPhilipp Tomsich 	if (!dev) {
121e842c8b7SPhilipp Tomsich 		/* Fall back to the first available timer */
122e842c8b7SPhilipp Tomsich 		ret = uclass_first_device_err(UCLASS_TIMER, &dev);
123e842c8b7SPhilipp Tomsich 		if (ret)
124e842c8b7SPhilipp Tomsich 			return ret;
125c8336975SMugunthan V N 	}
126c8336975SMugunthan V N 
127c8336975SMugunthan V N 	if (dev) {
128c8336975SMugunthan V N 		gd->timer = dev;
129c8336975SMugunthan V N 		return 0;
130c8336975SMugunthan V N 	}
131c8336975SMugunthan V N 
132c8336975SMugunthan V N 	return -ENODEV;
133c8336975SMugunthan V N }
134c8336975SMugunthan V N 
135c8a7ba9eSThomas Chou UCLASS_DRIVER(timer) = {
136c8a7ba9eSThomas Chou 	.id		= UCLASS_TIMER,
137c8a7ba9eSThomas Chou 	.name		= "timer",
138579eb5a0SBin Meng 	.pre_probe	= timer_pre_probe,
139a5d80113SMugunthan V N 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
1400a7edce0SStephen Warren 	.post_probe	= timer_post_probe,
141c8a7ba9eSThomas Chou 	.per_device_auto_alloc_size = sizeof(struct timer_dev_priv),
142c8a7ba9eSThomas Chou };
143