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 = gd->arch.mck_rate_hz / 16;
76 * Get the current 64 bit timer tick count
78 unsigned long long get_ticks(void)
80 at91_pit_t *pit = (at91_pit_t *)ATMEL_BASE_PIT;
82 ulong now = readl(&pit->piir);
84 /* increment tbu if tbl has rolled over */
85 if (now < gd->arch.tbl)
88 return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
91 void __udelay(unsigned long usec)
93 unsigned long long start;
96 start = get_ticks(); /* get current timestamp */
97 tmo = usec_to_tick(usec); /* convert usecs to ticks */
98 while ((get_ticks() - start) < tmo)
99 ; /* loop till time has passed */
103 * get_timer(base) can be used to check for timeouts or
104 * to measure elasped time relative to an event:
106 * ulong start_time = get_timer(0) sets start_time to the current
108 * get_timer(start_time) returns the time elapsed since then.
110 * The time is used in CONFIG_SYS_HZ units!
112 ulong get_timer(ulong base)
114 return tick_to_time(get_ticks()) - base;
118 * Return the number of timer ticks per second.
120 ulong get_tbclk(void)
122 return gd->arch.timer_rate_hz;