common: Move wait_ticks functions out of common.h
[oweals/u-boot.git] / drivers / timer / mpc83xx_timer.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2018
4  * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5  */
6
7 #include <common.h>
8 #include <board.h>
9 #include <clk.h>
10 #include <dm.h>
11 #include <status_led.h>
12 #include <time.h>
13 #include <timer.h>
14 #include <watchdog.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 /**
19  * struct mpc83xx_timer_priv - Private data structure for MPC83xx timer driver
20  * @decrementer_count: Value to which the decrementer register should be re-set
21  *                     to when a timer interrupt occurs, thus determines the
22  *                     interrupt frequency (value for 1e6/HZ microseconds)
23  * @timestamp:         Counter for the number of timer interrupts that have
24  *                     occurred (i.e. can be used to trigger events
25  *                     periodically in the timer interrupt)
26  */
27 struct mpc83xx_timer_priv {
28         uint decrementer_count;
29         ulong timestamp;
30 };
31
32 /*
33  * Bitmask for enabling the time base in the SPCR (System Priority
34  * Configuration Register)
35  */
36 static const u32 SPCR_TBEN_MASK = BIT(31 - 9);
37
38 /**
39  * get_dec() - Get the value of the decrementer register
40  *
41  * Return: The value of the decrementer register
42  */
43 static inline unsigned long get_dec(void)
44 {
45         unsigned long val;
46
47         asm volatile ("mfdec %0" : "=r" (val) : );
48
49         return val;
50 }
51
52 /**
53  * set_dec() - Set the value of the decrementer register
54  * @val: The value of the decrementer register to be set
55  */
56 static inline void set_dec(unsigned long val)
57 {
58         if (val)
59                 asm volatile ("mtdec %0"::"r" (val));
60 }
61
62 /**
63  * mftbu() - Get value of TBU (upper time base) register
64  *
65  * Return: Value of the TBU register
66  */
67 static inline u32 mftbu(void)
68 {
69         u32 rval;
70
71         asm volatile("mftbu %0" : "=r" (rval));
72         return rval;
73 }
74
75 /**
76  * mftb() - Get value of TBL (lower time base) register
77  *
78  * Return: Value of the TBL register
79  */
80 static inline u32 mftb(void)
81 {
82         u32 rval;
83
84         asm volatile("mftb %0" : "=r" (rval));
85         return rval;
86 }
87
88 /*
89  * TODO(mario.six@gdsys.cc): This should really be done by timer_init, and the
90  * interrupt init should go into a interrupt driver.
91  */
92 int interrupt_init(void)
93 {
94         immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
95         struct udevice *csb;
96         struct udevice *board;
97         struct udevice *timer;
98         struct mpc83xx_timer_priv *timer_priv;
99         struct clk clock;
100         int ret;
101
102         ret = uclass_first_device_err(UCLASS_TIMER, &timer);
103         if (ret) {
104                 debug("%s: Could not find timer device (error: %d)",
105                       __func__, ret);
106                 return ret;
107         }
108
109         timer_priv = dev_get_priv(timer);
110
111         if (board_get(&board)) {
112                 debug("%s: board device could not be fetched.\n", __func__);
113                 return -ENOENT;
114         }
115
116         ret = uclass_get_device_by_phandle(UCLASS_SIMPLE_BUS, board,
117                                            "csb", &csb);
118         if (ret) {
119                 debug("%s: Could not retrieve CSB device (error: %d)",
120                       __func__, ret);
121                 return ret;
122         }
123
124         ret = clk_get_by_index(csb, 0, &clock);
125         if (ret) {
126                 debug("%s: Could not retrieve clock (error: %d)",
127                       __func__, ret);
128                 return ret;
129         }
130
131         timer_priv->decrementer_count = (clk_get_rate(&clock) / 4)
132                                         / CONFIG_SYS_HZ;
133         /* Enable e300 time base */
134         setbits_be32(&immr->sysconf.spcr, SPCR_TBEN_MASK);
135
136         set_dec(timer_priv->decrementer_count);
137
138         /* Switch on interrupts */
139         set_msr(get_msr() | MSR_EE);
140
141         return 0;
142 }
143
144 /**
145  * timer_interrupt() - Handler for the timer interrupt
146  * @regs: Array of register values
147  */
148 void timer_interrupt(struct pt_regs *regs)
149 {
150         struct udevice *timer = gd->timer;
151         struct mpc83xx_timer_priv *priv;
152
153         /*
154          * During initialization, gd->timer might not be set yet, but the timer
155          * interrupt may already be enabled. In this case, wait for the
156          * initialization to complete
157          */
158         if (!timer)
159                 return;
160
161         priv = dev_get_priv(timer);
162
163         /* Restore Decrementer Count */
164         set_dec(priv->decrementer_count);
165
166         priv->timestamp++;
167
168 #if defined(CONFIG_WATCHDOG) || defined(CONFIG_HW_WATCHDOG)
169         if ((timestamp % (CONFIG_SYS_WATCHDOG_FREQ)) == 0)
170                 WATCHDOG_RESET();
171 #endif    /* CONFIG_WATCHDOG || CONFIG_HW_WATCHDOG */
172
173 #ifdef CONFIG_LED_STATUS
174         status_led_tick(priv->timestamp);
175 #endif /* CONFIG_LED_STATUS */
176 }
177
178 void wait_ticks(ulong ticks)
179 {
180         ulong end = get_ticks() + ticks;
181
182         while (end > get_ticks())
183                 WATCHDOG_RESET();
184 }
185
186 static int mpc83xx_timer_get_count(struct udevice *dev, u64 *count)
187 {
188         u32 tbu, tbl;
189
190         /*
191          * To make sure that no tbl overflow occurred between reading tbl and
192          * tbu, read tbu again, and compare it with the previously read tbu
193          * value: If they're different, a tbl overflow has occurred.
194          */
195         do {
196                 tbu = mftbu();
197                 tbl = mftb();
198         } while (tbu != mftbu());
199
200         *count = (tbu * 0x10000ULL) + tbl;
201
202         return 0;
203 }
204
205 static int mpc83xx_timer_probe(struct udevice *dev)
206 {
207         struct timer_dev_priv *uc_priv = dev->uclass_priv;
208         struct clk clock;
209         int ret;
210
211         ret = interrupt_init();
212         if (ret) {
213                 debug("%s: interrupt_init failed (err = %d)\n",
214                       dev->name, ret);
215                 return ret;
216         }
217
218         ret = clk_get_by_index(dev, 0, &clock);
219         if (ret) {
220                 debug("%s: Could not retrieve clock (err = %d)\n",
221                       dev->name, ret);
222                 return ret;
223         }
224
225         uc_priv->clock_rate = (clk_get_rate(&clock) + 3L) / 4L;
226
227         return 0;
228 }
229
230 static const struct timer_ops mpc83xx_timer_ops = {
231         .get_count = mpc83xx_timer_get_count,
232 };
233
234 static const struct udevice_id mpc83xx_timer_ids[] = {
235         { .compatible = "fsl,mpc83xx-timer" },
236         { /* sentinel */ }
237 };
238
239 U_BOOT_DRIVER(mpc83xx_timer) = {
240         .name   = "mpc83xx_timer",
241         .id     = UCLASS_TIMER,
242         .of_match = mpc83xx_timer_ids,
243         .probe = mpc83xx_timer_probe,
244         .ops    = &mpc83xx_timer_ops,
245         .priv_auto_alloc_size = sizeof(struct mpc83xx_timer_priv),
246 };