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