2 * (C) Copyright 2011, Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>
3 * (C) Copyright 2011, Julius Baxter <julius@opencores.org>
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * SPDX-License-Identifier: GPL-2.0+
11 #include <asm/system.h>
12 #include <asm/openrisc_exc.h>
14 static ulong timestamp;
16 /* how many counter cycles in a jiffy */
17 #define TIMER_COUNTER_CYCLES (CONFIG_SYS_CLK_FREQ/CONFIG_SYS_OPENRISC_TMR_HZ)
18 /* how many ms elapses between each timer interrupt */
19 #define TIMER_TIMESTAMP_INC (1000/CONFIG_SYS_OPENRISC_TMR_HZ)
20 /* how many cycles per ms */
21 #define TIMER_CYCLES_MS (CONFIG_SYS_CLK_FREQ/1000)
22 /* how many cycles per us */
23 #define TIMER_CYCLES_US (CONFIG_SYS_CLK_FREQ/1000000uL)
27 timestamp += TIMER_TIMESTAMP_INC;
28 mtspr(SPR_TTMR, SPR_TTMR_IE | SPR_TTMR_RT |
29 (TIMER_COUNTER_CYCLES & SPR_TTMR_TP));
34 /* Install timer exception handler */
35 exception_install_handler(EXC_TIMER, timer_isr);
37 /* Set up the timer for the first expiration. */
40 mtspr(SPR_TTMR, SPR_TTMR_IE | SPR_TTMR_RT |
41 (TIMER_COUNTER_CYCLES & SPR_TTMR_TP));
43 /* Enable tick timer exception in supervisor register */
44 mtspr(SPR_SR, mfspr(SPR_SR) | SPR_SR_TEE);
49 void reset_timer(void)
53 mtspr(SPR_TTMR, SPR_TTMR_IE | SPR_TTMR_RT |
54 (TIMER_COUNTER_CYCLES & SPR_TTMR_TP));
58 * The timer value in ms is calculated by taking the
59 * value accumulated by full timer revolutions plus the value
60 * accumulated in this period
62 ulong get_timer(ulong base)
64 return timestamp + mfspr(SPR_TTCR)/TIMER_CYCLES_MS - base;
67 void set_timer(ulong t)
73 unsigned long long get_ticks(void)
83 void __udelay(ulong usec)
89 last_tick = mfspr(SPR_TTCR);
90 while ((elapsed / TIMER_CYCLES_US) < usec) {
91 tick = mfspr(SPR_TTCR);
92 if (tick >= last_tick)
93 elapsed += (tick - last_tick);
95 elapsed += TIMER_COUNTER_CYCLES - (last_tick - tick);