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