blob: f8a092b8cb1a3aabe1c69dc47de63e77f37913cc [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Thomas Chouc8a7ba92015-10-09 13:46:34 +08002/*
3 * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
Thomas Chouc8a7ba92015-10-09 13:46:34 +08004 */
5
6#include <common.h>
Sean Andersona5d4f862020-10-04 21:39:52 -04007#include <clk.h>
Sean Anderson35761212020-09-28 10:52:22 -04008#include <cpu.h>
Thomas Chouc8a7ba92015-10-09 13:46:34 +08009#include <dm.h>
Mugunthan V Nc8336972016-01-16 21:33:58 +053010#include <dm/lists.h>
Sean Andersona5d4f862020-10-04 21:39:52 -040011#include <dm/device_compat.h>
Mugunthan V Nc8336972016-01-16 21:33:58 +053012#include <dm/device-internal.h>
Philipp Tomsichb61e8b02017-09-11 22:04:10 +020013#include <dm/root.h>
Thomas Chouc8a7ba92015-10-09 13:46:34 +080014#include <errno.h>
Sean Andersona5d4f862020-10-04 21:39:52 -040015#include <init.h>
Thomas Chouc8a7ba92015-10-09 13:46:34 +080016#include <timer.h>
Simon Glass61b29b82020-02-03 07:36:15 -070017#include <linux/err.h>
Thomas Chouc8a7ba92015-10-09 13:46:34 +080018
Bin Meng579eb5a2015-11-13 00:11:15 -080019DECLARE_GLOBAL_DATA_PTR;
20
Thomas Chouc8a7ba92015-10-09 13:46:34 +080021/*
Bin Meng435ae762015-11-13 00:11:14 -080022 * Implement a timer uclass to work with lib/time.c. The timer is usually
Bin Meng9ca07eb2015-11-24 13:31:17 -070023 * a 32/64 bits free-running up counter. The get_rate() method is used to get
Thomas Chouc8a7ba92015-10-09 13:46:34 +080024 * the input clock frequency of the timer. The get_count() method is used
Bin Meng9ca07eb2015-11-24 13:31:17 -070025 * to get the current 64 bits count value. If the hardware is counting down,
Thomas Chouc8a7ba92015-10-09 13:46:34 +080026 * the value should be inversed inside the method. There may be no real
27 * tick, and no timer interrupt.
28 */
29
Simon Glass4f051822016-02-24 09:14:48 -070030int notrace timer_get_count(struct udevice *dev, u64 *count)
Thomas Chouc8a7ba92015-10-09 13:46:34 +080031{
32 const struct timer_ops *ops = device_get_ops(dev);
33
34 if (!ops->get_count)
35 return -ENOSYS;
36
37 return ops->get_count(dev, count);
38}
39
Simon Glass4f051822016-02-24 09:14:48 -070040unsigned long notrace timer_get_rate(struct udevice *dev)
Thomas Chouc8a7ba92015-10-09 13:46:34 +080041{
Simon Glass4f051822016-02-24 09:14:48 -070042 struct timer_dev_priv *uc_priv = dev->uclass_priv;
Thomas Chouc8a7ba92015-10-09 13:46:34 +080043
44 return uc_priv->clock_rate;
45}
46
Bin Meng579eb5a2015-11-13 00:11:15 -080047static int timer_pre_probe(struct udevice *dev)
48{
Philipp Tomsichb1a16002017-07-28 17:19:58 +020049#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Bin Meng579eb5a2015-11-13 00:11:15 -080050 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Zakharov Vlada5acafb2016-12-09 17:18:32 +030051 struct clk timer_clk;
52 int err;
53 ulong ret;
Bin Meng579eb5a2015-11-13 00:11:15 -080054
Bin Meng7efb4a62019-07-05 09:23:15 -070055 /* It is possible that a timer device has a null ofnode */
56 if (!dev_of_valid(dev))
57 return 0;
58
Zakharov Vlada5acafb2016-12-09 17:18:32 +030059 err = clk_get_by_index(dev, 0, &timer_clk);
60 if (!err) {
61 ret = clk_get_rate(&timer_clk);
62 if (IS_ERR_VALUE(ret))
63 return ret;
64 uc_priv->clock_rate = ret;
Philipp Tomsichb61e8b02017-09-11 22:04:10 +020065 } else {
66 uc_priv->clock_rate =
67 dev_read_u32_default(dev, "clock-frequency", 0);
68 }
Philipp Tomsichb1a16002017-07-28 17:19:58 +020069#endif
Bin Meng579eb5a2015-11-13 00:11:15 -080070
71 return 0;
72}
73
Stephen Warren0a7edce2016-01-06 10:33:03 -070074static int timer_post_probe(struct udevice *dev)
75{
76 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
77
78 if (!uc_priv->clock_rate)
79 return -EINVAL;
80
81 return 0;
82}
83
Sean Anderson35761212020-09-28 10:52:22 -040084/*
85 * TODO: should be CONFIG_IS_ENABLED(CPU), but the SPL config has _SUPPORT on
86 * the end...
87 */
88#if defined(CONFIG_CPU) || defined(CONFIG_SPL_CPU_SUPPORT)
89int timer_timebase_fallback(struct udevice *dev)
90{
91 struct udevice *cpu;
92 struct cpu_platdata *cpu_plat;
93 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
94
95 /* Did we get our clock rate from the device tree? */
96 if (uc_priv->clock_rate)
97 return 0;
98
99 /* Fall back to timebase-frequency */
100 dev_dbg(dev, "missing clocks or clock-frequency property; falling back on timebase-frequency\n");
101 cpu = cpu_get_current_dev();
102 if (!cpu)
103 return -ENODEV;
104
105 cpu_plat = dev_get_parent_platdata(cpu);
106 if (!cpu_plat)
107 return -ENODEV;
108
109 uc_priv->clock_rate = cpu_plat->timebase_freq;
110 return 0;
111}
112#endif
113
Bin Meng9ca07eb2015-11-24 13:31:17 -0700114u64 timer_conv_64(u32 count)
115{
116 /* increment tbh if tbl has rolled over */
117 if (count < gd->timebase_l)
118 gd->timebase_h++;
119 gd->timebase_l = count;
120 return ((u64)gd->timebase_h << 32) | gd->timebase_l;
121}
122
Mugunthan V Nc8336972016-01-16 21:33:58 +0530123int notrace dm_timer_init(void)
124{
Mugunthan V Nc8336972016-01-16 21:33:58 +0530125 struct udevice *dev = NULL;
Philipp Tomsichb61e8b02017-09-11 22:04:10 +0200126 __maybe_unused ofnode node;
Mugunthan V Nc8336972016-01-16 21:33:58 +0530127 int ret;
128
129 if (gd->timer)
130 return 0;
131
Philipp Tomsichaf823152017-09-11 22:04:11 +0200132 /*
133 * Directly access gd->dm_root to suppress error messages, if the
134 * virtual root driver does not yet exist.
135 */
136 if (gd->dm_root == NULL)
137 return -EAGAIN;
138
Philipp Tomsichb1a16002017-07-28 17:19:58 +0200139#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Mugunthan V Nc8336972016-01-16 21:33:58 +0530140 /* Check for a chosen timer to be used for tick */
Philipp Tomsichb61e8b02017-09-11 22:04:10 +0200141 node = ofnode_get_chosen_node("tick-timer");
142
143 if (ofnode_valid(node) &&
144 uclass_get_device_by_ofnode(UCLASS_TIMER, node, &dev)) {
145 /*
146 * If the timer is not marked to be bound before
147 * relocation, bind it anyway.
148 */
Bin Meng8d773c42018-10-10 22:06:58 -0700149 if (!lists_bind_fdt(dm_root(), node, &dev, false)) {
Philipp Tomsichb61e8b02017-09-11 22:04:10 +0200150 ret = device_probe(dev);
151 if (ret)
152 return ret;
153 }
154 }
Philipp Tomsichb1a16002017-07-28 17:19:58 +0200155#endif
Philipp Tomsichb61e8b02017-09-11 22:04:10 +0200156
157 if (!dev) {
158 /* Fall back to the first available timer */
Simon Glass3f603cb2016-02-11 13:23:26 -0700159 ret = uclass_first_device_err(UCLASS_TIMER, &dev);
Mugunthan V Nc8336972016-01-16 21:33:58 +0530160 if (ret)
161 return ret;
Mugunthan V Nc8336972016-01-16 21:33:58 +0530162 }
163
164 if (dev) {
165 gd->timer = dev;
166 return 0;
167 }
168
169 return -ENODEV;
170}
171
Thomas Chouc8a7ba92015-10-09 13:46:34 +0800172UCLASS_DRIVER(timer) = {
173 .id = UCLASS_TIMER,
174 .name = "timer",
Bin Meng579eb5a2015-11-13 00:11:15 -0800175 .pre_probe = timer_pre_probe,
Mugunthan V Na5d80112015-12-24 16:08:06 +0530176 .flags = DM_UC_FLAG_SEQ_ALIAS,
Stephen Warren0a7edce2016-01-06 10:33:03 -0700177 .post_probe = timer_post_probe,
Thomas Chouc8a7ba92015-10-09 13:46:34 +0800178 .per_device_auto_alloc_size = sizeof(struct timer_dev_priv),
179};