1 // SPDX-License-Identifier: GPL-2.0+
4 * Lineo, Inc. <www.lineo.com>
5 * Bernhard Kuhn <bkuhn@lineo.com>
8 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9 * Marius Groeger <mgroeger@sysgo.de>
12 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
13 * Alex Zuepke <azu@sysgo.de>
19 #include <asm/global_data.h>
20 #include <linux/delay.h>
23 #include <asm/arch/hardware.h>
24 #include <asm/arch/at91_tc.h>
25 #include <asm/arch/clk.h>
27 DECLARE_GLOBAL_DATA_PTR;
29 /* the number of clocks per CONFIG_SYS_HZ */
30 #define TIMER_LOAD_VAL (CFG_SYS_HZ_CLOCK/CONFIG_SYS_HZ)
34 at91_tc_t *tc = (at91_tc_t *) ATMEL_BASE_TC;
36 at91_periph_clk_enable(ATMEL_ID_TC0);
39 writel(AT91_TC_BMR_TC0XC0S_NONE | AT91_TC_BMR_TC1XC1S_NONE |
40 AT91_TC_BMR_TC2XC2S_NONE , &tc->bmr);
42 writel(AT91_TC_CCR_CLKDIS, &tc->tc[0].ccr);
43 /* set to MCLK/2 and restart the timer
44 when the value in TC_RC is reached */
45 writel(AT91_TC_CMR_TCCLKS_CLOCK1 | AT91_TC_CMR_CPCTRG, &tc->tc[0].cmr);
47 writel(0xFFFFFFFF, &tc->tc[0].idr); /* disable interrupts */
48 writel(TIMER_LOAD_VAL, &tc->tc[0].rc);
50 writel(AT91_TC_CCR_SWTRG | AT91_TC_CCR_CLKEN, &tc->tc[0].ccr);
58 * timer without interrupts
60 ulong get_timer_raw(void)
62 at91_tc_t *tc = (at91_tc_t *) ATMEL_BASE_TC;
65 now = readl(&tc->tc[0].cv) & 0x0000ffff;
67 if (now >= gd->arch.lastinc) {
69 gd->arch.tbl += now - gd->arch.lastinc;
71 /* we have an overflow ... */
72 gd->arch.tbl += now + TIMER_LOAD_VAL - gd->arch.lastinc;
74 gd->arch.lastinc = now;
79 static ulong get_timer_masked(void)
81 return get_timer_raw()/TIMER_LOAD_VAL;
84 ulong get_timer(ulong base)
86 return get_timer_masked() - base;
89 void __udelay(unsigned long usec)
95 tmo = CFG_SYS_HZ_CLOCK / 1000;
99 endtime = get_timer_raw() + tmo;
102 u32 now = get_timer_raw();
103 diff = endtime - now;
108 * This function is derived from PowerPC code (read timebase as long long).
109 * On ARM it just returns the timer value.
111 unsigned long long get_ticks(void)
117 * This function is derived from PowerPC code (timebase clock frequency).
118 * On ARM it returns the number of timer ticks per second.
120 ulong get_tbclk(void)
122 return CONFIG_SYS_HZ;