1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
4  */
5 
6 #define LOG_CATEGORY UCLASS_TIMER
7 
8 #include <common.h>
9 #include <clk.h>
10 #include <cpu.h>
11 #include <dm.h>
12 #include <asm/global_data.h>
13 #include <dm/lists.h>
14 #include <dm/device_compat.h>
15 #include <dm/device-internal.h>
16 #include <dm/root.h>
17 #include <errno.h>
18 #include <init.h>
19 #include <timer.h>
20 #include <linux/err.h>
21 #include <relocate.h>
22 
23 DECLARE_GLOBAL_DATA_PTR;
24 
25 /*
26  * Implement a timer uclass to work with lib/time.c. The timer is usually
27  * a 32/64 bits free-running up counter. The get_rate() method is used to get
28  * the input clock frequency of the timer. The get_count() method is used
29  * to get the current 64 bits count value. If the hardware is counting down,
30  * the value should be inversed inside the method. There may be no real
31  * tick, and no timer interrupt.
32  */
33 
timer_get_count(struct udevice * dev,u64 * count)34 int notrace timer_get_count(struct udevice *dev, u64 *count)
35 {
36 	struct timer_ops *ops = timer_get_ops(dev);
37 
38 	if (!ops->get_count)
39 		return -ENOSYS;
40 
41 	*count = ops->get_count(dev);
42 	return 0;
43 }
44 
timer_get_rate(struct udevice * dev)45 unsigned long notrace timer_get_rate(struct udevice *dev)
46 {
47 	struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
48 
49 	return uc_priv->clock_rate;
50 }
51 
timer_pre_probe(struct udevice * dev)52 static int timer_pre_probe(struct udevice *dev)
53 {
54 	if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC) &&
55 	    (gd->flags & GD_FLG_RELOC)) {
56 		struct timer_ops *ops = timer_get_ops(dev);
57 		static int reloc_done;
58 
59 		if (!reloc_done) {
60 			if (ops->get_count)
61 				MANUAL_RELOC(ops->get_count);
62 
63 			reloc_done++;
64 		}
65 	}
66 
67 	if (CONFIG_IS_ENABLED(OF_REAL)) {
68 		struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
69 		struct clk timer_clk;
70 		int err;
71 		ulong ret;
72 
73 		/*
74 		 * It is possible that a timer device has a null ofnode
75 		 */
76 		if (!dev_has_ofnode(dev))
77 			return 0;
78 
79 		err = clk_get_by_index(dev, 0, &timer_clk);
80 		if (!err) {
81 			ret = clk_get_rate(&timer_clk);
82 			if (IS_ERR_VALUE(ret))
83 				return ret;
84 			uc_priv->clock_rate = ret;
85 		} else {
86 			uc_priv->clock_rate =
87 				dev_read_u32_default(dev, "clock-frequency", 0);
88 		}
89 	}
90 
91 	return 0;
92 }
93 
timer_post_probe(struct udevice * dev)94 static int timer_post_probe(struct udevice *dev)
95 {
96 	struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
97 
98 	if (!uc_priv->clock_rate)
99 		return -EINVAL;
100 
101 	return 0;
102 }
103 
104 #if CONFIG_IS_ENABLED(CPU)
timer_timebase_fallback(struct udevice * dev)105 int timer_timebase_fallback(struct udevice *dev)
106 {
107 	struct udevice *cpu;
108 	struct cpu_plat *cpu_plat;
109 	struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
110 
111 	/* Did we get our clock rate from the device tree? */
112 	if (uc_priv->clock_rate)
113 		return 0;
114 
115 	/* Fall back to timebase-frequency */
116 	dev_dbg(dev, "missing clocks or clock-frequency property; falling back on timebase-frequency\n");
117 	cpu = cpu_get_current_dev();
118 	if (!cpu)
119 		return -ENODEV;
120 
121 	cpu_plat = dev_get_parent_plat(cpu);
122 	if (!cpu_plat)
123 		return -ENODEV;
124 
125 	uc_priv->clock_rate = cpu_plat->timebase_freq;
126 	return 0;
127 }
128 #endif
129 
timer_conv_64(u32 count)130 u64 timer_conv_64(u32 count)
131 {
132 	/* increment tbh if tbl has rolled over */
133 	if (count < gd->timebase_l)
134 		gd->timebase_h++;
135 	gd->timebase_l = count;
136 	return ((u64)gd->timebase_h << 32) | gd->timebase_l;
137 }
138 
dm_timer_init(void)139 int dm_timer_init(void)
140 {
141 	struct udevice *dev = NULL;
142 	__maybe_unused ofnode node;
143 	int ret;
144 
145 	if (gd->timer)
146 		return 0;
147 
148 	/*
149 	 * Directly access gd->dm_root to suppress error messages, if the
150 	 * virtual root driver does not yet exist.
151 	 */
152 	if (gd->dm_root == NULL)
153 		return -EAGAIN;
154 
155 	if (CONFIG_IS_ENABLED(OF_REAL)) {
156 		/* Check for a chosen timer to be used for tick */
157 		node = ofnode_get_chosen_node("tick-timer");
158 
159 		if (ofnode_valid(node) &&
160 		    uclass_get_device_by_ofnode(UCLASS_TIMER, node, &dev)) {
161 			/*
162 			 * If the timer is not marked to be bound before
163 			 * relocation, bind it anyway.
164 			 */
165 			if (!lists_bind_fdt(dm_root(), node, &dev, NULL, false)) {
166 				ret = device_probe(dev);
167 				if (ret)
168 					return ret;
169 			}
170 		}
171 	}
172 
173 	if (!dev) {
174 		/* Fall back to the first available timer */
175 		ret = uclass_first_device_err(UCLASS_TIMER, &dev);
176 		if (ret)
177 			return ret;
178 	}
179 
180 	if (dev) {
181 		gd->timer = dev;
182 		return 0;
183 	}
184 
185 	return -ENODEV;
186 }
187 
188 UCLASS_DRIVER(timer) = {
189 	.id		= UCLASS_TIMER,
190 	.name		= "timer",
191 	.pre_probe	= timer_pre_probe,
192 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
193 	.post_probe	= timer_post_probe,
194 	.per_device_auto	= sizeof(struct timer_dev_priv),
195 };
196