3 * Lineo, Inc. <www.lineo.com>
4 * Bernhard Kuhn <bkuhn@lineo.com>
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Marius Groeger <mgroeger@sysgo.de>
11 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
12 * Alex Zuepke <azu@sysgo.de>
14 * SPDX-License-Identifier: GPL-2.0+
20 #include <asm/arch/hardware.h>
21 #include <asm/arch/at91_tc.h>
22 #include <asm/arch/clk.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 /* the number of clocks per CONFIG_SYS_HZ */
27 #define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK/CONFIG_SYS_HZ)
31 at91_tc_t *tc = (at91_tc_t *) ATMEL_BASE_TC;
33 at91_periph_clk_enable(ATMEL_ID_TC0);
36 writel(AT91_TC_BMR_TC0XC0S_NONE | AT91_TC_BMR_TC1XC1S_NONE |
37 AT91_TC_BMR_TC2XC2S_NONE , &tc->bmr);
39 writel(AT91_TC_CCR_CLKDIS, &tc->tc[0].ccr);
40 /* set to MCLK/2 and restart the timer
41 when the value in TC_RC is reached */
42 writel(AT91_TC_CMR_TCCLKS_CLOCK1 | AT91_TC_CMR_CPCTRG, &tc->tc[0].cmr);
44 writel(0xFFFFFFFF, &tc->tc[0].idr); /* disable interrupts */
45 writel(TIMER_LOAD_VAL, &tc->tc[0].rc);
47 writel(AT91_TC_CCR_SWTRG | AT91_TC_CCR_CLKEN, &tc->tc[0].ccr);
55 * timer without interrupts
57 ulong get_timer(ulong base)
59 return get_timer_masked() - base;
62 void __udelay(unsigned long usec)
67 ulong get_timer_raw(void)
69 at91_tc_t *tc = (at91_tc_t *) ATMEL_BASE_TC;
72 now = readl(&tc->tc[0].cv) & 0x0000ffff;
74 if (now >= gd->arch.lastinc) {
76 gd->arch.tbl += now - gd->arch.lastinc;
78 /* we have an overflow ... */
79 gd->arch.tbl += now + TIMER_LOAD_VAL - gd->arch.lastinc;
81 gd->arch.lastinc = now;
86 ulong get_timer_masked(void)
88 return get_timer_raw()/TIMER_LOAD_VAL;
91 void udelay_masked(unsigned long usec)
97 tmo = CONFIG_SYS_HZ_CLOCK / 1000;
101 endtime = get_timer_raw() + tmo;
104 u32 now = get_timer_raw();
105 diff = endtime - now;
110 * This function is derived from PowerPC code (read timebase as long long).
111 * On ARM it just returns the timer value.
113 unsigned long long get_ticks(void)
119 * This function is derived from PowerPC code (timebase clock frequency).
120 * On ARM it returns the number of timer ticks per second.
122 ulong get_tbclk(void)
124 return CONFIG_SYS_HZ;