]>
git.ipfire.org Git - people/ms/u-boot.git/blob - arch/arm/cpu/armv7/at91/timer.c
2 * (C) Copyright 2007-2008
3 * Stelian Pop <stelian@popies.net>
4 * Lead Tech Design <www.leadtechdesign.com>
7 * Bo Shen <voice.shen@atmel.com>
9 * SPDX-License-Identifier: GPL-2.0+
14 #include <asm/arch/hardware.h>
15 #include <asm/arch/at91_pit.h>
16 #include <asm/arch/at91_pmc.h>
17 #include <asm/arch/clk.h>
20 #if !defined(CONFIG_AT91FAMILY)
21 # error You need to define CONFIG_AT91FAMILY in your board config!
24 DECLARE_GLOBAL_DATA_PTR
;
27 * We're using the SAMA5D3x PITC in 32 bit mode, by
28 * setting the 20 bit counter period to its maximum (0xfffff).
29 * (See the relevant data sheets to understand that this really works)
31 * We do also mimic the typical powerpc way of incrementing
32 * two 32 bit registers called tbl and tbu.
34 * Those registers increment at 1/16 the main clock rate.
37 #define TIMER_LOAD_VAL 0xfffff
39 static inline unsigned long long tick_to_time(unsigned long long tick
)
41 tick
*= CONFIG_SYS_HZ
;
42 do_div(tick
, gd
->arch
.timer_rate_hz
);
47 static inline unsigned long long usec_to_tick(unsigned long long usec
)
49 usec
*= gd
->arch
.timer_rate_hz
;
50 do_div(usec
, 1000000);
56 * Use the PITC in full 32 bit incrementing mode
60 at91_pit_t
*pit
= (at91_pit_t
*)ATMEL_BASE_PIT
;
62 /* Enable PITC Clock */
63 at91_periph_clk_enable(ATMEL_ID_PIT
);
66 writel(TIMER_LOAD_VAL
| AT91_PIT_MR_EN
, &pit
->mr
);
68 gd
->arch
.timer_rate_hz
= get_pit_clk_rate() / 16;
77 * Get the current 64 bit timer tick count
79 unsigned long long get_ticks(void)
81 at91_pit_t
*pit
= (at91_pit_t
*)ATMEL_BASE_PIT
;
83 ulong now
= readl(&pit
->piir
);
85 /* increment tbu if tbl has rolled over */
86 if (now
< gd
->arch
.tbl
)
89 return (((unsigned long long)gd
->arch
.tbu
) << 32) | gd
->arch
.tbl
;
92 void __udelay(unsigned long usec
)
94 unsigned long long start
;
97 start
= get_ticks(); /* get current timestamp */
98 tmo
= usec_to_tick(usec
); /* convert usecs to ticks */
99 while ((get_ticks() - start
) < tmo
)
100 ; /* loop till time has passed */
104 * get_timer(base) can be used to check for timeouts or
105 * to measure elasped time relative to an event:
107 * ulong start_time = get_timer(0) sets start_time to the current
109 * get_timer(start_time) returns the time elapsed since then.
111 * The time is used in CONFIG_SYS_HZ units!
113 ulong
get_timer(ulong base
)
115 return tick_to_time(get_ticks()) - base
;
119 * Return the number of timer ticks per second.
121 ulong
get_tbclk(void)
123 return gd
->arch
.timer_rate_hz
;