1 // SPDX-License-Identifier: GPL-2.0+
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
14 DECLARE_GLOBAL_DATA_PTR;
17 * struct mpc83xx_timer_priv - Private data structure for MPC83xx timer driver
18 * @decrementer_count: Value to which the decrementer register should be re-set
19 * to when a timer interrupt occurs, thus determines the
20 * interrupt frequency (value for 1e6/HZ microseconds)
21 * @timestamp: Counter for the number of timer interrupts that have
22 * occurred (i.e. can be used to trigger events
23 * periodically in the timer interrupt)
25 struct mpc83xx_timer_priv {
26 uint decrementer_count;
31 * Bitmask for enabling the time base in the SPCR (System Priority
32 * Configuration Register)
34 static const u32 SPCR_TBEN_MASK = BIT(31 - 9);
37 * get_dec() - Get the value of the decrementer register
39 * Return: The value of the decrementer register
41 static inline unsigned long get_dec(void)
45 asm volatile ("mfdec %0" : "=r" (val) : );
51 * set_dec() - Set the value of the decrementer register
52 * @val: The value of the decrementer register to be set
54 static inline void set_dec(unsigned long val)
57 asm volatile ("mtdec %0"::"r" (val));
61 * mftbu() - Get value of TBU (upper time base) register
63 * Return: Value of the TBU register
65 static inline u32 mftbu(void)
69 asm volatile("mftbu %0" : "=r" (rval));
74 * mftb() - Get value of TBL (lower time base) register
76 * Return: Value of the TBL register
78 static inline u32 mftb(void)
82 asm volatile("mftb %0" : "=r" (rval));
87 * TODO(mario.six@gdsys.cc): This should really be done by timer_init, and the
88 * interrupt init should go into a interrupt driver.
90 int interrupt_init(void)
92 immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
94 struct udevice *board;
95 struct udevice *timer;
96 struct mpc83xx_timer_priv *timer_priv;
100 ret = uclass_first_device_err(UCLASS_TIMER, &timer);
102 debug("%s: Could not find timer device (error: %d)",
107 timer_priv = dev_get_priv(timer);
109 if (board_get(&board)) {
110 debug("%s: board device could not be fetched.\n", __func__);
114 ret = uclass_get_device_by_phandle(UCLASS_SIMPLE_BUS, board,
117 debug("%s: Could not retrieve CSB device (error: %d)",
122 ret = clk_get_by_index(csb, 0, &clock);
124 debug("%s: Could not retrieve clock (error: %d)",
129 timer_priv->decrementer_count = (clk_get_rate(&clock) / 4)
131 /* Enable e300 time base */
132 setbits_be32(&immr->sysconf.spcr, SPCR_TBEN_MASK);
134 set_dec(timer_priv->decrementer_count);
136 /* Switch on interrupts */
137 set_msr(get_msr() | MSR_EE);
143 * timer_interrupt() - Handler for the timer interrupt
144 * @regs: Array of register values
146 void timer_interrupt(struct pt_regs *regs)
148 struct udevice *timer = gd->timer;
149 struct mpc83xx_timer_priv *priv;
152 * During initialization, gd->timer might not be set yet, but the timer
153 * interrupt may already be enabled. In this case, wait for the
154 * initialization to complete
159 priv = dev_get_priv(timer);
161 /* Restore Decrementer Count */
162 set_dec(priv->decrementer_count);
166 #if defined(CONFIG_WATCHDOG) || defined(CONFIG_HW_WATCHDOG)
167 if ((timestamp % (CONFIG_SYS_WATCHDOG_FREQ)) == 0)
169 #endif /* CONFIG_WATCHDOG || CONFIG_HW_WATCHDOG */
171 #ifdef CONFIG_LED_STATUS
172 status_led_tick(priv->timestamp);
173 #endif /* CONFIG_LED_STATUS */
175 #ifdef CONFIG_SHOW_ACTIVITY
176 board_show_activity(priv->timestamp);
177 #endif /* CONFIG_SHOW_ACTIVITY */
180 void wait_ticks(ulong ticks)
182 ulong end = get_ticks() + ticks;
184 while (end > get_ticks())
188 static int mpc83xx_timer_get_count(struct udevice *dev, u64 *count)
193 * To make sure that no tbl overflow occurred between reading tbl and
194 * tbu, read tbu again, and compare it with the previously read tbu
195 * value: If they're different, a tbl overflow has occurred.
200 } while (tbu != mftbu());
202 *count = (tbu * 0x10000ULL) + tbl;
207 static int mpc83xx_timer_probe(struct udevice *dev)
209 struct timer_dev_priv *uc_priv = dev->uclass_priv;
213 ret = interrupt_init();
215 debug("%s: interrupt_init failed (err = %d)\n",
220 ret = clk_get_by_index(dev, 0, &clock);
222 debug("%s: Could not retrieve clock (err = %d)\n",
227 uc_priv->clock_rate = (clk_get_rate(&clock) + 3L) / 4L;
232 static const struct timer_ops mpc83xx_timer_ops = {
233 .get_count = mpc83xx_timer_get_count,
236 static const struct udevice_id mpc83xx_timer_ids[] = {
237 { .compatible = "fsl,mpc83xx-timer" },
241 U_BOOT_DRIVER(mpc83xx_timer) = {
242 .name = "mpc83xx_timer",
244 .of_match = mpc83xx_timer_ids,
245 .probe = mpc83xx_timer_probe,
246 .ops = &mpc83xx_timer_ops,
247 .priv_auto_alloc_size = sizeof(struct mpc83xx_timer_priv),