1 // SPDX-License-Identifier: GPL-2.0+
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
12 #include <status_led.h>
17 DECLARE_GLOBAL_DATA_PTR;
20 * struct mpc83xx_timer_priv - Private data structure for MPC83xx timer driver
21 * @decrementer_count: Value to which the decrementer register should be re-set
22 * to when a timer interrupt occurs, thus determines the
23 * interrupt frequency (value for 1e6/HZ microseconds)
24 * @timestamp: Counter for the number of timer interrupts that have
25 * occurred (i.e. can be used to trigger events
26 * periodically in the timer interrupt)
28 struct mpc83xx_timer_priv {
29 uint decrementer_count;
34 * Bitmask for enabling the time base in the SPCR (System Priority
35 * Configuration Register)
37 static const u32 SPCR_TBEN_MASK = BIT(31 - 9);
40 * get_dec() - Get the value of the decrementer register
42 * Return: The value of the decrementer register
44 static inline unsigned long get_dec(void)
48 asm volatile ("mfdec %0" : "=r" (val) : );
54 * set_dec() - Set the value of the decrementer register
55 * @val: The value of the decrementer register to be set
57 static inline void set_dec(unsigned long val)
60 asm volatile ("mtdec %0"::"r" (val));
64 * mftbu() - Get value of TBU (upper time base) register
66 * Return: Value of the TBU register
68 static inline u32 mftbu(void)
72 asm volatile("mftbu %0" : "=r" (rval));
77 * mftb() - Get value of TBL (lower time base) register
79 * Return: Value of the TBL register
81 static inline u32 mftb(void)
85 asm volatile("mftb %0" : "=r" (rval));
90 * TODO(mario.six@gdsys.cc): This should really be done by timer_init, and the
91 * interrupt init should go into a interrupt driver.
93 int interrupt_init(void)
95 immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
97 struct udevice *board;
98 struct udevice *timer;
99 struct mpc83xx_timer_priv *timer_priv;
103 ret = uclass_first_device_err(UCLASS_TIMER, &timer);
105 debug("%s: Could not find timer device (error: %d)",
110 timer_priv = dev_get_priv(timer);
112 if (board_get(&board)) {
113 debug("%s: board device could not be fetched.\n", __func__);
117 ret = uclass_get_device_by_phandle(UCLASS_SIMPLE_BUS, board,
120 debug("%s: Could not retrieve CSB device (error: %d)",
125 ret = clk_get_by_index(csb, 0, &clock);
127 debug("%s: Could not retrieve clock (error: %d)",
132 timer_priv->decrementer_count = (clk_get_rate(&clock) / 4)
134 /* Enable e300 time base */
135 setbits_be32(&immr->sysconf.spcr, SPCR_TBEN_MASK);
137 set_dec(timer_priv->decrementer_count);
139 /* Switch on interrupts */
140 set_msr(get_msr() | MSR_EE);
146 * timer_interrupt() - Handler for the timer interrupt
147 * @regs: Array of register values
149 void timer_interrupt(struct pt_regs *regs)
151 struct udevice *timer = gd->timer;
152 struct mpc83xx_timer_priv *priv;
155 * During initialization, gd->timer might not be set yet, but the timer
156 * interrupt may already be enabled. In this case, wait for the
157 * initialization to complete
162 priv = dev_get_priv(timer);
164 /* Restore Decrementer Count */
165 set_dec(priv->decrementer_count);
169 #if defined(CONFIG_WATCHDOG) || defined(CONFIG_HW_WATCHDOG)
170 if ((timestamp % (CONFIG_SYS_WATCHDOG_FREQ)) == 0)
172 #endif /* CONFIG_WATCHDOG || CONFIG_HW_WATCHDOG */
174 #ifdef CONFIG_LED_STATUS
175 status_led_tick(priv->timestamp);
176 #endif /* CONFIG_LED_STATUS */
179 void wait_ticks(ulong ticks)
181 ulong end = get_ticks() + ticks;
183 while (end > get_ticks())
187 static int mpc83xx_timer_get_count(struct udevice *dev, u64 *count)
192 * To make sure that no tbl overflow occurred between reading tbl and
193 * tbu, read tbu again, and compare it with the previously read tbu
194 * value: If they're different, a tbl overflow has occurred.
199 } while (tbu != mftbu());
201 *count = (tbu * 0x10000ULL) + tbl;
206 static int mpc83xx_timer_probe(struct udevice *dev)
208 struct timer_dev_priv *uc_priv = dev->uclass_priv;
212 ret = interrupt_init();
214 debug("%s: interrupt_init failed (err = %d)\n",
219 ret = clk_get_by_index(dev, 0, &clock);
221 debug("%s: Could not retrieve clock (err = %d)\n",
226 uc_priv->clock_rate = (clk_get_rate(&clock) + 3L) / 4L;
231 static const struct timer_ops mpc83xx_timer_ops = {
232 .get_count = mpc83xx_timer_get_count,
235 static const struct udevice_id mpc83xx_timer_ids[] = {
236 { .compatible = "fsl,mpc83xx-timer" },
240 U_BOOT_DRIVER(mpc83xx_timer) = {
241 .name = "mpc83xx_timer",
243 .of_match = mpc83xx_timer_ids,
244 .probe = mpc83xx_timer_probe,
245 .ops = &mpc83xx_timer_ops,
246 .priv_auto_alloc_size = sizeof(struct mpc83xx_timer_priv),