1 // SPDX-License-Identifier: GPL-2.0+
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
12 #include <status_led.h>
17 #include <asm/global_data.h>
18 #include <asm/ptrace.h>
19 #include <linux/bitops.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 #ifndef CFG_SYS_WATCHDOG_FREQ
24 #define CFG_SYS_WATCHDOG_FREQ (CONFIG_SYS_HZ / 2)
28 * struct mpc83xx_timer_priv - Private data structure for MPC83xx timer driver
29 * @decrementer_count: Value to which the decrementer register should be re-set
30 * to when a timer interrupt occurs, thus determines the
31 * interrupt frequency (value for 1e6/HZ microseconds)
32 * @timestamp: Counter for the number of timer interrupts that have
33 * occurred (i.e. can be used to trigger events
34 * periodically in the timer interrupt)
36 struct mpc83xx_timer_priv {
37 uint decrementer_count;
42 * Bitmask for enabling the time base in the SPCR (System Priority
43 * Configuration Register)
45 static const u32 SPCR_TBEN_MASK = BIT(31 - 9);
48 * get_dec() - Get the value of the decrementer register
50 * Return: The value of the decrementer register
52 static inline unsigned long get_dec(void)
56 asm volatile ("mfdec %0" : "=r" (val) : );
62 * set_dec() - Set the value of the decrementer register
63 * @val: The value of the decrementer register to be set
65 static inline void set_dec(unsigned long val)
68 asm volatile ("mtdec %0"::"r" (val));
72 * mftbu() - Get value of TBU (upper time base) register
74 * Return: Value of the TBU register
76 static inline u32 mftbu(void)
80 asm volatile("mftbu %0" : "=r" (rval));
85 * mftb() - Get value of TBL (lower time base) register
87 * Return: Value of the TBL register
89 static inline u32 mftb(void)
93 asm volatile("mftb %0" : "=r" (rval));
98 * TODO(mario.six@gdsys.cc): This should really be done by timer_init, and the
99 * interrupt init should go into a interrupt driver.
101 int interrupt_init(void)
103 immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
105 struct udevice *sysinfo;
106 struct udevice *timer;
107 struct mpc83xx_timer_priv *timer_priv;
111 ret = uclass_first_device_err(UCLASS_TIMER, &timer);
113 debug("%s: Could not find timer device (error: %d)",
118 timer_priv = dev_get_priv(timer);
120 if (sysinfo_get(&sysinfo)) {
121 debug("%s: sysinfo device could not be fetched.\n", __func__);
125 ret = uclass_get_device_by_phandle(UCLASS_SIMPLE_BUS, sysinfo,
128 debug("%s: Could not retrieve CSB device (error: %d)",
133 ret = clk_get_by_index(csb, 0, &clock);
135 debug("%s: Could not retrieve clock (error: %d)",
140 timer_priv->decrementer_count = (clk_get_rate(&clock) / 4)
142 /* Enable e300 time base */
143 setbits_be32(&immr->sysconf.spcr, SPCR_TBEN_MASK);
145 set_dec(timer_priv->decrementer_count);
147 /* Switch on interrupts */
148 set_msr(get_msr() | MSR_EE);
154 * timer_interrupt() - Handler for the timer interrupt
155 * @regs: Array of register values
157 void timer_interrupt(struct pt_regs *regs)
159 struct udevice *timer = gd->timer;
160 struct mpc83xx_timer_priv *priv;
163 * During initialization, gd->timer might not be set yet, but the timer
164 * interrupt may already be enabled. In this case, wait for the
165 * initialization to complete
170 priv = dev_get_priv(timer);
172 /* Restore Decrementer Count */
173 set_dec(priv->decrementer_count);
177 #if defined(CONFIG_WATCHDOG) || defined(CONFIG_HW_WATCHDOG)
178 if (CFG_SYS_WATCHDOG_FREQ && (priv->timestamp % (CFG_SYS_WATCHDOG_FREQ)) == 0)
180 #endif /* CONFIG_WATCHDOG || CONFIG_HW_WATCHDOG */
182 #ifdef CONFIG_LED_STATUS
183 status_led_tick(priv->timestamp);
184 #endif /* CONFIG_LED_STATUS */
187 void wait_ticks(ulong ticks)
189 ulong end = get_ticks() + ticks;
191 while (end > get_ticks())
195 static u64 mpc83xx_timer_get_count(struct udevice *dev)
200 * To make sure that no tbl overflow occurred between reading tbl and
201 * tbu, read tbu again, and compare it with the previously read tbu
202 * value: If they're different, a tbl overflow has occurred.
207 } while (tbu != mftbu());
209 return (tbu * 0x10000ULL) + tbl;
212 static int mpc83xx_timer_probe(struct udevice *dev)
214 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
218 ret = interrupt_init();
220 debug("%s: interrupt_init failed (err = %d)\n",
225 ret = clk_get_by_index(dev, 0, &clock);
227 debug("%s: Could not retrieve clock (err = %d)\n",
232 uc_priv->clock_rate = (clk_get_rate(&clock) + 3L) / 4L;
237 static const struct timer_ops mpc83xx_timer_ops = {
238 .get_count = mpc83xx_timer_get_count,
241 static const struct udevice_id mpc83xx_timer_ids[] = {
242 { .compatible = "fsl,mpc83xx-timer" },
246 U_BOOT_DRIVER(mpc83xx_timer) = {
247 .name = "mpc83xx_timer",
249 .of_match = mpc83xx_timer_ids,
250 .probe = mpc83xx_timer_probe,
251 .ops = &mpc83xx_timer_ops,
252 .priv_auto = sizeof(struct mpc83xx_timer_priv),