2 * (C) Copyright 2003 Josef Baumgartner <josef.baumgartner@telex.de>
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/timer.h>
13 #include <asm/immap.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 static volatile ulong timestamp = 0;
20 #ifndef CONFIG_SYS_WATCHDOG_FREQ
21 #define CONFIG_SYS_WATCHDOG_FREQ (CONFIG_SYS_HZ / 2)
24 #if defined(CONFIG_MCFTMR)
25 #ifndef CONFIG_SYS_UDELAY_BASE
26 # error "uDelay base not defined!"
29 #if !defined(CONFIG_SYS_TMR_BASE) || !defined(CONFIG_SYS_INTR_BASE) || !defined(CONFIG_SYS_TMRINTR_NO) || !defined(CONFIG_SYS_TMRINTR_MASK)
30 # error "TMR_BASE, INTR_BASE, TMRINTR_NO or TMRINTR_MASk not defined!"
32 extern void dtimer_intr_setup(void);
34 void __udelay(unsigned long usec)
36 volatile dtmr_t *timerp = (dtmr_t *) (CONFIG_SYS_UDELAY_BASE);
46 /* Set up TIMER 3 as timebase clock */
47 timerp->tmr = DTIM_DTMR_RST_RST;
49 /* set period to 1 us */
51 CONFIG_SYS_TIMER_PRESCALER | DTIM_DTMR_CLK_DIV1 | DTIM_DTMR_FRR |
54 start = now = timerp->tcn;
55 while (now < start + tmp)
60 void dtimer_interrupt(void *not_used)
62 volatile dtmr_t *timerp = (dtmr_t *) (CONFIG_SYS_TMR_BASE);
64 /* check for timer interrupt asserted */
65 if ((CONFIG_SYS_TMRPND_REG & CONFIG_SYS_TMRINTR_MASK) == CONFIG_SYS_TMRINTR_PEND) {
66 timerp->ter = (DTIM_DTER_CAP | DTIM_DTER_REF);
69 #if defined(CONFIG_WATCHDOG) || defined (CONFIG_HW_WATCHDOG)
70 if ((timestamp % (CONFIG_SYS_WATCHDOG_FREQ)) == 0) {
73 #endif /* CONFIG_WATCHDOG || CONFIG_HW_WATCHDOG */
80 volatile dtmr_t *timerp = (dtmr_t *) (CONFIG_SYS_TMR_BASE);
87 /* Set up TIMER 4 as clock */
88 timerp->tmr = DTIM_DTMR_RST_RST;
90 /* initialize and enable timer interrupt */
91 irq_install_handler(CONFIG_SYS_TMRINTR_NO, dtimer_interrupt, 0);
94 timerp->trr = 1000; /* Interrupt every ms */
98 /* set a period of 1us, set timer mode to restart and enable timer and interrupt */
99 timerp->tmr = CONFIG_SYS_TIMER_PRESCALER | DTIM_DTMR_CLK_DIV1 |
100 DTIM_DTMR_FRR | DTIM_DTMR_ORRI | DTIM_DTMR_RST_EN;
105 ulong get_timer(ulong base)
107 return (timestamp - base);
110 #endif /* CONFIG_MCFTMR */
112 #if defined(CONFIG_MCFPIT)
113 #if !defined(CONFIG_SYS_PIT_BASE)
114 # error "CONFIG_SYS_PIT_BASE not defined!"
117 static unsigned short lastinc;
119 void __udelay(unsigned long usec)
121 volatile pit_t *timerp = (pit_t *) (CONFIG_SYS_UDELAY_BASE);
131 /* Set up TIMER 3 as timebase clock */
132 timerp->pcsr = PIT_PCSR_OVW;
134 /* set period to 1 us */
135 timerp->pcsr |= PIT_PCSR_PRE(CONFIG_SYS_PIT_PRESCALE) | PIT_PCSR_EN;
138 while (timerp->pcntr > 0) ;
142 void timer_init(void)
144 volatile pit_t *timerp = (pit_t *) (CONFIG_SYS_PIT_BASE);
147 /* Set up TIMER 4 as poll clock */
148 timerp->pcsr = PIT_PCSR_OVW;
149 timerp->pmr = lastinc = 0;
150 timerp->pcsr |= PIT_PCSR_PRE(CONFIG_SYS_PIT_PRESCALE) | PIT_PCSR_EN;
155 ulong get_timer(ulong base)
157 unsigned short now, diff;
158 volatile pit_t *timerp = (pit_t *) (CONFIG_SYS_PIT_BASE);
161 diff = -(now - lastinc);
165 return timestamp - base;
168 void wait_ticks(unsigned long ticks)
170 u32 start = get_timer(0);
171 while (get_timer(start) < ticks) ;
173 #endif /* CONFIG_MCFPIT */
176 * This function is derived from PowerPC code (read timebase as long long).
177 * On M68K it just returns the timer value.
179 unsigned long long get_ticks(void)
184 unsigned long usec2ticks(unsigned long usec)
186 return get_timer(usec);
190 * This function is derived from PowerPC code (timebase clock frequency).
191 * On M68K it returns the number of timer ticks per second.
193 ulong get_tbclk(void)
196 tbclk = CONFIG_SYS_HZ;